Skip to content

Commit 57ce011

Browse files
authored
Merge pull request #535 from jannic/link_section
Add link_section = ".HardFault.user" to _HardFault
2 parents febacfd + 94bd9c8 commit 57ce011

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

cortex-m-rt/ci/script.sh

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ main() {
2121
divergent-default-handler
2222
divergent-exception
2323
entry-static
24+
hard-fault-trampoline
2425
main
2526
minimal
2627
override-exception
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! This is not an example; this is a linker test that ensures
2+
//! that the jump from HardFault to _HardFault doesn't exceed
3+
//! the 2kB limit of the branch instruction.
4+
5+
#![deny(warnings)]
6+
#![no_main]
7+
#![no_std]
8+
9+
extern crate cortex_m_rt;
10+
extern crate panic_halt;
11+
12+
use core::arch::asm;
13+
use cortex_m_rt::{entry, exception, ExceptionFrame};
14+
15+
// This defines both `HardFault` and `_HardFault`. Both should have
16+
// link_section attributes placing them at the end of the .text section,
17+
// close to each other. If one of them is missing that attribute, they
18+
// will end up separated by `foo`, which will make the linker fail.
19+
#[exception(trampoline = true)]
20+
unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
21+
loop {}
22+
}
23+
24+
#[entry]
25+
fn foo() -> ! {
26+
unsafe {
27+
// 2kB of NOP instructions to make the function artificially larger
28+
asm!(".fill 1024,2,0xbf00",);
29+
}
30+
loop {}
31+
}

cortex-m-rt/macros/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
368368
#(#attrs)*
369369
#[doc(hidden)]
370370
#[export_name = "_HardFault"]
371+
// Only emit link_section when building for embedded targets,
372+
// because some hosted platforms (used to check the build)
373+
// cannot handle the long link section names.
374+
#[cfg_attr(target_os = "none", link_section = ".HardFault.user")]
371375
unsafe extern "C" fn #tramp_ident(frame: &::cortex_m_rt::ExceptionFrame) {
372376
#ident(frame)
373377
}
@@ -379,7 +383,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
379383
// Depending on the stack mode in EXC_RETURN, fetches stack from either MSP or PSP.
380384
core::arch::global_asm!(
381385
".cfi_sections .debug_frame
382-
.section .HardFault.user, \"ax\"
386+
.section .HardFaultTrampoline, \"ax\"
383387
.global HardFault
384388
.type HardFault,%function
385389
.thumb_func

0 commit comments

Comments
 (0)