Linux如何执行文件? [英] How does Linux execute a file?

查看:112
本文介绍了Linux如何执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Linux操作系统如何执行文件. 因此,通过我在网络上的搜索,我了解到每个可运行位都设置为打开的文件都可以执行.但是后来我了解到有一种称为格式的ELF,这是可执行文件的Linux标准.

I want to know how does Linux operating systems execute files. So from my searches across the web I understood that every file which has the runnable bit set on can be executed. But then I learned that there is an ELF called format which is the Linux standard for executables.

所以我想知道,对于具有运行权限(可运行位处于打开状态)的文件,为了在系统中执行代码有什么必要?我可以只使用十六进制编辑器创建一个新文件,然后在其中写入90(NOP操作码)并期望它被执行吗?还是Linux需要某种标准格式,例如ELF格式或Bash格式?

So what I want to know is what is necessary to a file which has permission to run (runnable bit is on), in order to execute code in the system? Can I just create a new file with hex editor and write 90 inside (NOP opcode) and expect it to be executed? Or does Linux requires some kind of standard format, like ELF format or Bash format?

推荐答案

还是Linux需要某种标准格式,例如ELF格式或bash格式?

Or does linux requires some kind of standard format, like ELF format or bash format?

是的,Linux要求文件采用某种受支持的(注册的)格式并执行位设置才能执行. Linux中的大多数文件都具有 ELF格式 "shebang"格式 (其中两个前两个符号分别为#!和然后编写解释器的路径,供bash,perl,python和大多数其他脚本使用.有时允许文本文件作为shell脚本执行,例如当您从bash中执行./script时(不是由内核处理,而是由bash shell处理).

Yes, linux requires file to be in some supported (registered) format and execute bit set in order to execute it. Most files in Linux has either ELF format, or "shebang" format (two first symbols of them are #! and then path to interpreter is written, used by bash, perl, python and most other scripts). Sometimes text files are allowed to execute as shell scripts, e.g. when you do ./script from bash (handled not by kernel, but by bash shell).

更多详细信息可从以下网站的 fs/exec.c文件中找到. linux内核,从do_execve函数开始.

More details are available in fs/exec.c file from linux kernel, beginning from do_execve function.

有内核子系统"binfmt"来注册其他可执行格式.例如, binfmt_misc 允许您定义和注册自己的二进制文件通过/proc/sys/fs/binfmt_misc特殊文件进行格式化.通过用户定义的解释器"处理执行,该程序可以读取,加载和执行目标可执行文件.例如,可以在wine非仿真器的帮助下启动Windows PE二进制文件.

There is kernel subsystem "binfmt" to register other executable formats. For example, binfmt_misc allows you to define and register own binary format via /proc/sys/fs/binfmt_misc special file. The execution is handled via user-defined "interpreter", the program which can read, load and execute target executable. For example, Windows PE binaries may be started with help of wine not-an-emulator.

我们可以在以下目录的 fs目录中看到几个内置的binfmt模块内核资源.最常见的是:binfmt_elf.c(ELF二进制格式)和binfmt_script.c(检测"shebang"并启动解释器).有一个简单的二进制格式"a.out" ; T,由binfmt_aout.c处理,比ELF更容易生成.

We can see several builtin binfmt modules in fs directory of kernel sources. Most common are: binfmt_elf.c (ELF binary format) and binfmt_script.c (which detects "shebang" and starts the interpreter). There is simple binary format "a.out" from AT&T, handled by binfmt_aout.c, which can be easier to generate than ELF.

binfmt_aout.c   11374 bytes
binfmt_elf.c    58415 bytes
binfmt_elf_fdpic.c  48256 bytes
binfmt_em86.c   2710 bytes
binfmt_flat.c   27054 bytes
binfmt_misc.c   15175 bytes
binfmt_script.c 2768 bytes
binfmt_som.c    7315 bytes

如果您尝试执行的文件不是受支持的格式,则exec* syscalls将返回错误:

If the file you try to execute is not of supported format, exec* syscalls will return error:

$ hexdump -C asd
00000000  07 01 09 00 11 12 13 14  0a                       |.........|
00000009
$ strace ./asd
execve("./asd", ["./asd"], [/* 179 vars */]) = -1 ENOEXEC (Exec format error)
....

根据 execve手册页,返回代码表示:

According to execve man page, the return code means:

ENOEXEC

可执行文件的格式不正确,结构错误或存在其他格式错误,表示无法执行.

An executable is not in a recognized format, is for the wrong architecture, or has some other format error that means it cannot be executed.

这篇关于Linux如何执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