If you just need to check, try this, this is magic ...
const unsigned char code[2] = {0xc9, 0xc3};
^^^^^
const const (! !), , text. . , :
__attribute__((section("text"))
const unsigned char code[2] = {0xc9, 0xc3};
,
.text
.globl code
code:
.byte 0xc9
.byte 0xc3
: , mprotect. .
:
#include <stdlib.h>
#include <sys/mman.h>
#include <err.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
unsigned char *p = malloc(4);
int r;
p[0] = 0x8d;
p[1] = 0x47;
p[2] = 0x01;
p[3] = 0xc3;
r = mprotect((void *) ((uintptr_t) p & ~(uintptr_t) 4095), 4096,
PROT_READ | PROT_WRITE | PROT_EXEC);
if (r)
err(1, "mprotect");
int (*f)(int) = (int (*)(int)) p;
return f(1);
}
mprotect , undefined, mmap, , , , OS X, OS X malloc mmap (, ).