1/*
2 * Copyright (c) 1999 Apple Computer, 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#ifdef __cplusplus
27extern "C" {
28#endif /* __cplusplus */
29
30#if defined(__MWERKS__) && !defined(__private_extern__)
31#define __private_extern__ __declspec(private_extern)
32#endif
33
34#include <mach-o/loader.h>
35#include <AvailabilityMacros.h>
36#ifndef AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER
37#define AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER
38#endif
39
40#ifndef ENUM_DYLD_BOOL
41#define ENUM_DYLD_BOOL
42#undef FALSE
43#undef TRUE
44enum DYLD_BOOL {
45    FALSE,
46    TRUE
47};
48#endif /* ENUM_DYLD_BOOL */
49
50/*
51 * The high level NS... API.
52 */
53
54/* Object file image API */
55typedef enum {
56    NSObjectFileImageFailure, /* for this a message is printed on stderr */
57    NSObjectFileImageSuccess,
58    NSObjectFileImageInappropriateFile,
59    NSObjectFileImageArch,
60    NSObjectFileImageFormat, /* for this a message is printed on stderr */
61    NSObjectFileImageAccess
62} NSObjectFileImageReturnCode;
63
64typedef void * NSObjectFileImage;
65
66/* limited implementation, only MH_BUNDLE files can be used */
67extern NSObjectFileImageReturnCode NSCreateObjectFileImageFromFile(
68    const char *pathName,
69    NSObjectFileImage *objectFileImage);
70extern NSObjectFileImageReturnCode NSCreateCoreFileImageFromFile(
71    const char *pathName,
72    NSObjectFileImage *objectFileImage);
73/* not yet implemented */
74extern NSObjectFileImageReturnCode NSCreateObjectFileImageFromMemory(
75    void *address,
76    unsigned long size,
77    NSObjectFileImage *objectFileImage);
78extern enum DYLD_BOOL NSDestroyObjectFileImage(
79    NSObjectFileImage objectFileImage);
80/*
81 * API on NSObjectFileImage's for:
82 *   "for Each Symbol Definition In Object File Image" (for Dynamic Bundles)
83 *   and the same thing for references
84 */
85extern unsigned long NSSymbolDefinitionCountInObjectFileImage(
86    NSObjectFileImage objectFileImage);
87extern const char * NSSymbolDefinitionNameInObjectFileImage(
88    NSObjectFileImage objectFileImage,
89    unsigned long ordinal);
90extern unsigned long NSSymbolReferenceCountInObjectFileImage(
91    NSObjectFileImage objectFileImage);
92extern const char * NSSymbolReferenceNameInObjectFileImage(
93    NSObjectFileImage objectFileImage,
94    unsigned long ordinal,
95    enum DYLD_BOOL *tentative_definition); /* can be NULL */
96/*
97 * API on NSObjectFileImage:
98 *   "does Object File Image define symbol name X" (using sorted symbol table)
99 *   and a way to get the named objective-C section
100 */
101extern enum DYLD_BOOL NSIsSymbolDefinedInObjectFileImage(
102    NSObjectFileImage objectFileImage,
103    const char *symbolName);
104extern void * NSGetSectionDataInObjectFileImage(
105    NSObjectFileImage objectFileImage,
106    const char *segmentName,
107    const char *sectionName,
108    unsigned long *size); /* can be NULL */
109/* SPI first appeared in Mac OS X 10.3 */
110extern enum DYLD_BOOL NSHasModInitObjectFileImage(
111    NSObjectFileImage objectFileImage)
112    AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
113
114/* module API */
115typedef void * NSModule;
116extern const char * NSNameOfModule(
117    NSModule m);
118extern const char * NSLibraryNameForModule(
119    NSModule m);
120
121/* limited implementation, only MH_BUNDLE files can be linked */
122extern NSModule NSLinkModule(
123    NSObjectFileImage objectFileImage,
124    const char *moduleName,
125    unsigned long options);
126#define NSLINKMODULE_OPTION_NONE		0x0
127#define NSLINKMODULE_OPTION_BINDNOW		0x1
128#define NSLINKMODULE_OPTION_PRIVATE		0x2
129#define NSLINKMODULE_OPTION_RETURN_ON_ERROR	0x4
130#define NSLINKMODULE_OPTION_DONT_CALL_MOD_INIT_ROUTINES 0x8
131#define NSLINKMODULE_OPTION_TRAILING_PHYS_NAME	0x10
132
133/* limited implementation, only modules loaded with NSLinkModule() can be
134   unlinked */
135extern enum DYLD_BOOL NSUnLinkModule(
136    NSModule module,
137    unsigned long options);
138#define NSUNLINKMODULE_OPTION_NONE			0x0
139#define NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED	0x1
140#define NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES	0x2
141
142/* not yet implemented */
143extern NSModule NSReplaceModule(
144    NSModule moduleToReplace,
145    NSObjectFileImage newObjectFileImage,
146    unsigned long options);
147
148/* symbol API */
149typedef void * NSSymbol;
150extern enum DYLD_BOOL NSIsSymbolNameDefined(
151    const char *symbolName);
152extern enum DYLD_BOOL NSIsSymbolNameDefinedWithHint(
153    const char *symbolName,
154    const char *libraryNameHint);
155extern enum DYLD_BOOL NSIsSymbolNameDefinedInImage(
156    const struct mach_header *image,
157    const char *symbolName);
158extern NSSymbol NSLookupAndBindSymbol(
159    const char *symbolName);
160extern NSSymbol NSLookupAndBindSymbolWithHint(
161    const char *symbolName,
162    const char *libraryNameHint);
163extern NSSymbol NSLookupSymbolInModule(
164    NSModule module,
165    const char *symbolName);
166extern NSSymbol NSLookupSymbolInImage(
167    const struct mach_header *image,
168    const char *symbolName,
169    unsigned long options);
170#define NSLOOKUPSYMBOLINIMAGE_OPTION_BIND            0x0
171#define NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW        0x1
172#define NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_FULLY      0x2
173#define NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR 0x4
174extern const char * NSNameOfSymbol(
175    NSSymbol symbol);
176extern void * NSAddressOfSymbol(
177    NSSymbol symbol);
178extern NSModule NSModuleForSymbol(
179    NSSymbol symbol);
180
181/* error handling API */
182typedef enum {
183    NSLinkEditFileAccessError,
184    NSLinkEditFileFormatError,
185    NSLinkEditMachResourceError,
186    NSLinkEditUnixResourceError,
187    NSLinkEditOtherError,
188    NSLinkEditWarningError,
189    NSLinkEditMultiplyDefinedError,
190    NSLinkEditUndefinedError
191} NSLinkEditErrors;
192
193/*
194 * For the NSLinkEditErrors value NSLinkEditOtherError these are the values
195 * passed to the link edit error handler as the errorNumber (what would be an
196 * errno value for NSLinkEditUnixResourceError or a kern_return_t value for
197 * NSLinkEditMachResourceError).
198 */
199typedef enum {
200    NSOtherErrorRelocation,
201    NSOtherErrorLazyBind,
202    NSOtherErrorIndrLoop,
203    NSOtherErrorLazyInit,
204    NSOtherErrorInvalidArgs
205} NSOtherErrorNumbers;
206
207extern void NSLinkEditError(
208    NSLinkEditErrors *c,
209    int *errorNumber,
210    const char **fileName,
211    const char **errorString);
212
213typedef struct {
214     void     (*undefined)(const char *symbolName);
215     NSModule (*multiple)(NSSymbol s, NSModule oldModule, NSModule newModule);
216     void     (*linkEdit)(NSLinkEditErrors errorClass, int errorNumber,
217                          const char *fileName, const char *errorString);
218} NSLinkEditErrorHandlers;
219
220extern void NSInstallLinkEditErrorHandlers(
221    NSLinkEditErrorHandlers *handlers);
222
223/* other API */
224extern enum DYLD_BOOL NSAddLibrary(
225    const char *pathName);
226extern enum DYLD_BOOL NSAddLibraryWithSearching(
227    const char *pathName);
228extern const struct mach_header * NSAddImage(
229    const char *image_name,
230    unsigned long options);
231#define NSADDIMAGE_OPTION_NONE                  	0x0
232#define NSADDIMAGE_OPTION_RETURN_ON_ERROR       	0x1
233#define NSADDIMAGE_OPTION_WITH_SEARCHING        	0x2
234#define NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED 	0x4
235#define NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME	0x8
236extern long NSVersionOfRunTimeLibrary(
237    const char *libraryName);
238extern long NSVersionOfLinkTimeLibrary(
239    const char *libraryName);
240extern int _NSGetExecutablePath( /* SPI first appeared in Mac OS X 10.2 */
241    char *buf,
242    uint32_t *bufsize);
243
244/*
245 * The low level _dyld_... API.
246 * (used by the objective-C runtime primarily)
247 */
248extern unsigned long _dyld_present(
249    void);
250
251extern unsigned long _dyld_image_count(
252    void);
253#ifdef __LP64__
254extern struct mach_header_64 * _dyld_get_image_header(
255    uint32_t image_index);
256#else /* !defined(__LP64__) */
257extern struct mach_header * _dyld_get_image_header(
258    unsigned long image_index);
259#endif /* !defined(__LP64__) */
260extern unsigned long _dyld_get_image_vmaddr_slide(
261    unsigned long image_index);
262extern char * _dyld_get_image_name(
263    unsigned long image_index);
264
265extern void _dyld_register_func_for_add_image(
266    void (*func)(struct mach_header *mh, unsigned long vmaddr_slide));
267extern void _dyld_register_func_for_remove_image(
268    void (*func)(struct mach_header *mh, unsigned long vmaddr_slide));
269extern void _dyld_register_func_for_link_module(
270    void (*func)(NSModule module));
271/* not yet implemented */
272extern void _dyld_register_func_for_unlink_module(
273    void (*func)(NSModule module));
274/* not yet implemented */
275extern void _dyld_register_func_for_replace_module(
276    void (*func)(NSModule oldmodule, NSModule newmodule));
277extern void _dyld_get_objc_module_sect_for_module(
278    NSModule module,
279    void **objc_module,
280    unsigned long *size);
281extern void _dyld_bind_objc_module(
282    void *objc_module);
283extern enum DYLD_BOOL _dyld_bind_fully_image_containing_address(
284    unsigned long *address);
285extern enum DYLD_BOOL _dyld_image_containing_address(
286    unsigned long address);
287/* SPI first appeared in Mac OS X 10.3 */
288extern struct mach_header * _dyld_get_image_header_containing_address(
289    unsigned long address)
290    AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
291
292extern void _dyld_moninit(
293    void (*monaddition)(char *lowpc, char *highpc));
294extern enum DYLD_BOOL _dyld_launched_prebound(
295    void);
296/* SPI first appeared in Mac OS X 10.3 */
297extern enum DYLD_BOOL _dyld_all_twolevel_modules_prebound(
298    void)
299    AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
300
301extern void _dyld_lookup_and_bind(
302    const char *symbol_name,
303    unsigned long *address,
304    void **module);
305extern void _dyld_lookup_and_bind_with_hint(
306    const char *symbol_name,
307    const char *library_name_hint,
308    unsigned long *address,
309    void **module);
310extern void _dyld_lookup_and_bind_objc(
311    const char *symbol_name,
312    unsigned long *address,
313    void **module);
314extern void _dyld_lookup_and_bind_fully(
315    const char *symbol_name,
316    unsigned long *address,
317    void **module);
318
319__private_extern__ int _dyld_func_lookup(
320    const char *dyld_func_name,
321    unsigned long *address);
322
323#ifdef __cplusplus
324}
325#endif /* __cplusplus */
326
327#endif /* _MACH_O_DYLD_H_ */
328