blob: 279e2403d7bacaa102a133d273ff1c7c2b1e2d87 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossum22a1d361999-12-20 21:18:49 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum22a1d361999-12-20 21:18:49 +000014
15******************************************************************/
16
17/* Support for dynamic loading of extension modules */
18
19#include "Python.h"
20#include "importdl.h"
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#if defined(__NetBSD__) && (NetBSD < 199712)
25#include <nlist.h>
26#include <link.h>
27#define dlerror() "error in dynamic linking"
28#else
29#ifdef HAVE_DLFCN_H
30#include <dlfcn.h>
31#endif
32#endif
33
34#ifndef RTLD_LAZY
35#define RTLD_LAZY 1
36#endif
37
38
39const struct filedescr _PyImport_DynLoadFiletab[] = {
40 {".so", "rb", C_EXTENSION},
41 {"module.so", "rb", C_EXTENSION},
42 {0, 0}
43};
44
45static struct {
46 dev_t dev;
47 ino_t ino;
48 void *handle;
49} handles[128];
50static int nhandles = 0;
51
52
Guido van Rossum96a8fb71999-12-22 14:09:35 +000053dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +000054 const char *pathname, FILE *fp)
55{
56 dl_funcptr p;
57 void *handle;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000058 char funcname[258];
59 char pathbuf[260];
60
61 if (strchr(pathname, '/') == NULL) {
62 /* Prefix bare filename with "./" */
63 sprintf(pathbuf, "./%-.255s", pathname);
64 pathname = pathbuf;
65 }
66
67 /* ### should there be a leading underscore for some platforms? */
68 sprintf(funcname, "init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000069
70 if (fp != NULL) {
71 int i;
72 struct stat statb;
73 fstat(fileno(fp), &statb);
74 for (i = 0; i < nhandles; i++) {
75 if (statb.st_dev == handles[i].dev &&
76 statb.st_ino == handles[i].ino) {
77 p = (dl_funcptr) dlsym(handles[i].handle,
78 funcname);
79 return p;
80 }
81 }
82 if (nhandles < 128) {
83 handles[nhandles].dev = statb.st_dev;
84 handles[nhandles].ino = statb.st_ino;
85 }
86 }
87
88#ifdef RTLD_NOW
89 /* RTLD_NOW: resolve externals now
90 (i.e. core dump now if some are missing) */
91 handle = dlopen(pathname, RTLD_NOW);
92#else
93 if (Py_VerboseFlag)
94 printf("dlopen(\"%s\", %d);\n", pathname,
95 RTLD_LAZY);
96 handle = dlopen(pathname, RTLD_LAZY);
97#endif /* RTLD_NOW */
98 if (handle == NULL) {
99 PyErr_SetString(PyExc_ImportError, dlerror());
100 return NULL;
101 }
102 if (fp != NULL && nhandles < 128)
103 handles[nhandles++].handle = handle;
104 p = (dl_funcptr) dlsym(handle, funcname);
105 return p;
106}