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 "lld/Common/Strings.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 std::string lld::toString(const Symbol &sym) {
32   return demangle(sym.getName(), config->demangle);
33 }
34 
35 std::string lld::toMachOString(const object::Archive::Symbol &b) {
36   return demangle(b.getName(), config->demangle);
37 }
38 
39 uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); }
40 uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); }
41 uint64_t Symbol::getTlvVA() const { return in.tlvPointers->getVA(gotIndex); }
42 
43 Defined::Defined(StringRefZ name, InputFile *file, InputSection *isec,
44                  uint64_t value, uint64_t size, bool isWeakDef, bool isExternal,
45                  bool isPrivateExtern, bool includeInSymtab, bool isThumb,
46                  bool isReferencedDynamically, bool noDeadStrip,
47                  bool canOverrideWeakDef, bool isWeakDefCanBeHidden,
48                  bool interposable)
49     : Symbol(DefinedKind, name, file), overridesWeakDef(canOverrideWeakDef),
50       privateExtern(isPrivateExtern), includeInSymtab(includeInSymtab),
51       wasIdenticalCodeFolded(false), thumb(isThumb),
52       referencedDynamically(isReferencedDynamically), noDeadStrip(noDeadStrip),
53       interposable(interposable), weakDefCanBeHidden(isWeakDefCanBeHidden),
54       weakDef(isWeakDef), external(isExternal), isec(isec), value(value),
55       size(size) {
56   if (isec) {
57     isec->symbols.push_back(this);
58     // Maintain sorted order.
59     for (auto it = isec->symbols.rbegin(), rend = isec->symbols.rend();
60          it != rend; ++it) {
61       auto next = std::next(it);
62       if (next == rend)
63         break;
64       if ((*it)->value < (*next)->value)
65         std::swap(*next, *it);
66       else
67         break;
68     }
69   }
70 }
71 
72 bool Defined::isTlv() const {
73   return !isAbsolute() && isThreadLocalVariables(isec->getFlags());
74 }
75 
76 uint64_t Defined::getVA() const {
77   assert(isLive() && "this should only be called for live symbols");
78 
79   if (isAbsolute())
80     return value;
81 
82   if (!isec->isFinal) {
83     // A target arch that does not use thunks ought never ask for
84     // the address of a function that has not yet been finalized.
85     assert(target->usesThunks());
86 
87     // ConcatOutputSection::finalize() can seek the address of a
88     // function before its address is assigned. The thunking algorithm
89     // knows that unfinalized functions will be out of range, so it is
90     // expedient to return a contrived out-of-range address.
91     return TargetInfo::outOfRangeVA;
92   }
93   return isec->getVA(value);
94 }
95 
96 void Defined::canonicalize() {
97   if (unwindEntry)
98     unwindEntry = unwindEntry->canonical();
99   if (isec)
100     isec = isec->canonical();
101 }
102 
103 std::string Defined::getSourceLocation() {
104   if (!isec)
105     return {};
106   return isec->getSourceLocation(value);
107 }
108 
109 uint64_t DylibSymbol::getVA() const {
110   return isInStubs() ? getStubVA() : Symbol::getVA();
111 }
112 
113 void LazyArchive::fetchArchiveMember() { getFile()->fetch(sym); }
114