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