在可执行文件上使用dlopen() [英] Using dlopen() on an executable

查看:261
本文介绍了在可执行文件上使用dlopen()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从另一个程序调用一个函数.如果其他程序是一个库,我可以简单地使用dlopen和dlsym获取该函数的句柄.不幸的是,另一个程序是Unix Executable,并且不能将其构建为库.在可执行文件上尝试dlopen()会显示以下错误消息:

I need to call a function from another program. If the other program were a library, I could simply use dlopen and dlsym to get a handle to the function. Unfortunately, the other program is a Unix Executable, and building it as a library is not an option. Trying dlopen() on the executable gives this error message:

dlopen([...]/testprogram, 1): no suitable image found. Did find: [...]/testprogram: can't map

dlopen([...]/testprogram, 1): no suitable image found. Did find: [...]/testprogram: can't map

这并不奇怪,因为dlopen是用于库而不是可执行文件.有什么方法可以使dlopen和dlsym与可执行文件一起使用?如果不是,是否有另一种方法可以实现同一目标?

This isn't surprising, as dlopen is meant for use with libraries, not executables. Is there any way to get dlopen and dlsym to work with executables? If not, is there an alternative way of achieving the same thing?

推荐答案

您无法将可执行文件作为库打开.可执行文件的入口点将尝试重新初始化C库,并接管brk指针.这将破坏您的malloc堆.此外,该可执行文件很可能会映射到没有重定位的固定地址,并且如果此地址与已加载的任何内容重叠,则也无法出于这个原因对其进行映射.

You can't open executables as libraries. The entry point of an executable will attempt to re-initialize the C library, and take over the brk pointer. This will corrupt your malloc heap. Additionally, the executable is likely to be mapped at a fixed address with no relocations, and if this address overlaps with anything already loaded, it's not possible to map it for that reason as well.

您需要将另一个程序重构到库中,或向另一个程序添加RPC接口.

You need to refactor the other program into a library, or add a RPC interface to the other program.

请注意,这不一定适用于PIE可执行文件.但是,除非可执行文件是专门为dlopen()设计的,否则这是不安全的,因为不会运行main(),因此不会发生在main()中完成的任何初始化.

Note that this does not necessarily apply for PIE executables. However, unless the executable is specifically designed for being dlopen()ed, this is unsafe, as main() will not be run, and any initialization done in main() therefore will not occur.

这篇关于在可执行文件上使用dlopen()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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