Trying to build a Zephyr RTOS project for their native_posix target errors with incompatible libasan.so

I’m trying to get a Nix development shell that can be used for building a pre-existing Zephyr RTOS project. I found this which is part of a demo of using languages other than C with Zephyr. I’ve been attempting to use that as a base to build our project. It seems to work on the actual ARM Cortex-M target, but I can’t seem to get it working for the native_posix target with address sanitizer enabled that we use for our unit tests.

When trying to link, ld can’t find a compatible libasan:

Build output (mildly censored, replaced company name, product name, etc just in case NDA would be an issue)

❯ west build -p -b native_posix COMPANY/apps/hw_test/ -- -DEXTRA_CFLAGS=-I/nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/include -DEXTRA_LDFLAGS=-L/nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/lib

-- west build: making build dir /home/USER/PROJECT/workspace/build pristine
-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /home/USER/PROJECT/workspace/COMPANY/apps/hw_test
-- CMake version: 3.25.3
-- Found Python3: /nix/store/kwfm05rnidaaz5gxxviwv7xlpzgwnf2m-python3-3.10.11-env/bin/python3.10 (found suitable exact version "3.10.11") found components: Interpreter 
-- Cache files will be written to: /home/USER/.cache//zephyr
-- Zephyr version: 3.3.0 (/home/USER/PROJECT/workspace/zephyr)
-- Found west (found suitable version "1.0.0", minimum required is "0.7.1")
-- Board: native_posix
-- Found host-tools: zephyr 0.15.0 (/nix/store/12hn0v46zmrc0p9s3qdqkly3niv45p7g-zephyr-sdk)
-- Found toolchain: host (gcc/ld)
-- Found Dtc: /nix/store/12hn0v46zmrc0p9s3qdqkly3niv45p7g-zephyr-sdk/sysroots/x86_64-pokysdk-linux/usr/bin/dtc (found suitable version "1.6.0", minimum required is "1.4.6") 
-- Found BOARD.dts: /home/USER/PROJECT/workspace/zephyr/boards/posix/native_posix/native_posix.dts
-- Found devicetree overlay: /home/USER/PROJECT/workspace/COMPANY/apps/hw_test/boards/native_posix.overlay
-- Generated zephyr.dts: /home/USER/PROJECT/workspace/build/zephyr/zephyr.dts
-- Generated devicetree_generated.h: /home/USER/PROJECT/workspace/build/zephyr/include/generated/devicetree_generated.h
-- Including generated dts.cmake file: /home/USER/PROJECT/workspace/build/zephyr/dts.cmake
Parsing /home/USER/PROJECT/workspace/zephyr/Kconfig
Loaded configuration '/home/USER/PROJECT/workspace/zephyr/boards/posix/native_posix/native_posix_defconfig'
Merged configuration '/home/USER/PROJECT/workspace/COMPANY/apps/hw_test/prj.conf'
Merged configuration '/home/USER/PROJECT/workspace/COMPANY/apps/hw_test/boards/native_posix.conf'
Configuration saved to '/home/USER/PROJECT/workspace/build/zephyr/.config'
Kconfig header saved to '/home/USER/PROJECT/workspace/build/zephyr/include/generated/autoconf.h'
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /nix/store/mbivnikl2i042h8f5b4vn9sd5zin4y90-gnat-wrapper-12.2.0/bin/gcc
CMake Warning at /home/USER/PROJECT/workspace/zephyr/CMakeLists.txt:137 (message):
  The environment variable 'CFLAGS' was set to
  -I/nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/include,
  but Zephyr ignores flags from the environment.  Use 'cmake
  -DEXTRA_CFLAGS=-I/nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/include'
  instead.


CMake Warning at /home/USER/PROJECT/workspace/zephyr/CMakeLists.txt:137 (message):
  The environment variable 'LDFLAGS' was set to
  -L/nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/lib,
  but Zephyr ignores flags from the environment.  Use 'cmake
  -DEXTRA_LDFLAGS=-L/nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/lib'
  instead.


-- Configuring done
-- Generating done
-- Build files have been written to: /home/USER/PROJECT/workspace/build
-- west build: building application
[1/172] Preparing syscall dependency handling

[2/172] Generating include/generated/version.h
-- Zephyr version: 3.3.0 (/home/USER/PROJECT/workspace/zephyr), build: zephyr-v3.3.0

