[project @ 1999-10-15 19:11:54 by andy]
[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.6 $
11  * $Date: 1999/10/15 19:11:54 $
12  * ------------------------------------------------------------------------*/
13
14 #include "prelude.h"
15 #include "storage.h"
16 #include "errors.h"
17 #include "dynamic.h"
18
19 #if HAVE_WINDOWS_H && !defined(__MSDOS__)
20
21 #include <windows.h>
22
23 ObjectFile loadLibrary(fn)
24 String fn; {
25     return LoadLibrary(fn);
26 }
27
28 void* lookupSymbol(file,symbol)
29 ObjectFile file;
30 String symbol; {
31     return GetProcAddress(file,symbol);
32 }
33
34 const char *dlerror(void)
35 {
36    return "<unknown>";
37 }
38
39 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
40 String dll;
41 String symbol; {
42     ObjectFile instance = LoadLibrary(dll);
43     if (NULL == instance) {
44         /* GetLastError allegedly provides more detail - in practice,
45          * it tells you nothing more.
46          */
47         ERRMSG(0) "Error while importing DLL \"%s\"", dll
48         EEND;
49     }
50     return GetProcAddress(instance,symbol);
51 }
52
53 #elif HAVE_DLFCN_H /* eg LINUX, SOLARIS, ULTRIX */
54
55 #include <stdio.h>
56 #include <dlfcn.h>
57
58 ObjectFile loadLibrary(fn)
59 String fn; {
60     return dlopen(fn,RTLD_NOW | RTLD_GLOBAL);
61 }
62
63 void* lookupSymbol(file,symbol)
64 ObjectFile file;
65 String symbol; {
66     return dlsym(file,symbol);
67 }
68
69 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
70 String dll;
71 String symbol; {
72 #ifdef RTLD_NOW
73     ObjectFile instance = dlopen(dll,RTLD_NOW);
74 #elif defined RTLD_LAZY /* eg SunOS4 doesn't have RTLD_NOW */
75     ObjectFile instance = dlopen(dll,RTLD_LAZY);
76 #else /* eg FreeBSD doesn't have RTLD_LAZY */
77     ObjectFile instance = dlopen(dll,1);
78 #endif
79     if (NULL == instance) {
80         ERRMSG(0) "Error %s while importing DLL \"%s\"", dlerror(), dll
81         EEND;
82     }
83     return dlsym(instance,symbol);
84 }
85
86 #elif HAVE_DL_H /* eg HPUX */
87
88 #include <dl.h>
89
90 void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
91 String dll;
92 String symbol; {
93     ObjectFile instance = shl_load(dll,BIND_IMMEDIATE,0L);
94     void* r;
95     if (NULL == instance) {
96         ERRMSG(0) "Error while importing DLL \"%s\"", dll
97         EEND;
98     }
99     return (0 == shl_findsym(&instance,symbol,TYPE_PROCEDURE,&r)) ? r : 0;
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