可以从文件中执行机器代码吗?(It's possible to execute machine code from file? [closed])

编程入门 行业动态 更新时间:2024-10-11 11:20:02
可以从文件中执行机器代码吗?(It's possible to execute machine code from file? [closed])

我知道内联asm存在,但是在RUNTIME期间是否也可以从文件中执行机器代码?

我需要写我自己的口译员吗?

我在Windows 7上使用启用了c ++ 14的GNU C ++编译器。

谢谢阅读。

I know that the inline asm exists, but is it also possible to execute machine code from a file during RUNTIME?

Would i need to write my own interpreter?

I'm using the GNU C++ compiler with c++ 14 enabled, on Windows 7.

Thanks for reading.

最满意答案

通过重新编写机器代码,这个问题开始采用更合理的形式。

简短的回答:是的,您可以在应用程序中运行机器代码。 更长的答案是 - 它很复杂。

实质上,只要满足一些条件,例如数据是合法的机器指令(否则处理器将调用非法指令异常并且OS将终止你的程序),可以执行存储器中的任何位和字节串,并且存储器加载数据的页面标有可执行权限。

话虽如此,那些机器代码实际运行正常并按照您期望的方式运行所需的条件要困难得多,并且与理解虚拟内存,动态加载程序和动态链接程序有关。

为了直截了当地回答你的问题,至少对于符合POSIX的环境,你总是可以使用mmap系统调用通过PROT_EXEC权限将文件映射到内存中,并跳转到希望获得最佳效果的内存空间中。 当然,任何代码将会在内存中找到的符号不可能存在,并且代码更好地编译为PIC(位置独立代码),但这大致用YES来回答您的问题。

为了更好的控制,你通常更喜欢使用更标准的方法,比如将额外的代码编译为共享对象(动态链接库,Windows中的DLL)并使用dlopen将其加载到应用程序中,同时使用dlsym访问符号它。 它仍允许您将机器代码从磁盘加载到您的应用程序中,但它也会以格式良好的标准方式存储机器代码,从而允许动态链接器将新代码段正确加载并链接到应用程序中,从而减少意外行为。

在这两种情况下,您都不需要解释器,但它也不是语言或编译器使用的问题 - 这是特定于操作系统的功能,并且在Windows上的行为会有很大不同。

作为一种不同的方法,您可以考虑在编译时使用#include指令将汇编代码的外部块导入到工作中,并将其合并到编译时,这将产生更为确定的结果。

编辑:对于Windows, mmap的并行是CreateFileMapping dlopen是LoadLibrary

不是Windows专家,对不起......

With your rephrasing into machine code, this question starts taking a more reasonable shape.

A short answer: Yes, you can run machine code from within your application. A longer answer is - it's complicated.

Essentially, any string of bits and bytes in memory can be executed, given some conditions are met, such as the data being legal machine instructions (Otherwise the processor will invoke the illegal instruction exception and the OS will terminate your program) and that the memory page into which the data is loaded is marked with executable permissions.

Having said that, the conditions required for that machine code to actually run correctly and do what you expect it to do, is significantly harder, and have to do with understanding of Virtual Memory, Dynamic Loaders and Dynamic Linkers.

To bluntly answer your question, for a POSIX compliant environment at the least, you could always use the mmap system call to map a file into memory with PROT_EXEC permissions and jump into that memory space hoping for the best. Naturally, any symbols that code would be expecting to find in memory aren't likely to be there, and the code was better compiled as PIC (Position Independent Code) but this roughly answers your question with a YES.

For better control, you'd usually prefer to use a more standard method, such as compiling your extra code as a shared object (Dynamic Link Library, DLL in Windows) and loading it into your application with dlopen while using dlsym to access symbols within it. It still allows you to load machine code from the disk into your application, but it also stores the machine code in a well formatted, standard way, which allows the dynamic linker to properly load and link the new code segment into your application, reducing unexpected behavior.

In neither of these cases will you need an interpreter, but neither is it a matter of language or compiler used - this is OS specific functionality, and will behave quite differently on Windows.

As a different approach, you could consider using the #include directive to import an external chunk of assembly code into your work while you're still working on it and properly incorporate it in compile time, which will yield far more deterministic results.

Edit: For windows, the parallel for mmap is CreateFileMapping dlopen is LoadLibrary

Not a Windows expert, sorry...

更多推荐

本文发布于:2023-07-27 15:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:机器   代码   文件   execute   closed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!