Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell gcc to stop using built-in functions?

I am using my own modified glibc. I saw in the compiled code that compiler was not using many standard library functions from my glibc when I linked with it. Then I put -fno-builtin flag. Things got better and I could see that many functions which were not taken from glibc were now taken from there, such as malloc.

However, still for many functions, such as mmap, the compiler is using some built-in-code. Now how can I ask the compiler to please exclusively use the code from glibc rather than using its built-in-functions?

On my x86-64 function, if I do objdump of the compiled glibc, following is the generated mmap function. I can't find equivalent code in the glibc source.

0000000000000000 <__mmap>:
   0:   49 89 ca                mov    %rcx,%r10
   3:   b8 09 00 00 00          mov    $0x9,%eax
   8:   0f 05                   syscall 
   a:   48 3d 01 f0 ff ff       cmp    $0xfffffffffffff001,%rax
  10:   0f 83 00 00 00 00       jae    16 <__mmap+0x16>
  16:   c3                      retq  
like image 823
MetallicPriest Avatar asked May 21 '12 14:05

MetallicPriest


People also ask

What are GCC builtin functions?

GCC provides built-in versions of the ISO C99 floating-point comparison macros that avoid raising exceptions for unordered operands. They have the same names as the standard macros ( isgreater , isgreaterequal , isless , islessequal , islessgreater , and isunordered ) , with __builtin_ prefixed.

What is __ Builtin_clz?

__builtin_clz(x): This function is used to count the leading zeros of the integer. Note : clz = count leading zero's. Example: It counts number of zeros before the first occurrence of one(set bit).

What is __ Builtin_memcpy?

on a compiler note, __builtin_memcpy can fall back to emitting a memcpy function call. also less-capable compilers the ability to simplify, by choosing the slow path of unconditionally emitting a memcpy call. http://lwn.net/Articles/29183/ Follow this answer to receive notifications.


2 Answers

The wrapper you disassemble above comes from the INLINE_SYSCALL macro in sysdeps/unix/sysv/linux/x86_64/sysdep.h. This macro is the 'magic glue' used to turn a normal function call into a system call.

As part of the build process of glibc, for every defined system call foo that is not in a list of special exceptions for that architecture, it generates a function __foo that contains just a single INLINE_SYSCALL macro invocation. mmap is not in the exception list for x86_64 (in sysdeps/unix/sysv/linux/x86_64/syscalls.list), so it gets the generic treatment.

like image 118
Chris Dodd Avatar answered Nov 10 '22 13:11

Chris Dodd


How about passing "-ffreestanding" to gcc as explained in this answer?

like image 23
ZeZNiQ Avatar answered Nov 10 '22 11:11

ZeZNiQ