/[cmucl]/src/lisp/ppc-darwin-dlshim.c
ViewVC logotype

Contents of /src/lisp/ppc-darwin-dlshim.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Wed Mar 19 09:17:13 2008 UTC (6 years ago) by cshapiro
Branch: MAIN
CVS Tags: sparc-tramp-assem-base, post-merge-intl-branch, merged-unicode-utf16-extfmt-2009-06-11, unicode-utf16-extfmt-2009-03-27, snapshot-2008-08, snapshot-2008-09, sse2-packed-2008-11-12, snapshot-2008-05, snapshot-2008-06, snapshot-2008-07, intl-branch-working-2010-02-19-1000, unicode-string-buffer-impl-base, sse2-base, release-20b-pre1, release-20b-pre2, unicode-string-buffer-base, sse2-packed-base, sparc-tramp-assem-2010-07-19, amd64-dd-start, release-19f-pre1, snapshot-2008-12, snapshot-2008-11, intl-2-branch-base, GIT-CONVERSION, unicode-utf16-sync-2008-12, cross-sol-x86-merged, label-2009-03-16, release-19f-base, merge-sse2-packed, merge-with-19f, intl-branch-working-2010-02-11-1000, unicode-snapshot-2009-05, unicode-snapshot-2009-06, unicode-utf16-sync-2008-07, unicode-utf16-sync-2008-09, unicode-utf16-extfmts-sync-2008-12, RELEASE_20b, snapshot-2008-04, unicode-utf16-sync-label-2009-03-16, RELEASE_19f, release-20a-base, cross-sol-x86-base, unicode-utf16-char-support-2009-03-26, unicode-utf16-char-support-2009-03-25, unicode-utf16-extfmts-pre-sync-2008-11, snapshot-2008-10, snapshot-2010-12, snapshot-2010-11, unicode-utf16-sync-2008-11, snapshot-2011-09, snapshot-2011-06, snapshot-2011-07, snapshot-2011-04, snapshot-2011-02, snapshot-2011-03, snapshot-2011-01, pre-merge-intl-branch, snapshot-2010-05, snapshot-2010-04, snapshot-2010-07, snapshot-2010-06, snapshot-2010-01, snapshot-2010-03, snapshot-2010-02, snapshot-2010-08, label-2009-03-25, cross-sol-x86-2010-12-20, sse2-checkpoint-2008-10-01, intl-branch-2010-03-18-1300, sse2-merge-with-2008-11, sse2-merge-with-2008-10, RELEASE_20a, release-20a-pre1, snapshot-2009-11, snapshot-2009-12, unicode-utf16-extfmt-2009-06-11, portable-clx-import-2009-06-16, unicode-utf16-string-support, cross-sparc-branch-base, intl-branch-base, unicode-utf16-base, portable-clx-base, snapshot-2009-08, snapshot-2009-02, snapshot-2009-01, snapshot-2009-07, snapshot-2009-05, snapshot-2009-04, HEAD
Branch point for: RELEASE-19F-BRANCH, portable-clx-branch, cross-sparc-branch, RELEASE-20B-BRANCH, unicode-string-buffer-branch, sparc-tramp-assem-branch, sse2-packed-branch, RELEASE-20A-BRANCH, amd64-dd-branch, unicode-string-buffer-impl-branch, intl-branch, unicode-utf16-branch, cross-sol-x86-branch, sse2-branch, intl-2-branch, unicode-utf16-extfmt-branch
Changes since 1.2: +1 -1 lines
File MIME type: text/plain
Always use prototype style function definitions.  Consistently use the
void keyword to specify empty parameter lists.
1 /*
2 * These functions emulate a small subset of the dlopen / dlsym
3 * functionality under Darwin's Mach-O dyld system.
4 *
5 * This file was graciously taken from the SBCL OS X/Darwin port, for
6 * which it was originally written by Brian Mastenbrook:
7 */
8
9 /*
10 * This software is part of the SBCL system. See the README file for
11 * more information.
12 *
13 * This software is derived from the CMU CL system, which was
14 * written at Carnegie Mellon University and released into the
15 * public domain. The software is in the public domain and is
16 * provided with absolutely no warranty. See the COPYING and CREDITS
17 * files for more information.
18 */
19
20
21 #include <mach-o/dyld.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 /* Darwin does not define the standard ELF
27 * dlopen/dlclose/dlsym/dlerror interface to shared libraries, so this
28 * is an attempt at a minimal wrapper to allow SBCL to work without
29 * external dependency on pogma's dlcompat library.
30 */
31
32 /* For now, there is no RTLD_GLOBAL emulation either. */
33
34 static char dl_self; /* I'm going to abuse this */
35
36 #define RTLD_LAZY 1
37 #define RTLD_NOW 2
38 #define RTLD_GLOBAL 0x100
39
40 static int callback_count;
41 static struct mach_header *last_header;
42
43 void
44 dlshim_image_callback(struct mach_header *ptr, unsigned long phooey)
45 {
46 callback_count++;
47 last_header = ptr;
48 }
49
50 int
51 lib_path_count(void)
52 {
53 char *libpath;
54 int i;
55 int count;
56
57 libpath = getenv("DYLD_LIBRARY_PATH");
58 count = 1;
59 if (libpath) {
60 for (i = 0; libpath[i] != '\0'; i++) {
61 if (libpath[i] == ':')
62 count++;
63 }
64 }
65 return count;
66 }
67
68 const char *
69 lib_path_prefixify(int index, const char *filename)
70 {
71 static char *retbuf = NULL;
72 int fi, li, i, count;
73 char *libpath;
74
75 if (!retbuf) {
76 retbuf = (char *) malloc(1024 * sizeof(char));
77 }
78 count = 0;
79 fi = 0;
80 li = -1;
81 libpath = getenv("DYLD_LIBRARY_PATH");
82 if (libpath) {
83 i = 0;
84 while (count != index && libpath[i] != '\0') {
85 if (libpath[i] == ':')
86 count++;
87 i++;
88 }
89 fi = i;
90 while (libpath[i] != '\0' && libpath[i] != ':') {
91 i++;
92 }
93 li = i - 1;
94 }
95 if (li - fi > 0) {
96 if (li - fi + 1 > 1022 - strlen(filename)) {
97 retbuf =
98 (char *) realloc(retbuf,
99
100 (li - fi + 3 +
101 strlen(filename)) * sizeof(char));
102 }
103 memcpy(retbuf, libpath + fi, (li - fi + 1) * sizeof(char));
104
105 retbuf[li - fi + 1] = '/';
106 memcpy(retbuf + li - fi + 2, filename, strlen(filename) + 1);
107 return retbuf;
108 } else {
109 return filename;
110 }
111 }
112
113 void *
114 dlopen(const char *filename, int flags)
115 {
116 static char has_callback = 0;
117
118 if (!has_callback) {
119 _dyld_register_func_for_add_image(dlshim_image_callback);
120 }
121 if (!filename) {
122 return &dl_self;
123 } else {
124 struct mach_header *img = NULL;
125
126 if (!img)
127 img = NSAddImage(filename, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
128 if (!img)
129 img =
130 NSAddImage(filename,
131 NSADDIMAGE_OPTION_RETURN_ON_ERROR |
132 NSADDIMAGE_OPTION_WITH_SEARCHING);
133 if (!img) {
134 NSObjectFileImage fileImage;
135
136 callback_count = 0;
137 last_header = NULL;
138 if (NSCreateObjectFileImageFromFile(filename, &fileImage) ==
139 NSObjectFileImageSuccess) {
140 NSLinkModule(fileImage, filename,
141 NSLINKMODULE_OPTION_BINDNOW |
142 ((flags & RTLD_GLOBAL) ?
143 NSLINKMODULE_OPTION_PRIVATE : 0) |
144 NSLINKMODULE_OPTION_RETURN_ON_ERROR);
145 if (callback_count && last_header)
146 img = last_header;
147 }
148 }
149 if (!img) {
150 NSObjectFileImage fileImage;
151 int i, maxi;
152 char *prefixfilename;
153
154 maxi = lib_path_count();
155 for (i = 0; i < maxi && !img; i++) {
156 prefixfilename = lib_path_prefixify(i, filename);
157 callback_count = 0;
158 last_header = NULL;
159 if (NSCreateObjectFileImageFromFile(prefixfilename, &fileImage)
160 == NSObjectFileImageSuccess) {
161 NSLinkModule(fileImage, filename,
162 NSLINKMODULE_OPTION_BINDNOW |
163 ((flags & RTLD_GLOBAL) ?
164 NSLINKMODULE_OPTION_PRIVATE : 0) |
165 NSLINKMODULE_OPTION_RETURN_ON_ERROR);
166 if (callback_count && last_header)
167 img = last_header;
168 }
169 }
170 }
171 if (img) {
172 if (flags & RTLD_NOW) {
173 NSLookupSymbolInImage(img, "",
174 NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_FULLY |
175 NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
176 }
177 if (NSIsSymbolNameDefinedInImage(img, "__init")) {
178 NSSymbol *initsymbol;
179 void (*initfunc) (void);
180
181 initsymbol = NSLookupSymbolInImage(img, "__init", 0);
182 initfunc = NSAddressOfSymbol(initsymbol);
183 initfunc();
184 }
185 }
186 return img;
187 }
188 }
189
190 const char *
191 dlerror(void)
192 {
193 static char *errbuf = NULL;
194 NSLinkEditErrors a;
195 int b;
196 char *c, *d;
197
198 NSLinkEditError(&a, &b, &c, &d);
199 if (!errbuf) {
200 errbuf = (char *) malloc(256 * sizeof(char));
201 }
202 snprintf(errbuf, 255, "%s in %s: %d %d", c, d, a, b);
203 return errbuf;
204 }
205
206 void *
207 dlsym(void *handle, char *symbol)
208 {
209 if (handle == &dl_self) {
210 if (NSIsSymbolNameDefined(symbol)) {
211 NSSymbol *retsym;
212
213 retsym = NSLookupAndBindSymbol(symbol);
214 return NSAddressOfSymbol(retsym);
215 } else {
216 return NULL;
217 }
218 } else {
219 if (NSIsSymbolNameDefinedInImage(handle, symbol)) {
220 NSSymbol *retsym;
221
222 retsym = NSLookupSymbolInImage(handle, symbol, 0);
223 return NSAddressOfSymbol(retsym);
224 } else {
225 return NULL;
226 }
227 }
228 }
229
230 int
231 dlclose(void *handle)
232 {
233 /* dlclose is not implemented, and never will be for dylibs.
234 * return -1 to signal an error; it's not used by SBCL anyhow */
235 return -1;
236 }

  ViewVC Help
Powered by ViewVC 1.1.5