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))
71 #define ARM64_RELOC_UNSIGNED 0 // for pointers
74 #ifndef LC_LOAD_UPWARD_DYLIB
75 #define LC_LOAD_UPWARD_DYLIB (0x23|LC_REQ_DYLD) /* load of dylib whose initializers run later */
78 #ifndef EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
79 #define EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER 0x10
81 #ifndef EXPORT_SYMBOL_FLAGS_REEXPORT
82 #define EXPORT_SYMBOL_FLAGS_REEXPORT 0x08
85 #ifndef LC_FUNCTION_STARTS
86 #define LC_FUNCTION_STARTS 0x26
89 #ifndef LC_DATA_IN_CODE
90 #define LC_DATA_IN_CODE 0x29
93 #ifndef LC_DYLIB_CODE_SIGN_DRS
94 #define LC_DYLIB_CODE_SIGN_DRS 0x2B
97 #ifndef CPU_SUBTYPE_X86_64_H
98 #define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t) 8)
102 #define DYLD_CACHE_ADJ_V2_FORMAT 0x7F
104 #define DYLD_CACHE_ADJ_V2_POINTER_32 0x01
105 #define DYLD_CACHE_ADJ_V2_POINTER_64 0x02
106 #define DYLD_CACHE_ADJ_V2_DELTA_32 0x03
107 #define DYLD_CACHE_ADJ_V2_DELTA_64 0x04
108 #define DYLD_CACHE_ADJ_V2_ARM64_ADRP 0x05
109 #define DYLD_CACHE_ADJ_V2_ARM64_OFF12 0x06
110 #define DYLD_CACHE_ADJ_V2_ARM64_BR26 0x07
111 #define DYLD_CACHE_ADJ_V2_ARM_MOVW_MOVT 0x08
112 #define DYLD_CACHE_ADJ_V2_ARM_BR24 0x09
113 #define DYLD_CACHE_ADJ_V2_THUMB_MOVW_MOVT 0x0A
114 #define DYLD_CACHE_ADJ_V2_THUMB_BR22 0x0B
115 #define DYLD_CACHE_ADJ_V2_IMAGE_OFF_32 0x0C
117 #define MH_HAS_OBJC 0x40000000
119 #include "FileAbstraction.hpp"
120 //#include "Architectures.hpp"
122 // utility to pair together a cpu-type and cpu-sub-type
128 ArchPair(uint32_t cputype, uint32_t cpusubtype) : arch(cputype), subtype(cpusubtype) {}
130 bool operator<(const ArchPair& other) const {
131 if ( this->arch != other.arch )
132 return (this->arch < other.arch);
133 return (this->subtype < other.subtype);
136 bool operator==(const ArchPair& other) const {
137 return this->arch == other.arch && this->subtype == other.subtype;
143 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
147 // mach-o load command
149 template <typename P>
150 class macho_load_command {
152 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
153 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
155 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
156 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
158 typedef typename P::E E;
160 load_command command;
165 // mach-o segment load command
167 template <typename P> struct macho_segment_content {};
168 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
169 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
170 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
171 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
173 template <typename P>
174 class macho_segment_command {
176 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
177 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
179 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
180 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
182 const char* segname() const INLINE { return segment.fields.segname; }
183 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
185 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
186 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
188 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
189 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
191 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
192 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
194 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
195 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
197 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
198 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
200 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
201 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
203 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
204 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
206 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
207 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
210 CMD = macho_segment_content<P>::CMD
213 typedef typename P::E E;
215 macho_segment_content<P> segment;
222 template <typename P> struct macho_section_content {};
223 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
224 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
225 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
226 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
228 template <typename P>
229 class macho_section {
231 const char* sectname() const INLINE { return section.fields.sectname; }
232 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
234 const char* segname() const INLINE { return section.fields.segname; }
235 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
237 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
238 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
240 uint64_t size() const INLINE { return P::getP(section.fields.size); }
241 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
243 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
244 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
246 uint32_t align() const INLINE { return E::get32(section.fields.align); }
247 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
249 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
250 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
252 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
253 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
255 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
256 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
258 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
259 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
261 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
262 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
264 typedef typename P::E E;
266 macho_section_content<P> section;
271 // mach-o dylib load command
273 template <typename P>
274 class macho_dylib_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.dylib.name.offset); }
283 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
285 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
286 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
288 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
289 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
291 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
292 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
294 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
295 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
297 typedef typename P::E E;
299 dylib_command fields;
304 // mach-o dylinker load command
306 template <typename P>
307 class macho_dylinker_command {
309 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
310 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
312 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
313 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
315 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
316 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
318 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
319 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
321 typedef typename P::E E;
323 dylinker_command fields;
328 // mach-o sub_framework load command
330 template <typename P>
331 class macho_sub_framework_command {
333 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
334 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
336 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
337 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
339 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
340 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
342 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
343 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
345 typedef typename P::E E;
347 sub_framework_command fields;
352 // mach-o sub_client load command
354 template <typename P>
355 class macho_sub_client_command {
357 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
358 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
360 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
361 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
363 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
364 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
366 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
367 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
369 typedef typename P::E E;
371 sub_client_command fields;
376 // mach-o sub_umbrella load command
378 template <typename P>
379 class macho_sub_umbrella_command {
381 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
382 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
384 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
385 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
387 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
388 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
390 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
391 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
393 typedef typename P::E E;
395 sub_umbrella_command fields;
400 // mach-o sub_library load command
402 template <typename P>
403 class macho_sub_library_command {
405 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
406 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
408 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
409 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
411 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
412 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
414 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
415 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
417 typedef typename P::E E;
419 sub_library_command fields;
424 // mach-o uuid load command
426 template <typename P>
427 class macho_uuid_command {
429 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
430 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
432 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
433 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
435 const uint8_t* uuid() const INLINE { return fields.uuid; }
436 void set_uuid(uint8_t value[16]) INLINE { memcpy(&fields.uuid, value, 16); }
438 typedef typename P::E E;
445 // mach-o routines load command
447 template <typename P> struct macho_routines_content {};
448 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
449 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
450 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
451 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
453 template <typename P>
454 class macho_routines_command {
456 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
457 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
459 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
460 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
462 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
463 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
465 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
466 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
468 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
469 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
471 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
472 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
474 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
475 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
477 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
478 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
480 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
481 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
483 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
484 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
486 typedef typename P::E E;
488 CMD = macho_routines_content<P>::CMD
491 macho_routines_content<P> routines;
496 // mach-o symbol table load command
498 template <typename P>
499 class macho_symtab_command {
501 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
502 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
504 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
505 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
507 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
508 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
510 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
511 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
513 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
514 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
516 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
517 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
520 typedef typename P::E E;
522 symtab_command fields;
527 // mach-o dynamic symbol table load command
529 template <typename P>
530 class macho_dysymtab_command {
532 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
533 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
535 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
536 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
538 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
539 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
541 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
542 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
544 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
545 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
547 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
548 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
550 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
551 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
553 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
554 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
556 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
557 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
559 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
560 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
562 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
563 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
565 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
566 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
568 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
569 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
571 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
572 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
574 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
575 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
577 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
578 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
580 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
581 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
583 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
584 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
586 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
587 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
589 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
590 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
592 typedef typename P::E E;
594 dysymtab_command fields;
599 // mach-o two-level hints load command
601 template <typename P>
602 class macho_twolevel_hints_command {
604 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
605 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
607 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
608 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
610 uint32_t offset() const INLINE { return E::get32(fields.offset); }
611 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
613 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
614 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
616 typedef typename P::E E;
618 twolevel_hints_command fields;
623 // mach-o threads load command
625 template <typename P>
626 class macho_thread_command {
628 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
629 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
631 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
632 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
634 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
635 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
637 uint32_t count() const INLINE { return E::get32(fields_count); }
638 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
640 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
641 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
643 typedef typename P::E E;
644 typedef typename P::uint_t pint_t;
646 struct thread_command fields;
647 uint32_t fields_flavor;
648 uint32_t fields_count;
649 pint_t thread_registers[1];
656 template <typename P>
657 class macho_linkedit_data_command {
659 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
660 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
662 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
663 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
665 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
666 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
668 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
669 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
672 typedef typename P::E E;
674 linkedit_data_command fields;
679 // mach-o symbol table entry
681 template <typename P> struct macho_nlist_content {};
682 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
683 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
684 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
685 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
687 template <typename P>
690 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
691 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
693 uint8_t n_type() const INLINE { return entry.fields.n_type; }
694 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
696 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
697 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
699 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
700 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
702 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
703 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
705 typedef typename P::E E;
707 macho_nlist_content<P> entry;
713 // mach-o relocation info
715 template <typename P>
716 class macho_relocation_info {
718 uint32_t r_address() const INLINE { return E::get32(address); }
719 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
721 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
722 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
724 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
725 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
727 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
728 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
730 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
731 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
733 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
734 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
736 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
738 typedef typename P::E E;
746 // mach-o scattered relocation info
747 // The bit fields are always in big-endian order (see mach-o/reloc.h)
749 template <typename P>
750 class macho_scattered_relocation_info {
752 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
753 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
755 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
756 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
758 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
759 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
761 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
762 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
764 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
765 void set_r_address(uint32_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
767 uint32_t r_value() const INLINE { return E::get32(value); }
768 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
770 uint32_t r_other() const INLINE { return other; }
772 typedef typename P::E E;
780 // mach-o file header
782 template <typename P> struct macho_header_content {};
783 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
784 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
785 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
786 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
788 template <typename P>
791 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
792 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
794 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
795 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
797 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
798 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
800 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
801 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
803 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
804 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
806 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
807 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
809 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
810 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
812 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
813 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
815 const macho_segment_command<P>* getSegment(const char *segname) 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() == macho_segment_command<P>::CMD ) {
822 const macho_segment_command<P>* segcmd = (macho_segment_command<P>*)cmd;
823 if (0 == strncmp(segname, segcmd->segname(), 16)) {
827 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
832 const macho_section<P>* getSection(const char *segname, const char *sectname) const
834 const macho_segment_command<P>* segcmd = getSegment(segname);
835 if (!segcmd) return NULL;
837 const macho_section<P>* sectcmd = (macho_section<P>*)(segcmd+1);
838 uint32_t section_count = segcmd->nsects();
839 for (uint32_t j = 0; j < section_count; ++j) {
840 if (0 == ::strncmp(sectcmd[j].sectname(), sectname, 16)) {
845 if (strcmp(segname, "__DATA") == 0)
846 return getSection("__DATA_CONST", sectname);
850 const macho_load_command<P>* getLoadCommand(int query) const
852 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
853 uint32_t cmd_count = this->ncmds();
854 const macho_load_command<P>* cmd = cmds;
855 for (uint32_t i = 0; i < cmd_count; ++i) {
856 if ( cmd->cmd() == query ) {
859 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
864 typedef typename P::E E;
866 macho_header_content<P> header;
872 // compressed dyld info load command
874 template <typename P>
875 class macho_dyld_info_command {
877 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
878 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
880 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
881 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
883 uint32_t rebase_off() const INLINE { return E::get32(fields.rebase_off); }
884 void set_rebase_off(uint32_t value) INLINE { E::set32(fields.rebase_off, value); }
886 uint32_t rebase_size() const INLINE { return E::get32(fields.rebase_size); }
887 void set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value); }
889 uint32_t bind_off() const INLINE { return E::get32(fields.bind_off); }
890 void set_bind_off(uint32_t value) INLINE { E::set32(fields.bind_off, value); }
892 uint32_t bind_size() const INLINE { return E::get32(fields.bind_size); }
893 void set_bind_size(uint32_t value) INLINE { E::set32(fields.bind_size, value); }
895 uint32_t weak_bind_off() const INLINE { return E::get32(fields.weak_bind_off); }
896 void set_weak_bind_off(uint32_t value) INLINE { E::set32(fields.weak_bind_off, value); }
898 uint32_t weak_bind_size() const INLINE { return E::get32(fields.weak_bind_size); }
899 void set_weak_bind_size(uint32_t value) INLINE { E::set32(fields.weak_bind_size, value); }
901 uint32_t lazy_bind_off() const INLINE { return E::get32(fields.lazy_bind_off); }
902 void set_lazy_bind_off(uint32_t value) INLINE { E::set32(fields.lazy_bind_off, value); }
904 uint32_t lazy_bind_size() const INLINE { return E::get32(fields.lazy_bind_size); }
905 void set_lazy_bind_size(uint32_t value) INLINE { E::set32(fields.lazy_bind_size, value); }
907 uint32_t export_off() const INLINE { return E::get32(fields.export_off); }
908 void set_export_off(uint32_t value) INLINE { E::set32(fields.export_off, value); }
910 uint32_t export_size() const INLINE { return E::get32(fields.export_size); }
911 void set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value); }
914 typedef typename P::E E;
916 dyld_info_command fields;
920 inline uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end) {
925 throw "malformed uleb128 extends beyond trie";
927 uint64_t slice = *p & 0x7f;
929 if (bit >= 64 || slice << bit >> bit != slice)
930 throw "uleb128 too big for 64-bits";
932 result |= (slice << bit);
941 inline int64_t read_sleb128(const uint8_t*& p, const uint8_t* end)
948 throw "malformed sleb128";
950 result |= (((int64_t)(byte & 0x7f)) << bit);
952 } while (byte & 0x80);
953 // sign extend negative numbers
954 if ( (byte & 0x40) != 0 )
955 result |= (-1LL) << bit;
962 #endif // __MACH_O_FILE_ABSTRACTION__