195-6
[darwin-xtools.git] / dyld / launch-cache / MachOFileAbstraction.hpp
blob3be3fa2eb0c7d8d7fe052062f889b902a3f79f3d
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
24 #ifndef __MACH_O_FILE_ABSTRACTION__
25 #define __MACH_O_FILE_ABSTRACTION__
27 #include <mach-o/loader.h>
28 #include <mach-o/nlist.h>
29 #include <mach-o/reloc.h>
30 #include <mach/machine.h>
32 // suport older versions of mach-o/loader.h
33 #ifndef LC_UUID
34 #define LC_UUID 0x1b
35 struct uuid_command {
36 uint32_t cmd; /* LC_UUID */
37 uint32_t cmdsize; /* sizeof(struct uuid_command) */
38 uint8_t uuid[16]; /* the 128-bit uuid */
40 #endif
42 #ifndef S_16BYTE_LITERALS
43 #define S_16BYTE_LITERALS 0xE
44 #endif
46 #ifndef CPU_SUBTYPE_ARM_V5TEJ
47 #define CPU_SUBTYPE_ARM_V5TEJ ((cpu_subtype_t) 7)
48 #endif
49 #ifndef CPU_SUBTYPE_ARM_XSCALE
50 #define CPU_SUBTYPE_ARM_XSCALE ((cpu_subtype_t) 8)
51 #endif
52 #ifndef CPU_SUBTYPE_ARM_V7
53 #define CPU_SUBTYPE_ARM_V7 ((cpu_subtype_t) 9)
54 #endif
56 #ifndef LC_LOAD_UPWARD_DYLIB
57 #define LC_LOAD_UPWARD_DYLIB (0x23|LC_REQ_DYLD) /* load of dylib whose initializers run later */
58 #endif
60 #ifndef EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER
61 #define EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER 0x10
62 #endif
63 #ifndef EXPORT_SYMBOL_FLAGS_REEXPORT
64 #define EXPORT_SYMBOL_FLAGS_REEXPORT 0x08
65 #endif
68 #include "FileAbstraction.hpp"
69 #include "Architectures.hpp"
71 // utility to pair together a cpu-type and cpu-sub-type
72 struct ArchPair
74 uint32_t arch;
75 uint32_t subtype;
77 ArchPair(uint32_t cputype, uint32_t cpusubtype) : arch(cputype), subtype(cpusubtype) {}
79 bool operator<(const ArchPair& other) const {
80 if ( this->arch != other.arch )
81 return (this->arch < other.arch);
82 return (this->subtype < other.subtype);
88 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
92 // mach-o load command
94 template <typename P>
95 class macho_load_command {
96 public:
97 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
98 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
100 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
101 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
103 typedef typename P::E E;
104 private:
105 load_command command;
110 // mach-o segment load command
112 template <typename P> struct macho_segment_content {};
113 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
114 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
115 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
116 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
118 template <typename P>
119 class macho_segment_command {
120 public:
121 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
122 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
124 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
125 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
127 const char* segname() const INLINE { return segment.fields.segname; }
128 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
130 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
131 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
133 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
134 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
136 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
137 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
139 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
140 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
142 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
143 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
145 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
146 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
148 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
149 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
151 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
152 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
154 enum {
155 CMD = macho_segment_content<P>::CMD
158 typedef typename P::E E;
159 private:
160 macho_segment_content<P> segment;
165 // mach-o section
167 template <typename P> struct macho_section_content {};
168 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
169 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
170 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
171 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
173 template <typename P>
174 class macho_section {
175 public:
176 const char* sectname() const INLINE { return section.fields.sectname; }
177 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
179 const char* segname() const INLINE { return section.fields.segname; }
180 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
182 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
183 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
185 uint64_t size() const INLINE { return P::getP(section.fields.size); }
186 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
188 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
189 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
191 uint32_t align() const INLINE { return E::get32(section.fields.align); }
192 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
194 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
195 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
197 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
198 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
200 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
201 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
203 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
204 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
206 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
207 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
209 typedef typename P::E E;
210 private:
211 macho_section_content<P> section;
216 // mach-o dylib load command
218 template <typename P>
219 class macho_dylib_command {
220 public:
221 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
222 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
224 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
225 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
227 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
228 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
230 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
231 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
233 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
234 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
236 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
237 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
239 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
240 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
242 typedef typename P::E E;
243 private:
244 dylib_command fields;
249 // mach-o dylinker load command
251 template <typename P>
252 class macho_dylinker_command {
253 public:
254 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
255 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
257 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
258 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
260 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
261 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
263 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
264 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
266 typedef typename P::E E;
267 private:
268 dylinker_command fields;
273 // mach-o sub_framework load command
275 template <typename P>
276 class macho_sub_framework_command {
277 public:
278 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
279 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
281 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
282 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
284 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
285 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
287 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
288 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
290 typedef typename P::E E;
291 private:
292 sub_framework_command fields;
297 // mach-o sub_client load command
299 template <typename P>
300 class macho_sub_client_command {
301 public:
302 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
303 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
305 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
306 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
308 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
309 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
311 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
312 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
314 typedef typename P::E E;
315 private:
316 sub_client_command fields;
321 // mach-o sub_umbrella load command
323 template <typename P>
324 class macho_sub_umbrella_command {
325 public:
326 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
327 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
329 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
330 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
332 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
333 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
335 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
336 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
338 typedef typename P::E E;
339 private:
340 sub_umbrella_command fields;
345 // mach-o sub_library load command
347 template <typename P>
348 class macho_sub_library_command {
349 public:
350 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
351 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
353 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
354 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
356 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
357 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
359 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
360 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
362 typedef typename P::E E;
363 private:
364 sub_library_command fields;
369 // mach-o uuid load command
371 template <typename P>
372 class macho_uuid_command {
373 public:
374 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
375 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
377 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
378 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
380 const uint8_t* uuid() const INLINE { return fields.uuid; }
381 void set_uuid(uint8_t value[16]) INLINE { memcpy(&fields.uuid, value, 16); }
383 typedef typename P::E E;
384 private:
385 uuid_command fields;
390 // mach-o routines load command
392 template <typename P> struct macho_routines_content {};
393 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
394 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
395 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
396 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
398 template <typename P>
399 class macho_routines_command {
400 public:
401 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
402 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
404 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
405 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
407 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
408 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
410 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
411 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
413 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
414 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
416 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
417 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
419 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
420 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
422 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
423 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
425 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
426 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
428 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
429 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
431 typedef typename P::E E;
432 enum {
433 CMD = macho_routines_content<P>::CMD
435 private:
436 macho_routines_content<P> routines;
441 // mach-o symbol table load command
443 template <typename P>
444 class macho_symtab_command {
445 public:
446 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
447 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
449 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
450 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
452 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
453 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
455 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
456 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
458 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
459 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
461 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
462 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
465 typedef typename P::E E;
466 private:
467 symtab_command fields;
472 // mach-o dynamic symbol table load command
474 template <typename P>
475 class macho_dysymtab_command {
476 public:
477 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
478 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
480 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
481 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
483 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
484 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
486 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
487 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
489 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
490 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
492 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
493 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
495 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
496 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
498 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
499 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
501 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
502 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
504 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
505 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
507 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
508 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
510 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
511 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
513 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
514 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
516 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
517 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
519 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
520 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
522 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
523 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
525 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
526 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
528 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
529 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
531 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
532 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
534 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
535 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
537 typedef typename P::E E;
538 private:
539 dysymtab_command fields;
544 // mach-o two-level hints load command
546 template <typename P>
547 class macho_twolevel_hints_command {
548 public:
549 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
550 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
552 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
553 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
555 uint32_t offset() const INLINE { return E::get32(fields.offset); }
556 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
558 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
559 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
561 typedef typename P::E E;
562 private:
563 twolevel_hints_command fields;
568 // mach-o threads load command
570 template <typename P>
571 class macho_thread_command {
572 public:
573 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
574 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
576 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
577 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
579 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
580 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
582 uint32_t count() const INLINE { return E::get32(fields_count); }
583 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
585 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
586 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
588 typedef typename P::E E;
589 typedef typename P::uint_t pint_t;
590 private:
591 struct thread_command fields;
592 uint32_t fields_flavor;
593 uint32_t fields_count;
594 pint_t thread_registers[1];
599 // mach-o misc data
601 template <typename P>
602 class macho_linkedit_data_command {
603 public:
604 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
605 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
607 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
608 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
610 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
611 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
613 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
614 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
617 typedef typename P::E E;
618 private:
619 linkedit_data_command fields;
624 // mach-o symbol table entry
626 template <typename P> struct macho_nlist_content {};
627 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
628 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
629 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
630 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
632 template <typename P>
633 class macho_nlist {
634 public:
635 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
636 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
638 uint8_t n_type() const INLINE { return entry.fields.n_type; }
639 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
641 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
642 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
644 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
645 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
647 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
648 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
650 typedef typename P::E E;
651 private:
652 macho_nlist_content<P> entry;
658 // mach-o relocation info
660 template <typename P>
661 class macho_relocation_info {
662 public:
663 uint32_t r_address() const INLINE { return E::get32(address); }
664 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
666 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
667 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
669 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
670 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
672 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
673 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
675 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
676 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
678 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
679 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
681 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
683 typedef typename P::E E;
684 private:
685 uint32_t address;
686 uint32_t other;
691 // mach-o scattered relocation info
692 // The bit fields are always in big-endian order (see mach-o/reloc.h)
694 template <typename P>
695 class macho_scattered_relocation_info {
696 public:
697 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
698 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
700 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
701 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
703 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
704 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
706 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
707 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
709 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
710 void set_r_address(uint32_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
712 uint32_t r_value() const INLINE { return E::get32(value); }
713 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
715 uint32_t r_other() const INLINE { return other; }
717 typedef typename P::E E;
718 private:
719 uint32_t other;
720 uint32_t value;
725 // mach-o file header
727 template <typename P> struct macho_header_content {};
728 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
729 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
730 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
731 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
733 template <typename P>
734 class macho_header {
735 public:
736 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
737 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
739 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
740 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
742 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
743 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
745 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
746 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
748 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
749 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
751 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
752 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
754 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
755 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
757 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
758 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
760 const macho_segment_command<P>* getSegment(const char *segname) const
762 const macho_load_command<P>* cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
763 uint32_t cmd_count = this->ncmds();
764 const macho_load_command<P>* cmd = cmds;
765 for (uint32_t i = 0; i < cmd_count; ++i) {
766 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
767 const macho_segment_command<P>* segcmd = (macho_segment_command<P>*)cmd;
768 if (0 == strncmp(segname, segcmd->segname(), 16)) {
769 return segcmd;
772 cmd = (macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
774 return NULL;
777 const macho_section<P>* getSection(const char *segname, const char *sectname) const
779 const macho_segment_command<P>* segcmd = getSegment(segname);
780 if (!segcmd) return NULL;
782 const macho_section<P>* sectcmd = (macho_section<P>*)(segcmd+1);
783 uint32_t section_count = segcmd->nsects();
784 for (uint32_t j = 0; j < section_count; ++j) {
785 if (0 == ::strncmp(sectcmd[j].sectname(), sectname, 16)) {
786 return sectcmd+j;
790 return NULL;
793 typedef typename P::E E;
794 private:
795 macho_header_content<P> header;
801 // compressed dyld info load command
803 template <typename P>
804 class macho_dyld_info_command {
805 public:
806 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
807 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
809 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
810 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
812 uint32_t rebase_off() const INLINE { return E::get32(fields.rebase_off); }
813 void set_rebase_off(uint32_t value) INLINE { E::set32(fields.rebase_off, value); }
815 uint32_t rebase_size() const INLINE { return E::get32(fields.rebase_size); }
816 void set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value); }
818 uint32_t bind_off() const INLINE { return E::get32(fields.bind_off); }
819 void set_bind_off(uint32_t value) INLINE { E::set32(fields.bind_off, value); }
821 uint32_t bind_size() const INLINE { return E::get32(fields.bind_size); }
822 void set_bind_size(uint32_t value) INLINE { E::set32(fields.bind_size, value); }
824 uint32_t weak_bind_off() const INLINE { return E::get32(fields.weak_bind_off); }
825 void set_weak_bind_off(uint32_t value) INLINE { E::set32(fields.weak_bind_off, value); }
827 uint32_t weak_bind_size() const INLINE { return E::get32(fields.weak_bind_size); }
828 void set_weak_bind_size(uint32_t value) INLINE { E::set32(fields.weak_bind_size, value); }
830 uint32_t lazy_bind_off() const INLINE { return E::get32(fields.lazy_bind_off); }
831 void set_lazy_bind_off(uint32_t value) INLINE { E::set32(fields.lazy_bind_off, value); }
833 uint32_t lazy_bind_size() const INLINE { return E::get32(fields.lazy_bind_size); }
834 void set_lazy_bind_size(uint32_t value) INLINE { E::set32(fields.lazy_bind_size, value); }
836 uint32_t export_off() const INLINE { return E::get32(fields.export_off); }
837 void set_export_off(uint32_t value) INLINE { E::set32(fields.export_off, value); }
839 uint32_t export_size() const INLINE { return E::get32(fields.export_size); }
840 void set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value); }
843 typedef typename P::E E;
844 private:
845 dyld_info_command fields;
848 #ifndef NO_ULEB
849 inline uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end) {
850 uint64_t result = 0;
851 int bit = 0;
852 do {
853 if (p == end)
854 throw "malformed uleb128 extends beyond trie";
856 uint64_t slice = *p & 0x7f;
858 if (bit >= 64 || slice << bit >> bit != slice)
859 throw "uleb128 too big for 64-bits";
860 else {
861 result |= (slice << bit);
862 bit += 7;
865 while (*p++ & 0x80);
866 return result;
870 static int64_t read_sleb128(const uint8_t*& p, const uint8_t* end)
872 int64_t result = 0;
873 int bit = 0;
874 uint8_t byte;
875 do {
876 if (p == end)
877 throw "malformed sleb128";
878 byte = *p++;
879 result |= ((byte & 0x7f) << bit);
880 bit += 7;
881 } while (byte & 0x80);
882 // sign extend negative numbers
883 if ( (byte & 0x40) != 0 )
884 result |= (-1LL) << bit;
885 return result;
888 #endif
891 #endif // __MACH_O_FILE_ABSTRACTION__