内核如何获取在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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:40:36  来源:igfitidea点击:

How does kernel get an executable binary file running under linux?

clinux

提问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

After reading the ELF docsalready referenced, you should just read the kernel codethat actually does it.

阅读已经引用的ELF 文档后,您应该阅读实际执行此操作的内核代码

If you have trouble understanding that code, build a UML Linux, and you could step through that code in the debugger.

如果您无法理解该代码,请构建一个UML Linux,然后您可以在调试器中逐步执行该代码。

回答by Basile Starynkevitch

Two system callsfrom the linux kernelare relevant. The forksystem call (or perhaps vforkor clone) is used to create a new process, similar to the calling one (every Linux user-land process except initis created by forkor 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透露详情。vforkcloneinitfork

The dynamic linking happens after execveand involves the /lib/x86_64-linux-gnu/ld-2.13.sofile, which for ELF is viewed as an "interpreter".

动态链接发生在文件之后execve并涉及/lib/x86_64-linux-gnu/ld-2.13.so文件,对于 ELF 来说,它被视为“解释器”。