2 * Generic Dynamic compiler generator
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 /* elf format definitions. We use these macros to test the CPU to
31 allow cross compilation (this tool must be ran on the build
33 #if defined(HOST_I386)
35 #define ELF_CLASS ELFCLASS32
36 #define ELF_ARCH EM_386
37 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
38 #undef ELF_USES_RELOCA
40 #elif defined(HOST_PPC)
42 #define ELF_CLASS ELFCLASS32
43 #define ELF_ARCH EM_PPC
44 #define elf_check_arch(x) ((x) == EM_PPC)
45 #define ELF_USES_RELOCA
47 #elif defined(HOST_S390)
49 #define ELF_CLASS ELFCLASS32
50 #define ELF_ARCH EM_S390
51 #define elf_check_arch(x) ((x) == EM_S390)
52 #define ELF_USES_RELOCA
54 #elif defined(HOST_ALPHA)
56 #define ELF_CLASS ELFCLASS64
57 #define ELF_ARCH EM_ALPHA
58 #define elf_check_arch(x) ((x) == EM_ALPHA)
59 #define ELF_USES_RELOCA
61 #elif defined(HOST_IA64)
63 #define ELF_CLASS ELFCLASS64
64 #define ELF_ARCH EM_IA_64
65 #define elf_check_arch(x) ((x) == EM_IA_64)
66 #define ELF_USES_RELOCA
68 #elif defined(HOST_SPARC)
70 #define ELF_CLASS ELFCLASS32
71 #define ELF_ARCH EM_SPARC
72 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
73 #define ELF_USES_RELOCA
75 #elif defined(HOST_SPARC64)
77 #define ELF_CLASS ELFCLASS64
78 #define ELF_ARCH EM_SPARCV9
79 #define elf_check_arch(x) ((x) == EM_SPARCV9)
80 #define ELF_USES_RELOCA
83 #error unsupported CPU - please update the code
88 #if ELF_CLASS == ELFCLASS32
89 typedef int32_t host_long;
90 typedef uint32_t host_ulong;
91 #define swabls(x) swab32s(x)
93 typedef int64_t host_long;
94 typedef uint64_t host_ulong;
95 #define swabls(x) swab64s(x)
100 /* all dynamically generated functions begin with this code */
101 #define OP_PREFIX "op_"
103 int elf_must_swap(struct elfhdr *h)
111 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
112 (swaptest.b[0] == 0);
115 void swab16s(uint16_t *p)
120 void swab32s(uint32_t *p)
125 void swab64s(uint64_t *p)
130 void elf_swap_ehdr(struct elfhdr *h)
132 swab16s(&h->e_type); /* Object file type */
133 swab16s(&h-> e_machine); /* Architecture */
134 swab32s(&h-> e_version); /* Object file version */
135 swabls(&h-> e_entry); /* Entry point virtual address */
136 swabls(&h-> e_phoff); /* Program header table file offset */
137 swabls(&h-> e_shoff); /* Section header table file offset */
138 swab32s(&h-> e_flags); /* Processor-specific flags */
139 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
140 swab16s(&h-> e_phentsize); /* Program header table entry size */
141 swab16s(&h-> e_phnum); /* Program header table entry count */
142 swab16s(&h-> e_shentsize); /* Section header table entry size */
143 swab16s(&h-> e_shnum); /* Section header table entry count */
144 swab16s(&h-> e_shstrndx); /* Section header string table index */
147 void elf_swap_shdr(struct elf_shdr *h)
149 swab32s(&h-> sh_name); /* Section name (string tbl index) */
150 swab32s(&h-> sh_type); /* Section type */
151 swabls(&h-> sh_flags); /* Section flags */
152 swabls(&h-> sh_addr); /* Section virtual addr at execution */
153 swabls(&h-> sh_offset); /* Section file offset */
154 swabls(&h-> sh_size); /* Section size in bytes */
155 swab32s(&h-> sh_link); /* Link to another section */
156 swab32s(&h-> sh_info); /* Additional section information */
157 swabls(&h-> sh_addralign); /* Section alignment */
158 swabls(&h-> sh_entsize); /* Entry size if section holds table */
161 void elf_swap_phdr(struct elf_phdr *h)
163 swab32s(&h->p_type); /* Segment type */
164 swabls(&h->p_offset); /* Segment file offset */
165 swabls(&h->p_vaddr); /* Segment virtual address */
166 swabls(&h->p_paddr); /* Segment physical address */
167 swabls(&h->p_filesz); /* Segment size in file */
168 swabls(&h->p_memsz); /* Segment size in memory */
169 swab32s(&h->p_flags); /* Segment flags */
170 swabls(&h->p_align); /* Segment alignment */
175 uint16_t get16(uint16_t *p)
184 uint32_t get32(uint32_t *p)
193 void put16(uint16_t *p, uint16_t val)
200 void put32(uint32_t *p, uint32_t val)
207 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
211 fprintf(stderr, "dyngen: ");
212 vfprintf(stderr, fmt, ap);
213 fprintf(stderr, "\n");
219 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
224 struct elf_shdr *sec;
226 for(i = 0; i < shnum; i++) {
230 shname = shstr + sec->sh_name;
231 if (!strcmp(shname, name))
237 void *load_data(int fd, long offset, unsigned int size)
244 lseek(fd, offset, SEEK_SET);
245 if (read(fd, data, size) != size) {
252 int strstart(const char *str, const char *val, const char **ptr)
270 /* generate op code */
271 void gen_code(const char *name, host_ulong offset, host_ulong size,
272 FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs, int reloc_sh_type,
273 ElfW(Sym) *symtab, char *strtab, int gen_switch)
276 uint8_t *p_start, *p_end;
277 host_ulong start_offset;
279 uint8_t args_present[MAX_ARGS];
280 const char *sym_name, *p;
283 /* Compute exact size excluding prologue and epilogue instructions.
284 * Increment start_offset to skip epilogue instructions, then compute
285 * copy_size the indicate the size of the remaining instructions (in
288 p_start = text + offset;
289 p_end = p_start + size;
290 start_offset = offset;
297 error("empty code for %s", name);
299 error("ret expected at the end of %s", name);
300 copy_size = p - p_start;
306 p = (void *)(p_end - 4);
308 error("empty code for %s", name);
309 if (get32((uint32_t *)p) != 0x4e800020)
310 error("blr expected at the end of %s", name);
311 copy_size = p - p_start;
317 p = (void *)(p_end - 2);
319 error("empty code for %s", name);
320 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
321 error("br %%r14 expected at the end of %s", name);
322 copy_size = p - p_start;
330 error("empty code for %s", name);
331 if (get32((uint32_t *)p) != 0x6bfa8001)
332 error("ret expected at the end of %s", name);
333 copy_size = p - p_start;
339 p = (void *)(p_end - 4);
341 error("empty code for %s", name);
342 /* br.ret.sptk.many b0;; */
344 if (get32((uint32_t *)p) != 0x00840008)
345 error("br.ret.sptk.many b0;; expected at the end of %s", name);
346 copy_size = p - p_start;
352 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
354 p = (void *)(p_end - 8);
356 error("empty code for %s", name);
357 start_insn = get32((uint32_t *)(p_start + 0x0));
358 end_insn1 = get32((uint32_t *)(p + 0x0));
359 end_insn2 = get32((uint32_t *)(p + 0x4));
360 if ((start_insn & ~0x1fff) == 0x9de3a000) {
363 if ((int)(start_insn | ~0x1fff) < -128)
364 error("Found bogus save at the start of %s", name);
365 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
366 error("ret; restore; not found at end of %s", name);
368 error("No save at the beginning of %s", name);
371 /* Skip a preceeding nop, if present. */
373 skip_insn = get32((uint32_t *)(p - 0x4));
374 if (skip_insn == 0x01000000)
378 copy_size = p - p_start;
383 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
385 p = (void *)(p_end - 8);
387 error("empty code for %s", name);
388 start_insn = get32((uint32_t *)(p_start + 0x0));
389 end_insn1 = get32((uint32_t *)(p + 0x0));
390 end_insn2 = get32((uint32_t *)(p + 0x4));
391 if ((start_insn & ~0x1fff) == 0x9de3a000) {
394 if ((int)(start_insn | ~0x1fff) < -256)
395 error("Found bogus save at the start of %s", name);
396 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
397 error("ret; restore; not found at end of %s", name);
399 error("No save at the beginning of %s", name);
402 /* Skip a preceeding nop, if present. */
404 skip_insn = get32((uint32_t *)(p - 0x4));
405 if (skip_insn == 0x01000000)
409 copy_size = p - p_start;
413 error("unknown ELF architecture");
416 /* compute the number of arguments by looking at the relocations */
417 for(i = 0;i < MAX_ARGS; i++)
420 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
421 if (rel->r_offset >= start_offset &&
422 rel->r_offset < start_offset + copy_size) {
423 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
424 if (strstart(sym_name, "__op_param", &p)) {
425 n = strtoul(p, NULL, 10);
427 error("too many arguments in %s", name);
428 args_present[n - 1] = 1;
434 while (nb_args < MAX_ARGS && args_present[nb_args])
436 for(i = nb_args; i < MAX_ARGS; i++) {
438 error("inconsistent argument numbering in %s", name);
441 if (gen_switch == 2) {
442 fprintf(outfile, "DEF(%s, %d)\n", name + 3, nb_args);
443 } else if (gen_switch == 1) {
446 fprintf(outfile, "case INDEX_%s: {\n", name);
448 fprintf(outfile, " long ");
449 for(i = 0; i < nb_args; i++) {
451 fprintf(outfile, ", ");
452 fprintf(outfile, "param%d", i + 1);
454 fprintf(outfile, ";\n");
456 fprintf(outfile, " extern void %s();\n", name);
458 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
459 if (rel->r_offset >= start_offset &&
460 rel->r_offset < start_offset + copy_size) {
461 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
462 if (*sym_name && !strstart(sym_name, "__op_param", &p)) {
463 #if defined(HOST_SPARC)
464 if (sym_name[0] == '.') {
466 "extern char __dot_%s __asm__(\"%s\");\n",
467 sym_name+1, sym_name);
471 fprintf(outfile, "extern char %s;\n", sym_name);
476 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
477 for(i = 0; i < nb_args; i++) {
478 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
481 /* patch relocations */
482 #if defined(HOST_I386)
487 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
488 if (rel->r_offset >= start_offset &&
489 rel->r_offset < start_offset + copy_size) {
490 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
491 if (strstart(sym_name, "__op_param", &p)) {
492 snprintf(name, sizeof(name), "param%s", p);
494 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
496 type = ELF32_R_TYPE(rel->r_info);
497 addend = get32((uint32_t *)(text + rel->r_offset));
500 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
501 rel->r_offset - start_offset, name, addend);
504 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
505 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
508 error("unsupported i386 relocation (%d)", type);
513 #elif defined(HOST_PPC)
518 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
519 if (rel->r_offset >= start_offset &&
520 rel->r_offset < start_offset + copy_size) {
521 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
522 if (strstart(sym_name, "__op_param", &p)) {
523 snprintf(name, sizeof(name), "param%s", p);
525 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
527 type = ELF32_R_TYPE(rel->r_info);
528 addend = rel->r_addend;
531 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
532 rel->r_offset - start_offset, name, addend);
534 case R_PPC_ADDR16_LO:
535 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
536 rel->r_offset - start_offset, name, addend);
538 case R_PPC_ADDR16_HI:
539 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
540 rel->r_offset - start_offset, name, addend);
542 case R_PPC_ADDR16_HA:
543 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
544 rel->r_offset - start_offset, name, addend);
547 /* warning: must be at 32 MB distancy */
548 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
549 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
552 error("unsupported powerpc relocation (%d)", type);
557 #elif defined(HOST_S390)
562 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
563 if (rel->r_offset >= start_offset &&
564 rel->r_offset < start_offset + copy_size) {
565 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
566 if (strstart(sym_name, "__op_param", &p)) {
567 snprintf(name, sizeof(name), "param%s", p);
569 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
571 type = ELF32_R_TYPE(rel->r_info);
572 addend = rel->r_addend;
575 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
576 rel->r_offset - start_offset, name, addend);
579 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
580 rel->r_offset - start_offset, name, addend);
583 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
584 rel->r_offset - start_offset, name, addend);
587 error("unsupported s390 relocation (%d)", type);
592 #elif defined(HOST_ALPHA)
594 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
595 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
598 type = ELF64_R_TYPE(rel->r_info);
599 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
602 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
603 as an immediate instead of constructing it from the pv or ra. */
604 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
605 rel->r_offset - start_offset);
606 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
607 rel->r_offset - start_offset + rel->r_addend);
610 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
611 now, since some called functions (libc) need pv to be set up. */
614 /* Branch target prediction hint. Ignore for now. Should be already
615 correct for in-function jumps. */
617 case R_ALPHA_LITERAL:
618 /* Load a literal from the GOT relative to the gp. Since there's only a
619 single gp, nothing is to be done. */
621 case R_ALPHA_GPRELHIGH:
622 /* Handle fake relocations against __op_param symbol. Need to emit the
623 high part of the immediate value instead. Other symbols need no
624 special treatment. */
625 if (strstart(sym_name, "__op_param", &p))
626 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
627 rel->r_offset - start_offset, p);
629 case R_ALPHA_GPRELLOW:
630 if (strstart(sym_name, "__op_param", &p))
631 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
632 rel->r_offset - start_offset, p);
635 /* PC-relative jump. Tweak offset to skip the two instructions that try to
636 set up the gp from the pv. */
637 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld) + 4);\n",
638 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
641 error("unsupported Alpha relocation (%d)", type);
646 #elif defined(HOST_IA64)
651 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
652 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
653 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
654 if (strstart(sym_name, "__op_param", &p)) {
655 snprintf(name, sizeof(name), "param%s", p);
657 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
659 type = ELF64_R_TYPE(rel->r_info);
660 addend = rel->r_addend;
663 error("must implemnt R_IA64_LTOFF22 relocation");
664 case R_IA64_PCREL21B:
665 error("must implemnt R_IA64_PCREL21B relocation");
667 error("unsupported ia64 relocation (%d)", type);
672 #elif defined(HOST_SPARC)
677 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
678 if (rel->r_offset >= start_offset &&
679 rel->r_offset < start_offset + copy_size) {
680 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
681 if (strstart(sym_name, "__op_param", &p)) {
682 snprintf(name, sizeof(name), "param%s", p);
684 if (sym_name[0] == '.')
685 snprintf(name, sizeof(name),
689 snprintf(name, sizeof(name),
690 "(long)(&%s)", sym_name);
692 type = ELF32_R_TYPE(rel->r_info);
693 addend = rel->r_addend;
696 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
697 rel->r_offset - start_offset, name, addend);
701 " *(uint32_t *)(gen_code_ptr + %d) = "
702 "((*(uint32_t *)(gen_code_ptr + %d)) "
704 " | (((%s + %d) >> 10) & 0x3fffff);\n",
705 rel->r_offset - start_offset,
706 rel->r_offset - start_offset,
711 " *(uint32_t *)(gen_code_ptr + %d) = "
712 "((*(uint32_t *)(gen_code_ptr + %d)) "
714 " | ((%s + %d) & 0x3ff);\n",
715 rel->r_offset - start_offset,
716 rel->r_offset - start_offset,
719 case R_SPARC_WDISP30:
721 " *(uint32_t *)(gen_code_ptr + %d) = "
722 "((*(uint32_t *)(gen_code_ptr + %d)) "
724 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
726 rel->r_offset - start_offset,
727 rel->r_offset - start_offset,
729 rel->r_offset - start_offset);
732 error("unsupported sparc relocation (%d)", type);
737 #elif defined(HOST_SPARC64)
742 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
743 if (rel->r_offset >= start_offset &&
744 rel->r_offset < start_offset + copy_size) {
745 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
746 if (strstart(sym_name, "__op_param", &p)) {
747 snprintf(name, sizeof(name), "param%s", p);
749 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
751 type = ELF64_R_TYPE(rel->r_info);
752 addend = rel->r_addend;
755 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
756 rel->r_offset - start_offset, name, addend);
760 " *(uint32_t *)(gen_code_ptr + %d) = "
761 "((*(uint32_t *)(gen_code_ptr + %d)) "
763 " | (((%s + %d) >> 10) & 0x3fffff);\n",
764 rel->r_offset - start_offset,
765 rel->r_offset - start_offset,
770 " *(uint32_t *)(gen_code_ptr + %d) = "
771 "((*(uint32_t *)(gen_code_ptr + %d)) "
773 " | ((%s + %d) & 0x3ff);\n",
774 rel->r_offset - start_offset,
775 rel->r_offset - start_offset,
778 case R_SPARC_WDISP30:
780 " *(uint32_t *)(gen_code_ptr + %d) = "
781 "((*(uint32_t *)(gen_code_ptr + %d)) "
783 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
785 rel->r_offset - start_offset,
786 rel->r_offset - start_offset,
788 rel->r_offset - start_offset);
791 error("unsupported sparc64 relocation (%d)", type);
797 #error unsupported CPU
799 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
800 fprintf(outfile, "}\n");
801 fprintf(outfile, "break;\n\n");
803 fprintf(outfile, "static inline void gen_%s(", name);
805 fprintf(outfile, "void");
807 for(i = 0; i < nb_args; i++) {
809 fprintf(outfile, ", ");
810 fprintf(outfile, "long param%d", i + 1);
813 fprintf(outfile, ")\n");
814 fprintf(outfile, "{\n");
815 for(i = 0; i < nb_args; i++) {
816 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
818 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
819 fprintf(outfile, "}\n\n");
823 /* load an elf object file */
824 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
828 struct elf_shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
830 ElfW(Sym) *symtab, *sym;
831 char *shstr, *strtab;
834 int nb_relocs, reloc_sh_type;
836 fd = open(filename, O_RDONLY);
838 error("can't open file '%s'", filename);
840 /* Read ELF header. */
841 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
842 error("unable to read file header");
844 /* Check ELF identification. */
845 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
846 || ehdr.e_ident[EI_MAG1] != ELFMAG1
847 || ehdr.e_ident[EI_MAG2] != ELFMAG2
848 || ehdr.e_ident[EI_MAG3] != ELFMAG3
849 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
850 error("bad ELF header");
853 do_swap = elf_must_swap(&ehdr);
855 elf_swap_ehdr(&ehdr);
856 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
857 error("Unsupported ELF class");
858 if (ehdr.e_type != ET_REL)
859 error("ELF object file expected");
860 if (ehdr.e_version != EV_CURRENT)
861 error("Invalid ELF version");
862 if (!elf_check_arch(ehdr.e_machine))
863 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
865 /* read section headers */
866 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
868 for(i = 0; i < ehdr.e_shnum; i++) {
869 elf_swap_shdr(&shdr[i]);
873 sec = &shdr[ehdr.e_shstrndx];
874 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
878 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
880 error("could not find .text section");
881 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
883 /* find text relocations, if any */
887 for(i = 0; i < ehdr.e_shnum; i++) {
889 if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) &&
890 sec->sh_info == (text_sec - shdr)) {
891 reloc_sh_type = sec->sh_type;
892 relocs = load_data(fd, sec->sh_offset, sec->sh_size);
893 nb_relocs = sec->sh_size / sec->sh_entsize;
895 if (sec->sh_type == SHT_REL) {
896 ElfW(Rel) *rel = relocs;
897 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
898 swabls(&rel->r_offset);
899 swabls(&rel->r_info);
902 ElfW(Rela) *rel = relocs;
903 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
904 swabls(&rel->r_offset);
905 swabls(&rel->r_info);
906 swabls(&rel->r_addend);
914 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
916 error("could not find .symtab section");
917 strtab_sec = &shdr[symtab_sec->sh_link];
919 symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size);
920 strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size);
922 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
924 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
925 swab32s(&sym->st_name);
926 swabls(&sym->st_value);
927 swabls(&sym->st_size);
928 swab16s(&sym->st_shndx);
933 fprintf(outfile, "DEF(end, 0)\n");
934 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
935 const char *name, *p;
936 name = strtab + sym->st_name;
937 if (strstart(name, OP_PREFIX, &p)) {
938 gen_code(name, sym->st_value, sym->st_size, outfile,
939 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 2);
943 /* generate big code generation switch */
946 "register int gp asm(\"$29\");\n"
947 "static inline void immediate_ldah(void *p, int val) {\n"
948 " uint32_t *dest = p;\n"
949 " long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff;\n"
951 " *dest &= ~0xffff;\n"
953 " *dest |= 31 << 16;\n"
955 "static inline void immediate_lda(void *dest, int val) {\n"
956 " *(uint16_t *) dest = val;\n"
958 "void fix_bsr(void *p, int offset) {\n"
959 " uint32_t *dest = p;\n"
960 " *dest &= ~((1 << 21) - 1);\n"
961 " *dest |= (offset >> 2) & ((1 << 21) - 1);\n"
965 "int dyngen_code(uint8_t *gen_code_buf,\n"
966 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
968 " uint8_t *gen_code_ptr;\n"
969 " const uint16_t *opc_ptr;\n"
970 " const uint32_t *opparam_ptr;\n"
971 " gen_code_ptr = gen_code_buf;\n"
972 " opc_ptr = opc_buf;\n"
973 " opparam_ptr = opparam_buf;\n");
975 /* Generate prologue, if needed. */
978 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c23a080; /* sub %%sp, 128, %%sp */\n");
979 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc27a080; /* sub %%fp, 128, %%fp */\n");
983 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c23a100; /* sub %%sp, 256, %%sp */\n");
984 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc27a100; /* sub %%fp, 256, %%fp */\n");
990 " switch(*opc_ptr++) {\n"
993 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
995 name = strtab + sym->st_name;
996 if (strstart(name, OP_PREFIX, NULL)) {
998 printf("%4d: %s pos=0x%08x len=%d\n",
999 i, name, sym->st_value, sym->st_size);
1001 if (sym->st_shndx != (text_sec - shdr))
1002 error("invalid section for opcode (0x%x)", sym->st_shndx);
1003 gen_code(name, sym->st_value, sym->st_size, outfile,
1004 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 1);
1016 /* generate epilogue */
1019 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
1022 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
1025 fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
1028 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n");
1031 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n");
1034 case EM_SPARC32PLUS:
1035 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc07a080; /* add %%fp, 256, %%fp */\n");
1036 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c62008; /* jmpl %%i0 + 8, %%g0 */\n");
1037 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c03a080; /* add %%sp, 256, %%sp */\n");
1040 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c7e008; /* ret */\n");
1041 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81e80000; /* restore */\n");
1044 error("unknown ELF architecture");
1047 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
1048 fprintf(outfile, "}\n\n");
1050 /* generate gen_xxx functions */
1051 /* XXX: suppress the use of these functions to simplify code */
1052 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1054 name = strtab + sym->st_name;
1055 if (strstart(name, OP_PREFIX, NULL)) {
1056 if (sym->st_shndx != (text_sec - shdr))
1057 error("invalid section for opcode (0x%x)", sym->st_shndx);
1058 gen_code(name, sym->st_value, sym->st_size, outfile,
1059 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 0);
1070 printf("dyngen (c) 2003 Fabrice Bellard\n"
1071 "usage: dyngen [-o outfile] [-c] objfile\n"
1072 "Generate a dynamic code generator from an object file\n"
1073 "-c output enum of operations\n"
1078 int main(int argc, char **argv)
1080 int c, do_print_enum;
1081 const char *filename, *outfilename;
1084 outfilename = "out.c";
1087 c = getopt(argc, argv, "ho:c");
1095 outfilename = optarg;
1104 filename = argv[optind];
1105 outfile = fopen(outfilename, "w");
1107 error("could not open '%s'", outfilename);
1108 load_elf(filename, outfile, do_print_enum);