This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix missing X in 2 embed.fnc entries
[perl5.git] / ext / DynaLoader / dl_dyld.xs
CommitLineData
f556e5b9 1/* dl_dyld.xs
8f1f23e8 2 *
f556e5b9
JH
3 * Platform: Darwin (Mac OS)
4 * Author: Wilfredo Sanchez <wsanchez@apple.com>
8f1f23e8
W
5 * Based on: dl_next.xs by Paul Marquess
6 * Based on: dl_dlopen.xs by Anno Siegel
7 * Created: Aug 15th, 1994
8 *
9 */
10
11/*
4ac71550
TC
12 * And Gandalf said: 'Many folk like to know beforehand what is to
13 * be set on the table; but those who have laboured to prepare the
14 * feast like to keep their secret; for wonder makes the words of
15 * praise louder.'
16 *
17 * [p.970 of _The Lord of the Rings_, VI/v: "The Steward and the King"]
18 */
8f1f23e8
W
19
20/* Porting notes:
21
f556e5b9
JH
22dl_dyld.xs is based on dl_next.xs by Anno Siegel.
23
24dl_next.xs is in turn a port from dl_dlopen.xs by Paul Marquess. It
8f1f23e8
W
25should not be used as a base for further ports though it may be used
26as an example for how dl_dlopen.xs can be ported to other platforms.
27
28The method used here is just to supply the sun style dlopen etc.
f556e5b9
JH
29functions in terms of NeXT's/Apple's dyld. The xs code proper is
30unchanged from Paul's original.
8f1f23e8
W
31
32The port could use some streamlining. For one, error handling could
33be simplified.
34
f556e5b9
JH
35This should be useable as a replacement for dl_next.xs, but it has not
36been tested on NeXT platforms.
37
38 Wilfredo Sanchez
8f1f23e8
W
39
40*/
41
73e43954 42#define PERL_EXT
8f1f23e8 43#include "EXTERN.h"
d96ba2c4 44#define PERL_IN_DL_DYLD_XS
8f1f23e8
W
45#include "perl.h"
46#include "XSUB.h"
47
cdc73a10 48#include "dlutils.c" /* for SaveError() etc */
8f1f23e8
W
49
50#undef environ
f556e5b9 51#undef bool
8f1f23e8
W
52#import <mach-o/dyld.h>
53
8f1f23e8
W
54static char *dlerror()
55{
cdc73a10
JH
56 dTHX;
57 dMY_CXT;
8f1f23e8
W
58 return dl_last_error;
59}
60
2ec6e385 61static int dlclose(void *handle) /* stub only */
8f1f23e8
W
62{
63 return 0;
64}
65
66enum dyldErrorSource
67{
68 OFImage,
69};
70
71static void TranslateError
72 (const char *path, enum dyldErrorSource type, int number)
73{
5b877257 74 dTHX;
cdc73a10 75 dMY_CXT;
8f1f23e8
W
76 char *error;
77 unsigned int index;
78 static char *OFIErrorStrings[] =
79 {
80 "%s(%d): Object Image Load Failure\n",
81 "%s(%d): Object Image Load Success\n",
d1be9408 82 "%s(%d): Not a recognisable object file\n",
8f1f23e8
W
83 "%s(%d): No valid architecture\n",
84 "%s(%d): Object image has an invalid format\n",
85 "%s(%d): Invalid access (permissions?)\n",
86 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
87 };
88#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
89
90 switch (type)
91 {
92 case OFImage:
93 index = number;
94 if (index > NUM_OFI_ERRORS - 1)
95 index = NUM_OFI_ERRORS - 1;
7a3f2258 96 error = Perl_form_nocontext(OFIErrorStrings[index], path, number);
8f1f23e8
W
97 break;
98
99 default:
7a3f2258 100 error = Perl_form_nocontext("%s(%d): Totally unknown error type %d\n",
8f1f23e8
W
101 path, number, type);
102 break;
103 }
379d1ffd 104 sv_setpv(MY_CXT.x_dl_last_error, error);
8f1f23e8
W
105}
106
107static char *dlopen(char *path, int mode /* mode is ignored */)
108{
109 int dyld_result;
110 NSObjectFileImage ofile;
111 NSModule handle = NULL;
112
113 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
114 if (dyld_result != NSObjectFileImageSuccess)
115 TranslateError(path, OFImage, dyld_result);
116 else
117 {
03f6ae00 118 // NSLinkModule will cause the run to abort on any link errors
8f1f23e8
W
119 // not very friendly but the error recovery functionality is limited.
120 handle = NSLinkModule(ofile, path, TRUE);
bb2834fe 121 NSDestroyObjectFileImage(ofile);
8f1f23e8
W
122 }
123
124 return handle;
125}
126
a97fb7de 127static void *
2ec6e385 128dlsym(void *handle, char *symbol)
8f1f23e8
W
129{
130 void *addr;
131
132 if (NSIsSymbolNameDefined(symbol))
133 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
134 else
135 addr = NULL;
136
137 return addr;
138}
139
140
141
142/* ----- code from dl_dlopen.xs below here ----- */
143
144
145static void
cea2e8a9 146dl_private_init(pTHX)
8f1f23e8 147{
cea2e8a9 148 (void)dl_generic_private_init(aTHX);
8f1f23e8
W
149}
150
151MODULE = DynaLoader PACKAGE = DynaLoader
152
153BOOT:
cea2e8a9 154 (void)dl_private_init(aTHX);
8f1f23e8
W
155
156
157
158void *
159dl_load_file(filename, flags=0)
160 char * filename
161 int flags
162 PREINIT:
163 int mode = 1;
164 CODE:
bf49b057 165 DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
8f1f23e8 166 if (flags & 0x01)
cea2e8a9 167 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
8f1f23e8 168 RETVAL = dlopen(filename, mode) ;
bf49b057 169 DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", RETVAL));
8f1f23e8
W
170 ST(0) = sv_newmortal() ;
171 if (RETVAL == NULL)
cea2e8a9 172 SaveError(aTHX_ "%s",dlerror()) ;
8f1f23e8 173 else
3175b8cd 174 sv_setiv( ST(0), PTR2IV(RETVAL) );
8f1f23e8
W
175
176
177void *
fd46a708 178dl_find_symbol(libhandle, symbolname, ign_err=0)
8f1f23e8
W
179 void * libhandle
180 char * symbolname
fd46a708 181 int ign_err
8f1f23e8 182 CODE:
7a3f2258 183 symbolname = Perl_form_nocontext("_%s", symbolname);
bf49b057 184 DLDEBUG(2, PerlIO_printf(Perl_debug_log,
8f1f23e8
W
185 "dl_find_symbol(handle=%lx, symbol=%s)\n",
186 (unsigned long) libhandle, symbolname));
187 RETVAL = dlsym(libhandle, symbolname);
bf49b057 188 DLDEBUG(2, PerlIO_printf(Perl_debug_log,
8f1f23e8
W
189 " symbolref = %lx\n", (unsigned long) RETVAL));
190 ST(0) = sv_newmortal() ;
fd46a708
RU
191 if (RETVAL == NULL) {
192 if (!ign_err)
193 SaveError(aTHX_ "%s",dlerror()) ;
194 } else
3175b8cd 195 sv_setiv( ST(0), PTR2IV(RETVAL) );
8f1f23e8
W
196
197
198void
199dl_undef_symbols()
200 PPCODE:
201
202
203
204# These functions should not need changing on any platform:
205
206void
207dl_install_xsub(perl_name, symref, filename="$Package")
208 char * perl_name
209 void * symref
d3f5e399 210 const char * filename
8f1f23e8 211 CODE:
bf49b057 212 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
8f1f23e8 213 perl_name, symref));
77004dee
NC
214 ST(0) = sv_2mortal(newRV((SV*)newXS_flags(perl_name,
215 (void(*)(pTHX_ CV *))symref,
216 filename, NULL,
217 XS_DYNAMIC_FILENAME)));
8f1f23e8
W
218
219
bb6a367a 220SV *
8f1f23e8
W
221dl_error()
222 CODE:
cdc73a10 223 dMY_CXT;
bb6a367a 224 RETVAL = newSVsv(MY_CXT.x_dl_last_error);
8f1f23e8
W
225 OUTPUT:
226 RETVAL
227
8c472fc1
CB
228#if defined(USE_ITHREADS)
229
230void
231CLONE(...)
232 CODE:
233 MY_CXT_CLONE;
234
3bd46979
DM
235 PERL_UNUSED_VAR(items);
236
8c472fc1
CB
237 /* MY_CXT_CLONE just does a memcpy on the whole structure, so to avoid
238 * using Perl variables that belong to another thread, we create our
239 * own for this thread.
240 */
c2b90b61 241 MY_CXT.x_dl_last_error = newSVpvs("");
8c472fc1
CB
242
243#endif
244
f556e5b9 245# end.