llvm.org GIT mirror llvm / ce66300
[yaml2obj] Get rid of MachO header union This is based on post-commit feedback from Sean Silva. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271942 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 3 years ago
1 changed file(s) with 7 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
3030 MachOWriter(MachOYAML::Object &Obj) : Obj(Obj), is64Bit(true), fileStart(0) {
3131 is64Bit = Obj.Header.magic == MachO::MH_MAGIC_64 ||
3232 Obj.Header.magic == MachO::MH_CIGAM_64;
33 memset(reinterpret_cast(&Header64), 0,
34 sizeof(MachO::mach_header_64));
33 memset(reinterpret_cast(&Header), 0, sizeof(MachO::mach_header_64));
3534 assert((is64Bit || Obj.Header.reserved == 0xDEADBEEFu) &&
3635 "32-bit MachO has reserved in header");
3736 assert((!is64Bit || Obj.Header.reserved != 0xDEADBEEFu) &&
6362 bool is64Bit;
6463 uint64_t fileStart;
6564
66 union {
67 MachO::mach_header_64 Header64;
68 MachO::mach_header Header;
69 };
65 MachO::mach_header_64 Header;
7066 };
7167
7268 Error MachOWriter::writeMachO(raw_ostream &OS) {
8884 Header.ncmds = Obj.Header.ncmds;
8985 Header.sizeofcmds = Obj.Header.sizeofcmds;
9086 Header.flags = Obj.Header.flags;
91 Header64.reserved = Obj.Header.reserved;
92
93 if (is64Bit)
94 OS.write((const char *)&Header64, sizeof(MachO::mach_header_64));
95 else
96 OS.write((const char *)&Header, sizeof(MachO::mach_header));
87 Header.reserved = Obj.Header.reserved;
88
89 auto header_size =
90 is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
91 OS.write((const char *)&Header, header_size);
9792
9893 return Error::success();
9994 }