Skip to content

Remove extra indirection from statics with linkage? #156468

Open

Description

@RalfJung

I just learned from @bjorn3 that the way we represent statics with linkage, such as #[linkage = "extern_weak"], is kind of strange:

unsafe extern "C" {
    #[linkage = "extern_weak"]
    static test_symbol_that_does_not_exist: Option<unsafe extern "C" fn()>;
}

Here, test_symbol_that_does_not_exist will actually be a global variable local to this module that points to the weak symbol.

Now that we have the &raw address-of operator, we could avoid this extra indirection. Whether we should, I do not know.
Cc @bjorn3 @nikic @chorman0773

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on May 11, 2026
  2. added
    A-linkageArea: linking into static, shared libraries and binaries
    F-linkage#![feature(linkage)]`
    on May 11, 2026
  3. chorman0773 commented on May 12, 2026

    @chorman0773
    Contributor

    Note that this replacement isn't possible for functions, only statics (since &raw const fn doesn't work). Otherwise, yeah, I agree that the added level of indirection is unneeded (and personally, not something I'd want to use).

  4. RalfJung commented on May 12, 2026

    @RalfJung
    MemberAuthor

    As @bjorn3 mentioned on Zulip, it's possible, just awkward: you'd do something like

    unsafe extern "C" {
        #[linkage = "extern_weak"]
        static weak_fn: ();
    }
    
    let fn_ptr: Option<fn(i32) -> i32> = transmute(&raw const weak_fn);
    // ...
  5. bjorn3 commented on May 12, 2026

    @bjorn3
    Member

    Targets where function pointers are larger than data pointers would be an issue, but the (unstable) std::marker::FnPtr::addr method would truncate on those targets too.

  6. chorman0773 commented on May 12, 2026

    @chorman0773
    Contributor

    That also loses the ability to use the definition safely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesF-linkage#![feature(linkage)]`needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions