1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2005-2008 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
24 #ifndef __MACH_O_FILE_ABSTRACTION__
25 #define __MACH_O_FILE_ABSTRACTION__
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/ppc/reloc.h>
34 #include <mach-o/x86_64/reloc.h>
35 #include <mach/machine.h>
37 #include "FileAbstraction.hpp"
38 #include "Architectures.hpp"
40 // stuff that will eventually go away once newer cctools headers are widespread
41 #ifndef LC_LAZY_LOAD_DYLIB
42 #define LC_LAZY_LOAD_DYLIB 0x20
44 #ifndef S_LAZY_DYLIB_SYMBOL_POINTERS
45 #define S_LAZY_DYLIB_SYMBOL_POINTERS 0x10
47 #ifndef CPU_SUBTYPE_ARM_V5TEJ
48 #define CPU_SUBTYPE_ARM_V5TEJ ((cpu_subtype_t) 7)
50 #ifndef CPU_SUBTYPE_ARM_XSCALE
51 #define CPU_SUBTYPE_ARM_XSCALE ((cpu_subtype_t) 8)
53 #ifndef CPU_SUBTYPE_ARM_V7
54 #define CPU_SUBTYPE_ARM_V7 ((cpu_subtype_t) 9)
56 #ifndef N_ARM_THUMB_DEF
57 #define N_ARM_THUMB_DEF 0x0008
61 ARM_RELOC_VANILLA, /* generic relocation as discribed above */
62 ARM_RELOC_PAIR, /* the second relocation entry of a pair */
63 ARM_RELOC_SECTDIFF, /* a PAIR follows with subtract symbol value */
64 ARM_RELOC_LOCAL_SECTDIFF, /* like ARM_RELOC_SECTDIFF, but the symbol
65 referenced was local. */
66 ARM_RELOC_PB_LA_PTR,/* prebound lazy pointer */
67 ARM_RELOC_BR24, /* 24 bit branch displacement (to a word address) */
68 ARM_THUMB_RELOC_BR22, /* 22 bit branch displacement (to a half-word
72 #ifndef LC_ENCRYPTION_INFO
73 #define LC_ENCRYPTION_INFO 0x21
74 struct encryption_info_command {
77 uint32_t cryptoff; /* file offset of encrypted range */
78 uint32_t cryptsize; /* file size of encrypted range */
79 uint32_t cryptid; /* which enryption system, 0 means not-encrypted yet */
85 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
93 template <typename P> struct macho_header_content {};
94 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
95 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
96 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
97 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
102 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
103 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
105 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
106 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
108 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
109 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
111 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
112 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
114 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
115 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
117 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
118 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
120 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
121 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
123 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
124 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
126 typedef typename P::E E;
128 macho_header_content<P> header;
133 // mach-o load command
135 template <typename P>
136 class macho_load_command {
138 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
139 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
141 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
142 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
144 typedef typename P::E E;
146 load_command command;
151 // mach-o segment load command
153 template <typename P> struct macho_segment_content {};
154 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
155 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
156 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
157 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
159 template <typename P>
160 class macho_segment_command {
162 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
163 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
165 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
166 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
168 const char* segname() const INLINE { return segment.fields.segname; }
169 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
171 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
172 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
174 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
175 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
177 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
178 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
180 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
181 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
183 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
184 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
186 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
187 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
189 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
190 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
192 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
193 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
196 CMD = macho_segment_content<P>::CMD
199 typedef typename P::E E;
201 macho_segment_content<P> segment;
208 template <typename P> struct macho_section_content {};
209 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
210 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
211 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
212 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
214 template <typename P>
215 class macho_section {
217 const char* sectname() const INLINE { return section.fields.sectname; }
218 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
220 const char* segname() const INLINE { return section.fields.segname; }
221 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
223 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
224 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
226 uint64_t size() const INLINE { return P::getP(section.fields.size); }
227 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
229 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
230 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
232 uint32_t align() const INLINE { return E::get32(section.fields.align); }
233 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
235 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
236 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
238 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
239 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
241 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
242 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
244 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
245 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
247 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
248 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
250 typedef typename P::E E;
252 macho_section_content<P> section;
257 // mach-o dylib load command
259 template <typename P>
260 class macho_dylib_command {
262 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
263 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
265 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
266 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
268 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
269 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
271 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
272 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
274 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
275 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
277 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
278 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
280 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
281 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
283 typedef typename P::E E;
285 dylib_command fields;
290 // mach-o dylinker load command
292 template <typename P>
293 class macho_dylinker_command {
295 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
296 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
298 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
299 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
301 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
302 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
304 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
305 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
307 typedef typename P::E E;
309 dylinker_command fields;
314 // mach-o sub_framework load command
316 template <typename P>
317 class macho_sub_framework_command {
319 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
320 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
322 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
323 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
325 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
326 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
328 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
329 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
331 typedef typename P::E E;
333 sub_framework_command fields;
338 // mach-o sub_client load command
340 template <typename P>
341 class macho_sub_client_command {
343 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
344 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
346 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
347 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
349 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
350 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
352 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
353 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
355 typedef typename P::E E;
357 sub_client_command fields;
362 // mach-o sub_umbrella load command
364 template <typename P>
365 class macho_sub_umbrella_command {
367 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
368 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
370 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
371 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
373 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
374 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
376 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
377 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
379 typedef typename P::E E;
381 sub_umbrella_command fields;
386 // mach-o sub_library load command
388 template <typename P>
389 class macho_sub_library_command {
391 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
392 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
394 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
395 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
397 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
398 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
400 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
401 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
403 typedef typename P::E E;
405 sub_library_command fields;
410 // mach-o uuid load command
412 template <typename P>
413 class macho_uuid_command {
415 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
416 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
418 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
419 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
421 const uint8_t* uuid() const INLINE { return fields.uuid; }
422 void set_uuid(uint8_t uuid[16]) INLINE { memcpy(&fields.uuid, uuid, 16); }
424 typedef typename P::E E;
431 // mach-o routines load command
433 template <typename P> struct macho_routines_content {};
434 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
435 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
436 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
437 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
439 template <typename P>
440 class macho_routines_command {
442 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
443 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
445 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
446 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
448 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
449 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
451 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
452 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
454 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
455 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
457 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
458 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
460 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
461 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
463 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
464 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
466 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
467 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
469 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
470 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
472 typedef typename P::E E;
474 CMD = macho_routines_content<P>::CMD
477 macho_routines_content<P> routines;
482 // mach-o symbol table load command
484 template <typename P>
485 class macho_symtab_command {
487 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
488 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
490 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
491 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
493 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
494 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
496 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
497 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
499 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
500 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
502 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
503 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
506 typedef typename P::E E;
508 symtab_command fields;
513 // mach-o dynamic symbol table load command
515 template <typename P>
516 class macho_dysymtab_command {
518 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
519 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
521 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
522 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
524 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
525 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
527 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
528 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
530 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
531 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
533 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
534 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
536 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
537 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
539 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
540 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
542 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
543 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
545 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
546 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
548 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
549 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
551 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
552 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
554 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
555 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
557 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
558 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
560 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
561 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
563 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
564 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
566 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
567 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
569 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
570 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
572 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
573 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
575 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
576 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
578 typedef typename P::E E;
580 dysymtab_command fields;
587 // mach-o module table entry (for compatibility with old ld/dyld)
589 template <typename P> struct macho_dylib_module_content {};
590 template <> struct macho_dylib_module_content<Pointer32<BigEndian> > { struct dylib_module fields; };
591 template <> struct macho_dylib_module_content<Pointer32<LittleEndian> > { struct dylib_module fields; };
592 template <> struct macho_dylib_module_content<Pointer64<BigEndian> > { struct dylib_module_64 fields; };
593 template <> struct macho_dylib_module_content<Pointer64<LittleEndian> > { struct dylib_module_64 fields; };
595 template <typename P>
596 class macho_dylib_module {
598 uint32_t module_name() const INLINE { return E::get32(module.fields.module_name); }
599 void set_module_name(uint32_t value) INLINE { E::set32(module.fields.module_name, value); }
601 uint32_t iextdefsym() const INLINE { return E::get32(module.fields.iextdefsym); }
602 void set_iextdefsym(uint32_t value) INLINE { E::set32(module.fields.iextdefsym, value); }
604 uint32_t nextdefsym() const INLINE { return E::get32(module.fields.nextdefsym); }
605 void set_nextdefsym(uint32_t value) INLINE { E::set32(module.fields.nextdefsym, value); }
607 uint32_t irefsym() const INLINE { return E::get32(module.fields.irefsym); }
608 void set_irefsym(uint32_t value) INLINE { E::set32(module.fields.irefsym, value); }
610 uint32_t nrefsym() const INLINE { return E::get32(module.fields.nrefsym); }
611 void set_nrefsym(uint32_t value) INLINE { E::set32(module.fields.nrefsym, value); }
613 uint32_t ilocalsym() const INLINE { return E::get32(module.fields.ilocalsym); }
614 void set_ilocalsym(uint32_t value) INLINE { E::set32(module.fields.ilocalsym, value); }
616 uint32_t nlocalsym() const INLINE { return E::get32(module.fields.nlocalsym); }
617 void set_nlocalsym(uint32_t value) INLINE { E::set32(module.fields.nlocalsym, value); }
619 uint32_t iextrel() const INLINE { return E::get32(module.fields.iextrel); }
620 void set_iextrel(uint32_t value) INLINE { E::set32(module.fields.iextrel, value); }
622 uint32_t nextrel() const INLINE { return E::get32(module.fields.nextrel); }
623 void set_nextrel(uint32_t value) INLINE { E::set32(module.fields.nextrel, value); }
625 uint16_t iinit() const INLINE { return E::get32(module.fields.iinit_iterm) & 0xFFFF; }
626 uint16_t iterm() const INLINE { return E::get32(module.fields.iinit_iterm) > 16; }
627 void set_iinit_iterm(uint16_t init, uint16_t term) INLINE { E::set32(module.fields.iinit_iterm, (term<<16) | (init &0xFFFF)); }
629 uint16_t ninit() const INLINE { return E::get32(module.fields.ninit_nterm) & 0xFFFF; }
630 uint16_t nterm() const INLINE { return E::get32(module.fields.ninit_nterm) > 16; }
631 void set_ninit_nterm(uint16_t init, uint16_t term) INLINE { E::set32(module.fields.ninit_nterm, (term<<16) | (init &0xFFFF)); }
633 uint64_t objc_module_info_addr() const INLINE { return P::getP(module.fields.objc_module_info_addr); }
634 void set_objc_module_info_addr(uint64_t value) INLINE { P::setP(module.fields.objc_module_info_addr, value); }
636 uint32_t objc_module_info_size() const INLINE { return E::get32(module.fields.objc_module_info_size); }
637 void set_objc_module_info_size(uint32_t value) INLINE { E::set32(module.fields.objc_module_info_size, value); }
640 typedef typename P::E E;
642 macho_dylib_module_content<P> module;
647 // mach-o dylib_reference entry
649 template <typename P>
650 class macho_dylib_reference {
652 uint32_t isym() const INLINE { return E::getBits(fields, 0, 24); }
653 void set_isym(uint32_t value) INLINE { E::setBits(fields, value, 0, 24); }
655 uint8_t flags() const INLINE { return E::getBits(fields, 24, 8); }
656 void set_flags(uint8_t value) INLINE { E::setBits(fields, value, 24, 8); }
658 typedef typename P::E E;
666 // mach-o two-level hints load command
668 template <typename P>
669 class macho_dylib_table_of_contents {
671 uint32_t symbol_index() const INLINE { return E::get32(fields.symbol_index); }
672 void set_symbol_index(uint32_t value) INLINE { E::set32(fields.symbol_index, value); }
674 uint32_t module_index() const INLINE { return E::get32(fields.module_index); }
675 void set_module_index(uint32_t value) INLINE { E::set32(fields.module_index, value); }
677 typedef typename P::E E;
679 dylib_table_of_contents fields;
685 // mach-o two-level hints load command
687 template <typename P>
688 class macho_twolevel_hints_command {
690 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
691 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
693 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
694 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
696 uint32_t offset() const INLINE { return E::get32(fields.offset); }
697 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
699 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
700 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
702 typedef typename P::E E;
704 twolevel_hints_command fields;
709 // mach-o threads load command
711 template <typename P>
712 class macho_thread_command {
714 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
715 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
717 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
718 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
720 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
721 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
723 uint32_t count() const INLINE { return E::get32(fields_count); }
724 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
726 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
727 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
729 typedef typename P::E E;
730 typedef typename P::uint_t pint_t;
732 struct thread_command fields;
733 uint32_t fields_flavor;
734 uint32_t fields_count;
735 pint_t thread_registers[1];
742 template <typename P>
743 class macho_linkedit_data_command {
745 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
746 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
748 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
749 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
751 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
752 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
754 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
755 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
758 typedef typename P::E E;
760 struct linkedit_data_command fields;
767 template <typename P>
768 class macho_rpath_command {
770 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
771 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
773 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
774 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
776 uint32_t path_offset() const INLINE { return E::get32(fields.path.offset); }
777 void set_path_offset(uint32_t value) INLINE { E::set32(fields.path.offset, value); }
779 const char* path() const INLINE { return (const char*)&fields + path_offset(); }
780 void set_path_offset() INLINE { set_path_offset(sizeof(fields)); }
783 typedef typename P::E E;
785 struct rpath_command fields;
791 // mach-o symbol table entry
793 template <typename P> struct macho_nlist_content {};
794 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
795 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
796 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
797 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
799 template <typename P>
802 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
803 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
805 uint8_t n_type() const INLINE { return entry.fields.n_type; }
806 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
808 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
809 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
811 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
812 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
814 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
815 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
817 typedef typename P::E E;
819 macho_nlist_content<P> entry;
825 // mach-o relocation info
827 template <typename P>
828 class macho_relocation_info {
830 uint32_t r_address() const INLINE { return E::get32(address); }
831 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
833 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
834 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
836 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
837 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
839 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
840 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
842 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
843 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
845 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
846 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
848 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
850 typedef typename P::E E;
858 // mach-o scattered relocation info
859 // The bit fields are always in big-endian order (see mach-o/reloc.h)
861 template <typename P>
862 class macho_scattered_relocation_info {
864 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
865 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
867 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
868 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
870 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
871 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
873 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
874 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
876 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
877 void set_r_address(uint32_t x) { if ( x > 0x00FFFFFF ) throw "scattered reloc r_address too large";
878 uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
880 uint32_t r_value() const INLINE { return E::get32(value); }
881 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
883 uint32_t r_other() const INLINE { return other; }
885 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
887 typedef typename P::E E;
896 // mach-o encyrption info load command
898 template <typename P>
899 class macho_encryption_info_command {
901 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
902 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
904 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
905 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
907 uint32_t cryptoff() const INLINE { return E::get32(fields.cryptoff); }
908 void set_cryptoff(uint32_t value) INLINE { E::set32(fields.cryptoff, value); }
910 uint32_t cryptsize() const INLINE { return E::get32(fields.cryptsize); }
911 void set_cryptsize(uint32_t value) INLINE { E::set32(fields.cryptsize, value); }
913 uint32_t cryptid() const INLINE { return E::get32(fields.cryptid); }
914 void set_cryptid(uint32_t value) INLINE { E::set32(fields.cryptid, value); }
916 typedef typename P::E E;
918 encryption_info_command fields;
923 #endif // __MACH_O_FILE_ABSTRACTION__