4 /* TODO(bernhard): use {u,}int* types */
7 // Note by hs: my signal.h includes sys/uconctext which conflicts with
8 // asm/ucontext - this hack kinda solves the problem for me ;-)
9 // so feel free to blame me for that s**t
10 #if defined __USE_XOPEN2K8
13 #warning hs-hack: undefining __USE_XOPEN2K8 for signal.h
17 #define __USE_XOPEN2K8
20 #include <sys/ucontext.h>
22 unsigned int getMethodEntry(unsigned int, unsigned int);
23 unsigned int getStaticFieldAddr(unsigned int, void*);
26 #define dprintf(args...) do { printf (args); } while (0);
28 #define dprintf(args...)
31 #define NEW_MAP(prefix) \
32 void* prefix ## _map = NULL; \
33 void set_ ## prefix ## map(void *map) \
35 dprintf("set_%s: 0x%08x\n", #prefix , (unsigned int) map); \
36 prefix ## _map = map; \
38 void *get_ ## prefix ## map() \
40 dprintf("get_%s: 0x%08x\n", #prefix , (unsigned int) prefix ## _map); \
41 return prefix ## _map; \
51 void mainresult(unsigned int a)
53 dprintf("mainresult: 0x%08x\n", a);
56 void callertrap(int nSignal, siginfo_t *info, void *ctx)
58 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
59 unsigned int from = (unsigned int) mctx->gregs[REG_EIP] - 2;
60 unsigned int *to_patch = (unsigned int *) (from + 1);
61 dprintf("callertrap(mctx) by 0x%08x\n", from);
62 if (*to_patch != 0x90ffff90) {
63 dprintf("callertrap: something is wrong here. abort\n");
66 unsigned int patchme = getMethodEntry(from, 0);
68 unsigned char *insn = (unsigned char *) from;
69 *insn = 0xe8; // call opcode
70 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
71 dprintf("*to_patch: 0x%08x\n", *to_patch);
72 *to_patch = patchme - (from + 5);
73 dprintf("*to_patch: 0x%08x\n", *to_patch);
74 mctx->gregs[REG_EIP] = (unsigned long) insn;
77 void staticfieldtrap(int nSignal, siginfo_t *info, void *ctx)
79 /* TODO(bernhard): more generic and cleaner please... */
80 mcontext_t *mctx = &((ucontext_t *) ctx)->uc_mcontext;
81 unsigned int from = (unsigned int) mctx->gregs[REG_EIP];
82 if (from < 0x10000) { // invokevirtual
84 dprintf("from: 0x%08x but should be 0 :-(\n", from);
86 unsigned int method_table_ptr = (unsigned int) mctx->gregs[REG_EAX];
87 unsigned int *esp = (unsigned int *) mctx->gregs[REG_ESP];
88 /* get actual eip from stack storage */
89 unsigned int from = (*esp) - 3;
90 unsigned char offset = *((unsigned char *) (*esp) - 1);
91 /* method entry to patch */
92 unsigned int *to_patch = (unsigned int*) (method_table_ptr + offset);
93 dprintf("invokevirtual by 0x%08x with offset 0x%08x\n", from, offset);
94 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
95 dprintf("*to_patch: 0x%08x\n", *to_patch);
96 *to_patch = getMethodEntry(from, method_table_ptr);
97 mctx->gregs[REG_EIP] = *to_patch;
98 dprintf("*to_patch: 0x%08x\n", *to_patch);
100 unsigned int *to_patch = (unsigned int *) (from + 2);
101 dprintf("staticfieldtrap by 0x%08x\n", from);
102 if (*to_patch != 0x00000000) {
103 dprintf("staticfieldtrap: something is wrong here. abort\n");
106 unsigned int patchme = getStaticFieldAddr(from, trap_map);
108 dprintf(" to_patch: 0x%08x\n", (unsigned int) to_patch);
109 dprintf("*to_patch: 0x%08x\n", *to_patch);
111 dprintf("*to_patch: 0x%08x\n", *to_patch);
115 void register_signal(void)
117 struct sigaction illaction;
118 illaction.sa_sigaction = callertrap;
119 sigemptyset(&illaction.sa_mask);
120 illaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
121 sigaction(SIGILL, &illaction, NULL);
123 struct sigaction segvaction;
124 segvaction.sa_sigaction = staticfieldtrap;
125 sigemptyset(&segvaction.sa_mask);
126 segvaction.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
127 sigaction(SIGSEGV, &segvaction, NULL);
130 unsigned int getaddr(void)
132 return (unsigned int) mainresult;
135 unsigned int getMallocAddr(void)
137 return (unsigned int) malloc;