Skip to content

Instantly share code, notes, and snippets.

@bts
Created January 3, 2018 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bts/0f0abdef6f69b1a165181ef1d1be0360 to your computer and use it in GitHub Desktop.
Save bts/0f0abdef6f69b1a165181ef1d1be0360 to your computer and use it in GitHub Desktop.
[1 of 1] Compiling Columnate ( src/Columnate.hs, dist/build/Columnate.o )
/nix/store/drcl6z8jqwkqkbwspilyk017biyfd969-binutils-2.28.1/bin/ld: /nix/store/v8qa2qbwrb7s8b4piygdiii7p7jpv068-gcc-6.4.0/lib/gcc/x86_64-unknown-linux-gnu/6.4.0/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
/nix/store/drcl6z8jqwkqkbwspilyk017biyfd969-binutils-2.28.1/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
`cc' failed in phase `Linker'. (Exit code: 1)
@bts
Copy link
Author

bts commented Jan 3, 2018

this from when I try to add bts/columnate@976906e

before adding these configure flags, ldd output looks like:

> ldd result/bin/columnate
	linux-vdso.so.1 (0x00007fff89bf5000)
	librt.so.1 => /nix/store/xzx1bv1d7z4mgg6sg6ly0jx609qvka4x-glibc-2.25-49/lib/librt.so.1 (0x00007f3f3c9db000)
	libutil.so.1 => /nix/store/xzx1bv1d7z4mgg6sg6ly0jx609qvka4x-glibc-2.25-49/lib/libutil.so.1 (0x00007f3f3c7d8000)
	libdl.so.2 => /nix/store/xzx1bv1d7z4mgg6sg6ly0jx609qvka4x-glibc-2.25-49/lib/libdl.so.2 (0x00007f3f3c5d4000)
	libm.so.6 => /nix/store/xzx1bv1d7z4mgg6sg6ly0jx609qvka4x-glibc-2.25-49/lib/libm.so.6 (0x00007f3f3c2c1000)
	libgmp.so.10 => /nix/store/h4hz48cvx05f1gw6mcqrdkxvq8b2s2d5-gmp-6.1.2/lib/libgmp.so.10 (0x00007f3f3c02e000)
	libpthread.so.0 => /nix/store/xzx1bv1d7z4mgg6sg6ly0jx609qvka4x-glibc-2.25-49/lib/libpthread.so.0 (0x00007f3f3be10000)
	libc.so.6 => /nix/store/xzx1bv1d7z4mgg6sg6ly0jx609qvka4x-glibc-2.25-49/lib/libc.so.6 (0x00007f3f3ba71000)
	/nix/store/xzx1bv1d7z4mgg6sg6ly0jx609qvka4x-glibc-2.25-49/lib/ld-linux-x86-64.so.2 (0x00007f3f3cbe3000)

@vaibhavsagar
Copy link

This suggests that you need to add -fPIC, so can you add "--ghc-option=-fPIC" to configureFlags?

@bts
Copy link
Author

bts commented Jan 3, 2018

@vaibhavsagar thanks for the idea! unfortunately no luck; I get the same error with the extra flag. seems like this is due to lack of support for x86 linux for PIC (https://ghc.haskell.org/trac/ghc/wiki/SharedLibraries/PlatformSupport):

GHC can generate position independent code (when given the -fPIC flag) on Mac OS X (Darwin/PPC) and PowerPC (32-bit) Linux. Some minor work is required to make it generate position independent code for x86 ELF platforms (Linux and others). Position independent code is not needed for Windows and PowerPC64 Linux.

@vaibhavsagar
Copy link

I tried this with GHC 8.2.2 and it didn't seem to make a difference:

{ nixpkgs ? import <nixpkgs> {}, compiler ? "ghc822" }:

let

  inherit (nixpkgs) pkgs;

  f = { mkDerivation, base, hedgehog, optparse-generic, stdenv
      , tasty, tasty-hedgehog, text, these
      }: let these' = pkgs.haskell.lib.doJailbreak these; in
      mkDerivation {
        pname = "columnate";
        version = "0.1.0.0";
        src = ./.;
        isLibrary = true;
        isExecutable = true;
        libraryHaskellDepends = [ base optparse-generic text these' ];
        executableHaskellDepends = [ base ];
        testHaskellDepends = [ base hedgehog tasty tasty-hedgehog text ];
        homepage = "http://github.com/bts/columnate";
        description = "Columnate data sets while preserving color codes";
        license = stdenv.lib.licenses.bsd3;
        enableSharedExecutables = false;
        configureFlags = [
          "--ghc-option=-optl=-static"
          "--ghc-option=-optl=-pthread"
          "--ghc-option=-optl=-L${pkgs.gmp6.static}/lib"
          "--ghc-option=-optl=-L${pkgs.glibc.static}/lib"
          "--ghc-option=-fPIC"
        ];
      };

  haskellPackages = if compiler == "default"
                       then pkgs.haskellPackages
                       else pkgs.haskell.packages.${compiler};

  drv = haskellPackages.callPackage f {};

in

  if pkgs.lib.inNixShell then drv.env else drv

@vaibhavsagar
Copy link

This might be a wild goose chase, but I suspect the crtbeginT.o/crtbeginS.o swap might make a difference, so I have this:

{ nixpkgs ? import <nixpkgs> {}, compiler ? "default" }:

let

  inherit (nixpkgs) pkgs;

  gcc' = pkgs.wrapCC (pkgs.runCommand "gcc" {
      src = pkgs.stdenv.cc.cc;
      isGNU = true;
    } ''
      mkdir -p $out
      cp -r --no-preserve=mode,ownership $src/* $out
      cd $out/lib/gcc/x86*/6*
      cp crtbeginS.o crtbeginT.o
    '');

  stdenv' = pkgs.overrideCC pkgs.stdenv gcc';

  f = { base, hedgehog, optparse-generic
      , tasty, tasty-hedgehog, text, these
      }:
      stdenv'.mkDerivation {
        pname = "columnate";
        version = "0.1.0.0";
        src = ./.;
        isLibrary = true;
        isExecutable = true;
        libraryHaskellDepends = [ base optparse-generic text these ];
        executableHaskellDepends = [ base ];
        testHaskellDepends = [ base hedgehog tasty tasty-hedgehog text ];
        homepage = "http://github.com/bts/columnate";
        description = "Columnate data sets while preserving color codes";
        license = stdenv'.lib.licenses.bsd3;
      };

  haskellPackages = if compiler == "default"
                       then pkgs.haskellPackages
                       else pkgs.haskell.packages.${compiler};

  drv = haskellPackages.callPackage f {};