[168/172] Linking C executable zephyr/zephyr_pre0.elf
FAILED: zephyr/zephyr_pre0.elf zephyr/zephyr_pre0.map /home/USER/PROJECT/workspace/build/zephyr/zephyr_pre0.map 
: && ccache /nix/store/mbivnikl2i042h8f5b4vn9sd5zin4y90-gnat-wrapper-12.2.0/bin/gcc -I/nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/include -L/nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/lib   -gdwarf-4 zephyr/CMakeFiles/zephyr_pre0.dir/misc/empty_file.c.obj -o zephyr/zephyr_pre0.elf  zephyr/CMakeFiles/offsets.dir/./arch/posix/core/offsets/offsets.c.obj  -Wl,-T  zephyr/linker_zephyr_pre0.cmd  -Wl,-Map=/home/USER/PROJECT/workspace/build/zephyr/zephyr_pre0.map  -Wl,--whole-archive  app/libapp.a  zephyr/libzephyr.a  zephyr/arch/arch/posix/core/libarch__posix__core.a  zephyr/soc/posix/inf_clock/libsoc__posix__inf_clock.a  zephyr/boards/posix/native_posix/libboards__posix__native_posix.a  zephyr/subsys/emul/libsubsys__emul.a  zephyr/drivers/adc/libdrivers__adc.a  zephyr/drivers/console/libdrivers__console.a  zephyr/drivers/gpio/libdrivers__gpio.a  zephyr/drivers/i2c/libdrivers__i2c.a  zephyr/drivers/sensor/libdrivers__sensor.a  zephyr/drivers/can/libdrivers__can.a  zephyr/drivers/can/transceiver/libdrivers__can__transceiver.a  zephyr/drivers/regulator/libdrivers__regulator.a  zephyr/drivers/flash/libdrivers__flash.a  zephyr/drivers/serial/libdrivers__serial.a  zephyr/drivers/timer/libdrivers__timer.a  modules/modules/lib/PRODUCT_vehicle_intf/lib..__COMPANY__modules__lib__PRODUCT_vehicle_intf.a  modules/modules/drivers/sensor/isl28022/lib..__COMPANY__modules__drivers__sensor__isl28022.a  modules/modules/drivers/sensor/icm42607/lib..__COMPANY__modules__drivers__sensor__icm42607.a  -Wl,--no-whole-archive  zephyr/kernel/libkernel.a  -L/home/USER/PROJECT/workspace/build/zephyr  -no-pie  -Wl,--gc-sections  -Wl,--build-id=none  -Wl,--sort-common=descending  -Wl,--sort-section=alignment  -Wl,-u,_OffsetAbsSyms  -Wl,-u,_ConfigAbsSyms  -L/nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/lib  -m32  -ldl  -pthread  -fsanitize=address,undefined  -lm && cd /home/USER/PROJECT/workspace/build/zephyr && /nix/store/fic9avx8yabwb3ib8m49ld3kq9fkwwwd-cmake-3.25.3/bin/cmake -E echo
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: skipping incompatible /nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/lib/libasan.so when searching for -lasan
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: skipping incompatible /nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/lib/libasan.so when searching for -lasan
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: skipping incompatible /nix/store/20j7fvm1xl8c9b0z3rw20r4ziyg3y4k2-gnat-12.2.0-lib/lib/libasan.so when searching for -lasan
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: skipping incompatible /nix/store/20j7fvm1xl8c9b0z3rw20r4ziyg3y4k2-gnat-12.2.0-lib/lib/libasan.so when searching for -lasan
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: skipping incompatible /nix/store/f0mv1wbxar5sq6airkrlrc3d2r7xm51l-gnat-12.2.0/lib/gcc/x86_64-unknown-linux-gnu/12.2.0/../../../../lib/libasan.a when searching for -lasan
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: skipping incompatible /nix/store/f0mv1wbxar5sq6airkrlrc3d2r7xm51l-gnat-12.2.0/lib/gcc/x86_64-unknown-linux-gnu/12.2.0/../../../libasan.a when searching for -lasan
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: cannot find -lasan: file in wrong format
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: skipping incompatible /nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/lib/libasan.so when searching for -lasan
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: skipping incompatible /nix/store/fl19mivgh8cw7a8dm8i00y1si17na4dc-native-dependencies-env/lib/libasan.so when searching for -lasan
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: skipping incompatible /nix/store/20j7fvm1xl8c9b0z3rw20r4ziyg3y4k2-gnat-12.2.0-lib/lib/libasan.so when searching for -lasan
/nix/store/22p5nv7fbxhm06mfkwwnibv1nsz06x4b-binutils-2.40/bin/ld: skipping incompatible /nix/store/20j7fvm1xl8c9b0z3rw20r4ziyg3y4k2-gnat-12.2.0-lib/lib/libasan.so when searching for -lasan
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /nix/store/fic9avx8yabwb3ib8m49ld3kq9fkwwwd-cmake-3.25.3/bin/cmake --build /home/USER/PROJECT/workspace/build

