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