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

            Bug ID: 87942
           Summary: every int seems to be unaligned in packed structure
           Product: gcc
           Version: 8.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: johannes.vet...@dom-sicherheitstechnik.de
  Target Milestone: ---

testcode:

#pragma pack(1)
    typedef struct
    {
        volatile int cmd;
    } test_t;
#pragma pack(0)

test_t test;

int read32() 
{ 
    test.cmd = 11;
    return 0; 
}

host: win/linux x86_64
target: arm, STM32L471
gcc versions: 4.8.3 (ok), bugs in: 5.4.1, 6.3.0, 7.2.1, 8.2.0

---
since version gcc version>=5 there seems to be a bug if pragma pack(1) is used.
gcc thinks test.cmd is missaligned, but it isn't.

The code is ok in gcc version 4. The code with AND without pack(1) should be
identical in this example.

The inner code should be something like this:
        mov     r2, #11
        str     r2, [r3, #0]

but with pack(1):
        mov     r1, #11
        mov     r2, #0
        ldr     r3, .L3
        ldrb    r0, [r3]        @ zero_extendqisi2
        strb    r1, [r3]
        ldrb    r1, [r3, #1]    @ zero_extendqisi2
        strb    r2, [r3, #1]
        ldrb    r1, [r3, #2]    @ zero_extendqisi2
        strb    r2, [r3, #2]
        mov     r0, r2
        ldrb    r1, [r3, #3]    @ zero_extendqisi2
        strb    r2, [r3, #3]

The problem is that the variable in this case is a special function register of
the chip and have to be accessed in 32bit only. Every 8bit access to this
register crashes the system.

It would be nice, if there is a compiler switch, which leeds into an error, if
a volatile variable can't be read/write in one single access.

Reply via email to