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 * Mach-O Support by Matt Reda and Pierre d'Herbemont
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include "config-host.h"
35 /* NOTE: we test CONFIG_WIN32 instead of _WIN32 to enabled cross
37 #if defined(CONFIG_WIN32)
38 #define CONFIG_FORMAT_COFF
39 #elif defined(CONFIG_DARWIN)
40 #define CONFIG_FORMAT_MACH
42 #define CONFIG_FORMAT_ELF
45 #ifdef CONFIG_FORMAT_ELF
47 /* elf format definitions. We use these macros to test the CPU to
48 allow cross compilation (this tool must be ran on the build
50 #if defined(HOST_I386)
52 #define ELF_CLASS ELFCLASS32
53 #define ELF_ARCH EM_386
54 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
55 #undef ELF_USES_RELOCA
57 #elif defined(HOST_X86_64)
59 #define ELF_CLASS ELFCLASS64
60 #define ELF_ARCH EM_X86_64
61 #define elf_check_arch(x) ((x) == EM_X86_64)
62 #define ELF_USES_RELOCA
64 #elif defined(HOST_PPC)
66 #define ELF_CLASS ELFCLASS32
67 #define ELF_ARCH EM_PPC
68 #define elf_check_arch(x) ((x) == EM_PPC)
69 #define ELF_USES_RELOCA
71 #elif defined(HOST_S390)
73 #define ELF_CLASS ELFCLASS32
74 #define ELF_ARCH EM_S390
75 #define elf_check_arch(x) ((x) == EM_S390)
76 #define ELF_USES_RELOCA
78 #elif defined(HOST_ALPHA)
80 #define ELF_CLASS ELFCLASS64
81 #define ELF_ARCH EM_ALPHA
82 #define elf_check_arch(x) ((x) == EM_ALPHA)
83 #define ELF_USES_RELOCA
85 #elif defined(HOST_IA64)
87 #define ELF_CLASS ELFCLASS64
88 #define ELF_ARCH EM_IA_64
89 #define elf_check_arch(x) ((x) == EM_IA_64)
90 #define ELF_USES_RELOCA
92 #elif defined(HOST_SPARC)
94 #define ELF_CLASS ELFCLASS32
95 #define ELF_ARCH EM_SPARC
96 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
97 #define ELF_USES_RELOCA
99 #elif defined(HOST_SPARC64)
101 #define ELF_CLASS ELFCLASS64
102 #define ELF_ARCH EM_SPARCV9
103 #define elf_check_arch(x) ((x) == EM_SPARCV9)
104 #define ELF_USES_RELOCA
106 #elif defined(HOST_ARM)
108 #define ELF_CLASS ELFCLASS32
109 #define ELF_ARCH EM_ARM
110 #define elf_check_arch(x) ((x) == EM_ARM)
111 #define ELF_USES_RELOC
113 #elif defined(HOST_M68K)
115 #define ELF_CLASS ELFCLASS32
116 #define ELF_ARCH EM_68K
117 #define elf_check_arch(x) ((x) == EM_68K)
118 #define ELF_USES_RELOCA
120 #elif defined(HOST_HPPA)
122 #define ELF_CLASS ELFCLASS32
123 #define ELF_ARCH EM_PARISC
124 #define elf_check_arch(x) ((x) == EM_PARISC)
125 #define ELF_USES_RELOCA
127 #elif defined(HOST_MIPS)
129 #define ELF_CLASS ELFCLASS32
130 #define ELF_ARCH EM_MIPS
131 #define elf_check_arch(x) ((x) == EM_MIPS)
132 #define ELF_USES_RELOC
134 #elif defined(HOST_MIPS64)
136 /* Assume n32 ABI here, which is ELF32. */
137 #define ELF_CLASS ELFCLASS32
138 #define ELF_ARCH EM_MIPS
139 #define elf_check_arch(x) ((x) == EM_MIPS)
140 #define ELF_USES_RELOCA
143 #error unsupported CPU - please update the code
148 #if ELF_CLASS == ELFCLASS32
149 typedef int32_t host_long
;
150 typedef uint32_t host_ulong
;
151 #define swabls(x) swab32s(x)
152 #define swablss(x) swab32ss(x)
154 typedef int64_t host_long
;
155 typedef uint64_t host_ulong
;
156 #define swabls(x) swab64s(x)
157 #define swablss(x) swab64ss(x)
160 #ifdef ELF_USES_RELOCA
161 #define SHT_RELOC SHT_RELA
163 #define SHT_RELOC SHT_REL
166 #define EXE_RELOC ELF_RELOC
167 #define EXE_SYM ElfW(Sym)
169 #endif /* CONFIG_FORMAT_ELF */
171 #ifdef CONFIG_FORMAT_COFF
173 typedef int32_t host_long
;
174 typedef uint32_t host_ulong
;
178 #define FILENAMELEN 256
180 typedef struct coff_sym
{
181 struct external_syment
*st_syment
;
182 char st_name
[FILENAMELEN
];
189 typedef struct coff_rel
{
190 struct external_reloc
*r_reloc
;
195 #define EXE_RELOC struct coff_rel
196 #define EXE_SYM struct coff_sym
198 #endif /* CONFIG_FORMAT_COFF */
200 #ifdef CONFIG_FORMAT_MACH
202 #include <mach-o/loader.h>
203 #include <mach-o/nlist.h>
204 #include <mach-o/reloc.h>
205 #include <mach-o/ppc/reloc.h>
207 # define check_mach_header(x) (x.magic == MH_MAGIC)
208 typedef int32_t host_long
;
209 typedef uint32_t host_ulong
;
211 struct nlist_extended
217 unsigned char n_type
;
218 unsigned char n_sect
;
220 unsigned long st_value
;
221 unsigned long st_size
;
224 #define EXE_RELOC struct relocation_info
225 #define EXE_SYM struct nlist_extended
227 #endif /* CONFIG_FORMAT_MACH */
237 /* all dynamically generated functions begin with this code */
238 #define OP_PREFIX "op_"
242 static void __attribute__((noreturn
)) __attribute__((format (printf
, 1, 2))) error(const char *fmt
, ...)
246 fprintf(stderr
, "dyngen: ");
247 vfprintf(stderr
, fmt
, ap
);
248 fprintf(stderr
, "\n");
253 static void *load_data(int fd
, long offset
, unsigned int size
)
260 lseek(fd
, offset
, SEEK_SET
);
261 if (read(fd
, data
, size
) != size
) {
268 int strstart(const char *str
, const char *val
, const char **ptr
)
284 void pstrcpy(char *buf
, int buf_size
, const char *str
)
294 if (c
== 0 || q
>= buf
+ buf_size
- 1)
301 void swab16s(uint16_t *p
)
306 void swab32s(uint32_t *p
)
311 void swab32ss(int32_t *p
)
316 void swab64s(uint64_t *p
)
321 void swab64ss(int64_t *p
)
326 uint16_t get16(uint16_t *p
)
335 uint32_t get32(uint32_t *p
)
344 void put16(uint16_t *p
, uint16_t val
)
351 void put32(uint32_t *p
, uint32_t val
)
358 /* executable information */
366 #ifdef CONFIG_FORMAT_ELF
369 struct elf_shdr
*shdr
;
374 int elf_must_swap(struct elfhdr
*h
)
382 return (h
->e_ident
[EI_DATA
] == ELFDATA2MSB
) !=
383 (swaptest
.b
[0] == 0);
386 void elf_swap_ehdr(struct elfhdr
*h
)
388 swab16s(&h
->e_type
); /* Object file type */
389 swab16s(&h
-> e_machine
); /* Architecture */
390 swab32s(&h
-> e_version
); /* Object file version */
391 swabls(&h
-> e_entry
); /* Entry point virtual address */
392 swabls(&h
-> e_phoff
); /* Program header table file offset */
393 swabls(&h
-> e_shoff
); /* Section header table file offset */
394 swab32s(&h
-> e_flags
); /* Processor-specific flags */
395 swab16s(&h
-> e_ehsize
); /* ELF header size in bytes */
396 swab16s(&h
-> e_phentsize
); /* Program header table entry size */
397 swab16s(&h
-> e_phnum
); /* Program header table entry count */
398 swab16s(&h
-> e_shentsize
); /* Section header table entry size */
399 swab16s(&h
-> e_shnum
); /* Section header table entry count */
400 swab16s(&h
-> e_shstrndx
); /* Section header string table index */
403 void elf_swap_shdr(struct elf_shdr
*h
)
405 swab32s(&h
-> sh_name
); /* Section name (string tbl index) */
406 swab32s(&h
-> sh_type
); /* Section type */
407 swabls(&h
-> sh_flags
); /* Section flags */
408 swabls(&h
-> sh_addr
); /* Section virtual addr at execution */
409 swabls(&h
-> sh_offset
); /* Section file offset */
410 swabls(&h
-> sh_size
); /* Section size in bytes */
411 swab32s(&h
-> sh_link
); /* Link to another section */
412 swab32s(&h
-> sh_info
); /* Additional section information */
413 swabls(&h
-> sh_addralign
); /* Section alignment */
414 swabls(&h
-> sh_entsize
); /* Entry size if section holds table */
417 void elf_swap_phdr(struct elf_phdr
*h
)
419 swab32s(&h
->p_type
); /* Segment type */
420 swabls(&h
->p_offset
); /* Segment file offset */
421 swabls(&h
->p_vaddr
); /* Segment virtual address */
422 swabls(&h
->p_paddr
); /* Segment physical address */
423 swabls(&h
->p_filesz
); /* Segment size in file */
424 swabls(&h
->p_memsz
); /* Segment size in memory */
425 swab32s(&h
->p_flags
); /* Segment flags */
426 swabls(&h
->p_align
); /* Segment alignment */
429 void elf_swap_rel(ELF_RELOC
*rel
)
431 swabls(&rel
->r_offset
);
432 swabls(&rel
->r_info
);
433 #ifdef ELF_USES_RELOCA
434 swablss(&rel
->r_addend
);
438 struct elf_shdr
*find_elf_section(struct elf_shdr
*shdr
, int shnum
, const char *shstr
,
443 struct elf_shdr
*sec
;
445 for(i
= 0; i
< shnum
; i
++) {
449 shname
= shstr
+ sec
->sh_name
;
450 if (!strcmp(shname
, name
))
456 int find_reloc(int sh_index
)
458 struct elf_shdr
*sec
;
461 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
463 if (sec
->sh_type
== SHT_RELOC
&& sec
->sh_info
== sh_index
)
469 static host_ulong
get_rel_offset(EXE_RELOC
*rel
)
471 return rel
->r_offset
;
474 static char *get_rel_sym_name(EXE_RELOC
*rel
)
476 return strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
479 static char *get_sym_name(EXE_SYM
*sym
)
481 return strtab
+ sym
->st_name
;
484 /* load an elf object file */
485 int load_object(const char *filename
)
488 struct elf_shdr
*sec
, *symtab_sec
, *strtab_sec
, *text_sec
;
494 fd
= open(filename
, O_RDONLY
);
496 error("can't open file '%s'", filename
);
498 /* Read ELF header. */
499 if (read(fd
, &ehdr
, sizeof (ehdr
)) != sizeof (ehdr
))
500 error("unable to read file header");
502 /* Check ELF identification. */
503 if (ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
504 || ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
505 || ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
506 || ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
507 || ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) {
508 error("bad ELF header");
511 do_swap
= elf_must_swap(&ehdr
);
513 elf_swap_ehdr(&ehdr
);
514 if (ehdr
.e_ident
[EI_CLASS
] != ELF_CLASS
)
515 error("Unsupported ELF class");
516 if (ehdr
.e_type
!= ET_REL
)
517 error("ELF object file expected");
518 if (ehdr
.e_version
!= EV_CURRENT
)
519 error("Invalid ELF version");
520 if (!elf_check_arch(ehdr
.e_machine
))
521 error("Unsupported CPU (e_machine=%d)", ehdr
.e_machine
);
523 /* read section headers */
524 shdr
= load_data(fd
, ehdr
.e_shoff
, ehdr
.e_shnum
* sizeof(struct elf_shdr
));
526 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
527 elf_swap_shdr(&shdr
[i
]);
531 /* read all section data */
532 sdata
= malloc(sizeof(void *) * ehdr
.e_shnum
);
533 memset(sdata
, 0, sizeof(void *) * ehdr
.e_shnum
);
535 for(i
= 0;i
< ehdr
.e_shnum
; i
++) {
537 if (sec
->sh_type
!= SHT_NOBITS
)
538 sdata
[i
] = load_data(fd
, sec
->sh_offset
, sec
->sh_size
);
541 sec
= &shdr
[ehdr
.e_shstrndx
];
542 shstr
= (char *)sdata
[ehdr
.e_shstrndx
];
544 /* swap relocations */
545 for(i
= 0; i
< ehdr
.e_shnum
; i
++) {
547 if (sec
->sh_type
== SHT_RELOC
) {
548 nb_relocs
= sec
->sh_size
/ sec
->sh_entsize
;
550 for(j
= 0, rel
= (ELF_RELOC
*)sdata
[i
]; j
< nb_relocs
; j
++, rel
++)
557 text_sec
= find_elf_section(shdr
, ehdr
.e_shnum
, shstr
, ".text");
559 error("could not find .text section");
560 text_shndx
= text_sec
- shdr
;
561 text
= sdata
[text_shndx
];
563 /* find text relocations, if any */
566 i
= find_reloc(text_shndx
);
568 relocs
= (ELF_RELOC
*)sdata
[i
];
569 nb_relocs
= shdr
[i
].sh_size
/ shdr
[i
].sh_entsize
;
572 symtab_sec
= find_elf_section(shdr
, ehdr
.e_shnum
, shstr
, ".symtab");
574 error("could not find .symtab section");
575 strtab_sec
= &shdr
[symtab_sec
->sh_link
];
577 symtab
= (ElfW(Sym
) *)sdata
[symtab_sec
- shdr
];
578 strtab
= (char *)sdata
[symtab_sec
->sh_link
];
580 nb_syms
= symtab_sec
->sh_size
/ sizeof(ElfW(Sym
));
582 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
583 swab32s(&sym
->st_name
);
584 swabls(&sym
->st_value
);
585 swabls(&sym
->st_size
);
586 swab16s(&sym
->st_shndx
);
593 #endif /* CONFIG_FORMAT_ELF */
595 #ifdef CONFIG_FORMAT_COFF
598 struct external_scnhdr
*shdr
;
600 struct external_filehdr fhdr
;
601 struct external_syment
*coff_symtab
;
603 int coff_text_shndx
, coff_data_shndx
;
607 #define STRTAB_SIZE 4
612 #define T_FUNCTION 0x20
615 void sym_ent_name(struct external_syment
*ext_sym
, EXE_SYM
*sym
)
620 if (ext_sym
->e
.e
.e_zeroes
!= 0) {
622 for(i
= 0; i
< 8; i
++) {
623 c
= ext_sym
->e
.e_name
[i
];
630 pstrcpy(sym
->st_name
, sizeof(sym
->st_name
), strtab
+ ext_sym
->e
.e
.e_offset
);
633 /* now convert the name to a C name (suppress the leading '_') */
634 if (sym
->st_name
[0] == '_') {
635 len
= strlen(sym
->st_name
);
636 memmove(sym
->st_name
, sym
->st_name
+ 1, len
- 1);
637 sym
->st_name
[len
- 1] = '\0';
641 char *name_for_dotdata(struct coff_rel
*rel
)
644 struct coff_sym
*sym
;
647 text_data
= *(uint32_t *)(text
+ rel
->r_offset
);
649 for (i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
650 if (sym
->st_syment
->e_scnum
== data_shndx
&&
651 text_data
>= sym
->st_value
&&
652 text_data
< sym
->st_value
+ sym
->st_size
) {
661 static char *get_sym_name(EXE_SYM
*sym
)
666 static char *get_rel_sym_name(EXE_RELOC
*rel
)
669 name
= get_sym_name(symtab
+ *(uint32_t *)(rel
->r_reloc
->r_symndx
));
670 if (!strcmp(name
, ".data"))
671 name
= name_for_dotdata(rel
);
677 static host_ulong
get_rel_offset(EXE_RELOC
*rel
)
679 return rel
->r_offset
;
682 struct external_scnhdr
*find_coff_section(struct external_scnhdr
*shdr
, int shnum
, const char *name
)
686 struct external_scnhdr
*sec
;
688 for(i
= 0; i
< shnum
; i
++) {
692 shname
= sec
->s_name
;
693 if (!strcmp(shname
, name
))
699 /* load a coff object file */
700 int load_object(const char *filename
)
703 struct external_scnhdr
*sec
, *text_sec
, *data_sec
;
705 struct external_syment
*ext_sym
;
706 struct external_reloc
*coff_relocs
;
707 struct external_reloc
*ext_rel
;
714 fd
= open(filename
, O_RDONLY
720 error("can't open file '%s'", filename
);
722 /* Read COFF header. */
723 if (read(fd
, &fhdr
, sizeof (fhdr
)) != sizeof (fhdr
))
724 error("unable to read file header");
726 /* Check COFF identification. */
727 if (fhdr
.f_magic
!= I386MAGIC
) {
728 error("bad COFF header");
732 /* read section headers */
733 shdr
= load_data(fd
, sizeof(struct external_filehdr
) + fhdr
.f_opthdr
, fhdr
.f_nscns
* sizeof(struct external_scnhdr
));
735 /* read all section data */
736 sdata
= malloc(sizeof(void *) * fhdr
.f_nscns
);
737 memset(sdata
, 0, sizeof(void *) * fhdr
.f_nscns
);
739 for(i
= 0;i
< fhdr
.f_nscns
; i
++) {
741 if (!strstart(sec
->s_name
, ".bss", &p
))
742 sdata
[i
] = load_data(fd
, sec
->s_scnptr
, sec
->s_size
);
747 text_sec
= find_coff_section(shdr
, fhdr
.f_nscns
, ".text");
749 error("could not find .text section");
750 coff_text_shndx
= text_sec
- shdr
;
751 text
= sdata
[coff_text_shndx
];
754 data_sec
= find_coff_section(shdr
, fhdr
.f_nscns
, ".data");
756 error("could not find .data section");
757 coff_data_shndx
= data_sec
- shdr
;
759 coff_symtab
= load_data(fd
, fhdr
.f_symptr
, fhdr
.f_nsyms
*SYMESZ
);
760 for (i
= 0, ext_sym
= coff_symtab
; i
< nb_syms
; i
++, ext_sym
++) {
762 printf(" %02x", ((uint8_t *)ext_sym
->e
.e_name
)[i
]);
767 n_strtab
= load_data(fd
, (fhdr
.f_symptr
+ fhdr
.f_nsyms
*SYMESZ
), STRTAB_SIZE
);
768 strtab
= load_data(fd
, (fhdr
.f_symptr
+ fhdr
.f_nsyms
*SYMESZ
), *n_strtab
);
770 nb_syms
= fhdr
.f_nsyms
;
772 for (i
= 0, ext_sym
= coff_symtab
; i
< nb_syms
; i
++, ext_sym
++) {
773 if (strstart(ext_sym
->e
.e_name
, ".text", NULL
))
774 text_shndx
= ext_sym
->e_scnum
;
775 if (strstart(ext_sym
->e
.e_name
, ".data", NULL
))
776 data_shndx
= ext_sym
->e_scnum
;
779 /* set coff symbol */
780 symtab
= malloc(sizeof(struct coff_sym
) * nb_syms
);
782 for (i
= 0, ext_sym
= coff_symtab
, sym
= symtab
; i
< nb_syms
; i
++, ext_sym
++, sym
++) {
783 memset(sym
, 0, sizeof(*sym
));
784 sym
->st_syment
= ext_sym
;
785 sym_ent_name(ext_sym
, sym
);
786 sym
->st_value
= ext_sym
->e_value
;
788 aux_size
= *(int8_t *)ext_sym
->e_numaux
;
789 if (ext_sym
->e_scnum
== text_shndx
&& ext_sym
->e_type
== T_FUNCTION
) {
790 for (j
= aux_size
+ 1; j
< nb_syms
- i
; j
++) {
791 if ((ext_sym
+ j
)->e_scnum
== text_shndx
&&
792 (ext_sym
+ j
)->e_type
== T_FUNCTION
){
793 sym
->st_size
= (ext_sym
+ j
)->e_value
- ext_sym
->e_value
;
795 } else if (j
== nb_syms
- i
- 1) {
796 sec
= &shdr
[coff_text_shndx
];
797 sym
->st_size
= sec
->s_size
- ext_sym
->e_value
;
801 } else if (ext_sym
->e_scnum
== data_shndx
&& *(uint8_t *)ext_sym
->e_sclass
== C_EXTERNAL
) {
802 for (j
= aux_size
+ 1; j
< nb_syms
- i
; j
++) {
803 if ((ext_sym
+ j
)->e_scnum
== data_shndx
) {
804 sym
->st_size
= (ext_sym
+ j
)->e_value
- ext_sym
->e_value
;
806 } else if (j
== nb_syms
- i
- 1) {
807 sec
= &shdr
[coff_data_shndx
];
808 sym
->st_size
= sec
->s_size
- ext_sym
->e_value
;
816 sym
->st_type
= ext_sym
->e_type
;
817 sym
->st_shndx
= ext_sym
->e_scnum
;
821 /* find text relocations, if any */
822 sec
= &shdr
[coff_text_shndx
];
823 coff_relocs
= load_data(fd
, sec
->s_relptr
, sec
->s_nreloc
*RELSZ
);
824 nb_relocs
= sec
->s_nreloc
;
826 /* set coff relocation */
827 relocs
= malloc(sizeof(struct coff_rel
) * nb_relocs
);
828 for (i
= 0, ext_rel
= coff_relocs
, rel
= relocs
; i
< nb_relocs
;
829 i
++, ext_rel
++, rel
++) {
830 memset(rel
, 0, sizeof(*rel
));
831 rel
->r_reloc
= ext_rel
;
832 rel
->r_offset
= *(uint32_t *)ext_rel
->r_vaddr
;
833 rel
->r_type
= *(uint16_t *)ext_rel
->r_type
;
838 #endif /* CONFIG_FORMAT_COFF */
840 #ifdef CONFIG_FORMAT_MACH
843 struct mach_header mach_hdr
;
846 struct segment_command
*segment
= 0;
847 struct dysymtab_command
*dysymtabcmd
= 0;
848 struct symtab_command
*symtabcmd
= 0;
851 struct section
*section_hdr
;
852 struct section
*text_sec_hdr
;
856 struct relocation_info
*relocs
;
860 struct nlist
*symtab_std
;
863 /* indirect symbols */
866 /* Utility functions */
868 static inline char *find_str_by_index(int index
)
873 /* Used by dyngen common code */
874 static char *get_sym_name(EXE_SYM
*sym
)
876 char *name
= find_str_by_index(sym
->n_un
.n_strx
);
878 if ( sym
->n_type
& N_STAB
) /* Debug symbols are ignored */
889 /* find a section index given its segname, sectname */
890 static int find_mach_sec_index(struct section
*section_hdr
, int shnum
, const char *segname
,
891 const char *sectname
)
894 struct section
*sec
= section_hdr
;
896 for(i
= 0; i
< shnum
; i
++, sec
++) {
897 if (!sec
->segname
|| !sec
->sectname
)
899 if (!strcmp(sec
->sectname
, sectname
) && !strcmp(sec
->segname
, segname
))
905 /* find a section header given its segname, sectname */
906 struct section
*find_mach_sec_hdr(struct section
*section_hdr
, int shnum
, const char *segname
,
907 const char *sectname
)
909 int index
= find_mach_sec_index(section_hdr
, shnum
, segname
, sectname
);
912 return section_hdr
+index
;
916 static inline void fetch_next_pair_value(struct relocation_info
* rel
, unsigned int *value
)
918 struct scattered_relocation_info
* scarel
;
920 if(R_SCATTERED
& rel
->r_address
) {
921 scarel
= (struct scattered_relocation_info
*)rel
;
922 if(scarel
->r_type
!= PPC_RELOC_PAIR
)
923 error("fetch_next_pair_value: looking for a pair which was not found (1)");
924 *value
= scarel
->r_value
;
926 if(rel
->r_type
!= PPC_RELOC_PAIR
)
927 error("fetch_next_pair_value: looking for a pair which was not found (2)");
928 *value
= rel
->r_address
;
932 /* find a sym name given its value, in a section number */
933 static const char * find_sym_with_value_and_sec_number( int value
, int sectnum
, int * offset
)
937 for( i
= 0 ; i
< nb_syms
; i
++ )
939 if( !(symtab
[i
].n_type
& N_STAB
) && (symtab
[i
].n_type
& N_SECT
) &&
940 (symtab
[i
].n_sect
== sectnum
) && (symtab
[i
].st_value
<= value
) )
942 if( (ret
<0) || (symtab
[i
].st_value
>= symtab
[ret
].st_value
) )
950 *offset
= value
- symtab
[ret
].st_value
;
951 return get_sym_name(&symtab
[ret
]);
956 * Find symbol name given a (virtual) address, and a section which is of type
957 * S_NON_LAZY_SYMBOL_POINTERS or S_LAZY_SYMBOL_POINTERS or S_SYMBOL_STUBS
959 static const char * find_reloc_name_in_sec_ptr(int address
, struct section
* sec_hdr
)
961 unsigned int tocindex
, symindex
, size
;
962 const char *name
= 0;
965 if(!( address
>= sec_hdr
->addr
&& address
< (sec_hdr
->addr
+ sec_hdr
->size
) ) )
968 if( sec_hdr
->flags
& S_SYMBOL_STUBS
){
969 size
= sec_hdr
->reserved2
;
974 else if( sec_hdr
->flags
& S_LAZY_SYMBOL_POINTERS
||
975 sec_hdr
->flags
& S_NON_LAZY_SYMBOL_POINTERS
)
976 size
= sizeof(unsigned long);
980 /* Compute our index in toc */
981 tocindex
= (address
- sec_hdr
->addr
)/size
;
982 symindex
= tocdylib
[sec_hdr
->reserved1
+ tocindex
];
984 name
= get_sym_name(&symtab
[symindex
]);
989 static const char * find_reloc_name_given_its_address(int address
)
992 for(i
= 0; i
< segment
->nsects
; i
++)
994 const char * name
= find_reloc_name_in_sec_ptr(address
, §ion_hdr
[i
]);
1001 static const char * get_reloc_name(EXE_RELOC
* rel
, int * sslide
)
1004 struct scattered_relocation_info
* sca_rel
= (struct scattered_relocation_info
*)rel
;
1005 int sectnum
= rel
->r_symbolnum
;
1009 /* init the slide value */
1012 if(R_SCATTERED
& rel
->r_address
)
1013 return (char *)find_reloc_name_given_its_address(sca_rel
->r_value
);
1017 /* ignore debug sym */
1018 if ( symtab
[rel
->r_symbolnum
].n_type
& N_STAB
)
1020 return get_sym_name(&symtab
[rel
->r_symbolnum
]);
1023 /* Intruction contains an offset to the symbols pointed to, in the rel->r_symbolnum section */
1024 sectoffset
= *(uint32_t *)(text
+ rel
->r_address
) & 0xffff;
1026 if(sectnum
==0xffffff)
1030 if(sectnum
> segment
->nsects
)
1031 error("sectnum > segment->nsects");
1035 case PPC_RELOC_LO16
: fetch_next_pair_value(rel
+1, &other_half
); sectoffset
|= (other_half
<< 16);
1037 case PPC_RELOC_HI16
: fetch_next_pair_value(rel
+1, &other_half
); sectoffset
= (sectoffset
<< 16) | (uint16_t)(other_half
& 0xffff);
1039 case PPC_RELOC_HA16
: fetch_next_pair_value(rel
+1, &other_half
); sectoffset
= (sectoffset
<< 16) + (int16_t)(other_half
& 0xffff);
1041 case PPC_RELOC_BR24
:
1042 sectoffset
= ( *(uint32_t *)(text
+ rel
->r_address
) & 0x03fffffc );
1043 if (sectoffset
& 0x02000000) sectoffset
|= 0xfc000000;
1046 error("switch(rel->type) not found");
1050 sectoffset
+= rel
->r_address
;
1052 if (rel
->r_type
== PPC_RELOC_BR24
)
1053 name
= (char *)find_reloc_name_in_sec_ptr((int)sectoffset
, §ion_hdr
[sectnum
-1]);
1055 /* search it in the full symbol list, if not found */
1057 name
= (char *)find_sym_with_value_and_sec_number(sectoffset
, sectnum
, sslide
);
1062 /* Used by dyngen common code */
1063 static const char * get_rel_sym_name(EXE_RELOC
* rel
)
1066 return get_reloc_name( rel
, &sslide
);
1069 /* Used by dyngen common code */
1070 static host_ulong
get_rel_offset(EXE_RELOC
*rel
)
1072 struct scattered_relocation_info
* sca_rel
= (struct scattered_relocation_info
*)rel
;
1073 if(R_SCATTERED
& rel
->r_address
)
1074 return sca_rel
->r_address
;
1076 return rel
->r_address
;
1079 /* load a mach-o object file */
1080 int load_object(const char *filename
)
1083 unsigned int offset_to_segment
= 0;
1084 unsigned int offset_to_dysymtab
= 0;
1085 unsigned int offset_to_symtab
= 0;
1086 struct load_command lc
;
1089 struct nlist
*syment
;
1091 fd
= open(filename
, O_RDONLY
);
1093 error("can't open file '%s'", filename
);
1095 /* Read Mach header. */
1096 if (read(fd
, &mach_hdr
, sizeof (mach_hdr
)) != sizeof (mach_hdr
))
1097 error("unable to read file header");
1099 /* Check Mach identification. */
1100 if (!check_mach_header(mach_hdr
)) {
1101 error("bad Mach header");
1104 if (mach_hdr
.cputype
!= CPU_TYPE_POWERPC
)
1105 error("Unsupported CPU");
1107 if (mach_hdr
.filetype
!= MH_OBJECT
)
1108 error("Unsupported Mach Object");
1110 /* read segment headers */
1111 for(i
=0, j
=sizeof(mach_hdr
); i
<mach_hdr
.ncmds
; i
++)
1113 if(read(fd
, &lc
, sizeof(struct load_command
)) != sizeof(struct load_command
))
1114 error("unable to read load_command");
1115 if(lc
.cmd
== LC_SEGMENT
)
1117 offset_to_segment
= j
;
1118 lseek(fd
, offset_to_segment
, SEEK_SET
);
1119 segment
= malloc(sizeof(struct segment_command
));
1120 if(read(fd
, segment
, sizeof(struct segment_command
)) != sizeof(struct segment_command
))
1121 error("unable to read LC_SEGMENT");
1123 if(lc
.cmd
== LC_DYSYMTAB
)
1125 offset_to_dysymtab
= j
;
1126 lseek(fd
, offset_to_dysymtab
, SEEK_SET
);
1127 dysymtabcmd
= malloc(sizeof(struct dysymtab_command
));
1128 if(read(fd
, dysymtabcmd
, sizeof(struct dysymtab_command
)) != sizeof(struct dysymtab_command
))
1129 error("unable to read LC_DYSYMTAB");
1131 if(lc
.cmd
== LC_SYMTAB
)
1133 offset_to_symtab
= j
;
1134 lseek(fd
, offset_to_symtab
, SEEK_SET
);
1135 symtabcmd
= malloc(sizeof(struct symtab_command
));
1136 if(read(fd
, symtabcmd
, sizeof(struct symtab_command
)) != sizeof(struct symtab_command
))
1137 error("unable to read LC_SYMTAB");
1141 lseek(fd
, j
, SEEK_SET
);
1145 error("unable to find LC_SEGMENT");
1147 /* read section headers */
1148 section_hdr
= load_data(fd
, offset_to_segment
+ sizeof(struct segment_command
), segment
->nsects
* sizeof(struct section
));
1150 /* read all section data */
1151 sdata
= (uint8_t **)malloc(sizeof(void *) * segment
->nsects
);
1152 memset(sdata
, 0, sizeof(void *) * segment
->nsects
);
1154 /* Load the data in section data */
1155 for(i
= 0; i
< segment
->nsects
; i
++) {
1156 sdata
[i
] = load_data(fd
, section_hdr
[i
].offset
, section_hdr
[i
].size
);
1160 text_sec_hdr
= find_mach_sec_hdr(section_hdr
, segment
->nsects
, SEG_TEXT
, SECT_TEXT
);
1161 i
= find_mach_sec_index(section_hdr
, segment
->nsects
, SEG_TEXT
, SECT_TEXT
);
1162 if (i
== -1 || !text_sec_hdr
)
1163 error("could not find __TEXT,__text section");
1166 /* Make sure dysym was loaded */
1167 if(!(int)dysymtabcmd
)
1168 error("could not find __DYSYMTAB segment");
1170 /* read the table of content of the indirect sym */
1171 tocdylib
= load_data( fd
, dysymtabcmd
->indirectsymoff
, dysymtabcmd
->nindirectsyms
* sizeof(uint32_t) );
1173 /* Make sure symtab was loaded */
1175 error("could not find __SYMTAB segment");
1176 nb_syms
= symtabcmd
->nsyms
;
1178 symtab_std
= load_data(fd
, symtabcmd
->symoff
, symtabcmd
->nsyms
* sizeof(struct nlist
));
1179 strtab
= load_data(fd
, symtabcmd
->stroff
, symtabcmd
->strsize
);
1181 symtab
= malloc(sizeof(EXE_SYM
) * nb_syms
);
1183 /* Now transform the symtab, to an extended version, with the sym size, and the C name */
1184 for(i
= 0, sym
= symtab
, syment
= symtab_std
; i
< nb_syms
; i
++, sym
++, syment
++) {
1185 struct nlist
*sym_follow
, *sym_next
= 0;
1187 memset(sym
, 0, sizeof(*sym
));
1189 if ( syment
->n_type
& N_STAB
) /* Debug symbols are skipped */
1192 memcpy(sym
, syment
, sizeof(*syment
));
1194 /* Find the following symbol in order to get the current symbol size */
1195 for(j
= 0, sym_follow
= symtab_std
; j
< nb_syms
; j
++, sym_follow
++) {
1196 if ( sym_follow
->n_sect
!= 1 || sym_follow
->n_type
& N_STAB
|| !(sym_follow
->n_value
> sym
->st_value
))
1199 sym_next
= sym_follow
;
1202 if(!(sym_next
->n_value
> sym_follow
->n_value
))
1204 sym_next
= sym_follow
;
1207 sym
->st_size
= sym_next
->n_value
- sym
->st_value
;
1209 sym
->st_size
= text_sec_hdr
->size
- sym
->st_value
;
1213 relocs
= load_data(fd
, text_sec_hdr
->reloff
, text_sec_hdr
->nreloc
* sizeof(struct relocation_info
));
1214 nb_relocs
= text_sec_hdr
->nreloc
;
1220 #endif /* CONFIG_FORMAT_MACH */
1222 /* return true if the expression is a label reference */
1223 int get_reloc_expr(char *name
, int name_size
, const char *sym_name
)
1227 if (strstart(sym_name
, "__op_param", &p
)) {
1228 snprintf(name
, name_size
, "param%s", p
);
1229 } else if (strstart(sym_name
, "__op_gen_label", &p
)) {
1230 snprintf(name
, name_size
, "param%s", p
);
1233 #if defined(HOST_SPARC) || defined(HOST_HPPA)
1234 if (sym_name
[0] == '.')
1235 snprintf(name
, name_size
,
1236 "(long)(&__dot_%s)",
1240 snprintf(name
, name_size
, "(long)(&%s)", sym_name
);
1247 #define PLT_ENTRY_SIZE 16 /* 1 bundle containing "brl" */
1250 struct plt_entry
*next
;
1252 unsigned long addend
;
1256 get_plt_index (const char *name
, unsigned long addend
)
1258 struct plt_entry
*plt
, *prev
= NULL
;
1261 /* see if we already have an entry for this target: */
1262 for (plt
= plt_list
; plt
; ++index
, prev
= plt
, plt
= plt
->next
)
1263 if (strcmp(plt
->name
, name
) == 0 && plt
->addend
== addend
)
1266 /* nope; create a new PLT entry: */
1268 plt
= malloc(sizeof(*plt
));
1273 memset(plt
, 0, sizeof(*plt
));
1274 plt
->name
= strdup(name
);
1275 plt
->addend
= addend
;
1277 /* append to plt-list: */
1289 /* generate op code */
1290 void gen_code(const char *name
, host_ulong offset
, host_ulong size
,
1291 FILE *outfile
, int gen_switch
)
1294 uint8_t *p_start
, *p_end
;
1295 host_ulong start_offset
;
1297 uint8_t args_present
[MAX_ARGS
];
1298 const char *sym_name
, *p
;
1301 /* Compute exact size excluding prologue and epilogue instructions.
1302 * Increment start_offset to skip epilogue instructions, then compute
1303 * copy_size the indicate the size of the remaining instructions (in
1306 p_start
= text
+ offset
;
1307 p_end
= p_start
+ size
;
1308 start_offset
= offset
;
1309 #if defined(HOST_I386) || defined(HOST_X86_64)
1310 #ifdef CONFIG_FORMAT_COFF
1315 error("empty code for %s", name
);
1316 while (*p
!= 0xc3) {
1319 error("ret or jmp expected at the end of %s", name
);
1321 copy_size
= p
- p_start
;
1326 len
= p_end
- p_start
;
1328 error("empty code for %s", name
);
1329 if (p_end
[-1] == 0xc3) {
1332 error("ret or jmp expected at the end of %s", name
);
1337 #elif defined(HOST_PPC)
1340 p
= (void *)(p_end
- 4);
1342 error("empty code for %s", name
);
1343 if (get32((uint32_t *)p
) != 0x4e800020)
1344 error("blr expected at the end of %s", name
);
1345 copy_size
= p
- p_start
;
1347 #elif defined(HOST_S390)
1350 p
= (void *)(p_end
- 2);
1352 error("empty code for %s", name
);
1353 if ((get16((uint16_t *)p
) & 0xfff0) != 0x07f0)
1354 error("br expected at the end of %s", name
);
1355 copy_size
= p
- p_start
;
1357 #elif defined(HOST_ALPHA)
1362 /* XXX: check why it occurs */
1364 error("empty code for %s", name
);
1366 if (get32((uint32_t *)p
) != 0x6bfa8001)
1367 error("ret expected at the end of %s", name
);
1368 copy_size
= p
- p_start
;
1370 #elif defined(HOST_IA64)
1373 p
= (void *)(p_end
- 4);
1375 error("empty code for %s", name
);
1376 /* br.ret.sptk.many b0;; */
1378 if (get32((uint32_t *)p
) != 0x00840008)
1379 error("br.ret.sptk.many b0;; expected at the end of %s", name
);
1380 copy_size
= p_end
- p_start
;
1382 #elif defined(HOST_SPARC)
1384 #define INSN_SAVE 0x9de3a000
1385 #define INSN_RET 0x81c7e008
1386 #define INSN_RETL 0x81c3e008
1387 #define INSN_RESTORE 0x81e80000
1388 #define INSN_RETURN 0x81cfe008
1389 #define INSN_NOP 0x01000000
1390 #define INSN_ADD_SP 0x9c03a000 // add %sp, nn, %sp
1391 #define INSN_SUB_SP 0x9c23a000 // sub %sp, nn, %sp
1393 uint32_t start_insn
, end_insn1
, end_insn2
;
1395 p
= (void *)(p_end
- 8);
1397 error("empty code for %s", name
);
1398 start_insn
= get32((uint32_t *)(p_start
+ 0x0));
1399 end_insn1
= get32((uint32_t *)(p
+ 0x0));
1400 end_insn2
= get32((uint32_t *)(p
+ 0x4));
1401 if (((start_insn
& ~0x1fff) == INSN_SAVE
) ||
1402 (start_insn
& ~0x1fff) == INSN_ADD_SP
) {
1404 start_offset
+= 0x4;
1405 if (end_insn1
== INSN_RET
&& end_insn2
== INSN_RESTORE
)
1406 /* SPARC v7: ret; restore; */ ;
1407 else if (end_insn1
== INSN_RETURN
&& end_insn2
== INSN_NOP
)
1408 /* SPARC v9: return; nop; */ ;
1409 else if (end_insn1
== INSN_RETL
&& (end_insn2
& ~0x1fff) == INSN_SUB_SP
)
1410 /* SPARC v7: retl; sub %sp, nn, %sp; */ ;
1413 error("ret; restore; not found at end of %s", name
);
1414 } else if (end_insn1
== INSN_RETL
&& end_insn2
== INSN_NOP
) {
1417 error("No save at the beginning of %s", name
);
1420 /* Skip a preceeding nop, if present. */
1422 skip_insn
= get32((uint32_t *)(p
- 0x4));
1423 if (skip_insn
== INSN_NOP
)
1427 copy_size
= p
- p_start
;
1429 #elif defined(HOST_SPARC64)
1431 #define INSN_SAVE 0x9de3a000
1432 #define INSN_RET 0x81c7e008
1433 #define INSN_RETL 0x81c3e008
1434 #define INSN_RESTORE 0x81e80000
1435 #define INSN_RETURN 0x81cfe008
1436 #define INSN_NOP 0x01000000
1437 #define INSN_ADD_SP 0x9c03a000 // add %sp, nn, %sp
1438 #define INSN_SUB_SP 0x9c23a000 // sub %sp, nn, %sp
1440 uint32_t start_insn
, end_insn1
, end_insn2
, skip_insn
;
1442 p
= (void *)(p_end
- 8);
1444 /* XXX: check why it occurs */
1446 error("empty code for %s", name
);
1448 start_insn
= get32((uint32_t *)(p_start
+ 0x0));
1449 end_insn1
= get32((uint32_t *)(p
+ 0x0));
1450 end_insn2
= get32((uint32_t *)(p
+ 0x4));
1451 if (((start_insn
& ~0x1fff) == INSN_SAVE
) ||
1452 (start_insn
& ~0x1fff) == INSN_ADD_SP
) {
1454 start_offset
+= 0x4;
1455 if (end_insn1
== INSN_RET
&& end_insn2
== INSN_RESTORE
)
1456 /* SPARC v7: ret; restore; */ ;
1457 else if (end_insn1
== INSN_RETURN
&& end_insn2
== INSN_NOP
)
1458 /* SPARC v9: return; nop; */ ;
1459 else if (end_insn1
== INSN_RETL
&& (end_insn2
& ~0x1fff) == INSN_SUB_SP
)
1460 /* SPARC v7: retl; sub %sp, nn, %sp; */ ;
1463 error("ret; restore; not found at end of %s", name
);
1464 } else if (end_insn1
== INSN_RETL
&& end_insn2
== INSN_NOP
) {
1467 error("No save at the beginning of %s", name
);
1471 /* Skip a preceeding nop, if present. */
1473 skip_insn
= get32((uint32_t *)(p
- 0x4));
1474 if (skip_insn
== 0x01000000)
1479 copy_size
= p
- p_start
;
1481 #elif defined(HOST_M68K)
1484 p
= (void *)(p_end
- 2);
1486 error("empty code for %s", name
);
1487 // remove NOP's, probably added for alignment
1488 while ((get16((uint16_t *)p
) == 0x4e71) &&
1491 if (get16((uint16_t *)p
) != 0x4e75)
1492 error("rts expected at the end of %s", name
);
1493 copy_size
= p
- p_start
;
1495 #elif defined(HOST_HPPA)
1500 uint32_t insn
= get32((uint32_t *)p
);
1501 if (insn
== 0x6bc23fd9 || /* stw rp,-14(sp) */
1502 insn
== 0x08030241 || /* copy r3,r1 */
1503 insn
== 0x081e0243 || /* copy sp,r3 */
1504 (insn
& 0xffffc000) == 0x37de0000 || /* ldo x(sp),sp */
1505 (insn
& 0xffffc000) == 0x6fc10000) /* stwm r1,x(sp) */
1510 start_offset
+= p
- p_start
;
1514 while (p
> p_start
) {
1515 uint32_t insn
= get32((uint32_t *)p
);
1516 if ((insn
& 0xffffc000) == 0x347e0000 || /* ldo x(r3),sp */
1517 (insn
& 0xffe0c000) == 0x4fc00000 || /* ldwm x(sp),rx */
1518 (insn
& 0xffffc000) == 0x37de0000 || /* ldo x(sp),sp */
1519 insn
== 0x48623fd9 || /* ldw -14(r3),rp */
1520 insn
== 0xe840c000 || /* bv r0(rp) */
1521 insn
== 0xe840c002) /* bv,n r0(rp) */
1528 error("empty code for %s", name
);
1530 copy_size
= p
- p_start
;
1532 #elif defined(HOST_MIPS) || defined(HOST_MIPS64)
1534 #define INSN_RETURN 0x03e00008
1535 #define INSN_NOP 0x00000000
1539 if (p
< (p_start
+ 0x8)) {
1540 error("empty code for %s", name
);
1542 uint32_t end_insn1
, end_insn2
;
1545 end_insn1
= get32((uint32_t *)(p
+ 0x0));
1546 end_insn2
= get32((uint32_t *)(p
+ 0x4));
1547 if (end_insn1
!= INSN_RETURN
&& end_insn2
!= INSN_NOP
)
1548 error("jr ra not found at end of %s", name
);
1550 copy_size
= p
- p_start
;
1552 #elif defined(HOST_ARM)
1553 error("dyngen targets not supported on ARM");
1555 #error unsupported CPU
1558 /* compute the number of arguments by looking at the relocations */
1559 for(i
= 0;i
< MAX_ARGS
; i
++)
1560 args_present
[i
] = 0;
1562 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1563 host_ulong offset
= get_rel_offset(rel
);
1564 if (offset
>= start_offset
&&
1565 offset
< start_offset
+ (p_end
- p_start
)) {
1566 sym_name
= get_rel_sym_name(rel
);
1569 if (strstart(sym_name
, "__op_param", &p
) ||
1570 strstart(sym_name
, "__op_gen_label", &p
)) {
1571 n
= strtoul(p
, NULL
, 10);
1573 error("too many arguments in %s", name
);
1574 args_present
[n
- 1] = 1;
1580 while (nb_args
< MAX_ARGS
&& args_present
[nb_args
])
1582 for(i
= nb_args
; i
< MAX_ARGS
; i
++) {
1583 if (args_present
[i
])
1584 error("inconsistent argument numbering in %s", name
);
1587 if (gen_switch
== 2) {
1589 #if defined(HOST_HPPA)
1590 int op_size
= copy_size
;
1595 for (i
= 0, rel
= relocs
; i
< nb_relocs
; i
++, rel
++) {
1596 if (rel
->r_offset
>= start_offset
&&
1597 rel
->r_offset
< start_offset
+ copy_size
) {
1598 sym_name
= get_rel_sym_name(rel
);
1599 sym_name
= strtab
+ symtab
[ELF32_R_SYM(rel
->r_info
)].st_name
;
1600 is_label
= get_reloc_expr(relname
, sizeof(relname
), sym_name
);
1601 type
= ELF32_R_TYPE(rel
->r_info
);
1603 if (!is_label
&& type
== R_PARISC_PCREL17F
) {
1605 op_size
+= 8; /* ldil and be,n instructions */
1611 op_size
+= 4; /* b,l,n instruction, to skip past the stubs */
1613 fprintf(outfile
, "DEF(%s, %d, %d)\n", name
+ 3, nb_args
, op_size
);
1615 fprintf(outfile
, "DEF(%s, %d, %d)\n", name
+ 3, nb_args
, copy_size
);
1618 } else if (gen_switch
== 1) {
1621 fprintf(outfile
, "case INDEX_%s: {\n", name
);
1623 fprintf(outfile
, " long ");
1624 for(i
= 0; i
< nb_args
; i
++) {
1626 fprintf(outfile
, ", ");
1627 fprintf(outfile
, "param%d", i
+ 1);
1629 fprintf(outfile
, ";\n");
1631 #if defined(HOST_IA64)
1632 fprintf(outfile
, " extern char %s;\n", name
);
1634 fprintf(outfile
, " extern void %s();\n", name
);
1637 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1638 host_ulong offset
= get_rel_offset(rel
);
1639 if (offset
>= start_offset
&&
1640 offset
< start_offset
+ (p_end
- p_start
)) {
1641 sym_name
= get_rel_sym_name(rel
);
1645 !strstart(sym_name
, "__op_param", NULL
) &&
1646 !strstart(sym_name
, "__op_jmp", NULL
) &&
1647 !strstart(sym_name
, "__op_gen_label", NULL
)) {
1648 #if defined(HOST_SPARC) || defined(HOST_HPPA)
1649 if (sym_name
[0] == '.') {
1651 "extern char __dot_%s __asm__(\"%s\");\n",
1652 sym_name
+1, sym_name
);
1656 #if defined(__APPLE__)
1657 /* Set __attribute((unused)) on darwin because we
1658 want to avoid warning when we don't use the symbol. */
1659 fprintf(outfile
, " extern char %s __attribute__((unused));\n", sym_name
);
1660 #elif defined(HOST_IA64)
1661 if (ELF64_R_TYPE(rel
->r_info
) != R_IA64_PCREL21B
)
1663 * PCREL21 br.call targets generally
1664 * are out of range and need to go
1665 * through an "import stub".
1667 fprintf(outfile
, " extern char %s;\n",
1670 fprintf(outfile
, "extern char %s;\n", sym_name
);
1677 fprintf(outfile
, " memcpy(gen_code_ptr, (void *)((char *)__canonicalize_funcptr_for_compare(%s)+%d), %d);\n",
1678 name
, (int)(start_offset
- offset
), copy_size
);
1680 fprintf(outfile
, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n",
1681 name
, (int)(start_offset
- offset
), copy_size
);
1684 /* emit code offset information */
1687 const char *sym_name
, *p
;
1691 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
1692 sym_name
= get_sym_name(sym
);
1693 if (strstart(sym_name
, "__op_label", &p
)) {
1695 unsigned long offset
;
1697 /* test if the variable refers to a label inside
1698 the code we are generating */
1699 #ifdef CONFIG_FORMAT_COFF
1700 if (sym
->st_shndx
== text_shndx
) {
1701 ptr
= sdata
[coff_text_shndx
];
1702 } else if (sym
->st_shndx
== data_shndx
) {
1703 ptr
= sdata
[coff_data_shndx
];
1707 #elif defined(CONFIG_FORMAT_MACH)
1710 ptr
= sdata
[sym
->n_sect
-1];
1712 ptr
= sdata
[sym
->st_shndx
];
1715 error("__op_labelN in invalid section");
1716 offset
= sym
->st_value
;
1717 #ifdef CONFIG_FORMAT_MACH
1718 offset
-= section_hdr
[sym
->n_sect
-1].addr
;
1720 val
= *(host_ulong
*)(ptr
+ offset
);
1721 #ifdef ELF_USES_RELOCA
1723 int reloc_shndx
, nb_relocs1
, j
;
1725 /* try to find a matching relocation */
1726 reloc_shndx
= find_reloc(sym
->st_shndx
);
1728 nb_relocs1
= shdr
[reloc_shndx
].sh_size
/
1729 shdr
[reloc_shndx
].sh_entsize
;
1730 rel
= (ELF_RELOC
*)sdata
[reloc_shndx
];
1731 for(j
= 0; j
< nb_relocs1
; j
++) {
1732 if (rel
->r_offset
== offset
) {
1733 val
= rel
->r_addend
;
1741 if (val
>= start_offset
&& val
<= start_offset
+ copy_size
) {
1742 n
= strtol(p
, NULL
, 10);
1743 fprintf(outfile
, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n
, (long)(val
- start_offset
));
1749 /* load parameters in variables */
1750 for(i
= 0; i
< nb_args
; i
++) {
1751 fprintf(outfile
, " param%d = *opparam_ptr++;\n", i
+ 1);
1754 /* patch relocations */
1755 #if defined(HOST_I386)
1761 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1762 if (rel
->r_offset
>= start_offset
&&
1763 rel
->r_offset
< start_offset
+ copy_size
) {
1764 sym_name
= get_rel_sym_name(rel
);
1767 reloc_offset
= rel
->r_offset
- start_offset
;
1768 if (strstart(sym_name
, "__op_jmp", &p
)) {
1770 n
= strtol(p
, NULL
, 10);
1771 /* __op_jmp relocations are done at
1772 runtime to do translated block
1773 chaining: the offset of the instruction
1774 needs to be stored */
1775 fprintf(outfile
, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1780 is_label
= get_reloc_expr(relname
, sizeof(relname
), sym_name
);
1781 addend
= get32((uint32_t *)(text
+ rel
->r_offset
));
1782 #ifdef CONFIG_FORMAT_ELF
1783 type
= ELF32_R_TYPE(rel
->r_info
);
1788 fprintf(outfile
, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1789 reloc_offset
, type
, relname
, addend
);
1792 error("unsupported i386 relocation (%d)", type
);
1797 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1798 reloc_offset
, relname
, addend
);
1801 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1802 reloc_offset
, relname
, reloc_offset
, addend
);
1805 error("unsupported i386 relocation (%d)", type
);
1808 #elif defined(CONFIG_FORMAT_COFF)
1813 temp_name
= get_sym_name(symtab
+ *(uint32_t *)(rel
->r_reloc
->r_symndx
));
1814 if (!strcmp(temp_name
, ".data")) {
1815 for (j
= 0, sym
= symtab
; j
< nb_syms
; j
++, sym
++) {
1816 if (strstart(sym
->st_name
, sym_name
, NULL
)) {
1817 addend
-= sym
->st_value
;
1824 /* TCG uses elf relocation constants */
1826 #define R_386_PC32 2
1835 fprintf(outfile
, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1836 reloc_offset
, type
, relname
, addend
);
1839 error("unsupported i386 relocation (%d)", type
);
1844 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1845 reloc_offset
, relname
, addend
);
1848 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n",
1849 reloc_offset
, relname
, reloc_offset
, addend
);
1852 error("unsupported i386 relocation (%d)", type
);
1856 #error unsupport object format
1861 #elif defined(HOST_X86_64)
1867 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1868 if (rel
->r_offset
>= start_offset
&&
1869 rel
->r_offset
< start_offset
+ copy_size
) {
1870 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1871 is_label
= get_reloc_expr(relname
, sizeof(relname
), sym_name
);
1872 type
= ELF32_R_TYPE(rel
->r_info
);
1873 addend
= rel
->r_addend
;
1874 reloc_offset
= rel
->r_offset
- start_offset
;
1880 fprintf(outfile
, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1881 reloc_offset
, type
, relname
, addend
);
1884 error("unsupported X86_64 relocation (%d)", type
);
1889 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
1890 reloc_offset
, relname
, addend
);
1893 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
1894 reloc_offset
, relname
, addend
);
1897 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1898 reloc_offset
, relname
, reloc_offset
, addend
);
1901 error("unsupported X86_64 relocation (%d)", type
);
1907 #elif defined(HOST_PPC)
1909 #ifdef CONFIG_FORMAT_ELF
1915 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
1916 if (rel
->r_offset
>= start_offset
&&
1917 rel
->r_offset
< start_offset
+ copy_size
) {
1918 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
1919 reloc_offset
= rel
->r_offset
- start_offset
;
1920 if (strstart(sym_name
, "__op_jmp", &p
)) {
1922 n
= strtol(p
, NULL
, 10);
1923 /* __op_jmp relocations are done at
1924 runtime to do translated block
1925 chaining: the offset of the instruction
1926 needs to be stored */
1927 fprintf(outfile
, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1932 get_reloc_expr(relname
, sizeof(relname
), sym_name
);
1933 type
= ELF32_R_TYPE(rel
->r_info
);
1934 is_label
= get_reloc_expr(relname
, sizeof(relname
), sym_name
);
1935 addend
= rel
->r_addend
;
1939 fprintf (outfile
, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1940 reloc_offset
, type
, relname
, addend
);
1943 error ("unsupported ppc relocation (%d)", type
);
1949 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1950 reloc_offset
, relname
, addend
);
1952 case R_PPC_ADDR16_LO
:
1953 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
1954 reloc_offset
, relname
, addend
);
1956 case R_PPC_ADDR16_HI
:
1957 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
1958 reloc_offset
, relname
, addend
);
1960 case R_PPC_ADDR16_HA
:
1961 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
1962 reloc_offset
, relname
, addend
);
1965 /* warning: must be at 32 MB distancy */
1966 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
1967 reloc_offset
, reloc_offset
, relname
, reloc_offset
, addend
);
1970 error("unsupported powerpc relocation (%d)", type
);
1975 #elif defined(CONFIG_FORMAT_MACH)
1976 struct scattered_relocation_info
*scarel
;
1977 struct relocation_info
* rel
;
1978 char final_sym_name
[256];
1979 const char *sym_name
;
1984 for(i
= 0, rel
= relocs
; i
< nb_relocs
; i
++, rel
++) {
1985 unsigned int offset
, length
, value
= 0;
1986 unsigned int type
, pcrel
, isym
= 0;
1987 unsigned int usesym
= 0;
1989 if(R_SCATTERED
& rel
->r_address
) {
1990 scarel
= (struct scattered_relocation_info
*)rel
;
1991 offset
= (unsigned int)scarel
->r_address
;
1992 length
= scarel
->r_length
;
1993 pcrel
= scarel
->r_pcrel
;
1994 type
= scarel
->r_type
;
1995 value
= scarel
->r_value
;
1997 value
= isym
= rel
->r_symbolnum
;
1998 usesym
= (rel
->r_extern
);
1999 offset
= rel
->r_address
;
2000 length
= rel
->r_length
;
2001 pcrel
= rel
->r_pcrel
;
2005 slide
= offset
- start_offset
;
2007 if (!(offset
>= start_offset
&& offset
< start_offset
+ size
))
2008 continue; /* not in our range */
2010 sym_name
= get_reloc_name(rel
, &sslide
);
2012 if(usesym
&& symtab
[isym
].n_type
& N_STAB
)
2013 continue; /* don't handle STAB (debug sym) */
2015 if (sym_name
&& strstart(sym_name
, "__op_jmp", &p
)) {
2017 n
= strtol(p
, NULL
, 10);
2018 fprintf(outfile
, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
2020 continue; /* Nothing more to do */
2024 fprintf(outfile
, "/* #warning relocation not handled in %s (value 0x%x, %s, offset 0x%x, length 0x%x, %s, type 0x%x) */\n",
2025 name
, value
, usesym
? "use sym" : "don't use sym", offset
, length
, pcrel
? "pcrel":"", type
);
2026 continue; /* dunno how to handle without final_sym_name */
2029 get_reloc_expr(final_sym_name
, sizeof(final_sym_name
),
2032 case PPC_RELOC_BR24
:
2033 if (!strstart(sym_name
,"__op_gen_label",&p
)) {
2034 fprintf(outfile
, "{\n");
2035 fprintf(outfile
, " uint32_t imm = *(uint32_t *)(gen_code_ptr + %d) & 0x3fffffc;\n", slide
);
2036 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((imm + ((long)%s - (long)gen_code_ptr) + %d) & 0x03fffffc);\n",
2037 slide
, slide
, name
, sslide
);
2038 fprintf(outfile
, "}\n");
2040 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | (((long)%s - (long)gen_code_ptr - %d) & 0x03fffffc);\n",
2041 slide
, slide
, final_sym_name
, slide
);
2044 case PPC_RELOC_HI16
:
2045 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d) >> 16;\n",
2046 slide
, final_sym_name
, sslide
);
2048 case PPC_RELOC_LO16
:
2049 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d);\n",
2050 slide
, final_sym_name
, sslide
);
2052 case PPC_RELOC_HA16
:
2053 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d + 0x8000) >> 16;\n",
2054 slide
, final_sym_name
, sslide
);
2057 error("unsupported powerpc relocation (%d)", type
);
2061 #error unsupport object format
2064 #elif defined(HOST_S390)
2070 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
2071 if (rel
->r_offset
>= start_offset
&&
2072 rel
->r_offset
< start_offset
+ copy_size
) {
2073 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
2074 get_reloc_expr(relname
, sizeof(relname
), sym_name
);
2075 type
= ELF32_R_TYPE(rel
->r_info
);
2076 addend
= rel
->r_addend
;
2077 reloc_offset
= rel
->r_offset
- start_offset
;
2080 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2081 reloc_offset
, relname
, addend
);
2084 fprintf(outfile
, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
2085 reloc_offset
, relname
, addend
);
2088 fprintf(outfile
, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
2089 reloc_offset
, relname
, addend
);
2092 if (ELF32_ST_TYPE(symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_info
) == STT_SECTION
) {
2094 " *(uint32_t *)(gen_code_ptr + %d) += "
2095 "((long)&%s - (long)gen_code_ptr) >> 1;\n",
2096 reloc_offset
, name
);
2100 " *(uint32_t *)(gen_code_ptr + %d) = "
2101 "(%s + %d - ((uint32_t)gen_code_ptr + %d)) >> 1;\n",
2102 reloc_offset
, relname
, addend
, reloc_offset
);
2105 error("unsupported s390 relocation (%d)", type
);
2110 #elif defined(HOST_ALPHA)
2112 for (i
= 0, rel
= relocs
; i
< nb_relocs
; i
++, rel
++) {
2113 if (rel
->r_offset
>= start_offset
&& rel
->r_offset
< start_offset
+ copy_size
) {
2117 type
= ELF64_R_TYPE(rel
->r_info
);
2118 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
2119 reloc_offset
= rel
->r_offset
- start_offset
;
2121 case R_ALPHA_GPDISP
:
2122 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
2123 as an immediate instead of constructing it from the pv or ra. */
2124 fprintf(outfile
, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
2126 fprintf(outfile
, " immediate_lda(gen_code_ptr + %ld, gp);\n",
2127 reloc_offset
+ (int)rel
->r_addend
);
2129 case R_ALPHA_LITUSE
:
2130 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
2131 now, since some called functions (libc) need pv to be set up. */
2134 /* Branch target prediction hint. Ignore for now. Should be already
2135 correct for in-function jumps. */
2137 case R_ALPHA_LITERAL
:
2138 /* Load a literal from the GOT relative to the gp. Since there's only a
2139 single gp, nothing is to be done. */
2141 case R_ALPHA_GPRELHIGH
:
2142 /* Handle fake relocations against __op_param symbol. Need to emit the
2143 high part of the immediate value instead. Other symbols need no
2144 special treatment. */
2145 if (strstart(sym_name
, "__op_param", &p
))
2146 fprintf(outfile
, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
2149 case R_ALPHA_GPRELLOW
:
2150 if (strstart(sym_name
, "__op_param", &p
))
2151 fprintf(outfile
, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
2155 /* PC-relative jump. Tweak offset to skip the two instructions that try to
2156 set up the gp from the pv. */
2157 fprintf(outfile
, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
2158 reloc_offset
, sym_name
, reloc_offset
);
2161 error("unsupported Alpha relocation (%d)", type
);
2166 #elif defined(HOST_IA64)
2168 unsigned long sym_idx
;
2174 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
2175 sym_idx
= ELF64_R_SYM(rel
->r_info
);
2176 if (rel
->r_offset
< start_offset
2177 || rel
->r_offset
>= start_offset
+ copy_size
)
2179 sym_name
= (strtab
+ symtab
[sym_idx
].st_name
);
2180 code_offset
= rel
->r_offset
- start_offset
;
2181 if (strstart(sym_name
, "__op_jmp", &p
)) {
2183 n
= strtol(p
, NULL
, 10);
2184 /* __op_jmp relocations are done at
2185 runtime to do translated block
2186 chaining: the offset of the instruction
2187 needs to be stored */
2188 fprintf(outfile
, " jmp_offsets[%d] ="
2189 "%ld + (gen_code_ptr - gen_code_buf);\n",
2193 get_reloc_expr(relname
, sizeof(relname
), sym_name
);
2194 type
= ELF64_R_TYPE(rel
->r_info
);
2195 addend
= rel
->r_addend
;
2199 " ia64_imm64(gen_code_ptr + %ld, "
2201 code_offset
, relname
, addend
);
2203 case R_IA64_LTOFF22X
:
2204 case R_IA64_LTOFF22
:
2205 fprintf(outfile
, " IA64_LTOFF(gen_code_ptr + %ld,"
2206 " %s + %ld, %d);\n",
2207 code_offset
, relname
, addend
,
2208 (type
== R_IA64_LTOFF22X
));
2212 " ia64_ldxmov(gen_code_ptr + %ld,"
2213 " %s + %ld);\n", code_offset
, relname
, addend
);
2216 case R_IA64_PCREL21B
:
2217 if (strstart(sym_name
, "__op_gen_label", NULL
)) {
2219 " ia64_imm21b(gen_code_ptr + %ld,"
2220 " (long) (%s + %ld -\n\t\t"
2221 "((long) gen_code_ptr + %ld)) >> 4);\n",
2222 code_offset
, relname
, addend
,
2223 code_offset
& ~0xfUL
);
2226 " IA64_PLT(gen_code_ptr + %ld, "
2227 "%d);\t/* %s + %ld */\n",
2229 get_plt_index(sym_name
, addend
),
2234 error("unsupported ia64 relocation (0x%x)",
2238 fprintf(outfile
, " ia64_nop_b(gen_code_ptr + %d);\n",
2239 copy_size
- 16 + 2);
2241 #elif defined(HOST_SPARC)
2247 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
2248 if (rel
->r_offset
>= start_offset
&&
2249 rel
->r_offset
< start_offset
+ copy_size
) {
2250 sym_name
= strtab
+ symtab
[ELF32_R_SYM(rel
->r_info
)].st_name
;
2251 get_reloc_expr(relname
, sizeof(relname
), sym_name
);
2252 type
= ELF32_R_TYPE(rel
->r_info
);
2253 addend
= rel
->r_addend
;
2254 reloc_offset
= rel
->r_offset
- start_offset
;
2257 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2258 reloc_offset
, relname
, addend
);
2262 " *(uint32_t *)(gen_code_ptr + %d) = "
2263 "((*(uint32_t *)(gen_code_ptr + %d)) "
2265 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2266 reloc_offset
, reloc_offset
, relname
, addend
);
2270 " *(uint32_t *)(gen_code_ptr + %d) = "
2271 "((*(uint32_t *)(gen_code_ptr + %d)) "
2273 " | ((%s + %d) & 0x3ff);\n",
2274 reloc_offset
, reloc_offset
, relname
, addend
);
2276 case R_SPARC_WDISP30
:
2278 " *(uint32_t *)(gen_code_ptr + %d) = "
2279 "((*(uint32_t *)(gen_code_ptr + %d)) "
2281 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2282 " & 0x3fffffff);\n",
2283 reloc_offset
, reloc_offset
, relname
, addend
,
2286 case R_SPARC_WDISP22
:
2288 " *(uint32_t *)(gen_code_ptr + %d) = "
2289 "((*(uint32_t *)(gen_code_ptr + %d)) "
2291 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2293 rel
->r_offset
- start_offset
,
2294 rel
->r_offset
- start_offset
,
2296 rel
->r_offset
- start_offset
);
2299 error("unsupported sparc relocation (%d)", type
);
2304 #elif defined(HOST_SPARC64)
2310 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
2311 if (rel
->r_offset
>= start_offset
&&
2312 rel
->r_offset
< start_offset
+ copy_size
) {
2313 sym_name
= strtab
+ symtab
[ELF64_R_SYM(rel
->r_info
)].st_name
;
2314 get_reloc_expr(relname
, sizeof(relname
), sym_name
);
2315 type
= ELF32_R_TYPE(rel
->r_info
);
2316 addend
= rel
->r_addend
;
2317 reloc_offset
= rel
->r_offset
- start_offset
;
2320 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2321 reloc_offset
, relname
, addend
);
2325 " *(uint32_t *)(gen_code_ptr + %d) = "
2326 "((*(uint32_t *)(gen_code_ptr + %d)) "
2328 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2329 reloc_offset
, reloc_offset
, relname
, addend
);
2333 " *(uint32_t *)(gen_code_ptr + %d) = "
2334 "((*(uint32_t *)(gen_code_ptr + %d)) "
2336 " | ((%s + %d) & 0x3ff);\n",
2337 reloc_offset
, reloc_offset
, relname
, addend
);
2340 addend
+= ELF64_R_TYPE_DATA (rel
->r_info
);
2342 " *(uint32_t *)(gen_code_ptr + %d) = "
2343 "((*(uint32_t *)(gen_code_ptr + %d)) "
2345 " | ((%s + %d) & 0x3ff);\n",
2346 reloc_offset
, reloc_offset
, relname
, addend
);
2348 case R_SPARC_WDISP30
:
2350 " *(uint32_t *)(gen_code_ptr + %d) = "
2351 "((*(uint32_t *)(gen_code_ptr + %d)) "
2353 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2354 " & 0x3fffffff);\n",
2355 reloc_offset
, reloc_offset
, relname
, addend
,
2358 case R_SPARC_WDISP22
:
2360 " *(uint32_t *)(gen_code_ptr + %d) = "
2361 "((*(uint32_t *)(gen_code_ptr + %d)) "
2363 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2365 reloc_offset
, reloc_offset
, relname
, addend
,
2370 " *(uint32_t *)(gen_code_ptr + %d) = "
2371 "((*(uint32_t *)(gen_code_ptr + %d)) "
2373 " | (((%s + %d) >> 42) & 0x00000000);\n",
2374 reloc_offset
, reloc_offset
, relname
, addend
);
2379 " *(uint32_t *)(gen_code_ptr + %d) = "
2380 "((*(uint32_t *)(gen_code_ptr + %d)) "
2382 " | (((%s + %d) >> 10) & 0x00000000);\n",
2383 reloc_offset
, reloc_offset
, relname
, addend
);
2388 " *(uint32_t *)(gen_code_ptr + %d) = "
2389 "((*(uint32_t *)(gen_code_ptr + %d)) "
2391 " | ((((%s + %d) >> 32 & 0x3ff)) & 0x00000000);\n",
2392 reloc_offset
, reloc_offset
, relname
, addend
);
2396 error("unsupported sparc64 relocation (%d) for symbol %s", type
, relname
);
2401 #elif defined(HOST_M68K)
2408 for(i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
2409 if (rel
->r_offset
>= start_offset
&&
2410 rel
->r_offset
< start_offset
+ copy_size
) {
2411 sym
= &(symtab
[ELFW(R_SYM
)(rel
->r_info
)]);
2412 sym_name
= strtab
+ symtab
[ELFW(R_SYM
)(rel
->r_info
)].st_name
;
2413 get_reloc_expr(relname
, sizeof(relname
), sym_name
);
2414 type
= ELF32_R_TYPE(rel
->r_info
);
2415 addend
= get32((uint32_t *)(text
+ rel
->r_offset
)) + rel
->r_addend
;
2416 reloc_offset
= rel
->r_offset
- start_offset
;
2419 fprintf(outfile
, " /* R_68K_32 RELOC, offset %x */\n", rel
->r_offset
) ;
2420 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
2421 reloc_offset
, relname
, addend
);
2424 fprintf(outfile
, " /* R_68K_PC32 RELOC, offset %x */\n", rel
->r_offset
);
2425 fprintf(outfile
, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
2426 reloc_offset
, relname
, reloc_offset
, /*sym->st_value+*/ addend
);
2429 error("unsupported m68k relocation (%d)", type
);
2434 #elif defined(HOST_HPPA)
2440 for (i
= 0, rel
= relocs
; i
< nb_relocs
; i
++, rel
++) {
2441 if (rel
->r_offset
>= start_offset
&&
2442 rel
->r_offset
< start_offset
+ copy_size
) {
2443 sym_name
= get_rel_sym_name(rel
);
2444 sym_name
= strtab
+ symtab
[ELF32_R_SYM(rel
->r_info
)].st_name
;
2445 is_label
= get_reloc_expr(relname
, sizeof(relname
), sym_name
);
2446 type
= ELF32_R_TYPE(rel
->r_info
);
2447 addend
= rel
->r_addend
;
2448 reloc_offset
= rel
->r_offset
- start_offset
;
2452 case R_PARISC_PCREL17F
:
2454 " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
2455 reloc_offset
, type
, relname
, addend
);
2458 error("unsupported hppa label relocation (%d)", type
);
2462 case R_PARISC_DIR21L
:
2464 " hppa_patch21l((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2465 reloc_offset
, relname
, addend
);
2467 case R_PARISC_DIR14R
:
2469 " hppa_patch14r((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2470 reloc_offset
, relname
, addend
);
2472 case R_PARISC_PCREL17F
:
2473 if (strstart(sym_name
, "__op_gen_label", NULL
)) {
2475 " hppa_patch17f((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2476 reloc_offset
, relname
, addend
);
2479 " HPPA_RECORD_BRANCH(hppa_stubs, (uint32_t *)(gen_code_ptr + %d), %s);\n",
2480 reloc_offset
, relname
);
2483 case R_PARISC_DPREL21L
:
2484 if (strstart(sym_name
, "__op_param", &p
))
2486 " hppa_load_imm21l((uint32_t *)(gen_code_ptr + %d), param%s, %d);\n",
2487 reloc_offset
, p
, addend
);
2490 " hppa_patch21l_dprel((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2491 reloc_offset
, relname
, addend
);
2493 case R_PARISC_DPREL14R
:
2494 if (strstart(sym_name
, "__op_param", &p
))
2496 " hppa_load_imm14r((uint32_t *)(gen_code_ptr + %d), param%s, %d);\n",
2497 reloc_offset
, p
, addend
);
2500 " hppa_patch14r_dprel((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2501 reloc_offset
, relname
, addend
);
2504 error("unsupported hppa relocation (%d)", type
);
2510 #elif defined(HOST_MIPS) || defined(HOST_MIPS64)
2512 for (i
= 0, rel
= relocs
; i
< nb_relocs
; i
++, rel
++) {
2513 if (rel
->r_offset
>= start_offset
&& rel
->r_offset
< start_offset
+ copy_size
) {
2519 sym_name
= strtab
+ symtab
[ELF32_R_SYM(rel
->r_info
)].st_name
;
2520 /* the compiler leave some unnecessary references to the code */
2521 if (sym_name
[0] == '\0')
2523 get_reloc_expr(relname
, sizeof(relname
), sym_name
);
2524 type
= ELF32_R_TYPE(rel
->r_info
);
2525 addend
= get32((uint32_t *)(text
+ rel
->r_offset
));
2526 reloc_offset
= rel
->r_offset
- start_offset
;
2529 fprintf(outfile
, " /* R_MIPS_26 RELOC, offset 0x%x, name %s */\n",
2530 rel
->r_offset
, sym_name
);
2532 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2533 "(0x%x & ~0x3fffff) "
2534 "| ((0x%x + ((%s - (*(uint32_t *)(gen_code_ptr + 0x%x))) >> 2)) "
2536 reloc_offset
, addend
, addend
, relname
, reloc_offset
);
2539 fprintf(outfile
, " /* R_MIPS_HI16 RELOC, offset 0x%x, name %s */\n",
2540 rel
->r_offset
, sym_name
);
2542 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2543 "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
2545 " | (((%s - 0x8000) >> 16) & 0xffff);\n",
2546 reloc_offset
, reloc_offset
, relname
);
2549 fprintf(outfile
, " /* R_MIPS_LO16 RELOC, offset 0x%x, name %s */\n",
2550 rel
->r_offset
, sym_name
);
2552 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2553 "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
2555 " | (%s & 0xffff);\n",
2556 reloc_offset
, reloc_offset
, relname
);
2559 fprintf(outfile
, " /* R_MIPS_PC16 RELOC, offset 0x%x, name %s */\n",
2560 rel
->r_offset
, sym_name
);
2562 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2564 "| ((0x%x + ((%s - (*(uint32_t *)(gen_code_ptr + 0x%x))) >> 2)) "
2566 reloc_offset
, addend
, addend
, relname
, reloc_offset
);
2570 fprintf(outfile
, " /* R_MIPS_GOT16 RELOC, offset 0x%x, name %s */\n",
2571 rel
->r_offset
, sym_name
);
2573 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2574 "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
2576 " | (((%s - 0x8000) >> 16) & 0xffff);\n",
2577 reloc_offset
, reloc_offset
, relname
);
2580 error("unsupported MIPS relocation (%d)", type
);
2585 #elif defined(HOST_ARM)
2586 error("dyngen targets not supported on ARM");
2588 #error unsupported CPU
2590 fprintf(outfile
, " gen_code_ptr += %d;\n", copy_size
);
2591 fprintf(outfile
, "}\n");
2592 fprintf(outfile
, "break;\n\n");
2594 fprintf(outfile
, "static inline void gen_%s(", name
);
2596 fprintf(outfile
, "void");
2598 for(i
= 0; i
< nb_args
; i
++) {
2600 fprintf(outfile
, ", ");
2601 fprintf(outfile
, "long param%d", i
+ 1);
2604 fprintf(outfile
, ")\n");
2605 fprintf(outfile
, "{\n");
2606 for(i
= 0; i
< nb_args
; i
++) {
2607 fprintf(outfile
, " *gen_opparam_ptr++ = param%d;\n", i
+ 1);
2609 fprintf(outfile
, " *gen_opc_ptr++ = INDEX_%s;\n", name
);
2610 fprintf(outfile
, "}\n\n");
2614 int gen_file(FILE *outfile
, int out_type
)
2619 if (out_type
== OUT_INDEX_OP
) {
2620 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
2622 name
= get_sym_name(sym
);
2623 if (strstart(name
, OP_PREFIX
, NULL
)) {
2624 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
, 2);
2627 } else if (out_type
== OUT_GEN_OP
) {
2628 /* generate gen_xxx functions */
2629 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
2631 name
= get_sym_name(sym
);
2632 if (strstart(name
, OP_PREFIX
, NULL
)) {
2633 #if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
2634 if (sym
->st_shndx
!= text_shndx
)
2635 error("invalid section for opcode (0x%x)", sym
->st_shndx
);
2637 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
, 0);
2642 /* generate big code generation switch */
2645 error("dyngen targets not supported on ARM");
2650 long addend
, not_first
= 0;
2651 unsigned long sym_idx
;
2652 int index
, max_index
;
2653 const char *sym_name
;
2657 for (i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
2658 sym_idx
= ELF64_R_SYM(rel
->r_info
);
2659 sym_name
= (strtab
+ symtab
[sym_idx
].st_name
);
2660 if (strstart(sym_name
, "__op_gen_label", NULL
))
2662 if (ELF64_R_TYPE(rel
->r_info
) != R_IA64_PCREL21B
)
2665 addend
= rel
->r_addend
;
2666 index
= get_plt_index(sym_name
, addend
);
2667 if (index
<= max_index
)
2670 fprintf(outfile
, " extern void %s(void);\n", sym_name
);
2674 " struct ia64_fixup *plt_fixes = NULL, "
2675 "*ltoff_fixes = NULL;\n"
2676 " static long plt_target[] = {\n\t");
2679 for (i
= 0, rel
= relocs
;i
< nb_relocs
; i
++, rel
++) {
2680 sym_idx
= ELF64_R_SYM(rel
->r_info
);
2681 sym_name
= (strtab
+ symtab
[sym_idx
].st_name
);
2682 if (strstart(sym_name
, "__op_gen_label", NULL
))
2684 if (ELF64_R_TYPE(rel
->r_info
) != R_IA64_PCREL21B
)
2687 addend
= rel
->r_addend
;
2688 index
= get_plt_index(sym_name
, addend
);
2689 if (index
<= max_index
)
2694 fprintf(outfile
, ",\n\t");
2697 fprintf(outfile
, "(long) &%s + %ld", sym_name
, addend
);
2699 fprintf(outfile
, "(long) &%s", sym_name
);
2701 fprintf(outfile
, "\n };\n"
2702 " unsigned int plt_offset[%u] = { 0 };\n", max_index
+ 1);
2706 for(i
= 0, sym
= symtab
; i
< nb_syms
; i
++, sym
++) {
2708 name
= get_sym_name(sym
);
2709 if (strstart(name
, OP_PREFIX
, NULL
)) {
2711 printf("%4d: %s pos=0x%08x len=%d\n",
2712 i
, name
, sym
->st_value
, sym
->st_size
);
2714 #if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
2715 if (sym
->st_shndx
!= text_shndx
)
2716 error("invalid section for opcode (0x%x)", sym
->st_shndx
);
2718 gen_code(name
, sym
->st_value
, sym
->st_size
, outfile
, 1);
2728 printf("dyngen (c) 2003 Fabrice Bellard\n"
2729 "usage: dyngen [-o outfile] [-c] objfile\n"
2730 "Generate a dynamic code generator from an object file\n"
2731 "-c output enum of operations\n"
2732 "-g output gen_op_xx() functions\n"
2737 int main(int argc
, char **argv
)
2740 const char *filename
, *outfilename
;
2743 outfilename
= "out.c";
2744 out_type
= OUT_CODE
;
2746 c
= getopt(argc
, argv
, "ho:cg");
2754 outfilename
= optarg
;
2757 out_type
= OUT_INDEX_OP
;
2760 out_type
= OUT_GEN_OP
;
2766 filename
= argv
[optind
];
2767 outfile
= fopen(outfilename
, "w");
2769 error("could not open '%s'", outfilename
);
2771 load_object(filename
);
2772 gen_file(outfile
, out_type
);