xref: /freebsd/contrib/llvm-project/lld/MachO/Symbols.cpp (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
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 // Returns a symbol for an error message.
18 static std::string demangle(StringRef symName) {
19   if (config->demangle)
20     return demangleItanium(symName);
21   return std::string(symName);
22 }
23 
24 std::string lld::toString(const Symbol &sym) { return demangle(sym.getName()); }
25 
26 std::string lld::toMachOString(const object::Archive::Symbol &b) {
27   return demangle(b.getName());
28 }
29 
30 uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); }
31 uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); }
32 uint64_t Symbol::getTlvVA() const { return in.tlvPointers->getVA(gotIndex); }
33 
34 bool Symbol::isLive() const {
35   if (isa<DylibSymbol>(this) || isa<Undefined>(this))
36     return used;
37 
38   if (auto *d = dyn_cast<Defined>(this)) {
39     // Non-absolute symbols might be alive because their section is
40     // no_dead_strip or live_support. In that case, the section will know
41     // that it's live but `used` might be false. Non-absolute symbols always
42     // have to use the section's `live` bit as source of truth.
43     if (d->isAbsolute())
44       return used;
45     return d->isec->canonical()->isLive(d->value);
46   }
47 
48   assert(!isa<CommonSymbol>(this) &&
49          "replaceCommonSymbols() runs before dead code stripping, and isLive() "
50          "should only be called after dead code stripping");
51 
52   // Assume any other kind of symbol is live.
53   return true;
54 }
55 
56 uint64_t Defined::getVA() const {
57   assert(isLive() && "this should only be called for live symbols");
58 
59   if (isAbsolute())
60     return value;
61 
62   if (!isec->canonical()->isFinal) {
63     // A target arch that does not use thunks ought never ask for
64     // the address of a function that has not yet been finalized.
65     assert(target->usesThunks());
66 
67     // ConcatOutputSection::finalize() can seek the address of a
68     // function before its address is assigned. The thunking algorithm
69     // knows that unfinalized functions will be out of range, so it is
70     // expedient to return a contrived out-of-range address.
71     return TargetInfo::outOfRangeVA;
72   }
73   return isec->canonical()->getVA(value);
74 }
75 
76 uint64_t DylibSymbol::getVA() const {
77   return isInStubs() ? getStubVA() : Symbol::getVA();
78 }
79 
80 void LazySymbol::fetchArchiveMember() { getFile()->fetch(sym); }
81