Statistics
| Branch: | Revision:

root / dyngen.h @ 480c1cdb

History | View | Annotate | Download (12.3 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 fdbb4691 bellard
#ifdef __sparc__
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 ff1f20a3 bellard
#define MAX_OP_SIZE    (128 * 4) /* in bytes */
149 ff1f20a3 bellard
/* max size of the code that can be generated without calling arm_flush_ldr */
150 ff1f20a3 bellard
#define MAX_FRAG_SIZE  (1024 * 4) 
151 ff1f20a3 bellard
//#define MAX_FRAG_SIZE  (135 * 4) /* for testing */ 
152 ff1f20a3 bellard
153 ff1f20a3 bellard
typedef struct LDREntry {
154 ff1f20a3 bellard
    uint8_t *ptr;
155 ff1f20a3 bellard
    uint32_t *data_ptr;
156 ff1f20a3 bellard
} LDREntry;
157 ff1f20a3 bellard
158 ff1f20a3 bellard
static LDREntry arm_ldr_table[1024];
159 ff1f20a3 bellard
static uint32_t arm_data_table[1024];
160 ff1f20a3 bellard
161 ff1f20a3 bellard
extern char exec_loop;
162 ff1f20a3 bellard
163 ff1f20a3 bellard
static inline void arm_reloc_pc24(uint32_t *ptr, uint32_t insn, int val)
164 ff1f20a3 bellard
{
165 ff1f20a3 bellard
    *ptr = (insn & ~0xffffff) | ((insn + ((val - (int)ptr) >> 2)) & 0xffffff);
166 ff1f20a3 bellard
}
167 ff1f20a3 bellard
168 ff1f20a3 bellard
static uint8_t *arm_flush_ldr(uint8_t *gen_code_ptr,
169 ff1f20a3 bellard
                              LDREntry *ldr_start, LDREntry *ldr_end, 
170 ff1f20a3 bellard
                              uint32_t *data_start, uint32_t *data_end, 
171 ff1f20a3 bellard
                              int gen_jmp)
172 ff1f20a3 bellard
{
173 ff1f20a3 bellard
    LDREntry *le;
174 ff1f20a3 bellard
    uint32_t *ptr;
175 ff1f20a3 bellard
    int offset, data_size, target;
176 ff1f20a3 bellard
    uint8_t *data_ptr;
177 ff1f20a3 bellard
    uint32_t insn;
178 ff1f20a3 bellard
 
179 ff1f20a3 bellard
    data_size = (uint8_t *)data_end - (uint8_t *)data_start;
180 ff1f20a3 bellard
181 9621339d bellard
    if (gen_jmp) {
182 ff1f20a3 bellard
        /* generate branch to skip the data */
183 ff1f20a3 bellard
        if (data_size == 0)
184 ff1f20a3 bellard
            return gen_code_ptr;
185 ff1f20a3 bellard
        target = (long)gen_code_ptr + data_size + 4;
186 ff1f20a3 bellard
        arm_reloc_pc24((uint32_t *)gen_code_ptr, 0xeafffffe, target);
187 ff1f20a3 bellard
        gen_code_ptr += 4;
188 ff1f20a3 bellard
    }
189 ff1f20a3 bellard
   
190 ff1f20a3 bellard
    /* copy the data */
191 ff1f20a3 bellard
    data_ptr = gen_code_ptr;
192 ff1f20a3 bellard
    memcpy(gen_code_ptr, data_start, data_size);
193 ff1f20a3 bellard
    gen_code_ptr += data_size;
194 ff1f20a3 bellard
    
195 ff1f20a3 bellard
    /* patch the ldr to point to the data */
196 ff1f20a3 bellard
    for(le = ldr_start; le < ldr_end; le++) {
197 ff1f20a3 bellard
        ptr = (uint32_t *)le->ptr;
198 ff1f20a3 bellard
        offset = ((unsigned long)(le->data_ptr) - (unsigned long)data_start) + 
199 ff1f20a3 bellard
            (unsigned long)data_ptr - 
200 ff1f20a3 bellard
            (unsigned long)ptr - 8;
201 ff1f20a3 bellard
        insn = *ptr & ~(0xfff | 0x00800000);
202 ff1f20a3 bellard
        if (offset < 0) {
203 ff1f20a3 bellard
            offset = - offset;
204 ff1f20a3 bellard
        } else {
205 ff1f20a3 bellard
            insn |= 0x00800000;
206 ff1f20a3 bellard
        }
207 ff1f20a3 bellard
        if (offset > 0xfff) {
208 ff1f20a3 bellard
            fprintf(stderr, "Error ldr offset\n");
209 ff1f20a3 bellard
            abort();
210 ff1f20a3 bellard
        }
211 ff1f20a3 bellard
        insn |= offset;
212 ff1f20a3 bellard
        *ptr = insn;
213 ff1f20a3 bellard
    }
214 ff1f20a3 bellard
    return gen_code_ptr;
215 ff1f20a3 bellard
}
216 ff1f20a3 bellard
217 ff1f20a3 bellard
#endif /* __arm__ */
218 b8076a74 bellard
219 b8076a74 bellard
#ifdef __ia64
220 b8076a74 bellard
221 b8076a74 bellard
222 b8076a74 bellard
/* Patch instruction with "val" where "mask" has 1 bits. */
223 b8076a74 bellard
static inline void ia64_patch (uint64_t insn_addr, uint64_t mask, uint64_t val)
224 b8076a74 bellard
{
225 b8076a74 bellard
    uint64_t m0, m1, v0, v1, b0, b1, *b = (uint64_t *) (insn_addr & -16);
226 b8076a74 bellard
#   define insn_mask ((1UL << 41) - 1)
227 b8076a74 bellard
    unsigned long shift;
228 b8076a74 bellard
229 b8076a74 bellard
    b0 = b[0]; b1 = b[1];
230 b8076a74 bellard
    shift = 5 + 41 * (insn_addr % 16); /* 5 template, 3 x 41-bit insns */
231 b8076a74 bellard
    if (shift >= 64) {
232 b8076a74 bellard
        m1 = mask << (shift - 64);
233 b8076a74 bellard
        v1 = val << (shift - 64);
234 b8076a74 bellard
    } else {
235 b8076a74 bellard
        m0 = mask << shift; m1 = mask >> (64 - shift);
236 b8076a74 bellard
        v0 = val  << shift; v1 = val >> (64 - shift);
237 b8076a74 bellard
        b[0] = (b0 & ~m0) | (v0 & m0);
238 b8076a74 bellard
    }
239 b8076a74 bellard
    b[1] = (b1 & ~m1) | (v1 & m1);
240 b8076a74 bellard
}
241 b8076a74 bellard
242 b8076a74 bellard
static inline void ia64_patch_imm60 (uint64_t insn_addr, uint64_t val)
243 b8076a74 bellard
{
244 b8076a74 bellard
        ia64_patch(insn_addr,
245 b8076a74 bellard
                   0x011ffffe000UL,
246 b8076a74 bellard
                   (  ((val & 0x0800000000000000UL) >> 23) /* bit 59 -> 36 */
247 b8076a74 bellard
                    | ((val & 0x00000000000fffffUL) << 13) /* bit 0 -> 13 */));
248 b8076a74 bellard
        ia64_patch(insn_addr - 1, 0x1fffffffffcUL, val >> 18);
249 b8076a74 bellard
}
250 b8076a74 bellard
251 b8076a74 bellard
static inline void ia64_imm64 (void *insn, uint64_t val)
252 b8076a74 bellard
{
253 b8076a74 bellard
    /* Ignore the slot number of the relocation; GCC and Intel
254 b8076a74 bellard
       toolchains differed for some time on whether IMM64 relocs are
255 b8076a74 bellard
       against slot 1 (Intel) or slot 2 (GCC).  */
256 b8076a74 bellard
    uint64_t insn_addr = (uint64_t) insn & ~3UL;
257 b8076a74 bellard
258 b8076a74 bellard
    ia64_patch(insn_addr + 2,
259 b8076a74 bellard
               0x01fffefe000UL,
260 b8076a74 bellard
               (  ((val & 0x8000000000000000UL) >> 27) /* bit 63 -> 36 */
261 b8076a74 bellard
                | ((val & 0x0000000000200000UL) <<  0) /* bit 21 -> 21 */
262 b8076a74 bellard
                | ((val & 0x00000000001f0000UL) <<  6) /* bit 16 -> 22 */
263 b8076a74 bellard
                | ((val & 0x000000000000ff80UL) << 20) /* bit  7 -> 27 */
264 b8076a74 bellard
                | ((val & 0x000000000000007fUL) << 13) /* bit  0 -> 13 */)
265 b8076a74 bellard
            );
266 b8076a74 bellard
    ia64_patch(insn_addr + 1, 0x1ffffffffffUL, val >> 22);
267 b8076a74 bellard
}
268 b8076a74 bellard
269 b8076a74 bellard
static inline void ia64_imm60b (void *insn, uint64_t val)
270 b8076a74 bellard
{
271 b8076a74 bellard
    /* Ignore the slot number of the relocation; GCC and Intel
272 b8076a74 bellard
       toolchains differed for some time on whether IMM64 relocs are
273 b8076a74 bellard
       against slot 1 (Intel) or slot 2 (GCC).  */
274 b8076a74 bellard
    uint64_t insn_addr = (uint64_t) insn & ~3UL;
275 b8076a74 bellard
276 b8076a74 bellard
    if (val + ((uint64_t) 1 << 59) >= (1UL << 60))
277 b8076a74 bellard
        fprintf(stderr, "%s: value %ld out of IMM60 range\n",
278 b8076a74 bellard
                __FUNCTION__, (int64_t) val);
279 b8076a74 bellard
    ia64_patch_imm60(insn_addr + 2, val);
280 b8076a74 bellard
}
281 b8076a74 bellard
282 b8076a74 bellard
static inline void ia64_imm22 (void *insn, uint64_t val)
283 b8076a74 bellard
{
284 b8076a74 bellard
    if (val + (1 << 21) >= (1 << 22))
285 b8076a74 bellard
        fprintf(stderr, "%s: value %li out of IMM22 range\n",
286 b8076a74 bellard
                __FUNCTION__, (int64_t)val);
287 b8076a74 bellard
    ia64_patch((uint64_t) insn, 0x01fffcfe000UL,
288 b8076a74 bellard
               (  ((val & 0x200000UL) << 15) /* bit 21 -> 36 */
289 b8076a74 bellard
                | ((val & 0x1f0000UL) <<  6) /* bit 16 -> 22 */
290 b8076a74 bellard
                | ((val & 0x00ff80UL) << 20) /* bit  7 -> 27 */
291 b8076a74 bellard
                | ((val & 0x00007fUL) << 13) /* bit  0 -> 13 */));
292 b8076a74 bellard
}
293 b8076a74 bellard
294 b8076a74 bellard
/* Like ia64_imm22(), but also clear bits 20-21.  For addl, this has
295 b8076a74 bellard
   the effect of turning "addl rX=imm22,rY" into "addl
296 b8076a74 bellard
   rX=imm22,r0".  */
297 b8076a74 bellard
static inline void ia64_imm22_r0 (void *insn, uint64_t val)
298 b8076a74 bellard
{
299 b8076a74 bellard
    if (val + (1 << 21) >= (1 << 22))
300 b8076a74 bellard
        fprintf(stderr, "%s: value %li out of IMM22 range\n",
301 b8076a74 bellard
                __FUNCTION__, (int64_t)val);
302 b8076a74 bellard
    ia64_patch((uint64_t) insn, 0x01fffcfe000UL | (0x3UL << 20),
303 b8076a74 bellard
               (  ((val & 0x200000UL) << 15) /* bit 21 -> 36 */
304 b8076a74 bellard
                | ((val & 0x1f0000UL) <<  6) /* bit 16 -> 22 */
305 b8076a74 bellard
                | ((val & 0x00ff80UL) << 20) /* bit  7 -> 27 */
306 b8076a74 bellard
                | ((val & 0x00007fUL) << 13) /* bit  0 -> 13 */));
307 b8076a74 bellard
}
308 b8076a74 bellard
309 b8076a74 bellard
static inline void ia64_imm21b (void *insn, uint64_t val)
310 b8076a74 bellard
{
311 b8076a74 bellard
    if (val + (1 << 20) >= (1 << 21))
312 b8076a74 bellard
        fprintf(stderr, "%s: value %li out of IMM21b range\n",
313 b8076a74 bellard
                __FUNCTION__, (int64_t)val);
314 b8076a74 bellard
    ia64_patch((uint64_t) insn, 0x11ffffe000UL,
315 b8076a74 bellard
               (  ((val & 0x100000UL) << 16) /* bit 20 -> 36 */
316 b8076a74 bellard
                | ((val & 0x0fffffUL) << 13) /* bit  0 -> 13 */));
317 b8076a74 bellard
}
318 b8076a74 bellard
319 b8076a74 bellard
static inline void ia64_nop_b (void *insn)
320 b8076a74 bellard
{
321 b8076a74 bellard
    ia64_patch((uint64_t) insn, (1UL << 41) - 1, 2UL << 37);
322 b8076a74 bellard
}
323 b8076a74 bellard
324 b8076a74 bellard
static inline void ia64_ldxmov(void *insn, uint64_t val)
325 b8076a74 bellard
{
326 b8076a74 bellard
    if (val + (1 << 21) < (1 << 22))
327 b8076a74 bellard
        ia64_patch((uint64_t) insn, 0x1fff80fe000UL, 8UL << 37);
328 b8076a74 bellard
}
329 b8076a74 bellard
330 b8076a74 bellard
static inline int ia64_patch_ltoff(void *insn, uint64_t val,
331 b8076a74 bellard
                                   int relaxable)
332 b8076a74 bellard
{
333 b8076a74 bellard
    if (relaxable && (val + (1 << 21) < (1 << 22))) {
334 b8076a74 bellard
        ia64_imm22_r0(insn, val);
335 b8076a74 bellard
        return 0;
336 b8076a74 bellard
    }
337 b8076a74 bellard
    return 1;
338 b8076a74 bellard
}
339 b8076a74 bellard
340 b8076a74 bellard
struct ia64_fixup {
341 b8076a74 bellard
    struct ia64_fixup *next;
342 b8076a74 bellard
    void *addr;                        /* address that needs to be patched */
343 b8076a74 bellard
    long value;
344 b8076a74 bellard
};
345 b8076a74 bellard
346 b8076a74 bellard
#define IA64_PLT(insn, plt_index)                        \
347 b8076a74 bellard
do {                                                        \
348 b8076a74 bellard
    struct ia64_fixup *fixup = alloca(sizeof(*fixup));        \
349 b8076a74 bellard
    fixup->next = plt_fixes;                                \
350 b8076a74 bellard
    plt_fixes = fixup;                                        \
351 b8076a74 bellard
    fixup->addr = (insn);                                \
352 b8076a74 bellard
    fixup->value = (plt_index);                                \
353 b8076a74 bellard
    plt_offset[(plt_index)] = 1;                        \
354 b8076a74 bellard
} while (0)
355 b8076a74 bellard
356 b8076a74 bellard
#define IA64_LTOFF(insn, val, relaxable)                        \
357 b8076a74 bellard
do {                                                                \
358 b8076a74 bellard
    if (ia64_patch_ltoff(insn, val, relaxable)) {                \
359 b8076a74 bellard
        struct ia64_fixup *fixup = alloca(sizeof(*fixup));        \
360 b8076a74 bellard
        fixup->next = ltoff_fixes;                                \
361 b8076a74 bellard
        ltoff_fixes = fixup;                                        \
362 b8076a74 bellard
        fixup->addr = (insn);                                        \
363 b8076a74 bellard
        fixup->value = (val);                                        \
364 b8076a74 bellard
    }                                                                \
365 b8076a74 bellard
} while (0)
366 b8076a74 bellard
367 b8076a74 bellard
static inline void ia64_apply_fixes (uint8_t **gen_code_pp,
368 b8076a74 bellard
                                     struct ia64_fixup *ltoff_fixes,
369 b8076a74 bellard
                                     uint64_t gp,
370 b8076a74 bellard
                                     struct ia64_fixup *plt_fixes,
371 b8076a74 bellard
                                     int num_plts,
372 b8076a74 bellard
                                     unsigned long *plt_target,
373 b8076a74 bellard
                                     unsigned int *plt_offset)
374 b8076a74 bellard
{
375 b8076a74 bellard
    static const uint8_t plt_bundle[] = {
376 b8076a74 bellard
        0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,        /* nop 0; movl r1=GP */
377 b8076a74 bellard
        0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x60,
378 b8076a74 bellard
379 b8076a74 bellard
        0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,        /* nop 0; brl IP */
380 b8076a74 bellard
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0
381 b8076a74 bellard
    };
382 b8076a74 bellard
    uint8_t *gen_code_ptr = *gen_code_pp, *plt_start, *got_start, *vp;
383 b8076a74 bellard
    struct ia64_fixup *fixup;
384 b8076a74 bellard
    unsigned int offset = 0;
385 b8076a74 bellard
    struct fdesc {
386 b8076a74 bellard
        long ip;
387 b8076a74 bellard
        long gp;
388 b8076a74 bellard
    } *fdesc;
389 b8076a74 bellard
    int i;
390 b8076a74 bellard
391 b8076a74 bellard
    if (plt_fixes) {
392 b8076a74 bellard
        plt_start = gen_code_ptr;
393 b8076a74 bellard
394 b8076a74 bellard
        for (i = 0; i < num_plts; ++i) {
395 b8076a74 bellard
            if (plt_offset[i]) {
396 b8076a74 bellard
                plt_offset[i] = offset;
397 b8076a74 bellard
                offset += sizeof(plt_bundle);
398 b8076a74 bellard
399 b8076a74 bellard
                fdesc = (struct fdesc *) plt_target[i];
400 b8076a74 bellard
                memcpy(gen_code_ptr, plt_bundle, sizeof(plt_bundle));
401 b8076a74 bellard
                ia64_imm64 (gen_code_ptr + 0x02, fdesc->gp);
402 b8076a74 bellard
                ia64_imm60b(gen_code_ptr + 0x12,
403 b8076a74 bellard
                            (fdesc->ip - (long) (gen_code_ptr + 0x10)) >> 4);
404 b8076a74 bellard
                gen_code_ptr += sizeof(plt_bundle);
405 b8076a74 bellard
            }
406 b8076a74 bellard
        }
407 b8076a74 bellard
408 b8076a74 bellard
        for (fixup = plt_fixes; fixup; fixup = fixup->next)
409 b8076a74 bellard
            ia64_imm21b(fixup->addr,
410 b8076a74 bellard
                        ((long) plt_start + plt_offset[fixup->value]
411 b8076a74 bellard
                         - ((long) fixup->addr & ~0xf)) >> 4);
412 b8076a74 bellard
    }
413 b8076a74 bellard
414 b8076a74 bellard
    got_start = gen_code_ptr;
415 b8076a74 bellard
416 b8076a74 bellard
    /* First, create the GOT: */
417 b8076a74 bellard
    for (fixup = ltoff_fixes; fixup; fixup = fixup->next) {
418 b8076a74 bellard
        /* first check if we already have this value in the GOT: */
419 b8076a74 bellard
        for (vp = got_start; vp < gen_code_ptr; ++vp)
420 b8076a74 bellard
            if (*(uint64_t *) vp == fixup->value)
421 b8076a74 bellard
                break;
422 b8076a74 bellard
        if (vp == gen_code_ptr) {
423 b8076a74 bellard
            /* Nope, we need to put the value in the GOT: */
424 b8076a74 bellard
            *(uint64_t *) vp = fixup->value;
425 b8076a74 bellard
            gen_code_ptr += 8;
426 b8076a74 bellard
        }
427 b8076a74 bellard
        ia64_imm22(fixup->addr, (long) vp - gp);
428 b8076a74 bellard
    }
429 fd4a43e4 bellard
    /* Keep code ptr aligned. */
430 fd4a43e4 bellard
    if ((long) gen_code_ptr & 15)
431 fd4a43e4 bellard
        gen_code_ptr += 8;
432 b8076a74 bellard
    *gen_code_pp = gen_code_ptr;
433 b8076a74 bellard
}
434 b8076a74 bellard
435 b8076a74 bellard
#endif