blob: a66bb04df3e18f8bd66ece1426a33268c33b77be [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
Rui Ueyamac9559d92016-01-05 20:47:37 +000011// all input files to the symbol table. The symbol table is basically
Rui Ueyama34f29242015-10-13 19:51:57 +000012// 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"
Rui Ueyamadeb15402016-01-07 17:20:07 +000021#include "llvm/Support/StringSaver.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
23using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000024using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000025using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
27using namespace lld;
28using namespace lld::elf2;
29
Rui Ueyamac9559d92016-01-05 20:47:37 +000030// All input object files must be for the same architecture
31// (e.g. it does not make sense to link x86 object files with
32// MIPS object files.) This function checks for that error.
Rui Ueyama3ce825e2015-10-09 21:07:25 +000033template <class ELFT>
Rui Ueyama25b44c92015-12-16 23:31:22 +000034static void checkCompatibility(InputFile *FileP) {
35 auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP);
36 if (!F)
37 return;
38 if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine)
39 return;
40 StringRef A = F->getName();
41 StringRef B = Config->Emulation;
42 if (B.empty())
43 B = Config->FirstElf->getName();
44 error(A + " is incompatible with " + B);
45}
46
Rui Ueyamac9559d92016-01-05 20:47:37 +000047// Add symbols in File to the symbol table.
Rui Ueyama25b44c92015-12-16 23:31:22 +000048template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000049void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000050 InputFile *FileP = File.get();
Rui Ueyama25b44c92015-12-16 23:31:22 +000051 checkCompatibility<ELFT>(FileP);
Rafael Espindola525914d2015-10-11 03:36:49 +000052
Rui Ueyama89575742015-12-16 22:59:13 +000053 // .a file
54 if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000055 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Rui Ueyama89575742015-12-16 22:59:13 +000056 F->parse();
57 for (Lazy &Sym : F->getLazySymbols())
Michael J. Spencer1b348a62015-09-04 22:28:10 +000058 addLazy(&Sym);
59 return;
60 }
Rui Ueyama3d451792015-10-12 18:03:21 +000061
Rui Ueyama89575742015-12-16 22:59:13 +000062 // .so file
63 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
64 // DSOs are uniquified not by filename but by soname.
65 F->parseSoName();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000066 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000067 return;
Rui Ueyama89575742015-12-16 22:59:13 +000068
Rafael Espindola21f7bd42015-12-23 14:35:51 +000069 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000070 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000071 for (SharedSymbol<ELFT> &B : F->getSharedSymbols())
72 resolve(&B);
73 return;
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000074 }
Rui Ueyama89575742015-12-16 22:59:13 +000075
76 // .o file
77 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +000078 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +000079 F->parse(ComdatGroups);
Rui Ueyama89575742015-12-16 22:59:13 +000080 for (SymbolBody *B : F->getSymbols())
81 resolve(B);
Michael J. Spencer84487f12015-07-24 21:03:07 +000082}
83
Rui Ueyama01a65b12015-12-24 10:37:32 +000084// Add an undefined symbol.
Rui Ueyamaff777682015-10-09 21:12:40 +000085template <class ELFT>
86SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +000087 auto *Sym = new (Alloc) Undefined(Name, false, STV_DEFAULT, false);
Rui Ueyamaff777682015-10-09 21:12:40 +000088 resolve(Sym);
89 return Sym;
Rafael Espindola1d6063e2015-09-22 21:24:52 +000090}
91
Rui Ueyama01a65b12015-12-24 10:37:32 +000092// Add an undefined symbol. Unlike addUndefined, that symbol
93// doesn't have to be resolved, thus "opt" (optional).
Rui Ueyamaff777682015-10-09 21:12:40 +000094template <class ELFT>
95SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +000096 auto *Sym = new (Alloc) Undefined(Name, false, STV_HIDDEN, true);
Rui Ueyamaff777682015-10-09 21:12:40 +000097 resolve(Sym);
98 return Sym;
Denis Protivensky22220d52015-10-05 09:43:57 +000099}
100
Rafael Espindola0e604f92015-09-25 18:56:53 +0000101template <class ELFT>
Rui Ueyama79c73732016-01-08 21:53:28 +0000102SymbolBody *SymbolTable<ELFT>::addAbsolute(StringRef Name, Elf_Sym &ESym) {
103 // Pass nullptr because absolute symbols have no corresponding input sections.
104 auto *Sym = new (Alloc) DefinedRegular<ELFT>(Name, ESym, nullptr);
105 resolve(Sym);
106 return Sym;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000107}
108
109template <class ELFT>
Rui Ueyama79c73732016-01-08 21:53:28 +0000110SymbolBody *SymbolTable<ELFT>::addSynthetic(StringRef Name,
111 OutputSectionBase<ELFT> &Section,
112 uintX_t Value) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000113 auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, Value, Section);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000114 resolve(Sym);
Rui Ueyama79c73732016-01-08 21:53:28 +0000115 return Sym;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000116}
117
Rui Ueyamac9559d92016-01-05 20:47:37 +0000118// Add Name as an "ignored" symbol. An ignored symbol is a regular
119// linker-synthesized defined symbol, but it is not recorded to the output
120// file's symbol table. Such symbols are useful for some linker-defined symbols.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000121template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000122SymbolBody *SymbolTable<ELFT>::addIgnored(StringRef Name) {
Rui Ueyama79c73732016-01-08 21:53:28 +0000123 return addAbsolute(Name, ElfSym<ELFT>::IgnoreUndef);
Rafael Espindola5d413262015-10-01 21:22:26 +0000124}
125
Simon Atanasyan188558e2016-01-12 06:23:57 +0000126// The 'strong' variant of the addIgnored. Adds symbol which has a global
127// binding and cannot be substituted.
128template <class ELFT>
129SymbolBody *SymbolTable<ELFT>::addIgnoredStrong(StringRef Name) {
130 return addAbsolute(Name, ElfSym<ELFT>::IgnoreUndefStrong);
131}
132
Rui Ueyamadeb15402016-01-07 17:20:07 +0000133// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
134// Used to implement --wrap.
135template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
136 if (Symtab.count(Name) == 0)
137 return;
138 StringSaver Saver(Alloc);
139 Symbol *Sym = addUndefined(Name)->getSymbol();
140 Symbol *Real = addUndefined(Saver.save("__real_" + Name))->getSymbol();
141 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name))->getSymbol();
142 Real->Body = Sym->Body;
143 Sym->Body = Wrap->Body;
144}
145
Rui Ueyama533336a2015-12-16 22:26:48 +0000146// Returns a file from which symbol B was created.
Rui Ueyama2a65a492016-01-05 20:01:29 +0000147// If B does not belong to any file, returns a nullptr.
Rafael Espindola1a49e582015-09-23 14:10:24 +0000148template <class ELFT>
Rui Ueyama2a65a492016-01-05 20:01:29 +0000149ELFFileBase<ELFT> *SymbolTable<ELFT>::findFile(SymbolBody *B) {
Rui Ueyama533336a2015-12-16 22:26:48 +0000150 for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000151 ArrayRef<SymbolBody *> Syms = F->getSymbols();
152 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
Rui Ueyama533336a2015-12-16 22:26:48 +0000153 return F.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000154 }
Rui Ueyama533336a2015-12-16 22:26:48 +0000155 return nullptr;
156}
157
Rui Ueyamab4de5952016-01-08 22:01:33 +0000158// Construct a string in the form of "Sym in File1 and File2".
159// Used to construct an error message.
Rui Ueyama533336a2015-12-16 22:26:48 +0000160template <class ELFT>
161std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
Rui Ueyama2a65a492016-01-05 20:01:29 +0000162 ELFFileBase<ELFT> *OldFile = findFile(Old);
163 ELFFileBase<ELFT> *NewFile = findFile(New);
Rafael Espindola1a49e582015-09-23 14:10:24 +0000164
Rui Ueyamaf0904012015-12-16 22:26:45 +0000165 StringRef Sym = Old->getName();
166 StringRef F1 = OldFile ? OldFile->getName() : "(internal)";
167 StringRef F2 = NewFile ? NewFile->getName() : "(internal)";
168 return (Sym + " in " + F1 + " and " + F2).str();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000169}
170
Michael J. Spencer84487f12015-07-24 21:03:07 +0000171// This function resolves conflicts if there's an existing symbol with
172// the same name. Decisions are made based on symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000173template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000174 Symbol *Sym = insert(New);
175 if (Sym->Body == New)
176 return;
177
178 SymbolBody *Existing = Sym->Body;
179
180 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000181 if (auto *Undef = dyn_cast<Undefined>(New)) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000182 addMemberFile(Undef, L);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000183 return;
184 }
Rui Ueyamac5b95122015-12-16 23:23:14 +0000185 // Found a definition for something also in an archive.
186 // Ignore the archive definition.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000187 Sym->Body = New;
188 return;
189 }
190
Rui Ueyama62d0e322015-12-17 00:04:18 +0000191 if (New->isTls() != Existing->isTls())
Rui Ueyamaf0904012015-12-16 22:26:45 +0000192 error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New));
Igor Kudrin65bddea2015-10-09 09:58:39 +0000193
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000194 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
195 // equivalent (conflicting), or more preferable, respectively.
Rui Ueyama2e0a9ff2016-01-06 00:09:39 +0000196 int Comp = Existing->compare<ELFT>(New);
197 if (Comp == 0) {
Rui Ueyamaf0904012015-12-16 22:26:45 +0000198 std::string S = "duplicate symbol: " + conflictMsg(Existing, New);
199 if (!Config->AllowMultipleDefinition)
200 error(S);
201 warning(S);
202 return;
203 }
Rui Ueyama2e0a9ff2016-01-06 00:09:39 +0000204 if (Comp < 0)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000205 Sym->Body = New;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000206}
207
Rui Ueyamab4de5952016-01-08 22:01:33 +0000208// Find an existing symbol or create and insert a new one.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000209template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000210 StringRef Name = New->getName();
211 Symbol *&Sym = Symtab[Name];
Rui Ueyama38dcc9e2015-12-16 23:25:31 +0000212 if (!Sym)
Rui Ueyama3554f592015-12-17 00:01:25 +0000213 Sym = new (Alloc) Symbol{New};
Michael J. Spencer84487f12015-07-24 21:03:07 +0000214 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000215 return Sym;
216}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000217
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000218template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
219 auto It = Symtab.find(Name);
220 if (It == Symtab.end())
221 return nullptr;
222 return It->second->Body;
223}
224
Rui Ueyamac5b95122015-12-16 23:23:14 +0000225template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) {
226 Symbol *Sym = insert(L);
227 if (Sym->Body == L)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000228 return;
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000229 if (auto *Undef = dyn_cast<Undefined>(Sym->Body)) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000230 Sym->Body = L;
231 addMemberFile(Undef, L);
Rafael Espindola8614c562015-10-06 14:33:58 +0000232 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000233}
234
Rui Ueyama3d451792015-10-12 18:03:21 +0000235template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000236void SymbolTable<ELFT>::addMemberFile(Undefined *Undef, Lazy *L) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000237 // Weak undefined symbols should not fetch members from archives.
238 // If we were to keep old symbol we would not know that an archive member was
239 // available if a strong undefined symbol shows up afterwards in the link.
240 // If a strong undefined symbol never shows up, this lazy symbol will
241 // get to the end of the link and must be treated as the weak undefined one.
242 // We set UsedInRegularObj in a similar way to what is done with shared
243 // symbols and mark it as weak to reduce how many special cases are needed.
244 if (Undef->isWeak()) {
245 L->setUsedInRegularObj();
246 L->setWeak();
247 return;
248 }
249
250 // Fetch a member file that has the definition for L.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000251 // getMember returns nullptr if the member was already read from the library.
Rui Ueyamac5b95122015-12-16 23:23:14 +0000252 if (std::unique_ptr<InputFile> File = L->getMember())
Rui Ueyama690db672015-10-14 22:32:10 +0000253 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000254}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000255
Rui Ueyama93bfee52015-10-13 18:10:33 +0000256// This function takes care of the case in which shared libraries depend on
257// the user program (not the other way, which is usual). Shared libraries
258// may have undefined symbols, expecting that the user program provides
259// the definitions for them. An example is BSD's __progname symbol.
260// We need to put such symbols to the main program's .dynsym so that
261// shared libraries can find them.
262// Except this, we ignore undefined symbols in DSOs.
263template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000264 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
265 for (StringRef U : File->getUndefinedSymbols())
266 if (SymbolBody *Sym = find(U))
267 if (Sym->isDefined())
268 Sym->setUsedInDynamicReloc();
269}
270
Rui Ueyamaf588ac42016-01-06 00:09:41 +0000271template class elf2::SymbolTable<ELF32LE>;
272template class elf2::SymbolTable<ELF32BE>;
273template class elf2::SymbolTable<ELF64LE>;
274template class elf2::SymbolTable<ELF64BE>;