From 3c547e6031725e4446e7752d5b0ab27efd3db4ef Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Mon, 13 Jan 2025 22:32:32 +0000 Subject: [PATCH] newlib: add installation of libgloss for embedded targets (#367275) I was trying to get an embedded RISC-V toolchain working, by following pretty much a documented workflow: ``` pkgsCross = import pkgs.path { localSystem = pkgs.stdenv.buildPlatform.system; crossSystem = { config = "riscv32-none-elf"; libc = "newlib-nano"; gcc.arch = "rv32im"; }; }; ``` This is supposed to work for compiling programs that target "bare-metal". But when I tried to compile my project, GCC complained that it can't fild `-lgloss`. If you're curious what libgloss is, in very simple terms it's a glue layer that allows you to provide implementation for the bare minimum functionality that would get the rest of the libc working. (such as `_sbrk`, `_open`, `_read` and friends). After digging into it for a while, I've figured out that newlib which is shipped by nixpkgs doesn't contain libgloss as part of the build resuts. So this isn't just me misconfiguring the search paths. You may be wondering - why didn't anyone else find this issue? My current guess is that nobody really uses this combination (newlib-nano plus a bare-metal deployment). Most people who use the cross toolchains likely target an operating system which provides syscalls already and don't implement the stubs themselves. If you really want to try and reproduce the bug, you need to pass this as a flag to gcc: `--specs=nano.specs`. Also, this bug is not really specific to NixOS, but happened in ArchLinux as well. Here's a relevant bug report: https://bugs.archlinux.org/task/66548. This is where I've found the fix. Adding the fix in the way I did seems to fix the problem for good. The fix itself doesn't seem to be dangerous because in case libgloss is absent, it would be skipped and not copied to the build results. NB: during review it's been also suggested to add libm for the same reasons, so this is what I did as well. --- pkgs/development/misc/newlib/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/development/misc/newlib/default.nix b/pkgs/development/misc/newlib/default.nix index 3794b631f8ce0..fe71113a6f7d1 100644 --- a/pkgs/development/misc/newlib/default.nix +++ b/pkgs/development/misc/newlib/default.nix @@ -115,7 +115,7 @@ stdenv.mkDerivation (finalAttrs: { ( cd $out${finalAttrs.passthru.libdir} - for f in librdimon.a libc.a libg.a; do + for f in librdimon.a libc.a libm.a libg.a libgloss.a; do # Some libraries are only available for specific architectures. # For example, librdimon.a is only available on ARM. [ -f "$f" ] && cp "$f" "''${f%%\.a}_nano.a"