/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- dl_open
- dl_error
- dl_sym
- dl_close
1 /*
2 * dl_darwin.c - dlopen() interface for MacOS X/Darwin
3 *
4 * Copyright (c) 2000-2003 Shiro Kawai, All rights reserved.
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 * $Id: dl_darwin.c,v 1.4 2003/07/05 03:29:12 shirok Exp $
34 */
35
36 /* Cf: Technical Note TN2071 "Porting Command Line Unix Tools to MacOS X"
37 http://developer.apple.com/technotes/tn2002/tn2071.html
38 Cf: Object File Image Functions
39 http://developer.apple.com/techpubs/macosx/DeveloperTools/MachORuntime/5rt_api_reference/chapter_5_section_3.html
40 */
41
42 /* This file is included in load.c */
43
44 /* NB: This isn't really used for now, since GC requires a special wrapper
45 around dlopen() and we can't casually replace it. I need to tweak
46 GC's external API so that we can safely wrap these calls. */
47
48 #include <mach-o/dyld.h>
49
50 /* we can store the error message in static variable, because
51 the caller guarantees dl_open() and subsequent dl_error()
52 (in case dl_open() fails) is atomic. */
53 static const char *dl_open_errstr = NULL;
54
55 static void *dl_open(const char *path)
56 {
57 NSObjectFileImage image;
58 NSObjectFileImageReturnCode r;
59 NSModule module;
60 unsigned long options = NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR;
61
62 r = NSCreateObjectFileImageFromFile(path, &image);
63 if (r != NSObjectFileImageSuccess) {
64 switch (r) {
65 case NSObjectFileImageInappropriateFile:
66 dl_open_errstr = "The specified Mach-O file is not of a type this function can operate upon.";
67 break;
68 case NSObjectFileImageArch:
69 dl_open_errstr = "The specified Mach-O file is for a different CPU architecture.";
70 break;
71 case NSObjectFileImageFormat:
72 dl_open_errstr = "The specified file does not appear to be a Mach-O file.";
73 break;
74 case NSObjectFileImageAccess:
75 dl_open_errstr = "The access permissions for the specified file do not permit the creation of the image.";
76 break;
77 default:
78 dl_open_errstr = "Unknown error.";
79 break;
80 }
81 return NULL;
82 }
83
84 module = NSLinkModule(image, path, options);
85 NSDestroyObjectFileImage(image);
86 if (module == NULL) {
87 dl_open_errstr = "NSLinkModule failed";
88 }
89 return module;
90 }
91
92 static const char *dl_error(void)
93 {
94 return dl_open_errstr;
95 }
96
97 static ScmDynLoadInitFn dl_sym(void *handle, const char *name)
98 {
99 NSSymbol sym = NSLookupSymbolInModule((NSModule)handle, name);
100 if (sym == NULL) return NULL;
101 return (ScmDynLoadInitFn)NSAddressOfSymbol(sym);
102 }
103
104 static void dl_close(void *handle)
105 {
106 (void)NSUnLinkModule((NSModule)handle, NSUNLINKMODULE_OPTION_NONE);
107 }
108