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}, |
Matthias Klose | f69af1e | 2010-09-08 16:22:10 +0000 | [diff] [blame^] | 56 | {".so", "rb", C_EXTENSION}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | {"module.so", "rb", C_EXTENSION}, |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 58 | #endif /* __VMS */ |
| 59 | #endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */ |
| 60 | #endif /* __CYGWIN__ */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | {0, 0} |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | dev_t dev; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 66 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | ino_t ino[3]; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 68 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | ino_t ino; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 70 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | void *handle; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 72 | } handles[128]; |
| 73 | static int nhandles = 0; |
| 74 | |
| 75 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 76 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | const char *pathname, FILE *fp) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 78 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | dl_funcptr p; |
| 80 | void *handle; |
| 81 | char funcname[258]; |
| 82 | char pathbuf[260]; |
| 83 | int dlopenflags=0; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 84 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | if (strchr(pathname, '/') == NULL) { |
| 86 | /* Prefix bare filename with "./" */ |
| 87 | PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname); |
| 88 | pathname = pathbuf; |
| 89 | } |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 90 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | PyOS_snprintf(funcname, sizeof(funcname), |
| 92 | LEAD_UNDERSCORE "PyInit_%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 93 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | 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öwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 108 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | 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öwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 112 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | handles[nhandles].ino = statb.st_ino; |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 114 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 117 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 118 | #if !(defined(PYOS_OS2) && defined(PYCC_GCC)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | dlopenflags = PyThreadState_GET()->interp->dlopenflags; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 120 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | if (Py_VerboseFlag) |
| 123 | PySys_WriteStderr("dlopen(\"%s\", %x);\n", pathname, |
| 124 | dlopenflags); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 125 | |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 126 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | /* 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öwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | handle = dlopen(pathname, dlopenflags); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | 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 Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 150 | } |