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 replaceSymbol<SharedSymbol>(S, File, Name, Sym.getBinding(), Sym.st_other, 495 Sym.getType(), Sym.st_value, Sym.st_size, 496 Alignment, VerdefIndex); 497 if (!WasInserted) { 498 S->Binding = Binding; 499 if (!S->isWeak() && !Config->GcSections) 500 File.IsNeeded = true; 501 } 502 } 503 } 504 505 Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding, 506 uint8_t StOther, uint8_t Type, 507 bool CanOmitFromDynSym, BitcodeFile &F) { 508 Symbol *S; 509 bool WasInserted; 510 std::tie(S, WasInserted) = 511 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, &F); 512 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, 513 /*IsAbs*/ false, /*Value*/ 0, Name); 514 if (Cmp > 0) 515 replaceSymbol<Defined>(S, &F, Name, Binding, StOther, Type, 0, 0, nullptr); 516 else if (Cmp == 0) 517 reportDuplicate(S, &F); 518 return S; 519 } 520 521 Symbol *SymbolTable::find(StringRef Name) { 522 auto It = SymMap.find(CachedHashStringRef(Name)); 523 if (It == SymMap.end()) 524 return nullptr; 525 if (It->second == -1) 526 return nullptr; 527 return SymVector[It->second]; 528 } 529 530 template <class ELFT> 531 Symbol *SymbolTable::addLazyArchive(StringRef Name, ArchiveFile &F, 532 const object::Archive::Symbol Sym) { 533 Symbol *S; 534 bool WasInserted; 535 std::tie(S, WasInserted) = insert(Name); 536 if (WasInserted) { 537 replaceSymbol<LazyArchive>(S, F, Sym, Symbol::UnknownType); 538 return S; 539 } 540 if (!S->isUndefined()) 541 return S; 542 543 // An undefined weak will not fetch archive members. See comment on Lazy in 544 // Symbols.h for the details. 545 if (S->isWeak()) { 546 replaceSymbol<LazyArchive>(S, F, Sym, S->Type); 547 S->Binding = STB_WEAK; 548 return S; 549 } 550 std::pair<MemoryBufferRef, uint64_t> MBInfo = F.getMember(&Sym); 551 if (!MBInfo.first.getBuffer().empty()) 552 addFile<ELFT>(createObjectFile(MBInfo.first, F.getName(), MBInfo.second)); 553 return S; 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 else 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 // This function takes care of the case in which shared libraries depend on 588 // the user program (not the other way, which is usual). Shared libraries 589 // may have undefined symbols, expecting that the user program provides 590 // the definitions for them. An example is BSD's __progname symbol. 591 // We need to put such symbols to the main program's .dynsym so that 592 // shared libraries can find them. 593 // Except this, we ignore undefined symbols in DSOs. 594 template <class ELFT> void SymbolTable::scanShlibUndefined() { 595 for (InputFile *F : SharedFiles) { 596 for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) { 597 Symbol *Sym = find(U); 598 if (!Sym || !Sym->isDefined()) 599 continue; 600 Sym->ExportDynamic = true; 601 } 602 } 603 } 604 605 // Initialize DemangledSyms with a map from demangled symbols to symbol 606 // objects. Used to handle "extern C++" directive in version scripts. 607 // 608 // The map will contain all demangled symbols. That can be very large, 609 // and in LLD we generally want to avoid do anything for each symbol. 610 // Then, why are we doing this? Here's why. 611 // 612 // Users can use "extern C++ {}" directive to match against demangled 613 // C++ symbols. For example, you can write a pattern such as 614 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this 615 // other than trying to match a pattern against all demangled symbols. 616 // So, if "extern C++" feature is used, we need to demangle all known 617 // symbols. 618 StringMap<std::vector<Symbol *>> &SymbolTable::getDemangledSyms() { 619 if (!DemangledSyms) { 620 DemangledSyms.emplace(); 621 for (Symbol *Sym : SymVector) { 622 if (!Sym->isDefined()) 623 continue; 624 if (Optional<std::string> S = demangleItanium(Sym->getName())) 625 (*DemangledSyms)[*S].push_back(Sym); 626 else 627 (*DemangledSyms)[Sym->getName()].push_back(Sym); 628 } 629 } 630 return *DemangledSyms; 631 } 632 633 std::vector<Symbol *> SymbolTable::findByVersion(SymbolVersion Ver) { 634 if (Ver.IsExternCpp) 635 return getDemangledSyms().lookup(Ver.Name); 636 if (Symbol *B = find(Ver.Name)) 637 if (B->isDefined()) 638 return {B}; 639 return {}; 640 } 641 642 std::vector<Symbol *> SymbolTable::findAllByVersion(SymbolVersion Ver) { 643 std::vector<Symbol *> Res; 644 StringMatcher M(Ver.Name); 645 646 if (Ver.IsExternCpp) { 647 for (auto &P : getDemangledSyms()) 648 if (M.match(P.first())) 649 Res.insert(Res.end(), P.second.begin(), P.second.end()); 650 return Res; 651 } 652 653 for (Symbol *Sym : SymVector) 654 if (Sym->isDefined() && M.match(Sym->getName())) 655 Res.push_back(Sym); 656 return Res; 657 } 658 659 // If there's only one anonymous version definition in a version 660 // script file, the script does not actually define any symbol version, 661 // but just specifies symbols visibilities. 662 void SymbolTable::handleAnonymousVersion() { 663 for (SymbolVersion &Ver : Config->VersionScriptGlobals) 664 assignExactVersion(Ver, VER_NDX_GLOBAL, "global"); 665 for (SymbolVersion &Ver : Config->VersionScriptGlobals) 666 assignWildcardVersion(Ver, VER_NDX_GLOBAL); 667 for (SymbolVersion &Ver : Config->VersionScriptLocals) 668 assignExactVersion(Ver, VER_NDX_LOCAL, "local"); 669 for (SymbolVersion &Ver : Config->VersionScriptLocals) 670 assignWildcardVersion(Ver, VER_NDX_LOCAL); 671 } 672 673 // Handles -dynamic-list. 674 void SymbolTable::handleDynamicList() { 675 for (SymbolVersion &Ver : Config->DynamicList) { 676 std::vector<Symbol *> Syms; 677 if (Ver.HasWildcard) 678 Syms = findAllByVersion(Ver); 679 else 680 Syms = findByVersion(Ver); 681 682 for (Symbol *B : Syms) { 683 if (!Config->Shared) 684 B->ExportDynamic = true; 685 else if (B->includeInDynsym()) 686 B->IsPreemptible = true; 687 } 688 } 689 } 690 691 // Set symbol versions to symbols. This function handles patterns 692 // containing no wildcard characters. 693 void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId, 694 StringRef VersionName) { 695 if (Ver.HasWildcard) 696 return; 697 698 // Get a list of symbols which we need to assign the version to. 699 std::vector<Symbol *> Syms = findByVersion(Ver); 700 if (Syms.empty()) { 701 if (Config->NoUndefinedVersion) 702 error("version script assignment of '" + VersionName + "' to symbol '" + 703 Ver.Name + "' failed: symbol not defined"); 704 return; 705 } 706 707 // Assign the version. 708 for (Symbol *Sym : Syms) { 709 // Skip symbols containing version info because symbol versions 710 // specified by symbol names take precedence over version scripts. 711 // See parseSymbolVersion(). 712 if (Sym->getName().contains('@')) 713 continue; 714 715 if (Sym->InVersionScript) 716 warn("duplicate symbol '" + Ver.Name + "' in version script"); 717 Sym->VersionId = VersionId; 718 Sym->InVersionScript = true; 719 } 720 } 721 722 void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) { 723 if (!Ver.HasWildcard) 724 return; 725 726 // Exact matching takes precendence over fuzzy matching, 727 // so we set a version to a symbol only if no version has been assigned 728 // to the symbol. This behavior is compatible with GNU. 729 for (Symbol *B : findAllByVersion(Ver)) 730 if (B->VersionId == Config->DefaultSymbolVersion) 731 B->VersionId = VersionId; 732 } 733 734 // This function processes version scripts by updating VersionId 735 // member of symbols. 736 void SymbolTable::scanVersionScript() { 737 // Handle edge cases first. 738 handleAnonymousVersion(); 739 handleDynamicList(); 740 741 // Now we have version definitions, so we need to set version ids to symbols. 742 // Each version definition has a glob pattern, and all symbols that match 743 // with the pattern get that version. 744 745 // First, we assign versions to exact matching symbols, 746 // i.e. version definitions not containing any glob meta-characters. 747 for (VersionDefinition &V : Config->VersionDefinitions) 748 for (SymbolVersion &Ver : V.Globals) 749 assignExactVersion(Ver, V.Id, V.Name); 750 751 // Next, we assign versions to fuzzy matching symbols, 752 // i.e. version definitions containing glob meta-characters. 753 // Note that because the last match takes precedence over previous matches, 754 // we iterate over the definitions in the reverse order. 755 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions)) 756 for (SymbolVersion &Ver : V.Globals) 757 assignWildcardVersion(Ver, V.Id); 758 759 // Symbol themselves might know their versions because symbols 760 // can contain versions in the form of <name>@<version>. 761 // Let them parse and update their names to exclude version suffix. 762 for (Symbol *Sym : SymVector) 763 Sym->parseSymbolVersion(); 764 } 765 766 template void SymbolTable::addFile<ELF32LE>(InputFile *); 767 template void SymbolTable::addFile<ELF32BE>(InputFile *); 768 template void SymbolTable::addFile<ELF64LE>(InputFile *); 769 template void SymbolTable::addFile<ELF64BE>(InputFile *); 770 771 template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef); 772 template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef); 773 template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef); 774 template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef); 775 776 template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef); 777 template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef); 778 template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef); 779 template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef); 780 781 template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, uint8_t, uint8_t, 782 uint8_t, bool, InputFile *); 783 template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, uint8_t, uint8_t, 784 uint8_t, bool, InputFile *); 785 template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, uint8_t, uint8_t, 786 uint8_t, bool, InputFile *); 787 template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, uint8_t, uint8_t, 788 uint8_t, bool, InputFile *); 789 790 template void SymbolTable::addCombinedLTOObject<ELF32LE>(); 791 template void SymbolTable::addCombinedLTOObject<ELF32BE>(); 792 template void SymbolTable::addCombinedLTOObject<ELF64LE>(); 793 template void SymbolTable::addCombinedLTOObject<ELF64BE>(); 794 795 template Symbol * 796 SymbolTable::addLazyArchive<ELF32LE>(StringRef, ArchiveFile &, 797 const object::Archive::Symbol); 798 template Symbol * 799 SymbolTable::addLazyArchive<ELF32BE>(StringRef, ArchiveFile &, 800 const object::Archive::Symbol); 801 template Symbol * 802 SymbolTable::addLazyArchive<ELF64LE>(StringRef, ArchiveFile &, 803 const object::Archive::Symbol); 804 template Symbol * 805 SymbolTable::addLazyArchive<ELF64BE>(StringRef, ArchiveFile &, 806 const object::Archive::Symbol); 807 808 template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &); 809 template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &); 810 template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &); 811 template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &); 812 813 template void SymbolTable::addShared<ELF32LE>(StringRef, SharedFile<ELF32LE> &, 814 const typename ELF32LE::Sym &, 815 uint32_t Alignment, uint32_t); 816 template void SymbolTable::addShared<ELF32BE>(StringRef, SharedFile<ELF32BE> &, 817 const typename ELF32BE::Sym &, 818 uint32_t Alignment, uint32_t); 819 template void SymbolTable::addShared<ELF64LE>(StringRef, SharedFile<ELF64LE> &, 820 const typename ELF64LE::Sym &, 821 uint32_t Alignment, uint32_t); 822 template void SymbolTable::addShared<ELF64BE>(StringRef, SharedFile<ELF64BE> &, 823 const typename ELF64BE::Sym &, 824 uint32_t Alignment, uint32_t); 825 826 template void SymbolTable::fetchIfLazy<ELF32LE>(StringRef); 827 template void SymbolTable::fetchIfLazy<ELF32BE>(StringRef); 828 template void SymbolTable::fetchIfLazy<ELF64LE>(StringRef); 829 template void SymbolTable::fetchIfLazy<ELF64BE>(StringRef); 830 831 template void SymbolTable::scanShlibUndefined<ELF32LE>(); 832 template void SymbolTable::scanShlibUndefined<ELF32BE>(); 833 template void SymbolTable::scanShlibUndefined<ELF64LE>(); 834 template void SymbolTable::scanShlibUndefined<ELF64BE>(); 835