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