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