直接以二进制执行计算机指令 [英] Execute computer instructions directly in binary

查看:155
本文介绍了直接以二进制执行计算机指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中,我需要一种在程序运行时直接执行计算机指令的方法.我知道如何以二进制形式编写计算机指令的唯一方法是通过十六进制编辑器,然后将文件作为应用程序运行.我将如何使用程序编写二进制文件,然后从其执行,而不必创建新的执行过程.看来他们应该是执行此操作的简单方法,但是无论我在哪里看,我都找不到它.

In C. I need a way to execute computer instructions DIRECTLY while a program is running. The only way I know how to make computer instructions in binary is through a hex editor, then you run the file as an application. How would I write the binary using a program and then execute it from their without having to create a new process for execution. It seems like their should be a simple way to do this but no matter where I look I can't find it.

我能想到的唯一另一种方法是通过内联汇编,但是在我当前的项目中,这是一个缺点,直接使用二进制执行是最好的方法. (这可能会需要在Windows上使用驱动程序吗?如何在Linux上运行?换句话说,跨平台方法会很好)

The only other way I can think of doing this is through inline assembly, but in my current project that would be a drawback, executing directly with binary is the best way to go. (would this possibly require a driver on windows? how to do on linux? in other words a cross-platform method would be nice)

谢谢.

推荐答案

您想要做的事情有点问题,容易使很多人问您为什么要这样做?"

What you want to do is a bit problematic and is liable to make a lot of people ask "Why are you doing that?"

假设您有一个没有内存保护的操作系统(这种情况非常少见),则只需将一个函数指向字节数组并调用该函数即可.这是要点:

Assuming you have an operating system WITHOUT memory protection (which is very rare), you can just point a function to array of bytes and call the function. Here's the gist of it:

unsigned char* code_to_execute = "\xB8\x13\x00\xCD\x10";
void (*runCode)();

runCode = code_to_execute;

runCode();

但是,做这样的事情时有很多事情要担心.您需要知道您的C编译器如何设置函数调用框架,并在二进制代码"中加以尊重.以这种方式创建跨平台代码是不可能的.即使使其在单个平台上的多个C编译器中运行也很棘手.然后是内存保护.大多数现代操作系统根本不允许您随意将数据作为代码执行.您需要将内存显式标记为可执行文件,并且在没有特殊许可的情况下,许多操作系统都不允许您执行该操作.也没有跨平台的方法可以做到这一点.

But, there are SO MANY THINGS to worry about when doing something like this. You need to know how your C compiler is setting up function call frames and respect that in your "binary code". It's impossible to create cross-platform code in this manner. Even making it run in multiple C compilers on a single platform would be tricky. And then there's memory protection. Most modern operating systems simply won't let you arbitrarily execute data as code. You need to explicitly mark the memory as executable and many operating systems won't let you do that without special permission. There is no cross-platform way to do this either.

我再次强调,这确实不是一个好主意.使用内联汇编语言会更好.或者更好的是,根本不使用汇编语言.也许您可以解释一些有关您的项目的信息,以及为什么直接在C程序中编写二进制代码"很重要.这将有助于我们制定出可以对您有很大帮助的答案或建议.

Again, I want to stress that this is really not a good idea. You would be better off using inline assembly language. Or better yet, don't use assembly language at all. Maybe you could explain a little more about your project and why it's important to write "binary code" directly in your C program. It would help us craft an answer or recommendation that could help you considerably.

这篇关于直接以二进制执行计算机指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