Is C – mmap a built-in function?

mmap a built-in function?… here is a solution to the problem.

mmap a built-in function?

I know mmap is a system call, but there must be some wrapper in glibc to perform the system call. However, when I try to step through the mmap function in my program using gdb, gdb ignores it because it can’t find any source files for this (note I compiled my own glibc from source). I can step into other glibc library functions like printf and malloc but not mmap. I also use the flag –fno-builtin so that gcc doesn’t use built-in functions. Any help with this would be appreciated.

Solution

I don’t know what your problem is. It works very well for me.

Using the system libc.so.6, install the debug symbol:

// mmap.c
#include <sys/mman.h>

int main()
{
  void *p = mmap(0, 4096, PROT_READ, MAP_PRIVATE| MAP_ANONYMOUS, -1, 0);
  return 0;
}

gcc -g mmap.c

$ gdb -q a.out
Reading symbols from /tmp/a.out... done.
(gdb) start
Temporary breakpoint 1 at 0x40052c: file mmap.c, line 5.

Temporary breakpoint 1, main () at mmap.c:5
5         void *p = mmap(0, 4096, PROT_READ, MAP_PRIVATE| MAP_ANONYMOUS, -1, 0);
(gdb) step
mmap64 () at .. /sysdeps/unix/syscall-template. S:82
82      .. /sysdeps/unix/syscall-template. S: No such file or directory.
(gdb) 
mmap64 () at .. /sysdeps/unix/syscall-template. S:83
83      in .. /sysdeps/unix/syscall-template. S
(gdb) 
main () at mmap.c:6
6         return 0;
(gdb) q

Build with my own glibc

:

gdb -q a.out
Reading symbols from /tmp/a.out... done.
(gdb) start
Temporary breakpoint 1 at 0x40056c: file mmap.c, line 5.
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?

Temporary breakpoint 1, main () at mmap.c:5
5         void *p = mmap(0, 4096, PROT_READ, MAP_PRIVATE| MAP_ANONYMOUS, -1, 0);
(gdb) step
mmap64 () at .. /sysdeps/unix/syscall-template. S:81
81      T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
(gdb) 
mmap64 () at .. /sysdeps/unix/syscall-template. S:82
82              ret
(gdb) 
main () at mmap.c:6
6         return 0;
(gdb) q

Related Problems and Solutions