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