Statistics
| Branch: | Revision:

root / dyngen.h @ 19221bda

History | View | Annotate | Download (13.1 kB)

1 ff1f20a3 bellard
/*
2 ff1f20a3 bellard
 * dyngen helpers
3 ff1f20a3 bellard
 * 
4 ff1f20a3 bellard
 *  Copyright (c) 2003 Fabrice Bellard
5 ff1f20a3 bellard
 *
6 ff1f20a3 bellard
 * This library is free software; you can redistribute it and/or
7 ff1f20a3 bellard
 * modify it under the terms of the GNU Lesser General Public
8 ff1f20a3 bellard
 * License as published by the Free Software Foundation; either
9 ff1f20a3 bellard
 * version 2 of the License, or (at your option) any later version.
10 ff1f20a3 bellard
 *
11 ff1f20a3 bellard
 * This library is distributed in the hope that it will be useful,
12 ff1f20a3 bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ff1f20a3 bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ff1f20a3 bellard
 * Lesser General Public License for more details.
15 ff1f20a3 bellard
 *
16 ff1f20a3 bellard
 * You should have received a copy of the GNU Lesser General Public
17 ff1f20a3 bellard
 * License along with this library; if not, write to the Free Software
18 ff1f20a3 bellard
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 ff1f20a3 bellard
 */
20 ff1f20a3 bellard
21 03daf0e3 bellard
int __op_param1, __op_param2, __op_param3;
22 46152182 pbrook
#if defined(__sparc__) || defined(__arm__)
23 fdbb4691 bellard
  void __op_gen_label1(){}
24 fdbb4691 bellard
  void __op_gen_label2(){}
25 fdbb4691 bellard
  void __op_gen_label3(){}
26 fdbb4691 bellard
#else
27 fdbb4691 bellard
  int __op_gen_label1, __op_gen_label2, __op_gen_label3;
