1 //===- SymbolTable.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "SymbolTable.h"
11 #include "InputChunks.h"
12 #include "InputElement.h"
13 #include "WriterUtils.h"
14 #include "lld/Common/CommonLinkerContext.h"
17 #define DEBUG_TYPE "lld"
20 using namespace llvm::wasm
;
21 using namespace llvm::object
;
26 void SymbolTable::addFile(InputFile
*file
, StringRef symName
) {
27 log("Processing: " + toString(file
));
30 if (auto *f
= dyn_cast
<ArchiveFile
>(file
)) {
36 if (auto *f
= dyn_cast
<SharedFile
>(file
)) {
37 sharedFiles
.push_back(f
);
42 if (auto *f
= dyn_cast
<StubFile
>(file
)) {
44 stubFiles
.push_back(f
);
49 message(toString(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
);
60 // Regular object file
61 auto *f
= cast
<ObjFile
>(file
);
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())
80 // Compile bitcode files and replace bitcode symbols.
81 lto
.reset(new BitcodeCompiler
);
82 for (BitcodeFile
*f
: bitcodeFiles
)
85 for (StringRef filename
: lto
->compile()) {
86 auto *obj
= make
<ObjFile
>(MemoryBufferRef(filename
, "lto.tmp"), "");
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)
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
) {
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();
116 return {symVector
[symIndex
], false};
118 Symbol
*sym
= reinterpret_cast<Symbol
*>(make
<SymbolUnion
>());
119 sym
->isUsedInRegularObj
= false;
120 sym
->canInline
= true;
122 sym
->forceExport
= false;
123 sym
->referenced
= !config
->gcSections
;
124 symVector
.emplace_back(sym
);
128 std::pair
<Symbol
*, bool> SymbolTable::insert(StringRef name
,
129 const InputFile
*file
) {
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
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
)
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
);
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
);
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
);
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
,
218 InputFunction
*function
) {
219 LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name
<< "\n");
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
230 DefinedData
*SymbolTable::addOptionalDataSymbol(StringRef name
,
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())
237 LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name
<< "\n");
238 auto *rtn
= replaceSymbol
<DefinedData
>(
239 s
, name
, WASM_SYMBOL_VISIBILITY_HIDDEN
| WASM_SYMBOL_ABSOLUTE
);
241 rtn
->referenced
= true;
245 DefinedData
*SymbolTable::addSyntheticDataSymbol(StringRef name
,
247 LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << name
<< "\n");
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
258 syntheticGlobals
.emplace_back(global
);
259 return replaceSymbol
<DefinedGlobal
>(insertName(name
).first
, name
, flags
,
263 DefinedGlobal
*SymbolTable::addOptionalGlobalSymbol(StringRef name
,
264 InputGlobal
*global
) {
265 Symbol
*s
= find(name
);
266 if (!s
|| s
->isDefined())
268 LLVM_DEBUG(dbgs() << "addOptionalGlobalSymbol: " << name
<< " -> " << global
270 syntheticGlobals
.emplace_back(global
);
271 return replaceSymbol
<DefinedGlobal
>(s
, name
, WASM_SYMBOL_VISIBILITY_HIDDEN
,
275 DefinedTable
*SymbolTable::addSyntheticTable(StringRef name
, uint32_t flags
,
277 LLVM_DEBUG(dbgs() << "addSyntheticTable: " << name
<< " -> " << table
279 Symbol
*s
= find(name
);
280 assert(!s
|| s
->isUndefined());
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
,
289 // If existing symbol is undefined, replace it.
290 if (!existing
->isDefined()) {
291 LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
292 << existing
->getName() << "\n");
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");
302 // If the existing symbol is weak, we should replace it.
303 if (existing
->isWeak()) {
304 LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
308 // Neither symbol is week. They conflict.
309 error("duplicate symbol: " + toString(*existing
) + "\n>>> defined in " +
310 toString(existing
->getFile()) + "\n>>> defined in " +
315 Symbol
*SymbolTable::addDefinedFunction(StringRef name
, uint32_t flags
,
317 InputFunction
*function
) {
318 LLVM_DEBUG(dbgs() << "addDefinedFunction: " << name
<< " ["
319 << (function
? toString(function
->signature
) : "none")
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()) {
339 auto existingFunction
= dyn_cast
<FunctionSymbol
>(s
);
340 if (!existingFunction
) {
341 reportTypeError(s
, file
, WASM_SYMBOL_TYPE_FUNCTION
);
345 bool checkSig
= true;
346 if (auto ud
= dyn_cast
<UndefinedFunction
>(existingFunction
))
347 checkSig
= ud
->isCalledDirectly
;
349 if (checkSig
&& function
&& !signatureMatches(existingFunction
, &function
->signature
)) {
351 if (getFunctionVariant(s
, &function
->signature
, file
, &variant
))
352 // New variant, always replace
354 else if (shouldReplace(s
, file
, flags
))
355 // Variant already exists, replace it after checking shouldReplace
358 // This variant we found take the place in the symbol table as the primary
360 replace(name
, variant
);
364 // Existing function with matching signature.
365 if (shouldReplace(s
, file
, flags
))
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
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()) {
389 checkDataType(s
, file
);
391 if (shouldReplace(s
, file
, flags
))
396 Symbol
*SymbolTable::addDefinedGlobal(StringRef name
, uint32_t flags
,
397 InputFile
*file
, InputGlobal
*global
) {
398 LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << name
<< "\n");
402 std::tie(s
, wasInserted
) = insert(name
, file
);
404 auto replaceSym
= [&]() {
405 replaceSymbol
<DefinedGlobal
>(s
, name
, flags
, file
, global
);
408 if (wasInserted
|| s
->isLazy()) {
413 checkGlobalType(s
, file
, &global
->getType());
415 if (shouldReplace(s
, file
, flags
))
420 Symbol
*SymbolTable::addDefinedTag(StringRef name
, uint32_t flags
,
421 InputFile
*file
, InputTag
*tag
) {
422 LLVM_DEBUG(dbgs() << "addDefinedTag:" << name
<< "\n");
426 std::tie(s
, wasInserted
) = insert(name
, file
);
428 auto replaceSym
= [&]() {
429 replaceSymbol
<DefinedTag
>(s
, name
, flags
, file
, tag
);
432 if (wasInserted
|| s
->isLazy()) {
437 checkTagType(s
, file
, &tag
->signature
);
439 if (shouldReplace(s
, file
, flags
))
444 Symbol
*SymbolTable::addDefinedTable(StringRef name
, uint32_t flags
,
445 InputFile
*file
, InputTable
*table
) {
446 LLVM_DEBUG(dbgs() << "addDefinedTable:" << name
<< "\n");
450 std::tie(s
, wasInserted
) = insert(name
, file
);
452 auto replaceSym
= [&]() {
453 replaceSymbol
<DefinedTable
>(s
, name
, flags
, file
, table
);
456 if (wasInserted
|| s
->isLazy()) {
461 checkTableType(s
, file
, &table
->getType());
463 if (shouldReplace(s
, file
, flags
))
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
) {
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
));
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
);
520 std::tie(s
, wasInserted
) = insert(name
, file
);
522 printTraceSymbolUndefined(name
, file
);
524 auto replaceSym
= [&]() {
525 replaceSymbol
<UndefinedFunction
>(s
, name
, importName
, importModule
, flags
,
526 file
, sig
, isCalledDirectly
);
531 } else if (auto *lazy
= dyn_cast
<LazySymbol
>(s
)) {
532 if ((flags
& WASM_SYMBOL_BINDING_MASK
) == WASM_SYMBOL_BINDING_WEAK
) {
534 lazy
->signature
= sig
;
537 if (!config
->whyExtract
.empty())
538 config
->whyExtractRecords
.emplace_back(toString(file
), s
->getFile(),
542 auto existingFunction
= dyn_cast
<FunctionSymbol
>(s
);
543 if (!existingFunction
) {
544 reportTypeError(s
, file
, WASM_SYMBOL_TYPE_FUNCTION
);
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
)
556 else if (getFunctionVariant(s
, sig
, file
, &s
))
559 if (existingUndefined
) {
560 setImportAttributes(existingUndefined
, importName
, importModule
, flags
,
562 if (isCalledDirectly
)
563 existingUndefined
->isCalledDirectly
= true;
572 Symbol
*SymbolTable::addUndefinedData(StringRef name
, uint32_t flags
,
574 LLVM_DEBUG(dbgs() << "addUndefinedData: " << name
<< "\n");
575 assert(flags
& WASM_SYMBOL_UNDEFINED
);
579 std::tie(s
, wasInserted
) = insert(name
, file
);
581 printTraceSymbolUndefined(name
, file
);
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
)
590 } else if (s
->isDefined()) {
591 checkDataType(s
, file
);
592 } else if (s
->isWeak()) {
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
);
608 std::tie(s
, wasInserted
) = insert(name
, file
);
610 printTraceSymbolUndefined(name
, file
);
613 replaceSymbol
<UndefinedGlobal
>(s
, name
, importName
, importModule
, flags
,
615 else if (auto *lazy
= dyn_cast
<LazySymbol
>(s
))
617 else if (s
->isDefined())
618 checkGlobalType(s
, file
, type
);
619 else if (s
->isWeak())
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
);
634 std::tie(s
, wasInserted
) = insert(name
, file
);
636 printTraceSymbolUndefined(name
, file
);
639 replaceSymbol
<UndefinedTable
>(s
, name
, importName
, importModule
, flags
,
641 else if (auto *lazy
= dyn_cast
<LazySymbol
>(s
))
643 else if (s
->isDefined())
644 checkTableType(s
, file
, type
);
645 else if (s
->isWeak())
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
);
660 std::tie(s
, wasInserted
) = insert(name
, file
);
662 printTraceSymbolUndefined(name
, file
);
665 replaceSymbol
<UndefinedTag
>(s
, name
, importName
, importModule
, flags
, file
,
667 else if (auto *lazy
= dyn_cast
<LazySymbol
>(s
))
669 else if (s
->isDefined())
670 checkTagType(s
, file
, sig
);
671 else if (s
->isWeak())
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
);
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
);
699 sym
->forceExport
= config
->exportTable
;
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
);
711 if (!isa
<TableSymbol
>(existing
)) {
712 error(Twine("reserved symbol must be of type table: `") +
713 functionTableName
+ "`");
716 if (existing
->isDefined()) {
717 error(Twine("reserved symbol must not be defined in input files: `") +
718 functionTableName
+ "`");
723 if (config
->importTable
) {
725 return cast
<TableSymbol
>(existing
);
727 return createUndefinedIndirectFunctionTable(functionTableName
);
728 } else if ((existing
&& existing
->isLive()) || config
->exportTable
||
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.
741 void SymbolTable::addLazy(ArchiveFile
*file
, const Archive::Symbol
*sym
) {
742 LLVM_DEBUG(dbgs() << "addLazy: " << sym
->getName() << "\n");
743 StringRef name
= sym
->getName();
747 std::tie(s
, wasInserted
) = insertName(name
);
750 replaceSymbol
<LazySymbol
>(s
, name
, 0, file
, *sym
);
754 if (!s
->isUndefined())
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.
761 const WasmSignature
*oldSig
= nullptr;
762 // In the case of an UndefinedFunction we need to preserve the expected
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
,
769 newSym
->signature
= oldSig
;
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
) {
806 bool wasAdded
= !variant
;
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
);
818 LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant
) << "\n");
819 assert(*variant
->getSignature() == *sig
);
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
841 // Anyone looking up the original should get the __wrap symbol
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
,
864 // Ensure the stub function doesn't get a table entry. Its address
865 // should always compare equal to the null pointer.
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
);
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;
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
;
916 static void reportFunctionSignatureMismatch(StringRef symName
,
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()))
930 // Remove any variant symbols that were created due to function signature
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
;
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");
948 // Find the one definition.
949 DefinedFunction
*defined
= nullptr;
950 for (auto *symbol
: variants
) {
951 if (auto f
= dyn_cast
<DefinedFunction
>(symbol
)) {
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.
961 reportFunctionSignatureMismatch(symName
,
962 cast
<FunctionSymbol
>(variants
[0]),
963 cast
<FunctionSymbol
>(variants
[1]), true);
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