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