Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 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" |
Rui Ueyama | 8fd9fb9 | 2015-06-01 02:58:15 +0000 | [diff] [blame] | 12 | #include "Error.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 13 | #include "SymbolTable.h" |
Chandler Carruth | be6e80b | 2015-06-29 18:50:11 +0000 | [diff] [blame] | 14 | #include "Symbols.h" |
Rui Ueyama | f4d05d7 | 2015-09-19 01:48:26 +0000 | [diff] [blame] | 15 | #include "lld/Core/Parallel.h" |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 16 | #include "llvm/LTO/LTOCodeGenerator.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
Rui Ueyama | f5313b3 | 2015-06-28 22:16:41 +0000 | [diff] [blame] | 19 | #include <utility> |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 20 | |
Rui Ueyama | d68ff34 | 2015-05-31 03:57:30 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 23 | namespace lld { |
| 24 | namespace coff { |
| 25 | |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 26 | void SymbolTable::addFile(std::unique_ptr<InputFile> FileP) { |
Rui Ueyama | 98a98cf | 2015-09-21 19:12:36 +0000 | [diff] [blame] | 27 | #if LLVM_ENABLE_THREADS |
| 28 | std::launch Policy = std::launch::async; |
| 29 | #else |
| 30 | std::launch Policy = std::launch::deferred; |
| 31 | #endif |
| 32 | |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 33 | InputFile *File = FileP.get(); |
| 34 | Files.push_back(std::move(FileP)); |
| 35 | if (auto *F = dyn_cast<ArchiveFile>(File)) { |
Rui Ueyama | 997b357 | 2015-09-20 03:11:16 +0000 | [diff] [blame] | 36 | ArchiveQueue.push_back( |
Rui Ueyama | 98a98cf | 2015-09-21 19:12:36 +0000 | [diff] [blame] | 37 | std::async(Policy, [=]() { F->parse(); return F; })); |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 38 | return; |
| 39 | } |
Rui Ueyama | 997b357 | 2015-09-20 03:11:16 +0000 | [diff] [blame] | 40 | ObjectQueue.push_back( |
Rui Ueyama | 98a98cf | 2015-09-21 19:12:36 +0000 | [diff] [blame] | 41 | std::async(Policy, [=]() { File->parse(); return File; })); |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 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 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 51 | void SymbolTable::step() { |
Rui Ueyama | 85225b0 | 2015-07-02 03:15:15 +0000 | [diff] [blame] | 52 | if (queueEmpty()) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 53 | return; |
| 54 | readObjects(); |
| 55 | readArchives(); |
Rui Ueyama | 85225b0 | 2015-07-02 03:15:15 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 58 | void SymbolTable::run() { |
Rui Ueyama | 85225b0 | 2015-07-02 03:15:15 +0000 | [diff] [blame] | 59 | while (!queueEmpty()) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 60 | step(); |
Peter Collingbourne | ace2f09 | 2015-06-06 02:00:45 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 63 | void SymbolTable::readArchives() { |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 64 | if (ArchiveQueue.empty()) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 65 | return; |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 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; |
Rui Ueyama | 997b357 | 2015-09-20 03:11:16 +0000 | [diff] [blame] | 70 | for (std::future<ArchiveFile *> &Future : ArchiveQueue) { |
| 71 | ArchiveFile *File = Future.get(); |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 72 | if (Config->Verbose) |
| 73 | llvm::outs() << "Reading " << File->getShortName() << "\n"; |
Rafael Espindola | fb497d7 | 2015-09-02 16:07:11 +0000 | [diff] [blame] | 74 | for (Lazy &Sym : File->getLazySymbols()) |
| 75 | addLazy(&Sym, &LazySyms); |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 76 | } |
| 77 | ArchiveQueue.clear(); |
| 78 | |
| 79 | // Add archive member files to ObjectQueue that should resolve |
| 80 | // existing undefined symbols. |
| 81 | for (Symbol *Sym : LazySyms) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 82 | addMemberFile(cast<Lazy>(Sym->Body)); |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 85 | void SymbolTable::readObjects() { |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 86 | if (ObjectQueue.empty()) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 87 | return; |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 88 | |
| 89 | // Add defined and undefined symbols to the symbol table. |
| 90 | std::vector<StringRef> Directives; |
Rui Ueyama | 997b357 | 2015-09-20 03:11:16 +0000 | [diff] [blame] | 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); |
Rui Ueyama | a3d463d | 2015-07-04 01:39:11 +0000 | [diff] [blame] | 103 | if (Config->Verbose) |
Rui Ueyama | 997b357 | 2015-09-20 03:11:16 +0000 | [diff] [blame] | 104 | llvm::outs() << "Directives: " << File->getShortName() |
| 105 | << ": " << S << "\n"; |
Rui Ueyama | a3d463d | 2015-07-04 01:39:11 +0000 | [diff] [blame] | 106 | } |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 107 | } |
| 108 | ObjectQueue.clear(); |
| 109 | |
| 110 | // Parse directive sections. This may add files to |
| 111 | // ArchiveQueue and ObjectQueue. |
| 112 | for (StringRef S : Directives) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 113 | Driver->parseDirectives(S); |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Rui Ueyama | 3d4c69c | 2015-07-02 02:38:59 +0000 | [diff] [blame] | 116 | bool SymbolTable::queueEmpty() { |
| 117 | return ArchiveQueue.empty() && ObjectQueue.empty(); |
| 118 | } |
| 119 | |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 120 | void SymbolTable::reportRemainingUndefines(bool Resolve) { |
Peter Collingbourne | f5339ec | 2015-07-07 18:38:39 +0000 | [diff] [blame] | 121 | llvm::SmallPtrSet<SymbolBody *, 8> Undefs; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 122 | for (auto &I : Symtab) { |
| 123 | Symbol *Sym = I.second; |
Rui Ueyama | 183f53f | 2015-07-06 17:45:22 +0000 | [diff] [blame] | 124 | auto *Undef = dyn_cast<Undefined>(Sym->Body); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 125 | if (!Undef) |
| 126 | continue; |
Rui Ueyama | d766653 | 2015-06-25 02:21:44 +0000 | [diff] [blame] | 127 | StringRef Name = Undef->getName(); |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 128 | // A weak alias may have been resolved, so check for that. |
| 129 | if (Defined *D = Undef->getWeakAlias()) { |
| 130 | if (Resolve) |
Rui Ueyama | 6bf638e | 2015-07-02 00:04:14 +0000 | [diff] [blame] | 131 | Sym->Body = D; |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 132 | continue; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 133 | } |
Rui Ueyama | d766653 | 2015-06-25 02:21:44 +0000 | [diff] [blame] | 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_")) { |
Rui Ueyama | 458d744 | 2015-07-02 03:59:04 +0000 | [diff] [blame] | 137 | Symbol *Imp = find(Name.substr(strlen("__imp_"))); |
Rui Ueyama | 183f53f | 2015-07-06 17:45:22 +0000 | [diff] [blame] | 138 | if (Imp && isa<Defined>(Imp->Body)) { |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 139 | if (!Resolve) |
| 140 | continue; |
Rui Ueyama | 183f53f | 2015-07-06 17:45:22 +0000 | [diff] [blame] | 141 | auto *D = cast<Defined>(Imp->Body); |
Rui Ueyama | 458d744 | 2015-07-02 03:59:04 +0000 | [diff] [blame] | 142 | auto *S = new (Alloc) DefinedLocalImport(Name, D); |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 +0000 | [diff] [blame] | 143 | LocalImportChunks.push_back(S->getChunk()); |
| 144 | Sym->Body = S; |
Rui Ueyama | d766653 | 2015-06-25 02:21:44 +0000 | [diff] [blame] | 145 | continue; |
| 146 | } |
| 147 | } |
Rui Ueyama | 95925fd | 2015-06-28 19:35:15 +0000 | [diff] [blame] | 148 | // Remaining undefined symbols are not fatal if /force is specified. |
| 149 | // They are replaced with dummy defined symbols. |
Peter Collingbourne | f5339ec | 2015-07-07 18:38:39 +0000 | [diff] [blame] | 150 | if (Config->Force && Resolve) |
| 151 | Sym->Body = new (Alloc) DefinedAbsolute(Name, 0); |
| 152 | Undefs.insert(Sym->Body); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 153 | } |
Peter Collingbourne | f5339ec | 2015-07-07 18:38:39 +0000 | [diff] [blame] | 154 | if (Undefs.empty()) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 155 | return; |
Peter Collingbourne | f5339ec | 2015-07-07 18:38:39 +0000 | [diff] [blame] | 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"; |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 165 | if (!Config->Force) |
| 166 | error("Link failed"); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 169 | void SymbolTable::addLazy(Lazy *New, std::vector<Symbol *> *Accum) { |
Rui Ueyama | 6be9099 | 2015-07-02 22:52:33 +0000 | [diff] [blame] | 170 | Symbol *Sym = insert(New); |
| 171 | if (Sym->Body == New) |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 172 | return; |
Rui Ueyama | 1cce300 | 2015-09-20 00:00:05 +0000 | [diff] [blame] | 173 | SymbolBody *Existing = Sym->Body; |
| 174 | if (isa<Defined>(Existing)) |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 175 | return; |
Rui Ueyama | 1cce300 | 2015-09-20 00:00:05 +0000 | [diff] [blame] | 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); |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 185 | void SymbolTable::addSymbol(SymbolBody *New) { |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 186 | // Find an existing symbol or create and insert a new one. |
| 187 | assert(isa<Defined>(New) || isa<Undefined>(New)); |
Rui Ueyama | 6be9099 | 2015-07-02 22:52:33 +0000 | [diff] [blame] | 188 | Symbol *Sym = insert(New); |
| 189 | if (Sym->Body == New) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 190 | return; |
Rui Ueyama | 1cce300 | 2015-09-20 00:00:05 +0000 | [diff] [blame] | 191 | SymbolBody *Existing = Sym->Body; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 192 | |
Rui Ueyama | 1cce300 | 2015-09-20 00:00:05 +0000 | [diff] [blame] | 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 | } |
Rui Ueyama | e2eb155 | 2015-07-05 22:05:08 +0000 | [diff] [blame] | 204 | } |
Rui Ueyama | 1cce300 | 2015-09-20 00:00:05 +0000 | [diff] [blame] | 205 | Sym->Body = New; |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 206 | return; |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 207 | } |
Rui Ueyama | 1cce300 | 2015-09-20 00:00:05 +0000 | [diff] [blame] | 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; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Rui Ueyama | 6be9099 | 2015-07-02 22:52:33 +0000 | [diff] [blame] | 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 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 230 | // Reads an archive member file pointed by a given symbol. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 231 | void SymbolTable::addMemberFile(Lazy *Body) { |
| 232 | std::unique_ptr<InputFile> File = Body->getMember(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 233 | |
| 234 | // getMember returns an empty buffer if the member was already |
| 235 | // read from the library. |
| 236 | if (!File) |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 237 | return; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 238 | if (Config->Verbose) |
Rui Ueyama | 5b2588a | 2015-06-08 05:43:50 +0000 | [diff] [blame] | 239 | llvm::outs() << "Loaded " << File->getShortName() << " for " |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 240 | << Body->getName() << "\n"; |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 241 | addFile(std::move(File)); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | std::vector<Chunk *> SymbolTable::getChunks() { |
| 245 | std::vector<Chunk *> Res; |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 246 | for (ObjectFile *File : ObjectFiles) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 247 | std::vector<Chunk *> &V = File->getChunks(); |
| 248 | Res.insert(Res.end(), V.begin(), V.end()); |
| 249 | } |
| 250 | return Res; |
| 251 | } |
| 252 | |
Rui Ueyama | 458d744 | 2015-07-02 03:59:04 +0000 | [diff] [blame] | 253 | Symbol *SymbolTable::find(StringRef Name) { |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 254 | auto It = Symtab.find(Name); |
| 255 | if (It == Symtab.end()) |
| 256 | return nullptr; |
Rui Ueyama | 4b66989 | 2015-06-30 23:46:52 +0000 | [diff] [blame] | 257 | return It->second; |
Rui Ueyama | 45044f4 | 2015-06-29 01:03:53 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Rui Ueyama | 506f6d1 | 2015-07-28 22:56:02 +0000 | [diff] [blame] | 260 | Symbol *SymbolTable::findUnderscore(StringRef Name) { |
| 261 | if (Config->Machine == I386) |
| 262 | return find(("_" + Name).str()); |
| 263 | return find(Name); |
| 264 | } |
| 265 | |
Rui Ueyama | a50387f | 2015-07-14 02:58:13 +0000 | [diff] [blame] | 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; |
Rui Ueyama | 5e706b3 | 2015-07-25 21:54:50 +0000 | [diff] [blame] | 279 | if (Config->Machine != I386) |
Rui Ueyama | a50387f | 2015-07-14 02:58:13 +0000 | [diff] [blame] | 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 | |
Rui Ueyama | 6bf638e | 2015-07-02 00:04:14 +0000 | [diff] [blame] | 291 | void SymbolTable::mangleMaybe(Undefined *U) { |
| 292 | if (U->WeakAlias) |
| 293 | return; |
Rui Ueyama | 0744e87 | 2015-07-02 00:21:11 +0000 | [diff] [blame] | 294 | if (!isa<Undefined>(U->repl())) |
Rui Ueyama | 6bf638e | 2015-07-02 00:04:14 +0000 | [diff] [blame] | 295 | return; |
Rui Ueyama | a50387f | 2015-07-14 02:58:13 +0000 | [diff] [blame] | 296 | StringRef Alias = findMangle(U->getName()); |
| 297 | if (!Alias.empty()) |
| 298 | U->WeakAlias = addUndefined(Alias); |
Rui Ueyama | f5313b3 | 2015-06-28 22:16:41 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Rui Ueyama | 6bf638e | 2015-07-02 00:04:14 +0000 | [diff] [blame] | 301 | Undefined *SymbolTable::addUndefined(StringRef Name) { |
Rui Ueyama | 3d4c69c | 2015-07-02 02:38:59 +0000 | [diff] [blame] | 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; |
Rui Ueyama | 360bace | 2015-05-31 22:31:31 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 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; |
Rui Ueyama | 49d6cd3 | 2015-07-03 00:02:19 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Peter Collingbourne | be54955 | 2015-06-26 18:58:24 +0000 | [diff] [blame] | 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)) |
Rui Ueyama | 63dd876 | 2015-09-16 23:55:52 +0000 | [diff] [blame] | 326 | if (R->getChunk()->isLive()) |
Peter Collingbourne | be54955 | 2015-06-26 18:58:24 +0000 | [diff] [blame] | 327 | OS << Twine::utohexstr(Config->ImageBase + R->getRVA()) |
| 328 | << " " << R->getName() << "\n"; |
| 329 | } |
| 330 | } |
| 331 | |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 332 | void SymbolTable::addCombinedLTOObject(ObjectFile *Obj) { |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 333 | for (SymbolBody *Body : Obj->getSymbols()) { |
| 334 | if (!Body->isExternal()) |
| 335 | continue; |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 336 | // We should not see any new undefined symbols at this point, but we'll |
| 337 | // diagnose them later in reportRemainingUndefines(). |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 338 | StringRef Name = Body->getName(); |
Rui Ueyama | 6be9099 | 2015-07-02 22:52:33 +0000 | [diff] [blame] | 339 | Symbol *Sym = insert(Body); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 340 | |
Rui Ueyama | 183f53f | 2015-07-06 17:45:22 +0000 | [diff] [blame] | 341 | if (isa<DefinedBitcode>(Sym->Body)) { |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 342 | Sym->Body = Body; |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 343 | continue; |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 344 | } |
Rui Ueyama | 183f53f | 2015-07-06 17:45:22 +0000 | [diff] [blame] | 345 | if (auto *L = dyn_cast<Lazy>(Sym->Body)) { |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 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. |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 348 | addMemberFile(L); |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 349 | continue; |
| 350 | } |
| 351 | SymbolBody *Existing = Sym->Body; |
| 352 | int Comp = Existing->compare(Body); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 353 | if (Comp == 0) |
| 354 | error(Twine("LTO: unexpected duplicate symbol: ") + Name); |
Rui Ueyama | 8d3010a | 2015-06-30 19:35:21 +0000 | [diff] [blame] | 355 | if (Comp < 0) |
| 356 | Sym->Body = Body; |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 357 | } |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 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); |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 377 | |
| 378 | size_t NumBitcodeFiles = BitcodeFiles.size(); |
Rafael Espindola | b835ae8 | 2015-08-06 14:58:50 +0000 | [diff] [blame] | 379 | run(); |
| 380 | if (BitcodeFiles.size() != NumBitcodeFiles) |
| 381 | error("LTO: late loaded symbol created new bitcode reference"); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 384 | // Combine and compile bitcode files and then return the result |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 385 | // as a vector of regular COFF object files. |
| 386 | std::vector<ObjectFile *> SymbolTable::createLTOObjects(LTOCodeGenerator *CG) { |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 387 | // All symbols referenced by non-bitcode objects must be preserved. |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 388 | for (ObjectFile *File : ObjectFiles) |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 389 | for (SymbolBody *Body : File->getSymbols()) |
Rui Ueyama | 0744e87 | 2015-07-02 00:21:11 +0000 | [diff] [blame] | 390 | if (auto *S = dyn_cast<DefinedBitcode>(Body->repl())) |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 391 | CG->addMustPreserveSymbol(S->getName()); |
| 392 | |
Peter Collingbourne | 1b6fd1f | 2015-06-11 21:49:54 +0000 | [diff] [blame] | 393 | // Likewise for bitcode symbols which we initially resolved to non-bitcode. |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 394 | for (BitcodeFile *File : BitcodeFiles) |
Peter Collingbourne | 1b6fd1f | 2015-06-11 21:49:54 +0000 | [diff] [blame] | 395 | for (SymbolBody *Body : File->getSymbols()) |
Rui Ueyama | 0744e87 | 2015-07-02 00:21:11 +0000 | [diff] [blame] | 396 | if (isa<DefinedBitcode>(Body) && !isa<DefinedBitcode>(Body->repl())) |
Peter Collingbourne | 1b6fd1f | 2015-06-11 21:49:54 +0000 | [diff] [blame] | 397 | CG->addMustPreserveSymbol(Body->getName()); |
| 398 | |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 399 | // Likewise for other symbols that must be preserved. |
Peter Collingbourne | 2612a32 | 2015-07-04 05:28:41 +0000 | [diff] [blame] | 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 | } |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 406 | |
Peter Collingbourne | 0bb50d9 | 2015-08-24 22:22:58 +0000 | [diff] [blame] | 407 | CG->setModule(BitcodeFiles[0]->takeModule()); |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 408 | for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I) |
| 409 | CG->addModule(BitcodeFiles[I]->getModule()); |
| 410 | |
Rui Ueyama | 4151972 | 2015-09-17 22:54:08 +0000 | [diff] [blame] | 411 | bool DisableVerify = true; |
| 412 | #ifdef NDEBUG |
| 413 | DisableVerify = false; |
| 414 | #endif |
Yunzhong Gao | fbee892 | 2015-11-17 20:48:12 +0000 | [diff] [blame] | 415 | if (!CG->optimize(DisableVerify, false, false, false)) |
| 416 | error(""); // optimize() should have emitted any error message. |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 417 | |
| 418 | Objs.resize(Config->LTOJobs); |
| 419 | // Use std::list to avoid invalidation of pointers in OSPtrs. |
| 420 | std::list<raw_svector_ostream> OSs; |
| 421 | std::vector<raw_pwrite_stream *> OSPtrs; |
| 422 | for (SmallVector<char, 0> &Obj : Objs) { |
| 423 | OSs.emplace_back(Obj); |
| 424 | OSPtrs.push_back(&OSs.back()); |
| 425 | } |
| 426 | |
Yunzhong Gao | fbee892 | 2015-11-17 20:48:12 +0000 | [diff] [blame] | 427 | if (!CG->compileOptimized(OSPtrs)) |
| 428 | error(""); // compileOptimized() should have emitted any error message. |
Peter Collingbourne | df5783b | 2015-08-28 22:16:09 +0000 | [diff] [blame] | 429 | |
| 430 | std::vector<ObjectFile *> ObjFiles; |
| 431 | for (SmallVector<char, 0> &Obj : Objs) { |
| 432 | auto *ObjFile = new ObjectFile( |
| 433 | MemoryBufferRef(StringRef(Obj.data(), Obj.size()), "<LTO object>")); |
| 434 | Files.emplace_back(ObjFile); |
| 435 | ObjectFiles.push_back(ObjFile); |
| 436 | ObjFile->parse(); |
| 437 | ObjFiles.push_back(ObjFile); |
| 438 | } |
| 439 | |
| 440 | return ObjFiles; |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 443 | } // namespace coff |
| 444 | } // namespace lld |