-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
support external weak symbol references #1917
Copy link
Copy link
Closed
Labels
acceptedThis proposal is planned.This proposal is planned.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.This issue suggests language modifications. If it also has the "accepted" label then it is planned.
Milestone
Metadata
Metadata
Assignees
Labels
acceptedThis proposal is planned.This proposal is planned.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.This issue suggests language modifications. If it also has the "accepted" label then it is planned.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Accepted Proposal
We need a way to declare an external symbol, but one that should be NULL rather than a linker error if the symbol is never supplied.
One use case is to detect if an executable is dynamically linked, like this: https://git.musl-libc.org/cgit/musl/tree/src/env/__init_tls.c#n89:
When an external weak symbol is not linked, there actually is no address for it. So representing it as an optional type is incorrect. What we need is an optional pointer to the symbol. For that I propose a builtin function:
@externWeak(comptime name: []const u8, comptime PointerType: type) ?PointerTypePointerTypecould be any pointer type. The reason we supply the pointer type and not the element type is so that the default alignment can optionally be overridden by using a pointer type with an explicit alignment.With this proposal the C code above could be represented in Zig like this:
The result value of the function is a runtime-time known value, however it is eligible to participate in simple global variable initialization expressions.