Commit | Line | Data |
a0d0e21e |
1 | /* dl_dlopen.xs |
2 | * |
3 | * Platform: SunOS/Solaris, possibly others which use dlopen. |
0536e0eb |
4 | * Author: Paul Marquess (Paul.Marquess@btinternet.com) |
a0d0e21e |
5 | * Created: 10th July 1994 |
6 | * |
7 | * Modified: |
8 | * 15th July 1994 - Added code to explicitly save any error messages. |
9 | * 3rd August 1994 - Upgraded to v3 spec. |
10 | * 9th August 1994 - Changed to use IV |
11 | * 10th August 1994 - Tim Bunce: Added RTLD_LAZY, switchable debugging, |
12 | * basic FreeBSD support, removed ClearError |
13 | * |
14 | */ |
15 | |
16 | /* Porting notes: |
17 | |
18 | |
19 | Definition of Sunos dynamic Linking functions |
20 | ============================================= |
21 | In order to make this implementation easier to understand here is a |
22 | quick definition of the SunOS Dynamic Linking functions which are |
23 | used here. |
24 | |
25 | dlopen |
26 | ------ |
27 | void * |
28 | dlopen(path, mode) |
29 | char * path; |
30 | int mode; |
31 | |
32 | This function takes the name of a dynamic object file and returns |
33 | a descriptor which can be used by dlsym later. It returns NULL on |
34 | error. |
35 | |
36 | The mode parameter must be set to 1 for Solaris 1 and to |
8e07c86e |
37 | RTLD_LAZY (==2) on Solaris 2. |
a0d0e21e |
38 | |
39 | |
40 | dlsym |
41 | ------ |
42 | void * |
43 | dlsym(handle, symbol) |
44 | void * handle; |
45 | char * symbol; |
46 | |
47 | Takes the handle returned from dlopen and the name of a symbol to |
48 | get the address of. If the symbol was found a pointer is |
49 | returned. It returns NULL on error. If DL_PREPEND_UNDERSCORE is |
50 | defined an underscore will be added to the start of symbol. This |
51 | is required on some platforms (freebsd). |
52 | |
53 | dlerror |
54 | ------ |
55 | char * dlerror() |
56 | |
57 | Returns a null-terminated string which describes the last error |
58 | that occurred with either dlopen or dlsym. After each call to |
59 | dlerror the error message will be reset to a null pointer. The |
60 | SaveError function is used to save the error as soo as it happens. |
61 | |
62 | |
63 | Return Types |
64 | ============ |
65 | In this implementation the two functions, dl_load_file & |
66 | dl_find_symbol, return void *. This is because the underlying SunOS |
67 | dynamic linker calls also return void *. This is not necessarily |
68 | the case for all architectures. For example, some implementation |
69 | will want to return a char * for dl_load_file. |
70 | |
71 | If void * is not appropriate for your architecture, you will have to |
72 | change the void * to whatever you require. If you are not certain of |
73 | how Perl handles C data types, I suggest you start by consulting |
74 | Dean Roerich's Perl 5 API document. Also, have a look in the typemap |
75 | file (in the ext directory) for a fairly comprehensive list of types |
76 | that are already supported. If you are completely stuck, I suggest you |
13934ef4 |
77 | post a message to perl5-porters, comp.lang.perl.misc or if you are really |
a0d0e21e |
78 | desperate to me. |
79 | |
80 | Remember when you are making any changes that the return value from |
81 | dl_load_file is used as a parameter in the dl_find_symbol |
82 | function. Also the return value from find_symbol is used as a parameter |
83 | to install_xsub. |
84 | |
85 | |
86 | Dealing with Error Messages |
87 | ============================ |
88 | In order to make the handling of dynamic linking errors as generic as |
89 | possible you should store any error messages associated with your |
90 | implementation with the StoreError function. |
91 | |
92 | In the case of SunOS the function dlerror returns the error message |
93 | associated with the last dynamic link error. As the SunOS dynamic |
94 | linker functions dlopen & dlsym both return NULL on error every call |
95 | to a SunOS dynamic link routine is coded like this |
96 | |
97 | RETVAL = dlopen(filename, 1) ; |
98 | if (RETVAL == NULL) |
99 | SaveError("%s",dlerror()) ; |
100 | |
101 | Note that SaveError() takes a printf format string. Use a "%s" as |
102 | the first parameter if the error may contain and % characters. |
103 | |
104 | */ |
105 | |
106 | #include "EXTERN.h" |
107 | #include "perl.h" |
108 | #include "XSUB.h" |
109 | |
110 | #ifdef I_DLFCN |
111 | #include <dlfcn.h> /* the dynamic linker include file for Sunos/Solaris */ |
112 | #else |
113 | #include <nlist.h> |
114 | #include <link.h> |
115 | #endif |
116 | |
8e07c86e |
117 | #ifndef RTLD_LAZY |
118 | # define RTLD_LAZY 1 /* Solaris 1 */ |
119 | #endif |
120 | |
a0d0e21e |
121 | #ifndef HAS_DLERROR |
5d94fbed |
122 | # ifdef __NetBSD__ |
123 | # define dlerror() strerror(errno) |
124 | # else |
125 | # define dlerror() "Unknown error - dlerror() not implemented" |
126 | # endif |
a0d0e21e |
127 | #endif |
128 | |
129 | |
130 | #include "dlutils.c" /* SaveError() etc */ |
131 | |
132 | |
133 | static void |
cea2e8a9 |
134 | dl_private_init(pTHX) |
a0d0e21e |
135 | { |
cea2e8a9 |
136 | (void)dl_generic_private_init(aTHX); |
a0d0e21e |
137 | } |
138 | |
139 | MODULE = DynaLoader PACKAGE = DynaLoader |
140 | |
141 | BOOT: |
cea2e8a9 |
142 | (void)dl_private_init(aTHX); |
a0d0e21e |
143 | |
144 | |
145 | void * |
ff7f3c60 |
146 | dl_load_file(filename, flags=0) |
147 | char * filename |
148 | int flags |
9d4ce9a5 |
149 | PREINIT: |
8e07c86e |
150 | int mode = RTLD_LAZY; |
9d4ce9a5 |
151 | CODE: |
152 | { |
153 | #if defined(DLOPEN_WONT_DO_RELATIVE_PATHS) |
154 | char pathbuf[PATH_MAX + 2]; |
155 | if (*filename != '/' && strchr(filename, '/')) { |
156 | if (getcwd(pathbuf, PATH_MAX - strlen(filename))) { |
157 | strcat(pathbuf, "/"); |
158 | strcat(pathbuf, filename); |
159 | filename = pathbuf; |
160 | } |
161 | } |
162 | #endif |
8e07c86e |
163 | #ifdef RTLD_NOW |
164 | if (dl_nonlazy) |
165 | mode = RTLD_NOW; |
a0d0e21e |
166 | #endif |
ff7f3c60 |
167 | if (flags & 0x01) |
168 | #ifdef RTLD_GLOBAL |
169 | mode |= RTLD_GLOBAL; |
170 | #else |
cea2e8a9 |
171 | Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename); |
ff7f3c60 |
172 | #endif |
bf49b057 |
173 | DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags)); |
a0d0e21e |
174 | RETVAL = dlopen(filename, mode) ; |
bf49b057 |
175 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%lx\n", (unsigned long) RETVAL)); |
a0d0e21e |
176 | ST(0) = sv_newmortal() ; |
177 | if (RETVAL == NULL) |
cea2e8a9 |
178 | SaveError(aTHX_ "%s",dlerror()) ; |
a0d0e21e |
179 | else |
56431972 |
180 | sv_setiv( ST(0), PTR2IV(RETVAL)); |
9d4ce9a5 |
181 | } |
a0d0e21e |
182 | |
183 | void * |
184 | dl_find_symbol(libhandle, symbolname) |
185 | void * libhandle |
186 | char * symbolname |
187 | CODE: |
188 | #ifdef DLSYM_NEEDS_UNDERSCORE |
7a3f2258 |
189 | symbolname = Perl_form_nocontext("_%s", symbolname); |
a0d0e21e |
190 | #endif |
bf49b057 |
191 | DLDEBUG(2, PerlIO_printf(Perl_debug_log, |
46fc3d4c |
192 | "dl_find_symbol(handle=%lx, symbol=%s)\n", |
193 | (unsigned long) libhandle, symbolname)); |
a0d0e21e |
194 | RETVAL = dlsym(libhandle, symbolname); |
bf49b057 |
195 | DLDEBUG(2, PerlIO_printf(Perl_debug_log, |
46fc3d4c |
196 | " symbolref = %lx\n", (unsigned long) RETVAL)); |
a0d0e21e |
197 | ST(0) = sv_newmortal() ; |
198 | if (RETVAL == NULL) |
cea2e8a9 |
199 | SaveError(aTHX_ "%s",dlerror()) ; |
a0d0e21e |
200 | else |
56431972 |
201 | sv_setiv( ST(0), PTR2IV(RETVAL)); |
a0d0e21e |
202 | |
203 | |
204 | void |
205 | dl_undef_symbols() |
206 | PPCODE: |
207 | |
208 | |
209 | |
210 | # These functions should not need changing on any platform: |
211 | |
212 | void |
213 | dl_install_xsub(perl_name, symref, filename="$Package") |
214 | char * perl_name |
215 | void * symref |
216 | char * filename |
217 | CODE: |
bf49b057 |
218 | DLDEBUG(2,PerlIO_printf(Perl_debug_log, "dl_install_xsub(name=%s, symref=%lx)\n", |
248e2fea |
219 | perl_name, (unsigned long) symref)); |
cea2e8a9 |
220 | ST(0) = sv_2mortal(newRV((SV*)newXS(perl_name, |
221 | (void(*)(pTHX_ CV *))symref, |
222 | filename))); |
a0d0e21e |
223 | |
224 | |
225 | char * |
226 | dl_error() |
227 | CODE: |
228 | RETVAL = LastError ; |
229 | OUTPUT: |
230 | RETVAL |
231 | |
232 | # end. |