blob: f8c0c23af161c3e1cb47b4714da5b6cab135e8db [file] [log] [blame]
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Support for dynamic loading of extension modules */
26/* If no dynamic linking is supported, this file still generates some code! */
27
28#include "allobjects.h"
29#include "osdefs.h"
30#include "importdl.h"
31
32extern int verbose; /* Defined in pythonrun.c */
33
34/* Explanation of some of the the various #defines used by dynamic linking...
35
36 symbol -- defined for:
37
38 DYNAMIC_LINK -- any kind of dynamic linking
Guido van Rossum46c76a61995-01-20 16:53:54 +000039 USE_RLD -- NeXT dynamic linking (currently disabled)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000040 USE_DL -- Jack's dl for IRIX 4 or GNU dld with emulation for Jack's dl
41 USE_SHLIB -- SunOS or IRIX 5 (SVR4?) shared libraries
42 _AIX -- AIX style dynamic linking
43 NT -- NT style dynamic linking (using DLLs)
44 _DL_FUNCPTR_DEFINED -- if the typedef dl_funcptr has been defined
Guido van Rossum6a75d261995-02-18 14:51:15 +000045 USE_MAC_SHARED_LIBRARY -- Mac CFM shared libraries
Guido van Rossum1ae940a1995-01-02 19:04:15 +000046 SHORT_EXT -- short extension for dynamic module, e.g. ".so"
47 LONG_EXT -- long extension, e.g. "module.so"
48 hpux -- HP-UX Dynamic Linking - defined by the compiler
Guido van Rossum46c76a61995-01-20 16:53:54 +000049 __NetBSD__ -- NetBSD shared libraries (not quite SVR4 compatible)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050
51 (The other WITH_* symbols are used only once, to set the
52 appropriate symbols.)
53*/
54
55/* Configure dynamic linking */
56
57#ifdef hpux
58#define DYNAMIC_LINK
59#include <errno.h>
60typedef void (*dl_funcptr)();
61#define _DL_FUNCPTR_DEFINED 1
62#define SHORT_EXT ".sl"
63#define LONG_EXT "module.sl"
64#endif
65
Guido van Rossum46c76a61995-01-20 16:53:54 +000066#ifdef __NetBSD__
67#define DYNAMIC_LINK
68#define USE_SHLIB
69
70#define dlerror() "error in dynamic linking"
71#endif
72
Guido van Rossum0fbec641995-02-27 10:15:36 +000073#ifdef __WIN32__
74#define NT
75#endif
76
Guido van Rossum1ae940a1995-01-02 19:04:15 +000077#ifdef NT
78#define DYNAMIC_LINK
79#include <windows.h>
80typedef FARPROC dl_funcptr;
81#define _DL_FUNCPTR_DEFINED
Guido van Rossum5fb1da71995-01-07 12:36:02 +000082#define SHORT_EXT ".pyd"
83#define LONG_EXT "module.pyd"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000084#endif
85
Guido van Rossum46c76a61995-01-20 16:53:54 +000086#ifdef WITH_RLD
Guido van Rossum1ae940a1995-01-02 19:04:15 +000087#define DYNAMIC_LINK
88#define USE_RLD
89#endif
90
91#ifdef WITH_SGI_DL
92#define DYNAMIC_LINK
93#define USE_DL
94#endif
95
96#ifdef WITH_DL_DLD
97#define DYNAMIC_LINK
98#define USE_DL
99#endif
100
Guido van Rossum6a75d261995-02-18 14:51:15 +0000101#ifdef __CFM68K__
102#define USE_MAC_SHARED_LIBRARY
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000103#endif
104
Jack Jansen4e043731995-02-13 22:42:34 +0000105#ifdef USE_MAC_SHARED_LIBRARY
106#define DYNAMIC_LINK
Guido van Rossum6a75d261995-02-18 14:51:15 +0000107#define SHORT_EXT ".slb"
108#define LONG_EXT "module.slb"
Jack Jansen4e043731995-02-13 22:42:34 +0000109#ifndef _DL_FUNCPTR_DEFINED
110typedef void (*dl_funcptr)();
111#endif
112#endif
113
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000114#if !defined(DYNAMIC_LINK) && defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
115#define DYNAMIC_LINK
116#define USE_SHLIB
117#endif
118
119#ifdef _AIX
120#define DYNAMIC_LINK
121#include <sys/ldr.h>
122typedef void (*dl_funcptr)();
123#define _DL_FUNCPTR_DEFINED
124static void aix_loaderror(char *name);
125#endif
126
127#ifdef DYNAMIC_LINK
128
129#ifdef USE_SHLIB
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000130#include <sys/types.h>
131#include <sys/stat.h>
Guido van Rossum46c76a61995-01-20 16:53:54 +0000132#ifdef __NetBSD__
133#include <nlist.h>
134#include <link.h>
135#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000136#include <dlfcn.h>
Guido van Rossum46c76a61995-01-20 16:53:54 +0000137#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000138#ifndef _DL_FUNCPTR_DEFINED
139typedef void (*dl_funcptr)();
140#endif
141#ifndef RTLD_LAZY
142#define RTLD_LAZY 1
143#endif
144#define SHORT_EXT ".so"
145#define LONG_EXT "module.so"
146#endif /* USE_SHLIB */
147
148#if defined(USE_DL) || defined(hpux)
149#include "dl.h"
150#endif
151
Jack Jansen4e043731995-02-13 22:42:34 +0000152#ifdef USE_MAC_SHARED_LIBRARY
153#include <CodeFragments.h>
Guido van Rossum3097c3a1995-02-21 21:02:46 +0000154#ifdef __CFM68K__ /* Really just an older version of Universal Headers */
Guido van Rossum6a75d261995-02-18 14:51:15 +0000155#define CFragConnectionID ConnectionID
156#define kLoadCFrag 0x01
157#endif
Jack Jansen4e043731995-02-13 22:42:34 +0000158#include <Files.h>
159#include "macdefs.h"
160#include "macglue.h"
161#endif
162
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000163#ifdef USE_RLD
164#include <mach-o/rld.h>
165#define FUNCNAME_PATTERN "_init%.200s"
166#ifndef _DL_FUNCPTR_DEFINED
167typedef void (*dl_funcptr)();
168#endif
169#endif /* USE_RLD */
170
171extern char *getprogramname();
172
173#ifndef FUNCNAME_PATTERN
Guido van Rossum0fbec641995-02-27 10:15:36 +0000174#if defined(__hp9000s300) || defined(__NetBSD__) || defined(__BORLANDC__)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000175#define FUNCNAME_PATTERN "_init%.200s"
176#else
177#define FUNCNAME_PATTERN "init%.200s"
178#endif
179#endif
180
181#if !defined(SHORT_EXT) && !defined(LONG_EXT)
182#define SHORT_EXT ".o"
183#define LONG_EXT "module.o"
184#endif /* !SHORT_EXT && !LONG_EXT */
185
186#endif /* DYNAMIC_LINK */
187
Guido van Rossum6a75d261995-02-18 14:51:15 +0000188/* Max length of module suffix searched for -- accommodates "module.slb" */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000189#ifndef MAXSUFFIXSIZE
Jack Jansen4e043731995-02-13 22:42:34 +0000190#define MAXSUFFIXSIZE 12
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000191#endif
192
193/* Pass it on to import.c */
194int import_maxsuffixsize = MAXSUFFIXSIZE;
195
196struct filedescr import_filetab[] = {
197#ifdef SHORT_EXT
198 {SHORT_EXT, "rb", C_EXTENSION},
199#endif /* !SHORT_EXT */
200#ifdef LONG_EXT
201 {LONG_EXT, "rb", C_EXTENSION},
202#endif /* !LONG_EXT */
203 {".py", "r", PY_SOURCE},
204 {".pyc", "rb", PY_COMPILED},
205 {0, 0}
206};
207
208object *
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000209load_dynamic_module(name, pathname, fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000210 char *name;
211 char *pathname;
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000212 FILE *fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000213{
214#ifndef DYNAMIC_LINK
215 err_setstr(ImportError, "dynamically linked modules not supported");
216 return NULL;
217#else
218 object *m;
219 char funcname[258];
220 dl_funcptr p = NULL;
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000221#ifdef USE_SHLIB
222 static struct {
223 dev_t dev;
224 ino_t ino;
225 void *handle;
226 } handles[128];
227 static int nhandles = 0;
228#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000229 sprintf(funcname, FUNCNAME_PATTERN, name);
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000230#ifdef USE_SHLIB
231 if (fp != NULL) {
232 int i;
233 struct stat statb;
234 fstat(fileno(fp), &statb);
235 for (i = 0; i < nhandles; i++) {
236 if (statb.st_dev == handles[i].dev &&
237 statb.st_ino == handles[i].ino) {
238 p = (dl_funcptr) dlsym(handles[i].handle,
239 funcname);
240 goto got_it;
241 }
242 }
243 if (nhandles < 128) {
244 handles[nhandles].dev = statb.st_dev;
245 handles[nhandles].ino = statb.st_ino;
246 }
247 }
248#endif /* USE_SHLIB */
Jack Jansen4e043731995-02-13 22:42:34 +0000249#ifdef USE_MAC_SHARED_LIBRARY
Guido van Rossum6a75d261995-02-18 14:51:15 +0000250 /* Dynamic loading of CFM shared libraries on the Mac */
Jack Jansen4e043731995-02-13 22:42:34 +0000251 {
252 FSSpec libspec;
253 CFragConnectionID connID;
Guido van Rossum6a75d261995-02-18 14:51:15 +0000254 Ptr mainAddr;
255 Str255 errMessage;
256 OSErr err;
Jack Jansen4e043731995-02-13 22:42:34 +0000257
258 (void)FSMakeFSSpec(0, 0, Pstring(pathname), &libspec);
Guido van Rossum6a75d261995-02-18 14:51:15 +0000259 err = GetDiskFragment(&libspec, 0, 0, Pstring(name),
260 kLoadCFrag, &connID, &mainAddr,
261 errMessage);
Jack Jansen4e043731995-02-13 22:42:34 +0000262 if ( err ) {
263 char buf[512];
264
Guido van Rossum3097c3a1995-02-21 21:02:46 +0000265 sprintf(buf, "%.*s: %s", errMessage[0], errMessage+1, PyMac_StrError(err));
Jack Jansen4e043731995-02-13 22:42:34 +0000266 err_setstr(ImportError, buf);
267 return NULL;
268 }
269 p = (dl_funcptr)mainAddr;
270 }
271#endif /* USE_MAC_SHARED_LIBRARY */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000272#ifdef USE_SHLIB
273 {
274#ifdef RTLD_NOW
275 /* RTLD_NOW: resolve externals now
276 (i.e. core dump now if some are missing) */
277 void *handle = dlopen(pathname, RTLD_NOW);
278#else
279 void *handle;
280 if (verbose)
281 printf("dlopen(\"%s\", %d);\n", pathname, RTLD_LAZY);
282 handle = dlopen(pathname, RTLD_LAZY);
283#endif /* RTLD_NOW */
284 if (handle == NULL) {
285 err_setstr(ImportError, dlerror());
286 return NULL;
287 }
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000288 if (fp != NULL && nhandles < 128)
289 handles[nhandles++].handle = handle;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000290 p = (dl_funcptr) dlsym(handle, funcname);
291 }
292#endif /* USE_SHLIB */
293#ifdef _AIX
294 p = (dl_funcptr) load(pathname, 1, 0);
295 if (p == NULL) {
296 aix_loaderror(pathname);
297 return NULL;
298 }
299#endif /* _AIX */
300#ifdef NT
301 {
302 HINSTANCE hDLL;
303 hDLL = LoadLibrary(pathname);
304 if (hDLL==NULL){
305 char errBuf[64];
306 sprintf(errBuf, "DLL load failed with error code %d",
307 GetLastError());
308 err_setstr(ImportError, errBuf);
309 return NULL;
310 }
311 p = GetProcAddress(hDLL, funcname);
312 }
313#endif /* NT */
314#ifdef USE_DL
315 p = dl_loadmod(getprogramname(), pathname, funcname);
316#endif /* USE_DL */
317#ifdef USE_RLD
318 {
319 NXStream *errorStream;
320 struct mach_header *new_header;
321 const char *filenames[2];
322 long ret;
323 unsigned long ptr;
324
325 errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
326 filenames[0] = pathname;
327 filenames[1] = NULL;
328 ret = rld_load(errorStream, &new_header,
329 filenames, NULL);
330
331 /* extract the error messages for the exception */
332 if(!ret) {
333 char *streamBuf;
334 int len, maxLen;
335
336 NXPutc(errorStream, (char)0);
337
338 NXGetMemoryBuffer(errorStream,
339 &streamBuf, &len, &maxLen);
340 err_setstr(ImportError, streamBuf);
341 }
342
343 if(ret && rld_lookup(errorStream, funcname, &ptr))
344 p = (dl_funcptr) ptr;
345
346 NXCloseMemory(errorStream, NX_FREEBUFFER);
347
348 if(!ret)
349 return NULL;
350 }
351#endif /* USE_RLD */
352#ifdef hpux
353 {
354 shl_t lib;
355 int flags;
356
357 flags = BIND_DEFERRED;
358 if (verbose)
359 {
360 flags = BIND_IMMEDIATE | BIND_NONFATAL | BIND_VERBOSE;
361 printf("shl_load %s\n",pathname);
362 }
363 lib = shl_load(pathname, flags, 0);
364 if (lib == NULL)
365 {
366 char buf[256];
367 if (verbose)
368 perror(pathname);
369 sprintf(buf, "Failed to load %.200s", pathname);
370 err_setstr(ImportError, buf);
371 return NULL;
372 }
373 if (verbose)
374 printf("shl_findsym %s\n", funcname);
375 shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p);
376 if (p == NULL && verbose)
377 perror(funcname);
378 }
379#endif /* hpux */
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000380 got_it:
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000381 if (p == NULL) {
382 err_setstr(ImportError,
383 "dynamic module does not define init function");
384 return NULL;
385 }
386 (*p)();
387
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000388 m = dictlookup(import_modules, name);
389 if (m == NULL) {
390 if (err_occurred() == NULL)
391 err_setstr(SystemError,
392 "dynamic module not initialized properly");
393 return NULL;
394 }
395 if (verbose)
396 fprintf(stderr,
397 "import %s # dynamically loaded from %s\n",
398 name, pathname);
399 INCREF(m);
400 return m;
401#endif /* DYNAMIC_LINK */
402}
403
404
405#ifdef _AIX
406
407#include <ctype.h> /* for isdigit() */
408#include <errno.h> /* for global errno */
409#include <string.h> /* for strerror() */
410
411void aix_loaderror(char *pathname)
412{
413
414 char *message[8], errbuf[1024];
415 int i,j;
416
417 struct errtab {
418 int errno;
419 char *errstr;
420 } load_errtab[] = {
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000421 {L_ERROR_TOOMANY, "too many errors, rest skipped."},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000422 {L_ERROR_NOLIB, "can't load library:"},
423 {L_ERROR_UNDEF, "can't find symbol in library:"},
424 {L_ERROR_RLDBAD,
425 "RLD index out of range or bad relocation type:"},
426 {L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
427 {L_ERROR_MEMBER,
428 "file not an archive or does not contain requested member:"},
429 {L_ERROR_TYPE, "symbol table mismatch:"},
430 {L_ERROR_ALIGN, "text allignment in file is wrong."},
431 {L_ERROR_SYSTEM, "System error:"},
432 {L_ERROR_ERRNO, NULL}
433 };
434
435#define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0]))
436#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
437
438 sprintf(errbuf, " from module %.200s ", pathname);
439
440 if (!loadquery(1, &message[0], sizeof(message)))
441 ERRBUF_APPEND(strerror(errno));
442 for(i = 0; message[i] && *message[i]; i++) {
443 int nerr = atoi(message[i]);
444 for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
445 if (nerr == load_errtab[i].errno && load_errtab[i].errstr)
446 ERRBUF_APPEND(load_errtab[i].errstr);
447 }
448 while (isdigit(*message[i])) message[i]++ ;
449 ERRBUF_APPEND(message[i]);
450 ERRBUF_APPEND("\n");
451 }
452 errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
453 err_setstr(ImportError, errbuf);
454 return;
455}
456
457#endif /* _AIX */