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