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 | |
Peter Collingbourne | ace2f09 | 2015-06-06 02:00:45 +0000 | [diff] [blame] | 43 | std::error_code SymbolTable::addDirectives(StringRef Dir) { |
| 44 | if (Dir.empty()) |
| 45 | return std::error_code(); |
| 46 | std::vector<std::unique_ptr<InputFile>> Libs; |
| 47 | if (auto EC = Driver->parseDirectives(Dir, &Libs)) |
| 48 | return EC; |
| 49 | for (std::unique_ptr<InputFile> &Lib : Libs) |
| 50 | addFile(std::move(Lib)); |
| 51 | return std::error_code(); |
| 52 | } |
| 53 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 54 | std::error_code SymbolTable::addObject(ObjectFile *File) { |
| 55 | ObjectFiles.emplace_back(File); |
| 56 | for (SymbolBody *Body : File->getSymbols()) |
| 57 | if (Body->isExternal()) |
| 58 | if (auto EC = resolve(Body)) |
| 59 | return EC; |
| 60 | |
| 61 | // If an object file contains .drectve section, read it and add |
| 62 | // files listed in the section. |
Peter Collingbourne | ace2f09 | 2015-06-06 02:00:45 +0000 | [diff] [blame] | 63 | return addDirectives(File->getDirectives()); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | std::error_code SymbolTable::addArchive(ArchiveFile *File) { |
| 67 | ArchiveFiles.emplace_back(File); |
| 68 | for (SymbolBody *Body : File->getSymbols()) |
| 69 | if (auto EC = resolve(Body)) |
| 70 | return EC; |
| 71 | return std::error_code(); |
| 72 | } |
| 73 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 74 | std::error_code SymbolTable::addBitcode(BitcodeFile *File) { |
| 75 | BitcodeFiles.emplace_back(File); |
| 76 | for (SymbolBody *Body : File->getSymbols()) |
| 77 | if (Body->isExternal()) |
| 78 | if (auto EC = resolve(Body)) |
| 79 | return EC; |
| 80 | |
Peter Collingbourne | ace2f09 | 2015-06-06 02:00:45 +0000 | [diff] [blame] | 81 | // Add any linker directives from the module flags metadata. |
| 82 | return addDirectives(File->getDirectives()); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 85 | std::error_code SymbolTable::addImport(ImportFile *File) { |
| 86 | ImportFiles.emplace_back(File); |
| 87 | for (SymbolBody *Body : File->getSymbols()) |
| 88 | if (auto EC = resolve(Body)) |
| 89 | return EC; |
| 90 | return std::error_code(); |
| 91 | } |
| 92 | |
| 93 | bool SymbolTable::reportRemainingUndefines() { |
| 94 | bool Ret = false; |
| 95 | for (auto &I : Symtab) { |
| 96 | Symbol *Sym = I.second; |
| 97 | auto *Undef = dyn_cast<Undefined>(Sym->Body); |
| 98 | if (!Undef) |
| 99 | continue; |
| 100 | if (SymbolBody *Alias = Undef->getWeakAlias()) { |
| 101 | Sym->Body = Alias->getReplacement(); |
| 102 | if (!isa<Defined>(Sym->Body)) { |
| 103 | // Aliases are yet another symbols pointed by other symbols |
| 104 | // that could also remain undefined. |
| 105 | llvm::errs() << "undefined symbol: " << Undef->getName() << "\n"; |
| 106 | Ret = true; |
| 107 | } |
| 108 | continue; |
| 109 | } |
| 110 | llvm::errs() << "undefined symbol: " << Undef->getName() << "\n"; |
| 111 | Ret = true; |
| 112 | } |
| 113 | return Ret; |
| 114 | } |
| 115 | |
| 116 | // This function resolves conflicts if there's an existing symbol with |
| 117 | // the same name. Decisions are made based on symbol type. |
| 118 | std::error_code SymbolTable::resolve(SymbolBody *New) { |
| 119 | // Find an existing Symbol or create and insert a new one. |
| 120 | StringRef Name = New->getName(); |
| 121 | Symbol *&Sym = Symtab[Name]; |
| 122 | if (!Sym) { |
| 123 | Sym = new (Alloc) Symbol(New); |
| 124 | New->setBackref(Sym); |
| 125 | return std::error_code(); |
| 126 | } |
| 127 | New->setBackref(Sym); |
| 128 | |
| 129 | // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, |
| 130 | // equivalent (conflicting), or more preferable, respectively. |
| 131 | SymbolBody *Existing = Sym->Body; |
| 132 | int comp = Existing->compare(New); |
| 133 | if (comp < 0) |
| 134 | Sym->Body = New; |
Rui Ueyama | 8fd9fb9 | 2015-06-01 02:58:15 +0000 | [diff] [blame] | 135 | if (comp == 0) { |
| 136 | llvm::errs() << "duplicate symbol: " << Name << "\n"; |
| 137 | return make_error_code(LLDError::DuplicateSymbols); |
| 138 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 139 | |
| 140 | // If we have an Undefined symbol for a Lazy symbol, we need |
| 141 | // to read an archive member to replace the Lazy symbol with |
| 142 | // a Defined symbol. |
| 143 | if (isa<Undefined>(Existing) || isa<Undefined>(New)) |
| 144 | if (auto *B = dyn_cast<Lazy>(Sym->Body)) |
| 145 | return addMemberFile(B); |
| 146 | return std::error_code(); |
| 147 | } |
| 148 | |
| 149 | // Reads an archive member file pointed by a given symbol. |
| 150 | std::error_code SymbolTable::addMemberFile(Lazy *Body) { |
| 151 | auto FileOrErr = Body->getMember(); |
| 152 | if (auto EC = FileOrErr.getError()) |
| 153 | return EC; |
| 154 | std::unique_ptr<InputFile> File = std::move(FileOrErr.get()); |
| 155 | |
| 156 | // getMember returns an empty buffer if the member was already |
| 157 | // read from the library. |
| 158 | if (!File) |
| 159 | return std::error_code(); |
| 160 | if (Config->Verbose) |
| 161 | llvm::dbgs() << "Loaded " << File->getShortName() << " for " |
| 162 | << Body->getName() << "\n"; |
| 163 | return addFile(std::move(File)); |
| 164 | } |
| 165 | |
| 166 | std::vector<Chunk *> SymbolTable::getChunks() { |
| 167 | std::vector<Chunk *> Res; |
| 168 | for (std::unique_ptr<ObjectFile> &File : ObjectFiles) { |
| 169 | std::vector<Chunk *> &V = File->getChunks(); |
| 170 | Res.insert(Res.end(), V.begin(), V.end()); |
| 171 | } |
| 172 | return Res; |
| 173 | } |
| 174 | |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 175 | Defined *SymbolTable::find(StringRef Name) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 176 | auto It = Symtab.find(Name); |
| 177 | if (It == Symtab.end()) |
| 178 | return nullptr; |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 179 | if (auto *Def = dyn_cast<Defined>(It->second->Body)) |
| 180 | return Def; |
| 181 | return nullptr; |
| 182 | } |
| 183 | |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 184 | // Windows specific -- Link default entry point name. |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 185 | ErrorOr<StringRef> SymbolTable::findDefaultEntry() { |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 186 | // User-defined main functions and their corresponding entry points. |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 187 | static const char *Entries[][2] = { |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 188 | {"main", "mainCRTStartup"}, |
| 189 | {"wmain", "wmainCRTStartup"}, |
| 190 | {"WinMain", "WinMainCRTStartup"}, |
| 191 | {"wWinMain", "wWinMainCRTStartup"}, |
| 192 | }; |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 193 | for (auto E : Entries) { |
| 194 | if (find(E[1])) |
| 195 | return StringRef(E[1]); |
| 196 | if (!find(E[0])) |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 197 | continue; |
Rui Ueyama | 07e661f | 2015-06-03 05:39:12 +0000 | [diff] [blame] | 198 | if (auto EC = resolve(new (Alloc) Undefined(E[1]))) |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 199 | return EC; |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 200 | return StringRef(E[1]); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 201 | } |
Rui Ueyama | 8fd9fb9 | 2015-06-01 02:58:15 +0000 | [diff] [blame] | 202 | llvm::errs() << "entry point must be defined\n"; |
| 203 | return make_error_code(LLDError::InvalidOption); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Rui Ueyama | e042fa9a | 2015-05-31 19:55:40 +0000 | [diff] [blame] | 206 | std::error_code SymbolTable::addUndefined(StringRef Name) { |
Rui Ueyama | 07e661f | 2015-06-03 05:39:12 +0000 | [diff] [blame] | 207 | return resolve(new (Alloc) Undefined(Name)); |
Rui Ueyama | e042fa9a | 2015-05-31 19:55:40 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Rui Ueyama | 360bace | 2015-05-31 22:31:31 +0000 | [diff] [blame] | 210 | // Resolve To, and make From an alias to To. |
| 211 | std::error_code SymbolTable::rename(StringRef From, StringRef To) { |
| 212 | SymbolBody *Body = new (Alloc) Undefined(To); |
| 213 | if (auto EC = resolve(Body)) |
| 214 | return EC; |
| 215 | Symtab[From]->Body = Body->getReplacement(); |
| 216 | return std::error_code(); |
| 217 | } |
| 218 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 219 | void SymbolTable::dump() { |
| 220 | for (auto &P : Symtab) { |
| 221 | Symbol *Ref = P.second; |
| 222 | if (auto *Body = dyn_cast<Defined>(Ref->Body)) |
| 223 | llvm::dbgs() << Twine::utohexstr(Config->ImageBase + Body->getRVA()) |
| 224 | << " " << Body->getName() << "\n"; |
| 225 | } |
| 226 | } |
| 227 | |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 228 | std::error_code SymbolTable::addCombinedLTOObject() { |
| 229 | if (BitcodeFiles.empty()) |
| 230 | return std::error_code(); |
| 231 | |
| 232 | llvm::LTOCodeGenerator CG; |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 233 | |
| 234 | // All symbols referenced by non-bitcode objects must be preserved. |
| 235 | for (std::unique_ptr<ObjectFile> &File : ObjectFiles) |
| 236 | for (SymbolBody *Body : File->getSymbols()) |
| 237 | if (auto *S = dyn_cast<DefinedBitcode>(Body->getReplacement())) |
Rui Ueyama | eb262ce | 2015-06-04 02:12:16 +0000 | [diff] [blame] | 238 | CG.addMustPreserveSymbol(S->getName()); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 239 | |
Rui Ueyama | eb262ce | 2015-06-04 02:12:16 +0000 | [diff] [blame] | 240 | // Likewise for other symbols that must be preserved. |
| 241 | for (StringRef Name : Config->GCRoots) |
| 242 | if (isa<DefinedBitcode>(Symtab[Name]->Body)) |
| 243 | CG.addMustPreserveSymbol(Name); |
Peter Collingbourne | 60c1616 | 2015-06-01 20:10:10 +0000 | [diff] [blame] | 244 | |
| 245 | CG.setModule(BitcodeFiles[0]->releaseModule()); |
| 246 | for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I) |
| 247 | CG.addModule(BitcodeFiles[I]->getModule()); |
| 248 | |
| 249 | std::string ErrMsg; |
| 250 | LTOObjectFile = CG.compile(false, false, false, ErrMsg); |
| 251 | if (!LTOObjectFile) { |
| 252 | llvm::errs() << ErrMsg << '\n'; |
| 253 | return make_error_code(LLDError::BrokenFile); |
| 254 | } |
| 255 | |
| 256 | // Create an object file and add it to the symbol table by replacing any |
| 257 | // DefinedBitcode symbols with the definitions in the object file. |
| 258 | auto Obj = new ObjectFile(LTOObjectFile->getMemBufferRef()); |
| 259 | ObjectFiles.emplace_back(Obj); |
| 260 | if (auto EC = Obj->parse()) |
| 261 | return EC; |
| 262 | for (SymbolBody *Body : Obj->getSymbols()) { |
| 263 | if (!Body->isExternal()) |
| 264 | continue; |
| 265 | |
| 266 | // Find an existing Symbol. We should not see any new symbols at this point. |
| 267 | StringRef Name = Body->getName(); |
| 268 | Symbol *Sym = Symtab[Name]; |
| 269 | if (!Sym) { |
| 270 | llvm::errs() << "LTO: unexpected new symbol: " << Name << '\n'; |
| 271 | return make_error_code(LLDError::BrokenFile); |
| 272 | } |
| 273 | Body->setBackref(Sym); |
| 274 | |
| 275 | if (isa<DefinedBitcode>(Sym->Body)) { |
| 276 | // The symbol should now be defined. |
| 277 | if (!isa<Defined>(Body)) { |
| 278 | llvm::errs() << "LTO: undefined symbol: " << Name << '\n'; |
| 279 | return make_error_code(LLDError::BrokenFile); |
| 280 | } |
| 281 | Sym->Body = Body; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return std::error_code(); |
| 286 | } |
| 287 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 288 | } // namespace coff |
| 289 | } // namespace lld |