Pseudo-random behavior of GCC start-up template when main() returns #15
Labels
bug
Something isn't working
cmsis
CMSIS-related issue or pull-request.
good first issue
Good for newcomers
internal bug tracker
Issue confirmed and reported into a ticket in the internal bug tracking system.
Milestone
Board: NUCLEO-H723ZG
Compiler: arm-none-eabi-gcc (Arch Repository) 14.1.0
The GCC start-up assembly template for the H723xx (see here) contains the following code at the end of
Reset_Handler
:Usually,
main
is assumed not to return. However, if it does, the next instructionBX LR
will jump to the address stored in the link register. This address will usually be the return address for the last sub-routine call insidemain
. So, this instruction will jump back intomain
to a more or less random location. In my particular case, this location was close to the end ofmain
, and execution continued through to the end ofmain
where it pop-ed a value of zero into the program counter. This then triggered a hard fault, because zero is not a thumb mode address, and the default hard fault handler (an infinite loop) was entered.Of course, one can debate about the desired behavior if
main
returns, but I think we can agree the current behavior is not it. One approach would be to replaceBX LR
with an infinite loop, similar to the GCC start-up templates for the H5 series. See for example this snippet below (taken from here):One could also consider throwing in a call to
__libc_fini_array()
aftermain
and before the infinite loop for symmetry.The text was updated successfully, but these errors were encountered: