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