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