Skip to content

GHC 9.2.x: Linking error when using -pgmc (PIE/relocation failure) #11712

@zlonast

Description

@zlonast

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 <wrapper-script>.
  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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions