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