dyld-625.13.tar.gz
[apple/dyld.git] / launch-cache / MachOFileAbstraction.hpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- 
2  *
3  * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4  *
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.
13  * 
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.
21  * 
22  * @APPLE_LICENSE_HEADER_END@
23 */
24 #ifndef __MACH_O_FILE_ABSTRACTION__
25 #define __MACH_O_FILE_ABSTRACTION__
26
27 #include <mach-o/loader.h>
28 #include <mach-o/nlist.h>
29 #include <mach-o/reloc.h>
30 #include <mach/machine.h>
31
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 */
39 };
40 #endif
41
42 #ifndef S_16BYTE_LITERALS
43         #define S_16BYTE_LITERALS 0xE
44 #endif
45
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
70
71 #ifndef CPU_ARCH_ABI64_32
72         #define CPU_ARCH_ABI64_32                               ((cpu_type_t) 0x02000000)
73 #endif
74 #ifndef CPU_TYPE_ARM64_32
75         #define CPU_TYPE_ARM64_32                               ((cpu_type_t) (CPU_TYPE_ARM | CPU_ARCH_ABI64_32))
76 #endif
77 #ifndef CPU_SUBTYPE_ARM64_32_V8
78         #define CPU_SUBTYPE_ARM64_32_V8         ((cpu_subtype_t) 1)
79 #endif
80
81
82 #define ARM64_RELOC_UNSIGNED            0 // for pointers
83
84
85 #ifndef LC_LOAD_UPWARD_DYLIB
86         #define LC_LOAD_UPWARD_DYLIB (0x23|LC_REQ_DYLD) /* load of dylib whose initializers run later */
87 #endif
88
89 #ifndef EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
90         #define EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER 0x10
91 #endif
92 #ifndef EXPORT_SYMBOL_FLAGS_REEXPORT
93         #define EXPORT_SYMBOL_FLAGS_REEXPORT 0x08
94 #endif
95
96 #ifndef LC_FUNCTION_STARTS
97         #define LC_FUNCTION_STARTS 0x26
98 #endif
99
100 #ifndef LC_DATA_IN_CODE
101         #define LC_DATA_IN_CODE 0x29
102 #endif
103
104 #ifndef LC_DYLIB_CODE_SIGN_DRS
105         #define LC_DYLIB_CODE_SIGN_DRS 0x2B
106 #endif
107
108 #ifndef CPU_SUBTYPE_X86_64_H
109         #define CPU_SUBTYPE_X86_64_H            ((cpu_subtype_t) 8) 
110 #endif
111
112
113 #define DYLD_CACHE_ADJ_V2_FORMAT                                0x7F
114
115 #define DYLD_CACHE_ADJ_V2_POINTER_32                    0x01
116 #define DYLD_CACHE_ADJ_V2_POINTER_64                    0x02
117 #define DYLD_CACHE_ADJ_V2_DELTA_32                          0x03
118 #define DYLD_CACHE_ADJ_V2_DELTA_64                          0x04
119 #define DYLD_CACHE_ADJ_V2_ARM64_ADRP                    0x05
120 #define DYLD_CACHE_ADJ_V2_ARM64_OFF12                   0x06
121 #define DYLD_CACHE_ADJ_V2_ARM64_BR26                    0x07
122 #define DYLD_CACHE_ADJ_V2_ARM_MOVW_MOVT                 0x08
123 #define DYLD_CACHE_ADJ_V2_ARM_BR24                              0x09
124 #define DYLD_CACHE_ADJ_V2_THUMB_MOVW_MOVT               0x0A
125 #define DYLD_CACHE_ADJ_V2_THUMB_BR22                    0x0B
126 #define DYLD_CACHE_ADJ_V2_IMAGE_OFF_32                  0x0C
127
128 #define MH_HAS_OBJC                     0x40000000
129
130 #ifndef CPU_SUBTYPE_ARM64_E
131         #define CPU_SUBTYPE_ARM64_E    2
132 #endif
133
134 #include "FileAbstraction.hpp"
135 #include "Architectures.hpp"
136
137 // utility to pair together a cpu-type and cpu-sub-type
138 struct ArchPair
139 {
140         uint32_t        arch;
141         uint32_t        subtype;
142         
143         ArchPair(uint32_t cputype, uint32_t cpusubtype) : arch(cputype), subtype(cpusubtype) {}
144         
145         bool operator<(const ArchPair& other) const { 
146                 if ( this->arch != other.arch )
147                         return (this->arch < other.arch);
148                 return (this->subtype < other.subtype);
149         }
150
151         bool operator==(const ArchPair& other) const { 
152         return this->arch == other.arch  &&  this->subtype == other.subtype;
153         }
154 };
155
156
157 //
158 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
159 //
160
161 //
162 // mach-o load command
163 //
164 template <typename P>
165 class macho_load_command {
166 public:
167         uint32_t                cmd() const                                             INLINE { return E::get32(command.cmd); }
168         void                    set_cmd(uint32_t value)                 INLINE { E::set32(command.cmd, value); }
169
170         uint32_t                cmdsize() const                                 INLINE { return E::get32(command.cmdsize); }
171         void                    set_cmdsize(uint32_t value)             INLINE { E::set32(command.cmdsize, value); }
172
173         typedef typename P::E           E;
174 private:
175         load_command    command;
176 };
177
178
179 //
180 // mach-o segment load command
181 //
182 template <typename P> struct macho_segment_content {};
183 template <> struct macho_segment_content<Pointer32<BigEndian> >    { segment_command    fields; enum { CMD = LC_SEGMENT         }; };
184 template <> struct macho_segment_content<Pointer64<BigEndian> >    { segment_command_64 fields; enum { CMD = LC_SEGMENT_64      }; };
185 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command    fields; enum { CMD = LC_SEGMENT         }; };
186 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64      }; };
187
188 template <typename P>
189 class macho_segment_command {
190 public:
191         uint32_t                cmd() const                                             INLINE { return E::get32(segment.fields.cmd); }
192         void                    set_cmd(uint32_t value)                 INLINE { E::set32(segment.fields.cmd, value); }
193
194         uint32_t                cmdsize() const                                 INLINE { return E::get32(segment.fields.cmdsize); }
195         void                    set_cmdsize(uint32_t value)             INLINE { E::set32(segment.fields.cmdsize, value); }
196
197         const char*             segname() const                                 INLINE { return segment.fields.segname; }
198         void                    set_segname(const char* value)  INLINE { strncpy(segment.fields.segname, value, 16); }
199         
200         uint64_t                vmaddr() const                                  INLINE { return P::getP(segment.fields.vmaddr); }
201         void                    set_vmaddr(uint64_t value)              INLINE { P::setP(segment.fields.vmaddr, value); }
202
203         uint64_t                vmsize() const                                  INLINE { return P::getP(segment.fields.vmsize); }
204         void                    set_vmsize(uint64_t value)              INLINE { P::setP(segment.fields.vmsize, value); }
205
206         uint64_t                fileoff() const                                 INLINE { return P::getP(segment.fields.fileoff); }
207         void                    set_fileoff(uint64_t value)             INLINE { P::setP(segment.fields.fileoff, value); }
208
209         uint64_t                filesize() const                                INLINE { return P::getP(segment.fields.filesize); }
210         void                    set_filesize(uint64_t value)    INLINE { P::setP(segment.fields.filesize, value); }
211
212         uint32_t                maxprot() const                                 INLINE { return E::get32(segment.fields.maxprot); }
213         void                    set_maxprot(uint32_t value)             INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
214
215         uint32_t                initprot() const                                INLINE { return E::get32(segment.fields.initprot); }
216         void                    set_initprot(uint32_t value)    INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
217
218         uint32_t                nsects() const                                  INLINE { return E::get32(segment.fields.nsects); }
219         void                    set_nsects(uint32_t value)              INLINE { E::set32(segment.fields.nsects, value); }
220
221         uint32_t                flags() const                                   INLINE { return E::get32(segment.fields.flags); }
222         void                    set_flags(uint32_t value)               INLINE { E::set32(segment.fields.flags, value); }
223
224         enum {
225                 CMD = macho_segment_content<P>::CMD
226         };
227
228         typedef typename P::E           E;
229 private:
230         macho_segment_content<P>        segment;
231 };
232
233
234 //
235 // mach-o section 
236 //
237 template <typename P> struct macho_section_content {};
238 template <> struct macho_section_content<Pointer32<BigEndian> >    { section    fields; };
239 template <> struct macho_section_content<Pointer64<BigEndian> >    { section_64 fields; };
240 template <> struct macho_section_content<Pointer32<LittleEndian> > { section    fields; };
241 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
242
243 template <typename P>
244 class macho_section {
245 public:
246         const char*             sectname() const                                INLINE { return section.fields.sectname; }
247         void                    set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
248         
249         const char*             segname() const                                 INLINE { return section.fields.segname; }
250         void                    set_segname(const char* value)  INLINE { strncpy(section.fields.segname, value, 16); }
251         
252         uint64_t                addr() const                                    INLINE { return P::getP(section.fields.addr); }
253         void                    set_addr(uint64_t value)                INLINE { P::setP(section.fields.addr, value); }
254
255         uint64_t                size() const                                    INLINE { return P::getP(section.fields.size); }
256         void                    set_size(uint64_t value)                INLINE { P::setP(section.fields.size, value); }
257
258         uint32_t                offset() const                                  INLINE { return E::get32(section.fields.offset); }
259         void                    set_offset(uint32_t value)              INLINE { E::set32(section.fields.offset, value); }
260
261         uint32_t                align() const                                   INLINE { return E::get32(section.fields.align); }
262         void                    set_align(uint32_t value)               INLINE { E::set32(section.fields.align, value); }
263
264         uint32_t                reloff() const                                  INLINE { return E::get32(section.fields.reloff); }
265         void                    set_reloff(uint32_t value)              INLINE { E::set32(section.fields.reloff, value); }
266
267         uint32_t                nreloc() const                                  INLINE { return E::get32(section.fields.nreloc); }
268         void                    set_nreloc(uint32_t value)              INLINE { E::set32(section.fields.nreloc, value); }
269
270         uint32_t                flags() const                                   INLINE { return E::get32(section.fields.flags); }
271         void                    set_flags(uint32_t value)               INLINE { E::set32(section.fields.flags, value); }
272
273         uint32_t                reserved1() const                               INLINE { return E::get32(section.fields.reserved1); }
274         void                    set_reserved1(uint32_t value)   INLINE { E::set32(section.fields.reserved1, value); }
275
276         uint32_t                reserved2() const                               INLINE { return E::get32(section.fields.reserved2); }
277         void                    set_reserved2(uint32_t value)   INLINE { E::set32(section.fields.reserved2, value); }
278
279         typedef typename P::E           E;
280 private:
281         macho_section_content<P>        section;
282 };
283
284
285 //
286 // mach-o dylib load command
287 //
288 template <typename P>
289 class macho_dylib_command {
290 public:
291         uint32_t                cmd() const                                                                     INLINE { return E::get32(fields.cmd); }
292         void                    set_cmd(uint32_t value)                                         INLINE { E::set32(fields.cmd, value); }
293
294         uint32_t                cmdsize() const                                                         INLINE { return E::get32(fields.cmdsize); }
295         void                    set_cmdsize(uint32_t value)                                     INLINE { E::set32(fields.cmdsize, value); }
296
297         uint32_t                name_offset() const                                                     INLINE { return E::get32(fields.dylib.name.offset); }
298         void                    set_name_offset(uint32_t value)                         INLINE { E::set32(fields.dylib.name.offset, value);  }
299         
300         uint32_t                timestamp() const                                                       INLINE { return E::get32(fields.dylib.timestamp); }
301         void                    set_timestamp(uint32_t value)                           INLINE { E::set32(fields.dylib.timestamp, value); }
302
303         uint32_t                current_version() const                                         INLINE { return E::get32(fields.dylib.current_version); }
304         void                    set_current_version(uint32_t value)                     INLINE { E::set32(fields.dylib.current_version, value); }
305
306         uint32_t                compatibility_version() const                           INLINE { return E::get32(fields.dylib.compatibility_version); }
307         void                    set_compatibility_version(uint32_t value)       INLINE { E::set32(fields.dylib.compatibility_version, value); }
308
309         const char*             name() const                                                            INLINE { return (const char*)&fields + name_offset(); }
310         void                    set_name_offset()                                                       INLINE { set_name_offset(sizeof(fields)); }
311         
312         typedef typename P::E           E;
313 private:
314         dylib_command   fields;
315 };
316
317
318 //
319 // mach-o dylinker load command
320 //
321 template <typename P>
322 class macho_dylinker_command {
323 public:
324         uint32_t                cmd() const                                                     INLINE { return E::get32(fields.cmd); }
325         void                    set_cmd(uint32_t value)                         INLINE { E::set32(fields.cmd, value); }
326
327         uint32_t                cmdsize() const                                         INLINE { return E::get32(fields.cmdsize); }
328         void                    set_cmdsize(uint32_t value)                     INLINE { E::set32(fields.cmdsize, value); }
329
330         uint32_t                name_offset() const                                     INLINE { return E::get32(fields.name.offset); }
331         void                    set_name_offset(uint32_t value)         INLINE { E::set32(fields.name.offset, value);  }
332         
333         const char*             name() const                                            INLINE { return (const char*)&fields + name_offset(); }
334         void                    set_name_offset()                                       INLINE { set_name_offset(sizeof(fields)); }
335         
336         typedef typename P::E           E;
337 private:
338         dylinker_command        fields;
339 };
340
341
342 //
343 // mach-o sub_framework load command
344 //
345 template <typename P>
346 class macho_sub_framework_command {
347 public:
348         uint32_t                cmd() const                                                     INLINE { return E::get32(fields.cmd); }
349         void                    set_cmd(uint32_t value)                         INLINE { E::set32(fields.cmd, value); }
350
351         uint32_t                cmdsize() const                                         INLINE { return E::get32(fields.cmdsize); }
352         void                    set_cmdsize(uint32_t value)                     INLINE { E::set32(fields.cmdsize, value); }
353
354         uint32_t                umbrella_offset() const                         INLINE { return E::get32(fields.umbrella.offset); }
355         void                    set_umbrella_offset(uint32_t value)     INLINE { E::set32(fields.umbrella.offset, value);  }
356         
357         const char*             umbrella() const                                        INLINE { return (const char*)&fields + umbrella_offset(); }
358         void                    set_umbrella_offset()                           INLINE { set_umbrella_offset(sizeof(fields)); }
359                 
360         typedef typename P::E           E;
361 private:
362         sub_framework_command   fields;
363 };
364
365
366 //
367 // mach-o sub_client load command
368 //
369 template <typename P>
370 class macho_sub_client_command {
371 public:
372         uint32_t                cmd() const                                                     INLINE { return E::get32(fields.cmd); }
373         void                    set_cmd(uint32_t value)                         INLINE { E::set32(fields.cmd, value); }
374
375         uint32_t                cmdsize() const                                         INLINE { return E::get32(fields.cmdsize); }
376         void                    set_cmdsize(uint32_t value)                     INLINE { E::set32(fields.cmdsize, value); }
377
378         uint32_t                client_offset() const                           INLINE { return E::get32(fields.client.offset); }
379         void                    set_client_offset(uint32_t value)       INLINE { E::set32(fields.client.offset, value);  }
380         
381         const char*             client() const                                          INLINE { return (const char*)&fields + client_offset(); }
382         void                    set_client_offset()                                     INLINE { set_client_offset(sizeof(fields)); }
383                 
384         typedef typename P::E           E;
385 private:
386         sub_client_command      fields;
387 };
388
389
390 //
391 // mach-o sub_umbrella load command
392 //
393 template <typename P>
394 class macho_sub_umbrella_command {
395 public:
396         uint32_t                cmd() const                                                             INLINE { return E::get32(fields.cmd); }
397         void                    set_cmd(uint32_t value)                                 INLINE { E::set32(fields.cmd, value); }
398
399         uint32_t                cmdsize() const                                                 INLINE { return E::get32(fields.cmdsize); }
400         void                    set_cmdsize(uint32_t value)                             INLINE { E::set32(fields.cmdsize, value); }
401
402         uint32_t                sub_umbrella_offset() const                             INLINE { return E::get32(fields.sub_umbrella.offset); }
403         void                    set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value);  }
404         
405         const char*             sub_umbrella() const                                    INLINE { return (const char*)&fields + sub_umbrella_offset(); }
406         void                    set_sub_umbrella_offset()                               INLINE { set_sub_umbrella_offset(sizeof(fields)); }
407                 
408         typedef typename P::E           E;
409 private:
410         sub_umbrella_command    fields;
411 };
412
413
414 //
415 // mach-o sub_library load command
416 //
417 template <typename P>
418 class macho_sub_library_command {
419 public:
420         uint32_t                cmd() const                                                             INLINE { return E::get32(fields.cmd); }
421         void                    set_cmd(uint32_t value)                                 INLINE { E::set32(fields.cmd, value); }
422
423         uint32_t                cmdsize() const                                                 INLINE { return E::get32(fields.cmdsize); }
424         void                    set_cmdsize(uint32_t value)                             INLINE { E::set32(fields.cmdsize, value); }
425
426         uint32_t                sub_library_offset() const                              INLINE { return E::get32(fields.sub_library.offset); }
427         void                    set_sub_library_offset(uint32_t value)  INLINE { E::set32(fields.sub_library.offset, value);  }
428         
429         const char*             sub_library() const                                             INLINE { return (const char*)&fields + sub_library_offset(); }
430         void                    set_sub_library_offset()                                INLINE { set_sub_library_offset(sizeof(fields)); }
431                 
432         typedef typename P::E           E;
433 private:
434         sub_library_command     fields;
435 };
436
437
438 //
439 // mach-o uuid load command
440 //
441 template <typename P>
442 class macho_uuid_command {
443 public:
444         uint32_t                cmd() const                                                             INLINE { return E::get32(fields.cmd); }
445         void                    set_cmd(uint32_t value)                                 INLINE { E::set32(fields.cmd, value); }
446
447         uint32_t                cmdsize() const                                                 INLINE { return E::get32(fields.cmdsize); }
448         void                    set_cmdsize(uint32_t value)                             INLINE { E::set32(fields.cmdsize, value); }
449
450         const uint8_t*  uuid() const                                                    INLINE { return fields.uuid; }
451         void                    set_uuid(uint8_t value[16])                             INLINE { memcpy(&fields.uuid, value, 16); }
452                         
453         typedef typename P::E           E;
454 private:
455         uuid_command    fields;
456 };
457
458
459 //
460 // mach-o routines load command
461 //
462 template <typename P> struct macho_routines_content {};
463 template <> struct macho_routines_content<Pointer32<BigEndian> >    { routines_command          fields; enum { CMD = LC_ROUTINES        }; };
464 template <> struct macho_routines_content<Pointer64<BigEndian> >        { routines_command_64   fields; enum { CMD = LC_ROUTINES_64     }; };
465 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command          fields; enum { CMD = LC_ROUTINES        }; };
466 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64       fields; enum { CMD = LC_ROUTINES_64     }; };
467
468 template <typename P>
469 class macho_routines_command {
470 public:
471         uint32_t                cmd() const                                                     INLINE { return E::get32(routines.fields.cmd); }
472         void                    set_cmd(uint32_t value)                         INLINE { E::set32(routines.fields.cmd, value); }
473
474         uint32_t                cmdsize() const                                         INLINE { return E::get32(routines.fields.cmdsize); }
475         void                    set_cmdsize(uint32_t value)                     INLINE { E::set32(routines.fields.cmdsize, value); }
476
477         uint64_t                init_address() const                            INLINE { return P::getP(routines.fields.init_address); }
478         void                    set_init_address(uint64_t value)        INLINE { P::setP(routines.fields.init_address, value); }
479
480         uint64_t                init_module() const                                     INLINE { return P::getP(routines.fields.init_module); }
481         void                    set_init_module(uint64_t value)         INLINE { P::setP(routines.fields.init_module, value); }
482
483         uint64_t                reserved1() const                                       INLINE { return P::getP(routines.fields.reserved1); }
484         void                    set_reserved1(uint64_t value)           INLINE { P::setP(routines.fields.reserved1, value); }
485         
486         uint64_t                reserved2() const                                       INLINE { return P::getP(routines.fields.reserved2); }
487         void                    set_reserved2(uint64_t value)           INLINE { P::setP(routines.fields.reserved2, value); }
488         
489         uint64_t                reserved3() const                                       INLINE { return P::getP(routines.fields.reserved3); }
490         void                    set_reserved3(uint64_t value)           INLINE { P::setP(routines.fields.reserved3, value); }
491         
492         uint64_t                reserved4() const                                       INLINE { return P::getP(routines.fields.reserved4); }
493         void                    set_reserved4(uint64_t value)           INLINE { P::setP(routines.fields.reserved4, value); }
494         
495         uint64_t                reserved5() const                                       INLINE { return P::getP(routines.fields.reserved5); }
496         void                    set_reserved5(uint64_t value)           INLINE { P::setP(routines.fields.reserved5, value); }
497         
498         uint64_t                reserved6() const                                       INLINE { return P::getP(routines.fields.reserved6); }
499         void                    set_reserved6(uint64_t value)           INLINE { P::setP(routines.fields.reserved6, value); }
500         
501         typedef typename P::E           E;
502         enum {
503                 CMD = macho_routines_content<P>::CMD
504         };
505 private:
506         macho_routines_content<P>       routines;
507 };
508
509
510 //
511 // mach-o symbol table load command
512 //
513 template <typename P>
514 class macho_symtab_command {
515 public:
516         uint32_t                cmd() const                                     INLINE { return E::get32(fields.cmd); }
517         void                    set_cmd(uint32_t value)         INLINE { E::set32(fields.cmd, value); }
518
519         uint32_t                cmdsize() const                         INLINE { return E::get32(fields.cmdsize); }
520         void                    set_cmdsize(uint32_t value)     INLINE { E::set32(fields.cmdsize, value); }
521
522         uint32_t                symoff() const                          INLINE { return E::get32(fields.symoff); }
523         void                    set_symoff(uint32_t value)      INLINE { E::set32(fields.symoff, value);  }
524         
525         uint32_t                nsyms() const                           INLINE { return E::get32(fields.nsyms); }
526         void                    set_nsyms(uint32_t value)       INLINE { E::set32(fields.nsyms, value);  }
527         
528         uint32_t                stroff() const                          INLINE { return E::get32(fields.stroff); }
529         void                    set_stroff(uint32_t value)      INLINE { E::set32(fields.stroff, value);  }
530         
531         uint32_t                strsize() const                         INLINE { return E::get32(fields.strsize); }
532         void                    set_strsize(uint32_t value)     INLINE { E::set32(fields.strsize, value);  }
533         
534         
535         typedef typename P::E           E;
536 private:
537         symtab_command  fields;
538 };
539
540
541 //
542 // mach-o dynamic symbol table load command
543 //
544 template <typename P>
545 class macho_dysymtab_command {
546 public:
547         uint32_t                cmd() const                                                     INLINE { return E::get32(fields.cmd); }
548         void                    set_cmd(uint32_t value)                         INLINE { E::set32(fields.cmd, value); }
549
550         uint32_t                cmdsize() const                                         INLINE { return E::get32(fields.cmdsize); }
551         void                    set_cmdsize(uint32_t value)                     INLINE { E::set32(fields.cmdsize, value); }
552
553         uint32_t                ilocalsym() const                                       INLINE { return E::get32(fields.ilocalsym); }
554         void                    set_ilocalsym(uint32_t value)           INLINE { E::set32(fields.ilocalsym, value);  }
555         
556         uint32_t                nlocalsym() const                                       INLINE { return E::get32(fields.nlocalsym); }
557         void                    set_nlocalsym(uint32_t value)           INLINE { E::set32(fields.nlocalsym, value);  }
558         
559         uint32_t                iextdefsym() const                                      INLINE { return E::get32(fields.iextdefsym); }
560         void                    set_iextdefsym(uint32_t value)          INLINE { E::set32(fields.iextdefsym, value);  }
561         
562         uint32_t                nextdefsym() const                                      INLINE { return E::get32(fields.nextdefsym); }
563         void                    set_nextdefsym(uint32_t value)          INLINE { E::set32(fields.nextdefsym, value);  }
564         
565         uint32_t                iundefsym() const                                       INLINE { return E::get32(fields.iundefsym); }
566         void                    set_iundefsym(uint32_t value)           INLINE { E::set32(fields.iundefsym, value);  }
567         
568         uint32_t                nundefsym() const                                       INLINE { return E::get32(fields.nundefsym); }
569         void                    set_nundefsym(uint32_t value)           INLINE { E::set32(fields.nundefsym, value);  }
570         
571         uint32_t                tocoff() const                                          INLINE { return E::get32(fields.tocoff); }
572         void                    set_tocoff(uint32_t value)                      INLINE { E::set32(fields.tocoff, value);  }
573         
574         uint32_t                ntoc() const                                            INLINE { return E::get32(fields.ntoc); }
575         void                    set_ntoc(uint32_t value)                        INLINE { E::set32(fields.ntoc, value);  }
576         
577         uint32_t                modtaboff() const                                       INLINE { return E::get32(fields.modtaboff); }
578         void                    set_modtaboff(uint32_t value)           INLINE { E::set32(fields.modtaboff, value);  }
579         
580         uint32_t                nmodtab() const                                         INLINE { return E::get32(fields.nmodtab); }
581         void                    set_nmodtab(uint32_t value)                     INLINE { E::set32(fields.nmodtab, value);  }
582         
583         uint32_t                extrefsymoff() const                            INLINE { return E::get32(fields.extrefsymoff); }
584         void                    set_extrefsymoff(uint32_t value)        INLINE { E::set32(fields.extrefsymoff, value);  }
585         
586         uint32_t                nextrefsyms() const                                     INLINE { return E::get32(fields.nextrefsyms); }
587         void                    set_nextrefsyms(uint32_t value)         INLINE { E::set32(fields.nextrefsyms, value);  }
588         
589         uint32_t                indirectsymoff() const                          INLINE { return E::get32(fields.indirectsymoff); }
590         void                    set_indirectsymoff(uint32_t value)      INLINE { E::set32(fields.indirectsymoff, value);  }
591         
592         uint32_t                nindirectsyms() const                           INLINE { return E::get32(fields.nindirectsyms); }
593         void                    set_nindirectsyms(uint32_t value)       INLINE { E::set32(fields.nindirectsyms, value);  }
594         
595         uint32_t                extreloff() const                                       INLINE { return E::get32(fields.extreloff); }
596         void                    set_extreloff(uint32_t value)           INLINE { E::set32(fields.extreloff, value);  }
597         
598         uint32_t                nextrel() const                                         INLINE { return E::get32(fields.nextrel); }
599         void                    set_nextrel(uint32_t value)                     INLINE { E::set32(fields.nextrel, value);  }
600         
601         uint32_t                locreloff() const                                       INLINE { return E::get32(fields.locreloff); }
602         void                    set_locreloff(uint32_t value)           INLINE { E::set32(fields.locreloff, value);  }
603         
604         uint32_t                nlocrel() const                                         INLINE { return E::get32(fields.nlocrel); }
605         void                    set_nlocrel(uint32_t value)                     INLINE { E::set32(fields.nlocrel, value);  }
606         
607         typedef typename P::E           E;
608 private:
609         dysymtab_command        fields;
610 };
611
612
613 //
614 // mach-o two-level hints load command
615 //
616 template <typename P>
617 class macho_twolevel_hints_command {
618 public:
619         uint32_t                cmd() const                                     INLINE { return E::get32(fields.cmd); }
620         void                    set_cmd(uint32_t value)         INLINE { E::set32(fields.cmd, value); }
621
622         uint32_t                cmdsize() const                         INLINE { return E::get32(fields.cmdsize); }
623         void                    set_cmdsize(uint32_t value)     INLINE { E::set32(fields.cmdsize, value); }
624
625         uint32_t                offset() const                          INLINE { return E::get32(fields.offset); }
626         void                    set_offset(uint32_t value)      INLINE { E::set32(fields.offset, value);  }
627         
628         uint32_t                nhints() const                          INLINE { return E::get32(fields.nhints); }
629         void                    set_nhints(uint32_t value)      INLINE { E::set32(fields.nhints, value);  }
630         
631         typedef typename P::E           E;
632 private:
633         twolevel_hints_command  fields;
634 };
635
636
637 //
638 // mach-o threads load command
639 //
640 template <typename P>
641 class macho_thread_command {
642 public:
643         uint32_t                cmd() const                                                                                     INLINE { return E::get32(fields.cmd); }
644         void                    set_cmd(uint32_t value)                                                         INLINE { E::set32(fields.cmd, value); }
645
646         uint32_t                cmdsize() const                                                                         INLINE { return E::get32(fields.cmdsize); }
647         void                    set_cmdsize(uint32_t value)                                                     INLINE { E::set32(fields.cmdsize, value); }
648
649         uint32_t                flavor() const                                                                          INLINE { return E::get32(fields_flavor); }
650         void                    set_flavor(uint32_t value)                                                      INLINE { E::set32(fields_flavor, value);  }
651         
652         uint32_t                count() const                                                                           INLINE { return E::get32(fields_count); }
653         void                    set_count(uint32_t value)                                                       INLINE { E::set32(fields_count, value);  }
654         
655         uint64_t                thread_register(uint32_t index) const                           INLINE { return P::getP(thread_registers[index]); }
656         void                    set_thread_register(uint32_t index, uint64_t value)     INLINE { P::setP(thread_registers[index], value); }
657         
658         typedef typename P::E           E;
659         typedef typename P::uint_t      pint_t;
660 private:
661         struct thread_command   fields;
662         uint32_t                                fields_flavor;
663         uint32_t                                fields_count;
664         pint_t                                  thread_registers[1];
665 };
666
667
668 //
669 // mach-o misc data 
670 //
671 template <typename P>
672 class macho_linkedit_data_command {
673 public:
674         uint32_t                cmd() const                                     INLINE { return E::get32(fields.cmd); }
675         void                    set_cmd(uint32_t value)         INLINE { E::set32(fields.cmd, value); }
676
677         uint32_t                cmdsize() const                         INLINE { return E::get32(fields.cmdsize); }
678         void                    set_cmdsize(uint32_t value)     INLINE { E::set32(fields.cmdsize, value); }
679
680         uint32_t                dataoff() const                         INLINE { return E::get32(fields.dataoff); }
681         void                    set_dataoff(uint32_t value)     INLINE { E::set32(fields.dataoff, value);  }
682         
683         uint32_t                datasize() const                        INLINE { return E::get32(fields.datasize); }
684         void                    set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value);  }
685         
686         
687         typedef typename P::E           E;
688 private:
689         linkedit_data_command   fields;
690 };
691
692
693 //
694 // mach-o symbol table entry 
695 //
696 template <typename P> struct macho_nlist_content {};
697 template <> struct macho_nlist_content<Pointer32<BigEndian> >    { struct nlist         fields; };
698 template <> struct macho_nlist_content<Pointer64<BigEndian> >    { struct nlist_64      fields; };
699 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist         fields; };
700 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64      fields; };
701
702 template <typename P>
703 class macho_nlist {
704 public:
705         uint32_t                n_strx() const                                  INLINE { return E::get32(entry.fields.n_un.n_strx); }
706         void                    set_n_strx(uint32_t value)              INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
707
708         uint8_t                 n_type() const                                  INLINE { return entry.fields.n_type; }
709         void                    set_n_type(uint8_t value)               INLINE { entry.fields.n_type = value; }
710
711         uint8_t                 n_sect() const                                  INLINE { return entry.fields.n_sect; }
712         void                    set_n_sect(uint8_t value)               INLINE { entry.fields.n_sect = value; }
713
714         uint16_t                n_desc() const                                  INLINE { return E::get16(entry.fields.n_desc); }
715         void                    set_n_desc(uint16_t value)              INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
716
717         uint64_t                n_value() const                                 INLINE { return P::getP(entry.fields.n_value); }
718         void                    set_n_value(uint64_t value)             INLINE { P::setP(entry.fields.n_value, value); }
719
720         typedef typename P::E           E;
721 private:
722         macho_nlist_content<P>  entry;
723 };
724
725
726
727 //
728 // mach-o relocation info
729 //
730 template <typename P>
731 class macho_relocation_info {
732 public:
733         uint32_t                r_address() const                               INLINE { return E::get32(address); }
734         void                    set_r_address(uint32_t value)   INLINE { E::set32(address, value); }
735
736         uint32_t                r_symbolnum() const                             INLINE { return E::getBits(other, 0, 24); }
737         void                    set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
738
739         bool                    r_pcrel() const                                 INLINE { return E::getBits(other, 24, 1); }
740         void                    set_r_pcrel(bool value)                 INLINE { E::setBits(other, value, 24, 1); }     
741         
742         uint8_t                 r_length() const                                INLINE { return E::getBits(other, 25, 2); }
743         void                    set_r_length(uint8_t value)             INLINE { E::setBits(other, value, 25, 2); }
744         
745         bool                    r_extern() const                                INLINE { return E::getBits(other, 27, 1); }
746         void                    set_r_extern(bool value)                INLINE { E::setBits(other, value, 27, 1); }
747         
748         uint8_t                 r_type() const                                  INLINE { return E::getBits(other, 28, 4); }
749         void                    set_r_type(uint8_t value)               INLINE { E::setBits(other, value, 28, 4); }
750                 
751         void                    set_r_length()                                  INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
752
753         typedef typename P::E           E;
754 private:
755         uint32_t                address;
756         uint32_t                other;
757 };
758
759
760 //
761 // mach-o scattered relocation info
762 // The bit fields are always in big-endian order (see mach-o/reloc.h)
763 //
764 template <typename P>
765 class macho_scattered_relocation_info {
766 public:
767         bool                    r_scattered() const                     INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
768         void                    set_r_scattered(bool x)         INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1);  E::set32(other, temp); }
769
770         bool                    r_pcrel() const                         INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
771         void                    set_r_pcrel(bool x)                     INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1);  E::set32(other, temp); }
772
773         uint8_t                 r_length() const                        INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
774         void                    set_r_length(uint8_t x)         INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2);  E::set32(other, temp); }
775
776         uint8_t                 r_type() const                          INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
777         void                    set_r_type(uint8_t x)           INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4);  E::set32(other, temp); }
778
779         uint32_t                r_address() const                       INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
780         void                    set_r_address(uint32_t x)       INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24);  E::set32(other, temp); }
781
782         uint32_t                r_value() const                         INLINE { return E::get32(value); }
783         void                    set_r_value(uint32_t x)         INLINE { E::set32(value, x); }
784
785         uint32_t                r_other() const                         INLINE { return other; }
786         
787         typedef typename P::E           E;
788 private:
789         uint32_t                other;
790         uint32_t                value;
791 };
792
793
794 //
795 // mach-o file header
796 //
797 template <typename P> struct macho_header_content {};
798 template <> struct macho_header_content<Pointer32<BigEndian> >    { mach_header         fields; };
799 template <> struct macho_header_content<Pointer64<BigEndian> >    { mach_header_64      fields; };
800 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header         fields; };
801 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64      fields; };
802
803 template <typename P>
804 class macho_header {
805 public:
806         uint32_t                magic() const                                   INLINE { return E::get32(header.fields.magic); }
807         void                    set_magic(uint32_t value)               INLINE { E::set32(header.fields.magic, value); }
808
809         uint32_t                cputype() const                                 INLINE { return E::get32(header.fields.cputype); }
810         void                    set_cputype(uint32_t value)             INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
811
812         uint32_t                cpusubtype() const                              INLINE { return E::get32(header.fields.cpusubtype); }
813         void                    set_cpusubtype(uint32_t value)  INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
814
815         uint32_t                filetype() const                                INLINE { return E::get32(header.fields.filetype); }
816         void                    set_filetype(uint32_t value)    INLINE { E::set32(header.fields.filetype, value); }
817
818         uint32_t                ncmds() const                                   INLINE { return E::get32(header.fields.ncmds); }
819         void                    set_ncmds(uint32_t value)               INLINE { E::set32(header.fields.ncmds, value); }
820
821         uint32_t                sizeofcmds() const                              INLINE { return E::get32(header.fields.sizeofcmds); }
822         void                    set_sizeofcmds(uint32_t value)  INLINE { E::set32(header.fields.sizeofcmds, value); }
823
824         uint32_t                flags() const                                   INLINE { return E::get32(header.fields.flags); }
825         void                    set_flags(uint32_t value)               INLINE { E::set32(header.fields.flags, value); }
826
827         uint32_t                reserved() const                                INLINE { return E::get32(header.fields.reserved); }
828         void                    set_reserved(uint32_t value)    INLINE { E::set32(header.fields.reserved, value); }
829
830     const macho_segment_command<P>* getSegment(const char *segname) const
831     {
832         const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
833         uint32_t cmd_count = this->ncmds();
834         const macho_load_command<P>* cmd = cmds;
835         for (uint32_t i = 0; i < cmd_count; ++i) {
836             if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
837                 const macho_segment_command<P>* segcmd = (macho_segment_command<P>*)cmd;
838                 if (0 == strncmp(segname, segcmd->segname(), 16)) {
839                     return segcmd;
840               }
841             }
842             cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
843         }
844         return NULL;
845     }
846
847     const macho_section<P>* getSection(const char *segname, const char *sectname) const
848     {
849        const  macho_segment_command<P>* segcmd = getSegment(segname);
850         if (!segcmd) return NULL;
851
852         const macho_section<P>* sectcmd = (macho_section<P>*)(segcmd+1);
853         uint32_t section_count = segcmd->nsects();
854         for (uint32_t j = 0; j < section_count; ++j) {
855             if (0 == ::strncmp(sectcmd[j].sectname(), sectname, 16)) {
856                 return sectcmd+j;
857             }
858         }
859
860                 if (strcmp(segname, "__DATA") == 0)
861                         return getSection("__DATA_CONST", sectname);
862         return NULL;
863     }
864
865     const macho_load_command<P>* getLoadCommand(int query) const
866     {
867         const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
868         uint32_t cmd_count = this->ncmds();
869         const macho_load_command<P>* cmd = cmds;
870         for (uint32_t i = 0; i < cmd_count; ++i) {
871             if ( cmd->cmd() == query ) {
872                 return cmd;
873             }
874             cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
875         }
876         return NULL;
877     }
878
879         typedef typename P::E           E;
880 private:
881         macho_header_content<P> header;
882 };
883
884
885
886 //
887 // compressed dyld info load command
888 //
889 template <typename P>
890 class macho_dyld_info_command {
891 public:
892         uint32_t                cmd() const                                     INLINE { return E::get32(fields.cmd); }
893         void                    set_cmd(uint32_t value)         INLINE { E::set32(fields.cmd, value); }
894
895         uint32_t                cmdsize() const                         INLINE { return E::get32(fields.cmdsize); }
896         void                    set_cmdsize(uint32_t value)     INLINE { E::set32(fields.cmdsize, value); }
897
898         uint32_t                rebase_off() const                              INLINE { return E::get32(fields.rebase_off); }
899         void                    set_rebase_off(uint32_t value)  INLINE { E::set32(fields.rebase_off, value);  }
900         
901         uint32_t                rebase_size() const                             INLINE { return E::get32(fields.rebase_size); }
902         void                    set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value);  }
903         
904         uint32_t                bind_off() const                                INLINE { return E::get32(fields.bind_off); }
905         void                    set_bind_off(uint32_t value)    INLINE { E::set32(fields.bind_off, value);  }
906         
907         uint32_t                bind_size() const                               INLINE { return E::get32(fields.bind_size); }
908         void                    set_bind_size(uint32_t value)   INLINE { E::set32(fields.bind_size, value);  }
909         
910         uint32_t                weak_bind_off() const                           INLINE { return E::get32(fields.weak_bind_off); }
911         void                    set_weak_bind_off(uint32_t value)       INLINE { E::set32(fields.weak_bind_off, value);  }
912         
913         uint32_t                weak_bind_size() const                          INLINE { return E::get32(fields.weak_bind_size); }
914         void                    set_weak_bind_size(uint32_t value)      INLINE { E::set32(fields.weak_bind_size, value);  }
915         
916         uint32_t                lazy_bind_off() const                           INLINE { return E::get32(fields.lazy_bind_off); }
917         void                    set_lazy_bind_off(uint32_t value)       INLINE { E::set32(fields.lazy_bind_off, value);  }
918         
919         uint32_t                lazy_bind_size() const                          INLINE { return E::get32(fields.lazy_bind_size); }
920         void                    set_lazy_bind_size(uint32_t value)      INLINE { E::set32(fields.lazy_bind_size, value);  }
921         
922         uint32_t                export_off() const                              INLINE { return E::get32(fields.export_off); }
923         void                    set_export_off(uint32_t value)  INLINE { E::set32(fields.export_off, value);  }
924         
925         uint32_t                export_size() const                             INLINE { return E::get32(fields.export_size); }
926         void                    set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value);  }
927         
928         
929         typedef typename P::E           E;
930 private:
931         dyld_info_command       fields;
932 };
933
934 #ifndef NO_ULEB 
935 inline uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end) {
936         uint64_t result = 0;
937         int              bit = 0;
938         do {
939                 if (p == end)
940                         throw "malformed uleb128 extends beyond trie";
941
942                 uint64_t slice = *p & 0x7f;
943
944                 if (bit >= 64 || slice << bit >> bit != slice)
945                         throw "uleb128 too big for 64-bits";
946                 else {
947                         result |= (slice << bit);
948                         bit += 7;
949                 }
950         } 
951         while (*p++ & 0x80);
952         return result;
953 }
954         
955
956 inline int64_t read_sleb128(const uint8_t*& p, const uint8_t* end)
957 {
958         int64_t result = 0;
959         int bit = 0;
960         uint8_t byte;
961         do {
962                 if (p == end)
963                         throw "malformed sleb128";
964                 byte = *p++;
965                 result |= (((int64_t)(byte & 0x7f)) << bit);
966                 bit += 7;
967         } while (byte & 0x80);
968         // sign extend negative numbers
969         if ( (byte & 0x40) != 0 )
970                 result |= (~0ULL) << bit;
971         return result;
972 }
973
974 #endif
975
976
977 #endif  // __MACH_O_FILE_ABSTRACTION__
978
979