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)
64 #ifndef CPU_SUBTYPE_ARM64_ALL
65 #define CPU_SUBTYPE_ARM64_ALL ((cpu_subtype_t) 0)
67 #ifndef CPU_TYPE_ARM64
68 #define CPU_TYPE_ARM64 ((cpu_type_t) (CPU_TYPE_ARM | CPU_ARCH_ABI64))
70 #ifndef CPU_TYPE_ARM64_32
71 #ifndef CPU_ARCH_ABI64_32
72 #define CPU_ARCH_ABI64_32 0x02000000
74 #define CPU_TYPE_ARM64_32 (CPU_TYPE_ARM | CPU_ARCH_ABI64_32)
77 #ifndef CPU_SUBTYPE_ARM64_32_V8
78 #define CPU_SUBTYPE_ARM64_32_V8 1
81 #ifndef CPU_SUBTYPE_ARM64_E
82 #define CPU_SUBTYPE_ARM64_E 2
85 #define ARM64_RELOC_UNSIGNED 0 // for pointers
88 #ifndef LC_LOAD_UPWARD_DYLIB
89 #define LC_LOAD_UPWARD_DYLIB (0x23|LC_REQ_DYLD) /* load of dylib whose initializers run later */
92 #ifndef EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
93 #define EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER 0x10
95 #ifndef EXPORT_SYMBOL_FLAGS_REEXPORT
96 #define EXPORT_SYMBOL_FLAGS_REEXPORT 0x08
99 #ifndef LC_FUNCTION_STARTS
100 #define LC_FUNCTION_STARTS 0x26
103 #ifndef LC_DATA_IN_CODE
104 #define LC_DATA_IN_CODE 0x29
107 #ifndef LC_DYLIB_CODE_SIGN_DRS
108 #define LC_DYLIB_CODE_SIGN_DRS 0x2B
111 #ifndef CPU_SUBTYPE_X86_64_H
112 #define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t) 8)
116 #define DYLD_CACHE_ADJ_V2_FORMAT 0x7F
118 #define DYLD_CACHE_ADJ_V2_POINTER_32 0x01
119 #define DYLD_CACHE_ADJ_V2_POINTER_64 0x02
120 #define DYLD_CACHE_ADJ_V2_DELTA_32 0x03
121 #define DYLD_CACHE_ADJ_V2_DELTA_64 0x04
122 #define DYLD_CACHE_ADJ_V2_ARM64_ADRP 0x05
123 #define DYLD_CACHE_ADJ_V2_ARM64_OFF12 0x06
124 #define DYLD_CACHE_ADJ_V2_ARM64_BR26 0x07
125 #define DYLD_CACHE_ADJ_V2_ARM_MOVW_MOVT 0x08
126 #define DYLD_CACHE_ADJ_V2_ARM_BR24 0x09
127 #define DYLD_CACHE_ADJ_V2_THUMB_MOVW_MOVT 0x0A
128 #define DYLD_CACHE_ADJ_V2_THUMB_BR22 0x0B
129 #define DYLD_CACHE_ADJ_V2_IMAGE_OFF_32 0x0C
130 #define DYLD_CACHE_ADJ_V2_THREADED_POINTER_64 0x0D
132 #define MH_HAS_OBJC 0x40000000
134 #include "FileAbstraction.hpp"
135 //#include "Architectures.hpp"
137 // utility to pair together a cpu-type and cpu-sub-type
143 ArchPair(uint32_t cputype, uint32_t cpusubtype) : arch(cputype), subtype(cpusubtype) {}
145 bool operator<(const ArchPair& other) const {
146 if ( this->arch != other.arch )
147 return (this->arch < other.arch);
148 return (this->subtype < other.subtype);
151 bool operator==(const ArchPair& other) const {
152 return this->arch == other.arch && this->subtype == other.subtype;
158 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
162 // mach-o load command
164 template <typename P>
165 class macho_load_command {
167 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
168 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
170 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
171 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
173 typedef typename P::E E;
175 load_command command;
180 // mach-o segment load command
182 template <typename P> struct macho_segment_content {};
183 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
184 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
185 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
186 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
188 template <typename P>
189 class macho_segment_command {
191 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
192 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
194 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
195 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
197 const char* segname() const INLINE { return segment.fields.segname; }
198 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
200 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
201 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
203 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
204 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
206 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
207 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
209 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
210 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
212 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
213 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
215 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
216 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
218 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
219 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
221 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
222 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
225 CMD = macho_segment_content<P>::CMD
228 typedef typename P::E E;
230 macho_segment_content<P> segment;
237 template <typename P> struct macho_section_content {};
238 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
239 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
240 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
241 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
243 template <typename P>
244 class macho_section {
246 const char* sectname() const INLINE { return section.fields.sectname; }
247 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
249 const char* segname() const INLINE { return section.fields.segname; }
250 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
252 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
253 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
255 uint64_t size() const INLINE { return P::getP(section.fields.size); }
256 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
258 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
259 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
261 uint32_t align() const INLINE { return E::get32(section.fields.align); }
262 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
264 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
265 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
267 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
268 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
270 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
271 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
273 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
274 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
276 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
277 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
279 typedef typename P::E E;
281 macho_section_content<P> section;
286 // mach-o dylib load command
288 template <typename P>
289 class macho_dylib_command {
291 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
292 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
294 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
295 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
297 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
298 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
300 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
301 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
303 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
304 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
306 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
307 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
309 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
310 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
312 typedef typename P::E E;
314 dylib_command fields;
319 // mach-o dylinker load command
321 template <typename P>
322 class macho_dylinker_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 name_offset() const INLINE { return E::get32(fields.name.offset); }
331 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
333 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
334 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
336 typedef typename P::E E;
338 dylinker_command fields;
343 // mach-o sub_framework load command
345 template <typename P>
346 class macho_sub_framework_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 umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
355 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
357 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
358 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
360 typedef typename P::E E;
362 sub_framework_command fields;
367 // mach-o sub_client load command
369 template <typename P>
370 class macho_sub_client_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 client_offset() const INLINE { return E::get32(fields.client.offset); }
379 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
381 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
382 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
384 typedef typename P::E E;
386 sub_client_command fields;
391 // mach-o sub_umbrella load command
393 template <typename P>
394 class macho_sub_umbrella_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 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
403 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
405 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
406 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
408 typedef typename P::E E;
410 sub_umbrella_command fields;
415 // mach-o sub_library load command
417 template <typename P>
418 class macho_sub_library_command {
420 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
421 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
423 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
424 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
426 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
427 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
429 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
430 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
432 typedef typename P::E E;
434 sub_library_command fields;
439 // mach-o uuid load command
441 template <typename P>
442 class macho_uuid_command {
444 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
445 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
447 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
448 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
450 const uint8_t* uuid() const INLINE { return fields.uuid; }
451 void set_uuid(uint8_t value[16]) INLINE { memcpy(&fields.uuid, value, 16); }
453 typedef typename P::E E;
460 // mach-o routines load command
462 template <typename P> struct macho_routines_content {};
463 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
464 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
465 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
466 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
468 template <typename P>
469 class macho_routines_command {
471 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
472 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
474 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
475 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
477 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
478 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
480 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
481 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
483 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
484 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
486 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
487 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
489 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
490 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
492 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
493 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
495 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
496 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
498 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
499 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
501 typedef typename P::E E;
503 CMD = macho_routines_content<P>::CMD
506 macho_routines_content<P> routines;
511 // mach-o symbol table load command
513 template <typename P>
514 class macho_symtab_command {
516 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
517 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
519 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
520 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
522 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
523 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
525 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
526 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
528 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
529 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
531 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
532 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
535 typedef typename P::E E;
537 symtab_command fields;
542 // mach-o dynamic symbol table load command
544 template <typename P>
545 class macho_dysymtab_command {
547 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
548 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
550 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
551 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
553 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
554 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
556 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
557 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
559 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
560 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
562 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
563 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
565 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
566 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
568 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
569 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
571 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
572 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
574 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
575 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
577 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
578 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
580 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
581 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
583 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
584 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
586 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
587 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
589 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
590 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
592 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
593 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
595 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
596 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
598 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
599 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
601 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
602 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
604 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
605 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
607 typedef typename P::E E;
609 dysymtab_command fields;
614 // mach-o two-level hints load command
616 template <typename P>
617 class macho_twolevel_hints_command {
619 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
620 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
622 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
623 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
625 uint32_t offset() const INLINE { return E::get32(fields.offset); }
626 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
628 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
629 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
631 typedef typename P::E E;
633 twolevel_hints_command fields;
638 // mach-o threads load command
640 template <typename P>
641 class macho_thread_command {
643 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
644 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
646 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
647 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
649 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
650 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
652 uint32_t count() const INLINE { return E::get32(fields_count); }
653 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
655 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
656 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
658 typedef typename P::E E;
659 typedef typename P::uint_t pint_t;
661 struct thread_command fields;
662 uint32_t fields_flavor;
663 uint32_t fields_count;
664 pint_t thread_registers[1];
671 template <typename P>
672 class macho_linkedit_data_command {
674 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
675 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
677 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
678 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
680 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
681 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
683 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
684 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
687 typedef typename P::E E;
689 linkedit_data_command fields;
694 // mach-o symbol table entry
696 template <typename P> struct macho_nlist_content {};
697 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
698 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
699 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
700 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
702 template <typename P>
705 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
706 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
708 uint8_t n_type() const INLINE { return entry.fields.n_type; }
709 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
711 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
712 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
714 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
715 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
717 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
718 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
720 typedef typename P::E E;
722 macho_nlist_content<P> entry;
728 // mach-o relocation info
730 template <typename P>
731 class macho_relocation_info {
733 uint32_t r_address() const INLINE { return E::get32(address); }
734 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
736 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
737 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
739 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
740 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
742 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
743 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
745 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
746 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
748 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
749 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
751 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
753 typedef typename P::E E;
761 // mach-o scattered relocation info
762 // The bit fields are always in big-endian order (see mach-o/reloc.h)
764 template <typename P>
765 class macho_scattered_relocation_info {
767 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
768 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
770 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
771 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
773 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
774 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
776 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
777 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
779 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
780 void set_r_address(uint32_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
782 uint32_t r_value() const INLINE { return E::get32(value); }
783 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
785 uint32_t r_other() const INLINE { return other; }
787 typedef typename P::E E;
795 // mach-o file header
797 template <typename P> struct macho_header_content {};
798 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
799 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
800 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
801 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
803 template <typename P>
806 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
807 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
809 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
810 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
812 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
813 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
815 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
816 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
818 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
819 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
821 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
822 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
824 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
825 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
827 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
828 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
830 const macho_segment_command<P>* getSegment(const char *segname) const
832 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
833 uint32_t cmd_count = this->ncmds();
834 const macho_load_command<P>* cmd = cmds;
835 for (uint32_t i = 0; i < cmd_count; ++i) {
836 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
837 const macho_segment_command<P>* segcmd = (macho_segment_command<P>*)cmd;
838 if (0 == strncmp(segname, segcmd->segname(), 16)) {
842 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
847 const macho_section<P>* getSection(const char *segname, const char *sectname) const
849 const macho_segment_command<P>* segcmd = getSegment(segname);
850 if (!segcmd) return NULL;
852 const macho_section<P>* sectcmd = (macho_section<P>*)(segcmd+1);
853 uint32_t section_count = segcmd->nsects();
854 for (uint32_t j = 0; j < section_count; ++j) {
855 if (0 == ::strncmp(sectcmd[j].sectname(), sectname, 16)) {
860 if (strcmp(segname, "__DATA") == 0)
861 return getSection("__DATA_CONST", sectname);
865 const macho_load_command<P>* getLoadCommand(int query) const
867 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
868 uint32_t cmd_count = this->ncmds();
869 const macho_load_command<P>* cmd = cmds;
870 for (uint32_t i = 0; i < cmd_count; ++i) {
871 if ( cmd->cmd() == query ) {
874 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
879 typedef typename P::E E;
881 macho_header_content<P> header;
887 // compressed dyld info load command
889 template <typename P>
890 class macho_dyld_info_command {
892 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
893 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
895 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
896 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
898 uint32_t rebase_off() const INLINE { return E::get32(fields.rebase_off); }
899 void set_rebase_off(uint32_t value) INLINE { E::set32(fields.rebase_off, value); }
901 uint32_t rebase_size() const INLINE { return E::get32(fields.rebase_size); }
902 void set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value); }
904 uint32_t bind_off() const INLINE { return E::get32(fields.bind_off); }
905 void set_bind_off(uint32_t value) INLINE { E::set32(fields.bind_off, value); }
907 uint32_t bind_size() const INLINE { return E::get32(fields.bind_size); }
908 void set_bind_size(uint32_t value) INLINE { E::set32(fields.bind_size, value); }
910 uint32_t weak_bind_off() const INLINE { return E::get32(fields.weak_bind_off); }
911 void set_weak_bind_off(uint32_t value) INLINE { E::set32(fields.weak_bind_off, value); }
913 uint32_t weak_bind_size() const INLINE { return E::get32(fields.weak_bind_size); }
914 void set_weak_bind_size(uint32_t value) INLINE { E::set32(fields.weak_bind_size, value); }
916 uint32_t lazy_bind_off() const INLINE { return E::get32(fields.lazy_bind_off); }
917 void set_lazy_bind_off(uint32_t value) INLINE { E::set32(fields.lazy_bind_off, value); }
919 uint32_t lazy_bind_size() const INLINE { return E::get32(fields.lazy_bind_size); }
920 void set_lazy_bind_size(uint32_t value) INLINE { E::set32(fields.lazy_bind_size, value); }
922 uint32_t export_off() const INLINE { return E::get32(fields.export_off); }
923 void set_export_off(uint32_t value) INLINE { E::set32(fields.export_off, value); }
925 uint32_t export_size() const INLINE { return E::get32(fields.export_size); }
926 void set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value); }
929 typedef typename P::E E;
931 dyld_info_command fields;
935 inline uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end) {
940 throw "malformed uleb128 extends beyond trie";
942 uint64_t slice = *p & 0x7f;
944 if (bit >= 64 || slice << bit >> bit != slice)
945 throw "uleb128 too big for 64-bits";
947 result |= (slice << bit);
956 inline int64_t read_sleb128(const uint8_t*& p, const uint8_t* end)
963 throw "malformed sleb128";
965 result |= (((int64_t)(byte & 0x7f)) << bit);
967 } while (byte & 0x80);
968 // sign extend negative numbers
969 if ( (byte & 0x40) != 0 )
970 result |= (~0ULL) << bit;
977 #endif // __MACH_O_FILE_ABSTRACTION__