1 //===- Symbols.cpp --------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "Symbols.h"
10 #include "InputFiles.h"
11 #include "SyntheticSections.h"
12 #include "llvm/Demangle/Demangle.h"
13
14 using namespace llvm;
15 using namespace lld;
16 using namespace lld::macho;
17
18 static_assert(sizeof(void *) != 8 || sizeof(Symbol) == 56,
19 "Try to minimize Symbol's size; we create many instances");
20
21 // The Microsoft ABI doesn't support using parent class tail padding for child
22 // members, hence the _MSC_VER check.
23 #if !defined(_MSC_VER)
24 static_assert(sizeof(void *) != 8 || sizeof(Defined) == 88,
25 "Try to minimize Defined's size; we create many instances");
26 #endif
27
28 static_assert(sizeof(SymbolUnion) == sizeof(Defined),
29 "Defined should be the largest Symbol kind");
30
31 // Returns a symbol name for an error message.
maybeDemangleSymbol(StringRef symName)32 static std::string maybeDemangleSymbol(StringRef symName) {
33 if (config->demangle) {
34 symName.consume_front("_");
35 return demangle(symName);
36 }
37 return symName.str();
38 }
39
toString(const Symbol & sym)40 std::string lld::toString(const Symbol &sym) {
41 return maybeDemangleSymbol(sym.getName());
42 }
43
toMachOString(const object::Archive::Symbol & b)44 std::string lld::toMachOString(const object::Archive::Symbol &b) {
45 return maybeDemangleSymbol(b.getName());
46 }
47
getStubVA() const48 uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); }
getLazyPtrVA() const49 uint64_t Symbol::getLazyPtrVA() const {
50 return in.lazyPointers->getVA(stubsIndex);
51 }
getGotVA() const52 uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); }
getTlvVA() const53 uint64_t Symbol::getTlvVA() const { return in.tlvPointers->getVA(gotIndex); }
54
Defined(StringRefZ name,InputFile * file,InputSection * isec,uint64_t value,uint64_t size,bool isWeakDef,bool isExternal,bool isPrivateExtern,bool includeInSymtab,bool isReferencedDynamically,bool noDeadStrip,bool canOverrideWeakDef,bool isWeakDefCanBeHidden,bool interposable)55 Defined::Defined(StringRefZ name, InputFile *file, InputSection *isec,
56 uint64_t value, uint64_t size, bool isWeakDef, bool isExternal,
57 bool isPrivateExtern, bool includeInSymtab,
58 bool isReferencedDynamically, bool noDeadStrip,
59 bool canOverrideWeakDef, bool isWeakDefCanBeHidden,
60 bool interposable)
61 : Symbol(DefinedKind, name, file), overridesWeakDef(canOverrideWeakDef),
62 privateExtern(isPrivateExtern), includeInSymtab(includeInSymtab),
63 wasIdenticalCodeFolded(false),
64 referencedDynamically(isReferencedDynamically), noDeadStrip(noDeadStrip),
65 interposable(interposable), weakDefCanBeHidden(isWeakDefCanBeHidden),
66 weakDef(isWeakDef), external(isExternal), originalIsec(isec),
67 value(value), size(size) {
68 if (isec) {
69 isec->symbols.push_back(this);
70 // Maintain sorted order.
71 for (auto it = isec->symbols.rbegin(), rend = isec->symbols.rend();
72 it != rend; ++it) {
73 auto next = std::next(it);
74 if (next == rend)
75 break;
76 if ((*it)->value < (*next)->value)
77 std::swap(*next, *it);
78 else
79 break;
80 }
81 }
82 }
83
isTlv() const84 bool Defined::isTlv() const {
85 return !isAbsolute() && isThreadLocalVariables(originalIsec->getFlags());
86 }
87
getVA() const88 uint64_t Defined::getVA() const {
89 assert(isLive() && "this should only be called for live symbols");
90
91 if (isAbsolute())
92 return value;
93
94 if (!isec()->isFinal) {
95 // A target arch that does not use thunks ought never ask for
96 // the address of a function that has not yet been finalized.
97 assert(target->usesThunks());
98
99 // ConcatOutputSection::finalize() can seek the address of a
100 // function before its address is assigned. The thunking algorithm
101 // knows that unfinalized functions will be out of range, so it is
102 // expedient to return a contrived out-of-range address.
103 return TargetInfo::outOfRangeVA;
104 }
105 return isec()->getVA(value);
106 }
107
getObjectFile() const108 ObjFile *Defined::getObjectFile() const {
109 return originalIsec ? dyn_cast_or_null<ObjFile>(originalIsec->getFile())
110 : nullptr;
111 }
112
getSourceLocation()113 std::string Defined::getSourceLocation() {
114 if (!originalIsec)
115 return {};
116 return originalIsec->getSourceLocation(value);
117 }
118
119 // Get the canonical InputSection of the symbol.
isec() const120 InputSection *Defined::isec() const {
121 return originalIsec ? originalIsec->canonical() : nullptr;
122 }
123
124 // Get the canonical unwind entry of the symbol.
unwindEntry() const125 ConcatInputSection *Defined::unwindEntry() const {
126 return originalUnwindEntry ? originalUnwindEntry->canonical() : nullptr;
127 }
128
getVA() const129 uint64_t DylibSymbol::getVA() const {
130 return isInStubs() ? getStubVA() : Symbol::getVA();
131 }
132
fetchArchiveMember()133 void LazyArchive::fetchArchiveMember() { getFile()->fetch(sym); }
134