-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Proposal: Add linkname in addition to linksection #19999
Comments
Additional potential drive-by proposal by myself: Replace |
Could you make that a separate, easy to accept, proposal? That syntax often gets confused with C++'s |
sure! |
What does it boil down to on Windows? What will the linker see? EDIT: actually, I should probably expand the question to "what happen on each platform". Do we get two exports, one being an alias for the other? Or is the primary definition a local, while |
Basically what you do is that you have a Zig symbol name and a linker symbol name. Right now, these are the same unless |
Nice, ok, so there's no aliasing. In that case I am in favour of this change. |
I'm assuming this is similar to how Hare does linking? export @symbol("glfwInit") fn init() void; which would link the pub extern fn init() linkname("glfwInit") void; would do the same, I imagine? I feel like this syntax would be a lot easier to understand than the status-quo. |
Yep! Exactly! |
In AshetOS it would be very nice to have this feature: pub const syscalls = struct {
/// All syscalls related to generic resource management.
pub const resources = struct {
/// Returns the type of the system resource.
pub const get_type = @extern(*const fn (
SystemResource,
) SystemResource.Type, .{ .name = "ashet_syscalls_resources_get_type" });
/// Returns the current owner of this resource.
pub const get_owners = @extern(*const fn (
SystemResource,
owners_ptr: ?[*]Process,
owners_len: usize,
) usize, .{ .name = "ashet_syscalls_resources_get_owners" });
/// Adds the process to the owners of this resource, so the process
pub const send_to_process = @extern(*const fn (
SystemResource,
Process,
) void, .{ .name = "ashet_syscalls_resources_send_to_process" });
/// Drops the ownership of the resource for the current process.
pub const release = @extern(*const fn (
SystemResource,
) void, .{ .name = "ashet_syscalls_resources_release" });
};
}; It makes the code hard to read using |
the llvm backend in the compiler would also a prime benefactor from this pattern |
Right now, exported and imported symbols in Zig use verbatim the name that is given when using
export fn
/extern fn
or use a much more complex scheme when using@export
/@extern
.Problem
So if i want to export (or import) a function with another name than i want to have it in Zig, i have two options:
or
Both options feel clunky when i only want to change the name of the emitted symbol, so a C library will have a nice prefixed variant that doesn't clash names, and the Zig variant feels way more ziggy.
Proposal
Thus, i propose introduction of a new keyword named
linkname
which is valid whereeverlinksection
is valid which changes the way a function symbol is emitted internally:Use Case
Ashet OS
Ashet OS uses dynamic linking as the primary kernel interface, and i keep my symbols nicely namespaced and grouped:
which sadly leads to Zig code looking likes this:
which doesn't really read well, and renaming all symbols manually is tedious and error prone
C Library Wrappers
Using wrappers in Zig is common and having a simple wrapper that would export SDL functions without
SDL_
prefix would make the code much nicer to read:apigen
apigen will gain support for something like
global_prefix SDL_
which adds this prefix to all exported/imported symbols.This would allow making nice generated Zig "Headers" which do not expose this prefix on the API surface in Zig, making the usage and the reading/writing way more convenient.
The text was updated successfully, but these errors were encountered: