Skip to content

Commit 98eb7ea

Browse files
committed
b UserHardFault
this commit replaces the `bl UserHardFault` instruction in HardFault with `b UserHardFault`. This lets us drop the prologue (`push {r0,lr}`) while preserving the ability to unwind the stack using GDB. To prevent linker errors about relocations when UserHardFault and HardFault end up far away from each other and the target is ARMv6-M (where the `b` instruction only supports offsets of +/- 2KB) we use two *input* sections: .HardFault and .UserHardFault. HardFault is placed in the .HardFault section and UserHardFault is placed in the .UserHardFault section. The .HardFault input section is placed in the output .text section after all the input .text sections, and the .UserHardFault input section is placed after the .HardFault section. This causes the two symbols to always appear next to each other in the output binary, furthermore UserHardFault *always* appears after HardFault so the branch offset of `b UserHardFault` is always a few bytes. It should be noted that neither .HardFault or .UserHardFault will appear in the output of the `size -A` command as they are input sections that get merged into the output .text section. IOW, the sizes of HardFault and UserHardFault will continue to be reported under the .text section.
1 parent 3d43614 commit 98eb7ea

File tree

8 files changed

+10
-3
lines changed

8 files changed

+10
-3
lines changed

cortex-m-rt/asm.s

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
.section .text.HardFault
1+
# LLD requires that the section flags are explicitly set here
2+
.section .HardFault, "ax"
23
.global HardFault
4+
# .type and .thumb_func are both required; otherwise its Thumb bit does not
5+
# get set and an invalid vector table is generated
6+
.type HardFault,%function
37
.thumb_func
48
HardFault:
5-
push {r0, lr}
69
mrs r0, MSP
7-
bl UserHardFault
10+
b UserHardFault

cortex-m-rt/bin/thumbv6m-none-eabi.a

-38 Bytes
Binary file not shown.

cortex-m-rt/bin/thumbv7em-none-eabi.a

-38 Bytes
Binary file not shown.
-38 Bytes
Binary file not shown.

cortex-m-rt/bin/thumbv7m-none-eabi.a

-38 Bytes
Binary file not shown.

cortex-m-rt/link.x.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ SECTIONS
8686
.text _stext :
8787
{
8888
*(.text .text.*);
89+
*(.HardFault);
90+
*(.UserHardFault);
8991
} > FLASH
9092

9193
/* ### .rodata */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
365365

366366
quote!(
367367
#[export_name = "UserHardFault"]
368+
#[link_section = ".UserHardFault"]
368369
#(#attrs)*
369370
pub #unsafety extern "C" fn #hash(#arg) -> ! {
370371
extern crate cortex_m_rt;

cortex-m-rt/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ pub unsafe extern "C" fn Reset() -> ! {
532532

533533
#[allow(unused_variables)]
534534
#[doc(hidden)]
535+
#[link_section = ".UserHardFault"]
535536
#[no_mangle]
536537
pub unsafe extern "C" fn UserHardFault_(ef: &ExceptionFrame) -> ! {
537538
loop {

0 commit comments

Comments
 (0)