llvm.org GIT mirror llvm / 5e76a3f
[ObjectYAML] MachO support for endianness This patch adds support to the macho<->yaml tools for preserving endianness in MachO structures and DWARF data. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@290381 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 2 years ago
11 changed file(s) with 1317 addition(s) and 89 deletion(s). Raw diff Collapse all Expand all
6666 };
6767
6868 struct Data {
69 bool IsLittleEndian;
6970 std::vector AbbrevDecls;
7071 std::vector DebugStrings;
7172 std::vector ARanges;
105105 };
106106
107107 struct Object {
108 bool IsLittleEndian;
108109 FileHeader Header;
109110 std::vector LoadCommands;
110111 std::vector
Sections;
1313 #include "llvm/ObjectYAML/MachOYAML.h"
1414 #include "llvm/Support/Casting.h"
1515 #include "llvm/Support/Format.h"
16 #include "llvm/Support/Host.h"
1617 #include "llvm/Support/MachO.h"
1718
1819 #include // For memcpy, memset and strnlen.
99100 IO.setContext(&Object);
100101 }
101102 IO.mapTag("!mach-o", true);
103 IO.mapOptional("IsLittleEndian", Object.IsLittleEndian,
104 sys::IsLittleEndianHost);
105 Object.DWARF.IsLittleEndian = Object.IsLittleEndian;
106
102107 IO.mapRequired("FileHeader", Object.Header);
103108 IO.mapOptional("LoadCommands", Object.LoadCommands);
104109 if(!Object.LinkEdit.isEmpty() || !IO.outputting())
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
1
2 --- !mach-o
3 IsLittleEndian: false
4 FileHeader:
5 magic: 0xFEEDFACE
6 cputype: 0x00000012
7 cpusubtype: 0x00000000
8 filetype: 0x00000001
9 ncmds: 3
10 sizeofcmds: 368
11 flags: 0x00002000
12 LoadCommands:
13 - cmd: LC_SEGMENT
14 cmdsize: 328
15 segname: ''
16 vmaddr: 0
17 vmsize: 236
18 fileoff: 476
19 filesize: 236
20 maxprot: 7
21 initprot: 7
22 nsects: 4
23 flags: 0
24 Sections:
25 - sectname: __text
26 segname: __TEXT
27 addr: 0x0000000000000000
28 size: 188
29 offset: 0x000001DC
30 align: 4
31 reloff: 0x000002C8
32 nreloc: 9
33 flags: 0x80000400
34 reserved1: 0x00000000
35 reserved2: 0x00000000
36 reserved3: 0x00000000
37 - sectname: __textcoal_nt
38 segname: __TEXT
39 addr: 0x00000000000000BC
40 size: 0
41 offset: 0x00000298
42 align: 0
43 reloff: 0x00000000
44 nreloc: 0
45 flags: 0x8000000B
46 reserved1: 0x00000000
47 reserved2: 0x00000000
48 reserved3: 0x00000000
49 - sectname: __picsymbolstub1
50 segname: __TEXT
51 addr: 0x00000000000000BC
52 size: 0
53 offset: 0x00000298
54 align: 0
55 reloff: 0x00000000
56 nreloc: 0
57 flags: 0x80000008
58 reserved1: 0x00000000
59 reserved2: 0x00000020
60 reserved3: 0x00000000
61 - sectname: __cstring
62 segname: __TEXT
63 addr: 0x00000000000000BC
64 size: 48
65 offset: 0x00000298
66 align: 0
67 reloff: 0x00000000
68 nreloc: 0
69 flags: 0x00000002
70 reserved1: 0x00000000
71 reserved2: 0x00000000
72 reserved3: 0x00000000
73 - cmd: LC_VERSION_MIN_MACOSX
74 cmdsize: 16
75 version: 658432
76 sdk: 0
77 - cmd: LC_SYMTAB
78 cmdsize: 24
79 symoff: 784
80 nsyms: 0
81 stroff: 808
82 strsize: 36
83 LinkEditData:
84 StringTable:
85 - ''
86 - _compilerrt_abort_impl
87 - ___absvdi2
88 - ''
89 ...
90
91 #CHECK: IsLittleEndian: false
92 #CHECK: FileHeader:
93 #CHECK: magic: 0xFEEDFACE
94 #CHECK: cputype: 0x00000012
95 #CHECK: cpusubtype: 0x00000000
96 #CHECK: filetype: 0x00000001
97 #CHECK: ncmds: 3
98 #CHECK: sizeofcmds: 368
99 #CHECK: flags: 0x00002000
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
1
2 --- !mach-o
3 IsLittleEndian: false
4 FileHeader:
5 magic: 0xFEEDFACE
6 cputype: 0x00000012
7 cpusubtype: 0x00000000
8 filetype: 0x00000001
9 ncmds: 4
10 sizeofcmds: 1264
11 flags: 0x00002000
12 LoadCommands:
13 - cmd: LC_SEGMENT
14 cmdsize: 1144
15 segname: ''
16 vmaddr: 0
17 vmsize: 1122
18 fileoff: 1292
19 filesize: 1122
20 maxprot: 7
21 initprot: 7
22 nsects: 16
23 flags: 0
24 Sections:
25 - sectname: __text
26 segname: __TEXT
27 addr: 0x0000000000000000
28 size: 188
29 offset: 0x0000050C
30 align: 4
31 reloff: 0x00000970
32 nreloc: 9
33 flags: 0x80000400
34 reserved1: 0x00000000
35 reserved2: 0x00000000
36 reserved3: 0x00000000
37 - sectname: __textcoal_nt
38 segname: __TEXT
39 addr: 0x00000000000000BC
40 size: 0
41 offset: 0x000005C8
42 align: 0
43 reloff: 0x00000000
44 nreloc: 0
45 flags: 0x8000000B
46 reserved1: 0x00000000
47 reserved2: 0x00000000
48 reserved3: 0x00000000
49 - sectname: __picsymbolstub1
50 segname: __TEXT
51 addr: 0x00000000000000BC
52 size: 0
53 offset: 0x000005C8
54 align: 0
55 reloff: 0x00000000
56 nreloc: 0
57 flags: 0x80000008
58 reserved1: 0x00000000
59 reserved2: 0x00000020
60 reserved3: 0x00000000
61 - sectname: __cstring
62 segname: __TEXT
63 addr: 0x00000000000000BC
64 size: 48
65 offset: 0x000005C8
66 align: 0
67 reloff: 0x00000000
68 nreloc: 0
69 flags: 0x00000002
70 reserved1: 0x00000000
71 reserved2: 0x00000000
72 reserved3: 0x00000000
73 - sectname: __debug_str
74 segname: __DWARF
75 addr: 0x00000000000000EC
76 size: 182
77 offset: 0x000005F8
78 align: 0
79 reloff: 0x00000000
80 nreloc: 0
81 flags: 0x02000000
82 reserved1: 0x00000000
83 reserved2: 0x00000000
84 reserved3: 0x00000000
85 - sectname: __debug_loc
86 segname: __DWARF
87 addr: 0x00000000000001A2
88 size: 0
89 offset: 0x000006AE
90 align: 0
91 reloff: 0x00000000
92 nreloc: 0
93 flags: 0x02000000
94 reserved1: 0x00000000
95 reserved2: 0x00000000
96 reserved3: 0x00000000
97 - sectname: __debug_abbrev
98 segname: __DWARF
99 addr: 0x00000000000001A2
100 size: 104
101 offset: 0x000006AE
102 align: 0
103 reloff: 0x00000000
104 nreloc: 0
105 flags: 0x02000000
106 reserved1: 0x00000000
107 reserved2: 0x00000000
108 reserved3: 0x00000000
109 - sectname: __debug_info
110 segname: __DWARF
111 addr: 0x000000000000020A
112 size: 141
113 offset: 0x00000716
114 align: 0
115 reloff: 0x000009B8
116 nreloc: 2
117 flags: 0x02000000
118 reserved1: 0x00000000
119 reserved2: 0x00000000
120 reserved3: 0x00000000
121 - sectname: __debug_ranges
122 segname: __DWARF
123 addr: 0x0000000000000297
124 size: 0
125 offset: 0x000007A3
126 align: 0
127 reloff: 0x00000000
128 nreloc: 0
129 flags: 0x02000000
130 reserved1: 0x00000000
131 reserved2: 0x00000000
132 reserved3: 0x00000000
133 - sectname: __debug_macinfo
134 segname: __DWARF
135 addr: 0x0000000000000297
136 size: 1
137 offset: 0x000007A3
138 align: 0
139 reloff: 0x00000000
140 nreloc: 0
141 flags: 0x02000000
142 reserved1: 0x00000000
143 reserved2: 0x00000000
144 reserved3: 0x00000000
145 - sectname: __apple_names
146 segname: __DWARF
147 addr: 0x0000000000000298
148 size: 60
149 offset: 0x000007A4
150 align: 0
151 reloff: 0x00000000
152 nreloc: 0
153 flags: 0x02000000
154 reserved1: 0x00000000
155 reserved2: 0x00000000
156 reserved3: 0x00000000
157 - sectname: __apple_objc
158 segname: __DWARF
159 addr: 0x00000000000002D4
160 size: 36
161 offset: 0x000007E0
162 align: 0
163 reloff: 0x00000000
164 nreloc: 0
165 flags: 0x02000000
166 reserved1: 0x00000000
167 reserved2: 0x00000000
168 reserved3: 0x00000000
169 - sectname: __apple_namespac
170 segname: __DWARF
171 addr: 0x00000000000002F8
172 size: 36
173 offset: 0x00000804
174 align: 0
175 reloff: 0x00000000
176 nreloc: 0
177 flags: 0x02000000
178 reserved1: 0x00000000
179 reserved2: 0x00000000
180 reserved3: 0x00000000
181 - sectname: __apple_types
182 segname: __DWARF
183 addr: 0x000000000000031C
184 size: 133
185 offset: 0x00000828
186 align: 0
187 reloff: 0x00000000
188 nreloc: 0
189 flags: 0x02000000
190 reserved1: 0x00000000
191 reserved2: 0x00000000
192 reserved3: 0x00000000
193 - sectname: __debug_frame
194 segname: __DWARF
195 addr: 0x00000000000003A4
196 size: 48
197 offset: 0x000008B0
198 align: 2
199 reloff: 0x000009C8
200 nreloc: 1
201 flags: 0x02000000
202 reserved1: 0x00000000
203 reserved2: 0x00000000
204 reserved3: 0x00000000
205 - sectname: __debug_line
206 segname: __DWARF
207 addr: 0x00000000000003D4
208 size: 142
209 offset: 0x000008E0
210 align: 0
211 reloff: 0x000009D0
212 nreloc: 1
213 flags: 0x02000000
214 reserved1: 0x00000000
215 reserved2: 0x00000000
216 reserved3: 0x00000000
217 - cmd: LC_VERSION_MIN_MACOSX
218 cmdsize: 16
219 version: 658432
220 sdk: 0
221 - cmd: LC_SYMTAB
222 cmdsize: 24
223 symoff: 2520
224 nsyms: 2
225 stroff: 2544
226 strsize: 36
227 - cmd: LC_DYSYMTAB
228 cmdsize: 80
229 ilocalsym: 0
230 nlocalsym: 0
231 iextdefsym: 0
232 nextdefsym: 1
233 iundefsym: 1
234 nundefsym: 1
235 tocoff: 0
236 ntoc: 0
237 modtaboff: 0
238 nmodtab: 0
239 extrefsymoff: 0
240 nextrefsyms: 0
241 indirectsymoff: 0
242 nindirectsyms: 0
243 extreloff: 0
244 nextrel: 0
245 locreloff: 0
246 nlocrel: 0
247 LinkEditData:
248 NameList:
249 - n_strx: 24
250 n_type: 0x0F
251 n_sect: 1
252 n_desc: 0
253 n_value: 0
254 - n_strx: 1
255 n_type: 0x01
256 n_sect: 0
257 n_desc: 0
258 n_value: 0
259 StringTable:
260 - ''
261 - _compilerrt_abort_impl
262 - ___absvdi2
263 - ''
264 DWARF:
265 debug_str:
266 - 'clang version 4.0.0 (trunk 290181) (llvm/trunk 290209)'
267 - ../compiler-rt/lib/builtins/absvdi2.c
268 - /Users/cbieneman/dev/open-source/llvm-build-rel
269 - int
270 - di_int
271 - long long int
272 - __absvdi2
273 - a
274 - N
275 - t
276 debug_abbrev:
277 - Code: 0x00000001
278 Tag: DW_TAG_compile_unit
279 Children: DW_CHILDREN_yes
280 Attributes:
281 - Attribute: DW_AT_producer
282 Form: DW_FORM_strp
283 - Attribute: DW_AT_language
284 Form: DW_FORM_data2
285 - Attribute: DW_AT_name
286 Form: DW_FORM_strp
287 - Attribute: DW_AT_stmt_list
288 Form: DW_FORM_sec_offset
289 - Attribute: DW_AT_comp_dir
290 Form: DW_FORM_strp
291 - Attribute: DW_AT_low_pc
292 Form: DW_FORM_addr
293 - Attribute: DW_AT_high_pc
294 Form: DW_FORM_data4
295 - Code: 0x00000002
296 Tag: DW_TAG_base_type
297 Children: DW_CHILDREN_no
298 Attributes:
299 - Attribute: DW_AT_name
300 Form: DW_FORM_strp
301 - Attribute: DW_AT_encoding
302 Form: DW_FORM_data1
303 - Attribute: DW_AT_byte_size
304 Form: DW_FORM_data1
305 - Code: 0x00000003
306 Tag: DW_TAG_typedef
307 Children: DW_CHILDREN_no
308 Attributes:
309 - Attribute: DW_AT_type
310 Form: DW_FORM_ref4
311 - Attribute: DW_AT_name
312 Form: DW_FORM_strp
313 - Attribute: DW_AT_decl_file
314 Form: DW_FORM_data1
315 - Attribute: DW_AT_decl_line
316 Form: DW_FORM_data1
317 - Code: 0x00000004
318 Tag: DW_TAG_subprogram
319 Children: DW_CHILDREN_yes
320 Attributes:
321 - Attribute: DW_AT_low_pc
322 Form: DW_FORM_addr
323 - Attribute: DW_AT_high_pc
324 Form: DW_FORM_data4
325 - Attribute: DW_AT_frame_base
326 Form: DW_FORM_exprloc
327 - Attribute: DW_AT_name
328 Form: DW_FORM_strp
329 - Attribute: DW_AT_decl_file
330 Form: DW_FORM_data1
331 - Attribute: DW_AT_decl_line
332 Form: DW_FORM_data1
333 - Attribute: DW_AT_prototyped
334 Form: DW_FORM_flag_present
335 - Attribute: DW_AT_type
336 Form: DW_FORM_ref4
337 - Attribute: DW_AT_external
338 Form: DW_FORM_flag_present
339 - Code: 0x00000005
340 Tag: DW_TAG_formal_parameter
341 Children: DW_CHILDREN_no
342 Attributes:
343 - Attribute: DW_AT_location
344 Form: DW_FORM_exprloc
345 - Attribute: DW_AT_name
346 Form: DW_FORM_strp
347 - Attribute: DW_AT_decl_file
348 Form: DW_FORM_data1
349 - Attribute: DW_AT_decl_line
350 Form: DW_FORM_data1
351 - Attribute: DW_AT_type
352 Form: DW_FORM_ref4
353 - Code: 0x00000006
354 Tag: DW_TAG_variable
355 Children: DW_CHILDREN_no
356 Attributes:
357 - Attribute: DW_AT_location
358 Form: DW_FORM_exprloc
359 - Attribute: DW_AT_name
360 Form: DW_FORM_strp
361 - Attribute: DW_AT_decl_file
362 Form: DW_FORM_data1
363 - Attribute: DW_AT_decl_line
364 Form: DW_FORM_data1
365 - Attribute: DW_AT_type
366 Form: DW_FORM_ref4
367 - Code: 0x00000007
368 Tag: DW_TAG_const_type
369 Children: DW_CHILDREN_no
370 Attributes:
371 - Attribute: DW_AT_type
372 Form: DW_FORM_ref4
373 ...
374
375 #CHECK: DWARF:
376 #CHECK: debug_str:
377 #CHECK: - 'clang version 4.0.0 (trunk 290181) (llvm/trunk 290209)'
378 #CHECK: - ../compiler-rt/lib/builtins/absvdi2.c
379 #CHECK: - /Users/cbieneman/dev/open-source/llvm-build-rel
380 #CHECK: - int
381 #CHECK: - di_int
382 #CHECK: - long long int
383 #CHECK: - __absvdi2
384 #CHECK: - a
385 #CHECK: - N
386 #CHECK: - t
387 #CHECK: debug_abbrev:
388 #CHECK: - Code: 0x00000001
389 #CHECK: Tag: DW_TAG_compile_unit
390 #CHECK: Children: DW_CHILDREN_yes
391 #CHECK: Attributes:
392 #CHECK: - Attribute: DW_AT_producer
393 #CHECK: Form: DW_FORM_strp
394 #CHECK: - Attribute: DW_AT_language
395 #CHECK: Form: DW_FORM_data2
396 #CHECK: - Attribute: DW_AT_name
397 #CHECK: Form: DW_FORM_strp
398 #CHECK: - Attribute: DW_AT_stmt_list
399 #CHECK: Form: DW_FORM_sec_offset
400 #CHECK: - Attribute: DW_AT_comp_dir
401 #CHECK: Form: DW_FORM_strp
402 #CHECK: - Attribute: DW_AT_low_pc
403 #CHECK: Form: DW_FORM_addr
404 #CHECK: - Attribute: DW_AT_high_pc
405 #CHECK: Form: DW_FORM_data4
406 #CHECK: - Code: 0x00000002
407 #CHECK: Tag: DW_TAG_base_type
408 #CHECK: Children: DW_CHILDREN_no
409 #CHECK: Attributes:
410 #CHECK: - Attribute: DW_AT_name
411 #CHECK: Form: DW_FORM_strp
412 #CHECK: - Attribute: DW_AT_encoding
413 #CHECK: Form: DW_FORM_data1
414 #CHECK: - Attribute: DW_AT_byte_size
415 #CHECK: Form: DW_FORM_data1
416 #CHECK: - Code: 0x00000003
417 #CHECK: Tag: DW_TAG_typedef
418 #CHECK: Children: DW_CHILDREN_no
419 #CHECK: Attributes:
420 #CHECK: - Attribute: DW_AT_type
421 #CHECK: Form: DW_FORM_ref4
422 #CHECK: - Attribute: DW_AT_name
423 #CHECK: Form: DW_FORM_strp
424 #CHECK: - Attribute: DW_AT_decl_file
425 #CHECK: Form: DW_FORM_data1
426 #CHECK: - Attribute: DW_AT_decl_line
427 #CHECK: Form: DW_FORM_data1
428 #CHECK: - Code: 0x00000004
429 #CHECK: Tag: DW_TAG_subprogram
430 #CHECK: Children: DW_CHILDREN_yes
431 #CHECK: Attributes:
432 #CHECK: - Attribute: DW_AT_low_pc
433 #CHECK: Form: DW_FORM_addr
434 #CHECK: - Attribute: DW_AT_high_pc
435 #CHECK: Form: DW_FORM_data4
436 #CHECK: - Attribute: DW_AT_frame_base
437 #CHECK: Form: DW_FORM_exprloc
438 #CHECK: - Attribute: DW_AT_name
439 #CHECK: Form: DW_FORM_strp
440 #CHECK: - Attribute: DW_AT_decl_file
441 #CHECK: Form: DW_FORM_data1
442 #CHECK: - Attribute: DW_AT_decl_line
443 #CHECK: Form: DW_FORM_data1
444 #CHECK: - Attribute: DW_AT_prototyped
445 #CHECK: Form: DW_FORM_flag_present
446 #CHECK: - Attribute: DW_AT_type
447 #CHECK: Form: DW_FORM_ref4
448 #CHECK: - Attribute: DW_AT_external
449 #CHECK: Form: DW_FORM_flag_present
450 #CHECK: - Code: 0x00000005
451 #CHECK: Tag: DW_TAG_formal_parameter
452 #CHECK: Children: DW_CHILDREN_no
453 #CHECK: Attributes:
454 #CHECK: - Attribute: DW_AT_location
455 #CHECK: Form: DW_FORM_exprloc
456 #CHECK: - Attribute: DW_AT_name
457 #CHECK: Form: DW_FORM_strp
458 #CHECK: - Attribute: DW_AT_decl_file
459 #CHECK: Form: DW_FORM_data1
460 #CHECK: - Attribute: DW_AT_decl_line
461 #CHECK: Form: DW_FORM_data1
462 #CHECK: - Attribute: DW_AT_type
463 #CHECK: Form: DW_FORM_ref4
464 #CHECK: - Code: 0x00000006
465 #CHECK: Tag: DW_TAG_variable
466 #CHECK: Children: DW_CHILDREN_no
467 #CHECK: Attributes:
468 #CHECK: - Attribute: DW_AT_location
469 #CHECK: Form: DW_FORM_exprloc
470 #CHECK: - Attribute: DW_AT_name
471 #CHECK: Form: DW_FORM_strp
472 #CHECK: - Attribute: DW_AT_decl_file
473 #CHECK: Form: DW_FORM_data1
474 #CHECK: - Attribute: DW_AT_decl_line
475 #CHECK: Form: DW_FORM_data1
476 #CHECK: - Attribute: DW_AT_type
477 #CHECK: Form: DW_FORM_ref4
478 #CHECK: - Code: 0x00000007
479 #CHECK: Tag: DW_TAG_const_type
480 #CHECK: Children: DW_CHILDREN_no
481 #CHECK: Attributes:
482 #CHECK: - Attribute: DW_AT_type
483 #CHECK: Form: DW_FORM_ref4
484
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
1
2 --- !mach-o
3 IsLittleEndian: true
4 FileHeader:
5 magic: 0xFEEDFACF
6 cputype: 0x01000007
7 cpusubtype: 0x00000003
8 filetype: 0x00000001
9 ncmds: 4
10 sizeofcmds: 1392
11 flags: 0x00002000
12 reserved: 0x00000000
13 LoadCommands:
14 - cmd: LC_SEGMENT_64
15 cmdsize: 1272
16 segname: ''
17 vmaddr: 0
18 vmsize: 1086
19 fileoff: 1424
20 filesize: 1086
21 maxprot: 7
22 initprot: 7
23 nsects: 15
24 flags: 0
25 Sections:
26 - sectname: __text
27 segname: __TEXT
28 addr: 0x0000000000000000
29 size: 93
30 offset: 0x00000590
31 align: 4
32 reloff: 0x000009D0
33 nreloc: 3
34 flags: 0x80000400
35 reserved1: 0x00000000
36 reserved2: 0x00000000
37 reserved3: 0x00000000
38 - sectname: __cstring
39 segname: __TEXT
40 addr: 0x000000000000005D
41 size: 48
42 offset: 0x000005ED
43 align: 0
44 reloff: 0x00000000
45 nreloc: 0
46 flags: 0x00000002
47 reserved1: 0x00000000
48 reserved2: 0x00000000
49 reserved3: 0x00000000
50 - sectname: __debug_str
51 segname: __DWARF
52 addr: 0x000000000000008D
53 size: 182
54 offset: 0x0000061D
55 align: 0
56 reloff: 0x00000000
57 nreloc: 0
58 flags: 0x02000000
59 reserved1: 0x00000000
60 reserved2: 0x00000000
61 reserved3: 0x00000000
62 - sectname: __debug_loc
63 segname: __DWARF
64 addr: 0x0000000000000143
65 size: 0
66 offset: 0x000006D3
67 align: 0
68 reloff: 0x00000000
69 nreloc: 0
70 flags: 0x02000000
71 reserved1: 0x00000000
72 reserved2: 0x00000000
73 reserved3: 0x00000000
74 - sectname: __debug_abbrev
75 segname: __DWARF
76 addr: 0x0000000000000143
77 size: 104
78 offset: 0x000006D3
79 align: 0
80 reloff: 0x00000000
81 nreloc: 0
82 flags: 0x02000000
83 reserved1: 0x00000000
84 reserved2: 0x00000000
85 reserved3: 0x00000000
86 - sectname: __debug_info
87 segname: __DWARF
88 addr: 0x00000000000001AB
89 size: 146
90 offset: 0x0000073B
91 align: 0
92 reloff: 0x000009E8
93 nreloc: 2
94 flags: 0x02000000
95 reserved1: 0x00000000
96 reserved2: 0x00000000
97 reserved3: 0x00000000
98 - sectname: __debug_ranges
99 segname: __DWARF
100 addr: 0x000000000000023D
101 size: 0
102 offset: 0x000007CD
103 align: 0
104 reloff: 0x00000000
105 nreloc: 0
106 flags: 0x02000000
107 reserved1: 0x00000000
108 reserved2: 0x00000000
109 reserved3: 0x00000000
110 - sectname: __debug_macinfo
111 segname: __DWARF
112 addr: 0x000000000000023D
113 size: 1
114 offset: 0x000007CD
115 align: 0
116 reloff: 0x00000000
117 nreloc: 0
118 flags: 0x02000000
119 reserved1: 0x00000000
120 reserved2: 0x00000000
121 reserved3: 0x00000000
122 - sectname: __apple_names
123 segname: __DWARF
124 addr: 0x000000000000023E
125 size: 60
126 offset: 0x000007CE
127 align: 0
128 reloff: 0x00000000
129 nreloc: 0
130 flags: 0x02000000
131 reserved1: 0x00000000
132 reserved2: 0x00000000
133 reserved3: 0x00000000
134 - sectname: __apple_objc
135 segname: __DWARF
136 addr: 0x000000000000027A
137 size: 36
138 offset: 0x0000080A
139 align: 0
140 reloff: 0x00000000
141 nreloc: 0
142 flags: 0x02000000
143 reserved1: 0x00000000
144 reserved2: 0x00000000
145 reserved3: 0x00000000
146 - sectname: __apple_namespac
147 segname: __DWARF
148 addr: 0x000000000000029E
149 size: 36
150 offset: 0x0000082E
151 align: 0
152 reloff: 0x00000000
153 nreloc: 0
154 flags: 0x02000000
155 reserved1: 0x00000000
156 reserved2: 0x00000000
157 reserved3: 0x00000000
158 - sectname: __apple_types
159 segname: __DWARF
160 addr: 0x00000000000002C2
161 size: 133
162 offset: 0x00000852
163 align: 0
164 reloff: 0x00000000
165 nreloc: 0
166 flags: 0x02000000
167 reserved1: 0x00000000
168 reserved2: 0x00000000
169 reserved3: 0x00000000
170 - sectname: __compact_unwind
171 segname: __LD
172 addr: 0x0000000000000348
173 size: 32
174 offset: 0x000008D8
175 align: 3
176 reloff: 0x000009F8
177 nreloc: 1
178 flags: 0x02000000
179 reserved1: 0x00000000
180 reserved2: 0x00000000
181 reserved3: 0x00000000
182 - sectname: __eh_frame
183 segname: __TEXT
184 addr: 0x0000000000000368
185 size: 64
186 offset: 0x000008F8
187 align: 3
188 reloff: 0x00000000
189 nreloc: 0
190 flags: 0x6800000B
191 reserved1: 0x00000000
192 reserved2: 0x00000000
193 reserved3: 0x00000000
194 - sectname: __debug_line
195 segname: __DWARF
196 addr: 0x00000000000003A8
197 size: 150
198 offset: 0x00000938
199 align: 0
200 reloff: 0x00000A00
201 nreloc: 1
202 flags: 0x02000000
203 reserved1: 0x00000000
204 reserved2: 0x00000000
205 reserved3: 0x00000000
206 - cmd: LC_VERSION_MIN_MACOSX
207 cmdsize: 16
208 version: 658432
209 sdk: 0
210 - cmd: LC_SYMTAB
211 cmdsize: 24
212 symoff: 2568
213 nsyms: 2
214 stroff: 2600
215 strsize: 36
216 - cmd: LC_DYSYMTAB
217 cmdsize: 80
218 ilocalsym: 0
219 nlocalsym: 0
220 iextdefsym: 0
221 nextdefsym: 1
222 iundefsym: 1
223 nundefsym: 1
224 tocoff: 0
225 ntoc: 0
226 modtaboff: 0
227 nmodtab: 0
228 extrefsymoff: 0
229 nextrefsyms: 0
230 indirectsymoff: 0
231 nindirectsyms: 0
232 extreloff: 0
233 nextrel: 0
234 locreloff: 0
235 nlocrel: 0
236 LinkEditData:
237 NameList:
238 - n_strx: 24
239 n_type: 0x0F
240 n_sect: 1
241 n_desc: 0
242 n_value: 0
243 - n_strx: 1
244 n_type: 0x01
245 n_sect: 0
246 n_desc: 0
247 n_value: 0
248 StringTable:
249 - ''
250 - _compilerrt_abort_impl
251 - ___absvdi2
252 - ''
253 DWARF:
254 debug_str:
255 - 'clang version 4.0.0 (trunk 290181) (llvm/trunk 290209)'
256 - ../compiler-rt/lib/builtins/absvdi2.c
257 - /Users/cbieneman/dev/open-source/llvm-build-rel
258 - int
259 - di_int
260 - long long int
261 - __absvdi2
262 - a
263 - N
264 - t
265 debug_abbrev:
266 - Code: 0x00000001
267 Tag: DW_TAG_compile_unit
268 Children: DW_CHILDREN_yes
269 Attributes:
270 - Attribute: DW_AT_producer
271 Form: DW_FORM_strp
272 - Attribute: DW_AT_language
273 Form: DW_FORM_data2
274 - Attribute: DW_AT_name
275 Form: DW_FORM_strp
276 - Attribute: DW_AT_stmt_list
277 Form: DW_FORM_sec_offset
278 - Attribute: DW_AT_comp_dir
279 Form: DW_FORM_strp
280 - Attribute: DW_AT_low_pc
281 Form: DW_FORM_addr
282 - Attribute: DW_AT_high_pc
283 Form: DW_FORM_data4
284 - Code: 0x00000002
285 Tag: DW_TAG_base_type
286 Children: DW_CHILDREN_no
287 Attributes:
288 - Attribute: DW_AT_name
289 Form: DW_FORM_strp
290 - Attribute: DW_AT_encoding
291 Form: DW_FORM_data1
292 - Attribute: DW_AT_byte_size
293 Form: DW_FORM_data1
294 - Code: 0x00000003
295 Tag: DW_TAG_typedef
296 Children: DW_CHILDREN_no
297 Attributes:
298 - Attribute: DW_AT_type
299 Form: DW_FORM_ref4
300 - Attribute: DW_AT_name
301 Form: DW_FORM_strp
302 - Attribute: DW_AT_decl_file
303 Form: DW_FORM_data1
304 - Attribute: DW_AT_decl_line
305 Form: DW_FORM_data1
306 - Code: 0x00000004
307 Tag: DW_TAG_subprogram
308 Children: DW_CHILDREN_yes
309 Attributes:
310 - Attribute: DW_AT_low_pc
311 Form: DW_FORM_addr
312 - Attribute: DW_AT_high_pc
313 Form: DW_FORM_data4
314 - Attribute: DW_AT_frame_base
315 Form: DW_FORM_exprloc
316 - Attribute: DW_AT_name
317 Form: DW_FORM_strp
318 - Attribute: DW_AT_decl_file
319 Form: DW_FORM_data1
320 - Attribute: DW_AT_decl_line
321 Form: DW_FORM_data1
322 - Attribute: DW_AT_prototyped
323 Form: DW_FORM_flag_present
324 - Attribute: DW_AT_type
325 Form: DW_FORM_ref4
326 - Attribute: DW_AT_external
327 Form: DW_FORM_flag_present
328 - Code: 0x00000005
329 Tag: DW_TAG_formal_parameter
330 Children: DW_CHILDREN_no
331 Attributes:
332 - Attribute: DW_AT_location
333 Form: DW_FORM_exprloc
334 - Attribute: DW_AT_name
335 Form: DW_FORM_strp
336 - Attribute: DW_AT_decl_file
337 Form: DW_FORM_data1
338 - Attribute: DW_AT_decl_line
339 Form: DW_FORM_data1
340 - Attribute: DW_AT_type
341 Form: DW_FORM_ref4
342 - Code: 0x00000006
343 Tag: DW_TAG_variable
344 Children: DW_CHILDREN_no
345 Attributes:
346 - Attribute: DW_AT_location
347 Form: DW_FORM_exprloc
348 - Attribute: DW_AT_name
349 Form: DW_FORM_strp
350 - Attribute: DW_AT_decl_file
351 Form: DW_FORM_data1
352 - Attribute: DW_AT_decl_line
353 Form: DW_FORM_data1
354 - Attribute: DW_AT_type
355 Form: DW_FORM_ref4
356 - Code: 0x00000007
357 Tag: DW_TAG_const_type
358 Children: DW_CHILDREN_no
359 Attributes:
360 - Attribute: DW_AT_type
361 Form: DW_FORM_ref4
362 ...
363
364 #CHECK: DWARF:
365 #CHECK: debug_str:
366 #CHECK: - 'clang version 4.0.0 (trunk 290181) (llvm/trunk 290209)'
367 #CHECK: - ../compiler-rt/lib/builtins/absvdi2.c
368 #CHECK: - /Users/cbieneman/dev/open-source/llvm-build-rel
369 #CHECK: - int
370 #CHECK: - di_int
371 #CHECK: - long long int
372 #CHECK: - __absvdi2
373 #CHECK: - a
374 #CHECK: - N
375 #CHECK: - t
376 #CHECK: debug_abbrev:
377 #CHECK: - Code: 0x00000001
378 #CHECK: Tag: DW_TAG_compile_unit
379 #CHECK: Children: DW_CHILDREN_yes
380 #CHECK: Attributes:
381 #CHECK: - Attribute: DW_AT_producer
382 #CHECK: Form: DW_FORM_strp
383 #CHECK: - Attribute: DW_AT_language
384 #CHECK: Form: DW_FORM_data2
385 #CHECK: - Attribute: DW_AT_name
386 #CHECK: Form: DW_FORM_strp
387 #CHECK: - Attribute: DW_AT_stmt_list
388 #CHECK: Form: DW_FORM_sec_offset
389 #CHECK: - Attribute: DW_AT_comp_dir
390 #CHECK: Form: DW_FORM_strp
391 #CHECK: - Attribute: DW_AT_low_pc
392 #CHECK: Form: DW_FORM_addr
393 #CHECK: - Attribute: DW_AT_high_pc
394 #CHECK: Form: DW_FORM_data4
395 #CHECK: - Code: 0x00000002
396 #CHECK: Tag: DW_TAG_base_type
397 #CHECK: Children: DW_CHILDREN_no
398 #CHECK: Attributes:
399 #CHECK: - Attribute: DW_AT_name
400 #CHECK: Form: DW_FORM_strp
401 #CHECK: - Attribute: DW_AT_encoding
402 #CHECK: Form: DW_FORM_data1
403 #CHECK: - Attribute: DW_AT_byte_size
404 #CHECK: Form: DW_FORM_data1
405 #CHECK: - Code: 0x00000003
406 #CHECK: Tag: DW_TAG_typedef
407 #CHECK: Children: DW_CHILDREN_no
408 #CHECK: Attributes:
409 #CHECK: - Attribute: DW_AT_type
410 #CHECK: Form: DW_FORM_ref4
411 #CHECK: - Attribute: DW_AT_name
412 #CHECK: Form: DW_FORM_strp
413 #CHECK: - Attribute: DW_AT_decl_file
414 #CHECK: Form: DW_FORM_data1
415 #CHECK: - Attribute: DW_AT_decl_line
416 #CHECK: Form: DW_FORM_data1
417 #CHECK: - Code: 0x00000004
418 #CHECK: Tag: DW_TAG_subprogram
419 #CHECK: Children: DW_CHILDREN_yes
420 #CHECK: Attributes:
421 #CHECK: - Attribute: DW_AT_low_pc
422 #CHECK: Form: DW_FORM_addr
423 #CHECK: - Attribute: DW_AT_high_pc
424 #CHECK: Form: DW_FORM_data4
425 #CHECK: - Attribute: DW_AT_frame_base
426 #CHECK: Form: DW_FORM_exprloc
427 #CHECK: - Attribute: DW_AT_name
428 #CHECK: Form: DW_FORM_strp
429 #CHECK: - Attribute: DW_AT_decl_file
430 #CHECK: Form: DW_FORM_data1
431 #CHECK: - Attribute: DW_AT_decl_line
432 #CHECK: Form: DW_FORM_data1
433 #CHECK: - Attribute: DW_AT_prototyped
434 #CHECK: Form: DW_FORM_flag_present
435 #CHECK: - Attribute: DW_AT_type
436 #CHECK: Form: DW_FORM_ref4
437 #CHECK: - Attribute: DW_AT_external
438 #CHECK: Form: DW_FORM_flag_present
439 #CHECK: - Code: 0x00000005
440 #CHECK: Tag: DW_TAG_formal_parameter
441 #CHECK: Children: DW_CHILDREN_no
442 #CHECK: Attributes:
443 #CHECK: - Attribute: DW_AT_location
444 #CHECK: Form: DW_FORM_exprloc
445 #CHECK: - Attribute: DW_AT_name
446 #CHECK: Form: DW_FORM_strp
447 #CHECK: - Attribute: DW_AT_decl_file
448 #CHECK: Form: DW_FORM_data1
449 #CHECK: - Attribute: DW_AT_decl_line
450 #CHECK: Form: DW_FORM_data1
451 #CHECK: - Attribute: DW_AT_type
452 #CHECK: Form: DW_FORM_ref4
453 #CHECK: - Code: 0x00000006
454 #CHECK: Tag: DW_TAG_variable
455 #CHECK: Children: DW_CHILDREN_no
456 #CHECK: Attributes:
457 #CHECK: - Attribute: DW_AT_location
458 #CHECK: Form: DW_FORM_exprloc
459 #CHECK: - Attribute: DW_AT_name
460 #CHECK: Form: DW_FORM_strp
461 #CHECK: - Attribute: DW_AT_decl_file
462 #CHECK: Form: DW_FORM_data1
463 #CHECK: - Attribute: DW_AT_decl_line
464 #CHECK: Form: DW_FORM_data1
465 #CHECK: - Attribute: DW_AT_type
466 #CHECK: Form: DW_FORM_ref4
467 #CHECK: - Code: 0x00000007
468 #CHECK: Tag: DW_TAG_const_type
469 #CHECK: Children: DW_CHILDREN_no
470 #CHECK: Attributes:
471 #CHECK: - Attribute: DW_AT_type
472 #CHECK: Form: DW_FORM_ref4
473 #CHECK: ...
0 # RUN: yaml2obj %s | obj2yaml | FileCheck %s
1
2 --- !mach-o
3 IsLittleEndian: true
4 FileHeader:
5 magic: 0xFEEDFACF
6 cputype: 0x01000007
7 cpusubtype: 0x00000003
8 filetype: 0x00000001
9 ncmds: 4
10 sizeofcmds: 512
11 flags: 0x00002000
12 reserved: 0x00000000
13 LoadCommands:
14 - cmd: LC_SEGMENT_64
15 cmdsize: 392
16 segname: ''
17 vmaddr: 0
18 vmsize: 240
19 fileoff: 544
20 filesize: 240
21 maxprot: 7
22 initprot: 7
23 nsects: 4
24 flags: 0
25 Sections:
26 - sectname: __text
27 segname: __TEXT
28 addr: 0x0000000000000000
29 size: 93
30 offset: 0x00000220
31 align: 4
32 reloff: 0x00000310
33 nreloc: 3
34 flags: 0x80000400
35 reserved1: 0x00000000
36 reserved2: 0x00000000
37 reserved3: 0x00000000
38 - sectname: __cstring
39 segname: __TEXT
40 addr: 0x000000000000005D
41 size: 48
42 offset: 0x0000027D
43 align: 0
44 reloff: 0x00000000
45 nreloc: 0
46 flags: 0x00000002
47 reserved1: 0x00000000
48 reserved2: 0x00000000
49 reserved3: 0x00000000
50 - sectname: __compact_unwind
51 segname: __LD
52 addr: 0x0000000000000090
53 size: 32
54 offset: 0x000002B0
55 align: 3
56 reloff: 0x00000328
57 nreloc: 1
58 flags: 0x02000000
59 reserved1: 0x00000000
60 reserved2: 0x00000000
61 reserved3: 0x00000000
62 - sectname: __eh_frame
63 segname: __TEXT
64 addr: 0x00000000000000B0
65 size: 64
66 offset: 0x000002D0
67 align: 3
68 reloff: 0x00000000
69 nreloc: 0
70 flags: 0x6800000B
71 reserved1: 0x00000000
72 reserved2: 0x00000000
73 reserved3: 0x00000000
74 - cmd: LC_VERSION_MIN_MACOSX
75 cmdsize: 16
76 version: 658432
77 sdk: 0
78 - cmd: LC_SYMTAB
79 cmdsize: 24
80 symoff: 816
81 nsyms: 2
82 stroff: 848
83 strsize: 36
84 - cmd: LC_DYSYMTAB
85 cmdsize: 80
86 ilocalsym: 0
87 nlocalsym: 0
88 iextdefsym: 0
89 nextdefsym: 1
90 iundefsym: 1
91 nundefsym: 1
92 tocoff: 0
93 ntoc: 0
94 modtaboff: 0
95 nmodtab: 0
96 extrefsymoff: 0
97 nextrefsyms: 0
98 indirectsymoff: 0
99 nindirectsyms: 0
100 extreloff: 0
101 nextrel: 0
102 locreloff: 0
103 nlocrel: 0
104 LinkEditData:
105 NameList:
106 - n_strx: 24
107 n_type: 0x0F
108 n_sect: 1
109 n_desc: 0
110 n_value: 0
111 - n_strx: 1
112 n_type: 0x01
113 n_sect: 0
114 n_desc: 0
115 n_value: 0
116 StringTable:
117 - ''
118 - _compilerrt_abort_impl
119 - ___absvdi2
120 - ''
121 ...
122
123 #CHECK: FileHeader:
124 #CHECK: magic: 0xFEEDFACF
125 #CHECK: cputype: 0x01000007
126 #CHECK: cpusubtype: 0x00000003
127 #CHECK: filetype: 0x00000001
128 #CHECK: ncmds: 4
129 #CHECK: sizeofcmds: 512
130 #CHECK: flags: 0x00002000
131 #CHECK: reserved: 0x00000000
3535 void dumpExportTrie(std::unique_ptr &Y);
3636 void dumpSymbols(std::unique_ptr &Y);
3737 void dumpDebugAbbrev(DWARFContextInMemory &DCtx,
38 std::unique_ptr &Y);
38 std::unique_ptr &Y);
3939 void dumpDebugStrings(DWARFContextInMemory &DCtx,
4040 std::unique_ptr &Y);
4141
164164
165165 Expected> MachODumper::dump() {
166166 auto Y = make_unique();
167 Y->IsLittleEndian = Obj.isLittleEndian();
167168 dumpHeader(Y);
168169 dumpLoadCommands(Y);
169170 dumpLinkEdit(Y);
170171
171172 DWARFContextInMemory DICtx(Obj);
172 if(auto Err = dwarf2yaml(DICtx, Y->DWARF))
173 if (auto Err = dwarf2yaml(DICtx, Y->DWARF))
173174 return errorCodeToError(Err);
174175 return std::move(Y);
175176 }
450451
451452 for (auto Symbol : Obj.symbols()) {
452453 MachOYAML::NListEntry NLE =
453 Obj.is64Bit() ? constructNameList(
454 *reinterpret_cast(
455 Symbol.getRawDataRefImpl().p))
456 : constructNameList(
457 *reinterpret_cast(
458 Symbol.getRawDataRefImpl().p));
454 Obj.is64Bit()
455 ? constructNameList(
456 Obj.getSymbol64TableEntry(Symbol.getRawDataRefImpl()))
457 : constructNameList(
458 Obj.getSymbolTableEntry(Symbol.getRawDataRefImpl()));
459459 LEData.NameList.push_back(NLE);
460460 }
461461
528528 }
529529 return obj2yaml_error::success;
530530 }
531
531
532532 return obj2yaml_error::unsupported_obj_file_format;
533533 }
1515 #include "llvm/Support/Error.h"
1616 #include "llvm/Support/LEB128.h"
1717 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Support/SwapByteOrder.h"
1819
1920 using namespace llvm;
21
22 template
23 void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
24 if (IsLittleEndian != sys::IsLittleEndianHost)
25 sys::swapByteOrder(Integer);
26 OS.write(reinterpret_cast(&Integer), sizeof(T));
27 }
28
29 void writeVariableSizedInteger(uint64_t Integer, size_t Size, raw_ostream &OS,
30 bool IsLittleEndian) {
31 if (8 == Size)
32 writeInteger((uint64_t)Integer, OS, IsLittleEndian);
33 else if (4 == Size)
34 writeInteger((uint32_t)Integer, OS, IsLittleEndian);
35 else if (2 == Size)
36 writeInteger((uint16_t)Integer, OS, IsLittleEndian);
37 else if (1 == Size)
38 writeInteger((uint8_t)Integer, OS, IsLittleEndian);
39 else
40 assert(false && "Invalid integer write size.");
41 }
2042
2143 void ZeroFillBytes(raw_ostream &OS, size_t Size) {
2244 std::vector FillData;
4870 void yaml2debug_aranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
4971 for (auto Range : DI.ARanges) {
5072 auto HeaderStart = OS.tell();
51 OS.write(reinterpret_cast(&Range.Length), 4);
52 OS.write(reinterpret_cast(&Range.Version), 2);
53 OS.write(reinterpret_cast(&Range.CuOffset), 4);
54 OS.write(reinterpret_cast(&Range.AddrSize), 1);
55 OS.write(reinterpret_cast(&Range.SegSize), 1);
73 writeInteger((uint32_t)Range.Length, OS, DI.IsLittleEndian);
74 writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
75 writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
76 writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
77 writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
5678
5779 auto HeaderSize = OS.tell() - HeaderStart;
5880 auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2);
5981 ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
6082
6183 for (auto Descriptor : Range.Descriptors) {
62 OS.write(reinterpret_cast(&Descriptor.Address), Range.AddrSize);
63 OS.write(reinterpret_cast(&Descriptor.Length), Range.AddrSize);
84 writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS,
85 DI.IsLittleEndian);
86 writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
87 DI.IsLittleEndian);
6488 }
6589 ZeroFillBytes(OS, Range.AddrSize * 2);
6690 }
6791 }
6892
69 void yaml2pubsection(raw_ostream &OS, const DWARFYAML::PubSection &Sect) {
70 OS.write(reinterpret_cast(&Sect.Length), 4);
71 OS.write(reinterpret_cast(&Sect.Version), 2);
72 OS.write(reinterpret_cast(&Sect.UnitOffset), 4);
73 OS.write(reinterpret_cast(&Sect.UnitSize), 4);
93 void yaml2pubsection(raw_ostream &OS, const DWARFYAML::PubSection &Sect,
94 bool IsLittleEndian) {
95 writeInteger((uint32_t)Sect.Length, OS, IsLittleEndian);
96 writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
97 writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
98 writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
7499 for (auto Entry : Sect.Entries) {
75 OS.write(reinterpret_cast(&Entry.DieOffset), 4);
100 writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
76101 if (Sect.IsGNUStyle)
77 OS.write(reinterpret_cast(&Entry.Descriptor), 4);
102 writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
78103 OS.write(Entry.Name.data(), Entry.Name.size());
79104 OS.write('\0');
80105 }
81 }
106 }
4040 Error writeLoadCommands(raw_ostream &OS);
4141 Error writeSectionData(raw_ostream &OS);
4242 Error writeLinkEditData(raw_ostream &OS);
43 Error writeDWARFData(raw_ostream &OS,
44 std::vector &Sections);
43
4544 void writeBindOpcodes(raw_ostream &OS,
4645 std::vector &BindOpcodes);
4746 // LinkEdit writers
8382 Header.sizeofcmds = Obj.Header.sizeofcmds;
8483 Header.flags = Obj.Header.flags;
8584 Header.reserved = Obj.Header.reserved;
85
86 if (Obj.IsLittleEndian != sys::IsLittleEndianHost)
87 MachO::swapStruct(Header);
8688
8789 auto header_size =
8890 is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
109111 }
110112
111113 template
112 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC, raw_ostream &OS) {
114 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC, raw_ostream &OS,
115 bool IsLittleEndian) {
113116 return 0;
114117 }
115118
116119 template <>
117120 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC,
118 raw_ostream &OS) {
121 raw_ostream &OS,
122 bool IsLittleEndian) {
119123 size_t BytesWritten = 0;
120124 for (const auto &Sec : LC.Sections) {
121125 auto TempSec = constructSection(Sec);
126 if (IsLittleEndian != sys::IsLittleEndianHost)
127 MachO::swapStruct(TempSec);
122128 OS.write(reinterpret_cast(&(TempSec)),
123129 sizeof(MachO::section));
124130 BytesWritten += sizeof(MachO::section);
127133 }
128134
129135 template <>
130 size_t
131 writeLoadCommandData(MachOYAML::LoadCommand &LC,
132 raw_ostream &OS) {
136 size_t writeLoadCommandData(
137 MachOYAML::LoadCommand &LC, raw_ostream &OS, bool IsLittleEndian) {
133138 size_t BytesWritten = 0;
134139 for (const auto &Sec : LC.Sections) {
135140 auto TempSec = constructSection(Sec);
136141 TempSec.reserved3 = Sec.reserved3;
142 if (IsLittleEndian != sys::IsLittleEndianHost)
143 MachO::swapStruct(TempSec);
137144 OS.write(reinterpret_cast(&(TempSec)),
138145 sizeof(MachO::section_64));
139146 BytesWritten += sizeof(MachO::section_64);
152159
153160 template <>
154161 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC,
155 raw_ostream &OS) {
162 raw_ostream &OS,
163 bool IsLittleEndian) {
156164 return writePayloadString(LC, OS);
157165 }
158166
159167 template <>
160168 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC,
161 raw_ostream &OS) {
169 raw_ostream &OS,
170 bool IsLittleEndian) {
162171 return writePayloadString(LC, OS);
163172 }
164173
165174 template <>
166175 size_t writeLoadCommandData(MachOYAML::LoadCommand &LC,
167 raw_ostream &OS) {
176 raw_ostream &OS,
177 bool IsLittleEndian) {
168178 return writePayloadString(LC, OS);
169179 }
170180
189199 Error MachOWriter::writeLoadCommands(raw_ostream &OS) {
190200 for (auto &LC : Obj.LoadCommands) {
191201 size_t BytesWritten = 0;
202 llvm::MachO::macho_load_command Data = LC.Data;
203
192204 #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
193205 case MachO::LCName: \
194 OS.write(reinterpret_cast(&(LC.Data.LCStruct##_data)), \
206 if (Obj.IsLittleEndian != sys::IsLittleEndianHost) \
207 MachO::swapStruct(Data.LCStruct##_data); \
208 OS.write(reinterpret_cast(&(Data.LCStruct##_data)), \
195209 sizeof(MachO::LCStruct)); \
196210 BytesWritten = sizeof(MachO::LCStruct); \
197 BytesWritten += writeLoadCommandData(LC, OS); \
211 BytesWritten += \
212 writeLoadCommandData(LC, OS, Obj.IsLittleEndian); \
198213 break;
199214
200215 switch (LC.Data.load_command_data.cmd) {
201216 default:
202 OS.write(reinterpret_cast(&(LC.Data.load_command_data)),
217 if (Obj.IsLittleEndian != sys::IsLittleEndianHost)
218 MachO::swapStruct(Data.load_command_data);
219 OS.write(reinterpret_cast(&(Data.load_command_data)),
203220 sizeof(MachO::load_command));
204221 BytesWritten = sizeof(MachO::load_command);
205 BytesWritten += writeLoadCommandData(LC, OS);
222 BytesWritten +=
223 writeLoadCommandData(LC, OS, Obj.IsLittleEndian);
206224 break;
207225 #include "llvm/Support/MachO.def"
208226 }
229247 }
230248
231249 Error MachOWriter::writeSectionData(raw_ostream &OS) {
250 bool FoundLinkEditSeg = false;
232251 for (auto &LC : Obj.LoadCommands) {
233252 switch (LC.Data.load_command_data.cmd) {
234253 case MachO::LC_SEGMENT:
235254 case MachO::LC_SEGMENT_64:
236 auto currOffset = OS.tell() - fileStart;
237 auto segname = LC.Data.segment_command_data.segname;
238255 uint64_t segOff = is64Bit ? LC.Data.segment_command_64_data.fileoff
239256 : LC.Data.segment_command_data.fileoff;
240
241 if (0 == strncmp(&segname[0], "__LINKEDIT", 16)) {
257 if (0 == strncmp(&LC.Data.segment_command_data.segname[0], "__LINKEDIT", 16)) {
258 FoundLinkEditSeg = true;
242259 if (auto Err = writeLinkEditData(OS))
243260 return Err;
244 } else if (0 == strncmp(&segname[0], "__DWARF", 16)) {
245 if (auto Err = writeDWARFData(OS, LC.Sections))
246 return Err;
247 } else {
261 }
262 for (auto &Sec : LC.Sections) {
263 ZeroToOffset(OS, Sec.offset);
248264 // Zero Fill any data between the end of the last thing we wrote and the
249265 // start of this section.
250 if (currOffset < segOff) {
251 ZeroFillBytes(OS, segOff - currOffset);
252 }
253
254 for (auto &Sec : LC.Sections) {
255 // Zero Fill any data between the end of the last thing we wrote and
256 // the
257 // start of this section.
258 assert(
259 (OS.tell() - fileStart <= Sec.offset ||
260 Sec.offset == (uint32_t)0) &&
261 "Wrote too much data somewhere, section offsets don't line up.");
262 currOffset = OS.tell() - fileStart;
263 if (currOffset < Sec.offset) {
264 ZeroFillBytes(OS, Sec.offset - currOffset);
266 assert((OS.tell() - fileStart <= Sec.offset ||
267 Sec.offset == (uint32_t)0) &&
268 "Wrote too much data somewhere, section offsets don't line up.");
269 if (0 == strncmp(&Sec.segname[0], "__DWARF", 16)) {
270 if (0 == strncmp(&Sec.sectname[0], "__debug_str", 16)) {
271 yaml2debug_str(OS, Obj.DWARF);
272 } else if (0 == strncmp(&Sec.sectname[0], "__debug_abbrev", 16)) {
273 yaml2debug_abbrev(OS, Obj.DWARF);
274 } else if (0 == strncmp(&Sec.sectname[0], "__debug_aranges", 16)) {
275 yaml2debug_aranges(OS, Obj.DWARF);
276 } else if (0 == strncmp(&Sec.sectname[0], "__debug_pubnames", 16)) {
277 yaml2pubsection(OS, Obj.DWARF.PubNames, Obj.IsLittleEndian);
278 } else if (0 == strncmp(&Sec.sectname[0], "__debug_pubtypes", 16)) {
279 yaml2pubsection(OS, Obj.DWARF.PubTypes, Obj.IsLittleEndian);
265280 }
266
281 } else {
267282 // Fills section data with 0xDEADBEEF
268283 Fill(OS, Sec.size, 0xDEADBEEFu);
269284 }
273288 ZeroToOffset(OS, segOff + segSize);
274289 break;
275290 }
291 }
292 // Old PPC Object Files didn't have __LINKEDIT segments, the data was just
293 // stuck at the end of the file.
294 if (!FoundLinkEditSeg) {
295 if (auto Err = writeLinkEditData(OS))
296 return Err;
276297 }
277298 return Error::success();
278299 }
327348 }
328349
329350 template
330 void writeNListEntry(MachOYAML::NListEntry &NLE, raw_ostream &OS) {
351 void writeNListEntry(MachOYAML::NListEntry &NLE, raw_ostream &OS,
352 bool IsLittleEndian) {
331353 NListType ListEntry;
332354 ListEntry.n_strx = NLE.n_strx;
333355 ListEntry.n_type = NLE.n_type;
334356 ListEntry.n_sect = NLE.n_sect;
335357 ListEntry.n_desc = NLE.n_desc;
336358 ListEntry.n_value = NLE.n_value;
359
360 if (sys::IsBigEndianHost)
361 MachO::swapStruct(ListEntry);
337362 OS.write(reinterpret_cast(&ListEntry), sizeof(NListType));
338363 }
339364
383408 return Error::success();
384409 }
385410
386 Error MachOWriter::writeDWARFData(raw_ostream &OS,
387 std::vector &Sections) {
388 for (auto Section : Sections) {
389 ZeroToOffset(OS, Section.offset);
390 if (0 == strncmp(&Section.sectname[0], "__debug_str", 16)) {
391 yaml2debug_str(OS, Obj.DWARF);
392 } else if (0 == strncmp(&Section.sectname[0], "__debug_abbrev", 16)) {
393 yaml2debug_abbrev(OS, Obj.DWARF);
394 } else if (0 == strncmp(&Section.sectname[0], "__debug_aranges", 16)) {
395 yaml2debug_aranges(OS, Obj.DWARF);
396 } else if (0 == strncmp(&Section.sectname[0], "__debug_pubnames", 16)) {
397 yaml2pubsection(OS, Obj.DWARF.PubNames);
398 } else if (0 == strncmp(&Section.sectname[0], "__debug_pubtypes", 16)) {
399 yaml2pubsection(OS, Obj.DWARF.PubTypes);
400 }
401 }
402 return Error::success();
403 }
404
405411 Error MachOWriter::writeRebaseOpcodes(raw_ostream &OS) {
406412 MachOYAML::LinkEditData &LinkEdit = Obj.LinkEdit;
407413
433439 Error MachOWriter::writeNameList(raw_ostream &OS) {
434440 for (auto NLE : Obj.LinkEdit.NameList) {
435441 if (is64Bit)
436 writeNListEntry(NLE, OS);
442 writeNListEntry(NLE, OS, Obj.IsLittleEndian);
437443 else
438 writeNListEntry(NLE, OS);
444 writeNListEntry(NLE, OS, Obj.IsLittleEndian);
439445 }
440446 return Error::success();
441447 }
3737 int yaml2elf(llvm::ELFYAML::Object &Doc, llvm::raw_ostream &Out);
3838 int yaml2macho(llvm::yaml::YamlObjectFile &Doc, llvm::raw_ostream &Out);
3939
40 void yaml2debug_abbrev(llvm::raw_ostream &OS,
41 const llvm::DWARFYAML::Data &DI);
42 void yaml2debug_str(llvm::raw_ostream &OS,
43 const llvm::DWARFYAML::Data &DI);
40 void yaml2debug_abbrev(llvm::raw_ostream &OS, const llvm::DWARFYAML::Data &DI);
41 void yaml2debug_str(llvm::raw_ostream &OS, const llvm::DWARFYAML::Data &DI);
4442
4543 void yaml2debug_aranges(llvm::raw_ostream &OS, const llvm::DWARFYAML::Data &DI);
4644 void yaml2pubsection(llvm::raw_ostream &OS,
47 const llvm::DWARFYAML::PubSection &Sect);
45 const llvm::DWARFYAML::PubSection &Sect,
46 bool IsLittleEndian);
4847
4948 #endif