withMulklib working the first time
[lazyeval.git] / withAsm.c
1 #include <stdio.h>
2 #include <signal.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #define __USE_GNU
6 #include <ucontext.h>
7
8 // set this to REG_ECX on x86_32
9 #define myREG REG_RCX
10
11 int k = 42;
12
13 void handle_segv(int segv, siginfo_t* siginfo, void* ucontext) {
14   ucontext_t* uc = (ucontext_t*) ucontext;
15   uc->uc_mcontext.gregs[myREG] = (greg_t) &k;
16 }
17
18 void cause_segv (void* ptr) {
19   int d;
20   // d =  *ptr;
21   asm ("movl (%%ecx), %%edx" : "=d"(d) : "c"(ptr));
22   printf("%d\n", d);
23 }
24
25 int main (void) {
26   struct sigaction q;
27   bzero(&q, sizeof(q));
28   q.sa_sigaction = handle_segv;
29   q.sa_flags = SA_SIGINFO;
30   sigaction(11, &q, NULL);
31   cause_segv(NULL);
32 }