blob: fe025c083837965d52f3fd703df7a0d1ecfaed64 [file] [log] [blame]
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum1ae940a1995-01-02 19:04:15 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum1ae940a1995-01-02 19:04:15 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum1ae940a1995-01-02 19:04:15 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum1ae940a1995-01-02 19:04:15 +000029
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 Rossum79f25d91997-04-29 20:08:16 +000035#include "Python.h"
Guido van Rossumcecadc41998-04-10 23:45:14 +000036
37#ifdef HAVE_SYS_PARAM_H
38/* osdefs.h will define MAXPATHLEN if it's not already defined. */
39#include <sys/param.h>
40#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041#include "osdefs.h"
42#include "importdl.h"
43
Guido van Rossum1ae940a1995-01-02 19:04:15 +000044/* Explanation of some of the the various #defines used by dynamic linking...
45
46 symbol -- defined for:
47
48 DYNAMIC_LINK -- any kind of dynamic linking
Guido van Rossum75f288d1995-06-14 22:07:26 +000049 USE_RLD -- NeXT dynamic linking
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050 USE_DL -- Jack's dl for IRIX 4 or GNU dld with emulation for Jack's dl
51 USE_SHLIB -- SunOS or IRIX 5 (SVR4?) shared libraries
52 _AIX -- AIX style dynamic linking
Guido van Rossum9b38a141996-09-11 23:12:24 +000053 MS_WIN32 -- Windows NT style dynamic linking (using DLLs)
54 MS_WIN16 -- Windows 16-bit dynamic linking (using DLLs)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000055 PYOS_OS2 -- IBM OS/2 dynamic linking (using DLLs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000056 _DL_FUNCPTR_DEFINED -- if the typedef dl_funcptr has been defined
Jack Jansen5d9acb61995-06-14 14:54:25 +000057 USE_MAC_DYNAMIC_LOADING -- Mac CFM shared libraries
Guido van Rossum1ae940a1995-01-02 19:04:15 +000058 SHORT_EXT -- short extension for dynamic module, e.g. ".so"
59 LONG_EXT -- long extension, e.g. "module.so"
60 hpux -- HP-UX Dynamic Linking - defined by the compiler
Guido van Rossum15af20a1998-01-19 22:03:52 +000061 __NetBSD__ -- NetBSD shared libraries
62 (assuming dlerror() was introduced between 1.2 and 1.3)
Guido van Rossum25e85291996-02-25 05:02:29 +000063 __FreeBSD__ -- FreeBSD shared libraries
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064
65 (The other WITH_* symbols are used only once, to set the
66 appropriate symbols.)
67*/
68
69/* Configure dynamic linking */
70
Guido van Rossumff4af061996-01-12 01:17:50 +000071#ifdef __hpux
Guido van Rossum1e612491996-08-19 22:12:10 +000072#ifndef hpux
Guido van Rossumff4af061996-01-12 01:17:50 +000073#define hpux
74#endif
Guido van Rossum1e612491996-08-19 22:12:10 +000075#endif
Guido van Rossumff4af061996-01-12 01:17:50 +000076
Guido van Rossum1ae940a1995-01-02 19:04:15 +000077#ifdef hpux
78#define DYNAMIC_LINK
79#include <errno.h>
80typedef void (*dl_funcptr)();
81#define _DL_FUNCPTR_DEFINED 1
82#define SHORT_EXT ".sl"
83#define LONG_EXT "module.sl"
84#endif
85
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000086#if defined(PYOS_OS2)
87#define DYNAMIC_LINK
88#define INCL_DOSERRORS
89#define INCL_DOSMODULEMGR
90#include <os2.h>
91typedef int (* APIENTRY dl_funcptr)();
92#define _DL_FUNCPTR_DEFINED 1
93#define SHORT_EXT ".pyd"
94#define LONG_EXT ".dll"
95#endif
96
Guido van Rossum15af20a1998-01-19 22:03:52 +000097#if defined(__NetBSD__) && (NetBSD < 199712)
Guido van Rossum46c76a61995-01-20 16:53:54 +000098#define DYNAMIC_LINK
99#define USE_SHLIB
100
101#define dlerror() "error in dynamic linking"
102#endif
103
Guido van Rossum9b38a141996-09-11 23:12:24 +0000104#ifdef MS_WINDOWS /* i.e. MS_WIN32 or MS_WIN16 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000105#define DYNAMIC_LINK
106#include <windows.h>
107typedef FARPROC dl_funcptr;
108#define _DL_FUNCPTR_DEFINED
Guido van Rossum859b16c1998-05-15 20:22:08 +0000109#ifdef _DEBUG
110#define SHORT_EXT "_d.pyd"
111#define LONG_EXT "_d.dll"
112#else
Guido van Rossum5fb1da71995-01-07 12:36:02 +0000113#define SHORT_EXT ".pyd"
Guido van Rossume71a9471996-04-09 02:39:15 +0000114#define LONG_EXT ".dll"
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000115#endif
Guido van Rossum859b16c1998-05-15 20:22:08 +0000116#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000117
Guido van Rossum75f288d1995-06-14 22:07:26 +0000118#ifdef NeXT
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000119#define DYNAMIC_LINK
120#define USE_RLD
Guido van Rossum54dec591997-08-16 14:38:09 +0000121#define SHORT_EXT ".so"
122#define LONG_EXT "module.so"
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000123#endif
124
125#ifdef WITH_SGI_DL
126#define DYNAMIC_LINK
127#define USE_DL
128#endif
129
130#ifdef WITH_DL_DLD
131#define DYNAMIC_LINK
132#define USE_DL
133#endif
134
Jack Jansen5d9acb61995-06-14 14:54:25 +0000135#ifdef USE_MAC_DYNAMIC_LOADING
Jack Jansen4e043731995-02-13 22:42:34 +0000136#define DYNAMIC_LINK
Guido van Rossum6a75d261995-02-18 14:51:15 +0000137#define SHORT_EXT ".slb"
Guido van Rossum1e612491996-08-19 22:12:10 +0000138#ifdef __CFM68K__
139#define LONG_EXT ".CFM68K.slb"
140#else
141#define LONG_EXT ".ppc.slb"
142#endif
Jack Jansen4e043731995-02-13 22:42:34 +0000143#ifndef _DL_FUNCPTR_DEFINED
144typedef void (*dl_funcptr)();
145#endif
146#endif
147
Guido van Rossum504f4a91996-08-20 19:59:07 +0000148#if !defined(DYNAMIC_LINK) && (defined(HAVE_DLOPEN) || defined(M_UNIX))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000149#define DYNAMIC_LINK
150#define USE_SHLIB
151#endif
152
153#ifdef _AIX
Guido van Rossumef3d02e1997-07-21 14:54:36 +0000154#undef USE_SHLIB /* AIX 4.2 and higher have dlfcn.h but we don't want it */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000155#define DYNAMIC_LINK
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000156#define SHORT_EXT ".so"
157#define LONG_EXT "module.so"
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000158#include <sys/ldr.h>
159typedef void (*dl_funcptr)();
160#define _DL_FUNCPTR_DEFINED
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000161static int aix_getoldmodules(void **);
162static int aix_bindnewmodule(void *, void *);
163static void aix_loaderror(char *);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000164#endif
165
166#ifdef DYNAMIC_LINK
167
168#ifdef USE_SHLIB
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000169#include <sys/types.h>
170#include <sys/stat.h>
Guido van Rossum15af20a1998-01-19 22:03:52 +0000171#if defined(__NetBSD__) && (NetBSD < 199712)
Guido van Rossum46c76a61995-01-20 16:53:54 +0000172#include <nlist.h>
173#include <link.h>
174#else
Guido van Rossum504f4a91996-08-20 19:59:07 +0000175#ifdef HAVE_DLFCN_H
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000176#include <dlfcn.h>
Guido van Rossum46c76a61995-01-20 16:53:54 +0000177#endif
Guido van Rossum504f4a91996-08-20 19:59:07 +0000178#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000179#ifndef _DL_FUNCPTR_DEFINED
180typedef void (*dl_funcptr)();
181#endif
182#ifndef RTLD_LAZY
183#define RTLD_LAZY 1
184#endif
185#define SHORT_EXT ".so"
186#define LONG_EXT "module.so"
187#endif /* USE_SHLIB */
188
189#if defined(USE_DL) || defined(hpux)
190#include "dl.h"
191#endif
192
Jack Jansen5d9acb61995-06-14 14:54:25 +0000193#ifdef USE_MAC_DYNAMIC_LOADING
Jack Jansen0a72e8d1995-10-23 13:54:01 +0000194#include <Aliases.h>
Jack Jansen4e043731995-02-13 22:42:34 +0000195#include <CodeFragments.h>
Guido van Rossum79f25d91997-04-29 20:08:16 +0000196#ifdef SYMANTEC__CFM68K__ /* Really an older version of Universal Headers */
Guido van Rossum6a75d261995-02-18 14:51:15 +0000197#define CFragConnectionID ConnectionID
198#define kLoadCFrag 0x01
199#endif
Jack Jansen4e043731995-02-13 22:42:34 +0000200#include <Files.h>
201#include "macdefs.h"
202#include "macglue.h"
203#endif
204
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000205#ifdef USE_RLD
206#include <mach-o/rld.h>
207#define FUNCNAME_PATTERN "_init%.200s"
208#ifndef _DL_FUNCPTR_DEFINED
209typedef void (*dl_funcptr)();
210#endif
211#endif /* USE_RLD */
212
Guido van Rossum79f25d91997-04-29 20:08:16 +0000213extern char *Py_GetProgramName();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000214
215#ifndef FUNCNAME_PATTERN
Guido van Rossum1f740161998-04-13 15:27:21 +0000216#if defined(__hp9000s300) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__BORLANDC__)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000217#define FUNCNAME_PATTERN "_init%.200s"
218#else
219#define FUNCNAME_PATTERN "init%.200s"
220#endif
221#endif
222
223#if !defined(SHORT_EXT) && !defined(LONG_EXT)
224#define SHORT_EXT ".o"
225#define LONG_EXT "module.o"
226#endif /* !SHORT_EXT && !LONG_EXT */
227
228#endif /* DYNAMIC_LINK */
229
Guido van Rossum79f25d91997-04-29 20:08:16 +0000230struct filedescr _PyImport_Filetab[] = {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000231#ifdef SHORT_EXT
232 {SHORT_EXT, "rb", C_EXTENSION},
233#endif /* !SHORT_EXT */
234#ifdef LONG_EXT
235 {LONG_EXT, "rb", C_EXTENSION},
236#endif /* !LONG_EXT */
237 {".py", "r", PY_SOURCE},
238 {".pyc", "rb", PY_COMPILED},
239 {0, 0}
240};
241
Guido van Rossum38234201996-07-31 17:55:19 +0000242#ifdef NO_DYNAMIC_LINK
243#undef DYNAMIC_LINK
244#endif
245
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246PyObject *
247_PyImport_LoadDynamicModule(name, pathname, fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000248 char *name;
249 char *pathname;
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000250 FILE *fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000251{
252#ifndef DYNAMIC_LINK
Guido van Rossum79f25d91997-04-29 20:08:16 +0000253 PyErr_SetString(PyExc_ImportError,
254 "dynamically linked modules not supported");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000255 return NULL;
256#else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000257 PyObject *m, *d, *s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000258 char funcname[258];
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000259 char *lastdot, *shortname, *packagecontext;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000260 dl_funcptr p = NULL;
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000261#ifdef USE_SHLIB
262 static struct {
263 dev_t dev;
264 ino_t ino;
265 void *handle;
266 } handles[128];
267 static int nhandles = 0;
Guido van Rossum0bbf2531996-08-09 20:55:05 +0000268 char pathbuf[260];
269 if (strchr(pathname, '/') == NULL) {
270 /* Prefix bare filename with "./" */
271 sprintf(pathbuf, "./%-.255s", pathname);
272 pathname = pathbuf;
273 }
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000274#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000275 if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
276 Py_INCREF(m);
277 return m;
278 }
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000279 lastdot = strrchr(name, '.');
280 if (lastdot == NULL) {
281 packagecontext = NULL;
282 shortname = name;
283 }
284 else {
285 packagecontext = name;
286 shortname = lastdot+1;
287 }
288 sprintf(funcname, FUNCNAME_PATTERN, shortname);
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000289#ifdef USE_SHLIB
290 if (fp != NULL) {
291 int i;
292 struct stat statb;
293 fstat(fileno(fp), &statb);
294 for (i = 0; i < nhandles; i++) {
295 if (statb.st_dev == handles[i].dev &&
296 statb.st_ino == handles[i].ino) {
297 p = (dl_funcptr) dlsym(handles[i].handle,
298 funcname);
299 goto got_it;
300 }
301 }
302 if (nhandles < 128) {
303 handles[nhandles].dev = statb.st_dev;
304 handles[nhandles].ino = statb.st_ino;
305 }
306 }
307#endif /* USE_SHLIB */
Jack Jansen5d9acb61995-06-14 14:54:25 +0000308#ifdef USE_MAC_DYNAMIC_LOADING
309 /*
Guido van Rossum79f25d91997-04-29 20:08:16 +0000310 ** Dynamic loading of CFM shared libraries on the Mac. The
311 ** code has become more convoluted than it was, because we
312 ** want to be able to put multiple modules in a single
313 ** file. For this reason, we have to determine the fragment
314 ** name, and we cannot use the library entry point but we have
315 ** to locate the correct init routine "by hand".
Jack Jansen5d9acb61995-06-14 14:54:25 +0000316 */
Jack Jansen4e043731995-02-13 22:42:34 +0000317 {
318 FSSpec libspec;
319 CFragConnectionID connID;
Guido van Rossum6a75d261995-02-18 14:51:15 +0000320 Ptr mainAddr;
321 Str255 errMessage;
322 OSErr err;
Jack Jansen5d9acb61995-06-14 14:54:25 +0000323 Boolean isfolder, didsomething;
324 char buf[512];
325 Str63 fragname;
326 Ptr symAddr;
327 CFragSymbolClass class;
Jack Jansen4e043731995-02-13 22:42:34 +0000328
Jack Jansen5d9acb61995-06-14 14:54:25 +0000329 /* First resolve any aliases to find the real file */
Jack Jansen4e043731995-02-13 22:42:34 +0000330 (void)FSMakeFSSpec(0, 0, Pstring(pathname), &libspec);
Jack Jansen5d9acb61995-06-14 14:54:25 +0000331 err = ResolveAliasFile(&libspec, 1, &isfolder, &didsomething);
332 if ( err ) {
Guido van Rossumbc2472d1997-04-30 19:07:54 +0000333 sprintf(buf, "%.255s: %.200s",
334 pathname, PyMac_StrError(err));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335 PyErr_SetString(PyExc_ImportError, buf);
Jack Jansen5d9acb61995-06-14 14:54:25 +0000336 return NULL;
337 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338 /* Next, determine the fragment name,
339 by stripping '.slb' and 'module' */
Jack Jansen5d9acb61995-06-14 14:54:25 +0000340 memcpy(fragname+1, libspec.name+1, libspec.name[0]);
341 fragname[0] = libspec.name[0];
Guido van Rossum79f25d91997-04-29 20:08:16 +0000342 if( strncmp((char *)(fragname+1+fragname[0]-4),
343 ".slb", 4) == 0 )
Jack Jansen5d9acb61995-06-14 14:54:25 +0000344 fragname[0] -= 4;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000345 if ( strncmp((char *)(fragname+1+fragname[0]-6),
346 "module", 6) == 0 )
Jack Jansen5d9acb61995-06-14 14:54:25 +0000347 fragname[0] -= 6;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000348 /* Load the fragment
349 (or return the connID if it is already loaded */
Jack Jansen5d9acb61995-06-14 14:54:25 +0000350 err = GetDiskFragment(&libspec, 0, 0, fragname,
Guido van Rossum6a75d261995-02-18 14:51:15 +0000351 kLoadCFrag, &connID, &mainAddr,
352 errMessage);
Jack Jansen4e043731995-02-13 22:42:34 +0000353 if ( err ) {
Guido van Rossumbc2472d1997-04-30 19:07:54 +0000354 sprintf(buf, "%.*s: %.200s",
355 errMessage[0], errMessage+1,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000356 PyMac_StrError(err));
357 PyErr_SetString(PyExc_ImportError, buf);
Jack Jansen4e043731995-02-13 22:42:34 +0000358 return NULL;
359 }
Jack Jansen5d9acb61995-06-14 14:54:25 +0000360 /* Locate the address of the correct init function */
361 err = FindSymbol(connID, Pstring(funcname), &symAddr, &class);
362 if ( err ) {
Guido van Rossumbc2472d1997-04-30 19:07:54 +0000363 sprintf(buf, "%s: %.200s",
364 funcname, PyMac_StrError(err));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000365 PyErr_SetString(PyExc_ImportError, buf);
Jack Jansen5d9acb61995-06-14 14:54:25 +0000366 return NULL;
367 }
368 p = (dl_funcptr)symAddr;
Jack Jansen4e043731995-02-13 22:42:34 +0000369 }
Jack Jansen5d9acb61995-06-14 14:54:25 +0000370#endif /* USE_MAC_DYNAMIC_LOADING */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000371#ifdef USE_SHLIB
372 {
373#ifdef RTLD_NOW
374 /* RTLD_NOW: resolve externals now
375 (i.e. core dump now if some are missing) */
Guido van Rossum6b077871998-05-18 13:42:45 +0000376 void *handle = dlopen(pathname, RTLD_NOW);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000377#else
378 void *handle;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000379 if (Py_VerboseFlag)
Guido van Rossum08052c71997-12-02 20:43:18 +0000380 printf("dlopen(\"%s\", %d);\n", pathname,
Guido van Rossum6b077871998-05-18 13:42:45 +0000381 RTLD_LAZY);
382 handle = dlopen(pathname, RTLD_LAZY);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000383#endif /* RTLD_NOW */
384 if (handle == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000385 PyErr_SetString(PyExc_ImportError, dlerror());
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000386 return NULL;
387 }
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000388 if (fp != NULL && nhandles < 128)
389 handles[nhandles++].handle = handle;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000390 p = (dl_funcptr) dlsym(handle, funcname);
391 }
392#endif /* USE_SHLIB */
393#ifdef _AIX
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000394 /*
395 -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
396 -- of the shared module unresolved. Thus we have to resolve them
397 -- explicitely with loadbind. The new module is loaded, then we
398 -- resolve its symbols using the list of already loaded modules
399 -- (only those that belong to the python executable). Get these
400 -- with loadquery(L_GETINFO).
401 */
402 {
403 static void *staticmodlistptr = NULL;
404
405 if (!staticmodlistptr)
406 if (aix_getoldmodules(&staticmodlistptr) == -1)
407 return NULL;
408 p = (dl_funcptr) load(pathname, L_NOAUTODEFER, 0);
409 if (p == NULL) {
410 aix_loaderror(pathname);
411 return NULL;
412 }
413 if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) {
414 aix_loaderror(pathname);
415 return NULL;
416 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000417 }
418#endif /* _AIX */
Guido van Rossum9b38a141996-09-11 23:12:24 +0000419#ifdef MS_WIN32
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000420 {
421 HINSTANCE hDLL;
422 hDLL = LoadLibrary(pathname);
423 if (hDLL==NULL){
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000424 char errBuf[256];
425 unsigned int errorCode;
426
427 /* Get an error string from Win32 error code */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000428 char theInfo[256]; /* Pointer to error text
429 from system */
430 int theLength; /* Length of error text */
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000431
432 errorCode = GetLastError();
433
Guido van Rossum79f25d91997-04-29 20:08:16 +0000434 theLength = FormatMessage(
435 FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
436 NULL, /* message source */
437 errorCode, /* the message (error) ID */
438 0, /* default language environment */
439 (LPTSTR) theInfo, /* the buffer */
440 sizeof(theInfo), /* the buffer size */
441 NULL); /* no additional format args. */
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000442
Guido van Rossum79f25d91997-04-29 20:08:16 +0000443 /* Problem: could not get the error message.
444 This should not happen if called correctly. */
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000445 if (theLength == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000446 sprintf(errBuf,
447 "DLL load failed with error code %d",
448 errorCode);
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000449 } else {
450 int len;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000451 /* For some reason a \r\n
452 is appended to the text */
453 if (theLength >= 2 &&
454 theInfo[theLength-2] == '\r' &&
455 theInfo[theLength-1] == '\n') {
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000456 theLength -= 2;
457 theInfo[theLength] = '\0';
458 }
459 strcpy(errBuf, "DLL load failed: ");
460 len = strlen(errBuf);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000461 strncpy(errBuf+len, theInfo,
462 sizeof(errBuf)-len);
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000463 errBuf[sizeof(errBuf)-1] = '\0';
464 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000465 PyErr_SetString(PyExc_ImportError, errBuf);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000466 return NULL;
467 }
468 p = GetProcAddress(hDLL, funcname);
469 }
Guido van Rossum9b38a141996-09-11 23:12:24 +0000470#endif /* MS_WIN32 */
471#ifdef MS_WIN16
Guido van Rossumdadc8241996-05-23 22:51:40 +0000472 {
473 HINSTANCE hDLL;
474 hDLL = LoadLibrary(pathname);
475 if (hDLL < HINSTANCE_ERROR){
476 char errBuf[256];
Guido van Rossum79f25d91997-04-29 20:08:16 +0000477 sprintf(errBuf,
478 "DLL load failed with error code %d", hDLL);
479 PyErr_SetString(PyExc_ImportError, errBuf);
Guido van Rossumdadc8241996-05-23 22:51:40 +0000480 return NULL;
481 }
482 p = GetProcAddress(hDLL, funcname);
483 }
Guido van Rossum9b38a141996-09-11 23:12:24 +0000484#endif /* MS_WIN16 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000485
486#if defined(PYOS_OS2)
487 {
488 APIRET rc;
489 HMODULE hDLL;
490 char failreason[256];
491
492 rc = DosLoadModule(failreason,
493 sizeof(failreason),
494 pathname,
495 &hDLL);
496
497 if (rc != NO_ERROR) {
498 char errBuf[256];
499 sprintf(errBuf,
500 "DLL load failed, rc = %d, problem '%s': %s", rc, failreason);
501 PyErr_SetString(PyExc_ImportError, errBuf);
502 return NULL;
503 }
504
505 rc = DosQueryProcAddr(hDLL, 0L, funcname, &p);
506 if (rc != NO_ERROR)
507 p = NULL; /* Signify Failure to Acquire Entrypoint */
508 }
509#endif /* PYOS_OS2 */
510
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000511#ifdef USE_DL
Guido van Rossum79f25d91997-04-29 20:08:16 +0000512 p = dl_loadmod(Py_GetProgramName(), pathname, funcname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000513#endif /* USE_DL */
514#ifdef USE_RLD
515 {
516 NXStream *errorStream;
517 struct mach_header *new_header;
518 const char *filenames[2];
519 long ret;
520 unsigned long ptr;
521
522 errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
523 filenames[0] = pathname;
524 filenames[1] = NULL;
525 ret = rld_load(errorStream, &new_header,
526 filenames, NULL);
527
528 /* extract the error messages for the exception */
529 if(!ret) {
530 char *streamBuf;
531 int len, maxLen;
532
533 NXPutc(errorStream, (char)0);
534
535 NXGetMemoryBuffer(errorStream,
536 &streamBuf, &len, &maxLen);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000537 PyErr_SetString(PyExc_ImportError, streamBuf);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000538 }
539
540 if(ret && rld_lookup(errorStream, funcname, &ptr))
541 p = (dl_funcptr) ptr;
542
543 NXCloseMemory(errorStream, NX_FREEBUFFER);
544
545 if(!ret)
546 return NULL;
547 }
548#endif /* USE_RLD */
549#ifdef hpux
550 {
551 shl_t lib;
552 int flags;
553
Guido van Rossum3afb5951996-12-05 23:15:35 +0000554 flags = BIND_FIRST | BIND_DEFERRED;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557 flags = DYNAMIC_PATH | BIND_FIRST | BIND_IMMEDIATE |
558 BIND_NONFATAL | BIND_VERBOSE;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559 printf("shl_load %s\n",pathname);
560 }
561 lib = shl_load(pathname, flags, 0);
Guido van Rossum6b077871998-05-18 13:42:45 +0000562 /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000563 if (lib == NULL)
564 {
565 char buf[256];
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000567 perror(pathname);
568 sprintf(buf, "Failed to load %.200s", pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000569 PyErr_SetString(PyExc_ImportError, buf);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000570 return NULL;
571 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000572 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000573 printf("shl_findsym %s\n", funcname);
574 shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575 if (p == NULL && Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000576 perror(funcname);
577 }
578#endif /* hpux */
Guido van Rossum644a12b1997-04-09 19:24:53 +0000579#ifdef USE_SHLIB
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000580 got_it:
Guido van Rossum644a12b1997-04-09 19:24:53 +0000581#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582 if (p == NULL) {
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000583 PyErr_Format(PyExc_ImportError,
Guido van Rossum42e8e5d1998-01-19 22:23:08 +0000584 "dynamic module does not define init function (%.200s)",
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000585 funcname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586 return NULL;
587 }
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000588 _Py_PackageContext = packagecontext;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589 (*p)();
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000590 _Py_PackageContext = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591 if (PyErr_Occurred())
592 return NULL;
593 if (_PyImport_FixupExtension(name, pathname) == NULL)
594 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000595
Guido van Rossumef3d02e1997-07-21 14:54:36 +0000596 m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000597 if (m == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000598 PyErr_SetString(PyExc_SystemError,
599 "dynamic module not initialized properly");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600 return NULL;
601 }
Guido van Rossum1e612491996-08-19 22:12:10 +0000602 /* Remember the filename as the __file__ attribute */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603 d = PyModule_GetDict(m);
604 s = PyString_FromString(pathname);
605 if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0)
606 PyErr_Clear(); /* Not important enough to report */
607 Py_XDECREF(s);
608 if (Py_VerboseFlag)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000609 fprintf(stderr,
610 "import %s # dynamically loaded from %s\n",
611 name, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000613 return m;
614#endif /* DYNAMIC_LINK */
615}
616
617
618#ifdef _AIX
619
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000620#include <ctype.h> /* for isdigit() */
621#include <errno.h> /* for global errno */
622#include <string.h> /* for strerror() */
623#include <stdlib.h> /* for malloc(), free() */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000624
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000625typedef struct Module {
626 struct Module *next;
627 void *entry;
628} Module, *ModulePtr;
629
630static int
631aix_getoldmodules(modlistptr)
632 void **modlistptr;
633{
634 register ModulePtr modptr, prevmodptr;
635 register struct ld_info *ldiptr;
636 register char *ldibuf;
637 register int errflag, bufsize = 1024;
638 register unsigned int offset;
639
640 /*
641 -- Get the list of loaded modules into ld_info structures.
642 */
643 if ((ldibuf = malloc(bufsize)) == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644 PyErr_SetString(PyExc_ImportError, strerror(errno));
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000645 return -1;
646 }
647 while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1
648 && errno == ENOMEM) {
649 free(ldibuf);
650 bufsize += 1024;
651 if ((ldibuf = malloc(bufsize)) == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 PyErr_SetString(PyExc_ImportError, strerror(errno));
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000653 return -1;
654 }
655 }
656 if (errflag == -1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 PyErr_SetString(PyExc_ImportError, strerror(errno));
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000658 return -1;
659 }
660 /*
661 -- Make the modules list from the ld_info structures.
662 */
663 ldiptr = (struct ld_info *)ldibuf;
664 prevmodptr = NULL;
665 do {
Guido van Rossum3b31cd21997-10-10 17:40:00 +0000666 if (strstr(ldiptr->ldinfo_filename, "python") == NULL) {
667 /*
668 -- Extract only the modules containing "python" as a
669 -- substring, like the "python[version]" executable or
670 -- "libpython[version].a" in case python is embedded.
671 */
672 offset = (unsigned int)ldiptr->ldinfo_next;
673 ldiptr = (struct ld_info *)((unsigned int)
674 ldiptr + offset);
675 continue;
676 }
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000677 if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000678 PyErr_SetString(PyExc_ImportError, strerror(errno));
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000679 while (*modlistptr) {
680 modptr = (ModulePtr)*modlistptr;
681 *modlistptr = (void *)modptr->next;
682 free(modptr);
683 }
684 return -1;
685 }
686 modptr->entry = ldiptr->ldinfo_dataorg;
687 modptr->next = NULL;
688 if (prevmodptr == NULL)
689 *modlistptr = (void *)modptr;
690 else
691 prevmodptr->next = modptr;
692 prevmodptr = modptr;
693 offset = (unsigned int)ldiptr->ldinfo_next;
694 ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset);
695 } while (offset);
696 free(ldibuf);
697 return 0;
698}
699
700static int
701aix_bindnewmodule(newmoduleptr, modlistptr)
702 void *newmoduleptr;
703 void *modlistptr;
704{
705 register ModulePtr modptr;
706
707 /*
708 -- Bind the new module with the list of loaded modules.
709 */
710 for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next)
711 if (loadbind(0, modptr->entry, newmoduleptr) != 0)
712 return -1;
713 return 0;
714}
715
716static void
717aix_loaderror(pathname)
718 char *pathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719{
720
Guido van Rossum236f62d1996-06-26 21:07:08 +0000721 char *message[1024], errbuf[1024];
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000722 register int i,j;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000723
724 struct errtab {
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000725 int errNo;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726 char *errstr;
727 } load_errtab[] = {
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000728 {L_ERROR_TOOMANY, "too many errors, rest skipped."},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000729 {L_ERROR_NOLIB, "can't load library:"},
730 {L_ERROR_UNDEF, "can't find symbol in library:"},
731 {L_ERROR_RLDBAD,
732 "RLD index out of range or bad relocation type:"},
733 {L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
734 {L_ERROR_MEMBER,
735 "file not an archive or does not contain requested member:"},
736 {L_ERROR_TYPE, "symbol table mismatch:"},
Guido van Rossum236f62d1996-06-26 21:07:08 +0000737 {L_ERROR_ALIGN, "text alignment in file is wrong."},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738 {L_ERROR_SYSTEM, "System error:"},
739 {L_ERROR_ERRNO, NULL}
740 };
741
742#define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0]))
743#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
744
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000745 sprintf(errbuf, "from module %.200s ", pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000747 if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000748 ERRBUF_APPEND(strerror(errno));
Guido van Rossum236f62d1996-06-26 21:07:08 +0000749 ERRBUF_APPEND("\n");
750 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000751 for(i = 0; message[i] && *message[i]; i++) {
752 int nerr = atoi(message[i]);
753 for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000754 if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
Guido van Rossum236f62d1996-06-26 21:07:08 +0000755 ERRBUF_APPEND(load_errtab[j].errstr);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000756 }
757 while (isdigit(*message[i])) message[i]++ ;
758 ERRBUF_APPEND(message[i]);
759 ERRBUF_APPEND("\n");
760 }
761 errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 PyErr_SetString(PyExc_ImportError, errbuf);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000763 return;
764}
765
766#endif /* _AIX */