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 "Strings.h" 22 #include "SymbolListFile.h" 23 #include "Symbols.h" 24 #include "llvm/Bitcode/ReaderWriter.h" 25 #include "llvm/Support/StringSaver.h" 26 27 using namespace llvm; 28 using namespace llvm::object; 29 using namespace llvm::ELF; 30 31 using namespace lld; 32 using namespace lld::elf; 33 34 // All input object files must be for the same architecture 35 // (e.g. it does not make sense to link x86 object files with 36 // MIPS object files.) This function checks for that error. 37 template <class ELFT> static bool isCompatible(InputFile *F) { 38 if (!isa<ELFFileBase<ELFT>>(F) && !isa<BitcodeFile>(F)) 39 return true; 40 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) 41 return true; 42 StringRef A = F->getName(); 43 StringRef B = Config->Emulation; 44 if (B.empty()) 45 B = Config->FirstElf->getName(); 46 error(A + " is incompatible with " + B); 47 return false; 48 } 49 50 // Add symbols in File to the symbol table. 51 template <class ELFT> 52 void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) { 53 InputFile *FileP = File.get(); 54 if (!isCompatible<ELFT>(FileP)) 55 return; 56 57 // .a file 58 if (auto *F = dyn_cast<ArchiveFile>(FileP)) { 59 ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release())); 60 F->parse<ELFT>(); 61 return; 62 } 63 64 // Lazy object file 65 if (auto *F = dyn_cast<LazyObjectFile>(FileP)) { 66 LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release())); 67 F->parse<ELFT>(); 68 return; 69 } 70 71 if (Config->Trace) 72 outs() << getFilename(FileP) << "\n"; 73 74 // .so file 75 if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) { 76 // DSOs are uniquified not by filename but by soname. 77 F->parseSoName(); 78 if (!SoNames.insert(F->getSoName()).second) 79 return; 80 81 SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release())); 82 F->parseRest(); 83 return; 84 } 85 86 // LLVM bitcode file 87 if (auto *F = dyn_cast<BitcodeFile>(FileP)) { 88 BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release())); 89 F->parse<ELFT>(ComdatGroups); 90 return; 91 } 92 93 // Regular object file 94 auto *F = cast<ObjectFile<ELFT>>(FileP); 95 ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release())); 96 F->parse(ComdatGroups); 97 } 98 99 // This function is where all the optimizations of link-time 100 // optimization happens. When LTO is in use, some input files are 101 // not in native object file format but in the LLVM bitcode format. 102 // This function compiles bitcode files into a few big native files 103 // using LLVM functions and replaces bitcode symbols with the results. 104 // Because all bitcode files that consist of a program are passed 105 // to the compiler at once, it can do whole-program optimization. 106 template <class ELFT> void SymbolTable<ELFT>::addCombinedLtoObject() { 107 if (BitcodeFiles.empty()) 108 return; 109 110 // Compile bitcode files. 111 Lto.reset(new BitcodeCompiler); 112 for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles) 113 Lto->add(*F); 114 std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile(); 115 116 // Replace bitcode symbols. 117 for (auto &IF : IFs) { 118 ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release()); 119 120 DenseSet<StringRef> DummyGroups; 121 Obj->parse(DummyGroups); 122 ObjectFiles.emplace_back(Obj); 123 } 124 } 125 126 template <class ELFT> 127 DefinedRegular<ELFT> *SymbolTable<ELFT>::addAbsolute(StringRef Name, 128 uint8_t Visibility) { 129 return cast<DefinedRegular<ELFT>>( 130 addRegular(Name, STB_GLOBAL, Visibility)->body()); 131 } 132 133 // Add Name as an "ignored" symbol. An ignored symbol is a regular 134 // linker-synthesized defined symbol, but is only defined if needed. 135 template <class ELFT> 136 DefinedRegular<ELFT> *SymbolTable<ELFT>::addIgnored(StringRef Name, 137 uint8_t Visibility) { 138 if (!find(Name)) 139 return nullptr; 140 return addAbsolute(Name, Visibility); 141 } 142 143 // Set a flag for --trace-symbol so that we can print out a log message 144 // if a new symbol with the same name is inserted into the symbol table. 145 template <class ELFT> void SymbolTable<ELFT>::trace(StringRef Name) { 146 Symtab.insert({Name, {-1, true}}); 147 } 148 149 // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM. 150 // Used to implement --wrap. 151 template <class ELFT> void SymbolTable<ELFT>::wrap(StringRef Name) { 152 SymbolBody *B = find(Name); 153 if (!B) 154 return; 155 StringSaver Saver(Alloc); 156 Symbol *Sym = B->symbol(); 157 Symbol *Real = addUndefined(Saver.save("__real_" + Name)); 158 Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name)); 159 // We rename symbols by replacing the old symbol's SymbolBody with the new 160 // symbol's SymbolBody. This causes all SymbolBody pointers referring to the 161 // old symbol to instead refer to the new symbol. 162 memcpy(Real->Body.buffer, Sym->Body.buffer, sizeof(Sym->Body)); 163 memcpy(Sym->Body.buffer, Wrap->Body.buffer, sizeof(Wrap->Body)); 164 } 165 166 static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { 167 if (VA == STV_DEFAULT) 168 return VB; 169 if (VB == STV_DEFAULT) 170 return VA; 171 return std::min(VA, VB); 172 } 173 174 // Parses a symbol in the form of <name>@<version> or <name>@@<version>. 175 static std::pair<StringRef, uint16_t> getSymbolVersion(StringRef S) { 176 if (Config->VersionDefinitions.empty()) 177 return {S, Config->DefaultSymbolVersion}; 178 179 size_t Pos = S.find('@'); 180 if (Pos == 0 || Pos == StringRef::npos) 181 return {S, Config->DefaultSymbolVersion}; 182 183 StringRef Name = S.substr(0, Pos); 184 StringRef Verstr = S.substr(Pos + 1); 185 if (Verstr.empty()) 186 return {S, Config->DefaultSymbolVersion}; 187 188 // '@@' in a symbol name means the default version. 189 // It is usually the most recent one. 190 bool IsDefault = (Verstr[0] == '@'); 191 if (IsDefault) 192 Verstr = Verstr.substr(1); 193 194 for (VersionDefinition &V : Config->VersionDefinitions) { 195 if (V.Name == Verstr) 196 return {Name, IsDefault ? V.Id : (V.Id | VERSYM_HIDDEN)}; 197 } 198 199 // It is an error if the specified version was not defined. 200 error("symbol " + S + " has undefined version " + Verstr); 201 return {S, Config->DefaultSymbolVersion}; 202 } 203 204 // Find an existing symbol or create and insert a new one. 205 template <class ELFT> 206 std::pair<Symbol *, bool> SymbolTable<ELFT>::insert(StringRef &Name) { 207 auto P = Symtab.insert({Name, SymIndex((int)SymVector.size(), false)}); 208 SymIndex &V = P.first->second; 209 bool IsNew = P.second; 210 211 if (V.Idx == -1) { 212 IsNew = true; 213 V = SymIndex((int)SymVector.size(), true); 214 } 215 216 Symbol *Sym; 217 if (IsNew) { 218 Sym = new (Alloc) Symbol; 219 Sym->Binding = STB_WEAK; 220 Sym->Visibility = STV_DEFAULT; 221 Sym->IsUsedInRegularObj = false; 222 Sym->ExportDynamic = false; 223 Sym->Traced = V.Traced; 224 std::tie(Name, Sym->VersionId) = getSymbolVersion(Name); 225 SymVector.push_back(Sym); 226 } else { 227 Sym = SymVector[V.Idx]; 228 } 229 return {Sym, IsNew}; 230 } 231 232 // Find an existing symbol or create and insert a new one, then apply the given 233 // attributes. 234 template <class ELFT> 235 std::pair<Symbol *, bool> 236 SymbolTable<ELFT>::insert(StringRef &Name, uint8_t Type, uint8_t Visibility, 237 bool CanOmitFromDynSym, bool IsUsedInRegularObj, 238 InputFile *File) { 239 Symbol *S; 240 bool WasInserted; 241 std::tie(S, WasInserted) = insert(Name); 242 243 // Merge in the new symbol's visibility. 244 S->Visibility = getMinVisibility(S->Visibility, Visibility); 245 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic)) 246 S->ExportDynamic = true; 247 if (IsUsedInRegularObj) 248 S->IsUsedInRegularObj = true; 249 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType && 250 ((Type == STT_TLS) != S->body()->isTls())) 251 error("TLS attribute mismatch for symbol: " + 252 conflictMsg(S->body(), File)); 253 254 return {S, WasInserted}; 255 } 256 257 // Construct a string in the form of "Sym in File1 and File2". 258 // Used to construct an error message. 259 template <typename ELFT> 260 std::string SymbolTable<ELFT>::conflictMsg(SymbolBody *Existing, 261 InputFile *NewFile) { 262 std::string Sym = Existing->getName(); 263 if (Config->Demangle) 264 Sym = demangle(Sym); 265 return Sym + " in " + getFilename(Existing->File) + " and " + 266 getFilename(NewFile); 267 } 268 269 template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) { 270 return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0, 271 /*CanOmitFromDynSym*/ false, /*File*/ nullptr); 272 } 273 274 template <class ELFT> 275 Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding, 276 uint8_t StOther, uint8_t Type, 277 bool CanOmitFromDynSym, 278 InputFile *File) { 279 Symbol *S; 280 bool WasInserted; 281 std::tie(S, WasInserted) = 282 insert(Name, Type, StOther & 3, CanOmitFromDynSym, 283 /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File); 284 if (WasInserted) { 285 S->Binding = Binding; 286 replaceBody<Undefined>(S, Name, StOther, Type, File); 287 return S; 288 } 289 if (Binding != STB_WEAK) { 290 if (S->body()->isShared() || S->body()->isLazy()) 291 S->Binding = Binding; 292 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(S->body())) 293 SS->file()->IsUsed = true; 294 } 295 if (auto *L = dyn_cast<Lazy>(S->body())) { 296 // An undefined weak will not fetch archive members, but we have to remember 297 // its type. See also comment in addLazyArchive. 298 if (S->isWeak()) 299 L->Type = Type; 300 else if (auto F = L->fetch()) 301 addFile(std::move(F)); 302 } 303 return S; 304 } 305 306 // We have a new defined symbol with the specified binding. Return 1 if the new 307 // symbol should win, -1 if the new symbol should lose, or 0 if both symbols are 308 // strong defined symbols. 309 static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding) { 310 if (WasInserted) 311 return 1; 312 SymbolBody *Body = S->body(); 313 if (Body->isLazy() || Body->isUndefined() || Body->isShared()) 314 return 1; 315 if (Binding == STB_WEAK) 316 return -1; 317 if (S->isWeak()) 318 return 1; 319 return 0; 320 } 321 322 // We have a new non-common defined symbol with the specified binding. Return 1 323 // if the new symbol should win, -1 if the new symbol should lose, or 0 if there 324 // is a conflict. If the new symbol wins, also update the binding. 325 static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding) { 326 if (int Cmp = compareDefined(S, WasInserted, Binding)) { 327 if (Cmp > 0) 328 S->Binding = Binding; 329 return Cmp; 330 } 331 if (isa<DefinedCommon>(S->body())) { 332 // Non-common symbols take precedence over common symbols. 333 if (Config->WarnCommon) 334 warning("common " + S->body()->getName() + " is overridden"); 335 return 1; 336 } 337 return 0; 338 } 339 340 template <class ELFT> 341 Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size, 342 uint64_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) = 348 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, 349 /*IsUsedInRegularObj*/ true, File); 350 int Cmp = compareDefined(S, WasInserted, Binding); 351 if (Cmp > 0) { 352 S->Binding = Binding; 353 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File); 354 } else if (Cmp == 0) { 355 auto *C = dyn_cast<DefinedCommon>(S->body()); 356 if (!C) { 357 // Non-common symbols take precedence over common symbols. 358 if (Config->WarnCommon) 359 warning("common " + S->body()->getName() + " is overridden"); 360 return S; 361 } 362 363 if (Config->WarnCommon) 364 warning("multiple common of " + S->body()->getName()); 365 366 C->Size = std::max(C->Size, Size); 367 C->Alignment = std::max(C->Alignment, Alignment); 368 } 369 return S; 370 } 371 372 template <class ELFT> 373 void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing, 374 InputFile *NewFile) { 375 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile); 376 if (Config->AllowMultipleDefinition) 377 warning(Msg); 378 else 379 error(Msg); 380 } 381 382 template <typename ELFT> 383 Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym, 384 InputSectionBase<ELFT> *Section) { 385 Symbol *S; 386 bool WasInserted; 387 std::tie(S, WasInserted) = 388 insert(Name, Sym.getType(), Sym.getVisibility(), 389 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true, 390 Section ? Section->getFile() : nullptr); 391 int Cmp = compareDefinedNonCommon(S, WasInserted, Sym.getBinding()); 392 if (Cmp > 0) 393 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section); 394 else if (Cmp == 0) 395 reportDuplicate(S->body(), Section->getFile()); 396 return S; 397 } 398 399 template <typename ELFT> 400 Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding, 401 uint8_t StOther) { 402 Symbol *S; 403 bool WasInserted; 404 std::tie(S, WasInserted) = 405 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false, 406 /*IsUsedInRegularObj*/ true, nullptr); 407 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding); 408 if (Cmp > 0) 409 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther); 410 else if (Cmp == 0) 411 reportDuplicate(S->body(), nullptr); 412 return S; 413 } 414 415 template <typename ELFT> 416 Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N, 417 OutputSectionBase<ELFT> *Section, 418 uintX_t Value) { 419 Symbol *S; 420 bool WasInserted; 421 std::tie(S, WasInserted) = 422 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false, 423 /*IsUsedInRegularObj*/ true, nullptr); 424 int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL); 425 if (Cmp > 0) 426 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section); 427 else if (Cmp == 0) 428 reportDuplicate(S->body(), nullptr); 429 return S; 430 } 431 432 template <typename ELFT> 433 void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name, 434 const Elf_Sym &Sym, 435 const typename ELFT::Verdef *Verdef) { 436 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT 437 // as the visibility, which will leave the visibility in the symbol table 438 // unchanged. 439 Symbol *S; 440 bool WasInserted; 441 std::tie(S, WasInserted) = 442 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, 443 /*IsUsedInRegularObj*/ false, F); 444 // Make sure we preempt DSO symbols with default visibility. 445 if (Sym.getVisibility() == STV_DEFAULT) 446 S->ExportDynamic = true; 447 if (WasInserted || isa<Undefined>(S->body())) { 448 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef); 449 if (!S->isWeak()) 450 F->IsUsed = true; 451 } 452 } 453 454 template <class ELFT> 455 Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak, 456 uint8_t StOther, uint8_t Type, 457 bool CanOmitFromDynSym, BitcodeFile *F) { 458 Symbol *S; 459 bool WasInserted; 460 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym, 461 /*IsUsedInRegularObj*/ false, F); 462 int Cmp = 463 compareDefinedNonCommon(S, WasInserted, IsWeak ? STB_WEAK : STB_GLOBAL); 464 if (Cmp > 0) 465 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F); 466 else if (Cmp == 0) 467 reportDuplicate(S->body(), F); 468 return S; 469 } 470 471 template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) { 472 auto It = Symtab.find(Name); 473 if (It == Symtab.end()) 474 return nullptr; 475 SymIndex V = It->second; 476 if (V.Idx == -1) 477 return nullptr; 478 return SymVector[V.Idx]->body(); 479 } 480 481 // Returns a list of defined symbols that match with a given glob pattern. 482 template <class ELFT> 483 std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) { 484 std::vector<SymbolBody *> Res; 485 for (Symbol *Sym : SymVector) { 486 SymbolBody *B = Sym->body(); 487 if (!B->isUndefined() && globMatch(Pattern, B->getName())) 488 Res.push_back(B); 489 } 490 return Res; 491 } 492 493 template <class ELFT> 494 void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F, 495 const object::Archive::Symbol Sym) { 496 Symbol *S; 497 bool WasInserted; 498 StringRef Name = Sym.getName(); 499 std::tie(S, WasInserted) = insert(Name); 500 if (WasInserted) { 501 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType); 502 return; 503 } 504 if (!S->body()->isUndefined()) 505 return; 506 507 // Weak undefined symbols should not fetch members from archives. If we were 508 // to keep old symbol we would not know that an archive member was available 509 // if a strong undefined symbol shows up afterwards in the link. If a strong 510 // undefined symbol never shows up, this lazy symbol will get to the end of 511 // the link and must be treated as the weak undefined one. We already marked 512 // this symbol as used when we added it to the symbol table, but we also need 513 // to preserve its type. FIXME: Move the Type field to Symbol. 514 if (S->isWeak()) { 515 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type); 516 return; 517 } 518 MemoryBufferRef MBRef = F->getMember(&Sym); 519 if (!MBRef.getBuffer().empty()) 520 addFile(createObjectFile(MBRef, F->getName())); 521 } 522 523 template <class ELFT> 524 void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) { 525 Symbol *S; 526 bool WasInserted; 527 std::tie(S, WasInserted) = insert(Name); 528 if (WasInserted) { 529 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType); 530 return; 531 } 532 if (!S->body()->isUndefined()) 533 return; 534 535 // See comment for addLazyArchive above. 536 if (S->isWeak()) { 537 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type); 538 } else { 539 MemoryBufferRef MBRef = Obj.getBuffer(); 540 if (!MBRef.getBuffer().empty()) 541 addFile(createObjectFile(MBRef)); 542 } 543 } 544 545 // Process undefined (-u) flags by loading lazy symbols named by those flags. 546 template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() { 547 for (StringRef S : Config->Undefined) 548 if (auto *L = dyn_cast_or_null<Lazy>(find(S))) 549 if (std::unique_ptr<InputFile> File = L->fetch()) 550 addFile(std::move(File)); 551 } 552 553 // This function takes care of the case in which shared libraries depend on 554 // the user program (not the other way, which is usual). Shared libraries 555 // may have undefined symbols, expecting that the user program provides 556 // the definitions for them. An example is BSD's __progname symbol. 557 // We need to put such symbols to the main program's .dynsym so that 558 // shared libraries can find them. 559 // Except this, we ignore undefined symbols in DSOs. 560 template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() { 561 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles) 562 for (StringRef U : File->getUndefinedSymbols()) 563 if (SymbolBody *Sym = find(U)) 564 if (Sym->isDefined()) 565 Sym->symbol()->ExportDynamic = true; 566 } 567 568 // This function process the dynamic list option by marking all the symbols 569 // to be exported in the dynamic table. 570 template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() { 571 for (StringRef S : Config->DynamicList) 572 if (SymbolBody *B = find(S)) 573 B->symbol()->ExportDynamic = true; 574 } 575 576 static bool hasWildcard(StringRef S) { 577 return S.find_first_of("?*") != StringRef::npos; 578 } 579 580 static void setVersionId(SymbolBody *Body, StringRef VersionName, 581 StringRef Name, uint16_t Version) { 582 if (!Body || Body->isUndefined()) { 583 if (Config->NoUndefinedVersion) 584 error("version script assignment of " + VersionName + " to symbol " + 585 Name + " failed: symbol not defined"); 586 return; 587 } 588 589 Symbol *Sym = Body->symbol(); 590 if (Sym->VersionId != Config->DefaultSymbolVersion) 591 warning("duplicate symbol " + Name + " in version script"); 592 Sym->VersionId = Version; 593 } 594 595 template <class ELFT> 596 std::map<std::string, SymbolBody *> SymbolTable<ELFT>::getDemangledSyms() { 597 std::map<std::string, SymbolBody *> Result; 598 for (Symbol *Sym : SymVector) { 599 SymbolBody *B = Sym->body(); 600 Result[demangle(B->getName())] = B; 601 } 602 return Result; 603 } 604 605 static bool hasExternCpp() { 606 for (VersionDefinition &V : Config->VersionDefinitions) 607 for (SymbolVersion Sym : V.Globals) 608 if (Sym.IsExternCpp) 609 return true; 610 return false; 611 } 612 613 // This function processes the --version-script option by marking all global 614 // symbols with the VersionScriptGlobal flag, which acts as a filter on the 615 // dynamic symbol table. 616 template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() { 617 // If version script does not contain versions declarations, 618 // we just should mark global symbols. 619 if (!Config->VersionScriptGlobals.empty()) { 620 for (SymbolVersion &Sym : Config->VersionScriptGlobals) 621 if (SymbolBody *B = find(Sym.Name)) 622 B->symbol()->VersionId = VER_NDX_GLOBAL; 623 return; 624 } 625 626 if (Config->VersionDefinitions.empty()) 627 return; 628 629 // If we have symbols version declarations, we should 630 // assign version references for each symbol. 631 // Current rules are: 632 // * If there is an exact match for the mangled name or we have extern C++ 633 // exact match, then we use it. 634 // * Otherwise, we look through the wildcard patterns. We look through the 635 // version tags in reverse order. We use the first match we find (the last 636 // matching version tag in the file). 637 // Handle exact matches and build a map of demangled externs for 638 // quick search during next step. 639 std::map<std::string, SymbolBody *> Demangled; 640 if (hasExternCpp()) 641 Demangled = getDemangledSyms(); 642 643 for (VersionDefinition &V : Config->VersionDefinitions) { 644 for (SymbolVersion Sym : V.Globals) { 645 if (hasWildcard(Sym.Name)) 646 continue; 647 SymbolBody *B = Sym.IsExternCpp ? Demangled[Sym.Name] : find(Sym.Name); 648 setVersionId(B, V.Name, Sym.Name, V.Id); 649 } 650 } 651 652 // Handle wildcards. 653 for (size_t I = Config->VersionDefinitions.size() - 1; I != (size_t)-1; --I) { 654 VersionDefinition &V = Config->VersionDefinitions[I]; 655 for (SymbolVersion &Sym : V.Globals) 656 if (hasWildcard(Sym.Name)) 657 for (SymbolBody *B : findAll(Sym.Name)) 658 if (B->symbol()->VersionId == Config->DefaultSymbolVersion) 659 B->symbol()->VersionId = V.Id; 660 } 661 } 662 663 template class elf::SymbolTable<ELF32LE>; 664 template class elf::SymbolTable<ELF32BE>; 665 template class elf::SymbolTable<ELF64LE>; 666 template class elf::SymbolTable<ELF64BE>; 667