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