57653d578f822ec87265bef3b75320d6c3d0aacd
[ghc-hetmet.git] / ghc / interpreter / dynamic.c
1
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.4 $
11  * $Date: 1999/03/01 14:46:45 $
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 #if 0 /* apparently unused */
25 ObjectFile loadLibrary(fn)
26 String fn; {
27     return dlopen(fn,RTLD_NOW | RTLD_GLOBAL);
28 }
29
30 void* lookupSymbol(file,symbol)
31 ObjectFile file;
32 String symbol; {
33     return dlsym(file,symbol);
34 }
35 #endif
36
37 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
38 String dll;
39 String symbol; {
40 #ifdef RTLD_NOW
41     ObjectFile instance = dlopen(dll,RTLD_NOW);
42 #elif defined RTLD_LAZY /* eg SunOS4 doesn't have RTLD_NOW */
43     ObjectFile instance = dlopen(dll,RTLD_LAZY);
44 #else /* eg FreeBSD doesn't have RTLD_LAZY */
45     ObjectFile instance = dlopen(dll,1);
46 #endif
47     if (NULL == instance) {
48         ERRMSG(0) "Error %s while importing DLL \"%s\"", dlerror(), dll
49         EEND;
50     }
51     return dlsym(instance,symbol);
52 }
53
54 #elif HAVE_DL_H /* eg HPUX */
55
56 #include <dl.h>
57
58 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
59 String dll;
60 String symbol; {
61     ObjectFile instance = shl_load(dll,BIND_IMMEDIATE,0L);
62     void* r;
63     if (NULL == instance) {
64         ERRMSG(0) "Error while importing DLL \"%s\"", dll
65         EEND;
66     }
67     return (0 == shl_findsym(&instance,symbol,TYPE_PROCEDURE,&r)) ? r : 0;
68 }
69
70 #elif HAVE_WINDOWS_H && !defined(__MSDOS__)
71
72 #include <windows.h>
73
74 ObjectFile loadLibrary(fn)
75 String fn; {
76     return LoadLibrary(fn);
77 }
78
79 void* lookupSymbol(file,symbol)
80 ObjectFile file;
81 String symbol; {
82     return GetProcAddress(file,symbol);
83 }
84
85 const char *dlerror(void)
86 {
87    return "<unknown>";
88 }
89
90 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
91 String dll;
92 String symbol; {
93     ObjectFile instance = LoadLibrary(dll);
94     if (NULL == instance) {
95         /* GetLastError allegedly provides more detail - in practice,
96          * it tells you nothing more.
97          */
98         ERRMSG(0) "Error while importing DLL \"%s\"", dll
99         EEND;
100     }
101     return GetProcAddress(instance,symbol);
102 }
103
104 #else /* Dynamic loading not available */
105
106 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
107 String dll;
108 String symbol; {
109 #if 1 /* very little to choose between these options */
110     return 0;
111 #else
112     ERRMSG(0) "This Hugs build does not support dynamic loading\n"
113     EEND;
114 #endif
115 }
116
117 #endif /* Dynamic loading not available */
118