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