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