我目前被困在我的 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)