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
82 #elif defined(HOST_ARM)
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
90 #error unsupported CPU - please update the code
95 #if ELF_CLASS == ELFCLASS32
96 typedef int32_t host_long;
97 typedef uint32_t host_ulong;
98 #define swabls(x) swab32s(x)
100 typedef int64_t host_long;
101 typedef uint64_t host_ulong;
102 #define swabls(x) swab64s(x)
105 #ifdef ELF_USES_RELOCA
106 #define SHT_RELOC SHT_RELA
108 #define SHT_RELOC SHT_REL
111 #define NO_THUNK_TYPE_SIZE
120 /* all dynamically generated functions begin with this code */
121 #define OP_PREFIX "op_"
123 int elf_must_swap(struct elfhdr *h)
131 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
132 (swaptest.b[0] == 0);
135 void swab16s(uint16_t *p)
140 void swab32s(uint32_t *p)
145 void swab64s(uint64_t *p)
150 void elf_swap_ehdr(struct elfhdr *h)
152 swab16s(&h->e_type); /* Object file type */
153 swab16s(&h-> e_machine); /* Architecture */
154 swab32s(&h-> e_version); /* Object file version */
155 swabls(&h-> e_entry); /* Entry point virtual address */
156 swabls(&h-> e_phoff); /* Program header table file offset */
157 swabls(&h-> e_shoff); /* Section header table file offset */
158 swab32s(&h-> e_flags); /* Processor-specific flags */
159 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
160 swab16s(&h-> e_phentsize); /* Program header table entry size */
161 swab16s(&h-> e_phnum); /* Program header table entry count */
162 swab16s(&h-> e_shentsize); /* Section header table entry size */
163 swab16s(&h-> e_shnum); /* Section header table entry count */
164 swab16s(&h-> e_shstrndx); /* Section header string table index */
167 void elf_swap_shdr(struct elf_shdr *h)
169 swab32s(&h-> sh_name); /* Section name (string tbl index) */
170 swab32s(&h-> sh_type); /* Section type */
171 swabls(&h-> sh_flags); /* Section flags */
172 swabls(&h-> sh_addr); /* Section virtual addr at execution */
173 swabls(&h-> sh_offset); /* Section file offset */
174 swabls(&h-> sh_size); /* Section size in bytes */
175 swab32s(&h-> sh_link); /* Link to another section */
176 swab32s(&h-> sh_info); /* Additional section information */
177 swabls(&h-> sh_addralign); /* Section alignment */
178 swabls(&h-> sh_entsize); /* Entry size if section holds table */
181 void elf_swap_phdr(struct elf_phdr *h)
183 swab32s(&h->p_type); /* Segment type */
184 swabls(&h->p_offset); /* Segment file offset */
185 swabls(&h->p_vaddr); /* Segment virtual address */
186 swabls(&h->p_paddr); /* Segment physical address */
187 swabls(&h->p_filesz); /* Segment size in file */
188 swabls(&h->p_memsz); /* Segment size in memory */
189 swab32s(&h->p_flags); /* Segment flags */
190 swabls(&h->p_align); /* Segment alignment */
193 void elf_swap_rel(ELF_RELOC *rel)
195 swabls(&rel->r_offset);
196 swabls(&rel->r_info);
197 #ifdef ELF_USES_RELOCA
198 swabls(&rel->r_addend);
204 struct elf_shdr *shdr;
212 uint16_t get16(uint16_t *p)
221 uint32_t get32(uint32_t *p)
230 void put16(uint16_t *p, uint16_t val)
237 void put32(uint32_t *p, uint32_t val)
244 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
248 fprintf(stderr, "dyngen: ");
249 vfprintf(stderr, fmt, ap);
250 fprintf(stderr, "\n");
256 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
261 struct elf_shdr *sec;
263 for(i = 0; i < shnum; i++) {
267 shname = shstr + sec->sh_name;
268 if (!strcmp(shname, name))
274 int find_reloc(int sh_index)
276 struct elf_shdr *sec;
279 for(i = 0; i < ehdr.e_shnum; i++) {
281 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
287 void *load_data(int fd, long offset, unsigned int size)
294 lseek(fd, offset, SEEK_SET);
295 if (read(fd, data, size) != size) {
302 int strstart(const char *str, const char *val, const char **ptr)
320 int arm_emit_ldr_info(const char *name, unsigned long start_offset,
321 FILE *outfile, uint8_t *p_start, uint8_t *p_end,
322 ELF_RELOC *relocs, int nb_relocs)
326 int offset, min_offset, pc_offset, data_size;
327 uint8_t data_allocated[1024];
328 unsigned int data_index;
330 memset(data_allocated, 0, sizeof(data_allocated));
333 min_offset = p_end - p_start;
334 while (p < p_start + min_offset) {
335 insn = get32((uint32_t *)p);
336 if ((insn & 0x0d5f0000) == 0x051f0000) {
337 /* ldr reg, [pc, #im] */
338 offset = insn & 0xfff;
339 if (!(insn & 0x00800000))
341 if ((offset & 3) !=0)
342 error("%s:%04x: ldr pc offset must be 32 bit aligned",
343 name, start_offset + p - p_start);
344 pc_offset = p - p_start + offset + 8;
345 if (pc_offset <= (p - p_start) ||
346 pc_offset >= (p_end - p_start))
347 error("%s:%04x: ldr pc offset must point inside the function code",
348 name, start_offset + p - p_start);
349 if (pc_offset < min_offset)
350 min_offset = pc_offset;
353 fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
356 data_index = ((p_end - p_start) - pc_offset - 4) >> 2;
357 fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
359 fprintf(outfile, " arm_ldr_ptr++;\n");
360 if (data_index >= sizeof(data_allocated))
361 error("%s: too many data", name);
362 if (!data_allocated[data_index]) {
365 const char *sym_name, *p;
368 data_allocated[data_index] = 1;
371 addend = get32((uint32_t *)(p_start + pc_offset));
373 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
374 if (rel->r_offset == (pc_offset + start_offset)) {
375 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
376 /* the compiler leave some unnecessary references to the code */
377 if (strstart(sym_name, "__op_param", &p)) {
378 snprintf(relname, sizeof(relname), "param%s", p);
380 snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name);
382 type = ELF32_R_TYPE(rel->r_info);
383 if (type != R_ARM_ABS32)
384 error("%s: unsupported data relocation", name);
388 fprintf(outfile, " arm_data_ptr[%d] = 0x%x",
390 if (relname[0] != '\0')
391 fprintf(outfile, " + %s", relname);
392 fprintf(outfile, ";\n");
398 data_size = (p_end - p_start) - min_offset;
399 if (data_size > 0 && outfile) {
400 fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2);
403 /* the last instruction must be a mov pc, lr */
407 insn = get32((uint32_t *)p);
408 if ((insn & 0xffff0000) != 0xe91b0000) {
411 printf("%s: invalid epilog\n", name);
420 /* generate op code */
421 void gen_code(const char *name, host_ulong offset, host_ulong size,
422 FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs,
426 uint8_t *p_start, *p_end;
427 host_ulong start_offset;
429 uint8_t args_present[MAX_ARGS];
430 const char *sym_name, *p;
433 /* Compute exact size excluding prologue and epilogue instructions.
434 * Increment start_offset to skip epilogue instructions, then compute
435 * copy_size the indicate the size of the remaining instructions (in
438 p_start = text + offset;
439 p_end = p_start + size;
440 start_offset = offset;
445 len = p_end - p_start;
447 error("empty code for %s", name);
448 if (p_end[-1] == 0xc3) {
451 error("ret or jmp expected at the end of %s", name);
459 p = (void *)(p_end - 4);
461 error("empty code for %s", name);
462 if (get32((uint32_t *)p) != 0x4e800020)
463 error("blr expected at the end of %s", name);
464 copy_size = p - p_start;
470 p = (void *)(p_end - 2);
472 error("empty code for %s", name);
473 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
474 error("br %%r14 expected at the end of %s", name);
475 copy_size = p - p_start;
483 error("empty code for %s", name);
484 if (get32((uint32_t *)p) != 0x6bfa8001)
485 error("ret expected at the end of %s", name);
486 copy_size = p - p_start;
492 p = (void *)(p_end - 4);
494 error("empty code for %s", name);
495 /* br.ret.sptk.many b0;; */
497 if (get32((uint32_t *)p) != 0x00840008)
498 error("br.ret.sptk.many b0;; expected at the end of %s", name);
499 copy_size = p - p_start;
505 uint32_t start_insn, end_insn1, end_insn2;
507 p = (void *)(p_end - 8);
509 error("empty code for %s", name);
510 start_insn = get32((uint32_t *)(p_start + 0x0));
511 end_insn1 = get32((uint32_t *)(p + 0x0));
512 end_insn2 = get32((uint32_t *)(p + 0x4));
513 if ((start_insn & ~0x1fff) == 0x9de3a000) {
516 if ((int)(start_insn | ~0x1fff) < -128)
517 error("Found bogus save at the start of %s", name);
518 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
519 error("ret; restore; not found at end of %s", name);
521 error("No save at the beginning of %s", name);
524 /* Skip a preceeding nop, if present. */
526 skip_insn = get32((uint32_t *)(p - 0x4));
527 if (skip_insn == 0x01000000)
531 copy_size = p - p_start;
536 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
538 p = (void *)(p_end - 8);
540 error("empty code for %s", name);
541 start_insn = get32((uint32_t *)(p_start + 0x0));
542 end_insn1 = get32((uint32_t *)(p + 0x0));
543 end_insn2 = get32((uint32_t *)(p + 0x4));
544 if ((start_insn & ~0x1fff) == 0x9de3a000) {
547 if ((int)(start_insn | ~0x1fff) < -256)
548 error("Found bogus save at the start of %s", name);
549 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
550 error("ret; restore; not found at end of %s", name);
552 error("No save at the beginning of %s", name);
555 /* Skip a preceeding nop, if present. */
557 skip_insn = get32((uint32_t *)(p - 0x4));
558 if (skip_insn == 0x01000000)
562 copy_size = p - p_start;
567 if ((p_end - p_start) <= 16)
568 error("%s: function too small", name);
569 if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
570 (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
571 get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
572 error("%s: invalid prolog", name);
575 copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end,
580 error("unknown ELF architecture");
583 /* compute the number of arguments by looking at the relocations */
584 for(i = 0;i < MAX_ARGS; i++)
587 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
588 if (rel->r_offset >= start_offset &&
589 rel->r_offset < start_offset + (p_end - p_start)) {
590 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
591 if (strstart(sym_name, "__op_param", &p)) {
592 n = strtoul(p, NULL, 10);
594 error("too many arguments in %s", name);
595 args_present[n - 1] = 1;
601 while (nb_args < MAX_ARGS && args_present[nb_args])
603 for(i = nb_args; i < MAX_ARGS; i++) {
605 error("inconsistent argument numbering in %s", name);
608 if (gen_switch == 2) {
609 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
610 } else if (gen_switch == 1) {
613 fprintf(outfile, "case INDEX_%s: {\n", name);
615 fprintf(outfile, " long ");
616 for(i = 0; i < nb_args; i++) {
618 fprintf(outfile, ", ");
619 fprintf(outfile, "param%d", i + 1);
621 fprintf(outfile, ";\n");
623 fprintf(outfile, " extern void %s();\n", name);
625 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
626 if (rel->r_offset >= start_offset &&
627 rel->r_offset < start_offset + (p_end - p_start)) {
628 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
630 !strstart(sym_name, "__op_param", NULL) &&
631 !strstart(sym_name, "__op_jmp", NULL)) {
632 #if defined(HOST_SPARC)
633 if (sym_name[0] == '.') {
635 "extern char __dot_%s __asm__(\"%s\");\n",
636 sym_name+1, sym_name);
640 fprintf(outfile, "extern char %s;\n", sym_name);
645 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
647 /* emit code offset information */
650 const char *sym_name, *p;
654 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
655 sym_name = strtab + sym->st_name;
656 if (strstart(sym_name, "__op_label", &p)) {
658 unsigned long offset;
660 /* test if the variable refers to a label inside
661 the code we are generating */
662 ptr = sdata[sym->st_shndx];
664 error("__op_labelN in invalid section");
665 offset = sym->st_value;
666 val = *(target_ulong *)(ptr + offset);
667 #ifdef ELF_USES_RELOCA
669 int reloc_shndx, nb_relocs1, j;
671 /* try to find a matching relocation */
672 reloc_shndx = find_reloc(sym->st_shndx);
674 nb_relocs1 = shdr[reloc_shndx].sh_size /
675 shdr[reloc_shndx].sh_entsize;
676 rel = (ELF_RELOC *)sdata[reloc_shndx];
677 for(j = 0; j < nb_relocs1; j++) {
678 if (rel->r_offset == offset) {
688 if (val >= start_offset && val < start_offset + copy_size) {
689 n = strtol(p, NULL, 10);
690 fprintf(outfile, " label_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
696 /* load parameres in variables */
697 for(i = 0; i < nb_args; i++) {
698 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
701 /* patch relocations */
702 #if defined(HOST_I386)
707 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
708 if (rel->r_offset >= start_offset &&
709 rel->r_offset < start_offset + copy_size) {
710 sym_name = strtab + symtab[ELFW(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 = ELF32_R_TYPE(rel->r_info);
717 addend = get32((uint32_t *)(text + rel->r_offset));
720 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
721 rel->r_offset - start_offset, name, addend);
724 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
725 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
728 error("unsupported i386 relocation (%d)", type);
733 #elif defined(HOST_PPC)
738 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
739 if (rel->r_offset >= start_offset &&
740 rel->r_offset < start_offset + copy_size) {
741 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
742 if (strstart(sym_name, "__op_jmp", &p)) {
744 n = strtol(p, NULL, 10);
745 /* __op_jmp relocations are done at
746 runtime to do translated block
747 chaining: the offset of the instruction
748 needs to be stored */
749 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
750 n, rel->r_offset - start_offset);
754 if (strstart(sym_name, "__op_param", &p)) {
755 snprintf(name, sizeof(name), "param%s", p);
757 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
759 type = ELF32_R_TYPE(rel->r_info);
760 addend = rel->r_addend;
763 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
764 rel->r_offset - start_offset, name, addend);
766 case R_PPC_ADDR16_LO:
767 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
768 rel->r_offset - start_offset, name, addend);
770 case R_PPC_ADDR16_HI:
771 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
772 rel->r_offset - start_offset, name, addend);
774 case R_PPC_ADDR16_HA:
775 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
776 rel->r_offset - start_offset, name, addend);
779 /* warning: must be at 32 MB distancy */
780 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
781 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
784 error("unsupported powerpc relocation (%d)", type);
789 #elif defined(HOST_S390)
794 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
795 if (rel->r_offset >= start_offset &&
796 rel->r_offset < start_offset + copy_size) {
797 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
798 if (strstart(sym_name, "__op_param", &p)) {
799 snprintf(name, sizeof(name), "param%s", p);
801 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
803 type = ELF32_R_TYPE(rel->r_info);
804 addend = rel->r_addend;
807 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
808 rel->r_offset - start_offset, name, addend);
811 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
812 rel->r_offset - start_offset, name, addend);
815 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
816 rel->r_offset - start_offset, name, addend);
819 error("unsupported s390 relocation (%d)", type);
824 #elif defined(HOST_ALPHA)
826 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
827 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
830 type = ELF64_R_TYPE(rel->r_info);
831 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
834 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
835 as an immediate instead of constructing it from the pv or ra. */
836 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
837 rel->r_offset - start_offset);
838 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
839 rel->r_offset - start_offset + rel->r_addend);
842 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
843 now, since some called functions (libc) need pv to be set up. */
846 /* Branch target prediction hint. Ignore for now. Should be already
847 correct for in-function jumps. */
849 case R_ALPHA_LITERAL:
850 /* Load a literal from the GOT relative to the gp. Since there's only a
851 single gp, nothing is to be done. */
853 case R_ALPHA_GPRELHIGH:
854 /* Handle fake relocations against __op_param symbol. Need to emit the
855 high part of the immediate value instead. Other symbols need no
856 special treatment. */
857 if (strstart(sym_name, "__op_param", &p))
858 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
859 rel->r_offset - start_offset, p);
861 case R_ALPHA_GPRELLOW:
862 if (strstart(sym_name, "__op_param", &p))
863 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
864 rel->r_offset - start_offset, p);
867 /* PC-relative jump. Tweak offset to skip the two instructions that try to
868 set up the gp from the pv. */
869 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
870 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
873 error("unsupported Alpha relocation (%d)", type);
878 #elif defined(HOST_IA64)
883 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
884 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
885 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
886 if (strstart(sym_name, "__op_param", &p)) {
887 snprintf(name, sizeof(name), "param%s", p);
889 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
891 type = ELF64_R_TYPE(rel->r_info);
892 addend = rel->r_addend;
895 error("must implemnt R_IA64_LTOFF22 relocation");
896 case R_IA64_PCREL21B:
897 error("must implemnt R_IA64_PCREL21B relocation");
899 error("unsupported ia64 relocation (%d)", type);
904 #elif defined(HOST_SPARC)
909 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
910 if (rel->r_offset >= start_offset &&
911 rel->r_offset < start_offset + copy_size) {
912 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
913 if (strstart(sym_name, "__op_param", &p)) {
914 snprintf(name, sizeof(name), "param%s", p);
916 if (sym_name[0] == '.')
917 snprintf(name, sizeof(name),
921 snprintf(name, sizeof(name),
922 "(long)(&%s)", sym_name);
924 type = ELF32_R_TYPE(rel->r_info);
925 addend = rel->r_addend;
928 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
929 rel->r_offset - start_offset, name, addend);
933 " *(uint32_t *)(gen_code_ptr + %d) = "
934 "((*(uint32_t *)(gen_code_ptr + %d)) "
936 " | (((%s + %d) >> 10) & 0x3fffff);\n",
937 rel->r_offset - start_offset,
938 rel->r_offset - start_offset,
943 " *(uint32_t *)(gen_code_ptr + %d) = "
944 "((*(uint32_t *)(gen_code_ptr + %d)) "
946 " | ((%s + %d) & 0x3ff);\n",
947 rel->r_offset - start_offset,
948 rel->r_offset - start_offset,
951 case R_SPARC_WDISP30:
953 " *(uint32_t *)(gen_code_ptr + %d) = "
954 "((*(uint32_t *)(gen_code_ptr + %d)) "
956 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
958 rel->r_offset - start_offset,
959 rel->r_offset - start_offset,
961 rel->r_offset - start_offset);
964 error("unsupported sparc relocation (%d)", type);
969 #elif defined(HOST_SPARC64)
974 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
975 if (rel->r_offset >= start_offset &&
976 rel->r_offset < start_offset + copy_size) {
977 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
978 if (strstart(sym_name, "__op_param", &p)) {
979 snprintf(name, sizeof(name), "param%s", p);
981 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
983 type = ELF64_R_TYPE(rel->r_info);
984 addend = rel->r_addend;
987 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
988 rel->r_offset - start_offset, name, addend);
992 " *(uint32_t *)(gen_code_ptr + %d) = "
993 "((*(uint32_t *)(gen_code_ptr + %d)) "
995 " | (((%s + %d) >> 10) & 0x3fffff);\n",
996 rel->r_offset - start_offset,
997 rel->r_offset - start_offset,
1002 " *(uint32_t *)(gen_code_ptr + %d) = "
1003 "((*(uint32_t *)(gen_code_ptr + %d)) "
1005 " | ((%s + %d) & 0x3ff);\n",
1006 rel->r_offset - start_offset,
1007 rel->r_offset - start_offset,
1010 case R_SPARC_WDISP30:
1012 " *(uint32_t *)(gen_code_ptr + %d) = "
1013 "((*(uint32_t *)(gen_code_ptr + %d)) "
1015 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1016 " & 0x3fffffff);\n",
1017 rel->r_offset - start_offset,
1018 rel->r_offset - start_offset,
1020 rel->r_offset - start_offset);
1023 error("unsupported sparc64 relocation (%d)", type);
1028 #elif defined(HOST_ARM)
1034 arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
1037 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1038 if (rel->r_offset >= start_offset &&
1039 rel->r_offset < start_offset + copy_size) {
1040 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1041 /* the compiler leave some unnecessary references to the code */
1042 if (sym_name[0] == '\0')
1044 if (strstart(sym_name, "__op_param", &p)) {
1045 snprintf(name, sizeof(name), "param%s", p);
1047 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1049 type = ELF32_R_TYPE(rel->r_info);
1050 addend = get32((uint32_t *)(text + rel->r_offset));
1053 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1054 rel->r_offset - start_offset, name, addend);
1057 fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
1058 rel->r_offset - start_offset, addend, name);
1061 error("unsupported arm relocation (%d)", type);
1067 #error unsupported CPU
1069 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
1070 fprintf(outfile, "}\n");
1071 fprintf(outfile, "break;\n\n");
1073 fprintf(outfile, "static inline void gen_%s(", name);
1075 fprintf(outfile, "void");
1077 for(i = 0; i < nb_args; i++) {
1079 fprintf(outfile, ", ");
1080 fprintf(outfile, "long param%d", i + 1);
1083 fprintf(outfile, ")\n");
1084 fprintf(outfile, "{\n");
1085 for(i = 0; i < nb_args; i++) {
1086 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
1088 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
1089 fprintf(outfile, "}\n\n");
1093 /* load an elf object file */
1094 int load_elf(const char *filename, FILE *outfile, int out_type)
1097 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
1106 fd = open(filename, O_RDONLY);
1108 error("can't open file '%s'", filename);
1110 /* Read ELF header. */
1111 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
1112 error("unable to read file header");
1114 /* Check ELF identification. */
1115 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
1116 || ehdr.e_ident[EI_MAG1] != ELFMAG1
1117 || ehdr.e_ident[EI_MAG2] != ELFMAG2
1118 || ehdr.e_ident[EI_MAG3] != ELFMAG3
1119 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
1120 error("bad ELF header");
1123 do_swap = elf_must_swap(&ehdr);
1125 elf_swap_ehdr(&ehdr);
1126 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
1127 error("Unsupported ELF class");
1128 if (ehdr.e_type != ET_REL)
1129 error("ELF object file expected");
1130 if (ehdr.e_version != EV_CURRENT)
1131 error("Invalid ELF version");
1132 if (!elf_check_arch(ehdr.e_machine))
1133 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
1135 /* read section headers */
1136 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
1138 for(i = 0; i < ehdr.e_shnum; i++) {
1139 elf_swap_shdr(&shdr[i]);
1143 /* read all section data */
1144 sdata = malloc(sizeof(void *) * ehdr.e_shnum);
1145 memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
1147 for(i = 0;i < ehdr.e_shnum; i++) {
1149 if (sec->sh_type != SHT_NOBITS)
1150 sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
1153 sec = &shdr[ehdr.e_shstrndx];
1154 shstr = sdata[ehdr.e_shstrndx];
1156 /* swap relocations */
1157 for(i = 0; i < ehdr.e_shnum; i++) {
1159 if (sec->sh_type == SHT_RELOC) {
1160 nb_relocs = sec->sh_size / sec->sh_entsize;
1162 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
1169 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
1171 error("could not find .text section");
1172 text_shndx = text_sec - shdr;
1173 text = sdata[text_shndx];
1175 /* find text relocations, if any */
1178 i = find_reloc(text_shndx);
1180 relocs = (ELF_RELOC *)sdata[i];
1181 nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
1184 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
1186 error("could not find .symtab section");
1187 strtab_sec = &shdr[symtab_sec->sh_link];
1189 symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
1190 strtab = sdata[symtab_sec->sh_link];
1192 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
1194 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1195 swab32s(&sym->st_name);
1196 swabls(&sym->st_value);
1197 swabls(&sym->st_size);
1198 swab16s(&sym->st_shndx);
1202 if (out_type == OUT_INDEX_OP) {
1203 fprintf(outfile, "DEF(end, 0, 0)\n");
1204 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1205 const char *name, *p;
1206 name = strtab + sym->st_name;
1207 if (strstart(name, OP_PREFIX, &p)) {
1208 gen_code(name, sym->st_value, sym->st_size, outfile,
1209 text, relocs, nb_relocs, 2);
1212 } else if (out_type == OUT_GEN_OP) {
1213 /* generate gen_xxx functions */
1215 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1217 name = strtab + sym->st_name;
1218 if (strstart(name, OP_PREFIX, NULL)) {
1219 if (sym->st_shndx != (text_sec - shdr))
1220 error("invalid section for opcode (0x%x)", sym->st_shndx);
1221 gen_code(name, sym->st_value, sym->st_size, outfile,
1222 text, relocs, nb_relocs, 0);
1227 /* generate big code generation switch */
1229 "int dyngen_code(uint8_t *gen_code_buf,\n"
1230 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1231 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1233 " uint8_t *gen_code_ptr;\n"
1234 " const uint16_t *opc_ptr;\n"
1235 " const uint32_t *opparam_ptr;\n");
1239 " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1240 " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1241 " uint32_t *arm_data_ptr = arm_data_table;\n");
1246 " gen_code_ptr = gen_code_buf;\n"
1247 " opc_ptr = opc_buf;\n"
1248 " opparam_ptr = opparam_buf;\n");
1250 /* Generate prologue, if needed. */
1254 " switch(*opc_ptr++) {\n"
1257 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1259 name = strtab + sym->st_name;
1260 if (strstart(name, OP_PREFIX, NULL)) {
1262 printf("%4d: %s pos=0x%08x len=%d\n",
1263 i, name, sym->st_value, sym->st_size);
1265 if (sym->st_shndx != (text_sec - shdr))
1266 error("invalid section for opcode (0x%x)", sym->st_shndx);
1267 gen_code(name, sym->st_value, sym->st_size, outfile,
1268 text, relocs, nb_relocs, 1);
1278 /* generate constant table if needed */
1280 " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1281 " gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1282 " last_gen_code_ptr = gen_code_ptr;\n"
1283 " arm_ldr_ptr = arm_ldr_table;\n"
1284 " arm_data_ptr = arm_data_table;\n"
1294 /* generate epilogue */
1297 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
1300 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
1303 fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
1306 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n");
1309 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n");
1312 case EM_SPARC32PLUS:
1313 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c62008; /* jmpl %%i0 + 8, %%g0 */\n");
1314 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x01000000; /* nop */\n");
1317 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c7e008; /* ret */\n");
1318 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81e80000; /* restore */\n");
1321 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 error("unknown ELF architecture");
1326 /* flush instruction cache */
1327 fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
1329 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
1330 fprintf(outfile, "}\n\n");
1340 printf("dyngen (c) 2003 Fabrice Bellard\n"
1341 "usage: dyngen [-o outfile] [-c] objfile\n"
1342 "Generate a dynamic code generator from an object file\n"
1343 "-c output enum of operations\n"
1344 "-g output gen_op_xx() functions\n"
1349 int main(int argc, char **argv)
1352 const char *filename, *outfilename;
1355 outfilename = "out.c";
1356 out_type = OUT_CODE;
1358 c = getopt(argc, argv, "ho:cg");
1366 outfilename = optarg;
1369 out_type = OUT_INDEX_OP;
1372 out_type = OUT_GEN_OP;
1378 filename = argv[optind];
1379 outfile = fopen(outfilename, "w");
1381 error("could not open '%s'", outfilename);
1382 load_elf(filename, outfile, out_type);