18bcb0991SDimitry Andric //===- yaml2macho - Convert YAML to a Mach object file --------------------===// 28bcb0991SDimitry Andric // 38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 68bcb0991SDimitry Andric // 78bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 88bcb0991SDimitry Andric /// 98bcb0991SDimitry Andric /// \file 108bcb0991SDimitry Andric /// The Mach component of yaml2obj. 118bcb0991SDimitry Andric /// 128bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 138bcb0991SDimitry Andric 148bcb0991SDimitry Andric #include "llvm/BinaryFormat/MachO.h" 158bcb0991SDimitry Andric #include "llvm/ObjectYAML/DWARFEmitter.h" 168bcb0991SDimitry Andric #include "llvm/ObjectYAML/ObjectYAML.h" 178bcb0991SDimitry Andric #include "llvm/ObjectYAML/yaml2obj.h" 185ffd83dbSDimitry Andric #include "llvm/Support/Errc.h" 195ffd83dbSDimitry Andric #include "llvm/Support/Error.h" 20bdd1243dSDimitry Andric #include "llvm/Support/FormatVariadic.h" 218bcb0991SDimitry Andric #include "llvm/Support/LEB128.h" 22*5f757f3fSDimitry Andric #include "llvm/Support/SystemZ/zOSSupport.h" 238bcb0991SDimitry Andric #include "llvm/Support/YAMLTraits.h" 248bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h" 258bcb0991SDimitry Andric 268bcb0991SDimitry Andric #include "llvm/Support/Format.h" 278bcb0991SDimitry Andric 288bcb0991SDimitry Andric using namespace llvm; 298bcb0991SDimitry Andric 308bcb0991SDimitry Andric namespace { 318bcb0991SDimitry Andric 328bcb0991SDimitry Andric class MachOWriter { 338bcb0991SDimitry Andric public: 34e8d8bef9SDimitry Andric MachOWriter(MachOYAML::Object &Obj) : Obj(Obj), fileStart(0) { 358bcb0991SDimitry Andric is64Bit = Obj.Header.magic == MachO::MH_MAGIC_64 || 368bcb0991SDimitry Andric Obj.Header.magic == MachO::MH_CIGAM_64; 378bcb0991SDimitry Andric memset(reinterpret_cast<void *>(&Header), 0, sizeof(MachO::mach_header_64)); 388bcb0991SDimitry Andric } 398bcb0991SDimitry Andric 405ffd83dbSDimitry Andric Error writeMachO(raw_ostream &OS); 418bcb0991SDimitry Andric 428bcb0991SDimitry Andric private: 438bcb0991SDimitry Andric void writeHeader(raw_ostream &OS); 448bcb0991SDimitry Andric void writeLoadCommands(raw_ostream &OS); 455ffd83dbSDimitry Andric Error writeSectionData(raw_ostream &OS); 465ffd83dbSDimitry Andric void writeRelocations(raw_ostream &OS); 478bcb0991SDimitry Andric void writeLinkEditData(raw_ostream &OS); 488bcb0991SDimitry Andric 498bcb0991SDimitry Andric void writeBindOpcodes(raw_ostream &OS, 508bcb0991SDimitry Andric std::vector<MachOYAML::BindOpcode> &BindOpcodes); 518bcb0991SDimitry Andric // LinkEdit writers 528bcb0991SDimitry Andric void writeRebaseOpcodes(raw_ostream &OS); 538bcb0991SDimitry Andric void writeBasicBindOpcodes(raw_ostream &OS); 548bcb0991SDimitry Andric void writeWeakBindOpcodes(raw_ostream &OS); 558bcb0991SDimitry Andric void writeLazyBindOpcodes(raw_ostream &OS); 568bcb0991SDimitry Andric void writeNameList(raw_ostream &OS); 578bcb0991SDimitry Andric void writeStringTable(raw_ostream &OS); 588bcb0991SDimitry Andric void writeExportTrie(raw_ostream &OS); 594824e7fdSDimitry Andric void writeDynamicSymbolTable(raw_ostream &OS); 6081ad6265SDimitry Andric void writeFunctionStarts(raw_ostream &OS); 61bdd1243dSDimitry Andric void writeChainedFixups(raw_ostream &OS); 62bdd1243dSDimitry Andric void writeDyldExportsTrie(raw_ostream &OS); 63bdd1243dSDimitry Andric void writeDataInCode(raw_ostream &OS); 648bcb0991SDimitry Andric 658bcb0991SDimitry Andric void dumpExportEntry(raw_ostream &OS, MachOYAML::ExportEntry &Entry); 668bcb0991SDimitry Andric void ZeroToOffset(raw_ostream &OS, size_t offset); 678bcb0991SDimitry Andric 688bcb0991SDimitry Andric MachOYAML::Object &Obj; 698bcb0991SDimitry Andric bool is64Bit; 708bcb0991SDimitry Andric uint64_t fileStart; 718bcb0991SDimitry Andric MachO::mach_header_64 Header; 725ffd83dbSDimitry Andric 735ffd83dbSDimitry Andric // Old PPC Object Files didn't have __LINKEDIT segments, the data was just 745ffd83dbSDimitry Andric // stuck at the end of the file. 755ffd83dbSDimitry Andric bool FoundLinkEditSeg = false; 768bcb0991SDimitry Andric }; 778bcb0991SDimitry Andric 785ffd83dbSDimitry Andric Error MachOWriter::writeMachO(raw_ostream &OS) { 798bcb0991SDimitry Andric fileStart = OS.tell(); 808bcb0991SDimitry Andric writeHeader(OS); 818bcb0991SDimitry Andric writeLoadCommands(OS); 825ffd83dbSDimitry Andric if (Error Err = writeSectionData(OS)) 835ffd83dbSDimitry Andric return Err; 845ffd83dbSDimitry Andric writeRelocations(OS); 855ffd83dbSDimitry Andric if (!FoundLinkEditSeg) 865ffd83dbSDimitry Andric writeLinkEditData(OS); 875ffd83dbSDimitry Andric return Error::success(); 888bcb0991SDimitry Andric } 898bcb0991SDimitry Andric 908bcb0991SDimitry Andric void MachOWriter::writeHeader(raw_ostream &OS) { 918bcb0991SDimitry Andric Header.magic = Obj.Header.magic; 928bcb0991SDimitry Andric Header.cputype = Obj.Header.cputype; 938bcb0991SDimitry Andric Header.cpusubtype = Obj.Header.cpusubtype; 948bcb0991SDimitry Andric Header.filetype = Obj.Header.filetype; 958bcb0991SDimitry Andric Header.ncmds = Obj.Header.ncmds; 968bcb0991SDimitry Andric Header.sizeofcmds = Obj.Header.sizeofcmds; 978bcb0991SDimitry Andric Header.flags = Obj.Header.flags; 988bcb0991SDimitry Andric Header.reserved = Obj.Header.reserved; 998bcb0991SDimitry Andric 1008bcb0991SDimitry Andric if (Obj.IsLittleEndian != sys::IsLittleEndianHost) 1018bcb0991SDimitry Andric MachO::swapStruct(Header); 1028bcb0991SDimitry Andric 1038bcb0991SDimitry Andric auto header_size = 1048bcb0991SDimitry Andric is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header); 1058bcb0991SDimitry Andric OS.write((const char *)&Header, header_size); 1068bcb0991SDimitry Andric } 1078bcb0991SDimitry Andric 1088bcb0991SDimitry Andric template <typename SectionType> 10906c3fb27SDimitry Andric SectionType constructSection(const MachOYAML::Section &Sec) { 1108bcb0991SDimitry Andric SectionType TempSec; 1118bcb0991SDimitry Andric memcpy(reinterpret_cast<void *>(&TempSec.sectname[0]), &Sec.sectname[0], 16); 1128bcb0991SDimitry Andric memcpy(reinterpret_cast<void *>(&TempSec.segname[0]), &Sec.segname[0], 16); 1138bcb0991SDimitry Andric TempSec.addr = Sec.addr; 1148bcb0991SDimitry Andric TempSec.size = Sec.size; 1158bcb0991SDimitry Andric TempSec.offset = Sec.offset; 1168bcb0991SDimitry Andric TempSec.align = Sec.align; 1178bcb0991SDimitry Andric TempSec.reloff = Sec.reloff; 1188bcb0991SDimitry Andric TempSec.nreloc = Sec.nreloc; 1198bcb0991SDimitry Andric TempSec.flags = Sec.flags; 1208bcb0991SDimitry Andric TempSec.reserved1 = Sec.reserved1; 1218bcb0991SDimitry Andric TempSec.reserved2 = Sec.reserved2; 1228bcb0991SDimitry Andric return TempSec; 1238bcb0991SDimitry Andric } 1248bcb0991SDimitry Andric 1258bcb0991SDimitry Andric template <typename StructType> 1268bcb0991SDimitry Andric size_t writeLoadCommandData(MachOYAML::LoadCommand &LC, raw_ostream &OS, 1278bcb0991SDimitry Andric bool IsLittleEndian) { 1288bcb0991SDimitry Andric return 0; 1298bcb0991SDimitry Andric } 1308bcb0991SDimitry Andric 1318bcb0991SDimitry Andric template <> 1328bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::segment_command>(MachOYAML::LoadCommand &LC, 1338bcb0991SDimitry Andric raw_ostream &OS, 1348bcb0991SDimitry Andric bool IsLittleEndian) { 1358bcb0991SDimitry Andric size_t BytesWritten = 0; 1368bcb0991SDimitry Andric for (const auto &Sec : LC.Sections) { 1378bcb0991SDimitry Andric auto TempSec = constructSection<MachO::section>(Sec); 1388bcb0991SDimitry Andric if (IsLittleEndian != sys::IsLittleEndianHost) 1398bcb0991SDimitry Andric MachO::swapStruct(TempSec); 1408bcb0991SDimitry Andric OS.write(reinterpret_cast<const char *>(&(TempSec)), 1418bcb0991SDimitry Andric sizeof(MachO::section)); 1428bcb0991SDimitry Andric BytesWritten += sizeof(MachO::section); 1438bcb0991SDimitry Andric } 1448bcb0991SDimitry Andric return BytesWritten; 1458bcb0991SDimitry Andric } 1468bcb0991SDimitry Andric 1478bcb0991SDimitry Andric template <> 1488bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::segment_command_64>( 1498bcb0991SDimitry Andric MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) { 1508bcb0991SDimitry Andric size_t BytesWritten = 0; 1518bcb0991SDimitry Andric for (const auto &Sec : LC.Sections) { 1528bcb0991SDimitry Andric auto TempSec = constructSection<MachO::section_64>(Sec); 1538bcb0991SDimitry Andric TempSec.reserved3 = Sec.reserved3; 1548bcb0991SDimitry Andric if (IsLittleEndian != sys::IsLittleEndianHost) 1558bcb0991SDimitry Andric MachO::swapStruct(TempSec); 1568bcb0991SDimitry Andric OS.write(reinterpret_cast<const char *>(&(TempSec)), 1578bcb0991SDimitry Andric sizeof(MachO::section_64)); 1588bcb0991SDimitry Andric BytesWritten += sizeof(MachO::section_64); 1598bcb0991SDimitry Andric } 1608bcb0991SDimitry Andric return BytesWritten; 1618bcb0991SDimitry Andric } 1628bcb0991SDimitry Andric 1638bcb0991SDimitry Andric size_t writePayloadString(MachOYAML::LoadCommand &LC, raw_ostream &OS) { 1648bcb0991SDimitry Andric size_t BytesWritten = 0; 165fe6060f1SDimitry Andric if (!LC.Content.empty()) { 166fe6060f1SDimitry Andric OS.write(LC.Content.c_str(), LC.Content.length()); 167fe6060f1SDimitry Andric BytesWritten = LC.Content.length(); 1688bcb0991SDimitry Andric } 1698bcb0991SDimitry Andric return BytesWritten; 1708bcb0991SDimitry Andric } 1718bcb0991SDimitry Andric 1728bcb0991SDimitry Andric template <> 1738bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::dylib_command>(MachOYAML::LoadCommand &LC, 1748bcb0991SDimitry Andric raw_ostream &OS, 1758bcb0991SDimitry Andric bool IsLittleEndian) { 1768bcb0991SDimitry Andric return writePayloadString(LC, OS); 1778bcb0991SDimitry Andric } 1788bcb0991SDimitry Andric 1798bcb0991SDimitry Andric template <> 1808bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::dylinker_command>(MachOYAML::LoadCommand &LC, 1818bcb0991SDimitry Andric raw_ostream &OS, 1828bcb0991SDimitry Andric bool IsLittleEndian) { 1838bcb0991SDimitry Andric return writePayloadString(LC, OS); 1848bcb0991SDimitry Andric } 1858bcb0991SDimitry Andric 1868bcb0991SDimitry Andric template <> 1878bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::rpath_command>(MachOYAML::LoadCommand &LC, 1888bcb0991SDimitry Andric raw_ostream &OS, 1898bcb0991SDimitry Andric bool IsLittleEndian) { 1908bcb0991SDimitry Andric return writePayloadString(LC, OS); 1918bcb0991SDimitry Andric } 1928bcb0991SDimitry Andric 1938bcb0991SDimitry Andric template <> 194349cc55cSDimitry Andric size_t writeLoadCommandData<MachO::sub_framework_command>( 195349cc55cSDimitry Andric MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) { 196349cc55cSDimitry Andric return writePayloadString(LC, OS); 197349cc55cSDimitry Andric } 198349cc55cSDimitry Andric 199349cc55cSDimitry Andric template <> 200349cc55cSDimitry Andric size_t writeLoadCommandData<MachO::sub_umbrella_command>( 201349cc55cSDimitry Andric MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) { 202349cc55cSDimitry Andric return writePayloadString(LC, OS); 203349cc55cSDimitry Andric } 204349cc55cSDimitry Andric 205349cc55cSDimitry Andric template <> 206349cc55cSDimitry Andric size_t writeLoadCommandData<MachO::sub_client_command>( 207349cc55cSDimitry Andric MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) { 208349cc55cSDimitry Andric return writePayloadString(LC, OS); 209349cc55cSDimitry Andric } 210349cc55cSDimitry Andric 211349cc55cSDimitry Andric template <> 212349cc55cSDimitry Andric size_t writeLoadCommandData<MachO::sub_library_command>( 213349cc55cSDimitry Andric MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) { 214349cc55cSDimitry Andric return writePayloadString(LC, OS); 215349cc55cSDimitry Andric } 216349cc55cSDimitry Andric 217349cc55cSDimitry Andric template <> 2188bcb0991SDimitry Andric size_t writeLoadCommandData<MachO::build_version_command>( 2198bcb0991SDimitry Andric MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) { 2208bcb0991SDimitry Andric size_t BytesWritten = 0; 2218bcb0991SDimitry Andric for (const auto &T : LC.Tools) { 2228bcb0991SDimitry Andric struct MachO::build_tool_version tool = T; 2238bcb0991SDimitry Andric if (IsLittleEndian != sys::IsLittleEndianHost) 2248bcb0991SDimitry Andric MachO::swapStruct(tool); 2258bcb0991SDimitry Andric OS.write(reinterpret_cast<const char *>(&tool), 2268bcb0991SDimitry Andric sizeof(MachO::build_tool_version)); 2278bcb0991SDimitry Andric BytesWritten += sizeof(MachO::build_tool_version); 2288bcb0991SDimitry Andric } 2298bcb0991SDimitry Andric return BytesWritten; 2308bcb0991SDimitry Andric } 2318bcb0991SDimitry Andric 2328bcb0991SDimitry Andric void ZeroFillBytes(raw_ostream &OS, size_t Size) { 233e8d8bef9SDimitry Andric std::vector<uint8_t> FillData(Size, 0); 2348bcb0991SDimitry Andric OS.write(reinterpret_cast<char *>(FillData.data()), Size); 2358bcb0991SDimitry Andric } 2368bcb0991SDimitry Andric 2378bcb0991SDimitry Andric void Fill(raw_ostream &OS, size_t Size, uint32_t Data) { 238e8d8bef9SDimitry Andric std::vector<uint32_t> FillData((Size / 4) + 1, Data); 2398bcb0991SDimitry Andric OS.write(reinterpret_cast<char *>(FillData.data()), Size); 2408bcb0991SDimitry Andric } 2418bcb0991SDimitry Andric 2428bcb0991SDimitry Andric void MachOWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) { 2438bcb0991SDimitry Andric auto currOffset = OS.tell() - fileStart; 2448bcb0991SDimitry Andric if (currOffset < Offset) 2458bcb0991SDimitry Andric ZeroFillBytes(OS, Offset - currOffset); 2468bcb0991SDimitry Andric } 2478bcb0991SDimitry Andric 2488bcb0991SDimitry Andric void MachOWriter::writeLoadCommands(raw_ostream &OS) { 2498bcb0991SDimitry Andric for (auto &LC : Obj.LoadCommands) { 2508bcb0991SDimitry Andric size_t BytesWritten = 0; 2518bcb0991SDimitry Andric llvm::MachO::macho_load_command Data = LC.Data; 2528bcb0991SDimitry Andric 2538bcb0991SDimitry Andric #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \ 2548bcb0991SDimitry Andric case MachO::LCName: \ 2558bcb0991SDimitry Andric if (Obj.IsLittleEndian != sys::IsLittleEndianHost) \ 2568bcb0991SDimitry Andric MachO::swapStruct(Data.LCStruct##_data); \ 2578bcb0991SDimitry Andric OS.write(reinterpret_cast<const char *>(&(Data.LCStruct##_data)), \ 2588bcb0991SDimitry Andric sizeof(MachO::LCStruct)); \ 2598bcb0991SDimitry Andric BytesWritten = sizeof(MachO::LCStruct); \ 2608bcb0991SDimitry Andric BytesWritten += \ 2618bcb0991SDimitry Andric writeLoadCommandData<MachO::LCStruct>(LC, OS, Obj.IsLittleEndian); \ 2628bcb0991SDimitry Andric break; 2638bcb0991SDimitry Andric 2648bcb0991SDimitry Andric switch (LC.Data.load_command_data.cmd) { 2658bcb0991SDimitry Andric default: 2668bcb0991SDimitry Andric if (Obj.IsLittleEndian != sys::IsLittleEndianHost) 2678bcb0991SDimitry Andric MachO::swapStruct(Data.load_command_data); 2688bcb0991SDimitry Andric OS.write(reinterpret_cast<const char *>(&(Data.load_command_data)), 2698bcb0991SDimitry Andric sizeof(MachO::load_command)); 2708bcb0991SDimitry Andric BytesWritten = sizeof(MachO::load_command); 2718bcb0991SDimitry Andric BytesWritten += 2728bcb0991SDimitry Andric writeLoadCommandData<MachO::load_command>(LC, OS, Obj.IsLittleEndian); 2738bcb0991SDimitry Andric break; 2748bcb0991SDimitry Andric #include "llvm/BinaryFormat/MachO.def" 2758bcb0991SDimitry Andric } 2768bcb0991SDimitry Andric 2778bcb0991SDimitry Andric if (LC.PayloadBytes.size() > 0) { 2788bcb0991SDimitry Andric OS.write(reinterpret_cast<const char *>(LC.PayloadBytes.data()), 2798bcb0991SDimitry Andric LC.PayloadBytes.size()); 2808bcb0991SDimitry Andric BytesWritten += LC.PayloadBytes.size(); 2818bcb0991SDimitry Andric } 2828bcb0991SDimitry Andric 2838bcb0991SDimitry Andric if (LC.ZeroPadBytes > 0) { 2848bcb0991SDimitry Andric ZeroFillBytes(OS, LC.ZeroPadBytes); 2858bcb0991SDimitry Andric BytesWritten += LC.ZeroPadBytes; 2868bcb0991SDimitry Andric } 2878bcb0991SDimitry Andric 2888bcb0991SDimitry Andric // Fill remaining bytes with 0. This will only get hit in partially 2898bcb0991SDimitry Andric // specified test cases. 2908bcb0991SDimitry Andric auto BytesRemaining = LC.Data.load_command_data.cmdsize - BytesWritten; 2918bcb0991SDimitry Andric if (BytesRemaining > 0) { 2928bcb0991SDimitry Andric ZeroFillBytes(OS, BytesRemaining); 2938bcb0991SDimitry Andric } 2948bcb0991SDimitry Andric } 2958bcb0991SDimitry Andric } 2968bcb0991SDimitry Andric 2975ffd83dbSDimitry Andric Error MachOWriter::writeSectionData(raw_ostream &OS) { 298349cc55cSDimitry Andric uint64_t LinkEditOff = 0; 2998bcb0991SDimitry Andric for (auto &LC : Obj.LoadCommands) { 3008bcb0991SDimitry Andric switch (LC.Data.load_command_data.cmd) { 3018bcb0991SDimitry Andric case MachO::LC_SEGMENT: 3028bcb0991SDimitry Andric case MachO::LC_SEGMENT_64: 3038bcb0991SDimitry Andric uint64_t segOff = is64Bit ? LC.Data.segment_command_64_data.fileoff 3048bcb0991SDimitry Andric : LC.Data.segment_command_data.fileoff; 3058bcb0991SDimitry Andric if (0 == 3068bcb0991SDimitry Andric strncmp(&LC.Data.segment_command_data.segname[0], "__LINKEDIT", 16)) { 3078bcb0991SDimitry Andric FoundLinkEditSeg = true; 308349cc55cSDimitry Andric LinkEditOff = segOff; 309349cc55cSDimitry Andric if (Obj.RawLinkEditSegment) 310349cc55cSDimitry Andric continue; 3118bcb0991SDimitry Andric writeLinkEditData(OS); 3128bcb0991SDimitry Andric } 3138bcb0991SDimitry Andric for (auto &Sec : LC.Sections) { 3148bcb0991SDimitry Andric ZeroToOffset(OS, Sec.offset); 3158bcb0991SDimitry Andric // Zero Fill any data between the end of the last thing we wrote and the 3168bcb0991SDimitry Andric // start of this section. 3175ffd83dbSDimitry Andric if (OS.tell() - fileStart > Sec.offset && Sec.offset != (uint32_t)0) 3185ffd83dbSDimitry Andric return createStringError( 3195ffd83dbSDimitry Andric errc::invalid_argument, 320bdd1243dSDimitry Andric llvm::formatv( 321bdd1243dSDimitry Andric "wrote too much data somewhere, section offsets in " 322bdd1243dSDimitry Andric "section {0} for segment {1} don't line up: " 323bdd1243dSDimitry Andric "[cursor={2:x}], [fileStart={3:x}], [sectionOffset={4:x}]", 324bdd1243dSDimitry Andric Sec.sectname, Sec.segname, OS.tell(), fileStart, 325bdd1243dSDimitry Andric Sec.offset.value)); 3265ffd83dbSDimitry Andric 327e8d8bef9SDimitry Andric StringRef SectName(Sec.sectname, 328e8d8bef9SDimitry Andric strnlen(Sec.sectname, sizeof(Sec.sectname))); 329e8d8bef9SDimitry Andric // If the section's content is specified in the 'DWARF' entry, we will 330e8d8bef9SDimitry Andric // emit it regardless of the section's segname. 331e8d8bef9SDimitry Andric if (Obj.DWARF.getNonEmptySectionNames().count(SectName.substr(2))) { 332e8d8bef9SDimitry Andric if (Sec.content) 333e8d8bef9SDimitry Andric return createStringError(errc::invalid_argument, 334e8d8bef9SDimitry Andric "cannot specify section '" + SectName + 335e8d8bef9SDimitry Andric "' contents in the 'DWARF' entry and " 336e8d8bef9SDimitry Andric "the 'content' at the same time"); 337e8d8bef9SDimitry Andric auto EmitFunc = DWARFYAML::getDWARFEmitterByName(SectName.substr(2)); 338e8d8bef9SDimitry Andric if (Error Err = EmitFunc(OS, Obj.DWARF)) 3395ffd83dbSDimitry Andric return Err; 3408bcb0991SDimitry Andric continue; 3418bcb0991SDimitry Andric } 3428bcb0991SDimitry Andric 3438bcb0991SDimitry Andric // Skip if it's a virtual section. 3448bcb0991SDimitry Andric if (MachO::isVirtualSection(Sec.flags & MachO::SECTION_TYPE)) 3458bcb0991SDimitry Andric continue; 3468bcb0991SDimitry Andric 3478bcb0991SDimitry Andric if (Sec.content) { 3488bcb0991SDimitry Andric yaml::BinaryRef Content = *Sec.content; 3498bcb0991SDimitry Andric Content.writeAsBinary(OS); 3508bcb0991SDimitry Andric ZeroFillBytes(OS, Sec.size - Content.binary_size()); 3518bcb0991SDimitry Andric } else { 3528bcb0991SDimitry Andric // Fill section data with 0xDEADBEEF. 3538bcb0991SDimitry Andric Fill(OS, Sec.size, 0xDEADBEEFu); 3548bcb0991SDimitry Andric } 3558bcb0991SDimitry Andric } 3568bcb0991SDimitry Andric uint64_t segSize = is64Bit ? LC.Data.segment_command_64_data.filesize 3578bcb0991SDimitry Andric : LC.Data.segment_command_data.filesize; 3588bcb0991SDimitry Andric ZeroToOffset(OS, segOff + segSize); 3598bcb0991SDimitry Andric break; 3608bcb0991SDimitry Andric } 3618bcb0991SDimitry Andric } 3625ffd83dbSDimitry Andric 363349cc55cSDimitry Andric if (Obj.RawLinkEditSegment) { 364349cc55cSDimitry Andric ZeroToOffset(OS, LinkEditOff); 365349cc55cSDimitry Andric if (OS.tell() - fileStart > LinkEditOff || !LinkEditOff) 366349cc55cSDimitry Andric return createStringError(errc::invalid_argument, 367349cc55cSDimitry Andric "section offsets don't line up"); 368349cc55cSDimitry Andric Obj.RawLinkEditSegment->writeAsBinary(OS); 369349cc55cSDimitry Andric } 3705ffd83dbSDimitry Andric return Error::success(); 3715ffd83dbSDimitry Andric } 3725ffd83dbSDimitry Andric 3735ffd83dbSDimitry Andric // The implementation of makeRelocationInfo and makeScatteredRelocationInfo is 3745ffd83dbSDimitry Andric // consistent with how libObject parses MachO binary files. For the reference 3755ffd83dbSDimitry Andric // see getStruct, getRelocation, getPlainRelocationPCRel, 3765ffd83dbSDimitry Andric // getPlainRelocationLength and related methods in MachOObjectFile.cpp 3775ffd83dbSDimitry Andric static MachO::any_relocation_info 3785ffd83dbSDimitry Andric makeRelocationInfo(const MachOYAML::Relocation &R, bool IsLE) { 3795ffd83dbSDimitry Andric assert(!R.is_scattered && "non-scattered relocation expected"); 3805ffd83dbSDimitry Andric MachO::any_relocation_info MRE; 3815ffd83dbSDimitry Andric MRE.r_word0 = R.address; 3825ffd83dbSDimitry Andric if (IsLE) 3835ffd83dbSDimitry Andric MRE.r_word1 = ((unsigned)R.symbolnum << 0) | ((unsigned)R.is_pcrel << 24) | 3845ffd83dbSDimitry Andric ((unsigned)R.length << 25) | ((unsigned)R.is_extern << 27) | 3855ffd83dbSDimitry Andric ((unsigned)R.type << 28); 3865ffd83dbSDimitry Andric else 3875ffd83dbSDimitry Andric MRE.r_word1 = ((unsigned)R.symbolnum << 8) | ((unsigned)R.is_pcrel << 7) | 3885ffd83dbSDimitry Andric ((unsigned)R.length << 5) | ((unsigned)R.is_extern << 4) | 3895ffd83dbSDimitry Andric ((unsigned)R.type << 0); 3905ffd83dbSDimitry Andric return MRE; 3915ffd83dbSDimitry Andric } 3925ffd83dbSDimitry Andric 3935ffd83dbSDimitry Andric static MachO::any_relocation_info 3945ffd83dbSDimitry Andric makeScatteredRelocationInfo(const MachOYAML::Relocation &R) { 3955ffd83dbSDimitry Andric assert(R.is_scattered && "scattered relocation expected"); 3965ffd83dbSDimitry Andric MachO::any_relocation_info MRE; 3975ffd83dbSDimitry Andric MRE.r_word0 = (((unsigned)R.address << 0) | ((unsigned)R.type << 24) | 3985ffd83dbSDimitry Andric ((unsigned)R.length << 28) | ((unsigned)R.is_pcrel << 30) | 3995ffd83dbSDimitry Andric MachO::R_SCATTERED); 4005ffd83dbSDimitry Andric MRE.r_word1 = R.value; 4015ffd83dbSDimitry Andric return MRE; 4025ffd83dbSDimitry Andric } 4035ffd83dbSDimitry Andric 4045ffd83dbSDimitry Andric void MachOWriter::writeRelocations(raw_ostream &OS) { 4055ffd83dbSDimitry Andric for (const MachOYAML::LoadCommand &LC : Obj.LoadCommands) { 4065ffd83dbSDimitry Andric switch (LC.Data.load_command_data.cmd) { 4075ffd83dbSDimitry Andric case MachO::LC_SEGMENT: 4085ffd83dbSDimitry Andric case MachO::LC_SEGMENT_64: 4095ffd83dbSDimitry Andric for (const MachOYAML::Section &Sec : LC.Sections) { 4105ffd83dbSDimitry Andric if (Sec.relocations.empty()) 4115ffd83dbSDimitry Andric continue; 4125ffd83dbSDimitry Andric ZeroToOffset(OS, Sec.reloff); 4135ffd83dbSDimitry Andric for (const MachOYAML::Relocation &R : Sec.relocations) { 4145ffd83dbSDimitry Andric MachO::any_relocation_info MRE = 4155ffd83dbSDimitry Andric R.is_scattered ? makeScatteredRelocationInfo(R) 4165ffd83dbSDimitry Andric : makeRelocationInfo(R, Obj.IsLittleEndian); 4175ffd83dbSDimitry Andric if (Obj.IsLittleEndian != sys::IsLittleEndianHost) 4185ffd83dbSDimitry Andric MachO::swapStruct(MRE); 4195ffd83dbSDimitry Andric OS.write(reinterpret_cast<const char *>(&MRE), 4205ffd83dbSDimitry Andric sizeof(MachO::any_relocation_info)); 4215ffd83dbSDimitry Andric } 4225ffd83dbSDimitry Andric } 4235ffd83dbSDimitry Andric } 4245ffd83dbSDimitry Andric } 4258bcb0991SDimitry Andric } 4268bcb0991SDimitry Andric 4278bcb0991SDimitry Andric void MachOWriter::writeBindOpcodes( 4288bcb0991SDimitry Andric raw_ostream &OS, std::vector<MachOYAML::BindOpcode> &BindOpcodes) { 4298bcb0991SDimitry Andric 430*5f757f3fSDimitry Andric for (const auto &Opcode : BindOpcodes) { 4318bcb0991SDimitry Andric uint8_t OpByte = Opcode.Opcode | Opcode.Imm; 4328bcb0991SDimitry Andric OS.write(reinterpret_cast<char *>(&OpByte), 1); 4338bcb0991SDimitry Andric for (auto Data : Opcode.ULEBExtraData) { 4348bcb0991SDimitry Andric encodeULEB128(Data, OS); 4358bcb0991SDimitry Andric } 4368bcb0991SDimitry Andric for (auto Data : Opcode.SLEBExtraData) { 4378bcb0991SDimitry Andric encodeSLEB128(Data, OS); 4388bcb0991SDimitry Andric } 4398bcb0991SDimitry Andric if (!Opcode.Symbol.empty()) { 4408bcb0991SDimitry Andric OS.write(Opcode.Symbol.data(), Opcode.Symbol.size()); 4418bcb0991SDimitry Andric OS.write('\0'); 4428bcb0991SDimitry Andric } 4438bcb0991SDimitry Andric } 4448bcb0991SDimitry Andric } 4458bcb0991SDimitry Andric 4468bcb0991SDimitry Andric void MachOWriter::dumpExportEntry(raw_ostream &OS, 4478bcb0991SDimitry Andric MachOYAML::ExportEntry &Entry) { 448bdd1243dSDimitry Andric encodeULEB128(Entry.TerminalSize, OS); 4498bcb0991SDimitry Andric if (Entry.TerminalSize > 0) { 450bdd1243dSDimitry Andric encodeULEB128(Entry.Flags, OS); 4518bcb0991SDimitry Andric if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) { 452bdd1243dSDimitry Andric encodeULEB128(Entry.Other, OS); 4538bcb0991SDimitry Andric OS << Entry.ImportName; 4548bcb0991SDimitry Andric OS.write('\0'); 4558bcb0991SDimitry Andric } else { 456bdd1243dSDimitry Andric encodeULEB128(Entry.Address, OS); 4578bcb0991SDimitry Andric if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) 458bdd1243dSDimitry Andric encodeULEB128(Entry.Other, OS); 4598bcb0991SDimitry Andric } 4608bcb0991SDimitry Andric } 4618bcb0991SDimitry Andric OS.write(static_cast<uint8_t>(Entry.Children.size())); 462*5f757f3fSDimitry Andric for (const auto &EE : Entry.Children) { 4638bcb0991SDimitry Andric OS << EE.Name; 4648bcb0991SDimitry Andric OS.write('\0'); 465bdd1243dSDimitry Andric encodeULEB128(EE.NodeOffset, OS); 4668bcb0991SDimitry Andric } 4678bcb0991SDimitry Andric for (auto EE : Entry.Children) 4688bcb0991SDimitry Andric dumpExportEntry(OS, EE); 4698bcb0991SDimitry Andric } 4708bcb0991SDimitry Andric 4718bcb0991SDimitry Andric void MachOWriter::writeExportTrie(raw_ostream &OS) { 4728bcb0991SDimitry Andric dumpExportEntry(OS, Obj.LinkEdit.ExportTrie); 4738bcb0991SDimitry Andric } 4748bcb0991SDimitry Andric 4758bcb0991SDimitry Andric template <typename NListType> 4768bcb0991SDimitry Andric void writeNListEntry(MachOYAML::NListEntry &NLE, raw_ostream &OS, 4778bcb0991SDimitry Andric bool IsLittleEndian) { 4788bcb0991SDimitry Andric NListType ListEntry; 4798bcb0991SDimitry Andric ListEntry.n_strx = NLE.n_strx; 4808bcb0991SDimitry Andric ListEntry.n_type = NLE.n_type; 4818bcb0991SDimitry Andric ListEntry.n_sect = NLE.n_sect; 4828bcb0991SDimitry Andric ListEntry.n_desc = NLE.n_desc; 4838bcb0991SDimitry Andric ListEntry.n_value = NLE.n_value; 4848bcb0991SDimitry Andric 4858bcb0991SDimitry Andric if (IsLittleEndian != sys::IsLittleEndianHost) 4868bcb0991SDimitry Andric MachO::swapStruct(ListEntry); 4878bcb0991SDimitry Andric OS.write(reinterpret_cast<const char *>(&ListEntry), sizeof(NListType)); 4888bcb0991SDimitry Andric } 4898bcb0991SDimitry Andric 4908bcb0991SDimitry Andric void MachOWriter::writeLinkEditData(raw_ostream &OS) { 4918bcb0991SDimitry Andric typedef void (MachOWriter::*writeHandler)(raw_ostream &); 4928bcb0991SDimitry Andric typedef std::pair<uint64_t, writeHandler> writeOperation; 4938bcb0991SDimitry Andric std::vector<writeOperation> WriteQueue; 4948bcb0991SDimitry Andric 49504eeddc0SDimitry Andric MachO::dyld_info_command *DyldInfoOnlyCmd = nullptr; 49604eeddc0SDimitry Andric MachO::symtab_command *SymtabCmd = nullptr; 49704eeddc0SDimitry Andric MachO::dysymtab_command *DSymtabCmd = nullptr; 49881ad6265SDimitry Andric MachO::linkedit_data_command *FunctionStartsCmd = nullptr; 499bdd1243dSDimitry Andric MachO::linkedit_data_command *ChainedFixupsCmd = nullptr; 500bdd1243dSDimitry Andric MachO::linkedit_data_command *DyldExportsTrieCmd = nullptr; 501bdd1243dSDimitry Andric MachO::linkedit_data_command *DataInCodeCmd = nullptr; 5028bcb0991SDimitry Andric for (auto &LC : Obj.LoadCommands) { 5038bcb0991SDimitry Andric switch (LC.Data.load_command_data.cmd) { 5048bcb0991SDimitry Andric case MachO::LC_SYMTAB: 5058bcb0991SDimitry Andric SymtabCmd = &LC.Data.symtab_command_data; 5068bcb0991SDimitry Andric WriteQueue.push_back( 5078bcb0991SDimitry Andric std::make_pair(SymtabCmd->symoff, &MachOWriter::writeNameList)); 5088bcb0991SDimitry Andric WriteQueue.push_back( 5098bcb0991SDimitry Andric std::make_pair(SymtabCmd->stroff, &MachOWriter::writeStringTable)); 5108bcb0991SDimitry Andric break; 5118bcb0991SDimitry Andric case MachO::LC_DYLD_INFO_ONLY: 5128bcb0991SDimitry Andric DyldInfoOnlyCmd = &LC.Data.dyld_info_command_data; 5138bcb0991SDimitry Andric WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->rebase_off, 5148bcb0991SDimitry Andric &MachOWriter::writeRebaseOpcodes)); 5158bcb0991SDimitry Andric WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->bind_off, 5168bcb0991SDimitry Andric &MachOWriter::writeBasicBindOpcodes)); 5178bcb0991SDimitry Andric WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->weak_bind_off, 5188bcb0991SDimitry Andric &MachOWriter::writeWeakBindOpcodes)); 5198bcb0991SDimitry Andric WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->lazy_bind_off, 5208bcb0991SDimitry Andric &MachOWriter::writeLazyBindOpcodes)); 5218bcb0991SDimitry Andric WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->export_off, 5228bcb0991SDimitry Andric &MachOWriter::writeExportTrie)); 5238bcb0991SDimitry Andric break; 5244824e7fdSDimitry Andric case MachO::LC_DYSYMTAB: 5254824e7fdSDimitry Andric DSymtabCmd = &LC.Data.dysymtab_command_data; 5264824e7fdSDimitry Andric WriteQueue.push_back(std::make_pair( 5274824e7fdSDimitry Andric DSymtabCmd->indirectsymoff, &MachOWriter::writeDynamicSymbolTable)); 5284824e7fdSDimitry Andric break; 52981ad6265SDimitry Andric case MachO::LC_FUNCTION_STARTS: 53081ad6265SDimitry Andric FunctionStartsCmd = &LC.Data.linkedit_data_command_data; 53181ad6265SDimitry Andric WriteQueue.push_back(std::make_pair(FunctionStartsCmd->dataoff, 53281ad6265SDimitry Andric &MachOWriter::writeFunctionStarts)); 53381ad6265SDimitry Andric break; 534bdd1243dSDimitry Andric case MachO::LC_DYLD_CHAINED_FIXUPS: 535bdd1243dSDimitry Andric ChainedFixupsCmd = &LC.Data.linkedit_data_command_data; 536bdd1243dSDimitry Andric WriteQueue.push_back(std::make_pair(ChainedFixupsCmd->dataoff, 537bdd1243dSDimitry Andric &MachOWriter::writeChainedFixups)); 538bdd1243dSDimitry Andric break; 539bdd1243dSDimitry Andric case MachO::LC_DYLD_EXPORTS_TRIE: 540bdd1243dSDimitry Andric DyldExportsTrieCmd = &LC.Data.linkedit_data_command_data; 541bdd1243dSDimitry Andric WriteQueue.push_back(std::make_pair(DyldExportsTrieCmd->dataoff, 542bdd1243dSDimitry Andric &MachOWriter::writeDyldExportsTrie)); 543bdd1243dSDimitry Andric break; 544bdd1243dSDimitry Andric case MachO::LC_DATA_IN_CODE: 545bdd1243dSDimitry Andric DataInCodeCmd = &LC.Data.linkedit_data_command_data; 546bdd1243dSDimitry Andric WriteQueue.push_back(std::make_pair(DataInCodeCmd->dataoff, 547bdd1243dSDimitry Andric &MachOWriter::writeDataInCode)); 548bdd1243dSDimitry Andric break; 5498bcb0991SDimitry Andric } 5508bcb0991SDimitry Andric } 5518bcb0991SDimitry Andric 55281ad6265SDimitry Andric llvm::sort(WriteQueue, llvm::less_first()); 5538bcb0991SDimitry Andric 5548bcb0991SDimitry Andric for (auto writeOp : WriteQueue) { 5558bcb0991SDimitry Andric ZeroToOffset(OS, writeOp.first); 5568bcb0991SDimitry Andric (this->*writeOp.second)(OS); 5578bcb0991SDimitry Andric } 5588bcb0991SDimitry Andric } 5598bcb0991SDimitry Andric 5608bcb0991SDimitry Andric void MachOWriter::writeRebaseOpcodes(raw_ostream &OS) { 5618bcb0991SDimitry Andric MachOYAML::LinkEditData &LinkEdit = Obj.LinkEdit; 5628bcb0991SDimitry Andric 563*5f757f3fSDimitry Andric for (const auto &Opcode : LinkEdit.RebaseOpcodes) { 5648bcb0991SDimitry Andric uint8_t OpByte = Opcode.Opcode | Opcode.Imm; 5658bcb0991SDimitry Andric OS.write(reinterpret_cast<char *>(&OpByte), 1); 5668bcb0991SDimitry Andric for (auto Data : Opcode.ExtraData) 5678bcb0991SDimitry Andric encodeULEB128(Data, OS); 5688bcb0991SDimitry Andric } 5698bcb0991SDimitry Andric } 5708bcb0991SDimitry Andric 5718bcb0991SDimitry Andric void MachOWriter::writeBasicBindOpcodes(raw_ostream &OS) { 5728bcb0991SDimitry Andric writeBindOpcodes(OS, Obj.LinkEdit.BindOpcodes); 5738bcb0991SDimitry Andric } 5748bcb0991SDimitry Andric 5758bcb0991SDimitry Andric void MachOWriter::writeWeakBindOpcodes(raw_ostream &OS) { 5768bcb0991SDimitry Andric writeBindOpcodes(OS, Obj.LinkEdit.WeakBindOpcodes); 5778bcb0991SDimitry Andric } 5788bcb0991SDimitry Andric 5798bcb0991SDimitry Andric void MachOWriter::writeLazyBindOpcodes(raw_ostream &OS) { 5808bcb0991SDimitry Andric writeBindOpcodes(OS, Obj.LinkEdit.LazyBindOpcodes); 5818bcb0991SDimitry Andric } 5828bcb0991SDimitry Andric 5838bcb0991SDimitry Andric void MachOWriter::writeNameList(raw_ostream &OS) { 5848bcb0991SDimitry Andric for (auto NLE : Obj.LinkEdit.NameList) { 5858bcb0991SDimitry Andric if (is64Bit) 5868bcb0991SDimitry Andric writeNListEntry<MachO::nlist_64>(NLE, OS, Obj.IsLittleEndian); 5878bcb0991SDimitry Andric else 5888bcb0991SDimitry Andric writeNListEntry<MachO::nlist>(NLE, OS, Obj.IsLittleEndian); 5898bcb0991SDimitry Andric } 5908bcb0991SDimitry Andric } 5918bcb0991SDimitry Andric 5928bcb0991SDimitry Andric void MachOWriter::writeStringTable(raw_ostream &OS) { 5938bcb0991SDimitry Andric for (auto Str : Obj.LinkEdit.StringTable) { 5948bcb0991SDimitry Andric OS.write(Str.data(), Str.size()); 5958bcb0991SDimitry Andric OS.write('\0'); 5968bcb0991SDimitry Andric } 5978bcb0991SDimitry Andric } 5988bcb0991SDimitry Andric 5994824e7fdSDimitry Andric void MachOWriter::writeDynamicSymbolTable(raw_ostream &OS) { 6004824e7fdSDimitry Andric for (auto Data : Obj.LinkEdit.IndirectSymbols) 6014824e7fdSDimitry Andric OS.write(reinterpret_cast<const char *>(&Data), 6024824e7fdSDimitry Andric sizeof(yaml::Hex32::BaseType)); 6034824e7fdSDimitry Andric } 6044824e7fdSDimitry Andric 60581ad6265SDimitry Andric void MachOWriter::writeFunctionStarts(raw_ostream &OS) { 60681ad6265SDimitry Andric uint64_t Addr = 0; 60781ad6265SDimitry Andric for (uint64_t NextAddr : Obj.LinkEdit.FunctionStarts) { 60881ad6265SDimitry Andric uint64_t Delta = NextAddr - Addr; 60981ad6265SDimitry Andric encodeULEB128(Delta, OS); 61081ad6265SDimitry Andric Addr = NextAddr; 61181ad6265SDimitry Andric } 61281ad6265SDimitry Andric 61381ad6265SDimitry Andric OS.write('\0'); 61481ad6265SDimitry Andric } 61581ad6265SDimitry Andric 616bdd1243dSDimitry Andric void MachOWriter::writeDataInCode(raw_ostream &OS) { 617bdd1243dSDimitry Andric for (const auto &Entry : Obj.LinkEdit.DataInCode) { 618bdd1243dSDimitry Andric MachO::data_in_code_entry DICE{Entry.Offset, Entry.Length, Entry.Kind}; 619bdd1243dSDimitry Andric if (Obj.IsLittleEndian != sys::IsLittleEndianHost) 620bdd1243dSDimitry Andric MachO::swapStruct(DICE); 621bdd1243dSDimitry Andric OS.write(reinterpret_cast<const char *>(&DICE), 622bdd1243dSDimitry Andric sizeof(MachO::data_in_code_entry)); 623bdd1243dSDimitry Andric } 624bdd1243dSDimitry Andric } 625bdd1243dSDimitry Andric 626bdd1243dSDimitry Andric void MachOWriter::writeChainedFixups(raw_ostream &OS) { 627bdd1243dSDimitry Andric if (Obj.LinkEdit.ChainedFixups.size() > 0) 628bdd1243dSDimitry Andric OS.write(reinterpret_cast<const char *>(Obj.LinkEdit.ChainedFixups.data()), 629bdd1243dSDimitry Andric Obj.LinkEdit.ChainedFixups.size()); 630bdd1243dSDimitry Andric } 631bdd1243dSDimitry Andric 632bdd1243dSDimitry Andric void MachOWriter::writeDyldExportsTrie(raw_ostream &OS) { 633bdd1243dSDimitry Andric dumpExportEntry(OS, Obj.LinkEdit.ExportTrie); 634bdd1243dSDimitry Andric } 635bdd1243dSDimitry Andric 6368bcb0991SDimitry Andric class UniversalWriter { 6378bcb0991SDimitry Andric public: 6388bcb0991SDimitry Andric UniversalWriter(yaml::YamlObjectFile &ObjectFile) 6398bcb0991SDimitry Andric : ObjectFile(ObjectFile), fileStart(0) {} 6408bcb0991SDimitry Andric 6415ffd83dbSDimitry Andric Error writeMachO(raw_ostream &OS); 6428bcb0991SDimitry Andric 6438bcb0991SDimitry Andric private: 6448bcb0991SDimitry Andric void writeFatHeader(raw_ostream &OS); 6458bcb0991SDimitry Andric void writeFatArchs(raw_ostream &OS); 6468bcb0991SDimitry Andric 6478bcb0991SDimitry Andric void ZeroToOffset(raw_ostream &OS, size_t offset); 6488bcb0991SDimitry Andric 6498bcb0991SDimitry Andric yaml::YamlObjectFile &ObjectFile; 6508bcb0991SDimitry Andric uint64_t fileStart; 6518bcb0991SDimitry Andric }; 6528bcb0991SDimitry Andric 6535ffd83dbSDimitry Andric Error UniversalWriter::writeMachO(raw_ostream &OS) { 6548bcb0991SDimitry Andric fileStart = OS.tell(); 6558bcb0991SDimitry Andric if (ObjectFile.MachO) { 6568bcb0991SDimitry Andric MachOWriter Writer(*ObjectFile.MachO); 6575ffd83dbSDimitry Andric return Writer.writeMachO(OS); 6588bcb0991SDimitry Andric } 6598bcb0991SDimitry Andric 6608bcb0991SDimitry Andric writeFatHeader(OS); 6618bcb0991SDimitry Andric writeFatArchs(OS); 6628bcb0991SDimitry Andric 6638bcb0991SDimitry Andric auto &FatFile = *ObjectFile.FatMachO; 6645ffd83dbSDimitry Andric if (FatFile.FatArchs.size() < FatFile.Slices.size()) 6655ffd83dbSDimitry Andric return createStringError( 6665ffd83dbSDimitry Andric errc::invalid_argument, 6675ffd83dbSDimitry Andric "cannot write 'Slices' if not described in 'FatArches'"); 6685ffd83dbSDimitry Andric 6698bcb0991SDimitry Andric for (size_t i = 0; i < FatFile.Slices.size(); i++) { 6708bcb0991SDimitry Andric ZeroToOffset(OS, FatFile.FatArchs[i].offset); 6718bcb0991SDimitry Andric MachOWriter Writer(FatFile.Slices[i]); 6725ffd83dbSDimitry Andric if (Error Err = Writer.writeMachO(OS)) 6735ffd83dbSDimitry Andric return Err; 6748bcb0991SDimitry Andric 6758bcb0991SDimitry Andric auto SliceEnd = FatFile.FatArchs[i].offset + FatFile.FatArchs[i].size; 6768bcb0991SDimitry Andric ZeroToOffset(OS, SliceEnd); 6778bcb0991SDimitry Andric } 6785ffd83dbSDimitry Andric 6795ffd83dbSDimitry Andric return Error::success(); 6808bcb0991SDimitry Andric } 6818bcb0991SDimitry Andric 6828bcb0991SDimitry Andric void UniversalWriter::writeFatHeader(raw_ostream &OS) { 6838bcb0991SDimitry Andric auto &FatFile = *ObjectFile.FatMachO; 6848bcb0991SDimitry Andric MachO::fat_header header; 6858bcb0991SDimitry Andric header.magic = FatFile.Header.magic; 6868bcb0991SDimitry Andric header.nfat_arch = FatFile.Header.nfat_arch; 6878bcb0991SDimitry Andric if (sys::IsLittleEndianHost) 6888bcb0991SDimitry Andric swapStruct(header); 6898bcb0991SDimitry Andric OS.write(reinterpret_cast<const char *>(&header), sizeof(MachO::fat_header)); 6908bcb0991SDimitry Andric } 6918bcb0991SDimitry Andric 6928bcb0991SDimitry Andric template <typename FatArchType> 6938bcb0991SDimitry Andric FatArchType constructFatArch(MachOYAML::FatArch &Arch) { 6948bcb0991SDimitry Andric FatArchType FatArch; 6958bcb0991SDimitry Andric FatArch.cputype = Arch.cputype; 6968bcb0991SDimitry Andric FatArch.cpusubtype = Arch.cpusubtype; 6978bcb0991SDimitry Andric FatArch.offset = Arch.offset; 6988bcb0991SDimitry Andric FatArch.size = Arch.size; 6998bcb0991SDimitry Andric FatArch.align = Arch.align; 7008bcb0991SDimitry Andric return FatArch; 7018bcb0991SDimitry Andric } 7028bcb0991SDimitry Andric 7038bcb0991SDimitry Andric template <typename StructType> 7048bcb0991SDimitry Andric void writeFatArch(MachOYAML::FatArch &LC, raw_ostream &OS) {} 7058bcb0991SDimitry Andric 7068bcb0991SDimitry Andric template <> 7078bcb0991SDimitry Andric void writeFatArch<MachO::fat_arch>(MachOYAML::FatArch &Arch, raw_ostream &OS) { 7088bcb0991SDimitry Andric auto FatArch = constructFatArch<MachO::fat_arch>(Arch); 7098bcb0991SDimitry Andric if (sys::IsLittleEndianHost) 7108bcb0991SDimitry Andric swapStruct(FatArch); 7118bcb0991SDimitry Andric OS.write(reinterpret_cast<const char *>(&FatArch), sizeof(MachO::fat_arch)); 7128bcb0991SDimitry Andric } 7138bcb0991SDimitry Andric 7148bcb0991SDimitry Andric template <> 7158bcb0991SDimitry Andric void writeFatArch<MachO::fat_arch_64>(MachOYAML::FatArch &Arch, 7168bcb0991SDimitry Andric raw_ostream &OS) { 7178bcb0991SDimitry Andric auto FatArch = constructFatArch<MachO::fat_arch_64>(Arch); 7188bcb0991SDimitry Andric FatArch.reserved = Arch.reserved; 7198bcb0991SDimitry Andric if (sys::IsLittleEndianHost) 7208bcb0991SDimitry Andric swapStruct(FatArch); 7218bcb0991SDimitry Andric OS.write(reinterpret_cast<const char *>(&FatArch), 7228bcb0991SDimitry Andric sizeof(MachO::fat_arch_64)); 7238bcb0991SDimitry Andric } 7248bcb0991SDimitry Andric 7258bcb0991SDimitry Andric void UniversalWriter::writeFatArchs(raw_ostream &OS) { 7268bcb0991SDimitry Andric auto &FatFile = *ObjectFile.FatMachO; 7278bcb0991SDimitry Andric bool is64Bit = FatFile.Header.magic == MachO::FAT_MAGIC_64; 7288bcb0991SDimitry Andric for (auto Arch : FatFile.FatArchs) { 7298bcb0991SDimitry Andric if (is64Bit) 7308bcb0991SDimitry Andric writeFatArch<MachO::fat_arch_64>(Arch, OS); 7318bcb0991SDimitry Andric else 7328bcb0991SDimitry Andric writeFatArch<MachO::fat_arch>(Arch, OS); 7338bcb0991SDimitry Andric } 7348bcb0991SDimitry Andric } 7358bcb0991SDimitry Andric 7368bcb0991SDimitry Andric void UniversalWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) { 7378bcb0991SDimitry Andric auto currOffset = OS.tell() - fileStart; 7388bcb0991SDimitry Andric if (currOffset < Offset) 7398bcb0991SDimitry Andric ZeroFillBytes(OS, Offset - currOffset); 7408bcb0991SDimitry Andric } 7418bcb0991SDimitry Andric 7428bcb0991SDimitry Andric } // end anonymous namespace 7438bcb0991SDimitry Andric 7448bcb0991SDimitry Andric namespace llvm { 7458bcb0991SDimitry Andric namespace yaml { 7468bcb0991SDimitry Andric 7475ffd83dbSDimitry Andric bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out, ErrorHandler EH) { 7488bcb0991SDimitry Andric UniversalWriter Writer(Doc); 7495ffd83dbSDimitry Andric if (Error Err = Writer.writeMachO(Out)) { 7505ffd83dbSDimitry Andric handleAllErrors(std::move(Err), 7515ffd83dbSDimitry Andric [&](const ErrorInfoBase &Err) { EH(Err.message()); }); 7525ffd83dbSDimitry Andric return false; 7535ffd83dbSDimitry Andric } 7548bcb0991SDimitry Andric return true; 7558bcb0991SDimitry Andric } 7568bcb0991SDimitry Andric 7578bcb0991SDimitry Andric } // namespace yaml 7588bcb0991SDimitry Andric } // namespace llvm 759