blob: 569a6f52ce837df975174280199d5df4049d45cc [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001
2/* Support for dynamic loading of extension modules */
3
4#include "Python.h"
5#include "importdl.h"
6
7#include <sys/types.h>
8#include <sys/stat.h>
Martin v. Löwis8a57f002001-10-18 21:24:04 +00009
Martin v. Löwis0eb1ed52001-10-18 11:45:19 +000010#if defined(__NetBSD__)
11#include <sys/param.h>
12#if (NetBSD < 199712)
Guido van Rossum22a1d361999-12-20 21:18:49 +000013#include <nlist.h>
14#include <link.h>
15#define dlerror() "error in dynamic linking"
Martin v. Löwis8a57f002001-10-18 21:24:04 +000016#endif
17#endif /* NetBSD */
18
Guido van Rossum22a1d361999-12-20 21:18:49 +000019#ifdef HAVE_DLFCN_H
20#include <dlfcn.h>
21#endif
Guido van Rossum22a1d361999-12-20 21:18:49 +000022
Martin v. Löwis0eb1ed52001-10-18 11:45:19 +000023#if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
Guido van Rossumc8fcdcb2000-10-25 22:07:45 +000024#define LEAD_UNDERSCORE "_"
25#else
26#define LEAD_UNDERSCORE ""
27#endif
28
Guido van Rossum22a1d361999-12-20 21:18:49 +000029
30const struct filedescr _PyImport_DynLoadFiletab[] = {
Tim Peters98dc0652000-10-05 19:24:26 +000031#ifdef __CYGWIN__
Tim Peters98dc0652000-10-05 19:24:26 +000032 {".dll", "rb", C_EXTENSION},
Guido van Rossumfef12432001-01-10 21:17:27 +000033 {"module.dll", "rb", C_EXTENSION},
Tim Peters98dc0652000-10-05 19:24:26 +000034#else
Guido van Rossum22a1d361999-12-20 21:18:49 +000035 {".so", "rb", C_EXTENSION},
36 {"module.so", "rb", C_EXTENSION},
Tim Peters98dc0652000-10-05 19:24:26 +000037#endif
Guido van Rossum22a1d361999-12-20 21:18:49 +000038 {0, 0}
39};
40
41static struct {
42 dev_t dev;
43 ino_t ino;
44 void *handle;
45} handles[128];
46static int nhandles = 0;
47
48
Guido van Rossum96a8fb71999-12-22 14:09:35 +000049dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +000050 const char *pathname, FILE *fp)
51{
52 dl_funcptr p;
53 void *handle;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000054 char funcname[258];
55 char pathbuf[260];
Martin v. Löwisf0473d52001-07-18 16:17:16 +000056 int dlopenflags=0;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000057
58 if (strchr(pathname, '/') == NULL) {
59 /* Prefix bare filename with "./" */
60 sprintf(pathbuf, "./%-.255s", pathname);
61 pathname = pathbuf;
62 }
63
Guido van Rossumc8fcdcb2000-10-25 22:07:45 +000064 sprintf(funcname, LEAD_UNDERSCORE "init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000065
66 if (fp != NULL) {
67 int i;
68 struct stat statb;
69 fstat(fileno(fp), &statb);
70 for (i = 0; i < nhandles; i++) {
71 if (statb.st_dev == handles[i].dev &&
72 statb.st_ino == handles[i].ino) {
73 p = (dl_funcptr) dlsym(handles[i].handle,
74 funcname);
75 return p;
76 }
77 }
78 if (nhandles < 128) {
79 handles[nhandles].dev = statb.st_dev;
80 handles[nhandles].ino = statb.st_ino;
81 }
82 }
83
Martin v. Löwisf0473d52001-07-18 16:17:16 +000084 dlopenflags = PyThreadState_Get()->interp->dlopenflags;
85
Guido van Rossum22a1d361999-12-20 21:18:49 +000086 if (Py_VerboseFlag)
Martin v. Löwisf0473d52001-07-18 16:17:16 +000087 printf("dlopen(\"%s\", %x);\n", pathname, dlopenflags);
88
89 handle = dlopen(pathname, dlopenflags);
90
Guido van Rossum22a1d361999-12-20 21:18:49 +000091 if (handle == NULL) {
92 PyErr_SetString(PyExc_ImportError, dlerror());
93 return NULL;
94 }
95 if (fp != NULL && nhandles < 128)
96 handles[nhandles++].handle = handle;
97 p = (dl_funcptr) dlsym(handle, funcname);
98 return p;
99}