dyld-433.5.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 <stdbool.h>
28 #include <Availability.h>
29 #include <mach-o/dyld.h>
30 #include <mach-o/dyld_images.h>
31
32 #if __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35
36
37
38 //
39 // private interface between libSystem.dylib and dyld
40 //
41 extern int _dyld_func_lookup(const char* dyld_func_name, void **address);
42
43 //
44 // private interface between libSystem.dylib and dyld
45 //
46 extern void _dyld_fork_child();
47
48
49 // DEPRECATED
50 enum dyld_image_states
51 {
52 dyld_image_state_mapped = 10, // No batch notification for this
53 dyld_image_state_dependents_mapped = 20, // Only batch notification for this
54 dyld_image_state_rebased = 30,
55 dyld_image_state_bound = 40,
56 dyld_image_state_dependents_initialized = 45, // Only single notification for this
57 dyld_image_state_initialized = 50,
58 dyld_image_state_terminated = 60 // Only single notification for this
59 };
60
61 // DEPRECATED
62 typedef const char* (*dyld_image_state_change_handler)(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[]);
63
64
65
66 typedef void (*_dyld_objc_notify_mapped)(unsigned count, const char* const paths[], const struct mach_header* const mh[]);
67 typedef void (*_dyld_objc_notify_init)(const char* path, const struct mach_header* mh);
68 typedef void (*_dyld_objc_notify_unmapped)(const char* path, const struct mach_header* mh);
69
70
71 //
72 // Note: only for use by objc runtime
73 // Register handlers to be called when objc images are mapped, unmapped, and initialized.
74 // Dyld will call back the "mapped" function with an array of images that contain an objc-image-info section.
75 // Those images that are dylibs will have the ref-counts automatically bumped, so objc will no longer need to
76 // call dlopen() on them to keep them from being unloaded. During the call to _dyld_objc_notify_register(),
77 // dyld will call the "mapped" function with already loaded objc images. During any later dlopen() call,
78 // dyld will also call the "mapped" function. Dyld will call the "init" function when dyld would be called
79 // initializers in that image. This is when objc calls any +load methods in that image.
80 //
81 void _dyld_objc_notify_register(_dyld_objc_notify_mapped mapped,
82 _dyld_objc_notify_init init,
83 _dyld_objc_notify_unmapped unmapped);
84
85
86
87 //
88 // Possible thread-local variable state changes for which you can register to be notified
89 //
90 enum dyld_tlv_states {
91 dyld_tlv_state_allocated = 10, // TLV range newly allocated
92 dyld_tlv_state_deallocated = 20 // TLV range about to be deallocated
93 };
94
95 //
96 // Info about thread-local variable storage.
97 //
98 typedef struct {
99 size_t info_size; // sizeof(dyld_tlv_info)
100 void * tlv_addr; // Base address of TLV storage
101 size_t tlv_size; // Byte size of TLV storage
102 } dyld_tlv_info;
103
104 #if __BLOCKS__
105
106 //
107 // Callback that notes changes to thread-local variable storage.
108 //
109 typedef void (^dyld_tlv_state_change_handler)(enum dyld_tlv_states state, const dyld_tlv_info *info);
110
111 //
112 // Register a handler to be called when a thread adds or removes storage for thread-local variables.
113 // The registered handler will only be called from and on behalf of the thread that owns the storage.
114 // The registered handler will NOT be called for any storage that was
115 // already allocated before dyld_register_tlv_state_change_handler() was
116 // called. Use dyld_enumerate_tlv_storage() to get that information.
117 // Exists in Mac OS X 10.7 and later
118 //
119 extern void
120 dyld_register_tlv_state_change_handler(enum dyld_tlv_states state, dyld_tlv_state_change_handler handler);
121
122 //
123 // Enumerate the current thread-local variable storage allocated for the current thread.
124 // Exists in Mac OS X 10.7 and later
125 //
126 extern void
127 dyld_enumerate_tlv_storage(dyld_tlv_state_change_handler handler);
128
129 #endif
130
131
132 //
133 // get slide for a given loaded mach_header
134 // Mac OS X 10.6 and later
135 //
136 extern intptr_t _dyld_get_image_slide(const struct mach_header* mh);
137
138
139 //
140 // get pointer to this process's dyld_all_image_infos
141 // Exists in Mac OS X 10.4 and later through _dyld_func_lookup()
142 // Exists in Mac OS X 10.6 and later through libSystem.dylib
143 //
144 const struct dyld_all_image_infos* _dyld_get_all_image_infos() __attribute__((deprecated));
145
146
147
148 struct dyld_unwind_sections
149 {
150 const struct mach_header* mh;
151 const void* dwarf_section;
152 uintptr_t dwarf_section_length;
153 const void* compact_unwind_section;
154 uintptr_t compact_unwind_section_length;
155 };
156
157
158 //
159 // Returns true iff some loaded mach-o image contains "addr".
160 // info->mh mach header of image containing addr
161 // info->dwarf_section pointer to start of __TEXT/__eh_frame section
162 // info->dwarf_section_length length of __TEXT/__eh_frame section
163 // info->compact_unwind_section pointer to start of __TEXT/__unwind_info section
164 // info->compact_unwind_section_length length of __TEXT/__unwind_info section
165 //
166 // Exists in Mac OS X 10.6 and later
167 extern bool _dyld_find_unwind_sections(void* addr, struct dyld_unwind_sections* info);
168
169
170 //
171 // This is an optimized form of dladdr() that only returns the dli_fname field.
172 //
173 // Exists in Mac OS X 10.6 and later
174 extern const char* dyld_image_path_containing_address(const void* addr);
175
176
177 //
178 // This is an optimized form of dladdr() that only returns the dli_fbase field.
179 // Return NULL, if address is not in any image tracked by dyld.
180 //
181 // Exists in Mac OS X 10.11 and later
182 extern const struct mach_header* dyld_image_header_containing_address(const void* addr);
183
184
185
186 // Convienence constants for return values from dyld_get_sdk_version() and friends.
187
188 //@MAC_VERSION_DEFS@
189
190 //@IOS_VERSION_DEFS@
191
192 //@WATCHOS_VERSION_DEFS@
193
194
195 //
196 // This finds the SDK version a binary was built against.
197 // Returns zero on error, or if SDK version could not be determined.
198 //
199 // Exists in Mac OS X 10.8 and later
200 // Exists in iOS 6.0 and later
201 extern uint32_t dyld_get_sdk_version(const struct mach_header* mh);
202
203
204 //
205 // This finds the SDK version that the main executable was built against.
206 // Returns zero on error, or if SDK version could not be determined.
207 //
208 // Note on WatchOS, this returns the equivalent iOS SDK version number
209 // (i.e an app built against WatchOS 2.0 SDK returne 9.0). To see the
210 // platform specific sdk version use dyld_get_program_sdk_watch_os_version().
211 //
212 // Exists in Mac OS X 10.8 and later
213 // Exists in iOS 6.0 and later
214 extern uint32_t dyld_get_program_sdk_version();
215
216
217 // Watch OS only.
218 // This finds the Watch OS SDK version that the main executable was built against.
219 // Exists in Watch OS 2.0 and later
220 extern uint32_t dyld_get_program_sdk_watch_os_version(); // __WATCHOS_AVAILABLE(2.0);
221
222
223 // Watch OS only.
224 // This finds the Watch min OS version that the main executable was built to run on.
225 // Note: dyld_get_program_min_os_version() returns the iOS equivalent (e.g. 9.0)
226 // whereas this returns the raw watchOS version (e.g. 2.0).
227 // Exists in Watch OS 3.0 and later
228 extern uint32_t dyld_get_program_min_watch_os_version(); // __WATCHOS_AVAILABLE(3.0);
229
230
231 //
232 // This finds the min OS version a binary was built to run on.
233 // Returns zero on error, or if no min OS recorded in binary.
234 //
235 // Exists in Mac OS X 10.8 and later
236 // Exists in iOS 6.0 and later
237 extern uint32_t dyld_get_min_os_version(const struct mach_header* mh);
238
239
240 //
241 // This finds the min OS version the main executable was built to run on.
242 // Returns zero on error, or if no min OS recorded in binary.
243 //
244 // Exists in Mac OS X 10.8 and later
245 // Exists in iOS 6.0 and later
246 extern uint32_t dyld_get_program_min_os_version();
247
248
249
250
251 //
252 // Returns if any OS dylib has overridden its copy in the shared cache
253 //
254 // Exists in iPhoneOS 3.1 and later
255 // Exists in Mac OS X 10.10 and later
256 extern bool dyld_shared_cache_some_image_overridden();
257
258
259
260 //
261 // Returns if the process is setuid or is code signed with entitlements.
262 //
263 // Exists in Mac OS X 10.9 and later
264 extern bool dyld_process_is_restricted();
265
266
267
268 //
269 // Returns path used by dyld for standard dyld shared cache file for the current arch.
270 //
271 // Exists in Mac OS X 10.11 and later
272 extern const char* dyld_shared_cache_file_path();
273
274
275
276 //
277 // <rdar://problem/13820686> for OpenGL to tell dyld it is ok to deallocate a memory based image when done.
278 //
279 // Exists in Mac OS X 10.9 and later
280 #define NSLINKMODULE_OPTION_CAN_UNLOAD 0x20
281
282
283 //
284 // Update all bindings on specified image.
285 // Looks for uses of 'replacement' and changes it to 'replacee'.
286 // NOTE: this is less safe than using static interposing via DYLD_INSERT_LIBRARIES
287 // because the running program may have already copy the pointer values to other
288 // locations that dyld does not know about.
289 //
290 struct dyld_interpose_tuple {
291 const void* replacement;
292 const void* replacee;
293 };
294 extern void dyld_dynamic_interpose(const struct mach_header* mh, const struct dyld_interpose_tuple array[], size_t count);
295
296
297 struct dyld_shared_cache_dylib_text_info {
298 uint64_t version; // current version 1
299 // following fields all exist in version 1
300 uint64_t loadAddressUnslid;
301 uint64_t textSegmentSize;
302 uuid_t dylibUuid;
303 const char* path; // pointer invalid at end of iterations
304 };
305 typedef struct dyld_shared_cache_dylib_text_info dyld_shared_cache_dylib_text_info;
306
307 //
308 // Given the UUID of a dyld shared cache file, this function will attempt to locate the cache
309 // file and if found iterate all images, returning info about each one. Returns 0 on success.
310 //
311 // Exists in Mac OS X 10.11 and later
312 // iOS 9.0 and later
313 extern int dyld_shared_cache_iterate_text(const uuid_t cacheUuid, void (^callback)(const dyld_shared_cache_dylib_text_info* info));
314
315
316 //
317 // Given the UUID of a dyld shared cache file, and a NULL terminated array of extra directory paths to search,
318 // this function will scan the standard and extra directories looking for a cache file that matches the UUID
319 // and if found iterate all images, returning info about each one. Returns 0 on success.
320 //
321 // Exists in Mac OS X 10.12 and later
322 // iOS 10.0 and later
323 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));
324
325
326 //
327 // Returns if the specified address range is in a dyld owned memory
328 // that is mapped read-only and will never be unloaded.
329 //
330 // Exists in Mac OS X 10.12 and later
331 // iOS 10.0 and later
332 extern bool _dyld_is_memory_immutable(const void* addr, size_t length);
333
334
335 //
336 // Finds the UUID (from LC_UUID load command) of given image.
337 // Returns false if LC_UUID is missing or mach_header is malformed.
338 //
339 // Exists in Mac OS X 10.12 and later
340 // Exists in iOS 10.0 and later
341 extern bool _dyld_get_image_uuid(const struct mach_header* mh, uuid_t uuid);
342
343
344 //
345 // Gets the UUID of the dyld shared cache in the current process.
346 // Returns false if there is no dyld shared cache in use by the processes.
347 //
348 // Exists in Mac OS X 10.12 and later
349 // Exists in iOS 10.0 and later
350 extern bool _dyld_get_shared_cache_uuid(uuid_t uuid);
351
352
353 //
354 // Returns the start address of the dyld cache in the process and sets length to the size of the cache.
355 // Returns NULL if the process is not using a dyld shared cache
356 //
357 // Exists in Mac OS X 10.13 and later
358 // Exists in iOS 11.0 and later
359 extern const void* _dyld_get_shared_cache_range(size_t* length);
360
361
362
363 //
364 // When dyld must terminate a process because of a required dependent dylib
365 // could not be loaded or a symbol is missing, dyld calls abort_with_reason()
366 // using one of the following error codes.
367 //
368 #define DYLD_EXIT_REASON_DYLIB_MISSING 1
369 #define DYLD_EXIT_REASON_DYLIB_WRONG_ARCH 2
370 #define DYLD_EXIT_REASON_DYLIB_WRONG_VERSION 3
371 #define DYLD_EXIT_REASON_SYMBOL_MISSING 4
372 #define DYLD_EXIT_REASON_CODE_SIGNATURE 5
373 #define DYLD_EXIT_REASON_FILE_SYSTEM_SANDBOX 6
374 #define DYLD_EXIT_REASON_MALFORMED_MACHO 7
375 #define DYLD_EXIT_REASON_OTHER 9
376
377 //
378 // When it has more information about the termination, dyld will use abort_with_payload().
379 // The payload is a dyld_abort_payload structure. The fixed fields are offsets into the
380 // payload for the corresponding string. If the offset is zero, that string is not available.
381 //
382 struct dyld_abort_payload {
383 uint32_t version; // first version is 1
384 uint32_t flags; // 0x00000001 means dyld terminated at launch, backtrace not useful
385 uint32_t targetDylibPathOffset; // offset in payload of path string to dylib that could not be loaded
386 uint32_t clientPathOffset; // offset in payload of path string to image requesting dylib
387 uint32_t symbolOffset; // offset in payload of symbol string that could not be found
388 // string data
389 };
390 typedef struct dyld_abort_payload dyld_abort_payload;
391
392 #if __cplusplus
393 }
394 #endif /* __cplusplus */
395
396 #endif /* _MACH_O_DYLD_PRIV_H_ */