Why am I getting a HardFault with -O2 optimization only?
In my STM32F091 project, the Debug build works all just fine.
Release build does not.
So I kept -O2 to keep the behavior, but added -g3 to be able to debug.
Fixed a couple typical bugs like a forgotten volatile on an ISR-accessed flag etc.
Now I get a HARD FAULT upon, if I read this right (my asm is rusty and I never actively did ARM asm),
the preparations towards a function call. Within function 'ReadBytes', another function 'Read' is called.
It does crash if, in the debugger, I want to step over it.
In instruction stepping mode, it's the line at address 0800e880 which causes the hard fault.
I do not understand why the asm code is doing some of the things it does (see comments there).
It looks like it is doing a wrong memory access.
But why? Assuming the compiler did nothing wrong - what could I have done wrong?
With -O1, the asm code is somewhat different, and the crash does not happen.
(and at first glance, the program seems to run normally overall)
EDIT: The argument values going into the outer function, ReadBytes, are the same in both, the -O2 and the -O1 build (seen in debugger).
Does someone have an idea what's going on?
// code with -O2
//
// Breakpoint here @ line 173
//
173 const auto ret = m_i2c.Read( m_addr, memoryWordAddr, dest, count );
SerialEEPROM::ReadBytes(unsigned int, unsigned char*, unsigned int, unsigned int):
0800e870: push {r4, r5, lr}
0800e872: movs r4, r0
0800e874: sub sp, #12
0800e876: movs r3, r2 // r0 == 0 here, which is ok, it's EEPROM-internal address (see C++ func below, arg 'memoryWordAddr')
0800e878: ldr r0, [r0, #0] // No idea why r0 is used as pointer here? And it's null...
0800e87a: uxtb r2, r1 // r0 == 0x20008000 here, i.e. END OF RAM (32KB)
0800e87c: ldrb r1, [r4, #4]
0800e87e: ldr r4, [sp, #24]
0800e880: ldr r5, [r0, #0] // HARD FAULT. Did not like access after end of RAM, I guess.
0800e882: uxtb r4, r4
0800e884: str r4, [sp, #0]
0800e886: ldr r4, [r5, #4]
0800e888: blx r4 // so here it should jump
174 if (ret != I2cResult::Ok)
0800e88a: subs r3, r0, #1
0800e88c: sbcs r0, r3
0800e88e: negs r0, r0
0800e890: add sp, #12
0800e892: pop {r4, r5, pc}// code with -O1: there actually are some instructions before the breakpoint - with -O2 this was not so (as belonging to func ReadBytes()).
//
169 {
SerialEEPROM::ReadBytes(unsigned int, unsigned char*, unsigned int, unsigned int):
0800e6e6: push {r4, r5, lr}
0800e6e8: sub sp, #12
0800e6ea: movs r4, r0
0800e6ec: movs r3, r2
//
// Breakpoint here @ line 173
//
173 const auto ret = m_i2c.Read( m_addr, memoryWordAddr, dest, count );
0800e6ee: ldr r0, [r0, #0] // before this, r0 == 0x200009b8 -- not 0 like with -O2 above, hm?!
0800e6f0: uxtb r2, r1 // now r0 == 0x200008e8
0800e6f2: ldrb r1, [r4, #4]
0800e6f4: ldr r5, [r0, #0] // hunky dory
0800e6f6: ldr r4, [sp, #24]
0800e6f8: uxtb r4, r4
0800e6fa: str r4, [sp, #0]
0800e6fc: ldr r4, [r5, #4]
0800e6fe: blx r4
174 if (ret != I2cResult::Ok)
0800e700: subs r3, r0, #1
0800e702: sbcs r0, r3
0800e704: negs r0, r0
0800e706: add sp, #12
0800e708: pop {r4, r5, pc}int SerialEEPROM::ReadBytes(uint_fast8_t memoryWordAddr, uint8_t* dest, unsigned destLen, unsigned count)
{
ASSERT( memoryWordAddr + count <= TotalSizeBytes );
ASSERT( count <= destLen );
// Breakpoint here
const auto ret = m_i2c.Read( m_addr, memoryWordAddr, dest, count );
if (ret != I2cResult::Ok)
return -1;
return 0;
}