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