1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
24 #ifndef __MACH_O_FILE_ABSTRACTION__
25 #define __MACH_O_FILE_ABSTRACTION__
27 #include <mach-o/loader.h>
28 #include <mach-o/nlist.h>
29 #include <mach-o/reloc.h>
30 #include <mach/machine.h>
32 // suport older versions of mach-o/loader.h
36 uint32_t cmd; /* LC_UUID */
37 uint32_t cmdsize; /* sizeof(struct uuid_command) */
38 uint8_t uuid[16]; /* the 128-bit uuid */
42 #ifndef S_16BYTE_LITERALS
43 #define S_16BYTE_LITERALS 0xE
47 #include "FileAbstraction.hpp"
48 #include "Architectures.hpp"
50 // utility to pair together a cpu-type and cpu-sub-type
56 ArchPair(uint32_t cputype, uint32_t cpusubtype) : arch(cputype), subtype(cpusubtype) {}
58 bool operator<(const ArchPair& other) const {
59 if ( this->arch != other.arch )
60 return (this->arch < other.arch);
61 return (this->subtype < other.subtype);
67 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
71 // mach-o load command
74 class macho_load_command {
76 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
77 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
79 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
80 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
82 typedef typename P::E E;
89 // mach-o segment load command
91 template <typename P> struct macho_segment_content {};
92 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
93 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
94 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
95 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
98 class macho_segment_command {
100 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
101 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
103 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
104 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
106 const char* segname() const INLINE { return segment.fields.segname; }
107 void set_segname(const char* value) INLINE { strncpy(segment.fields.segname, value, 16); }
109 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
110 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
112 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
113 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
115 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
116 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
118 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
119 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
121 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
122 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
124 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
125 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
127 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
128 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
130 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
131 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
134 CMD = macho_segment_content<P>::CMD
137 typedef typename P::E E;
139 macho_segment_content<P> segment;
146 template <typename P> struct macho_section_content {};
147 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
148 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
149 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
150 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
152 template <typename P>
153 class macho_section {
155 const char* sectname() const INLINE { return section.fields.sectname; }
156 void set_sectname(const char* value) INLINE { strncpy(section.fields.sectname, value, 16); }
158 const char* segname() const INLINE { return section.fields.segname; }
159 void set_segname(const char* value) INLINE { strncpy(section.fields.segname, value, 16); }
161 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
162 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
164 uint64_t size() const INLINE { return P::getP(section.fields.size); }
165 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
167 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
168 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
170 uint32_t align() const INLINE { return E::get32(section.fields.align); }
171 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
173 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
174 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
176 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
177 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
179 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
180 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
182 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
183 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
185 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
186 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
188 typedef typename P::E E;
190 macho_section_content<P> section;
195 // mach-o dylib load command
197 template <typename P>
198 class macho_dylib_command {
200 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
201 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
203 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
204 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
206 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
207 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
209 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
210 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
212 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
213 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
215 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
216 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
218 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
219 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
221 typedef typename P::E E;
223 dylib_command fields;
228 // mach-o dylinker load command
230 template <typename P>
231 class macho_dylinker_command {
233 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
234 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
236 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
237 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
239 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
240 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
242 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
243 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
245 typedef typename P::E E;
247 dylinker_command fields;
252 // mach-o sub_framework load command
254 template <typename P>
255 class macho_sub_framework_command {
257 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
258 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
260 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
261 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
263 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
264 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
266 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
267 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
269 typedef typename P::E E;
271 sub_framework_command fields;
276 // mach-o sub_client load command
278 template <typename P>
279 class macho_sub_client_command {
281 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
282 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
284 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
285 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
287 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
288 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
290 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
291 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
293 typedef typename P::E E;
295 sub_client_command fields;
300 // mach-o sub_umbrella load command
302 template <typename P>
303 class macho_sub_umbrella_command {
305 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
306 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
308 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
309 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
311 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
312 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
314 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
315 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
317 typedef typename P::E E;
319 sub_umbrella_command fields;
324 // mach-o sub_library load command
326 template <typename P>
327 class macho_sub_library_command {
329 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
330 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
332 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
333 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
335 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
336 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
338 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
339 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
341 typedef typename P::E E;
343 sub_library_command fields;
348 // mach-o uuid load command
350 template <typename P>
351 class macho_uuid_command {
353 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
354 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
356 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
357 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
359 const uint8_t* uuid() const INLINE { return fields.uuid; }
360 void set_uuid(uint8_t value[16]) INLINE { memcpy(&fields.uuid, value, 16); }
362 typedef typename P::E E;
369 // mach-o routines load command
371 template <typename P> struct macho_routines_content {};
372 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
373 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
374 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
375 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
377 template <typename P>
378 class macho_routines_command {
380 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
381 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
383 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
384 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
386 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
387 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
389 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
390 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
392 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
393 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
395 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
396 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
398 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
399 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
401 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
402 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
404 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
405 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
407 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
408 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
410 typedef typename P::E E;
412 CMD = macho_routines_content<P>::CMD
415 macho_routines_content<P> routines;
420 // mach-o symbol table load command
422 template <typename P>
423 class macho_symtab_command {
425 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
426 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
428 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
429 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
431 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
432 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
434 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
435 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
437 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
438 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
440 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
441 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
444 typedef typename P::E E;
446 symtab_command fields;
451 // mach-o dynamic symbol table load command
453 template <typename P>
454 class macho_dysymtab_command {
456 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
457 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
459 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
460 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
462 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
463 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
465 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
466 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
468 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
469 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
471 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
472 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
474 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
475 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
477 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
478 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
480 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
481 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
483 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
484 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
486 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
487 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
489 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
490 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
492 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
493 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
495 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
496 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
498 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
499 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
501 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
502 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
504 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
505 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
507 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
508 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
510 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
511 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
513 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
514 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
516 typedef typename P::E E;
518 dysymtab_command fields;
523 // mach-o two-level hints load command
525 template <typename P>
526 class macho_twolevel_hints_command {
528 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
529 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
531 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
532 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
534 uint32_t offset() const INLINE { return E::get32(fields.offset); }
535 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
537 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
538 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
540 typedef typename P::E E;
542 twolevel_hints_command fields;
547 // mach-o threads load command
549 template <typename P>
550 class macho_thread_command {
552 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
553 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
555 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
556 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
558 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
559 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
561 uint32_t count() const INLINE { return E::get32(fields_count); }
562 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
564 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
565 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
567 typedef typename P::E E;
568 typedef typename P::uint_t pint_t;
570 struct thread_command fields;
571 uint32_t fields_flavor;
572 uint32_t fields_count;
573 pint_t thread_registers[1];
580 template <typename P>
581 class macho_linkedit_data_command {
583 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
584 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
586 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
587 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
589 uint32_t dataoff() const INLINE { return E::get32(fields.dataoff); }
590 void set_dataoff(uint32_t value) INLINE { E::set32(fields.dataoff, value); }
592 uint32_t datasize() const INLINE { return E::get32(fields.datasize); }
593 void set_datasize(uint32_t value)INLINE { E::set32(fields.datasize, value); }
596 typedef typename P::E E;
598 linkedit_data_command fields;
603 // mach-o symbol table entry
605 template <typename P> struct macho_nlist_content {};
606 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
607 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
608 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
609 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
611 template <typename P>
614 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
615 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
617 uint8_t n_type() const INLINE { return entry.fields.n_type; }
618 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
620 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
621 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
623 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
624 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
626 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
627 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
629 typedef typename P::E E;
631 macho_nlist_content<P> entry;
637 // mach-o relocation info
639 template <typename P>
640 class macho_relocation_info {
642 uint32_t r_address() const INLINE { return E::get32(address); }
643 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
645 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
646 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
648 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
649 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
651 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
652 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
654 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
655 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
657 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
658 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
660 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
662 typedef typename P::E E;
670 // mach-o scattered relocation info
671 // The bit fields are always in big-endian order (see mach-o/reloc.h)
673 template <typename P>
674 class macho_scattered_relocation_info {
676 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
677 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
679 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
680 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
682 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
683 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
685 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
686 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
688 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
689 void set_r_address(uint32_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
691 uint32_t r_value() const INLINE { return E::get32(value); }
692 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
694 uint32_t r_other() const INLINE { return other; }
696 typedef typename P::E E;
704 // mach-o file header
706 template <typename P> struct macho_header_content {};
707 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
708 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
709 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
710 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
712 template <typename P>
715 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
716 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
718 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
719 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
721 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
722 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
724 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
725 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
727 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
728 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
730 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
731 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
733 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
734 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
736 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
737 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
739 const macho_segment_command<P>* getSegment(const char *segname) const
741 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)this + sizeof(macho_header<P>));
742 const uint32_t cmd_count = this->ncmds();
743 const macho_load_command<P>* cmd = cmds;
744 for (uint32_t i = 0; i < cmd_count; ++i) {
745 if ( cmd->cmd() == macho_segment_command<P>::CMD ) {
746 const macho_segment_command<P>* segcmd =
747 (macho_segment_command<P>*)cmd;
748 if (0 == strncmp(segname, segcmd->segname(), 16)) {
752 cmd = (const macho_load_command<P>*)(((uint8_t*)cmd)+cmd->cmdsize());
757 const macho_section<P>* getSection(const char *segname, const char *sectname) const
759 const macho_segment_command<P>* const segcmd = getSegment(segname);
760 if (!segcmd) return NULL;
762 const macho_section<P>* sectcmd = (macho_section<P>*)(segcmd+1);
763 const uint32_t section_count = segcmd->nsects();
764 for (uint32_t j = 0; j < section_count; ++j) {
765 if (0 == ::strncmp(sectcmd[j].sectname(), sectname, 16)) {
773 typedef typename P::E E;
775 macho_header_content<P> header;
781 // compressed dyld info load command
783 template <typename P>
784 class macho_dyld_info_command {
786 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
787 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
789 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
790 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
792 uint32_t rebase_off() const INLINE { return E::get32(fields.rebase_off); }
793 void set_rebase_off(uint32_t value) INLINE { E::set32(fields.rebase_off, value); }
795 uint32_t rebase_size() const INLINE { return E::get32(fields.rebase_size); }
796 void set_rebase_size(uint32_t value) INLINE { E::set32(fields.rebase_size, value); }
798 uint32_t bind_off() const INLINE { return E::get32(fields.bind_off); }
799 void set_bind_off(uint32_t value) INLINE { E::set32(fields.bind_off, value); }
801 uint32_t bind_size() const INLINE { return E::get32(fields.bind_size); }
802 void set_bind_size(uint32_t value) INLINE { E::set32(fields.bind_size, value); }
804 uint32_t weak_bind_off() const INLINE { return E::get32(fields.weak_bind_off); }
805 void set_weak_bind_off(uint32_t value) INLINE { E::set32(fields.weak_bind_off, value); }
807 uint32_t weak_bind_size() const INLINE { return E::get32(fields.weak_bind_size); }
808 void set_weak_bind_size(uint32_t value) INLINE { E::set32(fields.weak_bind_size, value); }
810 uint32_t lazy_bind_off() const INLINE { return E::get32(fields.lazy_bind_off); }
811 void set_lazy_bind_off(uint32_t value) INLINE { E::set32(fields.lazy_bind_off, value); }
813 uint32_t lazy_bind_size() const INLINE { return E::get32(fields.lazy_bind_size); }
814 void set_lazy_bind_size(uint32_t value) INLINE { E::set32(fields.lazy_bind_size, value); }
816 uint32_t export_off() const INLINE { return E::get32(fields.export_off); }
817 void set_export_off(uint32_t value) INLINE { E::set32(fields.export_off, value); }
819 uint32_t export_size() const INLINE { return E::get32(fields.export_size); }
820 void set_export_size(uint32_t value) INLINE { E::set32(fields.export_size, value); }
823 typedef typename P::E E;
825 dyld_info_command fields;
829 inline uint64_t read_uleb128(const uint8_t*& p, const uint8_t* end) {
834 throw "malformed uleb128 extends beyond trie";
836 uint64_t slice = *p & 0x7f;
838 if (bit >= 64 || slice << bit >> bit != slice)
839 throw "uleb128 too big for 64-bits";
841 result |= (slice << bit);
850 static int64_t read_sleb128(const uint8_t*& p, const uint8_t* end)
857 throw "malformed sleb128";
859 result |= ((byte & 0x7f) << bit);
861 } while (byte & 0x80);
862 // sign extend negative numbers
863 if ( (byte & 0x40) != 0 )
864 result |= (-1LL) << bit;
871 #endif // __MACH_O_FILE_ABSTRACTION__