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 ARM64_RELOC_UNSIGNED 0 // for pointers
41 #define DYLD_CACHE_ADJ_V2_FORMAT 0x7F
43 #define DYLD_CACHE_ADJ_V2_POINTER_32 0x01
44 #define DYLD_CACHE_ADJ_V2_POINTER_64 0x02
45 #define DYLD_CACHE_ADJ_V2_DELTA_32 0x03
46 #define DYLD_CACHE_ADJ_V2_DELTA_64 0x04
47 #define DYLD_CACHE_ADJ_V2_ARM64_ADRP 0x05
48 #define DYLD_CACHE_ADJ_V2_ARM64_OFF12 0x06
49 #define DYLD_CACHE_ADJ_V2_ARM64_BR26 0x07
50 #define DYLD_CACHE_ADJ_V2_ARM_MOVW_MOVT 0x08
51 #define DYLD_CACHE_ADJ_V2_ARM_BR24 0x09
52 #define DYLD_CACHE_ADJ_V2_THUMB_MOVW_MOVT 0x0A
53 #define DYLD_CACHE_ADJ_V2_THUMB_BR22 0x0B
54 #define DYLD_CACHE_ADJ_V2_IMAGE_OFF_32 0x0C
57 #include "FileAbstraction.hpp"
58 #include "Architectures.hpp"
60 // utility to pair together a cpu-type and cpu-sub-type
66 ArchPair(uint32_t cputype, uint32_t cpusubtype) : arch(cputype), subtype(cpusubtype) {}
68 bool operator<(const ArchPair& other) const {
69 if ( this->arch != other.arch )
70 return (this->arch < other.arch);
71 return (this->subtype < other.subtype);
74 bool operator==(const ArchPair& other) const {
75 return this->arch == other.arch && this->subtype == other.subtype;
81 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
85 // mach-o load command
88 class macho_load_command {
90 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
91 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
93 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
94 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
96 typedef typename P::E E;
103 // mach-o segment load command
105 template <typename P> struct macho_segment_content {};
106 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
107 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
108 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
109 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
111 template <typename P>
112 class macho_segment_command {
114 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
115 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
117 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
118 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
120 const char* segname() const INLINE { return segment.fields.segname; }
121 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
123 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
124 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
126 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
127 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
129 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
130 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
132 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
133 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
135 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
136 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
138 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
139 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
141 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
142 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
144 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
145 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
148 CMD = macho_segment_content<P>::CMD
151 typedef typename P::E E;
153 macho_segment_content<P> segment;
160 template <typename P> struct macho_section_content {};
161 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
162 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
163 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
164 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
166 template <typename P>
167 class macho_section {
169 const char* sectname() const INLINE { return section.fields.sectname; }
170 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
172 const char* segname() const INLINE { return section.fields.segname; }
173 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
175 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
176 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
178 uint64_t size() const INLINE { return P::getP(section.fields.size); }
179 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
181 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
182 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
184 uint32_t align() const INLINE { return E::get32(section.fields.align); }
185 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
187 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
188 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
190 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
191 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
193 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
194 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
196 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
197 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
199 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
200 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
202 typedef typename P::E E;
204 macho_section_content<P> section;
209 // mach-o dylib load command
211 template <typename P>
212 class macho_dylib_command {
214 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
215 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
217 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
218 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
220 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
221 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
223 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
224 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
226 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
227 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
229 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
230 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
232 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
233 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
235 typedef typename P::E E;
237 dylib_command fields;
242 // mach-o dylinker load command
244 template <typename P>
245 class macho_dylinker_command {
247 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
248 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
250 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
251 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
253 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
254 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
256 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
257 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
259 typedef typename P::E E;
261 dylinker_command fields;
266 // mach-o sub_framework load command
268 template <typename P>
269 class macho_sub_framework_command {
271 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
272 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
274 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
275 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
277 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
278 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
280 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
281 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
283 typedef typename P::E E;
285 sub_framework_command fields;
290 // mach-o sub_client load command
292 template <typename P>
293 class macho_sub_client_command {
295 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
296 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
298 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
299 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
301 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
302 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
304 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
305 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
307 typedef typename P::E E;
309 sub_client_command fields;
314 // mach-o sub_umbrella load command
316 template <typename P>
317 class macho_sub_umbrella_command {
319 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
320 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
322 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
323 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
325 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
326 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
328 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
329 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
331 typedef typename P::E E;
333 sub_umbrella_command fields;
338 // mach-o sub_library load command
340 template <typename P>
341 class macho_sub_library_command {
343 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
344 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
346 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
347 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
349 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
350 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
352 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
353 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
355 typedef typename P::E E;
357 sub_library_command fields;
362 // mach-o uuid load command
364 template <typename P>
365 class macho_uuid_command {
367 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
368 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
370 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
371 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
373 const uint8_t* uuid() const INLINE { return fields.uuid; }
374 void set_uuid(uint8_t value[16]) INLINE { memcpy(&fields.uuid, value, 16); }
376 typedef typename P::E E;
383 // mach-o routines load command
385 template <typename P> struct macho_routines_content {};
386 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
387 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
388 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
389 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
391 template <typename P>
392 class macho_routines_command {
394 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
395 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
397 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
398 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
400 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
401 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
403 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
404 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
406 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
407 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
409 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
410 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
412 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
413 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
415 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
416 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
418 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
419 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
421 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
422 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
424 typedef typename P::E E;
426 CMD = macho_routines_content<P>::CMD
429 macho_routines_content<P> routines;
434 // mach-o symbol table load command
436 template <typename P>
437 class macho_symtab_command {
439 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
440 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
442 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
443 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
445 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
446 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
448 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
449 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
451 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
452 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
454 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
455 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
458 typedef typename P::E E;
460 symtab_command fields;
465 // mach-o dynamic symbol table load command
467 template <typename P>
468 class macho_dysymtab_command {
470 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
471 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
473 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
474 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
476 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
477 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
479 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
480 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
482 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
483 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
485 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
486 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
488 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
489 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
491 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
492 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
494 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
495 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
497 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
498 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
500 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
501 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
503 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
504 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
506 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
507 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
509 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
510 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
512 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
513 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
515 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
516 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
518 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
519 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
521 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
522 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
524 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
525 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
527 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
528 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
530 typedef typename P::E E;
532 dysymtab_command fields;
537 // mach-o two-level hints load command
539 template <typename P>
540 class macho_twolevel_hints_command {
542 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
543 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
545 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
546 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
548 uint32_t offset() const INLINE { return E::get32(fields.offset); }
549 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
551 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
552 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
554 typedef typename P::E E;
556 twolevel_hints_command fields;
561 // mach-o threads load command
563 template <typename P>
564 class macho_thread_command {
566 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
567 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
569 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
570 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
572 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
573 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
575 uint32_t count() const INLINE { return E::get32(fields_count); }
576 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
578 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
579 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
581 typedef typename P::E E;
582 typedef typename P::uint_t pint_t;
584 struct thread_command fields;
585 uint32_t fields_flavor;
586 uint32_t fields_count;
587 pint_t thread_registers[1];
594 template <typename P>
595 class macho_linkedit_data_command {
597 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
598 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
600 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
601 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
603 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
604 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
606 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
607 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
610 typedef typename P::E E;
612 linkedit_data_command fields;
617 // mach-o symbol table entry
619 template <typename P> struct macho_nlist_content {};
620 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
621 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
622 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
623 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
625 template <typename P>
628 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
629 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
631 uint8_t n_type() const INLINE { return entry.fields.n_type; }
632 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
634 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
635 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
637 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
638 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
640 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
641 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
643 typedef typename P::E E;
645 macho_nlist_content<P> entry;
651 // mach-o relocation info
653 template <typename P>
654 class macho_relocation_info {
656 uint32_t r_address() const INLINE { return E::get32(address); }
657 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
659 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
660 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
662 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
663 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
665 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
666 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
668 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
669 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
671 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
672 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
674 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
676 typedef typename P::E E;
684 // mach-o scattered relocation info
685 // The bit fields are always in big-endian order (see mach-o/reloc.h)
687 template <typename P>
688 class macho_scattered_relocation_info {
690 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
691 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
693 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
694 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
696 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
697 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
699 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
700 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
702 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
703 void set_r_address(uint32_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
705 uint32_t r_value() const INLINE { return E::get32(value); }
706 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
708 uint32_t r_other() const INLINE { return other; }
710 typedef typename P::E E;
718 // mach-o file header
720 template <typename P> struct macho_header_content {};
721 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
722 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
723 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
724 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
726 template <typename P>
729 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
730 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
732 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
733 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
735 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
736 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
738 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
739 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
741 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
742 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
744 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
745 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
747 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
748 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
750 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
751 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
753 const macho_segment_command<P>* getSegment(const char *segname) const
755 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
756 uint32_t cmd_count = this->ncmds();
757 const macho_load_command<P>* cmd = cmds;
758 for (uint32_t i = 0; i < cmd_count; ++i) {
759 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
760 const macho_segment_command<P>* segcmd = (macho_segment_command<P>*)cmd;
761 if (0 == strncmp(segname, segcmd->segname(), 16)) {
765 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
770 const macho_section<P>* getSection(const char *segname, const char *sectname) const
772 const macho_segment_command<P>* segcmd = getSegment(segname);
773 if (!segcmd) return NULL;
775 const macho_section<P>* sectcmd = (macho_section<P>*)(segcmd+1);
776 uint32_t section_count = segcmd->nsects();
777 for (uint32_t j = 0; j < section_count; ++j) {
778 if (0 == ::strncmp(sectcmd[j].sectname(), sectname, 16)) {
783 if (strcmp(segname, "__DATA") == 0) {
784 if (const macho_section<P>* dataConst = getSection("__DATA_CONST", sectname))
786 if (const macho_section<P>* dataDirty = getSection("__DATA_DIRTY", sectname))
792 const macho_load_command<P>* getLoadCommand(int query) const
794 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
795 uint32_t cmd_count = this->ncmds();
796 const macho_load_command<P>* cmd = cmds;
797 for (uint32_t i = 0; i < cmd_count; ++i) {
798 if ( cmd->cmd() == query ) {
801 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
806 typedef typename P::E E;
808 macho_header_content<P> header;
814 // compressed dyld info load command
816 template <typename P>
817 class macho_dyld_info_command {
819 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
820 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
822 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
823 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
825 uint32_t rebase_off() const INLINE { return E::get32(fields.rebase_off); }
826 void set_rebase_off(uint32_t value) INLINE { E::set32(fields.rebase_off, value); }
828 uint32_t rebase_size() const INLINE { return E::get32(fields.rebase_size); }
829 void set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value); }
831 uint32_t bind_off() const INLINE { return E::get32(fields.bind_off); }
832 void set_bind_off(uint32_t value) INLINE { E::set32(fields.bind_off, value); }
834 uint32_t bind_size() const INLINE { return E::get32(fields.bind_size); }
835 void set_bind_size(uint32_t value) INLINE { E::set32(fields.bind_size, value); }
837 uint32_t weak_bind_off() const INLINE { return E::get32(fields.weak_bind_off); }
838 void set_weak_bind_off(uint32_t value) INLINE { E::set32(fields.weak_bind_off, value); }
840 uint32_t weak_bind_size() const INLINE { return E::get32(fields.weak_bind_size); }
841 void set_weak_bind_size(uint32_t value) INLINE { E::set32(fields.weak_bind_size, value); }
843 uint32_t lazy_bind_off() const INLINE { return E::get32(fields.lazy_bind_off); }
844 void set_lazy_bind_off(uint32_t value) INLINE { E::set32(fields.lazy_bind_off, value); }
846 uint32_t lazy_bind_size() const INLINE { return E::get32(fields.lazy_bind_size); }
847 void set_lazy_bind_size(uint32_t value) INLINE { E::set32(fields.lazy_bind_size, value); }
849 uint32_t export_off() const INLINE { return E::get32(fields.export_off); }
850 void set_export_off(uint32_t value) INLINE { E::set32(fields.export_off, value); }
852 uint32_t export_size() const INLINE { return E::get32(fields.export_size); }
853 void set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value); }
856 typedef typename P::E E;
858 dyld_info_command fields;
862 inline uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end) {
867 throw "malformed uleb128 extends beyond trie";
869 uint64_t slice = *p & 0x7f;
871 if (bit >= 64 || slice << bit >> bit != slice)
872 throw "uleb128 too big for 64-bits";
874 result |= (slice << bit);
883 inline int64_t read_sleb128(const uint8_t*& p, const uint8_t* end)
890 throw "malformed sleb128";
892 result |= (((int64_t)(byte & 0x7f)) << bit);
894 } while (byte & 0x80);
895 // sign extend negative numbers
896 if ( (byte & 0x40) != 0 )
897 result |= (~0ULL) << bit;
904 #endif // __MACH_O_FILE_ABSTRACTION__