xref: /freebsd/contrib/llvm-project/lld/MachO/Symbols.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
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 
13 using namespace llvm;
14 using namespace lld;
15 using namespace lld::macho;
16 
17 static_assert(sizeof(void *) != 8 || sizeof(Symbol) == 48,
18               "Try to minimize Symbol's size; we create many instances");
19 
20 // The Microsoft ABI doesn't support using parent class tail padding for child
21 // members, hence the _MSC_VER check.
22 #if !defined(_MSC_VER)
23 static_assert(sizeof(void *) != 8 || sizeof(Defined) == 80,
24               "Try to minimize Defined's size; we create many instances");
25 #endif
26 
27 static_assert(sizeof(SymbolUnion) == sizeof(Defined),
28               "Defined should be the largest Symbol kind");
29 
30 // Returns a symbol for an error message.
31 static std::string demangle(StringRef symName) {
32   if (config->demangle)
33     return demangleItanium(symName);
34   return std::string(symName);
35 }
36 
37 std::string lld::toString(const Symbol &sym) { return demangle(sym.getName()); }
38 
39 std::string lld::toMachOString(const object::Archive::Symbol &b) {
40   return demangle(b.getName());
41 }
42 
43 uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); }
44 uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); }
45 uint64_t Symbol::getTlvVA() const { return in.tlvPointers->getVA(gotIndex); }
46 
47 Defined::Defined(StringRefZ name, InputFile *file, InputSection *isec,
48                  uint64_t value, uint64_t size, bool isWeakDef, bool isExternal,
49                  bool isPrivateExtern, bool isThumb,
50                  bool isReferencedDynamically, bool noDeadStrip,
51                  bool canOverrideWeakDef, bool isWeakDefCanBeHidden)
52     : Symbol(DefinedKind, name, file), overridesWeakDef(canOverrideWeakDef),
53       privateExtern(isPrivateExtern), includeInSymtab(true), thumb(isThumb),
54       referencedDynamically(isReferencedDynamically), noDeadStrip(noDeadStrip),
55       weakDefCanBeHidden(isWeakDefCanBeHidden), weakDef(isWeakDef),
56       external(isExternal), isec(isec), value(value), size(size) {
57   if (isec) {
58     isec->symbols.push_back(this);
59     // Maintain sorted order.
60     for (auto it = isec->symbols.rbegin(), rend = isec->symbols.rend();
61          it != rend; ++it) {
62       auto next = std::next(it);
63       if (next == rend)
64         break;
65       if ((*it)->value < (*next)->value)
66         std::swap(*next, *it);
67       else
68         break;
69     }
70   }
71 }
72 
73 bool Defined::isTlv() const {
74   return !isAbsolute() && isThreadLocalVariables(isec->getFlags());
75 }
76 
77 uint64_t Defined::getVA() const {
78   assert(isLive() && "this should only be called for live symbols");
79 
80   if (isAbsolute())
81     return value;
82 
83   if (!isec->isFinal) {
84     // A target arch that does not use thunks ought never ask for
85     // the address of a function that has not yet been finalized.
86     assert(target->usesThunks());
87 
88     // ConcatOutputSection::finalize() can seek the address of a
89     // function before its address is assigned. The thunking algorithm
90     // knows that unfinalized functions will be out of range, so it is
91     // expedient to return a contrived out-of-range address.
92     return TargetInfo::outOfRangeVA;
93   }
94   return isec->getVA(value);
95 }
96 
97 void Defined::canonicalize() {
98   if (unwindEntry)
99     unwindEntry = unwindEntry->canonical();
100   if (isec)
101     isec = isec->canonical();
102 }
103 
104 uint64_t DylibSymbol::getVA() const {
105   return isInStubs() ? getStubVA() : Symbol::getVA();
106 }
107 
108 void LazySymbol::fetchArchiveMember() { getFile()->fetch(sym); }
109