factorized more definitions - suppressed broken sound ioctls
[qemu] / dyngen.c
1 /*
2  *  Generic Dynamic compiler generator
3  * 
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27
28 #include "config.h"
29
30 /* elf format definitions. We use these macros to test the CPU to
31    allow cross compilation (this tool must be ran on the build
32    platform) */
33 #if defined(HOST_I386)
34
35 #define ELF_CLASS       ELFCLASS32
36 #define ELF_ARCH        EM_386
37 #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
38 #undef ELF_USES_RELOCA
39
40 #elif defined(HOST_PPC)
41
42 #define ELF_CLASS       ELFCLASS32
43 #define ELF_ARCH        EM_PPC
44 #define elf_check_arch(x) ((x) == EM_PPC)
45 #define ELF_USES_RELOCA
46
47 #elif defined(HOST_S390)
48
49 #define ELF_CLASS       ELFCLASS32
50 #define ELF_ARCH        EM_S390
51 #define elf_check_arch(x) ((x) == EM_S390)
52 #define ELF_USES_RELOCA
53
54 #elif defined(HOST_ALPHA)
55
56 #define ELF_CLASS       ELFCLASS64
57 #define ELF_ARCH        EM_ALPHA
58 #define elf_check_arch(x) ((x) == EM_ALPHA)
59 #define ELF_USES_RELOCA
60
61 #elif defined(HOST_IA64)
62
63 #define ELF_CLASS       ELFCLASS64
64 #define ELF_ARCH        EM_IA_64
65 #define elf_check_arch(x) ((x) == EM_IA_64)
66 #define ELF_USES_RELOCA
67
68 #elif defined(HOST_SPARC)
69
70 #define ELF_CLASS       ELFCLASS32
71 #define ELF_ARCH        EM_SPARC
72 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
73 #define ELF_USES_RELOCA
74
75 #elif defined(HOST_SPARC64)
76
77 #define ELF_CLASS       ELFCLASS64
78 #define ELF_ARCH        EM_SPARCV9
79 #define elf_check_arch(x) ((x) == EM_SPARCV9)
80 #define ELF_USES_RELOCA
81
82 #elif defined(HOST_ARM)
83
84 #define ELF_CLASS       ELFCLASS32
85 #define ELF_ARCH        EM_ARM
86 #define elf_check_arch(x) ((x) == EM_ARM)
87 #define ELF_USES_RELOC
88
89 #else
90 #error unsupported CPU - please update the code
91 #endif
92
93 #include "elf.h"
94
95 #if ELF_CLASS == ELFCLASS32
96 typedef int32_t host_long;
97 typedef uint32_t host_ulong;
98 #define swabls(x) swab32s(x)
99 #else
100 typedef int64_t host_long;
101 typedef uint64_t host_ulong;
102 #define swabls(x) swab64s(x)
103 #endif
104
105 #ifdef ELF_USES_RELOCA
106 #define SHT_RELOC SHT_RELA
107 #else
108 #define SHT_RELOC SHT_REL
109 #endif
110
111 #include "thunk.h"
112
113 enum {
114     OUT_GEN_OP,
115     OUT_CODE,
116     OUT_INDEX_OP,
117 };
118
119 /* all dynamically generated functions begin with this code */
120 #define OP_PREFIX "op_"
121
122 int elf_must_swap(struct elfhdr *h)
123 {
124   union {
125       uint32_t i;
126       uint8_t b[4];
127   } swaptest;
128
129   swaptest.i = 1;
130   return (h->e_ident[EI_DATA] == ELFDATA2MSB) != 
131       (swaptest.b[0] == 0);
132 }
133   
134 void swab16s(uint16_t *p)
135 {
136     *p = bswap16(*p);
137 }
138
139 void swab32s(uint32_t *p)
140 {
141     *p = bswap32(*p);
142 }
143
144 void swab64s(uint64_t *p)
145 {
146     *p = bswap64(*p);
147 }
148
149 void elf_swap_ehdr(struct elfhdr *h)
150 {
151     swab16s(&h->e_type);                        /* Object file type */
152     swab16s(&h->        e_machine);             /* Architecture */
153     swab32s(&h->        e_version);             /* Object file version */
154     swabls(&h-> e_entry);               /* Entry point virtual address */
155     swabls(&h-> e_phoff);               /* Program header table file offset */
156     swabls(&h-> e_shoff);               /* Section header table file offset */
157     swab32s(&h->        e_flags);               /* Processor-specific flags */
158     swab16s(&h->        e_ehsize);              /* ELF header size in bytes */
159     swab16s(&h->        e_phentsize);           /* Program header table entry size */
160     swab16s(&h->        e_phnum);               /* Program header table entry count */
161     swab16s(&h->        e_shentsize);           /* Section header table entry size */
162     swab16s(&h->        e_shnum);               /* Section header table entry count */
163     swab16s(&h->        e_shstrndx);            /* Section header string table index */
164 }
165
166 void elf_swap_shdr(struct elf_shdr *h)
167 {
168   swab32s(&h->  sh_name);               /* Section name (string tbl index) */
169   swab32s(&h->  sh_type);               /* Section type */
170   swabls(&h->   sh_flags);              /* Section flags */
171   swabls(&h->   sh_addr);               /* Section virtual addr at execution */
172   swabls(&h->   sh_offset);             /* Section file offset */
173   swabls(&h->   sh_size);               /* Section size in bytes */
174   swab32s(&h->  sh_link);               /* Link to another section */
175   swab32s(&h->  sh_info);               /* Additional section information */
176   swabls(&h->   sh_addralign);          /* Section alignment */
177   swabls(&h->   sh_entsize);            /* Entry size if section holds table */
178 }
179
180 void elf_swap_phdr(struct elf_phdr *h)
181 {
182     swab32s(&h->p_type);                        /* Segment type */
183     swabls(&h->p_offset);               /* Segment file offset */
184     swabls(&h->p_vaddr);                /* Segment virtual address */
185     swabls(&h->p_paddr);                /* Segment physical address */
186     swabls(&h->p_filesz);               /* Segment size in file */
187     swabls(&h->p_memsz);                /* Segment size in memory */
188     swab32s(&h->p_flags);               /* Segment flags */
189     swabls(&h->p_align);                /* Segment alignment */
190 }
191
192 void elf_swap_rel(ELF_RELOC *rel)
193 {
194     swabls(&rel->r_offset);
195     swabls(&rel->r_info);
196 #ifdef ELF_USES_RELOCA
197     swabls(&rel->r_addend);
198 #endif
199 }
200
201 /* ELF file info */
202 int do_swap;
203 struct elf_shdr *shdr;
204 uint8_t **sdata;
205 struct elfhdr ehdr;
206 ElfW(Sym) *symtab;
207 int nb_syms;
208 char *strtab;
209 int text_shndx;
210
211 uint16_t get16(uint16_t *p)
212 {
213     uint16_t val;
214     val = *p;
215     if (do_swap)
216         val = bswap16(val);
217     return val;
218 }
219
220 uint32_t get32(uint32_t *p)
221 {
222     uint32_t val;
223     val = *p;
224     if (do_swap)
225         val = bswap32(val);
226     return val;
227 }
228
229 void put16(uint16_t *p, uint16_t val)
230 {
231     if (do_swap)
232         val = bswap16(val);
233     *p = val;
234 }
235
236 void put32(uint32_t *p, uint32_t val)
237 {
238     if (do_swap)
239         val = bswap32(val);
240     *p = val;
241 }
242
243 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
244 {
245     va_list ap;
246     va_start(ap, fmt);
247     fprintf(stderr, "dyngen: ");
248     vfprintf(stderr, fmt, ap);
249     fprintf(stderr, "\n");
250     va_end(ap);
251     exit(1);
252 }
253
254
255 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr, 
256                                   const char *name)
257 {
258     int i;
259     const char *shname;
260     struct elf_shdr *sec;
261
262     for(i = 0; i < shnum; i++) {
263         sec = &shdr[i];
264         if (!sec->sh_name)
265             continue;
266         shname = shstr + sec->sh_name;
267         if (!strcmp(shname, name))
268             return sec;
269     }
270     return NULL;
271 }
272
273 int find_reloc(int sh_index)
274 {
275     struct elf_shdr *sec;
276     int i;
277
278     for(i = 0; i < ehdr.e_shnum; i++) {
279         sec = &shdr[i];
280         if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index) 
281             return i;
282     }
283     return 0;
284 }
285
286 void *load_data(int fd, long offset, unsigned int size)
287 {
288     char *data;
289
290     data = malloc(size);
291     if (!data)
292         return NULL;
293     lseek(fd, offset, SEEK_SET);
294     if (read(fd, data, size) != size) {
295         free(data);
296         return NULL;
297     }
298     return data;
299 }
300
301 int strstart(const char *str, const char *val, const char **ptr)
302 {
303     const char *p, *q;
304     p = str;
305     q = val;
306     while (*q != '\0') {
307         if (*p != *q)
308             return 0;
309         p++;
310         q++;
311     }
312     if (ptr)
313         *ptr = p;
314     return 1;
315 }
316
317 #ifdef HOST_ARM
318
319 int arm_emit_ldr_info(const char *name, unsigned long start_offset,
320                       FILE *outfile, uint8_t *p_start, uint8_t *p_end,
321                       ELF_RELOC *relocs, int nb_relocs)
322 {
323     uint8_t *p;
324     uint32_t insn;
325     int offset, min_offset, pc_offset, data_size;
326     uint8_t data_allocated[1024];
327     unsigned int data_index;
328     
329     memset(data_allocated, 0, sizeof(data_allocated));
330     
331     p = p_start;
332     min_offset = p_end - p_start;
333     while (p < p_start + min_offset) {
334         insn = get32((uint32_t *)p);
335         if ((insn & 0x0d5f0000) == 0x051f0000) {
336             /* ldr reg, [pc, #im] */
337             offset = insn & 0xfff;
338             if (!(insn & 0x00800000))
339                         offset = -offset;
340             if ((offset & 3) !=0)
341                 error("%s:%04x: ldr pc offset must be 32 bit aligned", 
342                       name, start_offset + p - p_start);
343             pc_offset = p - p_start + offset + 8;
344             if (pc_offset <= (p - p_start) || 
345                 pc_offset >= (p_end - p_start))
346                 error("%s:%04x: ldr pc offset must point inside the function code", 
347                       name, start_offset + p - p_start);
348             if (pc_offset < min_offset)
349                 min_offset = pc_offset;
350             if (outfile) {
351                 /* ldr position */
352                 fprintf(outfile, "    arm_ldr_ptr->ptr = gen_code_ptr + %d;\n", 
353                         p - p_start);
354                 /* ldr data index */
355                 data_index = ((p_end - p_start) - pc_offset - 4) >> 2;
356                 fprintf(outfile, "    arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n", 
357                         data_index);
358                 fprintf(outfile, "    arm_ldr_ptr++;\n");
359                 if (data_index >= sizeof(data_allocated))
360                     error("%s: too many data", name);
361                 if (!data_allocated[data_index]) {
362                     ELF_RELOC *rel;
363                     int i, addend, type;
364                     const char *sym_name, *p;
365                     char relname[1024];
366
367                     data_allocated[data_index] = 1;
368
369                     /* data value */
370                     addend = get32((uint32_t *)(p_start + pc_offset));
371                     relname[0] = '\0';
372                     for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
373                         if (rel->r_offset == (pc_offset + start_offset)) {
374                             sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
375                             /* the compiler leave some unnecessary references to the code */
376                             if (strstart(sym_name, "__op_param", &p)) {
377                                 snprintf(relname, sizeof(relname), "param%s", p);
378                             } else {
379                                 snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name);
380                             }
381                             type = ELF32_R_TYPE(rel->r_info);
382                             if (type != R_ARM_ABS32)
383                                 error("%s: unsupported data relocation", name);
384                             break;
385                         }
386                     }
387                     fprintf(outfile, "    arm_data_ptr[%d] = 0x%x",
388                             data_index, addend);
389                     if (relname[0] != '\0')
390                         fprintf(outfile, " + %s", relname);
391                     fprintf(outfile, ";\n");
392                 }
393             }
394         }
395         p += 4;
396     }
397     data_size = (p_end - p_start) - min_offset;
398     if (data_size > 0 && outfile) {
399         fprintf(outfile, "    arm_data_ptr += %d;\n", data_size >> 2);
400     }
401
402     /* the last instruction must be a mov pc, lr */
403     if (p == p_start)
404         goto arm_ret_error;
405     p -= 4;
406     insn = get32((uint32_t *)p);
407     if ((insn & 0xffff0000) != 0xe91b0000) {
408     arm_ret_error:
409         if (!outfile)
410             printf("%s: invalid epilog\n", name);
411     }
412     return p - p_start;     
413 }
414 #endif
415
416
417 #define MAX_ARGS 3
418
419 /* generate op code */
420 void gen_code(const char *name, host_ulong offset, host_ulong size, 
421               FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs,
422               int gen_switch)
423 {
424     int copy_size = 0;
425     uint8_t *p_start, *p_end;
426     host_ulong start_offset;
427     int nb_args, i, n;
428     uint8_t args_present[MAX_ARGS];
429     const char *sym_name, *p;
430     ELF_RELOC *rel;
431
432     /* Compute exact size excluding prologue and epilogue instructions.
433      * Increment start_offset to skip epilogue instructions, then compute
434      * copy_size the indicate the size of the remaining instructions (in
435      * bytes).
436      */
437     p_start = text + offset;
438     p_end = p_start + size;
439     start_offset = offset;
440     switch(ELF_ARCH) {
441     case EM_386:
442         {
443             int len;
444             len = p_end - p_start;
445             if (len == 0)
446                 error("empty code for %s", name);
447             if (p_end[-1] == 0xc3) {
448                 len--;
449             } else {
450                 error("ret or jmp expected at the end of %s", name);
451             }
452             copy_size = len;
453         }
454         break;
455     case EM_PPC:
456         {
457             uint8_t *p;
458             p = (void *)(p_end - 4);
459             if (p == p_start)
460                 error("empty code for %s", name);
461             if (get32((uint32_t *)p) != 0x4e800020)
462                 error("blr expected at the end of %s", name);
463             copy_size = p - p_start;
464         }
465         break;
466     case EM_S390:
467         {
468             uint8_t *p;
469             p = (void *)(p_end - 2);
470             if (p == p_start)
471                 error("empty code for %s", name);
472             if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
473                 error("br %%r14 expected at the end of %s", name);
474             copy_size = p - p_start;
475         }
476         break;
477     case EM_ALPHA:
478         {
479             uint8_t *p;
480             p = p_end - 4;
481             if (p == p_start)
482                 error("empty code for %s", name);
483             if (get32((uint32_t *)p) != 0x6bfa8001)
484                 error("ret expected at the end of %s", name);
485             copy_size = p - p_start;        
486         }
487         break;
488     case EM_IA_64:
489         {
490             uint8_t *p;
491             p = (void *)(p_end - 4);
492             if (p == p_start)
493                 error("empty code for %s", name);
494             /* br.ret.sptk.many b0;; */
495             /* 08 00 84 00 */
496             if (get32((uint32_t *)p) != 0x00840008)
497                 error("br.ret.sptk.many b0;; expected at the end of %s", name);
498             copy_size = p - p_start;
499         }
500         break;
501     case EM_SPARC:
502     case EM_SPARC32PLUS:
503         {
504             uint32_t start_insn, end_insn1, end_insn2;
505             uint8_t *p;
506             p = (void *)(p_end - 8);
507             if (p <= p_start)
508                 error("empty code for %s", name);
509             start_insn = get32((uint32_t *)(p_start + 0x0));
510             end_insn1 = get32((uint32_t *)(p + 0x0));
511             end_insn2 = get32((uint32_t *)(p + 0x4));
512             if ((start_insn & ~0x1fff) == 0x9de3a000) {
513                 p_start += 0x4;
514                 start_offset += 0x4;
515                 if ((int)(start_insn | ~0x1fff) < -128)
516                     error("Found bogus save at the start of %s", name);
517                 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
518                     error("ret; restore; not found at end of %s", name);
519             } else {
520                 error("No save at the beginning of %s", name);
521             }
522 #if 0
523             /* Skip a preceeding nop, if present.  */
524             if (p > p_start) {
525                 skip_insn = get32((uint32_t *)(p - 0x4));
526                 if (skip_insn == 0x01000000)
527                     p -= 4;
528             }
529 #endif
530             copy_size = p - p_start;
531         }
532         break;
533     case EM_SPARCV9:
534         {
535             uint32_t start_insn, end_insn1, end_insn2, skip_insn;
536             uint8_t *p;
537             p = (void *)(p_end - 8);
538             if (p <= p_start)
539                 error("empty code for %s", name);
540             start_insn = get32((uint32_t *)(p_start + 0x0));
541             end_insn1 = get32((uint32_t *)(p + 0x0));
542             end_insn2 = get32((uint32_t *)(p + 0x4));
543             if ((start_insn & ~0x1fff) == 0x9de3a000) {
544                 p_start += 0x4;
545                 start_offset += 0x4;
546                 if ((int)(start_insn | ~0x1fff) < -256)
547                     error("Found bogus save at the start of %s", name);
548                 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
549                     error("ret; restore; not found at end of %s", name);
550             } else {
551                 error("No save at the beginning of %s", name);
552             }
553
554             /* Skip a preceeding nop, if present.  */
555             if (p > p_start) {
556                 skip_insn = get32((uint32_t *)(p - 0x4));
557                 if (skip_insn == 0x01000000)
558                     p -= 4;
559             }
560
561             copy_size = p - p_start;
562         }
563         break;
564 #ifdef HOST_ARM
565     case EM_ARM:
566         if ((p_end - p_start) <= 16)
567             error("%s: function too small", name);
568         if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
569             (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
570             get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
571             error("%s: invalid prolog", name);
572         p_start += 12;
573         start_offset += 12;
574         copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end, 
575                                       relocs, nb_relocs);
576         break;
577 #endif
578     default:
579         error("unknown ELF architecture");
580     }
581
582     /* compute the number of arguments by looking at the relocations */
583     for(i = 0;i < MAX_ARGS; i++)
584         args_present[i] = 0;
585
586     for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
587         if (rel->r_offset >= start_offset &&
588             rel->r_offset < start_offset + (p_end - p_start)) {
589             sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
590             if (strstart(sym_name, "__op_param", &p)) {
591                 n = strtoul(p, NULL, 10);
592                 if (n > MAX_ARGS)
593                     error("too many arguments in %s", name);
594                 args_present[n - 1] = 1;
595             }
596         }
597     }
598     
599     nb_args = 0;
600     while (nb_args < MAX_ARGS && args_present[nb_args])
601         nb_args++;
602     for(i = nb_args; i < MAX_ARGS; i++) {
603         if (args_present[i])
604             error("inconsistent argument numbering in %s", name);
605     }
606
607     if (gen_switch == 2) {
608         fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
609     } else if (gen_switch == 1) {
610
611         /* output C code */
612         fprintf(outfile, "case INDEX_%s: {\n", name);
613         if (nb_args > 0) {
614             fprintf(outfile, "    long ");
615             for(i = 0; i < nb_args; i++) {
616                 if (i != 0)
617                     fprintf(outfile, ", ");
618                 fprintf(outfile, "param%d", i + 1);
619             }
620             fprintf(outfile, ";\n");
621         }
622         fprintf(outfile, "    extern void %s();\n", name);
623
624         for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
625             if (rel->r_offset >= start_offset &&
626                 rel->r_offset < start_offset + (p_end - p_start)) {
627                 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
628                 if (*sym_name && 
629                     !strstart(sym_name, "__op_param", NULL) &&
630                     !strstart(sym_name, "__op_jmp", NULL)) {
631 #if defined(HOST_SPARC)
632                     if (sym_name[0] == '.') {
633                         fprintf(outfile,
634                                 "extern char __dot_%s __asm__(\"%s\");\n",
635                                 sym_name+1, sym_name);
636                         continue;
637                     }
638 #endif
639                     fprintf(outfile, "extern char %s;\n", sym_name);
640                 }
641             }
642         }
643
644         fprintf(outfile, "    memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
645
646         /* emit code offset information */
647         {
648             ElfW(Sym) *sym;
649             const char *sym_name, *p;
650             target_ulong val;
651             int n;
652
653             for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
654                 sym_name = strtab + sym->st_name;
655                 if (strstart(sym_name, "__op_label", &p)) {
656                     uint8_t *ptr;
657                     int addend;
658                     unsigned long offset;
659                     
660                     /* test if the variable refers to a label inside
661                        the code we are generating */
662                     ptr = sdata[sym->st_shndx];
663                     if (!ptr)
664                         error("__op_labelN in invalid section");
665                     offset = sym->st_value;
666                     addend = 0;
667 #ifdef ELF_USES_RELOCA
668                     {
669                         int reloc_shndx, nb_relocs1, j;
670
671                         /* try to find a matching relocation */
672                         reloc_shndx = find_reloc(sym->st_shndx);
673                         if (reloc_shndx) {
674                             nb_relocs1 = shdr[reloc_shndx].sh_size / 
675                                 shdr[reloc_shndx].sh_entsize;
676                             rel = (ELF_RELOC *)sdata[reloc_shndx];
677                             for(j = 0; j < nb_relocs1; j++) {
678                                 if (rel->r_offset == offset) {
679                                     addend = rel->r_addend;
680                                     break;
681                                 }
682                                 rel++;
683                             }
684                         }
685                     }
686 #endif                    
687                     val = *(target_ulong *)(ptr + offset);
688                     val += addend;
689
690                     if (val >= start_offset && val < start_offset + copy_size) {
691                         n = strtol(p, NULL, 10);
692                         fprintf(outfile, "    label_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
693                     }
694                 }
695             }
696         }
697
698         /* load parameres in variables */
699         for(i = 0; i < nb_args; i++) {
700             fprintf(outfile, "    param%d = *opparam_ptr++;\n", i + 1);
701         }
702
703         /* patch relocations */
704 #if defined(HOST_I386)
705             {
706                 char name[256];
707                 int type;
708                 int addend;
709                 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
710                 if (rel->r_offset >= start_offset &&
711                     rel->r_offset < start_offset + copy_size) {
712                     sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
713                     if (strstart(sym_name, "__op_param", &p)) {
714                         snprintf(name, sizeof(name), "param%s", p);
715                     } else {
716                         snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
717                     }
718                     type = ELF32_R_TYPE(rel->r_info);
719                     addend = get32((uint32_t *)(text + rel->r_offset));
720                     switch(type) {
721                     case R_386_32:
722                         fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
723                                 rel->r_offset - start_offset, name, addend);
724                         break;
725                     case R_386_PC32:
726                         fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n", 
727                                 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
728                         break;
729                     default:
730                         error("unsupported i386 relocation (%d)", type);
731                     }
732                 }
733                 }
734             }
735 #elif defined(HOST_PPC)
736             {
737                 char name[256];
738                 int type;
739                 int addend;
740                 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
741                     if (rel->r_offset >= start_offset &&
742                         rel->r_offset < start_offset + copy_size) {
743                         sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
744                         if (strstart(sym_name, "__op_jmp", &p)) {
745                             int n;
746                             n = strtol(p, NULL, 10);
747                             /* __op_jmp relocations are done at
748                                runtime to do translated block
749                                chaining: the offset of the instruction
750                                needs to be stored */
751                             fprintf(outfile, "    jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
752                                     n, rel->r_offset - start_offset);
753                             continue;
754                         }
755                         
756                         if (strstart(sym_name, "__op_param", &p)) {
757                             snprintf(name, sizeof(name), "param%s", p);
758                         } else {
759                             snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
760                         }
761                         type = ELF32_R_TYPE(rel->r_info);
762                         addend = rel->r_addend;
763                         switch(type) {
764                         case R_PPC_ADDR32:
765                             fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
766                                     rel->r_offset - start_offset, name, addend);
767                             break;
768                         case R_PPC_ADDR16_LO:
769                             fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n", 
770                                     rel->r_offset - start_offset, name, addend);
771                             break;
772                         case R_PPC_ADDR16_HI:
773                             fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n", 
774                                     rel->r_offset - start_offset, name, addend);
775                             break;
776                         case R_PPC_ADDR16_HA:
777                             fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n", 
778                                     rel->r_offset - start_offset, name, addend);
779                             break;
780                         case R_PPC_REL24:
781                             /* warning: must be at 32 MB distancy */
782                             fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n", 
783                                     rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
784                             break;
785                         default:
786                             error("unsupported powerpc relocation (%d)", type);
787                         }
788                     }
789                 }
790             }
791 #elif defined(HOST_S390)
792             {
793                 char name[256];
794                 int type;
795                 int addend;
796                 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
797                     if (rel->r_offset >= start_offset &&
798                         rel->r_offset < start_offset + copy_size) {
799                         sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
800                         if (strstart(sym_name, "__op_param", &p)) {
801                             snprintf(name, sizeof(name), "param%s", p);
802                         } else {
803                             snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
804                         }
805                         type = ELF32_R_TYPE(rel->r_info);
806                         addend = rel->r_addend;
807                         switch(type) {
808                         case R_390_32:
809                             fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
810                                     rel->r_offset - start_offset, name, addend);
811                             break;
812                         case R_390_16:
813                             fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n", 
814                                     rel->r_offset - start_offset, name, addend);
815                             break;
816                         case R_390_8:
817                             fprintf(outfile, "    *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n", 
818                                     rel->r_offset - start_offset, name, addend);
819                             break;
820                         default:
821                             error("unsupported s390 relocation (%d)", type);
822                         }
823                     }
824                 }
825             }
826 #elif defined(HOST_ALPHA)
827             {
828                 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
829                     if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
830                         int type;
831
832                         type = ELF64_R_TYPE(rel->r_info);
833                         sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
834                         switch (type) {
835                         case R_ALPHA_GPDISP:
836                             /* The gp is just 32 bit, and never changes, so it's easiest to emit it
837                                as an immediate instead of constructing it from the pv or ra.  */
838                             fprintf(outfile, "    immediate_ldah(gen_code_ptr + %ld, gp);\n",
839                                     rel->r_offset - start_offset);
840                             fprintf(outfile, "    immediate_lda(gen_code_ptr + %ld, gp);\n",
841                                     rel->r_offset - start_offset + rel->r_addend);
842                             break;
843                         case R_ALPHA_LITUSE:
844                             /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
845                                now, since some called functions (libc) need pv to be set up.  */
846                             break;
847                         case R_ALPHA_HINT:
848                             /* Branch target prediction hint. Ignore for now.  Should be already
849                                correct for in-function jumps.  */
850                             break;
851                         case R_ALPHA_LITERAL:
852                             /* Load a literal from the GOT relative to the gp.  Since there's only a
853                                single gp, nothing is to be done.  */
854                             break;
855                         case R_ALPHA_GPRELHIGH:
856                             /* Handle fake relocations against __op_param symbol.  Need to emit the
857                                high part of the immediate value instead.  Other symbols need no
858                                special treatment.  */
859                             if (strstart(sym_name, "__op_param", &p))
860                                 fprintf(outfile, "    immediate_ldah(gen_code_ptr + %ld, param%s);\n",
861                                         rel->r_offset - start_offset, p);
862                             break;
863                         case R_ALPHA_GPRELLOW:
864                             if (strstart(sym_name, "__op_param", &p))
865                                 fprintf(outfile, "    immediate_lda(gen_code_ptr + %ld, param%s);\n",
866                                         rel->r_offset - start_offset, p);
867                             break;
868                         case R_ALPHA_BRSGP:
869                             /* PC-relative jump. Tweak offset to skip the two instructions that try to
870                                set up the gp from the pv.  */
871                             fprintf(outfile, "    fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
872                                     rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
873                             break;
874                         default:
875                             error("unsupported Alpha relocation (%d)", type);
876                         }
877                     }
878                 }
879             }
880 #elif defined(HOST_IA64)
881             {
882                 char name[256];
883                 int type;
884                 int addend;
885                 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
886                     if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
887                         sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
888                         if (strstart(sym_name, "__op_param", &p)) {
889                             snprintf(name, sizeof(name), "param%s", p);
890                         } else {
891                             snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
892                         }
893                         type = ELF64_R_TYPE(rel->r_info);
894                         addend = rel->r_addend;
895                         switch(type) {
896                         case R_IA64_LTOFF22:
897                             error("must implemnt R_IA64_LTOFF22 relocation");
898                         case R_IA64_PCREL21B:
899                             error("must implemnt R_IA64_PCREL21B relocation");
900                         default:
901                             error("unsupported ia64 relocation (%d)", type);
902                         }
903                     }
904                 }
905             }
906 #elif defined(HOST_SPARC)
907             {
908                 char name[256];
909                 int type;
910                 int addend;
911                 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
912                     if (rel->r_offset >= start_offset &&
913                         rel->r_offset < start_offset + copy_size) {
914                         sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
915                         if (strstart(sym_name, "__op_param", &p)) {
916                             snprintf(name, sizeof(name), "param%s", p);
917                         } else {
918                                 if (sym_name[0] == '.')
919                                         snprintf(name, sizeof(name),
920                                                  "(long)(&__dot_%s)",
921                                                  sym_name + 1);
922                                 else
923                                         snprintf(name, sizeof(name),
924                                                  "(long)(&%s)", sym_name);
925                         }
926                         type = ELF32_R_TYPE(rel->r_info);
927                         addend = rel->r_addend;
928                         switch(type) {
929                         case R_SPARC_32:
930                             fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
931                                     rel->r_offset - start_offset, name, addend);
932                             break;
933                         case R_SPARC_HI22:
934                             fprintf(outfile,
935                                     "    *(uint32_t *)(gen_code_ptr + %d) = "
936                                     "((*(uint32_t *)(gen_code_ptr + %d)) "
937                                     " & ~0x3fffff) "
938                                     " | (((%s + %d) >> 10) & 0x3fffff);\n",
939                                     rel->r_offset - start_offset,
940                                     rel->r_offset - start_offset,
941                                     name, addend);
942                             break;
943                         case R_SPARC_LO10:
944                             fprintf(outfile,
945                                     "    *(uint32_t *)(gen_code_ptr + %d) = "
946                                     "((*(uint32_t *)(gen_code_ptr + %d)) "
947                                     " & ~0x3ff) "
948                                     " | ((%s + %d) & 0x3ff);\n",
949                                     rel->r_offset - start_offset,
950                                     rel->r_offset - start_offset,
951                                     name, addend);
952                             break;
953                         case R_SPARC_WDISP30:
954                             fprintf(outfile,
955                                     "    *(uint32_t *)(gen_code_ptr + %d) = "
956                                     "((*(uint32_t *)(gen_code_ptr + %d)) "
957                                     " & ~0x3fffffff) "
958                                     " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
959                                     "    & 0x3fffffff);\n",
960                                     rel->r_offset - start_offset,
961                                     rel->r_offset - start_offset,
962                                     name, addend,
963                                     rel->r_offset - start_offset);
964                             break;
965                         default:
966                             error("unsupported sparc relocation (%d)", type);
967                         }
968                     }
969                 }
970             }
971 #elif defined(HOST_SPARC64)
972             {
973                 char name[256];
974                 int type;
975                 int addend;
976                 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
977                     if (rel->r_offset >= start_offset &&
978                         rel->r_offset < start_offset + copy_size) {
979                         sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
980                         if (strstart(sym_name, "__op_param", &p)) {
981                             snprintf(name, sizeof(name), "param%s", p);
982                         } else {
983                             snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
984                         }
985                         type = ELF64_R_TYPE(rel->r_info);
986                         addend = rel->r_addend;
987                         switch(type) {
988                         case R_SPARC_32:
989                             fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
990                                     rel->r_offset - start_offset, name, addend);
991                             break;
992                         case R_SPARC_HI22:
993                             fprintf(outfile,
994                                     "    *(uint32_t *)(gen_code_ptr + %d) = "
995                                     "((*(uint32_t *)(gen_code_ptr + %d)) "
996                                     " & ~0x3fffff) "
997                                     " | (((%s + %d) >> 10) & 0x3fffff);\n",
998                                     rel->r_offset - start_offset,
999                                     rel->r_offset - start_offset,
1000                                     name, addend);
1001                             break;
1002                         case R_SPARC_LO10:
1003                             fprintf(outfile,
1004                                     "    *(uint32_t *)(gen_code_ptr + %d) = "
1005                                     "((*(uint32_t *)(gen_code_ptr + %d)) "
1006                                     " & ~0x3ff) "
1007                                     " | ((%s + %d) & 0x3ff);\n",
1008                                     rel->r_offset - start_offset,
1009                                     rel->r_offset - start_offset,
1010                                     name, addend);
1011                             break;
1012                         case R_SPARC_WDISP30:
1013                             fprintf(outfile,
1014                                     "    *(uint32_t *)(gen_code_ptr + %d) = "
1015                                     "((*(uint32_t *)(gen_code_ptr + %d)) "
1016                                     " & ~0x3fffffff) "
1017                                     " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1018                                     "    & 0x3fffffff);\n",
1019                                     rel->r_offset - start_offset,
1020                                     rel->r_offset - start_offset,
1021                                     name, addend,
1022                                     rel->r_offset - start_offset);
1023                             break;
1024                         default:
1025                             error("unsupported sparc64 relocation (%d)", type);
1026                         }
1027                     }
1028                 }
1029             }
1030 #elif defined(HOST_ARM)
1031             {
1032                 char name[256];
1033                 int type;
1034                 int addend;
1035
1036                 arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
1037                                   relocs, nb_relocs);
1038
1039                 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1040                 if (rel->r_offset >= start_offset &&
1041                     rel->r_offset < start_offset + copy_size) {
1042                     sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1043                     /* the compiler leave some unnecessary references to the code */
1044                     if (sym_name[0] == '\0')
1045                         continue;
1046                     if (strstart(sym_name, "__op_param", &p)) {
1047                         snprintf(name, sizeof(name), "param%s", p);
1048                     } else {
1049                         snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1050                     }
1051                     type = ELF32_R_TYPE(rel->r_info);
1052                     addend = get32((uint32_t *)(text + rel->r_offset));
1053                     switch(type) {
1054                     case R_ARM_ABS32:
1055                         fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
1056                                 rel->r_offset - start_offset, name, addend);
1057                         break;
1058                     case R_ARM_PC24:
1059                         fprintf(outfile, "    arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n", 
1060                                 rel->r_offset - start_offset, addend, name);
1061                         break;
1062                     default:
1063                         error("unsupported arm relocation (%d)", type);
1064                     }
1065                 }
1066                 }
1067             }
1068 #else
1069 #error unsupported CPU
1070 #endif
1071         fprintf(outfile, "    gen_code_ptr += %d;\n", copy_size);
1072         fprintf(outfile, "}\n");
1073         fprintf(outfile, "break;\n\n");
1074     } else {
1075         fprintf(outfile, "static inline void gen_%s(", name);
1076         if (nb_args == 0) {
1077             fprintf(outfile, "void");
1078         } else {
1079             for(i = 0; i < nb_args; i++) {
1080                 if (i != 0)
1081                     fprintf(outfile, ", ");
1082                 fprintf(outfile, "long param%d", i + 1);
1083             }
1084         }
1085         fprintf(outfile, ")\n");
1086         fprintf(outfile, "{\n");
1087         for(i = 0; i < nb_args; i++) {
1088             fprintf(outfile, "    *gen_opparam_ptr++ = param%d;\n", i + 1);
1089         }
1090         fprintf(outfile, "    *gen_opc_ptr++ = INDEX_%s;\n", name);
1091         fprintf(outfile, "}\n\n");
1092     }
1093 }
1094
1095 /* load an elf object file */
1096 int load_elf(const char *filename, FILE *outfile, int out_type)
1097 {
1098     int fd;
1099     struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
1100     int i, j;
1101     ElfW(Sym) *sym;
1102     char *shstr;
1103     uint8_t *text;
1104     ELF_RELOC *relocs;
1105     int nb_relocs;
1106     ELF_RELOC *rel;
1107     
1108     fd = open(filename, O_RDONLY);
1109     if (fd < 0) 
1110         error("can't open file '%s'", filename);
1111     
1112     /* Read ELF header.  */
1113     if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
1114         error("unable to read file header");
1115
1116     /* Check ELF identification.  */
1117     if (ehdr.e_ident[EI_MAG0] != ELFMAG0
1118      || ehdr.e_ident[EI_MAG1] != ELFMAG1
1119      || ehdr.e_ident[EI_MAG2] != ELFMAG2
1120      || ehdr.e_ident[EI_MAG3] != ELFMAG3
1121      || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
1122         error("bad ELF header");
1123     }
1124
1125     do_swap = elf_must_swap(&ehdr);
1126     if (do_swap)
1127         elf_swap_ehdr(&ehdr);
1128     if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
1129         error("Unsupported ELF class");
1130     if (ehdr.e_type != ET_REL)
1131         error("ELF object file expected");
1132     if (ehdr.e_version != EV_CURRENT)
1133         error("Invalid ELF version");
1134     if (!elf_check_arch(ehdr.e_machine))
1135         error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
1136
1137     /* read section headers */
1138     shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
1139     if (do_swap) {
1140         for(i = 0; i < ehdr.e_shnum; i++) {
1141             elf_swap_shdr(&shdr[i]);
1142         }
1143     }
1144
1145     /* read all section data */
1146     sdata = malloc(sizeof(void *) * ehdr.e_shnum);
1147     memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
1148     
1149     for(i = 0;i < ehdr.e_shnum; i++) {
1150         sec = &shdr[i];
1151         if (sec->sh_type != SHT_NOBITS)
1152             sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
1153     }
1154
1155     sec = &shdr[ehdr.e_shstrndx];
1156     shstr = sdata[ehdr.e_shstrndx];
1157
1158     /* swap relocations */
1159     for(i = 0; i < ehdr.e_shnum; i++) {
1160         sec = &shdr[i];
1161         if (sec->sh_type == SHT_RELOC) {
1162             nb_relocs = sec->sh_size / sec->sh_entsize;
1163             if (do_swap) {
1164                 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
1165                     elf_swap_rel(rel);
1166             }
1167         }
1168     }
1169     /* text section */
1170
1171     text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
1172     if (!text_sec)
1173         error("could not find .text section");
1174     text_shndx = text_sec - shdr;
1175     text = sdata[text_shndx];
1176
1177     /* find text relocations, if any */
1178     relocs = NULL;
1179     nb_relocs = 0;
1180     i = find_reloc(text_shndx);
1181     if (i != 0) {
1182         relocs = (ELF_RELOC *)sdata[i];
1183         nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
1184     }
1185
1186     symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
1187     if (!symtab_sec)
1188         error("could not find .symtab section");
1189     strtab_sec = &shdr[symtab_sec->sh_link];
1190
1191     symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
1192     strtab = sdata[symtab_sec->sh_link];
1193     
1194     nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
1195     if (do_swap) {
1196         for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1197             swab32s(&sym->st_name);
1198             swabls(&sym->st_value);
1199             swabls(&sym->st_size);
1200             swab16s(&sym->st_shndx);
1201         }
1202     }
1203
1204     if (out_type == OUT_INDEX_OP) {
1205         fprintf(outfile, "DEF(end, 0, 0)\n");
1206         for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1207             const char *name, *p;
1208             name = strtab + sym->st_name;
1209             if (strstart(name, OP_PREFIX, &p)) {
1210                 gen_code(name, sym->st_value, sym->st_size, outfile, 
1211                          text, relocs, nb_relocs, 2);
1212             }
1213         }
1214     } else if (out_type == OUT_GEN_OP) {
1215         /* generate gen_xxx functions */
1216
1217         for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1218             const char *name;
1219             name = strtab + sym->st_name;
1220             if (strstart(name, OP_PREFIX, NULL)) {
1221                 if (sym->st_shndx != (text_sec - shdr))
1222                     error("invalid section for opcode (0x%x)", sym->st_shndx);
1223                 gen_code(name, sym->st_value, sym->st_size, outfile, 
1224                          text, relocs, nb_relocs, 0);
1225             }
1226         }
1227         
1228     } else {
1229         /* generate big code generation switch */
1230 fprintf(outfile,
1231 "int dyngen_code(uint8_t *gen_code_buf,\n"
1232 "                uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1233 "                const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1234 "{\n"
1235 "    uint8_t *gen_code_ptr;\n"
1236 "    const uint16_t *opc_ptr;\n"
1237 "    const uint32_t *opparam_ptr;\n");
1238
1239 #ifdef HOST_ARM
1240 fprintf(outfile,
1241 "    uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1242 "    LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1243 "    uint32_t *arm_data_ptr = arm_data_table;\n");
1244 #endif
1245
1246 fprintf(outfile,
1247 "\n"
1248 "    gen_code_ptr = gen_code_buf;\n"
1249 "    opc_ptr = opc_buf;\n"
1250 "    opparam_ptr = opparam_buf;\n");
1251
1252         /* Generate prologue, if needed. */ 
1253
1254 fprintf(outfile,
1255 "    for(;;) {\n"
1256 "        switch(*opc_ptr++) {\n"
1257 );
1258
1259         for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1260             const char *name;
1261             name = strtab + sym->st_name;
1262             if (strstart(name, OP_PREFIX, NULL)) {
1263 #if 0
1264                 printf("%4d: %s pos=0x%08x len=%d\n", 
1265                        i, name, sym->st_value, sym->st_size);
1266 #endif
1267                 if (sym->st_shndx != (text_sec - shdr))
1268                     error("invalid section for opcode (0x%x)", sym->st_shndx);
1269                 gen_code(name, sym->st_value, sym->st_size, outfile, 
1270                          text, relocs, nb_relocs, 1);
1271             }
1272         }
1273
1274 fprintf(outfile,
1275 "        default:\n"
1276 "            goto the_end;\n"
1277 "        }\n");
1278
1279 #ifdef HOST_ARM
1280 /* generate constant table if needed */
1281 fprintf(outfile,
1282 "        if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1283 "            gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1284 "            last_gen_code_ptr = gen_code_ptr;\n"
1285 "            arm_ldr_ptr = arm_ldr_table;\n"
1286 "            arm_data_ptr = arm_data_table;\n"
1287 "        }\n");         
1288 #endif
1289
1290
1291 fprintf(outfile,
1292 "    }\n"
1293 " the_end:\n"
1294 );
1295
1296 /* generate epilogue */ 
1297     switch(ELF_ARCH) {
1298     case EM_386:
1299         fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
1300         break;
1301     case EM_PPC:
1302         fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
1303         break;
1304     case EM_S390:
1305         fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
1306         break;
1307     case EM_ALPHA:
1308         fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n");
1309         break;
1310     case EM_IA_64:
1311         fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n");
1312         break;
1313     case EM_SPARC:
1314     case EM_SPARC32PLUS:
1315         fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c62008; /* jmpl %%i0 + 8, %%g0 */\n");
1316         fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x01000000; /* nop */\n");
1317         break;
1318     case EM_SPARCV9:
1319         fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c7e008; /* ret */\n");
1320         fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81e80000; /* restore */\n");
1321         break;
1322     case EM_ARM:
1323         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");
1324         break;
1325     default:
1326         error("unknown ELF architecture");
1327     }
1328     /* flush instruction cache */
1329     fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
1330
1331     fprintf(outfile, "return gen_code_ptr -  gen_code_buf;\n");
1332     fprintf(outfile, "}\n\n");
1333
1334     }
1335
1336     close(fd);
1337     return 0;
1338 }
1339
1340 void usage(void)
1341 {
1342     printf("dyngen (c) 2003 Fabrice Bellard\n"
1343            "usage: dyngen [-o outfile] [-c] objfile\n"
1344            "Generate a dynamic code generator from an object file\n"
1345            "-c     output enum of operations\n"
1346            "-g     output gen_op_xx() functions\n"
1347            );
1348     exit(1);
1349 }
1350
1351 int main(int argc, char **argv)
1352 {
1353     int c, out_type;
1354     const char *filename, *outfilename;
1355     FILE *outfile;
1356
1357     outfilename = "out.c";
1358     out_type = OUT_CODE;
1359     for(;;) {
1360         c = getopt(argc, argv, "ho:cg");
1361         if (c == -1)
1362             break;
1363         switch(c) {
1364         case 'h':
1365             usage();
1366             break;
1367         case 'o':
1368             outfilename = optarg;
1369             break;
1370         case 'c':
1371             out_type = OUT_INDEX_OP;
1372             break;
1373         case 'g':
1374             out_type = OUT_GEN_OP;
1375             break;
1376         }
1377     }
1378     if (optind >= argc)
1379         usage();
1380     filename = argv[optind];
1381     outfile = fopen(outfilename, "w");
1382     if (!outfile)
1383         error("could not open '%s'", outfilename);
1384     load_elf(filename, outfile, out_type);
1385     fclose(outfile);
1386     return 0;
1387 }