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