28 fdbb4691 bellard
#endif
29 c106152d bellard
int __op_jmp0, __op_jmp1, __op_jmp2, __op_jmp3;
30 03daf0e3 bellard
31 03daf0e3 bellard
#ifdef __i386__
32 03daf0e3 bellard
static inline void flush_icache_range(unsigned long start, unsigned long stop)
33 03daf0e3 bellard
{
34 03daf0e3 bellard
}
35 03daf0e3 bellard
#endif
36 03daf0e3 bellard
37 bc51c5c9 bellard
#ifdef __x86_64__
38 bc51c5c9 bellard
static inline void flush_icache_range(unsigned long start, unsigned long stop)
39 bc51c5c9 bellard
{
40 bc51c5c9 bellard
}
41 bc51c5c9 bellard
#endif
42 bc51c5c9 bellard
43 03daf0e3 bellard
#ifdef __s390__
44 03daf0e3 bellard
static inline void flush_icache_range(unsigned long start, unsigned long stop)
45 03daf0e3 bellard
{
46 03daf0e3 bellard
}
47 03daf0e3 bellard
#endif
48 03daf0e3 bellard
49 03daf0e3 bellard
#ifdef __ia64__
50 03daf0e3 bellard
static inline void flush_icache_range(unsigned long start, unsigned long stop)
51 03daf0e3 bellard
{
52 b8076a74 bellard
    while (start < stop) {
53 b8076a74 bellard
        asm volatile ("fc %0" :: "r"(start));
54 b8076a74 bellard
        start += 32;
55 b8076a74 bellard
    }
56 b8076a74 bellard
    asm volatile (";;sync.i;;srlz.i;;");
57 03daf0e3 bellard
}
58 03daf0e3 bellard
#endif
59 03daf0e3 bellard
60 03daf0e3 bellard
#ifdef __powerpc__
61 03daf0e3 bellard
62 03daf0e3 bellard
#define MIN_CACHE_LINE_SIZE 8 /* conservative value */
63 03daf0e3 bellard
64 03daf0e3 bellard
static void inline flush_icache_range(unsigned long start, unsigned long stop)
65 03daf0e3 bellard
{
66 03daf0e3 bellard
    unsigned long p;
67 03daf0e3 bellard
68 f5ba07d3 pbrook
    start &= ~(MIN_CACHE_LINE_SIZE - 1);
69 03daf0e3 bellard
    stop = (stop + MIN_CACHE_LINE_SIZE - 1) & ~(MIN_CACHE_LINE_SIZE - 1);
70 03daf0e3 bellard
    
71 03daf0e3 bellard
    for (p = start; p < stop; p += MIN_CACHE_LINE_SIZE) {
72 03daf0e3 bellard
        asm volatile ("dcbst 0,%0" : : "r"(p) : "memory");
73 03daf0e3 bellard
    }
74 03daf0e3 bellard
    asm volatile ("sync" : : : "memory");
75 03daf0e3 bellard
    for (p = start; p < stop; p += MIN_CACHE_LINE_SIZE) {
76 03daf0e3 bellard
        asm volatile ("icbi 0,%0" : : "r"(p) : "memory");
77 03daf0e3 bellard
    }
78 03daf0e3 bellard
    asm volatile ("sync" : : : "memory");
79 03daf0e3 bellard
    asm volatile ("isync" : : : "memory");
80 03daf0e3 bellard
}
81 03daf0e3 bellard
#endif
82 03daf0e3 bellard
83 03daf0e3 bellard
#ifdef __alpha__
84 03daf0e3 bellard
static inline void flush_icache_range(unsigned long start, unsigned long stop)
85 03daf0e3 bellard
{
86 03daf0e3 bellard
    asm ("imb");
87 03daf0e3 bellard
}
88 03daf0e3 bellard
#endif
89 03daf0e3 bellard
90 03daf0e3 bellard
#ifdef __sparc__
91 03daf0e3 bellard
92 03daf0e3 bellard
static void inline flush_icache_range(unsigned long start, unsigned long stop)
93 03daf0e3 bellard
{
94 03daf0e3 bellard
        unsigned long p;
95 03daf0e3 bellard
96 03daf0e3 bellard
        p = start & ~(8UL - 1UL);
97 03daf0e3 bellard
        stop = (stop + (8UL - 1UL)) & ~(8UL - 1UL);
98 03daf0e3 bellard
99 03daf0e3 bellard
        for (; p < stop; p += 8)
100 03daf0e3 bellard
                __asm__ __volatile__("flush\t%0" : : "r" (p));
101 03daf0e3 bellard
}
102 03daf0e3 bellard
103 03daf0e3 bellard
#endif
104 03daf0e3 bellard
105 03daf0e3 bellard
#ifdef __arm__
106 03daf0e3 bellard
static inline void flush_icache_range(unsigned long start, unsigned long stop)
107 03daf0e3 bellard
{
108 03daf0e3 bellard
    register unsigned long _beg __asm ("a1") = start;
109 03daf0e3 bellard
    register unsigned long _end __asm ("a2") = stop;
110 03daf0e3 bellard
    register unsigned long _flg __asm ("a3") = 0;
111 03daf0e3 bellard
    __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
112 03daf0e3 bellard
}
113 03daf0e3 bellard
#endif
114 03daf0e3 bellard
115 38e584a0 bellard
#ifdef __mc68000
116 38e584a0 bellard
#include <asm/cachectl.h>
117 38e584a0 bellard
static inline void flush_icache_range(unsigned long start, unsigned long stop)
118 38e584a0 bellard
{
119 38e584a0 bellard
    cacheflush(start,FLUSH_SCOPE_LINE,FLUSH_CACHE_BOTH,stop-start+16);
120 38e584a0 bellard
}
121 38e584a0 bellard
#endif
122 38e584a0 bellard
123 ff1f20a3 bellard
#ifdef __alpha__
124 ff1f20a3 bellard
125 ff1f20a3 bellard
register int gp asm("$29");
126 ff1f20a3 bellard
127 ff1f20a3 bellard
static inline void immediate_ldah(void *p, int val) {
128 ff1f20a3 bellard
    uint32_t *dest = p;
129 ff1f20a3 bellard
    long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff;
130 ff1f20a3 bellard
131 ff1f20a3 bellard
    *dest &= ~0xffff;
132 ff1f20a3 bellard
    *dest |= high;
133 ff1f20a3 bellard
    *dest |= 31 << 16;
134 ff1f20a3 bellard
}
135 ff1f20a3 bellard
static inline void immediate_lda(void *dest, int val) {
136 ff1f20a3 bellard
    *(uint16_t *) dest = val;
137 ff1f20a3 bellard
}
138 ff1f20a3 bellard
void fix_bsr(void *p, int offset) {
139 ff1f20a3 bellard
    uint32_t *dest = p;
140 ff1f20a3 bellard
    *dest &= ~((1 << 21) - 1);
141 ff1f20a3 bellard
    *dest |= (offset >> 2) & ((1 << 21) - 1);
142 ff1f20a3 bellard
}
143 ff1f20a3 bellard
144 ff1f20a3 bellard
#endif /* __alpha__ */
145 ff1f20a3 bellard
146 ff1f20a3 bellard
#ifdef __arm__
147 ff1f20a3 bellard
148 46152182 pbrook
#define ARM_LDR_TABLE_SIZE 1024
149 ff1f20a3 bellard
150 ff1f20a3 bellard
typedef struct LDREntry {
151 ff1f20a3 bellard
    uint8_t *ptr;
152 ff1f20a3 bellard
    uint32_t *data_ptr;
153 46152182 pbrook
    unsigned type:2;
154 ff1f20a3 bellard
} LDREntry;
155 ff1f20a3 bellard
156 ff1f20a3 bellard
static LDREntry arm_ldr_table[1024];
157 46152182 pbrook
static uint32_t arm_data_table[ARM_LDR_TABLE_SIZE];
158 ff1f20a3 bellard
159 ff1f20a3 bellard
extern char exec_loop;
160 ff1f20a3 bellard
161 ff1f20a3 bellard
static inline void arm_reloc_pc24(uint32_t *ptr, uint32_t insn, int val)
162 ff1f20a3 bellard
{
163 ff1f20a3 bellard
    *ptr = (insn & ~0xffffff) | ((insn + ((val - (int)ptr) >> 2)) & 0xffffff);
164 ff1f20a3 bellard
}
165 ff1f20a3 bellard
166 ff1f20a3 bellard
static uint8_t *arm_flush_ldr(uint8_t *gen_code_ptr,
167 ff1f20a3 bellard
                              LDREntry *ldr_start, LDREntry *ldr_end, 
168 ff1f20a3 bellard
                              uint32_t *data_start, uint32_t *data_end, 
169 ff1f20a3 bellard
                              int gen_jmp)
