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.
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() */
23 #ifdef AIX_GENUINE_CPLUSPLUS
24 #include "/usr/lpp/xlC/include/load.h"
25 #define aix_load loadAndInit
31 extern char *Py_GetProgramName(void);
33 typedef struct Module
{
38 const struct filedescr _PyImport_DynLoadFiletab
[] = {
39 {".so", "rb", C_EXTENSION
},
40 {"module.so", "rb", C_EXTENSION
},
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
));
61 while ((errflag
= loadquery(L_GETINFO
, ldibuf
, bufsize
)) == -1
65 if ((ldibuf
= malloc(bufsize
)) == NULL
) {
66 PyErr_SetString(PyExc_ImportError
, strerror(errno
));
71 PyErr_SetString(PyExc_ImportError
, strerror(errno
));
75 -- Make the modules list from the ld_info structures.
77 ldiptr
= (struct ld_info
*)ldibuf
;
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)
93 if ((modptr
= (ModulePtr
)malloc(sizeof(Module
))) == NULL
) {
94 PyErr_SetString(PyExc_ImportError
, strerror(errno
));
96 modptr
= (ModulePtr
)*modlistptr
;
97 *modlistptr
= (void *)modptr
->next
;
102 modptr
->entry
= ldiptr
->ldinfo_dataorg
;
104 if (prevmodptr
== NULL
)
105 *modlistptr
= (void *)modptr
;
107 prevmodptr
->next
= modptr
;
109 offset
= (unsigned int)ldiptr
->ldinfo_next
;
110 ldiptr
= (struct ld_info
*)((unsigned int)ldiptr
+ offset
);
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)
131 aix_loaderror(char *pathname
)
134 char *message
[1024], errbuf
[1024];
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:"},
145 "RLD index out of range or bad relocation type:"},
146 {L_ERROR_FORMAT
, "not a valid, executable xcoff file:"},
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
));
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
]);
174 errbuf
[strlen(errbuf
)-1] = '\0'; /* trim off last newline */
175 PyErr_SetString(PyExc_ImportError
, errbuf
);
180 dl_funcptr
_PyImport_GetDynLoadFunc(const char *fqname
, const char *shortname
,
181 const char *pathname
, FILE *fp
)
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)
199 p
= (dl_funcptr
) aix_load((char *)pathname
, L_NOAUTODEFER
, 0);
201 aix_loaderror(pathname
);
204 if (aix_bindnewmodule((void *)p
, staticmodlistptr
) == -1) {
205 aix_loaderror(pathname
);