https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116856
Bug ID: 116856
Summary: Generated Code with unaligned uint32_t potentially
hardfaults on ARM (due to LDRD)
Product: gcc
Version: 13.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: robert.hoelzl at posteo dot de
Target Milestone: ---
When accessing two consecutive 32bit values and xor-ing them (or probably doing
any operation)
GCC ARM32 in optimization mode uses LDRD
(https://developer.arm.com/documentation/ddi0406/c/Application-Level-Architecture/Instruction-Details/Alphabetical-list-of-instructions/LDRD--immediate-?lang=en)
to load both values at once.
But LDRD does not work on unaligned pointers
(https://developer.arm.com/documentation/dui0473/m/using-the-assembler/address-alignment)
and thus causing a hard fault.
The code to reproduce the problem roughly looks like:
uint8_t array[100];
uint32_t * ptr = array + 1; // <- maybe you need another index to ensure
that ptr is unaligned
*ptr ^= *(ptr+1);
Tested on Cortex-M33 with GCC 13.3 and GCC 10.3 with optimization for size.