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 "Driver.h"
12 #include "LTO.h"
13 #include "PDB.h"
14 #include "Symbols.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Memory.h"
17 #include "lld/Common/Timer.h"
18 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/Object/WindowsMachineFlag.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <utility>
24 
25 using namespace llvm;
26 
27 namespace lld {
28 namespace coff {
29 
30 static Timer ltoTimer("LTO", Timer::root());
31 
32 SymbolTable *symtab;
33 
34 void SymbolTable::addFile(InputFile *file) {
35   log("Reading " + toString(file));
36   file->parse();
37 
38   MachineTypes mt = file->getMachineType();
39   if (config->machine == IMAGE_FILE_MACHINE_UNKNOWN) {
40     config->machine = mt;
41   } else if (mt != IMAGE_FILE_MACHINE_UNKNOWN && config->machine != mt) {
42     error(toString(file) + ": machine type " + machineToStr(mt) +
43           " conflicts with " + machineToStr(config->machine));
44     return;
45   }
46 
47   if (auto *f = dyn_cast<ObjFile>(file)) {
48     ObjFile::instances.push_back(f);
49   } else if (auto *f = dyn_cast<BitcodeFile>(file)) {
50     BitcodeFile::instances.push_back(f);
51   } else if (auto *f = dyn_cast<ImportFile>(file)) {
52     ImportFile::instances.push_back(f);
53   }
54 
55   driver->parseDirectives(file);
56 }
57 
58 static void errorOrWarn(const Twine &s) {
59   if (config->forceUnresolved)
60     warn(s);
61   else
62     error(s);
63 }
64 
65 // Causes the file associated with a lazy symbol to be linked in.
66 static void forceLazy(Symbol *s) {
67   s->pendingArchiveLoad = true;
68   switch (s->kind()) {
69   case Symbol::Kind::LazyArchiveKind: {
70     auto *l = cast<LazyArchive>(s);
71     l->file->addMember(l->sym);
72     break;
73   }
74   case Symbol::Kind::LazyObjectKind:
75     cast<LazyObject>(s)->file->fetch();
76     break;
77   default:
78     llvm_unreachable(
79         "symbol passed to forceLazy is not a LazyArchive or LazyObject");
80   }
81 }
82 
83 // Returns the symbol in SC whose value is <= Addr that is closest to Addr.
84 // This is generally the global variable or function whose definition contains
85 // Addr.
86 static Symbol *getSymbol(SectionChunk *sc, uint32_t addr) {
87   DefinedRegular *candidate = nullptr;
88 
89   for (Symbol *s : sc->file->getSymbols()) {
90     auto *d = dyn_cast_or_null<DefinedRegular>(s);
91     if (!d || !d->data || d->file != sc->file || d->getChunk() != sc ||
92         d->getValue() > addr ||
93         (candidate && d->getValue() < candidate->getValue()))
94       continue;
95 
96     candidate = d;
97   }
98 
99   return candidate;
100 }
101 
102 static std::vector<std::string> getSymbolLocations(BitcodeFile *file) {
103   std::string res("\n>>> referenced by ");
104   StringRef source = file->obj->getSourceFileName();
105   if (!source.empty())
106     res += source.str() + "\n>>>               ";
107   res += toString(file);
108   return {res};
109 }
110 
111 static Optional<std::pair<StringRef, uint32_t>>
112 getFileLineDwarf(const SectionChunk *c, uint32_t addr) {
113   if (!config->symbolizer)
114     config->symbolizer = make<symbolize::LLVMSymbolizer>();
115   Expected<DILineInfo> expectedLineInfo = config->symbolizer->symbolizeCode(
116       *c->file->getCOFFObj(), {addr, c->getSectionNumber() - 1});
117   if (!expectedLineInfo)
118     return None;
119   const DILineInfo &lineInfo = *expectedLineInfo;
120   if (lineInfo.FileName == DILineInfo::BadString)
121     return None;
122   return std::make_pair(saver.save(lineInfo.FileName), lineInfo.Line);
123 }
124 
125 static Optional<std::pair<StringRef, uint32_t>>
126 getFileLine(const SectionChunk *c, uint32_t addr) {
127   // MinGW can optionally use codeview, even if the default is dwarf.
128   Optional<std::pair<StringRef, uint32_t>> fileLine =
129       getFileLineCodeView(c, addr);
130   // If codeview didn't yield any result, check dwarf in MinGW mode.
131   if (!fileLine && config->mingw)
132     fileLine = getFileLineDwarf(c, addr);
133   return fileLine;
134 }
135 
136 // Given a file and the index of a symbol in that file, returns a description
137 // of all references to that symbol from that file. If no debug information is
138 // available, returns just the name of the file, else one string per actual
139 // reference as described in the debug info.
140 std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex) {
141   struct Location {
142     Symbol *sym;
143     std::pair<StringRef, uint32_t> fileLine;
144   };
145   std::vector<Location> locations;
146 
147   for (Chunk *c : file->getChunks()) {
148     auto *sc = dyn_cast<SectionChunk>(c);
149     if (!sc)
150       continue;
151     for (const coff_relocation &r : sc->getRelocs()) {
152       if (r.SymbolTableIndex != symIndex)
153         continue;
154       Optional<std::pair<StringRef, uint32_t>> fileLine =
155           getFileLine(sc, r.VirtualAddress);
156       Symbol *sym = getSymbol(sc, r.VirtualAddress);
157       if (fileLine)
158         locations.push_back({sym, *fileLine});
159       else if (sym)
160         locations.push_back({sym, {"", 0}});
161     }
162   }
163 
164   if (locations.empty())
165     return std::vector<std::string>({"\n>>> referenced by " + toString(file)});
166 
167   std::vector<std::string> symbolLocations(locations.size());
168   size_t i = 0;
169   for (Location loc : locations) {
170     llvm::raw_string_ostream os(symbolLocations[i++]);
171     os << "\n>>> referenced by ";
172     if (!loc.fileLine.first.empty())
173       os << loc.fileLine.first << ":" << loc.fileLine.second
174          << "\n>>>               ";
175     os << toString(file);
176     if (loc.sym)
177       os << ":(" << toString(*loc.sym) << ')';
178   }
179   return symbolLocations;
180 }
181 
182 std::vector<std::string> getSymbolLocations(InputFile *file,
183                                             uint32_t symIndex) {
184   if (auto *o = dyn_cast<ObjFile>(file))
185     return getSymbolLocations(o, symIndex);
186   if (auto *b = dyn_cast<BitcodeFile>(file))
187     return getSymbolLocations(b);
188   llvm_unreachable("unsupported file type passed to getSymbolLocations");
189   return {};
190 }
191 
192 // For an undefined symbol, stores all files referencing it and the index of
193 // the undefined symbol in each file.
194 struct UndefinedDiag {
195   Symbol *sym;
196   struct File {
197     InputFile *file;
198     uint32_t symIndex;
199   };
200   std::vector<File> files;
201 };
202 
203 static void reportUndefinedSymbol(const UndefinedDiag &undefDiag) {
204   std::string out;
205   llvm::raw_string_ostream os(out);
206   os << "undefined symbol: " << toString(*undefDiag.sym);
207 
208   const size_t maxUndefReferences = 10;
209   size_t i = 0, numRefs = 0;
210   for (const UndefinedDiag::File &ref : undefDiag.files) {
211     std::vector<std::string> symbolLocations =
212         getSymbolLocations(ref.file, ref.symIndex);
213     numRefs += symbolLocations.size();
214     for (const std::string &s : symbolLocations) {
215       if (i >= maxUndefReferences)
216         break;
217       os << s;
218       i++;
219     }
220   }
221   if (i < numRefs)
222     os << "\n>>> referenced " << numRefs - i << " more times";
223   errorOrWarn(os.str());
224 }
225 
226 void SymbolTable::loadMinGWAutomaticImports() {
227   for (auto &i : symMap) {
228     Symbol *sym = i.second;
229     auto *undef = dyn_cast<Undefined>(sym);
230     if (!undef)
231       continue;
232     if (!sym->isUsedInRegularObj)
233       continue;
234     if (undef->getWeakAlias())
235       continue;
236 
237     StringRef name = undef->getName();
238 
239     if (name.startswith("__imp_"))
240       continue;
241     // If we have an undefined symbol, but we have a lazy symbol we could
242     // load, load it.
243     Symbol *l = find(("__imp_" + name).str());
244     if (!l || l->pendingArchiveLoad || !l->isLazy())
245       continue;
246 
247     log("Loading lazy " + l->getName() + " from " + l->getFile()->getName() +
248         " for automatic import");
249     forceLazy(l);
250   }
251 }
252 
253 Defined *SymbolTable::impSymbol(StringRef name) {
254   if (name.startswith("__imp_"))
255     return nullptr;
256   return dyn_cast_or_null<Defined>(find(("__imp_" + name).str()));
257 }
258 
259 bool SymbolTable::handleMinGWAutomaticImport(Symbol *sym, StringRef name) {
260   Defined *imp = impSymbol(name);
261   if (!imp)
262     return false;
263 
264   // Replace the reference directly to a variable with a reference
265   // to the import address table instead. This obviously isn't right,
266   // but we mark the symbol as isRuntimePseudoReloc, and a later pass
267   // will add runtime pseudo relocations for every relocation against
268   // this Symbol. The runtime pseudo relocation framework expects the
269   // reference itself to point at the IAT entry.
270   size_t impSize = 0;
271   if (isa<DefinedImportData>(imp)) {
272     log("Automatically importing " + name + " from " +
273         cast<DefinedImportData>(imp)->getDLLName());
274     impSize = sizeof(DefinedImportData);
275   } else if (isa<DefinedRegular>(imp)) {
276     log("Automatically importing " + name + " from " +
277         toString(cast<DefinedRegular>(imp)->file));
278     impSize = sizeof(DefinedRegular);
279   } else {
280     warn("unable to automatically import " + name + " from " + imp->getName() +
281          " from " + toString(cast<DefinedRegular>(imp)->file) +
282          "; unexpected symbol type");
283     return false;
284   }
285   sym->replaceKeepingName(imp, impSize);
286   sym->isRuntimePseudoReloc = true;
287 
288   // There may exist symbols named .refptr.<name> which only consist
289   // of a single pointer to <name>. If it turns out <name> is
290   // automatically imported, we don't need to keep the .refptr.<name>
291   // pointer at all, but redirect all accesses to it to the IAT entry
292   // for __imp_<name> instead, and drop the whole .refptr.<name> chunk.
293   DefinedRegular *refptr =
294       dyn_cast_or_null<DefinedRegular>(find((".refptr." + name).str()));
295   if (refptr && refptr->getChunk()->getSize() == config->wordsize) {
296     SectionChunk *sc = dyn_cast_or_null<SectionChunk>(refptr->getChunk());
297     if (sc && sc->getRelocs().size() == 1 && *sc->symbols().begin() == sym) {
298       log("Replacing .refptr." + name + " with " + imp->getName());
299       refptr->getChunk()->live = false;
300       refptr->replaceKeepingName(imp, impSize);
301     }
302   }
303   return true;
304 }
305 
306 /// Helper function for reportUnresolvable and resolveRemainingUndefines.
307 /// This function emits an "undefined symbol" diagnostic for each symbol in
308 /// undefs. If localImports is not nullptr, it also emits a "locally
309 /// defined symbol imported" diagnostic for symbols in localImports.
310 /// objFiles and bitcodeFiles (if not nullptr) are used to report where
311 /// undefined symbols are referenced.
312 static void
313 reportProblemSymbols(const SmallPtrSetImpl<Symbol *> &undefs,
314                      const DenseMap<Symbol *, Symbol *> *localImports,
315                      const std::vector<ObjFile *> objFiles,
316                      const std::vector<BitcodeFile *> *bitcodeFiles) {
317 
318   // Return early if there is nothing to report (which should be
319   // the common case).
320   if (undefs.empty() && (!localImports || localImports->empty()))
321     return;
322 
323   for (Symbol *b : config->gcroot) {
324     if (undefs.count(b))
325       errorOrWarn("<root>: undefined symbol: " + toString(*b));
326     if (localImports)
327       if (Symbol *imp = localImports->lookup(b))
328         warn("<root>: locally defined symbol imported: " + toString(*imp) +
329              " (defined in " + toString(imp->getFile()) + ") [LNK4217]");
330   }
331 
332   std::vector<UndefinedDiag> undefDiags;
333   DenseMap<Symbol *, int> firstDiag;
334 
335   auto processFile = [&](InputFile *file, ArrayRef<Symbol *> symbols) {
336     uint32_t symIndex = (uint32_t)-1;
337     for (Symbol *sym : symbols) {
338       ++symIndex;
339       if (!sym)
340         continue;
341       if (undefs.count(sym)) {
342         auto it = firstDiag.find(sym);
343         if (it == firstDiag.end()) {
344           firstDiag[sym] = undefDiags.size();
345           undefDiags.push_back({sym, {{file, symIndex}}});
346         } else {
347           undefDiags[it->second].files.push_back({file, symIndex});
348         }
349       }
350       if (localImports)
351         if (Symbol *imp = localImports->lookup(sym))
352           warn(toString(file) +
353                ": locally defined symbol imported: " + toString(*imp) +
354                " (defined in " + toString(imp->getFile()) + ") [LNK4217]");
355     }
356   };
357 
358   for (ObjFile *file : objFiles)
359     processFile(file, file->getSymbols());
360 
361   if (bitcodeFiles)
362     for (BitcodeFile *file : *bitcodeFiles)
363       processFile(file, file->getSymbols());
364 
365   for (const UndefinedDiag &undefDiag : undefDiags)
366     reportUndefinedSymbol(undefDiag);
367 }
368 
369 void SymbolTable::reportUnresolvable() {
370   SmallPtrSet<Symbol *, 8> undefs;
371   for (auto &i : symMap) {
372     Symbol *sym = i.second;
373     auto *undef = dyn_cast<Undefined>(sym);
374     if (!undef)
375       continue;
376     if (undef->getWeakAlias())
377       continue;
378     StringRef name = undef->getName();
379     if (name.startswith("__imp_")) {
380       Symbol *imp = find(name.substr(strlen("__imp_")));
381       if (imp && isa<Defined>(imp))
382         continue;
383     }
384     if (name.contains("_PchSym_"))
385       continue;
386     if (config->mingw && impSymbol(name))
387       continue;
388     undefs.insert(sym);
389   }
390 
391   reportProblemSymbols(undefs,
392                        /* localImports */ nullptr, ObjFile::instances,
393                        &BitcodeFile::instances);
394 }
395 
396 void SymbolTable::resolveRemainingUndefines() {
397   SmallPtrSet<Symbol *, 8> undefs;
398   DenseMap<Symbol *, Symbol *> localImports;
399 
400   for (auto &i : symMap) {
401     Symbol *sym = i.second;
402     auto *undef = dyn_cast<Undefined>(sym);
403     if (!undef)
404       continue;
405     if (!sym->isUsedInRegularObj)
406       continue;
407 
408     StringRef name = undef->getName();
409 
410     // A weak alias may have been resolved, so check for that.
411     if (Defined *d = undef->getWeakAlias()) {
412       // We want to replace Sym with D. However, we can't just blindly
413       // copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
414       // internal symbol, and internal symbols are stored as "unparented"
415       // Symbols. For that reason we need to check which type of symbol we
416       // are dealing with and copy the correct number of bytes.
417       if (isa<DefinedRegular>(d))
418         memcpy(sym, d, sizeof(DefinedRegular));
419       else if (isa<DefinedAbsolute>(d))
420         memcpy(sym, d, sizeof(DefinedAbsolute));
421       else
422         memcpy(sym, d, sizeof(SymbolUnion));
423       continue;
424     }
425 
426     // If we can resolve a symbol by removing __imp_ prefix, do that.
427     // This odd rule is for compatibility with MSVC linker.
428     if (name.startswith("__imp_")) {
429       Symbol *imp = find(name.substr(strlen("__imp_")));
430       if (imp && isa<Defined>(imp)) {
431         auto *d = cast<Defined>(imp);
432         replaceSymbol<DefinedLocalImport>(sym, name, d);
433         localImportChunks.push_back(cast<DefinedLocalImport>(sym)->getChunk());
434         localImports[sym] = d;
435         continue;
436       }
437     }
438 
439     // We don't want to report missing Microsoft precompiled headers symbols.
440     // A proper message will be emitted instead in PDBLinker::aquirePrecompObj
441     if (name.contains("_PchSym_"))
442       continue;
443 
444     if (config->mingw && handleMinGWAutomaticImport(sym, name))
445       continue;
446 
447     // Remaining undefined symbols are not fatal if /force is specified.
448     // They are replaced with dummy defined symbols.
449     if (config->forceUnresolved)
450       replaceSymbol<DefinedAbsolute>(sym, name, 0);
451     undefs.insert(sym);
452   }
453 
454   reportProblemSymbols(
455       undefs, config->warnLocallyDefinedImported ? &localImports : nullptr,
456       ObjFile::instances, /* bitcode files no longer needed */ nullptr);
457 }
458 
459 std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
460   bool inserted = false;
461   Symbol *&sym = symMap[CachedHashStringRef(name)];
462   if (!sym) {
463     sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
464     sym->isUsedInRegularObj = false;
465     sym->pendingArchiveLoad = false;
466     inserted = true;
467   }
468   return {sym, inserted};
469 }
470 
471 std::pair<Symbol *, bool> SymbolTable::insert(StringRef name, InputFile *file) {
472   std::pair<Symbol *, bool> result = insert(name);
473   if (!file || !isa<BitcodeFile>(file))
474     result.first->isUsedInRegularObj = true;
475   return result;
476 }
477 
478 Symbol *SymbolTable::addUndefined(StringRef name, InputFile *f,
479                                   bool isWeakAlias) {
480   Symbol *s;
481   bool wasInserted;
482   std::tie(s, wasInserted) = insert(name, f);
483   if (wasInserted || (s->isLazy() && isWeakAlias)) {
484     replaceSymbol<Undefined>(s, name);
485     return s;
486   }
487   if (s->isLazy())
488     forceLazy(s);
489   return s;
490 }
491 
492 void SymbolTable::addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym) {
493   StringRef name = sym.getName();
494   Symbol *s;
495   bool wasInserted;
496   std::tie(s, wasInserted) = insert(name);
497   if (wasInserted) {
498     replaceSymbol<LazyArchive>(s, f, sym);
499     return;
500   }
501   auto *u = dyn_cast<Undefined>(s);
502   if (!u || u->weakAlias || s->pendingArchiveLoad)
503     return;
504   s->pendingArchiveLoad = true;
505   f->addMember(sym);
506 }
507 
508 void SymbolTable::addLazyObject(LazyObjFile *f, StringRef n) {
509   Symbol *s;
510   bool wasInserted;
511   std::tie(s, wasInserted) = insert(n, f);
512   if (wasInserted) {
513     replaceSymbol<LazyObject>(s, f, n);
514     return;
515   }
516   auto *u = dyn_cast<Undefined>(s);
517   if (!u || u->weakAlias || s->pendingArchiveLoad)
518     return;
519   s->pendingArchiveLoad = true;
520   f->fetch();
521 }
522 
523 static std::string getSourceLocationBitcode(BitcodeFile *file) {
524   std::string res("\n>>> defined at ");
525   StringRef source = file->obj->getSourceFileName();
526   if (!source.empty())
527     res += source.str() + "\n>>>            ";
528   res += toString(file);
529   return res;
530 }
531 
532 static std::string getSourceLocationObj(ObjFile *file, SectionChunk *sc,
533                                         uint32_t offset, StringRef name) {
534   Optional<std::pair<StringRef, uint32_t>> fileLine;
535   if (sc)
536     fileLine = getFileLine(sc, offset);
537   if (!fileLine)
538     fileLine = file->getVariableLocation(name);
539 
540   std::string res;
541   llvm::raw_string_ostream os(res);
542   os << "\n>>> defined at ";
543   if (fileLine)
544     os << fileLine->first << ":" << fileLine->second << "\n>>>            ";
545   os << toString(file);
546   return os.str();
547 }
548 
549 static std::string getSourceLocation(InputFile *file, SectionChunk *sc,
550                                      uint32_t offset, StringRef name) {
551   if (auto *o = dyn_cast<ObjFile>(file))
552     return getSourceLocationObj(o, sc, offset, name);
553   if (auto *b = dyn_cast<BitcodeFile>(file))
554     return getSourceLocationBitcode(b);
555   return "\n>>> defined at " + toString(file);
556 }
557 
558 // Construct and print an error message in the form of:
559 //
560 //   lld-link: error: duplicate symbol: foo
561 //   >>> defined at bar.c:30
562 //   >>>            bar.o
563 //   >>> defined at baz.c:563
564 //   >>>            baz.o
565 void SymbolTable::reportDuplicate(Symbol *existing, InputFile *newFile,
566                                   SectionChunk *newSc,
567                                   uint32_t newSectionOffset) {
568   std::string msg;
569   llvm::raw_string_ostream os(msg);
570   os << "duplicate symbol: " << toString(*existing);
571 
572   DefinedRegular *d = cast<DefinedRegular>(existing);
573   if (d && isa<ObjFile>(d->getFile())) {
574     os << getSourceLocation(d->getFile(), d->getChunk(), d->getValue(),
575                             existing->getName());
576   } else {
577     os << getSourceLocation(existing->getFile(), nullptr, 0, "");
578   }
579   os << getSourceLocation(newFile, newSc, newSectionOffset,
580                           existing->getName());
581 
582   if (config->forceMultiple)
583     warn(os.str());
584   else
585     error(os.str());
586 }
587 
588 Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {
589   Symbol *s;
590   bool wasInserted;
591   std::tie(s, wasInserted) = insert(n, nullptr);
592   s->isUsedInRegularObj = true;
593   if (wasInserted || isa<Undefined>(s) || s->isLazy())
594     replaceSymbol<DefinedAbsolute>(s, n, sym);
595   else if (!isa<DefinedCOFF>(s))
596     reportDuplicate(s, nullptr);
597   return s;
598 }
599 
600 Symbol *SymbolTable::addAbsolute(StringRef n, uint64_t va) {
601   Symbol *s;
602   bool wasInserted;
603   std::tie(s, wasInserted) = insert(n, nullptr);
604   s->isUsedInRegularObj = true;
605   if (wasInserted || isa<Undefined>(s) || s->isLazy())
606     replaceSymbol<DefinedAbsolute>(s, n, va);
607   else if (!isa<DefinedCOFF>(s))
608     reportDuplicate(s, nullptr);
609   return s;
610 }
611 
612 Symbol *SymbolTable::addSynthetic(StringRef n, Chunk *c) {
613   Symbol *s;
614   bool wasInserted;
615   std::tie(s, wasInserted) = insert(n, nullptr);
616   s->isUsedInRegularObj = true;
617   if (wasInserted || isa<Undefined>(s) || s->isLazy())
618     replaceSymbol<DefinedSynthetic>(s, n, c);
619   else if (!isa<DefinedCOFF>(s))
620     reportDuplicate(s, nullptr);
621   return s;
622 }
623 
624 Symbol *SymbolTable::addRegular(InputFile *f, StringRef n,
625                                 const coff_symbol_generic *sym, SectionChunk *c,
626                                 uint32_t sectionOffset) {
627   Symbol *s;
628   bool wasInserted;
629   std::tie(s, wasInserted) = insert(n, f);
630   if (wasInserted || !isa<DefinedRegular>(s))
631     replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ false,
632                                   /*IsExternal*/ true, sym, c);
633   else
634     reportDuplicate(s, f, c, sectionOffset);
635   return s;
636 }
637 
638 std::pair<DefinedRegular *, bool>
639 SymbolTable::addComdat(InputFile *f, StringRef n,
640                        const coff_symbol_generic *sym) {
641   Symbol *s;
642   bool wasInserted;
643   std::tie(s, wasInserted) = insert(n, f);
644   if (wasInserted || !isa<DefinedRegular>(s)) {
645     replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ true,
646                                   /*IsExternal*/ true, sym, nullptr);
647     return {cast<DefinedRegular>(s), true};
648   }
649   auto *existingSymbol = cast<DefinedRegular>(s);
650   if (!existingSymbol->isCOMDAT)
651     reportDuplicate(s, f);
652   return {existingSymbol, false};
653 }
654 
655 Symbol *SymbolTable::addCommon(InputFile *f, StringRef n, uint64_t size,
656                                const coff_symbol_generic *sym, CommonChunk *c) {
657   Symbol *s;
658   bool wasInserted;
659   std::tie(s, wasInserted) = insert(n, f);
660   if (wasInserted || !isa<DefinedCOFF>(s))
661     replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
662   else if (auto *dc = dyn_cast<DefinedCommon>(s))
663     if (size > dc->getSize())
664       replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
665   return s;
666 }
667 
668 Symbol *SymbolTable::addImportData(StringRef n, ImportFile *f) {
669   Symbol *s;
670   bool wasInserted;
671   std::tie(s, wasInserted) = insert(n, nullptr);
672   s->isUsedInRegularObj = true;
673   if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
674     replaceSymbol<DefinedImportData>(s, n, f);
675     return s;
676   }
677 
678   reportDuplicate(s, f);
679   return nullptr;
680 }
681 
682 Symbol *SymbolTable::addImportThunk(StringRef name, DefinedImportData *id,
683                                     uint16_t machine) {
684   Symbol *s;
685   bool wasInserted;
686   std::tie(s, wasInserted) = insert(name, nullptr);
687   s->isUsedInRegularObj = true;
688   if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
689     replaceSymbol<DefinedImportThunk>(s, name, id, machine);
690     return s;
691   }
692 
693   reportDuplicate(s, id->file);
694   return nullptr;
695 }
696 
697 void SymbolTable::addLibcall(StringRef name) {
698   Symbol *sym = findUnderscore(name);
699   if (!sym)
700     return;
701 
702   if (auto *l = dyn_cast<LazyArchive>(sym)) {
703     MemoryBufferRef mb = l->getMemberBuffer();
704     if (isBitcode(mb))
705       addUndefined(sym->getName());
706   } else if (LazyObject *o = dyn_cast<LazyObject>(sym)) {
707     if (isBitcode(o->file->mb))
708       addUndefined(sym->getName());
709   }
710 }
711 
712 std::vector<Chunk *> SymbolTable::getChunks() {
713   std::vector<Chunk *> res;
714   for (ObjFile *file : ObjFile::instances) {
715     ArrayRef<Chunk *> v = file->getChunks();
716     res.insert(res.end(), v.begin(), v.end());
717   }
718   return res;
719 }
720 
721 Symbol *SymbolTable::find(StringRef name) {
722   return symMap.lookup(CachedHashStringRef(name));
723 }
724 
725 Symbol *SymbolTable::findUnderscore(StringRef name) {
726   if (config->machine == I386)
727     return find(("_" + name).str());
728   return find(name);
729 }
730 
731 // Return all symbols that start with Prefix, possibly ignoring the first
732 // character of Prefix or the first character symbol.
733 std::vector<Symbol *> SymbolTable::getSymsWithPrefix(StringRef prefix) {
734   std::vector<Symbol *> syms;
735   for (auto pair : symMap) {
736     StringRef name = pair.first.val();
737     if (name.startswith(prefix) || name.startswith(prefix.drop_front()) ||
738         name.drop_front().startswith(prefix) ||
739         name.drop_front().startswith(prefix.drop_front())) {
740       syms.push_back(pair.second);
741     }
742   }
743   return syms;
744 }
745 
746 Symbol *SymbolTable::findMangle(StringRef name) {
747   if (Symbol *sym = find(name))
748     if (!isa<Undefined>(sym))
749       return sym;
750 
751   // Efficient fuzzy string lookup is impossible with a hash table, so iterate
752   // the symbol table once and collect all possibly matching symbols into this
753   // vector. Then compare each possibly matching symbol with each possible
754   // mangling.
755   std::vector<Symbol *> syms = getSymsWithPrefix(name);
756   auto findByPrefix = [&syms](const Twine &t) -> Symbol * {
757     std::string prefix = t.str();
758     for (auto *s : syms)
759       if (s->getName().startswith(prefix))
760         return s;
761     return nullptr;
762   };
763 
764   // For non-x86, just look for C++ functions.
765   if (config->machine != I386)
766     return findByPrefix("?" + name + "@@Y");
767 
768   if (!name.startswith("_"))
769     return nullptr;
770   // Search for x86 stdcall function.
771   if (Symbol *s = findByPrefix(name + "@"))
772     return s;
773   // Search for x86 fastcall function.
774   if (Symbol *s = findByPrefix("@" + name.substr(1) + "@"))
775     return s;
776   // Search for x86 vectorcall function.
777   if (Symbol *s = findByPrefix(name.substr(1) + "@@"))
778     return s;
779   // Search for x86 C++ non-member function.
780   return findByPrefix("?" + name.substr(1) + "@@Y");
781 }
782 
783 Symbol *SymbolTable::addUndefined(StringRef name) {
784   return addUndefined(name, nullptr, false);
785 }
786 
787 std::vector<StringRef> SymbolTable::compileBitcodeFiles() {
788   lto.reset(new BitcodeCompiler);
789   for (BitcodeFile *f : BitcodeFile::instances)
790     lto->add(*f);
791   return lto->compile();
792 }
793 
794 void SymbolTable::addCombinedLTOObjects() {
795   if (BitcodeFile::instances.empty())
796     return;
797 
798   ScopedTimer t(ltoTimer);
799   for (StringRef object : compileBitcodeFiles()) {
800     auto *obj = make<ObjFile>(MemoryBufferRef(object, "lto.tmp"));
801     obj->parse();
802     ObjFile::instances.push_back(obj);
803   }
804 }
805 
806 } // namespace coff
807 } // namespace lld
808