1 //! Mach-O definitions.
2 //!
3 //! These definitions are independent of read/write support, although we do implement
4 //! some traits useful for those.
5 //!
6 //! This module is based heavily on macOS header files.
7 
8 #![allow(missing_docs)]
9 
10 use crate::endian::{BigEndian, Endian, U64Bytes, U16, U32, U64};
11 use crate::pod::Pod;
12 
13 // Definitions from "/usr/include/mach-o/machine.h".
14 
15 /*
16  * Capability bits used in the definition of cpu_type.
17  */
18 
19 /// mask for architecture bits
20 pub const CPU_ARCH_MASK: u32 = 0xff00_0000;
21 /// 64 bit ABI
22 pub const CPU_ARCH_ABI64: u32 = 0x0100_0000;
23 /// ABI for 64-bit hardware with 32-bit types; LP32
24 pub const CPU_ARCH_ABI64_32: u32 = 0x0200_0000;
25 
26 /*
27  *	Machine types known by all.
28  */
29 
30 pub const CPU_TYPE_ANY: u32 = !0;
31 
32 pub const CPU_TYPE_VAX: u32 = 1;
33 pub const CPU_TYPE_MC680X0: u32 = 6;
34 pub const CPU_TYPE_X86: u32 = 7;
35 pub const CPU_TYPE_X86_64: u32 = CPU_TYPE_X86 | CPU_ARCH_ABI64;
36 pub const CPU_TYPE_MIPS: u32 = 8;
37 pub const CPU_TYPE_MC98000: u32 = 10;
38 pub const CPU_TYPE_HPPA: u32 = 11;
39 pub const CPU_TYPE_ARM: u32 = 12;
40 pub const CPU_TYPE_ARM64: u32 = CPU_TYPE_ARM | CPU_ARCH_ABI64;
41 pub const CPU_TYPE_ARM64_32: u32 = CPU_TYPE_ARM | CPU_ARCH_ABI64_32;
42 pub const CPU_TYPE_MC88000: u32 = 13;
43 pub const CPU_TYPE_SPARC: u32 = 14;
44 pub const CPU_TYPE_I860: u32 = 15;
45 pub const CPU_TYPE_ALPHA: u32 = 16;
46 pub const CPU_TYPE_POWERPC: u32 = 18;
47 pub const CPU_TYPE_POWERPC64: u32 = CPU_TYPE_POWERPC | CPU_ARCH_ABI64;
48 
49 /*
50  * Capability bits used in the definition of cpu_subtype.
51  */
52 /// mask for feature flags
53 pub const CPU_SUBTYPE_MASK: u32 = 0xff00_0000;
54 /// 64 bit libraries
55 pub const CPU_SUBTYPE_LIB64: u32 = 0x8000_0000;
56 
57 /*
58  *	Object files that are hand-crafted to run on any
59  *	implementation of an architecture are tagged with
60  *	CPU_SUBTYPE_MULTIPLE.  This functions essentially the same as
61  *	the "ALL" subtype of an architecture except that it allows us
62  *	to easily find object files that may need to be modified
63  *	whenever a new implementation of an architecture comes out.
64  *
65  *	It is the responsibility of the implementor to make sure the
66  *	software handles unsupported implementations elegantly.
67  */
68 pub const CPU_SUBTYPE_MULTIPLE: u32 = !0;
69 pub const CPU_SUBTYPE_LITTLE_ENDIAN: u32 = 0;
70 pub const CPU_SUBTYPE_BIG_ENDIAN: u32 = 1;
71 
72 /*
73  *	VAX subtypes (these do *not* necessary conform to the actual cpu
74  *	ID assigned by DEC available via the SID register).
75  */
76 
77 pub const CPU_SUBTYPE_VAX_ALL: u32 = 0;
78 pub const CPU_SUBTYPE_VAX780: u32 = 1;
79 pub const CPU_SUBTYPE_VAX785: u32 = 2;
80 pub const CPU_SUBTYPE_VAX750: u32 = 3;
81 pub const CPU_SUBTYPE_VAX730: u32 = 4;
82 pub const CPU_SUBTYPE_UVAXI: u32 = 5;
83 pub const CPU_SUBTYPE_UVAXII: u32 = 6;
84 pub const CPU_SUBTYPE_VAX8200: u32 = 7;
85 pub const CPU_SUBTYPE_VAX8500: u32 = 8;
86 pub const CPU_SUBTYPE_VAX8600: u32 = 9;
87 pub const CPU_SUBTYPE_VAX8650: u32 = 10;
88 pub const CPU_SUBTYPE_VAX8800: u32 = 11;
89 pub const CPU_SUBTYPE_UVAXIII: u32 = 12;
90 
91 /*
92  *      680x0 subtypes
93  *
94  * The subtype definitions here are unusual for historical reasons.
95  * NeXT used to consider 68030 code as generic 68000 code.  For
96  * backwards compatability:
97  *
98  *	CPU_SUBTYPE_MC68030 symbol has been preserved for source code
99  *	compatability.
100  *
101  *	CPU_SUBTYPE_MC680x0_ALL has been defined to be the same
102  *	subtype as CPU_SUBTYPE_MC68030 for binary comatability.
103  *
104  *	CPU_SUBTYPE_MC68030_ONLY has been added to allow new object
105  *	files to be tagged as containing 68030-specific instructions.
106  */
107 
108 pub const CPU_SUBTYPE_MC680X0_ALL: u32 = 1;
109 // compat
110 pub const CPU_SUBTYPE_MC68030: u32 = 1;
111 pub const CPU_SUBTYPE_MC68040: u32 = 2;
112 pub const CPU_SUBTYPE_MC68030_ONLY: u32 = 3;
113 
114 /*
115  *	I386 subtypes
116  */
117 
118 #[inline]
cpu_subtype_intel(f: u32, m: u32) -> u32119 pub const fn cpu_subtype_intel(f: u32, m: u32) -> u32 {
120     f + (m << 4)
121 }
122 
123 pub const CPU_SUBTYPE_I386_ALL: u32 = cpu_subtype_intel(3, 0);
124 pub const CPU_SUBTYPE_386: u32 = cpu_subtype_intel(3, 0);
125 pub const CPU_SUBTYPE_486: u32 = cpu_subtype_intel(4, 0);
126 pub const CPU_SUBTYPE_486SX: u32 = cpu_subtype_intel(4, 8);
127 pub const CPU_SUBTYPE_586: u32 = cpu_subtype_intel(5, 0);
128 pub const CPU_SUBTYPE_PENT: u32 = cpu_subtype_intel(5, 0);
129 pub const CPU_SUBTYPE_PENTPRO: u32 = cpu_subtype_intel(6, 1);
130 pub const CPU_SUBTYPE_PENTII_M3: u32 = cpu_subtype_intel(6, 3);
131 pub const CPU_SUBTYPE_PENTII_M5: u32 = cpu_subtype_intel(6, 5);
132 pub const CPU_SUBTYPE_CELERON: u32 = cpu_subtype_intel(7, 6);
133 pub const CPU_SUBTYPE_CELERON_MOBILE: u32 = cpu_subtype_intel(7, 7);
134 pub const CPU_SUBTYPE_PENTIUM_3: u32 = cpu_subtype_intel(8, 0);
135 pub const CPU_SUBTYPE_PENTIUM_3_M: u32 = cpu_subtype_intel(8, 1);
136 pub const CPU_SUBTYPE_PENTIUM_3_XEON: u32 = cpu_subtype_intel(8, 2);
137 pub const CPU_SUBTYPE_PENTIUM_M: u32 = cpu_subtype_intel(9, 0);
138 pub const CPU_SUBTYPE_PENTIUM_4: u32 = cpu_subtype_intel(10, 0);
139 pub const CPU_SUBTYPE_PENTIUM_4_M: u32 = cpu_subtype_intel(10, 1);
140 pub const CPU_SUBTYPE_ITANIUM: u32 = cpu_subtype_intel(11, 0);
141 pub const CPU_SUBTYPE_ITANIUM_2: u32 = cpu_subtype_intel(11, 1);
142 pub const CPU_SUBTYPE_XEON: u32 = cpu_subtype_intel(12, 0);
143 pub const CPU_SUBTYPE_XEON_MP: u32 = cpu_subtype_intel(12, 1);
144 
145 #[inline]
cpu_subtype_intel_family(x: u32) -> u32146 pub const fn cpu_subtype_intel_family(x: u32) -> u32 {
147     x & 15
148 }
149 pub const CPU_SUBTYPE_INTEL_FAMILY_MAX: u32 = 15;
150 
151 #[inline]
cpu_subtype_intel_model(x: u32) -> u32152 pub const fn cpu_subtype_intel_model(x: u32) -> u32 {
153     x >> 4
154 }
155 pub const CPU_SUBTYPE_INTEL_MODEL_ALL: u32 = 0;
156 
157 /*
158  *	X86 subtypes.
159  */
160 
161 pub const CPU_SUBTYPE_X86_ALL: u32 = 3;
162 pub const CPU_SUBTYPE_X86_64_ALL: u32 = 3;
163 pub const CPU_SUBTYPE_X86_ARCH1: u32 = 4;
164 /// Haswell feature subset
165 pub const CPU_SUBTYPE_X86_64_H: u32 = 8;
166 
167 /*
168  *	Mips subtypes.
169  */
170 
171 pub const CPU_SUBTYPE_MIPS_ALL: u32 = 0;
172 pub const CPU_SUBTYPE_MIPS_R2300: u32 = 1;
173 pub const CPU_SUBTYPE_MIPS_R2600: u32 = 2;
174 pub const CPU_SUBTYPE_MIPS_R2800: u32 = 3;
175 /// pmax
176 pub const CPU_SUBTYPE_MIPS_R2000A: u32 = 4;
177 pub const CPU_SUBTYPE_MIPS_R2000: u32 = 5;
178 /// 3max
179 pub const CPU_SUBTYPE_MIPS_R3000A: u32 = 6;
180 pub const CPU_SUBTYPE_MIPS_R3000: u32 = 7;
181 
182 /*
183  *	MC98000 (PowerPC) subtypes
184  */
185 pub const CPU_SUBTYPE_MC98000_ALL: u32 = 0;
186 pub const CPU_SUBTYPE_MC98601: u32 = 1;
187 
188 /*
189  *	HPPA subtypes for Hewlett-Packard HP-PA family of
190  *	risc processors. Port by NeXT to 700 series.
191  */
192 
193 pub const CPU_SUBTYPE_HPPA_ALL: u32 = 0;
194 pub const CPU_SUBTYPE_HPPA_7100LC: u32 = 1;
195 
196 /*
197  *	MC88000 subtypes.
198  */
199 pub const CPU_SUBTYPE_MC88000_ALL: u32 = 0;
200 pub const CPU_SUBTYPE_MC88100: u32 = 1;
201 pub const CPU_SUBTYPE_MC88110: u32 = 2;
202 
203 /*
204  *	SPARC subtypes
205  */
206 pub const CPU_SUBTYPE_SPARC_ALL: u32 = 0;
207 
208 /*
209  *	I860 subtypes
210  */
211 pub const CPU_SUBTYPE_I860_ALL: u32 = 0;
212 pub const CPU_SUBTYPE_I860_860: u32 = 1;
213 
214 /*
215  *	PowerPC subtypes
216  */
217 pub const CPU_SUBTYPE_POWERPC_ALL: u32 = 0;
218 pub const CPU_SUBTYPE_POWERPC_601: u32 = 1;
219 pub const CPU_SUBTYPE_POWERPC_602: u32 = 2;
220 pub const CPU_SUBTYPE_POWERPC_603: u32 = 3;
221 pub const CPU_SUBTYPE_POWERPC_603E: u32 = 4;
222 pub const CPU_SUBTYPE_POWERPC_603EV: u32 = 5;
223 pub const CPU_SUBTYPE_POWERPC_604: u32 = 6;
224 pub const CPU_SUBTYPE_POWERPC_604E: u32 = 7;
225 pub const CPU_SUBTYPE_POWERPC_620: u32 = 8;
226 pub const CPU_SUBTYPE_POWERPC_750: u32 = 9;
227 pub const CPU_SUBTYPE_POWERPC_7400: u32 = 10;
228 pub const CPU_SUBTYPE_POWERPC_7450: u32 = 11;
229 pub const CPU_SUBTYPE_POWERPC_970: u32 = 100;
230 
231 /*
232  *	ARM subtypes
233  */
234 pub const CPU_SUBTYPE_ARM_ALL: u32 = 0;
235 pub const CPU_SUBTYPE_ARM_V4T: u32 = 5;
236 pub const CPU_SUBTYPE_ARM_V6: u32 = 6;
237 pub const CPU_SUBTYPE_ARM_V5TEJ: u32 = 7;
238 pub const CPU_SUBTYPE_ARM_XSCALE: u32 = 8;
239 pub const CPU_SUBTYPE_ARM_V7: u32 = 9;
240 /// Cortex A9
241 pub const CPU_SUBTYPE_ARM_V7F: u32 = 10;
242 /// Swift
243 pub const CPU_SUBTYPE_ARM_V7S: u32 = 11;
244 pub const CPU_SUBTYPE_ARM_V7K: u32 = 12;
245 /// Not meant to be run under xnu
246 pub const CPU_SUBTYPE_ARM_V6M: u32 = 14;
247 /// Not meant to be run under xnu
248 pub const CPU_SUBTYPE_ARM_V7M: u32 = 15;
249 /// Not meant to be run under xnu
250 pub const CPU_SUBTYPE_ARM_V7EM: u32 = 16;
251 
252 pub const CPU_SUBTYPE_ARM_V8: u32 = 13;
253 
254 /*
255  *  ARM64 subtypes
256  */
257 pub const CPU_SUBTYPE_ARM64_ALL: u32 = 0;
258 pub const CPU_SUBTYPE_ARM64_V8: u32 = 1;
259 pub const CPU_SUBTYPE_ARM64E: u32 = 2;
260 
261 /*
262  *  ARM64_32 subtypes
263  */
264 pub const CPU_SUBTYPE_ARM64_32_ALL: u32 = 0;
265 pub const CPU_SUBTYPE_ARM64_32_V8: u32 = 1;
266 
267 // Definitions from "/usr/include/mach/vm_prot.h".
268 
269 /// read permission
270 pub const VM_PROT_READ: u32 = 0x01;
271 /// write permission
272 pub const VM_PROT_WRITE: u32 = 0x02;
273 /// execute permission
274 pub const VM_PROT_EXECUTE: u32 = 0x04;
275 
276 // Definitions from "/usr/include/mach-o/loader.h".
277 
278 /*
279  * This header file describes the structures of the file format for "fat"
280  * architecture specific file (wrapper design).  At the begining of the file
281  * there is one `FatHeader` structure followed by a number of `FatArch*`
282  * structures.  For each architecture in the file, specified by a pair of
283  * cputype and cpusubtype, the `FatHeader` describes the file offset, file
284  * size and alignment in the file of the architecture specific member.
285  * The padded bytes in the file to place each member on it's specific alignment
286  * are defined to be read as zeros and can be left as "holes" if the file system
287  * can support them as long as they read as zeros.
288  *
289  * All structures defined here are always written and read to/from disk
290  * in big-endian order.
291  */
292 
293 pub const FAT_MAGIC: u32 = 0xcafe_babe;
294 /// NXSwapLong(FAT_MAGIC)
295 pub const FAT_CIGAM: u32 = 0xbeba_feca;
296 
297 #[derive(Debug, Clone, Copy)]
298 #[repr(C)]
299 pub struct FatHeader {
300     /// FAT_MAGIC or FAT_MAGIC_64
301     pub magic: U32<BigEndian>,
302     /// number of structs that follow
303     pub nfat_arch: U32<BigEndian>,
304 }
305 
306 #[derive(Debug, Clone, Copy)]
307 #[repr(C)]
308 pub struct FatArch32 {
309     /// cpu specifier (int)
310     pub cputype: U32<BigEndian>,
311     /// machine specifier (int)
312     pub cpusubtype: U32<BigEndian>,
313     /// file offset to this object file
314     pub offset: U32<BigEndian>,
315     /// size of this object file
316     pub size: U32<BigEndian>,
317     /// alignment as a power of 2
318     pub align: U32<BigEndian>,
319 }
320 
321 /*
322  * The support for the 64-bit fat file format described here is a work in
323  * progress and not yet fully supported in all the Apple Developer Tools.
324  *
325  * When a slice is greater than 4mb or an offset to a slice is greater than 4mb
326  * then the 64-bit fat file format is used.
327  */
328 pub const FAT_MAGIC_64: u32 = 0xcafe_babf;
329 /// NXSwapLong(FAT_MAGIC_64)
330 pub const FAT_CIGAM_64: u32 = 0xbfba_feca;
331 
332 #[derive(Debug, Clone, Copy)]
333 #[repr(C)]
334 pub struct FatArch64 {
335     /// cpu specifier (int)
336     pub cputype: U32<BigEndian>,
337     /// machine specifier (int)
338     pub cpusubtype: U32<BigEndian>,
339     /// file offset to this object file
340     pub offset: U64<BigEndian>,
341     /// size of this object file
342     pub size: U64<BigEndian>,
343     /// alignment as a power of 2
344     pub align: U32<BigEndian>,
345     /// reserved
346     pub reserved: U32<BigEndian>,
347 }
348 
349 // Definitions from "/usr/include/mach-o/loader.h".
350 
351 /// The 32-bit mach header.
352 ///
353 /// Appears at the very beginning of the object file for 32-bit architectures.
354 #[derive(Debug, Clone, Copy)]
355 #[repr(C)]
356 pub struct MachHeader32<E: Endian> {
357     /// mach magic number identifier
358     pub magic: U32<BigEndian>,
359     /// cpu specifier
360     pub cputype: U32<E>,
361     /// machine specifier
362     pub cpusubtype: U32<E>,
363     /// type of file
364     pub filetype: U32<E>,
365     /// number of load commands
366     pub ncmds: U32<E>,
367     /// the size of all the load commands
368     pub sizeofcmds: U32<E>,
369     /// flags
370     pub flags: U32<E>,
371 }
372 
373 // Values for `MachHeader32::magic`.
374 /// the mach magic number
375 pub const MH_MAGIC: u32 = 0xfeed_face;
376 /// NXSwapInt(MH_MAGIC)
377 pub const MH_CIGAM: u32 = 0xcefa_edfe;
378 
379 /// The 64-bit mach header.
380 ///
381 /// Appears at the very beginning of object files for 64-bit architectures.
382 #[derive(Debug, Clone, Copy)]
383 #[repr(C)]
384 pub struct MachHeader64<E: Endian> {
385     /// mach magic number identifier
386     pub magic: U32<BigEndian>,
387     /// cpu specifier
388     pub cputype: U32<E>,
389     /// machine specifier
390     pub cpusubtype: U32<E>,
391     /// type of file
392     pub filetype: U32<E>,
393     /// number of load commands
394     pub ncmds: U32<E>,
395     /// the size of all the load commands
396     pub sizeofcmds: U32<E>,
397     /// flags
398     pub flags: U32<E>,
399     /// reserved
400     pub reserved: U32<E>,
401 }
402 
403 // Values for `MachHeader64::magic`.
404 /// the 64-bit mach magic number
405 pub const MH_MAGIC_64: u32 = 0xfeed_facf;
406 /// NXSwapInt(MH_MAGIC_64)
407 pub const MH_CIGAM_64: u32 = 0xcffa_edfe;
408 
409 /*
410  * The layout of the file depends on the filetype.  For all but the MH_OBJECT
411  * file type the segments are padded out and aligned on a segment alignment
412  * boundary for efficient demand pageing.  The MH_EXECUTE, MH_FVMLIB, MH_DYLIB,
413  * MH_DYLINKER and MH_BUNDLE file types also have the headers included as part
414  * of their first segment.
415  *
416  * The file type MH_OBJECT is a compact format intended as output of the
417  * assembler and input (and possibly output) of the link editor (the .o
418  * format).  All sections are in one unnamed segment with no segment padding.
419  * This format is used as an executable format when the file is so small the
420  * segment padding greatly increases its size.
421  *
422  * The file type MH_PRELOAD is an executable format intended for things that
423  * are not executed under the kernel (proms, stand alones, kernels, etc).  The
424  * format can be executed under the kernel but may demand paged it and not
425  * preload it before execution.
426  *
427  * A core file is in MH_CORE format and can be any in an arbritray legal
428  * Mach-O file.
429  */
430 
431 // Values for `MachHeader*::filetype`.
432 /// relocatable object file
433 pub const MH_OBJECT: u32 = 0x1;
434 /// demand paged executable file
435 pub const MH_EXECUTE: u32 = 0x2;
436 /// fixed VM shared library file
437 pub const MH_FVMLIB: u32 = 0x3;
438 /// core file
439 pub const MH_CORE: u32 = 0x4;
440 /// preloaded executable file
441 pub const MH_PRELOAD: u32 = 0x5;
442 /// dynamically bound shared library
443 pub const MH_DYLIB: u32 = 0x6;
444 /// dynamic link editor
445 pub const MH_DYLINKER: u32 = 0x7;
446 /// dynamically bound bundle file
447 pub const MH_BUNDLE: u32 = 0x8;
448 /// shared library stub for static linking only, no section contents
449 pub const MH_DYLIB_STUB: u32 = 0x9;
450 /// companion file with only debug sections
451 pub const MH_DSYM: u32 = 0xa;
452 /// x86_64 kexts
453 pub const MH_KEXT_BUNDLE: u32 = 0xb;
454 
455 // Values for `MachHeader*::flags`.
456 /// the object file has no undefined references
457 pub const MH_NOUNDEFS: u32 = 0x1;
458 /// the object file is the output of an incremental link against a base file and can't be link edited again
459 pub const MH_INCRLINK: u32 = 0x2;
460 /// the object file is input for the dynamic linker and can't be staticly link edited again
461 pub const MH_DYLDLINK: u32 = 0x4;
462 /// the object file's undefined references are bound by the dynamic linker when loaded.
463 pub const MH_BINDATLOAD: u32 = 0x8;
464 /// the file has its dynamic undefined references prebound.
465 pub const MH_PREBOUND: u32 = 0x10;
466 /// the file has its read-only and read-write segments split
467 pub const MH_SPLIT_SEGS: u32 = 0x20;
468 /// the shared library init routine is to be run lazily via catching memory faults to its writeable segments (obsolete)
469 pub const MH_LAZY_INIT: u32 = 0x40;
470 /// the image is using two-level name space bindings
471 pub const MH_TWOLEVEL: u32 = 0x80;
472 /// the executable is forcing all images to use flat name space bindings
473 pub const MH_FORCE_FLAT: u32 = 0x100;
474 /// this umbrella guarantees no multiple defintions of symbols in its sub-images so the two-level namespace hints can always be used.
475 pub const MH_NOMULTIDEFS: u32 = 0x200;
476 /// do not have dyld notify the prebinding agent about this executable
477 pub const MH_NOFIXPREBINDING: u32 = 0x400;
478 /// the binary is not prebound but can have its prebinding redone. only used when MH_PREBOUND is not set.
479 pub const MH_PREBINDABLE: u32 = 0x800;
480 /// indicates that this binary binds to all two-level namespace modules of its dependent libraries. only used when MH_PREBINDABLE and MH_TWOLEVEL are both set.
481 pub const MH_ALLMODSBOUND: u32 = 0x1000;
482 /// safe to divide up the sections into sub-sections via symbols for dead code stripping
483 pub const MH_SUBSECTIONS_VIA_SYMBOLS: u32 = 0x2000;
484 /// the binary has been canonicalized via the unprebind operation
485 pub const MH_CANONICAL: u32 = 0x4000;
486 /// the final linked image contains external weak symbols
487 pub const MH_WEAK_DEFINES: u32 = 0x8000;
488 /// the final linked image uses weak symbols
489 pub const MH_BINDS_TO_WEAK: u32 = 0x10000;
490 
491 /// When this bit is set, all stacks in the task will be given stack execution privilege.  Only used in MH_EXECUTE filetypes.
492 pub const MH_ALLOW_STACK_EXECUTION: u32 = 0x20000;
493 /// When this bit is set, the binary declares it is safe for use in processes with uid zero
494 pub const MH_ROOT_SAFE: u32 = 0x40000;
495 
496 /// When this bit is set, the binary declares it is safe for use in processes when issetugid() is true
497 pub const MH_SETUID_SAFE: u32 = 0x80000;
498 
499 /// When this bit is set on a dylib, the static linker does not need to examine dependent dylibs to see if any are re-exported
500 pub const MH_NO_REEXPORTED_DYLIBS: u32 = 0x10_0000;
501 /// When this bit is set, the OS will load the main executable at a random address.  Only used in MH_EXECUTE filetypes.
502 pub const MH_PIE: u32 = 0x20_0000;
503 /// Only for use on dylibs.  When linking against a dylib that has this bit set, the static linker will automatically not create a LC_LOAD_DYLIB load command to the dylib if no symbols are being referenced from the dylib.
504 pub const MH_DEAD_STRIPPABLE_DYLIB: u32 = 0x40_0000;
505 /// Contains a section of type S_THREAD_LOCAL_VARIABLES
506 pub const MH_HAS_TLV_DESCRIPTORS: u32 = 0x80_0000;
507 
508 /// When this bit is set, the OS will run the main executable with a non-executable heap even on platforms (e.g. i386) that don't require it. Only used in MH_EXECUTE filetypes.
509 pub const MH_NO_HEAP_EXECUTION: u32 = 0x100_0000;
510 
511 /// The code was linked for use in an application extension.
512 pub const MH_APP_EXTENSION_SAFE: u32 = 0x0200_0000;
513 
514 pub const MH_NLIST_OUTOFSYNC_WITH_DYLDINFO: u32 = 0x0400_0000;
515 /// The external symbols listed in the nlist symbol table do not include all the symbols listed in the dyld info.
516 
517 pub const MH_SIM_SUPPORT: u32 = 0x0800_0000;
518 
519 /// Common fields at the start of every load command.
520 ///
521 /// The load commands directly follow the mach_header.  The total size of all
522 /// of the commands is given by the sizeofcmds field in the mach_header.  All
523 /// load commands must have as their first two fields `cmd` and `cmdsize`.  The `cmd`
524 /// field is filled in with a constant for that command type.  Each command type
525 /// has a structure specifically for it.  The `cmdsize` field is the size in bytes
526 /// of the particular load command structure plus anything that follows it that
527 /// is a part of the load command (i.e. section structures, strings, etc.).  To
528 /// advance to the next load command the `cmdsize` can be added to the offset or
529 /// pointer of the current load command.  The `cmdsize` for 32-bit architectures
530 /// MUST be a multiple of 4 bytes and for 64-bit architectures MUST be a multiple
531 /// of 8 bytes (these are forever the maximum alignment of any load commands).
532 /// The padded bytes must be zero.  All tables in the object file must also
533 /// follow these rules so the file can be memory mapped.  Otherwise the pointers
534 /// to these tables will not work well or at all on some machines.  With all
535 /// padding zeroed like objects will compare byte for byte.
536 #[derive(Debug, Clone, Copy)]
537 #[repr(C)]
538 pub struct LoadCommand<E: Endian> {
539     /// Type of load command.
540     ///
541     /// One of the `LC_*` constants.
542     pub cmd: U32<E>,
543     /// Total size of command in bytes.
544     pub cmdsize: U32<E>,
545 }
546 
547 /*
548  * After MacOS X 10.1 when a new load command is added that is required to be
549  * understood by the dynamic linker for the image to execute properly the
550  * LC_REQ_DYLD bit will be or'ed into the load command constant.  If the dynamic
551  * linker sees such a load command it it does not understand will issue a
552  * "unknown load command required for execution" error and refuse to use the
553  * image.  Other load commands without this bit that are not understood will
554  * simply be ignored.
555  */
556 pub const LC_REQ_DYLD: u32 = 0x8000_0000;
557 
558 /* Constants for the cmd field of all load commands, the type */
559 /// segment of this file to be mapped
560 pub const LC_SEGMENT: u32 = 0x1;
561 /// link-edit stab symbol table info
562 pub const LC_SYMTAB: u32 = 0x2;
563 /// link-edit gdb symbol table info (obsolete)
564 pub const LC_SYMSEG: u32 = 0x3;
565 /// thread
566 pub const LC_THREAD: u32 = 0x4;
567 /// unix thread (includes a stack)
568 pub const LC_UNIXTHREAD: u32 = 0x5;
569 /// load a specified fixed VM shared library
570 pub const LC_LOADFVMLIB: u32 = 0x6;
571 /// fixed VM shared library identification
572 pub const LC_IDFVMLIB: u32 = 0x7;
573 /// object identification info (obsolete)
574 pub const LC_IDENT: u32 = 0x8;
575 /// fixed VM file inclusion (internal use)
576 pub const LC_FVMFILE: u32 = 0x9;
577 /// prepage command (internal use)
578 pub const LC_PREPAGE: u32 = 0xa;
579 /// dynamic link-edit symbol table info
580 pub const LC_DYSYMTAB: u32 = 0xb;
581 /// load a dynamically linked shared library
582 pub const LC_LOAD_DYLIB: u32 = 0xc;
583 /// dynamically linked shared lib ident
584 pub const LC_ID_DYLIB: u32 = 0xd;
585 /// load a dynamic linker
586 pub const LC_LOAD_DYLINKER: u32 = 0xe;
587 /// dynamic linker identification
588 pub const LC_ID_DYLINKER: u32 = 0xf;
589 /// modules prebound for a dynamically linked shared library
590 pub const LC_PREBOUND_DYLIB: u32 = 0x10;
591 /// image routines
592 pub const LC_ROUTINES: u32 = 0x11;
593 /// sub framework
594 pub const LC_SUB_FRAMEWORK: u32 = 0x12;
595 /// sub umbrella
596 pub const LC_SUB_UMBRELLA: u32 = 0x13;
597 /// sub client
598 pub const LC_SUB_CLIENT: u32 = 0x14;
599 /// sub library
600 pub const LC_SUB_LIBRARY: u32 = 0x15;
601 /// two-level namespace lookup hints
602 pub const LC_TWOLEVEL_HINTS: u32 = 0x16;
603 /// prebind checksum
604 pub const LC_PREBIND_CKSUM: u32 = 0x17;
605 
606 /*
607  * load a dynamically linked shared library that is allowed to be missing
608  * (all symbols are weak imported).
609  */
610 pub const LC_LOAD_WEAK_DYLIB: u32 = 0x18 | LC_REQ_DYLD;
611 
612 /// 64-bit segment of this file to be mapped
613 pub const LC_SEGMENT_64: u32 = 0x19;
614 /// 64-bit image routines
615 pub const LC_ROUTINES_64: u32 = 0x1a;
616 /// the uuid
617 pub const LC_UUID: u32 = 0x1b;
618 /// runpath additions
619 pub const LC_RPATH: u32 = 0x1c | LC_REQ_DYLD;
620 /// local of code signature
621 pub const LC_CODE_SIGNATURE: u32 = 0x1d;
622 /// local of info to split segments
623 pub const LC_SEGMENT_SPLIT_INFO: u32 = 0x1e;
624 /// load and re-export dylib
625 pub const LC_REEXPORT_DYLIB: u32 = 0x1f | LC_REQ_DYLD;
626 /// delay load of dylib until first use
627 pub const LC_LAZY_LOAD_DYLIB: u32 = 0x20;
628 /// encrypted segment information
629 pub const LC_ENCRYPTION_INFO: u32 = 0x21;
630 /// compressed dyld information
631 pub const LC_DYLD_INFO: u32 = 0x22;
632 /// compressed dyld information only
633 pub const LC_DYLD_INFO_ONLY: u32 = 0x22 | LC_REQ_DYLD;
634 /// load upward dylib
635 pub const LC_LOAD_UPWARD_DYLIB: u32 = 0x23 | LC_REQ_DYLD;
636 /// build for MacOSX min OS version
637 pub const LC_VERSION_MIN_MACOSX: u32 = 0x24;
638 /// build for iPhoneOS min OS version
639 pub const LC_VERSION_MIN_IPHONEOS: u32 = 0x25;
640 /// compressed table of function start addresses
641 pub const LC_FUNCTION_STARTS: u32 = 0x26;
642 /// string for dyld to treat like environment variable
643 pub const LC_DYLD_ENVIRONMENT: u32 = 0x27;
644 /// replacement for LC_UNIXTHREAD
645 pub const LC_MAIN: u32 = 0x28 | LC_REQ_DYLD;
646 /// table of non-instructions in __text
647 pub const LC_DATA_IN_CODE: u32 = 0x29;
648 /// source version used to build binary
649 pub const LC_SOURCE_VERSION: u32 = 0x2A;
650 /// Code signing DRs copied from linked dylibs
651 pub const LC_DYLIB_CODE_SIGN_DRS: u32 = 0x2B;
652 /// 64-bit encrypted segment information
653 pub const LC_ENCRYPTION_INFO_64: u32 = 0x2C;
654 /// linker options in MH_OBJECT files
655 pub const LC_LINKER_OPTION: u32 = 0x2D;
656 /// optimization hints in MH_OBJECT files
657 pub const LC_LINKER_OPTIMIZATION_HINT: u32 = 0x2E;
658 /// build for AppleTV min OS version
659 pub const LC_VERSION_MIN_TVOS: u32 = 0x2F;
660 /// build for Watch min OS version
661 pub const LC_VERSION_MIN_WATCHOS: u32 = 0x30;
662 /// arbitrary data included within a Mach-O file
663 pub const LC_NOTE: u32 = 0x31;
664 /// build for platform min OS version
665 pub const LC_BUILD_VERSION: u32 = 0x32;
666 /// used with `LinkeditDataCommand`, payload is trie
667 pub const LC_DYLD_EXPORTS_TRIE: u32 = 0x33 | LC_REQ_DYLD;
668 /// used with `LinkeditDataCommand`
669 pub const LC_DYLD_CHAINED_FIXUPS: u32 = 0x34 | LC_REQ_DYLD;
670 
671 /// A variable length string in a load command.
672 ///
673 /// The strings are stored just after the load command structure and
674 /// the offset is from the start of the load command structure.  The size
675 /// of the string is reflected in the `cmdsize` field of the load command.
676 /// Once again any padded bytes to bring the `cmdsize` field to a multiple
677 /// of 4 bytes must be zero.
678 #[derive(Debug, Clone, Copy)]
679 #[repr(C)]
680 pub struct LcStr<E: Endian> {
681     /// offset to the string
682     pub offset: U32<E>,
683 }
684 
685 /// 32-bit segment load command.
686 ///
687 /// The segment load command indicates that a part of this file is to be
688 /// mapped into the task's address space.  The size of this segment in memory,
689 /// vmsize, maybe equal to or larger than the amount to map from this file,
690 /// filesize.  The file is mapped starting at fileoff to the beginning of
691 /// the segment in memory, vmaddr.  The rest of the memory of the segment,
692 /// if any, is allocated zero fill on demand.  The segment's maximum virtual
693 /// memory protection and initial virtual memory protection are specified
694 /// by the maxprot and initprot fields.  If the segment has sections then the
695 /// `Section32` structures directly follow the segment command and their size is
696 /// reflected in `cmdsize`.
697 #[derive(Debug, Clone, Copy)]
698 #[repr(C)]
699 pub struct SegmentCommand32<E: Endian> {
700     /// LC_SEGMENT
701     pub cmd: U32<E>,
702     /// includes sizeof section structs
703     pub cmdsize: U32<E>,
704     /// segment name
705     pub segname: [u8; 16],
706     /// memory address of this segment
707     pub vmaddr: U32<E>,
708     /// memory size of this segment
709     pub vmsize: U32<E>,
710     /// file offset of this segment
711     pub fileoff: U32<E>,
712     /// amount to map from the file
713     pub filesize: U32<E>,
714     /// maximum VM protection
715     pub maxprot: U32<E>,
716     /// initial VM protection
717     pub initprot: U32<E>,
718     /// number of sections in segment
719     pub nsects: U32<E>,
720     /// flags
721     pub flags: U32<E>,
722 }
723 
724 /// 64-bit segment load command.
725 ///
726 /// The 64-bit segment load command indicates that a part of this file is to be
727 /// mapped into a 64-bit task's address space.  If the 64-bit segment has
728 /// sections then `Section64` structures directly follow the 64-bit segment
729 /// command and their size is reflected in `cmdsize`.
730 #[derive(Debug, Clone, Copy)]
731 #[repr(C)]
732 pub struct SegmentCommand64<E: Endian> {
733     /// LC_SEGMENT_64
734     pub cmd: U32<E>,
735     /// includes sizeof section_64 structs
736     pub cmdsize: U32<E>,
737     /// segment name
738     pub segname: [u8; 16],
739     /// memory address of this segment
740     pub vmaddr: U64<E>,
741     /// memory size of this segment
742     pub vmsize: U64<E>,
743     /// file offset of this segment
744     pub fileoff: U64<E>,
745     /// amount to map from the file
746     pub filesize: U64<E>,
747     /// maximum VM protection
748     pub maxprot: U32<E>,
749     /// initial VM protection
750     pub initprot: U32<E>,
751     /// number of sections in segment
752     pub nsects: U32<E>,
753     /// flags
754     pub flags: U32<E>,
755 }
756 
757 // Values for `SegmentCommand*::flags`.
758 /// the file contents for this segment is for the high part of the VM space, the low part is zero filled (for stacks in core files)
759 pub const SG_HIGHVM: u32 = 0x1;
760 /// this segment is the VM that is allocated by a fixed VM library, for overlap checking in the link editor
761 pub const SG_FVMLIB: u32 = 0x2;
762 /// this segment has nothing that was relocated in it and nothing relocated to it, that is it maybe safely replaced without relocation
763 pub const SG_NORELOC: u32 = 0x4;
764 /// This segment is protected.  If the segment starts at file offset 0, the first page of the segment is not protected.  All other pages of the segment are protected.
765 pub const SG_PROTECTED_VERSION_1: u32 = 0x8;
766 /// This segment is made read-only after fixups
767 pub const SG_READ_ONLY: u32 = 0x10;
768 
769 /*
770  * A segment is made up of zero or more sections.  Non-MH_OBJECT files have
771  * all of their segments with the proper sections in each, and padded to the
772  * specified segment alignment when produced by the link editor.  The first
773  * segment of a MH_EXECUTE and MH_FVMLIB format file contains the mach_header
774  * and load commands of the object file before its first section.  The zero
775  * fill sections are always last in their segment (in all formats).  This
776  * allows the zeroed segment padding to be mapped into memory where zero fill
777  * sections might be. The gigabyte zero fill sections, those with the section
778  * type S_GB_ZEROFILL, can only be in a segment with sections of this type.
779  * These segments are then placed after all other segments.
780  *
781  * The MH_OBJECT format has all of its sections in one segment for
782  * compactness.  There is no padding to a specified segment boundary and the
783  * mach_header and load commands are not part of the segment.
784  *
785  * Sections with the same section name, sectname, going into the same segment,
786  * segname, are combined by the link editor.  The resulting section is aligned
787  * to the maximum alignment of the combined sections and is the new section's
788  * alignment.  The combined sections are aligned to their original alignment in
789  * the combined section.  Any padded bytes to get the specified alignment are
790  * zeroed.
791  *
792  * The format of the relocation entries referenced by the reloff and nreloc
793  * fields of the section structure for mach object files is described in the
794  * header file <reloc.h>.
795  */
796 /// 32-bit section.
797 #[derive(Debug, Clone, Copy)]
798 #[repr(C)]
799 pub struct Section32<E: Endian> {
800     /// name of this section
801     pub sectname: [u8; 16],
802     /// segment this section goes in
803     pub segname: [u8; 16],
804     /// memory address of this section
805     pub addr: U32<E>,
806     /// size in bytes of this section
807     pub size: U32<E>,
808     /// file offset of this section
809     pub offset: U32<E>,
810     /// section alignment (power of 2)
811     pub align: U32<E>,
812     /// file offset of relocation entries
813     pub reloff: U32<E>,
814     /// number of relocation entries
815     pub nreloc: U32<E>,
816     /// flags (section type and attributes)
817     pub flags: U32<E>,
818     /// reserved (for offset or index)
819     pub reserved1: U32<E>,
820     /// reserved (for count or sizeof)
821     pub reserved2: U32<E>,
822 }
823 
824 /// 64-bit section.
825 #[derive(Debug, Clone, Copy)]
826 #[repr(C)]
827 pub struct Section64<E: Endian> {
828     /// name of this section
829     pub sectname: [u8; 16],
830     /// segment this section goes in
831     pub segname: [u8; 16],
832     /// memory address of this section
833     pub addr: U64<E>,
834     /// size in bytes of this section
835     pub size: U64<E>,
836     /// file offset of this section
837     pub offset: U32<E>,
838     /// section alignment (power of 2)
839     pub align: U32<E>,
840     /// file offset of relocation entries
841     pub reloff: U32<E>,
842     /// number of relocation entries
843     pub nreloc: U32<E>,
844     /// flags (section type and attributes)
845     pub flags: U32<E>,
846     /// reserved (for offset or index)
847     pub reserved1: U32<E>,
848     /// reserved (for count or sizeof)
849     pub reserved2: U32<E>,
850     /// reserved
851     pub reserved3: U32<E>,
852 }
853 
854 /*
855  * The flags field of a section structure is separated into two parts a section
856  * type and section attributes.  The section types are mutually exclusive (it
857  * can only have one type) but the section attributes are not (it may have more
858  * than one attribute).
859  */
860 /// 256 section types
861 pub const SECTION_TYPE: u32 = 0x0000_00ff;
862 /// 24 section attributes
863 pub const SECTION_ATTRIBUTES: u32 = 0xffff_ff00;
864 
865 /* Constants for the type of a section */
866 /// regular section
867 pub const S_REGULAR: u32 = 0x0;
868 /// zero fill on demand section
869 pub const S_ZEROFILL: u32 = 0x1;
870 /// section with only literal C strings
871 pub const S_CSTRING_LITERALS: u32 = 0x2;
872 /// section with only 4 byte literals
873 pub const S_4BYTE_LITERALS: u32 = 0x3;
874 /// section with only 8 byte literals
875 pub const S_8BYTE_LITERALS: u32 = 0x4;
876 /// section with only pointers to literals
877 pub const S_LITERAL_POINTERS: u32 = 0x5;
878 /*
879  * For the two types of symbol pointers sections and the symbol stubs section
880  * they have indirect symbol table entries.  For each of the entries in the
881  * section the indirect symbol table entries, in corresponding order in the
882  * indirect symbol table, start at the index stored in the reserved1 field
883  * of the section structure.  Since the indirect symbol table entries
884  * correspond to the entries in the section the number of indirect symbol table
885  * entries is inferred from the size of the section divided by the size of the
886  * entries in the section.  For symbol pointers sections the size of the entries
887  * in the section is 4 bytes and for symbol stubs sections the byte size of the
888  * stubs is stored in the reserved2 field of the section structure.
889  */
890 /// section with only non-lazy symbol pointers
891 pub const S_NON_LAZY_SYMBOL_POINTERS: u32 = 0x6;
892 /// section with only lazy symbol pointers
893 pub const S_LAZY_SYMBOL_POINTERS: u32 = 0x7;
894 /// section with only symbol stubs, byte size of stub in the reserved2 field
895 pub const S_SYMBOL_STUBS: u32 = 0x8;
896 /// section with only function pointers for initialization
897 pub const S_MOD_INIT_FUNC_POINTERS: u32 = 0x9;
898 /// section with only function pointers for termination
899 pub const S_MOD_TERM_FUNC_POINTERS: u32 = 0xa;
900 /// section contains symbols that are to be coalesced
901 pub const S_COALESCED: u32 = 0xb;
902 /// zero fill on demand section (that can be larger than 4 gigabytes)
903 pub const S_GB_ZEROFILL: u32 = 0xc;
904 /// section with only pairs of function pointers for interposing
905 pub const S_INTERPOSING: u32 = 0xd;
906 /// section with only 16 byte literals
907 pub const S_16BYTE_LITERALS: u32 = 0xe;
908 /// section contains DTrace Object Format
909 pub const S_DTRACE_DOF: u32 = 0xf;
910 /// section with only lazy symbol pointers to lazy loaded dylibs
911 pub const S_LAZY_DYLIB_SYMBOL_POINTERS: u32 = 0x10;
912 /*
913  * Section types to support thread local variables
914  */
915 /// template of initial values for TLVs
916 pub const S_THREAD_LOCAL_REGULAR: u32 = 0x11;
917 /// template of initial values for TLVs
918 pub const S_THREAD_LOCAL_ZEROFILL: u32 = 0x12;
919 /// TLV descriptors
920 pub const S_THREAD_LOCAL_VARIABLES: u32 = 0x13;
921 /// pointers to TLV descriptors
922 pub const S_THREAD_LOCAL_VARIABLE_POINTERS: u32 = 0x14;
923 /// functions to call to initialize TLV values
924 pub const S_THREAD_LOCAL_INIT_FUNCTION_POINTERS: u32 = 0x15;
925 /// 32-bit offsets to initializers
926 pub const S_INIT_FUNC_OFFSETS: u32 = 0x16;
927 
928 /*
929  * Constants for the section attributes part of the flags field of a section
930  * structure.
931  */
932 /// User setable attributes
933 pub const SECTION_ATTRIBUTES_USR: u32 = 0xff00_0000;
934 /// section contains only true machine instructions
935 pub const S_ATTR_PURE_INSTRUCTIONS: u32 = 0x8000_0000;
936 /// section contains coalesced symbols that are not to be in a ranlib table of contents
937 pub const S_ATTR_NO_TOC: u32 = 0x4000_0000;
938 /// ok to strip static symbols in this section in files with the MH_DYLDLINK flag
939 pub const S_ATTR_STRIP_STATIC_SYMS: u32 = 0x2000_0000;
940 /// no dead stripping
941 pub const S_ATTR_NO_DEAD_STRIP: u32 = 0x1000_0000;
942 /// blocks are live if they reference live blocks
943 pub const S_ATTR_LIVE_SUPPORT: u32 = 0x0800_0000;
944 /// Used with i386 code stubs written on by dyld
945 pub const S_ATTR_SELF_MODIFYING_CODE: u32 = 0x0400_0000;
946 /*
947  * If a segment contains any sections marked with S_ATTR_DEBUG then all
948  * sections in that segment must have this attribute.  No section other than
949  * a section marked with this attribute may reference the contents of this
950  * section.  A section with this attribute may contain no symbols and must have
951  * a section type S_REGULAR.  The static linker will not copy section contents
952  * from sections with this attribute into its output file.  These sections
953  * generally contain DWARF debugging info.
954  */
955 /// a debug section
956 pub const S_ATTR_DEBUG: u32 = 0x0200_0000;
957 /// system setable attributes
958 pub const SECTION_ATTRIBUTES_SYS: u32 = 0x00ff_ff00;
959 /// section contains some machine instructions
960 pub const S_ATTR_SOME_INSTRUCTIONS: u32 = 0x0000_0400;
961 /// section has external relocation entries
962 pub const S_ATTR_EXT_RELOC: u32 = 0x0000_0200;
963 /// section has local relocation entries
964 pub const S_ATTR_LOC_RELOC: u32 = 0x0000_0100;
965 
966 /*
967  * The names of segments and sections in them are mostly meaningless to the
968  * link-editor.  But there are few things to support traditional UNIX
969  * executables that require the link-editor and assembler to use some names
970  * agreed upon by convention.
971  *
972  * The initial protection of the "__TEXT" segment has write protection turned
973  * off (not writeable).
974  *
975  * The link-editor will allocate common symbols at the end of the "__common"
976  * section in the "__DATA" segment.  It will create the section and segment
977  * if needed.
978  */
979 
980 /* The currently known segment names and the section names in those segments */
981 
982 /// the pagezero segment which has no protections and catches NULL references for MH_EXECUTE files
983 pub const SEG_PAGEZERO: &str = "__PAGEZERO";
984 
985 /// the tradition UNIX text segment
986 pub const SEG_TEXT: &str = "__TEXT";
987 /// the real text part of the text section no headers, and no padding
988 pub const SECT_TEXT: &str = "__text";
989 /// the fvmlib initialization section
990 pub const SECT_FVMLIB_INIT0: &str = "__fvmlib_init0";
991 /// the section following the fvmlib initialization section
992 pub const SECT_FVMLIB_INIT1: &str = "__fvmlib_init1";
993 
994 /// the tradition UNIX data segment
995 pub const SEG_DATA: &str = "__DATA";
996 /// the real initialized data section no padding, no bss overlap
997 pub const SECT_DATA: &str = "__data";
998 /// the real uninitialized data section no padding
999 pub const SECT_BSS: &str = "__bss";
1000 /// the section common symbols are allocated in by the link editor
1001 pub const SECT_COMMON: &str = "__common";
1002 
1003 /// objective-C runtime segment
1004 pub const SEG_OBJC: &str = "__OBJC";
1005 /// symbol table
1006 pub const SECT_OBJC_SYMBOLS: &str = "__symbol_table";
1007 /// module information
1008 pub const SECT_OBJC_MODULES: &str = "__module_info";
1009 /// string table
1010 pub const SECT_OBJC_STRINGS: &str = "__selector_strs";
1011 /// string table
1012 pub const SECT_OBJC_REFS: &str = "__selector_refs";
1013 
1014 /// the icon segment
1015 pub const SEG_ICON: &str = "__ICON";
1016 /// the icon headers
1017 pub const SECT_ICON_HEADER: &str = "__header";
1018 /// the icons in tiff format
1019 pub const SECT_ICON_TIFF: &str = "__tiff";
1020 
1021 /// the segment containing all structs created and maintained by the link editor.  Created with -seglinkedit option to ld(1) for MH_EXECUTE and FVMLIB file types only
1022 pub const SEG_LINKEDIT: &str = "__LINKEDIT";
1023 
1024 /// the unix stack segment
1025 pub const SEG_UNIXSTACK: &str = "__UNIXSTACK";
1026 
1027 /// the segment for the self (dyld) modifing code stubs that has read, write and execute permissions
1028 pub const SEG_IMPORT: &str = "__IMPORT";
1029 
1030 /*
1031  * Fixed virtual memory shared libraries are identified by two things.  The
1032  * target pathname (the name of the library as found for execution), and the
1033  * minor version number.  The address of where the headers are loaded is in
1034  * header_addr. (THIS IS OBSOLETE and no longer supported).
1035  */
1036 #[derive(Debug, Clone, Copy)]
1037 #[repr(C)]
1038 pub struct Fvmlib<E: Endian> {
1039     /// library's target pathname
1040     pub name: LcStr<E>,
1041     /// library's minor version number
1042     pub minor_version: U32<E>,
1043     /// library's header address
1044     pub header_addr: U32<E>,
1045 }
1046 
1047 /*
1048  * A fixed virtual shared library (filetype == MH_FVMLIB in the mach header)
1049  * contains a `FvmlibCommand` (cmd == LC_IDFVMLIB) to identify the library.
1050  * An object that uses a fixed virtual shared library also contains a
1051  * `FvmlibCommand` (cmd == LC_LOADFVMLIB) for each library it uses.
1052  * (THIS IS OBSOLETE and no longer supported).
1053  */
1054 #[derive(Debug, Clone, Copy)]
1055 #[repr(C)]
1056 pub struct FvmlibCommand<E: Endian> {
1057     /// LC_IDFVMLIB or LC_LOADFVMLIB
1058     pub cmd: U32<E>,
1059     /// includes pathname string
1060     pub cmdsize: U32<E>,
1061     /// the library identification
1062     pub fvmlib: Fvmlib<E>,
1063 }
1064 
1065 /*
1066  * Dynamicly linked shared libraries are identified by two things.  The
1067  * pathname (the name of the library as found for execution), and the
1068  * compatibility version number.  The pathname must match and the compatibility
1069  * number in the user of the library must be greater than or equal to the
1070  * library being used.  The time stamp is used to record the time a library was
1071  * built and copied into user so it can be use to determined if the library used
1072  * at runtime is exactly the same as used to built the program.
1073  */
1074 #[derive(Debug, Clone, Copy)]
1075 #[repr(C)]
1076 pub struct Dylib<E: Endian> {
1077     /// library's path name
1078     pub name: LcStr<E>,
1079     /// library's build time stamp
1080     pub timestamp: U32<E>,
1081     /// library's current version number
1082     pub current_version: U32<E>,
1083     /// library's compatibility vers number
1084     pub compatibility_version: U32<E>,
1085 }
1086 
1087 /*
1088  * A dynamically linked shared library (filetype == MH_DYLIB in the mach header)
1089  * contains a `DylibCommand` (cmd == LC_ID_DYLIB) to identify the library.
1090  * An object that uses a dynamically linked shared library also contains a
1091  * `DylibCommand` (cmd == LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, or
1092  * LC_REEXPORT_DYLIB) for each library it uses.
1093  */
1094 #[derive(Debug, Clone, Copy)]
1095 #[repr(C)]
1096 pub struct DylibCommand<E: Endian> {
1097     /// LC_ID_DYLIB, LC_LOAD_{,WEAK_}DYLIB, LC_REEXPORT_DYLIB
1098     pub cmd: U32<E>,
1099     /// includes pathname string
1100     pub cmdsize: U32<E>,
1101     /// the library identification
1102     pub dylib: Dylib<E>,
1103 }
1104 
1105 /*
1106  * A dynamically linked shared library may be a subframework of an umbrella
1107  * framework.  If so it will be linked with "-umbrella umbrella_name" where
1108  * Where "umbrella_name" is the name of the umbrella framework. A subframework
1109  * can only be linked against by its umbrella framework or other subframeworks
1110  * that are part of the same umbrella framework.  Otherwise the static link
1111  * editor produces an error and states to link against the umbrella framework.
1112  * The name of the umbrella framework for subframeworks is recorded in the
1113  * following structure.
1114  */
1115 #[derive(Debug, Clone, Copy)]
1116 #[repr(C)]
1117 pub struct SubFrameworkCommand<E: Endian> {
1118     /// LC_SUB_FRAMEWORK
1119     pub cmd: U32<E>,
1120     /// includes umbrella string
1121     pub cmdsize: U32<E>,
1122     /// the umbrella framework name
1123     pub umbrella: LcStr<E>,
1124 }
1125 
1126 /*
1127  * For dynamically linked shared libraries that are subframework of an umbrella
1128  * framework they can allow clients other than the umbrella framework or other
1129  * subframeworks in the same umbrella framework.  To do this the subframework
1130  * is built with "-allowable_client client_name" and an LC_SUB_CLIENT load
1131  * command is created for each -allowable_client flag.  The client_name is
1132  * usually a framework name.  It can also be a name used for bundles clients
1133  * where the bundle is built with "-client_name client_name".
1134  */
1135 #[derive(Debug, Clone, Copy)]
1136 #[repr(C)]
1137 pub struct SubClientCommand<E: Endian> {
1138     /// LC_SUB_CLIENT
1139     pub cmd: U32<E>,
1140     /// includes client string
1141     pub cmdsize: U32<E>,
1142     /// the client name
1143     pub client: LcStr<E>,
1144 }
1145 
1146 /*
1147  * A dynamically linked shared library may be a sub_umbrella of an umbrella
1148  * framework.  If so it will be linked with "-sub_umbrella umbrella_name" where
1149  * Where "umbrella_name" is the name of the sub_umbrella framework.  When
1150  * staticly linking when -twolevel_namespace is in effect a twolevel namespace
1151  * umbrella framework will only cause its subframeworks and those frameworks
1152  * listed as sub_umbrella frameworks to be implicited linked in.  Any other
1153  * dependent dynamic libraries will not be linked it when -twolevel_namespace
1154  * is in effect.  The primary library recorded by the static linker when
1155  * resolving a symbol in these libraries will be the umbrella framework.
1156  * Zero or more sub_umbrella frameworks may be use by an umbrella framework.
1157  * The name of a sub_umbrella framework is recorded in the following structure.
1158  */
1159 #[derive(Debug, Clone, Copy)]
1160 #[repr(C)]
1161 pub struct SubUmbrellaCommand<E: Endian> {
1162     /// LC_SUB_UMBRELLA
1163     pub cmd: U32<E>,
1164     /// includes sub_umbrella string
1165     pub cmdsize: U32<E>,
1166     /// the sub_umbrella framework name
1167     pub sub_umbrella: LcStr<E>,
1168 }
1169 
1170 /*
1171  * A dynamically linked shared library may be a sub_library of another shared
1172  * library.  If so it will be linked with "-sub_library library_name" where
1173  * Where "library_name" is the name of the sub_library shared library.  When
1174  * staticly linking when -twolevel_namespace is in effect a twolevel namespace
1175  * shared library will only cause its subframeworks and those frameworks
1176  * listed as sub_umbrella frameworks and libraries listed as sub_libraries to
1177  * be implicited linked in.  Any other dependent dynamic libraries will not be
1178  * linked it when -twolevel_namespace is in effect.  The primary library
1179  * recorded by the static linker when resolving a symbol in these libraries
1180  * will be the umbrella framework (or dynamic library). Zero or more sub_library
1181  * shared libraries may be use by an umbrella framework or (or dynamic library).
1182  * The name of a sub_library framework is recorded in the following structure.
1183  * For example /usr/lib/libobjc_profile.A.dylib would be recorded as "libobjc".
1184  */
1185 #[derive(Debug, Clone, Copy)]
1186 #[repr(C)]
1187 pub struct SubLibraryCommand<E: Endian> {
1188     /// LC_SUB_LIBRARY
1189     pub cmd: U32<E>,
1190     /// includes sub_library string
1191     pub cmdsize: U32<E>,
1192     /// the sub_library name
1193     pub sub_library: LcStr<E>,
1194 }
1195 
1196 /*
1197  * A program (filetype == MH_EXECUTE) that is
1198  * prebound to its dynamic libraries has one of these for each library that
1199  * the static linker used in prebinding.  It contains a bit vector for the
1200  * modules in the library.  The bits indicate which modules are bound (1) and
1201  * which are not (0) from the library.  The bit for module 0 is the low bit
1202  * of the first byte.  So the bit for the Nth module is:
1203  * (linked_modules[N/8] >> N%8) & 1
1204  */
1205 #[derive(Debug, Clone, Copy)]
1206 #[repr(C)]
1207 pub struct PreboundDylibCommand<E: Endian> {
1208     /// LC_PREBOUND_DYLIB
1209     pub cmd: U32<E>,
1210     /// includes strings
1211     pub cmdsize: U32<E>,
1212     /// library's path name
1213     pub name: LcStr<E>,
1214     /// number of modules in library
1215     pub nmodules: U32<E>,
1216     /// bit vector of linked modules
1217     pub linked_modules: LcStr<E>,
1218 }
1219 
1220 /*
1221  * A program that uses a dynamic linker contains a `DylinkerCommand` to identify
1222  * the name of the dynamic linker (LC_LOAD_DYLINKER).  And a dynamic linker
1223  * contains a `DylinkerCommand` to identify the dynamic linker (LC_ID_DYLINKER).
1224  * A file can have at most one of these.
1225  * This struct is also used for the LC_DYLD_ENVIRONMENT load command and
1226  * contains string for dyld to treat like environment variable.
1227  */
1228 #[derive(Debug, Clone, Copy)]
1229 #[repr(C)]
1230 pub struct DylinkerCommand<E: Endian> {
1231     /// LC_ID_DYLINKER, LC_LOAD_DYLINKER or LC_DYLD_ENVIRONMENT
1232     pub cmd: U32<E>,
1233     /// includes pathname string
1234     pub cmdsize: U32<E>,
1235     /// dynamic linker's path name
1236     pub name: LcStr<E>,
1237 }
1238 
1239 /*
1240  * Thread commands contain machine-specific data structures suitable for
1241  * use in the thread state primitives.  The machine specific data structures
1242  * follow the struct `ThreadCommand` as follows.
1243  * Each flavor of machine specific data structure is preceded by an uint32_t
1244  * constant for the flavor of that data structure, an uint32_t that is the
1245  * count of uint32_t's of the size of the state data structure and then
1246  * the state data structure follows.  This triple may be repeated for many
1247  * flavors.  The constants for the flavors, counts and state data structure
1248  * definitions are expected to be in the header file <machine/thread_status.h>.
1249  * These machine specific data structures sizes must be multiples of
1250  * 4 bytes.  The `cmdsize` reflects the total size of the `ThreadCommand`
1251  * and all of the sizes of the constants for the flavors, counts and state
1252  * data structures.
1253  *
1254  * For executable objects that are unix processes there will be one
1255  * `ThreadCommand` (cmd == LC_UNIXTHREAD) created for it by the link-editor.
1256  * This is the same as a LC_THREAD, except that a stack is automatically
1257  * created (based on the shell's limit for the stack size).  Command arguments
1258  * and environment variables are copied onto that stack.
1259  */
1260 #[derive(Debug, Clone, Copy)]
1261 #[repr(C)]
1262 pub struct ThreadCommand<E: Endian> {
1263     /// LC_THREAD or  LC_UNIXTHREAD
1264     pub cmd: U32<E>,
1265     /// total size of this command
1266     pub cmdsize: U32<E>,
1267     /* uint32_t flavor		   flavor of thread state */
1268     /* uint32_t count		   count of uint32_t's in thread state */
1269     /* struct XXX_thread_state state   thread state for this flavor */
1270     /* ... */
1271 }
1272 
1273 /*
1274  * The routines command contains the address of the dynamic shared library
1275  * initialization routine and an index into the module table for the module
1276  * that defines the routine.  Before any modules are used from the library the
1277  * dynamic linker fully binds the module that defines the initialization routine
1278  * and then calls it.  This gets called before any module initialization
1279  * routines (used for C++ static constructors) in the library.
1280  */
1281 #[derive(Debug, Clone, Copy)]
1282 #[repr(C)]
1283 pub struct RoutinesCommand<E: Endian> {
1284     /* for 32-bit architectures */
1285     /// LC_ROUTINES
1286     pub cmd: U32<E>,
1287     /// total size of this command
1288     pub cmdsize: U32<E>,
1289     /// address of initialization routine
1290     pub init_address: U32<E>,
1291     /// index into the module table that the init routine is defined in
1292     pub init_module: U32<E>,
1293     pub reserved1: U32<E>,
1294     pub reserved2: U32<E>,
1295     pub reserved3: U32<E>,
1296     pub reserved4: U32<E>,
1297     pub reserved5: U32<E>,
1298     pub reserved6: U32<E>,
1299 }
1300 
1301 /*
1302  * The 64-bit routines command.  Same use as above.
1303  */
1304 #[derive(Debug, Clone, Copy)]
1305 #[repr(C)]
1306 pub struct RoutinesCommand_64<E: Endian> {
1307     /* for 64-bit architectures */
1308     /// LC_ROUTINES_64
1309     pub cmd: U32<E>,
1310     /// total size of this command
1311     pub cmdsize: U32<E>,
1312     /// address of initialization routine
1313     pub init_address: U64<E>,
1314     /// index into the module table that the init routine is defined in
1315     pub init_module: U64<E>,
1316     pub reserved1: U64<E>,
1317     pub reserved2: U64<E>,
1318     pub reserved3: U64<E>,
1319     pub reserved4: U64<E>,
1320     pub reserved5: U64<E>,
1321     pub reserved6: U64<E>,
1322 }
1323 
1324 /*
1325  * The `SymtabCommand` contains the offsets and sizes of the link-edit 4.3BSD
1326  * "stab" style symbol table information as described in the header files
1327  * <nlist.h> and <stab.h>.
1328  */
1329 #[derive(Debug, Clone, Copy)]
1330 #[repr(C)]
1331 pub struct SymtabCommand<E: Endian> {
1332     /// LC_SYMTAB
1333     pub cmd: U32<E>,
1334     /// sizeof(struct SymtabCommand)
1335     pub cmdsize: U32<E>,
1336     /// symbol table offset
1337     pub symoff: U32<E>,
1338     /// number of symbol table entries
1339     pub nsyms: U32<E>,
1340     /// string table offset
1341     pub stroff: U32<E>,
1342     /// string table size in bytes
1343     pub strsize: U32<E>,
1344 }
1345 
1346 /*
1347  * This is the second set of the symbolic information which is used to support
1348  * the data structures for the dynamically link editor.
1349  *
1350  * The original set of symbolic information in the `SymtabCommand` which contains
1351  * the symbol and string tables must also be present when this load command is
1352  * present.  When this load command is present the symbol table is organized
1353  * into three groups of symbols:
1354  *	local symbols (static and debugging symbols) - grouped by module
1355  *	defined external symbols - grouped by module (sorted by name if not lib)
1356  *	undefined external symbols (sorted by name if MH_BINDATLOAD is not set,
1357  *	     			    and in order the were seen by the static
1358  *				    linker if MH_BINDATLOAD is set)
1359  * In this load command there are offsets and counts to each of the three groups
1360  * of symbols.
1361  *
1362  * This load command contains a the offsets and sizes of the following new
1363  * symbolic information tables:
1364  *	table of contents
1365  *	module table
1366  *	reference symbol table
1367  *	indirect symbol table
1368  * The first three tables above (the table of contents, module table and
1369  * reference symbol table) are only present if the file is a dynamically linked
1370  * shared library.  For executable and object modules, which are files
1371  * containing only one module, the information that would be in these three
1372  * tables is determined as follows:
1373  * 	table of contents - the defined external symbols are sorted by name
1374  *	module table - the file contains only one module so everything in the
1375  *		       file is part of the module.
1376  *	reference symbol table - is the defined and undefined external symbols
1377  *
1378  * For dynamically linked shared library files this load command also contains
1379  * offsets and sizes to the pool of relocation entries for all sections
1380  * separated into two groups:
1381  *	external relocation entries
1382  *	local relocation entries
1383  * For executable and object modules the relocation entries continue to hang
1384  * off the section structures.
1385  */
1386 #[derive(Debug, Clone, Copy)]
1387 #[repr(C)]
1388 pub struct DysymtabCommand<E: Endian> {
1389     /// LC_DYSYMTAB
1390     pub cmd: U32<E>,
1391     /// sizeof(struct DysymtabCommand)
1392     pub cmdsize: U32<E>,
1393 
1394     /*
1395      * The symbols indicated by symoff and nsyms of the LC_SYMTAB load command
1396      * are grouped into the following three groups:
1397      *    local symbols (further grouped by the module they are from)
1398      *    defined external symbols (further grouped by the module they are from)
1399      *    undefined symbols
1400      *
1401      * The local symbols are used only for debugging.  The dynamic binding
1402      * process may have to use them to indicate to the debugger the local
1403      * symbols for a module that is being bound.
1404      *
1405      * The last two groups are used by the dynamic binding process to do the
1406      * binding (indirectly through the module table and the reference symbol
1407      * table when this is a dynamically linked shared library file).
1408      */
1409     /// index to local symbols
1410     pub ilocalsym: U32<E>,
1411     /// number of local symbols
1412     pub nlocalsym: U32<E>,
1413 
1414     /// index to externally defined symbols
1415     pub iextdefsym: U32<E>,
1416     /// number of externally defined symbols
1417     pub nextdefsym: U32<E>,
1418 
1419     /// index to undefined symbols
1420     pub iundefsym: U32<E>,
1421     /// number of undefined symbols
1422     pub nundefsym: U32<E>,
1423 
1424     /*
1425      * For the for the dynamic binding process to find which module a symbol
1426      * is defined in the table of contents is used (analogous to the ranlib
1427      * structure in an archive) which maps defined external symbols to modules
1428      * they are defined in.  This exists only in a dynamically linked shared
1429      * library file.  For executable and object modules the defined external
1430      * symbols are sorted by name and is use as the table of contents.
1431      */
1432     /// file offset to table of contents
1433     pub tocoff: U32<E>,
1434     /// number of entries in table of contents
1435     pub ntoc: U32<E>,
1436 
1437     /*
1438      * To support dynamic binding of "modules" (whole object files) the symbol
1439      * table must reflect the modules that the file was created from.  This is
1440      * done by having a module table that has indexes and counts into the merged
1441      * tables for each module.  The module structure that these two entries
1442      * refer to is described below.  This exists only in a dynamically linked
1443      * shared library file.  For executable and object modules the file only
1444      * contains one module so everything in the file belongs to the module.
1445      */
1446     /// file offset to module table
1447     pub modtaboff: U32<E>,
1448     /// number of module table entries
1449     pub nmodtab: U32<E>,
1450 
1451     /*
1452      * To support dynamic module binding the module structure for each module
1453      * indicates the external references (defined and undefined) each module
1454      * makes.  For each module there is an offset and a count into the
1455      * reference symbol table for the symbols that the module references.
1456      * This exists only in a dynamically linked shared library file.  For
1457      * executable and object modules the defined external symbols and the
1458      * undefined external symbols indicates the external references.
1459      */
1460     /// offset to referenced symbol table
1461     pub extrefsymoff: U32<E>,
1462     /// number of referenced symbol table entries
1463     pub nextrefsyms: U32<E>,
1464 
1465     /*
1466      * The sections that contain "symbol pointers" and "routine stubs" have
1467      * indexes and (implied counts based on the size of the section and fixed
1468      * size of the entry) into the "indirect symbol" table for each pointer
1469      * and stub.  For every section of these two types the index into the
1470      * indirect symbol table is stored in the section header in the field
1471      * reserved1.  An indirect symbol table entry is simply a 32bit index into
1472      * the symbol table to the symbol that the pointer or stub is referring to.
1473      * The indirect symbol table is ordered to match the entries in the section.
1474      */
1475     /// file offset to the indirect symbol table
1476     pub indirectsymoff: U32<E>,
1477     /// number of indirect symbol table entries
1478     pub nindirectsyms: U32<E>,
1479 
1480     /*
1481      * To support relocating an individual module in a library file quickly the
1482      * external relocation entries for each module in the library need to be
1483      * accessed efficiently.  Since the relocation entries can't be accessed
1484      * through the section headers for a library file they are separated into
1485      * groups of local and external entries further grouped by module.  In this
1486      * case the presents of this load command who's extreloff, nextrel,
1487      * locreloff and nlocrel fields are non-zero indicates that the relocation
1488      * entries of non-merged sections are not referenced through the section
1489      * structures (and the reloff and nreloc fields in the section headers are
1490      * set to zero).
1491      *
1492      * Since the relocation entries are not accessed through the section headers
1493      * this requires the r_address field to be something other than a section
1494      * offset to identify the item to be relocated.  In this case r_address is
1495      * set to the offset from the vmaddr of the first LC_SEGMENT command.
1496      * For MH_SPLIT_SEGS images r_address is set to the the offset from the
1497      * vmaddr of the first read-write LC_SEGMENT command.
1498      *
1499      * The relocation entries are grouped by module and the module table
1500      * entries have indexes and counts into them for the group of external
1501      * relocation entries for that the module.
1502      *
1503      * For sections that are merged across modules there must not be any
1504      * remaining external relocation entries for them (for merged sections
1505      * remaining relocation entries must be local).
1506      */
1507     /// offset to external relocation entries
1508     pub extreloff: U32<E>,
1509     /// number of external relocation entries
1510     pub nextrel: U32<E>,
1511 
1512     /*
1513      * All the local relocation entries are grouped together (they are not
1514      * grouped by their module since they are only used if the object is moved
1515      * from it staticly link edited address).
1516      */
1517     /// offset to local relocation entries
1518     pub locreloff: U32<E>,
1519     /// number of local relocation entries
1520     pub nlocrel: U32<E>,
1521 }
1522 
1523 /*
1524  * An indirect symbol table entry is simply a 32bit index into the symbol table
1525  * to the symbol that the pointer or stub is refering to.  Unless it is for a
1526  * non-lazy symbol pointer section for a defined symbol which strip(1) as
1527  * removed.  In which case it has the value INDIRECT_SYMBOL_LOCAL.  If the
1528  * symbol was also absolute INDIRECT_SYMBOL_ABS is or'ed with that.
1529  */
1530 pub const INDIRECT_SYMBOL_LOCAL: u32 = 0x8000_0000;
1531 pub const INDIRECT_SYMBOL_ABS: u32 = 0x4000_0000;
1532 
1533 /* a table of contents entry */
1534 #[derive(Debug, Clone, Copy)]
1535 #[repr(C)]
1536 pub struct DylibTableOfContents<E: Endian> {
1537     /// the defined external symbol (index into the symbol table)
1538     pub symbol_index: U32<E>,
1539     /// index into the module table this symbol is defined in
1540     pub module_index: U32<E>,
1541 }
1542 
1543 /* a module table entry */
1544 #[derive(Debug, Clone, Copy)]
1545 #[repr(C)]
1546 pub struct DylibModule32<E: Endian> {
1547     /// the module name (index into string table)
1548     pub module_name: U32<E>,
1549 
1550     /// index into externally defined symbols
1551     pub iextdefsym: U32<E>,
1552     /// number of externally defined symbols
1553     pub nextdefsym: U32<E>,
1554     /// index into reference symbol table
1555     pub irefsym: U32<E>,
1556     /// number of reference symbol table entries
1557     pub nrefsym: U32<E>,
1558     /// index into symbols for local symbols
1559     pub ilocalsym: U32<E>,
1560     /// number of local symbols
1561     pub nlocalsym: U32<E>,
1562 
1563     /// index into external relocation entries
1564     pub iextrel: U32<E>,
1565     /// number of external relocation entries
1566     pub nextrel: U32<E>,
1567 
1568     /// low 16 bits are the index into the init section, high 16 bits are the index into the term section
1569     pub iinit_iterm: U32<E>,
1570     /// low 16 bits are the number of init section entries, high 16 bits are the number of term section entries
1571     pub ninit_nterm: U32<E>,
1572 
1573     /// for this module address of the start of the (__OBJC,__module_info) section
1574     pub objc_module_info_addr: U32<E>,
1575     /// for this module size of the (__OBJC,__module_info) section
1576     pub objc_module_info_size: U32<E>,
1577 }
1578 
1579 /* a 64-bit module table entry */
1580 #[derive(Debug, Clone, Copy)]
1581 #[repr(C)]
1582 pub struct DylibModule64<E: Endian> {
1583     /// the module name (index into string table)
1584     pub module_name: U32<E>,
1585 
1586     /// index into externally defined symbols
1587     pub iextdefsym: U32<E>,
1588     /// number of externally defined symbols
1589     pub nextdefsym: U32<E>,
1590     /// index into reference symbol table
1591     pub irefsym: U32<E>,
1592     /// number of reference symbol table entries
1593     pub nrefsym: U32<E>,
1594     /// index into symbols for local symbols
1595     pub ilocalsym: U32<E>,
1596     /// number of local symbols
1597     pub nlocalsym: U32<E>,
1598 
1599     /// index into external relocation entries
1600     pub iextrel: U32<E>,
1601     /// number of external relocation entries
1602     pub nextrel: U32<E>,
1603 
1604     /// low 16 bits are the index into the init section, high 16 bits are the index into the term section
1605     pub iinit_iterm: U32<E>,
1606     /// low 16 bits are the number of init section entries, high 16 bits are the number of term section entries
1607     pub ninit_nterm: U32<E>,
1608 
1609     /// for this module size of the (__OBJC,__module_info) section
1610     pub objc_module_info_size: U32<E>,
1611     /// for this module address of the start of the (__OBJC,__module_info) section
1612     pub objc_module_info_addr: U64<E>,
1613 }
1614 
1615 /*
1616  * The entries in the reference symbol table are used when loading the module
1617  * (both by the static and dynamic link editors) and if the module is unloaded
1618  * or replaced.  Therefore all external symbols (defined and undefined) are
1619  * listed in the module's reference table.  The flags describe the type of
1620  * reference that is being made.  The constants for the flags are defined in
1621  * <mach-o/nlist.h> as they are also used for symbol table entries.
1622  */
1623 #[derive(Debug, Clone, Copy)]
1624 #[repr(C)]
1625 pub struct DylibReference<E: Endian> {
1626     /* TODO:
1627     uint32_t isym:24,		/* index into the symbol table */
1628               flags:8;	/* flags to indicate the type of reference */
1629     */
1630     pub bitfield: U32<E>,
1631 }
1632 
1633 /*
1634  * The TwolevelHintsCommand contains the offset and number of hints in the
1635  * two-level namespace lookup hints table.
1636  */
1637 #[derive(Debug, Clone, Copy)]
1638 #[repr(C)]
1639 pub struct TwolevelHintsCommand<E: Endian> {
1640     /// LC_TWOLEVEL_HINTS
1641     pub cmd: U32<E>,
1642     /// sizeof(struct TwolevelHintsCommand)
1643     pub cmdsize: U32<E>,
1644     /// offset to the hint table
1645     pub offset: U32<E>,
1646     /// number of hints in the hint table
1647     pub nhints: U32<E>,
1648 }
1649 
1650 /*
1651  * The entries in the two-level namespace lookup hints table are TwolevelHint
1652  * structs.  These provide hints to the dynamic link editor where to start
1653  * looking for an undefined symbol in a two-level namespace image.  The
1654  * isub_image field is an index into the sub-images (sub-frameworks and
1655  * sub-umbrellas list) that made up the two-level image that the undefined
1656  * symbol was found in when it was built by the static link editor.  If
1657  * isub-image is 0 the the symbol is expected to be defined in library and not
1658  * in the sub-images.  If isub-image is non-zero it is an index into the array
1659  * of sub-images for the umbrella with the first index in the sub-images being
1660  * 1. The array of sub-images is the ordered list of sub-images of the umbrella
1661  * that would be searched for a symbol that has the umbrella recorded as its
1662  * primary library.  The table of contents index is an index into the
1663  * library's table of contents.  This is used as the starting point of the
1664  * binary search or a directed linear search.
1665  */
1666 #[derive(Debug, Clone, Copy)]
1667 #[repr(C)]
1668 pub struct TwolevelHint<E: Endian> {
1669     /* TODO:
1670     uint32_t
1671     isub_image:8,	/* index into the sub images */
1672     itoc:24;	/* index into the table of contents */
1673     */
1674     pub bitfield: U32<E>,
1675 }
1676 
1677 /*
1678  * The PrebindCksumCommand contains the value of the original check sum for
1679  * prebound files or zero.  When a prebound file is first created or modified
1680  * for other than updating its prebinding information the value of the check sum
1681  * is set to zero.  When the file has it prebinding re-done and if the value of
1682  * the check sum is zero the original check sum is calculated and stored in
1683  * cksum field of this load command in the output file.  If when the prebinding
1684  * is re-done and the cksum field is non-zero it is left unchanged from the
1685  * input file.
1686  */
1687 #[derive(Debug, Clone, Copy)]
1688 #[repr(C)]
1689 pub struct PrebindCksumCommand<E: Endian> {
1690     /// LC_PREBIND_CKSUM
1691     pub cmd: U32<E>,
1692     /// sizeof(struct PrebindCksumCommand)
1693     pub cmdsize: U32<E>,
1694     /// the check sum or zero
1695     pub cksum: U32<E>,
1696 }
1697 
1698 /*
1699  * The uuid load command contains a single 128-bit unique random number that
1700  * identifies an object produced by the static link editor.
1701  */
1702 #[derive(Debug, Clone, Copy)]
1703 #[repr(C)]
1704 pub struct UuidCommand<E: Endian> {
1705     /// LC_UUID
1706     pub cmd: U32<E>,
1707     /// sizeof(struct UuidCommand)
1708     pub cmdsize: U32<E>,
1709     /// the 128-bit uuid
1710     pub uuid: [u8; 16],
1711 }
1712 
1713 /*
1714  * The RpathCommand contains a path which at runtime should be added to
1715  * the current run path used to find @rpath prefixed dylibs.
1716  */
1717 #[derive(Debug, Clone, Copy)]
1718 #[repr(C)]
1719 pub struct RpathCommand<E: Endian> {
1720     /// LC_RPATH
1721     pub cmd: U32<E>,
1722     /// includes string
1723     pub cmdsize: U32<E>,
1724     /// path to add to run path
1725     pub path: LcStr<E>,
1726 }
1727 
1728 /*
1729  * The LinkeditDataCommand contains the offsets and sizes of a blob
1730  * of data in the __LINKEDIT segment.
1731  */
1732 #[derive(Debug, Clone, Copy)]
1733 #[repr(C)]
1734 pub struct LinkeditDataCommand<E: Endian> {
1735     /// LC_CODE_SIGNATURE, LC_SEGMENT_SPLIT_INFO, LC_FUNCTION_STARTS, LC_DATA_IN_CODE, LC_DYLIB_CODE_SIGN_DRS or LC_LINKER_OPTIMIZATION_HINT.
1736     pub cmd: U32<E>,
1737     /// sizeof(struct LinkeditDataCommand)
1738     pub cmdsize: U32<E>,
1739     /// file offset of data in __LINKEDIT segment
1740     pub dataoff: U32<E>,
1741     /// file size of data in __LINKEDIT segment
1742     pub datasize: U32<E>,
1743 }
1744 
1745 /*
1746  * The EncryptionInfoCommand contains the file offset and size of an
1747  * of an encrypted segment.
1748  */
1749 #[derive(Debug, Clone, Copy)]
1750 #[repr(C)]
1751 pub struct EncryptionInfoCommand<E: Endian> {
1752     /// LC_ENCRYPTION_INFO
1753     pub cmd: U32<E>,
1754     /// sizeof(struct EncryptionInfoCommand)
1755     pub cmdsize: U32<E>,
1756     /// file offset of encrypted range
1757     pub cryptoff: U32<E>,
1758     /// file size of encrypted range
1759     pub cryptsize: U32<E>,
1760     /// which enryption system, 0 means not-encrypted yet
1761     pub cryptid: U32<E>,
1762 }
1763 
1764 /*
1765  * The EncryptionInfoCommand64 contains the file offset and size of an
1766  * of an encrypted segment (for use in x86_64 targets).
1767  */
1768 #[derive(Debug, Clone, Copy)]
1769 #[repr(C)]
1770 pub struct EncryptionInfoCommand64<E: Endian> {
1771     /// LC_ENCRYPTION_INFO_64
1772     pub cmd: U32<E>,
1773     /// sizeof(struct EncryptionInfoCommand64)
1774     pub cmdsize: U32<E>,
1775     /// file offset of encrypted range
1776     pub cryptoff: U32<E>,
1777     /// file size of encrypted range
1778     pub cryptsize: U32<E>,
1779     /// which enryption system, 0 means not-encrypted yet
1780     pub cryptid: U32<E>,
1781     /// padding to make this struct's size a multiple of 8 bytes
1782     pub pad: U32<E>,
1783 }
1784 
1785 /*
1786  * The VersionMinCommand contains the min OS version on which this
1787  * binary was built to run.
1788  */
1789 #[derive(Debug, Clone, Copy)]
1790 #[repr(C)]
1791 pub struct VersionMinCommand<E: Endian> {
1792     /// LC_VERSION_MIN_MACOSX or LC_VERSION_MIN_IPHONEOS or LC_VERSION_MIN_WATCHOS or LC_VERSION_MIN_TVOS
1793     pub cmd: U32<E>,
1794     /// sizeof(struct VersionMinCommand)
1795     pub cmdsize: U32<E>,
1796     /// X.Y.Z is encoded in nibbles xxxx.yy.zz
1797     pub version: U32<E>,
1798     /// X.Y.Z is encoded in nibbles xxxx.yy.zz
1799     pub sdk: U32<E>,
1800 }
1801 
1802 /*
1803  * The BuildVersionCommand contains the min OS version on which this
1804  * binary was built to run for its platform.  The list of known platforms and
1805  * tool values following it.
1806  */
1807 #[derive(Debug, Clone, Copy)]
1808 #[repr(C)]
1809 pub struct BuildVersionCommand<E: Endian> {
1810     /// LC_BUILD_VERSION
1811     pub cmd: U32<E>,
1812     /// sizeof(struct BuildVersionCommand) plus ntools * sizeof(struct BuildToolVersion)
1813     pub cmdsize: U32<E>,
1814     /// platform
1815     pub platform: U32<E>,
1816     /// X.Y.Z is encoded in nibbles xxxx.yy.zz
1817     pub minos: U32<E>,
1818     /// X.Y.Z is encoded in nibbles xxxx.yy.zz
1819     pub sdk: U32<E>,
1820     /// number of tool entries following this
1821     pub ntools: U32<E>,
1822 }
1823 
1824 #[derive(Debug, Clone, Copy)]
1825 #[repr(C)]
1826 pub struct BuildToolVersion<E: Endian> {
1827     /// enum for the tool
1828     pub tool: U32<E>,
1829     /// version number of the tool
1830     pub version: U32<E>,
1831 }
1832 
1833 /* Known values for the platform field above. */
1834 pub const PLATFORM_MACOS: u32 = 1;
1835 pub const PLATFORM_IOS: u32 = 2;
1836 pub const PLATFORM_TVOS: u32 = 3;
1837 pub const PLATFORM_WATCHOS: u32 = 4;
1838 pub const PLATFORM_BRIDGEOS: u32 = 5;
1839 pub const PLATFORM_IOSMAC: u32 = 6;
1840 pub const PLATFORM_IOSSIMULATOR: u32 = 7;
1841 pub const PLATFORM_TVOSSIMULATOR: u32 = 8;
1842 pub const PLATFORM_WATCHOSSIMULATOR: u32 = 9;
1843 
1844 /* Known values for the tool field above. */
1845 pub const TOOL_CLANG: u32 = 1;
1846 pub const TOOL_SWIFT: u32 = 2;
1847 pub const TOOL_LD: u32 = 3;
1848 
1849 /*
1850  * The DyldInfoCommand contains the file offsets and sizes of
1851  * the new compressed form of the information dyld needs to
1852  * load the image.  This information is used by dyld on Mac OS X
1853  * 10.6 and later.  All information pointed to by this command
1854  * is encoded using byte streams, so no endian swapping is needed
1855  * to interpret it.
1856  */
1857 #[derive(Debug, Clone, Copy)]
1858 #[repr(C)]
1859 pub struct DyldInfoCommand<E: Endian> {
1860     /// LC_DYLD_INFO or LC_DYLD_INFO_ONLY
1861     pub cmd: U32<E>,
1862     /// sizeof(struct DyldInfoCommand)
1863     pub cmdsize: U32<E>,
1864 
1865     /*
1866      * Dyld rebases an image whenever dyld loads it at an address different
1867      * from its preferred address.  The rebase information is a stream
1868      * of byte sized opcodes whose symbolic names start with REBASE_OPCODE_.
1869      * Conceptually the rebase information is a table of tuples:
1870      *    <seg-index, seg-offset, type>
1871      * The opcodes are a compressed way to encode the table by only
1872      * encoding when a column changes.  In addition simple patterns
1873      * like "every n'th offset for m times" can be encoded in a few
1874      * bytes.
1875      */
1876     /// file offset to rebase info
1877     pub rebase_off: U32<E>,
1878     /// size of rebase info
1879     pub rebase_size: U32<E>,
1880 
1881     /*
1882      * Dyld binds an image during the loading process, if the image
1883      * requires any pointers to be initialized to symbols in other images.
1884      * The bind information is a stream of byte sized
1885      * opcodes whose symbolic names start with BIND_OPCODE_.
1886      * Conceptually the bind information is a table of tuples:
1887      *    <seg-index, seg-offset, type, symbol-library-ordinal, symbol-name, addend>
1888      * The opcodes are a compressed way to encode the table by only
1889      * encoding when a column changes.  In addition simple patterns
1890      * like for runs of pointers initialzed to the same value can be
1891      * encoded in a few bytes.
1892      */
1893     /// file offset to binding info
1894     pub bind_off: U32<E>,
1895     /// size of binding info
1896     pub bind_size: U32<E>,
1897 
1898     /*
1899      * Some C++ programs require dyld to unique symbols so that all
1900      * images in the process use the same copy of some code/data.
1901      * This step is done after binding. The content of the weak_bind
1902      * info is an opcode stream like the bind_info.  But it is sorted
1903      * alphabetically by symbol name.  This enable dyld to walk
1904      * all images with weak binding information in order and look
1905      * for collisions.  If there are no collisions, dyld does
1906      * no updating.  That means that some fixups are also encoded
1907      * in the bind_info.  For instance, all calls to "operator new"
1908      * are first bound to libstdc++.dylib using the information
1909      * in bind_info.  Then if some image overrides operator new
1910      * that is detected when the weak_bind information is processed
1911      * and the call to operator new is then rebound.
1912      */
1913     /// file offset to weak binding info
1914     pub weak_bind_off: U32<E>,
1915     /// size of weak binding info
1916     pub weak_bind_size: U32<E>,
1917 
1918     /*
1919      * Some uses of external symbols do not need to be bound immediately.
1920      * Instead they can be lazily bound on first use.  The lazy_bind
1921      * are contains a stream of BIND opcodes to bind all lazy symbols.
1922      * Normal use is that dyld ignores the lazy_bind section when
1923      * loading an image.  Instead the static linker arranged for the
1924      * lazy pointer to initially point to a helper function which
1925      * pushes the offset into the lazy_bind area for the symbol
1926      * needing to be bound, then jumps to dyld which simply adds
1927      * the offset to lazy_bind_off to get the information on what
1928      * to bind.
1929      */
1930     /// file offset to lazy binding info
1931     pub lazy_bind_off: U32<E>,
1932     /// size of lazy binding infs
1933     pub lazy_bind_size: U32<E>,
1934 
1935     /*
1936      * The symbols exported by a dylib are encoded in a trie.  This
1937      * is a compact representation that factors out common prefixes.
1938      * It also reduces LINKEDIT pages in RAM because it encodes all
1939      * information (name, address, flags) in one small, contiguous range.
1940      * The export area is a stream of nodes.  The first node sequentially
1941      * is the start node for the trie.
1942      *
1943      * Nodes for a symbol start with a uleb128 that is the length of
1944      * the exported symbol information for the string so far.
1945      * If there is no exported symbol, the node starts with a zero byte.
1946      * If there is exported info, it follows the length.
1947      *
1948      * First is a uleb128 containing flags. Normally, it is followed by
1949      * a uleb128 encoded offset which is location of the content named
1950      * by the symbol from the mach_header for the image.  If the flags
1951      * is EXPORT_SYMBOL_FLAGS_REEXPORT, then following the flags is
1952      * a uleb128 encoded library ordinal, then a zero terminated
1953      * UTF8 string.  If the string is zero length, then the symbol
1954      * is re-export from the specified dylib with the same name.
1955      * If the flags is EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER, then following
1956      * the flags is two uleb128s: the stub offset and the resolver offset.
1957      * The stub is used by non-lazy pointers.  The resolver is used
1958      * by lazy pointers and must be called to get the actual address to use.
1959      *
1960      * After the optional exported symbol information is a byte of
1961      * how many edges (0-255) that this node has leaving it,
1962      * followed by each edge.
1963      * Each edge is a zero terminated UTF8 of the addition chars
1964      * in the symbol, followed by a uleb128 offset for the node that
1965      * edge points to.
1966      *
1967      */
1968     /// file offset to lazy binding info
1969     pub export_off: U32<E>,
1970     /// size of lazy binding infs
1971     pub export_size: U32<E>,
1972 }
1973 
1974 /*
1975  * The following are used to encode rebasing information
1976  */
1977 pub const REBASE_TYPE_POINTER: u8 = 1;
1978 pub const REBASE_TYPE_TEXT_ABSOLUTE32: u8 = 2;
1979 pub const REBASE_TYPE_TEXT_PCREL32: u8 = 3;
1980 
1981 pub const REBASE_OPCODE_MASK: u8 = 0xF0;
1982 pub const REBASE_IMMEDIATE_MASK: u8 = 0x0F;
1983 pub const REBASE_OPCODE_DONE: u8 = 0x00;
1984 pub const REBASE_OPCODE_SET_TYPE_IMM: u8 = 0x10;
1985 pub const REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: u8 = 0x20;
1986 pub const REBASE_OPCODE_ADD_ADDR_ULEB: u8 = 0x30;
1987 pub const REBASE_OPCODE_ADD_ADDR_IMM_SCALED: u8 = 0x40;
1988 pub const REBASE_OPCODE_DO_REBASE_IMM_TIMES: u8 = 0x50;
1989 pub const REBASE_OPCODE_DO_REBASE_ULEB_TIMES: u8 = 0x60;
1990 pub const REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: u8 = 0x70;
1991 pub const REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: u8 = 0x80;
1992 
1993 /*
1994  * The following are used to encode binding information
1995  */
1996 pub const BIND_TYPE_POINTER: u8 = 1;
1997 pub const BIND_TYPE_TEXT_ABSOLUTE32: u8 = 2;
1998 pub const BIND_TYPE_TEXT_PCREL32: u8 = 3;
1999 
2000 pub const BIND_SPECIAL_DYLIB_SELF: i8 = 0;
2001 pub const BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE: i8 = -1;
2002 pub const BIND_SPECIAL_DYLIB_FLAT_LOOKUP: i8 = -2;
2003 pub const BIND_SPECIAL_DYLIB_WEAK_LOOKUP: i8 = -3;
2004 
2005 pub const BIND_SYMBOL_FLAGS_WEAK_IMPORT: u8 = 0x1;
2006 pub const BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION: u8 = 0x8;
2007 
2008 pub const BIND_OPCODE_MASK: u8 = 0xF0;
2009 pub const BIND_IMMEDIATE_MASK: u8 = 0x0F;
2010 pub const BIND_OPCODE_DONE: u8 = 0x00;
2011 pub const BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: u8 = 0x10;
2012 pub const BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: u8 = 0x20;
2013 pub const BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: u8 = 0x30;
2014 pub const BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: u8 = 0x40;
2015 pub const BIND_OPCODE_SET_TYPE_IMM: u8 = 0x50;
2016 pub const BIND_OPCODE_SET_ADDEND_SLEB: u8 = 0x60;
2017 pub const BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: u8 = 0x70;
2018 pub const BIND_OPCODE_ADD_ADDR_ULEB: u8 = 0x80;
2019 pub const BIND_OPCODE_DO_BIND: u8 = 0x90;
2020 pub const BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: u8 = 0xA0;
2021 pub const BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: u8 = 0xB0;
2022 pub const BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: u8 = 0xC0;
2023 pub const BIND_OPCODE_THREADED: u8 = 0xD0;
2024 pub const BIND_SUBOPCODE_THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB: u8 = 0x00;
2025 pub const BIND_SUBOPCODE_THREADED_APPLY: u8 = 0x01;
2026 
2027 /*
2028  * The following are used on the flags byte of a terminal node
2029  * in the export information.
2030  */
2031 pub const EXPORT_SYMBOL_FLAGS_KIND_MASK: u32 = 0x03;
2032 pub const EXPORT_SYMBOL_FLAGS_KIND_REGULAR: u32 = 0x00;
2033 pub const EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL: u32 = 0x01;
2034 pub const EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE: u32 = 0x02;
2035 pub const EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION: u32 = 0x04;
2036 pub const EXPORT_SYMBOL_FLAGS_REEXPORT: u32 = 0x08;
2037 pub const EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER: u32 = 0x10;
2038 
2039 /*
2040  * The LinkerOptionCommand contains linker options embedded in object files.
2041  */
2042 #[derive(Debug, Clone, Copy)]
2043 #[repr(C)]
2044 pub struct LinkerOptionCommand<E: Endian> {
2045     /// LC_LINKER_OPTION only used in MH_OBJECT filetypes
2046     pub cmd: U32<E>,
2047     pub cmdsize: U32<E>,
2048     /// number of strings
2049     pub count: U32<E>,
2050     /* concatenation of zero terminated UTF8 strings.
2051     Zero filled at end to align */
2052 }
2053 
2054 /*
2055  * The SymSegCommand contains the offset and size of the GNU style
2056  * symbol table information as described in the header file <symseg.h>.
2057  * The symbol roots of the symbol segments must also be aligned properly
2058  * in the file.  So the requirement of keeping the offsets aligned to a
2059  * multiple of a 4 bytes translates to the length field of the symbol
2060  * roots also being a multiple of a long.  Also the padding must again be
2061  * zeroed. (THIS IS OBSOLETE and no longer supported).
2062  */
2063 #[derive(Debug, Clone, Copy)]
2064 #[repr(C)]
2065 pub struct SymSegCommand<E: Endian> {
2066     /// LC_SYMSEG
2067     pub cmd: U32<E>,
2068     /// sizeof(struct SymSegCommand)
2069     pub cmdsize: U32<E>,
2070     /// symbol segment offset
2071     pub offset: U32<E>,
2072     /// symbol segment size in bytes
2073     pub size: U32<E>,
2074 }
2075 
2076 /*
2077  * The IdentCommand contains a free format string table following the
2078  * IdentCommand structure.  The strings are null terminated and the size of
2079  * the command is padded out with zero bytes to a multiple of 4 bytes/
2080  * (THIS IS OBSOLETE and no longer supported).
2081  */
2082 #[derive(Debug, Clone, Copy)]
2083 #[repr(C)]
2084 pub struct IdentCommand<E: Endian> {
2085     /// LC_IDENT
2086     pub cmd: U32<E>,
2087     /// strings that follow this command
2088     pub cmdsize: U32<E>,
2089 }
2090 
2091 /*
2092  * The FvmfileCommand contains a reference to a file to be loaded at the
2093  * specified virtual address.  (Presently, this command is reserved for
2094  * internal use.  The kernel ignores this command when loading a program into
2095  * memory).
2096  */
2097 #[derive(Debug, Clone, Copy)]
2098 #[repr(C)]
2099 pub struct FvmfileCommand<E: Endian> {
2100     /// LC_FVMFILE
2101     pub cmd: U32<E>,
2102     /// includes pathname string
2103     pub cmdsize: U32<E>,
2104     /// files pathname
2105     pub name: LcStr<E>,
2106     /// files virtual address
2107     pub header_addr: U32<E>,
2108 }
2109 
2110 /*
2111  * The EntryPointCommand is a replacement for thread_command.
2112  * It is used for main executables to specify the location (file offset)
2113  * of main().  If -stack_size was used at link time, the stacksize
2114  * field will contain the stack size need for the main thread.
2115  */
2116 #[derive(Debug, Clone, Copy)]
2117 #[repr(C)]
2118 pub struct EntryPointCommand<E: Endian> {
2119     /// LC_MAIN only used in MH_EXECUTE filetypes
2120     pub cmd: U32<E>,
2121     /// 24
2122     pub cmdsize: U32<E>,
2123     /// file (__TEXT) offset of main()
2124     pub entryoff: U64<E>,
2125     /// if not zero, initial stack size
2126     pub stacksize: U64<E>,
2127 }
2128 
2129 /*
2130  * The SourceVersionCommand is an optional load command containing
2131  * the version of the sources used to build the binary.
2132  */
2133 #[derive(Debug, Clone, Copy)]
2134 #[repr(C)]
2135 pub struct SourceVersionCommand<E: Endian> {
2136     /// LC_SOURCE_VERSION
2137     pub cmd: U32<E>,
2138     /// 16
2139     pub cmdsize: U32<E>,
2140     /// A.B.C.D.E packed as a24.b10.c10.d10.e10
2141     pub version: U64<E>,
2142 }
2143 
2144 /*
2145  * The LC_DATA_IN_CODE load commands uses a linkedit_data_command
2146  * to point to an array of DataInCodeEntry entries. Each entry
2147  * describes a range of data in a code section.
2148  */
2149 #[derive(Debug, Clone, Copy)]
2150 #[repr(C)]
2151 pub struct DataInCodeEntry<E: Endian> {
2152     /// from mach_header to start of data range
2153     pub offset: U32<E>,
2154     /// number of bytes in data range
2155     pub length: U16<E>,
2156     /// a DICE_KIND_* value
2157     pub kind: U16<E>,
2158 }
2159 pub const DICE_KIND_DATA: u32 = 0x0001;
2160 pub const DICE_KIND_JUMP_TABLE8: u32 = 0x0002;
2161 pub const DICE_KIND_JUMP_TABLE16: u32 = 0x0003;
2162 pub const DICE_KIND_JUMP_TABLE32: u32 = 0x0004;
2163 pub const DICE_KIND_ABS_JUMP_TABLE32: u32 = 0x0005;
2164 
2165 /*
2166  * Sections of type S_THREAD_LOCAL_VARIABLES contain an array
2167  * of TlvDescriptor structures.
2168  */
2169 /* TODO:
2170 #[derive(Debug, Clone, Copy)]
2171 #[repr(C)]
2172 pub struct TlvDescriptor<E: Endian>
2173 {
2174     void*		(*thunk)(struct TlvDescriptor*);
2175     unsigned long	key;
2176     unsigned long	offset;
2177 }
2178 */
2179 
2180 /*
2181  * LC_NOTE commands describe a region of arbitrary data included in a Mach-O
2182  * file.  Its initial use is to record extra data in MH_CORE files.
2183  */
2184 #[derive(Debug, Clone, Copy)]
2185 #[repr(C)]
2186 pub struct NoteCommand<E: Endian> {
2187     /// LC_NOTE
2188     pub cmd: U32<E>,
2189     /// sizeof(struct NoteCommand)
2190     pub cmdsize: U32<E>,
2191     /// owner name for this LC_NOTE
2192     pub data_owner: [u8; 16],
2193     /// file offset of this data
2194     pub offset: U64<E>,
2195     /// length of data region
2196     pub size: U64<E>,
2197 }
2198 
2199 // Definitions from "/usr/include/mach-o/nlist.h".
2200 
2201 #[derive(Debug, Clone, Copy)]
2202 #[repr(C)]
2203 pub struct Nlist32<E: Endian> {
2204     /// index into the string table
2205     pub n_strx: U32<E>,
2206     /// type flag, see below
2207     pub n_type: u8,
2208     /// section number or NO_SECT
2209     pub n_sect: u8,
2210     /// see <mach-o/stab.h>
2211     pub n_desc: U16<E>,
2212     /// value of this symbol (or stab offset)
2213     pub n_value: U32<E>,
2214 }
2215 
2216 /*
2217  * This is the symbol table entry structure for 64-bit architectures.
2218  */
2219 #[derive(Debug, Clone, Copy)]
2220 #[repr(C)]
2221 pub struct Nlist64<E: Endian> {
2222     /// index into the string table
2223     pub n_strx: U32<E>,
2224     /// type flag, see below
2225     pub n_type: u8,
2226     /// section number or NO_SECT
2227     pub n_sect: u8,
2228     /// see <mach-o/stab.h>
2229     pub n_desc: U16<E>,
2230     /// value of this symbol (or stab offset)
2231     // Note: 4 byte alignment has been observed in practice.
2232     pub n_value: U64Bytes<E>,
2233 }
2234 
2235 /*
2236  * Symbols with a index into the string table of zero (n_un.n_strx == 0) are
2237  * defined to have a null, "", name.  Therefore all string indexes to non null
2238  * names must not have a zero string index.  This is bit historical information
2239  * that has never been well documented.
2240  */
2241 
2242 /*
2243  * The n_type field really contains four fields:
2244  *	unsigned char N_STAB:3,
2245  *		      N_PEXT:1,
2246  *		      N_TYPE:3,
2247  *		      N_EXT:1;
2248  * which are used via the following masks.
2249  */
2250 /// if any of these bits set, a symbolic debugging entry
2251 pub const N_STAB: u8 = 0xe0;
2252 /// private external symbol bit
2253 pub const N_PEXT: u8 = 0x10;
2254 /// mask for the type bits
2255 pub const N_TYPE: u8 = 0x0e;
2256 /// external symbol bit, set for external symbols
2257 pub const N_EXT: u8 = 0x01;
2258 
2259 /*
2260  * Only symbolic debugging entries have some of the N_STAB bits set and if any
2261  * of these bits are set then it is a symbolic debugging entry (a stab).  In
2262  * which case then the values of the n_type field (the entire field) are given
2263  * in <mach-o/stab.h>
2264  */
2265 
2266 /*
2267  * Values for N_TYPE bits of the n_type field.
2268  */
2269 /// undefined, n_sect == NO_SECT
2270 pub const N_UNDF: u8 = 0x0;
2271 /// absolute, n_sect == NO_SECT
2272 pub const N_ABS: u8 = 0x2;
2273 /// defined in section number n_sect
2274 pub const N_SECT: u8 = 0xe;
2275 /// prebound undefined (defined in a dylib)
2276 pub const N_PBUD: u8 = 0xc;
2277 /// indirect
2278 pub const N_INDR: u8 = 0xa;
2279 
2280 /*
2281  * If the type is N_INDR then the symbol is defined to be the same as another
2282  * symbol.  In this case the n_value field is an index into the string table
2283  * of the other symbol's name.  When the other symbol is defined then they both
2284  * take on the defined type and value.
2285  */
2286 
2287 /*
2288  * If the type is N_SECT then the n_sect field contains an ordinal of the
2289  * section the symbol is defined in.  The sections are numbered from 1 and
2290  * refer to sections in order they appear in the load commands for the file
2291  * they are in.  This means the same ordinal may very well refer to different
2292  * sections in different files.
2293  *
2294  * The n_value field for all symbol table entries (including N_STAB's) gets
2295  * updated by the link editor based on the value of it's n_sect field and where
2296  * the section n_sect references gets relocated.  If the value of the n_sect
2297  * field is NO_SECT then it's n_value field is not changed by the link editor.
2298  */
2299 /// symbol is not in any section
2300 pub const NO_SECT: u8 = 0;
2301 /// 1 thru 255 inclusive
2302 pub const MAX_SECT: u8 = 255;
2303 
2304 /*
2305  * Common symbols are represented by undefined (N_UNDF) external (N_EXT) types
2306  * who's values (n_value) are non-zero.  In which case the value of the n_value
2307  * field is the size (in bytes) of the common symbol.  The n_sect field is set
2308  * to NO_SECT.  The alignment of a common symbol may be set as a power of 2
2309  * between 2^1 and 2^15 as part of the n_desc field using the macros below. If
2310  * the alignment is not set (a value of zero) then natural alignment based on
2311  * the size is used.
2312  */
2313 /* TODO:
2314 #define GET_COMM_ALIGN(n_desc) (((n_desc) >> 8) & 0x0f)
2315 #define SET_COMM_ALIGN(n_desc,align) \
2316     (n_desc) = (((n_desc) & 0xf0ff) | (((align) & 0x0f) << 8))
2317  */
2318 
2319 /*
2320  * To support the lazy binding of undefined symbols in the dynamic link-editor,
2321  * the undefined symbols in the symbol table (the nlist structures) are marked
2322  * with the indication if the undefined reference is a lazy reference or
2323  * non-lazy reference.  If both a non-lazy reference and a lazy reference is
2324  * made to the same symbol the non-lazy reference takes precedence.  A reference
2325  * is lazy only when all references to that symbol are made through a symbol
2326  * pointer in a lazy symbol pointer section.
2327  *
2328  * The implementation of marking nlist structures in the symbol table for
2329  * undefined symbols will be to use some of the bits of the n_desc field as a
2330  * reference type.  The mask REFERENCE_TYPE will be applied to the n_desc field
2331  * of an nlist structure for an undefined symbol to determine the type of
2332  * undefined reference (lazy or non-lazy).
2333  *
2334  * The constants for the REFERENCE FLAGS are propagated to the reference table
2335  * in a shared library file.  In that case the constant for a defined symbol,
2336  * REFERENCE_FLAG_DEFINED, is also used.
2337  */
2338 /* Reference type bits of the n_desc field of undefined symbols */
2339 pub const REFERENCE_TYPE: u16 = 0x7;
2340 /* types of references */
2341 pub const REFERENCE_FLAG_UNDEFINED_NON_LAZY: u16 = 0;
2342 pub const REFERENCE_FLAG_UNDEFINED_LAZY: u16 = 1;
2343 pub const REFERENCE_FLAG_DEFINED: u16 = 2;
2344 pub const REFERENCE_FLAG_PRIVATE_DEFINED: u16 = 3;
2345 pub const REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY: u16 = 4;
2346 pub const REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY: u16 = 5;
2347 
2348 /*
2349  * To simplify stripping of objects that use are used with the dynamic link
2350  * editor, the static link editor marks the symbols defined an object that are
2351  * referenced by a dynamicly bound object (dynamic shared libraries, bundles).
2352  * With this marking strip knows not to strip these symbols.
2353  */
2354 pub const REFERENCED_DYNAMICALLY: u16 = 0x0010;
2355 
2356 /*
2357  * For images created by the static link editor with the -twolevel_namespace
2358  * option in effect the flags field of the mach header is marked with
2359  * MH_TWOLEVEL.  And the binding of the undefined references of the image are
2360  * determined by the static link editor.  Which library an undefined symbol is
2361  * bound to is recorded by the static linker in the high 8 bits of the n_desc
2362  * field using the SET_LIBRARY_ORDINAL macro below.  The ordinal recorded
2363  * references the libraries listed in the Mach-O's LC_LOAD_DYLIB,
2364  * LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB, LC_LOAD_UPWARD_DYLIB, and
2365  * LC_LAZY_LOAD_DYLIB, etc. load commands in the order they appear in the
2366  * headers.   The library ordinals start from 1.
2367  * For a dynamic library that is built as a two-level namespace image the
2368  * undefined references from module defined in another use the same nlist struct
2369  * an in that case SELF_LIBRARY_ORDINAL is used as the library ordinal.  For
2370  * defined symbols in all images they also must have the library ordinal set to
2371  * SELF_LIBRARY_ORDINAL.  The EXECUTABLE_ORDINAL refers to the executable
2372  * image for references from plugins that refer to the executable that loads
2373  * them.
2374  *
2375  * The DYNAMIC_LOOKUP_ORDINAL is for undefined symbols in a two-level namespace
2376  * image that are looked up by the dynamic linker with flat namespace semantics.
2377  * This ordinal was added as a feature in Mac OS X 10.3 by reducing the
2378  * value of MAX_LIBRARY_ORDINAL by one.  So it is legal for existing binaries
2379  * or binaries built with older tools to have 0xfe (254) dynamic libraries.  In
2380  * this case the ordinal value 0xfe (254) must be treated as a library ordinal
2381  * for compatibility.
2382  */
2383 /* TODO:
2384 #define GET_LIBRARY_ORDINAL(n_desc) (((n_desc) >> 8) & 0xff)
2385 #define SET_LIBRARY_ORDINAL(n_desc,ordinal) \
2386     (n_desc) = (((n_desc) & 0x00ff) | (((ordinal) & 0xff) << 8))
2387  */
2388 pub const SELF_LIBRARY_ORDINAL: u8 = 0x0;
2389 pub const MAX_LIBRARY_ORDINAL: u8 = 0xfd;
2390 pub const DYNAMIC_LOOKUP_ORDINAL: u8 = 0xfe;
2391 pub const EXECUTABLE_ORDINAL: u8 = 0xff;
2392 
2393 /*
2394  * The bit 0x0020 of the n_desc field is used for two non-overlapping purposes
2395  * and has two different symbolic names, N_NO_DEAD_STRIP and N_DESC_DISCARDED.
2396  */
2397 
2398 /*
2399  * The N_NO_DEAD_STRIP bit of the n_desc field only ever appears in a
2400  * relocatable .o file (MH_OBJECT filetype). And is used to indicate to the
2401  * static link editor it is never to dead strip the symbol.
2402  */
2403 /// symbol is not to be dead stripped
2404 pub const N_NO_DEAD_STRIP: u16 = 0x0020;
2405 
2406 /*
2407  * The N_DESC_DISCARDED bit of the n_desc field never appears in linked image.
2408  * But is used in very rare cases by the dynamic link editor to mark an in
2409  * memory symbol as discared and longer used for linking.
2410  */
2411 /// symbol is discarded
2412 pub const N_DESC_DISCARDED: u16 = 0x0020;
2413 
2414 /*
2415  * The N_WEAK_REF bit of the n_desc field indicates to the dynamic linker that
2416  * the undefined symbol is allowed to be missing and is to have the address of
2417  * zero when missing.
2418  */
2419 /// symbol is weak referenced
2420 pub const N_WEAK_REF: u16 = 0x0040;
2421 
2422 /*
2423  * The N_WEAK_DEF bit of the n_desc field indicates to the static and dynamic
2424  * linkers that the symbol definition is weak, allowing a non-weak symbol to
2425  * also be used which causes the weak definition to be discared.  Currently this
2426  * is only supported for symbols in coalesed sections.
2427  */
2428 /// coalesed symbol is a weak definition
2429 pub const N_WEAK_DEF: u16 = 0x0080;
2430 
2431 /*
2432  * The N_REF_TO_WEAK bit of the n_desc field indicates to the dynamic linker
2433  * that the undefined symbol should be resolved using flat namespace searching.
2434  */
2435 /// reference to a weak symbol
2436 pub const N_REF_TO_WEAK: u16 = 0x0080;
2437 
2438 /*
2439  * The N_ARM_THUMB_DEF bit of the n_desc field indicates that the symbol is
2440  * a defintion of a Thumb function.
2441  */
2442 /// symbol is a Thumb function (ARM)
2443 pub const N_ARM_THUMB_DEF: u16 = 0x0008;
2444 
2445 /*
2446  * The N_SYMBOL_RESOLVER bit of the n_desc field indicates that the
2447  * that the function is actually a resolver function and should
2448  * be called to get the address of the real function to use.
2449  * This bit is only available in .o files (MH_OBJECT filetype)
2450  */
2451 pub const N_SYMBOL_RESOLVER: u16 = 0x0100;
2452 
2453 /*
2454  * The N_ALT_ENTRY bit of the n_desc field indicates that the
2455  * symbol is pinned to the previous content.
2456  */
2457 pub const N_ALT_ENTRY: u16 = 0x0200;
2458 
2459 // Definitions from "/usr/include/mach-o/stab.h".
2460 
2461 /*
2462  * This file gives definitions supplementing <nlist.h> for permanent symbol
2463  * table entries of Mach-O files.  Modified from the BSD definitions.  The
2464  * modifications from the original definitions were changing what the values of
2465  * what was the n_other field (an unused field) which is now the n_sect field.
2466  * These modifications are required to support symbols in an arbitrary number of
2467  * sections not just the three sections (text, data and bss) in a BSD file.
2468  * The values of the defined constants have NOT been changed.
2469  *
2470  * These must have one of the N_STAB bits on.  The n_value fields are subject
2471  * to relocation according to the value of their n_sect field.  So for types
2472  * that refer to things in sections the n_sect field must be filled in with the
2473  * proper section ordinal.  For types that are not to have their n_value field
2474  * relocatated the n_sect field must be NO_SECT.
2475  */
2476 
2477 /*
2478  * Symbolic debugger symbols.  The comments give the conventional use for
2479  *
2480  * 	.stabs "n_name", n_type, n_sect, n_desc, n_value
2481  *
2482  * where n_type is the defined constant and not listed in the comment.  Other
2483  * fields not listed are zero. n_sect is the section ordinal the entry is
2484  * refering to.
2485  */
2486 /// global symbol: name,,NO_SECT,type,0
2487 pub const N_GSYM: u8 = 0x20;
2488 /// procedure name (f77 kludge): name,,NO_SECT,0,0
2489 pub const N_FNAME: u8 = 0x22;
2490 /// procedure: name,,n_sect,linenumber,address
2491 pub const N_FUN: u8 = 0x24;
2492 /// static symbol: name,,n_sect,type,address
2493 pub const N_STSYM: u8 = 0x26;
2494 /// .lcomm symbol: name,,n_sect,type,address
2495 pub const N_LCSYM: u8 = 0x28;
2496 /// begin nsect sym: 0,,n_sect,0,address
2497 pub const N_BNSYM: u8 = 0x2e;
2498 /// AST file path: name,,NO_SECT,0,0
2499 pub const N_AST: u8 = 0x32;
2500 /// emitted with gcc2_compiled and in gcc source
2501 pub const N_OPT: u8 = 0x3c;
2502 /// register sym: name,,NO_SECT,type,register
2503 pub const N_RSYM: u8 = 0x40;
2504 /// src line: 0,,n_sect,linenumber,address
2505 pub const N_SLINE: u8 = 0x44;
2506 /// end nsect sym: 0,,n_sect,0,address
2507 pub const N_ENSYM: u8 = 0x4e;
2508 /// structure elt: name,,NO_SECT,type,struct_offset
2509 pub const N_SSYM: u8 = 0x60;
2510 /// source file name: name,,n_sect,0,address
2511 pub const N_SO: u8 = 0x64;
2512 /// object file name: name,,0,0,st_mtime
2513 pub const N_OSO: u8 = 0x66;
2514 /// local sym: name,,NO_SECT,type,offset
2515 pub const N_LSYM: u8 = 0x80;
2516 /// include file beginning: name,,NO_SECT,0,sum
2517 pub const N_BINCL: u8 = 0x82;
2518 /// #included file name: name,,n_sect,0,address
2519 pub const N_SOL: u8 = 0x84;
2520 /// compiler parameters: name,,NO_SECT,0,0
2521 pub const N_PARAMS: u8 = 0x86;
2522 /// compiler version: name,,NO_SECT,0,0
2523 pub const N_VERSION: u8 = 0x88;
2524 /// compiler -O level: name,,NO_SECT,0,0
2525 pub const N_OLEVEL: u8 = 0x8A;
2526 /// parameter: name,,NO_SECT,type,offset
2527 pub const N_PSYM: u8 = 0xa0;
2528 /// include file end: name,,NO_SECT,0,0
2529 pub const N_EINCL: u8 = 0xa2;
2530 /// alternate entry: name,,n_sect,linenumber,address
2531 pub const N_ENTRY: u8 = 0xa4;
2532 /// left bracket: 0,,NO_SECT,nesting level,address
2533 pub const N_LBRAC: u8 = 0xc0;
2534 /// deleted include file: name,,NO_SECT,0,sum
2535 pub const N_EXCL: u8 = 0xc2;
2536 /// right bracket: 0,,NO_SECT,nesting level,address
2537 pub const N_RBRAC: u8 = 0xe0;
2538 /// begin common: name,,NO_SECT,0,0
2539 pub const N_BCOMM: u8 = 0xe2;
2540 /// end common: name,,n_sect,0,0
2541 pub const N_ECOMM: u8 = 0xe4;
2542 /// end common (local name): 0,,n_sect,0,address
2543 pub const N_ECOML: u8 = 0xe8;
2544 /// second stab entry with length information
2545 pub const N_LENG: u8 = 0xfe;
2546 
2547 /*
2548  * for the berkeley pascal compiler, pc(1):
2549  */
2550 /// global pascal symbol: name,,NO_SECT,subtype,line
2551 pub const N_PC: u8 = 0x30;
2552 
2553 // Definitions from "/usr/include/mach-o/reloc.h".
2554 
2555 /// A relocation entry.
2556 ///
2557 /// Mach-O relocations have plain and scattered variants, with the
2558 /// meaning of the fields depending on the variant.
2559 ///
2560 /// This type provides functions for determining whether the relocation
2561 /// is scattered, and for accessing the fields of each variant.
2562 #[derive(Debug, Clone, Copy)]
2563 #[repr(C)]
2564 pub struct Relocation<E: Endian> {
2565     pub r_word0: U32<E>,
2566     pub r_word1: U32<E>,
2567 }
2568 
2569 impl<E: Endian> Relocation<E> {
2570     /// Determine whether this is a scattered relocation.
2571     #[inline]
r_scattered(self, endian: E, cputype: u32) -> bool2572     pub fn r_scattered(self, endian: E, cputype: u32) -> bool {
2573         if cputype == CPU_TYPE_X86_64 {
2574             false
2575         } else {
2576             self.r_word0.get(endian) & R_SCATTERED != 0
2577         }
2578     }
2579 
2580     /// Return the fields of a plain relocation.
info(self, endian: E) -> RelocationInfo2581     pub fn info(self, endian: E) -> RelocationInfo {
2582         let r_address = self.r_word0.get(endian);
2583         let r_word1 = self.r_word1.get(endian);
2584         if endian.is_little_endian() {
2585             RelocationInfo {
2586                 r_address,
2587                 r_symbolnum: r_word1 & 0x00ff_ffff,
2588                 r_pcrel: ((r_word1 >> 24) & 0x1) != 0,
2589                 r_length: ((r_word1 >> 25) & 0x3) as u8,
2590                 r_extern: ((r_word1 >> 27) & 0x1) != 0,
2591                 r_type: (r_word1 >> 28) as u8,
2592             }
2593         } else {
2594             RelocationInfo {
2595                 r_address,
2596                 r_symbolnum: r_word1 >> 8,
2597                 r_pcrel: ((r_word1 >> 7) & 0x1) != 0,
2598                 r_length: ((r_word1 >> 5) & 0x3) as u8,
2599                 r_extern: ((r_word1 >> 4) & 0x1) != 0,
2600                 r_type: (r_word1 & 0xf) as u8,
2601             }
2602         }
2603     }
2604 
2605     /// Return the fields of a scattered relocation.
scattered_info(self, endian: E) -> ScatteredRelocationInfo2606     pub fn scattered_info(self, endian: E) -> ScatteredRelocationInfo {
2607         let r_word0 = self.r_word0.get(endian);
2608         let r_value = self.r_word1.get(endian);
2609         ScatteredRelocationInfo {
2610             r_address: r_word0 & 0x00ff_ffff,
2611             r_type: ((r_word0 >> 24) & 0xf) as u8,
2612             r_length: ((r_word0 >> 28) & 0x3) as u8,
2613             r_pcrel: ((r_word0 >> 30) & 0x1) != 0,
2614             r_value,
2615         }
2616     }
2617 }
2618 
2619 /*
2620  * Format of a relocation entry of a Mach-O file.  Modified from the 4.3BSD
2621  * format.  The modifications from the original format were changing the value
2622  * of the r_symbolnum field for "local" (r_extern == 0) relocation entries.
2623  * This modification is required to support symbols in an arbitrary number of
2624  * sections not just the three sections (text, data and bss) in a 4.3BSD file.
2625  * Also the last 4 bits have had the r_type tag added to them.
2626  */
2627 
2628 #[derive(Debug, Clone, Copy)]
2629 pub struct RelocationInfo {
2630     /// offset in the section to what is being relocated
2631     pub r_address: u32,
2632     /// symbol index if r_extern == 1 or section ordinal if r_extern == 0
2633     pub r_symbolnum: u32,
2634     /// was relocated pc relative already
2635     pub r_pcrel: bool,
2636     /// 0=byte, 1=word, 2=long, 3=quad
2637     pub r_length: u8,
2638     /// does not include value of sym referenced
2639     pub r_extern: bool,
2640     /// if not 0, machine specific relocation type
2641     pub r_type: u8,
2642 }
2643 
2644 impl RelocationInfo {
2645     /// Combine the fields into a `Relocation`.
relocation<E: Endian>(self, endian: E) -> Relocation<E>2646     pub fn relocation<E: Endian>(self, endian: E) -> Relocation<E> {
2647         let r_word0 = U32::new(endian, self.r_address);
2648         let r_word1 = U32::new(
2649             endian,
2650             if endian.is_little_endian() {
2651                 self.r_symbolnum & 0x00ff_ffff
2652                     | u32::from(self.r_pcrel) << 24
2653                     | u32::from(self.r_length & 0x3) << 25
2654                     | u32::from(self.r_extern) << 27
2655                     | u32::from(self.r_type) << 28
2656             } else {
2657                 self.r_symbolnum >> 8
2658                     | u32::from(self.r_pcrel) << 7
2659                     | u32::from(self.r_length & 0x3) << 5
2660                     | u32::from(self.r_extern) << 4
2661                     | u32::from(self.r_type) & 0xf
2662             },
2663         );
2664         Relocation { r_word0, r_word1 }
2665     }
2666 }
2667 
2668 /// absolute relocation type for Mach-O files
2669 pub const R_ABS: u8 = 0;
2670 
2671 /*
2672  * The r_address is not really the address as it's name indicates but an offset.
2673  * In 4.3BSD a.out objects this offset is from the start of the "segment" for
2674  * which relocation entry is for (text or data).  For Mach-O object files it is
2675  * also an offset but from the start of the "section" for which the relocation
2676  * entry is for.  See comments in <mach-o/loader.h> about the r_address feild
2677  * in images for used with the dynamic linker.
2678  *
2679  * In 4.3BSD a.out objects if r_extern is zero then r_symbolnum is an ordinal
2680  * for the segment the symbol being relocated is in.  These ordinals are the
2681  * symbol types N_TEXT, N_DATA, N_BSS or N_ABS.  In Mach-O object files these
2682  * ordinals refer to the sections in the object file in the order their section
2683  * structures appear in the headers of the object file they are in.  The first
2684  * section has the ordinal 1, the second 2, and so on.  This means that the
2685  * same ordinal in two different object files could refer to two different
2686  * sections.  And further could have still different ordinals when combined
2687  * by the link-editor.  The value R_ABS is used for relocation entries for
2688  * absolute symbols which need no further relocation.
2689  */
2690 
2691 /*
2692  * For RISC machines some of the references are split across two instructions
2693  * and the instruction does not contain the complete value of the reference.
2694  * In these cases a second, or paired relocation entry, follows each of these
2695  * relocation entries, using a PAIR r_type, which contains the other part of the
2696  * reference not contained in the instruction.  This other part is stored in the
2697  * pair's r_address field.  The exact number of bits of the other part of the
2698  * reference store in the r_address field is dependent on the particular
2699  * relocation type for the particular architecture.
2700  */
2701 
2702 /*
2703  * To make scattered loading by the link editor work correctly "local"
2704  * relocation entries can't be used when the item to be relocated is the value
2705  * of a symbol plus an offset (where the resulting expresion is outside the
2706  * block the link editor is moving, a blocks are divided at symbol addresses).
2707  * In this case. where the item is a symbol value plus offset, the link editor
2708  * needs to know more than just the section the symbol was defined.  What is
2709  * needed is the actual value of the symbol without the offset so it can do the
2710  * relocation correctly based on where the value of the symbol got relocated to
2711  * not the value of the expression (with the offset added to the symbol value).
2712  * So for the NeXT 2.0 release no "local" relocation entries are ever used when
2713  * there is a non-zero offset added to a symbol.  The "external" and "local"
2714  * relocation entries remain unchanged.
2715  *
2716  * The implemention is quite messy given the compatibility with the existing
2717  * relocation entry format.  The ASSUMPTION is that a section will never be
2718  * bigger than 2**24 - 1 (0x00ffffff or 16,777,215) bytes.  This assumption
2719  * allows the r_address (which is really an offset) to fit in 24 bits and high
2720  * bit of the r_address field in the relocation_info structure to indicate
2721  * it is really a scattered_relocation_info structure.  Since these are only
2722  * used in places where "local" relocation entries are used and not where
2723  * "external" relocation entries are used the r_extern field has been removed.
2724  *
2725  * For scattered loading to work on a RISC machine where some of the references
2726  * are split across two instructions the link editor needs to be assured that
2727  * each reference has a unique 32 bit reference (that more than one reference is
2728  * NOT sharing the same high 16 bits for example) so it move each referenced
2729  * item independent of each other.  Some compilers guarantees this but the
2730  * compilers don't so scattered loading can be done on those that do guarantee
2731  * this.
2732  */
2733 
2734 /// Bit set in `Relocation::r_word0` for scattered relocations.
2735 pub const R_SCATTERED: u32 = 0x8000_0000;
2736 
2737 #[derive(Debug, Clone, Copy)]
2738 pub struct ScatteredRelocationInfo {
2739     /// offset in the section to what is being relocated
2740     pub r_address: u32,
2741     /// if not 0, machine specific relocation type
2742     pub r_type: u8,
2743     /// 0=byte, 1=word, 2=long, 3=quad
2744     pub r_length: u8,
2745     /// was relocated pc relative already
2746     pub r_pcrel: bool,
2747     /// the value the item to be relocated is refering to (without any offset added)
2748     pub r_value: u32,
2749 }
2750 
2751 impl ScatteredRelocationInfo {
2752     /// Combine the fields into a `Relocation`.
relocation<E: Endian>(self, endian: E) -> Relocation<E>2753     pub fn relocation<E: Endian>(self, endian: E) -> Relocation<E> {
2754         let r_word0 = U32::new(
2755             endian,
2756             self.r_address & 0x00ff_ffff
2757                 | u32::from(self.r_type & 0xf) << 24
2758                 | u32::from(self.r_length & 0x3) << 28
2759                 | u32::from(self.r_pcrel) << 30
2760                 | R_SCATTERED,
2761         );
2762         let r_word1 = U32::new(endian, self.r_value);
2763         Relocation { r_word0, r_word1 }
2764     }
2765 }
2766 
2767 /*
2768  * Relocation types used in a generic implementation.  Relocation entries for
2769  * normal things use the generic relocation as discribed above and their r_type
2770  * is GENERIC_RELOC_VANILLA (a value of zero).
2771  *
2772  * Another type of generic relocation, GENERIC_RELOC_SECTDIFF, is to support
2773  * the difference of two symbols defined in different sections.  That is the
2774  * expression "symbol1 - symbol2 + constant" is a relocatable expression when
2775  * both symbols are defined in some section.  For this type of relocation the
2776  * both relocations entries are scattered relocation entries.  The value of
2777  * symbol1 is stored in the first relocation entry's r_value field and the
2778  * value of symbol2 is stored in the pair's r_value field.
2779  *
2780  * A special case for a prebound lazy pointer is needed to beable to set the
2781  * value of the lazy pointer back to its non-prebound state.  This is done
2782  * using the GENERIC_RELOC_PB_LA_PTR r_type.  This is a scattered relocation
2783  * entry where the r_value feild is the value of the lazy pointer not prebound.
2784  */
2785 /// generic relocation as discribed above
2786 pub const GENERIC_RELOC_VANILLA: u8 = 0;
2787 /// Only follows a GENERIC_RELOC_SECTDIFF
2788 pub const GENERIC_RELOC_PAIR: u8 = 1;
2789 pub const GENERIC_RELOC_SECTDIFF: u8 = 2;
2790 /// prebound lazy pointer
2791 pub const GENERIC_RELOC_PB_LA_PTR: u8 = 3;
2792 pub const GENERIC_RELOC_LOCAL_SECTDIFF: u8 = 4;
2793 /// thread local variables
2794 pub const GENERIC_RELOC_TLV: u8 = 5;
2795 
2796 // Definitions from "/usr/include/mach-o/arm/reloc.h".
2797 
2798 /*
2799  * Relocation types used in the arm implementation.  Relocation entries for
2800  * things other than instructions use the same generic relocation as discribed
2801  * in <mach-o/reloc.h> and their r_type is ARM_RELOC_VANILLA, one of the
2802  * *_SECTDIFF or the *_PB_LA_PTR types.  The rest of the relocation types are
2803  * for instructions.  Since they are for instructions the r_address field
2804  * indicates the 32 bit instruction that the relocation is to be preformed on.
2805  */
2806 /// generic relocation as discribed above
2807 pub const ARM_RELOC_VANILLA: u8 = 0;
2808 /// the second relocation entry of a pair
2809 pub const ARM_RELOC_PAIR: u8 = 1;
2810 /// a PAIR follows with subtract symbol value
2811 pub const ARM_RELOC_SECTDIFF: u8 = 2;
2812 /// like ARM_RELOC_SECTDIFF, but the symbol referenced was local.
2813 pub const ARM_RELOC_LOCAL_SECTDIFF: u8 = 3;
2814 /// prebound lazy pointer
2815 pub const ARM_RELOC_PB_LA_PTR: u8 = 4;
2816 /// 24 bit branch displacement (to a word address)
2817 pub const ARM_RELOC_BR24: u8 = 5;
2818 /// 22 bit branch displacement (to a half-word address)
2819 pub const ARM_THUMB_RELOC_BR22: u8 = 6;
2820 /// obsolete - a thumb 32-bit branch instruction possibly needing page-spanning branch workaround
2821 pub const ARM_THUMB_32BIT_BRANCH: u8 = 7;
2822 
2823 /*
2824  * For these two r_type relocations they always have a pair following them
2825  * and the r_length bits are used differently.  The encoding of the
2826  * r_length is as follows:
2827  * low bit of r_length:
2828  *  0 - :lower16: for movw instructions
2829  *  1 - :upper16: for movt instructions
2830  * high bit of r_length:
2831  *  0 - arm instructions
2832  *  1 - thumb instructions
2833  * the other half of the relocated expression is in the following pair
2834  * relocation entry in the the low 16 bits of r_address field.
2835  */
2836 pub const ARM_RELOC_HALF: u8 = 8;
2837 pub const ARM_RELOC_HALF_SECTDIFF: u8 = 9;
2838 
2839 // Definitions from "/usr/include/mach-o/arm64/reloc.h".
2840 
2841 /*
2842  * Relocation types used in the arm64 implementation.
2843  */
2844 /// for pointers
2845 pub const ARM64_RELOC_UNSIGNED: u8 = 0;
2846 /// must be followed by a ARM64_RELOC_UNSIGNED
2847 pub const ARM64_RELOC_SUBTRACTOR: u8 = 1;
2848 /// a B/BL instruction with 26-bit displacement
2849 pub const ARM64_RELOC_BRANCH26: u8 = 2;
2850 /// pc-rel distance to page of target
2851 pub const ARM64_RELOC_PAGE21: u8 = 3;
2852 /// offset within page, scaled by r_length
2853 pub const ARM64_RELOC_PAGEOFF12: u8 = 4;
2854 /// pc-rel distance to page of GOT slot
2855 pub const ARM64_RELOC_GOT_LOAD_PAGE21: u8 = 5;
2856 /// offset within page of GOT slot, scaled by r_length
2857 pub const ARM64_RELOC_GOT_LOAD_PAGEOFF12: u8 = 6;
2858 /// for pointers to GOT slots
2859 pub const ARM64_RELOC_POINTER_TO_GOT: u8 = 7;
2860 /// pc-rel distance to page of TLVP slot
2861 pub const ARM64_RELOC_TLVP_LOAD_PAGE21: u8 = 8;
2862 /// offset within page of TLVP slot, scaled by r_length
2863 pub const ARM64_RELOC_TLVP_LOAD_PAGEOFF12: u8 = 9;
2864 /// must be followed by PAGE21 or PAGEOFF12
2865 pub const ARM64_RELOC_ADDEND: u8 = 10;
2866 
2867 // An arm64e authenticated pointer.
2868 //
2869 // Represents a pointer to a symbol (like ARM64_RELOC_UNSIGNED).
2870 // Additionally, the resulting pointer is signed.  The signature is
2871 // specified in the target location: the addend is restricted to the lower
2872 // 32 bits (instead of the full 64 bits for ARM64_RELOC_UNSIGNED):
2873 //
2874 //   |63|62|61-51|50-49|  48  |47     -     32|31  -  0|
2875 //   | 1| 0|  0  | key | addr | discriminator | addend |
2876 //
2877 // The key is one of:
2878 //   IA: 00 IB: 01
2879 //   DA: 10 DB: 11
2880 //
2881 // The discriminator field is used as extra signature diversification.
2882 //
2883 // The addr field indicates whether the target address should be blended
2884 // into the discriminator.
2885 //
2886 pub const ARM64_RELOC_AUTHENTICATED_POINTER: u8 = 11;
2887 
2888 // Definitions from "/usr/include/mach-o/ppc/reloc.h".
2889 
2890 /*
2891  * Relocation types used in the ppc implementation.  Relocation entries for
2892  * things other than instructions use the same generic relocation as discribed
2893  * above and their r_type is RELOC_VANILLA.  The rest of the relocation types
2894  * are for instructions.  Since they are for instructions the r_address field
2895  * indicates the 32 bit instruction that the relocation is to be preformed on.
2896  * The fields r_pcrel and r_length are ignored for non-RELOC_VANILLA r_types
2897  * except for PPC_RELOC_BR14.
2898  *
2899  * For PPC_RELOC_BR14 if the r_length is the unused value 3, then the branch was
2900  * statically predicted setting or clearing the Y-bit based on the sign of the
2901  * displacement or the opcode.  If this is the case the static linker must flip
2902  * the value of the Y-bit if the sign of the displacement changes for non-branch
2903  * always conditions.
2904  */
2905 /// generic relocation as discribed above
2906 pub const PPC_RELOC_VANILLA: u8 = 0;
2907 /// the second relocation entry of a pair
2908 pub const PPC_RELOC_PAIR: u8 = 1;
2909 /// 14 bit branch displacement (to a word address)
2910 pub const PPC_RELOC_BR14: u8 = 2;
2911 /// 24 bit branch displacement (to a word address)
2912 pub const PPC_RELOC_BR24: u8 = 3;
2913 /// a PAIR follows with the low half
2914 pub const PPC_RELOC_HI16: u8 = 4;
2915 /// a PAIR follows with the high half
2916 pub const PPC_RELOC_LO16: u8 = 5;
2917 /// Same as the RELOC_HI16 except the low 16 bits and the high 16 bits are added together
2918 /// with the low 16 bits sign extened first.  This means if bit 15 of the low 16 bits is
2919 /// set the high 16 bits stored in the instruction will be adjusted.
2920 pub const PPC_RELOC_HA16: u8 = 6;
2921 /// Same as the LO16 except that the low 2 bits are not stored in the instruction and are
2922 /// always zero.  This is used in double word load/store instructions.
2923 pub const PPC_RELOC_LO14: u8 = 7;
2924 /// a PAIR follows with subtract symbol value
2925 pub const PPC_RELOC_SECTDIFF: u8 = 8;
2926 /// prebound lazy pointer
2927 pub const PPC_RELOC_PB_LA_PTR: u8 = 9;
2928 /// section difference forms of above.  a PAIR
2929 pub const PPC_RELOC_HI16_SECTDIFF: u8 = 10;
2930 /// follows these with subtract symbol value
2931 pub const PPC_RELOC_LO16_SECTDIFF: u8 = 11;
2932 pub const PPC_RELOC_HA16_SECTDIFF: u8 = 12;
2933 pub const PPC_RELOC_JBSR: u8 = 13;
2934 pub const PPC_RELOC_LO14_SECTDIFF: u8 = 14;
2935 /// like PPC_RELOC_SECTDIFF, but the symbol referenced was local.
2936 pub const PPC_RELOC_LOCAL_SECTDIFF: u8 = 15;
2937 
2938 // Definitions from "/usr/include/mach-o/x86_64/reloc.h".
2939 
2940 /*
2941  * Relocations for x86_64 are a bit different than for other architectures in
2942  * Mach-O: Scattered relocations are not used.  Almost all relocations produced
2943  * by the compiler are external relocations.  An external relocation has the
2944  * r_extern bit set to 1 and the r_symbolnum field contains the symbol table
2945  * index of the target label.
2946  *
2947  * When the assembler is generating relocations, if the target label is a local
2948  * label (begins with 'L'), then the previous non-local label in the same
2949  * section is used as the target of the external relocation.  An addend is used
2950  * with the distance from that non-local label to the target label.  Only when
2951  * there is no previous non-local label in the section is an internal
2952  * relocation used.
2953  *
2954  * The addend (i.e. the 4 in _foo+4) is encoded in the instruction (Mach-O does
2955  * not have RELA relocations).  For PC-relative relocations, the addend is
2956  * stored directly in the instruction.  This is different from other Mach-O
2957  * architectures, which encode the addend minus the current section offset.
2958  *
2959  * The relocation types are:
2960  *
2961  * 	X86_64_RELOC_UNSIGNED	// for absolute addresses
2962  * 	X86_64_RELOC_SIGNED		// for signed 32-bit displacement
2963  * 	X86_64_RELOC_BRANCH		// a CALL/JMP instruction with 32-bit displacement
2964  * 	X86_64_RELOC_GOT_LOAD	// a MOVQ load of a GOT entry
2965  * 	X86_64_RELOC_GOT		// other GOT references
2966  * 	X86_64_RELOC_SUBTRACTOR	// must be followed by a X86_64_RELOC_UNSIGNED
2967  *
2968  * The following are sample assembly instructions, followed by the relocation
2969  * and section content they generate in an object file:
2970  *
2971  * 	call _foo
2972  * 		r_type=X86_64_RELOC_BRANCH, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo
2973  * 		E8 00 00 00 00
2974  *
2975  * 	call _foo+4
2976  * 		r_type=X86_64_RELOC_BRANCH, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo
2977  * 		E8 04 00 00 00
2978  *
2979  * 	movq _foo@GOTPCREL(%rip), %rax
2980  * 		r_type=X86_64_RELOC_GOT_LOAD, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo
2981  * 		48 8B 05 00 00 00 00
2982  *
2983  * 	pushq _foo@GOTPCREL(%rip)
2984  * 		r_type=X86_64_RELOC_GOT, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo
2985  * 		FF 35 00 00 00 00
2986  *
2987  * 	movl _foo(%rip), %eax
2988  * 		r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo
2989  * 		8B 05 00 00 00 00
2990  *
2991  * 	movl _foo+4(%rip), %eax
2992  * 		r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo
2993  * 		8B 05 04 00 00 00
2994  *
2995  * 	movb  $0x12, _foo(%rip)
2996  * 		r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo
2997  * 		C6 05 FF FF FF FF 12
2998  *
2999  * 	movl  $0x12345678, _foo(%rip)
3000  * 		r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_foo
3001  * 		C7 05 FC FF FF FF 78 56 34 12
3002  *
3003  * 	.quad _foo
3004  * 		r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo
3005  * 		00 00 00 00 00 00 00 00
3006  *
3007  * 	.quad _foo+4
3008  * 		r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo
3009  * 		04 00 00 00 00 00 00 00
3010  *
3011  * 	.quad _foo - _bar
3012  * 		r_type=X86_64_RELOC_SUBTRACTOR, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_bar
3013  * 		r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo
3014  * 		00 00 00 00 00 00 00 00
3015  *
3016  * 	.quad _foo - _bar + 4
3017  * 		r_type=X86_64_RELOC_SUBTRACTOR, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_bar
3018  * 		r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo
3019  * 		04 00 00 00 00 00 00 00
3020  *
3021  * 	.long _foo - _bar
3022  * 		r_type=X86_64_RELOC_SUBTRACTOR, r_length=2, r_extern=1, r_pcrel=0, r_symbolnum=_bar
3023  * 		r_type=X86_64_RELOC_UNSIGNED, r_length=2, r_extern=1, r_pcrel=0, r_symbolnum=_foo
3024  * 		00 00 00 00
3025  *
3026  * 	lea L1(%rip), %rax
3027  * 		r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=1, r_pcrel=1, r_symbolnum=_prev
3028  * 		48 8d 05 12 00 00 00
3029  * 		// assumes _prev is the first non-local label 0x12 bytes before L1
3030  *
3031  * 	lea L0(%rip), %rax
3032  * 		r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=0, r_pcrel=1, r_symbolnum=3
3033  * 		48 8d 05 56 00 00 00
3034  *		// assumes L0 is in third section and there is no previous non-local label.
3035  *		// The rip-relative-offset of 0x00000056 is L0-address_of_next_instruction.
3036  *		// address_of_next_instruction is the address of the relocation + 4.
3037  *
3038  *     add     $6,L0(%rip)
3039  *             r_type=X86_64_RELOC_SIGNED_1, r_length=2, r_extern=0, r_pcrel=1, r_symbolnum=3
3040  *		83 05 18 00 00 00 06
3041  *		// assumes L0 is in third section and there is no previous non-local label.
3042  *		// The rip-relative-offset of 0x00000018 is L0-address_of_next_instruction.
3043  *		// address_of_next_instruction is the address of the relocation + 4 + 1.
3044  *		// The +1 comes from SIGNED_1.  This is used because the relocation is not
3045  *		// at the end of the instruction.
3046  *
3047  * 	.quad L1
3048  * 		r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_prev
3049  * 		12 00 00 00 00 00 00 00
3050  * 		// assumes _prev is the first non-local label 0x12 bytes before L1
3051  *
3052  * 	.quad L0
3053  * 		r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=0, r_pcrel=0, r_symbolnum=3
3054  * 		56 00 00 00 00 00 00 00
3055  * 		// assumes L0 is in third section, has an address of 0x00000056 in .o
3056  * 		// file, and there is no previous non-local label
3057  *
3058  * 	.quad _foo - .
3059  * 		r_type=X86_64_RELOC_SUBTRACTOR, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_prev
3060  * 		r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo
3061  * 		EE FF FF FF FF FF FF FF
3062  * 		// assumes _prev is the first non-local label 0x12 bytes before this
3063  * 		// .quad
3064  *
3065  * 	.quad _foo - L1
3066  * 		r_type=X86_64_RELOC_SUBTRACTOR, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_prev
3067  * 		r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_extern=1, r_pcrel=0, r_symbolnum=_foo
3068  * 		EE FF FF FF FF FF FF FF
3069  * 		// assumes _prev is the first non-local label 0x12 bytes before L1
3070  *
3071  * 	.quad L1 - _prev
3072  * 		// No relocations.  This is an assembly time constant.
3073  * 		12 00 00 00 00 00 00 00
3074  * 		// assumes _prev is the first non-local label 0x12 bytes before L1
3075  *
3076  *
3077  *
3078  * In final linked images, there are only two valid relocation kinds:
3079  *
3080  *     r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_pcrel=0, r_extern=1, r_symbolnum=sym_index
3081  *	This tells dyld to add the address of a symbol to a pointer sized (8-byte)
3082  *  piece of data (i.e on disk the 8-byte piece of data contains the addend). The
3083  *  r_symbolnum contains the index into the symbol table of the target symbol.
3084  *
3085  *     r_type=X86_64_RELOC_UNSIGNED, r_length=3, r_pcrel=0, r_extern=0, r_symbolnum=0
3086  * This tells dyld to adjust the pointer sized (8-byte) piece of data by the amount
3087  * the containing image was loaded from its base address (e.g. slide).
3088  *
3089  */
3090 /// for absolute addresses
3091 pub const X86_64_RELOC_UNSIGNED: u8 = 0;
3092 /// for signed 32-bit displacement
3093 pub const X86_64_RELOC_SIGNED: u8 = 1;
3094 /// a CALL/JMP instruction with 32-bit displacement
3095 pub const X86_64_RELOC_BRANCH: u8 = 2;
3096 /// a MOVQ load of a GOT entry
3097 pub const X86_64_RELOC_GOT_LOAD: u8 = 3;
3098 /// other GOT references
3099 pub const X86_64_RELOC_GOT: u8 = 4;
3100 /// must be followed by a X86_64_RELOC_UNSIGNED
3101 pub const X86_64_RELOC_SUBTRACTOR: u8 = 5;
3102 /// for signed 32-bit displacement with a -1 addend
3103 pub const X86_64_RELOC_SIGNED_1: u8 = 6;
3104 /// for signed 32-bit displacement with a -2 addend
3105 pub const X86_64_RELOC_SIGNED_2: u8 = 7;
3106 /// for signed 32-bit displacement with a -4 addend
3107 pub const X86_64_RELOC_SIGNED_4: u8 = 8;
3108 /// for thread local variables
3109 pub const X86_64_RELOC_TLV: u8 = 9;
3110 
3111 unsafe_impl_pod!(FatHeader, FatArch32, FatArch64,);
3112 unsafe_impl_endian_pod!(
3113     MachHeader32,
3114     MachHeader64,
3115     LoadCommand,
3116     LcStr,
3117     SegmentCommand32,
3118     SegmentCommand64,
3119     Section32,
3120     Section64,
3121     Fvmlib,
3122     FvmlibCommand,
3123     Dylib,
3124     DylibCommand,
3125     SubFrameworkCommand,
3126     SubClientCommand,
3127     SubUmbrellaCommand,
3128     SubLibraryCommand,
3129     PreboundDylibCommand,
3130     DylinkerCommand,
3131     ThreadCommand,
3132     RoutinesCommand,
3133     RoutinesCommand_64,
3134     SymtabCommand,
3135     DysymtabCommand,
3136     DylibTableOfContents,
3137     DylibModule32,
3138     DylibModule64,
3139     DylibReference,
3140     TwolevelHintsCommand,
3141     TwolevelHint,
3142     PrebindCksumCommand,
3143     UuidCommand,
3144     RpathCommand,
3145     LinkeditDataCommand,
3146     EncryptionInfoCommand,
3147     EncryptionInfoCommand64,
3148     VersionMinCommand,
3149     BuildVersionCommand,
3150     BuildToolVersion,
3151     DyldInfoCommand,
3152     LinkerOptionCommand,
3153     SymSegCommand,
3154     IdentCommand,
3155     FvmfileCommand,
3156     EntryPointCommand,
3157     SourceVersionCommand,
3158     DataInCodeEntry,
3159     //TlvDescriptor,
3160     NoteCommand,
3161     Nlist32,
3162     Nlist64,
3163     Relocation,
3164 );
3165