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 "InputSection.h" 12 #include "LinkerScript.h" 13 #include "SymbolTable.h" 14 #include "Symbols.h" 15 #include "SyntheticSections.h" 16 #include "lld/Common/ErrorHandler.h" 17 #include "lld/Common/Memory.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/CodeGen/Analysis.h" 20 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 21 #include "llvm/IR/LLVMContext.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/LTO/LTO.h" 24 #include "llvm/MC/StringTableBuilder.h" 25 #include "llvm/Object/ELFObjectFile.h" 26 #include "llvm/Support/ARMAttributeParser.h" 27 #include "llvm/Support/ARMBuildAttributes.h" 28 #include "llvm/Support/Path.h" 29 #include "llvm/Support/TarWriter.h" 30 #include "llvm/Support/raw_ostream.h" 31 32 using namespace llvm; 33 using namespace llvm::ELF; 34 using namespace llvm::object; 35 using namespace llvm::sys; 36 using namespace llvm::sys::fs; 37 38 using namespace lld; 39 using namespace lld::elf; 40 41 bool InputFile::IsInGroup; 42 uint32_t InputFile::NextGroupId; 43 std::vector<BinaryFile *> elf::BinaryFiles; 44 std::vector<BitcodeFile *> elf::BitcodeFiles; 45 std::vector<InputFile *> elf::ObjectFiles; 46 std::vector<InputFile *> elf::SharedFiles; 47 48 TarWriter *elf::Tar; 49 50 InputFile::InputFile(Kind K, MemoryBufferRef M) 51 : MB(M), GroupId(NextGroupId), FileKind(K) { 52 // All files within the same --{start,end}-group get the same group ID. 53 // Otherwise, a new file will get a new group ID. 54 if (!IsInGroup) 55 ++NextGroupId; 56 } 57 58 Optional<MemoryBufferRef> elf::readFile(StringRef Path) { 59 // The --chroot option changes our virtual root directory. 60 // This is useful when you are dealing with files created by --reproduce. 61 if (!Config->Chroot.empty() && Path.startswith("/")) 62 Path = Saver.save(Config->Chroot + Path); 63 64 log(Path); 65 66 auto MBOrErr = MemoryBuffer::getFile(Path); 67 if (auto EC = MBOrErr.getError()) { 68 error("cannot open " + Path + ": " + EC.message()); 69 return None; 70 } 71 72 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; 73 MemoryBufferRef MBRef = MB->getMemBufferRef(); 74 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership 75 76 if (Tar) 77 Tar->append(relativeToRoot(Path), MBRef.getBuffer()); 78 return MBRef; 79 } 80 81 // Concatenates arguments to construct a string representing an error location. 82 static std::string createFileLineMsg(StringRef Path, unsigned Line) { 83 std::string Filename = path::filename(Path); 84 std::string Lineno = ":" + std::to_string(Line); 85 if (Filename == Path) 86 return Filename + Lineno; 87 return Filename + Lineno + " (" + Path.str() + Lineno + ")"; 88 } 89 90 template <class ELFT> 91 static std::string getSrcMsgAux(ObjFile<ELFT> &File, const Symbol &Sym, 92 InputSectionBase &Sec, uint64_t Offset) { 93 // In DWARF, functions and variables are stored to different places. 94 // First, lookup a function for a given offset. 95 if (Optional<DILineInfo> Info = File.getDILineInfo(&Sec, Offset)) 96 return createFileLineMsg(Info->FileName, Info->Line); 97 98 // If it failed, lookup again as a variable. 99 if (Optional<std::pair<std::string, unsigned>> FileLine = 100 File.getVariableLoc(Sym.getName())) 101 return createFileLineMsg(FileLine->first, FileLine->second); 102 103 // File.SourceFile contains STT_FILE symbol, and that is a last resort. 104 return File.SourceFile; 105 } 106 107 std::string InputFile::getSrcMsg(const Symbol &Sym, InputSectionBase &Sec, 108 uint64_t Offset) { 109 if (kind() != ObjKind) 110 return ""; 111 switch (Config->EKind) { 112 default: 113 llvm_unreachable("Invalid kind"); 114 case ELF32LEKind: 115 return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), Sym, Sec, Offset); 116 case ELF32BEKind: 117 return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), Sym, Sec, Offset); 118 case ELF64LEKind: 119 return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), Sym, Sec, Offset); 120 case ELF64BEKind: 121 return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), Sym, Sec, Offset); 122 } 123 } 124 125 template <class ELFT> void ObjFile<ELFT>::initializeDwarf() { 126 Dwarf = llvm::make_unique<DWARFContext>(make_unique<LLDDwarfObj<ELFT>>(this)); 127 const DWARFObject &Obj = Dwarf->getDWARFObj(); 128 DwarfLine.reset(new DWARFDebugLine); 129 DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE, 130 Config->Wordsize); 131 132 for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf->compile_units()) { 133 const DWARFDebugLine::LineTable *LT = Dwarf->getLineTableForUnit(CU.get()); 134 if (!LT) 135 continue; 136 LineTables.push_back(LT); 137 138 // Loop over variable records and insert them to VariableLoc. 139 for (const auto &Entry : CU->dies()) { 140 DWARFDie Die(CU.get(), &Entry); 141 // Skip all tags that are not variables. 142 if (Die.getTag() != dwarf::DW_TAG_variable) 143 continue; 144 145 // Skip if a local variable because we don't need them for generating 146 // error messages. In general, only non-local symbols can fail to be 147 // linked. 148 if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0)) 149 continue; 150 151 // Get the source filename index for the variable. 152 unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0); 153 if (!LT->hasFileAtIndex(File)) 154 continue; 155 156 // Get the line number on which the variable is declared. 157 unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0); 158 159 // Get the name of the variable and add the collected information to 160 // VariableLoc. Usually Name is non-empty, but it can be empty if the 161 // input object file lacks some debug info. 162 StringRef Name = dwarf::toString(Die.find(dwarf::DW_AT_name), ""); 163 if (!Name.empty()) 164 VariableLoc.insert({Name, {LT, File, Line}}); 165 } 166 } 167 } 168 169 // Returns the pair of file name and line number describing location of data 170 // object (variable, array, etc) definition. 171 template <class ELFT> 172 Optional<std::pair<std::string, unsigned>> 173 ObjFile<ELFT>::getVariableLoc(StringRef Name) { 174 llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); }); 175 176 // Return if we have no debug information about data object. 177 auto It = VariableLoc.find(Name); 178 if (It == VariableLoc.end()) 179 return None; 180 181 // Take file name string from line table. 182 std::string FileName; 183 if (!It->second.LT->getFileNameByIndex( 184 It->second.File, nullptr, 185 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName)) 186 return None; 187 188 return std::make_pair(FileName, It->second.Line); 189 } 190 191 // Returns source line information for a given offset 192 // using DWARF debug info. 193 template <class ELFT> 194 Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *S, 195 uint64_t Offset) { 196 llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); }); 197 198 // Use fake address calcuated by adding section file offset and offset in 199 // section. See comments for ObjectInfo class. 200 DILineInfo Info; 201 for (const llvm::DWARFDebugLine::LineTable *LT : LineTables) 202 if (LT->getFileLineInfoForAddress( 203 S->getOffsetInFile() + Offset, nullptr, 204 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info)) 205 return Info; 206 return None; 207 } 208 209 // Returns source line information for a given offset using DWARF debug info. 210 template <class ELFT> 211 std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) { 212 if (Optional<DILineInfo> Info = getDILineInfo(S, Offset)) 213 return Info->FileName + ":" + std::to_string(Info->Line); 214 return ""; 215 } 216 217 // Returns "<internal>", "foo.a(bar.o)" or "baz.o". 218 std::string lld::toString(const InputFile *F) { 219 if (!F) 220 return "<internal>"; 221 222 if (F->ToStringCache.empty()) { 223 if (F->ArchiveName.empty()) 224 F->ToStringCache = F->getName(); 225 else 226 F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str(); 227 } 228 return F->ToStringCache; 229 } 230 231 template <class ELFT> 232 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) { 233 if (ELFT::TargetEndianness == support::little) 234 EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind; 235 else 236 EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind; 237 238 EMachine = getObj().getHeader()->e_machine; 239 OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI]; 240 } 241 242 template <class ELFT> 243 typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() { 244 return makeArrayRef(ELFSyms.begin() + FirstGlobal, ELFSyms.end()); 245 } 246 247 template <class ELFT> 248 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const { 249 return CHECK(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX), this); 250 } 251 252 template <class ELFT> 253 void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections, 254 const Elf_Shdr *Symtab) { 255 FirstGlobal = Symtab->sh_info; 256 ELFSyms = CHECK(getObj().symbols(Symtab), this); 257 if (FirstGlobal == 0 || FirstGlobal > ELFSyms.size()) 258 fatal(toString(this) + ": invalid sh_info in symbol table"); 259 260 StringTable = 261 CHECK(getObj().getStringTableForSymtab(*Symtab, Sections), this); 262 } 263 264 template <class ELFT> 265 ObjFile<ELFT>::ObjFile(MemoryBufferRef M, StringRef ArchiveName) 266 : ELFFileBase<ELFT>(Base::ObjKind, M) { 267 this->ArchiveName = ArchiveName; 268 } 269 270 template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() { 271 if (this->Symbols.empty()) 272 return {}; 273 return makeArrayRef(this->Symbols).slice(1, this->FirstGlobal - 1); 274 } 275 276 template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getGlobalSymbols() { 277 return makeArrayRef(this->Symbols).slice(this->FirstGlobal); 278 } 279 280 template <class ELFT> 281 void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) { 282 // Read a section table. JustSymbols is usually false. 283 if (this->JustSymbols) 284 initializeJustSymbols(); 285 else 286 initializeSections(ComdatGroups); 287 288 // Read a symbol table. 289 initializeSymbols(); 290 } 291 292 // Sections with SHT_GROUP and comdat bits define comdat section groups. 293 // They are identified and deduplicated by group name. This function 294 // returns a group name. 295 template <class ELFT> 296 StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections, 297 const Elf_Shdr &Sec) { 298 // Group signatures are stored as symbol names in object files. 299 // sh_info contains a symbol index, so we fetch a symbol and read its name. 300 if (this->ELFSyms.empty()) 301 this->initSymtab( 302 Sections, CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), this)); 303 304 const Elf_Sym *Sym = 305 CHECK(object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), this); 306 StringRef Signature = CHECK(Sym->getName(this->StringTable), this); 307 308 // As a special case, if a symbol is a section symbol and has no name, 309 // we use a section name as a signature. 310 // 311 // Such SHT_GROUP sections are invalid from the perspective of the ELF 312 // standard, but GNU gold 1.14 (the newest version as of July 2017) or 313 // older produce such sections as outputs for the -r option, so we need 314 // a bug-compatibility. 315 if (Signature.empty() && Sym->getType() == STT_SECTION) 316 return getSectionName(Sec); 317 return Signature; 318 } 319 320 template <class ELFT> 321 ArrayRef<typename ObjFile<ELFT>::Elf_Word> 322 ObjFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) { 323 const ELFFile<ELFT> &Obj = this->getObj(); 324 ArrayRef<Elf_Word> Entries = 325 CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this); 326 if (Entries.empty() || Entries[0] != GRP_COMDAT) 327 fatal(toString(this) + ": unsupported SHT_GROUP format"); 328 return Entries.slice(1); 329 } 330 331 template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) { 332 // On a regular link we don't merge sections if -O0 (default is -O1). This 333 // sometimes makes the linker significantly faster, although the output will 334 // be bigger. 335 // 336 // Doing the same for -r would create a problem as it would combine sections 337 // with different sh_entsize. One option would be to just copy every SHF_MERGE 338 // section as is to the output. While this would produce a valid ELF file with 339 // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when 340 // they see two .debug_str. We could have separate logic for combining 341 // SHF_MERGE sections based both on their name and sh_entsize, but that seems 342 // to be more trouble than it is worth. Instead, we just use the regular (-O1) 343 // logic for -r. 344 if (Config->Optimize == 0 && !Config->Relocatable) 345 return false; 346 347 // A mergeable section with size 0 is useless because they don't have 348 // any data to merge. A mergeable string section with size 0 can be 349 // argued as invalid because it doesn't end with a null character. 350 // We'll avoid a mess by handling them as if they were non-mergeable. 351 if (Sec.sh_size == 0) 352 return false; 353 354 // Check for sh_entsize. The ELF spec is not clear about the zero 355 // sh_entsize. It says that "the member [sh_entsize] contains 0 if 356 // the section does not hold a table of fixed-size entries". We know 357 // that Rust 1.13 produces a string mergeable section with a zero 358 // sh_entsize. Here we just accept it rather than being picky about it. 359 uint64_t EntSize = Sec.sh_entsize; 360 if (EntSize == 0) 361 return false; 362 if (Sec.sh_size % EntSize) 363 fatal(toString(this) + 364 ": SHF_MERGE section size must be a multiple of sh_entsize"); 365 366 uint64_t Flags = Sec.sh_flags; 367 if (!(Flags & SHF_MERGE)) 368 return false; 369 if (Flags & SHF_WRITE) 370 fatal(toString(this) + ": writable SHF_MERGE section is not supported"); 371 372 return true; 373 } 374 375 // This is for --just-symbols. 376 // 377 // --just-symbols is a very minor feature that allows you to link your 378 // output against other existing program, so that if you load both your 379 // program and the other program into memory, your output can refer the 380 // other program's symbols. 381 // 382 // When the option is given, we link "just symbols". The section table is 383 // initialized with null pointers. 384 template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() { 385 ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this); 386 this->Sections.resize(ObjSections.size()); 387 388 for (const Elf_Shdr &Sec : ObjSections) { 389 if (Sec.sh_type != SHT_SYMTAB) 390 continue; 391 this->initSymtab(ObjSections, &Sec); 392 return; 393 } 394 } 395 396 template <class ELFT> 397 void ObjFile<ELFT>::initializeSections( 398 DenseSet<CachedHashStringRef> &ComdatGroups) { 399 const ELFFile<ELFT> &Obj = this->getObj(); 400 401 ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this); 402 uint64_t Size = ObjSections.size(); 403 this->Sections.resize(Size); 404 this->SectionStringTable = 405 CHECK(Obj.getSectionStringTable(ObjSections), this); 406 407 for (size_t I = 0, E = ObjSections.size(); I < E; I++) { 408 if (this->Sections[I] == &InputSection::Discarded) 409 continue; 410 const Elf_Shdr &Sec = ObjSections[I]; 411 412 // SHF_EXCLUDE'ed sections are discarded by the linker. However, 413 // if -r is given, we'll let the final link discard such sections. 414 // This is compatible with GNU. 415 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) { 416 this->Sections[I] = &InputSection::Discarded; 417 continue; 418 } 419 420 switch (Sec.sh_type) { 421 case SHT_GROUP: { 422 // De-duplicate section groups by their signatures. 423 StringRef Signature = getShtGroupSignature(ObjSections, Sec); 424 bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second; 425 this->Sections[I] = &InputSection::Discarded; 426 427 // If it is a new section group, we want to keep group members. 428 // Group leader sections, which contain indices of group members, are 429 // discarded because they are useless beyond this point. The only 430 // exception is the -r option because in order to produce re-linkable 431 // object files, we want to pass through basically everything. 432 if (IsNew) { 433 if (Config->Relocatable) 434 this->Sections[I] = createInputSection(Sec); 435 continue; 436 } 437 438 // Otherwise, discard group members. 439 for (uint32_t SecIndex : getShtGroupEntries(Sec)) { 440 if (SecIndex >= Size) 441 fatal(toString(this) + 442 ": invalid section index in group: " + Twine(SecIndex)); 443 this->Sections[SecIndex] = &InputSection::Discarded; 444 } 445 break; 446 } 447 case SHT_SYMTAB: 448 this->initSymtab(ObjSections, &Sec); 449 break; 450 case SHT_SYMTAB_SHNDX: 451 this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this); 452 break; 453 case SHT_STRTAB: 454 case SHT_NULL: 455 break; 456 default: 457 this->Sections[I] = createInputSection(Sec); 458 } 459 460 // .ARM.exidx sections have a reverse dependency on the InputSection they 461 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link. 462 if (Sec.sh_flags & SHF_LINK_ORDER) { 463 if (Sec.sh_link >= this->Sections.size()) 464 fatal(toString(this) + 465 ": invalid sh_link index: " + Twine(Sec.sh_link)); 466 467 InputSectionBase *LinkSec = this->Sections[Sec.sh_link]; 468 InputSection *IS = cast<InputSection>(this->Sections[I]); 469 LinkSec->DependentSections.push_back(IS); 470 if (!isa<InputSection>(LinkSec)) 471 error("a section " + IS->Name + 472 " with SHF_LINK_ORDER should not refer a non-regular " 473 "section: " + 474 toString(LinkSec)); 475 } 476 } 477 } 478 479 // The ARM support in lld makes some use of instructions that are not available 480 // on all ARM architectures. Namely: 481 // - Use of BLX instruction for interworking between ARM and Thumb state. 482 // - Use of the extended Thumb branch encoding in relocation. 483 // - Use of the MOVT/MOVW instructions in Thumb Thunks. 484 // The ARM Attributes section contains information about the architecture chosen 485 // at compile time. We follow the convention that if at least one input object 486 // is compiled with an architecture that supports these features then lld is 487 // permitted to use them. 488 static void updateSupportedARMFeatures(const ARMAttributeParser &Attributes) { 489 if (!Attributes.hasAttribute(ARMBuildAttrs::CPU_arch)) 490 return; 491 auto Arch = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch); 492 switch (Arch) { 493 case ARMBuildAttrs::Pre_v4: 494 case ARMBuildAttrs::v4: 495 case ARMBuildAttrs::v4T: 496 // Architectures prior to v5 do not support BLX instruction 497 break; 498 case ARMBuildAttrs::v5T: 499 case ARMBuildAttrs::v5TE: 500 case ARMBuildAttrs::v5TEJ: 501 case ARMBuildAttrs::v6: 502 case ARMBuildAttrs::v6KZ: 503 case ARMBuildAttrs::v6K: 504 Config->ARMHasBlx = true; 505 // Architectures used in pre-Cortex processors do not support 506 // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception 507 // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do. 508 break; 509 default: 510 // All other Architectures have BLX and extended branch encoding 511 Config->ARMHasBlx = true; 512 Config->ARMJ1J2BranchEncoding = true; 513 if (Arch != ARMBuildAttrs::v6_M && Arch != ARMBuildAttrs::v6S_M) 514 // All Architectures used in Cortex processors with the exception 515 // of v6-M and v6S-M have the MOVT and MOVW instructions. 516 Config->ARMHasMovtMovw = true; 517 break; 518 } 519 } 520 521 template <class ELFT> 522 InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) { 523 uint32_t Idx = Sec.sh_info; 524 if (Idx >= this->Sections.size()) 525 fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx)); 526 InputSectionBase *Target = this->Sections[Idx]; 527 528 // Strictly speaking, a relocation section must be included in the 529 // group of the section it relocates. However, LLVM 3.3 and earlier 530 // would fail to do so, so we gracefully handle that case. 531 if (Target == &InputSection::Discarded) 532 return nullptr; 533 534 if (!Target) 535 fatal(toString(this) + ": unsupported relocation reference"); 536 return Target; 537 } 538 539 // Create a regular InputSection class that has the same contents 540 // as a given section. 541 static InputSection *toRegularSection(MergeInputSection *Sec) { 542 return make<InputSection>(Sec->File, Sec->Flags, Sec->Type, Sec->Alignment, 543 Sec->Data, Sec->Name); 544 } 545 546 template <class ELFT> 547 InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) { 548 StringRef Name = getSectionName(Sec); 549 550 switch (Sec.sh_type) { 551 case SHT_ARM_ATTRIBUTES: { 552 if (Config->EMachine != EM_ARM) 553 break; 554 ARMAttributeParser Attributes; 555 ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec)); 556 Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind); 557 updateSupportedARMFeatures(Attributes); 558 // FIXME: Retain the first attribute section we see. The eglibc ARM 559 // dynamic loaders require the presence of an attribute section for dlopen 560 // to work. In a full implementation we would merge all attribute sections. 561 if (InX::ARMAttributes == nullptr) { 562 InX::ARMAttributes = make<InputSection>(*this, Sec, Name); 563 return InX::ARMAttributes; 564 } 565 return &InputSection::Discarded; 566 } 567 case SHT_RELA: 568 case SHT_REL: { 569 // Find a relocation target section and associate this section with that. 570 // Target may have been discarded if it is in a different section group 571 // and the group is discarded, even though it's a violation of the 572 // spec. We handle that situation gracefully by discarding dangling 573 // relocation sections. 574 InputSectionBase *Target = getRelocTarget(Sec); 575 if (!Target) 576 return nullptr; 577 578 // This section contains relocation information. 579 // If -r is given, we do not interpret or apply relocation 580 // but just copy relocation sections to output. 581 if (Config->Relocatable) 582 return make<InputSection>(*this, Sec, Name); 583 584 if (Target->FirstRelocation) 585 fatal(toString(this) + 586 ": multiple relocation sections to one section are not supported"); 587 588 // ELF spec allows mergeable sections with relocations, but they are 589 // rare, and it is in practice hard to merge such sections by contents, 590 // because applying relocations at end of linking changes section 591 // contents. So, we simply handle such sections as non-mergeable ones. 592 // Degrading like this is acceptable because section merging is optional. 593 if (auto *MS = dyn_cast<MergeInputSection>(Target)) { 594 Target = toRegularSection(MS); 595 this->Sections[Sec.sh_info] = Target; 596 } 597 598 if (Sec.sh_type == SHT_RELA) { 599 ArrayRef<Elf_Rela> Rels = CHECK(this->getObj().relas(&Sec), this); 600 Target->FirstRelocation = Rels.begin(); 601 Target->NumRelocations = Rels.size(); 602 Target->AreRelocsRela = true; 603 } else { 604 ArrayRef<Elf_Rel> Rels = CHECK(this->getObj().rels(&Sec), this); 605 Target->FirstRelocation = Rels.begin(); 606 Target->NumRelocations = Rels.size(); 607 Target->AreRelocsRela = false; 608 } 609 assert(isUInt<31>(Target->NumRelocations)); 610 611 // Relocation sections processed by the linker are usually removed 612 // from the output, so returning `nullptr` for the normal case. 613 // However, if -emit-relocs is given, we need to leave them in the output. 614 // (Some post link analysis tools need this information.) 615 if (Config->EmitRelocs) { 616 InputSection *RelocSec = make<InputSection>(*this, Sec, Name); 617 // We will not emit relocation section if target was discarded. 618 Target->DependentSections.push_back(RelocSec); 619 return RelocSec; 620 } 621 return nullptr; 622 } 623 } 624 625 // The GNU linker uses .note.GNU-stack section as a marker indicating 626 // that the code in the object file does not expect that the stack is 627 // executable (in terms of NX bit). If all input files have the marker, 628 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to 629 // make the stack non-executable. Most object files have this section as 630 // of 2017. 631 // 632 // But making the stack non-executable is a norm today for security 633 // reasons. Failure to do so may result in a serious security issue. 634 // Therefore, we make LLD always add PT_GNU_STACK unless it is 635 // explicitly told to do otherwise (by -z execstack). Because the stack 636 // executable-ness is controlled solely by command line options, 637 // .note.GNU-stack sections are simply ignored. 638 if (Name == ".note.GNU-stack") 639 return &InputSection::Discarded; 640 641 // Split stacks is a feature to support a discontiguous stack. At least 642 // as of 2017, it seems that the feature is not being used widely. 643 // Only GNU gold supports that. We don't. For the details about that, 644 // see https://gcc.gnu.org/wiki/SplitStacks 645 if (Name == ".note.GNU-split-stack") { 646 error(toString(this) + 647 ": object file compiled with -fsplit-stack is not supported"); 648 return &InputSection::Discarded; 649 } 650 651 // The linkonce feature is a sort of proto-comdat. Some glibc i386 object 652 // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce 653 // sections. Drop those sections to avoid duplicate symbol errors. 654 // FIXME: This is glibc PR20543, we should remove this hack once that has been 655 // fixed for a while. 656 if (Name.startswith(".gnu.linkonce.")) 657 return &InputSection::Discarded; 658 659 // If we are creating a new .build-id section, strip existing .build-id 660 // sections so that the output won't have more than one .build-id. 661 // This is not usually a problem because input object files normally don't 662 // have .build-id sections, but you can create such files by 663 // "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it. 664 if (Name == ".note.gnu.build-id" && Config->BuildId != BuildIdKind::None) 665 return &InputSection::Discarded; 666 667 // The linker merges EH (exception handling) frames and creates a 668 // .eh_frame_hdr section for runtime. So we handle them with a special 669 // class. For relocatable outputs, they are just passed through. 670 if (Name == ".eh_frame" && !Config->Relocatable) 671 return make<EhInputSection>(*this, Sec, Name); 672 673 if (shouldMerge(Sec)) 674 return make<MergeInputSection>(*this, Sec, Name); 675 return make<InputSection>(*this, Sec, Name); 676 } 677 678 template <class ELFT> 679 StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) { 680 return CHECK(this->getObj().getSectionName(&Sec, SectionStringTable), this); 681 } 682 683 template <class ELFT> void ObjFile<ELFT>::initializeSymbols() { 684 this->Symbols.reserve(this->ELFSyms.size()); 685 for (const Elf_Sym &Sym : this->ELFSyms) 686 this->Symbols.push_back(createSymbol(&Sym)); 687 } 688 689 template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) { 690 int Binding = Sym->getBinding(); 691 692 uint32_t SecIdx = this->getSectionIndex(*Sym); 693 if (SecIdx >= this->Sections.size()) 694 fatal(toString(this) + ": invalid section index: " + Twine(SecIdx)); 695 696 InputSectionBase *Sec = this->Sections[SecIdx]; 697 uint8_t StOther = Sym->st_other; 698 uint8_t Type = Sym->getType(); 699 uint64_t Value = Sym->st_value; 700 uint64_t Size = Sym->st_size; 701 702 if (Binding == STB_LOCAL) { 703 if (Sym->getType() == STT_FILE) 704 SourceFile = CHECK(Sym->getName(this->StringTable), this); 705 706 if (this->StringTable.size() <= Sym->st_name) 707 fatal(toString(this) + ": invalid symbol name offset"); 708 709 StringRefZ Name = this->StringTable.data() + Sym->st_name; 710 if (Sym->st_shndx == SHN_UNDEF) 711 return make<Undefined>(this, Name, Binding, StOther, Type); 712 713 return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec); 714 } 715 716 StringRef Name = CHECK(Sym->getName(this->StringTable), this); 717 718 switch (Sym->st_shndx) { 719 case SHN_UNDEF: 720 return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type, 721 /*CanOmitFromDynSym=*/false, this); 722 case SHN_COMMON: 723 if (Value == 0 || Value >= UINT32_MAX) 724 fatal(toString(this) + ": common symbol '" + Name + 725 "' has invalid alignment: " + Twine(Value)); 726 return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, *this); 727 } 728 729 switch (Binding) { 730 default: 731 fatal(toString(this) + ": unexpected binding: " + Twine(Binding)); 732 case STB_GLOBAL: 733 case STB_WEAK: 734 case STB_GNU_UNIQUE: 735 if (Sec == &InputSection::Discarded) 736 return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type, 737 /*CanOmitFromDynSym=*/false, this); 738 return Symtab->addRegular(Name, StOther, Type, Value, Size, Binding, Sec, 739 this); 740 } 741 } 742 743 ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File) 744 : InputFile(ArchiveKind, File->getMemoryBufferRef()), 745 File(std::move(File)) {} 746 747 template <class ELFT> void ArchiveFile::parse() { 748 for (const Archive::Symbol &Sym : File->symbols()) 749 Symtab->addLazyArchive<ELFT>(Sym.getName(), *this, Sym); 750 } 751 752 // Returns a buffer pointing to a member file containing a given symbol. 753 InputFile *ArchiveFile::fetch(const Archive::Symbol &Sym) { 754 Archive::Child C = 755 CHECK(Sym.getMember(), toString(this) + 756 ": could not get the member for symbol " + 757 Sym.getName()); 758 759 if (!Seen.insert(C.getChildOffset()).second) 760 return nullptr; 761 762 MemoryBufferRef MB = 763 CHECK(C.getMemoryBufferRef(), 764 toString(this) + 765 ": could not get the buffer for the member defining symbol " + 766 Sym.getName()); 767 768 if (Tar && C.getParent()->isThin()) 769 Tar->append(relativeToRoot(CHECK(C.getFullName(), this)), MB.getBuffer()); 770 771 InputFile *File = createObjectFile( 772 MB, getName(), C.getParent()->isThin() ? 0 : C.getChildOffset()); 773 File->GroupId = GroupId; 774 return File; 775 } 776 777 template <class ELFT> 778 SharedFile<ELFT>::SharedFile(MemoryBufferRef M, StringRef DefaultSoName) 779 : ELFFileBase<ELFT>(Base::SharedKind, M), SoName(DefaultSoName), 780 IsNeeded(!Config->AsNeeded) {} 781 782 // Partially parse the shared object file so that we can call 783 // getSoName on this object. 784 template <class ELFT> void SharedFile<ELFT>::parseSoName() { 785 const Elf_Shdr *DynamicSec = nullptr; 786 const ELFFile<ELFT> Obj = this->getObj(); 787 ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this); 788 789 // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d. 790 for (const Elf_Shdr &Sec : Sections) { 791 switch (Sec.sh_type) { 792 default: 793 continue; 794 case SHT_DYNSYM: 795 this->initSymtab(Sections, &Sec); 796 break; 797 case SHT_DYNAMIC: 798 DynamicSec = &Sec; 799 break; 800 case SHT_SYMTAB_SHNDX: 801 this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, Sections), this); 802 break; 803 case SHT_GNU_versym: 804 this->VersymSec = &Sec; 805 break; 806 case SHT_GNU_verdef: 807 this->VerdefSec = &Sec; 808 break; 809 } 810 } 811 812 if (this->VersymSec && this->ELFSyms.empty()) 813 error("SHT_GNU_versym should be associated with symbol table"); 814 815 // Search for a DT_SONAME tag to initialize this->SoName. 816 if (!DynamicSec) 817 return; 818 ArrayRef<Elf_Dyn> Arr = 819 CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), this); 820 for (const Elf_Dyn &Dyn : Arr) { 821 if (Dyn.d_tag == DT_SONAME) { 822 uint64_t Val = Dyn.getVal(); 823 if (Val >= this->StringTable.size()) 824 fatal(toString(this) + ": invalid DT_SONAME entry"); 825 SoName = this->StringTable.data() + Val; 826 return; 827 } 828 } 829 } 830 831 // Parses ".gnu.version" section which is a parallel array for the symbol table. 832 // If a given file doesn't have ".gnu.version" section, returns VER_NDX_GLOBAL. 833 template <class ELFT> std::vector<uint32_t> SharedFile<ELFT>::parseVersyms() { 834 size_t Size = this->ELFSyms.size() - this->FirstGlobal; 835 if (!VersymSec) 836 return std::vector<uint32_t>(Size, VER_NDX_GLOBAL); 837 838 const char *Base = this->MB.getBuffer().data(); 839 const Elf_Versym *Versym = 840 reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) + 841 this->FirstGlobal; 842 843 std::vector<uint32_t> Ret(Size); 844 for (size_t I = 0; I < Size; ++I) 845 Ret[I] = Versym[I].vs_index; 846 return Ret; 847 } 848 849 // Parse the version definitions in the object file if present. Returns a vector 850 // whose nth element contains a pointer to the Elf_Verdef for version identifier 851 // n. Version identifiers that are not definitions map to nullptr. 852 template <class ELFT> 853 std::vector<const typename ELFT::Verdef *> SharedFile<ELFT>::parseVerdefs() { 854 if (!VerdefSec) 855 return {}; 856 857 // We cannot determine the largest verdef identifier without inspecting 858 // every Elf_Verdef, but both bfd and gold assign verdef identifiers 859 // sequentially starting from 1, so we predict that the largest identifier 860 // will be VerdefCount. 861 unsigned VerdefCount = VerdefSec->sh_info; 862 std::vector<const Elf_Verdef *> Verdefs(VerdefCount + 1); 863 864 // Build the Verdefs array by following the chain of Elf_Verdef objects 865 // from the start of the .gnu.version_d section. 866 const char *Base = this->MB.getBuffer().data(); 867 const char *Verdef = Base + VerdefSec->sh_offset; 868 for (unsigned I = 0; I != VerdefCount; ++I) { 869 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef); 870 Verdef += CurVerdef->vd_next; 871 unsigned VerdefIndex = CurVerdef->vd_ndx; 872 if (Verdefs.size() <= VerdefIndex) 873 Verdefs.resize(VerdefIndex + 1); 874 Verdefs[VerdefIndex] = CurVerdef; 875 } 876 877 return Verdefs; 878 } 879 880 // We do not usually care about alignments of data in shared object 881 // files because the loader takes care of it. However, if we promote a 882 // DSO symbol to point to .bss due to copy relocation, we need to keep 883 // the original alignment requirements. We infer it in this function. 884 template <class ELFT> 885 uint32_t SharedFile<ELFT>::getAlignment(ArrayRef<Elf_Shdr> Sections, 886 const Elf_Sym &Sym) { 887 uint64_t Ret = 1; 888 if (Sym.st_value) 889 Ret = 1ULL << countTrailingZeros((uint64_t)Sym.st_value); 890 if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size()) 891 Ret = std::min<uint64_t>(Ret, Sections[Sym.st_shndx].sh_addralign); 892 893 if (Ret > UINT32_MAX) 894 error(toString(this) + ": alignment too large: " + 895 CHECK(Sym.getName(this->StringTable), this)); 896 return Ret; 897 } 898 899 // Fully parse the shared object file. This must be called after parseSoName(). 900 // 901 // This function parses symbol versions. If a DSO has version information, 902 // the file has a ".gnu.version_d" section which contains symbol version 903 // definitions. Each symbol is associated to one version through a table in 904 // ".gnu.version" section. That table is a parallel array for the symbol 905 // table, and each table entry contains an index in ".gnu.version_d". 906 // 907 // The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for 908 // VER_NDX_GLOBAL. There's no table entry for these special versions in 909 // ".gnu.version_d". 910 // 911 // The file format for symbol versioning is perhaps a bit more complicated 912 // than necessary, but you can easily understand the code if you wrap your 913 // head around the data structure described above. 914 template <class ELFT> void SharedFile<ELFT>::parseRest() { 915 Verdefs = parseVerdefs(); // parse .gnu.version_d 916 std::vector<uint32_t> Versyms = parseVersyms(); // parse .gnu.version 917 ArrayRef<Elf_Shdr> Sections = CHECK(this->getObj().sections(), this); 918 919 // System libraries can have a lot of symbols with versions. Using a 920 // fixed buffer for computing the versions name (foo@ver) can save a 921 // lot of allocations. 922 SmallString<0> VersionedNameBuffer; 923 924 // Add symbols to the symbol table. 925 ArrayRef<Elf_Sym> Syms = this->getGlobalELFSyms(); 926 for (size_t I = 0; I < Syms.size(); ++I) { 927 const Elf_Sym &Sym = Syms[I]; 928 929 StringRef Name = CHECK(Sym.getName(this->StringTable), this); 930 if (Sym.isUndefined()) { 931 Symbol *S = Symtab->addUndefined<ELFT>(Name, Sym.getBinding(), 932 Sym.st_other, Sym.getType(), 933 /*CanOmitFromDynSym=*/false, this); 934 S->ExportDynamic = true; 935 continue; 936 } 937 938 // ELF spec requires that all local symbols precede weak or global 939 // symbols in each symbol table, and the index of first non-local symbol 940 // is stored to sh_info. If a local symbol appears after some non-local 941 // symbol, that's a violation of the spec. 942 if (Sym.getBinding() == STB_LOCAL) { 943 warn("found local symbol '" + Name + 944 "' in global part of symbol table in file " + toString(this)); 945 continue; 946 } 947 948 // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly 949 // assigns VER_NDX_LOCAL to this section global symbol. Here is a 950 // workaround for this bug. 951 uint32_t Idx = Versyms[I] & ~VERSYM_HIDDEN; 952 if (Config->EMachine == EM_MIPS && Idx == VER_NDX_LOCAL && 953 Name == "_gp_disp") 954 continue; 955 956 uint64_t Alignment = getAlignment(Sections, Sym); 957 if (!(Versyms[I] & VERSYM_HIDDEN)) 958 Symtab->addShared(Name, *this, Sym, Alignment, Idx); 959 960 // Also add the symbol with the versioned name to handle undefined symbols 961 // with explicit versions. 962 if (Idx == VER_NDX_GLOBAL) 963 continue; 964 965 if (Idx >= Verdefs.size() || Idx == VER_NDX_LOCAL) { 966 error("corrupt input file: version definition index " + Twine(Idx) + 967 " for symbol " + Name + " is out of bounds\n>>> defined in " + 968 toString(this)); 969 continue; 970 } 971 972 StringRef VerName = 973 this->StringTable.data() + Verdefs[Idx]->getAux()->vda_name; 974 VersionedNameBuffer.clear(); 975 Name = (Name + "@" + VerName).toStringRef(VersionedNameBuffer); 976 Symtab->addShared(Saver.save(Name), *this, Sym, Alignment, Idx); 977 } 978 } 979 980 static ELFKind getBitcodeELFKind(const Triple &T) { 981 if (T.isLittleEndian()) 982 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind; 983 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind; 984 } 985 986 static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) { 987 switch (T.getArch()) { 988 case Triple::aarch64: 989 return EM_AARCH64; 990 case Triple::arm: 991 case Triple::thumb: 992 return EM_ARM; 993 case Triple::avr: 994 return EM_AVR; 995 case Triple::mips: 996 case Triple::mipsel: 997 case Triple::mips64: 998 case Triple::mips64el: 999 return EM_MIPS; 1000 case Triple::ppc: 1001 return EM_PPC; 1002 case Triple::ppc64: 1003 return EM_PPC64; 1004 case Triple::x86: 1005 return T.isOSIAMCU() ? EM_IAMCU : EM_386; 1006 case Triple::x86_64: 1007 return EM_X86_64; 1008 default: 1009 fatal(Path + ": could not infer e_machine from bitcode target triple " + 1010 T.str()); 1011 } 1012 } 1013 1014 BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName, 1015 uint64_t OffsetInArchive) 1016 : InputFile(BitcodeKind, MB) { 1017 this->ArchiveName = ArchiveName; 1018 1019 // Here we pass a new MemoryBufferRef which is identified by ArchiveName 1020 // (the fully resolved path of the archive) + member name + offset of the 1021 // member in the archive. 1022 // ThinLTO uses the MemoryBufferRef identifier to access its internal 1023 // data structures and if two archives define two members with the same name, 1024 // this causes a collision which result in only one of the objects being 1025 // taken into consideration at LTO time (which very likely causes undefined 1026 // symbols later in the link stage). 1027 MemoryBufferRef MBRef(MB.getBuffer(), 1028 Saver.save(ArchiveName + MB.getBufferIdentifier() + 1029 utostr(OffsetInArchive))); 1030 Obj = CHECK(lto::InputFile::create(MBRef), this); 1031 1032 Triple T(Obj->getTargetTriple()); 1033 EKind = getBitcodeELFKind(T); 1034 EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T); 1035 } 1036 1037 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) { 1038 switch (GvVisibility) { 1039 case GlobalValue::DefaultVisibility: 1040 return STV_DEFAULT; 1041 case GlobalValue::HiddenVisibility: 1042 return STV_HIDDEN; 1043 case GlobalValue::ProtectedVisibility: 1044 return STV_PROTECTED; 1045 } 1046 llvm_unreachable("unknown visibility"); 1047 } 1048 1049 template <class ELFT> 1050 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats, 1051 const lto::InputFile::Symbol &ObjSym, 1052 BitcodeFile &F) { 1053 StringRef NameRef = Saver.save(ObjSym.getName()); 1054 uint32_t Binding = ObjSym.isWeak() ? STB_WEAK : STB_GLOBAL; 1055 1056 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE; 1057 uint8_t Visibility = mapVisibility(ObjSym.getVisibility()); 1058 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable(); 1059 1060 int C = ObjSym.getComdatIndex(); 1061 if (C != -1 && !KeptComdats[C]) 1062 return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type, 1063 CanOmitFromDynSym, &F); 1064 1065 if (ObjSym.isUndefined()) 1066 return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type, 1067 CanOmitFromDynSym, &F); 1068 1069 if (ObjSym.isCommon()) 1070 return Symtab->addCommon(NameRef, ObjSym.getCommonSize(), 1071 ObjSym.getCommonAlignment(), Binding, Visibility, 1072 STT_OBJECT, F); 1073 1074 return Symtab->addBitcode(NameRef, Binding, Visibility, Type, 1075 CanOmitFromDynSym, F); 1076 } 1077 1078 template <class ELFT> 1079 void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) { 1080 std::vector<bool> KeptComdats; 1081 for (StringRef S : Obj->getComdatTable()) 1082 KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second); 1083 1084 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) 1085 Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, *this)); 1086 } 1087 1088 static ELFKind getELFKind(MemoryBufferRef MB) { 1089 unsigned char Size; 1090 unsigned char Endian; 1091 std::tie(Size, Endian) = getElfArchType(MB.getBuffer()); 1092 1093 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB) 1094 fatal(MB.getBufferIdentifier() + ": invalid data encoding"); 1095 if (Size != ELFCLASS32 && Size != ELFCLASS64) 1096 fatal(MB.getBufferIdentifier() + ": invalid file class"); 1097 1098 size_t BufSize = MB.getBuffer().size(); 1099 if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) || 1100 (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr))) 1101 fatal(MB.getBufferIdentifier() + ": file is too short"); 1102 1103 if (Size == ELFCLASS32) 1104 return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind; 1105 return (Endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind; 1106 } 1107 1108 void BinaryFile::parse() { 1109 ArrayRef<uint8_t> Data = toArrayRef(MB.getBuffer()); 1110 auto *Section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 1111 8, Data, ".data"); 1112 Sections.push_back(Section); 1113 1114 // For each input file foo that is embedded to a result as a binary 1115 // blob, we define _binary_foo_{start,end,size} symbols, so that 1116 // user programs can access blobs by name. Non-alphanumeric 1117 // characters in a filename are replaced with underscore. 1118 std::string S = "_binary_" + MB.getBufferIdentifier().str(); 1119 for (size_t I = 0; I < S.size(); ++I) 1120 if (!isAlnum(S[I])) 1121 S[I] = '_'; 1122 1123 Symtab->addRegular(Saver.save(S + "_start"), STV_DEFAULT, STT_OBJECT, 0, 0, 1124 STB_GLOBAL, Section, nullptr); 1125 Symtab->addRegular(Saver.save(S + "_end"), STV_DEFAULT, STT_OBJECT, 1126 Data.size(), 0, STB_GLOBAL, Section, nullptr); 1127 Symtab->addRegular(Saver.save(S + "_size"), STV_DEFAULT, STT_OBJECT, 1128 Data.size(), 0, STB_GLOBAL, nullptr, nullptr); 1129 } 1130 1131 static bool isBitcode(MemoryBufferRef MB) { 1132 using namespace sys::fs; 1133 return identify_magic(MB.getBuffer()) == file_magic::bitcode; 1134 } 1135 1136 InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName, 1137 uint64_t OffsetInArchive) { 1138 if (isBitcode(MB)) 1139 return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive); 1140 1141 switch (getELFKind(MB)) { 1142 case ELF32LEKind: 1143 return make<ObjFile<ELF32LE>>(MB, ArchiveName); 1144 case ELF32BEKind: 1145 return make<ObjFile<ELF32BE>>(MB, ArchiveName); 1146 case ELF64LEKind: 1147 return make<ObjFile<ELF64LE>>(MB, ArchiveName); 1148 case ELF64BEKind: 1149 return make<ObjFile<ELF64BE>>(MB, ArchiveName); 1150 default: 1151 llvm_unreachable("getELFKind"); 1152 } 1153 } 1154 1155 InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) { 1156 switch (getELFKind(MB)) { 1157 case ELF32LEKind: 1158 return make<SharedFile<ELF32LE>>(MB, DefaultSoName); 1159 case ELF32BEKind: 1160 return make<SharedFile<ELF32BE>>(MB, DefaultSoName); 1161 case ELF64LEKind: 1162 return make<SharedFile<ELF64LE>>(MB, DefaultSoName); 1163 case ELF64BEKind: 1164 return make<SharedFile<ELF64BE>>(MB, DefaultSoName); 1165 default: 1166 llvm_unreachable("getELFKind"); 1167 } 1168 } 1169 1170 MemoryBufferRef LazyObjFile::getBuffer() { 1171 if (Seen) 1172 return MemoryBufferRef(); 1173 Seen = true; 1174 return MB; 1175 } 1176 1177 InputFile *LazyObjFile::fetch() { 1178 MemoryBufferRef MBRef = getBuffer(); 1179 if (MBRef.getBuffer().empty()) 1180 return nullptr; 1181 1182 InputFile *File = createObjectFile(MBRef, ArchiveName, OffsetInArchive); 1183 File->GroupId = GroupId; 1184 return File; 1185 } 1186 1187 template <class ELFT> void LazyObjFile::parse() { 1188 // A lazy object file wraps either a bitcode file or an ELF file. 1189 if (isBitcode(this->MB)) { 1190 std::unique_ptr<lto::InputFile> Obj = 1191 CHECK(lto::InputFile::create(this->MB), this); 1192 for (const lto::InputFile::Symbol &Sym : Obj->symbols()) 1193 if (!Sym.isUndefined()) 1194 Symtab->addLazyObject<ELFT>(Saver.save(Sym.getName()), *this); 1195 return; 1196 } 1197 1198 switch (getELFKind(this->MB)) { 1199 case ELF32LEKind: 1200 addElfSymbols<ELF32LE>(); 1201 return; 1202 case ELF32BEKind: 1203 addElfSymbols<ELF32BE>(); 1204 return; 1205 case ELF64LEKind: 1206 addElfSymbols<ELF64LE>(); 1207 return; 1208 case ELF64BEKind: 1209 addElfSymbols<ELF64BE>(); 1210 return; 1211 default: 1212 llvm_unreachable("getELFKind"); 1213 } 1214 } 1215 1216 template <class ELFT> void LazyObjFile::addElfSymbols() { 1217 ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(MB.getBuffer())); 1218 ArrayRef<typename ELFT::Shdr> Sections = CHECK(Obj.sections(), this); 1219 1220 for (const typename ELFT::Shdr &Sec : Sections) { 1221 if (Sec.sh_type != SHT_SYMTAB) 1222 continue; 1223 1224 typename ELFT::SymRange Syms = CHECK(Obj.symbols(&Sec), this); 1225 uint32_t FirstGlobal = Sec.sh_info; 1226 StringRef StringTable = 1227 CHECK(Obj.getStringTableForSymtab(Sec, Sections), this); 1228 1229 for (const typename ELFT::Sym &Sym : Syms.slice(FirstGlobal)) 1230 if (Sym.st_shndx != SHN_UNDEF) 1231 Symtab->addLazyObject<ELFT>(CHECK(Sym.getName(StringTable), this), 1232 *this); 1233 return; 1234 } 1235 } 1236 1237 template void ArchiveFile::parse<ELF32LE>(); 1238 template void ArchiveFile::parse<ELF32BE>(); 1239 template void ArchiveFile::parse<ELF64LE>(); 1240 template void ArchiveFile::parse<ELF64BE>(); 1241 1242 template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &); 1243 template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &); 1244 template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &); 1245 template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &); 1246 1247 template void LazyObjFile::parse<ELF32LE>(); 1248 template void LazyObjFile::parse<ELF32BE>(); 1249 template void LazyObjFile::parse<ELF64LE>(); 1250 template void LazyObjFile::parse<ELF64BE>(); 1251 1252 template class elf::ELFFileBase<ELF32LE>; 1253 template class elf::ELFFileBase<ELF32BE>; 1254 template class elf::ELFFileBase<ELF64LE>; 1255 template class elf::ELFFileBase<ELF64BE>; 1256 1257 template class elf::ObjFile<ELF32LE>; 1258 template class elf::ObjFile<ELF32BE>; 1259 template class elf::ObjFile<ELF64LE>; 1260 template class elf::ObjFile<ELF64BE>; 1261 1262 template class elf::SharedFile<ELF32LE>; 1263 template class elf::SharedFile<ELF32BE>; 1264 template class elf::SharedFile<ELF64LE>; 1265 template class elf::SharedFile<ELF64BE>; 1266