Merge pull request #8 from biergaizi/upstream
[darwin-xtools.git] / dyld / launch-cache / MachOFileAbstraction.hpp
blob7a65370a64b6e92c7e5731a2fc174f3312db650b
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@
6 *
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
12 * file.
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
33 #ifndef LC_UUID
34 #define LC_UUID 0x1b
35 struct uuid_command {
36 uint32_t cmd; /* LC_UUID */
37 uint32_t cmdsize; /* sizeof(struct uuid_command) */
38 uint8_t uuid[16]; /* the 128-bit uuid */
40 #endif
42 #ifndef S_16BYTE_LITERALS
43 #define S_16BYTE_LITERALS 0xE
44 #endif
46 #ifndef CPU_SUBTYPE_ARM_V5TEJ
47 #define CPU_SUBTYPE_ARM_V5TEJ ((cpu_subtype_t) 7)
48 #endif
49 #ifndef CPU_SUBTYPE_ARM_XSCALE
50 #define CPU_SUBTYPE_ARM_XSCALE ((cpu_subtype_t) 8)
51 #endif
52 #ifndef CPU_SUBTYPE_ARM_V7
53 #define CPU_SUBTYPE_ARM_V7 ((cpu_subtype_t) 9)
54 #endif
55 #ifndef CPU_SUBTYPE_ARM_V7F
56 #define CPU_SUBTYPE_ARM_V7F ((cpu_subtype_t) 10)
57 #endif
58 #ifndef CPU_SUBTYPE_ARM_V7K
59 #define CPU_SUBTYPE_ARM_V7K ((cpu_subtype_t) 12)
60 #endif
61 #ifndef CPU_SUBTYPE_ARM_V7S
62 #define CPU_SUBTYPE_ARM_V7S ((cpu_subtype_t) 11)
63 #endif
64 #ifndef CPU_SUBTYPE_ARM64_ALL
65 #define CPU_SUBTYPE_ARM64_ALL ((cpu_subtype_t) 0)
66 #endif
67 #ifndef CPU_TYPE_ARM64
68 #define CPU_TYPE_ARM64 ((cpu_type_t) (CPU_TYPE_ARM | CPU_ARCH_ABI64))
69 #endif
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 */
76 #endif
78 #ifndef EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
79 #define EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER 0x10
80 #endif
81 #ifndef EXPORT_SYMBOL_FLAGS_REEXPORT
82 #define EXPORT_SYMBOL_FLAGS_REEXPORT 0x08
83 #endif
85 #ifndef LC_FUNCTION_STARTS
86 #define LC_FUNCTION_STARTS 0x26
87 #endif
89 #ifndef LC_DATA_IN_CODE
90 #define LC_DATA_IN_CODE 0x29
91 #endif
93 #ifndef LC_DYLIB_CODE_SIGN_DRS
94 #define LC_DYLIB_CODE_SIGN_DRS 0x2B
95 #endif
97 #ifndef CPU_SUBTYPE_X86_64_H
98 #define CPU_SUBTYPE_X86_64_H ((cpu_subtype_t) 8)
99 #endif
102 #include "FileAbstraction.hpp"
103 #include "Architectures.hpp"
105 // utility to pair together a cpu-type and cpu-sub-type
106 struct ArchPair
108 uint32_t arch;
109 uint32_t subtype;
111 ArchPair(uint32_t cputype, uint32_t cpusubtype) : arch(cputype), subtype(cpusubtype) {}
113 bool operator<(const ArchPair& other) const {
114 if ( this->arch != other.arch )
115 return (this->arch < other.arch);
116 return (this->subtype < other.subtype);
122 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
126 // mach-o load command
128 template <typename P>
129 class macho_load_command {
130 public:
131 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
132 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
134 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
135 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
137 typedef typename P::E E;
138 private:
139 load_command command;
144 // mach-o segment load command
146 template <typename P> struct macho_segment_content {};
147 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
148 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
149 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
150 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
152 template <typename P>
153 class macho_segment_command {
154 public:
155 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
156 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
158 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
159 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
161 const char* segname() const INLINE { return segment.fields.segname; }
162 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
164 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
165 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
167 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
168 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
170 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
171 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
173 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
174 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
176 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
177 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
179 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
180 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
182 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
183 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
185 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
186 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
188 enum {
189 CMD = macho_segment_content<P>::CMD
192 typedef typename P::E E;
193 private:
194 macho_segment_content<P> segment;
199 // mach-o section
201 template <typename P> struct macho_section_content {};
202 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
203 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
204 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
205 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
207 template <typename P>
208 class macho_section {
209 public:
210 const char* sectname() const INLINE { return section.fields.sectname; }
211 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
213 const char* segname() const INLINE { return section.fields.segname; }
214 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
216 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
217 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
219 uint64_t size() const INLINE { return P::getP(section.fields.size); }
220 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
222 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
223 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
225 uint32_t align() const INLINE { return E::get32(section.fields.align); }
226 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
228 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
229 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
231 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
232 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
234 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
235 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
237 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
238 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
240 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
241 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
243 typedef typename P::E E;
244 private:
245 macho_section_content<P> section;
250 // mach-o dylib load command
252 template <typename P>
253 class macho_dylib_command {
254 public:
255 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
256 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
258 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
259 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
261 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
262 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
264 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
265 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
267 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
268 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
270 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
271 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
273 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
274 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
276 typedef typename P::E E;
277 private:
278 dylib_command fields;
283 // mach-o dylinker load command
285 template <typename P>
286 class macho_dylinker_command {
287 public:
288 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
289 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
291 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
292 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
294 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
295 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
297 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
298 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
300 typedef typename P::E E;
301 private:
302 dylinker_command fields;
307 // mach-o sub_framework load command
309 template <typename P>
310 class macho_sub_framework_command {
311 public:
312 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
313 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
315 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
316 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
318 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
319 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
321 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
322 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
324 typedef typename P::E E;
325 private:
326 sub_framework_command fields;
331 // mach-o sub_client load command
333 template <typename P>
334 class macho_sub_client_command {
335 public:
336 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
337 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
339 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
340 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
342 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
343 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
345 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
346 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
348 typedef typename P::E E;
349 private:
350 sub_client_command fields;
355 // mach-o sub_umbrella load command
357 template <typename P>
358 class macho_sub_umbrella_command {
359 public:
360 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
361 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
363 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
364 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
366 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
367 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
369 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
370 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
372 typedef typename P::E E;
373 private:
374 sub_umbrella_command fields;
379 // mach-o sub_library load command
381 template <typename P>
382 class macho_sub_library_command {
383 public:
384 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
385 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
387 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
388 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
390 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
391 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
393 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
394 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
396 typedef typename P::E E;
397 private:
398 sub_library_command fields;
403 // mach-o uuid load command
405 template <typename P>
406 class macho_uuid_command {
407 public:
408 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
409 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
411 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
412 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
414 const uint8_t* uuid() const INLINE { return fields.uuid; }
415 void set_uuid(uint8_t value[16]) INLINE { memcpy(&fields.uuid, value, 16); }
417 typedef typename P::E E;
418 private:
419 uuid_command fields;
424 // mach-o routines load command
426 template <typename P> struct macho_routines_content {};
427 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
428 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
429 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
430 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
432 template <typename P>
433 class macho_routines_command {
434 public:
435 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
436 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
438 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
439 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
441 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
442 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
444 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
445 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
447 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
448 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
450 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
451 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
453 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
454 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
456 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
457 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
459 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
460 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
462 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
463 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
465 typedef typename P::E E;
466 enum {
467 CMD = macho_routines_content<P>::CMD
469 private:
470 macho_routines_content<P> routines;
475 // mach-o symbol table load command
477 template <typename P>
478 class macho_symtab_command {
479 public:
480 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
481 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
483 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
484 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
486 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
487 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
489 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
490 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
492 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
493 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
495 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
496 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
499 typedef typename P::E E;
500 private:
501 symtab_command fields;
506 // mach-o dynamic symbol table load command
508 template <typename P>
509 class macho_dysymtab_command {
510 public:
511 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
512 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
514 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
515 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
517 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
518 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
520 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
521 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
523 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
524 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
526 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
527 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
529 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
530 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
532 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
533 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
535 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
536 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
538 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
539 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
541 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
542 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
544 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
545 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
547 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
548 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
550 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
551 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
553 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
554 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
556 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
557 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
559 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
560 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
562 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
563 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
565 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
566 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
568 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
569 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
571 typedef typename P::E E;
572 private:
573 dysymtab_command fields;
578 // mach-o two-level hints load command
580 template <typename P>
581 class macho_twolevel_hints_command {
582 public:
583 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
584 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
586 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
587 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
589 uint32_t offset() const INLINE { return E::get32(fields.offset); }
590 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
592 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
593 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
595 typedef typename P::E E;
596 private:
597 twolevel_hints_command fields;
602 // mach-o threads load command
604 template <typename P>
605 class macho_thread_command {
606 public:
607 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
608 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
610 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
611 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
613 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
614 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
616 uint32_t count() const INLINE { return E::get32(fields_count); }
617 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
619 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
620 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
622 typedef typename P::E E;
623 typedef typename P::uint_t pint_t;
624 private:
625 struct thread_command fields;
626 uint32_t fields_flavor;
627 uint32_t fields_count;
628 pint_t thread_registers[1];
633 // mach-o misc data
635 template <typename P>
636 class macho_linkedit_data_command {
637 public:
638 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
639 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
641 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
642 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
644 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
645 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
647 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
648 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
651 typedef typename P::E E;
652 private:
653 linkedit_data_command fields;
658 // mach-o symbol table entry
660 template <typename P> struct macho_nlist_content {};
661 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
662 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
663 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
664 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
666 template <typename P>
667 class macho_nlist {
668 public:
669 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
670 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
672 uint8_t n_type() const INLINE { return entry.fields.n_type; }
673 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
675 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
676 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
678 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
679 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
681 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
682 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
684 typedef typename P::E E;
685 private:
686 macho_nlist_content<P> entry;
692 // mach-o relocation info
694 template <typename P>
695 class macho_relocation_info {
696 public:
697 uint32_t r_address() const INLINE { return E::get32(address); }
698 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
700 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
701 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
703 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
704 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
706 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
707 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
709 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
710 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
712 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
713 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
715 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
717 typedef typename P::E E;
718 private:
719 uint32_t address;
720 uint32_t other;
725 // mach-o scattered relocation info
726 // The bit fields are always in big-endian order (see mach-o/reloc.h)
728 template <typename P>
729 class macho_scattered_relocation_info {
730 public:
731 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
732 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
734 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
735 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
737 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
738 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
740 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
741 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
743 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
744 void set_r_address(uint32_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
746 uint32_t r_value() const INLINE { return E::get32(value); }
747 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
749 uint32_t r_other() const INLINE { return other; }
751 typedef typename P::E E;
752 private:
753 uint32_t other;
754 uint32_t value;
759 // mach-o file header
761 template <typename P> struct macho_header_content {};
762 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
763 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
764 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
765 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
767 template <typename P>
768 class macho_header {
769 public:
770 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
771 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
773 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
774 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
776 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
777 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
779 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
780 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
782 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
783 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
785 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
786 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
788 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
789 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
791 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
792 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
794 const macho_segment_command<P>* getSegment(const char *segname) const
796 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
797 uint32_t cmd_count = this->ncmds();
798 const macho_load_command<P>* cmd = cmds;
799 for (uint32_t i = 0; i < cmd_count; ++i) {
800 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
801 const macho_segment_command<P>* segcmd = (macho_segment_command<P>*)cmd;
802 if (0 == strncmp(segname, segcmd->segname(), 16)) {
803 return segcmd;
806 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
808 return NULL;
811 const macho_section<P>* getSection(const char *segname, const char *sectname) const
813 const macho_segment_command<P>* segcmd = getSegment(segname);
814 if (!segcmd) return NULL;
816 const macho_section<P>* sectcmd = (macho_section<P>*)(segcmd+1);
817 uint32_t section_count = segcmd->nsects();
818 for (uint32_t j = 0; j < section_count; ++j) {
819 if (0 == ::strncmp(sectcmd[j].sectname(), sectname, 16)) {
820 return sectcmd+j;
824 return NULL;
827 const macho_load_command<P>* getLoadCommand(int query) const
829 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
830 uint32_t cmd_count = this->ncmds();
831 const macho_load_command<P>* cmd = cmds;
832 for (uint32_t i = 0; i < cmd_count; ++i) {
833 if ( cmd->cmd() == query ) {
834 return cmd;
836 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
838 return NULL;
841 typedef typename P::E E;
842 private:
843 macho_header_content<P> header;
849 // compressed dyld info load command
851 template <typename P>
852 class macho_dyld_info_command {
853 public:
854 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
855 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
857 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
858 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
860 uint32_t rebase_off() const INLINE { return E::get32(fields.rebase_off); }
861 void set_rebase_off(uint32_t value) INLINE { E::set32(fields.rebase_off, value); }
863 uint32_t rebase_size() const INLINE { return E::get32(fields.rebase_size); }
864 void set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value); }
866 uint32_t bind_off() const INLINE { return E::get32(fields.bind_off); }
867 void set_bind_off(uint32_t value) INLINE { E::set32(fields.bind_off, value); }
869 uint32_t bind_size() const INLINE { return E::get32(fields.bind_size); }
870 void set_bind_size(uint32_t value) INLINE { E::set32(fields.bind_size, value); }
872 uint32_t weak_bind_off() const INLINE { return E::get32(fields.weak_bind_off); }
873 void set_weak_bind_off(uint32_t value) INLINE { E::set32(fields.weak_bind_off, value); }
875 uint32_t weak_bind_size() const INLINE { return E::get32(fields.weak_bind_size); }
876 void set_weak_bind_size(uint32_t value) INLINE { E::set32(fields.weak_bind_size, value); }
878 uint32_t lazy_bind_off() const INLINE { return E::get32(fields.lazy_bind_off); }
879 void set_lazy_bind_off(uint32_t value) INLINE { E::set32(fields.lazy_bind_off, value); }
881 uint32_t lazy_bind_size() const INLINE { return E::get32(fields.lazy_bind_size); }
882 void set_lazy_bind_size(uint32_t value) INLINE { E::set32(fields.lazy_bind_size, value); }
884 uint32_t export_off() const INLINE { return E::get32(fields.export_off); }
885 void set_export_off(uint32_t value) INLINE { E::set32(fields.export_off, value); }
887 uint32_t export_size() const INLINE { return E::get32(fields.export_size); }
888 void set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value); }
891 typedef typename P::E E;
892 private:
893 dyld_info_command fields;
896 #ifndef NO_ULEB
897 inline uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end) {
898 uint64_t result = 0;
899 int bit = 0;
900 do {
901 if (p == end)
902 throw "malformed uleb128 extends beyond trie";
904 uint64_t slice = *p & 0x7f;
906 if (bit >= 64 || slice << bit >> bit != slice)
907 throw "uleb128 too big for 64-bits";
908 else {
909 result |= (slice << bit);
910 bit += 7;
913 while (*p++ & 0x80);
914 return result;
918 static int64_t read_sleb128(const uint8_t*& p, const uint8_t* end)
920 int64_t result = 0;
921 int bit = 0;
922 uint8_t byte;
923 do {
924 if (p == end)
925 throw "malformed sleb128";
926 byte = *p++;
927 result |= ((byte & 0x7f) << bit);
928 bit += 7;
929 } while (byte & 0x80);
930 // sign extend negative numbers
931 if ( (byte & 0x40) != 0 )
932 result |= (-1LL) << bit;
933 return result;
936 #endif
939 #endif // __MACH_O_FILE_ABSTRACTION__