1 /*
2  * Copyright (c) 1999-2008 Apple Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
13  * The Original Code and all software distributed under the License are
14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18  * Please see the License for the specific language governing rights and
19  * limitations under the License.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23 #ifndef _MACH_O_DYLD_H_
24 #define _MACH_O_DYLD_H_
25 
26 
27 #include <stddef.h>
28 #include <stdint.h>
29 #include <stdbool.h>
30 
31 #include <mach-o/loader.h>
32 #include <Availability.h>
33 
34 #if __cplusplus
35 extern "C" {
36 #endif
37 
38 #ifdef __DRIVERKIT_19_0
39  #define DYLD_DRIVERKIT_UNAVAILABLE __API_UNAVAILABLE(driverkit)
40 #else
41  #define DYLD_DRIVERKIT_UNAVAILABLE
42 #endif
43 
44 /*
45  * The following functions allow you to iterate through all loaded images.
46  * This is not a thread safe operation.  Another thread can add or remove
47  * an image during the iteration.
48  *
49  * Many uses of these routines can be replace by a call to dladdr() which
50  * will return the mach_header and name of an image, given an address in
51  * the image. dladdr() is thread safe.
52  */
53 extern uint32_t                    _dyld_image_count(void)                              __OSX_AVAILABLE_STARTING(__MAC_10_1, __IPHONE_2_0);
54 extern const struct mach_header*   _dyld_get_image_header(uint32_t image_index)         __OSX_AVAILABLE_STARTING(__MAC_10_1, __IPHONE_2_0);
55 extern intptr_t                    _dyld_get_image_vmaddr_slide(uint32_t image_index)   __OSX_AVAILABLE_STARTING(__MAC_10_1, __IPHONE_2_0);
56 extern const char*                 _dyld_get_image_name(uint32_t image_index)           __OSX_AVAILABLE_STARTING(__MAC_10_1, __IPHONE_2_0);
57 
58 
59 /*
60  * The following functions allow you to install callbacks which will be called
61  * by dyld whenever an image is loaded or unloaded.  During a call to _dyld_register_func_for_add_image()
62  * the callback func is called for every existing image.  Later, it is called as each new image
63  * is loaded and bound (but initializers not yet run).  The callback registered with
64  * _dyld_register_func_for_remove_image() is called after any terminators in an image are run
65  * and before the image is un-memory-mapped.
66  */
67 extern void _dyld_register_func_for_add_image(void (*func)(const struct mach_header* mh, intptr_t vmaddr_slide))    __OSX_AVAILABLE_STARTING(__MAC_10_1, __IPHONE_2_0);
68 extern void _dyld_register_func_for_remove_image(void (*func)(const struct mach_header* mh, intptr_t vmaddr_slide)) __OSX_AVAILABLE_STARTING(__MAC_10_1, __IPHONE_2_0);
69 
70 
71 /*
72  * NSVersionOfRunTimeLibrary() returns the current_version number of the currently dylib
73  * specifed by the libraryName.  The libraryName parameter would be "bar" for /path/libbar.3.dylib and
74  * "Foo" for /path/Foo.framework/Versions/A/Foo.  It returns -1 if no such library is loaded.
75  */
76 extern int32_t NSVersionOfRunTimeLibrary(const char* libraryName)            __OSX_AVAILABLE_STARTING(__MAC_10_1, __IPHONE_2_0);
77 
78 
79 /*
80  * NSVersionOfLinkTimeLibrary() returns the current_version number that the main executable was linked
81  * against at build time.  The libraryName parameter would be "bar" for /path/libbar.3.dylib and
82  * "Foo" for /path/Foo.framework/Versions/A/Foo.  It returns -1 if the main executable did not link
83  * against the specified library.
84  */
85 extern int32_t NSVersionOfLinkTimeLibrary(const char* libraryName)           __OSX_AVAILABLE_STARTING(__MAC_10_1, __IPHONE_2_0);
86 
87 
88 /*
89  * _NSGetExecutablePath() copies the path of the main executable into the buffer. The bufsize parameter
90  * should initially be the size of the buffer.  The function returns 0 if the path was successfully copied,
91  * and *bufsize is left unchanged. It returns -1 if the buffer is not large enough, and *bufsize is set
92  * to the size required.
93  *
94  * Note that _NSGetExecutablePath will return "a path" to the executable not a "real path" to the executable.
95  * That is the path may be a symbolic link and not the real file. With deep directories the total bufsize
96  * needed could be more than MAXPATHLEN.
97  */
98 extern int _NSGetExecutablePath(char* buf, uint32_t* bufsize)                 __OSX_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
99 
100 
101 
102 /*
103  * Registers a function to be called when the current thread terminates.
104  * Called by c++ compiler to implement destructors on thread_local object variables.
105  */
106 extern void _tlv_atexit(void (*termFunc)(void* objAddr), void* objAddr)      __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
107 
108 
109 /*
110  * Never called. On-disk thread local variables contain a pointer to this.  Once
111  * the thread local is prepared, the pointer changes to a real handler such as tlv_get_addr.
112  */
113 extern void _tlv_bootstrap(void)                                             __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0) DYLD_DRIVERKIT_UNAVAILABLE ;
114 
115 
116 /*
117  * Dylibs that are incorporated into the dyld cache are removed from disk. That means code
118  * cannot stat() the file to see if it "exists".  This function is like a stat() call that checks if a
119  * path is to a dylib that was removed from disk and is incorporated into the active dyld cache.
120  */
121 extern bool _dyld_shared_cache_contains_path(const char* path)               __API_AVAILABLE(macos(11.0), ios(14.0), watchos(7.0), tvos(14.0)) DYLD_DRIVERKIT_UNAVAILABLE;
122 
123 
124 /*
125  * The following dyld API's are deprecated as of Mac OS X 10.5.  They are either
126  * no longer necessary or are superceeded by dlopen and friends in <dlfcn.h>.
127  * dlopen/dlsym/dlclose have been available since Mac OS X 10.3 and work with
128  * dylibs and bundles.
129  *
130  *    NSAddImage                           -> dlopen
131  *    NSLookupSymbolInImage                -> dlsym
132  *    NSCreateObjectFileImageFromFile      -> dlopen
133  *    NSDestroyObjectFileImage             -> dlclose
134  *    NSLinkModule                         -> not needed when dlopen used
135  *    NSUnLinkModule                       -> not needed when dlclose used
136  *    NSLookupSymbolInModule               -> dlsym
137  *    _dyld_image_containing_address       -> dladdr
138  *    NSLinkEditError                      -> dlerror
139  *
140  */
141 
142 #ifndef ENUM_DYLD_BOOL
143 #define ENUM_DYLD_BOOL
144   #undef FALSE
145   #undef TRUE
146   enum DYLD_BOOL { FALSE, TRUE };
147 #endif /* ENUM_DYLD_BOOL */
148 
149 
150 /* Object file image API */
151 typedef enum {
152     NSObjectFileImageFailure, /* for this a message is printed on stderr */
153     NSObjectFileImageSuccess,
154     NSObjectFileImageInappropriateFile,
155     NSObjectFileImageArch,
156     NSObjectFileImageFormat, /* for this a message is printed on stderr */
157     NSObjectFileImageAccess
158 } NSObjectFileImageReturnCode;
159 
160 typedef struct __NSObjectFileImage* NSObjectFileImage;
161 
162 
163 
164 /* NSObjectFileImage can only be used with MH_BUNDLE files */
165 extern NSObjectFileImageReturnCode NSCreateObjectFileImageFromFile(const char* pathName, NSObjectFileImage *objectFileImage)               __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dlopen()");
166 extern NSObjectFileImageReturnCode NSCreateObjectFileImageFromMemory(const void *address, size_t size, NSObjectFileImage *objectFileImage) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "");
167 extern bool                        NSDestroyObjectFileImage(NSObjectFileImage objectFileImage)                                             __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dlclose()");
168 
169 extern uint32_t     NSSymbolDefinitionCountInObjectFileImage(NSObjectFileImage objectFileImage)                   __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "");
170 extern const char*  NSSymbolDefinitionNameInObjectFileImage(NSObjectFileImage objectFileImage, uint32_t ordinal)  __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "");
171 extern uint32_t     NSSymbolReferenceCountInObjectFileImage(NSObjectFileImage objectFileImage)                    __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "");
172 extern const char*  NSSymbolReferenceNameInObjectFileImage(NSObjectFileImage objectFileImage, uint32_t ordinal, bool *tentative_definition) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "");
173 extern bool         NSIsSymbolDefinedInObjectFileImage(NSObjectFileImage objectFileImage, const char* symbolName) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.4, "dlsym()");
174 extern void*        NSGetSectionDataInObjectFileImage(NSObjectFileImage objectFileImage, const char* segmentName, const char* sectionName, size_t *size) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "getsectiondata()");
175 
176 typedef struct __NSModule* NSModule;
177 extern const char*  NSNameOfModule(NSModule m)         __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "");
178 extern const char*  NSLibraryNameForModule(NSModule m) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "");
179 
180 extern NSModule NSLinkModule(NSObjectFileImage objectFileImage, const char* moduleName, uint32_t options) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dlopen()");
181 #define NSLINKMODULE_OPTION_NONE                         0x0
182 #define NSLINKMODULE_OPTION_BINDNOW                      0x1
183 #define NSLINKMODULE_OPTION_PRIVATE                      0x2
184 #define NSLINKMODULE_OPTION_RETURN_ON_ERROR              0x4
185 #define NSLINKMODULE_OPTION_DONT_CALL_MOD_INIT_ROUTINES  0x8
186 #define NSLINKMODULE_OPTION_TRAILING_PHYS_NAME          0x10
187 
188 extern bool NSUnLinkModule(NSModule module, uint32_t options) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "");
189 #define NSUNLINKMODULE_OPTION_NONE                  0x0
190 #define NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED    0x1
191 #define NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES	0x2
192 
193 /* symbol API */
194 typedef struct __NSSymbol* NSSymbol;
195 extern bool     NSIsSymbolNameDefined(const char* symbolName)                                                    __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.4, "dlsym()");
196 extern bool     NSIsSymbolNameDefinedWithHint(const char* symbolName, const char* libraryNameHint)               __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.4, "dlsym()");
197 extern bool     NSIsSymbolNameDefinedInImage(const struct mach_header* image, const char* symbolName)            __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.4, "dlsym()");
198 extern NSSymbol NSLookupAndBindSymbol(const char* symbolName)                                                    __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.4, "dlsym()");
199 extern NSSymbol NSLookupAndBindSymbolWithHint(const char* symbolName, const char* libraryNameHint)               __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.4, "dlsym()");
200 extern NSSymbol NSLookupSymbolInModule(NSModule module, const char* symbolName)                                  __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dlsym()");
201 extern NSSymbol NSLookupSymbolInImage(const struct mach_header* image, const char* symbolName, uint32_t options) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dlsym()");
202 #define NSLOOKUPSYMBOLINIMAGE_OPTION_BIND            0x0
203 #define NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW        0x1
204 #define NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_FULLY      0x2
205 #define NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR 0x4
206 extern const char*  NSNameOfSymbol(NSSymbol symbol)    __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "");
207 extern void *       NSAddressOfSymbol(NSSymbol symbol) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dlsym()");
208 extern NSModule     NSModuleForSymbol(NSSymbol symbol) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dladdr()");
209 
210 /* error handling API */
211 typedef enum {
212     NSLinkEditFileAccessError,
213     NSLinkEditFileFormatError,
214     NSLinkEditMachResourceError,
215     NSLinkEditUnixResourceError,
216     NSLinkEditOtherError,
217     NSLinkEditWarningError,
218     NSLinkEditMultiplyDefinedError,
219     NSLinkEditUndefinedError
220 } NSLinkEditErrors;
221 
222 /*
223  * For the NSLinkEditErrors value NSLinkEditOtherError these are the values
224  * passed to the link edit error handler as the errorNumber (what would be an
225  * errno value for NSLinkEditUnixResourceError or a kern_return_t value for
226  * NSLinkEditMachResourceError).
227  */
228 typedef enum {
229     NSOtherErrorRelocation,
230     NSOtherErrorLazyBind,
231     NSOtherErrorIndrLoop,
232     NSOtherErrorLazyInit,
233     NSOtherErrorInvalidArgs
234 } NSOtherErrorNumbers;
235 
236 extern void NSLinkEditError(NSLinkEditErrors *c, int *errorNumber, const char** fileName, const char** errorString) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dlerror()");
237 
238 typedef struct {
239      void     (*undefined)(const char* symbolName);
240      NSModule (*multiple)(NSSymbol s, NSModule oldModule, NSModule newModule);
241      void     (*linkEdit)(NSLinkEditErrors errorClass, int errorNumber,
242                           const char* fileName, const char* errorString);
243 } NSLinkEditErrorHandlers;
244 
245 extern void NSInstallLinkEditErrorHandlers(const NSLinkEditErrorHandlers *handlers) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "");
246 
247 extern bool                      NSAddLibrary(const char* pathName)                   __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.4, "dlopen()");
248 extern bool                      NSAddLibraryWithSearching(const char* pathName)      __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.4, "dlopen()");
249 extern const struct mach_header* NSAddImage(const char* image_name, uint32_t options) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dlopen()");
250 #define NSADDIMAGE_OPTION_NONE                  	0x0
251 #define NSADDIMAGE_OPTION_RETURN_ON_ERROR       	0x1
252 #define NSADDIMAGE_OPTION_WITH_SEARCHING        	0x2
253 #define NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED 	0x4
254 #define NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME	0x8
255 
256 extern bool _dyld_present(void)                                                              __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "always true");
257 extern bool _dyld_launched_prebound(void)                                                    __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "moot");
258 extern bool _dyld_all_twolevel_modules_prebound(void)                                        __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.3, 10.5, "moot");
259 extern bool _dyld_bind_fully_image_containing_address(const void* address)                   __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dlopen(RTLD_NOW)");
260 extern bool _dyld_image_containing_address(const void* address)                              __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.3, 10.5, "dladdr()");
261 extern void _dyld_lookup_and_bind(const char* symbol_name, void **address, NSModule* module) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.4, "dlsym()");
262 extern void _dyld_lookup_and_bind_with_hint(const char* symbol_name, const char* library_name_hint, void** address, NSModule* module) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.4, "dlsym()");
263 extern void _dyld_lookup_and_bind_fully(const char* symbol_name, void** address, NSModule* module) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.1, 10.5, "dlsym()");
264 
265 extern const struct mach_header*  _dyld_get_image_header_containing_address(const void* address) __API_UNAVAILABLE(ios, tvos, watchos)  DYLD_DRIVERKIT_UNAVAILABLE  __OSX_DEPRECATED(10.3, 10.5, "dladdr()");
266 
267 
268 #if __cplusplus
269 }
270 #endif
271 
272 #endif /* _MACH_O_DYLD_H_ */