annotate Python/dynload_shlib.c @ 68233:e849d484029f 2.1

Close 2.1 branch.
author Georg Brandl <georg@python.org>
date Sat, 05 Mar 2011 14:54:46 +0100
parents 11cb3de0823d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11599
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Support for dynamic loading of extension modules */
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #include "Python.h"
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #include "importdl.h"
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #include <sys/types.h>
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 #include <sys/stat.h>
21032
11cb3de0823d backport of patches 2.10, 2.11, 2.12, by MvL.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 15806
diff changeset
9
11cb3de0823d backport of patches 2.10, 2.11, 2.12, by MvL.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 15806
diff changeset
10 #if defined(__NetBSD__)
11cb3de0823d backport of patches 2.10, 2.11, 2.12, by MvL.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 15806
diff changeset
11 #include <sys/param.h>
11cb3de0823d backport of patches 2.10, 2.11, 2.12, by MvL.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 15806
diff changeset
12 #if (NetBSD < 199712)
11599
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 #include <nlist.h>
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 #include <link.h>
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 #define dlerror() "error in dynamic linking"
21032
11cb3de0823d backport of patches 2.10, 2.11, 2.12, by MvL.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 15806
diff changeset
16 #endif
11cb3de0823d backport of patches 2.10, 2.11, 2.12, by MvL.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 15806
diff changeset
17 #endif /* NetBSD */
11cb3de0823d backport of patches 2.10, 2.11, 2.12, by MvL.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 15806
diff changeset
18
11599
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 #ifdef HAVE_DLFCN_H
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 #include <dlfcn.h>
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 #endif
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
21032
11cb3de0823d backport of patches 2.10, 2.11, 2.12, by MvL.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 15806
diff changeset
23 #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
15336
bbc991ab6d32 Patch 102114, Bug 11725. On OpenBSD (but apparently not on the other
Guido van Rossum <guido@python.org>
parents: 15036
diff changeset
24 #define LEAD_UNDERSCORE "_"
bbc991ab6d32 Patch 102114, Bug 11725. On OpenBSD (but apparently not on the other
Guido van Rossum <guido@python.org>
parents: 15036
diff changeset
25 #else
bbc991ab6d32 Patch 102114, Bug 11725. On OpenBSD (but apparently not on the other
Guido van Rossum <guido@python.org>
parents: 15036
diff changeset
26 #define LEAD_UNDERSCORE ""
bbc991ab6d32 Patch 102114, Bug 11725. On OpenBSD (but apparently not on the other
Guido van Rossum <guido@python.org>
parents: 15036
diff changeset
27 #endif
bbc991ab6d32 Patch 102114, Bug 11725. On OpenBSD (but apparently not on the other
Guido van Rossum <guido@python.org>
parents: 15036
diff changeset
28
11599
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #ifndef RTLD_LAZY
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 #define RTLD_LAZY 1
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 #endif
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 const struct filedescr _PyImport_DynLoadFiletab[] = {
15036
6e9ff060a728 SF "bug" 115973: patches from Norman Vine so that shared libraries and
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
35 #ifdef __CYGWIN__
6e9ff060a728 SF "bug" 115973: patches from Norman Vine so that shared libraries and
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
36 {".dll", "rb", C_EXTENSION},
15806
3b9b8f2ddfe7 Oops, one more part of the cygwin patch (SF patch #102409 by jlt63:
Guido van Rossum <guido@python.org>
parents: 15336
diff changeset
37 {"module.dll", "rb", C_EXTENSION},
15036
6e9ff060a728 SF "bug" 115973: patches from Norman Vine so that shared libraries and
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
38 #else
11599
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 {".so", "rb", C_EXTENSION},
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 {"module.so", "rb", C_EXTENSION},
15036
6e9ff060a728 SF "bug" 115973: patches from Norman Vine so that shared libraries and
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
41 #endif
11599
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 {0, 0}
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 };
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 static struct {
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 dev_t dev;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 ino_t ino;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 void *handle;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 } handles[128];
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 static int nhandles = 0;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11599
diff changeset
53 dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
11599
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 const char *pathname, FILE *fp)
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 {
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 dl_funcptr p;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 void *handle;
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11599
diff changeset
58 char funcname[258];
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11599
diff changeset
59 char pathbuf[260];
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11599
diff changeset
60
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11599
diff changeset
61 if (strchr(pathname, '/') == NULL) {
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11599
diff changeset
62 /* Prefix bare filename with "./" */
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11599
diff changeset
63 sprintf(pathbuf, "./%-.255s", pathname);
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11599
diff changeset
64 pathname = pathbuf;
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11599
diff changeset
65 }
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11599
diff changeset
66
15336
bbc991ab6d32 Patch 102114, Bug 11725. On OpenBSD (but apparently not on the other
Guido van Rossum <guido@python.org>
parents: 15036
diff changeset
67 sprintf(funcname, LEAD_UNDERSCORE "init%.200s", shortname);
11599
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 if (fp != NULL) {
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 int i;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 struct stat statb;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 fstat(fileno(fp), &statb);
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 for (i = 0; i < nhandles; i++) {
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 if (statb.st_dev == handles[i].dev &&
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 statb.st_ino == handles[i].ino) {
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 p = (dl_funcptr) dlsym(handles[i].handle,
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 funcname);
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 return p;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 }
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 }
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 if (nhandles < 128) {
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 handles[nhandles].dev = statb.st_dev;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 handles[nhandles].ino = statb.st_ino;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 }
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 }
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 #ifdef RTLD_NOW
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 /* RTLD_NOW: resolve externals now
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 (i.e. core dump now if some are missing) */
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 handle = dlopen(pathname, RTLD_NOW);
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 #else
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 if (Py_VerboseFlag)
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 printf("dlopen(\"%s\", %d);\n", pathname,
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 RTLD_LAZY);
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 handle = dlopen(pathname, RTLD_LAZY);
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 #endif /* RTLD_NOW */
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 if (handle == NULL) {
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 PyErr_SetString(PyExc_ImportError, dlerror());
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 return NULL;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 }
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 if (fp != NULL && nhandles < 128)
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 handles[nhandles++].handle = handle;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 p = (dl_funcptr) dlsym(handle, funcname);
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 return p;
78fe87ce0ded The old platform-specific contents of importdl.c, broken down into one
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 }