2 /* --------------------------------------------------------------------------
3 * Dynamic loading (of .dll or .so files) for Hugs
5 * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
6 * Yale Haskell Group, and the Oregon Graduate Institute of Science and
7 * Technology, 1994-1999, All rights reserved. It is distributed as
8 * free software under the license in the file "License", which is
9 * included in the distribution.
11 * $RCSfile: dynamic.c,v $
13 * $Date: 1999/10/22 10:00:19 $
14 * ------------------------------------------------------------------------*/
21 #if HAVE_WINDOWS_H && !defined(__MSDOS__)
25 ObjectFile loadLibrary(fn)
27 return LoadLibrary(fn);
30 void* lookupSymbol(file,symbol)
33 return GetProcAddress(file,symbol);
36 const char *dlerror(void)
41 void* getDLLSymbol(dll,symbol) /* load dll and lookup symbol */
44 ObjectFile instance = LoadLibrary(dll);
45 if (NULL == instance) {
46 /* GetLastError allegedly provides more detail - in practice,
47 * it tells you nothing more.
49 ERRMSG(0) "Error while importing DLL \"%s\"", dll
52 return GetProcAddress(instance,symbol);
55 #elif HAVE_DLFCN_H /* eg LINUX, SOLARIS, ULTRIX */
60 ObjectFile loadLibrary(fn)
62 return dlopen(fn,RTLD_NOW | RTLD_GLOBAL);
65 void* lookupSymbol(file,symbol)
68 return dlsym(file,symbol);
71 void* getDLLSymbol(dll,symbol) /* load dll and lookup symbol */
75 ObjectFile instance = dlopen(dll,RTLD_NOW);
76 #elif defined RTLD_LAZY /* eg SunOS4 doesn't have RTLD_NOW */
77 ObjectFile instance = dlopen(dll,RTLD_LAZY);
78 #else /* eg FreeBSD doesn't have RTLD_LAZY */
79 ObjectFile instance = dlopen(dll,1);
83 if (NULL == instance) {
84 ERRMSG(0) "Error while importing DLL \"%s\":\n%s\n", dll, dlerror()
87 if ((sym = dlsym(instance,symbol)))
90 ERRMSG(0) "Error loading sym:\n%s\n", dlerror()
94 #elif HAVE_DL_H /* eg HPUX */
98 void* getDLLSymbol(dll,symbol) /* load dll and lookup symbol */
101 ObjectFile instance = shl_load(dll,BIND_IMMEDIATE,0L);
103 if (NULL == instance) {
104 ERRMSG(0) "Error while importing DLL \"%s\"", dll
107 return (0 == shl_findsym(&instance,symbol,TYPE_PROCEDURE,&r)) ? r : 0;
110 #else /* Dynamic loading not available */
112 void* getDLLSymbol(dll,symbol) /* load dll and lookup symbol */
115 #if 1 /* very little to choose between these options */
118 ERRMSG(0) "This Hugs build does not support dynamic loading\n"
123 #endif /* Dynamic loading not available */