blob: 84fc40ca707282f4b3ee30f1c085a43fa2d5f9a3 [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
Peter Collingbourneace2f092015-06-06 02:00:45 +000043std::error_code SymbolTable::addDirectives(StringRef Dir) {
44 if (Dir.empty())
45 return std::error_code();
46 std::vector<std::unique_ptr<InputFile>> Libs;
47 if (auto EC = Driver->parseDirectives(Dir, &Libs))
48 return EC;
49 for (std::unique_ptr<InputFile> &Lib : Libs)
50 addFile(std::move(Lib));
51 return std::error_code();
52}
53
Rui Ueyama411c63602015-05-28 19:09:30 +000054std::error_code SymbolTable::addObject(ObjectFile *File) {
55 ObjectFiles.emplace_back(File);
56 for (SymbolBody *Body : File->getSymbols())
57 if (Body->isExternal())
58 if (auto EC = resolve(Body))
59 return EC;
60
61 // If an object file contains .drectve section, read it and add
62 // files listed in the section.
Peter Collingbourneace2f092015-06-06 02:00:45 +000063 return addDirectives(File->getDirectives());
Rui Ueyama411c63602015-05-28 19:09:30 +000064}
65
66std::error_code SymbolTable::addArchive(ArchiveFile *File) {
67 ArchiveFiles.emplace_back(File);
68 for (SymbolBody *Body : File->getSymbols())
69 if (auto EC = resolve(Body))
70 return EC;
71 return std::error_code();
72}
73
Peter Collingbourne60c16162015-06-01 20:10:10 +000074std::error_code SymbolTable::addBitcode(BitcodeFile *File) {
75 BitcodeFiles.emplace_back(File);
76 for (SymbolBody *Body : File->getSymbols())
77 if (Body->isExternal())
78 if (auto EC = resolve(Body))
79 return EC;
80
Peter Collingbourneace2f092015-06-06 02:00:45 +000081 // Add any linker directives from the module flags metadata.
82 return addDirectives(File->getDirectives());
Peter Collingbourne60c16162015-06-01 20:10:10 +000083}
84
Rui Ueyama411c63602015-05-28 19:09:30 +000085std::error_code SymbolTable::addImport(ImportFile *File) {
86 ImportFiles.emplace_back(File);
87 for (SymbolBody *Body : File->getSymbols())
88 if (auto EC = resolve(Body))
89 return EC;
90 return std::error_code();
91}
92
93bool SymbolTable::reportRemainingUndefines() {
94 bool Ret = false;
95 for (auto &I : Symtab) {
96 Symbol *Sym = I.second;
97 auto *Undef = dyn_cast<Undefined>(Sym->Body);
98 if (!Undef)
99 continue;
100 if (SymbolBody *Alias = Undef->getWeakAlias()) {
101 Sym->Body = Alias->getReplacement();
102 if (!isa<Defined>(Sym->Body)) {
103 // Aliases are yet another symbols pointed by other symbols
104 // that could also remain undefined.
105 llvm::errs() << "undefined symbol: " << Undef->getName() << "\n";
106 Ret = true;
107 }
108 continue;
109 }
110 llvm::errs() << "undefined symbol: " << Undef->getName() << "\n";
111 Ret = true;
112 }
113 return Ret;
114}
115
116// This function resolves conflicts if there's an existing symbol with
117// the same name. Decisions are made based on symbol type.
118std::error_code SymbolTable::resolve(SymbolBody *New) {
119 // Find an existing Symbol or create and insert a new one.
120 StringRef Name = New->getName();
121 Symbol *&Sym = Symtab[Name];
122 if (!Sym) {
123 Sym = new (Alloc) Symbol(New);
124 New->setBackref(Sym);
125 return std::error_code();
126 }
127 New->setBackref(Sym);
128
129 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
130 // equivalent (conflicting), or more preferable, respectively.
131 SymbolBody *Existing = Sym->Body;
132 int comp = Existing->compare(New);
133 if (comp < 0)
134 Sym->Body = New;
Rui Ueyama8fd9fb92015-06-01 02:58:15 +0000135 if (comp == 0) {
136 llvm::errs() << "duplicate symbol: " << Name << "\n";
137 return make_error_code(LLDError::DuplicateSymbols);
138 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000139
140 // If we have an Undefined symbol for a Lazy symbol, we need
141 // to read an archive member to replace the Lazy symbol with
142 // a Defined symbol.
143 if (isa<Undefined>(Existing) || isa<Undefined>(New))
144 if (auto *B = dyn_cast<Lazy>(Sym->Body))
145 return addMemberFile(B);
146 return std::error_code();
147}
148
149// Reads an archive member file pointed by a given symbol.
150std::error_code SymbolTable::addMemberFile(Lazy *Body) {
151 auto FileOrErr = Body->getMember();
152 if (auto EC = FileOrErr.getError())
153 return EC;
154 std::unique_ptr<InputFile> File = std::move(FileOrErr.get());
155
156 // getMember returns an empty buffer if the member was already
157 // read from the library.
158 if (!File)
159 return std::error_code();
160 if (Config->Verbose)
Rui Ueyama5b2588a2015-06-08 05:43:50 +0000161 llvm::outs() << "Loaded " << File->getShortName() << " for "
Rui Ueyama411c63602015-05-28 19:09:30 +0000162 << Body->getName() << "\n";
163 return addFile(std::move(File));
164}
165
166std::vector<Chunk *> SymbolTable::getChunks() {
167 std::vector<Chunk *> Res;
168 for (std::unique_ptr<ObjectFile> &File : ObjectFiles) {
169 std::vector<Chunk *> &V = File->getChunks();
170 Res.insert(Res.end(), V.begin(), V.end());
171 }
172 return Res;
173}
174
Rui Ueyama5cff6852015-05-31 03:34:08 +0000175Defined *SymbolTable::find(StringRef Name) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000176 auto It = Symtab.find(Name);
177 if (It == Symtab.end())
178 return nullptr;
Rui Ueyama5cff6852015-05-31 03:34:08 +0000179 if (auto *Def = dyn_cast<Defined>(It->second->Body))
180 return Def;
181 return nullptr;
182}
183
Rui Ueyama80b56892015-05-31 16:10:50 +0000184// Windows specific -- Link default entry point name.
Rui Ueyama5cff6852015-05-31 03:34:08 +0000185ErrorOr<StringRef> SymbolTable::findDefaultEntry() {
Rui Ueyama80b56892015-05-31 16:10:50 +0000186 // User-defined main functions and their corresponding entry points.
Rui Ueyama5cff6852015-05-31 03:34:08 +0000187 static const char *Entries[][2] = {
Rui Ueyama5cff6852015-05-31 03:34:08 +0000188 {"main", "mainCRTStartup"},
189 {"wmain", "wmainCRTStartup"},
190 {"WinMain", "WinMainCRTStartup"},
191 {"wWinMain", "wWinMainCRTStartup"},
192 };
Rui Ueyama80b56892015-05-31 16:10:50 +0000193 for (auto E : Entries) {
194 if (find(E[1]))
195 return StringRef(E[1]);
196 if (!find(E[0]))
Rui Ueyama5cff6852015-05-31 03:34:08 +0000197 continue;
Rui Ueyama07e661f2015-06-03 05:39:12 +0000198 if (auto EC = resolve(new (Alloc) Undefined(E[1])))
Rui Ueyama5cff6852015-05-31 03:34:08 +0000199 return EC;
Rui Ueyama80b56892015-05-31 16:10:50 +0000200 return StringRef(E[1]);
Rui Ueyama5cff6852015-05-31 03:34:08 +0000201 }
Rui Ueyama8fd9fb92015-06-01 02:58:15 +0000202 llvm::errs() << "entry point must be defined\n";
203 return make_error_code(LLDError::InvalidOption);
Rui Ueyama5cff6852015-05-31 03:34:08 +0000204}
205
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000206std::error_code SymbolTable::addUndefined(StringRef Name) {
Rui Ueyama07e661f2015-06-03 05:39:12 +0000207 return resolve(new (Alloc) Undefined(Name));
Rui Ueyamae042fa9a2015-05-31 19:55:40 +0000208}
209
Rui Ueyama360bace2015-05-31 22:31:31 +0000210// Resolve To, and make From an alias to To.
211std::error_code SymbolTable::rename(StringRef From, StringRef To) {
212 SymbolBody *Body = new (Alloc) Undefined(To);
213 if (auto EC = resolve(Body))
214 return EC;
215 Symtab[From]->Body = Body->getReplacement();
216 return std::error_code();
217}
218
Rui Ueyama411c63602015-05-28 19:09:30 +0000219void SymbolTable::dump() {
220 for (auto &P : Symtab) {
221 Symbol *Ref = P.second;
222 if (auto *Body = dyn_cast<Defined>(Ref->Body))
223 llvm::dbgs() << Twine::utohexstr(Config->ImageBase + Body->getRVA())
224 << " " << Body->getName() << "\n";
225 }
226}
227
Peter Collingbourne60c16162015-06-01 20:10:10 +0000228std::error_code SymbolTable::addCombinedLTOObject() {
229 if (BitcodeFiles.empty())
230 return std::error_code();
231
232 llvm::LTOCodeGenerator CG;
Peter Collingbourne60c16162015-06-01 20:10:10 +0000233
234 // All symbols referenced by non-bitcode objects must be preserved.
235 for (std::unique_ptr<ObjectFile> &File : ObjectFiles)
236 for (SymbolBody *Body : File->getSymbols())
237 if (auto *S = dyn_cast<DefinedBitcode>(Body->getReplacement()))
Rui Ueyamaeb262ce2015-06-04 02:12:16 +0000238 CG.addMustPreserveSymbol(S->getName());
Peter Collingbourne60c16162015-06-01 20:10:10 +0000239
Rui Ueyamaeb262ce2015-06-04 02:12:16 +0000240 // Likewise for other symbols that must be preserved.
241 for (StringRef Name : Config->GCRoots)
242 if (isa<DefinedBitcode>(Symtab[Name]->Body))
243 CG.addMustPreserveSymbol(Name);
Peter Collingbourne60c16162015-06-01 20:10:10 +0000244
245 CG.setModule(BitcodeFiles[0]->releaseModule());
246 for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I)
247 CG.addModule(BitcodeFiles[I]->getModule());
248
249 std::string ErrMsg;
250 LTOObjectFile = CG.compile(false, false, false, ErrMsg);
251 if (!LTOObjectFile) {
252 llvm::errs() << ErrMsg << '\n';
253 return make_error_code(LLDError::BrokenFile);
254 }
255
256 // Create an object file and add it to the symbol table by replacing any
257 // DefinedBitcode symbols with the definitions in the object file.
258 auto Obj = new ObjectFile(LTOObjectFile->getMemBufferRef());
259 ObjectFiles.emplace_back(Obj);
260 if (auto EC = Obj->parse())
261 return EC;
262 for (SymbolBody *Body : Obj->getSymbols()) {
263 if (!Body->isExternal())
264 continue;
265
266 // Find an existing Symbol. We should not see any new symbols at this point.
267 StringRef Name = Body->getName();
268 Symbol *Sym = Symtab[Name];
269 if (!Sym) {
270 llvm::errs() << "LTO: unexpected new symbol: " << Name << '\n';
271 return make_error_code(LLDError::BrokenFile);
272 }
273 Body->setBackref(Sym);
274
275 if (isa<DefinedBitcode>(Sym->Body)) {
276 // The symbol should now be defined.
277 if (!isa<Defined>(Body)) {
278 llvm::errs() << "LTO: undefined symbol: " << Name << '\n';
279 return make_error_code(LLDError::BrokenFile);
280 }
281 Sym->Body = Body;
282 }
283 }
284
285 return std::error_code();
286}
287
Rui Ueyama411c63602015-05-28 19:09:30 +0000288} // namespace coff
289} // namespace lld