1 //===- SymbolTable.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "SymbolTable.h"
10 #include "COFFLinkerContext.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/Support/Debug.h"
23 #include "llvm/Support/TimeProfiler.h"
24 #include "llvm/Support/raw_ostream.h"
31 StringRef
ltrim1(StringRef s
, const char *chars
) {
32 if (!s
.empty() && strchr(chars
, s
[0]))
37 static bool compatibleMachineType(COFFLinkerContext
&ctx
, MachineTypes mt
) {
38 if (mt
== IMAGE_FILE_MACHINE_UNKNOWN
)
40 switch (ctx
.config
.machine
) {
42 return mt
== ARM64
|| mt
== ARM64X
;
44 return COFF::isArm64EC(mt
) || mt
== AMD64
;
46 return COFF::isAnyArm64(mt
) || mt
== AMD64
;
48 return ctx
.config
.machine
== mt
;
52 void SymbolTable::addFile(InputFile
*file
) {
53 log("Reading " + toString(file
));
55 if (auto *f
= dyn_cast
<BitcodeFile
>(file
))
58 cast
<ObjFile
>(file
)->parseLazy();
61 if (auto *f
= dyn_cast
<ObjFile
>(file
)) {
62 ctx
.objFileInstances
.push_back(f
);
63 } else if (auto *f
= dyn_cast
<BitcodeFile
>(file
)) {
64 if (ltoCompilationDone
) {
65 error("LTO object file " + toString(file
) + " linked in after "
66 "doing LTO compilation.");
68 ctx
.bitcodeFileInstances
.push_back(f
);
69 } else if (auto *f
= dyn_cast
<ImportFile
>(file
)) {
70 ctx
.importFileInstances
.push_back(f
);
74 MachineTypes mt
= file
->getMachineType();
75 if (ctx
.config
.machine
== IMAGE_FILE_MACHINE_UNKNOWN
) {
76 ctx
.config
.machine
= mt
;
77 ctx
.driver
.addWinSysRootLibSearchPaths();
78 } else if (!compatibleMachineType(ctx
, mt
)) {
79 error(toString(file
) + ": machine type " + machineToStr(mt
) +
80 " conflicts with " + machineToStr(ctx
.config
.machine
));
84 ctx
.driver
.parseDirectives(file
);
87 static void errorOrWarn(const Twine
&s
, bool forceUnresolved
) {
94 // Causes the file associated with a lazy symbol to be linked in.
95 static void forceLazy(Symbol
*s
) {
96 s
->pendingArchiveLoad
= true;
98 case Symbol::Kind::LazyArchiveKind
: {
99 auto *l
= cast
<LazyArchive
>(s
);
100 l
->file
->addMember(l
->sym
);
103 case Symbol::Kind::LazyObjectKind
: {
104 InputFile
*file
= cast
<LazyObject
>(s
)->file
;
105 file
->ctx
.symtab
.addFile(file
);
108 case Symbol::Kind::LazyDLLSymbolKind
: {
109 auto *l
= cast
<LazyDLLSymbol
>(s
);
110 l
->file
->makeImport(l
->sym
);
115 "symbol passed to forceLazy is not a LazyArchive or LazyObject");
119 // Returns the symbol in SC whose value is <= Addr that is closest to Addr.
120 // This is generally the global variable or function whose definition contains
122 static Symbol
*getSymbol(SectionChunk
*sc
, uint32_t addr
) {
123 DefinedRegular
*candidate
= nullptr;
125 for (Symbol
*s
: sc
->file
->getSymbols()) {
126 auto *d
= dyn_cast_or_null
<DefinedRegular
>(s
);
127 if (!d
|| !d
->data
|| d
->file
!= sc
->file
|| d
->getChunk() != sc
||
128 d
->getValue() > addr
||
129 (candidate
&& d
->getValue() < candidate
->getValue()))
138 static std::vector
<std::string
> getSymbolLocations(BitcodeFile
*file
) {
139 std::string
res("\n>>> referenced by ");
140 StringRef source
= file
->obj
->getSourceFileName();
142 res
+= source
.str() + "\n>>> ";
143 res
+= toString(file
);
147 static std::optional
<std::pair
<StringRef
, uint32_t>>
148 getFileLineDwarf(const SectionChunk
*c
, uint32_t addr
) {
149 std::optional
<DILineInfo
> optionalLineInfo
=
150 c
->file
->getDILineInfo(addr
, c
->getSectionNumber() - 1);
151 if (!optionalLineInfo
)
153 const DILineInfo
&lineInfo
= *optionalLineInfo
;
154 if (lineInfo
.FileName
== DILineInfo::BadString
)
156 return std::make_pair(saver().save(lineInfo
.FileName
), lineInfo
.Line
);
159 static std::optional
<std::pair
<StringRef
, uint32_t>>
160 getFileLine(const SectionChunk
*c
, uint32_t addr
) {
161 // MinGW can optionally use codeview, even if the default is dwarf.
162 std::optional
<std::pair
<StringRef
, uint32_t>> fileLine
=
163 getFileLineCodeView(c
, addr
);
164 // If codeview didn't yield any result, check dwarf in MinGW mode.
165 if (!fileLine
&& c
->file
->ctx
.config
.mingw
)
166 fileLine
= getFileLineDwarf(c
, addr
);
170 // Given a file and the index of a symbol in that file, returns a description
171 // of all references to that symbol from that file. If no debug information is
172 // available, returns just the name of the file, else one string per actual
173 // reference as described in the debug info.
174 // Returns up to maxStrings string descriptions, along with the total number of
176 static std::pair
<std::vector
<std::string
>, size_t>
177 getSymbolLocations(ObjFile
*file
, uint32_t symIndex
, size_t maxStrings
) {
180 std::pair
<StringRef
, uint32_t> fileLine
;
182 std::vector
<Location
> locations
;
183 size_t numLocations
= 0;
185 for (Chunk
*c
: file
->getChunks()) {
186 auto *sc
= dyn_cast
<SectionChunk
>(c
);
189 for (const coff_relocation
&r
: sc
->getRelocs()) {
190 if (r
.SymbolTableIndex
!= symIndex
)
193 if (locations
.size() >= maxStrings
)
196 std::optional
<std::pair
<StringRef
, uint32_t>> fileLine
=
197 getFileLine(sc
, r
.VirtualAddress
);
198 Symbol
*sym
= getSymbol(sc
, r
.VirtualAddress
);
200 locations
.push_back({sym
, *fileLine
});
202 locations
.push_back({sym
, {"", 0}});
207 return std::make_pair(std::vector
<std::string
>(), numLocations
);
209 if (numLocations
== 0)
210 return std::make_pair(
211 std::vector
<std::string
>{"\n>>> referenced by " + toString(file
)}, 1);
213 std::vector
<std::string
> symbolLocations(locations
.size());
215 for (Location loc
: locations
) {
216 llvm::raw_string_ostream
os(symbolLocations
[i
++]);
217 os
<< "\n>>> referenced by ";
218 if (!loc
.fileLine
.first
.empty())
219 os
<< loc
.fileLine
.first
<< ":" << loc
.fileLine
.second
221 os
<< toString(file
);
223 os
<< ":(" << toString(file
->ctx
, *loc
.sym
) << ')';
225 return std::make_pair(symbolLocations
, numLocations
);
228 std::vector
<std::string
> getSymbolLocations(ObjFile
*file
, uint32_t symIndex
) {
229 return getSymbolLocations(file
, symIndex
, SIZE_MAX
).first
;
232 static std::pair
<std::vector
<std::string
>, size_t>
233 getSymbolLocations(InputFile
*file
, uint32_t symIndex
, size_t maxStrings
) {
234 if (auto *o
= dyn_cast
<ObjFile
>(file
))
235 return getSymbolLocations(o
, symIndex
, maxStrings
);
236 if (auto *b
= dyn_cast
<BitcodeFile
>(file
)) {
237 std::vector
<std::string
> symbolLocations
= getSymbolLocations(b
);
238 size_t numLocations
= symbolLocations
.size();
239 if (symbolLocations
.size() > maxStrings
)
240 symbolLocations
.resize(maxStrings
);
241 return std::make_pair(symbolLocations
, numLocations
);
243 llvm_unreachable("unsupported file type passed to getSymbolLocations");
244 return std::make_pair(std::vector
<std::string
>(), (size_t)0);
247 // For an undefined symbol, stores all files referencing it and the index of
248 // the undefined symbol in each file.
249 struct UndefinedDiag
{
255 std::vector
<File
> files
;
258 static void reportUndefinedSymbol(const COFFLinkerContext
&ctx
,
259 const UndefinedDiag
&undefDiag
) {
261 llvm::raw_string_ostream
os(out
);
262 os
<< "undefined symbol: " << toString(ctx
, *undefDiag
.sym
);
264 const size_t maxUndefReferences
= 3;
265 size_t numDisplayedRefs
= 0, numRefs
= 0;
266 for (const UndefinedDiag::File
&ref
: undefDiag
.files
) {
267 auto [symbolLocations
, totalLocations
] = getSymbolLocations(
268 ref
.file
, ref
.symIndex
, maxUndefReferences
- numDisplayedRefs
);
270 numRefs
+= totalLocations
;
271 numDisplayedRefs
+= symbolLocations
.size();
272 for (const std::string
&s
: symbolLocations
) {
276 if (numDisplayedRefs
< numRefs
)
277 os
<< "\n>>> referenced " << numRefs
- numDisplayedRefs
<< " more times";
278 errorOrWarn(os
.str(), ctx
.config
.forceUnresolved
);
281 void SymbolTable::loadMinGWSymbols() {
282 for (auto &i
: symMap
) {
283 Symbol
*sym
= i
.second
;
284 auto *undef
= dyn_cast
<Undefined
>(sym
);
287 if (undef
->getWeakAlias())
290 StringRef name
= undef
->getName();
292 if (ctx
.config
.machine
== I386
&& ctx
.config
.stdcallFixup
) {
293 // Check if we can resolve an undefined decorated symbol by finding
294 // the intended target as an undecorated symbol (only with a leading
296 StringRef origName
= name
;
297 StringRef baseName
= name
;
298 // Trim down stdcall/fastcall/vectorcall symbols to the base name.
299 baseName
= ltrim1(baseName
, "_@");
300 baseName
= baseName
.substr(0, baseName
.find('@'));
301 // Add a leading underscore, as it would be in cdecl form.
302 std::string newName
= ("_" + baseName
).str();
304 if (newName
!= origName
&& (l
= find(newName
)) != nullptr) {
305 // If we found a symbol and it is lazy; load it.
306 if (l
->isLazy() && !l
->pendingArchiveLoad
) {
307 log("Loading lazy " + l
->getName() + " from " +
308 l
->getFile()->getName() + " for stdcall fixup");
311 // If it's lazy or already defined, hook it up as weak alias.
312 if (l
->isLazy() || isa
<Defined
>(l
)) {
313 if (ctx
.config
.warnStdcallFixup
)
314 warn("Resolving " + origName
+ " by linking to " + newName
);
316 log("Resolving " + origName
+ " by linking to " + newName
);
317 undef
->weakAlias
= l
;
323 if (ctx
.config
.autoImport
) {
324 if (name
.starts_with("__imp_"))
326 // If we have an undefined symbol, but we have a lazy symbol we could
328 Symbol
*l
= find(("__imp_" + name
).str());
329 if (!l
|| l
->pendingArchiveLoad
|| !l
->isLazy())
332 log("Loading lazy " + l
->getName() + " from " + l
->getFile()->getName() +
333 " for automatic import");
339 Defined
*SymbolTable::impSymbol(StringRef name
) {
340 if (name
.starts_with("__imp_"))
342 return dyn_cast_or_null
<Defined
>(find(("__imp_" + name
).str()));
345 bool SymbolTable::handleMinGWAutomaticImport(Symbol
*sym
, StringRef name
) {
346 Defined
*imp
= impSymbol(name
);
350 // Replace the reference directly to a variable with a reference
351 // to the import address table instead. This obviously isn't right,
352 // but we mark the symbol as isRuntimePseudoReloc, and a later pass
353 // will add runtime pseudo relocations for every relocation against
354 // this Symbol. The runtime pseudo relocation framework expects the
355 // reference itself to point at the IAT entry.
357 if (isa
<DefinedImportData
>(imp
)) {
358 log("Automatically importing " + name
+ " from " +
359 cast
<DefinedImportData
>(imp
)->getDLLName());
360 impSize
= sizeof(DefinedImportData
);
361 } else if (isa
<DefinedRegular
>(imp
)) {
362 log("Automatically importing " + name
+ " from " +
363 toString(cast
<DefinedRegular
>(imp
)->file
));
364 impSize
= sizeof(DefinedRegular
);
366 warn("unable to automatically import " + name
+ " from " + imp
->getName() +
367 " from " + toString(cast
<DefinedRegular
>(imp
)->file
) +
368 "; unexpected symbol type");
371 sym
->replaceKeepingName(imp
, impSize
);
372 sym
->isRuntimePseudoReloc
= true;
374 // There may exist symbols named .refptr.<name> which only consist
375 // of a single pointer to <name>. If it turns out <name> is
376 // automatically imported, we don't need to keep the .refptr.<name>
377 // pointer at all, but redirect all accesses to it to the IAT entry
378 // for __imp_<name> instead, and drop the whole .refptr.<name> chunk.
379 DefinedRegular
*refptr
=
380 dyn_cast_or_null
<DefinedRegular
>(find((".refptr." + name
).str()));
381 if (refptr
&& refptr
->getChunk()->getSize() == ctx
.config
.wordsize
) {
382 SectionChunk
*sc
= dyn_cast_or_null
<SectionChunk
>(refptr
->getChunk());
383 if (sc
&& sc
->getRelocs().size() == 1 && *sc
->symbols().begin() == sym
) {
384 log("Replacing .refptr." + name
+ " with " + imp
->getName());
385 refptr
->getChunk()->live
= false;
386 refptr
->replaceKeepingName(imp
, impSize
);
392 /// Helper function for reportUnresolvable and resolveRemainingUndefines.
393 /// This function emits an "undefined symbol" diagnostic for each symbol in
394 /// undefs. If localImports is not nullptr, it also emits a "locally
395 /// defined symbol imported" diagnostic for symbols in localImports.
396 /// objFiles and bitcodeFiles (if not nullptr) are used to report where
397 /// undefined symbols are referenced.
398 static void reportProblemSymbols(
399 const COFFLinkerContext
&ctx
, const SmallPtrSetImpl
<Symbol
*> &undefs
,
400 const DenseMap
<Symbol
*, Symbol
*> *localImports
, bool needBitcodeFiles
) {
401 // Return early if there is nothing to report (which should be
403 if (undefs
.empty() && (!localImports
|| localImports
->empty()))
406 for (Symbol
*b
: ctx
.config
.gcroot
) {
408 errorOrWarn("<root>: undefined symbol: " + toString(ctx
, *b
),
409 ctx
.config
.forceUnresolved
);
411 if (Symbol
*imp
= localImports
->lookup(b
))
412 warn("<root>: locally defined symbol imported: " + toString(ctx
, *imp
) +
413 " (defined in " + toString(imp
->getFile()) + ") [LNK4217]");
416 std::vector
<UndefinedDiag
> undefDiags
;
417 DenseMap
<Symbol
*, int> firstDiag
;
419 auto processFile
= [&](InputFile
*file
, ArrayRef
<Symbol
*> symbols
) {
420 uint32_t symIndex
= (uint32_t)-1;
421 for (Symbol
*sym
: symbols
) {
425 if (undefs
.count(sym
)) {
426 auto it
= firstDiag
.find(sym
);
427 if (it
== firstDiag
.end()) {
428 firstDiag
[sym
] = undefDiags
.size();
429 undefDiags
.push_back({sym
, {{file
, symIndex
}}});
431 undefDiags
[it
->second
].files
.push_back({file
, symIndex
});
435 if (Symbol
*imp
= localImports
->lookup(sym
))
436 warn(toString(file
) +
437 ": locally defined symbol imported: " + toString(ctx
, *imp
) +
438 " (defined in " + toString(imp
->getFile()) + ") [LNK4217]");
442 for (ObjFile
*file
: ctx
.objFileInstances
)
443 processFile(file
, file
->getSymbols());
445 if (needBitcodeFiles
)
446 for (BitcodeFile
*file
: ctx
.bitcodeFileInstances
)
447 processFile(file
, file
->getSymbols());
449 for (const UndefinedDiag
&undefDiag
: undefDiags
)
450 reportUndefinedSymbol(ctx
, undefDiag
);
453 void SymbolTable::reportUnresolvable() {
454 SmallPtrSet
<Symbol
*, 8> undefs
;
455 for (auto &i
: symMap
) {
456 Symbol
*sym
= i
.second
;
457 auto *undef
= dyn_cast
<Undefined
>(sym
);
458 if (!undef
|| sym
->deferUndefined
)
460 if (undef
->getWeakAlias())
462 StringRef name
= undef
->getName();
463 if (name
.starts_with("__imp_")) {
464 Symbol
*imp
= find(name
.substr(strlen("__imp_")));
465 if (Defined
*def
= dyn_cast_or_null
<Defined
>(imp
)) {
466 def
->isUsedInRegularObj
= true;
470 if (name
.contains("_PchSym_"))
472 if (ctx
.config
.autoImport
&& impSymbol(name
))
477 reportProblemSymbols(ctx
, undefs
,
478 /* localImports */ nullptr, true);
481 void SymbolTable::resolveRemainingUndefines() {
482 llvm::TimeTraceScope
timeScope("Resolve remaining undefined symbols");
483 SmallPtrSet
<Symbol
*, 8> undefs
;
484 DenseMap
<Symbol
*, Symbol
*> localImports
;
486 for (auto &i
: symMap
) {
487 Symbol
*sym
= i
.second
;
488 auto *undef
= dyn_cast
<Undefined
>(sym
);
491 if (!sym
->isUsedInRegularObj
)
494 StringRef name
= undef
->getName();
496 // A weak alias may have been resolved, so check for that.
497 if (Defined
*d
= undef
->getWeakAlias()) {
498 // We want to replace Sym with D. However, we can't just blindly
499 // copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
500 // internal symbol, and internal symbols are stored as "unparented"
501 // Symbols. For that reason we need to check which type of symbol we
502 // are dealing with and copy the correct number of bytes.
503 if (isa
<DefinedRegular
>(d
))
504 memcpy(sym
, d
, sizeof(DefinedRegular
));
505 else if (isa
<DefinedAbsolute
>(d
))
506 memcpy(sym
, d
, sizeof(DefinedAbsolute
));
508 memcpy(sym
, d
, sizeof(SymbolUnion
));
512 // If we can resolve a symbol by removing __imp_ prefix, do that.
513 // This odd rule is for compatibility with MSVC linker.
514 if (name
.starts_with("__imp_")) {
515 Symbol
*imp
= find(name
.substr(strlen("__imp_")));
516 if (imp
&& isa
<Defined
>(imp
)) {
517 auto *d
= cast
<Defined
>(imp
);
518 replaceSymbol
<DefinedLocalImport
>(sym
, ctx
, name
, d
);
519 localImportChunks
.push_back(cast
<DefinedLocalImport
>(sym
)->getChunk());
520 localImports
[sym
] = d
;
525 // We don't want to report missing Microsoft precompiled headers symbols.
526 // A proper message will be emitted instead in PDBLinker::aquirePrecompObj
527 if (name
.contains("_PchSym_"))
530 if (ctx
.config
.autoImport
&& handleMinGWAutomaticImport(sym
, name
))
533 // Remaining undefined symbols are not fatal if /force is specified.
534 // They are replaced with dummy defined symbols.
535 if (ctx
.config
.forceUnresolved
)
536 replaceSymbol
<DefinedAbsolute
>(sym
, ctx
, name
, 0);
540 reportProblemSymbols(
542 ctx
.config
.warnLocallyDefinedImported
? &localImports
: nullptr, false);
545 std::pair
<Symbol
*, bool> SymbolTable::insert(StringRef name
) {
546 bool inserted
= false;
547 Symbol
*&sym
= symMap
[CachedHashStringRef(name
)];
549 sym
= reinterpret_cast<Symbol
*>(make
<SymbolUnion
>());
550 sym
->isUsedInRegularObj
= false;
551 sym
->pendingArchiveLoad
= false;
552 sym
->canInline
= true;
555 return {sym
, inserted
};
558 std::pair
<Symbol
*, bool> SymbolTable::insert(StringRef name
, InputFile
*file
) {
559 std::pair
<Symbol
*, bool> result
= insert(name
);
560 if (!file
|| !isa
<BitcodeFile
>(file
))
561 result
.first
->isUsedInRegularObj
= true;
565 void SymbolTable::addEntryThunk(Symbol
*from
, Symbol
*to
) {
566 entryThunks
.push_back({from
, to
});
569 void SymbolTable::initializeEntryThunks() {
570 for (auto it
: entryThunks
) {
571 auto *to
= dyn_cast
<Defined
>(it
.second
);
574 auto *from
= dyn_cast
<DefinedRegular
>(it
.first
);
575 // We need to be able to add padding to the function and fill it with an
576 // offset to its entry thunks. To ensure that padding the function is
577 // feasible, functions are required to be COMDAT symbols with no offset.
578 if (!from
|| !from
->getChunk()->isCOMDAT() ||
579 cast
<DefinedRegular
>(from
)->getValue()) {
580 error("non COMDAT symbol '" + from
->getName() + "' in hybrid map");
583 from
->getChunk()->setEntryThunk(to
);
587 Symbol
*SymbolTable::addUndefined(StringRef name
, InputFile
*f
,
589 auto [s
, wasInserted
] = insert(name
, f
);
590 if (wasInserted
|| (s
->isLazy() && isWeakAlias
)) {
591 replaceSymbol
<Undefined
>(s
, name
);
599 void SymbolTable::addLazyArchive(ArchiveFile
*f
, const Archive::Symbol
&sym
) {
600 StringRef name
= sym
.getName();
601 auto [s
, wasInserted
] = insert(name
);
603 replaceSymbol
<LazyArchive
>(s
, f
, sym
);
606 auto *u
= dyn_cast
<Undefined
>(s
);
607 if (!u
|| u
->weakAlias
|| s
->pendingArchiveLoad
)
609 s
->pendingArchiveLoad
= true;
613 void SymbolTable::addLazyObject(InputFile
*f
, StringRef n
) {
615 auto [s
, wasInserted
] = insert(n
, f
);
617 replaceSymbol
<LazyObject
>(s
, f
, n
);
620 auto *u
= dyn_cast
<Undefined
>(s
);
621 if (!u
|| u
->weakAlias
|| s
->pendingArchiveLoad
)
623 s
->pendingArchiveLoad
= true;
628 void SymbolTable::addLazyDLLSymbol(DLLFile
*f
, DLLFile::Symbol
*sym
,
630 auto [s
, wasInserted
] = insert(n
);
632 replaceSymbol
<LazyDLLSymbol
>(s
, f
, sym
, n
);
635 auto *u
= dyn_cast
<Undefined
>(s
);
636 if (!u
|| u
->weakAlias
|| s
->pendingArchiveLoad
)
638 s
->pendingArchiveLoad
= true;
642 static std::string
getSourceLocationBitcode(BitcodeFile
*file
) {
643 std::string
res("\n>>> defined at ");
644 StringRef source
= file
->obj
->getSourceFileName();
646 res
+= source
.str() + "\n>>> ";
647 res
+= toString(file
);
651 static std::string
getSourceLocationObj(ObjFile
*file
, SectionChunk
*sc
,
652 uint32_t offset
, StringRef name
) {
653 std::optional
<std::pair
<StringRef
, uint32_t>> fileLine
;
655 fileLine
= getFileLine(sc
, offset
);
657 fileLine
= file
->getVariableLocation(name
);
660 llvm::raw_string_ostream
os(res
);
661 os
<< "\n>>> defined at ";
663 os
<< fileLine
->first
<< ":" << fileLine
->second
<< "\n>>> ";
664 os
<< toString(file
);
668 static std::string
getSourceLocation(InputFile
*file
, SectionChunk
*sc
,
669 uint32_t offset
, StringRef name
) {
672 if (auto *o
= dyn_cast
<ObjFile
>(file
))
673 return getSourceLocationObj(o
, sc
, offset
, name
);
674 if (auto *b
= dyn_cast
<BitcodeFile
>(file
))
675 return getSourceLocationBitcode(b
);
676 return "\n>>> defined at " + toString(file
);
679 // Construct and print an error message in the form of:
681 // lld-link: error: duplicate symbol: foo
682 // >>> defined at bar.c:30
684 // >>> defined at baz.c:563
686 void SymbolTable::reportDuplicate(Symbol
*existing
, InputFile
*newFile
,
688 uint32_t newSectionOffset
) {
690 llvm::raw_string_ostream
os(msg
);
691 os
<< "duplicate symbol: " << toString(ctx
, *existing
);
693 DefinedRegular
*d
= dyn_cast
<DefinedRegular
>(existing
);
694 if (d
&& isa
<ObjFile
>(d
->getFile())) {
695 os
<< getSourceLocation(d
->getFile(), d
->getChunk(), d
->getValue(),
696 existing
->getName());
698 os
<< getSourceLocation(existing
->getFile(), nullptr, 0, "");
700 os
<< getSourceLocation(newFile
, newSc
, newSectionOffset
,
701 existing
->getName());
703 if (ctx
.config
.forceMultiple
)
709 Symbol
*SymbolTable::addAbsolute(StringRef n
, COFFSymbolRef sym
) {
710 auto [s
, wasInserted
] = insert(n
, nullptr);
711 s
->isUsedInRegularObj
= true;
712 if (wasInserted
|| isa
<Undefined
>(s
) || s
->isLazy())
713 replaceSymbol
<DefinedAbsolute
>(s
, ctx
, n
, sym
);
714 else if (auto *da
= dyn_cast
<DefinedAbsolute
>(s
)) {
715 if (da
->getVA() != sym
.getValue())
716 reportDuplicate(s
, nullptr);
717 } else if (!isa
<DefinedCOFF
>(s
))
718 reportDuplicate(s
, nullptr);
722 Symbol
*SymbolTable::addAbsolute(StringRef n
, uint64_t va
) {
723 auto [s
, wasInserted
] = insert(n
, nullptr);
724 s
->isUsedInRegularObj
= true;
725 if (wasInserted
|| isa
<Undefined
>(s
) || s
->isLazy())
726 replaceSymbol
<DefinedAbsolute
>(s
, ctx
, n
, va
);
727 else if (auto *da
= dyn_cast
<DefinedAbsolute
>(s
)) {
728 if (da
->getVA() != va
)
729 reportDuplicate(s
, nullptr);
730 } else if (!isa
<DefinedCOFF
>(s
))
731 reportDuplicate(s
, nullptr);
735 Symbol
*SymbolTable::addSynthetic(StringRef n
, Chunk
*c
) {
736 auto [s
, wasInserted
] = insert(n
, nullptr);
737 s
->isUsedInRegularObj
= true;
738 if (wasInserted
|| isa
<Undefined
>(s
) || s
->isLazy())
739 replaceSymbol
<DefinedSynthetic
>(s
, n
, c
);
740 else if (!isa
<DefinedCOFF
>(s
))
741 reportDuplicate(s
, nullptr);
745 Symbol
*SymbolTable::addRegular(InputFile
*f
, StringRef n
,
746 const coff_symbol_generic
*sym
, SectionChunk
*c
,
747 uint32_t sectionOffset
, bool isWeak
) {
748 auto [s
, wasInserted
] = insert(n
, f
);
749 if (wasInserted
|| !isa
<DefinedRegular
>(s
) || s
->isWeak
)
750 replaceSymbol
<DefinedRegular
>(s
, f
, n
, /*IsCOMDAT*/ false,
751 /*IsExternal*/ true, sym
, c
, isWeak
);
753 reportDuplicate(s
, f
, c
, sectionOffset
);
757 std::pair
<DefinedRegular
*, bool>
758 SymbolTable::addComdat(InputFile
*f
, StringRef n
,
759 const coff_symbol_generic
*sym
) {
760 auto [s
, wasInserted
] = insert(n
, f
);
761 if (wasInserted
|| !isa
<DefinedRegular
>(s
)) {
762 replaceSymbol
<DefinedRegular
>(s
, f
, n
, /*IsCOMDAT*/ true,
763 /*IsExternal*/ true, sym
, nullptr);
764 return {cast
<DefinedRegular
>(s
), true};
766 auto *existingSymbol
= cast
<DefinedRegular
>(s
);
767 if (!existingSymbol
->isCOMDAT
)
768 reportDuplicate(s
, f
);
769 return {existingSymbol
, false};
772 Symbol
*SymbolTable::addCommon(InputFile
*f
, StringRef n
, uint64_t size
,
773 const coff_symbol_generic
*sym
, CommonChunk
*c
) {
774 auto [s
, wasInserted
] = insert(n
, f
);
775 if (wasInserted
|| !isa
<DefinedCOFF
>(s
))
776 replaceSymbol
<DefinedCommon
>(s
, f
, n
, size
, sym
, c
);
777 else if (auto *dc
= dyn_cast
<DefinedCommon
>(s
))
778 if (size
> dc
->getSize())
779 replaceSymbol
<DefinedCommon
>(s
, f
, n
, size
, sym
, c
);
783 Symbol
*SymbolTable::addImportData(StringRef n
, ImportFile
*f
) {
784 auto [s
, wasInserted
] = insert(n
, nullptr);
785 s
->isUsedInRegularObj
= true;
786 if (wasInserted
|| isa
<Undefined
>(s
) || s
->isLazy()) {
787 replaceSymbol
<DefinedImportData
>(s
, n
, f
);
791 reportDuplicate(s
, f
);
795 Symbol
*SymbolTable::addImportThunk(StringRef name
, DefinedImportData
*id
,
797 auto [s
, wasInserted
] = insert(name
, nullptr);
798 s
->isUsedInRegularObj
= true;
799 if (wasInserted
|| isa
<Undefined
>(s
) || s
->isLazy()) {
800 replaceSymbol
<DefinedImportThunk
>(s
, ctx
, name
, id
, machine
);
804 reportDuplicate(s
, id
->file
);
808 void SymbolTable::addLibcall(StringRef name
) {
809 Symbol
*sym
= findUnderscore(name
);
813 if (auto *l
= dyn_cast
<LazyArchive
>(sym
)) {
814 MemoryBufferRef mb
= l
->getMemberBuffer();
816 addUndefined(sym
->getName());
817 } else if (LazyObject
*o
= dyn_cast
<LazyObject
>(sym
)) {
818 if (isBitcode(o
->file
->mb
))
819 addUndefined(sym
->getName());
823 std::vector
<Chunk
*> SymbolTable::getChunks() const {
824 std::vector
<Chunk
*> res
;
825 for (ObjFile
*file
: ctx
.objFileInstances
) {
826 ArrayRef
<Chunk
*> v
= file
->getChunks();
827 res
.insert(res
.end(), v
.begin(), v
.end());
832 Symbol
*SymbolTable::find(StringRef name
) const {
833 return symMap
.lookup(CachedHashStringRef(name
));
836 Symbol
*SymbolTable::findUnderscore(StringRef name
) const {
837 if (ctx
.config
.machine
== I386
)
838 return find(("_" + name
).str());
842 // Return all symbols that start with Prefix, possibly ignoring the first
843 // character of Prefix or the first character symbol.
844 std::vector
<Symbol
*> SymbolTable::getSymsWithPrefix(StringRef prefix
) {
845 std::vector
<Symbol
*> syms
;
846 for (auto pair
: symMap
) {
847 StringRef name
= pair
.first
.val();
848 if (name
.starts_with(prefix
) || name
.starts_with(prefix
.drop_front()) ||
849 name
.drop_front().starts_with(prefix
) ||
850 name
.drop_front().starts_with(prefix
.drop_front())) {
851 syms
.push_back(pair
.second
);
857 Symbol
*SymbolTable::findMangle(StringRef name
) {
858 if (Symbol
*sym
= find(name
)) {
859 if (auto *u
= dyn_cast
<Undefined
>(sym
)) {
860 // We're specifically looking for weak aliases that ultimately resolve to
861 // defined symbols, hence the call to getWeakAlias() instead of just using
862 // the weakAlias member variable. This matches link.exe's behavior.
863 if (Symbol
*weakAlias
= u
->getWeakAlias())
870 // Efficient fuzzy string lookup is impossible with a hash table, so iterate
871 // the symbol table once and collect all possibly matching symbols into this
872 // vector. Then compare each possibly matching symbol with each possible
874 std::vector
<Symbol
*> syms
= getSymsWithPrefix(name
);
875 auto findByPrefix
= [&syms
](const Twine
&t
) -> Symbol
* {
876 std::string prefix
= t
.str();
878 if (s
->getName().starts_with(prefix
))
883 // For non-x86, just look for C++ functions.
884 if (ctx
.config
.machine
!= I386
)
885 return findByPrefix("?" + name
+ "@@Y");
887 if (!name
.starts_with("_"))
889 // Search for x86 stdcall function.
890 if (Symbol
*s
= findByPrefix(name
+ "@"))
892 // Search for x86 fastcall function.
893 if (Symbol
*s
= findByPrefix("@" + name
.substr(1) + "@"))
895 // Search for x86 vectorcall function.
896 if (Symbol
*s
= findByPrefix(name
.substr(1) + "@@"))
898 // Search for x86 C++ non-member function.
899 return findByPrefix("?" + name
.substr(1) + "@@Y");
902 Symbol
*SymbolTable::addUndefined(StringRef name
) {
903 return addUndefined(name
, nullptr, false);
906 void SymbolTable::compileBitcodeFiles() {
907 ltoCompilationDone
= true;
908 if (ctx
.bitcodeFileInstances
.empty())
911 llvm::TimeTraceScope
timeScope("Compile bitcode");
912 ScopedTimer
t(ctx
.ltoTimer
);
913 lto
.reset(new BitcodeCompiler(ctx
));
914 for (BitcodeFile
*f
: ctx
.bitcodeFileInstances
)
916 for (InputFile
*newObj
: lto
->compile()) {
917 ObjFile
*obj
= cast
<ObjFile
>(newObj
);
919 ctx
.objFileInstances
.push_back(obj
);
923 } // namespace lld::coff