Linux 使用 GNU objcopy 从 Elf 制作可执行的二进制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19944441/
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
Make Executable Binary File From Elf Using GNU objcopy
提问by user2985888
I'd like to copy an executable ELF file via:
我想通过以下方式复制一个可执行的 ELF 文件:
$ objcopy -O binary myfile.elf myfile.bin
Unfortunately:
很遗憾:
$ chmod +x myfile.bin
$ ./myfile.bin
results in: cannot execute binary file
结果:无法执行二进制文件
Is there any way to retain the files executability?
有没有办法保留文件的可执行性?
采纳答案by Basile Starynkevitch
To be executable by the execve(2)syscall, a file usually has to be some elf(5)file (or some script, or some old a.out
format). But see also binfmt_misc
要由execve(2)系统调用执行,文件通常必须是一些elf(5)文件(或一些脚本,或一些旧a.out
格式)。但另见binfmt_misc
Your objcopy(1)command is loosing the essential meta-data in the ELF file. Maybe you want strip(1)
您的objcopy(1)命令正在丢失 ELF 文件中的基本元数据。也许你想要条(1)
Recall that ELFis quite a complex and versatile format, it specifies the starting address, the interpreter (ld-linux(8)dynamic linker), the several segments of the program etc. All this meta-data is needed by the kernel for execve(2)
and is lost with objcopy -O binary
...
回想一下,ELF是一种相当复杂和通用的格式,它指定了起始地址、解释器(ld-linux(8)动态链接器)、程序的几个段等。内核需要所有这些元数据来执行execve(2)
和失去了objcopy -O binary
...
When you use objcopy -O binary, you are copying only the binary data:
当您使用objcopy -O binary 时,您只复制二进制数据:
objcopy can be used to generate a raw binary file by using an output target of `binary' (e.g., use -O binary). When objcopy generates a raw binary file, it will essentially produce a memory dump of the contents of the input object file. All symbols and relocation information will be discarded. The memory dump will start at the load address of the lowest section copied into the output file.
objcopy 可用于通过使用`binary' 的输出目标(例如,使用-O binary)来生成原始二进制文件。当 objcopy 生成原始二进制文件时,它本质上会生成输入目标文件内容的内存转储。所有符号和重定位信息都将被丢弃。内存转储将从复制到输出文件的最低部分的加载地址开始。
In particular you lose the entry point and the segments list given in the original ELF header. The kernel cannot guess them.
特别是您丢失了原始 ELF 标头中给出的入口点和段列表。内核无法猜测它们。
I don't understand why you expect the result of objcopy -O binary
to be executable by Linux using execve(2)
. The main purpose of that objcopy -O binary
command is to make firmwareor kernel-like stand-alone (or freestanding) binaries, and then you need to exactly understand how they should look like (e.g. what is their starting point, how they are loaded and started) and you probably also use some very specific linker script, and of course that binary cannot contain any syscallto the linux kernel(in particular cannot do any kind of input or output the way a plain Linux executable does them).
我不明白为什么您希望objcopy -O binary
Linux 使用execve(2)
. 该objcopy -O binary
命令的主要目的是制作固件或类似内核的独立(或独立)二进制文件,然后您需要准确了解它们的外观(例如它们的起点是什么,它们如何加载和启动)并且您可能还使用了一些非常具体的链接器脚本,当然,该二进制文件不能包含对linux 内核的任何系统调用(特别是不能像普通的 Linux 可执行文件那样进行任何类型的输入或输出)。
Read also more about ABIs, the x86-64 ABI, the Linux Assembly HowTo, the Advanced Linux Programmingbook.
另请阅读有关ABI、x86-64 ABI、Linux Assembly HowTo、Advanced Linux Programming书籍的更多信息。
You probably should read a good OS textbook like Operating System: Three Easy Pieces.
你可能应该阅读一本好的操作系统教科书,比如操作系统:三件简单的作品。