authorgravatar for mail@linusgroh.deLinus Groh <mail@linusgroh.de> 2026-07-02 21:52:24+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-07-03 13:17:25+02:00
log2f7e082381e64bbcdd06cf8f06eb84a9c8ad67de
tree4a5b122a0ddd66a1afc4a369bf97e1805b2b2828
parentfd62635d78d5db943d52d5470ee7279c18275f5d

libc: Add missing macOS mach-o headers


7 files changed, 1305 insertions(+), 13 deletions(-)

lib/libc/include/any-darwin-any/mach-o/arm/reloc.h created+67
......@@ -0,0 +1,67 @@
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef _MACHO_ARM_RELOC_H_
25#define _MACHO_ARM_RELOC_H_
26
27/*
28 * Relocation types used in the arm implementation. Relocation entries for
29 * things other than instructions use the same generic relocation as discribed
30 * in <mach-o/reloc.h> and their r_type is ARM_RELOC_VANILLA, one of the
31 * *_SECTDIFF or the *_PB_LA_PTR types. The rest of the relocation types are
32 * for instructions. Since they are for instructions the r_address field
33 * indicates the 32 bit instruction that the relocation is to be preformed on.
34 */
35
36enum reloc_type_arm
37{
38 ARM_RELOC_VANILLA, /* generic relocation as discribed above */
39 ARM_RELOC_PAIR, /* the second relocation entry of a pair */
40 ARM_RELOC_SECTDIFF, /* a PAIR follows with subtract symbol value */
41 ARM_RELOC_LOCAL_SECTDIFF, /* like ARM_RELOC_SECTDIFF, but the symbol
42 referenced was local. */
43 ARM_RELOC_PB_LA_PTR,/* prebound lazy pointer */
44 ARM_RELOC_BR24, /* 24 bit branch displacement (to a word address) */
45 ARM_THUMB_RELOC_BR22, /* 22 bit branch displacement (to a half-word
46 address) */
47 ARM_THUMB_32BIT_BRANCH, /* obsolete - a thumb 32-bit branch instruction
48 possibly needing page-spanning branch workaround */
49
50 /*
51 * For these two r_type relocations they always have a pair following them
52 * and the r_length bits are used differently. The encoding of the
53 * r_length is as follows:
54 * low bit of r_length:
55 * 0 - :lower16: for movw instructions
56 * 1 - :upper16: for movt instructions
57 * high bit of r_length:
58 * 0 - arm instructions
59 * 1 - thumb instructions
60 * the other half of the relocated expression is in the following pair
61 * relocation entry in the the low 16 bits of r_address field.
62 */
63 ARM_RELOC_HALF,
64 ARM_RELOC_HALF_SECTDIFF
65};
66
67#endif /* #ifndef _MACHO_ARM_RELOC_H_ */
lib/libc/include/any-darwin-any/mach-o/dyld_images.h created+224
......@@ -0,0 +1,224 @@
1/*
2 * Copyright (c) 2006-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23#ifndef _DYLD_IMAGES_
24#define _DYLD_IMAGES_
25
26#include <stdbool.h>
27#include <uuid/uuid.h>
28#include <TargetConditionals.h>
29
30#ifndef __OPEN_SOURCE__
31#if !0
32#include <unistd.h>
33#include <mach/mach.h>
34#endif
35#endif
36
37#if defined(__cplusplus) && (BUILDING_LIBDYLD || BUILDING_DYLD)
38#include <atomic>
39#endif
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45
46
47/*
48 * Beginning in Mac OS X 10.4, this is how gdb discovers which mach-o images are loaded in a process.
49 *
50 * gdb looks for the symbol "_dyld_all_image_infos" in dyld. It contains the fields below.
51 *
52 * For a snashot of what images are currently loaded, the infoArray fields contain a pointer
53 * to an array of all images. If infoArray is NULL, it means it is being modified, come back later.
54 *
55 * To be notified of changes, gdb sets a break point on the address pointed to by the notificationn
56 * field. The function it points to is called by dyld with an array of information about what images
57 * have been added (dyld_image_adding) or are about to be removed (dyld_image_removing).
58 *
59 * The notification is called after infoArray is updated. This means that if gdb attaches to a process
60 * and infoArray is NULL, gdb can set a break point on notification and let the proccess continue to
61 * run until the break point. Then gdb can inspect the full infoArray.
62 *
63 * The dyldVersion field always points to a C string that contains the dyld version. For instance,
64 * in dyld-127.3, dyldVersion would contain a pointer to "127.3".
65 *
66 * The errorMessage and terminationFlags fields are normally zero. If dyld terminates a process
67 * (for instance because a required dylib or symbol is missing), then the errorMessage field will
68 * be set to point to a C string message buffer containing the reason dyld terminate the process.
69 * The low bit of the terminationFlags will be set if dyld terminated the process before any user
70 * code ran, in which case there is no need for the crash log to contain the backtrace.
71 *
72 * When dyld terminates a process because some required dylib or symbol cannot be bound, in
73 * addition to the errorMessage field, it now sets the errorKind field and the corresponding
74 * fields: errorClientOfDylibPath, errorTargetDylibPath, errorSymbol.
75 *
76 */
77
78enum dyld_image_mode { dyld_image_adding=0, dyld_image_removing=1, dyld_image_info_change=2, dyld_image_dyld_moved=3 };
79
80struct dyld_image_info {
81 const struct mach_header* imageLoadAddress; /* base address image is mapped into */
82 const char* imageFilePath; /* path dyld used to load the image */
83 uintptr_t imageFileModDate; /* time_t of image file */
84 /* if stat().st_mtime of imageFilePath does not match imageFileModDate, */
85 /* then file has been modified since dyld loaded it */
86};
87
88struct dyld_uuid_info {
89 const struct mach_header* imageLoadAddress; /* base address image is mapped into */
90 uuid_t imageUUID; /* UUID of image */
91};
92
93#define DYLD_AOT_IMAGE_KEY_SIZE 32
94struct dyld_aot_image_info {
95 const struct mach_header* x86LoadAddress;
96 const struct mach_header* aotLoadAddress;
97 uint64_t aotImageSize;
98 uint8_t aotImageKey[DYLD_AOT_IMAGE_KEY_SIZE]; // uniquely identifying SHA-256 key for this aot
99};
100
101struct dyld_aot_shared_cache_info {
102 const uintptr_t cacheBaseAddress;
103 uuid_t cacheUUID;
104};
105
106typedef void (*dyld_image_notifier)(enum dyld_image_mode mode, uint32_t infoCount, const struct dyld_image_info info[]);
107
108/* for use in dyld_all_image_infos.errorKind field */
109enum { dyld_error_kind_none=0,
110 dyld_error_kind_dylib_missing=1,
111 dyld_error_kind_dylib_wrong_arch=2,
112 dyld_error_kind_dylib_version=3,
113 dyld_error_kind_symbol_missing=4
114 };
115
116/* internal limit */
117#define DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT 8
118
119// Must be aligned to support atomic updates
120// Note sim cannot assume alignment until all host dylds are new enough
121#if TARGET_OS_SIMULATOR
122struct dyld_all_image_infos
123#else
124struct __attribute__((aligned(16))) dyld_all_image_infos
125#endif
126{
127 uint32_t version; /* 1 in Mac OS X 10.4 and 10.5 */
128 uint32_t infoArrayCount;
129#if defined(__cplusplus) && (BUILDING_LIBDYLD || BUILDING_DYLD)
130 std::atomic<const struct dyld_image_info*> infoArray;
131#else
132 const struct dyld_image_info* infoArray;
133#endif
134 dyld_image_notifier notification;
135 bool processDetachedFromSharedRegion;
136 /* the following fields are only in version 2 (Mac OS X 10.6, iPhoneOS 2.0) and later */
137 bool libSystemInitialized;
138 const struct mach_header* dyldImageLoadAddress;
139 /* the following field is only in version 3 (Mac OS X 10.6, iPhoneOS 3.0) and later */
140 void* jitInfo;
141 /* the following fields are only in version 5 (Mac OS X 10.6, iPhoneOS 3.0) and later */
142 const char* dyldVersion;
143 const char* errorMessage;
144 uintptr_t terminationFlags;
145 /* the following field is only in version 6 (Mac OS X 10.6, iPhoneOS 3.1) and later */
146 void* coreSymbolicationShmPage;
147 /* the following field is only in version 7 (Mac OS X 10.6, iPhoneOS 3.1) and later */
148 uintptr_t systemOrderFlag;
149 /* the following field is only in version 8 (Mac OS X 10.7, iPhoneOS 3.1) and later */
150 uintptr_t uuidArrayCount;
151 const struct dyld_uuid_info* uuidArray; /* only images not in dyld shared cache */
152 /* the following field is only in version 9 (Mac OS X 10.7, iOS 4.0) and later */
153 struct dyld_all_image_infos* dyldAllImageInfosAddress;
154 /* the following field is only in version 10 (Mac OS X 10.7, iOS 4.2) and later */
155 uintptr_t initialImageCount;
156 /* the following field is only in version 11 (Mac OS X 10.7, iOS 4.2) and later */
157 uintptr_t errorKind;
158 const char* errorClientOfDylibPath;
159 const char* errorTargetDylibPath;
160 const char* errorSymbol;
161 /* the following field is only in version 12 (Mac OS X 10.7, iOS 4.3) and later */
162 uintptr_t sharedCacheSlide;
163 /* the following field is only in version 13 (Mac OS X 10.9, iOS 7.0) and later */
164 uint8_t sharedCacheUUID[16];
165 /* the following field is only in version 15 (macOS 10.12, iOS 10.0) and later */
166 uintptr_t sharedCacheBaseAddress;
167#if defined(__cplusplus) && (BUILDING_LIBDYLD || BUILDING_DYLD)
168 // We want this to be atomic in libdyld so that we can see updates when we map it shared
169 std::atomic<uint64_t> infoArrayChangeTimestamp;
170#else
171 uint64_t infoArrayChangeTimestamp;
172#endif
173 const char* dyldPath;
174
175 mach_port_t notifyPorts[DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT];
176
177#if __LP64__
178 uintptr_t reserved[11-(DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT/2)];
179#else
180 uintptr_t reserved[9-DYLD_MAX_PROCESS_INFO_NOTIFY_COUNT];
181#endif
182 // The following fields were added in version 18 (previously they were reserved padding fields)
183 uint64_t sharedCacheFSID;
184 uint64_t sharedCacheFSObjID;
185 /* the following field is only in version 16 (macOS 10.13, iOS 11.0) and later */
186 uintptr_t compact_dyld_image_info_addr;
187 size_t compact_dyld_image_info_size;
188 uint32_t platform; // FIXME: really a dyld_platform_t, but those aren't exposed here.
189
190 /* the following field is only in version 17 (macOS 10.16) and later */
191 uint32_t aotInfoCount;
192 const struct dyld_aot_image_info* aotInfoArray;
193 uint64_t aotInfoArrayChangeTimestamp;
194 uintptr_t aotSharedCacheBaseAddress;
195 uint8_t aotSharedCacheUUID[16];
196};
197
198/*
199 * Beginning in Mac OS X 10.5, this is how gdb discovers where the shared cache is in a process.
200 * Images that are in the shared cache have their segments rearranged, so when using imageFilePath
201 * to load the file from disk, you have to know to adjust addresses based on how their segment
202 * was rearranged.
203 *
204 * gdb looks for the symbol "_dyld_shared_region_ranges" in dyld.
205 *
206 * It contains information the count of shared regions used by the process. The count is
207 * the number of start/length pairs.
208 */
209struct dyld_shared_cache_ranges {
210 uintptr_t sharedRegionsCount; /* how many ranges follow */
211 struct {
212 uintptr_t start;
213 uintptr_t length;
214 } ranges[4]; /* max regions */
215};
216extern struct dyld_shared_cache_ranges dyld_shared_cache_ranges __attribute__((visibility("hidden")));
217
218
219
220#ifdef __cplusplus
221}
222#endif
223
224#endif /* _DYLD_IMAGES_ */
lib/libc/include/any-darwin-any/mach-o/fixup-chains.h created+334
......@@ -0,0 +1,334 @@
1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2018 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25#ifndef __MACH_O_FIXUP_CHAINS__
26#define __MACH_O_FIXUP_CHAINS__ 7
27
28
29#include <stdint.h>
30
31
32//#define LC_DYLD_EXPORTS_TRIE 0x80000033 // used with linkedit_data_command
33//#define LC_DYLD_CHAINED_FIXUPS 0x80000034 // used with linkedit_data_command, payload is dyld_chained_fixups_header
34
35
36// header of the LC_DYLD_CHAINED_FIXUPS payload
37struct dyld_chained_fixups_header
38{
39 uint32_t fixups_version; // 0
40 uint32_t starts_offset; // offset of dyld_chained_starts_in_image in chain_data
41 uint32_t imports_offset; // offset of imports table in chain_data
42 uint32_t symbols_offset; // offset of symbol strings in chain_data
43 uint32_t imports_count; // number of imported symbol names
44 uint32_t imports_format; // DYLD_CHAINED_IMPORT*
45 uint32_t symbols_format; // 0 => uncompressed, 1 => zlib compressed
46};
47
48// This struct is embedded in LC_DYLD_CHAINED_FIXUPS payload
49struct dyld_chained_starts_in_image
50{
51 uint32_t seg_count;
52 uint32_t seg_info_offset[1]; // each entry is offset into this struct for that segment
53 // followed by pool of dyld_chain_starts_in_segment data
54};
55
56// This struct is embedded in dyld_chain_starts_in_image
57// and passed down to the kernel for page-in linking
58struct dyld_chained_starts_in_segment
59{
60 uint32_t size; // size of this (amount kernel needs to copy)
61 uint16_t page_size; // 0x1000 or 0x4000
62 uint16_t pointer_format; // DYLD_CHAINED_PTR_*
63 uint64_t segment_offset; // offset in memory to start of segment
64 uint32_t max_valid_pointer; // for 32-bit OS, any value beyond this is not a pointer
65 uint16_t page_count; // how many pages are in array
66 uint16_t page_start[1]; // each entry is offset in each page of first element in chain
67 // or DYLD_CHAINED_PTR_START_NONE if no fixups on page
68 // uint16_t chain_starts[1]; // some 32-bit formats may require multiple starts per page.
69 // for those, if high bit is set in page_starts[], then it
70 // is index into chain_starts[] which is a list of starts
71 // the last of which has the high bit set
72};
73
74enum {
75 DYLD_CHAINED_PTR_START_NONE = 0xFFFF, // used in page_start[] to denote a page with no fixups
76 DYLD_CHAINED_PTR_START_MULTI = 0x8000, // used in page_start[] to denote a page which has multiple starts
77 DYLD_CHAINED_PTR_START_LAST = 0x8000, // used in chain_starts[] to denote last start in list for page
78};
79
80// these values are set in the reserved1 field of the __chain_starts section
81enum {
82 DYLD_CHAINED_STARTS_USE_FILE_OFFSET = 0x1, // denotes chain starts linked with -fixup_chains_section
83 DYLD_CHAINED_STARTS_USE_VM_OFFSET = 0x2, // denotes chain starts linked with -fixup_chains_section_vm
84};
85
86// This struct is embedded in __TEXT,__chain_starts section in firmware
87struct dyld_chained_starts_offsets
88{
89 uint32_t pointer_format; // DYLD_CHAINED_PTR_32_FIRMWARE or DYLD_CHAINED_PTR_ARM64E_KERNEL
90 uint32_t starts_count; // number of starts in array
91 uint32_t chain_starts[1]; // array chain start offsets
92};
93
94
95// values for dyld_chained_starts_in_segment.pointer_format
96enum {
97 DYLD_CHAINED_PTR_ARM64E = 1, // stride 8, unauth target is vmaddr
98 DYLD_CHAINED_PTR_64 = 2, // target is vmaddr
99 DYLD_CHAINED_PTR_32 = 3, // target is vmaddr
100 DYLD_CHAINED_PTR_32_CACHE = 4,
101 DYLD_CHAINED_PTR_32_FIRMWARE = 5,
102 DYLD_CHAINED_PTR_64_OFFSET = 6, // target is vm offset
103 DYLD_CHAINED_PTR_ARM64E_OFFSET = 7, // old name
104 DYLD_CHAINED_PTR_ARM64E_KERNEL = 7, // stride 4, unauth target is vm offset
105 DYLD_CHAINED_PTR_64_KERNEL_CACHE = 8,
106 DYLD_CHAINED_PTR_ARM64E_USERLAND = 9, // stride 8, unauth target is vm offset
107 DYLD_CHAINED_PTR_ARM64E_FIRMWARE = 10, // stride 4, unauth target is vmaddr
108 DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE = 11, // stride 1, x86_64 kernel caches
109 DYLD_CHAINED_PTR_ARM64E_USERLAND24 = 12, // stride 8, unauth target is vm offset, 24-bit bind
110 DYLD_CHAINED_PTR_ARM64E_SHARED_CACHE = 13, // stride 8, regular/auth targets both vm offsets. Only A keys supported
111 DYLD_CHAINED_PTR_ARM64E_SEGMENTED = 14, // stride 4, rebase offsets use segIndex and segOffset
112};
113
114
115// DYLD_CHAINED_PTR_ARM64E
116struct dyld_chained_ptr_arm64e_rebase
117{
118 uint64_t target : 43,
119 high8 : 8,
120 next : 11, // 4 or 8-byte stide
121 bind : 1, // == 0
122 auth : 1; // == 0
123};
124
125// DYLD_CHAINED_PTR_ARM64E
126struct dyld_chained_ptr_arm64e_bind
127{
128 uint64_t ordinal : 16,
129 zero : 16,
130 addend : 19, // +/-256K
131 next : 11, // 4 or 8-byte stide
132 bind : 1, // == 1
133 auth : 1; // == 0
134};
135
136// DYLD_CHAINED_PTR_ARM64E
137struct dyld_chained_ptr_arm64e_auth_rebase
138{
139 uint64_t target : 32, // runtimeOffset
140 diversity : 16,
141 addrDiv : 1,
142 key : 2,
143 next : 11, // 4 or 8-byte stide
144 bind : 1, // == 0
145 auth : 1; // == 1
146};
147
148// DYLD_CHAINED_PTR_ARM64E
149struct dyld_chained_ptr_arm64e_auth_bind
150{
151 uint64_t ordinal : 16,
152 zero : 16,
153 diversity : 16,
154 addrDiv : 1,
155 key : 2,
156 next : 11, // 4 or 8-byte stide
157 bind : 1, // == 1
158 auth : 1; // == 1
159};
160
161// DYLD_CHAINED_PTR_64/DYLD_CHAINED_PTR_64_OFFSET
162struct dyld_chained_ptr_64_rebase
163{
164 uint64_t target : 36, // 64GB max image size (DYLD_CHAINED_PTR_64 => vmAddr, DYLD_CHAINED_PTR_64_OFFSET => runtimeOffset)
165 high8 : 8, // top 8 bits set to this (DYLD_CHAINED_PTR_64 => after slide added, DYLD_CHAINED_PTR_64_OFFSET => before slide added)
166 reserved : 7, // all zeros
167 next : 12, // 4-byte stride
168 bind : 1; // == 0
169};
170
171
172// DYLD_CHAINED_PTR_ARM64E_USERLAND24
173struct dyld_chained_ptr_arm64e_bind24
174{
175 uint64_t ordinal : 24,
176 zero : 8,
177 addend : 19, // +/-256K
178 next : 11, // 8-byte stide
179 bind : 1, // == 1
180 auth : 1; // == 0
181};
182
183// DYLD_CHAINED_PTR_ARM64E_USERLAND24
184struct dyld_chained_ptr_arm64e_auth_bind24
185{
186 uint64_t ordinal : 24,
187 zero : 8,
188 diversity : 16,
189 addrDiv : 1,
190 key : 2,
191 next : 11, // 8-byte stide
192 bind : 1, // == 1
193 auth : 1; // == 1
194};
195
196// DYLD_CHAINED_PTR_ARM64E_SEGMENTED
197struct dyld_chained_ptr_arm64e_segmented_rebase
198{
199 uint32_t targetSegOffset : 28, // offset in segment
200 targetSegIndex : 4; // index into segment address table
201 uint32_t padding : 19,
202 next : 12, // 4-byte stide
203 auth : 1; // == 0
204};
205
206// DYLD_CHAINED_PTR_ARM64E_SEGMENTED
207struct dyld_chained_ptr_arm64e_auth_segmented_rebase
208{
209 uint32_t targetSegOffset : 28, // offset in segment
210 targetSegIndex : 4; // index into segment address table
211 uint32_t diversity : 16,
212 addrDiv : 1,
213 key : 2,
214 next : 12, // 4-byte stide
215 auth : 1; // == 1
216};
217
218// DYLD_CHAINED_PTR_64
219struct dyld_chained_ptr_64_bind
220{
221 uint64_t ordinal : 24,
222 addend : 8, // 0 thru 255
223 reserved : 19, // all zeros
224 next : 12, // 4-byte stride
225 bind : 1; // == 1
226};
227
228// DYLD_CHAINED_PTR_64_KERNEL_CACHE, DYLD_CHAINED_PTR_X86_64_KERNEL_CACHE
229struct dyld_chained_ptr_64_kernel_cache_rebase
230{
231 uint64_t target : 30, // basePointers[cacheLevel] + target
232 cacheLevel : 2, // what level of cache to bind to (indexes a mach_header array)
233 diversity : 16,
234 addrDiv : 1,
235 key : 2,
236 next : 12, // 1 or 4-byte stide
237 isAuth : 1; // 0 -> not authenticated. 1 -> authenticated
238};
239
240// DYLD_CHAINED_PTR_32
241// Note: for DYLD_CHAINED_PTR_32 some non-pointer values are co-opted into the chain
242// as out of range rebases. If an entry in the chain is > max_valid_pointer, then it
243// is not a pointer. To restore the value, subtract off the bias, which is
244// (64MB+max_valid_pointer)/2.
245struct dyld_chained_ptr_32_rebase
246{
247 uint32_t target : 26, // vmaddr, 64MB max image size
248 next : 5, // 4-byte stride
249 bind : 1; // == 0
250};
251
252// DYLD_CHAINED_PTR_32
253struct dyld_chained_ptr_32_bind
254{
255 uint32_t ordinal : 20,
256 addend : 6, // 0 thru 63
257 next : 5, // 4-byte stride
258 bind : 1; // == 1
259};
260
261// DYLD_CHAINED_PTR_32_CACHE
262struct dyld_chained_ptr_32_cache_rebase
263{
264 uint32_t target : 30, // 1GB max dyld cache TEXT and DATA
265 next : 2; // 4-byte stride
266};
267
268
269// DYLD_CHAINED_PTR_32_FIRMWARE
270struct dyld_chained_ptr_32_firmware_rebase
271{
272 uint32_t target : 26, // 64MB max firmware TEXT and DATA
273 next : 6; // 4-byte stride
274};
275
276// DYLD_CHAINED_PTR_ARM64E_SHARED_CACHE
277struct dyld_chained_ptr_arm64e_shared_cache_rebase
278{
279 uint64_t runtimeOffset : 34, // offset from the start of the shared cache
280 high8 : 8,
281 unused : 10,
282 next : 11, // 8-byte stide
283 auth : 1; // == 0
284};
285
286// DYLD_CHAINED_PTR_ARM64E_SHARED_CACHE
287struct dyld_chained_ptr_arm64e_shared_cache_auth_rebase
288{
289 uint64_t runtimeOffset : 34, // offset from the start of the shared cache
290 diversity : 16,
291 addrDiv : 1,
292 keyIsData : 1, // implicitly always the 'A' key. 0 -> IA. 1 -> DA
293 next : 11, // 8-byte stide
294 auth : 1; // == 1
295};
296
297
298
299// values for dyld_chained_fixups_header.imports_format
300enum {
301 DYLD_CHAINED_IMPORT = 1,
302 DYLD_CHAINED_IMPORT_ADDEND = 2,
303 DYLD_CHAINED_IMPORT_ADDEND64 = 3,
304};
305
306// DYLD_CHAINED_IMPORT
307struct dyld_chained_import
308{
309 uint32_t lib_ordinal : 8, // -15 .. 240 (0xF1 .. 0xF0)
310 weak_import : 1,
311 name_offset : 23;
312};
313
314// DYLD_CHAINED_IMPORT_ADDEND
315struct dyld_chained_import_addend
316{
317 uint32_t lib_ordinal : 8, // -15 .. 240 (0xF1 .. 0xF0)
318 weak_import : 1,
319 name_offset : 23;
320 int32_t addend;
321};
322
323// DYLD_CHAINED_IMPORT_ADDEND64
324struct dyld_chained_import_addend64
325{
326 uint64_t lib_ordinal : 16, // -15 .. 65520 (0xFFF1 .. 0xFFF0)
327 weak_import : 1,
328 reserved : 15,
329 name_offset : 32;
330 uint64_t addend;
331};
332
333#endif // __MACH_O_FIXUP_CHAINS__
334
lib/libc/include/any-darwin-any/mach-o/ldsyms.h created+133
......@@ -0,0 +1,133 @@
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef _MACHO_LDSYMS_H_
25#define _MACHO_LDSYMS_H_
26
27#include <mach-o/loader.h>
28
29/*
30 * This file describes the link editor defined symbols. The semantics of a
31 * link editor symbol is that it is defined by the link editor only if it is
32 * referenced and it is an error for the user to define them (see the man page
33 * ld(1)). The standard UNIX link editor symbols: __end, __etext and __edata
34 * are not not supported by the Apple Mach-O link editor. These symbols are
35 * really not meaningful in a Mach-O object file and the link editor symbols
36 * that are supported (described here) replace them. In the case of the
37 * standard UNIX link editor symbols the program can use the symbol
38 * __mh_execute_header and walk the load commands of it's program to determine
39 * the ending (or beginning) of any section or segment in the program. Note
40 * that the compiler prepends an underbar to all external symbol names coded
41 * in a high level language. Thus in 'C' names are coded without an underbar
42 * and symbol names in the symbol table have an underbar. There are two cpp
43 * macros for each link editor defined name in this file. The macro with a
44 * leading underbar is the symbol name and the one without is the name as
45 * coded in 'C'.
46 */
47
48/*
49 * The value of the link editor defined symbol _MH_EXECUTE_SYM is the address
50 * of the mach header in a Mach-O executable file type. It does not appear in
51 * any file type other than a MH_EXECUTE file type. The type of the symbol is
52 * absolute as the header is not part of any section.
53 */
54#define _MH_EXECUTE_SYM "__mh_execute_header"
55#define MH_EXECUTE_SYM "_mh_execute_header"
56extern const struct
57#ifdef __LP64__
58mach_header_64
59#else
60mach_header
61#endif
62_mh_execute_header;
63
64/*
65 * The value of the link editor defined symbol _MH_BUNDLE_SYM is the address
66 * of the mach header in a Mach-O bundle file type. It does not appear in
67 * any file type other than a MH_BUNDLE file type. The type of the symbol is
68 * an N_SECT symbol even thought the header is not part of any section. This
69 * symbol is private to the code in the bundle it is a part of.
70 */
71#define _MH_BUNDLE_SYM "__mh_bundle_header"
72#define MH_BUNDLE_SYM "_mh_bundle_header"
73extern const struct
74#ifdef __LP64__
75mach_header_64
76#else
77mach_header
78#endif
79_mh_bundle_header;
80
81/*
82 * The value of the link editor defined symbol _MH_DYLIB_SYM is the address
83 * of the mach header in a Mach-O dylib file type. It does not appear in
84 * any file type other than a MH_DYLIB file type. The type of the symbol is
85 * an N_SECT symbol even thought the header is not part of any section. This
86 * symbol is private to the code in the library it is a part of.
87 */
88#define _MH_DYLIB_SYM "__mh_dylib_header"
89#define MH_DYLIB_SYM "_mh_dylib_header"
90extern const struct
91#ifdef __LP64__
92mach_header_64
93#else
94mach_header
95#endif
96_mh_dylib_header;
97
98/*
99 * The value of the link editor defined symbol _MH_DYLINKER_SYM is the address
100 * of the mach header in a Mach-O dylinker file type. It does not appear in
101 * any file type other than a MH_DYLINKER file type. The type of the symbol is
102 * an N_SECT symbol even thought the header is not part of any section. This
103 * symbol is private to the code in the dynamic linker it is a part of.
104 */
105#define _MH_DYLINKER_SYM "__mh_dylinker_header"
106#define MH_DYLINKER_SYM "_mh_dylinker_header"
107extern const struct
108#ifdef __LP64__
109mach_header_64
110#else
111mach_header
112#endif
113_mh_dylinker_header;
114
115/*
116 * For the MH_PRELOAD file type the headers are not loaded as part of any
117 * segment so the link editor defines symbols defined for the beginning
118 * and ending of each segment and each section in each segment. The names for
119 * the symbols for a segment's beginning and end will have the form:
120 * __SEGNAME__begin and __SEGNAME__end where __SEGNAME is the name of the
121 * segment. The names for the symbols for a section's beginning and end will
122 * have the form: __SEGNAME__sectname__begin and __SEGNAME__sectname__end
123 * where __sectname is the name of the section and __SEGNAME is the segment it
124 * is in.
125 *
126 * The above symbols' types are those of the section they are referring to.
127 * This is true even for symbols who's values are end's of a section and
128 * that value is next address after that section and not really in that
129 * section. This results in these symbols having types referring to sections
130 * who's values are not in that section.
131 */
132
133#endif /* _MACHO_LDSYMS_H_ */
lib/libc/include/any-darwin-any/mach-o/swap.h created+284
......@@ -0,0 +1,284 @@
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23#ifndef _MACH_O_SWAP_H_
24#define _MACH_O_SWAP_H_
25
26#include <stdint.h>
27#include <architecture/byte_order.h>
28#include <mach-o/fat.h>
29#include <mach-o/loader.h>
30#include <mach-o/nlist.h>
31#include <mach-o/reloc.h>
32#include <mach-o/ranlib.h>
33#include <Availability.h>
34#include <TargetConditionals.h>
35
36#ifndef __CCTOOLS_DEPRECATED
37 #define __CCTOOLS_DEPRECATED __API_DEPRECATED("No longer supported", macos(10.0, 13.0), ios(1.0, 16.0), watchos(1.0, 8.0), tvos(1.0, 16.0))
38 #define __CCTOOLS_DEPRECATED_MSG(_msg) __API_DEPRECATED_WITH_REPLACEMENT(_msg, macos(10.0, 13.0), ios(1.0, 16.0), watchos(1.0, 8.0), tvos(1.0, 16.0))
39#endif
40
41#ifdef __cplusplus
42extern "C" {
43#endif /* __cplusplus */
44
45extern void swap_fat_header(
46 struct fat_header *fat_header,
47 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED_MSG("use macho_for_each_slice()");
48
49extern void swap_fat_arch(
50 struct fat_arch *fat_archs,
51 uint32_t nfat_arch,
52 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED_MSG("use macho_for_each_slice()");
53
54extern void swap_fat_arch_64(
55 struct fat_arch_64 *fat_archs64,
56 uint32_t nfat_arch,
57 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED_MSG("use macho_for_each_slice()");
58
59extern void swap_mach_header(
60 struct mach_header *mh,
61 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
62
63extern void swap_mach_header_64(
64 struct mach_header_64 *mh,
65 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
66
67extern void swap_load_command(
68 struct load_command *lc,
69 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
70
71extern void swap_segment_command(
72 struct segment_command *sg,
73 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
74
75extern void swap_segment_command_64(
76 struct segment_command_64 *sg,
77 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
78
79extern void swap_section(
80 struct section *s,
81 uint32_t nsects,
82 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
83
84extern void swap_section_64(
85 struct section_64 *s,
86 uint32_t nsects,
87 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
88
89extern void swap_symtab_command(
90 struct symtab_command *st,
91 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
92
93extern void swap_dysymtab_command(
94 struct dysymtab_command *dyst,
95 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
96
97extern void swap_symseg_command(
98 struct symseg_command *ss,
99 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
100
101extern void swap_fvmlib_command(
102 struct fvmlib_command *fl,
103 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
104
105extern void swap_dylib_command(
106 struct dylib_command *dl,
107 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
108
109extern void swap_sub_framework_command(
110 struct sub_framework_command *sub,
111 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
112
113extern void swap_sub_umbrella_command(
114 struct sub_umbrella_command *usub,
115 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
116
117extern void swap_sub_library_command(
118 struct sub_library_command *lsub,
119 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
120
121extern void swap_sub_client_command(
122 struct sub_client_command *csub,
123 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
124
125extern void swap_prebound_dylib_command(
126 struct prebound_dylib_command *pbdylib,
127 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
128
129extern void swap_dylinker_command(
130 struct dylinker_command *dyld,
131 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
132
133extern void swap_fvmfile_command(
134 struct fvmfile_command *ff,
135 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
136
137extern void swap_thread_command(
138 struct thread_command *ut,
139 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
140
141extern void swap_ident_command(
142 struct ident_command *ident,
143 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
144
145extern void swap_routines_command(
146 struct routines_command *r_cmd,
147 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
148
149extern void swap_routines_command_64(
150 struct routines_command_64 *r_cmd,
151 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
152
153extern void swap_twolevel_hints_command(
154 struct twolevel_hints_command *hints_cmd,
155 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
156
157extern void swap_prebind_cksum_command(
158 struct prebind_cksum_command *cksum_cmd,
159 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
160
161extern void swap_uuid_command(
162 struct uuid_command *uuid_cmd,
163 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
164
165extern void swap_linkedit_data_command(
166 struct linkedit_data_command *ld,
167 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
168
169extern void swap_version_min_command(
170 struct version_min_command *ver_cmd,
171 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
172extern void swap_rpath_command(
173 struct rpath_command *rpath_cmd,
174 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
175
176extern void swap_encryption_command(
177 struct encryption_info_command *ec,
178 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
179
180extern void swap_encryption_command_64(
181 struct encryption_info_command_64 *ec,
182 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
183
184extern void swap_linker_option_command(
185 struct linker_option_command *lo,
186 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
187
188extern void swap_dyld_info_command(
189 struct dyld_info_command *ed,
190 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
191
192extern void swap_entry_point_command(
193 struct entry_point_command *ep,
194 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
195
196extern void swap_source_version_command(
197 struct source_version_command *sv,
198 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
199
200extern void swap_note_command(
201 struct note_command *nc,
202 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
203
204extern void swap_build_version_command(
205 struct build_version_command *bv,
206 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
207
208extern void swap_build_tool_version(
209 struct build_tool_version *bt,
210 uint32_t ntools,
211 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
212
213extern void swap_prebind_cksum_command(
214 struct prebind_cksum_command *cksum_cmd,
215 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
216
217extern void swap_uuid_command(
218 struct uuid_command *uuid_cmd,
219 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
220
221extern void swap_twolevel_hint(
222 struct twolevel_hint *hints,
223 uint32_t nhints,
224 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
225
226extern void swap_fileset_entry_command(
227 struct fileset_entry_command *lc,
228 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
229
230extern void swap_nlist(
231 struct nlist *symbols,
232 uint32_t nsymbols,
233 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
234
235extern void swap_nlist_64(
236 struct nlist_64 *symbols,
237 uint32_t nsymbols,
238 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
239
240extern void swap_ranlib(
241 struct ranlib *ranlibs,
242 uint32_t nranlibs,
243 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
244
245extern void swap_ranlib_64(
246 struct ranlib_64 *ranlibs,
247 uint64_t nranlibs,
248 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
249
250extern void swap_relocation_info(
251 struct relocation_info *relocs,
252 uint32_t nrelocs,
253 enum NXByteOrder target_byte_order) __CCTOOLS_DEPRECATED;
254
255extern void swap_indirect_symbols(
256 uint32_t *indirect_symbols,
257 uint32_t nindirect_symbols,
258 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
259
260extern void swap_dylib_reference(
261 struct dylib_reference *refs,
262 uint32_t nrefs,
263 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
264
265extern void swap_dylib_module(
266 struct dylib_module *mods,
267 uint32_t nmods,
268 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
269
270extern void swap_dylib_module_64(
271 struct dylib_module_64 *mods,
272 uint32_t nmods,
273 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
274
275extern void swap_dylib_table_of_contents(
276 struct dylib_table_of_contents *tocs,
277 uint32_t ntocs,
278 enum NXByteOrder target_byte_sex) __CCTOOLS_DEPRECATED;
279
280#ifdef __cplusplus
281}
282#endif /* __cplusplus */
283
284#endif /* _MACH_O_SWAP_H_ */
lib/libc/include/any-darwin-any/mach-o/utils.h created+242
......@@ -0,0 +1,242 @@
1/*
2 * Copyright (c) 2021 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23#ifndef _MACH_O_UTILS_H_
24#define _MACH_O_UTILS_H_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <stdbool.h>
29
30#include <mach-o/loader.h>
31#include <Availability.h>
32
33#include <TargetConditionals.h>
34
35
36#if __cplusplus
37extern "C" {
38#endif
39
40/*!
41 * @function macho_cpu_type_for_arch_name
42 *
43 * @abstract
44 * Converts an architecture name into a cpu type/subtype pair.
45 *
46 * @param archName
47 * An architecture name (e.g "arm64e" or "x86_64").
48 *
49 * @param type
50 * A pointer to where to store the cpu type of the given name.
51 *
52 * @param subtype
53 * A pointer to where to store the cpu subtype of the given name.
54 *
55 * @return
56 * If the archName is known, returns true and fills in the type/subtype.
57 * If the archName is unknown, returns false.
58 */
59extern bool macho_cpu_type_for_arch_name(const char* _Nonnull archName, cpu_type_t* _Nonnull type, cpu_subtype_t* _Nonnull subtype)
60__API_AVAILABLE(macos(13.0), ios(16.0), tvos(16.0), watchos(8.0));
61
62
63/*!
64 * @function macho_arch_name_for_cpu_type
65 *
66 * @abstract
67 * Converts a cpu type/subtype pair into the architecture name.
68 *
69 * @param type
70 * The cpu type from <machine/machine.h> (e.g CPU_TYPE_ARM64)
71 *
72 * @param subtype
73 * The cpu subtype from <machine/machine.h> (e.g CPU_SUBTYPE_ARM64E)
74 *
75 * @return
76 * Returns a static c-string which is the name for the cpu type/subtype (e.g. "arm64e").
77 * If the cpu type/subtype is unknown, NULL will be returned.
78 * The string returned is static and does not need to be deallocated.
79 */
80extern const char* _Nullable macho_arch_name_for_cpu_type(cpu_type_t type, cpu_subtype_t subtype)
81__API_AVAILABLE(macos(13.0), ios(16.0), tvos(16.0), watchos(8.0));
82
83
84/*!
85 * @function macho_arch_name_for_mach_header
86 *
87 * @abstract
88 * Returns the architecture name from the cpu type/subtype in a mach_header.
89 * This is a convenience wrapper around macho_arch_name_for_cpu_type().
90 *
91 * @param mh
92 * A pointer to the header of a mach-o file.
93 * If NULL is passed, the architecture name of the main executable will be returned.
94 *
95 * @return
96 * Returns a static c-string which is the name for architecture of the mach-o file (e.g. "arm64e").
97 * If the architecture is unknown, NULL will be returned.
98 * The string returned is static and does not need to be deallocated.
99 */
100extern const char* _Nullable macho_arch_name_for_mach_header(const struct mach_header* _Nullable mh)
101__API_AVAILABLE(macos(13.0), ios(16.0), tvos(16.0), watchos(8.0));
102
103#ifdef __BLOCKS__
104 #if __has_attribute(noescape)
105 #define __MACHO_NOESCAPE __attribute__((__noescape__))
106 #else
107 #define __MACHO_NOESCAPE
108 #endif
109
110/*!
111 * @function macho_for_each_slice
112 *
113 * @abstract
114 * Temporarily maps a mach-o or universal file and iterates the slices.
115 * If the file is mach-o, the block is called once with the mach-o file mapped.
116 * If the file is universal (aka fat), the block is called once per slice in the order in the header.
117 * If the path does not exist or does, but is not a mach-o file, the block is never called.
118 *
119 * @param path
120 * The path to the file to inspect.
121 *
122 * @param callback
123 * A block to call once per slice.
124 * Can be NULL. In which case the return value tells you if the file is mach-o or fat.
125 * The slice pointer is only valid during the block invocation.
126 * To stop iterating the slices, set *stop to true.
127 *
128 * @return
129 * Returns zero on success, otherwise it returns an errno value.
130 * Common returned errors:
131 * ENOENT - path does not exist
132 * EACCES - path exists put caller does not have permission to access it
133 * EFTYPE - path exists but it is not a mach-o or fat file
134 * EBADMACHO - path is a mach-o file, but it is malformed
135 */
136extern int macho_for_each_slice(const char* _Nonnull path, void (^ _Nullable callback)(const struct mach_header* _Nonnull slice, uint64_t sliceFileOffset, size_t size, bool* _Nonnull stop) __MACHO_NOESCAPE)
137__API_AVAILABLE(macos(13.0), ios(16.0), tvos(16.0), watchos(8.0));
138
139
140/*!
141 * @function macho_for_each_slice_in_fd
142 *
143 * @abstract
144 * Temporarily maps a mach-o or universal file and iterates the slices.
145 * If the fd is to a mach-o, the block is called once with the mach-o file mapped.
146 * If the fd is to a universal (aka fat), the block is called once per slice in the order in the header.
147 * If the fd is closed or not mmap()able, the block is never called.
148 *
149 * @param fd
150 * An open file descriptor to a mmap()able file.
151 *
152 * @param callback
153 * A block to call once per slice.
154 * Can be NULL. In which case the return value tells you if the file is mach-o or fat.
155 * The slice pointer is only valid during the block invocation.
156 * To stop iterating the slices, set *stop to true.
157 *
158 * @return
159 * Returns zero on success, otherwise it returns an errno value.
160 * Common returned errors:
161 * EFTYPE - fd content is not a mach-o or fat file
162 * EBADMACHO - fd content is a mach-o file, but it is malformed
163 */
164extern int macho_for_each_slice_in_fd(int fd, void (^ _Nullable callback)(const struct mach_header* _Nonnull slice, uint64_t sliceFileOffset, size_t size, bool* _Nonnull stop)__MACHO_NOESCAPE)
165__API_AVAILABLE(macos(13.0), ios(16.0), tvos(16.0), watchos(8.0));
166
167
168/*!
169 * @function macho_best_slice
170 *
171 * @abstract
172 * Examines a mach-o or universal file to find the slice that would be loaded. That is, for dylib/bundles, which
173 * slice dyld would load. For main executables, which slice the kernel would use.
174 * In simulator processes, only other simulator main executables will be considered loadable.
175 * If the file is mach-o and is the right arch and platform to load, the block is called once with the mach-o file mapped.
176 * If the file is universal (aka fat) file, the best slice is found and the block is called once with the mapped slice.
177 * If the file is universal (aka fat) file, but none of the slices are loadable, the callback is not called, and EBADARCH is returned.
178 * If the path does not exist or does but is not a mach-o or universal file, the block is never called, and an error is returned.
179 *
180 * @param path
181 * The path to the file to inspect.
182 *
183 * @param callback
184 * A block to call once with the best slice.
185 * Can be NULL. In which case the return value tells you if there was a loadable slice
186 * The slice pointer is only valid during the block invocation.
187 *
188 * @return
189 * Returns zero on success (meaning there is a best slice), otherwise it returns an errno value.
190 * Common returned errors:
191 * ENOENT - path does not exist
192 * EACCES - path exists put caller does not have permission to access it
193 * EFTYPE - path exists but it is not a mach-o or fat file
194 * EBADARCH - path exists and is mach-o or fat, but none of the slices are loadable
195 * EBADMACHO - path is a mach-o file, but it is malformed
196 */
197extern int macho_best_slice(const char* _Nonnull path, void (^ _Nullable bestSlice)(const struct mach_header* _Nonnull slice, uint64_t sliceFileOffset, size_t sliceSize)__MACHO_NOESCAPE)
198__API_AVAILABLE(macos(13.0), ios(16.0), tvos(16.0), watchos(8.0));
199
200
201/*!
202 * @function macho_best_slice_in_fd
203 *
204 * @abstract
205 * Examines a mach-o or universal file to find the slice that would be loaded. That is, for dylib/bundles, which
206 * slice dyld would load. For main executables, which slice the kernel would use.
207 * In simulator processes, only other simulator main executables will be considered loadable.
208 * If the fd is to a mach-o and is the right arch and platform to load, the block is call once with the mach-o file mapped.
209 * If the fd is to a universal (aka fat) file, the best slice is found and the block is called once with the mapped slice.
210 * If the fd is closed or not mmap()able, the block is never called.
211 *
212 * @param fd
213 * An open file descriptor to a mmap()able file.
214 *
215 * @param callback
216 * A block to call once with the best slice.
217 * Can be NULL. In which case the return value tells you if there was a loadable slice
218 * The slice pointer is only valid during the block invocation.
219 *
220 * @return
221 * Returns zero on success (meaning there is a best slice), otherwise it returns an errno value.
222 * Common returned errors:
223 * EFTYPE - fd content is not a mach-o or fat file
224 * EBADMACHO - fd content is a mach-o file, but it is malformed
225 * EBADARCH - fd content is a mach-o or fat, but none of the slices are loadable
226 */
227extern int macho_best_slice_in_fd(int fd, void (^ _Nullable bestSlice)(const struct mach_header* _Nonnull slice, uint64_t sliceFileOffset, size_t sliceSize)__MACHO_NOESCAPE)
228__API_AVAILABLE(macos(13.0), ios(16.0), tvos(16.0), watchos(8.0));
229
230#endif // __BLOCKS__
231
232
233
234
235
236#if __cplusplus
237}
238#endif
239
240
241#endif // _MACH_O_UTILS_H_
242
tools/macos-headers.c+21-13
......@@ -114,6 +114,9 @@
114114#include <tgmath.h>
115115#include <time.h>
116116/* #include <trace.h> - not found on macos catalina */
117#define _XOPEN_SOURCE
118#include <ucontext.h>
119#undef _XOPEN_SOURCE
117120#include <ulimit.h>
118121#include <unistd.h>
119122#include <utime.h>
......@@ -138,20 +141,31 @@
138141#include <sys/clonefile.h>
139142#include <libproc.h>
140143
141// Depended on by libcxx
142#include <Block.h>
143#include <xlocale.h>
144#include <copyfile.h>
144// Mach-O headers
145#include <mach-o/arch.h>
146#include <mach-o/compact_unwind_encoding.h>
145147#include <mach-o/dyld.h>
148#include <mach-o/dyld_images.h>
146149#include <mach-o/fat.h>
150#include <mach-o/fixup-chains.h>
151#include <mach-o/getsect.h>
152#include <mach-o/ldsyms.h>
153#include <mach-o/loader.h>
147154#include <mach-o/nlist.h>
155#include <mach-o/ranlib.h>
148156#include <mach-o/reloc.h>
149#include <mach-o/arch.h>
150157#include <mach-o/stab.h>
151#include <mach-o/ranlib.h>
152#include <mach-o/compact_unwind_encoding.h>
158#include <mach-o/swap.h>
159#include <mach-o/utils.h>
160
161#include <mach-o/arm/reloc.h>
153162#include <mach-o/arm64/reloc.h>
154163#include <mach-o/x86_64/reloc.h>
164
165// Depended on by libcxx
166#include <Block.h>
167#include <xlocale.h>
168#include <copyfile.h>
155169#include <ar.h>
156170
157171// Depended on by LLVM
......@@ -197,16 +211,10 @@
197211#include <sys/file.h>
198212#include <malloc/malloc.h>
199213
200// Depended on by bdwgc
201#include <mach-o/getsect.h>
202
203214// Provided by macOS LibC
204215#include <memory.h>
205216#include <zlib.h>
206217
207#define _XOPEN_SOURCE
208#include <ucontext.h>
209
210218int main(int argc, char **argv) {
211219 return 0;
212220}