Bump version to 0.9.1.
[python/dscho.git] / Python / dynload_aix.c
blob013e8f8bf5ddaa1fb48ca88c82ef2bb963b7df9c
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Support for dynamic loading of extension modules */
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>
19 #include "Python.h"
20 #include "importdl.h"
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
31 extern char *Py_GetProgramName(void);
33 typedef struct Module {
34 struct Module *next;
35 void *entry;
36 } Module, *ModulePtr;
38 const struct filedescr _PyImport_DynLoadFiletab[] = {
39 {".so", "rb", C_EXTENSION},
40 {"module.so", "rb", C_EXTENSION},
41 {0, 0}
44 static int
45 aix_getoldmodules(void **modlistptr)
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();
55 -- Get the list of loaded modules into ld_info structures.
57 if ((ldibuf = malloc(bufsize)) == NULL) {
58 PyErr_SetString(PyExc_ImportError, strerror(errno));
59 return -1;
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;
70 if (errflag == -1) {
71 PyErr_SetString(PyExc_ImportError, strerror(errno));
72 return -1;
75 -- Make the modules list from the ld_info structures.
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) {
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).
88 offset = (unsigned int)ldiptr->ldinfo_next;
89 ldiptr = (struct ld_info *)((unsigned int)
90 ldiptr + offset);
91 continue;
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);
100 return -1;
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;
116 static int
117 aix_bindnewmodule(void *newmoduleptr, void *modlistptr)
119 register ModulePtr modptr;
122 -- Bind the new module with the list of loaded modules.
124 for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next)
125 if (loadbind(0, modptr->entry, newmoduleptr) != 0)
126 return -1;
127 return 0;
130 static void
131 aix_loaderror(char *pathname)
134 char *message[1024], errbuf[1024];
135 register int i,j;
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}
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)
158 sprintf(errbuf, "from module %.200s ", pathname);
160 if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
161 ERRBUF_APPEND(strerror(errno));
162 ERRBUF_APPEND("\n");
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);
170 while (isdigit(*message[i])) message[i]++ ;
171 ERRBUF_APPEND(message[i]);
172 ERRBUF_APPEND("\n");
174 errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
175 PyErr_SetString(PyExc_ImportError, errbuf);
176 return;
180 dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
181 const char *pathname, FILE *fp)
183 dl_funcptr p;
186 -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
187 -- of the shared module unresolved. Thus we have to resolve them
188 -- explicitly with loadbind. The new module is loaded, then we
189 -- 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).
194 static void *staticmodlistptr = NULL;
196 if (!staticmodlistptr)
197 if (aix_getoldmodules(&staticmodlistptr) == -1)
198 return NULL;
199 p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0);
200 if (p == NULL) {
201 aix_loaderror(pathname);
202 return NULL;
204 if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) {
205 aix_loaderror(pathname);
206 return NULL;
209 return p;