Shell.nix:

# Basic configuration for Zephyr development.
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/refs/tags/23.05.tar.gz") { } }:
let
  pp = pkgs.python3.pkgs;
  imgtool = pp.buildPythonPackage rec {
    version = "1.10.0";
    pname = "imgtool";

    src = pp.fetchPypi {
      inherit pname version;
      sha256 = "sha256-A7NOdZNKw9lufEK2vK8Rzq9PRT98bybBfXJr0YMQS0A=";
    };

    propagatedBuildInputs = with pp; [
      cbor2
      click
      intelhex
      cryptography
    ];
    doCheck = false;
    pythonImportsCheck = [
      "imgtool"
    ];
  };

  python-packages = pkgs.python3.withPackages (p: with p; [
    autopep8
    pyelftools
    pyyaml
    pykwalify
    canopen
    packaging
    progress
    psutil
    anytree
    intelhex
    west
    imgtool

    cryptography
    intelhex
    click
    cbor2

    # For mcuboot CI
    toml

    # For twister
    tabulate
    ply

    # For TFM
    pyasn1
    graphviz
    jinja2

    requests
    beautifulsoup4

    # These are here because pip stupidly keeps trying to install
    # these in /nix/store.
    wcwidth
    sortedcontainers
  ]);

  # Build the Zephyr SDK as a nix package.
  new-zephyr-sdk-pkg =
    { stdenv
    , fetchurl
    , which
    , python38
    , wget
    , file
    , cmake
    , libusb
    , autoPatchelfHook
    }:
    let
      version = "0.15.0";
      arch = "arm";
      sdk = fetchurl {
        url = "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${version}/zephyr-sdk-${version}_linux-x86_64_minimal.tar.gz";
        hash = "sha256-dn+7HVBtvDs2EyXSLMb12Q+Q26+x6HYyPP69QdLKka8=";
      };
      armToolchain = fetchurl {
        url = "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${version}/toolchain_linux-x86_64_arm-zephyr-eabi.tar.gz";
        hash = "sha256-B7YIZEyuqE+XNI7IWnN6WiC1k9UdFEt4YN4Yr7Vn3Po=";
      };
      x86_64Toolchain = fetchurl {
        url = "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${version}/toolchain_linux-x86_64_x86_64-zephyr-elf.tar.gz";
        hash = "sha256-9PoILowiS8wgfB/vdrpJUostIMyS62jjd21nRzWBQ/k=";
      };
    in
    stdenv.mkDerivation {
      name = "zephyr-sdk";
      inherit version;
      srcs = [ sdk armToolchain x86_64Toolchain ];
      srcRoot = ".";
      nativeBuildInputs = [
        which
        wget
        file
        python38
        autoPatchelfHook
        cmake
        libusb
      ];
      phases = [ "installPhase" "fixupPhase" ];
      installPhase = ''
        runHook preInstall
        echo out=$out
        mkdir -p $out
        set $srcs
        tar -xf $1 -C $out --strip-components=1
        tar -xf $2 -C $out
        tar -xf $3 -C $out
        (cd $out; bash ./setup.sh -h)
        rm $out/zephyr-sdk-x86_64-hosttools-standalone-0.9.sh
        runHook postInstall
      '';
    };
  zephyr-sdk = pkgs.callPackage new-zephyr-sdk-pkg { };

  packages = with pkgs; [
    # Tools for building the languages we are using
    llvmPackages_16.clang-unwrapped # Newer than base clang
    gnat
    zig
    zls
    rustup
    glibc_multi

    # Dependencies of the Zephyr build system.
    (python-packages)
    cmake
    ninja
    gperf
    python3
    ccache
    dtc
    gmp.dev

    zephyr-sdk
  ];
in
pkgs.mkShell {
  nativeBuildInputs = packages;

  # For Zephyr work, we need to initialize some environment variables,
  # and then invoke the zephyr setup script.
  shellHook = ''
    export ZEPHYR_SDK_INSTALL_DIR=${zephyr-sdk:?}
    export PATH=$PATH:${zephyr-sdk:?}/arm-zephyr-eabi/bin
    export VIA_WORKSPACE_PATH="$(realpath ./workspace/)"
    source ./workspace/zephyr/zephyr-env.sh
  '';
}

