可以使用“dlopen”加载和映射静态 ELF 可执行文件(不是库)吗?
如果文件anexe
是静态编译的,没有符号,没有导出,ELF,可以使用dlopen("anexe", RTLD_LAZY)
将其映射到内存吗?
我的目标不是能够引用符号——没有符号。相反,我的目标是能够在给定地址(固定的,而不是 PIC)的情况下调用其函数,并在给定地址的情况下读取其数据。我本来计划通过 mmap
完成这一切,但意识到 dlopen
虽然不是为此设计的,但也许能够为我完成这一切。
dlopen可以这样用吗?
当我尝试时,我得到dlerror
:无法动态加载可执行文件
- 有任何解决方法吗?
If file anexe
is static compiled, no symbols, no exports, ELF, can dlopen("anexe", RTLD_LAZY)
be used to map it into memory?
My goal isn't to be able to reference symbols -- there are no symbols. Rather, my goal is to be able to call its functions given their addresses (which are fixed, not PIC), and to read its data given address. I was planning on doing this all via mmap
, but realized that dlopen
, though not designed for thhis, might be able to do it all for me.
Can dlopen be used that way?
When I try it, I get dlerror
: cannot dynamically load executable
- is there any workaround for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不(但见下文)。
请注意,非 PIE 可执行文件只能加载到其链接的地址。您可以通过检查程序头来找到哪个地址(以编程方式或使用 readelf -Wl)。
一旦您知道它应该加载到哪个地址(并假设该地址尚未被您自己的可执行文件占用),那么您可以执行一系列
mmap
调用来将该可执行文件“加载”到内存中,之后你可以调用它的函数并读取它的数据。一个复杂的问题是可执行文件的初始化程序不会运行(您没有调用它的
_start
,您也不想这样做),因此其中的函数可能无法正确运行,可能会崩溃等。例如,给定此二进制文件:
您需要使用
MAP_FIXED
执行 4 个mmap
调用(每个LOAD
段调用一次)。mmap
的地址、文件偏移量、大小和保护参数是显而易见的。最后一段的
mmap
参数可能不是:您需要将地址和偏移量向下舍入到Align
,并将大小扩展0x928
来解释该舍入。No (but see below).
Note that a non-PIE executable can only be loaded at the address it was linked at. You can find which address that is by examining its program headers (programmatically, or with
readelf -Wl
).Once you know which address it should be loaded at (and assuming that address is not already occupied by your own executable), then you can perform a series of
mmap
calls to "load" that executable into memory, and after that you can call its functions and read its data.One complication is that the executable's initializers will not have run (you didn't call its
_start
, nor would you want to), and so functions in it may not run correctly, may crash, etc.For example, given this binary:
you would need to perform 4
mmap
calls (one for eachLOAD
segment), withMAP_FIXED
. The address, file offset, size and protection arguments formmap
are obvious.The
mmap
arguments for the last segment may not be: you would need to round address and offset down toAlign
, and extend the size by0x928
to account for that rounding.