]> git.saurik.com Git - apple/ld64.git/blob - src/MachOFileAbstraction.hpp
6110e80b8afbb530abac3bb9e613b89a1c623d84
[apple/ld64.git] / src / 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
43 #include "FileAbstraction.hpp"
44 #include "Architectures.hpp"
45
46
47
48 //
49 // This abstraction layer makes every mach-o file look like a 64-bit mach-o file with native endianness
50 //
51
52
53
54 //
55 // mach-o file header
56 //
57 template <typename P> struct macho_header_content {};
58 template <> struct macho_header_content<Pointer32<BigEndian> > { mach_header fields; };
59 template <> struct macho_header_content<Pointer64<BigEndian> > { mach_header_64 fields; };
60 template <> struct macho_header_content<Pointer32<LittleEndian> > { mach_header fields; };
61 template <> struct macho_header_content<Pointer64<LittleEndian> > { mach_header_64 fields; };
62
63 template <typename P>
64 class macho_header {
65 public:
66 uint32_t magic() const INLINE { return E::get32(header.fields.magic); }
67 void set_magic(uint32_t value) INLINE { E::set32(header.fields.magic, value); }
68
69 uint32_t cputype() const INLINE { return E::get32(header.fields.cputype); }
70 void set_cputype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cputype, value); }
71
72 uint32_t cpusubtype() const INLINE { return E::get32(header.fields.cpusubtype); }
73 void set_cpusubtype(uint32_t value) INLINE { E::set32((uint32_t&)header.fields.cpusubtype, value); }
74
75 uint32_t filetype() const INLINE { return E::get32(header.fields.filetype); }
76 void set_filetype(uint32_t value) INLINE { E::set32(header.fields.filetype, value); }
77
78 uint32_t ncmds() const INLINE { return E::get32(header.fields.ncmds); }
79 void set_ncmds(uint32_t value) INLINE { E::set32(header.fields.ncmds, value); }
80
81 uint32_t sizeofcmds() const INLINE { return E::get32(header.fields.sizeofcmds); }
82 void set_sizeofcmds(uint32_t value) INLINE { E::set32(header.fields.sizeofcmds, value); }
83
84 uint32_t flags() const INLINE { return E::get32(header.fields.flags); }
85 void set_flags(uint32_t value) INLINE { E::set32(header.fields.flags, value); }
86
87 uint32_t reserved() const INLINE { return E::get32(header.fields.reserved); }
88 void set_reserved(uint32_t value) INLINE { E::set32(header.fields.reserved, value); }
89
90 typedef typename P::E E;
91 private:
92 macho_header_content<P> header;
93 };
94
95
96 //
97 // mach-o load command
98 //
99 template <typename P>
100 class macho_load_command {
101 public:
102 uint32_t cmd() const INLINE { return E::get32(command.cmd); }
103 void set_cmd(uint32_t value) INLINE { E::set32(command.cmd, value); }
104
105 uint32_t cmdsize() const INLINE { return E::get32(command.cmdsize); }
106 void set_cmdsize(uint32_t value) INLINE { E::set32(command.cmdsize, value); }
107
108 typedef typename P::E E;
109 private:
110 load_command command;
111 };
112
113
114 //
115 // mach-o segment load command
116 //
117 template <typename P> struct macho_segment_content {};
118 template <> struct macho_segment_content<Pointer32<BigEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
119 template <> struct macho_segment_content<Pointer64<BigEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
120 template <> struct macho_segment_content<Pointer32<LittleEndian> > { segment_command fields; enum { CMD = LC_SEGMENT }; };
121 template <> struct macho_segment_content<Pointer64<LittleEndian> > { segment_command_64 fields; enum { CMD = LC_SEGMENT_64 }; };
122
123 template <typename P>
124 class macho_segment_command {
125 public:
126 uint32_t cmd() const INLINE { return E::get32(segment.fields.cmd); }
127 void set_cmd(uint32_t value) INLINE { E::set32(segment.fields.cmd, value); }
128
129 uint32_t cmdsize() const INLINE { return E::get32(segment.fields.cmdsize); }
130 void set_cmdsize(uint32_t value) INLINE { E::set32(segment.fields.cmdsize, value); }
131
132 const char* segname() const INLINE { return segment.fields.segname; }
133 void set_segname(const char* value) INLINE { memcpy(&segment.fields.segname, value, 16); }
134
135 uint64_t vmaddr() const INLINE { return P::getP(segment.fields.vmaddr); }
136 void set_vmaddr(uint64_t value) INLINE { P::setP(segment.fields.vmaddr, value); }
137
138 uint64_t vmsize() const INLINE { return P::getP(segment.fields.vmsize); }
139 void set_vmsize(uint64_t value) INLINE { P::setP(segment.fields.vmsize, value); }
140
141 uint64_t fileoff() const INLINE { return P::getP(segment.fields.fileoff); }
142 void set_fileoff(uint64_t value) INLINE { P::setP(segment.fields.fileoff, value); }
143
144 uint64_t filesize() const INLINE { return P::getP(segment.fields.filesize); }
145 void set_filesize(uint64_t value) INLINE { P::setP(segment.fields.filesize, value); }
146
147 uint32_t maxprot() const INLINE { return E::get32(segment.fields.maxprot); }
148 void set_maxprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.maxprot, value); }
149
150 uint32_t initprot() const INLINE { return E::get32(segment.fields.initprot); }
151 void set_initprot(uint32_t value) INLINE { E::set32((uint32_t&)segment.fields.initprot, value); }
152
153 uint32_t nsects() const INLINE { return E::get32(segment.fields.nsects); }
154 void set_nsects(uint32_t value) INLINE { E::set32(segment.fields.nsects, value); }
155
156 uint32_t flags() const INLINE { return E::get32(segment.fields.flags); }
157 void set_flags(uint32_t value) INLINE { E::set32(segment.fields.flags, value); }
158
159 enum {
160 CMD = macho_segment_content<P>::CMD
161 };
162
163 typedef typename P::E E;
164 private:
165 macho_segment_content<P> segment;
166 };
167
168
169 //
170 // mach-o section
171 //
172 template <typename P> struct macho_section_content {};
173 template <> struct macho_section_content<Pointer32<BigEndian> > { section fields; };
174 template <> struct macho_section_content<Pointer64<BigEndian> > { section_64 fields; };
175 template <> struct macho_section_content<Pointer32<LittleEndian> > { section fields; };
176 template <> struct macho_section_content<Pointer64<LittleEndian> > { section_64 fields; };
177
178 template <typename P>
179 class macho_section {
180 public:
181 const char* sectname() const INLINE { return section.fields.sectname; }
182 void set_sectname(const char* value) INLINE { memcpy(&section.fields.sectname, value, 16); }
183
184 const char* segname() const INLINE { return section.fields.segname; }
185 void set_segname(const char* value) INLINE { memcpy(&section.fields.segname, value, 16); }
186
187 uint64_t addr() const INLINE { return P::getP(section.fields.addr); }
188 void set_addr(uint64_t value) INLINE { P::setP(section.fields.addr, value); }
189
190 uint64_t size() const INLINE { return P::getP(section.fields.size); }
191 void set_size(uint64_t value) INLINE { P::setP(section.fields.size, value); }
192
193 uint32_t offset() const INLINE { return E::get32(section.fields.offset); }
194 void set_offset(uint32_t value) INLINE { E::set32(section.fields.offset, value); }
195
196 uint32_t align() const INLINE { return E::get32(section.fields.align); }
197 void set_align(uint32_t value) INLINE { E::set32(section.fields.align, value); }
198
199 uint32_t reloff() const INLINE { return E::get32(section.fields.reloff); }
200 void set_reloff(uint32_t value) INLINE { E::set32(section.fields.reloff, value); }
201
202 uint32_t nreloc() const INLINE { return E::get32(section.fields.nreloc); }
203 void set_nreloc(uint32_t value) INLINE { E::set32(section.fields.nreloc, value); }
204
205 uint32_t flags() const INLINE { return E::get32(section.fields.flags); }
206 void set_flags(uint32_t value) INLINE { E::set32(section.fields.flags, value); }
207
208 uint32_t reserved1() const INLINE { return E::get32(section.fields.reserved1); }
209 void set_reserved1(uint32_t value) INLINE { E::set32(section.fields.reserved1, value); }
210
211 uint32_t reserved2() const INLINE { return E::get32(section.fields.reserved2); }
212 void set_reserved2(uint32_t value) INLINE { E::set32(section.fields.reserved2, value); }
213
214 typedef typename P::E E;
215 private:
216 macho_section_content<P> section;
217 };
218
219
220 //
221 // mach-o dylib load command
222 //
223 template <typename P>
224 class macho_dylib_command {
225 public:
226 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
227 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
228
229 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
230 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
231
232 uint32_t name_offset() const INLINE { return E::get32(fields.dylib.name.offset); }
233 void set_name_offset(uint32_t value) INLINE { E::set32(fields.dylib.name.offset, value); }
234
235 uint32_t timestamp() const INLINE { return E::get32(fields.dylib.timestamp); }
236 void set_timestamp(uint32_t value) INLINE { E::set32(fields.dylib.timestamp, value); }
237
238 uint32_t current_version() const INLINE { return E::get32(fields.dylib.current_version); }
239 void set_current_version(uint32_t value) INLINE { E::set32(fields.dylib.current_version, value); }
240
241 uint32_t compatibility_version() const INLINE { return E::get32(fields.dylib.compatibility_version); }
242 void set_compatibility_version(uint32_t value) INLINE { E::set32(fields.dylib.compatibility_version, value); }
243
244 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
245 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
246
247 typedef typename P::E E;
248 private:
249 dylib_command fields;
250 };
251
252
253 //
254 // mach-o dylinker load command
255 //
256 template <typename P>
257 class macho_dylinker_command {
258 public:
259 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
260 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
261
262 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
263 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
264
265 uint32_t name_offset() const INLINE { return E::get32(fields.name.offset); }
266 void set_name_offset(uint32_t value) INLINE { E::set32(fields.name.offset, value); }
267
268 const char* name() const INLINE { return (const char*)&fields + name_offset(); }
269 void set_name_offset() INLINE { set_name_offset(sizeof(fields)); }
270
271 typedef typename P::E E;
272 private:
273 dylinker_command fields;
274 };
275
276
277 //
278 // mach-o sub_framework load command
279 //
280 template <typename P>
281 class macho_sub_framework_command {
282 public:
283 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
284 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
285
286 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
287 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
288
289 uint32_t umbrella_offset() const INLINE { return E::get32(fields.umbrella.offset); }
290 void set_umbrella_offset(uint32_t value) INLINE { E::set32(fields.umbrella.offset, value); }
291
292 const char* umbrella() const INLINE { return (const char*)&fields + umbrella_offset(); }
293 void set_umbrella_offset() INLINE { set_umbrella_offset(sizeof(fields)); }
294
295 typedef typename P::E E;
296 private:
297 sub_framework_command fields;
298 };
299
300
301 //
302 // mach-o sub_client load command
303 //
304 template <typename P>
305 class macho_sub_client_command {
306 public:
307 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
308 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
309
310 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
311 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
312
313 uint32_t client_offset() const INLINE { return E::get32(fields.client.offset); }
314 void set_client_offset(uint32_t value) INLINE { E::set32(fields.client.offset, value); }
315
316 const char* client() const INLINE { return (const char*)&fields + client_offset(); }
317 void set_client_offset() INLINE { set_client_offset(sizeof(fields)); }
318
319 typedef typename P::E E;
320 private:
321 sub_client_command fields;
322 };
323
324
325 //
326 // mach-o sub_umbrella load command
327 //
328 template <typename P>
329 class macho_sub_umbrella_command {
330 public:
331 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
332 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
333
334 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
335 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
336
337 uint32_t sub_umbrella_offset() const INLINE { return E::get32(fields.sub_umbrella.offset); }
338 void set_sub_umbrella_offset(uint32_t value) INLINE { E::set32(fields.sub_umbrella.offset, value); }
339
340 const char* sub_umbrella() const INLINE { return (const char*)&fields + sub_umbrella_offset(); }
341 void set_sub_umbrella_offset() INLINE { set_sub_umbrella_offset(sizeof(fields)); }
342
343 typedef typename P::E E;
344 private:
345 sub_umbrella_command fields;
346 };
347
348
349 //
350 // mach-o sub_library load command
351 //
352 template <typename P>
353 class macho_sub_library_command {
354 public:
355 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
356 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
357
358 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
359 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
360
361 uint32_t sub_library_offset() const INLINE { return E::get32(fields.sub_library.offset); }
362 void set_sub_library_offset(uint32_t value) INLINE { E::set32(fields.sub_library.offset, value); }
363
364 const char* sub_library() const INLINE { return (const char*)&fields + sub_library_offset(); }
365 void set_sub_library_offset() INLINE { set_sub_library_offset(sizeof(fields)); }
366
367 typedef typename P::E E;
368 private:
369 sub_library_command fields;
370 };
371
372
373 //
374 // mach-o uuid load command
375 //
376 template <typename P>
377 class macho_uuid_command {
378 public:
379 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
380 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
381
382 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
383 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
384
385 const uint8_t* uuid() const INLINE { return fields.uuid; }
386 void set_uuid(uint8_t uuid[16]) INLINE { memcpy(&fields.uuid, uuid, 16); }
387
388 typedef typename P::E E;
389 private:
390 uuid_command fields;
391 };
392
393
394 //
395 // mach-o routines load command
396 //
397 template <typename P> struct macho_routines_content {};
398 template <> struct macho_routines_content<Pointer32<BigEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
399 template <> struct macho_routines_content<Pointer64<BigEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
400 template <> struct macho_routines_content<Pointer32<LittleEndian> > { routines_command fields; enum { CMD = LC_ROUTINES }; };
401 template <> struct macho_routines_content<Pointer64<LittleEndian> > { routines_command_64 fields; enum { CMD = LC_ROUTINES_64 }; };
402
403 template <typename P>
404 class macho_routines_command {
405 public:
406 uint32_t cmd() const INLINE { return E::get32(routines.fields.cmd); }
407 void set_cmd(uint32_t value) INLINE { E::set32(routines.fields.cmd, value); }
408
409 uint32_t cmdsize() const INLINE { return E::get32(routines.fields.cmdsize); }
410 void set_cmdsize(uint32_t value) INLINE { E::set32(routines.fields.cmdsize, value); }
411
412 uint64_t init_address() const INLINE { return P::getP(routines.fields.init_address); }
413 void set_init_address(uint64_t value) INLINE { P::setP(routines.fields.init_address, value); }
414
415 uint64_t init_module() const INLINE { return P::getP(routines.fields.init_module); }
416 void set_init_module(uint64_t value) INLINE { P::setP(routines.fields.init_module, value); }
417
418 uint64_t reserved1() const INLINE { return P::getP(routines.fields.reserved1); }
419 void set_reserved1(uint64_t value) INLINE { P::setP(routines.fields.reserved1, value); }
420
421 uint64_t reserved2() const INLINE { return P::getP(routines.fields.reserved2); }
422 void set_reserved2(uint64_t value) INLINE { P::setP(routines.fields.reserved2, value); }
423
424 uint64_t reserved3() const INLINE { return P::getP(routines.fields.reserved3); }
425 void set_reserved3(uint64_t value) INLINE { P::setP(routines.fields.reserved3, value); }
426
427 uint64_t reserved4() const INLINE { return P::getP(routines.fields.reserved4); }
428 void set_reserved4(uint64_t value) INLINE { P::setP(routines.fields.reserved4, value); }
429
430 uint64_t reserved5() const INLINE { return P::getP(routines.fields.reserved5); }
431 void set_reserved5(uint64_t value) INLINE { P::setP(routines.fields.reserved5, value); }
432
433 uint64_t reserved6() const INLINE { return P::getP(routines.fields.reserved6); }
434 void set_reserved6(uint64_t value) INLINE { P::setP(routines.fields.reserved6, value); }
435
436 typedef typename P::E E;
437 enum {
438 CMD = macho_routines_content<P>::CMD
439 };
440 private:
441 macho_routines_content<P> routines;
442 };
443
444
445 //
446 // mach-o symbol table load command
447 //
448 template <typename P>
449 class macho_symtab_command {
450 public:
451 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
452 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
453
454 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
455 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
456
457 uint32_t symoff() const INLINE { return E::get32(fields.symoff); }
458 void set_symoff(uint32_t value) INLINE { E::set32(fields.symoff, value); }
459
460 uint32_t nsyms() const INLINE { return E::get32(fields.nsyms); }
461 void set_nsyms(uint32_t value) INLINE { E::set32(fields.nsyms, value); }
462
463 uint32_t stroff() const INLINE { return E::get32(fields.stroff); }
464 void set_stroff(uint32_t value) INLINE { E::set32(fields.stroff, value); }
465
466 uint32_t strsize() const INLINE { return E::get32(fields.strsize); }
467 void set_strsize(uint32_t value) INLINE { E::set32(fields.strsize, value); }
468
469
470 typedef typename P::E E;
471 private:
472 symtab_command fields;
473 };
474
475
476 //
477 // mach-o dynamic symbol table load command
478 //
479 template <typename P>
480 class macho_dysymtab_command {
481 public:
482 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
483 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
484
485 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
486 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
487
488 uint32_t ilocalsym() const INLINE { return E::get32(fields.ilocalsym); }
489 void set_ilocalsym(uint32_t value) INLINE { E::set32(fields.ilocalsym, value); }
490
491 uint32_t nlocalsym() const INLINE { return E::get32(fields.nlocalsym); }
492 void set_nlocalsym(uint32_t value) INLINE { E::set32(fields.nlocalsym, value); }
493
494 uint32_t iextdefsym() const INLINE { return E::get32(fields.iextdefsym); }
495 void set_iextdefsym(uint32_t value) INLINE { E::set32(fields.iextdefsym, value); }
496
497 uint32_t nextdefsym() const INLINE { return E::get32(fields.nextdefsym); }
498 void set_nextdefsym(uint32_t value) INLINE { E::set32(fields.nextdefsym, value); }
499
500 uint32_t iundefsym() const INLINE { return E::get32(fields.iundefsym); }
501 void set_iundefsym(uint32_t value) INLINE { E::set32(fields.iundefsym, value); }
502
503 uint32_t nundefsym() const INLINE { return E::get32(fields.nundefsym); }
504 void set_nundefsym(uint32_t value) INLINE { E::set32(fields.nundefsym, value); }
505
506 uint32_t tocoff() const INLINE { return E::get32(fields.tocoff); }
507 void set_tocoff(uint32_t value) INLINE { E::set32(fields.tocoff, value); }
508
509 uint32_t ntoc() const INLINE { return E::get32(fields.ntoc); }
510 void set_ntoc(uint32_t value) INLINE { E::set32(fields.ntoc, value); }
511
512 uint32_t modtaboff() const INLINE { return E::get32(fields.modtaboff); }
513 void set_modtaboff(uint32_t value) INLINE { E::set32(fields.modtaboff, value); }
514
515 uint32_t nmodtab() const INLINE { return E::get32(fields.nmodtab); }
516 void set_nmodtab(uint32_t value) INLINE { E::set32(fields.nmodtab, value); }
517
518 uint32_t extrefsymoff() const INLINE { return E::get32(fields.extrefsymoff); }
519 void set_extrefsymoff(uint32_t value) INLINE { E::set32(fields.extrefsymoff, value); }
520
521 uint32_t nextrefsyms() const INLINE { return E::get32(fields.nextrefsyms); }
522 void set_nextrefsyms(uint32_t value) INLINE { E::set32(fields.nextrefsyms, value); }
523
524 uint32_t indirectsymoff() const INLINE { return E::get32(fields.indirectsymoff); }
525 void set_indirectsymoff(uint32_t value) INLINE { E::set32(fields.indirectsymoff, value); }
526
527 uint32_t nindirectsyms() const INLINE { return E::get32(fields.nindirectsyms); }
528 void set_nindirectsyms(uint32_t value) INLINE { E::set32(fields.nindirectsyms, value); }
529
530 uint32_t extreloff() const INLINE { return E::get32(fields.extreloff); }
531 void set_extreloff(uint32_t value) INLINE { E::set32(fields.extreloff, value); }
532
533 uint32_t nextrel() const INLINE { return E::get32(fields.nextrel); }
534 void set_nextrel(uint32_t value) INLINE { E::set32(fields.nextrel, value); }
535
536 uint32_t locreloff() const INLINE { return E::get32(fields.locreloff); }
537 void set_locreloff(uint32_t value) INLINE { E::set32(fields.locreloff, value); }
538
539 uint32_t nlocrel() const INLINE { return E::get32(fields.nlocrel); }
540 void set_nlocrel(uint32_t value) INLINE { E::set32(fields.nlocrel, value); }
541
542 typedef typename P::E E;
543 private:
544 dysymtab_command fields;
545 };
546
547
548 //
549 // mach-o two-level hints load command
550 //
551 template <typename P>
552 class macho_twolevel_hints_command {
553 public:
554 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
555 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
556
557 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
558 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
559
560 uint32_t offset() const INLINE { return E::get32(fields.offset); }
561 void set_offset(uint32_t value) INLINE { E::set32(fields.offset, value); }
562
563 uint32_t nhints() const INLINE { return E::get32(fields.nhints); }
564 void set_nhints(uint32_t value) INLINE { E::set32(fields.nhints, value); }
565
566 typedef typename P::E E;
567 private:
568 twolevel_hints_command fields;
569 };
570
571
572 //
573 // mach-o threads load command
574 //
575 template <typename P>
576 class macho_thread_command {
577 public:
578 uint32_t cmd() const INLINE { return E::get32(fields.cmd); }
579 void set_cmd(uint32_t value) INLINE { E::set32(fields.cmd, value); }
580
581 uint32_t cmdsize() const INLINE { return E::get32(fields.cmdsize); }
582 void set_cmdsize(uint32_t value) INLINE { E::set32(fields.cmdsize, value); }
583
584 uint32_t flavor() const INLINE { return E::get32(fields_flavor); }
585 void set_flavor(uint32_t value) INLINE { E::set32(fields_flavor, value); }
586
587 uint32_t count() const INLINE { return E::get32(fields_count); }
588 void set_count(uint32_t value) INLINE { E::set32(fields_count, value); }
589
590 uint64_t thread_register(uint32_t index) const INLINE { return P::getP(thread_registers[index]); }
591 void set_thread_register(uint32_t index, uint64_t value) INLINE { P::setP(thread_registers[index], value); }
592
593 typedef typename P::E E;
594 typedef typename P::uint_t pint_t;
595 private:
596 struct thread_command fields;
597 uint32_t fields_flavor;
598 uint32_t fields_count;
599 pint_t thread_registers[1];
600 };
601
602
603
604
605 //
606 // mach-o symbol table entry
607 //
608 template <typename P> struct macho_nlist_content {};
609 template <> struct macho_nlist_content<Pointer32<BigEndian> > { struct nlist fields; };
610 template <> struct macho_nlist_content<Pointer64<BigEndian> > { struct nlist_64 fields; };
611 template <> struct macho_nlist_content<Pointer32<LittleEndian> > { struct nlist fields; };
612 template <> struct macho_nlist_content<Pointer64<LittleEndian> > { struct nlist_64 fields; };
613
614 template <typename P>
615 class macho_nlist {
616 public:
617 uint32_t n_strx() const INLINE { return E::get32(entry.fields.n_un.n_strx); }
618 void set_n_strx(uint32_t value) INLINE { E::set32((uint32_t&)entry.fields.n_un.n_strx, value); }
619
620 uint8_t n_type() const INLINE { return entry.fields.n_type; }
621 void set_n_type(uint8_t value) INLINE { entry.fields.n_type = value; }
622
623 uint8_t n_sect() const INLINE { return entry.fields.n_sect; }
624 void set_n_sect(uint8_t value) INLINE { entry.fields.n_sect = value; }
625
626 uint16_t n_desc() const INLINE { return E::get16(entry.fields.n_desc); }
627 void set_n_desc(uint16_t value) INLINE { E::set16((uint16_t&)entry.fields.n_desc, value); }
628
629 uint64_t n_value() const INLINE { return P::getP(entry.fields.n_value); }
630 void set_n_value(uint64_t value) INLINE { P::setP(entry.fields.n_value, value); }
631
632 typedef typename P::E E;
633 private:
634 macho_nlist_content<P> entry;
635 };
636
637
638
639 //
640 // mach-o relocation info
641 //
642 template <typename P>
643 class macho_relocation_info {
644 public:
645 uint32_t r_address() const INLINE { return E::get32(address); }
646 void set_r_address(uint32_t value) INLINE { E::set32(address, value); }
647
648 uint32_t r_symbolnum() const INLINE { return E::getBits(other, 0, 24); }
649 void set_r_symbolnum(uint32_t value) INLINE { E::setBits(other, value, 0, 24); }
650
651 bool r_pcrel() const INLINE { return E::getBits(other, 24, 1); }
652 void set_r_pcrel(bool value) INLINE { E::setBits(other, value, 24, 1); }
653
654 uint8_t r_length() const INLINE { return E::getBits(other, 25, 2); }
655 void set_r_length(uint8_t value) INLINE { E::setBits(other, value, 25, 2); }
656
657 bool r_extern() const INLINE { return E::getBits(other, 27, 1); }
658 void set_r_extern(bool value) INLINE { E::setBits(other, value, 27, 1); }
659
660 uint8_t r_type() const INLINE { return E::getBits(other, 28, 4); }
661 void set_r_type(uint8_t value) INLINE { E::setBits(other, value, 28, 4); }
662
663 void set_r_length() INLINE { set_r_length((sizeof(typename P::uint_t)==8) ? 3 : 2); }
664
665 typedef typename P::E E;
666 private:
667 uint32_t address;
668 uint32_t other;
669 };
670
671
672 //
673 // mach-o scattered relocation info
674 // The bit fields are always in big-endian order (see mach-o/reloc.h)
675 //
676 template <typename P>
677 class macho_scattered_relocation_info {
678 public:
679 bool r_scattered() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 0, 1); }
680 void set_r_scattered(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 0, 1); E::set32(other, temp); }
681
682 bool r_pcrel() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 1, 1); }
683 void set_r_pcrel(bool x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 1, 1); E::set32(other, temp); }
684
685 uint8_t r_length() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 2, 2); }
686 void set_r_length(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 2, 2); E::set32(other, temp); }
687
688 uint8_t r_type() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 4, 4); }
689 void set_r_type(uint8_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 4, 4); E::set32(other, temp); }
690
691 uint32_t r_address() const INLINE { return BigEndian::getBitsRaw(E::get32(other), 8, 24); }
692 void set_r_address(uint32_t x) INLINE { uint32_t temp = E::get32(other); BigEndian::setBitsRaw(temp, x, 8, 24); E::set32(other, temp); }
693
694 uint32_t r_value() const INLINE { return E::get32(value); }
695 void set_r_value(uint32_t x) INLINE { E::set32(value, x); }
696
697 uint32_t r_other() const INLINE { return other; }
698
699 typedef typename P::E E;
700 private:
701 uint32_t other;
702 uint32_t value;
703 };
704
705
706
707
708
709
710 #endif // __MACH_O_FILE_ABSTRACTION__
711
712