Index: llvm/trunk/test/tools/llvm-objcopy/dynamic-relocations.test =================================================================== --- llvm/trunk/test/tools/llvm-objcopy/dynamic-relocations.test +++ llvm/trunk/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: llvm/trunk/tools/llvm-objcopy/Object.h =================================================================== --- llvm/trunk/tools/llvm-objcopy/Object.h +++ llvm/trunk/tools/llvm-objcopy/Object.h @@ -32,7 +32,6 @@ SectionBase *getSection(uint16_t Index, llvm::Twine ErrMsg); template - T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg, llvm::Twine TypeErrMsg); }; @@ -192,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(SectionTableRef SecTable) 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(SectionTableRef SecTable) 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; } }; @@ -244,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; @@ -260,12 +284,6 @@ void readProgramHeaders(const llvm::object::ELFFile &ElfFile); SectionTableRef readSectionHeaders(const llvm::object::ELFFile &ElfFile); - SectionBase *getSection(uint16_t Index, llvm::Twine ErrMsg); - - template - T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg, - llvm::Twine TypeErrMsg); - protected: StringTableSection *SectionNames; SymbolTableSection *SymbolTable; Index: llvm/trunk/tools/llvm-objcopy/Object.cpp =================================================================== --- llvm/trunk/tools/llvm-objcopy/Object.cpp +++ llvm/trunk/tools/llvm-objcopy/Object.cpp @@ -206,22 +206,24 @@ } } -template -void RelocationSection::initialize(SectionTableRef SecTable) { - setSymTab(SecTable.getSectionOfType( +template +void RelocationSectionBase::initialize(SectionTableRef 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 +254,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); } @@ -401,29 +408,13 @@ template T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg, - Twine TypeErrMsg) { + Twine TypeErrMsg) { if (T *Sec = llvm::dyn_cast(getSection(Index, IndexErrMsg))) return Sec; error(TypeErrMsg); } template -SectionBase *Object::getSection(uint16_t Index, Twine ErrMsg) { - if (Index == SHN_UNDEF || Index > Sections.size()) - error(ErrMsg); - return Sections[Index - 1].get(); -} - -template -template -T *Object::getSectionOfType(uint16_t Index, Twine IndexErrMsg, - Twine TypeErrMsg) { - if (T *TSec = llvm::dyn_cast(getSection(Index, IndexErrMsg))) - return TSec; - error(TypeErrMsg); -} - -template std::unique_ptr Object::makeSection(const llvm::object::ELFFile &ElfFile, const Elf_Shdr &Shdr) { @@ -431,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 @@ -517,7 +512,7 @@ } if (auto Sec = dyn_cast(Section.get())) { - Sec->setStrTab(getSectionOfType( + Sec->setStrTab(SecTable.getSectionOfType( Sec->Link, "Link field value " + Twine(Sec->Link) + " in section " + Sec->Name + " is invalid",