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