1c94d393aSSam Clegg //===- SymbolTable.cpp ----------------------------------------------------===//
2c94d393aSSam Clegg //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c94d393aSSam Clegg //
7c94d393aSSam Clegg //===----------------------------------------------------------------------===//
8c94d393aSSam Clegg
9c94d393aSSam Clegg #include "SymbolTable.h"
10c94d393aSSam Clegg #include "Config.h"
115fa274beSSam Clegg #include "InputChunks.h"
12a56e5749SAndy Wingo #include "InputElement.h"
13b8621596SSam Clegg #include "WriterUtils.h"
1483d59e05SAlexandre Ganea #include "lld/Common/CommonLinkerContext.h"
157d67dd11SRui Ueyama #include "llvm/ADT/SetVector.h"
16c94d393aSSam Clegg
17c94d393aSSam Clegg #define DEBUG_TYPE "lld"
18c94d393aSSam Clegg
19c94d393aSSam Clegg using namespace llvm;
2020db381bSSam Clegg using namespace llvm::wasm;
2145218f4aSSam Clegg using namespace llvm::object;
22c94d393aSSam Clegg
2333c59abfSFangrui Song namespace lld {
2433c59abfSFangrui Song namespace wasm {
2533c59abfSFangrui Song SymbolTable *symtab;
26c94d393aSSam Clegg
addFile(InputFile * file)27136d27abSRui Ueyama void SymbolTable::addFile(InputFile *file) {
28136d27abSRui Ueyama log("Processing: " + toString(file));
29a282a61bSSam Clegg
30a282a61bSSam Clegg // .a file
31136d27abSRui Ueyama if (auto *f = dyn_cast<ArchiveFile>(file)) {
32136d27abSRui Ueyama f->parse();
33a282a61bSSam Clegg return;
34a282a61bSSam Clegg }
35a282a61bSSam Clegg
36a282a61bSSam Clegg // .so file
37136d27abSRui Ueyama if (auto *f = dyn_cast<SharedFile>(file)) {
38136d27abSRui Ueyama sharedFiles.push_back(f);
39a282a61bSSam Clegg return;
40a282a61bSSam Clegg }
41a282a61bSSam Clegg
42136d27abSRui Ueyama if (config->trace)
43136d27abSRui Ueyama message(toString(file));
44c94d393aSSam Clegg
45c729c1b4SSam Clegg // LLVM bitcode file
46136d27abSRui Ueyama if (auto *f = dyn_cast<BitcodeFile>(file)) {
47136d27abSRui Ueyama f->parse();
48136d27abSRui Ueyama bitcodeFiles.push_back(f);
49a282a61bSSam Clegg return;
50a282a61bSSam Clegg }
51a282a61bSSam Clegg
52a282a61bSSam Clegg // Regular object file
53136d27abSRui Ueyama auto *f = cast<ObjFile>(file);
54136d27abSRui Ueyama f->parse(false);
55136d27abSRui Ueyama objectFiles.push_back(f);
56c94d393aSSam Clegg }
57c94d393aSSam Clegg
58c729c1b4SSam Clegg // This function is where all the optimizations of link-time
59c729c1b4SSam Clegg // optimization happens. When LTO is in use, some input files are
60c729c1b4SSam Clegg // not in native object file format but in the LLVM bitcode format.
61c729c1b4SSam Clegg // This function compiles bitcode files into a few big native files
62c729c1b4SSam Clegg // using LLVM functions and replaces bitcode symbols with the results.
63c729c1b4SSam Clegg // Because all bitcode files that the program consists of are passed
64c729c1b4SSam Clegg // to the compiler at once, it can do whole-program optimization.
compileBitcodeFiles()654da38c14SSam Clegg void SymbolTable::compileBitcodeFiles() {
66b062fe18SSam Clegg // Prevent further LTO objects being included
67b062fe18SSam Clegg BitcodeFile::doneLTO = true;
68b062fe18SSam Clegg
69136d27abSRui Ueyama if (bitcodeFiles.empty())
70c729c1b4SSam Clegg return;
71c729c1b4SSam Clegg
72c729c1b4SSam Clegg // Compile bitcode files and replace bitcode symbols.
73136d27abSRui Ueyama lto.reset(new BitcodeCompiler);
74136d27abSRui Ueyama for (BitcodeFile *f : bitcodeFiles)
75136d27abSRui Ueyama lto->add(*f);
76c729c1b4SSam Clegg
77136d27abSRui Ueyama for (StringRef filename : lto->compile()) {
78136d27abSRui Ueyama auto *obj = make<ObjFile>(MemoryBufferRef(filename, "lto.tmp"), "");
79136d27abSRui Ueyama obj->parse(true);
80136d27abSRui Ueyama objectFiles.push_back(obj);
81c729c1b4SSam Clegg }
82c729c1b4SSam Clegg }
83c729c1b4SSam Clegg
find(StringRef name)84136d27abSRui Ueyama Symbol *SymbolTable::find(StringRef name) {
85136d27abSRui Ueyama auto it = symMap.find(CachedHashStringRef(name));
86136d27abSRui Ueyama if (it == symMap.end() || it->second == -1)
871f3f774fSSam Clegg return nullptr;
88136d27abSRui Ueyama return symVector[it->second];
891f3f774fSSam Clegg }
901f3f774fSSam Clegg
replace(StringRef name,Symbol * sym)91136d27abSRui Ueyama void SymbolTable::replace(StringRef name, Symbol* sym) {
92136d27abSRui Ueyama auto it = symMap.find(CachedHashStringRef(name));
93136d27abSRui Ueyama symVector[it->second] = sym;
946540e570SSam Clegg }
956540e570SSam Clegg
insertName(StringRef name)96136d27abSRui Ueyama std::pair<Symbol *, bool> SymbolTable::insertName(StringRef name) {
97136d27abSRui Ueyama bool trace = false;
98136d27abSRui Ueyama auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()});
99136d27abSRui Ueyama int &symIndex = p.first->second;
100136d27abSRui Ueyama bool isNew = p.second;
101136d27abSRui Ueyama if (symIndex == -1) {
102136d27abSRui Ueyama symIndex = symVector.size();
103136d27abSRui Ueyama trace = true;
104136d27abSRui Ueyama isNew = true;
1051f3f774fSSam Clegg }
1061f3f774fSSam Clegg
107136d27abSRui Ueyama if (!isNew)
108136d27abSRui Ueyama return {symVector[symIndex], false};
1091f3f774fSSam Clegg
110136d27abSRui Ueyama Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
111136d27abSRui Ueyama sym->isUsedInRegularObj = false;
112136d27abSRui Ueyama sym->canInline = true;
113136d27abSRui Ueyama sym->traced = trace;
114064e9907SSam Clegg sym->forceExport = false;
115136d27abSRui Ueyama symVector.emplace_back(sym);
116136d27abSRui Ueyama return {sym, true};
117c94d393aSSam Clegg }
118c94d393aSSam Clegg
insert(StringRef name,const InputFile * file)119136d27abSRui Ueyama std::pair<Symbol *, bool> SymbolTable::insert(StringRef name,
120136d27abSRui Ueyama const InputFile *file) {
121136d27abSRui Ueyama Symbol *s;
122136d27abSRui Ueyama bool wasInserted;
123136d27abSRui Ueyama std::tie(s, wasInserted) = insertName(name);
1241f3f774fSSam Clegg
125136d27abSRui Ueyama if (!file || file->kind() == InputFile::ObjectKind)
126136d27abSRui Ueyama s->isUsedInRegularObj = true;
1271f3f774fSSam Clegg
128136d27abSRui Ueyama return {s, wasInserted};
129c94d393aSSam Clegg }
130c94d393aSSam Clegg
reportTypeError(const Symbol * existing,const InputFile * file,llvm::wasm::WasmSymbolType type)131136d27abSRui Ueyama static void reportTypeError(const Symbol *existing, const InputFile *file,
132136d27abSRui Ueyama llvm::wasm::WasmSymbolType type) {
133136d27abSRui Ueyama error("symbol type mismatch: " + toString(*existing) + "\n>>> defined as " +
134136d27abSRui Ueyama toString(existing->getWasmType()) + " in " +
135136d27abSRui Ueyama toString(existing->getFile()) + "\n>>> defined as " + toString(type) +
136136d27abSRui Ueyama " in " + toString(file));
137e3498ec5SRui Ueyama }
138e3498ec5SRui Ueyama
1396f4286fbSHeejin Ahn // Check the type of new symbol matches that of the symbol is replacing.
1407c5fcb35SKazuaki Ishizaki // Returns true if the function types match, false is there is a signature
1416540e570SSam Clegg // mismatch.
signatureMatches(FunctionSymbol * existing,const WasmSignature * newSig)142136d27abSRui Ueyama static bool signatureMatches(FunctionSymbol *existing,
143136d27abSRui Ueyama const WasmSignature *newSig) {
144136d27abSRui Ueyama const WasmSignature *oldSig = existing->signature;
145d506b0a4SSam Clegg
1469f903475SNico Weber // If either function is missing a signature (this happens for bitcode
147d506b0a4SSam Clegg // symbols) then assume they match. Any mismatch will be reported later
148d506b0a4SSam Clegg // when the LTO objects are added.
149136d27abSRui Ueyama if (!newSig || !oldSig)
1506540e570SSam Clegg return true;
151cefbf9acSSam Clegg
152136d27abSRui Ueyama return *newSig == *oldSig;
153e3498ec5SRui Ueyama }
154e3498ec5SRui Ueyama
checkGlobalType(const Symbol * existing,const InputFile * file,const WasmGlobalType * newType)155136d27abSRui Ueyama static void checkGlobalType(const Symbol *existing, const InputFile *file,
156136d27abSRui Ueyama const WasmGlobalType *newType) {
157136d27abSRui Ueyama if (!isa<GlobalSymbol>(existing)) {
158136d27abSRui Ueyama reportTypeError(existing, file, WASM_SYMBOL_TYPE_GLOBAL);
159b8621596SSam Clegg return;
160b8621596SSam Clegg }
161b8621596SSam Clegg
162136d27abSRui Ueyama const WasmGlobalType *oldType = cast<GlobalSymbol>(existing)->getGlobalType();
163136d27abSRui Ueyama if (*newType != *oldType) {
164136d27abSRui Ueyama error("Global type mismatch: " + existing->getName() + "\n>>> defined as " +
165136d27abSRui Ueyama toString(*oldType) + " in " + toString(existing->getFile()) +
166136d27abSRui Ueyama "\n>>> defined as " + toString(*newType) + " in " + toString(file));
167c94d393aSSam Clegg }
16824b3dcd4SSam Clegg }
16924b3dcd4SSam Clegg
checkTagType(const Symbol * existing,const InputFile * file,const WasmSignature * newSig)1701d891d44SHeejin Ahn static void checkTagType(const Symbol *existing, const InputFile *file,
171136d27abSRui Ueyama const WasmSignature *newSig) {
1721d891d44SHeejin Ahn const auto *existingTag = dyn_cast<TagSymbol>(existing);
1731d891d44SHeejin Ahn if (!isa<TagSymbol>(existing)) {
1741d891d44SHeejin Ahn reportTypeError(existing, file, WASM_SYMBOL_TYPE_TAG);
175e915a71fSHeejin Ahn return;
176e915a71fSHeejin Ahn }
177e915a71fSHeejin Ahn
1781d891d44SHeejin Ahn const WasmSignature *oldSig = existingTag->signature;
179136d27abSRui Ueyama if (*newSig != *oldSig)
1801d891d44SHeejin Ahn warn("Tag signature mismatch: " + existing->getName() +
181136d27abSRui Ueyama "\n>>> defined as " + toString(*oldSig) + " in " +
182136d27abSRui Ueyama toString(existing->getFile()) + "\n>>> defined as " +
183136d27abSRui Ueyama toString(*newSig) + " in " + toString(file));
184e915a71fSHeejin Ahn }
185e915a71fSHeejin Ahn
checkTableType(const Symbol * existing,const InputFile * file,const WasmTableType * newType)18653e3b81fSAndy Wingo static void checkTableType(const Symbol *existing, const InputFile *file,
18753e3b81fSAndy Wingo const WasmTableType *newType) {
18853e3b81fSAndy Wingo if (!isa<TableSymbol>(existing)) {
18953e3b81fSAndy Wingo reportTypeError(existing, file, WASM_SYMBOL_TYPE_TABLE);
19053e3b81fSAndy Wingo return;
19153e3b81fSAndy Wingo }
19253e3b81fSAndy Wingo
19353e3b81fSAndy Wingo const WasmTableType *oldType = cast<TableSymbol>(existing)->getTableType();
19453e3b81fSAndy Wingo if (newType->ElemType != oldType->ElemType) {
19553e3b81fSAndy Wingo error("Table type mismatch: " + existing->getName() + "\n>>> defined as " +
19653e3b81fSAndy Wingo toString(*oldType) + " in " + toString(existing->getFile()) +
19753e3b81fSAndy Wingo "\n>>> defined as " + toString(*newType) + " in " + toString(file));
19853e3b81fSAndy Wingo }
19953e3b81fSAndy Wingo // FIXME: No assertions currently on the limits.
20053e3b81fSAndy Wingo }
20153e3b81fSAndy Wingo
checkDataType(const Symbol * existing,const InputFile * file)202136d27abSRui Ueyama static void checkDataType(const Symbol *existing, const InputFile *file) {
203136d27abSRui Ueyama if (!isa<DataSymbol>(existing))
204136d27abSRui Ueyama reportTypeError(existing, file, WASM_SYMBOL_TYPE_DATA);
205e3498ec5SRui Ueyama }
206e3498ec5SRui Ueyama
addSyntheticFunction(StringRef name,uint32_t flags,InputFunction * function)207136d27abSRui Ueyama DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name,
208136d27abSRui Ueyama uint32_t flags,
209136d27abSRui Ueyama InputFunction *function) {
210136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n");
211136d27abSRui Ueyama assert(!find(name));
212136d27abSRui Ueyama syntheticFunctions.emplace_back(function);
213136d27abSRui Ueyama return replaceSymbol<DefinedFunction>(insertName(name).first, name,
214136d27abSRui Ueyama flags, nullptr, function);
21550686856SSam Clegg }
21650686856SSam Clegg
21729a3056bSSam Clegg // Adds an optional, linker generated, data symbol. The symbol will only be
2187ae3d335SKazuaki Ishizaki // added if there is an undefine reference to it, or if it is explicitly
2197ae3d335SKazuaki Ishizaki // exported via the --export flag. Otherwise we don't add the symbol and return
2207ae3d335SKazuaki Ishizaki // nullptr.
addOptionalDataSymbol(StringRef name,uint64_t value)221caa0db13SSam Clegg DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name,
222582fd474SWouter van Oortmerssen uint64_t value) {
223136d27abSRui Ueyama Symbol *s = find(name);
224136d27abSRui Ueyama if (!s && (config->exportAll || config->exportedSymbols.count(name) != 0))
225136d27abSRui Ueyama s = insertName(name).first;
226136d27abSRui Ueyama else if (!s || s->isDefined())
2274bce63a0SSam Clegg return nullptr;
228136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n");
229caa0db13SSam Clegg auto *rtn = replaceSymbol<DefinedData>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN);
23014ffbb84SSam Clegg rtn->setVA(value);
231136d27abSRui Ueyama rtn->referenced = true;
2324bce63a0SSam Clegg return rtn;
2334bce63a0SSam Clegg }
2344bce63a0SSam Clegg
addSyntheticDataSymbol(StringRef name,uint32_t flags)235136d27abSRui Ueyama DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef name,
236136d27abSRui Ueyama uint32_t flags) {
237136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << name << "\n");
238136d27abSRui Ueyama assert(!find(name));
239136d27abSRui Ueyama return replaceSymbol<DefinedData>(insertName(name).first, name, flags);
240c94d393aSSam Clegg }
241c94d393aSSam Clegg
addSyntheticGlobal(StringRef name,uint32_t flags,InputGlobal * global)242136d27abSRui Ueyama DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags,
243136d27abSRui Ueyama InputGlobal *global) {
244136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global
245e7245b42SNicola Zaghen << "\n");
246136d27abSRui Ueyama assert(!find(name));
247136d27abSRui Ueyama syntheticGlobals.emplace_back(global);
248136d27abSRui Ueyama return replaceSymbol<DefinedGlobal>(insertName(name).first, name, flags,
249136d27abSRui Ueyama nullptr, global);
25093102974SSam Clegg }
25193102974SSam Clegg
addOptionalGlobalSymbol(StringRef name,InputGlobal * global)25288e4056bSSam Clegg DefinedGlobal *SymbolTable::addOptionalGlobalSymbol(StringRef name,
25329a3056bSSam Clegg InputGlobal *global) {
25429a3056bSSam Clegg Symbol *s = find(name);
25529a3056bSSam Clegg if (!s || s->isDefined())
25629a3056bSSam Clegg return nullptr;
2578fe12847SSam Clegg LLVM_DEBUG(dbgs() << "addOptionalGlobalSymbol: " << name << " -> " << global
2588fe12847SSam Clegg << "\n");
25929a3056bSSam Clegg syntheticGlobals.emplace_back(global);
26088e4056bSSam Clegg return replaceSymbol<DefinedGlobal>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN,
26188e4056bSSam Clegg nullptr, global);
26229a3056bSSam Clegg }
26329a3056bSSam Clegg
addSyntheticTable(StringRef name,uint32_t flags,InputTable * table)26463393828SAndy Wingo DefinedTable *SymbolTable::addSyntheticTable(StringRef name, uint32_t flags,
26563393828SAndy Wingo InputTable *table) {
26663393828SAndy Wingo LLVM_DEBUG(dbgs() << "addSyntheticTable: " << name << " -> " << table
26763393828SAndy Wingo << "\n");
26863393828SAndy Wingo Symbol *s = find(name);
26963393828SAndy Wingo assert(!s || s->isUndefined());
27063393828SAndy Wingo if (!s)
27163393828SAndy Wingo s = insertName(name).first;
27263393828SAndy Wingo syntheticTables.emplace_back(table);
27363393828SAndy Wingo return replaceSymbol<DefinedTable>(s, name, flags, nullptr, table);
27463393828SAndy Wingo }
27563393828SAndy Wingo
shouldReplace(const Symbol * existing,InputFile * newFile,uint32_t newFlags)276136d27abSRui Ueyama static bool shouldReplace(const Symbol *existing, InputFile *newFile,
277136d27abSRui Ueyama uint32_t newFlags) {
278c03c9040SRui Ueyama // If existing symbol is undefined, replace it.
279136d27abSRui Ueyama if (!existing->isDefined()) {
280e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
281136d27abSRui Ueyama << existing->getName() << "\n");
282c03c9040SRui Ueyama return true;
283c03c9040SRui Ueyama }
284c03c9040SRui Ueyama
285c03c9040SRui Ueyama // Now we have two defined symbols. If the new one is weak, we can ignore it.
286136d27abSRui Ueyama if ((newFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
287e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");
288c03c9040SRui Ueyama return false;
289c03c9040SRui Ueyama }
290c03c9040SRui Ueyama
291c03c9040SRui Ueyama // If the existing symbol is weak, we should replace it.
292136d27abSRui Ueyama if (existing->isWeak()) {
293e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
294c03c9040SRui Ueyama return true;
295c03c9040SRui Ueyama }
296c03c9040SRui Ueyama
297c03c9040SRui Ueyama // Neither symbol is week. They conflict.
298136d27abSRui Ueyama error("duplicate symbol: " + toString(*existing) + "\n>>> defined in " +
299136d27abSRui Ueyama toString(existing->getFile()) + "\n>>> defined in " +
300136d27abSRui Ueyama toString(newFile));
301c03c9040SRui Ueyama return true;
302b8621596SSam Clegg }
30393e559b1SSam Clegg
addDefinedFunction(StringRef name,uint32_t flags,InputFile * file,InputFunction * function)304136d27abSRui Ueyama Symbol *SymbolTable::addDefinedFunction(StringRef name, uint32_t flags,
305136d27abSRui Ueyama InputFile *file,
306136d27abSRui Ueyama InputFunction *function) {
307136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addDefinedFunction: " << name << " ["
308136d27abSRui Ueyama << (function ? toString(function->signature) : "none")
3098b0b48f3SSam Clegg << "]\n");
310136d27abSRui Ueyama Symbol *s;
311136d27abSRui Ueyama bool wasInserted;
312136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file);
313c729c1b4SSam Clegg
314136d27abSRui Ueyama auto replaceSym = [&](Symbol *sym) {
3157c5fcb35SKazuaki Ishizaki // If the new defined function doesn't have signature (i.e. bitcode
3166540e570SSam Clegg // functions) but the old symbol does, then preserve the old signature
317136d27abSRui Ueyama const WasmSignature *oldSig = s->getSignature();
318136d27abSRui Ueyama auto* newSym = replaceSymbol<DefinedFunction>(sym, name, flags, file, function);
319136d27abSRui Ueyama if (!newSym->signature)
320136d27abSRui Ueyama newSym->signature = oldSig;
3216540e570SSam Clegg };
3226540e570SSam Clegg
323136d27abSRui Ueyama if (wasInserted || s->isLazy()) {
324136d27abSRui Ueyama replaceSym(s);
325136d27abSRui Ueyama return s;
326e3498ec5SRui Ueyama }
327e3498ec5SRui Ueyama
328136d27abSRui Ueyama auto existingFunction = dyn_cast<FunctionSymbol>(s);
329136d27abSRui Ueyama if (!existingFunction) {
330136d27abSRui Ueyama reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
331136d27abSRui Ueyama return s;
3328b0b48f3SSam Clegg }
3336540e570SSam Clegg
334136d27abSRui Ueyama bool checkSig = true;
335136d27abSRui Ueyama if (auto ud = dyn_cast<UndefinedFunction>(existingFunction))
336136d27abSRui Ueyama checkSig = ud->isCalledDirectly;
33759f959ffSSam Clegg
338136d27abSRui Ueyama if (checkSig && function && !signatureMatches(existingFunction, &function->signature)) {
339136d27abSRui Ueyama Symbol* variant;
340136d27abSRui Ueyama if (getFunctionVariant(s, &function->signature, file, &variant))
3416540e570SSam Clegg // New variant, always replace
342136d27abSRui Ueyama replaceSym(variant);
343136d27abSRui Ueyama else if (shouldReplace(s, file, flags))
3446540e570SSam Clegg // Variant already exists, replace it after checking shouldReplace
345136d27abSRui Ueyama replaceSym(variant);
3466540e570SSam Clegg
3476540e570SSam Clegg // This variant we found take the place in the symbol table as the primary
3486540e570SSam Clegg // variant.
349136d27abSRui Ueyama replace(name, variant);
350136d27abSRui Ueyama return variant;
3516540e570SSam Clegg }
3526540e570SSam Clegg
3536540e570SSam Clegg // Existing function with matching signature.
354136d27abSRui Ueyama if (shouldReplace(s, file, flags))
355136d27abSRui Ueyama replaceSym(s);
3566540e570SSam Clegg
357136d27abSRui Ueyama return s;
35893e559b1SSam Clegg }
35993e559b1SSam Clegg
addDefinedData(StringRef name,uint32_t flags,InputFile * file,InputChunk * segment,uint64_t address,uint64_t size)360136d27abSRui Ueyama Symbol *SymbolTable::addDefinedData(StringRef name, uint32_t flags,
3615a9b25e1SSam Clegg InputFile *file, InputChunk *segment,
3623b29376eSWouter van Oortmerssen uint64_t address, uint64_t size) {
363136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addDefinedData:" << name << " addr:" << address
364e7245b42SNicola Zaghen << "\n");
365136d27abSRui Ueyama Symbol *s;
366136d27abSRui Ueyama bool wasInserted;
367136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file);
368c729c1b4SSam Clegg
369136d27abSRui Ueyama auto replaceSym = [&]() {
370136d27abSRui Ueyama replaceSymbol<DefinedData>(s, name, flags, file, segment, address, size);
3716540e570SSam Clegg };
3726540e570SSam Clegg
373136d27abSRui Ueyama if (wasInserted || s->isLazy()) {
374136d27abSRui Ueyama replaceSym();
375136d27abSRui Ueyama return s;
376e3498ec5SRui Ueyama }
377e3498ec5SRui Ueyama
378136d27abSRui Ueyama checkDataType(s, file);
379e3498ec5SRui Ueyama
380136d27abSRui Ueyama if (shouldReplace(s, file, flags))
381136d27abSRui Ueyama replaceSym();
382136d27abSRui Ueyama return s;
383c94d393aSSam Clegg }
384c94d393aSSam Clegg
addDefinedGlobal(StringRef name,uint32_t flags,InputFile * file,InputGlobal * global)385136d27abSRui Ueyama Symbol *SymbolTable::addDefinedGlobal(StringRef name, uint32_t flags,
386136d27abSRui Ueyama InputFile *file, InputGlobal *global) {
387136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << name << "\n");
3884c2cbfe6SSam Clegg
389136d27abSRui Ueyama Symbol *s;
390136d27abSRui Ueyama bool wasInserted;
391136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file);
392c729c1b4SSam Clegg
393136d27abSRui Ueyama auto replaceSym = [&]() {
394136d27abSRui Ueyama replaceSymbol<DefinedGlobal>(s, name, flags, file, global);
3956540e570SSam Clegg };
3966540e570SSam Clegg
397136d27abSRui Ueyama if (wasInserted || s->isLazy()) {
398136d27abSRui Ueyama replaceSym();
399136d27abSRui Ueyama return s;
40093102974SSam Clegg }
40193102974SSam Clegg
402136d27abSRui Ueyama checkGlobalType(s, file, &global->getType());
403e3498ec5SRui Ueyama
404136d27abSRui Ueyama if (shouldReplace(s, file, flags))
405136d27abSRui Ueyama replaceSym();
406136d27abSRui Ueyama return s;
407e3498ec5SRui Ueyama }
408e3498ec5SRui Ueyama
addDefinedTag(StringRef name,uint32_t flags,InputFile * file,InputTag * tag)4091d891d44SHeejin Ahn Symbol *SymbolTable::addDefinedTag(StringRef name, uint32_t flags,
4101d891d44SHeejin Ahn InputFile *file, InputTag *tag) {
4111d891d44SHeejin Ahn LLVM_DEBUG(dbgs() << "addDefinedTag:" << name << "\n");
412e915a71fSHeejin Ahn
413136d27abSRui Ueyama Symbol *s;
414136d27abSRui Ueyama bool wasInserted;
415136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file);
416e915a71fSHeejin Ahn
417136d27abSRui Ueyama auto replaceSym = [&]() {
4181d891d44SHeejin Ahn replaceSymbol<DefinedTag>(s, name, flags, file, tag);
4196540e570SSam Clegg };
4206540e570SSam Clegg
421136d27abSRui Ueyama if (wasInserted || s->isLazy()) {
422136d27abSRui Ueyama replaceSym();
423136d27abSRui Ueyama return s;
424e915a71fSHeejin Ahn }
425e915a71fSHeejin Ahn
4263ec1760dSHeejin Ahn checkTagType(s, file, &tag->signature);
427e915a71fSHeejin Ahn
428136d27abSRui Ueyama if (shouldReplace(s, file, flags))
429136d27abSRui Ueyama replaceSym();
430136d27abSRui Ueyama return s;
431e915a71fSHeejin Ahn }
432e915a71fSHeejin Ahn
addDefinedTable(StringRef name,uint32_t flags,InputFile * file,InputTable * table)43353e3b81fSAndy Wingo Symbol *SymbolTable::addDefinedTable(StringRef name, uint32_t flags,
43453e3b81fSAndy Wingo InputFile *file, InputTable *table) {
43553e3b81fSAndy Wingo LLVM_DEBUG(dbgs() << "addDefinedTable:" << name << "\n");
43653e3b81fSAndy Wingo
43753e3b81fSAndy Wingo Symbol *s;
43853e3b81fSAndy Wingo bool wasInserted;
43953e3b81fSAndy Wingo std::tie(s, wasInserted) = insert(name, file);
44053e3b81fSAndy Wingo
44153e3b81fSAndy Wingo auto replaceSym = [&]() {
44253e3b81fSAndy Wingo replaceSymbol<DefinedTable>(s, name, flags, file, table);
44353e3b81fSAndy Wingo };
44453e3b81fSAndy Wingo
44553e3b81fSAndy Wingo if (wasInserted || s->isLazy()) {
44653e3b81fSAndy Wingo replaceSym();
44753e3b81fSAndy Wingo return s;
44853e3b81fSAndy Wingo }
44953e3b81fSAndy Wingo
45053e3b81fSAndy Wingo checkTableType(s, file, &table->getType());
45153e3b81fSAndy Wingo
45253e3b81fSAndy Wingo if (shouldReplace(s, file, flags))
45353e3b81fSAndy Wingo replaceSym();
45453e3b81fSAndy Wingo return s;
45553e3b81fSAndy Wingo }
45653e3b81fSAndy Wingo
45767b05584SSam Clegg // This function get called when an undefined symbol is added, and there is
45867b05584SSam Clegg // already an existing one in the symbols table. In this case we check that
45967b05584SSam Clegg // custom 'import-module' and 'import-field' symbol attributes agree.
4607ae3d335SKazuaki Ishizaki // With LTO these attributes are not available when the bitcode is read and only
46167b05584SSam Clegg // become available when the LTO object is read. In this case we silently
46267b05584SSam Clegg // replace the empty attributes with the valid ones.
46367b05584SSam Clegg template <typename T>
setImportAttributes(T * existing,Optional<StringRef> importName,Optional<StringRef> importModule,uint32_t flags,InputFile * file)464bd481277SSam Clegg static void setImportAttributes(T *existing, Optional<StringRef> importName,
465bd481277SSam Clegg Optional<StringRef> importModule,
466a57f1a54SSam Clegg uint32_t flags, InputFile *file) {
467bd481277SSam Clegg if (importName) {
468bd481277SSam Clegg if (!existing->importName)
46967b05584SSam Clegg existing->importName = importName;
47067b05584SSam Clegg if (existing->importName != importName)
47167b05584SSam Clegg error("import name mismatch for symbol: " + toString(*existing) +
472bd481277SSam Clegg "\n>>> defined as " + *existing->importName + " in " +
473bd481277SSam Clegg toString(existing->getFile()) + "\n>>> defined as " + *importName +
47467b05584SSam Clegg " in " + toString(file));
47567b05584SSam Clegg }
47667b05584SSam Clegg
477bd481277SSam Clegg if (importModule) {
478bd481277SSam Clegg if (!existing->importModule)
47967b05584SSam Clegg existing->importModule = importModule;
48067b05584SSam Clegg if (existing->importModule != importModule)
48167b05584SSam Clegg error("import module mismatch for symbol: " + toString(*existing) +
482bd481277SSam Clegg "\n>>> defined as " + *existing->importModule + " in " +
483bd481277SSam Clegg toString(existing->getFile()) + "\n>>> defined as " +
484bd481277SSam Clegg *importModule + " in " + toString(file));
48567b05584SSam Clegg }
486a57f1a54SSam Clegg
487a57f1a54SSam Clegg // Update symbol binding, if the existing symbol is weak
488a57f1a54SSam Clegg uint32_t binding = flags & WASM_SYMBOL_BINDING_MASK;
489a57f1a54SSam Clegg if (existing->isWeak() && binding != WASM_SYMBOL_BINDING_WEAK) {
490a57f1a54SSam Clegg existing->flags = (existing->flags & ~WASM_SYMBOL_BINDING_MASK) | binding;
491a57f1a54SSam Clegg }
49267b05584SSam Clegg }
49367b05584SSam Clegg
addUndefinedFunction(StringRef name,Optional<StringRef> importName,Optional<StringRef> importModule,uint32_t flags,InputFile * file,const WasmSignature * sig,bool isCalledDirectly)494bd481277SSam Clegg Symbol *SymbolTable::addUndefinedFunction(StringRef name,
495bd481277SSam Clegg Optional<StringRef> importName,
496bd481277SSam Clegg Optional<StringRef> importModule,
497136d27abSRui Ueyama uint32_t flags, InputFile *file,
498136d27abSRui Ueyama const WasmSignature *sig,
499136d27abSRui Ueyama bool isCalledDirectly) {
500136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " ["
501136d27abSRui Ueyama << (sig ? toString(*sig) : "none")
502a57f1a54SSam Clegg << "] IsCalledDirectly:" << isCalledDirectly << " flags=0x"
503a57f1a54SSam Clegg << utohexstr(flags) << "\n");
504f6f4b98fSSam Clegg assert(flags & WASM_SYMBOL_UNDEFINED);
505c03c9040SRui Ueyama
506136d27abSRui Ueyama Symbol *s;
507136d27abSRui Ueyama bool wasInserted;
508136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file);
509136d27abSRui Ueyama if (s->traced)
510136d27abSRui Ueyama printTraceSymbolUndefined(name, file);
511c729c1b4SSam Clegg
512136d27abSRui Ueyama auto replaceSym = [&]() {
513136d27abSRui Ueyama replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags,
514136d27abSRui Ueyama file, sig, isCalledDirectly);
5156540e570SSam Clegg };
5166540e570SSam Clegg
517b34ec596SSam Clegg if (wasInserted) {
518136d27abSRui Ueyama replaceSym();
519b34ec596SSam Clegg } else if (auto *lazy = dyn_cast<LazySymbol>(s)) {
520b34ec596SSam Clegg if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
521b34ec596SSam Clegg lazy->setWeak();
522b34ec596SSam Clegg lazy->signature = sig;
523b34ec596SSam Clegg } else {
524136d27abSRui Ueyama lazy->fetch();
525b34ec596SSam Clegg }
526b34ec596SSam Clegg } else {
527136d27abSRui Ueyama auto existingFunction = dyn_cast<FunctionSymbol>(s);
528136d27abSRui Ueyama if (!existingFunction) {
529136d27abSRui Ueyama reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
530136d27abSRui Ueyama return s;
5316540e570SSam Clegg }
532136d27abSRui Ueyama if (!existingFunction->signature && sig)
533136d27abSRui Ueyama existingFunction->signature = sig;
534a57f1a54SSam Clegg auto *existingUndefined = dyn_cast<UndefinedFunction>(existingFunction);
535fecfc592SSam Clegg if (isCalledDirectly && !signatureMatches(existingFunction, sig)) {
536a57f1a54SSam Clegg // If the existing undefined functions is not called directly then let
537fecfc592SSam Clegg // this one take precedence. Otherwise the existing function is either
538a57f1a54SSam Clegg // directly called or defined, in which case we need a function variant.
539fecfc592SSam Clegg if (existingUndefined && !existingUndefined->isCalledDirectly)
540136d27abSRui Ueyama replaceSym();
541fecfc592SSam Clegg else if (getFunctionVariant(s, sig, file, &s))
542fecfc592SSam Clegg replaceSym();
543fecfc592SSam Clegg }
544e4888be7SSam Clegg if (existingUndefined) {
545a57f1a54SSam Clegg setImportAttributes(existingUndefined, importName, importModule, flags,
546a57f1a54SSam Clegg file);
547e4888be7SSam Clegg if (isCalledDirectly)
548e4888be7SSam Clegg existingUndefined->isCalledDirectly = true;
549e4888be7SSam Clegg }
5506540e570SSam Clegg }
551cefbf9acSSam Clegg
552136d27abSRui Ueyama return s;
553c03c9040SRui Ueyama }
554c03c9040SRui Ueyama
addUndefinedData(StringRef name,uint32_t flags,InputFile * file)555136d27abSRui Ueyama Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags,
556136d27abSRui Ueyama InputFile *file) {
557136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n");
558f6f4b98fSSam Clegg assert(flags & WASM_SYMBOL_UNDEFINED);
55993102974SSam Clegg
560136d27abSRui Ueyama Symbol *s;
561136d27abSRui Ueyama bool wasInserted;
562136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file);
563136d27abSRui Ueyama if (s->traced)
564136d27abSRui Ueyama printTraceSymbolUndefined(name, file);
565f989a92dSSam Clegg
566b34ec596SSam Clegg if (wasInserted) {
567136d27abSRui Ueyama replaceSymbol<UndefinedData>(s, name, flags, file);
568b34ec596SSam Clegg } else if (auto *lazy = dyn_cast<LazySymbol>(s)) {
569b34ec596SSam Clegg if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK)
570b34ec596SSam Clegg lazy->setWeak();
571b34ec596SSam Clegg else
572136d27abSRui Ueyama lazy->fetch();
573b34ec596SSam Clegg } else if (s->isDefined()) {
574136d27abSRui Ueyama checkDataType(s, file);
575b34ec596SSam Clegg }
576136d27abSRui Ueyama return s;
577c94d393aSSam Clegg }
578c94d393aSSam Clegg
addUndefinedGlobal(StringRef name,Optional<StringRef> importName,Optional<StringRef> importModule,uint32_t flags,InputFile * file,const WasmGlobalType * type)579bd481277SSam Clegg Symbol *SymbolTable::addUndefinedGlobal(StringRef name,
580bd481277SSam Clegg Optional<StringRef> importName,
581bd481277SSam Clegg Optional<StringRef> importModule,
582bd481277SSam Clegg uint32_t flags, InputFile *file,
583136d27abSRui Ueyama const WasmGlobalType *type) {
584136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n");
585f6f4b98fSSam Clegg assert(flags & WASM_SYMBOL_UNDEFINED);
586e3498ec5SRui Ueyama
587136d27abSRui Ueyama Symbol *s;
588136d27abSRui Ueyama bool wasInserted;
589136d27abSRui Ueyama std::tie(s, wasInserted) = insert(name, file);
590136d27abSRui Ueyama if (s->traced)
591136d27abSRui Ueyama printTraceSymbolUndefined(name, file);
592c729c1b4SSam Clegg
593136d27abSRui Ueyama if (wasInserted)
594136d27abSRui Ueyama replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags,
595136d27abSRui Ueyama file, type);
596136d27abSRui Ueyama else if (auto *lazy = dyn_cast<LazySymbol>(s))
597136d27abSRui Ueyama lazy->fetch();
598136d27abSRui Ueyama else if (s->isDefined())
599136d27abSRui Ueyama checkGlobalType(s, file, type);
600136d27abSRui Ueyama return s;
601e3498ec5SRui Ueyama }
602e3498ec5SRui Ueyama
addUndefinedTable(StringRef name,Optional<StringRef> importName,Optional<StringRef> importModule,uint32_t flags,InputFile * file,const WasmTableType * type)60353e3b81fSAndy Wingo Symbol *SymbolTable::addUndefinedTable(StringRef name,
60453e3b81fSAndy Wingo Optional<StringRef> importName,
60553e3b81fSAndy Wingo Optional<StringRef> importModule,
60653e3b81fSAndy Wingo uint32_t flags, InputFile *file,
60753e3b81fSAndy Wingo const WasmTableType *type) {
60853e3b81fSAndy Wingo LLVM_DEBUG(dbgs() << "addUndefinedTable: " << name << "\n");
60953e3b81fSAndy Wingo assert(flags & WASM_SYMBOL_UNDEFINED);
61053e3b81fSAndy Wingo
61153e3b81fSAndy Wingo Symbol *s;
61253e3b81fSAndy Wingo bool wasInserted;
61353e3b81fSAndy Wingo std::tie(s, wasInserted) = insert(name, file);
61453e3b81fSAndy Wingo if (s->traced)
61553e3b81fSAndy Wingo printTraceSymbolUndefined(name, file);
61653e3b81fSAndy Wingo
61753e3b81fSAndy Wingo if (wasInserted)
61853e3b81fSAndy Wingo replaceSymbol<UndefinedTable>(s, name, importName, importModule, flags,
61953e3b81fSAndy Wingo file, type);
62053e3b81fSAndy Wingo else if (auto *lazy = dyn_cast<LazySymbol>(s))
62153e3b81fSAndy Wingo lazy->fetch();
62253e3b81fSAndy Wingo else if (s->isDefined())
62353e3b81fSAndy Wingo checkTableType(s, file, type);
62453e3b81fSAndy Wingo return s;
62553e3b81fSAndy Wingo }
62653e3b81fSAndy Wingo
addUndefinedTag(StringRef name,Optional<StringRef> importName,Optional<StringRef> importModule,uint32_t flags,InputFile * file,const WasmSignature * sig)6279261ee32SHeejin Ahn Symbol *SymbolTable::addUndefinedTag(StringRef name,
6289261ee32SHeejin Ahn Optional<StringRef> importName,
6299261ee32SHeejin Ahn Optional<StringRef> importModule,
6309261ee32SHeejin Ahn uint32_t flags, InputFile *file,
6319261ee32SHeejin Ahn const WasmSignature *sig) {
6329261ee32SHeejin Ahn LLVM_DEBUG(dbgs() << "addUndefinedTag: " << name << "\n");
6339261ee32SHeejin Ahn assert(flags & WASM_SYMBOL_UNDEFINED);
6349261ee32SHeejin Ahn
6359261ee32SHeejin Ahn Symbol *s;
6369261ee32SHeejin Ahn bool wasInserted;
6379261ee32SHeejin Ahn std::tie(s, wasInserted) = insert(name, file);
6389261ee32SHeejin Ahn if (s->traced)
6399261ee32SHeejin Ahn printTraceSymbolUndefined(name, file);
6409261ee32SHeejin Ahn
6419261ee32SHeejin Ahn if (wasInserted)
6429261ee32SHeejin Ahn replaceSymbol<UndefinedTag>(s, name, importName, importModule, flags, file,
6439261ee32SHeejin Ahn sig);
6449261ee32SHeejin Ahn else if (auto *lazy = dyn_cast<LazySymbol>(s))
6459261ee32SHeejin Ahn lazy->fetch();
6469261ee32SHeejin Ahn else if (s->isDefined())
6479261ee32SHeejin Ahn checkTagType(s, file, sig);
6489261ee32SHeejin Ahn return s;
6499261ee32SHeejin Ahn }
6509261ee32SHeejin Ahn
createUndefinedIndirectFunctionTable(StringRef name)651e638d8b2SAndy Wingo TableSymbol *SymbolTable::createUndefinedIndirectFunctionTable(StringRef name) {
652e638d8b2SAndy Wingo WasmLimits limits{0, 0, 0}; // Set by the writer.
653e638d8b2SAndy Wingo WasmTableType *type = make<WasmTableType>();
654e638d8b2SAndy Wingo type->ElemType = uint8_t(ValType::FUNCREF);
655e638d8b2SAndy Wingo type->Limits = limits;
656e638d8b2SAndy Wingo StringRef module(defaultModule);
657e638d8b2SAndy Wingo uint32_t flags = config->exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN;
658e638d8b2SAndy Wingo flags |= WASM_SYMBOL_UNDEFINED;
659e638d8b2SAndy Wingo Symbol *sym = addUndefinedTable(name, name, module, flags, nullptr, type);
660e638d8b2SAndy Wingo sym->markLive();
661e638d8b2SAndy Wingo sym->forceExport = config->exportTable;
662e638d8b2SAndy Wingo return cast<TableSymbol>(sym);
663e638d8b2SAndy Wingo }
664e638d8b2SAndy Wingo
createDefinedIndirectFunctionTable(StringRef name)665e638d8b2SAndy Wingo TableSymbol *SymbolTable::createDefinedIndirectFunctionTable(StringRef name) {
666e638d8b2SAndy Wingo const uint32_t invalidIndex = -1;
667e638d8b2SAndy Wingo WasmLimits limits{0, 0, 0}; // Set by the writer.
668e638d8b2SAndy Wingo WasmTableType type{uint8_t(ValType::FUNCREF), limits};
669e638d8b2SAndy Wingo WasmTable desc{invalidIndex, type, name};
670e638d8b2SAndy Wingo InputTable *table = make<InputTable>(desc, nullptr);
671e638d8b2SAndy Wingo uint32_t flags = config->exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN;
672e638d8b2SAndy Wingo TableSymbol *sym = addSyntheticTable(name, flags, table);
673e638d8b2SAndy Wingo sym->markLive();
674e638d8b2SAndy Wingo sym->forceExport = config->exportTable;
675e638d8b2SAndy Wingo return sym;
676e638d8b2SAndy Wingo }
677e638d8b2SAndy Wingo
678e638d8b2SAndy Wingo // Whether or not we need an indirect function table is usually a function of
679e638d8b2SAndy Wingo // whether an input declares a need for it. However sometimes it's possible for
680e638d8b2SAndy Wingo // no input to need the indirect function table, but then a late
681e638d8b2SAndy Wingo // addInternalGOTEntry causes a function to be allocated an address. In that
682e638d8b2SAndy Wingo // case address we synthesize a definition at the last minute.
resolveIndirectFunctionTable(bool required)683e638d8b2SAndy Wingo TableSymbol *SymbolTable::resolveIndirectFunctionTable(bool required) {
684e638d8b2SAndy Wingo Symbol *existing = find(functionTableName);
685e638d8b2SAndy Wingo if (existing) {
686e638d8b2SAndy Wingo if (!isa<TableSymbol>(existing)) {
687e638d8b2SAndy Wingo error(Twine("reserved symbol must be of type table: `") +
688e638d8b2SAndy Wingo functionTableName + "`");
689e638d8b2SAndy Wingo return nullptr;
690e638d8b2SAndy Wingo }
691e638d8b2SAndy Wingo if (existing->isDefined()) {
692e638d8b2SAndy Wingo error(Twine("reserved symbol must not be defined in input files: `") +
693e638d8b2SAndy Wingo functionTableName + "`");
694e638d8b2SAndy Wingo return nullptr;
695e638d8b2SAndy Wingo }
696e638d8b2SAndy Wingo }
697e638d8b2SAndy Wingo
698e638d8b2SAndy Wingo if (config->importTable) {
699e638d8b2SAndy Wingo if (existing)
700e638d8b2SAndy Wingo return cast<TableSymbol>(existing);
701e638d8b2SAndy Wingo if (required)
702e638d8b2SAndy Wingo return createUndefinedIndirectFunctionTable(functionTableName);
703e638d8b2SAndy Wingo } else if ((existing && existing->isLive()) || config->exportTable ||
704e638d8b2SAndy Wingo required) {
705e638d8b2SAndy Wingo // A defined table is required. Either because the user request an exported
706e638d8b2SAndy Wingo // table or because the table symbol is already live. The existing table is
707e638d8b2SAndy Wingo // guaranteed to be undefined due to the check above.
708e638d8b2SAndy Wingo return createDefinedIndirectFunctionTable(functionTableName);
709e638d8b2SAndy Wingo }
710e638d8b2SAndy Wingo
711e638d8b2SAndy Wingo // An indirect function table will only be present in the symbol table if
712e638d8b2SAndy Wingo // needed by a reloc; if we get here, we don't need one.
713e638d8b2SAndy Wingo return nullptr;
714e638d8b2SAndy Wingo }
715e638d8b2SAndy Wingo
addLazy(ArchiveFile * file,const Archive::Symbol * sym)716136d27abSRui Ueyama void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) {
717136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "addLazy: " << sym->getName() << "\n");
718136d27abSRui Ueyama StringRef name = sym->getName();
719c03c9040SRui Ueyama
720136d27abSRui Ueyama Symbol *s;
721136d27abSRui Ueyama bool wasInserted;
722136d27abSRui Ueyama std::tie(s, wasInserted) = insertName(name);
723c03c9040SRui Ueyama
724136d27abSRui Ueyama if (wasInserted) {
725136d27abSRui Ueyama replaceSymbol<LazySymbol>(s, name, 0, file, *sym);
726c03c9040SRui Ueyama return;
727c03c9040SRui Ueyama }
728c03c9040SRui Ueyama
729136d27abSRui Ueyama if (!s->isUndefined())
73037b4ee52SSam Clegg return;
73137b4ee52SSam Clegg
73237b4ee52SSam Clegg // The existing symbol is undefined, load a new one from the archive,
7337ae3d335SKazuaki Ishizaki // unless the existing symbol is weak in which case replace the undefined
73437b4ee52SSam Clegg // symbols with a LazySymbol.
735136d27abSRui Ueyama if (s->isWeak()) {
736136d27abSRui Ueyama const WasmSignature *oldSig = nullptr;
73737b4ee52SSam Clegg // In the case of an UndefinedFunction we need to preserve the expected
73837b4ee52SSam Clegg // signature.
739136d27abSRui Ueyama if (auto *f = dyn_cast<UndefinedFunction>(s))
740136d27abSRui Ueyama oldSig = f->signature;
74137b4ee52SSam Clegg LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n");
742136d27abSRui Ueyama auto newSym = replaceSymbol<LazySymbol>(s, name, WASM_SYMBOL_BINDING_WEAK,
743136d27abSRui Ueyama file, *sym);
744136d27abSRui Ueyama newSym->signature = oldSig;
74537b4ee52SSam Clegg return;
74637b4ee52SSam Clegg }
74737b4ee52SSam Clegg
748e7245b42SNicola Zaghen LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
749136d27abSRui Ueyama file->addMember(sym);
750c94d393aSSam Clegg }
751e0f6fcd0SSam Clegg
addComdat(StringRef name)752136d27abSRui Ueyama bool SymbolTable::addComdat(StringRef name) {
753136d27abSRui Ueyama return comdatGroups.insert(CachedHashStringRef(name)).second;
754e0f6fcd0SSam Clegg }
7551f3f774fSSam Clegg
7566540e570SSam Clegg // The new signature doesn't match. Create a variant to the symbol with the
7576540e570SSam Clegg // signature encoded in the name and return that instead. These symbols are
7586540e570SSam Clegg // then unified later in handleSymbolVariants.
getFunctionVariant(Symbol * sym,const WasmSignature * sig,const InputFile * file,Symbol ** out)759136d27abSRui Ueyama bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig,
760136d27abSRui Ueyama const InputFile *file, Symbol **out) {
761136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "getFunctionVariant: " << sym->getName() << " -> "
762136d27abSRui Ueyama << " " << toString(*sig) << "\n");
763136d27abSRui Ueyama Symbol *variant = nullptr;
7646540e570SSam Clegg
7656540e570SSam Clegg // Linear search through symbol variants. Should never be more than two
7666540e570SSam Clegg // or three entries here.
767136d27abSRui Ueyama auto &variants = symVariants[CachedHashStringRef(sym->getName())];
768136d27abSRui Ueyama if (variants.empty())
769136d27abSRui Ueyama variants.push_back(sym);
7706540e570SSam Clegg
771136d27abSRui Ueyama for (Symbol* v : variants) {
772136d27abSRui Ueyama if (*v->getSignature() == *sig) {
773136d27abSRui Ueyama variant = v;
7746540e570SSam Clegg break;
7756540e570SSam Clegg }
7766540e570SSam Clegg }
7776540e570SSam Clegg
778136d27abSRui Ueyama bool wasAdded = !variant;
779136d27abSRui Ueyama if (wasAdded) {
7806540e570SSam Clegg // Create a new variant;
7816540e570SSam Clegg LLVM_DEBUG(dbgs() << "added new variant\n");
782136d27abSRui Ueyama variant = reinterpret_cast<Symbol *>(make<SymbolUnion>());
783064e9907SSam Clegg variant->isUsedInRegularObj =
784064e9907SSam Clegg !file || file->kind() == InputFile::ObjectKind;
785064e9907SSam Clegg variant->canInline = true;
786064e9907SSam Clegg variant->traced = false;
787064e9907SSam Clegg variant->forceExport = false;
788136d27abSRui Ueyama variants.push_back(variant);
7896540e570SSam Clegg } else {
790136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant) << "\n");
791136d27abSRui Ueyama assert(*variant->getSignature() == *sig);
7926540e570SSam Clegg }
7936540e570SSam Clegg
794136d27abSRui Ueyama *out = variant;
795136d27abSRui Ueyama return wasAdded;
7966540e570SSam Clegg }
7976540e570SSam Clegg
7981f3f774fSSam Clegg // Set a flag for --trace-symbol so that we can print out a log message
7991f3f774fSSam Clegg // if a new symbol with the same name is inserted into the symbol table.
trace(StringRef name)800136d27abSRui Ueyama void SymbolTable::trace(StringRef name) {
801136d27abSRui Ueyama symMap.insert({CachedHashStringRef(name), -1});
8021f3f774fSSam Clegg }
803230dc11dSSam Clegg
wrap(Symbol * sym,Symbol * real,Symbol * wrap)804136d27abSRui Ueyama void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
805a5ca34e6SSam Clegg // Swap symbols as instructed by -wrap.
806136d27abSRui Ueyama int &origIdx = symMap[CachedHashStringRef(sym->getName())];
807136d27abSRui Ueyama int &realIdx= symMap[CachedHashStringRef(real->getName())];
808136d27abSRui Ueyama int &wrapIdx = symMap[CachedHashStringRef(wrap->getName())];
809136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "wrap: " << sym->getName() << "\n");
810a5ca34e6SSam Clegg
811a5ca34e6SSam Clegg // Anyone looking up __real symbols should get the original
812136d27abSRui Ueyama realIdx = origIdx;
813a5ca34e6SSam Clegg // Anyone looking up the original should get the __wrap symbol
814136d27abSRui Ueyama origIdx = wrapIdx;
815a5ca34e6SSam Clegg }
816a5ca34e6SSam Clegg
817136d27abSRui Ueyama static const uint8_t unreachableFn[] = {
818230dc11dSSam Clegg 0x03 /* ULEB length */, 0x00 /* ULEB num locals */,
819230dc11dSSam Clegg 0x00 /* opcode unreachable */, 0x0b /* opcode end */
820230dc11dSSam Clegg };
821230dc11dSSam Clegg
822230dc11dSSam Clegg // Replace the given symbol body with an unreachable function.
823230dc11dSSam Clegg // This is used by handleWeakUndefines in order to generate a callable
8246540e570SSam Clegg // equivalent of an undefined function and also handleSymbolVariants for
8256540e570SSam Clegg // undefined functions that don't match the signature of the definition.
replaceWithUnreachable(Symbol * sym,const WasmSignature & sig,StringRef debugName)826136d27abSRui Ueyama InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym,
827136d27abSRui Ueyama const WasmSignature &sig,
828136d27abSRui Ueyama StringRef debugName) {
829136d27abSRui Ueyama auto *func = make<SyntheticFunction>(sig, sym->getName(), debugName);
830136d27abSRui Ueyama func->setBody(unreachableFn);
831136d27abSRui Ueyama syntheticFunctions.emplace_back(func);
8324b8e2d8eSSam Clegg // Mark new symbols as local. For relocatable output we don't want them
8334b8e2d8eSSam Clegg // to be exported outside the object file.
8344b8e2d8eSSam Clegg replaceSymbol<DefinedFunction>(sym, debugName, WASM_SYMBOL_BINDING_LOCAL,
8354b8e2d8eSSam Clegg nullptr, func);
83648ddf5e1SSam Clegg // Ensure the stub function doesn't get a table entry. Its address
83748ddf5e1SSam Clegg // should always compare equal to the null pointer.
83848ddf5e1SSam Clegg sym->isStub = true;
839136d27abSRui Ueyama return func;
840230dc11dSSam Clegg }
841230dc11dSSam Clegg
replaceWithUndefined(Symbol * sym)842206884bfSSam Clegg void SymbolTable::replaceWithUndefined(Symbol *sym) {
843206884bfSSam Clegg // Add a synthetic dummy for weak undefined functions. These dummies will
844206884bfSSam Clegg // be GC'd if not used as the target of any "call" instructions.
84583d59e05SAlexandre Ganea StringRef debugName = saver().save("undefined_weak:" + toString(*sym));
846206884bfSSam Clegg replaceWithUnreachable(sym, *sym->getSignature(), debugName);
847206884bfSSam Clegg // Hide our dummy to prevent export.
848206884bfSSam Clegg sym->setHidden(true);
849206884bfSSam Clegg }
850206884bfSSam Clegg
851230dc11dSSam Clegg // For weak undefined functions, there may be "call" instructions that reference
852230dc11dSSam Clegg // the symbol. In this case, we need to synthesise a dummy/stub function that
853230dc11dSSam Clegg // will abort at runtime, so that relocations can still provided an operand to
854230dc11dSSam Clegg // the call instruction that passes Wasm validation.
handleWeakUndefines()855230dc11dSSam Clegg void SymbolTable::handleWeakUndefines() {
856136d27abSRui Ueyama for (Symbol *sym : getSymbols()) {
857*0e8f4ce3SSam Clegg if (sym->isUndefWeak() && sym->isUsedInRegularObj) {
858206884bfSSam Clegg if (sym->getSignature()) {
859206884bfSSam Clegg replaceWithUndefined(sym);
860206884bfSSam Clegg } else {
861206884bfSSam Clegg // It is possible for undefined functions not to have a signature (eg.
862206884bfSSam Clegg // if added via "--undefined"), but weak undefined ones do have a
863206884bfSSam Clegg // signature. Lazy symbols may not be functions and therefore Sig can
864206884bfSSam Clegg // still be null in some circumstance.
865136d27abSRui Ueyama assert(!isa<FunctionSymbol>(sym));
866206884bfSSam Clegg }
867206884bfSSam Clegg }
868206884bfSSam Clegg }
8696540e570SSam Clegg }
870230dc11dSSam Clegg
createUndefinedStub(const WasmSignature & sig)871206884bfSSam Clegg DefinedFunction *SymbolTable::createUndefinedStub(const WasmSignature &sig) {
872206884bfSSam Clegg if (stubFunctions.count(sig))
873206884bfSSam Clegg return stubFunctions[sig];
874206884bfSSam Clegg LLVM_DEBUG(dbgs() << "createUndefinedStub: " << toString(sig) << "\n");
875206884bfSSam Clegg auto *sym = reinterpret_cast<DefinedFunction *>(make<SymbolUnion>());
876206884bfSSam Clegg sym->isUsedInRegularObj = true;
877206884bfSSam Clegg sym->canInline = true;
878206884bfSSam Clegg sym->traced = false;
879206884bfSSam Clegg sym->forceExport = false;
880206884bfSSam Clegg sym->signature = &sig;
881206884bfSSam Clegg replaceSymbol<DefinedFunction>(
882206884bfSSam Clegg sym, "undefined_stub", WASM_SYMBOL_VISIBILITY_HIDDEN, nullptr, nullptr);
883206884bfSSam Clegg replaceWithUnreachable(sym, sig, "undefined_stub");
884206884bfSSam Clegg stubFunctions[sig] = sym;
885206884bfSSam Clegg return sym;
886230dc11dSSam Clegg }
8876540e570SSam Clegg
reportFunctionSignatureMismatch(StringRef symName,FunctionSymbol * a,FunctionSymbol * b,bool isError)888136d27abSRui Ueyama static void reportFunctionSignatureMismatch(StringRef symName,
889136d27abSRui Ueyama FunctionSymbol *a,
890136d27abSRui Ueyama FunctionSymbol *b, bool isError) {
891136d27abSRui Ueyama std::string msg = ("function signature mismatch: " + symName +
892136d27abSRui Ueyama "\n>>> defined as " + toString(*a->signature) + " in " +
893136d27abSRui Ueyama toString(a->getFile()) + "\n>>> defined as " +
894136d27abSRui Ueyama toString(*b->signature) + " in " + toString(b->getFile()))
8956540e570SSam Clegg .str();
896136d27abSRui Ueyama if (isError)
8976540e570SSam Clegg error(msg);
8986540e570SSam Clegg else
8996540e570SSam Clegg warn(msg);
9006540e570SSam Clegg }
9016540e570SSam Clegg
9026540e570SSam Clegg // Remove any variant symbols that were created due to function signature
9036540e570SSam Clegg // mismatches.
handleSymbolVariants()9046540e570SSam Clegg void SymbolTable::handleSymbolVariants() {
905136d27abSRui Ueyama for (auto pair : symVariants) {
9066540e570SSam Clegg // Push the initial symbol onto the list of variants.
907136d27abSRui Ueyama StringRef symName = pair.first.val();
908136d27abSRui Ueyama std::vector<Symbol *> &variants = pair.second;
9096540e570SSam Clegg
9106540e570SSam Clegg #ifndef NDEBUG
911136d27abSRui Ueyama LLVM_DEBUG(dbgs() << "symbol with (" << variants.size()
912136d27abSRui Ueyama << ") variants: " << symName << "\n");
913136d27abSRui Ueyama for (auto *s: variants) {
914136d27abSRui Ueyama auto *f = cast<FunctionSymbol>(s);
915136d27abSRui Ueyama LLVM_DEBUG(dbgs() << " variant: " + f->getName() << " "
916136d27abSRui Ueyama << toString(*f->signature) << "\n");
9176540e570SSam Clegg }
9186540e570SSam Clegg #endif
9196540e570SSam Clegg
9206540e570SSam Clegg // Find the one definition.
921136d27abSRui Ueyama DefinedFunction *defined = nullptr;
922136d27abSRui Ueyama for (auto *symbol : variants) {
923136d27abSRui Ueyama if (auto f = dyn_cast<DefinedFunction>(symbol)) {
924136d27abSRui Ueyama defined = f;
9256540e570SSam Clegg break;
9266540e570SSam Clegg }
9276540e570SSam Clegg }
9286540e570SSam Clegg
9296540e570SSam Clegg // If there are no definitions, and the undefined symbols disagree on
9306540e570SSam Clegg // the signature, there is not we can do since we don't know which one
9316540e570SSam Clegg // to use as the signature on the import.
932136d27abSRui Ueyama if (!defined) {
933136d27abSRui Ueyama reportFunctionSignatureMismatch(symName,
934136d27abSRui Ueyama cast<FunctionSymbol>(variants[0]),
935136d27abSRui Ueyama cast<FunctionSymbol>(variants[1]), true);
9366540e570SSam Clegg return;
9376540e570SSam Clegg }
9386540e570SSam Clegg
939136d27abSRui Ueyama for (auto *symbol : variants) {
940136d27abSRui Ueyama if (symbol != defined) {
941136d27abSRui Ueyama auto *f = cast<FunctionSymbol>(symbol);
942136d27abSRui Ueyama reportFunctionSignatureMismatch(symName, f, defined, false);
94383d59e05SAlexandre Ganea StringRef debugName =
94483d59e05SAlexandre Ganea saver().save("signature_mismatch:" + toString(*f));
945136d27abSRui Ueyama replaceWithUnreachable(f, *f->signature, debugName);
9466540e570SSam Clegg }
9476540e570SSam Clegg }
9486540e570SSam Clegg }
9496540e570SSam Clegg }
95033c59abfSFangrui Song
95133c59abfSFangrui Song } // namespace wasm
95233c59abfSFangrui Song } // namespace lld
953