2 * Generic Dynamic compiler generator
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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.
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
33 #if defined(HOST_I386)
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
40 #elif defined(HOST_PPC)
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
47 #elif defined(HOST_S390)
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
54 #elif defined(HOST_ALPHA)
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
61 #elif defined(HOST_IA64)
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
68 #elif defined(HOST_SPARC)
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
75 #elif defined(HOST_SPARC64)
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
83 #error unsupported CPU - please update the code
88 #if ELF_CLASS == ELFCLASS32
89 typedef int32_t host_long;
90 typedef uint32_t host_ulong;
91 #define swabls(x) swab32s(x)
93 typedef int64_t host_long;
94 typedef uint64_t host_ulong;
95 #define swabls(x) swab64s(x)
100 /* all dynamically generated functions begin with this code */
101 #define OP_PREFIX "op_"
103 int elf_must_swap(struct elfhdr *h)
111 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
112 (swaptest.b[0] == 0);
115 void swab16s(uint16_t *p)
120 void swab32s(uint32_t *p)
125 void swab64s(uint64_t *p)
130 void elf_swap_ehdr(struct elfhdr *h)
132 swab16s(&h->e_type); /* Object file type */
133 swab16s(&h-> e_machine); /* Architecture */
134 swab32s(&h-> e_version); /* Object file version */
135 swabls(&h-> e_entry); /* Entry point virtual address */
136 swabls(&h-> e_phoff); /* Program header table file offset */
137 swabls(&h-> e_shoff); /* Section header table file offset */
138 swab32s(&h-> e_flags); /* Processor-specific flags */
139 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
140 swab16s(&h-> e_phentsize); /* Program header table entry size */
141 swab16s(&h-> e_phnum); /* Program header table entry count */
142 swab16s(&h-> e_shentsize); /* Section header table entry size */
143 swab16s(&h-> e_shnum); /* Section header table entry count */
144 swab16s(&h-> e_shstrndx); /* Section header string table index */
147 void elf_swap_shdr(struct elf_shdr *h)
149 swab32s(&h-> sh_name); /* Section name (string tbl index) */
150 swab32s(&h-> sh_type); /* Section type */
151 swabls(&h-> sh_flags); /* Section flags */
152 swabls(&h-> sh_addr); /* Section virtual addr at execution */
153 swabls(&h-> sh_offset); /* Section file offset */
154 swabls(&h-> sh_size); /* Section size in bytes */
155 swab32s(&h-> sh_link); /* Link to another section */
156 swab32s(&h-> sh_info); /* Additional section information */
157 swabls(&h-> sh_addralign); /* Section alignment */
158 swabls(&h-> sh_entsize); /* Entry size if section holds table */
161 void elf_swap_phdr(struct elf_phdr *h)
163 swab32s(&h->p_type); /* Segment type */
164 swabls(&h->p_offset); /* Segment file offset */
165 swabls(&h->p_vaddr); /* Segment virtual address */
166 swabls(&h->p_paddr); /* Segment physical address */
167 swabls(&h->p_filesz); /* Segment size in file */
168 swabls(&h->p_memsz); /* Segment size in memory */
169 swab32s(&h->p_flags); /* Segment flags */
170 swabls(&h->p_align); /* Segment alignment */
175 struct elf_shdr *shdr;
181 uint8_t *data_data, *sdata_data;
182 int data_shndx, sdata_shndx;
184 uint16_t get16(uint16_t *p)
193 uint32_t get32(uint32_t *p)
202 void put16(uint16_t *p, uint16_t val)
209 void put32(uint32_t *p, uint32_t val)
216 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
220 fprintf(stderr, "dyngen: ");
221 vfprintf(stderr, fmt, ap);
222 fprintf(stderr, "\n");
228 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
233 struct elf_shdr *sec;
235 for(i = 0; i < shnum; i++) {
239 shname = shstr + sec->sh_name;
240 if (!strcmp(shname, name))
246 void *load_data(int fd, long offset, unsigned int size)
253 lseek(fd, offset, SEEK_SET);
254 if (read(fd, data, size) != size) {
261 int strstart(const char *str, const char *val, const char **ptr)
279 /* generate op code */
280 void gen_code(const char *name, host_ulong offset, host_ulong size,
281 FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs, int reloc_sh_type,
285 uint8_t *p_start, *p_end;
286 host_ulong start_offset;
288 uint8_t args_present[MAX_ARGS];
289 const char *sym_name, *p;
292 /* Compute exact size excluding prologue and epilogue instructions.
293 * Increment start_offset to skip epilogue instructions, then compute
294 * copy_size the indicate the size of the remaining instructions (in
297 p_start = text + offset;
298 p_end = p_start + size;
299 start_offset = offset;
304 len = p_end - p_start;
306 error("empty code for %s", name);
307 if (p_end[-1] == 0xc3) {
310 error("ret or jmp expected at the end of %s", name);
318 p = (void *)(p_end - 4);
320 error("empty code for %s", name);
321 if (get32((uint32_t *)p) != 0x4e800020)
322 error("blr expected at the end of %s", name);
323 copy_size = p - p_start;
329 p = (void *)(p_end - 2);
331 error("empty code for %s", name);
332 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
333 error("br %%r14 expected at the end of %s", name);
334 copy_size = p - p_start;
342 error("empty code for %s", name);
343 if (get32((uint32_t *)p) != 0x6bfa8001)
344 error("ret expected at the end of %s", name);
345 copy_size = p - p_start;
351 p = (void *)(p_end - 4);
353 error("empty code for %s", name);
354 /* br.ret.sptk.many b0;; */
356 if (get32((uint32_t *)p) != 0x00840008)
357 error("br.ret.sptk.many b0;; expected at the end of %s", name);
358 copy_size = p - p_start;
364 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
366 p = (void *)(p_end - 8);
368 error("empty code for %s", name);
369 start_insn = get32((uint32_t *)(p_start + 0x0));
370 end_insn1 = get32((uint32_t *)(p + 0x0));
371 end_insn2 = get32((uint32_t *)(p + 0x4));
372 if ((start_insn & ~0x1fff) == 0x9de3a000) {
375 if ((int)(start_insn | ~0x1fff) < -128)
376 error("Found bogus save at the start of %s", name);
377 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
378 error("ret; restore; not found at end of %s", name);
380 error("No save at the beginning of %s", name);
383 /* Skip a preceeding nop, if present. */
385 skip_insn = get32((uint32_t *)(p - 0x4));
386 if (skip_insn == 0x01000000)
390 copy_size = p - p_start;
395 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
397 p = (void *)(p_end - 8);
399 error("empty code for %s", name);
400 start_insn = get32((uint32_t *)(p_start + 0x0));
401 end_insn1 = get32((uint32_t *)(p + 0x0));
402 end_insn2 = get32((uint32_t *)(p + 0x4));
403 if ((start_insn & ~0x1fff) == 0x9de3a000) {
406 if ((int)(start_insn | ~0x1fff) < -256)
407 error("Found bogus save at the start of %s", name);
408 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
409 error("ret; restore; not found at end of %s", name);
411 error("No save at the beginning of %s", name);
414 /* Skip a preceeding nop, if present. */
416 skip_insn = get32((uint32_t *)(p - 0x4));
417 if (skip_insn == 0x01000000)
421 copy_size = p - p_start;
425 error("unknown ELF architecture");
428 /* compute the number of arguments by looking at the relocations */
429 for(i = 0;i < MAX_ARGS; i++)
432 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
433 if (rel->r_offset >= start_offset &&
434 rel->r_offset < start_offset + copy_size) {
435 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
436 if (strstart(sym_name, "__op_param", &p)) {
437 n = strtoul(p, NULL, 10);
439 error("too many arguments in %s", name);
440 args_present[n - 1] = 1;
446 while (nb_args < MAX_ARGS && args_present[nb_args])
448 for(i = nb_args; i < MAX_ARGS; i++) {
450 error("inconsistent argument numbering in %s", name);
453 if (gen_switch == 2) {
454 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
455 } else if (gen_switch == 1) {
458 fprintf(outfile, "case INDEX_%s: {\n", name);
460 fprintf(outfile, " long ");
461 for(i = 0; i < nb_args; i++) {
463 fprintf(outfile, ", ");
464 fprintf(outfile, "param%d", i + 1);
466 fprintf(outfile, ";\n");
468 fprintf(outfile, " extern void %s();\n", name);
470 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
471 if (rel->r_offset >= start_offset &&
472 rel->r_offset < start_offset + copy_size) {
473 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
475 !strstart(sym_name, "__op_param", NULL) &&
476 !strstart(sym_name, "__op_jmp", NULL)) {
477 #if defined(HOST_SPARC)
478 if (sym_name[0] == '.') {
480 "extern char __dot_%s __asm__(\"%s\");\n",
481 sym_name+1, sym_name);
485 fprintf(outfile, "extern char %s;\n", sym_name);
490 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
492 /* emit code offset information */
495 const char *sym_name, *p;
499 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
500 sym_name = strtab + sym->st_name;
501 if (strstart(sym_name, "__op_label", &p)) {
504 /* test if the variable refers to a label inside
505 the code we are generating */
506 if (sym->st_shndx == data_shndx)
508 else if (sym->st_shndx == sdata_shndx)
511 error("__op_labelN symbols must be in .data or .sdata section");
512 val = *(target_ulong *)(ptr + sym->st_value);
513 if (val >= start_offset && val < start_offset + copy_size) {
514 n = strtol(p, NULL, 10);
515 fprintf(outfile, " label_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
521 /* load parameres in variables */
522 for(i = 0; i < nb_args; i++) {
523 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
526 /* patch relocations */
527 #if defined(HOST_I386)
532 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
533 if (rel->r_offset >= start_offset &&
534 rel->r_offset < start_offset + copy_size) {
535 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
536 if (strstart(sym_name, "__op_param", &p)) {
537 snprintf(name, sizeof(name), "param%s", p);
539 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
541 type = ELF32_R_TYPE(rel->r_info);
542 addend = get32((uint32_t *)(text + rel->r_offset));
545 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
546 rel->r_offset - start_offset, name, addend);
549 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
550 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
553 error("unsupported i386 relocation (%d)", type);
558 #elif defined(HOST_PPC)
563 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
564 if (rel->r_offset >= start_offset &&
565 rel->r_offset < start_offset + copy_size) {
566 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
567 if (strstart(sym_name, "__op_jmp", &p)) {
569 n = strtol(p, NULL, 10);
570 /* __op_jmp relocations are done at
571 runtime to do translated block
572 chaining: the offset of the instruction
573 needs to be stored */
574 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
575 n, rel->r_offset - start_offset);
579 if (strstart(sym_name, "__op_param", &p)) {
580 snprintf(name, sizeof(name), "param%s", p);
582 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
584 type = ELF32_R_TYPE(rel->r_info);
585 addend = rel->r_addend;
588 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
589 rel->r_offset - start_offset, name, addend);
591 case R_PPC_ADDR16_LO:
592 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
593 rel->r_offset - start_offset, name, addend);
595 case R_PPC_ADDR16_HI:
596 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
597 rel->r_offset - start_offset, name, addend);
599 case R_PPC_ADDR16_HA:
600 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
601 rel->r_offset - start_offset, name, addend);
604 /* warning: must be at 32 MB distancy */
605 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
606 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
609 error("unsupported powerpc relocation (%d)", type);
614 #elif defined(HOST_S390)
619 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
620 if (rel->r_offset >= start_offset &&
621 rel->r_offset < start_offset + copy_size) {
622 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
623 if (strstart(sym_name, "__op_param", &p)) {
624 snprintf(name, sizeof(name), "param%s", p);
626 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
628 type = ELF32_R_TYPE(rel->r_info);
629 addend = rel->r_addend;
632 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
633 rel->r_offset - start_offset, name, addend);
636 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
637 rel->r_offset - start_offset, name, addend);
640 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
641 rel->r_offset - start_offset, name, addend);
644 error("unsupported s390 relocation (%d)", type);
649 #elif defined(HOST_ALPHA)
651 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
652 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
655 type = ELF64_R_TYPE(rel->r_info);
656 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
659 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
660 as an immediate instead of constructing it from the pv or ra. */
661 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
662 rel->r_offset - start_offset);
663 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
664 rel->r_offset - start_offset + rel->r_addend);
667 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
668 now, since some called functions (libc) need pv to be set up. */
671 /* Branch target prediction hint. Ignore for now. Should be already
672 correct for in-function jumps. */
674 case R_ALPHA_LITERAL:
675 /* Load a literal from the GOT relative to the gp. Since there's only a
676 single gp, nothing is to be done. */
678 case R_ALPHA_GPRELHIGH:
679 /* Handle fake relocations against __op_param symbol. Need to emit the
680 high part of the immediate value instead. Other symbols need no
681 special treatment. */
682 if (strstart(sym_name, "__op_param", &p))
683 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
684 rel->r_offset - start_offset, p);
686 case R_ALPHA_GPRELLOW:
687 if (strstart(sym_name, "__op_param", &p))
688 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
689 rel->r_offset - start_offset, p);
692 /* PC-relative jump. Tweak offset to skip the two instructions that try to
693 set up the gp from the pv. */
694 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
695 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
698 error("unsupported Alpha relocation (%d)", type);
703 #elif defined(HOST_IA64)
708 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
709 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
710 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
711 if (strstart(sym_name, "__op_param", &p)) {
712 snprintf(name, sizeof(name), "param%s", p);
714 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
716 type = ELF64_R_TYPE(rel->r_info);
717 addend = rel->r_addend;
720 error("must implemnt R_IA64_LTOFF22 relocation");
721 case R_IA64_PCREL21B:
722 error("must implemnt R_IA64_PCREL21B relocation");
724 error("unsupported ia64 relocation (%d)", type);
729 #elif defined(HOST_SPARC)
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[ELF32_R_SYM(rel->r_info)].st_name;
738 if (strstart(sym_name, "__op_param", &p)) {
739 snprintf(name, sizeof(name), "param%s", p);
741 if (sym_name[0] == '.')
742 snprintf(name, sizeof(name),
746 snprintf(name, sizeof(name),
747 "(long)(&%s)", sym_name);
749 type = ELF32_R_TYPE(rel->r_info);
750 addend = rel->r_addend;
753 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
754 rel->r_offset - start_offset, name, addend);
758 " *(uint32_t *)(gen_code_ptr + %d) = "
759 "((*(uint32_t *)(gen_code_ptr + %d)) "
761 " | (((%s + %d) >> 10) & 0x3fffff);\n",
762 rel->r_offset - start_offset,
763 rel->r_offset - start_offset,
768 " *(uint32_t *)(gen_code_ptr + %d) = "
769 "((*(uint32_t *)(gen_code_ptr + %d)) "
771 " | ((%s + %d) & 0x3ff);\n",
772 rel->r_offset - start_offset,
773 rel->r_offset - start_offset,
776 case R_SPARC_WDISP30:
778 " *(uint32_t *)(gen_code_ptr + %d) = "
779 "((*(uint32_t *)(gen_code_ptr + %d)) "
781 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
783 rel->r_offset - start_offset,
784 rel->r_offset - start_offset,
786 rel->r_offset - start_offset);
789 error("unsupported sparc relocation (%d)", type);
794 #elif defined(HOST_SPARC64)
799 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
800 if (rel->r_offset >= start_offset &&
801 rel->r_offset < start_offset + copy_size) {
802 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
803 if (strstart(sym_name, "__op_param", &p)) {
804 snprintf(name, sizeof(name), "param%s", p);
806 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
808 type = ELF64_R_TYPE(rel->r_info);
809 addend = rel->r_addend;
812 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
813 rel->r_offset - start_offset, name, addend);
817 " *(uint32_t *)(gen_code_ptr + %d) = "
818 "((*(uint32_t *)(gen_code_ptr + %d)) "
820 " | (((%s + %d) >> 10) & 0x3fffff);\n",
821 rel->r_offset - start_offset,
822 rel->r_offset - start_offset,
827 " *(uint32_t *)(gen_code_ptr + %d) = "
828 "((*(uint32_t *)(gen_code_ptr + %d)) "
830 " | ((%s + %d) & 0x3ff);\n",
831 rel->r_offset - start_offset,
832 rel->r_offset - start_offset,
835 case R_SPARC_WDISP30:
837 " *(uint32_t *)(gen_code_ptr + %d) = "
838 "((*(uint32_t *)(gen_code_ptr + %d)) "
840 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
842 rel->r_offset - start_offset,
843 rel->r_offset - start_offset,
845 rel->r_offset - start_offset);
848 error("unsupported sparc64 relocation (%d)", type);
854 #error unsupported CPU
856 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
857 fprintf(outfile, "}\n");
858 fprintf(outfile, "break;\n\n");
860 fprintf(outfile, "static inline void gen_%s(", name);
862 fprintf(outfile, "void");
864 for(i = 0; i < nb_args; i++) {
866 fprintf(outfile, ", ");
867 fprintf(outfile, "long param%d", i + 1);
870 fprintf(outfile, ")\n");
871 fprintf(outfile, "{\n");
872 for(i = 0; i < nb_args; i++) {
873 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
875 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
876 fprintf(outfile, "}\n\n");
880 /* load an elf object file */
881 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
884 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
890 int nb_relocs, reloc_sh_type;
892 fd = open(filename, O_RDONLY);
894 error("can't open file '%s'", filename);
896 /* Read ELF header. */
897 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
898 error("unable to read file header");
900 /* Check ELF identification. */
901 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
902 || ehdr.e_ident[EI_MAG1] != ELFMAG1
903 || ehdr.e_ident[EI_MAG2] != ELFMAG2
904 || ehdr.e_ident[EI_MAG3] != ELFMAG3
905 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
906 error("bad ELF header");
909 do_swap = elf_must_swap(&ehdr);
911 elf_swap_ehdr(&ehdr);
912 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
913 error("Unsupported ELF class");
914 if (ehdr.e_type != ET_REL)
915 error("ELF object file expected");
916 if (ehdr.e_version != EV_CURRENT)
917 error("Invalid ELF version");
918 if (!elf_check_arch(ehdr.e_machine))
919 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
921 /* read section headers */
922 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
924 for(i = 0; i < ehdr.e_shnum; i++) {
925 elf_swap_shdr(&shdr[i]);
929 sec = &shdr[ehdr.e_shstrndx];
930 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
934 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
936 error("could not find .text section");
937 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
940 sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".data");
942 data_shndx = sec - shdr;
943 data_data = load_data(fd, sec->sh_offset, sec->sh_size);
946 sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".sdata");
948 sdata_shndx = sec - shdr;
949 sdata_data = load_data(fd, sec->sh_offset, sec->sh_size);
952 /* find text relocations, if any */
956 for(i = 0; i < ehdr.e_shnum; i++) {
958 if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) &&
959 sec->sh_info == (text_sec - shdr)) {
960 reloc_sh_type = sec->sh_type;
961 relocs = load_data(fd, sec->sh_offset, sec->sh_size);
962 nb_relocs = sec->sh_size / sec->sh_entsize;
964 if (sec->sh_type == SHT_REL) {
965 ElfW(Rel) *rel = relocs;
966 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
967 swabls(&rel->r_offset);
968 swabls(&rel->r_info);
971 ElfW(Rela) *rel = relocs;
972 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
973 swabls(&rel->r_offset);
974 swabls(&rel->r_info);
975 swabls(&rel->r_addend);
983 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
985 error("could not find .symtab section");
986 strtab_sec = &shdr[symtab_sec->sh_link];
988 symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size);
989 strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size);
991 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
993 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
994 swab32s(&sym->st_name);
995 swabls(&sym->st_value);
996 swabls(&sym->st_size);
997 swab16s(&sym->st_shndx);
1001 if (do_print_enum) {
1002 fprintf(outfile, "DEF(end, 0, 0)\n");
1003 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1004 const char *name, *p;
1005 name = strtab + sym->st_name;
1006 if (strstart(name, OP_PREFIX, &p)) {
1007 gen_code(name, sym->st_value, sym->st_size, outfile,
1008 text, relocs, nb_relocs, reloc_sh_type, 2);
1012 /* generate big code generation switch */
1015 "register int gp asm(\"$29\");\n"
1016 "static inline void immediate_ldah(void *p, int val) {\n"
1017 " uint32_t *dest = p;\n"
1018 " long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff;\n"
1020 " *dest &= ~0xffff;\n"
1022 " *dest |= 31 << 16;\n"
1024 "static inline void immediate_lda(void *dest, int val) {\n"
1025 " *(uint16_t *) dest = val;\n"
1027 "void fix_bsr(void *p, int offset) {\n"
1028 " uint32_t *dest = p;\n"
1029 " *dest &= ~((1 << 21) - 1);\n"
1030 " *dest |= (offset >> 2) & ((1 << 21) - 1);\n"
1034 "int dyngen_code(uint8_t *gen_code_buf,\n"
1035 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1036 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1038 " uint8_t *gen_code_ptr;\n"
1039 " const uint16_t *opc_ptr;\n"
1040 " const uint32_t *opparam_ptr;\n"
1041 " gen_code_ptr = gen_code_buf;\n"
1042 " opc_ptr = opc_buf;\n"
1043 " opparam_ptr = opparam_buf;\n");
1045 /* Generate prologue, if needed. */
1048 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c23a080; /* sub %%sp, 128, %%sp */\n");
1049 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc27a080; /* sub %%fp, 128, %%fp */\n");
1053 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c23a100; /* sub %%sp, 256, %%sp */\n");
1054 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc27a100; /* sub %%fp, 256, %%fp */\n");
1060 " switch(*opc_ptr++) {\n"
1063 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1065 name = strtab + sym->st_name;
1066 if (strstart(name, OP_PREFIX, NULL)) {
1068 printf("%4d: %s pos=0x%08x len=%d\n",
1069 i, name, sym->st_value, sym->st_size);
1071 if (sym->st_shndx != (text_sec - shdr))
1072 error("invalid section for opcode (0x%x)", sym->st_shndx);
1073 gen_code(name, sym->st_value, sym->st_size, outfile,
1074 text, relocs, nb_relocs, reloc_sh_type, 1);
1086 /* generate epilogue */
1089 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
1092 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
1095 fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
1098 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n");
1101 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n");
1104 case EM_SPARC32PLUS:
1105 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc07a080; /* add %%fp, 256, %%fp */\n");
1106 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c62008; /* jmpl %%i0 + 8, %%g0 */\n");
1107 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c03a080; /* add %%sp, 256, %%sp */\n");
1110 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c7e008; /* ret */\n");
1111 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81e80000; /* restore */\n");
1114 error("unknown ELF architecture");
1117 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
1118 fprintf(outfile, "}\n\n");
1120 /* generate gen_xxx functions */
1121 /* XXX: suppress the use of these functions to simplify code */
1122 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1124 name = strtab + sym->st_name;
1125 if (strstart(name, OP_PREFIX, NULL)) {
1126 if (sym->st_shndx != (text_sec - shdr))
1127 error("invalid section for opcode (0x%x)", sym->st_shndx);
1128 gen_code(name, sym->st_value, sym->st_size, outfile,
1129 text, relocs, nb_relocs, reloc_sh_type, 0);
1140 printf("dyngen (c) 2003 Fabrice Bellard\n"
1141 "usage: dyngen [-o outfile] [-c] objfile\n"
1142 "Generate a dynamic code generator from an object file\n"
1143 "-c output enum of operations\n"
1148 int main(int argc, char **argv)
1150 int c, do_print_enum;
1151 const char *filename, *outfilename;
1154 outfilename = "out.c";
1157 c = getopt(argc, argv, "ho:c");
1165 outfilename = optarg;
1174 filename = argv[optind];
1175 outfile = fopen(outfilename, "w");
1177 error("could not open '%s'", outfilename);
1178 load_elf(filename, outfile, do_print_enum);