Building a shared library with FFI on x86_64

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.

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.

On Thu, 27 Jan 2011 20:59:06 +0100, 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 found your problem described at http://ubuntuforums.org/showthread.php?t=643081 and http://www.gentoo.org/proj/en/base/amd64/howtos/index.xml?part=1&chap=3 Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --
participants (2)
-
Eric Webster
-
Henk-Jan van Tuyl