1 //===- SymbolTable.cpp ----------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "SymbolTable.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/LTO/legacy/LTOCodeGenerator.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
29 void SymbolTable::addFile(InputFile *File) {
31 outs() << "Reading " << toString(File) << "\n";
34 MachineTypes MT = File->getMachineType();
35 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
37 } else if (MT != IMAGE_FILE_MACHINE_UNKNOWN && Config->Machine != MT) {
38 fatal(toString(File) + ": machine type " + machineToStr(MT) +
39 " conflicts with " + machineToStr(Config->Machine));
42 if (auto *F = dyn_cast<ObjectFile>(File)) {
43 ObjectFiles.push_back(F);
44 } else if (auto *F = dyn_cast<BitcodeFile>(File)) {
45 BitcodeFiles.push_back(F);
46 } else if (auto *F = dyn_cast<ImportFile>(File)) {
47 ImportFiles.push_back(F);
50 StringRef S = File->getDirectives();
55 outs() << "Directives: " << toString(File) << ": " << S << "\n";
56 Driver->parseDirectives(S);
59 void SymbolTable::reportRemainingUndefines() {
60 SmallPtrSet<SymbolBody *, 8> Undefs;
61 for (auto &I : Symtab) {
62 Symbol *Sym = I.second;
63 auto *Undef = dyn_cast<Undefined>(Sym->body());
66 if (!Sym->IsUsedInRegularObj)
68 StringRef Name = Undef->getName();
69 // A weak alias may have been resolved, so check for that.
70 if (Defined *D = Undef->getWeakAlias()) {
71 // We resolve weak aliases by replacing the alias's SymbolBody with the
72 // target's SymbolBody. This causes all SymbolBody pointers referring to
73 // the old symbol to instead refer to the new symbol. However, we can't
74 // just blindly copy sizeof(Symbol::Body) bytes from D to Sym->Body
75 // because D may be an internal symbol, and internal symbols are stored as
76 // "unparented" SymbolBodies. For that reason we need to check which type
77 // of symbol we are dealing with and copy the correct number of bytes.
78 if (isa<DefinedRegular>(D))
79 memcpy(Sym->Body.buffer, D, sizeof(DefinedRegular));
80 else if (isa<DefinedAbsolute>(D))
81 memcpy(Sym->Body.buffer, D, sizeof(DefinedAbsolute));
83 // No other internal symbols are possible.
84 Sym->Body = D->symbol()->Body;
87 // If we can resolve a symbol by removing __imp_ prefix, do that.
88 // This odd rule is for compatibility with MSVC linker.
89 if (Name.startswith("__imp_")) {
90 Symbol *Imp = find(Name.substr(strlen("__imp_")));
91 if (Imp && isa<Defined>(Imp->body())) {
92 auto *D = cast<Defined>(Imp->body());
93 replaceBody<DefinedLocalImport>(Sym, Name, D);
94 LocalImportChunks.push_back(
95 cast<DefinedLocalImport>(Sym->body())->getChunk());
99 // Remaining undefined symbols are not fatal if /force is specified.
100 // They are replaced with dummy defined symbols.
102 replaceBody<DefinedAbsolute>(Sym, Name, 0);
103 Undefs.insert(Sym->body());
107 for (SymbolBody *B : Config->GCRoot)
109 errs() << "<root>: undefined symbol: " << B->getName() << "\n";
110 for (ObjectFile *File : ObjectFiles)
111 for (SymbolBody *Sym : File->getSymbols())
112 if (Undefs.count(Sym))
113 errs() << toString(File) << ": undefined symbol: " << Sym->getName()
116 fatal("link failed");
119 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
120 Symbol *&Sym = Symtab[CachedHashStringRef(Name)];
123 Sym = make<Symbol>();
124 Sym->IsUsedInRegularObj = false;
125 Sym->PendingArchiveLoad = false;
129 Symbol *SymbolTable::addUndefined(StringRef Name, InputFile *F,
133 std::tie(S, WasInserted) = insert(Name);
134 if (!F || !isa<BitcodeFile>(F))
135 S->IsUsedInRegularObj = true;
136 if (WasInserted || (isa<Lazy>(S->body()) && IsWeakAlias)) {
137 replaceBody<Undefined>(S, Name);
140 if (auto *L = dyn_cast<Lazy>(S->body())) {
141 if (!S->PendingArchiveLoad) {
142 S->PendingArchiveLoad = true;
143 L->File->addMember(&L->Sym);
149 void SymbolTable::addLazy(ArchiveFile *F, const Archive::Symbol Sym) {
150 StringRef Name = Sym.getName();
153 std::tie(S, WasInserted) = insert(Name);
155 replaceBody<Lazy>(S, F, Sym);
158 auto *U = dyn_cast<Undefined>(S->body());
159 if (!U || U->WeakAlias || S->PendingArchiveLoad)
161 S->PendingArchiveLoad = true;
165 void SymbolTable::reportDuplicate(Symbol *Existing, InputFile *NewFile) {
166 fatal("duplicate symbol: " + toString(*Existing->body()) + " in " +
167 toString(Existing->body()->getFile()) + " and in " +
168 (NewFile ? toString(NewFile) : "(internal)"));
171 Symbol *SymbolTable::addAbsolute(StringRef N, COFFSymbolRef Sym) {
174 std::tie(S, WasInserted) = insert(N);
175 S->IsUsedInRegularObj = true;
176 if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
177 replaceBody<DefinedAbsolute>(S, N, Sym);
178 else if (!isa<DefinedCOFF>(S->body()))
179 reportDuplicate(S, nullptr);
183 Symbol *SymbolTable::addAbsolute(StringRef N, uint64_t VA) {
186 std::tie(S, WasInserted) = insert(N);
187 S->IsUsedInRegularObj = true;
188 if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
189 replaceBody<DefinedAbsolute>(S, N, VA);
190 else if (!isa<DefinedCOFF>(S->body()))
191 reportDuplicate(S, nullptr);
195 Symbol *SymbolTable::addRelative(StringRef N, uint64_t VA) {
198 std::tie(S, WasInserted) = insert(N);
199 S->IsUsedInRegularObj = true;
200 if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
201 replaceBody<DefinedRelative>(S, N, VA);
202 else if (!isa<DefinedCOFF>(S->body()))
203 reportDuplicate(S, nullptr);
207 Symbol *SymbolTable::addRegular(ObjectFile *F, COFFSymbolRef Sym,
210 F->getCOFFObj()->getSymbolName(Sym, Name);
213 std::tie(S, WasInserted) = insert(Name);
214 S->IsUsedInRegularObj = true;
215 if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
216 replaceBody<DefinedRegular>(S, F, Sym, C);
217 else if (auto *R = dyn_cast<DefinedRegular>(S->body())) {
218 if (!C->isCOMDAT() || !R->isCOMDAT())
219 reportDuplicate(S, F);
220 } else if (auto *B = dyn_cast<DefinedBitcode>(S->body())) {
221 if (B->IsReplaceable)
222 replaceBody<DefinedRegular>(S, F, Sym, C);
223 else if (!C->isCOMDAT())
224 reportDuplicate(S, F);
226 replaceBody<DefinedRegular>(S, F, Sym, C);
230 Symbol *SymbolTable::addBitcode(BitcodeFile *F, StringRef N, bool IsReplaceable) {
233 std::tie(S, WasInserted) = insert(N);
234 if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body())) {
235 replaceBody<DefinedBitcode>(S, F, N, IsReplaceable);
238 if (isa<DefinedCommon>(S->body()))
241 if (isa<DefinedRegular>(S->body()) || isa<DefinedBitcode>(S->body()))
243 reportDuplicate(S, F);
247 Symbol *SymbolTable::addCommon(ObjectFile *F, COFFSymbolRef Sym,
250 F->getCOFFObj()->getSymbolName(Sym, Name);
253 std::tie(S, WasInserted) = insert(Name);
254 S->IsUsedInRegularObj = true;
255 if (WasInserted || !isa<DefinedCOFF>(S->body()))
256 replaceBody<DefinedCommon>(S, F, Sym, C);
257 else if (auto *DC = dyn_cast<DefinedCommon>(S->body()))
258 if (Sym.getValue() > DC->getSize())
259 replaceBody<DefinedCommon>(S, F, Sym, C);
263 Symbol *SymbolTable::addImportData(StringRef N, ImportFile *F) {
266 std::tie(S, WasInserted) = insert(N);
267 S->IsUsedInRegularObj = true;
268 if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
269 replaceBody<DefinedImportData>(S, N, F);
270 else if (!isa<DefinedCOFF>(S->body()))
271 reportDuplicate(S, nullptr);
275 Symbol *SymbolTable::addImportThunk(StringRef Name, DefinedImportData *ID,
279 std::tie(S, WasInserted) = insert(Name);
280 S->IsUsedInRegularObj = true;
281 if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
282 replaceBody<DefinedImportThunk>(S, Name, ID, Machine);
283 else if (!isa<DefinedCOFF>(S->body()))
284 reportDuplicate(S, nullptr);
288 std::vector<Chunk *> SymbolTable::getChunks() {
289 std::vector<Chunk *> Res;
290 for (ObjectFile *File : ObjectFiles) {
291 std::vector<Chunk *> &V = File->getChunks();
292 Res.insert(Res.end(), V.begin(), V.end());
297 Symbol *SymbolTable::find(StringRef Name) {
298 auto It = Symtab.find(CachedHashStringRef(Name));
299 if (It == Symtab.end())
304 Symbol *SymbolTable::findUnderscore(StringRef Name) {
305 if (Config->Machine == I386)
306 return find(("_" + Name).str());
310 StringRef SymbolTable::findByPrefix(StringRef Prefix) {
311 for (auto Pair : Symtab) {
312 StringRef Name = Pair.first.val();
313 if (Name.startswith(Prefix))
319 StringRef SymbolTable::findMangle(StringRef Name) {
320 if (Symbol *Sym = find(Name))
321 if (!isa<Undefined>(Sym->body()))
323 if (Config->Machine != I386)
324 return findByPrefix(("?" + Name + "@@Y").str());
325 if (!Name.startswith("_"))
327 // Search for x86 C function.
328 StringRef S = findByPrefix((Name + "@").str());
331 // Search for x86 C++ non-member function.
332 return findByPrefix(("?" + Name.substr(1) + "@@Y").str());
335 void SymbolTable::mangleMaybe(SymbolBody *B) {
336 auto *U = dyn_cast<Undefined>(B);
337 if (!U || U->WeakAlias)
339 StringRef Alias = findMangle(U->getName());
341 U->WeakAlias = addUndefined(Alias);
344 SymbolBody *SymbolTable::addUndefined(StringRef Name) {
345 return addUndefined(Name, nullptr, false)->body();
348 void SymbolTable::printMap(llvm::raw_ostream &OS) {
349 for (ObjectFile *File : ObjectFiles) {
350 OS << toString(File) << ":\n";
351 for (SymbolBody *Body : File->getSymbols())
352 if (auto *R = dyn_cast<DefinedRegular>(Body))
353 if (R->getChunk()->isLive())
354 OS << Twine::utohexstr(Config->ImageBase + R->getRVA())
355 << " " << R->getName() << "\n";
359 void SymbolTable::addCombinedLTOObjects() {
360 if (BitcodeFiles.empty())
363 // Create an object file and add it to the symbol table by replacing any
364 // DefinedBitcode symbols with the definitions in the object file.
365 LTOCodeGenerator CG(BitcodeFile::Context);
366 CG.setOptLevel(Config->LTOOptLevel);
367 for (ObjectFile *Obj : createLTOObjects(&CG))
371 // Combine and compile bitcode files and then return the result
372 // as a vector of regular COFF object files.
373 std::vector<ObjectFile *> SymbolTable::createLTOObjects(LTOCodeGenerator *CG) {
374 // All symbols referenced by non-bitcode objects, including GC roots, must be
375 // preserved. We must also replace bitcode symbols with undefined symbols so
376 // that they may be replaced with real definitions without conflicting.
377 for (BitcodeFile *File : BitcodeFiles)
378 for (SymbolBody *Body : File->getSymbols()) {
379 if (!isa<DefinedBitcode>(Body))
381 if (Body->symbol()->IsUsedInRegularObj)
382 CG->addMustPreserveSymbol(Body->getName());
383 replaceBody<Undefined>(Body->symbol(), Body->getName());
386 CG->setModule(BitcodeFiles[0]->takeModule());
387 for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I)
388 CG->addModule(BitcodeFiles[I]->takeModule().get());
390 bool DisableVerify = true;
392 DisableVerify = false;
394 if (!CG->optimize(DisableVerify, false, false, false))
395 fatal(""); // optimize() should have emitted any error message.
397 Objs.resize(Config->LTOJobs);
398 // Use std::list to avoid invalidation of pointers in OSPtrs.
399 std::list<raw_svector_ostream> OSs;
400 std::vector<raw_pwrite_stream *> OSPtrs;
401 for (SmallString<0> &Obj : Objs) {
402 OSs.emplace_back(Obj);
403 OSPtrs.push_back(&OSs.back());
406 if (!CG->compileOptimized(OSPtrs))
407 fatal(""); // compileOptimized() should have emitted any error message.
409 std::vector<ObjectFile *> ObjFiles;
410 for (SmallString<0> &Obj : Objs) {
411 auto *ObjFile = make<ObjectFile>(MemoryBufferRef(Obj, "<LTO object>"));
412 ObjectFiles.push_back(ObjFile);
413 ObjFiles.push_back(ObjFile);