2 * Generic Dynamic compiler generator
4 * Copyright (c) 2003 Fabrice Bellard
6 * The COFF object format support was extracted from Kazu's QEMU port
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include "config-host.h"
33 /* NOTE: we test CONFIG_WIN32 instead of _WIN32 to enabled cross
35 #if defined(CONFIG_WIN32)
36 #define CONFIG_FORMAT_COFF
38 #define CONFIG_FORMAT_ELF
41 #ifdef CONFIG_FORMAT_ELF
43 /* elf format definitions. We use these macros to test the CPU to
44 allow cross compilation (this tool must be ran on the build
46 #if defined(HOST_I386)
48 #define ELF_CLASS ELFCLASS32
49 #define ELF_ARCH EM_386
50 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
51 #undef ELF_USES_RELOCA
53 #elif defined(HOST_AMD64)
55 #define ELF_CLASS ELFCLASS64
56 #define ELF_ARCH EM_X86_64
57 #define elf_check_arch(x) ((x) == EM_X86_64)
58 #define ELF_USES_RELOCA
60 #elif defined(HOST_PPC)
62 #define ELF_CLASS ELFCLASS32
63 #define ELF_ARCH EM_PPC
64 #define elf_check_arch(x) ((x) == EM_PPC)
65 #define ELF_USES_RELOCA
67 #elif defined(HOST_S390)
69 #define ELF_CLASS ELFCLASS32
70 #define ELF_ARCH EM_S390
71 #define elf_check_arch(x) ((x) == EM_S390)
72 #define ELF_USES_RELOCA
74 #elif defined(HOST_ALPHA)
76 #define ELF_CLASS ELFCLASS64
77 #define ELF_ARCH EM_ALPHA
78 #define elf_check_arch(x) ((x) == EM_ALPHA)
79 #define ELF_USES_RELOCA
81 #elif defined(HOST_IA64)
83 #define ELF_CLASS ELFCLASS64
84 #define ELF_ARCH EM_IA_64
85 #define elf_check_arch(x) ((x) == EM_IA_64)
86 #define ELF_USES_RELOCA
88 #elif defined(HOST_SPARC)
90 #define ELF_CLASS ELFCLASS32
91 #define ELF_ARCH EM_SPARC
92 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
93 #define ELF_USES_RELOCA
95 #elif defined(HOST_SPARC64)
97 #define ELF_CLASS ELFCLASS64
98 #define ELF_ARCH EM_SPARCV9
99 #define elf_check_arch(x) ((x) == EM_SPARCV9)
100 #define ELF_USES_RELOCA
102 #elif defined(HOST_ARM)
104 #define ELF_CLASS ELFCLASS32
105 #define ELF_ARCH EM_ARM
106 #define elf_check_arch(x) ((x) == EM_ARM)
107 #define ELF_USES_RELOC
109 #elif defined(HOST_M68K)
111 #define ELF_CLASS ELFCLASS32
112 #define ELF_ARCH EM_68K
113 #define elf_check_arch(x) ((x) == EM_68K)
114 #define ELF_USES_RELOCA
117 #error unsupported CPU - please update the code
122 #if ELF_CLASS == ELFCLASS32
123 typedef int32_t host_long;
124 typedef uint32_t host_ulong;
125 #define swabls(x) swab32s(x)
127 typedef int64_t host_long;
128 typedef uint64_t host_ulong;
129 #define swabls(x) swab64s(x)
132 #ifdef ELF_USES_RELOCA
133 #define SHT_RELOC SHT_RELA
135 #define SHT_RELOC SHT_REL
138 #define EXE_RELOC ELF_RELOC
139 #define EXE_SYM ElfW(Sym)
141 #endif /* CONFIG_FORMAT_ELF */
143 #ifdef CONFIG_FORMAT_COFF
147 typedef int32_t host_long;
148 typedef uint32_t host_ulong;
150 #define FILENAMELEN 256
152 typedef struct coff_sym {
153 struct external_syment *st_syment;
154 char st_name[FILENAMELEN];
161 typedef struct coff_rel {
162 struct external_reloc *r_reloc;
167 #define EXE_RELOC struct coff_rel
168 #define EXE_SYM struct coff_sym
170 #endif /* CONFIG_FORMAT_COFF */
180 /* all dynamically generated functions begin with this code */
181 #define OP_PREFIX "op_"
185 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
189 fprintf(stderr, "dyngen: ");
190 vfprintf(stderr, fmt, ap);
191 fprintf(stderr, "\n");
196 void *load_data(int fd, long offset, unsigned int size)
203 lseek(fd, offset, SEEK_SET);
204 if (read(fd, data, size) != size) {
211 int strstart(const char *str, const char *val, const char **ptr)
227 void pstrcpy(char *buf, int buf_size, const char *str)
237 if (c == 0 || q >= buf + buf_size - 1)
244 void swab16s(uint16_t *p)
249 void swab32s(uint32_t *p)
254 void swab64s(uint64_t *p)
259 uint16_t get16(uint16_t *p)
268 uint32_t get32(uint32_t *p)
277 void put16(uint16_t *p, uint16_t val)
284 void put32(uint32_t *p, uint32_t val)
291 /* executable information */
299 #ifdef CONFIG_FORMAT_ELF
302 struct elf_shdr *shdr;
307 int elf_must_swap(struct elfhdr *h)
315 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
316 (swaptest.b[0] == 0);
319 void elf_swap_ehdr(struct elfhdr *h)
321 swab16s(&h->e_type); /* Object file type */
322 swab16s(&h-> e_machine); /* Architecture */
323 swab32s(&h-> e_version); /* Object file version */
324 swabls(&h-> e_entry); /* Entry point virtual address */
325 swabls(&h-> e_phoff); /* Program header table file offset */
326 swabls(&h-> e_shoff); /* Section header table file offset */
327 swab32s(&h-> e_flags); /* Processor-specific flags */
328 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
329 swab16s(&h-> e_phentsize); /* Program header table entry size */
330 swab16s(&h-> e_phnum); /* Program header table entry count */
331 swab16s(&h-> e_shentsize); /* Section header table entry size */
332 swab16s(&h-> e_shnum); /* Section header table entry count */
333 swab16s(&h-> e_shstrndx); /* Section header string table index */
336 void elf_swap_shdr(struct elf_shdr *h)
338 swab32s(&h-> sh_name); /* Section name (string tbl index) */
339 swab32s(&h-> sh_type); /* Section type */
340 swabls(&h-> sh_flags); /* Section flags */
341 swabls(&h-> sh_addr); /* Section virtual addr at execution */
342 swabls(&h-> sh_offset); /* Section file offset */
343 swabls(&h-> sh_size); /* Section size in bytes */
344 swab32s(&h-> sh_link); /* Link to another section */
345 swab32s(&h-> sh_info); /* Additional section information */
346 swabls(&h-> sh_addralign); /* Section alignment */
347 swabls(&h-> sh_entsize); /* Entry size if section holds table */
350 void elf_swap_phdr(struct elf_phdr *h)
352 swab32s(&h->p_type); /* Segment type */
353 swabls(&h->p_offset); /* Segment file offset */
354 swabls(&h->p_vaddr); /* Segment virtual address */
355 swabls(&h->p_paddr); /* Segment physical address */
356 swabls(&h->p_filesz); /* Segment size in file */
357 swabls(&h->p_memsz); /* Segment size in memory */
358 swab32s(&h->p_flags); /* Segment flags */
359 swabls(&h->p_align); /* Segment alignment */
362 void elf_swap_rel(ELF_RELOC *rel)
364 swabls(&rel->r_offset);
365 swabls(&rel->r_info);
366 #ifdef ELF_USES_RELOCA
367 swabls(&rel->r_addend);
371 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
376 struct elf_shdr *sec;
378 for(i = 0; i < shnum; i++) {
382 shname = shstr + sec->sh_name;
383 if (!strcmp(shname, name))
389 int find_reloc(int sh_index)
391 struct elf_shdr *sec;
394 for(i = 0; i < ehdr.e_shnum; i++) {
396 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
402 static char *get_rel_sym_name(EXE_RELOC *rel)
404 return strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
407 static char *get_sym_name(EXE_SYM *sym)
409 return strtab + sym->st_name;
412 /* load an elf object file */
413 int load_object(const char *filename)
416 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
422 fd = open(filename, O_RDONLY);
424 error("can't open file '%s'", filename);
426 /* Read ELF header. */
427 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
428 error("unable to read file header");
430 /* Check ELF identification. */
431 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
432 || ehdr.e_ident[EI_MAG1] != ELFMAG1
433 || ehdr.e_ident[EI_MAG2] != ELFMAG2
434 || ehdr.e_ident[EI_MAG3] != ELFMAG3
435 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
436 error("bad ELF header");
439 do_swap = elf_must_swap(&ehdr);
441 elf_swap_ehdr(&ehdr);
442 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
443 error("Unsupported ELF class");
444 if (ehdr.e_type != ET_REL)
445 error("ELF object file expected");
446 if (ehdr.e_version != EV_CURRENT)
447 error("Invalid ELF version");
448 if (!elf_check_arch(ehdr.e_machine))
449 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
451 /* read section headers */
452 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
454 for(i = 0; i < ehdr.e_shnum; i++) {
455 elf_swap_shdr(&shdr[i]);
459 /* read all section data */
460 sdata = malloc(sizeof(void *) * ehdr.e_shnum);
461 memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
463 for(i = 0;i < ehdr.e_shnum; i++) {
465 if (sec->sh_type != SHT_NOBITS)
466 sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
469 sec = &shdr[ehdr.e_shstrndx];
470 shstr = sdata[ehdr.e_shstrndx];
472 /* swap relocations */
473 for(i = 0; i < ehdr.e_shnum; i++) {
475 if (sec->sh_type == SHT_RELOC) {
476 nb_relocs = sec->sh_size / sec->sh_entsize;
478 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
485 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
487 error("could not find .text section");
488 text_shndx = text_sec - shdr;
489 text = sdata[text_shndx];
491 /* find text relocations, if any */
494 i = find_reloc(text_shndx);
496 relocs = (ELF_RELOC *)sdata[i];
497 nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
500 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
502 error("could not find .symtab section");
503 strtab_sec = &shdr[symtab_sec->sh_link];
505 symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
506 strtab = sdata[symtab_sec->sh_link];
508 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
510 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
511 swab32s(&sym->st_name);
512 swabls(&sym->st_value);
513 swabls(&sym->st_size);
514 swab16s(&sym->st_shndx);
521 #endif /* CONFIG_FORMAT_ELF */
523 #ifdef CONFIG_FORMAT_COFF
526 struct external_scnhdr *shdr;
528 struct external_filehdr fhdr;
529 struct external_syment *coff_symtab;
531 int coff_text_shndx, coff_data_shndx;
535 #define STRTAB_SIZE 4
540 #define T_FUNCTION 0x20
543 void sym_ent_name(struct external_syment *ext_sym, EXE_SYM *sym)
548 if (ext_sym->e.e.e_zeroes != 0) {
550 for(i = 0; i < 8; i++) {
551 c = ext_sym->e.e_name[i];
558 pstrcpy(sym->st_name, sizeof(sym->st_name), strtab + ext_sym->e.e.e_offset);
561 /* now convert the name to a C name (suppress the leading '_') */
562 if (sym->st_name[0] == '_') {
563 len = strlen(sym->st_name);
564 memmove(sym->st_name, sym->st_name + 1, len - 1);
565 sym->st_name[len - 1] = '\0';
569 char *name_for_dotdata(struct coff_rel *rel)
572 struct coff_sym *sym;
575 text_data = *(uint32_t *)(text + rel->r_offset);
577 for (i = 0, sym = symtab; i < nb_syms; i++, sym++) {
578 if (sym->st_syment->e_scnum == data_shndx &&
579 text_data >= sym->st_value &&
580 text_data < sym->st_value + sym->st_size) {
589 static char *get_sym_name(EXE_SYM *sym)
594 static char *get_rel_sym_name(EXE_RELOC *rel)
597 name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx));
598 if (!strcmp(name, ".data"))
599 name = name_for_dotdata(rel);
603 struct external_scnhdr *find_coff_section(struct external_scnhdr *shdr, int shnum, const char *name)
607 struct external_scnhdr *sec;
609 for(i = 0; i < shnum; i++) {
613 shname = sec->s_name;
614 if (!strcmp(shname, name))
620 /* load a coff object file */
621 int load_object(const char *filename)
624 struct external_scnhdr *sec, *text_sec, *data_sec;
626 struct external_syment *ext_sym;
627 struct external_reloc *coff_relocs;
628 struct external_reloc *ext_rel;
633 fd = open(filename, O_RDONLY
639 error("can't open file '%s'", filename);
641 /* Read COFF header. */
642 if (read(fd, &fhdr, sizeof (fhdr)) != sizeof (fhdr))
643 error("unable to read file header");
645 /* Check COFF identification. */
646 if (fhdr.f_magic != I386MAGIC) {
647 error("bad COFF header");
651 /* read section headers */
652 shdr = load_data(fd, sizeof(struct external_filehdr) + fhdr.f_opthdr, fhdr.f_nscns * sizeof(struct external_scnhdr));
654 /* read all section data */
655 sdata = malloc(sizeof(void *) * fhdr.f_nscns);
656 memset(sdata, 0, sizeof(void *) * fhdr.f_nscns);
659 for(i = 0;i < fhdr.f_nscns; i++) {
661 if (!strstart(sec->s_name, ".bss", &p))
662 sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size);
667 text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text");
669 error("could not find .text section");
670 coff_text_shndx = text_sec - shdr;
671 text = sdata[coff_text_shndx];
674 data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data");
676 error("could not find .data section");
677 coff_data_shndx = data_sec - shdr;
679 coff_symtab = load_data(fd, fhdr.f_symptr, fhdr.f_nsyms*SYMESZ);
680 for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
682 printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]);
687 n_strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), STRTAB_SIZE);
688 strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), *n_strtab);
690 nb_syms = fhdr.f_nsyms;
692 for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
693 if (strstart(ext_sym->e.e_name, ".text", NULL))
694 text_shndx = ext_sym->e_scnum;
695 if (strstart(ext_sym->e.e_name, ".data", NULL))
696 data_shndx = ext_sym->e_scnum;
699 /* set coff symbol */
700 symtab = malloc(sizeof(struct coff_sym) * nb_syms);
703 for (i = 0, ext_sym = coff_symtab, sym = symtab; i < nb_syms; i++, ext_sym++, sym++) {
704 memset(sym, 0, sizeof(*sym));
705 sym->st_syment = ext_sym;
706 sym_ent_name(ext_sym, sym);
707 sym->st_value = ext_sym->e_value;
709 aux_size = *(int8_t *)ext_sym->e_numaux;
710 if (ext_sym->e_scnum == text_shndx && ext_sym->e_type == T_FUNCTION) {
711 for (j = aux_size + 1; j < nb_syms - i; j++) {
712 if ((ext_sym + j)->e_scnum == text_shndx &&
713 (ext_sym + j)->e_type == T_FUNCTION ){
714 sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
716 } else if (j == nb_syms - i - 1) {
717 sec = &shdr[coff_text_shndx];
718 sym->st_size = sec->s_size - ext_sym->e_value;
722 } else if (ext_sym->e_scnum == data_shndx && *(uint8_t *)ext_sym->e_sclass == C_EXTERNAL) {
723 for (j = aux_size + 1; j < nb_syms - i; j++) {
724 if ((ext_sym + j)->e_scnum == data_shndx) {
725 sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
727 } else if (j == nb_syms - i - 1) {
728 sec = &shdr[coff_data_shndx];
729 sym->st_size = sec->s_size - ext_sym->e_value;
737 sym->st_type = ext_sym->e_type;
738 sym->st_shndx = ext_sym->e_scnum;
742 /* find text relocations, if any */
743 sec = &shdr[coff_text_shndx];
744 coff_relocs = load_data(fd, sec->s_relptr, sec->s_nreloc*RELSZ);
745 nb_relocs = sec->s_nreloc;
747 /* set coff relocation */
748 relocs = malloc(sizeof(struct coff_rel) * nb_relocs);
749 for (i = 0, ext_rel = coff_relocs, rel = relocs; i < nb_relocs;
750 i++, ext_rel++, rel++) {
751 memset(rel, 0, sizeof(*rel));
752 rel->r_reloc = ext_rel;
753 rel->r_offset = *(uint32_t *)ext_rel->r_vaddr;
754 rel->r_type = *(uint16_t *)ext_rel->r_type;
759 #endif /* CONFIG_FORMAT_COFF */
763 int arm_emit_ldr_info(const char *name, unsigned long start_offset,
764 FILE *outfile, uint8_t *p_start, uint8_t *p_end,
765 ELF_RELOC *relocs, int nb_relocs)
769 int offset, min_offset, pc_offset, data_size;
770 uint8_t data_allocated[1024];
771 unsigned int data_index;
773 memset(data_allocated, 0, sizeof(data_allocated));
776 min_offset = p_end - p_start;
777 while (p < p_start + min_offset) {
778 insn = get32((uint32_t *)p);
779 if ((insn & 0x0d5f0000) == 0x051f0000) {
780 /* ldr reg, [pc, #im] */
781 offset = insn & 0xfff;
782 if (!(insn & 0x00800000))
784 if ((offset & 3) !=0)
785 error("%s:%04x: ldr pc offset must be 32 bit aligned",
786 name, start_offset + p - p_start);
787 pc_offset = p - p_start + offset + 8;
788 if (pc_offset <= (p - p_start) ||
789 pc_offset >= (p_end - p_start))
790 error("%s:%04x: ldr pc offset must point inside the function code",
791 name, start_offset + p - p_start);
792 if (pc_offset < min_offset)
793 min_offset = pc_offset;
796 fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
799 data_index = ((p_end - p_start) - pc_offset - 4) >> 2;
800 fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
802 fprintf(outfile, " arm_ldr_ptr++;\n");
803 if (data_index >= sizeof(data_allocated))
804 error("%s: too many data", name);
805 if (!data_allocated[data_index]) {
808 const char *sym_name, *p;
811 data_allocated[data_index] = 1;
814 addend = get32((uint32_t *)(p_start + pc_offset));
816 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
817 if (rel->r_offset == (pc_offset + start_offset)) {
818 sym_name = get_rel_sym_name(rel);
819 /* the compiler leave some unnecessary references to the code */
820 if (strstart(sym_name, "__op_param", &p)) {
821 snprintf(relname, sizeof(relname), "param%s", p);
823 snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name);
825 type = ELF32_R_TYPE(rel->r_info);
826 if (type != R_ARM_ABS32)
827 error("%s: unsupported data relocation", name);
831 fprintf(outfile, " arm_data_ptr[%d] = 0x%x",
833 if (relname[0] != '\0')
834 fprintf(outfile, " + %s", relname);
835 fprintf(outfile, ";\n");
841 data_size = (p_end - p_start) - min_offset;
842 if (data_size > 0 && outfile) {
843 fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2);
846 /* the last instruction must be a mov pc, lr */
850 insn = get32((uint32_t *)p);
851 if ((insn & 0xffff0000) != 0xe91b0000) {
854 printf("%s: invalid epilog\n", name);
863 /* generate op code */
864 void gen_code(const char *name, host_ulong offset, host_ulong size,
865 FILE *outfile, int gen_switch)
868 uint8_t *p_start, *p_end;
869 host_ulong start_offset;
871 uint8_t args_present[MAX_ARGS];
872 const char *sym_name, *p;
875 /* Compute exact size excluding prologue and epilogue instructions.
876 * Increment start_offset to skip epilogue instructions, then compute
877 * copy_size the indicate the size of the remaining instructions (in
880 p_start = text + offset;
881 p_end = p_start + size;
882 start_offset = offset;
883 #if defined(HOST_I386) || defined(HOST_AMD64)
884 #ifdef CONFIG_FORMAT_COFF
889 error("empty code for %s", name);
893 error("ret or jmp expected at the end of %s", name);
895 copy_size = p - p_start;
900 len = p_end - p_start;
902 error("empty code for %s", name);
903 if (p_end[-1] == 0xc3) {
906 error("ret or jmp expected at the end of %s", name);
911 #elif defined(HOST_PPC)
914 p = (void *)(p_end - 4);
916 error("empty code for %s", name);
917 if (get32((uint32_t *)p) != 0x4e800020)
918 error("blr expected at the end of %s", name);
919 copy_size = p - p_start;
921 #elif defined(HOST_S390)
924 p = (void *)(p_end - 2);
926 error("empty code for %s", name);
927 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
928 error("br %%r14 expected at the end of %s", name);
929 copy_size = p - p_start;
931 #elif defined(HOST_ALPHA)
936 /* XXX: check why it occurs */
938 error("empty code for %s", name);
940 if (get32((uint32_t *)p) != 0x6bfa8001)
941 error("ret expected at the end of %s", name);
942 copy_size = p - p_start;
944 #elif defined(HOST_IA64)
947 p = (void *)(p_end - 4);
949 error("empty code for %s", name);
950 /* br.ret.sptk.many b0;; */
952 if (get32((uint32_t *)p) != 0x00840008)
953 error("br.ret.sptk.many b0;; expected at the end of %s", name);
954 copy_size = p - p_start;
956 #elif defined(HOST_SPARC)
958 uint32_t start_insn, end_insn1, end_insn2;
960 p = (void *)(p_end - 8);
962 error("empty code for %s", name);
963 start_insn = get32((uint32_t *)(p_start + 0x0));
964 end_insn1 = get32((uint32_t *)(p + 0x0));
965 end_insn2 = get32((uint32_t *)(p + 0x4));
966 if ((start_insn & ~0x1fff) == 0x9de3a000) {
969 if ((int)(start_insn | ~0x1fff) < -128)
970 error("Found bogus save at the start of %s", name);
971 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
972 error("ret; restore; not found at end of %s", name);
974 error("No save at the beginning of %s", name);
977 /* Skip a preceeding nop, if present. */
979 skip_insn = get32((uint32_t *)(p - 0x4));
980 if (skip_insn == 0x01000000)
984 copy_size = p - p_start;
986 #elif defined(HOST_SPARC64)
988 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
990 p = (void *)(p_end - 8);
992 error("empty code for %s", name);
993 start_insn = get32((uint32_t *)(p_start + 0x0));
994 end_insn1 = get32((uint32_t *)(p + 0x0));
995 end_insn2 = get32((uint32_t *)(p + 0x4));
996 if ((start_insn & ~0x1fff) == 0x9de3a000) {
999 if ((int)(start_insn | ~0x1fff) < -256)
1000 error("Found bogus save at the start of %s", name);
1001 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
1002 error("ret; restore; not found at end of %s", name);
1004 error("No save at the beginning of %s", name);
1007 /* Skip a preceeding nop, if present. */
1009 skip_insn = get32((uint32_t *)(p - 0x4));
1010 if (skip_insn == 0x01000000)
1014 copy_size = p - p_start;
1016 #elif defined(HOST_ARM)
1018 if ((p_end - p_start) <= 16)
1019 error("%s: function too small", name);
1020 if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
1021 (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
1022 get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
1023 error("%s: invalid prolog", name);
1026 copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end,
1029 #elif defined(HOST_M68K)
1032 p = (void *)(p_end - 2);
1034 error("empty code for %s", name);
1035 // remove NOP's, probably added for alignment
1036 while ((get16((uint16_t *)p) == 0x4e71) &&
1039 if (get16((uint16_t *)p) != 0x4e75)
1040 error("rts expected at the end of %s", name);
1041 copy_size = p - p_start;
1044 #error unsupported CPU
1047 /* compute the number of arguments by looking at the relocations */
1048 for(i = 0;i < MAX_ARGS; i++)
1049 args_present[i] = 0;
1051 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1052 if (rel->r_offset >= start_offset &&
1053 rel->r_offset < start_offset + (p_end - p_start)) {
1054 sym_name = get_rel_sym_name(rel);
1055 if (strstart(sym_name, "__op_param", &p)) {
1056 n = strtoul(p, NULL, 10);
1058 error("too many arguments in %s", name);
1059 args_present[n - 1] = 1;
1065 while (nb_args < MAX_ARGS && args_present[nb_args])
1067 for(i = nb_args; i < MAX_ARGS; i++) {
1068 if (args_present[i])
1069 error("inconsistent argument numbering in %s", name);
1072 if (gen_switch == 2) {
1073 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
1074 } else if (gen_switch == 1) {
1077 fprintf(outfile, "case INDEX_%s: {\n", name);
1079 fprintf(outfile, " long ");
1080 for(i = 0; i < nb_args; i++) {
1082 fprintf(outfile, ", ");
1083 fprintf(outfile, "param%d", i + 1);
1085 fprintf(outfile, ";\n");
1087 fprintf(outfile, " extern void %s();\n", name);
1089 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1090 if (rel->r_offset >= start_offset &&
1091 rel->r_offset < start_offset + (p_end - p_start)) {
1092 sym_name = get_rel_sym_name(rel);
1094 !strstart(sym_name, "__op_param", NULL) &&
1095 !strstart(sym_name, "__op_jmp", NULL)) {
1096 #if defined(HOST_SPARC)
1097 if (sym_name[0] == '.') {
1099 "extern char __dot_%s __asm__(\"%s\");\n",
1100 sym_name+1, sym_name);
1104 fprintf(outfile, "extern char %s;\n", sym_name);
1109 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
1111 /* emit code offset information */
1114 const char *sym_name, *p;
1118 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1119 sym_name = get_sym_name(sym);
1120 if (strstart(sym_name, "__op_label", &p)) {
1122 unsigned long offset;
1124 /* test if the variable refers to a label inside
1125 the code we are generating */
1126 #ifdef CONFIG_FORMAT_COFF
1127 if (sym->st_shndx == text_shndx) {
1128 ptr = sdata[coff_text_shndx];
1129 } else if (sym->st_shndx == data_shndx) {
1130 ptr = sdata[coff_data_shndx];
1135 ptr = sdata[sym->st_shndx];
1138 error("__op_labelN in invalid section");
1139 offset = sym->st_value;
1140 val = *(unsigned long *)(ptr + offset);
1141 #ifdef ELF_USES_RELOCA
1143 int reloc_shndx, nb_relocs1, j;
1145 /* try to find a matching relocation */
1146 reloc_shndx = find_reloc(sym->st_shndx);
1148 nb_relocs1 = shdr[reloc_shndx].sh_size /
1149 shdr[reloc_shndx].sh_entsize;
1150 rel = (ELF_RELOC *)sdata[reloc_shndx];
1151 for(j = 0; j < nb_relocs1; j++) {
1152 if (rel->r_offset == offset) {
1153 val = rel->r_addend;
1162 if (val >= start_offset && val < start_offset + copy_size) {
1163 n = strtol(p, NULL, 10);
1164 fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
1170 /* load parameres in variables */
1171 for(i = 0; i < nb_args; i++) {
1172 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
1175 /* patch relocations */
1176 #if defined(HOST_I386)
1181 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1182 if (rel->r_offset >= start_offset &&
1183 rel->r_offset < start_offset + copy_size) {
1184 sym_name = get_rel_sym_name(rel);
1185 if (strstart(sym_name, "__op_jmp", &p)) {
1187 n = strtol(p, NULL, 10);
1188 /* __op_jmp relocations are done at
1189 runtime to do translated block
1190 chaining: the offset of the instruction
1191 needs to be stored */
1192 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1193 n, rel->r_offset - start_offset);
1197 if (strstart(sym_name, "__op_param", &p)) {
1198 snprintf(name, sizeof(name), "param%s", p);
1200 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1202 addend = get32((uint32_t *)(text + rel->r_offset));
1203 #ifdef CONFIG_FORMAT_ELF
1204 type = ELF32_R_TYPE(rel->r_info);
1207 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1208 rel->r_offset - start_offset, name, addend);
1211 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1212 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1215 error("unsupported i386 relocation (%d)", type);
1217 #elif defined(CONFIG_FORMAT_COFF)
1221 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1222 rel->r_offset - start_offset, name, addend);
1225 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n",
1226 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1229 error("unsupported i386 relocation (%d)", type);
1232 #error unsupport object format
1237 #elif defined(HOST_AMD64)
1242 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1243 if (rel->r_offset >= start_offset &&
1244 rel->r_offset < start_offset + copy_size) {
1245 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1246 if (strstart(sym_name, "__op_param", &p)) {
1247 snprintf(name, sizeof(name), "param%s", p);
1249 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1251 type = ELF32_R_TYPE(rel->r_info);
1252 addend = rel->r_addend;
1255 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
1256 rel->r_offset - start_offset, name, addend);
1259 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
1260 rel->r_offset - start_offset, name, addend);
1263 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1264 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1267 error("unsupported AMD64 relocation (%d)", type);
1272 #elif defined(HOST_PPC)
1277 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1278 if (rel->r_offset >= start_offset &&
1279 rel->r_offset < start_offset + copy_size) {
1280 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1281 if (strstart(sym_name, "__op_jmp", &p)) {
1283 n = strtol(p, NULL, 10);
1284 /* __op_jmp relocations are done at
1285 runtime to do translated block
1286 chaining: the offset of the instruction
1287 needs to be stored */
1288 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1289 n, rel->r_offset - start_offset);
1293 if (strstart(sym_name, "__op_param", &p)) {
1294 snprintf(name, sizeof(name), "param%s", p);
1296 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1298 type = ELF32_R_TYPE(rel->r_info);
1299 addend = rel->r_addend;
1302 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1303 rel->r_offset - start_offset, name, addend);
1305 case R_PPC_ADDR16_LO:
1306 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
1307 rel->r_offset - start_offset, name, addend);
1309 case R_PPC_ADDR16_HI:
1310 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
1311 rel->r_offset - start_offset, name, addend);
1313 case R_PPC_ADDR16_HA:
1314 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
1315 rel->r_offset - start_offset, name, addend);
1318 /* warning: must be at 32 MB distancy */
1319 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
1320 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1323 error("unsupported powerpc relocation (%d)", type);
1328 #elif defined(HOST_S390)
1333 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1334 if (rel->r_offset >= start_offset &&
1335 rel->r_offset < start_offset + copy_size) {
1336 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1337 if (strstart(sym_name, "__op_param", &p)) {
1338 snprintf(name, sizeof(name), "param%s", p);
1340 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1342 type = ELF32_R_TYPE(rel->r_info);
1343 addend = rel->r_addend;
1346 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1347 rel->r_offset - start_offset, name, addend);
1350 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
1351 rel->r_offset - start_offset, name, addend);
1354 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
1355 rel->r_offset - start_offset, name, addend);
1358 error("unsupported s390 relocation (%d)", type);
1363 #elif defined(HOST_ALPHA)
1365 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
1366 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
1369 type = ELF64_R_TYPE(rel->r_info);
1370 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
1372 case R_ALPHA_GPDISP:
1373 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
1374 as an immediate instead of constructing it from the pv or ra. */
1375 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
1376 rel->r_offset - start_offset);
1377 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
1378 rel->r_offset - start_offset + rel->r_addend);
1380 case R_ALPHA_LITUSE:
1381 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
1382 now, since some called functions (libc) need pv to be set up. */
1385 /* Branch target prediction hint. Ignore for now. Should be already
1386 correct for in-function jumps. */
1388 case R_ALPHA_LITERAL:
1389 /* Load a literal from the GOT relative to the gp. Since there's only a
1390 single gp, nothing is to be done. */
1392 case R_ALPHA_GPRELHIGH:
1393 /* Handle fake relocations against __op_param symbol. Need to emit the
1394 high part of the immediate value instead. Other symbols need no
1395 special treatment. */
1396 if (strstart(sym_name, "__op_param", &p))
1397 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
1398 rel->r_offset - start_offset, p);
1400 case R_ALPHA_GPRELLOW:
1401 if (strstart(sym_name, "__op_param", &p))
1402 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
1403 rel->r_offset - start_offset, p);
1406 /* PC-relative jump. Tweak offset to skip the two instructions that try to
1407 set up the gp from the pv. */
1408 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
1409 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
1412 error("unsupported Alpha relocation (%d)", type);
1417 #elif defined(HOST_IA64)
1422 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1423 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
1424 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
1425 if (strstart(sym_name, "__op_param", &p)) {
1426 snprintf(name, sizeof(name), "param%s", p);
1428 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1430 type = ELF64_R_TYPE(rel->r_info);
1431 addend = rel->r_addend;
1433 case R_IA64_LTOFF22:
1434 error("must implemnt R_IA64_LTOFF22 relocation");
1435 case R_IA64_PCREL21B:
1436 error("must implemnt R_IA64_PCREL21B relocation");
1438 error("unsupported ia64 relocation (%d)", type);
1443 #elif defined(HOST_SPARC)
1448 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1449 if (rel->r_offset >= start_offset &&
1450 rel->r_offset < start_offset + copy_size) {
1451 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
1452 if (strstart(sym_name, "__op_param", &p)) {
1453 snprintf(name, sizeof(name), "param%s", p);
1455 if (sym_name[0] == '.')
1456 snprintf(name, sizeof(name),
1457 "(long)(&__dot_%s)",
1460 snprintf(name, sizeof(name),
1461 "(long)(&%s)", sym_name);
1463 type = ELF32_R_TYPE(rel->r_info);
1464 addend = rel->r_addend;
1467 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1468 rel->r_offset - start_offset, name, addend);
1472 " *(uint32_t *)(gen_code_ptr + %d) = "
1473 "((*(uint32_t *)(gen_code_ptr + %d)) "
1475 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1476 rel->r_offset - start_offset,
1477 rel->r_offset - start_offset,
1482 " *(uint32_t *)(gen_code_ptr + %d) = "
1483 "((*(uint32_t *)(gen_code_ptr + %d)) "
1485 " | ((%s + %d) & 0x3ff);\n",
1486 rel->r_offset - start_offset,
1487 rel->r_offset - start_offset,
1490 case R_SPARC_WDISP30:
1492 " *(uint32_t *)(gen_code_ptr + %d) = "
1493 "((*(uint32_t *)(gen_code_ptr + %d)) "
1495 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1496 " & 0x3fffffff);\n",
1497 rel->r_offset - start_offset,
1498 rel->r_offset - start_offset,
1500 rel->r_offset - start_offset);
1503 error("unsupported sparc relocation (%d)", type);
1508 #elif defined(HOST_SPARC64)
1513 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1514 if (rel->r_offset >= start_offset &&
1515 rel->r_offset < start_offset + copy_size) {
1516 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
1517 if (strstart(sym_name, "__op_param", &p)) {
1518 snprintf(name, sizeof(name), "param%s", p);
1520 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1522 type = ELF64_R_TYPE(rel->r_info);
1523 addend = rel->r_addend;
1526 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1527 rel->r_offset - start_offset, name, addend);
1531 " *(uint32_t *)(gen_code_ptr + %d) = "
1532 "((*(uint32_t *)(gen_code_ptr + %d)) "
1534 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1535 rel->r_offset - start_offset,
1536 rel->r_offset - start_offset,
1541 " *(uint32_t *)(gen_code_ptr + %d) = "
1542 "((*(uint32_t *)(gen_code_ptr + %d)) "
1544 " | ((%s + %d) & 0x3ff);\n",
1545 rel->r_offset - start_offset,
1546 rel->r_offset - start_offset,
1549 case R_SPARC_WDISP30:
1551 " *(uint32_t *)(gen_code_ptr + %d) = "
1552 "((*(uint32_t *)(gen_code_ptr + %d)) "
1554 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1555 " & 0x3fffffff);\n",
1556 rel->r_offset - start_offset,
1557 rel->r_offset - start_offset,
1559 rel->r_offset - start_offset);
1562 error("unsupported sparc64 relocation (%d)", type);
1567 #elif defined(HOST_ARM)
1573 arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
1576 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1577 if (rel->r_offset >= start_offset &&
1578 rel->r_offset < start_offset + copy_size) {
1579 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1580 /* the compiler leave some unnecessary references to the code */
1581 if (sym_name[0] == '\0')
1583 if (strstart(sym_name, "__op_param", &p)) {
1584 snprintf(name, sizeof(name), "param%s", p);
1586 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1588 type = ELF32_R_TYPE(rel->r_info);
1589 addend = get32((uint32_t *)(text + rel->r_offset));
1592 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1593 rel->r_offset - start_offset, name, addend);
1596 fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
1597 rel->r_offset - start_offset, addend, name);
1600 error("unsupported arm relocation (%d)", type);
1605 #elif defined(HOST_M68K)
1611 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1612 if (rel->r_offset >= start_offset &&
1613 rel->r_offset < start_offset + copy_size) {
1614 sym = &(symtab[ELFW(R_SYM)(rel->r_info)]);
1615 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1616 if (strstart(sym_name, "__op_param", &p)) {
1617 snprintf(name, sizeof(name), "param%s", p);
1619 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1621 type = ELF32_R_TYPE(rel->r_info);
1622 addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend;
1625 fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ;
1626 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
1627 rel->r_offset - start_offset, name, addend );
1630 fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset);
1631 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
1632 rel->r_offset - start_offset, name, rel->r_offset - start_offset, /*sym->st_value+*/ addend);
1635 error("unsupported m68k relocation (%d)", type);
1641 #error unsupported CPU
1643 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
1644 fprintf(outfile, "}\n");
1645 fprintf(outfile, "break;\n\n");
1647 fprintf(outfile, "static inline void gen_%s(", name);
1649 fprintf(outfile, "void");
1651 for(i = 0; i < nb_args; i++) {
1653 fprintf(outfile, ", ");
1654 fprintf(outfile, "long param%d", i + 1);
1657 fprintf(outfile, ")\n");
1658 fprintf(outfile, "{\n");
1659 for(i = 0; i < nb_args; i++) {
1660 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
1662 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
1663 fprintf(outfile, "}\n\n");
1667 int gen_file(FILE *outfile, int out_type)
1672 if (out_type == OUT_INDEX_OP) {
1673 fprintf(outfile, "DEF(end, 0, 0)\n");
1674 fprintf(outfile, "DEF(nop, 0, 0)\n");
1675 fprintf(outfile, "DEF(nop1, 1, 0)\n");
1676 fprintf(outfile, "DEF(nop2, 2, 0)\n");
1677 fprintf(outfile, "DEF(nop3, 3, 0)\n");
1678 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1679 const char *name, *p;
1680 name = get_sym_name(sym);
1681 if (strstart(name, OP_PREFIX, &p)) {
1682 gen_code(name, sym->st_value, sym->st_size, outfile, 2);
1685 } else if (out_type == OUT_GEN_OP) {
1686 /* generate gen_xxx functions */
1688 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1690 name = get_sym_name(sym);
1691 if (strstart(name, OP_PREFIX, NULL)) {
1692 if (sym->st_shndx != text_shndx)
1693 error("invalid section for opcode (0x%x)", sym->st_shndx);
1694 gen_code(name, sym->st_value, sym->st_size, outfile, 0);
1699 /* generate big code generation switch */
1701 "int dyngen_code(uint8_t *gen_code_buf,\n"
1702 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1703 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1705 " uint8_t *gen_code_ptr;\n"
1706 " const uint16_t *opc_ptr;\n"
1707 " const uint32_t *opparam_ptr;\n");
1711 " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1712 " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1713 " uint32_t *arm_data_ptr = arm_data_table;\n");
1718 " gen_code_ptr = gen_code_buf;\n"
1719 " opc_ptr = opc_buf;\n"
1720 " opparam_ptr = opparam_buf;\n");
1722 /* Generate prologue, if needed. */
1726 " switch(*opc_ptr++) {\n"
1729 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1731 name = get_sym_name(sym);
1732 if (strstart(name, OP_PREFIX, NULL)) {
1734 printf("%4d: %s pos=0x%08x len=%d\n",
1735 i, name, sym->st_value, sym->st_size);
1737 if (sym->st_shndx != text_shndx)
1738 error("invalid section for opcode (0x%x)", sym->st_shndx);
1739 gen_code(name, sym->st_value, sym->st_size, outfile, 1);
1744 " case INDEX_op_nop:\n"
1746 " case INDEX_op_nop1:\n"
1749 " case INDEX_op_nop2:\n"
1750 " opparam_ptr += 2;\n"
1752 " case INDEX_op_nop3:\n"
1753 " opparam_ptr += 3;\n"
1760 /* generate constant table if needed */
1762 " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1763 " gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1764 " last_gen_code_ptr = gen_code_ptr;\n"
1765 " arm_ldr_ptr = arm_ldr_table;\n"
1766 " arm_data_ptr = arm_data_table;\n"
1776 /* generate some code patching */
1778 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");
1780 /* flush instruction cache */
1781 fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
1783 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
1784 fprintf(outfile, "}\n\n");
1793 printf("dyngen (c) 2003 Fabrice Bellard\n"
1794 "usage: dyngen [-o outfile] [-c] objfile\n"
1795 "Generate a dynamic code generator from an object file\n"
1796 "-c output enum of operations\n"
1797 "-g output gen_op_xx() functions\n"
1802 int main(int argc, char **argv)
1805 const char *filename, *outfilename;
1808 outfilename = "out.c";
1809 out_type = OUT_CODE;
1811 c = getopt(argc, argv, "ho:cg");
1819 outfilename = optarg;
1822 out_type = OUT_INDEX_OP;
1825 out_type = OUT_GEN_OP;
1831 filename = argv[optind];
1832 outfile = fopen(outfilename, "w");
1834 error("could not open '%s'", outfilename);
1836 load_object(filename);
1837 gen_file(outfile, out_type);