blob: 35042e5bad2d66cd70b3afa41cfcf15b05714fc4 [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
30std::error_code SymbolTable::addFile(std::unique_ptr<InputFile> File) {
31 if (auto EC = File->parse())
32 return EC;
33 InputFile *FileP = File.release();
34 if (auto *P = dyn_cast<ObjectFile>(FileP))
35 return addObject(P);
36 if (auto *P = dyn_cast<ArchiveFile>(FileP))
37 return addArchive(P);
Peter Collingbourne60c16162015-06-01 20:10:10 +000038 if (auto *P = dyn_cast<BitcodeFile>(FileP))
39 return addBitcode(P);
Rui Ueyama411c63602015-05-28 19:09:30 +000040 return addImport(cast<ImportFile>(FileP));
41}
42
43std::error_code SymbolTable::addObject(ObjectFile *File) {
44 ObjectFiles.emplace_back(File);
45 for (SymbolBody *Body : File->getSymbols())
46 if (Body->isExternal())
47 if (auto EC = resolve(Body))
48 return EC;
49
50 // If an object file contains .drectve section, read it and add
51 // files listed in the section.
52 StringRef Dir = File->getDirectives();
53 if (!Dir.empty()) {
54 std::vector<std::unique_ptr<InputFile>> Libs;
Rui Ueyamaa9cbbf82015-05-31 19:17:09 +000055 if (auto EC = Driver->parseDirectives(Dir, &Libs))
Rui Ueyama411c63602015-05-28 19:09:30 +000056 return EC;
57 for (std::unique_ptr<InputFile> &Lib : Libs)
58 addFile(std::move(Lib));
59 }
60 return std::error_code();
61}
62
63std::error_code SymbolTable::addArchive(ArchiveFile *File) {
64 ArchiveFiles.emplace_back(File);
65 for (SymbolBody *Body : File->getSymbols())
66 if (auto EC = resolve(Body))
67 return EC;
68 return std::error_code();
69}
70
Peter Collingbourne60c16162015-06-01 20:10:10 +000071std::error_code SymbolTable::addBitcode(BitcodeFile *File) {
72 BitcodeFiles.emplace_back(File);
73 for (SymbolBody *Body : File->getSymbols())
74 if (Body->isExternal())
75 if (auto EC = resolve(Body))
76 return EC;
77
78 // TODO: Handle linker directives.
79 return std::error_code();
80}
81
Rui Ueyama411c63602015-05-28 19:09:30 +000082std::error_code SymbolTable::addImport(ImportFile *File) {
83 ImportFiles.emplace_back(File);
84 for (SymbolBody *Body : File->getSymbols())
85 if (auto EC = resolve(Body))
86 return EC;
87 return std::error_code();
88}
89
90bool SymbolTable::reportRemainingUndefines() {
91 bool Ret = false;
92 for (auto &I : Symtab) {
93 Symbol *Sym = I.second;
94 auto *Undef = dyn_cast<Undefined>(Sym->Body);
95 if (!Undef)
96 continue;
97 if (SymbolBody *Alias = Undef->getWeakAlias()) {
98 Sym->Body = Alias->getReplacement();
99 if (!isa<Defined>(Sym->Body)) {
100 // Aliases are yet another symbols pointed by other symbols
101 // that could also remain undefined.
102 llvm::errs() << "undefined symbol: " << Undef->getName() << "\n";
103 Ret = true;
104 }
105 continue;
106 }
107 llvm::errs() << "undefined symbol: " << Undef->getName() << "\n";
108 Ret = true;
109 }
110 return Ret;
111}
112
113// This function resolves conflicts if there's an existing symbol with
114// the same name. Decisions are made based on symbol type.
115std::error_code SymbolTable::resolve(SymbolBody *New) {
116 // Find an existing Symbol or create and insert a new one.
117 StringRef Name = New->getName();
118 Symbol *&Sym = Symtab[Name];
119 if (!Sym) {
120 Sym = new (Alloc) Symbol(New);
121 New->setBackref(Sym);
122 return std::error_code();
123 }
124 New->setBackref(Sym);
125
126 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
127 // equivalent (conflicting), or more preferable, respectively.
128 SymbolBody *Existing = Sym->Body;
129 int comp = Existing->compare(New);
130 if (comp < 0)
131 Sym->Body = New;
Rui Ueyama8fd9fb92015-06-01 02:58:15 +0000132 if (comp == 0) {
133 llvm::errs() << "duplicate symbol: " << Name << "\n";
134 return make_error_code(LLDError::DuplicateSymbols);
135 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000136
137 // If we have an Undefined symbol for a Lazy symbol, we need
138 // to read an archive member to replace the Lazy symbol with
139 // a Defined symbol.
140 if (isa<Undefined>(Existing) || isa<Undefined>(New))
141 if (auto *B = dyn_cast<Lazy>(Sym->Body))
142 return addMemberFile(B);
143 return std::error_code();
144}
145
146// Reads an archive member file pointed by a given symbol.
147std::error_code SymbolTable::addMemberFile(Lazy *Body) {
148 auto FileOrErr = Body->getMember();
149 if (auto EC = FileOrErr.getError())
150 return EC;
151 std::unique_ptr<InputFile> File = std::move(FileOrErr.get());
152
153 // getMember returns an empty buffer if the member was already
154 // read from the library.
155 if (!File)
156 return std::error_code();
157 if (Config->Verbose)
158 llvm::dbgs() << "Loaded " << File->getShortName() << " for "
159 << Body->getName() << "\n";
160 return addFile(std::move(File));
161}
162
163std::vector<Chunk *> SymbolTable::getChunks() {
164 std::vector<Chunk *> Res;
165 for (std::unique_ptr<ObjectFile> &File : ObjectFiles) {
166 std::vector<Chunk *> &V = File->getChunks();
167 Res.insert(Res.end(), V.begin(), V.end());
168 }
169 return Res;
170}
171
Rui Ueyama5cff6852015-05-31 03:34:08 +0000172Defined *SymbolTable::find(StringRef Name) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000173 auto It = Symtab.find(Name);
174 if (It == Symtab.end())
175 return nullptr;
Rui Ueyama5cff6852015-05-31 03:34:08 +0000176 if (auto *Def = dyn_cast<Defined>(It->second->Body))
177 return Def;
178 return nullptr;
179}
180
Rui Ueyama80b56892015-05-31 16:10:50 +0000181// Windows specific -- Link default entry point name.
Rui Ueyama5cff6852015-05-31 03:34:08 +0000182ErrorOr<StringRef> SymbolTable::findDefaultEntry() {
Rui Ueyama80b56892015-05-31 16:10:50 +0000183 // User-defined main functions and their corresponding entry points.
Rui Ueyama5cff6852015-05-31 03:34:08 +0000184 static const char *Entries[][2] = {
Rui Ueyama5cff6852015-05-31 03:34:08 +0000185 {"main", "mainCRTStartup"},
186 {"wmain", "wmainCRTStartup"},
187 {"WinMain", "WinMainCRTStartup"},
188 {"wWinMain", "wWinMainCRTStartup"},
189 };
Rui Ueyama80b56892015-05-31 16:10:50 +0000190 for (auto E : Entries) {
191 if (find(E[1]))
192 return StringRef(E[1]);
193 if (!find(E[0]))
Rui Ueyama5cff6852015-05-31 03:34:08 +0000194 continue;
Rui Ueyama07e661f2015-06-03 05:39:12 +0000195 if (auto EC = resolve(new (Alloc) Undefined(E[1])))
Rui Ueyama5cff6852015-05-31 03:34:08 +0000196 return EC;
Rui Ueyama80b56892015-05-31 16:10:50 +0000197 return StringRef(E[1]);
Rui Ueyama5cff6852015-05-31 03:34:08 +0000198 }
Rui Ueyama8fd9fb92015-06-01 02:58:15 +0000199 llvm::errs() << "entry point must be defined\n";
200 return make_error_code(LLDError::InvalidOption);
Rui Ueyama5cff6852015-05-31 03:34:08 +0000201}
202
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000203std::error_code SymbolTable::addUndefined(StringRef Name) {
Rui Ueyama07e661f2015-06-03 05:39:12 +0000204 return resolve(new (Alloc) Undefined(Name));
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000205}
206
Rui Ueyama360bace2015-05-31 22:31:31 +0000207// Resolve To, and make From an alias to To.
208std::error_code SymbolTable::rename(StringRef From, StringRef To) {
209 SymbolBody *Body = new (Alloc) Undefined(To);
210 if (auto EC = resolve(Body))
211 return EC;
212 Symtab[From]->Body = Body->getReplacement();
213 return std::error_code();
214}
215
Rui Ueyama411c63602015-05-28 19:09:30 +0000216void SymbolTable::dump() {
217 for (auto &P : Symtab) {
218 Symbol *Ref = P.second;
219 if (auto *Body = dyn_cast<Defined>(Ref->Body))
220 llvm::dbgs() << Twine::utohexstr(Config->ImageBase + Body->getRVA())
221 << " " << Body->getName() << "\n";
222 }
223}
224
Peter Collingbourne60c16162015-06-01 20:10:10 +0000225std::error_code SymbolTable::addCombinedLTOObject() {
226 if (BitcodeFiles.empty())
227 return std::error_code();
228
229 llvm::LTOCodeGenerator CG;
Peter Collingbourne60c16162015-06-01 20:10:10 +0000230
231 // All symbols referenced by non-bitcode objects must be preserved.
232 for (std::unique_ptr<ObjectFile> &File : ObjectFiles)
233 for (SymbolBody *Body : File->getSymbols())
234 if (auto *S = dyn_cast<DefinedBitcode>(Body->getReplacement()))
Rui Ueyamaeb262ce2015-06-04 02:12:16 +0000235 CG.addMustPreserveSymbol(S->getName());
Peter Collingbourne60c16162015-06-01 20:10:10 +0000236
Rui Ueyamaeb262ce2015-06-04 02:12:16 +0000237 // Likewise for other symbols that must be preserved.
238 for (StringRef Name : Config->GCRoots)
239 if (isa<DefinedBitcode>(Symtab[Name]->Body))
240 CG.addMustPreserveSymbol(Name);
Peter Collingbourne60c16162015-06-01 20:10:10 +0000241
242 CG.setModule(BitcodeFiles[0]->releaseModule());
243 for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I)
244 CG.addModule(BitcodeFiles[I]->getModule());
245
246 std::string ErrMsg;
247 LTOObjectFile = CG.compile(false, false, false, ErrMsg);
248 if (!LTOObjectFile) {
249 llvm::errs() << ErrMsg << '\n';
250 return make_error_code(LLDError::BrokenFile);
251 }
252
253 // Create an object file and add it to the symbol table by replacing any
254 // DefinedBitcode symbols with the definitions in the object file.
255 auto Obj = new ObjectFile(LTOObjectFile->getMemBufferRef());
256 ObjectFiles.emplace_back(Obj);
257 if (auto EC = Obj->parse())
258 return EC;
259 for (SymbolBody *Body : Obj->getSymbols()) {
260 if (!Body->isExternal())
261 continue;
262
263 // Find an existing Symbol. We should not see any new symbols at this point.
264 StringRef Name = Body->getName();
265 Symbol *Sym = Symtab[Name];
266 if (!Sym) {
267 llvm::errs() << "LTO: unexpected new symbol: " << Name << '\n';
268 return make_error_code(LLDError::BrokenFile);
269 }
270 Body->setBackref(Sym);
271
272 if (isa<DefinedBitcode>(Sym->Body)) {
273 // The symbol should now be defined.
274 if (!isa<Defined>(Body)) {
275 llvm::errs() << "LTO: undefined symbol: " << Name << '\n';
276 return make_error_code(LLDError::BrokenFile);
277 }
278 Sym->Body = Body;
279 }
280 }
281
282 return std::error_code();
283}
284
Rui Ueyama411c63602015-05-28 19:09:30 +0000285} // namespace coff
286} // namespace lld