Skip to content
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

support external weak symbol references #1917

Open
andrewrk opened this issue Feb 5, 2019 · 8 comments
Open

support external weak symbol references #1917

andrewrk opened this issue Feb 5, 2019 · 8 comments
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Feb 5, 2019

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:

extern weak hidden const size_t _DYNAMIC[];
...
		if (_DYNAMIC)

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) ?PointerType

PointerType could 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:

if (@externWeak("_DYNAMIC", [*]usize) != null)

The result value of the function is a runtime-time known value, however it is eligible to participate in simple global variable initialization expressions.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Feb 5, 2019
@andrewrk andrewrk added this to the 0.4.0 milestone Feb 5, 2019
@andrewrk andrewrk changed the title support external weak symbols support external weak symbol references Feb 5, 2019
@andrewrk andrewrk added the accepted This proposal is planned. label Feb 5, 2019
@emekoi
Copy link
Contributor

emekoi commented Feb 6, 2019

could we change the order of parameters to be @externWeak(comptime PointerType: type, comptime name: []const u8, ) ?PointerType to match other builtins?

@andrewrk
Copy link
Member Author

andrewrk commented Feb 6, 2019

You mean to match the casting builtins? This builtin is closer to a variable declaration than a cast, which is why I picked parameters that matched variable declaration syntax: var name: Type.

@andrewrk andrewrk modified the milestones: 0.4.0, 0.5.0 Feb 9, 2019
@andrewrk
Copy link
Member Author

The result value of the function is a runtime-time known value, however it is eligible to participate in simple global variable initialization expressions.

This is related to #2174

@LemonBoy
Copy link
Contributor

I have implemented this but it's a bit awkward to use: it actually declares a global symbol during the codegen phase but it's not exposed to the Zig side since it's not really a variable declaration. The biggest problem here is that I have no clue about what to do if we detect a pre-existing symbol (coming either from a normal declaration or a previous use of @externWeak) nor how to handle the re-definition case in general.

I can upload a draft PR if you want to have a look at the code.

@andrewrk
Copy link
Member Author

andrewrk commented May 3, 2019

@LemonBoy I would be interested in seeing that draft. I reviewed this proposal and it still makes sense to me.

I think conflicts with other external symbols can simply be compile errors.

I'd be happy to work with you on getting the feature to feel correct.

andrewrk added a commit that referenced this issue May 3, 2019
`@setGlobalLinkage` was removed with #462. The not-yet-implemented
proposal for external weak symbols is #1917.
@LemonBoy
Copy link
Contributor

LemonBoy commented May 5, 2019

Here's my branch.

@LemonBoy
Copy link
Contributor

LemonBoy commented May 6, 2019

I think the same problem applies to @export, if you're not careful enough you may end up stomping over some other symbol without any warning.

@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Aug 27, 2019
LemonBoy added a commit to LemonBoy/zig that referenced this issue Dec 22, 2019
LemonBoy added a commit to LemonBoy/zig that referenced this issue Dec 22, 2019
LemonBoy added a commit to LemonBoy/zig that referenced this issue Dec 22, 2019
LemonBoy added a commit to LemonBoy/zig that referenced this issue Dec 22, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 10, 2020
@andrewrk
Copy link
Member Author

Accepted Proposal

When implementing this, here is some work-in-progress code to refer to: #3971

@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 10, 2020
@andrewrk andrewrk removed this from the 0.8.0 milestone May 19, 2021
@andrewrk andrewrk added this to the 0.9.0 milestone May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 20, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jun 29, 2023
@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants