blob: aae6b22b4a09fb12e806664302e98295a092d9bb [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"
Rui Ueyama411c63602015-05-28 19:09:30 +000014#include "llvm/ADT/STLExtras.h"
Peter Collingbourne60c16162015-06-01 20:10:10 +000015#include "llvm/LTO/LTOCodeGenerator.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000016#include "llvm/Support/Debug.h"
17#include "llvm/Support/raw_ostream.h"
18
Rui Ueyamad68ff342015-05-31 03:57:30 +000019using namespace llvm;
20
Rui Ueyama411c63602015-05-28 19:09:30 +000021namespace lld {
22namespace coff {
23
24SymbolTable::SymbolTable() {
Rui Ueyama07e661f2015-06-03 05:39:12 +000025 resolve(new (Alloc) DefinedAbsolute("__ImageBase", Config->ImageBase));
Rui Ueyama5cff6852015-05-31 03:34:08 +000026 if (!Config->EntryName.empty())
Rui Ueyama07e661f2015-06-03 05:39:12 +000027 resolve(new (Alloc) Undefined(Config->EntryName));
Rui Ueyama411c63602015-05-28 19:09:30 +000028}
29
Rui Ueyama0d2e9992015-06-23 23:56:39 +000030void SymbolTable::addFile(std::unique_ptr<InputFile> File) {
31 Files.push_back(std::move(File));
Rui Ueyama411c63602015-05-28 19:09:30 +000032}
33
Rui Ueyama0d2e9992015-06-23 23:56:39 +000034std::error_code SymbolTable::run() {
35 while (FileIdx < Files.size()) {
36 InputFile *F = Files[FileIdx++].get();
37 if (Config->Verbose)
38 llvm::outs() << "Reading " << F->getShortName() << "\n";
39 if (auto EC = F->parse())
40 return EC;
41 if (auto *P = dyn_cast<ObjectFile>(F)) {
42 ObjectFiles.push_back(P);
43 } else if (auto *P = dyn_cast<ArchiveFile>(F)) {
44 ArchiveFiles.push_back(P);
45 } else if (auto *P = dyn_cast<BitcodeFile>(F)) {
46 BitcodeFiles.push_back(P);
47 } else {
48 ImportFiles.push_back(cast<ImportFile>(F));
Rui Ueyamaeeae5dd2015-06-08 06:00:10 +000049 }
Rui Ueyama0d2e9992015-06-23 23:56:39 +000050
51 for (SymbolBody *B : F->getSymbols())
52 if (B->isExternal())
53 if (auto EC = resolve(B))
54 return EC;
55
56 // If a object file contains .drectve section,
57 // read that and add files listed there.
58 StringRef S = F->getDirectives();
59 if (!S.empty())
60 if (auto EC = Driver->parseDirectives(S))
61 return EC;
Rui Ueyamaeeae5dd2015-06-08 06:00:10 +000062 }
Peter Collingbourneace2f092015-06-06 02:00:45 +000063 return std::error_code();
64}
65
Rui Ueyama411c63602015-05-28 19:09:30 +000066bool SymbolTable::reportRemainingUndefines() {
67 bool Ret = false;
68 for (auto &I : Symtab) {
69 Symbol *Sym = I.second;
70 auto *Undef = dyn_cast<Undefined>(Sym->Body);
71 if (!Undef)
72 continue;
Rui Ueyamad7666532015-06-25 02:21:44 +000073 StringRef Name = Undef->getName();
Rui Ueyama411c63602015-05-28 19:09:30 +000074 if (SymbolBody *Alias = Undef->getWeakAlias()) {
75 Sym->Body = Alias->getReplacement();
76 if (!isa<Defined>(Sym->Body)) {
77 // Aliases are yet another symbols pointed by other symbols
78 // that could also remain undefined.
Rui Ueyamad7666532015-06-25 02:21:44 +000079 llvm::errs() << "undefined symbol: " << Name << "\n";
Rui Ueyama411c63602015-05-28 19:09:30 +000080 Ret = true;
81 }
82 continue;
83 }
Rui Ueyamad7666532015-06-25 02:21:44 +000084 // If we can resolve a symbol by removing __imp_ prefix, do that.
85 // This odd rule is for compatibility with MSVC linker.
86 if (Name.startswith("__imp_")) {
87 if (Defined *Imp = find(Name.substr(strlen("__imp_")))) {
Rui Ueyama88e0f922015-06-25 03:31:47 +000088 auto *S = new (Alloc) DefinedLocalImport(Name, Imp);
89 LocalImportChunks.push_back(S->getChunk());
90 Sym->Body = S;
Rui Ueyamad7666532015-06-25 02:21:44 +000091 continue;
92 }
93 }
94 llvm::errs() << "undefined symbol: " << Name << "\n";
Rui Ueyama95925fd2015-06-28 19:35:15 +000095 // Remaining undefined symbols are not fatal if /force is specified.
96 // They are replaced with dummy defined symbols.
97 if (Config->Force) {
98 Sym->Body = new (Alloc) DefinedAbsolute(Name, 0);
99 continue;
100 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000101 Ret = true;
102 }
103 return Ret;
104}
105
106// This function resolves conflicts if there's an existing symbol with
107// the same name. Decisions are made based on symbol type.
108std::error_code SymbolTable::resolve(SymbolBody *New) {
109 // Find an existing Symbol or create and insert a new one.
110 StringRef Name = New->getName();
111 Symbol *&Sym = Symtab[Name];
112 if (!Sym) {
113 Sym = new (Alloc) Symbol(New);
114 New->setBackref(Sym);
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000115 ++Version;
Rui Ueyama411c63602015-05-28 19:09:30 +0000116 return std::error_code();
117 }
118 New->setBackref(Sym);
119
120 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
121 // equivalent (conflicting), or more preferable, respectively.
122 SymbolBody *Existing = Sym->Body;
123 int comp = Existing->compare(New);
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000124 if (comp < 0) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000125 Sym->Body = New;
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000126 ++Version;
127 }
Rui Ueyama8fd9fb92015-06-01 02:58:15 +0000128 if (comp == 0) {
Rui Ueyama68633f12015-06-25 23:22:00 +0000129 llvm::errs() << "duplicate symbol: " << Existing->getDebugName()
130 << " and " << New->getDebugName() << "\n";
Rui Ueyama8fd9fb92015-06-01 02:58:15 +0000131 return make_error_code(LLDError::DuplicateSymbols);
132 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000133
134 // If we have an Undefined symbol for a Lazy symbol, we need
135 // to read an archive member to replace the Lazy symbol with
136 // a Defined symbol.
137 if (isa<Undefined>(Existing) || isa<Undefined>(New))
138 if (auto *B = dyn_cast<Lazy>(Sym->Body))
139 return addMemberFile(B);
140 return std::error_code();
141}
142
143// Reads an archive member file pointed by a given symbol.
144std::error_code SymbolTable::addMemberFile(Lazy *Body) {
145 auto FileOrErr = Body->getMember();
146 if (auto EC = FileOrErr.getError())
147 return EC;
148 std::unique_ptr<InputFile> File = std::move(FileOrErr.get());
149
150 // getMember returns an empty buffer if the member was already
151 // read from the library.
152 if (!File)
153 return std::error_code();
154 if (Config->Verbose)
Rui Ueyama5b2588a2015-06-08 05:43:50 +0000155 llvm::outs() << "Loaded " << File->getShortName() << " for "
Rui Ueyama411c63602015-05-28 19:09:30 +0000156 << Body->getName() << "\n";
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000157 addFile(std::move(File));
158 return std::error_code();
Rui Ueyama411c63602015-05-28 19:09:30 +0000159}
160
161std::vector<Chunk *> SymbolTable::getChunks() {
162 std::vector<Chunk *> Res;
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000163 for (ObjectFile *File : ObjectFiles) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000164 std::vector<Chunk *> &V = File->getChunks();
165 Res.insert(Res.end(), V.begin(), V.end());
166 }
167 return Res;
168}
169
Rui Ueyama5cff6852015-05-31 03:34:08 +0000170Defined *SymbolTable::find(StringRef Name) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000171 auto It = Symtab.find(Name);
172 if (It == Symtab.end())
173 return nullptr;
Rui Ueyama5cff6852015-05-31 03:34:08 +0000174 if (auto *Def = dyn_cast<Defined>(It->second->Body))
175 return Def;
176 return nullptr;
177}
178
Rui Ueyama23ed96d2015-06-18 17:29:50 +0000179std::error_code SymbolTable::resolveLazy(StringRef Name) {
Rui Ueyamaae369852015-06-18 00:40:33 +0000180 auto It = Symtab.find(Name);
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000181 if (It == Symtab.end())
182 return std::error_code();
183 if (auto *B = dyn_cast<Lazy>(It->second->Body)) {
184 if (auto EC = addMemberFile(B))
185 return EC;
186 return run();
187 }
Rui Ueyamaae369852015-06-18 00:40:33 +0000188 return std::error_code();
189}
190
Rui Ueyama80b56892015-05-31 16:10:50 +0000191// Windows specific -- Link default entry point name.
Rui Ueyama5cff6852015-05-31 03:34:08 +0000192ErrorOr<StringRef> SymbolTable::findDefaultEntry() {
Rui Ueyama80b56892015-05-31 16:10:50 +0000193 // User-defined main functions and their corresponding entry points.
Rui Ueyama5cff6852015-05-31 03:34:08 +0000194 static const char *Entries[][2] = {
Rui Ueyama5cff6852015-05-31 03:34:08 +0000195 {"main", "mainCRTStartup"},
196 {"wmain", "wmainCRTStartup"},
197 {"WinMain", "WinMainCRTStartup"},
198 {"wWinMain", "wWinMainCRTStartup"},
199 };
Rui Ueyama80b56892015-05-31 16:10:50 +0000200 for (auto E : Entries) {
Rui Ueyama23ed96d2015-06-18 17:29:50 +0000201 resolveLazy(E[1]);
Rui Ueyama80b56892015-05-31 16:10:50 +0000202 if (find(E[1]))
203 return StringRef(E[1]);
204 if (!find(E[0]))
Rui Ueyama5cff6852015-05-31 03:34:08 +0000205 continue;
Rui Ueyama07e661f2015-06-03 05:39:12 +0000206 if (auto EC = resolve(new (Alloc) Undefined(E[1])))
Rui Ueyama5cff6852015-05-31 03:34:08 +0000207 return EC;
Rui Ueyama80b56892015-05-31 16:10:50 +0000208 return StringRef(E[1]);
Rui Ueyama5cff6852015-05-31 03:34:08 +0000209 }
Rui Ueyama8fd9fb92015-06-01 02:58:15 +0000210 llvm::errs() << "entry point must be defined\n";
211 return make_error_code(LLDError::InvalidOption);
Rui Ueyama5cff6852015-05-31 03:34:08 +0000212}
213
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000214std::error_code SymbolTable::addUndefined(StringRef Name) {
Rui Ueyama07e661f2015-06-03 05:39:12 +0000215 return resolve(new (Alloc) Undefined(Name));
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000216}
217
Rui Ueyama360bace2015-05-31 22:31:31 +0000218// Resolve To, and make From an alias to To.
219std::error_code SymbolTable::rename(StringRef From, StringRef To) {
Rui Ueyama4d2834b2015-06-19 19:23:43 +0000220 // If From is not undefined, do nothing.
221 // Otherwise, rename it to see if To can be resolved instead.
222 auto It = Symtab.find(From);
223 if (It == Symtab.end())
224 return std::error_code();
225 Symbol *Sym = It->second;
226 if (!isa<Undefined>(Sym->Body))
227 return std::error_code();
Rui Ueyama360bace2015-05-31 22:31:31 +0000228 SymbolBody *Body = new (Alloc) Undefined(To);
229 if (auto EC = resolve(Body))
230 return EC;
Rui Ueyama4d2834b2015-06-19 19:23:43 +0000231 Sym->Body = Body->getReplacement();
232 Body->setBackref(Sym);
Rui Ueyama573bf7d2015-06-19 21:12:48 +0000233 ++Version;
Rui Ueyama360bace2015-05-31 22:31:31 +0000234 return std::error_code();
235}
236
Peter Collingbournebe549552015-06-26 18:58:24 +0000237void SymbolTable::printMap(llvm::raw_ostream &OS) {
238 for (ObjectFile *File : ObjectFiles) {
239 OS << File->getShortName() << ":\n";
240 for (SymbolBody *Body : File->getSymbols())
241 if (auto *R = dyn_cast<DefinedRegular>(Body))
242 if (R->isLive())
243 OS << Twine::utohexstr(Config->ImageBase + R->getRVA())
244 << " " << R->getName() << "\n";
245 }
246}
247
Peter Collingbourne60c16162015-06-01 20:10:10 +0000248std::error_code SymbolTable::addCombinedLTOObject() {
249 if (BitcodeFiles.empty())
250 return std::error_code();
251
Peter Collingbourne60c16162015-06-01 20:10:10 +0000252 // Create an object file and add it to the symbol table by replacing any
253 // DefinedBitcode symbols with the definitions in the object file.
Rui Ueyamaefba7812015-06-09 17:52:17 +0000254 LTOCodeGenerator CG;
255 auto FileOrErr = createLTOObject(&CG);
256 if (auto EC = FileOrErr.getError())
Peter Collingbourne60c16162015-06-01 20:10:10 +0000257 return EC;
Rui Ueyamaefba7812015-06-09 17:52:17 +0000258 ObjectFile *Obj = FileOrErr.get();
259
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000260 // Skip the combined object file as the file is processed below
261 // rather than by run().
262 ++FileIdx;
263
Peter Collingbourne60c16162015-06-01 20:10:10 +0000264 for (SymbolBody *Body : Obj->getSymbols()) {
265 if (!Body->isExternal())
266 continue;
Peter Collingbourned9e4e982015-06-09 02:53:09 +0000267 // Find an existing Symbol. We should not see any new undefined symbols at
268 // this point.
Peter Collingbourne60c16162015-06-01 20:10:10 +0000269 StringRef Name = Body->getName();
Peter Collingbourned9e4e982015-06-09 02:53:09 +0000270 Symbol *&Sym = Symtab[Name];
Peter Collingbourne60c16162015-06-01 20:10:10 +0000271 if (!Sym) {
Peter Collingbourned9e4e982015-06-09 02:53:09 +0000272 if (!isa<Defined>(Body)) {
273 llvm::errs() << "LTO: undefined symbol: " << Name << '\n';
274 return make_error_code(LLDError::BrokenFile);
275 }
276 Sym = new (Alloc) Symbol(Body);
277 Body->setBackref(Sym);
278 continue;
Peter Collingbourne60c16162015-06-01 20:10:10 +0000279 }
280 Body->setBackref(Sym);
281
282 if (isa<DefinedBitcode>(Sym->Body)) {
283 // The symbol should now be defined.
284 if (!isa<Defined>(Body)) {
285 llvm::errs() << "LTO: undefined symbol: " << Name << '\n';
286 return make_error_code(LLDError::BrokenFile);
287 }
288 Sym->Body = Body;
Peter Collingbourne1b6fd1f2015-06-11 21:49:54 +0000289 } else {
290 int comp = Sym->Body->compare(Body);
291 if (comp < 0)
292 Sym->Body = Body;
293 if (comp == 0) {
294 llvm::errs() << "LTO: unexpected duplicate symbol: " << Name << "\n";
295 return make_error_code(LLDError::BrokenFile);
296 }
Peter Collingbourne60c16162015-06-01 20:10:10 +0000297 }
Peter Collingbourne73b75e32015-06-09 04:29:54 +0000298
299 // We may see new references to runtime library symbols such as __chkstk
300 // here. These symbols must be wholly defined in non-bitcode files.
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000301 if (auto *B = dyn_cast<Lazy>(Sym->Body))
Peter Collingbourne2ed4c8f2015-06-24 00:12:34 +0000302 if (auto EC = addMemberFile(B))
303 return EC;
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000304 }
305
306 size_t NumBitcodeFiles = BitcodeFiles.size();
307 if (auto EC = run())
308 return EC;
309 if (BitcodeFiles.size() != NumBitcodeFiles) {
310 llvm::errs() << "LTO: late loaded symbol created new bitcode reference\n";
311 return make_error_code(LLDError::BrokenFile);
Peter Collingbourne60c16162015-06-01 20:10:10 +0000312 }
313
Peter Collingbourne73b75e32015-06-09 04:29:54 +0000314 // New runtime library symbol references may have created undefined references.
315 if (reportRemainingUndefines())
316 return make_error_code(LLDError::BrokenFile);
Peter Collingbourne60c16162015-06-01 20:10:10 +0000317 return std::error_code();
318}
319
Rui Ueyamaefba7812015-06-09 17:52:17 +0000320// Combine and compile bitcode files and then return the result
321// as a regular COFF object file.
322ErrorOr<ObjectFile *> SymbolTable::createLTOObject(LTOCodeGenerator *CG) {
323 // All symbols referenced by non-bitcode objects must be preserved.
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000324 for (ObjectFile *File : ObjectFiles)
Rui Ueyamaefba7812015-06-09 17:52:17 +0000325 for (SymbolBody *Body : File->getSymbols())
326 if (auto *S = dyn_cast<DefinedBitcode>(Body->getReplacement()))
327 CG->addMustPreserveSymbol(S->getName());
328
Peter Collingbourne1b6fd1f2015-06-11 21:49:54 +0000329 // Likewise for bitcode symbols which we initially resolved to non-bitcode.
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000330 for (BitcodeFile *File : BitcodeFiles)
Peter Collingbourne1b6fd1f2015-06-11 21:49:54 +0000331 for (SymbolBody *Body : File->getSymbols())
332 if (isa<DefinedBitcode>(Body) &&
333 !isa<DefinedBitcode>(Body->getReplacement()))
334 CG->addMustPreserveSymbol(Body->getName());
335
Rui Ueyamaefba7812015-06-09 17:52:17 +0000336 // Likewise for other symbols that must be preserved.
337 for (StringRef Name : Config->GCRoots)
338 if (isa<DefinedBitcode>(Symtab[Name]->Body))
339 CG->addMustPreserveSymbol(Name);
340
341 CG->setModule(BitcodeFiles[0]->releaseModule());
342 for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I)
343 CG->addModule(BitcodeFiles[I]->getModule());
344
345 std::string ErrMsg;
346 LTOMB = CG->compile(false, false, false, ErrMsg); // take MB ownership
347 if (!LTOMB) {
348 llvm::errs() << ErrMsg << '\n';
349 return make_error_code(LLDError::BrokenFile);
350 }
Rui Ueyama0d2e9992015-06-23 23:56:39 +0000351 auto *Obj = new ObjectFile(LTOMB->getMemBufferRef());
352 Files.emplace_back(Obj);
353 ObjectFiles.push_back(Obj);
Rui Ueyamaefba7812015-06-09 17:52:17 +0000354 if (auto EC = Obj->parse())
355 return EC;
356 return Obj;
357}
358
Rui Ueyama411c63602015-05-28 19:09:30 +0000359} // namespace coff
360} // namespace lld