Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* Support for dynamic loading of extension modules */ |
| 12 | |
| 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> |
| 18 | |
| 19 | #include "Python.h" |
| 20 | #include "importdl.h" |
| 21 | |
| 22 | |
| 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 |
| 29 | |
| 30 | |
| 31 | extern char *Py_GetProgramName(); |
| 32 | |
| 33 | typedef struct Module { |
| 34 | struct Module *next; |
| 35 | void *entry; |
| 36 | } Module, *ModulePtr; |
| 37 | |
| 38 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
| 39 | {".so", "rb", C_EXTENSION}, |
| 40 | {"module.so", "rb", C_EXTENSION}, |
| 41 | {0, 0} |
| 42 | }; |
| 43 | |
| 44 | static int |
| 45 | aix_getoldmodules(modlistptr) |
| 46 | void **modlistptr; |
| 47 | { |
| 48 | register ModulePtr modptr, prevmodptr; |
| 49 | register struct ld_info *ldiptr; |
| 50 | register char *ldibuf; |
| 51 | register int errflag, bufsize = 1024; |
| 52 | register unsigned int offset; |
| 53 | char *progname = Py_GetProgramName(); |
| 54 | |
| 55 | /* |
| 56 | -- Get the list of loaded modules into ld_info structures. |
| 57 | */ |
| 58 | if ((ldibuf = malloc(bufsize)) == NULL) { |
| 59 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
| 60 | return -1; |
| 61 | } |
| 62 | while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1 |
| 63 | && errno == ENOMEM) { |
| 64 | free(ldibuf); |
| 65 | bufsize += 1024; |
| 66 | if ((ldibuf = malloc(bufsize)) == NULL) { |
| 67 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
| 68 | return -1; |
| 69 | } |
| 70 | } |
| 71 | if (errflag == -1) { |
| 72 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
| 73 | return -1; |
| 74 | } |
| 75 | /* |
| 76 | -- Make the modules list from the ld_info structures. |
| 77 | */ |
| 78 | ldiptr = (struct ld_info *)ldibuf; |
| 79 | prevmodptr = NULL; |
| 80 | do { |
| 81 | if (strstr(progname, ldiptr->ldinfo_filename) == NULL && |
| 82 | strstr(ldiptr->ldinfo_filename, "python") == NULL) { |
| 83 | /* |
| 84 | -- Extract only the modules belonging to the main |
| 85 | -- executable + those containing "python" as a |
| 86 | -- substring (like the "python[version]" binary or |
| 87 | -- "libpython[version].a" in case it's a shared lib). |
| 88 | */ |
| 89 | offset = (unsigned int)ldiptr->ldinfo_next; |
| 90 | ldiptr = (struct ld_info *)((unsigned int) |
| 91 | ldiptr + offset); |
| 92 | continue; |
| 93 | } |
| 94 | if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) { |
| 95 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
| 96 | while (*modlistptr) { |
| 97 | modptr = (ModulePtr)*modlistptr; |
| 98 | *modlistptr = (void *)modptr->next; |
| 99 | free(modptr); |
| 100 | } |
| 101 | return -1; |
| 102 | } |
| 103 | modptr->entry = ldiptr->ldinfo_dataorg; |
| 104 | modptr->next = NULL; |
| 105 | if (prevmodptr == NULL) |
| 106 | *modlistptr = (void *)modptr; |
| 107 | else |
| 108 | prevmodptr->next = modptr; |
| 109 | prevmodptr = modptr; |
| 110 | offset = (unsigned int)ldiptr->ldinfo_next; |
| 111 | ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset); |
| 112 | } while (offset); |
| 113 | free(ldibuf); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int |
| 118 | aix_bindnewmodule(newmoduleptr, modlistptr) |
| 119 | void *newmoduleptr; |
| 120 | void *modlistptr; |
| 121 | { |
| 122 | register ModulePtr modptr; |
| 123 | |
| 124 | /* |
| 125 | -- Bind the new module with the list of loaded modules. |
| 126 | */ |
| 127 | for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next) |
| 128 | if (loadbind(0, modptr->entry, newmoduleptr) != 0) |
| 129 | return -1; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static void |
| 134 | aix_loaderror(pathname) |
| 135 | char *pathname; |
| 136 | { |
| 137 | |
| 138 | char *message[1024], errbuf[1024]; |
| 139 | register int i,j; |
| 140 | |
| 141 | struct errtab { |
| 142 | int errNo; |
| 143 | char *errstr; |
| 144 | } load_errtab[] = { |
| 145 | {L_ERROR_TOOMANY, "too many errors, rest skipped."}, |
| 146 | {L_ERROR_NOLIB, "can't load library:"}, |
| 147 | {L_ERROR_UNDEF, "can't find symbol in library:"}, |
| 148 | {L_ERROR_RLDBAD, |
| 149 | "RLD index out of range or bad relocation type:"}, |
| 150 | {L_ERROR_FORMAT, "not a valid, executable xcoff file:"}, |
| 151 | {L_ERROR_MEMBER, |
| 152 | "file not an archive or does not contain requested member:"}, |
| 153 | {L_ERROR_TYPE, "symbol table mismatch:"}, |
| 154 | {L_ERROR_ALIGN, "text alignment in file is wrong."}, |
| 155 | {L_ERROR_SYSTEM, "System error:"}, |
| 156 | {L_ERROR_ERRNO, NULL} |
| 157 | }; |
| 158 | |
| 159 | #define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0])) |
| 160 | #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1) |
| 161 | |
| 162 | sprintf(errbuf, "from module %.200s ", pathname); |
| 163 | |
| 164 | if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) { |
| 165 | ERRBUF_APPEND(strerror(errno)); |
| 166 | ERRBUF_APPEND("\n"); |
| 167 | } |
| 168 | for(i = 0; message[i] && *message[i]; i++) { |
| 169 | int nerr = atoi(message[i]); |
| 170 | for (j=0; j<LOAD_ERRTAB_LEN ; j++) { |
| 171 | if (nerr == load_errtab[j].errNo && load_errtab[j].errstr) |
| 172 | ERRBUF_APPEND(load_errtab[j].errstr); |
| 173 | } |
| 174 | while (isdigit(*message[i])) message[i]++ ; |
| 175 | ERRBUF_APPEND(message[i]); |
| 176 | ERRBUF_APPEND("\n"); |
| 177 | } |
| 178 | errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */ |
| 179 | PyErr_SetString(PyExc_ImportError, errbuf); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 184 | dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 185 | const char *pathname, FILE *fp) |
| 186 | { |
| 187 | dl_funcptr p; |
| 188 | |
| 189 | /* |
| 190 | -- Invoke load() with L_NOAUTODEFER leaving the imported symbols |
| 191 | -- of the shared module unresolved. Thus we have to resolve them |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 192 | -- explicitly with loadbind. The new module is loaded, then we |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 193 | -- resolve its symbols using the list of already loaded modules |
| 194 | -- (only those that belong to the python executable). Get these |
| 195 | -- with loadquery(L_GETINFO). |
| 196 | */ |
| 197 | |
| 198 | static void *staticmodlistptr = NULL; |
| 199 | |
| 200 | if (!staticmodlistptr) |
| 201 | if (aix_getoldmodules(&staticmodlistptr) == -1) |
| 202 | return NULL; |
Guido van Rossum | 96b5ee8 | 1999-12-21 15:55:47 +0000 | [diff] [blame] | 203 | p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 204 | if (p == NULL) { |
| 205 | aix_loaderror(pathname); |
| 206 | return NULL; |
| 207 | } |
| 208 | if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) { |
| 209 | aix_loaderror(pathname); |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | return p; |
| 214 | } |