3

I'm running into the following error when trying to compile a static Haskell executable:

/usr/bin/ld.gold: error: /usr/lib/gcc/x86_64-linux-gnu/6/crtbeginT.o: requires dynamic R_X86_64_32 reloc against '__TMC_END__' which may overflow at runtime; recompile with -fPIC
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)

I believe I'm running into this issue due to creating a static build. A dynamic build was successful previously.

How could I solve this? I don't think I've compiled anything manually. Here is the Dockerfile from the docker image I'm using: https://github.com/freebroccolo/docker-haskell/blob/03467e1a14543d83d33833e669249a3c42f7b7c8/8.6/Dockerfile

I'm running Debian Stretch (9).

Seems to possibly be a bug according to: https://bugs.launchpad.net/ubuntu/+source/gcc-4.4/+bug/640734


I've tried adding a --ghc-options='-fPIC to stack build but that fails too:

 /usr/bin/ld.gold: error: /usr/lib/gcc/x86_64-linux-gnu/6/crtbeginT.o: requires dynamic R_X86_64_32 reloc against '__TMC_END__' which may overflow at runtime; recompile with -fPIC
    collect2: error: ld returned 1 exit status
    `gcc' failed in phase `Linker'. (Exit code: 1)
The command '/bin/sh -c stack build --ghc-options='-fPIC -optl-static -optl-pthread' --force-dirty --copy-bins' returned a non-zero code: 1
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
  • 1
    Are you already familiar with GCC's -fPIC option? (I gather that you likely are, but should not assume. This is why I ask.) – thb Feb 03 '19 at 23:50
  • I'm not - I assumed I needed to modify a 'dependency' rather than the flags to Haskell's GHC compiler (which seems to be the case). I've just added that option and trying to build. Thanks! – Chris Stryczynski Feb 03 '19 at 23:58
  • 1
    Possibly related: https://stackoverflow.com/questions/41419102/haskell-stack-static-binary-relocation-r-x86-64-32-against-tmc-end-can-not – Chris Stryczynski Feb 05 '19 at 00:37

2 Answers2

2

My answer is probably incomplete but, until a more complete answer appears, we can start with the following.

1. GCC's manual. Debian and the Free Software Foundation unfortunately have slightly conflicting understandings of what constitutes free software. GCC's manual might help you at the moment, but the manual has been segregated for minor, technical legal reasons to Debian's non-free archive.

It goes against the spirit of Debian to advise anyone to install from non-free, doesn't it? I will only say that I have installed GCC's manual from non-free on my own Debian box as follows. In /etc/apt/sources.list:

deb     http://mirror.us.leaseweb.net/debian/ stretch main contrib non-free
deb-src http://mirror.us.leaseweb.net/debian/ stretch main contrib non-free

(You can list whichever mirror you like, but Leaseweb's is as good as any.) Then, apt-get update and apt-get install gcc-doc, followed by info gcc.)

2. Options to GCC. In GCC's manual, you can open the chapter "Invoking GCC," open the section "Code Gen Options," and press / to search for -fPIC. The explanation there is terse and technical, but it starts your investigation in the right place. (Even if the explanation does not make sense to you now, you'll return to it later.)

3. Uekawa's Debian Library Packaging Guide. The most illuminating document however is somewhat a secret. It is a standard Debian document, known to Debian Developers, which for obscure reasons is not packaged in Debian, so non-Developers usually don't know about it. Here it is.

The linked document tells you how these libraries work, how one compiles and links them, and so on. However, if you need to know more....

4. The ELF Specification.

These detail the format to which GCC builds and ld.gold links.

5. Autotools. At some point, your investigation might lead you to the GNU Autotools—namely, Autoconf, Automake, Libtool, M4, and so on. I do not believe that the Autotools will solve your problem and, even if they did, I doubt that they would do it in a way that illuminated your understanding of the process. The Autotools can be useful but, in your particular case, I would avoid pursuing the Autotools for the time being. Focus on the other sources, rather.

Admittedly, someone out there probably knows a more direct answer to your question but, as of this writing, that person had not yet posted. Meanwhile, if you need to research it for yourself, my answer has given the sources. Reading those is what I would do. Though not a Haskell programmer, reading those is how I have solved similar linking problems in the past. Those are the standard sources.

The reading is most educational and is broadly useful in any case, so it won't waste your time.

(I would recommend that you avoid accepting my answer even if you choose to upvote it. Your question is a good question. We want this question to remain open until it has drawn a more direct answer than mine.)

thb
  • 13,796
  • 3
  • 40
  • 68
1

Arghiaueohtnuea...

I had ghc-options: -O2 -dynamic in my cabal file... Removing this seems to have fixed everything

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
  • Excellent. Linking issues are hard but solving them is usually educational. One doubts that you have wasted your time. Glad that it works. – thb Feb 05 '19 at 01:28