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_AMD64)
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
121 #error unsupported CPU - please update the code
126 #if ELF_CLASS == ELFCLASS32
127 typedef int32_t host_long;
128 typedef uint32_t host_ulong;
129 #define swabls(x) swab32s(x)
131 typedef int64_t host_long;
132 typedef uint64_t host_ulong;
133 #define swabls(x) swab64s(x)
136 #ifdef ELF_USES_RELOCA
137 #define SHT_RELOC SHT_RELA
139 #define SHT_RELOC SHT_REL
142 #define EXE_RELOC ELF_RELOC
143 #define EXE_SYM ElfW(Sym)
145 #endif /* CONFIG_FORMAT_ELF */
147 #ifdef CONFIG_FORMAT_COFF
151 typedef int32_t host_long;
152 typedef uint32_t host_ulong;
154 #define FILENAMELEN 256
156 typedef struct coff_sym {
157 struct external_syment *st_syment;
158 char st_name[FILENAMELEN];
165 typedef struct coff_rel {
166 struct external_reloc *r_reloc;
171 #define EXE_RELOC struct coff_rel
172 #define EXE_SYM struct coff_sym
174 #endif /* CONFIG_FORMAT_COFF */
176 #ifdef CONFIG_FORMAT_MACH
178 #include <mach-o/loader.h>
179 #include <mach-o/nlist.h>
180 #include <mach-o/reloc.h>
181 #include <mach-o/ppc/reloc.h>
183 # define check_mach_header(x) (x.magic == MH_MAGIC)
184 typedef int32_t host_long;
185 typedef uint32_t host_ulong;
187 struct nlist_extended
193 unsigned char n_type;
194 unsigned char n_sect;
196 unsigned long st_value;
197 unsigned long st_size;
200 #define EXE_RELOC struct relocation_info
201 #define EXE_SYM struct nlist_extended
203 #endif /* CONFIG_FORMAT_MACH */
213 /* all dynamically generated functions begin with this code */
214 #define OP_PREFIX "op_"
218 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
222 fprintf(stderr, "dyngen: ");
223 vfprintf(stderr, fmt, ap);
224 fprintf(stderr, "\n");
229 void *load_data(int fd, long offset, unsigned int size)
236 lseek(fd, offset, SEEK_SET);
237 if (read(fd, data, size) != size) {
244 int strstart(const char *str, const char *val, const char **ptr)
260 void pstrcpy(char *buf, int buf_size, const char *str)
270 if (c == 0 || q >= buf + buf_size - 1)
277 void swab16s(uint16_t *p)
282 void swab32s(uint32_t *p)
287 void swab64s(uint64_t *p)
292 uint16_t get16(uint16_t *p)
301 uint32_t get32(uint32_t *p)
310 void put16(uint16_t *p, uint16_t val)
317 void put32(uint32_t *p, uint32_t val)
324 /* executable information */
332 #ifdef CONFIG_FORMAT_ELF
335 struct elf_shdr *shdr;
340 int elf_must_swap(struct elfhdr *h)
348 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
349 (swaptest.b[0] == 0);
352 void elf_swap_ehdr(struct elfhdr *h)
354 swab16s(&h->e_type); /* Object file type */
355 swab16s(&h-> e_machine); /* Architecture */
356 swab32s(&h-> e_version); /* Object file version */
357 swabls(&h-> e_entry); /* Entry point virtual address */
358 swabls(&h-> e_phoff); /* Program header table file offset */
359 swabls(&h-> e_shoff); /* Section header table file offset */
360 swab32s(&h-> e_flags); /* Processor-specific flags */
361 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
362 swab16s(&h-> e_phentsize); /* Program header table entry size */
363 swab16s(&h-> e_phnum); /* Program header table entry count */
364 swab16s(&h-> e_shentsize); /* Section header table entry size */
365 swab16s(&h-> e_shnum); /* Section header table entry count */
366 swab16s(&h-> e_shstrndx); /* Section header string table index */
369 void elf_swap_shdr(struct elf_shdr *h)
371 swab32s(&h-> sh_name); /* Section name (string tbl index) */
372 swab32s(&h-> sh_type); /* Section type */
373 swabls(&h-> sh_flags); /* Section flags */
374 swabls(&h-> sh_addr); /* Section virtual addr at execution */
375 swabls(&h-> sh_offset); /* Section file offset */
376 swabls(&h-> sh_size); /* Section size in bytes */
377 swab32s(&h-> sh_link); /* Link to another section */
378 swab32s(&h-> sh_info); /* Additional section information */
379 swabls(&h-> sh_addralign); /* Section alignment */
380 swabls(&h-> sh_entsize); /* Entry size if section holds table */
383 void elf_swap_phdr(struct elf_phdr *h)
385 swab32s(&h->p_type); /* Segment type */
386 swabls(&h->p_offset); /* Segment file offset */
387 swabls(&h->p_vaddr); /* Segment virtual address */
388 swabls(&h->p_paddr); /* Segment physical address */
389 swabls(&h->p_filesz); /* Segment size in file */
390 swabls(&h->p_memsz); /* Segment size in memory */
391 swab32s(&h->p_flags); /* Segment flags */
392 swabls(&h->p_align); /* Segment alignment */
395 void elf_swap_rel(ELF_RELOC *rel)
397 swabls(&rel->r_offset);
398 swabls(&rel->r_info);
399 #ifdef ELF_USES_RELOCA
400 swabls(&rel->r_addend);
404 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
409 struct elf_shdr *sec;
411 for(i = 0; i < shnum; i++) {
415 shname = shstr + sec->sh_name;
416 if (!strcmp(shname, name))
422 int find_reloc(int sh_index)
424 struct elf_shdr *sec;
427 for(i = 0; i < ehdr.e_shnum; i++) {
429 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
435 static host_ulong get_rel_offset(EXE_RELOC *rel)
437 return rel->r_offset;
440 static char *get_rel_sym_name(EXE_RELOC *rel)
442 return strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
445 static char *get_sym_name(EXE_SYM *sym)
447 return strtab + sym->st_name;
450 /* load an elf object file */
451 int load_object(const char *filename)
454 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
460 fd = open(filename, O_RDONLY);
462 error("can't open file '%s'", filename);
464 /* Read ELF header. */
465 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
466 error("unable to read file header");
468 /* Check ELF identification. */
469 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
470 || ehdr.e_ident[EI_MAG1] != ELFMAG1
471 || ehdr.e_ident[EI_MAG2] != ELFMAG2
472 || ehdr.e_ident[EI_MAG3] != ELFMAG3
473 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
474 error("bad ELF header");
477 do_swap = elf_must_swap(&ehdr);
479 elf_swap_ehdr(&ehdr);
480 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
481 error("Unsupported ELF class");
482 if (ehdr.e_type != ET_REL)
483 error("ELF object file expected");
484 if (ehdr.e_version != EV_CURRENT)
485 error("Invalid ELF version");
486 if (!elf_check_arch(ehdr.e_machine))
487 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
489 /* read section headers */
490 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
492 for(i = 0; i < ehdr.e_shnum; i++) {
493 elf_swap_shdr(&shdr[i]);
497 /* read all section data */
498 sdata = malloc(sizeof(void *) * ehdr.e_shnum);
499 memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
501 for(i = 0;i < ehdr.e_shnum; i++) {
503 if (sec->sh_type != SHT_NOBITS)
504 sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
507 sec = &shdr[ehdr.e_shstrndx];
508 shstr = sdata[ehdr.e_shstrndx];
510 /* swap relocations */
511 for(i = 0; i < ehdr.e_shnum; i++) {
513 if (sec->sh_type == SHT_RELOC) {
514 nb_relocs = sec->sh_size / sec->sh_entsize;
516 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
523 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
525 error("could not find .text section");
526 text_shndx = text_sec - shdr;
527 text = sdata[text_shndx];
529 /* find text relocations, if any */
532 i = find_reloc(text_shndx);
534 relocs = (ELF_RELOC *)sdata[i];
535 nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
538 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
540 error("could not find .symtab section");
541 strtab_sec = &shdr[symtab_sec->sh_link];
543 symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
544 strtab = sdata[symtab_sec->sh_link];
546 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
548 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
549 swab32s(&sym->st_name);
550 swabls(&sym->st_value);
551 swabls(&sym->st_size);
552 swab16s(&sym->st_shndx);
559 #endif /* CONFIG_FORMAT_ELF */
561 #ifdef CONFIG_FORMAT_COFF
564 struct external_scnhdr *shdr;
566 struct external_filehdr fhdr;
567 struct external_syment *coff_symtab;
569 int coff_text_shndx, coff_data_shndx;
573 #define STRTAB_SIZE 4
578 #define T_FUNCTION 0x20
581 void sym_ent_name(struct external_syment *ext_sym, EXE_SYM *sym)
586 if (ext_sym->e.e.e_zeroes != 0) {
588 for(i = 0; i < 8; i++) {
589 c = ext_sym->e.e_name[i];
596 pstrcpy(sym->st_name, sizeof(sym->st_name), strtab + ext_sym->e.e.e_offset);
599 /* now convert the name to a C name (suppress the leading '_') */
600 if (sym->st_name[0] == '_') {
601 len = strlen(sym->st_name);
602 memmove(sym->st_name, sym->st_name + 1, len - 1);
603 sym->st_name[len - 1] = '\0';
607 char *name_for_dotdata(struct coff_rel *rel)
610 struct coff_sym *sym;
613 text_data = *(uint32_t *)(text + rel->r_offset);
615 for (i = 0, sym = symtab; i < nb_syms; i++, sym++) {
616 if (sym->st_syment->e_scnum == data_shndx &&
617 text_data >= sym->st_value &&
618 text_data < sym->st_value + sym->st_size) {
627 static char *get_sym_name(EXE_SYM *sym)
632 static char *get_rel_sym_name(EXE_RELOC *rel)
635 name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx));
636 if (!strcmp(name, ".data"))
637 name = name_for_dotdata(rel);
641 static host_ulong get_rel_offset(EXE_RELOC *rel)
643 return rel->r_offset;
646 struct external_scnhdr *find_coff_section(struct external_scnhdr *shdr, int shnum, const char *name)
650 struct external_scnhdr *sec;
652 for(i = 0; i < shnum; i++) {
656 shname = sec->s_name;
657 if (!strcmp(shname, name))
663 /* load a coff object file */
664 int load_object(const char *filename)
667 struct external_scnhdr *sec, *text_sec, *data_sec;
669 struct external_syment *ext_sym;
670 struct external_reloc *coff_relocs;
671 struct external_reloc *ext_rel;
676 fd = open(filename, O_RDONLY
682 error("can't open file '%s'", filename);
684 /* Read COFF header. */
685 if (read(fd, &fhdr, sizeof (fhdr)) != sizeof (fhdr))
686 error("unable to read file header");
688 /* Check COFF identification. */
689 if (fhdr.f_magic != I386MAGIC) {
690 error("bad COFF header");
694 /* read section headers */
695 shdr = load_data(fd, sizeof(struct external_filehdr) + fhdr.f_opthdr, fhdr.f_nscns * sizeof(struct external_scnhdr));
697 /* read all section data */
698 sdata = malloc(sizeof(void *) * fhdr.f_nscns);
699 memset(sdata, 0, sizeof(void *) * fhdr.f_nscns);
702 for(i = 0;i < fhdr.f_nscns; i++) {
704 if (!strstart(sec->s_name, ".bss", &p))
705 sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size);
710 text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text");
712 error("could not find .text section");
713 coff_text_shndx = text_sec - shdr;
714 text = sdata[coff_text_shndx];
717 data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data");
719 error("could not find .data section");
720 coff_data_shndx = data_sec - shdr;
722 coff_symtab = load_data(fd, fhdr.f_symptr, fhdr.f_nsyms*SYMESZ);
723 for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
725 printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]);
730 n_strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), STRTAB_SIZE);
731 strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), *n_strtab);
733 nb_syms = fhdr.f_nsyms;
735 for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
736 if (strstart(ext_sym->e.e_name, ".text", NULL))
737 text_shndx = ext_sym->e_scnum;
738 if (strstart(ext_sym->e.e_name, ".data", NULL))
739 data_shndx = ext_sym->e_scnum;
742 /* set coff symbol */
743 symtab = malloc(sizeof(struct coff_sym) * nb_syms);
746 for (i = 0, ext_sym = coff_symtab, sym = symtab; i < nb_syms; i++, ext_sym++, sym++) {
747 memset(sym, 0, sizeof(*sym));
748 sym->st_syment = ext_sym;
749 sym_ent_name(ext_sym, sym);
750 sym->st_value = ext_sym->e_value;
752 aux_size = *(int8_t *)ext_sym->e_numaux;
753 if (ext_sym->e_scnum == text_shndx && ext_sym->e_type == T_FUNCTION) {
754 for (j = aux_size + 1; j < nb_syms - i; j++) {
755 if ((ext_sym + j)->e_scnum == text_shndx &&
756 (ext_sym + j)->e_type == T_FUNCTION ){
757 sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
759 } else if (j == nb_syms - i - 1) {
760 sec = &shdr[coff_text_shndx];
761 sym->st_size = sec->s_size - ext_sym->e_value;
765 } else if (ext_sym->e_scnum == data_shndx && *(uint8_t *)ext_sym->e_sclass == C_EXTERNAL) {
766 for (j = aux_size + 1; j < nb_syms - i; j++) {
767 if ((ext_sym + j)->e_scnum == data_shndx) {
768 sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
770 } else if (j == nb_syms - i - 1) {
771 sec = &shdr[coff_data_shndx];
772 sym->st_size = sec->s_size - ext_sym->e_value;
780 sym->st_type = ext_sym->e_type;
781 sym->st_shndx = ext_sym->e_scnum;
785 /* find text relocations, if any */
786 sec = &shdr[coff_text_shndx];
787 coff_relocs = load_data(fd, sec->s_relptr, sec->s_nreloc*RELSZ);
788 nb_relocs = sec->s_nreloc;
790 /* set coff relocation */
791 relocs = malloc(sizeof(struct coff_rel) * nb_relocs);
792 for (i = 0, ext_rel = coff_relocs, rel = relocs; i < nb_relocs;
793 i++, ext_rel++, rel++) {
794 memset(rel, 0, sizeof(*rel));
795 rel->r_reloc = ext_rel;
796 rel->r_offset = *(uint32_t *)ext_rel->r_vaddr;
797 rel->r_type = *(uint16_t *)ext_rel->r_type;
802 #endif /* CONFIG_FORMAT_COFF */
804 #ifdef CONFIG_FORMAT_MACH
807 struct mach_header mach_hdr;
810 struct segment_command *segment = 0;
811 struct dysymtab_command *dysymtabcmd = 0;
812 struct symtab_command *symtabcmd = 0;
815 struct section *section_hdr;
816 struct section *text_sec_hdr;
820 struct relocation_info *relocs;
824 struct nlist *symtab_std;
827 /* indirect symbols */
830 /* Utility functions */
832 static inline char *find_str_by_index(int index)
837 /* Used by dyngen common code */
838 static char *get_sym_name(EXE_SYM *sym)
840 char *name = find_str_by_index(sym->n_un.n_strx);
842 if ( sym->n_type & N_STAB ) /* Debug symbols are ignored */
853 /* find a section index given its segname, sectname */
854 static int find_mach_sec_index(struct section *section_hdr, int shnum, const char *segname,
855 const char *sectname)
858 struct section *sec = section_hdr;
860 for(i = 0; i < shnum; i++, sec++) {
861 if (!sec->segname || !sec->sectname)
863 if (!strcmp(sec->sectname, sectname) && !strcmp(sec->segname, segname))
869 /* find a section header given its segname, sectname */
870 struct section *find_mach_sec_hdr(struct section *section_hdr, int shnum, const char *segname,
871 const char *sectname)
873 int index = find_mach_sec_index(section_hdr, shnum, segname, sectname);
876 return section_hdr+index;
880 static inline void fetch_next_pair_value(struct relocation_info * rel, unsigned int *value)
882 struct scattered_relocation_info * scarel;
884 if(R_SCATTERED & rel->r_address) {
885 scarel = (struct scattered_relocation_info*)rel;
886 if(scarel->r_type != PPC_RELOC_PAIR)
887 error("fetch_next_pair_value: looking for a pair which was not found (1)");
888 *value = scarel->r_value;
890 if(rel->r_type != PPC_RELOC_PAIR)
891 error("fetch_next_pair_value: looking for a pair which was not found (2)");
892 *value = rel->r_address;
896 /* find a sym name given its value, in a section number */
897 static const char * find_sym_with_value_and_sec_number( int value, int sectnum, int * offset )
901 for( i = 0 ; i < nb_syms; i++ )
903 if( !(symtab[i].n_type & N_STAB) && (symtab[i].n_type & N_SECT) &&
904 (symtab[i].n_sect == sectnum) && (symtab[i].st_value <= value) )
906 if( (ret<0) || (symtab[i].st_value >= symtab[ret].st_value) )
914 *offset = value - symtab[ret].st_value;
915 return get_sym_name(&symtab[ret]);
920 * Find symbol name given a (virtual) address, and a section which is of type
921 * S_NON_LAZY_SYMBOL_POINTERS or S_LAZY_SYMBOL_POINTERS or S_SYMBOL_STUBS
923 static const char * find_reloc_name_in_sec_ptr(int address, struct section * sec_hdr)
925 unsigned int tocindex, symindex, size;
926 const char *name = 0;
929 if(!( address >= sec_hdr->addr && address < (sec_hdr->addr + sec_hdr->size) ) )
932 if( sec_hdr->flags & S_SYMBOL_STUBS ){
933 size = sec_hdr->reserved2;
938 else if( sec_hdr->flags & S_LAZY_SYMBOL_POINTERS ||
939 sec_hdr->flags & S_NON_LAZY_SYMBOL_POINTERS)
940 size = sizeof(unsigned long);
944 /* Compute our index in toc */
945 tocindex = (address - sec_hdr->addr)/size;
946 symindex = tocdylib[sec_hdr->reserved1 + tocindex];
948 name = get_sym_name(&symtab[symindex]);
953 static const char * find_reloc_name_given_its_address(int address)
956 for(i = 0; i < segment->nsects ; i++)
958 const char * name = find_reloc_name_in_sec_ptr(address, §ion_hdr[i]);
965 static const char * get_reloc_name(EXE_RELOC * rel, int * sslide)
968 struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
969 int sectnum = rel->r_symbolnum;
973 /* init the slide value */
976 if(R_SCATTERED & rel->r_address)
977 return (char *)find_reloc_name_given_its_address(sca_rel->r_value);
981 /* ignore debug sym */
982 if ( symtab[rel->r_symbolnum].n_type & N_STAB )
984 return get_sym_name(&symtab[rel->r_symbolnum]);
987 /* Intruction contains an offset to the symbols pointed to, in the rel->r_symbolnum section */
988 sectoffset = *(uint32_t *)(text + rel->r_address) & 0xffff;
990 if(sectnum==0xffffff)
994 if(sectnum > segment->nsects)
995 error("sectnum > segment->nsects");
999 case PPC_RELOC_LO16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset & 0xffff);
1001 case PPC_RELOC_HI16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (other_half & 0xffff);
1003 case PPC_RELOC_HA16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (other_half & 0xffff);
1005 case PPC_RELOC_BR24:
1006 sectoffset = ( *(uint32_t *)(text + rel->r_address) & 0x03fffffc );
1007 if (sectoffset & 0x02000000) sectoffset |= 0xfc000000;
1010 error("switch(rel->type) not found");
1014 sectoffset += rel->r_address;
1016 if (rel->r_type == PPC_RELOC_BR24)
1017 name = (char *)find_reloc_name_in_sec_ptr((int)sectoffset, §ion_hdr[sectnum-1]);
1019 /* search it in the full symbol list, if not found */
1021 name = (char *)find_sym_with_value_and_sec_number(sectoffset, sectnum, sslide);
1026 /* Used by dyngen common code */
1027 static const char * get_rel_sym_name(EXE_RELOC * rel)
1030 return get_reloc_name( rel, &sslide);
1033 /* Used by dyngen common code */
1034 static host_ulong get_rel_offset(EXE_RELOC *rel)
1036 struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
1037 if(R_SCATTERED & rel->r_address)
1038 return sca_rel->r_address;
1040 return rel->r_address;
1043 /* load a mach-o object file */
1044 int load_object(const char *filename)
1047 unsigned int offset_to_segment = 0;
1048 unsigned int offset_to_dysymtab = 0;
1049 unsigned int offset_to_symtab = 0;
1050 struct load_command lc;
1053 struct nlist *syment;
1055 fd = open(filename, O_RDONLY);
1057 error("can't open file '%s'", filename);
1059 /* Read Mach header. */
1060 if (read(fd, &mach_hdr, sizeof (mach_hdr)) != sizeof (mach_hdr))
1061 error("unable to read file header");
1063 /* Check Mach identification. */
1064 if (!check_mach_header(mach_hdr)) {
1065 error("bad Mach header");
1068 if (mach_hdr.cputype != CPU_TYPE_POWERPC)
1069 error("Unsupported CPU");
1071 if (mach_hdr.filetype != MH_OBJECT)
1072 error("Unsupported Mach Object");
1074 /* read segment headers */
1075 for(i=0, j=sizeof(mach_hdr); i<mach_hdr.ncmds ; i++)
1077 if(read(fd, &lc, sizeof(struct load_command)) != sizeof(struct load_command))
1078 error("unable to read load_command");
1079 if(lc.cmd == LC_SEGMENT)
1081 offset_to_segment = j;
1082 lseek(fd, offset_to_segment, SEEK_SET);
1083 segment = malloc(sizeof(struct segment_command));
1084 if(read(fd, segment, sizeof(struct segment_command)) != sizeof(struct segment_command))
1085 error("unable to read LC_SEGMENT");
1087 if(lc.cmd == LC_DYSYMTAB)
1089 offset_to_dysymtab = j;
1090 lseek(fd, offset_to_dysymtab, SEEK_SET);
1091 dysymtabcmd = malloc(sizeof(struct dysymtab_command));
1092 if(read(fd, dysymtabcmd, sizeof(struct dysymtab_command)) != sizeof(struct dysymtab_command))
1093 error("unable to read LC_DYSYMTAB");
1095 if(lc.cmd == LC_SYMTAB)
1097 offset_to_symtab = j;
1098 lseek(fd, offset_to_symtab, SEEK_SET);
1099 symtabcmd = malloc(sizeof(struct symtab_command));
1100 if(read(fd, symtabcmd, sizeof(struct symtab_command)) != sizeof(struct symtab_command))
1101 error("unable to read LC_SYMTAB");
1105 lseek(fd, j, SEEK_SET);
1109 error("unable to find LC_SEGMENT");
1111 /* read section headers */
1112 section_hdr = load_data(fd, offset_to_segment + sizeof(struct segment_command), segment->nsects * sizeof(struct section));
1114 /* read all section data */
1115 sdata = (uint8_t **)malloc(sizeof(void *) * segment->nsects);
1116 memset(sdata, 0, sizeof(void *) * segment->nsects);
1118 /* Load the data in section data */
1119 for(i = 0; i < segment->nsects; i++) {
1120 sdata[i] = load_data(fd, section_hdr[i].offset, section_hdr[i].size);
1124 text_sec_hdr = find_mach_sec_hdr(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT);
1125 i = find_mach_sec_index(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT);
1126 if (i == -1 || !text_sec_hdr)
1127 error("could not find __TEXT,__text section");
1130 /* Make sure dysym was loaded */
1131 if(!(int)dysymtabcmd)
1132 error("could not find __DYSYMTAB segment");
1134 /* read the table of content of the indirect sym */
1135 tocdylib = load_data( fd, dysymtabcmd->indirectsymoff, dysymtabcmd->nindirectsyms * sizeof(uint32_t) );
1137 /* Make sure symtab was loaded */
1139 error("could not find __SYMTAB segment");
1140 nb_syms = symtabcmd->nsyms;
1142 symtab_std = load_data(fd, symtabcmd->symoff, symtabcmd->nsyms * sizeof(struct nlist));
1143 strtab = load_data(fd, symtabcmd->stroff, symtabcmd->strsize);
1145 symtab = malloc(sizeof(EXE_SYM) * nb_syms);
1147 /* Now transform the symtab, to an extended version, with the sym size, and the C name */
1148 for(i = 0, sym = symtab, syment = symtab_std; i < nb_syms; i++, sym++, syment++) {
1150 struct nlist *sym_follow, *sym_next = 0;
1152 name = find_str_by_index(sym->n_un.n_strx);
1153 memset(sym, 0, sizeof(*sym));
1155 if ( sym->n_type & N_STAB ) /* Debug symbols are skipped */
1158 memcpy(sym, syment, sizeof(*syment));
1160 /* Find the following symbol in order to get the current symbol size */
1161 for(j = 0, sym_follow = symtab_std; j < nb_syms; j++, sym_follow++) {
1162 if ( sym_follow->n_sect != 1 || sym_follow->n_type & N_STAB || !(sym_follow->n_value > sym->st_value))
1165 sym_next = sym_follow;
1168 if(!(sym_next->n_value > sym_follow->n_value))
1170 sym_next = sym_follow;
1173 sym->st_size = sym_next->n_value - sym->st_value;
1175 sym->st_size = text_sec_hdr->size - sym->st_value;
1179 relocs = load_data(fd, text_sec_hdr->reloff, text_sec_hdr->nreloc * sizeof(struct relocation_info));
1180 nb_relocs = text_sec_hdr->nreloc;
1186 #endif /* CONFIG_FORMAT_MACH */
1190 int arm_emit_ldr_info(const char *name, unsigned long start_offset,
1191 FILE *outfile, uint8_t *p_start, uint8_t *p_end,
1192 ELF_RELOC *relocs, int nb_relocs)
1196 int offset, min_offset, pc_offset, data_size;
1197 uint8_t data_allocated[1024];
1198 unsigned int data_index;
1200 memset(data_allocated, 0, sizeof(data_allocated));
1203 min_offset = p_end - p_start;
1204 while (p < p_start + min_offset) {
1205 insn = get32((uint32_t *)p);
1206 if ((insn & 0x0d5f0000) == 0x051f0000) {
1207 /* ldr reg, [pc, #im] */
1208 offset = insn & 0xfff;
1209 if (!(insn & 0x00800000))
1211 if ((offset & 3) !=0)
1212 error("%s:%04x: ldr pc offset must be 32 bit aligned",
1213 name, start_offset + p - p_start);
1214 pc_offset = p - p_start + offset + 8;
1215 if (pc_offset <= (p - p_start) ||
1216 pc_offset >= (p_end - p_start))
1217 error("%s:%04x: ldr pc offset must point inside the function code",
1218 name, start_offset + p - p_start);
1219 if (pc_offset < min_offset)
1220 min_offset = pc_offset;
1223 fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
1225 /* ldr data index */
1226 data_index = ((p_end - p_start) - pc_offset - 4) >> 2;
1227 fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
1229 fprintf(outfile, " arm_ldr_ptr++;\n");
1230 if (data_index >= sizeof(data_allocated))
1231 error("%s: too many data", name);
1232 if (!data_allocated[data_index]) {
1234 int i, addend, type;
1235 const char *sym_name, *p;
1238 data_allocated[data_index] = 1;
1241 addend = get32((uint32_t *)(p_start + pc_offset));
1243 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1244 if (rel->r_offset == (pc_offset + start_offset)) {
1245 sym_name = get_rel_sym_name(rel);
1246 /* the compiler leave some unnecessary references to the code */
1247 if (strstart(sym_name, "__op_param", &p)) {
1248 snprintf(relname, sizeof(relname), "param%s", p);
1250 snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name);
1252 type = ELF32_R_TYPE(rel->r_info);
1253 if (type != R_ARM_ABS32)
1254 error("%s: unsupported data relocation", name);
1258 fprintf(outfile, " arm_data_ptr[%d] = 0x%x",
1259 data_index, addend);
1260 if (relname[0] != '\0')
1261 fprintf(outfile, " + %s", relname);
1262 fprintf(outfile, ";\n");
1268 data_size = (p_end - p_start) - min_offset;
1269 if (data_size > 0 && outfile) {
1270 fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2);
1273 /* the last instruction must be a mov pc, lr */
1277 insn = get32((uint32_t *)p);
1278 if ((insn & 0xffff0000) != 0xe91b0000) {
1281 printf("%s: invalid epilog\n", name);
1290 /* generate op code */
1291 void gen_code(const char *name, host_ulong offset, host_ulong size,
1292 FILE *outfile, int gen_switch)
1295 uint8_t *p_start, *p_end;
1296 host_ulong start_offset;
1298 uint8_t args_present[MAX_ARGS];
1299 const char *sym_name, *p;
1302 /* Compute exact size excluding prologue and epilogue instructions.
1303 * Increment start_offset to skip epilogue instructions, then compute
1304 * copy_size the indicate the size of the remaining instructions (in
1307 p_start = text + offset;
1308 p_end = p_start + size;
1309 start_offset = offset;
1310 #if defined(HOST_I386) || defined(HOST_AMD64)
1311 #ifdef CONFIG_FORMAT_COFF
1316 error("empty code for %s", name);
1317 while (*p != 0xc3) {
1320 error("ret or jmp expected at the end of %s", name);
1322 copy_size = p - p_start;
1327 len = p_end - p_start;
1329 error("empty code for %s", name);
1330 if (p_end[-1] == 0xc3) {
1333 error("ret or jmp expected at the end of %s", name);
1338 #elif defined(HOST_PPC)
1341 p = (void *)(p_end - 4);
1343 error("empty code for %s", name);
1344 if (get32((uint32_t *)p) != 0x4e800020)
1345 error("blr expected at the end of %s", name);
1346 copy_size = p - p_start;
1348 #elif defined(HOST_S390)
1351 p = (void *)(p_end - 2);
1353 error("empty code for %s", name);
1354 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
1355 error("br %%r14 expected at the end of %s", name);
1356 copy_size = p - p_start;
1358 #elif defined(HOST_ALPHA)
1363 /* XXX: check why it occurs */
1365 error("empty code for %s", name);
1367 if (get32((uint32_t *)p) != 0x6bfa8001)
1368 error("ret expected at the end of %s", name);
1369 copy_size = p - p_start;
1371 #elif defined(HOST_IA64)
1374 p = (void *)(p_end - 4);
1376 error("empty code for %s", name);
1377 /* br.ret.sptk.many b0;; */
1379 if (get32((uint32_t *)p) != 0x00840008)
1380 error("br.ret.sptk.many b0;; expected at the end of %s", name);
1381 copy_size = p - p_start;
1383 #elif defined(HOST_SPARC)
1385 uint32_t start_insn, end_insn1, end_insn2;
1387 p = (void *)(p_end - 8);
1389 error("empty code for %s", name);
1390 start_insn = get32((uint32_t *)(p_start + 0x0));
1391 end_insn1 = get32((uint32_t *)(p + 0x0));
1392 end_insn2 = get32((uint32_t *)(p + 0x4));
1393 if ((start_insn & ~0x1fff) == 0x9de3a000) {
1395 start_offset += 0x4;
1396 if ((int)(start_insn | ~0x1fff) < -128)
1397 error("Found bogus save at the start of %s", name);
1398 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
1399 error("ret; restore; not found at end of %s", name);
1401 error("No save at the beginning of %s", name);
1404 /* Skip a preceeding nop, if present. */
1406 skip_insn = get32((uint32_t *)(p - 0x4));
1407 if (skip_insn == 0x01000000)
1411 copy_size = p - p_start;
1413 #elif defined(HOST_SPARC64)
1415 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
1417 p = (void *)(p_end - 8);
1419 error("empty code for %s", name);
1420 start_insn = get32((uint32_t *)(p_start + 0x0));
1421 end_insn1 = get32((uint32_t *)(p + 0x0));
1422 end_insn2 = get32((uint32_t *)(p + 0x4));
1423 if ((start_insn & ~0x1fff) == 0x9de3a000) {
1425 start_offset += 0x4;
1426 if ((int)(start_insn | ~0x1fff) < -256)
1427 error("Found bogus save at the start of %s", name);
1428 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
1429 error("ret; restore; not found at end of %s", name);
1431 error("No save at the beginning of %s", name);
1434 /* Skip a preceeding nop, if present. */
1436 skip_insn = get32((uint32_t *)(p - 0x4));
1437 if (skip_insn == 0x01000000)
1441 copy_size = p - p_start;
1443 #elif defined(HOST_ARM)
1445 if ((p_end - p_start) <= 16)
1446 error("%s: function too small", name);
1447 if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
1448 (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
1449 get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
1450 error("%s: invalid prolog", name);
1453 copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end,
1456 #elif defined(HOST_M68K)
1459 p = (void *)(p_end - 2);
1461 error("empty code for %s", name);
1462 // remove NOP's, probably added for alignment
1463 while ((get16((uint16_t *)p) == 0x4e71) &&
1466 if (get16((uint16_t *)p) != 0x4e75)
1467 error("rts expected at the end of %s", name);
1468 copy_size = p - p_start;
1471 #error unsupported CPU
1474 /* compute the number of arguments by looking at the relocations */
1475 for(i = 0;i < MAX_ARGS; i++)
1476 args_present[i] = 0;
1478 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1479 host_ulong offset = get_rel_offset(rel);
1480 if (offset >= start_offset &&
1481 offset < start_offset + (p_end - p_start)) {
1482 sym_name = get_rel_sym_name(rel);
1485 if (strstart(sym_name, "__op_param", &p)) {
1486 n = strtoul(p, NULL, 10);
1488 error("too many arguments in %s", name);
1489 args_present[n - 1] = 1;
1495 while (nb_args < MAX_ARGS && args_present[nb_args])
1497 for(i = nb_args; i < MAX_ARGS; i++) {
1498 if (args_present[i])
1499 error("inconsistent argument numbering in %s", name);
1502 if (gen_switch == 2) {
1503 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
1504 } else if (gen_switch == 1) {
1507 fprintf(outfile, "case INDEX_%s: {\n", name);
1509 fprintf(outfile, " long ");
1510 for(i = 0; i < nb_args; i++) {
1512 fprintf(outfile, ", ");
1513 fprintf(outfile, "param%d", i + 1);
1515 fprintf(outfile, ";\n");
1517 fprintf(outfile, " extern void %s();\n", name);
1519 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1520 host_ulong offset = get_rel_offset(rel);
1521 if (offset >= start_offset &&
1522 offset < start_offset + (p_end - p_start)) {
1523 sym_name = get_rel_sym_name(rel);
1527 !strstart(sym_name, "__op_param", NULL) &&
1528 !strstart(sym_name, "__op_jmp", NULL)) {
1529 #if defined(HOST_SPARC)
1530 if (sym_name[0] == '.') {
1532 "extern char __dot_%s __asm__(\"%s\");\n",
1533 sym_name+1, sym_name);
1538 /* set __attribute((unused)) on darwin because we wan't to avoid warning when we don't use the symbol */
1539 fprintf(outfile, "extern char %s __attribute__((unused));\n", sym_name);
1541 fprintf(outfile, "extern char %s;\n", sym_name);
1547 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n",
1548 name, (int)(start_offset - offset), copy_size);
1550 /* emit code offset information */
1553 const char *sym_name, *p;
1557 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1558 sym_name = get_sym_name(sym);
1559 if (strstart(sym_name, "__op_label", &p)) {
1561 unsigned long offset;
1563 /* test if the variable refers to a label inside
1564 the code we are generating */
1565 #ifdef CONFIG_FORMAT_COFF
1566 if (sym->st_shndx == text_shndx) {
1567 ptr = sdata[coff_text_shndx];
1568 } else if (sym->st_shndx == data_shndx) {
1569 ptr = sdata[coff_data_shndx];
1573 #elif defined(CONFIG_FORMAT_MACH)
1576 ptr = sdata[sym->n_sect-1];
1578 ptr = sdata[sym->st_shndx];
1581 error("__op_labelN in invalid section");
1582 offset = sym->st_value;
1583 #ifdef CONFIG_FORMAT_MACH
1584 offset -= section_hdr[sym->n_sect-1].addr;
1586 val = *(unsigned long *)(ptr + offset);
1587 #ifdef ELF_USES_RELOCA
1589 int reloc_shndx, nb_relocs1, j;
1591 /* try to find a matching relocation */
1592 reloc_shndx = find_reloc(sym->st_shndx);
1594 nb_relocs1 = shdr[reloc_shndx].sh_size /
1595 shdr[reloc_shndx].sh_entsize;
1596 rel = (ELF_RELOC *)sdata[reloc_shndx];
1597 for(j = 0; j < nb_relocs1; j++) {
1598 if (rel->r_offset == offset) {
1599 val = rel->r_addend;
1608 if (val >= start_offset && val < start_offset + copy_size) {
1609 n = strtol(p, NULL, 10);
1610 fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
1616 /* load parameres in variables */
1617 for(i = 0; i < nb_args; i++) {
1618 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
1621 /* patch relocations */
1622 #if defined(HOST_I386)
1627 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1628 if (rel->r_offset >= start_offset &&
1629 rel->r_offset < start_offset + copy_size) {
1630 sym_name = get_rel_sym_name(rel);
1631 if (strstart(sym_name, "__op_jmp", &p)) {
1633 n = strtol(p, NULL, 10);
1634 /* __op_jmp relocations are done at
1635 runtime to do translated block
1636 chaining: the offset of the instruction
1637 needs to be stored */
1638 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1639 n, rel->r_offset - start_offset);
1643 if (strstart(sym_name, "__op_param", &p)) {
1644 snprintf(name, sizeof(name), "param%s", p);
1646 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1648 addend = get32((uint32_t *)(text + rel->r_offset));
1649 #ifdef CONFIG_FORMAT_ELF
1650 type = ELF32_R_TYPE(rel->r_info);
1653 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1654 rel->r_offset - start_offset, name, addend);
1657 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1658 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1661 error("unsupported i386 relocation (%d)", type);
1663 #elif defined(CONFIG_FORMAT_COFF)
1668 temp_name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx));
1669 if (!strcmp(temp_name, ".data")) {
1670 for (j = 0, sym = symtab; j < nb_syms; j++, sym++) {
1671 if (strstart(sym->st_name, sym_name, NULL)) {
1672 addend -= sym->st_value;
1680 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1681 rel->r_offset - start_offset, name, addend);
1684 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n",
1685 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1688 error("unsupported i386 relocation (%d)", type);
1691 #error unsupport object format
1696 #elif defined(HOST_AMD64)
1701 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1702 if (rel->r_offset >= start_offset &&
1703 rel->r_offset < start_offset + copy_size) {
1704 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1705 if (strstart(sym_name, "__op_param", &p)) {
1706 snprintf(name, sizeof(name), "param%s", p);
1708 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1710 type = ELF32_R_TYPE(rel->r_info);
1711 addend = rel->r_addend;
1714 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
1715 rel->r_offset - start_offset, name, addend);
1718 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
1719 rel->r_offset - start_offset, name, addend);
1722 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1723 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1726 error("unsupported AMD64 relocation (%d)", type);
1731 #elif defined(HOST_PPC)
1733 #ifdef CONFIG_FORMAT_ELF
1737 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1738 if (rel->r_offset >= start_offset &&
1739 rel->r_offset < start_offset + copy_size) {
1740 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1741 if (strstart(sym_name, "__op_jmp", &p)) {
1743 n = strtol(p, NULL, 10);
1744 /* __op_jmp relocations are done at
1745 runtime to do translated block
1746 chaining: the offset of the instruction
1747 needs to be stored */
1748 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1749 n, rel->r_offset - start_offset);
1753 if (strstart(sym_name, "__op_param", &p)) {
1754 snprintf(name, sizeof(name), "param%s", p);
1756 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1758 type = ELF32_R_TYPE(rel->r_info);
1759 addend = rel->r_addend;
1762 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1763 rel->r_offset - start_offset, name, addend);
1765 case R_PPC_ADDR16_LO:
1766 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
1767 rel->r_offset - start_offset, name, addend);
1769 case R_PPC_ADDR16_HI:
1770 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
1771 rel->r_offset - start_offset, name, addend);
1773 case R_PPC_ADDR16_HA:
1774 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
1775 rel->r_offset - start_offset, name, addend);
1778 /* warning: must be at 32 MB distancy */
1779 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
1780 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1783 error("unsupported powerpc relocation (%d)", type);
1787 #elif defined(CONFIG_FORMAT_MACH)
1788 struct scattered_relocation_info *scarel;
1789 struct relocation_info * rel;
1790 char final_sym_name[256];
1791 const char *sym_name;
1796 for(i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
1797 unsigned int offset, length, value = 0;
1798 unsigned int type, pcrel, isym = 0;
1799 unsigned int usesym = 0;
1801 if(R_SCATTERED & rel->r_address) {
1802 scarel = (struct scattered_relocation_info*)rel;
1803 offset = (unsigned int)scarel->r_address;
1804 length = scarel->r_length;
1805 pcrel = scarel->r_pcrel;
1806 type = scarel->r_type;
1807 value = scarel->r_value;
1809 value = isym = rel->r_symbolnum;
1810 usesym = (rel->r_extern);
1811 offset = rel->r_address;
1812 length = rel->r_length;
1813 pcrel = rel->r_pcrel;
1817 slide = offset - start_offset;
1819 if (!(offset >= start_offset && offset < start_offset + size))
1820 continue; /* not in our range */
1822 sym_name = get_reloc_name(rel, &sslide);
1824 if(usesym && symtab[isym].n_type & N_STAB)
1825 continue; /* don't handle STAB (debug sym) */
1827 if (sym_name && strstart(sym_name, "__op_jmp", &p)) {
1829 n = strtol(p, NULL, 10);
1830 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1832 continue; /* Nothing more to do */
1837 fprintf(outfile, "/* #warning relocation not handled in %s (value 0x%x, %s, offset 0x%x, length 0x%x, %s, type 0x%x) */\n",
1838 name, value, usesym ? "use sym" : "don't use sym", offset, length, pcrel ? "pcrel":"", type);
1839 continue; /* dunno how to handle without final_sym_name */
1842 if (strstart(sym_name, "__op_param", &p)) {
1843 snprintf(final_sym_name, sizeof(final_sym_name), "param%s", p);
1845 snprintf(final_sym_name, sizeof(final_sym_name), "(long)(&%s)", sym_name);
1849 case PPC_RELOC_BR24:
1850 fprintf(outfile, "{\n");
1851 fprintf(outfile, " uint32_t imm = *(uint32_t *)(gen_code_ptr + %d) & 0x3fffffc;\n", slide);
1852 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",
1853 slide, slide, name, sslide );
1854 fprintf(outfile, "}\n");
1856 case PPC_RELOC_HI16:
1857 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d) >> 16;\n",
1858 slide, final_sym_name, sslide);
1860 case PPC_RELOC_LO16:
1861 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d);\n",
1862 slide, final_sym_name, sslide);
1864 case PPC_RELOC_HA16:
1865 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d + 0x8000) >> 16;\n",
1866 slide, final_sym_name, sslide);
1869 error("unsupported powerpc relocation (%d)", type);
1873 #error unsupport object format
1876 #elif defined(HOST_S390)
1881 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1882 if (rel->r_offset >= start_offset &&
1883 rel->r_offset < start_offset + copy_size) {
1884 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1885 if (strstart(sym_name, "__op_param", &p)) {
1886 snprintf(name, sizeof(name), "param%s", p);
1888 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1890 type = ELF32_R_TYPE(rel->r_info);
1891 addend = rel->r_addend;
1894 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1895 rel->r_offset - start_offset, name, addend);
1898 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
1899 rel->r_offset - start_offset, name, addend);
1902 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
1903 rel->r_offset - start_offset, name, addend);
1906 error("unsupported s390 relocation (%d)", type);
1911 #elif defined(HOST_ALPHA)
1913 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
1914 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
1917 type = ELF64_R_TYPE(rel->r_info);
1918 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
1920 case R_ALPHA_GPDISP:
1921 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
1922 as an immediate instead of constructing it from the pv or ra. */
1923 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
1924 rel->r_offset - start_offset);
1925 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
1926 rel->r_offset - start_offset + rel->r_addend);
1928 case R_ALPHA_LITUSE:
1929 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
1930 now, since some called functions (libc) need pv to be set up. */
1933 /* Branch target prediction hint. Ignore for now. Should be already
1934 correct for in-function jumps. */
1936 case R_ALPHA_LITERAL:
1937 /* Load a literal from the GOT relative to the gp. Since there's only a
1938 single gp, nothing is to be done. */
1940 case R_ALPHA_GPRELHIGH:
1941 /* Handle fake relocations against __op_param symbol. Need to emit the
1942 high part of the immediate value instead. Other symbols need no
1943 special treatment. */
1944 if (strstart(sym_name, "__op_param", &p))
1945 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
1946 rel->r_offset - start_offset, p);
1948 case R_ALPHA_GPRELLOW:
1949 if (strstart(sym_name, "__op_param", &p))
1950 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
1951 rel->r_offset - start_offset, p);
1954 /* PC-relative jump. Tweak offset to skip the two instructions that try to
1955 set up the gp from the pv. */
1956 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
1957 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
1960 error("unsupported Alpha relocation (%d)", type);
1965 #elif defined(HOST_IA64)
1970 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1971 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
1972 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
1973 if (strstart(sym_name, "__op_param", &p)) {
1974 snprintf(name, sizeof(name), "param%s", p);
1976 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1978 type = ELF64_R_TYPE(rel->r_info);
1979 addend = rel->r_addend;
1981 case R_IA64_LTOFF22:
1982 error("must implemnt R_IA64_LTOFF22 relocation");
1983 case R_IA64_PCREL21B:
1984 error("must implemnt R_IA64_PCREL21B relocation");
1986 error("unsupported ia64 relocation (%d)", type);
1991 #elif defined(HOST_SPARC)
1996 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1997 if (rel->r_offset >= start_offset &&
1998 rel->r_offset < start_offset + copy_size) {
1999 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
2000 if (strstart(sym_name, "__op_param", &p)) {
2001 snprintf(name, sizeof(name), "param%s", p);
2003 if (sym_name[0] == '.')
2004 snprintf(name, sizeof(name),
2005 "(long)(&__dot_%s)",
2008 snprintf(name, sizeof(name),
2009 "(long)(&%s)", sym_name);
2011 type = ELF32_R_TYPE(rel->r_info);
2012 addend = rel->r_addend;
2015 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2016 rel->r_offset - start_offset, name, addend);
2020 " *(uint32_t *)(gen_code_ptr + %d) = "
2021 "((*(uint32_t *)(gen_code_ptr + %d)) "
2023 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2024 rel->r_offset - start_offset,
2025 rel->r_offset - start_offset,
2030 " *(uint32_t *)(gen_code_ptr + %d) = "
2031 "((*(uint32_t *)(gen_code_ptr + %d)) "
2033 " | ((%s + %d) & 0x3ff);\n",
2034 rel->r_offset - start_offset,
2035 rel->r_offset - start_offset,
2038 case R_SPARC_WDISP30:
2040 " *(uint32_t *)(gen_code_ptr + %d) = "
2041 "((*(uint32_t *)(gen_code_ptr + %d)) "
2043 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2044 " & 0x3fffffff);\n",
2045 rel->r_offset - start_offset,
2046 rel->r_offset - start_offset,
2048 rel->r_offset - start_offset);
2051 error("unsupported sparc relocation (%d)", type);
2056 #elif defined(HOST_SPARC64)
2061 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2062 if (rel->r_offset >= start_offset &&
2063 rel->r_offset < start_offset + copy_size) {
2064 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
2065 if (strstart(sym_name, "__op_param", &p)) {
2066 snprintf(name, sizeof(name), "param%s", p);
2068 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
2070 type = ELF64_R_TYPE(rel->r_info);
2071 addend = rel->r_addend;
2074 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2075 rel->r_offset - start_offset, name, addend);
2079 " *(uint32_t *)(gen_code_ptr + %d) = "
2080 "((*(uint32_t *)(gen_code_ptr + %d)) "
2082 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2083 rel->r_offset - start_offset,
2084 rel->r_offset - start_offset,
2089 " *(uint32_t *)(gen_code_ptr + %d) = "
2090 "((*(uint32_t *)(gen_code_ptr + %d)) "
2092 " | ((%s + %d) & 0x3ff);\n",
2093 rel->r_offset - start_offset,
2094 rel->r_offset - start_offset,
2097 case R_SPARC_WDISP30:
2099 " *(uint32_t *)(gen_code_ptr + %d) = "
2100 "((*(uint32_t *)(gen_code_ptr + %d)) "
2102 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2103 " & 0x3fffffff);\n",
2104 rel->r_offset - start_offset,
2105 rel->r_offset - start_offset,
2107 rel->r_offset - start_offset);
2110 error("unsupported sparc64 relocation (%d)", type);
2115 #elif defined(HOST_ARM)
2121 arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
2124 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2125 if (rel->r_offset >= start_offset &&
2126 rel->r_offset < start_offset + copy_size) {
2127 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
2128 /* the compiler leave some unnecessary references to the code */
2129 if (sym_name[0] == '\0')
2131 if (strstart(sym_name, "__op_param", &p)) {
2132 snprintf(name, sizeof(name), "param%s", p);
2134 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
2136 type = ELF32_R_TYPE(rel->r_info);
2137 addend = get32((uint32_t *)(text + rel->r_offset));
2140 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2141 rel->r_offset - start_offset, name, addend);
2144 fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
2145 rel->r_offset - start_offset, addend, name);
2148 error("unsupported arm relocation (%d)", type);
2153 #elif defined(HOST_M68K)
2159 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2160 if (rel->r_offset >= start_offset &&
2161 rel->r_offset < start_offset + copy_size) {
2162 sym = &(symtab[ELFW(R_SYM)(rel->r_info)]);
2163 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
2164 if (strstart(sym_name, "__op_param", &p)) {
2165 snprintf(name, sizeof(name), "param%s", p);
2167 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
2169 type = ELF32_R_TYPE(rel->r_info);
2170 addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend;
2173 fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ;
2174 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
2175 rel->r_offset - start_offset, name, addend );
2178 fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset);
2179 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
2180 rel->r_offset - start_offset, name, rel->r_offset - start_offset, /*sym->st_value+*/ addend);
2183 error("unsupported m68k relocation (%d)", type);
2189 #error unsupported CPU
2191 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
2192 fprintf(outfile, "}\n");
2193 fprintf(outfile, "break;\n\n");
2195 fprintf(outfile, "static inline void gen_%s(", name);
2197 fprintf(outfile, "void");
2199 for(i = 0; i < nb_args; i++) {
2201 fprintf(outfile, ", ");
2202 fprintf(outfile, "long param%d", i + 1);
2205 fprintf(outfile, ")\n");
2206 fprintf(outfile, "{\n");
2207 for(i = 0; i < nb_args; i++) {
2208 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
2210 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
2211 fprintf(outfile, "}\n\n");
2215 int gen_file(FILE *outfile, int out_type)
2220 if (out_type == OUT_INDEX_OP) {
2221 fprintf(outfile, "DEF(end, 0, 0)\n");
2222 fprintf(outfile, "DEF(nop, 0, 0)\n");
2223 fprintf(outfile, "DEF(nop1, 1, 0)\n");
2224 fprintf(outfile, "DEF(nop2, 2, 0)\n");
2225 fprintf(outfile, "DEF(nop3, 3, 0)\n");
2226 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2228 name = get_sym_name(sym);
2229 if (strstart(name, OP_PREFIX, NULL)) {
2230 gen_code(name, sym->st_value, sym->st_size, outfile, 2);
2233 } else if (out_type == OUT_GEN_OP) {
2234 /* generate gen_xxx functions */
2236 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2238 name = get_sym_name(sym);
2239 if (strstart(name, OP_PREFIX, NULL)) {
2240 #if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
2241 if (sym->st_shndx != text_shndx)
2242 error("invalid section for opcode (0x%x)", sym->st_shndx);
2244 gen_code(name, sym->st_value, sym->st_size, outfile, 0);
2249 /* generate big code generation switch */
2251 "int dyngen_code(uint8_t *gen_code_buf,\n"
2252 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
2253 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
2255 " uint8_t *gen_code_ptr;\n"
2256 " const uint16_t *opc_ptr;\n"
2257 " const uint32_t *opparam_ptr;\n");
2261 " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
2262 " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
2263 " uint32_t *arm_data_ptr = arm_data_table;\n");
2268 " gen_code_ptr = gen_code_buf;\n"
2269 " opc_ptr = opc_buf;\n"
2270 " opparam_ptr = opparam_buf;\n");
2272 /* Generate prologue, if needed. */
2276 " switch(*opc_ptr++) {\n"
2279 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2281 name = get_sym_name(sym);
2282 if (strstart(name, OP_PREFIX, NULL)) {
2284 printf("%4d: %s pos=0x%08x len=%d\n",
2285 i, name, sym->st_value, sym->st_size);
2287 #if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
2288 if (sym->st_shndx != text_shndx)
2289 error("invalid section for opcode (0x%x)", sym->st_shndx);
2291 gen_code(name, sym->st_value, sym->st_size, outfile, 1);
2296 " case INDEX_op_nop:\n"
2298 " case INDEX_op_nop1:\n"
2301 " case INDEX_op_nop2:\n"
2302 " opparam_ptr += 2;\n"
2304 " case INDEX_op_nop3:\n"
2305 " opparam_ptr += 3;\n"
2312 /* generate constant table if needed */
2314 " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
2315 " gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
2316 " last_gen_code_ptr = gen_code_ptr;\n"
2317 " arm_ldr_ptr = arm_ldr_table;\n"
2318 " arm_data_ptr = arm_data_table;\n"
2328 /* generate some code patching */
2330 fprintf(outfile, "gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 0);\n");
2332 /* flush instruction cache */
2333 fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
2335 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
2336 fprintf(outfile, "}\n\n");
2345 printf("dyngen (c) 2003 Fabrice Bellard\n"
2346 "usage: dyngen [-o outfile] [-c] objfile\n"
2347 "Generate a dynamic code generator from an object file\n"
2348 "-c output enum of operations\n"
2349 "-g output gen_op_xx() functions\n"
2354 int main(int argc, char **argv)
2357 const char *filename, *outfilename;
2360 outfilename = "out.c";
2361 out_type = OUT_CODE;
2363 c = getopt(argc, argv, "ho:cg");
2371 outfilename = optarg;
2374 out_type = OUT_INDEX_OP;
2377 out_type = OUT_GEN_OP;
2383 filename = argv[optind];
2384 outfile = fopen(outfilename, "w");
2386 error("could not open '%s'", outfilename);
2388 load_object(filename);
2389 gen_file(outfile, out_type);