1 //===- 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"
12 #include "Error.h"
13 #include "SymbolTable.h"
14 #include "Symbols.h"
15 #include "lld/Core/Parallel.h"
16 #include "llvm/LTO/LTOCodeGenerator.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <utility>
20 
21 using namespace llvm;
22 
23 namespace lld {
24 namespace coff {
25 
26 void SymbolTable::addFile(std::unique_ptr<InputFile> FileP) {
27 #if LLVM_ENABLE_THREADS
28   std::launch Policy = std::launch::async;
29 #else
30   std::launch Policy = std::launch::deferred;
31 #endif
32 
33   InputFile *File = FileP.get();
34   Files.push_back(std::move(FileP));
35   if (auto *F = dyn_cast<ArchiveFile>(File)) {
36     ArchiveQueue.push_back(
37         std::async(Policy, [=]() { F->parse(); return F; }));
38     return;
39   }
40   ObjectQueue.push_back(
41       std::async(Policy, [=]() { File->parse(); return File; }));
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 {
47     ImportFiles.push_back(cast<ImportFile>(File));
48   }
49 }
50 
51 void SymbolTable::step() {
52   if (queueEmpty())
53     return;
54   readObjects();
55   readArchives();
56 }
57 
58 void SymbolTable::run() {
59   while (!queueEmpty())
60     step();
61 }
62 
63 void SymbolTable::readArchives() {
64   if (ArchiveQueue.empty())
65     return;
66 
67   // Add lazy symbols to the symbol table. Lazy symbols that conflict
68   // with existing undefined symbols are accumulated in LazySyms.
69   std::vector<Symbol *> LazySyms;
70   for (std::future<ArchiveFile *> &Future : ArchiveQueue) {
71     ArchiveFile *File = Future.get();
72     if (Config->Verbose)
73       llvm::outs() << "Reading " << File->getShortName() << "\n";
74     for (Lazy &Sym : File->getLazySymbols())
75       addLazy(&Sym, &LazySyms);
76   }
77   ArchiveQueue.clear();
78 
79   // Add archive member files to ObjectQueue that should resolve
80   // existing undefined symbols.
81   for (Symbol *Sym : LazySyms)
82     addMemberFile(cast<Lazy>(Sym->Body));
83 }
84 
85 void SymbolTable::readObjects() {
86   if (ObjectQueue.empty())
87     return;
88 
89   // Add defined and undefined symbols to the symbol table.
90   std::vector<StringRef> Directives;
91   for (size_t I = 0; I < ObjectQueue.size(); ++I) {
92     InputFile *File = ObjectQueue[I].get();
93     if (Config->Verbose)
94       llvm::outs() << "Reading " << File->getShortName() << "\n";
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         addSymbol(Sym);
100     StringRef S = File->getDirectives();
101     if (!S.empty()) {
102       Directives.push_back(S);
103       if (Config->Verbose)
104         llvm::outs() << "Directives: " << File->getShortName()
105                      << ": " << S << "\n";
106     }
107   }
108   ObjectQueue.clear();
109 
110   // Parse directive sections. This may add files to
111   // ArchiveQueue and ObjectQueue.
112   for (StringRef S : Directives)
113     Driver->parseDirectives(S);
114 }
115 
116 bool SymbolTable::queueEmpty() {
117   return ArchiveQueue.empty() && ObjectQueue.empty();
118 }
119 
120 void SymbolTable::reportRemainingUndefines(bool Resolve) {
121   llvm::SmallPtrSet<SymbolBody *, 8> Undefs;
122   for (auto &I : Symtab) {
123     Symbol *Sym = I.second;
124     auto *Undef = dyn_cast<Undefined>(Sym->Body);
125     if (!Undef)
126       continue;
127     StringRef Name = Undef->getName();
128     // A weak alias may have been resolved, so check for that.
129     if (Defined *D = Undef->getWeakAlias()) {
130       if (Resolve)
131         Sym->Body = D;
132       continue;
133     }
134     // If we can resolve a symbol by removing __imp_ prefix, do that.
135     // This odd rule is for compatibility with MSVC linker.
136     if (Name.startswith("__imp_")) {
137       Symbol *Imp = find(Name.substr(strlen("__imp_")));
138       if (Imp && isa<Defined>(Imp->Body)) {
139         if (!Resolve)
140           continue;
141         auto *D = cast<Defined>(Imp->Body);
142         auto *S = new (Alloc) DefinedLocalImport(Name, D);
143         LocalImportChunks.push_back(S->getChunk());
144         Sym->Body = S;
145         continue;
146       }
147     }
148     // Remaining undefined symbols are not fatal if /force is specified.
149     // They are replaced with dummy defined symbols.
150     if (Config->Force && Resolve)
151       Sym->Body = new (Alloc) DefinedAbsolute(Name, 0);
152     Undefs.insert(Sym->Body);
153   }
154   if (Undefs.empty())
155     return;
156   for (Undefined *U : Config->GCRoot)
157     if (Undefs.count(U->repl()))
158       llvm::errs() << "<root>: undefined symbol: " << U->getName() << "\n";
159   for (std::unique_ptr<InputFile> &File : Files)
160     if (!isa<ArchiveFile>(File.get()))
161       for (SymbolBody *Sym : File->getSymbols())
162         if (Undefs.count(Sym->repl()))
163           llvm::errs() << File->getShortName() << ": undefined symbol: "
164                        << Sym->getName() << "\n";
165   if (!Config->Force)
166     error("Link failed");
167 }
168 
169 void SymbolTable::addLazy(Lazy *New, std::vector<Symbol *> *Accum) {
170   Symbol *Sym = insert(New);
171   if (Sym->Body == New)
172     return;
173   SymbolBody *Existing = Sym->Body;
174   if (isa<Defined>(Existing))
175     return;
176   if (Lazy *L = dyn_cast<Lazy>(Existing))
177     if (L->getFileIndex() < New->getFileIndex())
178       return;
179   Sym->Body = New;
180   New->setBackref(Sym);
181   if (isa<Undefined>(Existing))
182     Accum->push_back(Sym);
183 }
184 
185 void SymbolTable::addSymbol(SymbolBody *New) {
186   // Find an existing symbol or create and insert a new one.
187   assert(isa<Defined>(New) || isa<Undefined>(New));
188   Symbol *Sym = insert(New);
189   if (Sym->Body == New)
190     return;
191   SymbolBody *Existing = Sym->Body;
192 
193   // If we have an undefined symbol and a lazy symbol,
194   // let the lazy symbol to read a member file.
195   if (auto *L = dyn_cast<Lazy>(Existing)) {
196     // Undefined symbols with weak aliases need not to be resolved,
197     // since they would be replaced with weak aliases if they remain
198     // undefined.
199     if (auto *U = dyn_cast<Undefined>(New)) {
200       if (!U->WeakAlias) {
201         addMemberFile(L);
202         return;
203       }
204     }
205     Sym->Body = New;
206     return;
207   }
208 
209   // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
210   // equivalent (conflicting), or more preferable, respectively.
211   int Comp = Existing->compare(New);
212   if (Comp == 0)
213     error(Twine("duplicate symbol: ") + Existing->getDebugName() + " and " +
214           New->getDebugName());
215   if (Comp < 0)
216     Sym->Body = New;
217 }
218 
219 Symbol *SymbolTable::insert(SymbolBody *New) {
220   Symbol *&Sym = Symtab[New->getName()];
221   if (Sym) {
222     New->setBackref(Sym);
223     return Sym;
224   }
225   Sym = new (Alloc) Symbol(New);
226   New->setBackref(Sym);
227   return Sym;
228 }
229 
230 // Reads an archive member file pointed by a given symbol.
231 void SymbolTable::addMemberFile(Lazy *Body) {
232   std::unique_ptr<InputFile> File = Body->getMember();
233 
234   // getMember returns an empty buffer if the member was already
235   // read from the library.
236   if (!File)
237     return;
238   if (Config->Verbose)
239     llvm::outs() << "Loaded " << File->getShortName() << " for "
240                  << Body->getName() << "\n";
241   addFile(std::move(File));
242 }
243 
244 std::vector<Chunk *> SymbolTable::getChunks() {
245   std::vector<Chunk *> Res;
246   for (ObjectFile *File : ObjectFiles) {
247     std::vector<Chunk *> &V = File->getChunks();
248     Res.insert(Res.end(), V.begin(), V.end());
249   }
250   return Res;
251 }
252 
253 Symbol *SymbolTable::find(StringRef Name) {
254   auto It = Symtab.find(Name);
255   if (It == Symtab.end())
256     return nullptr;
257   return It->second;
258 }
259 
260 Symbol *SymbolTable::findUnderscore(StringRef Name) {
261   if (Config->Machine == I386)
262     return find(("_" + Name).str());
263   return find(Name);
264 }
265 
266 StringRef SymbolTable::findByPrefix(StringRef Prefix) {
267   for (auto Pair : Symtab) {
268     StringRef Name = Pair.first;
269     if (Name.startswith(Prefix))
270       return Name;
271   }
272   return "";
273 }
274 
275 StringRef SymbolTable::findMangle(StringRef Name) {
276   if (Symbol *Sym = find(Name))
277     if (!isa<Undefined>(Sym->Body))
278       return Name;
279   if (Config->Machine != I386)
280     return findByPrefix(("?" + Name + "@@Y").str());
281   if (!Name.startswith("_"))
282     return "";
283   // Search for x86 C function.
284   StringRef S = findByPrefix((Name + "@").str());
285   if (!S.empty())
286     return S;
287   // Search for x86 C++ non-member function.
288   return findByPrefix(("?" + Name.substr(1) + "@@Y").str());
289 }
290 
291 void SymbolTable::mangleMaybe(Undefined *U) {
292   if (U->WeakAlias)
293     return;
294   if (!isa<Undefined>(U->repl()))
295     return;
296   StringRef Alias = findMangle(U->getName());
297   if (!Alias.empty())
298     U->WeakAlias = addUndefined(Alias);
299 }
300 
301 Undefined *SymbolTable::addUndefined(StringRef Name) {
302   auto *New = new (Alloc) Undefined(Name);
303   addSymbol(New);
304   if (auto *U = dyn_cast<Undefined>(New->repl()))
305     return U;
306   return New;
307 }
308 
309 DefinedRelative *SymbolTable::addRelative(StringRef Name, uint64_t VA) {
310   auto *New = new (Alloc) DefinedRelative(Name, VA);
311   addSymbol(New);
312   return New;
313 }
314 
315 DefinedAbsolute *SymbolTable::addAbsolute(StringRef Name, uint64_t VA) {
316   auto *New = new (Alloc) DefinedAbsolute(Name, VA);
317   addSymbol(New);
318   return New;
319 }
320 
321 void SymbolTable::printMap(llvm::raw_ostream &OS) {
322   for (ObjectFile *File : ObjectFiles) {
323     OS << File->getShortName() << ":\n";
324     for (SymbolBody *Body : File->getSymbols())
325       if (auto *R = dyn_cast<DefinedRegular>(Body))
326         if (R->getChunk()->isLive())
327           OS << Twine::utohexstr(Config->ImageBase + R->getRVA())
328              << " " << R->getName() << "\n";
329   }
330 }
331 
332 void SymbolTable::addCombinedLTOObject(ObjectFile *Obj) {
333   for (SymbolBody *Body : Obj->getSymbols()) {
334     if (!Body->isExternal())
335       continue;
336     // We should not see any new undefined symbols at this point, but we'll
337     // diagnose them later in reportRemainingUndefines().
338     StringRef Name = Body->getName();
339     Symbol *Sym = insert(Body);
340 
341     if (isa<DefinedBitcode>(Sym->Body)) {
342       Sym->Body = Body;
343       continue;
344     }
345     if (auto *L = dyn_cast<Lazy>(Sym->Body)) {
346       // We may see new references to runtime library symbols such as __chkstk
347       // here. These symbols must be wholly defined in non-bitcode files.
348       addMemberFile(L);
349       continue;
350     }
351     SymbolBody *Existing = Sym->Body;
352     int Comp = Existing->compare(Body);
353     if (Comp == 0)
354       error(Twine("LTO: unexpected duplicate symbol: ") + Name);
355     if (Comp < 0)
356       Sym->Body = Body;
357   }
358 }
359 
360 void SymbolTable::addCombinedLTOObjects() {
361   if (BitcodeFiles.empty())
362     return;
363 
364   // Diagnose any undefined symbols early, but do not resolve weak externals,
365   // as resolution breaks the invariant that each Symbol points to a unique
366   // SymbolBody, which we rely on to replace DefinedBitcode symbols correctly.
367   reportRemainingUndefines(/*Resolve=*/false);
368 
369   // Create an object file and add it to the symbol table by replacing any
370   // DefinedBitcode symbols with the definitions in the object file.
371   LTOCodeGenerator CG;
372   CG.setOptLevel(Config->LTOOptLevel);
373   std::vector<ObjectFile *> Objs = createLTOObjects(&CG);
374 
375   for (ObjectFile *Obj : Objs)
376     addCombinedLTOObject(Obj);
377 
378   size_t NumBitcodeFiles = BitcodeFiles.size();
379   run();
380   if (BitcodeFiles.size() != NumBitcodeFiles)
381     error("LTO: late loaded symbol created new bitcode reference");
382 }
383 
384 // Combine and compile bitcode files and then return the result
385 // as a vector of regular COFF object files.
386 std::vector<ObjectFile *> SymbolTable::createLTOObjects(LTOCodeGenerator *CG) {
387   // All symbols referenced by non-bitcode objects must be preserved.
388   for (ObjectFile *File : ObjectFiles)
389     for (SymbolBody *Body : File->getSymbols())
390       if (auto *S = dyn_cast<DefinedBitcode>(Body->repl()))
391         CG->addMustPreserveSymbol(S->getName());
392 
393   // Likewise for bitcode symbols which we initially resolved to non-bitcode.
394   for (BitcodeFile *File : BitcodeFiles)
395     for (SymbolBody *Body : File->getSymbols())
396       if (isa<DefinedBitcode>(Body) && !isa<DefinedBitcode>(Body->repl()))
397         CG->addMustPreserveSymbol(Body->getName());
398 
399   // Likewise for other symbols that must be preserved.
400   for (Undefined *U : Config->GCRoot) {
401     if (auto *S = dyn_cast<DefinedBitcode>(U->repl()))
402       CG->addMustPreserveSymbol(S->getName());
403     else if (auto *S = dyn_cast_or_null<DefinedBitcode>(U->getWeakAlias()))
404       CG->addMustPreserveSymbol(S->getName());
405   }
406 
407   CG->setModule(BitcodeFiles[0]->takeModule());
408   for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I)
409     CG->addModule(BitcodeFiles[I]->getModule());
410 
411   bool DisableVerify = true;
412 #ifdef NDEBUG
413   DisableVerify = false;
414 #endif
415   std::string ErrMsg;
416   if (!CG->optimize(DisableVerify, false, false, false, ErrMsg))
417     error(ErrMsg);
418 
419   Objs.resize(Config->LTOJobs);
420   // Use std::list to avoid invalidation of pointers in OSPtrs.
421   std::list<raw_svector_ostream> OSs;
422   std::vector<raw_pwrite_stream *> OSPtrs;
423   for (SmallVector<char, 0> &Obj : Objs) {
424     OSs.emplace_back(Obj);
425     OSPtrs.push_back(&OSs.back());
426   }
427 
428   if (!CG->compileOptimized(OSPtrs, ErrMsg))
429     error(ErrMsg);
430 
431   std::vector<ObjectFile *> ObjFiles;
432   for (SmallVector<char, 0> &Obj : Objs) {
433     auto *ObjFile = new ObjectFile(
434         MemoryBufferRef(StringRef(Obj.data(), Obj.size()), "<LTO object>"));
435     Files.emplace_back(ObjFile);
436     ObjectFiles.push_back(ObjFile);
437     ObjFile->parse();
438     ObjFiles.push_back(ObjFile);
439   }
440 
441   return ObjFiles;
442 }
443 
444 } // namespace coff
445 } // namespace lld
446