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