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