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 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 33 | |
| 34 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 35 | #ifdef __CYGWIN__ |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 36 | {".dll", "rb", C_EXTENSION}, |
Guido van Rossum | fef1243 | 2001-01-10 21:17:27 +0000 | [diff] [blame] | 37 | {"module.dll", "rb", C_EXTENSION}, |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 38 | #else |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 39 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 40 | {".pyd", "rb", C_EXTENSION}, |
| 41 | {".dll", "rb", C_EXTENSION}, |
| 42 | #else |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 43 | {".so", "rb", C_EXTENSION}, |
| 44 | {"module.so", "rb", C_EXTENSION}, |
Tim Peters | 98dc065 | 2000-10-05 19:24:26 +0000 | [diff] [blame] | 45 | #endif |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 46 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 47 | {0, 0} |
| 48 | }; |
| 49 | |
| 50 | static struct { |
| 51 | dev_t dev; |
| 52 | ino_t ino; |
| 53 | void *handle; |
| 54 | } handles[128]; |
| 55 | static int nhandles = 0; |
| 56 | |
| 57 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 58 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 59 | const char *pathname, FILE *fp) |
| 60 | { |
| 61 | dl_funcptr p; |
| 62 | void *handle; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 63 | char funcname[258]; |
| 64 | char pathbuf[260]; |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 65 | int dlopenflags=0; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 66 | |
| 67 | if (strchr(pathname, '/') == NULL) { |
| 68 | /* Prefix bare filename with "./" */ |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 69 | PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname); |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 70 | pathname = pathbuf; |
| 71 | } |
| 72 | |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 73 | PyOS_snprintf(funcname, sizeof(funcname), |
| 74 | LEAD_UNDERSCORE "init%.200s", shortname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 75 | |
| 76 | if (fp != NULL) { |
| 77 | int i; |
| 78 | struct stat statb; |
| 79 | fstat(fileno(fp), &statb); |
| 80 | for (i = 0; i < nhandles; i++) { |
| 81 | if (statb.st_dev == handles[i].dev && |
| 82 | statb.st_ino == handles[i].ino) { |
| 83 | p = (dl_funcptr) dlsym(handles[i].handle, |
| 84 | funcname); |
| 85 | return p; |
| 86 | } |
| 87 | } |
| 88 | if (nhandles < 128) { |
| 89 | handles[nhandles].dev = statb.st_dev; |
| 90 | handles[nhandles].ino = statb.st_ino; |
| 91 | } |
| 92 | } |
| 93 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 94 | #if !(defined(PYOS_OS2) && defined(PYCC_GCC)) |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 95 | dlopenflags = PyThreadState_Get()->interp->dlopenflags; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 96 | #endif |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 98 | if (Py_VerboseFlag) |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 99 | printf("dlopen(\"%s\", %x);\n", pathname, dlopenflags); |
| 100 | |
| 101 | handle = dlopen(pathname, dlopenflags); |
| 102 | |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 103 | if (handle == NULL) { |
| 104 | PyErr_SetString(PyExc_ImportError, dlerror()); |
| 105 | return NULL; |
| 106 | } |
| 107 | if (fp != NULL && nhandles < 128) |
| 108 | handles[nhandles++].handle = handle; |
| 109 | p = (dl_funcptr) dlsym(handle, funcname); |
| 110 | return p; |
| 111 | } |