来自文件的动态二进制文件

发布于 2024-10-10 15:21:02 字数 566 浏览 2 评论 0原文

这是一个有点奇怪的问题。

假设我有一个 C++ 代码,在特定平台上运行。此代码的唯一目的是运行包含该平台本机的二进制文件。

现在 - 我的问题是 - 我如何将这些文件中的数据(甚至可以按位,例如一个周期 1024 位)获取到运行我的代码的机器的内存中,以便该数据位于执行部分中?

换句话说,我可以将数据放到可以指向指令指针的地方吗?

如果是,怎么办?

我不介意是否必须为此使用汇编程序 - 只是这样它才能工作。

编辑:抱歉,我不够具体。

基本上 - 我想要加载的文件数据不是 .exe、Mach-O 可执行文件或 ELF 等格式。它只是原始二进制数据(带有一些我的代码删除的自定义标头)。该二进制数据是机器代码,特定的并且适合当前机器上运行的处理器。

从技术上讲,这意味着我想绕过普通操作系统执行器并直接加载二进制文件。我可以解释它,但最多会慢 3 倍左右。

如果我需要在另一个子进程中运行数据,我根本不介意 - 但同样,我不能使用正常的进程开启程序,因为数据不是操作系统可以自动运行的任何格式(同样 - 像.exe,马赫-O,ELF)。

This is a little bit of weird problem here.

Say I have a C++ code, running on a specific platform. The only purpose of this code is to run files containing binary, NATIVE to that platform.

Now - my question is - HOW would I get the data from these files (could even be by bits, like 1024 bits a cycle) to the memory of machine running my code so that this data would be in the execution part?

In other words, can I get the data to somewhere where I can point the instruction pointer?

If yes, how?

I don't mind if I have to use assembler for this - just so it would work.

EDIT: Sorry, I wasn't specific enough.

Basically - the file data I want to load is in no format like .exe, Mach-O executable or ELF. It is just raw binary data (with some custom headers which my code removes). This binary data is machine code, specific and suited for the processor running on the current machine.

Technically this means I want to go around normal OS executors and load the binary directly. I could interpret it but that would be around 3x slower at best.

I would not mind at all if I need to run the data in another child process - but again, I can not use normal process openers, because the data is not in any format that the OS could run automatically (again - like .exe, Mach-O, ELF).

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

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

发布评论

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

评论(2

鱼忆七猫命九 2024-10-17 15:21:02

您应该将文件读取到内存,将此内存标记为可执行文件(它是特定于平台的,例如 Windows 的 VirtualProtect()),并将其作为函数调用:((void(*) ())ptr)();

当然文件中的代码应该是位置无关< /a>.

You should just read file to memory, mark this memory as executable (it's platform-specific, for example VirtualProtect() for Windows), and call it as function: ((void(*)())ptr)();

Of course code in file should be position-independent.

天冷不及心凉 2024-10-17 15:21:02

您的解决方案需要有多通用?

操作系统加载程序通常知道如何处理特定的文件格式以及如何将它们加载到内存中。您通常可以使用操作系统加载程序并调用入口点,但为此您需要知道入口点是什么,并且无论如何您都需要了解文件格式。我想不出一个便携式解决方案。

如果您能详细解释您想要做什么,也许可以提供解决方案。

How generic does your solution need to be?

The OS loader usually knows how to handle specific file formats and how to load them into memory. You can usually use the OS loader and call the entry point, but for that you need to know what is the entry point and for that you need to understand the file format anyway. I can't think of a portable solution for that.

If you'll explain more about what you want to do maybe it will be possible to supply a solution.

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