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