blob: 0a7dea8910b5f23e7dfba8940de6c3a68a1bcd10 [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 Ueyama411c63602015-05-28 19:09:30 +000028}
29
Rui Ueyama8d3010a2015-06-30 19:35:21 +000030void SymbolTable::addFile(std::unique_ptr<InputFile> FileP) {
31 InputFile *File = FileP.get();
32 Files.push_back(std::move(FileP));
33 if (auto *F = dyn_cast<ArchiveFile>(File)) {
34 ArchiveQueue.push_back(F);
35 return;
36 }
37 ObjectQueue.push_back(File);
38 if (auto *F = dyn_cast<ObjectFile>(File)) {
39 ObjectFiles.push_back(F);
40 } else if (auto *F = dyn_cast<BitcodeFile>(File)) {
41 BitcodeFiles.push_back(F);
42 } else {
43 ImportFiles.push_back(cast<ImportFile>(File));
44 }
Rui Ueyama411c63602015-05-28 19:09:30 +000045}
46
Rui Ueyama0d2e9992015-06-23 23:56:39 +000047std::error_code SymbolTable::run() {
Rui Ueyama8d3010a2015-06-30 19:35:21 +000048 while (!ArchiveQueue.empty() || !ObjectQueue.empty()) {
49 if (auto EC = readArchives())
Rui Ueyama0d2e9992015-06-23 23:56:39 +000050 return EC;
Rui Ueyama8d3010a2015-06-30 19:35:21 +000051 if (auto EC = readObjects())
52 return EC;
53 ++Version;
Rui Ueyamaeeae5dd2015-06-08 06:00:10 +000054 }
Peter Collingbourneace2f092015-06-06 02:00:45 +000055 return std::error_code();
56}
57
Rui Ueyama8d3010a2015-06-30 19:35:21 +000058std::error_code SymbolTable::readArchives() {
59 if (ArchiveQueue.empty())
60 return std::error_code();
61
62 // Add lazy symbols to the symbol table. Lazy symbols that conflict
63 // with existing undefined symbols are accumulated in LazySyms.
64 std::vector<Symbol *> LazySyms;
65 for (ArchiveFile *File : ArchiveQueue) {
66 if (Config->Verbose)
67 llvm::outs() << "Reading " << File->getShortName() << "\n";
68 if (auto EC = File->parse())
69 return EC;
70 for (Lazy *Sym : File->getLazySymbols())
71 addLazy(Sym, &LazySyms);
72 }
73 ArchiveQueue.clear();
74
75 // Add archive member files to ObjectQueue that should resolve
76 // existing undefined symbols.
77 for (Symbol *Sym : LazySyms)
78 if (auto EC = addMemberFile(cast<Lazy>(Sym->Body)))
79 return EC;
80 return std::error_code();
81}
82
83std::error_code SymbolTable::readObjects() {
84 if (ObjectQueue.empty())
85 return std::error_code();
86
87 // Add defined and undefined symbols to the symbol table.
88 std::vector<StringRef> Directives;
89 for (size_t I = 0; I < ObjectQueue.size(); ++I) {
90 InputFile *File = ObjectQueue[I];
91 if (Config->Verbose)
92 llvm::outs() << "Reading " << File->getShortName() << "\n";
93 if (auto EC = File->parse())
94 return EC;
95 // Adding symbols may add more files to ObjectQueue
96 // (but not to ArchiveQueue).
97 for (SymbolBody *Sym : File->getSymbols())
98 if (Sym->isExternal())
99 if (auto EC = addSymbol(Sym))
100 return EC;
101 StringRef S = File->getDirectives();
102 if (!S.empty())
103 Directives.push_back(S);
104 }
105 ObjectQueue.clear();
106
107 // Parse directive sections. This may add files to
108 // ArchiveQueue and ObjectQueue.
109 for (StringRef S : Directives)
110 if (auto EC = Driver->parseDirectives(S))
111 return EC;
112 return std::error_code();
113}
114
Rui Ueyama411c63602015-05-28 19:09:30 +0000115bool SymbolTable::reportRemainingUndefines() {
116 bool Ret = false;
117 for (auto &I : Symtab) {
118 Symbol *Sym = I.second;
119 auto *Undef = dyn_cast<Undefined>(Sym->Body);
120 if (!Undef)
121 continue;
Rui Ueyamad7666532015-06-25 02:21:44 +0000122 StringRef Name = Undef->getName();
Rui Ueyamaf24e6f82015-06-28 20:34:09 +0000123 // The weak alias may have been resovled, so check for that.
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000124 if (SymbolBody *Alias = Undef->WeakAlias) {
125 if (auto *D = dyn_cast<Defined>(Alias->getReplacement())) {
126 Sym->Body = D;
127 continue;
128 }
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 Ueyama6bf638e2015-07-02 00:04:14 +0000248void SymbolTable::mangleMaybe(Undefined *U) {
249 if (U->WeakAlias)
250 return;
251 if (!isa<Undefined>(U->getReplacement()))
252 return;
Rui Ueyamaf5313b32015-06-28 22:16:41 +0000253
254 // In Microsoft ABI, a non-member function name is mangled this way.
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000255 std::string Prefix = ("?" + U->getName() + "@@Y").str();
Rui Ueyamaf5313b32015-06-28 22:16:41 +0000256 for (auto I : Symtab) {
257 StringRef Name = I.first;
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000258 Symbol *New = I.second;
Rui Ueyamaf5313b32015-06-28 22:16:41 +0000259 if (!Name.startswith(Prefix))
260 continue;
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000261 U->WeakAlias = New->Body;
262 if (auto *L = dyn_cast<Lazy>(New->Body))
263 addMemberFile(L);
264 return;
Rui Ueyamaf5313b32015-06-28 22:16:41 +0000265 }
Rui Ueyamaf5313b32015-06-28 22:16:41 +0000266}
267
Rui Ueyama6bf638e2015-07-02 00:04:14 +0000268Undefined *SymbolTable::addUndefined(StringRef Name) {
269 auto *U = new (Alloc) Undefined(Name);
270 addSymbol(U);
271 return U;
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000272}
273
Rui Ueyama360bace2015-05-31 22:31:31 +0000274// Resolve To, and make From an alias to To.
275std::error_code SymbolTable::rename(StringRef From, StringRef To) {
Rui Ueyama4d2834b2015-06-19 19:23:43 +0000276 // If From is not undefined, do nothing.
277 // Otherwise, rename it to see if To can be resolved instead.
278 auto It = Symtab.find(From);
279 if (It == Symtab.end())
280 return std::error_code();
281 Symbol *Sym = It->second;
282 if (!isa<Undefined>(Sym->Body))
283 return std::error_code();
Rui Ueyama360bace2015-05-31 22:31:31 +0000284 SymbolBody *Body = new (Alloc) Undefined(To);
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000285 if (auto EC = addSymbol(Body))
Rui Ueyama360bace2015-05-31 22:31:31 +0000286 return EC;
Rui Ueyama45044f42015-06-29 01:03:53 +0000287 SymbolBody *Repl = Body->getReplacement();
288 if (isa<Undefined>(Repl))
289 return std::error_code();
290 Sym->Body = Repl;
Rui Ueyama4d2834b2015-06-19 19:23:43 +0000291 Body->setBackref(Sym);
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000292 ++Version;
Rui Ueyama360bace2015-05-31 22:31:31 +0000293 return std::error_code();
294}
295
Peter Collingbournebe549552015-06-26 18:58:24 +0000296void SymbolTable::printMap(llvm::raw_ostream &OS) {
297 for (ObjectFile *File : ObjectFiles) {
298 OS << File->getShortName() << ":\n";
299 for (SymbolBody *Body : File->getSymbols())
300 if (auto *R = dyn_cast<DefinedRegular>(Body))
301 if (R->isLive())
302 OS << Twine::utohexstr(Config->ImageBase + R->getRVA())
303 << " " << R->getName() << "\n";
304 }
305}
306
Peter Collingbourne60c16162015-06-01 20:10:10 +0000307std::error_code SymbolTable::addCombinedLTOObject() {
308 if (BitcodeFiles.empty())
309 return std::error_code();
310
Peter Collingbourne60c16162015-06-01 20:10:10 +0000311 // Create an object file and add it to the symbol table by replacing any
312 // DefinedBitcode symbols with the definitions in the object file.
Rui Ueyamaefba7812015-06-09 17:52:17 +0000313 LTOCodeGenerator CG;
314 auto FileOrErr = createLTOObject(&CG);
315 if (auto EC = FileOrErr.getError())
Peter Collingbourne60c16162015-06-01 20:10:10 +0000316 return EC;
Rui Ueyamaefba7812015-06-09 17:52:17 +0000317 ObjectFile *Obj = FileOrErr.get();
318
Peter Collingbourne60c16162015-06-01 20:10:10 +0000319 for (SymbolBody *Body : Obj->getSymbols()) {
320 if (!Body->isExternal())
321 continue;
Peter Collingbourned9e4e982015-06-09 02:53:09 +0000322 // Find an existing Symbol. We should not see any new undefined symbols at
323 // this point.
Peter Collingbourne60c16162015-06-01 20:10:10 +0000324 StringRef Name = Body->getName();
Peter Collingbourned9e4e982015-06-09 02:53:09 +0000325 Symbol *&Sym = Symtab[Name];
Peter Collingbourne60c16162015-06-01 20:10:10 +0000326 if (!Sym) {
Peter Collingbourned9e4e982015-06-09 02:53:09 +0000327 if (!isa<Defined>(Body)) {
328 llvm::errs() << "LTO: undefined symbol: " << Name << '\n';
329 return make_error_code(LLDError::BrokenFile);
330 }
331 Sym = new (Alloc) Symbol(Body);
332 Body->setBackref(Sym);
333 continue;
Peter Collingbourne60c16162015-06-01 20:10:10 +0000334 }
335 Body->setBackref(Sym);
336
337 if (isa<DefinedBitcode>(Sym->Body)) {
338 // The symbol should now be defined.
339 if (!isa<Defined>(Body)) {
340 llvm::errs() << "LTO: undefined symbol: " << Name << '\n';
341 return make_error_code(LLDError::BrokenFile);
342 }
343 Sym->Body = Body;
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000344 continue;
Peter Collingbourne60c16162015-06-01 20:10:10 +0000345 }
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000346 if (auto *L = dyn_cast<Lazy>(Sym->Body)) {
347 // We may see new references to runtime library symbols such as __chkstk
348 // here. These symbols must be wholly defined in non-bitcode files.
349 if (auto EC = addMemberFile(L))
Peter Collingbourne2ed4c8f2015-06-24 00:12:34 +0000350 return EC;
Rui Ueyama8d3010a2015-06-30 19:35:21 +0000351 continue;
352 }
353 SymbolBody *Existing = Sym->Body;
354 int Comp = Existing->compare(Body);
355 if (Comp == 0) {
356 llvm::errs() << "LTO: unexpected duplicate symbol: " << Name << "\n";
357 return make_error_code(LLDError::BrokenFile);
358 }
359 if (Comp < 0)
360 Sym->Body = Body;
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000361 }
362
363 size_t NumBitcodeFiles = BitcodeFiles.size();
364 if (auto EC = run())
365 return EC;
366 if (BitcodeFiles.size() != NumBitcodeFiles) {
367 llvm::errs() << "LTO: late loaded symbol created new bitcode reference\n";
368 return make_error_code(LLDError::BrokenFile);
Peter Collingbourne60c16162015-06-01 20:10:10 +0000369 }
370
Peter Collingbourne73b75e32015-06-09 04:29:54 +0000371 // New runtime library symbol references may have created undefined references.
372 if (reportRemainingUndefines())
373 return make_error_code(LLDError::BrokenFile);
Peter Collingbourne60c16162015-06-01 20:10:10 +0000374 return std::error_code();
375}
376
Rui Ueyamaefba7812015-06-09 17:52:17 +0000377// Combine and compile bitcode files and then return the result
378// as a regular COFF object file.
379ErrorOr<ObjectFile *> SymbolTable::createLTOObject(LTOCodeGenerator *CG) {
380 // All symbols referenced by non-bitcode objects must be preserved.
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000381 for (ObjectFile *File : ObjectFiles)
Rui Ueyamaefba7812015-06-09 17:52:17 +0000382 for (SymbolBody *Body : File->getSymbols())
383 if (auto *S = dyn_cast<DefinedBitcode>(Body->getReplacement()))
384 CG->addMustPreserveSymbol(S->getName());
385
Peter Collingbourne1b6fd1f2015-06-11 21:49:54 +0000386 // Likewise for bitcode symbols which we initially resolved to non-bitcode.
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000387 for (BitcodeFile *File : BitcodeFiles)
Peter Collingbourne1b6fd1f2015-06-11 21:49:54 +0000388 for (SymbolBody *Body : File->getSymbols())
389 if (isa<DefinedBitcode>(Body) &&
390 !isa<DefinedBitcode>(Body->getReplacement()))
391 CG->addMustPreserveSymbol(Body->getName());
392
Rui Ueyamaefba7812015-06-09 17:52:17 +0000393 // Likewise for other symbols that must be preserved.
Rui Ueyama18f8d2c2015-07-02 00:21:08 +0000394 for (Undefined *U : Config->GCRoot)
395 if (isa<DefinedBitcode>(U->getReplacement()))
396 CG->addMustPreserveSymbol(U->getName());
Rui Ueyamaefba7812015-06-09 17:52:17 +0000397
398 CG->setModule(BitcodeFiles[0]->releaseModule());
399 for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I)
400 CG->addModule(BitcodeFiles[I]->getModule());
401
402 std::string ErrMsg;
403 LTOMB = CG->compile(false, false, false, ErrMsg); // take MB ownership
404 if (!LTOMB) {
405 llvm::errs() << ErrMsg << '\n';
406 return make_error_code(LLDError::BrokenFile);
407 }
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000408 auto *Obj = new ObjectFile(LTOMB->getMemBufferRef());
409 Files.emplace_back(Obj);
410 ObjectFiles.push_back(Obj);
Rui Ueyamaefba7812015-06-09 17:52:17 +0000411 if (auto EC = Obj->parse())
412 return EC;
413 return Obj;
414}
415
Rui Ueyama411c63602015-05-28 19:09:30 +0000416} // namespace coff
417} // namespace lld