Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static compiling rust for aarch64-unknown-linux-musl results in undefined reference to `getauxval' #264687

Closed
n8henrie opened this issue Oct 31, 2023 · 6 comments
Labels
0.kind: bug Something is broken 6.topic: musl Running or building packages with musl libc 6.topic: static

Comments

@n8henrie
Copy link
Contributor

Describe the bug

I'm trying to cross-compile a static build of nix-index from an x86_64-linux host for aarch64-unknown-linux-musl, but it fails with:

  cpu_model.c:(.text.startup.init_cpu_features+0x20): undefined reference to `getauxval'
       >           /nix/store/qmasr7xm8lp9kfjz3z8jsz8xqqnaya6j-aarch64-unknown-linux-musl-binutils-2.40/bin/aarch64-unknown-linux-musl-ld: cpu_model.c:(.text.startup.init_cpu_features+0x2c): undefined reference to `getauxval'
       >           collect2: error: ld returned 1 exit status

Steps To Reproduce

Steps to reproduce the behavior:

  1. Clone the nix-index repo
  2. cargo update to generate Cargo.lock
  3. I've tried to simplify the flake to the one below to minimize confounders:
{
  description = "A files database for nixpkgs";

  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";

  outputs = {
    self,
    nixpkgs,
  }: let
    inherit (nixpkgs) lib;
    system = "x86_64-linux";
    pkgs = import nixpkgs {
      inherit system;
      config.allowUnsupportedSystem = true;
      crossSystem = {
        system = "aarch64-unknown-linux-musl";
        rustc.config = "aarch64-unknown-linux-musl";
        isStatic = true;
      };
    };
  in {
    packages.${system}.default = pkgs.rustPlatform.buildRustPackage {
      pname = "nix-index";
      inherit ((lib.importTOML ./Cargo.toml).package) version;

      src = lib.sourceByRegex self [
        "(examples|src)(/.*)?"
        ''Cargo\.(toml|lock)''
        ''command-not-found\.sh''
      ];

      cargoLock.lockFile = ./Cargo.lock;

      nativeBuildInputs = [pkgs.pkg-config];
      buildInputs = with pkgs;
        [
          openssl
          openssl.dev
          curl
          sqlite
        ]
        ++ lib.optionals stdenv.isDarwin [darwin.apple_sdk.frameworks.Security];

      # NIX_CFLAGS_COMPILE = "-mno-outline-atomics";
      # RUSTFLAGS = "-C -mno-outline-atomics";

      meta = with lib; {
        description = "A files database for nixpkgs";
        homepage = "https://github.com/nix-community/nix-index";
        license = with licenses; [bsd3];
        maintainers = [maintainers.bennofs];
      };
    };
  };
}

Expected behavior

I'm hoping for a successful cross compilation that looks like a static build when I file the output.

Additional context

I have been able to cross-compile a hello world (example), so I'm not sure if this is a problem with a dependency? I've tried scattering pkgsStatic and pkgsMusl around various places like buildInputs without a solution.

There is an existing rustup issue that recommends using CFLAGS=-mno-outline-atomics: rust-lang/rustup#3324

In the flake above, I've commented out my attempts to add this to both NIX_CFLAGS_COMPILE, which gives the same error, as well as RUSTFLAGS = "-C -mno-outline-atomics";, which leads to > error: unknown codegen option: -mno-outline-atomics

Notify maintainers

teams.rust.members:
@figsoda
@Mic92
@tjni
@Winter
@zowoq

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.1.59-1-lts, Arch Linux, noversion, rolling`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.1`
 - channels(root): `""`
 - channels(n8henrie): `"nixpkgs-22.11"`
 - nixpkgs: `/home/n8henrie/.nix-defexpr/channels/nixpkgs`
@n8henrie n8henrie added the 0.kind: bug Something is broken label Oct 31, 2023
@n8henrie
Copy link
Contributor Author

Perhaps also related: rust-lang/rust#89626

@n8henrie
Copy link
Contributor Author

n8henrie commented Nov 25, 2023

I made an example repo to hopefully make it easier to reproduce: https://github.com/n8henrie/nixpkgs-issue-264687

It seems that depending on / using hyper_proxy is sufficient to reproduce the issue (I presume because of its downstream use of openssl -> openssl-sys).

$ nix build --builders '' github:n8henrie/nixpkgs-issue-264687
error: builder for '/nix/store/mzbkyvwj35n5wwv6bcw1b0h631ixff5g-nixpkgs-issue-264687-example-aarch64-unknown-linux-musl.drv' failed with exit code 101;
       last 10 log lines:
       >           /nix/store/flrx5swv7a6g4lxb2004g5pfwg933nzi-aarch64-unknown-linux-musl-binutils-2.40/bin/aarch64-unknown-linux-musl-ld: /nix/store/imfslavps8w7la48kiyywnq08c56zyr0-aarch64-unknown-linux-musl-rustc-1.73.0/lib/rustlib/aarch64-unknown-linux-musl/lib/libcompiler_builtins-e29f93580d4466a0.rlib(d24dc0111d2d0654-cpu_model.o): in function `init_cpu_features':
       >           cpu_model.c:(.text.startup.init_cpu_features+0x24): undefined reference to `getauxval'
       >           /nix/store/flrx5swv7a6g4lxb2004g5pfwg933nzi-aarch64-unknown-linux-musl-binutils-2.40/bin/aarch64-unknown-linux-musl-ld: cpu_model.c:(.text.startup.init_cpu_features+0x30): undefined reference to `getauxval'
       >           collect2: error: ld returned 1 exit status
       >           
       >   = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
       >   = note: use the `-l` flag to specify native libraries to link
       >   = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)
       >
       > error: could not compile `nixpkgs-issue-264687-example` (bin "nixpkgs-issue-264687-example") due to previous error

