[GHC] #3807: Test for correct shared library generation
GHC
trac at galois.com
Wed Jan 6 18:34:19 EST 2010
#3807: Test for correct shared library generation
---------------------------------+------------------------------------------
Reporter: asuffield | Owner:
Type: bug | Status: new
Priority: normal | Component: Test Suite
Version: 6.12.1 | Keywords:
Os: Unknown/Multiple | Testcase:
Architecture: Unknown/Multiple | Failure: None/Unknown
---------------------------------+------------------------------------------
There doesn't seem to be a test that verifies shared libraries are
correctly created. It's not immediately obvious how to get the test suite
driver to run this sort of thing, so I don't have a full patch for it, but
this illustrates how it should work on ELF and ELF-like platforms:
{{{
asuffield at cyclone:~/work/ghc-test$ cat TestExport.hs
module Test where
asuffield at cyclone:~/work/ghc-test$ cat test-export.c
#include <HsFFI.h>
extern void __stginit_Test(void);
void
test_init (void)
{
static char *argv[] = { "test.so", 0 }, **argv_ = argv;
static int argc = 1;
hs_init (&argc, &argv_);
hs_add_root (__stginit_Test);
}
void
test_exit (void)
{
hs_exit ();
}
asuffield at cyclone:~/work/ghc-test$ cat testload.c
#include <dlfcn.h>
#include <stdio.h>
int main(void) {
void *dh = dlopen("./test.so", RTLD_NOW | RTLD_GLOBAL);
if (!dh) {
printf("%s\n", dlerror());
return 1;
}
void (*test_init)(void) = dlsym(dh, "test_init");
void (*test_exit)(void) = dlsym(dh, "test_exit");
test_init();
test_exit();
return 0;
}
asuffield at cyclone:~/work/ghc-test$ ghc --make -dynamic -fPIC -shared
TestExport.hs test-export.c -o test.so
[1 of 1] Compiling Test ( TestExport.hs, TestExport.o )
Linking test.so ...
asuffield at cyclone:~/work/ghc-test$ gcc -o testload testload.c -ldl
asuffield at cyclone:~/work/ghc-test$ ./testload
/usr/lib/ghc-6.12.0.20091126/ghc-prim-0.2.0.0/libHSghc-
prim-0.2.0.0-ghc6.12.0.20091126.so: undefined symbol: stg_newByteArrayzh
}}}
You'll note that the test failed here, in a way which illustrates why the
current FFI tests don't properly exercise this: the shared library
references symbols from libHSrts*.so, but does not link to it. If testload
had been compiled with ghc instead of gcc (like all the current FFI tests)
then the binary itself would have picked up that library and caused a
false pass; doing it this way verifies that shared libraries can be loaded
into foreign programs. The use of dlopen here is simply to force strict
loading of the shared library in a reasonably portable manner, since
linking directly to it is lazily evaluated on many platforms.
Incidentally, I suspect this will fail on current versions.
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/3807>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the Glasgow-haskell-bugs
mailing list