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