blob: 51cf3de1d1d7b3bca88ace3da3eb5386ffcc420e [file] [log] [blame]
Rui Ueyama411c63602015-05-28 19:09:30 +00001//===- SymbolTable.cpp ----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Config.h"
11#include "Driver.h"
Rui Ueyama8fd9fb92015-06-01 02:58:15 +000012#include "Error.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000013#include "SymbolTable.h"
Chandler Carruthbe6e80b2015-06-29 18:50:11 +000014#include "Symbols.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000015#include "llvm/ADT/STLExtras.h"
Peter Collingbourne60c16162015-06-01 20:10:10 +000016#include "llvm/LTO/LTOCodeGenerator.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000017#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
Rui Ueyamaf5313b32015-06-28 22:16:41 +000019#include <utility>
Rui Ueyama411c63602015-05-28 19:09:30 +000020
Rui Ueyamad68ff342015-05-31 03:57:30 +000021using namespace llvm;
22
Rui Ueyama411c63602015-05-28 19:09:30 +000023namespace lld {
24namespace coff {
25
26SymbolTable::SymbolTable() {
Rui Ueyama8d3010a2015-06-30 19:35:21 +000027 addSymbol(new (Alloc) DefinedAbsolute("__ImageBase", Config->ImageBase));
Rui Ueyama5cff6852015-05-31 03:34:08 +000028 if (!Config->EntryName.empty())
Rui Ueyama8d3010a2015-06-30 19:35:21 +000029 addSymbol(new (Alloc) Undefined(Config->EntryName));
Rui Ueyama411c63602015-05-28 19:09:30 +000030}
31
Rui Ueyama8d3010a2015-06-30 19:35:21 +000032void SymbolTable::addFile(std::unique_ptr<InputFile> FileP) {
33 InputFile *File = FileP.get();
34 Files.push_back(std::move(FileP));
35 if (auto *F = dyn_cast<ArchiveFile>(File)) {
36 ArchiveQueue.push_back(F);
37 return;
38 }
39 ObjectQueue.push_back(File);
40 if (auto *F = dyn_cast<ObjectFile>(File)) {
41 ObjectFiles.push_back(F);
42 } else if (auto *F = dyn_cast<BitcodeFile>(File)) {
43 BitcodeFiles.push_back(F);
44 } else {
45 ImportFiles.push_back(cast<ImportFile>(File));
46 }
Rui Ueyama411c63602015-05-28 19:09:30 +000047}
48
Rui Ueyama0d2e9992015-06-23 23:56:39 +000049std::error_code SymbolTable::run() {
Rui Ueyama8d3010a2015-06-30 19:35:21 +000050 while (!ArchiveQueue.empty() || !ObjectQueue.empty()) {
51 if (auto EC = readArchives())
Rui Ueyama0d2e9992015-06-23 23:56:39 +000052 return EC;
Rui Ueyama8d3010a2015-06-30 19:35:21 +000053 if (auto EC = readObjects())
54 return EC;
55 ++Version;
Rui Ueyamaeeae5dd2015-06-08 06:00:10 +000056 }
Peter Collingbourneace2f092015-06-06 02:00:45 +000057 return std::error_code();
58}
59
Rui Ueyama8d3010a2015-06-30 19:35:21 +000060std::error_code SymbolTable::readArchives() {
61 if (ArchiveQueue.empty())
62 return std::error_code();
63
64 // Add lazy symbols to the symbol table. Lazy symbols that conflict
65 // with existing undefined symbols are accumulated in LazySyms.
66 std::vector<Symbol *> LazySyms;
67 for (ArchiveFile *File : ArchiveQueue) {
68 if (Config->Verbose)
69 llvm::outs() << "Reading " << File->getShortName() << "\n";
70 if (auto EC = File->parse())
71 return EC;
72 for (Lazy *Sym : File->getLazySymbols())
73 addLazy(Sym, &LazySyms);
74 }
75 ArchiveQueue.clear();
76
77 // Add archive member files to ObjectQueue that should resolve
78 // existing undefined symbols.
79 for (Symbol *Sym : LazySyms)
80 if (auto EC = addMemberFile(cast<Lazy>(Sym->Body)))
81 return EC;
82 return std::error_code();
83}
84
85std::error_code SymbolTable::readObjects() {
86 if (ObjectQueue.empty())
87 return std::error_code();
88
89 // Add defined and undefined symbols to the symbol table.
90 std::vector<StringRef> Directives;
91 for (size_t I = 0; I < ObjectQueue.size(); ++I) {
92 InputFile *File = ObjectQueue[I];
93 if (Config->Verbose)
94 llvm::outs() << "Reading " << File->getShortName() << "\n";
95 if (auto EC = File->parse())
96 return EC;
97 // Adding symbols may add more files to ObjectQueue
98 // (but not to ArchiveQueue).
99 for (SymbolBody *Sym : File->getSymbols())
100 if (Sym->isExternal())
101 if (auto EC = addSymbol(Sym))
102 return EC;
103 StringRef S = File->getDirectives();
104 if (!S.empty())
105 Directives.push_back(S);
106 }
107 ObjectQueue.clear();
108
109 // Parse directive sections. This may add files to
110 // ArchiveQueue and ObjectQueue.
111 for (StringRef S : Directives)
112 if (auto EC = Driver->parseDirectives(S))
113 return EC;
114 return std::error_code();
115}
116
Rui Ueyama411c63602015-05-28 19:09:30 +0000117bool SymbolTable::reportRemainingUndefines() {
118 bool Ret = false;
119 for (auto &I : Symtab) {
120 Symbol *Sym = I.second;
121 auto *Undef = dyn_cast<Undefined>(Sym->Body);
122 if (!Undef)
123 continue;
Rui Ueyamad7666532015-06-25 02:21:44 +0000124 StringRef Name = Undef->getName();
Rui Ueyamaf24e6f82015-06-28 20:34:09 +0000125 // The weak alias may have been resovled, so check for that.
Rui Ueyama48975962015-07-01 22:32:23 +0000126 if (auto *D = dyn_cast_or_null<Defined>(Undef->WeakAlias)) {
127 Sym->Body = D;
128 continue;
Rui Ueyama411c63602015-05-28 19:09:30 +0000129 }
Rui Ueyamad7666532015-06-25 02:21:44 +0000130 // If we can resolve a symbol by removing __imp_ prefix, do that.
131 // This odd rule is for compatibility with MSVC linker.
132 if (Name.startswith("__imp_")) {
133 if (Defined *Imp = find(Name.substr(strlen("__imp_")))) {
Rui Ueyama88e0f922015-06-25 03:31:47 +0000134 auto *S = new (Alloc) DefinedLocalImport(Name, Imp);
135 LocalImportChunks.push_back(S->getChunk());
136 Sym->Body = S;
Rui Ueyamad7666532015-06-25 02:21:44 +0000137 continue;
138 }
139 }
140 llvm::errs() << "undefined symbol: " << Name << "\n";
Rui Ueyama95925fd2015-06-28 19:35:15 +0000141 // Remaining undefined symbols are not fatal if /force is specified.
142 // They are replaced with dummy defined symbols.
143 if (Config->Force) {
144 Sym->Body = new (Alloc) DefinedAbsolute(Name, 0);
145 continue;
146 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000147 Ret = true;
148 }
149 return Ret;
150}
151
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000152void SymbolTable::addLazy(Lazy *New, std::vector<Symbol *> *Accum) {
153 Symbol *&Sym = Symtab[New->getName()];
Rui Ueyama411c63602015-05-28 19:09:30 +0000154 if (!Sym) {
155 Sym = new (Alloc) Symbol(New);
156 New->setBackref(Sym);
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000157 return;
158 }
159 SymbolBody *Existing = Sym->Body;
160 if (!isa<Undefined>(Existing))
161 return;
162 Sym->Body = New;
163 New->setBackref(Sym);
164 Accum->push_back(Sym);
165}
166
167std::error_code SymbolTable::addSymbol(SymbolBody *New) {
168 // Find an existing symbol or create and insert a new one.
169 assert(isa<Defined>(New) || isa<Undefined>(New));
170 Symbol *&Sym = Symtab[New->getName()];
171 if (!Sym) {
172 Sym = new (Alloc) Symbol(New);
173 New->setBackref(Sym);
Rui Ueyama411c63602015-05-28 19:09:30 +0000174 return std::error_code();
175 }
176 New->setBackref(Sym);
177
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000178 // If we have an undefined symbol and a lazy symbol,
179 // let the lazy symbol to read a member file.
180 SymbolBody *Existing = Sym->Body;
181 if (auto *L = dyn_cast<Lazy>(Existing)) {
Rui Ueyama48975962015-07-01 22:32:23 +0000182 // Undefined symbols with weak aliases need not to be resolved,
183 // since they would be replaced with weak aliases if they remain
184 // undefined.
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000185 if (auto *U = dyn_cast<Undefined>(New))
Rui Ueyama48975962015-07-01 22:32:23 +0000186 if (!U->WeakAlias)
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000187 return addMemberFile(L);
188 Sym->Body = New;
189 return std::error_code();
190 }
191
Rui Ueyama411c63602015-05-28 19:09:30 +0000192 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
193 // equivalent (conflicting), or more preferable, respectively.
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000194 int Comp = Existing->compare(New);
195 if (Comp == 0) {
Rui Ueyama68633f12015-06-25 23:22:00 +0000196 llvm::errs() << "duplicate symbol: " << Existing->getDebugName()
197 << " and " << New->getDebugName() << "\n";
Rui Ueyama8fd9fb92015-06-01 02:58:15 +0000198 return make_error_code(LLDError::DuplicateSymbols);
199 }
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000200 if (Comp < 0)
201 Sym->Body = New;
Rui Ueyama411c63602015-05-28 19:09:30 +0000202 return std::error_code();
203}
204
205// Reads an archive member file pointed by a given symbol.
206std::error_code SymbolTable::addMemberFile(Lazy *Body) {
207 auto FileOrErr = Body->getMember();
208 if (auto EC = FileOrErr.getError())
209 return EC;
210 std::unique_ptr<InputFile> File = std::move(FileOrErr.get());
211
212 // getMember returns an empty buffer if the member was already
213 // read from the library.
214 if (!File)
215 return std::error_code();
216 if (Config->Verbose)
Rui Ueyama5b2588a2015-06-08 05:43:50 +0000217 llvm::outs() << "Loaded " << File->getShortName() << " for "
Rui Ueyama411c63602015-05-28 19:09:30 +0000218 << Body->getName() << "\n";
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000219 addFile(std::move(File));
220 return std::error_code();
Rui Ueyama411c63602015-05-28 19:09:30 +0000221}
222
223std::vector<Chunk *> SymbolTable::getChunks() {
224 std::vector<Chunk *> Res;
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000225 for (ObjectFile *File : ObjectFiles) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000226 std::vector<Chunk *> &V = File->getChunks();
227 Res.insert(Res.end(), V.begin(), V.end());
228 }
229 return Res;
230}
231
Rui Ueyama5cff6852015-05-31 03:34:08 +0000232Defined *SymbolTable::find(StringRef Name) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000233 auto It = Symtab.find(Name);
234 if (It == Symtab.end())
235 return nullptr;
Rui Ueyama5cff6852015-05-31 03:34:08 +0000236 if (auto *Def = dyn_cast<Defined>(It->second->Body))
237 return Def;
238 return nullptr;
239}
240
Rui Ueyama4b669892015-06-30 23:46:52 +0000241Symbol *SymbolTable::findSymbol(StringRef Name) {
Rui Ueyama45044f42015-06-29 01:03:53 +0000242 auto It = Symtab.find(Name);
243 if (It == Symtab.end())
244 return nullptr;
Rui Ueyama4b669892015-06-30 23:46:52 +0000245 return It->second;
Rui Ueyama45044f42015-06-29 01:03:53 +0000246}
247
Rui Ueyamaf5313b32015-06-28 22:16:41 +0000248// Find a given symbol or its mangled symbol.
249std::pair<StringRef, Symbol *> SymbolTable::findMangled(StringRef S) {
250 auto It = Symtab.find(S);
251 if (It != Symtab.end()) {
252 Symbol *Sym = It->second;
253 if (isa<Defined>(Sym->Body))
254 return std::make_pair(S, Sym);
255 }
256
257 // In Microsoft ABI, a non-member function name is mangled this way.
258 std::string Prefix = ("?" + S + "@@Y").str();
259 for (auto I : Symtab) {
260 StringRef Name = I.first;
261 Symbol *Sym = I.second;
262 if (!Name.startswith(Prefix))
263 continue;
Rui Ueyama6b79ed12015-06-29 14:27:10 +0000264 if (auto *B = dyn_cast<Lazy>(Sym->Body)) {
265 addMemberFile(B);
266 run();
267 }
Rui Ueyamaf5313b32015-06-28 22:16:41 +0000268 if (isa<Defined>(Sym->Body))
269 return std::make_pair(Name, Sym);
270 }
271 return std::make_pair(S, nullptr);
272}
273
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000274std::error_code SymbolTable::addUndefined(StringRef Name) {
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000275 return addSymbol(new (Alloc) Undefined(Name));
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000276}
277
Rui Ueyama360bace2015-05-31 22:31:31 +0000278// Resolve To, and make From an alias to To.
279std::error_code SymbolTable::rename(StringRef From, StringRef To) {
Rui Ueyama4d2834b2015-06-19 19:23:43 +0000280 // If From is not undefined, do nothing.
281 // Otherwise, rename it to see if To can be resolved instead.
282 auto It = Symtab.find(From);
283 if (It == Symtab.end())
284 return std::error_code();
285 Symbol *Sym = It->second;
286 if (!isa<Undefined>(Sym->Body))
287 return std::error_code();
Rui Ueyama360bace2015-05-31 22:31:31 +0000288 SymbolBody *Body = new (Alloc) Undefined(To);
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000289 if (auto EC = addSymbol(Body))
Rui Ueyama360bace2015-05-31 22:31:31 +0000290 return EC;
Rui Ueyama45044f42015-06-29 01:03:53 +0000291 SymbolBody *Repl = Body->getReplacement();
292 if (isa<Undefined>(Repl))
293 return std::error_code();
294 Sym->Body = Repl;
Rui Ueyama4d2834b2015-06-19 19:23:43 +0000295 Body->setBackref(Sym);
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000296 ++Version;
Rui Ueyama360bace2015-05-31 22:31:31 +0000297 return std::error_code();
298}
299
Peter Collingbournebe549552015-06-26 18:58:24 +0000300void SymbolTable::printMap(llvm::raw_ostream &OS) {
301 for (ObjectFile *File : ObjectFiles) {
302 OS << File->getShortName() << ":\n";
303 for (SymbolBody *Body : File->getSymbols())
304 if (auto *R = dyn_cast<DefinedRegular>(Body))
305 if (R->isLive())
306 OS << Twine::utohexstr(Config->ImageBase + R->getRVA())
307 << " " << R->getName() << "\n";
308 }
309}
310
Peter Collingbourne60c16162015-06-01 20:10:10 +0000311std::error_code SymbolTable::addCombinedLTOObject() {
312 if (BitcodeFiles.empty())
313 return std::error_code();
314
Peter Collingbourne60c16162015-06-01 20:10:10 +0000315 // Create an object file and add it to the symbol table by replacing any
316 // DefinedBitcode symbols with the definitions in the object file.
Rui Ueyamaefba7812015-06-09 17:52:17 +0000317 LTOCodeGenerator CG;
318 auto FileOrErr = createLTOObject(&CG);
319 if (auto EC = FileOrErr.getError())
Peter Collingbourne60c16162015-06-01 20:10:10 +0000320 return EC;
Rui Ueyamaefba7812015-06-09 17:52:17 +0000321 ObjectFile *Obj = FileOrErr.get();
322
Peter Collingbourne60c16162015-06-01 20:10:10 +0000323 for (SymbolBody *Body : Obj->getSymbols()) {
324 if (!Body->isExternal())
325 continue;
Peter Collingbourned9e4e982015-06-09 02:53:09 +0000326 // Find an existing Symbol. We should not see any new undefined symbols at
327 // this point.
Peter Collingbourne60c16162015-06-01 20:10:10 +0000328 StringRef Name = Body->getName();
Peter Collingbourned9e4e982015-06-09 02:53:09 +0000329 Symbol *&Sym = Symtab[Name];
Peter Collingbourne60c16162015-06-01 20:10:10 +0000330 if (!Sym) {
Peter Collingbourned9e4e982015-06-09 02:53:09 +0000331 if (!isa<Defined>(Body)) {
332 llvm::errs() << "LTO: undefined symbol: " << Name << '\n';
333 return make_error_code(LLDError::BrokenFile);
334 }
335 Sym = new (Alloc) Symbol(Body);
336 Body->setBackref(Sym);
337 continue;
Peter Collingbourne60c16162015-06-01 20:10:10 +0000338 }
339 Body->setBackref(Sym);
340
341 if (isa<DefinedBitcode>(Sym->Body)) {
342 // The symbol should now be defined.
343 if (!isa<Defined>(Body)) {
344 llvm::errs() << "LTO: undefined symbol: " << Name << '\n';
345 return make_error_code(LLDError::BrokenFile);
346 }
347 Sym->Body = Body;
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000348 continue;
Peter Collingbourne60c16162015-06-01 20:10:10 +0000349 }
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000350 if (auto *L = dyn_cast<Lazy>(Sym->Body)) {
351 // We may see new references to runtime library symbols such as __chkstk
352 // here. These symbols must be wholly defined in non-bitcode files.
353 if (auto EC = addMemberFile(L))
Peter Collingbourne2ed4c8f2015-06-24 00:12:34 +0000354 return EC;
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000355 continue;
356 }
357 SymbolBody *Existing = Sym->Body;
358 int Comp = Existing->compare(Body);
359 if (Comp == 0) {
360 llvm::errs() << "LTO: unexpected duplicate symbol: " << Name << "\n";
361 return make_error_code(LLDError::BrokenFile);
362 }
363 if (Comp < 0)
364 Sym->Body = Body;
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000365 }
366
367 size_t NumBitcodeFiles = BitcodeFiles.size();
368 if (auto EC = run())
369 return EC;
370 if (BitcodeFiles.size() != NumBitcodeFiles) {
371 llvm::errs() << "LTO: late loaded symbol created new bitcode reference\n";
372 return make_error_code(LLDError::BrokenFile);
Peter Collingbourne60c16162015-06-01 20:10:10 +0000373 }
374
Peter Collingbourne73b75e32015-06-09 04:29:54 +0000375 // New runtime library symbol references may have created undefined references.
376 if (reportRemainingUndefines())
377 return make_error_code(LLDError::BrokenFile);
Peter Collingbourne60c16162015-06-01 20:10:10 +0000378 return std::error_code();
379}
380
Rui Ueyamaefba7812015-06-09 17:52:17 +0000381// Combine and compile bitcode files and then return the result
382// as a regular COFF object file.
383ErrorOr<ObjectFile *> SymbolTable::createLTOObject(LTOCodeGenerator *CG) {
384 // All symbols referenced by non-bitcode objects must be preserved.
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000385 for (ObjectFile *File : ObjectFiles)
Rui Ueyamaefba7812015-06-09 17:52:17 +0000386 for (SymbolBody *Body : File->getSymbols())
387 if (auto *S = dyn_cast<DefinedBitcode>(Body->getReplacement()))
388 CG->addMustPreserveSymbol(S->getName());
389
Peter Collingbourne1b6fd1f2015-06-11 21:49:54 +0000390 // Likewise for bitcode symbols which we initially resolved to non-bitcode.
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000391 for (BitcodeFile *File : BitcodeFiles)
Peter Collingbourne1b6fd1f2015-06-11 21:49:54 +0000392 for (SymbolBody *Body : File->getSymbols())
393 if (isa<DefinedBitcode>(Body) &&
394 !isa<DefinedBitcode>(Body->getReplacement()))
395 CG->addMustPreserveSymbol(Body->getName());
396
Rui Ueyamaefba7812015-06-09 17:52:17 +0000397 // Likewise for other symbols that must be preserved.
398 for (StringRef Name : Config->GCRoots)
399 if (isa<DefinedBitcode>(Symtab[Name]->Body))
400 CG->addMustPreserveSymbol(Name);
401
402 CG->setModule(BitcodeFiles[0]->releaseModule());
403 for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I)
404 CG->addModule(BitcodeFiles[I]->getModule());
405
406 std::string ErrMsg;
407 LTOMB = CG->compile(false, false, false, ErrMsg); // take MB ownership
408 if (!LTOMB) {
409 llvm::errs() << ErrMsg << '\n';
410 return make_error_code(LLDError::BrokenFile);
411 }
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000412 auto *Obj = new ObjectFile(LTOMB->getMemBufferRef());
413 Files.emplace_back(Obj);
414 ObjectFiles.push_back(Obj);
Rui Ueyamaefba7812015-06-09 17:52:17 +0000415 if (auto EC = Obj->parse())
416 return EC;
417 return Obj;
418}
419
Rui Ueyama411c63602015-05-28 19:09:30 +0000420} // namespace coff
421} // namespace lld