- Got rid of newmodule.c
[python/dscho.git] / Python / dynload_aix.c
blobbb26c07248ba7f30553f0fb9840b83144a449e30
2 /* Support for dynamic loading of extension modules */
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>
10 #include "Python.h"
11 #include "importdl.h"
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
22 extern char *Py_GetProgramName(void);
24 typedef struct Module {
25 struct Module *next;
26 void *entry;
27 } Module, *ModulePtr;
29 const struct filedescr _PyImport_DynLoadFiletab[] = {
30 {".so", "rb", C_EXTENSION},
31 {"module.so", "rb", C_EXTENSION},
32 {0, 0}
35 static int
36 aix_getoldmodules(void **modlistptr)
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();
46 -- Get the list of loaded modules into ld_info structures.
48 if ((ldibuf = malloc(bufsize)) == NULL) {
49 PyErr_SetString(PyExc_ImportError, strerror(errno));
50 return -1;
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;
61 if (errflag == -1) {
62 PyErr_SetString(PyExc_ImportError, strerror(errno));
63 return -1;
66 -- Make the modules list from the ld_info structures.
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) {
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).
79 offset = (unsigned int)ldiptr->ldinfo_next;
80 ldiptr = (struct ld_info *)((unsigned int)
81 ldiptr + offset);
82 continue;
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);
91 return -1;
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;
108 static void
109 aix_loaderror(const char *pathname)
112 char *message[1024], errbuf[1024];
113 register int i,j;
115 struct errtab {
116 int errNo;
117 char *errstr;
118 } load_errtab[] = {
119 {L_ERROR_TOOMANY, "too many errors, rest skipped."},
120 {L_ERROR_NOLIB, "can't load library:"},
121 {L_ERROR_UNDEF, "can't find symbol in library:"},
122 {L_ERROR_RLDBAD,
123 "RLD index out of range or bad relocation type:"},
124 {L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
125 {L_ERROR_MEMBER,
126 "file not an archive or does not contain requested member:"},
127 {L_ERROR_TYPE, "symbol table mismatch:"},
128 {L_ERROR_ALIGN, "text alignment in file is wrong."},
129 {L_ERROR_SYSTEM, "System error:"},
130 {L_ERROR_ERRNO, NULL}
133 #define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0]))
134 #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
136 PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname);
138 if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
139 ERRBUF_APPEND(strerror(errno));
140 ERRBUF_APPEND("\n");
142 for(i = 0; message[i] && *message[i]; i++) {
143 int nerr = atoi(message[i]);
144 for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
145 if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
146 ERRBUF_APPEND(load_errtab[j].errstr);
148 while (isdigit(*message[i])) message[i]++ ;
149 ERRBUF_APPEND(message[i]);
150 ERRBUF_APPEND("\n");
152 errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
153 PyErr_SetString(PyExc_ImportError, errbuf);
154 return;
158 dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
159 const char *pathname, FILE *fp)
161 dl_funcptr p;
164 -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
165 -- of the shared module unresolved. Thus we have to resolve them
166 -- explicitly with loadbind. The new module is loaded, then we
167 -- resolve its symbols using the list of already loaded modules
168 -- (only those that belong to the python executable). Get these
169 -- with loadquery(L_GETINFO).
172 static void *staticmodlistptr = NULL;
174 if (!staticmodlistptr)
175 if (aix_getoldmodules(&staticmodlistptr) == -1)
176 return NULL;
177 p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0);
178 if (p == NULL) {
179 aix_loaderror(pathname);
180 return NULL;
183 return p;