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 #define DYLD_CACHE_ADJ_V2_FORMAT 0x7F
38 #define DYLD_CACHE_ADJ_V2_POINTER_32 0x01
39 #define DYLD_CACHE_ADJ_V2_POINTER_64 0x02
40 #define DYLD_CACHE_ADJ_V2_DELTA_32 0x03
41 #define DYLD_CACHE_ADJ_V2_DELTA_64 0x04
42 #define DYLD_CACHE_ADJ_V2_ARM64_ADRP 0x05
43 #define DYLD_CACHE_ADJ_V2_ARM64_OFF12 0x06
44 #define DYLD_CACHE_ADJ_V2_ARM64_BR26 0x07
45 #define DYLD_CACHE_ADJ_V2_ARM_MOVW_MOVT 0x08
46 #define DYLD_CACHE_ADJ_V2_ARM_BR24 0x09
47 #define DYLD_CACHE_ADJ_V2_THUMB_MOVW_MOVT 0x0A
48 #define DYLD_CACHE_ADJ_V2_THUMB_BR22 0x0B
49 #define DYLD_CACHE_ADJ_V2_IMAGE_OFF_32 0x0C
50 #define DYLD_CACHE_ADJ_V2_THREADED_POINTER_64 0x0D
52 #ifndef LC_FILESET_ENTRY
53 #define LC_FILESET_ENTRY (0x35 | LC_REQ_DYLD) /* used with fileset_entry_command */
54 struct fileset_entry_command {
55 uint32_t cmd; /* LC_FILESET_ENTRY */
56 uint32_t cmdsize; /* includes id string */
57 uint64_t vmaddr; /* memory address of the dylib */
58 uint64_t fileoff; /* file offset of the dylib */
59 union lc_str entry_id; /* contained entry id */
60 uint32_t reserved; /* entry_id is 32-bits long, so this is the reserved padding */
65 #include "FileAbstraction.hpp"
66 //#include "Architectures.hpp"
68 // utility to pair together a cpu-type and cpu-sub-type
74 ArchPair(uint32_t cputype, uint32_t cpusubtype) : arch(cputype), subtype(cpusubtype) {}
76 bool operator<(const ArchPair& other) const {
77 if ( this->arch != other.arch )
78 return (this->arch < other.arch);
79 return (this->subtype < other.subtype);
82 bool operator==(const ArchPair& other) const {
83 return this->arch == other.arch && this->subtype == other.subtype;
89 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
93 // mach-o load command
96 class macho_load_command {
98 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
99 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
101 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
102 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
104 typedef typename P::E E;
106 load_command command;
111 // mach-o segment load command
113 template <typename P> struct macho_segment_content {};
114 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
115 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
116 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
117 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
119 template <typename P>
120 class macho_segment_command {
122 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
123 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
125 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
126 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
128 const char* segname() const INLINE { return segment.fields.segname; }
129 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
131 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
132 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
134 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
135 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
137 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
138 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
140 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
141 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
143 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
144 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
146 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
147 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
149 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
150 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
152 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
153 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
156 CMD = macho_segment_content<P>::CMD
159 typedef typename P::E E;
161 macho_segment_content<P> segment;
168 template <typename P> struct macho_section_content {};
169 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
170 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
171 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
172 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
174 template <typename P>
175 class macho_section {
177 const char* sectname() const INLINE { return section.fields.sectname; }
178 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
180 const char* segname() const INLINE { return section.fields.segname; }
181 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
183 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
184 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
186 uint64_t size() const INLINE { return P::getP(section.fields.size); }
187 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
189 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
190 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
192 uint32_t align() const INLINE { return E::get32(section.fields.align); }
193 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
195 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
196 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
198 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
199 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
201 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
202 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
204 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
205 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
207 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
208 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
210 typedef typename P::E E;
212 macho_section_content<P> section;
217 // mach-o dylib load command
219 template <typename P>
220 class macho_dylib_command {
222 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
223 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
225 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
226 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
228 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
229 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
231 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
232 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
234 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
235 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
237 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
238 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
240 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
241 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
243 typedef typename P::E E;
245 dylib_command fields;
250 // mach-o dylinker load command
252 template <typename P>
253 class macho_dylinker_command {
255 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
256 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
258 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
259 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
261 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
262 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
264 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
265 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
267 typedef typename P::E E;
269 dylinker_command fields;
274 // mach-o sub_framework load command
276 template <typename P>
277 class macho_sub_framework_command {
279 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
280 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
282 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
283 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
285 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
286 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
288 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
289 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
291 typedef typename P::E E;
293 sub_framework_command fields;
298 // mach-o sub_client load command
300 template <typename P>
301 class macho_sub_client_command {
303 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
304 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
306 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
307 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
309 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
310 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
312 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
313 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
315 typedef typename P::E E;
317 sub_client_command fields;
322 // mach-o sub_umbrella load command
324 template <typename P>
325 class macho_sub_umbrella_command {
327 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
328 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
330 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
331 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
333 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
334 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
336 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
337 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
339 typedef typename P::E E;
341 sub_umbrella_command fields;
346 // mach-o sub_library load command
348 template <typename P>
349 class macho_sub_library_command {
351 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
352 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
354 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
355 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
357 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
358 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
360 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
361 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
363 typedef typename P::E E;
365 sub_library_command fields;
370 // mach-o uuid load command
372 template <typename P>
373 class macho_uuid_command {
375 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
376 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
378 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
379 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
381 const uint8_t* uuid() const INLINE { return fields.uuid; }
382 void set_uuid(uint8_t value[16]) INLINE { memcpy(&fields.uuid, value, 16); }
384 typedef typename P::E E;
391 // mach-o routines load command
393 template <typename P> struct macho_routines_content {};
394 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
395 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
396 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
397 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
399 template <typename P>
400 class macho_routines_command {
402 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
403 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
405 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
406 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
408 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
409 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
411 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
412 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
414 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
415 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
417 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
418 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
420 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
421 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
423 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
424 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
426 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
427 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
429 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
430 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
432 typedef typename P::E E;
434 CMD = macho_routines_content<P>::CMD
437 macho_routines_content<P> routines;
442 // mach-o symbol table load command
444 template <typename P>
445 class macho_symtab_command {
447 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
448 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
450 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
451 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
453 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
454 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
456 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
457 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
459 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
460 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
462 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
463 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
466 typedef typename P::E E;
468 symtab_command fields;
473 // mach-o dynamic symbol table load command
475 template <typename P>
476 class macho_dysymtab_command {
478 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
479 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
481 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
482 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
484 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
485 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
487 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
488 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
490 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
491 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
493 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
494 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
496 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
497 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
499 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
500 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
502 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
503 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
505 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
506 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
508 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
509 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
511 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
512 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
514 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
515 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
517 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
518 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
520 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
521 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
523 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
524 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
526 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
527 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
529 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
530 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
532 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
533 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
535 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
536 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
538 typedef typename P::E E;
540 dysymtab_command fields;
545 // mach-o two-level hints load command
547 template <typename P>
548 class macho_twolevel_hints_command {
550 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
551 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
553 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
554 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
556 uint32_t offset() const INLINE { return E::get32(fields.offset); }
557 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
559 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
560 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
562 typedef typename P::E E;
564 twolevel_hints_command fields;
569 // mach-o threads load command
571 template <typename P>
572 class macho_thread_command {
574 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
575 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
577 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
578 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
580 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
581 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
583 uint32_t count() const INLINE { return E::get32(fields_count); }
584 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
586 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
587 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
589 typedef typename P::E E;
590 typedef typename P::uint_t pint_t;
592 struct thread_command fields;
593 uint32_t fields_flavor;
594 uint32_t fields_count;
595 pint_t thread_registers[1];
602 template <typename P>
603 class macho_linkedit_data_command {
605 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
606 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
608 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
609 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
611 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
612 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
614 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
615 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
618 typedef typename P::E E;
620 linkedit_data_command fields;
625 // mach-o symbol table entry
627 template <typename P> struct macho_nlist_content {};
628 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
629 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
630 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
631 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
633 template <typename P>
636 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
637 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
639 uint8_t n_type() const INLINE { return entry.fields.n_type; }
640 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
642 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
643 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
645 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
646 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
648 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
649 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
651 typedef typename P::E E;
653 macho_nlist_content<P> entry;
659 // mach-o relocation info
661 template <typename P>
662 class macho_relocation_info {
664 uint32_t r_address() const INLINE { return E::get32(address); }
665 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
667 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
668 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
670 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
671 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
673 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
674 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
676 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
677 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
679 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
680 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
682 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
684 typedef typename P::E E;
692 // mach-o scattered relocation info
693 // The bit fields are always in big-endian order (see mach-o/reloc.h)
695 template <typename P>
696 class macho_scattered_relocation_info {
698 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
699 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
701 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
702 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
704 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
705 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
707 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
708 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
710 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
711 void set_r_address(uint32_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
713 uint32_t r_value() const INLINE { return E::get32(value); }
714 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
716 uint32_t r_other() const INLINE { return other; }
718 typedef typename P::E E;
726 // mach-o file header
728 template <typename P> struct macho_header_content {};
729 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
730 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
731 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
732 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
734 template <typename P>
737 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
738 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
740 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
741 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
743 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
744 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
746 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
747 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
749 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
750 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
752 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
753 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
755 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
756 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
758 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
759 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
761 const macho_segment_command<P>* getSegment(const char *segname) const
763 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
764 uint32_t cmd_count = this->ncmds();
765 const macho_load_command<P>* cmd = cmds;
766 for (uint32_t i = 0; i < cmd_count; ++i) {
767 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
768 const macho_segment_command<P>* segcmd = (macho_segment_command<P>*)cmd;
769 if (0 == strncmp(segname, segcmd->segname(), 16)) {
773 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
778 const macho_section<P>* getSection(const char *segname, const char *sectname) const
780 const macho_segment_command<P>* segcmd = getSegment(segname);
781 if (!segcmd) return NULL;
783 const macho_section<P>* sectcmd = (macho_section<P>*)(segcmd+1);
784 uint32_t section_count = segcmd->nsects();
785 for (uint32_t j = 0; j < section_count; ++j) {
786 if (0 == ::strncmp(sectcmd[j].sectname(), sectname, 16)) {
791 if (strcmp(segname, "__DATA") == 0) {
792 if (const macho_section<P>* dataConst = getSection("__DATA_CONST", sectname))
794 if (const macho_section<P>* dataDirty = getSection("__DATA_DIRTY", sectname))
800 const macho_load_command<P>* getLoadCommand(int query) const
802 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
803 uint32_t cmd_count = this->ncmds();
804 const macho_load_command<P>* cmd = cmds;
805 for (uint32_t i = 0; i < cmd_count; ++i) {
806 if ( cmd->cmd() == query ) {
809 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
814 typedef typename P::E E;
816 macho_header_content<P> header;
822 // compressed dyld info load command
824 template <typename P>
825 class macho_dyld_info_command {
827 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
828 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
830 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
831 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
833 uint32_t rebase_off() const INLINE { return E::get32(fields.rebase_off); }
834 void set_rebase_off(uint32_t value) INLINE { E::set32(fields.rebase_off, value); }
836 uint32_t rebase_size() const INLINE { return E::get32(fields.rebase_size); }
837 void set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value); }
839 uint32_t bind_off() const INLINE { return E::get32(fields.bind_off); }
840 void set_bind_off(uint32_t value) INLINE { E::set32(fields.bind_off, value); }
842 uint32_t bind_size() const INLINE { return E::get32(fields.bind_size); }
843 void set_bind_size(uint32_t value) INLINE { E::set32(fields.bind_size, value); }
845 uint32_t weak_bind_off() const INLINE { return E::get32(fields.weak_bind_off); }
846 void set_weak_bind_off(uint32_t value) INLINE { E::set32(fields.weak_bind_off, value); }
848 uint32_t weak_bind_size() const INLINE { return E::get32(fields.weak_bind_size); }
849 void set_weak_bind_size(uint32_t value) INLINE { E::set32(fields.weak_bind_size, value); }
851 uint32_t lazy_bind_off() const INLINE { return E::get32(fields.lazy_bind_off); }
852 void set_lazy_bind_off(uint32_t value) INLINE { E::set32(fields.lazy_bind_off, value); }
854 uint32_t lazy_bind_size() const INLINE { return E::get32(fields.lazy_bind_size); }
855 void set_lazy_bind_size(uint32_t value) INLINE { E::set32(fields.lazy_bind_size, value); }
857 uint32_t export_off() const INLINE { return E::get32(fields.export_off); }
858 void set_export_off(uint32_t value) INLINE { E::set32(fields.export_off, value); }
860 uint32_t export_size() const INLINE { return E::get32(fields.export_size); }
861 void set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value); }
864 typedef typename P::E E;
866 dyld_info_command fields;
871 // mach-o build version load command
873 template <typename P>
874 class macho_build_version_command {
876 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
877 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
879 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
880 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
882 uint32_t platform() const INLINE { return E::get32(fields.platform); }
883 void set_platform(uint32_t value) INLINE { E::set32(fields.platform, value); }
885 uint32_t minos() const INLINE { return E::get32(fields.minos); }
886 void set_minos(uint32_t value) INLINE { E::set32(fields.minos, value); }
888 uint32_t sdk() const INLINE { return E::get32(fields.sdk); }
889 void set_sdk(uint32_t value) INLINE { E::set32(fields.sdk, value); }
891 uint32_t ntools() const INLINE { return E::get32(fields.ntools); }
892 void set_ntools(uint32_t value) INLINE { E::set32(fields.ntools, value); }
894 typedef typename P::E E;
896 build_version_command fields;
900 // mach-o routines load command
902 template <typename P> struct macho_fileset_entry_command_content {};
903 template <> struct macho_fileset_entry_command_content<Pointer32<BigEndian> > { fileset_entry_command fields; enum { CMD = LC_FILESET_ENTRY }; };
904 template <> struct macho_fileset_entry_command_content<Pointer64<BigEndian> > { fileset_entry_command fields; enum { CMD = LC_FILESET_ENTRY }; };
905 template <> struct macho_fileset_entry_command_content<Pointer32<LittleEndian> > { fileset_entry_command fields; enum { CMD = LC_FILESET_ENTRY }; };
906 template <> struct macho_fileset_entry_command_content<Pointer64<LittleEndian> > { fileset_entry_command fields; enum { CMD = LC_FILESET_ENTRY }; };
908 template <typename P>
909 class macho_fileset_entry_command {
911 uint32_t cmd() const INLINE { return E::get32(cache_entry_id.fields.cmd); }
912 void set_cmd(uint32_t value) INLINE { E::set32(cache_entry_id.fields.cmd, value); }
914 uint32_t cmdsize() const INLINE { return E::get32(cache_entry_id.fields.cmdsize); }
915 void set_cmdsize(uint32_t value) INLINE { E::set32(cache_entry_id.fields.cmdsize, value); }
917 uint64_t vmaddr() const INLINE { return P::getP(cache_entry_id.fields.vmaddr); }
918 void set_vmaddr(uint64_t value) INLINE { P::setP(cache_entry_id.fields.vmaddr, value); }
920 uint64_t fileoff() const INLINE { return P::getP(cache_entry_id.fields.fileoff); }
921 void set_fileoff(uint64_t value) INLINE { P::setP(cache_entry_id.fields.fileoff, value); }
923 uint32_t entry_id_offset() const INLINE { return E::get32(cache_entry_id.fields.entry_id.offset); }
924 void set_entry_id_offset(uint32_t value) INLINE { E::set32(cache_entry_id.fields.entry_id.offset, value); }
926 const char* entry_id() const INLINE { return (const char*)&cache_entry_id.fields + entry_id_offset(); }
927 void set_entry_id(const char* value) INLINE {
928 set_entry_id_offset(sizeof(cache_entry_id.fields));
929 strcpy(((char*)&cache_entry_id.fields) + sizeof(cache_entry_id.fields), value);
932 typedef typename P::E E;
934 CMD = macho_fileset_entry_command_content<P>::CMD
937 macho_fileset_entry_command_content<P> cache_entry_id;
941 inline uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end) {
946 throw "malformed uleb128 extends beyond trie";
948 uint64_t slice = *p & 0x7f;
950 if (bit >= 64 || slice << bit >> bit != slice)
951 throw "uleb128 too big for 64-bits";
953 result |= (slice << bit);
962 inline int64_t read_sleb128(const uint8_t*& p, const uint8_t* end)
969 throw "malformed sleb128";
971 result |= (((int64_t)(byte & 0x7f)) << bit);
973 } while (byte & 0x80);
974 // sign extend negative numbers
975 if ( ((byte & 0x40) != 0) && (bit < 64) )
976 result |= (~0ULL) << bit;
983 #endif // __MACH_O_FILE_ABSTRACTION__