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