_unhandled_exception strikes back! Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
User avatar
Spym
Posts: 45
Joined: Tue Oct 15, 2013 10:20 am
Location: Tallinn, Estonia
Has thanked: 2 times
Been thanked: 3 times
Contact:

_unhandled_exception strikes back!

Postby Spym » Thu Apr 19, 2018 3:09 pm

Hello again, people.

Last year I came up with this simple proposition which ended up being successfully implemented in ChibiOS: viewtopic.php?f=35&t=3819 (Thanks, Giovanni). TLDR the unexpected exception handler has been made weak, which allowed me to redefine it in my application. This is absolutely crucial for me, and it should be utterly important to everybody, otherwise, any unexpected IRQ causes the system to just hang bypassing the proper panic protocol (imagine what happens if you're running a power converter or an electric motor).

Anyway, the current implementation has a problem. This is how the weak unexpected handler is defined for GCC ARM CM4:

Code: Select all

         b          _unhandled_exception

        .thumb_func
        .weak       _unhandled_exception
_unhandled_exception:
.stay:
        b           .stay


The problem is that if your firmware is sufficiently large, the branch "b _unhandled_exception" may fail to reach the redefined handler, resulting in the following error:

Code: Select all

chibios/os/common/startup/ARMCMx/compilers/GCC/vectors.S:767:(.text+0x2): relocation truncated to fit: R_ARM_THM_JUMP11 against symbol `_unhandled_exception' defined in .text._unhandled_exception section in /tmp/ccI8EdoC.ltrans5.ltrans.o


We need a different branch instruction that has a wider reach. A solution that should be compatible with any kind of Cortex M is to simply replace "B" with "BL":

Code: Select all

         bl         _unhandled_exception

        .thumb_func
        .weak       _unhandled_exception
_unhandled_exception:
.stay:
        b           .stay


Yes, it clobbers the link register, but at this point, we don't care about it, because the unexpected handler is not supposed to return ever.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: _unhandled_exception strikes back!  Topic is solved

Postby Giovanni » Fri Apr 27, 2018 2:16 pm

Hi,

Fixed as bug #941.

Giovanni


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 65 guests