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 "COFFLinkerContext.h"
11#include "InputFiles.h"
12#include "lld/Common/ErrorHandler.h"
13#include "llvm/Demangle/Demangle.h"
14
15using namespace llvm;
16using namespace llvm::object;
17
18using namespace lld::coff;
19
20namespace lld {
21
22static_assert(sizeof(SymbolUnion) <= 48,
23 "symbols should be optimized for memory usage");
24
25// Returns a symbol name for an error message.
26std::string maybeDemangleSymbol(const COFFLinkerContext &ctx,
27 StringRef symName) {
28 if (ctx.config.demangle) {
29 std::string prefix;
30 StringRef prefixless = symName;
31 if (prefixless.consume_front(Prefix: "__imp_"))
32 prefix = "__declspec(dllimport) ";
33 StringRef demangleInput = prefixless;
34 if (ctx.config.machine == I386)
35 demangleInput.consume_front(Prefix: "_");
36 std::string demangled = demangle(MangledName: demangleInput);
37 if (demangled != demangleInput)
38 return prefix + demangled;
39 return (prefix + prefixless).str();
40 }
41 return std::string(symName);
42}
43std::string toString(const COFFLinkerContext &ctx, coff::Symbol &b) {
44 return maybeDemangleSymbol(ctx, symName: b.getName());
45}
46std::string toCOFFString(const COFFLinkerContext &ctx,
47 const Archive::Symbol &b) {
48 return maybeDemangleSymbol(ctx, symName: b.getName());
49}
50
51const COFFSyncStream &
52coff::operator<<(const COFFSyncStream &s,
53 const llvm::object::Archive::Symbol *sym) {
54 s << maybeDemangleSymbol(ctx: s.ctx, symName: sym->getName());
55 return s;
56}
57
58namespace coff {
59
60void Symbol::computeName() {
61 assert(nameData == nullptr &&
62 "should only compute the name once for DefinedCOFF symbols");
63 auto *d = cast<DefinedCOFF>(Val: this);
64 StringRef nameStr =
65 check(e: cast<ObjFile>(Val: d->file)->getCOFFObj()->getSymbolName(Symbol: d->sym));
66 nameData = nameStr.data();
67 nameSize = nameStr.size();
68 assert(nameSize == nameStr.size() && "name length truncated");
69}
70
71InputFile *Symbol::getFile() {
72 if (auto *sym = dyn_cast<DefinedCOFF>(Val: this))
73 return sym->file;
74 if (auto *sym = dyn_cast<LazyArchive>(Val: this))
75 return sym->file;
76 if (auto *sym = dyn_cast<LazyObject>(Val: this))
77 return sym->file;
78 if (auto *sym = dyn_cast<LazyDLLSymbol>(Val: this))
79 return sym->file;
80 return nullptr;
81}
82
83bool Symbol::isLive() const {
84 if (auto *r = dyn_cast<DefinedRegular>(Val: this))
85 return r->getChunk()->live;
86 if (auto *imp = dyn_cast<DefinedImportData>(Val: this))
87 return imp->file->live;
88 if (auto *imp = dyn_cast<DefinedImportThunk>(Val: this))
89 return imp->getChunk()->live;
90 // Assume any other kind of symbol is live.
91 return true;
92}
93
94Defined *Symbol::getDefined() {
95 if (auto d = dyn_cast<Defined>(Val: this))
96 return d;
97 if (auto u = dyn_cast<Undefined>(Val: this))
98 return u->getDefinedWeakAlias();
99 return nullptr;
100}
101
102void Symbol::replaceKeepingName(Symbol *other, size_t size) {
103 StringRef origName = getName();
104 memcpy(dest: this, src: other, n: size);
105 nameData = origName.data();
106 nameSize = origName.size();
107}
108
109COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
110 size_t symSize = cast<ObjFile>(Val: file)->getCOFFObj()->getSymbolTableEntrySize();
111 if (symSize == sizeof(coff_symbol16))
112 return COFFSymbolRef(reinterpret_cast<const coff_symbol16 *>(sym));
113 assert(symSize == sizeof(coff_symbol32));
114 return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(sym));
115}
116
117uint64_t DefinedAbsolute::getRVA() { return va - ctx.config.imageBase; }
118
119DefinedImportThunk::DefinedImportThunk(COFFLinkerContext &ctx, StringRef name,
120 DefinedImportData *s,
121 ImportThunkChunk *chunk)
122 : Defined(DefinedImportThunkKind, name), wrappedSym(s), data(chunk) {}
123
124Symbol *Undefined::getWeakAlias() {
125 // A weak alias may be a weak alias to another symbol, so check recursively.
126 DenseSet<Symbol *> weakChain;
127 for (Symbol *a = weakAlias; a; a = cast<Undefined>(Val: a)->weakAlias) {
128 // Anti-dependency symbols can't be chained.
129 if (a->isAntiDep)
130 break;
131 if (!isa<Undefined>(Val: a))
132 return a;
133 if (!weakChain.insert(V: a).second)
134 break; // We have a cycle.
135 }
136 return nullptr;
137}
138
139bool Undefined::resolveWeakAlias() {
140 Defined *d = getDefinedWeakAlias();
141 if (!d)
142 return false;
143
144 // We want to replace Sym with D. However, we can't just blindly
145 // copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
146 // internal symbol, and internal symbols are stored as "unparented"
147 // Symbols. For that reason we need to check which type of symbol we
148 // are dealing with and copy the correct number of bytes.
149 StringRef name = getName();
150 bool wasAntiDep = isAntiDep;
151 if (isa<DefinedRegular>(Val: d))
152 memcpy(dest: this, src: d, n: sizeof(DefinedRegular));
153 else if (isa<DefinedAbsolute>(Val: d))
154 memcpy(dest: this, src: d, n: sizeof(DefinedAbsolute));
155 else
156 memcpy(dest: this, src: d, n: sizeof(SymbolUnion));
157
158 nameData = name.data();
159 nameSize = name.size();
160 isAntiDep = wasAntiDep;
161 return true;
162}
163
164MemoryBufferRef LazyArchive::getMemberBuffer() {
165 Archive::Child c =
166 CHECK(sym.getMember(), "could not get the member for symbol " +
167 toCOFFString(file->symtab.ctx, sym));
168 return CHECK(c.getMemoryBufferRef(),
169 "could not get the buffer for the member defining symbol " +
170 toCOFFString(file->symtab.ctx, sym));
171}
172} // namespace coff
173} // namespace lld
174