blob: 017ac832c87718eb1a80e893d3b7169730df243b [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"
Rafael Espindola9f77ef02016-02-12 20:54:57 +000021#include "llvm/Bitcode/ReaderWriter.h"
Rui Ueyamadeb15402016-01-07 17:20:07 +000022#include "llvm/Support/StringSaver.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
24using namespace llvm;
Rafael Espindoladaa92a62015-08-31 01:16:19 +000025using namespace llvm::object;
Rafael Espindola01205f72015-09-22 18:19:46 +000026using namespace llvm::ELF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000027
28using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000029using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
Rui Ueyamac9559d92016-01-05 20:47:37 +000031// All input object files must be for the same architecture
32// (e.g. it does not make sense to link x86 object files with
33// MIPS object files.) This function checks for that error.
Rui Ueyama16ba6692016-01-29 19:41:13 +000034template <class ELFT> static bool isCompatible(InputFile *FileP) {
Rui Ueyama25b44c92015-12-16 23:31:22 +000035 auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP);
36 if (!F)
Rui Ueyama16ba6692016-01-29 19:41:13 +000037 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000038 if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine)
Rui Ueyama16ba6692016-01-29 19:41:13 +000039 return true;
Rui Ueyama25b44c92015-12-16 23:31:22 +000040 StringRef A = F->getName();
41 StringRef B = Config->Emulation;
42 if (B.empty())
43 B = Config->FirstElf->getName();
Rui Ueyama16ba6692016-01-29 19:41:13 +000044 error(A + " is incompatible with " + B);
45 return false;
Rui Ueyama25b44c92015-12-16 23:31:22 +000046}
47
Rui Ueyamac9559d92016-01-05 20:47:37 +000048// Add symbols in File to the symbol table.
Rui Ueyama25b44c92015-12-16 23:31:22 +000049template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +000050void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000051 InputFile *FileP = File.get();
Rui Ueyama16ba6692016-01-29 19:41:13 +000052 if (!isCompatible<ELFT>(FileP))
53 return;
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)) {
Rafael Espindola21f7bd42015-12-23 14:35:51 +000057 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
Rui Ueyama89575742015-12-16 22:59:13 +000058 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();
Rui Ueyama131e0ff2016-01-08 22:17:42 +000068 if (!SoNames.insert(F->getSoName()).second)
Rafael Espindola6a3b5de2015-10-01 19:52:48 +000069 return;
Rui Ueyama89575742015-12-16 22:59:13 +000070
Rafael Espindola21f7bd42015-12-23 14:35:51 +000071 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
Rui Ueyama7c713312016-01-06 01:56:36 +000072 F->parseRest();
Rui Ueyama89575742015-12-16 22:59:13 +000073 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
Rafael Espindola9f77ef02016-02-12 20:54:57 +000078 // LLVM bitcode file.
79 if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
80 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
Rafael Espindola4de44b72016-03-02 15:43:50 +000081 F->parse(ComdatGroups);
Rafael Espindola297ce4e2016-02-26 21:31:34 +000082 for (SymbolBody *B : F->getSymbols())
Rui Ueyamaf7149552016-03-11 18:46:51 +000083 if (B)
84 resolve(B);
Rafael Espindola9f77ef02016-02-12 20:54:57 +000085 return;
86 }
87
Rui Ueyama89575742015-12-16 22:59:13 +000088 // .o file
89 auto *F = cast<ObjectFile<ELFT>>(FileP);
Rafael Espindola21f7bd42015-12-23 14:35:51 +000090 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
Rui Ueyama52d3b672016-01-06 02:06:33 +000091 F->parse(ComdatGroups);
Rafael Espindola67d72c02016-03-11 12:06:30 +000092 for (SymbolBody *B : F->getNonLocalSymbols())
Rui Ueyama89575742015-12-16 22:59:13 +000093 resolve(B);
Michael J. Spencer84487f12015-07-24 21:03:07 +000094}
95
Rafael Espindola9f77ef02016-02-12 20:54:57 +000096template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
97 if (BitcodeFiles.empty())
98 return;
Rui Ueyama25992482016-03-22 20:52:10 +000099
100 // Compile bitcode files.
101 Lto.reset(new BitcodeCompiler);
102 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
103 Lto->add(*F);
104 std::unique_ptr<ObjectFile<ELFT>> Obj = Lto->compile<ELFT>();
105
106 // Replace bitcode symbols.
Rafael Espindola4de44b72016-03-02 15:43:50 +0000107 llvm::DenseSet<StringRef> DummyGroups;
108 Obj->parse(DummyGroups);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000109 for (SymbolBody *Body : Obj->getNonLocalSymbols()) {
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000110 Symbol *Sym = insert(Body);
Davide Italiano04d6aa22016-03-29 00:15:52 +0000111 Sym->Body->setUsedInRegularObj();
Rafael Espindolacdf3a2a2016-03-02 18:21:46 +0000112 if (!Sym->Body->isUndefined() && Body->isUndefined())
113 continue;
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000114 Sym->Body = Body;
115 }
Rui Ueyama25992482016-03-22 20:52:10 +0000116 ObjectFiles.push_back(std::move(Obj));
Rafael Espindola9f77ef02016-02-12 20:54:57 +0000117}
118
Rui Ueyama01a65b12015-12-24 10:37:32 +0000119// Add an undefined symbol.
Rui Ueyamaff777682015-10-09 21:12:40 +0000120template <class ELFT>
121SymbolBody *SymbolTable<ELFT>::addUndefined(StringRef Name) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000122 auto *Sym = new (Alloc) Undefined(Name, false, STV_DEFAULT, false);
Rui Ueyamaff777682015-10-09 21:12:40 +0000123 resolve(Sym);
124 return Sym;
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000125}
126
Rui Ueyama01a65b12015-12-24 10:37:32 +0000127// Add an undefined symbol. Unlike addUndefined, that symbol
128// doesn't have to be resolved, thus "opt" (optional).
Rui Ueyamaff777682015-10-09 21:12:40 +0000129template <class ELFT>
130SymbolBody *SymbolTable<ELFT>::addUndefinedOpt(StringRef Name) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000131 auto *Sym = new (Alloc) Undefined(Name, false, STV_HIDDEN, true);
Rui Ueyamaff777682015-10-09 21:12:40 +0000132 resolve(Sym);
133 return Sym;
Denis Protivensky22220d52015-10-05 09:43:57 +0000134}
135
Rafael Espindola0e604f92015-09-25 18:56:53 +0000136template <class ELFT>
Rui Ueyama79c73732016-01-08 21:53:28 +0000137SymbolBody *SymbolTable<ELFT>::addAbsolute(StringRef Name, Elf_Sym &ESym) {
138 // Pass nullptr because absolute symbols have no corresponding input sections.
139 auto *Sym = new (Alloc) DefinedRegular<ELFT>(Name, ESym, nullptr);
140 resolve(Sym);
141 return Sym;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000142}
143
144template <class ELFT>
Rui Ueyama79c73732016-01-08 21:53:28 +0000145SymbolBody *SymbolTable<ELFT>::addSynthetic(StringRef Name,
George Rimaraa4dc202016-03-01 16:23:13 +0000146 OutputSectionBase<ELFT> &Sec,
147 uintX_t Val, uint8_t Visibility) {
148 auto *Sym = new (Alloc) DefinedSynthetic<ELFT>(Name, Val, Sec, Visibility);
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000149 resolve(Sym);
Rui Ueyama79c73732016-01-08 21:53:28 +0000150 return Sym;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000151}
152
Rui Ueyamac9559d92016-01-05 20:47:37 +0000153// Add Name as an "ignored" symbol. An ignored symbol is a regular
154// linker-synthesized defined symbol, but it is not recorded to the output
155// file's symbol table. Such symbols are useful for some linker-defined symbols.
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000156template <class ELFT>
Rui Ueyamadd7d9982015-12-16 22:31:14 +0000157SymbolBody *SymbolTable<ELFT>::addIgnored(StringRef Name) {
Rafael Espindola65e80b92016-01-19 21:19:52 +0000158 return addAbsolute(Name, ElfSym<ELFT>::Ignored);
Rafael Espindola5d413262015-10-01 21:22:26 +0000159}
160
Rui Ueyamadeb15402016-01-07 17:20:07 +0000161// Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
162// Used to implement --wrap.
163template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
164 if (Symtab.count(Name) == 0)
165 return;
166 StringSaver Saver(Alloc);
167 Symbol *Sym = addUndefined(Name)->getSymbol();
168 Symbol *Real = addUndefined(Saver.save("__real_" + Name))->getSymbol();
169 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name))->getSymbol();
170 Real->Body = Sym->Body;
171 Sym->Body = Wrap->Body;
172}
173
Rui Ueyama533336a2015-12-16 22:26:48 +0000174// Returns a file from which symbol B was created.
Rui Ueyama2a65a492016-01-05 20:01:29 +0000175// If B does not belong to any file, returns a nullptr.
Rafael Espindola18f09502016-02-26 21:49:38 +0000176template <class ELFT> InputFile *SymbolTable<ELFT>::findFile(SymbolBody *B) {
Rui Ueyama533336a2015-12-16 22:26:48 +0000177 for (const std::unique_ptr<ObjectFile<ELFT>> &F : ObjectFiles) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000178 ArrayRef<SymbolBody *> Syms = F->getSymbols();
179 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
Rui Ueyama533336a2015-12-16 22:26:48 +0000180 return F.get();
Rafael Espindola1a49e582015-09-23 14:10:24 +0000181 }
Rafael Espindola18f09502016-02-26 21:49:38 +0000182 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) {
183 ArrayRef<SymbolBody *> Syms = F->getSymbols();
184 if (std::find(Syms.begin(), Syms.end(), B) != Syms.end())
185 return F.get();
186 }
Rui Ueyama533336a2015-12-16 22:26:48 +0000187 return nullptr;
188}
189
Rui Ueyama71c066d2016-02-02 08:22:41 +0000190// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
Rafael Espindola18f09502016-02-26 21:49:38 +0000191static std::string getFilename(InputFile *F) {
Rui Ueyama71c066d2016-02-02 08:22:41 +0000192 if (!F)
193 return "(internal)";
194 if (!F->ArchiveName.empty())
195 return (F->ArchiveName + "(" + F->getName() + ")").str();
196 return F->getName();
197}
198
Rui Ueyamab4de5952016-01-08 22:01:33 +0000199// Construct a string in the form of "Sym in File1 and File2".
200// Used to construct an error message.
Rui Ueyama533336a2015-12-16 22:26:48 +0000201template <class ELFT>
202std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Old, SymbolBody *New) {
Rafael Espindola18f09502016-02-26 21:49:38 +0000203 InputFile *F1 = findFile(Old);
204 InputFile *F2 = findFile(New);
Rui Ueyamaf0904012015-12-16 22:26:45 +0000205 StringRef Sym = Old->getName();
Rui Ueyama71c066d2016-02-02 08:22:41 +0000206 return demangle(Sym) + " in " + getFilename(F1) + " and " + getFilename(F2);
Rafael Espindola1a49e582015-09-23 14:10:24 +0000207}
208
Michael J. Spencer84487f12015-07-24 21:03:07 +0000209// This function resolves conflicts if there's an existing symbol with
210// the same name. Decisions are made based on symbol type.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000211template <class ELFT> void SymbolTable<ELFT>::resolve(SymbolBody *New) {
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000212 Symbol *Sym = insert(New);
213 if (Sym->Body == New)
214 return;
215
216 SymbolBody *Existing = Sym->Body;
217
218 if (Lazy *L = dyn_cast<Lazy>(Existing)) {
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000219 if (auto *Undef = dyn_cast<Undefined>(New)) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000220 addMemberFile(Undef, L);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000221 return;
222 }
Rui Ueyamac5b95122015-12-16 23:23:14 +0000223 // Found a definition for something also in an archive.
224 // Ignore the archive definition.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000225 Sym->Body = New;
226 return;
227 }
228
George Rimar2f0fab52016-03-06 06:26:18 +0000229 if (New->IsTls != Existing->IsTls) {
Rui Ueyama16ba6692016-01-29 19:41:13 +0000230 error("TLS attribute mismatch for symbol: " + conflictMsg(Existing, New));
231 return;
232 }
Igor Kudrin65bddea2015-10-09 09:58:39 +0000233
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000234 // compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
235 // equivalent (conflicting), or more preferable, respectively.
Rui Ueyama2e0a9ff2016-01-06 00:09:39 +0000236 int Comp = Existing->compare<ELFT>(New);
237 if (Comp == 0) {
George Rimar57610422016-03-11 14:43:02 +0000238 std::string S = "duplicate symbol: " + conflictMsg(Existing, New);
Rui Ueyama16ba6692016-01-29 19:41:13 +0000239 if (Config->AllowMultipleDefinition)
240 warning(S);
241 else
242 error(S);
Rui Ueyamaf0904012015-12-16 22:26:45 +0000243 return;
244 }
Rui Ueyama2e0a9ff2016-01-06 00:09:39 +0000245 if (Comp < 0)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000246 Sym->Body = New;
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000247}
248
Rui Ueyamab4de5952016-01-08 22:01:33 +0000249// Find an existing symbol or create and insert a new one.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000250template <class ELFT> Symbol *SymbolTable<ELFT>::insert(SymbolBody *New) {
Michael J. Spencer84487f12015-07-24 21:03:07 +0000251 StringRef Name = New->getName();
252 Symbol *&Sym = Symtab[Name];
Rui Ueyama38dcc9e2015-12-16 23:25:31 +0000253 if (!Sym)
Rui Ueyama3554f592015-12-17 00:01:25 +0000254 Sym = new (Alloc) Symbol{New};
Michael J. Spencer84487f12015-07-24 21:03:07 +0000255 New->setBackref(Sym);
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000256 return Sym;
257}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000258
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000259template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
260 auto It = Symtab.find(Name);
261 if (It == Symtab.end())
262 return nullptr;
263 return It->second->Body;
264}
265
Rui Ueyamac5b95122015-12-16 23:23:14 +0000266template <class ELFT> void SymbolTable<ELFT>::addLazy(Lazy *L) {
267 Symbol *Sym = insert(L);
268 if (Sym->Body == L)
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000269 return;
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000270 if (auto *Undef = dyn_cast<Undefined>(Sym->Body)) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000271 Sym->Body = L;
272 addMemberFile(Undef, L);
Rafael Espindola8614c562015-10-06 14:33:58 +0000273 }
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000274}
275
Rui Ueyama3d451792015-10-12 18:03:21 +0000276template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000277void SymbolTable<ELFT>::addMemberFile(Undefined *Undef, Lazy *L) {
Rui Ueyamac5b95122015-12-16 23:23:14 +0000278 // Weak undefined symbols should not fetch members from archives.
279 // If we were to keep old symbol we would not know that an archive member was
280 // available if a strong undefined symbol shows up afterwards in the link.
281 // If a strong undefined symbol never shows up, this lazy symbol will
282 // get to the end of the link and must be treated as the weak undefined one.
283 // We set UsedInRegularObj in a similar way to what is done with shared
Rafael Espindola8176d572016-02-22 23:19:29 +0000284 // symbols and copy information to reduce how many special cases are needed.
Rui Ueyamac5b95122015-12-16 23:23:14 +0000285 if (Undef->isWeak()) {
286 L->setUsedInRegularObj();
287 L->setWeak();
Rafael Espindola8176d572016-02-22 23:19:29 +0000288
289 // FIXME: Do we need to copy more?
George Rimar2f0fab52016-03-06 06:26:18 +0000290 L->IsTls |= Undef->IsTls;
Rui Ueyamac5b95122015-12-16 23:23:14 +0000291 return;
292 }
293
294 // Fetch a member file that has the definition for L.
Michael J. Spencer1b348a62015-09-04 22:28:10 +0000295 // getMember returns nullptr if the member was already read from the library.
Rui Ueyamac5b95122015-12-16 23:23:14 +0000296 if (std::unique_ptr<InputFile> File = L->getMember())
Rui Ueyama690db672015-10-14 22:32:10 +0000297 addFile(std::move(File));
Michael J. Spencer84487f12015-07-24 21:03:07 +0000298}
Rafael Espindola0e604f92015-09-25 18:56:53 +0000299
Rui Ueyama93bfee52015-10-13 18:10:33 +0000300// This function takes care of the case in which shared libraries depend on
301// the user program (not the other way, which is usual). Shared libraries
302// may have undefined symbols, expecting that the user program provides
303// the definitions for them. An example is BSD's __progname symbol.
304// We need to put such symbols to the main program's .dynsym so that
305// shared libraries can find them.
306// Except this, we ignore undefined symbols in DSOs.
307template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000308 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
309 for (StringRef U : File->getUndefinedSymbols())
310 if (SymbolBody *Sym = find(U))
311 if (Sym->isDefined())
Rafael Espindolaabebed92016-02-05 15:27:15 +0000312 Sym->MustBeInDynSym = true;
Rui Ueyamaf8432d92015-10-13 16:34:14 +0000313}
314
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000315template class elf::SymbolTable<ELF32LE>;
316template class elf::SymbolTable<ELF32BE>;
317template class elf::SymbolTable<ELF64LE>;
318template class elf::SymbolTable<ELF64BE>;