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_PPC64)
73 #define ELF_CLASS ELFCLASS64
74 #define ELF_ARCH EM_PPC64
75 #define elf_check_arch(x) ((x) == EM_PPC64)
76 #define ELF_USES_RELOCA
78 #elif defined(HOST_S390)
80 #define ELF_CLASS ELFCLASS32
81 #define ELF_ARCH EM_S390
82 #define elf_check_arch(x) ((x) == EM_S390)
83 #define ELF_USES_RELOCA
85 #elif defined(HOST_ALPHA)
87 #define ELF_CLASS ELFCLASS64
88 #define ELF_ARCH EM_ALPHA
89 #define elf_check_arch(x) ((x) == EM_ALPHA)
90 #define ELF_USES_RELOCA
92 #elif defined(HOST_IA64)
94 #define ELF_CLASS ELFCLASS64
95 #define ELF_ARCH EM_IA_64
96 #define elf_check_arch(x) ((x) == EM_IA_64)
97 #define ELF_USES_RELOCA
99 #elif defined(HOST_SPARC)
101 #define ELF_CLASS ELFCLASS32
102 #define ELF_ARCH EM_SPARC
103 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
104 #define ELF_USES_RELOCA
106 #elif defined(HOST_SPARC64)
108 #define ELF_CLASS ELFCLASS64
109 #define ELF_ARCH EM_SPARCV9
110 #define elf_check_arch(x) ((x) == EM_SPARCV9)
111 #define ELF_USES_RELOCA
113 #elif defined(HOST_ARM)
115 #define ELF_CLASS ELFCLASS32
116 #define ELF_ARCH EM_ARM
117 #define elf_check_arch(x) ((x) == EM_ARM)
118 #define ELF_USES_RELOC
120 #elif defined(HOST_M68K)
122 #define ELF_CLASS ELFCLASS32
123 #define ELF_ARCH EM_68K
124 #define elf_check_arch(x) ((x) == EM_68K)
125 #define ELF_USES_RELOCA
127 #elif defined(HOST_HPPA)
129 #define ELF_CLASS ELFCLASS32
130 #define ELF_ARCH EM_PARISC
131 #define elf_check_arch(x) ((x) == EM_PARISC)
132 #define ELF_USES_RELOCA
134 #elif defined(HOST_MIPS)
136 #define ELF_CLASS ELFCLASS32
137 #define ELF_ARCH EM_MIPS
138 #define elf_check_arch(x) ((x) == EM_MIPS)
139 #define ELF_USES_RELOC
141 #elif defined(HOST_MIPS64)
143 /* Assume n32 ABI here, which is ELF32. */
144 #define ELF_CLASS ELFCLASS32
145 #define ELF_ARCH EM_MIPS
146 #define elf_check_arch(x) ((x) == EM_MIPS)
147 #define ELF_USES_RELOCA
150 #error unsupported CPU - please update the code
155 #if ELF_CLASS == ELFCLASS32
156 typedef int32_t host_long;
157 typedef uint32_t host_ulong;
158 #define swabls(x) swab32s(x)
159 #define swablss(x) swab32ss(x)
161 typedef int64_t host_long;
162 typedef uint64_t host_ulong;
163 #define swabls(x) swab64s(x)
164 #define swablss(x) swab64ss(x)
167 #ifdef ELF_USES_RELOCA
168 #define SHT_RELOC SHT_RELA
170 #define SHT_RELOC SHT_REL
173 #define EXE_RELOC ELF_RELOC
174 #define EXE_SYM ElfW(Sym)
176 #endif /* CONFIG_FORMAT_ELF */
178 #ifdef CONFIG_FORMAT_COFF
180 typedef int32_t host_long;
181 typedef uint32_t host_ulong;
185 #define FILENAMELEN 256
187 typedef struct coff_sym {
188 struct external_syment *st_syment;
189 char st_name[FILENAMELEN];
196 typedef struct coff_rel {
197 struct external_reloc *r_reloc;
202 #define EXE_RELOC struct coff_rel
203 #define EXE_SYM struct coff_sym
205 #endif /* CONFIG_FORMAT_COFF */
207 #ifdef CONFIG_FORMAT_MACH
209 #include <mach-o/loader.h>
210 #include <mach-o/nlist.h>
211 #include <mach-o/reloc.h>
212 #include <mach-o/ppc/reloc.h>
214 # define check_mach_header(x) (x.magic == MH_MAGIC)
215 typedef int32_t host_long;
216 typedef uint32_t host_ulong;
218 struct nlist_extended
224 unsigned char n_type;
225 unsigned char n_sect;
227 unsigned long st_value;
228 unsigned long st_size;
231 #define EXE_RELOC struct relocation_info
232 #define EXE_SYM struct nlist_extended
234 #endif /* CONFIG_FORMAT_MACH */
244 /* all dynamically generated functions begin with this code */
245 #define OP_PREFIX "op_"
249 static void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
253 fprintf(stderr, "dyngen: ");
254 vfprintf(stderr, fmt, ap);
255 fprintf(stderr, "\n");
260 static void *load_data(int fd, long offset, unsigned int size)
267 lseek(fd, offset, SEEK_SET);
268 if (read(fd, data, size) != size) {
275 static int strstart(const char *str, const char *val, const char **ptr)
291 static void pstrcpy(char *buf, int buf_size, const char *str)
301 if (c == 0 || q >= buf + buf_size - 1)
308 static void swab16s(uint16_t *p)
313 static void swab32s(uint32_t *p)
318 static void swab32ss(int32_t *p)
323 static void swab64s(uint64_t *p)
328 static void swab64ss(int64_t *p)
333 static uint16_t get16(uint16_t *p)
342 static uint32_t get32(uint32_t *p)
351 static void put16(uint16_t *p, uint16_t val)
358 static void put32(uint32_t *p, uint32_t val)
365 /* executable information */
373 #ifdef CONFIG_FORMAT_ELF
376 struct elf_shdr *shdr;
381 static int elf_must_swap(struct elfhdr *h)
389 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
390 (swaptest.b[0] == 0);
393 static void elf_swap_ehdr(struct elfhdr *h)
395 swab16s(&h->e_type); /* Object file type */
396 swab16s(&h-> e_machine); /* Architecture */
397 swab32s(&h-> e_version); /* Object file version */
398 swabls(&h-> e_entry); /* Entry point virtual address */
399 swabls(&h-> e_phoff); /* Program header table file offset */
400 swabls(&h-> e_shoff); /* Section header table file offset */
401 swab32s(&h-> e_flags); /* Processor-specific flags */
402 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
403 swab16s(&h-> e_phentsize); /* Program header table entry size */
404 swab16s(&h-> e_phnum); /* Program header table entry count */
405 swab16s(&h-> e_shentsize); /* Section header table entry size */
406 swab16s(&h-> e_shnum); /* Section header table entry count */
407 swab16s(&h-> e_shstrndx); /* Section header string table index */
410 static void elf_swap_shdr(struct elf_shdr *h)
412 swab32s(&h-> sh_name); /* Section name (string tbl index) */
413 swab32s(&h-> sh_type); /* Section type */
414 swabls(&h-> sh_flags); /* Section flags */
415 swabls(&h-> sh_addr); /* Section virtual addr at execution */
416 swabls(&h-> sh_offset); /* Section file offset */
417 swabls(&h-> sh_size); /* Section size in bytes */
418 swab32s(&h-> sh_link); /* Link to another section */
419 swab32s(&h-> sh_info); /* Additional section information */
420 swabls(&h-> sh_addralign); /* Section alignment */
421 swabls(&h-> sh_entsize); /* Entry size if section holds table */
424 static void elf_swap_phdr(struct elf_phdr *h)
426 swab32s(&h->p_type); /* Segment type */
427 swabls(&h->p_offset); /* Segment file offset */
428 swabls(&h->p_vaddr); /* Segment virtual address */
429 swabls(&h->p_paddr); /* Segment physical address */
430 swabls(&h->p_filesz); /* Segment size in file */
431 swabls(&h->p_memsz); /* Segment size in memory */
432 swab32s(&h->p_flags); /* Segment flags */
433 swabls(&h->p_align); /* Segment alignment */
436 static void elf_swap_rel(ELF_RELOC *rel)
438 swabls(&rel->r_offset);
439 swabls(&rel->r_info);
440 #ifdef ELF_USES_RELOCA
441 swablss(&rel->r_addend);
445 static struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum,
446 const char *shstr, const char *name)
450 struct elf_shdr *sec;
452 for(i = 0; i < shnum; i++) {
456 shname = shstr + sec->sh_name;
457 if (!strcmp(shname, name))
463 static int find_reloc(int sh_index)
465 struct elf_shdr *sec;
468 for(i = 0; i < ehdr.e_shnum; i++) {
470 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
476 static host_ulong get_rel_offset(EXE_RELOC *rel)
478 return rel->r_offset;
481 static char *get_rel_sym_name(EXE_RELOC *rel)
483 return strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
486 static char *get_sym_name(EXE_SYM *sym)
488 return strtab + sym->st_name;
491 /* load an elf object file */
492 static int load_object(const char *filename)
495 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
501 fd = open(filename, O_RDONLY);
503 error("can't open file '%s'", filename);
505 /* Read ELF header. */
506 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
507 error("unable to read file header");
509 /* Check ELF identification. */
510 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
511 || ehdr.e_ident[EI_MAG1] != ELFMAG1
512 || ehdr.e_ident[EI_MAG2] != ELFMAG2
513 || ehdr.e_ident[EI_MAG3] != ELFMAG3
514 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
515 error("bad ELF header");
518 do_swap = elf_must_swap(&ehdr);
520 elf_swap_ehdr(&ehdr);
521 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
522 error("Unsupported ELF class");
523 if (ehdr.e_type != ET_REL)
524 error("ELF object file expected");
525 if (ehdr.e_version != EV_CURRENT)
526 error("Invalid ELF version");
527 if (!elf_check_arch(ehdr.e_machine))
528 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
530 /* read section headers */
531 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
533 for(i = 0; i < ehdr.e_shnum; i++) {
534 elf_swap_shdr(&shdr[i]);
538 /* read all section data */
539 sdata = malloc(sizeof(void *) * ehdr.e_shnum);
540 memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
542 for(i = 0;i < ehdr.e_shnum; i++) {
544 if (sec->sh_type != SHT_NOBITS)
545 sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
548 sec = &shdr[ehdr.e_shstrndx];
549 shstr = (char *)sdata[ehdr.e_shstrndx];
551 /* swap relocations */
552 for(i = 0; i < ehdr.e_shnum; i++) {
554 if (sec->sh_type == SHT_RELOC) {
555 nb_relocs = sec->sh_size / sec->sh_entsize;
557 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
564 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
566 error("could not find .text section");
567 text_shndx = text_sec - shdr;
568 text = sdata[text_shndx];
570 /* find text relocations, if any */
573 i = find_reloc(text_shndx);
575 relocs = (ELF_RELOC *)sdata[i];
576 nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
579 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
581 error("could not find .symtab section");
582 strtab_sec = &shdr[symtab_sec->sh_link];
584 symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
585 strtab = (char *)sdata[symtab_sec->sh_link];
587 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
589 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
590 swab32s(&sym->st_name);
591 swabls(&sym->st_value);
592 swabls(&sym->st_size);
593 swab16s(&sym->st_shndx);
600 #endif /* CONFIG_FORMAT_ELF */
602 #ifdef CONFIG_FORMAT_COFF
605 struct external_scnhdr *shdr;
607 struct external_filehdr fhdr;
608 struct external_syment *coff_symtab;
610 int coff_text_shndx, coff_data_shndx;
614 #define STRTAB_SIZE 4
619 #define T_FUNCTION 0x20
622 void sym_ent_name(struct external_syment *ext_sym, EXE_SYM *sym)
627 if (ext_sym->e.e.e_zeroes != 0) {
629 for(i = 0; i < 8; i++) {
630 c = ext_sym->e.e_name[i];
637 pstrcpy(sym->st_name, sizeof(sym->st_name), strtab + ext_sym->e.e.e_offset);
640 /* now convert the name to a C name (suppress the leading '_') */
641 if (sym->st_name[0] == '_') {
642 len = strlen(sym->st_name);
643 memmove(sym->st_name, sym->st_name + 1, len - 1);
644 sym->st_name[len - 1] = '\0';
648 char *name_for_dotdata(struct coff_rel *rel)
651 struct coff_sym *sym;
654 text_data = *(uint32_t *)(text + rel->r_offset);
656 for (i = 0, sym = symtab; i < nb_syms; i++, sym++) {
657 if (sym->st_syment->e_scnum == data_shndx &&
658 text_data >= sym->st_value &&
659 text_data < sym->st_value + sym->st_size) {
668 static char *get_sym_name(EXE_SYM *sym)
673 static char *get_rel_sym_name(EXE_RELOC *rel)
676 name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx));
677 if (!strcmp(name, ".data"))
678 name = name_for_dotdata(rel);
684 static host_ulong get_rel_offset(EXE_RELOC *rel)
686 return rel->r_offset;
689 struct external_scnhdr *find_coff_section(struct external_scnhdr *shdr, int shnum, const char *name)
693 struct external_scnhdr *sec;
695 for(i = 0; i < shnum; i++) {
699 shname = sec->s_name;
700 if (!strcmp(shname, name))
706 /* load a coff object file */
707 int load_object(const char *filename)
710 struct external_scnhdr *sec, *text_sec, *data_sec;
712 struct external_syment *ext_sym;
713 struct external_reloc *coff_relocs;
714 struct external_reloc *ext_rel;
721 fd = open(filename, O_RDONLY
727 error("can't open file '%s'", filename);
729 /* Read COFF header. */
730 if (read(fd, &fhdr, sizeof (fhdr)) != sizeof (fhdr))
731 error("unable to read file header");
733 /* Check COFF identification. */
734 if (fhdr.f_magic != I386MAGIC) {
735 error("bad COFF header");
739 /* read section headers */
740 shdr = load_data(fd, sizeof(struct external_filehdr) + fhdr.f_opthdr, fhdr.f_nscns * sizeof(struct external_scnhdr));
742 /* read all section data */
743 sdata = malloc(sizeof(void *) * fhdr.f_nscns);
744 memset(sdata, 0, sizeof(void *) * fhdr.f_nscns);
746 for(i = 0;i < fhdr.f_nscns; i++) {
748 if (!strstart(sec->s_name, ".bss", &p))
749 sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size);
754 text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text");
756 error("could not find .text section");
757 coff_text_shndx = text_sec - shdr;
758 text = sdata[coff_text_shndx];
761 data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data");
763 error("could not find .data section");
764 coff_data_shndx = data_sec - shdr;
766 coff_symtab = load_data(fd, fhdr.f_symptr, fhdr.f_nsyms*SYMESZ);
767 for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
769 printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]);
774 n_strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), STRTAB_SIZE);
775 strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), *n_strtab);
777 nb_syms = fhdr.f_nsyms;
779 for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
780 if (strstart(ext_sym->e.e_name, ".text", NULL))
781 text_shndx = ext_sym->e_scnum;
782 if (strstart(ext_sym->e.e_name, ".data", NULL))
783 data_shndx = ext_sym->e_scnum;
786 /* set coff symbol */
787 symtab = malloc(sizeof(struct coff_sym) * nb_syms);
789 for (i = 0, ext_sym = coff_symtab, sym = symtab; i < nb_syms; i++, ext_sym++, sym++) {
790 memset(sym, 0, sizeof(*sym));
791 sym->st_syment = ext_sym;
792 sym_ent_name(ext_sym, sym);
793 sym->st_value = ext_sym->e_value;
795 aux_size = *(int8_t *)ext_sym->e_numaux;
796 if (ext_sym->e_scnum == text_shndx && ext_sym->e_type == T_FUNCTION) {
797 for (j = aux_size + 1; j < nb_syms - i; j++) {
798 if ((ext_sym + j)->e_scnum == text_shndx &&
799 (ext_sym + j)->e_type == T_FUNCTION ){
800 sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
802 } else if (j == nb_syms - i - 1) {
803 sec = &shdr[coff_text_shndx];
804 sym->st_size = sec->s_size - ext_sym->e_value;
808 } else if (ext_sym->e_scnum == data_shndx && *(uint8_t *)ext_sym->e_sclass == C_EXTERNAL) {
809 for (j = aux_size + 1; j < nb_syms - i; j++) {
810 if ((ext_sym + j)->e_scnum == data_shndx) {
811 sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
813 } else if (j == nb_syms - i - 1) {
814 sec = &shdr[coff_data_shndx];
815 sym->st_size = sec->s_size - ext_sym->e_value;
823 sym->st_type = ext_sym->e_type;
824 sym->st_shndx = ext_sym->e_scnum;
828 /* find text relocations, if any */
829 sec = &shdr[coff_text_shndx];
830 coff_relocs = load_data(fd, sec->s_relptr, sec->s_nreloc*RELSZ);
831 nb_relocs = sec->s_nreloc;
833 /* set coff relocation */
834 relocs = malloc(sizeof(struct coff_rel) * nb_relocs);
835 for (i = 0, ext_rel = coff_relocs, rel = relocs; i < nb_relocs;
836 i++, ext_rel++, rel++) {
837 memset(rel, 0, sizeof(*rel));
838 rel->r_reloc = ext_rel;
839 rel->r_offset = *(uint32_t *)ext_rel->r_vaddr;
840 rel->r_type = *(uint16_t *)ext_rel->r_type;
845 #endif /* CONFIG_FORMAT_COFF */
847 #ifdef CONFIG_FORMAT_MACH
850 struct mach_header mach_hdr;
853 struct segment_command *segment = 0;
854 struct dysymtab_command *dysymtabcmd = 0;
855 struct symtab_command *symtabcmd = 0;
858 struct section *section_hdr;
859 struct section *text_sec_hdr;
863 struct relocation_info *relocs;
867 struct nlist *symtab_std;
870 /* indirect symbols */
873 /* Utility functions */
875 static inline char *find_str_by_index(int index)
880 /* Used by dyngen common code */
881 static char *get_sym_name(EXE_SYM *sym)
883 char *name = find_str_by_index(sym->n_un.n_strx);
885 if ( sym->n_type & N_STAB ) /* Debug symbols are ignored */
896 /* find a section index given its segname, sectname */
897 static int find_mach_sec_index(struct section *section_hdr, int shnum, const char *segname,
898 const char *sectname)
901 struct section *sec = section_hdr;
903 for(i = 0; i < shnum; i++, sec++) {
904 if (!sec->segname || !sec->sectname)
906 if (!strcmp(sec->sectname, sectname) && !strcmp(sec->segname, segname))
912 /* find a section header given its segname, sectname */
913 struct section *find_mach_sec_hdr(struct section *section_hdr, int shnum, const char *segname,
914 const char *sectname)
916 int index = find_mach_sec_index(section_hdr, shnum, segname, sectname);
919 return section_hdr+index;
923 static inline void fetch_next_pair_value(struct relocation_info * rel, unsigned int *value)
925 struct scattered_relocation_info * scarel;
927 if(R_SCATTERED & rel->r_address) {
928 scarel = (struct scattered_relocation_info*)rel;
929 if(scarel->r_type != PPC_RELOC_PAIR)
930 error("fetch_next_pair_value: looking for a pair which was not found (1)");
931 *value = scarel->r_value;
933 if(rel->r_type != PPC_RELOC_PAIR)
934 error("fetch_next_pair_value: looking for a pair which was not found (2)");
935 *value = rel->r_address;
939 /* find a sym name given its value, in a section number */
940 static const char * find_sym_with_value_and_sec_number( int value, int sectnum, int * offset )
944 for( i = 0 ; i < nb_syms; i++ )
946 if( !(symtab[i].n_type & N_STAB) && (symtab[i].n_type & N_SECT) &&
947 (symtab[i].n_sect == sectnum) && (symtab[i].st_value <= value) )
949 if( (ret<0) || (symtab[i].st_value >= symtab[ret].st_value) )
957 *offset = value - symtab[ret].st_value;
958 return get_sym_name(&symtab[ret]);
963 * Find symbol name given a (virtual) address, and a section which is of type
964 * S_NON_LAZY_SYMBOL_POINTERS or S_LAZY_SYMBOL_POINTERS or S_SYMBOL_STUBS
966 static const char * find_reloc_name_in_sec_ptr(int address, struct section * sec_hdr)
968 unsigned int tocindex, symindex, size;
969 const char *name = 0;
972 if(!( address >= sec_hdr->addr && address < (sec_hdr->addr + sec_hdr->size) ) )
975 if( sec_hdr->flags & S_SYMBOL_STUBS ){
976 size = sec_hdr->reserved2;
981 else if( sec_hdr->flags & S_LAZY_SYMBOL_POINTERS ||
982 sec_hdr->flags & S_NON_LAZY_SYMBOL_POINTERS)
983 size = sizeof(unsigned long);
987 /* Compute our index in toc */
988 tocindex = (address - sec_hdr->addr)/size;
989 symindex = tocdylib[sec_hdr->reserved1 + tocindex];
991 name = get_sym_name(&symtab[symindex]);
996 static const char * find_reloc_name_given_its_address(int address)
999 for(i = 0; i < segment->nsects ; i++)
1001 const char * name = find_reloc_name_in_sec_ptr(address, §ion_hdr[i]);
1002 if((long)name != -1)
1008 static const char * get_reloc_name(EXE_RELOC * rel, int * sslide)
1011 struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
1012 int sectnum = rel->r_symbolnum;
1016 /* init the slide value */
1019 if(R_SCATTERED & rel->r_address)
1020 return (char *)find_reloc_name_given_its_address(sca_rel->r_value);
1024 /* ignore debug sym */
1025 if ( symtab[rel->r_symbolnum].n_type & N_STAB )
1027 return get_sym_name(&symtab[rel->r_symbolnum]);
1030 /* Intruction contains an offset to the symbols pointed to, in the rel->r_symbolnum section */
1031 sectoffset = *(uint32_t *)(text + rel->r_address) & 0xffff;
1033 if(sectnum==0xffffff)
1037 if(sectnum > segment->nsects)
1038 error("sectnum > segment->nsects");
1042 case PPC_RELOC_LO16: fetch_next_pair_value(rel+1, &other_half); sectoffset |= (other_half << 16);
1044 case PPC_RELOC_HI16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset << 16) | (uint16_t)(other_half & 0xffff);
1046 case PPC_RELOC_HA16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset << 16) + (int16_t)(other_half & 0xffff);
1048 case PPC_RELOC_BR24:
1049 sectoffset = ( *(uint32_t *)(text + rel->r_address) & 0x03fffffc );
1050 if (sectoffset & 0x02000000) sectoffset |= 0xfc000000;
1053 error("switch(rel->type) not found");
1057 sectoffset += rel->r_address;
1059 if (rel->r_type == PPC_RELOC_BR24)
1060 name = (char *)find_reloc_name_in_sec_ptr((int)sectoffset, §ion_hdr[sectnum-1]);
1062 /* search it in the full symbol list, if not found */
1064 name = (char *)find_sym_with_value_and_sec_number(sectoffset, sectnum, sslide);
1069 /* Used by dyngen common code */
1070 static const char * get_rel_sym_name(EXE_RELOC * rel)
1073 return get_reloc_name( rel, &sslide);
1076 /* Used by dyngen common code */
1077 static host_ulong get_rel_offset(EXE_RELOC *rel)
1079 struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
1080 if(R_SCATTERED & rel->r_address)
1081 return sca_rel->r_address;
1083 return rel->r_address;
1086 /* load a mach-o object file */
1087 int load_object(const char *filename)
1090 unsigned int offset_to_segment = 0;
1091 unsigned int offset_to_dysymtab = 0;
1092 unsigned int offset_to_symtab = 0;
1093 struct load_command lc;
1096 struct nlist *syment;
1098 fd = open(filename, O_RDONLY);
1100 error("can't open file '%s'", filename);
1102 /* Read Mach header. */
1103 if (read(fd, &mach_hdr, sizeof (mach_hdr)) != sizeof (mach_hdr))
1104 error("unable to read file header");
1106 /* Check Mach identification. */
1107 if (!check_mach_header(mach_hdr)) {
1108 error("bad Mach header");
1111 if (mach_hdr.cputype != CPU_TYPE_POWERPC)
1112 error("Unsupported CPU");
1114 if (mach_hdr.filetype != MH_OBJECT)
1115 error("Unsupported Mach Object");
1117 /* read segment headers */
1118 for(i=0, j=sizeof(mach_hdr); i<mach_hdr.ncmds ; i++)
1120 if(read(fd, &lc, sizeof(struct load_command)) != sizeof(struct load_command))
1121 error("unable to read load_command");
1122 if(lc.cmd == LC_SEGMENT)
1124 offset_to_segment = j;
1125 lseek(fd, offset_to_segment, SEEK_SET);
1126 segment = malloc(sizeof(struct segment_command));
1127 if(read(fd, segment, sizeof(struct segment_command)) != sizeof(struct segment_command))
1128 error("unable to read LC_SEGMENT");
1130 if(lc.cmd == LC_DYSYMTAB)
1132 offset_to_dysymtab = j;
1133 lseek(fd, offset_to_dysymtab, SEEK_SET);
1134 dysymtabcmd = malloc(sizeof(struct dysymtab_command));
1135 if(read(fd, dysymtabcmd, sizeof(struct dysymtab_command)) != sizeof(struct dysymtab_command))
1136 error("unable to read LC_DYSYMTAB");
1138 if(lc.cmd == LC_SYMTAB)
1140 offset_to_symtab = j;
1141 lseek(fd, offset_to_symtab, SEEK_SET);
1142 symtabcmd = malloc(sizeof(struct symtab_command));
1143 if(read(fd, symtabcmd, sizeof(struct symtab_command)) != sizeof(struct symtab_command))
1144 error("unable to read LC_SYMTAB");
1148 lseek(fd, j, SEEK_SET);
1152 error("unable to find LC_SEGMENT");
1154 /* read section headers */
1155 section_hdr = load_data(fd, offset_to_segment + sizeof(struct segment_command), segment->nsects * sizeof(struct section));
1157 /* read all section data */
1158 sdata = (uint8_t **)malloc(sizeof(void *) * segment->nsects);
1159 memset(sdata, 0, sizeof(void *) * segment->nsects);
1161 /* Load the data in section data */
1162 for(i = 0; i < segment->nsects; i++) {
1163 sdata[i] = load_data(fd, section_hdr[i].offset, section_hdr[i].size);
1167 text_sec_hdr = find_mach_sec_hdr(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT);
1168 i = find_mach_sec_index(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT);
1169 if (i == -1 || !text_sec_hdr)
1170 error("could not find __TEXT,__text section");
1173 /* Make sure dysym was loaded */
1174 if(!(int)dysymtabcmd)
1175 error("could not find __DYSYMTAB segment");
1177 /* read the table of content of the indirect sym */
1178 tocdylib = load_data( fd, dysymtabcmd->indirectsymoff, dysymtabcmd->nindirectsyms * sizeof(uint32_t) );
1180 /* Make sure symtab was loaded */
1182 error("could not find __SYMTAB segment");
1183 nb_syms = symtabcmd->nsyms;
1185 symtab_std = load_data(fd, symtabcmd->symoff, symtabcmd->nsyms * sizeof(struct nlist));
1186 strtab = load_data(fd, symtabcmd->stroff, symtabcmd->strsize);
1188 symtab = malloc(sizeof(EXE_SYM) * nb_syms);
1190 /* Now transform the symtab, to an extended version, with the sym size, and the C name */
1191 for(i = 0, sym = symtab, syment = symtab_std; i < nb_syms; i++, sym++, syment++) {
1192 struct nlist *sym_follow, *sym_next = 0;
1194 memset(sym, 0, sizeof(*sym));
1196 if ( syment->n_type & N_STAB ) /* Debug symbols are skipped */
1199 memcpy(sym, syment, sizeof(*syment));
1201 /* Find the following symbol in order to get the current symbol size */
1202 for(j = 0, sym_follow = symtab_std; j < nb_syms; j++, sym_follow++) {
1203 if ( sym_follow->n_sect != 1 || sym_follow->n_type & N_STAB || !(sym_follow->n_value > sym->st_value))
1206 sym_next = sym_follow;
1209 if(!(sym_next->n_value > sym_follow->n_value))
1211 sym_next = sym_follow;
1214 sym->st_size = sym_next->n_value - sym->st_value;
1216 sym->st_size = text_sec_hdr->size - sym->st_value;
1220 relocs = load_data(fd, text_sec_hdr->reloff, text_sec_hdr->nreloc * sizeof(struct relocation_info));
1221 nb_relocs = text_sec_hdr->nreloc;
1227 #endif /* CONFIG_FORMAT_MACH */
1229 /* return true if the expression is a label reference */
1230 static int get_reloc_expr(char *name, int name_size, const char *sym_name)
1234 if (strstart(sym_name, "__op_param", &p)) {
1235 snprintf(name, name_size, "param%s", p);
1236 } else if (strstart(sym_name, "__op_gen_label", &p)) {
1237 snprintf(name, name_size, "param%s", p);
1240 #if defined(HOST_SPARC) || defined(HOST_HPPA)
1241 if (sym_name[0] == '.')
1242 snprintf(name, name_size,
1243 "(long)(&__dot_%s)",
1247 snprintf(name, name_size, "(long)(&%s)", sym_name);
1254 #define PLT_ENTRY_SIZE 16 /* 1 bundle containing "brl" */
1257 struct plt_entry *next;
1259 unsigned long addend;
1263 get_plt_index (const char *name, unsigned long addend)
1265 struct plt_entry *plt, *prev= NULL;
1268 /* see if we already have an entry for this target: */
1269 for (plt = plt_list; plt; ++index, prev = plt, plt = plt->next)
1270 if (strcmp(plt->name, name) == 0 && plt->addend == addend)
1273 /* nope; create a new PLT entry: */
1275 plt = malloc(sizeof(*plt));
1280 memset(plt, 0, sizeof(*plt));
1281 plt->name = strdup(name);
1282 plt->addend = addend;
1284 /* append to plt-list: */
1296 /* generate op code */
1297 static void gen_code(const char *name, host_ulong offset, host_ulong size,
1298 FILE *outfile, int gen_switch)
1301 uint8_t *p_start, *p_end;
1302 host_ulong start_offset;
1304 uint8_t args_present[MAX_ARGS];
1305 const char *sym_name, *p;
1308 /* Compute exact size excluding prologue and epilogue instructions.
1309 * Increment start_offset to skip epilogue instructions, then compute
1310 * copy_size the indicate the size of the remaining instructions (in
1313 p_start = text + offset;
1314 p_end = p_start + size;
1315 start_offset = offset;
1316 #if defined(HOST_I386) || defined(HOST_X86_64)
1317 #ifdef CONFIG_FORMAT_COFF
1322 error("empty code for %s", name);
1323 while (*p != 0xc3) {
1326 error("ret or jmp expected at the end of %s", name);
1328 copy_size = p - p_start;
1333 len = p_end - p_start;
1335 error("empty code for %s", name);
1336 if (p_end[-1] == 0xc3) {
1339 error("ret or jmp expected at the end of %s", name);
1344 #elif defined(HOST_PPC)
1347 p = (void *)(p_end - 4);
1349 error("empty code for %s", name);
1350 if (get32((uint32_t *)p) != 0x4e800020)
1351 error("blr expected at the end of %s", name);
1352 copy_size = p - p_start;
1354 #elif defined(HOST_S390)
1357 p = (void *)(p_end - 2);
1359 error("empty code for %s", name);
1360 if ((get16((uint16_t *)p) & 0xfff0) != 0x07f0)
1361 error("br expected at the end of %s", name);
1362 copy_size = p - p_start;
1364 #elif defined(HOST_ALPHA)
1369 /* XXX: check why it occurs */
1371 error("empty code for %s", name);
1373 if (get32((uint32_t *)p) != 0x6bfa8001)
1374 error("ret expected at the end of %s", name);
1375 copy_size = p - p_start;
1377 #elif defined(HOST_IA64)
1380 p = (void *)(p_end - 4);
1382 error("empty code for %s", name);
1383 /* br.ret.sptk.many b0;; */
1385 if (get32((uint32_t *)p) != 0x00840008)
1386 error("br.ret.sptk.many b0;; expected at the end of %s", name);
1387 copy_size = p_end - p_start;
1389 #elif defined(HOST_SPARC)
1391 #define INSN_SAVE 0x9de3a000
1392 #define INSN_RET 0x81c7e008
1393 #define INSN_RETL 0x81c3e008
1394 #define INSN_RESTORE 0x81e80000
1395 #define INSN_RETURN 0x81cfe008
1396 #define INSN_NOP 0x01000000
1397 #define INSN_ADD_SP 0x9c03a000 // add %sp, nn, %sp
1398 #define INSN_SUB_SP 0x9c23a000 // sub %sp, nn, %sp
1400 uint32_t start_insn, end_insn1, end_insn2;
1402 p = (void *)(p_end - 8);
1404 error("empty code for %s", name);
1405 start_insn = get32((uint32_t *)(p_start + 0x0));
1406 end_insn1 = get32((uint32_t *)(p + 0x0));
1407 end_insn2 = get32((uint32_t *)(p + 0x4));
1408 if (((start_insn & ~0x1fff) == INSN_SAVE) ||
1409 (start_insn & ~0x1fff) == INSN_ADD_SP) {
1411 start_offset += 0x4;
1412 if (end_insn1 == INSN_RET && end_insn2 == INSN_RESTORE)
1413 /* SPARC v7: ret; restore; */ ;
1414 else if (end_insn1 == INSN_RETURN && end_insn2 == INSN_NOP)
1415 /* SPARC v9: return; nop; */ ;
1416 else if (end_insn1 == INSN_RETL && (end_insn2 & ~0x1fff) == INSN_SUB_SP)
1417 /* SPARC v7: retl; sub %sp, nn, %sp; */ ;
1420 error("ret; restore; not found at end of %s", name);
1421 } else if (end_insn1 == INSN_RETL && end_insn2 == INSN_NOP) {
1424 error("No save at the beginning of %s", name);
1427 /* Skip a preceeding nop, if present. */
1429 skip_insn = get32((uint32_t *)(p - 0x4));
1430 if (skip_insn == INSN_NOP)
1434 copy_size = p - p_start;
1436 #elif defined(HOST_SPARC64)
1438 #define INSN_SAVE 0x9de3a000
1439 #define INSN_RET 0x81c7e008
1440 #define INSN_RETL 0x81c3e008
1441 #define INSN_RESTORE 0x81e80000
1442 #define INSN_RETURN 0x81cfe008
1443 #define INSN_NOP 0x01000000
1444 #define INSN_ADD_SP 0x9c03a000 // add %sp, nn, %sp
1445 #define INSN_SUB_SP 0x9c23a000 // sub %sp, nn, %sp
1447 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
1449 p = (void *)(p_end - 8);
1451 /* XXX: check why it occurs */
1453 error("empty code for %s", name);
1455 start_insn = get32((uint32_t *)(p_start + 0x0));
1456 end_insn1 = get32((uint32_t *)(p + 0x0));
1457 end_insn2 = get32((uint32_t *)(p + 0x4));
1458 if (((start_insn & ~0x1fff) == INSN_SAVE) ||
1459 (start_insn & ~0x1fff) == INSN_ADD_SP) {
1461 start_offset += 0x4;
1462 if (end_insn1 == INSN_RET && end_insn2 == INSN_RESTORE)
1463 /* SPARC v7: ret; restore; */ ;
1464 else if (end_insn1 == INSN_RETURN && end_insn2 == INSN_NOP)
1465 /* SPARC v9: return; nop; */ ;
1466 else if (end_insn1 == INSN_RETL && (end_insn2 & ~0x1fff) == INSN_SUB_SP)
1467 /* SPARC v7: retl; sub %sp, nn, %sp; */ ;
1470 error("ret; restore; not found at end of %s", name);
1471 } else if (end_insn1 == INSN_RETL && end_insn2 == INSN_NOP) {
1474 error("No save at the beginning of %s", name);
1478 /* Skip a preceeding nop, if present. */
1480 skip_insn = get32((uint32_t *)(p - 0x4));
1481 if (skip_insn == 0x01000000)
1486 copy_size = p - p_start;
1488 #elif defined(HOST_M68K)
1491 p = (void *)(p_end - 2);
1493 error("empty code for %s", name);
1494 // remove NOP's, probably added for alignment
1495 while ((get16((uint16_t *)p) == 0x4e71) &&
1498 if (get16((uint16_t *)p) != 0x4e75)
1499 error("rts expected at the end of %s", name);
1500 copy_size = p - p_start;
1502 #elif defined(HOST_HPPA)
1507 uint32_t insn = get32((uint32_t *)p);
1508 if (insn == 0x6bc23fd9 || /* stw rp,-14(sp) */
1509 insn == 0x08030241 || /* copy r3,r1 */
1510 insn == 0x081e0243 || /* copy sp,r3 */
1511 (insn & 0xffffc000) == 0x37de0000 || /* ldo x(sp),sp */
1512 (insn & 0xffffc000) == 0x6fc10000) /* stwm r1,x(sp) */
1517 start_offset += p - p_start;
1521 while (p > p_start) {
1522 uint32_t insn = get32((uint32_t *)p);
1523 if ((insn & 0xffffc000) == 0x347e0000 || /* ldo x(r3),sp */
1524 (insn & 0xffe0c000) == 0x4fc00000 || /* ldwm x(sp),rx */
1525 (insn & 0xffffc000) == 0x37de0000 || /* ldo x(sp),sp */
1526 insn == 0x48623fd9 || /* ldw -14(r3),rp */
1527 insn == 0xe840c000 || /* bv r0(rp) */
1528 insn == 0xe840c002) /* bv,n r0(rp) */
1535 error("empty code for %s", name);
1537 copy_size = p - p_start;
1539 #elif defined(HOST_MIPS) || defined(HOST_MIPS64)
1541 #define INSN_RETURN 0x03e00008
1542 #define INSN_NOP 0x00000000
1546 if (p < (p_start + 0x8)) {
1547 error("empty code for %s", name);
1549 uint32_t end_insn1, end_insn2;
1552 end_insn1 = get32((uint32_t *)(p + 0x0));
1553 end_insn2 = get32((uint32_t *)(p + 0x4));
1554 if (end_insn1 != INSN_RETURN && end_insn2 != INSN_NOP)
1555 error("jr ra not found at end of %s", name);
1557 copy_size = p - p_start;
1559 #elif defined(HOST_ARM)
1560 error("dyngen targets not supported on ARM");
1561 #elif defined(HOST_PPC64)
1562 error("dyngen targets not supported on PPC64");
1564 #error unsupported CPU
1567 /* compute the number of arguments by looking at the relocations */
1568 for(i = 0;i < MAX_ARGS; i++)
1569 args_present[i] = 0;
1571 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1572 host_ulong offset = get_rel_offset(rel);
1573 if (offset >= start_offset &&
1574 offset < start_offset + (p_end - p_start)) {
1575 sym_name = get_rel_sym_name(rel);
1578 if (strstart(sym_name, "__op_param", &p) ||
1579 strstart(sym_name, "__op_gen_label", &p)) {
1580 n = strtoul(p, NULL, 10);
1582 error("too many arguments in %s", name);
1583 args_present[n - 1] = 1;
1589 while (nb_args < MAX_ARGS && args_present[nb_args])
1591 for(i = nb_args; i < MAX_ARGS; i++) {
1592 if (args_present[i])
1593 error("inconsistent argument numbering in %s", name);
1596 if (gen_switch == 2) {
1598 #if defined(HOST_HPPA)
1599 int op_size = copy_size;
1604 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
1605 if (rel->r_offset >= start_offset &&
1606 rel->r_offset < start_offset + copy_size) {
1607 sym_name = get_rel_sym_name(rel);
1608 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
1609 is_label = get_reloc_expr(relname, sizeof(relname), sym_name);
1610 type = ELF32_R_TYPE(rel->r_info);
1612 if (!is_label && type == R_PARISC_PCREL17F) {
1614 op_size += 8; /* ldil and be,n instructions */
1620 op_size += 4; /* b,l,n instruction, to skip past the stubs */
1622 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, op_size);
1624 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
1627 } else if (gen_switch == 1) {
1630 fprintf(outfile, "case INDEX_%s: {\n", name);
1632 fprintf(outfile, " long ");
1633 for(i = 0; i < nb_args; i++) {
1635 fprintf(outfile, ", ");
1636 fprintf(outfile, "param%d", i + 1);
1638 fprintf(outfile, ";\n");
1640 #if defined(HOST_IA64)
1641 fprintf(outfile, " extern char %s;\n", name);
1643 fprintf(outfile, " extern void %s();\n", name);
1646 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1647 host_ulong offset = get_rel_offset(rel);
1648 if (offset >= start_offset &&
1649 offset < start_offset + (p_end - p_start)) {
1650 sym_name = get_rel_sym_name(rel);
1654 !strstart(sym_name, "__op_param", NULL) &&
1655 !strstart(sym_name, "__op_jmp", NULL) &&
1656 !strstart(sym_name, "__op_gen_label", NULL)) {
1657 #if defined(HOST_SPARC) || defined(HOST_HPPA)
1658 if (sym_name[0] == '.') {
1660 "extern char __dot_%s __asm__(\"%s\");\n",
1661 sym_name+1, sym_name);
1665 #if defined(__APPLE__)
1666 /* Set __attribute((unused)) on darwin because we
1667 want to avoid warning when we don't use the symbol. */
1668 fprintf(outfile, " extern char %s __attribute__((unused));\n", sym_name);
1669 #elif defined(HOST_IA64)
1670 if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B)
1672 * PCREL21 br.call targets generally
1673 * are out of range and need to go
1674 * through an "import stub".
1676 fprintf(outfile, " extern char %s;\n",
1679 fprintf(outfile, "extern char %s;\n", sym_name);
1686 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)__canonicalize_funcptr_for_compare(%s)+%d), %d);\n",
1687 name, (int)(start_offset - offset), copy_size);
1689 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n",
1690 name, (int)(start_offset - offset), copy_size);
1693 /* emit code offset information */
1696 const char *sym_name, *p;
1700 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1701 sym_name = get_sym_name(sym);
1702 if (strstart(sym_name, "__op_label", &p)) {
1704 unsigned long offset;
1706 /* test if the variable refers to a label inside
1707 the code we are generating */
1708 #ifdef CONFIG_FORMAT_COFF
1709 if (sym->st_shndx == text_shndx) {
1710 ptr = sdata[coff_text_shndx];
1711 } else if (sym->st_shndx == data_shndx) {
1712 ptr = sdata[coff_data_shndx];
1716 #elif defined(CONFIG_FORMAT_MACH)
1719 ptr = sdata[sym->n_sect-1];
1721 ptr = sdata[sym->st_shndx];
1724 error("__op_labelN in invalid section");
1725 offset = sym->st_value;
1726 #ifdef CONFIG_FORMAT_MACH
1727 offset -= section_hdr[sym->n_sect-1].addr;
1729 val = *(host_ulong *)(ptr + offset);
1730 #ifdef ELF_USES_RELOCA
1732 int reloc_shndx, nb_relocs1, j;
1734 /* try to find a matching relocation */
1735 reloc_shndx = find_reloc(sym->st_shndx);
1737 nb_relocs1 = shdr[reloc_shndx].sh_size /
1738 shdr[reloc_shndx].sh_entsize;
1739 rel = (ELF_RELOC *)sdata[reloc_shndx];
1740 for(j = 0; j < nb_relocs1; j++) {
1741 if (rel->r_offset == offset) {
1742 val = rel->r_addend;
1750 if (val >= start_offset && val <= start_offset + copy_size) {
1751 n = strtol(p, NULL, 10);
1752 fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, (long)(val - start_offset));
1758 /* load parameters in variables */
1759 for(i = 0; i < nb_args; i++) {
1760 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
1763 /* patch relocations */
1764 #if defined(HOST_I386)
1770 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1771 if (rel->r_offset >= start_offset &&
1772 rel->r_offset < start_offset + copy_size) {
1773 sym_name = get_rel_sym_name(rel);
1776 reloc_offset = rel->r_offset - start_offset;
1777 if (strstart(sym_name, "__op_jmp", &p)) {
1779 n = strtol(p, NULL, 10);
1780 /* __op_jmp relocations are done at
1781 runtime to do translated block
1782 chaining: the offset of the instruction
1783 needs to be stored */
1784 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1789 is_label = get_reloc_expr(relname, sizeof(relname), sym_name);
1790 addend = get32((uint32_t *)(text + rel->r_offset));
1791 #ifdef CONFIG_FORMAT_ELF
1792 type = ELF32_R_TYPE(rel->r_info);
1797 fprintf(outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1798 reloc_offset, type, relname, addend);
1801 error("unsupported i386 relocation (%d)", type);
1806 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1807 reloc_offset, relname, addend);
1810 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1811 reloc_offset, relname, reloc_offset, addend);
1814 error("unsupported i386 relocation (%d)", type);
1817 #elif defined(CONFIG_FORMAT_COFF)
1822 temp_name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx));
1823 if (!strcmp(temp_name, ".data")) {
1824 for (j = 0, sym = symtab; j < nb_syms; j++, sym++) {
1825 if (strstart(sym->st_name, sym_name, NULL)) {
1826 addend -= sym->st_value;
1833 /* TCG uses elf relocation constants */
1835 #define R_386_PC32 2
1844 fprintf(outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1845 reloc_offset, type, relname, addend);
1848 error("unsupported i386 relocation (%d)", type);
1853 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1854 reloc_offset, relname, addend);
1857 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n",
1858 reloc_offset, relname, reloc_offset, addend);
1861 error("unsupported i386 relocation (%d)", type);
1865 #error unsupport object format
1870 #elif defined(HOST_X86_64)
1876 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1877 if (rel->r_offset >= start_offset &&
1878 rel->r_offset < start_offset + copy_size) {
1879 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1880 is_label = get_reloc_expr(relname, sizeof(relname), sym_name);
1881 type = ELF32_R_TYPE(rel->r_info);
1882 addend = rel->r_addend;
1883 reloc_offset = rel->r_offset - start_offset;
1889 fprintf(outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1890 reloc_offset, type, relname, addend);
1893 error("unsupported X86_64 relocation (%d)", type);
1898 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
1899 reloc_offset, relname, addend);
1902 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
1903 reloc_offset, relname, addend);
1906 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1907 reloc_offset, relname, reloc_offset, addend);
1910 error("unsupported X86_64 relocation (%d)", type);
1916 #elif defined(HOST_PPC)
1918 #ifdef CONFIG_FORMAT_ELF
1924 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1925 if (rel->r_offset >= start_offset &&
1926 rel->r_offset < start_offset + copy_size) {
1927 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1928 reloc_offset = rel->r_offset - start_offset;
1929 if (strstart(sym_name, "__op_jmp", &p)) {
1931 n = strtol(p, NULL, 10);
1932 /* __op_jmp relocations are done at
1933 runtime to do translated block
1934 chaining: the offset of the instruction
1935 needs to be stored */
1936 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1941 get_reloc_expr(relname, sizeof(relname), sym_name);
1942 type = ELF32_R_TYPE(rel->r_info);
1943 is_label = get_reloc_expr(relname, sizeof(relname), sym_name);
1944 addend = rel->r_addend;
1948 fprintf (outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1949 reloc_offset, type, relname, addend);
1952 error ("unsupported ppc relocation (%d)", type);
1958 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1959 reloc_offset, relname, addend);
1961 case R_PPC_ADDR16_LO:
1962 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
1963 reloc_offset, relname, addend);
1965 case R_PPC_ADDR16_HI:
1966 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
1967 reloc_offset, relname, addend);
1969 case R_PPC_ADDR16_HA:
1970 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
1971 reloc_offset, relname, addend);
1974 /* warning: must be at 32 MB distancy */
1975 fprintf(outfile, "{\n"
1976 " long disp = (%s - (long)(gen_code_ptr + %d) + %d);\n"
1977 " if ((disp << 6) >> 6 != disp) {;\n"
1978 " fprintf(stderr, \"Branch target is too far away\\n\");"
1982 relname, reloc_offset, addend);
1983 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
1984 reloc_offset, reloc_offset, relname, reloc_offset, addend);
1987 error("unsupported powerpc relocation (%d)", type);
1992 #elif defined(CONFIG_FORMAT_MACH)
1993 struct scattered_relocation_info *scarel;
1994 struct relocation_info * rel;
1995 char final_sym_name[256];
1996 const char *sym_name;
2001 for(i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
2002 unsigned int offset, length, value = 0;
2003 unsigned int type, pcrel, isym = 0;
2004 unsigned int usesym = 0;
2006 if(R_SCATTERED & rel->r_address) {
2007 scarel = (struct scattered_relocation_info*)rel;
2008 offset = (unsigned int)scarel->r_address;
2009 length = scarel->r_length;
2010 pcrel = scarel->r_pcrel;
2011 type = scarel->r_type;
2012 value = scarel->r_value;
2014 value = isym = rel->r_symbolnum;
2015 usesym = (rel->r_extern);
2016 offset = rel->r_address;
2017 length = rel->r_length;
2018 pcrel = rel->r_pcrel;
2022 slide = offset - start_offset;
2024 if (!(offset >= start_offset && offset < start_offset + size))
2025 continue; /* not in our range */
2027 sym_name = get_reloc_name(rel, &sslide);
2029 if(usesym && symtab[isym].n_type & N_STAB)
2030 continue; /* don't handle STAB (debug sym) */
2032 if (sym_name && strstart(sym_name, "__op_jmp", &p)) {
2034 n = strtol(p, NULL, 10);
2035 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
2037 continue; /* Nothing more to do */
2041 fprintf(outfile, "/* #warning relocation not handled in %s (value 0x%x, %s, offset 0x%x, length 0x%x, %s, type 0x%x) */\n",
2042 name, value, usesym ? "use sym" : "don't use sym", offset, length, pcrel ? "pcrel":"", type);
2043 continue; /* dunno how to handle without final_sym_name */
2046 get_reloc_expr(final_sym_name, sizeof(final_sym_name),
2049 case PPC_RELOC_BR24:
2050 if (!strstart(sym_name,"__op_gen_label",&p)) {
2051 fprintf(outfile, "{\n");
2052 fprintf(outfile, " uint32_t imm = *(uint32_t *)(gen_code_ptr + %d) & 0x3fffffc;\n", slide);
2053 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",
2054 slide, slide, name, sslide);
2055 fprintf(outfile, "}\n");
2057 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | (((long)%s - (long)gen_code_ptr - %d) & 0x03fffffc);\n",
2058 slide, slide, final_sym_name, slide);
2061 case PPC_RELOC_HI16:
2062 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d) >> 16;\n",
2063 slide, final_sym_name, sslide);
2065 case PPC_RELOC_LO16:
2066 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d);\n",
2067 slide, final_sym_name, sslide);
2069 case PPC_RELOC_HA16:
2070 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d + 0x8000) >> 16;\n",
2071 slide, final_sym_name, sslide);
2074 error("unsupported powerpc relocation (%d)", type);
2078 #error unsupport object format
2081 #elif defined(HOST_S390)
2087 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2088 if (rel->r_offset >= start_offset &&
2089 rel->r_offset < start_offset + copy_size) {
2090 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
2091 get_reloc_expr(relname, sizeof(relname), sym_name);
2092 type = ELF32_R_TYPE(rel->r_info);
2093 addend = rel->r_addend;
2094 reloc_offset = rel->r_offset - start_offset;
2097 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2098 reloc_offset, relname, addend);
2101 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
2102 reloc_offset, relname, addend);
2105 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
2106 reloc_offset, relname, addend);
2109 if (ELF32_ST_TYPE(symtab[ELFW(R_SYM)(rel->r_info)].st_info) == STT_SECTION) {
2111 " *(uint32_t *)(gen_code_ptr + %d) += "
2112 "((long)&%s - (long)gen_code_ptr) >> 1;\n",
2113 reloc_offset, name);
2117 " *(uint32_t *)(gen_code_ptr + %d) = "
2118 "(%s + %d - ((uint32_t)gen_code_ptr + %d)) >> 1;\n",
2119 reloc_offset, relname, addend, reloc_offset);
2122 error("unsupported s390 relocation (%d)", type);
2127 #elif defined(HOST_ALPHA)
2129 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
2130 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
2134 type = ELF64_R_TYPE(rel->r_info);
2135 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
2136 reloc_offset = rel->r_offset - start_offset;
2138 case R_ALPHA_GPDISP:
2139 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
2140 as an immediate instead of constructing it from the pv or ra. */
2141 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
2143 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
2144 reloc_offset + (int)rel->r_addend);
2146 case R_ALPHA_LITUSE:
2147 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
2148 now, since some called functions (libc) need pv to be set up. */
2151 /* Branch target prediction hint. Ignore for now. Should be already
2152 correct for in-function jumps. */
2154 case R_ALPHA_LITERAL:
2155 /* Load a literal from the GOT relative to the gp. Since there's only a
2156 single gp, nothing is to be done. */
2158 case R_ALPHA_GPRELHIGH:
2159 /* Handle fake relocations against __op_param symbol. Need to emit the
2160 high part of the immediate value instead. Other symbols need no
2161 special treatment. */
2162 if (strstart(sym_name, "__op_param", &p))
2163 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
2166 case R_ALPHA_GPRELLOW:
2167 if (strstart(sym_name, "__op_param", &p))
2168 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
2172 /* PC-relative jump. Tweak offset to skip the two instructions that try to
2173 set up the gp from the pv. */
2174 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
2175 reloc_offset, sym_name, reloc_offset);
2178 error("unsupported Alpha relocation (%d)", type);
2183 #elif defined(HOST_IA64)
2185 unsigned long sym_idx;
2191 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2192 sym_idx = ELF64_R_SYM(rel->r_info);
2193 if (rel->r_offset < start_offset
2194 || rel->r_offset >= start_offset + copy_size)
2196 sym_name = (strtab + symtab[sym_idx].st_name);
2197 code_offset = rel->r_offset - start_offset;
2198 if (strstart(sym_name, "__op_jmp", &p)) {
2200 n = strtol(p, NULL, 10);
2201 /* __op_jmp relocations are done at
2202 runtime to do translated block
2203 chaining: the offset of the instruction
2204 needs to be stored */
2205 fprintf(outfile, " jmp_offsets[%d] ="
2206 "%ld + (gen_code_ptr - gen_code_buf);\n",
2210 get_reloc_expr(relname, sizeof(relname), sym_name);
2211 type = ELF64_R_TYPE(rel->r_info);
2212 addend = rel->r_addend;
2216 " ia64_imm64(gen_code_ptr + %ld, "
2218 code_offset, relname, addend);
2220 case R_IA64_LTOFF22X:
2221 case R_IA64_LTOFF22:
2222 fprintf(outfile, " IA64_LTOFF(gen_code_ptr + %ld,"
2223 " %s + %ld, %d);\n",
2224 code_offset, relname, addend,
2225 (type == R_IA64_LTOFF22X));
2229 " ia64_ldxmov(gen_code_ptr + %ld,"
2230 " %s + %ld);\n", code_offset, relname, addend);
2233 case R_IA64_PCREL21B:
2234 if (strstart(sym_name, "__op_gen_label", NULL)) {
2236 " ia64_imm21b(gen_code_ptr + %ld,"
2237 " (long) (%s + %ld -\n\t\t"
2238 "((long) gen_code_ptr + %ld)) >> 4);\n",
2239 code_offset, relname, addend,
2240 code_offset & ~0xfUL);
2243 " IA64_PLT(gen_code_ptr + %ld, "
2244 "%d);\t/* %s + %ld */\n",
2246 get_plt_index(sym_name, addend),
2251 error("unsupported ia64 relocation (0x%x)",
2255 fprintf(outfile, " ia64_nop_b(gen_code_ptr + %d);\n",
2256 copy_size - 16 + 2);
2258 #elif defined(HOST_SPARC)
2264 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2265 if (rel->r_offset >= start_offset &&
2266 rel->r_offset < start_offset + copy_size) {
2267 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
2268 get_reloc_expr(relname, sizeof(relname), sym_name);
2269 type = ELF32_R_TYPE(rel->r_info);
2270 addend = rel->r_addend;
2271 reloc_offset = rel->r_offset - start_offset;
2274 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2275 reloc_offset, relname, addend);
2279 " *(uint32_t *)(gen_code_ptr + %d) = "
2280 "((*(uint32_t *)(gen_code_ptr + %d)) "
2282 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2283 reloc_offset, reloc_offset, relname, addend);
2287 " *(uint32_t *)(gen_code_ptr + %d) = "
2288 "((*(uint32_t *)(gen_code_ptr + %d)) "
2290 " | ((%s + %d) & 0x3ff);\n",
2291 reloc_offset, reloc_offset, relname, addend);
2293 case R_SPARC_WDISP30:
2295 " *(uint32_t *)(gen_code_ptr + %d) = "
2296 "((*(uint32_t *)(gen_code_ptr + %d)) "
2298 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2299 " & 0x3fffffff);\n",
2300 reloc_offset, reloc_offset, relname, addend,
2303 case R_SPARC_WDISP22:
2305 " *(uint32_t *)(gen_code_ptr + %d) = "
2306 "((*(uint32_t *)(gen_code_ptr + %d)) "
2308 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2310 rel->r_offset - start_offset,
2311 rel->r_offset - start_offset,
2313 rel->r_offset - start_offset);
2316 error("unsupported sparc relocation (%d)", type);
2321 #elif defined(HOST_SPARC64)
2327 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2328 if (rel->r_offset >= start_offset &&
2329 rel->r_offset < start_offset + copy_size) {
2330 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
2331 get_reloc_expr(relname, sizeof(relname), sym_name);
2332 type = ELF32_R_TYPE(rel->r_info);
2333 addend = rel->r_addend;
2334 reloc_offset = rel->r_offset - start_offset;
2337 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2338 reloc_offset, relname, addend);
2342 " *(uint32_t *)(gen_code_ptr + %d) = "
2343 "((*(uint32_t *)(gen_code_ptr + %d)) "
2345 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2346 reloc_offset, reloc_offset, relname, addend);
2350 " *(uint32_t *)(gen_code_ptr + %d) = "
2351 "((*(uint32_t *)(gen_code_ptr + %d)) "
2353 " | ((%s + %d) & 0x3ff);\n",
2354 reloc_offset, reloc_offset, relname, addend);
2357 addend += ELF64_R_TYPE_DATA (rel->r_info);
2359 " *(uint32_t *)(gen_code_ptr + %d) = "
2360 "((*(uint32_t *)(gen_code_ptr + %d)) "
2362 " | ((%s + %d) & 0x3ff);\n",
2363 reloc_offset, reloc_offset, relname, addend);
2365 case R_SPARC_WDISP30:
2367 " *(uint32_t *)(gen_code_ptr + %d) = "
2368 "((*(uint32_t *)(gen_code_ptr + %d)) "
2370 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2371 " & 0x3fffffff);\n",
2372 reloc_offset, reloc_offset, relname, addend,
2375 case R_SPARC_WDISP22:
2377 " *(uint32_t *)(gen_code_ptr + %d) = "
2378 "((*(uint32_t *)(gen_code_ptr + %d)) "
2380 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2382 reloc_offset, reloc_offset, relname, addend,
2387 " *(uint32_t *)(gen_code_ptr + %d) = "
2388 "((*(uint32_t *)(gen_code_ptr + %d)) "
2390 " | (((%s + %d) >> 42) & 0x00000000);\n",
2391 reloc_offset, reloc_offset, relname, addend);
2396 " *(uint32_t *)(gen_code_ptr + %d) = "
2397 "((*(uint32_t *)(gen_code_ptr + %d)) "
2399 " | (((%s + %d) >> 10) & 0x00000000);\n",
2400 reloc_offset, reloc_offset, relname, addend);
2405 " *(uint32_t *)(gen_code_ptr + %d) = "
2406 "((*(uint32_t *)(gen_code_ptr + %d)) "
2408 " | ((((%s + %d) >> 32 & 0x3ff)) & 0x00000000);\n",
2409 reloc_offset, reloc_offset, relname, addend);
2413 error("unsupported sparc64 relocation (%d) for symbol %s", type, relname);
2418 #elif defined(HOST_M68K)
2425 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2426 if (rel->r_offset >= start_offset &&
2427 rel->r_offset < start_offset + copy_size) {
2428 sym = &(symtab[ELFW(R_SYM)(rel->r_info)]);
2429 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
2430 get_reloc_expr(relname, sizeof(relname), sym_name);
2431 type = ELF32_R_TYPE(rel->r_info);
2432 addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend;
2433 reloc_offset = rel->r_offset - start_offset;
2436 fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ;
2437 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
2438 reloc_offset, relname, addend );
2441 fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset);
2442 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
2443 reloc_offset, relname, reloc_offset, /*sym->st_value+*/ addend);
2446 error("unsupported m68k relocation (%d)", type);
2451 #elif defined(HOST_HPPA)
2457 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
2458 if (rel->r_offset >= start_offset &&
2459 rel->r_offset < start_offset + copy_size) {
2460 sym_name = get_rel_sym_name(rel);
2461 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
2462 is_label = get_reloc_expr(relname, sizeof(relname), sym_name);
2463 type = ELF32_R_TYPE(rel->r_info);
2464 addend = rel->r_addend;
2465 reloc_offset = rel->r_offset - start_offset;
2469 case R_PARISC_PCREL17F:
2471 " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
2472 reloc_offset, type, relname, addend);
2475 error("unsupported hppa label relocation (%d)", type);
2479 case R_PARISC_DIR21L:
2481 " hppa_patch21l((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2482 reloc_offset, relname, addend);
2484 case R_PARISC_DIR14R:
2486 " hppa_patch14r((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2487 reloc_offset, relname, addend);
2489 case R_PARISC_PCREL17F:
2490 if (strstart(sym_name, "__op_gen_label", NULL)) {
2492 " hppa_patch17f((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2493 reloc_offset, relname, addend);
2496 " HPPA_RECORD_BRANCH(hppa_stubs, (uint32_t *)(gen_code_ptr + %d), %s);\n",
2497 reloc_offset, relname);
2500 case R_PARISC_DPREL21L:
2501 if (strstart(sym_name, "__op_param", &p))
2503 " hppa_load_imm21l((uint32_t *)(gen_code_ptr + %d), param%s, %d);\n",
2504 reloc_offset, p, addend);
2507 " hppa_patch21l_dprel((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2508 reloc_offset, relname, addend);
2510 case R_PARISC_DPREL14R:
2511 if (strstart(sym_name, "__op_param", &p))
2513 " hppa_load_imm14r((uint32_t *)(gen_code_ptr + %d), param%s, %d);\n",
2514 reloc_offset, p, addend);
2517 " hppa_patch14r_dprel((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2518 reloc_offset, relname, addend);
2521 error("unsupported hppa relocation (%d)", type);
2527 #elif defined(HOST_MIPS) || defined(HOST_MIPS64)
2529 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
2530 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
2536 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
2537 /* the compiler leave some unnecessary references to the code */
2538 if (sym_name[0] == '\0')
2540 get_reloc_expr(relname, sizeof(relname), sym_name);
2541 type = ELF32_R_TYPE(rel->r_info);
2542 addend = get32((uint32_t *)(text + rel->r_offset));
2543 reloc_offset = rel->r_offset - start_offset;
2546 fprintf(outfile, " /* R_MIPS_26 RELOC, offset 0x%x, name %s */\n",
2547 rel->r_offset, sym_name);
2549 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2550 "(0x%x & ~0x3fffff) "
2551 "| ((0x%x + ((%s - (*(uint32_t *)(gen_code_ptr + 0x%x))) >> 2)) "
2553 reloc_offset, addend, addend, relname, reloc_offset);
2556 fprintf(outfile, " /* R_MIPS_HI16 RELOC, offset 0x%x, name %s */\n",
2557 rel->r_offset, sym_name);
2559 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2560 "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
2562 " | (((%s - 0x8000) >> 16) & 0xffff);\n",
2563 reloc_offset, reloc_offset, relname);
2566 fprintf(outfile, " /* R_MIPS_LO16 RELOC, offset 0x%x, name %s */\n",
2567 rel->r_offset, sym_name);
2569 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2570 "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
2572 " | (%s & 0xffff);\n",
2573 reloc_offset, reloc_offset, relname);
2576 fprintf(outfile, " /* R_MIPS_PC16 RELOC, offset 0x%x, name %s */\n",
2577 rel->r_offset, sym_name);
2579 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2581 "| ((0x%x + ((%s - (*(uint32_t *)(gen_code_ptr + 0x%x))) >> 2)) "
2583 reloc_offset, addend, addend, relname, reloc_offset);
2587 fprintf(outfile, " /* R_MIPS_GOT16 RELOC, offset 0x%x, name %s */\n",
2588 rel->r_offset, sym_name);
2590 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2591 "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
2593 " | (((%s - 0x8000) >> 16) & 0xffff);\n",
2594 reloc_offset, reloc_offset, relname);
2597 error("unsupported MIPS relocation (%d)", type);
2602 #elif defined(HOST_ARM)
2603 error("dyngen targets not supported on ARM");
2604 #elif defined(HOST_PPC64)
2605 error("dyngen targets not supported on PPC64");
2607 #error unsupported CPU
2609 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
2610 fprintf(outfile, "}\n");
2611 fprintf(outfile, "break;\n\n");
2613 fprintf(outfile, "static inline void gen_%s(", name);
2615 fprintf(outfile, "void");
2617 for(i = 0; i < nb_args; i++) {
2619 fprintf(outfile, ", ");
2620 fprintf(outfile, "long param%d", i + 1);
2623 fprintf(outfile, ")\n");
2624 fprintf(outfile, "{\n");
2625 for(i = 0; i < nb_args; i++) {
2626 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
2628 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
2629 fprintf(outfile, "}\n\n");
2633 static int gen_file(FILE *outfile, int out_type)
2638 if (out_type == OUT_INDEX_OP) {
2639 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2641 name = get_sym_name(sym);
2642 if (strstart(name, OP_PREFIX, NULL)) {
2643 gen_code(name, sym->st_value, sym->st_size, outfile, 2);
2646 } else if (out_type == OUT_GEN_OP) {
2647 /* generate gen_xxx functions */
2648 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2650 name = get_sym_name(sym);
2651 if (strstart(name, OP_PREFIX, NULL)) {
2652 #if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
2653 if (sym->st_shndx != text_shndx)
2654 error("invalid section for opcode (0x%x)", sym->st_shndx);
2656 gen_code(name, sym->st_value, sym->st_size, outfile, 0);
2661 /* generate big code generation switch */
2664 error("dyngen targets not supported on ARM");
2669 long addend, not_first = 0;
2670 unsigned long sym_idx;
2671 int index, max_index;
2672 const char *sym_name;
2676 for (i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2677 sym_idx = ELF64_R_SYM(rel->r_info);
2678 sym_name = (strtab + symtab[sym_idx].st_name);
2679 if (strstart(sym_name, "__op_gen_label", NULL))
2681 if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B)
2684 addend = rel->r_addend;
2685 index = get_plt_index(sym_name, addend);
2686 if (index <= max_index)
2689 fprintf(outfile, " extern void %s(void);\n", sym_name);
2693 " struct ia64_fixup *plt_fixes = NULL, "
2694 "*ltoff_fixes = NULL;\n"
2695 " static long plt_target[] = {\n\t");
2698 for (i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2699 sym_idx = ELF64_R_SYM(rel->r_info);
2700 sym_name = (strtab + symtab[sym_idx].st_name);
2701 if (strstart(sym_name, "__op_gen_label", NULL))
2703 if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B)
2706 addend = rel->r_addend;
2707 index = get_plt_index(sym_name, addend);
2708 if (index <= max_index)
2713 fprintf(outfile, ",\n\t");
2716 fprintf(outfile, "(long) &%s + %ld", sym_name, addend);
2718 fprintf(outfile, "(long) &%s", sym_name);
2720 fprintf(outfile, "\n };\n"
2721 " unsigned int plt_offset[%u] = { 0 };\n", max_index + 1);
2725 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2727 name = get_sym_name(sym);
2728 if (strstart(name, OP_PREFIX, NULL)) {
2730 printf("%4d: %s pos=0x%08x len=%d\n",
2731 i, name, sym->st_value, sym->st_size);
2733 #if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
2734 if (sym->st_shndx != text_shndx)
2735 error("invalid section for opcode (0x%x)", sym->st_shndx);
2737 gen_code(name, sym->st_value, sym->st_size, outfile, 1);
2745 static void usage(void)
2747 printf("dyngen (c) 2003 Fabrice Bellard\n"
2748 "usage: dyngen [-o outfile] [-c] objfile\n"
2749 "Generate a dynamic code generator from an object file\n"
2750 "-c output enum of operations\n"
2751 "-g output gen_op_xx() functions\n"
2756 int main(int argc, char **argv)
2759 const char *filename, *outfilename;
2762 outfilename = "out.c";
2763 out_type = OUT_CODE;
2765 c = getopt(argc, argv, "ho:cg");
2773 outfilename = optarg;
2776 out_type = OUT_INDEX_OP;
2779 out_type = OUT_GEN_OP;
2785 filename = argv[optind];
2786 outfile = fopen(outfilename, "w");
2788 error("could not open '%s'", outfilename);
2790 load_object(filename);
2791 gen_file(outfile, out_type);