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
113 /* all dynamically generated functions begin with this code */
114 #define OP_PREFIX "op_"
116 int elf_must_swap(struct elfhdr *h)
124 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
125 (swaptest.b[0] == 0);
128 void swab16s(uint16_t *p)
133 void swab32s(uint32_t *p)
138 void swab64s(uint64_t *p)
143 void elf_swap_ehdr(struct elfhdr *h)
145 swab16s(&h->e_type); /* Object file type */
146 swab16s(&h-> e_machine); /* Architecture */
147 swab32s(&h-> e_version); /* Object file version */
148 swabls(&h-> e_entry); /* Entry point virtual address */
149 swabls(&h-> e_phoff); /* Program header table file offset */
150 swabls(&h-> e_shoff); /* Section header table file offset */
151 swab32s(&h-> e_flags); /* Processor-specific flags */
152 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
153 swab16s(&h-> e_phentsize); /* Program header table entry size */
154 swab16s(&h-> e_phnum); /* Program header table entry count */
155 swab16s(&h-> e_shentsize); /* Section header table entry size */
156 swab16s(&h-> e_shnum); /* Section header table entry count */
157 swab16s(&h-> e_shstrndx); /* Section header string table index */
160 void elf_swap_shdr(struct elf_shdr *h)
162 swab32s(&h-> sh_name); /* Section name (string tbl index) */
163 swab32s(&h-> sh_type); /* Section type */
164 swabls(&h-> sh_flags); /* Section flags */
165 swabls(&h-> sh_addr); /* Section virtual addr at execution */
166 swabls(&h-> sh_offset); /* Section file offset */
167 swabls(&h-> sh_size); /* Section size in bytes */
168 swab32s(&h-> sh_link); /* Link to another section */
169 swab32s(&h-> sh_info); /* Additional section information */
170 swabls(&h-> sh_addralign); /* Section alignment */
171 swabls(&h-> sh_entsize); /* Entry size if section holds table */
174 void elf_swap_phdr(struct elf_phdr *h)
176 swab32s(&h->p_type); /* Segment type */
177 swabls(&h->p_offset); /* Segment file offset */
178 swabls(&h->p_vaddr); /* Segment virtual address */
179 swabls(&h->p_paddr); /* Segment physical address */
180 swabls(&h->p_filesz); /* Segment size in file */
181 swabls(&h->p_memsz); /* Segment size in memory */
182 swab32s(&h->p_flags); /* Segment flags */
183 swabls(&h->p_align); /* Segment alignment */
186 void elf_swap_rel(ELF_RELOC *rel)
188 swabls(&rel->r_offset);
189 swabls(&rel->r_info);
190 #ifdef ELF_USES_RELOCA
191 swabls(&rel->r_addend);
197 struct elf_shdr *shdr;
205 uint16_t get16(uint16_t *p)
214 uint32_t get32(uint32_t *p)
223 void put16(uint16_t *p, uint16_t val)
230 void put32(uint32_t *p, uint32_t val)
237 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
241 fprintf(stderr, "dyngen: ");
242 vfprintf(stderr, fmt, ap);
243 fprintf(stderr, "\n");
249 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
254 struct elf_shdr *sec;
256 for(i = 0; i < shnum; i++) {
260 shname = shstr + sec->sh_name;
261 if (!strcmp(shname, name))
267 int find_reloc(int sh_index)
269 struct elf_shdr *sec;
272 for(i = 0; i < ehdr.e_shnum; i++) {
274 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
280 void *load_data(int fd, long offset, unsigned int size)
287 lseek(fd, offset, SEEK_SET);
288 if (read(fd, data, size) != size) {
295 int strstart(const char *str, const char *val, const char **ptr)
313 int arm_emit_ldr_info(const char *name, unsigned long start_offset,
314 FILE *outfile, uint8_t *p_start, uint8_t *p_end,
315 ELF_RELOC *relocs, int nb_relocs)
319 int offset, min_offset, pc_offset, data_size;
320 uint8_t data_allocated[1024];
321 unsigned int data_index;
323 memset(data_allocated, 0, sizeof(data_allocated));
326 min_offset = p_end - p_start;
327 while (p < p_start + min_offset) {
328 insn = get32((uint32_t *)p);
329 if ((insn & 0x0d5f0000) == 0x051f0000) {
330 /* ldr reg, [pc, #im] */
331 offset = insn & 0xfff;
332 if (!(insn & 0x00800000))
334 if ((offset & 3) !=0)
335 error("%s:%04x: ldr pc offset must be 32 bit aligned",
336 name, start_offset + p - p_start);
337 pc_offset = p - p_start + offset + 8;
338 if (pc_offset <= (p - p_start) ||
339 pc_offset >= (p_end - p_start))
340 error("%s:%04x: ldr pc offset must point inside the function code",
341 name, start_offset + p - p_start);
342 if (pc_offset < min_offset)
343 min_offset = pc_offset;
346 fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
349 data_index = ((p_end - p_start) - pc_offset - 4) >> 2;
350 fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
352 fprintf(outfile, " arm_ldr_ptr++;\n");
353 if (data_index >= sizeof(data_allocated))
354 error("%s: too many data", name);
355 if (!data_allocated[data_index]) {
358 const char *sym_name, *p;
361 data_allocated[data_index] = 1;
364 addend = get32((uint32_t *)(p_start + pc_offset));
366 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
367 if (rel->r_offset == (pc_offset + start_offset)) {
368 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
369 /* the compiler leave some unnecessary references to the code */
370 if (strstart(sym_name, "__op_param", &p)) {
371 snprintf(relname, sizeof(relname), "param%s", p);
373 snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name);
375 type = ELF32_R_TYPE(rel->r_info);
376 if (type != R_ARM_ABS32)
377 error("%s: unsupported data relocation", name);
381 fprintf(outfile, " arm_data_ptr[%d] = 0x%x",
383 if (relname[0] != '\0')
384 fprintf(outfile, " + %s", relname);
385 fprintf(outfile, ";\n");
391 data_size = (p_end - p_start) - min_offset;
392 if (data_size > 0 && outfile) {
393 fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2);
396 /* the last instruction must be a mov pc, lr */
400 insn = get32((uint32_t *)p);
401 if ((insn & 0xffff0000) != 0xe91b0000) {
404 printf("%s: invalid epilog\n", name);
413 /* generate op code */
414 void gen_code(const char *name, host_ulong offset, host_ulong size,
415 FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs,
419 uint8_t *p_start, *p_end;
420 host_ulong start_offset;
422 uint8_t args_present[MAX_ARGS];
423 const char *sym_name, *p;
426 /* Compute exact size excluding prologue and epilogue instructions.
427 * Increment start_offset to skip epilogue instructions, then compute
428 * copy_size the indicate the size of the remaining instructions (in
431 p_start = text + offset;
432 p_end = p_start + size;
433 start_offset = offset;
438 len = p_end - p_start;
440 error("empty code for %s", name);
441 if (p_end[-1] == 0xc3) {
444 error("ret or jmp expected at the end of %s", name);
452 p = (void *)(p_end - 4);
454 error("empty code for %s", name);
455 if (get32((uint32_t *)p) != 0x4e800020)
456 error("blr expected at the end of %s", name);
457 copy_size = p - p_start;
463 p = (void *)(p_end - 2);
465 error("empty code for %s", name);
466 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
467 error("br %%r14 expected at the end of %s", name);
468 copy_size = p - p_start;
476 error("empty code for %s", name);
477 if (get32((uint32_t *)p) != 0x6bfa8001)
478 error("ret expected at the end of %s", name);
479 copy_size = p - p_start;
485 p = (void *)(p_end - 4);
487 error("empty code for %s", name);
488 /* br.ret.sptk.many b0;; */
490 if (get32((uint32_t *)p) != 0x00840008)
491 error("br.ret.sptk.many b0;; expected at the end of %s", name);
492 copy_size = p - p_start;
498 uint32_t start_insn, end_insn1, end_insn2;
500 p = (void *)(p_end - 8);
502 error("empty code for %s", name);
503 start_insn = get32((uint32_t *)(p_start + 0x0));
504 end_insn1 = get32((uint32_t *)(p + 0x0));
505 end_insn2 = get32((uint32_t *)(p + 0x4));
506 if ((start_insn & ~0x1fff) == 0x9de3a000) {
509 if ((int)(start_insn | ~0x1fff) < -128)
510 error("Found bogus save at the start of %s", name);
511 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
512 error("ret; restore; not found at end of %s", name);
514 error("No save at the beginning of %s", name);
517 /* Skip a preceeding nop, if present. */
519 skip_insn = get32((uint32_t *)(p - 0x4));
520 if (skip_insn == 0x01000000)
524 copy_size = p - p_start;
529 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
531 p = (void *)(p_end - 8);
533 error("empty code for %s", name);
534 start_insn = get32((uint32_t *)(p_start + 0x0));
535 end_insn1 = get32((uint32_t *)(p + 0x0));
536 end_insn2 = get32((uint32_t *)(p + 0x4));
537 if ((start_insn & ~0x1fff) == 0x9de3a000) {
540 if ((int)(start_insn | ~0x1fff) < -256)
541 error("Found bogus save at the start of %s", name);
542 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
543 error("ret; restore; not found at end of %s", name);
545 error("No save at the beginning of %s", name);
548 /* Skip a preceeding nop, if present. */
550 skip_insn = get32((uint32_t *)(p - 0x4));
551 if (skip_insn == 0x01000000)
555 copy_size = p - p_start;
560 if ((p_end - p_start) <= 16)
561 error("%s: function too small", name);
562 if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
563 (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
564 get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
565 error("%s: invalid prolog", name);
568 copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end,
573 error("unknown ELF architecture");
576 /* compute the number of arguments by looking at the relocations */
577 for(i = 0;i < MAX_ARGS; i++)
580 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
581 if (rel->r_offset >= start_offset &&
582 rel->r_offset < start_offset + (p_end - p_start)) {
583 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
584 if (strstart(sym_name, "__op_param", &p)) {
585 n = strtoul(p, NULL, 10);
587 error("too many arguments in %s", name);
588 args_present[n - 1] = 1;
594 while (nb_args < MAX_ARGS && args_present[nb_args])
596 for(i = nb_args; i < MAX_ARGS; i++) {
598 error("inconsistent argument numbering in %s", name);
601 if (gen_switch == 2) {
602 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
603 } else if (gen_switch == 1) {
606 fprintf(outfile, "case INDEX_%s: {\n", name);
608 fprintf(outfile, " long ");
609 for(i = 0; i < nb_args; i++) {
611 fprintf(outfile, ", ");
612 fprintf(outfile, "param%d", i + 1);
614 fprintf(outfile, ";\n");
616 fprintf(outfile, " extern void %s();\n", name);
618 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
619 if (rel->r_offset >= start_offset &&
620 rel->r_offset < start_offset + (p_end - p_start)) {
621 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
623 !strstart(sym_name, "__op_param", NULL) &&
624 !strstart(sym_name, "__op_jmp", NULL)) {
625 #if defined(HOST_SPARC)
626 if (sym_name[0] == '.') {
628 "extern char __dot_%s __asm__(\"%s\");\n",
629 sym_name+1, sym_name);
633 fprintf(outfile, "extern char %s;\n", sym_name);
638 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
640 /* emit code offset information */
643 const char *sym_name, *p;
647 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
648 sym_name = strtab + sym->st_name;
649 if (strstart(sym_name, "__op_label", &p)) {
652 unsigned long offset;
654 /* test if the variable refers to a label inside
655 the code we are generating */
656 ptr = sdata[sym->st_shndx];
658 error("__op_labelN in invalid section");
659 offset = sym->st_value;
661 #ifdef ELF_USES_RELOCA
663 int reloc_shndx, nb_relocs1, j;
665 /* try to find a matching relocation */
666 reloc_shndx = find_reloc(sym->st_shndx);
668 nb_relocs1 = shdr[reloc_shndx].sh_size /
669 shdr[reloc_shndx].sh_entsize;
670 rel = (ELF_RELOC *)sdata[reloc_shndx];
671 for(j = 0; j < nb_relocs1; j++) {
672 if (rel->r_offset == offset) {
673 addend = rel->r_addend;
681 val = *(target_ulong *)(ptr + offset);
684 if (val >= start_offset && val < start_offset + copy_size) {
685 n = strtol(p, NULL, 10);
686 fprintf(outfile, " label_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
692 /* load parameres in variables */
693 for(i = 0; i < nb_args; i++) {
694 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
697 /* patch relocations */
698 #if defined(HOST_I386)
703 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
704 if (rel->r_offset >= start_offset &&
705 rel->r_offset < start_offset + copy_size) {
706 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
707 if (strstart(sym_name, "__op_param", &p)) {
708 snprintf(name, sizeof(name), "param%s", p);
710 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
712 type = ELF32_R_TYPE(rel->r_info);
713 addend = get32((uint32_t *)(text + rel->r_offset));
716 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
717 rel->r_offset - start_offset, name, addend);
720 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
721 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
724 error("unsupported i386 relocation (%d)", type);
729 #elif defined(HOST_PPC)
734 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
735 if (rel->r_offset >= start_offset &&
736 rel->r_offset < start_offset + copy_size) {
737 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
738 if (strstart(sym_name, "__op_jmp", &p)) {
740 n = strtol(p, NULL, 10);
741 /* __op_jmp relocations are done at
742 runtime to do translated block
743 chaining: the offset of the instruction
744 needs to be stored */
745 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
746 n, rel->r_offset - start_offset);
750 if (strstart(sym_name, "__op_param", &p)) {
751 snprintf(name, sizeof(name), "param%s", p);
753 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
755 type = ELF32_R_TYPE(rel->r_info);
756 addend = rel->r_addend;
759 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
760 rel->r_offset - start_offset, name, addend);
762 case R_PPC_ADDR16_LO:
763 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
764 rel->r_offset - start_offset, name, addend);
766 case R_PPC_ADDR16_HI:
767 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
768 rel->r_offset - start_offset, name, addend);
770 case R_PPC_ADDR16_HA:
771 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
772 rel->r_offset - start_offset, name, addend);
775 /* warning: must be at 32 MB distancy */
776 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
777 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
780 error("unsupported powerpc relocation (%d)", type);
785 #elif defined(HOST_S390)
790 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
791 if (rel->r_offset >= start_offset &&
792 rel->r_offset < start_offset + copy_size) {
793 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
794 if (strstart(sym_name, "__op_param", &p)) {
795 snprintf(name, sizeof(name), "param%s", p);
797 snprintf(name, sizeof(name), "(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);
807 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
808 rel->r_offset - start_offset, name, addend);
811 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
812 rel->r_offset - start_offset, name, addend);
815 error("unsupported s390 relocation (%d)", type);
820 #elif defined(HOST_ALPHA)
822 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
823 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
826 type = ELF64_R_TYPE(rel->r_info);
827 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
830 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
831 as an immediate instead of constructing it from the pv or ra. */
832 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
833 rel->r_offset - start_offset);
834 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
835 rel->r_offset - start_offset + rel->r_addend);
838 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
839 now, since some called functions (libc) need pv to be set up. */
842 /* Branch target prediction hint. Ignore for now. Should be already
843 correct for in-function jumps. */
845 case R_ALPHA_LITERAL:
846 /* Load a literal from the GOT relative to the gp. Since there's only a
847 single gp, nothing is to be done. */
849 case R_ALPHA_GPRELHIGH:
850 /* Handle fake relocations against __op_param symbol. Need to emit the
851 high part of the immediate value instead. Other symbols need no
852 special treatment. */
853 if (strstart(sym_name, "__op_param", &p))
854 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
855 rel->r_offset - start_offset, p);
857 case R_ALPHA_GPRELLOW:
858 if (strstart(sym_name, "__op_param", &p))
859 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
860 rel->r_offset - start_offset, p);
863 /* PC-relative jump. Tweak offset to skip the two instructions that try to
864 set up the gp from the pv. */
865 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
866 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
869 error("unsupported Alpha relocation (%d)", type);
874 #elif defined(HOST_IA64)
879 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
880 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
881 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
882 if (strstart(sym_name, "__op_param", &p)) {
883 snprintf(name, sizeof(name), "param%s", p);
885 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
887 type = ELF64_R_TYPE(rel->r_info);
888 addend = rel->r_addend;
891 error("must implemnt R_IA64_LTOFF22 relocation");
892 case R_IA64_PCREL21B:
893 error("must implemnt R_IA64_PCREL21B relocation");
895 error("unsupported ia64 relocation (%d)", type);
900 #elif defined(HOST_SPARC)
905 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
906 if (rel->r_offset >= start_offset &&
907 rel->r_offset < start_offset + copy_size) {
908 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
909 if (strstart(sym_name, "__op_param", &p)) {
910 snprintf(name, sizeof(name), "param%s", p);
912 if (sym_name[0] == '.')
913 snprintf(name, sizeof(name),
917 snprintf(name, sizeof(name),
918 "(long)(&%s)", sym_name);
920 type = ELF32_R_TYPE(rel->r_info);
921 addend = rel->r_addend;
924 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
925 rel->r_offset - start_offset, name, addend);
929 " *(uint32_t *)(gen_code_ptr + %d) = "
930 "((*(uint32_t *)(gen_code_ptr + %d)) "
932 " | (((%s + %d) >> 10) & 0x3fffff);\n",
933 rel->r_offset - start_offset,
934 rel->r_offset - start_offset,
939 " *(uint32_t *)(gen_code_ptr + %d) = "
940 "((*(uint32_t *)(gen_code_ptr + %d)) "
942 " | ((%s + %d) & 0x3ff);\n",
943 rel->r_offset - start_offset,
944 rel->r_offset - start_offset,
947 case R_SPARC_WDISP30:
949 " *(uint32_t *)(gen_code_ptr + %d) = "
950 "((*(uint32_t *)(gen_code_ptr + %d)) "
952 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
954 rel->r_offset - start_offset,
955 rel->r_offset - start_offset,
957 rel->r_offset - start_offset);
960 error("unsupported sparc relocation (%d)", type);
965 #elif defined(HOST_SPARC64)
970 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
971 if (rel->r_offset >= start_offset &&
972 rel->r_offset < start_offset + copy_size) {
973 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
974 if (strstart(sym_name, "__op_param", &p)) {
975 snprintf(name, sizeof(name), "param%s", p);
977 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
979 type = ELF64_R_TYPE(rel->r_info);
980 addend = rel->r_addend;
983 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
984 rel->r_offset - start_offset, name, addend);
988 " *(uint32_t *)(gen_code_ptr + %d) = "
989 "((*(uint32_t *)(gen_code_ptr + %d)) "
991 " | (((%s + %d) >> 10) & 0x3fffff);\n",
992 rel->r_offset - start_offset,
993 rel->r_offset - start_offset,
998 " *(uint32_t *)(gen_code_ptr + %d) = "
999 "((*(uint32_t *)(gen_code_ptr + %d)) "
1001 " | ((%s + %d) & 0x3ff);\n",
1002 rel->r_offset - start_offset,
1003 rel->r_offset - start_offset,
1006 case R_SPARC_WDISP30:
1008 " *(uint32_t *)(gen_code_ptr + %d) = "
1009 "((*(uint32_t *)(gen_code_ptr + %d)) "
1011 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1012 " & 0x3fffffff);\n",
1013 rel->r_offset - start_offset,
1014 rel->r_offset - start_offset,
1016 rel->r_offset - start_offset);
1019 error("unsupported sparc64 relocation (%d)", type);
1024 #elif defined(HOST_ARM)
1030 arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
1033 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1034 if (rel->r_offset >= start_offset &&
1035 rel->r_offset < start_offset + copy_size) {
1036 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1037 /* the compiler leave some unnecessary references to the code */
1038 if (sym_name[0] == '\0')
1040 if (strstart(sym_name, "__op_param", &p)) {
1041 snprintf(name, sizeof(name), "param%s", p);
1043 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1045 type = ELF32_R_TYPE(rel->r_info);
1046 addend = get32((uint32_t *)(text + rel->r_offset));
1049 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1050 rel->r_offset - start_offset, name, addend);
1053 fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
1054 rel->r_offset - start_offset, addend, name);
1057 error("unsupported arm relocation (%d)", type);
1063 #error unsupported CPU
1065 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
1066 fprintf(outfile, "}\n");
1067 fprintf(outfile, "break;\n\n");
1069 fprintf(outfile, "static inline void gen_%s(", name);
1071 fprintf(outfile, "void");
1073 for(i = 0; i < nb_args; i++) {
1075 fprintf(outfile, ", ");
1076 fprintf(outfile, "long param%d", i + 1);
1079 fprintf(outfile, ")\n");
1080 fprintf(outfile, "{\n");
1081 for(i = 0; i < nb_args; i++) {
1082 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
1084 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
1085 fprintf(outfile, "}\n\n");
1089 /* load an elf object file */
1090 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
1093 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
1102 fd = open(filename, O_RDONLY);
1104 error("can't open file '%s'", filename);
1106 /* Read ELF header. */
1107 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
1108 error("unable to read file header");
1110 /* Check ELF identification. */
1111 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
1112 || ehdr.e_ident[EI_MAG1] != ELFMAG1
1113 || ehdr.e_ident[EI_MAG2] != ELFMAG2
1114 || ehdr.e_ident[EI_MAG3] != ELFMAG3
1115 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
1116 error("bad ELF header");
1119 do_swap = elf_must_swap(&ehdr);
1121 elf_swap_ehdr(&ehdr);
1122 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
1123 error("Unsupported ELF class");
1124 if (ehdr.e_type != ET_REL)
1125 error("ELF object file expected");
1126 if (ehdr.e_version != EV_CURRENT)
1127 error("Invalid ELF version");
1128 if (!elf_check_arch(ehdr.e_machine))
1129 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
1131 /* read section headers */
1132 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
1134 for(i = 0; i < ehdr.e_shnum; i++) {
1135 elf_swap_shdr(&shdr[i]);
1139 /* read all section data */
1140 sdata = malloc(sizeof(void *) * ehdr.e_shnum);
1141 memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
1143 for(i = 0;i < ehdr.e_shnum; i++) {
1145 if (sec->sh_type != SHT_NOBITS)
1146 sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
1149 sec = &shdr[ehdr.e_shstrndx];
1150 shstr = sdata[ehdr.e_shstrndx];
1152 /* swap relocations */
1153 for(i = 0; i < ehdr.e_shnum; i++) {
1155 if (sec->sh_type == SHT_RELOC) {
1156 nb_relocs = sec->sh_size / sec->sh_entsize;
1158 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
1165 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
1167 error("could not find .text section");
1168 text_shndx = text_sec - shdr;
1169 text = sdata[text_shndx];
1171 /* find text relocations, if any */
1174 i = find_reloc(text_shndx);
1176 relocs = (ELF_RELOC *)sdata[i];
1177 nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
1180 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
1182 error("could not find .symtab section");
1183 strtab_sec = &shdr[symtab_sec->sh_link];
1185 symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
1186 strtab = sdata[symtab_sec->sh_link];
1188 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
1190 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1191 swab32s(&sym->st_name);
1192 swabls(&sym->st_value);
1193 swabls(&sym->st_size);
1194 swab16s(&sym->st_shndx);
1198 if (do_print_enum) {
1199 fprintf(outfile, "DEF(end, 0, 0)\n");
1200 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1201 const char *name, *p;
1202 name = strtab + sym->st_name;
1203 if (strstart(name, OP_PREFIX, &p)) {
1204 gen_code(name, sym->st_value, sym->st_size, outfile,
1205 text, relocs, nb_relocs, 2);
1209 /* generate big code generation switch */
1212 "register int gp asm(\"$29\");\n"
1213 "static inline void immediate_ldah(void *p, int val) {\n"
1214 " uint32_t *dest = p;\n"
1215 " long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff;\n"
1217 " *dest &= ~0xffff;\n"
1219 " *dest |= 31 << 16;\n"
1221 "static inline void immediate_lda(void *dest, int val) {\n"
1222 " *(uint16_t *) dest = val;\n"
1224 "void fix_bsr(void *p, int offset) {\n"
1225 " uint32_t *dest = p;\n"
1226 " *dest &= ~((1 << 21) - 1);\n"
1227 " *dest |= (offset >> 2) & ((1 << 21) - 1);\n"
1231 "int dyngen_code(uint8_t *gen_code_buf,\n"
1232 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1233 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1235 " uint8_t *gen_code_ptr;\n"
1236 " const uint16_t *opc_ptr;\n"
1237 " const uint32_t *opparam_ptr;\n");
1241 " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1242 " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1243 " uint32_t *arm_data_ptr = arm_data_table;\n");
1248 " gen_code_ptr = gen_code_buf;\n"
1249 " opc_ptr = opc_buf;\n"
1250 " opparam_ptr = opparam_buf;\n");
1252 /* Generate prologue, if needed. */
1256 " switch(*opc_ptr++) {\n"
1259 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1261 name = strtab + sym->st_name;
1262 if (strstart(name, OP_PREFIX, NULL)) {
1264 printf("%4d: %s pos=0x%08x len=%d\n",
1265 i, name, sym->st_value, sym->st_size);
1267 if (sym->st_shndx != (text_sec - shdr))
1268 error("invalid section for opcode (0x%x)", sym->st_shndx);
1269 gen_code(name, sym->st_value, sym->st_size, outfile,
1270 text, relocs, nb_relocs, 1);
1280 /* generate constant table if needed */
1282 " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1283 " gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1284 " last_gen_code_ptr = gen_code_ptr;\n"
1285 " arm_ldr_ptr = arm_ldr_table;\n"
1286 " arm_data_ptr = arm_data_table;\n"
1296 /* generate epilogue */
1299 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
1302 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
1305 fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
1308 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n");
1311 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n");
1314 case EM_SPARC32PLUS:
1315 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c62008; /* jmpl %%i0 + 8, %%g0 */\n");
1316 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x01000000; /* nop */\n");
1319 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c7e008; /* ret */\n");
1320 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81e80000; /* restore */\n");
1323 fprintf(outfile, "gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 0);\n");
1326 error("unknown ELF architecture");
1329 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
1330 fprintf(outfile, "}\n\n");
1332 /* generate gen_xxx functions */
1333 /* XXX: suppress the use of these functions to simplify code */
1334 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1336 name = strtab + sym->st_name;
1337 if (strstart(name, OP_PREFIX, NULL)) {
1338 if (sym->st_shndx != (text_sec - shdr))
1339 error("invalid section for opcode (0x%x)", sym->st_shndx);
1340 gen_code(name, sym->st_value, sym->st_size, outfile,
1341 text, relocs, nb_relocs, 0);
1352 printf("dyngen (c) 2003 Fabrice Bellard\n"
1353 "usage: dyngen [-o outfile] [-c] objfile\n"
1354 "Generate a dynamic code generator from an object file\n"
1355 "-c output enum of operations\n"
1360 int main(int argc, char **argv)
1362 int c, do_print_enum;
1363 const char *filename, *outfilename;
1366 outfilename = "out.c";
1369 c = getopt(argc, argv, "ho:c");
1377 outfilename = optarg;
1386 filename = argv[optind];
1387 outfile = fopen(outfilename, "w");
1389 error("could not open '%s'", outfilename);
1390 load_elf(filename, outfile, do_print_enum);