Make perl_exp.SH smarter about what to include and what to exclude.
[p5sagit/p5-mst-13.2.git] / ext / DynaLoader / dl_rhapsody.xs
CommitLineData
8f1f23e8 1/* dl_rhapsody.xs
2 *
3 * Platform: Apple Rhapsody 5.0
4 * Based on: dl_next.xs by Paul Marquess
5 * Based on: dl_dlopen.xs by Anno Siegel
6 * Created: Aug 15th, 1994
7 *
8 */
9
10/*
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
17/* Porting notes:
18
19dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess. It
20should not be used as a base for further ports though it may be used
21as an example for how dl_dlopen.xs can be ported to other platforms.
22
23The method used here is just to supply the sun style dlopen etc.
24functions in terms of NeXTs rld_*. The xs code proper is unchanged
25from Paul's original.
26
27The port could use some streamlining. For one, error handling could
28be simplified.
29
30Anno Siegel
31
32*/
33
34#include "EXTERN.h"
35#include "perl.h"
36#include "XSUB.h"
37
38#define DL_LOADONCEONLY
39
40#include "dlutils.c" /* SaveError() etc */
41
42#undef environ
43#import <mach-o/dyld.h>
44
45static char * dl_last_error = (char *) 0;
46static AV *dl_resolve_using = Nullav;
47
48static char *dlerror()
49{
50 return dl_last_error;
51}
52
53int dlclose(handle) /* stub only */
54void *handle;
55{
56 return 0;
57}
58
59enum dyldErrorSource
60{
61 OFImage,
62};
63
64static void TranslateError
65 (const char *path, enum dyldErrorSource type, int number)
66{
67 char *error;
68 unsigned int index;
69 static char *OFIErrorStrings[] =
70 {
71 "%s(%d): Object Image Load Failure\n",
72 "%s(%d): Object Image Load Success\n",
73 "%s(%d): Not an recognisable object file\n",
74 "%s(%d): No valid architecture\n",
75 "%s(%d): Object image has an invalid format\n",
76 "%s(%d): Invalid access (permissions?)\n",
77 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
78 };
79#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
80
81 switch (type)
82 {
83 case OFImage:
84 index = number;
85 if (index > NUM_OFI_ERRORS - 1)
86 index = NUM_OFI_ERRORS - 1;
87 error = form(OFIErrorStrings[index], path, number);
88 break;
89
90 default:
91 error = form("%s(%d): Totally unknown error type %d\n",
92 path, number, type);
93 break;
94 }
95 safefree(dl_last_error);
96 dl_last_error = savepv(error);
97}
98
99static char *dlopen(char *path, int mode /* mode is ignored */)
100{
101 int dyld_result;
102 NSObjectFileImage ofile;
103 NSModule handle = NULL;
104
105 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
106 if (dyld_result != NSObjectFileImageSuccess)
107 TranslateError(path, OFImage, dyld_result);
108 else
109 {
110 // NSLinkModule will cause the run to abort on any link error's
111 // not very friendly but the error recovery functionality is limited.
112 handle = NSLinkModule(ofile, path, TRUE);
113 }
114
115 return handle;
116}
117
118void *
119dlsym(handle, symbol)
120void *handle;
121char *symbol;
122{
123 void *addr;
124
125 if (NSIsSymbolNameDefined(symbol))
126 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
127 else
128 addr = NULL;
129
130 return addr;
131}
132
133
134
135/* ----- code from dl_dlopen.xs below here ----- */
136
137
138static void
cea2e8a9 139dl_private_init(pTHX)
8f1f23e8 140{
cea2e8a9 141 (void)dl_generic_private_init(aTHX);
142 dl_resolve_using = get_av("DynaLoader::dl_resolve_using", 0x4);
8f1f23e8 143}
144
145MODULE = DynaLoader PACKAGE = DynaLoader
146
147BOOT:
cea2e8a9 148 (void)dl_private_init(aTHX);
8f1f23e8 149
150
151
152void *
153dl_load_file(filename, flags=0)
154 char * filename
155 int flags
156 PREINIT:
157 int mode = 1;
158 CODE:
159 DLDEBUG(1,PerlIO_printf(PerlIO_stderr(), "dl_load_file(%s,%x):\n", filename,flags));
160 if (flags & 0x01)
cea2e8a9 161 Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
8f1f23e8 162 RETVAL = dlopen(filename, mode) ;
163 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), " libref=%x\n", RETVAL));
164 ST(0) = sv_newmortal() ;
165 if (RETVAL == NULL)
cea2e8a9 166 SaveError(aTHX_ "%s",dlerror()) ;
8f1f23e8 167 else
168 sv_setiv( ST(0), (IV)RETVAL);
169
170
171void *
172dl_find_symbol(libhandle, symbolname)
173 void * libhandle
174 char * symbolname
175 CODE:
176 symbolname = form("_%s", symbolname);
177 DLDEBUG(2, PerlIO_printf(PerlIO_stderr(),
178 "dl_find_symbol(handle=%lx, symbol=%s)\n",
179 (unsigned long) libhandle, symbolname));
180 RETVAL = dlsym(libhandle, symbolname);
181 DLDEBUG(2, PerlIO_printf(PerlIO_stderr(),
182 " symbolref = %lx\n", (unsigned long) RETVAL));
183 ST(0) = sv_newmortal() ;
184 if (RETVAL == NULL)
cea2e8a9 185 SaveError(aTHX_ "%s",dlerror()) ;
8f1f23e8 186 else
187 sv_setiv( ST(0), (IV)RETVAL);
188
189
190void
191dl_undef_symbols()
192 PPCODE:
193
194
195
196# These functions should not need changing on any platform:
197
198void
199dl_install_xsub(perl_name, symref, filename="$Package")
200 char * perl_name
201 void * symref
202 char * filename
203 CODE:
204 DLDEBUG(2,PerlIO_printf(PerlIO_stderr(), "dl_install_xsub(name=%s, symref=%x)\n",
205 perl_name, symref));
cea2e8a9 206 ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name,
207 (void(*)(pTHX_ CV *))symref,
208 filename)));
8f1f23e8 209
210
211char *
212dl_error()
213 CODE:
214 RETVAL = LastError ;
215 OUTPUT:
216 RETVAL
217
218+# end.