blob: b878f019279b95a5d58ef231f203e7dda343e599 [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 <ctype.h> /* for isdigit() */
20#include <errno.h> /* for global errno */
21#include <string.h> /* for strerror() */
22#include <stdlib.h> /* for malloc(), free() */
23#include <sys/ldr.h>
24
25#include "Python.h"
26#include "importdl.h"
27
28
29#ifdef AIX_GENUINE_CPLUSPLUS
30#include "/usr/lpp/xlC/include/load.h"
31#define aix_load loadAndInit
32#else
33#define aix_load load
34#endif
35
36
37extern char *Py_GetProgramName();
38
39typedef struct Module {
40 struct Module *next;
41 void *entry;
42} Module, *ModulePtr;
43
44const struct filedescr _PyImport_DynLoadFiletab[] = {
45 {".so", "rb", C_EXTENSION},
46 {"module.so", "rb", C_EXTENSION},
47 {0, 0}
48};
49
50static int
51aix_getoldmodules(modlistptr)
52 void **modlistptr;
53{
54 register ModulePtr modptr, prevmodptr;
55 register struct ld_info *ldiptr;
56 register char *ldibuf;
57 register int errflag, bufsize = 1024;
58 register unsigned int offset;
59 char *progname = Py_GetProgramName();
60
61 /*
62 -- Get the list of loaded modules into ld_info structures.
63 */
64 if ((ldibuf = malloc(bufsize)) == NULL) {
65 PyErr_SetString(PyExc_ImportError, strerror(errno));
66 return -1;
67 }
68 while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1
69 && errno == ENOMEM) {
70 free(ldibuf);
71 bufsize += 1024;
72 if ((ldibuf = malloc(bufsize)) == NULL) {
73 PyErr_SetString(PyExc_ImportError, strerror(errno));
74 return -1;
75 }
76 }
77 if (errflag == -1) {
78 PyErr_SetString(PyExc_ImportError, strerror(errno));
79 return -1;
80 }
81 /*
82 -- Make the modules list from the ld_info structures.
83 */
84 ldiptr = (struct ld_info *)ldibuf;
85 prevmodptr = NULL;
86 do {
87 if (strstr(progname, ldiptr->ldinfo_filename) == NULL &&
88 strstr(ldiptr->ldinfo_filename, "python") == NULL) {
89 /*
90 -- Extract only the modules belonging to the main
91 -- executable + those containing "python" as a
92 -- substring (like the "python[version]" binary or
93 -- "libpython[version].a" in case it's a shared lib).
94 */
95 offset = (unsigned int)ldiptr->ldinfo_next;
96 ldiptr = (struct ld_info *)((unsigned int)
97 ldiptr + offset);
98 continue;
99 }
100 if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) {
101 PyErr_SetString(PyExc_ImportError, strerror(errno));
102 while (*modlistptr) {
103 modptr = (ModulePtr)*modlistptr;
104 *modlistptr = (void *)modptr->next;
105 free(modptr);
106 }
107 return -1;
108 }
109 modptr->entry = ldiptr->ldinfo_dataorg;
110 modptr->next = NULL;
111 if (prevmodptr == NULL)
112 *modlistptr = (void *)modptr;
113 else
114 prevmodptr->next = modptr;
115 prevmodptr = modptr;
116 offset = (unsigned int)ldiptr->ldinfo_next;
117 ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset);
118 } while (offset);
119 free(ldibuf);
120 return 0;
121}
122
123static int
124aix_bindnewmodule(newmoduleptr, modlistptr)
125 void *newmoduleptr;
126 void *modlistptr;
127{
128 register ModulePtr modptr;
129
130 /*
131 -- Bind the new module with the list of loaded modules.
132 */
133 for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next)
134 if (loadbind(0, modptr->entry, newmoduleptr) != 0)
135 return -1;
136 return 0;
137}
138
139static void
140aix_loaderror(pathname)
141 char *pathname;
142{
143
144 char *message[1024], errbuf[1024];
145 register int i,j;
146
147 struct errtab {
148 int errNo;
149 char *errstr;
150 } load_errtab[] = {
151 {L_ERROR_TOOMANY, "too many errors, rest skipped."},
152 {L_ERROR_NOLIB, "can't load library:"},
153 {L_ERROR_UNDEF, "can't find symbol in library:"},
154 {L_ERROR_RLDBAD,
155 "RLD index out of range or bad relocation type:"},
156 {L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
157 {L_ERROR_MEMBER,
158 "file not an archive or does not contain requested member:"},
159 {L_ERROR_TYPE, "symbol table mismatch:"},
160 {L_ERROR_ALIGN, "text alignment in file is wrong."},
161 {L_ERROR_SYSTEM, "System error:"},
162 {L_ERROR_ERRNO, NULL}
163 };
164
165#define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0]))
166#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
167
168 sprintf(errbuf, "from module %.200s ", pathname);
169
170 if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
171 ERRBUF_APPEND(strerror(errno));
172 ERRBUF_APPEND("\n");
173 }
174 for(i = 0; message[i] && *message[i]; i++) {
175 int nerr = atoi(message[i]);
176 for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
177 if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
178 ERRBUF_APPEND(load_errtab[j].errstr);
179 }
180 while (isdigit(*message[i])) message[i]++ ;
181 ERRBUF_APPEND(message[i]);
182 ERRBUF_APPEND("\n");
183 }
184 errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
185 PyErr_SetString(PyExc_ImportError, errbuf);
186 return;
187}
188
189
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000190dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +0000191 const char *pathname, FILE *fp)
192{
193 dl_funcptr p;
194
195 /*
196 -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
197 -- of the shared module unresolved. Thus we have to resolve them
198 -- explicitely with loadbind. The new module is loaded, then we
199 -- resolve its symbols using the list of already loaded modules
200 -- (only those that belong to the python executable). Get these
201 -- with loadquery(L_GETINFO).
202 */
203
204 static void *staticmodlistptr = NULL;
205
206 if (!staticmodlistptr)
207 if (aix_getoldmodules(&staticmodlistptr) == -1)
208 return NULL;
Guido van Rossum96b5ee81999-12-21 15:55:47 +0000209 p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000210 if (p == NULL) {
211 aix_loaderror(pathname);
212 return NULL;
213 }
214 if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) {
215 aix_loaderror(pathname);
216 return NULL;
217 }
218
219 return p;
220}