dyld-625.13.tar.gz
[apple/dyld.git] / include / mach-o / dyld_priv.h
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2003-2010 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 #ifndef _MACH_O_DYLD_PRIV_H_
25 #define _MACH_O_DYLD_PRIV_H_
26
27 #include <assert.h>
28 #include <stdbool.h>
29 #include <Availability.h>
30 #include <TargetConditionals.h>
31 #include <mach-o/dyld.h>
32 #include <mach-o/dyld_images.h>
33 #include <uuid/uuid.h>
34
35 #if __cplusplus
36 extern "C" {
37 #endif /* __cplusplus */
38
39
40
41 //
42 // private interface between libSystem.dylib and dyld
43 //
44 extern void _dyld_fork_child(void);
45
46
47 // DEPRECATED
48 enum dyld_image_states
49 {
50 dyld_image_state_mapped = 10, // No batch notification for this
51 dyld_image_state_dependents_mapped = 20, // Only batch notification for this
52 dyld_image_state_rebased = 30,
53 dyld_image_state_bound = 40,
54 dyld_image_state_dependents_initialized = 45, // Only single notification for this
55 dyld_image_state_initialized = 50,
56 dyld_image_state_terminated = 60 // Only single notification for this
57 };
58
59 // DEPRECATED
60 typedef const char* (*dyld_image_state_change_handler)(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[]);
61
62
63
64 typedef void (*_dyld_objc_notify_mapped)(unsigned count, const char* const paths[], const struct mach_header* const mh[]);
65 typedef void (*_dyld_objc_notify_init)(const char* path, const struct mach_header* mh);
66 typedef void (*_dyld_objc_notify_unmapped)(const char* path, const struct mach_header* mh);
67
68
69 //
70 // Note: only for use by objc runtime
71 // Register handlers to be called when objc images are mapped, unmapped, and initialized.
72 // Dyld will call back the "mapped" function with an array of images that contain an objc-image-info section.
73 // Those images that are dylibs will have the ref-counts automatically bumped, so objc will no longer need to
74 // call dlopen() on them to keep them from being unloaded. During the call to _dyld_objc_notify_register(),
75 // dyld will call the "mapped" function with already loaded objc images. During any later dlopen() call,
76 // dyld will also call the "mapped" function. Dyld will call the "init" function when dyld would be called
77 // initializers in that image. This is when objc calls any +load methods in that image.
78 //
79 void _dyld_objc_notify_register(_dyld_objc_notify_mapped mapped,
80 _dyld_objc_notify_init init,
81 _dyld_objc_notify_unmapped unmapped);
82
83
84
85 //
86 // Possible thread-local variable state changes for which you can register to be notified
87 //
88 enum dyld_tlv_states {
89 dyld_tlv_state_allocated = 10, // TLV range newly allocated
90 dyld_tlv_state_deallocated = 20 // TLV range about to be deallocated
91 };
92
93 //
94 // Info about thread-local variable storage.
95 //
96 typedef struct {
97 size_t info_size; // sizeof(dyld_tlv_info)
98 void * tlv_addr; // Base address of TLV storage
99 size_t tlv_size; // Byte size of TLV storage
100 } dyld_tlv_info;
101
102 #if __BLOCKS__
103
104 //
105 // Callback that notes changes to thread-local variable storage.
106 //
107 typedef void (^dyld_tlv_state_change_handler)(enum dyld_tlv_states state, const dyld_tlv_info *info);
108
109 //
110 // Register a handler to be called when a thread adds or removes storage for thread-local variables.
111 // The registered handler will only be called from and on behalf of the thread that owns the storage.
112 // The registered handler will NOT be called for any storage that was
113 // already allocated before dyld_register_tlv_state_change_handler() was
114 // called. Use dyld_enumerate_tlv_storage() to get that information.
115 // Exists in Mac OS X 10.7 and later
116 //
117 extern void
118 dyld_register_tlv_state_change_handler(enum dyld_tlv_states state, dyld_tlv_state_change_handler handler);
119
120 //
121 // Enumerate the current thread-local variable storage allocated for the current thread.
122 // Exists in Mac OS X 10.7 and later
123 //
124 extern void
125 dyld_enumerate_tlv_storage(dyld_tlv_state_change_handler handler);
126
127 #endif
128
129
130 //
131 // get slide for a given loaded mach_header
132 // Mac OS X 10.6 and later
133 //
134 extern intptr_t _dyld_get_image_slide(const struct mach_header* mh);
135
136
137
138 struct dyld_unwind_sections
139 {
140 const struct mach_header* mh;
141 const void* dwarf_section;
142 uintptr_t dwarf_section_length;
143 const void* compact_unwind_section;
144 uintptr_t compact_unwind_section_length;
145 };
146
147
148 //
149 // Returns true iff some loaded mach-o image contains "addr".
150 // info->mh mach header of image containing addr
151 // info->dwarf_section pointer to start of __TEXT/__eh_frame section
152 // info->dwarf_section_length length of __TEXT/__eh_frame section
153 // info->compact_unwind_section pointer to start of __TEXT/__unwind_info section
154 // info->compact_unwind_section_length length of __TEXT/__unwind_info section
155 //
156 // Exists in Mac OS X 10.6 and later
157 #if !__USING_SJLJ_EXCEPTIONS__
158 extern bool _dyld_find_unwind_sections(void* addr, struct dyld_unwind_sections* info);
159 #endif
160
161
162 //
163 // This is an optimized form of dladdr() that only returns the dli_fname field.
164 //
165 // Exists in Mac OS X 10.6 and later
166 extern const char* dyld_image_path_containing_address(const void* addr);
167
168
169 //
170 // This is an optimized form of dladdr() that only returns the dli_fbase field.
171 // Return NULL, if address is not in any image tracked by dyld.
172 //
173 // Exists in Mac OS X 10.11 and later
174 extern const struct mach_header* dyld_image_header_containing_address(const void* addr);
175
176 typedef uint32_t dyld_platform_t;
177
178 typedef struct {
179 dyld_platform_t platform;
180 uint32_t version;
181 } dyld_build_version_t;
182
183 // Returns the active platform of the process
184 extern dyld_platform_t dyld_get_active_platform(void) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
185
186 // Base platforms are platforms that have version numbers (macOS, iOS, watchos, tvOS, bridgeOS)
187 // All other platforms are mapped to a base platform for version checks
188 extern dyld_platform_t dyld_get_base_platform(dyld_platform_t platform) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
189
190 // SPI to ask if a platform is a simulation platform
191 extern bool dyld_is_simulator_platform(dyld_platform_t platform) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
192
193 // Takes a version and returns if the image was built againt that SDK or newer
194 // In the case of multi_plaform mach-o's it tests against the active platform
195 extern bool dyld_sdk_at_least(const struct mach_header* mh, dyld_build_version_t version) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
196
197 // Takes a version and returns if the image was built with that minos version or newer
198 // In the case of multi_plaform mach-o's it tests against the active platform
199 extern bool dyld_minos_at_least(const struct mach_header* mh, dyld_build_version_t version) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
200
201 // Convenience versions of the previous two functions that run against the the main executable
202 extern bool dyld_program_sdk_at_least(dyld_build_version_t version) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
203 extern bool dyld_program_minos_at_least(dyld_build_version_t version) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
204
205 // Function that walks through the load commands and calls the internal block for every version found
206 // Intended as a fallback for very complex (and rare) version checks, or for tools that need to
207 // print our everything for diagnostic reasons
208 extern void dyld_get_image_versions(const struct mach_header* mh, void (^callback)(dyld_platform_t platform, uint32_t sdk_version, uint32_t min_version)) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
209
210 // Convienence constants for dyld version SPIs.
211
212 //@VERSION_SET_DEFS@
213
214 //@MACOS_PLATFORM_VERSION_DEFS@
215
216 //@IOS_PLATFORM_VERSION_DEFS@
217
218 //@WATCHOS_PLATFORM_VERSION_DEFS@
219
220 //@TVOS_PLATFORM_VERSION_DEFS@
221
222 //@BRIDGEOS_PLATFORM_VERSION_DEFS@
223
224 // Convienence constants for return values from dyld_get_sdk_version() and friends.
225
226 //@MAC_VERSION_DEFS@
227
228 //@IOS_VERSION_DEFS@
229
230 //@WATCHOS_VERSION_DEFS@
231
232 //
233 // This finds the SDK version a binary was built against.
234 // Returns zero on error, or if SDK version could not be determined.
235 //
236 // Exists in Mac OS X 10.8 and later
237 // Exists in iOS 6.0 and later
238 extern uint32_t dyld_get_sdk_version(const struct mach_header* mh);
239
240
241 //
242 // This finds the SDK version that the main executable was built against.
243 // Returns zero on error, or if SDK version could not be determined.
244 //
245 // Note on watchOS, this returns the equivalent iOS SDK version number
246 // (i.e an app built against watchOS 2.0 SDK returne 9.0). To see the
247 // platform specific sdk version use dyld_get_program_sdk_watch_os_version().
248 //
249 // Exists in Mac OS X 10.8 and later
250 // Exists in iOS 6.0 and later
251 extern uint32_t dyld_get_program_sdk_version(void);
252
253 #if TARGET_OS_WATCH
254 // watchOS only.
255 // This finds the Watch OS SDK version that the main executable was built against.
256 // Exists in Watch OS 2.0 and later
257 extern uint32_t dyld_get_program_sdk_watch_os_version(void) __API_AVAILABLE(watchos(2.0));
258
259
260 // watchOS only.
261 // This finds the Watch min OS version that the main executable was built to run on.
262 // Note: dyld_get_program_min_os_version() returns the iOS equivalent (e.g. 9.0)
263 // whereas this returns the raw watchOS version (e.g. 2.0).
264 // Exists in Watch OS 3.0 and later
265 extern uint32_t dyld_get_program_min_watch_os_version(void) __API_AVAILABLE(watchos(3.0));
266 #endif
267
268 #if TARGET_OS_BRIDGE
269 // bridgeOS only.
270 // This finds the bridgeOS SDK version that the main executable was built against.
271 // Exists in bridgeOSOS 2.0 and later
272 extern uint32_t dyld_get_program_sdk_bridge_os_version(void) __API_AVAILABLE(bridgeos(2.0));
273
274 // bridgeOS only.
275 // This finds the Watch min OS version that the main executable was built to run on.
276 // Note: dyld_get_program_min_os_version() returns the iOS equivalent (e.g. 9.0)
277 // whereas this returns the raw bridgeOS version (e.g. 2.0).
278 // Exists in bridgeOS 2.0 and later
279 extern uint32_t dyld_get_program_min_bridge_os_version(void) __API_AVAILABLE(bridgeos(2.0));
280 #endif
281
282 //
283 // This finds the min OS version a binary was built to run on.
284 // Returns zero on error, or if no min OS recorded in binary.
285 //
286 // Exists in Mac OS X 10.8 and later
287 // Exists in iOS 6.0 and later
288 extern uint32_t dyld_get_min_os_version(const struct mach_header* mh);
289
290
291 //
292 // This finds the min OS version the main executable was built to run on.
293 // Returns zero on error, or if no min OS recorded in binary.
294 //
295 // Exists in Mac OS X 10.8 and later
296 // Exists in iOS 6.0 and later
297 extern uint32_t dyld_get_program_min_os_version(void);
298
299
300
301
302 //
303 // Returns if any OS dylib has overridden its copy in the shared cache
304 //
305 // Exists in iPhoneOS 3.1 and later
306 // Exists in Mac OS X 10.10 and later
307 extern bool dyld_shared_cache_some_image_overridden(void);
308
309
310
311 //
312 // Returns if the process is setuid or is code signed with entitlements.
313 //
314 // Exists in Mac OS X 10.9 and later
315 extern bool dyld_process_is_restricted(void);
316
317
318
319 //
320 // Returns path used by dyld for standard dyld shared cache file for the current arch.
321 //
322 // Exists in Mac OS X 10.11 and later
323 extern const char* dyld_shared_cache_file_path(void);
324
325
326
327 //
328 // <rdar://problem/13820686> for OpenGL to tell dyld it is ok to deallocate a memory based image when done.
329 //
330 // Exists in Mac OS X 10.9 and later
331 #define NSLINKMODULE_OPTION_CAN_UNLOAD 0x20
332
333
334 //
335 // Update all bindings on specified image.
336 // Looks for uses of 'replacement' and changes it to 'replacee'.
337 // NOTE: this is less safe than using static interposing via DYLD_INSERT_LIBRARIES
338 // because the running program may have already copy the pointer values to other
339 // locations that dyld does not know about.
340 //
341 struct dyld_interpose_tuple {
342 const void* replacement;
343 const void* replacee;
344 };
345 extern void dyld_dynamic_interpose(const struct mach_header* mh, const struct dyld_interpose_tuple array[], size_t count);
346
347
348 struct dyld_shared_cache_dylib_text_info {
349 uint64_t version; // current version 1
350 // following fields all exist in version 1
351 uint64_t loadAddressUnslid;
352 uint64_t textSegmentSize;
353 uuid_t dylibUuid;
354 const char* path; // pointer invalid at end of iterations
355 // following fields all exist in version 2
356 uint64_t textSegmentOffset; // offset from start of cache
357 };
358 typedef struct dyld_shared_cache_dylib_text_info dyld_shared_cache_dylib_text_info;
359
360
361 #ifdef __BLOCKS__
362 //
363 // Given the UUID of a dyld shared cache file, this function will attempt to locate the cache
364 // file and if found iterate all images, returning info about each one. Returns 0 on success.
365 //
366 // Exists in Mac OS X 10.11 and later
367 // iOS 9.0 and later
368 extern int dyld_shared_cache_iterate_text(const uuid_t cacheUuid, void (^callback)(const dyld_shared_cache_dylib_text_info* info));
369
370
371 //
372 // Given the UUID of a dyld shared cache file, and a NULL terminated array of extra directory paths to search,
373 // this function will scan the standard and extra directories looking for a cache file that matches the UUID
374 // and if found iterate all images, returning info about each one. Returns 0 on success.
375 //
376 // Exists in Mac OS X 10.12 and later
377 // iOS 10.0 and later
378 extern int dyld_shared_cache_find_iterate_text(const uuid_t cacheUuid, const char* extraSearchDirs[], void (^callback)(const dyld_shared_cache_dylib_text_info* info));
379 #endif /* __BLOCKS */
380
381
382 //
383 // Returns if the specified address range is in a dyld owned memory
384 // that is mapped read-only and will never be unloaded.
385 //
386 // Exists in Mac OS X 10.12 and later
387 // iOS 10.0 and later
388 extern bool _dyld_is_memory_immutable(const void* addr, size_t length);
389
390
391 //
392 // Finds the UUID (from LC_UUID load command) of given image.
393 // Returns false if LC_UUID is missing or mach_header is malformed.
394 //
395 // Exists in Mac OS X 10.12 and later
396 // Exists in iOS 10.0 and later
397 extern bool _dyld_get_image_uuid(const struct mach_header* mh, uuid_t uuid);
398
399
400 //
401 // Gets the UUID of the dyld shared cache in the current process.
402 // Returns false if there is no dyld shared cache in use by the processes.
403 //
404 // Exists in Mac OS X 10.12 and later
405 // Exists in iOS 10.0 and later
406 extern bool _dyld_get_shared_cache_uuid(uuid_t uuid);
407
408
409 //
410 // Returns the start address of the dyld cache in the process and sets length to the size of the cache.
411 // Returns NULL if the process is not using a dyld shared cache
412 //
413 // Exists in Mac OS X 10.13 and later
414 // Exists in iOS 11.0 and later
415 extern const void* _dyld_get_shared_cache_range(size_t* length);
416
417
418
419
420 struct dyld_image_uuid_offset {
421 uuid_t uuid;
422 uint64_t offsetInImage;
423 const struct mach_header* image;
424 };
425
426 //
427 // Given an array of addresses, returns info about each address.
428 // Common usage is the array or addresses was produced by a stack backtrace.
429 // For each address, returns the where that image was loaded, the offset
430 // of the address in the image, and the image's uuid. If a specified
431 // address is unknown to dyld, all fields will be returned a zeros.
432 //
433 // Exists in macOS 10.14 and later
434 // Exists in iOS 12.0 and later
435 extern void _dyld_images_for_addresses(unsigned count, const void* addresses[], struct dyld_image_uuid_offset infos[]);
436
437
438 //
439 // Lets you register a callback which is called each time an image is loaded and provides the mach_header*, path, and
440 // whether the image may be unloaded later. During the call to _dyld_register_for_image_loads(), the callback is called
441 // once for each image currently loaded.
442 //
443 // Exists in macOS 10.14 and later
444 // Exists in iOS 12.0 and later
445 extern void _dyld_register_for_image_loads(void (*func)(const struct mach_header* mh, const char* path, bool unloadable));
446
447
448
449 //
450 // When dyld must terminate a process because of a required dependent dylib
451 // could not be loaded or a symbol is missing, dyld calls abort_with_reason()
452 // using one of the following error codes.
453 //
454 #define DYLD_EXIT_REASON_DYLIB_MISSING 1
455 #define DYLD_EXIT_REASON_DYLIB_WRONG_ARCH 2
456 #define DYLD_EXIT_REASON_DYLIB_WRONG_VERSION 3
457 #define DYLD_EXIT_REASON_SYMBOL_MISSING 4
458 #define DYLD_EXIT_REASON_CODE_SIGNATURE 5
459 #define DYLD_EXIT_REASON_FILE_SYSTEM_SANDBOX 6
460 #define DYLD_EXIT_REASON_MALFORMED_MACHO 7
461 #define DYLD_EXIT_REASON_OTHER 9
462
463 //
464 // When it has more information about the termination, dyld will use abort_with_payload().
465 // The payload is a dyld_abort_payload structure. The fixed fields are offsets into the
466 // payload for the corresponding string. If the offset is zero, that string is not available.
467 //
468 struct dyld_abort_payload {
469 uint32_t version; // first version is 1
470 uint32_t flags; // 0x00000001 means dyld terminated at launch, backtrace not useful
471 uint32_t targetDylibPathOffset; // offset in payload of path string to dylib that could not be loaded
472 uint32_t clientPathOffset; // offset in payload of path string to image requesting dylib
473 uint32_t symbolOffset; // offset in payload of symbol string that could not be found
474 // string data
475 };
476 typedef struct dyld_abort_payload dyld_abort_payload;
477
478
479 // These global variables are implemented in libdyld.dylib
480 // Old programs that used crt1.o also defined these globals.
481 // The ones in dyld are not used when an old program is run.
482 extern int NXArgc;
483 extern const char** NXArgv;
484 extern char** environ; // POSIX says this not const, because it pre-dates const
485 extern const char* __progname;
486
487
488 // called by libSystem_initializer only
489 extern void _dyld_initializer(void);
490
491 // never called from source code. Used by static linker to implement lazy binding
492 extern void dyld_stub_binder(void) __asm__("dyld_stub_binder");
493
494
495 // called by exit() before it calls cxa_finalize() so that thread_local
496 // objects are destroyed before global objects.
497 extern void _tlv_exit(void);
498
499
500 // temp exports to keep tapi happy, until ASan stops using dyldVersionNumber
501 extern double dyldVersionNumber;
502 extern const char* dyldVersionString;
503
504 #if __cplusplus
505 }
506 #endif /* __cplusplus */
507
508 #endif /* _MACH_O_DYLD_PRIV_H_ */