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