This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/46483] New: Built-in memcpy() does not handle unaligned int for ARM


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

           Summary: Built-in memcpy() does not handle unaligned int for
                    ARM
           Product: gcc
           Version: 4.5.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: fredrik.hederstierna@securitas-direct.com


I had some problems with memcpy() when copying unaligned integers for ARM.

For intel target it worked fine, and I concluded that if I overloaded gcclib
weak memcpy-reference I had the same problem, but if I just overloaded memcpy
with a #define to my own function, it worked fine.

At first glance I though it was just a possible strict-aliasing violation, but
when having a closer look I'm not so sure, so I submit this to Bugzilla anyway
for validation.

I submit some lines of source-code that gives an example of the problem.

----------------------
#include <stdio.h>
----------------------
struct unaligned_int {
  char dummy1;
  char dummy2;
  char dummy3;
  unsigned int number;
} __attribute__((packed));
//---------------------
void *my_memcpy(void *dst, const void *src, size_t n)
{
  char *s = (char*)src;
  char *d = (char*)dst;
  while (n--) {
    *d++ = *s++;
  }
  return dst;
}
//---------------------
static void copy_x_into_struct(char *buf, unsigned int x)
{
  unsigned int i;
  struct unaligned_int* testp = (struct unaligned_int*)buf;

  memset(buf, 0xFF, sizeof(unsigned int));
  memcpy((void*)&(testp->number), &x, sizeof(unsigned int));

  printf("BUILT-IN MEMCPY TO %08x: ", &(testp->number));
  for (i = 0; i < sizeof(struct unaligned_int); i++) {
    printf(" %02x", buf[i]);
  }
  printf("\n");

  memset(buf, 0xFF, sizeof(unsigned int));
  my_memcpy((void*)&(testp->number), &x, sizeof(int));

  printf("USER-DEF MEMCPY TO %08x: ", &(testp->number));
  for (i = 0; i < sizeof(struct unaligned_int); i++) {
    printf(" %02x", buf[i]);
  }
  printf("\n");
}
//---------------------
int main(void)
{
  char buf[100];
  unsigned int x = 0x12345678;
  copy_x_into_struct(buf, x);
  return 0;
}

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

PROGRAM OUTPUT:

BUILT-IN MEMCPY TO 04016bd7:  78 56 34 12 00 00 00
USER-DEF MEMCPY TO 04016bd7:  ff ff ff 78 56 34 12

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

GCC-COMMAND-LINE:

arm-elf-gcc -g3 -ggdb3 -gdwarf-2 -mthumb -c -Wall -W -Wextra
-Wno-unused-parameter -mcpu=arm966e-s -Os -fno-omit-frame-pointer -fno-web
-mhard-float -mfpu=fpa -ffunction-sections -fdata-sections 
-o test.o test.c

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

TOOLCHAIN:  Attaching toolchain build-script.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]