Skip to content

Reimplement crt0 code in Zig for vendored libcs #23875

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
alexrp opened this issue May 14, 2025 · 2 comments
Open

Reimplement crt0 code in Zig for vendored libcs #23875

alexrp opened this issue May 14, 2025 · 2 comments
Assignees
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. libc Issues related to libzigc and Zig's vendored libc code. os-freebsd os-linux os-netbsd os-wasi os-windows
Milestone

Comments

@alexrp
Copy link
Member

alexrp commented May 14, 2025

SLoC for crt0 code in some of the libcs we vendor:

  • FreeBSD libc: 825
  • MinGW-w64: 524
  • musl: 321
  • NetBSD libc: 467
  • wasi-libc: 56

(glibc is a bit harder to quantify because its organization is incredibly horrible, which results in us having a bunch of probably unnecessary code in lib/libc/glibc. I would be surprised if it's actually more than 2-3k lines of necessary crt0 code after pruning.)

This is really not a lot of code, so rewriting it in Zig would make a lot of sense, and would make progress towards #16270.

A secondary benefit would be finding any areas of the language that make writing this sort of code harder than it should be. In that regard, I can list these upfront:

@alexrp alexrp added this to the 0.16.0 milestone May 14, 2025
@alexrp alexrp added enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-windows os-linux os-freebsd os-netbsd os-wasi libc Issues related to libzigc and Zig's vendored libc code. labels May 14, 2025
@alexrp alexrp self-assigned this May 15, 2025
@rootbeer
Copy link
Contributor

Once re-written in Zig, how does the Zig crt0 code keep up with changes in the original libraries? Do Zig maintainers just need to look through the diffs in the source C library for changes that should get reflected in the Zig code? Seems fragile to me, but maybe the crt0 code doesn't change very much?

@alexrp
Copy link
Member Author

alexrp commented May 15, 2025

Once re-written in Zig, how does the Zig crt0 code keep up with changes in the original libraries? Do Zig maintainers just need to look through the diffs in the source C library for changes that should get reflected in the Zig code?

That's right.

Seems fragile to me, but maybe the crt0 code doesn't change very much?

I don't think so:

  • We can just add a step to the wiki page along the lines of "do a diff between the current and previous release's startup code".
  • You already have to do some manual checks anyway because update_glibc.zig, update_freebsd_libc.zig, etc only update existing files, not new files.
  • And yeah, the startup ABI does not change very often at all. For glibc, I think that's happened exactly once in all the versions we support targeting, as an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. libc Issues related to libzigc and Zig's vendored libc code. os-freebsd os-linux os-netbsd os-wasi os-windows
Projects
None yet
Development

No branches or pull requests

2 participants