VirtualBox

source: vbox/trunk/include/iprt/formats/mach-o.h

Last change on this file was 109183, checked in by vboxsync, 4 days ago

IPRT/ldrMachO: Deal with LC_DYLD_EXPORTS_TRIE, LC_DYLD_CHAINED_FIXUPS and S_INIT_FUNC_OFFSETS for debug loading (assertions) and similar, just so the build works on 14.7 with clang v16. jiraref:VBP-1653

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 34.2 KB
Line 
1/* $Id: mach-o.h 109183 2025-05-06 20:50:35Z vboxsync $ */
2/** @file
3 * IPRT - Mach-O Structures and Constants.
4 */
5
6/*
7 * Copyright (C) 2011-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37#ifndef IPRT_INCLUDED_formats_mach_o_h
38#define IPRT_INCLUDED_formats_mach_o_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43#include <iprt/types.h>
44#include <iprt/assertcompile.h>
45
46#ifndef CPU_ARCH_MASK
47# define IPRT_MACHO_NEED_CPU_DEFINES
48#endif
49
50#ifdef IPRT_MACHO_NEED_CPU_DEFINES
51
52/* cputype */
53#define CPU_ARCH_MASK INT32_C(0xff000000)
54#define CPU_ARCH_ABI64 INT32_C(0x01000000)
55#define CPU_ARCH_ABI64_32 INT32_C(0x02000000) /**< LP32 on 64-bit hardware */
56
57#define CPU_TYPE_ANY INT32_C(-1)
58#define CPU_TYPE_VAX INT32_C(1)
59#define CPU_TYPE_MC680x0 INT32_C(6)
60#define CPU_TYPE_X86 INT32_C(7)
61#define CPU_TYPE_I386 CPU_TYPE_X86
62#define CPU_TYPE_X86_64 (CPU_TYPE_X86 | CPU_ARCH_ABI64)
63#define CPU_TYPE_MC98000 INT32_C(10)
64#define CPU_TYPE_HPPA INT32_C(11)
65#define CPU_TYPE_ARM INT32_C(12)
66#define CPU_TYPE_ARM32 CPU_TYPE_ARM
67#define CPU_TYPE_ARM64 (CPU_TYPE_ARM | CPU_ARCH_ABI64)
68#define CPU_TYPE_ARM64_32 (CPU_TYPE_ARM | CPU_ARCH_ABI64_32)
69#define CPU_TYPE_MC88000 INT32_C(13)
70#define CPU_TYPE_SPARC INT32_C(14)
71#define CPU_TYPE_I860 INT32_C(15)
72#define CPU_TYPE_POWERPC INT32_C(18)
73#define CPU_TYPE_POWERPC64 (CPU_TYPE_POWERPC | CPU_ARCH_ABI64)
74
75/* cpusubtype */
76#define CPU_SUBTYPE_MULTIPLE INT32_C(-1)
77#define CPU_SUBTYPE_LITTLE_ENDIAN INT32_C(0)
78#define CPU_SUBTYPE_BIG_ENDIAN INT32_C(1)
79
80#define CPU_SUBTYPE_VAX_ALL INT32_C(0)
81#define CPU_SUBTYPE_VAX780 INT32_C(1)
82#define CPU_SUBTYPE_VAX785 INT32_C(2)
83#define CPU_SUBTYPE_VAX750 INT32_C(3)
84#define CPU_SUBTYPE_VAX730 INT32_C(4)
85#define CPU_SUBTYPE_UVAXI INT32_C(5)
86#define CPU_SUBTYPE_UVAXII INT32_C(6)
87#define CPU_SUBTYPE_VAX8200 INT32_C(7)
88#define CPU_SUBTYPE_VAX8500 INT32_C(8)
89#define CPU_SUBTYPE_VAX8600 INT32_C(9)
90#define CPU_SUBTYPE_VAX8650 INT32_C(10)
91#define CPU_SUBTYPE_VAX8800 INT32_C(11)
92#define CPU_SUBTYPE_UVAXIII INT32_C(12)
93
94#define CPU_SUBTYPE_MC680x0_ALL INT32_C(1)
95#define CPU_SUBTYPE_MC68030 INT32_C(1)
96#define CPU_SUBTYPE_MC68040 INT32_C(2)
97#define CPU_SUBTYPE_MC68030_ONLY INT32_C(3)
98
99#define CPU_SUBTYPE_INTEL(fam, model) ( (int32_t )(((model) << 4) | (fam)) )
100#define CPU_SUBTYPE_INTEL_FAMILY(subtype) ( (subtype) & 0xf )
101#define CPU_SUBTYPE_INTEL_MODEL(subtype) ( (subtype) >> 4 )
102#define CPU_SUBTYPE_INTEL_FAMILY_MAX 0xf
103#define CPU_SUBTYPE_INTEL_MODEL_ALL 0
104
105#define CPU_SUBTYPE_I386_ALL CPU_SUBTYPE_INTEL(3, 0)
106#define CPU_SUBTYPE_386 CPU_SUBTYPE_INTEL(3, 0)
107#define CPU_SUBTYPE_486 CPU_SUBTYPE_INTEL(4, 0)
108#define CPU_SUBTYPE_486SX CPU_SUBTYPE_INTEL(4, 8)
109#define CPU_SUBTYPE_586 CPU_SUBTYPE_INTEL(5, 0)
110#define CPU_SUBTYPE_PENT CPU_SUBTYPE_INTEL(5, 0)
111#define CPU_SUBTYPE_PENTPRO CPU_SUBTYPE_INTEL(6, 1)
112#define CPU_SUBTYPE_PENTII_M3 CPU_SUBTYPE_INTEL(6, 3)
113#define CPU_SUBTYPE_PENTII_M5 CPU_SUBTYPE_INTEL(6, 5)
114#define CPU_SUBTYPE_CELERON CPU_SUBTYPE_INTEL(7, 6)
115#define CPU_SUBTYPE_CELERON_MOBILE CPU_SUBTYPE_INTEL(7, 7)
116#define CPU_SUBTYPE_PENTIUM_3 CPU_SUBTYPE_INTEL(8, 0)
117#define CPU_SUBTYPE_PENTIUM_3_M CPU_SUBTYPE_INTEL(8, 1)
118#define CPU_SUBTYPE_PENTIUM_3_XEON CPU_SUBTYPE_INTEL(8, 2)
119#define CPU_SUBTYPE_PENTIUM_M CPU_SUBTYPE_INTEL(9, 0)
120#define CPU_SUBTYPE_PENTIUM_4 CPU_SUBTYPE_INTEL(10, 0)
121#define CPU_SUBTYPE_PENTIUM_4_M CPU_SUBTYPE_INTEL(10, 1)
122#define CPU_SUBTYPE_ITANIUM CPU_SUBTYPE_INTEL(11, 0)
123#define CPU_SUBTYPE_ITANIUM_2 CPU_SUBTYPE_INTEL(11, 1)
124#define CPU_SUBTYPE_XEON CPU_SUBTYPE_INTEL(12, 0)
125#define CPU_SUBTYPE_XEON_MP CPU_SUBTYPE_INTEL(12, 1)
126
127#define CPU_SUBTYPE_X86_ALL INT32_C(3)
128#define CPU_SUBTYPE_X86_64_ALL INT32_C(3)
129#define CPU_SUBTYPE_X86_ARCH1 INT32_C(4)
130
131#define CPU_SUBTYPE_MIPS_ALL INT32_C(0)
132#define CPU_SUBTYPE_MIPS_R2300 INT32_C(1)
133#define CPU_SUBTYPE_MIPS_R2600 INT32_C(2)
134#define CPU_SUBTYPE_MIPS_R2800 INT32_C(3)
135#define CPU_SUBTYPE_MIPS_R2000a INT32_C(4)
136#define CPU_SUBTYPE_MIPS_R2000 INT32_C(5)
137#define CPU_SUBTYPE_MIPS_R3000a INT32_C(6)
138#define CPU_SUBTYPE_MIPS_R3000 INT32_C(7)
139
140#define CPU_SUBTYPE_MC98000_ALL INT32_C(0)
141#define CPU_SUBTYPE_MC98601 INT32_C(1)
142
143#define CPU_SUBTYPE_HPPA_ALL INT32_C(0)
144#define CPU_SUBTYPE_HPPA_7100 INT32_C(0)
145#define CPU_SUBTYPE_HPPA_7100LC INT32_C(1)
146
147#define CPU_SUBTYPE_ARM_ALL INT32_C(0)
148#define CPU_SUBTYPE_ARM_V4T INT32_C(5)
149#define CPU_SUBTYPE_ARM_V6 INT32_C(6)
150#define CPU_SUBTYPE_ARM_V5TEJ INT32_C(7)
151#define CPU_SUBTYPE_ARM_XSCALE INT32_C(8)
152#define CPU_SUBTYPE_ARM_V7 INT32_C(9)
153#define CPU_SUBTYPE_ARM_V7F INT32_C(10)
154#define CPU_SUBTYPE_ARM_V7S INT32_C(11)
155#define CPU_SUBTYPE_ARM_V7K INT32_C(12)
156#define CPU_SUBTYPE_ARM_V8 INT32_C(13)
157#define CPU_SUBTYPE_ARM_V6M INT32_C(14)
158#define CPU_SUBTYPE_ARM_V7M INT32_C(15)
159#define CPU_SUBTYPE_ARM_V7EM INT32_C(16)
160#define CPU_SUBTYPE_ARM_V8M INT32_C(17)
161
162#define CPU_SUBTYPE_ARM64_ALL INT32_C(0)
163#define CPU_SUBTYPE_ARM64_V8 INT32_C(1)
164#define CPU_SUBTYPE_ARM64E INT32_C(2)
165#endif /* IPRT_MACHO_NEED_CPU_DEFINES */
166#define CPU_SUBTYPE_ARM64E_PTR_AUTH_MASK UINT32_C(0x0f000000)
167#define CPU_SUBTYPE_ARM64E_PTR_AUTH_VERSION(a_uSubType) ( ((a_uSubType) & CPU_SUBTYPE_ARM64E_PTR_AUTH_MASK) >> 24 )
168#define CPU_SUBTYPE_ARM64E_VERSIONED_PTRAUTH_ABI RT_BIT_32(29)
169#define CPU_SUBTYPE_ARM64E_KERNEL_PTRAUTH_ABI RT_BIT_32(30)
170#define CPU_SUBTYPE_ARM64E_PTRAUTH_ABI RT_BIT_32(31) /**< AKA CPU_SUBTYPE_PTRAUTH_ABI */
171#ifdef IPRT_MACHO_NEED_CPU_DEFINES
172
173#define CPU_SUBTYPE_ARM64_32_ALL INT32_C(0)
174#define CPU_SUBTYPE_ARM64_32_V8 INT32_C(1)
175
176#define CPU_SUBTYPE_MC88000_ALL INT32_C(0)
177#define CPU_SUBTYPE_MC88100 INT32_C(1)
178#define CPU_SUBTYPE_MC88110 INT32_C(2)
179
180#define CPU_SUBTYPE_SPARC_ALL INT32_C(0)
181
182#define CPU_SUBTYPE_I860_ALL INT32_C(0)
183#define CPU_SUBTYPE_I860_860 INT32_C(1)
184
185#define CPU_SUBTYPE_POWERPC_ALL INT32_C(0)
186#define CPU_SUBTYPE_POWERPC_601 INT32_C(1)
187#define CPU_SUBTYPE_POWERPC_602 INT32_C(2)
188#define CPU_SUBTYPE_POWERPC_603 INT32_C(3)
189#define CPU_SUBTYPE_POWERPC_603e INT32_C(4)
190#define CPU_SUBTYPE_POWERPC_603ev INT32_C(5)
191#define CPU_SUBTYPE_POWERPC_604 INT32_C(6)
192#define CPU_SUBTYPE_POWERPC_604e INT32_C(7)
193#define CPU_SUBTYPE_POWERPC_620 INT32_C(8)
194#define CPU_SUBTYPE_POWERPC_750 INT32_C(9)
195#define CPU_SUBTYPE_POWERPC_7400 INT32_C(10)
196#define CPU_SUBTYPE_POWERPC_7450 INT32_C(11)
197#define CPU_SUBTYPE_POWERPC_Max INT32_C(10)
198#define CPU_SUBTYPE_POWERPC_SCVger INT32_C(11)
199#define CPU_SUBTYPE_POWERPC_970 INT32_C(100)
200
201#define CPU_SUBTYPE_MASK UINT32_C(0xff000000) /**< Architecture specific bits. */
202#define CPU_SUBTYPE_LIB64 UINT32_C(0x80000000)
203
204#endif /* IPRT_MACHO_NEED_CPU_DEFINES */
205
206
207typedef struct fat_header
208{
209 uint32_t magic;
210 uint32_t nfat_arch;
211} fat_header_t;
212
213#ifndef IMAGE_FAT_SIGNATURE
214# define IMAGE_FAT_SIGNATURE UINT32_C(0xcafebabe)
215#endif
216#ifndef IMAGE_FAT_SIGNATURE_OE
217# define IMAGE_FAT_SIGNATURE_OE UINT32_C(0xbebafeca)
218#endif
219
220typedef struct fat_arch
221{
222 int32_t cputype;
223 int32_t cpusubtype;
224 uint32_t offset;
225 uint32_t size;
226 uint32_t align;
227} fat_arch_t;
228
229typedef struct mach_header_32
230{
231 uint32_t magic;
232 int32_t cputype;
233 int32_t cpusubtype;
234 uint32_t filetype;
235 uint32_t ncmds;
236 uint32_t sizeofcmds;
237 uint32_t flags;
238} mach_header_32_t;
239
240/* magic */
241#ifndef IMAGE_MACHO32_SIGNATURE
242# define IMAGE_MACHO32_SIGNATURE UINT32_C(0xfeedface)
243#endif
244#ifndef IMAGE_MACHO32_SIGNATURE_OE
245# define IMAGE_MACHO32_SIGNATURE_OE UINT32_C(0xcefaedfe)
246#endif
247#define MH_MAGIC IMAGE_MACHO32_SIGNATURE
248#define MH_CIGAM IMAGE_MACHO32_SIGNATURE_OE
249
250typedef struct mach_header_64
251{
252 uint32_t magic; /**< 0x00 */
253 int32_t cputype; /**< 0x04 */
254 int32_t cpusubtype; /**< 0x08 */
255 uint32_t filetype; /**< 0x0c */
256 uint32_t ncmds; /**< 0x10 */
257 uint32_t sizeofcmds; /**< 0x14 */
258 uint32_t flags; /**< 0x18 */
259 uint32_t reserved; /**< 0x1c */
260} mach_header_64_t;
261AssertCompileSize(mach_header_64_t, 0x20);
262
263/* magic */
264#ifndef IMAGE_MACHO64_SIGNATURE
265# define IMAGE_MACHO64_SIGNATURE UINT32_C(0xfeedfacf)
266#endif
267#ifndef IMAGE_MACHO64_SIGNATURE_OE
268# define IMAGE_MACHO64_SIGNATURE_OE UINT32_C(0xfefaedfe)
269#endif
270#define MH_MAGIC_64 IMAGE_MACHO64_SIGNATURE
271#define MH_CIGAM_64 IMAGE_MACHO64_SIGNATURE_OE
272
273/* mach_header_* filetype */
274#define MH_OBJECT UINT32_C(1)
275#define MH_EXECUTE UINT32_C(2)
276#define MH_FVMLIB UINT32_C(3)
277#define MH_CORE UINT32_C(4)
278#define MH_PRELOAD UINT32_C(5)
279#define MH_DYLIB UINT32_C(6)
280#define MH_DYLINKER UINT32_C(7)
281#define MH_BUNDLE UINT32_C(8)
282#define MH_DYLIB_STUB UINT32_C(9)
283#define MH_DSYM UINT32_C(10)
284#define MH_KEXT_BUNDLE UINT32_C(11)
285
286/* mach_header_* flags */
287#define MH_NOUNDEFS UINT32_C(0x00000001)
288#define MH_INCRLINK UINT32_C(0x00000002)
289#define MH_DYLDLINK UINT32_C(0x00000004)
290#define MH_BINDATLOAD UINT32_C(0x00000008)
291#define MH_PREBOUND UINT32_C(0x00000010)
292#define MH_SPLIT_SEGS UINT32_C(0x00000020)
293#define MH_LAZY_INIT UINT32_C(0x00000040)
294#define MH_TWOLEVEL UINT32_C(0x00000080)
295#define MH_FORCE_FLAT UINT32_C(0x00000100)
296#define MH_NOMULTIDEFS UINT32_C(0x00000200)
297#define MH_NOFIXPREBINDING UINT32_C(0x00000400)
298#define MH_PREBINDABLE UINT32_C(0x00000800)
299#define MH_ALLMODSBOUND UINT32_C(0x00001000)
300#define MH_SUBSECTIONS_VIA_SYMBOLS UINT32_C(0x00002000)
301#define MH_CANONICAL UINT32_C(0x00004000)
302#define MH_WEAK_DEFINES UINT32_C(0x00008000)
303#define MH_BINDS_TO_WEAK UINT32_C(0x00010000)
304#define MH_ALLOW_STACK_EXECUTION UINT32_C(0x00020000)
305#define MH_ROOT_SAFE UINT32_C(0x00040000)
306#define MH_SETUID_SAFE UINT32_C(0x00080000)
307#define MH_NO_REEXPORTED_DYLIBS UINT32_C(0x00100000)
308#define MH_PIE UINT32_C(0x00200000)
309#define MH_DEAD_STRIPPABLE_DYLIB UINT32_C(0x00400000)
310#define MH_HAS_TLV_DESCRIPTORS UINT32_C(0x00800000)
311#define MH_NO_HEAP_EXECUTION UINT32_C(0x01000000)
312#define MH_UNKNOWN UINT32_C(0x80000000)
313#define MH_VALID_FLAGS UINT32_C(0x81ffffff)
314
315
316typedef struct load_command
317{
318 uint32_t cmd;
319 uint32_t cmdsize;
320} load_command_t;
321
322/* load cmd */
323#define LC_REQ_DYLD UINT32_C(0x80000000)
324#define LC_SEGMENT_32 UINT32_C(0x01)
325#define LC_SYMTAB UINT32_C(0x02)
326#define LC_SYMSEG UINT32_C(0x03)
327#define LC_THREAD UINT32_C(0x04)
328#define LC_UNIXTHREAD UINT32_C(0x05)
329#define LC_LOADFVMLIB UINT32_C(0x06)
330#define LC_IDFVMLIB UINT32_C(0x07)
331#define LC_IDENT UINT32_C(0x08)
332#define LC_FVMFILE UINT32_C(0x09)
333#define LC_PREPAGE UINT32_C(0x0a)
334#define LC_DYSYMTAB UINT32_C(0x0b)
335#define LC_LOAD_DYLIB UINT32_C(0x0c)
336#define LC_ID_DYLIB UINT32_C(0x0d)
337#define LC_LOAD_DYLINKER UINT32_C(0x0e)
338#define LC_ID_DYLINKER UINT32_C(0x0f)
339#define LC_PREBOUND_DYLIB UINT32_C(0x10)
340#define LC_ROUTINES UINT32_C(0x11)
341#define LC_SUB_FRAMEWORK UINT32_C(0x12)
342#define LC_SUB_UMBRELLA UINT32_C(0x13)
343#define LC_SUB_CLIENT UINT32_C(0x14)
344#define LC_SUB_LIBRARY UINT32_C(0x15)
345#define LC_TWOLEVEL_HINTS UINT32_C(0x16)
346#define LC_PREBIND_CKSUM UINT32_C(0x17)
347#define LC_LOAD_WEAK_DYLIB (UINT32_C(0x18) | LC_REQ_DYLD)
348#define LC_SEGMENT_64 UINT32_C(0x19)
349#define LC_ROUTINES_64 UINT32_C(0x1a)
350#define LC_UUID UINT32_C(0x1b)
351#define LC_RPATH (UINT32_C(0x1c) | LC_REQ_DYLD)
352#define LC_CODE_SIGNATURE UINT32_C(0x1d)
353#define LC_SEGMENT_SPLIT_INFO UINT32_C(0x1e)
354#define LC_REEXPORT_DYLIB (UINT32_C(0x1f) | LC_REQ_DYLD)
355#define LC_LAZY_LOAD_DYLIB UINT32_C(0x20)
356#define LC_ENCRYPTION_INFO UINT32_C(0x21)
357#define LC_DYLD_INFO UINT32_C(0x22)
358#define LC_DYLD_INFO_ONLY (UINT32_C(0x22) | LC_REQ_DYLD)
359#define LC_LOAD_UPWARD_DYLIB (UINT32_C(0x23) | LC_REQ_DYLD)
360#define LC_VERSION_MIN_MACOSX UINT32_C(0x24)
361#define LC_VERSION_MIN_IPHONEOS UINT32_C(0x25)
362#define LC_FUNCTION_STARTS UINT32_C(0x26)
363#define LC_DYLD_ENVIRONMENT UINT32_C(0x27)
364#define LC_MAIN (UINT32_C(0x28) | LC_REQ_DYLD)
365#define LC_DATA_IN_CODE UINT32_C(0x29)
366#define LC_SOURCE_VERSION UINT32_C(0x2a) /**< source_version_command */
367#define LC_DYLIB_CODE_SIGN_DRS UINT32_C(0x2b)
368#define LC_ENCRYPTION_INFO_64 UINT32_C(0x2c)
369#define LC_LINKER_OPTION UINT32_C(0x2d)
370#define LC_LINKER_OPTIMIZATION_HINT UINT32_C(0x2e)
371#define LC_VERSION_MIN_TVOS UINT32_C(0x2f)
372#define LC_VERSION_MIN_WATCHOS UINT32_C(0x30)
373#define LC_NOTE UINT32_C(0x31)
374#define LC_BUILD_VERSION UINT32_C(0x32)
375#define LC_DYLD_EXPORTS_TRIE (UINT32_C(0x33) | LC_REQ_DYLD)
376#define LC_DYLD_CHAINED_FIXUPS (UINT32_C(0x34) | LC_REQ_DYLD)
377#define LC_FILESET_ENTRY (UINT32_C(0x35) | LC_REQ_DYLD)
378#define LC_ATOM_INFO UINT32_C(0x36)
379
380
381typedef struct lc_str
382{
383 uint32_t offset;
384} lc_str_t;
385
386typedef struct segment_command_32
387{
388 uint32_t cmd;
389 uint32_t cmdsize;
390 char segname[16];
391 uint32_t vmaddr;
392 uint32_t vmsize;
393 uint32_t fileoff;
394 uint32_t filesize;
395 uint32_t maxprot;
396 uint32_t initprot;
397 uint32_t nsects;
398 uint32_t flags;
399} segment_command_32_t;
400
401typedef struct segment_command_64
402{
403 uint32_t cmd;
404 uint32_t cmdsize;
405 char segname[16];
406 uint64_t vmaddr;
407 uint64_t vmsize;
408 uint64_t fileoff;
409 uint64_t filesize;
410 uint32_t maxprot;
411 uint32_t initprot;
412 uint32_t nsects;
413 uint32_t flags;
414} segment_command_64_t;
415
416/* segment flags */
417#define SG_HIGHVM UINT32_C(0x00000001)
418#define SG_FVMLIB UINT32_C(0x00000002)
419#define SG_NORELOC UINT32_C(0x00000004)
420#define SG_PROTECTED_VERSION_1 UINT32_C(0x00000008)
421#define SG_READ_ONLY UINT32_C(0x00000010) /**< Make it read-only after applying fixups. @since 10.14 */
422
423/* maxprot/initprot */
424#ifndef VM_PROT_NONE
425# define VM_PROT_NONE UINT32_C(0x00000000)
426# define VM_PROT_READ UINT32_C(0x00000001)
427# define VM_PROT_WRITE UINT32_C(0x00000002)
428# define VM_PROT_EXECUTE UINT32_C(0x00000004)
429# define VM_PROT_ALL UINT32_C(0x00000007)
430#endif
431
432typedef struct section_32
433{
434 char sectname[16];
435 char segname[16];
436 uint32_t addr;
437 uint32_t size;
438 uint32_t offset;
439 uint32_t align;
440 uint32_t reloff;
441 uint32_t nreloc;
442 uint32_t flags;
443 /** For S_LAZY_SYMBOL_POINTERS, S_NON_LAZY_SYMBOL_POINTERS and S_SYMBOL_STUBS
444 * this is the index into the indirect symbol table. */
445 uint32_t reserved1;
446 /** For S_SYMBOL_STUBS this is the entry size. */
447 uint32_t reserved2;
448} section_32_t;
449
450typedef struct section_64
451{
452 char sectname[16];
453 char segname[16];
454 uint64_t addr;
455 uint64_t size;
456 uint32_t offset;
457 uint32_t align;
458 uint32_t reloff;
459 uint32_t nreloc;
460 uint32_t flags;
461 /** For S_LAZY_SYMBOL_POINTERS, S_NON_LAZY_SYMBOL_POINTERS and S_SYMBOL_STUBS
462 * this is the index into the indirect symbol table. */
463 uint32_t reserved1;
464 uint32_t reserved2;
465 uint32_t reserved3;
466} section_64_t;
467
468/* section flags */
469#define SECTION_TYPE UINT32_C(0xff)
470#define S_REGULAR UINT32_C(0x00)
471#define S_ZEROFILL UINT32_C(0x01)
472#define S_CSTRING_LITERALS UINT32_C(0x02)
473#define S_4BYTE_LITERALS UINT32_C(0x03)
474#define S_8BYTE_LITERALS UINT32_C(0x04)
475#define S_LITERAL_POINTERS UINT32_C(0x05)
476#define S_NON_LAZY_SYMBOL_POINTERS UINT32_C(0x06)
477#define S_LAZY_SYMBOL_POINTERS UINT32_C(0x07)
478#define S_SYMBOL_STUBS UINT32_C(0x08)
479#define S_MOD_INIT_FUNC_POINTERS UINT32_C(0x09)
480#define S_MOD_TERM_FUNC_POINTERS UINT32_C(0x0a)
481#define S_COALESCED UINT32_C(0x0b)
482#define S_GB_ZEROFILL UINT32_C(0x0c)
483#define S_INTERPOSING UINT32_C(0x0d)
484#define S_16BYTE_LITERALS UINT32_C(0x0e)
485#define S_DTRACE_DOF UINT32_C(0x0f)
486#define S_LAZY_DYLIB_SYMBOL_POINTERS UINT32_C(0x10)
487#define S_THREAD_LOCAL_REGULAR UINT32_C(0x11)
488#define S_THREAD_LOCAL_ZEROFILL UINT32_C(0x12)
489#define S_THREAD_LOCAL_VARIABLES UINT32_C(0x13)
490#define S_THREAD_LOCAL_VARIABLE_POINTERS UINT32_C(0x14)
491#define S_THREAD_LOCAL_INIT_FUNCTION_POINTERS UINT32_C(0x15)
492#define S_INIT_FUNC_OFFSETS UINT32_C(0x16)
493
494
495
496
497#define SECTION_ATTRIBUTES UINT32_C(0xffffff00)
498#define SECTION_ATTRIBUTES_USR UINT32_C(0xff000000)
499#define S_ATTR_PURE_INSTRUCTIONS UINT32_C(0x80000000)
500#define S_ATTR_NO_TOC UINT32_C(0x40000000)
501#define S_ATTR_STRIP_STATIC_SYMS UINT32_C(0x20000000)
502#define S_ATTR_NO_DEAD_STRIP UINT32_C(0x10000000)
503#define S_ATTR_LIVE_SUPPORT UINT32_C(0x08000000)
504#define S_ATTR_SELF_MODIFYING_CODE UINT32_C(0x04000000)
505#define S_ATTR_DEBUG UINT32_C(0x02000000)
506#define SECTION_ATTRIBUTES_SYS UINT32_C(0x00ffff00)
507#define S_ATTR_SOME_INSTRUCTIONS UINT32_C(0x00000400)
508#define S_ATTR_EXT_RELOC UINT32_C(0x00000200)
509#define S_ATTR_LOC_RELOC UINT32_C(0x00000100)
510
511/* standard section names */
512#define SEG_PAGEZERO "__PAGEZERO"
513#define SEG_TEXT "__TEXT"
514#define SECT_TEXT "__text"
515#define SECT_FVMLIB_INIT0 "__fvmlib_init0"
516#define SECT_FVMLIB_INIT1 "__fvmlib_init1"
517#define SEG_DATA "__DATA"
518#define SECT_DATA "__data"
519#define SECT_BSS "__bss"
520#define SECT_COMMON "__common"
521#define SEG_OBJC "__OBJC"
522#define SECT_OBJC_SYMBOLS "__symbol_table"
523#define SECT_OBJC_MODULES "__module_info"
524#define SECT_OBJC_STRINGS "__selector_strs"
525#define SECT_OBJC_REFS "__selector_refs"
526#define SEG_ICON "__ICON"
527#define SECT_ICON_HEADER "__header"
528#define SECT_ICON_TIFF "__tiff"
529#define SEG_LINKEDIT "__LINKEDIT"
530#define SEG_UNIXSTACK "__UNIXSTACK"
531#define SEG_IMPORT "__IMPORT"
532
533typedef struct thread_command
534{
535 uint32_t cmd;
536 uint32_t cmdsize;
537} thread_command_t;
538
539typedef struct symtab_command
540{
541 uint32_t cmd;
542 uint32_t cmdsize;
543 uint32_t symoff;
544 uint32_t nsyms;
545 uint32_t stroff;
546 uint32_t strsize;
547} symtab_command_t;
548
549typedef struct dysymtab_command
550{
551 uint32_t cmd;
552 uint32_t cmdsize;
553 /** @name Symbol groupings.
554 * @{ */
555 uint32_t ilocalsym; /**< Index into the symbol table of the first local symbol. */
556 uint32_t nlocalsym; /**< Number of local symbols. */
557 uint32_t iextdefsym; /**< Index into the symbol table of the first externally defined symbol. */
558 uint32_t nextdefsym; /**< Number of externally defined symbols. */
559 uint32_t iundefsym; /**< Index into the symbol table of the first undefined symbol. */
560 uint32_t nundefsym; /**< Number of undefined symbols. */
561 /** @} */
562 uint32_t tocoff; /**< Table of content file offset. (usually empty) */
563 uint32_t ntoc; /**< Number of entries in TOC. */
564 uint32_t modtaboff; /** The module table file offset. (usually empty) */
565 uint32_t nmodtab; /**< Number of entries in the module table. */
566 /** @name Dynamic symbol tables.
567 * @{ */
568 uint32_t extrefsymoff; /**< Externally referenceable symbol table file offset. @sa dylib_reference_t */
569 uint32_t nextrefsym; /**< Number externally referenceable symbols. */
570 uint32_t indirectsymboff; /**< Indirect symbol table (32-bit symtab indexes) for thunks and offset tables. */
571 uint32_t nindirectsymb; /**< Number of indirect symbol table entries. */
572 /** @} */
573 /** @name Relocations.
574 * @{ */
575 uint32_t extreloff; /**< External relocations (r_address is relative to first segment (i.e. RVA)). */
576 uint32_t nextrel; /**< Number of external relocations. */
577 uint32_t locreloff; /**< Local relocations (r_address is relative to first segment (i.e. RVA)). */
578 uint32_t nlocrel; /**< Number of local relocations. */
579 /** @} */
580} dysymtab_command_t;
581AssertCompileSize(dysymtab_command_t, 80);
582
583/** Special indirect symbol table entry value, stripped local symbol. */
584#define INDIRECT_SYMBOL_LOCAL UINT32_C(0x80000000)
585/** Special indirect symbol table entry value, stripped absolute symbol. */
586#define INDIRECT_SYMBOL_ABS UINT32_C(0x40000000)
587
588typedef struct dylib_reference
589{
590 uint32_t isym : 24; /**< Symbol table index. */
591 uint32_t flags : 8; /**< REFERENCE_FLAG_XXX? */
592} dylib_reference_t;
593AssertCompileSize(dylib_reference_t, 4);
594
595
596typedef struct dylib_table_of_contents
597{
598 uint32_t symbol_index; /**< External symbol table entry. */
599 uint32_t module_index; /**< The module table index of the module defining it. */
600} dylib_table_of_contents_t;
601AssertCompileSize(dylib_table_of_contents_t, 8);
602
603
604/** 32-bit module table entry. */
605typedef struct dylib_module
606{
607 uint32_t module_name;
608 uint32_t iextdefsym;
609 uint32_t nextdefsym;
610 uint32_t irefsym;
611 uint32_t nrefsym;
612 uint32_t ilocalsym;
613 uint32_t nlocalsym;
614 uint32_t iextrel;
615 uint32_t nextrel;
616 uint32_t iinit_iterm;
617 uint32_t ninit_nterm;
618 uint32_t objc_module_info_addr;
619 uint32_t objc_module_info_size;
620} dylib_module_32_t;
621AssertCompileSize(dylib_module_32_t, 13*4);
622
623/* a 64-bit module table entry */
624typedef struct dylib_module_64
625{
626 uint32_t module_name;
627 uint32_t iextdefsym;
628 uint32_t nextdefsym;
629 uint32_t irefsym;
630 uint32_t nrefsym;
631 uint32_t ilocalsym;
632 uint32_t nlocalsym;
633 uint32_t iextrel;
634 uint32_t nextrel;
635 uint32_t iinit_iterm;
636 uint32_t ninit_nterm;
637 uint32_t objc_module_info_size;
638 uint64_t objc_module_info_addr;
639} dylib_module_64_t;
640AssertCompileSize(dylib_module_64_t, 12*4+8);
641
642typedef struct uuid_command
643{
644 uint32_t cmd;
645 uint32_t cmdsize;
646 uint8_t uuid[16];
647} uuid_command_t;
648AssertCompileSize(uuid_command_t, 24);
649
650typedef struct linkedit_data_command
651{
652 uint32_t cmd; /**< LC_CODE_SIGNATURE, LC_SEGMENT_SPLIT_INFO, LC_FUNCTION_STARTS */
653 uint32_t cmdsize; /**< Size of this structure (16). */
654 uint32_t dataoff; /**< Offset into the file of the data. */
655 uint32_t datasize; /**< The size of the data. */
656} linkedit_data_command_t;
657AssertCompileSize(linkedit_data_command_t, 16);
658
659typedef struct version_min_command
660{
661 uint32_t cmd; /**< LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_TVOS, LC_VERSION_MIN_WATCHOS */
662 uint32_t cmdsize; /**< Size of this structure (16). */
663 uint32_t version; /**< 31..16=major, 15..8=minor, 7..0=patch. */
664 uint32_t reserved; /**< MBZ. */
665} version_min_command_t;
666AssertCompileSize(version_min_command_t, 16);
667
668typedef struct build_tool_version
669{
670 uint32_t tool; /**< TOOL_XXX */
671 uint32_t version; /**< 31..16=major, 15..8=minor, 7..0=patch. */
672} build_tool_version_t;
673AssertCompileSize(build_tool_version_t, 8);
674
675/** @name TOOL_XXX - Values for build_tool_version::tool
676 * @{ */
677#define TOOL_CLANG 1
678#define TOOL_SWIFT 2
679#define TOOL_LD 3
680/** @} */
681
682typedef struct build_version_command
683{
684 uint32_t cmd; /**< LC_BUILD_VERSION */
685 uint32_t cmdsize; /**< Size of this structure (at least 24). */
686 uint32_t platform; /**< PLATFORM_XXX */
687 uint32_t minos; /**< Minimum OS version: 31..16=major, 15..8=minor, 7..0=patch */
688 uint32_t sdk; /**< SDK version: 31..16=major, 15..8=minor, 7..0=patch */
689 uint32_t ntools; /**< Number of build_tool_version entries following in aTools. */
690 RT_FLEXIBLE_ARRAY_EXTENSION
691 build_tool_version_t aTools[RT_FLEXIBLE_ARRAY];
692} build_version_command_t;
693AssertCompileMemberOffset(build_version_command_t, aTools, 24);
694
695/** @name PLATFORM_XXX - Values for build_version_command::platform
696 * @{ */
697#define PLATFORM_MACOS 1
698#define PLATFORM_IOS 2
699#define PLATFORM_TVOS 3
700#define PLATFORM_WATCHOS 4
701/** @} */
702
703typedef struct source_version_command
704{
705 uint32_t cmd; /**< LC_SOURCE_VERSION */
706 uint32_t cmdsize; /**< Size of this structure (16). */
707 uint64_t version; /**< A.B.C.D.E, where A is 24 bits wide and the rest 10 bits each. */
708} source_version_command_t;
709AssertCompileSize(source_version_command_t, 16);
710
711
712typedef struct macho_nlist_32
713{
714 union
715 {
716 int32_t n_strx;
717 } n_un;
718 uint8_t n_type;
719 uint8_t n_sect;
720 int16_t n_desc;
721 uint32_t n_value;
722} macho_nlist_32_t;
723
724
725typedef struct macho_nlist_64
726{
727 union
728 {
729 uint32_t n_strx;
730 } n_un;
731 uint8_t n_type;
732 uint8_t n_sect;
733 int16_t n_desc;
734 uint64_t n_value;
735} macho_nlist_64_t;
736
737#define MACHO_N_EXT UINT8_C(0x01) /**< bit 0: external symbol*/
738#define MACHO_N_PEXT UINT8_C(0x10) /**< bit 4: priate external (hidden) */
739
740#define MACHO_N_TYPE UINT8_C(0x0e) /**< bit 1, 2, 3: type (UNDF, ABS, INDR, PBUD, SECT)*/
741#define MACHO_N_UNDF UINT8_C(0x00)
742#define MACHO_N_ABS UINT8_C(0x02)
743#define MACHO_N_INDR UINT8_C(0x0a)
744#define MACHO_N_PBUD UINT8_C(0x0c)
745#define MACHO_N_SECT UINT8_C(0x0e)
746
747#define MACHO_N_STAB UINT8_C(0xe0) /* bit 5, 6, 7: set if symbolic debugging entry. */
748#define MACHO_N_GSYM UINT8_C(0x20)
749#define MACHO_N_FNAME UINT8_C(0x22)
750#define MACHO_N_FUN UINT8_C(0x24)
751#define MACHO_N_STSYM UINT8_C(0x26)
752#define MACHO_N_LCSYM UINT8_C(0x28)
753#define MACHO_N_BNSYM UINT8_C(0x2e)
754#define MACHO_N_PC UINT8_C(0x30)
755#define MACHO_N_OPT UINT8_C(0x3c)
756#define MACHO_N_RSYM UINT8_C(0x40)
757#define MACHO_N_SLINE UINT8_C(0x44)
758#define MACHO_N_ENSYM UINT8_C(0x4e)
759#define MACHO_N_SSYM UINT8_C(0x60)
760#define MACHO_N_SO UINT8_C(0x64)
761#define MACHO_N_OSO UINT8_C(0x66)
762#define MACHO_N_LSYM UINT8_C(0x80)
763#define MACHO_N_BINCL UINT8_C(0x82)
764#define MACHO_N_SOL UINT8_C(0x84)
765#define MACHO_N_PARAMS UINT8_C(0x86)
766#define MACHO_N_VERSION UINT8_C(0x88)
767#define MACHO_N_OLEVEL UINT8_C(0x8A)
768#define MACHO_N_PSYM UINT8_C(0xa0)
769#define MACHO_N_EINCL UINT8_C(0xa2)
770#define MACHO_N_ENTRY UINT8_C(0xa4)
771#define MACHO_N_LBRAC UINT8_C(0xc0)
772#define MACHO_N_EXCL UINT8_C(0xc2)
773#define MACHO_N_RBRAC UINT8_C(0xe0)
774#define MACHO_N_BCOMM UINT8_C(0xe2)
775#define MACHO_N_ECOMM UINT8_C(0xe4)
776#define MACHO_N_ECOML UINT8_C(0xe8)
777#define MACHO_N_LENG UINT8_C(0xfe)
778
779#define MACHO_NO_SECT UINT8_C(0x00)
780#define MACHO_MAX_SECT UINT8_C(0xff)
781
782#define REFERENCE_TYPE UINT16_C(0x000f)
783#define REFERENCE_FLAG_UNDEFINED_NON_LAZY 0
784#define REFERENCE_FLAG_UNDEFINED_LAZY 1
785#define REFERENCE_FLAG_DEFINED 2
786#define REFERENCE_FLAG_PRIVATE_DEFINED 3
787#define REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY 4
788#define REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY 5
789#define REFERENCED_DYNAMICALLY UINT16_C(0x0010)
790
791#define GET_LIBRARY_ORDINAL(a_n_desc) \
792 RT_BYTE2(a_n_desc)
793#define SET_LIBRARY_ORDINAL(a_n_desc, a_ordinal) \
794 do { (a_n_desc) = RT_MAKE_U16(RT_BYTE1(a_n_desc), a_ordinal); } while (0)
795
796#define SELF_LIBRARY_ORDINAL 0x00
797#define MAX_LIBRARY_ORDINAL 0xfd
798#define DYNAMIC_LOOKUP_ORDINAL 0xfe
799#define EXECUTABLE_ORDINAL 0xff
800
801#define N_NO_DEAD_STRIP UINT16_C(0x0020)
802#define N_DESC_DISCARDED UINT16_C(0x0020)
803#define N_WEAK_REF UINT16_C(0x0040)
804#define N_WEAK_DEF UINT16_C(0x0080)
805#define N_REF_TO_WEAK UINT16_C(0x0080)
806#define N_SYMBOL_RESOLVER UINT16_C(0x0100)
807#define N_ALT_ENTRY UINT16_C(0x0200)
808
809typedef struct macho_relocation_info
810{
811 int32_t r_address;
812 uint32_t r_symbolnum : 24;
813 uint32_t r_pcrel : 1;
814 uint32_t r_length : 2;
815 uint32_t r_extern : 1;
816 uint32_t r_type : 4;
817} macho_relocation_info_t;
818AssertCompileSize(macho_relocation_info_t, 8);
819
820#define R_ABS 0
821#define R_SCATTERED UINT32_C(0x80000000)
822
823typedef struct scattered_relocation_info
824{
825#ifdef RT_LITTLE_ENDIAN
826 uint32_t r_address : 24;
827 uint32_t r_type : 4;
828 uint32_t r_length : 2;
829 uint32_t r_pcrel : 1;
830 uint32_t r_scattered : 1;
831#elif defined(RT_BIG_ENDIAN)
832 uint32_t r_scattered : 1;
833 uint32_t r_pcrel : 1;
834 uint32_t r_length : 2;
835 uint32_t r_type : 4;
836 uint32_t r_address : 24;
837#else
838# error "Neither K_ENDIAN isn't LITTLE or BIG!"
839#endif
840 int32_t r_value;
841} scattered_relocation_info_t;
842AssertCompileSize(scattered_relocation_info_t, 8);
843
844typedef union
845{
846 macho_relocation_info_t r;
847 scattered_relocation_info_t s;
848} macho_relocation_union_t;
849AssertCompileSize(macho_relocation_union_t, 8);
850
851typedef enum reloc_type_generic
852{
853 GENERIC_RELOC_VANILLA = 0,
854 GENERIC_RELOC_PAIR,
855 GENERIC_RELOC_SECTDIFF,
856 GENERIC_RELOC_PB_LA_PTR,
857 GENERIC_RELOC_LOCAL_SECTDIFF
858} reloc_type_generic_t;
859
860typedef enum reloc_type_x86_64
861{
862 X86_64_RELOC_UNSIGNED = 0,
863 X86_64_RELOC_SIGNED,
864 X86_64_RELOC_BRANCH,
865 X86_64_RELOC_GOT_LOAD,
866 X86_64_RELOC_GOT,
867 X86_64_RELOC_SUBTRACTOR,
868 X86_64_RELOC_SIGNED_1,
869 X86_64_RELOC_SIGNED_2,
870 X86_64_RELOC_SIGNED_4
871} reloc_type_x86_64_t;
872
873#endif /* !IPRT_INCLUDED_formats_mach_o_h */
874
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette