llvm.org GIT mirror llvm / 7ec0ea1
Re-apply: [obj2yaml] [yaml2obj] Support MachO section and section_64 This re-applies r269845, r269846, and r269850 with an included fix for a crash reported by zturner. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269953 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 3 years ago
6 changed file(s) with 427 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
2121 namespace llvm {
2222 namespace MachOYAML {
2323
24 struct Section {
25 char sectname[16];
26 char segname[16];
27 llvm::yaml::Hex64 addr;
28 uint64_t size;
29 llvm::yaml::Hex32 offset;
30 uint32_t align;
31 llvm::yaml::Hex32 reloff;
32 uint32_t nreloc;
33 llvm::yaml::Hex32 flags;
34 llvm::yaml::Hex32 reserved1;
35 llvm::yaml::Hex32 reserved2;
36 llvm::yaml::Hex32 reserved3;
37 };
38
2439 struct FileHeader {
2540 llvm::yaml::Hex32 magic;
2641 llvm::yaml::Hex32 cputype;
3550 struct LoadCommand {
3651 virtual ~LoadCommand();
3752 llvm::MachO::macho_load_command Data;
53 std::vector
Sections;
3854 };
3955
4056 struct Object {
4157 FileHeader Header;
4258 std::vector LoadCommands;
59 std::vector
Sections;
4360 };
4461
4562 } // namespace llvm::MachOYAML
4663 } // namespace llvm
4764
4865 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand)
66 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Section)
4967
5068 namespace llvm {
5169 namespace yaml {
6078
6179 template <> struct MappingTraits {
6280 static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand);
81 };
82
83 template <> struct MappingTraits {
84 static void mapping(IO &IO, MachOYAML::Section &Section);
6385 };
6486
6587 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
1414 #include "llvm/Support/Casting.h"
1515 #include "llvm/Support/Format.h"
1616
17 #include // For memcpy and memset.
17 #include // For memcpy, memset and strnlen.
1818
1919 namespace llvm {
2020
2424
2525 void ScalarTraits::output(const char_16 &Val, void *,
2626 llvm::raw_ostream &Out) {
27 Out << Val;
27 auto Len = strnlen(&Val[0], 16);
28 Out << StringRef(&Val[0], Len);
2829 }
2930
3031 StringRef ScalarTraits::input(StringRef Scalar, void *, char_16 &Val) {
109110 switch (LoadCommand.Data.load_command_data.cmd) {
110111 #include "llvm/Support/MachO.def"
111112 }
113 if (LoadCommand.Data.load_command_data.cmd == MachO::LC_SEGMENT ||
114 LoadCommand.Data.load_command_data.cmd == MachO::LC_SEGMENT_64) {
115 IO.mapOptional("Sections", LoadCommand.Sections);
116 }
112117 }
113118
114119 void MappingTraits::mapping(
115120 IO &IO, MachO::dyld_info_command &LoadCommand) {
116121 IO.mapRequired("rebase_off", LoadCommand.rebase_off);
117122 IO.mapRequired("rebase_size", LoadCommand.rebase_size);
118 IO.mapRequired("bind_off", LoadCommand.bind_size);
123 IO.mapRequired("bind_off", LoadCommand.bind_off);
124 IO.mapRequired("bind_size", LoadCommand.bind_size);
119125 IO.mapRequired("weak_bind_off", LoadCommand.weak_bind_off);
120126 IO.mapRequired("weak_bind_size", LoadCommand.weak_bind_size);
121 IO.mapRequired("lazy_bind_off", LoadCommand.lazy_bind_size);
127 IO.mapRequired("lazy_bind_off", LoadCommand.lazy_bind_off);
128 IO.mapRequired("lazy_bind_size", LoadCommand.lazy_bind_size);
122129 IO.mapRequired("export_off", LoadCommand.export_off);
123130 IO.mapRequired("export_size", LoadCommand.export_size);
131 }
132
133 void MappingTraits::mapping(IO &IO,
134 MachOYAML::Section &Section) {
135 IO.mapRequired("sectname", Section.sectname);
136 IO.mapRequired("segname", Section.segname);
137 IO.mapRequired("addr", Section.addr);
138 IO.mapRequired("size", Section.size);
139 IO.mapRequired("offset", Section.offset);
140 IO.mapRequired("align", Section.align);
141 IO.mapRequired("reloff", Section.reloff);
142 IO.mapRequired("nreloc", Section.nreloc);
143 IO.mapRequired("flags", Section.flags);
144 IO.mapRequired("reserved1", Section.reserved1);
145 IO.mapRequired("reserved2", Section.reserved2);
146 IO.mapOptional("reserved3", Section.reserved3);
124147 }
125148
126149 void MappingTraits::mapping(IO &IO, MachO::dylib &DylibStruct) {
5858 cmdsize: 48
5959 rebase_off: 12288
6060 rebase_size: 8
61 bind_off: 96
61 bind_off: 12296
62 bind_size: 96
6263 weak_bind_off: 0
6364 weak_bind_size: 0
64 lazy_bind_off: 624
65 lazy_bind_off: 12392
66 lazy_bind_size: 624
6567 export_off: 13016
6668 export_size: 48
6769 - cmd: LC_SYMTAB
183185 #CHECK: cmdsize: 48
184186 #CHECK: rebase_off: 12288
185187 #CHECK: rebase_size: 8
186 #CHECK: bind_off: 96
188 #CHECK: bind_off: 12296
189 #CHECK: bind_size: 96
187190 #CHECK: weak_bind_off: 0
188191 #CHECK: weak_bind_size: 0
189 #CHECK: lazy_bind_off: 624
192 #CHECK: lazy_bind_off: 12392
193 #CHECK: lazy_bind_size: 624
190194 #CHECK: export_off: 13016
191195 #CHECK: export_size: 48
192196 #CHECK: - cmd: LC_SYMTAB
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: 16
9 sizeofcmds: 1408
10 flags: 0x00218085
11 reserved: 0x00000000
12 LoadCommands:
13 - cmd: LC_SEGMENT_64
14 cmdsize: 72
15 segname: __PAGEZERO
16 vmaddr: 0
17 vmsize: 4294967296
18 fileoff: 0
19 filesize: 0
20 maxprot: 0
21 initprot: 0
22 nsects: 0
23 flags: 0
24 - cmd: LC_SEGMENT_64
25 cmdsize: 552
26 segname: __TEXT
27 vmaddr: 4294967296
28 vmsize: 8192
29 fileoff: 0
30 filesize: 8192
31 maxprot: 7
32 initprot: 5
33 nsects: 6
34 flags: 0
35 Sections:
36 - sectname: __text
37 segname: __TEXT
38 addr: 0x0000000100001160
39 size: 3099
40 offset: 0x00001160
41 align: 4
42 reloff: 0x00000000
43 nreloc: 0
44 flags: 0x80000400
45 reserved1: 0x00000000
46 reserved2: 0x00000000
47 reserved3: 0x00000000
48 - sectname: __stubs
49 segname: __TEXT
50 addr: 0x0000000100001D7C
51 size: 90
52 offset: 0x00001D7C
53 align: 1
54 reloff: 0x00000000
55 nreloc: 0
56 flags: 0x80000408
57 reserved1: 0x00000000
58 reserved2: 0x00000006
59 reserved3: 0x00000000
60 - sectname: __stub_helper
61 segname: __TEXT
62 addr: 0x0000000100001DD8
63 size: 166
64 offset: 0x00001DD8
65 align: 2
66 reloff: 0x00000000
67 nreloc: 0
68 flags: 0x80000400
69 reserved1: 0x00000000
70 reserved2: 0x00000000
71 reserved3: 0x00000000
72 - sectname: __gcc_except_tab
73 segname: __TEXT
74 addr: 0x0000000100001E80
75 size: 240
76 offset: 0x00001E80
77 align: 2
78 reloff: 0x00000000
79 nreloc: 0
80 flags: 0x00000000
81 reserved1: 0x00000000
82 reserved2: 0x00000000
83 reserved3: 0x00000000
84 - sectname: __cstring
85 segname: __TEXT
86 addr: 0x0000000100001F70
87 size: 15
88 offset: 0x00001F70
89 align: 0
90 reloff: 0x00000000
91 nreloc: 0
92 flags: 0x00000002
93 reserved1: 0x00000000
94 reserved2: 0x00000000
95 reserved3: 0x00000000
96 - sectname: __unwind_info
97 segname: __TEXT
98 addr: 0x0000000100001F80
99 size: 120
100 offset: 0x00001F80
101 align: 2
102 reloff: 0x00000000
103 nreloc: 0
104 flags: 0x00000000
105 reserved1: 0x00000000
106 reserved2: 0x00000000
107 reserved3: 0x00000000
108 - cmd: LC_SEGMENT_64
109 cmdsize: 312
110 segname: __DATA
111 vmaddr: 4294975488
112 vmsize: 4096
113 fileoff: 8192
114 filesize: 4096
115 maxprot: 7
116 initprot: 3
117 nsects: 3
118 flags: 0
119 Sections:
120 - sectname: __got
121 segname: __DATA
122 addr: 0x0000000100002000
123 size: 24
124 offset: 0x00002000
125 align: 3
126 reloff: 0x00000000
127 nreloc: 0
128 flags: 0x00000006
129 reserved1: 0x0000000F
130 reserved2: 0x00000000
131 reserved3: 0x00000000
132 - sectname: __nl_symbol_ptr
133 segname: __DATA
134 addr: 0x0000000100002018
135 size: 16
136 offset: 0x00002018
137 align: 3
138 reloff: 0x00000000
139 nreloc: 0
140 flags: 0x00000006
141 reserved1: 0x00000012
142 reserved2: 0x00000000
143 reserved3: 0x00000000
144 - sectname: __la_symbol_ptr
145 segname: __DATA
146 addr: 0x0000000100002028
147 size: 120
148 offset: 0x00002028
149 align: 3
150 reloff: 0x00000000
151 nreloc: 0
152 flags: 0x00000007
153 reserved1: 0x00000014
154 reserved2: 0x00000000
155 reserved3: 0x00000000
156 - cmd: LC_SEGMENT_64
157 cmdsize: 72
158 segname: __LINKEDIT
159 vmaddr: 4294979584
160 vmsize: 4096
161 fileoff: 12288
162 filesize: 2508
163 maxprot: 7
164 initprot: 1
165 nsects: 0
166 flags: 0
167 - cmd: LC_DYLD_INFO_ONLY
168 cmdsize: 48
169 rebase_off: 12288
170 rebase_size: 8
171 bind_off: 12296
172 bind_size: 96
173 weak_bind_off: 0
174 weak_bind_size: 0
175 lazy_bind_off: 12392
176 lazy_bind_size: 624
177 export_off: 13016
178 export_size: 48
179 - cmd: LC_SYMTAB
180 cmdsize: 24
181 symoff: 13080
182 nsyms: 30
183 stroff: 13700
184 strsize: 1096
185 - cmd: LC_DYSYMTAB
186 cmdsize: 80
187 ilocalsym: 0
188 nlocalsym: 9
189 iextdefsym: 9
190 nextdefsym: 2
191 iundefsym: 11
192 nundefsym: 19
193 tocoff: 0
194 ntoc: 0
195 modtaboff: 0
196 nmodtab: 0
197 extrefsymoff: 0
198 nextrefsyms: 0
199 indirectsymoff: 13560
200 nindirectsyms: 35
201 extreloff: 0
202 nextrel: 0
203 locreloff: 0
204 nlocrel: 0
205 - cmd: LC_LOAD_DYLINKER
206 cmdsize: 32
207 name: 12
208 - cmd: LC_UUID
209 cmdsize: 24
210 cmdsize: 24
211 uuid: 461A1B28-822F-3F38-B670-645419E636F5
212 - cmd: LC_VERSION_MIN_MACOSX
213 cmdsize: 16
214 version: 658176
215 sdk: 658176
216 - cmd: LC_SOURCE_VERSION
217 cmdsize: 16
218 version: 0
219 - cmd: LC_MAIN
220 cmdsize: 24
221 entryoff: 4448
222 stacksize: 0
223 - cmd: LC_LOAD_DYLIB
224 cmdsize: 48
225 dylib:
226 name: 24
227 timestamp: 2
228 current_version: 7864576
229 compatibility_version: 65536
230 - cmd: LC_LOAD_DYLIB
231 cmdsize: 56
232 dylib:
233 name: 24
234 timestamp: 2
235 current_version: 80349697
236 compatibility_version: 65536
237 - cmd: LC_FUNCTION_STARTS
238 cmdsize: 16
239 dataoff: 13064
240 datasize: 16
241 - cmd: LC_DATA_IN_CODE
242 cmdsize: 16
243 dataoff: 13080
244 datasize: 0
245 ...
246
247
248 #CHECK: - cmd: LC_SEGMENT_64
249 #CHECK: segname: __PAGEZERO
250 #CHECK: - cmd: LC_SEGMENT_64
251 #CHECK: segname: __TEXT
252 #CHECK: Sections:
253 #CHECK: - sectname: __text
254 #CHECK: segname: __TEXT
255 #CHECK: addr: 0x0000000100001160
256 #CHECK: size: 3099
257 #CHECK: offset: 0x00001160
258 #CHECK: align: 4
259 #CHECK: reloff: 0x00000000
260 #CHECK: nreloc: 0
261 #CHECK: flags: 0x80000400
262 #CHECK: reserved1: 0x00000000
263 #CHECK: reserved2: 0x00000000
264 #CHECK: reserved3: 0x00000000
265 #CHECK: - sectname: __stubs
266 #CHECK: segname: __TEXT
267 #CHECK: - sectname: __stub_helper
268 #CHECK: segname: __TEXT
269 #CHECK: - sectname: __gcc_except_tab
270 #CHECK: segname: __TEXT
271 #CHECK: - sectname: __cstring
272 #CHECK: segname: __TEXT
273 #CHECK: - sectname: __unwind_info
274 #CHECK: segname: __TEXT
275 #CHECK: - cmd: LC_SEGMENT_64
276 #CHECK: segname: __DATA
277 #CHECK: Sections:
278 #CHECK: - sectname: __got
279 #CHECK: segname: __DATA
280 #CHECK: - sectname: __nl_symbol_ptr
281 #CHECK: segname: __DATA
282 #CHECK: - sectname: __la_symbol_ptr
283 #CHECK: segname: __DATA
1111 #include "llvm/Object/MachOUniversal.h"
1212 #include "llvm/ObjectYAML/MachOYAML.h"
1313 #include "llvm/Support/ErrorHandling.h"
14
15 #include // for memcpy
1416
1517 using namespace llvm;
1618
3032 if (Obj.isLittleEndian() != sys::IsLittleEndianHost) \
3133 MachO::swapStruct(LC.Data.LCStruct##_data); \
3234 break;
35
36 template
37 MachOYAML::Section constructSection(SectionType Sec) {
38 MachOYAML::Section TempSec;
39 memcpy(reinterpret_cast(&TempSec.sectname[0]), &Sec.sectname[0], 16);
40 memcpy(reinterpret_cast(&TempSec.segname[0]), &Sec.segname[0], 16);
41 TempSec.addr = Sec.addr;
42 TempSec.size = Sec.size;
43 TempSec.offset = Sec.offset;
44 TempSec.align = Sec.align;
45 TempSec.reloff = Sec.reloff;
46 TempSec.nreloc = Sec.nreloc;
47 TempSec.flags = Sec.flags;
48 TempSec.reserved1 = Sec.reserved1;
49 TempSec.reserved2 = Sec.reserved2;
50 TempSec.reserved3 = 0;
51 return TempSec;
52 }
3353
3454 Expected> MachODumper::dump() {
3555 auto Y = make_unique();
5272 MachO::swapStruct(LC.Data.load_command_data);
5373 break;
5474 #include "llvm/Support/MachO.def"
75 }
76 if (LoadCmd.C.cmd == MachO::LC_SEGMENT) {
77 auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize;
78 const MachO::section *Curr = reinterpret_cast(
79 LoadCmd.Ptr + sizeof(MachO::segment_command));
80 for (; reinterpret_cast(Curr) < End; Curr++) {
81 if (Obj.isLittleEndian() != sys::IsLittleEndianHost) {
82 MachO::section Sec;
83 memcpy((void *)&Sec, Curr, sizeof(MachO::section));
84 MachO::swapStruct(Sec);
85 LC.Sections.push_back(constructSection(Sec));
86 } else {
87 LC.Sections.push_back(constructSection(*Curr));
88 }
89 }
90 } else if (LoadCmd.C.cmd == MachO::LC_SEGMENT_64) {
91 auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize;
92 const MachO::section_64 *Curr =
93 reinterpret_cast(
94 LoadCmd.Ptr + sizeof(MachO::segment_command_64));
95 for (; reinterpret_cast(Curr) < End; Curr++) {
96 MachOYAML::Section TempSec;
97 if (Obj.isLittleEndian() != sys::IsLittleEndianHost) {
98 MachO::section_64 Sec;
99 memcpy((void *)&Sec, Curr, sizeof(MachO::section_64));
100 MachO::swapStruct(Sec);
101 LC.Sections.push_back(constructSection(Sec));
102 TempSec = constructSection(Sec);
103 } else {
104 TempSec = constructSection(*Curr);
105 }
106 TempSec.reserved3 = Curr->reserved3;
107 LC.Sections.push_back(TempSec);
108 }
55109 }
56110 Y->LoadCommands.push_back(std::move(LC));
57111 }
7676 return Error::success();
7777 }
7878
79 template
80 SectionType constructSection(MachOYAML::Section Sec) {
81 SectionType TempSec;
82 memcpy(reinterpret_cast(&TempSec.sectname[0]), &Sec.sectname[0], 16);
83 memcpy(reinterpret_cast(&TempSec.segname[0]), &Sec.segname[0], 16);
84 TempSec.addr = Sec.addr;
85 TempSec.size = Sec.size;
86 TempSec.offset = Sec.offset;
87 TempSec.align = Sec.align;
88 TempSec.reloff = Sec.reloff;
89 TempSec.nreloc = Sec.nreloc;
90 TempSec.flags = Sec.flags;
91 TempSec.reserved1 = Sec.reserved1;
92 TempSec.reserved2 = Sec.reserved2;
93 return TempSec;
94 }
95
7996 Error MachOWriter::writeLoadCommands(raw_ostream &OS) {
8097 for (auto &LC : Obj.LoadCommands) {
8198 size_t BytesWritten = 0;
93110 BytesWritten = sizeof(MachO::load_command);
94111 break;
95112 #include "llvm/Support/MachO.def"
113 }
114
115 if(LC.Data.load_command_data.cmd == MachO::LC_SEGMENT) {
116 for(auto Sec : LC.Sections) {
117 auto TempSec = constructSection(Sec);
118 OS.write(reinterpret_cast(&(TempSec)), sizeof(MachO::section));
119 BytesWritten += sizeof(MachO::section);
120 }
121 } else if(LC.Data.load_command_data.cmd == MachO::LC_SEGMENT_64) {
122 for(auto Sec : LC.Sections) {
123 auto TempSec = constructSection(Sec);
124 TempSec.reserved3 = Sec.reserved3;
125 OS.write(reinterpret_cast(&(TempSec)), sizeof(MachO::section_64));
126 BytesWritten += sizeof(MachO::section_64);
127 }
96128 }
97129
98130 auto BytesRemaining =