summaryrefslogtreecommitdiff
path: root/ghc/interpreter/dynamic.c
blob: 3718c443e59281272d7a8b8ddcc7e015884dd6ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

/* --------------------------------------------------------------------------
 * Dynamic loading (of .dll or .so files) for Hugs
 *
 * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
 * Yale Haskell Group, and the Oregon Graduate Institute of Science and
 * Technology, 1994-1999, All rights reserved.  It is distributed as
 * free software under the license in the file "License", which is
 * included in the distribution.
 *
 * $RCSfile: dynamic.c,v $
 * $Revision: 1.7 $
 * $Date: 1999/10/15 21:41:05 $
 * ------------------------------------------------------------------------*/

#include "prelude.h"
#include "storage.h"
#include "errors.h"
#include "dynamic.h"

#if HAVE_WINDOWS_H && !defined(__MSDOS__)

#include <windows.h>

ObjectFile loadLibrary(fn)
String fn; {
    return LoadLibrary(fn);
}

void* lookupSymbol(file,symbol)
ObjectFile file;
String symbol; {
    return GetProcAddress(file,symbol);
}

const char *dlerror(void)
{
   return "<unknown>";
}

void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
String dll;
String symbol; {
    ObjectFile instance = LoadLibrary(dll);
    if (NULL == instance) {
        /* GetLastError allegedly provides more detail - in practice,
	 * it tells you nothing more.
         */
        ERRMSG(0) "Error while importing DLL \"%s\"", dll
        EEND;
    }
    return GetProcAddress(instance,symbol);
}

#elif HAVE_DLFCN_H /* eg LINUX, SOLARIS, ULTRIX */

#include <stdio.h>
#include <dlfcn.h>

ObjectFile loadLibrary(fn)
String fn; {
    return dlopen(fn,RTLD_NOW | RTLD_GLOBAL);
}

void* lookupSymbol(file,symbol)
ObjectFile file;
String symbol; {
    return dlsym(file,symbol);
}

void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
String dll;
String symbol; {
#ifdef RTLD_NOW
    ObjectFile instance = dlopen(dll,RTLD_NOW);
#elif defined RTLD_LAZY /* eg SunOS4 doesn't have RTLD_NOW */
    ObjectFile instance = dlopen(dll,RTLD_LAZY);
#else /* eg FreeBSD doesn't have RTLD_LAZY */
    ObjectFile instance = dlopen(dll,1);
#endif
    if (NULL == instance) {
        ERRMSG(0) "Error %s while importing DLL \"%s\"", dlerror(), dll
        EEND;
    }
    return dlsym(instance,symbol);
}

#elif HAVE_DL_H /* eg HPUX */

#include <dl.h>

void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
String dll;
String symbol; {
    ObjectFile instance = shl_load(dll,BIND_IMMEDIATE,0L);
    void* r;
    if (NULL == instance) {
        ERRMSG(0) "Error while importing DLL \"%s\"", dll
        EEND;
    }
    return (0 == shl_findsym(&instance,symbol,TYPE_PROCEDURE,&r)) ? r : 0;
}

#else /* Dynamic loading not available */

void* getDLLSymbol(dll,symbol)  /* load dll and lookup symbol */
String dll;
String symbol; {
#if 1 /* very little to choose between these options */
    return 0;
#else
    ERRMSG(0) "This Hugs build does not support dynamic loading\n"
    EEND;
#endif
}

#endif /* Dynamic loading not available */