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 "LinkerScript.h" 14 #include "Memory.h" 15 #include "SymbolTable.h" 16 #include "Symbols.h" 17 #include "SyntheticSections.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/Bitcode/BitcodeReader.h" 20 #include "llvm/CodeGen/Analysis.h" 21 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 22 #include "llvm/IR/LLVMContext.h" 23 #include "llvm/IR/Module.h" 24 #include "llvm/LTO/LTO.h" 25 #include "llvm/MC/StringTableBuilder.h" 26 #include "llvm/Object/ELFObjectFile.h" 27 #include "llvm/Support/Path.h" 28 #include "llvm/Support/TarWriter.h" 29 #include "llvm/Support/raw_ostream.h" 30 31 using namespace llvm; 32 using namespace llvm::ELF; 33 using namespace llvm::object; 34 using namespace llvm::sys::fs; 35 36 using namespace lld; 37 using namespace lld::elf; 38 39 TarWriter *elf::Tar; 40 41 namespace { 42 // In ELF object file all section addresses are zero. If we have multiple 43 // .text sections (when using -ffunction-section or comdat group) then 44 // LLVM DWARF parser will not be able to parse .debug_line correctly, unless 45 // we assign each section some unique address. This callback method assigns 46 // each section an address equal to its offset in ELF object file. 47 class ObjectInfo : public LoadedObjectInfo { 48 public: 49 uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override { 50 return static_cast<const ELFSectionRef &>(Sec).getOffset(); 51 } 52 std::unique_ptr<LoadedObjectInfo> clone() const override { 53 return std::unique_ptr<LoadedObjectInfo>(); 54 } 55 }; 56 } 57 58 Optional<MemoryBufferRef> elf::readFile(StringRef Path) { 59 log(Path); 60 auto MBOrErr = MemoryBuffer::getFile(Path); 61 if (auto EC = MBOrErr.getError()) { 62 error("cannot open " + Path + ": " + EC.message()); 63 return None; 64 } 65 66 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; 67 MemoryBufferRef MBRef = MB->getMemBufferRef(); 68 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership 69 70 if (Tar) 71 Tar->append(relativeToRoot(Path), MBRef.getBuffer()); 72 return MBRef; 73 } 74 75 template <class ELFT> void elf::ObjectFile<ELFT>::initializeDwarfLine() { 76 std::unique_ptr<object::ObjectFile> Obj = 77 check(object::ObjectFile::createObjectFile(this->MB), 78 "createObjectFile failed"); 79 80 ObjectInfo ObjInfo; 81 DWARFContextInMemory Dwarf(*Obj, &ObjInfo); 82 DwarfLine.reset(new DWARFDebugLine(&Dwarf.getLineSection().Relocs)); 83 DataExtractor LineData(Dwarf.getLineSection().Data, Config->IsLE, 84 Config->Wordsize); 85 86 // The second parameter is offset in .debug_line section 87 // for compilation unit (CU) of interest. We have only one 88 // CU (object file), so offset is always 0. 89 DwarfLine->getOrParseLineTable(LineData, 0); 90 } 91 92 // Returns source line information for a given offset 93 // using DWARF debug info. 94 template <class ELFT> 95 std::string elf::ObjectFile<ELFT>::getLineInfo(InputSectionBase *S, 96 uint64_t Offset) { 97 if (!DwarfLine) 98 initializeDwarfLine(); 99 100 // The offset to CU is 0. 101 const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0); 102 if (!Tbl) 103 return ""; 104 105 // Use fake address calcuated by adding section file offset and offset in 106 // section. See comments for ObjectInfo class. 107 DILineInfo Info; 108 Tbl->getFileLineInfoForAddress( 109 S->getOffsetInFile() + Offset, nullptr, 110 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info); 111 if (Info.Line == 0) 112 return ""; 113 return Info.FileName + ":" + std::to_string(Info.Line); 114 } 115 116 // Returns "(internal)", "foo.a(bar.o)" or "baz.o". 117 std::string lld::toString(const InputFile *F) { 118 if (!F) 119 return "(internal)"; 120 if (!F->ArchiveName.empty()) 121 return (F->ArchiveName + "(" + F->getName() + ")").str(); 122 return F->getName(); 123 } 124 125 template <class ELFT> static ELFKind getELFKind() { 126 if (ELFT::TargetEndianness == support::little) 127 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind; 128 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind; 129 } 130 131 template <class ELFT> 132 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) { 133 EKind = getELFKind<ELFT>(); 134 EMachine = getObj().getHeader()->e_machine; 135 OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI]; 136 } 137 138 template <class ELFT> 139 typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalSymbols() { 140 return makeArrayRef(Symbols.begin() + FirstNonLocal, Symbols.end()); 141 } 142 143 template <class ELFT> 144 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const { 145 return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX)); 146 } 147 148 template <class ELFT> 149 void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections, 150 const Elf_Shdr *Symtab) { 151 FirstNonLocal = Symtab->sh_info; 152 Symbols = check(getObj().symbols(Symtab)); 153 if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size()) 154 fatal(toString(this) + ": invalid sh_info in symbol table"); 155 156 StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections)); 157 } 158 159 template <class ELFT> 160 elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M) 161 : ELFFileBase<ELFT>(Base::ObjectKind, M) {} 162 163 template <class ELFT> 164 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() { 165 return makeArrayRef(this->SymbolBodies).slice(this->FirstNonLocal); 166 } 167 168 template <class ELFT> 169 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() { 170 if (this->SymbolBodies.empty()) 171 return this->SymbolBodies; 172 return makeArrayRef(this->SymbolBodies).slice(1, this->FirstNonLocal - 1); 173 } 174 175 template <class ELFT> 176 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() { 177 if (this->SymbolBodies.empty()) 178 return this->SymbolBodies; 179 return makeArrayRef(this->SymbolBodies).slice(1); 180 } 181 182 template <class ELFT> 183 void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) { 184 // Read section and symbol tables. 185 initializeSections(ComdatGroups); 186 initializeSymbols(); 187 } 188 189 // Sections with SHT_GROUP and comdat bits define comdat section groups. 190 // They are identified and deduplicated by group name. This function 191 // returns a group name. 192 template <class ELFT> 193 StringRef 194 elf::ObjectFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections, 195 const Elf_Shdr &Sec) { 196 if (this->Symbols.empty()) 197 this->initSymtab(Sections, 198 check(object::getSection<ELFT>(Sections, Sec.sh_link))); 199 const Elf_Sym *Sym = 200 check(object::getSymbol<ELFT>(this->Symbols, Sec.sh_info)); 201 return check(Sym->getName(this->StringTable)); 202 } 203 204 template <class ELFT> 205 ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word> 206 elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) { 207 const ELFFile<ELFT> &Obj = this->getObj(); 208 ArrayRef<Elf_Word> Entries = 209 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec)); 210 if (Entries.empty() || Entries[0] != GRP_COMDAT) 211 fatal(toString(this) + ": unsupported SHT_GROUP format"); 212 return Entries.slice(1); 213 } 214 215 template <class ELFT> 216 bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) { 217 // We don't merge sections if -O0 (default is -O1). This makes sometimes 218 // the linker significantly faster, although the output will be bigger. 219 if (Config->Optimize == 0) 220 return false; 221 222 // Do not merge sections if generating a relocatable object. It makes 223 // the code simpler because we do not need to update relocation addends 224 // to reflect changes introduced by merging. Instead of that we write 225 // such "merge" sections into separate OutputSections and keep SHF_MERGE 226 // / SHF_STRINGS flags and sh_entsize value to be able to perform merging 227 // later during a final linking. 228 if (Config->Relocatable) 229 return false; 230 231 // A mergeable section with size 0 is useless because they don't have 232 // any data to merge. A mergeable string section with size 0 can be 233 // argued as invalid because it doesn't end with a null character. 234 // We'll avoid a mess by handling them as if they were non-mergeable. 235 if (Sec.sh_size == 0) 236 return false; 237 238 // Check for sh_entsize. The ELF spec is not clear about the zero 239 // sh_entsize. It says that "the member [sh_entsize] contains 0 if 240 // the section does not hold a table of fixed-size entries". We know 241 // that Rust 1.13 produces a string mergeable section with a zero 242 // sh_entsize. Here we just accept it rather than being picky about it. 243 uint64_t EntSize = Sec.sh_entsize; 244 if (EntSize == 0) 245 return false; 246 if (Sec.sh_size % EntSize) 247 fatal(toString(this) + 248 ": SHF_MERGE section size must be a multiple of sh_entsize"); 249 250 uint64_t Flags = Sec.sh_flags; 251 if (!(Flags & SHF_MERGE)) 252 return false; 253 if (Flags & SHF_WRITE) 254 fatal(toString(this) + ": writable SHF_MERGE section is not supported"); 255 256 // Don't try to merge if the alignment is larger than the sh_entsize and this 257 // is not SHF_STRINGS. 258 // 259 // Since this is not a SHF_STRINGS, we would need to pad after every entity. 260 // It would be equivalent for the producer of the .o to just set a larger 261 // sh_entsize. 262 if (Flags & SHF_STRINGS) 263 return true; 264 265 return Sec.sh_addralign <= EntSize; 266 } 267 268 template <class ELFT> 269 void elf::ObjectFile<ELFT>::initializeSections( 270 DenseSet<CachedHashStringRef> &ComdatGroups) { 271 ArrayRef<Elf_Shdr> ObjSections = check(this->getObj().sections()); 272 const ELFFile<ELFT> &Obj = this->getObj(); 273 uint64_t Size = ObjSections.size(); 274 this->Sections.resize(Size); 275 unsigned I = -1; 276 StringRef SectionStringTable = check(Obj.getSectionStringTable(ObjSections)); 277 for (const Elf_Shdr &Sec : ObjSections) { 278 ++I; 279 if (this->Sections[I] == &InputSection::Discarded) 280 continue; 281 282 // SHF_EXCLUDE'ed sections are discarded by the linker. However, 283 // if -r is given, we'll let the final link discard such sections. 284 // This is compatible with GNU. 285 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) { 286 this->Sections[I] = &InputSection::Discarded; 287 continue; 288 } 289 290 switch (Sec.sh_type) { 291 case SHT_GROUP: 292 this->Sections[I] = &InputSection::Discarded; 293 if (ComdatGroups 294 .insert( 295 CachedHashStringRef(getShtGroupSignature(ObjSections, Sec))) 296 .second) 297 continue; 298 for (uint32_t SecIndex : getShtGroupEntries(Sec)) { 299 if (SecIndex >= Size) 300 fatal(toString(this) + 301 ": invalid section index in group: " + Twine(SecIndex)); 302 this->Sections[SecIndex] = &InputSection::Discarded; 303 } 304 break; 305 case SHT_SYMTAB: 306 this->initSymtab(ObjSections, &Sec); 307 break; 308 case SHT_SYMTAB_SHNDX: 309 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec, ObjSections)); 310 break; 311 case SHT_STRTAB: 312 case SHT_NULL: 313 break; 314 default: 315 this->Sections[I] = createInputSection(Sec, SectionStringTable); 316 } 317 318 // .ARM.exidx sections have a reverse dependency on the InputSection they 319 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link. 320 if (Sec.sh_flags & SHF_LINK_ORDER) { 321 if (Sec.sh_link >= this->Sections.size()) 322 fatal(toString(this) + ": invalid sh_link index: " + 323 Twine(Sec.sh_link)); 324 this->Sections[Sec.sh_link]->DependentSections.push_back( 325 this->Sections[I]); 326 } 327 } 328 } 329 330 template <class ELFT> 331 InputSectionBase *elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) { 332 uint32_t Idx = Sec.sh_info; 333 if (Idx >= this->Sections.size()) 334 fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx)); 335 InputSectionBase *Target = this->Sections[Idx]; 336 337 // Strictly speaking, a relocation section must be included in the 338 // group of the section it relocates. However, LLVM 3.3 and earlier 339 // would fail to do so, so we gracefully handle that case. 340 if (Target == &InputSection::Discarded) 341 return nullptr; 342 343 if (!Target) 344 fatal(toString(this) + ": unsupported relocation reference"); 345 return Target; 346 } 347 348 template <class ELFT> 349 InputSectionBase * 350 elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec, 351 StringRef SectionStringTable) { 352 StringRef Name = 353 check(this->getObj().getSectionName(&Sec, SectionStringTable)); 354 355 switch (Sec.sh_type) { 356 case SHT_ARM_ATTRIBUTES: 357 // FIXME: ARM meta-data section. Retain the first attribute section 358 // we see. The eglibc ARM dynamic loaders require the presence of an 359 // attribute section for dlopen to work. 360 // In a full implementation we would merge all attribute sections. 361 if (In<ELFT>::ARMAttributes == nullptr) { 362 In<ELFT>::ARMAttributes = make<InputSection>(this, &Sec, Name); 363 return In<ELFT>::ARMAttributes; 364 } 365 return &InputSection::Discarded; 366 case SHT_RELA: 367 case SHT_REL: { 368 // Find the relocation target section and associate this 369 // section with it. Target can be discarded, for example 370 // if it is a duplicated member of SHT_GROUP section, we 371 // do not create or proccess relocatable sections then. 372 InputSectionBase *Target = getRelocTarget(Sec); 373 if (!Target) 374 return nullptr; 375 376 // This section contains relocation information. 377 // If -r is given, we do not interpret or apply relocation 378 // but just copy relocation sections to output. 379 if (Config->Relocatable) 380 return make<InputSection>(this, &Sec, Name); 381 382 if (Target->FirstRelocation) 383 fatal(toString(this) + 384 ": multiple relocation sections to one section are not supported"); 385 if (isa<MergeInputSection>(Target)) 386 fatal(toString(this) + 387 ": relocations pointing to SHF_MERGE are not supported"); 388 389 size_t NumRelocations; 390 if (Sec.sh_type == SHT_RELA) { 391 ArrayRef<Elf_Rela> Rels = check(this->getObj().relas(&Sec)); 392 Target->FirstRelocation = Rels.begin(); 393 NumRelocations = Rels.size(); 394 Target->AreRelocsRela = true; 395 } else { 396 ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec)); 397 Target->FirstRelocation = Rels.begin(); 398 NumRelocations = Rels.size(); 399 Target->AreRelocsRela = false; 400 } 401 assert(isUInt<31>(NumRelocations)); 402 Target->NumRelocations = NumRelocations; 403 404 // Relocation sections processed by the linker are usually removed 405 // from the output, so returning `nullptr` for the normal case. 406 // However, if -emit-relocs is given, we need to leave them in the output. 407 // (Some post link analysis tools need this information.) 408 if (Config->EmitRelocs) { 409 InputSection *RelocSec = make<InputSection>(this, &Sec, Name); 410 // We will not emit relocation section if target was discarded. 411 Target->DependentSections.push_back(RelocSec); 412 return RelocSec; 413 } 414 return nullptr; 415 } 416 } 417 418 // The GNU linker uses .note.GNU-stack section as a marker indicating 419 // that the code in the object file does not expect that the stack is 420 // executable (in terms of NX bit). If all input files have the marker, 421 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to 422 // make the stack non-executable. Most object files have this section as 423 // of 2017. 424 // 425 // But making the stack non-executable is a norm today for security 426 // reasons. Failure to do so may result in a serious security issue. 427 // Therefore, we make LLD always add PT_GNU_STACK unless it is 428 // explicitly told to do otherwise (by -z execstack). Because the stack 429 // executable-ness is controlled solely by command line options, 430 // .note.GNU-stack sections are simply ignored. 431 if (Name == ".note.GNU-stack") 432 return &InputSection::Discarded; 433 434 // Split stacks is a feature to support a discontiguous stack. At least 435 // as of 2017, it seems that the feature is not being used widely. 436 // Only GNU gold supports that. We don't. For the details about that, 437 // see https://gcc.gnu.org/wiki/SplitStacks 438 if (Name == ".note.GNU-split-stack") { 439 error(toString(this) + 440 ": object file compiled with -fsplit-stack is not supported"); 441 return &InputSection::Discarded; 442 } 443 444 if (Config->Strip != StripPolicy::None && Name.startswith(".debug")) 445 return &InputSection::Discarded; 446 447 // The linkonce feature is a sort of proto-comdat. Some glibc i386 object 448 // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce 449 // sections. Drop those sections to avoid duplicate symbol errors. 450 // FIXME: This is glibc PR20543, we should remove this hack once that has been 451 // fixed for a while. 452 if (Name.startswith(".gnu.linkonce.")) 453 return &InputSection::Discarded; 454 455 // The linker merges EH (exception handling) frames and creates a 456 // .eh_frame_hdr section for runtime. So we handle them with a special 457 // class. For relocatable outputs, they are just passed through. 458 if (Name == ".eh_frame" && !Config->Relocatable) 459 return make<EhInputSection>(this, &Sec, Name); 460 461 if (shouldMerge(Sec)) 462 return make<MergeInputSection>(this, &Sec, Name); 463 return make<InputSection>(this, &Sec, Name); 464 } 465 466 template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() { 467 SymbolBodies.reserve(this->Symbols.size()); 468 for (const Elf_Sym &Sym : this->Symbols) 469 SymbolBodies.push_back(createSymbolBody(&Sym)); 470 } 471 472 template <class ELFT> 473 InputSectionBase *elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const { 474 uint32_t Index = this->getSectionIndex(Sym); 475 if (Index >= this->Sections.size()) 476 fatal(toString(this) + ": invalid section index: " + Twine(Index)); 477 InputSectionBase *S = this->Sections[Index]; 478 479 // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could 480 // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be 481 // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections. 482 // In this case it is fine for section to be null here as we do not 483 // allocate sections of these types. 484 if (!S) { 485 if (Index == 0 || Sym.getType() == STT_SECTION || 486 Sym.getType() == STT_NOTYPE) 487 return nullptr; 488 fatal(toString(this) + ": invalid section index: " + Twine(Index)); 489 } 490 491 if (S == &InputSection::Discarded) 492 return S; 493 return S->Repl; 494 } 495 496 template <class ELFT> 497 SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) { 498 int Binding = Sym->getBinding(); 499 InputSectionBase *Sec = getSection(*Sym); 500 501 uint8_t StOther = Sym->st_other; 502 uint8_t Type = Sym->getType(); 503 uint64_t Value = Sym->st_value; 504 uint64_t Size = Sym->st_size; 505 506 if (Binding == STB_LOCAL) { 507 if (Sym->getType() == STT_FILE) 508 SourceFile = check(Sym->getName(this->StringTable)); 509 510 if (this->StringTable.size() <= Sym->st_name) 511 fatal(toString(this) + ": invalid symbol name offset"); 512 513 StringRefZ Name = this->StringTable.data() + Sym->st_name; 514 if (Sym->st_shndx == SHN_UNDEF) 515 return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type, this); 516 517 return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value, 518 Size, Sec, this); 519 } 520 521 StringRef Name = check(Sym->getName(this->StringTable)); 522 523 switch (Sym->st_shndx) { 524 case SHN_UNDEF: 525 return elf::Symtab<ELFT>::X 526 ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type, 527 /*CanOmitFromDynSym=*/false, this) 528 ->body(); 529 case SHN_COMMON: 530 if (Value == 0 || Value >= UINT32_MAX) 531 fatal(toString(this) + ": common symbol '" + Name + 532 "' has invalid alignment: " + Twine(Value)); 533 return elf::Symtab<ELFT>::X 534 ->addCommon(Name, Size, Value, Binding, StOther, Type, this) 535 ->body(); 536 } 537 538 switch (Binding) { 539 default: 540 fatal(toString(this) + ": unexpected binding: " + Twine(Binding)); 541 case STB_GLOBAL: 542 case STB_WEAK: 543 case STB_GNU_UNIQUE: 544 if (Sec == &InputSection::Discarded) 545 return elf::Symtab<ELFT>::X 546 ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type, 547 /*CanOmitFromDynSym=*/false, this) 548 ->body(); 549 return elf::Symtab<ELFT>::X 550 ->addRegular(Name, StOther, Type, Value, Size, Binding, Sec, this) 551 ->body(); 552 } 553 } 554 555 template <class ELFT> void ArchiveFile::parse() { 556 File = check(Archive::create(MB), 557 MB.getBufferIdentifier() + ": failed to parse archive"); 558 559 // Read the symbol table to construct Lazy objects. 560 for (const Archive::Symbol &Sym : File->symbols()) { 561 Symtab<ELFT>::X->addLazyArchive(this, Sym); 562 } 563 564 if (File->symbols().begin() == File->symbols().end()) 565 Config->ArchiveWithoutSymbolsSeen = true; 566 } 567 568 // Returns a buffer pointing to a member file containing a given symbol. 569 std::pair<MemoryBufferRef, uint64_t> 570 ArchiveFile::getMember(const Archive::Symbol *Sym) { 571 Archive::Child C = 572 check(Sym->getMember(), 573 "could not get the member for symbol " + Sym->getName()); 574 575 if (!Seen.insert(C.getChildOffset()).second) 576 return {MemoryBufferRef(), 0}; 577 578 MemoryBufferRef Ret = 579 check(C.getMemoryBufferRef(), 580 "could not get the buffer for the member defining symbol " + 581 Sym->getName()); 582 583 if (C.getParent()->isThin() && Tar) 584 Tar->append(relativeToRoot(check(C.getFullName())), Ret.getBuffer()); 585 if (C.getParent()->isThin()) 586 return {Ret, 0}; 587 return {Ret, C.getChildOffset()}; 588 } 589 590 template <class ELFT> 591 SharedFile<ELFT>::SharedFile(MemoryBufferRef M) 592 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {} 593 594 template <class ELFT> 595 const typename ELFT::Shdr * 596 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const { 597 return check( 598 this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX)); 599 } 600 601 // Partially parse the shared object file so that we can call 602 // getSoName on this object. 603 template <class ELFT> void SharedFile<ELFT>::parseSoName() { 604 const Elf_Shdr *DynamicSec = nullptr; 605 606 const ELFFile<ELFT> Obj = this->getObj(); 607 ArrayRef<Elf_Shdr> Sections = check(Obj.sections()); 608 for (const Elf_Shdr &Sec : Sections) { 609 switch (Sec.sh_type) { 610 default: 611 continue; 612 case SHT_DYNSYM: 613 this->initSymtab(Sections, &Sec); 614 break; 615 case SHT_DYNAMIC: 616 DynamicSec = &Sec; 617 break; 618 case SHT_SYMTAB_SHNDX: 619 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec, Sections)); 620 break; 621 case SHT_GNU_versym: 622 this->VersymSec = &Sec; 623 break; 624 case SHT_GNU_verdef: 625 this->VerdefSec = &Sec; 626 break; 627 } 628 } 629 630 if (this->VersymSec && this->Symbols.empty()) 631 error("SHT_GNU_versym should be associated with symbol table"); 632 633 // DSOs are identified by soname, and they usually contain 634 // DT_SONAME tag in their header. But if they are missing, 635 // filenames are used as default sonames. 636 SoName = sys::path::filename(this->getName()); 637 638 if (!DynamicSec) 639 return; 640 641 ArrayRef<Elf_Dyn> Arr = 642 check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), 643 toString(this) + ": getSectionContentsAsArray failed"); 644 for (const Elf_Dyn &Dyn : Arr) { 645 if (Dyn.d_tag == DT_SONAME) { 646 uint64_t Val = Dyn.getVal(); 647 if (Val >= this->StringTable.size()) 648 fatal(toString(this) + ": invalid DT_SONAME entry"); 649 SoName = StringRef(this->StringTable.data() + Val); 650 return; 651 } 652 } 653 } 654 655 // Parse the version definitions in the object file if present. Returns a vector 656 // whose nth element contains a pointer to the Elf_Verdef for version identifier 657 // n. Version identifiers that are not definitions map to nullptr. The array 658 // always has at least length 1. 659 template <class ELFT> 660 std::vector<const typename ELFT::Verdef *> 661 SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) { 662 std::vector<const Elf_Verdef *> Verdefs(1); 663 // We only need to process symbol versions for this DSO if it has both a 664 // versym and a verdef section, which indicates that the DSO contains symbol 665 // version definitions. 666 if (!VersymSec || !VerdefSec) 667 return Verdefs; 668 669 // The location of the first global versym entry. 670 const char *Base = this->MB.getBuffer().data(); 671 Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) + 672 this->FirstNonLocal; 673 674 // We cannot determine the largest verdef identifier without inspecting 675 // every Elf_Verdef, but both bfd and gold assign verdef identifiers 676 // sequentially starting from 1, so we predict that the largest identifier 677 // will be VerdefCount. 678 unsigned VerdefCount = VerdefSec->sh_info; 679 Verdefs.resize(VerdefCount + 1); 680 681 // Build the Verdefs array by following the chain of Elf_Verdef objects 682 // from the start of the .gnu.version_d section. 683 const char *Verdef = Base + VerdefSec->sh_offset; 684 for (unsigned I = 0; I != VerdefCount; ++I) { 685 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef); 686 Verdef += CurVerdef->vd_next; 687 unsigned VerdefIndex = CurVerdef->vd_ndx; 688 if (Verdefs.size() <= VerdefIndex) 689 Verdefs.resize(VerdefIndex + 1); 690 Verdefs[VerdefIndex] = CurVerdef; 691 } 692 693 return Verdefs; 694 } 695 696 // Fully parse the shared object file. This must be called after parseSoName(). 697 template <class ELFT> void SharedFile<ELFT>::parseRest() { 698 // Create mapping from version identifiers to Elf_Verdef entries. 699 const Elf_Versym *Versym = nullptr; 700 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym); 701 702 Elf_Sym_Range Syms = this->getGlobalSymbols(); 703 for (const Elf_Sym &Sym : Syms) { 704 unsigned VersymIndex = 0; 705 if (Versym) { 706 VersymIndex = Versym->vs_index; 707 ++Versym; 708 } 709 bool Hidden = VersymIndex & VERSYM_HIDDEN; 710 VersymIndex = VersymIndex & ~VERSYM_HIDDEN; 711 712 StringRef Name = check(Sym.getName(this->StringTable)); 713 if (Sym.isUndefined()) { 714 Undefs.push_back(Name); 715 continue; 716 } 717 718 // Ignore local symbols. 719 if (Versym && VersymIndex == VER_NDX_LOCAL) 720 continue; 721 722 const Elf_Verdef *V = 723 VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex]; 724 725 if (!Hidden) 726 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V); 727 728 // Also add the symbol with the versioned name to handle undefined symbols 729 // with explicit versions. 730 if (V) { 731 StringRef VerName = this->StringTable.data() + V->getAux()->vda_name; 732 Name = Saver.save(Twine(Name) + "@" + VerName); 733 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V); 734 } 735 } 736 } 737 738 static ELFKind getBitcodeELFKind(MemoryBufferRef MB) { 739 Triple T(check(getBitcodeTargetTriple(MB))); 740 if (T.isLittleEndian()) 741 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind; 742 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind; 743 } 744 745 static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) { 746 Triple T(check(getBitcodeTargetTriple(MB))); 747 switch (T.getArch()) { 748 case Triple::aarch64: 749 return EM_AARCH64; 750 case Triple::arm: 751 case Triple::thumb: 752 return EM_ARM; 753 case Triple::mips: 754 case Triple::mipsel: 755 case Triple::mips64: 756 case Triple::mips64el: 757 return EM_MIPS; 758 case Triple::ppc: 759 return EM_PPC; 760 case Triple::ppc64: 761 return EM_PPC64; 762 case Triple::x86: 763 return T.isOSIAMCU() ? EM_IAMCU : EM_386; 764 case Triple::x86_64: 765 return EM_X86_64; 766 default: 767 fatal(MB.getBufferIdentifier() + 768 ": could not infer e_machine from bitcode target triple " + T.str()); 769 } 770 } 771 772 BitcodeFile::BitcodeFile(MemoryBufferRef MB, uint64_t OffsetInArchive) 773 : InputFile(BitcodeKind, MB), OffsetInArchive(OffsetInArchive) { 774 EKind = getBitcodeELFKind(MB); 775 EMachine = getBitcodeMachineKind(MB); 776 } 777 778 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) { 779 switch (GvVisibility) { 780 case GlobalValue::DefaultVisibility: 781 return STV_DEFAULT; 782 case GlobalValue::HiddenVisibility: 783 return STV_HIDDEN; 784 case GlobalValue::ProtectedVisibility: 785 return STV_PROTECTED; 786 } 787 llvm_unreachable("unknown visibility"); 788 } 789 790 template <class ELFT> 791 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats, 792 const lto::InputFile::Symbol &ObjSym, 793 BitcodeFile *F) { 794 StringRef NameRef = Saver.save(ObjSym.getName()); 795 uint32_t Flags = ObjSym.getFlags(); 796 uint32_t Binding = (Flags & BasicSymbolRef::SF_Weak) ? STB_WEAK : STB_GLOBAL; 797 798 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE; 799 uint8_t Visibility = mapVisibility(ObjSym.getVisibility()); 800 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable(); 801 802 int C = check(ObjSym.getComdatIndex()); 803 if (C != -1 && !KeptComdats[C]) 804 return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding, 805 Visibility, Type, CanOmitFromDynSym, 806 F); 807 808 if (Flags & BasicSymbolRef::SF_Undefined) 809 return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding, 810 Visibility, Type, CanOmitFromDynSym, 811 F); 812 813 if (Flags & BasicSymbolRef::SF_Common) 814 return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(), 815 ObjSym.getCommonAlignment(), Binding, 816 Visibility, STT_OBJECT, F); 817 818 return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type, 819 CanOmitFromDynSym, F); 820 } 821 822 template <class ELFT> 823 void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) { 824 825 // Here we pass a new MemoryBufferRef which is identified by ArchiveName 826 // (the fully resolved path of the archive) + member name + offset of the 827 // member in the archive. 828 // ThinLTO uses the MemoryBufferRef identifier to access its internal 829 // data structures and if two archives define two members with the same name, 830 // this causes a collision which result in only one of the objects being 831 // taken into consideration at LTO time (which very likely causes undefined 832 // symbols later in the link stage). 833 Obj = check(lto::InputFile::create(MemoryBufferRef( 834 MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier() + 835 utostr(OffsetInArchive))))); 836 837 std::vector<bool> KeptComdats; 838 for (StringRef S : Obj->getComdatTable()) { 839 StringRef N = Saver.save(S); 840 KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(N)).second); 841 } 842 843 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) 844 Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this)); 845 } 846 847 template <template <class> class T> 848 static InputFile *createELFFile(MemoryBufferRef MB) { 849 unsigned char Size; 850 unsigned char Endian; 851 std::tie(Size, Endian) = getElfArchType(MB.getBuffer()); 852 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB) 853 fatal(MB.getBufferIdentifier() + ": invalid data encoding"); 854 855 size_t BufSize = MB.getBuffer().size(); 856 if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) || 857 (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr))) 858 fatal(MB.getBufferIdentifier() + ": file is too short"); 859 860 InputFile *Obj; 861 if (Size == ELFCLASS32 && Endian == ELFDATA2LSB) 862 Obj = make<T<ELF32LE>>(MB); 863 else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB) 864 Obj = make<T<ELF32BE>>(MB); 865 else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB) 866 Obj = make<T<ELF64LE>>(MB); 867 else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB) 868 Obj = make<T<ELF64BE>>(MB); 869 else 870 fatal(MB.getBufferIdentifier() + ": invalid file class"); 871 872 if (!Config->FirstElf) 873 Config->FirstElf = Obj; 874 return Obj; 875 } 876 877 template <class ELFT> void BinaryFile::parse() { 878 StringRef Buf = MB.getBuffer(); 879 ArrayRef<uint8_t> Data = 880 makeArrayRef<uint8_t>((const uint8_t *)Buf.data(), Buf.size()); 881 882 std::string Filename = MB.getBufferIdentifier(); 883 std::transform(Filename.begin(), Filename.end(), Filename.begin(), 884 [](char C) { return isalnum(C) ? C : '_'; }); 885 Filename = "_binary_" + Filename; 886 StringRef StartName = Saver.save(Twine(Filename) + "_start"); 887 StringRef EndName = Saver.save(Twine(Filename) + "_end"); 888 StringRef SizeName = Saver.save(Twine(Filename) + "_size"); 889 890 auto *Section = 891 make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, Data, ".data"); 892 Sections.push_back(Section); 893 894 elf::Symtab<ELFT>::X->addRegular(StartName, STV_DEFAULT, STT_OBJECT, 0, 0, 895 STB_GLOBAL, Section, nullptr); 896 elf::Symtab<ELFT>::X->addRegular(EndName, STV_DEFAULT, STT_OBJECT, 897 Data.size(), 0, STB_GLOBAL, Section, 898 nullptr); 899 elf::Symtab<ELFT>::X->addRegular(SizeName, STV_DEFAULT, STT_OBJECT, 900 Data.size(), 0, STB_GLOBAL, nullptr, 901 nullptr); 902 } 903 904 static bool isBitcode(MemoryBufferRef MB) { 905 using namespace sys::fs; 906 return identify_magic(MB.getBuffer()) == file_magic::bitcode; 907 } 908 909 InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName, 910 uint64_t OffsetInArchive) { 911 InputFile *F = isBitcode(MB) ? make<BitcodeFile>(MB, OffsetInArchive) 912 : createELFFile<ObjectFile>(MB); 913 F->ArchiveName = ArchiveName; 914 return F; 915 } 916 917 InputFile *elf::createSharedFile(MemoryBufferRef MB) { 918 return createELFFile<SharedFile>(MB); 919 } 920 921 MemoryBufferRef LazyObjectFile::getBuffer() { 922 if (Seen) 923 return MemoryBufferRef(); 924 Seen = true; 925 return MB; 926 } 927 928 template <class ELFT> void LazyObjectFile::parse() { 929 for (StringRef Sym : getSymbols()) 930 Symtab<ELFT>::X->addLazyObject(Sym, *this); 931 } 932 933 template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() { 934 typedef typename ELFT::Shdr Elf_Shdr; 935 typedef typename ELFT::Sym Elf_Sym; 936 typedef typename ELFT::SymRange Elf_Sym_Range; 937 938 const ELFFile<ELFT> Obj(this->MB.getBuffer()); 939 ArrayRef<Elf_Shdr> Sections = check(Obj.sections()); 940 for (const Elf_Shdr &Sec : Sections) { 941 if (Sec.sh_type != SHT_SYMTAB) 942 continue; 943 Elf_Sym_Range Syms = check(Obj.symbols(&Sec)); 944 uint32_t FirstNonLocal = Sec.sh_info; 945 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec, Sections)); 946 std::vector<StringRef> V; 947 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal)) 948 if (Sym.st_shndx != SHN_UNDEF) 949 V.push_back(check(Sym.getName(StringTable))); 950 return V; 951 } 952 return {}; 953 } 954 955 std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() { 956 std::unique_ptr<lto::InputFile> Obj = check(lto::InputFile::create(this->MB)); 957 std::vector<StringRef> V; 958 for (const lto::InputFile::Symbol &Sym : Obj->symbols()) 959 if (!(Sym.getFlags() & BasicSymbolRef::SF_Undefined)) 960 V.push_back(Saver.save(Sym.getName())); 961 return V; 962 } 963 964 // Returns a vector of globally-visible defined symbol names. 965 std::vector<StringRef> LazyObjectFile::getSymbols() { 966 if (isBitcode(this->MB)) 967 return getBitcodeSymbols(); 968 969 unsigned char Size; 970 unsigned char Endian; 971 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer()); 972 if (Size == ELFCLASS32) { 973 if (Endian == ELFDATA2LSB) 974 return getElfSymbols<ELF32LE>(); 975 return getElfSymbols<ELF32BE>(); 976 } 977 if (Endian == ELFDATA2LSB) 978 return getElfSymbols<ELF64LE>(); 979 return getElfSymbols<ELF64BE>(); 980 } 981 982 template void ArchiveFile::parse<ELF32LE>(); 983 template void ArchiveFile::parse<ELF32BE>(); 984 template void ArchiveFile::parse<ELF64LE>(); 985 template void ArchiveFile::parse<ELF64BE>(); 986 987 template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &); 988 template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &); 989 template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &); 990 template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &); 991 992 template void LazyObjectFile::parse<ELF32LE>(); 993 template void LazyObjectFile::parse<ELF32BE>(); 994 template void LazyObjectFile::parse<ELF64LE>(); 995 template void LazyObjectFile::parse<ELF64BE>(); 996 997 template class elf::ELFFileBase<ELF32LE>; 998 template class elf::ELFFileBase<ELF32BE>; 999 template class elf::ELFFileBase<ELF64LE>; 1000 template class elf::ELFFileBase<ELF64BE>; 1001 1002 template class elf::ObjectFile<ELF32LE>; 1003 template class elf::ObjectFile<ELF32BE>; 1004 template class elf::ObjectFile<ELF64LE>; 1005 template class elf::ObjectFile<ELF64BE>; 1006 1007 template class elf::SharedFile<ELF32LE>; 1008 template class elf::SharedFile<ELF32BE>; 1009 template class elf::SharedFile<ELF64LE>; 1010 template class elf::SharedFile<ELF64BE>; 1011 1012 template void BinaryFile::parse<ELF32LE>(); 1013 template void BinaryFile::parse<ELF32BE>(); 1014 template void BinaryFile::parse<ELF64LE>(); 1015 template void BinaryFile::parse<ELF64BE>(); 1016