Skip to the content.

exec

说实话,都是 快速的阅读 程序员的自我修养 ,现在对于 elf 格式各种的恐惧,但是静态链接怕个屁。

我想知道的问题 :

  1. linux_binprm : 执行 exec 的时候,包含二进制文件的各种属性, 使用 alloc_bprm 分配
  2. linux_binfmt : 提供给不同格式的标准接口
/*
 * Create a new mm_struct and populate it with a temporary stack
 * vm_area_struct.  We don't have enough context at this point to set the stack
 * flags, permissions, and offset, so we use temporary values.  We'll update
 * them later in setup_arg_pages().
 */
static int bprm_mm_init(struct linux_binprm *bprm)
static struct linux_binfmt elf_format = {
	.module		= THIS_MODULE,
	.load_binary	= load_elf_binary,
	.load_shlib	= load_elf_library,
	.core_dump	= elf_core_dump,
	.min_coredump	= ELF_EXEC_PAGESIZE,
};

syscalls

存在 kexec ,暂时并不关心:

270 n64 kexec_load sys_kexec_load 320 common kexec_file_load sys_kexec_file_load 57 n64 execve sys_execve 316 n64 execveat sys_execveat

SYSCALL_DEFINE3(execve,
		const char __user *, filename,
		const char __user *const __user *, argv,
		const char __user *const __user *, envp)
{
	return do_execve(getname(filename), argv, envp);
}

SYSCALL_DEFINE5(execveat,
		int, fd, const char __user *, filename,
		const char __user *const __user *, argv,
		const char __user *const __user *, envp,
		int, flags)
{
	int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;

	return do_execveat(fd,
			   getname_flags(filename, lookup_flags, NULL),
			   argv, envp, flags);
}

两个系统调用没有什么区别,只是搜索方式即将执行的文件方式不同而已.

posix 对于这个提供了一些列的封装: 提供了:

  1. l : 参数使用变长数组提供数组
  2. v : 参数使用数组的的形式提供
  3. e : 可以通过参数执行 env
  4. p : 指定执行文件的搜索路径

close on exec

O_CLOEXEC can be set by fnctl, and file is closed by do_close_on_exec

在进行 exec 的时候,会创建新的地址空间出来,exec_mmap 调用 exec_mm_release 来, 并且 mmput 之前的 mm_struct 减少引用计数 - bprm_mm_init : 创建 mm_struct - exec_mmap : 切换新的 mm_struct

本站所有文章转发 CSDN 将按侵权追究法律责任,其它情况随意。