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