2 * Generic Dynamic compiler generator
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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.
31 /* temporary fix to make it compile with old elf headers (XXX: use
32 included elf.h in all cases) */
34 #define EM_S390 22 /* IBM S390 */
35 #define R_390_8 1 /* Direct 8 bit. */
36 #define R_390_16 3 /* Direct 16 bit. */
37 #define R_390_32 4 /* Direct 32 bit. */
40 /* all dynamically generated functions begin with this code */
41 #define OP_PREFIX "op_"
43 int elf_must_swap(Elf32_Ehdr *h)
51 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
55 void swab16s(uint16_t *p)
60 void swab32s(uint32_t *p)
65 void swab64s(uint32_t *p)
70 void elf_swap_ehdr(Elf32_Ehdr *h)
72 swab16s(&h->e_type); /* Object file type */
73 swab16s(&h-> e_machine); /* Architecture */
74 swab32s(&h-> e_version); /* Object file version */
75 swab32s(&h-> e_entry); /* Entry point virtual address */
76 swab32s(&h-> e_phoff); /* Program header table file offset */
77 swab32s(&h-> e_shoff); /* Section header table file offset */
78 swab32s(&h-> e_flags); /* Processor-specific flags */
79 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
80 swab16s(&h-> e_phentsize); /* Program header table entry size */
81 swab16s(&h-> e_phnum); /* Program header table entry count */
82 swab16s(&h-> e_shentsize); /* Section header table entry size */
83 swab16s(&h-> e_shnum); /* Section header table entry count */
84 swab16s(&h-> e_shstrndx); /* Section header string table index */
87 void elf_swap_shdr(Elf32_Shdr *h)
89 swab32s(&h-> sh_name); /* Section name (string tbl index) */
90 swab32s(&h-> sh_type); /* Section type */
91 swab32s(&h-> sh_flags); /* Section flags */
92 swab32s(&h-> sh_addr); /* Section virtual addr at execution */
93 swab32s(&h-> sh_offset); /* Section file offset */
94 swab32s(&h-> sh_size); /* Section size in bytes */
95 swab32s(&h-> sh_link); /* Link to another section */
96 swab32s(&h-> sh_info); /* Additional section information */
97 swab32s(&h-> sh_addralign); /* Section alignment */
98 swab32s(&h-> sh_entsize); /* Entry size if section holds table */
101 void elf_swap_phdr(Elf32_Phdr *h)
103 swab32s(&h->p_type); /* Segment type */
104 swab32s(&h->p_offset); /* Segment file offset */
105 swab32s(&h->p_vaddr); /* Segment virtual address */
106 swab32s(&h->p_paddr); /* Segment physical address */
107 swab32s(&h->p_filesz); /* Segment size in file */
108 swab32s(&h->p_memsz); /* Segment size in memory */
109 swab32s(&h->p_flags); /* Segment flags */
110 swab32s(&h->p_align); /* Segment alignment */
116 uint16_t get16(uint16_t *p)
125 uint32_t get32(uint32_t *p)
134 void put16(uint16_t *p, uint16_t val)
141 void put32(uint32_t *p, uint32_t val)
148 void __attribute__((noreturn)) error(const char *fmt, ...)
152 fprintf(stderr, "dyngen: ");
153 vfprintf(stderr, fmt, ap);
154 fprintf(stderr, "\n");
160 Elf32_Shdr *find_elf_section(Elf32_Shdr *shdr, int shnum, const char *shstr,
167 for(i = 0; i < shnum; i++) {
171 shname = shstr + sec->sh_name;
172 if (!strcmp(shname, name))
178 void *load_data(int fd, long offset, unsigned int size)
185 lseek(fd, offset, SEEK_SET);
186 if (read(fd, data, size) != size) {
193 int strstart(const char *str, const char *val, const char **ptr)
211 /* generate op code */
212 void gen_code(const char *name, unsigned long offset, unsigned long size,
213 FILE *outfile, uint8_t *text, void *relocs, int nb_relocs, int reloc_sh_type,
214 Elf32_Sym *symtab, char *strtab, int gen_switch)
217 uint8_t *p_start, *p_end;
219 uint8_t args_present[MAX_ARGS];
220 const char *sym_name, *p;
222 /* compute exact size excluding return instruction */
223 p_start = text + offset;
224 p_end = p_start + size;
231 error("empty code for %s", name);
233 error("ret expected at the end of %s", name);
234 copy_size = p - p_start;
240 p = (void *)(p_end - 4);
242 error("empty code for %s", name);
243 if (get32((uint32_t *)p) != 0x4e800020)
244 error("blr expected at the end of %s", name);
245 copy_size = p - p_start;
251 p = (void *)(p_end - 2);
253 error("empty code for %s", name);
254 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
255 error("br %r14 expected at the end of %s", name);
256 copy_size = p - p_start;
260 error("unsupported CPU (%d)", e_machine);
263 /* compute the number of arguments by looking at the relocations */
264 for(i = 0;i < MAX_ARGS; i++)
267 if (reloc_sh_type == SHT_REL) {
270 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
271 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
272 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
273 if (strstart(sym_name, "__op_param", &p)) {
274 n = strtoul(p, NULL, 10);
276 error("too many arguments in %s", name);
277 args_present[n - 1] = 1;
284 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
285 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
286 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
287 if (strstart(sym_name, "__op_param", &p)) {
288 n = strtoul(p, NULL, 10);
290 error("too many arguments in %s", name);
291 args_present[n - 1] = 1;
298 while (nb_args < MAX_ARGS && args_present[nb_args])
300 for(i = nb_args; i < MAX_ARGS; i++) {
302 error("inconsistent argument numbering in %s", name);
305 if (gen_switch == 2) {
306 fprintf(outfile, "DEF(%s, %d)\n", name + 3, nb_args);
307 } else if (gen_switch == 1) {
310 fprintf(outfile, "case INDEX_%s: {\n", name);
312 fprintf(outfile, " long ");
313 for(i = 0; i < nb_args; i++) {
315 fprintf(outfile, ", ");
316 fprintf(outfile, "param%d", i + 1);
318 fprintf(outfile, ";\n");
320 fprintf(outfile, " extern void %s();\n", name);
322 if (reloc_sh_type == SHT_REL) {
324 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
325 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
326 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
327 if (!strstart(sym_name, "__op_param", &p)) {
328 fprintf(outfile, "extern char %s;\n", sym_name);
334 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
335 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
336 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
337 if (!strstart(sym_name, "__op_param", &p)) {
338 fprintf(outfile, "extern char %s;\n", sym_name);
344 fprintf(outfile, " memcpy(gen_code_ptr, &%s, %d);\n", name, copy_size);
345 for(i = 0; i < nb_args; i++) {
346 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
349 /* patch relocations */
357 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
358 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
359 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
360 if (strstart(sym_name, "__op_param", &p)) {
361 snprintf(name, sizeof(name), "param%s", p);
363 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
365 type = ELF32_R_TYPE(rel->r_info);
366 addend = get32((uint32_t *)(text + rel->r_offset));
369 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
370 rel->r_offset - offset, name, addend);
373 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s - (long)(gen_code_ptr + %ld) + %ld;\n",
374 rel->r_offset - offset, name, rel->r_offset - offset, addend);
377 error("unsupported i386 relocation (%d)", type);
389 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
390 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
391 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
392 if (strstart(sym_name, "__op_param", &p)) {
393 snprintf(name, sizeof(name), "param%s", p);
395 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
397 type = ELF32_R_TYPE(rel->r_info);
398 addend = rel->r_addend;
401 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
402 rel->r_offset - offset, name, addend);
404 case R_PPC_ADDR16_LO:
405 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld);\n",
406 rel->r_offset - offset, name, addend);
408 case R_PPC_ADDR16_HI:
409 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld) >> 16;\n",
410 rel->r_offset - offset, name, addend);
412 case R_PPC_ADDR16_HA:
413 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld + 0x8000) >> 16;\n",
414 rel->r_offset - offset, name, addend);
417 /* warning: must be at 32 MB distancy */
418 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = (*(uint32_t *)(gen_code_ptr + %ld) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %ld) + %ld) & 0x03fffffc);\n",
419 rel->r_offset - offset, rel->r_offset - offset, name, rel->r_offset - offset, addend);
422 error("unsupported powerpc relocation (%d)", type);
434 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
435 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
436 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
437 if (strstart(sym_name, "__op_param", &p)) {
438 snprintf(name, sizeof(name), "param%s", p);
440 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
442 type = ELF32_R_TYPE(rel->r_info);
443 addend = rel->r_addend;
446 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
447 rel->r_offset - offset, name, addend);
450 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
451 rel->r_offset - offset, name, addend);
454 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
455 rel->r_offset - offset, name, addend);
458 error("unsupported s390 relocation (%d)", type);
465 error("unsupported CPU for relocations (%d)", e_machine);
467 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
468 fprintf(outfile, "}\n");
469 fprintf(outfile, "break;\n\n");
471 fprintf(outfile, "static inline void gen_%s(", name);
473 fprintf(outfile, "void");
475 for(i = 0; i < nb_args; i++) {
477 fprintf(outfile, ", ");
478 fprintf(outfile, "long param%d", i + 1);
481 fprintf(outfile, ")\n");
482 fprintf(outfile, "{\n");
483 for(i = 0; i < nb_args; i++) {
484 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
486 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
487 fprintf(outfile, "}\n\n");
491 /* load an elf object file */
492 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
496 Elf32_Shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
498 Elf32_Sym *symtab, *sym;
499 const char *cpu_name;
500 char *shstr, *strtab;
503 int nb_relocs, reloc_sh_type;
505 fd = open(filename, O_RDONLY);
507 error("can't open file '%s'", filename);
509 /* Read ELF header. */
510 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
511 error("unable to read file header");
513 /* Check ELF identification. */
514 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
515 || ehdr.e_ident[EI_MAG1] != ELFMAG1
516 || ehdr.e_ident[EI_MAG2] != ELFMAG2
517 || ehdr.e_ident[EI_MAG3] != ELFMAG3
518 || ehdr.e_ident[EI_CLASS] != ELFCLASS32
519 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
520 error("bad ELF header");
523 do_swap = elf_must_swap(&ehdr);
525 elf_swap_ehdr(&ehdr);
526 if (ehdr.e_type != ET_REL)
527 error("ELF object file expected");
528 if (ehdr.e_version != EV_CURRENT)
529 error("Invalid ELF version");
530 e_machine = ehdr.e_machine;
532 /* read section headers */
533 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(Elf32_Shdr));
535 for(i = 0; i < ehdr.e_shnum; i++) {
536 elf_swap_shdr(&shdr[i]);
540 sec = &shdr[ehdr.e_shstrndx];
541 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
545 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
547 error("could not find .text section");
548 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
550 /* find text relocations, if any */
554 for(i = 0; i < ehdr.e_shnum; i++) {
556 if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) &&
557 sec->sh_info == (text_sec - shdr)) {
558 reloc_sh_type = sec->sh_type;
559 relocs = load_data(fd, sec->sh_offset, sec->sh_size);
560 nb_relocs = sec->sh_size / sec->sh_entsize;
562 if (sec->sh_type == SHT_REL) {
563 Elf32_Rel *rel = relocs;
564 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
565 swab32s(&rel->r_offset);
566 swab32s(&rel->r_info);
569 Elf32_Rela *rel = relocs;
570 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
571 swab32s(&rel->r_offset);
572 swab32s(&rel->r_info);
573 swab32s(&rel->r_addend);
581 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
583 error("could not find .symtab section");
584 strtab_sec = &shdr[symtab_sec->sh_link];
586 symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size);
587 strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size);
589 nb_syms = symtab_sec->sh_size / sizeof(Elf32_Sym);
591 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
592 swab32s(&sym->st_name);
593 swab32s(&sym->st_value);
594 swab32s(&sym->st_size);
595 swab16s(&sym->st_shndx);
619 error("unsupported CPU (e_machine=%d)", e_machine);
623 fprintf(outfile, "DEF(end, 0)\n");
624 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
625 const char *name, *p;
626 name = strtab + sym->st_name;
627 if (strstart(name, OP_PREFIX, &p)) {
628 gen_code(name, sym->st_value, sym->st_size, outfile,
629 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 2);
633 /* generate big code generation switch */
635 "int dyngen_code(uint8_t *gen_code_buf,\n"
636 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
638 " uint8_t *gen_code_ptr;\n"
639 " const uint16_t *opc_ptr;\n"
640 " const uint32_t *opparam_ptr;\n"
641 " gen_code_ptr = gen_code_buf;\n"
642 " opc_ptr = opc_buf;\n"
643 " opparam_ptr = opparam_buf;\n"
645 " switch(*opc_ptr++) {\n"
648 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
650 name = strtab + sym->st_name;
651 if (strstart(name, OP_PREFIX, NULL)) {
653 printf("%4d: %s pos=0x%08x len=%d\n",
654 i, name, sym->st_value, sym->st_size);
656 if (sym->st_shndx != (text_sec - shdr))
657 error("invalid section for opcode (0x%x)", sym->st_shndx);
658 gen_code(name, sym->st_value, sym->st_size, outfile,
659 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 1);
671 /* generate a return */
674 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
677 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
680 fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
683 error("no return generation for cpu '%s'", cpu_name);
686 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
687 fprintf(outfile, "}\n\n");
689 /* generate gen_xxx functions */
690 /* XXX: suppress the use of these functions to simplify code */
691 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
693 name = strtab + sym->st_name;
694 if (strstart(name, OP_PREFIX, NULL)) {
695 if (sym->st_shndx != (text_sec - shdr))
696 error("invalid section for opcode (0x%x)", sym->st_shndx);
697 gen_code(name, sym->st_value, sym->st_size, outfile,
698 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 0);
709 printf("dyngen (c) 2003 Fabrice Bellard\n"
710 "usage: dyngen [-o outfile] [-c] objfile\n"
711 "Generate a dynamic code generator from an object file\n"
712 "-c output enum of operations\n"
717 int main(int argc, char **argv)
719 int c, do_print_enum;
720 const char *filename, *outfilename;
723 outfilename = "out.c";
726 c = getopt(argc, argv, "ho:c");
734 outfilename = optarg;
743 filename = argv[optind];
744 outfile = fopen(outfilename, "w");
746 error("could not open '%s'", outfilename);
747 load_elf(filename, outfile, do_print_enum);