http://nicosia.is.s.u-tokyo.ac.jp/pub/essay/hagiya/h/curry
を Sun から x86 に。
C言語は便利で楽しいなあ、と思わざる得ないのである。
に激しく同意なのです。
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
int (*curry(int (*F)(), int A))() {
char *code = (char*)malloc(18);
*(char*)code = 0x55;
*(short int*)(code+1) = 0xe589;
*(short int*)(code+3) = 0x75ff;
*(char*)(code+5) = 0x08;
*(char*)(code+6) = 0x6a;
*(char*)(code+7) = (char)A;
*(char*)(code+8) = 0xe8;
*(int*)(code+9) = (int)F-((int)code+9+4);
*(short int*)(code+13) = 0xc483;
*(short int*)(code+15) = 0x08;
*(short int*)(code+16) = 0xc9;
*(short int*)(code+17) = 0xc3;
mprotect((void*)(((int)code+4095) & ~4095 - 4096),
4096, PROT_READ | PROT_EXEC);
return((int (*)())code);
}
int add(int x, int y) {
return(x + y);
}
int main() {
int (*c)();
c = curry(add, 100);
printf("%d\n", (*c)(10));
}