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