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"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Memory.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
29 void SymbolTable::addFile(InputFile *File) {
30 log("Reading " + toString(File));
33 MachineTypes MT = File->getMachineType();
34 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
36 } else if (MT != IMAGE_FILE_MACHINE_UNKNOWN && Config->Machine != MT) {
37 fatal(toString(File) + ": machine type " + machineToStr(MT) +
38 " conflicts with " + machineToStr(Config->Machine));
41 if (auto *F = dyn_cast<ObjFile>(File)) {
42 ObjFile::Instances.push_back(F);
43 } else if (auto *F = dyn_cast<BitcodeFile>(File)) {
44 BitcodeFile::Instances.push_back(F);
45 } else if (auto *F = dyn_cast<ImportFile>(File)) {
46 ImportFile::Instances.push_back(F);
49 StringRef S = File->getDirectives();
53 log("Directives: " + toString(File) + ": " + S);
54 Driver->parseDirectives(S);
57 static void errorOrWarn(const Twine &S) {
64 void SymbolTable::reportRemainingUndefines() {
65 SmallPtrSet<Symbol *, 8> Undefs;
66 DenseMap<Symbol *, Symbol *> LocalImports;
68 for (auto &I : SymMap) {
69 Symbol *Sym = I.second;
70 auto *Undef = dyn_cast<Undefined>(Sym);
73 if (!Sym->IsUsedInRegularObj)
76 StringRef Name = Undef->getName();
78 // A weak alias may have been resolved, so check for that.
79 if (Defined *D = Undef->getWeakAlias()) {
80 // We want to replace Sym with D. However, we can't just blindly
81 // copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
82 // internal symbol, and internal symbols are stored as "unparented"
83 // Symbols. For that reason we need to check which type of symbol we
84 // are dealing with and copy the correct number of bytes.
85 if (isa<DefinedRegular>(D))
86 memcpy(Sym, D, sizeof(DefinedRegular));
87 else if (isa<DefinedAbsolute>(D))
88 memcpy(Sym, D, sizeof(DefinedAbsolute));
90 memcpy(Sym, D, sizeof(SymbolUnion));
94 // If we can resolve a symbol by removing __imp_ prefix, do that.
95 // This odd rule is for compatibility with MSVC linker.
96 if (Name.startswith("__imp_")) {
97 Symbol *Imp = find(Name.substr(strlen("__imp_")));
98 if (Imp && isa<Defined>(Imp)) {
99 auto *D = cast<Defined>(Imp);
100 replaceSymbol<DefinedLocalImport>(Sym, Name, D);
101 LocalImportChunks.push_back(cast<DefinedLocalImport>(Sym)->getChunk());
102 LocalImports[Sym] = D;
107 // Remaining undefined symbols are not fatal if /force is specified.
108 // They are replaced with dummy defined symbols.
110 replaceSymbol<DefinedAbsolute>(Sym, Name, 0);
114 if (Undefs.empty() && LocalImports.empty())
117 for (Symbol *B : Config->GCRoot) {
119 errorOrWarn("<root>: undefined symbol: " + B->getName());
120 if (Config->WarnLocallyDefinedImported)
121 if (Symbol *Imp = LocalImports.lookup(B))
122 warn("<root>: locally defined symbol imported: " + Imp->getName() +
123 " (defined in " + toString(Imp->getFile()) + ")");
126 for (ObjFile *File : ObjFile::Instances) {
127 for (Symbol *Sym : File->getSymbols()) {
130 if (Undefs.count(Sym))
131 errorOrWarn(toString(File) + ": undefined symbol: " + Sym->getName());
132 if (Config->WarnLocallyDefinedImported)
133 if (Symbol *Imp = LocalImports.lookup(Sym))
134 warn(toString(File) + ": locally defined symbol imported: " +
135 Imp->getName() + " (defined in " + toString(Imp->getFile()) +
141 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
142 Symbol *&Sym = SymMap[CachedHashStringRef(Name)];
145 Sym = (Symbol *)make<SymbolUnion>();
146 Sym->IsUsedInRegularObj = false;
147 Sym->PendingArchiveLoad = false;
151 Symbol *SymbolTable::addUndefined(StringRef Name, InputFile *F,
155 std::tie(S, WasInserted) = insert(Name);
156 if (!F || !isa<BitcodeFile>(F))
157 S->IsUsedInRegularObj = true;
158 if (WasInserted || (isa<Lazy>(S) && IsWeakAlias)) {
159 replaceSymbol<Undefined>(S, Name);
162 if (auto *L = dyn_cast<Lazy>(S)) {
163 if (!S->PendingArchiveLoad) {
164 S->PendingArchiveLoad = true;
165 L->File->addMember(&L->Sym);
171 void SymbolTable::addLazy(ArchiveFile *F, const Archive::Symbol Sym) {
172 StringRef Name = Sym.getName();
175 std::tie(S, WasInserted) = insert(Name);
177 replaceSymbol<Lazy>(S, F, Sym);
180 auto *U = dyn_cast<Undefined>(S);
181 if (!U || U->WeakAlias || S->PendingArchiveLoad)
183 S->PendingArchiveLoad = true;
187 void SymbolTable::reportDuplicate(Symbol *Existing, InputFile *NewFile) {
188 error("duplicate symbol: " + toString(*Existing) + " in " +
189 toString(Existing->getFile()) + " and in " + toString(NewFile));
192 Symbol *SymbolTable::addAbsolute(StringRef N, COFFSymbolRef Sym) {
195 std::tie(S, WasInserted) = insert(N);
196 S->IsUsedInRegularObj = true;
197 if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S))
198 replaceSymbol<DefinedAbsolute>(S, N, Sym);
199 else if (!isa<DefinedCOFF>(S))
200 reportDuplicate(S, nullptr);
204 Symbol *SymbolTable::addAbsolute(StringRef N, uint64_t VA) {
207 std::tie(S, WasInserted) = insert(N);
208 S->IsUsedInRegularObj = true;
209 if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S))
210 replaceSymbol<DefinedAbsolute>(S, N, VA);
211 else if (!isa<DefinedCOFF>(S))
212 reportDuplicate(S, nullptr);
216 Symbol *SymbolTable::addSynthetic(StringRef N, Chunk *C) {
219 std::tie(S, WasInserted) = insert(N);
220 S->IsUsedInRegularObj = true;
221 if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S))
222 replaceSymbol<DefinedSynthetic>(S, N, C);
223 else if (!isa<DefinedCOFF>(S))
224 reportDuplicate(S, nullptr);
228 Symbol *SymbolTable::addRegular(InputFile *F, StringRef N,
229 const coff_symbol_generic *Sym,
233 std::tie(S, WasInserted) = insert(N);
234 if (!isa<BitcodeFile>(F))
235 S->IsUsedInRegularObj = true;
236 if (WasInserted || !isa<DefinedRegular>(S))
237 replaceSymbol<DefinedRegular>(S, F, N, /*IsCOMDAT*/ false,
238 /*IsExternal*/ true, Sym, C);
240 reportDuplicate(S, F);
244 std::pair<Symbol *, bool>
245 SymbolTable::addComdat(InputFile *F, StringRef N,
246 const coff_symbol_generic *Sym) {
249 std::tie(S, WasInserted) = insert(N);
250 if (!isa<BitcodeFile>(F))
251 S->IsUsedInRegularObj = true;
252 if (WasInserted || !isa<DefinedRegular>(S)) {
253 replaceSymbol<DefinedRegular>(S, F, N, /*IsCOMDAT*/ true,
254 /*IsExternal*/ true, Sym, nullptr);
257 if (!cast<DefinedRegular>(S)->isCOMDAT())
258 reportDuplicate(S, F);
262 Symbol *SymbolTable::addCommon(InputFile *F, StringRef N, uint64_t Size,
263 const coff_symbol_generic *Sym, CommonChunk *C) {
266 std::tie(S, WasInserted) = insert(N);
267 if (!isa<BitcodeFile>(F))
268 S->IsUsedInRegularObj = true;
269 if (WasInserted || !isa<DefinedCOFF>(S))
270 replaceSymbol<DefinedCommon>(S, F, N, Size, Sym, C);
271 else if (auto *DC = dyn_cast<DefinedCommon>(S))
272 if (Size > DC->getSize())
273 replaceSymbol<DefinedCommon>(S, F, N, Size, Sym, C);
277 DefinedImportData *SymbolTable::addImportData(StringRef N, ImportFile *F) {
280 std::tie(S, WasInserted) = insert(N);
281 S->IsUsedInRegularObj = true;
282 if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S)) {
283 replaceSymbol<DefinedImportData>(S, N, F);
284 return cast<DefinedImportData>(S);
287 reportDuplicate(S, F);
291 DefinedImportThunk *SymbolTable::addImportThunk(StringRef Name,
292 DefinedImportData *ID,
296 std::tie(S, WasInserted) = insert(Name);
297 S->IsUsedInRegularObj = true;
298 if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S)) {
299 replaceSymbol<DefinedImportThunk>(S, Name, ID, Machine);
300 return cast<DefinedImportThunk>(S);
303 reportDuplicate(S, ID->File);
307 std::vector<Chunk *> SymbolTable::getChunks() {
308 std::vector<Chunk *> Res;
309 for (ObjFile *File : ObjFile::Instances) {
310 ArrayRef<Chunk *> V = File->getChunks();
311 Res.insert(Res.end(), V.begin(), V.end());
316 Symbol *SymbolTable::find(StringRef Name) {
317 auto It = SymMap.find(CachedHashStringRef(Name));
318 if (It == SymMap.end())
323 Symbol *SymbolTable::findUnderscore(StringRef Name) {
324 if (Config->Machine == I386)
325 return find(("_" + Name).str());
329 StringRef SymbolTable::findByPrefix(StringRef Prefix) {
330 for (auto Pair : SymMap) {
331 StringRef Name = Pair.first.val();
332 if (Name.startswith(Prefix))
338 StringRef SymbolTable::findMangle(StringRef Name) {
339 if (Symbol *Sym = find(Name))
340 if (!isa<Undefined>(Sym))
342 if (Config->Machine != I386)
343 return findByPrefix(("?" + Name + "@@Y").str());
344 if (!Name.startswith("_"))
346 // Search for x86 stdcall function.
347 StringRef S = findByPrefix((Name + "@").str());
350 // Search for x86 fastcall function.
351 S = findByPrefix(("@" + Name.substr(1) + "@").str());
354 // Search for x86 vectorcall function.
355 S = findByPrefix((Name.substr(1) + "@@").str());
358 // Search for x86 C++ non-member function.
359 return findByPrefix(("?" + Name.substr(1) + "@@Y").str());
362 void SymbolTable::mangleMaybe(Symbol *B) {
363 auto *U = dyn_cast<Undefined>(B);
364 if (!U || U->WeakAlias)
366 StringRef Alias = findMangle(U->getName());
367 if (!Alias.empty()) {
368 log(U->getName() + " aliased to " + Alias);
369 U->WeakAlias = addUndefined(Alias);
373 Symbol *SymbolTable::addUndefined(StringRef Name) {
374 return addUndefined(Name, nullptr, false);
377 std::vector<StringRef> SymbolTable::compileBitcodeFiles() {
378 LTO.reset(new BitcodeCompiler);
379 for (BitcodeFile *F : BitcodeFile::Instances)
381 return LTO->compile();
384 void SymbolTable::addCombinedLTOObjects() {
385 if (BitcodeFile::Instances.empty())
387 for (StringRef Object : compileBitcodeFiles()) {
388 auto *Obj = make<ObjFile>(MemoryBufferRef(Object, "lto.tmp"));
390 ObjFile::Instances.push_back(Obj);