]> git.proxmox.com Git - mirror_qemu.git/blob - dyngen.c
arm support - suppressed possibly unsafe sparc nop deletion
[mirror_qemu.git] / dyngen.c
1 /*
2 * Generic Dynamic compiler generator
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27
28 #include "config.h"
29
30 /* elf format definitions. We use these macros to test the CPU to
31 allow cross compilation (this tool must be ran on the build
32 platform) */
33 #if defined(HOST_I386)
34
35 #define ELF_CLASS ELFCLASS32
36 #define ELF_ARCH EM_386
37 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
38 #undef ELF_USES_RELOCA
39
40 #elif defined(HOST_PPC)
41
42 #define ELF_CLASS ELFCLASS32
43 #define ELF_ARCH EM_PPC
44 #define elf_check_arch(x) ((x) == EM_PPC)
45 #define ELF_USES_RELOCA
46
47 #elif defined(HOST_S390)
48
49 #define ELF_CLASS ELFCLASS32
50 #define ELF_ARCH EM_S390
51 #define elf_check_arch(x) ((x) == EM_S390)
52 #define ELF_USES_RELOCA
53
54 #elif defined(HOST_ALPHA)
55
56 #define ELF_CLASS ELFCLASS64
57 #define ELF_ARCH EM_ALPHA
58 #define elf_check_arch(x) ((x) == EM_ALPHA)
59 #define ELF_USES_RELOCA
60
61 #elif defined(HOST_IA64)
62
63 #define ELF_CLASS ELFCLASS64
64 #define ELF_ARCH EM_IA_64
65 #define elf_check_arch(x) ((x) == EM_IA_64)
66 #define ELF_USES_RELOCA
67
68 #elif defined(HOST_SPARC)
69
70 #define ELF_CLASS ELFCLASS32
71 #define ELF_ARCH EM_SPARC
72 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
73 #define ELF_USES_RELOCA
74
75 #elif defined(HOST_SPARC64)
76
77 #define ELF_CLASS ELFCLASS64
78 #define ELF_ARCH EM_SPARCV9
79 #define elf_check_arch(x) ((x) == EM_SPARCV9)
80 #define ELF_USES_RELOCA
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
89 #else
90 #error unsupported CPU - please update the code
91 #endif
92
93 #include "elf.h"
94
95 #if ELF_CLASS == ELFCLASS32
96 typedef int32_t host_long;
97 typedef uint32_t host_ulong;
98 #define swabls(x) swab32s(x)
99 #else
100 typedef int64_t host_long;
101 typedef uint64_t host_ulong;
102 #define swabls(x) swab64s(x)
103 #endif
104
105 #ifdef ELF_USES_RELOCA
106 #define SHT_RELOC SHT_RELA
107 #else
108 #define SHT_RELOC SHT_REL
109 #endif
110
111 #include "thunk.h"
112
113 /* all dynamically generated functions begin with this code */
114 #define OP_PREFIX "op_"
115
116 int elf_must_swap(struct elfhdr *h)
117 {
118 union {
119 uint32_t i;
120 uint8_t b[4];
121 } swaptest;
122
123 swaptest.i = 1;
124 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
125 (swaptest.b[0] == 0);
126 }
127
128 void swab16s(uint16_t *p)
129 {
130 *p = bswap16(*p);
131 }
132
133 void swab32s(uint32_t *p)
134 {
135 *p = bswap32(*p);
136 }
137
138 void swab64s(uint64_t *p)
139 {
140 *p = bswap64(*p);
141 }
142
143 void elf_swap_ehdr(struct elfhdr *h)
144 {
145 swab16s(&h->e_type); /* Object file type */
146 swab16s(&h-> e_machine); /* Architecture */
147 swab32s(&h-> e_version); /* Object file version */
148 swabls(&h-> e_entry); /* Entry point virtual address */
149 swabls(&h-> e_phoff); /* Program header table file offset */
150 swabls(&h-> e_shoff); /* Section header table file offset */
151 swab32s(&h-> e_flags); /* Processor-specific flags */
152 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
153 swab16s(&h-> e_phentsize); /* Program header table entry size */
154 swab16s(&h-> e_phnum); /* Program header table entry count */
155 swab16s(&h-> e_shentsize); /* Section header table entry size */
156 swab16s(&h-> e_shnum); /* Section header table entry count */
157 swab16s(&h-> e_shstrndx); /* Section header string table index */
158 }
159
160 void elf_swap_shdr(struct elf_shdr *h)
161 {
162 swab32s(&h-> sh_name); /* Section name (string tbl index) */
163 swab32s(&h-> sh_type); /* Section type */
164 swabls(&h-> sh_flags); /* Section flags */
165 swabls(&h-> sh_addr); /* Section virtual addr at execution */
166 swabls(&h-> sh_offset); /* Section file offset */
167 swabls(&h-> sh_size); /* Section size in bytes */
168 swab32s(&h-> sh_link); /* Link to another section */
169 swab32s(&h-> sh_info); /* Additional section information */
170 swabls(&h-> sh_addralign); /* Section alignment */
171 swabls(&h-> sh_entsize); /* Entry size if section holds table */
172 }
173
174 void elf_swap_phdr(struct elf_phdr *h)
175 {
176 swab32s(&h->p_type); /* Segment type */
177 swabls(&h->p_offset); /* Segment file offset */
178 swabls(&h->p_vaddr); /* Segment virtual address */
179 swabls(&h->p_paddr); /* Segment physical address */
180 swabls(&h->p_filesz); /* Segment size in file */
181 swabls(&h->p_memsz); /* Segment size in memory */
182 swab32s(&h->p_flags); /* Segment flags */
183 swabls(&h->p_align); /* Segment alignment */
184 }
185
186 void elf_swap_rel(ELF_RELOC *rel)
187 {
188 swabls(&rel->r_offset);
189 swabls(&rel->r_info);
190 #ifdef ELF_USES_RELOCA
191 swabls(&rel->r_addend);
192 #endif
193 }
194
195 /* ELF file info */
196 int do_swap;
197 struct elf_shdr *shdr;
198 uint8_t **sdata;
199 struct elfhdr ehdr;
200 ElfW(Sym) *symtab;
201 int nb_syms;
202 char *strtab;
203 int text_shndx;
204
205 uint16_t get16(uint16_t *p)
206 {
207 uint16_t val;
208 val = *p;
209 if (do_swap)
210 val = bswap16(val);
211 return val;
212 }
213
214 uint32_t get32(uint32_t *p)
215 {
216 uint32_t val;
217 val = *p;
218 if (do_swap)
219 val = bswap32(val);
220 return val;
221 }
222
223 void put16(uint16_t *p, uint16_t val)
224 {
225 if (do_swap)
226 val = bswap16(val);
227 *p = val;
228 }
229
230 void put32(uint32_t *p, uint32_t val)
231 {
232 if (do_swap)
233 val = bswap32(val);
234 *p = val;
235 }
236
237 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
238 {
239 va_list ap;
240 va_start(ap, fmt);
241 fprintf(stderr, "dyngen: ");
242 vfprintf(stderr, fmt, ap);
243 fprintf(stderr, "\n");
244 va_end(ap);
245 exit(1);
246 }
247
248
249 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
250 const char *name)
251 {
252 int i;
253 const char *shname;
254 struct elf_shdr *sec;
255
256 for(i = 0; i < shnum; i++) {
257 sec = &shdr[i];
258 if (!sec->sh_name)
259 continue;
260 shname = shstr + sec->sh_name;
261 if (!strcmp(shname, name))
262 return sec;
263 }
264 return NULL;
265 }
266
267 int find_reloc(int sh_index)
268 {
269 struct elf_shdr *sec;
270 int i;
271
272 for(i = 0; i < ehdr.e_shnum; i++) {
273 sec = &shdr[i];
274 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
275 return i;
276 }
277 return 0;
278 }
279
280 void *load_data(int fd, long offset, unsigned int size)
281 {
282 char *data;
283
284 data = malloc(size);
285 if (!data)
286 return NULL;
287 lseek(fd, offset, SEEK_SET);
288 if (read(fd, data, size) != size) {
289 free(data);
290 return NULL;
291 }
292 return data;
293 }
294
295 int strstart(const char *str, const char *val, const char **ptr)
296 {
297 const char *p, *q;
298 p = str;
299 q = val;
300 while (*q != '\0') {
301 if (*p != *q)
302 return 0;
303 p++;
304 q++;
305 }
306 if (ptr)
307 *ptr = p;
308 return 1;
309 }
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
411 #define MAX_ARGS 3
412
413 /* generate op code */
414 void gen_code(const char *name, host_ulong offset, host_ulong size,
415 FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs,
416 int gen_switch)
417 {
418 int copy_size = 0;
419 uint8_t *p_start, *p_end;
420 host_ulong start_offset;
421 int nb_args, i, n;
422 uint8_t args_present[MAX_ARGS];
423 const char *sym_name, *p;
424 ELF_RELOC *rel;
425
426 /* Compute exact size excluding prologue and epilogue instructions.
427 * Increment start_offset to skip epilogue instructions, then compute
428 * copy_size the indicate the size of the remaining instructions (in
429 * bytes).
430 */
431 p_start = text + offset;
432 p_end = p_start + size;
433 start_offset = offset;
434 switch(ELF_ARCH) {
435 case EM_386:
436 {
437 int len;
438 len = p_end - p_start;
439 if (len == 0)
440 error("empty code for %s", name);
441 if (p_end[-1] == 0xc3) {
442 len--;
443 } else {
444 error("ret or jmp expected at the end of %s", name);
445 }
446 copy_size = len;
447 }
448 break;
449 case EM_PPC:
450 {
451 uint8_t *p;
452 p = (void *)(p_end - 4);
453 if (p == p_start)
454 error("empty code for %s", name);
455 if (get32((uint32_t *)p) != 0x4e800020)
456 error("blr expected at the end of %s", name);
457 copy_size = p - p_start;
458 }
459 break;
460 case EM_S390:
461 {
462 uint8_t *p;
463 p = (void *)(p_end - 2);
464 if (p == p_start)
465 error("empty code for %s", name);
466 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
467 error("br %%r14 expected at the end of %s", name);
468 copy_size = p - p_start;
469 }
470 break;
471 case EM_ALPHA:
472 {
473 uint8_t *p;
474 p = p_end - 4;
475 if (p == p_start)
476 error("empty code for %s", name);
477 if (get32((uint32_t *)p) != 0x6bfa8001)
478 error("ret expected at the end of %s", name);
479 copy_size = p - p_start;
480 }
481 break;
482 case EM_IA_64:
483 {
484 uint8_t *p;
485 p = (void *)(p_end - 4);
486 if (p == p_start)
487 error("empty code for %s", name);
488 /* br.ret.sptk.many b0;; */
489 /* 08 00 84 00 */
490 if (get32((uint32_t *)p) != 0x00840008)
491 error("br.ret.sptk.many b0;; expected at the end of %s", name);
492 copy_size = p - p_start;
493 }
494 break;
495 case EM_SPARC:
496 case EM_SPARC32PLUS:
497 {
498 uint32_t start_insn, end_insn1, end_insn2;
499 uint8_t *p;
500 p = (void *)(p_end - 8);
501 if (p <= p_start)
502 error("empty code for %s", name);
503 start_insn = get32((uint32_t *)(p_start + 0x0));
504 end_insn1 = get32((uint32_t *)(p + 0x0));
505 end_insn2 = get32((uint32_t *)(p + 0x4));
506 if ((start_insn & ~0x1fff) == 0x9de3a000) {
507 p_start += 0x4;
508 start_offset += 0x4;
509 if ((int)(start_insn | ~0x1fff) < -128)
510 error("Found bogus save at the start of %s", name);
511 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
512 error("ret; restore; not found at end of %s", name);
513 } else {
514 error("No save at the beginning of %s", name);
515 }
516 #if 0
517 /* Skip a preceeding nop, if present. */
518 if (p > p_start) {
519 skip_insn = get32((uint32_t *)(p - 0x4));
520 if (skip_insn == 0x01000000)
521 p -= 4;
522 }
523 #endif
524 copy_size = p - p_start;
525 }
526 break;
527 case EM_SPARCV9:
528 {
529 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
530 uint8_t *p;
531 p = (void *)(p_end - 8);
532 if (p <= p_start)
533 error("empty code for %s", name);
534 start_insn = get32((uint32_t *)(p_start + 0x0));
535 end_insn1 = get32((uint32_t *)(p + 0x0));
536 end_insn2 = get32((uint32_t *)(p + 0x4));
537 if ((start_insn & ~0x1fff) == 0x9de3a000) {
538 p_start += 0x4;
539 start_offset += 0x4;
540 if ((int)(start_insn | ~0x1fff) < -256)
541 error("Found bogus save at the start of %s", name);
542 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
543 error("ret; restore; not found at end of %s", name);
544 } else {
545 error("No save at the beginning of %s", name);
546 }
547
548 /* Skip a preceeding nop, if present. */
549 if (p > p_start) {
550 skip_insn = get32((uint32_t *)(p - 0x4));
551 if (skip_insn == 0x01000000)
552 p -= 4;
553 }
554
555 copy_size = p - p_start;
556 }
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
572 default:
573 error("unknown ELF architecture");
574 }
575
576 /* compute the number of arguments by looking at the relocations */
577 for(i = 0;i < MAX_ARGS; i++)
578 args_present[i] = 0;
579
580 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
581 if (rel->r_offset >= start_offset &&
582 rel->r_offset < start_offset + (p_end - p_start)) {
583 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
584 if (strstart(sym_name, "__op_param", &p)) {
585 n = strtoul(p, NULL, 10);
586 if (n > MAX_ARGS)
587 error("too many arguments in %s", name);
588 args_present[n - 1] = 1;
589 }
590 }
591 }
592
593 nb_args = 0;
594 while (nb_args < MAX_ARGS && args_present[nb_args])
595 nb_args++;
596 for(i = nb_args; i < MAX_ARGS; i++) {
597 if (args_present[i])
598 error("inconsistent argument numbering in %s", name);
599 }
600
601 if (gen_switch == 2) {
602 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
603 } else if (gen_switch == 1) {
604
605 /* output C code */
606 fprintf(outfile, "case INDEX_%s: {\n", name);
607 if (nb_args > 0) {
608 fprintf(outfile, " long ");
609 for(i = 0; i < nb_args; i++) {
610 if (i != 0)
611 fprintf(outfile, ", ");
612 fprintf(outfile, "param%d", i + 1);
613 }
614 fprintf(outfile, ";\n");
615 }
616 fprintf(outfile, " extern void %s();\n", name);
617
618 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
619 if (rel->r_offset >= start_offset &&
620 rel->r_offset < start_offset + (p_end - p_start)) {
621 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
622 if (*sym_name &&
623 !strstart(sym_name, "__op_param", NULL) &&
624 !strstart(sym_name, "__op_jmp", NULL)) {
625 #if defined(HOST_SPARC)
626 if (sym_name[0] == '.') {
627 fprintf(outfile,
628 "extern char __dot_%s __asm__(\"%s\");\n",
629 sym_name+1, sym_name);
630 continue;
631 }
632 #endif
633 fprintf(outfile, "extern char %s;\n", sym_name);
634 }
635 }
636 }
637
638 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
639
640 /* emit code offset information */
641 {
642 ElfW(Sym) *sym;
643 const char *sym_name, *p;
644 target_ulong val;
645 int n;
646
647 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
648 sym_name = strtab + sym->st_name;
649 if (strstart(sym_name, "__op_label", &p)) {
650 uint8_t *ptr;
651 int addend;
652 unsigned long offset;
653
654 /* test if the variable refers to a label inside
655 the code we are generating */
656 ptr = sdata[sym->st_shndx];
657 if (!ptr)
658 error("__op_labelN in invalid section");
659 offset = sym->st_value;
660 addend = 0;
661 #ifdef ELF_USES_RELOCA
662 {
663 int reloc_shndx, nb_relocs1, j;
664
665 /* try to find a matching relocation */
666 reloc_shndx = find_reloc(sym->st_shndx);
667 if (reloc_shndx) {
668 nb_relocs1 = shdr[reloc_shndx].sh_size /
669 shdr[reloc_shndx].sh_entsize;
670 rel = (ELF_RELOC *)sdata[reloc_shndx];
671 for(j = 0; j < nb_relocs1; j++) {
672 if (rel->r_offset == offset) {
673 addend = rel->r_addend;
674 break;
675 }
676 rel++;
677 }
678 }
679 }
680 #endif
681 val = *(target_ulong *)(ptr + offset);
682 val += addend;
683
684 if (val >= start_offset && val < start_offset + copy_size) {
685 n = strtol(p, NULL, 10);
686 fprintf(outfile, " label_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
687 }
688 }
689 }
690 }
691
692 /* load parameres in variables */
693 for(i = 0; i < nb_args; i++) {
694 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
695 }
696
697 /* patch relocations */
698 #if defined(HOST_I386)
699 {
700 char name[256];
701 int type;
702 int addend;
703 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
704 if (rel->r_offset >= start_offset &&
705 rel->r_offset < start_offset + copy_size) {
706 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
707 if (strstart(sym_name, "__op_param", &p)) {
708 snprintf(name, sizeof(name), "param%s", p);
709 } else {
710 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
711 }
712 type = ELF32_R_TYPE(rel->r_info);
713 addend = get32((uint32_t *)(text + rel->r_offset));
714 switch(type) {
715 case R_386_32:
716 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
717 rel->r_offset - start_offset, name, addend);
718 break;
719 case R_386_PC32:
720 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
721 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
722 break;
723 default:
724 error("unsupported i386 relocation (%d)", type);
725 }
726 }
727 }
728 }
729 #elif defined(HOST_PPC)
730 {
731 char name[256];
732 int type;
733 int addend;
734 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
735 if (rel->r_offset >= start_offset &&
736 rel->r_offset < start_offset + copy_size) {
737 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
738 if (strstart(sym_name, "__op_jmp", &p)) {
739 int n;
740 n = strtol(p, NULL, 10);
741 /* __op_jmp relocations are done at
742 runtime to do translated block
743 chaining: the offset of the instruction
744 needs to be stored */
745 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
746 n, rel->r_offset - start_offset);
747 continue;
748 }
749
750 if (strstart(sym_name, "__op_param", &p)) {
751 snprintf(name, sizeof(name), "param%s", p);
752 } else {
753 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
754 }
755 type = ELF32_R_TYPE(rel->r_info);
756 addend = rel->r_addend;
757 switch(type) {
758 case R_PPC_ADDR32:
759 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
760 rel->r_offset - start_offset, name, addend);
761 break;
762 case R_PPC_ADDR16_LO:
763 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
764 rel->r_offset - start_offset, name, addend);
765 break;
766 case R_PPC_ADDR16_HI:
767 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
768 rel->r_offset - start_offset, name, addend);
769 break;
770 case R_PPC_ADDR16_HA:
771 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
772 rel->r_offset - start_offset, name, addend);
773 break;
774 case R_PPC_REL24:
775 /* warning: must be at 32 MB distancy */
776 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
777 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
778 break;
779 default:
780 error("unsupported powerpc relocation (%d)", type);
781 }
782 }
783 }
784 }
785 #elif defined(HOST_S390)
786 {
787 char name[256];
788 int type;
789 int addend;
790 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
791 if (rel->r_offset >= start_offset &&
792 rel->r_offset < start_offset + copy_size) {
793 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
794 if (strstart(sym_name, "__op_param", &p)) {
795 snprintf(name, sizeof(name), "param%s", p);
796 } else {
797 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
798 }
799 type = ELF32_R_TYPE(rel->r_info);
800 addend = rel->r_addend;
801 switch(type) {
802 case R_390_32:
803 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
804 rel->r_offset - start_offset, name, addend);
805 break;
806 case R_390_16:
807 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
808 rel->r_offset - start_offset, name, addend);
809 break;
810 case R_390_8:
811 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
812 rel->r_offset - start_offset, name, addend);
813 break;
814 default:
815 error("unsupported s390 relocation (%d)", type);
816 }
817 }
818 }
819 }
820 #elif defined(HOST_ALPHA)
821 {
822 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
823 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
824 int type;
825
826 type = ELF64_R_TYPE(rel->r_info);
827 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
828 switch (type) {
829 case R_ALPHA_GPDISP:
830 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
831 as an immediate instead of constructing it from the pv or ra. */
832 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
833 rel->r_offset - start_offset);
834 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
835 rel->r_offset - start_offset + rel->r_addend);
836 break;
837 case R_ALPHA_LITUSE:
838 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
839 now, since some called functions (libc) need pv to be set up. */
840 break;
841 case R_ALPHA_HINT:
842 /* Branch target prediction hint. Ignore for now. Should be already
843 correct for in-function jumps. */
844 break;
845 case R_ALPHA_LITERAL:
846 /* Load a literal from the GOT relative to the gp. Since there's only a
847 single gp, nothing is to be done. */
848 break;
849 case R_ALPHA_GPRELHIGH:
850 /* Handle fake relocations against __op_param symbol. Need to emit the
851 high part of the immediate value instead. Other symbols need no
852 special treatment. */
853 if (strstart(sym_name, "__op_param", &p))
854 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
855 rel->r_offset - start_offset, p);
856 break;
857 case R_ALPHA_GPRELLOW:
858 if (strstart(sym_name, "__op_param", &p))
859 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
860 rel->r_offset - start_offset, p);
861 break;
862 case R_ALPHA_BRSGP:
863 /* PC-relative jump. Tweak offset to skip the two instructions that try to
864 set up the gp from the pv. */
865 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
866 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
867 break;
868 default:
869 error("unsupported Alpha relocation (%d)", type);
870 }
871 }
872 }
873 }
874 #elif defined(HOST_IA64)
875 {
876 char name[256];
877 int type;
878 int addend;
879 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
880 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
881 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
882 if (strstart(sym_name, "__op_param", &p)) {
883 snprintf(name, sizeof(name), "param%s", p);
884 } else {
885 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
886 }
887 type = ELF64_R_TYPE(rel->r_info);
888 addend = rel->r_addend;
889 switch(type) {
890 case R_IA64_LTOFF22:
891 error("must implemnt R_IA64_LTOFF22 relocation");
892 case R_IA64_PCREL21B:
893 error("must implemnt R_IA64_PCREL21B relocation");
894 default:
895 error("unsupported ia64 relocation (%d)", type);
896 }
897 }
898 }
899 }
900 #elif defined(HOST_SPARC)
901 {
902 char name[256];
903 int type;
904 int addend;
905 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
906 if (rel->r_offset >= start_offset &&
907 rel->r_offset < start_offset + copy_size) {
908 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
909 if (strstart(sym_name, "__op_param", &p)) {
910 snprintf(name, sizeof(name), "param%s", p);
911 } else {
912 if (sym_name[0] == '.')
913 snprintf(name, sizeof(name),
914 "(long)(&__dot_%s)",
915 sym_name + 1);
916 else
917 snprintf(name, sizeof(name),
918 "(long)(&%s)", sym_name);
919 }
920 type = ELF32_R_TYPE(rel->r_info);
921 addend = rel->r_addend;
922 switch(type) {
923 case R_SPARC_32:
924 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
925 rel->r_offset - start_offset, name, addend);
926 break;
927 case R_SPARC_HI22:
928 fprintf(outfile,
929 " *(uint32_t *)(gen_code_ptr + %d) = "
930 "((*(uint32_t *)(gen_code_ptr + %d)) "
931 " & ~0x3fffff) "
932 " | (((%s + %d) >> 10) & 0x3fffff);\n",
933 rel->r_offset - start_offset,
934 rel->r_offset - start_offset,
935 name, addend);
936 break;
937 case R_SPARC_LO10:
938 fprintf(outfile,
939 " *(uint32_t *)(gen_code_ptr + %d) = "
940 "((*(uint32_t *)(gen_code_ptr + %d)) "
941 " & ~0x3ff) "
942 " | ((%s + %d) & 0x3ff);\n",
943 rel->r_offset - start_offset,
944 rel->r_offset - start_offset,
945 name, addend);
946 break;
947 case R_SPARC_WDISP30:
948 fprintf(outfile,
949 " *(uint32_t *)(gen_code_ptr + %d) = "
950 "((*(uint32_t *)(gen_code_ptr + %d)) "
951 " & ~0x3fffffff) "
952 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
953 " & 0x3fffffff);\n",
954 rel->r_offset - start_offset,
955 rel->r_offset - start_offset,
956 name, addend,
957 rel->r_offset - start_offset);
958 break;
959 default:
960 error("unsupported sparc relocation (%d)", type);
961 }
962 }
963 }
964 }
965 #elif defined(HOST_SPARC64)
966 {
967 char name[256];
968 int type;
969 int addend;
970 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
971 if (rel->r_offset >= start_offset &&
972 rel->r_offset < start_offset + copy_size) {
973 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
974 if (strstart(sym_name, "__op_param", &p)) {
975 snprintf(name, sizeof(name), "param%s", p);
976 } else {
977 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
978 }
979 type = ELF64_R_TYPE(rel->r_info);
980 addend = rel->r_addend;
981 switch(type) {
982 case R_SPARC_32:
983 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
984 rel->r_offset - start_offset, name, addend);
985 break;
986 case R_SPARC_HI22:
987 fprintf(outfile,
988 " *(uint32_t *)(gen_code_ptr + %d) = "
989 "((*(uint32_t *)(gen_code_ptr + %d)) "
990 " & ~0x3fffff) "
991 " | (((%s + %d) >> 10) & 0x3fffff);\n",
992 rel->r_offset - start_offset,
993 rel->r_offset - start_offset,
994 name, addend);
995 break;
996 case R_SPARC_LO10:
997 fprintf(outfile,
998 " *(uint32_t *)(gen_code_ptr + %d) = "
999 "((*(uint32_t *)(gen_code_ptr + %d)) "
1000 " & ~0x3ff) "
1001 " | ((%s + %d) & 0x3ff);\n",
1002 rel->r_offset - start_offset,
1003 rel->r_offset - start_offset,
1004 name, addend);
1005 break;
1006 case R_SPARC_WDISP30:
1007 fprintf(outfile,
1008 " *(uint32_t *)(gen_code_ptr + %d) = "
1009 "((*(uint32_t *)(gen_code_ptr + %d)) "
1010 " & ~0x3fffffff) "
1011 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1012 " & 0x3fffffff);\n",
1013 rel->r_offset - start_offset,
1014 rel->r_offset - start_offset,
1015 name, addend,
1016 rel->r_offset - start_offset);
1017 break;
1018 default:
1019 error("unsupported sparc64 relocation (%d)", type);
1020 }
1021 }
1022 }
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 }
1062 #else
1063 #error unsupported CPU
1064 #endif
1065 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
1066 fprintf(outfile, "}\n");
1067 fprintf(outfile, "break;\n\n");
1068 } else {
1069 fprintf(outfile, "static inline void gen_%s(", name);
1070 if (nb_args == 0) {
1071 fprintf(outfile, "void");
1072 } else {
1073 for(i = 0; i < nb_args; i++) {
1074 if (i != 0)
1075 fprintf(outfile, ", ");
1076 fprintf(outfile, "long param%d", i + 1);
1077 }
1078 }
1079 fprintf(outfile, ")\n");
1080 fprintf(outfile, "{\n");
1081 for(i = 0; i < nb_args; i++) {
1082 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
1083 }
1084 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
1085 fprintf(outfile, "}\n\n");
1086 }
1087 }
1088
1089 /* load an elf object file */
1090 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
1091 {
1092 int fd;
1093 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
1094 int i, j;
1095 ElfW(Sym) *sym;
1096 char *shstr;
1097 uint8_t *text;
1098 ELF_RELOC *relocs;
1099 int nb_relocs;
1100 ELF_RELOC *rel;
1101
1102 fd = open(filename, O_RDONLY);
1103 if (fd < 0)
1104 error("can't open file '%s'", filename);
1105
1106 /* Read ELF header. */
1107 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
1108 error("unable to read file header");
1109
1110 /* Check ELF identification. */
1111 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
1112 || ehdr.e_ident[EI_MAG1] != ELFMAG1
1113 || ehdr.e_ident[EI_MAG2] != ELFMAG2
1114 || ehdr.e_ident[EI_MAG3] != ELFMAG3
1115 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
1116 error("bad ELF header");
1117 }
1118
1119 do_swap = elf_must_swap(&ehdr);
1120 if (do_swap)
1121 elf_swap_ehdr(&ehdr);
1122 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
1123 error("Unsupported ELF class");
1124 if (ehdr.e_type != ET_REL)
1125 error("ELF object file expected");
1126 if (ehdr.e_version != EV_CURRENT)
1127 error("Invalid ELF version");
1128 if (!elf_check_arch(ehdr.e_machine))
1129 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
1130
1131 /* read section headers */
1132 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
1133 if (do_swap) {
1134 for(i = 0; i < ehdr.e_shnum; i++) {
1135 elf_swap_shdr(&shdr[i]);
1136 }
1137 }
1138
1139 /* read all section data */
1140 sdata = malloc(sizeof(void *) * ehdr.e_shnum);
1141 memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
1142
1143 for(i = 0;i < ehdr.e_shnum; i++) {
1144 sec = &shdr[i];
1145 if (sec->sh_type != SHT_NOBITS)
1146 sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
1147 }
1148
1149 sec = &shdr[ehdr.e_shstrndx];
1150 shstr = sdata[ehdr.e_shstrndx];
1151
1152 /* swap relocations */
1153 for(i = 0; i < ehdr.e_shnum; i++) {
1154 sec = &shdr[i];
1155 if (sec->sh_type == SHT_RELOC) {
1156 nb_relocs = sec->sh_size / sec->sh_entsize;
1157 if (do_swap) {
1158 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
1159 elf_swap_rel(rel);
1160 }
1161 }
1162 }
1163 /* text section */
1164
1165 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
1166 if (!text_sec)
1167 error("could not find .text section");
1168 text_shndx = text_sec - shdr;
1169 text = sdata[text_shndx];
1170
1171 /* find text relocations, if any */
1172 relocs = NULL;
1173 nb_relocs = 0;
1174 i = find_reloc(text_shndx);
1175 if (i != 0) {
1176 relocs = (ELF_RELOC *)sdata[i];
1177 nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
1178 }
1179
1180 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
1181 if (!symtab_sec)
1182 error("could not find .symtab section");
1183 strtab_sec = &shdr[symtab_sec->sh_link];
1184
1185 symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
1186 strtab = sdata[symtab_sec->sh_link];
1187
1188 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
1189 if (do_swap) {
1190 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1191 swab32s(&sym->st_name);
1192 swabls(&sym->st_value);
1193 swabls(&sym->st_size);
1194 swab16s(&sym->st_shndx);
1195 }
1196 }
1197
1198 if (do_print_enum) {
1199 fprintf(outfile, "DEF(end, 0, 0)\n");
1200 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1201 const char *name, *p;
1202 name = strtab + sym->st_name;
1203 if (strstart(name, OP_PREFIX, &p)) {
1204 gen_code(name, sym->st_value, sym->st_size, outfile,
1205 text, relocs, nb_relocs, 2);
1206 }
1207 }
1208 } else {
1209 /* generate big code generation switch */
1210 #ifdef HOST_ALPHA
1211 fprintf(outfile,
1212 "register int gp asm(\"$29\");\n"
1213 "static inline void immediate_ldah(void *p, int val) {\n"
1214 " uint32_t *dest = p;\n"
1215 " long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff;\n"
1216 "\n"
1217 " *dest &= ~0xffff;\n"
1218 " *dest |= high;\n"
1219 " *dest |= 31 << 16;\n"
1220 "}\n"
1221 "static inline void immediate_lda(void *dest, int val) {\n"
1222 " *(uint16_t *) dest = val;\n"
1223 "}\n"
1224 "void fix_bsr(void *p, int offset) {\n"
1225 " uint32_t *dest = p;\n"
1226 " *dest &= ~((1 << 21) - 1);\n"
1227 " *dest |= (offset >> 2) & ((1 << 21) - 1);\n"
1228 "}\n");
1229 #endif
1230 fprintf(outfile,
1231 "int dyngen_code(uint8_t *gen_code_buf,\n"
1232 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1233 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1234 "{\n"
1235 " uint8_t *gen_code_ptr;\n"
1236 " const uint16_t *opc_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"
1248 " gen_code_ptr = gen_code_buf;\n"
1249 " opc_ptr = opc_buf;\n"
1250 " opparam_ptr = opparam_buf;\n");
1251
1252 /* Generate prologue, if needed. */
1253
1254 fprintf(outfile,
1255 " for(;;) {\n"
1256 " switch(*opc_ptr++) {\n"
1257 );
1258
1259 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1260 const char *name;
1261 name = strtab + sym->st_name;
1262 if (strstart(name, OP_PREFIX, NULL)) {
1263 #if 0
1264 printf("%4d: %s pos=0x%08x len=%d\n",
1265 i, name, sym->st_value, sym->st_size);
1266 #endif
1267 if (sym->st_shndx != (text_sec - shdr))
1268 error("invalid section for opcode (0x%x)", sym->st_shndx);
1269 gen_code(name, sym->st_value, sym->st_size, outfile,
1270 text, relocs, nb_relocs, 1);
1271 }
1272 }
1273
1274 fprintf(outfile,
1275 " default:\n"
1276 " goto the_end;\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,
1292 " }\n"
1293 " the_end:\n"
1294 );
1295
1296 /* generate epilogue */
1297 switch(ELF_ARCH) {
1298 case EM_386:
1299 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
1300 break;
1301 case EM_PPC:
1302 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
1303 break;
1304 case EM_S390:
1305 fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
1306 break;
1307 case EM_ALPHA:
1308 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n");
1309 break;
1310 case EM_IA_64:
1311 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n");
1312 break;
1313 case EM_SPARC:
1314 case EM_SPARC32PLUS:
1315 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c62008; /* jmpl %%i0 + 8, %%g0 */\n");
1316 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x01000000; /* nop */\n");
1317 break;
1318 case EM_SPARCV9:
1319 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c7e008; /* ret */\n");
1320 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81e80000; /* restore */\n");
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;
1325 default:
1326 error("unknown ELF architecture");
1327 }
1328
1329 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
1330 fprintf(outfile, "}\n\n");
1331
1332 /* generate gen_xxx functions */
1333 /* XXX: suppress the use of these functions to simplify code */
1334 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1335 const char *name;
1336 name = strtab + sym->st_name;
1337 if (strstart(name, OP_PREFIX, NULL)) {
1338 if (sym->st_shndx != (text_sec - shdr))
1339 error("invalid section for opcode (0x%x)", sym->st_shndx);
1340 gen_code(name, sym->st_value, sym->st_size, outfile,
1341 text, relocs, nb_relocs, 0);
1342 }
1343 }
1344 }
1345
1346 close(fd);
1347 return 0;
1348 }
1349
1350 void usage(void)
1351 {
1352 printf("dyngen (c) 2003 Fabrice Bellard\n"
1353 "usage: dyngen [-o outfile] [-c] objfile\n"
1354 "Generate a dynamic code generator from an object file\n"
1355 "-c output enum of operations\n"
1356 );
1357 exit(1);
1358 }
1359
1360 int main(int argc, char **argv)
1361 {
1362 int c, do_print_enum;
1363 const char *filename, *outfilename;
1364 FILE *outfile;
1365
1366 outfilename = "out.c";
1367 do_print_enum = 0;
1368 for(;;) {
1369 c = getopt(argc, argv, "ho:c");
1370 if (c == -1)
1371 break;
1372 switch(c) {
1373 case 'h':
1374 usage();
1375 break;
1376 case 'o':
1377 outfilename = optarg;
1378 break;
1379 case 'c':
1380 do_print_enum = 1;
1381 break;
1382 }
1383 }
1384 if (optind >= argc)
1385 usage();
1386 filename = argv[optind];
1387 outfile = fopen(outfilename, "w");
1388 if (!outfile)
1389 error("could not open '%s'", outfilename);
1390 load_elf(filename, outfile, do_print_enum);
1391 fclose(outfile);
1392 return 0;
1393 }