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);
288 fprintf(outfile, "case INDEX_%s: {\n", name);
290 fprintf(outfile, " long ");
291 for(i = 0; i < nb_args; i++) {
293 fprintf(outfile, ", ");
294 fprintf(outfile, "param%d", i + 1);
296 fprintf(outfile, ";\n");
298 fprintf(outfile, " extern void %s();\n", name);
300 if (reloc_sh_type == SHT_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);
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);
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);
327 /* patch relocations */
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);
341 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
343 type = ELF32_R_TYPE(rel->r_info);
344 addend = get32((uint32_t *)(text + rel->r_offset));
347 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
348 rel->r_offset - offset, name, addend);
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);
355 error("unsupported i386 relocation (%d)", type);
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);
373 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
375 type = ELF32_R_TYPE(rel->r_info);
376 addend = rel->r_addend;
379 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
380 rel->r_offset - offset, name, addend);
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);
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);
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);
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);
400 error("unsupported powerpc relocation (%d)", type);
407 error("unsupported CPU for relocations (%d)", e_machine);
409 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
410 fprintf(outfile, "}\n");
411 fprintf(outfile, "break;\n\n");
413 fprintf(outfile, "static inline void gen_%s(", name);
415 fprintf(outfile, "void");
417 for(i = 0; i < nb_args; i++) {
419 fprintf(outfile, ", ");
420 fprintf(outfile, "long param%d", i + 1);
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);
428 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
429 fprintf(outfile, "}\n\n");
433 /* load an elf object file */
434 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
438 Elf32_Shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
440 Elf32_Sym *symtab, *sym;
441 const char *cpu_name;
442 char *shstr, *strtab;
445 int nb_relocs, reloc_sh_type;
447 fd = open(filename, O_RDONLY);
449 error("can't open file '%s'", filename);
451 /* Read ELF header. */
452 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
453 error("unable to read file header");
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");
465 do_swap = elf_must_swap(&ehdr);
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;
474 /* read section headers */
475 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(Elf32_Shdr));
477 for(i = 0; i < ehdr.e_shnum; i++) {
478 elf_swap_shdr(&shdr[i]);
482 sec = &shdr[ehdr.e_shstrndx];
483 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
487 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
489 error("could not find .text section");
490 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
492 /* find text relocations, if any */
496 for(i = 0; i < ehdr.e_shnum; 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;
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);
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);
523 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
525 error("could not find .symtab section");
526 strtab_sec = &shdr[symtab_sec->sh_link];
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);
531 nb_syms = symtab_sec->sh_size / sizeof(Elf32_Sym);
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);
558 error("unsupported CPU (e_machine=%d)", e_machine);
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);
571 /* generate big code generation switch */
573 "int dyngen_code(uint8_t *gen_code_buf,\n"
574 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\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"
583 " switch(*opc_ptr++) {\n"
586 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
588 name = strtab + sym->st_name;
589 if (strstart(name, OP_PREFIX, NULL)) {
591 printf("%4d: %s pos=0x%08x len=%d\n",
592 i, name, sym->st_value, sym->st_size);
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);
609 /* generate a return */
612 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
615 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
618 error("no return generation for cpu '%s'", cpu_name);
621 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
622 fprintf(outfile, "}\n\n");
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++) {
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);
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"
652 int main(int argc, char **argv)
654 int c, do_print_enum;
655 const char *filename, *outfilename;
658 outfilename = "out.c";
661 c = getopt(argc, argv, "ho:c");
669 outfilename = optarg;
678 filename = argv[optind];
679 outfile = fopen(outfilename, "w");
681 error("could not open '%s'", outfilename);
682 load_elf(filename, outfile, do_print_enum);