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 "Memory.h" 21 #include "Symbols.h" 22 #include "SyntheticSections.h" 23 #include "lld/Common/ErrorHandler.h" 24 #include "llvm/ADT/STLExtras.h" 25 26 using namespace llvm; 27 using namespace llvm::object; 28 using namespace llvm::ELF; 29 30 using namespace lld; 31 using namespace lld::elf; 32 33 SymbolTable *elf::Symtab; 34 35 static InputFile *getFirstElf() { 36 if (!ObjectFiles.empty()) 37 return ObjectFiles[0]; 38 if (!SharedFiles.empty()) 39 return SharedFiles[0]; 40 return nullptr; 41 } 42 43 // All input object files must be for the same architecture 44 // (e.g. it does not make sense to link x86 object files with 45 // MIPS object files.) This function checks for that error. 46 template <class ELFT> static bool isCompatible(InputFile *F) { 47 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F)) 48 return true; 49 50 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) { 51 if (Config->EMachine != EM_MIPS) 52 return true; 53 if (isMipsN32Abi(F) == Config->MipsN32Abi) 54 return true; 55 } 56 57 if (!Config->Emulation.empty()) 58 error(toString(F) + " is incompatible with " + Config->Emulation); 59 else 60 error(toString(F) + " is incompatible with " + toString(getFirstElf())); 61 return false; 62 } 63 64 // Add symbols in File to the symbol table. 65 template <class ELFT> void SymbolTable::addFile(InputFile *File) { 66 if (!isCompatible<ELFT>(File)) 67 return; 68 69 // Binary file 70 if (auto *F = dyn_cast<BinaryFile>(File)) { 71 BinaryFiles.push_back(F); 72 F->parse<ELFT>(); 73 return; 74 } 75 76 // .a file 77 if (auto *F = dyn_cast<ArchiveFile>(File)) { 78 F->parse<ELFT>(); 79 return; 80 } 81 82 // Lazy object file 83 if (auto *F = dyn_cast<LazyObjFile>(File)) { 84 F->parse<ELFT>(); 85 return; 86 } 87 88 if (Config->Trace) 89 message(toString(File)); 90 91 // .so file 92 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) { 93 // DSOs are uniquified not by filename but by soname. 94 F->parseSoName(); 95 if (errorCount() || !SoNames.insert(F->SoName).second) 96 return; 97 SharedFiles.push_back(F); 98 F->parseRest(); 99 return; 100 } 101 102 // LLVM bitcode file 103 if (auto *F = dyn_cast<BitcodeFile>(File)) { 104 BitcodeFiles.push_back(F); 105 F->parse<ELFT>(ComdatGroups); 106 return; 107 } 108 109 // Regular object file 110 ObjectFiles.push_back(File); 111 cast<ObjFile<ELFT>>(File)->parse(ComdatGroups); 112 } 113 114 // This function is where all the optimizations of link-time 115 // optimization happens. When LTO is in use, some input files are 116 // not in native object file format but in the LLVM bitcode format. 117 // This function compiles bitcode files into a few big native files 118 // using LLVM functions and replaces bitcode symbols with the results. 119 // Because all bitcode files that consist of a program are passed 120 // to the compiler at once, it can do whole-program optimization. 121 template <class ELFT> void SymbolTable::addCombinedLTOObject() { 122 if (BitcodeFiles.empty()) 123 return; 124 125 // Compile bitcode files and replace bitcode symbols. 126 LTO.reset(new BitcodeCompiler); 127 for (BitcodeFile *F : BitcodeFiles) 128 LTO->add(*F); 129 130 for (InputFile *File : LTO->compile()) { 131 DenseSet<CachedHashStringRef> DummyGroups; 132 cast<ObjFile<ELFT>>(File)->parse(DummyGroups); 133 ObjectFiles.push_back(File); 134 } 135 } 136 137 template <class ELFT> 138 Defined *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility, 139 uint8_t Binding) { 140 Symbol *Sym = addRegular<ELFT>(Name, Visibility, STT_NOTYPE, 0, 0, Binding, 141 nullptr, nullptr); 142 return cast<Defined>(Sym); 143 } 144 145 // Set a flag for --trace-symbol so that we can print out a log message 146 // if a new symbol with the same name is inserted into the symbol table. 147 void SymbolTable::trace(StringRef Name) { 148 Symtab.insert({CachedHashStringRef(Name), -1}); 149 } 150 151 // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM. 152 // Used to implement --wrap. 153 template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) { 154 Symbol *Sym = find(Name); 155 if (!Sym) 156 return; 157 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name)); 158 Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name)); 159 WrappedSymbols.push_back({Sym, Real, Wrap}); 160 161 // We want to tell LTO not to inline symbols to be overwritten 162 // because LTO doesn't know the final symbol contents after renaming. 163 Real->CanInline = false; 164 Sym->CanInline = false; 165 166 // Tell LTO not to eliminate these symbols. 167 Sym->IsUsedInRegularObj = true; 168 Wrap->IsUsedInRegularObj = true; 169 } 170 171 // Apply symbol renames created by -wrap. The renames are created 172 // before LTO in addSymbolWrap() to have a chance to inform LTO (if 173 // LTO is running) not to include these symbols in IPO. Now that the 174 // symbols are finalized, we can perform the replacement. 175 void SymbolTable::applySymbolWrap() { 176 // This function rotates 3 symbols: 177 // 178 // __real_sym becomes sym 179 // sym becomes __wrap_sym 180 // __wrap_sym becomes __real_sym 181 // 182 // The last part is special in that we don't want to change what references to 183 // __wrap_sym point to, we just want have __real_sym in the symbol table. 184 185 for (WrappedSymbol &W : WrappedSymbols) { 186 // First, make a copy of __real_sym. 187 Symbol *Real = nullptr; 188 if (W.Real->isDefined()) { 189 Real = (Symbol *)make<SymbolUnion>(); 190 memcpy(Real, W.Real, sizeof(SymbolUnion)); 191 } 192 193 // Replace __real_sym with sym and sym with __wrap_sym. 194 W.Real->copyFrom(W.Sym); 195 W.Sym->copyFrom(W.Wrap); 196 197 // We now have two copies of __wrap_sym. Drop one. 198 W.Wrap->IsUsedInRegularObj = false; 199 200 if (Real) 201 SymVector.push_back(Real); 202 } 203 } 204 205 static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { 206 if (VA == STV_DEFAULT) 207 return VB; 208 if (VB == STV_DEFAULT) 209 return VA; 210 return std::min(VA, VB); 211 } 212 213 // Find an existing symbol or create and insert a new one. 214 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) { 215 // <name>@@<version> means the symbol is the default version. In that 216 // case <name>@@<version> will be used to resolve references to <name>. 217 // 218 // Since this is a hot path, the following string search code is 219 // optimized for speed. StringRef::find(char) is much faster than 220 // StringRef::find(StringRef). 221 size_t Pos = Name.find('@'); 222 if (Pos != StringRef::npos && Pos + 1 < Name.size() && Name[Pos + 1] == '@') 223 Name = Name.take_front(Pos); 224 225 auto P = Symtab.insert({CachedHashStringRef(Name), (int)SymVector.size()}); 226 int &SymIndex = P.first->second; 227 bool IsNew = P.second; 228 bool Traced = false; 229 230 if (SymIndex == -1) { 231 SymIndex = SymVector.size(); 232 IsNew = Traced = true; 233 } 234 235 Symbol *Sym; 236 if (IsNew) { 237 Sym = (Symbol *)make<SymbolUnion>(); 238 Sym->InVersionScript = false; 239 Sym->Binding = STB_WEAK; 240 Sym->Visibility = STV_DEFAULT; 241 Sym->IsUsedInRegularObj = false; 242 Sym->ExportDynamic = false; 243 Sym->CanInline = true; 244 Sym->Traced = Traced; 245 Sym->VersionId = Config->DefaultSymbolVersion; 246 SymVector.push_back(Sym); 247 } else { 248 Sym = SymVector[SymIndex]; 249 } 250 return {Sym, IsNew}; 251 } 252 253 // Find an existing symbol or create and insert a new one, then apply the given 254 // attributes. 255 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type, 256 uint8_t Visibility, 257 bool CanOmitFromDynSym, 258 InputFile *File) { 259 Symbol *S; 260 bool WasInserted; 261 std::tie(S, WasInserted) = insert(Name); 262 263 // Merge in the new symbol's visibility. 264 S->Visibility = getMinVisibility(S->Visibility, Visibility); 265 266 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic)) 267 S->ExportDynamic = true; 268 269 if (!File || File->kind() == InputFile::ObjKind) 270 S->IsUsedInRegularObj = true; 271 272 if (!WasInserted && S->Type != Symbol::UnknownType && 273 ((Type == STT_TLS) != S->isTls())) { 274 error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " + 275 toString(S->File) + "\n>>> defined in " + toString(File)); 276 } 277 278 return {S, WasInserted}; 279 } 280 281 template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) { 282 return addUndefined<ELFT>(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT, 283 /*Type*/ 0, 284 /*CanOmitFromDynSym*/ false, /*File*/ nullptr); 285 } 286 287 static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; } 288 289 template <class ELFT> 290 Symbol *SymbolTable::addUndefined(StringRef Name, bool IsLocal, uint8_t Binding, 291 uint8_t StOther, uint8_t Type, 292 bool CanOmitFromDynSym, InputFile *File) { 293 Symbol *S; 294 bool WasInserted; 295 uint8_t Visibility = getVisibility(StOther); 296 std::tie(S, WasInserted) = 297 insert(Name, Type, Visibility, CanOmitFromDynSym, File); 298 // An undefined symbol with non default visibility must be satisfied 299 // in the same DSO. 300 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) { 301 S->Binding = Binding; 302 replaceSymbol<Undefined>(S, File, Name, IsLocal, StOther, Type); 303 return S; 304 } 305 if (Binding != STB_WEAK) { 306 if (!S->isDefined()) 307 S->Binding = Binding; 308 if (auto *SS = dyn_cast<SharedSymbol>(S)) 309 SS->getFile<ELFT>()->IsUsed = true; 310 } 311 if (auto *L = dyn_cast<Lazy>(S)) { 312 // An undefined weak will not fetch archive members. See comment on Lazy in 313 // Symbols.h for the details. 314 if (S->isWeak()) 315 L->Type = Type; 316 else if (InputFile *F = L->fetch()) 317 addFile<ELFT>(F); 318 } 319 return S; 320 } 321 322 // Using .symver foo,foo@@VER unfortunately creates two symbols: foo and 323 // foo@@VER. We want to effectively ignore foo, so give precedence to 324 // foo@@VER. 325 // FIXME: If users can transition to using 326 // .symver foo,foo@@@VER 327 // we can delete this hack. 328 static int compareVersion(Symbol *S, StringRef Name) { 329 bool A = Name.contains("@@"); 330 bool B = S->getName().contains("@@"); 331 if (A && !B) 332 return 1; 333 if (!A && B) 334 return -1; 335 return 0; 336 } 337 338 // We have a new defined symbol with the specified binding. Return 1 if the new 339 // symbol should win, -1 if the new symbol should lose, or 0 if both symbols are 340 // strong defined symbols. 341 static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding, 342 StringRef Name) { 343 if (WasInserted) 344 return 1; 345 if (!S->isDefined()) 346 return 1; 347 if (int R = compareVersion(S, Name)) 348 return R; 349 if (Binding == STB_WEAK) 350 return -1; 351 if (S->isWeak()) 352 return 1; 353 return 0; 354 } 355 356 // We have a new non-common defined symbol with the specified binding. Return 1 357 // if the new symbol should win, -1 if the new symbol should lose, or 0 if there 358 // is a conflict. If the new symbol wins, also update the binding. 359 static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding, 360 bool IsAbsolute, uint64_t Value, 361 StringRef Name) { 362 if (int Cmp = compareDefined(S, WasInserted, Binding, Name)) { 363 if (Cmp > 0) 364 S->Binding = Binding; 365 return Cmp; 366 } 367 if (auto *R = dyn_cast<Defined>(S)) { 368 if (R->Section && isa<BssSection>(R->Section)) { 369 // Non-common symbols take precedence over common symbols. 370 if (Config->WarnCommon) 371 warn("common " + S->getName() + " is overridden"); 372 return 1; 373 } 374 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute && 375 R->Value == Value) 376 return -1; 377 } 378 return 0; 379 } 380 381 Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment, 382 uint8_t Binding, uint8_t StOther, uint8_t Type, 383 InputFile *File) { 384 Symbol *S; 385 bool WasInserted; 386 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther), 387 /*CanOmitFromDynSym*/ false, File); 388 int Cmp = compareDefined(S, WasInserted, Binding, N); 389 if (Cmp > 0) { 390 auto *Bss = make<BssSection>("COMMON", Size, Alignment); 391 Bss->File = File; 392 Bss->Live = !Config->GcSections; 393 InputSections.push_back(Bss); 394 395 S->Binding = Binding; 396 replaceSymbol<Defined>(S, File, N, /*IsLocal=*/false, StOther, Type, 0, 397 Size, Bss); 398 } else if (Cmp == 0) { 399 auto *D = cast<Defined>(S); 400 auto *Bss = dyn_cast_or_null<BssSection>(D->Section); 401 if (!Bss) { 402 // Non-common symbols take precedence over common symbols. 403 if (Config->WarnCommon) 404 warn("common " + S->getName() + " is overridden"); 405 return S; 406 } 407 408 if (Config->WarnCommon) 409 warn("multiple common of " + D->getName()); 410 411 Bss->Alignment = std::max(Bss->Alignment, Alignment); 412 if (Size > Bss->Size) { 413 D->File = Bss->File = File; 414 D->Size = Bss->Size = Size; 415 } 416 } 417 return S; 418 } 419 420 static void warnOrError(const Twine &Msg) { 421 if (Config->AllowMultipleDefinition) 422 warn(Msg); 423 else 424 error(Msg); 425 } 426 427 static void reportDuplicate(Symbol *Sym, InputFile *NewFile) { 428 warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " + 429 toString(Sym->getFile()) + "\n>>> defined in " + 430 toString(NewFile)); 431 } 432 433 template <class ELFT> 434 static void reportDuplicate(Symbol *Sym, InputSectionBase *ErrSec, 435 typename ELFT::uint ErrOffset) { 436 Defined *D = dyn_cast<Defined>(Sym); 437 if (!D || !D->Section || !ErrSec) { 438 reportDuplicate(Sym, ErrSec ? ErrSec->File : nullptr); 439 return; 440 } 441 442 // Construct and print an error message in the form of: 443 // 444 // ld.lld: error: duplicate symbol: foo 445 // >>> defined at bar.c:30 446 // >>> bar.o (/home/alice/src/bar.o) 447 // >>> defined at baz.c:563 448 // >>> baz.o in archive libbaz.a 449 auto *Sec1 = cast<InputSectionBase>(D->Section); 450 std::string Src1 = Sec1->getSrcMsg<ELFT>(*Sym, D->Value); 451 std::string Obj1 = Sec1->getObjMsg(D->Value); 452 std::string Src2 = ErrSec->getSrcMsg<ELFT>(*Sym, ErrOffset); 453 std::string Obj2 = ErrSec->getObjMsg(ErrOffset); 454 455 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at "; 456 if (!Src1.empty()) 457 Msg += Src1 + "\n>>> "; 458 Msg += Obj1 + "\n>>> defined at "; 459 if (!Src2.empty()) 460 Msg += Src2 + "\n>>> "; 461 Msg += Obj2; 462 warnOrError(Msg); 463 } 464 465 template <typename ELFT> 466 Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type, 467 uint64_t Value, uint64_t Size, uint8_t Binding, 468 SectionBase *Section, InputFile *File) { 469 Symbol *S; 470 bool WasInserted; 471 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther), 472 /*CanOmitFromDynSym*/ false, File); 473 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr, 474 Value, Name); 475 if (Cmp > 0) 476 replaceSymbol<Defined>(S, File, Name, /*IsLocal=*/false, StOther, Type, 477 Value, Size, Section); 478 else if (Cmp == 0) 479 reportDuplicate<ELFT>(S, dyn_cast_or_null<InputSectionBase>(Section), 480 Value); 481 return S; 482 } 483 484 template <typename ELFT> 485 void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> *File, 486 const typename ELFT::Sym &Sym, uint32_t Alignment, 487 const typename ELFT::Verdef *Verdef) { 488 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT 489 // as the visibility, which will leave the visibility in the symbol table 490 // unchanged. 491 Symbol *S; 492 bool WasInserted; 493 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT, 494 /*CanOmitFromDynSym*/ true, File); 495 // Make sure we preempt DSO symbols with default visibility. 496 if (Sym.getVisibility() == STV_DEFAULT) 497 S->ExportDynamic = true; 498 499 // An undefined symbol with non default visibility must be satisfied 500 // in the same DSO. 501 if (WasInserted || ((S->isUndefined() || S->isLazy()) && 502 S->getVisibility() == STV_DEFAULT)) { 503 replaceSymbol<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(), 504 Sym.st_value, Sym.st_size, Alignment, Verdef); 505 if (!S->isWeak()) 506 File->IsUsed = true; 507 } 508 } 509 510 Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding, 511 uint8_t StOther, uint8_t Type, 512 bool CanOmitFromDynSym, BitcodeFile *F) { 513 Symbol *S; 514 bool WasInserted; 515 std::tie(S, WasInserted) = 516 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F); 517 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, 518 /*IsAbs*/ false, /*Value*/ 0, Name); 519 if (Cmp > 0) 520 replaceSymbol<Defined>(S, F, Name, /*IsLocal=*/false, StOther, Type, 0, 0, 521 nullptr); 522 else if (Cmp == 0) 523 reportDuplicate(S, F); 524 return S; 525 } 526 527 Symbol *SymbolTable::find(StringRef Name) { 528 auto It = Symtab.find(CachedHashStringRef(Name)); 529 if (It == Symtab.end()) 530 return nullptr; 531 if (It->second == -1) 532 return nullptr; 533 return SymVector[It->second]; 534 } 535 536 template <class ELFT> 537 Symbol *SymbolTable::addLazyArchive(StringRef Name, ArchiveFile *F, 538 const object::Archive::Symbol Sym) { 539 Symbol *S; 540 bool WasInserted; 541 std::tie(S, WasInserted) = insert(Name); 542 if (WasInserted) { 543 replaceSymbol<LazyArchive>(S, F, Sym, Symbol::UnknownType); 544 return S; 545 } 546 if (!S->isUndefined()) 547 return S; 548 549 // An undefined weak will not fetch archive members. See comment on Lazy in 550 // Symbols.h for the details. 551 if (S->isWeak()) { 552 replaceSymbol<LazyArchive>(S, F, Sym, S->Type); 553 return S; 554 } 555 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym); 556 if (!MBInfo.first.getBuffer().empty()) 557 addFile<ELFT>(createObjectFile(MBInfo.first, F->getName(), MBInfo.second)); 558 return S; 559 } 560 561 template <class ELFT> 562 void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) { 563 Symbol *S; 564 bool WasInserted; 565 std::tie(S, WasInserted) = insert(Name); 566 if (WasInserted) { 567 replaceSymbol<LazyObject>(S, &Obj, Name, Symbol::UnknownType); 568 return; 569 } 570 if (!S->isUndefined()) 571 return; 572 573 // See comment for addLazyArchive above. 574 if (S->isWeak()) 575 replaceSymbol<LazyObject>(S, &Obj, Name, S->Type); 576 else if (InputFile *F = Obj.fetch()) 577 addFile<ELFT>(F); 578 } 579 580 // If we already saw this symbol, force loading its file. 581 template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) { 582 if (Symbol *B = find(Name)) { 583 // Mark the symbol not to be eliminated by LTO 584 // even if it is a bitcode symbol. 585 B->IsUsedInRegularObj = true; 586 if (auto *L = dyn_cast_or_null<Lazy>(B)) 587 if (InputFile *File = L->fetch()) 588 addFile<ELFT>(File); 589 } 590 } 591 592 // This function takes care of the case in which shared libraries depend on 593 // the user program (not the other way, which is usual). Shared libraries 594 // may have undefined symbols, expecting that the user program provides 595 // the definitions for them. An example is BSD's __progname symbol. 596 // We need to put such symbols to the main program's .dynsym so that 597 // shared libraries can find them. 598 // Except this, we ignore undefined symbols in DSOs. 599 template <class ELFT> void SymbolTable::scanShlibUndefined() { 600 for (InputFile *F : SharedFiles) { 601 for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) { 602 Symbol *Sym = find(U); 603 if (!Sym || !Sym->isDefined()) 604 continue; 605 Sym->ExportDynamic = true; 606 607 // If -dynamic-list is given, the default version is set to 608 // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym. 609 // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were 610 // specified by -dynamic-list. 611 Sym->VersionId = VER_NDX_GLOBAL; 612 } 613 } 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 = demangle(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 = findByVersion(Ver); 690 else 691 Syms = findAllByVersion(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->NoUndefinedVersion) 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->InVersionScript) 727 warn("duplicate symbol '" + Ver.Name + "' in version script"); 728 Sym->VersionId = VersionId; 729 Sym->InVersionScript = true; 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::addSymbolWrap<ELF32LE>(StringRef); 778 template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef); 779 template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef); 780 template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef); 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, bool, uint8_t, 788 uint8_t, uint8_t, bool, 789 InputFile *); 790 template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, bool, uint8_t, 791 uint8_t, uint8_t, bool, 792 InputFile *); 793 template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, bool, uint8_t, 794 uint8_t, uint8_t, bool, 795 InputFile *); 796 template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, bool, uint8_t, 797 uint8_t, uint8_t, bool, 798 InputFile *); 799 800 template void SymbolTable::addCombinedLTOObject<ELF32LE>(); 801 template void SymbolTable::addCombinedLTOObject<ELF32BE>(); 802 template void SymbolTable::addCombinedLTOObject<ELF64LE>(); 803 template void SymbolTable::addCombinedLTOObject<ELF64BE>(); 804 805 template Symbol *SymbolTable::addRegular<ELF32LE>(StringRef, uint8_t, uint8_t, 806 uint64_t, uint64_t, uint8_t, 807 SectionBase *, InputFile *); 808 template Symbol *SymbolTable::addRegular<ELF32BE>(StringRef, uint8_t, uint8_t, 809 uint64_t, uint64_t, uint8_t, 810 SectionBase *, InputFile *); 811 template Symbol *SymbolTable::addRegular<ELF64LE>(StringRef, uint8_t, uint8_t, 812 uint64_t, uint64_t, uint8_t, 813 SectionBase *, InputFile *); 814 template Symbol *SymbolTable::addRegular<ELF64BE>(StringRef, uint8_t, uint8_t, 815 uint64_t, uint64_t, uint8_t, 816 SectionBase *, InputFile *); 817 818 template Defined *SymbolTable::addAbsolute<ELF32LE>(StringRef, uint8_t, 819 uint8_t); 820 template Defined *SymbolTable::addAbsolute<ELF32BE>(StringRef, uint8_t, 821 uint8_t); 822 template Defined *SymbolTable::addAbsolute<ELF64LE>(StringRef, uint8_t, 823 uint8_t); 824 template Defined *SymbolTable::addAbsolute<ELF64BE>(StringRef, uint8_t, 825 uint8_t); 826 827 template Symbol * 828 SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile *, 829 const object::Archive::Symbol); 830 template Symbol * 831 SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile *, 832 const object::Archive::Symbol); 833 template Symbol * 834 SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile *, 835 const object::Archive::Symbol); 836 template Symbol * 837 SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile *, 838 const object::Archive::Symbol); 839 840 template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &); 841 template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &); 842 template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &); 843 template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &); 844 845 template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> *, 846 const typename ELF32LE::Sym &, 847 uint32_t Alignment, 848 const typename ELF32LE::Verdef *); 849 template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> *, 850 const typename ELF32BE::Sym &, 851 uint32_t Alignment, 852 const typename ELF32BE::Verdef *); 853 template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> *, 854 const typename ELF64LE::Sym &, 855 uint32_t Alignment, 856 const typename ELF64LE::Verdef *); 857 template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> *, 858 const typename ELF64BE::Sym &, 859 uint32_t Alignment, 860 const typename ELF64BE::Verdef *); 861 862 template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef); 863 template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef); 864 template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef); 865 template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef); 866 867 template void SymbolTable::scanShlibUndefined<ELF32LE>(); 868 template void SymbolTable::scanShlibUndefined<ELF32BE>(); 869 template void SymbolTable::scanShlibUndefined<ELF64LE>(); 870 template void SymbolTable::scanShlibUndefined<ELF64BE>(); 871