[Clang][SME2] Enable multi-vector loads & stores for SME2 (#75821)
[llvm-project.git] / lld / wasm / SymbolTable.cpp
blob76370525c371995793ff1427f7f09d76402c993b
1 //===- SymbolTable.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 //===----------------------------------------------------------------------===//
9 #include "SymbolTable.h"
10 #include "Config.h"
11 #include "InputChunks.h"
12 #include "InputElement.h"
13 #include "WriterUtils.h"
14 #include "lld/Common/CommonLinkerContext.h"
15 #include <optional>
17 #define DEBUG_TYPE "lld"
19 using namespace llvm;
20 using namespace llvm::wasm;
21 using namespace llvm::object;
23 namespace lld::wasm {
24 SymbolTable *symtab;
26 void SymbolTable::addFile(InputFile *file, StringRef symName) {
27 log("Processing: " + toString(file));
29 // .a file
30 if (auto *f = dyn_cast<ArchiveFile>(file)) {
31 f->parse();
32 return;
35 // .so file
36 if (auto *f = dyn_cast<SharedFile>(file)) {
37 sharedFiles.push_back(f);
38 return;
41 // stub file
42 if (auto *f = dyn_cast<StubFile>(file)) {
43 f->parse();
44 stubFiles.push_back(f);
45 return;
48 if (config->trace)
49 message(toString(file));
51 // LLVM bitcode file
52 if (auto *f = dyn_cast<BitcodeFile>(file)) {
53 // This order, first adding to `bitcodeFiles` and then parsing is necessary.
54 // See https://github.com/llvm/llvm-project/pull/73095
55 bitcodeFiles.push_back(f);
56 f->parse(symName);
57 return;
60 // Regular object file
61 auto *f = cast<ObjFile>(file);
62 f->parse(false);
63 objectFiles.push_back(f);
66 // This function is where all the optimizations of link-time
67 // optimization happens. When LTO is in use, some input files are
68 // not in native object file format but in the LLVM bitcode format.
69 // This function compiles bitcode files into a few big native files
70 // using LLVM functions and replaces bitcode symbols with the results.
71 // Because all bitcode files that the program consists of are passed
72 // to the compiler at once, it can do whole-program optimization.
73 void SymbolTable::compileBitcodeFiles() {
74 // Prevent further LTO objects being included
75 BitcodeFile::doneLTO = true;
77 if (bitcodeFiles.empty())
78 return;
80 // Compile bitcode files and replace bitcode symbols.
81 lto.reset(new BitcodeCompiler);
82 for (BitcodeFile *f : bitcodeFiles)
83 lto->add(*f);
85 for (StringRef filename : lto->compile()) {
86 auto *obj = make<ObjFile>(MemoryBufferRef(filename, "lto.tmp"), "");
87 obj->parse(true);
88 objectFiles.push_back(obj);
92 Symbol *SymbolTable::find(StringRef name) {
93 auto it = symMap.find(CachedHashStringRef(name));
94 if (it == symMap.end() || it->second == -1)
95 return nullptr;
96 return symVector[it->second];
99 void SymbolTable::replace(StringRef name, Symbol* sym) {
100 auto it = symMap.find(CachedHashStringRef(name));
101 symVector[it->second] = sym;
104 std::pair<Symbol *, bool> SymbolTable::insertName(StringRef name) {
105 bool trace = false;
106 auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()});
107 int &symIndex = p.first->second;
108 bool isNew = p.second;
109 if (symIndex == -1) {
110 symIndex = symVector.size();
111 trace = true;
112 isNew = true;
115 if (!isNew)
116 return {symVector[symIndex], false};
118 Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
119 sym->isUsedInRegularObj = false;
120 sym->canInline = true;
121 sym->traced = trace;
122 sym->forceExport = false;
123 sym->referenced = !config->gcSections;
124 symVector.emplace_back(sym);
125 return {sym, true};
128 std::pair<Symbol *, bool> SymbolTable::insert(StringRef name,
129 const InputFile *file) {
130 Symbol *s;
131 bool wasInserted;
132 std::tie(s, wasInserted) = insertName(name);
134 if (!file || file->kind() == InputFile::ObjectKind)
135 s->isUsedInRegularObj = true;
137 return {s, wasInserted};
140 static void reportTypeError(const Symbol *existing, const InputFile *file,
141 llvm::wasm::WasmSymbolType type) {
142 error("symbol type mismatch: " + toString(*existing) + "\n>>> defined as " +
143 toString(existing->getWasmType()) + " in " +
144 toString(existing->getFile()) + "\n>>> defined as " + toString(type) +
145 " in " + toString(file));
148 // Check the type of new symbol matches that of the symbol is replacing.
149 // Returns true if the function types match, false is there is a signature
150 // mismatch.
151 static bool signatureMatches(FunctionSymbol *existing,
152 const WasmSignature *newSig) {
153 const WasmSignature *oldSig = existing->signature;
155 // If either function is missing a signature (this happens for bitcode
156 // symbols) then assume they match. Any mismatch will be reported later
157 // when the LTO objects are added.
158 if (!newSig || !oldSig)
159 return true;
161 return *newSig == *oldSig;
164 static void checkGlobalType(const Symbol *existing, const InputFile *file,
165 const WasmGlobalType *newType) {
166 if (!isa<GlobalSymbol>(existing)) {
167 reportTypeError(existing, file, WASM_SYMBOL_TYPE_GLOBAL);
168 return;
171 const WasmGlobalType *oldType = cast<GlobalSymbol>(existing)->getGlobalType();
172 if (*newType != *oldType) {
173 error("Global type mismatch: " + existing->getName() + "\n>>> defined as " +
174 toString(*oldType) + " in " + toString(existing->getFile()) +
175 "\n>>> defined as " + toString(*newType) + " in " + toString(file));
179 static void checkTagType(const Symbol *existing, const InputFile *file,
180 const WasmSignature *newSig) {
181 const auto *existingTag = dyn_cast<TagSymbol>(existing);
182 if (!isa<TagSymbol>(existing)) {
183 reportTypeError(existing, file, WASM_SYMBOL_TYPE_TAG);
184 return;
187 const WasmSignature *oldSig = existingTag->signature;
188 if (*newSig != *oldSig)
189 warn("Tag signature mismatch: " + existing->getName() +
190 "\n>>> defined as " + toString(*oldSig) + " in " +
191 toString(existing->getFile()) + "\n>>> defined as " +
192 toString(*newSig) + " in " + toString(file));
195 static void checkTableType(const Symbol *existing, const InputFile *file,
196 const WasmTableType *newType) {
197 if (!isa<TableSymbol>(existing)) {
198 reportTypeError(existing, file, WASM_SYMBOL_TYPE_TABLE);
199 return;
202 const WasmTableType *oldType = cast<TableSymbol>(existing)->getTableType();
203 if (newType->ElemType != oldType->ElemType) {
204 error("Table type mismatch: " + existing->getName() + "\n>>> defined as " +
205 toString(*oldType) + " in " + toString(existing->getFile()) +
206 "\n>>> defined as " + toString(*newType) + " in " + toString(file));
208 // FIXME: No assertions currently on the limits.
211 static void checkDataType(const Symbol *existing, const InputFile *file) {
212 if (!isa<DataSymbol>(existing))
213 reportTypeError(existing, file, WASM_SYMBOL_TYPE_DATA);
216 DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name,
217 uint32_t flags,
218 InputFunction *function) {
219 LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n");
220 assert(!find(name));
221 syntheticFunctions.emplace_back(function);
222 return replaceSymbol<DefinedFunction>(insertName(name).first, name,
223 flags, nullptr, function);
226 // Adds an optional, linker generated, data symbol. The symbol will only be
227 // added if there is an undefine reference to it, or if it is explicitly
228 // exported via the --export flag. Otherwise we don't add the symbol and return
229 // nullptr.
230 DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name,
231 uint64_t value) {
232 Symbol *s = find(name);
233 if (!s && (config->exportAll || config->exportedSymbols.count(name) != 0))
234 s = insertName(name).first;
235 else if (!s || s->isDefined())
236 return nullptr;
237 LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n");
238 auto *rtn = replaceSymbol<DefinedData>(
239 s, name, WASM_SYMBOL_VISIBILITY_HIDDEN | WASM_SYMBOL_ABSOLUTE);
240 rtn->setVA(value);
241 rtn->referenced = true;
242 return rtn;
245 DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef name,
246 uint32_t flags) {
247 LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << name << "\n");
248 assert(!find(name));
249 return replaceSymbol<DefinedData>(insertName(name).first, name,
250 flags | WASM_SYMBOL_ABSOLUTE);
253 DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags,
254 InputGlobal *global) {
255 LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global
256 << "\n");
257 assert(!find(name));
258 syntheticGlobals.emplace_back(global);
259 return replaceSymbol<DefinedGlobal>(insertName(name).first, name, flags,
260 nullptr, global);
263 DefinedGlobal *SymbolTable::addOptionalGlobalSymbol(StringRef name,
264 InputGlobal *global) {
265 Symbol *s = find(name);
266 if (!s || s->isDefined())
267 return nullptr;
268 LLVM_DEBUG(dbgs() << "addOptionalGlobalSymbol: " << name << " -> " << global
269 << "\n");
270 syntheticGlobals.emplace_back(global);
271 return replaceSymbol<DefinedGlobal>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN,
272 nullptr, global);
275 DefinedTable *SymbolTable::addSyntheticTable(StringRef name, uint32_t flags,
276 InputTable *table) {
277 LLVM_DEBUG(dbgs() << "addSyntheticTable: " << name << " -> " << table
278 << "\n");
279 Symbol *s = find(name);
280 assert(!s || s->isUndefined());
281 if (!s)
282 s = insertName(name).first;
283 syntheticTables.emplace_back(table);
284 return replaceSymbol<DefinedTable>(s, name, flags, nullptr, table);
287 static bool shouldReplace(const Symbol *existing, InputFile *newFile,
288 uint32_t newFlags) {
289 // If existing symbol is undefined, replace it.
290 if (!existing->isDefined()) {
291 LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
292 << existing->getName() << "\n");
293 return true;
296 // Now we have two defined symbols. If the new one is weak, we can ignore it.
297 if ((newFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
298 LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");
299 return false;
302 // If the existing symbol is weak, we should replace it.
303 if (existing->isWeak()) {
304 LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
305 return true;
308 // Neither symbol is week. They conflict.
309 error("duplicate symbol: " + toString(*existing) + "\n>>> defined in " +
310 toString(existing->getFile()) + "\n>>> defined in " +
311 toString(newFile));
312 return true;
315 Symbol *SymbolTable::addDefinedFunction(StringRef name, uint32_t flags,
316 InputFile *file,
317 InputFunction *function) {
318 LLVM_DEBUG(dbgs() << "addDefinedFunction: " << name << " ["
319 << (function ? toString(function->signature) : "none")
320 << "]\n");
321 Symbol *s;
322 bool wasInserted;
323 std::tie(s, wasInserted) = insert(name, file);
325 auto replaceSym = [&](Symbol *sym) {
326 // If the new defined function doesn't have signature (i.e. bitcode
327 // functions) but the old symbol does, then preserve the old signature
328 const WasmSignature *oldSig = s->getSignature();
329 auto* newSym = replaceSymbol<DefinedFunction>(sym, name, flags, file, function);
330 if (!newSym->signature)
331 newSym->signature = oldSig;
334 if (wasInserted || s->isLazy()) {
335 replaceSym(s);
336 return s;
339 auto existingFunction = dyn_cast<FunctionSymbol>(s);
340 if (!existingFunction) {
341 reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
342 return s;
345 bool checkSig = true;
346 if (auto ud = dyn_cast<UndefinedFunction>(existingFunction))
347 checkSig = ud->isCalledDirectly;
349 if (checkSig && function && !signatureMatches(existingFunction, &function->signature)) {
350 Symbol* variant;
351 if (getFunctionVariant(s, &function->signature, file, &variant))
352 // New variant, always replace
353 replaceSym(variant);
354 else if (shouldReplace(s, file, flags))
355 // Variant already exists, replace it after checking shouldReplace
356 replaceSym(variant);
358 // This variant we found take the place in the symbol table as the primary
359 // variant.
360 replace(name, variant);
361 return variant;
364 // Existing function with matching signature.
365 if (shouldReplace(s, file, flags))
366 replaceSym(s);
368 return s;
371 Symbol *SymbolTable::addDefinedData(StringRef name, uint32_t flags,
372 InputFile *file, InputChunk *segment,
373 uint64_t address, uint64_t size) {
374 LLVM_DEBUG(dbgs() << "addDefinedData:" << name << " addr:" << address
375 << "\n");
376 Symbol *s;
377 bool wasInserted;
378 std::tie(s, wasInserted) = insert(name, file);
380 auto replaceSym = [&]() {
381 replaceSymbol<DefinedData>(s, name, flags, file, segment, address, size);
384 if (wasInserted || s->isLazy()) {
385 replaceSym();
386 return s;
389 checkDataType(s, file);
391 if (shouldReplace(s, file, flags))
392 replaceSym();
393 return s;
396 Symbol *SymbolTable::addDefinedGlobal(StringRef name, uint32_t flags,
397 InputFile *file, InputGlobal *global) {
398 LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << name << "\n");
400 Symbol *s;
401 bool wasInserted;
402 std::tie(s, wasInserted) = insert(name, file);
404 auto replaceSym = [&]() {
405 replaceSymbol<DefinedGlobal>(s, name, flags, file, global);
408 if (wasInserted || s->isLazy()) {
409 replaceSym();
410 return s;
413 checkGlobalType(s, file, &global->getType());
415 if (shouldReplace(s, file, flags))
416 replaceSym();
417 return s;
420 Symbol *SymbolTable::addDefinedTag(StringRef name, uint32_t flags,
421 InputFile *file, InputTag *tag) {
422 LLVM_DEBUG(dbgs() << "addDefinedTag:" << name << "\n");
424 Symbol *s;
425 bool wasInserted;
426 std::tie(s, wasInserted) = insert(name, file);
428 auto replaceSym = [&]() {
429 replaceSymbol<DefinedTag>(s, name, flags, file, tag);
432 if (wasInserted || s->isLazy()) {
433 replaceSym();
434 return s;
437 checkTagType(s, file, &tag->signature);
439 if (shouldReplace(s, file, flags))
440 replaceSym();
441 return s;
444 Symbol *SymbolTable::addDefinedTable(StringRef name, uint32_t flags,
445 InputFile *file, InputTable *table) {
446 LLVM_DEBUG(dbgs() << "addDefinedTable:" << name << "\n");
448 Symbol *s;
449 bool wasInserted;
450 std::tie(s, wasInserted) = insert(name, file);
452 auto replaceSym = [&]() {
453 replaceSymbol<DefinedTable>(s, name, flags, file, table);
456 if (wasInserted || s->isLazy()) {
457 replaceSym();
458 return s;
461 checkTableType(s, file, &table->getType());
463 if (shouldReplace(s, file, flags))
464 replaceSym();
465 return s;
468 // This function get called when an undefined symbol is added, and there is
469 // already an existing one in the symbols table. In this case we check that
470 // custom 'import-module' and 'import-field' symbol attributes agree.
471 // With LTO these attributes are not available when the bitcode is read and only
472 // become available when the LTO object is read. In this case we silently
473 // replace the empty attributes with the valid ones.
474 template <typename T>
475 static void setImportAttributes(T *existing,
476 std::optional<StringRef> importName,
477 std::optional<StringRef> importModule,
478 uint32_t flags, InputFile *file) {
479 if (importName) {
480 if (!existing->importName)
481 existing->importName = importName;
482 if (existing->importName != importName)
483 error("import name mismatch for symbol: " + toString(*existing) +
484 "\n>>> defined as " + *existing->importName + " in " +
485 toString(existing->getFile()) + "\n>>> defined as " + *importName +
486 " in " + toString(file));
489 if (importModule) {
490 if (!existing->importModule)
491 existing->importModule = importModule;
492 if (existing->importModule != importModule)
493 error("import module mismatch for symbol: " + toString(*existing) +
494 "\n>>> defined as " + *existing->importModule + " in " +
495 toString(existing->getFile()) + "\n>>> defined as " +
496 *importModule + " in " + toString(file));
499 // Update symbol binding, if the existing symbol is weak
500 uint32_t binding = flags & WASM_SYMBOL_BINDING_MASK;
501 if (existing->isWeak() && binding != WASM_SYMBOL_BINDING_WEAK) {
502 existing->flags = (existing->flags & ~WASM_SYMBOL_BINDING_MASK) | binding;
506 Symbol *SymbolTable::addUndefinedFunction(StringRef name,
507 std::optional<StringRef> importName,
508 std::optional<StringRef> importModule,
509 uint32_t flags, InputFile *file,
510 const WasmSignature *sig,
511 bool isCalledDirectly) {
512 LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " ["
513 << (sig ? toString(*sig) : "none")
514 << "] IsCalledDirectly:" << isCalledDirectly << " flags=0x"
515 << utohexstr(flags) << "\n");
516 assert(flags & WASM_SYMBOL_UNDEFINED);
518 Symbol *s;
519 bool wasInserted;
520 std::tie(s, wasInserted) = insert(name, file);
521 if (s->traced)
522 printTraceSymbolUndefined(name, file);
524 auto replaceSym = [&]() {
525 replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags,
526 file, sig, isCalledDirectly);
529 if (wasInserted) {
530 replaceSym();
531 } else if (auto *lazy = dyn_cast<LazySymbol>(s)) {
532 if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
533 lazy->setWeak();
534 lazy->signature = sig;
535 } else {
536 lazy->fetch();
537 if (!config->whyExtract.empty())
538 config->whyExtractRecords.emplace_back(toString(file), s->getFile(),
539 *s);
541 } else {
542 auto existingFunction = dyn_cast<FunctionSymbol>(s);
543 if (!existingFunction) {
544 reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
545 return s;
547 if (!existingFunction->signature && sig)
548 existingFunction->signature = sig;
549 auto *existingUndefined = dyn_cast<UndefinedFunction>(existingFunction);
550 if (isCalledDirectly && !signatureMatches(existingFunction, sig)) {
551 // If the existing undefined functions is not called directly then let
552 // this one take precedence. Otherwise the existing function is either
553 // directly called or defined, in which case we need a function variant.
554 if (existingUndefined && !existingUndefined->isCalledDirectly)
555 replaceSym();
556 else if (getFunctionVariant(s, sig, file, &s))
557 replaceSym();
559 if (existingUndefined) {
560 setImportAttributes(existingUndefined, importName, importModule, flags,
561 file);
562 if (isCalledDirectly)
563 existingUndefined->isCalledDirectly = true;
564 if (s->isWeak())
565 s->flags = flags;
569 return s;
572 Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags,
573 InputFile *file) {
574 LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n");
575 assert(flags & WASM_SYMBOL_UNDEFINED);
577 Symbol *s;
578 bool wasInserted;
579 std::tie(s, wasInserted) = insert(name, file);
580 if (s->traced)
581 printTraceSymbolUndefined(name, file);
583 if (wasInserted) {
584 replaceSymbol<UndefinedData>(s, name, flags, file);
585 } else if (auto *lazy = dyn_cast<LazySymbol>(s)) {
586 if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK)
587 lazy->setWeak();
588 else
589 lazy->fetch();
590 } else if (s->isDefined()) {
591 checkDataType(s, file);
592 } else if (s->isWeak()) {
593 s->flags = flags;
595 return s;
598 Symbol *SymbolTable::addUndefinedGlobal(StringRef name,
599 std::optional<StringRef> importName,
600 std::optional<StringRef> importModule,
601 uint32_t flags, InputFile *file,
602 const WasmGlobalType *type) {
603 LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n");
604 assert(flags & WASM_SYMBOL_UNDEFINED);
606 Symbol *s;
607 bool wasInserted;
608 std::tie(s, wasInserted) = insert(name, file);
609 if (s->traced)
610 printTraceSymbolUndefined(name, file);
612 if (wasInserted)
613 replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags,
614 file, type);
615 else if (auto *lazy = dyn_cast<LazySymbol>(s))
616 lazy->fetch();
617 else if (s->isDefined())
618 checkGlobalType(s, file, type);
619 else if (s->isWeak())
620 s->flags = flags;
621 return s;
624 Symbol *SymbolTable::addUndefinedTable(StringRef name,
625 std::optional<StringRef> importName,
626 std::optional<StringRef> importModule,
627 uint32_t flags, InputFile *file,
628 const WasmTableType *type) {
629 LLVM_DEBUG(dbgs() << "addUndefinedTable: " << name << "\n");
630 assert(flags & WASM_SYMBOL_UNDEFINED);
632 Symbol *s;
633 bool wasInserted;
634 std::tie(s, wasInserted) = insert(name, file);
635 if (s->traced)
636 printTraceSymbolUndefined(name, file);
638 if (wasInserted)
639 replaceSymbol<UndefinedTable>(s, name, importName, importModule, flags,
640 file, type);
641 else if (auto *lazy = dyn_cast<LazySymbol>(s))
642 lazy->fetch();
643 else if (s->isDefined())
644 checkTableType(s, file, type);
645 else if (s->isWeak())
646 s->flags = flags;
647 return s;
650 Symbol *SymbolTable::addUndefinedTag(StringRef name,
651 std::optional<StringRef> importName,
652 std::optional<StringRef> importModule,
653 uint32_t flags, InputFile *file,
654 const WasmSignature *sig) {
655 LLVM_DEBUG(dbgs() << "addUndefinedTag: " << name << "\n");
656 assert(flags & WASM_SYMBOL_UNDEFINED);
658 Symbol *s;
659 bool wasInserted;
660 std::tie(s, wasInserted) = insert(name, file);
661 if (s->traced)
662 printTraceSymbolUndefined(name, file);
664 if (wasInserted)
665 replaceSymbol<UndefinedTag>(s, name, importName, importModule, flags, file,
666 sig);
667 else if (auto *lazy = dyn_cast<LazySymbol>(s))
668 lazy->fetch();
669 else if (s->isDefined())
670 checkTagType(s, file, sig);
671 else if (s->isWeak())
672 s->flags = flags;
673 return s;
676 TableSymbol *SymbolTable::createUndefinedIndirectFunctionTable(StringRef name) {
677 WasmLimits limits{0, 0, 0}; // Set by the writer.
678 WasmTableType *type = make<WasmTableType>();
679 type->ElemType = uint8_t(ValType::FUNCREF);
680 type->Limits = limits;
681 StringRef module(defaultModule);
682 uint32_t flags = config->exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN;
683 flags |= WASM_SYMBOL_UNDEFINED;
684 Symbol *sym = addUndefinedTable(name, name, module, flags, nullptr, type);
685 sym->markLive();
686 sym->forceExport = config->exportTable;
687 return cast<TableSymbol>(sym);
690 TableSymbol *SymbolTable::createDefinedIndirectFunctionTable(StringRef name) {
691 const uint32_t invalidIndex = -1;
692 WasmLimits limits{0, 0, 0}; // Set by the writer.
693 WasmTableType type{uint8_t(ValType::FUNCREF), limits};
694 WasmTable desc{invalidIndex, type, name};
695 InputTable *table = make<InputTable>(desc, nullptr);
696 uint32_t flags = config->exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN;
697 TableSymbol *sym = addSyntheticTable(name, flags, table);
698 sym->markLive();
699 sym->forceExport = config->exportTable;
700 return sym;
703 // Whether or not we need an indirect function table is usually a function of
704 // whether an input declares a need for it. However sometimes it's possible for
705 // no input to need the indirect function table, but then a late
706 // addInternalGOTEntry causes a function to be allocated an address. In that
707 // case address we synthesize a definition at the last minute.
708 TableSymbol *SymbolTable::resolveIndirectFunctionTable(bool required) {
709 Symbol *existing = find(functionTableName);
710 if (existing) {
711 if (!isa<TableSymbol>(existing)) {
712 error(Twine("reserved symbol must be of type table: `") +
713 functionTableName + "`");
714 return nullptr;
716 if (existing->isDefined()) {
717 error(Twine("reserved symbol must not be defined in input files: `") +
718 functionTableName + "`");
719 return nullptr;
723 if (config->importTable) {
724 if (existing)
725 return cast<TableSymbol>(existing);
726 if (required)
727 return createUndefinedIndirectFunctionTable(functionTableName);
728 } else if ((existing && existing->isLive()) || config->exportTable ||
729 required) {
730 // A defined table is required. Either because the user request an exported
731 // table or because the table symbol is already live. The existing table is
732 // guaranteed to be undefined due to the check above.
733 return createDefinedIndirectFunctionTable(functionTableName);
736 // An indirect function table will only be present in the symbol table if
737 // needed by a reloc; if we get here, we don't need one.
738 return nullptr;
741 void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) {
742 LLVM_DEBUG(dbgs() << "addLazy: " << sym->getName() << "\n");
743 StringRef name = sym->getName();
745 Symbol *s;
746 bool wasInserted;
747 std::tie(s, wasInserted) = insertName(name);
749 if (wasInserted) {
750 replaceSymbol<LazySymbol>(s, name, 0, file, *sym);
751 return;
754 if (!s->isUndefined())
755 return;
757 // The existing symbol is undefined, load a new one from the archive,
758 // unless the existing symbol is weak in which case replace the undefined
759 // symbols with a LazySymbol.
760 if (s->isWeak()) {
761 const WasmSignature *oldSig = nullptr;
762 // In the case of an UndefinedFunction we need to preserve the expected
763 // signature.
764 if (auto *f = dyn_cast<UndefinedFunction>(s))
765 oldSig = f->signature;
766 LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n");
767 auto newSym = replaceSymbol<LazySymbol>(s, name, WASM_SYMBOL_BINDING_WEAK,
768 file, *sym);
769 newSym->signature = oldSig;
770 return;
773 LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
774 const InputFile *oldFile = s->getFile();
775 file->addMember(sym);
776 if (!config->whyExtract.empty())
777 config->whyExtractRecords.emplace_back(toString(oldFile), s->getFile(), *s);
780 bool SymbolTable::addComdat(StringRef name) {
781 return comdatGroups.insert(CachedHashStringRef(name)).second;
784 // The new signature doesn't match. Create a variant to the symbol with the
785 // signature encoded in the name and return that instead. These symbols are
786 // then unified later in handleSymbolVariants.
787 bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig,
788 const InputFile *file, Symbol **out) {
789 LLVM_DEBUG(dbgs() << "getFunctionVariant: " << sym->getName() << " -> "
790 << " " << toString(*sig) << "\n");
791 Symbol *variant = nullptr;
793 // Linear search through symbol variants. Should never be more than two
794 // or three entries here.
795 auto &variants = symVariants[CachedHashStringRef(sym->getName())];
796 if (variants.empty())
797 variants.push_back(sym);
799 for (Symbol* v : variants) {
800 if (*v->getSignature() == *sig) {
801 variant = v;
802 break;
806 bool wasAdded = !variant;
807 if (wasAdded) {
808 // Create a new variant;
809 LLVM_DEBUG(dbgs() << "added new variant\n");
810 variant = reinterpret_cast<Symbol *>(make<SymbolUnion>());
811 variant->isUsedInRegularObj =
812 !file || file->kind() == InputFile::ObjectKind;
813 variant->canInline = true;
814 variant->traced = false;
815 variant->forceExport = false;
816 variants.push_back(variant);
817 } else {
818 LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant) << "\n");
819 assert(*variant->getSignature() == *sig);
822 *out = variant;
823 return wasAdded;
826 // Set a flag for --trace-symbol so that we can print out a log message
827 // if a new symbol with the same name is inserted into the symbol table.
828 void SymbolTable::trace(StringRef name) {
829 symMap.insert({CachedHashStringRef(name), -1});
832 void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
833 // Swap symbols as instructed by -wrap.
834 int &origIdx = symMap[CachedHashStringRef(sym->getName())];
835 int &realIdx= symMap[CachedHashStringRef(real->getName())];
836 int &wrapIdx = symMap[CachedHashStringRef(wrap->getName())];
837 LLVM_DEBUG(dbgs() << "wrap: " << sym->getName() << "\n");
839 // Anyone looking up __real symbols should get the original
840 realIdx = origIdx;
841 // Anyone looking up the original should get the __wrap symbol
842 origIdx = wrapIdx;
845 static const uint8_t unreachableFn[] = {
846 0x03 /* ULEB length */, 0x00 /* ULEB num locals */,
847 0x00 /* opcode unreachable */, 0x0b /* opcode end */
850 // Replace the given symbol body with an unreachable function.
851 // This is used by handleWeakUndefines in order to generate a callable
852 // equivalent of an undefined function and also handleSymbolVariants for
853 // undefined functions that don't match the signature of the definition.
854 InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym,
855 const WasmSignature &sig,
856 StringRef debugName) {
857 auto *func = make<SyntheticFunction>(sig, sym->getName(), debugName);
858 func->setBody(unreachableFn);
859 syntheticFunctions.emplace_back(func);
860 // Mark new symbols as local. For relocatable output we don't want them
861 // to be exported outside the object file.
862 replaceSymbol<DefinedFunction>(sym, debugName, WASM_SYMBOL_BINDING_LOCAL,
863 nullptr, func);
864 // Ensure the stub function doesn't get a table entry. Its address
865 // should always compare equal to the null pointer.
866 sym->isStub = true;
867 return func;
870 void SymbolTable::replaceWithUndefined(Symbol *sym) {
871 // Add a synthetic dummy for weak undefined functions. These dummies will
872 // be GC'd if not used as the target of any "call" instructions.
873 StringRef debugName = saver().save("undefined_weak:" + toString(*sym));
874 replaceWithUnreachable(sym, *sym->getSignature(), debugName);
875 // Hide our dummy to prevent export.
876 sym->setHidden(true);
879 // For weak undefined functions, there may be "call" instructions that reference
880 // the symbol. In this case, we need to synthesise a dummy/stub function that
881 // will abort at runtime, so that relocations can still provided an operand to
882 // the call instruction that passes Wasm validation.
883 void SymbolTable::handleWeakUndefines() {
884 for (Symbol *sym : symbols()) {
885 if (sym->isUndefWeak() && sym->isUsedInRegularObj) {
886 if (sym->getSignature()) {
887 replaceWithUndefined(sym);
888 } else {
889 // It is possible for undefined functions not to have a signature (eg.
890 // if added via "--undefined"), but weak undefined ones do have a
891 // signature. Lazy symbols may not be functions and therefore Sig can
892 // still be null in some circumstance.
893 assert(!isa<FunctionSymbol>(sym));
899 DefinedFunction *SymbolTable::createUndefinedStub(const WasmSignature &sig) {
900 if (stubFunctions.count(sig))
901 return stubFunctions[sig];
902 LLVM_DEBUG(dbgs() << "createUndefinedStub: " << toString(sig) << "\n");
903 auto *sym = reinterpret_cast<DefinedFunction *>(make<SymbolUnion>());
904 sym->isUsedInRegularObj = true;
905 sym->canInline = true;
906 sym->traced = false;
907 sym->forceExport = false;
908 sym->signature = &sig;
909 replaceSymbol<DefinedFunction>(
910 sym, "undefined_stub", WASM_SYMBOL_VISIBILITY_HIDDEN, nullptr, nullptr);
911 replaceWithUnreachable(sym, sig, "undefined_stub");
912 stubFunctions[sig] = sym;
913 return sym;
916 static void reportFunctionSignatureMismatch(StringRef symName,
917 FunctionSymbol *a,
918 FunctionSymbol *b, bool isError) {
919 std::string msg = ("function signature mismatch: " + symName +
920 "\n>>> defined as " + toString(*a->signature) + " in " +
921 toString(a->getFile()) + "\n>>> defined as " +
922 toString(*b->signature) + " in " + toString(b->getFile()))
923 .str();
924 if (isError)
925 error(msg);
926 else
927 warn(msg);
930 // Remove any variant symbols that were created due to function signature
931 // mismatches.
932 void SymbolTable::handleSymbolVariants() {
933 for (auto pair : symVariants) {
934 // Push the initial symbol onto the list of variants.
935 StringRef symName = pair.first.val();
936 std::vector<Symbol *> &variants = pair.second;
938 #ifndef NDEBUG
939 LLVM_DEBUG(dbgs() << "symbol with (" << variants.size()
940 << ") variants: " << symName << "\n");
941 for (auto *s: variants) {
942 auto *f = cast<FunctionSymbol>(s);
943 LLVM_DEBUG(dbgs() << " variant: " + f->getName() << " "
944 << toString(*f->signature) << "\n");
946 #endif
948 // Find the one definition.
949 DefinedFunction *defined = nullptr;
950 for (auto *symbol : variants) {
951 if (auto f = dyn_cast<DefinedFunction>(symbol)) {
952 defined = f;
953 break;
957 // If there are no definitions, and the undefined symbols disagree on
958 // the signature, there is not we can do since we don't know which one
959 // to use as the signature on the import.
960 if (!defined) {
961 reportFunctionSignatureMismatch(symName,
962 cast<FunctionSymbol>(variants[0]),
963 cast<FunctionSymbol>(variants[1]), true);
964 return;
967 for (auto *symbol : variants) {
968 if (symbol != defined) {
969 auto *f = cast<FunctionSymbol>(symbol);
970 reportFunctionSignatureMismatch(symName, f, defined, false);
971 StringRef debugName =
972 saver().save("signature_mismatch:" + toString(*f));
973 replaceWithUnreachable(f, *f->signature, debugName);
979 } // namespace wasm::lld