blob: db1e0cfd01e8c041e2204668d72ac17f9ba6c688 [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>
Rafael Espindolaae81a7b2015-10-15 15:29:53 +000074void SymbolTable<ELFT>::addSyntheticSym(
75 StringRef Name, OutputSectionBase<ELFT::Is64Bits> &Section,
76 typename ELFFile<ELFT>::uintX_t Value) {
Rafael Espindola0e604f92015-09-25 18:56:53 +000077 typedef typename DefinedSynthetic<ELFT>::Elf_Sym Elf_Sym;
78 auto ESym = new (Alloc) Elf_Sym;
79 memset(ESym, 0, sizeof(Elf_Sym));
80 ESym->st_value = Value;
81 auto Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, *ESym, Section);
Rui Ueyama3ce825e2015-10-09 21:07:25 +000082 resolve(Sym);
Rafael Espindola0e604f92015-09-25 18:56:53 +000083}
84
Rui Ueyama3ce825e2015-10-09 21:07:25 +000085template <class ELFT> void SymbolTable<ELFT>::addIgnoredSym(StringRef Name) {
Rafael Espindola5d413262015-10-01 21:22:26 +000086 auto Sym = new (Alloc)
87 DefinedAbsolute<ELFT>(Name, DefinedAbsolute<ELFT>::IgnoreUndef);
Rui Ueyama3ce825e2015-10-09 21:07:25 +000088 resolve(Sym);
Rafael Espindola5d413262015-10-01 21:22:26 +000089}
90
Rui Ueyamad9189ce2015-10-15 17:11:03 +000091template <class ELFT> bool SymbolTable<ELFT>::isUndefined(StringRef Name) {
92 if (SymbolBody *Sym = find(Name))
93 return Sym->isUndefined();
94 return false;
95}
96
Rafael Espindolaaf707642015-10-12 01:55:32 +000097template <class ELFT>
98void SymbolTable<ELFT>::addELFFile(ELFFileBase<ELFT> *File) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +000099 if (auto *O = dyn_cast<ObjectFile<ELFT>>(File))
Rafael Espindolab90582db2015-10-06 15:03:52 +0000100 ObjectFiles.emplace_back(O);
101 else if (auto *S = dyn_cast<SharedFile<ELFT>>(File))
102 SharedFiles.emplace_back(S);
103
Rafael Espindoladfce5a22015-10-12 02:22:58 +0000104 if (auto *O = dyn_cast<ObjectFile<ELFT>>(File)) {
Rafael Espindola824d1a92015-09-04 00:09:43 +0000105 for (SymbolBody *Body : O->getSymbols())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000106 resolve(Body);
Rafael Espindoladaa92a62015-08-31 01:16:19 +0000107 }
Rafael Espindolaf98d6d82015-09-03 20:03:54 +0000108
Rafael Espindola18173d42015-09-08 15:50:05 +0000109 if (auto *S = dyn_cast<SharedFile<ELFT>>(File)) {
Rafael Espindola18173d42015-09-08 15:50:05 +0000110 for (SharedSymbol<ELFT> &Body : S->getSharedSymbols())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000111 resolve(&Body);
Rafael Espindola824d1a92015-09-04 00:09:43 +0000112 }
113}
114
Rafael Espindola1a49e582015-09-23 14:10:24 +0000115template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000116void SymbolTable<ELFT>::reportConflict(const Twine &Message,
117 const SymbolBody &Old,
118 const SymbolBody &New, bool Warning) {
Rafael Espindola1a49e582015-09-23 14:10:24 +0000119 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
120 typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
121
122 const Elf_Sym &OldE = cast<ELFSymbolBody<ELFT>>(Old).Sym;
123 const Elf_Sym &NewE = cast<ELFSymbolBody<ELFT>>(New).Sym;
Rafael Espindolaaf707642015-10-12 01:55:32 +0000124 ELFFileBase<ELFT> *OldFile = nullptr;
125 ELFFileBase<ELFT> *NewFile = nullptr;
Rafael Espindola1a49e582015-09-23 14:10:24 +0000126
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000127 for (const std::unique_ptr<ObjectFile<ELFT>> &File : ObjectFiles) {
128 Elf_Sym_Range Syms = File->getObj().symbols(File->getSymbolTable());
Rafael Espindola1a49e582015-09-23 14:10:24 +0000129 if (&OldE > Syms.begin() && &OldE < Syms.end())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000130 OldFile = File.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000131 if (&NewE > Syms.begin() && &NewE < Syms.end())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000132 NewFile = File.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000133 }
134
Igor Kudrin65bddea2015-10-09 09:58:39 +0000135 std::string Msg = (Message + ": " + Old.getName() + " in " +
Rafael Espindola4b2ca852015-09-28 20:30:11 +0000136 OldFile->getName() + " and " + NewFile->getName())
137 .str();
Igor Kudrin65bddea2015-10-09 09:58:39 +0000138 if (Warning)
Rafael Espindola4b2ca852015-09-28 20:30:11 +0000139 warning(Msg);
140 else
141 error(Msg);
Rafael Espindola1a49e582015-09-23 14:10:24 +0000142}
143
Michael J. Spencer84487f12015-07-24 21:03:07 +0000144// This function resolves conflicts if there's an existing symbol with
145// the same name. Decisions are made based on symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000146template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000147 Symbol *Sym = insert(New);
148 if (Sym->Body == New)
149 return;
150
151 SymbolBody *Existing = Sym->Body;
152
153 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
154 if (New->isUndefined()) {
Rafael Espindola85a6e0f2015-10-06 15:18:50 +0000155 if (New->isWeak()) {
156 // See the explanation in SymbolTable::addLazy
157 L->setUsedInRegularObj();
158 L->setWeak();
159 return;
160 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000161 addMemberFile(L);
162 return;
163 }
164
165 // Found a definition for something also in an archive. Ignore the archive
166 // definition.
167 Sym->Body = New;
168 return;
169 }
170
Igor Kudrin65bddea2015-10-09 09:58:39 +0000171 if (New->isTLS() != Existing->isTLS())
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000172 reportConflict("TLS attribute mismatch for symbol", *Existing, *New, false);
Igor Kudrin65bddea2015-10-09 09:58:39 +0000173
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000174 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
175 // equivalent (conflicting), or more preferable, respectively.
176 int comp = Existing->compare<ELFT>(New);
177 if (comp < 0)
178 Sym->Body = New;
Rafael Espindola1a49e582015-09-23 14:10:24 +0000179 else if (comp == 0)
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000180 reportConflict("duplicate symbol", *Existing, *New,
181 Config->AllowMultipleDefinition);
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];
188 if (!Sym) {
189 Sym = new (Alloc) Symbol(New);
190 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000191 return Sym;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000192 }
193 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000194 return Sym;
195}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000196
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000197template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
198 auto It = Symtab.find(Name);
199 if (It == Symtab.end())
200 return nullptr;
201 return It->second->Body;
202}
203
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000204template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000205 Symbol *Sym = insert(New);
206 if (Sym->Body == New)
207 return;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000208 SymbolBody *Existing = Sym->Body;
Rafael Espindola8614c562015-10-06 14:33:58 +0000209 if (Existing->isDefined() || Existing->isLazy())
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000210 return;
211 Sym->Body = New;
212 assert(Existing->isUndefined() && "Unexpected symbol kind.");
Rafael Espindola8614c562015-10-06 14:33:58 +0000213
214 // Weak undefined symbols should not fetch members from archives.
215 // If we were to keep old symbol we would not know that an archive member was
216 // available if a strong undefined symbol shows up afterwards in the link.
217 // If a strong undefined symbol never shows up, this lazy symbol will
218 // get to the end of the link and must be treated as the weak undefined one.
219 // We set UsedInRegularObj in a similar way to what is done with shared
220 // symbols and mark it as weak to reduce how many special cases are needed.
221 if (Existing->isWeak()) {
222 New->setUsedInRegularObj();
223 New->setWeak();
224 return;
225 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000226 addMemberFile(New);
227}
228
Rui Ueyama3d451792015-10-12 18:03:21 +0000229template <class ELFT>
230void SymbolTable<ELFT>::checkCompatibility(std::unique_ptr<InputFile> &File) {
231 auto *E = dyn_cast<ELFFileBase<ELFT>>(File.get());
232 if (!E)
233 return;
Rui Ueyamae717a712015-10-13 16:20:50 +0000234 if (E->getELFKind() == Config->EKind && E->getEMachine() == Config->EMachine)
Rui Ueyama3d451792015-10-12 18:03:21 +0000235 return;
236 StringRef A = E->getName();
237 StringRef B = Config->Emulation;
238 if (B.empty())
239 B = Config->FirstElf->getName();
240 error(A + " is incompatible with " + B);
241}
242
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000243template <class ELFT> void SymbolTable<ELFT>::addMemberFile(Lazy *Body) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000244 // getMember returns nullptr if the member was already read from the library.
Rui Ueyama690db672015-10-14 22:32:10 +0000245 if (std::unique_ptr<InputFile> File = Body->getMember())
246 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000247}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000248
Rui Ueyama93bfee52015-10-13 18:10:33 +0000249// This function takes care of the case in which shared libraries depend on
250// the user program (not the other way, which is usual). Shared libraries
251// may have undefined symbols, expecting that the user program provides
252// the definitions for them. An example is BSD's __progname symbol.
253// We need to put such symbols to the main program's .dynsym so that
254// shared libraries can find them.
255// Except this, we ignore undefined symbols in DSOs.
256template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000257 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
258 for (StringRef U : File->getUndefinedSymbols())
259 if (SymbolBody *Sym = find(U))
260 if (Sym->isDefined())
261 Sym->setUsedInDynamicReloc();
262}
263
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000264template class lld::elf2::SymbolTable<ELF32LE>;
265template class lld::elf2::SymbolTable<ELF32BE>;
266template class lld::elf2::SymbolTable<ELF64LE>;
267template class lld::elf2::SymbolTable<ELF64BE>;