trap: give disasm some nop's, so it shows the label
[mate.git] / ffi / trap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 // Note by hs: my signal.h includes sys/uconctext which conflicts with
5 // asm/ucontext - this hack kinda solves the problem for me ;-) 
6 // so feel free to blame me for that s**t
7 #if defined __USE_XOPEN2K8
8 #undef __USE_XOPEN2K8
9 #define RESTORE
10 #warning hs-hack: undefining __USE_XOPEN2K8 for signal.h
11 #endif
12 #include <signal.h>
13 #ifdef RESTORE
14 #define __USE_XOPEN2K8
15 #endif
16
17 #include <asm/ucontext.h>
18
19 unsigned int getMethodEntry(unsigned int, void *, void *);
20
21 void *method_map = NULL;
22 void *caller_map = NULL;
23
24 void set_mmap(void *mmap)
25 {
26         printf("set_mmap: 0x%08x\n", (unsigned int) mmap);
27         method_map = mmap;
28 }
29
30 void *get_mmap()
31 {
32         printf("get_mmap: 0x%08x\n", (unsigned int) method_map);
33         return method_map;
34 }
35
36 void set_cmap(void *cmap)
37 {
38         printf("set_cmap: 0x%08x\n", (unsigned int) cmap);
39         caller_map = cmap;
40 }
41
42 void *get_cmap()
43 {
44         printf("get_cmap: 0x%08x\n", (unsigned int) caller_map);
45         return caller_map;
46 }
47
48
49 void mainresult(unsigned int a)
50 {
51         printf("mainresult: 0x%08x\n", a);
52 }
53
54 void callertrap(int nSignal, siginfo_t *info, void *ctx)
55 {
56         struct ucontext *uctx = (struct ucontext *) ctx;
57         unsigned int from = (unsigned int) uctx->uc_mcontext.eip - 2;
58         unsigned int patchme = getMethodEntry(from, method_map, caller_map);
59
60         printf("callertrap(mctx)  by 0x%08x\n", from);
61
62         unsigned int *to_patch = (unsigned int *) (from + 1);
63         unsigned char *insn = (unsigned char *) from;
64         *insn = 0xe8; // call opcode
65         printf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
66         printf("*to_patch: 0x%08x\n", *to_patch);
67         if (*to_patch != 0x90ffff90) {
68                 printf("something is wrong here. abort\n");
69                 exit(0);
70         }
71         *to_patch = patchme - (from + 5);
72         printf("*to_patch: 0x%08x\n", *to_patch);
73         uctx->uc_mcontext.eip = (unsigned long) insn;
74         // while (1) ;
75 }
76
77 void register_signal(void)
78 {
79         struct sigaction segvaction;
80         segvaction.sa_sigaction = callertrap;
81         sigemptyset(&segvaction.sa_mask);
82         segvaction.sa_flags = SA_SIGINFO | SA_RESTART;
83         sigaction(SIGILL, &segvaction, NULL);
84 }
85
86 unsigned int getaddr(void)
87 {
88         return (unsigned int) mainresult;
89 }