]>
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.
30 /* elf format definitions. We use these macros to test the CPU to
31 allow cross compilation (this tool must be ran on the build
33 #if defined(HOST_I386)
35 #define ELF_CLASS ELFCLASS32
36 #define ELF_ARCH EM_386
37 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
38 #undef ELF_USES_RELOCA
40 #elif defined(HOST_PPC)
42 #define ELF_CLASS ELFCLASS32
43 #define ELF_ARCH EM_PPC
44 #define elf_check_arch(x) ((x) == EM_PPC)
45 #define ELF_USES_RELOCA
47 #elif defined(HOST_S390)
49 #define ELF_CLASS ELFCLASS32
50 #define ELF_ARCH EM_S390
51 #define elf_check_arch(x) ((x) == EM_S390)
52 #define ELF_USES_RELOCA
54 #elif defined(HOST_ALPHA)
56 #define ELF_CLASS ELFCLASS64
57 #define ELF_ARCH EM_ALPHA
58 #define elf_check_arch(x) ((x) == EM_ALPHA)
59 #define ELF_USES_RELOCA
61 #elif defined(HOST_IA64)
63 #define ELF_CLASS ELFCLASS64
64 #define ELF_ARCH EM_IA_64
65 #define elf_check_arch(x) ((x) == EM_IA_64)
66 #define ELF_USES_RELOCA
68 #elif defined(HOST_SPARC)
70 #define ELF_CLASS ELFCLASS32
71 #define ELF_ARCH EM_SPARC
72 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
73 #define ELF_USES_RELOCA
75 #elif defined(HOST_SPARC64)
77 #define ELF_CLASS ELFCLASS64
78 #define ELF_ARCH EM_SPARCV9
79 #define elf_check_arch(x) ((x) == EM_SPARCV9)
80 #define ELF_USES_RELOCA
83 #error unsupported CPU - please update the code
88 #if ELF_CLASS == ELFCLASS32
89 typedef int32_t host_long
;
90 typedef uint32_t host_ulong
;
91 #define swabls(x) swab32s(x)
93 typedef int64_t host_long
;
94 typedef uint64_t host_ulong
;
95 #define swabls(x) swab64s(x)
100 /* all dynamically generated functions begin with this code */
101 #define OP_PREFIX "op_"
103 int elf_must_swap(struct elfhdr
*h
)
111 return (h
->e_ident
[EI_DATA
] == ELFDATA2MSB
) !=
112 (swaptest
.b
[0] == 0);
115 void swab16s(uint16_t *p
)
120 void swab32s(uint32_t *p
)
125 void swab64s(uint64_t *p
)
130 void elf_swap_ehdr(struct elfhdr
*h
)
132 swab16s(&h
->e_type
); /* Object file type */
133 swab16s(&h
-> e_machine
); /* Architecture */
134 swab32s(&h
-> e_version
); /* Object file version */
135 swabls(&h
-> e_entry
); /* Entry point virtual address */
136 swabls(&h
-> e_phoff
); /* Program header table file offset */
137 swabls(&h
-> e_shoff
); /* Section header table file offset */
138 swab32s(&h
-> e_flags
); /* Processor-specific flags */
139 swab16s(&h
-> e_ehsize
); /* ELF header size in bytes */
140 swab16s(&h
-> e_phentsize
); /* Program header table entry size */
141 swab16s(&h
-> e_phnum
); /* Program header table entry count */
142 swab16s(&h
-> e_shentsize
); /* Section header table entry size */
143 swab16s(&h
-> e_shnum
); /* Section header table entry count */
144 swab16s(&h
-> e_shstrndx
); /* Section header string table index */
147 void elf_swap_shdr(struct elf_shdr
*h
)
149 swab32s(&h
-> sh_name
); /* Section name (string tbl index) */
150 swab32s(&h
-> sh_type
); /* Section type */
151 swabls(&h
-> sh_flags
); /* Section flags */
152 swabls(&h
-> sh_addr
); /* Section virtual addr at execution */
153 swabls(&h
-> sh_offset
); /* Section file offset */
154 swabls(&h
-> sh_size
); /* Section size in bytes */
155 swab32s(&h
-> sh_link
); /* Link to another section */
156 swab32s(&h
-> sh_info
); /* Additional section information */
157 swabls(&h
-> sh_addralign
); /* Section alignment */
158 swabls(&h
-> sh_entsize
); /* Entry size if section holds table */
161 void elf_swap_phdr(struct elf_phdr
*h
)
163 swab32s(&h
->p_type
); /* Segment type */
164 swabls(&h
->p_offset
); /* Segment file offset */
165 swabls(&h
->p_vaddr
); /* Segment virtual address */
166 swabls(&h
->p_paddr
); /* Segment physical address */
167 swabls(&h
->p_filesz
); /* Segment size in file */
168 swabls(&h
->p_memsz
); /* Segment size in memory */
169 swab32s(&h
->p_flags
); /* Segment flags */
170 swabls(&h
->p_align
); /* Segment alignment */
175 struct elf_shdr
*shdr
;
184 uint16_t get16(uint16_t *p
)
193 uint32_t get32(uint32_t *p
)
202 void put16(uint16_t *p
, uint16_t val
)
209 void put32(uint32_t *p
, uint32_t val
)
216 void __attribute__((noreturn
)) __attribute__((format (printf
, 1, 2))) error(const char *fmt
, ...)
220 fprintf(stderr
, "dyngen: ");
221 vfprintf(stderr
, fmt
, ap
);
222 fprintf(stderr
, "\n");
228 struct elf_shdr
*find_elf_section(struct elf_shdr
*shdr
, int shnum
, const char *shstr
,
233 struct elf_shdr
*sec
;
235 for(i
= 0; i
< shnum
; i
++) {
239 shname
= shstr
+ sec
->sh_name
;
240 if (!strcmp(shname
, name
))
246 void *load_data(int fd
, long offset
, unsigned int size
)
253 lseek(fd
, offset
, SEEK_SET
);
254 if (read(fd
, data
, size
) != size
) {
261 int strstart(const char *str
, const char *val
, const char **ptr
)
279 /* generate op code */
280 void gen_code(const char *name
, host_ulong offset
, host_ulong size
,
281 FILE *outfile
, uint8_t *text
, ELF_RELOC
*relocs
, int nb_relocs
, int reloc_sh_type
,
285 uint8_t *p_start
, *p_end
;
286 host_ulong start_offset
;
288 uint8_t args_present
[MAX_ARGS
];
289 const char *sym_name
, *p
;
292 /* Compute exact size excluding prologue and epilogue instructions.
293 * Increment start_offset to skip epilogue instructions, then compute
294 * copy_size the indicate the size of the remaining instructions (in
297 p_start
= text
+ offset
;
298 p_end
= p_start
+ size
;
299 start_offset
= offset
;
304 len
= p_end
- p_start
;
306 error("empty code for %s", name
);
307 if (p_end
[-1] == 0xc3) {
310 error("ret or jmp expected at the end of %s", name
);
318 p
= (void *)(p_end
- 4);
320 error("empty code for %s", name
);
321 if (get32((uint32_t *)p
) != 0x4e800020)
322 error("blr expected at the end of %s", name
);
323 copy_size
= p
- p_start
;
329 p
= (void *)(p_end
- 2);
331 error("empty code for %s", name
);
332 if (get16((uint16_t *)p
) != 0x07fe && get16((uint16_t *)p
) != 0x07f4)
333 error("br %%r14 expected at the end of %s", name
);
334 copy_size
= p
- p_start
;
342 error("empty code for %s", name
);
343 if (get32((uint32_t *)p
) != 0x6bfa8001)
344 error("ret expected at the end of %s", name
);
345 copy_size
= p
- p_start
;
351 p
= (void *)(p_end
- 4);
353 error("empty code for %s", name
);
354 /* br.ret.sptk.many b0;; */
356 if (get32((uint32_t *)p
) != 0x00840008)
357 error("br.ret.sptk.many b0;; expected at the end of %s", name
);
358 copy_size
= p
- p_start
;
364 uint32_t start_insn
, end_insn1
, end_insn2
, skip_insn
;
366 p
= (void *)(p_end
- 8);
368 error("empty code for %s", name
);
369 start_insn
= get32((uint32_t *)(p_start
+ 0x0));
370 end_insn1
= get32((uint32_t *)(p
+ 0x0));
371 end_insn2
= get32((uint32_t *)(p
+ 0x4));
372 if ((start_insn
& ~0x1fff) == 0x9de3a000) {
375 if ((int)(start_insn
| ~0x1fff) < -128)
376 error("Found bogus save at the start of %s", name
);
377 if (end_insn1
!= 0x81c7e008 || end_insn2
!= 0x81e80000)
378 error("ret; restore; not found at end of %s", name
);
380 error("No save at the beginning of %s", name
);
383 /* Skip a preceeding nop, if present. */
385 skip_insn
= get32((uint32_t *)(p
- 0x4));
386 if (skip_insn
== 0x01000000)
390 copy_size
= p
- p_start
;
395 uint32_t start_insn
, end_insn1
, end_insn2
, skip_insn
;
397 p
= (void *)(p_end
- 8);
399 error("empty code for %s", name
);
400 start_insn
= get32((uint32_t *)(p_start
+ 0x0));
401 end_insn1
= get32((uint32_t *)(p
+ 0x0));
402 end_insn2
= get32((uint32_t *)(p
+ 0x4));
403 if ((start_insn
& ~0x1fff) == 0x9de3a000) {
406 if ((int)(start_insn
| ~0x1fff) < -256)
407 error("Found bogus save at the start of %s", name
);
408 if (end_insn1
!= 0x81c7e008 || end_insn2
!= 0x81e80000)
409 error("ret; restore; not found at end of %s", name
);
411 error("No save at the beginning of %s", name
);
414 /* Skip a preceeding nop, if present. */
416 skip_insn
= get32((uint32_t *)(p
- 0x4));
417 if (skip_insn
== 0x01000000)
421 copy_size
= p
- p_start
;
425 error("unknown ELF architecture");
428 /* compute the number of arguments by looking at the relocations */
429 for(i
= 0;i
< MAX_ARGS
; i
++)
432 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
433 if (rel
->r_offset
>= start_offset
&&
434 rel
->r_offset
< start_offset
+ copy_size
) {
435 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
436 if (strstart(sym_name
, "__op_param", &p
)) {
437 n
= strtoul(p
, NULL
, 10);
439 error("too many arguments in %s", name
);
440 args_present
[n
- 1] = 1;
446 while (nb_args
< MAX_ARGS
&& args_present
[nb_args
])
448 for(i
= nb_args
; i
< MAX_ARGS
; i
++) {
450 error("inconsistent argument numbering in %s", name
);
453 if (gen_switch
== 2) {
454 fprintf(outfile
, "DEF(%s, %d, %d)\n", name
+ 3, nb_args
, copy_size
);
455 } else if (gen_switch
== 1) {
458 fprintf(outfile
, "case INDEX_%s: {\n", name
);
460 fprintf(outfile
, " long ");
461 for(i
= 0; i
< nb_args
; i
++) {
463 fprintf(outfile
, ", ");
464 fprintf(outfile
, "param%d", i
+ 1);
466 fprintf(outfile
, ";\n");
468 fprintf(outfile
, " extern void %s();\n", name
);
470 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
471 if (rel
->r_offset
>= start_offset
&&
472 rel
->r_offset
< start_offset
+ copy_size
) {
473 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
475 !strstart(sym_name
, "__op_param", NULL
) &&
476 !strstart(sym_name
, "__op_jmp", NULL
)) {
477 #if defined(HOST_SPARC)
478 if (sym_name
[0] == '.') {
480 "extern char __dot_%s __asm__(\"%s\");\n",
481 sym_name
+1, sym_name
);
485 fprintf(outfile
, "extern char %s;\n", sym_name
);
490 fprintf(outfile
, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name
, start_offset
- offset
, copy_size
);
492 /* emit code offset information */
495 const char *sym_name
, *p
;
499 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
500 sym_name
= strtab
+ sym
->st_name
;
501 if (strstart(sym_name
, "__op_label", &p
)) {
502 /* test if the variable refers to a label inside
503 the code we are generating */
504 if (sym
->st_shndx
!= data_shndx
)
505 error("__op_labelN symbols must be in .data or .sdata section");
506 val
= *(target_ulong
*)(data_data
+ sym
->st_value
);
507 if (val
>= start_offset
&& val
< start_offset
+ copy_size
) {
508 n
= strtol(p
, NULL
, 10);
509 fprintf(outfile
, " label_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", n
, val
- start_offset
);
515 /* load parameres in variables */
516 for(i
= 0; i
< nb_args
; i
++) {
517 fprintf(outfile
, " param%d = *opparam_ptr++;\n", i
+ 1);
520 /* patch relocations */
521 #if defined(HOST_I386)
526 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
527 if (rel
->r_offset
>= start_offset
&&
528 rel
->r_offset
< start_offset
+ copy_size
) {
529 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
530 if (strstart(sym_name
, "__op_param", &p
)) {
531 snprintf(name
, sizeof(name
), "param%s", p
);
533 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
535 type
= ELF32_R_TYPE(rel
->r_info
);
536 addend
= get32((uint32_t *)(text
+ rel
->r_offset
));
539 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
540 rel
->r_offset
- start_offset
, name
, addend
);
543 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
544 rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
547 error("unsupported i386 relocation (%d)", type
);
552 #elif defined(HOST_PPC)
557 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
558 if (rel
->r_offset
>= start_offset
&&
559 rel
->r_offset
< start_offset
+ copy_size
) {
560 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
561 if (strstart(sym_name
, "__op_jmp", &p
)) {
563 n
= strtol(p
, NULL
, 10);
564 /* __op_jmp relocations are done at
565 runtime to do translated block
566 chaining: the offset of the instruction
567 needs to be stored */
568 fprintf(outfile
, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
569 n
, rel
->r_offset
- start_offset
);
573 if (strstart(sym_name
, "__op_param", &p
)) {
574 snprintf(name
, sizeof(name
), "param%s", p
);
576 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
578 type
= ELF32_R_TYPE(rel
->r_info
);
579 addend
= rel
->r_addend
;
582 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
583 rel
->r_offset
- start_offset
, name
, addend
);
585 case R_PPC_ADDR16_LO
:
586 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
587 rel
->r_offset
- start_offset
, name
, addend
);
589 case R_PPC_ADDR16_HI
:
590 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
591 rel
->r_offset
- start_offset
, name
, addend
);
593 case R_PPC_ADDR16_HA
:
594 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
595 rel
->r_offset
- start_offset
, name
, addend
);
598 /* warning: must be at 32 MB distancy */
599 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
600 rel
->r_offset
- start_offset
, rel
->r_offset
- start_offset
, name
, rel
->r_offset
- start_offset
, addend
);
603 error("unsupported powerpc relocation (%d)", type
);
608 #elif defined(HOST_S390)
613 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
614 if (rel
->r_offset
>= start_offset
&&
615 rel
->r_offset
< start_offset
+ copy_size
) {
616 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
617 if (strstart(sym_name
, "__op_param", &p
)) {
618 snprintf(name
, sizeof(name
), "param%s", p
);
620 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
622 type
= ELF32_R_TYPE(rel
->r_info
);
623 addend
= rel
->r_addend
;
626 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
627 rel
->r_offset
- start_offset
, name
, addend
);
630 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
631 rel
->r_offset
- start_offset
, name
, addend
);
634 fprintf(outfile
, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
635 rel
->r_offset
- start_offset
, name
, addend
);
638 error("unsupported s390 relocation (%d)", type
);
643 #elif defined(HOST_ALPHA)
645 for (i
= 0, rel
= relocs
; i
< nb_relocs
; i
++, rel
++) {
646 if (rel
->r_offset
>= start_offset
&& rel
->r_offset
< start_offset
+ copy_size
) {
649 type
= ELF64_R_TYPE(rel
->r_info
);
650 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
653 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
654 as an immediate instead of constructing it from the pv or ra. */
655 fprintf(outfile
, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
656 rel
->r_offset
- start_offset
);
657 fprintf(outfile
, " immediate_lda(gen_code_ptr + %ld, gp);\n",
658 rel
->r_offset
- start_offset
+ rel
->r_addend
);
661 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
662 now, since some called functions (libc) need pv to be set up. */
665 /* Branch target prediction hint. Ignore for now. Should be already
666 correct for in-function jumps. */
668 case R_ALPHA_LITERAL
:
669 /* Load a literal from the GOT relative to the gp. Since there's only a
670 single gp, nothing is to be done. */
672 case R_ALPHA_GPRELHIGH
:
673 /* Handle fake relocations against __op_param symbol. Need to emit the
674 high part of the immediate value instead. Other symbols need no
675 special treatment. */
676 if (strstart(sym_name
, "__op_param", &p
))
677 fprintf(outfile
, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
678 rel
->r_offset
- start_offset
, p
);
680 case R_ALPHA_GPRELLOW
:
681 if (strstart(sym_name
, "__op_param", &p
))
682 fprintf(outfile
, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
683 rel
->r_offset
- start_offset
, p
);
686 /* PC-relative jump. Tweak offset to skip the two instructions that try to
687 set up the gp from the pv. */
688 fprintf(outfile
, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld) + 4);\n",
689 rel
->r_offset
- start_offset
, sym_name
, rel
->r_offset
- start_offset
);
692 error("unsupported Alpha relocation (%d)", type
);
697 #elif defined(HOST_IA64)
702 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
703 if (rel
->r_offset
>= start_offset
&& rel
->r_offset
< start_offset
+ copy_size
) {
704 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
705 if (strstart(sym_name
, "__op_param", &p
)) {
706 snprintf(name
, sizeof(name
), "param%s", p
);
708 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
710 type
= ELF64_R_TYPE(rel
->r_info
);
711 addend
= rel
->r_addend
;
714 error("must implemnt R_IA64_LTOFF22 relocation");
715 case R_IA64_PCREL21B
:
716 error("must implemnt R_IA64_PCREL21B relocation");
718 error("unsupported ia64 relocation (%d)", type
);
723 #elif defined(HOST_SPARC)
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
[ELF32_R_SYM(rel
->r_info
)].st_name
;
732 if (strstart(sym_name
, "__op_param", &p
)) {
733 snprintf(name
, sizeof(name
), "param%s", p
);
735 if (sym_name
[0] == '.')
736 snprintf(name
, sizeof(name
),
740 snprintf(name
, sizeof(name
),
741 "(long)(&%s)", sym_name
);
743 type
= ELF32_R_TYPE(rel
->r_info
);
744 addend
= rel
->r_addend
;
747 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
748 rel
->r_offset
- start_offset
, name
, addend
);
752 " *(uint32_t *)(gen_code_ptr + %d) = "
753 "((*(uint32_t *)(gen_code_ptr + %d)) "
755 " | (((%s + %d) >> 10) & 0x3fffff);\n",
756 rel
->r_offset
- start_offset
,
757 rel
->r_offset
- start_offset
,
762 " *(uint32_t *)(gen_code_ptr + %d) = "
763 "((*(uint32_t *)(gen_code_ptr + %d)) "
765 " | ((%s + %d) & 0x3ff);\n",
766 rel
->r_offset
- start_offset
,
767 rel
->r_offset
- start_offset
,
770 case R_SPARC_WDISP30
:
772 " *(uint32_t *)(gen_code_ptr + %d) = "
773 "((*(uint32_t *)(gen_code_ptr + %d)) "
775 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
777 rel
->r_offset
- start_offset
,
778 rel
->r_offset
- start_offset
,
780 rel
->r_offset
- start_offset
);
783 error("unsupported sparc relocation (%d)", type
);
788 #elif defined(HOST_SPARC64)
793 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
794 if (rel
->r_offset
>= start_offset
&&
795 rel
->r_offset
< start_offset
+ copy_size
) {
796 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
797 if (strstart(sym_name
, "__op_param", &p
)) {
798 snprintf(name
, sizeof(name
), "param%s", p
);
800 snprintf(name
, sizeof(name
), "(long)(&%s)", sym_name
);
802 type
= ELF64_R_TYPE(rel
->r_info
);
803 addend
= rel
->r_addend
;
806 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
807 rel
->r_offset
- start_offset
, name
, addend
);
811 " *(uint32_t *)(gen_code_ptr + %d) = "
812 "((*(uint32_t *)(gen_code_ptr + %d)) "
814 " | (((%s + %d) >> 10) & 0x3fffff);\n",
815 rel
->r_offset
- start_offset
,
816 rel
->r_offset
- start_offset
,
821 " *(uint32_t *)(gen_code_ptr + %d) = "
822 "((*(uint32_t *)(gen_code_ptr + %d)) "
824 " | ((%s + %d) & 0x3ff);\n",
825 rel
->r_offset
- start_offset
,
826 rel
->r_offset
- start_offset
,
829 case R_SPARC_WDISP30
:
831 " *(uint32_t *)(gen_code_ptr + %d) = "
832 "((*(uint32_t *)(gen_code_ptr + %d)) "
834 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
836 rel
->r_offset
- start_offset
,
837 rel
->r_offset
- start_offset
,
839 rel
->r_offset
- start_offset
);
842 error("unsupported sparc64 relocation (%d)", type
);
848 #error unsupported CPU
850 fprintf(outfile
, " gen_code_ptr += %d;\n", copy_size
);
851 fprintf(outfile
, "}\n");
852 fprintf(outfile
, "break;\n\n");
854 fprintf(outfile
, "static inline void gen_%s(", name
);
856 fprintf(outfile
, "void");
858 for(i
= 0; i
< nb_args
; i
++) {
860 fprintf(outfile
, ", ");
861 fprintf(outfile
, "long param%d", i
+ 1);
864 fprintf(outfile
, ")\n");
865 fprintf(outfile
, "{\n");
866 for(i
= 0; i
< nb_args
; i
++) {
867 fprintf(outfile
, " *gen_opparam_ptr++ = param%d;\n", i
+ 1);
869 fprintf(outfile
, " *gen_opc_ptr++ = INDEX_%s;\n", name
);
870 fprintf(outfile
, "}\n\n");
874 /* load an elf object file */
875 int load_elf(const char *filename
, FILE *outfile
, int do_print_enum
)
878 struct elf_shdr
*sec
, *symtab_sec
, *strtab_sec
, *text_sec
;
881 char *shstr
, *data_name
;
884 int nb_relocs
, reloc_sh_type
;
886 fd
= open(filename
, O_RDONLY
);
888 error("can't open file '%s'", filename
);
890 /* Read ELF header. */
891 if (read(fd
, &ehdr
, sizeof (ehdr
)) != sizeof (ehdr
))
892 error("unable to read file header");
894 /* Check ELF identification. */
895 if (ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
896 || ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
897 || ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
898 || ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
899 || ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
900 error("bad ELF header");
903 do_swap
= elf_must_swap(&ehdr
);
905 elf_swap_ehdr(&ehdr
);
906 if (ehdr
.e_ident
[EI_CLASS
] != ELF_CLASS
)
907 error("Unsupported ELF class");
908 if (ehdr
.e_type
!= ET_REL
)
909 error("ELF object file expected");
910 if (ehdr
.e_version
!= EV_CURRENT
)
911 error("Invalid ELF version");
912 if (!elf_check_arch(ehdr
.e_machine
))
913 error("Unsupported CPU (e_machine=%d)", ehdr
.e_machine
);
915 /* read section headers */
916 shdr
= load_data(fd
, ehdr
.e_shoff
, ehdr
.e_shnum
* sizeof(struct elf_shdr
));
918 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
919 elf_swap_shdr(&shdr
[i
]);
923 sec
= &shdr
[ehdr
.e_shstrndx
];
924 shstr
= load_data(fd
, sec
->sh_offset
, sec
->sh_size
);
928 text_sec
= find_elf_section(shdr
, ehdr
.e_shnum
, shstr
, ".text");
930 error("could not find .text section");
931 text
= load_data(fd
, text_sec
->sh_offset
, text_sec
->sh_size
);
933 #if defined(HOST_PPC)
934 data_name
= ".sdata";
938 sec
= find_elf_section(shdr
, ehdr
.e_shnum
, shstr
, data_name
);
940 error("could not find %s section", data_name
);
941 data_shndx
= sec
- shdr
;
942 data_data
= load_data(fd
, sec
->sh_offset
, sec
->sh_size
);
944 /* find text relocations, if any */
948 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
950 if ((sec
->sh_type
== SHT_REL
|| sec
->sh_type
== SHT_RELA
) &&
951 sec
->sh_info
== (text_sec
- shdr
)) {
952 reloc_sh_type
= sec
->sh_type
;
953 relocs
= load_data(fd
, sec
->sh_offset
, sec
->sh_size
);
954 nb_relocs
= sec
->sh_size
/ sec
->sh_entsize
;
956 if (sec
->sh_type
== SHT_REL
) {
957 ElfW(Rel
) *rel
= relocs
;
958 for(j
= 0, rel
= relocs
; j
< nb_relocs
; j
++, rel
++) {
959 swabls(&rel
->r_offset
);
960 swabls(&rel
->r_info
);
963 ElfW(Rela
) *rel
= relocs
;
964 for(j
= 0, rel
= relocs
; j
< nb_relocs
; j
++, rel
++) {
965 swabls(&rel
->r_offset
);
966 swabls(&rel
->r_info
);
967 swabls(&rel
->r_addend
);
975 symtab_sec
= find_elf_section(shdr
, ehdr
.e_shnum
, shstr
, ".symtab");
977 error("could not find .symtab section");
978 strtab_sec
= &shdr
[symtab_sec
->sh_link
];
980 symtab
= load_data(fd
, symtab_sec
->sh_offset
, symtab_sec
->sh_size
);
981 strtab
= load_data(fd
, strtab_sec
->sh_offset
, strtab_sec
->sh_size
);
983 nb_syms
= symtab_sec
->sh_size
/ sizeof(ElfW(Sym
));
985 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
986 swab32s(&sym
->st_name
);
987 swabls(&sym
->st_value
);
988 swabls(&sym
->st_size
);
989 swab16s(&sym
->st_shndx
);
994 fprintf(outfile
, "DEF(end, 0, 0)\n");
995 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
996 const char *name
, *p
;
997 name
= strtab
+ sym
->st_name
;
998 if (strstart(name
, OP_PREFIX
, &p
)) {
999 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
,
1000 text
, relocs
, nb_relocs
, reloc_sh_type
, 2);
1004 /* generate big code generation switch */
1007 "register int gp asm(\"$29\");\n"
1008 "static inline void immediate_ldah(void *p, int val) {\n"
1009 " uint32_t *dest = p;\n"
1010 " long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff;\n"
1012 " *dest &= ~0xffff;\n"
1014 " *dest |= 31 << 16;\n"
1016 "static inline void immediate_lda(void *dest, int val) {\n"
1017 " *(uint16_t *) dest = val;\n"
1019 "void fix_bsr(void *p, int offset) {\n"
1020 " uint32_t *dest = p;\n"
1021 " *dest &= ~((1 << 21) - 1);\n"
1022 " *dest |= (offset >> 2) & ((1 << 21) - 1);\n"
1026 "int dyngen_code(uint8_t *gen_code_buf,\n"
1027 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1028 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1030 " uint8_t *gen_code_ptr;\n"
1031 " const uint16_t *opc_ptr;\n"
1032 " const uint32_t *opparam_ptr;\n"
1033 " gen_code_ptr = gen_code_buf;\n"
1034 " opc_ptr = opc_buf;\n"
1035 " opparam_ptr = opparam_buf;\n");
1037 /* Generate prologue, if needed. */
1040 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0x9c23a080; /* sub %%sp, 128, %%sp */\n");
1041 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0xbc27a080; /* sub %%fp, 128, %%fp */\n");
1045 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0x9c23a100; /* sub %%sp, 256, %%sp */\n");
1046 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0xbc27a100; /* sub %%fp, 256, %%fp */\n");
1052 " switch(*opc_ptr++) {\n"
1055 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1057 name
= strtab
+ sym
->st_name
;
1058 if (strstart(name
, OP_PREFIX
, NULL
)) {
1060 printf("%4d: %s pos=0x%08x len=%d\n",
1061 i
, name
, sym
->st_value
, sym
->st_size
);
1063 if (sym
->st_shndx
!= (text_sec
- shdr
))
1064 error("invalid section for opcode (0x%x)", sym
->st_shndx
);
1065 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
,
1066 text
, relocs
, nb_relocs
, reloc_sh_type
, 1);
1078 /* generate epilogue */
1081 fprintf(outfile
, "*gen_code_ptr++ = 0xc3; /* ret */\n");
1084 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
1087 fprintf(outfile
, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
1090 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n");
1093 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n");
1096 case EM_SPARC32PLUS
:
1097 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0xbc07a080; /* add %%fp, 256, %%fp */\n");
1098 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0x81c62008; /* jmpl %%i0 + 8, %%g0 */\n");
1099 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0x9c03a080; /* add %%sp, 256, %%sp */\n");
1102 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0x81c7e008; /* ret */\n");
1103 fprintf(outfile
, "*((uint32_t *)gen_code_ptr)++ = 0x81e80000; /* restore */\n");
1106 error("unknown ELF architecture");
1109 fprintf(outfile
, "return gen_code_ptr - gen_code_buf;\n");
1110 fprintf(outfile
, "}\n\n");
1112 /* generate gen_xxx functions */
1113 /* XXX: suppress the use of these functions to simplify code */
1114 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1116 name
= strtab
+ sym
->st_name
;
1117 if (strstart(name
, OP_PREFIX
, NULL
)) {
1118 if (sym
->st_shndx
!= (text_sec
- shdr
))
1119 error("invalid section for opcode (0x%x)", sym
->st_shndx
);
1120 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
,
1121 text
, relocs
, nb_relocs
, reloc_sh_type
, 0);
1132 printf("dyngen (c) 2003 Fabrice Bellard\n"
1133 "usage: dyngen [-o outfile] [-c] objfile\n"
1134 "Generate a dynamic code generator from an object file\n"
1135 "-c output enum of operations\n"
1140 int main(int argc
, char **argv
)
1142 int c
, do_print_enum
;
1143 const char *filename
, *outfilename
;
1146 outfilename
= "out.c";
1149 c
= getopt(argc
, argv
, "ho:c");
1157 outfilename
= optarg
;
1166 filename
= argv
[optind
];
1167 outfile
= fopen(outfilename
, "w");
1169 error("could not open '%s'", outfilename
);
1170 load_elf(filename
, outfile
, do_print_enum
);