170 ff1f20a3 bellard
{
171 ff1f20a3 bellard
    LDREntry *le;
172 ff1f20a3 bellard
    uint32_t *ptr;
173 ff1f20a3 bellard
    int offset, data_size, target;
174 ff1f20a3 bellard
    uint8_t *data_ptr;
175 ff1f20a3 bellard
    uint32_t insn;
176 46152182 pbrook
    uint32_t mask;
177 ff1f20a3 bellard
 
178 46152182 pbrook
    data_size = (data_end - data_start) << 2;
179 ff1f20a3 bellard
180 9621339d bellard
    if (gen_jmp) {
181 ff1f20a3 bellard
        /* generate branch to skip the data */
182 ff1f20a3 bellard
        if (data_size == 0)
183 ff1f20a3 bellard
            return gen_code_ptr;
184 ff1f20a3 bellard
        target = (long)gen_code_ptr + data_size + 4;
185 ff1f20a3 bellard
        arm_reloc_pc24((uint32_t *)gen_code_ptr, 0xeafffffe, target);
186 ff1f20a3 bellard
        gen_code_ptr += 4;
187 ff1f20a3 bellard
    }
188 ff1f20a3 bellard
   
189 ff1f20a3 bellard
    /* copy the data */
190 ff1f20a3 bellard
    data_ptr = gen_code_ptr;
191 ff1f20a3 bellard
    memcpy(gen_code_ptr, data_start, data_size);
192 ff1f20a3 bellard
    gen_code_ptr += data_size;
193 ff1f20a3 bellard
    
194 ff1f20a3 bellard
    /* patch the ldr to point to the data */
195 ff1f20a3 bellard
    for(le = ldr_start; le < ldr_end; le++) {
196 ff1f20a3 bellard
        ptr = (uint32_t *)le->ptr;
197 ff1f20a3 bellard
        offset = ((unsigned long)(le->data_ptr) - (unsigned long)data_start) + 
198 ff1f20a3 bellard
            (unsigned long)data_ptr - 
199 ff1f20a3 bellard
            (unsigned long)ptr - 8;
200 ff1f20a3 bellard
        if (offset < 0) {
201 46152182 pbrook
            fprintf(stderr, "Negative constant pool offset\n");
202 ff1f20a3 bellard
            abort();
203 ff1f20a3 bellard
        }
204 46152182 pbrook
        switch (le->type) {
205 46152182 pbrook
          case 0: /* ldr */
206 46152182 pbrook
            mask = ~0x00800fff;
207 46152182 pbrook
            if (offset >= 4096) {
208 46152182 pbrook
                fprintf(stderr, "Bad ldr offset\n");
209 46152182 pbrook
                abort();
210 46152182 pbrook
            }
211 46152182 pbrook
            break;
212 46152182 pbrook
          case 1: /* ldc */
213 46152182 pbrook
            mask = ~0x008000ff;
214 46152182 pbrook
            if (offset >= 1024 ) {
215 46152182 pbrook
                fprintf(stderr, "Bad ldc offset\n");
216 46152182 pbrook
                abort();
217 46152182 pbrook
            }
218 46152182 pbrook
            break;
219 46152182 pbrook
          case 2: /* add */
220 46152182 pbrook
            mask = ~0xfff;
221 46152182 pbrook
            if (offset >= 1024 ) {
222 46152182 pbrook
                fprintf(stderr, "Bad add offset\n");
223 46152182 pbrook
                abort();
224 46152182 pbrook
            }
225 46152182 pbrook
            break;
226 46152182 pbrook
          default:
227 46152182 pbrook
            fprintf(stderr, "Bad pc relative fixup\n");
228 46152182 pbrook
            abort();
229 46152182 pbrook
          }
230 46152182 pbrook
        insn = *ptr & mask;
231 46152182 pbrook
        switch (le->type) {
232 46152182 pbrook
          case 0: /* ldr */
233 46152182 pbrook
            insn |= offset | 0x00800000;
234 46152182 pbrook
            break;
235 46152182 pbrook
          case 1: /* ldc */
236 46152182 pbrook
            insn |= (offset >> 2) | 0x00800000;
237 46152182 pbrook
            break;
238 46152182 pbrook
          case 2: /* add */
239 46152182 pbrook
            insn |= (offset >> 2) | 0xf00;
240 46152182 pbrook
            break;
241 46152182 pbrook
          }
242 ff1f20a3 bellard
        *ptr = insn;
243 ff1f20a3 bellard
    }
244 ff1f20a3 bellard
    return gen_code_ptr;
245 ff1f20a3 bellard
}
246 ff1f20a3 bellard
247 ff1f20a3 bellard
#endif /* __arm__ */
248 b8076a74 bellard
249 b8076a74 bellard
#ifdef __ia64
250 b8076a74 bellard
251 b8076a74 bellard
252 b8076a74 bellard
/* Patch instruction with "val" where "mask" has 1 bits. */
253 b8076a74 bellard
static inline void ia64_patch (uint64_t insn_addr, uint64_t mask, uint64_t val)
254 b8076a74 bellard
{
255 b8076a74 bellard
    uint64_t m0, m1, v0, v1, b0, b1, *b = (uint64_t *) (insn_addr & -16);
256 b8076a74 bellard
#   define insn_mask ((1UL << 41) - 1)
257 b8076a74 bellard
    unsigned long shift;
258 b8076a74 bellard
259 b8076a74 bellard
    b0 = b[0]; b1 = b[1];
260 b8076a74 bellard
    shift = 5 + 41 * (insn_addr % 16); /* 5 template, 3 x 41-bit insns */
261 b8076a74 bellard
    if (shift >= 64) {
262 b8076a74 bellard
        m1 = mask << (shift - 64);
263 b8076a74 bellard
        v1 = val << (shift - 64);
264 b8076a74 bellard
    } else {
265 b8076a74 bellard
        m0 = mask << shift; m1 = mask >> (64 - shift);
266 b8076a74 bellard
        v0 = val  << shift; v1 = val >> (64 - shift);
267 b8076a74 bellard
        b[0] = (b0 & ~m0) | (v0 & m0);
268 b8076a74 bellard
    }
269 b8076a74 bellard
    b[1] = (b1 & ~m1) | (v1 & m1);
270 b8076a74 bellard
}
271 b8076a74 bellard
272 b8076a74 bellard
static inline void ia64_patch_imm60 (uint64_t insn_addr, uint64_t val)
273 b8076a74 bellard
{
274 b8076a74 bellard
        ia64_patch(insn_addr,
275 b8076a74 bellard
                   0x011ffffe000UL,
276 b8076a74 bellard
                   (  ((val & 0x0800000000000000UL) >> 23) /* bit 59 -> 36 */
277 b8076a74 bellard
                    | ((val & 0x00000000000fffffUL) << 13) /* bit 0 -> 13 */));
278 b8076a74 bellard
        ia64_patch(insn_addr - 1, 0x1fffffffffcUL, val >> 18);
279 b8076a74 bellard
}
280 b8076a74 bellard
281 b8076a74 bellard
static inline void ia64_imm64 (void *insn, uint64_t val)
282 b8076a74 bellard
{
283 b8076a74 bellard
    /* Ignore the slot number of the relocation; GCC and Intel
284 b8076a74 bellard
       toolchains differed for some time on whether IMM64 relocs are
285 b8076a74 bellard
       against slot 1 (Intel) or slot 2 (GCC).  */
286 b8076a74 bellard
    uint64_t insn_addr = (uint64_t) insn & ~3UL;
287 b8076a74 bellard
288 b8076a74 bellard
    ia64_patch(insn_addr + 2,
289 b8076a74 bellard
               0x01fffefe000UL,
290 b8076a74 bellard
               (  ((val & 0x8000000000000000UL) >> 27) /* bit 63 -> 36 */
291 b8076a74 bellard
                | ((val & 0x0000000000200000UL) <<  0) /* bit 21 -> 21 */
292 b8076a74 bellard
                | ((val & 0x00000000001f0000UL) <<  6) /* bit 16 -> 22 */
293 b8076a74 bellard
                | ((val & 0x000000000000ff80UL) << 20) /* bit  7 -> 27 */
294 b8076a74 bellard
                | ((val & 0x000000000000007fUL) << 13) /* bit  0 -> 13 */)
295 b8076a74 bellard
            );
296 b8076a74 bellard
    ia64_patch(insn_addr + 1, 0x1ffffffffffUL, val >> 22);
297 b8076a74 bellard
}
298 b8076a74 bellard
299 b8076a74 bellard
static inline void ia64_imm60b (void *insn, uint64_t val)
300 b8076a74 bellard
{
301 b8076a74 bellard
    /* Ignore the slot number of the relocation; GCC and Intel
302 b8076a74 bellard
       toolchains differed for some time on whether IMM64 relocs are
303 b8076a74 bellard
       against slot 1 (Intel) or slot 2 (GCC).  */
304 b8076a74 bellard
    uint64_t insn_addr = (uint64_t) insn & ~3UL;
305 b8076a74 bellard
306 b8076a74 bellard
    if (val + ((uint64_t) 1 << 59) >= (1UL << 60))
307 b8076a74 bellard
        fprintf(stderr, "%s: value %ld out of IMM60 range\n",
308 b8076a74 bellard
                __FUNCTION__, (int64_t) val);
309 b8076a74 bellard
    ia64_patch_imm60(insn_addr + 2, val);
310 b8076a74 bellard
}
311 b8076a74 bellard
312 b8076a74 bellard
static inline void ia64_imm22 (void *insn, uint64_t val)
313 b8076a74 bellard
{
314 b8076a74 bellard
    if (val + (1 << 21) >= (1 << 22))
315 b8076a74 bellard
        fprintf(stderr, "%s: value %li out of IMM22 range\n",
316 b8076a74 bellard
                __FUNCTION__, (int64_t)val);
317 b8076a74 bellard
    ia64_patch((uint64_t) insn, 0x01fffcfe000UL,
318 b8076a74 bellard
               (  ((val & 0x200000UL) << 15) /* bit 21 -> 36 */
319 b8076a74 bellard
                | ((val & 0x1f0000UL) <<  6) /* bit 16 -> 22 */
320 b8076a74 bellard
                | ((val & 0x00ff80UL) << 20) /* bit  7 -> 27 */
321 b8076a74 bellard
                | ((val & 0x00007fUL) << 13) /* bit  0 -> 13 */));
322 b8076a74 bellard
}
323 b8076a74 bellard
324 b8076a74 bellard
/* Like ia64_imm22(), but also clear bits 20-21.  For addl, this has
325 b8076a74 bellard
   the effect of turning "addl rX=imm22,rY" into "addl
326 b8076a74 bellard
   rX=imm22,r0".  */
