Commit ff1f20a3ee3fe484bcf5147a124c4e4e878907ba
1 parent
9c5d1246
arm support - suppressed possibly unsafe sparc nop deletion
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@225 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
2 changed files
with
316 additions
and
20 deletions
dyngen.c
... | ... | @@ -79,6 +79,13 @@ |
79 | 79 | #define elf_check_arch(x) ((x) == EM_SPARCV9) |
80 | 80 | #define ELF_USES_RELOCA |
81 | 81 | |
82 | +#elif defined(HOST_ARM) | |
83 | + | |
84 | +#define ELF_CLASS ELFCLASS32 | |
85 | +#define ELF_ARCH EM_ARM | |
86 | +#define elf_check_arch(x) ((x) == EM_ARM) | |
87 | +#define ELF_USES_RELOC | |
88 | + | |
82 | 89 | #else |
83 | 90 | #error unsupported CPU - please update the code |
84 | 91 | #endif |
... | ... | @@ -301,6 +308,106 @@ int strstart(const char *str, const char *val, const char **ptr) |
301 | 308 | return 1; |
302 | 309 | } |
303 | 310 | |
311 | +#ifdef HOST_ARM | |
312 | + | |
313 | +int arm_emit_ldr_info(const char *name, unsigned long start_offset, | |
314 | + FILE *outfile, uint8_t *p_start, uint8_t *p_end, | |
315 | + ELF_RELOC *relocs, int nb_relocs) | |
316 | +{ | |
317 | + uint8_t *p; | |
318 | + uint32_t insn; | |
319 | + int offset, min_offset, pc_offset, data_size; | |
320 | + uint8_t data_allocated[1024]; | |
321 | + unsigned int data_index; | |
322 | + | |
323 | + memset(data_allocated, 0, sizeof(data_allocated)); | |
324 | + | |
325 | + p = p_start; | |
326 | + min_offset = p_end - p_start; | |
327 | + while (p < p_start + min_offset) { | |
328 | + insn = get32((uint32_t *)p); | |
329 | + if ((insn & 0x0d5f0000) == 0x051f0000) { | |
330 | + /* ldr reg, [pc, #im] */ | |
331 | + offset = insn & 0xfff; | |
332 | + if (!(insn & 0x00800000)) | |
333 | + offset = -offset; | |
334 | + if ((offset & 3) !=0) | |
335 | + error("%s:%04x: ldr pc offset must be 32 bit aligned", | |
336 | + name, start_offset + p - p_start); | |
337 | + pc_offset = p - p_start + offset + 8; | |
338 | + if (pc_offset <= (p - p_start) || | |
339 | + pc_offset >= (p_end - p_start)) | |
340 | + error("%s:%04x: ldr pc offset must point inside the function code", | |
341 | + name, start_offset + p - p_start); | |
342 | + if (pc_offset < min_offset) | |
343 | + min_offset = pc_offset; | |
344 | + if (outfile) { | |
345 | + /* ldr position */ | |
346 | + fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n", | |
347 | + p - p_start); | |
348 | + /* ldr data index */ | |
349 | + data_index = ((p_end - p_start) - pc_offset - 4) >> 2; | |
350 | + fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n", | |
351 | + data_index); | |
352 | + fprintf(outfile, " arm_ldr_ptr++;\n"); | |
353 | + if (data_index >= sizeof(data_allocated)) | |
354 | + error("%s: too many data", name); | |
355 | + if (!data_allocated[data_index]) { | |
356 | + ELF_RELOC *rel; | |
357 | + int i, addend, type; | |
358 | + const char *sym_name, *p; | |
359 | + char relname[1024]; | |
360 | + | |
361 | + data_allocated[data_index] = 1; | |
362 | + | |
363 | + /* data value */ | |
364 | + addend = get32((uint32_t *)(p_start + pc_offset)); | |
365 | + relname[0] = '\0'; | |
366 | + for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
367 | + if (rel->r_offset == (pc_offset + start_offset)) { | |
368 | + sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; | |
369 | + /* the compiler leave some unnecessary references to the code */ | |
370 | + if (strstart(sym_name, "__op_param", &p)) { | |
371 | + snprintf(relname, sizeof(relname), "param%s", p); | |
372 | + } else { | |
373 | + snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name); | |
374 | + } | |
375 | + type = ELF32_R_TYPE(rel->r_info); | |
376 | + if (type != R_ARM_ABS32) | |
377 | + error("%s: unsupported data relocation", name); | |
378 | + break; | |
379 | + } | |
380 | + } | |
381 | + fprintf(outfile, " arm_data_ptr[%d] = 0x%x", | |
382 | + data_index, addend); | |
383 | + if (relname[0] != '\0') | |
384 | + fprintf(outfile, " + %s", relname); | |
385 | + fprintf(outfile, ";\n"); | |
386 | + } | |
387 | + } | |
388 | + } | |
389 | + p += 4; | |
390 | + } | |
391 | + data_size = (p_end - p_start) - min_offset; | |
392 | + if (data_size > 0 && outfile) { | |
393 | + fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2); | |
394 | + } | |
395 | + | |
396 | + /* the last instruction must be a mov pc, lr */ | |
397 | + if (p == p_start) | |
398 | + goto arm_ret_error; | |
399 | + p -= 4; | |
400 | + insn = get32((uint32_t *)p); | |
401 | + if ((insn & 0xffff0000) != 0xe91b0000) { | |
402 | + arm_ret_error: | |
403 | + if (!outfile) | |
404 | + printf("%s: invalid epilog\n", name); | |
405 | + } | |
406 | + return p - p_start; | |
407 | +} | |
408 | +#endif | |
409 | + | |
410 | + | |
304 | 411 | #define MAX_ARGS 3 |
305 | 412 | |
306 | 413 | /* generate op code */ |
... | ... | @@ -388,7 +495,7 @@ void gen_code(const char *name, host_ulong offset, host_ulong size, |
388 | 495 | case EM_SPARC: |
389 | 496 | case EM_SPARC32PLUS: |
390 | 497 | { |
391 | - uint32_t start_insn, end_insn1, end_insn2, skip_insn; | |
498 | + uint32_t start_insn, end_insn1, end_insn2; | |
392 | 499 | uint8_t *p; |
393 | 500 | p = (void *)(p_end - 8); |
394 | 501 | if (p <= p_start) |
... | ... | @@ -406,14 +513,14 @@ void gen_code(const char *name, host_ulong offset, host_ulong size, |
406 | 513 | } else { |
407 | 514 | error("No save at the beginning of %s", name); |
408 | 515 | } |
409 | - | |
516 | +#if 0 | |
410 | 517 | /* Skip a preceeding nop, if present. */ |
411 | 518 | if (p > p_start) { |
412 | 519 | skip_insn = get32((uint32_t *)(p - 0x4)); |
413 | 520 | if (skip_insn == 0x01000000) |
414 | 521 | p -= 4; |
415 | 522 | } |
416 | - | |
523 | +#endif | |
417 | 524 | copy_size = p - p_start; |
418 | 525 | } |
419 | 526 | break; |
... | ... | @@ -448,6 +555,20 @@ void gen_code(const char *name, host_ulong offset, host_ulong size, |
448 | 555 | copy_size = p - p_start; |
449 | 556 | } |
450 | 557 | break; |
558 | +#ifdef HOST_ARM | |
559 | + case EM_ARM: | |
560 | + if ((p_end - p_start) <= 16) | |
561 | + error("%s: function too small", name); | |
562 | + if (get32((uint32_t *)p_start) != 0xe1a0c00d || | |
563 | + (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 || | |
564 | + get32((uint32_t *)(p_start + 8)) != 0xe24cb004) | |
565 | + error("%s: invalid prolog", name); | |
566 | + p_start += 12; | |
567 | + start_offset += 12; | |
568 | + copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end, | |
569 | + relocs, nb_relocs); | |
570 | + break; | |
571 | +#endif | |
451 | 572 | default: |
452 | 573 | error("unknown ELF architecture"); |
453 | 574 | } |
... | ... | @@ -458,7 +579,7 @@ void gen_code(const char *name, host_ulong offset, host_ulong size, |
458 | 579 | |
459 | 580 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
460 | 581 | if (rel->r_offset >= start_offset && |
461 | - rel->r_offset < start_offset + copy_size) { | |
582 | + rel->r_offset < start_offset + (p_end - p_start)) { | |
462 | 583 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
463 | 584 | if (strstart(sym_name, "__op_param", &p)) { |
464 | 585 | n = strtoul(p, NULL, 10); |
... | ... | @@ -496,7 +617,7 @@ void gen_code(const char *name, host_ulong offset, host_ulong size, |
496 | 617 | |
497 | 618 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
498 | 619 | if (rel->r_offset >= start_offset && |
499 | - rel->r_offset < start_offset + copy_size) { | |
620 | + rel->r_offset < start_offset + (p_end - p_start)) { | |
500 | 621 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
501 | 622 | if (*sym_name && |
502 | 623 | !strstart(sym_name, "__op_param", NULL) && |
... | ... | @@ -900,6 +1021,44 @@ void gen_code(const char *name, host_ulong offset, host_ulong size, |
900 | 1021 | } |
901 | 1022 | } |
902 | 1023 | } |
1024 | +#elif defined(HOST_ARM) | |
1025 | + { | |
1026 | + char name[256]; | |
1027 | + int type; | |
1028 | + int addend; | |
1029 | + | |
1030 | + arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end, | |
1031 | + relocs, nb_relocs); | |
1032 | + | |
1033 | + for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
1034 | + if (rel->r_offset >= start_offset && | |
1035 | + rel->r_offset < start_offset + copy_size) { | |
1036 | + sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; | |
1037 | + /* the compiler leave some unnecessary references to the code */ | |
1038 | + if (sym_name[0] == '\0') | |
1039 | + continue; | |
1040 | + if (strstart(sym_name, "__op_param", &p)) { | |
1041 | + snprintf(name, sizeof(name), "param%s", p); | |
1042 | + } else { | |
1043 | + snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
1044 | + } | |
1045 | + type = ELF32_R_TYPE(rel->r_info); | |
1046 | + addend = get32((uint32_t *)(text + rel->r_offset)); | |
1047 | + switch(type) { | |
1048 | + case R_ARM_ABS32: | |
1049 | + fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", | |
1050 | + rel->r_offset - start_offset, name, addend); | |
1051 | + break; | |
1052 | + case R_ARM_PC24: | |
1053 | + fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n", | |
1054 | + rel->r_offset - start_offset, addend, name); | |
1055 | + break; | |
1056 | + default: | |
1057 | + error("unsupported arm relocation (%d)", type); | |
1058 | + } | |
1059 | + } | |
1060 | + } | |
1061 | + } | |
903 | 1062 | #else |
904 | 1063 | #error unsupported CPU |
905 | 1064 | #endif |
... | ... | @@ -1075,23 +1234,22 @@ fprintf(outfile, |
1075 | 1234 | "{\n" |
1076 | 1235 | " uint8_t *gen_code_ptr;\n" |
1077 | 1236 | " const uint16_t *opc_ptr;\n" |
1078 | -" const uint32_t *opparam_ptr;\n" | |
1237 | +" const uint32_t *opparam_ptr;\n"); | |
1238 | + | |
1239 | +#ifdef HOST_ARM | |
1240 | +fprintf(outfile, | |
1241 | +" uint8_t *last_gen_code_ptr = gen_code_buf;\n" | |
1242 | +" LDREntry *arm_ldr_ptr = arm_ldr_table;\n" | |
1243 | +" uint32_t *arm_data_ptr = arm_data_table;\n"); | |
1244 | +#endif | |
1245 | + | |
1246 | +fprintf(outfile, | |
1247 | +"\n" | |
1079 | 1248 | " gen_code_ptr = gen_code_buf;\n" |
1080 | 1249 | " opc_ptr = opc_buf;\n" |
1081 | 1250 | " opparam_ptr = opparam_buf;\n"); |
1082 | 1251 | |
1083 | 1252 | /* Generate prologue, if needed. */ |
1084 | - switch(ELF_ARCH) { | |
1085 | - case EM_SPARC: | |
1086 | - fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c23a080; /* sub %%sp, 128, %%sp */\n"); | |
1087 | - fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc27a080; /* sub %%fp, 128, %%fp */\n"); | |
1088 | - break; | |
1089 | - | |
1090 | - case EM_SPARCV9: | |
1091 | - fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c23a100; /* sub %%sp, 256, %%sp */\n"); | |
1092 | - fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc27a100; /* sub %%fp, 256, %%fp */\n"); | |
1093 | - break; | |
1094 | - }; | |
1095 | 1253 | |
1096 | 1254 | fprintf(outfile, |
1097 | 1255 | " for(;;) {\n" |
... | ... | @@ -1116,7 +1274,21 @@ fprintf(outfile, |
1116 | 1274 | fprintf(outfile, |
1117 | 1275 | " default:\n" |
1118 | 1276 | " goto the_end;\n" |
1119 | -" }\n" | |
1277 | +" }\n"); | |
1278 | + | |
1279 | +#ifdef HOST_ARM | |
1280 | +/* generate constant table if needed */ | |
1281 | +fprintf(outfile, | |
1282 | +" if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n" | |
1283 | +" gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n" | |
1284 | +" last_gen_code_ptr = gen_code_ptr;\n" | |
1285 | +" arm_ldr_ptr = arm_ldr_table;\n" | |
1286 | +" arm_data_ptr = arm_data_table;\n" | |
1287 | +" }\n"); | |
1288 | +#endif | |
1289 | + | |
1290 | + | |
1291 | +fprintf(outfile, | |
1120 | 1292 | " }\n" |
1121 | 1293 | " the_end:\n" |
1122 | 1294 | ); |
... | ... | @@ -1140,14 +1312,16 @@ fprintf(outfile, |
1140 | 1312 | break; |
1141 | 1313 | case EM_SPARC: |
1142 | 1314 | case EM_SPARC32PLUS: |
1143 | - fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc07a080; /* add %%fp, 256, %%fp */\n"); | |
1144 | 1315 | fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c62008; /* jmpl %%i0 + 8, %%g0 */\n"); |
1145 | - fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c03a080; /* add %%sp, 256, %%sp */\n"); | |
1316 | + fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x01000000; /* nop */\n"); | |
1146 | 1317 | break; |
1147 | 1318 | case EM_SPARCV9: |
1148 | 1319 | fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c7e008; /* ret */\n"); |
1149 | 1320 | fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81e80000; /* restore */\n"); |
1150 | 1321 | break; |
1322 | + case EM_ARM: | |
1323 | + fprintf(outfile, "gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 0);\n"); | |
1324 | + break; | |
1151 | 1325 | default: |
1152 | 1326 | error("unknown ELF architecture"); |
1153 | 1327 | } | ... | ... |
dyngen.h
0 โ 100644
1 | +/* | |
2 | + * dyngen helpers | |
3 | + * | |
4 | + * Copyright (c) 2003 Fabrice Bellard | |
5 | + * | |
6 | + * This library is free software; you can redistribute it and/or | |
7 | + * modify it under the terms of the GNU Lesser General Public | |
8 | + * License as published by the Free Software Foundation; either | |
9 | + * version 2 of the License, or (at your option) any later version. | |
10 | + * | |
11 | + * This library is distributed in the hope that it will be useful, | |
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | + * Lesser General Public License for more details. | |
15 | + * | |
16 | + * You should have received a copy of the GNU Lesser General Public | |
17 | + * License along with this library; if not, write to the Free Software | |
18 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | + */ | |
20 | + | |
21 | +#ifdef __alpha__ | |
22 | + | |
23 | +register int gp asm("$29"); | |
24 | + | |
25 | +static inline void immediate_ldah(void *p, int val) { | |
26 | + uint32_t *dest = p; | |
27 | + long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff; | |
28 | + | |
29 | + *dest &= ~0xffff; | |
30 | + *dest |= high; | |
31 | + *dest |= 31 << 16; | |
32 | +} | |
33 | +static inline void immediate_lda(void *dest, int val) { | |
34 | + *(uint16_t *) dest = val; | |
35 | +} | |
36 | +void fix_bsr(void *p, int offset) { | |
37 | + uint32_t *dest = p; | |
38 | + *dest &= ~((1 << 21) - 1); | |
39 | + *dest |= (offset >> 2) & ((1 << 21) - 1); | |
40 | +} | |
41 | + | |
42 | +#endif /* __alpha__ */ | |
43 | + | |
44 | +#ifdef __arm__ | |
45 | + | |
46 | +#define MAX_OP_SIZE (128 * 4) /* in bytes */ | |
47 | +/* max size of the code that can be generated without calling arm_flush_ldr */ | |
48 | +#define MAX_FRAG_SIZE (1024 * 4) | |
49 | +//#define MAX_FRAG_SIZE (135 * 4) /* for testing */ | |
50 | + | |
51 | +typedef struct LDREntry { | |
52 | + uint8_t *ptr; | |
53 | + uint32_t *data_ptr; | |
54 | +} LDREntry; | |
55 | + | |
56 | +static LDREntry arm_ldr_table[1024]; | |
57 | +static uint32_t arm_data_table[1024]; | |
58 | + | |
59 | +extern char exec_loop; | |
60 | + | |
61 | +static inline void arm_reloc_pc24(uint32_t *ptr, uint32_t insn, int val) | |
62 | +{ | |
63 | + *ptr = (insn & ~0xffffff) | ((insn + ((val - (int)ptr) >> 2)) & 0xffffff); | |
64 | +} | |
65 | + | |
66 | +static uint8_t *arm_flush_ldr(uint8_t *gen_code_ptr, | |
67 | + LDREntry *ldr_start, LDREntry *ldr_end, | |
68 | + uint32_t *data_start, uint32_t *data_end, | |
69 | + int gen_jmp) | |
70 | +{ | |
71 | + LDREntry *le; | |
72 | + uint32_t *ptr; | |
73 | + int offset, data_size, target; | |
74 | + uint8_t *data_ptr; | |
75 | + uint32_t insn; | |
76 | + | |
77 | + data_size = (uint8_t *)data_end - (uint8_t *)data_start; | |
78 | + | |
79 | + if (!gen_jmp) { | |
80 | + /* b exec_loop */ | |
81 | + arm_reloc_pc24((uint32_t *)gen_code_ptr, 0xeafffffe, (long)(&exec_loop)); | |
82 | + gen_code_ptr += 4; | |
83 | + } else { | |
84 | + /* generate branch to skip the data */ | |
85 | + if (data_size == 0) | |
86 | + return gen_code_ptr; | |
87 | + target = (long)gen_code_ptr + data_size + 4; | |
88 | + arm_reloc_pc24((uint32_t *)gen_code_ptr, 0xeafffffe, target); | |
89 | + gen_code_ptr += 4; | |
90 | + } | |
91 | + | |
92 | + /* copy the data */ | |
93 | + data_ptr = gen_code_ptr; | |
94 | + memcpy(gen_code_ptr, data_start, data_size); | |
95 | + gen_code_ptr += data_size; | |
96 | + | |
97 | + /* patch the ldr to point to the data */ | |
98 | + for(le = ldr_start; le < ldr_end; le++) { | |
99 | + ptr = (uint32_t *)le->ptr; | |
100 | + offset = ((unsigned long)(le->data_ptr) - (unsigned long)data_start) + | |
101 | + (unsigned long)data_ptr - | |
102 | + (unsigned long)ptr - 8; | |
103 | + insn = *ptr & ~(0xfff | 0x00800000); | |
104 | + if (offset < 0) { | |
105 | + offset = - offset; | |
106 | + } else { | |
107 | + insn |= 0x00800000; | |
108 | + } | |
109 | + if (offset > 0xfff) { | |
110 | + fprintf(stderr, "Error ldr offset\n"); | |
111 | + abort(); | |
112 | + } | |
113 | + insn |= offset; | |
114 | + *ptr = insn; | |
115 | + } | |
116 | + return gen_code_ptr; | |
117 | +} | |
118 | + | |
119 | +#endif /* __arm__ */ | |
120 | + | |
121 | + | |
122 | + | ... | ... |