1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2003-2010 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
24 #ifndef _MACH_O_DYLD_PRIV_H_
25 #define _MACH_O_DYLD_PRIV_H_
28 #include <Availability.h>
29 #include <TargetConditionals.h>
30 #include <mach-o/dyld.h>
31 #include <mach-o/dyld_images.h>
35 #endif /* __cplusplus */
40 // private interface between libSystem.dylib and dyld
42 extern void _dyld_fork_child();
46 enum dyld_image_states
48 dyld_image_state_mapped
= 10, // No batch notification for this
49 dyld_image_state_dependents_mapped
= 20, // Only batch notification for this
50 dyld_image_state_rebased
= 30,
51 dyld_image_state_bound
= 40,
52 dyld_image_state_dependents_initialized
= 45, // Only single notification for this
53 dyld_image_state_initialized
= 50,
54 dyld_image_state_terminated
= 60 // Only single notification for this
58 typedef const char* (*dyld_image_state_change_handler
)(enum dyld_image_states state
, uint32_t infoCount
, const struct dyld_image_info info
[]);
62 typedef void (*_dyld_objc_notify_mapped
)(unsigned count
, const char* const paths
[], const struct mach_header
* const mh
[]);
63 typedef void (*_dyld_objc_notify_init
)(const char* path
, const struct mach_header
* mh
);
64 typedef void (*_dyld_objc_notify_unmapped
)(const char* path
, const struct mach_header
* mh
);
68 // Note: only for use by objc runtime
69 // Register handlers to be called when objc images are mapped, unmapped, and initialized.
70 // Dyld will call back the "mapped" function with an array of images that contain an objc-image-info section.
71 // Those images that are dylibs will have the ref-counts automatically bumped, so objc will no longer need to
72 // call dlopen() on them to keep them from being unloaded. During the call to _dyld_objc_notify_register(),
73 // dyld will call the "mapped" function with already loaded objc images. During any later dlopen() call,
74 // dyld will also call the "mapped" function. Dyld will call the "init" function when dyld would be called
75 // initializers in that image. This is when objc calls any +load methods in that image.
77 void _dyld_objc_notify_register(_dyld_objc_notify_mapped mapped
,
78 _dyld_objc_notify_init init
,
79 _dyld_objc_notify_unmapped unmapped
);
84 // Possible thread-local variable state changes for which you can register to be notified
86 enum dyld_tlv_states
{
87 dyld_tlv_state_allocated
= 10, // TLV range newly allocated
88 dyld_tlv_state_deallocated
= 20 // TLV range about to be deallocated
92 // Info about thread-local variable storage.
95 size_t info_size
; // sizeof(dyld_tlv_info)
96 void * tlv_addr
; // Base address of TLV storage
97 size_t tlv_size
; // Byte size of TLV storage
103 // Callback that notes changes to thread-local variable storage.
105 typedef void (^dyld_tlv_state_change_handler
)(enum dyld_tlv_states state
, const dyld_tlv_info
*info
);
108 // Register a handler to be called when a thread adds or removes storage for thread-local variables.
109 // The registered handler will only be called from and on behalf of the thread that owns the storage.
110 // The registered handler will NOT be called for any storage that was
111 // already allocated before dyld_register_tlv_state_change_handler() was
112 // called. Use dyld_enumerate_tlv_storage() to get that information.
113 // Exists in Mac OS X 10.7 and later
116 dyld_register_tlv_state_change_handler(enum dyld_tlv_states state
, dyld_tlv_state_change_handler handler
);
119 // Enumerate the current thread-local variable storage allocated for the current thread.
120 // Exists in Mac OS X 10.7 and later
123 dyld_enumerate_tlv_storage(dyld_tlv_state_change_handler handler
);
129 // get slide for a given loaded mach_header
130 // Mac OS X 10.6 and later
132 extern intptr_t _dyld_get_image_slide(const struct mach_header
* mh
);
136 struct dyld_unwind_sections
138 const struct mach_header
* mh
;
139 const void* dwarf_section
;
140 uintptr_t dwarf_section_length
;
141 const void* compact_unwind_section
;
142 uintptr_t compact_unwind_section_length
;
147 // Returns true iff some loaded mach-o image contains "addr".
148 // info->mh mach header of image containing addr
149 // info->dwarf_section pointer to start of __TEXT/__eh_frame section
150 // info->dwarf_section_length length of __TEXT/__eh_frame section
151 // info->compact_unwind_section pointer to start of __TEXT/__unwind_info section
152 // info->compact_unwind_section_length length of __TEXT/__unwind_info section
154 // Exists in Mac OS X 10.6 and later
155 #if !__USING_SJLJ_EXCEPTIONS__
156 extern bool _dyld_find_unwind_sections(void* addr
, struct dyld_unwind_sections
* info
);
161 // This is an optimized form of dladdr() that only returns the dli_fname field.
163 // Exists in Mac OS X 10.6 and later
164 extern const char* dyld_image_path_containing_address(const void* addr
);
168 // This is an optimized form of dladdr() that only returns the dli_fbase field.
169 // Return NULL, if address is not in any image tracked by dyld.
171 // Exists in Mac OS X 10.11 and later
172 extern const struct mach_header
* dyld_image_header_containing_address(const void* addr
);
176 // Convienence constants for return values from dyld_get_sdk_version() and friends.
182 //@WATCHOS_VERSION_DEFS@
186 // This finds the SDK version a binary was built against.
187 // Returns zero on error, or if SDK version could not be determined.
189 // Exists in Mac OS X 10.8 and later
190 // Exists in iOS 6.0 and later
191 extern uint32_t dyld_get_sdk_version(const struct mach_header
* mh
);
195 // This finds the SDK version that the main executable was built against.
196 // Returns zero on error, or if SDK version could not be determined.
198 // Note on watchOS, this returns the equivalent iOS SDK version number
199 // (i.e an app built against watchOS 2.0 SDK returne 9.0). To see the
200 // platform specific sdk version use dyld_get_program_sdk_watch_os_version().
202 // Exists in Mac OS X 10.8 and later
203 // Exists in iOS 6.0 and later
204 extern uint32_t dyld_get_program_sdk_version();
207 #if __WATCH_OS_VERSION_MIN_REQUIRED
209 // This finds the Watch OS SDK version that the main executable was built against.
210 // Exists in Watch OS 2.0 and later
211 extern uint32_t dyld_get_program_sdk_watch_os_version() __IOS_UNAVAILABLE __OSX_UNAVAILABLE
__WATCHOS_AVAILABLE(2.0);
215 // This finds the Watch min OS version that the main executable was built to run on.
216 // Note: dyld_get_program_min_os_version() returns the iOS equivalent (e.g. 9.0)
217 // whereas this returns the raw watchOS version (e.g. 2.0).
218 // Exists in Watch OS 3.0 and later
219 extern uint32_t dyld_get_program_min_watch_os_version(); // __WATCHOS_AVAILABLE(3.0);
225 // This finds the bridgeOS SDK version that the main executable was built against.
226 // Exists in bridgeOSOS 2.0 and later
227 extern uint32_t dyld_get_program_sdk_bridge_os_version();
230 // This finds the Watch min OS version that the main executable was built to run on.
231 // Note: dyld_get_program_min_os_version() returns the iOS equivalent (e.g. 9.0)
232 // whereas this returns the raw bridgeOS version (e.g. 2.0).
233 // Exists in bridgeOS 2.0 and later
234 extern uint32_t dyld_get_program_min_bridge_os_version();
238 // This finds the min OS version a binary was built to run on.
239 // Returns zero on error, or if no min OS recorded in binary.
241 // Exists in Mac OS X 10.8 and later
242 // Exists in iOS 6.0 and later
243 extern uint32_t dyld_get_min_os_version(const struct mach_header
* mh
);
247 // This finds the min OS version the main executable was built to run on.
248 // Returns zero on error, or if no min OS recorded in binary.
250 // Exists in Mac OS X 10.8 and later
251 // Exists in iOS 6.0 and later
252 extern uint32_t dyld_get_program_min_os_version();
258 // Returns if any OS dylib has overridden its copy in the shared cache
260 // Exists in iPhoneOS 3.1 and later
261 // Exists in Mac OS X 10.10 and later
262 extern bool dyld_shared_cache_some_image_overridden();
267 // Returns if the process is setuid or is code signed with entitlements.
269 // Exists in Mac OS X 10.9 and later
270 extern bool dyld_process_is_restricted();
275 // Returns path used by dyld for standard dyld shared cache file for the current arch.
277 // Exists in Mac OS X 10.11 and later
278 extern const char* dyld_shared_cache_file_path();
283 // <rdar://problem/13820686> for OpenGL to tell dyld it is ok to deallocate a memory based image when done.
285 // Exists in Mac OS X 10.9 and later
286 #define NSLINKMODULE_OPTION_CAN_UNLOAD 0x20
290 // Update all bindings on specified image.
291 // Looks for uses of 'replacement' and changes it to 'replacee'.
292 // NOTE: this is less safe than using static interposing via DYLD_INSERT_LIBRARIES
293 // because the running program may have already copy the pointer values to other
294 // locations that dyld does not know about.
296 struct dyld_interpose_tuple
{
297 const void* replacement
;
298 const void* replacee
;
300 extern void dyld_dynamic_interpose(const struct mach_header
* mh
, const struct dyld_interpose_tuple array
[], size_t count
);
303 struct dyld_shared_cache_dylib_text_info
{
304 uint64_t version
; // current version 1
305 // following fields all exist in version 1
306 uint64_t loadAddressUnslid
;
307 uint64_t textSegmentSize
;
309 const char* path
; // pointer invalid at end of iterations
310 // following fields all exist in version 2
311 uint64_t textSegmentOffset
; // offset from start of cache
313 typedef struct dyld_shared_cache_dylib_text_info dyld_shared_cache_dylib_text_info
;
318 // Given the UUID of a dyld shared cache file, this function will attempt to locate the cache
319 // file and if found iterate all images, returning info about each one. Returns 0 on success.
321 // Exists in Mac OS X 10.11 and later
323 extern int dyld_shared_cache_iterate_text(const uuid_t cacheUuid
, void (^callback
)(const dyld_shared_cache_dylib_text_info
* info
));
327 // Given the UUID of a dyld shared cache file, and a NULL terminated array of extra directory paths to search,
328 // this function will scan the standard and extra directories looking for a cache file that matches the UUID
329 // and if found iterate all images, returning info about each one. Returns 0 on success.
331 // Exists in Mac OS X 10.12 and later
332 // iOS 10.0 and later
333 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
));
334 #endif /* __BLOCKS */
338 // Returns if the specified address range is in a dyld owned memory
339 // that is mapped read-only and will never be unloaded.
341 // Exists in Mac OS X 10.12 and later
342 // iOS 10.0 and later
343 extern bool _dyld_is_memory_immutable(const void* addr
, size_t length
);
347 // Finds the UUID (from LC_UUID load command) of given image.
348 // Returns false if LC_UUID is missing or mach_header is malformed.
350 // Exists in Mac OS X 10.12 and later
351 // Exists in iOS 10.0 and later
352 extern bool _dyld_get_image_uuid(const struct mach_header
* mh
, uuid_t uuid
);
356 // Gets the UUID of the dyld shared cache in the current process.
357 // Returns false if there is no dyld shared cache in use by the processes.
359 // Exists in Mac OS X 10.12 and later
360 // Exists in iOS 10.0 and later
361 extern bool _dyld_get_shared_cache_uuid(uuid_t uuid
);
365 // Returns the start address of the dyld cache in the process and sets length to the size of the cache.
366 // Returns NULL if the process is not using a dyld shared cache
368 // Exists in Mac OS X 10.13 and later
369 // Exists in iOS 11.0 and later
370 extern const void* _dyld_get_shared_cache_range(size_t* length
);
375 // When dyld must terminate a process because of a required dependent dylib
376 // could not be loaded or a symbol is missing, dyld calls abort_with_reason()
377 // using one of the following error codes.
379 #define DYLD_EXIT_REASON_DYLIB_MISSING 1
380 #define DYLD_EXIT_REASON_DYLIB_WRONG_ARCH 2
381 #define DYLD_EXIT_REASON_DYLIB_WRONG_VERSION 3
382 #define DYLD_EXIT_REASON_SYMBOL_MISSING 4
383 #define DYLD_EXIT_REASON_CODE_SIGNATURE 5
384 #define DYLD_EXIT_REASON_FILE_SYSTEM_SANDBOX 6
385 #define DYLD_EXIT_REASON_MALFORMED_MACHO 7
386 #define DYLD_EXIT_REASON_OTHER 9
389 // When it has more information about the termination, dyld will use abort_with_payload().
390 // The payload is a dyld_abort_payload structure. The fixed fields are offsets into the
391 // payload for the corresponding string. If the offset is zero, that string is not available.
393 struct dyld_abort_payload
{
394 uint32_t version
; // first version is 1
395 uint32_t flags
; // 0x00000001 means dyld terminated at launch, backtrace not useful
396 uint32_t targetDylibPathOffset
; // offset in payload of path string to dylib that could not be loaded
397 uint32_t clientPathOffset
; // offset in payload of path string to image requesting dylib
398 uint32_t symbolOffset
; // offset in payload of symbol string that could not be found
401 typedef struct dyld_abort_payload dyld_abort_payload
;
404 // These global variables are implemented in libdyld.dylib
405 // Old programs that used crt1.o also defined these globals.
406 // The ones in dyld are not used when an old program is run.
408 extern const char** NXArgv
;
409 extern char** environ
; // POSIX says this not const, because it pre-dates const
410 extern const char* __progname
;
413 // called by libSystem_initializer only
414 extern void _dyld_initializer();
416 // never called from source code. Used by static linker to implement lazy binding
417 extern void dyld_stub_binder() __asm__("dyld_stub_binder");
420 // called by exit() before it calls cxa_finalize() so that thread_local
421 // objects are destroyed before global objects.
422 extern void _tlv_exit();
425 // temp exports to keep tapi happy, until ASan stops using dyldVersionNumber
426 extern double dyldVersionNumber
;
427 extern const char* dyldVersionString
;
431 #endif /* __cplusplus */
433 #endif /* _MACH_O_DYLD_PRIV_H_ */