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