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