Compiling ghc with -fPIC support
Asked Answered
R

2

22

I'm trying to install GHC with -fPIC support in Fedora. I've grabbed a source tarball since it seems no binary one has this.

In Build.mk i've changed the quick build type to

ifeq "$(BuildFlavour)" "quick"

SRC_HC_OPTS        = -H64m -O0 -fasm -fPIC
GhcStage1HcOpts    = -O -fasm -fPIC
GhcStage2HcOpts    = -O0 -fasm -fPIC
GhcLibHcOpts       = -O -fasm -fPIC
SplitObjs          = NO
HADDOCK_DOCS       = NO
BUILD_DOCBOOK_HTML = NO
BUILD_DOCBOOK_PS   = NO
BUILD_DOCBOOK_PDF  = NO

endif

unfortunately, when compiling i still get the ld error

ghc -fglasgow-exts --make -shared -oHs2lib.a /tmp/Hs2lib924498/Hs2lib.hs dllmain.o -static -fno-warn-deprecated-flags -O2 -package ghc -package Hs2lib -i/home/phyx/Documents/Haskell/Hs2lib -optl-Wl,-s -funfolding-use-threshold=16 -optc-O3 -optc-ffast-math
Linking a.out ...
/usr/bin/ld: /tmp/Hs2lib924498/Hs2lib.o: relocation R_X86_64_32 against `ghczmprim_GHCziUnit_Z0T_closure' can not be used when making a shared object; recompile with -fPIC
/tmp/Hs2lib924498/Hs2lib.o: could not read symbols: Bad value

So it seems that GHC-prim still isn't compiled with -FPIC I've also told cabal to build any packages with -fPIC and shared.

Anyone have any ideas?

EDIT: Thanks to dcouts I've been able to make some progress. But now i'm at the point where I thnk libffi isn't compiled with -fPIC. I've edited the makefile(.in) for it but so far, no luck.

The new command is:

 ghc -fPIC -shared dllmain.o Hs2lib.o /usr/local/lib/ghc-7.0.3/libHSrts.a -o Hs2lib.so

where dllmain.c and Hs2lib.hs have both been compiled using -fPIC. The error I get is:

/usr/bin/ld: /usr/local/lib/ghc-7.0.3/libHSffi.a(closures.o): relocation R_X86_64_32 
against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/ghc-7.0.3/libHSffi.a: could not read symbols: Bad value

collect2: ld returned 1 exit status

Rolandorolandson answered 4/10, 2011 at 19:2 Comment(5)
which version of GHC do you try to compile?Sruti
Do you not need -optc-fpic or perhaps just -fPIC on the command line too?Acatalectic
@Thomas -fPIC only works with the native backend - that's why -fasm is explicitly given. a -optc option has no effect if the C-backend is not in use.Sruti
You say you made some progress but don't mention how. I have same problem. Can you tell me what to do?Meteorograph
@KaikoKaur Sorry, I never got this to work and I am now back to mostly being a windows developer so I didn't really look much further into this. From what I remember, the progress was having to manually specify the Haskell RTS in the compile after adding -fPIC to the ghc makefile and recompiling.Rolandorolandson
C
1

After you see this error, do the following:

cd /tmp/Hs2lib924498/
ghc -fglasgow-exts --make -shared -oHs2lib.a /tmp/Hs2lib924498/Hs2lib.hs dllmain.o -static -fno-warn-deprecated-flags -fPIC -O2 -package ghc -package Hs2lib -i/home/phyx/Documents/Haskell/Hs2lib -optl-Wl,-s -funfolding-use-threshold=16 -optc-O3 -optc-ffast-math

Note I added -fPIC to the failed ghc command.

Once the command succeeds, continue the compilation from within the tmp directory without cleaning already compiled files. It should skip them and continue where it ended.

Correlation answered 10/12, 2011 at 2:5 Comment(4)
That alone unfortunately won't do it. It seems the version of the command i posted here lacked -fPIC, but that's already in the command. dllmain.c is also compiled with -fPIC. I've progressed somewhat since I posted this question, and I'm now up to the point where libFFI is the problem. I've been told that build.mk doesn't have an influence on it's compilation, so i'm trying to it's config file to support it, but so far no luck.Rolandorolandson
What does the compiler say now? Can you post the new command and error message?Correlation
I can post it once i get back home in a few daysRolandorolandson
Sorry, was out of the country for a while, I've updated the question with my progress so far.Rolandorolandson
P
0

There's an FAQ entry on this topic on the Haskell Stack page.

It basically says the problem is environment related and sometimes non-deterministic.

The issue may be related to the use of hardening flags in some cases, specifically those related to producing position independent executables (PIE).

There's also a work around suggestion for Arch Linux:

On Arch Linux, installing the ncurses5-compat-libs package from AUR resolves this issue.

Prolix answered 30/11, 2017 at 7:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.