]>
git.proxmox.com Git - mirror_qemu.git/blob - dyngen.c
2 * Generic Dynamic compiler generator
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include "config-host.h"
30 /* elf format definitions. We use these macros to test the CPU to
31 allow cross compilation (this tool must be ran on the build
33 #if defined(HOST_I386)
35 #define ELF_CLASS ELFCLASS32
36 #define ELF_ARCH EM_386
37 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
38 #undef ELF_USES_RELOCA
40 #elif defined(HOST_PPC)
42 #define ELF_CLASS ELFCLASS32
43 #define ELF_ARCH EM_PPC
44 #define elf_check_arch(x) ((x) == EM_PPC)
45 #define ELF_USES_RELOCA
47 #elif defined(HOST_S390)
49 #define ELF_CLASS ELFCLASS32
50 #define ELF_ARCH EM_S390
51 #define elf_check_arch(x) ((x) == EM_S390)
52 #define ELF_USES_RELOCA
54 #elif defined(HOST_ALPHA)
56 #define ELF_CLASS ELFCLASS64
57 #define ELF_ARCH EM_ALPHA
58 #define elf_check_arch(x) ((x) == EM_ALPHA)
59 #define ELF_USES_RELOCA
61 #elif defined(HOST_IA64)
63 #define ELF_CLASS ELFCLASS64
64 #define ELF_ARCH EM_IA_64
65 #define elf_check_arch(x) ((x) == EM_IA_64)
66 #define ELF_USES_RELOCA
68 #elif defined(HOST_SPARC)
70 #define ELF_CLASS ELFCLASS32
71 #define ELF_ARCH EM_SPARC
72 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
73 #define ELF_USES_RELOCA
75 #elif defined(HOST_SPARC64)
77 #define ELF_CLASS ELFCLASS64
78 #define ELF_ARCH EM_SPARCV9
79 #define elf_check_arch(x) ((x) == EM_SPARCV9)
80 #define ELF_USES_RELOCA
82 #elif defined(HOST_ARM)
84 #define ELF_CLASS ELFCLASS32
85 #define ELF_ARCH EM_ARM
86 #define elf_check_arch(x) ((x) == EM_ARM)
87 #define ELF_USES_RELOC
89 #elif defined(HOST_M68K)
91 #define ELF_CLASS ELFCLASS32
92 #define ELF_ARCH EM_68K
93 #define elf_check_arch(x) ((x) == EM_68K)
94 #define ELF_USES_RELOCA
97 #error unsupported CPU - please update the code
102 #if ELF_CLASS == ELFCLASS32
103 typedef int32_t host_long
;
104 typedef uint32_t host_ulong
;
105 #define swabls(x) swab32s(x)
107 typedef int64_t host_long
;
108 typedef uint64_t host_ulong
;
109 #define swabls(x) swab64s(x)
112 #ifdef ELF_USES_RELOCA
113 #define SHT_RELOC SHT_RELA
115 #define SHT_RELOC SHT_REL
126 /* all dynamically generated functions begin with this code */
127 #define OP_PREFIX "op_"
129 int elf_must_swap(struct elfhdr
*h
)
137 return (h
->e_ident
[EI_DATA
] == ELFDATA2MSB
) !=
138 (swaptest
.b
[0] == 0);
141 void swab16s(uint16_t *p
)
146 void swab32s(uint32_t *p
)
151 void swab64s(uint64_t *p
)
156 void elf_swap_ehdr(struct elfhdr
*h
)
158 swab16s(&h
->e_type
); /* Object file type */
159 swab16s(&h
-> e_machine
); /* Architecture */
160 swab32s(&h
-> e_version
); /* Object file version */
161 swabls(&h
-> e_entry
); /* Entry point virtual address */
162 swabls(&h
-> e_phoff
); /* Program header table file offset */
163 swabls(&h
-> e_shoff
); /* Section header table file offset */
164 swab32s(&h
-> e_flags
); /* Processor-specific flags */
165 swab16s(&h
-> e_ehsize
); /* ELF header size in bytes */
166 swab16s(&h
-> e_phentsize
); /* Program header table entry size */
167 swab16s(&h
-> e_phnum
); /* Program header table entry count */
168 swab16s(&h
-> e_shentsize
); /* Section header table entry size */
169 swab16s(&h
-> e_shnum
); /* Section header table entry count */
170 swab16s(&h
-> e_shstrndx
); /* Section header string table index */
173 void elf_swap_shdr(struct elf_shdr
*h
)
175 swab32s(&h
-> sh_name
); /* Section name (string tbl index) */
176 swab32s(&h
-> sh_type
); /* Section type */
177 swabls(&h
-> sh_flags
); /* Section flags */
178 swabls(&h
-> sh_addr
); /* Section virtual addr at execution */
179 swabls(&h
-> sh_offset
); /* Section file offset */
180 swabls(&h
-> sh_size
); /* Section size in bytes */
181 swab32s(&h
-> sh_link
); /* Link to another section */
182 swab32s(&h
-> sh_info
); /* Additional section information */
183 swabls(&h
-> sh_addralign
); /* Section alignment */
184 swabls(&h
-> sh_entsize
); /* Entry size if section holds table */
187 void elf_swap_phdr(struct elf_phdr
*h
)
189 swab32s(&h
->p_type
); /* Segment type */
190 swabls(&h
->p_offset
); /* Segment file offset */
191 swabls(&h
->p_vaddr
); /* Segment virtual address */
192 swabls(&h
->p_paddr
); /* Segment physical address */
193 swabls(&h
->p_filesz
); /* Segment size in file */
194 swabls(&h
->p_memsz
); /* Segment size in memory */
195 swab32s(&h
->p_flags
); /* Segment flags */
196 swabls(&h
->p_align
); /* Segment alignment */
199 void elf_swap_rel(ELF_RELOC
*rel
)
201 swabls(&rel
->r_offset
);
202 swabls(&rel
->r_info
);
203 #ifdef ELF_USES_RELOCA
204 swabls(&rel
->r_addend
);
210 struct elf_shdr
*shdr
;
218 uint16_t get16(uint16_t *p
)
227 uint32_t get32(uint32_t *p
)
236 void put16(uint16_t *p
, uint16_t val
)
243 void put32(uint32_t *p
, uint32_t val
)
250 void __attribute__((noreturn
)) __attribute__((format (printf
, 1, 2))) error(const char *fmt
, ...)
254 fprintf(stderr
, "dyngen: ");
255 vfprintf(stderr
, fmt
, ap
);
256 fprintf(stderr
, "\n");
262 struct elf_shdr
*find_elf_section(struct elf_shdr
*shdr
, int shnum
, const char *shstr
,
267 struct elf_shdr
*sec
;
269 for(i
= 0; i
< shnum
; i
++) {
273 shname
= shstr
+ sec
->sh_name
;
274 if (!strcmp(shname
, name
))
280 int find_reloc(int sh_index
)
282 struct elf_shdr
*sec
;
285 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
287 if (sec
->sh_type
== SHT_RELOC
&& sec
->sh_info
== sh_index
)
293 void *load_data(int fd
, long offset
, unsigned int size
)
300 lseek(fd
, offset
, SEEK_SET
);
301 if (read(fd
, data
, size
) != size
) {
308 int strstart(const char *str
, const char *val
, const char **ptr
)
326 int arm_emit_ldr_info(const char *name
, unsigned long start_offset
,
327 FILE *outfile
, uint8_t *p_start
, uint8_t *p_end
,
328 ELF_RELOC
*relocs
, int nb_relocs
)
332 int offset
, min_offset
, pc_offset
, data_size
;
333 uint8_t data_allocated
[1024];
334 unsigned int data_index
;
336 memset(data_allocated
, 0, sizeof(data_allocated
));
339 min_offset
= p_end
- p_start
;
340 while (p
< p_start
+ min_offset
) {
341 insn
= get32((uint32_t *)p
);
342 if ((insn
& 0x0d5f0000) == 0x051f0000) {
343 /* ldr reg, [pc, #im] */
344 offset
= insn
& 0xfff;
345 if (!(insn
& 0x00800000))
347 if ((offset
& 3) !=0)
348 error("%s:%04x: ldr pc offset must be 32 bit aligned",
349 name
, start_offset
+ p
- p_start
);
350 pc_offset
= p
- p_start
+ offset
+ 8;
351 if (pc_offset
<= (p
- p_start
) ||
352 pc_offset
>= (p_end
- p_start
))
353 error("%s:%04x: ldr pc offset must point inside the function code",
354 name
, start_offset
+ p
- p_start
);
355 if (pc_offset
< min_offset
)
356 min_offset
= pc_offset
;
359 fprintf(outfile
, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
362 data_index
= ((p_end
- p_start
) - pc_offset
- 4) >> 2;
363 fprintf(outfile
, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
365 fprintf(outfile
, " arm_ldr_ptr++;\n");
366 if (data_index
>= sizeof(data_allocated
))
367 error("%s: too many data", name
);
368 if (!data_allocated
[data_index
]) {
371 const char *sym_name
, *p
;
374 data_allocated
[data_index
] = 1;
377 addend
= get32((uint32_t *)(p_start
+ pc_offset
));
379 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
380 if (rel
->r_offset
== (pc_offset
+ start_offset
)) {
381 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
382 /* the compiler leave some unnecessary references to the code */
383 if (strstart(sym_name
, "__op_param", &p
)) {
384 snprintf(relname
, sizeof(relname
), "param%s", p
);
386 snprintf(relname
, sizeof(relname
), "(long)(&%s)", sym_name
);
388 type
= ELF32_R_TYPE(rel
->r_info
);
389 if (type
!= R_ARM_ABS32
)
390 error("%s: unsupported data relocation", name
);
394 fprintf(outfile
, " arm_data_ptr[%d] = 0x%x",
396 if (relname
[0] != '\0')
397 fprintf(outfile
, " + %s", relname
);
398 fprintf(outfile
, ";\n");
404 data_size
= (p_end
- p_start
) - min_offset
;
405 if (data_size
> 0 && outfile
) {
406 fprintf(outfile
, " arm_data_ptr += %d;\n", data_size
>> 2);
409 /* the last instruction must be a mov pc, lr */
413 insn
= get32((uint32_t *)p
);
414 if ((insn
& 0xffff0000) != 0xe91b0000) {
417 printf("%s: invalid epilog\n", name
);
426 /* generate op code */
427 void gen_code(const char *name
, host_ulong offset
, host_ulong size
,
428 FILE *outfile
, uint8_t *text
, ELF_RELOC
*relocs
, int nb_relocs
,
432 uint8_t *p_start
, *p_end
;
433 host_ulong start_offset
;
435 uint8_t args_present
[MAX_ARGS
];
436 const char *sym_name
, *p
;
439 /* Compute exact size excluding prologue and epilogue instructions.
440 * Increment start_offset to skip epilogue instructions, then compute
441 * copy_size the indicate the size of the remaining instructions (in
444 p_start
= text
+ offset
;
445 p_end
= p_start
+ size
;
446 start_offset
= offset
;
451 len
= p_end
- p_start
;
453 error("empty code for %s", name
);
454 if (p_end
[-1] == 0xc3) {
457 error("ret or jmp expected at the end of %s", name
);
465 p
= (void *)(p_end
- 4);
467 error("empty code for %s", name
);
468 if (get32((uint32_t *)p
) != 0x4e800020)
469 error("blr expected at the end of %s", name
);
470 copy_size
= p
- p_start
;
476 p
= (void *)(p_end
- 2);
478 error("empty code for %s", name
);
479 if (get16((uint16_t *)p
) != 0x07fe && get16((uint16_t *)p
) != 0x07f4)
480 error("br %%r14 expected at the end of %s", name
);
481 copy_size
= p
- p_start
;
489 error("empty code for %s", name
);
490 if (get32((uint32_t *)p
) != 0x6bfa8001)
491 error("ret expected at the end of %s", name
);
492 copy_size
= p
- p_start
;
498 p
= (void *)(p_end
- 4);
500 error("empty code for %s", name
);
501 /* br.ret.sptk.many b0;; */
503 if (get32((uint32_t *)p
) != 0x00840008)
504 error("br.ret.sptk.many b0;; expected at the end of %s", name
);
505 copy_size
= p
- p_start
;
511 uint32_t start_insn
, end_insn1
, end_insn2
;
513 p
= (void *)(p_end
- 8);
515 error("empty code for %s", name
);
516 start_insn
= get32((uint32_t *)(p_start
+ 0x0));
517 end_insn1
= get32((uint32_t *)(p
+ 0x0));
518 end_insn2
= get32((uint32_t *)(p
+ 0x4));
519 if ((start_insn
& ~0x1fff) == 0x9de3a000) {
522 if ((int)(start_insn
| ~0x1fff) < -128)
523 error("Found bogus save at the start of %s", name
);
524 if (end_insn1
!= 0x81c7e008 || end_insn2
!= 0x81e80000)
525 error("ret; restore; not found at end of %s", name
);
527 error("No save at the beginning of %s", name
);
530 /* Skip a preceeding nop, if present. */
532 skip_insn
= get32((uint32_t *)(p
- 0x4));
533 if (skip_insn
== 0x01000000)
537 copy_size
= p
- p_start
;
542 uint32_t start_insn
, end_insn1
, end_insn2
, skip_insn
;
544 p
= (void *)(p_end
- 8);
546 error("empty code for %s", name
);
547 start_insn
= get32((uint32_t *)(p_start
+ 0x0));
548 end_insn1
= get32((uint32_t *)(p
+ 0x0));
549 end_insn2
= get32((uint32_t *)(p
+ 0x4));
550 if ((start_insn
& ~0x1fff) == 0x9de3a000) {
553 if ((int)(start_insn
| ~0x1fff) < -256)
554 error("Found bogus save at the start of %s", name
);
555 if (end_insn1
!= 0x81c7e008 || end_insn2
!= 0x81e80000)
556 error("ret; restore; not found at end of %s", name
);
558 error("No save at the beginning of %s", name
);
561 /* Skip a preceeding nop, if present. */
563 skip_insn
= get32((uint32_t *)(p
- 0x4));
564 if (skip_insn
== 0x01000000)
568 copy_size
= p
- p_start
;
573 if ((p_end
- p_start
) <= 16)
574 error("%s: function too small", name
);
575 if (get32((uint32_t *)p_start
) != 0xe1a0c00d ||
576 (get32((uint32_t *)(p_start
+ 4)) & 0xffff0000) != 0xe92d0000 ||
577 get32((uint32_t *)(p_start
+ 8)) != 0xe24cb004)
578 error("%s: invalid prolog", name
);
581 copy_size
= arm_emit_ldr_info(name
, start_offset
, NULL
, p_start
, p_end
,
588 p
= (void *)(p_end
- 2);
590 error("empty code for %s", name
);
591 // remove NOP's, probably added for alignment
592 while ((get16((uint16_t *)p
) == 0x4e71) &&
595 if (get16((uint16_t *)p
) != 0x4e75)
596 error("rts expected at the end of %s", name
);
597 copy_size
= p
- p_start
;
601 error("unknown ELF architecture");
604 /* compute the number of arguments by looking at the relocations */
605 for(i
= 0;i
< MAX_ARGS
; i
++)
608 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
609 if (rel
->r_offset
>= start_offset
&&
610 rel
->r_offset
< start_offset
+ (p_end
- p_start
)) {
611 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
612 if (strstart(sym_name
, "__op_param", &p
)) {
613 n
= strtoul(p
, NULL
, 10);
615 error("too many arguments in %s", name
);
616 args_present
[n
- 1] = 1;
622 while (nb_args
< MAX_ARGS
&& args_present
[nb_args
])
624 for(i
= nb_args
; i
< MAX_ARGS
; i
++) {
626 error("inconsistent argument numbering in %s", name
);
629 if (gen_switch
== 2) {
630 fprintf(outfile
, "DEF(%s, %d, %d)\n", name
+ 3, nb_args
, copy_size
);
631 } else if (gen_switch
== 1) {
634 fprintf(outfile
, "case INDEX_%s: {\n", name
);
636 fprintf(outfile
, " long ");
637 for(i
= 0; i
< nb_args
; i
++) {
639 fprintf(outfile
, ", ");
640 fprintf(outfile
, "param%d", i
+ 1);
642 fprintf(outfile
, ";\n");
644 fprintf(outfile
, " extern void %s();\n", name
);
646 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
647 if (rel
->r_offset
>= start_offset
&&
648 rel
->r_offset
< start_offset
+ (p_end
- p_start
)) {
649 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
651 !strstart(sym_name
, "__op_param", NULL
) &&
652 !strstart(sym_name
, "__op_jmp", NULL
)) {
653 #if defined(HOST_SPARC)
654 if (sym_name
[0] == '.') {
656 "extern char __dot_%s __asm__(\"%s\");\n",
657 sym_name
+1, sym_name
);
661 fprintf(outfile
, "extern char %s;\n", sym_name
);
666 fprintf(outfile
, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name
, start_offset
- offset
, copy_size
);
668 /* emit code offset information */
671 const char *sym_name
, *p
;
675 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
676 sym_name
= strtab
+ sym
->st_name
;
677 if (strstart(sym_name
, "__op_label", &p
)) {
679 unsigned long offset
;
681 /* test if the variable refers to a label inside
682 the code we are generating */
683 ptr
= sdata
[sym
->st_shndx
];
685 error("__op_labelN in invalid section");
686 offset
= sym
->st_value
;
687 val
= *(unsigned long *)(ptr
+ offset
);
688 #ifdef ELF_USES_RELOCA
690 int reloc_shndx
, nb_relocs1
, j
;
692 /* try to find a matching relocation */
693 reloc_shndx
= find_reloc(sym
->st_shndx
);
695 nb_relocs1
= shdr
[reloc_shndx
].sh_size
/
696 shdr
[reloc_shndx
].sh_entsize
;
697 rel
= (ELF_RELOC
*)sdata
[reloc_shndx
];
698 for(j
= 0; j
< nb_relocs1
; j
++) {
699 if (rel
->r_offset
== offset
) {
709 if (val
>= start_offset
&& val
< start_offset
+ copy_size
) {
710 n
= strtol(p
, NULL
, 10);
711 fprintf(outfile
, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n
, val
- start_offset
);
717 /* load parameres in variables */
718 for(i
= 0; i
< nb_args
; i
++) {
719 fprintf(outfile
, " param%d = *opparam_ptr++;\n", i
+ 1);
722 /* patch relocations */
723 #if defined(HOST_I386)
728 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
729 if (rel
->r_offset
>= start_offset
&&
730 rel
->r_offset
< start_offset
+ copy_size
) {
731 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
732 if (strstart(sym_name
, "__op_jmp", &p
)) {
734 n
= strtol(p
, NULL
, 10);
735 /* __op_jmp relocations are done at
736 runtime to do translated block
737 chaining: the offset of the instruction
738 needs to be stored */
739 fprintf(outfile
, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
740 n
, rel
->r_offset
- start_offset
);
744 if (strstart(sym_name
, "__op_param", &p
)) {
745 snprintf(name
, sizeof(name
), "param%s", p
);
747 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
749 type
= ELF32_R_TYPE(rel
->r_info
);
750 addend
= get32((uint32_t *)(text
+ rel
->r_offset
));
753 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
754 rel
->r_offset
- start_offset
, name
, addend
);
757 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
758 rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
761 error("unsupported i386 relocation (%d)", type
);
766 #elif defined(HOST_PPC)
771 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
772 if (rel
->r_offset
>= start_offset
&&
773 rel
->r_offset
< start_offset
+ copy_size
) {
774 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
775 if (strstart(sym_name
, "__op_jmp", &p
)) {
777 n
= strtol(p
, NULL
, 10);
778 /* __op_jmp relocations are done at
779 runtime to do translated block
780 chaining: the offset of the instruction
781 needs to be stored */
782 fprintf(outfile
, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
783 n
, rel
->r_offset
- start_offset
);
787 if (strstart(sym_name
, "__op_param", &p
)) {
788 snprintf(name
, sizeof(name
), "param%s", p
);
790 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
792 type
= ELF32_R_TYPE(rel
->r_info
);
793 addend
= rel
->r_addend
;
796 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
797 rel
->r_offset
- start_offset
, name
, addend
);
799 case R_PPC_ADDR16_LO
:
800 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
801 rel
->r_offset
- start_offset
, name
, addend
);
803 case R_PPC_ADDR16_HI
:
804 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
805 rel
->r_offset
- start_offset
, name
, addend
);
807 case R_PPC_ADDR16_HA
:
808 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
809 rel
->r_offset
- start_offset
, name
, addend
);
812 /* warning: must be at 32 MB distancy */
813 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
814 rel
->r_offset
- start_offset
, rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
817 error("unsupported powerpc relocation (%d)", type
);
822 #elif defined(HOST_S390)
827 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
828 if (rel
->r_offset
>= start_offset
&&
829 rel
->r_offset
< start_offset
+ copy_size
) {
830 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
831 if (strstart(sym_name
, "__op_param", &p
)) {
832 snprintf(name
, sizeof(name
), "param%s", p
);
834 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
836 type
= ELF32_R_TYPE(rel
->r_info
);
837 addend
= rel
->r_addend
;
840 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
841 rel
->r_offset
- start_offset
, name
, addend
);
844 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
845 rel
->r_offset
- start_offset
, name
, addend
);
848 fprintf(outfile
, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
849 rel
->r_offset
- start_offset
, name
, addend
);
852 error("unsupported s390 relocation (%d)", type
);
857 #elif defined(HOST_ALPHA)
859 for (i
= 0, rel
= relocs
; i
< nb_relocs
; i
++, rel
++) {
860 if (rel
->r_offset
>= start_offset
&& rel
->r_offset
< start_offset
+ copy_size
) {
863 type
= ELF64_R_TYPE(rel
->r_info
);
864 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
867 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
868 as an immediate instead of constructing it from the pv or ra. */
869 fprintf(outfile
, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
870 rel
->r_offset
- start_offset
);
871 fprintf(outfile
, " immediate_lda(gen_code_ptr + %ld, gp);\n",
872 rel
->r_offset
- start_offset
+ rel
->r_addend
);
875 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
876 now, since some called functions (libc) need pv to be set up. */
879 /* Branch target prediction hint. Ignore for now. Should be already
880 correct for in-function jumps. */
882 case R_ALPHA_LITERAL
:
883 /* Load a literal from the GOT relative to the gp. Since there's only a
884 single gp, nothing is to be done. */
886 case R_ALPHA_GPRELHIGH
:
887 /* Handle fake relocations against __op_param symbol. Need to emit the
888 high part of the immediate value instead. Other symbols need no
889 special treatment. */
890 if (strstart(sym_name
, "__op_param", &p
))
891 fprintf(outfile
, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
892 rel
->r_offset
- start_offset
, p
);
894 case R_ALPHA_GPRELLOW
:
895 if (strstart(sym_name
, "__op_param", &p
))
896 fprintf(outfile
, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
897 rel
->r_offset
- start_offset
, p
);
900 /* PC-relative jump. Tweak offset to skip the two instructions that try to
901 set up the gp from the pv. */
902 fprintf(outfile
, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
903 rel
->r_offset
- start_offset
, sym_name
, rel
->r_offset
- start_offset
);
906 error("unsupported Alpha relocation (%d)", type
);
911 #elif defined(HOST_IA64)
916 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
917 if (rel
->r_offset
>= start_offset
&& rel
->r_offset
< start_offset
+ copy_size
) {
918 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
919 if (strstart(sym_name
, "__op_param", &p
)) {
920 snprintf(name
, sizeof(name
), "param%s", p
);
922 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
924 type
= ELF64_R_TYPE(rel
->r_info
);
925 addend
= rel
->r_addend
;
928 error("must implemnt R_IA64_LTOFF22 relocation");
929 case R_IA64_PCREL21B
:
930 error("must implemnt R_IA64_PCREL21B relocation");
932 error("unsupported ia64 relocation (%d)", type
);
937 #elif defined(HOST_SPARC)
942 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
943 if (rel
->r_offset
>= start_offset
&&
944 rel
->r_offset
< start_offset
+ copy_size
) {
945 sym_name
= strtab
+ symtab
[ELF32_R_SYM(rel
->r_info
)].st_name
;
946 if (strstart(sym_name
, "__op_param", &p
)) {
947 snprintf(name
, sizeof(name
), "param%s", p
);
949 if (sym_name
[0] == '.')
950 snprintf(name
, sizeof(name
),
954 snprintf(name
, sizeof(name
),
955 "(long)(&%s)", sym_name
);
957 type
= ELF32_R_TYPE(rel
->r_info
);
958 addend
= rel
->r_addend
;
961 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
962 rel
->r_offset
- start_offset
, name
, addend
);
966 " *(uint32_t *)(gen_code_ptr + %d) = "
967 "((*(uint32_t *)(gen_code_ptr + %d)) "
969 " | (((%s + %d) >> 10) & 0x3fffff);\n",
970 rel
->r_offset
- start_offset
,
971 rel
->r_offset
- start_offset
,
976 " *(uint32_t *)(gen_code_ptr + %d) = "
977 "((*(uint32_t *)(gen_code_ptr + %d)) "
979 " | ((%s + %d) & 0x3ff);\n",
980 rel
->r_offset
- start_offset
,
981 rel
->r_offset
- start_offset
,
984 case R_SPARC_WDISP30
:
986 " *(uint32_t *)(gen_code_ptr + %d) = "
987 "((*(uint32_t *)(gen_code_ptr + %d)) "
989 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
991 rel
->r_offset
- start_offset
,
992 rel
->r_offset
- start_offset
,
994 rel
->r_offset
- start_offset
);
997 error("unsupported sparc relocation (%d)", type
);
1002 #elif defined(HOST_SPARC64)
1007 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1008 if (rel
->r_offset
>= start_offset
&&
1009 rel
->r_offset
< start_offset
+ copy_size
) {
1010 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
1011 if (strstart(sym_name
, "__op_param", &p
)) {
1012 snprintf(name
, sizeof(name
), "param%s", p
);
1014 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1016 type
= ELF64_R_TYPE(rel
->r_info
);
1017 addend
= rel
->r_addend
;
1020 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1021 rel
->r_offset
- start_offset
, name
, addend
);
1025 " *(uint32_t *)(gen_code_ptr + %d) = "
1026 "((*(uint32_t *)(gen_code_ptr + %d)) "
1028 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1029 rel
->r_offset
- start_offset
,
1030 rel
->r_offset
- start_offset
,
1035 " *(uint32_t *)(gen_code_ptr + %d) = "
1036 "((*(uint32_t *)(gen_code_ptr + %d)) "
1038 " | ((%s + %d) & 0x3ff);\n",
1039 rel
->r_offset
- start_offset
,
1040 rel
->r_offset
- start_offset
,
1043 case R_SPARC_WDISP30
:
1045 " *(uint32_t *)(gen_code_ptr + %d) = "
1046 "((*(uint32_t *)(gen_code_ptr + %d)) "
1048 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1049 " & 0x3fffffff);\n",
1050 rel
->r_offset
- start_offset
,
1051 rel
->r_offset
- start_offset
,
1053 rel
->r_offset
- start_offset
);
1056 error("unsupported sparc64 relocation (%d)", type
);
1061 #elif defined(HOST_ARM)
1067 arm_emit_ldr_info(name
, start_offset
, outfile
, p_start
, p_end
,
1070 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1071 if (rel
->r_offset
>= start_offset
&&
1072 rel
->r_offset
< start_offset
+ copy_size
) {
1073 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1074 /* the compiler leave some unnecessary references to the code */
1075 if (sym_name
[0] == '\0')
1077 if (strstart(sym_name
, "__op_param", &p
)) {
1078 snprintf(name
, sizeof(name
), "param%s", p
);
1080 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1082 type
= ELF32_R_TYPE(rel
->r_info
);
1083 addend
= get32((uint32_t *)(text
+ rel
->r_offset
));
1086 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1087 rel
->r_offset
- start_offset
, name
, addend
);
1090 fprintf(outfile
, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
1091 rel
->r_offset
- start_offset
, addend
, name
);
1094 error("unsupported arm relocation (%d)", type
);
1099 #elif defined(HOST_M68K)
1105 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1106 if (rel
->r_offset
>= start_offset
&&
1107 rel
->r_offset
< start_offset
+ copy_size
) {
1108 sym
= &(symtab
[ELFW(R_SYM
)(rel
->r_info
)]);
1109 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1110 if (strstart(sym_name
, "__op_param", &p
)) {
1111 snprintf(name
, sizeof(name
), "param%s", p
);
1113 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
1115 type
= ELF32_R_TYPE(rel
->r_info
);
1116 addend
= get32((uint32_t *)(text
+ rel
->r_offset
)) + rel
->r_addend
;
1119 fprintf(outfile
, " /* R_68K_32 RELOC, offset %x */\n", rel
->r_offset
) ;
1120 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
1121 rel
->r_offset
- start_offset
, name
, addend
);
1124 fprintf(outfile
, " /* R_68K_PC32 RELOC, offset %x */\n", rel
->r_offset
);
1125 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
1126 rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, /*sym->st_value+*/ addend
);
1129 error("unsupported m68k relocation (%d)", type
);
1135 #error unsupported CPU
1137 fprintf(outfile
, " gen_code_ptr += %d;\n", copy_size
);
1138 fprintf(outfile
, "}\n");
1139 fprintf(outfile
, "break;\n\n");
1141 fprintf(outfile
, "static inline void gen_%s(", name
);
1143 fprintf(outfile
, "void");
1145 for(i
= 0; i
< nb_args
; i
++) {
1147 fprintf(outfile
, ", ");
1148 fprintf(outfile
, "long param%d", i
+ 1);
1151 fprintf(outfile
, ")\n");
1152 fprintf(outfile
, "{\n");
1153 for(i
= 0; i
< nb_args
; i
++) {
1154 fprintf(outfile
, " *gen_opparam_ptr++ = param%d;\n", i
+ 1);
1156 fprintf(outfile
, " *gen_opc_ptr++ = INDEX_%s;\n", name
);
1157 fprintf(outfile
, "}\n\n");
1161 /* load an elf object file */
1162 int load_elf(const char *filename
, FILE *outfile
, int out_type
)
1165 struct elf_shdr
*sec
, *symtab_sec
, *strtab_sec
, *text_sec
;
1174 fd
= open(filename
, O_RDONLY
);
1176 error("can't open file '%s'", filename
);
1178 /* Read ELF header. */
1179 if (read(fd
, &ehdr
, sizeof (ehdr
)) != sizeof (ehdr
))
1180 error("unable to read file header");
1182 /* Check ELF identification. */
1183 if (ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
1184 || ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
1185 || ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
1186 || ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
1187 || ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
1188 error("bad ELF header");
1191 do_swap
= elf_must_swap(&ehdr
);
1193 elf_swap_ehdr(&ehdr
);
1194 if (ehdr
.e_ident
[EI_CLASS
] != ELF_CLASS
)
1195 error("Unsupported ELF class");
1196 if (ehdr
.e_type
!= ET_REL
)
1197 error("ELF object file expected");
1198 if (ehdr
.e_version
!= EV_CURRENT
)
1199 error("Invalid ELF version");
1200 if (!elf_check_arch(ehdr
.e_machine
))
1201 error("Unsupported CPU (e_machine=%d)", ehdr
.e_machine
);
1203 /* read section headers */
1204 shdr
= load_data(fd
, ehdr
.e_shoff
, ehdr
.e_shnum
* sizeof(struct elf_shdr
));
1206 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
1207 elf_swap_shdr(&shdr
[i
]);
1211 /* read all section data */
1212 sdata
= malloc(sizeof(void *) * ehdr
.e_shnum
);
1213 memset(sdata
, 0, sizeof(void *) * ehdr
.e_shnum
);
1215 for(i
= 0;i
< ehdr
.e_shnum
; i
++) {
1217 if (sec
->sh_type
!= SHT_NOBITS
)
1218 sdata
[i
] = load_data(fd
, sec
->sh_offset
, sec
->sh_size
);
1221 sec
= &shdr
[ehdr
.e_shstrndx
];
1222 shstr
= sdata
[ehdr
.e_shstrndx
];
1224 /* swap relocations */
1225 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
1227 if (sec
->sh_type
== SHT_RELOC
) {
1228 nb_relocs
= sec
->sh_size
/ sec
->sh_entsize
;
1230 for(j
= 0, rel
= (ELF_RELOC
*)sdata
[i
]; j
< nb_relocs
; j
++, rel
++)
1237 text_sec
= find_elf_section(shdr
, ehdr
.e_shnum
, shstr
, ".text");
1239 error("could not find .text section");
1240 text_shndx
= text_sec
- shdr
;
1241 text
= sdata
[text_shndx
];
1243 /* find text relocations, if any */
1246 i
= find_reloc(text_shndx
);
1248 relocs
= (ELF_RELOC
*)sdata
[i
];
1249 nb_relocs
= shdr
[i
].sh_size
/ shdr
[i
].sh_entsize
;
1252 symtab_sec
= find_elf_section(shdr
, ehdr
.e_shnum
, shstr
, ".symtab");
1254 error("could not find .symtab section");
1255 strtab_sec
= &shdr
[symtab_sec
->sh_link
];
1257 symtab
= (ElfW(Sym
) *)sdata
[symtab_sec
- shdr
];
1258 strtab
= sdata
[symtab_sec
->sh_link
];
1260 nb_syms
= symtab_sec
->sh_size
/ sizeof(ElfW(Sym
));
1262 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1263 swab32s(&sym
->st_name
);
1264 swabls(&sym
->st_value
);
1265 swabls(&sym
->st_size
);
1266 swab16s(&sym
->st_shndx
);
1270 if (out_type
== OUT_INDEX_OP
) {
1271 fprintf(outfile
, "DEF(end, 0, 0)\n");
1272 fprintf(outfile
, "DEF(nop, 0, 0)\n");
1273 fprintf(outfile
, "DEF(nop1, 1, 0)\n");
1274 fprintf(outfile
, "DEF(nop2, 2, 0)\n");
1275 fprintf(outfile
, "DEF(nop3, 3, 0)\n");
1276 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1277 const char *name
, *p
;
1278 name
= strtab
+ sym
->st_name
;
1279 if (strstart(name
, OP_PREFIX
, &p
)) {
1280 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
,
1281 text
, relocs
, nb_relocs
, 2);
1284 } else if (out_type
== OUT_GEN_OP
) {
1285 /* generate gen_xxx functions */
1287 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1289 name
= strtab
+ sym
->st_name
;
1290 if (strstart(name
, OP_PREFIX
, NULL
)) {
1291 if (sym
->st_shndx
!= (text_sec
- shdr
))
1292 error("invalid section for opcode (0x%x)", sym
->st_shndx
);
1293 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
,
1294 text
, relocs
, nb_relocs
, 0);
1299 /* generate big code generation switch */
1301 "int dyngen_code(uint8_t *gen_code_buf,\n"
1302 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1303 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1305 " uint8_t *gen_code_ptr;\n"
1306 " const uint16_t *opc_ptr;\n"
1307 " const uint32_t *opparam_ptr;\n");
1311 " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1312 " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1313 " uint32_t *arm_data_ptr = arm_data_table;\n");
1318 " gen_code_ptr = gen_code_buf;\n"
1319 " opc_ptr = opc_buf;\n"
1320 " opparam_ptr = opparam_buf;\n");
1322 /* Generate prologue, if needed. */
1326 " switch(*opc_ptr++) {\n"
1329 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1331 name
= strtab
+ sym
->st_name
;
1332 if (strstart(name
, OP_PREFIX
, NULL
)) {
1334 printf("%4d: %s pos=0x%08x len=%d\n",
1335 i
, name
, sym
->st_value
, sym
->st_size
);
1337 if (sym
->st_shndx
!= (text_sec
- shdr
))
1338 error("invalid section for opcode (0x%x)", sym
->st_shndx
);
1339 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
,
1340 text
, relocs
, nb_relocs
, 1);
1345 " case INDEX_op_nop:\n"
1347 " case INDEX_op_nop1:\n"
1350 " case INDEX_op_nop2:\n"
1351 " opparam_ptr += 2;\n"
1353 " case INDEX_op_nop3:\n"
1354 " opparam_ptr += 3;\n"
1361 /* generate constant table if needed */
1363 " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1364 " gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1365 " last_gen_code_ptr = gen_code_ptr;\n"
1366 " arm_ldr_ptr = arm_ldr_table;\n"
1367 " arm_data_ptr = arm_data_table;\n"
1377 /* generate some code patching */
1379 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");
1381 /* flush instruction cache */
1382 fprintf(outfile
, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
1384 fprintf(outfile
, "return gen_code_ptr - gen_code_buf;\n");
1385 fprintf(outfile
, "}\n\n");
1395 printf("dyngen (c) 2003 Fabrice Bellard\n"
1396 "usage: dyngen [-o outfile] [-c] objfile\n"
1397 "Generate a dynamic code generator from an object file\n"
1398 "-c output enum of operations\n"
1399 "-g output gen_op_xx() functions\n"
1404 int main(int argc
, char **argv
)
1407 const char *filename
, *outfilename
;
1410 outfilename
= "out.c";
1411 out_type
= OUT_CODE
;
1413 c
= getopt(argc
, argv
, "ho:cg");
1421 outfilename
= optarg
;
1424 out_type
= OUT_INDEX_OP
;
1427 out_type
= OUT_GEN_OP
;
1433 filename
= argv
[optind
];
1434 outfile
= fopen(outfilename
, "w");
1436 error("could not open '%s'", outfilename
);
1437 load_elf(filename
, outfile
, out_type
);