]>
git.proxmox.com Git - mirror_qemu.git/blob - dyngen.c
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"
34 #define CONFIG_FORMAT_COFF
36 #define CONFIG_FORMAT_ELF
39 #ifdef CONFIG_FORMAT_ELF
41 /* elf format definitions. We use these macros to test the CPU to
42 allow cross compilation (this tool must be ran on the build
44 #if defined(HOST_I386)
46 #define ELF_CLASS ELFCLASS32
47 #define ELF_ARCH EM_386
48 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
49 #undef ELF_USES_RELOCA
51 #elif defined(HOST_AMD64)
53 #define ELF_CLASS ELFCLASS64
54 #define ELF_ARCH EM_X86_64
55 #define elf_check_arch(x) ((x) == EM_X86_64)
56 #define ELF_USES_RELOCA
58 #elif defined(HOST_PPC)
60 #define ELF_CLASS ELFCLASS32
61 #define ELF_ARCH EM_PPC
62 #define elf_check_arch(x) ((x) == EM_PPC)
63 #define ELF_USES_RELOCA
65 #elif defined(HOST_S390)
67 #define ELF_CLASS ELFCLASS32
68 #define ELF_ARCH EM_S390
69 #define elf_check_arch(x) ((x) == EM_S390)
70 #define ELF_USES_RELOCA
72 #elif defined(HOST_ALPHA)
74 #define ELF_CLASS ELFCLASS64
75 #define ELF_ARCH EM_ALPHA
76 #define elf_check_arch(x) ((x) == EM_ALPHA)
77 #define ELF_USES_RELOCA
79 #elif defined(HOST_IA64)
81 #define ELF_CLASS ELFCLASS64
82 #define ELF_ARCH EM_IA_64
83 #define elf_check_arch(x) ((x) == EM_IA_64)
84 #define ELF_USES_RELOCA
86 #elif defined(HOST_SPARC)
88 #define ELF_CLASS ELFCLASS32
89 #define ELF_ARCH EM_SPARC
90 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
91 #define ELF_USES_RELOCA
93 #elif defined(HOST_SPARC64)
95 #define ELF_CLASS ELFCLASS64
96 #define ELF_ARCH EM_SPARCV9
97 #define elf_check_arch(x) ((x) == EM_SPARCV9)
98 #define ELF_USES_RELOCA
100 #elif defined(HOST_ARM)
102 #define ELF_CLASS ELFCLASS32
103 #define ELF_ARCH EM_ARM
104 #define elf_check_arch(x) ((x) == EM_ARM)
105 #define ELF_USES_RELOC
107 #elif defined(HOST_M68K)
109 #define ELF_CLASS ELFCLASS32
110 #define ELF_ARCH EM_68K
111 #define elf_check_arch(x) ((x) == EM_68K)
112 #define ELF_USES_RELOCA
115 #error unsupported CPU - please update the code
120 #if ELF_CLASS == ELFCLASS32
121 typedef int32_t host_long
;
122 typedef uint32_t host_ulong
;
123 #define swabls(x) swab32s(x)
125 typedef int64_t host_long
;
126 typedef uint64_t host_ulong
;
127 #define swabls(x) swab64s(x)
130 #ifdef ELF_USES_RELOCA
131 #define SHT_RELOC SHT_RELA
133 #define SHT_RELOC SHT_REL
136 #define EXE_RELOC ELF_RELOC
137 #define EXE_SYM ElfW(Sym)
139 #endif /* CONFIG_FORMAT_ELF */
141 #ifdef CONFIG_FORMAT_COFF
145 typedef int32_t host_long
;
146 typedef uint32_t host_ulong
;
148 #define FILENAMELEN 256
150 typedef struct coff_sym
{
151 struct external_syment
*st_syment
;
152 char st_name
[FILENAMELEN
];
159 typedef struct coff_rel
{
160 struct external_reloc
*r_reloc
;
165 #define EXE_RELOC struct coff_rel
166 #define EXE_SYM struct coff_sym
168 #endif /* CONFIG_FORMAT_COFF */
178 /* all dynamically generated functions begin with this code */
179 #define OP_PREFIX "op_"
183 void __attribute__((noreturn
)) __attribute__((format (printf
, 1, 2))) error(const char *fmt
, ...)
187 fprintf(stderr
, "dyngen: ");
188 vfprintf(stderr
, fmt
, ap
);
189 fprintf(stderr
, "\n");
194 void *load_data(int fd
, long offset
, unsigned int size
)
201 lseek(fd
, offset
, SEEK_SET
);
202 if (read(fd
, data
, size
) != size
) {
209 int strstart(const char *str
, const char *val
, const char **ptr
)
225 void pstrcpy(char *buf
, int buf_size
, const char *str
)
235 if (c
== 0 || q
>= buf
+ buf_size
- 1)
242 void swab16s(uint16_t *p
)
247 void swab32s(uint32_t *p
)
252 void swab64s(uint64_t *p
)
257 uint16_t get16(uint16_t *p
)
266 uint32_t get32(uint32_t *p
)
275 void put16(uint16_t *p
, uint16_t val
)
282 void put32(uint32_t *p
, uint32_t val
)
289 /* executable information */
297 #ifdef CONFIG_FORMAT_ELF
300 struct elf_shdr
*shdr
;
305 int elf_must_swap(struct elfhdr
*h
)
313 return (h
->e_ident
[EI_DATA
] == ELFDATA2MSB
) !=
314 (swaptest
.b
[0] == 0);
317 void elf_swap_ehdr(struct elfhdr
*h
)
319 swab16s(&h
->e_type
); /* Object file type */
320 swab16s(&h
-> e_machine
); /* Architecture */
321 swab32s(&h
-> e_version
); /* Object file version */
322 swabls(&h
-> e_entry
); /* Entry point virtual address */
323 swabls(&h
-> e_phoff
); /* Program header table file offset */
324 swabls(&h
-> e_shoff
); /* Section header table file offset */
325 swab32s(&h
-> e_flags
); /* Processor-specific flags */
326 swab16s(&h
-> e_ehsize
); /* ELF header size in bytes */
327 swab16s(&h
-> e_phentsize
); /* Program header table entry size */
328 swab16s(&h
-> e_phnum
); /* Program header table entry count */
329 swab16s(&h
-> e_shentsize
); /* Section header table entry size */
330 swab16s(&h
-> e_shnum
); /* Section header table entry count */
331 swab16s(&h
-> e_shstrndx
); /* Section header string table index */
334 void elf_swap_shdr(struct elf_shdr
*h
)
336 swab32s(&h
-> sh_name
); /* Section name (string tbl index) */
337 swab32s(&h
-> sh_type
); /* Section type */
338 swabls(&h
-> sh_flags
); /* Section flags */
339 swabls(&h
-> sh_addr
); /* Section virtual addr at execution */
340 swabls(&h
-> sh_offset
); /* Section file offset */
341 swabls(&h
-> sh_size
); /* Section size in bytes */
342 swab32s(&h
-> sh_link
); /* Link to another section */
343 swab32s(&h
-> sh_info
); /* Additional section information */
344 swabls(&h
-> sh_addralign
); /* Section alignment */
345 swabls(&h
-> sh_entsize
); /* Entry size if section holds table */
348 void elf_swap_phdr(struct elf_phdr
*h
)
350 swab32s(&h
->p_type
); /* Segment type */
351 swabls(&h
->p_offset
); /* Segment file offset */
352 swabls(&h
->p_vaddr
); /* Segment virtual address */
353 swabls(&h
->p_paddr
); /* Segment physical address */
354 swabls(&h
->p_filesz
); /* Segment size in file */
355 swabls(&h
->p_memsz
); /* Segment size in memory */
356 swab32s(&h
->p_flags
); /* Segment flags */
357 swabls(&h
->p_align
); /* Segment alignment */
360 void elf_swap_rel(ELF_RELOC
*rel
)
362 swabls(&rel
->r_offset
);
363 swabls(&rel
->r_info
);
364 #ifdef ELF_USES_RELOCA
365 swabls(&rel
->r_addend
);
369 struct elf_shdr
*find_elf_section(struct elf_shdr
*shdr
, int shnum
, const char *shstr
,
374 struct elf_shdr
*sec
;
376 for(i
= 0; i
< shnum
; i
++) {
380 shname
= shstr
+ sec
->sh_name
;
381 if (!strcmp(shname
, name
))
387 int find_reloc(int sh_index
)
389 struct elf_shdr
*sec
;
392 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
394 if (sec
->sh_type
== SHT_RELOC
&& sec
->sh_info
== sh_index
)
400 static char *get_rel_sym_name(EXE_RELOC
*rel
)
402 return strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
405 static char *get_sym_name(EXE_SYM
*sym
)
407 return strtab
+ sym
->st_name
;
410 /* load an elf object file */
411 int load_object(const char *filename
)
414 struct elf_shdr
*sec
, *symtab_sec
, *strtab_sec
, *text_sec
;
420 fd
= open(filename
, O_RDONLY
);
422 error("can't open file '%s'", filename
);
424 /* Read ELF header. */
425 if (read(fd
, &ehdr
, sizeof (ehdr
)) != sizeof (ehdr
))
426 error("unable to read file header");
428 /* Check ELF identification. */
429 if (ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
430 || ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
431 || ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
432 || ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
433 || ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
434 error("bad ELF header");
437 do_swap
= elf_must_swap(&ehdr
);
439 elf_swap_ehdr(&ehdr
);
440 if (ehdr
.e_ident
[EI_CLASS
] != ELF_CLASS
)
441 error("Unsupported ELF class");
442 if (ehdr
.e_type
!= ET_REL
)
443 error("ELF object file expected");
444 if (ehdr
.e_version
!= EV_CURRENT
)
445 error("Invalid ELF version");
446 if (!elf_check_arch(ehdr
.e_machine
))
447 error("Unsupported CPU (e_machine=%d)", ehdr
.e_machine
);
449 /* read section headers */
450 shdr
= load_data(fd
, ehdr
.e_shoff
, ehdr
.e_shnum
* sizeof(struct elf_shdr
));
452 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
453 elf_swap_shdr(&shdr
[i
]);
457 /* read all section data */
458 sdata
= malloc(sizeof(void *) * ehdr
.e_shnum
);
459 memset(sdata
, 0, sizeof(void *) * ehdr
.e_shnum
);
461 for(i
= 0;i
< ehdr
.e_shnum
; i
++) {
463 if (sec
->sh_type
!= SHT_NOBITS
)
464 sdata
[i
] = load_data(fd
, sec
->sh_offset
, sec
->sh_size
);
467 sec
= &shdr
[ehdr
.e_shstrndx
];
468 shstr
= sdata
[ehdr
.e_shstrndx
];
470 /* swap relocations */
471 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
473 if (sec
->sh_type
== SHT_RELOC
) {
474 nb_relocs
= sec
->sh_size
/ sec
->sh_entsize
;
476 for(j
= 0, rel
= (ELF_RELOC
*)sdata
[i
]; j
< nb_relocs
; j
++, rel
++)
483 text_sec
= find_elf_section(shdr
, ehdr
.e_shnum
, shstr
, ".text");
485 error("could not find .text section");
486 text_shndx
= text_sec
- shdr
;
487 text
= sdata
[text_shndx
];
489 /* find text relocations, if any */
492 i
= find_reloc(text_shndx
);
494 relocs
= (ELF_RELOC
*)sdata
[i
];
495 nb_relocs
= shdr
[i
].sh_size
/ shdr
[i
].sh_entsize
;
498 symtab_sec
= find_elf_section(shdr
, ehdr
.e_shnum
, shstr
, ".symtab");
500 error("could not find .symtab section");
501 strtab_sec
= &shdr
[symtab_sec
->sh_link
];
503 symtab
= (ElfW(Sym
) *)sdata
[symtab_sec
- shdr
];
504 strtab
= sdata
[symtab_sec
->sh_link
];
506 nb_syms
= symtab_sec
->sh_size
/ sizeof(ElfW(Sym
));
508 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
509 swab32s(&sym
->st_name
);
510 swabls(&sym
->st_value
);
511 swabls(&sym
->st_size
);
512 swab16s(&sym
->st_shndx
);
519 #endif /* CONFIG_FORMAT_ELF */
521 #ifdef CONFIG_FORMAT_COFF
524 struct external_scnhdr
*shdr
;
526 struct external_filehdr fhdr
;
527 struct external_syment
*coff_symtab
;
529 int coff_text_shndx
, coff_data_shndx
;
533 #define STRTAB_SIZE 4
538 #define T_FUNCTION 0x20
541 void sym_ent_name(struct external_syment
*ext_sym
, EXE_SYM
*sym
)
546 if (ext_sym
->e
.e
.e_zeroes
!= 0) {
548 for(i
= 0; i
< 8; i
++) {
549 c
= ext_sym
->e
.e_name
[i
];
556 pstrcpy(sym
->st_name
, sizeof(sym
->st_name
), strtab
+ ext_sym
->e
.e
.e_offset
);
559 /* now convert the name to a C name (suppress the leading '_') */
560 if (sym
->st_name
[0] == '_') {
561 len
= strlen(sym
->st_name
);
562 memmove(sym
->st_name
, sym
->st_name
+ 1, len
- 1);
563 sym
->st_name
[len
- 1] = '\0';
567 char *name_for_dotdata(struct coff_rel
*rel
)
570 struct coff_sym
*sym
;
573 text_data
= *(uint32_t *)(text
+ rel
->r_offset
);
575 for (i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
576 if (sym
->st_syment
->e_scnum
== data_shndx
&&
577 text_data
>= sym
->st_value
&&
578 text_data
< sym
->st_value
+ sym
->st_size
) {
587 static char *get_sym_name(EXE_SYM
*sym
)
592 static char *get_rel_sym_name(EXE_RELOC
*rel
)
595 name
= get_sym_name(symtab
+ *(uint32_t *)(rel
->r_reloc
->r_symndx
));
596 if (!strcmp(name
, ".data"))
597 name
= name_for_dotdata(rel
);
601 struct external_scnhdr
*find_coff_section(struct external_scnhdr
*shdr
, int shnum
, const char *name
)
605 struct external_scnhdr
*sec
;
607 for(i
= 0; i
< shnum
; i
++) {
611 shname
= sec
->s_name
;
612 if (!strcmp(shname
, name
))
618 /* load a coff object file */
619 int load_object(const char *filename
)
622 struct external_scnhdr
*sec
, *text_sec
, *data_sec
;
624 struct external_syment
*ext_sym
;
625 struct external_reloc
*coff_relocs
;
626 struct external_reloc
*ext_rel
;
631 fd
= open(filename
, O_RDONLY
637 error("can't open file '%s'", filename
);
639 /* Read COFF header. */
640 if (read(fd
, &fhdr
, sizeof (fhdr
)) != sizeof (fhdr
))
641 error("unable to read file header");
643 /* Check COFF identification. */
644 if (fhdr
.f_magic
!= I386MAGIC
) {
645 error("bad COFF header");
649 /* read section headers */
650 shdr
= load_data(fd
, sizeof(struct external_filehdr
) + fhdr
.f_opthdr
, fhdr
.f_nscns
* sizeof(struct external_scnhdr
));
652 /* read all section data */
653 sdata
= malloc(sizeof(void *) * fhdr
.f_nscns
);
654 memset(sdata
, 0, sizeof(void *) * fhdr
.f_nscns
);
657 for(i
= 0;i
< fhdr
.f_nscns
; i
++) {
659 if (!strstart(sec
->s_name
, ".bss", &p
))
660 sdata
[i
] = load_data(fd
, sec
->s_scnptr
, sec
->s_size
);
665 text_sec
= find_coff_section(shdr
, fhdr
.f_nscns
, ".text");
667 error("could not find .text section");
668 coff_text_shndx
= text_sec
- shdr
;
669 text
= sdata
[coff_text_shndx
];
672 data_sec
= find_coff_section(shdr
, fhdr
.f_nscns
, ".data");
674 error("could not find .data section");
675 coff_data_shndx
= data_sec
- shdr
;
677 coff_symtab
= load_data(fd
, fhdr
.f_symptr
, fhdr
.f_nsyms
*SYMESZ
);
678 for (i
= 0, ext_sym
= coff_symtab
; i
< nb_syms
; i
++, ext_sym
++) {
680 printf(" %02x", ((uint8_t *)ext_sym
->e
.e_name
)[i
]);
685 n_strtab
= load_data(fd
, (fhdr
.f_symptr
+ fhdr
.f_nsyms
*SYMESZ
), STRTAB_SIZE
);
686 strtab
= load_data(fd
, (fhdr
.f_symptr
+ fhdr
.f_nsyms
*SYMESZ
), *n_strtab
);
688 nb_syms
= fhdr
.f_nsyms
;
690 for (i
= 0, ext_sym
= coff_symtab
; i
< nb_syms
; i
++, ext_sym
++) {
691 if (strstart(ext_sym
->e
.e_name
, ".text", NULL
))
692 text_shndx
= ext_sym
->e_scnum
;
693 if (strstart(ext_sym
->e
.e_name
, ".data", NULL
))
694 data_shndx
= ext_sym
->e_scnum
;
697 /* set coff symbol */
698 symtab
= malloc(sizeof(struct coff_sym
) * nb_syms
);
701 for (i
= 0, ext_sym
= coff_symtab
, sym
= symtab
; i
< nb_syms
; i
++, ext_sym
++, sym
++) {
702 memset(sym
, 0, sizeof(*sym
));
703 sym
->st_syment
= ext_sym
;
704 sym_ent_name(ext_sym
, sym
);
705 sym
->st_value
= ext_sym
->e_value
;
707 aux_size
= *(int8_t *)ext_sym
->e_numaux
;
708 if (ext_sym
->e_scnum
== text_shndx
&& ext_sym
->e_type
== T_FUNCTION
) {
709 for (j
= aux_size
+ 1; j
< nb_syms
- i
; j
++) {
710 if ((ext_sym
+ j
)->e_scnum
== text_shndx
&&
711 (ext_sym
+ j
)->e_type
== T_FUNCTION
){
712 sym
->st_size
= (ext_sym
+ j
)->e_value
- ext_sym
->e_value
;
714 } else if (j
== nb_syms
- i
- 1) {
715 sec
= &shdr
[coff_text_shndx
];
716 sym
->st_size
= sec
->s_size
- ext_sym
->e_value
;
720 } else if (ext_sym
->e_scnum
== data_shndx
&& *(uint8_t *)ext_sym
->e_sclass
== C_EXTERNAL
) {
721 for (j
= aux_size
+ 1; j
< nb_syms
- i
; j
++) {
722 if ((ext_sym
+ j
)->e_scnum
== data_shndx
) {
723 sym
->st_size
= (ext_sym
+ j
)->e_value
- ext_sym
->e_value
;
725 } else if (j
== nb_syms
- i
- 1) {
726 sec
= &shdr
[coff_data_shndx
];
727 sym
->st_size
= sec
->s_size
- ext_sym
->e_value
;
735 sym
->st_type
= ext_sym
->e_type
;
736 sym
->st_shndx
= ext_sym
->e_scnum
;
740 /* find text relocations, if any */
741 sec
= &shdr
[coff_text_shndx
];
742 coff_relocs
= load_data(fd
, sec
->s_relptr
, sec
->s_nreloc
*RELSZ
);
743 nb_relocs
= sec
->s_nreloc
;
745 /* set coff relocation */
746 relocs
= malloc(sizeof(struct coff_rel
) * nb_relocs
);
747 for (i
= 0, ext_rel
= coff_relocs
, rel
= relocs
; i
< nb_relocs
;
748 i
++, ext_rel
++, rel
++) {
749 memset(rel
, 0, sizeof(*rel
));
750 rel
->r_reloc
= ext_rel
;
751 rel
->r_offset
= *(uint32_t *)ext_rel
->r_vaddr
;
752 rel
->r_type
= *(uint16_t *)ext_rel
->r_type
;
757 #endif /* CONFIG_FORMAT_COFF */
761 int arm_emit_ldr_info(const char *name
, unsigned long start_offset
,
762 FILE *outfile
, uint8_t *p_start
, uint8_t *p_end
,
763 ELF_RELOC
*relocs
, int nb_relocs
)
767 int offset
, min_offset
, pc_offset
, data_size
;
768 uint8_t data_allocated
[1024];
769 unsigned int data_index
;
771 memset(data_allocated
, 0, sizeof(data_allocated
));
774 min_offset
= p_end
- p_start
;
775 while (p
< p_start
+ min_offset
) {
776 insn
= get32((uint32_t *)p
);
777 if ((insn
& 0x0d5f0000) == 0x051f0000) {
778 /* ldr reg, [pc, #im] */
779 offset
= insn
& 0xfff;
780 if (!(insn
& 0x00800000))
782 if ((offset
& 3) !=0)
783 error("%s:%04x: ldr pc offset must be 32 bit aligned",
784 name
, start_offset
+ p
- p_start
);
785 pc_offset
= p
- p_start
+ offset
+ 8;
786 if (pc_offset
<= (p
- p_start
) ||
787 pc_offset
>= (p_end
- p_start
))
788 error("%s:%04x: ldr pc offset must point inside the function code",
789 name
, start_offset
+ p
- p_start
);
790 if (pc_offset
< min_offset
)
791 min_offset
= pc_offset
;
794 fprintf(outfile
, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
797 data_index
= ((p_end
- p_start
) - pc_offset
- 4) >> 2;
798 fprintf(outfile
, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
800 fprintf(outfile
, " arm_ldr_ptr++;\n");
801 if (data_index
>= sizeof(data_allocated
))
802 error("%s: too many data", name
);
803 if (!data_allocated
[data_index
]) {
806 const char *sym_name
, *p
;
809 data_allocated
[data_index
] = 1;
812 addend
= get32((uint32_t *)(p_start
+ pc_offset
));
814 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
815 if (rel
->r_offset
== (pc_offset
+ start_offset
)) {
816 sym_name
= get_rel_sym_name(rel
);
817 /* the compiler leave some unnecessary references to the code */
818 if (strstart(sym_name
, "__op_param", &p
)) {
819 snprintf(relname
, sizeof(relname
), "param%s", p
);
821 snprintf(relname
, sizeof(relname
), "(long)(&%s)", sym_name
);
823 type
= ELF32_R_TYPE(rel
->r_info
);
824 if (type
!= R_ARM_ABS32
)
825 error("%s: unsupported data relocation", name
);
829 fprintf(outfile
, " arm_data_ptr[%d] = 0x%x",
831 if (relname
[0] != '\0')
832 fprintf(outfile
, " + %s", relname
);
833 fprintf(outfile
, ";\n");
839 data_size
= (p_end
- p_start
) - min_offset
;
840 if (data_size
> 0 && outfile
) {
841 fprintf(outfile
, " arm_data_ptr += %d;\n", data_size
>> 2);
844 /* the last instruction must be a mov pc, lr */
848 insn
= get32((uint32_t *)p
);
849 if ((insn
& 0xffff0000) != 0xe91b0000) {
852 printf("%s: invalid epilog\n", name
);
861 /* generate op code */
862 void gen_code(const char *name
, host_ulong offset
, host_ulong size
,
863 FILE *outfile
, int gen_switch
)
866 uint8_t *p_start
, *p_end
;
867 host_ulong start_offset
;
869 uint8_t args_present
[MAX_ARGS
];
870 const char *sym_name
, *p
;
873 /* Compute exact size excluding prologue and epilogue instructions.
874 * Increment start_offset to skip epilogue instructions, then compute
875 * copy_size the indicate the size of the remaining instructions (in
878 p_start
= text
+ offset
;
879 p_end
= p_start
+ size
;
880 start_offset
= offset
;
881 #if defined(HOST_I386) || defined(HOST_AMD64)
882 #ifdef CONFIG_FORMAT_COFF
887 error("empty code for %s", name
);
891 error("ret or jmp expected at the end of %s", name
);
893 copy_size
= p
- p_start
;
898 len
= p_end
- p_start
;
900 error("empty code for %s", name
);
901 if (p_end
[-1] == 0xc3) {
904 error("ret or jmp expected at the end of %s", name
);
909 #elif defined(HOST_PPC)
912 p
= (void *)(p_end
- 4);
914 error("empty code for %s", name
);
915 if (get32((uint32_t *)p
) != 0x4e800020)
916 error("blr expected at the end of %s", name
);
917 copy_size
= p
- p_start
;
919 #elif defined(HOST_S390)
922 p
= (void *)(p_end
- 2);
924 error("empty code for %s", name
);
925 if (get16((uint16_t *)p
) != 0x07fe && get16((uint16_t *)p
) != 0x07f4)
926 error("br %%r14 expected at the end of %s", name
);
927 copy_size
= p
- p_start
;
929 #elif defined(HOST_ALPHA)
934 /* XXX: check why it occurs */
936 error("empty code for %s", name
);
938 if (get32((uint32_t *)p
) != 0x6bfa8001)
939 error("ret expected at the end of %s", name
);
940 copy_size
= p
- p_start
;
942 #elif defined(HOST_IA64)
945 p
= (void *)(p_end
- 4);
947 error("empty code for %s", name
);
948 /* br.ret.sptk.many b0;; */
950 if (get32((uint32_t *)p
) != 0x00840008)
951 error("br.ret.sptk.many b0;; expected at the end of %s", name
);
952 copy_size
= p
- p_start
;
954 #elif defined(HOST_SPARC)
956 uint32_t start_insn
, end_insn1
, end_insn2
;
958 p
= (void *)(p_end
- 8);
960 error("empty code for %s", name
);
961 start_insn
= get32((uint32_t *)(p_start
+ 0x0));
962 end_insn1
= get32((uint32_t *)(p
+ 0x0));
963 end_insn2
= get32((uint32_t *)(p
+ 0x4));
964 if ((start_insn
& ~0x1fff) == 0x9de3a000) {
967 if ((int)(start_insn
| ~0x1fff) < -128)
968 error("Found bogus save at the start of %s", name
);
969 if (end_insn1
!= 0x81c7e008 || end_insn2
!= 0x81e80000)
970 error("ret; restore; not found at end of %s", name
);
972 error("No save at the beginning of %s", name
);
975 /* Skip a preceeding nop, if present. */
977 skip_insn
= get32((uint32_t *)(p
- 0x4));
978 if (skip_insn
== 0x01000000)
982 copy_size
= p
- p_start
;
984 #elif defined(HOST_SPARC64)
986 uint32_t start_insn
, end_insn1
, end_insn2
, skip_insn
;
988 p
= (void *)(p_end
- 8);
990 error("empty code for %s", name
);
991 start_insn
= get32((uint32_t *)(p_start
+ 0x0));
992 end_insn1
= get32((uint32_t *)(p
+ 0x0));
993 end_insn2
= get32((uint32_t *)(p
+ 0x4));
994 if ((start_insn
& ~0x1fff) == 0x9de3a000) {
997 if ((int)(start_insn
| ~0x1fff) < -256)
998 error("Found bogus save at the start of %s", name
);
999 if (end_insn1
!= 0x81c7e008 || end_insn2
!= 0x81e80000)
1000 error("ret; restore; not found at end of %s", name
);
1002 error("No save at the beginning of %s", name
);
1005 /* Skip a preceeding nop, if present. */
1007 skip_insn
= get32((uint32_t *)(p
- 0x4));
1008 if (skip_insn
== 0x01000000)
1012 copy_size
= p
- p_start
;
1014 #elif defined(HOST_ARM)
1016 if ((p_end
- p_start
) <= 16)
1017 error("%s: function too small", name
);
1018 if (get32((uint32_t *)p_start
) != 0xe1a0c00d ||
1019 (get32((uint32_t *)(p_start
+ 4)) & 0xffff0000) != 0xe92d0000 ||
1020 get32((uint32_t *)(p_start
+ 8)) != 0xe24cb004)
1021 error("%s: invalid prolog", name
);
1024 copy_size
= arm_emit_ldr_info(name
, start_offset
, NULL
, p_start
, p_end
,
1027 #elif defined(HOST_M68K)
1030 p
= (void *)(p_end
- 2);
1032 error("empty code for %s", name
);
1033 // remove NOP's, probably added for alignment
1034 while ((get16((uint16_t *)p
) == 0x4e71) &&
1037 if (get16((uint16_t *)p
) != 0x4e75)
1038 error("rts expected at the end of %s", name
);
1039 copy_size
= p
- p_start
;
1042 #error unsupported CPU
1045 /* compute the number of arguments by looking at the relocations */
1046 for(i
= 0;i
< MAX_ARGS
; i
++)
1047 args_present
[i
] = 0;
1049 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1050 if (rel
->r_offset
>= start_offset
&&
1051 rel
->r_offset
< start_offset
+ (p_end
- p_start
)) {
1052 sym_name
= get_rel_sym_name(rel
);
1053 if (strstart(sym_name
, "__op_param", &p
)) {
1054 n
= strtoul(p
, NULL
, 10);
1056 error("too many arguments in %s", name
);
1057 args_present
[n
- 1] = 1;
1063 while (nb_args
< MAX_ARGS
&& args_present
[nb_args
])
1065 for(i
= nb_args
; i
< MAX_ARGS
; i
++) {
1066 if (args_present
[i
])
1067 error("inconsistent argument numbering in %s", name
);
1070 if (gen_switch
== 2) {
1071 fprintf(outfile
, "DEF(%s, %d, %d)\n", name
+ 3, nb_args
, copy_size
);
1072 } else if (gen_switch
== 1) {
1075 fprintf(outfile
, "case INDEX_%s: {\n", name
);
1077 fprintf(outfile
, " long ");
1078 for(i
= 0; i
< nb_args
; i
++) {
1080 fprintf(outfile
, ", ");
1081 fprintf(outfile
, "param%d", i
+ 1);
1083 fprintf(outfile
, ";\n");
1085 fprintf(outfile
, " extern void %s();\n", name
);
1087 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1088 if (rel
->r_offset
>= start_offset
&&
1089 rel
->r_offset
< start_offset
+ (p_end
- p_start
)) {
1090 sym_name
= get_rel_sym_name(rel
);
1092 !strstart(sym_name
, "__op_param", NULL
) &&
1093 !strstart(sym_name
, "__op_jmp", NULL
)) {
1094 #if defined(HOST_SPARC)
1095 if (sym_name
[0] == '.') {
1097 "extern char __dot_%s __asm__(\"%s\");\n",
1098 sym_name
+1, sym_name
);
1102 fprintf(outfile
, "extern char %s;\n", sym_name
);
1107 fprintf(outfile
, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name
, start_offset
- offset
, copy_size
);
1109 /* emit code offset information */
1112 const char *sym_name
, *p
;
1116 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1117 sym_name
= get_sym_name(sym
);
1118 if (strstart(sym_name
, "__op_label", &p
)) {
1120 unsigned long offset
;
1122 /* test if the variable refers to a label inside
1123 the code we are generating */
1124 #ifdef CONFIG_FORMAT_COFF
1125 if (sym
->st_shndx
== text_shndx
) {
1126 ptr
= sdata
[coff_text_shndx
];
1127 } else if (sym
->st_shndx
== data_shndx
) {
1128 ptr
= sdata
[coff_data_shndx
];
1133 ptr
= sdata
[sym
->st_shndx
];
1136 error("__op_labelN in invalid section");
1137 offset
= sym
->st_value
;
1138 val
= *(unsigned long *)(ptr
+ offset
);
1139 #ifdef ELF_USES_RELOCA
1141 int reloc_shndx
, nb_relocs1
, j
;
1143 /* try to find a matching relocation */
1144 reloc_shndx
= find_reloc(sym
->st_shndx
);
1146 nb_relocs1
= shdr
[reloc_shndx
].sh_size
/
1147 shdr
[reloc_shndx
].sh_entsize
;
1148 rel
= (ELF_RELOC
*)sdata
[reloc_shndx
];
1149 for(j
= 0; j
< nb_relocs1
; j
++) {
1150 if (rel
->r_offset
== offset
) {
1151 val
= rel
->r_addend
;
1160 if (val
>= start_offset
&& val
< start_offset
+ copy_size
) {
1161 n
= strtol(p
, NULL
, 10);
1162 fprintf(outfile
, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n
, val
- start_offset
);
1168 /* load parameres in variables */
1169 for(i
= 0; i
< nb_args
; i
++) {
1170 fprintf(outfile
, " param%d = *opparam_ptr++;\n", i
+ 1);
1173 /* patch relocations */
1174 #if defined(HOST_I386)
1179 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1180 if (rel
->r_offset
>= start_offset
&&
1181 rel
->r_offset
< start_offset
+ copy_size
) {
1182 sym_name
= get_rel_sym_name(rel
);
1183 if (strstart(sym_name
, "__op_jmp", &p
)) {
1185 n
= strtol(p
, NULL
, 10);
1186 /* __op_jmp relocations are done at
1187 runtime to do translated block
1188 chaining: the offset of the instruction
1189 needs to be stored */
1190 fprintf(outfile
, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1191 n
, rel
->r_offset
- start_offset
);
1195 if (strstart(sym_name
, "__op_param", &p
)) {
1196 snprintf(name
, sizeof(name
), "param%s", p
);
1198 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1200 addend
= get32((uint32_t *)(text
+ rel
->r_offset
));
1201 #ifdef CONFIG_FORMAT_ELF
1202 type
= ELF32_R_TYPE(rel
->r_info
);
1205 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1206 rel
->r_offset
- start_offset
, name
, addend
);
1209 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1210 rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
1213 error("unsupported i386 relocation (%d)", type
);
1215 #elif defined(CONFIG_FORMAT_COFF)
1219 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1220 rel
->r_offset
- start_offset
, name
, addend
);
1223 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n",
1224 rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
1227 error("unsupported i386 relocation (%d)", type
);
1230 #error unsupport object format
1235 #elif defined(HOST_AMD64)
1240 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1241 if (rel
->r_offset
>= start_offset
&&
1242 rel
->r_offset
< start_offset
+ copy_size
) {
1243 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1244 if (strstart(sym_name
, "__op_param", &p
)) {
1245 snprintf(name
, sizeof(name
), "param%s", p
);
1247 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1249 type
= ELF32_R_TYPE(rel
->r_info
);
1250 addend
= rel
->r_addend
;
1253 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
1254 rel
->r_offset
- start_offset
, name
, addend
);
1257 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
1258 rel
->r_offset
- start_offset
, name
, addend
);
1261 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1262 rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
1265 error("unsupported AMD64 relocation (%d)", type
);
1270 #elif defined(HOST_PPC)
1275 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1276 if (rel
->r_offset
>= start_offset
&&
1277 rel
->r_offset
< start_offset
+ copy_size
) {
1278 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1279 if (strstart(sym_name
, "__op_jmp", &p
)) {
1281 n
= strtol(p
, NULL
, 10);
1282 /* __op_jmp relocations are done at
1283 runtime to do translated block
1284 chaining: the offset of the instruction
1285 needs to be stored */
1286 fprintf(outfile
, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1287 n
, rel
->r_offset
- start_offset
);
1291 if (strstart(sym_name
, "__op_param", &p
)) {
1292 snprintf(name
, sizeof(name
), "param%s", p
);
1294 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1296 type
= ELF32_R_TYPE(rel
->r_info
);
1297 addend
= rel
->r_addend
;
1300 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1301 rel
->r_offset
- start_offset
, name
, addend
);
1303 case R_PPC_ADDR16_LO
:
1304 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
1305 rel
->r_offset
- start_offset
, name
, addend
);
1307 case R_PPC_ADDR16_HI
:
1308 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
1309 rel
->r_offset
- start_offset
, name
, addend
);
1311 case R_PPC_ADDR16_HA
:
1312 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
1313 rel
->r_offset
- start_offset
, name
, addend
);
1316 /* warning: must be at 32 MB distancy */
1317 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
1318 rel
->r_offset
- start_offset
, rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
1321 error("unsupported powerpc relocation (%d)", type
);
1326 #elif defined(HOST_S390)
1331 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1332 if (rel
->r_offset
>= start_offset
&&
1333 rel
->r_offset
< start_offset
+ copy_size
) {
1334 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1335 if (strstart(sym_name
, "__op_param", &p
)) {
1336 snprintf(name
, sizeof(name
), "param%s", p
);
1338 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1340 type
= ELF32_R_TYPE(rel
->r_info
);
1341 addend
= rel
->r_addend
;
1344 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1345 rel
->r_offset
- start_offset
, name
, addend
);
1348 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
1349 rel
->r_offset
- start_offset
, name
, addend
);
1352 fprintf(outfile
, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
1353 rel
->r_offset
- start_offset
, name
, addend
);
1356 error("unsupported s390 relocation (%d)", type
);
1361 #elif defined(HOST_ALPHA)
1363 for (i
= 0, rel
= relocs
; i
< nb_relocs
; i
++, rel
++) {
1364 if (rel
->r_offset
>= start_offset
&& rel
->r_offset
< start_offset
+ copy_size
) {
1367 type
= ELF64_R_TYPE(rel
->r_info
);
1368 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
1370 case R_ALPHA_GPDISP
:
1371 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
1372 as an immediate instead of constructing it from the pv or ra. */
1373 fprintf(outfile
, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
1374 rel
->r_offset
- start_offset
);
1375 fprintf(outfile
, " immediate_lda(gen_code_ptr + %ld, gp);\n",
1376 rel
->r_offset
- start_offset
+ rel
->r_addend
);
1378 case R_ALPHA_LITUSE
:
1379 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
1380 now, since some called functions (libc) need pv to be set up. */
1383 /* Branch target prediction hint. Ignore for now. Should be already
1384 correct for in-function jumps. */
1386 case R_ALPHA_LITERAL
:
1387 /* Load a literal from the GOT relative to the gp. Since there's only a
1388 single gp, nothing is to be done. */
1390 case R_ALPHA_GPRELHIGH
:
1391 /* Handle fake relocations against __op_param symbol. Need to emit the
1392 high part of the immediate value instead. Other symbols need no
1393 special treatment. */
1394 if (strstart(sym_name
, "__op_param", &p
))
1395 fprintf(outfile
, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
1396 rel
->r_offset
- start_offset
, p
);
1398 case R_ALPHA_GPRELLOW
:
1399 if (strstart(sym_name
, "__op_param", &p
))
1400 fprintf(outfile
, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
1401 rel
->r_offset
- start_offset
, p
);
1404 /* PC-relative jump. Tweak offset to skip the two instructions that try to
1405 set up the gp from the pv. */
1406 fprintf(outfile
, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
1407 rel
->r_offset
- start_offset
, sym_name
, rel
->r_offset
- start_offset
);
1410 error("unsupported Alpha relocation (%d)", type
);
1415 #elif defined(HOST_IA64)
1420 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1421 if (rel
->r_offset
>= start_offset
&& rel
->r_offset
< start_offset
+ copy_size
) {
1422 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
1423 if (strstart(sym_name
, "__op_param", &p
)) {
1424 snprintf(name
, sizeof(name
), "param%s", p
);
1426 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1428 type
= ELF64_R_TYPE(rel
->r_info
);
1429 addend
= rel
->r_addend
;
1431 case R_IA64_LTOFF22
:
1432 error("must implemnt R_IA64_LTOFF22 relocation");
1433 case R_IA64_PCREL21B
:
1434 error("must implemnt R_IA64_PCREL21B relocation");
1436 error("unsupported ia64 relocation (%d)", type
);
1441 #elif defined(HOST_SPARC)
1446 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1447 if (rel
->r_offset
>= start_offset
&&
1448 rel
->r_offset
< start_offset
+ copy_size
) {
1449 sym_name
= strtab
+ symtab
[ELF32_R_SYM(rel
->r_info
)].st_name
;
1450 if (strstart(sym_name
, "__op_param", &p
)) {
1451 snprintf(name
, sizeof(name
), "param%s", p
);
1453 if (sym_name
[0] == '.')
1454 snprintf(name
, sizeof(name
),
1455 "(long)(&__dot_%s)",
1458 snprintf(name
, sizeof(name
),
1459 "(long)(&%s)", sym_name
);
1461 type
= ELF32_R_TYPE(rel
->r_info
);
1462 addend
= rel
->r_addend
;
1465 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1466 rel
->r_offset
- start_offset
, name
, addend
);
1470 " *(uint32_t *)(gen_code_ptr + %d) = "
1471 "((*(uint32_t *)(gen_code_ptr + %d)) "
1473 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1474 rel
->r_offset
- start_offset
,
1475 rel
->r_offset
- start_offset
,
1480 " *(uint32_t *)(gen_code_ptr + %d) = "
1481 "((*(uint32_t *)(gen_code_ptr + %d)) "
1483 " | ((%s + %d) & 0x3ff);\n",
1484 rel
->r_offset
- start_offset
,
1485 rel
->r_offset
- start_offset
,
1488 case R_SPARC_WDISP30
:
1490 " *(uint32_t *)(gen_code_ptr + %d) = "
1491 "((*(uint32_t *)(gen_code_ptr + %d)) "
1493 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1494 " & 0x3fffffff);\n",
1495 rel
->r_offset
- start_offset
,
1496 rel
->r_offset
- start_offset
,
1498 rel
->r_offset
- start_offset
);
1501 error("unsupported sparc relocation (%d)", type
);
1506 #elif defined(HOST_SPARC64)
1511 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1512 if (rel
->r_offset
>= start_offset
&&
1513 rel
->r_offset
< start_offset
+ copy_size
) {
1514 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
1515 if (strstart(sym_name
, "__op_param", &p
)) {
1516 snprintf(name
, sizeof(name
), "param%s", p
);
1518 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1520 type
= ELF64_R_TYPE(rel
->r_info
);
1521 addend
= rel
->r_addend
;
1524 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1525 rel
->r_offset
- start_offset
, name
, addend
);
1529 " *(uint32_t *)(gen_code_ptr + %d) = "
1530 "((*(uint32_t *)(gen_code_ptr + %d)) "
1532 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1533 rel
->r_offset
- start_offset
,
1534 rel
->r_offset
- start_offset
,
1539 " *(uint32_t *)(gen_code_ptr + %d) = "
1540 "((*(uint32_t *)(gen_code_ptr + %d)) "
1542 " | ((%s + %d) & 0x3ff);\n",
1543 rel
->r_offset
- start_offset
,
1544 rel
->r_offset
- start_offset
,
1547 case R_SPARC_WDISP30
:
1549 " *(uint32_t *)(gen_code_ptr + %d) = "
1550 "((*(uint32_t *)(gen_code_ptr + %d)) "
1552 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1553 " & 0x3fffffff);\n",
1554 rel
->r_offset
- start_offset
,
1555 rel
->r_offset
- start_offset
,
1557 rel
->r_offset
- start_offset
);
1560 error("unsupported sparc64 relocation (%d)", type
);
1565 #elif defined(HOST_ARM)
1571 arm_emit_ldr_info(name
, start_offset
, outfile
, p_start
, p_end
,
1574 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1575 if (rel
->r_offset
>= start_offset
&&
1576 rel
->r_offset
< start_offset
+ copy_size
) {
1577 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1578 /* the compiler leave some unnecessary references to the code */
1579 if (sym_name
[0] == '\0')
1581 if (strstart(sym_name
, "__op_param", &p
)) {
1582 snprintf(name
, sizeof(name
), "param%s", p
);
1584 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1586 type
= ELF32_R_TYPE(rel
->r_info
);
1587 addend
= get32((uint32_t *)(text
+ rel
->r_offset
));
1590 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1591 rel
->r_offset
- start_offset
, name
, addend
);
1594 fprintf(outfile
, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
1595 rel
->r_offset
- start_offset
, addend
, name
);
1598 error("unsupported arm relocation (%d)", type
);
1603 #elif defined(HOST_M68K)
1609 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1610 if (rel
->r_offset
>= start_offset
&&
1611 rel
->r_offset
< start_offset
+ copy_size
) {
1612 sym
= &(symtab
[ELFW(R_SYM
)(rel
->r_info
)]);
1613 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1614 if (strstart(sym_name
, "__op_param", &p
)) {
1615 snprintf(name
, sizeof(name
), "param%s", p
);
1617 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1619 type
= ELF32_R_TYPE(rel
->r_info
);
1620 addend
= get32((uint32_t *)(text
+ rel
->r_offset
)) + rel
->r_addend
;
1623 fprintf(outfile
, " /* R_68K_32 RELOC, offset %x */\n", rel
->r_offset
) ;
1624 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
1625 rel
->r_offset
- start_offset
, name
, addend
);
1628 fprintf(outfile
, " /* R_68K_PC32 RELOC, offset %x */\n", rel
->r_offset
);
1629 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
1630 rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, /*sym->st_value+*/ addend
);
1633 error("unsupported m68k relocation (%d)", type
);
1639 #error unsupported CPU
1641 fprintf(outfile
, " gen_code_ptr += %d;\n", copy_size
);
1642 fprintf(outfile
, "}\n");
1643 fprintf(outfile
, "break;\n\n");
1645 fprintf(outfile
, "static inline void gen_%s(", name
);
1647 fprintf(outfile
, "void");
1649 for(i
= 0; i
< nb_args
; i
++) {
1651 fprintf(outfile
, ", ");
1652 fprintf(outfile
, "long param%d", i
+ 1);
1655 fprintf(outfile
, ")\n");
1656 fprintf(outfile
, "{\n");
1657 for(i
= 0; i
< nb_args
; i
++) {
1658 fprintf(outfile
, " *gen_opparam_ptr++ = param%d;\n", i
+ 1);
1660 fprintf(outfile
, " *gen_opc_ptr++ = INDEX_%s;\n", name
);
1661 fprintf(outfile
, "}\n\n");
1665 int gen_file(FILE *outfile
, int out_type
)
1670 if (out_type
== OUT_INDEX_OP
) {
1671 fprintf(outfile
, "DEF(end, 0, 0)\n");
1672 fprintf(outfile
, "DEF(nop, 0, 0)\n");
1673 fprintf(outfile
, "DEF(nop1, 1, 0)\n");
1674 fprintf(outfile
, "DEF(nop2, 2, 0)\n");
1675 fprintf(outfile
, "DEF(nop3, 3, 0)\n");
1676 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1677 const char *name
, *p
;
1678 name
= get_sym_name(sym
);
1679 if (strstart(name
, OP_PREFIX
, &p
)) {
1680 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
, 2);
1683 } else if (out_type
== OUT_GEN_OP
) {
1684 /* generate gen_xxx functions */
1686 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1688 name
= get_sym_name(sym
);
1689 if (strstart(name
, OP_PREFIX
, NULL
)) {
1690 if (sym
->st_shndx
!= text_shndx
)
1691 error("invalid section for opcode (0x%x)", sym
->st_shndx
);
1692 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
, 0);
1697 /* generate big code generation switch */
1699 "int dyngen_code(uint8_t *gen_code_buf,\n"
1700 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1701 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1703 " uint8_t *gen_code_ptr;\n"
1704 " const uint16_t *opc_ptr;\n"
1705 " const uint32_t *opparam_ptr;\n");
1709 " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1710 " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1711 " uint32_t *arm_data_ptr = arm_data_table;\n");
1716 " gen_code_ptr = gen_code_buf;\n"
1717 " opc_ptr = opc_buf;\n"
1718 " opparam_ptr = opparam_buf;\n");
1720 /* Generate prologue, if needed. */
1724 " switch(*opc_ptr++) {\n"
1727 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1729 name
= get_sym_name(sym
);
1730 if (strstart(name
, OP_PREFIX
, NULL
)) {
1732 printf("%4d: %s pos=0x%08x len=%d\n",
1733 i
, name
, sym
->st_value
, sym
->st_size
);
1735 if (sym
->st_shndx
!= text_shndx
)
1736 error("invalid section for opcode (0x%x)", sym
->st_shndx
);
1737 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
, 1);
1742 " case INDEX_op_nop:\n"
1744 " case INDEX_op_nop1:\n"
1747 " case INDEX_op_nop2:\n"
1748 " opparam_ptr += 2;\n"
1750 " case INDEX_op_nop3:\n"
1751 " opparam_ptr += 3;\n"
1758 /* generate constant table if needed */
1760 " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1761 " gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1762 " last_gen_code_ptr = gen_code_ptr;\n"
1763 " arm_ldr_ptr = arm_ldr_table;\n"
1764 " arm_data_ptr = arm_data_table;\n"
1774 /* generate some code patching */
1776 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");
1778 /* flush instruction cache */
1779 fprintf(outfile
, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
1781 fprintf(outfile
, "return gen_code_ptr - gen_code_buf;\n");
1782 fprintf(outfile
, "}\n\n");
1791 printf("dyngen (c) 2003 Fabrice Bellard\n"
1792 "usage: dyngen [-o outfile] [-c] objfile\n"
1793 "Generate a dynamic code generator from an object file\n"
1794 "-c output enum of operations\n"
1795 "-g output gen_op_xx() functions\n"
1800 int main(int argc
, char **argv
)
1803 const char *filename
, *outfilename
;
1806 outfilename
= "out.c";
1807 out_type
= OUT_CODE
;
1809 c
= getopt(argc
, argv
, "ho:cg");
1817 outfilename
= optarg
;
1820 out_type
= OUT_INDEX_OP
;
1823 out_type
= OUT_GEN_OP
;
1829 filename
= argv
[optind
];
1830 outfile
= fopen(outfilename
, "w");
1832 error("could not open '%s'", outfilename
);
1834 load_object(filename
);
1835 gen_file(outfile
, out_type
);