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

newlib: add installation of libgloss for embedded targets #367275

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

knazarov
Copy link

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.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant