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