This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Argument sanity checking.
[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/*
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
18/* Porting notes:
19
f556e5b9
JH
20dl_dyld.xs is based on dl_next.xs by Anno Siegel.
21
22dl_next.xs is in turn a port from dl_dlopen.xs by Paul Marquess. It
8f1f23e8
W
23should not be used as a base for further ports though it may be used
24as an example for how dl_dlopen.xs can be ported to other platforms.
25
26The method used here is just to supply the sun style dlopen etc.
f556e5b9
JH
27functions in terms of NeXT's/Apple's dyld. The xs code proper is
28unchanged from Paul's original.
8f1f23e8
W
29
30The port could use some streamlining. For one, error handling could
31be simplified.
32
f556e5b9
JH
33This should be useable as a replacement for dl_next.xs, but it has not
34been tested on NeXT platforms.
35
36 Wilfredo Sanchez
8f1f23e8
W
37
38*/
39
40#include "EXTERN.h"
41#include "perl.h"
42#include "XSUB.h"
43
cdc73a10 44#include "dlutils.c" /* for SaveError() etc */
8f1f23e8
W
45
46#undef environ
f556e5b9 47#undef bool
8f1f23e8
W
48#import <mach-o/dyld.h>
49
8f1f23e8
W
50static char *dlerror()
51{
cdc73a10
JH
52 dTHX;
53 dMY_CXT;
8f1f23e8
W
54 return dl_last_error;
55}
56
a97fb7de 57static int dlclose(handle) /* stub only */
8f1f23e8
W
58void *handle;
59{
60 return 0;
61}
62
63enum dyldErrorSource
64{
65 OFImage,
66};
67
68static void TranslateError
69 (const char *path, enum dyldErrorSource type, int number)
70{
5b877257 71 dTHX;
cdc73a10 72 dMY_CXT;
8f1f23e8
W
73 char *error;
74 unsigned int index;
75 static char *OFIErrorStrings[] =
76 {
77 "%s(%d): Object Image Load Failure\n",
78 "%s(%d): Object Image Load Success\n",
d1be9408 79 "%s(%d): Not a recognisable object file\n",
8f1f23e8
W
80 "%s(%d): No valid architecture\n",
81 "%s(%d): Object image has an invalid format\n",
82 "%s(%d): Invalid access (permissions?)\n",
83 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
84 };
85#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
86
87 switch (type)
88 {
89 case OFImage:
90 index = number;
91 if (index > NUM_OFI_ERRORS - 1)
92 index = NUM_OFI_ERRORS - 1;
7a3f2258 93 error = Perl_form_nocontext(OFIErrorStrings[index], path, number);
8f1f23e8
W
94 break;
95
96 default:
7a3f2258 97 error = Perl_form_nocontext("%s(%d): Totally unknown error type %d\n",
8f1f23e8
W
98 path, number, type);
99 break;
100 }
101 safefree(dl_last_error);
102 dl_last_error = savepv(error);
103}
104
105static char *dlopen(char *path, int mode /* mode is ignored */)
106{
107 int dyld_result;
108 NSObjectFileImage ofile;
109 NSModule handle = NULL;
110
111 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
112 if (dyld_result != NSObjectFileImageSuccess)
113 TranslateError(path, OFImage, dyld_result);
114 else
115 {
116 // NSLinkModule will cause the run to abort on any link error's
117 // not very friendly but the error recovery functionality is limited.
118 handle = NSLinkModule(ofile, path, TRUE);
bb2834fe 119 NSDestroyObjectFileImage(ofile);
8f1f23e8
W
120 }
121
122 return handle;
123}
124
a97fb7de 125static void *
8f1f23e8
W
126dlsym(handle, symbol)
127void *handle;
128char *symbol;
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 *
178dl_find_symbol(libhandle, symbolname)
179 void * libhandle
180 char * symbolname
181 CODE:
7a3f2258 182 symbolname = Perl_form_nocontext("_%s", symbolname);
bf49b057 183 DLDEBUG(2, PerlIO_printf(Perl_debug_log,
8f1f23e8
W
184 "dl_find_symbol(handle=%lx, symbol=%s)\n",
185 (unsigned long) libhandle, symbolname));
186 RETVAL = dlsym(libhandle, symbolname);
bf49b057 187 DLDEBUG(2, PerlIO_printf(Perl_debug_log,
8f1f23e8
W
188 " symbolref = %lx\n", (unsigned long) RETVAL));
189 ST(0) = sv_newmortal() ;
190 if (RETVAL == NULL)
cea2e8a9 191 SaveError(aTHX_ "%s",dlerror()) ;
8f1f23e8 192 else
3175b8cd 193 sv_setiv( ST(0), PTR2IV(RETVAL) );
8f1f23e8
W
194
195
196void
197dl_undef_symbols()
198 PPCODE:
199
200
201
202# These functions should not need changing on any platform:
203
204void
205dl_install_xsub(perl_name, symref, filename="$Package")
206 char * perl_name
207 void * symref
208 char * filename
209 CODE:
bf49b057 210 DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%x)\n",
8f1f23e8 211 perl_name, symref));
cea2e8a9
GS
212 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
213 (void(*)(pTHX_ CV *))symref,
214 filename)));
8f1f23e8
W
215
216
217char *
218dl_error()
219 CODE:
cdc73a10
JH
220 dMY_CXT;
221 RETVAL = dl_last_error ;
8f1f23e8
W
222 OUTPUT:
223 RETVAL
224
f556e5b9 225# end.