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 | |
| 39 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 40 | #ifdef __CYGWIN__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | {".dll", "rb", C_EXTENSION}, |
| 42 | {"module.dll", "rb", C_EXTENSION}, |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 43 | #else /* !__CYGWIN__ */ |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 44 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | {".pyd", "rb", C_EXTENSION}, |
| 46 | {".dll", "rb", C_EXTENSION}, |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 47 | #else /* !(defined(PYOS_OS2) && defined(PYCC_GCC)) */ |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 48 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | {".exe", "rb", C_EXTENSION}, |
| 50 | {".EXE", "rb", C_EXTENSION}, |
| 51 | {"module.exe", "rb", C_EXTENSION}, |
| 52 | {"MODULE.EXE", "rb", C_EXTENSION}, |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 53 | #else /* !__VMS */ |
| 54 | {"." SOABI ".so", "rb", C_EXTENSION}, |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 55 | {"module." SOABI ".so", "rb", C_EXTENSION}, |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 56 | {".abi" PYTHON_ABI_STRING ".so", "rb", C_EXTENSION}, |
| 57 | {"module.abi" PYTHON_ABI_STRING ".so", "rb", C_EXTENSION}, |
Matthias Klose | f69af1e | 2010-09-08 16:22:10 +0000 | [diff] [blame] | 58 | {".so", "rb", C_EXTENSION}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | {"module.so", "rb", C_EXTENSION}, |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 60 | #endif /* __VMS */ |
| 61 | #endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */ |
| 62 | #endif /* __CYGWIN__ */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | {0, 0} |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | dev_t dev; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 68 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | ino_t ino[3]; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 70 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | ino_t ino; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 72 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | void *handle; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 74 | } handles[128]; |
| 75 | static int nhandles = 0; |
| 76 | |
| 77 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 78 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | const char *pathname, FILE *fp) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 80 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | dl_funcptr p; |
| 82 | void *handle; |
| 83 | char funcname[258]; |
| 84 | char pathbuf[260]; |
| 85 | int dlopenflags=0; |
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 | if (strchr(pathname, '/') == NULL) { |
| 88 | /* Prefix bare filename with "./" */ |
| 89 | PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname); |
| 90 | pathname = pathbuf; |
| 91 | } |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 92 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | PyOS_snprintf(funcname, sizeof(funcname), |
| 94 | LEAD_UNDERSCORE "PyInit_%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | if (fp != NULL) { |
| 97 | int i; |
| 98 | struct stat statb; |
| 99 | fstat(fileno(fp), &statb); |
| 100 | for (i = 0; i < nhandles; i++) { |
| 101 | if (statb.st_dev == handles[i].dev && |
| 102 | statb.st_ino == handles[i].ino) { |
| 103 | p = (dl_funcptr) dlsym(handles[i].handle, |
| 104 | funcname); |
| 105 | return p; |
| 106 | } |
| 107 | } |
| 108 | if (nhandles < 128) { |
| 109 | handles[nhandles].dev = statb.st_dev; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 110 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | handles[nhandles].ino[0] = statb.st_ino[0]; |
| 112 | handles[nhandles].ino[1] = statb.st_ino[1]; |
| 113 | handles[nhandles].ino[2] = statb.st_ino[2]; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 114 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | handles[nhandles].ino = statb.st_ino; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 116 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 119 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 120 | #if !(defined(PYOS_OS2) && defined(PYCC_GCC)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | dlopenflags = PyThreadState_GET()->interp->dlopenflags; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 122 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | if (Py_VerboseFlag) |
| 125 | PySys_WriteStderr("dlopen(\"%s\", %x);\n", pathname, |
| 126 | dlopenflags); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 127 | |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 128 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | /* VMS currently don't allow a pathname, use a logical name instead */ |
| 130 | /* Concatenate 'python_module_' and shortname */ |
| 131 | /* so "import vms.bar" will use the logical python_module_bar */ |
| 132 | /* As C module use only one name space this is probably not a */ |
| 133 | /* important limitation */ |
| 134 | PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s", |
| 135 | shortname); |
| 136 | pathname = pathbuf; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 137 | #endif |
| 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | handle = dlopen(pathname, dlopenflags); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | if (handle == NULL) { |
| 142 | const char *error = dlerror(); |
| 143 | if (error == NULL) |
| 144 | error = "unknown dlopen() error"; |
| 145 | PyErr_SetString(PyExc_ImportError, error); |
| 146 | return NULL; |
| 147 | } |
| 148 | if (fp != NULL && nhandles < 128) |
| 149 | handles[nhandles++].handle = handle; |
| 150 | p = (dl_funcptr) dlsym(handle, funcname); |
| 151 | return p; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 152 | } |