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