CRIS: Correct typos.
[qemu/qemu-JZ.git] / dyngen.c
blob68f23ad37663a6b2dd75652fcec9ca6919c68f8e
1 /*
2 * Generic Dynamic compiler generator
4 * Copyright (c) 2003 Fabrice Bellard
6 * The COFF object format support was extracted from Kazu's QEMU port
7 * to Win32.
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.
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <inttypes.h>
30 #include <unistd.h>
31 #include <fcntl.h>
33 #include "config-host.h"
35 /* NOTE: we test CONFIG_WIN32 instead of _WIN32 to enabled cross
36 compilation */
37 #if defined(CONFIG_WIN32)
38 #define CONFIG_FORMAT_COFF
39 #elif defined(CONFIG_DARWIN)
40 #define CONFIG_FORMAT_MACH
41 #else
42 #define CONFIG_FORMAT_ELF
43 #endif
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
49 platform) */
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
149 #else
150 #error unsupported CPU - please update the code
151 #endif
153 #include "elf.h"
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)
160 #else
161 typedef int64_t host_long;
162 typedef uint64_t host_ulong;
163 #define swabls(x) swab64s(x)
164 #define swablss(x) swab64ss(x)
165 #endif
167 #ifdef ELF_USES_RELOCA
168 #define SHT_RELOC SHT_RELA
169 #else
170 #define SHT_RELOC SHT_REL
171 #endif
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;
183 #include "a.out.h"
185 #define FILENAMELEN 256
187 typedef struct coff_sym {
188 struct external_syment *st_syment;
189 char st_name[FILENAMELEN];
190 uint32_t st_value;
191 int st_size;
192 uint8_t st_type;
193 uint8_t st_shndx;
194 } coff_Sym;
196 typedef struct coff_rel {
197 struct external_reloc *r_reloc;
198 int r_offset;
199 uint8_t r_type;
200 } coff_Rel;
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
220 union {
221 char *n_name;
222 long n_strx;
223 } n_un;
224 unsigned char n_type;
225 unsigned char n_sect;
226 short st_desc;
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 */
236 #include "bswap.h"
238 enum {
239 OUT_GEN_OP,
240 OUT_CODE,
241 OUT_INDEX_OP,
244 /* all dynamically generated functions begin with this code */
245 #define OP_PREFIX "op_"
247 int do_swap;
249 static void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
251 va_list ap;
252 va_start(ap, fmt);
253 fprintf(stderr, "dyngen: ");
254 vfprintf(stderr, fmt, ap);
255 fprintf(stderr, "\n");
256 va_end(ap);
257 exit(1);
260 static void *load_data(int fd, long offset, unsigned int size)
262 char *data;
264 data = malloc(size);
265 if (!data)
266 return NULL;
267 lseek(fd, offset, SEEK_SET);
268 if (read(fd, data, size) != size) {
269 free(data);
270 return NULL;
272 return data;
275 static int strstart(const char *str, const char *val, const char **ptr)
277 const char *p, *q;
278 p = str;
279 q = val;
280 while (*q != '\0') {
281 if (*p != *q)
282 return 0;
283 p++;
284 q++;
286 if (ptr)
287 *ptr = p;
288 return 1;
291 static void pstrcpy(char *buf, int buf_size, const char *str)
293 int c;
294 char *q = buf;
296 if (buf_size <= 0)
297 return;
299 for(;;) {
300 c = *str++;
301 if (c == 0 || q >= buf + buf_size - 1)
302 break;
303 *q++ = c;
305 *q = '\0';
308 static void swab16s(uint16_t *p)
310 *p = bswap16(*p);
313 static void swab32s(uint32_t *p)
315 *p = bswap32(*p);
318 static void swab32ss(int32_t *p)
320 *p = bswap32(*p);
323 static void swab64s(uint64_t *p)
325 *p = bswap64(*p);
328 static void swab64ss(int64_t *p)
330 *p = bswap64(*p);
333 static uint16_t get16(uint16_t *p)
335 uint16_t val;
336 val = *p;
337 if (do_swap)
338 val = bswap16(val);
339 return val;
342 static uint32_t get32(uint32_t *p)
344 uint32_t val;
345 val = *p;
346 if (do_swap)
347 val = bswap32(val);
348 return val;
351 static void put16(uint16_t *p, uint16_t val)
353 if (do_swap)
354 val = bswap16(val);
355 *p = val;
358 static void put32(uint32_t *p, uint32_t val)
360 if (do_swap)
361 val = bswap32(val);
362 *p = val;
365 /* executable information */
366 EXE_SYM *symtab;
367 int nb_syms;
368 int text_shndx;
369 uint8_t *text;
370 EXE_RELOC *relocs;
371 int nb_relocs;
373 #ifdef CONFIG_FORMAT_ELF
375 /* ELF file info */
376 struct elf_shdr *shdr;
377 uint8_t **sdata;
378 struct elfhdr ehdr;
379 char *strtab;
381 static int elf_must_swap(struct elfhdr *h)
383 union {
384 uint32_t i;
385 uint8_t b[4];
386 } swaptest;
388 swaptest.i = 1;
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);
442 #endif
445 static struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum,
446 const char *shstr, const char *name)
448 int i;
449 const char *shname;
450 struct elf_shdr *sec;
452 for(i = 0; i < shnum; i++) {
453 sec = &shdr[i];
454 if (!sec->sh_name)
455 continue;
456 shname = shstr + sec->sh_name;
457 if (!strcmp(shname, name))
458 return sec;
460 return NULL;
463 static int find_reloc(int sh_index)
465 struct elf_shdr *sec;
466 int i;
468 for(i = 0; i < ehdr.e_shnum; i++) {
469 sec = &shdr[i];
470 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
471 return i;
473 return 0;
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)
494 int fd;
495 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
496 int i, j;
497 ElfW(Sym) *sym;
498 char *shstr;
499 ELF_RELOC *rel;
501 fd = open(filename, O_RDONLY);
502 if (fd < 0)
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);
519 if (do_swap)
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));
532 if (do_swap) {
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++) {
543 sec = &shdr[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++) {
553 sec = &shdr[i];
554 if (sec->sh_type == SHT_RELOC) {
555 nb_relocs = sec->sh_size / sec->sh_entsize;
556 if (do_swap) {
557 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
558 elf_swap_rel(rel);
562 /* text section */
564 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
565 if (!text_sec)
566 error("could not find .text section");
567 text_shndx = text_sec - shdr;
568 text = sdata[text_shndx];
570 /* find text relocations, if any */
571 relocs = NULL;
572 nb_relocs = 0;
573 i = find_reloc(text_shndx);
574 if (i != 0) {
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");
580 if (!symtab_sec)
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));
588 if (do_swap) {
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);
596 close(fd);
597 return 0;
600 #endif /* CONFIG_FORMAT_ELF */
602 #ifdef CONFIG_FORMAT_COFF
604 /* COFF file info */
605 struct external_scnhdr *shdr;
606 uint8_t **sdata;
607 struct external_filehdr fhdr;
608 struct external_syment *coff_symtab;
609 char *strtab;
610 int coff_text_shndx, coff_data_shndx;
612 int data_shndx;
614 #define STRTAB_SIZE 4
616 #define DIR32 0x06
617 #define DISP32 0x14
619 #define T_FUNCTION 0x20
620 #define C_EXTERNAL 2
622 void sym_ent_name(struct external_syment *ext_sym, EXE_SYM *sym)
624 char *q;
625 int c, i, len;
627 if (ext_sym->e.e.e_zeroes != 0) {
628 q = sym->st_name;
629 for(i = 0; i < 8; i++) {
630 c = ext_sym->e.e_name[i];
631 if (c == '\0')
632 break;
633 *q++ = c;
635 *q = '\0';
636 } else {
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)
650 int i;
651 struct coff_sym *sym;
652 uint32_t text_data;
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) {
661 return sym->st_name;
665 return NULL;
668 static char *get_sym_name(EXE_SYM *sym)
670 return sym->st_name;
673 static char *get_rel_sym_name(EXE_RELOC *rel)
675 char *name;
676 name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx));
677 if (!strcmp(name, ".data"))
678 name = name_for_dotdata(rel);
679 if (name[0] == '.')
680 return NULL;
681 return name;
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)
691 int i;
692 const char *shname;
693 struct external_scnhdr *sec;
695 for(i = 0; i < shnum; i++) {
696 sec = &shdr[i];
697 if (!sec->s_name)
698 continue;
699 shname = sec->s_name;
700 if (!strcmp(shname, name))
701 return sec;
703 return NULL;
706 /* load a coff object file */
707 int load_object(const char *filename)
709 int fd;
710 struct external_scnhdr *sec, *text_sec, *data_sec;
711 int i;
712 struct external_syment *ext_sym;
713 struct external_reloc *coff_relocs;
714 struct external_reloc *ext_rel;
715 uint32_t *n_strtab;
716 EXE_SYM *sym;
717 EXE_RELOC *rel;
718 const char *p;
719 int aux_size, j;
721 fd = open(filename, O_RDONLY
722 #ifdef _WIN32
723 | O_BINARY
724 #endif
726 if (fd < 0)
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");
737 do_swap = 0;
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++) {
747 sec = &shdr[i];
748 if (!strstart(sec->s_name, ".bss", &p))
749 sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size);
753 /* text section */
754 text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text");
755 if (!text_sec)
756 error("could not find .text section");
757 coff_text_shndx = text_sec - shdr;
758 text = sdata[coff_text_shndx];
760 /* data section */
761 data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data");
762 if (!data_sec)
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++) {
768 for(i=0;i<8;i++)
769 printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]);
770 printf("\n");
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;
801 break;
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;
805 break;
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;
812 break;
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;
816 break;
819 } else {
820 sym->st_size = 0;
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;
842 return 0;
845 #endif /* CONFIG_FORMAT_COFF */
847 #ifdef CONFIG_FORMAT_MACH
849 /* File Header */
850 struct mach_header mach_hdr;
852 /* commands */
853 struct segment_command *segment = 0;
854 struct dysymtab_command *dysymtabcmd = 0;
855 struct symtab_command *symtabcmd = 0;
857 /* section */
858 struct section *section_hdr;
859 struct section *text_sec_hdr;
860 uint8_t **sdata;
862 /* relocs */
863 struct relocation_info *relocs;
865 /* symbols */
866 EXE_SYM *symtab;
867 struct nlist *symtab_std;
868 char *strtab;
870 /* indirect symbols */
871 uint32_t *tocdylib;
873 /* Utility functions */
875 static inline char *find_str_by_index(int index)
877 return strtab+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 */
886 return "debug";
888 if(!name)
889 return name;
890 if(name[0]=='_')
891 return name + 1;
892 else
893 return name;
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)
900 int i;
901 struct section *sec = section_hdr;
903 for(i = 0; i < shnum; i++, sec++) {
904 if (!sec->segname || !sec->sectname)
905 continue;
906 if (!strcmp(sec->sectname, sectname) && !strcmp(sec->segname, segname))
907 return i;
909 return -1;
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);
917 if(index == -1)
918 return NULL;
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;
932 } else {
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 )
942 int i, ret = -1;
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) )
950 ret = i;
953 if( ret < 0 ) {
954 *offset = 0;
955 return 0;
956 } else {
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;
971 /* Sanity check */
972 if(!( address >= sec_hdr->addr && address < (sec_hdr->addr + sec_hdr->size) ) )
973 return (char*)0;
975 if( sec_hdr->flags & S_SYMBOL_STUBS ){
976 size = sec_hdr->reserved2;
977 if(size == 0)
978 error("size = 0");
981 else if( sec_hdr->flags & S_LAZY_SYMBOL_POINTERS ||
982 sec_hdr->flags & S_NON_LAZY_SYMBOL_POINTERS)
983 size = sizeof(unsigned long);
984 else
985 return 0;
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]);
993 return name;
996 static const char * find_reloc_name_given_its_address(int address)
998 unsigned int i;
999 for(i = 0; i < segment->nsects ; i++)
1001 const char * name = find_reloc_name_in_sec_ptr(address, &section_hdr[i]);
1002 if((long)name != -1)
1003 return name;
1005 return 0;
1008 static const char * get_reloc_name(EXE_RELOC * rel, int * sslide)
1010 char * name = 0;
1011 struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
1012 int sectnum = rel->r_symbolnum;
1013 int sectoffset;
1014 int other_half=0;
1016 /* init the slide value */
1017 *sslide = 0;
1019 if(R_SCATTERED & rel->r_address)
1020 return (char *)find_reloc_name_given_its_address(sca_rel->r_value);
1022 if(rel->r_extern)
1024 /* ignore debug sym */
1025 if ( symtab[rel->r_symbolnum].n_type & N_STAB )
1026 return 0;
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)
1034 return 0;
1036 /* Sanity Check */
1037 if(sectnum > segment->nsects)
1038 error("sectnum > segment->nsects");
1040 switch(rel->r_type)
1042 case PPC_RELOC_LO16: fetch_next_pair_value(rel+1, &other_half); sectoffset |= (other_half << 16);
1043 break;
1044 case PPC_RELOC_HI16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset << 16) | (uint16_t)(other_half & 0xffff);
1045 break;
1046 case PPC_RELOC_HA16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset << 16) + (int16_t)(other_half & 0xffff);
1047 break;
1048 case PPC_RELOC_BR24:
1049 sectoffset = ( *(uint32_t *)(text + rel->r_address) & 0x03fffffc );
1050 if (sectoffset & 0x02000000) sectoffset |= 0xfc000000;
1051 break;
1052 default:
1053 error("switch(rel->type) not found");
1056 if(rel->r_pcrel)
1057 sectoffset += rel->r_address;
1059 if (rel->r_type == PPC_RELOC_BR24)
1060 name = (char *)find_reloc_name_in_sec_ptr((int)sectoffset, &section_hdr[sectnum-1]);
1062 /* search it in the full symbol list, if not found */
1063 if(!name)
1064 name = (char *)find_sym_with_value_and_sec_number(sectoffset, sectnum, sslide);
1066 return name;
1069 /* Used by dyngen common code */
1070 static const char * get_rel_sym_name(EXE_RELOC * rel)
1072 int sslide;
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;
1082 else
1083 return rel->r_address;
1086 /* load a mach-o object file */
1087 int load_object(const char *filename)
1089 int fd;
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;
1094 unsigned int i, j;
1095 EXE_SYM *sym;
1096 struct nlist *syment;
1098 fd = open(filename, O_RDONLY);
1099 if (fd < 0)
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");
1146 j+=lc.cmdsize;
1148 lseek(fd, j, SEEK_SET);
1151 if(!segment)
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);
1166 /* text section */
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");
1171 text = sdata[i];
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 */
1181 if(!(int)symtabcmd)
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;
1193 unsigned int j;
1194 memset(sym, 0, sizeof(*sym));
1196 if ( syment->n_type & N_STAB ) /* Debug symbols are skipped */
1197 continue;
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))
1204 continue;
1205 if(!sym_next) {
1206 sym_next = sym_follow;
1207 continue;
1209 if(!(sym_next->n_value > sym_follow->n_value))
1210 continue;
1211 sym_next = sym_follow;
1213 if(sym_next)
1214 sym->st_size = sym_next->n_value - sym->st_value;
1215 else
1216 sym->st_size = text_sec_hdr->size - sym->st_value;
1219 /* Find Reloc */
1220 relocs = load_data(fd, text_sec_hdr->reloff, text_sec_hdr->nreloc * sizeof(struct relocation_info));
1221 nb_relocs = text_sec_hdr->nreloc;
1223 close(fd);
1224 return 0;
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)
1232 const char *p;
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);
1238 return 1;
1239 } else {
1240 #if defined(HOST_SPARC) || defined(HOST_HPPA)
1241 if (sym_name[0] == '.')
1242 snprintf(name, name_size,
1243 "(long)(&__dot_%s)",
1244 sym_name + 1);
1245 else
1246 #endif
1247 snprintf(name, name_size, "(long)(&%s)", sym_name);
1249 return 0;
1252 #ifdef HOST_IA64
1254 #define PLT_ENTRY_SIZE 16 /* 1 bundle containing "brl" */
1256 struct plt_entry {
1257 struct plt_entry *next;
1258 const char *name;
1259 unsigned long addend;
1260 } *plt_list;
1262 static int
1263 get_plt_index (const char *name, unsigned long addend)
1265 struct plt_entry *plt, *prev= NULL;
1266 int index = 0;
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)
1271 return index;
1273 /* nope; create a new PLT entry: */
1275 plt = malloc(sizeof(*plt));
1276 if (!plt) {
1277 perror("malloc");
1278 exit(1);
1280 memset(plt, 0, sizeof(*plt));
1281 plt->name = strdup(name);
1282 plt->addend = addend;
1284 /* append to plt-list: */
1285 if (prev)
1286 prev->next = plt;
1287 else
1288 plt_list = plt;
1289 return index;
1292 #endif
1294 #define MAX_ARGS 3
1296 /* generate op code */
1297 static void gen_code(const char *name, host_ulong offset, host_ulong size,
1298 FILE *outfile, int gen_switch)
1300 int copy_size = 0;
1301 uint8_t *p_start, *p_end;
1302 host_ulong start_offset;
1303 int nb_args, i, n;
1304 uint8_t args_present[MAX_ARGS];
1305 const char *sym_name, *p;
1306 EXE_RELOC *rel;
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
1311 * bytes).
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
1319 uint8_t *p;
1320 p = p_end - 1;
1321 if (p == p_start)
1322 error("empty code for %s", name);
1323 while (*p != 0xc3) {
1324 p--;
1325 if (p <= p_start)
1326 error("ret or jmp expected at the end of %s", name);
1328 copy_size = p - p_start;
1330 #else
1332 int len;
1333 len = p_end - p_start;
1334 if (len == 0)
1335 error("empty code for %s", name);
1336 if (p_end[-1] == 0xc3) {
1337 len--;
1338 } else {
1339 error("ret or jmp expected at the end of %s", name);
1341 copy_size = len;
1343 #endif
1344 #elif defined(HOST_PPC)
1346 uint8_t *p;
1347 p = (void *)(p_end - 4);
1348 if (p == p_start)
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)
1356 uint8_t *p;
1357 p = (void *)(p_end - 2);
1358 if (p == p_start)
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)
1366 uint8_t *p;
1367 p = p_end - 4;
1368 #if 0
1369 /* XXX: check why it occurs */
1370 if (p == p_start)
1371 error("empty code for %s", name);
1372 #endif
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)
1379 uint8_t *p;
1380 p = (void *)(p_end - 4);
1381 if (p == p_start)
1382 error("empty code for %s", name);
1383 /* br.ret.sptk.many b0;; */
1384 /* 08 00 84 00 */
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;
1401 uint8_t *p;
1402 p = (void *)(p_end - 8);
1403 if (p <= p_start)
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) {
1410 p_start += 0x4;
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; */ ;
1418 else
1420 error("ret; restore; not found at end of %s", name);
1421 } else if (end_insn1 == INSN_RETL && end_insn2 == INSN_NOP) {
1423 } else {
1424 error("No save at the beginning of %s", name);
1426 #if 0
1427 /* Skip a preceeding nop, if present. */
1428 if (p > p_start) {
1429 skip_insn = get32((uint32_t *)(p - 0x4));
1430 if (skip_insn == INSN_NOP)
1431 p -= 4;
1433 #endif
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;
1448 uint8_t *p;
1449 p = (void *)(p_end - 8);
1450 #if 0
1451 /* XXX: check why it occurs */
1452 if (p <= p_start)
1453 error("empty code for %s", name);
1454 #endif
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) {
1460 p_start += 0x4;
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; */ ;
1468 else
1470 error("ret; restore; not found at end of %s", name);
1471 } else if (end_insn1 == INSN_RETL && end_insn2 == INSN_NOP) {
1473 } else {
1474 error("No save at the beginning of %s", name);
1477 #if 0
1478 /* Skip a preceeding nop, if present. */
1479 if (p > p_start) {
1480 skip_insn = get32((uint32_t *)(p - 0x4));
1481 if (skip_insn == 0x01000000)
1482 p -= 4;
1484 #endif
1486 copy_size = p - p_start;
1488 #elif defined(HOST_M68K)
1490 uint8_t *p;
1491 p = (void *)(p_end - 2);
1492 if (p == p_start)
1493 error("empty code for %s", name);
1494 // remove NOP's, probably added for alignment
1495 while ((get16((uint16_t *)p) == 0x4e71) &&
1496 (p>p_start))
1497 p -= 2;
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)
1504 uint8_t *p;
1505 p = p_start;
1506 while (p < p_end) {
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) */
1513 p += 4;
1514 else
1515 break;
1517 start_offset += p - p_start;
1518 p_start = p;
1519 p = p_end - 4;
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) */
1529 p -= 4;
1530 else
1531 break;
1533 p += 4;
1534 if (p <= p_start)
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
1544 uint8_t *p = p_end;
1546 if (p < (p_start + 0x8)) {
1547 error("empty code for %s", name);
1548 } else {
1549 uint32_t end_insn1, end_insn2;
1551 p -= 0x8;
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");
1563 #else
1564 #error unsupported CPU
1565 #endif
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);
1576 if(!sym_name)
1577 continue;
1578 if (strstart(sym_name, "__op_param", &p) ||
1579 strstart(sym_name, "__op_gen_label", &p)) {
1580 n = strtoul(p, NULL, 10);
1581 if (n > MAX_ARGS)
1582 error("too many arguments in %s", name);
1583 args_present[n - 1] = 1;
1588 nb_args = 0;
1589 while (nb_args < MAX_ARGS && args_present[nb_args])
1590 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;
1600 int has_stubs = 0;
1601 char relname[256];
1602 int type, is_label;
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) {
1613 has_stubs = 1;
1614 op_size += 8; /* ldil and be,n instructions */
1619 if (has_stubs)
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);
1623 #else
1624 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
1625 #endif
1627 } else if (gen_switch == 1) {
1629 /* output C code */
1630 fprintf(outfile, "case INDEX_%s: {\n", name);
1631 if (nb_args > 0) {
1632 fprintf(outfile, " long ");
1633 for(i = 0; i < nb_args; i++) {
1634 if (i != 0)
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);
1642 #else
1643 fprintf(outfile, " extern void %s();\n", name);
1644 #endif
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);
1651 if(!sym_name)
1652 continue;
1653 if (*sym_name &&
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] == '.') {
1659 fprintf(outfile,
1660 "extern char __dot_%s __asm__(\"%s\");\n",
1661 sym_name+1, sym_name);
1662 continue;
1664 #endif
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",
1677 sym_name);
1678 #else
1679 fprintf(outfile, "extern char %s;\n", sym_name);
1680 #endif
1685 #ifdef __hppa__
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);
1688 #else
1689 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n",
1690 name, (int)(start_offset - offset), copy_size);
1691 #endif
1693 /* emit code offset information */
1695 EXE_SYM *sym;
1696 const char *sym_name, *p;
1697 host_ulong val;
1698 int n;
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)) {
1703 uint8_t *ptr;
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];
1713 } else {
1714 ptr = NULL;
1716 #elif defined(CONFIG_FORMAT_MACH)
1717 if(!sym->n_sect)
1718 continue;
1719 ptr = sdata[sym->n_sect-1];
1720 #else
1721 ptr = sdata[sym->st_shndx];
1722 #endif
1723 if (!ptr)
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;
1728 #endif
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);
1736 if (reloc_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;
1743 break;
1745 rel++;
1749 #endif
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)
1766 char relname[256];
1767 int type, is_label;
1768 int addend;
1769 int reloc_offset;
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);
1774 if (!sym_name)
1775 continue;
1776 reloc_offset = rel->r_offset - start_offset;
1777 if (strstart(sym_name, "__op_jmp", &p)) {
1778 int n;
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",
1785 n, reloc_offset);
1786 continue;
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);
1793 if (is_label) {
1794 switch(type) {
1795 case R_386_32:
1796 case R_386_PC32:
1797 fprintf(outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1798 reloc_offset, type, relname, addend);
1799 break;
1800 default:
1801 error("unsupported i386 relocation (%d)", type);
1803 } else {
1804 switch(type) {
1805 case R_386_32:
1806 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1807 reloc_offset, relname, addend);
1808 break;
1809 case R_386_PC32:
1810 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1811 reloc_offset, relname, reloc_offset, addend);
1812 break;
1813 default:
1814 error("unsupported i386 relocation (%d)", type);
1817 #elif defined(CONFIG_FORMAT_COFF)
1819 char *temp_name;
1820 int j;
1821 EXE_SYM *sym;
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;
1831 type = rel->r_type;
1832 if (is_label) {
1833 /* TCG uses elf relocation constants */
1834 #define R_386_32 1
1835 #define R_386_PC32 2
1836 switch(type) {
1837 case DIR32:
1838 type = R_386_32;
1839 goto do_reloc;
1840 case DISP32:
1841 type = R_386_PC32;
1842 addend -= 4;
1843 do_reloc:
1844 fprintf(outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1845 reloc_offset, type, relname, addend);
1846 break;
1847 default:
1848 error("unsupported i386 relocation (%d)", type);
1850 } else {
1851 switch(type) {
1852 case DIR32:
1853 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1854 reloc_offset, relname, addend);
1855 break;
1856 case DISP32:
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);
1859 break;
1860 default:
1861 error("unsupported i386 relocation (%d)", type);
1864 #else
1865 #error unsupport object format
1866 #endif
1870 #elif defined(HOST_X86_64)
1872 char relname[256];
1873 int type, is_label;
1874 int addend;
1875 int reloc_offset;
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;
1884 if (is_label) {
1885 switch(type) {
1886 case R_X86_64_32:
1887 case R_X86_64_32S:
1888 case R_X86_64_PC32:
1889 fprintf(outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1890 reloc_offset, type, relname, addend);
1891 break;
1892 default:
1893 error("unsupported X86_64 relocation (%d)", type);
1895 } else {
1896 switch(type) {
1897 case R_X86_64_32:
1898 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
1899 reloc_offset, relname, addend);
1900 break;
1901 case R_X86_64_32S:
1902 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
1903 reloc_offset, relname, addend);
1904 break;
1905 case R_X86_64_PC32:
1906 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1907 reloc_offset, relname, reloc_offset, addend);
1908 break;
1909 default:
1910 error("unsupported X86_64 relocation (%d)", type);
1916 #elif defined(HOST_PPC)
1918 #ifdef CONFIG_FORMAT_ELF
1919 char relname[256];
1920 int type;
1921 int addend;
1922 int is_label;
1923 int reloc_offset;
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)) {
1930 int n;
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",
1937 n, reloc_offset);
1938 continue;
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;
1945 if (is_label) {
1946 switch (type) {
1947 case R_PPC_REL24:
1948 fprintf (outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
1949 reloc_offset, type, relname, addend);
1950 break;
1951 default:
1952 error ("unsupported ppc relocation (%d)", type);
1955 else {
1956 switch(type) {
1957 case R_PPC_ADDR32:
1958 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1959 reloc_offset, relname, addend);
1960 break;
1961 case R_PPC_ADDR16_LO:
1962 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
1963 reloc_offset, relname, addend);
1964 break;
1965 case R_PPC_ADDR16_HI:
1966 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
1967 reloc_offset, relname, addend);
1968 break;
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);
1972 break;
1973 case R_PPC_REL24:
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\");"
1979 " abort();\n"
1980 " }\n"
1981 "}\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);
1985 break;
1986 default:
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;
1997 const char *p;
1998 int slide, sslide;
1999 int i;
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;
2013 } else {
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;
2019 type = rel->r_type;
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)) {
2033 int n;
2034 n = strtol(p, NULL, 10);
2035 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
2036 n, slide);
2037 continue; /* Nothing more to do */
2040 if(!sym_name) {
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),
2047 sym_name);
2048 switch(type) {
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");
2056 } else {
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);
2060 break;
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);
2064 break;
2065 case PPC_RELOC_LO16:
2066 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d);\n",
2067 slide, final_sym_name, sslide);
2068 break;
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);
2072 break;
2073 default:
2074 error("unsupported powerpc relocation (%d)", type);
2077 #else
2078 #error unsupport object format
2079 #endif
2081 #elif defined(HOST_S390)
2083 char relname[256];
2084 int type;
2085 int addend;
2086 int reloc_offset;
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;
2095 switch(type) {
2096 case R_390_32:
2097 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2098 reloc_offset, relname, addend);
2099 break;
2100 case R_390_16:
2101 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
2102 reloc_offset, relname, addend);
2103 break;
2104 case R_390_8:
2105 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
2106 reloc_offset, relname, addend);
2107 break;
2108 case R_390_PC32DBL:
2109 if (ELF32_ST_TYPE(symtab[ELFW(R_SYM)(rel->r_info)].st_info) == STT_SECTION) {
2110 fprintf(outfile,
2111 " *(uint32_t *)(gen_code_ptr + %d) += "
2112 "((long)&%s - (long)gen_code_ptr) >> 1;\n",
2113 reloc_offset, name);
2115 else
2116 fprintf(outfile,
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);
2120 break;
2121 default:
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) {
2131 int type;
2132 long reloc_offset;
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;
2137 switch (type) {
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",
2142 reloc_offset);
2143 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
2144 reloc_offset + (int)rel->r_addend);
2145 break;
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. */
2149 break;
2150 case R_ALPHA_HINT:
2151 /* Branch target prediction hint. Ignore for now. Should be already
2152 correct for in-function jumps. */
2153 break;
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. */
2157 break;
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",
2164 reloc_offset, p);
2165 break;
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",
2169 reloc_offset, p);
2170 break;
2171 case R_ALPHA_BRSGP:
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);
2176 break;
2177 default:
2178 error("unsupported Alpha relocation (%d)", type);
2183 #elif defined(HOST_IA64)
2185 unsigned long sym_idx;
2186 long code_offset;
2187 char relname[256];
2188 int type;
2189 long addend;
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)
2195 continue;
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)) {
2199 int n;
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",
2207 n, code_offset);
2208 continue;
2210 get_reloc_expr(relname, sizeof(relname), sym_name);
2211 type = ELF64_R_TYPE(rel->r_info);
2212 addend = rel->r_addend;
2213 switch(type) {
2214 case R_IA64_IMM64:
2215 fprintf(outfile,
2216 " ia64_imm64(gen_code_ptr + %ld, "
2217 "%s + %ld);\n",
2218 code_offset, relname, addend);
2219 break;
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));
2226 break;
2227 case R_IA64_LDXMOV:
2228 fprintf(outfile,
2229 " ia64_ldxmov(gen_code_ptr + %ld,"
2230 " %s + %ld);\n", code_offset, relname, addend);
2231 break;
2233 case R_IA64_PCREL21B:
2234 if (strstart(sym_name, "__op_gen_label", NULL)) {
2235 fprintf(outfile,
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);
2241 } else {
2242 fprintf(outfile,
2243 " IA64_PLT(gen_code_ptr + %ld, "
2244 "%d);\t/* %s + %ld */\n",
2245 code_offset,
2246 get_plt_index(sym_name, addend),
2247 sym_name, addend);
2249 break;
2250 default:
2251 error("unsupported ia64 relocation (0x%x)",
2252 type);
2255 fprintf(outfile, " ia64_nop_b(gen_code_ptr + %d);\n",
2256 copy_size - 16 + 2);
2258 #elif defined(HOST_SPARC)
2260 char relname[256];
2261 int type;
2262 int addend;
2263 int reloc_offset;
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;
2272 switch(type) {
2273 case R_SPARC_32:
2274 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2275 reloc_offset, relname, addend);
2276 break;
2277 case R_SPARC_HI22:
2278 fprintf(outfile,
2279 " *(uint32_t *)(gen_code_ptr + %d) = "
2280 "((*(uint32_t *)(gen_code_ptr + %d)) "
2281 " & ~0x3fffff) "
2282 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2283 reloc_offset, reloc_offset, relname, addend);
2284 break;
2285 case R_SPARC_LO10:
2286 fprintf(outfile,
2287 " *(uint32_t *)(gen_code_ptr + %d) = "
2288 "((*(uint32_t *)(gen_code_ptr + %d)) "
2289 " & ~0x3ff) "
2290 " | ((%s + %d) & 0x3ff);\n",
2291 reloc_offset, reloc_offset, relname, addend);
2292 break;
2293 case R_SPARC_WDISP30:
2294 fprintf(outfile,
2295 " *(uint32_t *)(gen_code_ptr + %d) = "
2296 "((*(uint32_t *)(gen_code_ptr + %d)) "
2297 " & ~0x3fffffff) "
2298 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2299 " & 0x3fffffff);\n",
2300 reloc_offset, reloc_offset, relname, addend,
2301 reloc_offset);
2302 break;
2303 case R_SPARC_WDISP22:
2304 fprintf(outfile,
2305 " *(uint32_t *)(gen_code_ptr + %d) = "
2306 "((*(uint32_t *)(gen_code_ptr + %d)) "
2307 " & ~0x3fffff) "
2308 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2309 " & 0x3fffff);\n",
2310 rel->r_offset - start_offset,
2311 rel->r_offset - start_offset,
2312 relname, addend,
2313 rel->r_offset - start_offset);
2314 break;
2315 default:
2316 error("unsupported sparc relocation (%d)", type);
2321 #elif defined(HOST_SPARC64)
2323 char relname[256];
2324 int type;
2325 int addend;
2326 int reloc_offset;
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;
2335 switch(type) {
2336 case R_SPARC_32:
2337 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2338 reloc_offset, relname, addend);
2339 break;
2340 case R_SPARC_HI22:
2341 fprintf(outfile,
2342 " *(uint32_t *)(gen_code_ptr + %d) = "
2343 "((*(uint32_t *)(gen_code_ptr + %d)) "
2344 " & ~0x3fffff) "
2345 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2346 reloc_offset, reloc_offset, relname, addend);
2347 break;
2348 case R_SPARC_LO10:
2349 fprintf(outfile,
2350 " *(uint32_t *)(gen_code_ptr + %d) = "
2351 "((*(uint32_t *)(gen_code_ptr + %d)) "
2352 " & ~0x3ff) "
2353 " | ((%s + %d) & 0x3ff);\n",
2354 reloc_offset, reloc_offset, relname, addend);
2355 break;
2356 case R_SPARC_OLO10:
2357 addend += ELF64_R_TYPE_DATA (rel->r_info);
2358 fprintf(outfile,
2359 " *(uint32_t *)(gen_code_ptr + %d) = "
2360 "((*(uint32_t *)(gen_code_ptr + %d)) "
2361 " & ~0x3ff) "
2362 " | ((%s + %d) & 0x3ff);\n",
2363 reloc_offset, reloc_offset, relname, addend);
2364 break;
2365 case R_SPARC_WDISP30:
2366 fprintf(outfile,
2367 " *(uint32_t *)(gen_code_ptr + %d) = "
2368 "((*(uint32_t *)(gen_code_ptr + %d)) "
2369 " & ~0x3fffffff) "
2370 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2371 " & 0x3fffffff);\n",
2372 reloc_offset, reloc_offset, relname, addend,
2373 reloc_offset);
2374 break;
2375 case R_SPARC_WDISP22:
2376 fprintf(outfile,
2377 " *(uint32_t *)(gen_code_ptr + %d) = "
2378 "((*(uint32_t *)(gen_code_ptr + %d)) "
2379 " & ~0x3fffff) "
2380 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2381 " & 0x3fffff);\n",
2382 reloc_offset, reloc_offset, relname, addend,
2383 reloc_offset);
2384 break;
2385 case R_SPARC_HH22:
2386 fprintf(outfile,
2387 " *(uint32_t *)(gen_code_ptr + %d) = "
2388 "((*(uint32_t *)(gen_code_ptr + %d)) "
2389 " & ~0x00000000) "
2390 " | (((%s + %d) >> 42) & 0x00000000);\n",
2391 reloc_offset, reloc_offset, relname, addend);
2392 break;
2394 case R_SPARC_LM22:
2395 fprintf(outfile,
2396 " *(uint32_t *)(gen_code_ptr + %d) = "
2397 "((*(uint32_t *)(gen_code_ptr + %d)) "
2398 " & ~0x00000000) "
2399 " | (((%s + %d) >> 10) & 0x00000000);\n",
2400 reloc_offset, reloc_offset, relname, addend);
2401 break;
2403 case R_SPARC_HM10:
2404 fprintf(outfile,
2405 " *(uint32_t *)(gen_code_ptr + %d) = "
2406 "((*(uint32_t *)(gen_code_ptr + %d)) "
2407 " & ~0x00000000) "
2408 " | ((((%s + %d) >> 32 & 0x3ff)) & 0x00000000);\n",
2409 reloc_offset, reloc_offset, relname, addend);
2410 break;
2412 default:
2413 error("unsupported sparc64 relocation (%d) for symbol %s", type, relname);
2418 #elif defined(HOST_M68K)
2420 char relname[256];
2421 int type;
2422 int addend;
2423 int reloc_offset;
2424 Elf32_Sym *sym;
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;
2434 switch(type) {
2435 case R_68K_32:
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 );
2439 break;
2440 case R_68K_PC32:
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);
2444 break;
2445 default:
2446 error("unsupported m68k relocation (%d)", type);
2451 #elif defined(HOST_HPPA)
2453 char relname[256];
2454 int type, is_label;
2455 int addend;
2456 int reloc_offset;
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;
2467 if (is_label) {
2468 switch (type) {
2469 case R_PARISC_PCREL17F:
2470 fprintf(outfile,
2471 " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n",
2472 reloc_offset, type, relname, addend);
2473 break;
2474 default:
2475 error("unsupported hppa label relocation (%d)", type);
2477 } else {
2478 switch (type) {
2479 case R_PARISC_DIR21L:
2480 fprintf(outfile,
2481 " hppa_patch21l((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2482 reloc_offset, relname, addend);
2483 break;
2484 case R_PARISC_DIR14R:
2485 fprintf(outfile,
2486 " hppa_patch14r((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2487 reloc_offset, relname, addend);
2488 break;
2489 case R_PARISC_PCREL17F:
2490 if (strstart(sym_name, "__op_gen_label", NULL)) {
2491 fprintf(outfile,
2492 " hppa_patch17f((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2493 reloc_offset, relname, addend);
2494 } else {
2495 fprintf(outfile,
2496 " HPPA_RECORD_BRANCH(hppa_stubs, (uint32_t *)(gen_code_ptr + %d), %s);\n",
2497 reloc_offset, relname);
2499 break;
2500 case R_PARISC_DPREL21L:
2501 if (strstart(sym_name, "__op_param", &p))
2502 fprintf(outfile,
2503 " hppa_load_imm21l((uint32_t *)(gen_code_ptr + %d), param%s, %d);\n",
2504 reloc_offset, p, addend);
2505 else
2506 fprintf(outfile,
2507 " hppa_patch21l_dprel((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2508 reloc_offset, relname, addend);
2509 break;
2510 case R_PARISC_DPREL14R:
2511 if (strstart(sym_name, "__op_param", &p))
2512 fprintf(outfile,
2513 " hppa_load_imm14r((uint32_t *)(gen_code_ptr + %d), param%s, %d);\n",
2514 reloc_offset, p, addend);
2515 else
2516 fprintf(outfile,
2517 " hppa_patch14r_dprel((uint32_t *)(gen_code_ptr + %d), %s, %d);\n",
2518 reloc_offset, relname, addend);
2519 break;
2520 default:
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) {
2531 char relname[256];
2532 int type;
2533 int addend;
2534 int reloc_offset;
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')
2539 continue;
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;
2544 switch (type) {
2545 case R_MIPS_26:
2546 fprintf(outfile, " /* R_MIPS_26 RELOC, offset 0x%x, name %s */\n",
2547 rel->r_offset, sym_name);
2548 fprintf(outfile,
2549 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2550 "(0x%x & ~0x3fffff) "
2551 "| ((0x%x + ((%s - (*(uint32_t *)(gen_code_ptr + 0x%x))) >> 2)) "
2552 " & 0x3fffff);\n",
2553 reloc_offset, addend, addend, relname, reloc_offset);
2554 break;
2555 case R_MIPS_HI16:
2556 fprintf(outfile, " /* R_MIPS_HI16 RELOC, offset 0x%x, name %s */\n",
2557 rel->r_offset, sym_name);
2558 fprintf(outfile,
2559 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2560 "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
2561 " & ~0xffff) "
2562 " | (((%s - 0x8000) >> 16) & 0xffff);\n",
2563 reloc_offset, reloc_offset, relname);
2564 break;
2565 case R_MIPS_LO16:
2566 fprintf(outfile, " /* R_MIPS_LO16 RELOC, offset 0x%x, name %s */\n",
2567 rel->r_offset, sym_name);
2568 fprintf(outfile,
2569 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2570 "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
2571 " & ~0xffff) "
2572 " | (%s & 0xffff);\n",
2573 reloc_offset, reloc_offset, relname);
2574 break;
2575 case R_MIPS_PC16:
2576 fprintf(outfile, " /* R_MIPS_PC16 RELOC, offset 0x%x, name %s */\n",
2577 rel->r_offset, sym_name);
2578 fprintf(outfile,
2579 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2580 "(0x%x & ~0xffff) "
2581 "| ((0x%x + ((%s - (*(uint32_t *)(gen_code_ptr + 0x%x))) >> 2)) "
2582 " & 0xffff);\n",
2583 reloc_offset, addend, addend, relname, reloc_offset);
2584 break;
2585 case R_MIPS_GOT16:
2586 case R_MIPS_CALL16:
2587 fprintf(outfile, " /* R_MIPS_GOT16 RELOC, offset 0x%x, name %s */\n",
2588 rel->r_offset, sym_name);
2589 fprintf(outfile,
2590 " *(uint32_t *)(gen_code_ptr + 0x%x) = "
2591 "((*(uint32_t *)(gen_code_ptr + 0x%x)) "
2592 " & ~0xffff) "
2593 " | (((%s - 0x8000) >> 16) & 0xffff);\n",
2594 reloc_offset, reloc_offset, relname);
2595 break;
2596 default:
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");
2606 #else
2607 #error unsupported CPU
2608 #endif
2609 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
2610 fprintf(outfile, "}\n");
2611 fprintf(outfile, "break;\n\n");
2612 } else {
2613 fprintf(outfile, "static inline void gen_%s(", name);
2614 if (nb_args == 0) {
2615 fprintf(outfile, "void");
2616 } else {
2617 for(i = 0; i < nb_args; i++) {
2618 if (i != 0)
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)
2635 int i;
2636 EXE_SYM *sym;
2638 if (out_type == OUT_INDEX_OP) {
2639 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2640 const char *name;
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++) {
2649 const char *name;
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);
2655 #endif
2656 gen_code(name, sym->st_value, sym->st_size, outfile, 0);
2660 } else {
2661 /* generate big code generation switch */
2663 #ifdef HOST_ARM
2664 error("dyngen targets not supported on ARM");
2665 #endif
2666 #ifdef HOST_IA64
2667 #error broken
2669 long addend, not_first = 0;
2670 unsigned long sym_idx;
2671 int index, max_index;
2672 const char *sym_name;
2673 EXE_RELOC *rel;
2675 max_index = -1;
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))
2680 continue;
2681 if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B)
2682 continue;
2684 addend = rel->r_addend;
2685 index = get_plt_index(sym_name, addend);
2686 if (index <= max_index)
2687 continue;
2688 max_index = index;
2689 fprintf(outfile, " extern void %s(void);\n", sym_name);
2692 fprintf(outfile,
2693 " struct ia64_fixup *plt_fixes = NULL, "
2694 "*ltoff_fixes = NULL;\n"
2695 " static long plt_target[] = {\n\t");
2697 max_index = -1;
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))
2702 continue;
2703 if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B)
2704 continue;
2706 addend = rel->r_addend;
2707 index = get_plt_index(sym_name, addend);
2708 if (index <= max_index)
2709 continue;
2710 max_index = index;
2712 if (not_first)
2713 fprintf(outfile, ",\n\t");
2714 not_first = 1;
2715 if (addend)
2716 fprintf(outfile, "(long) &%s + %ld", sym_name, addend);
2717 else
2718 fprintf(outfile, "(long) &%s", sym_name);
2720 fprintf(outfile, "\n };\n"
2721 " unsigned int plt_offset[%u] = { 0 };\n", max_index + 1);
2723 #endif
2725 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2726 const char *name;
2727 name = get_sym_name(sym);
2728 if (strstart(name, OP_PREFIX, NULL)) {
2729 #if 0
2730 printf("%4d: %s pos=0x%08x len=%d\n",
2731 i, name, sym->st_value, sym->st_size);
2732 #endif
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);
2736 #endif
2737 gen_code(name, sym->st_value, sym->st_size, outfile, 1);
2742 return 0;
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"
2753 exit(1);
2756 int main(int argc, char **argv)
2758 int c, out_type;
2759 const char *filename, *outfilename;
2760 FILE *outfile;
2762 outfilename = "out.c";
2763 out_type = OUT_CODE;
2764 for(;;) {
2765 c = getopt(argc, argv, "ho:cg");
2766 if (c == -1)
2767 break;
2768 switch(c) {
2769 case 'h':
2770 usage();
2771 break;
2772 case 'o':
2773 outfilename = optarg;
2774 break;
2775 case 'c':
2776 out_type = OUT_INDEX_OP;
2777 break;
2778 case 'g':
2779 out_type = OUT_GEN_OP;
2780 break;
2783 if (optind >= argc)
2784 usage();
2785 filename = argv[optind];
2786 outfile = fopen(outfilename, "w");
2787 if (!outfile)
2788 error("could not open '%s'", outfilename);
2790 load_object(filename);
2791 gen_file(outfile, out_type);
2792 fclose(outfile);
2793 return 0;