我正在开展一个项目,以在 Linux 平台上加载和运行自定义二进制格式的可执行文件(在我的例子中为 PE)。到目前为止,我已经非常成功地完成了这项工作,首先加载可执行文件,然后加载一个小型 ELF 共享库,该共享库调用可执行文件的起始地址,然后安全退出。
不过,出于一些原因,我真的不想自己加载 ELF。首先,我使用的共享库是用汇编编写的(我不能使用其他任何东西,因为我没有链接到 libc
等),这将是非常特定于平台的,而且我'我想摆脱它并使用 C,这样我就可以为任何平台编译。此外,使用 Linux 的原生 ELF 加载器而不是我自己的简化版本会更容易和更安全。
我想知道是否有办法使用我的 binfmt 处理程序(一个已安装的内核模块)来加载我的可执行文件,然后让 Linux 将我的共享库(及其依赖项)加载到相同的地址空间而不覆盖我的可执行代码。我首先想到 uselib
系统调用可能有用,但手册页上的描述不清楚这是否符合我的目的:
From libc 4.4.4 on only the library "/lib/ld.so" is loaded, so that this dynamic library can load the remaining libraries needed (again using this call). This is also the state of affairs in libc5.
glibc2 does not use this call.
我也从未见过它的使用示例,而且我总是对使用我不理解的系统调用持谨慎态度。
是否有实现我所描述的目标的好方法?我能否使用 Linux 的现有功能将共享库(用 C 语言编写)加载到已经包含可执行代码的地址空间中,如果可以,我如何在不知道加载位置的情况下使用该库?
最佳答案
已经有一个像这样的项目称为 binfmt_pe(由我完成!),它是一个内核模块,将有自己的链接器(类似于/lib/ld)。 check it out here .
至于你关于制作模块和加载器/链接器的问题,有下面的链接。我还包含了有关 ELF 和 PE 可执行文件的信息的链接。
希望对您有所帮助。 :)
制作 Linux 内核模块的有用信息
- The Linux Kernel Module Programming Guide
- Writing Your Own Loadable Kernel Module
- Linux Data Structures
- The Linux kernel: The kernel source
- Kernel Support for miscellaneous Binary Formats
- binfmt_elf.c
- binfmt_misc.c
关于动态加载/链接的信息
- Understanding ld-linux.so.2
- ld.so : Dynamic-Link Library support
- How To Write Shared Libraries - PDF
- ld-linux(8) - Linux man page
- rpath
- Listing Shared Library Dependencies
关于 ELF 和 PE 格式的信息
- OSRC: Executable File Formats
- EXE Format
- How Windows NT Recognizes MS-DOS - Based Applications
- Common Object File Format (COFF)
- Peering Inside the PE: A Tour of the Win32 Portable Executable File Format
- Executable and Linkable Format
- An In-Depth Look into the Win32 Portable Executable File Format
- An In-Depth Look into the Win32 Portable Executable File Format, Part 2
- Injective Code Inside an Import Table
- The PE Format | Hackers Library
- IMAGE_NT_HEADERS structure (Windows)
- x86 Disassembly/Windows Executable Files
- the Portable Executable Format on Windows
关于linux - 将 ELF 共享库和自定义 binfmt 可执行文件加载到同一 Linux 地址空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13095941/