1 //===- yaml2macho - Convert YAML to a Mach 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 Mach component of yaml2obj. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/BinaryFormat/MachO.h" 15 #include "llvm/ObjectYAML/DWARFEmitter.h" 16 #include "llvm/ObjectYAML/ObjectYAML.h" 17 #include "llvm/ObjectYAML/yaml2obj.h" 18 #include "llvm/Support/Errc.h" 19 #include "llvm/Support/Error.h" 20 #include "llvm/Support/LEB128.h" 21 #include "llvm/Support/YAMLTraits.h" 22 #include "llvm/Support/raw_ostream.h" 23 24 #include "llvm/Support/Format.h" 25 26 using namespace llvm; 27 28 namespace { 29 30 class MachOWriter { 31 public: 32 MachOWriter(MachOYAML::Object &Obj) : Obj(Obj), fileStart(0) { 33 is64Bit = Obj.Header.magic == MachO::MH_MAGIC_64 || 34 Obj.Header.magic == MachO::MH_CIGAM_64; 35 memset(reinterpret_cast<void *>(&Header), 0, sizeof(MachO::mach_header_64)); 36 } 37 38 Error writeMachO(raw_ostream &OS); 39 40 private: 41 void writeHeader(raw_ostream &OS); 42 void writeLoadCommands(raw_ostream &OS); 43 Error writeSectionData(raw_ostream &OS); 44 void writeRelocations(raw_ostream &OS); 45 void writeLinkEditData(raw_ostream &OS); 46 47 void writeBindOpcodes(raw_ostream &OS, 48 std::vector<MachOYAML::BindOpcode> &BindOpcodes); 49 // LinkEdit writers 50 void writeRebaseOpcodes(raw_ostream &OS); 51 void writeBasicBindOpcodes(raw_ostream &OS); 52 void writeWeakBindOpcodes(raw_ostream &OS); 53 void writeLazyBindOpcodes(raw_ostream &OS); 54 void writeNameList(raw_ostream &OS); 55 void writeStringTable(raw_ostream &OS); 56 void writeExportTrie(raw_ostream &OS); 57 58 void dumpExportEntry(raw_ostream &OS, MachOYAML::ExportEntry &Entry); 59 void ZeroToOffset(raw_ostream &OS, size_t offset); 60 61 MachOYAML::Object &Obj; 62 bool is64Bit; 63 uint64_t fileStart; 64 MachO::mach_header_64 Header; 65 66 // Old PPC Object Files didn't have __LINKEDIT segments, the data was just 67 // stuck at the end of the file. 68 bool FoundLinkEditSeg = false; 69 }; 70 71 Error MachOWriter::writeMachO(raw_ostream &OS) { 72 fileStart = OS.tell(); 73 writeHeader(OS); 74 writeLoadCommands(OS); 75 if (Error Err = writeSectionData(OS)) 76 return Err; 77 writeRelocations(OS); 78 if (!FoundLinkEditSeg) 79 writeLinkEditData(OS); 80 return Error::success(); 81 } 82 83 void MachOWriter::writeHeader(raw_ostream &OS) { 84 Header.magic = Obj.Header.magic; 85 Header.cputype = Obj.Header.cputype; 86 Header.cpusubtype = Obj.Header.cpusubtype; 87 Header.filetype = Obj.Header.filetype; 88 Header.ncmds = Obj.Header.ncmds; 89 Header.sizeofcmds = Obj.Header.sizeofcmds; 90 Header.flags = Obj.Header.flags; 91 Header.reserved = Obj.Header.reserved; 92 93 if (Obj.IsLittleEndian != sys::IsLittleEndianHost) 94 MachO::swapStruct(Header); 95 96 auto header_size = 97 is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header); 98 OS.write((const char *)&Header, header_size); 99 } 100 101 template <typename SectionType> 102 SectionType constructSection(MachOYAML::Section Sec) { 103 SectionType TempSec; 104 memcpy(reinterpret_cast<void *>(&TempSec.sectname[0]), &Sec.sectname[0], 16); 105 memcpy(reinterpret_cast<void *>(&TempSec.segname[0]), &Sec.segname[0], 16); 106 TempSec.addr = Sec.addr; 107 TempSec.size = Sec.size; 108 TempSec.offset = Sec.offset; 109 TempSec.align = Sec.align; 110 TempSec.reloff = Sec.reloff; 111 TempSec.nreloc = Sec.nreloc; 112 TempSec.flags = Sec.flags; 113 TempSec.reserved1 = Sec.reserved1; 114 TempSec.reserved2 = Sec.reserved2; 115 return TempSec; 116 } 117 118 template <typename StructType> 119 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC, raw_ostream &OS, 120 bool IsLittleEndian) { 121 return 0; 122 } 123 124 template <> 125 size_t writeLoadCommandData<MachO::segment_command>(MachOYAML::LoadCommand &LC, 126 raw_ostream &OS, 127 bool IsLittleEndian) { 128 size_t BytesWritten = 0; 129 for (const auto &Sec : LC.Sections) { 130 auto TempSec = constructSection<MachO::section>(Sec); 131 if (IsLittleEndian != sys::IsLittleEndianHost) 132 MachO::swapStruct(TempSec); 133 OS.write(reinterpret_cast<const char *>(&(TempSec)), 134 sizeof(MachO::section)); 135 BytesWritten += sizeof(MachO::section); 136 } 137 return BytesWritten; 138 } 139 140 template <> 141 size_t writeLoadCommandData<MachO::segment_command_64>( 142 MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) { 143 size_t BytesWritten = 0; 144 for (const auto &Sec : LC.Sections) { 145 auto TempSec = constructSection<MachO::section_64>(Sec); 146 TempSec.reserved3 = Sec.reserved3; 147 if (IsLittleEndian != sys::IsLittleEndianHost) 148 MachO::swapStruct(TempSec); 149 OS.write(reinterpret_cast<const char *>(&(TempSec)), 150 sizeof(MachO::section_64)); 151 BytesWritten += sizeof(MachO::section_64); 152 } 153 return BytesWritten; 154 } 155 156 size_t writePayloadString(MachOYAML::LoadCommand &LC, raw_ostream &OS) { 157 size_t BytesWritten = 0; 158 if (!LC.Content.empty()) { 159 OS.write(LC.Content.c_str(), LC.Content.length()); 160 BytesWritten = LC.Content.length(); 161 } 162 return BytesWritten; 163 } 164 165 template <> 166 size_t writeLoadCommandData<MachO::dylib_command>(MachOYAML::LoadCommand &LC, 167 raw_ostream &OS, 168 bool IsLittleEndian) { 169 return writePayloadString(LC, OS); 170 } 171 172 template <> 173 size_t writeLoadCommandData<MachO::dylinker_command>(MachOYAML::LoadCommand &LC, 174 raw_ostream &OS, 175 bool IsLittleEndian) { 176 return writePayloadString(LC, OS); 177 } 178 179 template <> 180 size_t writeLoadCommandData<MachO::rpath_command>(MachOYAML::LoadCommand &LC, 181 raw_ostream &OS, 182 bool IsLittleEndian) { 183 return writePayloadString(LC, OS); 184 } 185 186 template <> 187 size_t writeLoadCommandData<MachO::build_version_command>( 188 MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) { 189 size_t BytesWritten = 0; 190 for (const auto &T : LC.Tools) { 191 struct MachO::build_tool_version tool = T; 192 if (IsLittleEndian != sys::IsLittleEndianHost) 193 MachO::swapStruct(tool); 194 OS.write(reinterpret_cast<const char *>(&tool), 195 sizeof(MachO::build_tool_version)); 196 BytesWritten += sizeof(MachO::build_tool_version); 197 } 198 return BytesWritten; 199 } 200 201 void ZeroFillBytes(raw_ostream &OS, size_t Size) { 202 std::vector<uint8_t> FillData(Size, 0); 203 OS.write(reinterpret_cast<char *>(FillData.data()), Size); 204 } 205 206 void Fill(raw_ostream &OS, size_t Size, uint32_t Data) { 207 std::vector<uint32_t> FillData((Size / 4) + 1, Data); 208 OS.write(reinterpret_cast<char *>(FillData.data()), Size); 209 } 210 211 void MachOWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) { 212 auto currOffset = OS.tell() - fileStart; 213 if (currOffset < Offset) 214 ZeroFillBytes(OS, Offset - currOffset); 215 } 216 217 void MachOWriter::writeLoadCommands(raw_ostream &OS) { 218 for (auto &LC : Obj.LoadCommands) { 219 size_t BytesWritten = 0; 220 llvm::MachO::macho_load_command Data = LC.Data; 221 222 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \ 223 case MachO::LCName: \ 224 if (Obj.IsLittleEndian != sys::IsLittleEndianHost) \ 225 MachO::swapStruct(Data.LCStruct##_data); \ 226 OS.write(reinterpret_cast<const char *>(&(Data.LCStruct##_data)), \ 227 sizeof(MachO::LCStruct)); \ 228 BytesWritten = sizeof(MachO::LCStruct); \ 229 BytesWritten += \ 230 writeLoadCommandData<MachO::LCStruct>(LC, OS, Obj.IsLittleEndian); \ 231 break; 232 233 switch (LC.Data.load_command_data.cmd) { 234 default: 235 if (Obj.IsLittleEndian != sys::IsLittleEndianHost) 236 MachO::swapStruct(Data.load_command_data); 237 OS.write(reinterpret_cast<const char *>(&(Data.load_command_data)), 238 sizeof(MachO::load_command)); 239 BytesWritten = sizeof(MachO::load_command); 240 BytesWritten += 241 writeLoadCommandData<MachO::load_command>(LC, OS, Obj.IsLittleEndian); 242 break; 243 #include "llvm/BinaryFormat/MachO.def" 244 } 245 246 if (LC.PayloadBytes.size() > 0) { 247 OS.write(reinterpret_cast<const char *>(LC.PayloadBytes.data()), 248 LC.PayloadBytes.size()); 249 BytesWritten += LC.PayloadBytes.size(); 250 } 251 252 if (LC.ZeroPadBytes > 0) { 253 ZeroFillBytes(OS, LC.ZeroPadBytes); 254 BytesWritten += LC.ZeroPadBytes; 255 } 256 257 // Fill remaining bytes with 0. This will only get hit in partially 258 // specified test cases. 259 auto BytesRemaining = LC.Data.load_command_data.cmdsize - BytesWritten; 260 if (BytesRemaining > 0) { 261 ZeroFillBytes(OS, BytesRemaining); 262 } 263 } 264 } 265 266 Error MachOWriter::writeSectionData(raw_ostream &OS) { 267 for (auto &LC : Obj.LoadCommands) { 268 switch (LC.Data.load_command_data.cmd) { 269 case MachO::LC_SEGMENT: 270 case MachO::LC_SEGMENT_64: 271 uint64_t segOff = is64Bit ? LC.Data.segment_command_64_data.fileoff 272 : LC.Data.segment_command_data.fileoff; 273 if (0 == 274 strncmp(&LC.Data.segment_command_data.segname[0], "__LINKEDIT", 16)) { 275 FoundLinkEditSeg = true; 276 writeLinkEditData(OS); 277 } 278 for (auto &Sec : LC.Sections) { 279 ZeroToOffset(OS, Sec.offset); 280 // Zero Fill any data between the end of the last thing we wrote and the 281 // start of this section. 282 if (OS.tell() - fileStart > Sec.offset && Sec.offset != (uint32_t)0) 283 return createStringError( 284 errc::invalid_argument, 285 "wrote too much data somewhere, section offsets don't line up"); 286 287 StringRef SectName(Sec.sectname, 288 strnlen(Sec.sectname, sizeof(Sec.sectname))); 289 // If the section's content is specified in the 'DWARF' entry, we will 290 // emit it regardless of the section's segname. 291 if (Obj.DWARF.getNonEmptySectionNames().count(SectName.substr(2))) { 292 if (Sec.content) 293 return createStringError(errc::invalid_argument, 294 "cannot specify section '" + SectName + 295 "' contents in the 'DWARF' entry and " 296 "the 'content' at the same time"); 297 auto EmitFunc = DWARFYAML::getDWARFEmitterByName(SectName.substr(2)); 298 if (Error Err = EmitFunc(OS, Obj.DWARF)) 299 return Err; 300 continue; 301 } 302 303 // Skip if it's a virtual section. 304 if (MachO::isVirtualSection(Sec.flags & MachO::SECTION_TYPE)) 305 continue; 306 307 if (Sec.content) { 308 yaml::BinaryRef Content = *Sec.content; 309 Content.writeAsBinary(OS); 310 ZeroFillBytes(OS, Sec.size - Content.binary_size()); 311 } else { 312 // Fill section data with 0xDEADBEEF. 313 Fill(OS, Sec.size, 0xDEADBEEFu); 314 } 315 } 316 uint64_t segSize = is64Bit ? LC.Data.segment_command_64_data.filesize 317 : LC.Data.segment_command_data.filesize; 318 ZeroToOffset(OS, segOff + segSize); 319 break; 320 } 321 } 322 323 return Error::success(); 324 } 325 326 // The implementation of makeRelocationInfo and makeScatteredRelocationInfo is 327 // consistent with how libObject parses MachO binary files. For the reference 328 // see getStruct, getRelocation, getPlainRelocationPCRel, 329 // getPlainRelocationLength and related methods in MachOObjectFile.cpp 330 static MachO::any_relocation_info 331 makeRelocationInfo(const MachOYAML::Relocation &R, bool IsLE) { 332 assert(!R.is_scattered && "non-scattered relocation expected"); 333 MachO::any_relocation_info MRE; 334 MRE.r_word0 = R.address; 335 if (IsLE) 336 MRE.r_word1 = ((unsigned)R.symbolnum << 0) | ((unsigned)R.is_pcrel << 24) | 337 ((unsigned)R.length << 25) | ((unsigned)R.is_extern << 27) | 338 ((unsigned)R.type << 28); 339 else 340 MRE.r_word1 = ((unsigned)R.symbolnum << 8) | ((unsigned)R.is_pcrel << 7) | 341 ((unsigned)R.length << 5) | ((unsigned)R.is_extern << 4) | 342 ((unsigned)R.type << 0); 343 return MRE; 344 } 345 346 static MachO::any_relocation_info 347 makeScatteredRelocationInfo(const MachOYAML::Relocation &R) { 348 assert(R.is_scattered && "scattered relocation expected"); 349 MachO::any_relocation_info MRE; 350 MRE.r_word0 = (((unsigned)R.address << 0) | ((unsigned)R.type << 24) | 351 ((unsigned)R.length << 28) | ((unsigned)R.is_pcrel << 30) | 352 MachO::R_SCATTERED); 353 MRE.r_word1 = R.value; 354 return MRE; 355 } 356 357 void MachOWriter::writeRelocations(raw_ostream &OS) { 358 for (const MachOYAML::LoadCommand &LC : Obj.LoadCommands) { 359 switch (LC.Data.load_command_data.cmd) { 360 case MachO::LC_SEGMENT: 361 case MachO::LC_SEGMENT_64: 362 for (const MachOYAML::Section &Sec : LC.Sections) { 363 if (Sec.relocations.empty()) 364 continue; 365 ZeroToOffset(OS, Sec.reloff); 366 for (const MachOYAML::Relocation &R : Sec.relocations) { 367 MachO::any_relocation_info MRE = 368 R.is_scattered ? makeScatteredRelocationInfo(R) 369 : makeRelocationInfo(R, Obj.IsLittleEndian); 370 if (Obj.IsLittleEndian != sys::IsLittleEndianHost) 371 MachO::swapStruct(MRE); 372 OS.write(reinterpret_cast<const char *>(&MRE), 373 sizeof(MachO::any_relocation_info)); 374 } 375 } 376 } 377 } 378 } 379 380 void MachOWriter::writeBindOpcodes( 381 raw_ostream &OS, std::vector<MachOYAML::BindOpcode> &BindOpcodes) { 382 383 for (auto Opcode : BindOpcodes) { 384 uint8_t OpByte = Opcode.Opcode | Opcode.Imm; 385 OS.write(reinterpret_cast<char *>(&OpByte), 1); 386 for (auto Data : Opcode.ULEBExtraData) { 387 encodeULEB128(Data, OS); 388 } 389 for (auto Data : Opcode.SLEBExtraData) { 390 encodeSLEB128(Data, OS); 391 } 392 if (!Opcode.Symbol.empty()) { 393 OS.write(Opcode.Symbol.data(), Opcode.Symbol.size()); 394 OS.write('\0'); 395 } 396 } 397 } 398 399 void MachOWriter::dumpExportEntry(raw_ostream &OS, 400 MachOYAML::ExportEntry &Entry) { 401 encodeSLEB128(Entry.TerminalSize, OS); 402 if (Entry.TerminalSize > 0) { 403 encodeSLEB128(Entry.Flags, OS); 404 if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) { 405 encodeSLEB128(Entry.Other, OS); 406 OS << Entry.ImportName; 407 OS.write('\0'); 408 } else { 409 encodeSLEB128(Entry.Address, OS); 410 if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) 411 encodeSLEB128(Entry.Other, OS); 412 } 413 } 414 OS.write(static_cast<uint8_t>(Entry.Children.size())); 415 for (auto EE : Entry.Children) { 416 OS << EE.Name; 417 OS.write('\0'); 418 encodeSLEB128(EE.NodeOffset, OS); 419 } 420 for (auto EE : Entry.Children) 421 dumpExportEntry(OS, EE); 422 } 423 424 void MachOWriter::writeExportTrie(raw_ostream &OS) { 425 dumpExportEntry(OS, Obj.LinkEdit.ExportTrie); 426 } 427 428 template <typename NListType> 429 void writeNListEntry(MachOYAML::NListEntry &NLE, raw_ostream &OS, 430 bool IsLittleEndian) { 431 NListType ListEntry; 432 ListEntry.n_strx = NLE.n_strx; 433 ListEntry.n_type = NLE.n_type; 434 ListEntry.n_sect = NLE.n_sect; 435 ListEntry.n_desc = NLE.n_desc; 436 ListEntry.n_value = NLE.n_value; 437 438 if (IsLittleEndian != sys::IsLittleEndianHost) 439 MachO::swapStruct(ListEntry); 440 OS.write(reinterpret_cast<const char *>(&ListEntry), sizeof(NListType)); 441 } 442 443 void MachOWriter::writeLinkEditData(raw_ostream &OS) { 444 typedef void (MachOWriter::*writeHandler)(raw_ostream &); 445 typedef std::pair<uint64_t, writeHandler> writeOperation; 446 std::vector<writeOperation> WriteQueue; 447 448 MachO::dyld_info_command *DyldInfoOnlyCmd = 0; 449 MachO::symtab_command *SymtabCmd = 0; 450 for (auto &LC : Obj.LoadCommands) { 451 switch (LC.Data.load_command_data.cmd) { 452 case MachO::LC_SYMTAB: 453 SymtabCmd = &LC.Data.symtab_command_data; 454 WriteQueue.push_back( 455 std::make_pair(SymtabCmd->symoff, &MachOWriter::writeNameList)); 456 WriteQueue.push_back( 457 std::make_pair(SymtabCmd->stroff, &MachOWriter::writeStringTable)); 458 break; 459 case MachO::LC_DYLD_INFO_ONLY: 460 DyldInfoOnlyCmd = &LC.Data.dyld_info_command_data; 461 WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->rebase_off, 462 &MachOWriter::writeRebaseOpcodes)); 463 WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->bind_off, 464 &MachOWriter::writeBasicBindOpcodes)); 465 WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->weak_bind_off, 466 &MachOWriter::writeWeakBindOpcodes)); 467 WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->lazy_bind_off, 468 &MachOWriter::writeLazyBindOpcodes)); 469 WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->export_off, 470 &MachOWriter::writeExportTrie)); 471 break; 472 } 473 } 474 475 llvm::sort(WriteQueue, [](const writeOperation &a, const writeOperation &b) { 476 return a.first < b.first; 477 }); 478 479 for (auto writeOp : WriteQueue) { 480 ZeroToOffset(OS, writeOp.first); 481 (this->*writeOp.second)(OS); 482 } 483 } 484 485 void MachOWriter::writeRebaseOpcodes(raw_ostream &OS) { 486 MachOYAML::LinkEditData &LinkEdit = Obj.LinkEdit; 487 488 for (auto Opcode : LinkEdit.RebaseOpcodes) { 489 uint8_t OpByte = Opcode.Opcode | Opcode.Imm; 490 OS.write(reinterpret_cast<char *>(&OpByte), 1); 491 for (auto Data : Opcode.ExtraData) 492 encodeULEB128(Data, OS); 493 } 494 } 495 496 void MachOWriter::writeBasicBindOpcodes(raw_ostream &OS) { 497 writeBindOpcodes(OS, Obj.LinkEdit.BindOpcodes); 498 } 499 500 void MachOWriter::writeWeakBindOpcodes(raw_ostream &OS) { 501 writeBindOpcodes(OS, Obj.LinkEdit.WeakBindOpcodes); 502 } 503 504 void MachOWriter::writeLazyBindOpcodes(raw_ostream &OS) { 505 writeBindOpcodes(OS, Obj.LinkEdit.LazyBindOpcodes); 506 } 507 508 void MachOWriter::writeNameList(raw_ostream &OS) { 509 for (auto NLE : Obj.LinkEdit.NameList) { 510 if (is64Bit) 511 writeNListEntry<MachO::nlist_64>(NLE, OS, Obj.IsLittleEndian); 512 else 513 writeNListEntry<MachO::nlist>(NLE, OS, Obj.IsLittleEndian); 514 } 515 } 516 517 void MachOWriter::writeStringTable(raw_ostream &OS) { 518 for (auto Str : Obj.LinkEdit.StringTable) { 519 OS.write(Str.data(), Str.size()); 520 OS.write('\0'); 521 } 522 } 523 524 class UniversalWriter { 525 public: 526 UniversalWriter(yaml::YamlObjectFile &ObjectFile) 527 : ObjectFile(ObjectFile), fileStart(0) {} 528 529 Error writeMachO(raw_ostream &OS); 530 531 private: 532 void writeFatHeader(raw_ostream &OS); 533 void writeFatArchs(raw_ostream &OS); 534 535 void ZeroToOffset(raw_ostream &OS, size_t offset); 536 537 yaml::YamlObjectFile &ObjectFile; 538 uint64_t fileStart; 539 }; 540 541 Error UniversalWriter::writeMachO(raw_ostream &OS) { 542 fileStart = OS.tell(); 543 if (ObjectFile.MachO) { 544 MachOWriter Writer(*ObjectFile.MachO); 545 return Writer.writeMachO(OS); 546 } 547 548 writeFatHeader(OS); 549 writeFatArchs(OS); 550 551 auto &FatFile = *ObjectFile.FatMachO; 552 if (FatFile.FatArchs.size() < FatFile.Slices.size()) 553 return createStringError( 554 errc::invalid_argument, 555 "cannot write 'Slices' if not described in 'FatArches'"); 556 557 for (size_t i = 0; i < FatFile.Slices.size(); i++) { 558 ZeroToOffset(OS, FatFile.FatArchs[i].offset); 559 MachOWriter Writer(FatFile.Slices[i]); 560 if (Error Err = Writer.writeMachO(OS)) 561 return Err; 562 563 auto SliceEnd = FatFile.FatArchs[i].offset + FatFile.FatArchs[i].size; 564 ZeroToOffset(OS, SliceEnd); 565 } 566 567 return Error::success(); 568 } 569 570 void UniversalWriter::writeFatHeader(raw_ostream &OS) { 571 auto &FatFile = *ObjectFile.FatMachO; 572 MachO::fat_header header; 573 header.magic = FatFile.Header.magic; 574 header.nfat_arch = FatFile.Header.nfat_arch; 575 if (sys::IsLittleEndianHost) 576 swapStruct(header); 577 OS.write(reinterpret_cast<const char *>(&header), sizeof(MachO::fat_header)); 578 } 579 580 template <typename FatArchType> 581 FatArchType constructFatArch(MachOYAML::FatArch &Arch) { 582 FatArchType FatArch; 583 FatArch.cputype = Arch.cputype; 584 FatArch.cpusubtype = Arch.cpusubtype; 585 FatArch.offset = Arch.offset; 586 FatArch.size = Arch.size; 587 FatArch.align = Arch.align; 588 return FatArch; 589 } 590 591 template <typename StructType> 592 void writeFatArch(MachOYAML::FatArch &LC, raw_ostream &OS) {} 593 594 template <> 595 void writeFatArch<MachO::fat_arch>(MachOYAML::FatArch &Arch, raw_ostream &OS) { 596 auto FatArch = constructFatArch<MachO::fat_arch>(Arch); 597 if (sys::IsLittleEndianHost) 598 swapStruct(FatArch); 599 OS.write(reinterpret_cast<const char *>(&FatArch), sizeof(MachO::fat_arch)); 600 } 601 602 template <> 603 void writeFatArch<MachO::fat_arch_64>(MachOYAML::FatArch &Arch, 604 raw_ostream &OS) { 605 auto FatArch = constructFatArch<MachO::fat_arch_64>(Arch); 606 FatArch.reserved = Arch.reserved; 607 if (sys::IsLittleEndianHost) 608 swapStruct(FatArch); 609 OS.write(reinterpret_cast<const char *>(&FatArch), 610 sizeof(MachO::fat_arch_64)); 611 } 612 613 void UniversalWriter::writeFatArchs(raw_ostream &OS) { 614 auto &FatFile = *ObjectFile.FatMachO; 615 bool is64Bit = FatFile.Header.magic == MachO::FAT_MAGIC_64; 616 for (auto Arch : FatFile.FatArchs) { 617 if (is64Bit) 618 writeFatArch<MachO::fat_arch_64>(Arch, OS); 619 else 620 writeFatArch<MachO::fat_arch>(Arch, OS); 621 } 622 } 623 624 void UniversalWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) { 625 auto currOffset = OS.tell() - fileStart; 626 if (currOffset < Offset) 627 ZeroFillBytes(OS, Offset - currOffset); 628 } 629 630 } // end anonymous namespace 631 632 namespace llvm { 633 namespace yaml { 634 635 bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out, ErrorHandler EH) { 636 UniversalWriter Writer(Doc); 637 if (Error Err = Writer.writeMachO(Out)) { 638 handleAllErrors(std::move(Err), 639 [&](const ErrorInfoBase &Err) { EH(Err.message()); }); 640 return false; 641 } 642 return true; 643 } 644 645 } // namespace yaml 646 } // namespace llvm 647