从内存加载 .so 文件

发布于 2024-12-29 11:04:19 字数 305 浏览 0 评论 0原文

可能的重复:
从内存中打开 dlopen?

内存缓冲区加载,但我在 Linux 的任何地方都找不到它,而“ld”源代码是我见过的最复杂的代码。那么:

有没有从内存加载.so文件的例子?即使是简单的一个我也能完成吗?我只是不知道从哪里开始,尽管我已经阅读了大部分 ELF 规范,但它对我来说仍然很神秘。

Possible Duplicate:
dlopen from memory?

I've seen this for Windows' DLL files, being loaded from a memory buffer, but I cant find it anywhere for Linux, and "ld" source code is the most complex code I've ever seen. So:

Is there any example of loading .so files from memory? Even a simple one that I can finish? I just don't know where to start, even though I've read most of the ELF specifications it's still mysterious to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱要勇敢去追 2025-01-05 11:04:19

您正在查看错误的源代码:ld 不执行程序和库加载。相反,您应该查看 libc 中的 dlopen 和 dlsym 函数的源代码。另外,您应该查看动态链接器的源代码:ld-linux.so(真实名称因平台而异;执行ldd /bin/ls即可找到动态链接器所在的位置)。

ELF 解析并不困难,但需要注意细节并理解特定 CPU 的汇编代码;您还需要适用于您的平台的 ABI 规范(32 位和 64 位 Linux 不同,CPU 之间也不同。)

如果您只需要在运行时从内存加载目标文件 (即,它不一定是 SO),您可以查看 X11 项目:他们实现了一个模块系统,该系统基本上在某个地址加载目标代码并重新定位它。

You're looking at the source code of a wrong thing: ld doesn't do program and library loading. Instead, you should look at the source code of dlopen and dlsym functions found in libc. Also, you should look at the source of the dynamic linker: ld-linux.so (the true name varies with the platform; execute ldd /bin/ls to find out where the dynamic linker resides).

ELF parsing isn't difficult, but it requires attention to detail and understanding of assembly code for the particular CPU; you need also ABI specification for your platform (and it's different for 32- and 64-bit linux, and is also different between CPUs.)

If you just need to load object files from memory at run-time (i.e., it doesn't have to be a SO), you can look at X11 project: they have implemented a module system which, basically, loads object code at some address and relocates it.

逆蝶 2025-01-05 11:04:19

您需要 dlopen() 系列函数(在 GNU/Linux 上,它们在 /usr/include/dlfcn.h 中定义)。

例如,请查看PHP 如何处理模块

You need dlopen() family of functions (on GNU/Linux, they are defined in /usr/include/dlfcn.h).

For an example, take a look at how PHP does modules.

一绘本一梦想 2025-01-05 11:04:19

“从内存加载 .so 文件”对您来说意味着什么?

如果您有任何 *.so 文件,那么它位于某个文件系统中,并且有一个路径。然后只需使用 dlopen 就可以了。

如果它不是一个文件,那它是什么?你是怎么进入记忆的?你的记忆中到底有什么? (内存中是否有 ELF 标头和 ELF 布局?)

如果您有足够的信息来制作 ELF *.so 文件,请将此类文件转储(即写入)到某个文件系统中(使用临时文件)如果您关心磁盘性能,则像 tmpfs 这样的文件系统)。然后dlopen

如果您没有足够的信息来创建 ELF .so 文件,那么您可能正在内存中动态构建代码。查看现有的机器代码生成基础设施(例如 LLVMGCCJIT, libjit ,<一href="http://www.gnu.org/software/lightning/" rel="nofollow">GNU 闪电, LuaJit ....)正在做。

如果内存中有完整的功能代码,请确保内存可以使用 mmap & 执行; mprotect 并跳转到其中(例如使用函数指针技巧)。

What does "loading .so files from memory" means to you?

If you have any *.so file, then it is in some file system, and has a path. Then just use dlopen on it.

If it is not a file, what is it? How did you get in memory? What exactly have you in memory? (Do you have an ELF header and ELF layout in memory?)

If you have enough information to make an ELF *.so file, dump (i.e. write) such file into some file system (use a temporary filesystem like tmpfs if you are concerned with disk performance). Then dlopen that.

If you don't have enough information to make an ELF .so file, then probably you are dynamically building code in memory. Look at what existing machine code generating infrastructure (like LLVM, GCCJIT, libjit, GNU lightning, LuaJit ....) are doing.

If you have a full functional code in memory, ensure that the memory is executable with mmap & mprotect and jump into it (e.g. using function pointer tricks).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文