1 //===- InputFiles.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 #include "InputFiles.h" 11 #include "Error.h" 12 #include "InputSection.h" 13 #include "Symbols.h" 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/IR/LLVMContext.h" 16 #include "llvm/IR/Module.h" 17 #include "llvm/Object/IRObjectFile.h" 18 #include "llvm/Support/raw_ostream.h" 19 20 using namespace llvm; 21 using namespace llvm::ELF; 22 using namespace llvm::object; 23 using namespace llvm::sys::fs; 24 25 using namespace lld; 26 using namespace lld::elf; 27 28 template <class ELFT> 29 static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) { 30 std::error_code EC; 31 ELFFile<ELFT> F(MB.getBuffer(), EC); 32 check(EC); 33 return F; 34 } 35 36 template <class ELFT> 37 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) 38 : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {} 39 40 template <class ELFT> 41 ELFKind ELFFileBase<ELFT>::getELFKind() { 42 if (ELFT::TargetEndianness == support::little) 43 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind; 44 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind; 45 } 46 47 template <class ELFT> 48 typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) { 49 if (!Symtab) 50 return Elf_Sym_Range(nullptr, nullptr); 51 Elf_Sym_Range Syms = ELFObj.symbols(Symtab); 52 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); 53 uint32_t FirstNonLocal = Symtab->sh_info; 54 if (FirstNonLocal > NumSymbols) 55 fatal("invalid sh_info in symbol table"); 56 57 if (OnlyGlobals) 58 return makeArrayRef(Syms.begin() + FirstNonLocal, Syms.end()); 59 return makeArrayRef(Syms.begin(), Syms.end()); 60 } 61 62 template <class ELFT> 63 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const { 64 uint32_t I = Sym.st_shndx; 65 if (I == ELF::SHN_XINDEX) 66 return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX); 67 if (I >= ELF::SHN_LORESERVE) 68 return 0; 69 return I; 70 } 71 72 template <class ELFT> void ELFFileBase<ELFT>::initStringTable() { 73 if (!Symtab) 74 return; 75 StringTable = check(ELFObj.getStringTableForSymtab(*Symtab)); 76 } 77 78 template <class ELFT> 79 elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M) 80 : ELFFileBase<ELFT>(Base::ObjectKind, M) {} 81 82 template <class ELFT> 83 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() { 84 if (!this->Symtab) 85 return this->SymbolBodies; 86 uint32_t FirstNonLocal = this->Symtab->sh_info; 87 return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal); 88 } 89 90 template <class ELFT> 91 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() { 92 if (!this->Symtab) 93 return this->SymbolBodies; 94 uint32_t FirstNonLocal = this->Symtab->sh_info; 95 return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1); 96 } 97 98 template <class ELFT> 99 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() { 100 if (!this->Symtab) 101 return this->SymbolBodies; 102 return makeArrayRef(this->SymbolBodies).slice(1); 103 } 104 105 template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const { 106 if (MipsReginfo) 107 return MipsReginfo->Reginfo->ri_gp_value; 108 return 0; 109 } 110 111 template <class ELFT> 112 void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) { 113 // Read section and symbol tables. 114 initializeSections(ComdatGroups); 115 initializeSymbols(); 116 } 117 118 // Sections with SHT_GROUP and comdat bits define comdat section groups. 119 // They are identified and deduplicated by group name. This function 120 // returns a group name. 121 template <class ELFT> 122 StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) { 123 const ELFFile<ELFT> &Obj = this->ELFObj; 124 uint32_t SymtabdSectionIndex = Sec.sh_link; 125 const Elf_Shdr *SymtabSec = check(Obj.getSection(SymtabdSectionIndex)); 126 uint32_t SymIndex = Sec.sh_info; 127 const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex); 128 StringRef StringTable = check(Obj.getStringTableForSymtab(*SymtabSec)); 129 return check(Sym->getName(StringTable)); 130 } 131 132 template <class ELFT> 133 ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word> 134 elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) { 135 const ELFFile<ELFT> &Obj = this->ELFObj; 136 ArrayRef<Elf_Word> Entries = 137 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec)); 138 if (Entries.empty() || Entries[0] != GRP_COMDAT) 139 fatal("unsupported SHT_GROUP format"); 140 return Entries.slice(1); 141 } 142 143 template <class ELFT> static bool shouldMerge(const typename ELFT::Shdr &Sec) { 144 typedef typename ELFT::uint uintX_t; 145 uintX_t Flags = Sec.sh_flags; 146 if (!(Flags & SHF_MERGE)) 147 return false; 148 if (Flags & SHF_WRITE) 149 fatal("writable SHF_MERGE sections are not supported"); 150 uintX_t EntSize = Sec.sh_entsize; 151 if (!EntSize || Sec.sh_size % EntSize) 152 fatal("SHF_MERGE section size must be a multiple of sh_entsize"); 153 154 // Don't try to merge if the aligment is larger than the sh_entsize and this 155 // is not SHF_STRINGS. 156 // 157 // Since this is not a SHF_STRINGS, we would need to pad after every entity. 158 // It would be equivalent for the producer of the .o to just set a larger 159 // sh_entsize. 160 if (Flags & SHF_STRINGS) 161 return true; 162 163 if (Sec.sh_addralign > EntSize) 164 return false; 165 166 return true; 167 } 168 169 template <class ELFT> 170 void elf::ObjectFile<ELFT>::initializeSections( 171 DenseSet<StringRef> &ComdatGroups) { 172 uint64_t Size = this->ELFObj.getNumSections(); 173 Sections.resize(Size); 174 unsigned I = -1; 175 const ELFFile<ELFT> &Obj = this->ELFObj; 176 for (const Elf_Shdr &Sec : Obj.sections()) { 177 ++I; 178 if (Sections[I] == &InputSection<ELFT>::Discarded) 179 continue; 180 181 switch (Sec.sh_type) { 182 case SHT_GROUP: 183 Sections[I] = &InputSection<ELFT>::Discarded; 184 if (ComdatGroups.insert(getShtGroupSignature(Sec)).second) 185 continue; 186 for (uint32_t SecIndex : getShtGroupEntries(Sec)) { 187 if (SecIndex >= Size) 188 fatal("invalid section index in group"); 189 Sections[SecIndex] = &InputSection<ELFT>::Discarded; 190 } 191 break; 192 case SHT_SYMTAB: 193 this->Symtab = &Sec; 194 break; 195 case SHT_SYMTAB_SHNDX: 196 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec)); 197 break; 198 case SHT_STRTAB: 199 case SHT_NULL: 200 break; 201 case SHT_RELA: 202 case SHT_REL: { 203 // This section contains relocation information. 204 // If -r is given, we do not interpret or apply relocation 205 // but just copy relocation sections to output. 206 if (Config->Relocatable) { 207 Sections[I] = new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec); 208 break; 209 } 210 211 // Find the relocation target section and associate this 212 // section with it. 213 InputSectionBase<ELFT> *Target = getRelocTarget(Sec); 214 if (!Target) 215 break; 216 if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) { 217 S->RelocSections.push_back(&Sec); 218 break; 219 } 220 if (auto *S = dyn_cast<EHInputSection<ELFT>>(Target)) { 221 if (S->RelocSection) 222 fatal("multiple relocation sections to .eh_frame are not supported"); 223 S->RelocSection = &Sec; 224 break; 225 } 226 fatal("relocations pointing to SHF_MERGE are not supported"); 227 } 228 default: 229 Sections[I] = createInputSection(Sec); 230 } 231 } 232 } 233 234 template <class ELFT> 235 InputSectionBase<ELFT> * 236 elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) { 237 uint32_t Idx = Sec.sh_info; 238 if (Idx >= Sections.size()) 239 fatal("invalid relocated section index"); 240 InputSectionBase<ELFT> *Target = Sections[Idx]; 241 242 // Strictly speaking, a relocation section must be included in the 243 // group of the section it relocates. However, LLVM 3.3 and earlier 244 // would fail to do so, so we gracefully handle that case. 245 if (Target == &InputSection<ELFT>::Discarded) 246 return nullptr; 247 248 if (!Target) 249 fatal("unsupported relocation reference"); 250 return Target; 251 } 252 253 template <class ELFT> 254 InputSectionBase<ELFT> * 255 elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) { 256 StringRef Name = check(this->ELFObj.getSectionName(&Sec)); 257 258 // .note.GNU-stack is a marker section to control the presence of 259 // PT_GNU_STACK segment in outputs. Since the presence of the segment 260 // is controlled only by the command line option (-z execstack) in LLD, 261 // .note.GNU-stack is ignored. 262 if (Name == ".note.GNU-stack") 263 return &InputSection<ELFT>::Discarded; 264 265 if (Name == ".note.GNU-split-stack") { 266 error("objects using splitstacks are not supported"); 267 return &InputSection<ELFT>::Discarded; 268 } 269 270 if (Config->StripDebug && Name.startswith(".debug")) 271 return &InputSection<ELFT>::Discarded; 272 273 // A MIPS object file has a special section that contains register 274 // usage info, which needs to be handled by the linker specially. 275 if (Config->EMachine == EM_MIPS && Name == ".reginfo") { 276 MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec)); 277 return MipsReginfo.get(); 278 } 279 280 // We dont need special handling of .eh_frame sections if relocatable 281 // output was choosen. Proccess them as usual input sections. 282 if (!Config->Relocatable && Name == ".eh_frame") 283 return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec); 284 if (shouldMerge<ELFT>(Sec)) 285 return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec); 286 return new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec); 287 } 288 289 template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() { 290 this->initStringTable(); 291 Elf_Sym_Range Syms = this->getElfSymbols(false); 292 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); 293 SymbolBodies.reserve(NumSymbols); 294 for (const Elf_Sym &Sym : Syms) 295 SymbolBodies.push_back(createSymbolBody(&Sym)); 296 } 297 298 template <class ELFT> 299 InputSectionBase<ELFT> * 300 elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const { 301 uint32_t Index = this->getSectionIndex(Sym); 302 if (Index == 0) 303 return nullptr; 304 if (Index >= Sections.size() || !Sections[Index]) 305 fatal("invalid section index"); 306 InputSectionBase<ELFT> *S = Sections[Index]; 307 if (S == &InputSectionBase<ELFT>::Discarded) 308 return S; 309 return S->Repl; 310 } 311 312 template <class ELFT> 313 SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) { 314 unsigned char Binding = Sym->getBinding(); 315 InputSectionBase<ELFT> *Sec = getSection(*Sym); 316 if (Binding == STB_LOCAL) { 317 if (Sym->st_shndx == SHN_UNDEF) 318 return new (Alloc) UndefinedElf<ELFT>(*Sym); 319 return new (Alloc) DefinedRegular<ELFT>(*Sym, Sec); 320 } 321 322 StringRef Name = check(Sym->getName(this->StringTable)); 323 324 switch (Sym->st_shndx) { 325 case SHN_UNDEF: 326 return new (Alloc) UndefinedElf<ELFT>(Name, *Sym); 327 case SHN_COMMON: 328 return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value, Binding, 329 Sym->st_other, Sym->getType()); 330 } 331 332 switch (Binding) { 333 default: 334 fatal("unexpected binding"); 335 case STB_GLOBAL: 336 case STB_WEAK: 337 case STB_GNU_UNIQUE: 338 if (Sec == &InputSection<ELFT>::Discarded) 339 return new (Alloc) UndefinedElf<ELFT>(Name, *Sym); 340 return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec); 341 } 342 } 343 344 void ArchiveFile::parse() { 345 File = check(Archive::create(MB), "failed to parse archive"); 346 347 // Allocate a buffer for Lazy objects. 348 size_t NumSyms = File->getNumberOfSymbols(); 349 LazySymbols.reserve(NumSyms); 350 351 // Read the symbol table to construct Lazy objects. 352 for (const Archive::Symbol &Sym : File->symbols()) 353 LazySymbols.emplace_back(this, Sym); 354 } 355 356 // Returns a buffer pointing to a member file containing a given symbol. 357 MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) { 358 Archive::Child C = 359 check(Sym->getMember(), 360 "could not get the member for symbol " + Sym->getName()); 361 362 if (!Seen.insert(C.getChildOffset()).second) 363 return MemoryBufferRef(); 364 365 return check(C.getMemoryBufferRef(), 366 "could not get the buffer for the member defining symbol " + 367 Sym->getName()); 368 } 369 370 template <class ELFT> 371 SharedFile<ELFT>::SharedFile(MemoryBufferRef M) 372 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {} 373 374 template <class ELFT> 375 const typename ELFT::Shdr * 376 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const { 377 uint32_t Index = this->getSectionIndex(Sym); 378 if (Index == 0) 379 return nullptr; 380 return check(this->ELFObj.getSection(Index)); 381 } 382 383 // Partially parse the shared object file so that we can call 384 // getSoName on this object. 385 template <class ELFT> void SharedFile<ELFT>::parseSoName() { 386 typedef typename ELFT::Dyn Elf_Dyn; 387 typedef typename ELFT::uint uintX_t; 388 const Elf_Shdr *DynamicSec = nullptr; 389 390 const ELFFile<ELFT> Obj = this->ELFObj; 391 for (const Elf_Shdr &Sec : Obj.sections()) { 392 switch (Sec.sh_type) { 393 default: 394 continue; 395 case SHT_DYNSYM: 396 this->Symtab = &Sec; 397 break; 398 case SHT_DYNAMIC: 399 DynamicSec = &Sec; 400 break; 401 case SHT_SYMTAB_SHNDX: 402 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec)); 403 break; 404 } 405 } 406 407 this->initStringTable(); 408 SoName = this->getName(); 409 410 if (!DynamicSec) 411 return; 412 auto *Begin = 413 reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset); 414 const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn); 415 416 for (const Elf_Dyn &Dyn : make_range(Begin, End)) { 417 if (Dyn.d_tag == DT_SONAME) { 418 uintX_t Val = Dyn.getVal(); 419 if (Val >= this->StringTable.size()) 420 fatal("invalid DT_SONAME entry"); 421 SoName = StringRef(this->StringTable.data() + Val); 422 return; 423 } 424 } 425 } 426 427 // Fully parse the shared object file. This must be called after parseSoName(). 428 template <class ELFT> void SharedFile<ELFT>::parseRest() { 429 Elf_Sym_Range Syms = this->getElfSymbols(true); 430 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); 431 SymbolBodies.reserve(NumSymbols); 432 for (const Elf_Sym &Sym : Syms) { 433 StringRef Name = check(Sym.getName(this->StringTable)); 434 if (Sym.isUndefined()) 435 Undefs.push_back(Name); 436 else 437 SymbolBodies.emplace_back(this, Name, Sym); 438 } 439 } 440 441 BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {} 442 443 bool BitcodeFile::classof(const InputFile *F) { 444 return F->kind() == BitcodeKind; 445 } 446 447 static uint8_t getGvVisibility(const GlobalValue *GV) { 448 switch (GV->getVisibility()) { 449 case GlobalValue::DefaultVisibility: 450 return STV_DEFAULT; 451 case GlobalValue::HiddenVisibility: 452 return STV_HIDDEN; 453 case GlobalValue::ProtectedVisibility: 454 return STV_PROTECTED; 455 } 456 llvm_unreachable("unknown visibility"); 457 } 458 459 SymbolBody * 460 BitcodeFile::createSymbolBody(const DenseSet<const Comdat *> &KeptComdats, 461 const IRObjectFile &Obj, 462 const BasicSymbolRef &Sym) { 463 const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl()); 464 assert(GV); 465 if (const Comdat *C = GV->getComdat()) 466 if (!KeptComdats.count(C)) 467 return nullptr; 468 469 uint8_t Visibility = getGvVisibility(GV); 470 471 SmallString<64> Name; 472 raw_svector_ostream OS(Name); 473 Sym.printName(OS); 474 StringRef NameRef = Saver.save(StringRef(Name)); 475 476 const Module &M = Obj.getModule(); 477 SymbolBody *Body; 478 uint32_t Flags = Sym.getFlags(); 479 bool IsWeak = Flags & BasicSymbolRef::SF_Weak; 480 if (Flags & BasicSymbolRef::SF_Undefined) { 481 Body = new (Alloc) UndefinedBitcode(NameRef, IsWeak, Visibility); 482 } else if (Flags & BasicSymbolRef::SF_Common) { 483 const DataLayout &DL = M.getDataLayout(); 484 uint64_t Size = DL.getTypeAllocSize(GV->getValueType()); 485 Body = new (Alloc) 486 DefinedCommon(NameRef, Size, GV->getAlignment(), 487 IsWeak ? STB_WEAK : STB_GLOBAL, Visibility, /*Type*/ 0); 488 } else { 489 Body = new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility); 490 } 491 if (GV->isThreadLocal()) 492 Body->Type = STT_TLS; 493 return Body; 494 } 495 496 bool BitcodeFile::shouldSkip(const BasicSymbolRef &Sym) { 497 uint32_t Flags = Sym.getFlags(); 498 if (!(Flags & BasicSymbolRef::SF_Global)) 499 return true; 500 if (Flags & BasicSymbolRef::SF_FormatSpecific) 501 return true; 502 return false; 503 } 504 505 void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) { 506 LLVMContext Context; 507 std::unique_ptr<IRObjectFile> Obj = check(IRObjectFile::create(MB, Context)); 508 const Module &M = Obj->getModule(); 509 510 DenseSet<const Comdat *> KeptComdats; 511 for (const auto &P : M.getComdatSymbolTable()) { 512 StringRef N = Saver.save(P.first()); 513 if (ComdatGroups.insert(N).second) 514 KeptComdats.insert(&P.second); 515 } 516 517 for (const BasicSymbolRef &Sym : Obj->symbols()) 518 if (!shouldSkip(Sym)) 519 SymbolBodies.push_back(createSymbolBody(KeptComdats, *Obj, Sym)); 520 } 521 522 template <typename T> 523 static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) { 524 std::unique_ptr<T> Ret = llvm::make_unique<T>(MB); 525 526 if (!Config->FirstElf) 527 Config->FirstElf = Ret.get(); 528 529 if (Config->EKind == ELFNoneKind) { 530 Config->EKind = Ret->getELFKind(); 531 Config->EMachine = Ret->getEMachine(); 532 } 533 534 return std::move(Ret); 535 } 536 537 template <template <class> class T> 538 static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) { 539 unsigned char Size; 540 unsigned char Endian; 541 std::tie(Size, Endian) = getElfArchType(MB.getBuffer()); 542 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB) 543 fatal("invalid data encoding: " + MB.getBufferIdentifier()); 544 545 if (Size == ELFCLASS32) { 546 if (Endian == ELFDATA2LSB) 547 return createELFFileAux<T<ELF32LE>>(MB); 548 return createELFFileAux<T<ELF32BE>>(MB); 549 } 550 if (Size == ELFCLASS64) { 551 if (Endian == ELFDATA2LSB) 552 return createELFFileAux<T<ELF64LE>>(MB); 553 return createELFFileAux<T<ELF64BE>>(MB); 554 } 555 fatal("invalid file class: " + MB.getBufferIdentifier()); 556 } 557 558 static bool isBitcode(MemoryBufferRef MB) { 559 using namespace sys::fs; 560 return identify_magic(MB.getBuffer()) == file_magic::bitcode; 561 } 562 563 std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB, 564 StringRef ArchiveName) { 565 std::unique_ptr<InputFile> F; 566 if (isBitcode(MB)) 567 F.reset(new BitcodeFile(MB)); 568 else 569 F = createELFFile<ObjectFile>(MB); 570 F->ArchiveName = ArchiveName; 571 return F; 572 } 573 574 std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) { 575 return createELFFile<SharedFile>(MB); 576 } 577 578 void LazyObjectFile::parse() { 579 for (StringRef Sym : getSymbols()) 580 LazySymbols.emplace_back(Sym, this->MB); 581 } 582 583 template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() { 584 typedef typename ELFT::Shdr Elf_Shdr; 585 typedef typename ELFT::Sym Elf_Sym; 586 typedef typename ELFT::SymRange Elf_Sym_Range; 587 588 const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB); 589 for (const Elf_Shdr &Sec : Obj.sections()) { 590 if (Sec.sh_type != SHT_SYMTAB) 591 continue; 592 Elf_Sym_Range Syms = Obj.symbols(&Sec); 593 uint32_t FirstNonLocal = Sec.sh_info; 594 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec)); 595 std::vector<StringRef> V; 596 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal)) 597 if (Sym.st_shndx != SHN_UNDEF) 598 V.push_back(check(Sym.getName(StringTable))); 599 return V; 600 } 601 return {}; 602 } 603 604 std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() { 605 LLVMContext Context; 606 std::unique_ptr<IRObjectFile> Obj = 607 check(IRObjectFile::create(this->MB, Context)); 608 std::vector<StringRef> V; 609 for (const BasicSymbolRef &Sym : Obj->symbols()) { 610 if (BitcodeFile::shouldSkip(Sym)) 611 continue; 612 if (Sym.getFlags() & BasicSymbolRef::SF_Undefined) 613 continue; 614 SmallString<64> Name; 615 raw_svector_ostream OS(Name); 616 Sym.printName(OS); 617 V.push_back(Saver.save(StringRef(Name))); 618 } 619 return V; 620 } 621 622 // Returns a vector of globally-visible defined symbol names. 623 std::vector<StringRef> LazyObjectFile::getSymbols() { 624 if (isBitcode(this->MB)) 625 return getBitcodeSymbols(); 626 627 unsigned char Size; 628 unsigned char Endian; 629 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer()); 630 if (Size == ELFCLASS32) { 631 if (Endian == ELFDATA2LSB) 632 return getElfSymbols<ELF32LE>(); 633 return getElfSymbols<ELF32BE>(); 634 } 635 if (Endian == ELFDATA2LSB) 636 return getElfSymbols<ELF64LE>(); 637 return getElfSymbols<ELF64BE>(); 638 } 639 640 template class elf::ELFFileBase<ELF32LE>; 641 template class elf::ELFFileBase<ELF32BE>; 642 template class elf::ELFFileBase<ELF64LE>; 643 template class elf::ELFFileBase<ELF64BE>; 644 645 template class elf::ObjectFile<ELF32LE>; 646 template class elf::ObjectFile<ELF32BE>; 647 template class elf::ObjectFile<ELF64LE>; 648 template class elf::ObjectFile<ELF64BE>; 649 650 template class elf::SharedFile<ELF32LE>; 651 template class elf::SharedFile<ELF32BE>; 652 template class elf::SharedFile<ELF64LE>; 653 template class elf::SharedFile<ELF64BE>; 654