I’d appreciate any help getting this to work. Address Sanitizer is a very helpful tool for C development!

Needed a multiarch GCC, which gnat isn’t (of course, it’s GCC Native!).

# Basic configuration for Zephyr development.
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/refs/tags/23.05.tar.gz") { } }:
let
  pp = pkgs.python3.pkgs;
  imgtool = pp.buildPythonPackage rec {
    version = "1.10.0";
    pname = "imgtool";

    src = pp.fetchPypi {
      inherit pname version;
      sha256 = "sha256-A7NOdZNKw9lufEK2vK8Rzq9PRT98bybBfXJr0YMQS0A=";
    };

    propagatedBuildInputs = with pp; [
      cbor2
      click
      intelhex
      cryptography
    ];
    doCheck = false;
    pythonImportsCheck = [
      "imgtool"
    ];
  };

  python-packages = pkgs.python3.withPackages (p: with p; [
    autopep8
    pyelftools
    pyyaml
    pykwalify
    canopen
    packaging
    progress
    psutil
    anytree
    intelhex
    west
    imgtool

    cryptography
    intelhex
    click
    cbor2

    # For mcuboot CI
    toml

    # For twister
    tabulate
    ply

    # For TFM
    pyasn1
    graphviz
    jinja2

    requests
    beautifulsoup4

    # These are here because pip stupidly keeps trying to install
    # these in /nix/store.
    wcwidth
    sortedcontainers
  ]);

  # Build the Zephyr SDK as a nix package.
  new-zephyr-sdk-pkg =
    { stdenv
    , fetchurl
    , which
    , python38
    , wget
    , file
    , cmake
    , libusb
    , autoPatchelfHook
    }:
    let
      version = "0.15.0";
      arch = "arm";
      sdk = fetchurl {
        url = "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${version}/zephyr-sdk-${version}_linux-x86_64_minimal.tar.gz";
        hash = "sha256-dn+7HVBtvDs2EyXSLMb12Q+Q26+x6HYyPP69QdLKka8=";
      };
      armToolchain = fetchurl {
        url = "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${version}/toolchain_linux-x86_64_arm-zephyr-eabi.tar.gz";
        hash = "sha256-B7YIZEyuqE+XNI7IWnN6WiC1k9UdFEt4YN4Yr7Vn3Po=";
      };
      x86_64Toolchain = fetchurl {
        url = "https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v${version}/toolchain_linux-x86_64_x86_64-zephyr-elf.tar.gz";
        hash = "sha256-9PoILowiS8wgfB/vdrpJUostIMyS62jjd21nRzWBQ/k=";
      };
    in
    stdenv.mkDerivation {
      name = "zephyr-sdk";
      inherit version;
      srcs = [ sdk armToolchain x86_64Toolchain ];
      srcRoot = ".";
      nativeBuildInputs = [
        which
        wget
        file
        python38
        autoPatchelfHook
        cmake
        libusb
      ];
      phases = [ "installPhase" "fixupPhase" ];
      installPhase = ''
        runHook preInstall
        echo out=$out
        mkdir -p $out
        set $srcs
        tar -xf $1 -C $out --strip-components=1
        tar -xf $2 -C $out
        tar -xf $3 -C $out
        (cd $out; bash ./setup.sh -h)
        rm $out/zephyr-sdk-x86_64-hosttools-standalone-0.9.sh
        runHook postInstall
      '';
    };
  zephyr-sdk = pkgs.callPackage new-zephyr-sdk-pkg { };

  packages = with pkgs; [
    # Tools for building the languages we are using
    llvmPackages_16.clang-unwrapped # Newer than base clang, includes clang-format options Zephyr uses
    gcc_multi
    glibc_multi

    # Dependencies of the Zephyr build system.
    (python-packages)
    cmake
    ninja
    gperf
    python3
    ccache
    dtc
    gmp.dev

    zephyr-sdk
  ];
in
pkgs.mkShell {
  nativeBuildInputs = [ packages ];

  # For Zephyr work, we need to initialize some environment variables,
  # and then invoke the zephyr setup script.
  shellHook = ''
    export ZEPHYR_SDK_INSTALL_DIR=${zephyr-sdk}
    export PATH=$PATH:${zephyr-sdk}/arm-zephyr-eabi/bin
    unset CFLAGS
    unset LDFLAGS
    source ./workspace/zephyr/zephyr-env.sh
  '';
}

works.