Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/rustc_codegen_ssa/src/mir/naked_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ fn prefix_and_suffix<'tcx>(
emit_fatal("Functions may not have available_externally linkage")
}
Linkage::ExternalWeak => {
// FIXME: actually this causes a SIGILL in LLVM
emit_fatal("Functions may not have external weak linkage")
// ExternalWeak linkage can cause SIGILL in LLVM for naked functions.
// Fall back to External linkage as a safer alternative that preserves
// the intended visibility while avoiding the LLVM bug.
writeln!(w, ".globl {asm_name}")?;
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/ui/asm/naked-functions-external-weak.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ compile-flags: -C opt-level=0
//@ needs-asm-support
//@ only-x86_64

// Test that naked functions with external weak linkage don't cause SIGILL
// This is a regression test for issue #142880

#![feature(naked_functions)]
#![feature(linkage)]

use std::arch::asm;

#[naked]
#[linkage = "external_weak"]
extern "C" fn naked_weak_function() -> u32 {
unsafe {
asm!(
"mov rax, 42",
"ret",
options(noreturn)
);
}
}

fn main() {
// Test that the function compiles without causing SIGILL
// We don't actually call it as it may not be linked
println!("Test passed: naked function with external_weak linkage compiled successfully");
}
Loading