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