GHC compiling shared library linking problem

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello
This is the first time I'm mailing to this list, so I hope I have come
to the right place.
The problem I have is that I get a linker error when compiling a
shared library with GHC. This problem occurred after I updated GHC
from 7.0.3 to 7.4.1. Here is the error itself:
/usr/bin/ld: ../src/Shared.so relocation R_X86_64_32S against
`ghczmprim_GHCziTypes_ZC_con_info` can not be used when making a
shared object; recompile with -fPIC
../src/Shared.so: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
Here is the ghc command:
ghc -O2 --make -no-hs-main -optl '-shared' -optc '-DMODULE=Shared'
- - -o ../bin/pet_assembler_core.so Shared ../src/shared/module_init.c
- - -i../src/;
Shared.hs is the file where I specify the interface for the library,
it only has one function:
1 {-# LANGUAGE ForeignFunctionInterface #-}
2
3 module Shared where
4
5 import qualified Data.ByteString as BS
6 import Foreign.Ptr (Ptr)
7 import Foreign.Storable (poke)
8 import Foreign.Marshal.Utils (copyBytes)
9 import Foreign.Marshal.Alloc (mallocBytes)
10 import Foreign.C.Types (CSize, CChar)
11 import Foreign.C.String
12 import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
13 import Assembly.Data
14 import qualified Assembly as ASM
15
16 type PPString = Ptr (Ptr CChar)
17 type PSize = Ptr CSize
18
19 foreign export ccall assemble :: PPString -> PSize -> CString ->
IO Int
And the module_init.c contains initialization of the module:
1 #define CAT(a,b) XCAT(a,b)
2 #define XCAT(a,b) a ## b
3 #define STR(a) XSTR(a)
4 #define XSTR(a) #a
5
6 #include

Hello Richard, it sounds like you're using a ghc build that doesn't have the -dyn versions of the builtin libraries, do you have the same problem when doing a shared build without the foreign call ? On Mon, Oct 15, 2012 at 9:25 AM, Richard Zetterberg < richard.zetterberg@googlemail.com> wrote:
GHCziTypes_ZC_con_info` can not be used when making a shared object; recompile with -fPIC ../src/Shared.so: could not read

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello Thank you for the response! I have two parts of my application; one which builds a cli application which uses my module and one which builds a shared library which uses the same module. I have no problems compiling my cli application. And if I try to compile the shared library without the foreign export and the functions in Shared.hs I get another linker error: /usr/bin/ld: ../src/Assembly.o: relocation R_X86_64_32S against `stg_CAF_BLACKHOLE_info` can not be used when making a shared object; recompile with -fPIC ../src/Assembly.o: could not read symbols: Bad value collect2: error: ld returned 1 exit status (I forgot to add that I'm using the haskell-platform package in Debian sid (unstable). Here is the package and it's dependencies: http://packages.debian.org/sid/haskell-platform.) Best regards Richard On 10/15/12 6:44 PM, Carter Schonwald wrote: -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.17 (Darwin) Comment: GPGTools - http://gpgtools.org Comment: Using GnuPG with Mozilla - http://www.enigmail.net/ iQIcBAEBAgAGBQJQfmGPAAoJEP99n2Mvs+ma5KwP/1ZMZzZtNmQ7EUJSPg1mB3w7 Wh1KQtgLrfcCggfeMlA/XOtUE+pgtN66uarbi9iaqLULH4fLm/h6v12zSmaqG1uU XKuaIRtr886+bcFG/fXO0pW7OGbaF/w0nQN06iRqbFrce0f/U3VGHp8BqJNZFhSK qIHRM+WweM95LV9tgrCAeI3C2sGR4GkzhUunCCAOSZ8MfEwFxPV4OsmuCjKGcCcb GZXXhOynGhbLa8mg29dQNytt01AMgBBiRSWLHVFW6IfUxPk7uuQp33Q6wvjOUyA4 kJIUz9BU9IiPUeVdO2sg+fBB1ehOV2qPiqHf0xoJ1mpH6qd3KnUcJXsXrTXK4piz lo2lCOlqxspBiByX4HzyLE6pA+8OZcREO5GOHo5V4iI0RQAwkjaqARAU/6VVXHC0 fQbPJf8U1CQZZamkuoTgUfKcOHLFYIqVq9p8Ar1dykT74okAyMR+FU0ExWTbr/Xs 7oGD+Q44geh65FkkeLUoKIUD+aV35HQE6GL9O/OjKm1aMg3yGA5bM6UUiAw2FgqE jfZoHc9frO/WMP1XgEkKQjtupUCH92ol/PpyPbFJqfnxMvvvI+lYEEIL90XPudmS 5ygqeinIVwBKGVb6D08rLC1OzaS0dFasOjOeWYM12epbZsy9WCzIl+U14TFgy/Ze la4rqefI8sdBK1cQslYu =RN8H -----END PGP SIGNATURE-----

On 17/10/2012 08:43, Richard Zetterberg wrote:
I have two parts of my application; one which builds a cli application which uses my module and one which builds a shared library which uses the same module. I have no problems compiling my cli application. And if I try to compile the shared library without the foreign export and the functions in Shared.hs I get another linker error:
/usr/bin/ld: ../src/Assembly.o: relocation R_X86_64_32S against `stg_CAF_BLACKHOLE_info` can not be used when making a shared object; recompile with -fPIC ../src/Assembly.o: could not read symbols: Bad value collect2: error: ld returned 1 exit status
(I forgot to add that I'm using the haskell-platform package in Debian sid (unstable). Here is the package and it's dependencies: http://packages.debian.org/sid/haskell-platform.)
I think you're trying to make a shared library from Haskell code, right? You don't say what platform, but it looks like x86_64/Linux or maybe OS X. On these platforms, code that goes into a shared library must be compiled with -fPIC. You will also need to compile with -dynamic, in order to link against the shared versions of the RTS and the other Haskell libraries. Cheers, Simon
Best regards Richard
On 10/15/12 6:44 PM, Carter Schonwald wrote:
-----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.17 (Darwin) Comment: GPGTools - http://gpgtools.org Comment: Using GnuPG with Mozilla - http://www.enigmail.net/
iQIcBAEBAgAGBQJQfmGPAAoJEP99n2Mvs+ma5KwP/1ZMZzZtNmQ7EUJSPg1mB3w7 Wh1KQtgLrfcCggfeMlA/XOtUE+pgtN66uarbi9iaqLULH4fLm/h6v12zSmaqG1uU XKuaIRtr886+bcFG/fXO0pW7OGbaF/w0nQN06iRqbFrce0f/U3VGHp8BqJNZFhSK qIHRM+WweM95LV9tgrCAeI3C2sGR4GkzhUunCCAOSZ8MfEwFxPV4OsmuCjKGcCcb GZXXhOynGhbLa8mg29dQNytt01AMgBBiRSWLHVFW6IfUxPk7uuQp33Q6wvjOUyA4 kJIUz9BU9IiPUeVdO2sg+fBB1ehOV2qPiqHf0xoJ1mpH6qd3KnUcJXsXrTXK4piz lo2lCOlqxspBiByX4HzyLE6pA+8OZcREO5GOHo5V4iI0RQAwkjaqARAU/6VVXHC0 fQbPJf8U1CQZZamkuoTgUfKcOHLFYIqVq9p8Ar1dykT74okAyMR+FU0ExWTbr/Xs 7oGD+Q44geh65FkkeLUoKIUD+aV35HQE6GL9O/OjKm1aMg3yGA5bM6UUiAw2FgqE jfZoHc9frO/WMP1XgEkKQjtupUCH92ol/PpyPbFJqfnxMvvvI+lYEEIL90XPudmS 5ygqeinIVwBKGVb6D08rLC1OzaS0dFasOjOeWYM12epbZsy9WCzIl+U14TFgy/Ze la4rqefI8sdBK1cQslYu =RN8H -----END PGP SIGNATURE-----
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello and thank you for the reply Yes! It seems the details from my first mail (10/15/12 3:25 PM) fell out. Here is the relevant parts from that mail: " The problem I have is that I get a linker error when compiling a shared library with GHC. This problem occurred after I updated GHC from 7.0.3 to 7.4.1. Here is the error itself: /usr/bin/ld: ../src/Shared.so relocation R_X86_64_32S against `ghczmprim_GHCziTypes_ZC_con_info` can not be used when making a shared object; recompile with -fPIC ../src/Shared.so: could not read symbols: Bad value collect2: error: ld returned 1 exit status Here is the ghc command: ghc -O2 --make -no-hs-main -optl '-shared' -optc '-DMODULE=Shared' - -o ../bin/pet_assembler_core.so Shared ../src/shared/module_init.c - -i../src/; Information about the system: Linux 2.6.32-5-amd64 Debian 6.0.6 GHC 7.4.1 GCC 4.7.2 GNU ld 2.22 " I tried adding -fPIC and -dynamic to the ghc command: ghc -O2 --make -no-hs-main -optl '-shared' -optc '-DMODULE=Shared' - -o ../bin/pet_assembler_core.so Shared ../src/shared/module_init.c - -i../src/ -fPIC -dynamic; And got this error: ../src/Shared.hs:1:1: Could not find module `Prelude` Perhaps you haven't installed the "dyn" libraries for package `base'? Locations searched: Prelude.hs Prelude.lhs ../src/Prelude.hs ../src/Prelude.lhs /usr/lib/ghc/base-4.5.0.0/Prelude.dyn_hi Does that mean I can just install those libraries or do have to compile ghc from source and pass some flags to include those librares? Best regards Richard On 10/22/12 1:24 PM, Simon Marlow wrote:
I think you're trying to make a shared library from Haskell code, right? You don't say what platform, but it looks like x86_64/Linux or maybe OS X. On these platforms, code that goes into a shared library must be compiled with -fPIC. You will also need to compile with -dynamic, in order to link against the shared versions of the RTS and the other Haskell libraries.
Cheers, Simon
Best regards Richard
On 10/15/12 6:44 PM, Carter Schonwald wrote:
-----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.17 (Darwin) Comment: GPGTools - http://gpgtools.org Comment: Using GnuPG with Mozilla - http://www.enigmail.net/
iQIcBAEBAgAGBQJQfmGPAAoJEP99n2Mvs+ma5KwP/1ZMZzZtNmQ7EUJSPg1mB3w7 Wh1KQtgLrfcCggfeMlA/XOtUE+pgtN66uarbi9iaqLULH4fLm/h6v12zSmaqG1uU XKuaIRtr886+bcFG/fXO0pW7OGbaF/w0nQN06iRqbFrce0f/U3VGHp8BqJNZFhSK qIHRM+WweM95LV9tgrCAeI3C2sGR4GkzhUunCCAOSZ8MfEwFxPV4OsmuCjKGcCcb GZXXhOynGhbLa8mg29dQNytt01AMgBBiRSWLHVFW6IfUxPk7uuQp33Q6wvjOUyA4 kJIUz9BU9IiPUeVdO2sg+fBB1ehOV2qPiqHf0xoJ1mpH6qd3KnUcJXsXrTXK4piz lo2lCOlqxspBiByX4HzyLE6pA+8OZcREO5GOHo5V4iI0RQAwkjaqARAU/6VVXHC0 fQbPJf8U1CQZZamkuoTgUfKcOHLFYIqVq9p8Ar1dykT74okAyMR+FU0ExWTbr/Xs 7oGD+Q44geh65FkkeLUoKIUD+aV35HQE6GL9O/OjKm1aMg3yGA5bM6UUiAw2FgqE jfZoHc9frO/WMP1XgEkKQjtupUCH92ol/PpyPbFJqfnxMvvvI+lYEEIL90XPudmS 5ygqeinIVwBKGVb6D08rLC1OzaS0dFasOjOeWYM12epbZsy9WCzIl+U14TFgy/Ze la4rqefI8sdBK1cQslYu =RN8H -----END PGP SIGNATURE-----
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
-----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.17 (Darwin) Comment: GPGTools - http://gpgtools.org Comment: Using GnuPG with Mozilla - http://www.enigmail.net/ iQIcBAEBAgAGBQJQhTkLAAoJEP99n2Mvs+maat0P/RIqXRmsSwHw/9N7pcHPa3PC 6PtI6NvTb3djy0H/ErI5weKZbFQbfvh0vdB4uqH59/QA/IOCe6+SY0C1ez6n8W3/ nyGLi+EIPgENkzSFm2u/8L7+S0CL2A0sR+Yvx+3Ei7xvIcwa/3hKuvPYGNBzjODq 3RrYqQFA/AR/3A2yv7OafBtjALuntit4QJod8NdCc6mvgp2fcp0KcId+H3wjbhWU n053mWhJjMDY3DzOqDQi6BaSCj8+d4GZPOX1gdaxyIdXa7RM23QoLhHOAFoOPu2u PLIjQy9qRyMD9yXr36gii/N1Z32EtcyBYE5uyd7P6uqRv7cn4LRAWCNfT2icyjHG ph96YXYsTcQRgYh8HBlPWPgvQWLiBRs68RJBGLT1l5nAFzoMSFZYpb/Oh9J8d232 pfV4MwvakG1JVJuwJlGOfuWZJoTP+pMUA9GY+XTeXlPnGNlh5ATOs5BHDlECK52h U6m1qGQfaTlr+E4S4Af9N5XnwinV+YzXhVbmzAQtOFOgbQ+lW5b8zBev1CT8hUtt r4PNJdp44BJ2jBMx3VCrL/qWusDlQA0CrobOzHHHRlKEJG2jZnpTxT2VMOjpTC0I rQb/ehhosNvyaQglJ0RDjwf8596d5AfhYefv76iU1HBuf03JHQhh5wf+ltDotVc9 0Ld1V2LL1WtSnqs72r/n =mf8i -----END PGP SIGNATURE-----
participants (3)
-
Carter Schonwald
-
Richard Zetterberg
-
Simon Marlow