http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56997



             Bug #: 56997

           Summary: Incorrect write to packed field when

                    strict-volatile-bitfields enabled on aarch32

    Classification: Unclassified

           Product: gcc

           Version: 4.9.0

            Status: UNCONFIRMED

          Severity: normal

          Priority: P3

         Component: target

        AssignedTo: unassig...@gcc.gnu.org

        ReportedBy: joey...@arm.com

            Target: arm/aarch32





Attached case fails when

Target: all arm aarch32

Optimization level:all optimization levels.

Version: Trunk 197955, 4.7, 4.6



But it passes:

- if -fno-strict-volatile-bitfields is specified, or

- on x86 even if -fstrict-volatile-bitfields is specified



For example:



arm-none-eabi-gcc -mthumb -mcpu=cortex-m0 -Os  a.c -o a.s -S



foo:

        ldr     r3, .L2

        lsl     r2, r0, #8  ; First byte of input r0 is truncated

        ldr     r0, [r3]

        uxtb    r0, r0

        orr     r0, r2

        str     r0, [r3]

        bx      lr



Runtime output:

Write FAIL 0x20304



---------------------

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#ifdef SHORT

#define test_type unsigned short

#define MAGIC 0x102u

#else

#define test_type unsigned int

#define MAGIC 0x1020304u

#endif



typedef struct s{

 unsigned char Prefix;

 test_type Type;

}__attribute((__packed__)) ss;



volatile ss v;

ss g;



void __attribute__((noinline))

foo (test_type u)

{

  v.Type = u;

}



test_type __attribute__((noinline))

bar (void)

{

  return v.Type;

}



int main()

{

  test_type temp;

  int err = 0;

  foo(MAGIC);

  memcpy(&g, (void *)&v, sizeof(g));

  if (g.Type != MAGIC)

    {

      printf("Write FAIL 0x%x\n", g.Type);

      err ++;

    }



  g.Type = MAGIC;

  memcpy((void *)&v, &g, sizeof(v));

  temp = bar();

  if (temp != MAGIC)

    {

      printf("Read FAIL 0x%x\n", temp);

      err ++;

    }

  return err;

}

Reply via email to