| 1 | n/a | |
|---|
| 2 | n/a | /* Support for dynamic loading of extension modules */ |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | #include "Python.h" |
|---|
| 5 | n/a | #include "importdl.h" |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | #include <sys/types.h> |
|---|
| 8 | n/a | #include <sys/stat.h> |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | #if defined(__NetBSD__) |
|---|
| 11 | n/a | #include <sys/param.h> |
|---|
| 12 | n/a | #if (NetBSD < 199712) |
|---|
| 13 | n/a | #include <nlist.h> |
|---|
| 14 | n/a | #include <link.h> |
|---|
| 15 | n/a | #define dlerror() "error in dynamic linking" |
|---|
| 16 | n/a | #endif |
|---|
| 17 | n/a | #endif /* NetBSD */ |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | #ifdef HAVE_DLFCN_H |
|---|
| 20 | n/a | #include <dlfcn.h> |
|---|
| 21 | n/a | #endif |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__) |
|---|
| 24 | n/a | #define LEAD_UNDERSCORE "_" |
|---|
| 25 | n/a | #else |
|---|
| 26 | n/a | #define LEAD_UNDERSCORE "" |
|---|
| 27 | n/a | #endif |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | /* The .so extension module ABI tag, supplied by the Makefile via |
|---|
| 30 | n/a | Makefile.pre.in and configure. This is used to discriminate between |
|---|
| 31 | n/a | incompatible .so files so that extensions for different Python builds can |
|---|
| 32 | n/a | live in the same directory. E.g. foomodule.cpython-32.so |
|---|
| 33 | n/a | */ |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | const char *_PyImport_DynLoadFiletab[] = { |
|---|
| 36 | n/a | #ifdef __CYGWIN__ |
|---|
| 37 | n/a | ".dll", |
|---|
| 38 | n/a | #else /* !__CYGWIN__ */ |
|---|
| 39 | n/a | "." SOABI ".so", |
|---|
| 40 | n/a | ".abi" PYTHON_ABI_STRING ".so", |
|---|
| 41 | n/a | ".so", |
|---|
| 42 | n/a | #endif /* __CYGWIN__ */ |
|---|
| 43 | n/a | NULL, |
|---|
| 44 | n/a | }; |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | static struct { |
|---|
| 47 | n/a | dev_t dev; |
|---|
| 48 | n/a | ino_t ino; |
|---|
| 49 | n/a | void *handle; |
|---|
| 50 | n/a | } handles[128]; |
|---|
| 51 | n/a | static int nhandles = 0; |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | dl_funcptr |
|---|
| 55 | n/a | _PyImport_FindSharedFuncptr(const char *prefix, |
|---|
| 56 | n/a | const char *shortname, |
|---|
| 57 | n/a | const char *pathname, FILE *fp) |
|---|
| 58 | n/a | { |
|---|
| 59 | n/a | dl_funcptr p; |
|---|
| 60 | n/a | void *handle; |
|---|
| 61 | n/a | char funcname[258]; |
|---|
| 62 | n/a | char pathbuf[260]; |
|---|
| 63 | n/a | int dlopenflags=0; |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | if (strchr(pathname, '/') == NULL) { |
|---|
| 66 | n/a | /* Prefix bare filename with "./" */ |
|---|
| 67 | n/a | PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname); |
|---|
| 68 | n/a | pathname = pathbuf; |
|---|
| 69 | n/a | } |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | PyOS_snprintf(funcname, sizeof(funcname), |
|---|
| 72 | n/a | LEAD_UNDERSCORE "%.20s_%.200s", prefix, shortname); |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | if (fp != NULL) { |
|---|
| 75 | n/a | int i; |
|---|
| 76 | n/a | struct _Py_stat_struct status; |
|---|
| 77 | n/a | if (_Py_fstat(fileno(fp), &status) == -1) |
|---|
| 78 | n/a | return NULL; |
|---|
| 79 | n/a | for (i = 0; i < nhandles; i++) { |
|---|
| 80 | n/a | if (status.st_dev == handles[i].dev && |
|---|
| 81 | n/a | status.st_ino == handles[i].ino) { |
|---|
| 82 | n/a | p = (dl_funcptr) dlsym(handles[i].handle, |
|---|
| 83 | n/a | funcname); |
|---|
| 84 | n/a | return p; |
|---|
| 85 | n/a | } |
|---|
| 86 | n/a | } |
|---|
| 87 | n/a | if (nhandles < 128) { |
|---|
| 88 | n/a | handles[nhandles].dev = status.st_dev; |
|---|
| 89 | n/a | handles[nhandles].ino = status.st_ino; |
|---|
| 90 | n/a | } |
|---|
| 91 | n/a | } |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | dlopenflags = PyThreadState_GET()->interp->dlopenflags; |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | handle = dlopen(pathname, dlopenflags); |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | if (handle == NULL) { |
|---|
| 98 | n/a | PyObject *mod_name; |
|---|
| 99 | n/a | PyObject *path; |
|---|
| 100 | n/a | PyObject *error_ob; |
|---|
| 101 | n/a | const char *error = dlerror(); |
|---|
| 102 | n/a | if (error == NULL) |
|---|
| 103 | n/a | error = "unknown dlopen() error"; |
|---|
| 104 | n/a | error_ob = PyUnicode_FromString(error); |
|---|
| 105 | n/a | if (error_ob == NULL) |
|---|
| 106 | n/a | return NULL; |
|---|
| 107 | n/a | mod_name = PyUnicode_FromString(shortname); |
|---|
| 108 | n/a | if (mod_name == NULL) { |
|---|
| 109 | n/a | Py_DECREF(error_ob); |
|---|
| 110 | n/a | return NULL; |
|---|
| 111 | n/a | } |
|---|
| 112 | n/a | path = PyUnicode_FromString(pathname); |
|---|
| 113 | n/a | if (path == NULL) { |
|---|
| 114 | n/a | Py_DECREF(error_ob); |
|---|
| 115 | n/a | Py_DECREF(mod_name); |
|---|
| 116 | n/a | return NULL; |
|---|
| 117 | n/a | } |
|---|
| 118 | n/a | PyErr_SetImportError(error_ob, mod_name, path); |
|---|
| 119 | n/a | Py_DECREF(error_ob); |
|---|
| 120 | n/a | Py_DECREF(mod_name); |
|---|
| 121 | n/a | Py_DECREF(path); |
|---|
| 122 | n/a | return NULL; |
|---|
| 123 | n/a | } |
|---|
| 124 | n/a | if (fp != NULL && nhandles < 128) |
|---|
| 125 | n/a | handles[nhandles++].handle = handle; |
|---|
| 126 | n/a | p = (dl_funcptr) dlsym(handle, funcname); |
|---|
| 127 | n/a | return p; |
|---|
| 128 | n/a | } |
|---|