1 //===- yaml2elf - Convert YAML to a ELF object file -----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// The ELF component of yaml2obj. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/ArrayRef.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/StringSet.h" 17 #include "llvm/BinaryFormat/ELF.h" 18 #include "llvm/MC/StringTableBuilder.h" 19 #include "llvm/Object/ELFObjectFile.h" 20 #include "llvm/ObjectYAML/ELFYAML.h" 21 #include "llvm/ObjectYAML/yaml2obj.h" 22 #include "llvm/Support/EndianStream.h" 23 #include "llvm/Support/LEB128.h" 24 #include "llvm/Support/MemoryBuffer.h" 25 #include "llvm/Support/WithColor.h" 26 #include "llvm/Support/YAMLTraits.h" 27 #include "llvm/Support/raw_ostream.h" 28 29 using namespace llvm; 30 31 // This class is used to build up a contiguous binary blob while keeping 32 // track of an offset in the output (which notionally begins at 33 // `InitialOffset`). 34 namespace { 35 class ContiguousBlobAccumulator { 36 const uint64_t InitialOffset; 37 SmallVector<char, 128> Buf; 38 raw_svector_ostream OS; 39 40 public: 41 ContiguousBlobAccumulator(uint64_t InitialOffset_) 42 : InitialOffset(InitialOffset_), Buf(), OS(Buf) {} 43 44 uint64_t getOffset() const { return InitialOffset + OS.tell(); } 45 raw_ostream &getOS() { return OS; } 46 47 /// \returns The new offset. 48 uint64_t padToAlignment(unsigned Align) { 49 if (Align == 0) 50 Align = 1; 51 uint64_t CurrentOffset = getOffset(); 52 uint64_t AlignedOffset = alignTo(CurrentOffset, Align); 53 OS.write_zeros(AlignedOffset - CurrentOffset); 54 return AlignedOffset; // == CurrentOffset; 55 } 56 57 void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); } 58 }; 59 60 // Used to keep track of section and symbol names, so that in the YAML file 61 // sections and symbols can be referenced by name instead of by index. 62 class NameToIdxMap { 63 StringMap<unsigned> Map; 64 65 public: 66 /// \Returns false if name is already present in the map. 67 bool addName(StringRef Name, unsigned Ndx) { 68 return Map.insert({Name, Ndx}).second; 69 } 70 /// \Returns false if name is not present in the map. 71 bool lookup(StringRef Name, unsigned &Idx) const { 72 auto I = Map.find(Name); 73 if (I == Map.end()) 74 return false; 75 Idx = I->getValue(); 76 return true; 77 } 78 /// Asserts if name is not present in the map. 79 unsigned get(StringRef Name) const { 80 unsigned Idx; 81 if (lookup(Name, Idx)) 82 return Idx; 83 assert(false && "Expected section not found in index"); 84 return 0; 85 } 86 unsigned size() const { return Map.size(); } 87 }; 88 89 namespace { 90 struct Fragment { 91 uint64_t Offset; 92 uint64_t Size; 93 uint32_t Type; 94 uint64_t AddrAlign; 95 }; 96 } // namespace 97 98 /// "Single point of truth" for the ELF file construction. 99 /// TODO: This class still has a ways to go before it is truly a "single 100 /// point of truth". 101 template <class ELFT> class ELFState { 102 typedef typename ELFT::Ehdr Elf_Ehdr; 103 typedef typename ELFT::Phdr Elf_Phdr; 104 typedef typename ELFT::Shdr Elf_Shdr; 105 typedef typename ELFT::Sym Elf_Sym; 106 typedef typename ELFT::Rel Elf_Rel; 107 typedef typename ELFT::Rela Elf_Rela; 108 typedef typename ELFT::Relr Elf_Relr; 109 typedef typename ELFT::Dyn Elf_Dyn; 110 typedef typename ELFT::uint uintX_t; 111 112 enum class SymtabType { Static, Dynamic }; 113 114 /// The future ".strtab" section. 115 StringTableBuilder DotStrtab{StringTableBuilder::ELF}; 116 117 /// The future ".shstrtab" section. 118 StringTableBuilder DotShStrtab{StringTableBuilder::ELF}; 119 120 /// The future ".dynstr" section. 121 StringTableBuilder DotDynstr{StringTableBuilder::ELF}; 122 123 NameToIdxMap SN2I; 124 NameToIdxMap SymN2I; 125 NameToIdxMap DynSymN2I; 126 ELFYAML::Object &Doc; 127 128 uint64_t LocationCounter = 0; 129 bool HasError = false; 130 yaml::ErrorHandler ErrHandler; 131 void reportError(const Twine &Msg); 132 133 std::vector<Elf_Sym> toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols, 134 const StringTableBuilder &Strtab); 135 unsigned toSectionIndex(StringRef S, StringRef LocSec, StringRef LocSym = ""); 136 unsigned toSymbolIndex(StringRef S, StringRef LocSec, bool IsDynamic); 137 138 void buildSectionIndex(); 139 void buildSymbolIndexes(); 140 void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders); 141 bool initImplicitHeader(ContiguousBlobAccumulator &CBA, Elf_Shdr &Header, 142 StringRef SecName, ELFYAML::Section *YAMLSec); 143 void initSectionHeaders(std::vector<Elf_Shdr> &SHeaders, 144 ContiguousBlobAccumulator &CBA); 145 void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType, 146 ContiguousBlobAccumulator &CBA, 147 ELFYAML::Section *YAMLSec); 148 void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, 149 StringTableBuilder &STB, 150 ContiguousBlobAccumulator &CBA, 151 ELFYAML::Section *YAMLSec); 152 void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders, 153 std::vector<Elf_Shdr> &SHeaders); 154 155 std::vector<Fragment> 156 getPhdrFragments(const ELFYAML::ProgramHeader &Phdr, 157 ArrayRef<typename ELFT::Shdr> SHeaders); 158 159 void finalizeStrings(); 160 void writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS); 161 void writeSectionContent(Elf_Shdr &SHeader, 162 const ELFYAML::RawContentSection &Section, 163 ContiguousBlobAccumulator &CBA); 164 void writeSectionContent(Elf_Shdr &SHeader, 165 const ELFYAML::RelocationSection &Section, 166 ContiguousBlobAccumulator &CBA); 167 void writeSectionContent(Elf_Shdr &SHeader, 168 const ELFYAML::RelrSection &Section, 169 ContiguousBlobAccumulator &CBA); 170 void writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group, 171 ContiguousBlobAccumulator &CBA); 172 void writeSectionContent(Elf_Shdr &SHeader, 173 const ELFYAML::SymtabShndxSection &Shndx, 174 ContiguousBlobAccumulator &CBA); 175 void writeSectionContent(Elf_Shdr &SHeader, 176 const ELFYAML::SymverSection &Section, 177 ContiguousBlobAccumulator &CBA); 178 void writeSectionContent(Elf_Shdr &SHeader, 179 const ELFYAML::VerneedSection &Section, 180 ContiguousBlobAccumulator &CBA); 181 void writeSectionContent(Elf_Shdr &SHeader, 182 const ELFYAML::VerdefSection &Section, 183 ContiguousBlobAccumulator &CBA); 184 void writeSectionContent(Elf_Shdr &SHeader, 185 const ELFYAML::MipsABIFlags &Section, 186 ContiguousBlobAccumulator &CBA); 187 void writeSectionContent(Elf_Shdr &SHeader, 188 const ELFYAML::DynamicSection &Section, 189 ContiguousBlobAccumulator &CBA); 190 void writeSectionContent(Elf_Shdr &SHeader, 191 const ELFYAML::StackSizesSection &Section, 192 ContiguousBlobAccumulator &CBA); 193 void writeSectionContent(Elf_Shdr &SHeader, 194 const ELFYAML::HashSection &Section, 195 ContiguousBlobAccumulator &CBA); 196 void writeSectionContent(Elf_Shdr &SHeader, 197 const ELFYAML::AddrsigSection &Section, 198 ContiguousBlobAccumulator &CBA); 199 void writeSectionContent(Elf_Shdr &SHeader, 200 const ELFYAML::NoteSection &Section, 201 ContiguousBlobAccumulator &CBA); 202 void writeSectionContent(Elf_Shdr &SHeader, 203 const ELFYAML::GnuHashSection &Section, 204 ContiguousBlobAccumulator &CBA); 205 void writeSectionContent(Elf_Shdr &SHeader, 206 const ELFYAML::LinkerOptionsSection &Section, 207 ContiguousBlobAccumulator &CBA); 208 void writeSectionContent(Elf_Shdr &SHeader, 209 const ELFYAML::DependentLibrariesSection &Section, 210 ContiguousBlobAccumulator &CBA); 211 void writeSectionContent(Elf_Shdr &SHeader, 212 const ELFYAML::CallGraphProfileSection &Section, 213 ContiguousBlobAccumulator &CBA); 214 215 void writeFill(ELFYAML::Fill &Fill, ContiguousBlobAccumulator &CBA); 216 217 ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH); 218 219 void assignSectionAddress(Elf_Shdr &SHeader, ELFYAML::Section *YAMLSec); 220 221 DenseMap<StringRef, size_t> buildSectionHeaderReorderMap(); 222 223 BumpPtrAllocator StringAlloc; 224 uint64_t alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align, 225 llvm::Optional<llvm::yaml::Hex64> Offset); 226 227 public: 228 static bool writeELF(raw_ostream &OS, ELFYAML::Object &Doc, 229 yaml::ErrorHandler EH); 230 }; 231 } // end anonymous namespace 232 233 template <class T> static size_t arrayDataSize(ArrayRef<T> A) { 234 return A.size() * sizeof(T); 235 } 236 237 template <class T> static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) { 238 OS.write((const char *)A.data(), arrayDataSize(A)); 239 } 240 241 template <class T> static void zero(T &Obj) { memset(&Obj, 0, sizeof(Obj)); } 242 243 template <class ELFT> 244 ELFState<ELFT>::ELFState(ELFYAML::Object &D, yaml::ErrorHandler EH) 245 : Doc(D), ErrHandler(EH) { 246 std::vector<ELFYAML::Section *> Sections = Doc.getSections(); 247 // Insert SHT_NULL section implicitly when it is not defined in YAML. 248 if (Sections.empty() || Sections.front()->Type != ELF::SHT_NULL) 249 Doc.Chunks.insert( 250 Doc.Chunks.begin(), 251 std::make_unique<ELFYAML::Section>( 252 ELFYAML::Chunk::ChunkKind::RawContent, /*IsImplicit=*/true)); 253 254 // We add a technical suffix for each unnamed section/fill. It does not affect 255 // the output, but allows us to map them by name in the code and report better 256 // error messages. 257 StringSet<> DocSections; 258 for (size_t I = 0; I < Doc.Chunks.size(); ++I) { 259 const std::unique_ptr<ELFYAML::Chunk> &C = Doc.Chunks[I]; 260 if (C->Name.empty()) { 261 std::string NewName = ELFYAML::appendUniqueSuffix( 262 /*Name=*/"", "index " + Twine(I)); 263 C->Name = StringRef(NewName).copy(StringAlloc); 264 assert(ELFYAML::dropUniqueSuffix(C->Name).empty()); 265 } 266 267 if (!DocSections.insert(C->Name).second) 268 reportError("repeated section/fill name: '" + C->Name + 269 "' at YAML section/fill number " + Twine(I)); 270 } 271 272 std::vector<StringRef> ImplicitSections; 273 if (Doc.DynamicSymbols) 274 ImplicitSections.insert(ImplicitSections.end(), {".dynsym", ".dynstr"}); 275 if (Doc.Symbols) 276 ImplicitSections.push_back(".symtab"); 277 ImplicitSections.insert(ImplicitSections.end(), {".strtab", ".shstrtab"}); 278 279 // Insert placeholders for implicit sections that are not 280 // defined explicitly in YAML. 281 for (StringRef SecName : ImplicitSections) { 282 if (DocSections.count(SecName)) 283 continue; 284 285 std::unique_ptr<ELFYAML::Chunk> Sec = std::make_unique<ELFYAML::Section>( 286 ELFYAML::Chunk::ChunkKind::RawContent, true /*IsImplicit*/); 287 Sec->Name = SecName; 288 Doc.Chunks.push_back(std::move(Sec)); 289 } 290 } 291 292 template <class ELFT> 293 void ELFState<ELFT>::writeELFHeader(ContiguousBlobAccumulator &CBA, raw_ostream &OS) { 294 using namespace llvm::ELF; 295 296 Elf_Ehdr Header; 297 zero(Header); 298 Header.e_ident[EI_MAG0] = 0x7f; 299 Header.e_ident[EI_MAG1] = 'E'; 300 Header.e_ident[EI_MAG2] = 'L'; 301 Header.e_ident[EI_MAG3] = 'F'; 302 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; 303 Header.e_ident[EI_DATA] = Doc.Header.Data; 304 Header.e_ident[EI_VERSION] = EV_CURRENT; 305 Header.e_ident[EI_OSABI] = Doc.Header.OSABI; 306 Header.e_ident[EI_ABIVERSION] = Doc.Header.ABIVersion; 307 Header.e_type = Doc.Header.Type; 308 Header.e_machine = Doc.Header.Machine; 309 Header.e_version = EV_CURRENT; 310 Header.e_entry = Doc.Header.Entry; 311 Header.e_phoff = Doc.ProgramHeaders.size() ? sizeof(Header) : 0; 312 Header.e_flags = Doc.Header.Flags; 313 Header.e_ehsize = sizeof(Elf_Ehdr); 314 Header.e_phentsize = Doc.ProgramHeaders.size() ? sizeof(Elf_Phdr) : 0; 315 Header.e_phnum = Doc.ProgramHeaders.size(); 316 317 Header.e_shentsize = 318 Doc.Header.SHEntSize ? (uint16_t)*Doc.Header.SHEntSize : sizeof(Elf_Shdr); 319 // Align the start of the section header table, which is written after all 320 // other sections to the end of the file. 321 uint64_t SHOff = 322 alignToOffset(CBA, sizeof(typename ELFT::uint), /*Offset=*/None); 323 324 if (Doc.Header.SHOff) 325 Header.e_shoff = *Doc.Header.SHOff; 326 else if (Doc.SectionHeaders && Doc.SectionHeaders->Sections.empty()) 327 Header.e_shoff = 0; 328 else 329 Header.e_shoff = SHOff; 330 331 if (Doc.Header.SHNum) 332 Header.e_shnum = *Doc.Header.SHNum; 333 else if (!Doc.SectionHeaders) 334 Header.e_shnum = Doc.getSections().size(); 335 else if (Doc.SectionHeaders->Sections.empty()) 336 Header.e_shnum = 0; 337 else 338 Header.e_shnum = Doc.SectionHeaders->Sections.size() + /*Null section*/ 1; 339 340 if (Doc.Header.SHStrNdx) 341 Header.e_shstrndx = *Doc.Header.SHStrNdx; 342 else if (!Doc.SectionHeaders || !Doc.SectionHeaders->Sections.empty()) 343 Header.e_shstrndx = SN2I.get(".shstrtab"); 344 else 345 Header.e_shstrndx = 0; 346 347 OS.write((const char *)&Header, sizeof(Header)); 348 } 349 350 template <class ELFT> 351 void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) { 352 DenseMap<StringRef, ELFYAML::Fill *> NameToFill; 353 for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks) 354 if (auto S = dyn_cast<ELFYAML::Fill>(D.get())) 355 NameToFill[S->Name] = S; 356 357 std::vector<ELFYAML::Section *> Sections = Doc.getSections(); 358 for (ELFYAML::ProgramHeader &YamlPhdr : Doc.ProgramHeaders) { 359 Elf_Phdr Phdr; 360 zero(Phdr); 361 Phdr.p_type = YamlPhdr.Type; 362 Phdr.p_flags = YamlPhdr.Flags; 363 Phdr.p_vaddr = YamlPhdr.VAddr; 364 Phdr.p_paddr = YamlPhdr.PAddr; 365 PHeaders.push_back(Phdr); 366 367 // Map Sections list to corresponding chunks. 368 for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) { 369 if (ELFYAML::Fill *Fill = NameToFill.lookup(SecName.Section)) { 370 YamlPhdr.Chunks.push_back(Fill); 371 continue; 372 } 373 374 unsigned Index; 375 if (SN2I.lookup(SecName.Section, Index)) { 376 YamlPhdr.Chunks.push_back(Sections[Index]); 377 continue; 378 } 379 380 reportError("unknown section or fill referenced: '" + SecName.Section + 381 "' by program header"); 382 } 383 } 384 } 385 386 template <class ELFT> 387 unsigned ELFState<ELFT>::toSectionIndex(StringRef S, StringRef LocSec, 388 StringRef LocSym) { 389 unsigned Index; 390 if (SN2I.lookup(S, Index) || to_integer(S, Index)) 391 return Index; 392 393 assert(LocSec.empty() || LocSym.empty()); 394 if (!LocSym.empty()) 395 reportError("unknown section referenced: '" + S + "' by YAML symbol '" + 396 LocSym + "'"); 397 else 398 reportError("unknown section referenced: '" + S + "' by YAML section '" + 399 LocSec + "'"); 400 return 0; 401 } 402 403 template <class ELFT> 404 unsigned ELFState<ELFT>::toSymbolIndex(StringRef S, StringRef LocSec, 405 bool IsDynamic) { 406 const NameToIdxMap &SymMap = IsDynamic ? DynSymN2I : SymN2I; 407 unsigned Index; 408 // Here we try to look up S in the symbol table. If it is not there, 409 // treat its value as a symbol index. 410 if (!SymMap.lookup(S, Index) && !to_integer(S, Index)) { 411 reportError("unknown symbol referenced: '" + S + "' by YAML section '" + 412 LocSec + "'"); 413 return 0; 414 } 415 return Index; 416 } 417 418 template <class ELFT> 419 static void overrideFields(ELFYAML::Section *From, typename ELFT::Shdr &To) { 420 if (!From) 421 return; 422 if (From->ShFlags) 423 To.sh_flags = *From->ShFlags; 424 if (From->ShName) 425 To.sh_name = *From->ShName; 426 if (From->ShOffset) 427 To.sh_offset = *From->ShOffset; 428 if (From->ShSize) 429 To.sh_size = *From->ShSize; 430 } 431 432 template <class ELFT> 433 bool ELFState<ELFT>::initImplicitHeader(ContiguousBlobAccumulator &CBA, 434 Elf_Shdr &Header, StringRef SecName, 435 ELFYAML::Section *YAMLSec) { 436 // Check if the header was already initialized. 437 if (Header.sh_offset) 438 return false; 439 440 if (SecName == ".symtab") 441 initSymtabSectionHeader(Header, SymtabType::Static, CBA, YAMLSec); 442 else if (SecName == ".strtab") 443 initStrtabSectionHeader(Header, SecName, DotStrtab, CBA, YAMLSec); 444 else if (SecName == ".shstrtab") 445 initStrtabSectionHeader(Header, SecName, DotShStrtab, CBA, YAMLSec); 446 else if (SecName == ".dynsym") 447 initSymtabSectionHeader(Header, SymtabType::Dynamic, CBA, YAMLSec); 448 else if (SecName == ".dynstr") 449 initStrtabSectionHeader(Header, SecName, DotDynstr, CBA, YAMLSec); 450 else 451 return false; 452 453 LocationCounter += Header.sh_size; 454 455 // Override section fields if requested. 456 overrideFields<ELFT>(YAMLSec, Header); 457 return true; 458 } 459 460 constexpr char SuffixStart = '('; 461 constexpr char SuffixEnd = ')'; 462 463 std::string llvm::ELFYAML::appendUniqueSuffix(StringRef Name, 464 const Twine &Msg) { 465 // Do not add a space when a Name is empty. 466 std::string Ret = Name.empty() ? "" : Name.str() + ' '; 467 return Ret + (Twine(SuffixStart) + Msg + Twine(SuffixEnd)).str(); 468 } 469 470 StringRef llvm::ELFYAML::dropUniqueSuffix(StringRef S) { 471 if (S.empty() || S.back() != SuffixEnd) 472 return S; 473 474 // A special case for empty names. See appendUniqueSuffix() above. 475 size_t SuffixPos = S.rfind(SuffixStart); 476 if (SuffixPos == 0) 477 return ""; 478 479 if (SuffixPos == StringRef::npos || S[SuffixPos - 1] != ' ') 480 return S; 481 return S.substr(0, SuffixPos - 1); 482 } 483 484 template <class ELFT> 485 void ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders, 486 ContiguousBlobAccumulator &CBA) { 487 // Ensure SHN_UNDEF entry is present. An all-zero section header is a 488 // valid SHN_UNDEF entry since SHT_NULL == 0. 489 SHeaders.resize(Doc.getSections().size()); 490 491 for (const std::unique_ptr<ELFYAML::Chunk> &D : Doc.Chunks) { 492 if (ELFYAML::Fill *S = dyn_cast<ELFYAML::Fill>(D.get())) { 493 S->Offset = alignToOffset(CBA, /*Align=*/1, S->Offset); 494 writeFill(*S, CBA); 495 LocationCounter += S->Size; 496 continue; 497 } 498 499 ELFYAML::Section *Sec = cast<ELFYAML::Section>(D.get()); 500 bool IsFirstUndefSection = D == Doc.Chunks.front(); 501 if (IsFirstUndefSection && Sec->IsImplicit) 502 continue; 503 504 // We have a few sections like string or symbol tables that are usually 505 // added implicitly to the end. However, if they are explicitly specified 506 // in the YAML, we need to write them here. This ensures the file offset 507 // remains correct. 508 Elf_Shdr &SHeader = SHeaders[SN2I.get(Sec->Name)]; 509 if (initImplicitHeader(CBA, SHeader, Sec->Name, 510 Sec->IsImplicit ? nullptr : Sec)) 511 continue; 512 513 assert(Sec && "It can't be null unless it is an implicit section. But all " 514 "implicit sections should already have been handled above."); 515 516 SHeader.sh_name = 517 DotShStrtab.getOffset(ELFYAML::dropUniqueSuffix(Sec->Name)); 518 SHeader.sh_type = Sec->Type; 519 if (Sec->Flags) 520 SHeader.sh_flags = *Sec->Flags; 521 SHeader.sh_addralign = Sec->AddressAlign; 522 523 // Set the offset for all sections, except the SHN_UNDEF section with index 524 // 0 when not explicitly requested. 525 if (!IsFirstUndefSection || Sec->Offset) 526 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, Sec->Offset); 527 528 assignSectionAddress(SHeader, Sec); 529 530 if (!Sec->Link.empty()) 531 SHeader.sh_link = toSectionIndex(Sec->Link, Sec->Name); 532 533 if (IsFirstUndefSection) { 534 if (auto RawSec = dyn_cast<ELFYAML::RawContentSection>(Sec)) { 535 // We do not write any content for special SHN_UNDEF section. 536 if (RawSec->Size) 537 SHeader.sh_size = *RawSec->Size; 538 if (RawSec->Info) 539 SHeader.sh_info = *RawSec->Info; 540 } 541 if (Sec->EntSize) 542 SHeader.sh_entsize = *Sec->EntSize; 543 } else if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec)) { 544 writeSectionContent(SHeader, *S, CBA); 545 } else if (auto S = dyn_cast<ELFYAML::SymtabShndxSection>(Sec)) { 546 writeSectionContent(SHeader, *S, CBA); 547 } else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec)) { 548 writeSectionContent(SHeader, *S, CBA); 549 } else if (auto S = dyn_cast<ELFYAML::RelrSection>(Sec)) { 550 writeSectionContent(SHeader, *S, CBA); 551 } else if (auto S = dyn_cast<ELFYAML::Group>(Sec)) { 552 writeSectionContent(SHeader, *S, CBA); 553 } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec)) { 554 writeSectionContent(SHeader, *S, CBA); 555 } else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec)) { 556 // SHT_NOBITS sections do not have any content to write. 557 SHeader.sh_entsize = 0; 558 SHeader.sh_size = S->Size; 559 } else if (auto S = dyn_cast<ELFYAML::DynamicSection>(Sec)) { 560 writeSectionContent(SHeader, *S, CBA); 561 } else if (auto S = dyn_cast<ELFYAML::SymverSection>(Sec)) { 562 writeSectionContent(SHeader, *S, CBA); 563 } else if (auto S = dyn_cast<ELFYAML::VerneedSection>(Sec)) { 564 writeSectionContent(SHeader, *S, CBA); 565 } else if (auto S = dyn_cast<ELFYAML::VerdefSection>(Sec)) { 566 writeSectionContent(SHeader, *S, CBA); 567 } else if (auto S = dyn_cast<ELFYAML::StackSizesSection>(Sec)) { 568 writeSectionContent(SHeader, *S, CBA); 569 } else if (auto S = dyn_cast<ELFYAML::HashSection>(Sec)) { 570 writeSectionContent(SHeader, *S, CBA); 571 } else if (auto S = dyn_cast<ELFYAML::AddrsigSection>(Sec)) { 572 writeSectionContent(SHeader, *S, CBA); 573 } else if (auto S = dyn_cast<ELFYAML::LinkerOptionsSection>(Sec)) { 574 writeSectionContent(SHeader, *S, CBA); 575 } else if (auto S = dyn_cast<ELFYAML::NoteSection>(Sec)) { 576 writeSectionContent(SHeader, *S, CBA); 577 } else if (auto S = dyn_cast<ELFYAML::GnuHashSection>(Sec)) { 578 writeSectionContent(SHeader, *S, CBA); 579 } else if (auto S = dyn_cast<ELFYAML::DependentLibrariesSection>(Sec)) { 580 writeSectionContent(SHeader, *S, CBA); 581 } else if (auto S = dyn_cast<ELFYAML::CallGraphProfileSection>(Sec)) { 582 writeSectionContent(SHeader, *S, CBA); 583 } else { 584 llvm_unreachable("Unknown section type"); 585 } 586 587 LocationCounter += SHeader.sh_size; 588 589 // Override section fields if requested. 590 overrideFields<ELFT>(Sec, SHeader); 591 } 592 } 593 594 template <class ELFT> 595 void ELFState<ELFT>::assignSectionAddress(Elf_Shdr &SHeader, 596 ELFYAML::Section *YAMLSec) { 597 if (YAMLSec && YAMLSec->Address) { 598 SHeader.sh_addr = *YAMLSec->Address; 599 LocationCounter = *YAMLSec->Address; 600 return; 601 } 602 603 // sh_addr represents the address in the memory image of a process. Sections 604 // in a relocatable object file or non-allocatable sections do not need 605 // sh_addr assignment. 606 if (Doc.Header.Type.value == ELF::ET_REL || 607 !(SHeader.sh_flags & ELF::SHF_ALLOC)) 608 return; 609 610 LocationCounter = 611 alignTo(LocationCounter, SHeader.sh_addralign ? SHeader.sh_addralign : 1); 612 SHeader.sh_addr = LocationCounter; 613 } 614 615 static size_t findFirstNonGlobal(ArrayRef<ELFYAML::Symbol> Symbols) { 616 for (size_t I = 0; I < Symbols.size(); ++I) 617 if (Symbols[I].Binding.value != ELF::STB_LOCAL) 618 return I; 619 return Symbols.size(); 620 } 621 622 static uint64_t writeContent(raw_ostream &OS, 623 const Optional<yaml::BinaryRef> &Content, 624 const Optional<llvm::yaml::Hex64> &Size) { 625 size_t ContentSize = 0; 626 if (Content) { 627 Content->writeAsBinary(OS); 628 ContentSize = Content->binary_size(); 629 } 630 631 if (!Size) 632 return ContentSize; 633 634 OS.write_zeros(*Size - ContentSize); 635 return *Size; 636 } 637 638 template <class ELFT> 639 std::vector<typename ELFT::Sym> 640 ELFState<ELFT>::toELFSymbols(ArrayRef<ELFYAML::Symbol> Symbols, 641 const StringTableBuilder &Strtab) { 642 std::vector<Elf_Sym> Ret; 643 Ret.resize(Symbols.size() + 1); 644 645 size_t I = 0; 646 for (const ELFYAML::Symbol &Sym : Symbols) { 647 Elf_Sym &Symbol = Ret[++I]; 648 649 // If NameIndex, which contains the name offset, is explicitly specified, we 650 // use it. This is useful for preparing broken objects. Otherwise, we add 651 // the specified Name to the string table builder to get its offset. 652 if (Sym.StName) 653 Symbol.st_name = *Sym.StName; 654 else if (!Sym.Name.empty()) 655 Symbol.st_name = Strtab.getOffset(ELFYAML::dropUniqueSuffix(Sym.Name)); 656 657 Symbol.setBindingAndType(Sym.Binding, Sym.Type); 658 if (!Sym.Section.empty()) 659 Symbol.st_shndx = toSectionIndex(Sym.Section, "", Sym.Name); 660 else if (Sym.Index) 661 Symbol.st_shndx = *Sym.Index; 662 663 Symbol.st_value = Sym.Value; 664 Symbol.st_other = Sym.Other ? *Sym.Other : 0; 665 Symbol.st_size = Sym.Size; 666 } 667 668 return Ret; 669 } 670 671 template <class ELFT> 672 void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader, 673 SymtabType STType, 674 ContiguousBlobAccumulator &CBA, 675 ELFYAML::Section *YAMLSec) { 676 677 bool IsStatic = STType == SymtabType::Static; 678 ArrayRef<ELFYAML::Symbol> Symbols; 679 if (IsStatic && Doc.Symbols) 680 Symbols = *Doc.Symbols; 681 else if (!IsStatic && Doc.DynamicSymbols) 682 Symbols = *Doc.DynamicSymbols; 683 684 ELFYAML::RawContentSection *RawSec = 685 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec); 686 if (RawSec && (RawSec->Content || RawSec->Size)) { 687 bool HasSymbolsDescription = 688 (IsStatic && Doc.Symbols) || (!IsStatic && Doc.DynamicSymbols); 689 if (HasSymbolsDescription) { 690 StringRef Property = (IsStatic ? "`Symbols`" : "`DynamicSymbols`"); 691 if (RawSec->Content) 692 reportError("cannot specify both `Content` and " + Property + 693 " for symbol table section '" + RawSec->Name + "'"); 694 if (RawSec->Size) 695 reportError("cannot specify both `Size` and " + Property + 696 " for symbol table section '" + RawSec->Name + "'"); 697 return; 698 } 699 } 700 701 zero(SHeader); 702 SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym"); 703 704 if (YAMLSec) 705 SHeader.sh_type = YAMLSec->Type; 706 else 707 SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM; 708 709 if (RawSec && !RawSec->Link.empty()) { 710 // If the Link field is explicitly defined in the document, 711 // we should use it. 712 SHeader.sh_link = toSectionIndex(RawSec->Link, RawSec->Name); 713 } else { 714 // When we describe the .dynsym section in the document explicitly, it is 715 // allowed to omit the "DynamicSymbols" tag. In this case .dynstr is not 716 // added implicitly and we should be able to leave the Link zeroed if 717 // .dynstr is not defined. 718 unsigned Link = 0; 719 if (IsStatic) 720 Link = SN2I.get(".strtab"); 721 else 722 SN2I.lookup(".dynstr", Link); 723 SHeader.sh_link = Link; 724 } 725 726 if (YAMLSec && YAMLSec->Flags) 727 SHeader.sh_flags = *YAMLSec->Flags; 728 else if (!IsStatic) 729 SHeader.sh_flags = ELF::SHF_ALLOC; 730 731 // If the symbol table section is explicitly described in the YAML 732 // then we should set the fields requested. 733 SHeader.sh_info = (RawSec && RawSec->Info) ? (unsigned)(*RawSec->Info) 734 : findFirstNonGlobal(Symbols) + 1; 735 SHeader.sh_entsize = (YAMLSec && YAMLSec->EntSize) 736 ? (uint64_t)(*YAMLSec->EntSize) 737 : sizeof(Elf_Sym); 738 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 8; 739 740 assignSectionAddress(SHeader, YAMLSec); 741 742 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, /*Offset=*/None); 743 raw_ostream &OS = CBA.getOS(); 744 745 if (RawSec && (RawSec->Content || RawSec->Size)) { 746 assert(Symbols.empty()); 747 SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size); 748 return; 749 } 750 751 std::vector<Elf_Sym> Syms = 752 toELFSymbols(Symbols, IsStatic ? DotStrtab : DotDynstr); 753 writeArrayData(OS, makeArrayRef(Syms)); 754 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms)); 755 } 756 757 template <class ELFT> 758 void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, 759 StringTableBuilder &STB, 760 ContiguousBlobAccumulator &CBA, 761 ELFYAML::Section *YAMLSec) { 762 zero(SHeader); 763 SHeader.sh_name = DotShStrtab.getOffset(Name); 764 SHeader.sh_type = YAMLSec ? YAMLSec->Type : ELF::SHT_STRTAB; 765 SHeader.sh_addralign = YAMLSec ? (uint64_t)YAMLSec->AddressAlign : 1; 766 767 ELFYAML::RawContentSection *RawSec = 768 dyn_cast_or_null<ELFYAML::RawContentSection>(YAMLSec); 769 770 SHeader.sh_offset = alignToOffset(CBA, SHeader.sh_addralign, /*Offset=*/None); 771 raw_ostream &OS = CBA.getOS(); 772 773 if (RawSec && (RawSec->Content || RawSec->Size)) { 774 SHeader.sh_size = writeContent(OS, RawSec->Content, RawSec->Size); 775 } else { 776 STB.write(OS); 777 SHeader.sh_size = STB.getSize(); 778 } 779 780 if (YAMLSec && YAMLSec->EntSize) 781 SHeader.sh_entsize = *YAMLSec->EntSize; 782 783 if (RawSec && RawSec->Info) 784 SHeader.sh_info = *RawSec->Info; 785 786 if (YAMLSec && YAMLSec->Flags) 787 SHeader.sh_flags = *YAMLSec->Flags; 788 else if (Name == ".dynstr") 789 SHeader.sh_flags = ELF::SHF_ALLOC; 790 791 assignSectionAddress(SHeader, YAMLSec); 792 } 793 794 template <class ELFT> void ELFState<ELFT>::reportError(const Twine &Msg) { 795 ErrHandler(Msg); 796 HasError = true; 797 } 798 799 template <class ELFT> 800 std::vector<Fragment> 801 ELFState<ELFT>::getPhdrFragments(const ELFYAML::ProgramHeader &Phdr, 802 ArrayRef<Elf_Shdr> SHeaders) { 803 std::vector<Fragment> Ret; 804 for (const ELFYAML::Chunk *C : Phdr.Chunks) { 805 if (const ELFYAML::Fill *F = dyn_cast<ELFYAML::Fill>(C)) { 806 Ret.push_back({*F->Offset, F->Size, llvm::ELF::SHT_PROGBITS, 807 /*ShAddrAlign=*/1}); 808 continue; 809 } 810 811 const ELFYAML::Section *S = cast<ELFYAML::Section>(C); 812 const Elf_Shdr &H = SHeaders[SN2I.get(S->Name)]; 813 Ret.push_back({H.sh_offset, H.sh_size, H.sh_type, H.sh_addralign}); 814 } 815 return Ret; 816 } 817 818 template <class ELFT> 819 void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders, 820 std::vector<Elf_Shdr> &SHeaders) { 821 uint32_t PhdrIdx = 0; 822 for (auto &YamlPhdr : Doc.ProgramHeaders) { 823 Elf_Phdr &PHeader = PHeaders[PhdrIdx++]; 824 std::vector<Fragment> Fragments = getPhdrFragments(YamlPhdr, SHeaders); 825 if (!llvm::is_sorted(Fragments, [](const Fragment &A, const Fragment &B) { 826 return A.Offset < B.Offset; 827 })) 828 reportError("sections in the program header with index " + 829 Twine(PhdrIdx) + " are not sorted by their file offset"); 830 831 if (YamlPhdr.Offset) { 832 if (!Fragments.empty() && *YamlPhdr.Offset > Fragments.front().Offset) 833 reportError("'Offset' for segment with index " + Twine(PhdrIdx) + 834 " must be less than or equal to the minimum file offset of " 835 "all included sections (0x" + 836 Twine::utohexstr(Fragments.front().Offset) + ")"); 837 PHeader.p_offset = *YamlPhdr.Offset; 838 } else if (!Fragments.empty()) { 839 PHeader.p_offset = Fragments.front().Offset; 840 } 841 842 // Set the file size if not set explicitly. 843 if (YamlPhdr.FileSize) { 844 PHeader.p_filesz = *YamlPhdr.FileSize; 845 } else if (!Fragments.empty()) { 846 uint64_t FileSize = Fragments.back().Offset - PHeader.p_offset; 847 // SHT_NOBITS sections occupy no physical space in a file, we should not 848 // take their sizes into account when calculating the file size of a 849 // segment. 850 if (Fragments.back().Type != llvm::ELF::SHT_NOBITS) 851 FileSize += Fragments.back().Size; 852 PHeader.p_filesz = FileSize; 853 } 854 855 // Find the maximum offset of the end of a section in order to set p_memsz. 856 uint64_t MemOffset = PHeader.p_offset; 857 for (const Fragment &F : Fragments) 858 MemOffset = std::max(MemOffset, F.Offset + F.Size); 859 // Set the memory size if not set explicitly. 860 PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize) 861 : MemOffset - PHeader.p_offset; 862 863 if (YamlPhdr.Align) { 864 PHeader.p_align = *YamlPhdr.Align; 865 } else { 866 // Set the alignment of the segment to be the maximum alignment of the 867 // sections so that by default the segment has a valid and sensible 868 // alignment. 869 PHeader.p_align = 1; 870 for (const Fragment &F : Fragments) 871 PHeader.p_align = std::max((uint64_t)PHeader.p_align, F.AddrAlign); 872 } 873 } 874 } 875 876 template <class ELFT> 877 void ELFState<ELFT>::writeSectionContent( 878 Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, 879 ContiguousBlobAccumulator &CBA) { 880 SHeader.sh_size = writeContent(CBA.getOS(), Section.Content, Section.Size); 881 882 if (Section.EntSize) 883 SHeader.sh_entsize = *Section.EntSize; 884 885 if (Section.Info) 886 SHeader.sh_info = *Section.Info; 887 } 888 889 static bool isMips64EL(const ELFYAML::Object &Doc) { 890 return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) && 891 Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) && 892 Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); 893 } 894 895 template <class ELFT> 896 void ELFState<ELFT>::writeSectionContent( 897 Elf_Shdr &SHeader, const ELFYAML::RelocationSection &Section, 898 ContiguousBlobAccumulator &CBA) { 899 assert((Section.Type == llvm::ELF::SHT_REL || 900 Section.Type == llvm::ELF::SHT_RELA) && 901 "Section type is not SHT_REL nor SHT_RELA"); 902 903 bool IsRela = Section.Type == llvm::ELF::SHT_RELA; 904 if (Section.EntSize) 905 SHeader.sh_entsize = *Section.EntSize; 906 else 907 SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); 908 SHeader.sh_size = (IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel)) * 909 Section.Relocations.size(); 910 911 // For relocation section set link to .symtab by default. 912 unsigned Link = 0; 913 if (Section.Link.empty() && SN2I.lookup(".symtab", Link)) 914 SHeader.sh_link = Link; 915 916 if (!Section.RelocatableSec.empty()) 917 SHeader.sh_info = toSectionIndex(Section.RelocatableSec, Section.Name); 918 919 raw_ostream &OS = CBA.getOS(); 920 for (const auto &Rel : Section.Relocations) { 921 unsigned SymIdx = Rel.Symbol ? toSymbolIndex(*Rel.Symbol, Section.Name, 922 Section.Link == ".dynsym") 923 : 0; 924 if (IsRela) { 925 Elf_Rela REntry; 926 zero(REntry); 927 REntry.r_offset = Rel.Offset; 928 REntry.r_addend = Rel.Addend; 929 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc)); 930 OS.write((const char *)&REntry, sizeof(REntry)); 931 } else { 932 Elf_Rel REntry; 933 zero(REntry); 934 REntry.r_offset = Rel.Offset; 935 REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc)); 936 OS.write((const char *)&REntry, sizeof(REntry)); 937 } 938 } 939 } 940 941 template <class ELFT> 942 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 943 const ELFYAML::RelrSection &Section, 944 ContiguousBlobAccumulator &CBA) { 945 SHeader.sh_entsize = 946 Section.EntSize ? uint64_t(*Section.EntSize) : sizeof(Elf_Relr); 947 948 raw_ostream &OS = CBA.getOS(); 949 if (Section.Content) { 950 SHeader.sh_size = writeContent(OS, Section.Content, None); 951 return; 952 } 953 954 if (!Section.Entries) 955 return; 956 957 for (llvm::yaml::Hex64 E : *Section.Entries) { 958 if (!ELFT::Is64Bits && E > UINT32_MAX) 959 reportError(Section.Name + ": the value is too large for 32-bits: 0x" + 960 Twine::utohexstr(E)); 961 support::endian::write<uintX_t>(OS, E, ELFT::TargetEndianness); 962 } 963 964 SHeader.sh_size = sizeof(uintX_t) * Section.Entries->size(); 965 } 966 967 template <class ELFT> 968 void ELFState<ELFT>::writeSectionContent( 969 Elf_Shdr &SHeader, const ELFYAML::SymtabShndxSection &Shndx, 970 ContiguousBlobAccumulator &CBA) { 971 for (uint32_t E : Shndx.Entries) 972 support::endian::write<uint32_t>(CBA.getOS(), E, ELFT::TargetEndianness); 973 974 SHeader.sh_entsize = Shndx.EntSize ? (uint64_t)*Shndx.EntSize : 4; 975 SHeader.sh_size = Shndx.Entries.size() * SHeader.sh_entsize; 976 } 977 978 template <class ELFT> 979 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 980 const ELFYAML::Group &Section, 981 ContiguousBlobAccumulator &CBA) { 982 assert(Section.Type == llvm::ELF::SHT_GROUP && 983 "Section type is not SHT_GROUP"); 984 985 unsigned Link = 0; 986 if (Section.Link.empty() && SN2I.lookup(".symtab", Link)) 987 SHeader.sh_link = Link; 988 989 SHeader.sh_entsize = 4; 990 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size(); 991 992 if (Section.Signature) 993 SHeader.sh_info = 994 toSymbolIndex(*Section.Signature, Section.Name, /*IsDynamic=*/false); 995 996 raw_ostream &OS = CBA.getOS(); 997 for (const ELFYAML::SectionOrType &Member : Section.Members) { 998 unsigned int SectionIndex = 0; 999 if (Member.sectionNameOrType == "GRP_COMDAT") 1000 SectionIndex = llvm::ELF::GRP_COMDAT; 1001 else 1002 SectionIndex = toSectionIndex(Member.sectionNameOrType, Section.Name); 1003 support::endian::write<uint32_t>(OS, SectionIndex, ELFT::TargetEndianness); 1004 } 1005 } 1006 1007 template <class ELFT> 1008 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 1009 const ELFYAML::SymverSection &Section, 1010 ContiguousBlobAccumulator &CBA) { 1011 raw_ostream &OS = CBA.getOS(); 1012 for (uint16_t Version : Section.Entries) 1013 support::endian::write<uint16_t>(OS, Version, ELFT::TargetEndianness); 1014 1015 SHeader.sh_entsize = Section.EntSize ? (uint64_t)*Section.EntSize : 2; 1016 SHeader.sh_size = Section.Entries.size() * SHeader.sh_entsize; 1017 } 1018 1019 template <class ELFT> 1020 void ELFState<ELFT>::writeSectionContent( 1021 Elf_Shdr &SHeader, const ELFYAML::StackSizesSection &Section, 1022 ContiguousBlobAccumulator &CBA) { 1023 raw_ostream &OS = CBA.getOS(); 1024 if (Section.Content || Section.Size) { 1025 SHeader.sh_size = writeContent(OS, Section.Content, Section.Size); 1026 return; 1027 } 1028 1029 for (const ELFYAML::StackSizeEntry &E : *Section.Entries) { 1030 support::endian::write<uintX_t>(OS, E.Address, ELFT::TargetEndianness); 1031 SHeader.sh_size += sizeof(uintX_t) + encodeULEB128(E.Size, OS); 1032 } 1033 } 1034 1035 template <class ELFT> 1036 void ELFState<ELFT>::writeSectionContent( 1037 Elf_Shdr &SHeader, const ELFYAML::LinkerOptionsSection &Section, 1038 ContiguousBlobAccumulator &CBA) { 1039 raw_ostream &OS = CBA.getOS(); 1040 if (Section.Content) { 1041 SHeader.sh_size = writeContent(OS, Section.Content, None); 1042 return; 1043 } 1044 1045 if (!Section.Options) 1046 return; 1047 1048 for (const ELFYAML::LinkerOption &LO : *Section.Options) { 1049 OS.write(LO.Key.data(), LO.Key.size()); 1050 OS.write('\0'); 1051 OS.write(LO.Value.data(), LO.Value.size()); 1052 OS.write('\0'); 1053 SHeader.sh_size += (LO.Key.size() + LO.Value.size() + 2); 1054 } 1055 } 1056 1057 template <class ELFT> 1058 void ELFState<ELFT>::writeSectionContent( 1059 Elf_Shdr &SHeader, const ELFYAML::DependentLibrariesSection &Section, 1060 ContiguousBlobAccumulator &CBA) { 1061 raw_ostream &OS = CBA.getOS(); 1062 if (Section.Content) { 1063 SHeader.sh_size = writeContent(OS, Section.Content, None); 1064 return; 1065 } 1066 1067 if (!Section.Libs) 1068 return; 1069 1070 for (StringRef Lib : *Section.Libs) { 1071 OS.write(Lib.data(), Lib.size()); 1072 OS.write('\0'); 1073 SHeader.sh_size += Lib.size() + 1; 1074 } 1075 } 1076 1077 template <class ELFT> 1078 uint64_t 1079 ELFState<ELFT>::alignToOffset(ContiguousBlobAccumulator &CBA, uint64_t Align, 1080 llvm::Optional<llvm::yaml::Hex64> Offset) { 1081 uint64_t CurrentOffset = CBA.getOffset(); 1082 uint64_t AlignedOffset; 1083 1084 if (Offset) { 1085 if ((uint64_t)*Offset < CurrentOffset) { 1086 reportError("the 'Offset' value (0x" + 1087 Twine::utohexstr((uint64_t)*Offset) + ") goes backward"); 1088 return CurrentOffset; 1089 } 1090 1091 // We ignore an alignment when an explicit offset has been requested. 1092 AlignedOffset = *Offset; 1093 } else { 1094 AlignedOffset = alignTo(CurrentOffset, std::max(Align, (uint64_t)1)); 1095 } 1096 1097 CBA.getOS().write_zeros(AlignedOffset - CurrentOffset); 1098 return AlignedOffset; 1099 } 1100 1101 template <class ELFT> 1102 void ELFState<ELFT>::writeSectionContent( 1103 Elf_Shdr &SHeader, const ELFYAML::CallGraphProfileSection &Section, 1104 ContiguousBlobAccumulator &CBA) { 1105 if (Section.EntSize) 1106 SHeader.sh_entsize = *Section.EntSize; 1107 else 1108 SHeader.sh_entsize = 16; 1109 1110 unsigned Link = 0; 1111 if (Section.Link.empty() && SN2I.lookup(".symtab", Link)) 1112 SHeader.sh_link = Link; 1113 1114 raw_ostream &OS = CBA.getOS(); 1115 if (Section.Content) { 1116 SHeader.sh_size = writeContent(OS, Section.Content, None); 1117 return; 1118 } 1119 1120 if (!Section.Entries) 1121 return; 1122 1123 for (const ELFYAML::CallGraphEntry &E : *Section.Entries) { 1124 unsigned From = toSymbolIndex(E.From, Section.Name, /*IsDynamic=*/false); 1125 unsigned To = toSymbolIndex(E.To, Section.Name, /*IsDynamic=*/false); 1126 1127 support::endian::write<uint32_t>(OS, From, ELFT::TargetEndianness); 1128 support::endian::write<uint32_t>(OS, To, ELFT::TargetEndianness); 1129 support::endian::write<uint64_t>(OS, E.Weight, ELFT::TargetEndianness); 1130 SHeader.sh_size += 16; 1131 } 1132 } 1133 1134 template <class ELFT> 1135 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 1136 const ELFYAML::HashSection &Section, 1137 ContiguousBlobAccumulator &CBA) { 1138 unsigned Link = 0; 1139 if (Section.Link.empty() && SN2I.lookup(".dynsym", Link)) 1140 SHeader.sh_link = Link; 1141 1142 raw_ostream &OS = CBA.getOS(); 1143 if (Section.Content || Section.Size) { 1144 SHeader.sh_size = writeContent(OS, Section.Content, Section.Size); 1145 return; 1146 } 1147 1148 support::endian::write<uint32_t>( 1149 OS, Section.NBucket.getValueOr(llvm::yaml::Hex64(Section.Bucket->size())), 1150 ELFT::TargetEndianness); 1151 support::endian::write<uint32_t>( 1152 OS, Section.NChain.getValueOr(llvm::yaml::Hex64(Section.Chain->size())), 1153 ELFT::TargetEndianness); 1154 1155 for (uint32_t Val : *Section.Bucket) 1156 support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness); 1157 for (uint32_t Val : *Section.Chain) 1158 support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness); 1159 1160 SHeader.sh_size = (2 + Section.Bucket->size() + Section.Chain->size()) * 4; 1161 } 1162 1163 template <class ELFT> 1164 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 1165 const ELFYAML::VerdefSection &Section, 1166 ContiguousBlobAccumulator &CBA) { 1167 typedef typename ELFT::Verdef Elf_Verdef; 1168 typedef typename ELFT::Verdaux Elf_Verdaux; 1169 1170 SHeader.sh_info = Section.Info; 1171 1172 raw_ostream &OS = CBA.getOS(); 1173 if (Section.Content) { 1174 SHeader.sh_size = writeContent(OS, Section.Content, None); 1175 return; 1176 } 1177 1178 if (!Section.Entries) 1179 return; 1180 1181 uint64_t AuxCnt = 0; 1182 for (size_t I = 0; I < Section.Entries->size(); ++I) { 1183 const ELFYAML::VerdefEntry &E = (*Section.Entries)[I]; 1184 1185 Elf_Verdef VerDef; 1186 VerDef.vd_version = E.Version; 1187 VerDef.vd_flags = E.Flags; 1188 VerDef.vd_ndx = E.VersionNdx; 1189 VerDef.vd_hash = E.Hash; 1190 VerDef.vd_aux = sizeof(Elf_Verdef); 1191 VerDef.vd_cnt = E.VerNames.size(); 1192 if (I == Section.Entries->size() - 1) 1193 VerDef.vd_next = 0; 1194 else 1195 VerDef.vd_next = 1196 sizeof(Elf_Verdef) + E.VerNames.size() * sizeof(Elf_Verdaux); 1197 OS.write((const char *)&VerDef, sizeof(Elf_Verdef)); 1198 1199 for (size_t J = 0; J < E.VerNames.size(); ++J, ++AuxCnt) { 1200 Elf_Verdaux VernAux; 1201 VernAux.vda_name = DotDynstr.getOffset(E.VerNames[J]); 1202 if (J == E.VerNames.size() - 1) 1203 VernAux.vda_next = 0; 1204 else 1205 VernAux.vda_next = sizeof(Elf_Verdaux); 1206 OS.write((const char *)&VernAux, sizeof(Elf_Verdaux)); 1207 } 1208 } 1209 1210 SHeader.sh_size = Section.Entries->size() * sizeof(Elf_Verdef) + 1211 AuxCnt * sizeof(Elf_Verdaux); 1212 } 1213 1214 template <class ELFT> 1215 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 1216 const ELFYAML::VerneedSection &Section, 1217 ContiguousBlobAccumulator &CBA) { 1218 typedef typename ELFT::Verneed Elf_Verneed; 1219 typedef typename ELFT::Vernaux Elf_Vernaux; 1220 1221 SHeader.sh_info = Section.Info; 1222 1223 raw_ostream &OS = CBA.getOS(); 1224 if (Section.Content) { 1225 SHeader.sh_size = writeContent(OS, Section.Content, None); 1226 return; 1227 } 1228 1229 if (!Section.VerneedV) 1230 return; 1231 1232 uint64_t AuxCnt = 0; 1233 for (size_t I = 0; I < Section.VerneedV->size(); ++I) { 1234 const ELFYAML::VerneedEntry &VE = (*Section.VerneedV)[I]; 1235 1236 Elf_Verneed VerNeed; 1237 VerNeed.vn_version = VE.Version; 1238 VerNeed.vn_file = DotDynstr.getOffset(VE.File); 1239 if (I == Section.VerneedV->size() - 1) 1240 VerNeed.vn_next = 0; 1241 else 1242 VerNeed.vn_next = 1243 sizeof(Elf_Verneed) + VE.AuxV.size() * sizeof(Elf_Vernaux); 1244 VerNeed.vn_cnt = VE.AuxV.size(); 1245 VerNeed.vn_aux = sizeof(Elf_Verneed); 1246 OS.write((const char *)&VerNeed, sizeof(Elf_Verneed)); 1247 1248 for (size_t J = 0; J < VE.AuxV.size(); ++J, ++AuxCnt) { 1249 const ELFYAML::VernauxEntry &VAuxE = VE.AuxV[J]; 1250 1251 Elf_Vernaux VernAux; 1252 VernAux.vna_hash = VAuxE.Hash; 1253 VernAux.vna_flags = VAuxE.Flags; 1254 VernAux.vna_other = VAuxE.Other; 1255 VernAux.vna_name = DotDynstr.getOffset(VAuxE.Name); 1256 if (J == VE.AuxV.size() - 1) 1257 VernAux.vna_next = 0; 1258 else 1259 VernAux.vna_next = sizeof(Elf_Vernaux); 1260 OS.write((const char *)&VernAux, sizeof(Elf_Vernaux)); 1261 } 1262 } 1263 1264 SHeader.sh_size = Section.VerneedV->size() * sizeof(Elf_Verneed) + 1265 AuxCnt * sizeof(Elf_Vernaux); 1266 } 1267 1268 template <class ELFT> 1269 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 1270 const ELFYAML::MipsABIFlags &Section, 1271 ContiguousBlobAccumulator &CBA) { 1272 assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS && 1273 "Section type is not SHT_MIPS_ABIFLAGS"); 1274 1275 object::Elf_Mips_ABIFlags<ELFT> Flags; 1276 zero(Flags); 1277 SHeader.sh_entsize = sizeof(Flags); 1278 SHeader.sh_size = SHeader.sh_entsize; 1279 1280 Flags.version = Section.Version; 1281 Flags.isa_level = Section.ISALevel; 1282 Flags.isa_rev = Section.ISARevision; 1283 Flags.gpr_size = Section.GPRSize; 1284 Flags.cpr1_size = Section.CPR1Size; 1285 Flags.cpr2_size = Section.CPR2Size; 1286 Flags.fp_abi = Section.FpABI; 1287 Flags.isa_ext = Section.ISAExtension; 1288 Flags.ases = Section.ASEs; 1289 Flags.flags1 = Section.Flags1; 1290 Flags.flags2 = Section.Flags2; 1291 CBA.getOS().write((const char *)&Flags, sizeof(Flags)); 1292 } 1293 1294 template <class ELFT> 1295 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 1296 const ELFYAML::DynamicSection &Section, 1297 ContiguousBlobAccumulator &CBA) { 1298 assert(Section.Type == llvm::ELF::SHT_DYNAMIC && 1299 "Section type is not SHT_DYNAMIC"); 1300 1301 if (!Section.Entries.empty() && Section.Content) 1302 reportError("cannot specify both raw content and explicit entries " 1303 "for dynamic section '" + 1304 Section.Name + "'"); 1305 1306 if (Section.Content) 1307 SHeader.sh_size = Section.Content->binary_size(); 1308 else 1309 SHeader.sh_size = 2 * sizeof(uintX_t) * Section.Entries.size(); 1310 if (Section.EntSize) 1311 SHeader.sh_entsize = *Section.EntSize; 1312 else 1313 SHeader.sh_entsize = sizeof(Elf_Dyn); 1314 1315 raw_ostream &OS = CBA.getOS(); 1316 for (const ELFYAML::DynamicEntry &DE : Section.Entries) { 1317 support::endian::write<uintX_t>(OS, DE.Tag, ELFT::TargetEndianness); 1318 support::endian::write<uintX_t>(OS, DE.Val, ELFT::TargetEndianness); 1319 } 1320 if (Section.Content) 1321 Section.Content->writeAsBinary(OS); 1322 } 1323 1324 template <class ELFT> 1325 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 1326 const ELFYAML::AddrsigSection &Section, 1327 ContiguousBlobAccumulator &CBA) { 1328 unsigned Link = 0; 1329 if (Section.Link.empty() && SN2I.lookup(".symtab", Link)) 1330 SHeader.sh_link = Link; 1331 1332 raw_ostream &OS = CBA.getOS(); 1333 if (Section.Content || Section.Size) { 1334 SHeader.sh_size = writeContent(OS, Section.Content, Section.Size); 1335 return; 1336 } 1337 1338 for (StringRef Sym : *Section.Symbols) 1339 SHeader.sh_size += encodeULEB128( 1340 toSymbolIndex(Sym, Section.Name, /*IsDynamic=*/false), OS); 1341 } 1342 1343 template <class ELFT> 1344 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 1345 const ELFYAML::NoteSection &Section, 1346 ContiguousBlobAccumulator &CBA) { 1347 raw_ostream &OS = CBA.getOS(); 1348 uint64_t Offset = OS.tell(); 1349 if (Section.Content || Section.Size) { 1350 SHeader.sh_size = writeContent(OS, Section.Content, Section.Size); 1351 return; 1352 } 1353 1354 for (const ELFYAML::NoteEntry &NE : *Section.Notes) { 1355 // Write name size. 1356 if (NE.Name.empty()) 1357 support::endian::write<uint32_t>(OS, 0, ELFT::TargetEndianness); 1358 else 1359 support::endian::write<uint32_t>(OS, NE.Name.size() + 1, 1360 ELFT::TargetEndianness); 1361 1362 // Write description size. 1363 if (NE.Desc.binary_size() == 0) 1364 support::endian::write<uint32_t>(OS, 0, ELFT::TargetEndianness); 1365 else 1366 support::endian::write<uint32_t>(OS, NE.Desc.binary_size(), 1367 ELFT::TargetEndianness); 1368 1369 // Write type. 1370 support::endian::write<uint32_t>(OS, NE.Type, ELFT::TargetEndianness); 1371 1372 // Write name, null terminator and padding. 1373 if (!NE.Name.empty()) { 1374 support::endian::write<uint8_t>(OS, arrayRefFromStringRef(NE.Name), 1375 ELFT::TargetEndianness); 1376 support::endian::write<uint8_t>(OS, 0, ELFT::TargetEndianness); 1377 CBA.padToAlignment(4); 1378 } 1379 1380 // Write description and padding. 1381 if (NE.Desc.binary_size() != 0) { 1382 NE.Desc.writeAsBinary(OS); 1383 CBA.padToAlignment(4); 1384 } 1385 } 1386 1387 SHeader.sh_size = OS.tell() - Offset; 1388 } 1389 1390 template <class ELFT> 1391 void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, 1392 const ELFYAML::GnuHashSection &Section, 1393 ContiguousBlobAccumulator &CBA) { 1394 unsigned Link = 0; 1395 if (Section.Link.empty() && SN2I.lookup(".dynsym", Link)) 1396 SHeader.sh_link = Link; 1397 1398 raw_ostream &OS = CBA.getOS(); 1399 if (Section.Content) { 1400 SHeader.sh_size = writeContent(OS, Section.Content, None); 1401 return; 1402 } 1403 1404 // We write the header first, starting with the hash buckets count. Normally 1405 // it is the number of entries in HashBuckets, but the "NBuckets" property can 1406 // be used to override this field, which is useful for producing broken 1407 // objects. 1408 if (Section.Header->NBuckets) 1409 support::endian::write<uint32_t>(OS, *Section.Header->NBuckets, 1410 ELFT::TargetEndianness); 1411 else 1412 support::endian::write<uint32_t>(OS, Section.HashBuckets->size(), 1413 ELFT::TargetEndianness); 1414 1415 // Write the index of the first symbol in the dynamic symbol table accessible 1416 // via the hash table. 1417 support::endian::write<uint32_t>(OS, Section.Header->SymNdx, 1418 ELFT::TargetEndianness); 1419 1420 // Write the number of words in the Bloom filter. As above, the "MaskWords" 1421 // property can be used to set this field to any value. 1422 if (Section.Header->MaskWords) 1423 support::endian::write<uint32_t>(OS, *Section.Header->MaskWords, 1424 ELFT::TargetEndianness); 1425 else 1426 support::endian::write<uint32_t>(OS, Section.BloomFilter->size(), 1427 ELFT::TargetEndianness); 1428 1429 // Write the shift constant used by the Bloom filter. 1430 support::endian::write<uint32_t>(OS, Section.Header->Shift2, 1431 ELFT::TargetEndianness); 1432 1433 // We've finished writing the header. Now write the Bloom filter. 1434 for (llvm::yaml::Hex64 Val : *Section.BloomFilter) 1435 support::endian::write<typename ELFT::uint>(OS, Val, 1436 ELFT::TargetEndianness); 1437 1438 // Write an array of hash buckets. 1439 for (llvm::yaml::Hex32 Val : *Section.HashBuckets) 1440 support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness); 1441 1442 // Write an array of hash values. 1443 for (llvm::yaml::Hex32 Val : *Section.HashValues) 1444 support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness); 1445 1446 SHeader.sh_size = 16 /*Header size*/ + 1447 Section.BloomFilter->size() * sizeof(typename ELFT::uint) + 1448 Section.HashBuckets->size() * 4 + 1449 Section.HashValues->size() * 4; 1450 } 1451 1452 template <class ELFT> 1453 void ELFState<ELFT>::writeFill(ELFYAML::Fill &Fill, 1454 ContiguousBlobAccumulator &CBA) { 1455 raw_ostream &OS = CBA.getOS(); 1456 size_t PatternSize = Fill.Pattern ? Fill.Pattern->binary_size() : 0; 1457 if (!PatternSize) { 1458 OS.write_zeros(Fill.Size); 1459 return; 1460 } 1461 1462 // Fill the content with the specified pattern. 1463 uint64_t Written = 0; 1464 for (; Written + PatternSize <= Fill.Size; Written += PatternSize) 1465 Fill.Pattern->writeAsBinary(OS); 1466 Fill.Pattern->writeAsBinary(OS, Fill.Size - Written); 1467 } 1468 1469 template <class ELFT> 1470 DenseMap<StringRef, size_t> ELFState<ELFT>::buildSectionHeaderReorderMap() { 1471 if (!Doc.SectionHeaders || Doc.SectionHeaders->Sections.empty()) 1472 return DenseMap<StringRef, size_t>(); 1473 1474 DenseMap<StringRef, size_t> Ret; 1475 size_t SecNdx = 0; 1476 StringSet<> Seen; 1477 for (const ELFYAML::SectionHeader &Hdr : Doc.SectionHeaders->Sections) { 1478 if (!Ret.try_emplace(Hdr.Name, ++SecNdx).second) 1479 reportError("repeated section name: '" + Hdr.Name + 1480 "' in the section header description"); 1481 Seen.insert(Hdr.Name); 1482 } 1483 1484 for (const ELFYAML::Section *S : Doc.getSections()) { 1485 // Ignore special first SHT_NULL section. 1486 if (S == Doc.getSections().front()) 1487 continue; 1488 if (!Seen.count(S->Name)) 1489 reportError("section '" + S->Name + 1490 "' should be present in the 'Sections' list"); 1491 Seen.erase(S->Name); 1492 } 1493 1494 for (const auto &It : Seen) 1495 reportError("section header contains undefined section '" + It.getKey() + 1496 "'"); 1497 return Ret; 1498 } 1499 1500 template <class ELFT> void ELFState<ELFT>::buildSectionIndex() { 1501 // A YAML description can have an explicit section header declaration that allows 1502 // to change the order of section headers. 1503 DenseMap<StringRef, size_t> ReorderMap = buildSectionHeaderReorderMap(); 1504 1505 size_t SecNdx = -1; 1506 for (const std::unique_ptr<ELFYAML::Chunk> &C : Doc.Chunks) { 1507 if (!isa<ELFYAML::Section>(C.get())) 1508 continue; 1509 ++SecNdx; 1510 1511 size_t Index = ReorderMap.empty() ? SecNdx : ReorderMap.lookup(C->Name); 1512 if (!SN2I.addName(C->Name, Index)) 1513 llvm_unreachable("buildSectionIndex() failed"); 1514 DotShStrtab.add(ELFYAML::dropUniqueSuffix(C->Name)); 1515 } 1516 1517 DotShStrtab.finalize(); 1518 } 1519 1520 template <class ELFT> void ELFState<ELFT>::buildSymbolIndexes() { 1521 auto Build = [this](ArrayRef<ELFYAML::Symbol> V, NameToIdxMap &Map) { 1522 for (size_t I = 0, S = V.size(); I < S; ++I) { 1523 const ELFYAML::Symbol &Sym = V[I]; 1524 if (!Sym.Name.empty() && !Map.addName(Sym.Name, I + 1)) 1525 reportError("repeated symbol name: '" + Sym.Name + "'"); 1526 } 1527 }; 1528 1529 if (Doc.Symbols) 1530 Build(*Doc.Symbols, SymN2I); 1531 if (Doc.DynamicSymbols) 1532 Build(*Doc.DynamicSymbols, DynSymN2I); 1533 } 1534 1535 template <class ELFT> void ELFState<ELFT>::finalizeStrings() { 1536 // Add the regular symbol names to .strtab section. 1537 if (Doc.Symbols) 1538 for (const ELFYAML::Symbol &Sym : *Doc.Symbols) 1539 DotStrtab.add(ELFYAML::dropUniqueSuffix(Sym.Name)); 1540 DotStrtab.finalize(); 1541 1542 // Add the dynamic symbol names to .dynstr section. 1543 if (Doc.DynamicSymbols) 1544 for (const ELFYAML::Symbol &Sym : *Doc.DynamicSymbols) 1545 DotDynstr.add(ELFYAML::dropUniqueSuffix(Sym.Name)); 1546 1547 // SHT_GNU_verdef and SHT_GNU_verneed sections might also 1548 // add strings to .dynstr section. 1549 for (const ELFYAML::Chunk *Sec : Doc.getSections()) { 1550 if (auto VerNeed = dyn_cast<ELFYAML::VerneedSection>(Sec)) { 1551 if (VerNeed->VerneedV) { 1552 for (const ELFYAML::VerneedEntry &VE : *VerNeed->VerneedV) { 1553 DotDynstr.add(VE.File); 1554 for (const ELFYAML::VernauxEntry &Aux : VE.AuxV) 1555 DotDynstr.add(Aux.Name); 1556 } 1557 } 1558 } else if (auto VerDef = dyn_cast<ELFYAML::VerdefSection>(Sec)) { 1559 if (VerDef->Entries) 1560 for (const ELFYAML::VerdefEntry &E : *VerDef->Entries) 1561 for (StringRef Name : E.VerNames) 1562 DotDynstr.add(Name); 1563 } 1564 } 1565 1566 DotDynstr.finalize(); 1567 } 1568 1569 template <class ELFT> 1570 bool ELFState<ELFT>::writeELF(raw_ostream &OS, ELFYAML::Object &Doc, 1571 yaml::ErrorHandler EH) { 1572 ELFState<ELFT> State(Doc, EH); 1573 if (State.HasError) 1574 return false; 1575 1576 // Finalize .strtab and .dynstr sections. We do that early because want to 1577 // finalize the string table builders before writing the content of the 1578 // sections that might want to use them. 1579 State.finalizeStrings(); 1580 1581 State.buildSectionIndex(); 1582 State.buildSymbolIndexes(); 1583 1584 std::vector<Elf_Phdr> PHeaders; 1585 State.initProgramHeaders(PHeaders); 1586 1587 // XXX: This offset is tightly coupled with the order that we write 1588 // things to `OS`. 1589 const size_t SectionContentBeginOffset = 1590 sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size(); 1591 ContiguousBlobAccumulator CBA(SectionContentBeginOffset); 1592 1593 std::vector<Elf_Shdr> SHeaders; 1594 State.initSectionHeaders(SHeaders, CBA); 1595 1596 // Now we can decide segment offsets. 1597 State.setProgramHeaderLayout(PHeaders, SHeaders); 1598 1599 if (State.HasError) 1600 return false; 1601 1602 State.writeELFHeader(CBA, OS); 1603 writeArrayData(OS, makeArrayRef(PHeaders)); 1604 CBA.writeBlobToStream(OS); 1605 writeArrayData(OS, makeArrayRef(SHeaders)); 1606 return true; 1607 } 1608 1609 namespace llvm { 1610 namespace yaml { 1611 1612 bool yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) { 1613 bool IsLE = Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); 1614 bool Is64Bit = Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64); 1615 if (Is64Bit) { 1616 if (IsLE) 1617 return ELFState<object::ELF64LE>::writeELF(Out, Doc, EH); 1618 return ELFState<object::ELF64BE>::writeELF(Out, Doc, EH); 1619 } 1620 if (IsLE) 1621 return ELFState<object::ELF32LE>::writeELF(Out, Doc, EH); 1622 return ELFState<object::ELF32BE>::writeELF(Out, Doc, EH); 1623 } 1624 1625 } // namespace yaml 1626 } // namespace llvm 1627