http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53016
Bug #: 53016 Summary: memcpy optimization can cause unaligned access on ARM Classification: Unclassified Product: gcc Version: 4.4.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: jquesne...@gmail.com Created attachment 27174 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27174 reproduction files The built-in memcpy that -O2 substitutes in seems to cause an unaligned memory access on ARMv5TE when structs are stacked in a certain way. I originally discovered this when a release build of native code for inclusion in an Android program caused a SIGBUS. Attached is a simple test case that replicates this on Android. There is no main() function but it should be trivial to substitute in (sorry, I don't have access to a regular ARM Linux box). It appears to involve over-aggressive use of ldm/stm (possibly ignoring padding?). Works fine (-O0): memcpy((void*)&parent.children[2],(const void*)child3,size); 24: 4b0a ldr r3, [pc, #40] 26: 447b add r3, pc 28: 1c19 adds r1, r3, #0 2a: 3138 adds r1, #56 2c: 4b09 ldr r3, [pc, #36] 2e: 447b add r3, pc 30: 681b ldr r3, [r3, #0] 32: 9a03 ldr r2, [sp, #12] 34: 1c08 adds r0, r1, #0 36: 1c11 adds r1, r2, #0 38: 1c1a adds r2, r3, #0 3a: f7ff fffe bl 0 <memcpy> Gives SIGBUS (-O2): memcpy((void*)&parent.children[2],(const void*)child3,size); 2: 4b07 ldr r3, [pc, #28] 4: 4907 ldr r1, [pc, #28] 6: 447b add r3, pc 8: 681a ldr r2, [r3, #0] a: 4479 add r1, pc c: 3138 adds r1, #56 e: 1c0b adds r3, r1, #0 10: 323c adds r2, #60 12: ca31 ldmia r2!, {r0, r4, r5} <--- Unaligned access 14: c331 stmia r3!, {r0, r4, r5} 16: ca13 ldmia r2!, {r0, r1, r4} 18: c313 stmia r3!, {r0, r1, r4} 1a: 6812 ldr r2, [r2, #0] 1c: 601a str r2, [r3, #0] I have confirmed this both on a TI OMAP 3530 (BeagleBoard) and Samsung Exynos 3110 (Samsung Epic 4G). I'm not sure if this is the same as bug #47754.