blob: 3d0fb5eb01afc9c240ec4fd1fb2a169e85cf9a6f [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>
Andrew MacIntyred9400542002-02-26 11:41:34 +000021#else
22#if defined(PYOS_OS2) && defined(PYCC_GCC)
23#include "dlfcn.h"
24#endif
Guido van Rossum22a1d361999-12-20 21:18:49 +000025#endif
Guido van Rossum22a1d361999-12-20 21:18:49 +000026
Martin v. Löwis0eb1ed52001-10-18 11:45:19 +000027#if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
Guido van Rossumc8fcdcb2000-10-25 22:07:45 +000028#define LEAD_UNDERSCORE "_"
29#else
30#define LEAD_UNDERSCORE ""
31#endif
32
Barry Warsaw35f3a2c2010-09-03 18:30:30 +000033/* The .so extension module ABI tag, supplied by the Makefile via
34 Makefile.pre.in and configure. This is used to discriminate between
35 incompatible .so files so that extensions for different Python builds can
36 live in the same directory. E.g. foomodule.cpython-32.so
37*/
Guido van Rossum22a1d361999-12-20 21:18:49 +000038
39const struct filedescr _PyImport_DynLoadFiletab[] = {
Tim Peters98dc0652000-10-05 19:24:26 +000040#ifdef __CYGWIN__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 {".dll", "rb", C_EXTENSION},
42 {"module.dll", "rb", C_EXTENSION},
Barry Warsaw35f3a2c2010-09-03 18:30:30 +000043#else /* !__CYGWIN__ */
Andrew MacIntyred9400542002-02-26 11:41:34 +000044#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 {".pyd", "rb", C_EXTENSION},
46 {".dll", "rb", C_EXTENSION},
Barry Warsaw35f3a2c2010-09-03 18:30:30 +000047#else /* !(defined(PYOS_OS2) && defined(PYCC_GCC)) */
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000048#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 {".exe", "rb", C_EXTENSION},
50 {".EXE", "rb", C_EXTENSION},
51 {"module.exe", "rb", C_EXTENSION},
52 {"MODULE.EXE", "rb", C_EXTENSION},
Barry Warsaw35f3a2c2010-09-03 18:30:30 +000053#else /* !__VMS */
54 {"." SOABI ".so", "rb", C_EXTENSION},
Barry Warsaw35f3a2c2010-09-03 18:30:30 +000055 {"module." SOABI ".so", "rb", C_EXTENSION},
Matthias Klosef69af1e2010-09-08 16:22:10 +000056 {".so", "rb", C_EXTENSION},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 {"module.so", "rb", C_EXTENSION},
Barry Warsaw35f3a2c2010-09-03 18:30:30 +000058#endif /* __VMS */
59#endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */
60#endif /* __CYGWIN__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 {0, 0}
Guido van Rossum22a1d361999-12-20 21:18:49 +000062};
63
64static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 dev_t dev;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000066#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 ino_t ino[3];
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000068#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 ino_t ino;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000070#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 void *handle;
Guido van Rossum22a1d361999-12-20 21:18:49 +000072} handles[128];
73static int nhandles = 0;
74
75
Guido van Rossum96a8fb71999-12-22 14:09:35 +000076dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 const char *pathname, FILE *fp)
Guido van Rossum22a1d361999-12-20 21:18:49 +000078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 dl_funcptr p;
80 void *handle;
81 char funcname[258];
82 char pathbuf[260];
83 int dlopenflags=0;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (strchr(pathname, '/') == NULL) {
86 /* Prefix bare filename with "./" */
87 PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
88 pathname = pathbuf;
89 }
Guido van Rossum96a8fb71999-12-22 14:09:35 +000090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 PyOS_snprintf(funcname, sizeof(funcname),
92 LEAD_UNDERSCORE "PyInit_%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 if (fp != NULL) {
95 int i;
96 struct stat statb;
97 fstat(fileno(fp), &statb);
98 for (i = 0; i < nhandles; i++) {
99 if (statb.st_dev == handles[i].dev &&
100 statb.st_ino == handles[i].ino) {
101 p = (dl_funcptr) dlsym(handles[i].handle,
102 funcname);
103 return p;
104 }
105 }
106 if (nhandles < 128) {
107 handles[nhandles].dev = statb.st_dev;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000108#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 handles[nhandles].ino[0] = statb.st_ino[0];
110 handles[nhandles].ino[1] = statb.st_ino[1];
111 handles[nhandles].ino[2] = statb.st_ino[2];
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000112#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 handles[nhandles].ino = statb.st_ino;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000114#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 }
116 }
Guido van Rossum22a1d361999-12-20 21:18:49 +0000117
Andrew MacIntyred9400542002-02-26 11:41:34 +0000118#if !(defined(PYOS_OS2) && defined(PYCC_GCC))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 dlopenflags = PyThreadState_GET()->interp->dlopenflags;
Andrew MacIntyred9400542002-02-26 11:41:34 +0000120#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 if (Py_VerboseFlag)
123 PySys_WriteStderr("dlopen(\"%s\", %x);\n", pathname,
124 dlopenflags);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000125
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000126#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 /* VMS currently don't allow a pathname, use a logical name instead */
128 /* Concatenate 'python_module_' and shortname */
129 /* so "import vms.bar" will use the logical python_module_bar */
130 /* As C module use only one name space this is probably not a */
131 /* important limitation */
132 PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s",
133 shortname);
134 pathname = pathbuf;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000135#endif
136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 handle = dlopen(pathname, dlopenflags);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 if (handle == NULL) {
140 const char *error = dlerror();
141 if (error == NULL)
142 error = "unknown dlopen() error";
143 PyErr_SetString(PyExc_ImportError, error);
144 return NULL;
145 }
146 if (fp != NULL && nhandles < 128)
147 handles[nhandles++].handle = handle;
148 p = (dl_funcptr) dlsym(handle, funcname);
149 return p;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000150}