Skip to content

Support dynamically-linked FreeBSD libc when cross-compiling #23835

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

Merged
merged 20 commits into from
May 11, 2025

Conversation

alexrp
Copy link
Member

@alexrp alexrp commented May 8, 2025

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

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

Caveats:

  • This only works for 14+ (we still target 13 by default).
    • The startup ABI between crt1 and libc changed significantly in 14. I don't see much value in making it work for 13 since 15 will be released later this year, after which we'll bump our default target to 14.
  • We only track major releases in the abilists file, not minor/patch.
  • As with glibc and musl, we only ship the latest system and libc headers (14.2.0 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 #2876.

My next priority will be dynamically-linked libc for NetBSD (#2877), after which I'll look at static linking for both.

Release Notes

Zig now allows cross-compiling to FreeBSD 14+ 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 alexrp requested review from andrewrk and mikdusan May 8, 2025 19:48
@alexrp alexrp force-pushed the freebsd-libc branch 4 times, most recently from dac1fca to 40dfc11 Compare May 8, 2025 21:33
@andrewrk
Copy link
Member

andrewrk commented May 8, 2025

What is the delta installation size (both compressed and on disk)?

@alexrp
Copy link
Member Author

alexrp commented May 8, 2025

Uncompressed lib/libc is going from 104M to 118M, and xz -9-compressed tarball of lib/libc is going from 9.7M to 12M. Not sure about the full release tarball; don't have free CPU cores to build that right now. 😰

Speaking of size savings: #21258

@andrewrk
Copy link
Member

andrewrk commented May 8, 2025

hmmmm, +13% / +24% is pretty bad for only gaining 1 new target. 14 megabytes of headers seems excessive. The files added look reasonable though.

@alexrp
Copy link
Member Author

alexrp commented May 9, 2025

I think it looks worse than it actually is because we tend to take for granted the Linux situation of kernel and libc headers being separate. Some perspective:

  • lib/libc/freebsd lib/libc/include/*freebsd*: 13M
  • lib/libc/glibc lib/libc/include/*glibc* lib/libc/include/*gnu* lib/libc/include/*linux*: 16M
  • lib/libc/musl lib/libc/include/*musl* lib/libc/include/*linux*: 16M
  • lib/libc/mingw lib/libc/include/*windows*: 75M
  • lib/libc/darwin lib/libc/include/*macos*: 6.8M (though it's known that we're missing quite a few macOS headers)

In that light, I don't think it looks that bad actually.

That said, there's definitely room to prune FreeBSD headers; some are really kernel-only, and there are probably still some that aren't strictly related to the kernel or libc. But pruning these is going to be a very manual and tedious process.

only gaining 1 new target

Also, just to be clear, this is adding all FreeBSD 14 targets:

  • aarch64-freebsd-none
  • arm-freebsd-eabihf
  • powerpc-freebsd-eabihf
  • powerpc64-freebsd-none
  • powerpc64le-freebsd-none
  • riscv64-freebsd-none
  • x86-freebsd-none
  • x86_64-freebsd-none

@alexrp
Copy link
Member Author

alexrp commented May 9, 2025

zig-bootstrap works:

❯ file out/zig-x86_64-freebsd.14.0-none-baseline/zig
out/zig-x86_64-freebsd.14.0-none-baseline/zig: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld-elf.so.1, FreeBSD-style, stripped

@mikdusan also verified that the resulting binary actually runs on FreeBSD 14.

This should be ready for review.

@alexrp alexrp force-pushed the freebsd-libc branch 3 times, most recently from 6edf506 to 12465d3 Compare May 9, 2025 12:07
alexrp added 2 commits May 10, 2025 20:58
…raries.

Only works for FreeBSD 14+. Note that we still default to targeting FreeBSD 13.

Contributes to ziglang#2876.
The driver doesn't support it, and FreeBSD 13+ on PPC64 uses ELFv2 anyway.
@andrewrk
Copy link
Member

In that light, I don't think it looks that bad actually.

I agree, thanks for sharing this additional insight!

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

alexrp added 4 commits May 11, 2025 11:15
…ibc.so.

These symbols are defined in the statically-linked startup code. The real
libc.so.7 contains strong references to them, so they need to be put into the
dynamic symbol table.
We don't yet have a direct syscall layer in std.os.freebsd.
@alexrp alexrp enabled auto-merge May 11, 2025 18:43
@alexrp alexrp merged commit 833d4c9 into ziglang:master May 11, 2025
17 of 18 checks passed
@alexrp alexrp deleted the freebsd-libc branch May 11, 2025 23:27
@alexrp alexrp added the release notes This PR should be mentioned in the release notes. label May 12, 2025
@emaste
Copy link

emaste commented May 16, 2025

I removed some silly GCJ support code from crt1.

FWIW so did FreeBSD: freebsd/freebsd-src@a60615d

That said, there's definitely room to prune FreeBSD headers; some are really kernel-only, and there are probably still some that aren't strictly related to the kernel or libc.

I wonder if there's anything we can do in FreeBSD to make this less of a tedious, manual process.

@alexrp
Copy link
Member Author

alexrp commented May 16, 2025

FWIW so did FreeBSD: freebsd/freebsd-src@a60615d

Great!

I wonder if there's anything we can do in FreeBSD to make this less of a tedious, manual process.

Well, the tedious work is unavoidable; it's just a question of where it happens and who does it. 🙂

If you take a peek at our libc update instructions, you can see that we run a variation of this per-target:

MAKEOBJDIRPREFIX=$PWD/build tools/build/make.py installworld --bootstrap-toolchain -j$(nproc) TARGET=amd64 TARGET_ARCH=amd64 SRCCONF=$PWD/src.conf NO_SHARE=1 NO_LIBS=1 SUBDIR_OVERRIDE="include lib/libc lib/libdl lib/libelf lib/libexecinfo lib/librt lib/libthr lib/libstdthreads lib/libutil lib/msun libexec/rtld-elf" NO_ROOT=1 DESTDIR=$PWD/sysroot/amd64

Best as I can tell, the majority (if not all) of the "unwanted" headers come from the include directory there. Yet we can't really skip that directory because it does contain a huge amount of userspace headers that we do want.

Perhaps one solution here would be to move the installation logic for kernel-only headers into the sys directory, or something along those lines. Then we'd naturally skip them as we don't build that directory.

There are also some (I think...?) library headers that we manually remove:

rm -rf sysroot/*/usr/include/bsm
rm -rf sysroot/*/usr/include/gssapi
rm -rf sysroot/*/usr/include/teken

It seems a bit odd that these are installed from include rather than from the relevant library's makefile.

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.

3 participants