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" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 15 | #include "llvm/LTO/LTOCodeGenerator.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | |
Rui Ueyama | d68ff34 | 2015-05-31 03:57:30 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 21 | namespace lld { |
| 22 | namespace coff { |
| 23 | |
| 24 | SymbolTable::SymbolTable() { |
Rui Ueyama | 07e661f | 2015-06-03 05:39:12 +0000 | [diff] [blame] | 25 | resolve(new (Alloc) DefinedAbsolute("__ImageBase", Config->ImageBase)); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 26 | if (!Config->EntryName.empty()) |
Rui Ueyama | 07e661f | 2015-06-03 05:39:12 +0000 | [diff] [blame] | 27 | resolve(new (Alloc) Undefined(Config->EntryName)); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 30 | void SymbolTable::addFile(std::unique_ptr<InputFile> File) { |
| 31 | Files.push_back(std::move(File)); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 34 | std::error_code SymbolTable::run() { |
| 35 | while (FileIdx < Files.size()) { |
| 36 | InputFile *F = Files[FileIdx++].get(); |
| 37 | if (Config->Verbose) |
| 38 | llvm::outs() << "Reading " << F->getShortName() << "\n"; |
| 39 | if (auto EC = F->parse()) |
| 40 | return EC; |
| 41 | if (auto *P = dyn_cast<ObjectFile>(F)) { |
| 42 | ObjectFiles.push_back(P); |
| 43 | } else if (auto *P = dyn_cast<ArchiveFile>(F)) { |
| 44 | ArchiveFiles.push_back(P); |
| 45 | } else if (auto *P = dyn_cast<BitcodeFile>(F)) { |
| 46 | BitcodeFiles.push_back(P); |
| 47 | } else { |
| 48 | ImportFiles.push_back(cast<ImportFile>(F)); |
Rui Ueyama | eeae5dd | 2015-06-08 06:00:10 +0000 | [diff] [blame] | 49 | } |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 50 | |
| 51 | for (SymbolBody *B : F->getSymbols()) |
| 52 | if (B->isExternal()) |
| 53 | if (auto EC = resolve(B)) |
| 54 | return EC; |
| 55 | |
| 56 | // If a object file contains .drectve section, |
| 57 | // read that and add files listed there. |
| 58 | StringRef S = F->getDirectives(); |
| 59 | if (!S.empty()) |
| 60 | if (auto EC = Driver->parseDirectives(S)) |
| 61 | return EC; |
Rui Ueyama | eeae5dd | 2015-06-08 06:00:10 +0000 | [diff] [blame] | 62 | } |
Peter Collingbourne | ace2f09 | 2015-06-06 02:00:45 +0000 | [diff] [blame] | 63 | return std::error_code(); |
| 64 | } |
| 65 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 66 | bool SymbolTable::reportRemainingUndefines() { |
| 67 | bool Ret = false; |
| 68 | for (auto &I : Symtab) { |
| 69 | Symbol *Sym = I.second; |
| 70 | auto *Undef = dyn_cast<Undefined>(Sym->Body); |
| 71 | if (!Undef) |
| 72 | continue; |
Rui Ueyama | d766653 | 2015-06-25 02:21:44 +0000 | [diff] [blame] | 73 | StringRef Name = Undef->getName(); |
Rui Ueyama | f24e6f8 | 2015-06-28 20:34:09 +0000 | [diff] [blame^] | 74 | // The weak alias may have been resovled, so check for that. |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 75 | if (SymbolBody *Alias = Undef->getWeakAlias()) { |
Rui Ueyama | f24e6f8 | 2015-06-28 20:34:09 +0000 | [diff] [blame^] | 76 | if (auto *D = dyn_cast<Defined>(Alias->getReplacement())) { |
| 77 | Sym->Body = D; |
| 78 | continue; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 79 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 80 | } |
Rui Ueyama | d766653 | 2015-06-25 02:21:44 +0000 | [diff] [blame] | 81 | // If we can resolve a symbol by removing __imp_ prefix, do that. |
| 82 | // This odd rule is for compatibility with MSVC linker. |
| 83 | if (Name.startswith("__imp_")) { |
| 84 | if (Defined *Imp = find(Name.substr(strlen("__imp_")))) { |
Rui Ueyama | 88e0f92 | 2015-06-25 03:31:47 +0000 | [diff] [blame] | 85 | auto *S = new (Alloc) DefinedLocalImport(Name, Imp); |
| 86 | LocalImportChunks.push_back(S->getChunk()); |
| 87 | Sym->Body = S; |
Rui Ueyama | d766653 | 2015-06-25 02:21:44 +0000 | [diff] [blame] | 88 | continue; |
| 89 | } |
| 90 | } |
| 91 | llvm::errs() << "undefined symbol: " << Name << "\n"; |
Rui Ueyama | 95925fd | 2015-06-28 19:35:15 +0000 | [diff] [blame] | 92 | // Remaining undefined symbols are not fatal if /force is specified. |
| 93 | // They are replaced with dummy defined symbols. |
| 94 | if (Config->Force) { |
| 95 | Sym->Body = new (Alloc) DefinedAbsolute(Name, 0); |
| 96 | continue; |
| 97 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 98 | Ret = true; |
| 99 | } |
| 100 | return Ret; |
| 101 | } |
| 102 | |
| 103 | // This function resolves conflicts if there's an existing symbol with |
| 104 | // the same name. Decisions are made based on symbol type. |
| 105 | std::error_code SymbolTable::resolve(SymbolBody *New) { |
| 106 | // Find an existing Symbol or create and insert a new one. |
| 107 | StringRef Name = New->getName(); |
| 108 | Symbol *&Sym = Symtab[Name]; |
| 109 | if (!Sym) { |
| 110 | Sym = new (Alloc) Symbol(New); |
| 111 | New->setBackref(Sym); |
Rui Ueyama | 573bf7d | 2015-06-19 21:12:48 +0000 | [diff] [blame] | 112 | ++Version; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 113 | return std::error_code(); |
| 114 | } |
| 115 | New->setBackref(Sym); |
| 116 | |
| 117 | // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, |
| 118 | // equivalent (conflicting), or more preferable, respectively. |
| 119 | SymbolBody *Existing = Sym->Body; |
| 120 | int comp = Existing->compare(New); |
Rui Ueyama | 573bf7d | 2015-06-19 21:12:48 +0000 | [diff] [blame] | 121 | if (comp < 0) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 122 | Sym->Body = New; |
Rui Ueyama | 573bf7d | 2015-06-19 21:12:48 +0000 | [diff] [blame] | 123 | ++Version; |
| 124 | } |
Rui Ueyama | 8fd9fb9 | 2015-06-01 02:58:15 +0000 | [diff] [blame] | 125 | if (comp == 0) { |
Rui Ueyama | 68633f1 | 2015-06-25 23:22:00 +0000 | [diff] [blame] | 126 | llvm::errs() << "duplicate symbol: " << Existing->getDebugName() |
| 127 | << " and " << New->getDebugName() << "\n"; |
Rui Ueyama | 8fd9fb9 | 2015-06-01 02:58:15 +0000 | [diff] [blame] | 128 | return make_error_code(LLDError::DuplicateSymbols); |
| 129 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 130 | |
| 131 | // If we have an Undefined symbol for a Lazy symbol, we need |
| 132 | // to read an archive member to replace the Lazy symbol with |
| 133 | // a Defined symbol. |
| 134 | if (isa<Undefined>(Existing) || isa<Undefined>(New)) |
| 135 | if (auto *B = dyn_cast<Lazy>(Sym->Body)) |
| 136 | return addMemberFile(B); |
| 137 | return std::error_code(); |
| 138 | } |
| 139 | |
| 140 | // Reads an archive member file pointed by a given symbol. |
| 141 | std::error_code SymbolTable::addMemberFile(Lazy *Body) { |
| 142 | auto FileOrErr = Body->getMember(); |
| 143 | if (auto EC = FileOrErr.getError()) |
| 144 | return EC; |
| 145 | std::unique_ptr<InputFile> File = std::move(FileOrErr.get()); |
| 146 | |
| 147 | // getMember returns an empty buffer if the member was already |
| 148 | // read from the library. |
| 149 | if (!File) |
| 150 | return std::error_code(); |
| 151 | if (Config->Verbose) |
Rui Ueyama | 5b2588a | 2015-06-08 05:43:50 +0000 | [diff] [blame] | 152 | llvm::outs() << "Loaded " << File->getShortName() << " for " |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 153 | << Body->getName() << "\n"; |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 154 | addFile(std::move(File)); |
| 155 | return std::error_code(); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | std::vector<Chunk *> SymbolTable::getChunks() { |
| 159 | std::vector<Chunk *> Res; |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 160 | for (ObjectFile *File : ObjectFiles) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 161 | std::vector<Chunk *> &V = File->getChunks(); |
| 162 | Res.insert(Res.end(), V.begin(), V.end()); |
| 163 | } |
| 164 | return Res; |
| 165 | } |
| 166 | |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 167 | Defined *SymbolTable::find(StringRef Name) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 168 | auto It = Symtab.find(Name); |
| 169 | if (It == Symtab.end()) |
| 170 | return nullptr; |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 171 | if (auto *Def = dyn_cast<Defined>(It->second->Body)) |
| 172 | return Def; |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
Rui Ueyama | 23ed96d | 2015-06-18 17:29:50 +0000 | [diff] [blame] | 176 | std::error_code SymbolTable::resolveLazy(StringRef Name) { |
Rui Ueyama | ae36985 | 2015-06-18 00:40:33 +0000 | [diff] [blame] | 177 | auto It = Symtab.find(Name); |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 178 | if (It == Symtab.end()) |
| 179 | return std::error_code(); |
| 180 | if (auto *B = dyn_cast<Lazy>(It->second->Body)) { |
| 181 | if (auto EC = addMemberFile(B)) |
| 182 | return EC; |
| 183 | return run(); |
| 184 | } |
Rui Ueyama | ae36985 | 2015-06-18 00:40:33 +0000 | [diff] [blame] | 185 | return std::error_code(); |
| 186 | } |
| 187 | |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 188 | // Windows specific -- Link default entry point name. |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 189 | ErrorOr<StringRef> SymbolTable::findDefaultEntry() { |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 190 | // User-defined main functions and their corresponding entry points. |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 191 | static const char *Entries[][2] = { |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 192 | {"main", "mainCRTStartup"}, |
| 193 | {"wmain", "wmainCRTStartup"}, |
| 194 | {"WinMain", "WinMainCRTStartup"}, |
| 195 | {"wWinMain", "wWinMainCRTStartup"}, |
| 196 | }; |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 197 | for (auto E : Entries) { |
Rui Ueyama | 23ed96d | 2015-06-18 17:29:50 +0000 | [diff] [blame] | 198 | resolveLazy(E[1]); |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 199 | if (find(E[1])) |
| 200 | return StringRef(E[1]); |
| 201 | if (!find(E[0])) |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 202 | continue; |
Rui Ueyama | 07e661f | 2015-06-03 05:39:12 +0000 | [diff] [blame] | 203 | if (auto EC = resolve(new (Alloc) Undefined(E[1]))) |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 204 | return EC; |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 205 | return StringRef(E[1]); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 206 | } |
Rui Ueyama | 8fd9fb9 | 2015-06-01 02:58:15 +0000 | [diff] [blame] | 207 | llvm::errs() << "entry point must be defined\n"; |
| 208 | return make_error_code(LLDError::InvalidOption); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Rui Ueyama | e042fa9a | 2015-05-31 19:55:40 +0000 | [diff] [blame] | 211 | std::error_code SymbolTable::addUndefined(StringRef Name) { |
Rui Ueyama | 07e661f | 2015-06-03 05:39:12 +0000 | [diff] [blame] | 212 | return resolve(new (Alloc) Undefined(Name)); |
Rui Ueyama | e042fa9a | 2015-05-31 19:55:40 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Rui Ueyama | 360bace | 2015-05-31 22:31:31 +0000 | [diff] [blame] | 215 | // Resolve To, and make From an alias to To. |
| 216 | std::error_code SymbolTable::rename(StringRef From, StringRef To) { |
Rui Ueyama | 4d2834b | 2015-06-19 19:23:43 +0000 | [diff] [blame] | 217 | // If From is not undefined, do nothing. |
| 218 | // Otherwise, rename it to see if To can be resolved instead. |
| 219 | auto It = Symtab.find(From); |
| 220 | if (It == Symtab.end()) |
| 221 | return std::error_code(); |
| 222 | Symbol *Sym = It->second; |
| 223 | if (!isa<Undefined>(Sym->Body)) |
| 224 | return std::error_code(); |
Rui Ueyama | 360bace | 2015-05-31 22:31:31 +0000 | [diff] [blame] | 225 | SymbolBody *Body = new (Alloc) Undefined(To); |
| 226 | if (auto EC = resolve(Body)) |
| 227 | return EC; |
Rui Ueyama | 4d2834b | 2015-06-19 19:23:43 +0000 | [diff] [blame] | 228 | Sym->Body = Body->getReplacement(); |
| 229 | Body->setBackref(Sym); |
Rui Ueyama | 573bf7d | 2015-06-19 21:12:48 +0000 | [diff] [blame] | 230 | ++Version; |
Rui Ueyama | 360bace | 2015-05-31 22:31:31 +0000 | [diff] [blame] | 231 | return std::error_code(); |
| 232 | } |
| 233 | |
Peter Collingbourne | be54955 | 2015-06-26 18:58:24 +0000 | [diff] [blame] | 234 | void SymbolTable::printMap(llvm::raw_ostream &OS) { |
| 235 | for (ObjectFile *File : ObjectFiles) { |
| 236 | OS << File->getShortName() << ":\n"; |
| 237 | for (SymbolBody *Body : File->getSymbols()) |
| 238 | if (auto *R = dyn_cast<DefinedRegular>(Body)) |
| 239 | if (R->isLive()) |
| 240 | OS << Twine::utohexstr(Config->ImageBase + R->getRVA()) |
| 241 | << " " << R->getName() << "\n"; |
| 242 | } |
| 243 | } |
| 244 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 245 | std::error_code SymbolTable::addCombinedLTOObject() { |
| 246 | if (BitcodeFiles.empty()) |
| 247 | return std::error_code(); |
| 248 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 249 | // Create an object file and add it to the symbol table by replacing any |
| 250 | // DefinedBitcode symbols with the definitions in the object file. |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 251 | LTOCodeGenerator CG; |
| 252 | auto FileOrErr = createLTOObject(&CG); |
| 253 | if (auto EC = FileOrErr.getError()) |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 254 | return EC; |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 255 | ObjectFile *Obj = FileOrErr.get(); |
| 256 | |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 257 | // Skip the combined object file as the file is processed below |
| 258 | // rather than by run(). |
| 259 | ++FileIdx; |
| 260 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 261 | for (SymbolBody *Body : Obj->getSymbols()) { |
| 262 | if (!Body->isExternal()) |
| 263 | continue; |
Peter Collingbourne | d9e4e98 | 2015-06-09 02:53:09 +0000 | [diff] [blame] | 264 | // Find an existing Symbol. We should not see any new undefined symbols at |
| 265 | // this point. |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 266 | StringRef Name = Body->getName(); |
Peter Collingbourne | d9e4e98 | 2015-06-09 02:53:09 +0000 | [diff] [blame] | 267 | Symbol *&Sym = Symtab[Name]; |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 268 | if (!Sym) { |
Peter Collingbourne | d9e4e98 | 2015-06-09 02:53:09 +0000 | [diff] [blame] | 269 | if (!isa<Defined>(Body)) { |
| 270 | llvm::errs() << "LTO: undefined symbol: " << Name << '\n'; |
| 271 | return make_error_code(LLDError::BrokenFile); |
| 272 | } |
| 273 | Sym = new (Alloc) Symbol(Body); |
| 274 | Body->setBackref(Sym); |
| 275 | continue; |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 276 | } |
| 277 | Body->setBackref(Sym); |
| 278 | |
| 279 | if (isa<DefinedBitcode>(Sym->Body)) { |
| 280 | // The symbol should now be defined. |
| 281 | if (!isa<Defined>(Body)) { |
| 282 | llvm::errs() << "LTO: undefined symbol: " << Name << '\n'; |
| 283 | return make_error_code(LLDError::BrokenFile); |
| 284 | } |
| 285 | Sym->Body = Body; |
Peter Collingbourne | 1b6fd1f | 2015-06-11 21:49:54 +0000 | [diff] [blame] | 286 | } else { |
| 287 | int comp = Sym->Body->compare(Body); |
| 288 | if (comp < 0) |
| 289 | Sym->Body = Body; |
| 290 | if (comp == 0) { |
| 291 | llvm::errs() << "LTO: unexpected duplicate symbol: " << Name << "\n"; |
| 292 | return make_error_code(LLDError::BrokenFile); |
| 293 | } |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 294 | } |
Peter Collingbourne | 73b75e3 | 2015-06-09 04:29:54 +0000 | [diff] [blame] | 295 | |
| 296 | // We may see new references to runtime library symbols such as __chkstk |
| 297 | // here. These symbols must be wholly defined in non-bitcode files. |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 298 | if (auto *B = dyn_cast<Lazy>(Sym->Body)) |
Peter Collingbourne | 2ed4c8f | 2015-06-24 00:12:34 +0000 | [diff] [blame] | 299 | if (auto EC = addMemberFile(B)) |
| 300 | return EC; |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | size_t NumBitcodeFiles = BitcodeFiles.size(); |
| 304 | if (auto EC = run()) |
| 305 | return EC; |
| 306 | if (BitcodeFiles.size() != NumBitcodeFiles) { |
| 307 | llvm::errs() << "LTO: late loaded symbol created new bitcode reference\n"; |
| 308 | return make_error_code(LLDError::BrokenFile); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Peter Collingbourne | 73b75e3 | 2015-06-09 04:29:54 +0000 | [diff] [blame] | 311 | // New runtime library symbol references may have created undefined references. |
| 312 | if (reportRemainingUndefines()) |
| 313 | return make_error_code(LLDError::BrokenFile); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 314 | return std::error_code(); |
| 315 | } |
| 316 | |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 317 | // Combine and compile bitcode files and then return the result |
| 318 | // as a regular COFF object file. |
| 319 | ErrorOr<ObjectFile *> SymbolTable::createLTOObject(LTOCodeGenerator *CG) { |
| 320 | // All symbols referenced by non-bitcode objects must be preserved. |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 321 | for (ObjectFile *File : ObjectFiles) |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 322 | for (SymbolBody *Body : File->getSymbols()) |
| 323 | if (auto *S = dyn_cast<DefinedBitcode>(Body->getReplacement())) |
| 324 | CG->addMustPreserveSymbol(S->getName()); |
| 325 | |
Peter Collingbourne | 1b6fd1f | 2015-06-11 21:49:54 +0000 | [diff] [blame] | 326 | // Likewise for bitcode symbols which we initially resolved to non-bitcode. |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 327 | for (BitcodeFile *File : BitcodeFiles) |
Peter Collingbourne | 1b6fd1f | 2015-06-11 21:49:54 +0000 | [diff] [blame] | 328 | for (SymbolBody *Body : File->getSymbols()) |
| 329 | if (isa<DefinedBitcode>(Body) && |
| 330 | !isa<DefinedBitcode>(Body->getReplacement())) |
| 331 | CG->addMustPreserveSymbol(Body->getName()); |
| 332 | |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 333 | // Likewise for other symbols that must be preserved. |
| 334 | for (StringRef Name : Config->GCRoots) |
| 335 | if (isa<DefinedBitcode>(Symtab[Name]->Body)) |
| 336 | CG->addMustPreserveSymbol(Name); |
| 337 | |
| 338 | CG->setModule(BitcodeFiles[0]->releaseModule()); |
| 339 | for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I) |
| 340 | CG->addModule(BitcodeFiles[I]->getModule()); |
| 341 | |
| 342 | std::string ErrMsg; |
| 343 | LTOMB = CG->compile(false, false, false, ErrMsg); // take MB ownership |
| 344 | if (!LTOMB) { |
| 345 | llvm::errs() << ErrMsg << '\n'; |
| 346 | return make_error_code(LLDError::BrokenFile); |
| 347 | } |
Rui Ueyama | 0d2e999 | 2015-06-23 23:56:39 +0000 | [diff] [blame] | 348 | auto *Obj = new ObjectFile(LTOMB->getMemBufferRef()); |
| 349 | Files.emplace_back(Obj); |
| 350 | ObjectFiles.push_back(Obj); |
Rui Ueyama | efba781 | 2015-06-09 17:52:17 +0000 | [diff] [blame] | 351 | if (auto EC = Obj->parse()) |
| 352 | return EC; |
| 353 | return Obj; |
| 354 | } |
| 355 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 356 | } // namespace coff |
| 357 | } // namespace lld |