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 warnOrError(const Twine &Msg) { 414 if (Config->AllowMultipleDefinition) 415 warn(Msg); 416 else 417 error(Msg); 418 } 419 420 static void reportDuplicate(Symbol *Sym, InputFile *NewFile) { 421 warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " + 422 toString(Sym->File) + "\n>>> defined in " + toString(NewFile)); 423 } 424 425 static void reportDuplicate(Symbol *Sym, InputFile *NewFile, 426 InputSectionBase *ErrSec, uint64_t ErrOffset) { 427 Defined *D = cast<Defined>(Sym); 428 if (!D->Section || !ErrSec) { 429 reportDuplicate(Sym, NewFile); 430 return; 431 } 432 433 // Construct and print an error message in the form of: 434 // 435 // ld.lld: error: duplicate symbol: foo 436 // >>> defined at bar.c:30 437 // >>> bar.o (/home/alice/src/bar.o) 438 // >>> defined at baz.c:563 439 // >>> baz.o in archive libbaz.a 440 auto *Sec1 = cast<InputSectionBase>(D->Section); 441 std::string Src1 = Sec1->getSrcMsg(*Sym, D->Value); 442 std::string Obj1 = Sec1->getObjMsg(D->Value); 443 std::string Src2 = ErrSec->getSrcMsg(*Sym, ErrOffset); 444 std::string Obj2 = ErrSec->getObjMsg(ErrOffset); 445 446 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at "; 447 if (!Src1.empty()) 448 Msg += Src1 + "\n>>> "; 449 Msg += Obj1 + "\n>>> defined at "; 450 if (!Src2.empty()) 451 Msg += Src2 + "\n>>> "; 452 Msg += Obj2; 453 warnOrError(Msg); 454 } 455 456 Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type, 457 uint64_t Value, uint64_t Size, uint8_t Binding, 458 SectionBase *Section, InputFile *File) { 459 Symbol *S; 460 bool WasInserted; 461 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther), 462 /*CanOmitFromDynSym*/ false, File); 463 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr, 464 Value, Name); 465 if (Cmp > 0) 466 replaceSymbol<Defined>(S, File, Name, Binding, StOther, Type, Value, Size, 467 Section); 468 else if (Cmp == 0) 469 reportDuplicate(S, File, dyn_cast_or_null<InputSectionBase>(Section), 470 Value); 471 return S; 472 } 473 474 template <typename ELFT> 475 void SymbolTable::addShared(StringRef Name, SharedFile<ELFT> &File, 476 const typename ELFT::Sym &Sym, uint32_t Alignment, 477 uint32_t VerdefIndex) { 478 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT 479 // as the visibility, which will leave the visibility in the symbol table 480 // unchanged. 481 Symbol *S; 482 bool WasInserted; 483 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT, 484 /*CanOmitFromDynSym*/ true, &File); 485 // Make sure we preempt DSO symbols with default visibility. 486 if (Sym.getVisibility() == STV_DEFAULT) 487 S->ExportDynamic = true; 488 489 // An undefined symbol with non default visibility must be satisfied 490 // in the same DSO. 491 if (WasInserted || 492 ((S->isUndefined() || S->isLazy()) && S->Visibility == STV_DEFAULT)) { 493 uint8_t Binding = S->Binding; 494 bool WasUndefined = S->isUndefined(); 495 replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other, 496 Sym.getType(), Sym.st_value, Sym.st_size, 497 Alignment, VerdefIndex); 498 if (!WasInserted) { 499 S->Binding = Binding; 500 if (!S->isWeak() && !Config->GcSections && WasUndefined) 501 File.IsNeeded = true; 502 } 503 } 504 } 505 506 Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding, 507 uint8_t StOther, uint8_t Type, 508 bool CanOmitFromDynSym, BitcodeFile &F) { 509 Symbol *S; 510 bool WasInserted; 511 std::tie(S, WasInserted) = 512 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, &F); 513 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, 514 /*IsAbs*/ false, /*Value*/ 0, Name); 515 if (Cmp > 0) 516 replaceSymbol<Defined>(S, &F, Name, Binding, StOther, Type, 0, 0, nullptr); 517 else if (Cmp == 0) 518 reportDuplicate(S, &F); 519 return S; 520 } 521 522 Symbol *SymbolTable::find(StringRef Name) { 523 auto It = SymMap.find(CachedHashStringRef(Name)); 524 if (It == SymMap.end()) 525 return nullptr; 526 if (It->second == -1) 527 return nullptr; 528 return SymVector[It->second]; 529 } 530 531 template <class ELFT> 532 void SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &F, 533 const object::Archive::Symbol Sym) { 534 Symbol *S; 535 bool WasInserted; 536 std::tie(S, WasInserted) = insert(Name); 537 if (WasInserted) { 538 replaceSymbol<LazyArchive>(S, F, Sym, Symbol::UnknownType); 539 return; 540 } 541 if (!S->isUndefined()) 542 return; 543 544 // An undefined weak will not fetch archive members. See comment on Lazy in 545 // Symbols.h for the details. 546 if (S->isWeak()) { 547 replaceSymbol<LazyArchive>(S, F, Sym, S->Type); 548 S->Binding = STB_WEAK; 549 return; 550 } 551 std::pair<MemoryBufferRef, uint64_t> MBInfo = F.getMember(&Sym); 552 if (!MBInfo.first.getBuffer().empty()) 553 addFile<ELFT>(createObjectFile(MBInfo.first, F.getName(), MBInfo.second)); 554 } 555 556 template <class ELFT> 557 void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) { 558 Symbol *S; 559 bool WasInserted; 560 std::tie(S, WasInserted) = insert(Name); 561 if (WasInserted) { 562 replaceSymbol<LazyObject>(S, Obj, Name, Symbol::UnknownType); 563 return; 564 } 565 if (!S->isUndefined()) 566 return; 567 568 // See comment for addLazyArchive above. 569 if (S->isWeak()) { 570 replaceSymbol<LazyObject>(S, Obj, Name, S->Type); 571 S->Binding = STB_WEAK; 572 return; 573 } 574 if (InputFile *F = Obj.fetch()) 575 addFile<ELFT>(F); 576 } 577 578 // If we already saw this symbol, force loading its file. 579 template <class ELFT> void SymbolTable::fetchIfLazy(StringRef Name) { 580 if (Symbol *B = find(Name)) { 581 // Mark the symbol not to be eliminated by LTO 582 // even if it is a bitcode symbol. 583 B->IsUsedInRegularObj = true; 584 if (auto *L = dyn_cast<Lazy>(B)) 585 if (InputFile *File = L->fetch()) 586 addFile<ELFT>(File); 587 } 588 } 589 590 // Initialize DemangledSyms with a map from demangled symbols to symbol 591 // objects. Used to handle "extern C++" directive in version scripts. 592 // 593 // The map will contain all demangled symbols. That can be very large, 594 // and in LLD we generally want to avoid do anything for each symbol. 595 // Then, why are we doing this? Here's why. 596 // 597 // Users can use "extern C++ {}" directive to match against demangled 598 // C++ symbols. For example, you can write a pattern such as 599 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this 600 // other than trying to match a pattern against all demangled symbols. 601 // So, if "extern C++" feature is used, we need to demangle all known 602 // symbols. 603 StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() { 604 if (!DemangledSyms) { 605 DemangledSyms.emplace(); 606 for (Symbol *Sym : SymVector) { 607 if (!Sym->isDefined()) 608 continue; 609 if (Optional<std::string> S = demangleItanium(Sym->getName())) 610 (*DemangledSyms)[*S].push_back(Sym); 611 else 612 (*DemangledSyms)[Sym->getName()].push_back(Sym); 613 } 614 } 615 return *DemangledSyms; 616 } 617 618 std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) { 619 if (Ver.IsExternCpp) 620 return getDemangledSyms().lookup(Ver.Name); 621 if (Symbol *B = find(Ver.Name)) 622 if (B->isDefined()) 623 return {B}; 624 return {}; 625 } 626 627 std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) { 628 std::vector<Symbol *> Res; 629 StringMatcher M(Ver.Name); 630 631 if (Ver.IsExternCpp) { 632 for (auto &P : getDemangledSyms()) 633 if (M.match(P.first())) 634 Res.insert(Res.end(), P.second.begin(), P.second.end()); 635 return Res; 636 } 637 638 for (Symbol *Sym : SymVector) 639 if (Sym->isDefined() && M.match(Sym->getName())) 640 Res.push_back(Sym); 641 return Res; 642 } 643 644 // If there's only one anonymous version definition in a version 645 // script file, the script does not actually define any symbol version, 646 // but just specifies symbols visibilities. 647 void SymbolTable::handleAnonymousVersion() { 648 for (SymbolVersion &Ver : Config->VersionScriptGlobals) 649 assignExactVersion(Ver, VER_NDX_GLOBAL, "global"); 650 for (SymbolVersion &Ver : Config->VersionScriptGlobals) 651 assignWildcardVersion(Ver, VER_NDX_GLOBAL); 652 for (SymbolVersion &Ver : Config->VersionScriptLocals) 653 assignExactVersion(Ver, VER_NDX_LOCAL, "local"); 654 for (SymbolVersion &Ver : Config->VersionScriptLocals) 655 assignWildcardVersion(Ver, VER_NDX_LOCAL); 656 } 657 658 // Handles -dynamic-list. 659 void SymbolTable::handleDynamicList() { 660 for (SymbolVersion &Ver : Config->DynamicList) { 661 std::vector<Symbol *> Syms; 662 if (Ver.HasWildcard) 663 Syms = findAllByVersion(Ver); 664 else 665 Syms = findByVersion(Ver); 666 667 for (Symbol *B : Syms) { 668 if (!Config->Shared) 669 B->ExportDynamic = true; 670 else if (B->includeInDynsym()) 671 B->IsPreemptible = true; 672 } 673 } 674 } 675 676 // Set symbol versions to symbols. This function handles patterns 677 // containing no wildcard characters. 678 void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId, 679 StringRef VersionName) { 680 if (Ver.HasWildcard) 681 return; 682 683 // Get a list of symbols which we need to assign the version to. 684 std::vector<Symbol *> Syms = findByVersion(Ver); 685 if (Syms.empty()) { 686 if (!Config->UndefinedVersion) 687 error("version script assignment of '" + VersionName + "' to symbol '" + 688 Ver.Name + "' failed: symbol not defined"); 689 return; 690 } 691 692 // Assign the version. 693 for (Symbol *Sym : Syms) { 694 // Skip symbols containing version info because symbol versions 695 // specified by symbol names take precedence over version scripts. 696 // See parseSymbolVersion(). 697 if (Sym->getName().contains('@')) 698 continue; 699 700 if (Sym->VersionId != Config->DefaultSymbolVersion && 701 Sym->VersionId != VersionId) 702 error("duplicate symbol '" + Ver.Name + "' in version script"); 703 Sym->VersionId = VersionId; 704 } 705 } 706 707 void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) { 708 if (!Ver.HasWildcard) 709 return; 710 711 // Exact matching takes precendence over fuzzy matching, 712 // so we set a version to a symbol only if no version has been assigned 713 // to the symbol. This behavior is compatible with GNU. 714 for (Symbol *B : findAllByVersion(Ver)) 715 if (B->VersionId == Config->DefaultSymbolVersion) 716 B->VersionId = VersionId; 717 } 718 719 // This function processes version scripts by updating VersionId 720 // member of symbols. 721 void SymbolTable::scanVersionScript() { 722 // Handle edge cases first. 723 handleAnonymousVersion(); 724 handleDynamicList(); 725 726 // Now we have version definitions, so we need to set version ids to symbols. 727 // Each version definition has a glob pattern, and all symbols that match 728 // with the pattern get that version. 729 730 // First, we assign versions to exact matching symbols, 731 // i.e. version definitions not containing any glob meta-characters. 732 for (VersionDefinition &V : Config->VersionDefinitions) 733 for (SymbolVersion &Ver : V.Globals) 734 assignExactVersion(Ver, V.Id, V.Name); 735 736 // Next, we assign versions to fuzzy matching symbols, 737 // i.e. version definitions containing glob meta-characters. 738 // Note that because the last match takes precedence over previous matches, 739 // we iterate over the definitions in the reverse order. 740 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions)) 741 for (SymbolVersion &Ver : V.Globals) 742 assignWildcardVersion(Ver, V.Id); 743 744 // Symbol themselves might know their versions because symbols 745 // can contain versions in the form of <name>@<version>. 746 // Let them parse and update their names to exclude version suffix. 747 for (Symbol *Sym : SymVector) 748 Sym->parseSymbolVersion(); 749 } 750 751 template void SymbolTable::addFile<ELF32LE>(InputFile *); 752 template void SymbolTable::addFile<ELF32BE>(InputFile *); 753 template void SymbolTable::addFile<ELF64LE>(InputFile *); 754 template void SymbolTable::addFile<ELF64BE>(InputFile *); 755 756 template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef); 757 template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef); 758 template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef); 759 template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef); 760 761 template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef); 762 template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef); 763 template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef); 764 template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef); 765 766 template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t, 767 uint8_t, bool, InputFile *); 768 template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t, 769 uint8_t, bool, InputFile *); 770 template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t, 771 uint8_t, bool, InputFile *); 772 template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t, 773 uint8_t, bool, InputFile *); 774 775 template void SymbolTable::addCombinedLTOObject<ELF32LE>(); 776 template void SymbolTable::addCombinedLTOObject<ELF32BE>(); 777 template void SymbolTable::addCombinedLTOObject<ELF64LE>(); 778 template void SymbolTable::addCombinedLTOObject<ELF64BE>(); 779 780 template void 781 SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile &, 782 const object::Archive::Symbol); 783 template void 784 SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile &, 785 const object::Archive::Symbol); 786 template void 787 SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile &, 788 const object::Archive::Symbol); 789 template void 790 SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile &, 791 const object::Archive::Symbol); 792 793 template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &); 794 template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &); 795 template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &); 796 template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &); 797 798 template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> &, 799 const typename ELF32LE::Sym &, 800 uint32_t Alignment, uint32_t); 801 template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> &, 802 const typename ELF32BE::Sym &, 803 uint32_t Alignment, uint32_t); 804 template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> &, 805 const typename ELF64LE::Sym &, 806 uint32_t Alignment, uint32_t); 807 template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> &, 808 const typename ELF64BE::Sym &, 809 uint32_t Alignment, uint32_t); 810 811 template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef); 812 template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef); 813 template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef); 814 template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef); 815