http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59219
Bug ID: 59219 Summary: ____builtin___memcpy_chk and -fno-builtin-memcpy Product: gcc Version: 4.8.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gmail dot com The __builtin___xxx_chk intrinsics are useful in detecting buffer overflows via the Object Size Checking feature. But in a freestanding/embeeded environment with its own implementation of function xxx (such as memcpy), the __builtin___xxx_chk intrinsics cannot be used even with the -ffreestanding or -fno-builtin option because they result in the inline expansion of the related xxx function irrespective of the option (see the test program below). To get the benefit of Object Size Checking in these environments, it's necessary to hand-code __builtin___xxx_chk instead. It would simplify the adoption of Object Size Checking in these environments if instead of expanding xxx inline when -fno-builtin is specified, GCC emitted a call to xxx. (As a side note, this happens to be the behavior of the Intel compiler.) $ cat v.c && gcc -O2 -c -fno-builtin -std=c11 v.c && objdump -d v.o | sed -n "/<foo>:/,/^$/p" typedef __typeof__ (sizeof 0) size_t; extern inline __attribute__ ((always_inline, artificial)) void* memcpy (void* restrict d, const void* restrict s, size_t n) { return __builtin___memcpy_chk (d, s, n, __builtin_object_size (d, 1)); } char b [4]; void foo (const void *p) { memcpy (b, p, sizeof b); } 0000000000000010 <foo>: 10: 8b 07 mov (%rdi),%eax 12: 89 05 00 00 00 00 mov %eax,0(%rip) # 18 <foo+0x8> 18: c3 retq