Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add link_section = ".HardFault.user" to _HardFault #535

Merged
merged 3 commits into from
Sep 10, 2024
Merged
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
1 change: 1 addition & 0 deletions cortex-m-rt/ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ main() {
divergent-default-handler
divergent-exception
entry-static
hard-fault-trampoline
main
minimal
override-exception
Expand Down
31 changes: 31 additions & 0 deletions cortex-m-rt/examples/hard-fault-trampoline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! This is not an example; this is a linker test that ensures
//! that the jump from HardFault to _HardFault doesn't exceed
//! the 2kB limit of the branch instruction.

#![deny(warnings)]
#![no_main]
#![no_std]

extern crate cortex_m_rt;
extern crate panic_halt;

use core::arch::asm;
use cortex_m_rt::{entry, exception, ExceptionFrame};

// This defines both `HardFault` and `_HardFault`. Both should have
// link_section attributes placing them at the end of the .text section,
// close to each other. If one of them is missing that attribute, they
// will end up separated by `foo`, which will make the linker fail.
#[exception(trampoline = true)]
unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
loop {}
}

#[entry]
fn foo() -> ! {
unsafe {
// 2kB of NOP instructions to make the function artificially larger
asm!(".fill 1024,2,0xbf00",);
}
loop {}
}
6 changes: 5 additions & 1 deletion cortex-m-rt/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
#(#attrs)*
#[doc(hidden)]
#[export_name = "_HardFault"]
// Only emit link_section when building for embedded targets,
// because some hosted platforms (used to check the build)
// cannot handle the long link section names.
#[cfg_attr(target_os = "none", link_section = ".HardFault.user")]
unsafe extern "C" fn #tramp_ident(frame: &::cortex_m_rt::ExceptionFrame) {
#ident(frame)
}
Expand All @@ -379,7 +383,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
// Depending on the stack mode in EXC_RETURN, fetches stack from either MSP or PSP.
core::arch::global_asm!(
".cfi_sections .debug_frame
.section .HardFault.user, \"ax\"
.section .HardFaultTrampoline, \"ax\"
.global HardFault
.type HardFault,%function
.thumb_func
Expand Down