[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / interpreter / dynamic.c
1 /* -*- mode: hugs-c; -*- */
2 /* --------------------------------------------------------------------------
3  * Dynamic loading (of .dll or .so files) for Hugs
4  *
5  * Copyright (c) The University of Nottingham and Yale University, 1994-1997.
6  * All rights reserved. See NOTICE for details and conditions of use etc...
7  * Hugs version 1.4, December 1997
8  *
9  * $RCSfile: dynamic.c,v $
10  * $Revision: 1.2 $
11  * $Date: 1998/12/02 13:22:06 $
12  * ------------------------------------------------------------------------*/
13
14 #include "prelude.h"
15 #include "storage.h"
16 #include "errors.h"
17 #include "dynamic.h"
18
19 #if HAVE_DLFCN_H /* eg LINUX, SOLARIS, ULTRIX */
20
21 #include <stdio.h>
22 #include <dlfcn.h>
23
24 ObjectFile loadLibrary(fn)
25 String fn; {
26     return dlopen(fn,RTLD_NOW | RTLD_GLOBAL);
27 }
28
29 void* lookupSymbol(file,symbol)
30 ObjectFile file;
31 String symbol; {
32     return dlsym(file,symbol)
33 }
34
35 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
36 String dll;
37 String symbol; {
38 #ifdef RTLD_NOW
39     ObjectFile instance = dlopen(dll,RTLD_NOW);
40 #elif defined RTLD_LAZY /* eg SunOS4 doesn't have RTLD_NOW */
41     ObjectFile instance = dlopen(dll,RTLD_LAZY);
42 #else /* eg FreeBSD doesn't have RTLD_LAZY */
43     ObjectFile instance = dlopen(dll,1);
44 #endif
45     if (NULL == instance) {
46         ERRMSG(0) "Error %s while importing DLL \"%s\"", dlerror(), dll
47         EEND;
48     }
49     return dlsym(instance,symbol);
50 }
51
52 #elif HAVE_DL_H /* eg HPUX */
53
54 #include <dl.h>
55
56 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
57 String dll;
58 String symbol; {
59     ObjectFile instance = shl_load(dll,BIND_IMMEDIATE,0L);
60     void* r;
61     if (NULL == instance) {
62         ERRMSG(0) "Error while importing DLL \"%s\"", dll
63         EEND;
64     }
65     return (0 == shl_findsym(&instance,symbol,TYPE_PROCEDURE,&r)) ? r : 0;
66 }
67
68 #elif HAVE_WINDOWS_H && !defined(__MSDOS__)
69
70 #include <windows.h>
71
72 ObjectFile loadLibrary(fn)
73 String fn; {
74     return LoadLibrary(fn);
75 }
76
77 void* lookupSymbol(file,symbol)
78 ObjectFile file;
79 String symbol; {
80     return GetProcAddress(file,symbol);
81 }
82
83 const char *dlerror(void)
84 {
85    return "<unknown>";
86 }
87
88 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
89 String dll;
90 String symbol; {
91     ObjectFile instance = LoadLibrary(dll);
92     if (NULL == instance) {
93         /* GetLastError allegedly provides more detail - in practice,
94          * it tells you nothing more.
95          */
96         ERRMSG(0) "Error while importing DLL \"%s\"", dll
97         EEND;
98     }
99     return GetProcAddress(instance,symbol);
100 }
101
102 #else /* Dynamic loading not available */
103
104 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
105 String dll;
106 String symbol; {
107 #if 1 /* very little to choose between these options */
108     return 0;
109 #else
110     ERRMSG(0) "This Hugs build does not support dynamic loading\n"
111     EEND;
112 #endif
113 }
114
115 #endif /* Dynamic loading not available */
116