Skip to content

Commit 2557ac4

Browse files
committed
reduce the size of default handlers
this commit replaces the two default handler (DefaultDefaultHandler and DefaultUserHardFault) by a single handler named `EndlessLoop`. This results in one less symbol being generated. Also this new handler uses `compiler_fence` to avoid the "infinite loops w/o side effects are undef values" bug in LLVM. `compiler_fence` is guaranteed to generate no code so this reduces the size of the handler (when compiler in release).
1 parent 8c4a9d2 commit 2557ac4

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

cortex-m-rt/link.x.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ PROVIDE(DebugMonitor = DefaultHandler);
4545
PROVIDE(PendSV = DefaultHandler);
4646
PROVIDE(SysTick = DefaultHandler);
4747

48-
PROVIDE(DefaultHandler = DefaultDefaultHandler);
49-
PROVIDE(UserHardFault = DefaultUserHardFault);
48+
PROVIDE(DefaultHandler = EndlessLoop);
49+
PROVIDE(UserHardFault = EndlessLoop);
5050

5151
/* # Interrupt vectors */
5252
EXTERN(__INTERRUPTS); /* `static` variable similar to `__EXCEPTIONS` */

cortex-m-rt/src/lib.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,8 @@
403403

404404
extern crate r0;
405405

406-
use core::{fmt, ptr};
406+
use core::fmt;
407+
use core::sync::atomic::{self, Ordering};
407408

408409
/// Registers stacked (pushed into the stack) during an exception
409410
#[derive(Clone, Copy)]
@@ -531,21 +532,11 @@ pub unsafe extern "C" fn Reset() -> ! {
531532

532533
#[doc(hidden)]
533534
#[no_mangle]
534-
pub unsafe extern "C" fn DefaultDefaultHandler() {
535+
pub unsafe extern "C" fn EndlessLoop() -> ! {
535536
loop {
536537
// add some side effect to prevent this from turning into a UDF instruction
537-
// see rust-lang/rust#28728
538-
ptr::read_volatile(&0u8);
539-
}
540-
}
541-
542-
#[doc(hidden)]
543-
#[no_mangle]
544-
pub unsafe extern "C" fn DefaultUserHardFault() {
545-
loop {
546-
// add some side effect to prevent this from turning into a UDF instruction
547-
// see rust-lang/rust#28728
548-
ptr::read_volatile(&0u8);
538+
// see rust-lang/rust#28728 for details
539+
atomic::compiler_fence(Ordering::SeqCst);
549540
}
550541
}
551542

@@ -924,5 +915,5 @@ macro_rules! pre_init {
924915
let f: unsafe fn() = $handler;
925916
f();
926917
}
927-
}
918+
};
928919
}

0 commit comments

Comments
 (0)