| 1 | n/a | |
|---|
| 2 | n/a | /* Support for dynamic loading of extension modules */ |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | #include "Python.h" |
|---|
| 5 | n/a | #include "importdl.h" |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | #include <errno.h> /* for global errno */ |
|---|
| 8 | n/a | #include <string.h> /* for strerror() */ |
|---|
| 9 | n/a | #include <stdlib.h> /* for malloc(), free() */ |
|---|
| 10 | n/a | #include <sys/ldr.h> |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | #ifdef AIX_GENUINE_CPLUSPLUS |
|---|
| 14 | n/a | #include <load.h> |
|---|
| 15 | n/a | #define aix_load loadAndInit |
|---|
| 16 | n/a | #else |
|---|
| 17 | n/a | #define aix_load load |
|---|
| 18 | n/a | #endif |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | extern char *Py_GetProgramName(void); |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | typedef struct Module { |
|---|
| 24 | n/a | struct Module *next; |
|---|
| 25 | n/a | void *entry; |
|---|
| 26 | n/a | } Module, *ModulePtr; |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | const char *_PyImport_DynLoadFiletab[] = {".so", NULL}; |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | static int |
|---|
| 31 | n/a | aix_getoldmodules(void **modlistptr) |
|---|
| 32 | n/a | { |
|---|
| 33 | n/a | ModulePtr modptr, prevmodptr; |
|---|
| 34 | n/a | struct ld_info *ldiptr; |
|---|
| 35 | n/a | char *ldibuf; |
|---|
| 36 | n/a | int errflag, bufsize = 1024; |
|---|
| 37 | n/a | unsigned int offset; |
|---|
| 38 | n/a | char *progname = Py_GetProgramName(); |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | /* |
|---|
| 41 | n/a | -- Get the list of loaded modules into ld_info structures. |
|---|
| 42 | n/a | */ |
|---|
| 43 | n/a | if ((ldibuf = malloc(bufsize)) == NULL) { |
|---|
| 44 | n/a | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
|---|
| 45 | n/a | return -1; |
|---|
| 46 | n/a | } |
|---|
| 47 | n/a | while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1 |
|---|
| 48 | n/a | && errno == ENOMEM) { |
|---|
| 49 | n/a | free(ldibuf); |
|---|
| 50 | n/a | bufsize += 1024; |
|---|
| 51 | n/a | if ((ldibuf = malloc(bufsize)) == NULL) { |
|---|
| 52 | n/a | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
|---|
| 53 | n/a | return -1; |
|---|
| 54 | n/a | } |
|---|
| 55 | n/a | } |
|---|
| 56 | n/a | if (errflag == -1) { |
|---|
| 57 | n/a | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
|---|
| 58 | n/a | return -1; |
|---|
| 59 | n/a | } |
|---|
| 60 | n/a | /* |
|---|
| 61 | n/a | -- Make the modules list from the ld_info structures. |
|---|
| 62 | n/a | */ |
|---|
| 63 | n/a | ldiptr = (struct ld_info *)ldibuf; |
|---|
| 64 | n/a | prevmodptr = NULL; |
|---|
| 65 | n/a | do { |
|---|
| 66 | n/a | if (strstr(progname, ldiptr->ldinfo_filename) == NULL && |
|---|
| 67 | n/a | strstr(ldiptr->ldinfo_filename, "python") == NULL) { |
|---|
| 68 | n/a | /* |
|---|
| 69 | n/a | -- Extract only the modules belonging to the main |
|---|
| 70 | n/a | -- executable + those containing "python" as a |
|---|
| 71 | n/a | -- substring (like the "python[version]" binary or |
|---|
| 72 | n/a | -- "libpython[version].a" in case it's a shared lib). |
|---|
| 73 | n/a | */ |
|---|
| 74 | n/a | offset = (unsigned int)ldiptr->ldinfo_next; |
|---|
| 75 | n/a | ldiptr = (struct ld_info *)((char*)ldiptr + offset); |
|---|
| 76 | n/a | continue; |
|---|
| 77 | n/a | } |
|---|
| 78 | n/a | if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) { |
|---|
| 79 | n/a | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
|---|
| 80 | n/a | while (*modlistptr) { |
|---|
| 81 | n/a | modptr = (ModulePtr)*modlistptr; |
|---|
| 82 | n/a | *modlistptr = (void *)modptr->next; |
|---|
| 83 | n/a | free(modptr); |
|---|
| 84 | n/a | } |
|---|
| 85 | n/a | return -1; |
|---|
| 86 | n/a | } |
|---|
| 87 | n/a | modptr->entry = ldiptr->ldinfo_dataorg; |
|---|
| 88 | n/a | modptr->next = NULL; |
|---|
| 89 | n/a | if (prevmodptr == NULL) |
|---|
| 90 | n/a | *modlistptr = (void *)modptr; |
|---|
| 91 | n/a | else |
|---|
| 92 | n/a | prevmodptr->next = modptr; |
|---|
| 93 | n/a | prevmodptr = modptr; |
|---|
| 94 | n/a | offset = (unsigned int)ldiptr->ldinfo_next; |
|---|
| 95 | n/a | ldiptr = (struct ld_info *)((char*)ldiptr + offset); |
|---|
| 96 | n/a | } while (offset); |
|---|
| 97 | n/a | free(ldibuf); |
|---|
| 98 | n/a | return 0; |
|---|
| 99 | n/a | } |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | static void |
|---|
| 103 | n/a | aix_loaderror(const char *pathname) |
|---|
| 104 | n/a | { |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | char *message[1024], errbuf[1024]; |
|---|
| 107 | n/a | PyObject *pathname_ob = NULL; |
|---|
| 108 | n/a | PyObject *errbuf_ob = NULL; |
|---|
| 109 | n/a | int i,j; |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | struct errtab { |
|---|
| 112 | n/a | int errNo; |
|---|
| 113 | n/a | char *errstr; |
|---|
| 114 | n/a | } load_errtab[] = { |
|---|
| 115 | n/a | {L_ERROR_TOOMANY, "too many errors, rest skipped."}, |
|---|
| 116 | n/a | {L_ERROR_NOLIB, "can't load library:"}, |
|---|
| 117 | n/a | {L_ERROR_UNDEF, "can't find symbol in library:"}, |
|---|
| 118 | n/a | {L_ERROR_RLDBAD, |
|---|
| 119 | n/a | "RLD index out of range or bad relocation type:"}, |
|---|
| 120 | n/a | {L_ERROR_FORMAT, "not a valid, executable xcoff file:"}, |
|---|
| 121 | n/a | {L_ERROR_MEMBER, |
|---|
| 122 | n/a | "file not an archive or does not contain requested member:"}, |
|---|
| 123 | n/a | {L_ERROR_TYPE, "symbol table mismatch:"}, |
|---|
| 124 | n/a | {L_ERROR_ALIGN, "text alignment in file is wrong."}, |
|---|
| 125 | n/a | {L_ERROR_SYSTEM, "System error:"}, |
|---|
| 126 | n/a | {L_ERROR_ERRNO, NULL} |
|---|
| 127 | n/a | }; |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1) |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname); |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) { |
|---|
| 134 | n/a | ERRBUF_APPEND(strerror(errno)); |
|---|
| 135 | n/a | ERRBUF_APPEND("\n"); |
|---|
| 136 | n/a | } |
|---|
| 137 | n/a | for(i = 0; message[i] && *message[i]; i++) { |
|---|
| 138 | n/a | int nerr = atoi(message[i]); |
|---|
| 139 | n/a | for (j=0; j < Py_ARRAY_LENGTH(load_errtab); j++) { |
|---|
| 140 | n/a | if (nerr == load_errtab[j].errNo && load_errtab[j].errstr) |
|---|
| 141 | n/a | ERRBUF_APPEND(load_errtab[j].errstr); |
|---|
| 142 | n/a | } |
|---|
| 143 | n/a | while (Py_ISDIGIT(Py_CHARMASK(*message[i]))) message[i]++ ; |
|---|
| 144 | n/a | ERRBUF_APPEND(message[i]); |
|---|
| 145 | n/a | ERRBUF_APPEND("\n"); |
|---|
| 146 | n/a | } |
|---|
| 147 | n/a | errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */ |
|---|
| 148 | n/a | pathname_ob = PyUnicode_FromString(pathname); |
|---|
| 149 | n/a | errbuf_ob = PyUnicode_FromString(errbuf); |
|---|
| 150 | n/a | PyErr_SetImportError(errbuf_ob, NULL, pathname); |
|---|
| 151 | n/a | Py_DECREF(pathname_ob); |
|---|
| 152 | n/a | Py_DECREF(errbuf_ob); |
|---|
| 153 | n/a | return; |
|---|
| 154 | n/a | } |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix, |
|---|
| 158 | n/a | const char *shortname, |
|---|
| 159 | n/a | const char *pathname, FILE *fp) |
|---|
| 160 | n/a | { |
|---|
| 161 | n/a | dl_funcptr p; |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | /* |
|---|
| 164 | n/a | -- Invoke load() with L_NOAUTODEFER leaving the imported symbols |
|---|
| 165 | n/a | -- of the shared module unresolved. Thus we have to resolve them |
|---|
| 166 | n/a | -- explicitly with loadbind. The new module is loaded, then we |
|---|
| 167 | n/a | -- resolve its symbols using the list of already loaded modules |
|---|
| 168 | n/a | -- (only those that belong to the python executable). Get these |
|---|
| 169 | n/a | -- with loadquery(L_GETINFO). |
|---|
| 170 | n/a | */ |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | static void *staticmodlistptr = NULL; |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | if (!staticmodlistptr) |
|---|
| 175 | n/a | if (aix_getoldmodules(&staticmodlistptr) == -1) |
|---|
| 176 | n/a | return NULL; |
|---|
| 177 | n/a | p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0); |
|---|
| 178 | n/a | if (p == NULL) { |
|---|
| 179 | n/a | aix_loaderror(pathname); |
|---|
| 180 | n/a | return NULL; |
|---|
| 181 | n/a | } |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | return p; |
|---|
| 184 | n/a | } |
|---|