1 //===- Symbols.cpp --------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "InputFiles.h"
13 #include "InputSection.h"
14 #include "OutputSections.h"
16 #include "SyntheticSections.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/Path.h"
25 using namespace llvm::object;
26 using namespace llvm::ELF;
29 using namespace lld::elf;
31 DefinedRegular *ElfSym::Bss;
32 DefinedRegular *ElfSym::Etext1;
33 DefinedRegular *ElfSym::Etext2;
34 DefinedRegular *ElfSym::Edata1;
35 DefinedRegular *ElfSym::Edata2;
36 DefinedRegular *ElfSym::End1;
37 DefinedRegular *ElfSym::End2;
38 DefinedRegular *ElfSym::MipsGp;
39 DefinedRegular *ElfSym::MipsGpDisp;
40 DefinedRegular *ElfSym::MipsLocalGp;
42 static uint64_t getSymVA(const SymbolBody &Body, int64_t &Addend) {
43 switch (Body.kind()) {
44 case SymbolBody::DefinedRegularKind: {
45 auto &D = cast<DefinedRegular>(Body);
46 SectionBase *IS = D.Section;
47 if (auto *ISB = dyn_cast_or_null<InputSectionBase>(IS))
50 // According to the ELF spec reference to a local symbol from outside
51 // the group are not allowed. Unfortunately .eh_frame breaks that rule
52 // and must be treated specially. For now we just replace the symbol with
54 if (IS == &InputSection::Discarded)
57 // This is an absolute symbol.
61 uint64_t Offset = D.Value;
63 // An object in an SHF_MERGE section might be referenced via a
64 // section symbol (as a hack for reducing the number of local
66 // Depending on the addend, the reference via a section symbol
67 // refers to a different object in the merge section.
68 // Since the objects in the merge section are not necessarily
69 // contiguous in the output, the addend can thus affect the final
70 // VA in a non-linear way.
71 // To make this work, we incorporate the addend into the section
72 // offset (and zero out the addend for later processing) so that
73 // we find the right object in the section.
79 const OutputSection *OutSec = IS->getOutputSection();
81 // In the typical case, this is actually very simple and boils
82 // down to adding together 3 numbers:
83 // 1. The address of the output section.
84 // 2. The offset of the input section within the output section.
85 // 3. The offset within the input section (this addition happens
86 // inside InputSection::getOffset).
88 // If you understand the data structures involved with this next
89 // line (and how they get built), then you have a pretty good
90 // understanding of the linker.
91 uint64_t VA = (OutSec ? OutSec->Addr : 0) + IS->getOffset(Offset);
93 if (D.isTls() && !Config->Relocatable) {
95 fatal(toString(D.File) +
96 " has a STT_TLS symbol but doesn't have a PT_TLS section");
97 return VA - Out::TlsPhdr->p_vaddr;
101 case SymbolBody::DefinedCommonKind:
102 if (!Config->DefineCommon)
104 return InX::Common->OutSec->Addr + InX::Common->OutSecOff +
105 cast<DefinedCommon>(Body).Offset;
106 case SymbolBody::SharedKind: {
107 auto &SS = cast<SharedSymbol>(Body);
109 return SS.CopyRelSec->OutSec->Addr + SS.CopyRelSec->OutSecOff +
112 return Body.getPltVA();
115 case SymbolBody::UndefinedKind:
117 case SymbolBody::LazyArchiveKind:
118 case SymbolBody::LazyObjectKind:
119 assert(Body.symbol()->IsUsedInRegularObj && "lazy symbol reached writer");
122 llvm_unreachable("invalid symbol kind");
125 SymbolBody::SymbolBody(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
127 : SymbolKind(K), NeedsCopy(false), NeedsPltAddr(false), IsLocal(IsLocal),
128 IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
129 IsInIgot(false), Type(Type), StOther(StOther), Name(Name) {}
131 // Returns true if a symbol can be replaced at load-time by a symbol
132 // with the same name defined in other ELF executable or DSO.
133 bool SymbolBody::isPreemptible() const {
137 // Shared symbols resolve to the definition in the DSO. The exceptions are
138 // symbols with copy relocations (which resolve to .bss) or preempt plt
139 // entries (which resolve to that plt entry).
141 return !NeedsCopy && !NeedsPltAddr;
143 // That's all that can be preempted in a non-DSO.
147 // Only symbols that appear in dynsym can be preempted.
148 if (!symbol()->includeInDynsym())
151 // Only default visibility symbols can be preempted.
152 if (symbol()->Visibility != STV_DEFAULT)
155 // -Bsymbolic means that definitions are not preempted.
156 if (Config->Bsymbolic || (Config->BsymbolicFunctions && isFunc()))
161 uint64_t SymbolBody::getVA(int64_t Addend) const {
162 uint64_t OutVA = getSymVA(*this, Addend);
163 return OutVA + Addend;
166 uint64_t SymbolBody::getGotVA() const {
167 return InX::Got->getVA() + getGotOffset();
170 uint64_t SymbolBody::getGotOffset() const {
171 return GotIndex * Target->GotEntrySize;
174 uint64_t SymbolBody::getGotPltVA() const {
176 return InX::IgotPlt->getVA() + getGotPltOffset();
177 return InX::GotPlt->getVA() + getGotPltOffset();
180 uint64_t SymbolBody::getGotPltOffset() const {
181 return GotPltIndex * Target->GotPltEntrySize;
184 uint64_t SymbolBody::getPltVA() const {
186 return InX::Iplt->getVA() + PltIndex * Target->PltEntrySize;
187 return InX::Plt->getVA() + Target->PltHeaderSize +
188 PltIndex * Target->PltEntrySize;
191 template <class ELFT> typename ELFT::uint SymbolBody::getSize() const {
192 if (const auto *C = dyn_cast<DefinedCommon>(this))
194 if (const auto *DR = dyn_cast<DefinedRegular>(this))
196 if (const auto *S = dyn_cast<SharedSymbol>(this))
197 return S->getSize<ELFT>();
201 OutputSection *SymbolBody::getOutputSection() const {
202 if (auto *S = dyn_cast<DefinedRegular>(this)) {
204 return S->Section->getOutputSection();
208 if (auto *S = dyn_cast<SharedSymbol>(this)) {
210 return S->CopyRelSec->OutSec;
214 if (isa<DefinedCommon>(this)) {
215 if (Config->DefineCommon)
216 return InX::Common->OutSec;
223 // If a symbol name contains '@', the characters after that is
224 // a symbol version name. This function parses that.
225 void SymbolBody::parseSymbolVersion() {
226 StringRef S = getName();
227 size_t Pos = S.find('@');
228 if (Pos == 0 || Pos == StringRef::npos)
230 StringRef Verstr = S.substr(Pos + 1);
234 // Truncate the symbol name so that it doesn't include the version string.
235 Name = {S.data(), Pos};
237 // If this is not in this DSO, it is not a definition.
238 if (!isInCurrentDSO())
241 // '@@' in a symbol name means the default version.
242 // It is usually the most recent one.
243 bool IsDefault = (Verstr[0] == '@');
245 Verstr = Verstr.substr(1);
247 for (VersionDefinition &Ver : Config->VersionDefinitions) {
248 if (Ver.Name != Verstr)
252 symbol()->VersionId = Ver.Id;
254 symbol()->VersionId = Ver.Id | VERSYM_HIDDEN;
258 // It is an error if the specified version is not defined.
259 error(toString(File) + ": symbol " + S + " has undefined version " + Verstr);
262 Defined::Defined(Kind K, StringRefZ Name, bool IsLocal, uint8_t StOther,
264 : SymbolBody(K, Name, IsLocal, StOther, Type) {}
266 template <class ELFT> bool DefinedRegular::isMipsPIC() const {
267 if (!Section || !isFunc())
269 return (this->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC ||
270 (cast<InputSectionBase>(Section)
271 ->template getFile<ELFT>()
278 Undefined::Undefined(StringRefZ Name, bool IsLocal, uint8_t StOther,
279 uint8_t Type, InputFile *File)
280 : SymbolBody(SymbolBody::UndefinedKind, Name, IsLocal, StOther, Type) {
284 DefinedCommon::DefinedCommon(StringRef Name, uint64_t Size, uint32_t Alignment,
285 uint8_t StOther, uint8_t Type, InputFile *File)
286 : Defined(SymbolBody::DefinedCommonKind, Name, /*IsLocal=*/false, StOther,
288 Alignment(Alignment), Size(Size) {
292 // If a shared symbol is referred via a copy relocation, its alignment
293 // becomes part of the ABI. This function returns a symbol alignment.
294 // Because symbols don't have alignment attributes, we need to infer that.
295 template <class ELFT> uint32_t SharedSymbol::getAlignment() const {
296 auto *File = cast<SharedFile<ELFT>>(this->File);
297 uint32_t SecAlign = File->getSection(getSym<ELFT>())->sh_addralign;
298 uint64_t SymValue = getSym<ELFT>().st_value;
299 uint32_t SymAlign = uint32_t(1) << countTrailingZeros(SymValue);
300 return std::min(SecAlign, SymAlign);
303 InputFile *Lazy::fetch() {
304 if (auto *S = dyn_cast<LazyArchive>(this))
306 return cast<LazyObject>(this)->fetch();
309 LazyArchive::LazyArchive(ArchiveFile &File,
310 const llvm::object::Archive::Symbol S, uint8_t Type)
311 : Lazy(LazyArchiveKind, S.getName(), Type), Sym(S) {
315 LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
316 : Lazy(LazyObjectKind, Name, Type) {
320 InputFile *LazyArchive::fetch() {
321 std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym);
323 // getMember returns an empty buffer if the member was already
324 // read from the library.
325 if (MBInfo.first.getBuffer().empty())
327 return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second);
330 InputFile *LazyObject::fetch() { return file()->fetch(); }
332 uint8_t Symbol::computeBinding() const {
333 if (Config->Relocatable)
335 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
337 if (VersionId == VER_NDX_LOCAL && body()->isInCurrentDSO())
339 if (Config->NoGnuUnique && Binding == STB_GNU_UNIQUE)
344 bool Symbol::includeInDynsym() const {
345 if (computeBinding() == STB_LOCAL)
347 return ExportDynamic || body()->isShared() ||
348 (body()->isUndefined() && Config->Shared);
351 // Print out a log message for --trace-symbol.
352 void elf::printTraceSymbol(Symbol *Sym) {
353 SymbolBody *B = Sym->body();
355 if (B->isUndefined())
356 S = ": reference to ";
357 else if (B->isCommon())
358 S = ": common definition of ";
360 S = ": definition of ";
362 message(toString(B->File) + S + B->getName());
365 // Returns a symbol for an error message.
366 std::string lld::toString(const SymbolBody &B) {
367 if (Config->Demangle)
368 if (Optional<std::string> S = demangle(B.getName()))
373 template uint32_t SymbolBody::template getSize<ELF32LE>() const;
374 template uint32_t SymbolBody::template getSize<ELF32BE>() const;
375 template uint64_t SymbolBody::template getSize<ELF64LE>() const;
376 template uint64_t SymbolBody::template getSize<ELF64BE>() const;
378 template bool DefinedRegular::template isMipsPIC<ELF32LE>() const;
379 template bool DefinedRegular::template isMipsPIC<ELF32BE>() const;
380 template bool DefinedRegular::template isMipsPIC<ELF64LE>() const;
381 template bool DefinedRegular::template isMipsPIC<ELF64BE>() const;
383 template uint32_t SharedSymbol::template getAlignment<ELF32LE>() const;
384 template uint32_t SharedSymbol::template getAlignment<ELF32BE>() const;
385 template uint32_t SharedSymbol::template getAlignment<ELF64LE>() const;
386 template uint32_t SharedSymbol::template getAlignment<ELF64BE>() const;