我有一个生成自修改代码的程序(如果您有兴趣,请参阅https://tigress.wtf/selfModify.html)。它在 x86 Darwin 和 Linux 上运行。在达尔文,我编译
gcc -g -segprot __TEXT rwx rwx self_modifying.c -o self_modifying.exe
Run Code Online (Sandbox Code Playgroud)
最近,这似乎不起作用,我明白了
dyld: malformed mach-o image: __TEXT segment maps start of file but is writable
Run Code Online (Sandbox Code Playgroud)
当我运行程序时。
我在 MacOS 10.15.3 上运行 clang 6.0.1 版。任何帮助,将不胜感激。
作为移植 Forth 编译器的一部分,我正在尝试创建一个允许自修改代码的二进制文件。血淋淋的细节位于https://github.com/klapauciusisgreat/jonesforth-MacOS-x64
理想情况下,我为用户定义创建一堆页面并调用 mprotect,如下所示:
#define __NR_exit 0x2000001
#define __NR_open 0x2000005
#define __NR_close 0x2000006
#define __NR_read 0x2000003
#define __NR_write 0x2000004
#define __NR_mprotect 0x200004a
#define PROT_READ 0x01
#define PROT_WRITE 0x02
#define PROT_EXEC 0x04
#define PROT_ALL (PROT_READ | PROT_WRITE | PROT_EXEC)
#define PAGE_SIZE 4096
// https://opensource.apple.com/source/xnu/xnu-201/bsd/sys/errno.h
#define EACCES 13 /* Permission denied */
#define EINVAL 22 /* Invalid argument */
#define ENOTSUP 45 /* Operation not supported */
/* Assembler entry point. */
.text
.globl start
start:
// Use mprotect to …Run Code Online (Sandbox Code Playgroud)