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 "Error.h" 20 #include "LinkerScript.h" 21 #include "Memory.h" 22 #include "Symbols.h" 23 #include "llvm/ADT/STLExtras.h" 24 25 using namespace llvm; 26 using namespace llvm::object; 27 using namespace llvm::ELF; 28 29 using namespace lld; 30 using namespace lld::elf; 31 32 // All input object files must be for the same architecture 33 // (e.g. it does not make sense to link x86 object files with 34 // MIPS object files.) This function checks for that error. 35 template <class ELFT> static bool isCompatible(InputFile *F) { 36 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F)) 37 return true; 38 39 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) { 40 if (Config->EMachine != EM_MIPS) 41 return true; 42 if (isMipsN32Abi(F) == Config->MipsN32Abi) 43 return true; 44 } 45 46 if (!Config->Emulation.empty()) 47 error(toString(F) + " is incompatible with " + Config->Emulation); 48 else 49 error(toString(F) + " is incompatible with " + toString(Config->FirstElf)); 50 return false; 51 } 52 53 // Add symbols in File to the symbol table. 54 template <class ELFT> void SymbolTable<ELFT>::addFile(InputFile *File) { 55 if (!Config->FirstElf && isa<ELFFileBase<ELFT>>(File)) 56 Config->FirstElf = File; 57 58 if (!isCompatible<ELFT>(File)) 59 return; 60 61 // Binary file 62 if (auto *F = dyn_cast<BinaryFile>(File)) { 63 BinaryFiles.push_back(F); 64 F->parse<ELFT>(); 65 return; 66 } 67 68 // .a file 69 if (auto *F = dyn_cast<ArchiveFile>(File)) { 70 F->parse<ELFT>(); 71 return; 72 } 73 74 // Lazy object file 75 if (auto *F = dyn_cast<LazyObjectFile>(File)) { 76 F->parse<ELFT>(); 77 return; 78 } 79 80 if (Config->Trace) 81 message(toString(File)); 82 83 // .so file 84 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) { 85 // DSOs are uniquified not by filename but by soname. 86 F->parseSoName(); 87 if (ErrorCount || !SoNames.insert(F->SoName).second) 88 return; 89 SharedFiles.push_back(F); 90 F->parseRest(); 91 return; 92 } 93 94 // LLVM bitcode file 95 if (auto *F = dyn_cast<BitcodeFile>(File)) { 96 BitcodeFiles.push_back(F); 97 F->parse<ELFT>(ComdatGroups); 98 return; 99 } 100 101 // Regular object file 102 auto *F = cast<ObjectFile<ELFT>>(File); 103 ObjectFiles.push_back(F); 104 F->parse(ComdatGroups); 105 } 106 107 // This function is where all the optimizations of link-time 108 // optimization happens. When LTO is in use, some input files are 109 // not in native object file format but in the LLVM bitcode format. 110 // This function compiles bitcode files into a few big native files 111 // using LLVM functions and replaces bitcode symbols with the results. 112 // Because all bitcode files that consist of a program are passed 113 // to the compiler at once, it can do whole-program optimization. 114 template <class ELFT> void SymbolTable<ELFT>::addCombinedLTOObject() { 115 if (BitcodeFiles.empty()) 116 return; 117 118 // Compile bitcode files and replace bitcode symbols. 119 LTO.reset(new BitcodeCompiler); 120 for (BitcodeFile *F : BitcodeFiles) 121 LTO->add(*F); 122 123 for (InputFile *File : LTO->compile()) { 124 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(File); 125 DenseSet<StringRef> DummyGroups; 126 Obj->parse(DummyGroups); 127 ObjectFiles.push_back(Obj); 128 } 129 } 130 131 template <class ELFT> 132 DefinedRegular *SymbolTable<ELFT>::addAbsolute(StringRef Name, 133 uint8_t Visibility, 134 uint8_t Binding) { 135 Symbol *Sym = 136 addRegular(Name, Visibility, STT_NOTYPE, 0, 0, Binding, nullptr, nullptr); 137 return cast<DefinedRegular>(Sym->body()); 138 } 139 140 // Add Name as an "ignored" symbol. An ignored symbol is a regular 141 // linker-synthesized defined symbol, but is only defined if needed. 142 template <class ELFT> 143 DefinedRegular *SymbolTable<ELFT>::addIgnored(StringRef Name, 144 uint8_t Visibility) { 145 SymbolBody *S = find(Name); 146 if (!S || S->isInCurrentDSO()) 147 return nullptr; 148 return addAbsolute(Name, Visibility); 149 } 150 151 // Set a flag for --trace-symbol so that we can print out a log message 152 // if a new symbol with the same name is inserted into the symbol table. 153 template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) { 154 Symtab.insert({CachedHashStringRef(Name), {-1, true}}); 155 } 156 157 // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM. 158 // Used to implement --wrap. 159 template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) { 160 SymbolBody *B = find(Name); 161 if (!B) 162 return; 163 Symbol *Sym = B->symbol(); 164 Symbol *Real = addUndefined(Saver.save("__real_" + Name)); 165 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name)); 166 167 // We rename symbols by replacing the old symbol's SymbolBody with the new 168 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the 169 // old symbol to instead refer to the new symbol. 170 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body)); 171 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body)); 172 } 173 174 // Creates alias for symbol. Used to implement --defsym=ALIAS=SYM. 175 template <class ELFT> 176 void SymbolTable<ELFT>::alias(StringRef Alias, StringRef Name) { 177 SymbolBody *B = find(Name); 178 if (!B) { 179 error("-defsym: undefined symbol: " + Name); 180 return; 181 } 182 Symbol *Sym = B->symbol(); 183 Symbol *AliasSym = addUndefined(Alias); 184 memcpy(AliasSym->Body.buffer, Sym->Body.buffer, sizeof(AliasSym->Body)); 185 } 186 187 static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { 188 if (VA == STV_DEFAULT) 189 return VB; 190 if (VB == STV_DEFAULT) 191 return VA; 192 return std::min(VA, VB); 193 } 194 195 // Find an existing symbol or create and insert a new one. 196 template <class ELFT> 197 std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef Name) { 198 auto P = Symtab.insert( 199 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)}); 200 SymIndex &V = P.first->second; 201 bool IsNew = P.second; 202 203 if (V.Idx == -1) { 204 IsNew = true; 205 V = SymIndex((int)SymVector.size(), true); 206 } 207 208 Symbol *Sym; 209 if (IsNew) { 210 Sym = make<Symbol>(); 211 Sym->InVersionScript = false; 212 Sym->Binding = STB_WEAK; 213 Sym->Visibility = STV_DEFAULT; 214 Sym->IsUsedInRegularObj = false; 215 Sym->ExportDynamic = false; 216 Sym->Traced = V.Traced; 217 Sym->VersionId = Config->DefaultSymbolVersion; 218 SymVector.push_back(Sym); 219 } else { 220 Sym = SymVector[V.Idx]; 221 } 222 return {Sym, IsNew}; 223 } 224 225 // Find an existing symbol or create and insert a new one, then apply the given 226 // attributes. 227 template <class ELFT> 228 std::pair<Symbol *, bool> 229 SymbolTable<ELFT>::insert(StringRef Name, uint8_t Type, uint8_t Visibility, 230 bool CanOmitFromDynSym, InputFile *File) { 231 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind; 232 Symbol *S; 233 bool WasInserted; 234 std::tie(S, WasInserted) = insert(Name); 235 236 // Merge in the new symbol's visibility. 237 S->Visibility = getMinVisibility(S->Visibility, Visibility); 238 239 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic)) 240 S->ExportDynamic = true; 241 242 if (IsUsedInRegularObj) 243 S->IsUsedInRegularObj = true; 244 245 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType && 246 ((Type == STT_TLS) != S->body()->isTls())) { 247 error("TLS attribute mismatch: " + toString(*S->body()) + 248 "\n>>> defined in " + toString(S->body()->File) + 249 "\n>>> defined in " + toString(File)); 250 } 251 252 return {S, WasInserted}; 253 } 254 255 template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) { 256 return addUndefined(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT, 257 /*Type*/ 0, 258 /*CanOmitFromDynSym*/ false, /*File*/ nullptr); 259 } 260 261 static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; } 262 263 template <class ELFT> 264 Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, bool IsLocal, 265 uint8_t Binding, uint8_t StOther, 266 uint8_t Type, bool CanOmitFromDynSym, 267 InputFile *File) { 268 Symbol *S; 269 bool WasInserted; 270 uint8_t Visibility = getVisibility(StOther); 271 std::tie(S, WasInserted) = 272 insert(Name, Type, Visibility, CanOmitFromDynSym, File); 273 // An undefined symbol with non default visibility must be satisfied 274 // in the same DSO. 275 if (WasInserted || 276 (isa<SharedSymbol>(S->body()) && Visibility != STV_DEFAULT)) { 277 S->Binding = Binding; 278 replaceBody<Undefined>(S, Name, IsLocal, StOther, Type, File); 279 return S; 280 } 281 if (Binding != STB_WEAK) { 282 SymbolBody *B = S->body(); 283 if (B->isShared() || B->isLazy() || B->isUndefined()) 284 S->Binding = Binding; 285 if (auto *SS = dyn_cast<SharedSymbol>(B)) 286 cast<SharedFile<ELFT>>(SS->File)->IsUsed = true; 287 } 288 if (auto *L = dyn_cast<Lazy>(S->body())) { 289 // An undefined weak will not fetch archive members, but we have to remember 290 // its type. See also comment in addLazyArchive. 291 if (S->isWeak()) 292 L->Type = Type; 293 else if (InputFile *F = L->fetch()) 294 addFile(F); 295 } 296 return S; 297 } 298 299 // We have a new defined symbol with the specified binding. Return 1 if the new 300 // symbol should win, -1 if the new symbol should lose, or 0 if both symbols are 301 // strong defined symbols. 302 static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) { 303 if (WasInserted) 304 return 1; 305 SymbolBody *Body = S->body(); 306 if (Body->isLazy() || !Body->isInCurrentDSO()) 307 return 1; 308 if (Binding == STB_WEAK) 309 return -1; 310 if (S->isWeak()) 311 return 1; 312 return 0; 313 } 314 315 // We have a new non-common defined symbol with the specified binding. Return 1 316 // if the new symbol should win, -1 if the new symbol should lose, or 0 if there 317 // is a conflict. If the new symbol wins, also update the binding. 318 template <typename ELFT> 319 static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding, 320 bool IsAbsolute, typename ELFT::uint Value) { 321 if (int Cmp = compareDefined(S, WasInserted, Binding)) { 322 if (Cmp > 0) 323 S->Binding = Binding; 324 return Cmp; 325 } 326 SymbolBody *B = S->body(); 327 if (isa<DefinedCommon>(B)) { 328 // Non-common symbols take precedence over common symbols. 329 if (Config->WarnCommon) 330 warn("common " + S->body()->getName() + " is overridden"); 331 return 1; 332 } else if (auto *R = dyn_cast<DefinedRegular>(B)) { 333 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute && 334 R->Value == Value) 335 return -1; 336 } 337 return 0; 338 } 339 340 template <class ELFT> 341 Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size, 342 uint32_t Alignment, uint8_t Binding, 343 uint8_t StOther, uint8_t Type, 344 InputFile *File) { 345 Symbol *S; 346 bool WasInserted; 347 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther), 348 /*CanOmitFromDynSym*/ false, File); 349 int Cmp = compareDefined(S, WasInserted, Binding); 350 if (Cmp > 0) { 351 S->Binding = Binding; 352 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File); 353 } else if (Cmp == 0) { 354 auto *C = dyn_cast<DefinedCommon>(S->body()); 355 if (!C) { 356 // Non-common symbols take precedence over common symbols. 357 if (Config->WarnCommon) 358 warn("common " + S->body()->getName() + " is overridden"); 359 return S; 360 } 361 362 if (Config->WarnCommon) 363 warn("multiple common of " + S->body()->getName()); 364 365 Alignment = C->Alignment = std::max(C->Alignment, Alignment); 366 if (Size > C->Size) 367 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File); 368 } 369 return S; 370 } 371 372 static void warnOrError(const Twine &Msg) { 373 if (Config->AllowMultipleDefinition) 374 warn(Msg); 375 else 376 error(Msg); 377 } 378 379 static void reportDuplicate(SymbolBody *Sym, InputFile *NewFile) { 380 warnOrError("duplicate symbol: " + toString(*Sym) + 381 "\n>>> defined in " + toString(Sym->File) + 382 "\n>>> defined in " + toString(NewFile)); 383 } 384 385 template <class ELFT> 386 static void reportDuplicate(SymbolBody *Sym, InputSectionBase *ErrSec, 387 typename ELFT::uint ErrOffset) { 388 DefinedRegular *D = dyn_cast<DefinedRegular>(Sym); 389 if (!D || !D->Section || !ErrSec) { 390 reportDuplicate(Sym, ErrSec ? ErrSec->getFile<ELFT>() : nullptr); 391 return; 392 } 393 394 // Construct and print an error message in the form of: 395 // 396 // ld.lld: error: duplicate symbol: foo 397 // >>> defined at bar.c:30 398 // >>> bar.o (/home/alice/src/bar.o) 399 // >>> defined at baz.c:563 400 // >>> baz.o in archive libbaz.a 401 auto *Sec1 = cast<InputSectionBase>(D->Section); 402 std::string Src1 = Sec1->getSrcMsg<ELFT>(D->Value); 403 std::string Obj1 = Sec1->getObjMsg<ELFT>(D->Value); 404 std::string Src2 = ErrSec->getSrcMsg<ELFT>(ErrOffset); 405 std::string Obj2 = ErrSec->getObjMsg<ELFT>(ErrOffset); 406 407 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at "; 408 if (!Src1.empty()) 409 Msg += Src1 + "\n>>> "; 410 Msg += Obj1 + "\n>>> defined at "; 411 if (!Src2.empty()) 412 Msg += Src2 + "\n>>> "; 413 Msg += Obj2; 414 warnOrError(Msg); 415 } 416 417 template <typename ELFT> 418 Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t StOther, 419 uint8_t Type, uint64_t Value, 420 uint64_t Size, uint8_t Binding, 421 SectionBase *Section, InputFile *File) { 422 Symbol *S; 423 bool WasInserted; 424 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther), 425 /*CanOmitFromDynSym*/ false, File); 426 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding, 427 Section == nullptr, Value); 428 if (Cmp > 0) 429 replaceBody<DefinedRegular>(S, Name, /*IsLocal=*/false, StOther, Type, 430 Value, Size, Section, File); 431 else if (Cmp == 0) 432 reportDuplicate<ELFT>(S->body(), 433 dyn_cast_or_null<InputSectionBase>(Section), Value); 434 return S; 435 } 436 437 template <typename ELFT> 438 void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *File, StringRef Name, 439 const Elf_Sym &Sym, 440 const typename ELFT::Verdef *Verdef) { 441 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT 442 // as the visibility, which will leave the visibility in the symbol table 443 // unchanged. 444 Symbol *S; 445 bool WasInserted; 446 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT, 447 /*CanOmitFromDynSym*/ true, File); 448 // Make sure we preempt DSO symbols with default visibility. 449 if (Sym.getVisibility() == STV_DEFAULT) 450 S->ExportDynamic = true; 451 452 SymbolBody *Body = S->body(); 453 // An undefined symbol with non default visibility must be satisfied 454 // in the same DSO. 455 if (WasInserted || 456 (isa<Undefined>(Body) && Body->getVisibility() == STV_DEFAULT)) { 457 replaceBody<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(), &Sym, 458 Verdef); 459 if (!S->isWeak()) 460 File->IsUsed = true; 461 } 462 } 463 464 template <class ELFT> 465 Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding, 466 uint8_t StOther, uint8_t Type, 467 bool CanOmitFromDynSym, BitcodeFile *F) { 468 Symbol *S; 469 bool WasInserted; 470 std::tie(S, WasInserted) = 471 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F); 472 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding, 473 /*IsAbs*/ false, /*Value*/ 0); 474 if (Cmp > 0) 475 replaceBody<DefinedRegular>(S, Name, /*IsLocal=*/false, StOther, Type, 0, 0, 476 nullptr, F); 477 else if (Cmp == 0) 478 reportDuplicate(S->body(), F); 479 return S; 480 } 481 482 template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) { 483 auto It = Symtab.find(CachedHashStringRef(Name)); 484 if (It == Symtab.end()) 485 return nullptr; 486 SymIndex V = It->second; 487 if (V.Idx == -1) 488 return nullptr; 489 return SymVector[V.Idx]->body(); 490 } 491 492 template <class ELFT> 493 SymbolBody *SymbolTable<ELFT>::findInCurrentDSO(StringRef Name) { 494 if (SymbolBody *S = find(Name)) 495 if (S->isInCurrentDSO()) 496 return S; 497 return nullptr; 498 } 499 500 template <class ELFT> 501 void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F, 502 const object::Archive::Symbol Sym) { 503 Symbol *S; 504 bool WasInserted; 505 StringRef Name = Sym.getName(); 506 std::tie(S, WasInserted) = insert(Name); 507 if (WasInserted) { 508 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType); 509 return; 510 } 511 if (!S->body()->isUndefined()) 512 return; 513 514 // Weak undefined symbols should not fetch members from archives. If we were 515 // to keep old symbol we would not know that an archive member was available 516 // if a strong undefined symbol shows up afterwards in the link. If a strong 517 // undefined symbol never shows up, this lazy symbol will get to the end of 518 // the link and must be treated as the weak undefined one. We already marked 519 // this symbol as used when we added it to the symbol table, but we also need 520 // to preserve its type. FIXME: Move the Type field to Symbol. 521 if (S->isWeak()) { 522 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type); 523 return; 524 } 525 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym); 526 if (!MBInfo.first.getBuffer().empty()) 527 addFile(createObjectFile(MBInfo.first, F->getName(), MBInfo.second)); 528 } 529 530 template <class ELFT> 531 void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) { 532 Symbol *S; 533 bool WasInserted; 534 std::tie(S, WasInserted) = insert(Name); 535 if (WasInserted) { 536 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType); 537 return; 538 } 539 if (!S->body()->isUndefined()) 540 return; 541 542 // See comment for addLazyArchive above. 543 if (S->isWeak()) 544 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type); 545 else if (InputFile *F = Obj.fetch()) 546 addFile(F); 547 } 548 549 // Process undefined (-u) flags by loading lazy symbols named by those flags. 550 template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() { 551 for (StringRef S : Config->Undefined) 552 if (auto *L = dyn_cast_or_null<Lazy>(find(S))) 553 if (InputFile *File = L->fetch()) 554 addFile(File); 555 } 556 557 // This function takes care of the case in which shared libraries depend on 558 // the user program (not the other way, which is usual). Shared libraries 559 // may have undefined symbols, expecting that the user program provides 560 // the definitions for them. An example is BSD's __progname symbol. 561 // We need to put such symbols to the main program's .dynsym so that 562 // shared libraries can find them. 563 // Except this, we ignore undefined symbols in DSOs. 564 template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() { 565 for (SharedFile<ELFT> *File : SharedFiles) { 566 for (StringRef U : File->getUndefinedSymbols()) { 567 SymbolBody *Sym = find(U); 568 if (!Sym || !Sym->isDefined()) 569 continue; 570 Sym->symbol()->ExportDynamic = true; 571 572 // If -dynamic-list is given, the default version is set to 573 // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym. 574 // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were 575 // specified by -dynamic-list. 576 Sym->symbol()->VersionId = VER_NDX_GLOBAL; 577 } 578 } 579 } 580 581 // Initialize DemangledSyms with a map from demangled symbols to symbol 582 // objects. Used to handle "extern C++" directive in version scripts. 583 // 584 // The map will contain all demangled symbols. That can be very large, 585 // and in LLD we generally want to avoid do anything for each symbol. 586 // Then, why are we doing this? Here's why. 587 // 588 // Users can use "extern C++ {}" directive to match against demangled 589 // C++ symbols. For example, you can write a pattern such as 590 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this 591 // other than trying to match a pattern against all demangled symbols. 592 // So, if "extern C++" feature is used, we need to demangle all known 593 // symbols. 594 template <class ELFT> 595 StringMap<std::vector<SymbolBody *>> &SymbolTable<ELFT>::getDemangledSyms() { 596 if (!DemangledSyms) { 597 DemangledSyms.emplace(); 598 for (Symbol *Sym : SymVector) { 599 SymbolBody *B = Sym->body(); 600 if (B->isUndefined()) 601 continue; 602 if (Optional<std::string> S = demangle(B->getName())) 603 (*DemangledSyms)[*S].push_back(B); 604 else 605 (*DemangledSyms)[B->getName()].push_back(B); 606 } 607 } 608 return *DemangledSyms; 609 } 610 611 template <class ELFT> 612 std::vector<SymbolBody *> SymbolTable<ELFT>::findByVersion(SymbolVersion Ver) { 613 if (Ver.IsExternCpp) 614 return getDemangledSyms().lookup(Ver.Name); 615 if (SymbolBody *B = find(Ver.Name)) 616 if (!B->isUndefined()) 617 return {B}; 618 return {}; 619 } 620 621 template <class ELFT> 622 std::vector<SymbolBody *> 623 SymbolTable<ELFT>::findAllByVersion(SymbolVersion Ver) { 624 std::vector<SymbolBody *> Res; 625 StringMatcher M(Ver.Name); 626 627 if (Ver.IsExternCpp) { 628 for (auto &P : getDemangledSyms()) 629 if (M.match(P.first())) 630 Res.insert(Res.end(), P.second.begin(), P.second.end()); 631 return Res; 632 } 633 634 for (Symbol *Sym : SymVector) { 635 SymbolBody *B = Sym->body(); 636 if (!B->isUndefined() && M.match(B->getName())) 637 Res.push_back(B); 638 } 639 return Res; 640 } 641 642 // If there's only one anonymous version definition in a version 643 // script file, the script does not actually define any symbol version, 644 // but just specifies symbols visibilities. 645 template <class ELFT> void SymbolTable<ELFT>::handleAnonymousVersion() { 646 for (SymbolVersion &Ver : Config->VersionScriptGlobals) 647 assignExactVersion(Ver, VER_NDX_GLOBAL, "global"); 648 for (SymbolVersion &Ver : Config->VersionScriptGlobals) 649 assignWildcardVersion(Ver, VER_NDX_GLOBAL); 650 for (SymbolVersion &Ver : Config->VersionScriptLocals) 651 assignExactVersion(Ver, VER_NDX_LOCAL, "local"); 652 for (SymbolVersion &Ver : Config->VersionScriptLocals) 653 assignWildcardVersion(Ver, VER_NDX_LOCAL); 654 } 655 656 // Set symbol versions to symbols. This function handles patterns 657 // containing no wildcard characters. 658 template <class ELFT> 659 void SymbolTable<ELFT>::assignExactVersion(SymbolVersion Ver, uint16_t VersionId, 660 StringRef VersionName) { 661 if (Ver.HasWildcard) 662 return; 663 664 // Get a list of symbols which we need to assign the version to. 665 std::vector<SymbolBody *> Syms = findByVersion(Ver); 666 if (Syms.empty()) { 667 if (Config->NoUndefinedVersion) 668 error("version script assignment of '" + VersionName + "' to symbol '" + 669 Ver.Name + "' failed: symbol not defined"); 670 return; 671 } 672 673 // Assign the version. 674 for (SymbolBody *B : Syms) { 675 Symbol *Sym = B->symbol(); 676 if (Sym->InVersionScript) 677 warn("duplicate symbol '" + Ver.Name + "' in version script"); 678 Sym->VersionId = VersionId; 679 Sym->InVersionScript = true; 680 } 681 } 682 683 template <class ELFT> 684 void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver, 685 uint16_t VersionId) { 686 if (!Ver.HasWildcard) 687 return; 688 std::vector<SymbolBody *> Syms = findAllByVersion(Ver); 689 690 // Exact matching takes precendence over fuzzy matching, 691 // so we set a version to a symbol only if no version has been assigned 692 // to the symbol. This behavior is compatible with GNU. 693 for (SymbolBody *B : Syms) 694 if (B->symbol()->VersionId == Config->DefaultSymbolVersion) 695 B->symbol()->VersionId = VersionId; 696 } 697 698 // This function processes version scripts by updating VersionId 699 // member of symbols. 700 template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() { 701 // Symbol themselves might know their versions because symbols 702 // can contain versions in the form of <name>@<version>. 703 // Let them parse their names. 704 if (!Config->VersionDefinitions.empty()) 705 for (Symbol *Sym : SymVector) 706 Sym->body()->parseSymbolVersion(); 707 708 // Handle edge cases first. 709 handleAnonymousVersion(); 710 711 if (Config->VersionDefinitions.empty()) 712 return; 713 714 // Now we have version definitions, so we need to set version ids to symbols. 715 // Each version definition has a glob pattern, and all symbols that match 716 // with the pattern get that version. 717 718 // First, we assign versions to exact matching symbols, 719 // i.e. version definitions not containing any glob meta-characters. 720 for (VersionDefinition &V : Config->VersionDefinitions) 721 for (SymbolVersion &Ver : V.Globals) 722 assignExactVersion(Ver, V.Id, V.Name); 723 724 // Next, we assign versions to fuzzy matching symbols, 725 // i.e. version definitions containing glob meta-characters. 726 // Note that because the last match takes precedence over previous matches, 727 // we iterate over the definitions in the reverse order. 728 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions)) 729 for (SymbolVersion &Ver : V.Globals) 730 assignWildcardVersion(Ver, V.Id); 731 } 732 733 template class elf::SymbolTable<ELF32LE>; 734 template class elf::SymbolTable<ELF32BE>; 735 template class elf::SymbolTable<ELF64LE>; 736 template class elf::SymbolTable<ELF64BE>; 737