Hi,
I am trying to build a very simple Haskell function (foo)
which I can then
call from C, using an example I got from the GHC FFI Wiki.
The problem is that
the resulting program fails to run with a seg fault.
Here is the code:
---Haskell Module start (Foo.hs)
module Foo where
foreign export ccall foo :: Int -> IO Int
foo :: Int -> IO Int
foo n = return (length (f n))
f :: Int -> [Int]
f 0 = []
f n = n:(f (n-1))
---Haskell Module end
/*- C module start foo.c*/
#include <stdio.h>
#include "Foo_stub.h"
int main(void)
{
int x = foo(100);
//printf("x = %d\n", x);
return 0;
}
/*- C module end */
The I build the a.out with the commands:
ghc -fglasgow-exts -fffi -c Foo.hs
ghc -fglasgow-exts -fffi Foo.o Foo_stub.o foo.c
Now if I run the resulting a.out then I get:
Segmentation fault
Quickly putting this through GDB I get:
gdb ./a.out
GNU gdb Red Hat Linux (6.5-16.el5rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public
License, and you are
welcome to change it and/or distribute copies of it under
certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show
warranty" for details.
This GDB was configured as
"i386-redhat-linux-gnu"...
(no debugging symbols found)
Using host libthread_db library
"/lib/libthread_db.so.1".
run
Starting program: /home/brg/dev/haskell/ffi/a.out
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
[Thread debugging using libthread_db enabled]
[New Thread 7870144 (LWP 22448)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 7870144 (LWP 22448)]
0x0805c313 in allocateLocal ()
(gdb) where
#0 0x0805c313 in allocateLocal ()
#1 0x0000005c in ?? ()
#2 0x08075c97 in base_DataziTypeable_zdLr42klvl4_srt ()
#3 0x4ee1dca0 in _dl_argv_internal () from
/lib/ld-linux.so.2
#4 0x0807c920 in last_free_capability ()
#5 0x4ee1dca0 in _dl_argv_internal () from
/lib/ld-linux.so.2
#6 0x00000000 in ?? ()
I am using GHC 6.6.1 built from sources on RHE5.
Many regards,
Ben
Benedict R. Gaster
S/W
Architecture Team Lead
ClearSpeed Technology Plc
3110 Great Western Court
Hunts Ground Road
Stoke Gifford
Bristol
BS34 8HP
Phone: + 44 117 3172016
Fax: + 44 117 3172002
brg@clearspeed.com
www.clearspeed.com