I wanted to see how the Linux kernel function mmap()
is implemented, so I downloaded the GNU C Library (glibc
) source from the GNU page. (I downloaded glibc-2.27
because ldd --version
told me I was using GLIBC 2.27
)
Now to find the definition of mmap()
I did grep -r "mmap(void" *
which returned nothing, so I tried grep -r "mmap (void" *
which returned the following:
conform/data/sys/mman.h-data:function {void*} mmap (void*, size_t, int, int, int, off_t)
include/sys/mman.h:extern void *__mmap (void *__addr, size_t __len, int __prot,
malloc/memusage.c:mmap (void *start, size_t len, int prot, int flags, int fd, off_t offset)
manual/llio.texi:@deftypefun {void *} mmap (void *@var{address}, size_t @var{length}, int @var{protect}, int @var{flags}, int @var{filedes}, off_t @var{offset})
misc/sys/mman.h:extern void *mmap (void *__addr, size_t __len, int __prot,
misc/mmap.c:__mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
support/xunistd.h:void *xmmap (void *addr, size_t length, int prot, int flags, int fd);
support/xmmap.c:xmmap (void *addr, size_t length, int prot, int flags, int fd)
sysdeps/unix/sysv/linux/mmap.c:__mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
sysdeps/mach/hurd/dl-sysdep.c:__mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
sysdeps/mach/hurd/mmap.c:__mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
Of all the results that were about mmap()
and not about __mmap()
I found that the definition of mmap()
is in malloc/memusage.c
which defined mmap()
as the following:
/* `mmap' replacement. We do not have to keep track of the size since
`munmap' will get it as a parameter. */
void *
mmap (void *start, size_t len, int prot, int flags, int fd, off_t offset)
{
void *result = NULL;
/* Determine real implementation if not already happened. */
if (__glibc_unlikely (initialized <= 0))
{
if (initialized == -1)
return NULL;
me ();
}
/* Always get a block. We don't need extra memory. */
result = (*mmapp)(start, len, prot, flags, fd, offset);
...
/* Return the pointer to the user buffer. */
return result;
}
I figured result = (*mmapp)(start, len, prot, flags, fd, offset);
is what matters, and in this file there are two other parts that deal with this mmapp
function pointer, which are:
- declaration:
static void *(*mmapp) (void *, size_t, int, int, int, off_t);
- initialization in some function called
me()
:mmapp = (void *(*)(void *, size_t, int, int, int, off_t))dlsym (RTLD_NEXT, "mmap");
Thedlsym()
function, according to the manual, takes a "handle" of a dynamic library returned by dlopen() and the null-terminated symbol name, returning the address where that symbol is loaded into memory.
Thus, the whole process can be summarized as follows:
mmap()
calls a function pointed to by the pointermmapp
mmapp
is set to point to the symbol"mmap"
in a dynamic library loaded into memory
But I can't find any information on dynamic library with the symbol "mmap"
.
Am I doing something wrong in the process of code analysis? I haven't much experience in code analysis, let alone looking into system call functions or kernel codes, so any advice or push in the right direction would be greatly appreciated.
Thanks in advance!