1 #include "flush-cache.h"
16 # define GET_ENTRY(fdesc) (((uintptr_t *) (fdesc))[0])
17 # define GET_GP(fdesc) (((uintptr_t *) (fdesc))[0])
20 # define GET_ENTRY(fdesc) ((uintptr_t ) (fdesc))
21 # define GET_GP(fdesc) (0)
26 make_executable (void *addr, size_t len)
28 if (mprotect ((void *) (((long) addr) & -getpagesize ()), len,
29 PROT_READ | PROT_WRITE | PROT_EXEC) < 0)
34 flush_cache (addr, len);
39 create_func (unw_dyn_info_t *di, const char *name, long (*func) (),
40 void *end, unw_dyn_region_info_t *region)
42 void *mem, *memend, *addr, *fptr;
46 len = (uintptr_t) end - GET_ENTRY (func) + EXTRA;
49 printf ("%s: cloning %s at %p (%zu bytes)\n",
50 __FUNCTION__, name, mem, len);
51 memend = (char *) mem + len;
54 addr = (void *) GET_ENTRY (func);
56 /* build function descriptor: */
57 ((long *) mem)[0] = (long) mem + 16; /* entry point */
58 ((long *) mem)[1] = GET_GP (func); /* global-pointer */
60 mem = (void *) ((long) mem + 16);
65 len = (char *) memend - (char *) mem;
66 memcpy (mem, addr, len);
68 if (make_executable (mem, len) < 0)
73 memset (di, 0, sizeof (*di));
74 di->start_ip = (unw_word_t) mem;
75 di->end_ip = (unw_word_t) memend;
77 di->format = UNW_INFO_FORMAT_DYNAMIC;
78 di->u.pi.name_ptr = (unw_word_t) name;
79 di->u.pi.regions = region;
85 main (int argc, char **argv)
87 extern long func_add1 (long);
88 extern char func_add1_end[];
89 extern long func_add3 (long, long (*[])());
90 extern char func_add3_end[];
91 extern long func_vframe (long);
92 extern char func_vframe_end[];
93 unw_dyn_region_info_t *r_pro, *r_epi, *r, *rtmp;
94 unw_dyn_info_t di0, di1, di2, di3;
96 long (*add3_0) (long);
97 long (*add3_1) (long, void *[]);
98 long (*vframe) (long);
103 signal (SIGUSR1, SIG_IGN);
104 signal (SIGUSR2, SIG_IGN);
109 add1 = (long (*)(long))
110 create_func (&di0, "func_add1", func_add1, func_add1_end, NULL);
112 /* Describe the epilogue of func_add3: */
114 r_epi = alloca (_U_dyn_region_info_size (5));
117 r_epi->insn_count = -9;
118 _U_dyn_op_pop_frames (&r_epi->op[i++],
119 _U_QP_TRUE, /* when=*/ 5, /* num_frames=*/ 1);
120 _U_dyn_op_stop (&r_epi->op[i++]);
121 assert ((unsigned) i <= r_epi->op_count);
123 /* Describe the prologue of func_add3: */
125 r_pro = alloca (_U_dyn_region_info_size (4));
128 r_pro->insn_count = 8;
129 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 0,
130 /* reg=*/ UNW_IA64_AR_PFS, /* dst=*/ UNW_IA64_GR + 34);
131 _U_dyn_op_add (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 2,
132 /* reg= */ UNW_IA64_SP, /* val=*/ -16);
133 _U_dyn_op_save_reg (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 4,
134 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + 3);
135 _U_dyn_op_spill_sp_rel (&r_pro->op[i++], _U_QP_TRUE, /* when=*/ 7,
136 /* reg=*/ UNW_IA64_RP, /* off=*/ 16);
137 assert ((unsigned) i <= r_pro->op_count);
139 /* Create regions for func_vframe: */
141 r = alloca (_U_dyn_region_info_size (16));
145 _U_dyn_op_label_state (&r->op[i++], /* label=*/ 100402);
146 _U_dyn_op_pop_frames (&r->op[i++], _U_QP_TRUE, /* when=*/ 3, /* frames=*/ 1);
147 _U_dyn_op_stop (&r->op[i++]);
148 assert ((unsigned) i <= r->op_count);
152 r = alloca (_U_dyn_region_info_size (16));
156 _U_dyn_op_save_reg (&r->op[i++], _U_QP_TRUE, /* when=*/ 8,
157 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + 3);
158 _U_dyn_op_pop_frames (&r->op[i++], _U_QP_TRUE, /* when=*/ 10,
160 _U_dyn_op_stop (&r->op[i++]);
161 assert ((unsigned) i <= r->op_count);
165 r = alloca (_U_dyn_region_info_size (16));
169 _U_dyn_op_save_reg (&r->op[i++], _U_QP_TRUE, /* when=*/ 1,
170 /* reg=*/ UNW_IA64_RP, /* dst=*/ UNW_IA64_GR + 33);
171 _U_dyn_op_save_reg (&r->op[i++], _U_QP_TRUE, /* when=*/ 2,
172 /* reg=*/ UNW_IA64_SP, /* dst=*/ UNW_IA64_GR + 34);
173 _U_dyn_op_spill_fp_rel (&r->op[i++], _U_QP_TRUE, /* when=*/ 4,
174 /* reg=*/ UNW_IA64_AR_PFS, /* off=*/ 16);
175 _U_dyn_op_label_state (&r->op[i++], /* label=*/ 100402);
176 _U_dyn_op_stop (&r->op[i++]);
177 assert ((unsigned) i <= r->op_count);
179 /* Create two functions which can share the region-list: */
180 add3_0 = (long (*) (long))
181 create_func (&di1, "func_add3/0", func_add3, func_add3_end, r_pro);
182 add3_1 = (long (*) (long, void *[]))
183 create_func (&di2, "func_add3/1", func_add3, func_add3_end, r_pro);
184 vframe = (long (*) (long))
185 create_func (&di3, "func_vframe", func_vframe, func_vframe_end, r);
187 _U_dyn_register (&di1);
188 _U_dyn_register (&di2);
189 _U_dyn_register (&di3);
190 _U_dyn_register (&di0);
195 kill (getpid (), SIGUSR1); /* do something ptmon can latch onto */
196 ret = (*add3_1) (13, flist);
199 fprintf (stderr, "FAILURE: (*add3_1)(13) returned %ld\n", ret);
203 ret = (*vframe) (48);
206 fprintf (stderr, "FAILURE: (*vframe)(16) returned %ld\n", ret);
209 ret = (*vframe) (64);
212 fprintf (stderr, "FAILURE: (*vframe)(32) returned %ld\n", ret);
215 kill (getpid (), SIGUSR2); /* do something ptmon can latch onto */
217 _U_dyn_cancel (&di0);
218 _U_dyn_cancel (&di1);
219 _U_dyn_cancel (&di3);
220 _U_dyn_cancel (&di2);