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 "Config.h"
11#include "InputChunks.h"
12#include "InputElement.h"
13#include "InputFiles.h"
14#include "OutputSections.h"
15#include "OutputSegment.h"
16#include "SymbolTable.h"
17#include "lld/Common/ErrorHandler.h"
18#include "lld/Common/Memory.h"
19#include "llvm/Demangle/Demangle.h"
20
21#define DEBUG_TYPE "lld"
22
23using namespace llvm;
24using namespace llvm::object;
25using namespace llvm::wasm;
26using namespace lld::wasm;
27
28namespace lld {
29std::string toString(const wasm::Symbol &sym) {
30 return maybeDemangleSymbol(name: sym.getName());
31}
32
33std::string maybeDemangleSymbol(StringRef name) {
34 // WebAssembly requires caller and callee signatures to match, so we mangle
35 // `main` in the case where we need to pass it arguments.
36 if (name == "__main_argc_argv")
37 return "main";
38 if (wasm::ctx.arg.demangle)
39 return demangle(MangledName: name);
40 return name.str();
41}
42
43std::string toString(wasm::Symbol::Kind kind) {
44 switch (kind) {
45 case wasm::Symbol::DefinedFunctionKind:
46 return "DefinedFunction";
47 case wasm::Symbol::DefinedDataKind:
48 return "DefinedData";
49 case wasm::Symbol::DefinedGlobalKind:
50 return "DefinedGlobal";
51 case wasm::Symbol::DefinedTableKind:
52 return "DefinedTable";
53 case wasm::Symbol::DefinedTagKind:
54 return "DefinedTag";
55 case wasm::Symbol::UndefinedFunctionKind:
56 return "UndefinedFunction";
57 case wasm::Symbol::UndefinedDataKind:
58 return "UndefinedData";
59 case wasm::Symbol::UndefinedGlobalKind:
60 return "UndefinedGlobal";
61 case wasm::Symbol::UndefinedTableKind:
62 return "UndefinedTable";
63 case wasm::Symbol::UndefinedTagKind:
64 return "UndefinedTag";
65 case wasm::Symbol::LazyKind:
66 return "LazyKind";
67 case wasm::Symbol::SectionKind:
68 return "SectionKind";
69 case wasm::Symbol::OutputSectionKind:
70 return "OutputSectionKind";
71 case wasm::Symbol::SharedFunctionKind:
72 return "SharedFunctionKind";
73 case wasm::Symbol::SharedDataKind:
74 return "SharedDataKind";
75 }
76 llvm_unreachable("invalid symbol kind");
77}
78
79namespace wasm {
80
81WasmSymbolType Symbol::getWasmType() const {
82 if (isa<FunctionSymbol>(Val: this))
83 return WASM_SYMBOL_TYPE_FUNCTION;
84 if (isa<DataSymbol>(Val: this))
85 return WASM_SYMBOL_TYPE_DATA;
86 if (isa<GlobalSymbol>(Val: this))
87 return WASM_SYMBOL_TYPE_GLOBAL;
88 if (isa<TagSymbol>(Val: this))
89 return WASM_SYMBOL_TYPE_TAG;
90 if (isa<TableSymbol>(Val: this))
91 return WASM_SYMBOL_TYPE_TABLE;
92 if (isa<SectionSymbol>(Val: this) || isa<OutputSectionSymbol>(Val: this))
93 return WASM_SYMBOL_TYPE_SECTION;
94 llvm_unreachable("invalid symbol kind");
95}
96
97const WasmSignature *Symbol::getSignature() const {
98 if (auto *f = dyn_cast<FunctionSymbol>(Val: this))
99 return f->signature;
100 if (auto *t = dyn_cast<TagSymbol>(Val: this))
101 return t->signature;
102 if (auto *l = dyn_cast<LazySymbol>(Val: this))
103 return l->signature;
104 return nullptr;
105}
106
107InputChunk *Symbol::getChunk() const {
108 if (auto *f = dyn_cast<DefinedFunction>(Val: this))
109 return f->function;
110 if (auto *f = dyn_cast<UndefinedFunction>(Val: this))
111 if (f->stubFunction)
112 return f->stubFunction->function;
113 if (auto *d = dyn_cast<DefinedData>(Val: this))
114 return d->segment;
115 return nullptr;
116}
117
118bool Symbol::isDiscarded() const {
119 if (InputChunk *c = getChunk())
120 return c->discarded;
121 return false;
122}
123
124bool Symbol::isLive() const {
125 if (auto *g = dyn_cast<DefinedGlobal>(Val: this))
126 return g->global->live;
127 if (auto *t = dyn_cast<DefinedTag>(Val: this))
128 return t->tag->live;
129 if (auto *t = dyn_cast<DefinedTable>(Val: this))
130 return t->table->live;
131 if (InputChunk *c = getChunk())
132 return c->live;
133 return referenced;
134}
135
136void Symbol::markLive() {
137 assert(!isDiscarded());
138 referenced = true;
139 if (file != nullptr && isDefined())
140 file->markLive();
141 if (auto *g = dyn_cast<DefinedGlobal>(Val: this))
142 g->global->live = true;
143 if (auto *t = dyn_cast<DefinedTag>(Val: this))
144 t->tag->live = true;
145 if (auto *t = dyn_cast<DefinedTable>(Val: this))
146 t->table->live = true;
147 if (InputChunk *c = getChunk()) {
148 // Usually, a whole chunk is marked as live or dead, but in mergeable
149 // (splittable) sections, each piece of data has independent liveness bit.
150 // So we explicitly tell it which offset is in use.
151 if (auto *d = dyn_cast<DefinedData>(Val: this)) {
152 if (auto *ms = dyn_cast<MergeInputChunk>(Val: c)) {
153 ms->getSectionPiece(offset: d->value)->live = true;
154 }
155 }
156 c->live = true;
157 }
158}
159
160uint32_t Symbol::getOutputSymbolIndex() const {
161 assert(outputSymbolIndex != INVALID_INDEX || !isLive());
162 return outputSymbolIndex;
163}
164
165void Symbol::setOutputSymbolIndex(uint32_t index) {
166 LLVM_DEBUG(dbgs() << "setOutputSymbolIndex " << name << " -> " << index
167 << "\n");
168 assert(outputSymbolIndex == INVALID_INDEX);
169 outputSymbolIndex = index;
170}
171
172void Symbol::setGOTIndex(uint32_t index) {
173 LLVM_DEBUG(dbgs() << "setGOTIndex " << name << " -> " << index << "\n");
174 assert(gotIndex == INVALID_INDEX);
175 gotIndex = index;
176}
177
178bool Symbol::isWeak() const {
179 return (flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK;
180}
181
182bool Symbol::isLocal() const {
183 return (flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_LOCAL;
184}
185
186bool Symbol::isHidden() const {
187 return (flags & WASM_SYMBOL_VISIBILITY_MASK) == WASM_SYMBOL_VISIBILITY_HIDDEN;
188}
189
190bool Symbol::isTLS() const { return flags & WASM_SYMBOL_TLS; }
191
192void Symbol::setHidden(bool isHidden) {
193 LLVM_DEBUG(dbgs() << "setHidden: " << name << " -> " << isHidden << "\n");
194 flags &= ~WASM_SYMBOL_VISIBILITY_MASK;
195 if (isHidden)
196 flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
197 else
198 flags |= WASM_SYMBOL_VISIBILITY_DEFAULT;
199}
200
201bool Symbol::isImported() const {
202 return isShared() ||
203 (isUndefined() && (importName.has_value() || forceImport));
204}
205
206bool Symbol::isExported() const {
207 if (!isDefined() || isShared() || isLocal())
208 return false;
209
210 // Shared libraries must export all weakly defined symbols
211 // in case they contain the version that will be chosen by
212 // the dynamic linker.
213 if (ctx.arg.shared && isLive() && isWeak() && !isHidden())
214 return true;
215
216 if (ctx.arg.exportAll || (ctx.arg.exportDynamic && !isHidden()))
217 return true;
218
219 return isExportedExplicit();
220}
221
222bool Symbol::isExportedExplicit() const {
223 return forceExport || flags & WASM_SYMBOL_EXPORTED;
224}
225
226bool Symbol::isNoStrip() const { return flags & WASM_SYMBOL_NO_STRIP; }
227
228uint32_t FunctionSymbol::getFunctionIndex() const {
229 if (const auto *u = dyn_cast<UndefinedFunction>(Val: this))
230 if (u->stubFunction)
231 return u->stubFunction->getFunctionIndex();
232 if (functionIndex != INVALID_INDEX)
233 return functionIndex;
234 auto *f = cast<DefinedFunction>(Val: this);
235 return f->function->getFunctionIndex();
236}
237
238void FunctionSymbol::setFunctionIndex(uint32_t index) {
239 LLVM_DEBUG(dbgs() << "setFunctionIndex " << name << " -> " << index << "\n");
240 assert(functionIndex == INVALID_INDEX);
241 functionIndex = index;
242}
243
244bool FunctionSymbol::hasFunctionIndex() const {
245 if (auto *f = dyn_cast<DefinedFunction>(Val: this))
246 return f->function->hasFunctionIndex();
247 return functionIndex != INVALID_INDEX;
248}
249
250uint32_t FunctionSymbol::getTableIndex() const {
251 if (auto *f = dyn_cast<DefinedFunction>(Val: this))
252 return f->function->getTableIndex();
253 assert(tableIndex != INVALID_INDEX);
254 return tableIndex;
255}
256
257bool FunctionSymbol::hasTableIndex() const {
258 if (auto *f = dyn_cast<DefinedFunction>(Val: this))
259 return f->function->hasTableIndex();
260 return tableIndex != INVALID_INDEX;
261}
262
263void FunctionSymbol::setTableIndex(uint32_t index) {
264 // For imports, we set the table index here on the Symbol; for defined
265 // functions we set the index on the InputFunction so that we don't export
266 // the same thing twice (keeps the table size down).
267 if (auto *f = dyn_cast<DefinedFunction>(Val: this)) {
268 f->function->setTableIndex(index);
269 return;
270 }
271 LLVM_DEBUG(dbgs() << "setTableIndex " << name << " -> " << index << "\n");
272 assert(tableIndex == INVALID_INDEX);
273 tableIndex = index;
274}
275
276DefinedFunction::DefinedFunction(StringRef name, uint32_t flags, InputFile *f,
277 InputFunction *function)
278 : FunctionSymbol(name, DefinedFunctionKind, flags, f,
279 function ? &function->signature : nullptr),
280 function(function) {}
281
282uint32_t DefinedFunction::getExportedFunctionIndex() const {
283 return function->getFunctionIndex();
284}
285
286uint64_t DefinedData::getVA(bool absolute) const {
287 LLVM_DEBUG(dbgs() << "getVA: " << getName() << "\n");
288 // TLS symbols (by default) are relative to the start of the TLS output
289 // segment (__tls_base).
290 if (isTLS() && !absolute)
291 return getOutputSegmentOffset();
292 if (segment)
293 return segment->getVA(offset: value);
294 return value;
295}
296
297void DefinedData::setVA(uint64_t value_) {
298 LLVM_DEBUG(dbgs() << "setVA " << name << " -> " << value_ << "\n");
299 assert(!segment);
300 value = value_;
301}
302
303uint64_t DefinedData::getOutputSegmentOffset() const {
304 LLVM_DEBUG(dbgs() << "getOutputSegmentOffset: " << getName() << "\n");
305 return segment->getChunkOffset(offset: value);
306}
307
308uint64_t DefinedData::getOutputSegmentIndex() const {
309 LLVM_DEBUG(dbgs() << "getOutputSegmentIndex: " << getName() << "\n");
310 return segment->outputSeg->index;
311}
312
313uint32_t GlobalSymbol::getGlobalIndex() const {
314 if (auto *f = dyn_cast<DefinedGlobal>(Val: this))
315 return f->global->getAssignedIndex();
316 assert(globalIndex != INVALID_INDEX);
317 return globalIndex;
318}
319
320void GlobalSymbol::setGlobalIndex(uint32_t index) {
321 LLVM_DEBUG(dbgs() << "setGlobalIndex " << name << " -> " << index << "\n");
322 assert(globalIndex == INVALID_INDEX);
323 globalIndex = index;
324}
325
326bool GlobalSymbol::hasGlobalIndex() const {
327 if (auto *f = dyn_cast<DefinedGlobal>(Val: this))
328 return f->global->hasAssignedIndex();
329 return globalIndex != INVALID_INDEX;
330}
331
332DefinedGlobal::DefinedGlobal(StringRef name, uint32_t flags, InputFile *file,
333 InputGlobal *global)
334 : GlobalSymbol(name, DefinedGlobalKind, flags, file,
335 global ? &global->getType() : nullptr),
336 global(global) {}
337
338uint32_t TagSymbol::getTagIndex() const {
339 if (auto *f = dyn_cast<DefinedTag>(Val: this))
340 return f->tag->getAssignedIndex();
341 assert(tagIndex != INVALID_INDEX);
342 return tagIndex;
343}
344
345void TagSymbol::setTagIndex(uint32_t index) {
346 LLVM_DEBUG(dbgs() << "setTagIndex " << name << " -> " << index << "\n");
347 assert(tagIndex == INVALID_INDEX);
348 tagIndex = index;
349}
350
351bool TagSymbol::hasTagIndex() const {
352 if (auto *f = dyn_cast<DefinedTag>(Val: this))
353 return f->tag->hasAssignedIndex();
354 return tagIndex != INVALID_INDEX;
355}
356
357DefinedTag::DefinedTag(StringRef name, uint32_t flags, InputFile *file,
358 InputTag *tag)
359 : TagSymbol(name, DefinedTagKind, flags, file,
360 tag ? &tag->signature : nullptr),
361 tag(tag) {}
362
363void TableSymbol::setLimits(const WasmLimits &limits) {
364 if (auto *t = dyn_cast<DefinedTable>(Val: this))
365 t->table->setLimits(limits);
366 auto *newType = make<WasmTableType>(args: *tableType);
367 newType->Limits = limits;
368 tableType = newType;
369}
370
371uint32_t TableSymbol::getTableNumber() const {
372 if (const auto *t = dyn_cast<DefinedTable>(Val: this))
373 return t->table->getAssignedIndex();
374 assert(tableNumber != INVALID_INDEX);
375 return tableNumber;
376}
377
378void TableSymbol::setTableNumber(uint32_t number) {
379 if (const auto *t = dyn_cast<DefinedTable>(Val: this))
380 return t->table->assignIndex(index: number);
381 LLVM_DEBUG(dbgs() << "setTableNumber " << name << " -> " << number << "\n");
382 assert(tableNumber == INVALID_INDEX);
383 tableNumber = number;
384}
385
386bool TableSymbol::hasTableNumber() const {
387 if (const auto *t = dyn_cast<DefinedTable>(Val: this))
388 return t->table->hasAssignedIndex();
389 return tableNumber != INVALID_INDEX;
390}
391
392DefinedTable::DefinedTable(StringRef name, uint32_t flags, InputFile *file,
393 InputTable *table)
394 : TableSymbol(name, DefinedTableKind, flags, file,
395 table ? &table->getType() : nullptr),
396 table(table) {}
397
398const OutputSectionSymbol *SectionSymbol::getOutputSectionSymbol() const {
399 assert(section->outputSec && section->outputSec->sectionSym);
400 return section->outputSec->sectionSym;
401}
402
403void LazySymbol::extract() {
404 if (file->lazy) {
405 file->lazy = false;
406 symtab->addFile(file, symName: name);
407 }
408}
409
410void LazySymbol::setWeak() {
411 flags |= (flags & ~WASM_SYMBOL_BINDING_MASK) | WASM_SYMBOL_BINDING_WEAK;
412}
413
414void printTraceSymbolUndefined(StringRef name, const InputFile *file) {
415 message(msg: toString(file) + ": reference to " + name);
416}
417
418// Print out a log message for --trace-symbol.
419void printTraceSymbol(Symbol *sym) {
420 // Undefined symbols are traced via printTraceSymbolUndefined
421 if (sym->isUndefined())
422 return;
423
424 std::string s;
425 if (sym->isLazy())
426 s = ": lazy definition of ";
427 else
428 s = ": definition of ";
429
430 message(msg: toString(file: sym->getFile()) + s + sym->getName());
431}
432
433const char *defaultModule = "env";
434const char *functionTableName = "__indirect_function_table";
435const char *memoryName = "memory";
436
437} // namespace wasm
438} // namespace lld
439