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" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 5 | #include "pycore_pystate.h" |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 6 | #include "importdl.h" |
| 7 | |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
Martin v. Löwis | 8a57f00 | 2001-10-18 21:24:04 +0000 | [diff] [blame] | 10 | |
Martin v. Löwis | 0eb1ed5 | 2001-10-18 11:45:19 +0000 | [diff] [blame] | 11 | #if defined(__NetBSD__) |
| 12 | #include <sys/param.h> |
| 13 | #if (NetBSD < 199712) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 14 | #include <nlist.h> |
| 15 | #include <link.h> |
| 16 | #define dlerror() "error in dynamic linking" |
Martin v. Löwis | 8a57f00 | 2001-10-18 21:24:04 +0000 | [diff] [blame] | 17 | #endif |
| 18 | #endif /* NetBSD */ |
| 19 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 20 | #ifdef HAVE_DLFCN_H |
| 21 | #include <dlfcn.h> |
| 22 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 23 | |
Martin v. Löwis | 0eb1ed5 | 2001-10-18 11:45:19 +0000 | [diff] [blame] | 24 | #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__) |
Guido van Rossum | c8fcdcb | 2000-10-25 22:07:45 +0000 | [diff] [blame] | 25 | #define LEAD_UNDERSCORE "_" |
| 26 | #else |
| 27 | #define LEAD_UNDERSCORE "" |
| 28 | #endif |
| 29 | |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 30 | /* The .so extension module ABI tag, supplied by the Makefile via |
| 31 | Makefile.pre.in and configure. This is used to discriminate between |
| 32 | incompatible .so files so that extensions for different Python builds can |
| 33 | live in the same directory. E.g. foomodule.cpython-32.so |
| 34 | */ |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 35 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 36 | const char *_PyImport_DynLoadFiletab[] = { |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 37 | #ifdef __CYGWIN__ |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 38 | ".dll", |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 39 | #else /* !__CYGWIN__ */ |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 40 | "." SOABI ".so", |
Stefano Rivera | 338d54f | 2018-11-16 15:52:52 -0800 | [diff] [blame] | 41 | #ifndef Py_DEBUG |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 42 | ".abi" PYTHON_ABI_STRING ".so", |
Stefano Rivera | 338d54f | 2018-11-16 15:52:52 -0800 | [diff] [blame] | 43 | #endif /* ! Py_DEBUG */ |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 44 | ".so", |
Barry Warsaw | 35f3a2c | 2010-09-03 18:30:30 +0000 | [diff] [blame] | 45 | #endif /* __CYGWIN__ */ |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 46 | NULL, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | dev_t dev; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | ino_t ino; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | void *handle; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 53 | } handles[128]; |
| 54 | static int nhandles = 0; |
| 55 | |
| 56 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 57 | dl_funcptr |
| 58 | _PyImport_FindSharedFuncptr(const char *prefix, |
| 59 | const char *shortname, |
| 60 | const char *pathname, FILE *fp) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 61 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | dl_funcptr p; |
| 63 | void *handle; |
| 64 | char funcname[258]; |
| 65 | char pathbuf[260]; |
| 66 | int dlopenflags=0; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 67 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | if (strchr(pathname, '/') == NULL) { |
| 69 | /* Prefix bare filename with "./" */ |
| 70 | PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname); |
| 71 | pathname = pathbuf; |
| 72 | } |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | PyOS_snprintf(funcname, sizeof(funcname), |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 75 | LEAD_UNDERSCORE "%.20s_%.200s", prefix, shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 76 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | if (fp != NULL) { |
| 78 | int i; |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 79 | struct _Py_stat_struct status; |
| 80 | if (_Py_fstat(fileno(fp), &status) == -1) |
Benjamin Peterson | 7d28b6b | 2013-07-22 22:08:09 -0700 | [diff] [blame] | 81 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | for (i = 0; i < nhandles; i++) { |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 83 | if (status.st_dev == handles[i].dev && |
| 84 | status.st_ino == handles[i].ino) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | p = (dl_funcptr) dlsym(handles[i].handle, |
| 86 | funcname); |
| 87 | return p; |
| 88 | } |
| 89 | } |
| 90 | if (nhandles < 128) { |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 91 | handles[nhandles].dev = status.st_dev; |
| 92 | handles[nhandles].ino = status.st_ino; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 95 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 96 | dlopenflags = _PyInterpreterState_Get()->dlopenflags; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 97 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | handle = dlopen(pathname, dlopenflags); |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 99 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | if (handle == NULL) { |
Victor Stinner | 3b635cd | 2012-10-02 00:55:07 +0200 | [diff] [blame] | 101 | PyObject *mod_name; |
| 102 | PyObject *path; |
| 103 | PyObject *error_ob; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | const char *error = dlerror(); |
| 105 | if (error == NULL) |
| 106 | error = "unknown dlopen() error"; |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 107 | error_ob = PyUnicode_FromString(error); |
Victor Stinner | 3b635cd | 2012-10-02 00:55:07 +0200 | [diff] [blame] | 108 | if (error_ob == NULL) |
| 109 | return NULL; |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 110 | mod_name = PyUnicode_FromString(shortname); |
Victor Stinner | 3b635cd | 2012-10-02 00:55:07 +0200 | [diff] [blame] | 111 | if (mod_name == NULL) { |
| 112 | Py_DECREF(error_ob); |
| 113 | return NULL; |
| 114 | } |
| 115 | path = PyUnicode_FromString(pathname); |
| 116 | if (path == NULL) { |
| 117 | Py_DECREF(error_ob); |
| 118 | Py_DECREF(mod_name); |
| 119 | return NULL; |
| 120 | } |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 121 | PyErr_SetImportError(error_ob, mod_name, path); |
Victor Stinner | 3b635cd | 2012-10-02 00:55:07 +0200 | [diff] [blame] | 122 | Py_DECREF(error_ob); |
| 123 | Py_DECREF(mod_name); |
| 124 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | return NULL; |
| 126 | } |
| 127 | if (fp != NULL && nhandles < 128) |
| 128 | handles[nhandles++].handle = handle; |
| 129 | p = (dl_funcptr) dlsym(handle, funcname); |
| 130 | return p; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 131 | } |