1 //===- SymbolTable.cpp ----------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "SymbolTable.h"
16 #include "lld/Common/ErrorHandler.h"
17 #include "lld/Common/Memory.h"
18 #include "lld/Common/Timer.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
29 static Timer LTOTimer("LTO", Timer::root());
33 void SymbolTable::addFile(InputFile *File) {
34 log("Reading " + toString(File));
37 MachineTypes MT = File->getMachineType();
38 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
40 } else if (MT != IMAGE_FILE_MACHINE_UNKNOWN && Config->Machine != MT) {
41 error(toString(File) + ": machine type " + machineToStr(MT) +
42 " conflicts with " + machineToStr(Config->Machine));
46 if (auto *F = dyn_cast<ObjFile>(File)) {
47 ObjFile::Instances.push_back(F);
48 } else if (auto *F = dyn_cast<BitcodeFile>(File)) {
49 BitcodeFile::Instances.push_back(F);
50 } else if (auto *F = dyn_cast<ImportFile>(File)) {
51 ImportFile::Instances.push_back(F);
54 StringRef S = File->getDirectives();
58 log("Directives: " + toString(File) + ": " + S);
59 Driver->parseDirectives(S);
62 static void errorOrWarn(const Twine &S) {
63 if (Config->ForceUnresolved)
69 // Returns the symbol in SC whose value is <= Addr that is closest to Addr.
70 // This is generally the global variable or function whose definition contains
72 static Symbol *getSymbol(SectionChunk *SC, uint32_t Addr) {
73 DefinedRegular *Candidate = nullptr;
75 for (Symbol *S : SC->File->getSymbols()) {
76 auto *D = dyn_cast_or_null<DefinedRegular>(S);
77 if (!D || D->getChunk() != SC || D->getValue() > Addr ||
78 (Candidate && D->getValue() < Candidate->getValue()))
87 std::string getSymbolLocations(ObjFile *File, uint32_t SymIndex) {
90 std::pair<StringRef, uint32_t> FileLine;
92 std::vector<Location> Locations;
94 for (Chunk *C : File->getChunks()) {
95 auto *SC = dyn_cast<SectionChunk>(C);
98 for (const coff_relocation &R : SC->Relocs) {
99 if (R.SymbolTableIndex != SymIndex)
101 std::pair<StringRef, uint32_t> FileLine =
102 getFileLine(SC, R.VirtualAddress);
103 Symbol *Sym = getSymbol(SC, R.VirtualAddress);
104 if (!FileLine.first.empty() || Sym)
105 Locations.push_back({Sym, FileLine});
109 if (Locations.empty())
110 return "\n>>> referenced by " + toString(File);
113 llvm::raw_string_ostream OS(Out);
114 for (Location Loc : Locations) {
115 OS << "\n>>> referenced by ";
116 if (!Loc.FileLine.first.empty())
117 OS << Loc.FileLine.first << ":" << Loc.FileLine.second
119 OS << toString(File);
121 OS << ":(" << toString(*Loc.Sym) << ')';
126 void SymbolTable::loadMinGWAutomaticImports() {
127 for (auto &I : SymMap) {
128 Symbol *Sym = I.second;
129 auto *Undef = dyn_cast<Undefined>(Sym);
132 if (!Sym->IsUsedInRegularObj)
135 StringRef Name = Undef->getName();
137 if (Name.startswith("__imp_"))
139 // If we have an undefined symbol, but we have a Lazy representing a
140 // symbol we could load from file, make sure to load that.
141 Lazy *L = dyn_cast_or_null<Lazy>(find(("__imp_" + Name).str()));
142 if (!L || L->PendingArchiveLoad)
145 log("Loading lazy " + L->getName() + " from " + L->File->getName() +
146 " for automatic import");
147 L->PendingArchiveLoad = true;
148 L->File->addMember(&L->Sym);
152 bool SymbolTable::handleMinGWAutomaticImport(Symbol *Sym, StringRef Name) {
153 if (Name.startswith("__imp_"))
155 Defined *Imp = dyn_cast_or_null<Defined>(find(("__imp_" + Name).str()));
159 // Replace the reference directly to a variable with a reference
160 // to the import address table instead. This obviously isn't right,
161 // but we mark the symbol as IsRuntimePseudoReloc, and a later pass
162 // will add runtime pseudo relocations for every relocation against
163 // this Symbol. The runtime pseudo relocation framework expects the
164 // reference itself to point at the IAT entry.
166 if (isa<DefinedImportData>(Imp)) {
167 log("Automatically importing " + Name + " from " +
168 cast<DefinedImportData>(Imp)->getDLLName());
169 ImpSize = sizeof(DefinedImportData);
170 } else if (isa<DefinedRegular>(Imp)) {
171 log("Automatically importing " + Name + " from " +
172 toString(cast<DefinedRegular>(Imp)->File));
173 ImpSize = sizeof(DefinedRegular);
175 warn("unable to automatically import " + Name + " from " + Imp->getName() +
176 " from " + toString(cast<DefinedRegular>(Imp)->File) +
177 "; unexpected symbol type");
180 Sym->replaceKeepingName(Imp, ImpSize);
181 Sym->IsRuntimePseudoReloc = true;
183 // There may exist symbols named .refptr.<name> which only consist
184 // of a single pointer to <name>. If it turns out <name> is
185 // automatically imported, we don't need to keep the .refptr.<name>
186 // pointer at all, but redirect all accesses to it to the IAT entry
187 // for __imp_<name> instead, and drop the whole .refptr.<name> chunk.
188 DefinedRegular *Refptr =
189 dyn_cast_or_null<DefinedRegular>(find((".refptr." + Name).str()));
190 if (Refptr && Refptr->getChunk()->getSize() == Config->Wordsize) {
191 SectionChunk *SC = dyn_cast_or_null<SectionChunk>(Refptr->getChunk());
192 if (SC && SC->Relocs.size() == 1 && *SC->symbols().begin() == Sym) {
193 log("Replacing .refptr." + Name + " with " + Imp->getName());
194 Refptr->getChunk()->Live = false;
195 Refptr->replaceKeepingName(Imp, ImpSize);
201 void SymbolTable::reportRemainingUndefines() {
202 SmallPtrSet<Symbol *, 8> Undefs;
203 DenseMap<Symbol *, Symbol *> LocalImports;
205 for (auto &I : SymMap) {
206 Symbol *Sym = I.second;
207 auto *Undef = dyn_cast<Undefined>(Sym);
210 if (!Sym->IsUsedInRegularObj)
213 StringRef Name = Undef->getName();
215 // A weak alias may have been resolved, so check for that.
216 if (Defined *D = Undef->getWeakAlias()) {
217 // We want to replace Sym with D. However, we can't just blindly
218 // copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
219 // internal symbol, and internal symbols are stored as "unparented"
220 // Symbols. For that reason we need to check which type of symbol we
221 // are dealing with and copy the correct number of bytes.
222 if (isa<DefinedRegular>(D))
223 memcpy(Sym, D, sizeof(DefinedRegular));
224 else if (isa<DefinedAbsolute>(D))
225 memcpy(Sym, D, sizeof(DefinedAbsolute));
227 memcpy(Sym, D, sizeof(SymbolUnion));
231 // If we can resolve a symbol by removing __imp_ prefix, do that.
232 // This odd rule is for compatibility with MSVC linker.
233 if (Name.startswith("__imp_")) {
234 Symbol *Imp = find(Name.substr(strlen("__imp_")));
235 if (Imp && isa<Defined>(Imp)) {
236 auto *D = cast<Defined>(Imp);
237 replaceSymbol<DefinedLocalImport>(Sym, Name, D);
238 LocalImportChunks.push_back(cast<DefinedLocalImport>(Sym)->getChunk());
239 LocalImports[Sym] = D;
244 // We don't want to report missing Microsoft precompiled headers symbols.
245 // A proper message will be emitted instead in PDBLinker::aquirePrecompObj
246 if (Name.contains("_PchSym_"))
249 if (Config->MinGW && handleMinGWAutomaticImport(Sym, Name))
252 // Remaining undefined symbols are not fatal if /force is specified.
253 // They are replaced with dummy defined symbols.
254 if (Config->ForceUnresolved)
255 replaceSymbol<DefinedAbsolute>(Sym, Name, 0);
259 if (Undefs.empty() && LocalImports.empty())
262 for (Symbol *B : Config->GCRoot) {
264 errorOrWarn("<root>: undefined symbol: " + toString(*B));
265 if (Config->WarnLocallyDefinedImported)
266 if (Symbol *Imp = LocalImports.lookup(B))
267 warn("<root>: locally defined symbol imported: " + toString(*Imp) +
268 " (defined in " + toString(Imp->getFile()) + ") [LNK4217]");
271 for (ObjFile *File : ObjFile::Instances) {
272 size_t SymIndex = (size_t)-1;
273 for (Symbol *Sym : File->getSymbols()) {
277 if (Undefs.count(Sym))
278 errorOrWarn("undefined symbol: " + toString(*Sym) +
279 getSymbolLocations(File, SymIndex));
280 if (Config->WarnLocallyDefinedImported)
281 if (Symbol *Imp = LocalImports.lookup(Sym))
282 warn(toString(File) +
283 ": locally defined symbol imported: " + toString(*Imp) +
284 " (defined in " + toString(Imp->getFile()) + ") [LNK4217]");
289 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
290 bool Inserted = false;
291 Symbol *&Sym = SymMap[CachedHashStringRef(Name)];
293 Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
294 Sym->IsUsedInRegularObj = false;
295 Sym->PendingArchiveLoad = false;
298 return {Sym, Inserted};
301 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, InputFile *File) {
302 std::pair<Symbol *, bool> Result = insert(Name);
303 if (!File || !isa<BitcodeFile>(File))
304 Result.first->IsUsedInRegularObj = true;
308 Symbol *SymbolTable::addUndefined(StringRef Name, InputFile *F,
312 std::tie(S, WasInserted) = insert(Name, F);
313 if (WasInserted || (isa<Lazy>(S) && IsWeakAlias)) {
314 replaceSymbol<Undefined>(S, Name);
317 if (auto *L = dyn_cast<Lazy>(S)) {
318 if (!S->PendingArchiveLoad) {
319 S->PendingArchiveLoad = true;
320 L->File->addMember(&L->Sym);
326 void SymbolTable::addLazy(ArchiveFile *F, const Archive::Symbol Sym) {
327 StringRef Name = Sym.getName();
330 std::tie(S, WasInserted) = insert(Name);
332 replaceSymbol<Lazy>(S, F, Sym);
335 auto *U = dyn_cast<Undefined>(S);
336 if (!U || U->WeakAlias || S->PendingArchiveLoad)
338 S->PendingArchiveLoad = true;
342 void SymbolTable::reportDuplicate(Symbol *Existing, InputFile *NewFile) {
343 std::string Msg = "duplicate symbol: " + toString(*Existing) + " in " +
344 toString(Existing->getFile()) + " and in " +
347 if (Config->ForceMultiple)
353 Symbol *SymbolTable::addAbsolute(StringRef N, COFFSymbolRef Sym) {
356 std::tie(S, WasInserted) = insert(N, nullptr);
357 S->IsUsedInRegularObj = true;
358 if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S))
359 replaceSymbol<DefinedAbsolute>(S, N, Sym);
360 else if (!isa<DefinedCOFF>(S))
361 reportDuplicate(S, nullptr);
365 Symbol *SymbolTable::addAbsolute(StringRef N, uint64_t VA) {
368 std::tie(S, WasInserted) = insert(N, nullptr);
369 S->IsUsedInRegularObj = true;
370 if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S))
371 replaceSymbol<DefinedAbsolute>(S, N, VA);
372 else if (!isa<DefinedCOFF>(S))
373 reportDuplicate(S, nullptr);
377 Symbol *SymbolTable::addSynthetic(StringRef N, Chunk *C) {
380 std::tie(S, WasInserted) = insert(N, nullptr);
381 S->IsUsedInRegularObj = true;
382 if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S))
383 replaceSymbol<DefinedSynthetic>(S, N, C);
384 else if (!isa<DefinedCOFF>(S))
385 reportDuplicate(S, nullptr);
389 Symbol *SymbolTable::addRegular(InputFile *F, StringRef N,
390 const coff_symbol_generic *Sym,
394 std::tie(S, WasInserted) = insert(N, F);
395 if (WasInserted || !isa<DefinedRegular>(S))
396 replaceSymbol<DefinedRegular>(S, F, N, /*IsCOMDAT*/ false,
397 /*IsExternal*/ true, Sym, C);
399 reportDuplicate(S, F);
403 std::pair<Symbol *, bool>
404 SymbolTable::addComdat(InputFile *F, StringRef N,
405 const coff_symbol_generic *Sym) {
408 std::tie(S, WasInserted) = insert(N, F);
409 if (WasInserted || !isa<DefinedRegular>(S)) {
410 replaceSymbol<DefinedRegular>(S, F, N, /*IsCOMDAT*/ true,
411 /*IsExternal*/ true, Sym, nullptr);
414 if (!cast<DefinedRegular>(S)->isCOMDAT())
415 reportDuplicate(S, F);
419 Symbol *SymbolTable::addCommon(InputFile *F, StringRef N, uint64_t Size,
420 const coff_symbol_generic *Sym, CommonChunk *C) {
423 std::tie(S, WasInserted) = insert(N, F);
424 if (WasInserted || !isa<DefinedCOFF>(S))
425 replaceSymbol<DefinedCommon>(S, F, N, Size, Sym, C);
426 else if (auto *DC = dyn_cast<DefinedCommon>(S))
427 if (Size > DC->getSize())
428 replaceSymbol<DefinedCommon>(S, F, N, Size, Sym, C);
432 Symbol *SymbolTable::addImportData(StringRef N, ImportFile *F) {
435 std::tie(S, WasInserted) = insert(N, nullptr);
436 S->IsUsedInRegularObj = true;
437 if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S)) {
438 replaceSymbol<DefinedImportData>(S, N, F);
442 reportDuplicate(S, F);
446 Symbol *SymbolTable::addImportThunk(StringRef Name, DefinedImportData *ID,
450 std::tie(S, WasInserted) = insert(Name, nullptr);
451 S->IsUsedInRegularObj = true;
452 if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S)) {
453 replaceSymbol<DefinedImportThunk>(S, Name, ID, Machine);
457 reportDuplicate(S, ID->File);
461 std::vector<Chunk *> SymbolTable::getChunks() {
462 std::vector<Chunk *> Res;
463 for (ObjFile *File : ObjFile::Instances) {
464 ArrayRef<Chunk *> V = File->getChunks();
465 Res.insert(Res.end(), V.begin(), V.end());
470 Symbol *SymbolTable::find(StringRef Name) {
471 return SymMap.lookup(CachedHashStringRef(Name));
474 Symbol *SymbolTable::findUnderscore(StringRef Name) {
475 if (Config->Machine == I386)
476 return find(("_" + Name).str());
480 StringRef SymbolTable::findByPrefix(StringRef Prefix) {
481 for (auto Pair : SymMap) {
482 StringRef Name = Pair.first.val();
483 if (Name.startswith(Prefix))
489 StringRef SymbolTable::findMangle(StringRef Name) {
490 if (Symbol *Sym = find(Name))
491 if (!isa<Undefined>(Sym))
493 if (Config->Machine != I386)
494 return findByPrefix(("?" + Name + "@@Y").str());
495 if (!Name.startswith("_"))
497 // Search for x86 stdcall function.
498 StringRef S = findByPrefix((Name + "@").str());
501 // Search for x86 fastcall function.
502 S = findByPrefix(("@" + Name.substr(1) + "@").str());
505 // Search for x86 vectorcall function.
506 S = findByPrefix((Name.substr(1) + "@@").str());
509 // Search for x86 C++ non-member function.
510 return findByPrefix(("?" + Name.substr(1) + "@@Y").str());
513 void SymbolTable::mangleMaybe(Symbol *B) {
514 auto *U = dyn_cast<Undefined>(B);
515 if (!U || U->WeakAlias)
517 StringRef Alias = findMangle(U->getName());
518 if (!Alias.empty()) {
519 log(U->getName() + " aliased to " + Alias);
520 U->WeakAlias = addUndefined(Alias);
524 Symbol *SymbolTable::addUndefined(StringRef Name) {
525 return addUndefined(Name, nullptr, false);
528 std::vector<StringRef> SymbolTable::compileBitcodeFiles() {
529 LTO.reset(new BitcodeCompiler);
530 for (BitcodeFile *F : BitcodeFile::Instances)
532 return LTO->compile();
535 void SymbolTable::addCombinedLTOObjects() {
536 if (BitcodeFile::Instances.empty())
539 ScopedTimer T(LTOTimer);
540 for (StringRef Object : compileBitcodeFiles()) {
541 auto *Obj = make<ObjFile>(MemoryBufferRef(Object, "lto.tmp"));
543 ObjFile::Instances.push_back(Obj);