xref: /freebsd/contrib/llvm-project/lld/MachO/Symbols.cpp (revision 3078531de10dcae44b253a35125c949ff4235284)
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) == 48,
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) == 80,
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 isThumb,
46                  bool isReferencedDynamically, bool noDeadStrip,
47                  bool canOverrideWeakDef, bool isWeakDefCanBeHidden)
48     : Symbol(DefinedKind, name, file), overridesWeakDef(canOverrideWeakDef),
49       privateExtern(isPrivateExtern), includeInSymtab(true), thumb(isThumb),
50       referencedDynamically(isReferencedDynamically), noDeadStrip(noDeadStrip),
51       weakDefCanBeHidden(isWeakDefCanBeHidden), weakDef(isWeakDef),
52       external(isExternal), isec(isec), value(value), size(size) {
53   if (isec) {
54     isec->symbols.push_back(this);
55     // Maintain sorted order.
56     for (auto it = isec->symbols.rbegin(), rend = isec->symbols.rend();
57          it != rend; ++it) {
58       auto next = std::next(it);
59       if (next == rend)
60         break;
61       if ((*it)->value < (*next)->value)
62         std::swap(*next, *it);
63       else
64         break;
65     }
66   }
67 }
68 
69 bool Defined::isTlv() const {
70   return !isAbsolute() && isThreadLocalVariables(isec->getFlags());
71 }
72 
73 uint64_t Defined::getVA() const {
74   assert(isLive() && "this should only be called for live symbols");
75 
76   if (isAbsolute())
77     return value;
78 
79   if (!isec->isFinal) {
80     // A target arch that does not use thunks ought never ask for
81     // the address of a function that has not yet been finalized.
82     assert(target->usesThunks());
83 
84     // ConcatOutputSection::finalize() can seek the address of a
85     // function before its address is assigned. The thunking algorithm
86     // knows that unfinalized functions will be out of range, so it is
87     // expedient to return a contrived out-of-range address.
88     return TargetInfo::outOfRangeVA;
89   }
90   return isec->getVA(value);
91 }
92 
93 void Defined::canonicalize() {
94   if (unwindEntry)
95     unwindEntry = unwindEntry->canonical();
96   if (isec)
97     isec = isec->canonical();
98 }
99 
100 uint64_t DylibSymbol::getVA() const {
101   return isInStubs() ? getStubVA() : Symbol::getVA();
102 }
103 
104 void LazyArchive::fetchArchiveMember() { getFile()->fetch(sym); }
105