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