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