blob: 61826cbf5250a744bab8fc42acb8a335554fa028 [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);
Rui Ueyama89575742015-12-16 22:59:13 +000039 InputFile *FileP = File.release();
Rafael Espindola525914d2015-10-11 03:36:49 +000040
Rui Ueyama89575742015-12-16 22:59:13 +000041 // .a file
42 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
43 ArchiveFiles.emplace_back(F);
44 F->parse();
45 for (Lazy &Sym : F->getLazySymbols())
Michael J. Spencer1b348a62015-09-04 22:28:10 +000046 addLazy(&Sym);
47 return;
48 }
Rui Ueyama3d451792015-10-12 18:03:21 +000049
Rui Ueyama89575742015-12-16 22:59:13 +000050 // .so file
51 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
52 // DSOs are uniquified not by filename but by soname.
53 F->parseSoName();
54 if (!IncludedSoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000055 return;
Rui Ueyama89575742015-12-16 22:59:13 +000056
57 SharedFiles.emplace_back(F);
58 F->parse();
59 for (SharedSymbol<ELFT> &B : F->getSharedSymbols())
60 resolve(&B);
61 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000062 }
Rui Ueyama89575742015-12-16 22:59:13 +000063
64 // .o file
65 auto *F = cast<ObjectFile<ELFT>>(FileP);
66 ObjectFiles.emplace_back(F);
67 F->parse(Comdats);
68 for (SymbolBody *B : F->getSymbols())
69 resolve(B);
Michael J. Spencer84487f12015-07-24 21:03:07 +000070}
71
Rui Ueyamaff777682015-10-09 21:12:40 +000072template <class ELFT>
73SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
74 auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Required);
75 resolve(Sym);
76 return Sym;
Rafael Espindola1d6063e2015-09-22 21:24:52 +000077}
78
Rui Ueyamaff777682015-10-09 21:12:40 +000079template <class ELFT>
80SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
81 auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Optional);
82 resolve(Sym);
83 return Sym;
Denis Protivensky22220d52015-10-05 09:43:57 +000084}
85
Rafael Espindola0e604f92015-09-25 18:56:53 +000086template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +000087void SymbolTable<ELFT>::addAbsolute(StringRef Name,
88 typename ELFFile<ELFT>::Elf_Sym &ESym) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000089 resolve(new (Alloc) DefinedAbsolute<ELFT>(Name, ESym));
90}
91
92template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +000093void SymbolTable<ELFT>::addSynthetic(StringRef Name,
94 OutputSectionBase<ELFT> &Section,
95 typename ELFFile<ELFT>::uintX_t Value) {
Rafael Espindola0e604f92015-09-25 18:56:53 +000096 typedef typename DefinedSynthetic<ELFT>::Elf_Sym Elf_Sym;
Rui Ueyamaa71f3a72015-12-16 22:36:10 +000097 auto *ESym = new (Alloc) Elf_Sym;
Rafael Espindola0e604f92015-09-25 18:56:53 +000098 memset(ESym, 0, sizeof(Elf_Sym));
99 ESym->st_value = Value;
Rui Ueyamaa71f3a72015-12-16 22:36:10 +0000100 auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, *ESym, Section);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000101 resolve(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000102}
103
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000104template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000105SymbolBody *SymbolTable<ELFT>::addIgnored(StringRef Name) {
Rui Ueyamaa71f3a72015-12-16 22:36:10 +0000106 auto *Sym = new (Alloc)
Rafael Espindola5d413262015-10-01 21:22:26 +0000107 DefinedAbsolute<ELFT>(Name, DefinedAbsolute<ELFT>::IgnoreUndef);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000108 resolve(Sym);
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000109 return Sym;
Rafael Espindola5d413262015-10-01 21:22:26 +0000110}
111
Rui Ueyamad9189ce2015-10-15 17:11:03 +0000112template <class ELFT> bool SymbolTable<ELFT>::isUndefined(StringRef Name) {
113 if (SymbolBody *Sym = find(Name))
114 return Sym->isUndefined();
115 return false;
116}
117
Rui Ueyama533336a2015-12-16 22:26:48 +0000118// Returns a file from which symbol B was created.
119// If B does not belong to any file in ObjectFiles, returns a nullptr.
Rafael Espindola1a49e582015-09-23 14:10:24 +0000120template <class ELFT>
Rui Ueyama533336a2015-12-16 22:26:48 +0000121static ELFFileBase<ELFT> *
122findFile(std::vector<std::unique_ptr<ObjectFile<ELFT>>> &ObjectFiles,
123 SymbolBody *B) {
Rafael Espindola1a49e582015-09-23 14:10:24 +0000124 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
125 typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
126
Rui Ueyama533336a2015-12-16 22:26:48 +0000127 const Elf_Sym *Sym = &cast<ELFSymbolBody<ELFT>>(*B).Sym;
128 for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
129 Elf_Sym_Range R = F->getObj().symbols(F->getSymbolTable());
130 if (R.begin() <= Sym && Sym < R.end())
131 return F.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000132 }
Rui Ueyama533336a2015-12-16 22:26:48 +0000133 return nullptr;
134}
135
136template <class ELFT>
137std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
138 ELFFileBase<ELFT> *OldFile = findFile(ObjectFiles, Old);
139 ELFFileBase<ELFT> *NewFile = findFile(ObjectFiles, New);
Rafael Espindola1a49e582015-09-23 14:10:24 +0000140
Rui Ueyamaf0904012015-12-16 22:26:45 +0000141 StringRef Sym = Old->getName();
142 StringRef F1 = OldFile ? OldFile->getName() : "(internal)";
143 StringRef F2 = NewFile ? NewFile->getName() : "(internal)";
144 return (Sym + " in " + F1 + " and " + F2).str();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000145}
146
Michael J. Spencer84487f12015-07-24 21:03:07 +0000147// This function resolves conflicts if there's an existing symbol with
148// the same name. Decisions are made based on symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000149template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000150 Symbol *Sym = insert(New);
151 if (Sym->Body == New)
152 return;
153
154 SymbolBody *Existing = Sym->Body;
155
156 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000157 if (auto *Undef = dyn_cast<Undefined<ELFT>>(New)) {
158 addMemberFile(Undef, L);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000159 return;
160 }
Rui Ueyamac5b95122015-12-16 23:23:14 +0000161 // Found a definition for something also in an archive.
162 // Ignore the archive definition.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000163 Sym->Body = New;
164 return;
165 }
166
Igor Kudrin65bddea2015-10-09 09:58:39 +0000167 if (New->isTLS() != Existing->isTLS())
Rui Ueyamaf0904012015-12-16 22:26:45 +0000168 error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New));
Igor Kudrin65bddea2015-10-09 09:58:39 +0000169
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000170 // 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);
Rui Ueyamaf0904012015-12-16 22:26:45 +0000173 if (comp == 0) {
174 std::string S = "duplicate symbol: " + conflictMsg(Existing, New);
175 if (!Config->AllowMultipleDefinition)
176 error(S);
177 warning(S);
178 return;
179 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000180 if (comp < 0)
181 Sym->Body = New;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000182}
183
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000184template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000185 // Find an existing Symbol or create and insert a new one.
186 StringRef Name = New->getName();
187 Symbol *&Sym = Symtab[Name];
Rui Ueyama38dcc9e2015-12-16 23:25:31 +0000188 if (!Sym)
Michael J. Spencer84487f12015-07-24 21:03:07 +0000189 Sym = new (Alloc) Symbol(New);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000190 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000191 return Sym;
192}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000193
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000194template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
195 auto It = Symtab.find(Name);
196 if (It == Symtab.end())
197 return nullptr;
198 return It->second->Body;
199}
200
Rui Ueyamac5b95122015-12-16 23:23:14 +0000201template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) {
202 Symbol *Sym = insert(L);
203 if (Sym->Body == L)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000204 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000205 if (auto *Undef = dyn_cast<Undefined<ELFT>>(Sym->Body)) {
206 Sym->Body = L;
207 addMemberFile(Undef, L);
Rafael Espindola8614c562015-10-06 14:33:58 +0000208 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000209}
210
Rui Ueyama3d451792015-10-12 18:03:21 +0000211template <class ELFT>
212void SymbolTable<ELFT>::checkCompatibility(std::unique_ptr<InputFile> &File) {
213 auto *E = dyn_cast<ELFFileBase<ELFT>>(File.get());
214 if (!E)
215 return;
Rui Ueyamae717a712015-10-13 16:20:50 +0000216 if (E->getELFKind() == Config->EKind && E->getEMachine() == Config->EMachine)
Rui Ueyama3d451792015-10-12 18:03:21 +0000217 return;
218 StringRef A = E->getName();
219 StringRef B = Config->Emulation;
220 if (B.empty())
221 B = Config->FirstElf->getName();
222 error(A + " is incompatible with " + B);
223}
224
Rui Ueyamac5b95122015-12-16 23:23:14 +0000225template <class ELFT>
226void SymbolTable<ELFT>::addMemberFile(Undefined<ELFT> *Undef, Lazy *L) {
227 // Weak undefined symbols should not fetch members from archives.
228 // If we were to keep old symbol we would not know that an archive member was
229 // available if a strong undefined symbol shows up afterwards in the link.
230 // If a strong undefined symbol never shows up, this lazy symbol will
231 // get to the end of the link and must be treated as the weak undefined one.
232 // We set UsedInRegularObj in a similar way to what is done with shared
233 // symbols and mark it as weak to reduce how many special cases are needed.
234 if (Undef->isWeak()) {
235 L->setUsedInRegularObj();
236 L->setWeak();
237 return;
238 }
239
240 // Fetch a member file that has the definition for L.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000241 // getMember returns nullptr if the member was already read from the library.
Rui Ueyamac5b95122015-12-16 23:23:14 +0000242 if (std::unique_ptr<InputFile> File = L->getMember())
Rui Ueyama690db672015-10-14 22:32:10 +0000243 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000244}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000245
Rui Ueyama93bfee52015-10-13 18:10:33 +0000246// This function takes care of the case in which shared libraries depend on
247// the user program (not the other way, which is usual). Shared libraries
248// may have undefined symbols, expecting that the user program provides
249// the definitions for them. An example is BSD's __progname symbol.
250// We need to put such symbols to the main program's .dynsym so that
251// shared libraries can find them.
252// Except this, we ignore undefined symbols in DSOs.
253template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000254 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
255 for (StringRef U : File->getUndefinedSymbols())
256 if (SymbolBody *Sym = find(U))
257 if (Sym->isDefined())
258 Sym->setUsedInDynamicReloc();
259}
260
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000261template class lld::elf2::SymbolTable<ELF32LE>;
262template class lld::elf2::SymbolTable<ELF32BE>;
263template class lld::elf2::SymbolTable<ELF64LE>;
264template class lld::elf2::SymbolTable<ELF64BE>;