Bug 105830 - call to memcpy when -nostdlib -nodefaultlibs flags provided
Summary: call to memcpy when -nostdlib -nodefaultlibs flags provided
Status: RESOLVED DUPLICATE of bug 56888
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 12.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2022-06-03 06:52 UTC by AK
Modified: 2023-12-29 01:21 UTC (History)
2 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description AK 2022-06-03 06:52:35 UTC

// test.c

// Type your code here, or load an example.
/* Nonzero if either X or Y is not aligned on a "long" boundary.  */
#define UNALIGNED(X, Y) \
  (((unsigned long)X & (sizeof (unsigned long) - 1)) | ((unsigned long)Y & (sizeof (unsigned long) - 1)))

  #define UNALIGNED1(a) \
    ((unsigned long)(a) & (sizeof(unsigned long)-1))

/* How many bytes are copied each iteration of the 4X unrolled loop.  */
#define BIGBLOCKSIZE    (sizeof (unsigned long) * 4)

/* How many bytes are copied each iteration of the word copy loop.  */
#define LITTLEBLOCKSIZE (sizeof (unsigned long))

/* Threshhold for punting to the byte copier.  */

void * memcpy (void *__restrict dst0,
	const void *__restrict src0,
	unsigned long len0)
  unsigned char *dst = dst0;
  const unsigned char *src = src0;

  /* If the size is small, or either SRC or DST is unaligned,
     then punt into the byte copy loop.  This should be rare.  */
  if (len0 >= LITTLEBLOCKSIZE && !UNALIGNED (src, dst))
    unsigned long *aligned_dst;
    const unsigned long *aligned_src;
      aligned_dst = (unsigned long*)dst;
      aligned_src = (const unsigned long*)src;

      /* Copy one long word at a time if possible.  */

      /* Copy one long word at a time if possible.  */
          *aligned_dst++ = *aligned_src++;
          len0 -= LITTLEBLOCKSIZE;
        } while (len0 >= LITTLEBLOCKSIZE);

       /* Pick up any residual with a byte copier.  */
      dst = (unsigned char*)aligned_dst;
      src = (const unsigned char*)aligned_src;

  for (; len0; len0--)
    *dst++ = *src++;

  return dst0;

// ARM gcc trunk
gcc -O3 -nostdlib -nodefaultlibs -S -o -

        push    {r3, r4, r5, r6, r7, lr}
        cmp     r2, #3
        mov     r4, r2
        mov     r5, r0
        mov     r6, r1
        bls     .L5
        orr     r3, r0, r1
        lsls    r3, r3, #30
        beq     .L9
        mov     r2, r4
        mov     r1, r6
        bl      memcpy ; <------------- call to memcpy
        mov     r0, r5
        pop     {r3, r4, r5, r6, r7, pc}
        subs    r7, r2, #4
        and     r4, r2, #3
        bic     r7, r7, #3
        adds    r7, r7, #4
        mov     r2, r7
        add     r6, r6, r7
        bl      memcpy ; <------------- call to memcpy
        adds    r0, r5, r7
        cmp     r4, #0
        bne     .L3
        mov     r0, r5
        pop     {r3, r4, r5, r6, r7, pc}
Comment 1 Andrew Pinski 2022-06-03 06:55:58 UTC
Dup of bug 56888.

*** This bug has been marked as a duplicate of bug 56888 ***
Comment 2 Roger Sayle 2022-06-04 13:04:59 UTC
Hi AK,
The executive summary is that you need to specify -ffreestanding (which affects code generation) instead of -nostdlib and -nodefaultlibs (which affect linking).
With -ffreestanding (on your godbolt URL), there are no calls to memcpy.
Comment 3 AK 2022-06-04 22:51:51 UTC
with -ffreestanding the calls to memcpy did disappear. Thanks.