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
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:
I expected to see
MYSCRIPTin the LLVM IR to useglobal, placed intomysection.Instead, I saw the following in the IR:
It is clearly visible that the
#[used]attribute is respected.However, the section is not forwarded to the declaration and
internal constantis also incorrect, because I want to export the symbol globally.Meta
rustc --version --verbose: