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