]> git.proxmox.com Git - qemu.git/blob - dyngen.c
added runcom
[qemu.git] / 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 <elf.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28
29 #include "thunk.h"
30
31 /* all dynamically generated functions begin with this code */
32 #define OP_PREFIX "op_"
33
34 int elf_must_swap(Elf32_Ehdr *h)
35 {
36 union {
37 uint32_t i;
38 uint8_t b[4];
39 } swaptest;
40
41 swaptest.i = 1;
42 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
43 (swaptest.b[0] == 0);
44 }
45
46 void swab16s(uint16_t *p)
47 {
48 *p = bswap16(*p);
49 }
50
51 void swab32s(uint32_t *p)
52 {
53 *p = bswap32(*p);
54 }
55
56 void swab64s(uint32_t *p)
57 {
58 *p = bswap64(*p);
59 }
60
61 void elf_swap_ehdr(Elf32_Ehdr *h)
62 {
63 swab16s(&h->e_type); /* Object file type */
64 swab16s(&h-> e_machine); /* Architecture */
65 swab32s(&h-> e_version); /* Object file version */
66 swab32s(&h-> e_entry); /* Entry point virtual address */
67 swab32s(&h-> e_phoff); /* Program header table file offset */
68 swab32s(&h-> e_shoff); /* Section header table file offset */
69 swab32s(&h-> e_flags); /* Processor-specific flags */
70 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
71 swab16s(&h-> e_phentsize); /* Program header table entry size */
72 swab16s(&h-> e_phnum); /* Program header table entry count */
73 swab16s(&h-> e_shentsize); /* Section header table entry size */
74 swab16s(&h-> e_shnum); /* Section header table entry count */
75 swab16s(&h-> e_shstrndx); /* Section header string table index */
76 }
77
78 void elf_swap_shdr(Elf32_Shdr *h)
79 {
80 swab32s(&h-> sh_name); /* Section name (string tbl index) */
81 swab32s(&h-> sh_type); /* Section type */
82 swab32s(&h-> sh_flags); /* Section flags */
83 swab32s(&h-> sh_addr); /* Section virtual addr at execution */
84 swab32s(&h-> sh_offset); /* Section file offset */
85 swab32s(&h-> sh_size); /* Section size in bytes */
86 swab32s(&h-> sh_link); /* Link to another section */
87 swab32s(&h-> sh_info); /* Additional section information */
88 swab32s(&h-> sh_addralign); /* Section alignment */
89 swab32s(&h-> sh_entsize); /* Entry size if section holds table */
90 }
91
92 void elf_swap_phdr(Elf32_Phdr *h)
93 {
94 swab32s(&h->p_type); /* Segment type */
95 swab32s(&h->p_offset); /* Segment file offset */
96 swab32s(&h->p_vaddr); /* Segment virtual address */
97 swab32s(&h->p_paddr); /* Segment physical address */
98 swab32s(&h->p_filesz); /* Segment size in file */
99 swab32s(&h->p_memsz); /* Segment size in memory */
100 swab32s(&h->p_flags); /* Segment flags */
101 swab32s(&h->p_align); /* Segment alignment */
102 }
103
104 int do_swap;
105 int e_machine;
106
107 uint16_t get16(uint16_t *p)
108 {
109 uint16_t val;
110 val = *p;
111 if (do_swap)
112 val = bswap16(val);
113 return val;
114 }
115
116 uint32_t get32(uint32_t *p)
117 {
118 uint32_t val;
119 val = *p;
120 if (do_swap)
121 val = bswap32(val);
122 return val;
123 }
124
125 void put16(uint16_t *p, uint16_t val)
126 {
127 if (do_swap)
128 val = bswap16(val);
129 *p = val;
130 }
131
132 void put32(uint32_t *p, uint32_t val)
133 {
134 if (do_swap)
135 val = bswap32(val);
136 *p = val;
137 }
138
139 void __attribute__((noreturn)) error(const char *fmt, ...)
140 {
141 va_list ap;
142 va_start(ap, fmt);
143 fprintf(stderr, "dyngen: ");
144 vfprintf(stderr, fmt, ap);
145 fprintf(stderr, "\n");
146 va_end(ap);
147 exit(1);
148 }
149
150
151 Elf32_Shdr *find_elf_section(Elf32_Shdr *shdr, int shnum, const char *shstr,
152 const char *name)
153 {
154 int i;
155 const char *shname;
156 Elf32_Shdr *sec;
157
158 for(i = 0; i < shnum; i++) {
159 sec = &shdr[i];
160 if (!sec->sh_name)
161 continue;
162 shname = shstr + sec->sh_name;
163 if (!strcmp(shname, name))
164 return sec;
165 }
166 return NULL;
167 }
168
169 void *load_data(int fd, long offset, unsigned int size)
170 {
171 char *data;
172
173 data = malloc(size);
174 if (!data)
175 return NULL;
176 lseek(fd, offset, SEEK_SET);
177 if (read(fd, data, size) != size) {
178 free(data);
179 return NULL;
180 }
181 return data;
182 }
183
184 int strstart(const char *str, const char *val, const char **ptr)
185 {
186 const char *p, *q;
187 p = str;
188 q = val;
189 while (*q != '\0') {
190 if (*p != *q)
191 return 0;
192 p++;
193 q++;
194 }
195 if (ptr)
196 *ptr = p;
197 return 1;
198 }
199
200 #define MAX_ARGS 3
201
202 /* generate op code */
203 void gen_code(const char *name, unsigned long offset, unsigned long size,
204 FILE *outfile, uint8_t *text, void *relocs, int nb_relocs, int reloc_sh_type,
205 Elf32_Sym *symtab, char *strtab, int gen_switch)
206 {
207 int copy_size = 0;
208 uint8_t *p_start, *p_end;
209 int nb_args, i;
210 uint8_t args_present[MAX_ARGS];
211 const char *sym_name, *p;
212
213 /* compute exact size excluding return instruction */
214 p_start = text + offset;
215 p_end = p_start + size;
216 switch(e_machine) {
217 case EM_386:
218 {
219 uint8_t *p;
220 p = p_end - 1;
221 if (p == p_start)
222 error("empty code for %s", name);
223 if (p[0] != 0xc3)
224 error("ret expected at the end of %s", name);
225 copy_size = p - p_start;
226 }
227 break;
228 case EM_PPC:
229 {
230 uint8_t *p;
231 p = (void *)(p_end - 4);
232 if (p == p_start)
233 error("empty code for %s", name);
234 if (get32((uint32_t *)p) != 0x4e800020)
235 error("blr expected at the end of %s", name);
236 copy_size = p - p_start;
237 }
238 break;
239 default:
240 error("unsupported CPU (%d)", e_machine);
241 }
242
243 /* compute the number of arguments by looking at the relocations */
244 for(i = 0;i < MAX_ARGS; i++)
245 args_present[i] = 0;
246
247 if (reloc_sh_type == SHT_REL) {
248 Elf32_Rel *rel;
249 int n;
250 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
251 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
252 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
253 if (strstart(sym_name, "__op_param", &p)) {
254 n = strtoul(p, NULL, 10);
255 if (n >= MAX_ARGS)
256 error("too many arguments in %s", name);
257 args_present[n - 1] = 1;
258 }
259 }
260 }
261 } else {
262 Elf32_Rela *rel;
263 int n;
264 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
265 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
266 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
267 if (strstart(sym_name, "__op_param", &p)) {
268 n = strtoul(p, NULL, 10);
269 if (n >= MAX_ARGS)
270 error("too many arguments in %s", name);
271 args_present[n - 1] = 1;
272 }
273 }
274 }
275 }
276
277 nb_args = 0;
278 while (nb_args < MAX_ARGS && args_present[nb_args])
279 nb_args++;
280 for(i = nb_args; i < MAX_ARGS; i++) {
281 if (args_present[i])
282 error("inconsistent argument numbering in %s", name);
283 }
284
285 if (gen_switch) {
286
287 /* output C code */
288 fprintf(outfile, "case INDEX_%s: {\n", name);
289 if (nb_args > 0) {
290 fprintf(outfile, " long ");
291 for(i = 0; i < nb_args; i++) {
292 if (i != 0)
293 fprintf(outfile, ", ");
294 fprintf(outfile, "param%d", i + 1);
295 }
296 fprintf(outfile, ";\n");
297 }
298 fprintf(outfile, " extern void %s();\n", name);
299
300 if (reloc_sh_type == SHT_REL) {
301 Elf32_Rel *rel;
302 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
303 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
304 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
305 if (!strstart(sym_name, "__op_param", &p)) {
306 fprintf(outfile, "extern char %s;\n", sym_name);
307 }
308 }
309 }
310 } else {
311 Elf32_Rela *rel;
312 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
313 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
314 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
315 if (!strstart(sym_name, "__op_param", &p)) {
316 fprintf(outfile, "extern char %s;\n", sym_name);
317 }
318 }
319 }
320 }
321
322 fprintf(outfile, " memcpy(gen_code_ptr, &%s, %d);\n", name, copy_size);
323 for(i = 0; i < nb_args; i++) {
324 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
325 }
326
327 /* patch relocations */
328 switch(e_machine) {
329 case EM_386:
330 {
331 Elf32_Rel *rel;
332 char name[256];
333 int type;
334 long addend;
335 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
336 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
337 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
338 if (strstart(sym_name, "__op_param", &p)) {
339 snprintf(name, sizeof(name), "param%s", p);
340 } else {
341 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
342 }
343 type = ELF32_R_TYPE(rel->r_info);
344 addend = get32((uint32_t *)(text + rel->r_offset));
345 switch(type) {
346 case R_386_32:
347 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
348 rel->r_offset - offset, name, addend);
349 break;
350 case R_386_PC32:
351 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s - (long)(gen_code_ptr + %ld) + %ld;\n",
352 rel->r_offset - offset, name, rel->r_offset - offset, addend);
353 break;
354 default:
355 error("unsupported i386 relocation (%d)", type);
356 }
357 }
358 }
359 }
360 break;
361 case EM_PPC:
362 {
363 Elf32_Rela *rel;
364 char name[256];
365 int type;
366 long addend;
367 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
368 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
369 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
370 if (strstart(sym_name, "__op_param", &p)) {
371 snprintf(name, sizeof(name), "param%s", p);
372 } else {
373 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
374 }
375 type = ELF32_R_TYPE(rel->r_info);
376 addend = rel->r_addend;
377 switch(type) {
378 case R_PPC_ADDR32:
379 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
380 rel->r_offset - offset, name, addend);
381 break;
382 case R_PPC_ADDR16_LO:
383 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld);\n",
384 rel->r_offset - offset, name, addend);
385 break;
386 case R_PPC_ADDR16_HI:
387 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld) >> 16;\n",
388 rel->r_offset - offset, name, addend);
389 break;
390 case R_PPC_ADDR16_HA:
391 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld + 0x8000) >> 16;\n",
392 rel->r_offset - offset, name, addend);
393 break;
394 case R_PPC_REL24:
395 /* warning: must be at 32 MB distancy */
396 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = (*(uint32_t *)(gen_code_ptr + %ld) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %ld) + %ld) & 0x03fffffc);\n",
397 rel->r_offset - offset, rel->r_offset - offset, name, rel->r_offset - offset, addend);
398 break;
399 default:
400 error("unsupported powerpc relocation (%d)", type);
401 }
402 }
403 }
404 }
405 break;
406 default:
407 error("unsupported CPU for relocations (%d)", e_machine);
408 }
409 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
410 fprintf(outfile, "}\n");
411 fprintf(outfile, "break;\n\n");
412 } else {
413 fprintf(outfile, "static inline void gen_%s(", name);
414 if (nb_args == 0) {
415 fprintf(outfile, "void");
416 } else {
417 for(i = 0; i < nb_args; i++) {
418 if (i != 0)
419 fprintf(outfile, ", ");
420 fprintf(outfile, "long param%d", i + 1);
421 }
422 }
423 fprintf(outfile, ")\n");
424 fprintf(outfile, "{\n");
425 for(i = 0; i < nb_args; i++) {
426 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
427 }
428 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
429 fprintf(outfile, "}\n\n");
430 }
431 }
432
433 /* load an elf object file */
434 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
435 {
436 int fd;
437 Elf32_Ehdr ehdr;
438 Elf32_Shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
439 int i, j, nb_syms;
440 Elf32_Sym *symtab, *sym;
441 const char *cpu_name;
442 char *shstr, *strtab;
443 uint8_t *text;
444 void *relocs;
445 int nb_relocs, reloc_sh_type;
446
447 fd = open(filename, O_RDONLY);
448 if (fd < 0)
449 error("can't open file '%s'", filename);
450
451 /* Read ELF header. */
452 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
453 error("unable to read file header");
454
455 /* Check ELF identification. */
456 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
457 || ehdr.e_ident[EI_MAG1] != ELFMAG1
458 || ehdr.e_ident[EI_MAG2] != ELFMAG2
459 || ehdr.e_ident[EI_MAG3] != ELFMAG3
460 || ehdr.e_ident[EI_CLASS] != ELFCLASS32
461 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
462 error("bad ELF header");
463 }
464
465 do_swap = elf_must_swap(&ehdr);
466 if (do_swap)
467 elf_swap_ehdr(&ehdr);
468 if (ehdr.e_type != ET_REL)
469 error("ELF object file expected");
470 if (ehdr.e_version != EV_CURRENT)
471 error("Invalid ELF version");
472 e_machine = ehdr.e_machine;
473
474 /* read section headers */
475 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(Elf32_Shdr));
476 if (do_swap) {
477 for(i = 0; i < ehdr.e_shnum; i++) {
478 elf_swap_shdr(&shdr[i]);
479 }
480 }
481
482 sec = &shdr[ehdr.e_shstrndx];
483 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
484
485 /* text section */
486
487 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
488 if (!text_sec)
489 error("could not find .text section");
490 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
491
492 /* find text relocations, if any */
493 nb_relocs = 0;
494 relocs = NULL;
495 reloc_sh_type = 0;
496 for(i = 0; i < ehdr.e_shnum; i++) {
497 sec = &shdr[i];
498 if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) &&
499 sec->sh_info == (text_sec - shdr)) {
500 reloc_sh_type = sec->sh_type;
501 relocs = load_data(fd, sec->sh_offset, sec->sh_size);
502 nb_relocs = sec->sh_size / sec->sh_entsize;
503 if (do_swap) {
504 if (sec->sh_type == SHT_REL) {
505 Elf32_Rel *rel = relocs;
506 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
507 swab32s(&rel->r_offset);
508 swab32s(&rel->r_info);
509 }
510 } else {
511 Elf32_Rela *rel = relocs;
512 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
513 swab32s(&rel->r_offset);
514 swab32s(&rel->r_info);
515 swab32s(&rel->r_addend);
516 }
517 }
518 }
519 break;
520 }
521 }
522
523 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
524 if (!symtab_sec)
525 error("could not find .symtab section");
526 strtab_sec = &shdr[symtab_sec->sh_link];
527
528 symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size);
529 strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size);
530
531 nb_syms = symtab_sec->sh_size / sizeof(Elf32_Sym);
532 if (do_swap) {
533 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
534 swab32s(&sym->st_name);
535 swab32s(&sym->st_value);
536 swab32s(&sym->st_size);
537 swab16s(&sym->st_shndx);
538 }
539 }
540
541 switch(e_machine) {
542 case EM_386:
543 cpu_name = "i386";
544 break;
545 case EM_PPC:
546 cpu_name = "ppc";
547 break;
548 case EM_MIPS:
549 cpu_name = "mips";
550 break;
551 case EM_ARM:
552 cpu_name = "arm";
553 break;
554 case EM_SPARC:
555 cpu_name = "sparc";
556 break;
557 default:
558 error("unsupported CPU (e_machine=%d)", e_machine);
559 }
560
561 if (do_print_enum) {
562 fprintf(outfile, "DEF(end)\n");
563 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
564 const char *name, *p;
565 name = strtab + sym->st_name;
566 if (strstart(name, OP_PREFIX, &p)) {
567 fprintf(outfile, "DEF(%s)\n", p);
568 }
569 }
570 } else {
571 /* generate big code generation switch */
572 fprintf(outfile,
573 "int dyngen_code(uint8_t *gen_code_buf,\n"
574 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
575 "{\n"
576 " uint8_t *gen_code_ptr;\n"
577 " const uint16_t *opc_ptr;\n"
578 " const uint32_t *opparam_ptr;\n"
579 " gen_code_ptr = gen_code_buf;\n"
580 " opc_ptr = opc_buf;\n"
581 " opparam_ptr = opparam_buf;\n"
582 " for(;;) {\n"
583 " switch(*opc_ptr++) {\n"
584 );
585
586 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
587 const char *name;
588 name = strtab + sym->st_name;
589 if (strstart(name, OP_PREFIX, NULL)) {
590 #if 0
591 printf("%4d: %s pos=0x%08x len=%d\n",
592 i, name, sym->st_value, sym->st_size);
593 #endif
594 if (sym->st_shndx != (text_sec - shdr))
595 error("invalid section for opcode (0x%x)", sym->st_shndx);
596 gen_code(name, sym->st_value, sym->st_size, outfile,
597 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 1);
598 }
599 }
600
601 fprintf(outfile,
602 " default:\n"
603 " goto the_end;\n"
604 " }\n"
605 " }\n"
606 " the_end:\n"
607 );
608
609 /* generate a return */
610 switch(e_machine) {
611 case EM_386:
612 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
613 break;
614 case EM_PPC:
615 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
616 break;
617 default:
618 error("no return generation for cpu '%s'", cpu_name);
619 }
620
621 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
622 fprintf(outfile, "}\n\n");
623
624 /* generate gen_xxx functions */
625 /* XXX: suppress the use of these functions to simplify code */
626 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
627 const char *name;
628 name = strtab + sym->st_name;
629 if (strstart(name, OP_PREFIX, NULL)) {
630 if (sym->st_shndx != (text_sec - shdr))
631 error("invalid section for opcode (0x%x)", sym->st_shndx);
632 gen_code(name, sym->st_value, sym->st_size, outfile,
633 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 0);
634 }
635 }
636 }
637
638 close(fd);
639 return 0;
640 }
641
642 void usage(void)
643 {
644 printf("dyngen (c) 2003 Fabrice Bellard\n"
645 "usage: dyngen [-o outfile] [-c] objfile\n"
646 "Generate a dynamic code generator from an object file\n"
647 "-c output enum of operations\n"
648 );
649 exit(1);
650 }
651
652 int main(int argc, char **argv)
653 {
654 int c, do_print_enum;
655 const char *filename, *outfilename;
656 FILE *outfile;
657
658 outfilename = "out.c";
659 do_print_enum = 0;
660 for(;;) {
661 c = getopt(argc, argv, "ho:c");
662 if (c == -1)
663 break;
664 switch(c) {
665 case 'h':
666 usage();
667 break;
668 case 'o':
669 outfilename = optarg;
670 break;
671 case 'c':
672 do_print_enum = 1;
673 break;
674 }
675 }
676 if (optind >= argc)
677 usage();
678 filename = argv[optind];
679 outfile = fopen(outfilename, "w");
680 if (!outfile)
681 error("could not open '%s'", outfilename);
682 load_elf(filename, outfile, do_print_enum);
683 fclose(outfile);
684 return 0;
685 }