in

  if pkgs.lib.inNixShell then drv.env else drv

and it fails to build with a confusing error about the license attribute. Maybe the regular mkDerivation and Haskell's mkDerivation are different?

@vaibhavsagar
Copy link

I don't think that made a difference either. For posterity:

{ nixpkgs ? import <nixpkgs> {}, compiler ? "ghc822" }:

let

  inherit (nixpkgs) pkgs;

  gcc-hack = pkgs.wrapCC (pkgs.runCommand "gcc" {
      src = pkgs.gcc-unwrapped;
      isGNU = true;
    } ''
      mkdir -p $out
      cp -r --no-preserve=mode,ownership $src/* $out
      cd $out/lib/gcc/x86*/6*
      cp crtbeginS.o crtbeginT.o
    '');

  f = { mkDerivation, base, hedgehog, optparse-generic, stdenv
      , tasty, tasty-hedgehog, text, these
      }: let
        these' = pkgs.haskell.lib.doJailbreak these;
        stdenv' = stdenv.override { allowedRequisites = null; cc = gcc-hack; };
        mkDerivation' = args: (mkDerivation args).overrideDerivation (drv: { stdenv = stdenv'; });
      in
      mkDerivation' {
        pname = "columnate";
        version = "0.1.0.0";
        src = ./.;
        isLibrary = true;
        isExecutable = true;
        libraryHaskellDepends = [ base optparse-generic text these' ];
        executableHaskellDepends = [ base ];
        testHaskellDepends = [ base hedgehog tasty tasty-hedgehog text ];
        homepage = "http://github.com/bts/columnate";
        description = "Columnate data sets while preserving color codes";
        license = pkgs.stdenv.lib.licenses.bsd3;
        enableSharedExecutables = false;
        configureFlags = [
          "--ghc-option=-optl=-static"
          "--ghc-option=-optl=-pthread"
          "--ghc-option=-optl=-L${pkgs.gmp5.static}/lib"
          "--ghc-option=-optl=-L${pkgs.glibc.static}/lib"
          "--ghc-option=-fPIC"
        ];
      };

  haskellPackages = if compiler == "default"
                       then pkgs.haskellPackages
                       else pkgs.haskell.packages.${compiler};

  drv = haskellPackages.callPackage f {};

in

  if pkgs.lib.inNixShell then drv.env else drv

And the error seems to be related to https://stackoverflow.com/questions/44429253/building-a-shared-library-created-a-static-library-instead

@vaibhavsagar
Copy link

vaibhavsagar commented Jan 17, 2018

I did it! I got a pull request vaibhavsagar/experiments#3 that clarified a few things. Here's a working derivation:

{ nixpkgs ? import <nixpkgs> {}, compiler ? "ghc822" }:

let

  inherit (nixpkgs) pkgs;

  f = { mkDerivation, base, hedgehog, optparse-generic, stdenv
      , tasty, tasty-hedgehog, text, these
      }: let
        these' = pkgs.haskell.lib.doJailbreak these;
      in
      mkDerivation {
        pname = "columnate";
        version = "0.1.0.0";
        src = ./.;
        isLibrary = true;
        isExecutable = true;
        libraryHaskellDepends = [ base optparse-generic text these' ];
        executableHaskellDepends = [ base ];
        testHaskellDepends = [ base hedgehog tasty tasty-hedgehog text ];
        homepage = "http://github.com/bts/columnate";
        description = "Columnate data sets while preserving color codes";
        license = pkgs.stdenv.lib.licenses.bsd3;
        enableSharedExecutables = false;
        enableSharedLibraries = false;
        configureFlags = [
          "--ghc-option=-optl=-static"
          "--ghc-option=-optl=-L${pkgs.gmp5.static}/lib"
          "--ghc-option=-optl=-L${pkgs.glibc.static}/lib"
          "--ghc-option=-fPIC"
        ];
      };

  haskellPackages = if compiler == "default"
                       then pkgs.haskellPackages
                       else pkgs.haskell.packages.${compiler};

  drv = haskellPackages.callPackage f {};

in

  if pkgs.lib.inNixShell then drv.env else drv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment