Skip to content

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

Merged
alexrp merged 22 commits intoziglang:masterfrom
alexrp:netbsd-libc
May 20, 2025
Merged

Support dynamically-linked NetBSD libc when cross-compiling#23913
alexrp merged 22 commits intoziglang:masterfrom
alexrp:netbsd-libc

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 #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
@alexrp alexrp merged commit b27c5fb into ziglang:master May 20, 2025
16 of 18 checks passed
@alexrp alexrp deleted the netbsd-libc branch May 20, 2025 05:46
dasimmet pushed a commit to dasimmet/zig that referenced this pull request Aug 4, 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.

2 participants