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)
98 #ifdef ELF_USES_RELOCA
99 #define SHT_RELOC SHT_RELA
101 #define SHT_RELOC SHT_REL
106 /* all dynamically generated functions begin with this code */
107 #define OP_PREFIX "op_"
109 int elf_must_swap(struct elfhdr *h)
117 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
118 (swaptest.b[0] == 0);
121 void swab16s(uint16_t *p)
126 void swab32s(uint32_t *p)
131 void swab64s(uint64_t *p)
136 void elf_swap_ehdr(struct elfhdr *h)
138 swab16s(&h->e_type); /* Object file type */
139 swab16s(&h-> e_machine); /* Architecture */
140 swab32s(&h-> e_version); /* Object file version */
141 swabls(&h-> e_entry); /* Entry point virtual address */
142 swabls(&h-> e_phoff); /* Program header table file offset */
143 swabls(&h-> e_shoff); /* Section header table file offset */
144 swab32s(&h-> e_flags); /* Processor-specific flags */
145 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
146 swab16s(&h-> e_phentsize); /* Program header table entry size */
147 swab16s(&h-> e_phnum); /* Program header table entry count */
148 swab16s(&h-> e_shentsize); /* Section header table entry size */
149 swab16s(&h-> e_shnum); /* Section header table entry count */
150 swab16s(&h-> e_shstrndx); /* Section header string table index */
153 void elf_swap_shdr(struct elf_shdr *h)
155 swab32s(&h-> sh_name); /* Section name (string tbl index) */
156 swab32s(&h-> sh_type); /* Section type */
157 swabls(&h-> sh_flags); /* Section flags */
158 swabls(&h-> sh_addr); /* Section virtual addr at execution */
159 swabls(&h-> sh_offset); /* Section file offset */
160 swabls(&h-> sh_size); /* Section size in bytes */
161 swab32s(&h-> sh_link); /* Link to another section */
162 swab32s(&h-> sh_info); /* Additional section information */
163 swabls(&h-> sh_addralign); /* Section alignment */
164 swabls(&h-> sh_entsize); /* Entry size if section holds table */
167 void elf_swap_phdr(struct elf_phdr *h)
169 swab32s(&h->p_type); /* Segment type */
170 swabls(&h->p_offset); /* Segment file offset */
171 swabls(&h->p_vaddr); /* Segment virtual address */
172 swabls(&h->p_paddr); /* Segment physical address */
173 swabls(&h->p_filesz); /* Segment size in file */
174 swabls(&h->p_memsz); /* Segment size in memory */
175 swab32s(&h->p_flags); /* Segment flags */
176 swabls(&h->p_align); /* Segment alignment */
179 void elf_swap_rel(ELF_RELOC *rel)
181 swabls(&rel->r_offset);
182 swabls(&rel->r_info);
183 #ifdef ELF_USES_RELOCA
184 swabls(&rel->r_addend);
190 struct elf_shdr *shdr;
198 uint16_t get16(uint16_t *p)
207 uint32_t get32(uint32_t *p)
216 void put16(uint16_t *p, uint16_t val)
223 void put32(uint32_t *p, uint32_t val)
230 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
234 fprintf(stderr, "dyngen: ");
235 vfprintf(stderr, fmt, ap);
236 fprintf(stderr, "\n");
242 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
247 struct elf_shdr *sec;
249 for(i = 0; i < shnum; i++) {
253 shname = shstr + sec->sh_name;
254 if (!strcmp(shname, name))
260 int find_reloc(int sh_index)
262 struct elf_shdr *sec;
265 for(i = 0; i < ehdr.e_shnum; i++) {
267 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
273 void *load_data(int fd, long offset, unsigned int size)
280 lseek(fd, offset, SEEK_SET);
281 if (read(fd, data, size) != size) {
288 int strstart(const char *str, const char *val, const char **ptr)
306 /* generate op code */
307 void gen_code(const char *name, host_ulong offset, host_ulong size,
308 FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs,
312 uint8_t *p_start, *p_end;
313 host_ulong start_offset;
315 uint8_t args_present[MAX_ARGS];
316 const char *sym_name, *p;
319 /* Compute exact size excluding prologue and epilogue instructions.
320 * Increment start_offset to skip epilogue instructions, then compute
321 * copy_size the indicate the size of the remaining instructions (in
324 p_start = text + offset;
325 p_end = p_start + size;
326 start_offset = offset;
331 len = p_end - p_start;
333 error("empty code for %s", name);
334 if (p_end[-1] == 0xc3) {
337 error("ret or jmp expected at the end of %s", name);
345 p = (void *)(p_end - 4);
347 error("empty code for %s", name);
348 if (get32((uint32_t *)p) != 0x4e800020)
349 error("blr expected at the end of %s", name);
350 copy_size = p - p_start;
356 p = (void *)(p_end - 2);
358 error("empty code for %s", name);
359 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
360 error("br %%r14 expected at the end of %s", name);
361 copy_size = p - p_start;
369 error("empty code for %s", name);
370 if (get32((uint32_t *)p) != 0x6bfa8001)
371 error("ret expected at the end of %s", name);
372 copy_size = p - p_start;
378 p = (void *)(p_end - 4);
380 error("empty code for %s", name);
381 /* br.ret.sptk.many b0;; */
383 if (get32((uint32_t *)p) != 0x00840008)
384 error("br.ret.sptk.many b0;; expected at the end of %s", name);
385 copy_size = p - p_start;
391 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
393 p = (void *)(p_end - 8);
395 error("empty code for %s", name);
396 start_insn = get32((uint32_t *)(p_start + 0x0));
397 end_insn1 = get32((uint32_t *)(p + 0x0));
398 end_insn2 = get32((uint32_t *)(p + 0x4));
399 if ((start_insn & ~0x1fff) == 0x9de3a000) {
402 if ((int)(start_insn | ~0x1fff) < -128)
403 error("Found bogus save at the start of %s", name);
404 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
405 error("ret; restore; not found at end of %s", name);
407 error("No save at the beginning of %s", name);
410 /* Skip a preceeding nop, if present. */
412 skip_insn = get32((uint32_t *)(p - 0x4));
413 if (skip_insn == 0x01000000)
417 copy_size = p - p_start;
422 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
424 p = (void *)(p_end - 8);
426 error("empty code for %s", name);
427 start_insn = get32((uint32_t *)(p_start + 0x0));
428 end_insn1 = get32((uint32_t *)(p + 0x0));
429 end_insn2 = get32((uint32_t *)(p + 0x4));
430 if ((start_insn & ~0x1fff) == 0x9de3a000) {
433 if ((int)(start_insn | ~0x1fff) < -256)
434 error("Found bogus save at the start of %s", name);
435 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
436 error("ret; restore; not found at end of %s", name);
438 error("No save at the beginning of %s", name);
441 /* Skip a preceeding nop, if present. */
443 skip_insn = get32((uint32_t *)(p - 0x4));
444 if (skip_insn == 0x01000000)
448 copy_size = p - p_start;
452 error("unknown ELF architecture");
455 /* compute the number of arguments by looking at the relocations */
456 for(i = 0;i < MAX_ARGS; i++)
459 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
460 if (rel->r_offset >= start_offset &&
461 rel->r_offset < start_offset + copy_size) {
462 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
463 if (strstart(sym_name, "__op_param", &p)) {
464 n = strtoul(p, NULL, 10);
466 error("too many arguments in %s", name);
467 args_present[n - 1] = 1;
473 while (nb_args < MAX_ARGS && args_present[nb_args])
475 for(i = nb_args; i < MAX_ARGS; i++) {
477 error("inconsistent argument numbering in %s", name);
480 if (gen_switch == 2) {
481 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
482 } else if (gen_switch == 1) {
485 fprintf(outfile, "case INDEX_%s: {\n", name);
487 fprintf(outfile, " long ");
488 for(i = 0; i < nb_args; i++) {
490 fprintf(outfile, ", ");
491 fprintf(outfile, "param%d", i + 1);
493 fprintf(outfile, ";\n");
495 fprintf(outfile, " extern void %s();\n", name);
497 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
498 if (rel->r_offset >= start_offset &&
499 rel->r_offset < start_offset + copy_size) {
500 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
502 !strstart(sym_name, "__op_param", NULL) &&
503 !strstart(sym_name, "__op_jmp", NULL)) {
504 #if defined(HOST_SPARC)
505 if (sym_name[0] == '.') {
507 "extern char __dot_%s __asm__(\"%s\");\n",
508 sym_name+1, sym_name);
512 fprintf(outfile, "extern char %s;\n", sym_name);
517 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
519 /* emit code offset information */
522 const char *sym_name, *p;
526 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
527 sym_name = strtab + sym->st_name;
528 if (strstart(sym_name, "__op_label", &p)) {
531 unsigned long offset;
533 /* test if the variable refers to a label inside
534 the code we are generating */
535 ptr = sdata[sym->st_shndx];
537 error("__op_labelN in invalid section");
538 offset = sym->st_value;
540 #ifdef ELF_USES_RELOCA
542 int reloc_shndx, nb_relocs1, j;
544 /* try to find a matching relocation */
545 reloc_shndx = find_reloc(sym->st_shndx);
547 nb_relocs1 = shdr[reloc_shndx].sh_size /
548 shdr[reloc_shndx].sh_entsize;
549 rel = (ELF_RELOC *)sdata[reloc_shndx];
550 for(j = 0; j < nb_relocs1; j++) {
551 if (rel->r_offset == offset) {
552 addend = rel->r_addend;
560 val = *(target_ulong *)(ptr + offset);
563 if (val >= start_offset && val < start_offset + copy_size) {
564 n = strtol(p, NULL, 10);
565 fprintf(outfile, " label_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
571 /* load parameres in variables */
572 for(i = 0; i < nb_args; i++) {
573 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
576 /* patch relocations */
577 #if defined(HOST_I386)
582 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
583 if (rel->r_offset >= start_offset &&
584 rel->r_offset < start_offset + copy_size) {
585 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
586 if (strstart(sym_name, "__op_param", &p)) {
587 snprintf(name, sizeof(name), "param%s", p);
589 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
591 type = ELF32_R_TYPE(rel->r_info);
592 addend = get32((uint32_t *)(text + rel->r_offset));
595 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
596 rel->r_offset - start_offset, name, addend);
599 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
600 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
603 error("unsupported i386 relocation (%d)", type);
608 #elif defined(HOST_PPC)
613 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
614 if (rel->r_offset >= start_offset &&
615 rel->r_offset < start_offset + copy_size) {
616 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
617 if (strstart(sym_name, "__op_jmp", &p)) {
619 n = strtol(p, NULL, 10);
620 /* __op_jmp relocations are done at
621 runtime to do translated block
622 chaining: the offset of the instruction
623 needs to be stored */
624 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
625 n, rel->r_offset - start_offset);
629 if (strstart(sym_name, "__op_param", &p)) {
630 snprintf(name, sizeof(name), "param%s", p);
632 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
634 type = ELF32_R_TYPE(rel->r_info);
635 addend = rel->r_addend;
638 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
639 rel->r_offset - start_offset, name, addend);
641 case R_PPC_ADDR16_LO:
642 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
643 rel->r_offset - start_offset, name, addend);
645 case R_PPC_ADDR16_HI:
646 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
647 rel->r_offset - start_offset, name, addend);
649 case R_PPC_ADDR16_HA:
650 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
651 rel->r_offset - start_offset, name, addend);
654 /* warning: must be at 32 MB distancy */
655 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
656 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
659 error("unsupported powerpc relocation (%d)", type);
664 #elif defined(HOST_S390)
669 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
670 if (rel->r_offset >= start_offset &&
671 rel->r_offset < start_offset + copy_size) {
672 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
673 if (strstart(sym_name, "__op_param", &p)) {
674 snprintf(name, sizeof(name), "param%s", p);
676 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
678 type = ELF32_R_TYPE(rel->r_info);
679 addend = rel->r_addend;
682 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
683 rel->r_offset - start_offset, name, addend);
686 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
687 rel->r_offset - start_offset, name, addend);
690 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
691 rel->r_offset - start_offset, name, addend);
694 error("unsupported s390 relocation (%d)", type);
699 #elif defined(HOST_ALPHA)
701 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
702 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
705 type = ELF64_R_TYPE(rel->r_info);
706 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
709 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
710 as an immediate instead of constructing it from the pv or ra. */
711 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
712 rel->r_offset - start_offset);
713 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
714 rel->r_offset - start_offset + rel->r_addend);
717 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
718 now, since some called functions (libc) need pv to be set up. */
721 /* Branch target prediction hint. Ignore for now. Should be already
722 correct for in-function jumps. */
724 case R_ALPHA_LITERAL:
725 /* Load a literal from the GOT relative to the gp. Since there's only a
726 single gp, nothing is to be done. */
728 case R_ALPHA_GPRELHIGH:
729 /* Handle fake relocations against __op_param symbol. Need to emit the
730 high part of the immediate value instead. Other symbols need no
731 special treatment. */
732 if (strstart(sym_name, "__op_param", &p))
733 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
734 rel->r_offset - start_offset, p);
736 case R_ALPHA_GPRELLOW:
737 if (strstart(sym_name, "__op_param", &p))
738 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
739 rel->r_offset - start_offset, p);
742 /* PC-relative jump. Tweak offset to skip the two instructions that try to
743 set up the gp from the pv. */
744 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
745 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
748 error("unsupported Alpha relocation (%d)", type);
753 #elif defined(HOST_IA64)
758 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
759 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
760 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
761 if (strstart(sym_name, "__op_param", &p)) {
762 snprintf(name, sizeof(name), "param%s", p);
764 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
766 type = ELF64_R_TYPE(rel->r_info);
767 addend = rel->r_addend;
770 error("must implemnt R_IA64_LTOFF22 relocation");
771 case R_IA64_PCREL21B:
772 error("must implemnt R_IA64_PCREL21B relocation");
774 error("unsupported ia64 relocation (%d)", type);
779 #elif defined(HOST_SPARC)
784 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
785 if (rel->r_offset >= start_offset &&
786 rel->r_offset < start_offset + copy_size) {
787 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
788 if (strstart(sym_name, "__op_param", &p)) {
789 snprintf(name, sizeof(name), "param%s", p);
791 if (sym_name[0] == '.')
792 snprintf(name, sizeof(name),
796 snprintf(name, sizeof(name),
797 "(long)(&%s)", sym_name);
799 type = ELF32_R_TYPE(rel->r_info);
800 addend = rel->r_addend;
803 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
804 rel->r_offset - start_offset, name, addend);
808 " *(uint32_t *)(gen_code_ptr + %d) = "
809 "((*(uint32_t *)(gen_code_ptr + %d)) "
811 " | (((%s + %d) >> 10) & 0x3fffff);\n",
812 rel->r_offset - start_offset,
813 rel->r_offset - start_offset,
818 " *(uint32_t *)(gen_code_ptr + %d) = "
819 "((*(uint32_t *)(gen_code_ptr + %d)) "
821 " | ((%s + %d) & 0x3ff);\n",
822 rel->r_offset - start_offset,
823 rel->r_offset - start_offset,
826 case R_SPARC_WDISP30:
828 " *(uint32_t *)(gen_code_ptr + %d) = "
829 "((*(uint32_t *)(gen_code_ptr + %d)) "
831 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
833 rel->r_offset - start_offset,
834 rel->r_offset - start_offset,
836 rel->r_offset - start_offset);
839 error("unsupported sparc relocation (%d)", type);
844 #elif defined(HOST_SPARC64)
849 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
850 if (rel->r_offset >= start_offset &&
851 rel->r_offset < start_offset + copy_size) {
852 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
853 if (strstart(sym_name, "__op_param", &p)) {
854 snprintf(name, sizeof(name), "param%s", p);
856 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
858 type = ELF64_R_TYPE(rel->r_info);
859 addend = rel->r_addend;
862 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
863 rel->r_offset - start_offset, name, addend);
867 " *(uint32_t *)(gen_code_ptr + %d) = "
868 "((*(uint32_t *)(gen_code_ptr + %d)) "
870 " | (((%s + %d) >> 10) & 0x3fffff);\n",
871 rel->r_offset - start_offset,
872 rel->r_offset - start_offset,
877 " *(uint32_t *)(gen_code_ptr + %d) = "
878 "((*(uint32_t *)(gen_code_ptr + %d)) "
880 " | ((%s + %d) & 0x3ff);\n",
881 rel->r_offset - start_offset,
882 rel->r_offset - start_offset,
885 case R_SPARC_WDISP30:
887 " *(uint32_t *)(gen_code_ptr + %d) = "
888 "((*(uint32_t *)(gen_code_ptr + %d)) "
890 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
892 rel->r_offset - start_offset,
893 rel->r_offset - start_offset,
895 rel->r_offset - start_offset);
898 error("unsupported sparc64 relocation (%d)", type);
904 #error unsupported CPU
906 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
907 fprintf(outfile, "}\n");
908 fprintf(outfile, "break;\n\n");
910 fprintf(outfile, "static inline void gen_%s(", name);
912 fprintf(outfile, "void");
914 for(i = 0; i < nb_args; i++) {
916 fprintf(outfile, ", ");
917 fprintf(outfile, "long param%d", i + 1);
920 fprintf(outfile, ")\n");
921 fprintf(outfile, "{\n");
922 for(i = 0; i < nb_args; i++) {
923 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
925 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
926 fprintf(outfile, "}\n\n");
930 /* load an elf object file */
931 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
934 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
943 fd = open(filename, O_RDONLY);
945 error("can't open file '%s'", filename);
947 /* Read ELF header. */
948 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
949 error("unable to read file header");
951 /* Check ELF identification. */
952 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
953 || ehdr.e_ident[EI_MAG1] != ELFMAG1
954 || ehdr.e_ident[EI_MAG2] != ELFMAG2
955 || ehdr.e_ident[EI_MAG3] != ELFMAG3
956 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
957 error("bad ELF header");
960 do_swap = elf_must_swap(&ehdr);
962 elf_swap_ehdr(&ehdr);
963 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
964 error("Unsupported ELF class");
965 if (ehdr.e_type != ET_REL)
966 error("ELF object file expected");
967 if (ehdr.e_version != EV_CURRENT)
968 error("Invalid ELF version");
969 if (!elf_check_arch(ehdr.e_machine))
970 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
972 /* read section headers */
973 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
975 for(i = 0; i < ehdr.e_shnum; i++) {
976 elf_swap_shdr(&shdr[i]);
980 /* read all section data */
981 sdata = malloc(sizeof(void *) * ehdr.e_shnum);
982 memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
984 for(i = 0;i < ehdr.e_shnum; i++) {
986 if (sec->sh_type != SHT_NOBITS)
987 sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
990 sec = &shdr[ehdr.e_shstrndx];
991 shstr = sdata[ehdr.e_shstrndx];
993 /* swap relocations */
994 for(i = 0; i < ehdr.e_shnum; i++) {
996 if (sec->sh_type == SHT_RELOC) {
997 nb_relocs = sec->sh_size / sec->sh_entsize;
999 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
1006 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
1008 error("could not find .text section");
1009 text_shndx = text_sec - shdr;
1010 text = sdata[text_shndx];
1012 /* find text relocations, if any */
1015 i = find_reloc(text_shndx);
1017 relocs = (ELF_RELOC *)sdata[i];
1018 nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
1021 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
1023 error("could not find .symtab section");
1024 strtab_sec = &shdr[symtab_sec->sh_link];
1026 symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
1027 strtab = sdata[symtab_sec->sh_link];
1029 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
1031 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1032 swab32s(&sym->st_name);
1033 swabls(&sym->st_value);
1034 swabls(&sym->st_size);
1035 swab16s(&sym->st_shndx);
1039 if (do_print_enum) {
1040 fprintf(outfile, "DEF(end, 0, 0)\n");
1041 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1042 const char *name, *p;
1043 name = strtab + sym->st_name;
1044 if (strstart(name, OP_PREFIX, &p)) {
1045 gen_code(name, sym->st_value, sym->st_size, outfile,
1046 text, relocs, nb_relocs, 2);
1050 /* generate big code generation switch */
1053 "register int gp asm(\"$29\");\n"
1054 "static inline void immediate_ldah(void *p, int val) {\n"
1055 " uint32_t *dest = p;\n"
1056 " long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff;\n"
1058 " *dest &= ~0xffff;\n"
1060 " *dest |= 31 << 16;\n"
1062 "static inline void immediate_lda(void *dest, int val) {\n"
1063 " *(uint16_t *) dest = val;\n"
1065 "void fix_bsr(void *p, int offset) {\n"
1066 " uint32_t *dest = p;\n"
1067 " *dest &= ~((1 << 21) - 1);\n"
1068 " *dest |= (offset >> 2) & ((1 << 21) - 1);\n"
1072 "int dyngen_code(uint8_t *gen_code_buf,\n"
1073 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1074 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1076 " uint8_t *gen_code_ptr;\n"
1077 " const uint16_t *opc_ptr;\n"
1078 " const uint32_t *opparam_ptr;\n"
1079 " gen_code_ptr = gen_code_buf;\n"
1080 " opc_ptr = opc_buf;\n"
1081 " opparam_ptr = opparam_buf;\n");
1083 /* Generate prologue, if needed. */
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");
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");
1098 " switch(*opc_ptr++) {\n"
1101 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1103 name = strtab + sym->st_name;
1104 if (strstart(name, OP_PREFIX, NULL)) {
1106 printf("%4d: %s pos=0x%08x len=%d\n",
1107 i, name, sym->st_value, sym->st_size);
1109 if (sym->st_shndx != (text_sec - shdr))
1110 error("invalid section for opcode (0x%x)", sym->st_shndx);
1111 gen_code(name, sym->st_value, sym->st_size, outfile,
1112 text, relocs, nb_relocs, 1);
1124 /* generate epilogue */
1127 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
1130 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
1133 fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
1136 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n");
1139 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n");
1142 case EM_SPARC32PLUS:
1143 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc07a080; /* add %%fp, 256, %%fp */\n");
1144 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");
1148 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c7e008; /* ret */\n");
1149 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81e80000; /* restore */\n");
1152 error("unknown ELF architecture");
1155 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
1156 fprintf(outfile, "}\n\n");
1158 /* generate gen_xxx functions */
1159 /* XXX: suppress the use of these functions to simplify code */
1160 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1162 name = strtab + sym->st_name;
1163 if (strstart(name, OP_PREFIX, NULL)) {
1164 if (sym->st_shndx != (text_sec - shdr))
1165 error("invalid section for opcode (0x%x)", sym->st_shndx);
1166 gen_code(name, sym->st_value, sym->st_size, outfile,
1167 text, relocs, nb_relocs, 0);
1178 printf("dyngen (c) 2003 Fabrice Bellard\n"
1179 "usage: dyngen [-o outfile] [-c] objfile\n"
1180 "Generate a dynamic code generator from an object file\n"
1181 "-c output enum of operations\n"
1186 int main(int argc, char **argv)
1188 int c, do_print_enum;
1189 const char *filename, *outfilename;
1192 outfilename = "out.c";
1195 c = getopt(argc, argv, "ho:c");
1203 outfilename = optarg;
1212 filename = argv[optind];
1213 outfile = fopen(outfilename, "w");
1215 error("could not open '%s'", outfilename);
1216 load_elf(filename, outfile, do_print_enum);