blob: 565facaf7137ee69c2ec55922791d986f789246a [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},
Barry Warsaw35f3a2c2010-09-03 18:30:30 +000042#else /* !__CYGWIN__ */
Andrew MacIntyred9400542002-02-26 11:41:34 +000043#if defined(PYOS_OS2) && defined(PYCC_GCC)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 {".pyd", "rb", C_EXTENSION},
45 {".dll", "rb", C_EXTENSION},
Barry Warsaw35f3a2c2010-09-03 18:30:30 +000046#else /* !(defined(PYOS_OS2) && defined(PYCC_GCC)) */
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000047#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 {".exe", "rb", C_EXTENSION},
49 {".EXE", "rb", C_EXTENSION},
Barry Warsaw35f3a2c2010-09-03 18:30:30 +000050#else /* !__VMS */
51 {"." SOABI ".so", "rb", C_EXTENSION},
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000052 {".abi" PYTHON_ABI_STRING ".so", "rb", C_EXTENSION},
Matthias Klosef69af1e2010-09-08 16:22:10 +000053 {".so", "rb", C_EXTENSION},
Barry Warsaw35f3a2c2010-09-03 18:30:30 +000054#endif /* __VMS */
55#endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */
56#endif /* __CYGWIN__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 {0, 0}
Guido van Rossum22a1d361999-12-20 21:18:49 +000058};
59
60static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 dev_t dev;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000062#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 ino_t ino[3];
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000064#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 ino_t ino;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +000066#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 void *handle;
Guido van Rossum22a1d361999-12-20 21:18:49 +000068} handles[128];
69static int nhandles = 0;
70
71
Victor Stinner42040fb2011-02-22 23:16:19 +000072dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 const char *pathname, FILE *fp)
Guido van Rossum22a1d361999-12-20 21:18:49 +000074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 dl_funcptr p;
76 void *handle;
77 char funcname[258];
78 char pathbuf[260];
79 int dlopenflags=0;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (strchr(pathname, '/') == NULL) {
82 /* Prefix bare filename with "./" */
83 PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
84 pathname = pathbuf;
85 }
Guido van Rossum96a8fb71999-12-22 14:09:35 +000086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 PyOS_snprintf(funcname, sizeof(funcname),
88 LEAD_UNDERSCORE "PyInit_%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 if (fp != NULL) {
91 int i;
92 struct stat statb;
93 fstat(fileno(fp), &statb);
94 for (i = 0; i < nhandles; i++) {
95 if (statb.st_dev == handles[i].dev &&
96 statb.st_ino == handles[i].ino) {
97 p = (dl_funcptr) dlsym(handles[i].handle,
98 funcname);
99 return p;
100 }
101 }
102 if (nhandles < 128) {
103 handles[nhandles].dev = statb.st_dev;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000104#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 handles[nhandles].ino[0] = statb.st_ino[0];
106 handles[nhandles].ino[1] = statb.st_ino[1];
107 handles[nhandles].ino[2] = statb.st_ino[2];
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000108#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 handles[nhandles].ino = statb.st_ino;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000110#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 }
112 }
Guido van Rossum22a1d361999-12-20 21:18:49 +0000113
Andrew MacIntyred9400542002-02-26 11:41:34 +0000114#if !(defined(PYOS_OS2) && defined(PYCC_GCC))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 dlopenflags = PyThreadState_GET()->interp->dlopenflags;
Andrew MacIntyred9400542002-02-26 11:41:34 +0000116#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000117
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000118#ifdef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 /* VMS currently don't allow a pathname, use a logical name instead */
120 /* Concatenate 'python_module_' and shortname */
121 /* so "import vms.bar" will use the logical python_module_bar */
122 /* As C module use only one name space this is probably not a */
123 /* important limitation */
124 PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s",
125 shortname);
126 pathname = pathbuf;
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000127#endif
128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 handle = dlopen(pathname, dlopenflags);
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 if (handle == NULL) {
Brett Cannonf0434e62012-04-20 15:22:50 -0400132 PyObject *mod_name = NULL;
133 PyObject *path = NULL;
134 PyObject *error_ob = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 const char *error = dlerror();
136 if (error == NULL)
137 error = "unknown dlopen() error";
Brett Cannonf0434e62012-04-20 15:22:50 -0400138 error_ob = PyUnicode_FromString(error);
139 path = PyUnicode_FromString(pathname);
140 mod_name = PyUnicode_FromString(shortname);
141 PyErr_SetImportError(error_ob, mod_name, path);
142 Py_DECREF(error_ob);
143 Py_DECREF(path);
144 Py_DECREF(mod_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return NULL;
146 }
147 if (fp != NULL && nhandles < 128)
148 handles[nhandles++].handle = handle;
149 p = (dl_funcptr) dlsym(handle, funcname);
150 return p;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000151}