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