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.
28 #include "config-host.h"
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_AMD64)
42 #define ELF_CLASS ELFCLASS64
43 #define ELF_ARCH EM_X86_64
44 #define elf_check_arch(x) ((x) == EM_X86_64)
45 #define ELF_USES_RELOCA
47 #elif defined(HOST_PPC)
49 #define ELF_CLASS ELFCLASS32
50 #define ELF_ARCH EM_PPC
51 #define elf_check_arch(x) ((x) == EM_PPC)
52 #define ELF_USES_RELOCA
54 #elif defined(HOST_S390)
56 #define ELF_CLASS ELFCLASS32
57 #define ELF_ARCH EM_S390
58 #define elf_check_arch(x) ((x) == EM_S390)
59 #define ELF_USES_RELOCA
61 #elif defined(HOST_ALPHA)
63 #define ELF_CLASS ELFCLASS64
64 #define ELF_ARCH EM_ALPHA
65 #define elf_check_arch(x) ((x) == EM_ALPHA)
66 #define ELF_USES_RELOCA
68 #elif defined(HOST_IA64)
70 #define ELF_CLASS ELFCLASS64
71 #define ELF_ARCH EM_IA_64
72 #define elf_check_arch(x) ((x) == EM_IA_64)
73 #define ELF_USES_RELOCA
75 #elif defined(HOST_SPARC)
77 #define ELF_CLASS ELFCLASS32
78 #define ELF_ARCH EM_SPARC
79 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
80 #define ELF_USES_RELOCA
82 #elif defined(HOST_SPARC64)
84 #define ELF_CLASS ELFCLASS64
85 #define ELF_ARCH EM_SPARCV9
86 #define elf_check_arch(x) ((x) == EM_SPARCV9)
87 #define ELF_USES_RELOCA
89 #elif defined(HOST_ARM)
91 #define ELF_CLASS ELFCLASS32
92 #define ELF_ARCH EM_ARM
93 #define elf_check_arch(x) ((x) == EM_ARM)
94 #define ELF_USES_RELOC
96 #elif defined(HOST_M68K)
98 #define ELF_CLASS ELFCLASS32
99 #define ELF_ARCH EM_68K
100 #define elf_check_arch(x) ((x) == EM_68K)
101 #define ELF_USES_RELOCA
104 #error unsupported CPU - please update the code
109 #if ELF_CLASS == ELFCLASS32
110 typedef int32_t host_long;
111 typedef uint32_t host_ulong;
112 #define swabls(x) swab32s(x)
114 typedef int64_t host_long;
115 typedef uint64_t host_ulong;
116 #define swabls(x) swab64s(x)
119 #ifdef ELF_USES_RELOCA
120 #define SHT_RELOC SHT_RELA
122 #define SHT_RELOC SHT_REL
133 /* all dynamically generated functions begin with this code */
134 #define OP_PREFIX "op_"
136 int elf_must_swap(struct elfhdr *h)
144 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
145 (swaptest.b[0] == 0);
148 void swab16s(uint16_t *p)
153 void swab32s(uint32_t *p)
158 void swab64s(uint64_t *p)
163 void elf_swap_ehdr(struct elfhdr *h)
165 swab16s(&h->e_type); /* Object file type */
166 swab16s(&h-> e_machine); /* Architecture */
167 swab32s(&h-> e_version); /* Object file version */
168 swabls(&h-> e_entry); /* Entry point virtual address */
169 swabls(&h-> e_phoff); /* Program header table file offset */
170 swabls(&h-> e_shoff); /* Section header table file offset */
171 swab32s(&h-> e_flags); /* Processor-specific flags */
172 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
173 swab16s(&h-> e_phentsize); /* Program header table entry size */
174 swab16s(&h-> e_phnum); /* Program header table entry count */
175 swab16s(&h-> e_shentsize); /* Section header table entry size */
176 swab16s(&h-> e_shnum); /* Section header table entry count */
177 swab16s(&h-> e_shstrndx); /* Section header string table index */
180 void elf_swap_shdr(struct elf_shdr *h)
182 swab32s(&h-> sh_name); /* Section name (string tbl index) */
183 swab32s(&h-> sh_type); /* Section type */
184 swabls(&h-> sh_flags); /* Section flags */
185 swabls(&h-> sh_addr); /* Section virtual addr at execution */
186 swabls(&h-> sh_offset); /* Section file offset */
187 swabls(&h-> sh_size); /* Section size in bytes */
188 swab32s(&h-> sh_link); /* Link to another section */
189 swab32s(&h-> sh_info); /* Additional section information */
190 swabls(&h-> sh_addralign); /* Section alignment */
191 swabls(&h-> sh_entsize); /* Entry size if section holds table */
194 void elf_swap_phdr(struct elf_phdr *h)
196 swab32s(&h->p_type); /* Segment type */
197 swabls(&h->p_offset); /* Segment file offset */
198 swabls(&h->p_vaddr); /* Segment virtual address */
199 swabls(&h->p_paddr); /* Segment physical address */
200 swabls(&h->p_filesz); /* Segment size in file */
201 swabls(&h->p_memsz); /* Segment size in memory */
202 swab32s(&h->p_flags); /* Segment flags */
203 swabls(&h->p_align); /* Segment alignment */
206 void elf_swap_rel(ELF_RELOC *rel)
208 swabls(&rel->r_offset);
209 swabls(&rel->r_info);
210 #ifdef ELF_USES_RELOCA
211 swabls(&rel->r_addend);
217 struct elf_shdr *shdr;
225 uint16_t get16(uint16_t *p)
234 uint32_t get32(uint32_t *p)
243 void put16(uint16_t *p, uint16_t val)
250 void put32(uint32_t *p, uint32_t val)
257 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
261 fprintf(stderr, "dyngen: ");
262 vfprintf(stderr, fmt, ap);
263 fprintf(stderr, "\n");
269 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
274 struct elf_shdr *sec;
276 for(i = 0; i < shnum; i++) {
280 shname = shstr + sec->sh_name;
281 if (!strcmp(shname, name))
287 int find_reloc(int sh_index)
289 struct elf_shdr *sec;
292 for(i = 0; i < ehdr.e_shnum; i++) {
294 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
300 void *load_data(int fd, long offset, unsigned int size)
307 lseek(fd, offset, SEEK_SET);
308 if (read(fd, data, size) != size) {
315 int strstart(const char *str, const char *val, const char **ptr)
333 int arm_emit_ldr_info(const char *name, unsigned long start_offset,
334 FILE *outfile, uint8_t *p_start, uint8_t *p_end,
335 ELF_RELOC *relocs, int nb_relocs)
339 int offset, min_offset, pc_offset, data_size;
340 uint8_t data_allocated[1024];
341 unsigned int data_index;
343 memset(data_allocated, 0, sizeof(data_allocated));
346 min_offset = p_end - p_start;
347 while (p < p_start + min_offset) {
348 insn = get32((uint32_t *)p);
349 if ((insn & 0x0d5f0000) == 0x051f0000) {
350 /* ldr reg, [pc, #im] */
351 offset = insn & 0xfff;
352 if (!(insn & 0x00800000))
354 if ((offset & 3) !=0)
355 error("%s:%04x: ldr pc offset must be 32 bit aligned",
356 name, start_offset + p - p_start);
357 pc_offset = p - p_start + offset + 8;
358 if (pc_offset <= (p - p_start) ||
359 pc_offset >= (p_end - p_start))
360 error("%s:%04x: ldr pc offset must point inside the function code",
361 name, start_offset + p - p_start);
362 if (pc_offset < min_offset)
363 min_offset = pc_offset;
366 fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
369 data_index = ((p_end - p_start) - pc_offset - 4) >> 2;
370 fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
372 fprintf(outfile, " arm_ldr_ptr++;\n");
373 if (data_index >= sizeof(data_allocated))
374 error("%s: too many data", name);
375 if (!data_allocated[data_index]) {
378 const char *sym_name, *p;
381 data_allocated[data_index] = 1;
384 addend = get32((uint32_t *)(p_start + pc_offset));
386 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
387 if (rel->r_offset == (pc_offset + start_offset)) {
388 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
389 /* the compiler leave some unnecessary references to the code */
390 if (strstart(sym_name, "__op_param", &p)) {
391 snprintf(relname, sizeof(relname), "param%s", p);
393 snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name);
395 type = ELF32_R_TYPE(rel->r_info);
396 if (type != R_ARM_ABS32)
397 error("%s: unsupported data relocation", name);
401 fprintf(outfile, " arm_data_ptr[%d] = 0x%x",
403 if (relname[0] != '\0')
404 fprintf(outfile, " + %s", relname);
405 fprintf(outfile, ";\n");
411 data_size = (p_end - p_start) - min_offset;
412 if (data_size > 0 && outfile) {
413 fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2);
416 /* the last instruction must be a mov pc, lr */
420 insn = get32((uint32_t *)p);
421 if ((insn & 0xffff0000) != 0xe91b0000) {
424 printf("%s: invalid epilog\n", name);
433 /* generate op code */
434 void gen_code(const char *name, host_ulong offset, host_ulong size,
435 FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs,
439 uint8_t *p_start, *p_end;
440 host_ulong start_offset;
442 uint8_t args_present[MAX_ARGS];
443 const char *sym_name, *p;
446 /* Compute exact size excluding prologue and epilogue instructions.
447 * Increment start_offset to skip epilogue instructions, then compute
448 * copy_size the indicate the size of the remaining instructions (in
451 p_start = text + offset;
452 p_end = p_start + size;
453 start_offset = offset;
459 len = p_end - p_start;
461 error("empty code for %s", name);
462 if (p_end[-1] == 0xc3) {
465 error("ret or jmp expected at the end of %s", name);
473 p = (void *)(p_end - 4);
475 error("empty code for %s", name);
476 if (get32((uint32_t *)p) != 0x4e800020)
477 error("blr expected at the end of %s", name);
478 copy_size = p - p_start;
484 p = (void *)(p_end - 2);
486 error("empty code for %s", name);
487 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
488 error("br %%r14 expected at the end of %s", name);
489 copy_size = p - p_start;
497 /* XXX: check why it occurs */
499 error("empty code for %s", name);
501 if (get32((uint32_t *)p) != 0x6bfa8001)
502 error("ret expected at the end of %s", name);
503 copy_size = p - p_start;
509 p = (void *)(p_end - 4);
511 error("empty code for %s", name);
512 /* br.ret.sptk.many b0;; */
514 if (get32((uint32_t *)p) != 0x00840008)
515 error("br.ret.sptk.many b0;; expected at the end of %s", name);
516 copy_size = p - p_start;
522 uint32_t start_insn, end_insn1, end_insn2;
524 p = (void *)(p_end - 8);
526 error("empty code for %s", name);
527 start_insn = get32((uint32_t *)(p_start + 0x0));
528 end_insn1 = get32((uint32_t *)(p + 0x0));
529 end_insn2 = get32((uint32_t *)(p + 0x4));
530 if ((start_insn & ~0x1fff) == 0x9de3a000) {
533 if ((int)(start_insn | ~0x1fff) < -128)
534 error("Found bogus save at the start of %s", name);
535 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
536 error("ret; restore; not found at end of %s", name);
538 error("No save at the beginning of %s", name);
541 /* Skip a preceeding nop, if present. */
543 skip_insn = get32((uint32_t *)(p - 0x4));
544 if (skip_insn == 0x01000000)
548 copy_size = p - p_start;
553 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
555 p = (void *)(p_end - 8);
557 error("empty code for %s", name);
558 start_insn = get32((uint32_t *)(p_start + 0x0));
559 end_insn1 = get32((uint32_t *)(p + 0x0));
560 end_insn2 = get32((uint32_t *)(p + 0x4));
561 if ((start_insn & ~0x1fff) == 0x9de3a000) {
564 if ((int)(start_insn | ~0x1fff) < -256)
565 error("Found bogus save at the start of %s", name);
566 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
567 error("ret; restore; not found at end of %s", name);
569 error("No save at the beginning of %s", name);
572 /* Skip a preceeding nop, if present. */
574 skip_insn = get32((uint32_t *)(p - 0x4));
575 if (skip_insn == 0x01000000)
579 copy_size = p - p_start;
584 if ((p_end - p_start) <= 16)
585 error("%s: function too small", name);
586 if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
587 (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
588 get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
589 error("%s: invalid prolog", name);
592 copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end,
599 p = (void *)(p_end - 2);
601 error("empty code for %s", name);
602 // remove NOP's, probably added for alignment
603 while ((get16((uint16_t *)p) == 0x4e71) &&
606 if (get16((uint16_t *)p) != 0x4e75)
607 error("rts expected at the end of %s", name);
608 copy_size = p - p_start;
612 error("unknown ELF architecture");
615 /* compute the number of arguments by looking at the relocations */
616 for(i = 0;i < MAX_ARGS; i++)
619 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
620 if (rel->r_offset >= start_offset &&
621 rel->r_offset < start_offset + (p_end - p_start)) {
622 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
623 if (strstart(sym_name, "__op_param", &p)) {
624 n = strtoul(p, NULL, 10);
626 error("too many arguments in %s", name);
627 args_present[n - 1] = 1;
633 while (nb_args < MAX_ARGS && args_present[nb_args])
635 for(i = nb_args; i < MAX_ARGS; i++) {
637 error("inconsistent argument numbering in %s", name);
640 if (gen_switch == 2) {
641 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
642 } else if (gen_switch == 1) {
645 fprintf(outfile, "case INDEX_%s: {\n", name);
647 fprintf(outfile, " long ");
648 for(i = 0; i < nb_args; i++) {
650 fprintf(outfile, ", ");
651 fprintf(outfile, "param%d", i + 1);
653 fprintf(outfile, ";\n");
655 fprintf(outfile, " extern void %s();\n", name);
657 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
658 if (rel->r_offset >= start_offset &&
659 rel->r_offset < start_offset + (p_end - p_start)) {
660 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
662 !strstart(sym_name, "__op_param", NULL) &&
663 !strstart(sym_name, "__op_jmp", NULL)) {
664 #if defined(HOST_SPARC)
665 if (sym_name[0] == '.') {
667 "extern char __dot_%s __asm__(\"%s\");\n",
668 sym_name+1, sym_name);
672 fprintf(outfile, "extern char %s;\n", sym_name);
677 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
679 /* emit code offset information */
682 const char *sym_name, *p;
686 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
687 sym_name = strtab + sym->st_name;
688 if (strstart(sym_name, "__op_label", &p)) {
690 unsigned long offset;
692 /* test if the variable refers to a label inside
693 the code we are generating */
694 ptr = sdata[sym->st_shndx];
696 error("__op_labelN in invalid section");
697 offset = sym->st_value;
698 val = *(unsigned long *)(ptr + offset);
699 #ifdef ELF_USES_RELOCA
701 int reloc_shndx, nb_relocs1, j;
703 /* try to find a matching relocation */
704 reloc_shndx = find_reloc(sym->st_shndx);
706 nb_relocs1 = shdr[reloc_shndx].sh_size /
707 shdr[reloc_shndx].sh_entsize;
708 rel = (ELF_RELOC *)sdata[reloc_shndx];
709 for(j = 0; j < nb_relocs1; j++) {
710 if (rel->r_offset == offset) {
720 if (val >= start_offset && val < start_offset + copy_size) {
721 n = strtol(p, NULL, 10);
722 fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
728 /* load parameres in variables */
729 for(i = 0; i < nb_args; i++) {
730 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
733 /* patch relocations */
734 #if defined(HOST_I386)
739 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
740 if (rel->r_offset >= start_offset &&
741 rel->r_offset < start_offset + copy_size) {
742 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
743 if (strstart(sym_name, "__op_jmp", &p)) {
745 n = strtol(p, NULL, 10);
746 /* __op_jmp relocations are done at
747 runtime to do translated block
748 chaining: the offset of the instruction
749 needs to be stored */
750 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
751 n, rel->r_offset - start_offset);
755 if (strstart(sym_name, "__op_param", &p)) {
756 snprintf(name, sizeof(name), "param%s", p);
758 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
760 type = ELF32_R_TYPE(rel->r_info);
761 addend = get32((uint32_t *)(text + rel->r_offset));
764 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
765 rel->r_offset - start_offset, name, addend);
768 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
769 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
772 error("unsupported i386 relocation (%d)", type);
777 #elif defined(HOST_AMD64)
782 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
783 if (rel->r_offset >= start_offset &&
784 rel->r_offset < start_offset + copy_size) {
785 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
786 if (strstart(sym_name, "__op_param", &p)) {
787 snprintf(name, sizeof(name), "param%s", p);
789 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
791 type = ELF32_R_TYPE(rel->r_info);
792 addend = rel->r_addend;
795 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
796 rel->r_offset - start_offset, name, addend);
799 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
800 rel->r_offset - start_offset, name, addend);
803 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
804 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
807 error("unsupported AMD64 relocation (%d)", type);
812 #elif defined(HOST_PPC)
817 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
818 if (rel->r_offset >= start_offset &&
819 rel->r_offset < start_offset + copy_size) {
820 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
821 if (strstart(sym_name, "__op_jmp", &p)) {
823 n = strtol(p, NULL, 10);
824 /* __op_jmp relocations are done at
825 runtime to do translated block
826 chaining: the offset of the instruction
827 needs to be stored */
828 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
829 n, rel->r_offset - start_offset);
833 if (strstart(sym_name, "__op_param", &p)) {
834 snprintf(name, sizeof(name), "param%s", p);
836 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
838 type = ELF32_R_TYPE(rel->r_info);
839 addend = rel->r_addend;
842 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
843 rel->r_offset - start_offset, name, addend);
845 case R_PPC_ADDR16_LO:
846 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
847 rel->r_offset - start_offset, name, addend);
849 case R_PPC_ADDR16_HI:
850 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
851 rel->r_offset - start_offset, name, addend);
853 case R_PPC_ADDR16_HA:
854 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
855 rel->r_offset - start_offset, name, addend);
858 /* warning: must be at 32 MB distancy */
859 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
860 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
863 error("unsupported powerpc relocation (%d)", type);
868 #elif defined(HOST_S390)
873 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
874 if (rel->r_offset >= start_offset &&
875 rel->r_offset < start_offset + copy_size) {
876 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
877 if (strstart(sym_name, "__op_param", &p)) {
878 snprintf(name, sizeof(name), "param%s", p);
880 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
882 type = ELF32_R_TYPE(rel->r_info);
883 addend = rel->r_addend;
886 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
887 rel->r_offset - start_offset, name, addend);
890 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
891 rel->r_offset - start_offset, name, addend);
894 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
895 rel->r_offset - start_offset, name, addend);
898 error("unsupported s390 relocation (%d)", type);
903 #elif defined(HOST_ALPHA)
905 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
906 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
909 type = ELF64_R_TYPE(rel->r_info);
910 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
913 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
914 as an immediate instead of constructing it from the pv or ra. */
915 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
916 rel->r_offset - start_offset);
917 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
918 rel->r_offset - start_offset + rel->r_addend);
921 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
922 now, since some called functions (libc) need pv to be set up. */
925 /* Branch target prediction hint. Ignore for now. Should be already
926 correct for in-function jumps. */
928 case R_ALPHA_LITERAL:
929 /* Load a literal from the GOT relative to the gp. Since there's only a
930 single gp, nothing is to be done. */
932 case R_ALPHA_GPRELHIGH:
933 /* Handle fake relocations against __op_param symbol. Need to emit the
934 high part of the immediate value instead. Other symbols need no
935 special treatment. */
936 if (strstart(sym_name, "__op_param", &p))
937 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
938 rel->r_offset - start_offset, p);
940 case R_ALPHA_GPRELLOW:
941 if (strstart(sym_name, "__op_param", &p))
942 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
943 rel->r_offset - start_offset, p);
946 /* PC-relative jump. Tweak offset to skip the two instructions that try to
947 set up the gp from the pv. */
948 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
949 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
952 error("unsupported Alpha relocation (%d)", type);
957 #elif defined(HOST_IA64)
962 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
963 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
964 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
965 if (strstart(sym_name, "__op_param", &p)) {
966 snprintf(name, sizeof(name), "param%s", p);
968 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
970 type = ELF64_R_TYPE(rel->r_info);
971 addend = rel->r_addend;
974 error("must implemnt R_IA64_LTOFF22 relocation");
975 case R_IA64_PCREL21B:
976 error("must implemnt R_IA64_PCREL21B relocation");
978 error("unsupported ia64 relocation (%d)", type);
983 #elif defined(HOST_SPARC)
988 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
989 if (rel->r_offset >= start_offset &&
990 rel->r_offset < start_offset + copy_size) {
991 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
992 if (strstart(sym_name, "__op_param", &p)) {
993 snprintf(name, sizeof(name), "param%s", p);
995 if (sym_name[0] == '.')
996 snprintf(name, sizeof(name),
1000 snprintf(name, sizeof(name),
1001 "(long)(&%s)", sym_name);
1003 type = ELF32_R_TYPE(rel->r_info);
1004 addend = rel->r_addend;
1007 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1008 rel->r_offset - start_offset, name, addend);
1012 " *(uint32_t *)(gen_code_ptr + %d) = "
1013 "((*(uint32_t *)(gen_code_ptr + %d)) "
1015 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1016 rel->r_offset - start_offset,
1017 rel->r_offset - start_offset,
1022 " *(uint32_t *)(gen_code_ptr + %d) = "
1023 "((*(uint32_t *)(gen_code_ptr + %d)) "
1025 " | ((%s + %d) & 0x3ff);\n",
1026 rel->r_offset - start_offset,
1027 rel->r_offset - start_offset,
1030 case R_SPARC_WDISP30:
1032 " *(uint32_t *)(gen_code_ptr + %d) = "
1033 "((*(uint32_t *)(gen_code_ptr + %d)) "
1035 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1036 " & 0x3fffffff);\n",
1037 rel->r_offset - start_offset,
1038 rel->r_offset - start_offset,
1040 rel->r_offset - start_offset);
1043 error("unsupported sparc relocation (%d)", type);
1048 #elif defined(HOST_SPARC64)
1053 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1054 if (rel->r_offset >= start_offset &&
1055 rel->r_offset < start_offset + copy_size) {
1056 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
1057 if (strstart(sym_name, "__op_param", &p)) {
1058 snprintf(name, sizeof(name), "param%s", p);
1060 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1062 type = ELF64_R_TYPE(rel->r_info);
1063 addend = rel->r_addend;
1066 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1067 rel->r_offset - start_offset, name, addend);
1071 " *(uint32_t *)(gen_code_ptr + %d) = "
1072 "((*(uint32_t *)(gen_code_ptr + %d)) "
1074 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1075 rel->r_offset - start_offset,
1076 rel->r_offset - start_offset,
1081 " *(uint32_t *)(gen_code_ptr + %d) = "
1082 "((*(uint32_t *)(gen_code_ptr + %d)) "
1084 " | ((%s + %d) & 0x3ff);\n",
1085 rel->r_offset - start_offset,
1086 rel->r_offset - start_offset,
1089 case R_SPARC_WDISP30:
1091 " *(uint32_t *)(gen_code_ptr + %d) = "
1092 "((*(uint32_t *)(gen_code_ptr + %d)) "
1094 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1095 " & 0x3fffffff);\n",
1096 rel->r_offset - start_offset,
1097 rel->r_offset - start_offset,
1099 rel->r_offset - start_offset);
1102 error("unsupported sparc64 relocation (%d)", type);
1107 #elif defined(HOST_ARM)
1113 arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
1116 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1117 if (rel->r_offset >= start_offset &&
1118 rel->r_offset < start_offset + copy_size) {
1119 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1120 /* the compiler leave some unnecessary references to the code */
1121 if (sym_name[0] == '\0')
1123 if (strstart(sym_name, "__op_param", &p)) {
1124 snprintf(name, sizeof(name), "param%s", p);
1126 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1128 type = ELF32_R_TYPE(rel->r_info);
1129 addend = get32((uint32_t *)(text + rel->r_offset));
1132 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1133 rel->r_offset - start_offset, name, addend);
1136 fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
1137 rel->r_offset - start_offset, addend, name);
1140 error("unsupported arm relocation (%d)", type);
1145 #elif defined(HOST_M68K)
1151 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1152 if (rel->r_offset >= start_offset &&
1153 rel->r_offset < start_offset + copy_size) {
1154 sym = &(symtab[ELFW(R_SYM)(rel->r_info)]);
1155 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1156 if (strstart(sym_name, "__op_param", &p)) {
1157 snprintf(name, sizeof(name), "param%s", p);
1159 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1161 type = ELF32_R_TYPE(rel->r_info);
1162 addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend;
1165 fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ;
1166 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
1167 rel->r_offset - start_offset, name, addend );
1170 fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset);
1171 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
1172 rel->r_offset - start_offset, name, rel->r_offset - start_offset, /*sym->st_value+*/ addend);
1175 error("unsupported m68k relocation (%d)", type);
1181 #error unsupported CPU
1183 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
1184 fprintf(outfile, "}\n");
1185 fprintf(outfile, "break;\n\n");
1187 fprintf(outfile, "static inline void gen_%s(", name);
1189 fprintf(outfile, "void");
1191 for(i = 0; i < nb_args; i++) {
1193 fprintf(outfile, ", ");
1194 fprintf(outfile, "long param%d", i + 1);
1197 fprintf(outfile, ")\n");
1198 fprintf(outfile, "{\n");
1199 for(i = 0; i < nb_args; i++) {
1200 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
1202 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
1203 fprintf(outfile, "}\n\n");
1207 /* load an elf object file */
1208 int load_elf(const char *filename, FILE *outfile, int out_type)
1211 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
1220 fd = open(filename, O_RDONLY);
1222 error("can't open file '%s'", filename);
1224 /* Read ELF header. */
1225 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
1226 error("unable to read file header");
1228 /* Check ELF identification. */
1229 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
1230 || ehdr.e_ident[EI_MAG1] != ELFMAG1
1231 || ehdr.e_ident[EI_MAG2] != ELFMAG2
1232 || ehdr.e_ident[EI_MAG3] != ELFMAG3
1233 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
1234 error("bad ELF header");
1237 do_swap = elf_must_swap(&ehdr);
1239 elf_swap_ehdr(&ehdr);
1240 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
1241 error("Unsupported ELF class");
1242 if (ehdr.e_type != ET_REL)
1243 error("ELF object file expected");
1244 if (ehdr.e_version != EV_CURRENT)
1245 error("Invalid ELF version");
1246 if (!elf_check_arch(ehdr.e_machine))
1247 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
1249 /* read section headers */
1250 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
1252 for(i = 0; i < ehdr.e_shnum; i++) {
1253 elf_swap_shdr(&shdr[i]);
1257 /* read all section data */
1258 sdata = malloc(sizeof(void *) * ehdr.e_shnum);
1259 memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
1261 for(i = 0;i < ehdr.e_shnum; i++) {
1263 if (sec->sh_type != SHT_NOBITS)
1264 sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
1267 sec = &shdr[ehdr.e_shstrndx];
1268 shstr = sdata[ehdr.e_shstrndx];
1270 /* swap relocations */
1271 for(i = 0; i < ehdr.e_shnum; i++) {
1273 if (sec->sh_type == SHT_RELOC) {
1274 nb_relocs = sec->sh_size / sec->sh_entsize;
1276 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
1283 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
1285 error("could not find .text section");
1286 text_shndx = text_sec - shdr;
1287 text = sdata[text_shndx];
1289 /* find text relocations, if any */
1292 i = find_reloc(text_shndx);
1294 relocs = (ELF_RELOC *)sdata[i];
1295 nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
1298 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
1300 error("could not find .symtab section");
1301 strtab_sec = &shdr[symtab_sec->sh_link];
1303 symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
1304 strtab = sdata[symtab_sec->sh_link];
1306 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
1308 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1309 swab32s(&sym->st_name);
1310 swabls(&sym->st_value);
1311 swabls(&sym->st_size);
1312 swab16s(&sym->st_shndx);
1316 if (out_type == OUT_INDEX_OP) {
1317 fprintf(outfile, "DEF(end, 0, 0)\n");
1318 fprintf(outfile, "DEF(nop, 0, 0)\n");
1319 fprintf(outfile, "DEF(nop1, 1, 0)\n");
1320 fprintf(outfile, "DEF(nop2, 2, 0)\n");
1321 fprintf(outfile, "DEF(nop3, 3, 0)\n");
1322 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1323 const char *name, *p;
1324 name = strtab + sym->st_name;
1325 if (strstart(name, OP_PREFIX, &p)) {
1326 gen_code(name, sym->st_value, sym->st_size, outfile,
1327 text, relocs, nb_relocs, 2);
1330 } else if (out_type == OUT_GEN_OP) {
1331 /* generate gen_xxx functions */
1333 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1335 name = strtab + sym->st_name;
1336 if (strstart(name, OP_PREFIX, NULL)) {
1337 if (sym->st_shndx != (text_sec - shdr))
1338 error("invalid section for opcode (0x%x)", sym->st_shndx);
1339 gen_code(name, sym->st_value, sym->st_size, outfile,
1340 text, relocs, nb_relocs, 0);
1345 /* generate big code generation switch */
1347 "int dyngen_code(uint8_t *gen_code_buf,\n"
1348 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1349 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1351 " uint8_t *gen_code_ptr;\n"
1352 " const uint16_t *opc_ptr;\n"
1353 " const uint32_t *opparam_ptr;\n");
1357 " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1358 " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1359 " uint32_t *arm_data_ptr = arm_data_table;\n");
1364 " gen_code_ptr = gen_code_buf;\n"
1365 " opc_ptr = opc_buf;\n"
1366 " opparam_ptr = opparam_buf;\n");
1368 /* Generate prologue, if needed. */
1372 " switch(*opc_ptr++) {\n"
1375 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1377 name = strtab + sym->st_name;
1378 if (strstart(name, OP_PREFIX, NULL)) {
1380 printf("%4d: %s pos=0x%08x len=%d\n",
1381 i, name, sym->st_value, sym->st_size);
1383 if (sym->st_shndx != (text_sec - shdr))
1384 error("invalid section for opcode (0x%x)", sym->st_shndx);
1385 gen_code(name, sym->st_value, sym->st_size, outfile,
1386 text, relocs, nb_relocs, 1);
1391 " case INDEX_op_nop:\n"
1393 " case INDEX_op_nop1:\n"
1396 " case INDEX_op_nop2:\n"
1397 " opparam_ptr += 2;\n"
1399 " case INDEX_op_nop3:\n"
1400 " opparam_ptr += 3;\n"
1407 /* generate constant table if needed */
1409 " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1410 " gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1411 " last_gen_code_ptr = gen_code_ptr;\n"
1412 " arm_ldr_ptr = arm_ldr_table;\n"
1413 " arm_data_ptr = arm_data_table;\n"
1423 /* generate some code patching */
1425 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");
1427 /* flush instruction cache */
1428 fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
1430 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
1431 fprintf(outfile, "}\n\n");
1441 printf("dyngen (c) 2003 Fabrice Bellard\n"
1442 "usage: dyngen [-o outfile] [-c] objfile\n"
1443 "Generate a dynamic code generator from an object file\n"
1444 "-c output enum of operations\n"
1445 "-g output gen_op_xx() functions\n"
1450 int main(int argc, char **argv)
1453 const char *filename, *outfilename;
1456 outfilename = "out.c";
1457 out_type = OUT_CODE;
1459 c = getopt(argc, argv, "ho:cg");
1467 outfilename = optarg;
1470 out_type = OUT_INDEX_OP;
1473 out_type = OUT_GEN_OP;
1479 filename = argv[optind];
1480 outfile = fopen(outfilename, "w");
1482 error("could not open '%s'", outfilename);
1483 load_elf(filename, outfile, out_type);