简体   繁体   English

函数execve的实现(unistd.h)

[英]Implementation of function execve (unistd.h)

How can I see the implementation of function execve (under x86_64 Linux), it is in the library unistd? 我怎样才能看到函数execve的实现(在x86_64 Linux下),它在库中是unistd吗? I want this because I want to know how can I call an external program using assembler, without calling execve. 我想要这个,因为我想知道如何使用汇编程序调用外部程序,而不需要调用execve。 I know that there is a syscall named execve, but I don't know how can I use it. 我知道有一个名为execve的系统调用,但我不知道如何使用它。

How can I put a variable of type char * and type char * [] into registers ? 如何将char *类型的变量和char * []类型放入寄存器?

The implementation of the execve() function in userspace looks something like: 在用户空间中执行execve()函数看起来像:

int execve(const char *filename, char * const argv[], char * const envp[]) {
    return syscall(SYS_execve, filename, argv, envp);
}

All of the actual "work" is done in the kernel. 所有实际的“工作”都在内核中完成。 There's nothing particularly interesting happening in libc, besides perhaps some threading cleanup. 在libc中没有什么特别有趣的事情发生,除了一些线程清理之外。

Just take a look at the kernel sources (more specifically: arch/YOUR-ARCH/kernel/head*.S) for the system call convention on your architecture (registers and/or stack for the syscall number and the parameters). 只需查看内核源代码(更具体地说:arch / YOUR-ARCH / kernel / head * .S),了解架构上的系统调用约定(寄存器和/或堆栈中的系统调用号和参数)。

On ARM, for example, you would load __NR_execve into r7, load the arguments into r0, r1, r2 and then use swi 0 . 例如,在ARM上,您可以将__NR_execve加载到r7中,将参数加载到r0,r1,r2中,然后使用swi 0 You might be interested in this explantion of ARM EABI syscalls for more details. 您可能对ARM EABI系统调用的这种解释感兴趣了解更多详细信息。

There is no real straightforward implementation of system calls in the source code to glibc - this is generated at build time from various files defining the system call numbers. 源代码中没有真正简单的系统调用实现到glibc - 这是在构建时从定义系统调用号的各种文件生成的。

The relevant information can be found in sysdep.h if you understand it, except for the actual system call numbers (you want __NR_execve with, IIRC, #include <asm/unistd.h> - I can't recall offhand what it is on x86_64). 如果您了解相关信息,可以在sysdep.h中找到,除了实际的系统调用号码(您希望__NR_execve with,IIRC, #include <asm/unistd.h> - 我无法回想起它是什么x86_64的)。

The system call number goes in %rax, and the arguments go in %rdi %rsi %rdx. 系统调用号在%rax中,参数在%rdi%rsi%rdx中。 All this information (including stack alignment and something about register usage by the kernel) is commented in sysdep.h. 所有这些信息(包括堆栈对齐和内核的寄存器使用情况)都在sysdep.h中进行了注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM