Debian Codegraph @

src/dl_darwin.c

raw
1 /*
2 * dl_darwin.c - dlopen() interface for MacOS X/Darwin
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /* Cf: Technical Note TN2071 "Porting Command Line Unix Tools to MacOS X"
35 http://developer.apple.com/technotes/tn2002/tn2071.html
36 Cf: Object File Image Functions
37 http://developer.apple.com/techpubs/macosx/DeveloperTools/MachORuntime/5rt_api_reference/chapter_5_section_3.html
38 */
39
40 /* This file is included in load.c */
41
42 /* NB: This isn't really used for now, since GC requires a special wrapper
43 around dlopen() and we can't casually replace it. I need to tweak
44 GC's external API so that we can safely wrap these calls. */
45
46 #include <mach-o/dyld.h>
47
48 /* we can store the error message in static variable, because
49 the caller guarantees dl_open() and subsequent dl_error()
50 (in case dl_open() fails) is atomic. */
51 static const char *dl_open_errstr = NULL;
52
53 static void *dl_open(const char *path)
54 {
55 NSObjectFileImage image;
56 unsigned long options = NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR;
57
58 NSObjectFileImageReturnCode r = NSCreateObjectFileImageFromFile(path, &image);
59 if (r != NSObjectFileImageSuccess) {
60 switch (r) {
61 case NSObjectFileImageInappropriateFile:
62 dl_open_errstr = "The specified Mach-O file is not of a type this function can operate upon.";
63 break;
64 case NSObjectFileImageArch:
65 dl_open_errstr = "The specified Mach-O file is for a different CPU architecture.";
66 break;
67 case NSObjectFileImageFormat:
68 dl_open_errstr = "The specified file does not appear to be a Mach-O file.";
69 break;
70 case NSObjectFileImageAccess:
71 dl_open_errstr = "The access permissions for the specified file do not permit the creation of the image.";
72 break;
73 default:
74 dl_open_errstr = "Unknown error.";
75 break;
76 }
77 return NULL;
78 }
79
80 NSModule module = NSLinkModule(image, path, options);
81 NSDestroyObjectFileImage(image);
82 if (module == NULL) {
83 dl_open_errstr = "NSLinkModule failed";
84 }
85 return module;
86 }
87
88 static const char *dl_error(void)
89 {
90 return dl_open_errstr;
91 }
92
93 static ScmDynLoadEntry dl_sym(void *handle, const char *name)
94 {
95 NSSymbol sym = NSLookupSymbolInModule((NSModule)handle, name);
96 if (sym == NULL) return NULL;
97 return (ScmDynLoadEntry)NSAddressOfSymbol(sym);
98 }
99
100 static void dl_close(void *handle)
101 {
102 (void)NSUnLinkModule((NSModule)handle, NSUNLINKMODULE_OPTION_NONE);
103 }