[Haskell-cafe] Building a shared library with FFI on x86_64

2011-01-27 Thread Eric Webster
I have a project that involves building a shared library containing code 
generated by GHC and exposed using the foreign function interface to 
other C programs that link against it. I'm able to build a functioning 
library without issue on 32bit x86 systems using GHC 6.8.2 and 6.12.3. 
When I try to build on a 64bit x86 system I get a relocation error that 
advises I include -fPIC. Including -fPIC does not help though. I'm 
wondering if I'm either doing something wrong or if this could be a 
problem with GHC.


I've created and attached a small example that illustrates this issue.

When building on 64bit x86 with GHC 7.0.1 and -fPIC and -dynamic I get 
the following error. (Similar with GHC 6.12.3)
/usr/bin/ld: CLibrary.o: relocation R_X86_64_PC32 against 
`CLibrary_testFunctionCWrapper_srt' can not be used when making a shared 
object; recompile with -fPIC

/usr/bin/ld: final link failed: Bad value

When building on 64bit x86 with GHC 7.0.1 and -fPIC and without -dynamic 
I get the following error (Similar with GHC 6.12.3 and 6.8.2)
/usr/bin/ld: 
/home/CIRG/ew/local/lib/ghc-7.0.1/base-4.3.0.0/libHSbase-4.3.0.0.a(Base__1.o): 
relocation R_X86_64_32S against `a local symbol' can not be used when 
making a shared object; recompile with -fPIC
/home/CIRG/ew/local/lib/ghc-7.0.1/base-4.3.0.0/libHSbase-4.3.0.0.a: 
could not read symbols: Bad value


When building on 32bit x86 with GHC 6.12.3 I get no error regardless of 
-fPIC or -dynamic.
When building on 32bit x86 with GHC 6.8.2 I get no error regardless of 
-fPIC and without -dynamic.


Any advice would be appreciated! Thanks.

module CLibrary where

import Foreign.C.Types
import Foreign.C.String
import Foreign.Marshal.Alloc
import Foreign.Marshal.Array
import Foreign.Ptr
import Foreign.Storable

foreign export ccall testFunction testFunctionCWrapper :: CString - IO (Ptr 
CString)

testFunctionCWrapper :: CString - IO (Ptr CString)
testFunctionCWrapper cs = do
  input - peekCString cs
  output1 - newCString hello world
  output2 - newCString input
  newArray [output1, output2]

LIB=test.so
OPTIONS=-fPIC -XForeignFunctionInterface -no-hs-main -optl -shared -rdynamic
#OPTIONS=-dynamic -fPIC -XForeignFunctionInterface -no-hs-main -optl -shared 
-rdynamic

GHC=ghc
#GHC=/home/CIRG/ew/local/bin/ghc

all:
$(GHC) $(OPTIONS) --make CLibrary -o $(LIB)

clean:
rm -rf *.hi *.o CLibrary_stub.c CLibrary_stub.h

pretty: clean
rm -rf *~ $(LIB)
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Building a shared library with FFI on x86_64

2011-01-27 Thread Eric Webster
Thanks to http://www.well-typed.com/blog/30 I was able to figure out 
what I was doing wrong. Replacing -optl -shared with just -shared 
and using -dynamic allows linking to complete. :D



On 2011-01-27 11:59 AM, Eric Webster wrote:
I have a project that involves building a shared library containing 
code generated by GHC and exposed using the foreign function interface 
to other C programs that link against it. I'm able to build a 
functioning library without issue on 32bit x86 systems using GHC 6.8.2 
and 6.12.3. When I try to build on a 64bit x86 system I get a 
relocation error that advises I include -fPIC. Including -fPIC does 
not help though. I'm wondering if I'm either doing something wrong or 
if this could be a problem with GHC.


I've created and attached a small example that illustrates this issue.

When building on 64bit x86 with GHC 7.0.1 and -fPIC and -dynamic I get 
the following error. (Similar with GHC 6.12.3)
/usr/bin/ld: CLibrary.o: relocation R_X86_64_PC32 against 
`CLibrary_testFunctionCWrapper_srt' can not be used when making a 
shared object; recompile with -fPIC

/usr/bin/ld: final link failed: Bad value

When building on 64bit x86 with GHC 7.0.1 and -fPIC and without 
-dynamic I get the following error (Similar with GHC 6.12.3 and 6.8.2)
/usr/bin/ld: 
/home/CIRG/ew/local/lib/ghc-7.0.1/base-4.3.0.0/libHSbase-4.3.0.0.a(Base__1.o): 
relocation R_X86_64_32S against `a local symbol' can not be used when 
making a shared object; recompile with -fPIC
/home/CIRG/ew/local/lib/ghc-7.0.1/base-4.3.0.0/libHSbase-4.3.0.0.a: 
could not read symbols: Bad value


When building on 32bit x86 with GHC 6.12.3 I get no error regardless 
of -fPIC or -dynamic.
When building on 32bit x86 with GHC 6.8.2 I get no error regardless of 
-fPIC and without -dynamic.


Any advice would be appreciated! Thanks.



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe