Test for correct shared library generation
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@cyclone:~/work/ghc-test$ cat TestExport.hs
module Test where
asuffield@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@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@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@cyclone:~/work/ghc-test$ gcc -o testload testload.c -ldl
asuffield@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.
Trac metadata
Trac field | Value |
---|---|
Version | 6.12.1 |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Test Suite |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |