执行附加在可执行文件末尾的机器代码 [英] Executing machine codes attached at the end of an executable

查看:23
本文介绍了执行附加在可执行文件末尾的机器代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象文件,里面有一个 main() 函数,只需要与 crt... 对象链接即可成为可执行文件.不幸的是,我只能编译,不能将其链接为可执行文件.

所以我决定创建一个 c 程序(在带有工作 GCC 和链接器的 pc 上)以在其自身末尾附加对象并在运行时执行附加的代码(模拟链接对象).

我看到了 DL API,但我不知道如何使用它来解决我所说的问题.

请帮助我了解如何执行附加在可执行文件末尾的代码.

解决方案

避免这样做;这将是一团糟......而且它可能不会可靠地工作,至少如果程序动态链接到 libc6.so(例如因为 ASLR)

只需使用共享对象和动态链接库(请参阅动态链接器 wikipage).你需要了解dlopen(3)等>

如果您真的坚持,请花数周时间了解更多信息:阅读 Levine 关于链接器和加载程序,阅读Advanced Linux Programming,阅读很多手册页(包括execve(2), mmap(2), elf(5), ld.so(8), ...) 研究 execvemmap 的内核代码,GNU libcMUSL libc 源代码(有关动态链接器实现的详细信息),x86-64 ABIABI 用于您的目标处理器(是吗?ARM?),了解有关 GNU binutils 等的更多信息.

总之,做这些乱七八糟的事情,你的生命太短暂了,除非你已经是专家,例如能够实现自己的动态链接器.

附录

显然,您真正的问题似乎是在 ARM 上使用 tinycc(在 Android 下我是猜测).然后我会在他们的邮件列表中询问(也许会提供一些补丁),或者只是使用 binutils 并制作自己的 GNU ld 链接器脚本 使其工作.然后问题变得完全不同,并且与您的原始问题完全无关.根据 Google 搜索,之前可能有一些尝试解决该问题.

I have a object file which has a main() function inside and just needs to be linked with crt... objects to be an executable . Unfortunately I can only compile and I can not link it to be an executable .

so I decided to create a c program ( on a pc with working GCC and linker ) to attach object(s) at the end of itself and execute the codes attached at run time (simulating a linked object ).

I saw DL API but I do'nt know how to use it for the problem I said .

May sb help me to know , how I can executing a code attached at the end of an executable .

解决方案

Avoid doing that; it would be a mess .... And it probably won't reliably work, at least if the program is dynamically linked to the  libc6.so (e.g. because of ASLR)

Just use shared objects and dynamically linked libraries (See dynamic linker wikipage). You need to learn about dlopen(3) etc.

If you really insist, take many weeks to learn a lot more: read Levine's book on Linker and Loaders, read Advanced Linux Programming, read many man pages (including execve(2), mmap(2), elf(5), ld.so(8), ...) study the kernel code for execve and mmap, the GNU libc and MUSL libc source codes (for details about implementations of the dynamic linker), the x86-64 ABI or the ABI for your target processor (is it an ARM?), learn more about the GNU binutils etc, etc, etc.

In short, your life is too short doing such messy things, unless you are already an expert, e.g. able to implement your own dynamic linker.

addenda

Apparently your real issue seems to use tinycc on the ARM (under Android I am guessing). I would then ask on their mailing list (perhaps contribute with some patch), or simply use binutils and make your own GNU ld linker script to make it work. Then the question becomes entirely different and completely unrelated to your original question. There might be some previous attempts to solve that, according to Google searches.

这篇关于执行附加在可执行文件末尾的机器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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