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