Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +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 "SymbolTable.h" |
Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 11 | #include "Error.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 12 | #include "Symbols.h" |
| 13 | |
| 14 | using namespace llvm; |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 15 | using namespace llvm::object; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace lld; |
| 18 | using namespace lld::elf2; |
| 19 | |
Michael J. Spencer | ac5f048 | 2015-09-08 22:51:46 +0000 | [diff] [blame] | 20 | SymbolTable::SymbolTable() {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 21 | |
Rafael Espindola | 2ffdd4d | 2015-08-04 14:29:01 +0000 | [diff] [blame] | 22 | void SymbolTable::addFile(std::unique_ptr<InputFile> File) { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | File->parse(); |
| 24 | InputFile *FileP = File.release(); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 25 | if (auto *AF = dyn_cast<ArchiveFile>(FileP)) { |
| 26 | ArchiveFiles.emplace_back(AF); |
| 27 | for (Lazy &Sym : AF->getLazySymbols()) |
| 28 | addLazy(&Sym); |
| 29 | return; |
| 30 | } |
| 31 | addELFFile(cast<ELFFileBase>(FileP)); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 34 | template <class ELFT> void SymbolTable::init() { |
Michael J. Spencer | 546c64c | 2015-09-08 22:34:57 +0000 | [diff] [blame] | 35 | EntrySym = new (Alloc) Undefined<ELFT>("_start", Undefined<ELFT>::Synthetic); |
| 36 | resolve<ELFT>(EntrySym); |
Rafael Espindola | f7d45f0 | 2015-08-31 01:46:20 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Rafael Espindola | 824d1a9 | 2015-09-04 00:09:43 +0000 | [diff] [blame] | 39 | template <class ELFT> void SymbolTable::addELFFile(ELFFileBase *File) { |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 40 | if (const ELFFileBase *Old = getFirstELF()) { |
Rafael Espindola | 8aeb13f | 2015-09-03 19:13:13 +0000 | [diff] [blame] | 41 | if (!Old->isCompatibleWith(*File)) |
| 42 | error(Twine(Old->getName() + " is incompatible with " + File->getName())); |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 43 | } else { |
Rafael Espindola | 824d1a9 | 2015-09-04 00:09:43 +0000 | [diff] [blame] | 44 | init<ELFT>(); |
Rafael Espindola | 3c9cb4b | 2015-08-05 12:03:34 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 47 | if (auto *O = dyn_cast<ObjectFileBase>(File)) { |
| 48 | ObjectFiles.emplace_back(O); |
Rafael Espindola | 824d1a9 | 2015-09-04 00:09:43 +0000 | [diff] [blame] | 49 | for (SymbolBody *Body : O->getSymbols()) |
| 50 | resolve<ELFT>(Body); |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 51 | } |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 52 | |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 53 | if (auto *S = dyn_cast<SharedFile<ELFT>>(File)) { |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 54 | SharedFiles.emplace_back(S); |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 55 | for (SharedSymbol<ELFT> &Body : S->getSharedSymbols()) |
| 56 | resolve<ELFT>(&Body); |
| 57 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Rafael Espindola | 824d1a9 | 2015-09-04 00:09:43 +0000 | [diff] [blame] | 60 | void SymbolTable::addELFFile(ELFFileBase *File) { |
| 61 | switch (File->getELFKind()) { |
Rui Ueyama | d5004e1 | 2015-09-09 18:02:23 +0000 | [diff] [blame^] | 62 | case ELF32LEKind: |
| 63 | addELFFile<ELF32LE>(File); |
| 64 | break; |
| 65 | case ELF32BEKind: |
| 66 | addELFFile<ELF32BE>(File); |
| 67 | break; |
| 68 | case ELF64LEKind: |
| 69 | addELFFile<ELF64LE>(File); |
| 70 | break; |
| 71 | case ELF64BEKind: |
| 72 | addELFFile<ELF64BE>(File); |
| 73 | break; |
Rafael Espindola | 824d1a9 | 2015-09-04 00:09:43 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 77 | // This function resolves conflicts if there's an existing symbol with |
| 78 | // the same name. Decisions are made based on symbol type. |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 79 | template <class ELFT> void SymbolTable::resolve(SymbolBody *New) { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 80 | Symbol *Sym = insert(New); |
| 81 | if (Sym->Body == New) |
| 82 | return; |
| 83 | |
| 84 | SymbolBody *Existing = Sym->Body; |
| 85 | |
| 86 | if (Lazy *L = dyn_cast<Lazy>(Existing)) { |
| 87 | if (New->isUndefined()) { |
| 88 | addMemberFile(L); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // Found a definition for something also in an archive. Ignore the archive |
| 93 | // definition. |
| 94 | Sym->Body = New; |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, |
| 99 | // equivalent (conflicting), or more preferable, respectively. |
| 100 | int comp = Existing->compare<ELFT>(New); |
| 101 | if (comp < 0) |
| 102 | Sym->Body = New; |
| 103 | if (comp == 0) |
| 104 | error(Twine("duplicate symbol: ") + Sym->Body->getName()); |
| 105 | } |
| 106 | |
| 107 | Symbol *SymbolTable::insert(SymbolBody *New) { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 108 | // Find an existing Symbol or create and insert a new one. |
| 109 | StringRef Name = New->getName(); |
| 110 | Symbol *&Sym = Symtab[Name]; |
| 111 | if (!Sym) { |
| 112 | Sym = new (Alloc) Symbol(New); |
| 113 | New->setBackref(Sym); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 114 | return Sym; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 115 | } |
| 116 | New->setBackref(Sym); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 117 | return Sym; |
| 118 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 119 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 120 | void SymbolTable::addLazy(Lazy *New) { |
| 121 | Symbol *Sym = insert(New); |
| 122 | if (Sym->Body == New) |
| 123 | return; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 124 | SymbolBody *Existing = Sym->Body; |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 125 | if (Existing->isDefined() || Existing->isLazy()) |
| 126 | return; |
| 127 | Sym->Body = New; |
| 128 | assert(Existing->isUndefined() && "Unexpected symbol kind."); |
| 129 | addMemberFile(New); |
| 130 | } |
| 131 | |
| 132 | void SymbolTable::addMemberFile(Lazy *Body) { |
| 133 | std::unique_ptr<InputFile> File = Body->getMember(); |
| 134 | |
| 135 | // getMember returns nullptr if the member was already read from the library. |
| 136 | if (!File) |
| 137 | return; |
| 138 | |
| 139 | addFile(std::move(File)); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 140 | } |