1 //===- 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 //===----------------------------------------------------------------------===//
9 //
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 //===----------------------------------------------------------------------===//
16 
17 #include "SymbolTable.h"
18 #include "Config.h"
19 #include "Error.h"
20 #include "Symbols.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Support/StringSaver.h"
23 
24 using namespace llvm;
25 using namespace llvm::object;
26 using namespace llvm::ELF;
27 
28 using namespace lld;
29 using namespace lld::elf;
30 
31 // 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.
34 template <class ELFT> static bool isCompatible(InputFile *FileP) {
35   auto *F = dyn_cast<ELFFileBase<ELFT>>(FileP);
36   if (!F)
37     return true;
38   if (F->getELFKind() == Config->EKind && F->getEMachine() == Config->EMachine)
39     return true;
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   return false;
46 }
47 
48 // Add symbols in File to the symbol table.
49 template <class ELFT>
50 void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
51   InputFile *FileP = File.get();
52   if (!isCompatible<ELFT>(FileP))
53     return;
54 
55   // .a file
56   if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
57     ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
58     F->parse<ELFT>();
59     return;
60   }
61 
62   // Lazy object file
63   if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
64     LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
65     F->parse<ELFT>();
66     return;
67   }
68 
69   if (Config->Trace)
70     llvm::outs() << getFilename(FileP) << "\n";
71 
72   // .so file
73   if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
74     // DSOs are uniquified not by filename but by soname.
75     F->parseSoName();
76     if (!SoNames.insert(F->getSoName()).second)
77       return;
78 
79     SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
80     F->parseRest();
81     return;
82   }
83 
84   // LLVM bitcode file
85   if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
86     BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
87     F->parse<ELFT>(ComdatGroups);
88     return;
89   }
90 
91   // Regular object file
92   auto *F = cast<ObjectFile<ELFT>>(FileP);
93   ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
94   F->parse(ComdatGroups);
95 }
96 
97 // This function is where all the optimizations of link-time
98 // optimization happens. When LTO is in use, some input files are
99 // not in native object file format but in the LLVM bitcode format.
100 // This function compiles bitcode files into a few big native files
101 // using LLVM functions and replaces bitcode symbols with the results.
102 // Because all bitcode files that consist of a program are passed
103 // to the compiler at once, it can do whole-program optimization.
104 template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() {
105   if (BitcodeFiles.empty())
106     return;
107 
108   // Compile bitcode files.
109   Lto.reset(new BitcodeCompiler);
110   for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
111     Lto->add(*F);
112   std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
113 
114   // Replace bitcode symbols.
115   for (auto &IF : IFs) {
116     ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
117 
118     llvm::DenseSet<StringRef> DummyGroups;
119     Obj->parse(DummyGroups);
120     ObjectFiles.emplace_back(Obj);
121   }
122 }
123 
124 template <class ELFT>
125 DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name,
126                                                      uint8_t Visibility) {
127   return cast<DefinedRegular<ELFT>>(
128       addRegular(Name, STB_GLOBAL, Visibility)->body());
129 }
130 
131 // Add Name as an "ignored" symbol. An ignored symbol is a regular
132 // linker-synthesized defined symbol, but is only defined if needed.
133 template <class ELFT>
134 DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name,
135                                                     uint8_t Visibility) {
136   if (!find(Name))
137     return nullptr;
138   return addAbsolute(Name, Visibility);
139 }
140 
141 // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
142 // Used to implement --wrap.
143 template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) {
144   SymbolBody *B = find(Name);
145   if (!B)
146     return;
147   StringSaver Saver(Alloc);
148   Symbol *Sym = B->symbol();
149   Symbol *Real = addUndefined(Saver.save("__real_" + Name));
150   Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
151   // We rename symbols by replacing the old symbol's SymbolBody with the new
152   // symbol's SymbolBody. This causes all SymbolBody pointers referring to the
153   // old symbol to instead refer to the new symbol.
154   memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body));
155   memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body));
156 }
157 
158 static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
159   if (VA == STV_DEFAULT)
160     return VB;
161   if (VB == STV_DEFAULT)
162     return VA;
163   return std::min(VA, VB);
164 }
165 
166 // Find an existing symbol or create and insert a new one.
167 template <class ELFT>
168 std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) {
169   unsigned NumSyms = SymVector.size();
170   auto P = Symtab.insert(std::make_pair(Name, NumSyms));
171   Symbol *Sym;
172   if (P.second) {
173     Sym = new (Alloc) Symbol;
174     Sym->Binding = STB_WEAK;
175     Sym->Visibility = STV_DEFAULT;
176     Sym->IsUsedInRegularObj = false;
177     Sym->ExportDynamic = false;
178     Sym->VersionScriptGlobal = !Config->VersionScript;
179     SymVector.push_back(Sym);
180   } else {
181     Sym = SymVector[P.first->second];
182   }
183   return {Sym, P.second};
184 }
185 
186 // Find an existing symbol or create and insert a new one, then apply the given
187 // attributes.
188 template <class ELFT>
189 std::pair<Symbol *, bool>
190 SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility,
191                           bool CanOmitFromDynSym, bool IsUsedInRegularObj,
192                           InputFile *File) {
193   Symbol *S;
194   bool WasInserted;
195   std::tie(S, WasInserted) = insert(Name);
196 
197   // Merge in the new symbol's visibility.
198   S->Visibility = getMinVisibility(S->Visibility, Visibility);
199   if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
200     S->ExportDynamic = true;
201   if (IsUsedInRegularObj)
202     S->IsUsedInRegularObj = true;
203   if (!WasInserted && S->body()->Type != SymbolBody::UnknownType &&
204       ((Type == STT_TLS) != S->body()->isTls()))
205     error("TLS attribute mismatch for symbol: " +
206           conflictMsg(S->body(), File));
207 
208   return {S, WasInserted};
209 }
210 
211 // Construct a string in the form of "Sym in File1 and File2".
212 // Used to construct an error message.
213 template <typename ELFT>
214 std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing,
215                                            InputFile *NewFile) {
216   StringRef Sym = Existing->getName();
217   return demangle(Sym) + " in " + getFilename(Existing->getSourceFile<ELFT>()) +
218          " and " + getFilename(NewFile);
219 }
220 
221 template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
222   return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
223                       /*File*/ nullptr);
224 }
225 
226 template <class ELFT>
227 Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
228                                         uint8_t StOther, uint8_t Type,
229                                         InputFile *File) {
230   Symbol *S;
231   bool WasInserted;
232   std::tie(S, WasInserted) =
233       insert(Name, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
234              /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
235   if (WasInserted) {
236     S->Binding = Binding;
237     replaceBody<Undefined>(S, Name, StOther, Type);
238     cast<Undefined>(S->body())->File = File;
239     return S;
240   }
241   if (Binding != STB_WEAK) {
242     if (S->body()->isShared() || S->body()->isLazy())
243       S->Binding = Binding;
244     if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body()))
245       SS->File->IsUsed = true;
246   }
247   if (auto *L = dyn_cast<Lazy>(S->body())) {
248     // An undefined weak will not fetch archive members, but we have to remember
249     // its type. See also comment in addLazyArchive.
250     if (S->isWeak())
251       L->Type = Type;
252     else if (auto F = L->getFile())
253       addFile(std::move(F));
254   }
255   return S;
256 }
257 
258 // We have a new defined symbol with the specified binding. Return 1 if the new
259 // symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
260 // strong defined symbols.
261 static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) {
262   if (WasInserted)
263     return 1;
264   SymbolBody *Body = S->body();
265   if (Body->isLazy() || Body->isUndefined() || Body->isShared())
266     return 1;
267   if (Binding == STB_WEAK)
268     return -1;
269   if (S->isWeak())
270     return 1;
271   return 0;
272 }
273 
274 // We have a new non-common defined symbol with the specified binding. Return 1
275 // if the new symbol should win, -1 if the new symbol should lose, or 0 if there
276 // is a conflict. If the new symbol wins, also update the binding.
277 static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) {
278   if (int Cmp = compareDefined(S, WasInserted, Binding)) {
279     if (Cmp > 0)
280       S->Binding = Binding;
281     return Cmp;
282   }
283   if (isa<DefinedCommon>(S->body())) {
284     // Non-common symbols take precedence over common symbols.
285     if (Config->WarnCommon)
286       warning("common " + S->body()->getName() + " is overridden");
287     return 1;
288   }
289   return 0;
290 }
291 
292 template <class ELFT>
293 Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
294                                      uint64_t Alignment, uint8_t Binding,
295                                      uint8_t StOther, uint8_t Type,
296                                      InputFile *File) {
297   Symbol *S;
298   bool WasInserted;
299   std::tie(S, WasInserted) =
300       insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
301              /*IsUsedInRegularObj*/ true, File);
302   int Cmp = compareDefined(S, WasInserted, Binding);
303   if (Cmp > 0) {
304     S->Binding = Binding;
305     replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type);
306   } else if (Cmp == 0) {
307     auto *C = dyn_cast<DefinedCommon>(S->body());
308     if (!C) {
309       // Non-common symbols take precedence over common symbols.
310       if (Config->WarnCommon)
311         warning("common " + S->body()->getName() + " is overridden");
312       return S;
313     }
314 
315     if (Config->WarnCommon)
316       warning("multiple common of " + S->body()->getName());
317 
318     C->Size = std::max(C->Size, Size);
319     C->Alignment = std::max(C->Alignment, Alignment);
320   }
321   return S;
322 }
323 
324 template <class ELFT>
325 void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing,
326                                         InputFile *NewFile) {
327   std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile);
328   if (Config->AllowMultipleDefinition)
329     warning(Msg);
330   else
331     error(Msg);
332 }
333 
334 template <typename ELFT>
335 Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym,
336                                       InputSectionBase<ELFT> *Section) {
337   Symbol *S;
338   bool WasInserted;
339   std::tie(S, WasInserted) =
340       insert(Name, Sym.getType(), Sym.getVisibility(),
341              /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
342              Section ? Section->getFile() : nullptr);
343   int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding());
344   if (Cmp > 0)
345     replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
346   else if (Cmp == 0)
347     reportDuplicate(S->body(), Section->getFile());
348   return S;
349 }
350 
351 template <typename ELFT>
352 Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
353                                       uint8_t StOther) {
354   Symbol *S;
355   bool WasInserted;
356   std::tie(S, WasInserted) =
357       insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
358              /*IsUsedInRegularObj*/ true, nullptr);
359   int Cmp = compareDefinedNonCommon(S, WasInserted, Binding);
360   if (Cmp > 0)
361     replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
362   else if (Cmp == 0)
363     reportDuplicate(S->body(), nullptr);
364   return S;
365 }
366 
367 template <typename ELFT>
368 Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
369                                         OutputSectionBase<ELFT> *Section,
370                                         uintX_t Value) {
371   Symbol *S;
372   bool WasInserted;
373   std::tie(S, WasInserted) =
374       insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false,
375              /*IsUsedInRegularObj*/ true, nullptr);
376   int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL);
377   if (Cmp > 0)
378     replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
379   else if (Cmp == 0)
380     reportDuplicate(S->body(), nullptr);
381   return S;
382 }
383 
384 template <typename ELFT>
385 void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name,
386                                   const Elf_Sym &Sym,
387                                   const typename ELFT::Verdef *Verdef) {
388   // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
389   // as the visibility, which will leave the visibility in the symbol table
390   // unchanged.
391   Symbol *S;
392   bool WasInserted;
393   std::tie(S, WasInserted) =
394       insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
395              /*IsUsedInRegularObj*/ false, F);
396   // Make sure we preempt DSO symbols with default visibility.
397   if (Sym.getVisibility() == STV_DEFAULT)
398     S->ExportDynamic = true;
399   if (WasInserted || isa<Undefined>(S->body())) {
400     replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef);
401     if (!S->isWeak())
402       F->IsUsed = true;
403   }
404 }
405 
406 template <class ELFT>
407 Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
408                                       uint8_t StOther, uint8_t Type,
409                                       bool CanOmitFromDynSym, BitcodeFile *F) {
410   Symbol *S;
411   bool WasInserted;
412   std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
413                                     /*IsUsedInRegularObj*/ false, F);
414   int Cmp =
415       compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL);
416   if (Cmp > 0)
417     replaceBody<DefinedBitcode>(S, Name, StOther, Type, F);
418   else if (Cmp == 0)
419     reportDuplicate(S->body(), F);
420   return S;
421 }
422 
423 template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
424   auto It = Symtab.find(Name);
425   if (It == Symtab.end())
426     return nullptr;
427   return SymVector[It->second]->body();
428 }
429 
430 template <class ELFT>
431 void SymbolTable<ELFT>::addLazyArchive(
432     ArchiveFile *F, const llvm::object::Archive::Symbol Sym) {
433   Symbol *S;
434   bool WasInserted;
435   std::tie(S, WasInserted) = insert(Sym.getName());
436   if (WasInserted) {
437     replaceBody<LazyArchive>(S, F, Sym, SymbolBody::UnknownType);
438     return;
439   }
440   if (!S->body()->isUndefined())
441     return;
442 
443   // Weak undefined symbols should not fetch members from archives. If we were
444   // to keep old symbol we would not know that an archive member was available
445   // if a strong undefined symbol shows up afterwards in the link. If a strong
446   // undefined symbol never shows up, this lazy symbol will get to the end of
447   // the link and must be treated as the weak undefined one. We already marked
448   // this symbol as used when we added it to the symbol table, but we also need
449   // to preserve its type. FIXME: Move the Type field to Symbol.
450   if (S->isWeak()) {
451     replaceBody<LazyArchive>(S, F, Sym, S->body()->Type);
452     return;
453   }
454   MemoryBufferRef MBRef = F->getMember(&Sym);
455   if (!MBRef.getBuffer().empty())
456     addFile(createObjectFile(MBRef, F->getName()));
457 }
458 
459 template <class ELFT>
460 void SymbolTable<ELFT>::addLazyObject(StringRef Name, MemoryBufferRef MBRef) {
461   Symbol *S;
462   bool WasInserted;
463   std::tie(S, WasInserted) = insert(Name);
464   if (WasInserted) {
465     replaceBody<LazyObject>(S, Name, MBRef, SymbolBody::UnknownType);
466     return;
467   }
468   if (!S->body()->isUndefined())
469     return;
470 
471   // See comment for addLazyArchive above.
472   if (S->isWeak())
473     replaceBody<LazyObject>(S, Name, MBRef, S->body()->Type);
474   else
475     addFile(createObjectFile(MBRef));
476 }
477 
478 // Process undefined (-u) flags by loading lazy symbols named by those flags.
479 template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
480   for (StringRef S : Config->Undefined)
481     if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
482       if (std::unique_ptr<InputFile> File = L->getFile())
483         addFile(std::move(File));
484 }
485 
486 // This function takes care of the case in which shared libraries depend on
487 // the user program (not the other way, which is usual). Shared libraries
488 // may have undefined symbols, expecting that the user program provides
489 // the definitions for them. An example is BSD's __progname symbol.
490 // We need to put such symbols to the main program's .dynsym so that
491 // shared libraries can find them.
492 // Except this, we ignore undefined symbols in DSOs.
493 template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
494   for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
495     for (StringRef U : File->getUndefinedSymbols())
496       if (SymbolBody *Sym = find(U))
497         if (Sym->isDefined())
498           Sym->symbol()->ExportDynamic = true;
499 }
500 
501 // This function process the dynamic list option by marking all the symbols
502 // to be exported in the dynamic table.
503 template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() {
504   for (StringRef S : Config->DynamicList)
505     if (SymbolBody *B = find(S))
506       B->symbol()->ExportDynamic = true;
507 }
508 
509 // This function processes the --version-script option by marking all global
510 // symbols with the VersionScriptGlobal flag, which acts as a filter on the
511 // dynamic symbol table.
512 template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() {
513   for (StringRef S : Config->VersionScriptGlobals)
514     if (SymbolBody *B = find(S))
515       B->symbol()->VersionScriptGlobal = true;
516 }
517 
518 template class elf::SymbolTable<ELF32LE>;
519 template class elf::SymbolTable<ELF32BE>;
520 template class elf::SymbolTable<ELF64LE>;
521 template class elf::SymbolTable<ELF64BE>;
522