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