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