327 b8076a74 bellard
static inline void ia64_imm22_r0 (void *insn, uint64_t val)
328 b8076a74 bellard
{
329 b8076a74 bellard
    if (val + (1 << 21) >= (1 << 22))
330 b8076a74 bellard
        fprintf(stderr, "%s: value %li out of IMM22 range\n",
331 b8076a74 bellard
                __FUNCTION__, (int64_t)val);
332 b8076a74 bellard
    ia64_patch((uint64_t) insn, 0x01fffcfe000UL | (0x3UL << 20),
333 b8076a74 bellard
               (  ((val & 0x200000UL) << 15) /* bit 21 -> 36 */
334 b8076a74 bellard
                | ((val & 0x1f0000UL) <<  6) /* bit 16 -> 22 */
335 b8076a74 bellard
                | ((val & 0x00ff80UL) << 20) /* bit  7 -> 27 */
336 b8076a74 bellard
                | ((val & 0x00007fUL) << 13) /* bit  0 -> 13 */));
337 b8076a74 bellard
}
338 b8076a74 bellard
339 b8076a74 bellard
static inline void ia64_imm21b (void *insn, uint64_t val)
340 b8076a74 bellard
{
341 b8076a74 bellard
    if (val + (1 << 20) >= (1 << 21))
342 b8076a74 bellard
        fprintf(stderr, "%s: value %li out of IMM21b range\n",
343 b8076a74 bellard
                __FUNCTION__, (int64_t)val);
344 b8076a74 bellard
    ia64_patch((uint64_t) insn, 0x11ffffe000UL,
345 b8076a74 bellard
               (  ((val & 0x100000UL) << 16) /* bit 20 -> 36 */
346 b8076a74 bellard
                | ((val & 0x0fffffUL) << 13) /* bit  0 -> 13 */));
347 b8076a74 bellard
}
348 b8076a74 bellard
349 b8076a74 bellard
static inline void ia64_nop_b (void *insn)
350 b8076a74 bellard
{
351 b8076a74 bellard
    ia64_patch((uint64_t) insn, (1UL << 41) - 1, 2UL << 37);
352 b8076a74 bellard
}
353 b8076a74 bellard
354 b8076a74 bellard
static inline void ia64_ldxmov(void *insn, uint64_t val)
355 b8076a74 bellard
{
356 b8076a74 bellard
    if (val + (1 << 21) < (1 << 22))
357 b8076a74 bellard
        ia64_patch((uint64_t) insn, 0x1fff80fe000UL, 8UL << 37);
358 b8076a74 bellard
}
359 b8076a74 bellard
360 b8076a74 bellard
static inline int ia64_patch_ltoff(void *insn, uint64_t val,
361 b8076a74 bellard
                                   int relaxable)
