Index: lld/trunk/ELF/OutputSections.h =================================================================== --- lld/trunk/ELF/OutputSections.h +++ lld/trunk/ELF/OutputSections.h @@ -30,7 +30,7 @@ class OutputSection; template class ObjectFile; template class SharedFile; -template class SharedSymbol; +class SharedSymbol; template class DefinedRegular; // This represents a section in an output file. Index: lld/trunk/ELF/Relocations.cpp =================================================================== --- lld/trunk/ELF/Relocations.cpp +++ lld/trunk/ELF/Relocations.cpp @@ -387,23 +387,13 @@ return Expr; } -template static uint32_t getAlignment(SharedSymbol *SS) { - typedef typename ELFT::uint uintX_t; - - uintX_t SecAlign = SS->file()->getSection(SS->Sym)->sh_addralign; - uintX_t SymValue = SS->Sym.st_value; - int TrailingZeros = - std::min(countTrailingZeros(SecAlign), countTrailingZeros(SymValue)); - return 1 << TrailingZeros; -} - -template static bool isReadOnly(SharedSymbol *SS) { - typedef typename ELFT::uint uintX_t; +template static bool isReadOnly(SharedSymbol *SS) { typedef typename ELFT::Phdr Elf_Phdr; + uint64_t Value = SS->getValue(); // Determine if the symbol is read-only by scanning the DSO's program headers. - uintX_t Value = SS->Sym.st_value; - for (const Elf_Phdr &Phdr : check(SS->file()->getObj().program_headers())) + auto *File = cast>(SS->File); + for (const Elf_Phdr &Phdr : check(File->getObj().program_headers())) if ((Phdr.p_type == ELF::PT_LOAD || Phdr.p_type == ELF::PT_GNU_RELRO) && !(Phdr.p_flags & ELF::PF_W) && Value >= Phdr.p_vaddr && Value < Phdr.p_vaddr + Phdr.p_memsz) @@ -417,16 +407,20 @@ // them are copied by a copy relocation, all of them need to be copied. // Otherwise, they would refer different places at runtime. template -static std::vector *> getSymbolsAt(SharedSymbol *SS) { +static std::vector getSymbolsAt(SharedSymbol *SS) { typedef typename ELFT::Sym Elf_Sym; - std::vector *> Ret; - for (const Elf_Sym &S : SS->file()->getGlobalSymbols()) { - if (S.st_shndx != SS->Sym.st_shndx || S.st_value != SS->Sym.st_value) + auto *File = cast>(SS->File); + uint64_t Shndx = SS->getShndx(); + uint64_t Value = SS->getValue(); + + std::vector Ret; + for (const Elf_Sym &S : File->getGlobalSymbols()) { + if (S.st_shndx != Shndx || S.st_value != Value) continue; - StringRef Name = check(S.getName(SS->file()->getStringTable())); + StringRef Name = check(S.getName(File->getStringTable())); SymbolBody *Sym = Symtab::X->find(Name); - if (auto *Alias = dyn_cast_or_null>(Sym)) + if (auto *Alias = dyn_cast_or_null(Sym)) Ret.push_back(Alias); } return Ret; @@ -474,7 +468,7 @@ // to the variable in .bss. This kind of issue is sometimes very hard to // debug. What's a solution? Instead of exporting a varaible V from a DSO, // define an accessor getV(). -template static void addCopyRelSymbol(SharedSymbol *SS) { +template static void addCopyRelSymbol(SharedSymbol *SS) { typedef typename ELFT::uint uintX_t; // Copy relocation against zero-sized symbol doesn't make sense. @@ -484,18 +478,18 @@ // See if this symbol is in a read-only segment. If so, preserve the symbol's // memory protection by reserving space in the .bss.rel.ro section. - bool IsReadOnly = isReadOnly(SS); + bool IsReadOnly = isReadOnly(SS); OutputSection *OSec = IsReadOnly ? Out::BssRelRo : Out::Bss; // Create a SyntheticSection in Out to hold the .bss and the Copy Reloc. auto *ISec = - make>(IsReadOnly, getAlignment(SS), SymSize); + make>(IsReadOnly, SS->getAlignment(), SymSize); OSec->addSection(ISec); // Look through the DSO's dynamic symbol table for aliases and create a // dynamic symbol for each one. This causes the copy relocation to correctly // interpose any aliases. - for (SharedSymbol *Sym : getSymbolsAt(SS)) { + for (SharedSymbol *Sym : getSymbolsAt(SS)) { Sym->NeedsCopy = true; Sym->Section = ISec; Sym->symbol()->IsUsedInRegularObj = true; @@ -540,14 +534,14 @@ } if (Body.isObject()) { // Produce a copy relocation. - auto *B = cast>(&Body); + auto *B = cast(&Body); if (!B->NeedsCopy) { if (Config->ZNocopyreloc) error(S.getLocation(RelOff) + ": unresolvable relocation " + toString(Type) + " against symbol '" + toString(*B) + "'; recompile with -fPIC or remove '-z nocopyreloc'"); - addCopyRelSymbol(B); + addCopyRelSymbol(B); } return Expr; } Index: lld/trunk/ELF/SymbolTable.cpp =================================================================== --- lld/trunk/ELF/SymbolTable.cpp +++ lld/trunk/ELF/SymbolTable.cpp @@ -262,8 +262,8 @@ if (Binding != STB_WEAK) { if (S->body()->isShared() || S->body()->isLazy()) S->Binding = Binding; - if (auto *SS = dyn_cast>(S->body())) - SS->file()->IsUsed = true; + if (auto *SS = dyn_cast(S->body())) + cast>(SS->File)->IsUsed = true; } if (auto *L = dyn_cast(S->body())) { // An undefined weak will not fetch archive members, but we have to remember @@ -413,7 +413,7 @@ } template -void SymbolTable::addShared(SharedFile *F, StringRef Name, +void SymbolTable::addShared(SharedFile *File, StringRef Name, const Elf_Sym &Sym, const typename ELFT::Verdef *Verdef) { // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT @@ -421,15 +421,17 @@ // unchanged. Symbol *S; bool WasInserted; - std::tie(S, WasInserted) = - insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, F); + std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT, + /*CanOmitFromDynSym*/ true, File); // Make sure we preempt DSO symbols with default visibility. if (Sym.getVisibility() == STV_DEFAULT) S->ExportDynamic = true; + if (WasInserted || isa(S->body())) { - replaceBody>(S, F, Name, Sym, Verdef); + replaceBody(S, File, Name, Sym.st_other, Sym.getType(), &Sym, + Verdef); if (!S->isWeak()) - F->IsUsed = true; + File->IsUsed = true; } } Index: lld/trunk/ELF/Symbols.h =================================================================== --- lld/trunk/ELF/Symbols.h +++ lld/trunk/ELF/Symbols.h @@ -241,36 +241,48 @@ } }; -template class SharedSymbol : public Defined { - typedef typename ELFT::Sym Elf_Sym; - typedef typename ELFT::Verdef Elf_Verdef; - typedef typename ELFT::uint uintX_t; - +class SharedSymbol : public Defined { public: static bool classof(const SymbolBody *S) { return S->kind() == SymbolBody::SharedKind; } - SharedSymbol(SharedFile *F, StringRef Name, const Elf_Sym &Sym, - const Elf_Verdef *Verdef) - : Defined(SymbolBody::SharedKind, Name, /*IsLocal=*/false, Sym.st_other, - Sym.getType()), - Sym(Sym), Verdef(Verdef) { + SharedSymbol(InputFile *File, StringRef Name, uint8_t StOther, uint8_t Type, + const void *ElfSym, const void *Verdef) + : Defined(SymbolBody::SharedKind, Name, /*IsLocal=*/false, StOther, Type), + Verdef(Verdef), ElfSym(ElfSym) { // IFuncs defined in DSOs are treated as functions by the static linker. if (isGnuIFunc()) Type = llvm::ELF::STT_FUNC; - this->File = F; + this->File = File; } - SharedFile *file() { return (SharedFile *)this->File; } + template uint64_t getShndx() const { + return getSym().st_shndx; + } - const Elf_Sym &Sym; + template uint64_t getValue() const { + return getSym().st_value; + } + + template uint64_t getSize() const { + return getSym().st_size; + } + + template uint64_t getAlignment() const; // This field is a pointer to the symbol's version definition. - const Elf_Verdef *Verdef; + const void *Verdef; // Section is significant only when NeedsCopy is true. InputSection *Section = nullptr; + +private: + template const typename ELFT::Sym &getSym() const { + return *(const typename ELFT::Sym *)ElfSym; + } + + const void *ElfSym; }; // This class represents a symbol defined in an archive file. It is @@ -405,7 +417,7 @@ // ELFT, and we verify this with the static_asserts in replaceBody. llvm::AlignedCharArrayUnion< DefinedCommon, DefinedRegular, DefinedSynthetic, - Undefined, SharedSymbol, LazyArchive, LazyObject> + Undefined, SharedSymbol, LazyArchive, LazyObject> Body; SymbolBody *body() { return reinterpret_cast(Body.buffer); } Index: lld/trunk/ELF/Symbols.cpp =================================================================== --- lld/trunk/ELF/Symbols.cpp +++ lld/trunk/ELF/Symbols.cpp @@ -78,7 +78,7 @@ return In::Common->OutSec->Addr + In::Common->OutSecOff + cast(Body).Offset; case SymbolBody::SharedKind: { - auto &SS = cast>(Body); + auto &SS = cast(Body); if (SS.NeedsCopy) return SS.Section->OutSec->Addr + SS.Section->OutSecOff; if (SS.NeedsPltAddr) @@ -167,8 +167,8 @@ return C->Size; if (const auto *DR = dyn_cast>(this)) return DR->Size; - if (const auto *S = dyn_cast>(this)) - return S->Sym.st_size; + if (const auto *S = dyn_cast(this)) + return S->getSize(); return 0; } @@ -237,6 +237,17 @@ this->File = File; } +// If a shared symbol is referred via a copy relocation, its alignment +// becomes part of the ABI. This function returns a symbol alignment. +// Because symbols don't have alignment attributes, we need to infer that. +template uint64_t SharedSymbol::getAlignment() const { + auto *File = cast>(this->File); + uint64_t SecAlign = File->getSection(getSym())->sh_addralign; + uint64_t SymValue = getSym().st_value; + uint64_t SymAlign = uint64_t(1) << countTrailingZeros(SymValue); + return std::min(SecAlign, SymAlign); +} + InputFile *Lazy::fetch() { if (auto *S = dyn_cast(this)) return S->fetch(); @@ -348,12 +359,12 @@ template uint64_t SymbolBody::template getSize() const; template uint64_t SymbolBody::template getSize() const; -template class elf::SharedSymbol; -template class elf::SharedSymbol; -template class elf::SharedSymbol; -template class elf::SharedSymbol; - template class elf::DefinedRegular; template class elf::DefinedRegular; template class elf::DefinedRegular; template class elf::DefinedRegular; + +template uint64_t SharedSymbol::template getAlignment() const; +template uint64_t SharedSymbol::template getAlignment() const; +template uint64_t SharedSymbol::template getAlignment() const; +template uint64_t SharedSymbol::template getAlignment() const; Index: lld/trunk/ELF/SyntheticSections.h =================================================================== --- lld/trunk/ELF/SyntheticSections.h +++ lld/trunk/ELF/SyntheticSections.h @@ -668,7 +668,7 @@ public: VersionNeedSection(); - void addSymbol(SharedSymbol *SS); + void addSymbol(SharedSymbol *SS); void finalize() override; void writeTo(uint8_t *Buf) override; size_t getSize() const override; Index: lld/trunk/ELF/SyntheticSections.cpp =================================================================== --- lld/trunk/ELF/SyntheticSections.cpp +++ lld/trunk/ELF/SyntheticSections.cpp @@ -1455,7 +1455,7 @@ return nullptr; return In::Common->OutSec; case SymbolBody::SharedKind: { - auto &SS = cast>(*Sym); + auto &SS = cast(*Sym); if (SS.NeedsCopy) return SS.Section->OutSec; break; @@ -2003,24 +2003,27 @@ } template -void VersionNeedSection::addSymbol(SharedSymbol *SS) { - if (!SS->Verdef) { +void VersionNeedSection::addSymbol(SharedSymbol *SS) { + auto *Ver = reinterpret_cast(SS->Verdef); + if (!Ver) { SS->symbol()->VersionId = VER_NDX_GLOBAL; return; } - SharedFile *F = SS->file(); + + auto *File = cast>(SS->File); + // If we don't already know that we need an Elf_Verneed for this DSO, prepare // to create one by adding it to our needed list and creating a dynstr entry // for the soname. - if (F->VerdefMap.empty()) - Needed.push_back({F, In::DynStrTab->addString(F->getSoName())}); - typename SharedFile::NeededVer &NV = F->VerdefMap[SS->Verdef]; + if (File->VerdefMap.empty()) + Needed.push_back({File, In::DynStrTab->addString(File->getSoName())}); + typename SharedFile::NeededVer &NV = File->VerdefMap[Ver]; // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef, // prepare to create one by allocating a version identifier and creating a // dynstr entry for the version name. if (NV.Index == 0) { - NV.StrTab = In::DynStrTab->addString( - SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name); + NV.StrTab = In::DynStrTab->addString(File->getStringTable().data() + + Ver->getAux()->vda_name); NV.Index = NextIndex++; } SS->symbol()->VersionId = NV.Index; Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -1112,8 +1112,8 @@ if (In::DynSymTab && S->includeInDynsym()) { In::DynSymTab->addGlobal(Body); - if (auto *SS = dyn_cast>(Body)) - if (SS->file()->isNeeded()) + if (auto *SS = dyn_cast(Body)) + if (cast>(SS->File)->isNeeded()) In::VerNeed->addSymbol(SS); } }