如何“链接”目标文件可执行文件/编译后的二进制? [英] How to 'link' object file to executable/compiled binary?

查看:374
本文介绍了如何“链接”目标文件可执行文件/编译后的二进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我想注入的对象文件到现有的二进制文件。作为一个具体的例子,考虑有一个源 HELLO.C

I wish to inject an object file into an existing binary. As a concrete example, consider a source Hello.c:

#include <stdlib.h>

int main(void)
{
    return EXIT_SUCCESS;
}

可以到的gcc -std = gnu99 -Wall -o HELLO.C您好你好可执行C>。此外,现在考虑 Embed.c

It can be compiled to an executable named Hello through gcc -std=gnu99 -Wall Hello.c -o Hello. Furthermore, now consider Embed.c:

func1(void)
{
}

这是目标文件 Embed.o 可以从这个通过创建GCC -c Embed.c 。我的问题是如何将一般插入 Embed.o 您好在必要的重定位执行这样的方式,和适当的ELF内部表(例如符号表,PLT等)适当地修补?

An object file Embed.o can be created from this through gcc -c Embed.c. My question is how to generically insert Embed.o into Hello in such a way that the necessary relocations are performed, and the appropriate ELF internal tables (e.g. symbol table, PLT, etc.) are patched properly?

假设

可以假定要被嵌入的对象文件有它的依赖性静态已连结。任何动态依赖,如C运行时可被假定为present也在目标可执行

It can be assumed that the object file to be embedded has its dependencies statically linked already. Any dynamic dependencies, such as the C runtime can be assumed to be present also in the target executable.

当前试图/创意


  • 使用 libbfd 从对象文件复制到节二进制。我与该取得的进展是,我可以创建新的对象,从原来的二进制部分和部分从对象文件。的问题是,由于对象文件是可重定位的,其截面不能正确复制到输出而不先进行重定位。

  • 转换二进制回到一个目标文件和 LD 重新链接。到目前为止,我尝试使用 objcopy把来执行转换 objcopy把--input ELF64-X86-64 --output ELF64-X86-64你好Hello.o 。错误:显然,我因为 LD -o Hello2 Embed.o Hello.o 打算那么将导致 LD这不起作用Hello.o:不支持的ELF文件类型2 。我想这应该预计,虽然因为您好不是一个对象文件。

  • 找到现有的工具进行这种插入?

  • Use libbfd to copy sections from the object file into the binary. The progress I have made with this is that I can create a new object with the sections from the original binary and the sections from the object file. The problem is that since the object file is relocatable, its sections can not be copied properly to the output without performing the relocations first.
  • Convert the binary back to an object file and relink with ld. So far I tried using objcopy to perform the conversion objcopy --input elf64-x86-64 --output elf64-x86-64 Hello Hello.o. Evidently this does not work as I intend since ld -o Hello2 Embed.o Hello.o will then result in ld: error: Hello.o: unsupported ELF file type 2. I guess this should be expected though since Hello is not an object file.
  • Find an existing tool which performs this sort of insertion?

原理的(可选读)

Rationale (Optional Read)

我想提出一个静态可执行文件编辑器,这里的目标是允许任意用户定义的例程的仪表到现有的二进制文件。这将分两步工作:

I am making a static executable editor, where the vision is to allow the instrumentation of arbitrary user-defined routines into an existing binary. This will work in two steps:


  1. 的对象文件(包含用户定义例程)成二进制的注射。 这是一个必不可少的步骤,不能用替代品,如共享对象,而不是周围的注射工作。

  2. 上的新的二进制执行静态分析和使用这种静态从原来的code键新添加code迂回例程。

我有,在大多数情况下,已经完成了必要的步骤2的工作,但是我有对象文件的喷射故障。问题是绝对可解鉴于其他工具使用对象注射的相同的方法(例如 EEL )。

I have, for the most part, already completed the work necessary for step 2, but I am having trouble with the injection of the object file. The problem is definitely solvable given that other tools use the same method of object injection (e.g. EEL).

推荐答案

如果是我的话,我想看看创建 Embed.c 成一个共享对象, libembed.so ,就像这样:

If it were me, I'd look to create Embed.c into a shared object, libembed.so, like so:

gcc -Wall -shared -fPIC -o libembed.so Embed.c

这应该创建从 Embed.c 可重定位共享对象。有了这一点,你可以强制你的目标二进制通过设置环境变量在运行时(请参阅 LD_ preLOAD 了解更多信息的这里):

That should created a relocatable shared object from Embed.c. With that, you can force your target binary to load this shared object by setting the environment variable LD_PRELOAD when running it (see more information here):

LD_PRELOAD=/path/to/libembed.so Hello

绝招,这里将是找出如何做你的仪表,特别是考虑到它是一个静态的可执行文件。在那里,我不能帮你,但这是在一个进程的内存空间code present的一种方式。你可能想要做一些初始化的构造函数,你可以用一个属性做(如果你使用 GCC ,至少):

void __attribute__ ((constructor)) my_init()
{
    // put code here!
}

这篇关于如何“链接”目标文件可执行文件/编译后的二进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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