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