如何使用c++执行附加在可执行文件末尾的机器代码

How to execute machine code appended to the end of an executable file using c++?

本文关键字:机器 代码 可执行文件 c++ 何使用 执行      更新时间:2023-10-16

我有一个程序,它将机器代码附加到可执行文件的末尾,我想在可执行文件开头写一个c++程序来运行附加的机器代码。附加的代码将运行原始程序。

我的程序将在mac上运行。

这里还有一个类似的问题:执行附加在可执行文件末尾的机器代码然而,这个问题没有得到回答,而是提出了其他解决办法。我不能使用链表,因为这个程序的目的是修改机器上的可执行文件的功能,这些机器可能不一定安装编译器。

这里有一个例子:

// C++ program at the beginning of file, runs appended machine code.
// Original program, must be skipped over and run after appended machine code is run.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
    FILE *input,*output;
    char buffer[512];
    =fopen("a.out","rb");
    output=fopen("filename","ab");
    for(;fread(buffer,512,1,input)==1;)
        fwrite(buffer,512,1,output);
    fclose(output);
    cout << "Testing, testing, testing" << endl;
    //Run original program
}

您可以制作一个程序,从附加的代码中创建一个可执行文件,然后执行它。或者,如果您想要的是可执行文件执行附加的代码,那么您必须将附加的代码作为函数调用,或者跳转到内联程序集中的代码("call"或"jmp")。