Commit | Line | Data |
a0d0e21e |
1 | /* dl_next.xs |
2 | * |
3 | * Platform: NeXT NS 3.2 |
4 | * Author: Anno Siegel (siegel@zrz.TU-Berlin.DE) |
5 | * Based on: dl_dlopen.xs by Paul Marquess |
6 | * Created: Aug 15th, 1994 |
7 | * |
8 | */ |
9 | |
10 | /* |
4ac71550 |
11 | * And Gandalf said: 'Many folk like to know beforehand what is to |
12 | * be set on the table; but those who have laboured to prepare the |
13 | * feast like to keep their secret; for wonder makes the words of |
14 | * praise louder.' |
15 | * |
16 | * [p.970 of _The Lord of the Rings_, VI/v: "The Steward and the King"] |
17 | */ |
a0d0e21e |
18 | |
19 | /* Porting notes: |
20 | |
21 | dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess. It |
22 | should not be used as a base for further ports though it may be used |
23 | as an example for how dl_dlopen.xs can be ported to other platforms. |
24 | |
25 | The method used here is just to supply the sun style dlopen etc. |
26 | functions in terms of NeXTs rld_*. The xs code proper is unchanged |
27 | from Paul's original. |
28 | |
29 | The port could use some streamlining. For one, error handling could |
30 | be simplified. |
31 | |
32 | Anno Siegel |
33 | |
34 | */ |
35 | |
d6bcbf1c |
36 | #if NS_TARGET_MAJOR >= 4 |
37 | #else |
8e07c86e |
38 | /* include these before perl headers */ |
39 | #include <mach-o/rld.h> |
40 | #include <streams/streams.h> |
d6bcbf1c |
41 | #endif |
8e07c86e |
42 | |
a0d0e21e |
43 | #include "EXTERN.h" |
44 | #include "perl.h" |
45 | #include "XSUB.h" |
46 | |
8e07c86e |
47 | #define DL_LOADONCEONLY |
a0d0e21e |
48 | |
cdc73a10 |
49 | typedef struct { |
50 | AV * x_resolve_using; |
51 | } my_cxtx_t; /* this *must* be named my_cxtx_t */ |
a0d0e21e |
52 | |
cdc73a10 |
53 | #define DL_CXT_EXTRA /* ask for dl_cxtx to be defined in dlutils.c */ |
54 | #include "dlutils.c" /* SaveError() etc */ |
a0d0e21e |
55 | |
cdc73a10 |
56 | #define dl_resolve_using (dl_cxtx.x_resolve_using) |
a0d0e21e |
57 | |
d6bcbf1c |
58 | static char *dlerror() |
59 | { |
cdc73a10 |
60 | dTHX; |
61 | dMY_CXT; |
d6bcbf1c |
62 | return dl_last_error; |
63 | } |
64 | |
65 | int dlclose(handle) /* stub only */ |
66 | void *handle; |
67 | { |
68 | return 0; |
69 | } |
70 | |
71 | #if NS_TARGET_MAJOR >= 4 |
72 | #import <mach-o/dyld.h> |
73 | |
74 | enum dyldErrorSource |
75 | { |
76 | OFImage, |
77 | }; |
78 | |
79 | static void TranslateError |
80 | (const char *path, enum dyldErrorSource type, int number) |
81 | { |
5b877257 |
82 | dTHX; |
cdc73a10 |
83 | dMY_CXT; |
46fc3d4c |
84 | char *error; |
d6bcbf1c |
85 | unsigned int index; |
86 | static char *OFIErrorStrings[] = |
87 | { |
88 | "%s(%d): Object Image Load Failure\n", |
89 | "%s(%d): Object Image Load Success\n", |
d1be9408 |
90 | "%s(%d): Not a recognisable object file\n", |
d6bcbf1c |
91 | "%s(%d): No valid architecture\n", |
92 | "%s(%d): Object image has an invalid format\n", |
93 | "%s(%d): Invalid access (permissions?)\n", |
94 | "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n", |
95 | }; |
96 | #define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0])) |
97 | |
d6bcbf1c |
98 | switch (type) |
99 | { |
100 | case OFImage: |
101 | index = number; |
102 | if (index > NUM_OFI_ERRORS - 1) |
103 | index = NUM_OFI_ERRORS - 1; |
7a3f2258 |
104 | error = Perl_form_nocontext(OFIErrorStrings[index], path, number); |
d6bcbf1c |
105 | break; |
106 | |
107 | default: |
7a3f2258 |
108 | error = Perl_form_nocontext("%s(%d): Totally unknown error type %d\n", |
46fc3d4c |
109 | path, number, type); |
d6bcbf1c |
110 | break; |
111 | } |
8c52afec |
112 | Safefree(dl_last_error); |
46fc3d4c |
113 | dl_last_error = savepv(error); |
d6bcbf1c |
114 | } |
115 | |
116 | static char *dlopen(char *path, int mode /* mode is ignored */) |
117 | { |
118 | int dyld_result; |
119 | NSObjectFileImage ofile; |
120 | NSModule handle = NULL; |
121 | |
122 | dyld_result = NSCreateObjectFileImageFromFile(path, &ofile); |
123 | if (dyld_result != NSObjectFileImageSuccess) |
124 | TranslateError(path, OFImage, dyld_result); |
125 | else |
126 | { |
127 | // NSLinkModule will cause the run to abort on any link error's |
128 | // not very friendly but the error recovery functionality is limited. |
129 | handle = NSLinkModule(ofile, path, TRUE); |
130 | } |
131 | |
132 | return handle; |
133 | } |
134 | |
135 | void * |
136 | dlsym(handle, symbol) |
137 | void *handle; |
138 | char *symbol; |
139 | { |
140 | void *addr; |
141 | |
142 | if (NSIsSymbolNameDefined(symbol)) |
143 | addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol)); |
144 | else |
145 | addr = NULL; |
146 | |
147 | return addr; |
148 | } |
149 | |
150 | #else /* NS_TARGET_MAJOR <= 3 */ |
151 | |
152 | static NXStream *OpenError(void) |
a0d0e21e |
153 | { |
154 | return NXOpenMemory( (char *) 0, 0, NX_WRITEONLY); |
155 | } |
156 | |
d6bcbf1c |
157 | static void TransferError(NXStream *s) |
a0d0e21e |
158 | { |
159 | char *buffer; |
160 | int len, maxlen; |
ecdc15d9 |
161 | dTHX; |
cdc73a10 |
162 | dMY_CXT; |
a0d0e21e |
163 | |
164 | if ( dl_last_error ) { |
8c52afec |
165 | Safefree(dl_last_error); |
a0d0e21e |
166 | } |
167 | NXGetMemoryBuffer(s, &buffer, &len, &maxlen); |
a02a5408 |
168 | Newx(dl_last_error, len, char); |
a0d0e21e |
169 | strcpy(dl_last_error, buffer); |
170 | } |
171 | |
d6bcbf1c |
172 | static void CloseError(NXStream *s) |
a0d0e21e |
173 | { |
174 | if ( s ) { |
175 | NXCloseMemory( s, NX_FREEBUFFER); |
176 | } |
177 | } |
178 | |
d6bcbf1c |
179 | static char *dlopen(char *path, int mode /* mode is ignored */) |
a0d0e21e |
180 | { |
181 | int rld_success; |
8e07c86e |
182 | NXStream *nxerr; |
a0d0e21e |
183 | I32 i, psize; |
184 | char *result; |
185 | char **p; |
2d8e6c8d |
186 | STRLEN n_a; |
ecdc15d9 |
187 | dTHX; |
cdc73a10 |
188 | dMY_CXT; |
8e07c86e |
189 | |
190 | /* Do not load what is already loaded into this process */ |
191 | if (hv_fetch(dl_loaded_files, path, strlen(path), 0)) |
192 | return path; |
a0d0e21e |
193 | |
8e07c86e |
194 | nxerr = OpenError(); |
195 | psize = AvFILL(dl_resolve_using) + 3; |
a0d0e21e |
196 | p = (char **) safemalloc(psize * sizeof(char*)); |
197 | p[0] = path; |
198 | for(i=1; i<psize-1; i++) { |
2d8e6c8d |
199 | p[i] = SvPVx(*av_fetch(dl_resolve_using, i-1, TRUE), n_a); |
a0d0e21e |
200 | } |
201 | p[psize-1] = 0; |
202 | rld_success = rld_load(nxerr, (struct mach_header **)0, p, |
203 | (const char *) 0); |
204 | safefree((char*) p); |
205 | if (rld_success) { |
206 | result = path; |
8e07c86e |
207 | /* prevent multiple loads of same file into same process */ |
3280af22 |
208 | hv_store(dl_loaded_files, path, strlen(path), &PL_sv_yes, 0); |
a0d0e21e |
209 | } else { |
210 | TransferError(nxerr); |
211 | result = (char*) 0; |
212 | } |
213 | CloseError(nxerr); |
214 | return result; |
215 | } |
216 | |
a0d0e21e |
217 | void * |
218 | dlsym(handle, symbol) |
219 | void *handle; |
220 | char *symbol; |
221 | { |
222 | NXStream *nxerr = OpenError(); |
a0d0e21e |
223 | unsigned long symref = 0; |
224 | |
7a3f2258 |
225 | if (!rld_lookup(nxerr, Perl_form_nocontext("_%s", symbol), &symref)) |
a0d0e21e |
226 | TransferError(nxerr); |
a0d0e21e |
227 | CloseError(nxerr); |
228 | return (void*) symref; |
229 | } |
230 | |
d6bcbf1c |
231 | #endif /* NS_TARGET_MAJOR >= 4 */ |
232 | |
a0d0e21e |
233 | |
234 | /* ----- code from dl_dlopen.xs below here ----- */ |
235 | |
236 | |
237 | static void |
cea2e8a9 |
238 | dl_private_init(pTHX) |
a0d0e21e |
239 | { |
cea2e8a9 |
240 | (void)dl_generic_private_init(aTHX); |
cdc73a10 |
241 | { |
242 | dMY_CXT; |
243 | dl_resolve_using = get_av("DynaLoader::dl_resolve_using", GV_ADDMULTI); |
244 | } |
a0d0e21e |
245 | } |
246 | |
247 | MODULE = DynaLoader PACKAGE = DynaLoader |
248 | |
249 | BOOT: |
cea2e8a9 |
250 | (void)dl_private_init(aTHX); |
a0d0e21e |
251 | |
252 | |
253 | |
254 | void * |
ff7f3c60 |
255 | dl_load_file(filename, flags=0) |
a0d0e21e |
256 | char * filename |
ff7f3c60 |
257 | int flags |
258 | PREINIT: |
a0d0e21e |
259 | int mode = 1; |
ff7f3c60 |
260 | CODE: |
bf49b057 |
261 | DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags)); |
ff7f3c60 |
262 | if (flags & 0x01) |
cea2e8a9 |
263 | Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename); |
a0d0e21e |
264 | RETVAL = dlopen(filename, mode) ; |
bf49b057 |
265 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", RETVAL)); |
a0d0e21e |
266 | ST(0) = sv_newmortal() ; |
267 | if (RETVAL == NULL) |
cea2e8a9 |
268 | SaveError(aTHX_ "%s",dlerror()) ; |
a0d0e21e |
269 | else |
3175b8cd |
270 | sv_setiv( ST(0), PTR2IV(RETVAL) ); |
a0d0e21e |
271 | |
272 | |
273 | void * |
274 | dl_find_symbol(libhandle, symbolname) |
275 | void * libhandle |
276 | char * symbolname |
277 | CODE: |
d6bcbf1c |
278 | #if NS_TARGET_MAJOR >= 4 |
7a3f2258 |
279 | symbolname = Perl_form_nocontext("_%s", symbolname); |
d6bcbf1c |
280 | #endif |
bf49b057 |
281 | DLDEBUG(2, PerlIO_printf(Perl_debug_log, |
46fc3d4c |
282 | "dl_find_symbol(handle=%lx, symbol=%s)\n", |
283 | (unsigned long) libhandle, symbolname)); |
a0d0e21e |
284 | RETVAL = dlsym(libhandle, symbolname); |
bf49b057 |
285 | DLDEBUG(2, PerlIO_printf(Perl_debug_log, |
46fc3d4c |
286 | " symbolref = %lx\n", (unsigned long) RETVAL)); |
a0d0e21e |
287 | ST(0) = sv_newmortal() ; |
288 | if (RETVAL == NULL) |
cea2e8a9 |
289 | SaveError(aTHX_ "%s",dlerror()) ; |
a0d0e21e |
290 | else |
3175b8cd |
291 | sv_setiv( ST(0), PTR2IV(RETVAL) ); |
a0d0e21e |
292 | |
293 | |
294 | void |
295 | dl_undef_symbols() |
296 | PPCODE: |
297 | |
298 | |
299 | |
300 | # These functions should not need changing on any platform: |
301 | |
302 | void |
303 | dl_install_xsub(perl_name, symref, filename="$Package") |
304 | char * perl_name |
305 | void * symref |
d3f5e399 |
306 | const char * filename |
a0d0e21e |
307 | CODE: |
bf49b057 |
308 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n", |
a0d0e21e |
309 | perl_name, symref)); |
77004dee |
310 | ST(0) = sv_2mortal(newRV((SV*)newXS_flags(perl_name, |
311 | (void(*)(pTHX_ CV *))symref, |
312 | filename, NULL, |
313 | XS_DYNAMIC_FILENAME))); |
a0d0e21e |
314 | |
315 | |
316 | char * |
317 | dl_error() |
318 | CODE: |
cdc73a10 |
319 | dMY_CXT; |
320 | RETVAL = dl_last_error ; |
a0d0e21e |
321 | OUTPUT: |
322 | RETVAL |
323 | |
8c472fc1 |
324 | #if defined(USE_ITHREADS) |
325 | |
326 | void |
327 | CLONE(...) |
328 | CODE: |
329 | MY_CXT_CLONE; |
330 | |
331 | /* MY_CXT_CLONE just does a memcpy on the whole structure, so to avoid |
332 | * using Perl variables that belong to another thread, we create our |
333 | * own for this thread. |
334 | */ |
335 | MY_CXT.x_dl_last_error = newSVpvn("", 0); |
336 | dl_resolve_using = get_av("DynaLoader::dl_resolve_using", GV_ADDMULTI); |
337 | |
338 | #endif |
339 | |
a0d0e21e |
340 | # end. |