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 | |
| 18 | namespace lld { |
| 19 | namespace coff { |
| 20 | |
| 21 | SymbolTable::SymbolTable() { |
| 22 | addInitialSymbol(new DefinedAbsolute("__ImageBase", Config->ImageBase)); |
| 23 | addInitialSymbol(new Undefined(Config->EntryName)); |
| 24 | } |
| 25 | |
| 26 | void SymbolTable::addInitialSymbol(SymbolBody *Body) { |
| 27 | OwnedSymbols.push_back(std::unique_ptr<SymbolBody>(Body)); |
| 28 | Symtab[Body->getName()] = new (Alloc) Symbol(Body); |
| 29 | } |
| 30 | |
| 31 | std::error_code SymbolTable::addFile(std::unique_ptr<InputFile> File) { |
| 32 | if (auto EC = File->parse()) |
| 33 | return EC; |
| 34 | InputFile *FileP = File.release(); |
| 35 | if (auto *P = dyn_cast<ObjectFile>(FileP)) |
| 36 | return addObject(P); |
| 37 | if (auto *P = dyn_cast<ArchiveFile>(FileP)) |
| 38 | return addArchive(P); |
| 39 | return addImport(cast<ImportFile>(FileP)); |
| 40 | } |
| 41 | |
| 42 | std::error_code SymbolTable::addObject(ObjectFile *File) { |
| 43 | ObjectFiles.emplace_back(File); |
| 44 | for (SymbolBody *Body : File->getSymbols()) |
| 45 | if (Body->isExternal()) |
| 46 | if (auto EC = resolve(Body)) |
| 47 | return EC; |
| 48 | |
| 49 | // If an object file contains .drectve section, read it and add |
| 50 | // files listed in the section. |
| 51 | StringRef Dir = File->getDirectives(); |
| 52 | if (!Dir.empty()) { |
| 53 | std::vector<std::unique_ptr<InputFile>> Libs; |
| 54 | if (auto EC = parseDirectives(Dir, &Libs, &StringAlloc)) |
| 55 | return EC; |
| 56 | for (std::unique_ptr<InputFile> &Lib : Libs) |
| 57 | addFile(std::move(Lib)); |
| 58 | } |
| 59 | return std::error_code(); |
| 60 | } |
| 61 | |
| 62 | std::error_code SymbolTable::addArchive(ArchiveFile *File) { |
| 63 | ArchiveFiles.emplace_back(File); |
| 64 | for (SymbolBody *Body : File->getSymbols()) |
| 65 | if (auto EC = resolve(Body)) |
| 66 | return EC; |
| 67 | return std::error_code(); |
| 68 | } |
| 69 | |
| 70 | std::error_code SymbolTable::addImport(ImportFile *File) { |
| 71 | ImportFiles.emplace_back(File); |
| 72 | for (SymbolBody *Body : File->getSymbols()) |
| 73 | if (auto EC = resolve(Body)) |
| 74 | return EC; |
| 75 | return std::error_code(); |
| 76 | } |
| 77 | |
| 78 | bool SymbolTable::reportRemainingUndefines() { |
| 79 | bool Ret = false; |
| 80 | for (auto &I : Symtab) { |
| 81 | Symbol *Sym = I.second; |
| 82 | auto *Undef = dyn_cast<Undefined>(Sym->Body); |
| 83 | if (!Undef) |
| 84 | continue; |
| 85 | if (SymbolBody *Alias = Undef->getWeakAlias()) { |
| 86 | Sym->Body = Alias->getReplacement(); |
| 87 | if (!isa<Defined>(Sym->Body)) { |
| 88 | // Aliases are yet another symbols pointed by other symbols |
| 89 | // that could also remain undefined. |
| 90 | llvm::errs() << "undefined symbol: " << Undef->getName() << "\n"; |
| 91 | Ret = true; |
| 92 | } |
| 93 | continue; |
| 94 | } |
| 95 | llvm::errs() << "undefined symbol: " << Undef->getName() << "\n"; |
| 96 | Ret = true; |
| 97 | } |
| 98 | return Ret; |
| 99 | } |
| 100 | |
| 101 | // This function resolves conflicts if there's an existing symbol with |
| 102 | // the same name. Decisions are made based on symbol type. |
| 103 | std::error_code SymbolTable::resolve(SymbolBody *New) { |
| 104 | // Find an existing Symbol or create and insert a new one. |
| 105 | StringRef Name = New->getName(); |
| 106 | Symbol *&Sym = Symtab[Name]; |
| 107 | if (!Sym) { |
| 108 | Sym = new (Alloc) Symbol(New); |
| 109 | New->setBackref(Sym); |
| 110 | return std::error_code(); |
| 111 | } |
| 112 | New->setBackref(Sym); |
| 113 | |
| 114 | // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, |
| 115 | // equivalent (conflicting), or more preferable, respectively. |
| 116 | SymbolBody *Existing = Sym->Body; |
| 117 | int comp = Existing->compare(New); |
| 118 | if (comp < 0) |
| 119 | Sym->Body = New; |
| 120 | if (comp == 0) |
| 121 | return make_dynamic_error_code(Twine("duplicate symbol: ") + Name); |
| 122 | |
| 123 | // If we have an Undefined symbol for a Lazy symbol, we need |
| 124 | // to read an archive member to replace the Lazy symbol with |
| 125 | // a Defined symbol. |
| 126 | if (isa<Undefined>(Existing) || isa<Undefined>(New)) |
| 127 | if (auto *B = dyn_cast<Lazy>(Sym->Body)) |
| 128 | return addMemberFile(B); |
| 129 | return std::error_code(); |
| 130 | } |
| 131 | |
| 132 | // Reads an archive member file pointed by a given symbol. |
| 133 | std::error_code SymbolTable::addMemberFile(Lazy *Body) { |
| 134 | auto FileOrErr = Body->getMember(); |
| 135 | if (auto EC = FileOrErr.getError()) |
| 136 | return EC; |
| 137 | std::unique_ptr<InputFile> File = std::move(FileOrErr.get()); |
| 138 | |
| 139 | // getMember returns an empty buffer if the member was already |
| 140 | // read from the library. |
| 141 | if (!File) |
| 142 | return std::error_code(); |
| 143 | if (Config->Verbose) |
| 144 | llvm::dbgs() << "Loaded " << File->getShortName() << " for " |
| 145 | << Body->getName() << "\n"; |
| 146 | return addFile(std::move(File)); |
| 147 | } |
| 148 | |
| 149 | std::vector<Chunk *> SymbolTable::getChunks() { |
| 150 | std::vector<Chunk *> Res; |
| 151 | for (std::unique_ptr<ObjectFile> &File : ObjectFiles) { |
| 152 | std::vector<Chunk *> &V = File->getChunks(); |
| 153 | Res.insert(Res.end(), V.begin(), V.end()); |
| 154 | } |
| 155 | return Res; |
| 156 | } |
| 157 | |
| 158 | SymbolBody *SymbolTable::find(StringRef Name) { |
| 159 | auto It = Symtab.find(Name); |
| 160 | if (It == Symtab.end()) |
| 161 | return nullptr; |
| 162 | return It->second->Body; |
| 163 | } |
| 164 | |
| 165 | void SymbolTable::dump() { |
| 166 | for (auto &P : Symtab) { |
| 167 | Symbol *Ref = P.second; |
| 168 | if (auto *Body = dyn_cast<Defined>(Ref->Body)) |
| 169 | llvm::dbgs() << Twine::utohexstr(Config->ImageBase + Body->getRVA()) |
| 170 | << " " << Body->getName() << "\n"; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | } // namespace coff |
| 175 | } // namespace lld |