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