@n8henrie
Copy link
Contributor Author

For future readers that are just hoping to make a statically linked aarch64-linux rust-based executable that depends on openssl, after a bunch of trial and error I got the following flake to successfully build; the keys were:

  • don't include isStatic
  • do include RUSTFLAGS = ["-Ctarget-feature=+crt-static"];
  • do include glibc.static and pkgsStatic.openssl in your buildInputs
    • I found the asymmetry here very confusing -- why not pkgsStatic.glibc or openssl.static?
{
  description = "https://github.com/NixOS/nixpkgs/issues/264687";
  inputs.nixpkgs.url = "github:nixos/nixpkgs/59e7944d2a7d49264525dd6d9a91a3d86b0f0526";
  outputs = {
    self,
    nixpkgs,
  }: let
    pkgs = import nixpkgs {
      system = "x86_64-linux";
      crossSystem = {
        config = "aarch64-linux";
        rustc.config = "aarch64-unknown-linux-gnu";
      };
    };
  in {
    packages.x86_64-linux.default = pkgs.rustPlatform.buildRustPackage {
      name = "nixpkgs-issue-264687-example";
      version = "0.0";
      src = ./.;
      cargoLock.lockFile = ./Cargo.lock;
      RUSTFLAGS = ["-Ctarget-feature=+crt-static"];
      nativeBuildInputs = [pkgs.pkg-config];
      buildInputs = with pkgs; [
        glibc.static
        pkgsStatic.openssl
      ];
    };
  };
}

Aside from the above, the only other change required to get nix-index to statically compile for aarch64-linux was to depend on pkgsStatic.sqlite instead of sqlite.

However, this is all for aarch64-unknown-linux-gnu; I would still like to be able to compile for musl as well if possible, so I'll leave this issue open for the time being.

@alyssais
Copy link
Member

@yu-re-ka is this a duplicate of #254454?

@yu-re-ka
Copy link
Contributor

Yes, same issue

@wolfgangwalther
Copy link
Contributor

#278798 says it fixes this and was merged.

@tomodachi94 tomodachi94 added 6.topic: musl Running or building packages with musl libc 6.topic: static labels Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: bug Something is broken 6.topic: musl Running or building packages with musl libc 6.topic: static
Projects
None yet
Development

No branches or pull requests

5 participants