Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 1 | |
| 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öwis | 8a57f00 | 2001-10-18 21:24:04 +0000 | [diff] [blame] | 9 | |
Martin v. Löwis | 0eb1ed5 | 2001-10-18 11:45:19 +0000 | [diff] [blame] | 10 | #if defined(__NetBSD__) |
| 11 | #include <sys/param.h> |
| 12 | #if (NetBSD < 199712) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 13 | #include <nlist.h> |
| 14 | #include <link.h> |
| 15 | #define dlerror() "error in dynamic linking" |
Martin v. Löwis | 8a57f00 | 2001-10-18 21:24:04 +0000 | [diff] [blame] | 16 | #endif |
| 17 | #endif /* NetBSD */ |
| 18 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 19 | #ifdef HAVE_DLFCN_H |
| 20 | #include <dlfcn.h> |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 21 | #else |
| 22 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 23 | #include "dlfcn.h" |
| 24 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 25 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 26 | |
Martin v. Löwis | 0eb1ed5 | 2001-10-18 11:45:19 +0000 | [diff] [blame] | 27 | #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__) |
Guido van Rossum | c8fcdcb | 2000-10-25 22:07:45 +0000 | [diff] [blame] | 28 | #define LEAD_UNDERSCORE "_" |
| 29 | #else |
| 30 | #define LEAD_UNDERSCORE "" |
| 31 | #endif |
| 32 | |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 33 | /* 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 Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 38 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 39 | const char *_PyImport_DynLoadFiletab[] = { |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 40 | #ifdef __CYGWIN__ |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 41 | ".dll", |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 42 | #else /* !__CYGWIN__ */ |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 43 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 44 | ".pyd", |
| 45 | ".dll", |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 46 | #else /* !(defined(PYOS_OS2) && defined(PYCC_GCC)) */ |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 47 | #ifdef __VMS |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 48 | ".exe", |
| 49 | ".EXE", |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 50 | #else /* !__VMS */ |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 51 | "." SOABI ".so", |
| 52 | ".abi" PYTHON_ABI_STRING ".so", |
| 53 | ".so", |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 54 | #endif /* __VMS */ |
| 55 | #endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */ |
| 56 | #endif /* __CYGWIN__ */ |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 57 | NULL, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | dev_t dev; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 62 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | ino_t ino[3]; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 64 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | ino_t ino; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 66 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | void *handle; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 68 | } handles[128]; |
| 69 | static int nhandles = 0; |
| 70 | |
| 71 | |
Victor Stinner | 42040fb | 2011-02-22 23:16:19 +0000 | [diff] [blame] | 72 | dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | const char *pathname, FILE *fp) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 74 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | dl_funcptr p; |
| 76 | void *handle; |
| 77 | char funcname[258]; |
| 78 | char pathbuf[260]; |
| 79 | int dlopenflags=0; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | if (strchr(pathname, '/') == NULL) { |
| 82 | /* Prefix bare filename with "./" */ |
| 83 | PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname); |
| 84 | pathname = pathbuf; |
| 85 | } |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 86 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | PyOS_snprintf(funcname, sizeof(funcname), |
| 88 | LEAD_UNDERSCORE "PyInit_%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 89 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | 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öwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 104 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | 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öwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 108 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | handles[nhandles].ino = statb.st_ino; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 110 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 113 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 114 | #if !(defined(PYOS_OS2) && defined(PYCC_GCC)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | dlopenflags = PyThreadState_GET()->interp->dlopenflags; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 116 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 117 | |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 118 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | /* 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öwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | handle = dlopen(pathname, dlopenflags); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | if (handle == NULL) { |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 132 | PyObject *mod_name = NULL; |
| 133 | PyObject *path = NULL; |
| 134 | PyObject *error_ob = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | const char *error = dlerror(); |
| 136 | if (error == NULL) |
| 137 | error = "unknown dlopen() error"; |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 138 | error_ob = PyUnicode_FromString(error); |
| 139 | path = PyUnicode_FromString(pathname); |
| 140 | mod_name = PyUnicode_FromString(shortname); |
| 141 | PyErr_SetImportError(error_ob, mod_name, path); |
Victor Stinner | ad54c6d | 2012-08-22 17:45:52 +0200 | [diff] [blame] | 142 | Py_XDECREF(error_ob); |
| 143 | Py_XDECREF(path); |
| 144 | Py_XDECREF(mod_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | 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 Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 151 | } |