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"
14c94d393aSSam Clegg #include "lld/Common/ErrorHandler.h"
152017d52bSRui Ueyama #include "lld/Common/Memory.h"
167d67dd11SRui Ueyama #include "llvm/ADT/SetVector.h"
17c94d393aSSam Clegg 
18c94d393aSSam Clegg #define DEBUG_TYPE "lld"
19c94d393aSSam Clegg 
20c94d393aSSam Clegg using namespace llvm;
2120db381bSSam Clegg using namespace llvm::wasm;
2245218f4aSSam Clegg using namespace llvm::object;
23c94d393aSSam Clegg 
2433c59abfSFangrui Song namespace lld {
2533c59abfSFangrui Song namespace wasm {
2633c59abfSFangrui Song SymbolTable *symtab;
27c94d393aSSam Clegg 
28136d27abSRui Ueyama void SymbolTable::addFile(InputFile *file) {
29136d27abSRui Ueyama   log("Processing: " + toString(file));
30a282a61bSSam Clegg 
31a282a61bSSam Clegg   // .a file
32136d27abSRui Ueyama   if (auto *f = dyn_cast<ArchiveFile>(file)) {
33136d27abSRui Ueyama     f->parse();
34a282a61bSSam Clegg     return;
35a282a61bSSam Clegg   }
36a282a61bSSam Clegg 
37a282a61bSSam Clegg   // .so file
38136d27abSRui Ueyama   if (auto *f = dyn_cast<SharedFile>(file)) {
39136d27abSRui Ueyama     sharedFiles.push_back(f);
40a282a61bSSam Clegg     return;
41a282a61bSSam Clegg   }
42a282a61bSSam Clegg 
43136d27abSRui Ueyama   if (config->trace)
44136d27abSRui Ueyama     message(toString(file));
45c94d393aSSam Clegg 
46c729c1b4SSam Clegg   // LLVM bitcode file
47136d27abSRui Ueyama   if (auto *f = dyn_cast<BitcodeFile>(file)) {
48136d27abSRui Ueyama     f->parse();
49136d27abSRui Ueyama     bitcodeFiles.push_back(f);
50a282a61bSSam Clegg     return;
51a282a61bSSam Clegg   }
52a282a61bSSam Clegg 
53a282a61bSSam Clegg   // Regular object file
54136d27abSRui Ueyama   auto *f = cast<ObjFile>(file);
55136d27abSRui Ueyama   f->parse(false);
56136d27abSRui Ueyama   objectFiles.push_back(f);
57c94d393aSSam Clegg }
58c94d393aSSam Clegg 
59c729c1b4SSam Clegg // This function is where all the optimizations of link-time
60c729c1b4SSam Clegg // optimization happens. When LTO is in use, some input files are
61c729c1b4SSam Clegg // not in native object file format but in the LLVM bitcode format.
62c729c1b4SSam Clegg // This function compiles bitcode files into a few big native files
63c729c1b4SSam Clegg // using LLVM functions and replaces bitcode symbols with the results.
64c729c1b4SSam Clegg // Because all bitcode files that the program consists of are passed
65c729c1b4SSam Clegg // to the compiler at once, it can do whole-program optimization.
66c729c1b4SSam Clegg void SymbolTable::addCombinedLTOObject() {
67b062fe18SSam Clegg   // Prevent further LTO objects being included
68b062fe18SSam Clegg   BitcodeFile::doneLTO = true;
69b062fe18SSam Clegg 
70136d27abSRui Ueyama   if (bitcodeFiles.empty())
71c729c1b4SSam Clegg     return;
72c729c1b4SSam Clegg 
73c729c1b4SSam Clegg   // Compile bitcode files and replace bitcode symbols.
74136d27abSRui Ueyama   lto.reset(new BitcodeCompiler);
75136d27abSRui Ueyama   for (BitcodeFile *f : bitcodeFiles)
76136d27abSRui Ueyama     lto->add(*f);
77c729c1b4SSam Clegg 
78136d27abSRui Ueyama   for (StringRef filename : lto->compile()) {
79136d27abSRui Ueyama     auto *obj = make<ObjFile>(MemoryBufferRef(filename, "lto.tmp"), "");
80136d27abSRui Ueyama     obj->parse(true);
81136d27abSRui Ueyama     objectFiles.push_back(obj);
82c729c1b4SSam Clegg   }
83c729c1b4SSam Clegg }
84c729c1b4SSam Clegg 
85136d27abSRui Ueyama Symbol *SymbolTable::find(StringRef name) {
86136d27abSRui Ueyama   auto it = symMap.find(CachedHashStringRef(name));
87136d27abSRui Ueyama   if (it == symMap.end() || it->second == -1)
881f3f774fSSam Clegg     return nullptr;
89136d27abSRui Ueyama   return symVector[it->second];
901f3f774fSSam Clegg }
911f3f774fSSam Clegg 
92136d27abSRui Ueyama void SymbolTable::replace(StringRef name, Symbol* sym) {
93136d27abSRui Ueyama   auto it = symMap.find(CachedHashStringRef(name));
94136d27abSRui Ueyama   symVector[it->second] = sym;
956540e570SSam Clegg }
966540e570SSam Clegg 
97136d27abSRui Ueyama std::pair<Symbol *, bool> SymbolTable::insertName(StringRef name) {
98136d27abSRui Ueyama   bool trace = false;
99136d27abSRui Ueyama   auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()});
100136d27abSRui Ueyama   int &symIndex = p.first->second;
101136d27abSRui Ueyama   bool isNew = p.second;
102136d27abSRui Ueyama   if (symIndex == -1) {
103136d27abSRui Ueyama     symIndex = symVector.size();
104136d27abSRui Ueyama     trace = true;
105136d27abSRui Ueyama     isNew = true;
1061f3f774fSSam Clegg   }
1071f3f774fSSam Clegg 
108136d27abSRui Ueyama   if (!isNew)
109136d27abSRui Ueyama     return {symVector[symIndex], false};
1101f3f774fSSam Clegg 
111136d27abSRui Ueyama   Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
112136d27abSRui Ueyama   sym->isUsedInRegularObj = false;
113136d27abSRui Ueyama   sym->canInline = true;
114136d27abSRui Ueyama   sym->traced = trace;
115064e9907SSam Clegg   sym->forceExport = false;
116136d27abSRui Ueyama   symVector.emplace_back(sym);
117136d27abSRui Ueyama   return {sym, true};
118c94d393aSSam Clegg }
119c94d393aSSam Clegg 
120136d27abSRui Ueyama std::pair<Symbol *, bool> SymbolTable::insert(StringRef name,
121136d27abSRui Ueyama                                               const InputFile *file) {
122136d27abSRui Ueyama   Symbol *s;
123136d27abSRui Ueyama   bool wasInserted;
124136d27abSRui Ueyama   std::tie(s, wasInserted) = insertName(name);
1251f3f774fSSam Clegg 
126136d27abSRui Ueyama   if (!file || file->kind() == InputFile::ObjectKind)
127136d27abSRui Ueyama     s->isUsedInRegularObj = true;
1281f3f774fSSam Clegg 
129136d27abSRui Ueyama   return {s, wasInserted};
130c94d393aSSam Clegg }
131c94d393aSSam Clegg 
132136d27abSRui Ueyama static void reportTypeError(const Symbol *existing, const InputFile *file,
133136d27abSRui Ueyama                             llvm::wasm::WasmSymbolType type) {
134136d27abSRui Ueyama   error("symbol type mismatch: " + toString(*existing) + "\n>>> defined as " +
135136d27abSRui Ueyama         toString(existing->getWasmType()) + " in " +
136136d27abSRui Ueyama         toString(existing->getFile()) + "\n>>> defined as " + toString(type) +
137136d27abSRui Ueyama         " in " + toString(file));
138e3498ec5SRui Ueyama }
139e3498ec5SRui Ueyama 
1406f4286fbSHeejin Ahn // Check the type of new symbol matches that of the symbol is replacing.
1417c5fcb35SKazuaki Ishizaki // Returns true if the function types match, false is there is a signature
1426540e570SSam Clegg // mismatch.
143136d27abSRui Ueyama static bool signatureMatches(FunctionSymbol *existing,
144136d27abSRui Ueyama                              const WasmSignature *newSig) {
145136d27abSRui Ueyama   const WasmSignature *oldSig = existing->signature;
146d506b0a4SSam Clegg 
147d506b0a4SSam Clegg   // If either function is missing a signature (this happend for bitcode
148d506b0a4SSam Clegg   // symbols) then assume they match.  Any mismatch will be reported later
149d506b0a4SSam Clegg   // when the LTO objects are added.
150136d27abSRui Ueyama   if (!newSig || !oldSig)
1516540e570SSam Clegg     return true;
152cefbf9acSSam Clegg 
153136d27abSRui Ueyama   return *newSig == *oldSig;
154e3498ec5SRui Ueyama }
155e3498ec5SRui Ueyama 
156136d27abSRui Ueyama static void checkGlobalType(const Symbol *existing, const InputFile *file,
157136d27abSRui Ueyama                             const WasmGlobalType *newType) {
158136d27abSRui Ueyama   if (!isa<GlobalSymbol>(existing)) {
159136d27abSRui Ueyama     reportTypeError(existing, file, WASM_SYMBOL_TYPE_GLOBAL);
160b8621596SSam Clegg     return;
161b8621596SSam Clegg   }
162b8621596SSam Clegg 
163136d27abSRui Ueyama   const WasmGlobalType *oldType = cast<GlobalSymbol>(existing)->getGlobalType();
164136d27abSRui Ueyama   if (*newType != *oldType) {
165136d27abSRui Ueyama     error("Global type mismatch: " + existing->getName() + "\n>>> defined as " +
166136d27abSRui Ueyama           toString(*oldType) + " in " + toString(existing->getFile()) +
167136d27abSRui Ueyama           "\n>>> defined as " + toString(*newType) + " in " + toString(file));
168c94d393aSSam Clegg   }
16924b3dcd4SSam Clegg }
17024b3dcd4SSam Clegg 
1711d891d44SHeejin Ahn static void checkTagType(const Symbol *existing, const InputFile *file,
1721d891d44SHeejin Ahn                          const WasmTagType *newType,
173136d27abSRui Ueyama                          const WasmSignature *newSig) {
1741d891d44SHeejin Ahn   const auto *existingTag = dyn_cast<TagSymbol>(existing);
1751d891d44SHeejin Ahn   if (!isa<TagSymbol>(existing)) {
1761d891d44SHeejin Ahn     reportTypeError(existing, file, WASM_SYMBOL_TYPE_TAG);
177e915a71fSHeejin Ahn     return;
178e915a71fSHeejin Ahn   }
179e915a71fSHeejin Ahn 
1801d891d44SHeejin Ahn   const WasmTagType *oldType = cast<TagSymbol>(existing)->getTagType();
1811d891d44SHeejin Ahn   const WasmSignature *oldSig = existingTag->signature;
182136d27abSRui Ueyama   if (newType->Attribute != oldType->Attribute)
1831d891d44SHeejin Ahn     error("Tag type mismatch: " + existing->getName() + "\n>>> defined as " +
184136d27abSRui Ueyama           toString(*oldType) + " in " + toString(existing->getFile()) +
185136d27abSRui Ueyama           "\n>>> defined as " + toString(*newType) + " in " + toString(file));
186136d27abSRui Ueyama   if (*newSig != *oldSig)
1871d891d44SHeejin Ahn     warn("Tag signature mismatch: " + existing->getName() +
188136d27abSRui Ueyama          "\n>>> defined as " + toString(*oldSig) + " in " +
189136d27abSRui Ueyama          toString(existing->getFile()) + "\n>>> defined as " +
190136d27abSRui Ueyama          toString(*newSig) + " in " + toString(file));
191e915a71fSHeejin Ahn }
192e915a71fSHeejin Ahn 
19353e3b81fSAndy Wingo static void checkTableType(const Symbol *existing, const InputFile *file,
19453e3b81fSAndy Wingo                            const WasmTableType *newType) {
19553e3b81fSAndy Wingo   if (!isa<TableSymbol>(existing)) {
19653e3b81fSAndy Wingo     reportTypeError(existing, file, WASM_SYMBOL_TYPE_TABLE);
19753e3b81fSAndy Wingo     return;
19853e3b81fSAndy Wingo   }
19953e3b81fSAndy Wingo 
20053e3b81fSAndy Wingo   const WasmTableType *oldType = cast<TableSymbol>(existing)->getTableType();
20153e3b81fSAndy Wingo   if (newType->ElemType != oldType->ElemType) {
20253e3b81fSAndy Wingo     error("Table type mismatch: " + existing->getName() + "\n>>> defined as " +
20353e3b81fSAndy Wingo           toString(*oldType) + " in " + toString(existing->getFile()) +
20453e3b81fSAndy Wingo           "\n>>> defined as " + toString(*newType) + " in " + toString(file));
20553e3b81fSAndy Wingo   }
20653e3b81fSAndy Wingo   // FIXME: No assertions currently on the limits.
20753e3b81fSAndy Wingo }
20853e3b81fSAndy Wingo 
209136d27abSRui Ueyama static void checkDataType(const Symbol *existing, const InputFile *file) {
210136d27abSRui Ueyama   if (!isa<DataSymbol>(existing))
211136d27abSRui Ueyama     reportTypeError(existing, file, WASM_SYMBOL_TYPE_DATA);
212e3498ec5SRui Ueyama }
213e3498ec5SRui Ueyama 
214136d27abSRui Ueyama DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name,
215136d27abSRui Ueyama                                                    uint32_t flags,
216136d27abSRui Ueyama                                                    InputFunction *function) {
217136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n");
218136d27abSRui Ueyama   assert(!find(name));
219136d27abSRui Ueyama   syntheticFunctions.emplace_back(function);
220136d27abSRui Ueyama   return replaceSymbol<DefinedFunction>(insertName(name).first, name,
221136d27abSRui Ueyama                                         flags, nullptr, function);
22250686856SSam Clegg }
22350686856SSam Clegg 
22429a3056bSSam Clegg // Adds an optional, linker generated, data symbol.  The symbol will only be
2257ae3d335SKazuaki Ishizaki // added if there is an undefine reference to it, or if it is explicitly
2267ae3d335SKazuaki Ishizaki // exported via the --export flag.  Otherwise we don't add the symbol and return
2277ae3d335SKazuaki Ishizaki // nullptr.
228caa0db13SSam Clegg DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name,
229582fd474SWouter van Oortmerssen                                                 uint64_t value) {
230136d27abSRui Ueyama   Symbol *s = find(name);
231136d27abSRui Ueyama   if (!s && (config->exportAll || config->exportedSymbols.count(name) != 0))
232136d27abSRui Ueyama     s = insertName(name).first;
233136d27abSRui Ueyama   else if (!s || s->isDefined())
2344bce63a0SSam Clegg     return nullptr;
235136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n");
236caa0db13SSam Clegg   auto *rtn = replaceSymbol<DefinedData>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN);
23714ffbb84SSam Clegg   rtn->setVA(value);
238136d27abSRui Ueyama   rtn->referenced = true;
2394bce63a0SSam Clegg   return rtn;
2404bce63a0SSam Clegg }
2414bce63a0SSam Clegg 
242136d27abSRui Ueyama DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef name,
243136d27abSRui Ueyama                                                  uint32_t flags) {
244136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << name << "\n");
245136d27abSRui Ueyama   assert(!find(name));
246136d27abSRui Ueyama   return replaceSymbol<DefinedData>(insertName(name).first, name, flags);
247c94d393aSSam Clegg }
248c94d393aSSam Clegg 
249136d27abSRui Ueyama DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags,
250136d27abSRui Ueyama                                                InputGlobal *global) {
251136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global
252e7245b42SNicola Zaghen                     << "\n");
253136d27abSRui Ueyama   assert(!find(name));
254136d27abSRui Ueyama   syntheticGlobals.emplace_back(global);
255136d27abSRui Ueyama   return replaceSymbol<DefinedGlobal>(insertName(name).first, name, flags,
256136d27abSRui Ueyama                                       nullptr, global);
25793102974SSam Clegg }
25893102974SSam Clegg 
25988e4056bSSam Clegg DefinedGlobal *SymbolTable::addOptionalGlobalSymbol(StringRef name,
26029a3056bSSam Clegg                                                     InputGlobal *global) {
26129a3056bSSam Clegg   Symbol *s = find(name);
26229a3056bSSam Clegg   if (!s || s->isDefined())
26329a3056bSSam Clegg     return nullptr;
264*8fe12847SSam Clegg   LLVM_DEBUG(dbgs() << "addOptionalGlobalSymbol: " << name << " -> " << global
265*8fe12847SSam Clegg                     << "\n");
26629a3056bSSam Clegg   syntheticGlobals.emplace_back(global);
26788e4056bSSam Clegg   return replaceSymbol<DefinedGlobal>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN,
26888e4056bSSam Clegg                                       nullptr, global);
26929a3056bSSam Clegg }
27029a3056bSSam Clegg 
27163393828SAndy Wingo DefinedTable *SymbolTable::addSyntheticTable(StringRef name, uint32_t flags,
27263393828SAndy Wingo                                              InputTable *table) {
27363393828SAndy Wingo   LLVM_DEBUG(dbgs() << "addSyntheticTable: " << name << " -> " << table
27463393828SAndy Wingo                     << "\n");
27563393828SAndy Wingo   Symbol *s = find(name);
27663393828SAndy Wingo   assert(!s || s->isUndefined());
27763393828SAndy Wingo   if (!s)
27863393828SAndy Wingo     s = insertName(name).first;
27963393828SAndy Wingo   syntheticTables.emplace_back(table);
28063393828SAndy Wingo   return replaceSymbol<DefinedTable>(s, name, flags, nullptr, table);
28163393828SAndy Wingo }
28263393828SAndy Wingo 
283136d27abSRui Ueyama static bool shouldReplace(const Symbol *existing, InputFile *newFile,
284136d27abSRui Ueyama                           uint32_t newFlags) {
285c03c9040SRui Ueyama   // If existing symbol is undefined, replace it.
286136d27abSRui Ueyama   if (!existing->isDefined()) {
287e7245b42SNicola Zaghen     LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
288136d27abSRui Ueyama                       << existing->getName() << "\n");
289c03c9040SRui Ueyama     return true;
290c03c9040SRui Ueyama   }
291c03c9040SRui Ueyama 
292c03c9040SRui Ueyama   // Now we have two defined symbols. If the new one is weak, we can ignore it.
293136d27abSRui Ueyama   if ((newFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
294e7245b42SNicola Zaghen     LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");
295c03c9040SRui Ueyama     return false;
296c03c9040SRui Ueyama   }
297c03c9040SRui Ueyama 
298c03c9040SRui Ueyama   // If the existing symbol is weak, we should replace it.
299136d27abSRui Ueyama   if (existing->isWeak()) {
300e7245b42SNicola Zaghen     LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
301c03c9040SRui Ueyama     return true;
302c03c9040SRui Ueyama   }
303c03c9040SRui Ueyama 
304c03c9040SRui Ueyama   // Neither symbol is week. They conflict.
305136d27abSRui Ueyama   error("duplicate symbol: " + toString(*existing) + "\n>>> defined in " +
306136d27abSRui Ueyama         toString(existing->getFile()) + "\n>>> defined in " +
307136d27abSRui Ueyama         toString(newFile));
308c03c9040SRui Ueyama   return true;
309b8621596SSam Clegg }
31093e559b1SSam Clegg 
311136d27abSRui Ueyama Symbol *SymbolTable::addDefinedFunction(StringRef name, uint32_t flags,
312136d27abSRui Ueyama                                         InputFile *file,
313136d27abSRui Ueyama                                         InputFunction *function) {
314136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addDefinedFunction: " << name << " ["
315136d27abSRui Ueyama                     << (function ? toString(function->signature) : "none")
3168b0b48f3SSam Clegg                     << "]\n");
317136d27abSRui Ueyama   Symbol *s;
318136d27abSRui Ueyama   bool wasInserted;
319136d27abSRui Ueyama   std::tie(s, wasInserted) = insert(name, file);
320c729c1b4SSam Clegg 
321136d27abSRui Ueyama   auto replaceSym = [&](Symbol *sym) {
3227c5fcb35SKazuaki Ishizaki     // If the new defined function doesn't have signature (i.e. bitcode
3236540e570SSam Clegg     // functions) but the old symbol does, then preserve the old signature
324136d27abSRui Ueyama     const WasmSignature *oldSig = s->getSignature();
325136d27abSRui Ueyama     auto* newSym = replaceSymbol<DefinedFunction>(sym, name, flags, file, function);
326136d27abSRui Ueyama     if (!newSym->signature)
327136d27abSRui Ueyama       newSym->signature = oldSig;
3286540e570SSam Clegg   };
3296540e570SSam Clegg 
330136d27abSRui Ueyama   if (wasInserted || s->isLazy()) {
331136d27abSRui Ueyama     replaceSym(s);
332136d27abSRui Ueyama     return s;
333e3498ec5SRui Ueyama   }
334e3498ec5SRui Ueyama 
335136d27abSRui Ueyama   auto existingFunction = dyn_cast<FunctionSymbol>(s);
336136d27abSRui Ueyama   if (!existingFunction) {
337136d27abSRui Ueyama     reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
338136d27abSRui Ueyama     return s;
3398b0b48f3SSam Clegg   }
3406540e570SSam Clegg 
341136d27abSRui Ueyama   bool checkSig = true;
342136d27abSRui Ueyama   if (auto ud = dyn_cast<UndefinedFunction>(existingFunction))
343136d27abSRui Ueyama     checkSig = ud->isCalledDirectly;
34459f959ffSSam Clegg 
345136d27abSRui Ueyama   if (checkSig && function && !signatureMatches(existingFunction, &function->signature)) {
346136d27abSRui Ueyama     Symbol* variant;
347136d27abSRui Ueyama     if (getFunctionVariant(s, &function->signature, file, &variant))
3486540e570SSam Clegg       // New variant, always replace
349136d27abSRui Ueyama       replaceSym(variant);
350136d27abSRui Ueyama     else if (shouldReplace(s, file, flags))
3516540e570SSam Clegg       // Variant already exists, replace it after checking shouldReplace
352136d27abSRui Ueyama       replaceSym(variant);
3536540e570SSam Clegg 
3546540e570SSam Clegg     // This variant we found take the place in the symbol table as the primary
3556540e570SSam Clegg     // variant.
356136d27abSRui Ueyama     replace(name, variant);
357136d27abSRui Ueyama     return variant;
3586540e570SSam Clegg   }
3596540e570SSam Clegg 
3606540e570SSam Clegg   // Existing function with matching signature.
361136d27abSRui Ueyama   if (shouldReplace(s, file, flags))
362136d27abSRui Ueyama     replaceSym(s);
3636540e570SSam Clegg 
364136d27abSRui Ueyama   return s;
36593e559b1SSam Clegg }
36693e559b1SSam Clegg 
367136d27abSRui Ueyama Symbol *SymbolTable::addDefinedData(StringRef name, uint32_t flags,
3685a9b25e1SSam Clegg                                     InputFile *file, InputChunk *segment,
3693b29376eSWouter van Oortmerssen                                     uint64_t address, uint64_t size) {
370136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addDefinedData:" << name << " addr:" << address
371e7245b42SNicola Zaghen                     << "\n");
372136d27abSRui Ueyama   Symbol *s;
373136d27abSRui Ueyama   bool wasInserted;
374136d27abSRui Ueyama   std::tie(s, wasInserted) = insert(name, file);
375c729c1b4SSam Clegg 
376136d27abSRui Ueyama   auto replaceSym = [&]() {
377136d27abSRui Ueyama     replaceSymbol<DefinedData>(s, name, flags, file, segment, address, size);
3786540e570SSam Clegg   };
3796540e570SSam Clegg 
380136d27abSRui Ueyama   if (wasInserted || s->isLazy()) {
381136d27abSRui Ueyama     replaceSym();
382136d27abSRui Ueyama     return s;
383e3498ec5SRui Ueyama   }
384e3498ec5SRui Ueyama 
385136d27abSRui Ueyama   checkDataType(s, file);
386e3498ec5SRui Ueyama 
387136d27abSRui Ueyama   if (shouldReplace(s, file, flags))
388136d27abSRui Ueyama     replaceSym();
389136d27abSRui Ueyama   return s;
390c94d393aSSam Clegg }
391c94d393aSSam Clegg 
392136d27abSRui Ueyama Symbol *SymbolTable::addDefinedGlobal(StringRef name, uint32_t flags,
393136d27abSRui Ueyama                                       InputFile *file, InputGlobal *global) {
394136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << name << "\n");
3954c2cbfe6SSam Clegg 
396136d27abSRui Ueyama   Symbol *s;
397136d27abSRui Ueyama   bool wasInserted;
398136d27abSRui Ueyama   std::tie(s, wasInserted) = insert(name, file);
399c729c1b4SSam Clegg 
400136d27abSRui Ueyama   auto replaceSym = [&]() {
401136d27abSRui Ueyama     replaceSymbol<DefinedGlobal>(s, name, flags, file, global);
4026540e570SSam Clegg   };
4036540e570SSam Clegg 
404136d27abSRui Ueyama   if (wasInserted || s->isLazy()) {
405136d27abSRui Ueyama     replaceSym();
406136d27abSRui Ueyama     return s;
40793102974SSam Clegg   }
40893102974SSam Clegg 
409136d27abSRui Ueyama   checkGlobalType(s, file, &global->getType());
410e3498ec5SRui Ueyama 
411136d27abSRui Ueyama   if (shouldReplace(s, file, flags))
412136d27abSRui Ueyama     replaceSym();
413136d27abSRui Ueyama   return s;
414e3498ec5SRui Ueyama }
415e3498ec5SRui Ueyama 
4161d891d44SHeejin Ahn Symbol *SymbolTable::addDefinedTag(StringRef name, uint32_t flags,
4171d891d44SHeejin Ahn                                    InputFile *file, InputTag *tag) {
4181d891d44SHeejin Ahn   LLVM_DEBUG(dbgs() << "addDefinedTag:" << name << "\n");
419e915a71fSHeejin Ahn 
420136d27abSRui Ueyama   Symbol *s;
421136d27abSRui Ueyama   bool wasInserted;
422136d27abSRui Ueyama   std::tie(s, wasInserted) = insert(name, file);
423e915a71fSHeejin Ahn 
424136d27abSRui Ueyama   auto replaceSym = [&]() {
4251d891d44SHeejin Ahn     replaceSymbol<DefinedTag>(s, name, flags, file, tag);
4266540e570SSam Clegg   };
4276540e570SSam Clegg 
428136d27abSRui Ueyama   if (wasInserted || s->isLazy()) {
429136d27abSRui Ueyama     replaceSym();
430136d27abSRui Ueyama     return s;
431e915a71fSHeejin Ahn   }
432e915a71fSHeejin Ahn 
4331d891d44SHeejin Ahn   checkTagType(s, file, &tag->getType(), &tag->signature);
434e915a71fSHeejin Ahn 
435136d27abSRui Ueyama   if (shouldReplace(s, file, flags))
436136d27abSRui Ueyama     replaceSym();
437136d27abSRui Ueyama   return s;
438e915a71fSHeejin Ahn }
439e915a71fSHeejin Ahn 
44053e3b81fSAndy Wingo Symbol *SymbolTable::addDefinedTable(StringRef name, uint32_t flags,
44153e3b81fSAndy Wingo                                      InputFile *file, InputTable *table) {
44253e3b81fSAndy Wingo   LLVM_DEBUG(dbgs() << "addDefinedTable:" << name << "\n");
44353e3b81fSAndy Wingo 
44453e3b81fSAndy Wingo   Symbol *s;
44553e3b81fSAndy Wingo   bool wasInserted;
44653e3b81fSAndy Wingo   std::tie(s, wasInserted) = insert(name, file);
44753e3b81fSAndy Wingo 
44853e3b81fSAndy Wingo   auto replaceSym = [&]() {
44953e3b81fSAndy Wingo     replaceSymbol<DefinedTable>(s, name, flags, file, table);
45053e3b81fSAndy Wingo   };
45153e3b81fSAndy Wingo 
45253e3b81fSAndy Wingo   if (wasInserted || s->isLazy()) {
45353e3b81fSAndy Wingo     replaceSym();
45453e3b81fSAndy Wingo     return s;
45553e3b81fSAndy Wingo   }
45653e3b81fSAndy Wingo 
45753e3b81fSAndy Wingo   checkTableType(s, file, &table->getType());
45853e3b81fSAndy Wingo 
45953e3b81fSAndy Wingo   if (shouldReplace(s, file, flags))
46053e3b81fSAndy Wingo     replaceSym();
46153e3b81fSAndy Wingo   return s;
46253e3b81fSAndy Wingo }
46353e3b81fSAndy Wingo 
46467b05584SSam Clegg // This function get called when an undefined symbol is added, and there is
46567b05584SSam Clegg // already an existing one in the symbols table.  In this case we check that
46667b05584SSam Clegg // custom 'import-module' and 'import-field' symbol attributes agree.
4677ae3d335SKazuaki Ishizaki // With LTO these attributes are not available when the bitcode is read and only
46867b05584SSam Clegg // become available when the LTO object is read.  In this case we silently
46967b05584SSam Clegg // replace the empty attributes with the valid ones.
47067b05584SSam Clegg template <typename T>
471bd481277SSam Clegg static void setImportAttributes(T *existing, Optional<StringRef> importName,
472bd481277SSam Clegg                                 Optional<StringRef> importModule,
473a57f1a54SSam Clegg                                 uint32_t flags, InputFile *file) {
474bd481277SSam Clegg   if (importName) {
475bd481277SSam Clegg     if (!existing->importName)
47667b05584SSam Clegg       existing->importName = importName;
47767b05584SSam Clegg     if (existing->importName != importName)
47867b05584SSam Clegg       error("import name mismatch for symbol: " + toString(*existing) +
479bd481277SSam Clegg             "\n>>> defined as " + *existing->importName + " in " +
480bd481277SSam Clegg             toString(existing->getFile()) + "\n>>> defined as " + *importName +
48167b05584SSam Clegg             " in " + toString(file));
48267b05584SSam Clegg   }
48367b05584SSam Clegg 
484bd481277SSam Clegg   if (importModule) {
485bd481277SSam Clegg     if (!existing->importModule)
48667b05584SSam Clegg       existing->importModule = importModule;
48767b05584SSam Clegg     if (existing->importModule != importModule)
48867b05584SSam Clegg       error("import module mismatch for symbol: " + toString(*existing) +
489bd481277SSam Clegg             "\n>>> defined as " + *existing->importModule + " in " +
490bd481277SSam Clegg             toString(existing->getFile()) + "\n>>> defined as " +
491bd481277SSam Clegg             *importModule + " in " + toString(file));
49267b05584SSam Clegg   }
493a57f1a54SSam Clegg 
494a57f1a54SSam Clegg   // Update symbol binding, if the existing symbol is weak
495a57f1a54SSam Clegg   uint32_t binding = flags & WASM_SYMBOL_BINDING_MASK;
496a57f1a54SSam Clegg   if (existing->isWeak() && binding != WASM_SYMBOL_BINDING_WEAK) {
497a57f1a54SSam Clegg     existing->flags = (existing->flags & ~WASM_SYMBOL_BINDING_MASK) | binding;
498a57f1a54SSam Clegg   }
49967b05584SSam Clegg }
50067b05584SSam Clegg 
501bd481277SSam Clegg Symbol *SymbolTable::addUndefinedFunction(StringRef name,
502bd481277SSam Clegg                                           Optional<StringRef> importName,
503bd481277SSam Clegg                                           Optional<StringRef> importModule,
504136d27abSRui Ueyama                                           uint32_t flags, InputFile *file,
505136d27abSRui Ueyama                                           const WasmSignature *sig,
506136d27abSRui Ueyama                                           bool isCalledDirectly) {
507136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " ["
508136d27abSRui Ueyama                     << (sig ? toString(*sig) : "none")
509a57f1a54SSam Clegg                     << "] IsCalledDirectly:" << isCalledDirectly << " flags=0x"
510a57f1a54SSam Clegg                     << utohexstr(flags) << "\n");
511f6f4b98fSSam Clegg   assert(flags & WASM_SYMBOL_UNDEFINED);
512c03c9040SRui Ueyama 
513136d27abSRui Ueyama   Symbol *s;
514136d27abSRui Ueyama   bool wasInserted;
515136d27abSRui Ueyama   std::tie(s, wasInserted) = insert(name, file);
516136d27abSRui Ueyama   if (s->traced)
517136d27abSRui Ueyama     printTraceSymbolUndefined(name, file);
518c729c1b4SSam Clegg 
519136d27abSRui Ueyama   auto replaceSym = [&]() {
520136d27abSRui Ueyama     replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags,
521136d27abSRui Ueyama                                      file, sig, isCalledDirectly);
5226540e570SSam Clegg   };
5236540e570SSam Clegg 
524b34ec596SSam Clegg   if (wasInserted) {
525136d27abSRui Ueyama     replaceSym();
526b34ec596SSam Clegg   } else if (auto *lazy = dyn_cast<LazySymbol>(s)) {
527b34ec596SSam Clegg     if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
528b34ec596SSam Clegg       lazy->setWeak();
529b34ec596SSam Clegg       lazy->signature = sig;
530b34ec596SSam Clegg     } else {
531136d27abSRui Ueyama       lazy->fetch();
532b34ec596SSam Clegg     }
533b34ec596SSam Clegg   } else {
534136d27abSRui Ueyama     auto existingFunction = dyn_cast<FunctionSymbol>(s);
535136d27abSRui Ueyama     if (!existingFunction) {
536136d27abSRui Ueyama       reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
537136d27abSRui Ueyama       return s;
5386540e570SSam Clegg     }
539136d27abSRui Ueyama     if (!existingFunction->signature && sig)
540136d27abSRui Ueyama       existingFunction->signature = sig;
541a57f1a54SSam Clegg     auto *existingUndefined = dyn_cast<UndefinedFunction>(existingFunction);
542fecfc592SSam Clegg     if (isCalledDirectly && !signatureMatches(existingFunction, sig)) {
543a57f1a54SSam Clegg       // If the existing undefined functions is not called directly then let
544fecfc592SSam Clegg       // this one take precedence.  Otherwise the existing function is either
545a57f1a54SSam Clegg       // directly called or defined, in which case we need a function variant.
546fecfc592SSam Clegg       if (existingUndefined && !existingUndefined->isCalledDirectly)
547136d27abSRui Ueyama         replaceSym();
548fecfc592SSam Clegg       else if (getFunctionVariant(s, sig, file, &s))
549fecfc592SSam Clegg         replaceSym();
550fecfc592SSam Clegg     }
551e4888be7SSam Clegg     if (existingUndefined) {
552a57f1a54SSam Clegg       setImportAttributes(existingUndefined, importName, importModule, flags,
553a57f1a54SSam Clegg                           file);
554e4888be7SSam Clegg       if (isCalledDirectly)
555e4888be7SSam Clegg         existingUndefined->isCalledDirectly = true;
556e4888be7SSam Clegg     }
5576540e570SSam Clegg   }
558cefbf9acSSam Clegg 
559136d27abSRui Ueyama   return s;
560c03c9040SRui Ueyama }
561c03c9040SRui Ueyama 
562136d27abSRui Ueyama Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags,
563136d27abSRui Ueyama                                       InputFile *file) {
564136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n");
565f6f4b98fSSam Clegg   assert(flags & WASM_SYMBOL_UNDEFINED);
56693102974SSam Clegg 
567136d27abSRui Ueyama   Symbol *s;
568136d27abSRui Ueyama   bool wasInserted;
569136d27abSRui Ueyama   std::tie(s, wasInserted) = insert(name, file);
570136d27abSRui Ueyama   if (s->traced)
571136d27abSRui Ueyama     printTraceSymbolUndefined(name, file);
572f989a92dSSam Clegg 
573b34ec596SSam Clegg   if (wasInserted) {
574136d27abSRui Ueyama     replaceSymbol<UndefinedData>(s, name, flags, file);
575b34ec596SSam Clegg   } else if (auto *lazy = dyn_cast<LazySymbol>(s)) {
576b34ec596SSam Clegg     if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK)
577b34ec596SSam Clegg       lazy->setWeak();
578b34ec596SSam Clegg     else
579136d27abSRui Ueyama       lazy->fetch();
580b34ec596SSam Clegg   } else if (s->isDefined()) {
581136d27abSRui Ueyama     checkDataType(s, file);
582b34ec596SSam Clegg   }
583136d27abSRui Ueyama   return s;
584c94d393aSSam Clegg }
585c94d393aSSam Clegg 
586bd481277SSam Clegg Symbol *SymbolTable::addUndefinedGlobal(StringRef name,
587bd481277SSam Clegg                                         Optional<StringRef> importName,
588bd481277SSam Clegg                                         Optional<StringRef> importModule,
589bd481277SSam Clegg                                         uint32_t flags, InputFile *file,
590136d27abSRui Ueyama                                         const WasmGlobalType *type) {
591136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n");
592f6f4b98fSSam Clegg   assert(flags & WASM_SYMBOL_UNDEFINED);
593e3498ec5SRui Ueyama 
594136d27abSRui Ueyama   Symbol *s;
595136d27abSRui Ueyama   bool wasInserted;
596136d27abSRui Ueyama   std::tie(s, wasInserted) = insert(name, file);
597136d27abSRui Ueyama   if (s->traced)
598136d27abSRui Ueyama     printTraceSymbolUndefined(name, file);
599c729c1b4SSam Clegg 
600136d27abSRui Ueyama   if (wasInserted)
601136d27abSRui Ueyama     replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags,
602136d27abSRui Ueyama                                    file, type);
603136d27abSRui Ueyama   else if (auto *lazy = dyn_cast<LazySymbol>(s))
604136d27abSRui Ueyama     lazy->fetch();
605136d27abSRui Ueyama   else if (s->isDefined())
606136d27abSRui Ueyama     checkGlobalType(s, file, type);
607136d27abSRui Ueyama   return s;
608e3498ec5SRui Ueyama }
609e3498ec5SRui Ueyama 
61053e3b81fSAndy Wingo Symbol *SymbolTable::addUndefinedTable(StringRef name,
61153e3b81fSAndy Wingo                                        Optional<StringRef> importName,
61253e3b81fSAndy Wingo                                        Optional<StringRef> importModule,
61353e3b81fSAndy Wingo                                        uint32_t flags, InputFile *file,
61453e3b81fSAndy Wingo                                        const WasmTableType *type) {
61553e3b81fSAndy Wingo   LLVM_DEBUG(dbgs() << "addUndefinedTable: " << name << "\n");
61653e3b81fSAndy Wingo   assert(flags & WASM_SYMBOL_UNDEFINED);
61753e3b81fSAndy Wingo 
61853e3b81fSAndy Wingo   Symbol *s;
61953e3b81fSAndy Wingo   bool wasInserted;
62053e3b81fSAndy Wingo   std::tie(s, wasInserted) = insert(name, file);
62153e3b81fSAndy Wingo   if (s->traced)
62253e3b81fSAndy Wingo     printTraceSymbolUndefined(name, file);
62353e3b81fSAndy Wingo 
62453e3b81fSAndy Wingo   if (wasInserted)
62553e3b81fSAndy Wingo     replaceSymbol<UndefinedTable>(s, name, importName, importModule, flags,
62653e3b81fSAndy Wingo                                   file, type);
62753e3b81fSAndy Wingo   else if (auto *lazy = dyn_cast<LazySymbol>(s))
62853e3b81fSAndy Wingo     lazy->fetch();
62953e3b81fSAndy Wingo   else if (s->isDefined())
63053e3b81fSAndy Wingo     checkTableType(s, file, type);
63153e3b81fSAndy Wingo   return s;
63253e3b81fSAndy Wingo }
63353e3b81fSAndy Wingo 
634e638d8b2SAndy Wingo TableSymbol *SymbolTable::createUndefinedIndirectFunctionTable(StringRef name) {
635e638d8b2SAndy Wingo   WasmLimits limits{0, 0, 0}; // Set by the writer.
636e638d8b2SAndy Wingo   WasmTableType *type = make<WasmTableType>();
637e638d8b2SAndy Wingo   type->ElemType = uint8_t(ValType::FUNCREF);
638e638d8b2SAndy Wingo   type->Limits = limits;
639e638d8b2SAndy Wingo   StringRef module(defaultModule);
640e638d8b2SAndy Wingo   uint32_t flags = config->exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN;
641e638d8b2SAndy Wingo   flags |= WASM_SYMBOL_UNDEFINED;
642e638d8b2SAndy Wingo   Symbol *sym = addUndefinedTable(name, name, module, flags, nullptr, type);
643e638d8b2SAndy Wingo   sym->markLive();
644e638d8b2SAndy Wingo   sym->forceExport = config->exportTable;
645e638d8b2SAndy Wingo   return cast<TableSymbol>(sym);
646e638d8b2SAndy Wingo }
647e638d8b2SAndy Wingo 
648e638d8b2SAndy Wingo TableSymbol *SymbolTable::createDefinedIndirectFunctionTable(StringRef name) {
649e638d8b2SAndy Wingo   const uint32_t invalidIndex = -1;
650e638d8b2SAndy Wingo   WasmLimits limits{0, 0, 0}; // Set by the writer.
651e638d8b2SAndy Wingo   WasmTableType type{uint8_t(ValType::FUNCREF), limits};
652e638d8b2SAndy Wingo   WasmTable desc{invalidIndex, type, name};
653e638d8b2SAndy Wingo   InputTable *table = make<InputTable>(desc, nullptr);
654e638d8b2SAndy Wingo   uint32_t flags = config->exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN;
655e638d8b2SAndy Wingo   TableSymbol *sym = addSyntheticTable(name, flags, table);
656e638d8b2SAndy Wingo   sym->markLive();
657e638d8b2SAndy Wingo   sym->forceExport = config->exportTable;
658e638d8b2SAndy Wingo   return sym;
659e638d8b2SAndy Wingo }
660e638d8b2SAndy Wingo 
661e638d8b2SAndy Wingo // Whether or not we need an indirect function table is usually a function of
662e638d8b2SAndy Wingo // whether an input declares a need for it.  However sometimes it's possible for
663e638d8b2SAndy Wingo // no input to need the indirect function table, but then a late
664e638d8b2SAndy Wingo // addInternalGOTEntry causes a function to be allocated an address.  In that
665e638d8b2SAndy Wingo // case address we synthesize a definition at the last minute.
666e638d8b2SAndy Wingo TableSymbol *SymbolTable::resolveIndirectFunctionTable(bool required) {
667e638d8b2SAndy Wingo   Symbol *existing = find(functionTableName);
668e638d8b2SAndy Wingo   if (existing) {
669e638d8b2SAndy Wingo     if (!isa<TableSymbol>(existing)) {
670e638d8b2SAndy Wingo       error(Twine("reserved symbol must be of type table: `") +
671e638d8b2SAndy Wingo             functionTableName + "`");
672e638d8b2SAndy Wingo       return nullptr;
673e638d8b2SAndy Wingo     }
674e638d8b2SAndy Wingo     if (existing->isDefined()) {
675e638d8b2SAndy Wingo       error(Twine("reserved symbol must not be defined in input files: `") +
676e638d8b2SAndy Wingo             functionTableName + "`");
677e638d8b2SAndy Wingo       return nullptr;
678e638d8b2SAndy Wingo     }
679e638d8b2SAndy Wingo   }
680e638d8b2SAndy Wingo 
681e638d8b2SAndy Wingo   if (config->importTable) {
682e638d8b2SAndy Wingo     if (existing)
683e638d8b2SAndy Wingo       return cast<TableSymbol>(existing);
684e638d8b2SAndy Wingo     if (required)
685e638d8b2SAndy Wingo       return createUndefinedIndirectFunctionTable(functionTableName);
686e638d8b2SAndy Wingo   } else if ((existing && existing->isLive()) || config->exportTable ||
687e638d8b2SAndy Wingo              required) {
688e638d8b2SAndy Wingo     // A defined table is required.  Either because the user request an exported
689e638d8b2SAndy Wingo     // table or because the table symbol is already live.  The existing table is
690e638d8b2SAndy Wingo     // guaranteed to be undefined due to the check above.
691e638d8b2SAndy Wingo     return createDefinedIndirectFunctionTable(functionTableName);
692e638d8b2SAndy Wingo   }
693e638d8b2SAndy Wingo 
694e638d8b2SAndy Wingo   // An indirect function table will only be present in the symbol table if
695e638d8b2SAndy Wingo   // needed by a reloc; if we get here, we don't need one.
696e638d8b2SAndy Wingo   return nullptr;
697e638d8b2SAndy Wingo }
698e638d8b2SAndy Wingo 
699136d27abSRui Ueyama void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) {
700136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "addLazy: " << sym->getName() << "\n");
701136d27abSRui Ueyama   StringRef name = sym->getName();
702c03c9040SRui Ueyama 
703136d27abSRui Ueyama   Symbol *s;
704136d27abSRui Ueyama   bool wasInserted;
705136d27abSRui Ueyama   std::tie(s, wasInserted) = insertName(name);
706c03c9040SRui Ueyama 
707136d27abSRui Ueyama   if (wasInserted) {
708136d27abSRui Ueyama     replaceSymbol<LazySymbol>(s, name, 0, file, *sym);
709c03c9040SRui Ueyama     return;
710c03c9040SRui Ueyama   }
711c03c9040SRui Ueyama 
712136d27abSRui Ueyama   if (!s->isUndefined())
71337b4ee52SSam Clegg     return;
71437b4ee52SSam Clegg 
71537b4ee52SSam Clegg   // The existing symbol is undefined, load a new one from the archive,
7167ae3d335SKazuaki Ishizaki   // unless the existing symbol is weak in which case replace the undefined
71737b4ee52SSam Clegg   // symbols with a LazySymbol.
718136d27abSRui Ueyama   if (s->isWeak()) {
719136d27abSRui Ueyama     const WasmSignature *oldSig = nullptr;
72037b4ee52SSam Clegg     // In the case of an UndefinedFunction we need to preserve the expected
72137b4ee52SSam Clegg     // signature.
722136d27abSRui Ueyama     if (auto *f = dyn_cast<UndefinedFunction>(s))
723136d27abSRui Ueyama       oldSig = f->signature;
72437b4ee52SSam Clegg     LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n");
725136d27abSRui Ueyama     auto newSym = replaceSymbol<LazySymbol>(s, name, WASM_SYMBOL_BINDING_WEAK,
726136d27abSRui Ueyama                                             file, *sym);
727136d27abSRui Ueyama     newSym->signature = oldSig;
72837b4ee52SSam Clegg     return;
72937b4ee52SSam Clegg   }
73037b4ee52SSam Clegg 
731e7245b42SNicola Zaghen   LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
732136d27abSRui Ueyama   file->addMember(sym);
733c94d393aSSam Clegg }
734e0f6fcd0SSam Clegg 
735136d27abSRui Ueyama bool SymbolTable::addComdat(StringRef name) {
736136d27abSRui Ueyama   return comdatGroups.insert(CachedHashStringRef(name)).second;
737e0f6fcd0SSam Clegg }
7381f3f774fSSam Clegg 
7396540e570SSam Clegg // The new signature doesn't match.  Create a variant to the symbol with the
7406540e570SSam Clegg // signature encoded in the name and return that instead.  These symbols are
7416540e570SSam Clegg // then unified later in handleSymbolVariants.
742136d27abSRui Ueyama bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig,
743136d27abSRui Ueyama                                      const InputFile *file, Symbol **out) {
744136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "getFunctionVariant: " << sym->getName() << " -> "
745136d27abSRui Ueyama                     << " " << toString(*sig) << "\n");
746136d27abSRui Ueyama   Symbol *variant = nullptr;
7476540e570SSam Clegg 
7486540e570SSam Clegg   // Linear search through symbol variants.  Should never be more than two
7496540e570SSam Clegg   // or three entries here.
750136d27abSRui Ueyama   auto &variants = symVariants[CachedHashStringRef(sym->getName())];
751136d27abSRui Ueyama   if (variants.empty())
752136d27abSRui Ueyama     variants.push_back(sym);
7536540e570SSam Clegg 
754136d27abSRui Ueyama   for (Symbol* v : variants) {
755136d27abSRui Ueyama     if (*v->getSignature() == *sig) {
756136d27abSRui Ueyama       variant = v;
7576540e570SSam Clegg       break;
7586540e570SSam Clegg     }
7596540e570SSam Clegg   }
7606540e570SSam Clegg 
761136d27abSRui Ueyama   bool wasAdded = !variant;
762136d27abSRui Ueyama   if (wasAdded) {
7636540e570SSam Clegg     // Create a new variant;
7646540e570SSam Clegg     LLVM_DEBUG(dbgs() << "added new variant\n");
765136d27abSRui Ueyama     variant = reinterpret_cast<Symbol *>(make<SymbolUnion>());
766064e9907SSam Clegg     variant->isUsedInRegularObj =
767064e9907SSam Clegg         !file || file->kind() == InputFile::ObjectKind;
768064e9907SSam Clegg     variant->canInline = true;
769064e9907SSam Clegg     variant->traced = false;
770064e9907SSam Clegg     variant->forceExport = false;
771136d27abSRui Ueyama     variants.push_back(variant);
7726540e570SSam Clegg   } else {
773136d27abSRui Ueyama     LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant) << "\n");
774136d27abSRui Ueyama     assert(*variant->getSignature() == *sig);
7756540e570SSam Clegg   }
7766540e570SSam Clegg 
777136d27abSRui Ueyama   *out = variant;
778136d27abSRui Ueyama   return wasAdded;
7796540e570SSam Clegg }
7806540e570SSam Clegg 
7811f3f774fSSam Clegg // Set a flag for --trace-symbol so that we can print out a log message
7821f3f774fSSam Clegg // if a new symbol with the same name is inserted into the symbol table.
783136d27abSRui Ueyama void SymbolTable::trace(StringRef name) {
784136d27abSRui Ueyama   symMap.insert({CachedHashStringRef(name), -1});
7851f3f774fSSam Clegg }
786230dc11dSSam Clegg 
787136d27abSRui Ueyama void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
788a5ca34e6SSam Clegg   // Swap symbols as instructed by -wrap.
789136d27abSRui Ueyama   int &origIdx = symMap[CachedHashStringRef(sym->getName())];
790136d27abSRui Ueyama   int &realIdx= symMap[CachedHashStringRef(real->getName())];
791136d27abSRui Ueyama   int &wrapIdx = symMap[CachedHashStringRef(wrap->getName())];
792136d27abSRui Ueyama   LLVM_DEBUG(dbgs() << "wrap: " << sym->getName() << "\n");
793a5ca34e6SSam Clegg 
794a5ca34e6SSam Clegg   // Anyone looking up __real symbols should get the original
795136d27abSRui Ueyama   realIdx = origIdx;
796a5ca34e6SSam Clegg   // Anyone looking up the original should get the __wrap symbol
797136d27abSRui Ueyama   origIdx = wrapIdx;
798a5ca34e6SSam Clegg }
799a5ca34e6SSam Clegg 
800136d27abSRui Ueyama static const uint8_t unreachableFn[] = {
801230dc11dSSam Clegg     0x03 /* ULEB length */, 0x00 /* ULEB num locals */,
802230dc11dSSam Clegg     0x00 /* opcode unreachable */, 0x0b /* opcode end */
803230dc11dSSam Clegg };
804230dc11dSSam Clegg 
805230dc11dSSam Clegg // Replace the given symbol body with an unreachable function.
806230dc11dSSam Clegg // This is used by handleWeakUndefines in order to generate a callable
8076540e570SSam Clegg // equivalent of an undefined function and also handleSymbolVariants for
8086540e570SSam Clegg // undefined functions that don't match the signature of the definition.
809136d27abSRui Ueyama InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym,
810136d27abSRui Ueyama                                                    const WasmSignature &sig,
811136d27abSRui Ueyama                                                    StringRef debugName) {
812136d27abSRui Ueyama   auto *func = make<SyntheticFunction>(sig, sym->getName(), debugName);
813136d27abSRui Ueyama   func->setBody(unreachableFn);
814136d27abSRui Ueyama   syntheticFunctions.emplace_back(func);
8154b8e2d8eSSam Clegg   // Mark new symbols as local. For relocatable output we don't want them
8164b8e2d8eSSam Clegg   // to be exported outside the object file.
8174b8e2d8eSSam Clegg   replaceSymbol<DefinedFunction>(sym, debugName, WASM_SYMBOL_BINDING_LOCAL,
8184b8e2d8eSSam Clegg                                  nullptr, func);
81948ddf5e1SSam Clegg   // Ensure the stub function doesn't get a table entry.  Its address
82048ddf5e1SSam Clegg   // should always compare equal to the null pointer.
82148ddf5e1SSam Clegg   sym->isStub = true;
822136d27abSRui Ueyama   return func;
823230dc11dSSam Clegg }
824230dc11dSSam Clegg 
825206884bfSSam Clegg void SymbolTable::replaceWithUndefined(Symbol *sym) {
826206884bfSSam Clegg   // Add a synthetic dummy for weak undefined functions.  These dummies will
827206884bfSSam Clegg   // be GC'd if not used as the target of any "call" instructions.
828206884bfSSam Clegg   StringRef debugName = saver.save("undefined_weak:" + toString(*sym));
829206884bfSSam Clegg   replaceWithUnreachable(sym, *sym->getSignature(), debugName);
830206884bfSSam Clegg   // Hide our dummy to prevent export.
831206884bfSSam Clegg   sym->setHidden(true);
832206884bfSSam Clegg }
833206884bfSSam Clegg 
834230dc11dSSam Clegg // For weak undefined functions, there may be "call" instructions that reference
835230dc11dSSam Clegg // the symbol. In this case, we need to synthesise a dummy/stub function that
836230dc11dSSam Clegg // will abort at runtime, so that relocations can still provided an operand to
837230dc11dSSam Clegg // the call instruction that passes Wasm validation.
838230dc11dSSam Clegg void SymbolTable::handleWeakUndefines() {
839136d27abSRui Ueyama   for (Symbol *sym : getSymbols()) {
840206884bfSSam Clegg     if (sym->isUndefWeak()) {
841206884bfSSam Clegg       if (sym->getSignature()) {
842206884bfSSam Clegg         replaceWithUndefined(sym);
843206884bfSSam Clegg       } else {
844206884bfSSam Clegg         // It is possible for undefined functions not to have a signature (eg.
845206884bfSSam Clegg         // if added via "--undefined"), but weak undefined ones do have a
846206884bfSSam Clegg         // signature.  Lazy symbols may not be functions and therefore Sig can
847206884bfSSam Clegg         // still be null in some circumstance.
848136d27abSRui Ueyama         assert(!isa<FunctionSymbol>(sym));
849206884bfSSam Clegg       }
850206884bfSSam Clegg     }
851206884bfSSam Clegg   }
8526540e570SSam Clegg }
853230dc11dSSam Clegg 
854206884bfSSam Clegg DefinedFunction *SymbolTable::createUndefinedStub(const WasmSignature &sig) {
855206884bfSSam Clegg   if (stubFunctions.count(sig))
856206884bfSSam Clegg     return stubFunctions[sig];
857206884bfSSam Clegg   LLVM_DEBUG(dbgs() << "createUndefinedStub: " << toString(sig) << "\n");
858206884bfSSam Clegg   auto *sym = reinterpret_cast<DefinedFunction *>(make<SymbolUnion>());
859206884bfSSam Clegg   sym->isUsedInRegularObj = true;
860206884bfSSam Clegg   sym->canInline = true;
861206884bfSSam Clegg   sym->traced = false;
862206884bfSSam Clegg   sym->forceExport = false;
863206884bfSSam Clegg   sym->signature = &sig;
864206884bfSSam Clegg   replaceSymbol<DefinedFunction>(
865206884bfSSam Clegg       sym, "undefined_stub", WASM_SYMBOL_VISIBILITY_HIDDEN, nullptr, nullptr);
866206884bfSSam Clegg   replaceWithUnreachable(sym, sig, "undefined_stub");
867206884bfSSam Clegg   stubFunctions[sig] = sym;
868206884bfSSam Clegg   return sym;
869230dc11dSSam Clegg }
8706540e570SSam Clegg 
871136d27abSRui Ueyama static void reportFunctionSignatureMismatch(StringRef symName,
872136d27abSRui Ueyama                                             FunctionSymbol *a,
873136d27abSRui Ueyama                                             FunctionSymbol *b, bool isError) {
874136d27abSRui Ueyama   std::string msg = ("function signature mismatch: " + symName +
875136d27abSRui Ueyama                      "\n>>> defined as " + toString(*a->signature) + " in " +
876136d27abSRui Ueyama                      toString(a->getFile()) + "\n>>> defined as " +
877136d27abSRui Ueyama                      toString(*b->signature) + " in " + toString(b->getFile()))
8786540e570SSam Clegg                         .str();
879136d27abSRui Ueyama   if (isError)
8806540e570SSam Clegg     error(msg);
8816540e570SSam Clegg   else
8826540e570SSam Clegg     warn(msg);
8836540e570SSam Clegg }
8846540e570SSam Clegg 
8856540e570SSam Clegg // Remove any variant symbols that were created due to function signature
8866540e570SSam Clegg // mismatches.
8876540e570SSam Clegg void SymbolTable::handleSymbolVariants() {
888136d27abSRui Ueyama   for (auto pair : symVariants) {
8896540e570SSam Clegg     // Push the initial symbol onto the list of variants.
890136d27abSRui Ueyama     StringRef symName = pair.first.val();
891136d27abSRui Ueyama     std::vector<Symbol *> &variants = pair.second;
8926540e570SSam Clegg 
8936540e570SSam Clegg #ifndef NDEBUG
894136d27abSRui Ueyama     LLVM_DEBUG(dbgs() << "symbol with (" << variants.size()
895136d27abSRui Ueyama                       << ") variants: " << symName << "\n");
896136d27abSRui Ueyama     for (auto *s: variants) {
897136d27abSRui Ueyama       auto *f = cast<FunctionSymbol>(s);
898136d27abSRui Ueyama       LLVM_DEBUG(dbgs() << " variant: " + f->getName() << " "
899136d27abSRui Ueyama                         << toString(*f->signature) << "\n");
9006540e570SSam Clegg     }
9016540e570SSam Clegg #endif
9026540e570SSam Clegg 
9036540e570SSam Clegg     // Find the one definition.
904136d27abSRui Ueyama     DefinedFunction *defined = nullptr;
905136d27abSRui Ueyama     for (auto *symbol : variants) {
906136d27abSRui Ueyama       if (auto f = dyn_cast<DefinedFunction>(symbol)) {
907136d27abSRui Ueyama         defined = f;
9086540e570SSam Clegg         break;
9096540e570SSam Clegg       }
9106540e570SSam Clegg     }
9116540e570SSam Clegg 
9126540e570SSam Clegg     // If there are no definitions, and the undefined symbols disagree on
9136540e570SSam Clegg     // the signature, there is not we can do since we don't know which one
9146540e570SSam Clegg     // to use as the signature on the import.
915136d27abSRui Ueyama     if (!defined) {
916136d27abSRui Ueyama       reportFunctionSignatureMismatch(symName,
917136d27abSRui Ueyama                                       cast<FunctionSymbol>(variants[0]),
918136d27abSRui Ueyama                                       cast<FunctionSymbol>(variants[1]), true);
9196540e570SSam Clegg       return;
9206540e570SSam Clegg     }
9216540e570SSam Clegg 
922136d27abSRui Ueyama     for (auto *symbol : variants) {
923136d27abSRui Ueyama       if (symbol != defined) {
924136d27abSRui Ueyama         auto *f = cast<FunctionSymbol>(symbol);
925136d27abSRui Ueyama         reportFunctionSignatureMismatch(symName, f, defined, false);
92651b521c0SSam Clegg         StringRef debugName = saver.save("signature_mismatch:" + toString(*f));
927136d27abSRui Ueyama         replaceWithUnreachable(f, *f->signature, debugName);
9286540e570SSam Clegg       }
9296540e570SSam Clegg     }
9306540e570SSam Clegg   }
9316540e570SSam Clegg }
93233c59abfSFangrui Song 
93333c59abfSFangrui Song } // namespace wasm
93433c59abfSFangrui Song } // namespace lld
935