blob: 0e271521aa11524d47293b129464bcd7d0184e78 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
16
17While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
29
30******************************************************************/
31
32/* Support for dynamic loading of extension modules */
33
34#include <ctype.h> /* for isdigit() */
35#include <errno.h> /* for global errno */
36#include <string.h> /* for strerror() */
37#include <stdlib.h> /* for malloc(), free() */
38#include <sys/ldr.h>
39
40#include "Python.h"
41#include "importdl.h"
42
43
44#ifdef AIX_GENUINE_CPLUSPLUS
45#include "/usr/lpp/xlC/include/load.h"
46#define aix_load loadAndInit
47#else
48#define aix_load load
49#endif
50
51
52extern char *Py_GetProgramName();
53
54typedef struct Module {
55 struct Module *next;
56 void *entry;
57} Module, *ModulePtr;
58
59const struct filedescr _PyImport_DynLoadFiletab[] = {
60 {".so", "rb", C_EXTENSION},
61 {"module.so", "rb", C_EXTENSION},
62 {0, 0}
63};
64
65static int
66aix_getoldmodules(modlistptr)
67 void **modlistptr;
68{
69 register ModulePtr modptr, prevmodptr;
70 register struct ld_info *ldiptr;
71 register char *ldibuf;
72 register int errflag, bufsize = 1024;
73 register unsigned int offset;
74 char *progname = Py_GetProgramName();
75
76 /*
77 -- Get the list of loaded modules into ld_info structures.
78 */
79 if ((ldibuf = malloc(bufsize)) == NULL) {
80 PyErr_SetString(PyExc_ImportError, strerror(errno));
81 return -1;
82 }
83 while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1
84 && errno == ENOMEM) {
85 free(ldibuf);
86 bufsize += 1024;
87 if ((ldibuf = malloc(bufsize)) == NULL) {
88 PyErr_SetString(PyExc_ImportError, strerror(errno));
89 return -1;
90 }
91 }
92 if (errflag == -1) {
93 PyErr_SetString(PyExc_ImportError, strerror(errno));
94 return -1;
95 }
96 /*
97 -- Make the modules list from the ld_info structures.
98 */
99 ldiptr = (struct ld_info *)ldibuf;
100 prevmodptr = NULL;
101 do {
102 if (strstr(progname, ldiptr->ldinfo_filename) == NULL &&
103 strstr(ldiptr->ldinfo_filename, "python") == NULL) {
104 /*
105 -- Extract only the modules belonging to the main
106 -- executable + those containing "python" as a
107 -- substring (like the "python[version]" binary or
108 -- "libpython[version].a" in case it's a shared lib).
109 */
110 offset = (unsigned int)ldiptr->ldinfo_next;
111 ldiptr = (struct ld_info *)((unsigned int)
112 ldiptr + offset);
113 continue;
114 }
115 if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) {
116 PyErr_SetString(PyExc_ImportError, strerror(errno));
117 while (*modlistptr) {
118 modptr = (ModulePtr)*modlistptr;
119 *modlistptr = (void *)modptr->next;
120 free(modptr);
121 }
122 return -1;
123 }
124 modptr->entry = ldiptr->ldinfo_dataorg;
125 modptr->next = NULL;
126 if (prevmodptr == NULL)
127 *modlistptr = (void *)modptr;
128 else
129 prevmodptr->next = modptr;
130 prevmodptr = modptr;
131 offset = (unsigned int)ldiptr->ldinfo_next;
132 ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset);
133 } while (offset);
134 free(ldibuf);
135 return 0;
136}
137
138static int
139aix_bindnewmodule(newmoduleptr, modlistptr)
140 void *newmoduleptr;
141 void *modlistptr;
142{
143 register ModulePtr modptr;
144
145 /*
146 -- Bind the new module with the list of loaded modules.
147 */
148 for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next)
149 if (loadbind(0, modptr->entry, newmoduleptr) != 0)
150 return -1;
151 return 0;
152}
153
154static void
155aix_loaderror(pathname)
156 char *pathname;
157{
158
159 char *message[1024], errbuf[1024];
160 register int i,j;
161
162 struct errtab {
163 int errNo;
164 char *errstr;
165 } load_errtab[] = {
166 {L_ERROR_TOOMANY, "too many errors, rest skipped."},
167 {L_ERROR_NOLIB, "can't load library:"},
168 {L_ERROR_UNDEF, "can't find symbol in library:"},
169 {L_ERROR_RLDBAD,
170 "RLD index out of range or bad relocation type:"},
171 {L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
172 {L_ERROR_MEMBER,
173 "file not an archive or does not contain requested member:"},
174 {L_ERROR_TYPE, "symbol table mismatch:"},
175 {L_ERROR_ALIGN, "text alignment in file is wrong."},
176 {L_ERROR_SYSTEM, "System error:"},
177 {L_ERROR_ERRNO, NULL}
178 };
179
180#define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0]))
181#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
182
183 sprintf(errbuf, "from module %.200s ", pathname);
184
185 if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
186 ERRBUF_APPEND(strerror(errno));
187 ERRBUF_APPEND("\n");
188 }
189 for(i = 0; message[i] && *message[i]; i++) {
190 int nerr = atoi(message[i]);
191 for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
192 if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
193 ERRBUF_APPEND(load_errtab[j].errstr);
194 }
195 while (isdigit(*message[i])) message[i]++ ;
196 ERRBUF_APPEND(message[i]);
197 ERRBUF_APPEND("\n");
198 }
199 errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
200 PyErr_SetString(PyExc_ImportError, errbuf);
201 return;
202}
203
204
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000205dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +0000206 const char *pathname, FILE *fp)
207{
208 dl_funcptr p;
209
210 /*
211 -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
212 -- of the shared module unresolved. Thus we have to resolve them
213 -- explicitely with loadbind. The new module is loaded, then we
214 -- resolve its symbols using the list of already loaded modules
215 -- (only those that belong to the python executable). Get these
216 -- with loadquery(L_GETINFO).
217 */
218
219 static void *staticmodlistptr = NULL;
220
221 if (!staticmodlistptr)
222 if (aix_getoldmodules(&staticmodlistptr) == -1)
223 return NULL;
Guido van Rossum96b5ee81999-12-21 15:55:47 +0000224 p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000225 if (p == NULL) {
226 aix_loaderror(pathname);
227 return NULL;
228 }
229 if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) {
230 aix_loaderror(pathname);
231 return NULL;
232 }
233
234 return p;
235}