[英]Converting an ELF binary into an .o file
The question is as stated in the title: Is it possible to convert an ELF binary file hello_world
into an object file hello_world.o
, which can be used to generate a new binary hello_world_2
which is a replica of hello_world
?问题如标题所述:是否可以将 ELF 二进制文件
hello_world
转换为 object 文件hello_world.o
,该文件可用于生成新的二进制hello_world_2
,它是hello_world
的副本?
So from my searching, it seems that this is a bit difficult.所以从我的搜索来看,这似乎有点困难。 I have found one method that is the closest which is:
我找到了一种最接近的方法:
Using either objcopy
or ld
to create an object file from the binary.使用
objcopy
或ld
从二进制文件创建 object 文件。 An example command of this would be:一个示例命令是:
ld -r -b binary./hello_world -o hello_world.o
This command creates an object file consisting of sections something like此命令创建一个 object 文件,其中包含类似的部分
00000000 l d .data 00000000 .data
00000000 g .data 00000000 _binary_hello_world_start
00000010 g .data 00000000 _binary_hello_world_end
00000010 g *ABS* 00000000 _binary_hello_world_size
That can be accessed if you link this newly generated object file with a separate C file ( https://balau82.wordpress.com/2012/02/19/linking-a-binary-blob-with-gcc/ ). That can be accessed if you link this newly generated object file with a separate C file ( https://balau82.wordpress.com/2012/02/19/linking-a-binary-blob-with-gcc/ ). However, this is a bit different than what I wish to do since I need to create a separate code just to access this new object file.
但是,这与我希望做的有点不同,因为我需要创建一个单独的代码来访问这个新的 object 文件。
This: Make Executable Binary File From Elf Using GNU objcopy StackOverflow discussion provides a great explanation of a similar topic. This: Make Executable Binary File From Elf Using GNU objcopy StackOverflow 讨论为类似主题提供了很好的解释。 However, I'm still wondering if there was some sort of way to achieve my original question of:
但是,我仍然想知道是否有某种方法可以实现我最初的问题:
binary
--> binary.o
--> binary_new
binary
--> binary.o
--> binary_new
Side note: If anyone is curious why I am trying to do this, is because I am trying to add a.rodata section into the binary that I have no source code for (this is a whole another problem which is extensively discussed below).旁注:如果有人好奇我为什么要这样做,是因为我试图将 a.rodata 部分添加到我没有源代码的二进制文件中(这是另一个问题,下面将广泛讨论)。 This procedure is recommended to do with an object file because newly added section into the binary will be readable in the load-time.
建议使用 object 文件执行此过程,因为新添加到二进制文件中的部分将在加载时可读。
How can the --add-section switch of OBJCOPY be used? 如何使用 OBJCOPY 的 --add-section 开关?
How to replace a section of an elf file with another using objcopy or libelf, such that it does get loaded into the Memory? 如何使用 objcopy 或 libelf 将 elf 文件的一部分替换为另一个,以便将其加载到 Memory 中?
Define new code section in assembly code to compile ELF binary 在汇编代码中定义新的代码段以编译 ELF 二进制文件
Thank you for any suggestions in advance,感谢您提前提出任何建议,
it seems that this is a bit difficult
好像这个有点难
That's a bit of understatement: for all practical purposes this is impossible to do (at least on ELF platforms), because in the process of linking hello_world
the linker discards much of the information that was contained on object files which comprise hello_world
, and is necessary to reconstruct it again.这有点轻描淡写:出于所有实际目的,这是不可能做到的(至少在 ELF 平台上),因为在链接
hello_world
的过程中,linker丢弃了包含在 object 文件中的大部分信息,这些信息包含hello_world
,并且是必要的再次重建它。
I am trying to add a.rodata section into the binary that I have no source code for
我正在尝试将 a.rodata 部分添加到我没有源代码的二进制文件中
That is unlikely to be your real goal -- the original binary will not use your added .rodata
, so your goal must be something else .这不太可能是您的真正目标 - 原始二进制文件不会使用您添加的
.rodata
,因此您的目标必须是别的东西。 See also http://xyproblem.info/ .另见http://xyproblem.info/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.