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 "LinkerScript.h"
20 #include "Memory.h"
21 #include "Symbols.h"
22 #include "lld/Common/ErrorHandler.h"
23 #include "llvm/ADT/STLExtras.h"
24 
25 using namespace llvm;
26 using namespace llvm::object;
27 using namespace llvm::ELF;
28 
29 using namespace lld;
30 using namespace lld::elf;
31 
32 SymbolTable *elf::Symtab;
33 
34 static InputFile *getFirstElf() {
35   if (!ObjectFiles.empty())
36     return ObjectFiles[0];
37   if (!SharedFiles.empty())
38     return SharedFiles[0];
39   return nullptr;
40 }
41 
42 // All input object files must be for the same architecture
43 // (e.g. it does not make sense to link x86 object files with
44 // MIPS object files.) This function checks for that error.
45 template <class ELFT> static bool isCompatible(InputFile *F) {
46   if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F))
47     return true;
48 
49   if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
50     if (Config->EMachine != EM_MIPS)
51       return true;
52     if (isMipsN32Abi(F) == Config->MipsN32Abi)
53       return true;
54   }
55 
56   if (!Config->Emulation.empty())
57     error(toString(F) + " is incompatible with " + Config->Emulation);
58   else
59     error(toString(F) + " is incompatible with " + toString(getFirstElf()));
60   return false;
61 }
62 
63 // Add symbols in File to the symbol table.
64 template <class ELFT> void SymbolTable::addFile(InputFile *File) {
65   if (!isCompatible<ELFT>(File))
66     return;
67 
68   // Binary file
69   if (auto *F = dyn_cast<BinaryFile>(File)) {
70     BinaryFiles.push_back(F);
71     F->parse<ELFT>();
72     return;
73   }
74 
75   // .a file
76   if (auto *F = dyn_cast<ArchiveFile>(File)) {
77     F->parse<ELFT>();
78     return;
79   }
80 
81   // Lazy object file
82   if (auto *F = dyn_cast<LazyObjFile>(File)) {
83     F->parse<ELFT>();
84     return;
85   }
86 
87   if (Config->Trace)
88     message(toString(File));
89 
90   // .so file
91   if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
92     // DSOs are uniquified not by filename but by soname.
93     F->parseSoName();
94     if (errorCount() || !SoNames.insert(F->SoName).second)
95       return;
96     SharedFiles.push_back(F);
97     F->parseRest();
98     return;
99   }
100 
101   // LLVM bitcode file
102   if (auto *F = dyn_cast<BitcodeFile>(File)) {
103     BitcodeFiles.push_back(F);
104     F->parse<ELFT>(ComdatGroups);
105     return;
106   }
107 
108   // Regular object file
109   ObjectFiles.push_back(File);
110   cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
111 }
112 
113 // This function is where all the optimizations of link-time
114 // optimization happens. When LTO is in use, some input files are
115 // not in native object file format but in the LLVM bitcode format.
116 // This function compiles bitcode files into a few big native files
117 // using LLVM functions and replaces bitcode symbols with the results.
118 // Because all bitcode files that consist of a program are passed
119 // to the compiler at once, it can do whole-program optimization.
120 template <class ELFT> void SymbolTable::addCombinedLTOObject() {
121   if (BitcodeFiles.empty())
122     return;
123 
124   // Compile bitcode files and replace bitcode symbols.
125   LTO.reset(new BitcodeCompiler);
126   for (BitcodeFile *F : BitcodeFiles)
127     LTO->add(*F);
128 
129   for (InputFile *File : LTO->compile()) {
130     DenseSet<CachedHashStringRef> DummyGroups;
131     cast<ObjFile<ELFT>>(File)->parse(DummyGroups);
132     ObjectFiles.push_back(File);
133   }
134 }
135 
136 template <class ELFT>
137 DefinedRegular *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility,
138                                          uint8_t Binding) {
139   SymbolBody *Sym = addRegular<ELFT>(Name, Visibility, STT_NOTYPE, 0, 0,
140                                      Binding, nullptr, nullptr);
141   return cast<DefinedRegular>(Sym);
142 }
143 
144 // Set a flag for --trace-symbol so that we can print out a log message
145 // if a new symbol with the same name is inserted into the symbol table.
146 void SymbolTable::trace(StringRef Name) {
147   Symtab.insert({CachedHashStringRef(Name), {-1, true}});
148 }
149 
150 // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM.
151 // Used to implement --wrap.
152 template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) {
153   SymbolBody *Sym = find(Name);
154   if (!Sym)
155     return;
156   SymbolBody *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
157   SymbolBody *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
158 
159   defsym(Real, Sym);
160   defsym(Sym, Wrap);
161 
162   WrapSymbols.push_back({Wrap, Real});
163 }
164 
165 // Creates alias for symbol. Used to implement --defsym=ALIAS=SYM.
166 template <class ELFT>
167 void SymbolTable::addSymbolAlias(StringRef Alias, StringRef Name) {
168   SymbolBody *B = find(Name);
169   if (!B) {
170     error("-defsym: undefined symbol: " + Name);
171     return;
172   }
173 
174   defsym(addUndefined<ELFT>(Alias), B);
175 }
176 
177 // Apply symbol renames created by -wrap and -defsym. The renames are created
178 // before LTO in addSymbolWrap() and addSymbolAlias() to have a chance to inform
179 // LTO (if LTO is running) not to include these symbols in IPO. Now that the
180 // symbols are finalized, we can perform the replacement.
181 void SymbolTable::applySymbolRenames() {
182   // This function rotates 3 symbols:
183   //
184   // __real_foo becomes foo
185   // foo        becomes __wrap_foo
186   // __wrap_foo becomes __real_foo
187   //
188   // The last part is special in that we don't want to change what references to
189   // __wrap_foo point to, we just want have __real_foo in the symbol table.
190 
191   // First make a copy of __real_foo
192   std::vector<SymbolUnion> Origs;
193   for (const auto &P : WrapSymbols)
194     Origs.emplace_back(*(SymbolUnion *)P.second);
195 
196   // Replace __real_foo with foo and foo with __wrap_foo
197   for (SymbolRenaming &S : Defsyms) {
198     S.Dst->copyFrom(S.Src);
199     S.Dst->File = S.Src->File;
200     S.Dst->Binding = S.Binding;
201   }
202 
203   // Hide one of the copies of __wrap_foo, create a new symbol and copy
204   // __real_foo into it.
205   for (unsigned I = 0, N = WrapSymbols.size(); I < N; ++I) {
206     // We now have two copies of __wrap_foo. Drop one.
207     SymbolBody *Wrap = WrapSymbols[I].first;
208     Wrap->IsUsedInRegularObj = false;
209 
210     auto *Real = (SymbolBody *)&Origs[I];
211     // If __real_foo was undefined, we don't want it in the symbol table.
212     if (!Real->isInCurrentOutput())
213       continue;
214 
215     auto *NewSym = (SymbolBody *)make<SymbolUnion>();
216     memcpy(NewSym, Real, sizeof(SymbolUnion));
217     SymVector.push_back(NewSym);
218   }
219 }
220 
221 static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
222   if (VA == STV_DEFAULT)
223     return VB;
224   if (VB == STV_DEFAULT)
225     return VA;
226   return std::min(VA, VB);
227 }
228 
229 // Find an existing symbol or create and insert a new one.
230 std::pair<SymbolBody *, bool> SymbolTable::insert(StringRef Name) {
231   // <name>@@<version> means the symbol is the default version. In that
232   // case <name>@@<version> will be used to resolve references to <name>.
233   //
234   // Since this is a hot path, the following string search code is
235   // optimized for speed. StringRef::find(char) is much faster than
236   // StringRef::find(StringRef).
237   size_t Pos = Name.find('@');
238   if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@')
239     Name = Name.take_front(Pos);
240 
241   auto P = Symtab.insert(
242       {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)});
243   SymIndex &V = P.first->second;
244   bool IsNew = P.second;
245 
246   if (V.Idx == -1) {
247     IsNew = true;
248     V = SymIndex((int)SymVector.size(), true);
249   }
250 
251   SymbolBody *Sym;
252   if (IsNew) {
253     Sym = (SymbolBody *)make<SymbolUnion>();
254     Sym->InVersionScript = false;
255     Sym->Binding = STB_WEAK;
256     Sym->Visibility = STV_DEFAULT;
257     Sym->IsUsedInRegularObj = false;
258     Sym->ExportDynamic = false;
259     Sym->CanInline = true;
260     Sym->Traced = V.Traced;
261     Sym->VersionId = Config->DefaultSymbolVersion;
262     SymVector.push_back(Sym);
263   } else {
264     Sym = SymVector[V.Idx];
265   }
266   return {Sym, IsNew};
267 }
268 
269 // Find an existing symbol or create and insert a new one, then apply the given
270 // attributes.
271 std::pair<SymbolBody *, bool> SymbolTable::insert(StringRef Name, uint8_t Type,
272                                                   uint8_t Visibility,
273                                                   bool CanOmitFromDynSym,
274                                                   InputFile *File) {
275   SymbolBody *S;
276   bool WasInserted;
277   std::tie(S, WasInserted) = insert(Name);
278 
279   // Merge in the new symbol's visibility.
280   S->Visibility = getMinVisibility(S->Visibility, Visibility);
281 
282   if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
283     S->ExportDynamic = true;
284 
285   if (!File || File->kind() == InputFile::ObjKind)
286     S->IsUsedInRegularObj = true;
287 
288   if (!WasInserted && S->Type != SymbolBody::UnknownType &&
289       ((Type == STT_TLS) != S->isTls())) {
290     error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " +
291           toString(S->File) + "\n>>> defined in " + toString(File));
292   }
293 
294   return {S, WasInserted};
295 }
296 
297 template <class ELFT> SymbolBody *SymbolTable::addUndefined(StringRef Name) {
298   return addUndefined<ELFT>(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT,
299                             /*Type*/ 0,
300                             /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
301 }
302 
303 static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
304 
305 template <class ELFT>
306 SymbolBody *SymbolTable::addUndefined(StringRef Name, bool IsLocal,
307                                       uint8_t Binding, uint8_t StOther,
308                                       uint8_t Type, bool CanOmitFromDynSym,
309                                       InputFile *File) {
310   SymbolBody *S;
311   bool WasInserted;
312   uint8_t Visibility = getVisibility(StOther);
313   std::tie(S, WasInserted) =
314       insert(Name, Type, Visibility, CanOmitFromDynSym, File);
315   // An undefined symbol with non default visibility must be satisfied
316   // in the same DSO.
317   if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) {
318     S->Binding = Binding;
319     replaceBody<Undefined>(S, File, Name, IsLocal, StOther, Type);
320     return S;
321   }
322   if (Binding != STB_WEAK) {
323     if (!S->isInCurrentOutput())
324       S->Binding = Binding;
325     if (auto *SS = dyn_cast<SharedSymbol>(S))
326       SS->getFile<ELFT>()->IsUsed = true;
327   }
328   if (auto *L = dyn_cast<Lazy>(S)) {
329     // An undefined weak will not fetch archive members. See comment on Lazy in
330     // Symbols.h for the details.
331     if (S->isWeak())
332       L->Type = Type;
333     else if (InputFile *F = L->fetch())
334       addFile<ELFT>(F);
335   }
336   return S;
337 }
338 
339 // Using .symver foo,foo@@VER unfortunately creates two symbols: foo and
340 // foo@@VER. We want to effectively ignore foo, so give precedence to
341 // foo@@VER.
342 // FIXME: If users can transition to using
343 // .symver foo,foo@@@VER
344 // we can delete this hack.
345 static int compareVersion(SymbolBody *S, StringRef Name) {
346   bool A = Name.contains("@@");
347   bool B = S->getName().contains("@@");
348   if (A && !B)
349     return 1;
350   if (!A && B)
351     return -1;
352   return 0;
353 }
354 
355 // We have a new defined symbol with the specified binding. Return 1 if the new
356 // symbol should win, -1 if the new symbol should lose, or 0 if both symbols are
357 // strong defined symbols.
358 static int compareDefined(SymbolBody *S, bool WasInserted, uint8_t Binding,
359                           StringRef Name) {
360   if (WasInserted)
361     return 1;
362   if (!S->isInCurrentOutput())
363     return 1;
364   if (int R = compareVersion(S, Name))
365     return R;
366   if (Binding == STB_WEAK)
367     return -1;
368   if (S->isWeak())
369     return 1;
370   return 0;
371 }
372 
373 // We have a new non-common defined symbol with the specified binding. Return 1
374 // if the new symbol should win, -1 if the new symbol should lose, or 0 if there
375 // is a conflict. If the new symbol wins, also update the binding.
376 static int compareDefinedNonCommon(SymbolBody *S, bool WasInserted,
377                                    uint8_t Binding, bool IsAbsolute,
378                                    uint64_t Value, StringRef Name) {
379   if (int Cmp = compareDefined(S, WasInserted, Binding, Name)) {
380     if (Cmp > 0)
381       S->Binding = Binding;
382     return Cmp;
383   }
384   if (isa<DefinedCommon>(S)) {
385     // Non-common symbols take precedence over common symbols.
386     if (Config->WarnCommon)
387       warn("common " + S->getName() + " is overridden");
388     return 1;
389   }
390   if (auto *R = dyn_cast<DefinedRegular>(S)) {
391     if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
392         R->Value == Value)
393       return -1;
394   }
395   return 0;
396 }
397 
398 SymbolBody *SymbolTable::addCommon(StringRef N, uint64_t Size,
399                                    uint32_t Alignment, uint8_t Binding,
400                                    uint8_t StOther, uint8_t Type,
401                                    InputFile *File) {
402   SymbolBody *S;
403   bool WasInserted;
404   std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
405                                     /*CanOmitFromDynSym*/ false, File);
406   int Cmp = compareDefined(S, WasInserted, Binding, N);
407   if (Cmp > 0) {
408     S->Binding = Binding;
409     replaceBody<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type);
410   } else if (Cmp == 0) {
411     auto *C = dyn_cast<DefinedCommon>(S);
412     if (!C) {
413       // Non-common symbols take precedence over common symbols.
414       if (Config->WarnCommon)
415         warn("common " + S->getName() + " is overridden");
416       return S;
417     }
418 
419     if (Config->WarnCommon)
420       warn("multiple common of " + S->getName());
421 
422     Alignment = C->Alignment = std::max(C->Alignment, Alignment);
423     if (Size > C->Size)
424       replaceBody<DefinedCommon>(S, File, N, Size, Alignment, StOther, Type);
425   }
426   return S;
427 }
428 
429 static void warnOrError(const Twine &Msg) {
430   if (Config->AllowMultipleDefinition)
431     warn(Msg);
432   else
433     error(Msg);
434 }
435 
436 static void reportDuplicate(SymbolBody *Sym, InputFile *NewFile) {
437   warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " +
438               toString(Sym->getFile()) + "\n>>> defined in " +
439               toString(NewFile));
440 }
441 
442 template <class ELFT>
443 static void reportDuplicate(SymbolBody *Sym, InputSectionBase *ErrSec,
444                             typename ELFT::uint ErrOffset) {
445   DefinedRegular *D = dyn_cast<DefinedRegular>(Sym);
446   if (!D || !D->Section || !ErrSec) {
447     reportDuplicate(Sym, ErrSec ? ErrSec->File : nullptr);
448     return;
449   }
450 
451   // Construct and print an error message in the form of:
452   //
453   //   ld.lld: error: duplicate symbol: foo
454   //   >>> defined at bar.c:30
455   //   >>>            bar.o (/home/alice/src/bar.o)
456   //   >>> defined at baz.c:563
457   //   >>>            baz.o in archive libbaz.a
458   auto *Sec1 = cast<InputSectionBase>(D->Section);
459   std::string Src1 = Sec1->getSrcMsg<ELFT>(*Sym, D->Value);
460   std::string Obj1 = Sec1->getObjMsg(D->Value);
461   std::string Src2 = ErrSec->getSrcMsg<ELFT>(*Sym, ErrOffset);
462   std::string Obj2 = ErrSec->getObjMsg(ErrOffset);
463 
464   std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
465   if (!Src1.empty())
466     Msg += Src1 + "\n>>>            ";
467   Msg += Obj1 + "\n>>> defined at ";
468   if (!Src2.empty())
469     Msg += Src2 + "\n>>>            ";
470   Msg += Obj2;
471   warnOrError(Msg);
472 }
473 
474 template <typename ELFT>
475 SymbolBody *SymbolTable::addRegular(StringRef Name, uint8_t StOther,
476                                     uint8_t Type, uint64_t Value, uint64_t Size,
477                                     uint8_t Binding, SectionBase *Section,
478                                     InputFile *File) {
479   SymbolBody *S;
480   bool WasInserted;
481   std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther),
482                                     /*CanOmitFromDynSym*/ false, File);
483   int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr,
484                                     Value, Name);
485   if (Cmp > 0)
486     replaceBody<DefinedRegular>(S, File, Name, /*IsLocal=*/false, StOther, Type,
487                                 Value, Size, Section);
488   else if (Cmp == 0)
489     reportDuplicate<ELFT>(S, dyn_cast_or_null<InputSectionBase>(Section),
490                           Value);
491   return S;
492 }
493 
494 template <typename ELFT>
495 void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> *File,
496                             const typename ELFT::Sym &Sym, uint32_t Alignment,
497                             const typename ELFT::Verdef *Verdef) {
498   // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT
499   // as the visibility, which will leave the visibility in the symbol table
500   // unchanged.
501   SymbolBody *S;
502   bool WasInserted;
503   std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT,
504                                     /*CanOmitFromDynSym*/ true, File);
505   // Make sure we preempt DSO symbols with default visibility.
506   if (Sym.getVisibility() == STV_DEFAULT)
507     S->ExportDynamic = true;
508 
509   // An undefined symbol with non default visibility must be satisfied
510   // in the same DSO.
511   if (WasInserted || ((S->isUndefined() || S->isLazy()) &&
512                       S->getVisibility() == STV_DEFAULT)) {
513     replaceBody<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(),
514                               Sym.st_value, Sym.st_size, Alignment, Verdef);
515     if (!S->isWeak())
516       File->IsUsed = true;
517   }
518 }
519 
520 SymbolBody *SymbolTable::addBitcode(StringRef Name, uint8_t Binding,
521                                     uint8_t StOther, uint8_t Type,
522                                     bool CanOmitFromDynSym, BitcodeFile *F) {
523   SymbolBody *S;
524   bool WasInserted;
525   std::tie(S, WasInserted) =
526       insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F);
527   int Cmp = compareDefinedNonCommon(S, WasInserted, Binding,
528                                     /*IsAbs*/ false, /*Value*/ 0, Name);
529   if (Cmp > 0)
530     replaceBody<DefinedRegular>(S, F, Name, /*IsLocal=*/false, StOther, Type, 0,
531                                 0, nullptr);
532   else if (Cmp == 0)
533     reportDuplicate(S, F);
534   return S;
535 }
536 
537 SymbolBody *SymbolTable::find(StringRef Name) {
538   auto It = Symtab.find(CachedHashStringRef(Name));
539   if (It == Symtab.end())
540     return nullptr;
541   SymIndex V = It->second;
542   if (V.Idx == -1)
543     return nullptr;
544   return SymVector[V.Idx];
545 }
546 
547 void SymbolTable::defsym(SymbolBody *Dst, SymbolBody *Src) {
548   // We want to tell LTO not to inline Dst symbol because LTO doesn't
549   // know the final symbol contents after renaming.
550   Dst->CanInline = false;
551 
552   // Tell LTO not to eliminate this symbol.
553   Src->IsUsedInRegularObj = true;
554 
555   Defsyms.push_back({Dst, Src, Dst->Binding});
556 }
557 
558 template <class ELFT>
559 SymbolBody *SymbolTable::addLazyArchive(StringRef Name, ArchiveFile *F,
560                                         const object::Archive::Symbol Sym) {
561   SymbolBody *S;
562   bool WasInserted;
563   std::tie(S, WasInserted) = insert(Name);
564   if (WasInserted) {
565     replaceBody<LazyArchive>(S, F, Sym, SymbolBody::UnknownType);
566     return S;
567   }
568   if (!S->isUndefined())
569     return S;
570 
571   // An undefined weak will not fetch archive members. See comment on Lazy in
572   // Symbols.h for the details.
573   if (S->isWeak()) {
574     replaceBody<LazyArchive>(S, F, Sym, S->Type);
575     return S;
576   }
577   std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
578   if (!MBInfo.first.getBuffer().empty())
579     addFile<ELFT>(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
580   return S;
581 }
582 
583 template <class ELFT>
584 void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) {
585   SymbolBody *S;
586   bool WasInserted;
587   std::tie(S, WasInserted) = insert(Name);
588   if (WasInserted) {
589     replaceBody<LazyObject>(S, &Obj, Name, SymbolBody::UnknownType);
590     return;
591   }
592   if (!S->isUndefined())
593     return;
594 
595   // See comment for addLazyArchive above.
596   if (S->isWeak())
597     replaceBody<LazyObject>(S, &Obj, Name, S->Type);
598   else if (InputFile *F = Obj.fetch())
599     addFile<ELFT>(F);
600 }
601 
602 // If we already saw this symbol, force loading its file.
603 template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) {
604   if (SymbolBody *B = find(Name)) {
605     // Mark the symbol not to be eliminated by LTO
606     // even if it is a bitcode symbol.
607     B->IsUsedInRegularObj = true;
608     if (auto *L = dyn_cast_or_null<Lazy>(B))
609       if (InputFile *File = L->fetch())
610         addFile<ELFT>(File);
611   }
612 }
613 
614 // This function takes care of the case in which shared libraries depend on
615 // the user program (not the other way, which is usual). Shared libraries
616 // may have undefined symbols, expecting that the user program provides
617 // the definitions for them. An example is BSD's __progname symbol.
618 // We need to put such symbols to the main program's .dynsym so that
619 // shared libraries can find them.
620 // Except this, we ignore undefined symbols in DSOs.
621 template <class ELFT> void SymbolTable::scanShlibUndefined() {
622   for (InputFile *F : SharedFiles) {
623     for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) {
624       SymbolBody *Sym = find(U);
625       if (!Sym || !Sym->isDefined())
626         continue;
627       Sym->ExportDynamic = true;
628 
629       // If -dynamic-list is given, the default version is set to
630       // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym.
631       // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were
632       // specified by -dynamic-list.
633       Sym->VersionId = VER_NDX_GLOBAL;
634     }
635   }
636 }
637 
638 // Initialize DemangledSyms with a map from demangled symbols to symbol
639 // objects. Used to handle "extern C++" directive in version scripts.
640 //
641 // The map will contain all demangled symbols. That can be very large,
642 // and in LLD we generally want to avoid do anything for each symbol.
643 // Then, why are we doing this? Here's why.
644 //
645 // Users can use "extern C++ {}" directive to match against demangled
646 // C++ symbols. For example, you can write a pattern such as
647 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this
648 // other than trying to match a pattern against all demangled symbols.
649 // So, if "extern C++" feature is used, we need to demangle all known
650 // symbols.
651 StringMap<std::vector<SymbolBody *>> &SymbolTable::getDemangledSyms() {
652   if (!DemangledSyms) {
653     DemangledSyms.emplace();
654     for (SymbolBody *Sym : SymVector) {
655       if (!Sym->isInCurrentOutput())
656         continue;
657       if (Optional<std::string> S = demangle(Sym->getName()))
658         (*DemangledSyms)[*S].push_back(Sym);
659       else
660         (*DemangledSyms)[Sym->getName()].push_back(Sym);
661     }
662   }
663   return *DemangledSyms;
664 }
665 
666 std::vector<SymbolBody *> SymbolTable::findByVersion(SymbolVersion Ver) {
667   if (Ver.IsExternCpp)
668     return getDemangledSyms().lookup(Ver.Name);
669   if (SymbolBody *B = find(Ver.Name))
670     if (B->isInCurrentOutput())
671       return {B};
672   return {};
673 }
674 
675 std::vector<SymbolBody *> SymbolTable::findAllByVersion(SymbolVersion Ver) {
676   std::vector<SymbolBody *> Res;
677   StringMatcher M(Ver.Name);
678 
679   if (Ver.IsExternCpp) {
680     for (auto &P : getDemangledSyms())
681       if (M.match(P.first()))
682         Res.insert(Res.end(), P.second.begin(), P.second.end());
683     return Res;
684   }
685 
686   for (SymbolBody *Sym : SymVector)
687     if (Sym->isInCurrentOutput() && M.match(Sym->getName()))
688       Res.push_back(Sym);
689   return Res;
690 }
691 
692 // If there's only one anonymous version definition in a version
693 // script file, the script does not actually define any symbol version,
694 // but just specifies symbols visibilities.
695 void SymbolTable::handleAnonymousVersion() {
696   for (SymbolVersion &Ver : Config->VersionScriptGlobals)
697     assignExactVersion(Ver, VER_NDX_GLOBAL, "global");
698   for (SymbolVersion &Ver : Config->VersionScriptGlobals)
699     assignWildcardVersion(Ver, VER_NDX_GLOBAL);
700   for (SymbolVersion &Ver : Config->VersionScriptLocals)
701     assignExactVersion(Ver, VER_NDX_LOCAL, "local");
702   for (SymbolVersion &Ver : Config->VersionScriptLocals)
703     assignWildcardVersion(Ver, VER_NDX_LOCAL);
704 }
705 
706 // Handles -dynamic-list.
707 void SymbolTable::handleDynamicList() {
708   for (SymbolVersion &Ver : Config->DynamicList) {
709     std::vector<SymbolBody *> Syms;
710     if (Ver.HasWildcard)
711       Syms = findByVersion(Ver);
712     else
713       Syms = findAllByVersion(Ver);
714 
715     for (SymbolBody *B : Syms) {
716       if (!Config->Shared)
717         B->ExportDynamic = true;
718       else if (B->includeInDynsym())
719         B->IsPreemptible = true;
720     }
721   }
722 }
723 
724 // Set symbol versions to symbols. This function handles patterns
725 // containing no wildcard characters.
726 void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId,
727                                      StringRef VersionName) {
728   if (Ver.HasWildcard)
729     return;
730 
731   // Get a list of symbols which we need to assign the version to.
732   std::vector<SymbolBody *> Syms = findByVersion(Ver);
733   if (Syms.empty()) {
734     if (Config->NoUndefinedVersion)
735       error("version script assignment of '" + VersionName + "' to symbol '" +
736             Ver.Name + "' failed: symbol not defined");
737     return;
738   }
739 
740   // Assign the version.
741   for (SymbolBody *Sym : Syms) {
742     // Skip symbols containing version info because symbol versions
743     // specified by symbol names take precedence over version scripts.
744     // See parseSymbolVersion().
745     if (Sym->getName().contains('@'))
746       continue;
747 
748     if (Sym->InVersionScript)
749       warn("duplicate symbol '" + Ver.Name + "' in version script");
750     Sym->VersionId = VersionId;
751     Sym->InVersionScript = true;
752   }
753 }
754 
755 void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) {
756   if (!Ver.HasWildcard)
757     return;
758 
759   // Exact matching takes precendence over fuzzy matching,
760   // so we set a version to a symbol only if no version has been assigned
761   // to the symbol. This behavior is compatible with GNU.
762   for (SymbolBody *B : findAllByVersion(Ver))
763     if (B->VersionId == Config->DefaultSymbolVersion)
764       B->VersionId = VersionId;
765 }
766 
767 // This function processes version scripts by updating VersionId
768 // member of symbols.
769 void SymbolTable::scanVersionScript() {
770   // Handle edge cases first.
771   handleAnonymousVersion();
772   handleDynamicList();
773 
774   // Now we have version definitions, so we need to set version ids to symbols.
775   // Each version definition has a glob pattern, and all symbols that match
776   // with the pattern get that version.
777 
778   // First, we assign versions to exact matching symbols,
779   // i.e. version definitions not containing any glob meta-characters.
780   for (VersionDefinition &V : Config->VersionDefinitions)
781     for (SymbolVersion &Ver : V.Globals)
782       assignExactVersion(Ver, V.Id, V.Name);
783 
784   // Next, we assign versions to fuzzy matching symbols,
785   // i.e. version definitions containing glob meta-characters.
786   // Note that because the last match takes precedence over previous matches,
787   // we iterate over the definitions in the reverse order.
788   for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions))
789     for (SymbolVersion &Ver : V.Globals)
790       assignWildcardVersion(Ver, V.Id);
791 
792   // Symbol themselves might know their versions because symbols
793   // can contain versions in the form of <name>@<version>.
794   // Let them parse and update their names to exclude version suffix.
795   for (SymbolBody *Sym : SymVector)
796     Sym->parseSymbolVersion();
797 }
798 
799 template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef);
800 template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef);
801 template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef);
802 template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef);
803 
804 template SymbolBody *SymbolTable::addUndefined<ELF32LE>(StringRef);
805 template SymbolBody *SymbolTable::addUndefined<ELF32BE>(StringRef);
806 template SymbolBody *SymbolTable::addUndefined<ELF64LE>(StringRef);
807 template SymbolBody *SymbolTable::addUndefined<ELF64BE>(StringRef);
808 
809 template SymbolBody *SymbolTable::addUndefined<ELF32LE>(StringRef, bool,
810                                                         uint8_t, uint8_t,
811                                                         uint8_t, bool,
812                                                         InputFile *);
813 template SymbolBody *SymbolTable::addUndefined<ELF32BE>(StringRef, bool,
814                                                         uint8_t, uint8_t,
815                                                         uint8_t, bool,
816                                                         InputFile *);
817 template SymbolBody *SymbolTable::addUndefined<ELF64LE>(StringRef, bool,
818                                                         uint8_t, uint8_t,
819                                                         uint8_t, bool,
820                                                         InputFile *);
821 template SymbolBody *SymbolTable::addUndefined<ELF64BE>(StringRef, bool,
822                                                         uint8_t, uint8_t,
823                                                         uint8_t, bool,
824                                                         InputFile *);
825 
826 template void SymbolTable::addSymbolAlias<ELF32LE>(StringRef, StringRef);
827 template void SymbolTable::addSymbolAlias<ELF32BE>(StringRef, StringRef);
828 template void SymbolTable::addSymbolAlias<ELF64LE>(StringRef, StringRef);
829 template void SymbolTable::addSymbolAlias<ELF64BE>(StringRef, StringRef);
830 
831 template void SymbolTable::addCombinedLTOObject<ELF32LE>();
832 template void SymbolTable::addCombinedLTOObject<ELF32BE>();
833 template void SymbolTable::addCombinedLTOObject<ELF64LE>();
834 template void SymbolTable::addCombinedLTOObject<ELF64BE>();
835 
836 template SymbolBody *
837 SymbolTable::addRegular<ELF32LE>(StringRef, uint8_t, uint8_t, uint64_t,
838                                  uint64_t, uint8_t, SectionBase *, InputFile *);
839 template SymbolBody *
840 SymbolTable::addRegular<ELF32BE>(StringRef, uint8_t, uint8_t, uint64_t,
841                                  uint64_t, uint8_t, SectionBase *, InputFile *);
842 template SymbolBody *
843 SymbolTable::addRegular<ELF64LE>(StringRef, uint8_t, uint8_t, uint64_t,
844                                  uint64_t, uint8_t, SectionBase *, InputFile *);
845 template SymbolBody *
846 SymbolTable::addRegular<ELF64BE>(StringRef, uint8_t, uint8_t, uint64_t,
847                                  uint64_t, uint8_t, SectionBase *, InputFile *);
848 
849 template DefinedRegular *SymbolTable::addAbsolute<ELF32LE>(StringRef, uint8_t,
850                                                            uint8_t);
851 template DefinedRegular *SymbolTable::addAbsolute<ELF32BE>(StringRef, uint8_t,
852                                                            uint8_t);
853 template DefinedRegular *SymbolTable::addAbsolute<ELF64LE>(StringRef, uint8_t,
854                                                            uint8_t);
855 template DefinedRegular *SymbolTable::addAbsolute<ELF64BE>(StringRef, uint8_t,
856                                                            uint8_t);
857 
858 template SymbolBody *
859 SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile *,
860                                      const object::Archive::Symbol);
861 template SymbolBody *
862 SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile *,
863                                      const object::Archive::Symbol);
864 template SymbolBody *
865 SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile *,
866                                      const object::Archive::Symbol);
867 template SymbolBody *
868 SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile *,
869                                      const object::Archive::Symbol);
870 
871 template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &);
872 template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &);
873 template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &);
874 template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &);
875 
876 template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> *,
877                                               const typename ELF32LE::Sym &,
878                                               uint32_t Alignment,
879                                               const typename ELF32LE::Verdef *);
880 template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> *,
881                                               const typename ELF32BE::Sym &,
882                                               uint32_t Alignment,
883                                               const typename ELF32BE::Verdef *);
884 template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> *,
885                                               const typename ELF64LE::Sym &,
886                                               uint32_t Alignment,
887                                               const typename ELF64LE::Verdef *);
888 template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> *,
889                                               const typename ELF64BE::Sym &,
890                                               uint32_t Alignment,
891                                               const typename ELF64BE::Verdef *);
892 
893 template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef);
894 template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef);
895 template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef);
896 template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef);
897 
898 template void SymbolTable::scanShlibUndefined<ELF32LE>();
899 template void SymbolTable::scanShlibUndefined<ELF32BE>();
900 template void SymbolTable::scanShlibUndefined<ELF64LE>();
901 template void SymbolTable::scanShlibUndefined<ELF64BE>();
902