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

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

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.
  • 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.

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

Successfully merging this pull request may close these issues.

2 participants