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