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