使用GNU objcopy从Elf生成可执行二进制文件 [英] Make Executable Binary File From Elf Using GNU objcopy

查看:234
本文介绍了使用GNU objcopy从Elf生成可执行二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过以下方式复制可执行的ELF文件:

I'd like to copy an executable ELF file via:

$ objcopy -O binary myfile.elf myfile.bin

不幸的是:

$ chmod +x myfile.bin
$ ./myfile.bin

导致:无法执行二进制文件

results in: cannot execute binary file

有什么办法可以保持文件的可执行性?

Is there any way to retain the files executability?

推荐答案

execve(2)系统调用,文件通常必须是某些 elf(5)文件(或某些脚本,或某些旧的 a.out 格式).但另请参见 binfmt_misc

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

您的 objcopy(1)命令丢失了ELF文件中的基本元数据.也许您想要 strip(1)

Your objcopy(1) command is loosing the essential meta-data in the ELF file. Maybe you want strip(1)

回想 ELF 是一种非常复杂且用途广泛的格式,它指定了起始地址,解释器( ld-linux(8)动态链接器),程序的几个部分等.内核 execve(2)需要所有这些元数据,而 objcopy -O binary 会丢失所有这些元数据.

Recall that ELF is 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 ...

使用 objcopy -O二进制时,您仅复制了二进制数据:

When you use objcopy -O binary, you are copying only the binary data:

可以使用

objcopy来生成原始二进制文件,方法是使用`binary'的输出目标(例如,使用-O binary).当objcopy生成原始二进制文件时,它将实质上生成输入目标文件内容的内存转储.所有符号和重定位信息都将被丢弃.内存转储将从复制到输出文件中最低部分的加载地址开始.

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.

尤其是您丢失了原始ELF标头中给出的入口点和细分列表.内核无法猜测它们.

In particular you lose the entry point and the segments list given in the original ELF header. The kernel cannot guess them.

我不明白为什么您期望 objcopy -O binary 的结果可以由Linux使用 execve(2)执行.该 objcopy -O二进制命令的主要目的是使固件或类似内核的独立(或独立)二进制文件,然后您需要准确了解它们的外观(例如,它们的起点,如何加载和启动),并且还可能使用一些非常具体的<一个href ="https://sourceware.org/binutils/docs/ld/Scripts.html" rel ="nofollow noreferrer">链接脚本,当然二进制文件不能包含任何

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 firmware or 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 syscall to the linux kernel (in particular cannot do any kind of input or output the way a plain Linux executable does them).

还阅读有关 ABI 的更多信息.-beta.archive.org/web/20120918205429/http://www.x86-64.org/documentation/abi.pdf"rel =" nofollow noreferrer> x86-64 ABI , Linux汇编方法

Read also more about ABIs, the x86-64 ABI, the Linux Assembly HowTo, the Advanced Linux Programming book.

您可能应该阅读一本优秀的OS教科书,例如操作系统:三篇简单文章.

You probably should read a good OS textbook like Operating System: Three Easy Pieces.

这篇关于使用GNU objcopy从Elf生成可执行二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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