Overriding _unhandled_exception doesn't work

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

szekelyisz
Posts: 10
Joined: Wed Jan 03, 2018 8:15 pm

Overriding _unhandled_exception doesn't work

Postby szekelyisz » Thu May 06, 2021 7:03 pm

Hi,

I want to override the default exception handler. I see it's a weak symbol so all it should take is to create a function with the same name and signature, right? So I created a file called handlers.c with the following content:

Code: Select all

#include <stdbool.h>

void _unhandled_exception(void) {
  while (true) {
  }
}


I made sure it's compiled and linked. Yet the weak version is called. Any ideas what's wrong?

Thanks!

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

Re: Overriding _unhandled_exception doesn't work

Postby Giovanni » Thu May 06, 2021 7:24 pm

Hi,

You may look for the symbol in the .map file and see what happened.

Giovanni

szekelyisz
Posts: 10
Joined: Wed Jan 03, 2018 8:15 pm

Re: Overriding _unhandled_exception doesn't work

Postby szekelyisz » Thu May 06, 2021 11:28 pm

Thanks @Giovanni. I checked the map file, and my symbol appears under "Discarded Input Sections". I guess the linker dumps it before it has a chance to override the weak version. But I was unable to keep it in even by marking it with __attribute__((used)).

There are two more references to this symbol, one under "Cross Reference Table" that only tells that it's defined in my source file, and under "Linker script and memory map" on the top of all exception vectors from vectors.o, so it's the weak version.

szekelyisz
Posts: 10
Joined: Wed Jan 03, 2018 8:15 pm

Re: Overriding _unhandled_exception doesn't work

Postby szekelyisz » Thu May 06, 2021 11:53 pm

Here's my linker command:

Code: Select all

arm-none-eabi-g++  <all object files> -mcpu=cortex-m4 -O0 -ggdb3 -fomit-frame-pointer -falign-functions=16 -Wall -ffunction-sections -fdata-sections -fno-common -nostartfiles -Lboard/ -Wl,-Map=build/firmware.map,--cref,--no-warn-mismatch,--library-path=ChibiOS/os/common/startup/ARMCMx/compilers/GCC/ld,--script=board/STM32F427xG_boot_app.ld,--gc-sections,-print-memory-usage,--defsym=__process_stack_size__=0x1000,--defsym=__main_stack_size__=0x400 -mno-thumb-interwork   -o build/firmware.elf


I don't see anything wrong with it.

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

Re: Overriding _unhandled_exception doesn't work

Postby Giovanni » Fri May 07, 2021 6:01 am

Hi,

I tried to add that code to a main.c of an existing demo and it is linked correctly, no need to put the "used" attribute.

Which compiler are you using?

Giovanni

szekelyisz
Posts: 10
Joined: Wed Jan 03, 2018 8:15 pm

Re: Overriding _unhandled_exception doesn't work

Postby szekelyisz » Fri May 07, 2021 1:00 pm

I'm using the most recent version of GCC included with ChibiStudio:

Code: Select all

arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 9-2019-q4-major) 9.2.1 20191025 (release) [ARM/arm-9-branch revision 277599]


I tried it with 10-2020-q4-major, but got the same results.

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

Re: Overriding _unhandled_exception doesn't work

Postby Giovanni » Fri May 07, 2021 1:45 pm

Could you try putting it in main.c ?

Giovanni

szekelyisz
Posts: 10
Joined: Wed Jan 03, 2018 8:15 pm

Re: Overriding _unhandled_exception doesn't work

Postby szekelyisz » Fri May 07, 2021 7:49 pm

Actually I have a main.cpp. I tried putting it there making sure it has C-style linkage, but that didn't help, either.

Do you think it has something to do with messing around with the vector table? I partitioned my flash, the first 128kB is for the bootloader, the main app starts after that. So for the main app I moved my vectors 128kB into the flash by defining CORTEX_VTOR_INIT to 0x00020000U in my chconf.h. I also recompiled and flashed my bootloader with the overridden handler, but no luck.

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

Re: Overriding _unhandled_exception doesn't work

Postby Giovanni » Fri May 07, 2021 8:51 pm

I don't know if it is the moved vectors table, you have a customized setup so you should look into differences.

Giovanni


Return to “General Support”

Who is online

Users browsing this forum: Bing [Bot] and 17 guests