blob: 2c1fad0a2f74eb96b5ed1ba806d154851fe476f3 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Support for dynamic loading of extension modules */
26/* If no dynamic linking is supported, this file still generates some code! */
27
28#include "allobjects.h"
29#include "osdefs.h"
30#include "importdl.h"
31
Guido van Rossum1ae940a1995-01-02 19:04:15 +000032/* Explanation of some of the the various #defines used by dynamic linking...
33
34 symbol -- defined for:
35
36 DYNAMIC_LINK -- any kind of dynamic linking
Guido van Rossum75f288d1995-06-14 22:07:26 +000037 USE_RLD -- NeXT dynamic linking
Guido van Rossum1ae940a1995-01-02 19:04:15 +000038 USE_DL -- Jack's dl for IRIX 4 or GNU dld with emulation for Jack's dl
39 USE_SHLIB -- SunOS or IRIX 5 (SVR4?) shared libraries
40 _AIX -- AIX style dynamic linking
41 NT -- NT style dynamic linking (using DLLs)
Guido van Rossumdadc8241996-05-23 22:51:40 +000042 WIN16_DL -- Windows 16-bit dynamic linking (using DLLs)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000043 _DL_FUNCPTR_DEFINED -- if the typedef dl_funcptr has been defined
Jack Jansen5d9acb61995-06-14 14:54:25 +000044 USE_MAC_DYNAMIC_LOADING -- Mac CFM shared libraries
Guido van Rossum1ae940a1995-01-02 19:04:15 +000045 SHORT_EXT -- short extension for dynamic module, e.g. ".so"
46 LONG_EXT -- long extension, e.g. "module.so"
47 hpux -- HP-UX Dynamic Linking - defined by the compiler
Guido van Rossum46c76a61995-01-20 16:53:54 +000048 __NetBSD__ -- NetBSD shared libraries (not quite SVR4 compatible)
Guido van Rossum25e85291996-02-25 05:02:29 +000049 __FreeBSD__ -- FreeBSD shared libraries
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050
51 (The other WITH_* symbols are used only once, to set the
52 appropriate symbols.)
53*/
54
55/* Configure dynamic linking */
56
Guido van Rossumff4af061996-01-12 01:17:50 +000057#ifdef __hpux
58#define hpux
59#endif
60
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061#ifdef hpux
62#define DYNAMIC_LINK
63#include <errno.h>
64typedef void (*dl_funcptr)();
65#define _DL_FUNCPTR_DEFINED 1
66#define SHORT_EXT ".sl"
67#define LONG_EXT "module.sl"
68#endif
69
Guido van Rossum25e85291996-02-25 05:02:29 +000070#if defined(__NetBSD__) || defined(__FreeBSD__)
Guido van Rossum46c76a61995-01-20 16:53:54 +000071#define DYNAMIC_LINK
72#define USE_SHLIB
73
74#define dlerror() "error in dynamic linking"
75#endif
76
Guido van Rossum0fbec641995-02-27 10:15:36 +000077#ifdef __WIN32__
78#define NT
79#endif
80
Guido van Rossumdadc8241996-05-23 22:51:40 +000081#ifdef MS_WIN16
82#define WIN16_DL
83#endif
84
85#if defined(NT) || defined(WIN16_DL)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000086#define DYNAMIC_LINK
87#include <windows.h>
88typedef FARPROC dl_funcptr;
89#define _DL_FUNCPTR_DEFINED
Guido van Rossum5fb1da71995-01-07 12:36:02 +000090#define SHORT_EXT ".pyd"
Guido van Rossume71a9471996-04-09 02:39:15 +000091#define LONG_EXT ".dll"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000092#endif
93
Guido van Rossum75f288d1995-06-14 22:07:26 +000094#ifdef NeXT
Guido van Rossum1ae940a1995-01-02 19:04:15 +000095#define DYNAMIC_LINK
96#define USE_RLD
97#endif
98
99#ifdef WITH_SGI_DL
100#define DYNAMIC_LINK
101#define USE_DL
102#endif
103
104#ifdef WITH_DL_DLD
105#define DYNAMIC_LINK
106#define USE_DL
107#endif
108
Guido van Rossum6a75d261995-02-18 14:51:15 +0000109#ifdef __CFM68K__
Jack Jansen5d9acb61995-06-14 14:54:25 +0000110#define USE_MAC_DYNAMIC_LOADING
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000111#endif
112
Jack Jansen5d9acb61995-06-14 14:54:25 +0000113#ifdef USE_MAC_DYNAMIC_LOADING
Jack Jansen4e043731995-02-13 22:42:34 +0000114#define DYNAMIC_LINK
Guido van Rossum6a75d261995-02-18 14:51:15 +0000115#define SHORT_EXT ".slb"
116#define LONG_EXT "module.slb"
Jack Jansen4e043731995-02-13 22:42:34 +0000117#ifndef _DL_FUNCPTR_DEFINED
118typedef void (*dl_funcptr)();
119#endif
120#endif
121
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000122#if !defined(DYNAMIC_LINK) && defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
123#define DYNAMIC_LINK
124#define USE_SHLIB
125#endif
126
127#ifdef _AIX
128#define DYNAMIC_LINK
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000129#define SHORT_EXT ".so"
130#define LONG_EXT "module.so"
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000131#include <sys/ldr.h>
132typedef void (*dl_funcptr)();
133#define _DL_FUNCPTR_DEFINED
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000134static int aix_getoldmodules(void **);
135static int aix_bindnewmodule(void *, void *);
136static void aix_loaderror(char *);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000137#endif
138
139#ifdef DYNAMIC_LINK
140
141#ifdef USE_SHLIB
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000142#include <sys/types.h>
143#include <sys/stat.h>
Guido van Rossum25e85291996-02-25 05:02:29 +0000144#if defined(__NetBSD__) || defined(__FreeBSD__)
Guido van Rossum46c76a61995-01-20 16:53:54 +0000145#include <nlist.h>
146#include <link.h>
147#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000148#include <dlfcn.h>
Guido van Rossum46c76a61995-01-20 16:53:54 +0000149#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000150#ifndef _DL_FUNCPTR_DEFINED
151typedef void (*dl_funcptr)();
152#endif
153#ifndef RTLD_LAZY
154#define RTLD_LAZY 1
155#endif
156#define SHORT_EXT ".so"
157#define LONG_EXT "module.so"
158#endif /* USE_SHLIB */
159
160#if defined(USE_DL) || defined(hpux)
161#include "dl.h"
162#endif
163
Jack Jansen5d9acb61995-06-14 14:54:25 +0000164#ifdef USE_MAC_DYNAMIC_LOADING
Jack Jansen0a72e8d1995-10-23 13:54:01 +0000165#include <Aliases.h>
Jack Jansen4e043731995-02-13 22:42:34 +0000166#include <CodeFragments.h>
Jack Janseneceb3e31995-06-27 13:15:15 +0000167#ifdef SYMANTEC__CFM68K__ /* Really just an older version of Universal Headers */
Guido van Rossum6a75d261995-02-18 14:51:15 +0000168#define CFragConnectionID ConnectionID
169#define kLoadCFrag 0x01
170#endif
Jack Jansen4e043731995-02-13 22:42:34 +0000171#include <Files.h>
172#include "macdefs.h"
173#include "macglue.h"
174#endif
175
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000176#ifdef USE_RLD
177#include <mach-o/rld.h>
178#define FUNCNAME_PATTERN "_init%.200s"
179#ifndef _DL_FUNCPTR_DEFINED
180typedef void (*dl_funcptr)();
181#endif
182#endif /* USE_RLD */
183
184extern char *getprogramname();
185
186#ifndef FUNCNAME_PATTERN
Guido van Rossum25e85291996-02-25 05:02:29 +0000187#if defined(__hp9000s300) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__BORLANDC__)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000188#define FUNCNAME_PATTERN "_init%.200s"
189#else
190#define FUNCNAME_PATTERN "init%.200s"
191#endif
192#endif
193
194#if !defined(SHORT_EXT) && !defined(LONG_EXT)
195#define SHORT_EXT ".o"
196#define LONG_EXT "module.o"
197#endif /* !SHORT_EXT && !LONG_EXT */
198
199#endif /* DYNAMIC_LINK */
200
Guido van Rossum6a75d261995-02-18 14:51:15 +0000201/* Max length of module suffix searched for -- accommodates "module.slb" */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000202#ifndef MAXSUFFIXSIZE
Jack Jansen4e043731995-02-13 22:42:34 +0000203#define MAXSUFFIXSIZE 12
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000204#endif
205
206/* Pass it on to import.c */
207int import_maxsuffixsize = MAXSUFFIXSIZE;
208
209struct filedescr import_filetab[] = {
210#ifdef SHORT_EXT
211 {SHORT_EXT, "rb", C_EXTENSION},
212#endif /* !SHORT_EXT */
213#ifdef LONG_EXT
214 {LONG_EXT, "rb", C_EXTENSION},
215#endif /* !LONG_EXT */
216 {".py", "r", PY_SOURCE},
217 {".pyc", "rb", PY_COMPILED},
218 {0, 0}
219};
220
Guido van Rossum38234201996-07-31 17:55:19 +0000221#ifdef NO_DYNAMIC_LINK
222#undef DYNAMIC_LINK
223#endif
224
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000225object *
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000226load_dynamic_module(name, pathname, fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000227 char *name;
228 char *pathname;
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000229 FILE *fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000230{
231#ifndef DYNAMIC_LINK
232 err_setstr(ImportError, "dynamically linked modules not supported");
233 return NULL;
234#else
235 object *m;
236 char funcname[258];
237 dl_funcptr p = NULL;
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000238#ifdef USE_SHLIB
239 static struct {
240 dev_t dev;
241 ino_t ino;
242 void *handle;
243 } handles[128];
244 static int nhandles = 0;
245#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000246 sprintf(funcname, FUNCNAME_PATTERN, name);
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000247#ifdef USE_SHLIB
248 if (fp != NULL) {
249 int i;
250 struct stat statb;
251 fstat(fileno(fp), &statb);
252 for (i = 0; i < nhandles; i++) {
253 if (statb.st_dev == handles[i].dev &&
254 statb.st_ino == handles[i].ino) {
255 p = (dl_funcptr) dlsym(handles[i].handle,
256 funcname);
257 goto got_it;
258 }
259 }
260 if (nhandles < 128) {
261 handles[nhandles].dev = statb.st_dev;
262 handles[nhandles].ino = statb.st_ino;
263 }
264 }
265#endif /* USE_SHLIB */
Jack Jansen5d9acb61995-06-14 14:54:25 +0000266#ifdef USE_MAC_DYNAMIC_LOADING
267 /*
268 ** Dynamic loading of CFM shared libraries on the Mac.
269 ** The code has become more convoluted than it was, because we want to be able
270 ** to put multiple modules in a single file. For this reason, we have to determine
271 ** the fragment name, and we cannot use the library entry point but we have to locate
272 ** the correct init routine "by hand".
273 */
Jack Jansen4e043731995-02-13 22:42:34 +0000274 {
275 FSSpec libspec;
276 CFragConnectionID connID;
Guido van Rossum6a75d261995-02-18 14:51:15 +0000277 Ptr mainAddr;
278 Str255 errMessage;
279 OSErr err;
Jack Jansen5d9acb61995-06-14 14:54:25 +0000280 Boolean isfolder, didsomething;
281 char buf[512];
282 Str63 fragname;
283 Ptr symAddr;
284 CFragSymbolClass class;
Jack Jansen4e043731995-02-13 22:42:34 +0000285
Jack Jansen5d9acb61995-06-14 14:54:25 +0000286 /* First resolve any aliases to find the real file */
Jack Jansen4e043731995-02-13 22:42:34 +0000287 (void)FSMakeFSSpec(0, 0, Pstring(pathname), &libspec);
Jack Jansen5d9acb61995-06-14 14:54:25 +0000288 err = ResolveAliasFile(&libspec, 1, &isfolder, &didsomething);
289 if ( err ) {
290 sprintf(buf, "%s: %s", pathname, PyMac_StrError(err));
291 err_setstr(ImportError, buf);
292 return NULL;
293 }
294 /* Next, determine the fragment name, by stripping '.slb' and 'module' */
295 memcpy(fragname+1, libspec.name+1, libspec.name[0]);
296 fragname[0] = libspec.name[0];
297 if( strncmp((char *)(fragname+1+fragname[0]-4), ".slb", 4) == 0 )
298 fragname[0] -= 4;
299 if ( strncmp((char *)(fragname+1+fragname[0]-6), "module", 6) == 0 )
300 fragname[0] -= 6;
301 /* Load the fragment (or return the connID if it is already loaded */
302 err = GetDiskFragment(&libspec, 0, 0, fragname,
Guido van Rossum6a75d261995-02-18 14:51:15 +0000303 kLoadCFrag, &connID, &mainAddr,
304 errMessage);
Jack Jansen4e043731995-02-13 22:42:34 +0000305 if ( err ) {
Guido van Rossum3097c3a1995-02-21 21:02:46 +0000306 sprintf(buf, "%.*s: %s", errMessage[0], errMessage+1, PyMac_StrError(err));
Jack Jansen4e043731995-02-13 22:42:34 +0000307 err_setstr(ImportError, buf);
308 return NULL;
309 }
Jack Jansen5d9acb61995-06-14 14:54:25 +0000310 /* Locate the address of the correct init function */
311 err = FindSymbol(connID, Pstring(funcname), &symAddr, &class);
312 if ( err ) {
313 sprintf(buf, "%s: %s", funcname, PyMac_StrError(err));
314 err_setstr(ImportError, buf);
315 return NULL;
316 }
317 p = (dl_funcptr)symAddr;
Jack Jansen4e043731995-02-13 22:42:34 +0000318 }
Jack Jansen5d9acb61995-06-14 14:54:25 +0000319#endif /* USE_MAC_DYNAMIC_LOADING */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000320#ifdef USE_SHLIB
321 {
322#ifdef RTLD_NOW
323 /* RTLD_NOW: resolve externals now
324 (i.e. core dump now if some are missing) */
325 void *handle = dlopen(pathname, RTLD_NOW);
326#else
327 void *handle;
328 if (verbose)
329 printf("dlopen(\"%s\", %d);\n", pathname, RTLD_LAZY);
330 handle = dlopen(pathname, RTLD_LAZY);
331#endif /* RTLD_NOW */
332 if (handle == NULL) {
333 err_setstr(ImportError, dlerror());
334 return NULL;
335 }
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000336 if (fp != NULL && nhandles < 128)
337 handles[nhandles++].handle = handle;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000338 p = (dl_funcptr) dlsym(handle, funcname);
339 }
340#endif /* USE_SHLIB */
341#ifdef _AIX
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000342 /*
343 -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
344 -- of the shared module unresolved. Thus we have to resolve them
345 -- explicitely with loadbind. The new module is loaded, then we
346 -- resolve its symbols using the list of already loaded modules
347 -- (only those that belong to the python executable). Get these
348 -- with loadquery(L_GETINFO).
349 */
350 {
351 static void *staticmodlistptr = NULL;
352
353 if (!staticmodlistptr)
354 if (aix_getoldmodules(&staticmodlistptr) == -1)
355 return NULL;
356 p = (dl_funcptr) load(pathname, L_NOAUTODEFER, 0);
357 if (p == NULL) {
358 aix_loaderror(pathname);
359 return NULL;
360 }
361 if (aix_bindnewmodule((void *)p, staticmodlistptr) == -1) {
362 aix_loaderror(pathname);
363 return NULL;
364 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000365 }
366#endif /* _AIX */
367#ifdef NT
368 {
369 HINSTANCE hDLL;
370 hDLL = LoadLibrary(pathname);
371 if (hDLL==NULL){
Guido van Rossum11a3f0c21995-07-18 14:40:09 +0000372 char errBuf[256];
373 unsigned int errorCode;
374
375 /* Get an error string from Win32 error code */
376 char theInfo[256]; /* Pointer to error text from system */
377 int theLength; /* Length of error text */
378
379 errorCode = GetLastError();
380
381 theLength = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, /* flags */
382 NULL, /* message source */
383 errorCode, /* the message (error) ID */
384 0, /* default language environment */
385 (LPTSTR) theInfo, /* the buffer */
386 sizeof(theInfo), /* the buffer size */
387 NULL); /* no additional format args. */
388
389 /* Problem: could not get the error message. This should not happen if called correctly. */
390 if (theLength == 0) {
391 sprintf(errBuf, "DLL load failed with error code %d", errorCode);
392 } else {
393 int len;
394 /* For some reason a \r\n is appended to the text */
395 if (theLength >= 2 && theInfo[theLength-2] == '\r' && theInfo[theLength-1] == '\n') {
396 theLength -= 2;
397 theInfo[theLength] = '\0';
398 }
399 strcpy(errBuf, "DLL load failed: ");
400 len = strlen(errBuf);
401 strncpy(errBuf+len, theInfo, sizeof(errBuf)-len);
402 errBuf[sizeof(errBuf)-1] = '\0';
403 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000404 err_setstr(ImportError, errBuf);
405 return NULL;
406 }
407 p = GetProcAddress(hDLL, funcname);
408 }
409#endif /* NT */
Guido van Rossumdadc8241996-05-23 22:51:40 +0000410#ifdef WIN16_DL
411 {
412 HINSTANCE hDLL;
413 hDLL = LoadLibrary(pathname);
414 if (hDLL < HINSTANCE_ERROR){
415 char errBuf[256];
416 sprintf(errBuf, "DLL load failed with error code %d", hDLL);
417 err_setstr(ImportError, errBuf);
418 return NULL;
419 }
420 p = GetProcAddress(hDLL, funcname);
421 }
422#endif /* WIN16_DL */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000423#ifdef USE_DL
424 p = dl_loadmod(getprogramname(), pathname, funcname);
425#endif /* USE_DL */
426#ifdef USE_RLD
427 {
428 NXStream *errorStream;
429 struct mach_header *new_header;
430 const char *filenames[2];
431 long ret;
432 unsigned long ptr;
433
434 errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
435 filenames[0] = pathname;
436 filenames[1] = NULL;
437 ret = rld_load(errorStream, &new_header,
438 filenames, NULL);
439
440 /* extract the error messages for the exception */
441 if(!ret) {
442 char *streamBuf;
443 int len, maxLen;
444
445 NXPutc(errorStream, (char)0);
446
447 NXGetMemoryBuffer(errorStream,
448 &streamBuf, &len, &maxLen);
449 err_setstr(ImportError, streamBuf);
450 }
451
452 if(ret && rld_lookup(errorStream, funcname, &ptr))
453 p = (dl_funcptr) ptr;
454
455 NXCloseMemory(errorStream, NX_FREEBUFFER);
456
457 if(!ret)
458 return NULL;
459 }
460#endif /* USE_RLD */
461#ifdef hpux
462 {
463 shl_t lib;
464 int flags;
465
466 flags = BIND_DEFERRED;
467 if (verbose)
468 {
469 flags = BIND_IMMEDIATE | BIND_NONFATAL | BIND_VERBOSE;
470 printf("shl_load %s\n",pathname);
471 }
472 lib = shl_load(pathname, flags, 0);
473 if (lib == NULL)
474 {
475 char buf[256];
476 if (verbose)
477 perror(pathname);
478 sprintf(buf, "Failed to load %.200s", pathname);
479 err_setstr(ImportError, buf);
480 return NULL;
481 }
482 if (verbose)
483 printf("shl_findsym %s\n", funcname);
484 shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p);
485 if (p == NULL && verbose)
486 perror(funcname);
487 }
488#endif /* hpux */
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000489 got_it:
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000490 if (p == NULL) {
491 err_setstr(ImportError,
492 "dynamic module does not define init function");
493 return NULL;
494 }
495 (*p)();
496
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000497 m = dictlookup(import_modules, name);
498 if (m == NULL) {
499 if (err_occurred() == NULL)
500 err_setstr(SystemError,
501 "dynamic module not initialized properly");
502 return NULL;
503 }
504 if (verbose)
505 fprintf(stderr,
506 "import %s # dynamically loaded from %s\n",
507 name, pathname);
508 INCREF(m);
509 return m;
510#endif /* DYNAMIC_LINK */
511}
512
513
514#ifdef _AIX
515
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000516#include <ctype.h> /* for isdigit() */
517#include <errno.h> /* for global errno */
518#include <string.h> /* for strerror() */
519#include <stdlib.h> /* for malloc(), free() */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000520
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000521typedef struct Module {
522 struct Module *next;
523 void *entry;
524} Module, *ModulePtr;
525
526static int
527aix_getoldmodules(modlistptr)
528 void **modlistptr;
529{
530 register ModulePtr modptr, prevmodptr;
531 register struct ld_info *ldiptr;
532 register char *ldibuf;
533 register int errflag, bufsize = 1024;
534 register unsigned int offset;
535
536 /*
537 -- Get the list of loaded modules into ld_info structures.
538 */
539 if ((ldibuf = malloc(bufsize)) == NULL) {
540 err_setstr(ImportError, strerror(errno));
541 return -1;
542 }
543 while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1
544 && errno == ENOMEM) {
545 free(ldibuf);
546 bufsize += 1024;
547 if ((ldibuf = malloc(bufsize)) == NULL) {
548 err_setstr(ImportError, strerror(errno));
549 return -1;
550 }
551 }
552 if (errflag == -1) {
553 err_setstr(ImportError, strerror(errno));
554 return -1;
555 }
556 /*
557 -- Make the modules list from the ld_info structures.
558 */
559 ldiptr = (struct ld_info *)ldibuf;
560 prevmodptr = NULL;
561 do {
562 if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) {
563 err_setstr(ImportError, strerror(errno));
564 while (*modlistptr) {
565 modptr = (ModulePtr)*modlistptr;
566 *modlistptr = (void *)modptr->next;
567 free(modptr);
568 }
569 return -1;
570 }
571 modptr->entry = ldiptr->ldinfo_dataorg;
572 modptr->next = NULL;
573 if (prevmodptr == NULL)
574 *modlistptr = (void *)modptr;
575 else
576 prevmodptr->next = modptr;
577 prevmodptr = modptr;
578 offset = (unsigned int)ldiptr->ldinfo_next;
579 ldiptr = (struct ld_info *)((unsigned int)ldiptr + offset);
580 } while (offset);
581 free(ldibuf);
582 return 0;
583}
584
585static int
586aix_bindnewmodule(newmoduleptr, modlistptr)
587 void *newmoduleptr;
588 void *modlistptr;
589{
590 register ModulePtr modptr;
591
592 /*
593 -- Bind the new module with the list of loaded modules.
594 */
595 for (modptr = (ModulePtr)modlistptr; modptr; modptr = modptr->next)
596 if (loadbind(0, modptr->entry, newmoduleptr) != 0)
597 return -1;
598 return 0;
599}
600
601static void
602aix_loaderror(pathname)
603 char *pathname;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604{
605
Guido van Rossum236f62d1996-06-26 21:07:08 +0000606 char *message[1024], errbuf[1024];
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000607 register int i,j;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000608
609 struct errtab {
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000610 int errNo;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611 char *errstr;
612 } load_errtab[] = {
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000613 {L_ERROR_TOOMANY, "too many errors, rest skipped."},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614 {L_ERROR_NOLIB, "can't load library:"},
615 {L_ERROR_UNDEF, "can't find symbol in library:"},
616 {L_ERROR_RLDBAD,
617 "RLD index out of range or bad relocation type:"},
618 {L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
619 {L_ERROR_MEMBER,
620 "file not an archive or does not contain requested member:"},
621 {L_ERROR_TYPE, "symbol table mismatch:"},
Guido van Rossum236f62d1996-06-26 21:07:08 +0000622 {L_ERROR_ALIGN, "text alignment in file is wrong."},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000623 {L_ERROR_SYSTEM, "System error:"},
624 {L_ERROR_ERRNO, NULL}
625 };
626
627#define LOAD_ERRTAB_LEN (sizeof(load_errtab)/sizeof(load_errtab[0]))
628#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
629
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000630 sprintf(errbuf, "from module %.200s ", pathname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000631
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000632 if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000633 ERRBUF_APPEND(strerror(errno));
Guido van Rossum236f62d1996-06-26 21:07:08 +0000634 ERRBUF_APPEND("\n");
635 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636 for(i = 0; message[i] && *message[i]; i++) {
637 int nerr = atoi(message[i]);
638 for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
Guido van Rossumd5962ad1996-07-31 22:44:53 +0000639 if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
Guido van Rossum236f62d1996-06-26 21:07:08 +0000640 ERRBUF_APPEND(load_errtab[j].errstr);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641 }
642 while (isdigit(*message[i])) message[i]++ ;
643 ERRBUF_APPEND(message[i]);
644 ERRBUF_APPEND("\n");
645 }
646 errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
647 err_setstr(ImportError, errbuf);
648 return;
649}
650
651#endif /* _AIX */