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" |
| 12 | #include "SymbolTable.h" |
| 13 | #include "lld/Core/Error.h" |
| 14 | #include "llvm/ADT/STLExtras.h" |
| 15 | #include "llvm/Support/Debug.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | |
Rui Ueyama | d68ff34 | 2015-05-31 03:57:30 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 20 | namespace lld { |
| 21 | namespace coff { |
| 22 | |
| 23 | SymbolTable::SymbolTable() { |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 24 | addSymbol(new DefinedAbsolute("__ImageBase", Config->ImageBase)); |
| 25 | if (!Config->EntryName.empty()) |
Rui Ueyama | e042fa9a | 2015-05-31 19:55:40 +0000 | [diff] [blame] | 26 | addUndefined(Config->EntryName); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | std::error_code SymbolTable::addFile(std::unique_ptr<InputFile> File) { |
| 30 | if (auto EC = File->parse()) |
| 31 | return EC; |
| 32 | InputFile *FileP = File.release(); |
| 33 | if (auto *P = dyn_cast<ObjectFile>(FileP)) |
| 34 | return addObject(P); |
| 35 | if (auto *P = dyn_cast<ArchiveFile>(FileP)) |
| 36 | return addArchive(P); |
| 37 | return addImport(cast<ImportFile>(FileP)); |
| 38 | } |
| 39 | |
| 40 | std::error_code SymbolTable::addObject(ObjectFile *File) { |
| 41 | ObjectFiles.emplace_back(File); |
| 42 | for (SymbolBody *Body : File->getSymbols()) |
| 43 | if (Body->isExternal()) |
| 44 | if (auto EC = resolve(Body)) |
| 45 | return EC; |
| 46 | |
| 47 | // If an object file contains .drectve section, read it and add |
| 48 | // files listed in the section. |
| 49 | StringRef Dir = File->getDirectives(); |
| 50 | if (!Dir.empty()) { |
| 51 | std::vector<std::unique_ptr<InputFile>> Libs; |
Rui Ueyama | a9cbbf8 | 2015-05-31 19:17:09 +0000 | [diff] [blame] | 52 | if (auto EC = Driver->parseDirectives(Dir, &Libs)) |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 53 | return EC; |
| 54 | for (std::unique_ptr<InputFile> &Lib : Libs) |
| 55 | addFile(std::move(Lib)); |
| 56 | } |
| 57 | return std::error_code(); |
| 58 | } |
| 59 | |
| 60 | std::error_code SymbolTable::addArchive(ArchiveFile *File) { |
| 61 | ArchiveFiles.emplace_back(File); |
| 62 | for (SymbolBody *Body : File->getSymbols()) |
| 63 | if (auto EC = resolve(Body)) |
| 64 | return EC; |
| 65 | return std::error_code(); |
| 66 | } |
| 67 | |
| 68 | std::error_code SymbolTable::addImport(ImportFile *File) { |
| 69 | ImportFiles.emplace_back(File); |
| 70 | for (SymbolBody *Body : File->getSymbols()) |
| 71 | if (auto EC = resolve(Body)) |
| 72 | return EC; |
| 73 | return std::error_code(); |
| 74 | } |
| 75 | |
| 76 | bool SymbolTable::reportRemainingUndefines() { |
| 77 | bool Ret = false; |
| 78 | for (auto &I : Symtab) { |
| 79 | Symbol *Sym = I.second; |
| 80 | auto *Undef = dyn_cast<Undefined>(Sym->Body); |
| 81 | if (!Undef) |
| 82 | continue; |
| 83 | if (SymbolBody *Alias = Undef->getWeakAlias()) { |
| 84 | Sym->Body = Alias->getReplacement(); |
| 85 | if (!isa<Defined>(Sym->Body)) { |
| 86 | // Aliases are yet another symbols pointed by other symbols |
| 87 | // that could also remain undefined. |
| 88 | llvm::errs() << "undefined symbol: " << Undef->getName() << "\n"; |
| 89 | Ret = true; |
| 90 | } |
| 91 | continue; |
| 92 | } |
| 93 | llvm::errs() << "undefined symbol: " << Undef->getName() << "\n"; |
| 94 | Ret = true; |
| 95 | } |
| 96 | return Ret; |
| 97 | } |
| 98 | |
| 99 | // This function resolves conflicts if there's an existing symbol with |
| 100 | // the same name. Decisions are made based on symbol type. |
| 101 | std::error_code SymbolTable::resolve(SymbolBody *New) { |
| 102 | // Find an existing Symbol or create and insert a new one. |
| 103 | StringRef Name = New->getName(); |
| 104 | Symbol *&Sym = Symtab[Name]; |
| 105 | if (!Sym) { |
| 106 | Sym = new (Alloc) Symbol(New); |
| 107 | New->setBackref(Sym); |
| 108 | return std::error_code(); |
| 109 | } |
| 110 | New->setBackref(Sym); |
| 111 | |
| 112 | // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, |
| 113 | // equivalent (conflicting), or more preferable, respectively. |
| 114 | SymbolBody *Existing = Sym->Body; |
| 115 | int comp = Existing->compare(New); |
| 116 | if (comp < 0) |
| 117 | Sym->Body = New; |
| 118 | if (comp == 0) |
| 119 | return make_dynamic_error_code(Twine("duplicate symbol: ") + Name); |
| 120 | |
| 121 | // If we have an Undefined symbol for a Lazy symbol, we need |
| 122 | // to read an archive member to replace the Lazy symbol with |
| 123 | // a Defined symbol. |
| 124 | if (isa<Undefined>(Existing) || isa<Undefined>(New)) |
| 125 | if (auto *B = dyn_cast<Lazy>(Sym->Body)) |
| 126 | return addMemberFile(B); |
| 127 | return std::error_code(); |
| 128 | } |
| 129 | |
| 130 | // Reads an archive member file pointed by a given symbol. |
| 131 | std::error_code SymbolTable::addMemberFile(Lazy *Body) { |
| 132 | auto FileOrErr = Body->getMember(); |
| 133 | if (auto EC = FileOrErr.getError()) |
| 134 | return EC; |
| 135 | std::unique_ptr<InputFile> File = std::move(FileOrErr.get()); |
| 136 | |
| 137 | // getMember returns an empty buffer if the member was already |
| 138 | // read from the library. |
| 139 | if (!File) |
| 140 | return std::error_code(); |
| 141 | if (Config->Verbose) |
| 142 | llvm::dbgs() << "Loaded " << File->getShortName() << " for " |
| 143 | << Body->getName() << "\n"; |
| 144 | return addFile(std::move(File)); |
| 145 | } |
| 146 | |
| 147 | std::vector<Chunk *> SymbolTable::getChunks() { |
| 148 | std::vector<Chunk *> Res; |
| 149 | for (std::unique_ptr<ObjectFile> &File : ObjectFiles) { |
| 150 | std::vector<Chunk *> &V = File->getChunks(); |
| 151 | Res.insert(Res.end(), V.begin(), V.end()); |
| 152 | } |
| 153 | return Res; |
| 154 | } |
| 155 | |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 156 | Defined *SymbolTable::find(StringRef Name) { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 157 | auto It = Symtab.find(Name); |
| 158 | if (It == Symtab.end()) |
| 159 | return nullptr; |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 160 | if (auto *Def = dyn_cast<Defined>(It->second->Body)) |
| 161 | return Def; |
| 162 | return nullptr; |
| 163 | } |
| 164 | |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 165 | // Windows specific -- Link default entry point name. |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 166 | ErrorOr<StringRef> SymbolTable::findDefaultEntry() { |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 167 | // User-defined main functions and their corresponding entry points. |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 168 | static const char *Entries[][2] = { |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 169 | {"main", "mainCRTStartup"}, |
| 170 | {"wmain", "wmainCRTStartup"}, |
| 171 | {"WinMain", "WinMainCRTStartup"}, |
| 172 | {"wWinMain", "wWinMainCRTStartup"}, |
| 173 | }; |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 174 | for (auto E : Entries) { |
| 175 | if (find(E[1])) |
| 176 | return StringRef(E[1]); |
| 177 | if (!find(E[0])) |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 178 | continue; |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 179 | if (auto EC = addSymbol(new Undefined(E[1]))) |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 180 | return EC; |
Rui Ueyama | 80b5689 | 2015-05-31 16:10:50 +0000 | [diff] [blame] | 181 | return StringRef(E[1]); |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 182 | } |
| 183 | return make_dynamic_error_code("entry point must be defined"); |
| 184 | } |
| 185 | |
Rui Ueyama | e042fa9a | 2015-05-31 19:55:40 +0000 | [diff] [blame] | 186 | std::error_code SymbolTable::addUndefined(StringRef Name) { |
| 187 | return addSymbol(new Undefined(Name)); |
| 188 | } |
| 189 | |
Rui Ueyama | 360bace | 2015-05-31 22:31:31 +0000 | [diff] [blame^] | 190 | // Resolve To, and make From an alias to To. |
| 191 | std::error_code SymbolTable::rename(StringRef From, StringRef To) { |
| 192 | SymbolBody *Body = new (Alloc) Undefined(To); |
| 193 | if (auto EC = resolve(Body)) |
| 194 | return EC; |
| 195 | Symtab[From]->Body = Body->getReplacement(); |
| 196 | return std::error_code(); |
| 197 | } |
| 198 | |
Rui Ueyama | 5cff685 | 2015-05-31 03:34:08 +0000 | [diff] [blame] | 199 | std::error_code SymbolTable::addSymbol(SymbolBody *Body) { |
| 200 | OwningSymbols.push_back(std::unique_ptr<SymbolBody>(Body)); |
| 201 | return resolve(Body); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void SymbolTable::dump() { |
| 205 | for (auto &P : Symtab) { |
| 206 | Symbol *Ref = P.second; |
| 207 | if (auto *Body = dyn_cast<Defined>(Ref->Body)) |
| 208 | llvm::dbgs() << Twine::utohexstr(Config->ImageBase + Body->getRVA()) |
| 209 | << " " << Body->getName() << "\n"; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | } // namespace coff |
| 214 | } // namespace lld |