]>
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"
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)
1222 temp_name
= get_sym_name(symtab
+ *(uint32_t *)(rel
->r_reloc
->r_symndx
));
1223 if (!strcmp(temp_name
, ".data")) {
1224 for (j
= 0, sym
= symtab
; j
< nb_syms
; j
++, sym
++) {
1225 if (strstart(sym
->st_name
, sym_name
, NULL
)) {
1226 addend
-= sym
->st_value
;
1234 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1235 rel
->r_offset
- start_offset
, name
, addend
);
1238 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n",
1239 rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
1242 error("unsupported i386 relocation (%d)", type
);
1245 #error unsupport object format
1250 #elif defined(HOST_AMD64)
1255 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1256 if (rel
->r_offset
>= start_offset
&&
1257 rel
->r_offset
< start_offset
+ copy_size
) {
1258 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1259 if (strstart(sym_name
, "__op_param", &p
)) {
1260 snprintf(name
, sizeof(name
), "param%s", p
);
1262 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1264 type
= ELF32_R_TYPE(rel
->r_info
);
1265 addend
= rel
->r_addend
;
1268 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
1269 rel
->r_offset
- start_offset
, name
, addend
);
1272 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
1273 rel
->r_offset
- start_offset
, name
, addend
);
1276 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1277 rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
1280 error("unsupported AMD64 relocation (%d)", type
);
1285 #elif defined(HOST_PPC)
1290 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1291 if (rel
->r_offset
>= start_offset
&&
1292 rel
->r_offset
< start_offset
+ copy_size
) {
1293 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1294 if (strstart(sym_name
, "__op_jmp", &p
)) {
1296 n
= strtol(p
, NULL
, 10);
1297 /* __op_jmp relocations are done at
1298 runtime to do translated block
1299 chaining: the offset of the instruction
1300 needs to be stored */
1301 fprintf(outfile
, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1302 n
, rel
->r_offset
- start_offset
);
1306 if (strstart(sym_name
, "__op_param", &p
)) {
1307 snprintf(name
, sizeof(name
), "param%s", p
);
1309 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1311 type
= ELF32_R_TYPE(rel
->r_info
);
1312 addend
= rel
->r_addend
;
1315 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1316 rel
->r_offset
- start_offset
, name
, addend
);
1318 case R_PPC_ADDR16_LO
:
1319 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
1320 rel
->r_offset
- start_offset
, name
, addend
);
1322 case R_PPC_ADDR16_HI
:
1323 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
1324 rel
->r_offset
- start_offset
, name
, addend
);
1326 case R_PPC_ADDR16_HA
:
1327 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
1328 rel
->r_offset
- start_offset
, name
, addend
);
1331 /* warning: must be at 32 MB distancy */
1332 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
1333 rel
->r_offset
- start_offset
, rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
1336 error("unsupported powerpc relocation (%d)", type
);
1341 #elif defined(HOST_S390)
1346 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1347 if (rel
->r_offset
>= start_offset
&&
1348 rel
->r_offset
< start_offset
+ copy_size
) {
1349 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1350 if (strstart(sym_name
, "__op_param", &p
)) {
1351 snprintf(name
, sizeof(name
), "param%s", p
);
1353 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1355 type
= ELF32_R_TYPE(rel
->r_info
);
1356 addend
= rel
->r_addend
;
1359 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1360 rel
->r_offset
- start_offset
, name
, addend
);
1363 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
1364 rel
->r_offset
- start_offset
, name
, addend
);
1367 fprintf(outfile
, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
1368 rel
->r_offset
- start_offset
, name
, addend
);
1371 error("unsupported s390 relocation (%d)", type
);
1376 #elif defined(HOST_ALPHA)
1378 for (i
= 0, rel
= relocs
; i
< nb_relocs
; i
++, rel
++) {
1379 if (rel
->r_offset
>= start_offset
&& rel
->r_offset
< start_offset
+ copy_size
) {
1382 type
= ELF64_R_TYPE(rel
->r_info
);
1383 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
1385 case R_ALPHA_GPDISP
:
1386 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
1387 as an immediate instead of constructing it from the pv or ra. */
1388 fprintf(outfile
, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
1389 rel
->r_offset
- start_offset
);
1390 fprintf(outfile
, " immediate_lda(gen_code_ptr + %ld, gp);\n",
1391 rel
->r_offset
- start_offset
+ rel
->r_addend
);
1393 case R_ALPHA_LITUSE
:
1394 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
1395 now, since some called functions (libc) need pv to be set up. */
1398 /* Branch target prediction hint. Ignore for now. Should be already
1399 correct for in-function jumps. */
1401 case R_ALPHA_LITERAL
:
1402 /* Load a literal from the GOT relative to the gp. Since there's only a
1403 single gp, nothing is to be done. */
1405 case R_ALPHA_GPRELHIGH
:
1406 /* Handle fake relocations against __op_param symbol. Need to emit the
1407 high part of the immediate value instead. Other symbols need no
1408 special treatment. */
1409 if (strstart(sym_name
, "__op_param", &p
))
1410 fprintf(outfile
, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
1411 rel
->r_offset
- start_offset
, p
);
1413 case R_ALPHA_GPRELLOW
:
1414 if (strstart(sym_name
, "__op_param", &p
))
1415 fprintf(outfile
, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
1416 rel
->r_offset
- start_offset
, p
);
1419 /* PC-relative jump. Tweak offset to skip the two instructions that try to
1420 set up the gp from the pv. */
1421 fprintf(outfile
, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
1422 rel
->r_offset
- start_offset
, sym_name
, rel
->r_offset
- start_offset
);
1425 error("unsupported Alpha relocation (%d)", type
);
1430 #elif defined(HOST_IA64)
1435 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1436 if (rel
->r_offset
>= start_offset
&& rel
->r_offset
< start_offset
+ copy_size
) {
1437 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
1438 if (strstart(sym_name
, "__op_param", &p
)) {
1439 snprintf(name
, sizeof(name
), "param%s", p
);
1441 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1443 type
= ELF64_R_TYPE(rel
->r_info
);
1444 addend
= rel
->r_addend
;
1446 case R_IA64_LTOFF22
:
1447 error("must implemnt R_IA64_LTOFF22 relocation");
1448 case R_IA64_PCREL21B
:
1449 error("must implemnt R_IA64_PCREL21B relocation");
1451 error("unsupported ia64 relocation (%d)", type
);
1456 #elif defined(HOST_SPARC)
1461 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1462 if (rel
->r_offset
>= start_offset
&&
1463 rel
->r_offset
< start_offset
+ copy_size
) {
1464 sym_name
= strtab
+ symtab
[ELF32_R_SYM(rel
->r_info
)].st_name
;
1465 if (strstart(sym_name
, "__op_param", &p
)) {
1466 snprintf(name
, sizeof(name
), "param%s", p
);
1468 if (sym_name
[0] == '.')
1469 snprintf(name
, sizeof(name
),
1470 "(long)(&__dot_%s)",
1473 snprintf(name
, sizeof(name
),
1474 "(long)(&%s)", sym_name
);
1476 type
= ELF32_R_TYPE(rel
->r_info
);
1477 addend
= rel
->r_addend
;
1480 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1481 rel
->r_offset
- start_offset
, name
, addend
);
1485 " *(uint32_t *)(gen_code_ptr + %d) = "
1486 "((*(uint32_t *)(gen_code_ptr + %d)) "
1488 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1489 rel
->r_offset
- start_offset
,
1490 rel
->r_offset
- start_offset
,
1495 " *(uint32_t *)(gen_code_ptr + %d) = "
1496 "((*(uint32_t *)(gen_code_ptr + %d)) "
1498 " | ((%s + %d) & 0x3ff);\n",
1499 rel
->r_offset
- start_offset
,
1500 rel
->r_offset
- start_offset
,
1503 case R_SPARC_WDISP30
:
1505 " *(uint32_t *)(gen_code_ptr + %d) = "
1506 "((*(uint32_t *)(gen_code_ptr + %d)) "
1508 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1509 " & 0x3fffffff);\n",
1510 rel
->r_offset
- start_offset
,
1511 rel
->r_offset
- start_offset
,
1513 rel
->r_offset
- start_offset
);
1516 error("unsupported sparc relocation (%d)", type
);
1521 #elif defined(HOST_SPARC64)
1526 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1527 if (rel
->r_offset
>= start_offset
&&
1528 rel
->r_offset
< start_offset
+ copy_size
) {
1529 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
1530 if (strstart(sym_name
, "__op_param", &p
)) {
1531 snprintf(name
, sizeof(name
), "param%s", p
);
1533 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1535 type
= ELF64_R_TYPE(rel
->r_info
);
1536 addend
= rel
->r_addend
;
1539 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1540 rel
->r_offset
- start_offset
, name
, addend
);
1544 " *(uint32_t *)(gen_code_ptr + %d) = "
1545 "((*(uint32_t *)(gen_code_ptr + %d)) "
1547 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1548 rel
->r_offset
- start_offset
,
1549 rel
->r_offset
- start_offset
,
1554 " *(uint32_t *)(gen_code_ptr + %d) = "
1555 "((*(uint32_t *)(gen_code_ptr + %d)) "
1557 " | ((%s + %d) & 0x3ff);\n",
1558 rel
->r_offset
- start_offset
,
1559 rel
->r_offset
- start_offset
,
1562 case R_SPARC_WDISP30
:
1564 " *(uint32_t *)(gen_code_ptr + %d) = "
1565 "((*(uint32_t *)(gen_code_ptr + %d)) "
1567 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1568 " & 0x3fffffff);\n",
1569 rel
->r_offset
- start_offset
,
1570 rel
->r_offset
- start_offset
,
1572 rel
->r_offset
- start_offset
);
1575 error("unsupported sparc64 relocation (%d)", type
);
1580 #elif defined(HOST_ARM)
1586 arm_emit_ldr_info(name
, start_offset
, outfile
, p_start
, p_end
,
1589 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1590 if (rel
->r_offset
>= start_offset
&&
1591 rel
->r_offset
< start_offset
+ copy_size
) {
1592 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1593 /* the compiler leave some unnecessary references to the code */
1594 if (sym_name
[0] == '\0')
1596 if (strstart(sym_name
, "__op_param", &p
)) {
1597 snprintf(name
, sizeof(name
), "param%s", p
);
1599 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1601 type
= ELF32_R_TYPE(rel
->r_info
);
1602 addend
= get32((uint32_t *)(text
+ rel
->r_offset
));
1605 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1606 rel
->r_offset
- start_offset
, name
, addend
);
1609 fprintf(outfile
, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
1610 rel
->r_offset
- start_offset
, addend
, name
);
1613 error("unsupported arm relocation (%d)", type
);
1618 #elif defined(HOST_M68K)
1624 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1625 if (rel
->r_offset
>= start_offset
&&
1626 rel
->r_offset
< start_offset
+ copy_size
) {
1627 sym
= &(symtab
[ELFW(R_SYM
)(rel
->r_info
)]);
1628 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1629 if (strstart(sym_name
, "__op_param", &p
)) {
1630 snprintf(name
, sizeof(name
), "param%s", p
);
1632 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1634 type
= ELF32_R_TYPE(rel
->r_info
);
1635 addend
= get32((uint32_t *)(text
+ rel
->r_offset
)) + rel
->r_addend
;
1638 fprintf(outfile
, " /* R_68K_32 RELOC, offset %x */\n", rel
->r_offset
) ;
1639 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
1640 rel
->r_offset
- start_offset
, name
, addend
);
1643 fprintf(outfile
, " /* R_68K_PC32 RELOC, offset %x */\n", rel
->r_offset
);
1644 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
1645 rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, /*sym->st_value+*/ addend
);
1648 error("unsupported m68k relocation (%d)", type
);
1654 #error unsupported CPU
1656 fprintf(outfile
, " gen_code_ptr += %d;\n", copy_size
);
1657 fprintf(outfile
, "}\n");
1658 fprintf(outfile
, "break;\n\n");
1660 fprintf(outfile
, "static inline void gen_%s(", name
);
1662 fprintf(outfile
, "void");
1664 for(i
= 0; i
< nb_args
; i
++) {
1666 fprintf(outfile
, ", ");
1667 fprintf(outfile
, "long param%d", i
+ 1);
1670 fprintf(outfile
, ")\n");
1671 fprintf(outfile
, "{\n");
1672 for(i
= 0; i
< nb_args
; i
++) {
1673 fprintf(outfile
, " *gen_opparam_ptr++ = param%d;\n", i
+ 1);
1675 fprintf(outfile
, " *gen_opc_ptr++ = INDEX_%s;\n", name
);
1676 fprintf(outfile
, "}\n\n");
1680 int gen_file(FILE *outfile
, int out_type
)
1685 if (out_type
== OUT_INDEX_OP
) {
1686 fprintf(outfile
, "DEF(end, 0, 0)\n");
1687 fprintf(outfile
, "DEF(nop, 0, 0)\n");
1688 fprintf(outfile
, "DEF(nop1, 1, 0)\n");
1689 fprintf(outfile
, "DEF(nop2, 2, 0)\n");
1690 fprintf(outfile
, "DEF(nop3, 3, 0)\n");
1691 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1692 const char *name
, *p
;
1693 name
= get_sym_name(sym
);
1694 if (strstart(name
, OP_PREFIX
, &p
)) {
1695 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
, 2);
1698 } else if (out_type
== OUT_GEN_OP
) {
1699 /* generate gen_xxx functions */
1701 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1703 name
= get_sym_name(sym
);
1704 if (strstart(name
, OP_PREFIX
, NULL
)) {
1705 if (sym
->st_shndx
!= text_shndx
)
1706 error("invalid section for opcode (0x%x)", sym
->st_shndx
);
1707 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
, 0);
1712 /* generate big code generation switch */
1714 "int dyngen_code(uint8_t *gen_code_buf,\n"
1715 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1716 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1718 " uint8_t *gen_code_ptr;\n"
1719 " const uint16_t *opc_ptr;\n"
1720 " const uint32_t *opparam_ptr;\n");
1724 " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1725 " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1726 " uint32_t *arm_data_ptr = arm_data_table;\n");
1731 " gen_code_ptr = gen_code_buf;\n"
1732 " opc_ptr = opc_buf;\n"
1733 " opparam_ptr = opparam_buf;\n");
1735 /* Generate prologue, if needed. */
1739 " switch(*opc_ptr++) {\n"
1742 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1744 name
= get_sym_name(sym
);
1745 if (strstart(name
, OP_PREFIX
, NULL
)) {
1747 printf("%4d: %s pos=0x%08x len=%d\n",
1748 i
, name
, sym
->st_value
, sym
->st_size
);
1750 if (sym
->st_shndx
!= text_shndx
)
1751 error("invalid section for opcode (0x%x)", sym
->st_shndx
);
1752 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
, 1);
1757 " case INDEX_op_nop:\n"
1759 " case INDEX_op_nop1:\n"
1762 " case INDEX_op_nop2:\n"
1763 " opparam_ptr += 2;\n"
1765 " case INDEX_op_nop3:\n"
1766 " opparam_ptr += 3;\n"
1773 /* generate constant table if needed */
1775 " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1776 " gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1777 " last_gen_code_ptr = gen_code_ptr;\n"
1778 " arm_ldr_ptr = arm_ldr_table;\n"
1779 " arm_data_ptr = arm_data_table;\n"
1789 /* generate some code patching */
1791 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");
1793 /* flush instruction cache */
1794 fprintf(outfile
, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
1796 fprintf(outfile
, "return gen_code_ptr - gen_code_buf;\n");
1797 fprintf(outfile
, "}\n\n");
1806 printf("dyngen (c) 2003 Fabrice Bellard\n"
1807 "usage: dyngen [-o outfile] [-c] objfile\n"
1808 "Generate a dynamic code generator from an object file\n"
1809 "-c output enum of operations\n"
1810 "-g output gen_op_xx() functions\n"
1815 int main(int argc
, char **argv
)
1818 const char *filename
, *outfilename
;
1821 outfilename
= "out.c";
1822 out_type
= OUT_CODE
;
1824 c
= getopt(argc
, argv
, "ho:cg");
1832 outfilename
= optarg
;
1835 out_type
= OUT_INDEX_OP
;
1838 out_type
= OUT_GEN_OP
;
1844 filename
= argv
[optind
];
1845 outfile
= fopen(outfilename
, "w");
1847 error("could not open '%s'", outfilename
);
1849 load_object(filename
);
1850 gen_file(outfile
, out_type
);