//===- Symbols.cpp --------------------------------------------------------===// // // The LLVM Linker // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "Symbols.h" #include "Error.h" #include "InputFiles.h" #include "InputSection.h" #include "OutputSections.h" #include "Target.h" #include "llvm/ADT/STLExtras.h" using namespace llvm; using namespace llvm::object; using namespace llvm::ELF; using namespace lld; using namespace lld::elf; template static typename ELFT::uint getSymVA(const SymbolBody &Body, typename ELFT::uint &Addend) { typedef typename ELFT::uint uintX_t; switch (Body.kind()) { case SymbolBody::DefinedSyntheticKind: { auto &D = cast>(Body); const OutputSectionBase *Sec = D.Section; if (!Sec) return D.Value; if (D.Value == DefinedSynthetic::SectionEnd) return Sec->getVA() + Sec->getSize(); return Sec->getVA() + D.Value; } case SymbolBody::DefinedRegularKind: { auto &D = cast>(Body); InputSectionBase *SC = D.Section; // According to the ELF spec reference to a local symbol from outside // the group are not allowed. Unfortunately .eh_frame breaks that rule // and must be treated specially. For now we just replace the symbol with // 0. if (SC == &InputSection::Discarded) return 0; // This is an absolute symbol. if (!SC) return D.Value; uintX_t Offset = D.Value; if (D.isSection()) { Offset += Addend; Addend = 0; } uintX_t VA = SC->OutSec->getVA() + SC->getOffset(Offset); if (D.isTls()) return VA - Out::TlsPhdr->p_vaddr; return VA; } case SymbolBody::DefinedCommonKind: return Out::Bss->getVA() + cast(Body).OffsetInBss; case SymbolBody::SharedKind: { auto &SS = cast>(Body); if (!SS.NeedsCopyOrPltAddr) return 0; if (SS.isFunc()) return Body.getPltVA(); return Out::Bss->getVA() + SS.OffsetInBss; } case SymbolBody::UndefinedKind: return 0; case SymbolBody::LazyArchiveKind: case SymbolBody::LazyObjectKind: assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer"); return 0; case SymbolBody::DefinedBitcodeKind: llvm_unreachable("should have been replaced"); } llvm_unreachable("invalid symbol kind"); } SymbolBody::SymbolBody(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type) : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(true), IsInGlobalMipsGot(false), Type(Type), StOther(StOther), NameOffset(NameOffset) {} SymbolBody::SymbolBody(Kind K, StringRef Name, uint8_t StOther, uint8_t Type) : SymbolKind(K), NeedsCopyOrPltAddr(false), IsLocal(false), IsInGlobalMipsGot(false), Type(Type), StOther(StOther), Name({Name.data(), Name.size()}) {} StringRef SymbolBody::getName() const { assert(!isLocal()); return StringRef(Name.S, Name.Len); } void SymbolBody::setName(StringRef S) { Name.S = S.data(); Name.Len = S.size(); } // Returns true if a symbol can be replaced at load-time by a symbol // with the same name defined in other ELF executable or DSO. bool SymbolBody::isPreemptible() const { if (isLocal()) return false; // Shared symbols resolve to the definition in the DSO. The exceptions are // symbols with copy relocations (which resolve to .bss) or preempt plt // entries (which resolve to that plt entry). if (isShared()) return !NeedsCopyOrPltAddr; // That's all that can be preempted in a non-DSO. if (!Config->Shared) return false; // Only symbols that appear in dynsym can be preempted. if (!symbol()->includeInDynsym()) return false; // Only default visibility symbols can be preempted. if (symbol()->Visibility != STV_DEFAULT) return false; // -Bsymbolic means that definitions are not preempted. if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc())) return !isDefined(); return true; } template bool SymbolBody::hasThunk() const { if (auto *DR = dyn_cast>(this)) return DR->ThunkData != nullptr; if (auto *S = dyn_cast>(this)) return S->ThunkData != nullptr; return false; } template typename ELFT::uint SymbolBody::getVA(typename ELFT::uint Addend) const { typename ELFT::uint OutVA = getSymVA(*this, Addend); return OutVA + Addend; } template typename ELFT::uint SymbolBody::getGotVA() const { return Out::Got->getVA() + getGotOffset(); } template typename ELFT::uint SymbolBody::getGotOffset() const { return GotIndex * Target->GotEntrySize; } template typename ELFT::uint SymbolBody::getGotPltVA() const { return Out::GotPlt->getVA() + getGotPltOffset(); } template typename ELFT::uint SymbolBody::getGotPltOffset() const { return GotPltIndex * Target->GotPltEntrySize; } template typename ELFT::uint SymbolBody::getPltVA() const { return Out::Plt->getVA() + Target->PltHeaderSize + PltIndex * Target->PltEntrySize; } template typename ELFT::uint SymbolBody::getThunkVA() const { if (const auto *DR = dyn_cast>(this)) return DR->ThunkData->getVA(); if (const auto *S = dyn_cast>(this)) return S->ThunkData->getVA(); fatal("getThunkVA() not supported for Symbol class\n"); } template typename ELFT::uint SymbolBody::getSize() const { if (const auto *C = dyn_cast(this)) 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; return 0; } Defined::Defined(Kind K, StringRef Name, uint8_t StOther, uint8_t Type) : SymbolBody(K, Name, StOther, Type) {} Defined::Defined(Kind K, uint32_t NameOffset, uint8_t StOther, uint8_t Type) : SymbolBody(K, NameOffset, StOther, Type) {} DefinedBitcode::DefinedBitcode(StringRef Name, uint8_t StOther, uint8_t Type, BitcodeFile *F) : Defined(DefinedBitcodeKind, Name, StOther, Type) { this->File = F; } bool DefinedBitcode::classof(const SymbolBody *S) { return S->kind() == DefinedBitcodeKind; } Undefined::Undefined(StringRef Name, uint8_t StOther, uint8_t Type, InputFile *File) : SymbolBody(SymbolBody::UndefinedKind, Name, StOther, Type) { this->File = File; } Undefined::Undefined(uint32_t NameOffset, uint8_t StOther, uint8_t Type, InputFile *File) : SymbolBody(SymbolBody::UndefinedKind, NameOffset, StOther, Type) { this->File = File; } template DefinedSynthetic::DefinedSynthetic(StringRef N, uintX_t Value, OutputSectionBase *Section) : Defined(SymbolBody::DefinedSyntheticKind, N, STV_HIDDEN, 0 /* Type */), Value(Value), Section(Section) {} DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment, uint8_t StOther, uint8_t Type, InputFile *File) : Defined(SymbolBody::DefinedCommonKind, N, StOther, Type), Alignment(Alignment), Size(Size) { this->File = File; } std::unique_ptr Lazy::fetch() { if (auto *S = dyn_cast(this)) return S->fetch(); return cast(this)->fetch(); } LazyArchive::LazyArchive(ArchiveFile &File, const llvm::object::Archive::Symbol S, uint8_t Type) : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) { this->File = &File; } LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type) : Lazy(LazyObjectKind, Name, Type) { this->File = &File; } std::unique_ptr LazyArchive::fetch() { MemoryBufferRef MBRef = file()->getMember(&Sym); // getMember returns an empty buffer if the member was already // read from the library. if (MBRef.getBuffer().empty()) return std::unique_ptr(nullptr); return createObjectFile(MBRef, file()->getName()); } std::unique_ptr LazyObject::fetch() { MemoryBufferRef MBRef = file()->getBuffer(); if (MBRef.getBuffer().empty()) return std::unique_ptr(nullptr); return createObjectFile(MBRef); } bool Symbol::includeInDynsym() const { if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED) return false; return (ExportDynamic && VersionId != VER_NDX_LOCAL) || body()->isShared() || (body()->isUndefined() && Config->Shared); } // Print out a log message for --trace-symbol. void elf::printTraceSymbol(Symbol *Sym) { SymbolBody *B = Sym->body(); outs() << getFilename(B->File); if (B->isUndefined()) outs() << ": reference to "; else if (B->isCommon()) outs() << ": common definition of "; else outs() << ": definition of "; outs() << B->getName() << "\n"; } template bool SymbolBody::hasThunk() const; template bool SymbolBody::hasThunk() const; template bool SymbolBody::hasThunk() const; template bool SymbolBody::hasThunk() const; template uint32_t SymbolBody::template getVA(uint32_t) const; template uint32_t SymbolBody::template getVA(uint32_t) const; template uint64_t SymbolBody::template getVA(uint64_t) const; template uint64_t SymbolBody::template getVA(uint64_t) const; template uint32_t SymbolBody::template getGotVA() const; template uint32_t SymbolBody::template getGotVA() const; template uint64_t SymbolBody::template getGotVA() const; template uint64_t SymbolBody::template getGotVA() const; template uint32_t SymbolBody::template getGotOffset() const; template uint32_t SymbolBody::template getGotOffset() const; template uint64_t SymbolBody::template getGotOffset() const; template uint64_t SymbolBody::template getGotOffset() const; template uint32_t SymbolBody::template getGotPltVA() const; template uint32_t SymbolBody::template getGotPltVA() const; template uint64_t SymbolBody::template getGotPltVA() const; template uint64_t SymbolBody::template getGotPltVA() const; template uint32_t SymbolBody::template getThunkVA() const; template uint32_t SymbolBody::template getThunkVA() const; template uint64_t SymbolBody::template getThunkVA() const; template uint64_t SymbolBody::template getThunkVA() const; template uint32_t SymbolBody::template getGotPltOffset() const; template uint32_t SymbolBody::template getGotPltOffset() const; template uint64_t SymbolBody::template getGotPltOffset() const; template uint64_t SymbolBody::template getGotPltOffset() const; template uint32_t SymbolBody::template getPltVA() const; template uint32_t SymbolBody::template getPltVA() const; template uint64_t SymbolBody::template getPltVA() const; template uint64_t SymbolBody::template getPltVA() const; template uint32_t SymbolBody::template getSize() const; template uint32_t SymbolBody::template getSize() const; template uint64_t SymbolBody::template getSize() const; template uint64_t SymbolBody::template getSize() const; template class elf::DefinedSynthetic; template class elf::DefinedSynthetic; template class elf::DefinedSynthetic; template class elf::DefinedSynthetic;