Skip to content

Static and const variables definitions ignore visiblity and link section #146538

@sutajo

Description

@sutajo

I am working on an Emscripten library for Rust, and Emscripten itself heavily relies on link time trickery, which breaks when symbols are not exported with the right visibility, into the right section.

The following code produces unexpected results:

#[used]
#[unsafe(no_mangle)]
#[unsafe(link_section = "mysection")]
pub static MYSCRIPT: [u8; 16] = *b" return $0 + $1;";

let result = unsafe {
      emscripten_asm_const_int(MYSCRIPT.as_ptr() as _, c"ii".as_ptr(), 10, 43)
};

I expected to see MYSCRIPT in the LLVM IR to use global, placed into mysection.

Instead, I saw the following in the IR:

@MYSCRIPT = internal constant [16 x i8] c" return $0 + $1;", align 1, !dbg !0
...
@llvm.used = appending global [1 x ptr] [ptr @MYSCRIPT], section "llvm.metadata"

It is clearly visible that the #[used] attribute is respected.
However, the section is not forwarded to the declaration and internal constant is also incorrect, because I want to export the symbol globally.

Meta

rustc --version --verbose:

rustc 1.91.0-nightly (02c7b1a7a 2025-09-13)
binary: rustc
commit-hash: 02c7b1a7ac1d739663878030510508372e46f254
commit-date: 2025-09-13
host: x86_64-pc-windows-msvc
release: 1.91.0-nightly
LLVM version: 21.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.O-wasmTarget: WASM (WebAssembly), http://webassembly.org/T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions