From 69b17c35d9ca6bed869378787f10d72f2b2df51b Mon Sep 17 00:00:00 2001 From: George Rimar Date: Tue, 16 May 2017 10:04:42 +0000 Subject: [PATCH] [ELF] - Detemplate access to SymTab, DynSymTab, GnuHashTab. NFC. Follow up for r303150. llvm-svn: 303153 --- lld/ELF/InputSection.cpp | 2 +- lld/ELF/OutputSections.cpp | 2 +- lld/ELF/SyntheticSections.cpp | 34 +++++++++++----------- lld/ELF/Writer.cpp | 68 +++++++++++++++++-------------------------- 4 files changed, 45 insertions(+), 61 deletions(-) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 4e519ca..87896ec 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -324,7 +324,7 @@ void InputSection::copyRelocations(uint8_t *Buf, ArrayRef Rels) { // section, but for --emit-relocs it is an virtual address. P->r_offset = RelocatedSection->OutSec->Addr + RelocatedSection->getOffset(Rel.r_offset); - P->setSymbolAndType(In::SymTab->getSymbolIndex(&Body), Type, + P->setSymbolAndType(InX::SymTab->getSymbolIndex(&Body), Type, Config->IsMips64EL); if (Body.Type == STT_SECTION) { diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index c5db018..dcefd03 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -133,7 +133,7 @@ template void OutputSection::finalize() { if (isa(First)) return; - this->Link = In::SymTab->OutSec->SectionIndex; + this->Link = InX::SymTab->OutSec->SectionIndex; // sh_info for SHT_REL[A] sections should contain the section header index of // the section to which the relocation applies. InputSectionBase *S = First->getRelocatedSection(); diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index a24d6d3..c473dfe 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -298,8 +298,8 @@ SymbolBody *elf::addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value, uint64_t Size, InputSectionBase *Section) { auto *S = make(Name, /*IsLocal*/ true, STV_DEFAULT, Type, Value, Size, Section, nullptr); - if (In::SymTab) - In::SymTab->addSymbol(S); + if (InX::SymTab) + InX::SymTab->addSymbol(S); return S; } @@ -1092,14 +1092,14 @@ template void DynamicSection::finalizeContents() { add({DT_PLTREL, uint64_t(Config->IsRela ? DT_RELA : DT_REL)}); } - add({DT_SYMTAB, In::DynSymTab}); + add({DT_SYMTAB, InX::DynSymTab}); add({DT_SYMENT, sizeof(Elf_Sym)}); add({DT_STRTAB, InX::DynStrTab}); add({DT_STRSZ, InX::DynStrTab->getSize()}); if (!Config->ZText) add({DT_TEXTREL, (uint64_t)0}); - if (In::GnuHashTab) - add({DT_GNU_HASH, In::GnuHashTab}); + if (InX::GnuHashTab) + add({DT_GNU_HASH, InX::GnuHashTab}); if (In::HashTab) add({DT_HASH, In::HashTab}); @@ -1137,12 +1137,12 @@ template void DynamicSection::finalizeContents() { add({DT_MIPS_RLD_VERSION, 1}); add({DT_MIPS_FLAGS, RHF_NOTPOT}); add({DT_MIPS_BASE_ADDRESS, Config->ImageBase}); - add({DT_MIPS_SYMTABNO, In::DynSymTab->getNumSymbols()}); + add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()}); add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()}); if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry()) add({DT_MIPS_GOTSYM, B->DynsymIndex}); else - add({DT_MIPS_GOTSYM, In::DynSymTab->getNumSymbols()}); + add({DT_MIPS_GOTSYM, InX::DynSymTab->getNumSymbols()}); add({DT_PLTGOT, InX::MipsGot}); if (InX::MipsRldMap) add({DT_MIPS_RLD_MAP, InX::MipsRldMap}); @@ -1254,8 +1254,8 @@ template unsigned RelocationSection::getRelocOffset() { } template void RelocationSection::finalizeContents() { - this->Link = In::DynSymTab ? In::DynSymTab->OutSec->SectionIndex - : In::SymTab->OutSec->SectionIndex; + this->Link = InX::DynSymTab ? InX::DynSymTab->OutSec->SectionIndex + : InX::SymTab->OutSec->SectionIndex; // Set required output section properties. this->OutSec->Link = this->Link; @@ -1586,15 +1586,15 @@ HashTableSection::HashTableSection() } template void HashTableSection::finalizeContents() { - this->OutSec->Link = In::DynSymTab->OutSec->SectionIndex; + this->OutSec->Link = InX::DynSymTab->OutSec->SectionIndex; unsigned NumEntries = 2; // nbucket and nchain. - NumEntries += In::DynSymTab->getNumSymbols(); // The chain entries. + NumEntries += InX::DynSymTab->getNumSymbols(); // The chain entries. // Create as many buckets as there are symbols. // FIXME: This is simplistic. We can try to optimize it, but implementing // support for SHT_GNU_HASH is probably even more profitable. - NumEntries += In::DynSymTab->getNumSymbols(); + NumEntries += InX::DynSymTab->getNumSymbols(); this->Size = NumEntries * 4; } @@ -1602,7 +1602,7 @@ template void HashTableSection::writeTo(uint8_t *Buf) { // A 32-bit integer type in the target endianness. typedef typename ELFT::Word Elf_Word; - unsigned NumSymbols = In::DynSymTab->getNumSymbols(); + unsigned NumSymbols = InX::DynSymTab->getNumSymbols(); auto *P = reinterpret_cast(Buf); *P++ = NumSymbols; // nbucket @@ -1611,7 +1611,7 @@ template void HashTableSection::writeTo(uint8_t *Buf) { Elf_Word *Buckets = P; Elf_Word *Chains = P + NumSymbols; - for (const SymbolTableEntry &S : In::DynSymTab->getSymbols()) { + for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) { SymbolBody *Body = S.Symbol; StringRef Name = Body->getName(); unsigned I = Body->DynsymIndex; @@ -2001,16 +2001,16 @@ VersionTableSection::VersionTableSection() template void VersionTableSection::finalizeContents() { // At the moment of june 2016 GNU docs does not mention that sh_link field // should be set, but Sun docs do. Also readelf relies on this field. - this->OutSec->Link = In::DynSymTab->OutSec->SectionIndex; + this->OutSec->Link = InX::DynSymTab->OutSec->SectionIndex; } template size_t VersionTableSection::getSize() const { - return sizeof(Elf_Versym) * (In::DynSymTab->getSymbols().size() + 1); + return sizeof(Elf_Versym) * (InX::DynSymTab->getSymbols().size() + 1); } template void VersionTableSection::writeTo(uint8_t *Buf) { auto *OutVersym = reinterpret_cast(Buf) + 1; - for (const SymbolTableEntry &S : In::DynSymTab->getSymbols()) { + for (const SymbolTableEntry &S : InX::DynSymTab->getSymbols()) { OutVersym->vs_index = S.Symbol->symbol()->VersionId; ++OutVersym; } diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index fe5d55d..4cdfce7 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -368,8 +368,8 @@ template void Writer::createSyntheticSections() { } if (HasDynSymTab) { - In::DynSymTab = make>(*InX::DynStrTab); - Add(In::DynSymTab); + InX::DynSymTab = make>(*InX::DynStrTab); + Add(InX::DynSymTab); In::VerSym = make>(); Add(In::VerSym); @@ -383,8 +383,8 @@ template void Writer::createSyntheticSections() { Add(In::VerNeed); if (Config->GnuHash) { - In::GnuHashTab = make(); - Add(In::GnuHashTab); + InX::GnuHashTab = make(); + Add(InX::GnuHashTab); } if (Config->SysvHash) { @@ -444,8 +444,8 @@ template void Writer::createSyntheticSections() { Add(In::EhFrame); } - if (In::SymTab) - Add(In::SymTab); + if (InX::SymTab) + Add(InX::SymTab); Add(InX::ShStrTab); if (InX::StrTab) Add(InX::StrTab); @@ -503,7 +503,7 @@ static bool includeInSymtab(const SymbolBody &B) { // Local symbols are not in the linker's symbol table. This function scans // each object file's symbol table to copy local symbols to the output. template void Writer::copyLocalSymbols() { - if (!In::SymTab) + if (!InX::SymTab) return; for (elf::ObjectFile *F : Symtab::X->getObjectFiles()) { for (SymbolBody *B : F->getLocalSymbols()) { @@ -521,7 +521,7 @@ template void Writer::copyLocalSymbols() { SectionBase *Sec = DR->Section; if (!shouldKeepInSymtab(Sec, B->getName(), *B)) continue; - In::SymTab->addSymbol(B); + InX::SymTab->addSymbol(B); } } } @@ -541,7 +541,7 @@ template void Writer::addSectionSymbols() { auto *Sym = make("", /*IsLocal=*/true, /*StOther=*/0, STT_SECTION, /*Value=*/0, /*Size=*/0, IS, nullptr); - In::SymTab->addSymbol(Sym); + InX::SymTab->addSymbol(Sym); } } @@ -819,7 +819,7 @@ addOptionalRegular(StringRef Name, SectionBase *Sec, uint64_t Val, // need these symbols, since IRELATIVE relocs are resolved through GOT // and PLT. For details, see http://www.airs.com/blog/archives/403. template void Writer::addRelIpltSymbols() { - if (In::DynSymTab) + if (InX::DynSymTab) return; StringRef S = Config->IsRela ? "__rela_iplt_start" : "__rel_iplt_start"; addOptionalRegular(S, In::RelaIplt, 0, STV_HIDDEN, STB_WEAK); @@ -872,7 +872,7 @@ template void Writer::addReservedSymbols() { // static linking the linker is required to optimize away any references to // __tls_get_addr, so it's not defined anywhere. Create a hidden definition // to avoid the undefined symbol error. - if (!In::DynSymTab) + if (!InX::DynSymTab) Symtab::X->addIgnored("__tls_get_addr"); // __ehdr_start is the location of ELF file headers. Note that we define @@ -1131,7 +1131,7 @@ template void Writer::finalizeSections() { // It should be okay as no one seems to care about the type. // Even the author of gold doesn't remember why gold behaves that way. // https://sourceware.org/ml/binutils/2002-03/msg00360.html - if (In::DynSymTab) + if (InX::DynSymTab) addRegular("_DYNAMIC", InX::Dynamic, 0); // Define __rel[a]_iplt_{start,end} symbols if needed. @@ -1159,11 +1159,11 @@ template void Writer::finalizeSections() { if (!includeInSymtab(*Body)) continue; - if (In::SymTab) - In::SymTab->addSymbol(Body); + if (InX::SymTab) + InX::SymTab->addSymbol(Body); - if (In::DynSymTab && S->includeInDynsym()) { - In::DynSymTab->addSymbol(Body); + if (InX::DynSymTab && S->includeInDynsym()) { + InX::DynSymTab->addSymbol(Body); if (auto *SS = dyn_cast(Body)) if (cast>(SS->File)->isNeeded()) In::VerNeed->addSymbol(SS); @@ -1203,30 +1203,14 @@ template void Writer::finalizeSections() { // Dynamic section must be the last one in this list and dynamic // symbol table section (DynSymTab) must be the first one. - applySynthetic({In::DynSymTab, - InX::Bss, - InX::BssRelRo, - In::GnuHashTab, - In::HashTab, - In::SymTab, - InX::ShStrTab, - InX::StrTab, - In::VerDef, - InX::DynStrTab, - InX::GdbIndex, - InX::Got, - InX::MipsGot, - InX::IgotPlt, - InX::GotPlt, - In::RelaDyn, - In::RelaIplt, - In::RelaPlt, - InX::Plt, - InX::Iplt, - In::EhFrameHdr, - In::VerSym, - In::VerNeed, - InX::Dynamic}, + applySynthetic({InX::DynSymTab, InX::Bss, InX::BssRelRo, + InX::GnuHashTab, In::HashTab, InX::SymTab, + InX::ShStrTab, InX::StrTab, In::VerDef, + InX::DynStrTab, InX::GdbIndex, InX::Got, + InX::MipsGot, InX::IgotPlt, InX::GotPlt, + In::RelaDyn, In::RelaIplt, In::RelaPlt, + InX::Plt, InX::Iplt, In::EhFrameHdr, + In::VerSym, In::VerNeed, InX::Dynamic}, [](SyntheticSection *SS) { SS->finalizeContents(); }); // Some architectures use small displacements for jump instructions. @@ -1257,7 +1241,7 @@ template void Writer::finalizeSections() { [](OutputSection *S) { S->maybeCompress(); }); // createThunks may have added local symbols to the static symbol table - applySynthetic({In::SymTab, InX::ShStrTab, InX::StrTab}, + applySynthetic({InX::SymTab, InX::ShStrTab, InX::StrTab}, [](SyntheticSection *SS) { SS->postThunkContents(); }); } @@ -1392,7 +1376,7 @@ template std::vector Writer::createPhdrs() { Ret.push_back(std::move(TlsHdr)); // Add an entry for .dynamic. - if (In::DynSymTab) + if (InX::DynSymTab) AddHdr(PT_DYNAMIC, InX::Dynamic->OutSec->getPhdrFlags()) ->add(InX::Dynamic->OutSec); -- 2.7.4