我最近了解了strace,并在下面的简单c程序中对其进行了尝试。手册页指出mmap用于“将文件或设备映射或取消映射到内存”。为什么这个简单的程序需要这样做?是在内部为程序设置调用的吗?在哪里?
[human@attu4 hellok]$ cat nothing.c
#include <stdio.h>
#include <sys/types.h>
int
main(int argc, char *argv[])
{
return 0;
}
[human@attu4 hellok]$ gcc nothing.c
[human@attu4 hellok]$ strace ./a.out
execve("./a.out", ["./a.out"], [/* 43 vars */]) = 0
brk(NULL) = 0x25d9000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f320bd66000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/opt/rh/rh-ruby25/root/usr/lib64/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/rh-ruby25/root/usr/lib64/tls/x86_64", 0x7ffc0a359a30) = -1 ENOENT (No such file or directory)
open("/opt/rh/rh-ruby25/root/usr/lib64/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/rh-ruby25/root/usr/lib64/tls", {st_mode=S_IFDIR|0555, st_size=6, ...}) = 0
open("/opt/rh/rh-ruby25/root/usr/lib64/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/rh-ruby25/root/usr/lib64/x86_64", 0x7ffc0a359a30) = -1 ENOENT (No such file or directory)
open("/opt/rh/rh-ruby25/root/usr/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/rh-ruby25/root/usr/lib64", {st_mode=S_IFDIR|0555, st_size=4096, ...}) = 0
open("/opt/rh/devtoolset-7/root/usr/lib64/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/devtoolset-7/root/usr/lib64/tls/x86_64", 0x7ffc0a359a30) = -1 ENOENT (No such file or directory)
open("/opt/rh/devtoolset-7/root/usr/lib64/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/devtoolset-7/root/usr/lib64/tls", {st_mode=S_IFDIR|0555, st_size=6, ...}) = 0
open("/opt/rh/devtoolset-7/root/usr/lib64/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/devtoolset-7/root/usr/lib64/x86_64", 0x7ffc0a359a30) = -1 ENOENT (No such file or directory)
open("/opt/rh/devtoolset-7/root/usr/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/devtoolset-7/root/usr/lib64", {st_mode=S_IFDIR|0555, st_size=4096, ...}) = 0
open("/opt/rh/devtoolset-7/root/usr/lib/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/devtoolset-7/root/usr/lib/tls/x86_64", 0x7ffc0a359a30) = -1 ENOENT (No such file or directory)
open("/opt/rh/devtoolset-7/root/usr/lib/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/devtoolset-7/root/usr/lib/tls", 0x7ffc0a359a30) = -1 ENOENT (No such file or directory)
open("/opt/rh/devtoolset-7/root/usr/lib/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/devtoolset-7/root/usr/lib/x86_64", 0x7ffc0a359a30) = -1 ENOENT (No such file or directory)
open("/opt/rh/devtoolset-7/root/usr/lib/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/opt/rh/devtoolset-7/root/usr/lib", {st_mode=S_IFDIR|0555, st_size=16, ...}) = 0
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=138206, ...}) = 0
mmap(NULL, 138206, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f320bd44000
close(3) = 0
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20&\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2156160, ...}) = 0
mmap(NULL, 3985888, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f320b778000
mprotect(0x7f320b93b000, 2097152, PROT_NONE) = 0
mmap(0x7f320bb3b000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c3000) = 0x7f320bb3b000
mmap(0x7f320bb41000, 16864, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f320bb41000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f320bd43000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f320bd41000
arch_prctl(ARCH_SET_FS, 0x7f320bd41740) = 0
mprotect(0x7f320bb3b000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ) = 0
mprotect(0x7f320bd67000, 4096, PROT_READ) = 0
munmap(0x7f320bd44000, 138206) = 0
exit_group(0) = ?
+++ exited with 0 +++
我研究的另一件事是这个堆栈溢出问题-Where do I find the source code for execve()? 我看不到在这里提供答案的任何来源中都直接调用mmap。