-Os optimisation causes hard fault
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
GNU Arm Embedded Toolchain |
New
|
Undecided
|
Unassigned |
Bug Description
Toolchain: GNU Arm Embedded Toolchain 10-2020-q4-major (not rebuilt)
Host: Mac BigSur
IDE: Eclipse 4.21.0
Target: Cortex-M33 (Nordic nrf5340)
With optimisation level -Os (optimise for size), the following startup code for nrf5340 from Nordic fails:
void Reset_Handler(void)
{
unsigned long *pSrc, *pDst;
//SEGGER_
/* Copy data segment initializers from flash to SRAM. */
pSrc = &__text_end__;
pDst = &__data_start__;
while (pDst < &__data_end__)
{
*pDst++ = *pSrc++;
}
The first two assignments (pSrc = and pDst = ) are optimised away but the while loop is not. Hence a hard fault occurs when the while loop is executed (occurs immediately after reset so no code is actually executed)
Regards,
skajam66
Unfortunately such pointer operations are considered to have undefined behavior and the compiler is free to take whatever actions it thinks more appropriate.
I saw other such cases during startup when the compiler used float instructions before having the chance for the FPU to be initialised, also resulting in crashes.