Skip to content

Make the HardFault trampoline opt-in #406

Closed
@jonas-schievink

Description

@jonas-schievink

Currently, any user of this crate has to pay for a small Assembly trampoline that provides access to the &ExceptionFrame to the HardFault handler (even if they don't use the &ExceptionFrame, or don't even define any HardFault handler):

https://github.com/rust-embedded/cortex-m-rt/blob/492c7bf6212486b378ecf0518572d180a50c83a1/asm.s#L8-L19

Since few people make use of the &ExceptionFrame argument, we could add a Cargo feature to turn it on instead, and generate the right linker script in build.rs. By forwarding the feature to the macro crate, #[exception] can provide good diagnostics when trying to use &ExceptionFrame without the feature enabled.

I am planning on extending the trampoline mechanism to other faults and maybe even all exceptions, which I've prototyped by making the ASM trampoline generic and have it work for all faults. This makes it larger and adds more data to the excutable, so making this opt-in would help reduce the cost. The trampoline code could also benefit from ARMv7/v8-specific improvements, which I'll work on later, which should shrink it again (for the higher platforms at least).

I suppose we could also have one Cargo feature per exception. The build script would then generate a linker script that pulls in exactly the trampolines that are needed. This should even still work with the precompiled .a files, if we do it right (putting every trampoline in its own section and making sure they're only pulled in when necessary).

Initially, defining 10 or 11 Cargo features for this sounds pretty sketchy, but most apps won't use any of them, and even "advanced" apps that implement an RTOS can get away with just 1 or 2 (one for SVCall to read syscall arguments, and one on HardFault/DefaultHandler for debugging crashes).

An ideal solution would not require Cargo features at all and instead make the #[exception] macro pull in the Assembly trampoline. I have not found any way to do this though. If one does come up (perhaps using the currently unstable #[link_args] attribute), we can make the Cargo features no-ops until the next major version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions