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