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
14using namespace llvm;
15using namespace lld;
16using namespace lld::macho;
17
18static_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)
24static_assert(sizeof(void *) != 8 || sizeof(Defined) == 88,
25 "Try to minimize Defined's size; we create many instances");
26#endif
27
28static_assert(sizeof(SymbolUnion) == sizeof(Defined),
29 "Defined should be the largest Symbol kind");
30
31// Returns a symbol name for an error message.
32static std::string maybeDemangleSymbol(StringRef symName) {
33 if (config->demangle) {
34 symName.consume_front(Prefix: "_");
35 return demangle(MangledName: symName);
36 }
37 return symName.str();
38}
39
40std::string lld::toString(const Symbol &sym) {
41 return maybeDemangleSymbol(symName: sym.getName());
42}
43
44std::string lld::toMachOString(const object::Archive::Symbol &b) {
45 return maybeDemangleSymbol(symName: b.getName());
46}
47
48uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); }
49uint64_t Symbol::getLazyPtrVA() const {
50 return in.lazyPointers->getVA(index: stubsIndex);
51}
52uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); }
53
54Defined::Defined(StringRef name, InputFile *file, InputSection *isec,
55 uint64_t value, uint64_t size, bool isWeakDef, bool isExternal,
56 bool isPrivateExtern, bool includeInSymtab,
57 bool isReferencedDynamically, bool noDeadStrip,
58 bool canOverrideWeakDef, bool isWeakDefCanBeHidden,
59 bool interposable, bool cold)
60 : Symbol(DefinedKind, name, file), overridesWeakDef(canOverrideWeakDef),
61 privateExtern(isPrivateExtern), includeInSymtab(includeInSymtab),
62 identicalCodeFoldingKind(ICFFoldKind::None),
63 referencedDynamically(isReferencedDynamically), noDeadStrip(noDeadStrip),
64 interposable(interposable), weakDefCanBeHidden(isWeakDefCanBeHidden),
65 cold(cold), branchExtensionThunk(false), weakDef(isWeakDef),
66 external(isExternal), originalIsec(isec), value(value), size(size) {
67 if (isec) {
68 isec->symbols.push_back(NewVal: this);
69 // Maintain sorted order.
70 for (auto it = isec->symbols.rbegin(), rend = isec->symbols.rend();
71 it != rend; ++it) {
72 auto next = std::next(x: it);
73 if (next == rend)
74 break;
75 if ((*it)->value < (*next)->value)
76 std::swap(a&: *next, b&: *it);
77 else
78 break;
79 }
80 }
81}
82
83bool Defined::isTlv() const {
84 return !isAbsolute() && isThreadLocalVariables(flags: originalIsec->getFlags());
85}
86
87uint64_t Defined::getVA() const {
88 assert(isLive() && "this should only be called for live symbols");
89
90 if (isAbsolute())
91 return value;
92
93 if (!isec()->isFinal) {
94 // A target arch that does not use thunks ought never ask for
95 // the address of a function that has not yet been finalized.
96 assert(target->usesThunks());
97
98 // ConcatOutputSection::finalize() can seek the address of a
99 // function before its address is assigned. The thunking algorithm
100 // knows that unfinalized functions will be out of range, so it is
101 // expedient to return a contrived out-of-range address.
102 return TargetInfo::outOfRangeVA;
103 }
104 return isec()->getVA(off: value);
105}
106
107ObjFile *Defined::getObjectFile() const {
108 return originalIsec ? dyn_cast_or_null<ObjFile>(Val: originalIsec->getFile())
109 : nullptr;
110}
111
112std::string Defined::getSourceLocation() {
113 if (!originalIsec)
114 return {};
115 return originalIsec->getSourceLocation(off: value);
116}
117
118// Get the canonical InputSection of the symbol.
119InputSection *Defined::isec() const {
120 return originalIsec ? originalIsec->canonical() : nullptr;
121}
122
123// Get the canonical unwind entry of the symbol.
124ConcatInputSection *Defined::unwindEntry() const {
125 return originalUnwindEntry ? originalUnwindEntry->canonical() : nullptr;
126}
127
128uint64_t DylibSymbol::getVA() const {
129 return isInStubs() ? getStubVA() : Symbol::getVA();
130}
131
132void LazyArchive::fetchArchiveMember() { getFile()->fetch(sym); }
133