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