blob: ebb13af0577a1796032d93469d20f4fd23c79cd5 [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>
Rui Ueyama25b44c92015-12-16 23:31:22 +000037static void checkCompatibility(InputFile *FileP) {
38 auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP);
39 if (!F)
40 return;
41 if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine)
42 return;
43 StringRef A = F->getName();
44 StringRef B = Config->Emulation;
45 if (B.empty())
46 B = Config->FirstElf->getName();
47 error(A + " is incompatible with " + B);
48}
49
50template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000051void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rui Ueyama89575742015-12-16 22:59:13 +000052 InputFile *FileP = File.release();
Rui Ueyama25b44c92015-12-16 23:31:22 +000053 checkCompatibility<ELFT>(FileP);
Rafael Espindola525914d2015-10-11 03:36:49 +000054
Rui Ueyama89575742015-12-16 22:59:13 +000055 // .a file
56 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
57 ArchiveFiles.emplace_back(F);
58 F->parse();
59 for (Lazy &Sym : F->getLazySymbols())
Michael J. Spencer1b348a62015-09-04 22:28:10 +000060 addLazy(&Sym);
61 return;
62 }
Rui Ueyama3d451792015-10-12 18:03:21 +000063
Rui Ueyama89575742015-12-16 22:59:13 +000064 // .so file
65 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
66 // DSOs are uniquified not by filename but by soname.
67 F->parseSoName();
68 if (!IncludedSoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000069 return;
Rui Ueyama89575742015-12-16 22:59:13 +000070
71 SharedFiles.emplace_back(F);
72 F->parse();
73 for (SharedSymbol<ELFT> &B : F->getSharedSymbols())
74 resolve(&B);
75 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000076 }
Rui Ueyama89575742015-12-16 22:59:13 +000077
78 // .o file
79 auto *F = cast<ObjectFile<ELFT>>(FileP);
80 ObjectFiles.emplace_back(F);
81 F->parse(Comdats);
82 for (SymbolBody *B : F->getSymbols())
83 resolve(B);
Michael J. Spencer84487f12015-07-24 21:03:07 +000084}
85
Rui Ueyamaff777682015-10-09 21:12:40 +000086template <class ELFT>
87SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
88 auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Required);
89 resolve(Sym);
90 return Sym;
Rafael Espindola1d6063e2015-09-22 21:24:52 +000091}
92
Rui Ueyamaff777682015-10-09 21:12:40 +000093template <class ELFT>
94SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
95 auto *Sym = new (Alloc) Undefined<ELFT>(Name, Undefined<ELFT>::Optional);
96 resolve(Sym);
97 return Sym;
Denis Protivensky22220d52015-10-05 09:43:57 +000098}
99
Rafael Espindola0e604f92015-09-25 18:56:53 +0000100template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000101void SymbolTable<ELFT>::addAbsolute(StringRef Name,
102 typename ELFFile<ELFT>::Elf_Sym &ESym) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000103 resolve(new (Alloc) DefinedAbsolute<ELFT>(Name, ESym));
104}
105
106template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000107void SymbolTable<ELFT>::addSynthetic(StringRef Name,
108 OutputSectionBase<ELFT> &Section,
109 typename ELFFile<ELFT>::uintX_t Value) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000110 typedef typename DefinedSynthetic<ELFT>::Elf_Sym Elf_Sym;
Rui Ueyamaa71f3a72015-12-16 22:36:10 +0000111 auto *ESym = new (Alloc) Elf_Sym;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000112 memset(ESym, 0, sizeof(Elf_Sym));
113 ESym->st_value = Value;
Rui Ueyamaa71f3a72015-12-16 22:36:10 +0000114 auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, *ESym, Section);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000115 resolve(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +0000116}
117
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000118template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000119SymbolBody *SymbolTable<ELFT>::addIgnored(StringRef Name) {
Rui Ueyamaa71f3a72015-12-16 22:36:10 +0000120 auto *Sym = new (Alloc)
Rafael Espindola5d413262015-10-01 21:22:26 +0000121 DefinedAbsolute<ELFT>(Name, DefinedAbsolute<ELFT>::IgnoreUndef);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000122 resolve(Sym);
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000123 return Sym;
Rafael Espindola5d413262015-10-01 21:22:26 +0000124}
125
Rui Ueyamad9189ce2015-10-15 17:11:03 +0000126template <class ELFT> bool SymbolTable<ELFT>::isUndefined(StringRef Name) {
127 if (SymbolBody *Sym = find(Name))
128 return Sym->isUndefined();
129 return false;
130}
131
Rui Ueyama533336a2015-12-16 22:26:48 +0000132// Returns a file from which symbol B was created.
133// If B does not belong to any file in ObjectFiles, returns a nullptr.
Rafael Espindola1a49e582015-09-23 14:10:24 +0000134template <class ELFT>
Rui Ueyama533336a2015-12-16 22:26:48 +0000135static ELFFileBase<ELFT> *
136findFile(std::vector<std::unique_ptr<ObjectFile<ELFT>>> &ObjectFiles,
137 SymbolBody *B) {
Rafael Espindola1a49e582015-09-23 14:10:24 +0000138 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
139 typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
140
Rui Ueyama533336a2015-12-16 22:26:48 +0000141 const Elf_Sym *Sym = &cast<ELFSymbolBody<ELFT>>(*B).Sym;
142 for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
143 Elf_Sym_Range R = F->getObj().symbols(F->getSymbolTable());
144 if (R.begin() <= Sym && Sym < R.end())
145 return F.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000146 }
Rui Ueyama533336a2015-12-16 22:26:48 +0000147 return nullptr;
148}
149
150template <class ELFT>
151std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
152 ELFFileBase<ELFT> *OldFile = findFile(ObjectFiles, Old);
153 ELFFileBase<ELFT> *NewFile = findFile(ObjectFiles, New);
Rafael Espindola1a49e582015-09-23 14:10:24 +0000154
Rui Ueyamaf0904012015-12-16 22:26:45 +0000155 StringRef Sym = Old->getName();
156 StringRef F1 = OldFile ? OldFile->getName() : "(internal)";
157 StringRef F2 = NewFile ? NewFile->getName() : "(internal)";
158 return (Sym + " in " + F1 + " and " + F2).str();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000159}
160
Michael J. Spencer84487f12015-07-24 21:03:07 +0000161// This function resolves conflicts if there's an existing symbol with
162// the same name. Decisions are made based on symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000163template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000164 Symbol *Sym = insert(New);
165 if (Sym->Body == New)
166 return;
167
168 SymbolBody *Existing = Sym->Body;
169
170 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000171 if (auto *Undef = dyn_cast<Undefined<ELFT>>(New)) {
172 addMemberFile(Undef, L);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000173 return;
174 }
Rui Ueyamac5b95122015-12-16 23:23:14 +0000175 // Found a definition for something also in an archive.
176 // Ignore the archive definition.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000177 Sym->Body = New;
178 return;
179 }
180
Igor Kudrin65bddea2015-10-09 09:58:39 +0000181 if (New->isTLS() != Existing->isTLS())
Rui Ueyamaf0904012015-12-16 22:26:45 +0000182 error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New));
Igor Kudrin65bddea2015-10-09 09:58:39 +0000183
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000184 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
185 // equivalent (conflicting), or more preferable, respectively.
186 int comp = Existing->compare<ELFT>(New);
Rui Ueyamaf0904012015-12-16 22:26:45 +0000187 if (comp == 0) {
188 std::string S = "duplicate symbol: " + conflictMsg(Existing, New);
189 if (!Config->AllowMultipleDefinition)
190 error(S);
191 warning(S);
192 return;
193 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000194 if (comp < 0)
195 Sym->Body = New;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000196}
197
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000198template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000199 // Find an existing Symbol or create and insert a new one.
200 StringRef Name = New->getName();
201 Symbol *&Sym = Symtab[Name];
Rui Ueyama38dcc9e2015-12-16 23:25:31 +0000202 if (!Sym)
Michael J. Spencer84487f12015-07-24 21:03:07 +0000203 Sym = new (Alloc) Symbol(New);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000204 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000205 return Sym;
206}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000207
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000208template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
209 auto It = Symtab.find(Name);
210 if (It == Symtab.end())
211 return nullptr;
212 return It->second->Body;
213}
214
Rui Ueyamac5b95122015-12-16 23:23:14 +0000215template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) {
216 Symbol *Sym = insert(L);
217 if (Sym->Body == L)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000218 return;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000219 if (auto *Undef = dyn_cast<Undefined<ELFT>>(Sym->Body)) {
220 Sym->Body = L;
221 addMemberFile(Undef, L);
Rafael Espindola8614c562015-10-06 14:33:58 +0000222 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000223}
224
Rui Ueyama3d451792015-10-12 18:03:21 +0000225template <class ELFT>
Rui Ueyamac5b95122015-12-16 23:23:14 +0000226void 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>;