362 b8076a74 bellard
{
363 b8076a74 bellard
    if (relaxable && (val + (1 << 21) < (1 << 22))) {
364 b8076a74 bellard
        ia64_imm22_r0(insn, val);
365 b8076a74 bellard
        return 0;
366 b8076a74 bellard
    }
367 b8076a74 bellard
    return 1;
368 b8076a74 bellard
}
369 b8076a74 bellard
370 b8076a74 bellard
struct ia64_fixup {
371 b8076a74 bellard
    struct ia64_fixup *next;
372 b8076a74 bellard
    void *addr;                        /* address that needs to be patched */
373 b8076a74 bellard
    long value;
374 b8076a74 bellard
};
375 b8076a74 bellard
376 b8076a74 bellard
#define IA64_PLT(insn, plt_index)                        \
377 b8076a74 bellard
do {                                                        \
378 b8076a74 bellard
    struct ia64_fixup *fixup = alloca(sizeof(*fixup));        \
379 b8076a74 bellard
    fixup->next = plt_fixes;                                \
380 b8076a74 bellard
    plt_fixes = fixup;                                        \
381 b8076a74 bellard
    fixup->addr = (insn);                                \
382 b8076a74 bellard
    fixup->value = (plt_index);                                \
383 b8076a74 bellard
    plt_offset[(plt_index)] = 1;                        \
384 b8076a74 bellard
} while (0)
385 b8076a74 bellard
386 b8076a74 bellard
#define IA64_LTOFF(insn, val, relaxable)                        \
387 b8076a74 bellard
do {                                                                \
388 b8076a74 bellard
    if (ia64_patch_ltoff(insn, val, relaxable)) {                \
389 b8076a74 bellard
        struct ia64_fixup *fixup = alloca(sizeof(*fixup));        \
390 b8076a74 bellard
        fixup->next = ltoff_fixes;                                \
391 b8076a74 bellard
        ltoff_fixes = fixup;                                        \
392 b8076a74 bellard
        fixup->addr = (insn);                                        \
393 b8076a74 bellard
        fixup->value = (val);                                        \
394 b8076a74 bellard
    }                                                                \
395 b8076a74 bellard
} while (0)
396 b8076a74 bellard
397 b8076a74 bellard
static inline void ia64_apply_fixes (uint8_t **gen_code_pp,
398 b8076a74 bellard
                                     struct ia64_fixup *ltoff_fixes,
399 b8076a74 bellard
                                     uint64_t gp,
400 b8076a74 bellard
                                     struct ia64_fixup *plt_fixes,
401 b8076a74 bellard
                                     int num_plts,
402 b8076a74 bellard
                                     unsigned long *plt_target,
403 b8076a74 bellard
                                     unsigned int *plt_offset)
