Proposal: Link-time initialization of globals #9512
Labels
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
I believe there should be a concept of link-time initialization of global variables. There is for example a boot protocol called stivale2 that reads a structure straight from one of your ELF sections, and traverses a linked list of tags (pointers encoded as integers) in your memory before jumping to the entry point:
In C:
If you attempt to do the same thing in Zig, you're hit with something akin to "unable to evaluate constant expression" if you just have a single pointer-to-int conversion within any of your globals.
I propose that instead the link time pointers are tagged as exactly that, "link time known", so that you actually trigger the error only if you access them within a
comptime
context.The workaround for now is to declare these structs in C and linking the C file to your executable, or implementing the creation of these structs in the linker script, none of which have access to zig comptime values, or to get access to zig constants through inline assembly:
The text was updated successfully, but these errors were encountered: