public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/46483] New: Built-in memcpy() does not handle unaligned int for ARM
@ 2010-11-15 14:53 fredrik.hederstierna@securitas-direct.com
  2010-11-15 15:03 ` [Bug c/46483] " fredrik.hederstierna@securitas-direct.com
                   ` (15 more replies)
  0 siblings, 16 replies; 17+ messages in thread
From: fredrik.hederstierna@securitas-direct.com @ 2010-11-15 14:53 UTC (permalink / raw)
  To: gcc-bugs

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.


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2012-07-02 10:38 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-15 14:53 [Bug c/46483] New: Built-in memcpy() does not handle unaligned int for ARM fredrik.hederstierna@securitas-direct.com
2010-11-15 15:03 ` [Bug c/46483] " fredrik.hederstierna@securitas-direct.com
2010-11-15 15:12 ` [Bug target/46483] " rguenth at gcc dot gnu.org
2010-11-15 21:06 ` mikpe at it dot uu.se
2010-11-15 21:30 ` mikpe at it dot uu.se
2010-11-15 21:33 ` mikpe at it dot uu.se
2010-11-15 21:41 ` pinskia at gcc dot gnu.org
2010-11-15 23:04 ` rguenth at gcc dot gnu.org
2010-11-16 10:36 ` mikpe at it dot uu.se
2010-11-16 12:32 ` mikpe at it dot uu.se
2010-11-16 13:18 ` mikpe at it dot uu.se
2010-11-16 13:44 ` mikpe at it dot uu.se
2010-11-16 19:28 ` mikpe at it dot uu.se
2010-12-03 10:02 ` ramana at gcc dot gnu.org
2011-12-10 23:04 ` [Bug middle-end/46483] [4.4/4.5 regression] built-in memcpy() does not handle unaligned int ebotcazou at gcc dot gnu.org
2012-06-20 12:42 ` [Bug middle-end/46483] [4.5 " rguenth at gcc dot gnu.org
2012-07-02 10:38 ` rguenth at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).