https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79065

            Bug ID: 79065
           Summary: ARM generates ldm/stm on packed structs with aligned
                    members
           Product: gcc
           Version: 5.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vielster at zee dot aero
  Target Milestone: ---

LDM/STM do not support unaligned access, so packed structs should not be
accessed in this way. If internal members of the struct are aligned to pad for
inter-platform compatibility (in this case, variable enum size), GCC generates
LDM/STM and can cause unaligned access faults.

Below is the code and the assembly generated with arm-none-eabi-gcc -O1

#include <stdint.h>
typedef struct __attribute__((__packed__))
{
  uint32_t a;    
  uint8_t b __attribute__((aligned(4)));
  uint8_t c;         
}foo_t;

void struct_copy(foo_t *p_dest, foo_t *p_src)
{
  *p_src = *p_dest;
}

struct_copy(foo_t*, foo_t*):
        mov     r3, r1
        ldmia   r0, {r0, r1}
        stmia   r3, {r0, r1}
        bx      lr

Reply via email to