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 SymbolTable *elf::Symtab; 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 41 if (F->EKind == Config->EKind && F->EMachine == Config->EMachine) { 42 if (Config->EMachine != EM_MIPS) 43 return true; 44 if (isMipsN32Abi(F) == Config->MipsN32Abi) 45 return true; 46 } 47 48 if (!Config->Emulation.empty()) 49 error(toString(F) + " is incompatible with " + Config->Emulation); 50 else 51 error(toString(F) + " is incompatible with " + toString(Config->FirstElf)); 52 return false; 53 } 54 55 // Add symbols in File to the symbol table. 56 template <class ELFT> void SymbolTable::addFile(InputFile *File) { 57 if (!Config->FirstElf && isa<ELFFileBase<ELFT>>(File)) 58 Config->FirstElf = File; 59 60 if (!isCompatible<ELFT>(File)) 61 return; 62 63 // Binary file 64 if (auto *F = dyn_cast<BinaryFile>(File)) { 65 BinaryFile::Instances.push_back(F); 66 F->parse<ELFT>(); 67 return; 68 } 69 70 // .a file 71 if (auto *F = dyn_cast<ArchiveFile>(File)) { 72 F->parse<ELFT>(); 73 return; 74 } 75 76 // Lazy object file 77 if (auto *F = dyn_cast<LazyObjFile>(File)) { 78 F->parse<ELFT>(); 79 return; 80 } 81 82 if (Config->Trace) 83 message(toString(File)); 84 85 // .so file 86 if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) { 87 // DSOs are uniquified not by filename but by soname. 88 F->parseSoName(); 89 if (ErrorCount || !SoNames.insert(F->SoName).second) 90 return; 91 SharedFile<ELFT>::Instances.push_back(F); 92 F->parseRest(); 93 return; 94 } 95 96 // LLVM bitcode file 97 if (auto *F = dyn_cast<BitcodeFile>(File)) { 98 BitcodeFile::Instances.push_back(F); 99 F->parse<ELFT>(ComdatGroups); 100 return; 101 } 102 103 // Regular object file 104 auto *F = cast<ObjFile<ELFT>>(File); 105 ObjFile<ELFT>::Instances.push_back(F); 106 F->parse(ComdatGroups); 107 } 108 109 // This function is where all the optimizations of link-time 110 // optimization happens. When LTO is in use, some input files are 111 // not in native object file format but in the LLVM bitcode format. 112 // This function compiles bitcode files into a few big native files 113 // using LLVM functions and replaces bitcode symbols with the results. 114 // Because all bitcode files that consist of a program are passed 115 // to the compiler at once, it can do whole-program optimization. 116 template <class ELFT> void SymbolTable::addCombinedLTOObject() { 117 if (BitcodeFile::Instances.empty()) 118 return; 119 120 // Compile bitcode files and replace bitcode symbols. 121 LTO.reset(new BitcodeCompiler); 122 for (BitcodeFile *F : BitcodeFile::Instances) 123 LTO->add(*F); 124 125 for (InputFile *File : LTO->compile()) { 126 ObjFile<ELFT> *Obj = cast<ObjFile<ELFT>>(File); 127 DenseSet<CachedHashStringRef> DummyGroups; 128 Obj->parse(DummyGroups); 129 ObjFile<ELFT>::Instances.push_back(Obj); 130 } 131 } 132 133 template <class ELFT> 134 DefinedRegular *SymbolTable::addAbsolute(StringRef Name, uint8_t Visibility, 135 uint8_t Binding) { 136 Symbol *Sym = addRegular<ELFT>(Name, Visibility, STT_NOTYPE, 0, 0, Binding, 137 nullptr, nullptr); 138 return cast<DefinedRegular>(Sym->body()); 139 } 140 141 // Add Name as an "ignored" symbol. An ignored symbol is a regular 142 // linker-synthesized defined symbol, but is only defined if needed. 143 template <class ELFT> 144 DefinedRegular *SymbolTable::addIgnored(StringRef Name, uint8_t Visibility) { 145 SymbolBody *S = find(Name); 146 if (!S || S->isInCurrentDSO()) 147 return nullptr; 148 return addAbsolute<ELFT>(Name, Visibility); 149 } 150 151 // Set a flag for --trace-symbol so that we can print out a log message 152 // if a new symbol with the same name is inserted into the symbol table. 153 void SymbolTable::trace(StringRef Name) { 154 Symtab.insert({CachedHashStringRef(Name), {-1, true}}); 155 } 156 157 // Rename SYM as __wrap_SYM. The original symbol is preserved as __real_SYM. 158 // Used to implement --wrap. 159 template <class ELFT> void SymbolTable::addSymbolWrap(StringRef Name) { 160 SymbolBody *B = find(Name); 161 if (!B) 162 return; 163 Symbol *Sym = B->symbol(); 164 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name)); 165 Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name)); 166 167 // Tell LTO not to eliminate this symbol 168 Wrap->IsUsedInRegularObj = true; 169 170 Config->RenamedSymbols[Real] = {Sym, Real->Binding}; 171 Config->RenamedSymbols[Sym] = {Wrap, Sym->Binding}; 172 } 173 174 // Creates alias for symbol. Used to implement --defsym=ALIAS=SYM. 175 template <class ELFT> 176 void SymbolTable::addSymbolAlias(StringRef Alias, StringRef Name) { 177 SymbolBody *B = find(Name); 178 if (!B) { 179 error("-defsym: undefined symbol: " + Name); 180 return; 181 } 182 Symbol *Sym = B->symbol(); 183 Symbol *AliasSym = addUndefined<ELFT>(Alias); 184 185 // Tell LTO not to eliminate this symbol 186 Sym->IsUsedInRegularObj = true; 187 Config->RenamedSymbols[AliasSym] = {Sym, AliasSym->Binding}; 188 } 189 190 // Apply symbol renames created by -wrap and -defsym. The renames are created 191 // before LTO in addSymbolWrap() and addSymbolAlias() to have a chance to inform 192 // LTO (if LTO is running) not to include these symbols in IPO. Now that the 193 // symbols are finalized, we can perform the replacement. 194 void SymbolTable::applySymbolRenames() { 195 for (auto &KV : Config->RenamedSymbols) { 196 Symbol *Dst = KV.first; 197 Symbol *Src = KV.second.Target; 198 Dst->body()->copy(Src->body()); 199 Dst->Binding = KV.second.OriginalBinding; 200 } 201 } 202 203 static uint8_t getMinVisibility(uint8_t VA, uint8_t VB) { 204 if (VA == STV_DEFAULT) 205 return VB; 206 if (VB == STV_DEFAULT) 207 return VA; 208 return std::min(VA, VB); 209 } 210 211 // Find an existing symbol or create and insert a new one. 212 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) { 213 // <name>@@<version> means the symbol is the default version. In that 214 // case <name>@@<version> will be used to resolve references to <name>. 215 size_t Pos = Name.find("@@"); 216 if (Pos != StringRef::npos) 217 Name = Name.take_front(Pos); 218 219 auto P = Symtab.insert( 220 {CachedHashStringRef(Name), SymIndex((int)SymVector.size(), false)}); 221 SymIndex &V = P.first->second; 222 bool IsNew = P.second; 223 224 if (V.Idx == -1) { 225 IsNew = true; 226 V = SymIndex((int)SymVector.size(), true); 227 } 228 229 Symbol *Sym; 230 if (IsNew) { 231 Sym = make<Symbol>(); 232 Sym->InVersionScript = false; 233 Sym->Binding = STB_WEAK; 234 Sym->Visibility = STV_DEFAULT; 235 Sym->IsUsedInRegularObj = false; 236 Sym->ExportDynamic = false; 237 Sym->Traced = V.Traced; 238 Sym->VersionId = Config->DefaultSymbolVersion; 239 SymVector.push_back(Sym); 240 } else { 241 Sym = SymVector[V.Idx]; 242 } 243 return {Sym, IsNew}; 244 } 245 246 // Find an existing symbol or create and insert a new one, then apply the given 247 // attributes. 248 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, uint8_t Type, 249 uint8_t Visibility, 250 bool CanOmitFromDynSym, 251 InputFile *File) { 252 bool IsUsedInRegularObj = !File || File->kind() == InputFile::ObjectKind; 253 Symbol *S; 254 bool WasInserted; 255 std::tie(S, WasInserted) = insert(Name); 256 257 // Merge in the new symbol's visibility. 258 S->Visibility = getMinVisibility(S->Visibility, Visibility); 259 260 if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic)) 261 S->ExportDynamic = true; 262 263 if (IsUsedInRegularObj) 264 S->IsUsedInRegularObj = true; 265 266 if (!WasInserted && S->body()->Type != SymbolBody::UnknownType && 267 ((Type == STT_TLS) != S->body()->isTls())) { 268 error("TLS attribute mismatch: " + toString(*S->body()) + 269 "\n>>> defined in " + toString(S->body()->File) + 270 "\n>>> defined in " + toString(File)); 271 } 272 273 return {S, WasInserted}; 274 } 275 276 template <class ELFT> Symbol *SymbolTable::addUndefined(StringRef Name) { 277 return addUndefined<ELFT>(Name, /*IsLocal=*/false, STB_GLOBAL, STV_DEFAULT, 278 /*Type*/ 0, 279 /*CanOmitFromDynSym*/ false, /*File*/ nullptr); 280 } 281 282 static uint8_t getVisibility(uint8_t StOther) { return StOther & 3; } 283 284 template <class ELFT> 285 Symbol *SymbolTable::addUndefined(StringRef Name, bool IsLocal, uint8_t Binding, 286 uint8_t StOther, uint8_t Type, 287 bool CanOmitFromDynSym, InputFile *File) { 288 Symbol *S; 289 bool WasInserted; 290 uint8_t Visibility = getVisibility(StOther); 291 std::tie(S, WasInserted) = 292 insert(Name, Type, Visibility, CanOmitFromDynSym, File); 293 // An undefined symbol with non default visibility must be satisfied 294 // in the same DSO. 295 if (WasInserted || 296 (isa<SharedSymbol>(S->body()) && Visibility != STV_DEFAULT)) { 297 S->Binding = Binding; 298 replaceBody<Undefined>(S, Name, IsLocal, StOther, Type, File); 299 return S; 300 } 301 if (Binding != STB_WEAK) { 302 SymbolBody *B = S->body(); 303 if (B->isShared() || B->isLazy() || B->isUndefined()) 304 S->Binding = Binding; 305 if (auto *SS = dyn_cast<SharedSymbol>(B)) 306 cast<SharedFile<ELFT>>(SS->File)->IsUsed = true; 307 } 308 if (auto *L = dyn_cast<Lazy>(S->body())) { 309 // An undefined weak will not fetch archive members, but we have to remember 310 // its type. See also comment in addLazyArchive. 311 if (S->isWeak()) 312 L->Type = Type; 313 else if (InputFile *F = L->fetch()) 314 addFile<ELFT>(F); 315 } 316 return S; 317 } 318 319 // Using .symver foo,foo@@VER unfortunately creates two symbols: foo and 320 // foo@@VER. We want to effectively ignore foo, so give precedence to 321 // foo@@VER. 322 // FIXME: If users can transition to using 323 // .symver foo,foo@@@VER 324 // we can delete this hack. 325 static int compareVersion(Symbol *S, StringRef Name) { 326 bool A = Name.contains("@@"); 327 bool B = S->body()->getName().contains("@@"); 328 if (A && !B) 329 return 1; 330 if (!A && B) 331 return -1; 332 return 0; 333 } 334 335 // We have a new defined symbol with the specified binding. Return 1 if the new 336 // symbol should win, -1 if the new symbol should lose, or 0 if both symbols are 337 // strong defined symbols. 338 static int compareDefined(Symbol *S, bool WasInserted, uint8_t Binding, 339 StringRef Name) { 340 if (WasInserted) 341 return 1; 342 SymbolBody *Body = S->body(); 343 if (!Body->isInCurrentDSO()) 344 return 1; 345 346 if (int R = compareVersion(S, Name)) 347 return R; 348 349 if (Binding == STB_WEAK) 350 return -1; 351 if (S->isWeak()) 352 return 1; 353 return 0; 354 } 355 356 // We have a new non-common defined symbol with the specified binding. Return 1 357 // if the new symbol should win, -1 if the new symbol should lose, or 0 if there 358 // is a conflict. If the new symbol wins, also update the binding. 359 static int compareDefinedNonCommon(Symbol *S, bool WasInserted, uint8_t Binding, 360 bool IsAbsolute, uint64_t Value, 361 StringRef Name) { 362 if (int Cmp = compareDefined(S, WasInserted, Binding, Name)) { 363 if (Cmp > 0) 364 S->Binding = Binding; 365 return Cmp; 366 } 367 SymbolBody *B = S->body(); 368 if (isa<DefinedCommon>(B)) { 369 // Non-common symbols take precedence over common symbols. 370 if (Config->WarnCommon) 371 warn("common " + S->body()->getName() + " is overridden"); 372 return 1; 373 } else if (auto *R = dyn_cast<DefinedRegular>(B)) { 374 if (R->Section == nullptr && Binding == STB_GLOBAL && IsAbsolute && 375 R->Value == Value) 376 return -1; 377 } 378 return 0; 379 } 380 381 Symbol *SymbolTable::addCommon(StringRef N, uint64_t Size, uint32_t Alignment, 382 uint8_t Binding, uint8_t StOther, uint8_t Type, 383 InputFile *File) { 384 Symbol *S; 385 bool WasInserted; 386 std::tie(S, WasInserted) = insert(N, Type, getVisibility(StOther), 387 /*CanOmitFromDynSym*/ false, File); 388 int Cmp = compareDefined(S, WasInserted, Binding, N); 389 if (Cmp > 0) { 390 S->Binding = Binding; 391 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File); 392 } else if (Cmp == 0) { 393 auto *C = dyn_cast<DefinedCommon>(S->body()); 394 if (!C) { 395 // Non-common symbols take precedence over common symbols. 396 if (Config->WarnCommon) 397 warn("common " + S->body()->getName() + " is overridden"); 398 return S; 399 } 400 401 if (Config->WarnCommon) 402 warn("multiple common of " + S->body()->getName()); 403 404 Alignment = C->Alignment = std::max(C->Alignment, Alignment); 405 if (Size > C->Size) 406 replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File); 407 } 408 return S; 409 } 410 411 static void warnOrError(const Twine &Msg) { 412 if (Config->AllowMultipleDefinition) 413 warn(Msg); 414 else 415 error(Msg); 416 } 417 418 static void reportDuplicate(SymbolBody *Sym, InputFile *NewFile) { 419 warnOrError("duplicate symbol: " + toString(*Sym) + "\n>>> defined in " + 420 toString(Sym->File) + "\n>>> defined in " + toString(NewFile)); 421 } 422 423 template <class ELFT> 424 static void reportDuplicate(SymbolBody *Sym, InputSectionBase *ErrSec, 425 typename ELFT::uint ErrOffset) { 426 DefinedRegular *D = dyn_cast<DefinedRegular>(Sym); 427 if (!D || !D->Section || !ErrSec) { 428 reportDuplicate(Sym, ErrSec ? ErrSec->File : nullptr); 429 return; 430 } 431 432 // Construct and print an error message in the form of: 433 // 434 // ld.lld: error: duplicate symbol: foo 435 // >>> defined at bar.c:30 436 // >>> bar.o (/home/alice/src/bar.o) 437 // >>> defined at baz.c:563 438 // >>> baz.o in archive libbaz.a 439 auto *Sec1 = cast<InputSectionBase>(D->Section); 440 std::string Src1 = Sec1->getSrcMsg<ELFT>(D->Value); 441 std::string Obj1 = Sec1->getObjMsg<ELFT>(D->Value); 442 std::string Src2 = ErrSec->getSrcMsg<ELFT>(ErrOffset); 443 std::string Obj2 = ErrSec->getObjMsg<ELFT>(ErrOffset); 444 445 std::string Msg = "duplicate symbol: " + toString(*Sym) + "\n>>> defined at "; 446 if (!Src1.empty()) 447 Msg += Src1 + "\n>>> "; 448 Msg += Obj1 + "\n>>> defined at "; 449 if (!Src2.empty()) 450 Msg += Src2 + "\n>>> "; 451 Msg += Obj2; 452 warnOrError(Msg); 453 } 454 455 template <typename ELFT> 456 Symbol *SymbolTable::addRegular(StringRef Name, uint8_t StOther, uint8_t Type, 457 uint64_t Value, uint64_t Size, uint8_t Binding, 458 SectionBase *Section, InputFile *File) { 459 Symbol *S; 460 bool WasInserted; 461 std::tie(S, WasInserted) = insert(Name, Type, getVisibility(StOther), 462 /*CanOmitFromDynSym*/ false, File); 463 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, Section == nullptr, 464 Value, Name); 465 if (Cmp > 0) 466 replaceBody<DefinedRegular>(S, Name, /*IsLocal=*/false, StOther, Type, 467 Value, Size, Section, File); 468 else if (Cmp == 0) 469 reportDuplicate<ELFT>(S->body(), 470 dyn_cast_or_null<InputSectionBase>(Section), Value); 471 return S; 472 } 473 474 template <typename ELFT> 475 void SymbolTable::addShared(SharedFile<ELFT> *File, StringRef Name, 476 const typename ELFT::Sym &Sym, 477 const typename ELFT::Verdef *Verdef) { 478 // DSO symbols do not affect visibility in the output, so we pass STV_DEFAULT 479 // as the visibility, which will leave the visibility in the symbol table 480 // unchanged. 481 Symbol *S; 482 bool WasInserted; 483 std::tie(S, WasInserted) = insert(Name, Sym.getType(), STV_DEFAULT, 484 /*CanOmitFromDynSym*/ true, File); 485 // Make sure we preempt DSO symbols with default visibility. 486 if (Sym.getVisibility() == STV_DEFAULT) 487 S->ExportDynamic = true; 488 489 SymbolBody *Body = S->body(); 490 // An undefined symbol with non default visibility must be satisfied 491 // in the same DSO. 492 if (WasInserted || 493 (isa<Undefined>(Body) && Body->getVisibility() == STV_DEFAULT)) { 494 replaceBody<SharedSymbol>(S, File, Name, Sym.st_other, Sym.getType(), &Sym, 495 Verdef); 496 if (!S->isWeak()) 497 File->IsUsed = true; 498 } 499 } 500 501 Symbol *SymbolTable::addBitcode(StringRef Name, uint8_t Binding, 502 uint8_t StOther, uint8_t Type, 503 bool CanOmitFromDynSym, BitcodeFile *F) { 504 Symbol *S; 505 bool WasInserted; 506 std::tie(S, WasInserted) = 507 insert(Name, Type, getVisibility(StOther), CanOmitFromDynSym, F); 508 int Cmp = compareDefinedNonCommon(S, WasInserted, Binding, 509 /*IsAbs*/ false, /*Value*/ 0, Name); 510 if (Cmp > 0) 511 replaceBody<DefinedRegular>(S, Name, /*IsLocal=*/false, StOther, Type, 0, 0, 512 nullptr, F); 513 else if (Cmp == 0) 514 reportDuplicate(S->body(), F); 515 return S; 516 } 517 518 SymbolBody *SymbolTable::find(StringRef Name) { 519 auto It = Symtab.find(CachedHashStringRef(Name)); 520 if (It == Symtab.end()) 521 return nullptr; 522 SymIndex V = It->second; 523 if (V.Idx == -1) 524 return nullptr; 525 return SymVector[V.Idx]->body(); 526 } 527 528 SymbolBody *SymbolTable::findInCurrentDSO(StringRef Name) { 529 if (SymbolBody *S = find(Name)) 530 if (S->isInCurrentDSO()) 531 return S; 532 return nullptr; 533 } 534 535 template <class ELFT> 536 Symbol *SymbolTable::addLazyArchive(ArchiveFile *F, 537 const object::Archive::Symbol Sym) { 538 Symbol *S; 539 bool WasInserted; 540 StringRef Name = Sym.getName(); 541 std::tie(S, WasInserted) = insert(Name); 542 if (WasInserted) { 543 replaceBody<LazyArchive>(S, *F, Sym, SymbolBody::UnknownType); 544 return S; 545 } 546 if (!S->body()->isUndefined()) 547 return S; 548 549 // Weak undefined symbols should not fetch members from archives. If we were 550 // to keep old symbol we would not know that an archive member was available 551 // if a strong undefined symbol shows up afterwards in the link. If a strong 552 // undefined symbol never shows up, this lazy symbol will get to the end of 553 // the link and must be treated as the weak undefined one. We already marked 554 // this symbol as used when we added it to the symbol table, but we also need 555 // to preserve its type. FIXME: Move the Type field to Symbol. 556 if (S->isWeak()) { 557 replaceBody<LazyArchive>(S, *F, Sym, S->body()->Type); 558 return S; 559 } 560 std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym); 561 if (!MBInfo.first.getBuffer().empty()) 562 addFile<ELFT>(createObjectFile(MBInfo.first, F->getName(), MBInfo.second)); 563 return S; 564 } 565 566 template <class ELFT> 567 void SymbolTable::addLazyObject(StringRef Name, LazyObjFile &Obj) { 568 Symbol *S; 569 bool WasInserted; 570 std::tie(S, WasInserted) = insert(Name); 571 if (WasInserted) { 572 replaceBody<LazyObject>(S, Name, Obj, SymbolBody::UnknownType); 573 return; 574 } 575 if (!S->body()->isUndefined()) 576 return; 577 578 // See comment for addLazyArchive above. 579 if (S->isWeak()) 580 replaceBody<LazyObject>(S, Name, Obj, S->body()->Type); 581 else if (InputFile *F = Obj.fetch()) 582 addFile<ELFT>(F); 583 } 584 585 // Process undefined (-u) flags by loading lazy symbols named by those flags. 586 template <class ELFT> void SymbolTable::scanUndefinedFlags() { 587 for (StringRef S : Config->Undefined) 588 if (auto *L = dyn_cast_or_null<Lazy>(find(S))) 589 if (InputFile *File = L->fetch()) 590 addFile<ELFT>(File); 591 } 592 593 // This function takes care of the case in which shared libraries depend on 594 // the user program (not the other way, which is usual). Shared libraries 595 // may have undefined symbols, expecting that the user program provides 596 // the definitions for them. An example is BSD's __progname symbol. 597 // We need to put such symbols to the main program's .dynsym so that 598 // shared libraries can find them. 599 // Except this, we ignore undefined symbols in DSOs. 600 template <class ELFT> void SymbolTable::scanShlibUndefined() { 601 for (SharedFile<ELFT> *File : SharedFile<ELFT>::Instances) { 602 for (StringRef U : File->getUndefinedSymbols()) { 603 SymbolBody *Sym = find(U); 604 if (!Sym || !Sym->isDefined()) 605 continue; 606 Sym->symbol()->ExportDynamic = true; 607 608 // If -dynamic-list is given, the default version is set to 609 // VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym. 610 // Set to VER_NDX_GLOBAL so the symbol will be handled as if it were 611 // specified by -dynamic-list. 612 Sym->symbol()->VersionId = VER_NDX_GLOBAL; 613 } 614 } 615 } 616 617 // Initialize DemangledSyms with a map from demangled symbols to symbol 618 // objects. Used to handle "extern C++" directive in version scripts. 619 // 620 // The map will contain all demangled symbols. That can be very large, 621 // and in LLD we generally want to avoid do anything for each symbol. 622 // Then, why are we doing this? Here's why. 623 // 624 // Users can use "extern C++ {}" directive to match against demangled 625 // C++ symbols. For example, you can write a pattern such as 626 // "llvm::*::foo(int, ?)". Obviously, there's no way to handle this 627 // other than trying to match a pattern against all demangled symbols. 628 // So, if "extern C++" feature is used, we need to demangle all known 629 // symbols. 630 StringMap<std::vector<SymbolBody *>> &SymbolTable::getDemangledSyms() { 631 if (!DemangledSyms) { 632 DemangledSyms.emplace(); 633 for (Symbol *Sym : SymVector) { 634 SymbolBody *B = Sym->body(); 635 if (B->isUndefined()) 636 continue; 637 if (Optional<std::string> S = demangle(B->getName())) 638 (*DemangledSyms)[*S].push_back(B); 639 else 640 (*DemangledSyms)[B->getName()].push_back(B); 641 } 642 } 643 return *DemangledSyms; 644 } 645 646 std::vector<SymbolBody *> SymbolTable::findByVersion(SymbolVersion Ver) { 647 if (Ver.IsExternCpp) 648 return getDemangledSyms().lookup(Ver.Name); 649 if (SymbolBody *B = find(Ver.Name)) 650 if (!B->isUndefined()) 651 return {B}; 652 return {}; 653 } 654 655 std::vector<SymbolBody *> SymbolTable::findAllByVersion(SymbolVersion Ver) { 656 std::vector<SymbolBody *> Res; 657 StringMatcher M(Ver.Name); 658 659 if (Ver.IsExternCpp) { 660 for (auto &P : getDemangledSyms()) 661 if (M.match(P.first())) 662 Res.insert(Res.end(), P.second.begin(), P.second.end()); 663 return Res; 664 } 665 666 for (Symbol *Sym : SymVector) { 667 SymbolBody *B = Sym->body(); 668 if (!B->isUndefined() && M.match(B->getName())) 669 Res.push_back(B); 670 } 671 return Res; 672 } 673 674 // If there's only one anonymous version definition in a version 675 // script file, the script does not actually define any symbol version, 676 // but just specifies symbols visibilities. 677 void SymbolTable::handleAnonymousVersion() { 678 for (SymbolVersion &Ver : Config->VersionScriptGlobals) 679 assignExactVersion(Ver, VER_NDX_GLOBAL, "global"); 680 for (SymbolVersion &Ver : Config->VersionScriptGlobals) 681 assignWildcardVersion(Ver, VER_NDX_GLOBAL); 682 for (SymbolVersion &Ver : Config->VersionScriptLocals) 683 assignExactVersion(Ver, VER_NDX_LOCAL, "local"); 684 for (SymbolVersion &Ver : Config->VersionScriptLocals) 685 assignWildcardVersion(Ver, VER_NDX_LOCAL); 686 } 687 688 // Set symbol versions to symbols. This function handles patterns 689 // containing no wildcard characters. 690 void SymbolTable::assignExactVersion(SymbolVersion Ver, uint16_t VersionId, 691 StringRef VersionName) { 692 if (Ver.HasWildcard) 693 return; 694 695 // Get a list of symbols which we need to assign the version to. 696 std::vector<SymbolBody *> Syms = findByVersion(Ver); 697 if (Syms.empty()) { 698 if (Config->NoUndefinedVersion) 699 error("version script assignment of '" + VersionName + "' to symbol '" + 700 Ver.Name + "' failed: symbol not defined"); 701 return; 702 } 703 704 // Assign the version. 705 for (SymbolBody *B : Syms) { 706 // Skip symbols containing version info because symbol versions 707 // specified by symbol names take precedence over version scripts. 708 // See parseSymbolVersion(). 709 if (B->getName().contains('@')) 710 continue; 711 712 Symbol *Sym = B->symbol(); 713 if (Sym->InVersionScript) 714 warn("duplicate symbol '" + Ver.Name + "' in version script"); 715 Sym->VersionId = VersionId; 716 Sym->InVersionScript = true; 717 } 718 } 719 720 void SymbolTable::assignWildcardVersion(SymbolVersion Ver, uint16_t VersionId) { 721 if (!Ver.HasWildcard) 722 return; 723 724 // Exact matching takes precendence over fuzzy matching, 725 // so we set a version to a symbol only if no version has been assigned 726 // to the symbol. This behavior is compatible with GNU. 727 for (SymbolBody *B : findAllByVersion(Ver)) 728 if (B->symbol()->VersionId == Config->DefaultSymbolVersion) 729 B->symbol()->VersionId = VersionId; 730 } 731 732 // This function processes version scripts by updating VersionId 733 // member of symbols. 734 void SymbolTable::scanVersionScript() { 735 // Handle edge cases first. 736 handleAnonymousVersion(); 737 738 // Now we have version definitions, so we need to set version ids to symbols. 739 // Each version definition has a glob pattern, and all symbols that match 740 // with the pattern get that version. 741 742 // First, we assign versions to exact matching symbols, 743 // i.e. version definitions not containing any glob meta-characters. 744 for (VersionDefinition &V : Config->VersionDefinitions) 745 for (SymbolVersion &Ver : V.Globals) 746 assignExactVersion(Ver, V.Id, V.Name); 747 748 // Next, we assign versions to fuzzy matching symbols, 749 // i.e. version definitions containing glob meta-characters. 750 // Note that because the last match takes precedence over previous matches, 751 // we iterate over the definitions in the reverse order. 752 for (VersionDefinition &V : llvm::reverse(Config->VersionDefinitions)) 753 for (SymbolVersion &Ver : V.Globals) 754 assignWildcardVersion(Ver, V.Id); 755 756 // Symbol themselves might know their versions because symbols 757 // can contain versions in the form of <name>@<version>. 758 // Let them parse and update their names to exclude version suffix. 759 for (Symbol *Sym : SymVector) 760 Sym->body()->parseSymbolVersion(); 761 } 762 763 template void SymbolTable::addSymbolWrap<ELF32LE>(StringRef); 764 template void SymbolTable::addSymbolWrap<ELF32BE>(StringRef); 765 template void SymbolTable::addSymbolWrap<ELF64LE>(StringRef); 766 template void SymbolTable::addSymbolWrap<ELF64BE>(StringRef); 767 768 template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef); 769 template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef); 770 template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef); 771 template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef); 772 773 template Symbol *SymbolTable::addUndefined<ELF32LE>(StringRef, bool, uint8_t, 774 uint8_t, uint8_t, bool, 775 InputFile *); 776 template Symbol *SymbolTable::addUndefined<ELF32BE>(StringRef, bool, uint8_t, 777 uint8_t, uint8_t, bool, 778 InputFile *); 779 template Symbol *SymbolTable::addUndefined<ELF64LE>(StringRef, bool, uint8_t, 780 uint8_t, uint8_t, bool, 781 InputFile *); 782 template Symbol *SymbolTable::addUndefined<ELF64BE>(StringRef, bool, uint8_t, 783 uint8_t, uint8_t, bool, 784 InputFile *); 785 786 template void SymbolTable::addSymbolAlias<ELF32LE>(StringRef, StringRef); 787 template void SymbolTable::addSymbolAlias<ELF32BE>(StringRef, StringRef); 788 template void SymbolTable::addSymbolAlias<ELF64LE>(StringRef, StringRef); 789 template void SymbolTable::addSymbolAlias<ELF64BE>(StringRef, StringRef); 790 791 template void SymbolTable::addCombinedLTOObject<ELF32LE>(); 792 template void SymbolTable::addCombinedLTOObject<ELF32BE>(); 793 template void SymbolTable::addCombinedLTOObject<ELF64LE>(); 794 template void SymbolTable::addCombinedLTOObject<ELF64BE>(); 795 796 template Symbol *SymbolTable::addRegular<ELF32LE>(StringRef, uint8_t, uint8_t, 797 uint64_t, uint64_t, uint8_t, 798 SectionBase *, InputFile *); 799 template Symbol *SymbolTable::addRegular<ELF32BE>(StringRef, uint8_t, uint8_t, 800 uint64_t, uint64_t, uint8_t, 801 SectionBase *, InputFile *); 802 template Symbol *SymbolTable::addRegular<ELF64LE>(StringRef, uint8_t, uint8_t, 803 uint64_t, uint64_t, uint8_t, 804 SectionBase *, InputFile *); 805 template Symbol *SymbolTable::addRegular<ELF64BE>(StringRef, uint8_t, uint8_t, 806 uint64_t, uint64_t, uint8_t, 807 SectionBase *, InputFile *); 808 809 template DefinedRegular *SymbolTable::addAbsolute<ELF32LE>(StringRef, uint8_t, 810 uint8_t); 811 template DefinedRegular *SymbolTable::addAbsolute<ELF32BE>(StringRef, uint8_t, 812 uint8_t); 813 template DefinedRegular *SymbolTable::addAbsolute<ELF64LE>(StringRef, uint8_t, 814 uint8_t); 815 template DefinedRegular *SymbolTable::addAbsolute<ELF64BE>(StringRef, uint8_t, 816 uint8_t); 817 818 template DefinedRegular *SymbolTable::addIgnored<ELF32LE>(StringRef, uint8_t); 819 template DefinedRegular *SymbolTable::addIgnored<ELF32BE>(StringRef, uint8_t); 820 template DefinedRegular *SymbolTable::addIgnored<ELF64LE>(StringRef, uint8_t); 821 template DefinedRegular *SymbolTable::addIgnored<ELF64BE>(StringRef, uint8_t); 822 823 template Symbol * 824 SymbolTable::addLazyArchive<ELF32LE>(ArchiveFile *, 825 const object::Archive::Symbol); 826 template Symbol * 827 SymbolTable::addLazyArchive<ELF32BE>(ArchiveFile *, 828 const object::Archive::Symbol); 829 template Symbol * 830 SymbolTable::addLazyArchive<ELF64LE>(ArchiveFile *, 831 const object::Archive::Symbol); 832 template Symbol * 833 SymbolTable::addLazyArchive<ELF64BE>(ArchiveFile *, 834 const object::Archive::Symbol); 835 836 template void SymbolTable::addLazyObject<ELF32LE>(StringRef, LazyObjFile &); 837 template void SymbolTable::addLazyObject<ELF32BE>(StringRef, LazyObjFile &); 838 template void SymbolTable::addLazyObject<ELF64LE>(StringRef, LazyObjFile &); 839 template void SymbolTable::addLazyObject<ELF64BE>(StringRef, LazyObjFile &); 840 841 template void SymbolTable::addShared<ELF32LE>(SharedFile<ELF32LE> *, StringRef, 842 const typename ELF32LE::Sym &, 843 const typename ELF32LE::Verdef *); 844 template void SymbolTable::addShared<ELF32BE>(SharedFile<ELF32BE> *, StringRef, 845 const typename ELF32BE::Sym &, 846 const typename ELF32BE::Verdef *); 847 template void SymbolTable::addShared<ELF64LE>(SharedFile<ELF64LE> *, StringRef, 848 const typename ELF64LE::Sym &, 849 const typename ELF64LE::Verdef *); 850 template void SymbolTable::addShared<ELF64BE>(SharedFile<ELF64BE> *, StringRef, 851 const typename ELF64BE::Sym &, 852 const typename ELF64BE::Verdef *); 853 854 template void SymbolTable::scanUndefinedFlags<ELF32LE>(); 855 template void SymbolTable::scanUndefinedFlags<ELF32BE>(); 856 template void SymbolTable::scanUndefinedFlags<ELF64LE>(); 857 template void SymbolTable::scanUndefinedFlags<ELF64BE>(); 858 859 template void SymbolTable::scanShlibUndefined<ELF32LE>(); 860 template void SymbolTable::scanShlibUndefined<ELF32BE>(); 861 template void SymbolTable::scanShlibUndefined<ELF64LE>(); 862 template void SymbolTable::scanShlibUndefined<ELF64BE>(); 863