blob: 4c66f02cd18420122ce7052844850ed358477bcd [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- 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//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
10// Symbol table is a bag of all known symbols. We put all symbols of
11// all input files to the symbol table. The symbol Table is basically
12// a hash table with the logic to resolve symbol name conflicts using
13// the symbol types.
14//
15//===----------------------------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +000016
17#include "SymbolTable.h"
Rafael Espindola4340aad2015-09-11 22:42:45 +000018#include "Config.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000019#include "Error.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000020#include "Symbols.h"
21
22using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000023using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000024using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
26using namespace lld;
27using namespace lld::elf2;
28
Rui Ueyama3ce825e2015-10-09 21:07:25 +000029template <class ELFT> SymbolTable<ELFT>::SymbolTable() {}
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
Rui Ueyama3ce825e2015-10-09 21:07:25 +000031template <class ELFT> bool SymbolTable<ELFT>::shouldUseRela() const {
Rafael Espindolab9ca7bb2015-10-12 11:52:31 +000032 ELFKind K = cast<ELFFileBase<ELFT>>(Config->FirstElf)->getELFKind();
Rafael Espindola67a5da62015-09-17 14:02:10 +000033 return K == ELF64LEKind || K == ELF64BEKind;
34}
35
Rui Ueyama3ce825e2015-10-09 21:07:25 +000036template <class ELFT>
37void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rui Ueyama3d451792015-10-12 18:03:21 +000038 checkCompatibility(File);
Rafael Espindola525914d2015-10-11 03:36:49 +000039
Igor Kudrin2696bbe2015-10-01 18:02:21 +000040 if (auto *AF = dyn_cast<ArchiveFile>(File.get())) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +000041 ArchiveFiles.emplace_back(std::move(File));
Igor Kudrin2696bbe2015-10-01 18:02:21 +000042 AF->parse();
Michael J. Spencer1b348a62015-09-04 22:28:10 +000043 for (Lazy &Sym : AF->getLazySymbols())
44 addLazy(&Sym);
45 return;
46 }
Rui Ueyama3d451792015-10-12 18:03:21 +000047
Rafael Espindoladfce5a22015-10-12 02:22:58 +000048 if (auto *S = dyn_cast<SharedFile<ELFT>>(File.get())) {
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000049 S->parseSoName();
50 if (!IncludedSoNames.insert(S->getSoName()).second)
51 return;
Rafael Espindola444576d2015-10-09 19:25:07 +000052 S->parse();
53 } else {
Rafael Espindoladfce5a22015-10-12 02:22:58 +000054 cast<ObjectFile<ELFT>>(File.get())->parse(Comdats);
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000055 }
Rafael Espindolaaf707642015-10-12 01:55:32 +000056 addELFFile(cast<ELFFileBase<ELFT>>(File.release()));
Michael J. Spencer84487f12015-07-24 21:03:07 +000057}
58
Rui Ueyamaff777682015-10-09 21:12:40 +000059template <class ELFT>
60SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
61 auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Required);
62 resolve(Sym);
63 return Sym;
Rafael Espindola1d6063e2015-09-22 21:24:52 +000064}
65
Rui Ueyamaff777682015-10-09 21:12:40 +000066template <class ELFT>
67SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
68 auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Optional);
69 resolve(Sym);
70 return Sym;
Denis Protivensky22220d52015-10-05 09:43:57 +000071}
72
Rafael Espindola0e604f92015-09-25 18:56:53 +000073template <class ELFT>
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000074void SymbolTable<ELFT>::addAbsoluteSym(StringRef Name,
75 typename ELFFile<ELFT>::Elf_Sym &ESym) {
76 resolve(new (Alloc) DefinedAbsolute<ELFT>(Name, ESym));
77}
78
79template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000080void SymbolTable<ELFT>::addSyntheticSym(StringRef Name,
81 OutputSectionBase<ELFT> &Section,
82 typename ELFFile<ELFT>::uintX_t Value) {
Rafael Espindola0e604f92015-09-25 18:56:53 +000083 typedef typename DefinedSynthetic<ELFT>::Elf_Sym Elf_Sym;
84 auto ESym = new (Alloc) Elf_Sym;
85 memset(ESym, 0, sizeof(Elf_Sym));
86 ESym->st_value = Value;
87 auto Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, *ESym, Section);
Rui Ueyama3ce825e2015-10-09 21:07:25 +000088 resolve(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +000089}
90
Simon Atanasyan09dae7c2015-12-16 14:45:09 +000091template <class ELFT>
92SymbolBody *SymbolTable<ELFT>::addIgnoredSym(StringRef Name) {
Rafael Espindola5d413262015-10-01 21:22:26 +000093 auto Sym = new (Alloc)
94 DefinedAbsolute<ELFT>(Name, DefinedAbsolute<ELFT>::IgnoreUndef);
Rui Ueyama3ce825e2015-10-09 21:07:25 +000095 resolve(Sym);
Simon Atanasyan09dae7c2015-12-16 14:45:09 +000096 return Sym;
Rafael Espindola5d413262015-10-01 21:22:26 +000097}
98
Rui Ueyamad9189ce2015-10-15 17:11:03 +000099template <class ELFT> bool SymbolTable<ELFT>::isUndefined(StringRef Name) {
100 if (SymbolBody *Sym = find(Name))
101 return Sym->isUndefined();
102 return false;
103}
104
Rafael Espindolaaf707642015-10-12 01:55:32 +0000105template <class ELFT>
106void SymbolTable<ELFT>::addELFFile(ELFFileBase<ELFT> *File) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000107 if (auto *O = dyn_cast<ObjectFile<ELFT>>(File))
Rafael Espindolab90582db2015-10-06 15:03:52 +0000108 ObjectFiles.emplace_back(O);
109 else if (auto *S = dyn_cast<SharedFile<ELFT>>(File))
110 SharedFiles.emplace_back(S);
111
Rafael Espindoladfce5a22015-10-12 02:22:58 +0000112 if (auto *O = dyn_cast<ObjectFile<ELFT>>(File)) {
Rafael Espindola824d1a92015-09-04 00:09:43 +0000113 for (SymbolBody *Body : O->getSymbols())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000114 resolve(Body);
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000115 }
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000116
Rafael Espindola18173d42015-09-08 15:50:05 +0000117 if (auto *S = dyn_cast<SharedFile<ELFT>>(File)) {
Rafael Espindola18173d42015-09-08 15:50:05 +0000118 for (SharedSymbol<ELFT> &Body : S->getSharedSymbols())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000119 resolve(&Body);
Rafael Espindola824d1a92015-09-04 00:09:43 +0000120 }
121}
122
Rafael Espindola1a49e582015-09-23 14:10:24 +0000123template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000124void SymbolTable<ELFT>::reportConflict(const Twine &Message,
125 const SymbolBody &Old,
126 const SymbolBody &New, bool Warning) {
Rafael Espindola1a49e582015-09-23 14:10:24 +0000127 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
128 typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
129
130 const Elf_Sym &OldE = cast<ELFSymbolBody<ELFT>>(Old).Sym;
131 const Elf_Sym &NewE = cast<ELFSymbolBody<ELFT>>(New).Sym;
Rafael Espindolaaf707642015-10-12 01:55:32 +0000132 ELFFileBase<ELFT> *OldFile = nullptr;
133 ELFFileBase<ELFT> *NewFile = nullptr;
Rafael Espindola1a49e582015-09-23 14:10:24 +0000134
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000135 for (const std::unique_ptr<ObjectFile<ELFT>> &File : ObjectFiles) {
136 Elf_Sym_Range Syms = File->getObj().symbols(File->getSymbolTable());
Rafael Espindola1a49e582015-09-23 14:10:24 +0000137 if (&OldE > Syms.begin() && &OldE < Syms.end())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000138 OldFile = File.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000139 if (&NewE > Syms.begin() && &NewE < Syms.end())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000140 NewFile = File.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000141 }
142
Igor Kudrin65bddea2015-10-09 09:58:39 +0000143 std::string Msg = (Message + ": " + Old.getName() + " in " +
Igor Kudrin4bc5ad4c2015-11-19 19:08:45 +0000144 (OldFile ? OldFile->getName() : "(internal)") + " and " +
145 (NewFile ? NewFile->getName() : "(internal)"))
Rafael Espindola4b2ca852015-09-28 20:30:11 +0000146 .str();
Igor Kudrin65bddea2015-10-09 09:58:39 +0000147 if (Warning)
Rafael Espindola4b2ca852015-09-28 20:30:11 +0000148 warning(Msg);
149 else
150 error(Msg);
Rafael Espindola1a49e582015-09-23 14:10:24 +0000151}
152
Michael J. Spencer84487f12015-07-24 21:03:07 +0000153// This function resolves conflicts if there's an existing symbol with
154// the same name. Decisions are made based on symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000155template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000156 Symbol *Sym = insert(New);
157 if (Sym->Body == New)
158 return;
159
160 SymbolBody *Existing = Sym->Body;
161
162 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
163 if (New->isUndefined()) {
Rafael Espindola85a6e0f2015-10-06 15:18:50 +0000164 if (New->isWeak()) {
165 // See the explanation in SymbolTable::addLazy
166 L->setUsedInRegularObj();
167 L->setWeak();
168 return;
169 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000170 addMemberFile(L);
171 return;
172 }
173
174 // Found a definition for something also in an archive. Ignore the archive
175 // definition.
176 Sym->Body = New;
177 return;
178 }
179
Igor Kudrin65bddea2015-10-09 09:58:39 +0000180 if (New->isTLS() != Existing->isTLS())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000181 reportConflict("TLS attribute mismatch for symbol", *Existing, *New, false);
Igor Kudrin65bddea2015-10-09 09:58:39 +0000182
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000183 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
184 // equivalent (conflicting), or more preferable, respectively.
185 int comp = Existing->compare<ELFT>(New);
186 if (comp < 0)
187 Sym->Body = New;
Rafael Espindola1a49e582015-09-23 14:10:24 +0000188 else if (comp == 0)
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000189 reportConflict("duplicate symbol", *Existing, *New,
190 Config->AllowMultipleDefinition);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000191}
192
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000193template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000194 // Find an existing Symbol or create and insert a new one.
195 StringRef Name = New->getName();
196 Symbol *&Sym = Symtab[Name];
197 if (!Sym) {
198 Sym = new (Alloc) Symbol(New);
199 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000200 return Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000201 }
202 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000203 return Sym;
204}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000205
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000206template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
207 auto It = Symtab.find(Name);
208 if (It == Symtab.end())
209 return nullptr;
210 return It->second->Body;
211}
212
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000213template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000214 Symbol *Sym = insert(New);
215 if (Sym->Body == New)
216 return;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000217 SymbolBody *Existing = Sym->Body;
Rafael Espindola8614c562015-10-06 14:33:58 +0000218 if (Existing->isDefined() || Existing->isLazy())
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000219 return;
220 Sym->Body = New;
221 assert(Existing->isUndefined() && "Unexpected symbol kind.");
Rafael Espindola8614c562015-10-06 14:33:58 +0000222
223 // Weak undefined symbols should not fetch members from archives.
224 // If we were to keep old symbol we would not know that an archive member was
225 // available if a strong undefined symbol shows up afterwards in the link.
226 // If a strong undefined symbol never shows up, this lazy symbol will
227 // get to the end of the link and must be treated as the weak undefined one.
228 // We set UsedInRegularObj in a similar way to what is done with shared
229 // symbols and mark it as weak to reduce how many special cases are needed.
230 if (Existing->isWeak()) {
231 New->setUsedInRegularObj();
232 New->setWeak();
233 return;
234 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000235 addMemberFile(New);
236}
237
Rui Ueyama3d451792015-10-12 18:03:21 +0000238template <class ELFT>
239void SymbolTable<ELFT>::checkCompatibility(std::unique_ptr<InputFile> &File) {
240 auto *E = dyn_cast<ELFFileBase<ELFT>>(File.get());
241 if (!E)
242 return;
Rui Ueyamae717a712015-10-13 16:20:50 +0000243 if (E->getELFKind() == Config->EKind && E->getEMachine() == Config->EMachine)
Rui Ueyama3d451792015-10-12 18:03:21 +0000244 return;
245 StringRef A = E->getName();
246 StringRef B = Config->Emulation;
247 if (B.empty())
248 B = Config->FirstElf->getName();
249 error(A + " is incompatible with " + B);
250}
251
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000252template <class ELFT> void SymbolTable<ELFT>::addMemberFile(Lazy *Body) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000253 // getMember returns nullptr if the member was already read from the library.
Rui Ueyama690db672015-10-14 22:32:10 +0000254 if (std::unique_ptr<InputFile> File = Body->getMember())
255 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000256}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000257
Rui Ueyama93bfee52015-10-13 18:10:33 +0000258// This function takes care of the case in which shared libraries depend on
259// the user program (not the other way, which is usual). Shared libraries
260// may have undefined symbols, expecting that the user program provides
261// the definitions for them. An example is BSD's __progname symbol.
262// We need to put such symbols to the main program's .dynsym so that
263// shared libraries can find them.
264// Except this, we ignore undefined symbols in DSOs.
265template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000266 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
267 for (StringRef U : File->getUndefinedSymbols())
268 if (SymbolBody *Sym = find(U))
269 if (Sym->isDefined())
270 Sym->setUsedInDynamicReloc();
271}
272
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000273template class lld::elf2::SymbolTable<ELF32LE>;
274template class lld::elf2::SymbolTable<ELF32BE>;
275template class lld::elf2::SymbolTable<ELF64LE>;
276template class lld::elf2::SymbolTable<ELF64BE>;