]> git.saurik.com Git - apple/ld64.git/blob - src/abstraction/MachOFileAbstraction.hpp
4fd6123b73b7b6c7844a96fd1e663b81413ed99c
[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 #ifndef CPU_SUBTYPE_ARM_V7S
226 #define CPU_SUBTYPE_ARM_V7S ((cpu_subtype_t) 11)
227 #endif
228
229
230 #ifndef LC_SOURCE_VERSION
231 #define LC_SOURCE_VERSION 0x2A
232 struct source_version_command {
233 uint32_t cmd; /* LC_SOURCE_VERSION */
234 uint32_t cmdsize; /* 16 */
235 uint64_t version; /* A.B.C.D.E packed as a24.b10.c10.d10.e10 */
236 };
237 #endif
238
239 #ifndef LC_MAIN
240 #define LC_MAIN (0x28|LC_REQ_DYLD) /* replacement for LC_UNIXTHREAD */
241 struct entry_point_command {
242 uint32_t cmd; /* LC_MAIN only used in MH_EXECUTE filetypes */
243 uint32_t cmdsize; /* 24 */
244 uint64_t entryoff; /* file (__TEXT) offset of main() */
245 uint64_t stacksize;/* if not zero, initial stack size */
246 };
247 #endif
248
249 #ifndef LC_DYLIB_CODE_SIGN_DRS
250 #define LC_DYLIB_CODE_SIGN_DRS 0x2B
251 #endif
252
253
254 struct ArchInfo {
255 const char* archName;
256 cpu_type_t cpuType;
257 cpu_subtype_t cpuSubType;
258 const char* llvmTriplePrefix;
259 const char* llvmTriplePrefixAlt;
260 bool isSubType;
261 bool supportsThumb2;
262 };
263
264 static const ArchInfo archInfoArray[] = {
265 #if SUPPORT_ARCH_x86_64
266 { "x86_64", CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL, "x86_64-", "", false, false },
267 #endif
268 #if SUPPORT_ARCH_i386
269 { "i386", CPU_TYPE_I386, CPU_SUBTYPE_I386_ALL, "i386-", "", false, false },
270 #endif
271 #if SUPPORT_ARCH_armv4t
272 { "armv4t", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V4T, "armv4t-", "", true, false },
273 #define SUPPORT_ARCH_arm_any 1
274 #endif
275 #if SUPPORT_ARCH_armv5
276 { "armv5", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V5TEJ, "armv5e-", "", true, false },
277 #define SUPPORT_ARCH_arm_any 1
278 #endif
279 #if SUPPORT_ARCH_armv6
280 { "armv6", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6, "armv6-", "", true, false },
281 #define SUPPORT_ARCH_arm_any 1
282 #endif
283 #if SUPPORT_ARCH_armv7
284 { "armv7", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7, "thumbv7-", "armv7-", true, true },
285 #define SUPPORT_ARCH_arm_any 1
286 #endif
287 #if SUPPORT_ARCH_armv7f
288 { "armv7f", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7F, "thumbv7f-", "", true, true },
289 #define SUPPORT_ARCH_arm_any 1
290 #endif
291 #if SUPPORT_ARCH_armv7k
292 { "armv7k", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7K, "thumbv7k-", "", true, true },
293 #define SUPPORT_ARCH_arm_any 1
294 #endif
295 #if SUPPORT_ARCH_armv7s
296 { "armv7s", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S, "thumbv7s-", "armv7s", true, true },
297 #define SUPPORT_ARCH_arm_any 1
298 #endif
299 { NULL, 0, 0, NULL, NULL, false, false }
300 };
301
302
303 // weird, but this include must wait until after SUPPORT_ARCH_arm_any is set up
304 #if SUPPORT_ARCH_arm_any
305 #include <mach-o/arm/reloc.h>
306 #endif
307
308 // hack until newer <mach-o/arm/reloc.h> everywhere
309 #define ARM_RELOC_HALF 8
310 #define ARM_RELOC_HALF_SECTDIFF 9
311
312
313
314 //
315 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
316 //
317
318
319
320 //
321 // mach-o file header
322 //
323 template <typename P> struct macho_header_content {};
324 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
325 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
326 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
327 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
328
329 template <typename P>
330 class macho_header {
331 public:
332 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
333 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
334
335 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
336 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
337
338 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
339 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
340
341 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
342 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
343
344 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
345 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
346
347 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
348 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
349
350 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
351 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
352
353 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
354 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
355
356 typedef typename P::E E;
357 private:
358 macho_header_content<P> header;
359 };
360
361
362 //
363 // mach-o load command
364 //
365 template <typename P>
366 class macho_load_command {
367 public:
368 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
369 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
370
371 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
372 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
373
374 typedef typename P::E E;
375 private:
376 load_command command;
377 };
378
379
380 //
381 // mach-o segment load command
382 //
383 template <typename P> struct macho_segment_content {};
384 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
385 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
386 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
387 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
388
389 template <typename P>
390 class macho_segment_command {
391 public:
392 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
393 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
394
395 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
396 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
397
398 const char* segname() const INLINE { return segment.fields.segname; }
399 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
400
401 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
402 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
403
404 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
405 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
406
407 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
408 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
409
410 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
411 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
412
413 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
414 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
415
416 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
417 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
418
419 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
420 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
421
422 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
423 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
424
425 enum {
426 CMD = macho_segment_content<P>::CMD
427 };
428
429 typedef typename P::E E;
430 private:
431 macho_segment_content<P> segment;
432 };
433
434
435 //
436 // mach-o section
437 //
438 template <typename P> struct macho_section_content {};
439 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
440 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
441 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
442 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
443
444 template <typename P>
445 class macho_section {
446 public:
447 const char* sectname() const INLINE { return section.fields.sectname; }
448 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
449
450 const char* segname() const INLINE { return section.fields.segname; }
451 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
452
453 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
454 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
455
456 uint64_t size() const INLINE { return P::getP(section.fields.size); }
457 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
458
459 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
460 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
461
462 uint32_t align() const INLINE { return E::get32(section.fields.align); }
463 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
464
465 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
466 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
467
468 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
469 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
470
471 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
472 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
473
474 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
475 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
476
477 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
478 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
479
480 typedef typename P::E E;
481 private:
482 macho_section_content<P> section;
483 };
484
485
486 //
487 // mach-o dylib load command
488 //
489 template <typename P>
490 class macho_dylib_command {
491 public:
492 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
493 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
494
495 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
496 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
497
498 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
499 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
500
501 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
502 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
503
504 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
505 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
506
507 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
508 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
509
510 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
511 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
512
513 typedef typename P::E E;
514 private:
515 dylib_command fields;
516 };
517
518
519 //
520 // mach-o dylinker load command
521 //
522 template <typename P>
523 class macho_dylinker_command {
524 public:
525 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
526 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
527
528 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
529 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
530
531 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
532 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
533
534 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
535 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
536
537 typedef typename P::E E;
538 private:
539 dylinker_command fields;
540 };
541
542
543 //
544 // mach-o sub_framework load command
545 //
546 template <typename P>
547 class macho_sub_framework_command {
548 public:
549 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
550 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
551
552 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
553 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
554
555 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
556 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
557
558 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
559 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
560
561 typedef typename P::E E;
562 private:
563 sub_framework_command fields;
564 };
565
566
567 //
568 // mach-o sub_client load command
569 //
570 template <typename P>
571 class macho_sub_client_command {
572 public:
573 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
574 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
575
576 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
577 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
578
579 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
580 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
581
582 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
583 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
584
585 typedef typename P::E E;
586 private:
587 sub_client_command fields;
588 };
589
590
591 //
592 // mach-o sub_umbrella load command
593 //
594 template <typename P>
595 class macho_sub_umbrella_command {
596 public:
597 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
598 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
599
600 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
601 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
602
603 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
604 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
605
606 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
607 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
608
609 typedef typename P::E E;
610 private:
611 sub_umbrella_command fields;
612 };
613
614
615 //
616 // mach-o sub_library load command
617 //
618 template <typename P>
619 class macho_sub_library_command {
620 public:
621 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
622 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
623
624 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
625 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
626
627 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
628 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
629
630 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
631 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
632
633 typedef typename P::E E;
634 private:
635 sub_library_command fields;
636 };
637
638
639 //
640 // mach-o uuid load command
641 //
642 template <typename P>
643 class macho_uuid_command {
644 public:
645 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
646 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
647
648 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
649 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
650
651 const uint8_t* uuid() const INLINE { return fields.uuid; }
652 void set_uuid(const uint8_t u[16]) INLINE { memcpy(&fields.uuid, u, 16); }
653
654 typedef typename P::E E;
655 private:
656 uuid_command fields;
657 };
658
659
660 //
661 // mach-o routines load command
662 //
663 template <typename P> struct macho_routines_content {};
664 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
665 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
666 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
667 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
668
669 template <typename P>
670 class macho_routines_command {
671 public:
672 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
673 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
674
675 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
676 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
677
678 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
679 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
680
681 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
682 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
683
684 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
685 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
686
687 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
688 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
689
690 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
691 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
692
693 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
694 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
695
696 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
697 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
698
699 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
700 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
701
702 typedef typename P::E E;
703 enum {
704 CMD = macho_routines_content<P>::CMD
705 };
706 private:
707 macho_routines_content<P> routines;
708 };
709
710
711 //
712 // mach-o symbol table load command
713 //
714 template <typename P>
715 class macho_symtab_command {
716 public:
717 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
718 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
719
720 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
721 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
722
723 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
724 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
725
726 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
727 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
728
729 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
730 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
731
732 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
733 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
734
735
736 typedef typename P::E E;
737 private:
738 symtab_command fields;
739 };
740
741
742 //
743 // mach-o dynamic symbol table load command
744 //
745 template <typename P>
746 class macho_dysymtab_command {
747 public:
748 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
749 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
750
751 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
752 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
753
754 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
755 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
756
757 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
758 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
759
760 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
761 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
762
763 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
764 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
765
766 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
767 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
768
769 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
770 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
771
772 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
773 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
774
775 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
776 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
777
778 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
779 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
780
781 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
782 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
783
784 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
785 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
786
787 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
788 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
789
790 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
791 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
792
793 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
794 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
795
796 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
797 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
798
799 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
800 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
801
802 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
803 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
804
805 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
806 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
807
808 typedef typename P::E E;
809 private:
810 dysymtab_command fields;
811 };
812
813
814
815
816 //
817 // mach-o module table entry (for compatibility with old ld/dyld)
818 //
819 template <typename P> struct macho_dylib_module_content {};
820 template <> struct macho_dylib_module_content<Pointer32<BigEndian> > { struct dylib_module fields; };
821 template <> struct macho_dylib_module_content<Pointer32<LittleEndian> > { struct dylib_module fields; };
822 template <> struct macho_dylib_module_content<Pointer64<BigEndian> > { struct dylib_module_64 fields; };
823 template <> struct macho_dylib_module_content<Pointer64<LittleEndian> > { struct dylib_module_64 fields; };
824
825 template <typename P>
826 class macho_dylib_module {
827 public:
828 uint32_t module_name() const INLINE { return E::get32(module.fields.module_name); }
829 void set_module_name(uint32_t value) INLINE { E::set32(module.fields.module_name, value); }
830
831 uint32_t iextdefsym() const INLINE { return E::get32(module.fields.iextdefsym); }
832 void set_iextdefsym(uint32_t value) INLINE { E::set32(module.fields.iextdefsym, value); }
833
834 uint32_t nextdefsym() const INLINE { return E::get32(module.fields.nextdefsym); }
835 void set_nextdefsym(uint32_t value) INLINE { E::set32(module.fields.nextdefsym, value); }
836
837 uint32_t irefsym() const INLINE { return E::get32(module.fields.irefsym); }
838 void set_irefsym(uint32_t value) INLINE { E::set32(module.fields.irefsym, value); }
839
840 uint32_t nrefsym() const INLINE { return E::get32(module.fields.nrefsym); }
841 void set_nrefsym(uint32_t value) INLINE { E::set32(module.fields.nrefsym, value); }
842
843 uint32_t ilocalsym() const INLINE { return E::get32(module.fields.ilocalsym); }
844 void set_ilocalsym(uint32_t value) INLINE { E::set32(module.fields.ilocalsym, value); }
845
846 uint32_t nlocalsym() const INLINE { return E::get32(module.fields.nlocalsym); }
847 void set_nlocalsym(uint32_t value) INLINE { E::set32(module.fields.nlocalsym, value); }
848
849 uint32_t iextrel() const INLINE { return E::get32(module.fields.iextrel); }
850 void set_iextrel(uint32_t value) INLINE { E::set32(module.fields.iextrel, value); }
851
852 uint32_t nextrel() const INLINE { return E::get32(module.fields.nextrel); }
853 void set_nextrel(uint32_t value) INLINE { E::set32(module.fields.nextrel, value); }
854
855 uint16_t iinit() const INLINE { return E::get32(module.fields.iinit_iterm) & 0xFFFF; }
856 uint16_t iterm() const INLINE { return E::get32(module.fields.iinit_iterm) > 16; }
857 void set_iinit_iterm(uint16_t init, uint16_t term) INLINE { E::set32(module.fields.iinit_iterm, (term<<16) | (init &0xFFFF)); }
858
859 uint16_t ninit() const INLINE { return E::get32(module.fields.ninit_nterm) & 0xFFFF; }
860 uint16_t nterm() const INLINE { return E::get32(module.fields.ninit_nterm) > 16; }
861 void set_ninit_nterm(uint16_t init, uint16_t term) INLINE { E::set32(module.fields.ninit_nterm, (term<<16) | (init &0xFFFF)); }
862
863 uint64_t objc_module_info_addr() const INLINE { return P::getP(module.fields.objc_module_info_addr); }
864 void set_objc_module_info_addr(uint64_t value) INLINE { P::setP(module.fields.objc_module_info_addr, value); }
865
866 uint32_t objc_module_info_size() const INLINE { return E::get32(module.fields.objc_module_info_size); }
867 void set_objc_module_info_size(uint32_t value) INLINE { E::set32(module.fields.objc_module_info_size, value); }
868
869
870 typedef typename P::E E;
871 private:
872 macho_dylib_module_content<P> module;
873 };
874
875
876 //
877 // mach-o dylib_reference entry
878 //
879 template <typename P>
880 class macho_dylib_reference {
881 public:
882 uint32_t isym() const INLINE { return E::getBits(fields, 0, 24); }
883 void set_isym(uint32_t value) INLINE { E::setBits(fields, value, 0, 24); }
884
885 uint8_t flags() const INLINE { return E::getBits(fields, 24, 8); }
886 void set_flags(uint8_t value) INLINE { E::setBits(fields, value, 24, 8); }
887
888 typedef typename P::E E;
889 private:
890 uint32_t fields;
891 };
892
893
894
895 //
896 // mach-o two-level hints load command
897 //
898 template <typename P>
899 class macho_dylib_table_of_contents {
900 public:
901 uint32_t symbol_index() const INLINE { return E::get32(fields.symbol_index); }
902 void set_symbol_index(uint32_t value) INLINE { E::set32(fields.symbol_index, value); }
903
904 uint32_t module_index() const INLINE { return E::get32(fields.module_index); }
905 void set_module_index(uint32_t value) INLINE { E::set32(fields.module_index, value); }
906
907 typedef typename P::E E;
908 private:
909 dylib_table_of_contents fields;
910 };
911
912
913
914 //
915 // mach-o two-level hints load command
916 //
917 template <typename P>
918 class macho_twolevel_hints_command {
919 public:
920 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
921 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
922
923 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
924 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
925
926 uint32_t offset() const INLINE { return E::get32(fields.offset); }
927 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
928
929 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
930 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
931
932 typedef typename P::E E;
933 private:
934 twolevel_hints_command fields;
935 };
936
937
938 //
939 // mach-o threads load command
940 //
941 template <typename P>
942 class macho_thread_command {
943 public:
944 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
945 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
946
947 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
948 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
949
950 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
951 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
952
953 uint32_t count() const INLINE { return E::get32(fields_count); }
954 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
955
956 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
957 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
958
959 typedef typename P::E E;
960 typedef typename P::uint_t pint_t;
961 private:
962 struct thread_command fields;
963 uint32_t fields_flavor;
964 uint32_t fields_count;
965 pint_t thread_registers[1];
966 };
967
968
969 //
970 // mach-o misc data
971 //
972 template <typename P>
973 class macho_linkedit_data_command {
974 public:
975 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
976 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
977
978 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
979 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
980
981 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
982 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
983
984 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
985 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
986
987
988 typedef typename P::E E;
989 private:
990 struct linkedit_data_command fields;
991 };
992
993
994 //
995 // mach-o rpath
996 //
997 template <typename P>
998 class macho_rpath_command {
999 public:
1000 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1001 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1002
1003 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1004 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1005
1006 uint32_t path_offset() const INLINE { return E::get32(fields.path.offset); }
1007 void set_path_offset(uint32_t value) INLINE { E::set32(fields.path.offset, value); }
1008
1009 const char* path() const INLINE { return (const char*)&fields + path_offset(); }
1010 void set_path_offset() INLINE { set_path_offset(sizeof(fields)); }
1011
1012
1013 typedef typename P::E E;
1014 private:
1015 struct rpath_command fields;
1016 };
1017
1018
1019
1020 //
1021 // mach-o symbol table entry
1022 //
1023 template <typename P> struct macho_nlist_content {};
1024 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
1025 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
1026 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
1027 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
1028
1029 template <typename P>
1030 class macho_nlist {
1031 public:
1032 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
1033 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
1034
1035 uint8_t n_type() const INLINE { return entry.fields.n_type; }
1036 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
1037
1038 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
1039 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
1040
1041 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
1042 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
1043
1044 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
1045 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
1046
1047 typedef typename P::E E;
1048 private:
1049 macho_nlist_content<P> entry;
1050 };
1051
1052
1053
1054 //
1055 // mach-o relocation info
1056 //
1057 template <typename P>
1058 class macho_relocation_info {
1059 public:
1060 uint32_t r_address() const INLINE { return E::get32(address); }
1061 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
1062
1063 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
1064 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
1065
1066 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
1067 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
1068
1069 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
1070 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
1071
1072 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
1073 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
1074
1075 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
1076 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
1077
1078 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
1079
1080 typedef typename P::E E;
1081 private:
1082 uint32_t address;
1083 uint32_t other;
1084 };
1085
1086
1087 //
1088 // mach-o scattered relocation info
1089 // The bit fields are always in big-endian order (see mach-o/reloc.h)
1090 //
1091 template <typename P>
1092 class macho_scattered_relocation_info {
1093 public:
1094 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
1095 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
1096
1097 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
1098 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
1099
1100 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
1101 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
1102
1103 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
1104 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
1105
1106 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
1107 void set_r_address(uint32_t x) { if ( x > 0x00FFFFFF ) throw "scattered reloc r_address too large";
1108 uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
1109
1110 uint32_t r_value() const INLINE { return E::get32(value); }
1111 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
1112
1113 uint32_t r_other() const INLINE { return other; }
1114
1115 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
1116
1117 typedef typename P::E E;
1118 private:
1119 uint32_t other;
1120 uint32_t value;
1121 };
1122
1123
1124
1125 //
1126 // mach-o encyrption info load command
1127 //
1128 template <typename P>
1129 class macho_encryption_info_command {
1130 public:
1131 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1132 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1133
1134 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1135 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1136
1137 uint32_t cryptoff() const INLINE { return E::get32(fields.cryptoff); }
1138 void set_cryptoff(uint32_t value) INLINE { E::set32(fields.cryptoff, value); }
1139
1140 uint32_t cryptsize() const INLINE { return E::get32(fields.cryptsize); }
1141 void set_cryptsize(uint32_t value) INLINE { E::set32(fields.cryptsize, value); }
1142
1143 uint32_t cryptid() const INLINE { return E::get32(fields.cryptid); }
1144 void set_cryptid(uint32_t value) INLINE { E::set32(fields.cryptid, value); }
1145
1146 typedef typename P::E E;
1147 private:
1148 encryption_info_command fields;
1149 };
1150
1151
1152 //
1153 // start of __unwind_info section
1154 //
1155 template <typename P>
1156 class macho_unwind_info_section_header {
1157 public:
1158 uint32_t version() const INLINE { return E::get32(fields.version); }
1159 void set_version(uint32_t value) INLINE { E::set32(fields.version, value); }
1160
1161 uint32_t commonEncodingsArraySectionOffset() const INLINE { return E::get32(fields.commonEncodingsArraySectionOffset); }
1162 void set_commonEncodingsArraySectionOffset(uint32_t value) INLINE { E::set32(fields.commonEncodingsArraySectionOffset, value); }
1163
1164 uint32_t commonEncodingsArrayCount() const INLINE { return E::get32(fields.commonEncodingsArrayCount); }
1165 void set_commonEncodingsArrayCount(uint32_t value) INLINE { E::set32(fields.commonEncodingsArrayCount, value); }
1166
1167 uint32_t personalityArraySectionOffset() const INLINE { return E::get32(fields.personalityArraySectionOffset); }
1168 void set_personalityArraySectionOffset(uint32_t value) INLINE { E::set32(fields.personalityArraySectionOffset, value); }
1169
1170 uint32_t personalityArrayCount() const INLINE { return E::get32(fields.personalityArrayCount); }
1171 void set_personalityArrayCount(uint32_t value) INLINE { E::set32(fields.personalityArrayCount, value); }
1172
1173 uint32_t indexSectionOffset() const INLINE { return E::get32(fields.indexSectionOffset); }
1174 void set_indexSectionOffset(uint32_t value) INLINE { E::set32(fields.indexSectionOffset, value); }
1175
1176 uint32_t indexCount() const INLINE { return E::get32(fields.indexCount); }
1177 void set_indexCount(uint32_t value) INLINE { E::set32(fields.indexCount, value); }
1178
1179 typedef typename P::E E;
1180 private:
1181 unwind_info_section_header fields;
1182 };
1183
1184
1185
1186 //
1187 // uwind first level index entry
1188 //
1189 template <typename P>
1190 class macho_unwind_info_section_header_index_entry {
1191 public:
1192 uint32_t functionOffset() const INLINE { return E::get32(fields.functionOffset); }
1193 void set_functionOffset(uint32_t value) INLINE { E::set32(fields.functionOffset, value); }
1194
1195 uint32_t secondLevelPagesSectionOffset() const INLINE { return E::get32(fields.secondLevelPagesSectionOffset); }
1196 void set_secondLevelPagesSectionOffset(uint32_t value) INLINE { E::set32(fields.secondLevelPagesSectionOffset, value); }
1197
1198 uint32_t lsdaIndexArraySectionOffset() const INLINE { return E::get32(fields.lsdaIndexArraySectionOffset); }
1199 void set_lsdaIndexArraySectionOffset(uint32_t value) INLINE { E::set32(fields.lsdaIndexArraySectionOffset, value); }
1200
1201 typedef typename P::E E;
1202 private:
1203 unwind_info_section_header_index_entry fields;
1204 };
1205
1206
1207 //
1208 // LSDA table entry
1209 //
1210 template <typename P>
1211 class macho_unwind_info_section_header_lsda_index_entry {
1212 public:
1213 uint32_t functionOffset() const INLINE { return E::get32(fields.functionOffset); }
1214 void set_functionOffset(uint32_t value) INLINE { E::set32(fields.functionOffset, value); }
1215
1216 uint32_t lsdaOffset() const INLINE { return E::get32(fields.lsdaOffset); }
1217 void set_lsdaOffset(uint32_t value) INLINE { E::set32(fields.lsdaOffset, value); }
1218
1219 typedef typename P::E E;
1220 private:
1221 unwind_info_section_header_lsda_index_entry fields;
1222 };
1223
1224
1225 //
1226 // regular second level entry
1227 //
1228 template <typename P>
1229 class macho_unwind_info_regular_second_level_entry {
1230 public:
1231 uint32_t functionOffset() const INLINE { return E::get32(fields.functionOffset); }
1232 void set_functionOffset(uint32_t value) INLINE { E::set32(fields.functionOffset, value); }
1233
1234 uint32_t encoding() const INLINE { return E::get32(fields.encoding); }
1235 void set_encoding(uint32_t value) INLINE { E::set32(fields.encoding, value); }
1236
1237 typedef typename P::E E;
1238 private:
1239 unwind_info_regular_second_level_entry fields;
1240 };
1241
1242
1243 //
1244 // start of second level regular page
1245 //
1246 template <typename P>
1247 class macho_unwind_info_regular_second_level_page_header {
1248 public:
1249 uint32_t kind() const INLINE { return E::get32(fields.kind); }
1250 void set_kind(uint32_t value) INLINE { E::set32(fields.kind, value); }
1251
1252 uint16_t entryPageOffset() const INLINE { return E::get16(fields.entryPageOffset); }
1253 void set_entryPageOffset(uint16_t value) INLINE { E::set16((uint16_t&)fields.entryPageOffset, value); }
1254
1255 uint16_t entryCount() const INLINE { return E::get16(fields.entryCount); }
1256 void set_entryCount(uint16_t value) INLINE { E::set16((uint16_t&)fields.entryCount, value); }
1257
1258 typedef typename P::E E;
1259 private:
1260 unwind_info_regular_second_level_page_header fields;
1261 };
1262
1263
1264 //
1265 // start of second level compressed page
1266 //
1267 template <typename P>
1268 class macho_unwind_info_compressed_second_level_page_header {
1269 public:
1270 uint32_t kind() const INLINE { return E::get32(fields.kind); }
1271 void set_kind(uint32_t value) INLINE { E::set32(fields.kind, value); }
1272
1273 uint16_t entryPageOffset() const INLINE { return E::get16(fields.entryPageOffset); }
1274 void set_entryPageOffset(uint16_t value) INLINE { E::set16((uint16_t&)fields.entryPageOffset, value); }
1275
1276 uint16_t entryCount() const INLINE { return E::get16(fields.entryCount); }
1277 void set_entryCount(uint16_t value) INLINE { E::set16((uint16_t&)fields.entryCount, value); }
1278
1279 uint16_t encodingsPageOffset() const INLINE { return E::get16(fields.encodingsPageOffset); }
1280 void set_encodingsPageOffset(uint16_t value) INLINE { E::set16((uint16_t&)fields.encodingsPageOffset, value); }
1281
1282 uint16_t encodingsCount() const INLINE { return E::get16(fields.encodingsCount); }
1283 void set_encodingsCount(uint16_t value) INLINE { E::set16((uint16_t&)fields.encodingsCount, value); }
1284
1285 typedef typename P::E E;
1286 private:
1287 unwind_info_compressed_second_level_page_header fields;
1288 };
1289
1290
1291 //
1292 // compressed dyld info load command
1293 //
1294 template <typename P>
1295 class macho_dyld_info_command {
1296 public:
1297 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1298 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1299
1300 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1301 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1302
1303 uint32_t rebase_off() const INLINE { return E::get32(fields.rebase_off); }
1304 void set_rebase_off(uint32_t value) INLINE { E::set32(fields.rebase_off, value); }
1305
1306 uint32_t rebase_size() const INLINE { return E::get32(fields.rebase_size); }
1307 void set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value); }
1308
1309 uint32_t bind_off() const INLINE { return E::get32(fields.bind_off); }
1310 void set_bind_off(uint32_t value) INLINE { E::set32(fields.bind_off, value); }
1311
1312 uint32_t bind_size() const INLINE { return E::get32(fields.bind_size); }
1313 void set_bind_size(uint32_t value) INLINE { E::set32(fields.bind_size, value); }
1314
1315 uint32_t weak_bind_off() const INLINE { return E::get32(fields.weak_bind_off); }
1316 void set_weak_bind_off(uint32_t value) INLINE { E::set32(fields.weak_bind_off, value); }
1317
1318 uint32_t weak_bind_size() const INLINE { return E::get32(fields.weak_bind_size); }
1319 void set_weak_bind_size(uint32_t value) INLINE { E::set32(fields.weak_bind_size, value); }
1320
1321 uint32_t lazy_bind_off() const INLINE { return E::get32(fields.lazy_bind_off); }
1322 void set_lazy_bind_off(uint32_t value) INLINE { E::set32(fields.lazy_bind_off, value); }
1323
1324 uint32_t lazy_bind_size() const INLINE { return E::get32(fields.lazy_bind_size); }
1325 void set_lazy_bind_size(uint32_t value) INLINE { E::set32(fields.lazy_bind_size, value); }
1326
1327 uint32_t export_off() const INLINE { return E::get32(fields.export_off); }
1328 void set_export_off(uint32_t value) INLINE { E::set32(fields.export_off, value); }
1329
1330 uint32_t export_size() const INLINE { return E::get32(fields.export_size); }
1331 void set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value); }
1332
1333
1334 typedef typename P::E E;
1335 private:
1336 dyld_info_command fields;
1337 };
1338
1339
1340 //
1341 // mach-o version load command
1342 //
1343 template <typename P>
1344 class macho_version_min_command {
1345 public:
1346 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1347 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1348
1349 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1350 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1351
1352 uint32_t version() const INLINE { return fields.version; }
1353 void set_version(uint32_t value) INLINE { E::set32(fields.version, value); }
1354
1355 #ifdef DICE_KIND_DATA
1356 uint32_t sdk() const INLINE { return fields.sdk; }
1357 void set_sdk(uint32_t value) INLINE { E::set32(fields.sdk, value); }
1358 #else
1359 uint32_t sdk() const INLINE { return fields.reserved; }
1360 void set_sdk(uint32_t value) INLINE { E::set32(fields.reserved, value); }
1361 #endif
1362
1363 typedef typename P::E E;
1364 private:
1365 version_min_command fields;
1366 };
1367
1368
1369 //
1370 // mach-o __LD, __compact_unwind section in object files
1371 //
1372 template <typename P>
1373 class macho_compact_unwind_entry {
1374 public:
1375 typedef typename P::E E;
1376 typedef typename P::uint_t pint_t;
1377
1378 pint_t codeStart() const INLINE { return P::getP(_codeStart); }
1379 void set_codeStart(pint_t value) INLINE { P::setP(_codeStart, value); }
1380
1381 uint32_t codeLen() const INLINE { return E::get32(_codeLen); }
1382 void set_codeLen(uint32_t value) INLINE { E::set32(_codeLen, value); }
1383
1384 uint32_t compactUnwindInfo() const INLINE { return E::get32(_compactUnwindInfo); }
1385 void set_compactUnwindInfo(uint32_t value) INLINE { E::set32(_compactUnwindInfo, value); }
1386
1387 pint_t personality() const INLINE { return P::getP(_personality); }
1388 void set_personality(pint_t value) INLINE { P::setP(_personality, value); }
1389
1390 pint_t lsda() const INLINE { return P::getP(_lsda); }
1391 void set_lsda(pint_t value) INLINE { P::setP(_lsda, value); }
1392
1393 static uint32_t codeStartFieldOffset() INLINE { return offsetof(macho_compact_unwind_entry<P>,_codeStart); }
1394 static uint32_t personalityFieldOffset() INLINE { return offsetof(macho_compact_unwind_entry<P>,_personality); }
1395 static uint32_t lsdaFieldOffset() INLINE { return offsetof(macho_compact_unwind_entry<P>,_lsda); }
1396
1397 private:
1398 pint_t _codeStart;
1399 uint32_t _codeLen;
1400 uint32_t _compactUnwindInfo;
1401 pint_t _personality;
1402 pint_t _lsda;
1403 };
1404
1405
1406 //
1407 // mach-o source version load command
1408 //
1409 template <typename P>
1410 class macho_source_version_command {
1411 public:
1412 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1413 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1414
1415 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1416 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1417
1418 uint64_t version() const INLINE { return fields.version; }
1419 void set_version(uint64_t value) INLINE { E::set64(fields.version, value); }
1420
1421 typedef typename P::E E;
1422 private:
1423 source_version_command fields;
1424 };
1425
1426
1427 //
1428 // mach-o source version load command
1429 //
1430 template <typename P>
1431 class macho_entry_point_command {
1432 public:
1433 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
1434 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
1435
1436 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
1437 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
1438
1439 uint64_t entryoff() const INLINE { return fields.entryoff; }
1440 void set_entryoff(uint64_t value) INLINE { E::set64(fields.entryoff, value); }
1441
1442 uint64_t stacksize() const INLINE { return fields.stacksize; }
1443 void set_stacksize(uint64_t value) INLINE { E::set64(fields.stacksize, value); }
1444
1445 typedef typename P::E E;
1446 private:
1447 entry_point_command fields;
1448 };
1449
1450
1451
1452 template <typename P>
1453 class macho_data_in_code_entry {
1454 public:
1455 uint32_t offset() const INLINE { return E::get32(fields.offset); }
1456 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
1457
1458 uint16_t length() const INLINE { return E::get16(fields.length); }
1459 void set_length(uint16_t value) INLINE { E::set16((uint16_t&)fields.length, value); }
1460
1461 uint16_t kind() const INLINE { return E::get16(fields.kind); }
1462 void set_kind(uint16_t value) INLINE { E::set16((uint16_t&)fields.kind, value); }
1463
1464 typedef typename P::E E;
1465 private:
1466 data_in_code_entry fields;
1467 };
1468
1469
1470
1471 #endif // __MACH_O_FILE_ABSTRACTION__
1472
1473