GCC Not finding builtin memcpy

I am doing work on a ARM cortex-m MCU using the arm-none-eabi version of gcc. I am also using -fnostdlib and -fnostdin.

In my code I am using memcpy and strlen. Both of these functions are builtin function as per the gcc manual. When I use these function as is or as __buitin_..., I get undefined reference to ....

Why is gcc not generating the code as expected?

1 answer

  • answered 2021-01-13 15:51 0___________

    builtins are not real functions. Compiler is free to replace them with the "normal" function call. ARM b as in this example:

    void *m(void *a, void *b, size_t size)
    {
        return __builtin_memcpy(a,b,size);
    }
    
    void *m1(void *a, void *b)
    {
        return __builtin_memcpy(a,b,16);
    }
    
    void *m2(void *a, void *b)
    {
        return __builtin_memcpy(a,b,200);
    }
    
    
    volatile int a[1000],b[10000], c[1000];
    
    
    int main(void)
    {
        m((void *)a,(void *)b,16);
        __asm(":::m");
        m((void *)a,(void *)c,400);
    }
    

    The resulting code will depend on the ARM architecture (if not aligned accesses are legal).

    CORTEX-M4                                            CORTEX-M0
    m:                                                   m:
      b memcpy                                                   push    {r4, lr}
    m1:                                                          bl      memcpy
      push {r4, r5}                                              pop     {r4, pc}
      ldr r5, [r1] @ unaligned                           m1:
      ldr r4, [r1, #4] @ unaligned                               push    {r4, lr}
      ldr r2, [r1, #8] @ unaligned                               movs    r2, #16
      ldr r1, [r1, #12] @ unaligned                              bl      memcpy
      str r1, [r0, #12] @ unaligned                              pop     {r4, pc}
      str r5, [r0] @ unaligned                           m2:
      str r4, [r0, #4] @ unaligned                               push    {r4, lr}
      str r2, [r0, #8] @ unaligned                               movs    r2, #200
      pop {r4, r5}                                               bl      memcpy
      bx lr                                                      pop     {r4, pc}
    m2:                                                  main:
      movs r2, #200                                              ldr     r0, .L6
      b memcpy                                                   push    {r4, r5, r6, lr}
    main:                                                        movs    r2, r0
      push {r4, lr}                                              ldr     r3, .L6+4
      ldr r3, .L8                                                ldmia   r3!, {r1, r4, r5}
      ldr r4, .L8+4                                              stmia   r2!, {r1, r4, r5}
      ldm r3, {r0, r1, r2, r3}                                   ldr     r3, [r3]
      stm r4, {r0, r1, r2, r3}                                   str     r3, [r2]
      :::m                                                       :::m
      mov r2, #400                                               movs    r2, #200
      mov r0, r4                                                 ldr     r1, .L6+8
      ldr r1, .L8+8                                              lsls    r2, r2, #1
      bl memcpy                                                  bl      memcpy
      movs r0, #0                                                movs    r0, #0
      pop {r4, pc}                                               pop     {r4, r5, r6, pc}
    .L8:                                                 .L6:
      .word b                                                    .word   a
      .word a                                                    .word   b
      .word c                                                    .word   c
      
    

    https://godbolt.org/z/fh68cv