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 cast<ObjFile<ELFT>>(File)->parse(DummyGroups); 134 ObjectFiles.push_back(File); 135 } 136 } 137 138 Defined *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility, 139 uint8_t Binding) { 140 Symbol *Sym = 141 addRegular(Name, Visibility, STT_NOTYPE, 0, 0, Binding, 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 SymMap.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 = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 190 memcpy(Real, W.Real, sizeof(SymbolUnion)); 191 } 192 193 // Replace __real_sym with sym and sym with __wrap_sym. 194 memcpy(W.Real, W.Sym, sizeof(SymbolUnion)); 195 memcpy(W.Sym, W.Wrap, sizeof(SymbolUnion)); 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 = SymMap.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 = reinterpret_cast<Symbol *>(make<SymbolUnion>()); 238 Sym->Visibility = STV_DEFAULT; 239 Sym->IsUsedInRegularObj = false; 240 Sym->ExportDynamic = false; 241 Sym->CanInline = true; 242 Sym->Traced = Traced; 243 Sym->VersionId = Config->DefaultSymbolVersion; 244 SymVector.push_back(Sym); 245 } else { 246 Sym = SymVector[SymIndex]; 247 } 248 return {Sym, IsNew}; 249 } 250 251 // Find an existing symbol or create and insert a new one, then apply the given 252 // attributes. 253 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type, 254 uint8_t Visibility, 255 bool CanOmitFromDynSym, 256 InputFile *File) { 257 Symbol *S; 258 bool WasInserted; 259 std::tie(S, WasInserted) = insert(Name); 260 261 // Merge in the new symbol's visibility. 262 S->Visibility = getMinVisibility(S->Visibility, Visibility); 263 264 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic)) 265 S->ExportDynamic = true; 266 267 if (!File || File->kind() == InputFile::ObjKind) 268 S->IsUsedInRegularObj = true; 269 270 if (!WasInserted && S->Type != Symbol::UnknownType && 271 ((Type == STT_TLS) != S->isTls())) { 272 error("TLS attribute mismatch: " + toString(*S) + "\n>>> defined in " + 273 toString(S->File) + "\n>>> defined in " + toString(File)); 274 } 275 276 return {S, WasInserted}; 277 } 278 279 template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) { 280 return addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT, 281 /*Type*/ 0, 282 /*CanOmitFromDynSym*/ false, /*File*/ nullptr); 283 } 284 285 static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; } 286 287 template <class ELFT> 288 Symbol *SymbolTable::addUndefined(StringRef Name, uint8_t Binding, 289 uint8_t StOther, uint8_t Type, 290 bool CanOmitFromDynSym, InputFile *File) { 291 Symbol *S; 292 bool WasInserted; 293 uint8_t Visibility = getVisibility(StOther); 294 std::tie(S, WasInserted) = 295 insert(Name, Type, Visibility, CanOmitFromDynSym, File); 296 // An undefined symbol with non default visibility must be satisfied 297 // in the same DSO. 298 if (WasInserted || (isa<SharedSymbol>(S) && Visibility != STV_DEFAULT)) { 299 replaceSymbol<Undefined>(S, File, Name, Binding, StOther, Type); 300 return S; 301 } 302 if (S->isShared() || S->isLazy() || (S->isUndefined() && Binding != STB_WEAK)) 303 S->Binding = Binding; 304 if (Binding != STB_WEAK) { 305 if (auto *SS = dyn_cast<SharedSymbol>(S)) 306 if (!Config->GcSections) 307 SS->getFile<ELFT>().IsNeeded = true; 308 } 309 if (auto *L = dyn_cast<Lazy>(S)) { 310 // An undefined weak will not fetch archive members. See comment on Lazy in 311 // Symbols.h for the details. 312 if (Binding == STB_WEAK) 313 L->Type = Type; 314 else if (InputFile *F = L->fetch()) 315 addFile<ELFT>(F); 316 } 317 return S; 318 } 319 320 // Using .symver foo,foo@@VER unfortunately creates two symbols: foo and 321 // foo@@VER. We want to effectively ignore foo, so give precedence to 322 // foo@@VER. 323 // FIXME: If users can transition to using 324 // .symver foo,foo@@@VER 325 // we can delete this hack. 326 static int compareVersion(Symbol *S, StringRef Name) { 327 bool A = Name.contains("@@"); 328 bool B = S->getName().contains("@@"); 329 if (A && !B) 330 return 1; 331 if (!A && B) 332 return -1; 333 return 0; 334 } 335 336 // We have a new defined symbol with the specified binding. Return 1 if the new 337 // symbol should win, -1 if the new symbol should lose, or 0 if both symbols are 338 // strong defined symbols. 339 static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding, 340 StringRef Name) { 341 if (WasInserted) 342 return 1; 343 if (!S->isDefined()) 344 return 1; 345 if (int R = compareVersion(S, Name)) 346 return R; 347 if (Binding == STB_WEAK) 348 return -1; 349 if (S->isWeak()) 350 return 1; 351 return 0; 352 } 353 354 // We have a new non-common defined symbol with the specified binding. Return 1 355 // if the new symbol should win, -1 if the new symbol should lose, or 0 if there 356 // is a conflict. If the new symbol wins, also update the binding. 357 static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding, 358 bool IsAbsolute, uint64_t Value, 359 StringRef Name) { 360 if (int Cmp = compareDefined(S, WasInserted, Binding, Name)) 361 return Cmp; 362 if (auto *R = dyn_cast<Defined>(S)) { 363 if (R->Section && isa<BssSection>(R->Section)) { 364 // Non-common symbols take precedence over common symbols. 365 if (Config->WarnCommon) 366 warn("common " + S->getName() + " is overridden"); 367 return 1; 368 } 369 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute && 370 R->Value == Value) 371 return -1; 372 } 373 return 0; 374 } 375 376 Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment, 377 uint8_t Binding, uint8_t StOther, uint8_t Type, 378 InputFile &File) { 379 Symbol *S; 380 bool WasInserted; 381 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther), 382 /*CanOmitFromDynSym*/ false, &File); 383 int Cmp = compareDefined(S, WasInserted, Binding, N); 384 if (Cmp > 0) { 385 auto *Bss = make<BssSection>("COMMON", Size, Alignment); 386 Bss->File = &File; 387 Bss->Live = !Config->GcSections; 388 InputSections.push_back(Bss); 389 390 replaceSymbol<Defined>(S, &File, N, Binding, StOther, Type, 0, Size, Bss); 391 } else if (Cmp == 0) { 392 auto *D = cast<Defined>(S); 393 auto *Bss = dyn_cast_or_null<BssSection>(D->Section); 394 if (!Bss) { 395 // Non-common symbols take precedence over common symbols. 396 if (Config->WarnCommon) 397 warn("common " + S->getName() + " is overridden"); 398 return S; 399 } 400 401 if (Config->WarnCommon) 402 warn("multiple common of " + D->getName()); 403 404 Bss->Alignment = std::max(Bss->Alignment, Alignment); 405 if (Size > Bss->Size) { 406 D->File = Bss->File = &File; 407 D->Size = Bss->Size = Size; 408 } 409 } 410 return S; 411 } 412 413 static void reportDuplicate(Symbol *Sym, InputFile *NewFile) { 414 if (!Config->AllowMultipleDefinition) 415 error("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " + 416 toString(Sym->File) + "\n>>> defined in " + toString(NewFile)); 417 } 418 419 static void reportDuplicate(Symbol *Sym, InputFile *NewFile, 420 InputSectionBase *ErrSec, uint64_t ErrOffset) { 421 if (Config->AllowMultipleDefinition) 422 return; 423 424 Defined *D = cast<Defined>(Sym); 425 if (!D->Section || !ErrSec) { 426 reportDuplicate(Sym, NewFile); 427 return; 428 } 429 430 // Construct and print an error message in the form of: 431 // 432 // ld.lld: error: duplicate symbol: foo 433 // >>> defined at bar.c:30 434 // >>> bar.o (/home/alice/src/bar.o) 435 // >>> defined at baz.c:563 436 // >>> baz.o in archive libbaz.a 437 auto *Sec1 = cast<InputSectionBase>(D->Section); 438 std::string Src1 = Sec1->getSrcMsg(*Sym, D->Value); 439 std::string Obj1 = Sec1->getObjMsg(D->Value); 440 std::string Src2 = ErrSec->getSrcMsg(*Sym, ErrOffset); 441 std::string Obj2 = ErrSec->getObjMsg(ErrOffset); 442 443 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at "; 444 if (!Src1.empty()) 445 Msg += Src1 + "\n>>> "; 446 Msg += Obj1 + "\n>>> defined at "; 447 if (!Src2.empty()) 448 Msg += Src2 + "\n>>> "; 449 Msg += Obj2; 450 error(Msg); 451 } 452 453 Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type, 454 uint64_t Value, uint64_t Size, uint8_t Binding, 455 SectionBase *Section, InputFile *File) { 456 Symbol *S; 457 bool WasInserted; 458 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther), 459 /*CanOmitFromDynSym*/ false, File); 460 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr, 461 Value, Name); 462 if (Cmp > 0) 463 replaceSymbol<Defined>(S, File, Name, Binding, StOther, Type, Value, Size, 464 Section); 465 else if (Cmp == 0) 466 reportDuplicate(S, File, dyn_cast_or_null<InputSectionBase>(Section), 467 Value); 468 return S; 469 } 470 471 template <typename ELFT> 472 void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> &File, 473 const typename ELFT::Sym &Sym, uint32_t Alignment, 474 uint32_t VerdefIndex) { 475 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT 476 // as the visibility, which will leave the visibility in the symbol table 477 // unchanged. 478 Symbol *S; 479 bool WasInserted; 480 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT, 481 /*CanOmitFromDynSym*/ true, &File); 482 // Make sure we preempt DSO symbols with default visibility. 483 if (Sym.getVisibility() == STV_DEFAULT) 484 S->ExportDynamic = true; 485 486 // An undefined symbol with non default visibility must be satisfied 487 // in the same DSO. 488 if (WasInserted || 489 ((S->isUndefined() || S->isLazy()) && S->Visibility == STV_DEFAULT)) { 490 uint8_t Binding = S->Binding; 491 bool WasUndefined = S->isUndefined(); 492 replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other, 493 Sym.getType(), Sym.st_value, Sym.st_size, 494 Alignment, VerdefIndex); 495 if (!WasInserted) { 496 S->Binding = Binding; 497 if (!S->isWeak() && !Config->GcSections && WasUndefined) 498 File.IsNeeded = true; 499 } 500 } 501 } 502 503 Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding, 504 uint8_t StOther, uint8_t Type, 505 bool CanOmitFromDynSym, BitcodeFile &F) { 506 Symbol *S; 507 bool WasInserted; 508 std::tie(S, WasInserted) = 509 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, &F); 510 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, 511 /*IsAbs*/ false, /*Value*/ 0, Name); 512 if (Cmp > 0) 513 replaceSymbol<Defined>(S, &F, Name, Binding, StOther, Type, 0, 0, nullptr); 514 else if (Cmp == 0) 515 reportDuplicate(S, &F); 516 return S; 517 } 518 519 Symbol *SymbolTable::find(StringRef Name) { 520 auto It = SymMap.find(CachedHashStringRef(Name)); 521 if (It == SymMap.end()) 522 return nullptr; 523 if (It->second == -1) 524 return nullptr; 525 return SymVector[It->second]; 526 } 527 528 template <class ELFT> 529 void SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &F, 530 const object::Archive::Symbol Sym) { 531 Symbol *S; 532 bool WasInserted; 533 std::tie(S, WasInserted) = insert(Name); 534 if (WasInserted) { 535 replaceSymbol<LazyArchive>(S, F, Sym, Symbol::UnknownType); 536 return; 537 } 538 if (!S->isUndefined()) 539 return; 540 541 // An undefined weak will not fetch archive members. See comment on Lazy in 542 // Symbols.h for the details. 543 if (S->isWeak()) { 544 replaceSymbol<LazyArchive>(S, F, Sym, S->Type); 545 S->Binding = STB_WEAK; 546 return; 547 } 548 std::pair<MemoryBufferRef, uint64_t> MBInfo = F.getMember(&Sym); 549 if (!MBInfo.first.getBuffer().empty()) 550 addFile<ELFT>(createObjectFile(MBInfo.first, F.getName(), MBInfo.second)); 551 } 552 553 template <class ELFT> 554 void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) { 555 Symbol *S; 556 bool WasInserted; 557 std::tie(S, WasInserted) = insert(Name); 558 if (WasInserted) { 559 replaceSymbol<LazyObject>(S, Obj, Name, Symbol::UnknownType); 560 return; 561 } 562 if (!S->isUndefined()) 563 return; 564 565 // See comment for addLazyArchive above. 566 if (S->isWeak()) { 567 replaceSymbol<LazyObject>(S, Obj, Name, S->Type); 568 S->Binding = STB_WEAK; 569 return; 570 } 571 if (InputFile *F = Obj.fetch()) 572 addFile<ELFT>(F); 573 } 574 575 // If we already saw this symbol, force loading its file. 576 template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) { 577 if (Symbol *B = find(Name)) { 578 // Mark the symbol not to be eliminated by LTO 579 // even if it is a bitcode symbol. 580 B->IsUsedInRegularObj = true; 581 if (auto *L = dyn_cast<Lazy>(B)) 582 if (InputFile *File = L->fetch()) 583 addFile<ELFT>(File); 584 } 585 } 586 587 // Initialize DemangledSyms with a map from demangled symbols to symbol 588 // objects. Used to handle "extern C++" directive in version scripts. 589 // 590 // The map will contain all demangled symbols. That can be very large, 591 // and in LLD we generally want to avoid do anything for each symbol. 592 // Then, why are we doing this? Here's why. 593 // 594 // Users can use "extern C++ {}" directive to match against demangled 595 // C++ symbols. For example, you can write a pattern such as 596 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this 597 // other than trying to match a pattern against all demangled symbols. 598 // So, if "extern C++" feature is used, we need to demangle all known 599 // symbols. 600 StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() { 601 if (!DemangledSyms) { 602 DemangledSyms.emplace(); 603 for (Symbol *Sym : SymVector) { 604 if (!Sym->isDefined()) 605 continue; 606 if (Optional<std::string> S = demangleItanium(Sym->getName())) 607 (*DemangledSyms)[*S].push_back(Sym); 608 else 609 (*DemangledSyms)[Sym->getName()].push_back(Sym); 610 } 611 } 612 return *DemangledSyms; 613 } 614 615 std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) { 616 if (Ver.IsExternCpp) 617 return getDemangledSyms().lookup(Ver.Name); 618 if (Symbol *B = find(Ver.Name)) 619 if (B->isDefined()) 620 return {B}; 621 return {}; 622 } 623 624 std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) { 625 std::vector<Symbol *> Res; 626 StringMatcher M(Ver.Name); 627 628 if (Ver.IsExternCpp) { 629 for (auto &P : getDemangledSyms()) 630 if (M.match(P.first())) 631 Res.insert(Res.end(), P.second.begin(), P.second.end()); 632 return Res; 633 } 634 635 for (Symbol *Sym : SymVector) 636 if (Sym->isDefined() && M.match(Sym->getName())) 637 Res.push_back(Sym); 638 return Res; 639 } 640 641 // If there's only one anonymous version definition in a version 642 // script file, the script does not actually define any symbol version, 643 // but just specifies symbols visibilities. 644 void SymbolTable::handleAnonymousVersion() { 645 for (SymbolVersion &Ver : Config->VersionScriptGlobals) 646 assignExactVersion(Ver, VER_NDX_GLOBAL, "global"); 647 for (SymbolVersion &Ver : Config->VersionScriptGlobals) 648 assignWildcardVersion(Ver, VER_NDX_GLOBAL); 649 for (SymbolVersion &Ver : Config->VersionScriptLocals) 650 assignExactVersion(Ver, VER_NDX_LOCAL, "local"); 651 for (SymbolVersion &Ver : Config->VersionScriptLocals) 652 assignWildcardVersion(Ver, VER_NDX_LOCAL); 653 } 654 655 // Handles -dynamic-list. 656 void SymbolTable::handleDynamicList() { 657 for (SymbolVersion &Ver : Config->DynamicList) { 658 std::vector<Symbol *> Syms; 659 if (Ver.HasWildcard) 660 Syms = findAllByVersion(Ver); 661 else 662 Syms = findByVersion(Ver); 663 664 for (Symbol *B : Syms) { 665 if (!Config->Shared) 666 B->ExportDynamic = true; 667 else if (B->includeInDynsym()) 668 B->IsPreemptible = true; 669 } 670 } 671 } 672 673 // Set symbol versions to symbols. This function handles patterns 674 // containing no wildcard characters. 675 void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId, 676 StringRef VersionName) { 677 if (Ver.HasWildcard) 678 return; 679 680 // Get a list of symbols which we need to assign the version to. 681 std::vector<Symbol *> Syms = findByVersion(Ver); 682 if (Syms.empty()) { 683 if (!Config->UndefinedVersion) 684 error("version script assignment of '" + VersionName + "' to symbol '" + 685 Ver.Name + "' failed: symbol not defined"); 686 return; 687 } 688 689 // Assign the version. 690 for (Symbol *Sym : Syms) { 691 // Skip symbols containing version info because symbol versions 692 // specified by symbol names take precedence over version scripts. 693 // See parseSymbolVersion(). 694 if (Sym->getName().contains('@')) 695 continue; 696 697 if (Sym->VersionId != Config->DefaultSymbolVersion && 698 Sym->VersionId != VersionId) 699 error("duplicate symbol '" + Ver.Name + "' in version script"); 700 Sym->VersionId = VersionId; 701 } 702 } 703 704 void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) { 705 if (!Ver.HasWildcard) 706 return; 707 708 // Exact matching takes precendence over fuzzy matching, 709 // so we set a version to a symbol only if no version has been assigned 710 // to the symbol. This behavior is compatible with GNU. 711 for (Symbol *B : findAllByVersion(Ver)) 712 if (B->VersionId == Config->DefaultSymbolVersion) 713 B->VersionId = VersionId; 714 } 715 716 // This function processes version scripts by updating VersionId 717 // member of symbols. 718 void SymbolTable::scanVersionScript() { 719 // Handle edge cases first. 720 handleAnonymousVersion(); 721 handleDynamicList(); 722 723 // Now we have version definitions, so we need to set version ids to symbols. 724 // Each version definition has a glob pattern, and all symbols that match 725 // with the pattern get that version. 726 727 // First, we assign versions to exact matching symbols, 728 // i.e. version definitions not containing any glob meta-characters. 729 for (VersionDefinition &V : Config->VersionDefinitions) 730 for (SymbolVersion &Ver : V.Globals) 731 assignExactVersion(Ver, V.Id, V.Name); 732 733 // Next, we assign versions to fuzzy matching symbols, 734 // i.e. version definitions containing glob meta-characters. 735 // Note that because the last match takes precedence over previous matches, 736 // we iterate over the definitions in the reverse order. 737 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions)) 738 for (SymbolVersion &Ver : V.Globals) 739 assignWildcardVersion(Ver, V.Id); 740 741 // Symbol themselves might know their versions because symbols 742 // can contain versions in the form of <name>@<version>. 743 // Let them parse and update their names to exclude version suffix. 744 for (Symbol *Sym : SymVector) 745 Sym->parseSymbolVersion(); 746 } 747 748 template void SymbolTable::addFile<ELF32LE>(InputFile *); 749 template void SymbolTable::addFile<ELF32BE>(InputFile *); 750 template void SymbolTable::addFile<ELF64LE>(InputFile *); 751 template void SymbolTable::addFile<ELF64BE>(InputFile *); 752 753 template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef); 754 template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef); 755 template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef); 756 template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef); 757 758 template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef); 759 template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef); 760 template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef); 761 template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef); 762 763 template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t, 764 uint8_t, bool, InputFile *); 765 template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t, 766 uint8_t, bool, InputFile *); 767 template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t, 768 uint8_t, bool, InputFile *); 769 template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t, 770 uint8_t, bool, InputFile *); 771 772 template void SymbolTable::addCombinedLTOObject<ELF32LE>(); 773 template void SymbolTable::addCombinedLTOObject<ELF32BE>(); 774 template void SymbolTable::addCombinedLTOObject<ELF64LE>(); 775 template void SymbolTable::addCombinedLTOObject<ELF64BE>(); 776 777 template void 778 SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile &, 779 const object::Archive::Symbol); 780 template void 781 SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile &, 782 const object::Archive::Symbol); 783 template void 784 SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile &, 785 const object::Archive::Symbol); 786 template void 787 SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile &, 788 const object::Archive::Symbol); 789 790 template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &); 791 template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &); 792 template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &); 793 template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &); 794 795 template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> &, 796 const typename ELF32LE::Sym &, 797 uint32_t Alignment, uint32_t); 798 template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> &, 799 const typename ELF32BE::Sym &, 800 uint32_t Alignment, uint32_t); 801 template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> &, 802 const typename ELF64LE::Sym &, 803 uint32_t Alignment, uint32_t); 804 template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> &, 805 const typename ELF64BE::Sym &, 806 uint32_t Alignment, uint32_t); 807 808 template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef); 809 template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef); 810 template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef); 811 template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef); 812