Skip to content

Commit

Permalink
naked_asm: add cfi_startproc/cfi_endproc to all platforms
Browse files Browse the repository at this point in the history
Since 2024-12-12 the Rust compiler now requires all naked ASM functions
to include `.cfi_startproc` and `.cfi_endproc`. Without these
directives, the build will fail.

Add these directives to all supported platforms.

This also works around rust-lang/rust#80608 by forcing LLVM to consider that
code with the "d" extension has an FPU.

Signed-off-by: Sean Cross <sean@xobs.io>
  • Loading branch information
xobs authored and nbdd0121 committed Dec 26, 2024
1 parent ff0e91b commit 8f3beee
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/unwinder/arch/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ macro_rules! save {
(gp$(, $fp:ident)?) => {
// No need to save caller-saved registers here.
core::arch::naked_asm!(
maybe_cfi!(".cfi_startproc"),
"stp x29, x30, [sp, -16]!",
maybe_cfi!("
.cfi_def_cfa_offset 16
Expand Down Expand Up @@ -98,6 +99,7 @@ macro_rules! save {
.cfi_restore x30
"),
"ret",
maybe_cfi!(".cfi_endproc"),
);
};
(maybesavefp(fp)) => {
Expand Down
8 changes: 8 additions & 0 deletions src/unwinder/arch/riscv32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ macro_rules! code {
"
};
(save_fp) => {
// arch option manipulation needed due to LLVM/Rust bug, see rust-lang/rust#80608
"
.option push
.option arch, +d
fsd fs0, 0xC0(sp)
fsd fs1, 0xC8(sp)
fsd fs2, 0x110(sp)
Expand All @@ -95,6 +98,7 @@ macro_rules! code {
fsd fs9, 0x148(sp)
fsd fs10, 0x150(sp)
fsd fs11, 0x158(sp)
.option pop
"
};
(restore_gp) => {
Expand Down Expand Up @@ -175,6 +179,7 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
#[cfg(target_feature = "d")]
unsafe {
core::arch::naked_asm!(
maybe_cfi!(".cfi_startproc"),
"
mv t0, sp
add sp, sp, -0x190
Expand All @@ -194,11 +199,13 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
maybe_cfi!(".cfi_def_cfa_offset 0"),
maybe_cfi!(".cfi_restore ra"),
"ret",
maybe_cfi!(".cfi_endproc"),
);
}
#[cfg(not(target_feature = "d"))]
unsafe {
core::arch::naked_asm!(
maybe_cfi!(".cfi_startproc"),
"
mv t0, sp
add sp, sp, -0x90
Expand All @@ -217,6 +224,7 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
maybe_cfi!(".cfi_def_cfa_offset 0"),
maybe_cfi!(".cfi_restore ra"),
"ret",
maybe_cfi!(".cfi_endproc")
);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/unwinder/arch/riscv64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ macro_rules! code {
"
};
(save_fp) => {
// arch option manipulation needed due to LLVM/Rust bug, see rust-lang/rust#80608
"
.option push
.option arch, +d
fsd fs0, 0x140(sp)
fsd fs1, 0x148(sp)
fsd fs2, 0x190(sp)
Expand All @@ -95,6 +98,7 @@ macro_rules! code {
fsd fs9, 0x1C8(sp)
fsd fs10, 0x1D0(sp)
fsd fs11, 0x1D8(sp)
.option pop
"
};
(restore_gp) => {
Expand Down Expand Up @@ -175,6 +179,7 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
#[cfg(target_feature = "d")]
unsafe {
core::arch::naked_asm!(
maybe_cfi!(".cfi_startproc"),
"
mv t0, sp
add sp, sp, -0x210
Expand All @@ -194,11 +199,13 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
maybe_cfi!(".cfi_def_cfa_offset 0"),
maybe_cfi!(".cfi_restore ra"),
"ret",
maybe_cfi!(".cfi_endproc"),
);
}
#[cfg(not(target_feature = "d"))]
unsafe {
core::arch::naked_asm!(
maybe_cfi!(".cfi_startproc"),
"
mv t0, sp
add sp, sp, -0x110
Expand All @@ -217,6 +224,7 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
maybe_cfi!(".cfi_def_cfa_offset 0"),
maybe_cfi!(".cfi_restore ra"),
"ret",
maybe_cfi!(".cfi_endproc"),
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/unwinder/arch/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
// No need to save caller-saved registers here.
unsafe {
core::arch::naked_asm!(
maybe_cfi!(".cfi_startproc"),
"sub esp, 52",
maybe_cfi!(".cfi_def_cfa_offset 56"),
"
Expand Down Expand Up @@ -97,6 +98,7 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
",
maybe_cfi!(".cfi_def_cfa_offset 4"),
"ret",
maybe_cfi!(".cfi_endproc"),
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/unwinder/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
// No need to save caller-saved registers here.
unsafe {
core::arch::naked_asm!(
maybe_cfi!(".cfi_startproc"),
"sub rsp, 0x98",
maybe_cfi!(".cfi_def_cfa_offset 0xA0"),
"
Expand Down Expand Up @@ -91,7 +92,8 @@ pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), p
add rsp, 0x98
",
maybe_cfi!(".cfi_def_cfa_offset 8"),
"ret"
"ret",
maybe_cfi!(".cfi_endproc"),
);
}
}
Expand Down

0 comments on commit 8f3beee

Please sign in to comment.