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 /* all dynamically generated functions begin with this code */
32 #define OP_PREFIX "op_"
34 int elf_must_swap(Elf32_Ehdr *h)
42 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
46 void swab16s(uint16_t *p)
51 void swab32s(uint32_t *p)
56 void swab64s(uint32_t *p)
61 void elf_swap_ehdr(Elf32_Ehdr *h)
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 */
78 void elf_swap_shdr(Elf32_Shdr *h)
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 */
92 void elf_swap_phdr(Elf32_Phdr *h)
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 */
107 uint16_t get16(uint16_t *p)
116 uint32_t get32(uint32_t *p)
125 void put16(uint16_t *p, uint16_t val)
132 void put32(uint32_t *p, uint32_t val)
139 void __attribute__((noreturn)) error(const char *fmt, ...)
143 fprintf(stderr, "dyngen: ");
144 vfprintf(stderr, fmt, ap);
145 fprintf(stderr, "\n");
151 Elf32_Shdr *find_elf_section(Elf32_Shdr *shdr, int shnum, const char *shstr,
158 for(i = 0; i < shnum; i++) {
162 shname = shstr + sec->sh_name;
163 if (!strcmp(shname, name))
169 void *load_data(int fd, long offset, unsigned int size)
176 lseek(fd, offset, SEEK_SET);
177 if (read(fd, data, size) != size) {
184 int strstart(const char *str, const char *val, const char **ptr)
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)
208 uint8_t *p_start, *p_end;
210 uint8_t args_present[MAX_ARGS];
211 const char *sym_name, *p;
213 /* compute exact size excluding return instruction */
214 p_start = text + offset;
215 p_end = p_start + size;
222 error("empty code for %s", name);
224 error("ret expected at the end of %s", name);
225 copy_size = p - p_start;
231 p = (void *)(p_end - 4);
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;
240 error("unsupported CPU (%d)", e_machine);
243 /* compute the number of arguments by looking at the relocations */
244 for(i = 0;i < MAX_ARGS; i++)
247 if (reloc_sh_type == SHT_REL) {
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);
256 error("too many arguments in %s", name);
257 args_present[n - 1] = 1;
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);
270 error("too many arguments in %s", name);
271 args_present[n - 1] = 1;
278 while (nb_args < MAX_ARGS && args_present[nb_args])
280 for(i = nb_args; i < MAX_ARGS; i++) {
282 error("inconsistent argument numbering in %s", name);
285 if (gen_switch == 2) {
286 fprintf(outfile, "DEF(%s, %d)\n", name + 3, nb_args);
287 } else if (gen_switch == 1) {
290 fprintf(outfile, "case INDEX_%s: {\n", name);
292 fprintf(outfile, " long ");
293 for(i = 0; i < nb_args; i++) {
295 fprintf(outfile, ", ");
296 fprintf(outfile, "param%d", i + 1);
298 fprintf(outfile, ";\n");
300 fprintf(outfile, " extern void %s();\n", name);
302 if (reloc_sh_type == SHT_REL) {
304 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
305 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
306 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
307 if (!strstart(sym_name, "__op_param", &p)) {
308 fprintf(outfile, "extern char %s;\n", sym_name);
314 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
315 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
316 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
317 if (!strstart(sym_name, "__op_param", &p)) {
318 fprintf(outfile, "extern char %s;\n", sym_name);
324 fprintf(outfile, " memcpy(gen_code_ptr, &%s, %d);\n", name, copy_size);
325 for(i = 0; i < nb_args; i++) {
326 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
329 /* patch relocations */
337 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
338 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
339 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
340 if (strstart(sym_name, "__op_param", &p)) {
341 snprintf(name, sizeof(name), "param%s", p);
343 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
345 type = ELF32_R_TYPE(rel->r_info);
346 addend = get32((uint32_t *)(text + rel->r_offset));
349 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
350 rel->r_offset - offset, name, addend);
353 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s - (long)(gen_code_ptr + %ld) + %ld;\n",
354 rel->r_offset - offset, name, rel->r_offset - offset, addend);
357 error("unsupported i386 relocation (%d)", type);
369 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
370 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
371 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
372 if (strstart(sym_name, "__op_param", &p)) {
373 snprintf(name, sizeof(name), "param%s", p);
375 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
377 type = ELF32_R_TYPE(rel->r_info);
378 addend = rel->r_addend;
381 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
382 rel->r_offset - offset, name, addend);
384 case R_PPC_ADDR16_LO:
385 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld);\n",
386 rel->r_offset - offset, name, addend);
388 case R_PPC_ADDR16_HI:
389 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld) >> 16;\n",
390 rel->r_offset - offset, name, addend);
392 case R_PPC_ADDR16_HA:
393 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld + 0x8000) >> 16;\n",
394 rel->r_offset - offset, name, addend);
397 /* warning: must be at 32 MB distancy */
398 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = (*(uint32_t *)(gen_code_ptr + %ld) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %ld) + %ld) & 0x03fffffc);\n",
399 rel->r_offset - offset, rel->r_offset - offset, name, rel->r_offset - offset, addend);
402 error("unsupported powerpc relocation (%d)", type);
409 error("unsupported CPU for relocations (%d)", e_machine);
411 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
412 fprintf(outfile, "}\n");
413 fprintf(outfile, "break;\n\n");
415 fprintf(outfile, "static inline void gen_%s(", name);
417 fprintf(outfile, "void");
419 for(i = 0; i < nb_args; i++) {
421 fprintf(outfile, ", ");
422 fprintf(outfile, "long param%d", i + 1);
425 fprintf(outfile, ")\n");
426 fprintf(outfile, "{\n");
427 for(i = 0; i < nb_args; i++) {
428 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
430 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
431 fprintf(outfile, "}\n\n");
435 /* load an elf object file */
436 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
440 Elf32_Shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
442 Elf32_Sym *symtab, *sym;
443 const char *cpu_name;
444 char *shstr, *strtab;
447 int nb_relocs, reloc_sh_type;
449 fd = open(filename, O_RDONLY);
451 error("can't open file '%s'", filename);
453 /* Read ELF header. */
454 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
455 error("unable to read file header");
457 /* Check ELF identification. */
458 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
459 || ehdr.e_ident[EI_MAG1] != ELFMAG1
460 || ehdr.e_ident[EI_MAG2] != ELFMAG2
461 || ehdr.e_ident[EI_MAG3] != ELFMAG3
462 || ehdr.e_ident[EI_CLASS] != ELFCLASS32
463 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
464 error("bad ELF header");
467 do_swap = elf_must_swap(&ehdr);
469 elf_swap_ehdr(&ehdr);
470 if (ehdr.e_type != ET_REL)
471 error("ELF object file expected");
472 if (ehdr.e_version != EV_CURRENT)
473 error("Invalid ELF version");
474 e_machine = ehdr.e_machine;
476 /* read section headers */
477 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(Elf32_Shdr));
479 for(i = 0; i < ehdr.e_shnum; i++) {
480 elf_swap_shdr(&shdr[i]);
484 sec = &shdr[ehdr.e_shstrndx];
485 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
489 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
491 error("could not find .text section");
492 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
494 /* find text relocations, if any */
498 for(i = 0; i < ehdr.e_shnum; i++) {
500 if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) &&
501 sec->sh_info == (text_sec - shdr)) {
502 reloc_sh_type = sec->sh_type;
503 relocs = load_data(fd, sec->sh_offset, sec->sh_size);
504 nb_relocs = sec->sh_size / sec->sh_entsize;
506 if (sec->sh_type == SHT_REL) {
507 Elf32_Rel *rel = relocs;
508 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
509 swab32s(&rel->r_offset);
510 swab32s(&rel->r_info);
513 Elf32_Rela *rel = relocs;
514 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
515 swab32s(&rel->r_offset);
516 swab32s(&rel->r_info);
517 swab32s(&rel->r_addend);
525 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
527 error("could not find .symtab section");
528 strtab_sec = &shdr[symtab_sec->sh_link];
530 symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size);
531 strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size);
533 nb_syms = symtab_sec->sh_size / sizeof(Elf32_Sym);
535 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
536 swab32s(&sym->st_name);
537 swab32s(&sym->st_value);
538 swab32s(&sym->st_size);
539 swab16s(&sym->st_shndx);
560 error("unsupported CPU (e_machine=%d)", e_machine);
564 fprintf(outfile, "DEF(end, 0)\n");
565 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
566 const char *name, *p;
567 name = strtab + sym->st_name;
568 if (strstart(name, OP_PREFIX, &p)) {
569 gen_code(name, sym->st_value, sym->st_size, outfile,
570 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 2);
574 /* generate big code generation switch */
576 "int dyngen_code(uint8_t *gen_code_buf,\n"
577 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
579 " uint8_t *gen_code_ptr;\n"
580 " const uint16_t *opc_ptr;\n"
581 " const uint32_t *opparam_ptr;\n"
582 " gen_code_ptr = gen_code_buf;\n"
583 " opc_ptr = opc_buf;\n"
584 " opparam_ptr = opparam_buf;\n"
586 " switch(*opc_ptr++) {\n"
589 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
591 name = strtab + sym->st_name;
592 if (strstart(name, OP_PREFIX, NULL)) {
594 printf("%4d: %s pos=0x%08x len=%d\n",
595 i, name, sym->st_value, sym->st_size);
597 if (sym->st_shndx != (text_sec - shdr))
598 error("invalid section for opcode (0x%x)", sym->st_shndx);
599 gen_code(name, sym->st_value, sym->st_size, outfile,
600 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 1);
612 /* generate a return */
615 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
618 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
621 error("no return generation for cpu '%s'", cpu_name);
624 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
625 fprintf(outfile, "}\n\n");
627 /* generate gen_xxx functions */
628 /* XXX: suppress the use of these functions to simplify code */
629 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
631 name = strtab + sym->st_name;
632 if (strstart(name, OP_PREFIX, NULL)) {
633 if (sym->st_shndx != (text_sec - shdr))
634 error("invalid section for opcode (0x%x)", sym->st_shndx);
635 gen_code(name, sym->st_value, sym->st_size, outfile,
636 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 0);
647 printf("dyngen (c) 2003 Fabrice Bellard\n"
648 "usage: dyngen [-o outfile] [-c] objfile\n"
649 "Generate a dynamic code generator from an object file\n"
650 "-c output enum of operations\n"
655 int main(int argc, char **argv)
657 int c, do_print_enum;
658 const char *filename, *outfilename;
661 outfilename = "out.c";
664 c = getopt(argc, argv, "ho:c");
672 outfilename = optarg;
681 filename = argv[optind];
682 outfile = fopen(outfilename, "w");
684 error("could not open '%s'", outfilename);
685 load_elf(filename, outfile, do_print_enum);