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