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 template <class ELFT> 326 static int compareDefinedNonCommon(Symbol *S, bool WasInserted, 327 uint8_t Binding) { 328 if (int Cmp = compareDefined(S, WasInserted, Binding)) { 329 if (Cmp > 0) 330 S->Binding = Binding; 331 return Cmp; 332 } 333 if (isa<DefinedCommon<ELFT>>(S->body())) { 334 // Non-common symbols take precedence over common symbols. 335 if (Config->WarnCommon) 336 warning("common " + S->body()->getName() + " is overridden"); 337 return 1; 338 } 339 return 0; 340 } 341 342 template <class ELFT> 343 Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size, 344 uint64_t Alignment, uint8_t Binding, 345 uint8_t StOther, uint8_t Type, 346 InputFile *File) { 347 Symbol *S; 348 bool WasInserted; 349 std::tie(S, WasInserted) = 350 insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, 351 /*IsUsedInRegularObj*/ true, File); 352 int Cmp = compareDefined(S, WasInserted, Binding); 353 if (Cmp > 0) { 354 S->Binding = Binding; 355 replaceBody<DefinedCommon<ELFT>>(S, N, Size, Alignment, StOther, Type, 356 File); 357 } else if (Cmp == 0) { 358 auto *C = dyn_cast<DefinedCommon<ELFT>>(S->body()); 359 if (!C) { 360 // Non-common symbols take precedence over common symbols. 361 if (Config->WarnCommon) 362 warning("common " + S->body()->getName() + " is overridden"); 363 return S; 364 } 365 366 if (Config->WarnCommon) 367 warning("multiple common of " + S->body()->getName()); 368 369 C->Size = std::max(C->Size, Size); 370 C->Alignment = std::max(C->Alignment, Alignment); 371 } 372 return S; 373 } 374 375 template <class ELFT> 376 void SymbolTable<ELFT>::reportDuplicate(SymbolBody *Existing, 377 InputFile *NewFile) { 378 std::string Msg = "duplicate symbol: " + conflictMsg(Existing, NewFile); 379 if (Config->AllowMultipleDefinition) 380 warning(Msg); 381 else 382 error(Msg); 383 } 384 385 template <typename ELFT> 386 Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, const Elf_Sym &Sym, 387 InputSectionBase<ELFT> *Section) { 388 Symbol *S; 389 bool WasInserted; 390 std::tie(S, WasInserted) = 391 insert(Name, Sym.getType(), Sym.getVisibility(), 392 /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true, 393 Section ? Section->getFile() : nullptr); 394 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Sym.getBinding()); 395 if (Cmp > 0) 396 replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section); 397 else if (Cmp == 0) 398 reportDuplicate(S->body(), Section->getFile()); 399 return S; 400 } 401 402 template <typename ELFT> 403 Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding, 404 uint8_t StOther) { 405 Symbol *S; 406 bool WasInserted; 407 std::tie(S, WasInserted) = 408 insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false, 409 /*IsUsedInRegularObj*/ true, nullptr); 410 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding); 411 if (Cmp > 0) 412 replaceBody<DefinedRegular<ELFT>>(S, Name, StOther); 413 else if (Cmp == 0) 414 reportDuplicate(S->body(), nullptr); 415 return S; 416 } 417 418 template <typename ELFT> 419 Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N, 420 OutputSectionBase<ELFT> *Section, 421 uintX_t Value) { 422 Symbol *S; 423 bool WasInserted; 424 std::tie(S, WasInserted) = 425 insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false, 426 /*IsUsedInRegularObj*/ true, nullptr); 427 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, STB_GLOBAL); 428 if (Cmp > 0) 429 replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section); 430 else if (Cmp == 0) 431 reportDuplicate(S->body(), nullptr); 432 return S; 433 } 434 435 template <typename ELFT> 436 void SymbolTable<ELFT>::addShared(SharedFile<ELFT> *F, StringRef Name, 437 const Elf_Sym &Sym, 438 const typename ELFT::Verdef *Verdef) { 439 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT 440 // as the visibility, which will leave the visibility in the symbol table 441 // unchanged. 442 Symbol *S; 443 bool WasInserted; 444 std::tie(S, WasInserted) = 445 insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true, 446 /*IsUsedInRegularObj*/ false, F); 447 // Make sure we preempt DSO symbols with default visibility. 448 if (Sym.getVisibility() == STV_DEFAULT) 449 S->ExportDynamic = true; 450 if (WasInserted || isa<Undefined>(S->body())) { 451 replaceBody<SharedSymbol<ELFT>>(S, F, Name, Sym, Verdef); 452 if (!S->isWeak()) 453 F->IsUsed = true; 454 } 455 } 456 457 template <class ELFT> 458 Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak, 459 uint8_t StOther, uint8_t Type, 460 bool CanOmitFromDynSym, BitcodeFile *F) { 461 Symbol *S; 462 bool WasInserted; 463 std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym, 464 /*IsUsedInRegularObj*/ false, F); 465 int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, 466 IsWeak ? STB_WEAK : STB_GLOBAL); 467 if (Cmp > 0) 468 replaceBody<DefinedBitcode>(S, Name, StOther, Type, F); 469 else if (Cmp == 0) 470 reportDuplicate(S->body(), F); 471 return S; 472 } 473 474 template <class ELFT> SymbolBody *SymbolTable<ELFT>::find(StringRef Name) { 475 auto It = Symtab.find(Name); 476 if (It == Symtab.end()) 477 return nullptr; 478 SymIndex V = It->second; 479 if (V.Idx == -1) 480 return nullptr; 481 return SymVector[V.Idx]->body(); 482 } 483 484 // Returns a list of defined symbols that match with a given glob pattern. 485 template <class ELFT> 486 std::vector<SymbolBody *> SymbolTable<ELFT>::findAll(StringRef Pattern) { 487 std::vector<SymbolBody *> Res; 488 for (Symbol *Sym : SymVector) { 489 SymbolBody *B = Sym->body(); 490 if (!B->isUndefined() && globMatch(Pattern, B->getName())) 491 Res.push_back(B); 492 } 493 return Res; 494 } 495 496 template <class ELFT> 497 void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F, 498 const object::Archive::Symbol Sym) { 499 Symbol *S; 500 bool WasInserted; 501 StringRef Name = Sym.getName(); 502 std::tie(S, WasInserted) = insert(Name); 503 if (WasInserted) { 504 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType); 505 return; 506 } 507 if (!S->body()->isUndefined()) 508 return; 509 510 // Weak undefined symbols should not fetch members from archives. If we were 511 // to keep old symbol we would not know that an archive member was available 512 // if a strong undefined symbol shows up afterwards in the link. If a strong 513 // undefined symbol never shows up, this lazy symbol will get to the end of 514 // the link and must be treated as the weak undefined one. We already marked 515 // this symbol as used when we added it to the symbol table, but we also need 516 // to preserve its type. FIXME: Move the Type field to Symbol. 517 if (S->isWeak()) { 518 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type); 519 return; 520 } 521 MemoryBufferRef MBRef = F->getMember(&Sym); 522 if (!MBRef.getBuffer().empty()) 523 addFile(createObjectFile(MBRef, F->getName())); 524 } 525 526 template <class ELFT> 527 void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) { 528 Symbol *S; 529 bool WasInserted; 530 std::tie(S, WasInserted) = insert(Name); 531 if (WasInserted) { 532 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType); 533 return; 534 } 535 if (!S->body()->isUndefined()) 536 return; 537 538 // See comment for addLazyArchive above. 539 if (S->isWeak()) { 540 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type); 541 } else { 542 MemoryBufferRef MBRef = Obj.getBuffer(); 543 if (!MBRef.getBuffer().empty()) 544 addFile(createObjectFile(MBRef)); 545 } 546 } 547 548 // Process undefined (-u) flags by loading lazy symbols named by those flags. 549 template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() { 550 for (StringRef S : Config->Undefined) 551 if (auto *L = dyn_cast_or_null<Lazy>(find(S))) 552 if (std::unique_ptr<InputFile> File = L->fetch()) 553 addFile(std::move(File)); 554 } 555 556 // This function takes care of the case in which shared libraries depend on 557 // the user program (not the other way, which is usual). Shared libraries 558 // may have undefined symbols, expecting that the user program provides 559 // the definitions for them. An example is BSD's __progname symbol. 560 // We need to put such symbols to the main program's .dynsym so that 561 // shared libraries can find them. 562 // Except this, we ignore undefined symbols in DSOs. 563 template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() { 564 for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles) 565 for (StringRef U : File->getUndefinedSymbols()) 566 if (SymbolBody *Sym = find(U)) 567 if (Sym->isDefined()) 568 Sym->symbol()->ExportDynamic = true; 569 } 570 571 // This function process the dynamic list option by marking all the symbols 572 // to be exported in the dynamic table. 573 template <class ELFT> void SymbolTable<ELFT>::scanDynamicList() { 574 for (StringRef S : Config->DynamicList) 575 if (SymbolBody *B = find(S)) 576 B->symbol()->ExportDynamic = true; 577 } 578 579 static bool hasWildcard(StringRef S) { 580 return S.find_first_of("?*") != StringRef::npos; 581 } 582 583 static void setVersionId(SymbolBody *Body, StringRef VersionName, 584 StringRef Name, uint16_t Version) { 585 if (!Body || Body->isUndefined()) { 586 if (Config->NoUndefinedVersion) 587 error("version script assignment of " + VersionName + " to symbol " + 588 Name + " failed: symbol not defined"); 589 return; 590 } 591 592 Symbol *Sym = Body->symbol(); 593 if (Sym->VersionId != Config->DefaultSymbolVersion) 594 warning("duplicate symbol " + Name + " in version script"); 595 Sym->VersionId = Version; 596 } 597 598 template <class ELFT> 599 std::map<std::string, SymbolBody *> SymbolTable<ELFT>::getDemangledSyms() { 600 std::map<std::string, SymbolBody *> Result; 601 for (Symbol *Sym : SymVector) { 602 SymbolBody *B = Sym->body(); 603 Result[demangle(B->getName())] = B; 604 } 605 return Result; 606 } 607 608 static bool hasExternCpp() { 609 for (VersionDefinition &V : Config->VersionDefinitions) 610 for (SymbolVersion Sym : V.Globals) 611 if (Sym.IsExternCpp) 612 return true; 613 return false; 614 } 615 616 // This function processes the --version-script option by marking all global 617 // symbols with the VersionScriptGlobal flag, which acts as a filter on the 618 // dynamic symbol table. 619 template <class ELFT> void SymbolTable<ELFT>::scanVersionScript() { 620 // If version script does not contain versions declarations, 621 // we just should mark global symbols. 622 if (!Config->VersionScriptGlobals.empty()) { 623 for (SymbolVersion &Sym : Config->VersionScriptGlobals) 624 if (SymbolBody *B = find(Sym.Name)) 625 B->symbol()->VersionId = VER_NDX_GLOBAL; 626 return; 627 } 628 629 if (Config->VersionDefinitions.empty()) 630 return; 631 632 // If we have symbols version declarations, we should 633 // assign version references for each symbol. 634 // Current rules are: 635 // * If there is an exact match for the mangled name or we have extern C++ 636 // exact match, then we use it. 637 // * Otherwise, we look through the wildcard patterns. We look through the 638 // version tags in reverse order. We use the first match we find (the last 639 // matching version tag in the file). 640 // Handle exact matches and build a map of demangled externs for 641 // quick search during next step. 642 std::map<std::string, SymbolBody *> Demangled; 643 if (hasExternCpp()) 644 Demangled = getDemangledSyms(); 645 646 for (VersionDefinition &V : Config->VersionDefinitions) { 647 for (SymbolVersion Sym : V.Globals) { 648 if (hasWildcard(Sym.Name)) 649 continue; 650 SymbolBody *B = Sym.IsExternCpp ? Demangled[Sym.Name] : find(Sym.Name); 651 setVersionId(B, V.Name, Sym.Name, V.Id); 652 } 653 } 654 655 // Handle wildcards. 656 for (size_t I = Config->VersionDefinitions.size() - 1; I != (size_t)-1; --I) { 657 VersionDefinition &V = Config->VersionDefinitions[I]; 658 for (SymbolVersion &Sym : V.Globals) 659 if (hasWildcard(Sym.Name)) 660 for (SymbolBody *B : findAll(Sym.Name)) 661 if (B->symbol()->VersionId == Config->DefaultSymbolVersion) 662 B->symbol()->VersionId = V.Id; 663 } 664 } 665 666 template class elf::SymbolTable<ELF32LE>; 667 template class elf::SymbolTable<ELF32BE>; 668 template class elf::SymbolTable<ELF64LE>; 669 template class elf::SymbolTable<ELF64BE>; 670