]> git.saurik.com Git - apple/ld64.git/blob - src/abstraction/MachOFileAbstraction.hpp
80a4e812dc1d1d107fae813e50727bdf94b9649a
[apple/ld64.git] / src / abstraction / MachOFileAbstraction.hpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2005-2010 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 #ifndef __MACH_O_FILE_ABSTRACTION__
25 #define __MACH_O_FILE_ABSTRACTION__
26
27 #include <mach-o/loader.h>
28 #include <mach-o/nlist.h>
29 #include <mach-o/reloc.h>
30 #include <mach-o/fat.h>
31 #include <mach-o/stab.h>
32 #include <mach-o/reloc.h>
33 #include <mach-o/x86_64/reloc.h>
34 #include <mach-o/compact_unwind_encoding.h>
35 #include <mach/machine.h>
36 #include <stddef.h>
37
38 #include "FileAbstraction.hpp"
39
40 #include "configure.h"
41
42 // stuff that will eventually go away once newer cctools headers are widespread
43 #ifndef LC_LOAD_UPWARD_DYLIB
44 #define LC_LOAD_UPWARD_DYLIB (0x23|LC_REQ_DYLD) /* load of dylib whose initializers run later */
45 #endif
46
47 #ifndef CPU_SUBTYPE_ARM_V5TEJ
48 #define CPU_SUBTYPE_ARM_V5TEJ ((cpu_subtype_t) 7)
49 #endif
50 #ifndef CPU_SUBTYPE_ARM_XSCALE
51 #define CPU_SUBTYPE_ARM_XSCALE ((cpu_subtype_t) 8)
52 #endif
53 #ifndef CPU_SUBTYPE_ARM_V7
54 #define CPU_SUBTYPE_ARM_V7 ((cpu_subtype_t) 9)
55 #endif
56
57 #ifndef N_ARM_THUMB_DEF
58 #define N_ARM_THUMB_DEF 0x0008
59 #endif
60 #ifndef MH_DEAD_STRIPPABLE_DYLIB
61 #define MH_DEAD_STRIPPABLE_DYLIB 0x400000
62 #endif
63 #ifndef MH_KEXT_BUNDLE
64 #define MH_KEXT_BUNDLE 11
65 #endif
66 #ifndef LC_DYLD_INFO
67 #define LC_DYLD_INFO 0x22 /* compressed dyld information */
68 #define LC_DYLD_INFO_ONLY (0x22|LC_REQ_DYLD) /* compressed dyld information only */
69
70 struct dyld_info_command {
71 uint32_t cmd; /* LC_DYLD_INFO or LC_DYLD_INFO_ONLY */
72 uint32_t cmdsize; /* sizeof(struct dyld_info_command) */
73 uint32_t rebase_off; /* file offset to rebase info */
74 uint32_t rebase_size; /* size of rebase info */
75 uint32_t bind_off; /* file offset to binding info */
76 uint32_t bind_size; /* size of binding info */
77 uint32_t weak_bind_off; /* file offset to weak binding info */
78 uint32_t weak_bind_size; /* size of weak binding info */
79 uint32_t lazy_bind_off; /* file offset to lazy binding info */
80 uint32_t lazy_bind_size; /* size of lazy binding infs */
81 uint32_t export_off; /* file offset to lazy binding info */
82 uint32_t export_size; /* size of lazy binding infs */
83 };
84
85 #define REBASE_TYPE_POINTER 1
86 #define REBASE_TYPE_TEXT_ABSOLUTE32 2
87 #define REBASE_TYPE_TEXT_PCREL32 3
88
89 #define REBASE_OPCODE_MASK 0xF0
90 #define REBASE_IMMEDIATE_MASK 0x0F
91 #define REBASE_OPCODE_DONE 0x00
92 #define REBASE_OPCODE_SET_TYPE_IMM 0x10
93 #define REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x20
94 #define REBASE_OPCODE_ADD_ADDR_ULEB 0x30
95 #define REBASE_OPCODE_ADD_ADDR_IMM_SCALED 0x40
96 #define REBASE_OPCODE_DO_REBASE_IMM_TIMES 0x50
97 #define REBASE_OPCODE_DO_REBASE_ULEB_TIMES 0x60
98 #define REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB 0x70
99 #define REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB 0x80
100
101 #define BIND_TYPE_POINTER 1
102 #define BIND_TYPE_TEXT_ABSOLUTE32 2
103 #define BIND_TYPE_TEXT_PCREL32 3
104
105 #define BIND_SPECIAL_DYLIB_SELF 0
106 #define BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE -1
107 #define BIND_SPECIAL_DYLIB_FLAT_LOOKUP -2
108
109 #define BIND_SYMBOL_FLAGS_WEAK_IMPORT 0x1
110 #define BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION 0x8
111
112 #define BIND_OPCODE_MASK 0xF0
113 #define BIND_IMMEDIATE_MASK 0x0F
114 #define BIND_OPCODE_DONE 0x00
115 #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10
116 #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20
117 #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30
118 #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40
119 #define BIND_OPCODE_SET_TYPE_IMM 0x50
120 #define BIND_OPCODE_SET_ADDEND_SLEB 0x60
121 #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70
122 #define BIND_OPCODE_ADD_ADDR_ULEB 0x80
123 #define BIND_OPCODE_DO_BIND 0x90
124 #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xA0
125 #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xB0
126 #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xC0
127
128 #define EXPORT_SYMBOL_FLAGS_KIND_MASK 0x03
129 #define EXPORT_SYMBOL_FLAGS_KIND_REGULAR 0x00
130 #define EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL 0x01
131 #define EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION 0x04
132 #define EXPORT_SYMBOL_FLAGS_INDIRECT_DEFINITION 0x08
133 #define EXPORT_SYMBOL_FLAGS_HAS_SPECIALIZATIONS 0x10
134
135 #endif
136
137 #ifndef S_THREAD_LOCAL_REGULAR
138 #define S_THREAD_LOCAL_REGULAR 0x11
139 #endif
140
141 #ifndef S_THREAD_LOCAL_ZEROFILL
142 #define S_THREAD_LOCAL_ZEROFILL 0x12
143 #endif
144
145 #ifndef S_THREAD_LOCAL_VARIABLES
146 #define S_THREAD_LOCAL_VARIABLES 0x13
147 #endif
148
149 #ifndef S_THREAD_LOCAL_VARIABLE_POINTERS
150 #define S_THREAD_LOCAL_VARIABLE_POINTERS 0x14
151 #endif
152
153 #ifndef S_THREAD_LOCAL_INIT_FUNCTION_POINTERS
154 #define S_THREAD_LOCAL_INIT_FUNCTION_POINTERS 0x15
155 #endif
156
157 #ifndef MH_HAS_TLV_DESCRIPTORS
158 #define MH_HAS_TLV_DESCRIPTORS 0x800000
159 #endif
160
161 #ifndef X86_64_RELOC_TLV
162 #define X86_64_RELOC_TLV 9
163 #endif
164
165 #define GENERIC_RLEOC_TLV 5
166
167 #ifndef EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
168 #define EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER 0x10
169 #endif
170
171 #ifndef EXPORT_SYMBOL_FLAGS_REEXPORT
172 #define EXPORT_SYMBOL_FLAGS_REEXPORT 0x08
173 #endif
174
175 // type internal to linker
176 #define BIND_TYPE_OVERRIDE_OF_WEAKDEF_IN_DYLIB 0
177
178 #ifndef LC_VERSION_MIN_MACOSX
179 #define LC_VERSION_MIN_MACOSX 0x24
180 #define LC_VERSION_MIN_IPHONEOS 0x25
181
182 struct version_min_command {
183 uint32_t cmd; /* LC_VERSION_MIN_MACOSX or LC_VERSION_MIN_IPHONEOS */
184 uint32_t cmdsize; /* sizeof(struct min_version_command) */
185 uint32_t version; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
186 uint32_t reserved; /* zero */
187 };
188 #endif
189
190 #ifndef N_SYMBOL_RESOLVER
191 #define N_SYMBOL_RESOLVER 0x100
192 #endif
193
194 #ifndef LC_FUNCTION_STARTS
195 #define LC_FUNCTION_STARTS 0x26
196 #endif
197
198 #ifndef MH_NO_HEAP_EXECUTION
199 #define MH_NO_HEAP_EXECUTION 0x1000000
200 #endif
201
202 #ifndef LC_DYLD_ENVIRONMENT
203 #define LC_DYLD_ENVIRONMENT 0x27
204 #endif
205
206 #ifndef LC_DATA_IN_CODE
207 #define LC_DATA_IN_CODE 0x29 /* table of non-instructions in __text */
208 struct data_in_code_entry {
209 uint32_t offset;
210 uint16_t length;
211 uint16_t kind;
212 };
213 #endif
214
215 #ifndef LC_DYLIB_CODE_SIGN_DRS
216 #define LC_DYLIB_CODE_SIGN_DRS 0x2B
217 #endif
218
219 #ifndef CPU_SUBTYPE_ARM_V7F
220 #define CPU_SUBTYPE_ARM_V7F ((cpu_subtype_t) 10)
221 #endif
222 #ifndef CPU_SUBTYPE_ARM_V7K
223 #define CPU_SUBTYPE_ARM_V7K ((cpu_subtype_t) 12)
224 #endif
225
226
227 #ifndef LC_SOURCE_VERSION
228 #define LC_SOURCE_VERSION 0x2A
229 struct source_version_command {
230 uint32_t cmd; /* LC_SOURCE_VERSION */
231 uint32_t cmdsize; /* 16 */
232 uint64_t version; /* A.B.C.D.E packed as a24.b10.c10.d10.e10 */
233 };
234 #endif
235
236 #ifndef LC_MAIN
237 #define LC_MAIN (0x28|LC_REQ_DYLD) /* replacement for LC_UNIXTHREAD */
238 struct entry_point_command {
239 uint32_t cmd; /* LC_MAIN only used in MH_EXECUTE filetypes */
240 uint32_t cmdsize; /* 24 */
241 uint64_t entryoff; /* file (__TEXT) offset of main() */
242 uint64_t stacksize;/* if not zero, initial stack size */
243 };
244 #endif
245
246 #ifndef LC_DYLIB_CODE_SIGN_DRS
247 #define LC_DYLIB_CODE_SIGN_DRS 0x2B
248 #endif
249
250
251 struct ArchInfo {
252 const char* archName;
253 cpu_type_t cpuType;
254 cpu_subtype_t cpuSubType;
255 const char* llvmTriplePrefix;
256 const char* llvmTriplePrefixAlt;
257 bool isSubType;
258 bool supportsThumb2;
259 };
260
261 static const ArchInfo archInfoArray[] = {
262 #if SUPPORT_ARCH_x86_64
263 { "x86_64", CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL, "x86_64-", "", false, false },
264 #endif
265 #if SUPPORT_ARCH_i386
266 { "i386", CPU_TYPE_I386, CPU_SUBTYPE_I386_ALL, "i386-", "", false, false },
267 #endif
268 #if SUPPORT_ARCH_armv4t
269 { "armv4t", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V4T, "armv4t-", "", true, false },
270 #define SUPPORT_ARCH_arm_any 1
271 #endif
272 #if SUPPORT_ARCH_armv5
273 { "armv5", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V5TEJ, "armv5e-", "", true, false },
274 #define SUPPORT_ARCH_arm_any 1
275 #endif
276 #if SUPPORT_ARCH_armv6
277 { "armv6", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6, "armv6-", "", true, false },
278 #define SUPPORT_ARCH_arm_any 1
279 #endif
280 #if SUPPORT_ARCH_armv7
281 { "armv7", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7, "thumbv7-", "armv7-", true, true },
282 #define SUPPORT_ARCH_arm_any 1
283 #endif
284 { NULL, 0, 0, NULL, NULL, false, false }
285 };
286
287
288 // weird, but this include must wait until after SUPPORT_ARCH_arm_any is set up
289 #if SUPPORT_ARCH_arm_any
290 #include <mach-o/arm/reloc.h>
291 #endif
292
293 // hack until newer <mach-o/arm/reloc.h> everywhere
294 #define ARM_RELOC_HALF 8
295 #define ARM_RELOC_HALF_SECTDIFF 9
296
297
298
299 //
300 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
301 //
302
303
304
305 //
306 // mach-o file header
307 //
308 template <typename P> struct macho_header_content {};
309 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
310 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
311 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
312 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
313
314 template <typename P>
315 class macho_header {
316 public:
317 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
318 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
319
320 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
321 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
322
323 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
324 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
325
326 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
327 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
328
329 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
330 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
331
332 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
333 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
334
335 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
336 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
337
338 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
339 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
340
341 typedef typename P::E E;
342 private:
343 macho_header_content<P> header;
344 };
345
346
347 //
348 // mach-o load command
349 //
350 template <typename P>
351 class macho_load_command {
352 public:
353 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
354 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
355
356 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
357 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
358
359 typedef typename P::E E;
360 private:
361 load_command command;
362 };
363
364
365 //
366 // mach-o segment load command
367 //
368 template <typename P> struct macho_segment_content {};
369 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
370 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
371 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
372 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
373
374 template <typename P>
375 class macho_segment_command {
376 public:
377 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
378 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
379
380 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
381 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
382
383 const char* segname() const INLINE { return segment.fields.segname; }
384 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
385
386 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
387 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
388
389 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
390 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
391
392 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
393 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
394
395 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
396 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
397
398 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
399 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
400
401 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
402 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
403
404 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
405 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
406
407 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
408 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
409
410 enum {
411 CMD = macho_segment_content<P>::CMD
412 };
413
414 typedef typename P::E E;
415 private:
416 macho_segment_content<P> segment;
417 };
418
419
420 //
421 // mach-o section
422 //
423 template <typename P> struct macho_section_content {};
424 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
425 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
426 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
427 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
428
429 template <typename P>
430 class macho_section {
431 public:
432 const char* sectname() const INLINE { return section.fields.sectname; }
433 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
434
435 const char* segname() const INLINE { return section.fields.segname; }
436 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
437
438 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
439 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
440
441 uint64_t size() const INLINE { return P::getP(section.fields.size); }
442 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
443
444 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
445 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
446
447 uint32_t align() const INLINE { return E::get32(section.fields.align); }
448 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
449
450 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
451 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
452
453 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
454 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
455
456 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
457 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
458
459 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
460 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
461
462 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
463 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
464
465 typedef typename P::E E;
466 private:
467 macho_section_content<P> section;
468 };
469
470
471 //
472 // mach-o dylib load command
473 //
474 template <typename P>
475 class macho_dylib_command {
476 public:
477 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
478 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
479
480 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
481 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
482
483 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
484 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
485
486 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
487 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
488
489 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
490 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
491
492 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
493 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
494
495 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
496 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
497
498 typedef typename P::E E;
499 private:
500 dylib_command fields;
501 };
502
503
504 //
505 // mach-o dylinker load command
506 //
507 template <typename P>
508 class macho_dylinker_command {
509 public:
510 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
511 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
512
513 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
514 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
515
516 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
517 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
518
519 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
520 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
521
522 typedef typename P::E E;
523 private:
524 dylinker_command fields;
525 };
526
527
528 //
529 // mach-o sub_framework load command
530 //
531 template <typename P>
532 class macho_sub_framework_command {
533 public:
534 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
535 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
536
537 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
538 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
539
540 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
541 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
542
543 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
544 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
545
546 typedef typename P::E E;
547 private:
548 sub_framework_command fields;
549 };
550
551
552 //
553 // mach-o sub_client load command
554 //
555 template <typename P>
556 class macho_sub_client_command {
557 public:
558 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
559 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
560
561 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
562 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
563
564 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
565 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
566
567 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
568 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
569
570 typedef typename P::E E;
571 private:
572 sub_client_command fields;
573 };
574
575
576 //
577 // mach-o sub_umbrella load command
578 //
579 template <typename P>
580 class macho_sub_umbrella_command {
581 public:
582 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
583 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
584
585 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
586 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
587
588 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
589 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
590
591 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
592 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
593
594 typedef typename P::E E;
595 private:
596 sub_umbrella_command fields;
597 };
598
599
600 //
601 // mach-o sub_library load command
602 //
603 template <typename P>
604 class macho_sub_library_command {
605 public:
606 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
607 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
608
609 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
610 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
611
612 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
613 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
614
615 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
616 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
617
618 typedef typename P::E E;
619 private:
620 sub_library_command fields;
621 };
622
623
624 //
625 // mach-o uuid load command
626 //
627 template <typename P>
628 class macho_uuid_command {
629 public:
630 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
631 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
632
633 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
634 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
635
636 const uint8_t* uuid() const INLINE { return fields.uuid; }
637 void set_uuid(const uint8_t u[16]) INLINE { memcpy(&fields.uuid, u, 16); }
638
639 typedef typename P::E E;
640 private:
641 uuid_command fields;
642 };
643
644
645 //
646 // mach-o routines load command
647 //
648 template <typename P> struct macho_routines_content {};
649 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
650 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
651 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
652 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
653
654 template <typename P>
655 class macho_routines_command {
656 public:
657 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
658 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
659
660 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
661 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
662
663 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
664 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
665
666 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
667 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
668
669 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
670 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
671
672 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
673 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
674
675 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
676 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
677
678 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
679 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
680
681 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
682 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
683
684 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
685 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
686
687 typedef typename P::E E;
688 enum {
689 CMD = macho_routines_content<P>::CMD
690 };
691 private:
692 macho_routines_content<P> routines;
693 };
694
695
696 //
697 // mach-o symbol table load command
698 //
699 template <typename P>
700 class macho_symtab_command {
701 public:
702 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
703 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
704
705 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
706 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
707
708 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
709 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
710
711 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
712 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
713
714 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
715 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
716
717 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
718 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
719
720
721 typedef typename P::E E;
722 private:
723 symtab_command fields;
724 };
725
726
727 //
728 // mach-o dynamic symbol table load command
729 //
730 template <typename P>
731 class macho_dysymtab_command {
732 public:
733 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
734 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
735
736 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
737 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
738
739 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
740 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
741
742 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
743 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
744
745 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
746 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
747
748 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
749 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
750
751 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
752 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
753
754 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
755 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
756
757 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
758 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
759
760 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
761 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
762
763 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
764 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
765
766 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
767 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
768
769 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
770 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
771
772 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
773 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
774
775 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
776 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
777
778 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
779 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
780
781 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
782 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
783
784 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
785 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
786
787 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
788 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
789
790 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
791 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
792
793 typedef typename P::E E;
794 private:
795 dysymtab_command fields;
796 };
797
798
799
800
801 //
802 // mach-o module table entry (for compatibility with old ld/dyld)
803 //
804 template <typename P> struct macho_dylib_module_content {};
805 template <> struct macho_dylib_module_content<Pointer32<BigEndian> > { struct dylib_module fields; };
806 template <> struct macho_dylib_module_content<Pointer32<LittleEndian> > { struct dylib_module fields; };
807 template <> struct macho_dylib_module_content<Pointer64<BigEndian> > { struct dylib_module_64 fields; };
808 template <> struct macho_dylib_module_content<Pointer64<LittleEndian> > { struct dylib_module_64 fields; };
809
810 template <typename P>
811 class macho_dylib_module {
812 public:
813 uint32_t module_name() const INLINE { return E::get32(module.fields.module_name); }
814 void set_module_name(uint32_t value) INLINE { E::set32(module.fields.module_name, value); }
815
816 uint32_t iextdefsym() const INLINE { return E::get32(module.fields.iextdefsym); }
817 void set_iextdefsym(uint32_t value) INLINE { E::set32(module.fields.iextdefsym, value); }
818
819 uint32_t nextdefsym() const INLINE { return E::get32(module.fields.nextdefsym); }
820 void set_nextdefsym(uint32_t value) INLINE { E::set32(module.fields.nextdefsym, value); }
821
822 uint32_t irefsym() const INLINE { return E::get32(module.fields.irefsym); }
823 void set_irefsym(uint32_t value) INLINE { E::set32(module.fields.irefsym, value); }
824
825 uint32_t nrefsym() const INLINE { return E::get32(module.fields.nrefsym); }
826 void set_nrefsym(uint32_t value) INLINE { E::set32(module.fields.nrefsym, value); }
827
828 uint32_t ilocalsym() const INLINE { return E::get32(module.fields.ilocalsym); }
829 void set_ilocalsym(uint32_t value) INLINE { E::set32(module.fields.ilocalsym, value); }
830
831 uint32_t nlocalsym() const INLINE { return E::get32(module.fields.nlocalsym); }
832 void set_nlocalsym(uint32_t value) INLINE { E::set32(module.fields.nlocalsym, value); }
833
834 uint32_t iextrel() const INLINE { return E::get32(module.fields.iextrel); }
835 void set_iextrel(uint32_t value) INLINE { E::set32(module.fields.iextrel, value); }
836
837 uint32_t nextrel() const INLINE { return E::get32(module.fields.nextrel); }
838 void set_nextrel(uint32_t value) INLINE { E::set32(module.fields.nextrel, value); }
839
840 uint16_t iinit() const INLINE { return E::get32(module.fields.iinit_iterm) & 0xFFFF; }
841 uint16_t iterm() const INLINE { return E::get32(module.fields.iinit_iterm) > 16; }
842 void set_iinit_iterm(uint16_t init, uint16_t term) INLINE { E::set32(module.fields.iinit_iterm, (term<<16) | (init &0xFFFF)); }
843
844 uint16_t ninit() const INLINE { return E::get32(module.fields.ninit_nterm) & 0xFFFF; }
845 uint16_t nterm() const INLINE { return E::get32(module.fields.ninit_nterm) > 16; }
846 void set_ninit_nterm(uint16_t init, uint16_t term) INLINE { E::set32(module.fields.ninit_nterm, (term<<16) | (init &0xFFFF)); }
847
848 uint64_t objc_module_info_addr() const INLINE { return P::getP(module.fields.objc_module_info_addr); }
849 void set_objc_module_info_addr(uint64_t value) INLINE { P::setP(module.fields.objc_module_info_addr, value); }
850
851 uint32_t objc_module_info_size() const INLINE { return E::get32(module.fields.objc_module_info_size); }
852 void set_objc_module_info_size(uint32_t value) INLINE { E::set32(module.fields.objc_module_info_size, value); }
853
854
855 typedef typename P::E E;
856 private:
857 macho_dylib_module_content<P> module;
858 };
859
860
861 //
862 // mach-o dylib_reference entry
863 //
864 template <typename P>
865 class macho_dylib_reference {
866 public:
867 uint32_t isym() const INLINE { return E::getBits(fields, 0, 24); }
868 void set_isym(uint32_t value) INLINE { E::setBits(fields, value, 0, 24); }
869
870 uint8_t flags() const INLINE { return E::getBits(fields, 24, 8); }
871 void set_flags(uint8_t value) INLINE { E::setBits(fields, value, 24, 8); }
872
873 typedef typename P::E E;
874 private:
875 uint32_t fields;
876 };
877
878
879
880 //
881 // mach-o two-level hints load command
882 //
883 template <typename P>
884 class macho_dylib_table_of_contents {
885 public:
886 uint32_t symbol_index() const INLINE { return E::get32(fields.symbol_index); }
887 void set_symbol_index(uint32_t value) INLINE { E::set32(fields.symbol_index, value); }
888
889 uint32_t module_index() const INLINE { return E::get32(fields.module_index); }
890 void set_module_index(uint32_t value) INLINE { E::set32(fields.module_index, value); }
891
892 typedef typename P::E E;
893 private:
894 dylib_table_of_contents fields;
895 };
896
897
898
899 //
900 // mach-o two-level hints load command
901 //
902 template <typename P>
903 class macho_twolevel_hints_command {
904 public:
905 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
906 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
907
908 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
909 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
910
911 uint32_t offset() const INLINE { return E::get32(fields.offset); }
912 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
913
914 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
915 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
916
917 typedef typename P::E E;
918 private:
919 twolevel_hints_command fields;
920 };
921
922
923 //
924 // mach-o threads load command
925 //
926 template <typename P>
927 class macho_thread_command {
928 public:
929 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
930 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
931
932 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
933 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
934
935 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
936 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
937
938 uint32_t count() const INLINE { return E::get32(fields_count); }
939 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
940
941 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
942 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
943
944 typedef typename P::E E;
945 typedef typename P::uint_t pint_t;
946 private:
947 struct thread_command fields;
948 uint32_t fields_flavor;
949 uint32_t fields_count;
950 pint_t thread_registers[1];
951 };
952
953
954 //
955 // mach-o misc data
956 //
957 template <typename P>
958 class macho_linkedit_data_command {
959 public:
960 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
961 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
962
963 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
964 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
965
966 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
967 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
968
969 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
970 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
971
972
973 typedef typename P::E E;
974 private:
975 struct linkedit_data_command fields;
976 };
977
978
979 //
980 // mach-o rpath
981 //
982 template <typename P>
983 class macho_rpath_command {
984 public:
985 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
986 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
987
988 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
989 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
990
991 uint32_t path_offset() const INLINE { return E::get32(fields.path.offset); }
992 void set_path_offset(uint32_t value) INLINE { E::set32(fields.path.offset, value); }
993
994 const char* path() const INLINE { return (const char*)&fields + path_offset(); }
995 void set_path_offset() INLINE { set_path_offset(sizeof(fields)); }
996
997
998 typedef typename P::E E;
999 private:
1000 struct rpath_command fields;
1001 };
1002
1003
1004
1005 //
1006 // mach-o symbol table entry
1007 //
1008 template <typename P> struct macho_nlist_content {};
1009 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
1010 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
1011 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
1012 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
1013
1014 template <typename P>
1015 class macho_nlist {
1016 public:
1017 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
1018 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
1019
1020 uint8_t n_type() const INLINE { return entry.fields.n_type; }
1021 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
1022
1023 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
1024 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
1025
1026 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
1027 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
1028
1029 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
1030 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
1031
1032 typedef typename P::E E;
1033 private:
1034 macho_nlist_content<P> entry;
1035 };
1036
1037
1038
1039 //
1040 // mach-o relocation info
1041 //
1042 template <typename P>
1043 class macho_relocation_info {
1044 public:
1045 uint32_t r_address() const INLINE { return E::get32(address); }
1046 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
1047
1048 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
1049 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
1050
1051 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
1052 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
1053
1054 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
1055 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
1056
1057 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
1058 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
1059
1060 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
1061 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
1062
1063 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
1064
1065 typedef typename P::E E;
1066 private:
1067 uint32_t address;
1068 uint32_t other;
1069 };
1070
1071
1072 //
1073 // mach-o scattered relocation info
1074 // The bit fields are always in big-endian order (see mach-o/reloc.h)
1075 //
1076 template <typename P>
1077 class macho_scattered_relocation_info {
1078 public:
1079 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
1080 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
1081
1082 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
1083 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
1084
1085 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
1086 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
1087
1088 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
1089 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
1090
1091 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
1092 void set_r_address(uint32_t x) { if ( x > 0x00FFFFFF ) throw "scattered reloc r_address too large";
1093 uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
1094
1095 uint32_t r_value() const INLINE { return E::get32(value); }
1096 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
1097
1098 uint32_t r_other() const INLINE { return other; }
1099
1100 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
1101
1102 typedef typename P::E E;
1103 private:
1104 uint32_t other;
1105 uint32_t value;
1106 };
1107
1108
1109
1110 //
1111 // mach-o encyrption info load command
1112 //
1113 template <typename P>
1114 class macho_encryption_info_command {
1115 public:
1116 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1117 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1118
1119 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1120 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1121
1122 uint32_t cryptoff() const INLINE { return E::get32(fields.cryptoff); }
1123 void set_cryptoff(uint32_t value) INLINE { E::set32(fields.cryptoff, value); }
1124
1125 uint32_t cryptsize() const INLINE { return E::get32(fields.cryptsize); }
1126 void set_cryptsize(uint32_t value) INLINE { E::set32(fields.cryptsize, value); }
1127
1128 uint32_t cryptid() const INLINE { return E::get32(fields.cryptid); }
1129 void set_cryptid(uint32_t value) INLINE { E::set32(fields.cryptid, value); }
1130
1131 typedef typename P::E E;
1132 private:
1133 encryption_info_command fields;
1134 };
1135
1136
1137 //
1138 // start of __unwind_info section
1139 //
1140 template <typename P>
1141 class macho_unwind_info_section_header {
1142 public:
1143 uint32_t version() const INLINE { return E::get32(fields.version); }
1144 void set_version(uint32_t value) INLINE { E::set32(fields.version, value); }
1145
1146 uint32_t commonEncodingsArraySectionOffset() const INLINE { return E::get32(fields.commonEncodingsArraySectionOffset); }
1147 void set_commonEncodingsArraySectionOffset(uint32_t value) INLINE { E::set32(fields.commonEncodingsArraySectionOffset, value); }
1148
1149 uint32_t commonEncodingsArrayCount() const INLINE { return E::get32(fields.commonEncodingsArrayCount); }
1150 void set_commonEncodingsArrayCount(uint32_t value) INLINE { E::set32(fields.commonEncodingsArrayCount, value); }
1151
1152 uint32_t personalityArraySectionOffset() const INLINE { return E::get32(fields.personalityArraySectionOffset); }
1153 void set_personalityArraySectionOffset(uint32_t value) INLINE { E::set32(fields.personalityArraySectionOffset, value); }
1154
1155 uint32_t personalityArrayCount() const INLINE { return E::get32(fields.personalityArrayCount); }
1156 void set_personalityArrayCount(uint32_t value) INLINE { E::set32(fields.personalityArrayCount, value); }
1157
1158 uint32_t indexSectionOffset() const INLINE { return E::get32(fields.indexSectionOffset); }
1159 void set_indexSectionOffset(uint32_t value) INLINE { E::set32(fields.indexSectionOffset, value); }
1160
1161 uint32_t indexCount() const INLINE { return E::get32(fields.indexCount); }
1162 void set_indexCount(uint32_t value) INLINE { E::set32(fields.indexCount, value); }
1163
1164 typedef typename P::E E;
1165 private:
1166 unwind_info_section_header fields;
1167 };
1168
1169
1170
1171 //
1172 // uwind first level index entry
1173 //
1174 template <typename P>
1175 class macho_unwind_info_section_header_index_entry {
1176 public:
1177 uint32_t functionOffset() const INLINE { return E::get32(fields.functionOffset); }
1178 void set_functionOffset(uint32_t value) INLINE { E::set32(fields.functionOffset, value); }
1179
1180 uint32_t secondLevelPagesSectionOffset() const INLINE { return E::get32(fields.secondLevelPagesSectionOffset); }
1181 void set_secondLevelPagesSectionOffset(uint32_t value) INLINE { E::set32(fields.secondLevelPagesSectionOffset, value); }
1182
1183 uint32_t lsdaIndexArraySectionOffset() const INLINE { return E::get32(fields.lsdaIndexArraySectionOffset); }
1184 void set_lsdaIndexArraySectionOffset(uint32_t value) INLINE { E::set32(fields.lsdaIndexArraySectionOffset, value); }
1185
1186 typedef typename P::E E;
1187 private:
1188 unwind_info_section_header_index_entry fields;
1189 };
1190
1191
1192 //
1193 // LSDA table entry
1194 //
1195 template <typename P>
1196 class macho_unwind_info_section_header_lsda_index_entry {
1197 public:
1198 uint32_t functionOffset() const INLINE { return E::get32(fields.functionOffset); }
1199 void set_functionOffset(uint32_t value) INLINE { E::set32(fields.functionOffset, value); }
1200
1201 uint32_t lsdaOffset() const INLINE { return E::get32(fields.lsdaOffset); }
1202 void set_lsdaOffset(uint32_t value) INLINE { E::set32(fields.lsdaOffset, value); }
1203
1204 typedef typename P::E E;
1205 private:
1206 unwind_info_section_header_lsda_index_entry fields;
1207 };
1208
1209
1210 //
1211 // regular second level entry
1212 //
1213 template <typename P>
1214 class macho_unwind_info_regular_second_level_entry {
1215 public:
1216 uint32_t functionOffset() const INLINE { return E::get32(fields.functionOffset); }
1217 void set_functionOffset(uint32_t value) INLINE { E::set32(fields.functionOffset, value); }
1218
1219 uint32_t encoding() const INLINE { return E::get32(fields.encoding); }
1220 void set_encoding(uint32_t value) INLINE { E::set32(fields.encoding, value); }
1221
1222 typedef typename P::E E;
1223 private:
1224 unwind_info_regular_second_level_entry fields;
1225 };
1226
1227
1228 //
1229 // start of second level regular page
1230 //
1231 template <typename P>
1232 class macho_unwind_info_regular_second_level_page_header {
1233 public:
1234 uint32_t kind() const INLINE { return E::get32(fields.kind); }
1235 void set_kind(uint32_t value) INLINE { E::set32(fields.kind, value); }
1236
1237 uint16_t entryPageOffset() const INLINE { return E::get16(fields.entryPageOffset); }
1238 void set_entryPageOffset(uint16_t value) INLINE { E::set16((uint16_t&)fields.entryPageOffset, value); }
1239
1240 uint16_t entryCount() const INLINE { return E::get16(fields.entryCount); }
1241 void set_entryCount(uint16_t value) INLINE { E::set16((uint16_t&)fields.entryCount, value); }
1242
1243 typedef typename P::E E;
1244 private:
1245 unwind_info_regular_second_level_page_header fields;
1246 };
1247
1248
1249 //
1250 // start of second level compressed page
1251 //
1252 template <typename P>
1253 class macho_unwind_info_compressed_second_level_page_header {
1254 public:
1255 uint32_t kind() const INLINE { return E::get32(fields.kind); }
1256 void set_kind(uint32_t value) INLINE { E::set32(fields.kind, value); }
1257
1258 uint16_t entryPageOffset() const INLINE { return E::get16(fields.entryPageOffset); }
1259 void set_entryPageOffset(uint16_t value) INLINE { E::set16((uint16_t&)fields.entryPageOffset, value); }
1260
1261 uint16_t entryCount() const INLINE { return E::get16(fields.entryCount); }
1262 void set_entryCount(uint16_t value) INLINE { E::set16((uint16_t&)fields.entryCount, value); }
1263
1264 uint16_t encodingsPageOffset() const INLINE { return E::get16(fields.encodingsPageOffset); }
1265 void set_encodingsPageOffset(uint16_t value) INLINE { E::set16((uint16_t&)fields.encodingsPageOffset, value); }
1266
1267 uint16_t encodingsCount() const INLINE { return E::get16(fields.encodingsCount); }
1268 void set_encodingsCount(uint16_t value) INLINE { E::set16((uint16_t&)fields.encodingsCount, value); }
1269
1270 typedef typename P::E E;
1271 private:
1272 unwind_info_compressed_second_level_page_header fields;
1273 };
1274
1275
1276 //
1277 // compressed dyld info load command
1278 //
1279 template <typename P>
1280 class macho_dyld_info_command {
1281 public:
1282 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1283 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1284
1285 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1286 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1287
1288 uint32_t rebase_off() const INLINE { return E::get32(fields.rebase_off); }
1289 void set_rebase_off(uint32_t value) INLINE { E::set32(fields.rebase_off, value); }
1290
1291 uint32_t rebase_size() const INLINE { return E::get32(fields.rebase_size); }
1292 void set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value); }
1293
1294 uint32_t bind_off() const INLINE { return E::get32(fields.bind_off); }
1295 void set_bind_off(uint32_t value) INLINE { E::set32(fields.bind_off, value); }
1296
1297 uint32_t bind_size() const INLINE { return E::get32(fields.bind_size); }
1298 void set_bind_size(uint32_t value) INLINE { E::set32(fields.bind_size, value); }
1299
1300 uint32_t weak_bind_off() const INLINE { return E::get32(fields.weak_bind_off); }
1301 void set_weak_bind_off(uint32_t value) INLINE { E::set32(fields.weak_bind_off, value); }
1302
1303 uint32_t weak_bind_size() const INLINE { return E::get32(fields.weak_bind_size); }
1304 void set_weak_bind_size(uint32_t value) INLINE { E::set32(fields.weak_bind_size, value); }
1305
1306 uint32_t lazy_bind_off() const INLINE { return E::get32(fields.lazy_bind_off); }
1307 void set_lazy_bind_off(uint32_t value) INLINE { E::set32(fields.lazy_bind_off, value); }
1308
1309 uint32_t lazy_bind_size() const INLINE { return E::get32(fields.lazy_bind_size); }
1310 void set_lazy_bind_size(uint32_t value) INLINE { E::set32(fields.lazy_bind_size, value); }
1311
1312 uint32_t export_off() const INLINE { return E::get32(fields.export_off); }
1313 void set_export_off(uint32_t value) INLINE { E::set32(fields.export_off, value); }
1314
1315 uint32_t export_size() const INLINE { return E::get32(fields.export_size); }
1316 void set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value); }
1317
1318
1319 typedef typename P::E E;
1320 private:
1321 dyld_info_command fields;
1322 };
1323
1324
1325 //
1326 // mach-o version load command
1327 //
1328 template <typename P>
1329 class macho_version_min_command {
1330 public:
1331 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1332 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1333
1334 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1335 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1336
1337 uint32_t version() const INLINE { return fields.version; }
1338 void set_version(uint32_t value) INLINE { E::set32(fields.version, value); }
1339
1340 #ifdef DICE_KIND_DATA
1341 uint32_t sdk() const INLINE { return fields.sdk; }
1342 void set_sdk(uint32_t value) INLINE { E::set32(fields.sdk, value); }
1343 #else
1344 uint32_t sdk() const INLINE { return fields.reserved; }
1345 void set_sdk(uint32_t value) INLINE { E::set32(fields.reserved, value); }
1346 #endif
1347
1348 typedef typename P::E E;
1349 private:
1350 version_min_command fields;
1351 };
1352
1353
1354 //
1355 // mach-o __LD, __compact_unwind section in object files
1356 //
1357 template <typename P>
1358 class macho_compact_unwind_entry {
1359 public:
1360 typedef typename P::E E;
1361 typedef typename P::uint_t pint_t;
1362
1363 pint_t codeStart() const INLINE { return P::getP(_codeStart); }
1364 void set_codeStart(pint_t value) INLINE { P::setP(_codeStart, value); }
1365
1366 uint32_t codeLen() const INLINE { return E::get32(_codeLen); }
1367 void set_codeLen(uint32_t value) INLINE { E::set32(_codeLen, value); }
1368
1369 uint32_t compactUnwindInfo() const INLINE { return E::get32(_compactUnwindInfo); }
1370 void set_compactUnwindInfo(uint32_t value) INLINE { E::set32(_compactUnwindInfo, value); }
1371
1372 pint_t personality() const INLINE { return P::getP(_personality); }
1373 void set_personality(pint_t value) INLINE { P::setP(_personality, value); }
1374
1375 pint_t lsda() const INLINE { return P::getP(_lsda); }
1376 void set_lsda(pint_t value) INLINE { P::setP(_lsda, value); }
1377
1378 static uint32_t codeStartFieldOffset() INLINE { return offsetof(macho_compact_unwind_entry<P>,_codeStart); }
1379 static uint32_t personalityFieldOffset() INLINE { return offsetof(macho_compact_unwind_entry<P>,_personality); }
1380 static uint32_t lsdaFieldOffset() INLINE { return offsetof(macho_compact_unwind_entry<P>,_lsda); }
1381
1382 private:
1383 pint_t _codeStart;
1384 uint32_t _codeLen;
1385 uint32_t _compactUnwindInfo;
1386 pint_t _personality;
1387 pint_t _lsda;
1388 };
1389
1390
1391 //
1392 // mach-o source version load command
1393 //
1394 template <typename P>
1395 class macho_source_version_command {
1396 public:
1397 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1398 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1399
1400 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1401 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1402
1403 uint64_t version() const INLINE { return fields.version; }
1404 void set_version(uint64_t value) INLINE { E::set64(fields.version, value); }
1405
1406 typedef typename P::E E;
1407 private:
1408 source_version_command fields;
1409 };
1410
1411
1412 //
1413 // mach-o source version load command
1414 //
1415 template <typename P>
1416 class macho_entry_point_command {
1417 public:
1418 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1419 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1420
1421 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1422 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1423
1424 uint64_t entryoff() const INLINE { return fields.entryoff; }
1425 void set_entryoff(uint64_t value) INLINE { E::set64(fields.entryoff, value); }
1426
1427 uint64_t stacksize() const INLINE { return fields.stacksize; }
1428 void set_stacksize(uint64_t value) INLINE { E::set64(fields.stacksize, value); }
1429
1430 typedef typename P::E E;
1431 private:
1432 entry_point_command fields;
1433 };
1434
1435
1436
1437 template <typename P>
1438 class macho_data_in_code_entry {
1439 public:
1440 uint32_t offset() const INLINE { return E::get32(fields.offset); }
1441 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
1442
1443 uint16_t length() const INLINE { return E::get16(fields.length); }
1444 void set_length(uint16_t value) INLINE { E::set16((uint16_t&)fields.length, value); }
1445
1446 uint16_t kind() const INLINE { return E::get16(fields.kind); }
1447 void set_kind(uint16_t value) INLINE { E::set16((uint16_t&)fields.kind, value); }
1448
1449 typedef typename P::E E;
1450 private:
1451 data_in_code_entry fields;
1452 };
1453
1454
1455 #endif // __MACH_O_FILE_ABSTRACTION__
1456
1457