Skip to content

Support dynamically-linked NetBSD libc when cross-compiling #23913

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

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

alexrp
Copy link
Member

@alexrp alexrp commented May 16, 2025

libc update instructions: https://github.com/ziglang/zig/wiki/Updating-libc#netbsd

ABI tooling: https://github.com/ziglang/libc-abi-tools/tree/main/netbsd

Caveats:

  • This only works for 10.1+ (we still target 9.4 by default).
  • As with glibc and musl, we only ship the latest system and libc headers (10.1 specifically).
  • We don't support .init/.fini because it's 2025 and people should be using .init_array/.fini_array.
  • I removed some silly GCJ support code from crt1.

Contributes to #2877.

Release Notes

Zig now allows cross-compiling to NetBSD 10.1+ by providing stub libraries for dynamic libc, similar to how cross-compilation for glibc is handled. Additionally, all system and libc headers are provided.

@alexrp
Copy link
Member Author

alexrp commented May 16, 2025

@andrewrk FYI, the new NetBSD headers and sources add up to ~12M (uncompressed), so a little less than what we saw for FreeBSD in #23835.

@alexrp
Copy link
Member Author

alexrp commented May 16, 2025

I should note that libc++ currently fails to cross-compile for NetBSD even with this PR, meaning that I can't really make progress on zig-bootstrap. It's some obscure issue in locale code, and I don't feel particularly motivated to investigate that nightmare fuel right now.

@alexrp
Copy link
Member Author

alexrp commented May 17, 2025

It's some obscure issue in locale code, and I don't feel particularly motivated to investigate that nightmare fuel right now.

Ok well, I did, and apparently when the _LIBCPP_LOCALE__L_EXTENSIONS macro was removed, no NetBSD code path was added, so it ends up including just __locale_dir/locale_base_api/bsd_locale_fallbacks.h which it seems like it probably shouldn't...?

In file included from /home/alexrp/Source/ziglang/zig-bootstrap/out/host/lib/zig/libcxx/src/iostream.cpp:9:
    In file included from /home/alexrp/Source/ziglang/zig-bootstrap/out/host/lib/zig/libcxx/src/std_stream.h:14:
    In file included from /home/alexrp/Source/ziglang/zig-bootstrap/out/host/lib/zig/libcxx/include/__locale:14:
    In file included from /home/alexrp/Source/ziglang/zig-bootstrap/out/host/lib/zig/libcxx/include/__locale_dir/locale_base_api.h:138:
    /home/alexrp/Source/ziglang/zig-bootstrap/out/host/lib/zig/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h:32:72: error: no member named 'uselocale' in the global namespace
       32 |   _LIBCPP_HIDE_FROM_ABI __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {}
          |                                                                      ~~^
    /home/alexrp/Source/ziglang/zig-bootstrap/out/host/lib/zig/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h:36:9: error: no type named 'uselocale' in the global namespace
       36 |       ::uselocale(__old_loc_);
          |       ~~^

cc @brad0 I've seen you do some NetBSD work upstream in LLVM, maybe you'd be interested in this.

alexrp added 19 commits May 17, 2025 04:41
This didn't cause any problems in practice, but doing it this way is technically
more correct.
We want the latest unversioned inclusion that fits the target version. This
theoretically matters because it might have a different global vs weak linkage
compared to an older inclusion.
* mips64/mips64el on NetBSD are soft float; we have no support for this yet.
* powerpc64 does not appear to be a thing.
* riscv32/riscv64 have not seen official releases yet.
* powerpc64 does not appear to be a thing.
* riscv32/riscv64 have not had actual releases yet.
sys/param.h was manually adjusted to not define __NetBSD_Version__ since it will
be defined by the compiler.
* sysident_assym.h was manually expanded.
    * The ELF_NOTE_MARCH_DESC and ELF_NOTE_MARCH_DESCSZ macros will be defined
      by the compiler.
* Legacy .init/.fini stuff was removed.
* GCJ nonsense was removed.
Currently covers version 10.1.
We don't yet have a direct syscall layer in std.os.netbsd.
alexrp added 2 commits May 17, 2025 20:12
…aries.

Only works for NetBSD 10.1+. Note that we still default to targeting NetBSD 9.

Contributes to ziglang#2877.
@alexrp alexrp requested a review from andrewrk May 17, 2025 19:05
@alexrp
Copy link
Member Author

alexrp commented May 17, 2025

@mikdusan has verified that a cross-compiled program works on NetBSD. This should be ready for review.

As mentioned above, there's still more work to be done in libc++ for zig-bootstrap to work for NetBSD, however.

@alexrp alexrp added the release notes This PR should be mentioned in the release notes. label May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes This PR should be mentioned in the release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant