2 * Simple test for x86_64 rip relative access that can also be useful
3 * on other ports when data is close to instruction pointer.
16 # define MAP_ANON MAP_ANONYMOUS
17 # ifndef MAP_ANONYMOUS
18 # define MAP_ANONYMOUS 0
27 main(int argc, char *argv[])
35 void (*function)(void);
36 int mmap_prot, mmap_flags, result, pagesize;
44 pagesize = sysconf(_SC_PAGESIZE);
49 mmap_fd = open("/dev/zero", O_RDWR);
52 mmap_prot = PROT_READ | PROT_WRITE;
53 #if !(__OpenBSD__ || __APPLE__)
54 mmap_prot |= PROT_EXEC;
57 mmap_prot = PROT_MPROTECT(mmap_prot);
60 mmap_flags = MAP_PRIVATE;
62 mmap_flags |= MAP_ANON;
63 ptr = mmap(NULL, pagesize * mult, mmap_prot, mmap_flags, mmap_fd, 0);
64 assert(ptr != MAP_FAILED);
70 _jit = jit_new_state();
73 jit_movi(JIT_R0, 'c');
74 jit_sti_c(ptr + 0, JIT_R0);
75 jit_movi(JIT_R0, 'C');
76 jit_sti_c(ptr + 1, JIT_R0);
78 jit_movi(JIT_R0, 's');
79 jit_sti_s(ptr + 2, JIT_R0);
80 jit_movi(JIT_R0, 'S');
81 jit_sti_s(ptr + 4, JIT_R0);
83 jit_movi(JIT_R0, 'i');
84 jit_sti_i(ptr + 8, JIT_R0);
86 jit_movi(JIT_R0, 'I');
87 jit_sti_i(ptr + 12, JIT_R0);
89 jit_movi(JIT_R0, 'l');
90 jit_sti_l(ptr + 16, JIT_R0);
92 jit_movi_f(JIT_F0, 1.0);
93 jit_sti_f(ptr + 24, JIT_F0);
94 jit_movi_d(JIT_F0, 2.0);
95 jit_sti_d(ptr + 32, JIT_F0);
97 jit_ldi_c(JIT_R0, ptr + 0);
98 jit_ldi_s(JIT_R1, ptr + 2);
99 jit_ldi_i(JIT_R2, ptr + 8);
101 jit_ldi_l(JIT_V0, ptr + 16);
105 jit_pushargi((jit_word_t)"%c %c %c %c\n");
107 jit_pushargi((jit_word_t)"%c %c %c l\n");
110 jit_pushargr(JIT_R0);
111 jit_pushargr(JIT_R1);
112 jit_pushargr(JIT_R2);
114 jit_pushargr(JIT_V0);
118 jit_ldi_uc(JIT_R0, ptr + 1);
119 jit_ldi_us(JIT_R1, ptr + 4);
121 jit_ldi_ui(JIT_R2, ptr + 12);
125 jit_pushargi((jit_word_t)"%c %c %c\n");
127 jit_pushargi((jit_word_t)"%c %c I\n");
130 jit_pushargr(JIT_R0);
131 jit_pushargr(JIT_R1);
133 jit_pushargr(JIT_R2);
137 jit_ldi_f(JIT_F0, ptr + 24);
138 jit_extr_f_d(JIT_F0, JIT_F0);
139 jit_ldi_d(JIT_F1, ptr + 32);
142 jit_pushargi((jit_word_t)"%.1f %.1f\n");
144 jit_pushargr_d(JIT_F0);
145 jit_pushargr_d(JIT_F1);
150 jit_set_code(ptr + pagesize, pagesize * (mult - 1));
152 #if __NetBSD__ || __OpenBSD__ || __APPLE__
153 result = mprotect(ptr, pagesize, PROT_READ | PROT_WRITE);
156 function = jit_emit();
157 if (function == NULL)
162 #if __NetBSD__ || __OpenBSD__ || __APPLE__
163 result = mprotect(ptr + pagesize, pagesize, PROT_READ | PROT_EXEC);
170 munmap(ptr, pagesize * mult);