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(*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->isUndefined()) 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 template <class ELFT> 247 Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, bool IsLocal, 248 uint8_t Binding, uint8_t StOther, 249 uint8_t Type, bool CanOmitFromDynSym, 250 InputFile *File) { 251 Symbol *S; 252 bool WasInserted; 253 std::tie(S, WasInserted) = 254 insert(Name, Type, StOther & 3, CanOmitFromDynSym, File); 255 if (WasInserted) { 256 S->Binding = Binding; 257 replaceBody<Undefined>(S, Name, IsLocal, StOther, Type, File); 258 return S; 259 } 260 if (Binding != STB_WEAK) { 261 if (S->body()->isShared() || S->body()->isLazy()) 262 S->Binding = Binding; 263 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body())) 264 SS->file()->IsUsed = true; 265 } 266 if (auto *L = dyn_cast<Lazy>(S->body())) { 267 // An undefined weak will not fetch archive members, but we have to remember 268 // its type. See also comment in addLazyArchive. 269 if (S->isWeak()) 270 L->Type = Type; 271 else if (InputFile *F = L->fetch()) 272 addFile(F); 273 } 274 return S; 275 } 276 277 // We have a new defined symbol with the specified binding. Return 1 if the new 278 // symbol should win, -1 if the new symbol should lose, or 0 if both symbols are 279 // strong defined symbols. 280 static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) { 281 if (WasInserted) 282 return 1; 283 SymbolBody *Body = S->body(); 284 if (Body->isLazy() || Body->isUndefined() || Body->isShared()) 285 return 1; 286 if (Binding == STB_WEAK) 287 return -1; 288 if (S->isWeak()) 289 return 1; 290 return 0; 291 } 292 293 // We have a new non-common defined symbol with the specified binding. Return 1 294 // if the new symbol should win, -1 if the new symbol should lose, or 0 if there 295 // is a conflict. If the new symbol wins, also update the binding. 296 template <typename ELFT> 297 static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding, 298 bool IsAbsolute, typename ELFT::uint Value) { 299 if (int Cmp = compareDefined(S, WasInserted, Binding)) { 300 if (Cmp > 0) 301 S->Binding = Binding; 302 return Cmp; 303 } 304 SymbolBody *B = S->body(); 305 if (isa<DefinedCommon>(B)) { 306 // Non-common symbols take precedence over common symbols. 307 if (Config->WarnCommon) 308 warn("common " + S->body()->getName() + " is overridden"); 309 return 1; 310 } else if (auto *R = dyn_cast<DefinedRegular<ELFT>>(B)) { 311 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute && 312 R->Value == Value) 313 return -1; 314 } 315 return 0; 316 } 317 318 template <class ELFT> 319 Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size, 320 uint64_t Alignment, uint8_t Binding, 321 uint8_t StOther, uint8_t Type, 322 InputFile *File) { 323 Symbol *S; 324 bool WasInserted; 325 std::tie(S, WasInserted) = 326 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, File); 327 int Cmp = compareDefined(S, WasInserted, Binding); 328 if (Cmp > 0) { 329 S->Binding = Binding; 330 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File); 331 } else if (Cmp == 0) { 332 auto *C = dyn_cast<DefinedCommon>(S->body()); 333 if (!C) { 334 // Non-common symbols take precedence over common symbols. 335 if (Config->WarnCommon) 336 warn("common " + S->body()->getName() + " is overridden"); 337 return S; 338 } 339 340 if (Config->WarnCommon) 341 warn("multiple common of " + S->body()->getName()); 342 343 Alignment = C->Alignment = std::max(C->Alignment, Alignment); 344 if (Size > C->Size) 345 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File); 346 } 347 return S; 348 } 349 350 static void print(const Twine &Msg) { 351 if (Config->AllowMultipleDefinition) 352 warn(Msg); 353 else 354 error(Msg); 355 } 356 357 static void reportDuplicate(SymbolBody *Existing, InputFile *NewFile) { 358 print("duplicate symbol " + conflictMsg(Existing, NewFile)); 359 } 360 361 template <class ELFT> 362 static void reportDuplicate(SymbolBody *Existing, 363 InputSectionBase<ELFT> *ErrSec, 364 typename ELFT::uint ErrOffset) { 365 DefinedRegular<ELFT> *D = dyn_cast<DefinedRegular<ELFT>>(Existing); 366 if (!D || !D->Section || !ErrSec) { 367 reportDuplicate(Existing, ErrSec ? ErrSec->getFile() : nullptr); 368 return; 369 } 370 371 std::string OldLoc = D->Section->getLocation(D->Value); 372 std::string NewLoc = ErrSec->getLocation(ErrOffset); 373 374 print(NewLoc + ": duplicate symbol '" + toString(*Existing) + "'"); 375 print(OldLoc + ": previous definition was here"); 376 } 377 378 template <typename ELFT> 379 Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t StOther, 380 uint8_t Type, uintX_t Value, uintX_t Size, 381 uint8_t Binding, 382 InputSectionBase<ELFT> *Section, 383 InputFile *File) { 384 Symbol *S; 385 bool WasInserted; 386 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, 387 /*CanOmitFromDynSym*/ false, File); 388 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding, 389 Section == nullptr, Value); 390 if (Cmp > 0) 391 replaceBody<DefinedRegular<ELFT>>(S, Name, /*IsLocal=*/false, StOther, Type, 392 Value, Size, Section, File); 393 else if (Cmp == 0) 394 reportDuplicate(S->body(), Section, Value); 395 return S; 396 } 397 398 template <typename ELFT> 399 Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N, 400 const OutputSectionBase *Section, 401 uintX_t Value, uint8_t StOther) { 402 Symbol *S; 403 bool WasInserted; 404 std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3, 405 /*CanOmitFromDynSym*/ false, nullptr); 406 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, STB_GLOBAL, 407 /*IsAbsolute*/ false, /*Value*/ 0); 408 if (Cmp > 0) 409 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section); 410 else if (Cmp == 0) 411 reportDuplicate(S->body(), nullptr); 412 return S; 413 } 414 415 template <typename ELFT> 416 void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name, 417 const Elf_Sym &Sym, 418 const typename ELFT::Verdef *Verdef) { 419 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT 420 // as the visibility, which will leave the visibility in the symbol table 421 // unchanged. 422 Symbol *S; 423 bool WasInserted; 424 std::tie(S, WasInserted) = 425 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, F); 426 // Make sure we preempt DSO symbols with default visibility. 427 if (Sym.getVisibility() == STV_DEFAULT) { 428 S->ExportDynamic = true; 429 // Exporting preempting symbols takes precedence over linker scripts. 430 if (S->VersionId == VER_NDX_LOCAL) 431 S->VersionId = VER_NDX_GLOBAL; 432 } 433 if (WasInserted || isa<Undefined>(S->body())) { 434 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef); 435 if (!S->isWeak()) 436 F->IsUsed = true; 437 } 438 } 439 440 template <class ELFT> 441 Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, uint8_t Binding, 442 uint8_t StOther, uint8_t Type, 443 bool CanOmitFromDynSym, BitcodeFile *F) { 444 Symbol *S; 445 bool WasInserted; 446 std::tie(S, WasInserted) = 447 insert(Name, Type, StOther & 3, CanOmitFromDynSym, F); 448 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding, 449 /*IsAbs*/ false, /*Value*/ 0); 450 if (Cmp > 0) 451 replaceBody<DefinedRegular<ELFT>>(S, Name, /*IsLocal=*/false, StOther, Type, 452 0, 0, nullptr, F); 453 else if (Cmp == 0) 454 reportDuplicate(S->body(), F); 455 return S; 456 } 457 458 template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) { 459 auto It = Symtab.find(CachedHashStringRef(Name)); 460 if (It == Symtab.end()) 461 return nullptr; 462 SymIndex V = It->second; 463 if (V.Idx == -1) 464 return nullptr; 465 return SymVector[V.Idx]->body(); 466 } 467 468 template <class ELFT> 469 void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F, 470 const object::Archive::Symbol Sym) { 471 Symbol *S; 472 bool WasInserted; 473 StringRef Name = Sym.getName(); 474 std::tie(S, WasInserted) = insert(Name); 475 if (WasInserted) { 476 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType); 477 return; 478 } 479 if (!S->body()->isUndefined()) 480 return; 481 482 // Weak undefined symbols should not fetch members from archives. If we were 483 // to keep old symbol we would not know that an archive member was available 484 // if a strong undefined symbol shows up afterwards in the link. If a strong 485 // undefined symbol never shows up, this lazy symbol will get to the end of 486 // the link and must be treated as the weak undefined one. We already marked 487 // this symbol as used when we added it to the symbol table, but we also need 488 // to preserve its type. FIXME: Move the Type field to Symbol. 489 if (S->isWeak()) { 490 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type); 491 return; 492 } 493 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym); 494 if (!MBInfo.first.getBuffer().empty()) 495 addFile(createObjectFile(MBInfo.first, F->getName(), MBInfo.second)); 496 } 497 498 template <class ELFT> 499 void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) { 500 Symbol *S; 501 bool WasInserted; 502 std::tie(S, WasInserted) = insert(Name); 503 if (WasInserted) { 504 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType); 505 return; 506 } 507 if (!S->body()->isUndefined()) 508 return; 509 510 // See comment for addLazyArchive above. 511 if (S->isWeak()) { 512 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type); 513 } else { 514 MemoryBufferRef MBRef = Obj.getBuffer(); 515 if (!MBRef.getBuffer().empty()) 516 addFile(createObjectFile(MBRef)); 517 } 518 } 519 520 // Process undefined (-u) flags by loading lazy symbols named by those flags. 521 template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() { 522 for (StringRef S : Config->Undefined) 523 if (auto *L = dyn_cast_or_null<Lazy>(find(S))) 524 if (InputFile *File = L->fetch()) 525 addFile(File); 526 } 527 528 // This function takes care of the case in which shared libraries depend on 529 // the user program (not the other way, which is usual). Shared libraries 530 // may have undefined symbols, expecting that the user program provides 531 // the definitions for them. An example is BSD's __progname symbol. 532 // We need to put such symbols to the main program's .dynsym so that 533 // shared libraries can find them. 534 // Except this, we ignore undefined symbols in DSOs. 535 template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() { 536 for (SharedFile<ELFT> *File : SharedFiles) 537 for (StringRef U : File->getUndefinedSymbols()) 538 if (SymbolBody *Sym = find(U)) 539 if (Sym->isDefined()) 540 Sym->symbol()->ExportDynamic = true; 541 } 542 543 // Initialize DemangledSyms with a map from demangled symbols to symbol 544 // objects. Used to handle "extern C++" directive in version scripts. 545 // 546 // The map will contain all demangled symbols. That can be very large, 547 // and in LLD we generally want to avoid do anything for each symbol. 548 // Then, why are we doing this? Here's why. 549 // 550 // Users can use "extern C++ {}" directive to match against demangled 551 // C++ symbols. For example, you can write a pattern such as 552 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this 553 // other than trying to match a pattern against all demangled symbols. 554 // So, if "extern C++" feature is used, we need to demangle all known 555 // symbols. 556 template <class ELFT> 557 void SymbolTable<ELFT>::initDemangledSyms() { 558 if (DemangledSyms) 559 return; 560 DemangledSyms.emplace(); 561 562 for (Symbol *Sym : SymVector) { 563 SymbolBody *B = Sym->body(); 564 if (Optional<std::string> S = demangle(B->getName())) 565 (*DemangledSyms)[*S].push_back(B); 566 else 567 (*DemangledSyms)[B->getName()].push_back(B); 568 } 569 } 570 571 template <class ELFT> 572 std::vector<SymbolBody *> SymbolTable<ELFT>::findByVersion(SymbolVersion Ver) { 573 if (Ver.IsExternCpp) { 574 initDemangledSyms(); 575 auto I = DemangledSyms->find(Ver.Name); 576 if (I != DemangledSyms->end()) 577 return I->second; 578 return {}; 579 } 580 std::vector<SymbolBody *> Syms; 581 Syms.push_back(find(Ver.Name)); 582 return Syms; 583 } 584 585 template <class ELFT> 586 std::vector<SymbolBody *> 587 SymbolTable<ELFT>::findAllByVersion(SymbolVersion Ver) { 588 std::vector<SymbolBody *> Res; 589 StringMatcher M({Ver.Name}); 590 591 if (Ver.IsExternCpp) { 592 initDemangledSyms(); 593 for (auto &P : *DemangledSyms) 594 if (M.match(P.first())) 595 for (SymbolBody *Body : P.second) 596 if (!Body->isUndefined()) 597 Res.push_back(Body); 598 return Res; 599 } 600 601 for (Symbol *Sym : SymVector) { 602 SymbolBody *B = Sym->body(); 603 StringRef Name = B->getName(); 604 if (!B->isUndefined() && M.match(Name)) 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. We assume that the script was 613 // in the form of { global: foo; bar; local *; }. So, local is default. 614 // In this function, we make specified symbols global. 615 template <class ELFT> void SymbolTable<ELFT>::handleAnonymousVersion() { 616 for (SymbolVersion &Ver : Config->VersionScriptGlobals) { 617 if (Ver.HasWildcard) { 618 for (SymbolBody *B : findAllByVersion(Ver)) 619 B->symbol()->VersionId = VER_NDX_GLOBAL; 620 continue; 621 } 622 for (SymbolBody *B : findByVersion(Ver)) 623 if (B) 624 B->symbol()->VersionId = VER_NDX_GLOBAL; 625 } 626 } 627 628 // Set symbol versions to symbols. This function handles patterns 629 // containing no wildcard characters. 630 template <class ELFT> 631 void SymbolTable<ELFT>::assignExactVersion(SymbolVersion Ver, uint16_t VersionId, 632 StringRef VersionName) { 633 if (Ver.HasWildcard) 634 return; 635 636 // Get a list of symbols which we need to assign the version to. 637 std::vector<SymbolBody *> Syms = findByVersion(Ver); 638 639 // Assign the version. 640 for (SymbolBody *B : Syms) { 641 if (!B || B->isUndefined()) { 642 if (Config->NoUndefinedVersion) 643 error("version script assignment of '" + VersionName + "' to symbol '" + 644 Ver.Name + "' failed: symbol not defined"); 645 continue; 646 } 647 648 Symbol *Sym = B->symbol(); 649 if (Sym->InVersionScript) 650 warn("duplicate symbol '" + Ver.Name + "' in version script"); 651 Sym->VersionId = VersionId; 652 Sym->InVersionScript = true; 653 } 654 } 655 656 template <class ELFT> 657 void SymbolTable<ELFT>::assignWildcardVersion(SymbolVersion Ver, 658 uint16_t VersionId) { 659 if (!Ver.HasWildcard) 660 return; 661 std::vector<SymbolBody *> Syms = findAllByVersion(Ver); 662 663 // Exact matching takes precendence over fuzzy matching, 664 // so we set a version to a symbol only if no version has been assigned 665 // to the symbol. This behavior is compatible with GNU. 666 for (SymbolBody *B : Syms) 667 if (B->symbol()->VersionId == Config->DefaultSymbolVersion) 668 B->symbol()->VersionId = VersionId; 669 } 670 671 // This function processes version scripts by updating VersionId 672 // member of symbols. 673 template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() { 674 // Symbol themselves might know their versions because symbols 675 // can contain versions in the form of <name>@<version>. 676 // Let them parse their names. 677 if (!Config->VersionDefinitions.empty()) 678 for (Symbol *Sym : SymVector) 679 Sym->body()->parseSymbolVersion(); 680 681 // Handle edge cases first. 682 if (!Config->VersionScriptGlobals.empty()) { 683 handleAnonymousVersion(); 684 return; 685 } 686 687 if (Config->VersionDefinitions.empty()) 688 return; 689 690 // Now we have version definitions, so we need to set version ids to symbols. 691 // Each version definition has a glob pattern, and all symbols that match 692 // with the pattern get that version. 693 694 // First, we assign versions to exact matching symbols, 695 // i.e. version definitions not containing any glob meta-characters. 696 for (SymbolVersion &Ver : Config->VersionScriptLocals) 697 assignExactVersion(Ver, VER_NDX_LOCAL, "local"); 698 for (VersionDefinition &V : Config->VersionDefinitions) 699 for (SymbolVersion &Ver : V.Globals) 700 assignExactVersion(Ver, V.Id, V.Name); 701 702 // Next, we assign versions to fuzzy matching symbols, 703 // i.e. version definitions containing glob meta-characters. 704 // Note that because the last match takes precedence over previous matches, 705 // we iterate over the definitions in the reverse order. 706 for (SymbolVersion &Ver : Config->VersionScriptLocals) 707 assignWildcardVersion(Ver, VER_NDX_LOCAL); 708 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions)) 709 for (SymbolVersion &Ver : V.Globals) 710 assignWildcardVersion(Ver, V.Id); 711 } 712 713 template class elf::SymbolTable<ELF32LE>; 714 template class elf::SymbolTable<ELF32BE>; 715 template class elf::SymbolTable<ELF64LE>; 716 template class elf::SymbolTable<ELF64BE>; 717