Index: test/tools/llvm-objcopy/basic-relocations.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/basic-relocations.test @@ -0,0 +1,91 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy %t %t2 +# RUN: llvm-readobj -relocations %t2 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1000 + AddressAlign: 0x0000000000000010 + Content: "0000000000000000" + - Name: .rel.text + Type: SHT_REL + Link: .symtab + Info: .text + Relocations: + - Offset: 0x1000 + Symbol: foo + Type: R_X86_64_PC32 + - Name: .data + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + Address: 0x2000 + AddressAlign: 0x0000000000000010 + Content: "0000000000000000" + - Name: .rel.data + Type: SHT_REL + Link: .symtab + Info: .data + Relocations: + - Offset: 0x2000 + Symbol: bar + Type: R_X86_64_PC32 + - Name: .rela.data + Type: SHT_RELA + Link: .symtab + Info: .data + Relocations: + - Offset: 0x2000 + Symbol: barA + Type: R_X86_64_PC32 + Addend: 0x17 + - Name: .rela.text + Type: SHT_RELA + Link: .symtab + Info: .text + Relocations: + - Offset: 0x1000 + Symbol: fooA + Type: R_X86_64_PC32 + Addend: 0x13 +Symbols: + Global: + - Name: _start + Type: STT_FUNC + Section: .text + Value: 0x1000 + Size: 4 + - Name: foo + Type: STT_FUNC + Size: 4 + - Name: fooA + Type: STT_FUNC + Size: 4 + - Name: bar + Type: STT_OBJECT + Size: 4 + - Name: barA + Type: STT_OBJECT + Size: 4 + +# CHECK: Relocations [ +# CHECK-NEXT: Section (2) .rel.text { +# CHECK-NEXT: 0x1000 R_X86_64_PC32 foo 0x0 +# CHECK-NEXT: } +# CHECK-NEXT: Section (4) .rel.data { +# CHECK-NEXT: 0x2000 R_X86_64_PC32 bar 0x0 +# CHECK-NEXT: } +# CHECK-NEXT: Section (5) .rela.data { +# CHECK-NEXT: 0x2000 R_X86_64_PC32 barA 0x17 +# CHECK-NEXT: } +# CHECK-NEXT: Section (6) .rela.text { +# CHECK-NEXT: 0x1000 R_X86_64_PC32 fooA 0x13 +# CHECK-NEXT: } +# CHECK-NEXT:] Index: tools/llvm-objcopy/Object.h =================================================================== --- tools/llvm-objcopy/Object.h +++ tools/llvm-objcopy/Object.h @@ -146,6 +146,36 @@ } }; +struct Relocation { + llvm::StringRef Symbol; + uint64_t Offset; + uint64_t Addend; + uint32_t Type; +}; + +template class RelocationSection : public SectionBase { +private: + typedef typename ELFT::Rel Elf_Rel; + typedef typename ELFT::Rela Elf_Rela; + + std::vector Relocations; + SymbolTableSection *Symbols; + SectionBase *SecToApplyRel; + + void writeRel(Elf_Rel *Buf) const; + void writeRela(Elf_Rela *Buf) const; + +public: + void setSymTab(SymbolTableSection *StrTab) { Symbols = StrTab; } + void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } + void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } + void finalize() override; + void writeSection(llvm::FileOutputBuffer &Out) const override; + static bool classof(const SectionBase *S) { + return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA; + } +}; + template class Object { private: typedef std::unique_ptr SecPtr; Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -171,6 +171,36 @@ } } +template +void RelocationSection::writeRel(Elf_Rel *Buf) const { + for (const auto &Reloc : Relocations) { + Buf->r_offset = Reloc.Offset; + Buf->setSymbolAndType(Symbols->findIndex(Reloc.Symbol), Reloc.Type, false); + Buf++; + } +} + +template +void RelocationSection::writeRela(Elf_Rela *Buf) const { + for (const auto &Reloc : Relocations) { + Buf->r_offset = Reloc.Offset; + Buf->r_addend = Reloc.Addend; + Buf->setSymbolAndType(Symbols->findIndex(Reloc.Symbol), Reloc.Type, false); + Buf++; + } +} + +template void RelocationSection::finalize() {} + +template +void RelocationSection::writeSection(llvm::FileOutputBuffer &Out) const { + uint8_t *Buf = Out.getBufferStart() + Offset; + if (Type == SHT_REL) + writeRel(reinterpret_cast(Buf)); + else + writeRela(reinterpret_cast(Buf)); +} + // Returns true IFF a section is wholly inside the range of a segment static bool sectionWithinSegment(const SectionBase &Section, const Segment &Segment) { @@ -243,6 +273,37 @@ const Elf_Shdr &Shdr) { ArrayRef Data; switch (Shdr.sh_type) { + case SHT_RELA: { + auto Sec = make_unique>(); + auto SymTab = unwrapOrError(ElfFile.sections()).begin() + Shdr.sh_link; + StringRef StrTabData = + unwrapOrError(ElfFile.getStringTableForSymtab(*SymTab)); + for (const auto &Rel : unwrapOrError(ElfFile.relas(&Shdr))) { + Relocation ToAdd; + auto Sym = unwrapOrError(ElfFile.getSymbol(SymTab, Rel.getSymbol(false))); + ToAdd.Offset = Rel.r_offset; + ToAdd.Addend = Rel.r_addend; + ToAdd.Type = Rel.getType(false); + ToAdd.Symbol = unwrapOrError(Sym->getName(StrTabData)); + Sec->addRelocation(ToAdd); + } + return Sec; + } + case SHT_REL: { + auto Sec = make_unique>(); + auto SymTab = unwrapOrError(ElfFile.sections()).begin() + Shdr.sh_link; + StringRef StrTabData = + unwrapOrError(ElfFile.getStringTableForSymtab(*SymTab)); + for (const auto &Rel : unwrapOrError(ElfFile.rels(&Shdr))) { + Relocation ToAdd; + auto Sym = unwrapOrError(ElfFile.getSymbol(SymTab, Rel.getSymbol(false))); + ToAdd.Offset = Rel.r_offset; + ToAdd.Type = Rel.getType(false); + ToAdd.Symbol = unwrapOrError(Sym->getName(StrTabData)); + Sec->addRelocation(ToAdd); + } + return Sec; + } case SHT_STRTAB: return llvm::make_unique(); case SHT_SYMTAB: { @@ -286,6 +347,14 @@ // details about symbol tables. if (SymbolTable) initSymbolTable(ElfFile, SymbolTable); + + for (auto &Section : Sections) { + if (auto RelSec = dyn_cast>(Section.get())) { + RelSec->setSymTab( + dyn_cast>(Sections[RelSec->Link - 1].get())); + RelSec->setSection(Sections[RelSec->Info - 1].get()); + } + } } template Object::Object(const ELFObjectFile &Obj) {