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 | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 190 | // Windows specific -- Link default entry point name. |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 191 | ErrorOr<StringRef> SymbolTable::findDefaultEntry() { |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 192 | // User-defined main functions and their corresponding entry points. |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 193 | static const char *Entries[][2] = { |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 194 | {"main", "mainCRTStartup"}, |
| 195 | {"wmain", "wmainCRTStartup"}, |
| 196 | {"WinMain", "WinMainCRTStartup"}, |
| 197 | {"wWinMain", "wWinMainCRTStartup"}, |
| 198 | }; |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 199 | for (auto E : Entries) { |
| 200 | if (find(E[1])) |
| 201 | return StringRef(E[1]); |
| 202 | if (!find(E[0])) |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 203 | continue; |
Rui Ueyama | 07e661f | 2015-06-03 05:39:12 +0000 | [diff] [blame] | 204 | if (auto EC = resolve(new (Alloc) Undefined(E[1]))) |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 205 | return EC; |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 206 | return StringRef(E[1]); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 207 | } |
Rui Ueyama | 8fd9fb9 | 2015-06-01 02:58:15 +0000 | [diff] [blame] | 208 | llvm::errs() << "entry point must be defined\n"; |
| 209 | return make_error_code(LLDError::InvalidOption); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Rui Ueyama | e042fa9a | 2015-05-31 19:55:40 +0000 | [diff] [blame] | 212 | std::error_code SymbolTable::addUndefined(StringRef Name) { |
Rui Ueyama | 07e661f | 2015-06-03 05:39:12 +0000 | [diff] [blame] | 213 | return resolve(new (Alloc) Undefined(Name)); |
Rui Ueyama | e042fa9a | 2015-05-31 19:55:40 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Rui Ueyama | 360bace | 2015-05-31 22:31:31 +0000 | [diff] [blame] | 216 | // Resolve To, and make From an alias to To. |
| 217 | std::error_code SymbolTable::rename(StringRef From, StringRef To) { |
| 218 | SymbolBody *Body = new (Alloc) Undefined(To); |
| 219 | if (auto EC = resolve(Body)) |
| 220 | return EC; |
| 221 | Symtab[From]->Body = Body->getReplacement(); |
| 222 | return std::error_code(); |
| 223 | } |
| 224 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 225 | void SymbolTable::dump() { |
| 226 | for (auto &P : Symtab) { |
| 227 | Symbol *Ref = P.second; |
| 228 | if (auto *Body = dyn_cast<Defined>(Ref->Body)) |
| 229 | llvm::dbgs() << Twine::utohexstr(Config->ImageBase + Body->getRVA()) |
| 230 | << " " << Body->getName() << "\n"; |
| 231 | } |
| 232 | } |
| 233 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 234 | std::error_code SymbolTable::addCombinedLTOObject() { |
| 235 | if (BitcodeFiles.empty()) |
| 236 | return std::error_code(); |
| 237 | |
| 238 | llvm::LTOCodeGenerator CG; |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 239 | |
| 240 | // All symbols referenced by non-bitcode objects must be preserved. |
| 241 | for (std::unique_ptr<ObjectFile> &File : ObjectFiles) |
| 242 | for (SymbolBody *Body : File->getSymbols()) |
| 243 | if (auto *S = dyn_cast<DefinedBitcode>(Body->getReplacement())) |
Rui Ueyama | eb262ce | 2015-06-04 02:12:16 +0000 | [diff] [blame] | 244 | CG.addMustPreserveSymbol(S->getName()); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 245 | |
Rui Ueyama | eb262ce | 2015-06-04 02:12:16 +0000 | [diff] [blame] | 246 | // Likewise for other symbols that must be preserved. |
| 247 | for (StringRef Name : Config->GCRoots) |
| 248 | if (isa<DefinedBitcode>(Symtab[Name]->Body)) |
| 249 | CG.addMustPreserveSymbol(Name); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 250 | |
| 251 | CG.setModule(BitcodeFiles[0]->releaseModule()); |
| 252 | for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I) |
| 253 | CG.addModule(BitcodeFiles[I]->getModule()); |
| 254 | |
| 255 | std::string ErrMsg; |
| 256 | LTOObjectFile = CG.compile(false, false, false, ErrMsg); |
| 257 | if (!LTOObjectFile) { |
| 258 | llvm::errs() << ErrMsg << '\n'; |
| 259 | return make_error_code(LLDError::BrokenFile); |
| 260 | } |
| 261 | |
| 262 | // Create an object file and add it to the symbol table by replacing any |
| 263 | // DefinedBitcode symbols with the definitions in the object file. |
| 264 | auto Obj = new ObjectFile(LTOObjectFile->getMemBufferRef()); |
| 265 | ObjectFiles.emplace_back(Obj); |
| 266 | if (auto EC = Obj->parse()) |
| 267 | return EC; |
| 268 | for (SymbolBody *Body : Obj->getSymbols()) { |
| 269 | if (!Body->isExternal()) |
| 270 | continue; |
| 271 | |
| 272 | // Find an existing Symbol. We should not see any new symbols at this point. |
| 273 | StringRef Name = Body->getName(); |
| 274 | Symbol *Sym = Symtab[Name]; |
| 275 | if (!Sym) { |
| 276 | llvm::errs() << "LTO: unexpected new symbol: " << Name << '\n'; |
| 277 | return make_error_code(LLDError::BrokenFile); |
| 278 | } |
| 279 | Body->setBackref(Sym); |
| 280 | |
| 281 | if (isa<DefinedBitcode>(Sym->Body)) { |
| 282 | // The symbol should now be defined. |
| 283 | if (!isa<Defined>(Body)) { |
| 284 | llvm::errs() << "LTO: undefined symbol: " << Name << '\n'; |
| 285 | return make_error_code(LLDError::BrokenFile); |
| 286 | } |
| 287 | Sym->Body = Body; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return std::error_code(); |
| 292 | } |
| 293 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 294 | } // namespace coff |
| 295 | } // namespace lld |