Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Support for dynamic loading of extension modules */ |
| 33 | /* If no dynamic linking is supported, this file still generates some code! */ |
| 34 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 35 | #include "Python.h" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 36 | #include "osdefs.h" |
| 37 | #include "importdl.h" |
| 38 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 39 | /* Explanation of some of the the various #defines used by dynamic linking... |
| 40 | |
| 41 | symbol -- defined for: |
| 42 | |
| 43 | DYNAMIC_LINK -- any kind of dynamic linking |
Guido van Rossum | 75f288d | 1995-06-14 22:07:26 +0000 | [diff] [blame] | 44 | USE_RLD -- NeXT dynamic linking |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 45 | USE_DL -- Jack's dl for IRIX 4 or GNU dld with emulation for Jack's dl |
| 46 | USE_SHLIB -- SunOS or IRIX 5 (SVR4?) shared libraries |
| 47 | _AIX -- AIX style dynamic linking |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 48 | MS_WIN32 -- Windows NT style dynamic linking (using DLLs) |
| 49 | MS_WIN16 -- Windows 16-bit dynamic linking (using DLLs) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 50 | _DL_FUNCPTR_DEFINED -- if the typedef dl_funcptr has been defined |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 51 | USE_MAC_DYNAMIC_LOADING -- Mac CFM shared libraries |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 52 | SHORT_EXT -- short extension for dynamic module, e.g. ".so" |
| 53 | LONG_EXT -- long extension, e.g. "module.so" |
| 54 | hpux -- HP-UX Dynamic Linking - defined by the compiler |
Guido van Rossum | 46c76a6 | 1995-01-20 16:53:54 +0000 | [diff] [blame] | 55 | __NetBSD__ -- NetBSD shared libraries (not quite SVR4 compatible) |
Guido van Rossum | 25e8529 | 1996-02-25 05:02:29 +0000 | [diff] [blame] | 56 | __FreeBSD__ -- FreeBSD shared libraries |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 57 | |
| 58 | (The other WITH_* symbols are used only once, to set the |
| 59 | appropriate symbols.) |
| 60 | */ |
| 61 | |
| 62 | /* Configure dynamic linking */ |
| 63 | |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 64 | #ifdef __hpux |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 65 | #ifndef hpux |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 66 | #define hpux |
| 67 | #endif |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 68 | #endif |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 70 | #ifdef hpux |
| 71 | #define DYNAMIC_LINK |
| 72 | #include <errno.h> |
| 73 | typedef void (*dl_funcptr)(); |
| 74 | #define _DL_FUNCPTR_DEFINED 1 |
| 75 | #define SHORT_EXT ".sl" |
| 76 | #define LONG_EXT "module.sl" |
| 77 | #endif |
| 78 | |
Guido van Rossum | 9218b70 | 1997-05-05 15:03:26 +0000 | [diff] [blame] | 79 | #if defined(__NetBSD__) |
Guido van Rossum | 46c76a6 | 1995-01-20 16:53:54 +0000 | [diff] [blame] | 80 | #define DYNAMIC_LINK |
| 81 | #define USE_SHLIB |
| 82 | |
| 83 | #define dlerror() "error in dynamic linking" |
| 84 | #endif |
| 85 | |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 86 | #ifdef MS_WINDOWS /* i.e. MS_WIN32 or MS_WIN16 */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 87 | #define DYNAMIC_LINK |
| 88 | #include <windows.h> |
| 89 | typedef FARPROC dl_funcptr; |
| 90 | #define _DL_FUNCPTR_DEFINED |
Guido van Rossum | 5fb1da7 | 1995-01-07 12:36:02 +0000 | [diff] [blame] | 91 | #define SHORT_EXT ".pyd" |
Guido van Rossum | e71a947 | 1996-04-09 02:39:15 +0000 | [diff] [blame] | 92 | #define LONG_EXT ".dll" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 93 | #endif |
| 94 | |
Guido van Rossum | 75f288d | 1995-06-14 22:07:26 +0000 | [diff] [blame] | 95 | #ifdef NeXT |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 96 | #define DYNAMIC_LINK |
| 97 | #define USE_RLD |
Guido van Rossum | 54dec59 | 1997-08-16 14:38:09 +0000 | [diff] [blame] | 98 | #define SHORT_EXT ".so" |
| 99 | #define LONG_EXT "module.so" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 100 | #endif |
| 101 | |
| 102 | #ifdef WITH_SGI_DL |
| 103 | #define DYNAMIC_LINK |
| 104 | #define USE_DL |
| 105 | #endif |
| 106 | |
| 107 | #ifdef WITH_DL_DLD |
| 108 | #define DYNAMIC_LINK |
| 109 | #define USE_DL |
| 110 | #endif |
| 111 | |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 112 | #ifdef USE_MAC_DYNAMIC_LOADING |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 113 | #define DYNAMIC_LINK |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 114 | #define SHORT_EXT ".slb" |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 115 | #ifdef __CFM68K__ |
| 116 | #define LONG_EXT ".CFM68K.slb" |
| 117 | #else |
| 118 | #define LONG_EXT ".ppc.slb" |
| 119 | #endif |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 120 | #ifndef _DL_FUNCPTR_DEFINED |
| 121 | typedef void (*dl_funcptr)(); |
| 122 | #endif |
| 123 | #endif |
| 124 | |
Guido van Rossum | 504f4a9 | 1996-08-20 19:59:07 +0000 | [diff] [blame] | 125 | #if !defined(DYNAMIC_LINK) && (defined(HAVE_DLOPEN) || defined(M_UNIX)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 126 | #define DYNAMIC_LINK |
| 127 | #define USE_SHLIB |
| 128 | #endif |
| 129 | |
| 130 | #ifdef _AIX |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 131 | #undef USE_SHLIB /* AIX 4.2 and higher have dlfcn.h but we don't want it */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 132 | #define DYNAMIC_LINK |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 133 | #define SHORT_EXT ".so" |
| 134 | #define LONG_EXT "module.so" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 135 | #include <sys/ldr.h> |
| 136 | typedef void (*dl_funcptr)(); |
| 137 | #define _DL_FUNCPTR_DEFINED |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 138 | static int aix_getoldmodules(void **); |
| 139 | static int aix_bindnewmodule(void *, void *); |
| 140 | static void aix_loaderror(char *); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
| 143 | #ifdef DYNAMIC_LINK |
| 144 | |
| 145 | #ifdef USE_SHLIB |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 146 | #include <sys/types.h> |
| 147 | #include <sys/stat.h> |
Guido van Rossum | 9218b70 | 1997-05-05 15:03:26 +0000 | [diff] [blame] | 148 | #if defined(__NetBSD__) |
Guido van Rossum | 46c76a6 | 1995-01-20 16:53:54 +0000 | [diff] [blame] | 149 | #include <nlist.h> |
| 150 | #include <link.h> |
| 151 | #else |
Guido van Rossum | 504f4a9 | 1996-08-20 19:59:07 +0000 | [diff] [blame] | 152 | #ifdef HAVE_DLFCN_H |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 153 | #include <dlfcn.h> |
Guido van Rossum | 46c76a6 | 1995-01-20 16:53:54 +0000 | [diff] [blame] | 154 | #endif |
Guido van Rossum | 504f4a9 | 1996-08-20 19:59:07 +0000 | [diff] [blame] | 155 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 156 | #ifndef _DL_FUNCPTR_DEFINED |
| 157 | typedef void (*dl_funcptr)(); |
| 158 | #endif |
| 159 | #ifndef RTLD_LAZY |
| 160 | #define RTLD_LAZY 1 |
| 161 | #endif |
| 162 | #define SHORT_EXT ".so" |
| 163 | #define LONG_EXT "module.so" |
| 164 | #endif /* USE_SHLIB */ |
| 165 | |
| 166 | #if defined(USE_DL) || defined(hpux) |
| 167 | #include "dl.h" |
| 168 | #endif |
| 169 | |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 170 | #ifdef USE_MAC_DYNAMIC_LOADING |
Jack Jansen | 0a72e8d | 1995-10-23 13:54:01 +0000 | [diff] [blame] | 171 | #include <Aliases.h> |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 172 | #include <CodeFragments.h> |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 173 | #ifdef SYMANTEC__CFM68K__ /* Really an older version of Universal Headers */ |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 174 | #define CFragConnectionID ConnectionID |
| 175 | #define kLoadCFrag 0x01 |
| 176 | #endif |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 177 | #include <Files.h> |
| 178 | #include "macdefs.h" |
| 179 | #include "macglue.h" |
| 180 | #endif |
| 181 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 182 | #ifdef USE_RLD |
| 183 | #include <mach-o/rld.h> |
| 184 | #define FUNCNAME_PATTERN "_init%.200s" |
| 185 | #ifndef _DL_FUNCPTR_DEFINED |
| 186 | typedef void (*dl_funcptr)(); |
| 187 | #endif |
| 188 | #endif /* USE_RLD */ |
| 189 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 190 | extern char *Py_GetProgramName(); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 191 | |
| 192 | #ifndef FUNCNAME_PATTERN |
Guido van Rossum | 25e8529 | 1996-02-25 05:02:29 +0000 | [diff] [blame] | 193 | #if defined(__hp9000s300) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__BORLANDC__) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 194 | #define FUNCNAME_PATTERN "_init%.200s" |
| 195 | #else |
| 196 | #define FUNCNAME_PATTERN "init%.200s" |
| 197 | #endif |
| 198 | #endif |
| 199 | |
| 200 | #if !defined(SHORT_EXT) && !defined(LONG_EXT) |
| 201 | #define SHORT_EXT ".o" |
| 202 | #define LONG_EXT "module.o" |
| 203 | #endif /* !SHORT_EXT && !LONG_EXT */ |
| 204 | |
| 205 | #endif /* DYNAMIC_LINK */ |
| 206 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 207 | struct filedescr _PyImport_Filetab[] = { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 208 | #ifdef SHORT_EXT |
| 209 | {SHORT_EXT, "rb", C_EXTENSION}, |
| 210 | #endif /* !SHORT_EXT */ |
| 211 | #ifdef LONG_EXT |
| 212 | {LONG_EXT, "rb", C_EXTENSION}, |
| 213 | #endif /* !LONG_EXT */ |
| 214 | {".py", "r", PY_SOURCE}, |
| 215 | {".pyc", "rb", PY_COMPILED}, |
| 216 | {0, 0} |
| 217 | }; |
| 218 | |
Guido van Rossum | 3823420 | 1996-07-31 17:55:19 +0000 | [diff] [blame] | 219 | #ifdef NO_DYNAMIC_LINK |
| 220 | #undef DYNAMIC_LINK |
| 221 | #endif |
| 222 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 223 | PyObject * |
| 224 | _PyImport_LoadDynamicModule(name, pathname, fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 225 | char *name; |
| 226 | char *pathname; |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 227 | FILE *fp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 228 | { |
| 229 | #ifndef DYNAMIC_LINK |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 230 | PyErr_SetString(PyExc_ImportError, |
| 231 | "dynamically linked modules not supported"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 232 | return NULL; |
| 233 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 234 | PyObject *m, *d, *s; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 235 | char funcname[258]; |
| 236 | dl_funcptr p = NULL; |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 237 | #ifdef USE_SHLIB |
| 238 | static struct { |
| 239 | dev_t dev; |
| 240 | ino_t ino; |
| 241 | void *handle; |
| 242 | } handles[128]; |
| 243 | static int nhandles = 0; |
Guido van Rossum | 0bbf253 | 1996-08-09 20:55:05 +0000 | [diff] [blame] | 244 | char pathbuf[260]; |
| 245 | if (strchr(pathname, '/') == NULL) { |
| 246 | /* Prefix bare filename with "./" */ |
| 247 | sprintf(pathbuf, "./%-.255s", pathname); |
| 248 | pathname = pathbuf; |
| 249 | } |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 250 | #endif |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 251 | if ((m = _PyImport_FindExtension(name, pathname)) != NULL) { |
| 252 | Py_INCREF(m); |
| 253 | return m; |
| 254 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 255 | sprintf(funcname, FUNCNAME_PATTERN, name); |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 256 | #ifdef USE_SHLIB |
| 257 | if (fp != NULL) { |
| 258 | int i; |
| 259 | struct stat statb; |
| 260 | fstat(fileno(fp), &statb); |
| 261 | for (i = 0; i < nhandles; i++) { |
| 262 | if (statb.st_dev == handles[i].dev && |
| 263 | statb.st_ino == handles[i].ino) { |
| 264 | p = (dl_funcptr) dlsym(handles[i].handle, |
| 265 | funcname); |
| 266 | goto got_it; |
| 267 | } |
| 268 | } |
| 269 | if (nhandles < 128) { |
| 270 | handles[nhandles].dev = statb.st_dev; |
| 271 | handles[nhandles].ino = statb.st_ino; |
| 272 | } |
| 273 | } |
| 274 | #endif /* USE_SHLIB */ |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 275 | #ifdef USE_MAC_DYNAMIC_LOADING |
| 276 | /* |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 277 | ** Dynamic loading of CFM shared libraries on the Mac. The |
| 278 | ** code has become more convoluted than it was, because we |
| 279 | ** want to be able to put multiple modules in a single |
| 280 | ** file. For this reason, we have to determine the fragment |
| 281 | ** name, and we cannot use the library entry point but we have |
| 282 | ** to locate the correct init routine "by hand". |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 283 | */ |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 284 | { |
| 285 | FSSpec libspec; |
| 286 | CFragConnectionID connID; |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 287 | Ptr mainAddr; |
| 288 | Str255 errMessage; |
| 289 | OSErr err; |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 290 | Boolean isfolder, didsomething; |
| 291 | char buf[512]; |
| 292 | Str63 fragname; |
| 293 | Ptr symAddr; |
| 294 | CFragSymbolClass class; |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 295 | |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 296 | /* First resolve any aliases to find the real file */ |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 297 | (void)FSMakeFSSpec(0, 0, Pstring(pathname), &libspec); |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 298 | err = ResolveAliasFile(&libspec, 1, &isfolder, &didsomething); |
| 299 | if ( err ) { |
Guido van Rossum | bc2472d | 1997-04-30 19:07:54 +0000 | [diff] [blame] | 300 | sprintf(buf, "%.255s: %.200s", |
| 301 | pathname, PyMac_StrError(err)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 302 | PyErr_SetString(PyExc_ImportError, buf); |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 303 | return NULL; |
| 304 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 305 | /* Next, determine the fragment name, |
| 306 | by stripping '.slb' and 'module' */ |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 307 | memcpy(fragname+1, libspec.name+1, libspec.name[0]); |
| 308 | fragname[0] = libspec.name[0]; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 309 | if( strncmp((char *)(fragname+1+fragname[0]-4), |
| 310 | ".slb", 4) == 0 ) |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 311 | fragname[0] -= 4; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 312 | if ( strncmp((char *)(fragname+1+fragname[0]-6), |
| 313 | "module", 6) == 0 ) |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 314 | fragname[0] -= 6; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 315 | /* Load the fragment |
| 316 | (or return the connID if it is already loaded */ |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 317 | err = GetDiskFragment(&libspec, 0, 0, fragname, |
Guido van Rossum | 6a75d26 | 1995-02-18 14:51:15 +0000 | [diff] [blame] | 318 | kLoadCFrag, &connID, &mainAddr, |
| 319 | errMessage); |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 320 | if ( err ) { |
Guido van Rossum | bc2472d | 1997-04-30 19:07:54 +0000 | [diff] [blame] | 321 | sprintf(buf, "%.*s: %.200s", |
| 322 | errMessage[0], errMessage+1, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 323 | PyMac_StrError(err)); |
| 324 | PyErr_SetString(PyExc_ImportError, buf); |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 325 | return NULL; |
| 326 | } |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 327 | /* Locate the address of the correct init function */ |
| 328 | err = FindSymbol(connID, Pstring(funcname), &symAddr, &class); |
| 329 | if ( err ) { |
Guido van Rossum | bc2472d | 1997-04-30 19:07:54 +0000 | [diff] [blame] | 330 | sprintf(buf, "%s: %.200s", |
| 331 | funcname, PyMac_StrError(err)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 332 | PyErr_SetString(PyExc_ImportError, buf); |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 333 | return NULL; |
| 334 | } |
| 335 | p = (dl_funcptr)symAddr; |
Jack Jansen | 4e04373 | 1995-02-13 22:42:34 +0000 | [diff] [blame] | 336 | } |
Jack Jansen | 5d9acb6 | 1995-06-14 14:54:25 +0000 | [diff] [blame] | 337 | #endif /* USE_MAC_DYNAMIC_LOADING */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 338 | #ifdef USE_SHLIB |
| 339 | { |
| 340 | #ifdef RTLD_NOW |
| 341 | /* RTLD_NOW: resolve externals now |
| 342 | (i.e. core dump now if some are missing) */ |
| 343 | void *handle = dlopen(pathname, RTLD_NOW); |
| 344 | #else |
| 345 | void *handle; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 346 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 347 | printf("dlopen(\"%s\", %d);\n", pathname, RTLD_LAZY); |
| 348 | handle = dlopen(pathname, RTLD_LAZY); |
| 349 | #endif /* RTLD_NOW */ |
| 350 | if (handle == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 351 | PyErr_SetString(PyExc_ImportError, dlerror()); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 352 | return NULL; |
| 353 | } |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 354 | if (fp != NULL && nhandles < 128) |
| 355 | handles[nhandles++].handle = handle; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 356 | p = (dl_funcptr) dlsym(handle, funcname); |
| 357 | } |
| 358 | #endif /* USE_SHLIB */ |
| 359 | #ifdef _AIX |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 360 | /* |
| 361 | -- Invoke load() with L_NOAUTODEFER leaving the imported symbols |
| 362 | -- of the shared module unresolved. Thus we have to resolve them |
| 363 | -- explicitely with loadbind. The new module is loaded, then we |
| 364 | -- resolve its symbols using the list of already loaded modules |
| 365 | -- (only those that belong to the python executable). Get these |
| 366 | -- with loadquery(L_GETINFO). |
| 367 | */ |
| 368 | { |
| 369 | static void *staticmodlistptr = NULL; |
| 370 | |
| 371 | if (!staticmodlistptr) |
| 372 | if (aix_getoldmodules(&staticmodlistptr) == -1) |
| 373 | return NULL; |
| 374 | p = (dl_funcptr) load(pathname, L_NOAUTODEFER, 0); |
| 375 | if (p == NULL) { |
| 376 | aix_loaderror(pathname); |
| 377 | return NULL; |
| 378 | } |
| 379 | if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) { |
| 380 | aix_loaderror(pathname); |
| 381 | return NULL; |
| 382 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 383 | } |
| 384 | #endif /* _AIX */ |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 385 | #ifdef MS_WIN32 |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 386 | { |
| 387 | HINSTANCE hDLL; |
| 388 | hDLL = LoadLibrary(pathname); |
| 389 | if (hDLL==NULL){ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 390 | char errBuf[256]; |
| 391 | unsigned int errorCode; |
| 392 | |
| 393 | /* Get an error string from Win32 error code */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 394 | char theInfo[256]; /* Pointer to error text |
| 395 | from system */ |
| 396 | int theLength; /* Length of error text */ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 397 | |
| 398 | errorCode = GetLastError(); |
| 399 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 400 | theLength = FormatMessage( |
| 401 | FORMAT_MESSAGE_FROM_SYSTEM, /* flags */ |
| 402 | NULL, /* message source */ |
| 403 | errorCode, /* the message (error) ID */ |
| 404 | 0, /* default language environment */ |
| 405 | (LPTSTR) theInfo, /* the buffer */ |
| 406 | sizeof(theInfo), /* the buffer size */ |
| 407 | NULL); /* no additional format args. */ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 408 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 409 | /* Problem: could not get the error message. |
| 410 | This should not happen if called correctly. */ |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 411 | if (theLength == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 412 | sprintf(errBuf, |
| 413 | "DLL load failed with error code %d", |
| 414 | errorCode); |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 415 | } else { |
| 416 | int len; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 417 | /* For some reason a \r\n |
| 418 | is appended to the text */ |
| 419 | if (theLength >= 2 && |
| 420 | theInfo[theLength-2] == '\r' && |
| 421 | theInfo[theLength-1] == '\n') { |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 422 | theLength -= 2; |
| 423 | theInfo[theLength] = '\0'; |
| 424 | } |
| 425 | strcpy(errBuf, "DLL load failed: "); |
| 426 | len = strlen(errBuf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 427 | strncpy(errBuf+len, theInfo, |
| 428 | sizeof(errBuf)-len); |
Guido van Rossum | 11a3f0c2 | 1995-07-18 14:40:09 +0000 | [diff] [blame] | 429 | errBuf[sizeof(errBuf)-1] = '\0'; |
| 430 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 431 | PyErr_SetString(PyExc_ImportError, errBuf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 432 | return NULL; |
| 433 | } |
| 434 | p = GetProcAddress(hDLL, funcname); |
| 435 | } |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 436 | #endif /* MS_WIN32 */ |
| 437 | #ifdef MS_WIN16 |
Guido van Rossum | dadc824 | 1996-05-23 22:51:40 +0000 | [diff] [blame] | 438 | { |
| 439 | HINSTANCE hDLL; |
| 440 | hDLL = LoadLibrary(pathname); |
| 441 | if (hDLL < HINSTANCE_ERROR){ |
| 442 | char errBuf[256]; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 443 | sprintf(errBuf, |
| 444 | "DLL load failed with error code %d", hDLL); |
| 445 | PyErr_SetString(PyExc_ImportError, errBuf); |
Guido van Rossum | dadc824 | 1996-05-23 22:51:40 +0000 | [diff] [blame] | 446 | return NULL; |
| 447 | } |
| 448 | p = GetProcAddress(hDLL, funcname); |
| 449 | } |
Guido van Rossum | 9b38a14 | 1996-09-11 23:12:24 +0000 | [diff] [blame] | 450 | #endif /* MS_WIN16 */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 451 | #ifdef USE_DL |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 452 | p = dl_loadmod(Py_GetProgramName(), pathname, funcname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 453 | #endif /* USE_DL */ |
| 454 | #ifdef USE_RLD |
| 455 | { |
| 456 | NXStream *errorStream; |
| 457 | struct mach_header *new_header; |
| 458 | const char *filenames[2]; |
| 459 | long ret; |
| 460 | unsigned long ptr; |
| 461 | |
| 462 | errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY); |
| 463 | filenames[0] = pathname; |
| 464 | filenames[1] = NULL; |
| 465 | ret = rld_load(errorStream, &new_header, |
| 466 | filenames, NULL); |
| 467 | |
| 468 | /* extract the error messages for the exception */ |
| 469 | if(!ret) { |
| 470 | char *streamBuf; |
| 471 | int len, maxLen; |
| 472 | |
| 473 | NXPutc(errorStream, (char)0); |
| 474 | |
| 475 | NXGetMemoryBuffer(errorStream, |
| 476 | &streamBuf, &len, &maxLen); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 477 | PyErr_SetString(PyExc_ImportError, streamBuf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | if(ret && rld_lookup(errorStream, funcname, &ptr)) |
| 481 | p = (dl_funcptr) ptr; |
| 482 | |
| 483 | NXCloseMemory(errorStream, NX_FREEBUFFER); |
| 484 | |
| 485 | if(!ret) |
| 486 | return NULL; |
| 487 | } |
| 488 | #endif /* USE_RLD */ |
| 489 | #ifdef hpux |
| 490 | { |
| 491 | shl_t lib; |
| 492 | int flags; |
| 493 | |
Guido van Rossum | 3afb595 | 1996-12-05 23:15:35 +0000 | [diff] [blame] | 494 | flags = BIND_FIRST | BIND_DEFERRED; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 495 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 496 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 497 | flags = DYNAMIC_PATH | BIND_FIRST | BIND_IMMEDIATE | |
| 498 | BIND_NONFATAL | BIND_VERBOSE; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 499 | printf("shl_load %s\n",pathname); |
| 500 | } |
| 501 | lib = shl_load(pathname, flags, 0); |
| 502 | if (lib == NULL) |
| 503 | { |
| 504 | char buf[256]; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 505 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 506 | perror(pathname); |
| 507 | sprintf(buf, "Failed to load %.200s", pathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 508 | PyErr_SetString(PyExc_ImportError, buf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 509 | return NULL; |
| 510 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 511 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 512 | printf("shl_findsym %s\n", funcname); |
| 513 | shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 514 | if (p == NULL && Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 515 | perror(funcname); |
| 516 | } |
| 517 | #endif /* hpux */ |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 518 | #ifdef USE_SHLIB |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 519 | got_it: |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 520 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 521 | if (p == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 522 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 523 | "dynamic module does not define init function"); |
| 524 | return NULL; |
| 525 | } |
| 526 | (*p)(); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 527 | if (PyErr_Occurred()) |
| 528 | return NULL; |
| 529 | if (_PyImport_FixupExtension(name, pathname) == NULL) |
| 530 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 531 | |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 532 | m = PyDict_GetItemString(PyImport_GetModuleDict(), name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 533 | if (m == NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 534 | PyErr_SetString(PyExc_SystemError, |
| 535 | "dynamic module not initialized properly"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 536 | return NULL; |
| 537 | } |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 538 | /* Remember the filename as the __file__ attribute */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 539 | d = PyModule_GetDict(m); |
| 540 | s = PyString_FromString(pathname); |
| 541 | if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0) |
| 542 | PyErr_Clear(); /* Not important enough to report */ |
| 543 | Py_XDECREF(s); |
| 544 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 545 | fprintf(stderr, |
| 546 | "import %s # dynamically loaded from %s\n", |
| 547 | name, pathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 548 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 549 | return m; |
| 550 | #endif /* DYNAMIC_LINK */ |
| 551 | } |
| 552 | |
| 553 | |
| 554 | #ifdef _AIX |
| 555 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 556 | #include <ctype.h> /* for isdigit() */ |
| 557 | #include <errno.h> /* for global errno */ |
| 558 | #include <string.h> /* for strerror() */ |
| 559 | #include <stdlib.h> /* for malloc(), free() */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 560 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 561 | typedef struct Module { |
| 562 | struct Module *next; |
| 563 | void *entry; |
| 564 | } Module, *ModulePtr; |
| 565 | |
| 566 | static int |
| 567 | aix_getoldmodules(modlistptr) |
| 568 | void **modlistptr; |
| 569 | { |
| 570 | register ModulePtr modptr, prevmodptr; |
| 571 | register struct ld_info *ldiptr; |
| 572 | register char *ldibuf; |
| 573 | register int errflag, bufsize = 1024; |
| 574 | register unsigned int offset; |
| 575 | |
| 576 | /* |
| 577 | -- Get the list of loaded modules into ld_info structures. |
| 578 | */ |
| 579 | if ((ldibuf = malloc(bufsize)) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 580 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 581 | return -1; |
| 582 | } |
| 583 | while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1 |
| 584 | && errno == ENOMEM) { |
| 585 | free(ldibuf); |
| 586 | bufsize += 1024; |
| 587 | if ((ldibuf = malloc(bufsize)) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 588 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 589 | return -1; |
| 590 | } |
| 591 | } |
| 592 | if (errflag == -1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 593 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 594 | return -1; |
| 595 | } |
| 596 | /* |
| 597 | -- Make the modules list from the ld_info structures. |
| 598 | */ |
| 599 | ldiptr = (struct ld_info *)ldibuf; |
| 600 | prevmodptr = NULL; |
| 601 | do { |
| 602 | if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 603 | PyErr_SetString(PyExc_ImportError, strerror(errno)); |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 604 | while (*modlistptr) { |
| 605 | modptr = (ModulePtr)*modlistptr; |
| 606 | *modlistptr = (void *)modptr->next; |
| 607 | free(modptr); |
| 608 | } |
| 609 | return -1; |
| 610 | } |
| 611 | modptr->entry = ldiptr->ldinfo_dataorg; |
| 612 | modptr->next = NULL; |
| 613 | if (prevmodptr == NULL) |
| 614 | *modlistptr = (void *)modptr; |
| 615 | else |
| 616 | prevmodptr->next = modptr; |
| 617 | prevmodptr = modptr; |
| 618 | offset = (unsigned int)ldiptr->ldinfo_next; |
| 619 | ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset); |
| 620 | } while (offset); |
| 621 | free(ldibuf); |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int |
| 626 | aix_bindnewmodule(newmoduleptr, modlistptr) |
| 627 | void *newmoduleptr; |
| 628 | void *modlistptr; |
| 629 | { |
| 630 | register ModulePtr modptr; |
| 631 | |
| 632 | /* |
| 633 | -- Bind the new module with the list of loaded modules. |
| 634 | */ |
| 635 | for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next) |
| 636 | if (loadbind(0, modptr->entry, newmoduleptr) != 0) |
| 637 | return -1; |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static void |
| 642 | aix_loaderror(pathname) |
| 643 | char *pathname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 644 | { |
| 645 | |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 646 | char *message[1024], errbuf[1024]; |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 647 | register int i,j; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 648 | |
| 649 | struct errtab { |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 650 | int errNo; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 651 | char *errstr; |
| 652 | } load_errtab[] = { |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 653 | {L_ERROR_TOOMANY, "too many errors, rest skipped."}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 654 | {L_ERROR_NOLIB, "can't load library:"}, |
| 655 | {L_ERROR_UNDEF, "can't find symbol in library:"}, |
| 656 | {L_ERROR_RLDBAD, |
| 657 | "RLD index out of range or bad relocation type:"}, |
| 658 | {L_ERROR_FORMAT, "not a valid, executable xcoff file:"}, |
| 659 | {L_ERROR_MEMBER, |
| 660 | "file not an archive or does not contain requested member:"}, |
| 661 | {L_ERROR_TYPE, "symbol table mismatch:"}, |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 662 | {L_ERROR_ALIGN, "text alignment in file is wrong."}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 663 | {L_ERROR_SYSTEM, "System error:"}, |
| 664 | {L_ERROR_ERRNO, NULL} |
| 665 | }; |
| 666 | |
| 667 | #define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0])) |
| 668 | #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1) |
| 669 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 670 | sprintf(errbuf, "from module %.200s ", pathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 671 | |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 672 | if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 673 | ERRBUF_APPEND(strerror(errno)); |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 674 | ERRBUF_APPEND("\n"); |
| 675 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 676 | for(i = 0; message[i] && *message[i]; i++) { |
| 677 | int nerr = atoi(message[i]); |
| 678 | for (j=0; j<LOAD_ERRTAB_LEN ; j++) { |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 679 | if (nerr == load_errtab[j].errNo && load_errtab[j].errstr) |
Guido van Rossum | 236f62d | 1996-06-26 21:07:08 +0000 | [diff] [blame] | 680 | ERRBUF_APPEND(load_errtab[j].errstr); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 681 | } |
| 682 | while (isdigit(*message[i])) message[i]++ ; |
| 683 | ERRBUF_APPEND(message[i]); |
| 684 | ERRBUF_APPEND("\n"); |
| 685 | } |
| 686 | errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 687 | PyErr_SetString(PyExc_ImportError, errbuf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 688 | return; |
| 689 | } |
| 690 | |
| 691 | #endif /* _AIX */ |