diff options
author | Jake Ehrlich <jakehehrlich@google.com> | 2017-09-26 18:02:25 +0000 |
---|---|---|
committer | Jake Ehrlich <jakehehrlich@google.com> | 2017-09-26 18:02:25 +0000 |
commit | 9f1a390f72ea5ee64f3e7859133983e878835c0a (patch) | |
tree | f68df258ececeb26efed3e0db6227150b86f8df2 /llvm/tools/llvm-objcopy/Object.cpp | |
parent | 448e8ad94326713b2ef3d19b7fdc397e81114750 (diff) | |
download | bcm5719-llvm-9f1a390f72ea5ee64f3e7859133983e878835c0a.tar.gz bcm5719-llvm-9f1a390f72ea5ee64f3e7859133983e878835c0a.zip |
[llvm-objcopy] Add support for dynamic relocations
This change adds support for dynamic relocations (allocated
SHT_REL/SHT_RELA sections with a dynamic symbol table as their link).
The binary I added for the test is here:
https://drive.google.com/file/d/0B3gtIAmiMwZXSjJUZE9pUjd4M0k/view?usp=sharing
Unless support for dynamic symbol tables in yaml2obj is added this is
needed.
Differential Revision: https://reviews.llvm.org/D37915
llvm-svn: 314227
Diffstat (limited to 'llvm/tools/llvm-objcopy/Object.cpp')
-rw-r--r-- | llvm/tools/llvm-objcopy/Object.cpp | 47 |
1 files changed, 21 insertions, 26 deletions
diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp index a8cd321086b..cb7fddeee77 100644 --- a/llvm/tools/llvm-objcopy/Object.cpp +++ b/llvm/tools/llvm-objcopy/Object.cpp @@ -206,22 +206,24 @@ void SymbolTableSectionImpl<ELFT>::writeSection( } } -template <class ELFT> -void RelocationSection<ELFT>::initialize(SectionTableRef SecTable) { - setSymTab(SecTable.getSectionOfType<SymbolTableSection>( +template <class SymTabType> +void RelocationSectionBase<SymTabType>::initialize(SectionTableRef SecTable) { + setSymTab(SecTable.getSectionOfType<SymTabType>( 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 <class ELFT> void RelocationSection<ELFT>::finalize() { +template <class SymTabType> void RelocationSectionBase<SymTabType>::finalize() { this->Link = Symbols->Index; - this->Info = SecToApplyRel->Index; + if (SecToApplyRel != nullptr) + this->Info = SecToApplyRel->Index; } template <class ELFT> @@ -252,6 +254,11 @@ void RelocationSection<ELFT>::writeSection(llvm::FileOutputBuffer &Out) const { writeRel(reinterpret_cast<Elf_Rela *>(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<DynamicSymbolTableSection>(S) || isa<DynamicSection>(S); } @@ -401,29 +408,13 @@ SectionBase *SectionTableRef::getSection(uint16_t Index, Twine ErrMsg) { template <class T> T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg, - Twine TypeErrMsg) { + Twine TypeErrMsg) { if (T *Sec = llvm::dyn_cast<T>(getSection(Index, IndexErrMsg))) return Sec; error(TypeErrMsg); } template <class ELFT> -SectionBase *Object<ELFT>::getSection(uint16_t Index, Twine ErrMsg) { - if (Index == SHN_UNDEF || Index > Sections.size()) - error(ErrMsg); - return Sections[Index - 1].get(); -} - -template <class ELFT> -template <class T> -T *Object<ELFT>::getSectionOfType(uint16_t Index, Twine IndexErrMsg, - Twine TypeErrMsg) { - if (T *TSec = llvm::dyn_cast<T>(getSection(Index, IndexErrMsg))) - return TSec; - error(TypeErrMsg); -} - -template <class ELFT> std::unique_ptr<SectionBase> Object<ELFT>::makeSection(const llvm::object::ELFFile<ELFT> &ElfFile, const Elf_Shdr &Shdr) { @@ -431,6 +422,10 @@ Object<ELFT>::makeSection(const llvm::object::ELFFile<ELFT> &ElfFile, 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<DynamicRelocationSection>(Data); + } return llvm::make_unique<RelocationSection<ELFT>>(); case SHT_STRTAB: // If a string table is allocated we don't want to mess with it. That would @@ -517,7 +512,7 @@ SectionTableRef Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) { } if (auto Sec = dyn_cast<SectionWithStrTab>(Section.get())) { - Sec->setStrTab(getSectionOfType<StringTableSection>( + Sec->setStrTab(SecTable.getSectionOfType<StringTableSection>( Sec->Link, "Link field value " + Twine(Sec->Link) + " in section " + Sec->Name + " is invalid", |