2
/* Support for dynamic loading of extension modules */
7
#include <errno.h> /* for global errno */
8
#include <string.h> /* for strerror() */
9
#include <stdlib.h> /* for malloc(), free() */
13
#ifdef AIX_GENUINE_CPLUSPLUS
15
#define aix_load loadAndInit
21
extern char *Py_GetProgramName(void);
23
typedef struct Module {
28
const char *_PyImport_DynLoadFiletab[] = {".so", NULL};
31
aix_getoldmodules(void **modlistptr)
33
ModulePtr modptr, prevmodptr;
34
struct ld_info *ldiptr;
36
int errflag, bufsize = 1024;
38
char *progname = Py_GetProgramName();
41
-- Get the list of loaded modules into ld_info structures.
43
if ((ldibuf = malloc(bufsize)) == NULL) {
44
PyErr_SetString(PyExc_ImportError, strerror(errno));
47
while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1
51
if ((ldibuf = malloc(bufsize)) == NULL) {
52
PyErr_SetString(PyExc_ImportError, strerror(errno));
57
PyErr_SetString(PyExc_ImportError, strerror(errno));
61
-- Make the modules list from the ld_info structures.
63
ldiptr = (struct ld_info *)ldibuf;
66
if (strstr(progname, ldiptr->ldinfo_filename) == NULL &&
67
strstr(ldiptr->ldinfo_filename, "python") == NULL) {
69
-- Extract only the modules belonging to the main
70
-- executable + those containing "python" as a
71
-- substring (like the "python[version]" binary or
72
-- "libpython[version].a" in case it's a shared lib).
74
offset = (unsigned int)ldiptr->ldinfo_next;
75
ldiptr = (struct ld_info *)((char*)ldiptr + offset);
78
if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) {
79
PyErr_SetString(PyExc_ImportError, strerror(errno));
81
modptr = (ModulePtr)*modlistptr;
82
*modlistptr = (void *)modptr->next;
87
modptr->entry = ldiptr->ldinfo_dataorg;
89
if (prevmodptr == NULL)
90
*modlistptr = (void *)modptr;
92
prevmodptr->next = modptr;
94
offset = (unsigned int)ldiptr->ldinfo_next;
95
ldiptr = (struct ld_info *)((char*)ldiptr + offset);
103
aix_loaderror(const char *pathname)
106
char *message[1024], errbuf[1024];
107
PyObject *pathname_ob = NULL;
108
PyObject *errbuf_ob = NULL;
115
{L_ERROR_TOOMANY, "too many errors, rest skipped."},
116
{L_ERROR_NOLIB, "can't load library:"},
117
{L_ERROR_UNDEF, "can't find symbol in library:"},
119
"RLD index out of range or bad relocation type:"},
120
{L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
122
"file not an archive or does not contain requested member:"},
123
{L_ERROR_TYPE, "symbol table mismatch:"},
124
{L_ERROR_ALIGN, "text alignment in file is wrong."},
125
{L_ERROR_SYSTEM, "System error:"},
126
{L_ERROR_ERRNO, NULL}
129
#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
131
PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname);
133
if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
134
ERRBUF_APPEND(strerror(errno));
137
for(i = 0; message[i] && *message[i]; i++) {
138
int nerr = atoi(message[i]);
139
for (j=0; j < Py_ARRAY_LENGTH(load_errtab); j++) {
140
if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
141
ERRBUF_APPEND(load_errtab[j].errstr);
143
while (Py_ISDIGIT(Py_CHARMASK(*message[i]))) message[i]++ ;
144
ERRBUF_APPEND(message[i]);
147
errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
148
pathname_ob = PyUnicode_FromString(pathname);
149
errbuf_ob = PyUnicode_FromString(errbuf);
150
PyErr_SetImportError(errbuf_ob, NULL, pathname);
151
Py_DECREF(pathname_ob);
152
Py_DECREF(errbuf_ob);
157
dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
158
const char *pathname, FILE *fp)
163
-- Invoke load() with L_NOAUTODEFER leaving the imported symbols
164
-- of the shared module unresolved. Thus we have to resolve them
165
-- explicitly with loadbind. The new module is loaded, then we
166
-- resolve its symbols using the list of already loaded modules
167
-- (only those that belong to the python executable). Get these
168
-- with loadquery(L_GETINFO).
171
static void *staticmodlistptr = NULL;
173
if (!staticmodlistptr)
174
if (aix_getoldmodules(&staticmodlistptr) == -1)
176
p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0);
178
aix_loaderror(pathname);