GHC 9.2.x: Linking error when using -pgmc (PIE/relocation failure)
- Dominant language
- Haskell
- Stars
- 1.7k
- Forks
- 750
- Avg merge
- 4d 3h
- Merged PRs (30d)
- 28
Description
**Describe the bug**
When using the `-pgmc` flag to specify a custom C compiler (or a wrapper script) with GHC 9.2.8 and below, the build fails at the linking stage with a PIE (Position Independent Executable) error. It appears that using `-pgmc` prevents GHC from automatically injecting the necessary flags required to build PIE-compatible objects in these older GHC versions.
**To Reproduce**
The issue is consistently reproducible in the cabal test suite:
`PackageTests/FFI/ForeignOptsPgmc/cabal.test.hs`
Steps to reproduce:
1. Use GHC 9.2.8.
2. Attempt to build a project with FFI (C sources) passing `-pgmc `.
3. The build will fail during the linking phase.
**Expected behavior**
The build should succeed. The use of `-pgmc` should not interfere with GHC's ability to inject the required PIE/PIC flags for the C compiler and linker.
**System information**
- Operating system: Linux (x86_64)
- `cabal` version: 3.17.0.0
- `ghc` version: 9.2.8
**Additional context**
The linker fails with the following error:
```
/usr/bin/ld: .../Main.o: relocation R_X86_64_32S against symbol `stg_bh_upd_frame_info` can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
```
It seems that when `-pgmc` is provided, GHC fails to properly configure the C compilation flags for PIE compatibility, which GHC usually handles automatically. This behavior is specific to GHC 9.2.x and earlier versions.
Contributor guide
Research direction
Start with PackageTests/FFI/ForeignOptsPgmc/cabal.test.hs and reproduce the failure using GHC 9.2.8 with -pgmc and a wrapper script. Trace how Cabal passes the custom compiler and how GHC supplies PIE/PIC flags for the C sources. Done means the test builds and links successfully without the relocation error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, haskell
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100