404 b8076a74 bellard
{
405 b8076a74 bellard
    static const uint8_t plt_bundle[] = {
406 b8076a74 bellard
        0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,        /* nop 0; movl r1=GP */
407 b8076a74 bellard
        0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x60,
408 b8076a74 bellard
409 b8076a74 bellard
        0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,        /* nop 0; brl IP */
410 b8076a74 bellard
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0
411 b8076a74 bellard
    };
412 b8076a74 bellard
    uint8_t *gen_code_ptr = *gen_code_pp, *plt_start, *got_start, *vp;
413 b8076a74 bellard
    struct ia64_fixup *fixup;
414 b8076a74 bellard
    unsigned int offset = 0;
415 b8076a74 bellard
    struct fdesc {
416 b8076a74 bellard
        long ip;
417 b8076a74 bellard
        long gp;
418 b8076a74 bellard
    } *fdesc;
419 b8076a74 bellard
    int i;
420 b8076a74 bellard
421 b8076a74 bellard
    if (plt_fixes) {
422 b8076a74 bellard
        plt_start = gen_code_ptr;
423 b8076a74 bellard
424 b8076a74 bellard
        for (i = 0; i < num_plts; ++i) {
425 b8076a74 bellard
            if (plt_offset[i]) {
426 b8076a74 bellard
                plt_offset[i] = offset;
427 b8076a74 bellard
                offset += sizeof(plt_bundle);
428 b8076a74 bellard
429 b8076a74 bellard
                fdesc = (struct fdesc *) plt_target[i];
430 b8076a74 bellard
                memcpy(gen_code_ptr, plt_bundle, sizeof(plt_bundle));
431 b8076a74 bellard
                ia64_imm64 (gen_code_ptr + 0x02, fdesc->gp);
432 b8076a74 bellard
                ia64_imm60b(gen_code_ptr + 0x12,
433 b8076a74 bellard
                            (fdesc->ip - (long) (gen_code_ptr + 0x10)) >> 4);
434 b8076a74 bellard
                gen_code_ptr += sizeof(plt_bundle);
435 b8076a74 bellard
            }
436 b8076a74 bellard
        }
437 b8076a74 bellard
438 b8076a74 bellard
        for (fixup = plt_fixes; fixup; fixup = fixup->next)
439 b8076a74 bellard
            ia64_imm21b(fixup->addr,
440 b8076a74 bellard
                        ((long) plt_start + plt_offset[fixup->value]
441 b8076a74 bellard
                         - ((long) fixup->addr & ~0xf)) >> 4);
442 b8076a74 bellard
    }
443 b8076a74 bellard
444 b8076a74 bellard
    got_start = gen_code_ptr;
445 b8076a74 bellard
446 b8076a74 bellard
    /* First, create the GOT: */
447 b8076a74 bellard
    for (fixup = ltoff_fixes; fixup; fixup = fixup->next) {
448 b8076a74 bellard
        /* first check if we already have this value in the GOT: */
449 b8076a74 bellard
        for (vp = got_start; vp < gen_code_ptr; ++vp)
450 b8076a74 bellard
            if (*(uint64_t *) vp == fixup->value)
451 b8076a74 bellard
                break;
452 b8076a74 bellard
        if (vp == gen_code_ptr) {
453 b8076a74 bellard
            /* Nope, we need to put the value in the GOT: */
454 b8076a74 bellard
            *(uint64_t *) vp = fixup->value;
455 b8076a74 bellard
            gen_code_ptr += 8;
456 b8076a74 bellard
        }
457 b8076a74 bellard
        ia64_imm22(fixup->addr, (long) vp - gp);
458 b8076a74 bellard
    }
459 fd4a43e4 bellard
    /* Keep code ptr aligned. */
460 fd4a43e4 bellard
    if ((long) gen_code_ptr & 15)
461 fd4a43e4 bellard
        gen_code_ptr += 8;
462 b8076a74 bellard
    *gen_code_pp = gen_code_ptr;
463 b8076a74 bellard
}
464 b8076a74 bellard
465 b8076a74 bellard
#endif