1 //===- SymbolTable.cpp ----------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
17 #include "SymbolTable.h"
20 #include "LinkerScript.h"
23 #include "llvm/ADT/STLExtras.h"
26 using namespace llvm::object;
27 using namespace llvm::ELF;
30 using namespace lld::elf;
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))
39 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) {
40 if (Config->EMachine != EM_MIPS)
42 if (isMipsN32Abi(F) == Config->MipsN32Abi)
46 if (!Config->Emulation.empty())
47 error(toString(F) + " is incompatible with " + Config->Emulation);
49 error(toString(F) + " is incompatible with " + toString(Config->FirstElf));
53 // Add symbols in File to the symbol table.
54 template <class ELFT> void SymbolTable<ELFT>::addFile(InputFile *File) {
55 if (!isCompatible<ELFT>(File))
59 if (auto *F = dyn_cast<BinaryFile>(File)) {
60 BinaryFiles.push_back(F);
66 if (auto *F = dyn_cast<ArchiveFile>(File)) {
72 if (auto *F = dyn_cast<LazyObjectFile>(File)) {
78 message(toString(File));
81 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
82 // DSOs are uniquified not by filename but by soname.
84 if (ErrorCount || !SoNames.insert(F->getSoName()).second)
86 SharedFiles.push_back(F);
92 if (auto *F = dyn_cast<BitcodeFile>(File)) {
93 BitcodeFiles.push_back(F);
94 F->parse<ELFT>(ComdatGroups);
98 // Regular object file
99 auto *F = cast<ObjectFile<ELFT>>(File);
100 ObjectFiles.push_back(F);
101 F->parse(ComdatGroups);
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())
115 // Compile bitcode files and replace bitcode symbols.
116 LTO.reset(new BitcodeCompiler);
117 for (BitcodeFile *F : BitcodeFiles)
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);
128 template <class ELFT>
129 DefinedRegular *SymbolTable<ELFT>::addAbsolute(StringRef Name,
133 addRegular(Name, Visibility, STT_NOTYPE, 0, 0, Binding, nullptr, nullptr);
134 return cast<DefinedRegular>(Sym->body());
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())
145 return addAbsolute(Name, Visibility);
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}});
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);
160 Symbol *Sym = B->symbol();
161 Symbol *Real = addUndefined(Saver.save("__real_" + Name));
162 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
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));
171 static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) {
172 if (VA == STV_DEFAULT)
174 if (VB == STV_DEFAULT)
176 return std::min(VA, VB);
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;
189 V = SymIndex((int)SymVector.size(), true);
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);
204 Sym = SymVector[V.Idx];
209 // Find an existing symbol or create and insert a new one, then apply the given
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;
218 std::tie(S, WasInserted) = insert(Name);
220 // Merge in the new symbol's visibility.
221 S->Visibility = getMinVisibility(S->Visibility, Visibility);
223 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
224 S->ExportDynamic = true;
226 if (IsUsedInRegularObj)
227 S->IsUsedInRegularObj = true;
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));
236 return {S, WasInserted};
239 template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
240 return addUndefined(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT,
242 /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
245 static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; }
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,
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
260 (isa<SharedSymbol>(S->body()) && Visibility != STV_DEFAULT)) {
261 S->Binding = Binding;
262 replaceBody<Undefined>(S, Name, IsLocal, StOther, Type, File);
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;
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.
276 else if (InputFile *F = L->fetch())
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) {
288 SymbolBody *Body = S->body();
289 if (Body->isLazy() || !Body->isInCurrentDSO())
291 if (Binding == STB_WEAK)
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)) {
306 S->Binding = Binding;
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");
315 } else if (auto *R = dyn_cast<DefinedRegular>(B)) {
316 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute &&
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,
330 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther),
331 /*CanOmitFromDynSym*/ false, File);
332 int Cmp = compareDefined(S, WasInserted, Binding);
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());
339 // Non-common symbols take precedence over common symbols.
340 if (Config->WarnCommon)
341 warn("common " + S->body()->getName() + " is overridden");
345 if (Config->WarnCommon)
346 warn("multiple common of " + S->body()->getName());
348 Alignment = C->Alignment = std::max(C->Alignment, Alignment);
350 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
355 static void warnOrError(const Twine &Msg) {
356 if (Config->AllowMultipleDefinition)
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));
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);
377 // Construct and print an error message in the form of:
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);
390 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at ";
392 Msg += Src1 + "\n>>> ";
393 Msg += Obj1 + "\n>>> defined at ";
395 Msg += Src2 + "\n>>> ";
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) {
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);
412 replaceBody<DefinedRegular>(S, Name, /*IsLocal=*/false, StOther, Type,
413 Value, Size, Section, File);
415 reportDuplicate<ELFT>(S->body(),
416 dyn_cast_or_null<InputSectionBase>(Section), Value);
420 template <typename ELFT>
421 void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *File, StringRef Name,
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
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;
435 SymbolBody *Body = S->body();
436 // An undefined symbol with non default visibility must be satisfied
439 (isa<Undefined>(Body) && Body->getVisibility() == STV_DEFAULT)) {
440 replaceBody<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(), &Sym,
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) {
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);
458 replaceBody<DefinedRegular>(S, Name, /*IsLocal=*/false, StOther, Type, 0, 0,
461 reportDuplicate(S->body(), F);
465 template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) {
466 auto It = Symtab.find(CachedHashStringRef(Name));
467 if (It == Symtab.end())
469 SymIndex V = It->second;
472 return SymVector[V.Idx]->body();
475 template <class ELFT>
476 SymbolBody *SymbolTable<ELFT>::findInCurrentDSO(StringRef Name) {
477 if (SymbolBody *S = find(Name))
478 if (S->isInCurrentDSO())
483 template <class ELFT>
484 void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
485 const object::Archive::Symbol Sym) {
488 StringRef Name = Sym.getName();
489 std::tie(S, WasInserted) = insert(Name);
491 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType);
494 if (!S->body()->isUndefined())
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.
505 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type);
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));
513 template <class ELFT>
514 void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
517 std::tie(S, WasInserted) = insert(Name);
519 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType);
522 if (!S->body()->isUndefined())
525 // See comment for addLazyArchive above.
527 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type);
529 MemoryBufferRef MBRef = Obj.getBuffer();
530 if (!MBRef.getBuffer().empty())
531 addFile(createObjectFile(MBRef));
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())
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;
558 // Initialize DemangledSyms with a map from demangled symbols to symbol
559 // objects. Used to handle "extern C++" directive in version scripts.
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.
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
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())
579 if (Optional<std::string> S = demangle(B->getName()))
580 (*DemangledSyms)[*S].push_back(B);
582 (*DemangledSyms)[B->getName()].push_back(B);
585 return *DemangledSyms;
588 template <class ELFT>
589 std::vector<SymbolBody *> SymbolTable<ELFT>::findByVersion(SymbolVersion Ver) {
591 return getDemangledSyms().lookup(Ver.Name);
592 if (SymbolBody *B = find(Ver.Name))
593 if (!B->isUndefined())
598 template <class ELFT>
599 std::vector<SymbolBody *>
600 SymbolTable<ELFT>::findAllByVersion(SymbolVersion Ver) {
601 std::vector<SymbolBody *> Res;
602 StringMatcher M(Ver.Name);
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());
611 for (Symbol *Sym : SymVector) {
612 SymbolBody *B = Sym->body();
613 if (!B->isUndefined() && M.match(B->getName()))
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);
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) {
641 // Get a list of symbols which we need to assign the version to.
642 std::vector<SymbolBody *> Syms = findByVersion(Ver);
644 if (Config->NoUndefinedVersion)
645 error("version script assignment of '" + VersionName + "' to symbol '" +
646 Ver.Name + "' failed: symbol not defined");
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;
660 template <class ELFT>
661 void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver,
662 uint16_t VersionId) {
663 if (!Ver.HasWildcard)
665 std::vector<SymbolBody *> Syms = findAllByVersion(Ver);
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;
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();
685 // Handle edge cases first.
686 handleAnonymousVersion();
688 if (Config->VersionDefinitions.empty())
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.
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);
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);
710 template class elf::SymbolTable<ELF32LE>;
711 template class elf::SymbolTable<ELF32BE>;
712 template class elf::SymbolTable<ELF64LE>;
713 template class elf::SymbolTable<ELF64BE>;