global_asm!
sym operands emit functions without function body
#96623
Labels
A-inline-assembly
Area: Inline assembly (`asm!(…)`)
C-bug
Category: This is a bug.
requires-nightly
This issue requires a nightly compiler in some way.
Summary
If a function is only referenced by a
sym
operand inglobal_asm!
, the definition/body of that function will not be emitted by the compiler, and the linker will fail withunresolved symbol
error.Reproduction
Save the following code as
example.rs
: (https://rust.godbolt.org/z/4jbf3eWjb)(
.global _start
is there to stop the linker from pruning unreachable code.)Run
rustc example.rs -Cpanic=abort -Clink-args="-nostdlib"
produces a linker error "undefined reference toexample::main
":objdump -td example.example.2372ba8c-cgu.0.rcgu.o
verifies thatexample::foo
is indeed missing:The emitted llvm-ir looks something like this:
Some observations
The problem only appears if the function in question isn't used anywhere else. If we uncomment line
^4
, the resulting IR will be correctly emitted:Only functions can reproduce, static items work fine, as per line
^2
.Only
global_asm!
can reproduce,asm!
works fine, as per line^3
and co.Meta
cc @Amanieu #94468
@rustbot label +A-inline-assembly +requires-nightly
The text was updated successfully, but these errors were encountered: