小编coc*_*iño的帖子

用c在内存二进制文件中执行

我目前被困在我的 c 项目上,我试图用 C 语言在内存中运行一个程序,但我一直都有分段错误。

首先我有一个简单的hello_worl.c,我编译它,我有输出hello_world

第二,我hello_world像这样转换为 C 标头:

xxd -i hello_world > hello.h

(hello.h 的内容)

unsigned char hello[] = {
...
}
unsigned int hello_len = 16696;
Run Code Online (Sandbox Code Playgroud)

最后,我创建了程序main.c,看起来像这样:

#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include "hello.h"

int main ()
{
    int (*my_hello) () = NULL;

    // allocate executable buffer
    my_hello = mmap (0, sizeof(hello), PROT_READ|PROT_WRITE|PROT_EXEC,
                MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);

    // copy code to buffer
    memcpy (my_hello, hello, sizeof(hello));
    __builtin___clear_cache (my_hello, my_hello + …
Run Code Online (Sandbox Code Playgroud)

c linux x86-64 dynamic-memory-allocation

1
推荐指数
1
解决办法
228
查看次数

标签 统计

c ×1

dynamic-memory-allocation ×1

linux ×1

x86-64 ×1