内核如何获取在linux下运行的可执行二进制文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8352535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does kernel get an executable binary file running under linux?
提问by Daniel
How does kernel get an executable binary file running under linux?
内核如何获取在linux下运行的可执行二进制文件?
It seems a simple question, but anyone can help me dig deep? How the file is loaded to memory and how execution code get started?
这似乎是一个简单的问题,但谁能帮我深入挖掘?如何将文件加载到内存以及如何开始执行代码?
Can anyone help me and tell what's happening step by step?
谁能帮我一步一步地告诉我发生了什么?
回答by TJD
You can start by understanding executable file formats, such as ELF. http://en.wikipedia.org/wiki/Executable_and_Linkable_Format
您可以从了解可执行文件格式开始,例如 ELF。 http://en.wikipedia.org/wiki/Executable_and_Linkable_Format
The ELF file contains several sections with headers that describes how and where parts of the binary should be loaded into memory.
ELF 文件包含几个带有标题的部分,这些部分描述了二进制文件的各个部分应如何以及在何处加载到内存中。
Then, I suggest reading up on the part of linux that loads binaries and handles dynamic linking, ld-linux. This is also a good description of ld-linux: http://www.cs.virginia.edu/~dww4s/articles/ld_linux.html
然后,我建议阅读加载二进制文件和处理动态链接的 linux 部分,ld-linux。这也是对ld-linux的一个很好的描述:http: //www.cs.virginia.edu/~dww4s/articles/ld_linux.html
回答by Employed Russian
回答by Basile Starynkevitch
Two system callsfrom the linux kernelare relevant. The forksystem call (or perhaps vfork
or clone
) is used to create a new process, similar to the calling one (every Linux user-land process except init
is created by fork
or friends). The execvesystem call replace the process address space by a fresh one (essentially by sort-of mmap-ing segments from the ELF executable and anonymous segments, then initializing the registers, including the stack pointer). The x86-64 ABI supplementand the Linux assembly howtogive details.
来自linux 内核的两个系统调用是相关的。该叉系统调用(或许或)用于创建新的进程,类似于调用一个(每个Linux用户空间的过程,除了通过创建或朋友)。所述的execve系统调用用新鲜(通过基本上排序的替换进程地址空间MMAP从ELF可执行和匿名段-ing段,然后初始化寄存器,包括堆栈指针)。该X86-64 ABI补充和Linux的装配HOWTO透露详情。vfork
clone
init
fork
The dynamic linking happens after execve
and involves the /lib/x86_64-linux-gnu/ld-2.13.so
file, which for ELF is viewed as an "interpreter".
动态链接发生在文件之后execve
并涉及/lib/x86_64-linux-gnu/ld-2.13.so
文件,对于 ELF 来说,它被视为“解释器”。