Skip to content

Proposal: Allow @export to take an arbitrary pointer as its first argument #24076

@alexrp

Description

@alexrp

Proposal

Allow e.g. this:

comptime {
    @export(@as(*void, @ptrFromInt(0x2C)), .{ .name = "__tls_array" });
}

Motivated by this std code:

comptime {
if (builtin.cpu.arch == .x86 and !builtin.abi.isGnu() and builtin.zig_backend != .stage2_c) {
// The __tls_array is the offset of the ThreadLocalStoragePointer field
// in the TEB block whose base address held in the %fs segment.
asm (
\\ .global __tls_array
\\ __tls_array = 0x2C
);
}
}

Presumably it'll also be useful in certain embedded and OS scenarios.

Implementation

  • Lowering is straightforward for self-hosted backends.
  • Lowering in the LLVM backend will probably emit global assembly that's roughly equivalent to the above std code.
  • Lowering in the C backend is an open question. For GCC-compatible compilers, we can obviously just emit global assembly like the LLVM backend, but there doesn't seem to be anything usable for MSVC.
    • Fortunately the motivating use case is not relevant here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.enhancementSolving this issue will likely involve adding new logic or components to the codebase.proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions