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