Repository navigation
Open
Labels
enhancementSolving this issue will likely involve adding new logic or components to the codebase.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.
Milestone
Description
In certain specialized scenarios such as #20382, it's important to be able to designate constructor/destructor (henceforth init/fini) functions. Init functions allow running initialization code before main() in the case of a program, or when a shared library is loaded and thus before any exported functions are invoked. Conversely, fini functions allow running cleanup after main() returns in the case of a program, or when a shared library is unloaded.
To make this functionality available in Zig, I propose the following:
@appendInitFiniArray(comptime ptr: *const anyopaque, comptime options: std.builtin.InitFiniOptions) void;
// std.builtin
const InitFiniOptions = struct {
array: enum { init, fini },
priority: i32 = std.math.maxInt(u16),
};This builtin function appends ptr to the designated array (.init_array/.fini_array) or the equivalent for the object format.
ptrmust be comptime-known and resolve to a function with the signaturefn () callconv(.c) void.prioritycan be anyi32, but it should be documented that priorities in the range0through100tend to be used for low-level libraries like libc. That's why C compilers defaultpriorityto65535, which seems sensible for us to do, too.- It is valid to append the same
ptrmultiple times, even with the samepriorityvalue, though obviously not particularly sensible.- I see no point in attempting to prevent this in Sema as there are ways to circumvent it at link time anyway, and doing so is perfectly valid at the object format level.
- The language reference should strongly discourage using this builtin function as a general-purpose initialization/cleanup mechanism, since doing so does not lead to particularly idiomatic Zig code. Basically, if you don't know whether you need
@appendInitFiniArray(), you don't.- That's also part of why I chose this name, rather than the relatively innocent-sounding
__attribute__((constructor))/__attribute__((destructor))in GCC/Clang.
- That's also part of why I chose this name, rather than the relatively innocent-sounding
Example usage:
comptime {
@appendInitFiniArray(&ctor, .{ .array = .init });
@appendInitFiniArray(&dtor, .{ .array = .fini });
}
fn ctor() callconv(.c) void {
// ...
}
fn dtor() callconv(.c) void {
// ...
}Reacted by Nico Elbers, PierreLgol, Mehrad, February, cn and Trevor-Strong
Metadata
Metadata
Assignees
Labels
enhancementSolving this issue will likely involve adding new logic or components to the codebase.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.
Activity
alexrp commentedon Apr 15, 2025
I don't know that I would categorize it as "for C compatibility". It's an object format feature that happens to be used by C/C++, but use cases for it don't necessarily have anything to do with C. We already have partial support for it in libc-less Zig due to fuzzing, for example:
zig/lib/std/start.zig
Lines 571 to 583 in 76da028
Note that the
callconv(.c)requirement isn't there because we necessarily specifically need the C calling convention; rather, it's just that there are somecallconv(.auto)features that don't make sense in this context.alexrp commentedon May 10, 2025
TIL that compiler-rt is normally in charge of calling
__cxa_finalize()by way of.fini_array:We... just don't do that, which means that
zig c++is violating the Itanium C++ ABI.rootbeer commentedon May 11, 2025
I think the missing
__cxa_finalizewould also explain the root of the bug in #17908 (comment)?alexrp commentedon May 11, 2025
Sounds likely.
alexrp commentedon May 14, 2025
Just reproducing some discussion from Zulip here:
@jacobly0 rightly pointed out that it's possible to implement this builtin in userland with some helper functions in the standard library to abstract over the different object formats.
However, I think a weakness of that approach is that, when using the C backend,
builtin.ofmt == .c, so you can't directly figure out what object format you're ultimately compiling to. OTOH, with this proposal, the C backend would just lower@appendInitFiniArrayto__attribute__((constructor))or__attribute__((destructor)).That said, I could see an argument that we should just not have
ObjectFormat.cand instead rely onbuiltin.zig_backend == .stage2_cfor detecting the C backend in Zig code. We'd then require that the C code we generate actually gets compiled to the object format that was specified (or more likely picked by default) when invoking the Zig compiler. This would require a new command line flag to actually pick the C backend.