blob: 548863a91708afc758788d119ea98bf8cc1d58f2 [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öwis0eb1ed52001-10-18 11:45:19 +00009#if defined(__NetBSD__)
10#include <sys/param.h>
11#if (NetBSD < 199712)
Guido van Rossum22a1d361999-12-20 21:18:49 +000012#include <nlist.h>
13#include <link.h>
14#define dlerror() "error in dynamic linking"
15#else
16#ifdef HAVE_DLFCN_H
17#include <dlfcn.h>
18#endif
19#endif
Martin v. Löwis0eb1ed52001-10-18 11:45:19 +000020#endif /* NetBSD */
Guido van Rossum22a1d361999-12-20 21:18:49 +000021
Martin v. Löwis0eb1ed52001-10-18 11:45:19 +000022#if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
Guido van Rossumc8fcdcb2000-10-25 22:07:45 +000023#define LEAD_UNDERSCORE "_"
24#else
25#define LEAD_UNDERSCORE ""
26#endif
27
Guido van Rossum22a1d361999-12-20 21:18:49 +000028
29const struct filedescr _PyImport_DynLoadFiletab[] = {
Tim Peters98dc0652000-10-05 19:24:26 +000030#ifdef __CYGWIN__
Tim Peters98dc0652000-10-05 19:24:26 +000031 {".dll", "rb", C_EXTENSION},
Guido van Rossumfef12432001-01-10 21:17:27 +000032 {"module.dll", "rb", C_EXTENSION},
Tim Peters98dc0652000-10-05 19:24:26 +000033#else
Guido van Rossum22a1d361999-12-20 21:18:49 +000034 {".so", "rb", C_EXTENSION},
35 {"module.so", "rb", C_EXTENSION},
Tim Peters98dc0652000-10-05 19:24:26 +000036#endif
Guido van Rossum22a1d361999-12-20 21:18:49 +000037 {0, 0}
38};
39
40static struct {
41 dev_t dev;
42 ino_t ino;
43 void *handle;
44} handles[128];
45static int nhandles = 0;
46
47
Guido van Rossum96a8fb71999-12-22 14:09:35 +000048dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +000049 const char *pathname, FILE *fp)
50{
51 dl_funcptr p;
52 void *handle;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000053 char funcname[258];
54 char pathbuf[260];
Martin v. Löwisf0473d52001-07-18 16:17:16 +000055 int dlopenflags=0;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000056
57 if (strchr(pathname, '/') == NULL) {
58 /* Prefix bare filename with "./" */
59 sprintf(pathbuf, "./%-.255s", pathname);
60 pathname = pathbuf;
61 }
62
Guido van Rossumc8fcdcb2000-10-25 22:07:45 +000063 sprintf(funcname, LEAD_UNDERSCORE "init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000064
65 if (fp != NULL) {
66 int i;
67 struct stat statb;
68 fstat(fileno(fp), &statb);
69 for (i = 0; i < nhandles; i++) {
70 if (statb.st_dev == handles[i].dev &&
71 statb.st_ino == handles[i].ino) {
72 p = (dl_funcptr) dlsym(handles[i].handle,
73 funcname);
74 return p;
75 }
76 }
77 if (nhandles < 128) {
78 handles[nhandles].dev = statb.st_dev;
79 handles[nhandles].ino = statb.st_ino;
80 }
81 }
82
Martin v. Löwisf0473d52001-07-18 16:17:16 +000083 dlopenflags = PyThreadState_Get()->interp->dlopenflags;
84
Guido van Rossum22a1d361999-12-20 21:18:49 +000085 if (Py_VerboseFlag)
Martin v. Löwisf0473d52001-07-18 16:17:16 +000086 printf("dlopen(\"%s\", %x);\n", pathname, dlopenflags);
87
88 handle = dlopen(pathname, dlopenflags);
89
Guido van Rossum22a1d361999-12-20 21:18:49 +000090 if (handle == NULL) {
91 PyErr_SetString(PyExc_ImportError, dlerror());
92 return NULL;
93 }
94 if (fp != NULL && nhandles < 128)
95 handles[nhandles++].handle = handle;
96 p = (dl_funcptr) dlsym(handle, funcname);
97 return p;
98}