8

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  
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356

3 Answers3

6

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.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
2

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

ZeZNiQ
  • 612
  • 7
  • 14
  • I'm trying `-ffreestanding` and it still seems to use a lot of builtins. For instance, `__mulsi3` or other calls on really low-end systems. – Charles Lohr Jan 16 '23 at 21:09
0

This is what a call to mmap compiles to on my system:

movl    $1048837, 20(%esp)
movl    $1048836, 16(%esp)
movl    $1048835, 12(%esp)
movl    $1048834, 8(%esp)
movl    $1048833, 4(%esp)
movl    $1048832, (%esp)
call    mmap

I have used some constants like 0x100100 (1048832) as mmap arguments in order to be able to find corresponding instructions in the assembly easier.

This looks like pretty ordinary call to a pretty ordinary library function, and not a builtin.

The actual code for mmap is OS- and architecture-dependent. For instance, for i386 Linux it is in sysdeps/unix/sysv/linux/i386/mmap.S (yes it's written in assembly).

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243