blob: 3b43bbc0a213f6805052697de5ce55364bc91f7b [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 Rossum54ecc3d1999-01-27 17:53:11 +000049 USE_RLD -- NeXT dynamic linking with FVM shlibs
50 USE_DYLD -- NeXT dynamic linking with frameworks
Guido van Rossum1ae940a1995-01-02 19:04:15 +000051 USE_DL -- Jack's dl for IRIX 4 or GNU dld with emulation for Jack's dl
52 USE_SHLIB -- SunOS or IRIX 5 (SVR4?) shared libraries
53 _AIX -- AIX style dynamic linking
Guido van Rossum9b38a141996-09-11 23:12:24 +000054 MS_WIN32 -- Windows NT style dynamic linking (using DLLs)
55 MS_WIN16 -- Windows 16-bit dynamic linking (using DLLs)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000056 PYOS_OS2 -- IBM OS/2 dynamic linking (using DLLs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057 _DL_FUNCPTR_DEFINED -- if the typedef dl_funcptr has been defined
Jack Jansen5d9acb61995-06-14 14:54:25 +000058 USE_MAC_DYNAMIC_LOADING -- Mac CFM shared libraries
Guido van Rossum1ae940a1995-01-02 19:04:15 +000059 SHORT_EXT -- short extension for dynamic module, e.g. ".so"
60 LONG_EXT -- long extension, e.g. "module.so"
61 hpux -- HP-UX Dynamic Linking - defined by the compiler
Guido van Rossum15af20a1998-01-19 22:03:52 +000062 __NetBSD__ -- NetBSD shared libraries
63 (assuming dlerror() was introduced between 1.2 and 1.3)
Guido van Rossum25e85291996-02-25 05:02:29 +000064 __FreeBSD__ -- FreeBSD shared libraries
Guido van Rossum1a8791e1998-08-04 22:46:29 +000065 __BEOS__ -- BeOS shared libraries - defined by the compiler
Guido van Rossum1ae940a1995-01-02 19:04:15 +000066
67 (The other WITH_* symbols are used only once, to set the
68 appropriate symbols.)
69*/
70
71/* Configure dynamic linking */
72
Guido van Rossumff4af061996-01-12 01:17:50 +000073#ifdef __hpux
Guido van Rossum1e612491996-08-19 22:12:10 +000074#ifndef hpux
Guido van Rossumff4af061996-01-12 01:17:50 +000075#define hpux
76#endif
Guido van Rossum1e612491996-08-19 22:12:10 +000077#endif
Guido van Rossumff4af061996-01-12 01:17:50 +000078
Guido van Rossum1ae940a1995-01-02 19:04:15 +000079#ifdef hpux
80#define DYNAMIC_LINK
81#include <errno.h>
82typedef void (*dl_funcptr)();
83#define _DL_FUNCPTR_DEFINED 1
84#define SHORT_EXT ".sl"
85#define LONG_EXT "module.sl"
86#endif
87
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000088#if defined(PYOS_OS2)
89#define DYNAMIC_LINK
90#define INCL_DOSERRORS
91#define INCL_DOSMODULEMGR
92#include <os2.h>
93typedef int (* APIENTRY dl_funcptr)();
94#define _DL_FUNCPTR_DEFINED 1
95#define SHORT_EXT ".pyd"
96#define LONG_EXT ".dll"
97#endif
98
Guido van Rossum15af20a1998-01-19 22:03:52 +000099#if defined(__NetBSD__) && (NetBSD < 199712)
Guido van Rossum46c76a61995-01-20 16:53:54 +0000100#define DYNAMIC_LINK
101#define USE_SHLIB
102
103#define dlerror() "error in dynamic linking"
104#endif
105
Guido van Rossum9b38a141996-09-11 23:12:24 +0000106#ifdef MS_WINDOWS /* i.e. MS_WIN32 or MS_WIN16 */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000107#define DYNAMIC_LINK
108#include <windows.h>
Guido van Rossuma5e1b001998-06-27 21:53:17 +0000109#include <direct.h>
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000110typedef FARPROC dl_funcptr;
111#define _DL_FUNCPTR_DEFINED
Guido van Rossum859b16c1998-05-15 20:22:08 +0000112#ifdef _DEBUG
113#define SHORT_EXT "_d.pyd"
114#define LONG_EXT "_d.dll"
115#else
Guido van Rossum5fb1da71995-01-07 12:36:02 +0000116#define SHORT_EXT ".pyd"
Guido van Rossume71a9471996-04-09 02:39:15 +0000117#define LONG_EXT ".dll"
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000118#endif
Guido van Rossum859b16c1998-05-15 20:22:08 +0000119#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000120
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000121#ifdef WITH_DYLD
122#define DYNAMIC_LINK
123#define USE_DYLD
124#define SHORT_EXT ".so"
125#define LONG_EXT "module.so"
126#define FUNCNAME_PATTERN "_init%.200s"
127#endif
128
129#if defined(NeXT) && !defined(DYNAMIC_LINK)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000130#define DYNAMIC_LINK
131#define USE_RLD
Guido van Rossumab076fd1998-08-24 14:15:44 +0000132/* Define this to 1 if you want be able to load ObjC modules as well:
133 it switches between two different way of loading modules on the NeXT,
134 one that directly interfaces with the dynamic loader (rld_load(), which
135 does not correctly load ObjC object files), and another that uses the
136 ObjC runtime (objc_loadModules()) to do the job.
137 You'll have to add ``-ObjC'' to the compiler flags if you set this to 1.
138*/
139#define HANDLE_OBJC_MODULES 1
140#if HANDLE_OBJC_MODULES
141#include <objc/Object.h>
142#include <objc/objc-load.h>
143#endif
Guido van Rossum54dec591997-08-16 14:38:09 +0000144#define SHORT_EXT ".so"
145#define LONG_EXT "module.so"
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000146#endif
147
148#ifdef WITH_SGI_DL
149#define DYNAMIC_LINK
150#define USE_DL
151#endif
152
153#ifdef WITH_DL_DLD
154#define DYNAMIC_LINK
155#define USE_DL
156#endif
157
Jack Jansen5d9acb61995-06-14 14:54:25 +0000158#ifdef USE_MAC_DYNAMIC_LOADING
Jack Jansen4e043731995-02-13 22:42:34 +0000159#define DYNAMIC_LINK
Guido van Rossum6a75d261995-02-18 14:51:15 +0000160#define SHORT_EXT ".slb"
Guido van Rossum1e612491996-08-19 22:12:10 +0000161#ifdef __CFM68K__
162#define LONG_EXT ".CFM68K.slb"
163#else
164#define LONG_EXT ".ppc.slb"
165#endif
Jack Jansen4e043731995-02-13 22:42:34 +0000166#ifndef _DL_FUNCPTR_DEFINED
167typedef void (*dl_funcptr)();
168#endif
169#endif
170
Guido van Rossum504f4a91996-08-20 19:59:07 +0000171#if !defined(DYNAMIC_LINK) && (defined(HAVE_DLOPEN) || defined(M_UNIX))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000172#define DYNAMIC_LINK
173#define USE_SHLIB
174#endif
175
176#ifdef _AIX
Guido van Rossumef3d02e1997-07-21 14:54:36 +0000177#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 +0000178#define DYNAMIC_LINK
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000179#define SHORT_EXT ".so"
180#define LONG_EXT "module.so"
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000181#include <sys/ldr.h>
182typedef void (*dl_funcptr)();
183#define _DL_FUNCPTR_DEFINED
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000184static int aix_getoldmodules(void **);
185static int aix_bindnewmodule(void *, void *);
186static void aix_loaderror(char *);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000187#endif
188
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000189#ifdef __BEOS__
Guido van Rossume364b7d1998-10-01 20:43:23 +0000190#undef USE_SHLIB /* probably not defined anyway */
191#define DYNAMIC_LINK
192#define SHORT_EXT ".so"
193#define LONG_EXT "module.so"
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000194typedef void (*dl_funcptr)(void);
Guido van Rossume364b7d1998-10-01 20:43:23 +0000195#define _DL_FUNCPTR_DEFINED
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000196
Guido van Rossume364b7d1998-10-01 20:43:23 +0000197#if defined(MAXPATHLEN) && !defined(_SYS_PARAM_H)
198#undef MAXPATHLEN
199#endif
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000200
Guido van Rossume364b7d1998-10-01 20:43:23 +0000201#include <kernel/image.h>
202#include <kernel/OS.h>
203#include <stdlib.h>
204#include <unistd.h>
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000205
Guido van Rossume364b7d1998-10-01 20:43:23 +0000206#ifdef WITH_THREAD
207#include "pythread.h"
Guido van Rossum65d5b571998-12-21 19:32:43 +0000208static PyThread_type_lock beos_dyn_lock;
Guido van Rossume364b7d1998-10-01 20:43:23 +0000209#endif
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000210
211static PyObject *beos_dyn_images = NULL;
212
213static void beos_init_dyn( void );
214static void beos_cleanup_dyn( void );
215static void beos_nuke_dyn( PyObject *item );
216static void beos_add_dyn( char *pathname, image_id id );
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000217#endif
218
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000219#ifdef DYNAMIC_LINK
220
221#ifdef USE_SHLIB
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000222#include <sys/types.h>
223#include <sys/stat.h>
Guido van Rossum15af20a1998-01-19 22:03:52 +0000224#if defined(__NetBSD__) && (NetBSD < 199712)
Guido van Rossum46c76a61995-01-20 16:53:54 +0000225#include <nlist.h>
226#include <link.h>
227#else
Guido van Rossum504f4a91996-08-20 19:59:07 +0000228#ifdef HAVE_DLFCN_H
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000229#include <dlfcn.h>
Guido van Rossum46c76a61995-01-20 16:53:54 +0000230#endif
Guido van Rossum504f4a91996-08-20 19:59:07 +0000231#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000232#ifndef _DL_FUNCPTR_DEFINED
233typedef void (*dl_funcptr)();
234#endif
235#ifndef RTLD_LAZY
236#define RTLD_LAZY 1
237#endif
238#define SHORT_EXT ".so"
239#define LONG_EXT "module.so"
240#endif /* USE_SHLIB */
241
242#if defined(USE_DL) || defined(hpux)
243#include "dl.h"
244#endif
245
Jack Jansen5d9acb61995-06-14 14:54:25 +0000246#ifdef USE_MAC_DYNAMIC_LOADING
Jack Jansen0a72e8d1995-10-23 13:54:01 +0000247#include <Aliases.h>
Jack Jansen4e043731995-02-13 22:42:34 +0000248#include <CodeFragments.h>
Guido van Rossum79f25d91997-04-29 20:08:16 +0000249#ifdef SYMANTEC__CFM68K__ /* Really an older version of Universal Headers */
Guido van Rossum6a75d261995-02-18 14:51:15 +0000250#define CFragConnectionID ConnectionID
251#define kLoadCFrag 0x01
252#endif
Guido van Rossuma0f0a331998-09-14 13:40:53 +0000253#ifdef USE_GUSI
254#include "TFileSpec.h" /* for Path2FSSpec() */
255#endif
Jack Jansen4e043731995-02-13 22:42:34 +0000256#include <Files.h>
257#include "macdefs.h"
258#include "macglue.h"
259#endif
260
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000261#ifdef USE_RLD
262#include <mach-o/rld.h>
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000263#ifndef _DL_FUNCPTR_DEFINED
264typedef void (*dl_funcptr)();
265#endif
266#endif /* USE_RLD */
267
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000268#ifdef USE_DYLD
269#include <mach-o/dyld.h>
270#ifndef _DL_FUNCPTR_DEFINED
271typedef void (*dl_funcptr)();
272#endif
273#endif /* USE_DYLD */
274
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275extern char *Py_GetProgramName();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000276
277#ifndef FUNCNAME_PATTERN
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000278#if defined(__hp9000s300) || (defined(__NetBSD__) || defined(__FreeBSD__)) && !defined(__ELF__) || defined(__OpenBSD__) || defined(__BORLANDC__) || defined(NeXT)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000279#define FUNCNAME_PATTERN "_init%.200s"
280#else
281#define FUNCNAME_PATTERN "init%.200s"
282#endif
283#endif
284
285#if !defined(SHORT_EXT) && !defined(LONG_EXT)
286#define SHORT_EXT ".o"
287#define LONG_EXT "module.o"
288#endif /* !SHORT_EXT && !LONG_EXT */
289
290#endif /* DYNAMIC_LINK */
291
Guido van Rossum79f25d91997-04-29 20:08:16 +0000292struct filedescr _PyImport_Filetab[] = {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000293#ifdef SHORT_EXT
294 {SHORT_EXT, "rb", C_EXTENSION},
295#endif /* !SHORT_EXT */
296#ifdef LONG_EXT
297 {LONG_EXT, "rb", C_EXTENSION},
298#endif /* !LONG_EXT */
299 {".py", "r", PY_SOURCE},
300 {".pyc", "rb", PY_COMPILED},
301 {0, 0}
302};
303
Guido van Rossum38234201996-07-31 17:55:19 +0000304#ifdef NO_DYNAMIC_LINK
305#undef DYNAMIC_LINK
306#endif
307
Guido van Rossum79f25d91997-04-29 20:08:16 +0000308PyObject *
309_PyImport_LoadDynamicModule(name, pathname, fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000310 char *name;
311 char *pathname;
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000312 FILE *fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000313{
314#ifndef DYNAMIC_LINK
Guido van Rossum79f25d91997-04-29 20:08:16 +0000315 PyErr_SetString(PyExc_ImportError,
316 "dynamically linked modules not supported");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000317 return NULL;
318#else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000319 PyObject *m, *d, *s;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000320 char funcname[258];
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000321 char *lastdot, *shortname, *packagecontext;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000322 dl_funcptr p = NULL;
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000323#ifdef USE_SHLIB
324 static struct {
325 dev_t dev;
326 ino_t ino;
327 void *handle;
328 } handles[128];
329 static int nhandles = 0;
Guido van Rossum0bbf2531996-08-09 20:55:05 +0000330 char pathbuf[260];
331 if (strchr(pathname, '/') == NULL) {
332 /* Prefix bare filename with "./" */
333 sprintf(pathbuf, "./%-.255s", pathname);
334 pathname = pathbuf;
335 }
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000336#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000337 if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
338 Py_INCREF(m);
339 return m;
340 }
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000341 lastdot = strrchr(name, '.');
342 if (lastdot == NULL) {
343 packagecontext = NULL;
344 shortname = name;
345 }
346 else {
347 packagecontext = name;
348 shortname = lastdot+1;
349 }
350 sprintf(funcname, FUNCNAME_PATTERN, shortname);
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000351#ifdef USE_SHLIB
352 if (fp != NULL) {
353 int i;
354 struct stat statb;
355 fstat(fileno(fp), &statb);
356 for (i = 0; i < nhandles; i++) {
357 if (statb.st_dev == handles[i].dev &&
358 statb.st_ino == handles[i].ino) {
359 p = (dl_funcptr) dlsym(handles[i].handle,
360 funcname);
361 goto got_it;
362 }
363 }
364 if (nhandles < 128) {
365 handles[nhandles].dev = statb.st_dev;
366 handles[nhandles].ino = statb.st_ino;
367 }
368 }
369#endif /* USE_SHLIB */
Jack Jansen5d9acb61995-06-14 14:54:25 +0000370#ifdef USE_MAC_DYNAMIC_LOADING
371 /*
Guido van Rossum79f25d91997-04-29 20:08:16 +0000372 ** Dynamic loading of CFM shared libraries on the Mac. The
373 ** code has become more convoluted than it was, because we
374 ** want to be able to put multiple modules in a single
375 ** file. For this reason, we have to determine the fragment
376 ** name, and we cannot use the library entry point but we have
377 ** to locate the correct init routine "by hand".
Jack Jansen5d9acb61995-06-14 14:54:25 +0000378 */
Jack Jansen4e043731995-02-13 22:42:34 +0000379 {
380 FSSpec libspec;
381 CFragConnectionID connID;
Guido van Rossum6a75d261995-02-18 14:51:15 +0000382 Ptr mainAddr;
383 Str255 errMessage;
384 OSErr err;
Guido van Rossuma0f0a331998-09-14 13:40:53 +0000385#ifndef USE_GUSI
Jack Jansen5d9acb61995-06-14 14:54:25 +0000386 Boolean isfolder, didsomething;
Guido van Rossuma0f0a331998-09-14 13:40:53 +0000387#endif
Jack Jansen5d9acb61995-06-14 14:54:25 +0000388 char buf[512];
389 Str63 fragname;
390 Ptr symAddr;
391 CFragSymbolClass class;
Jack Jansen4e043731995-02-13 22:42:34 +0000392
Jack Jansen5d9acb61995-06-14 14:54:25 +0000393 /* First resolve any aliases to find the real file */
Guido van Rossuma0f0a331998-09-14 13:40:53 +0000394#ifdef USE_GUSI
395 err = Path2FSSpec(pathname, &libspec);
396#else
Jack Jansen4e043731995-02-13 22:42:34 +0000397 (void)FSMakeFSSpec(0, 0, Pstring(pathname), &libspec);
Jack Jansen5d9acb61995-06-14 14:54:25 +0000398 err = ResolveAliasFile(&libspec, 1, &isfolder, &didsomething);
Guido van Rossuma0f0a331998-09-14 13:40:53 +0000399#endif
Jack Jansen5d9acb61995-06-14 14:54:25 +0000400 if ( err ) {
Guido van Rossumbc2472d1997-04-30 19:07:54 +0000401 sprintf(buf, "%.255s: %.200s",
402 pathname, PyMac_StrError(err));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000403 PyErr_SetString(PyExc_ImportError, buf);
Jack Jansen5d9acb61995-06-14 14:54:25 +0000404 return NULL;
405 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000406 /* Next, determine the fragment name,
407 by stripping '.slb' and 'module' */
Jack Jansen5d9acb61995-06-14 14:54:25 +0000408 memcpy(fragname+1, libspec.name+1, libspec.name[0]);
409 fragname[0] = libspec.name[0];
Guido van Rossum79f25d91997-04-29 20:08:16 +0000410 if( strncmp((char *)(fragname+1+fragname[0]-4),
411 ".slb", 4) == 0 )
Jack Jansen5d9acb61995-06-14 14:54:25 +0000412 fragname[0] -= 4;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000413 if ( strncmp((char *)(fragname+1+fragname[0]-6),
414 "module", 6) == 0 )
Jack Jansen5d9acb61995-06-14 14:54:25 +0000415 fragname[0] -= 6;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000416 /* Load the fragment
417 (or return the connID if it is already loaded */
Jack Jansen5d9acb61995-06-14 14:54:25 +0000418 err = GetDiskFragment(&libspec, 0, 0, fragname,
Guido van Rossum6a75d261995-02-18 14:51:15 +0000419 kLoadCFrag, &connID, &mainAddr,
420 errMessage);
Jack Jansen4e043731995-02-13 22:42:34 +0000421 if ( err ) {
Guido van Rossumbc2472d1997-04-30 19:07:54 +0000422 sprintf(buf, "%.*s: %.200s",
423 errMessage[0], errMessage+1,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000424 PyMac_StrError(err));
425 PyErr_SetString(PyExc_ImportError, buf);
Jack Jansen4e043731995-02-13 22:42:34 +0000426 return NULL;
427 }
Jack Jansen5d9acb61995-06-14 14:54:25 +0000428 /* Locate the address of the correct init function */
429 err = FindSymbol(connID, Pstring(funcname), &symAddr, &class);
430 if ( err ) {
Guido van Rossumbc2472d1997-04-30 19:07:54 +0000431 sprintf(buf, "%s: %.200s",
432 funcname, PyMac_StrError(err));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000433 PyErr_SetString(PyExc_ImportError, buf);
Jack Jansen5d9acb61995-06-14 14:54:25 +0000434 return NULL;
435 }
436 p = (dl_funcptr)symAddr;
Jack Jansen4e043731995-02-13 22:42:34 +0000437 }
Jack Jansen5d9acb61995-06-14 14:54:25 +0000438#endif /* USE_MAC_DYNAMIC_LOADING */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000439#ifdef USE_SHLIB
440 {
441#ifdef RTLD_NOW
442 /* RTLD_NOW: resolve externals now
443 (i.e. core dump now if some are missing) */
Guido van Rossum6b077871998-05-18 13:42:45 +0000444 void *handle = dlopen(pathname, RTLD_NOW);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000445#else
446 void *handle;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000447 if (Py_VerboseFlag)
Guido van Rossum08052c71997-12-02 20:43:18 +0000448 printf("dlopen(\"%s\", %d);\n", pathname,
Guido van Rossum6b077871998-05-18 13:42:45 +0000449 RTLD_LAZY);
450 handle = dlopen(pathname, RTLD_LAZY);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000451#endif /* RTLD_NOW */
452 if (handle == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000453 PyErr_SetString(PyExc_ImportError, dlerror());
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000454 return NULL;
455 }
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000456 if (fp != NULL && nhandles < 128)
457 handles[nhandles++].handle = handle;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000458 p = (dl_funcptr) dlsym(handle, funcname);
459 }
460#endif /* USE_SHLIB */
461#ifdef _AIX
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000462 /*
463 -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
464 -- of the shared module unresolved. Thus we have to resolve them
465 -- explicitely with loadbind. The new module is loaded, then we
466 -- resolve its symbols using the list of already loaded modules
467 -- (only those that belong to the python executable). Get these
468 -- with loadquery(L_GETINFO).
469 */
470 {
471 static void *staticmodlistptr = NULL;
472
473 if (!staticmodlistptr)
474 if (aix_getoldmodules(&staticmodlistptr) == -1)
475 return NULL;
476 p = (dl_funcptr) load(pathname, L_NOAUTODEFER, 0);
477 if (p == NULL) {
478 aix_loaderror(pathname);
479 return NULL;
480 }
481 if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) {
482 aix_loaderror(pathname);
483 return NULL;
484 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000485 }
486#endif /* _AIX */
Guido van Rossum9b38a141996-09-11 23:12:24 +0000487#ifdef MS_WIN32
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000488 {
489 HINSTANCE hDLL;
Guido van Rossuma5e1b001998-06-27 21:53:17 +0000490 char pathbuf[260];
491 if (strchr(pathname, SEP) == NULL &&
492 strchr(pathname, ALTSEP) == NULL)
493 {
494 /* Prefix bare filename with ".\" */
495 char *p = pathbuf;
496 *p = '\0';
497 _getcwd(pathbuf, sizeof pathbuf);
498 if (*p != '\0' && p[1] == ':')
499 p += 2;
500 sprintf(p, ".\\%-.255s", pathname);
501 pathname = pathbuf;
502 }
Guido van Rossum0f8b30f1998-10-08 01:44:41 +0000503 /* Look for dependent DLLs in directory of pathname first */
504 /* XXX This call doesn't exist in Windows CE */
505 hDLL = LoadLibraryEx(pathname, NULL,
506 LOAD_WITH_ALTERED_SEARCH_PATH);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000507 if (hDLL==NULL){
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000508 char errBuf[256];
509 unsigned int errorCode;
510
511 /* Get an error string from Win32 error code */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000512 char theInfo[256]; /* Pointer to error text
513 from system */
514 int theLength; /* Length of error text */
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000515
516 errorCode = GetLastError();
517
Guido van Rossum79f25d91997-04-29 20:08:16 +0000518 theLength = FormatMessage(
519 FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
520 NULL, /* message source */
521 errorCode, /* the message (error) ID */
522 0, /* default language environment */
523 (LPTSTR) theInfo, /* the buffer */
524 sizeof(theInfo), /* the buffer size */
525 NULL); /* no additional format args. */
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000526
Guido van Rossum79f25d91997-04-29 20:08:16 +0000527 /* Problem: could not get the error message.
528 This should not happen if called correctly. */
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000529 if (theLength == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530 sprintf(errBuf,
531 "DLL load failed with error code %d",
532 errorCode);
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000533 } else {
534 int len;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000535 /* For some reason a \r\n
536 is appended to the text */
537 if (theLength >= 2 &&
538 theInfo[theLength-2] == '\r' &&
539 theInfo[theLength-1] == '\n') {
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000540 theLength -= 2;
541 theInfo[theLength] = '\0';
542 }
543 strcpy(errBuf, "DLL load failed: ");
544 len = strlen(errBuf);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000545 strncpy(errBuf+len, theInfo,
546 sizeof(errBuf)-len);
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000547 errBuf[sizeof(errBuf)-1] = '\0';
548 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000549 PyErr_SetString(PyExc_ImportError, errBuf);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550 return NULL;
551 }
552 p = GetProcAddress(hDLL, funcname);
553 }
Guido van Rossum9b38a141996-09-11 23:12:24 +0000554#endif /* MS_WIN32 */
555#ifdef MS_WIN16
Guido van Rossumdadc8241996-05-23 22:51:40 +0000556 {
557 HINSTANCE hDLL;
Guido van Rossuma5e1b001998-06-27 21:53:17 +0000558 char pathbuf[16];
559 if (strchr(pathname, SEP) == NULL &&
560 strchr(pathname, ALTSEP) == NULL)
561 {
562 /* Prefix bare filename with ".\" */
563 sprintf(pathbuf, ".\\%-.13s", pathname);
564 pathname = pathbuf;
565 }
Guido van Rossumdadc8241996-05-23 22:51:40 +0000566 hDLL = LoadLibrary(pathname);
567 if (hDLL < HINSTANCE_ERROR){
568 char errBuf[256];
Guido van Rossum79f25d91997-04-29 20:08:16 +0000569 sprintf(errBuf,
570 "DLL load failed with error code %d", hDLL);
571 PyErr_SetString(PyExc_ImportError, errBuf);
Guido van Rossumdadc8241996-05-23 22:51:40 +0000572 return NULL;
573 }
574 p = GetProcAddress(hDLL, funcname);
575 }
Guido van Rossum9b38a141996-09-11 23:12:24 +0000576#endif /* MS_WIN16 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000577
578#if defined(PYOS_OS2)
579 {
580 APIRET rc;
581 HMODULE hDLL;
Guido van Rossumbb71ab61998-07-08 13:47:12 +0000582 char failreason[256];
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000583
584 rc = DosLoadModule(failreason,
Guido van Rossumbb71ab61998-07-08 13:47:12 +0000585 sizeof(failreason),
586 pathname,
587 &hDLL);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000588
589 if (rc != NO_ERROR) {
590 char errBuf[256];
591 sprintf(errBuf,
Guido van Rossumbb71ab61998-07-08 13:47:12 +0000592 "DLL load failed, rc = %d, problem '%s': %s",
593 rc, failreason);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000594 PyErr_SetString(PyExc_ImportError, errBuf);
595 return NULL;
596 }
597
Guido van Rossumbb71ab61998-07-08 13:47:12 +0000598 rc = DosQueryProcAddr(hDLL, 0L, funcname, &p);
599 if (rc != NO_ERROR)
600 p = NULL; /* Signify Failure to Acquire Entrypoint */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000601 }
602#endif /* PYOS_OS2 */
603
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604#ifdef USE_DL
Guido van Rossum79f25d91997-04-29 20:08:16 +0000605 p = dl_loadmod(Py_GetProgramName(), pathname, funcname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000606#endif /* USE_DL */
607#ifdef USE_RLD
608 {
609 NXStream *errorStream;
610 struct mach_header *new_header;
611 const char *filenames[2];
612 long ret;
613 unsigned long ptr;
614
615 errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
616 filenames[0] = pathname;
617 filenames[1] = NULL;
Guido van Rossumab076fd1998-08-24 14:15:44 +0000618
619#if HANDLE_OBJC_MODULES
620
621/* The following very bogus line of code ensures that
622 objc_msgSend, etc are linked into the binary. Without
623 it, dynamic loading of a module that includes objective-c
624 method calls will fail with "undefined symbol _objc_msgSend()".
625 This remains true even in the presence of the -ObjC flag
626 to the compiler
627*/
628
629 [Object name];
630
631/* objc_loadModules() dynamically loads the object files
632 indicated by the paths in filenames. If there are any
633 errors generated during loading -- typically due to the
634 inability to find particular symbols -- an error message
635 will be written to errorStream.
636 It returns 0 if the module is successfully loaded, 1
637 otherwise.
638*/
639
640 ret = !objc_loadModules(filenames, errorStream,
641 NULL, &new_header, NULL);
642
643#else /* !HANDLE_OBJC_MODULES */
644
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645 ret = rld_load(errorStream, &new_header,
646 filenames, NULL);
647
Guido van Rossumab076fd1998-08-24 14:15:44 +0000648#endif /* HANDLE_OBJC_MODULES */
649
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000650 /* extract the error messages for the exception */
651 if(!ret) {
652 char *streamBuf;
653 int len, maxLen;
654
655 NXPutc(errorStream, (char)0);
656
657 NXGetMemoryBuffer(errorStream,
658 &streamBuf, &len, &maxLen);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000659 PyErr_SetString(PyExc_ImportError, streamBuf);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660 }
661
662 if(ret && rld_lookup(errorStream, funcname, &ptr))
663 p = (dl_funcptr) ptr;
664
665 NXCloseMemory(errorStream, NX_FREEBUFFER);
666
667 if(!ret)
668 return NULL;
669 }
670#endif /* USE_RLD */
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000671#ifdef USE_DYLD
672 /* This is also NeXT-specific. However, frameworks (the new style
673 of shared library) and rld() can't be used in the same program;
674 instead, you have to use dyld, which is mostly unimplemented. */
675 {
676 NSObjectFileImageReturnCode rc;
677 NSObjectFileImage image;
678 NSModule newModule;
679 NSSymbol theSym;
680 void *symaddr;
681 const char *errString;
682
683 rc = NSCreateObjectFileImageFromFile(pathname, &image);
684 switch(rc) {
685 default:
686 case NSObjectFileImageFailure:
687 NSObjectFileImageFormat:
688 /* for these a message is printed on stderr by dyld */
689 errString = "Can't create object file image";
690 break;
691 case NSObjectFileImageSuccess:
692 errString = NULL;
693 break;
694 case NSObjectFileImageInappropriateFile:
695 errString = "Inappropriate file type for dynamic loading";
696 break;
697 case NSObjectFileImageArch:
698 errString = "Wrong CPU type in object file";
699 break;
700 NSObjectFileImageAccess:
701 errString = "Can't read object file (no access)";
702 break;
703 }
704 if (errString == NULL) {
705 newModule = NSLinkModule(image, pathname, TRUE);
706 if (!newModule)
707 errString = "Failure linking new module";
708 }
709 if (errString != NULL) {
710 PyErr_SetString(PyExc_ImportError, errString);
711 return NULL;
712 }
713 if (!NSIsSymbolNameDefined(funcname)) {
714 /* UnlinkModule() isn't implimented in current versions, but calling it does no harm */
715 NSUnLinkModule(newModule, FALSE);
716 PyErr_Format(PyExc_ImportError, "Loaded module does not contain symbol %s", funcname);
717 return NULL;
718 }
719 theSym = NSLookupAndBindSymbol(funcname);
720 p = (dl_funcptr)NSAddressOfSymbol(theSym);
721 }
722#endif /* USE_DYLD */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000723#ifdef hpux
724 {
725 shl_t lib;
726 int flags;
727
Guido van Rossum3afb5951996-12-05 23:15:35 +0000728 flags = BIND_FIRST | BIND_DEFERRED;
Guido van Rossumbb71ab61998-07-08 13:47:12 +0000729 if (Py_VerboseFlag) {
730 flags = DYNAMIC_PATH | BIND_FIRST | BIND_IMMEDIATE |
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731 BIND_NONFATAL | BIND_VERBOSE;
Guido van Rossumbb71ab61998-07-08 13:47:12 +0000732 printf("shl_load %s\n",pathname);
733 }
734 lib = shl_load(pathname, flags, 0);
735 /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */
736 if (lib == NULL) {
737 char buf[256];
738 if (Py_VerboseFlag)
739 perror(pathname);
740 sprintf(buf, "Failed to load %.200s", pathname);
741 PyErr_SetString(PyExc_ImportError, buf);
742 return NULL;
743 }
744 if (Py_VerboseFlag)
745 printf("shl_findsym %s\n", funcname);
746 shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p);
747 if (p == NULL && Py_VerboseFlag)
748 perror(funcname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000749 }
750#endif /* hpux */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000751#ifdef __BEOS__
752 {
753 image_id the_id;
754 status_t retval;
755 char fullpath[PATH_MAX];
756
757 if( Py_VerboseFlag ) {
758 printf( "load_add_on( %s )\n", pathname );
759 }
760
761 /* Hmm, this old bug appears to have regenerated itself; if the
762 * path isn't absolute, load_add_on() will fail. Reported to Be
763 * April 21, 1998.
764 */
765 if( pathname[0] != '/' ) {
766 (void)getcwd( fullpath, PATH_MAX );
767 (void)strncat( fullpath, "/", PATH_MAX );
768 (void)strncat( fullpath, pathname, PATH_MAX );
769
770 if( Py_VerboseFlag ) {
771 printf( "load_add_on( %s )\n", fullpath );
772 }
773 } else {
774 (void)strcpy( fullpath, pathname );
775 }
776
777 the_id = load_add_on( fullpath );
778 if( the_id < B_NO_ERROR ) {
779 /* It's too bad load_add_on() doesn't set errno or something...
780 */
781 char buff[256]; /* hate hard-coded string sizes... */
782
783 if( Py_VerboseFlag ) {
784 printf( "load_add_on( %s ) failed", fullpath );
785 }
786
787 switch( the_id ) {
788 case B_ERROR:
789 sprintf( buff, "BeOS: Failed to load %.200s", fullpath );
790 break;
791 default:
792 sprintf( buff, "Unknown error loading %.200s", fullpath );
793 break;
794 }
795
796 PyErr_SetString( PyExc_ImportError, buff );
797 return NULL;
798 }
799
800 if( Py_VerboseFlag ) {
801 printf( "get_image_symbol( %s )\n", funcname );
802 }
803
804 retval = get_image_symbol( the_id, funcname, B_SYMBOL_TYPE_TEXT, &p );
805 if( retval != B_NO_ERROR || p == NULL ) {
806 /* That's bad, we can't find that symbol in the module...
807 */
808 char buff[256]; /* hate hard-coded string sizes... */
809
810 if( Py_VerboseFlag ) {
811 printf( "get_image_symbol( %s ) failed", funcname );
812 }
813
814 switch( retval ) {
815 case B_BAD_IMAGE_ID:
816 sprintf( buff, "can't load init function for dynamic module: "
817 "Invalid image ID for %.180s", fullpath );
818 break;
819 case B_BAD_INDEX:
820 sprintf( buff, "can't load init function for dynamic module: "
821 "Bad index for %.180s", funcname );
822 break;
823 default:
824 sprintf( buff, "can't load init function for dynamic module: "
825 "Unknown error looking up %.180s", funcname );
826 break;
827 }
828
829 retval = unload_add_on( the_id );
830
831 PyErr_SetString( PyExc_ImportError, buff );
832 return NULL;
833 }
834
835 /* Save the module name and image ID for later so we can clean up
836 * gracefully.
837 */
838 beos_add_dyn( name, the_id );
839 }
840#endif /* __BEOS__ */
Guido van Rossum644a12b1997-04-09 19:24:53 +0000841#ifdef USE_SHLIB
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000842 got_it:
Guido van Rossum644a12b1997-04-09 19:24:53 +0000843#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000844 if (p == NULL) {
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000845 PyErr_Format(PyExc_ImportError,
Guido van Rossum42e8e5d1998-01-19 22:23:08 +0000846 "dynamic module does not define init function (%.200s)",
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000847 funcname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000848 return NULL;
849 }
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000850 _Py_PackageContext = packagecontext;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000851 (*p)();
Guido van Rossum2e58ff31997-11-19 18:53:33 +0000852 _Py_PackageContext = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000853 if (PyErr_Occurred())
854 return NULL;
855 if (_PyImport_FixupExtension(name, pathname) == NULL)
856 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000857
Guido van Rossumef3d02e1997-07-21 14:54:36 +0000858 m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000859 if (m == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000860 PyErr_SetString(PyExc_SystemError,
861 "dynamic module not initialized properly");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000862 return NULL;
863 }
Guido van Rossum1e612491996-08-19 22:12:10 +0000864 /* Remember the filename as the __file__ attribute */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000865 d = PyModule_GetDict(m);
866 s = PyString_FromString(pathname);
867 if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0)
868 PyErr_Clear(); /* Not important enough to report */
869 Py_XDECREF(s);
870 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000871 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000872 "import %s # dynamically loaded from %s\n",
873 name, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000875 return m;
876#endif /* DYNAMIC_LINK */
877}
878
879
880#ifdef _AIX
881
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000882#include <ctype.h> /* for isdigit() */
883#include <errno.h> /* for global errno */
884#include <string.h> /* for strerror() */
885#include <stdlib.h> /* for malloc(), free() */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000886
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000887typedef struct Module {
888 struct Module *next;
889 void *entry;
890} Module, *ModulePtr;
891
892static int
893aix_getoldmodules(modlistptr)
894 void **modlistptr;
895{
896 register ModulePtr modptr, prevmodptr;
897 register struct ld_info *ldiptr;
898 register char *ldibuf;
899 register int errflag, bufsize = 1024;
900 register unsigned int offset;
901
902 /*
903 -- Get the list of loaded modules into ld_info structures.
904 */
905 if ((ldibuf = malloc(bufsize)) == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000906 PyErr_SetString(PyExc_ImportError, strerror(errno));
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000907 return -1;
908 }
909 while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1
910 && errno == ENOMEM) {
911 free(ldibuf);
912 bufsize += 1024;
913 if ((ldibuf = malloc(bufsize)) == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000914 PyErr_SetString(PyExc_ImportError, strerror(errno));
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000915 return -1;
916 }
917 }
918 if (errflag == -1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000919 PyErr_SetString(PyExc_ImportError, strerror(errno));
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000920 return -1;
921 }
922 /*
923 -- Make the modules list from the ld_info structures.
924 */
925 ldiptr = (struct ld_info *)ldibuf;
926 prevmodptr = NULL;
927 do {
Guido van Rossum3b31cd21997-10-10 17:40:00 +0000928 if (strstr(ldiptr->ldinfo_filename, "python") == NULL) {
929 /*
930 -- Extract only the modules containing "python" as a
931 -- substring, like the "python[version]" executable or
932 -- "libpython[version].a" in case python is embedded.
933 */
934 offset = (unsigned int)ldiptr->ldinfo_next;
935 ldiptr = (struct ld_info *)((unsigned int)
936 ldiptr + offset);
937 continue;
938 }
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000939 if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000940 PyErr_SetString(PyExc_ImportError, strerror(errno));
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000941 while (*modlistptr) {
942 modptr = (ModulePtr)*modlistptr;
943 *modlistptr = (void *)modptr->next;
944 free(modptr);
945 }
946 return -1;
947 }
948 modptr->entry = ldiptr->ldinfo_dataorg;
949 modptr->next = NULL;
950 if (prevmodptr == NULL)
951 *modlistptr = (void *)modptr;
952 else
953 prevmodptr->next = modptr;
954 prevmodptr = modptr;
955 offset = (unsigned int)ldiptr->ldinfo_next;
956 ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset);
957 } while (offset);
958 free(ldibuf);
959 return 0;
960}
961
962static int
963aix_bindnewmodule(newmoduleptr, modlistptr)
964 void *newmoduleptr;
965 void *modlistptr;
966{
967 register ModulePtr modptr;
968
969 /*
970 -- Bind the new module with the list of loaded modules.
971 */
972 for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next)
973 if (loadbind(0, modptr->entry, newmoduleptr) != 0)
974 return -1;
975 return 0;
976}
977
978static void
979aix_loaderror(pathname)
980 char *pathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000981{
982
Guido van Rossum236f62d1996-06-26 21:07:08 +0000983 char *message[1024], errbuf[1024];
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000984 register int i,j;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000985
986 struct errtab {
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000987 int errNo;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000988 char *errstr;
989 } load_errtab[] = {
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000990 {L_ERROR_TOOMANY, "too many errors, rest skipped."},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000991 {L_ERROR_NOLIB, "can't load library:"},
992 {L_ERROR_UNDEF, "can't find symbol in library:"},
993 {L_ERROR_RLDBAD,
994 "RLD index out of range or bad relocation type:"},
995 {L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
996 {L_ERROR_MEMBER,
997 "file not an archive or does not contain requested member:"},
998 {L_ERROR_TYPE, "symbol table mismatch:"},
Guido van Rossum236f62d1996-06-26 21:07:08 +0000999 {L_ERROR_ALIGN, "text alignment in file is wrong."},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001000 {L_ERROR_SYSTEM, "System error:"},
1001 {L_ERROR_ERRNO, NULL}
1002 };
1003
1004#define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0]))
1005#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
1006
Guido van Rossumd5962ad1996-07-31 22:44:53 +00001007 sprintf(errbuf, "from module %.200s ", pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001008
Guido van Rossumd5962ad1996-07-31 22:44:53 +00001009 if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001010 ERRBUF_APPEND(strerror(errno));
Guido van Rossum236f62d1996-06-26 21:07:08 +00001011 ERRBUF_APPEND("\n");
1012 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001013 for(i = 0; message[i] && *message[i]; i++) {
1014 int nerr = atoi(message[i]);
1015 for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
Guido van Rossumd5962ad1996-07-31 22:44:53 +00001016 if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
Guido van Rossum236f62d1996-06-26 21:07:08 +00001017 ERRBUF_APPEND(load_errtab[j].errstr);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001018 }
1019 while (isdigit(*message[i])) message[i]++ ;
1020 ERRBUF_APPEND(message[i]);
1021 ERRBUF_APPEND("\n");
1022 }
1023 errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001024 PyErr_SetString(PyExc_ImportError, errbuf);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001025 return;
1026}
1027
1028#endif /* _AIX */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001029
1030#ifdef __BEOS__
1031/* ----------------------------------------------------------------------
1032 * BeOS dynamic loading support
1033 *
1034 * This uses shared libraries, but BeOS has its own way of doing things
1035 * (much easier than dlfnc.h, from the look of things). We'll use a
1036 * Python Dictionary object to store the images_ids so we can be very
1037 * nice and unload them when we exit.
1038 *
1039 * Note that this is thread-safe. Probably irrelevent, because of losing
1040 * systems... Python probably disables threads while loading modules.
1041 * Note the use of "probably"! Better to be safe than sorry. [chrish]
1042 *
1043 * As of 1.5.1 this should also work properly when you've configured
1044 * Python without thread support; the 1.5 version required it, which wasn't
1045 * very friendly. Note that I haven't tested it without threading... why
1046 * would you want to avoid threads on BeOS? [chrish]
Guido van Rossumcad3d471999-01-04 16:45:59 +00001047 *
1048 * As of 1.5.2, the PyImport_BeImageID() function has been removed; Donn
1049 * tells me it's not necessary anymore because of PyCObject_Import().
1050 * [chrish]
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001051 */
1052
1053/*
1054 * Initialize our dictionary, and the dictionary mutex.
1055 */
1056static void beos_init_dyn( void )
1057{
1058 /* We're protected from a race condition here by the atomic init_count
1059 * variable.
1060 */
1061 static int32 init_count = 0;
1062 int32 val;
1063
1064 val = atomic_add( &init_count, 1 );
1065 if( beos_dyn_images == NULL && val == 0 ) {
1066 beos_dyn_images = PyDict_New();
1067#ifdef WITH_THREAD
1068 beos_dyn_lock = PyThread_allocate_lock();
1069#endif
1070 atexit( beos_cleanup_dyn );
1071 }
1072}
1073
1074/* atexit() handler that'll call unload_add_on() for every item in the
1075 * dictionary.
1076 */
1077static void beos_cleanup_dyn( void )
1078{
1079 if( beos_dyn_images ) {
1080 int idx;
1081 int list_size;
1082 PyObject *id_list;
1083
1084#ifdef WITH_THREAD
1085 PyThread_acquire_lock( beos_dyn_lock, 1 );
1086#endif
1087
1088 id_list = PyDict_Values( beos_dyn_images );
1089
1090 list_size = PyList_Size( id_list );
1091 for( idx = 0; idx < list_size; idx++ ) {
1092 PyObject *the_item;
1093
1094 the_item = PyList_GetItem( id_list, idx );
1095 beos_nuke_dyn( the_item );
1096 }
1097
1098 PyDict_Clear( beos_dyn_images );
1099
1100#ifdef WITH_THREAD
1101 PyThread_free_lock( beos_dyn_lock );
1102#endif
1103 }
1104}
1105
1106/* Whack an item; the item is an image_id in disguise, so we'll call
1107 * unload_add_on() for it.
1108 */
1109static void beos_nuke_dyn( PyObject *item )
1110{
1111 status_t retval;
1112
1113 if( item ) {
1114 image_id id = (image_id)PyInt_AsLong( item );
1115
1116 retval = unload_add_on( id );
1117 }
1118}
1119
1120/*
1121 * Add an image_id to the dictionary; the module name of the loaded image
1122 * is the key. Note that if the key is already in the dict, we unload
1123 * that image; this should allow reload() to work on dynamically loaded
1124 * modules (super-keen!).
1125 */
1126static void beos_add_dyn( char *name, image_id id )
1127{
1128 int retval;
1129 PyObject *py_id;
1130
1131 if( beos_dyn_images == NULL ) {
1132 beos_init_dyn();
1133 }
1134
1135#ifdef WITH_THREAD
1136 retval = PyThread_acquire_lock( beos_dyn_lock, 1 );
1137#endif
1138
1139 /* If there's already an object with this key in the dictionary,
1140 * we're doing a reload(), so let's nuke it.
1141 */
1142 py_id = PyDict_GetItemString( beos_dyn_images, name );
1143 if( py_id ) {
1144 beos_nuke_dyn( py_id );
1145 retval = PyDict_DelItemString( beos_dyn_images, name );
1146 }
1147
1148 py_id = PyInt_FromLong( (long)id );
1149 if( py_id ) {
1150 retval = PyDict_SetItemString( beos_dyn_images, name, py_id );
1151 }
1152
1153#ifdef WITH_THREAD
1154 PyThread_release_lock( beos_dyn_lock );
1155#endif
1156}
1157
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001158#endif /* __BEOS__ */