Unexpected unaligned access on arm
Lars Poeschel
poeschel@lemonage.de
Mon Jun 8 13:34:11 GMT 2020
Hi!
I am experiencing a problem on ARM with unaligned access where I think
it should not happen. I could track this down to the attached example. I
compile the code using the following command:
arm-linux-gnueabihf-gcc -Wall -Os -g -Wa,-mthumb -mthumb-interwork -mno-unaligned-access -mhard-float -march=armv7-a -static -o gcc_example gcc_example.c
The problem is in net_set_ip_header function. The relevant produced
assembler output is this:
1044c: b513 push {r0, r1, r4, lr}
1044e: 4604 mov r4, r0
10450: 9200 str r2, [sp, #0]
10452: 300c adds r0, #12
10454: 4a11 ldr r2, [pc, #68] ; (1049c <net_set_ip_header+0x50>)
10456: 4b10 ldr r3, [pc, #64] ; (10498 <net_set_ip_header+0x4c>)
10458: 447a add r2, pc
1045a: 9101 str r1, [sp, #4]
1045c: f840 3c0c str.w r3, [r0, #-12]
...
10498: 14000045 .word 0x14000045
1049c: 00060a48 .word 0x00060a48
The compiler is smart and optimizes setting the constants to the struct
ip_udp_hdr fields ip_hl_v ip_tos and ip_len to one single 32bit (word)
access. It stores the three constants pc relative in a word. This gets
loaded to r3 and then stored to the structure in memory (str.w
instruction).
Since I compile with -mno-unaligned-access and the relevant structure
members are 8 / 16bit and even the function argument uint8_t *pkt pointer
tell the compiler that these are not neccessarily 32bit aligned, I think
it should not do this.
The relevant code crashes on my cpu depending on the value of the pkt
pointer when calling the function of course. The
pkt += ETH_HDR_SIZE;
line can be used to modify the pointer if needed.
If I compile with -O0 or -O1 it does not happen. It does also not
happen when commenting out the asm("nop"); as a optimization barrier.
I tested this with gcc 8.3.1, 9.3.0 and 10.1.0. They all behave more or
less the same in this regard.
What am I missing ? What am I doing wrong ?
Thank you,
Lars
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gcc_test.c
Type: text/x-csrc
Size: 1708 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20200608/fe53c6ee/attachment.bin>
More information about the Gcc-help
mailing list