简体   繁体   English

使用GNU objcopy从Elf创建可执行二进制文件

[英]Make Executable Binary File From Elf Using GNU objcopy

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? 有没有办法保留文件的可执行性?

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). 要由execve(2)系统调用执行,文件通常必须是某个elf(5)文件(或某些脚本,或某些旧的a.out格式)。 But see also binfmt_misc 但另请参阅binfmt_misc

Your objcopy(1) command is loosing the essential meta-data in the ELF file. 你的objcopy(1)命令正在丢失ELF文件中的基本元数据。 Maybe you want strip(1) 也许你想要脱衣舞(1)

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 ... 回想一下, ELF是一个相当复杂的和灵活的格式时,它指定的起始地址,该解释器( LD-Linux的(8)动态连接器),该程序等的若干段所有这些元数据是由内核为所需execve(2)并且使用objcopy -O binary丢失...

When you use objcopy -O binary , you are copying only the binary data: 当您使用objcopy -O二进制文件时 ,您只复制二进制数据:

objcopy can be used to generate a raw binary file by using an output target of `binary' (eg, use -O binary). objcopy可以用于通过使用`binary'的输出目标生成原始二进制文件(例如,使用-O binary)。 When objcopy generates a raw binary file, it will essentially produce a memory dump of the contents of the input object file. 当objcopy生成原始二进制文件时,它实际上会产生输入对象文件内容的内存转储。 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. 内存转储将从复制到输出文件中的最低部分的加载地址开始。

In particular you lose the entry point and the segments list given in the original ELF header. 特别是你丢失了原始ELF标题中给出的入口点和段列表。 The kernel cannot guess them. 内核无法猜测它们。

I don't understand why you expect the result of objcopy -O binary to be executable by Linux using execve(2) . 我不明白为什么你期望objcopy -O binary文件的结果可以由Linux使用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 (eg 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). objcopy -O binary命令的主要目的是制作固件或类似内核的独立(或独立)二进制文件,然后你需要准确理解它们应该是什么样子(例如它们的起点是什么,它们是怎样的并且您可能还使用了一些非常具体的链接器脚本 ,当然二进制文件不能包含任何到Linux内核的 系统调用 (特别是不能像普通的Linux可执行文件那样进行任何类型的输入或输出)。

Read also more about ABIs , the x86-64 ABI , the Linux Assembly HowTo , the Advanced Linux Programming book. 另请阅读有关ABIx86-64 ABILinux Assembly HowTo高级Linux编程书籍的更多信息。

You probably should read a good OS textbook like Operating System: Three Easy Pieces . 您可能应该阅读一本好的操作系统教科书,如操作系统:三个简单的片断

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用十六进制编辑器在 Linux 中制作可执行的 ELF 文件? - How to make an executable ELF file in Linux using a hex editor? objcopy --writable-text不能使elf二进制文本节可写吗? - Objcopy --writable-text not making elf binary text section writable? 从 ELF 可执行文件中找到一个函数 - Locate a function from ELF executable file 如何让 GDB 将我手工制作的可运行 ELF 文件识别为可执行文件? - How to make GDB recognize my handmade runnable ELF file as an executable? 如何从 elf 文件格式创建可执行的十六进制 - How to create a executable hex from elf file format ELF可执行文件的起点? - starting point of ELF executable file? file(1) 实用程序如何识别 ELF 共享 object 和 ELF 可执行文件? - How does file(1) utility discern from an ELF shared object and an ELF executable? 是否可以检查ELF可执行文件的“GNU_HASH”部分中的哈希值? - Is it possible to check the hash in the “GNU_HASH” section of an ELF executable? 在 ubuntu 上使用 gnu cobol 编译 windows (.exe) 可执行文件 - Compiling a windows (.exe) executable file using gnu cobol on ubuntu 可以在Mac上使用gnu utils如gobjdump解析器Linux ELF二进制文件吗? - Can gnu utils on Mac such as gobjdump parser Linux ELF binary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM