LLVM 19.0.0git
MachOEmitter.cpp
Go to the documentation of this file.
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
18#include "llvm/Support/Errc.h"
19#include "llvm/Support/Error.h"
21#include "llvm/Support/LEB128.h"
25
26#include "llvm/Support/Format.h"
27
28using namespace llvm;
29
30namespace {
31
32class MachOWriter {
33public:
34 MachOWriter(MachOYAML::Object &Obj) : Obj(Obj), fileStart(0) {
37 memset(reinterpret_cast<void *>(&Header), 0, sizeof(MachO::mach_header_64));
38 }
39
40 Error writeMachO(raw_ostream &OS);
41
42private:
43 void writeHeader(raw_ostream &OS);
44 void writeLoadCommands(raw_ostream &OS);
45 Error writeSectionData(raw_ostream &OS);
46 void writeRelocations(raw_ostream &OS);
47 void writeLinkEditData(raw_ostream &OS);
48
49 void writeBindOpcodes(raw_ostream &OS,
50 std::vector<MachOYAML::BindOpcode> &BindOpcodes);
51 // LinkEdit writers
52 void writeRebaseOpcodes(raw_ostream &OS);
53 void writeBasicBindOpcodes(raw_ostream &OS);
54 void writeWeakBindOpcodes(raw_ostream &OS);
55 void writeLazyBindOpcodes(raw_ostream &OS);
56 void writeNameList(raw_ostream &OS);
57 void writeStringTable(raw_ostream &OS);
58 void writeExportTrie(raw_ostream &OS);
59 void writeDynamicSymbolTable(raw_ostream &OS);
60 void writeFunctionStarts(raw_ostream &OS);
61 void writeChainedFixups(raw_ostream &OS);
62 void writeDyldExportsTrie(raw_ostream &OS);
63 void writeDataInCode(raw_ostream &OS);
64
65 void dumpExportEntry(raw_ostream &OS, MachOYAML::ExportEntry &Entry);
66 void ZeroToOffset(raw_ostream &OS, size_t offset);
67
69 bool is64Bit;
70 uint64_t fileStart;
72
73 // Old PPC Object Files didn't have __LINKEDIT segments, the data was just
74 // stuck at the end of the file.
75 bool FoundLinkEditSeg = false;
76};
77
78Error MachOWriter::writeMachO(raw_ostream &OS) {
79 fileStart = OS.tell();
80 writeHeader(OS);
81 writeLoadCommands(OS);
82 if (Error Err = writeSectionData(OS))
83 return Err;
84 writeRelocations(OS);
85 if (!FoundLinkEditSeg)
86 writeLinkEditData(OS);
87 return Error::success();
88}
89
90void MachOWriter::writeHeader(raw_ostream &OS) {
91 Header.magic = Obj.Header.magic;
92 Header.cputype = Obj.Header.cputype;
93 Header.cpusubtype = Obj.Header.cpusubtype;
94 Header.filetype = Obj.Header.filetype;
95 Header.ncmds = Obj.Header.ncmds;
96 Header.sizeofcmds = Obj.Header.sizeofcmds;
97 Header.flags = Obj.Header.flags;
98 Header.reserved = Obj.Header.reserved;
99
101 MachO::swapStruct(Header);
102
103 auto header_size =
105 OS.write((const char *)&Header, header_size);
106}
107
108template <typename SectionType>
110 SectionType TempSec;
111 memcpy(reinterpret_cast<void *>(&TempSec.sectname[0]), &Sec.sectname[0], 16);
112 memcpy(reinterpret_cast<void *>(&TempSec.segname[0]), &Sec.segname[0], 16);
113 TempSec.addr = Sec.addr;
114 TempSec.size = Sec.size;
115 TempSec.offset = Sec.offset;
116 TempSec.align = Sec.align;
117 TempSec.reloff = Sec.reloff;
118 TempSec.nreloc = Sec.nreloc;
119 TempSec.flags = Sec.flags;
120 TempSec.reserved1 = Sec.reserved1;
121 TempSec.reserved2 = Sec.reserved2;
122 return TempSec;
123}
124
125template <typename StructType>
126size_t writeLoadCommandData(MachOYAML::LoadCommand &LC, raw_ostream &OS,
127 bool IsLittleEndian) {
128 return 0;
129}
130
131template <>
132size_t writeLoadCommandData<MachO::segment_command>(MachOYAML::LoadCommand &LC,
134 bool IsLittleEndian) {
135 size_t BytesWritten = 0;
136 for (const auto &Sec : LC.Sections) {
137 auto TempSec = constructSection<MachO::section>(Sec);
138 if (IsLittleEndian != sys::IsLittleEndianHost)
139 MachO::swapStruct(TempSec);
140 OS.write(reinterpret_cast<const char *>(&(TempSec)),
141 sizeof(MachO::section));
142 BytesWritten += sizeof(MachO::section);
143 }
144 return BytesWritten;
145}
146
147template <>
148size_t writeLoadCommandData<MachO::segment_command_64>(
149 MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) {
150 size_t BytesWritten = 0;
151 for (const auto &Sec : LC.Sections) {
152 auto TempSec = constructSection<MachO::section_64>(Sec);
153 TempSec.reserved3 = Sec.reserved3;
154 if (IsLittleEndian != sys::IsLittleEndianHost)
155 MachO::swapStruct(TempSec);
156 OS.write(reinterpret_cast<const char *>(&(TempSec)),
157 sizeof(MachO::section_64));
158 BytesWritten += sizeof(MachO::section_64);
159 }
160 return BytesWritten;
161}
162
163size_t writePayloadString(MachOYAML::LoadCommand &LC, raw_ostream &OS) {
164 size_t BytesWritten = 0;
165 if (!LC.Content.empty()) {
166 OS.write(LC.Content.c_str(), LC.Content.length());
167 BytesWritten = LC.Content.length();
168 }
169 return BytesWritten;
170}
171
172template <>
173size_t writeLoadCommandData<MachO::dylib_command>(MachOYAML::LoadCommand &LC,
175 bool IsLittleEndian) {
176 return writePayloadString(LC, OS);
177}
178
179template <>
180size_t writeLoadCommandData<MachO::dylinker_command>(MachOYAML::LoadCommand &LC,
182 bool IsLittleEndian) {
183 return writePayloadString(LC, OS);
184}
185
186template <>
187size_t writeLoadCommandData<MachO::rpath_command>(MachOYAML::LoadCommand &LC,
189 bool IsLittleEndian) {
190 return writePayloadString(LC, OS);
191}
192
193template <>
194size_t writeLoadCommandData<MachO::sub_framework_command>(
195 MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) {
196 return writePayloadString(LC, OS);
197}
198
199template <>
200size_t writeLoadCommandData<MachO::sub_umbrella_command>(
201 MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) {
202 return writePayloadString(LC, OS);
203}
204
205template <>
206size_t writeLoadCommandData<MachO::sub_client_command>(
207 MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) {
208 return writePayloadString(LC, OS);
209}
210
211template <>
212size_t writeLoadCommandData<MachO::sub_library_command>(
213 MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) {
214 return writePayloadString(LC, OS);
215}
216
217template <>
218size_t writeLoadCommandData<MachO::build_version_command>(
219 MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) {
220 size_t BytesWritten = 0;
221 for (const auto &T : LC.Tools) {
223 if (IsLittleEndian != sys::IsLittleEndianHost)
225 OS.write(reinterpret_cast<const char *>(&tool),
227 BytesWritten += sizeof(MachO::build_tool_version);
228 }
229 return BytesWritten;
230}
231
232void ZeroFillBytes(raw_ostream &OS, size_t Size) {
233 std::vector<uint8_t> FillData(Size, 0);
234 OS.write(reinterpret_cast<char *>(FillData.data()), Size);
235}
236
237void Fill(raw_ostream &OS, size_t Size, uint32_t Data) {
238 std::vector<uint32_t> FillData((Size / 4) + 1, Data);
239 OS.write(reinterpret_cast<char *>(FillData.data()), Size);
240}
241
242void MachOWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) {
243 auto currOffset = OS.tell() - fileStart;
244 if (currOffset < Offset)
245 ZeroFillBytes(OS, Offset - currOffset);
246}
247
248void MachOWriter::writeLoadCommands(raw_ostream &OS) {
249 for (auto &LC : Obj.LoadCommands) {
250 size_t BytesWritten = 0;
252
253#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
254 case MachO::LCName: \
255 if (Obj.IsLittleEndian != sys::IsLittleEndianHost) \
256 MachO::swapStruct(Data.LCStruct##_data); \
257 OS.write(reinterpret_cast<const char *>(&(Data.LCStruct##_data)), \
258 sizeof(MachO::LCStruct)); \
259 BytesWritten = sizeof(MachO::LCStruct); \
260 BytesWritten += \
261 writeLoadCommandData<MachO::LCStruct>(LC, OS, Obj.IsLittleEndian); \
262 break;
263
264 switch (LC.Data.load_command_data.cmd) {
265 default:
267 MachO::swapStruct(Data.load_command_data);
268 OS.write(reinterpret_cast<const char *>(&(Data.load_command_data)),
269 sizeof(MachO::load_command));
270 BytesWritten = sizeof(MachO::load_command);
271 BytesWritten +=
272 writeLoadCommandData<MachO::load_command>(LC, OS, Obj.IsLittleEndian);
273 break;
274#include "llvm/BinaryFormat/MachO.def"
275 }
276
277 if (LC.PayloadBytes.size() > 0) {
278 OS.write(reinterpret_cast<const char *>(LC.PayloadBytes.data()),
279 LC.PayloadBytes.size());
280 BytesWritten += LC.PayloadBytes.size();
281 }
282
283 if (LC.ZeroPadBytes > 0) {
285 BytesWritten += LC.ZeroPadBytes;
286 }
287
288 // Fill remaining bytes with 0. This will only get hit in partially
289 // specified test cases.
290 auto BytesRemaining = LC.Data.load_command_data.cmdsize - BytesWritten;
291 if (BytesRemaining > 0) {
292 ZeroFillBytes(OS, BytesRemaining);
293 }
294 }
295}
296
297Error MachOWriter::writeSectionData(raw_ostream &OS) {
298 uint64_t LinkEditOff = 0;
299 for (auto &LC : Obj.LoadCommands) {
300 switch (LC.Data.load_command_data.cmd) {
301 case MachO::LC_SEGMENT:
302 case MachO::LC_SEGMENT_64:
303 uint64_t segOff = is64Bit ? LC.Data.segment_command_64_data.fileoff
304 : LC.Data.segment_command_data.fileoff;
305 if (0 ==
306 strncmp(&LC.Data.segment_command_data.segname[0], "__LINKEDIT", 16)) {
307 FoundLinkEditSeg = true;
308 LinkEditOff = segOff;
309 if (Obj.RawLinkEditSegment)
310 continue;
311 writeLinkEditData(OS);
312 }
313 for (auto &Sec : LC.Sections) {
314 ZeroToOffset(OS, Sec.offset);
315 // Zero Fill any data between the end of the last thing we wrote and the
316 // start of this section.
317 if (OS.tell() - fileStart > Sec.offset && Sec.offset != (uint32_t)0)
318 return createStringError(
321 "wrote too much data somewhere, section offsets in "
322 "section {0} for segment {1} don't line up: "
323 "[cursor={2:x}], [fileStart={3:x}], [sectionOffset={4:x}]",
324 Sec.sectname, Sec.segname, OS.tell(), fileStart,
325 Sec.offset.value));
326
327 StringRef SectName(Sec.sectname,
328 strnlen(Sec.sectname, sizeof(Sec.sectname)));
329 // If the section's content is specified in the 'DWARF' entry, we will
330 // emit it regardless of the section's segname.
331 if (Obj.DWARF.getNonEmptySectionNames().count(SectName.substr(2))) {
332 if (Sec.content)
334 "cannot specify section '" + SectName +
335 "' contents in the 'DWARF' entry and "
336 "the 'content' at the same time");
337 auto EmitFunc = DWARFYAML::getDWARFEmitterByName(SectName.substr(2));
338 if (Error Err = EmitFunc(OS, Obj.DWARF))
339 return Err;
340 continue;
341 }
342
343 // Skip if it's a virtual section.
345 continue;
346
347 if (Sec.content) {
349 Content.writeAsBinary(OS);
350 ZeroFillBytes(OS, Sec.size - Content.binary_size());
351 } else {
352 // Fill section data with 0xDEADBEEF.
353 Fill(OS, Sec.size, 0xDEADBEEFu);
354 }
355 }
356 uint64_t segSize = is64Bit ? LC.Data.segment_command_64_data.filesize
357 : LC.Data.segment_command_data.filesize;
358 ZeroToOffset(OS, segOff + segSize);
359 break;
360 }
361 }
362
363 if (Obj.RawLinkEditSegment) {
364 ZeroToOffset(OS, LinkEditOff);
365 if (OS.tell() - fileStart > LinkEditOff || !LinkEditOff)
367 "section offsets don't line up");
368 Obj.RawLinkEditSegment->writeAsBinary(OS);
369 }
370 return Error::success();
371}
372
373// The implementation of makeRelocationInfo and makeScatteredRelocationInfo is
374// consistent with how libObject parses MachO binary files. For the reference
375// see getStruct, getRelocation, getPlainRelocationPCRel,
376// getPlainRelocationLength and related methods in MachOObjectFile.cpp
378makeRelocationInfo(const MachOYAML::Relocation &R, bool IsLE) {
379 assert(!R.is_scattered && "non-scattered relocation expected");
381 MRE.r_word0 = R.address;
382 if (IsLE)
383 MRE.r_word1 = ((unsigned)R.symbolnum << 0) | ((unsigned)R.is_pcrel << 24) |
384 ((unsigned)R.length << 25) | ((unsigned)R.is_extern << 27) |
385 ((unsigned)R.type << 28);
386 else
387 MRE.r_word1 = ((unsigned)R.symbolnum << 8) | ((unsigned)R.is_pcrel << 7) |
388 ((unsigned)R.length << 5) | ((unsigned)R.is_extern << 4) |
389 ((unsigned)R.type << 0);
390 return MRE;
391}
392
394makeScatteredRelocationInfo(const MachOYAML::Relocation &R) {
395 assert(R.is_scattered && "scattered relocation expected");
397 MRE.r_word0 = (((unsigned)R.address << 0) | ((unsigned)R.type << 24) |
398 ((unsigned)R.length << 28) | ((unsigned)R.is_pcrel << 30) |
400 MRE.r_word1 = R.value;
401 return MRE;
402}
403
404void MachOWriter::writeRelocations(raw_ostream &OS) {
405 for (const MachOYAML::LoadCommand &LC : Obj.LoadCommands) {
406 switch (LC.Data.load_command_data.cmd) {
407 case MachO::LC_SEGMENT:
408 case MachO::LC_SEGMENT_64:
409 for (const MachOYAML::Section &Sec : LC.Sections) {
410 if (Sec.relocations.empty())
411 continue;
412 ZeroToOffset(OS, Sec.reloff);
413 for (const MachOYAML::Relocation &R : Sec.relocations) {
415 R.is_scattered ? makeScatteredRelocationInfo(R)
416 : makeRelocationInfo(R, Obj.IsLittleEndian);
419 OS.write(reinterpret_cast<const char *>(&MRE),
421 }
422 }
423 }
424 }
425}
426
427void MachOWriter::writeBindOpcodes(
428 raw_ostream &OS, std::vector<MachOYAML::BindOpcode> &BindOpcodes) {
429
430 for (const auto &Opcode : BindOpcodes) {
431 uint8_t OpByte = Opcode.Opcode | Opcode.Imm;
432 OS.write(reinterpret_cast<char *>(&OpByte), 1);
433 for (auto Data : Opcode.ULEBExtraData) {
435 }
436 for (auto Data : Opcode.SLEBExtraData) {
438 }
439 if (!Opcode.Symbol.empty()) {
440 OS.write(Opcode.Symbol.data(), Opcode.Symbol.size());
441 OS.write('\0');
442 }
443 }
444}
445
446void MachOWriter::dumpExportEntry(raw_ostream &OS,
447 MachOYAML::ExportEntry &Entry) {
448 encodeULEB128(Entry.TerminalSize, OS);
449 if (Entry.TerminalSize > 0) {
450 encodeULEB128(Entry.Flags, OS);
452 encodeULEB128(Entry.Other, OS);
453 OS << Entry.ImportName;
454 OS.write('\0');
455 } else {
456 encodeULEB128(Entry.Address, OS);
458 encodeULEB128(Entry.Other, OS);
459 }
460 }
461 OS.write(static_cast<uint8_t>(Entry.Children.size()));
462 for (const auto &EE : Entry.Children) {
463 OS << EE.Name;
464 OS.write('\0');
465 encodeULEB128(EE.NodeOffset, OS);
466 }
467 for (auto EE : Entry.Children)
468 dumpExportEntry(OS, EE);
469}
470
471void MachOWriter::writeExportTrie(raw_ostream &OS) {
472 dumpExportEntry(OS, Obj.LinkEdit.ExportTrie);
473}
474
475template <typename NListType>
477 bool IsLittleEndian) {
478 NListType ListEntry;
479 ListEntry.n_strx = NLE.n_strx;
480 ListEntry.n_type = NLE.n_type;
481 ListEntry.n_sect = NLE.n_sect;
482 ListEntry.n_desc = NLE.n_desc;
483 ListEntry.n_value = NLE.n_value;
484
485 if (IsLittleEndian != sys::IsLittleEndianHost)
486 MachO::swapStruct(ListEntry);
487 OS.write(reinterpret_cast<const char *>(&ListEntry), sizeof(NListType));
488}
489
490void MachOWriter::writeLinkEditData(raw_ostream &OS) {
491 typedef void (MachOWriter::*writeHandler)(raw_ostream &);
492 typedef std::pair<uint64_t, writeHandler> writeOperation;
493 std::vector<writeOperation> WriteQueue;
494
495 MachO::dyld_info_command *DyldInfoOnlyCmd = nullptr;
496 MachO::symtab_command *SymtabCmd = nullptr;
497 MachO::dysymtab_command *DSymtabCmd = nullptr;
498 MachO::linkedit_data_command *FunctionStartsCmd = nullptr;
499 MachO::linkedit_data_command *ChainedFixupsCmd = nullptr;
500 MachO::linkedit_data_command *DyldExportsTrieCmd = nullptr;
501 MachO::linkedit_data_command *DataInCodeCmd = nullptr;
502 for (auto &LC : Obj.LoadCommands) {
503 switch (LC.Data.load_command_data.cmd) {
504 case MachO::LC_SYMTAB:
505 SymtabCmd = &LC.Data.symtab_command_data;
506 WriteQueue.push_back(
507 std::make_pair(SymtabCmd->symoff, &MachOWriter::writeNameList));
508 WriteQueue.push_back(
509 std::make_pair(SymtabCmd->stroff, &MachOWriter::writeStringTable));
510 break;
511 case MachO::LC_DYLD_INFO_ONLY:
512 DyldInfoOnlyCmd = &LC.Data.dyld_info_command_data;
513 WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->rebase_off,
514 &MachOWriter::writeRebaseOpcodes));
515 WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->bind_off,
516 &MachOWriter::writeBasicBindOpcodes));
517 WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->weak_bind_off,
518 &MachOWriter::writeWeakBindOpcodes));
519 WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->lazy_bind_off,
520 &MachOWriter::writeLazyBindOpcodes));
521 WriteQueue.push_back(std::make_pair(DyldInfoOnlyCmd->export_off,
522 &MachOWriter::writeExportTrie));
523 break;
524 case MachO::LC_DYSYMTAB:
525 DSymtabCmd = &LC.Data.dysymtab_command_data;
526 WriteQueue.push_back(std::make_pair(
527 DSymtabCmd->indirectsymoff, &MachOWriter::writeDynamicSymbolTable));
528 break;
529 case MachO::LC_FUNCTION_STARTS:
530 FunctionStartsCmd = &LC.Data.linkedit_data_command_data;
531 WriteQueue.push_back(std::make_pair(FunctionStartsCmd->dataoff,
532 &MachOWriter::writeFunctionStarts));
533 break;
534 case MachO::LC_DYLD_CHAINED_FIXUPS:
535 ChainedFixupsCmd = &LC.Data.linkedit_data_command_data;
536 WriteQueue.push_back(std::make_pair(ChainedFixupsCmd->dataoff,
537 &MachOWriter::writeChainedFixups));
538 break;
539 case MachO::LC_DYLD_EXPORTS_TRIE:
540 DyldExportsTrieCmd = &LC.Data.linkedit_data_command_data;
541 WriteQueue.push_back(std::make_pair(DyldExportsTrieCmd->dataoff,
542 &MachOWriter::writeDyldExportsTrie));
543 break;
544 case MachO::LC_DATA_IN_CODE:
545 DataInCodeCmd = &LC.Data.linkedit_data_command_data;
546 WriteQueue.push_back(std::make_pair(DataInCodeCmd->dataoff,
547 &MachOWriter::writeDataInCode));
548 break;
549 }
550 }
551
552 llvm::sort(WriteQueue, llvm::less_first());
553
554 for (auto writeOp : WriteQueue) {
555 ZeroToOffset(OS, writeOp.first);
556 (this->*writeOp.second)(OS);
557 }
558}
559
560void MachOWriter::writeRebaseOpcodes(raw_ostream &OS) {
561 MachOYAML::LinkEditData &LinkEdit = Obj.LinkEdit;
562
563 for (const auto &Opcode : LinkEdit.RebaseOpcodes) {
564 uint8_t OpByte = Opcode.Opcode | Opcode.Imm;
565 OS.write(reinterpret_cast<char *>(&OpByte), 1);
566 for (auto Data : Opcode.ExtraData)
568 }
569}
570
571void MachOWriter::writeBasicBindOpcodes(raw_ostream &OS) {
572 writeBindOpcodes(OS, Obj.LinkEdit.BindOpcodes);
573}
574
575void MachOWriter::writeWeakBindOpcodes(raw_ostream &OS) {
576 writeBindOpcodes(OS, Obj.LinkEdit.WeakBindOpcodes);
577}
578
579void MachOWriter::writeLazyBindOpcodes(raw_ostream &OS) {
580 writeBindOpcodes(OS, Obj.LinkEdit.LazyBindOpcodes);
581}
582
583void MachOWriter::writeNameList(raw_ostream &OS) {
584 for (auto NLE : Obj.LinkEdit.NameList) {
585 if (is64Bit)
586 writeNListEntry<MachO::nlist_64>(NLE, OS, Obj.IsLittleEndian);
587 else
588 writeNListEntry<MachO::nlist>(NLE, OS, Obj.IsLittleEndian);
589 }
590}
591
592void MachOWriter::writeStringTable(raw_ostream &OS) {
593 for (auto Str : Obj.LinkEdit.StringTable) {
594 OS.write(Str.data(), Str.size());
595 OS.write('\0');
596 }
597}
598
599void MachOWriter::writeDynamicSymbolTable(raw_ostream &OS) {
600 for (auto Data : Obj.LinkEdit.IndirectSymbols)
601 OS.write(reinterpret_cast<const char *>(&Data),
602 sizeof(yaml::Hex32::BaseType));
603}
604
605void MachOWriter::writeFunctionStarts(raw_ostream &OS) {
606 uint64_t Addr = 0;
607 for (uint64_t NextAddr : Obj.LinkEdit.FunctionStarts) {
608 uint64_t Delta = NextAddr - Addr;
609 encodeULEB128(Delta, OS);
610 Addr = NextAddr;
611 }
612
613 OS.write('\0');
614}
615
616void MachOWriter::writeDataInCode(raw_ostream &OS) {
617 for (const auto &Entry : Obj.LinkEdit.DataInCode) {
618 MachO::data_in_code_entry DICE{Entry.Offset, Entry.Length, Entry.Kind};
620 MachO::swapStruct(DICE);
621 OS.write(reinterpret_cast<const char *>(&DICE),
623 }
624}
625
626void MachOWriter::writeChainedFixups(raw_ostream &OS) {
627 if (Obj.LinkEdit.ChainedFixups.size() > 0)
628 OS.write(reinterpret_cast<const char *>(Obj.LinkEdit.ChainedFixups.data()),
629 Obj.LinkEdit.ChainedFixups.size());
630}
631
632void MachOWriter::writeDyldExportsTrie(raw_ostream &OS) {
633 dumpExportEntry(OS, Obj.LinkEdit.ExportTrie);
634}
635
636class UniversalWriter {
637public:
638 UniversalWriter(yaml::YamlObjectFile &ObjectFile)
639 : ObjectFile(ObjectFile), fileStart(0) {}
640
641 Error writeMachO(raw_ostream &OS);
642
643private:
644 void writeFatHeader(raw_ostream &OS);
645 void writeFatArchs(raw_ostream &OS);
646
647 void ZeroToOffset(raw_ostream &OS, size_t offset);
648
650 uint64_t fileStart;
651};
652
653Error UniversalWriter::writeMachO(raw_ostream &OS) {
654 fileStart = OS.tell();
655 if (ObjectFile.MachO) {
656 MachOWriter Writer(*ObjectFile.MachO);
657 return Writer.writeMachO(OS);
658 }
659
660 writeFatHeader(OS);
661 writeFatArchs(OS);
662
663 auto &FatFile = *ObjectFile.FatMachO;
664 if (FatFile.FatArchs.size() < FatFile.Slices.size())
665 return createStringError(
666 errc::invalid_argument,
667 "cannot write 'Slices' if not described in 'FatArches'");
668
669 for (size_t i = 0; i < FatFile.Slices.size(); i++) {
670 ZeroToOffset(OS, FatFile.FatArchs[i].offset);
671 MachOWriter Writer(FatFile.Slices[i]);
672 if (Error Err = Writer.writeMachO(OS))
673 return Err;
674
675 auto SliceEnd = FatFile.FatArchs[i].offset + FatFile.FatArchs[i].size;
676 ZeroToOffset(OS, SliceEnd);
677 }
678
679 return Error::success();
680}
681
682void UniversalWriter::writeFatHeader(raw_ostream &OS) {
683 auto &FatFile = *ObjectFile.FatMachO;
684 MachO::fat_header header;
685 header.magic = FatFile.Header.magic;
686 header.nfat_arch = FatFile.Header.nfat_arch;
688 swapStruct(header);
689 OS.write(reinterpret_cast<const char *>(&header), sizeof(MachO::fat_header));
690}
691
692template <typename FatArchType>
693FatArchType constructFatArch(MachOYAML::FatArch &Arch) {
694 FatArchType FatArch;
695 FatArch.cputype = Arch.cputype;
696 FatArch.cpusubtype = Arch.cpusubtype;
697 FatArch.offset = Arch.offset;
698 FatArch.size = Arch.size;
699 FatArch.align = Arch.align;
700 return FatArch;
701}
702
703template <typename StructType>
704void writeFatArch(MachOYAML::FatArch &LC, raw_ostream &OS) {}
705
706template <>
707void writeFatArch<MachO::fat_arch>(MachOYAML::FatArch &Arch, raw_ostream &OS) {
708 auto FatArch = constructFatArch<MachO::fat_arch>(Arch);
710 swapStruct(FatArch);
711 OS.write(reinterpret_cast<const char *>(&FatArch), sizeof(MachO::fat_arch));
712}
713
714template <>
715void writeFatArch<MachO::fat_arch_64>(MachOYAML::FatArch &Arch,
716 raw_ostream &OS) {
717 auto FatArch = constructFatArch<MachO::fat_arch_64>(Arch);
718 FatArch.reserved = Arch.reserved;
720 swapStruct(FatArch);
721 OS.write(reinterpret_cast<const char *>(&FatArch),
722 sizeof(MachO::fat_arch_64));
723}
724
725void UniversalWriter::writeFatArchs(raw_ostream &OS) {
726 auto &FatFile = *ObjectFile.FatMachO;
727 bool is64Bit = FatFile.Header.magic == MachO::FAT_MAGIC_64;
728 for (auto Arch : FatFile.FatArchs) {
729 if (is64Bit)
730 writeFatArch<MachO::fat_arch_64>(Arch, OS);
731 else
732 writeFatArch<MachO::fat_arch>(Arch, OS);
733 }
734}
735
736void UniversalWriter::ZeroToOffset(raw_ostream &OS, size_t Offset) {
737 auto currOffset = OS.tell() - fileStart;
738 if (currOffset < Offset)
739 ZeroFillBytes(OS, Offset - currOffset);
740}
741
742} // end anonymous namespace
743
744namespace llvm {
745namespace yaml {
746
748 UniversalWriter Writer(Doc);
749 if (Error Err = Writer.writeMachO(Out)) {
750 handleAllErrors(std::move(Err),
751 [&](const ErrorInfoBase &Err) { EH(Err.message()); });
752 return false;
753 }
754 return true;
755}
756
757} // namespace yaml
758} // namespace llvm
static void ZeroFillBytes(raw_ostream &OS, size_t Size)
Common declarations for yaml2obj.
T Content
uint64_t Addr
uint64_t Size
Section constructSection(const MachO::section &Sec, uint32_t Index)
Definition: MachOReader.cpp:50
void writeNListEntry(const SymbolEntry &SE, bool IsLittleEndian, char *&Out, uint32_t Nstrx)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
static bool is64Bit(const char *name)
Base class for error info classes.
Definition: Error.h:45
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
An efficient, type-erasing, non-owning reference to a callable.
This class is the base class for all object file types.
Definition: ObjectFile.h:229
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:151
raw_ostream & write(unsigned char C)
Specialized YAMLIO scalar type for representing a binary blob.
Definition: YAML.h:63
bool is64Bit(T Machine)
Definition: COFF.h:133
@ Entry
Definition: COFF.h:811
std::function< Error(raw_ostream &, const Data &)> getDWARFEmitterByName(StringRef SecName)
@ R_SCATTERED
Definition: MachO.h:402
@ MH_CIGAM_64
Definition: MachO.h:33
@ FAT_MAGIC_64
Definition: MachO.h:36
@ MH_MAGIC_64
Definition: MachO.h:32
bool isVirtualSection(uint8_t type)
Definition: MachO.h:595
SectionType
These are the section type and attributes fields.
Definition: MachO.h:122
void swapStruct(fat_header &mh)
Definition: MachO.h:1140
@ EXPORT_SYMBOL_FLAGS_REEXPORT
Definition: MachO.h:294
@ EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
Definition: MachO.h:295
@ SECTION_TYPE
Definition: MachO.h:114
static const bool IsLittleEndianHost
Definition: SwapByteOrder.h:29
bool yaml2macho(YamlObjectFile &Doc, raw_ostream &Out, ErrorHandler EH)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:977
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1286
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
SetVector< StringRef > getNonEmptySectionNames() const
Definition: DWARFYAML.cpp:25
llvm::yaml::Hex64 offset
Definition: MachOYAML.h:157
llvm::yaml::Hex32 cputype
Definition: MachOYAML.h:155
llvm::yaml::Hex32 cpusubtype
Definition: MachOYAML.h:156
llvm::yaml::Hex32 reserved
Definition: MachOYAML.h:160
llvm::yaml::Hex32 filetype
Definition: MachOYAML.h:66
llvm::yaml::Hex32 cpusubtype
Definition: MachOYAML.h:65
llvm::yaml::Hex32 cputype
Definition: MachOYAML.h:64
llvm::yaml::Hex32 flags
Definition: MachOYAML.h:69
llvm::yaml::Hex32 reserved
Definition: MachOYAML.h:70
llvm::yaml::Hex32 magic
Definition: MachOYAML.h:63
std::vector< MachOYAML::BindOpcode > WeakBindOpcodes
Definition: MachOYAML.h:126
std::vector< DataInCodeEntry > DataInCode
Definition: MachOYAML.h:133
std::vector< yaml::Hex8 > ChainedFixups
Definition: MachOYAML.h:134
std::vector< MachOYAML::BindOpcode > LazyBindOpcodes
Definition: MachOYAML.h:127
std::vector< MachOYAML::BindOpcode > BindOpcodes
Definition: MachOYAML.h:125
std::vector< MachOYAML::RebaseOpcode > RebaseOpcodes
Definition: MachOYAML.h:124
std::vector< StringRef > StringTable
Definition: MachOYAML.h:130
std::vector< yaml::Hex32 > IndirectSymbols
Definition: MachOYAML.h:131
std::vector< yaml::Hex64 > FunctionStarts
Definition: MachOYAML.h:132
MachOYAML::ExportEntry ExportTrie
Definition: MachOYAML.h:128
std::vector< NListEntry > NameList
Definition: MachOYAML.h:129
llvm::MachO::macho_load_command Data
Definition: MachOYAML.h:76
std::vector< Section > Sections
Definition: MachOYAML.h:77
std::vector< MachO::build_tool_version > Tools
Definition: MachOYAML.h:78
std::vector< llvm::yaml::Hex8 > PayloadBytes
Definition: MachOYAML.h:79
llvm::yaml::Hex8 n_type
Definition: MachOYAML.h:86
std::vector< LoadCommand > LoadCommands
Definition: MachOYAML.h:142
DWARFYAML::Data DWARF
Definition: MachOYAML.h:146
std::optional< llvm::yaml::BinaryRef > RawLinkEditSegment
Definition: MachOYAML.h:145
LinkEditData LinkEdit
Definition: MachOYAML.h:144
std::vector< Relocation > relocations
Definition: MachOYAML.h:59
std::optional< llvm::yaml::BinaryRef > content
Definition: MachOYAML.h:58
llvm::yaml::Hex32 reserved2
Definition: MachOYAML.h:56
llvm::yaml::Hex64 addr
Definition: MachOYAML.h:48
llvm::yaml::Hex32 reserved1
Definition: MachOYAML.h:55
llvm::yaml::Hex32 reloff
Definition: MachOYAML.h:52
llvm::yaml::Hex32 offset
Definition: MachOYAML.h:50
llvm::yaml::Hex32 reserved3
Definition: MachOYAML.h:57
llvm::yaml::Hex32 flags
Definition: MachOYAML.h:54
Definition: MachO.h:808
uint32_t nfat_arch
Definition: MachO.h:958
Function object to check whether the first component of a container supported by std::get (like std::...
Definition: STLExtras.h:1450
Common declarations for yaml2obj.