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