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 | 4340aad | 2015-09-11 22:42:45 +0000 | [diff] [blame] | 11 | #include "Config.h" |
Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 12 | #include "Error.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 13 | #include "Symbols.h" |
| 14 | |
| 15 | using namespace llvm; |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 16 | using namespace llvm::object; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 17 | using namespace llvm::ELF; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace lld; |
| 20 | using namespace lld::elf2; |
| 21 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 22 | template <class ELFT> SymbolTable<ELFT>::SymbolTable() {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 24 | template <class ELFT> bool SymbolTable<ELFT>::shouldUseRela() const { |
Rafael Espindola | 67a5da6 | 2015-09-17 14:02:10 +0000 | [diff] [blame] | 25 | ELFKind K = getFirstELF()->getELFKind(); |
| 26 | return K == ELF64LEKind || K == ELF64BEKind; |
| 27 | } |
| 28 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 29 | template <class ELFT> |
| 30 | void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) { |
Rafael Espindola | 525914d | 2015-10-11 03:36:49 +0000 | [diff] [blame] | 31 | |
Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 32 | if (auto *E = dyn_cast<ELFFileBase<ELFT>>(File.get())) { |
Rafael Espindola | 525914d | 2015-10-11 03:36:49 +0000 | [diff] [blame] | 33 | if (E->getELFKind() != Config->ElfKind || |
| 34 | E->getEMachine() != Config->EMachine) { |
| 35 | StringRef A = E->getName(); |
| 36 | StringRef B = Config->Emulation; |
| 37 | if (B.empty()) |
Rafael Espindola | 9afbac4 | 2015-10-11 20:19:20 +0000 | [diff] [blame] | 38 | B = Config->FirstElf->getName(); |
Rafael Espindola | 525914d | 2015-10-11 03:36:49 +0000 | [diff] [blame] | 39 | error(A + " is incompatible with " + B); |
| 40 | } |
| 41 | } |
| 42 | |
Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 43 | if (auto *AF = dyn_cast<ArchiveFile>(File.get())) { |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 44 | ArchiveFiles.emplace_back(std::move(File)); |
Igor Kudrin | 2696bbe | 2015-10-01 18:02:21 +0000 | [diff] [blame] | 45 | AF->parse(); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 46 | for (Lazy &Sym : AF->getLazySymbols()) |
| 47 | addLazy(&Sym); |
| 48 | return; |
| 49 | } |
Rafael Espindola | dfce5a2 | 2015-10-12 02:22:58 +0000 | [diff] [blame] | 50 | if (auto *S = dyn_cast<SharedFile<ELFT>>(File.get())) { |
Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 51 | S->parseSoName(); |
| 52 | if (!IncludedSoNames.insert(S->getSoName()).second) |
| 53 | return; |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 54 | S->parse(); |
| 55 | } else { |
Rafael Espindola | dfce5a2 | 2015-10-12 02:22:58 +0000 | [diff] [blame] | 56 | cast<ObjectFile<ELFT>>(File.get())->parse(Comdats); |
Rafael Espindola | 6a3b5de | 2015-10-01 19:52:48 +0000 | [diff] [blame] | 57 | } |
Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 58 | addELFFile(cast<ELFFileBase<ELFT>>(File.release())); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 61 | template <class ELFT> |
| 62 | SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) { |
| 63 | auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Required); |
| 64 | resolve(Sym); |
| 65 | return Sym; |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Rui Ueyama | ff77768 | 2015-10-09 21:12:40 +0000 | [diff] [blame] | 68 | template <class ELFT> |
| 69 | SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) { |
| 70 | auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Optional); |
| 71 | resolve(Sym); |
| 72 | return Sym; |
Denis Protivensky | 22220d5 | 2015-10-05 09:43:57 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 75 | template <class ELFT> |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 76 | void SymbolTable<ELFT>::addSyntheticSym(StringRef Name, |
| 77 | OutputSection<ELFT> &Section, |
| 78 | typename ELFFile<ELFT>::uintX_t Value) { |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 79 | typedef typename DefinedSynthetic<ELFT>::Elf_Sym Elf_Sym; |
| 80 | auto ESym = new (Alloc) Elf_Sym; |
| 81 | memset(ESym, 0, sizeof(Elf_Sym)); |
| 82 | ESym->st_value = Value; |
| 83 | auto Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, *ESym, Section); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 84 | resolve(Sym); |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 87 | template <class ELFT> void SymbolTable<ELFT>::addIgnoredSym(StringRef Name) { |
Rafael Espindola | 5d41326 | 2015-10-01 21:22:26 +0000 | [diff] [blame] | 88 | auto Sym = new (Alloc) |
| 89 | DefinedAbsolute<ELFT>(Name, DefinedAbsolute<ELFT>::IgnoreUndef); |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 90 | resolve(Sym); |
Rafael Espindola | 5d41326 | 2015-10-01 21:22:26 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 93 | template <class ELFT> |
| 94 | void SymbolTable<ELFT>::addELFFile(ELFFileBase<ELFT> *File) { |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 95 | if (auto *O = dyn_cast<ObjectFile<ELFT>>(File)) |
Rafael Espindola | b90582db | 2015-10-06 15:03:52 +0000 | [diff] [blame] | 96 | ObjectFiles.emplace_back(O); |
| 97 | else if (auto *S = dyn_cast<SharedFile<ELFT>>(File)) |
| 98 | SharedFiles.emplace_back(S); |
| 99 | |
Rafael Espindola | dfce5a2 | 2015-10-12 02:22:58 +0000 | [diff] [blame] | 100 | if (auto *O = dyn_cast<ObjectFile<ELFT>>(File)) { |
Rafael Espindola | 824d1a9 | 2015-09-04 00:09:43 +0000 | [diff] [blame] | 101 | for (SymbolBody *Body : O->getSymbols()) |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 102 | resolve(Body); |
Rafael Espindola | daa92a6 | 2015-08-31 01:16:19 +0000 | [diff] [blame] | 103 | } |
Rafael Espindola | f98d6d8 | 2015-09-03 20:03:54 +0000 | [diff] [blame] | 104 | |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 105 | if (auto *S = dyn_cast<SharedFile<ELFT>>(File)) { |
Rafael Espindola | 18173d4 | 2015-09-08 15:50:05 +0000 | [diff] [blame] | 106 | for (SharedSymbol<ELFT> &Body : S->getSharedSymbols()) |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 107 | resolve(&Body); |
Rafael Espindola | 824d1a9 | 2015-09-04 00:09:43 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 111 | template <class ELFT> |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 112 | void SymbolTable<ELFT>::reportConflict(const Twine &Message, |
| 113 | const SymbolBody &Old, |
| 114 | const SymbolBody &New, bool Warning) { |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 115 | typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 116 | typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; |
| 117 | |
| 118 | const Elf_Sym &OldE = cast<ELFSymbolBody<ELFT>>(Old).Sym; |
| 119 | const Elf_Sym &NewE = cast<ELFSymbolBody<ELFT>>(New).Sym; |
Rafael Espindola | af70764 | 2015-10-12 01:55:32 +0000 | [diff] [blame] | 120 | ELFFileBase<ELFT> *OldFile = nullptr; |
| 121 | ELFFileBase<ELFT> *NewFile = nullptr; |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 122 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 123 | for (const std::unique_ptr<ObjectFile<ELFT>> &File : ObjectFiles) { |
| 124 | Elf_Sym_Range Syms = File->getObj().symbols(File->getSymbolTable()); |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 125 | if (&OldE > Syms.begin() && &OldE < Syms.end()) |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 126 | OldFile = File.get(); |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 127 | if (&NewE > Syms.begin() && &NewE < Syms.end()) |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 128 | NewFile = File.get(); |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 131 | std::string Msg = (Message + ": " + Old.getName() + " in " + |
Rafael Espindola | 4b2ca85 | 2015-09-28 20:30:11 +0000 | [diff] [blame] | 132 | OldFile->getName() + " and " + NewFile->getName()) |
| 133 | .str(); |
Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 134 | if (Warning) |
Rafael Espindola | 4b2ca85 | 2015-09-28 20:30:11 +0000 | [diff] [blame] | 135 | warning(Msg); |
| 136 | else |
| 137 | error(Msg); |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 140 | // This function resolves conflicts if there's an existing symbol with |
| 141 | // the same name. Decisions are made based on symbol type. |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 142 | template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 143 | Symbol *Sym = insert(New); |
| 144 | if (Sym->Body == New) |
| 145 | return; |
| 146 | |
| 147 | SymbolBody *Existing = Sym->Body; |
| 148 | |
| 149 | if (Lazy *L = dyn_cast<Lazy>(Existing)) { |
| 150 | if (New->isUndefined()) { |
Rafael Espindola | 85a6e0f | 2015-10-06 15:18:50 +0000 | [diff] [blame] | 151 | if (New->isWeak()) { |
| 152 | // See the explanation in SymbolTable::addLazy |
| 153 | L->setUsedInRegularObj(); |
| 154 | L->setWeak(); |
| 155 | return; |
| 156 | } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 157 | addMemberFile(L); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // Found a definition for something also in an archive. Ignore the archive |
| 162 | // definition. |
| 163 | Sym->Body = New; |
| 164 | return; |
| 165 | } |
| 166 | |
Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 167 | if (New->isTLS() != Existing->isTLS()) |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 168 | reportConflict("TLS attribute mismatch for symbol", *Existing, *New, false); |
Igor Kudrin | 65bddea | 2015-10-09 09:58:39 +0000 | [diff] [blame] | 169 | |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 170 | // compare() returns -1, 0, or 1 if the lhs symbol is less preferable, |
| 171 | // equivalent (conflicting), or more preferable, respectively. |
| 172 | int comp = Existing->compare<ELFT>(New); |
| 173 | if (comp < 0) |
| 174 | Sym->Body = New; |
Rafael Espindola | 1a49e58 | 2015-09-23 14:10:24 +0000 | [diff] [blame] | 175 | else if (comp == 0) |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 176 | reportConflict("duplicate symbol", *Existing, *New, |
| 177 | Config->AllowMultipleDefinition); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 180 | template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 181 | // Find an existing Symbol or create and insert a new one. |
| 182 | StringRef Name = New->getName(); |
| 183 | Symbol *&Sym = Symtab[Name]; |
| 184 | if (!Sym) { |
| 185 | Sym = new (Alloc) Symbol(New); |
| 186 | New->setBackref(Sym); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 187 | return Sym; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 188 | } |
| 189 | New->setBackref(Sym); |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 190 | return Sym; |
| 191 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 192 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 193 | template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *New) { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 194 | Symbol *Sym = insert(New); |
| 195 | if (Sym->Body == New) |
| 196 | return; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 197 | SymbolBody *Existing = Sym->Body; |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 198 | if (Existing->isDefined() || Existing->isLazy()) |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 199 | return; |
| 200 | Sym->Body = New; |
| 201 | assert(Existing->isUndefined() && "Unexpected symbol kind."); |
Rafael Espindola | 8614c56 | 2015-10-06 14:33:58 +0000 | [diff] [blame] | 202 | |
| 203 | // Weak undefined symbols should not fetch members from archives. |
| 204 | // If we were to keep old symbol we would not know that an archive member was |
| 205 | // available if a strong undefined symbol shows up afterwards in the link. |
| 206 | // If a strong undefined symbol never shows up, this lazy symbol will |
| 207 | // get to the end of the link and must be treated as the weak undefined one. |
| 208 | // We set UsedInRegularObj in a similar way to what is done with shared |
| 209 | // symbols and mark it as weak to reduce how many special cases are needed. |
| 210 | if (Existing->isWeak()) { |
| 211 | New->setUsedInRegularObj(); |
| 212 | New->setWeak(); |
| 213 | return; |
| 214 | } |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 215 | addMemberFile(New); |
| 216 | } |
| 217 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 218 | template <class ELFT> void SymbolTable<ELFT>::addMemberFile(Lazy *Body) { |
Michael J. Spencer | 1b348a6 | 2015-09-04 22:28:10 +0000 | [diff] [blame] | 219 | std::unique_ptr<InputFile> File = Body->getMember(); |
| 220 | |
| 221 | // getMember returns nullptr if the member was already read from the library. |
| 222 | if (!File) |
| 223 | return; |
| 224 | |
| 225 | addFile(std::move(File)); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 226 | } |
Rafael Espindola | 0e604f9 | 2015-09-25 18:56:53 +0000 | [diff] [blame] | 227 | |
Rui Ueyama | 3ce825e | 2015-10-09 21:07:25 +0000 | [diff] [blame] | 228 | template class lld::elf2::SymbolTable<ELF32LE>; |
| 229 | template class lld::elf2::SymbolTable<ELF32BE>; |
| 230 | template class lld::elf2::SymbolTable<ELF64LE>; |
| 231 | template class lld::elf2::SymbolTable<ELF64BE>; |