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 if (GV) 465 if (const Comdat *C = GV->getComdat()) 466 if (!KeptComdats.count(C)) 467 return nullptr; 468 469 uint32_t Flags = Sym.getFlags(); 470 uint8_t Visibility; 471 if (GV) 472 Visibility = getGvVisibility(GV); 473 else 474 // FIXME: Set SF_Hidden flag correctly for module asm symbols, and expose 475 // protected visibility. 476 Visibility = STV_DEFAULT; 477 478 SmallString<64> Name; 479 raw_svector_ostream OS(Name); 480 Sym.printName(OS); 481 StringRef NameRef = Saver.save(StringRef(Name)); 482 483 const Module &M = Obj.getModule(); 484 SymbolBody *Body; 485 bool IsWeak = Flags & BasicSymbolRef::SF_Weak; 486 if (Flags & BasicSymbolRef::SF_Undefined) { 487 Body = new (Alloc) UndefinedBitcode(NameRef, IsWeak, Visibility); 488 } else if (Flags & BasicSymbolRef::SF_Common) { 489 // FIXME: Set SF_Common flag correctly for module asm symbols, and expose 490 // size and alignment. 491 assert(GV); 492 const DataLayout &DL = M.getDataLayout(); 493 uint64_t Size = DL.getTypeAllocSize(GV->getValueType()); 494 Body = new (Alloc) 495 DefinedCommon(NameRef, Size, GV->getAlignment(), 496 IsWeak ? STB_WEAK : STB_GLOBAL, Visibility, /*Type*/ 0); 497 } else { 498 Body = new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility); 499 } 500 // FIXME: Expose a thread-local flag for module asm symbols. 501 if (GV && GV->isThreadLocal()) 502 Body->Type = STT_TLS; 503 return Body; 504 } 505 506 bool BitcodeFile::shouldSkip(const BasicSymbolRef &Sym) { 507 uint32_t Flags = Sym.getFlags(); 508 if (!(Flags & BasicSymbolRef::SF_Global)) 509 return true; 510 if (Flags & BasicSymbolRef::SF_FormatSpecific) 511 return true; 512 return false; 513 } 514 515 void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) { 516 LLVMContext Context; 517 std::unique_ptr<IRObjectFile> Obj = check(IRObjectFile::create(MB, Context)); 518 const Module &M = Obj->getModule(); 519 520 DenseSet<const Comdat *> KeptComdats; 521 for (const auto &P : M.getComdatSymbolTable()) { 522 StringRef N = Saver.save(P.first()); 523 if (ComdatGroups.insert(N).second) 524 KeptComdats.insert(&P.second); 525 } 526 527 for (const BasicSymbolRef &Sym : Obj->symbols()) 528 if (!shouldSkip(Sym)) 529 SymbolBodies.push_back(createSymbolBody(KeptComdats, *Obj, Sym)); 530 } 531 532 template <typename T> 533 static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) { 534 std::unique_ptr<T> Ret = llvm::make_unique<T>(MB); 535 536 if (!Config->FirstElf) 537 Config->FirstElf = Ret.get(); 538 539 if (Config->EKind == ELFNoneKind) { 540 Config->EKind = Ret->getELFKind(); 541 Config->EMachine = Ret->getEMachine(); 542 } 543 544 return std::move(Ret); 545 } 546 547 template <template <class> class T> 548 static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) { 549 unsigned char Size; 550 unsigned char Endian; 551 std::tie(Size, Endian) = getElfArchType(MB.getBuffer()); 552 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB) 553 fatal("invalid data encoding: " + MB.getBufferIdentifier()); 554 555 if (Size == ELFCLASS32) { 556 if (Endian == ELFDATA2LSB) 557 return createELFFileAux<T<ELF32LE>>(MB); 558 return createELFFileAux<T<ELF32BE>>(MB); 559 } 560 if (Size == ELFCLASS64) { 561 if (Endian == ELFDATA2LSB) 562 return createELFFileAux<T<ELF64LE>>(MB); 563 return createELFFileAux<T<ELF64BE>>(MB); 564 } 565 fatal("invalid file class: " + MB.getBufferIdentifier()); 566 } 567 568 static bool isBitcode(MemoryBufferRef MB) { 569 using namespace sys::fs; 570 return identify_magic(MB.getBuffer()) == file_magic::bitcode; 571 } 572 573 std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB, 574 StringRef ArchiveName) { 575 std::unique_ptr<InputFile> F; 576 if (isBitcode(MB)) 577 F.reset(new BitcodeFile(MB)); 578 else 579 F = createELFFile<ObjectFile>(MB); 580 F->ArchiveName = ArchiveName; 581 return F; 582 } 583 584 std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) { 585 return createELFFile<SharedFile>(MB); 586 } 587 588 void LazyObjectFile::parse() { 589 for (StringRef Sym : getSymbols()) 590 LazySymbols.emplace_back(Sym, this->MB); 591 } 592 593 template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() { 594 typedef typename ELFT::Shdr Elf_Shdr; 595 typedef typename ELFT::Sym Elf_Sym; 596 typedef typename ELFT::SymRange Elf_Sym_Range; 597 598 const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB); 599 for (const Elf_Shdr &Sec : Obj.sections()) { 600 if (Sec.sh_type != SHT_SYMTAB) 601 continue; 602 Elf_Sym_Range Syms = Obj.symbols(&Sec); 603 uint32_t FirstNonLocal = Sec.sh_info; 604 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec)); 605 std::vector<StringRef> V; 606 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal)) 607 if (Sym.st_shndx != SHN_UNDEF) 608 V.push_back(check(Sym.getName(StringTable))); 609 return V; 610 } 611 return {}; 612 } 613 614 std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() { 615 LLVMContext Context; 616 std::unique_ptr<IRObjectFile> Obj = 617 check(IRObjectFile::create(this->MB, Context)); 618 std::vector<StringRef> V; 619 for (const BasicSymbolRef &Sym : Obj->symbols()) { 620 if (BitcodeFile::shouldSkip(Sym)) 621 continue; 622 if (Sym.getFlags() & BasicSymbolRef::SF_Undefined) 623 continue; 624 SmallString<64> Name; 625 raw_svector_ostream OS(Name); 626 Sym.printName(OS); 627 V.push_back(Saver.save(StringRef(Name))); 628 } 629 return V; 630 } 631 632 // Returns a vector of globally-visible defined symbol names. 633 std::vector<StringRef> LazyObjectFile::getSymbols() { 634 if (isBitcode(this->MB)) 635 return getBitcodeSymbols(); 636 637 unsigned char Size; 638 unsigned char Endian; 639 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer()); 640 if (Size == ELFCLASS32) { 641 if (Endian == ELFDATA2LSB) 642 return getElfSymbols<ELF32LE>(); 643 return getElfSymbols<ELF32BE>(); 644 } 645 if (Endian == ELFDATA2LSB) 646 return getElfSymbols<ELF64LE>(); 647 return getElfSymbols<ELF64BE>(); 648 } 649 650 template class elf::ELFFileBase<ELF32LE>; 651 template class elf::ELFFileBase<ELF32BE>; 652 template class elf::ELFFileBase<ELF64LE>; 653 template class elf::ELFFileBase<ELF64BE>; 654 655 template class elf::ObjectFile<ELF32LE>; 656 template class elf::ObjectFile<ELF32BE>; 657 template class elf::ObjectFile<ELF64LE>; 658 template class elf::ObjectFile<ELF64BE>; 659 660 template class elf::SharedFile<ELF32LE>; 661 template class elf::SharedFile<ELF32BE>; 662 template class elf::SharedFile<ELF64LE>; 663 template class elf::SharedFile<ELF64BE>; 664