llvm.org GIT mirror llvm / ed0b171
[yaml2obj] Support for dumping mach_header from yaml With this change obj2yaml and yaml2obj can now round-trip mach_headers. This change also adds ObjectYAML/MachO tests. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269314 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 3 years ago
4 changed file(s) with 114 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
0 # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
1
2 --- !mach-o
3 FileHeader:
4 magic: 0xFEEDFACE
5 cputype: 0x00000007
6 cpusubtype: 0x80000003
7 filetype: 0x00000002
8 ncmds: 0
9 sizeofcmds: 0
10 flags: 0x00218085
11 ...
12
13 # CHECK: --- !mach-o
14 # CHECK: FileHeader:
15 # CHECK: magic: 0xFEEDFACE
16 # CHECK: cputype: 0x00000007
17 # CHECK: cpusubtype: 0x80000003
18 # CHECK: filetype: 0x00000002
19 # CHECK: ncmds: 0
20 # CHECK: sizeofcmds: 0
21 # CHECK: flags: 0x00218085
22 # CHECK: ...
0 # RUN: yaml2obj -format=macho %s | obj2yaml | FileCheck %s
1
2 --- !mach-o
3 FileHeader:
4 magic: 0xFEEDFACF
5 cputype: 0x01000007
6 cpusubtype: 0x80000003
7 filetype: 0x00000002
8 ncmds: 0
9 sizeofcmds: 0
10 flags: 0x00218085
11 ...
12
13 # CHECK: --- !mach-o
14 # CHECK: FileHeader:
15 # CHECK: magic: 0xFEEDFACF
16 # CHECK: cputype: 0x01000007
17 # CHECK: cpusubtype: 0x80000003
18 # CHECK: filetype: 0x00000002
19 # CHECK: ncmds: 0
20 # CHECK: sizeofcmds: 0
21 # CHECK: flags: 0x00218085
22 # CHECK: ...
0 config.suffixes = ['.yaml']
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "yaml2obj.h"
15 #include "llvm/ObjectYAML/MachOYAML.h"
16 #include "llvm/Support/Error.h"
17 #include "llvm/Support/MachO.h"
18 #include "llvm/Support/YAMLTraits.h"
1519 #include "llvm/Support/raw_ostream.h"
1620
1721 using namespace llvm;
1822
19 int yaml2macho(llvm::yaml::Input &YIn, llvm::raw_ostream &Out) {
20 errs() << "yaml2obj: Mach-O not implemented yet!\n";
21 return 1;
23 namespace {
24
25 class MachOWriter {
26 public:
27 MachOWriter(MachOYAML::Object &Obj) : Obj(Obj) {
28 is64Bit = Obj.Header.magic == MachO::MH_MAGIC_64 ||
29 Obj.Header.magic == MachO::MH_CIGAM_64;
30 bzero(&Header64, sizeof(MachO::mach_header_64));
31 }
32
33 Error writeMachO(raw_ostream &OS);
34
35 private:
36 Error writeHeader(raw_ostream &OS);
37
38 MachOYAML::Object Obj;
39 bool is64Bit;
40
41 union {
42 MachO::mach_header_64 Header64;
43 MachO::mach_header Header;
44 };
45 };
46
47 Error MachOWriter::writeMachO(raw_ostream &OS) {
48 if (auto Err = writeHeader(OS))
49 return Err;
50 return Error::success();
2251 }
52
53 Error MachOWriter::writeHeader(raw_ostream &OS) {
54 Header.magic = Obj.Header.magic;
55 Header.cputype = Obj.Header.cputype;
56 Header.cpusubtype = Obj.Header.cpusubtype;
57 Header.filetype = Obj.Header.filetype;
58 Header.ncmds = Obj.Header.ncmds;
59 Header.sizeofcmds = Obj.Header.sizeofcmds;
60 Header.flags = Obj.Header.flags;
61
62 if (is64Bit)
63 OS.write((const char *)&Header64, sizeof(MachO::mach_header_64));
64 else
65 OS.write((const char *)&Header, sizeof(MachO::mach_header));
66
67 return Error::success();
68 }
69
70 } // end anonymous namespace
71
72 int yaml2macho(yaml::Input &YIn, raw_ostream &Out) {
73 MachOYAML::Object Doc;
74 YIn >> Doc;
75 if (YIn.error()) {
76 errs() << "yaml2obj: Failed to parse YAML file!\n";
77 return 1;
78 }
79
80 MachOWriter Writer(Doc);
81 if (auto Err = Writer.writeMachO(Out)) {
82 errs() << toString(std::move(Err));
83 return 1;
84 }
85 return 0;
86 }