From 022d8e8a86934d19d0173517c828c505e6114db5 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 24 May 2016 03:36:07 +0000 Subject: [PATCH] Make scanReloc and related functions non-member functions. scanReloc does not depend on Writer, so it doesn't have to be in the class. llvm-svn: 270530 --- lld/ELF/OutputSections.h | 4 ++ lld/ELF/Writer.cpp | 81 ++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 45 deletions(-) diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index 21a591a88218..e2bf3949d648 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -134,6 +134,10 @@ public: uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; } uint32_t getTlsIndexOff() { return TlsIndexOff; } + // Flag to force GOT to be in output if we have relocations + // that relies on its address. + bool HasGotOffRel = false; + private: std::vector Entries; uint32_t TlsIndexOff = -1; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 0f158238b709..4300431bcece 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -64,17 +64,6 @@ private: void addPredefinedSections(); bool needsGot(); - template - void scanRelocs(InputSectionBase &C, ArrayRef Rels); - - void scanRelocs(InputSection &C); - void scanRelocs(InputSectionBase &S, const Elf_Shdr &RelSec); - RelExpr adjustExpr(const elf::ObjectFile &File, SymbolBody &S, - bool IsWrite, RelExpr Expr, uint32_t Type); - template - uintX_t computeAddend(const elf::ObjectFile &File, - const uint8_t *SectionData, const RelTy *End, - const RelTy &RI, RelExpr Expr, SymbolBody &Body); void createPhdrs(); void assignAddresses(); void assignFileOffsets(); @@ -94,12 +83,8 @@ private: bool isOutputDynamic() const { return !Symtab.getSharedFiles().empty() || Config->Pic; } - template - void scanRelocsForThunks(const elf::ObjectFile &File, - ArrayRef Rels); void addCommonSymbols(std::vector &Syms); - void addCopyRelSymbol(SharedSymbol *Sym); std::unique_ptr Buffer; @@ -116,10 +101,6 @@ private: uintX_t FileSize; uintX_t SectionHeaderOff; - - // Flag to force GOT to be in output if we have relocations - // that relies on its address. - bool HasGotOffRel = false; }; } // anonymous namespace @@ -370,10 +351,9 @@ static unsigned handleTlsRelocation(uint32_t Type, SymbolBody &Body, // Some targets might require creation of thunks for relocations. Now we // support only MIPS which requires LA25 thunk to call PIC code from non-PIC // one. Scan relocations to find each one requires thunk. -template -template -void Writer::scanRelocsForThunks(const elf::ObjectFile &File, - ArrayRef Rels) { +template +static void scanRelocsForThunks(const elf::ObjectFile &File, + ArrayRef Rels) { for (const RelTy &RI : Rels) { uint32_t Type = RI.getType(Config->Mips64EL); SymbolBody &Body = File.getRelocTargetSym(RI); @@ -522,10 +502,11 @@ static RelExpr fromPlt(RelExpr Expr) { return Expr; } +template static void addCopyRelSymbol(SharedSymbol *SS); + template -RelExpr Writer::adjustExpr(const elf::ObjectFile &File, - SymbolBody &Body, bool IsWrite, RelExpr Expr, - uint32_t Type) { +static RelExpr adjustExpr(const elf::ObjectFile &File, SymbolBody &Body, + bool IsWrite, RelExpr Expr, uint32_t Type) { if (Target->needsThunk(Type, File, Body)) return R_THUNK; bool Preemptible = Body.isPreemptible(); @@ -586,12 +567,13 @@ RelExpr Writer::adjustExpr(const elf::ObjectFile &File, return Expr; } -template -template -typename ELFT::uint -Writer::computeAddend(const elf::ObjectFile &File, - const uint8_t *SectionData, const RelTy *End, - const RelTy &RI, RelExpr Expr, SymbolBody &Body) { +template +static typename ELFT::uint computeAddend(const elf::ObjectFile &File, + const uint8_t *SectionData, + const RelTy *End, const RelTy &RI, + RelExpr Expr, SymbolBody &Body) { + typedef typename ELFT::uint uintX_t; + uint32_t Type = RI.getType(Config->Mips64EL); uintX_t Addend = getAddend(RI); const uint8_t *BufLoc = SectionData + RI.r_offset; @@ -632,9 +614,10 @@ Writer::computeAddend(const elf::ObjectFile &File, // sections. Given that it is ro, we will need an extra PT_LOAD. This // complicates things for the dynamic linker and means we would have to reserve // space for the extra PT_LOAD even if we end up not using it. -template -template -void Writer::scanRelocs(InputSectionBase &C, ArrayRef Rels) { +template +void scanRelocs(InputSectionBase &C, ArrayRef Rels) { + typedef typename ELFT::uint uintX_t; + uintX_t Flags = C.getSectionHdr()->sh_flags; bool IsWrite = Flags & SHF_WRITE; @@ -667,7 +650,7 @@ void Writer::scanRelocs(InputSectionBase &C, ArrayRef Rels) { // This relocation does not require got entry, but it is relative to got and // needs it to be created. Here we request for that. if (Expr == R_GOTONLY_PC || Expr == R_GOTREL || Expr == R_PPC_TOC) - HasGotOffRel = true; + Out::Got->HasGotOffRel = true; uintX_t Addend = computeAddend(File, Buf, E, RI, Expr, Body); @@ -774,10 +757,16 @@ void Writer::scanRelocs(InputSectionBase &C, ArrayRef Rels) { // Scan relocations for necessary thunks. if (Config->EMachine == EM_MIPS) - scanRelocsForThunks(File, Rels); + scanRelocsForThunks(File, Rels); } -template void Writer::scanRelocs(InputSection &C) { +template +static void scanRelocs(InputSectionBase &S, + const typename ELFT::Shdr &RelSec); + +template static void scanRelocs(InputSection &C) { + typedef typename ELFT::Shdr Elf_Shdr; + // Scan all relocations. Each relocation goes through a series // of tests to determine if it needs special treatment, such as // creating GOT, PLT, copy relocations, etc. @@ -789,8 +778,8 @@ template void Writer::scanRelocs(InputSection &C) { } template -void Writer::scanRelocs(InputSectionBase &S, - const Elf_Shdr &RelSec) { +static void scanRelocs(InputSectionBase &S, + const typename ELFT::Shdr &RelSec) { ELFFile &EObj = S.getFile()->getObj(); if (RelSec.sh_type == SHT_RELA) scanRelocs(S, EObj.relas(&RelSec)); @@ -1031,7 +1020,7 @@ void Writer::addCommonSymbols(std::vector &Syms) { } template static uint32_t getAlignment(SharedSymbol *SS) { - typedef typename ELFFile::uintX_t uintX_t; + typedef typename ELFT::uint uintX_t; uintX_t SecAlign = SS->File->getSection(SS->Sym)->sh_addralign; uintX_t SymValue = SS->Sym.st_value; @@ -1041,8 +1030,10 @@ template static uint32_t getAlignment(SharedSymbol *SS) { } // Reserve space in .bss for copy relocation. -template -void Writer::addCopyRelSymbol(SharedSymbol *SS) { +template static void addCopyRelSymbol(SharedSymbol *SS) { + typedef typename ELFT::uint uintX_t; + typedef typename ELFT::Sym Elf_Sym; + // Copy relocation against zero-sized symbol doesn't make sense. uintX_t SymSize = SS->template getSize(); if (SymSize == 0) @@ -1061,7 +1052,7 @@ void Writer::addCopyRelSymbol(SharedSymbol *SS) { if (S.st_shndx != Shndx || S.st_value != Value) continue; auto *Alias = dyn_cast_or_null>( - Symtab.find(check(S.getName(SS->File->getStringTable())))); + Symtab::X->find(check(S.getName(SS->File->getStringTable())))); if (!Alias) continue; Alias->OffsetInBss = Off; @@ -1458,7 +1449,7 @@ template bool Writer::needsGot() { // If we have a relocation that is relative to GOT (such as GOTOFFREL), // we need to emit a GOT even if it's empty. - return HasGotOffRel; + return Out::Got->HasGotOffRel; } // This function add Out::* sections to OutputSections.