!ayCRiZriCVtuCUpeLp:nixos.org

Nix Cross Compiling

553 Members
119 Servers

Load older messages


SenderMessageTime
10 Dec 2021
@symphorien:xlumurb.eusymphorien
In reply to @pepe:matrix.giugl.io
How can I do this?
the thing is: there are many flags in systems: like gcc tune flags in https://nixos.wiki/wiki/Build_flags
14:37:50
@symphorien:xlumurb.eusymphorieniirc there is a useLLVM one but not sure14:38:06
@symphorien:xlumurb.eusymphorienI can't tell much14:38:24
@symphorien:xlumurb.eusymphorien* I can't tell much more14:38:27
@mic92:nixos.devMic92
In reply to @symphorien:xlumurb.eu
I even suspect no one ever tried to use wrapCC with cross
Are not all the compilers in nixpkgs using that?
14:38:40
@pepe:matrix.giugl.ioErPepone Thanks symphorien :) 14:39:18
@symphorien:xlumurb.eusymphorienhonestly the part of nixpkgs that define stdenv's is really dark magic to me14:39:33
@pepe:matrix.giugl.ioErPeponeit's certainly interesting :D14:43:36
@pepe:matrix.giugl.ioErPeponeI keep going back there and every time I understand less 😆14:43:53
@pepe:matrix.giugl.ioErPeponeOk, alternative question16:33:09
@pepe:matrix.giugl.ioErPeponeI have my modified clang that is built as a "normal" 64bit package: I want to use it in a cross-compiler stdenv (with 32bit libs). Is it possible?16:33:43
@pepe:matrix.giugl.ioErPepone Long story short, I just want to compile binaries with -m32 16:35:05
@pepe:matrix.giugl.ioErPepone

This is what I have now (take a look at stdenv at the bottom):

