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
27using namespace llvm;
28
29namespace lld::coff {
30
31StringRef ltrim1(StringRef s, const char *chars) {
32 if (!s.empty() && strchr(chars, s[0]))
33 return s.substr(1);
34 return s;
35}
36
37void SymbolTable::addFile(InputFile *file) {
38 log("Reading " + toString(file));
39 if (file->lazy) {
40 if (auto *f = dyn_cast<BitcodeFile>(file))
41 f->parseLazy();
42 else
43 cast<ObjFile>(file)->parseLazy();
44 } else {
45 file->parse();
46 if (auto *f = dyn_cast<ObjFile>(file)) {
47 ctx.objFileInstances.push_back(f);
48 } else if (auto *f = dyn_cast<BitcodeFile>(file)) {
49 ctx.bitcodeFileInstances.push_back(f);
50 } else if (auto *f = dyn_cast<ImportFile>(file)) {
51 ctx.importFileInstances.push_back(f);
52 }
53 }
54
55 MachineTypes mt = file->getMachineType();
56 if (config->machine == IMAGE_FILE_MACHINE_UNKNOWN) {
57 config->machine = mt;
58 driver->addWinSysRootLibSearchPaths();
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
68static 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.
76static 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.
103static 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
119static 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
128static Optional<std::pair<StringRef, uint32_t>>
129getFileLineDwarf(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
140static Optional<std::pair<StringRef, uint32_t>>
141getFileLine(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.
157static std::pair<std::vector<std::string>, size_t>
158getSymbolLocations(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
209std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex) {
210 return getSymbolLocations(file, symIndex, SIZE_MAX).first;
211}
212
213static std::pair<std::vector<std::string>, size_t>
214getSymbolLocations(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.
230struct UndefinedDiag {
231 Symbol *sym;
232 struct File {
233 InputFile *file;
234 uint32_t symIndex;
235 };
236 std::vector<File> files;
237};
238
239static 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 auto [symbolLocations, totalLocations] = getSymbolLocations(
248 ref.file, ref.symIndex, maxUndefReferences - numDisplayedRefs);
249
250 numRefs += totalLocations;
251 numDisplayedRefs += symbolLocations.size();
252 for (const std::string &s : symbolLocations) {
253 os << s;
254 }
255 }
256 if (numDisplayedRefs < numRefs)
257 os << "\n>>> referenced " << numRefs - numDisplayedRefs << " more times";
258 errorOrWarn(os.str());
259}
260
261void SymbolTable::loadMinGWSymbols() {
262 for (auto &i : symMap) {
263 Symbol *sym = i.second;
264 auto *undef = dyn_cast<Undefined>(sym);
265 if (!undef)
266 continue;
267 if (undef->getWeakAlias())
268 continue;
269
270 StringRef name = undef->getName();
271
272 if (config->machine == I386 && config->stdcallFixup) {
273 // Check if we can resolve an undefined decorated symbol by finding
274 // the intended target as an undecorated symbol (only with a leading
275 // underscore).
276 StringRef origName = name;
277 StringRef baseName = name;
278 // Trim down stdcall/fastcall/vectorcall symbols to the base name.
279 baseName = ltrim1(baseName, "_@");
280 baseName = baseName.substr(0, baseName.find('@'));
281 // Add a leading underscore, as it would be in cdecl form.
282 std::string newName = ("_" + baseName).str();
283 Symbol *l;
284 if (newName != origName && (l = find(newName)) != nullptr) {
285 // If we found a symbol and it is lazy; load it.
286 if (l->isLazy() && !l->pendingArchiveLoad) {
287 log("Loading lazy " + l->getName() + " from " +
288 l->getFile()->getName() + " for stdcall fixup");
289 forceLazy(l);
290 }
291 // If it's lazy or already defined, hook it up as weak alias.
292 if (l->isLazy() || isa<Defined>(l)) {
293 if (config->warnStdcallFixup)
294 warn("Resolving " + origName + " by linking to " + newName);
295 else
296 log("Resolving " + origName + " by linking to " + newName);
297 undef->weakAlias = l;
298 continue;
299 }
300 }
301 }
302
303 if (config->autoImport) {
304 if (name.startswith("__imp_"))
305 continue;
306 // If we have an undefined symbol, but we have a lazy symbol we could
307 // load, load it.
308 Symbol *l = find(("__imp_" + name).str());
309 if (!l || l->pendingArchiveLoad || !l->isLazy())
310 continue;
311
312 log("Loading lazy " + l->getName() + " from " + l->getFile()->getName() +
313 " for automatic import");
314 forceLazy(l);
315 }
316 }
317}
318
319Defined *SymbolTable::impSymbol(StringRef name) {
320 if (name.startswith("__imp_"))
321 return nullptr;
322 return dyn_cast_or_null<Defined>(find(("__imp_" + name).str()));
323}
324
325bool SymbolTable::handleMinGWAutomaticImport(Symbol *sym, StringRef name) {
326 Defined *imp = impSymbol(name);
327 if (!imp)
328 return false;
329
330 // Replace the reference directly to a variable with a reference
331 // to the import address table instead. This obviously isn't right,
332 // but we mark the symbol as isRuntimePseudoReloc, and a later pass
333 // will add runtime pseudo relocations for every relocation against
334 // this Symbol. The runtime pseudo relocation framework expects the
335 // reference itself to point at the IAT entry.
336 size_t impSize = 0;
337 if (isa<DefinedImportData>(imp)) {
338 log("Automatically importing " + name + " from " +
339 cast<DefinedImportData>(imp)->getDLLName());
340 impSize = sizeof(DefinedImportData);
341 } else if (isa<DefinedRegular>(imp)) {
342 log("Automatically importing " + name + " from " +
343 toString(cast<DefinedRegular>(imp)->file));
344 impSize = sizeof(DefinedRegular);
345 } else {
346 warn("unable to automatically import " + name + " from " + imp->getName() +
347 " from " + toString(cast<DefinedRegular>(imp)->file) +
348 "; unexpected symbol type");
349 return false;
350 }
351 sym->replaceKeepingName(imp, impSize);
352 sym->isRuntimePseudoReloc = true;
353
354 // There may exist symbols named .refptr.<name> which only consist
355 // of a single pointer to <name>. If it turns out <name> is
356 // automatically imported, we don't need to keep the .refptr.<name>
357 // pointer at all, but redirect all accesses to it to the IAT entry
358 // for __imp_<name> instead, and drop the whole .refptr.<name> chunk.
359 DefinedRegular *refptr =
360 dyn_cast_or_null<DefinedRegular>(find((".refptr." + name).str()));
361 if (refptr && refptr->getChunk()->getSize() == config->wordsize) {
362 SectionChunk *sc = dyn_cast_or_null<SectionChunk>(refptr->getChunk());
363 if (sc && sc->getRelocs().size() == 1 && *sc->symbols().begin() == sym) {
364 log("Replacing .refptr." + name + " with " + imp->getName());
365 refptr->getChunk()->live = false;
366 refptr->replaceKeepingName(imp, impSize);
367 }
368 }
369 return true;
370}
371
372/// Helper function for reportUnresolvable and resolveRemainingUndefines.
373/// This function emits an "undefined symbol" diagnostic for each symbol in
374/// undefs. If localImports is not nullptr, it also emits a "locally
375/// defined symbol imported" diagnostic for symbols in localImports.
376/// objFiles and bitcodeFiles (if not nullptr) are used to report where
377/// undefined symbols are referenced.
378static void reportProblemSymbols(
379 const COFFLinkerContext &ctx, const SmallPtrSetImpl<Symbol *> &undefs,
380 const DenseMap<Symbol *, Symbol *> *localImports, bool needBitcodeFiles) {
381 // Return early if there is nothing to report (which should be
382 // the common case).
383 if (undefs.empty() && (!localImports || localImports->empty()))
384 return;
385
386 for (Symbol *b : config->gcroot) {
387 if (undefs.count(b))
388 errorOrWarn("<root>: undefined symbol: " + toString(*b));
389 if (localImports)
390 if (Symbol *imp = localImports->lookup(b))
391 warn("<root>: locally defined symbol imported: " + toString(*imp) +
392 " (defined in " + toString(imp->getFile()) + ") [LNK4217]");
393 }
394
395 std::vector<UndefinedDiag> undefDiags;
396 DenseMap<Symbol *, int> firstDiag;
397
398 auto processFile = [&](InputFile *file, ArrayRef<Symbol *> symbols) {
399 uint32_t symIndex = (uint32_t)-1;
400 for (Symbol *sym : symbols) {
401 ++symIndex;
402 if (!sym)
403 continue;
404 if (undefs.count(sym)) {
405 auto it = firstDiag.find(sym);
406 if (it == firstDiag.end()) {
407 firstDiag[sym] = undefDiags.size();
408 undefDiags.push_back({sym, {{file, symIndex}}});
409 } else {
410 undefDiags[it->second].files.push_back({file, symIndex});
411 }
412 }
413 if (localImports)
414 if (Symbol *imp = localImports->lookup(sym))
415 warn(toString(file) +
416 ": locally defined symbol imported: " + toString(*imp) +
417 " (defined in " + toString(imp->getFile()) + ") [LNK4217]");
418 }
419 };
420
421 for (ObjFile *file : ctx.objFileInstances)
422 processFile(file, file->getSymbols());
423
424 if (needBitcodeFiles)
425 for (BitcodeFile *file : ctx.bitcodeFileInstances)
426 processFile(file, file->getSymbols());
427
428 for (const UndefinedDiag &undefDiag : undefDiags)
429 reportUndefinedSymbol(undefDiag);
430}
431
432void SymbolTable::reportUnresolvable() {
433 SmallPtrSet<Symbol *, 8> undefs;
434 for (auto &i : symMap) {
435 Symbol *sym = i.second;
436 auto *undef = dyn_cast<Undefined>(sym);
437 if (!undef || sym->deferUndefined)
438 continue;
439 if (undef->getWeakAlias())
440 continue;
441 StringRef name = undef->getName();
442 if (name.startswith("__imp_")) {
443 Symbol *imp = find(name.substr(strlen("__imp_")));
444 if (imp && isa<Defined>(imp))
445 continue;
446 }
447 if (name.contains("_PchSym_"))
448 continue;
449 if (config->autoImport && impSymbol(name))
450 continue;
451 undefs.insert(sym);
452 }
453
454 reportProblemSymbols(ctx, undefs,
455 /* localImports */ nullptr, true);
456}
457
458void SymbolTable::resolveRemainingUndefines() {
459 SmallPtrSet<Symbol *, 8> undefs;
460 DenseMap<Symbol *, Symbol *> localImports;
461
462 for (auto &i : symMap) {
463 Symbol *sym = i.second;
464 auto *undef = dyn_cast<Undefined>(sym);
465 if (!undef)
466 continue;
467 if (!sym->isUsedInRegularObj)
468 continue;
469
470 StringRef name = undef->getName();
471
472 // A weak alias may have been resolved, so check for that.
473 if (Defined *d = undef->getWeakAlias()) {
474 // We want to replace Sym with D. However, we can't just blindly
475 // copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
476 // internal symbol, and internal symbols are stored as "unparented"
477 // Symbols. For that reason we need to check which type of symbol we
478 // are dealing with and copy the correct number of bytes.
479 if (isa<DefinedRegular>(d))
480 memcpy(sym, d, sizeof(DefinedRegular));
481 else if (isa<DefinedAbsolute>(d))
482 memcpy(sym, d, sizeof(DefinedAbsolute));
483 else
484 memcpy(sym, d, sizeof(SymbolUnion));
485 continue;
486 }
487
488 // If we can resolve a symbol by removing __imp_ prefix, do that.
489 // This odd rule is for compatibility with MSVC linker.
490 if (name.startswith("__imp_")) {
491 Symbol *imp = find(name.substr(strlen("__imp_")));
492 if (imp && isa<Defined>(imp)) {
493 auto *d = cast<Defined>(imp);
494 replaceSymbol<DefinedLocalImport>(sym, name, d);
495 localImportChunks.push_back(cast<DefinedLocalImport>(sym)->getChunk());
496 localImports[sym] = d;
497 continue;
498 }
499 }
500
501 // We don't want to report missing Microsoft precompiled headers symbols.
502 // A proper message will be emitted instead in PDBLinker::aquirePrecompObj
503 if (name.contains("_PchSym_"))
504 continue;
505
506 if (config->autoImport && handleMinGWAutomaticImport(sym, name))
507 continue;
508
509 // Remaining undefined symbols are not fatal if /force is specified.
510 // They are replaced with dummy defined symbols.
511 if (config->forceUnresolved)
512 replaceSymbol<DefinedAbsolute>(sym, name, 0);
513 undefs.insert(sym);
514 }
515
516 reportProblemSymbols(
517 ctx, undefs, config->warnLocallyDefinedImported ? &localImports : nullptr,
518 false);
519}
520
521std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
522 bool inserted = false;
523 Symbol *&sym = symMap[CachedHashStringRef(name)];
524 if (!sym) {
525 sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
526 sym->isUsedInRegularObj = false;
527 sym->pendingArchiveLoad = false;
528 sym->canInline = true;
529 inserted = true;
530 }
531 return {sym, inserted};
532}
533
534std::pair<Symbol *, bool> SymbolTable::insert(StringRef name, InputFile *file) {
535 std::pair<Symbol *, bool> result = insert(name);
536 if (!file || !isa<BitcodeFile>(file))
537 result.first->isUsedInRegularObj = true;
538 return result;
539}
540
541Symbol *SymbolTable::addUndefined(StringRef name, InputFile *f,
542 bool isWeakAlias) {
543 auto [s, wasInserted] = insert(name, f);
544 if (wasInserted || (s->isLazy() && isWeakAlias)) {
545 replaceSymbol<Undefined>(s, name);
546 return s;
547 }
548 if (s->isLazy())
549 forceLazy(s);
550 return s;
551}
552
553void SymbolTable::addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym) {
554 StringRef name = sym.getName();
555 auto [s, wasInserted] = insert(name);
556 if (wasInserted) {
557 replaceSymbol<LazyArchive>(s, f, sym);
558 return;
559 }
560 auto *u = dyn_cast<Undefined>(s);
561 if (!u || u->weakAlias || s->pendingArchiveLoad)
562 return;
563 s->pendingArchiveLoad = true;
564 f->addMember(sym);
565}
566
567void SymbolTable::addLazyObject(InputFile *f, StringRef n) {
568 assert(f->lazy);
569 auto [s, wasInserted] = insert(n, f);
570 if (wasInserted) {
571 replaceSymbol<LazyObject>(s, f, n);
572 return;
573 }
574 auto *u = dyn_cast<Undefined>(s);
575 if (!u || u->weakAlias || s->pendingArchiveLoad)
576 return;
577 s->pendingArchiveLoad = true;
578 f->lazy = false;
579 addFile(f);
580}
581
582void SymbolTable::addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym,
583 StringRef n) {
584 auto [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
596static 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
605static 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
622static 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
640void 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
663Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {
664 auto [s, wasInserted] = insert(n, nullptr);
665 s->isUsedInRegularObj = true;
666 if (wasInserted || isa<Undefined>(s) || s->isLazy())
667 replaceSymbol<DefinedAbsolute>(s, n, sym);
668 else if (auto *da = dyn_cast<DefinedAbsolute>(s)) {
669 if (da->getVA() != sym.getValue())
670 reportDuplicate(s, nullptr);
671 } else if (!isa<DefinedCOFF>(s))
672 reportDuplicate(s, nullptr);
673 return s;
674}
675
676Symbol *SymbolTable::addAbsolute(StringRef n, uint64_t va) {
677 auto [s, wasInserted] = insert(n, nullptr);
678 s->isUsedInRegularObj = true;
679 if (wasInserted || isa<Undefined>(s) || s->isLazy())
680 replaceSymbol<DefinedAbsolute>(s, n, va);
681 else if (auto *da = dyn_cast<DefinedAbsolute>(s)) {
682 if (da->getVA() != va)
683 reportDuplicate(s, nullptr);
684 } else if (!isa<DefinedCOFF>(s))
685 reportDuplicate(s, nullptr);
686 return s;
687}
688
689Symbol *SymbolTable::addSynthetic(StringRef n, Chunk *c) {
690 auto [s, wasInserted] = insert(n, nullptr);
691 s->isUsedInRegularObj = true;
692 if (wasInserted || isa<Undefined>(s) || s->isLazy())
693 replaceSymbol<DefinedSynthetic>(s, n, c);
694 else if (!isa<DefinedCOFF>(s))
695 reportDuplicate(s, nullptr);
696 return s;
697}
698
699Symbol *SymbolTable::addRegular(InputFile *f, StringRef n,
700 const coff_symbol_generic *sym, SectionChunk *c,
701 uint32_t sectionOffset) {
702 auto [s, wasInserted] = insert(n, f);
703 if (wasInserted || !isa<DefinedRegular>(s))
704 replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ false,
705 /*IsExternal*/ true, sym, c);
706 else
707 reportDuplicate(s, f, c, sectionOffset);
708 return s;
709}
710
711std::pair<DefinedRegular *, bool>
712SymbolTable::addComdat(InputFile *f, StringRef n,
713 const coff_symbol_generic *sym) {
714 auto [s, wasInserted] = insert(n, f);
715 if (wasInserted || !isa<DefinedRegular>(s)) {
716 replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ true,
717 /*IsExternal*/ true, sym, nullptr);
718 return {cast<DefinedRegular>(s), true};
719 }
720 auto *existingSymbol = cast<DefinedRegular>(s);
721 if (!existingSymbol->isCOMDAT)
722 reportDuplicate(s, f);
723 return {existingSymbol, false};
724}
725
726Symbol *SymbolTable::addCommon(InputFile *f, StringRef n, uint64_t size,
727 const coff_symbol_generic *sym, CommonChunk *c) {
728 auto [s, wasInserted] = insert(n, f);
729 if (wasInserted || !isa<DefinedCOFF>(s))
730 replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
731 else if (auto *dc = dyn_cast<DefinedCommon>(s))
732 if (size > dc->getSize())
733 replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
734 return s;
735}
736
737Symbol *SymbolTable::addImportData(StringRef n, ImportFile *f) {
738 auto [s, wasInserted] = insert(n, nullptr);
739 s->isUsedInRegularObj = true;
740 if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
741 replaceSymbol<DefinedImportData>(s, n, f);
742 return s;
743 }
744
745 reportDuplicate(s, f);
746 return nullptr;
747}
748
749Symbol *SymbolTable::addImportThunk(StringRef name, DefinedImportData *id,
750 uint16_t machine) {
751 auto [s, wasInserted] = insert(name, nullptr);
752 s->isUsedInRegularObj = true;
753 if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
754 replaceSymbol<DefinedImportThunk>(s, name, id, machine);
755 return s;
756 }
757
758 reportDuplicate(s, id->file);
759 return nullptr;
760}
761
762void SymbolTable::addLibcall(StringRef name) {
763 Symbol *sym = findUnderscore(name);
764 if (!sym)
765 return;
766
767 if (auto *l = dyn_cast<LazyArchive>(sym)) {
768 MemoryBufferRef mb = l->getMemberBuffer();
769 if (isBitcode(mb))
770 addUndefined(sym->getName());
771 } else if (LazyObject *o = dyn_cast<LazyObject>(sym)) {
772 if (isBitcode(o->file->mb))
773 addUndefined(sym->getName());
774 }
775}
776
777std::vector<Chunk *> SymbolTable::getChunks() const {
778 std::vector<Chunk *> res;
779 for (ObjFile *file : ctx.objFileInstances) {
780 ArrayRef<Chunk *> v = file->getChunks();
781 res.insert(res.end(), v.begin(), v.end());
782 }
783 return res;
784}
785
786Symbol *SymbolTable::find(StringRef name) const {
787 return symMap.lookup(CachedHashStringRef(name));
788}
789
790Symbol *SymbolTable::findUnderscore(StringRef name) const {
791 if (config->machine == I386)
792 return find(("_" + name).str());
793 return find(name);
794}
795
796// Return all symbols that start with Prefix, possibly ignoring the first
797// character of Prefix or the first character symbol.
798std::vector<Symbol *> SymbolTable::getSymsWithPrefix(StringRef prefix) {
799 std::vector<Symbol *> syms;
800 for (auto pair : symMap) {
801 StringRef name = pair.first.val();
802 if (name.startswith(prefix) || name.startswith(prefix.drop_front()) ||
803 name.drop_front().startswith(prefix) ||
804 name.drop_front().startswith(prefix.drop_front())) {
805 syms.push_back(pair.second);
806 }
807 }
808 return syms;
809}
810
811Symbol *SymbolTable::findMangle(StringRef name) {
812 if (Symbol *sym = find(name))
813 if (!isa<Undefined>(sym))
814 return sym;
815
816 // Efficient fuzzy string lookup is impossible with a hash table, so iterate
817 // the symbol table once and collect all possibly matching symbols into this
818 // vector. Then compare each possibly matching symbol with each possible
819 // mangling.
820 std::vector<Symbol *> syms = getSymsWithPrefix(name);
821 auto findByPrefix = [&syms](const Twine &t) -> Symbol * {
822 std::string prefix = t.str();
823 for (auto *s : syms)
824 if (s->getName().startswith(prefix))
825 return s;
826 return nullptr;
827 };
828
829 // For non-x86, just look for C++ functions.
830 if (config->machine != I386)
831 return findByPrefix("?" + name + "@@Y");
832
833 if (!name.startswith("_"))
834 return nullptr;
835 // Search for x86 stdcall function.
836 if (Symbol *s = findByPrefix(name + "@"))
837 return s;
838 // Search for x86 fastcall function.
839 if (Symbol *s = findByPrefix("@" + name.substr(1) + "@"))
840 return s;
841 // Search for x86 vectorcall function.
842 if (Symbol *s = findByPrefix(name.substr(1) + "@@"))
843 return s;
844 // Search for x86 C++ non-member function.
845 return findByPrefix("?" + name.substr(1) + "@@Y");
846}
847
848Symbol *SymbolTable::addUndefined(StringRef name) {
849 return addUndefined(name, nullptr, false);
850}
851
852void SymbolTable::compileBitcodeFiles() {
853 if (ctx.bitcodeFileInstances.empty())
854 return;
855
856 ScopedTimer t(ctx.ltoTimer);
857 lto.reset(new BitcodeCompiler());
858 for (BitcodeFile *f : ctx.bitcodeFileInstances)
859 lto->add(*f);
860 for (InputFile *newObj : lto->compile(ctx)) {
861 ObjFile *obj = cast<ObjFile>(newObj);
862 obj->parse();
863 ctx.objFileInstances.push_back(obj);
864 }
865}
866
867} // namespace lld::coff
868

source code of lld/COFF/SymbolTable.cpp