Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
| 16 | |
| 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
| 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Support for dynamic loading of extension modules */ |
| 33 | |
| 34 | #include <ctype.h> /* for isdigit() */ |
| 35 | #include <errno.h> /* for global errno */ |
| 36 | #include <string.h> /* for strerror() */ |
| 37 | #include <stdlib.h> /* for malloc(), free() */ |
| 38 | #include <sys/ldr.h> |
| 39 | |
| 40 | #include "Python.h" |
| 41 | #include "importdl.h" |
| 42 | |
| 43 | |
| 44 | #ifdef AIX_GENUINE_CPLUSPLUS |
| 45 | #include "/usr/lpp/xlC/include/load.h" |
| 46 | #define aix_load loadAndInit |
| 47 | #else |
| 48 | #define aix_load load |
| 49 | #endif |
| 50 | |
| 51 | |
| 52 | extern char *Py_GetProgramName(); |
| 53 | |
| 54 | typedef struct Module { |
| 55 | struct Module *next; |
| 56 | void *entry; |
| 57 | } Module, *ModulePtr; |
| 58 | |
| 59 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
| 60 | {".so", "rb", C_EXTENSION}, |
| 61 | {"module.so", "rb", C_EXTENSION}, |
| 62 | {0, 0} |
| 63 | }; |
| 64 | |
| 65 | static int |
| 66 | aix_getoldmodules(modlistptr) |
| 67 | void **modlistptr; |
| 68 | { |
| 69 | register ModulePtr modptr, prevmodptr; |
| 70 | register struct ld_info *ldiptr; |
| 71 | register char *ldibuf; |
| 72 | register int errflag, bufsize = 1024; |
| 73 | register unsigned int offset; |
| 74 | char *progname = Py_GetProgramName(); |
| 75 | |
| 76 | /* |
| 77 | -- Get the list of loaded modules into ld_info structures. |
| 78 | */ |
| 79 | if ((ldibuf = malloc(bufsize)) == NULL) { |
| 80 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
| 81 | return -1; |
| 82 | } |
| 83 | while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1 |
| 84 | && errno == ENOMEM) { |
| 85 | free(ldibuf); |
| 86 | bufsize += 1024; |
| 87 | if ((ldibuf = malloc(bufsize)) == NULL) { |
| 88 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
| 89 | return -1; |
| 90 | } |
| 91 | } |
| 92 | if (errflag == -1) { |
| 93 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
| 94 | return -1; |
| 95 | } |
| 96 | /* |
| 97 | -- Make the modules list from the ld_info structures. |
| 98 | */ |
| 99 | ldiptr = (struct ld_info *)ldibuf; |
| 100 | prevmodptr = NULL; |
| 101 | do { |
| 102 | if (strstr(progname, ldiptr->ldinfo_filename) == NULL && |
| 103 | strstr(ldiptr->ldinfo_filename, "python") == NULL) { |
| 104 | /* |
| 105 | -- Extract only the modules belonging to the main |
| 106 | -- executable + those containing "python" as a |
| 107 | -- substring (like the "python[version]" binary or |
| 108 | -- "libpython[version].a" in case it's a shared lib). |
| 109 | */ |
| 110 | offset = (unsigned int)ldiptr->ldinfo_next; |
| 111 | ldiptr = (struct ld_info *)((unsigned int) |
| 112 | ldiptr + offset); |
| 113 | continue; |
| 114 | } |
| 115 | if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) { |
| 116 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
| 117 | while (*modlistptr) { |
| 118 | modptr = (ModulePtr)*modlistptr; |
| 119 | *modlistptr = (void *)modptr->next; |
| 120 | free(modptr); |
| 121 | } |
| 122 | return -1; |
| 123 | } |
| 124 | modptr->entry = ldiptr->ldinfo_dataorg; |
| 125 | modptr->next = NULL; |
| 126 | if (prevmodptr == NULL) |
| 127 | *modlistptr = (void *)modptr; |
| 128 | else |
| 129 | prevmodptr->next = modptr; |
| 130 | prevmodptr = modptr; |
| 131 | offset = (unsigned int)ldiptr->ldinfo_next; |
| 132 | ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset); |
| 133 | } while (offset); |
| 134 | free(ldibuf); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int |
| 139 | aix_bindnewmodule(newmoduleptr, modlistptr) |
| 140 | void *newmoduleptr; |
| 141 | void *modlistptr; |
| 142 | { |
| 143 | register ModulePtr modptr; |
| 144 | |
| 145 | /* |
| 146 | -- Bind the new module with the list of loaded modules. |
| 147 | */ |
| 148 | for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next) |
| 149 | if (loadbind(0, modptr->entry, newmoduleptr) != 0) |
| 150 | return -1; |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | aix_loaderror(pathname) |
| 156 | char *pathname; |
| 157 | { |
| 158 | |
| 159 | char *message[1024], errbuf[1024]; |
| 160 | register int i,j; |
| 161 | |
| 162 | struct errtab { |
| 163 | int errNo; |
| 164 | char *errstr; |
| 165 | } load_errtab[] = { |
| 166 | {L_ERROR_TOOMANY, "too many errors, rest skipped."}, |
| 167 | {L_ERROR_NOLIB, "can't load library:"}, |
| 168 | {L_ERROR_UNDEF, "can't find symbol in library:"}, |
| 169 | {L_ERROR_RLDBAD, |
| 170 | "RLD index out of range or bad relocation type:"}, |
| 171 | {L_ERROR_FORMAT, "not a valid, executable xcoff file:"}, |
| 172 | {L_ERROR_MEMBER, |
| 173 | "file not an archive or does not contain requested member:"}, |
| 174 | {L_ERROR_TYPE, "symbol table mismatch:"}, |
| 175 | {L_ERROR_ALIGN, "text alignment in file is wrong."}, |
| 176 | {L_ERROR_SYSTEM, "System error:"}, |
| 177 | {L_ERROR_ERRNO, NULL} |
| 178 | }; |
| 179 | |
| 180 | #define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0])) |
| 181 | #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1) |
| 182 | |
| 183 | sprintf(errbuf, "from module %.200s ", pathname); |
| 184 | |
| 185 | if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) { |
| 186 | ERRBUF_APPEND(strerror(errno)); |
| 187 | ERRBUF_APPEND("\n"); |
| 188 | } |
| 189 | for(i = 0; message[i] && *message[i]; i++) { |
| 190 | int nerr = atoi(message[i]); |
| 191 | for (j=0; j<LOAD_ERRTAB_LEN ; j++) { |
| 192 | if (nerr == load_errtab[j].errNo && load_errtab[j].errstr) |
| 193 | ERRBUF_APPEND(load_errtab[j].errstr); |
| 194 | } |
| 195 | while (isdigit(*message[i])) message[i]++ ; |
| 196 | ERRBUF_APPEND(message[i]); |
| 197 | ERRBUF_APPEND("\n"); |
| 198 | } |
| 199 | errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */ |
| 200 | PyErr_SetString(PyExc_ImportError, errbuf); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | dl_funcptr _PyImport_GetDynLoadFunc(const char *name, const char *funcname, |
| 206 | const char *pathname, FILE *fp) |
| 207 | { |
| 208 | dl_funcptr p; |
| 209 | |
| 210 | /* |
| 211 | -- Invoke load() with L_NOAUTODEFER leaving the imported symbols |
| 212 | -- of the shared module unresolved. Thus we have to resolve them |
| 213 | -- explicitely with loadbind. The new module is loaded, then we |
| 214 | -- resolve its symbols using the list of already loaded modules |
| 215 | -- (only those that belong to the python executable). Get these |
| 216 | -- with loadquery(L_GETINFO). |
| 217 | */ |
| 218 | |
| 219 | static void *staticmodlistptr = NULL; |
| 220 | |
| 221 | if (!staticmodlistptr) |
| 222 | if (aix_getoldmodules(&staticmodlistptr) == -1) |
| 223 | return NULL; |
Guido van Rossum | 96b5ee8 | 1999-12-21 15:55:47 +0000 | [diff] [blame^] | 224 | p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 225 | if (p == NULL) { |
| 226 | aix_loaderror(pathname); |
| 227 | return NULL; |
| 228 | } |
| 229 | if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) { |
| 230 | aix_loaderror(pathname); |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | return p; |
| 235 | } |