{ pkgs, llvm, clang, lib, fmt, tinytoml }:
let
  python-deps = python-packages: with python-packages; [ pygments ];
  python = pkgs.python3.withPackages python-deps;
  ccache_path = "/nix/var/cache/ccache";

  derivation_function = { stdenv, llvmPackages_13, cmake, git, curl, pkg-config, z3
    , libxml2, ninja, ccache, ocaml, use_ccache ? false, debug ? false }:
    stdenv.mkDerivation {
      pname = "ropfuscator";
      version = "0.1.0";
      enableParallelBuilding = true;
      nativeBuildInputs = [
        cmake
        git
        curl
        pkg-config
        ninja
        llvmPackages_13.bintools
      ] ++ lib.optional (use_ccache == true) [ ccache ];
      buildInputs = [ libxml2 python ];
      srcs = [ ./cmake ./src ./thirdparty ];
      patches = [ ./patches/ropfuscator_pass.patch ];
      postPatch = "patchShebangs .";

      cmakeFlags = [
        "-DLLVM_TARGETS_TO_BUILD=X86"
        "-DLLVM_USE_LINKER=lld"
#        "-DLLVM_BUILD_32_BITS=On"
        "-DLLVM_ENABLE_BINDINGS=Off"
        "-DLLVM_INCLUDE_BENCHMARKS=Off"
        "-DLLVM_INCLUDE_EXAMPLES=Off"
        "-DLLVM_INCLUDE_TESTS=Off"
        "-DLLVM_BUILD_TOOLS=Off"
        "-DLLVM_TARGET_ARCH=X86"
        "-GNinja"
      ] ++ lib.optional (debug == true) [
        "-DCMAKE_BUILD_TYPE=Debug"
        "-DLLVM_PARALLEL_LINK_JOBS=2"
      ] ++ lib.optional (use_ccache == true) [ "-DLLVM_CCACHE_BUILD=On" ];

      CCACHE_DIR=ccache_path;

      unpackPhase = ''
        runHook preUnpack

        cp --no-preserve=mode,ownership -r ${llvm}/* .

        # insert clang
        pushd tools
          mkdir clang
          cp --no-preserve=mode,ownership -r ${clang}/* clang
        popd

        # insert ropfuscator
        pushd lib/Target/X86
          mkdir ropfuscator
          
          for s in $srcs; do
            # strip hashes
            cp --no-preserve=mode,ownership -r $s ropfuscator/`echo $s | cut -d "-" -f 2`
          done
          
          # manually copy submodules due to nix currently not having
          # proper support for submodules
          pushd ropfuscator/thirdparty
            mkdir -p {tinytoml,fmt}
            cp --no-preserve=mode,ownership -r ${tinytoml}/* tinytoml
            cp --no-preserve=mode,ownership -r ${fmt}/* fmt
          popd
        popd

        runHook postUnpack
      '';

      buildPhase = ''
        runHook preBuild

        cmake --build . -- clang

        runHook postBuild
      '';
    };
in let
  ropfuscator = pkgs.callPackage derivation_function { };
  stdenv = pkgs.pkgsCross.gnu32.overrideCC pkgs.pkgsCross.gnu32.stdenv ropfuscator;
in {
  ropfuscator = ropfuscator;
  stdenv = stdenv;
}

The shell has an empty CC and CXX env vars and if I manually run clang -m32 helloworld.c I get clang complaining about not finding stdio.h

16:36:37
@pepe:matrix.giugl.ioErPepone

I am here again to ask for your help (sanity beware). I am trying to get a stdenv where, again, you can use my custom compiler in a 32-bit environment.

This is what I have now:

{ pkgs, llvm, clang, lib, fmt, tinytoml }:
let
  pkgs32 = pkgs.pkgsi686Linux;
  python-deps = python-packages: with python-packages; [ pygments ];
  python = pkgs.python3.withPackages python-deps;
  ccache_path = "/nix/var/cache/ccache";

  derivation_function = { stdenv, llvmPackages_13, cmake, git, curl, pkg-config, z3
    , libxml2, ninja, ccache, ocaml, use_ccache ? false, debug ? false }:
    stdenv.mkDerivation {
      pname = "ropfuscator";
      version = "0.1.0";
      enableParallelBuilding = true;
      nativeBuildInputs = [
        cmake
        git
        curl
        pkg-config
        ninja
        llvmPackages_13.bintools
      ] ++ lib.optional (use_ccache == true) [ ccache ];
      buildInputs = [ libxml2 python ];
      srcs = [ ./cmake ./src ./thirdparty ];
      patches = [ ./patches/ropfuscator_pass.patch ];
      postPatch = "patchShebangs .";

      cmakeFlags = [
        "-DLLVM_TARGETS_TO_BUILD=X86"
        "-DLLVM_USE_LINKER=lld"
        "-DLLVM_ENABLE_BINDINGS=Off"
        "-DLLVM_INCLUDE_BENCHMARKS=Off"
        "-DLLVM_INCLUDE_EXAMPLES=Off"
        "-DLLVM_INCLUDE_TESTS=Off"
        "-DLLVM_BUILD_TOOLS=Off"
        "-DLLVM_TARGET_ARCH=X86"
        "-GNinja"
      ] ++ lib.optional (debug == true) [
        "-DCMAKE_BUILD_TYPE=Debug"
        "-DLLVM_PARALLEL_LINK_JOBS=2"
      ] ++ lib.optional (use_ccache == true) [ "-DLLVM_CCACHE_BUILD=On" ];

      CCACHE_DIR=ccache_path;

      unpackPhase = ''
        runHook preUnpack

        cp --no-preserve=mode,ownership -r ${llvm}/* .

        # insert clang
        pushd tools
          mkdir clang
          cp --no-preserve=mode,ownership -r ${clang}/* clang
        popd

        # insert ropfuscator
        pushd lib/Target/X86
          mkdir ropfuscator
          
          for s in $srcs; do
            # strip hashes
            cp --no-preserve=mode,ownership -r $s ropfuscator/`echo $s | cut -d "-" -f 2`
          done
          
          # manually copy submodules due to nix currently not having
          # proper support for submodules
          pushd ropfuscator/thirdparty
            mkdir -p {tinytoml,fmt}
            cp --no-preserve=mode,ownership -r ${tinytoml}/* tinytoml
            cp --no-preserve=mode,ownership -r ${fmt}/* fmt
          popd
        popd

        runHook postUnpack
      '';

      buildPhase = ''
        runHook preBuild

        cmake --build . -- clang

        runHook postBuild
      '';
    };
in let
  ropfuscator = pkgs.pkgsCross.gnu32.callPackage derivation_function { };
  wrapped-ropfuscator = pkgs.pkgsCross.gnu32.gcc.override { cc = ropfuscator; };
  stdenv = pkgs.pkgsCross.gnu32.overrideCC pkgs.pkgsCross.gnu32.stdenv wrapped-ropfuscator;
in {
  ropfuscator = wrapped-ropfuscator;
  stdenv = stdenv;
}

When I drop in a shell, though, and try to compile an hello world I get this:

$ clang test.c 
In file included from test.c:1:
In file included from /nix/store/qvvywcj0h2ii8n408xhjcvg0gw7l5n1q-glibc-i686-unknown-linux-gnu-2.33-56-dev/include/stdio.h:27:
In file included from /nix/store/qvvywcj0h2ii8n408xhjcvg0gw7l5n1q-glibc-i686-unknown-linux-gnu-2.33-56-dev/include/bits/libc-header-start.h:33:
In file included from /nix/store/qvvywcj0h2ii8n408xhjcvg0gw7l5n1q-glibc-i686-unknown-linux-gnu-2.33-56-dev/include/features.h:497:
/nix/store/qvvywcj0h2ii8n408xhjcvg0gw7l5n1q-glibc-i686-unknown-linux-gnu-2.33-56-dev/include/gnu/stubs.h:10:11: fatal error: 'gnu/stubs-64.h' file not found
# include <gnu/stubs-64.h>
          ^~~~~~~~~~~~~~~~
1 error generated.

Any ideas? I am really going crazy over this 😰

18:37:40
@symphorien:xlumurb.eusymphorienno idea, but missing stubs for 32/64 bits hints at using glibc_multi21:26:56
11 Dec 2021
@mic92:nixos.devMic92There also seems to be llvm/multi.nix06:02:31
@pepe:matrix.giugl.ioErPepone
  ropfuscator = pkgs.pkgsCross.gnu32.callPackage derivation_function { };
  wrapped-ropfuscator = pkgs.clang.override { cc = ropfuscator; };
  stdenv = pkgs.overrideCC pkgs.clangMultiStdenv wrapped-ropfuscator;

I have tried symphorien suggestion with clangMultiStdenv but still nothing.

In file included from test.c:1:
In file included from /nix/store/7rfaw11na5ajdgwr55ffzwfibbrdpk8z-glibc-2.33-56-dev/include/stdio.h:27:
In file included from /nix/store/7rfaw11na5ajdgwr55ffzwfibbrdpk8z-glibc-2.33-56-dev/include/bits/libc-header-start.h:33:
In file included from /nix/store/7rfaw11na5ajdgwr55ffzwfibbrdpk8z-glibc-2.33-56-dev/include/features.h:497:
/nix/store/7rfaw11na5ajdgwr55ffzwfibbrdpk8z-glibc-2.33-56-dev/include/gnu/stubs.h:7:11: fatal error: 'gnu/stubs-32.h' file not found
# include <gnu/stubs-32.h>
          ^~~~~~~~~~~~~~~~
1 error generated.

13:04:50
@pepe:matrix.giugl.ioErPepone(but actually now is the other way around 😓)13:05:12
@pepe:matrix.giugl.ioErPeponeWhat I've tried here is a cross-compiled compiler with a multiarch env13:05:42
@symphorien:xlumurb.eusymphorienDid you try adding glibc_multi to buildInputs ?13:06:42
@pepe:matrix.giugl.ioErPeponehttps://github.com/NixOS/nixpkgs/blob/master/pkgs/development/compilers/llvm/multi.nix#L24-L2513:07:31
@pepe:matrix.giugl.ioErPepone symphorien: trying now 13:07:54
@pepe:matrix.giugl.ioErPepone So, I have added glibc_multi in the shell buildInputs and we are going forward! 13:10:20
@pepe:matrix.giugl.ioErPepone
/nix/store/js66s0xwjnzg0ggi2lq9bcvlk6x2za13-binutils-2.35.2/bin/ld: skipping incompatible /nix/store/h6d0c7lnxx65i925xmnxgwdfdvc7hc2p-gcc-10.3.0/lib/gcc/x86_64-unknown-linux-gnu/10.3.0/libgcc.a when searching for -lgcc
/nix/store/js66s0xwjnzg0ggi2lq9bcvlk6x2za13-binutils-2.35.2/bin/ld: cannot find -lgcc
/nix/store/js66s0xwjnzg0ggi2lq9bcvlk6x2za13-binutils-2.35.2/bin/ld: skipping incompatible /nix/store/z56jcx3j1gfyk4sv7g8iaan0ssbdkhz1-glibc-2.33-56/lib/libgcc_s.so.1 when searching for libgcc_s.so.1
/nix/store/js66s0xwjnzg0ggi2lq9bcvlk6x2za13-binutils-2.35.2/bin/ld: skipping incompatible /nix/store/z56jcx3j1gfyk4sv7g8iaan0ssbdkhz1-glibc-2.33-56/lib/libgcc_s.so.1 when searching for libgcc_s.so.1
/nix/store/js66s0xwjnzg0ggi2lq9bcvlk6x2za13-binutils-2.35.2/bin/ld: cannot find libgcc_s.so.1
/nix/store/js66s0xwjnzg0ggi2lq9bcvlk6x2za13-binutils-2.35.2/bin/ld: skipping incompatible /nix/store/z56jcx3j1gfyk4sv7g8iaan0ssbdkhz1-glibc-2.33-56/lib/libgcc_s.so.1 when searching for libgcc_s.so.1
/nix/store/js66s0xwjnzg0ggi2lq9bcvlk6x2za13-binutils-2.35.2/bin/ld: skipping incompatible /nix/store/h6d0c7lnxx65i925xmnxgwdfdvc7hc2p-gcc-10.3.0/lib/gcc/x86_64-unknown-linux-gnu/10.3.0/libgcc.a when searching for -lgcc
/nix/store/js66s0xwjnzg0ggi2lq9bcvlk6x2za13-binutils-2.35.2/bin/ld: cannot find -lgcc
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
13:10:24
@pepe:matrix.giugl.ioErPepone

I am using this to create the shell:

pkgs.mkShell.override { stdenv = ropfuscator.stdenv; };
13:11:31
@pepe:matrix.giugl.ioErPepone (where the stdenv = pkgs.overrideCC pkgs.clangMultiStdenv wrapped-ropfuscator;) 13:11:59
@pepe:matrix.giugl.ioErPepone Tried to add gcc_multi to buildInputs, nothing 13:28:47
@nbathum:matrix.orgnbathum (he or they) joined the room.19:23:05
13 Dec 2021
@alexfmpe:matrix.orgalexfmpeis it possible to do static cross compilation? e.g. for embedded so I can just scp in an executable I have a raspberry pi 3 and am trying to build for it from my x86_64 but what comes out of pkgsCross.raspberry has the interpreter/dlibs all pointing to /nix/store etc I could probably install nix on my rpi but would like to know what the options are here15:10:57
@wucke13:matrix.orgwucke13 pkgsMusl.pkgsCross.aarch64-multiplatform-musl. ? 15:11:56

There are no newer messages yet.


Back to Room ListRoom Version: 6