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
33extern "C" {
34#endif /* __cplusplus */
35
36
37
38//
39// private interface between libSystem.dylib and dyld
40//
41extern int _dyld_func_lookup(const char* dyld_func_name, void **address);
42
43//
44// private interface between libSystem.dylib and dyld
45//
46extern void _dyld_fork_child();
47
48
49//
50// Possible state changes for which you can register to be notified
51//
52enum dyld_image_states
53{
54	dyld_image_state_mapped					= 10,		// No batch notification for this
55	dyld_image_state_dependents_mapped		= 20,		// Only batch notification for this
56	dyld_image_state_rebased				= 30,
57	dyld_image_state_bound					= 40,
58	dyld_image_state_dependents_initialized	= 45,		// Only single notification for this
59	dyld_image_state_initialized			= 50,
60	dyld_image_state_terminated				= 60		// Only single notification for this
61};
62
63//
64// Callback that provides a bottom-up array of images
65// For dyld_image_state_[dependents_]mapped state only, returning non-NULL will cause dyld to abort loading all those images
66// and append the returned string to its load failure error message. dyld does not free the string, so
67// it should be a literal string or a static buffer
68//
69typedef const char* (*dyld_image_state_change_handler)(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[]);
70
71//
72// Register a handler to be called when any image changes to the requested state.
73// If 'batch' is true, the callback is called with an array of all images that are in the requested state sorted by dependency.
74// If 'batch' is false, the callback is called with one image at a time as each image transitions to the the requested state.
75// During the call to this function, the handler may be called back with existing images and the handler should
76// not return a string, since there is no load to abort.  In batch mode, existing images at or past the request
77// state supplied in the callback.  In non-batch mode, the callback is called for each image exactly in the
78// requested state.
79//
80extern void
81dyld_register_image_state_change_handler(enum dyld_image_states state, bool batch, dyld_image_state_change_handler handler);
82
83
84//
85// Possible thread-local variable state changes for which you can register to be notified
86//
87enum dyld_tlv_states {
88    dyld_tlv_state_allocated = 10,   // TLV range newly allocated
89    dyld_tlv_state_deallocated = 20  // TLV range about to be deallocated
90};
91
92//
93// Info about thread-local variable storage.
94//
95typedef struct {
96    size_t info_size;    // sizeof(dyld_tlv_info)
97    void * tlv_addr;     // Base address of TLV storage
98    size_t tlv_size;     // Byte size of TLV storage
99} dyld_tlv_info;
100
101#if __BLOCKS__
102
103//
104// Callback that notes changes to thread-local variable storage.
105//
106typedef void (^dyld_tlv_state_change_handler)(enum dyld_tlv_states state, const dyld_tlv_info *info);
107
108//
109// Register a handler to be called when a thread adds or removes storage for thread-local variables.
110// The registered handler will only be called from and on behalf of the thread that owns the storage.
111// The registered handler will NOT be called for any storage that was
112//   already allocated before dyld_register_tlv_state_change_handler() was
113//   called. Use dyld_enumerate_tlv_storage() to get that information.
114// Exists in Mac OS X 10.7 and later
115//
116extern void
117dyld_register_tlv_state_change_handler(enum dyld_tlv_states state, dyld_tlv_state_change_handler handler);
118
119//
120// Enumerate the current thread-local variable storage allocated for the current thread.
121// Exists in Mac OS X 10.7 and later
122//
123extern void
124dyld_enumerate_tlv_storage(dyld_tlv_state_change_handler handler);
125
126#endif
127
128
129//
130// get slide for a given loaded mach_header
131// Mac OS X 10.6 and later
132//
133extern intptr_t _dyld_get_image_slide(const struct mach_header* mh);
134
135
136//
137// get pointer to this process's dyld_all_image_infos
138// Exists in Mac OS X 10.4 and later through _dyld_func_lookup()
139// Exists in Mac OS X 10.6 and later through libSystem.dylib
140//
141const struct dyld_all_image_infos* _dyld_get_all_image_infos();
142
143
144
145struct dyld_unwind_sections
146{
147	const struct mach_header*		mh;
148	const void*						dwarf_section;
149	uintptr_t						dwarf_section_length;
150	const void*						compact_unwind_section;
151	uintptr_t						compact_unwind_section_length;
152};
153
154
155//
156// Returns true iff some loaded mach-o image contains "addr".
157//	info->mh							mach header of image containing addr
158//  info->dwarf_section					pointer to start of __TEXT/__eh_frame section
159//  info->dwarf_section_length			length of __TEXT/__eh_frame section
160//  info->compact_unwind_section		pointer to start of __TEXT/__unwind_info section
161//  info->compact_unwind_section_length	length of __TEXT/__unwind_info section
162//
163// Exists in Mac OS X 10.6 and later
164extern bool _dyld_find_unwind_sections(void* addr, struct dyld_unwind_sections* info);
165
166
167//
168// This is an optimized form of dladdr() that only returns the dli_fname field.
169//
170// Exists in Mac OS X 10.6 and later
171extern const char* dyld_image_path_containing_address(const void* addr);
172
173
174
175// Convienence constants for return values from dyld_get_sdk_version() and friends.
176#define DYLD_MACOSX_VERSION_10_4		0x000A0400
177#define DYLD_MACOSX_VERSION_10_5		0x000A0500
178#define DYLD_MACOSX_VERSION_10_6		0x000A0600
179#define DYLD_MACOSX_VERSION_10_7		0x000A0700
180#define DYLD_MACOSX_VERSION_10_8		0x000A0800
181#define DYLD_MACOSX_VERSION_10_9		0x000A0900
182#define DYLD_MACOSX_VERSION_10_10		0x000A0A00
183
184#define DYLD_IOS_VERSION_2_0		0x00020000
185#define DYLD_IOS_VERSION_2_1		0x00020100
186#define DYLD_IOS_VERSION_2_2		0x00020200
187#define DYLD_IOS_VERSION_3_0		0x00030000
188#define DYLD_IOS_VERSION_3_1		0x00030100
189#define DYLD_IOS_VERSION_3_2		0x00030200
190#define DYLD_IOS_VERSION_4_0		0x00040000
191#define DYLD_IOS_VERSION_4_1		0x00040100
192#define DYLD_IOS_VERSION_4_2		0x00040200
193#define DYLD_IOS_VERSION_4_3		0x00040300
194#define DYLD_IOS_VERSION_5_0		0x00050000
195#define DYLD_IOS_VERSION_5_1		0x00050100
196#define DYLD_IOS_VERSION_6_0		0x00060000
197#define DYLD_IOS_VERSION_6_1		0x00060100
198#define DYLD_IOS_VERSION_7_0		0x00070000
199#define DYLD_IOS_VERSION_7_1		0x00070100
200#define DYLD_IOS_VERSION_8_0		0x00080000
201
202//
203// This is finds the SDK version a binary was built against.
204// Returns zero on error, or if SDK version could not be determined.
205//
206// Exists in Mac OS X 10.8 and later
207// Exists in iOS 6.0 and later
208extern uint32_t dyld_get_sdk_version(const struct mach_header* mh);
209
210
211//
212// This is finds the SDK version the main executable was built against.
213// Returns zero on error, or if SDK version could not be determined.
214//
215// Exists in Mac OS X 10.8 and later
216// Exists in iOS 6.0 and later
217extern uint32_t dyld_get_program_sdk_version();
218
219
220//
221// This is finds the min OS version a binary was built to run on.
222// Returns zero on error, or if no min OS recorded in binary.
223//
224// Exists in Mac OS X 10.8 and later
225// Exists in iOS 6.0 and later
226extern uint32_t dyld_get_min_os_version(const struct mach_header* mh);
227
228
229//
230// This is finds the min OS version the main executable was built to run on.
231// Returns zero on error, or if no min OS recorded in binary.
232//
233// Exists in Mac OS X 10.8 and later
234// Exists in iOS 6.0 and later
235extern uint32_t dyld_get_program_min_os_version();
236
237
238
239
240//
241// Returns if any OS dylib has overridden its copy in the shared cache
242//
243// Exists in iPhoneOS 3.1 and later
244// Exists in Mac OS X 10.10 and later
245extern bool dyld_shared_cache_some_image_overridden();
246
247
248
249//
250// Returns if the process is setuid or is code signed with entitlements.
251//
252// Exists in Mac OS X 10.9 and later
253extern bool dyld_process_is_restricted();
254
255
256//
257// <rdar://problem/13820686> for OpenGL to tell dyld it is ok to deallocate a memory based image when done.
258//
259// Exists in Mac OS X 10.9 and later
260#define NSLINKMODULE_OPTION_CAN_UNLOAD                  0x20
261
262
263//
264// Update all bindings on specified image.
265// Looks for uses of 'replacement' and changes it to 'replacee'.
266// NOTE: this is less safe than using static interposing via DYLD_INSERT_LIBRARIES
267// because the running program may have already copy the pointer values to other
268// locations that dyld does not know about.
269//
270struct dyld_interpose_tuple {
271	const void* replacement;
272	const void* replacee;
273};
274extern void dyld_dynamic_interpose(const struct mach_header* mh, const struct dyld_interpose_tuple array[], size_t count);
275
276
277
278#if __cplusplus
279}
280#endif /* __cplusplus */
281
282#endif /* _MACH_O_DYLD_PRIV_H_ */
283