blob: d751e8b09168d04566d83ba312286491768409b5 [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>
Rui Ueyama25b44c92015-12-16 23:31:22 +000032static void checkCompatibility(InputFile *FileP) {
33 auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP);
34 if (!F)
35 return;
36 if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine)
37 return;
38 StringRef A = F->getName();
39 StringRef B = Config->Emulation;
40 if (B.empty())
41 B = Config->FirstElf->getName();
42 error(A + " is incompatible with " + B);
43}
44
45template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000046void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rui Ueyama89575742015-12-16 22:59:13 +000047 InputFile *FileP = File.release();
Rui Ueyama25b44c92015-12-16 23:31:22 +000048 checkCompatibility<ELFT>(FileP);
Rafael Espindola525914d2015-10-11 03:36:49 +000049
Rui Ueyama89575742015-12-16 22:59:13 +000050 // .a file
51 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
52 ArchiveFiles.emplace_back(F);
53 F->parse();
54 for (Lazy &Sym : F->getLazySymbols())
Michael J. Spencer1b348a62015-09-04 22:28:10 +000055 addLazy(&Sym);
56 return;
57 }
Rui Ueyama3d451792015-10-12 18:03:21 +000058
Rui Ueyama89575742015-12-16 22:59:13 +000059 // .so file
60 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
61 // DSOs are uniquified not by filename but by soname.
62 F->parseSoName();
63 if (!IncludedSoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000064 return;
Rui Ueyama89575742015-12-16 22:59:13 +000065
66 SharedFiles.emplace_back(F);
67 F->parse();
68 for (SharedSymbol<ELFT> &B : F->getSharedSymbols())
69 resolve(&B);
70 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000071 }
Rui Ueyama89575742015-12-16 22:59:13 +000072
73 // .o file
74 auto *F = cast<ObjectFile<ELFT>>(FileP);
75 ObjectFiles.emplace_back(F);
76 F->parse(Comdats);
77 for (SymbolBody *B : F->getSymbols())
78 resolve(B);
Michael J. Spencer84487f12015-07-24 21:03:07 +000079}
80
Rui Ueyamaff777682015-10-09 21:12:40 +000081template <class ELFT>
82SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
83 auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Required);
84 resolve(Sym);
85 return Sym;
Rafael Espindola1d6063e2015-09-22 21:24:52 +000086}
87
Rui Ueyamaff777682015-10-09 21:12:40 +000088template <class ELFT>
89SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
90 auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Optional);
91 resolve(Sym);
92 return Sym;
Denis Protivensky22220d52015-10-05 09:43:57 +000093}
94
Rafael Espindola0e604f92015-09-25 18:56:53 +000095template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +000096void SymbolTable<ELFT>::addAbsolute(StringRef Name,
97 typename ELFFile<ELFT>::Elf_Sym &ESym) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000098 resolve(new (Alloc) DefinedAbsolute<ELFT>(Name, ESym));
99}
100
101template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000102void SymbolTable<ELFT>::addSynthetic(StringRef Name,
103 OutputSectionBase<ELFT> &Section,
104 typename ELFFile<ELFT>::uintX_t Value) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000105 typedef typename DefinedSynthetic<ELFT>::Elf_Sym Elf_Sym;
Rui Ueyamaa71f3a72015-12-16 22:36:10 +0000106 auto *ESym = new (Alloc) Elf_Sym;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000107 memset(ESym, 0, sizeof(Elf_Sym));
108 ESym->st_value = Value;
Rui Ueyamaa71f3a72015-12-16 22:36:10 +0000109 auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, *ESym, Section);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000110 resolve(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000111}
112
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000113template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000114SymbolBody *SymbolTable<ELFT>::addIgnored(StringRef Name) {
Rui Ueyamaa71f3a72015-12-16 22:36:10 +0000115 auto *Sym = new (Alloc)
Rafael Espindola5d413262015-10-01 21:22:26 +0000116 DefinedAbsolute<ELFT>(Name, DefinedAbsolute<ELFT>::IgnoreUndef);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000117 resolve(Sym);
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000118 return Sym;
Rafael Espindola5d413262015-10-01 21:22:26 +0000119}
120
Rui Ueyamad9189ce2015-10-15 17:11:03 +0000121template <class ELFT> bool SymbolTable<ELFT>::isUndefined(StringRef Name) {
122 if (SymbolBody *Sym = find(Name))
123 return Sym->isUndefined();
124 return false;
125}
126
Rui Ueyama533336a2015-12-16 22:26:48 +0000127// Returns a file from which symbol B was created.
128// If B does not belong to any file in ObjectFiles, returns a nullptr.
Rafael Espindola1a49e582015-09-23 14:10:24 +0000129template <class ELFT>
Rui Ueyama533336a2015-12-16 22:26:48 +0000130static ELFFileBase<ELFT> *
131findFile(std::vector<std::unique_ptr<ObjectFile<ELFT>>> &ObjectFiles,
132 SymbolBody *B) {
Rafael Espindola1a49e582015-09-23 14:10:24 +0000133 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
134 typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
135
Rui Ueyama533336a2015-12-16 22:26:48 +0000136 const Elf_Sym *Sym = &cast<ELFSymbolBody<ELFT>>(*B).Sym;
137 for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
138 Elf_Sym_Range R = F->getObj().symbols(F->getSymbolTable());
139 if (R.begin() <= Sym && Sym < R.end())
140 return F.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000141 }
Rui Ueyama533336a2015-12-16 22:26:48 +0000142 return nullptr;
143}
144
145template <class ELFT>
146std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
147 ELFFileBase<ELFT> *OldFile = findFile(ObjectFiles, Old);
148 ELFFileBase<ELFT> *NewFile = findFile(ObjectFiles, New);
Rafael Espindola1a49e582015-09-23 14:10:24 +0000149
Rui Ueyamaf0904012015-12-16 22:26:45 +0000150 StringRef Sym = Old->getName();
151 StringRef F1 = OldFile ? OldFile->getName() : "(internal)";
152 StringRef F2 = NewFile ? NewFile->getName() : "(internal)";
153 return (Sym + " in " + F1 + " and " + F2).str();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000154}
155
Michael J. Spencer84487f12015-07-24 21:03:07 +0000156// This function resolves conflicts if there's an existing symbol with
157// the same name. Decisions are made based on symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000158template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000159 Symbol *Sym = insert(New);
160 if (Sym->Body == New)
161 return;
162
163 SymbolBody *Existing = Sym->Body;
164
165 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000166 if (auto *Undef = dyn_cast<Undefined<ELFT>>(New)) {
167 addMemberFile(Undef, L);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000168 return;
169 }
Rui Ueyamac5b95122015-12-16 23:23:14 +0000170 // Found a definition for something also in an archive.
171 // Ignore the archive definition.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000172 Sym->Body = New;
173 return;
174 }
175
Rui Ueyama62d0e322015-12-17 00:04:18 +0000176 if (New->isTls() != Existing->isTls())
Rui Ueyamaf0904012015-12-16 22:26:45 +0000177 error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New));
Igor Kudrin65bddea2015-10-09 09:58:39 +0000178
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000179 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
180 // equivalent (conflicting), or more preferable, respectively.
181 int comp = Existing->compare<ELFT>(New);
Rui Ueyamaf0904012015-12-16 22:26:45 +0000182 if (comp == 0) {
183 std::string S = "duplicate symbol: " + conflictMsg(Existing, New);
184 if (!Config->AllowMultipleDefinition)
185 error(S);
186 warning(S);
187 return;
188 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000189 if (comp < 0)
190 Sym->Body = New;
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];
Rui Ueyama38dcc9e2015-12-16 23:25:31 +0000197 if (!Sym)
Rui Ueyama3554f592015-12-17 00:01:25 +0000198 Sym = new (Alloc) Symbol{New};
Michael J. Spencer84487f12015-07-24 21:03:07 +0000199 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000200 return Sym;
201}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000202
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000203template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
204 auto It = Symtab.find(Name);
205 if (It == Symtab.end())
206 return nullptr;
207 return It->second->Body;
208}
209
Rui Ueyamac5b95122015-12-16 23:23:14 +0000210template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) {
211 Symbol *Sym = insert(L);
212 if (Sym->Body == L)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000213 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000214 if (auto *Undef = dyn_cast<Undefined<ELFT>>(Sym->Body)) {
215 Sym->Body = L;
216 addMemberFile(Undef, L);
Rafael Espindola8614c562015-10-06 14:33:58 +0000217 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000218}
219
Rui Ueyama3d451792015-10-12 18:03:21 +0000220template <class ELFT>
Rui Ueyamac5b95122015-12-16 23:23:14 +0000221void SymbolTable<ELFT>::addMemberFile(Undefined<ELFT> *Undef, Lazy *L) {
222 // Weak undefined symbols should not fetch members from archives.
223 // If we were to keep old symbol we would not know that an archive member was
224 // available if a strong undefined symbol shows up afterwards in the link.
225 // If a strong undefined symbol never shows up, this lazy symbol will
226 // get to the end of the link and must be treated as the weak undefined one.
227 // We set UsedInRegularObj in a similar way to what is done with shared
228 // symbols and mark it as weak to reduce how many special cases are needed.
229 if (Undef->isWeak()) {
230 L->setUsedInRegularObj();
231 L->setWeak();
232 return;
233 }
234
235 // Fetch a member file that has the definition for L.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000236 // getMember returns nullptr if the member was already read from the library.
Rui Ueyamac5b95122015-12-16 23:23:14 +0000237 if (std::unique_ptr<InputFile> File = L->getMember())
Rui Ueyama690db672015-10-14 22:32:10 +0000238 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000239}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000240
Rui Ueyama93bfee52015-10-13 18:10:33 +0000241// This function takes care of the case in which shared libraries depend on
242// the user program (not the other way, which is usual). Shared libraries
243// may have undefined symbols, expecting that the user program provides
244// the definitions for them. An example is BSD's __progname symbol.
245// We need to put such symbols to the main program's .dynsym so that
246// shared libraries can find them.
247// Except this, we ignore undefined symbols in DSOs.
248template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000249 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
250 for (StringRef U : File->getUndefinedSymbols())
251 if (SymbolBody *Sym = find(U))
252 if (Sym->isDefined())
253 Sym->setUsedInDynamicReloc();
254}
255
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000256template class lld::elf2::SymbolTable<ELF32LE>;
257template class lld::elf2::SymbolTable<ELF32BE>;
258template class lld::elf2::SymbolTable<ELF64LE>;
259template class lld::elf2::SymbolTable<ELF64BE>;