Index: test/tools/llvm-objcopy/dynamic-relocations.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/dynamic-relocations.test @@ -0,0 +1,19 @@ +# RUN: llvm-objcopy %p/Inputs/dynrel.elf %t +# RUN: llvm-readobj -sections -section-data %t | FileCheck %s + +#CHECK: Name: .rela.plt +#CHECK-NEXT: Type: SHT_RELA +#CHECK-NEXT: Flags [ +#CHECK-NEXT: SHF_ALLOC +#CHECK-NEXT: ] +#CHECK-NEXT: Address: +#CHECK-NEXT: Offset: +#CHECK-NEXT: Size: 24 +#CHECK-NEXT: Link: +#CHECK-NEXT: Info: +#CHECK-NEXT: AddressAlignment: 8 +#CHECK-NEXT: EntrySize: 24 +#CHECK-NEXT: SectionData ( +#CHECK-NEXT: 0000: 18202000 00000000 07000000 01000000 |. .............| +#CHECK-NEXT: 0010: 00000000 00000000 |........| +#CHECK-NEXT: ) Index: tools/llvm-objcopy/Object.h =================================================================== --- tools/llvm-objcopy/Object.h +++ tools/llvm-objcopy/Object.h @@ -191,25 +191,35 @@ uint32_t Type; }; -template class RelocationSection : public SectionBase { +template class RelocationSectionBase : public SectionBase { +private: + SymTabType *Symbols; + SectionBase *SecToApplyRel; + +public: + void setSymTab(SymTabType *StrTab) { Symbols = StrTab; } + void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } + void initialize(SectionTable Obj) override; + void finalize() override; +}; + +template +class RelocationSection : public RelocationSectionBase { private: typedef typename ELFT::Rel Elf_Rel; typedef typename ELFT::Rela Elf_Rela; std::vector Relocations; - SymbolTableSection *Symbols; - SectionBase *SecToApplyRel; template void writeRel(T *Buf) const; public: - void setSymTab(SymbolTableSection *StrTab) { Symbols = StrTab; } - void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } - void initialize(SectionTable Obj) override; - void finalize() override; void writeSection(llvm::FileOutputBuffer &Out) const override; + static bool classof(const SectionBase *S) { + if (S->Flags & llvm::ELF::SHF_ALLOC) + return false; return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA; } }; @@ -243,6 +253,21 @@ } }; +class DynamicRelocationSection + : public RelocationSectionBase { +private: + llvm::ArrayRef Contents; + +public: + DynamicRelocationSection(llvm::ArrayRef Data) : Contents(Data) {} + void writeSection(llvm::FileOutputBuffer &Out) const override; + static bool classof(const SectionBase *S) { + if (!(S->Flags & llvm::ELF::SHF_ALLOC)) + return false; + 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 @@ -206,22 +206,25 @@ } } -template -void RelocationSection::initialize(SectionTable SecTable) { - setSymTab(SecTable.getSectionOfType( + +template +void RelocationSectionBase::initialize(SectionTable SecTable) { + setSymTab(SecTable.getSectionOfType( Link, "Link field value " + Twine(Link) + " in section " + Name + " is invalid", "Link field value " + Twine(Link) + " in section " + Name + " is not a symbol table")); - setSection(SecTable.getSection(Info, - "Info field value " + Twine(Info) + - " in section " + Name + " is invalid")); + if (Info != SHN_UNDEF) + setSection(SecTable.getSection(Info, + "Info field value " + Twine(Info) + + " in section " + Name + " is invalid")); } -template void RelocationSection::finalize() { +template void RelocationSectionBase::finalize() { this->Link = Symbols->Index; - this->Info = SecToApplyRel->Index; + if (SecToApplyRel != nullptr) + this->Info = SecToApplyRel->Index; } template @@ -252,6 +255,11 @@ writeRel(reinterpret_cast(Buf)); } +void DynamicRelocationSection::writeSection(llvm::FileOutputBuffer &Out) const { + std::copy(std::begin(Contents), std::end(Contents), + Out.getBufferStart() + Offset); +} + bool SectionWithStrTab::classof(const SectionBase *S) { return isa(S) || isa(S); } @@ -414,6 +422,10 @@ switch (Shdr.sh_type) { case SHT_REL: case SHT_RELA: + if (Shdr.sh_flags & SHF_ALLOC) { + Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + return llvm::make_unique(Data); + } return llvm::make_unique>(); case SHT_STRTAB: // If a string table is allocated we don't want to mess with it. That would @@ -491,7 +503,6 @@ continue; Section->initialize(SecTable); if (auto RelSec = dyn_cast>(Section.get())) { - auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index; if (RelSec->Type == SHT_REL) initRelocations(RelSec, SymbolTable, unwrapOrError(ElfFile.rels(Shdr)));