1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2005 Apple Computer, 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/machine.h>
32 // suport older versions of mach-o/loader.h
36 uint32_t cmd; /* LC_UUID */
37 uint32_t cmdsize; /* sizeof(struct uuid_command) */
38 uint8_t uuid[16]; /* the 128-bit uuid */
42 #ifndef S_16BYTE_LITERALS
43 #define S_16BYTE_LITERALS 0xE
46 #ifndef CPU_SUBTYPE_ARM_V5TEJ
47 #define CPU_SUBTYPE_ARM_V5TEJ ((cpu_subtype_t) 7)
49 #ifndef CPU_SUBTYPE_ARM_XSCALE
50 #define CPU_SUBTYPE_ARM_XSCALE ((cpu_subtype_t) 8)
52 #ifndef CPU_SUBTYPE_ARM_V7
53 #define CPU_SUBTYPE_ARM_V7 ((cpu_subtype_t) 9)
55 #ifndef CPU_SUBTYPE_ARM_V7F
56 #define CPU_SUBTYPE_ARM_V7F ((cpu_subtype_t) 10)
58 #ifndef CPU_SUBTYPE_ARM_V7K
59 #define CPU_SUBTYPE_ARM_V7K ((cpu_subtype_t) 12)
61 #ifndef CPU_SUBTYPE_ARM_V7S
62 #define CPU_SUBTYPE_ARM_V7S ((cpu_subtype_t) 11)
65 #ifndef LC_LOAD_UPWARD_DYLIB
66 #define LC_LOAD_UPWARD_DYLIB (0x23|LC_REQ_DYLD) /* load of dylib whose initializers run later */
69 #ifndef EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
70 #define EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER 0x10
72 #ifndef EXPORT_SYMBOL_FLAGS_REEXPORT
73 #define EXPORT_SYMBOL_FLAGS_REEXPORT 0x08
76 #ifndef LC_FUNCTION_STARTS
77 #define LC_FUNCTION_STARTS 0x26
80 #ifndef LC_DATA_IN_CODE
81 #define LC_DATA_IN_CODE 0x29
84 #ifndef LC_DYLIB_CODE_SIGN_DRS
85 #define LC_DYLIB_CODE_SIGN_DRS 0x2B
90 #include "FileAbstraction.hpp"
91 #include "Architectures.hpp"
93 // utility to pair together a cpu-type and cpu-sub-type
99 ArchPair(uint32_t cputype, uint32_t cpusubtype) : arch(cputype), subtype(cpusubtype) {}
101 bool operator<(const ArchPair& other) const {
102 if ( this->arch != other.arch )
103 return (this->arch < other.arch);
104 return (this->subtype < other.subtype);
110 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
114 // mach-o load command
116 template <typename P>
117 class macho_load_command {
119 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
120 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
122 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
123 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
125 typedef typename P::E E;
127 load_command command;
132 // mach-o segment load command
134 template <typename P> struct macho_segment_content {};
135 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
136 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
137 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
138 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
140 template <typename P>
141 class macho_segment_command {
143 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
144 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
146 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
147 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
149 const char* segname() const INLINE { return segment.fields.segname; }
150 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
152 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
153 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
155 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
156 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
158 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
159 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
161 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
162 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
164 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
165 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
167 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
168 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
170 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
171 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
173 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
174 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
177 CMD = macho_segment_content<P>::CMD
180 typedef typename P::E E;
182 macho_segment_content<P> segment;
189 template <typename P> struct macho_section_content {};
190 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
191 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
192 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
193 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
195 template <typename P>
196 class macho_section {
198 const char* sectname() const INLINE { return section.fields.sectname; }
199 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
201 const char* segname() const INLINE { return section.fields.segname; }
202 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
204 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
205 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
207 uint64_t size() const INLINE { return P::getP(section.fields.size); }
208 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
210 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
211 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
213 uint32_t align() const INLINE { return E::get32(section.fields.align); }
214 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
216 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
217 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
219 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
220 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
222 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
223 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
225 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
226 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
228 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
229 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
231 typedef typename P::E E;
233 macho_section_content<P> section;
238 // mach-o dylib load command
240 template <typename P>
241 class macho_dylib_command {
243 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
244 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
246 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
247 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
249 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
250 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
252 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
253 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
255 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
256 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
258 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
259 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
261 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
262 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
264 typedef typename P::E E;
266 dylib_command fields;
271 // mach-o dylinker load command
273 template <typename P>
274 class macho_dylinker_command {
276 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
277 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
279 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
280 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
282 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
283 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
285 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
286 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
288 typedef typename P::E E;
290 dylinker_command fields;
295 // mach-o sub_framework load command
297 template <typename P>
298 class macho_sub_framework_command {
300 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
301 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
303 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
304 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
306 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
307 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
309 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
310 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
312 typedef typename P::E E;
314 sub_framework_command fields;
319 // mach-o sub_client load command
321 template <typename P>
322 class macho_sub_client_command {
324 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
325 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
327 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
328 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
330 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
331 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
333 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
334 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
336 typedef typename P::E E;
338 sub_client_command fields;
343 // mach-o sub_umbrella load command
345 template <typename P>
346 class macho_sub_umbrella_command {
348 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
349 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
351 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
352 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
354 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
355 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
357 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
358 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
360 typedef typename P::E E;
362 sub_umbrella_command fields;
367 // mach-o sub_library load command
369 template <typename P>
370 class macho_sub_library_command {
372 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
373 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
375 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
376 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
378 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
379 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
381 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
382 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
384 typedef typename P::E E;
386 sub_library_command fields;
391 // mach-o uuid load command
393 template <typename P>
394 class macho_uuid_command {
396 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
397 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
399 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
400 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
402 const uint8_t* uuid() const INLINE { return fields.uuid; }
403 void set_uuid(uint8_t value[16]) INLINE { memcpy(&fields.uuid, value, 16); }
405 typedef typename P::E E;
412 // mach-o routines load command
414 template <typename P> struct macho_routines_content {};
415 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
416 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
417 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
418 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
420 template <typename P>
421 class macho_routines_command {
423 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
424 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
426 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
427 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
429 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
430 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
432 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
433 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
435 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
436 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
438 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
439 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
441 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
442 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
444 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
445 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
447 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
448 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
450 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
451 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
453 typedef typename P::E E;
455 CMD = macho_routines_content<P>::CMD
458 macho_routines_content<P> routines;
463 // mach-o symbol table load command
465 template <typename P>
466 class macho_symtab_command {
468 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
469 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
471 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
472 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
474 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
475 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
477 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
478 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
480 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
481 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
483 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
484 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
487 typedef typename P::E E;
489 symtab_command fields;
494 // mach-o dynamic symbol table load command
496 template <typename P>
497 class macho_dysymtab_command {
499 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
500 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
502 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
503 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
505 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
506 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
508 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
509 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
511 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
512 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
514 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
515 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
517 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
518 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
520 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
521 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
523 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
524 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
526 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
527 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
529 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
530 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
532 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
533 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
535 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
536 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
538 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
539 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
541 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
542 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
544 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
545 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
547 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
548 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
550 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
551 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
553 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
554 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
556 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
557 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
559 typedef typename P::E E;
561 dysymtab_command fields;
566 // mach-o two-level hints load command
568 template <typename P>
569 class macho_twolevel_hints_command {
571 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
572 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
574 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
575 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
577 uint32_t offset() const INLINE { return E::get32(fields.offset); }
578 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
580 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
581 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
583 typedef typename P::E E;
585 twolevel_hints_command fields;
590 // mach-o threads load command
592 template <typename P>
593 class macho_thread_command {
595 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
596 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
598 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
599 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
601 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
602 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
604 uint32_t count() const INLINE { return E::get32(fields_count); }
605 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
607 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
608 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
610 typedef typename P::E E;
611 typedef typename P::uint_t pint_t;
613 struct thread_command fields;
614 uint32_t fields_flavor;
615 uint32_t fields_count;
616 pint_t thread_registers[1];
623 template <typename P>
624 class macho_linkedit_data_command {
626 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
627 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
629 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
630 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
632 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
633 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
635 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
636 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
639 typedef typename P::E E;
641 linkedit_data_command fields;
646 // mach-o symbol table entry
648 template <typename P> struct macho_nlist_content {};
649 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
650 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
651 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
652 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
654 template <typename P>
657 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
658 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
660 uint8_t n_type() const INLINE { return entry.fields.n_type; }
661 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
663 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
664 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
666 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
667 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
669 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
670 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
672 typedef typename P::E E;
674 macho_nlist_content<P> entry;
680 // mach-o relocation info
682 template <typename P>
683 class macho_relocation_info {
685 uint32_t r_address() const INLINE { return E::get32(address); }
686 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
688 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
689 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
691 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
692 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
694 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
695 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
697 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
698 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
700 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
701 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
703 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
705 typedef typename P::E E;
713 // mach-o scattered relocation info
714 // The bit fields are always in big-endian order (see mach-o/reloc.h)
716 template <typename P>
717 class macho_scattered_relocation_info {
719 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
720 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
722 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
723 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
725 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
726 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
728 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
729 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
731 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
732 void set_r_address(uint32_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
734 uint32_t r_value() const INLINE { return E::get32(value); }
735 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
737 uint32_t r_other() const INLINE { return other; }
739 typedef typename P::E E;
747 // mach-o file header
749 template <typename P> struct macho_header_content {};
750 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
751 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
752 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
753 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
755 template <typename P>
758 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
759 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
761 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
762 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
764 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
765 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
767 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
768 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
770 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
771 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
773 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
774 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
776 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
777 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
779 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
780 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
782 const macho_segment_command<P>* getSegment(const char *segname) const
784 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
785 uint32_t cmd_count = this->ncmds();
786 const macho_load_command<P>* cmd = cmds;
787 for (uint32_t i = 0; i < cmd_count; ++i) {
788 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
789 const macho_segment_command<P>* segcmd = (macho_segment_command<P>*)cmd;
790 if (0 == strncmp(segname, segcmd->segname(), 16)) {
794 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
799 const macho_section<P>* getSection(const char *segname, const char *sectname) const
801 const macho_segment_command<P>* segcmd = getSegment(segname);
802 if (!segcmd) return NULL;
804 const macho_section<P>* sectcmd = (macho_section<P>*)(segcmd+1);
805 uint32_t section_count = segcmd->nsects();
806 for (uint32_t j = 0; j < section_count; ++j) {
807 if (0 == ::strncmp(sectcmd[j].sectname(), sectname, 16)) {
815 const macho_load_command<P>* getLoadCommand(int query) const
817 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
818 uint32_t cmd_count = this->ncmds();
819 const macho_load_command<P>* cmd = cmds;
820 for (uint32_t i = 0; i < cmd_count; ++i) {
821 if ( cmd->cmd() == query ) {
824 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
829 typedef typename P::E E;
831 macho_header_content<P> header;
837 // compressed dyld info load command
839 template <typename P>
840 class macho_dyld_info_command {
842 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
843 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
845 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
846 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
848 uint32_t rebase_off() const INLINE { return E::get32(fields.rebase_off); }
849 void set_rebase_off(uint32_t value) INLINE { E::set32(fields.rebase_off, value); }
851 uint32_t rebase_size() const INLINE { return E::get32(fields.rebase_size); }
852 void set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value); }
854 uint32_t bind_off() const INLINE { return E::get32(fields.bind_off); }
855 void set_bind_off(uint32_t value) INLINE { E::set32(fields.bind_off, value); }
857 uint32_t bind_size() const INLINE { return E::get32(fields.bind_size); }
858 void set_bind_size(uint32_t value) INLINE { E::set32(fields.bind_size, value); }
860 uint32_t weak_bind_off() const INLINE { return E::get32(fields.weak_bind_off); }
861 void set_weak_bind_off(uint32_t value) INLINE { E::set32(fields.weak_bind_off, value); }
863 uint32_t weak_bind_size() const INLINE { return E::get32(fields.weak_bind_size); }
864 void set_weak_bind_size(uint32_t value) INLINE { E::set32(fields.weak_bind_size, value); }
866 uint32_t lazy_bind_off() const INLINE { return E::get32(fields.lazy_bind_off); }
867 void set_lazy_bind_off(uint32_t value) INLINE { E::set32(fields.lazy_bind_off, value); }
869 uint32_t lazy_bind_size() const INLINE { return E::get32(fields.lazy_bind_size); }
870 void set_lazy_bind_size(uint32_t value) INLINE { E::set32(fields.lazy_bind_size, value); }
872 uint32_t export_off() const INLINE { return E::get32(fields.export_off); }
873 void set_export_off(uint32_t value) INLINE { E::set32(fields.export_off, value); }
875 uint32_t export_size() const INLINE { return E::get32(fields.export_size); }
876 void set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value); }
879 typedef typename P::E E;
881 dyld_info_command fields;
885 inline uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end) {
890 throw "malformed uleb128 extends beyond trie";
892 uint64_t slice = *p & 0x7f;
894 if (bit >= 64 || slice << bit >> bit != slice)
895 throw "uleb128 too big for 64-bits";
897 result |= (slice << bit);
906 static int64_t read_sleb128(const uint8_t*& p, const uint8_t* end)
913 throw "malformed sleb128";
915 result |= ((byte & 0x7f) << bit);
917 } while (byte & 0x80);
918 // sign extend negative numbers
919 if ( (byte & 0x40) != 0 )
920 result |= (-1LL) << bit;
927 #endif // __MACH_O_FILE_ABSTRACTION__