dynload_shlib.c 3.92 KB
Newer Older
1 2 3 4 5 6 7 8

/* Support for dynamic loading of extension modules */

#include "Python.h"
#include "importdl.h"

#include <sys/types.h>
#include <sys/stat.h>
9

10 11 12
#if defined(__NetBSD__)
#include <sys/param.h>
#if (NetBSD < 199712)
13 14 15
#include <nlist.h>
#include <link.h>
#define dlerror() "error in dynamic linking"
16 17 18
#endif
#endif /* NetBSD */

19 20 21 22
#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#endif

23
#if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
24 25 26 27 28
#define LEAD_UNDERSCORE "_"
#else
#define LEAD_UNDERSCORE ""
#endif

Barry Warsaw's avatar
Barry Warsaw committed
29 30 31 32 33
/* The .so extension module ABI tag, supplied by the Makefile via
   Makefile.pre.in and configure.  This is used to discriminate between
   incompatible .so files so that extensions for different Python builds can
   live in the same directory.  E.g. foomodule.cpython-32.so
*/
34

35
const char *_PyImport_DynLoadFiletab[] = {
36
#ifdef __CYGWIN__
37
    ".dll",
Barry Warsaw's avatar
Barry Warsaw committed
38
#else  /* !__CYGWIN__ */
39
#ifdef __VMS
40 41
    ".exe",
    ".EXE",
Barry Warsaw's avatar
Barry Warsaw committed
42
#else  /* !__VMS */
43 44 45
    "." SOABI ".so",
    ".abi" PYTHON_ABI_STRING ".so",
    ".so",
Barry Warsaw's avatar
Barry Warsaw committed
46 47
#endif  /* __VMS */
#endif  /* __CYGWIN__ */
48
    NULL,
49 50 51
};

static struct {
52
    dev_t dev;
53
#ifdef __VMS
54
    ino_t ino[3];
55
#else
56
    ino_t ino;
57
#endif
58
    void *handle;
59 60 61 62
} handles[128];
static int nhandles = 0;


63
dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
64
                                    const char *pathname, FILE *fp)
65
{
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    dl_funcptr p;
    void *handle;
    char funcname[258];
    char pathbuf[260];
    int dlopenflags=0;

    if (strchr(pathname, '/') == NULL) {
        /* Prefix bare filename with "./" */
        PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
        pathname = pathbuf;
    }

    PyOS_snprintf(funcname, sizeof(funcname),
                  LEAD_UNDERSCORE "PyInit_%.200s", shortname);

    if (fp != NULL) {
        int i;
        struct stat statb;
        fstat(fileno(fp), &statb);
        for (i = 0; i < nhandles; i++) {
            if (statb.st_dev == handles[i].dev &&
                statb.st_ino == handles[i].ino) {
                p = (dl_funcptr) dlsym(handles[i].handle,
                                       funcname);
                return p;
            }
        }
        if (nhandles < 128) {
            handles[nhandles].dev = statb.st_dev;
95
#ifdef __VMS
96 97 98
            handles[nhandles].ino[0] = statb.st_ino[0];
            handles[nhandles].ino[1] = statb.st_ino[1];
            handles[nhandles].ino[2] = statb.st_ino[2];
99
#else
100
            handles[nhandles].ino = statb.st_ino;
101
#endif
102 103
        }
    }
104

105
    dlopenflags = PyThreadState_GET()->interp->dlopenflags;
106

107
#ifdef __VMS
108 109 110 111 112 113 114 115
    /* VMS currently don't allow a pathname, use a logical name instead */
    /* Concatenate 'python_module_' and shortname */
    /* so "import vms.bar" will use the logical python_module_bar */
    /* As C module use only one name space this is probably not a */
    /* important limitation */
    PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s",
                  shortname);
    pathname = pathbuf;
116 117
#endif

118 119 120
    handle = dlopen(pathname, dlopenflags);

    if (handle == NULL) {
121 122 123
        PyObject *mod_name;
        PyObject *path;
        PyObject *error_ob;
124 125 126
        const char *error = dlerror();
        if (error == NULL)
            error = "unknown dlopen() error";
127
        error_ob = PyUnicode_FromString(error);
128 129
        if (error_ob == NULL)
            return NULL;
130
        mod_name = PyUnicode_FromString(shortname);
131 132 133 134 135 136 137 138 139 140
        if (mod_name == NULL) {
            Py_DECREF(error_ob);
            return NULL;
        }
        path = PyUnicode_FromString(pathname);
        if (path == NULL) {
            Py_DECREF(error_ob);
            Py_DECREF(mod_name);
            return NULL;
        }
141
        PyErr_SetImportError(error_ob, mod_name, path);
142 143 144
        Py_DECREF(error_ob);
        Py_DECREF(mod_name);
        Py_DECREF(path);
145 146 147 148 149 150
        return NULL;
    }
    if (fp != NULL && nhandles < 128)
        handles[nhandles++].handle = handle;
    p = (dl_funcptr) dlsym(handle, funcname);
    return p;
151
}