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