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 
189     StringRef name = undef->getName();
190 
191     if (name.startswith("__imp_"))
192       continue;
193     // If we have an undefined symbol, but we have a Lazy representing a
194     // symbol we could load from file, make sure to load that.
195     Lazy *l = dyn_cast_or_null<Lazy>(find(("__imp_" + name).str()));
196     if (!l || l->pendingArchiveLoad)
197       continue;
198 
199     log("Loading lazy " + l->getName() + " from " + l->file->getName() +
200         " for automatic import");
201     l->pendingArchiveLoad = true;
202     l->file->addMember(l->sym);
203   }
204 }
205 
206 Defined *SymbolTable::impSymbol(StringRef name) {
207   if (name.startswith("__imp_"))
208     return nullptr;
209   return dyn_cast_or_null<Defined>(find(("__imp_" + name).str()));
210 }
211 
212 bool SymbolTable::handleMinGWAutomaticImport(Symbol *sym, StringRef name) {
213   Defined *imp = impSymbol(name);
214   if (!imp)
215     return false;
216 
217   // Replace the reference directly to a variable with a reference
218   // to the import address table instead. This obviously isn't right,
219   // but we mark the symbol as isRuntimePseudoReloc, and a later pass
220   // will add runtime pseudo relocations for every relocation against
221   // this Symbol. The runtime pseudo relocation framework expects the
222   // reference itself to point at the IAT entry.
223   size_t impSize = 0;
224   if (isa<DefinedImportData>(imp)) {
225     log("Automatically importing " + name + " from " +
226         cast<DefinedImportData>(imp)->getDLLName());
227     impSize = sizeof(DefinedImportData);
228   } else if (isa<DefinedRegular>(imp)) {
229     log("Automatically importing " + name + " from " +
230         toString(cast<DefinedRegular>(imp)->file));
231     impSize = sizeof(DefinedRegular);
232   } else {
233     warn("unable to automatically import " + name + " from " + imp->getName() +
234          " from " + toString(cast<DefinedRegular>(imp)->file) +
235          "; unexpected symbol type");
236     return false;
237   }
238   sym->replaceKeepingName(imp, impSize);
239   sym->isRuntimePseudoReloc = true;
240 
241   // There may exist symbols named .refptr.<name> which only consist
242   // of a single pointer to <name>. If it turns out <name> is
243   // automatically imported, we don't need to keep the .refptr.<name>
244   // pointer at all, but redirect all accesses to it to the IAT entry
245   // for __imp_<name> instead, and drop the whole .refptr.<name> chunk.
246   DefinedRegular *refptr =
247       dyn_cast_or_null<DefinedRegular>(find((".refptr." + name).str()));
248   if (refptr && refptr->getChunk()->getSize() == config->wordsize) {
249     SectionChunk *sc = dyn_cast_or_null<SectionChunk>(refptr->getChunk());
250     if (sc && sc->getRelocs().size() == 1 && *sc->symbols().begin() == sym) {
251       log("Replacing .refptr." + name + " with " + imp->getName());
252       refptr->getChunk()->live = false;
253       refptr->replaceKeepingName(imp, impSize);
254     }
255   }
256   return true;
257 }
258 
259 /// Helper function for reportUnresolvable and resolveRemainingUndefines.
260 /// This function emits an "undefined symbol" diagnostic for each symbol in
261 /// undefs. If localImports is not nullptr, it also emits a "locally
262 /// defined symbol imported" diagnostic for symbols in localImports.
263 /// objFiles and bitcodeFiles (if not nullptr) are used to report where
264 /// undefined symbols are referenced.
265 static void
266 reportProblemSymbols(const SmallPtrSetImpl<Symbol *> &undefs,
267                      const DenseMap<Symbol *, Symbol *> *localImports,
268                      const std::vector<ObjFile *> objFiles,
269                      const std::vector<BitcodeFile *> *bitcodeFiles) {
270 
271   // Return early if there is nothing to report (which should be
272   // the common case).
273   if (undefs.empty() && (!localImports || localImports->empty()))
274     return;
275 
276   for (Symbol *b : config->gcroot) {
277     if (undefs.count(b))
278       errorOrWarn("<root>: undefined symbol: " + toString(*b));
279     if (localImports)
280       if (Symbol *imp = localImports->lookup(b))
281         warn("<root>: locally defined symbol imported: " + toString(*imp) +
282              " (defined in " + toString(imp->getFile()) + ") [LNK4217]");
283   }
284 
285   std::vector<UndefinedDiag> undefDiags;
286   DenseMap<Symbol *, int> firstDiag;
287 
288   auto processFile = [&](InputFile *file, ArrayRef<Symbol *> symbols) {
289     uint32_t symIndex = (uint32_t)-1;
290     for (Symbol *sym : symbols) {
291       ++symIndex;
292       if (!sym)
293         continue;
294       if (undefs.count(sym)) {
295         auto it = firstDiag.find(sym);
296         if (it == firstDiag.end()) {
297           firstDiag[sym] = undefDiags.size();
298           undefDiags.push_back({sym, {{file, symIndex}}});
299         } else {
300           undefDiags[it->second].files.push_back({file, symIndex});
301         }
302       }
303       if (localImports)
304         if (Symbol *imp = localImports->lookup(sym))
305           warn(toString(file) +
306                ": locally defined symbol imported: " + toString(*imp) +
307                " (defined in " + toString(imp->getFile()) + ") [LNK4217]");
308     }
309   };
310 
311   for (ObjFile *file : objFiles)
312     processFile(file, file->getSymbols());
313 
314   if (bitcodeFiles)
315     for (BitcodeFile *file : *bitcodeFiles)
316       processFile(file, file->getSymbols());
317 
318   for (const UndefinedDiag &undefDiag : undefDiags)
319     reportUndefinedSymbol(undefDiag);
320 }
321 
322 void SymbolTable::reportUnresolvable() {
323   SmallPtrSet<Symbol *, 8> undefs;
324   for (auto &i : symMap) {
325     Symbol *sym = i.second;
326     auto *undef = dyn_cast<Undefined>(sym);
327     if (!undef)
328       continue;
329     if (Defined *d = undef->getWeakAlias())
330       continue;
331     StringRef name = undef->getName();
332     if (name.startswith("__imp_")) {
333       Symbol *imp = find(name.substr(strlen("__imp_")));
334       if (imp && isa<Defined>(imp))
335         continue;
336     }
337     if (name.contains("_PchSym_"))
338       continue;
339     if (config->mingw && impSymbol(name))
340       continue;
341     undefs.insert(sym);
342   }
343 
344   reportProblemSymbols(undefs,
345                        /* localImports */ nullptr, ObjFile::instances,
346                        &BitcodeFile::instances);
347 }
348 
349 void SymbolTable::resolveRemainingUndefines() {
350   SmallPtrSet<Symbol *, 8> undefs;
351   DenseMap<Symbol *, Symbol *> localImports;
352 
353   for (auto &i : symMap) {
354     Symbol *sym = i.second;
355     auto *undef = dyn_cast<Undefined>(sym);
356     if (!undef)
357       continue;
358     if (!sym->isUsedInRegularObj)
359       continue;
360 
361     StringRef name = undef->getName();
362 
363     // A weak alias may have been resolved, so check for that.
364     if (Defined *d = undef->getWeakAlias()) {
365       // We want to replace Sym with D. However, we can't just blindly
366       // copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
367       // internal symbol, and internal symbols are stored as "unparented"
368       // Symbols. For that reason we need to check which type of symbol we
369       // are dealing with and copy the correct number of bytes.
370       if (isa<DefinedRegular>(d))
371         memcpy(sym, d, sizeof(DefinedRegular));
372       else if (isa<DefinedAbsolute>(d))
373         memcpy(sym, d, sizeof(DefinedAbsolute));
374       else
375         memcpy(sym, d, sizeof(SymbolUnion));
376       continue;
377     }
378 
379     // If we can resolve a symbol by removing __imp_ prefix, do that.
380     // This odd rule is for compatibility with MSVC linker.
381     if (name.startswith("__imp_")) {
382       Symbol *imp = find(name.substr(strlen("__imp_")));
383       if (imp && isa<Defined>(imp)) {
384         auto *d = cast<Defined>(imp);
385         replaceSymbol<DefinedLocalImport>(sym, name, d);
386         localImportChunks.push_back(cast<DefinedLocalImport>(sym)->getChunk());
387         localImports[sym] = d;
388         continue;
389       }
390     }
391 
392     // We don't want to report missing Microsoft precompiled headers symbols.
393     // A proper message will be emitted instead in PDBLinker::aquirePrecompObj
394     if (name.contains("_PchSym_"))
395       continue;
396 
397     if (config->mingw && handleMinGWAutomaticImport(sym, name))
398       continue;
399 
400     // Remaining undefined symbols are not fatal if /force is specified.
401     // They are replaced with dummy defined symbols.
402     if (config->forceUnresolved)
403       replaceSymbol<DefinedAbsolute>(sym, name, 0);
404     undefs.insert(sym);
405   }
406 
407   reportProblemSymbols(
408       undefs, config->warnLocallyDefinedImported ? &localImports : nullptr,
409       ObjFile::instances, /* bitcode files no longer needed */ nullptr);
410 }
411 
412 std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
413   bool inserted = false;
414   Symbol *&sym = symMap[CachedHashStringRef(name)];
415   if (!sym) {
416     sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
417     sym->isUsedInRegularObj = false;
418     sym->pendingArchiveLoad = false;
419     inserted = true;
420   }
421   return {sym, inserted};
422 }
423 
424 std::pair<Symbol *, bool> SymbolTable::insert(StringRef name, InputFile *file) {
425   std::pair<Symbol *, bool> result = insert(name);
426   if (!file || !isa<BitcodeFile>(file))
427     result.first->isUsedInRegularObj = true;
428   return result;
429 }
430 
431 Symbol *SymbolTable::addUndefined(StringRef name, InputFile *f,
432                                   bool isWeakAlias) {
433   Symbol *s;
434   bool wasInserted;
435   std::tie(s, wasInserted) = insert(name, f);
436   if (wasInserted || (isa<Lazy>(s) && isWeakAlias)) {
437     replaceSymbol<Undefined>(s, name);
438     return s;
439   }
440   if (auto *l = dyn_cast<Lazy>(s)) {
441     if (!s->pendingArchiveLoad) {
442       s->pendingArchiveLoad = true;
443       l->file->addMember(l->sym);
444     }
445   }
446   return s;
447 }
448 
449 void SymbolTable::addLazy(ArchiveFile *f, const Archive::Symbol &sym) {
450   StringRef name = sym.getName();
451   Symbol *s;
452   bool wasInserted;
453   std::tie(s, wasInserted) = insert(name);
454   if (wasInserted) {
455     replaceSymbol<Lazy>(s, f, sym);
456     return;
457   }
458   auto *u = dyn_cast<Undefined>(s);
459   if (!u || u->weakAlias || s->pendingArchiveLoad)
460     return;
461   s->pendingArchiveLoad = true;
462   f->addMember(sym);
463 }
464 
465 void SymbolTable::reportDuplicate(Symbol *existing, InputFile *newFile) {
466   std::string msg = "duplicate symbol: " + toString(*existing) + " in " +
467                     toString(existing->getFile()) + " and in " +
468                     toString(newFile);
469 
470   if (config->forceMultiple)
471     warn(msg);
472   else
473     error(msg);
474 }
475 
476 Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {
477   Symbol *s;
478   bool wasInserted;
479   std::tie(s, wasInserted) = insert(n, nullptr);
480   s->isUsedInRegularObj = true;
481   if (wasInserted || isa<Undefined>(s) || isa<Lazy>(s))
482     replaceSymbol<DefinedAbsolute>(s, n, sym);
483   else if (!isa<DefinedCOFF>(s))
484     reportDuplicate(s, nullptr);
485   return s;
486 }
487 
488 Symbol *SymbolTable::addAbsolute(StringRef n, uint64_t va) {
489   Symbol *s;
490   bool wasInserted;
491   std::tie(s, wasInserted) = insert(n, nullptr);
492   s->isUsedInRegularObj = true;
493   if (wasInserted || isa<Undefined>(s) || isa<Lazy>(s))
494     replaceSymbol<DefinedAbsolute>(s, n, va);
495   else if (!isa<DefinedCOFF>(s))
496     reportDuplicate(s, nullptr);
497   return s;
498 }
499 
500 Symbol *SymbolTable::addSynthetic(StringRef n, Chunk *c) {
501   Symbol *s;
502   bool wasInserted;
503   std::tie(s, wasInserted) = insert(n, nullptr);
504   s->isUsedInRegularObj = true;
505   if (wasInserted || isa<Undefined>(s) || isa<Lazy>(s))
506     replaceSymbol<DefinedSynthetic>(s, n, c);
507   else if (!isa<DefinedCOFF>(s))
508     reportDuplicate(s, nullptr);
509   return s;
510 }
511 
512 Symbol *SymbolTable::addRegular(InputFile *f, StringRef n,
513                                 const coff_symbol_generic *sym,
514                                 SectionChunk *c) {
515   Symbol *s;
516   bool wasInserted;
517   std::tie(s, wasInserted) = insert(n, f);
518   if (wasInserted || !isa<DefinedRegular>(s))
519     replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ false,
520                                   /*IsExternal*/ true, sym, c);
521   else
522     reportDuplicate(s, f);
523   return s;
524 }
525 
526 std::pair<DefinedRegular *, bool>
527 SymbolTable::addComdat(InputFile *f, StringRef n,
528                        const coff_symbol_generic *sym) {
529   Symbol *s;
530   bool wasInserted;
531   std::tie(s, wasInserted) = insert(n, f);
532   if (wasInserted || !isa<DefinedRegular>(s)) {
533     replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ true,
534                                   /*IsExternal*/ true, sym, nullptr);
535     return {cast<DefinedRegular>(s), true};
536   }
537   auto *existingSymbol = cast<DefinedRegular>(s);
538   if (!existingSymbol->isCOMDAT)
539     reportDuplicate(s, f);
540   return {existingSymbol, false};
541 }
542 
543 Symbol *SymbolTable::addCommon(InputFile *f, StringRef n, uint64_t size,
544                                const coff_symbol_generic *sym, CommonChunk *c) {
545   Symbol *s;
546   bool wasInserted;
547   std::tie(s, wasInserted) = insert(n, f);
548   if (wasInserted || !isa<DefinedCOFF>(s))
549     replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
550   else if (auto *dc = dyn_cast<DefinedCommon>(s))
551     if (size > dc->getSize())
552       replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
553   return s;
554 }
555 
556 Symbol *SymbolTable::addImportData(StringRef n, ImportFile *f) {
557   Symbol *s;
558   bool wasInserted;
559   std::tie(s, wasInserted) = insert(n, nullptr);
560   s->isUsedInRegularObj = true;
561   if (wasInserted || isa<Undefined>(s) || isa<Lazy>(s)) {
562     replaceSymbol<DefinedImportData>(s, n, f);
563     return s;
564   }
565 
566   reportDuplicate(s, f);
567   return nullptr;
568 }
569 
570 Symbol *SymbolTable::addImportThunk(StringRef name, DefinedImportData *id,
571                                     uint16_t machine) {
572   Symbol *s;
573   bool wasInserted;
574   std::tie(s, wasInserted) = insert(name, nullptr);
575   s->isUsedInRegularObj = true;
576   if (wasInserted || isa<Undefined>(s) || isa<Lazy>(s)) {
577     replaceSymbol<DefinedImportThunk>(s, name, id, machine);
578     return s;
579   }
580 
581   reportDuplicate(s, id->file);
582   return nullptr;
583 }
584 
585 std::vector<Chunk *> SymbolTable::getChunks() {
586   std::vector<Chunk *> res;
587   for (ObjFile *file : ObjFile::instances) {
588     ArrayRef<Chunk *> v = file->getChunks();
589     res.insert(res.end(), v.begin(), v.end());
590   }
591   return res;
592 }
593 
594 Symbol *SymbolTable::find(StringRef name) {
595   return symMap.lookup(CachedHashStringRef(name));
596 }
597 
598 Symbol *SymbolTable::findUnderscore(StringRef name) {
599   if (config->machine == I386)
600     return find(("_" + name).str());
601   return find(name);
602 }
603 
604 // Return all symbols that start with Prefix, possibly ignoring the first
605 // character of Prefix or the first character symbol.
606 std::vector<Symbol *> SymbolTable::getSymsWithPrefix(StringRef prefix) {
607   std::vector<Symbol *> syms;
608   for (auto pair : symMap) {
609     StringRef name = pair.first.val();
610     if (name.startswith(prefix) || name.startswith(prefix.drop_front()) ||
611         name.drop_front().startswith(prefix) ||
612         name.drop_front().startswith(prefix.drop_front())) {
613       syms.push_back(pair.second);
614     }
615   }
616   return syms;
617 }
618 
619 Symbol *SymbolTable::findMangle(StringRef name) {
620   if (Symbol *sym = find(name))
621     if (!isa<Undefined>(sym))
622       return sym;
623 
624   // Efficient fuzzy string lookup is impossible with a hash table, so iterate
625   // the symbol table once and collect all possibly matching symbols into this
626   // vector. Then compare each possibly matching symbol with each possible
627   // mangling.
628   std::vector<Symbol *> syms = getSymsWithPrefix(name);
629   auto findByPrefix = [&syms](const Twine &t) -> Symbol * {
630     std::string prefix = t.str();
631     for (auto *s : syms)
632       if (s->getName().startswith(prefix))
633         return s;
634     return nullptr;
635   };
636 
637   // For non-x86, just look for C++ functions.
638   if (config->machine != I386)
639     return findByPrefix("?" + name + "@@Y");
640 
641   if (!name.startswith("_"))
642     return nullptr;
643   // Search for x86 stdcall function.
644   if (Symbol *s = findByPrefix(name + "@"))
645     return s;
646   // Search for x86 fastcall function.
647   if (Symbol *s = findByPrefix("@" + name.substr(1) + "@"))
648     return s;
649   // Search for x86 vectorcall function.
650   if (Symbol *s = findByPrefix(name.substr(1) + "@@"))
651     return s;
652   // Search for x86 C++ non-member function.
653   return findByPrefix("?" + name.substr(1) + "@@Y");
654 }
655 
656 Symbol *SymbolTable::addUndefined(StringRef name) {
657   return addUndefined(name, nullptr, false);
658 }
659 
660 std::vector<StringRef> SymbolTable::compileBitcodeFiles() {
661   lto.reset(new BitcodeCompiler);
662   for (BitcodeFile *f : BitcodeFile::instances)
663     lto->add(*f);
664   return lto->compile();
665 }
666 
667 void SymbolTable::addCombinedLTOObjects() {
668   if (BitcodeFile::instances.empty())
669     return;
670 
671   ScopedTimer t(ltoTimer);
672   for (StringRef object : compileBitcodeFiles()) {
673     auto *obj = make<ObjFile>(MemoryBufferRef(object, "lto.tmp"));
674     obj->parse();
675     ObjFile::instances.push_back(obj);
676   }
677 }
678 
679 } // namespace coff
680 } // namespace lld
681