Skip to content

Commit 68c56ab

Browse files
bors[bot]japaric
andcommitted
Merge #143
143: `b UserHardFault` r=adamgreig a=japaric 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. cc @adamgreen Co-authored-by: Jorge Aparicio <[email protected]>
2 parents f7ab39c + 98eb7ea commit 68c56ab

File tree

8 files changed

+10
-3
lines changed

8 files changed

+10
-3
lines changed

cortex-m-rt/asm.s

+6-3
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

+2
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

+1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
394394

395395
quote!(
396396
#[export_name = "UserHardFault"]
397+
#[link_section = ".UserHardFault"]
397398
#(#attrs)*
398399
pub #unsafety extern "C" fn #hash(#arg) -> ! {
399400
extern crate cortex_m_rt;

cortex-m-rt/src/lib.rs

+1
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)