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