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.
30 /* all dynamically generated functions begin with this code */
31 #define OP_PREFIX "op_"
33 int elf_must_swap(Elf32_Ehdr *h)
41 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
45 void swab16s(uint16_t *p)
50 void swab32s(uint32_t *p)
55 void swab64s(uint32_t *p)
60 void elf_swap_ehdr(Elf32_Ehdr *h)
62 swab16s(&h->e_type); /* Object file type */
63 swab16s(&h-> e_machine); /* Architecture */
64 swab32s(&h-> e_version); /* Object file version */
65 swab32s(&h-> e_entry); /* Entry point virtual address */
66 swab32s(&h-> e_phoff); /* Program header table file offset */
67 swab32s(&h-> e_shoff); /* Section header table file offset */
68 swab32s(&h-> e_flags); /* Processor-specific flags */
69 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
70 swab16s(&h-> e_phentsize); /* Program header table entry size */
71 swab16s(&h-> e_phnum); /* Program header table entry count */
72 swab16s(&h-> e_shentsize); /* Section header table entry size */
73 swab16s(&h-> e_shnum); /* Section header table entry count */
74 swab16s(&h-> e_shstrndx); /* Section header string table index */
77 void elf_swap_shdr(Elf32_Shdr *h)
79 swab32s(&h-> sh_name); /* Section name (string tbl index) */
80 swab32s(&h-> sh_type); /* Section type */
81 swab32s(&h-> sh_flags); /* Section flags */
82 swab32s(&h-> sh_addr); /* Section virtual addr at execution */
83 swab32s(&h-> sh_offset); /* Section file offset */
84 swab32s(&h-> sh_size); /* Section size in bytes */
85 swab32s(&h-> sh_link); /* Link to another section */
86 swab32s(&h-> sh_info); /* Additional section information */
87 swab32s(&h-> sh_addralign); /* Section alignment */
88 swab32s(&h-> sh_entsize); /* Entry size if section holds table */
91 void elf_swap_phdr(Elf32_Phdr *h)
93 swab32s(&h->p_type); /* Segment type */
94 swab32s(&h->p_offset); /* Segment file offset */
95 swab32s(&h->p_vaddr); /* Segment virtual address */
96 swab32s(&h->p_paddr); /* Segment physical address */
97 swab32s(&h->p_filesz); /* Segment size in file */
98 swab32s(&h->p_memsz); /* Segment size in memory */
99 swab32s(&h->p_flags); /* Segment flags */
100 swab32s(&h->p_align); /* Segment alignment */
106 uint16_t get16(uint16_t *p)
115 uint32_t get32(uint32_t *p)
124 void put16(uint16_t *p, uint16_t val)
131 void put32(uint32_t *p, uint32_t val)
138 void __attribute__((noreturn)) error(const char *fmt, ...)
142 fprintf(stderr, "dyngen: ");
143 vfprintf(stderr, fmt, ap);
144 fprintf(stderr, "\n");
150 Elf32_Shdr *find_elf_section(Elf32_Shdr *shdr, int shnum, const char *shstr,
157 for(i = 0; i < shnum; i++) {
161 shname = shstr + sec->sh_name;
162 if (!strcmp(shname, name))
168 void *load_data(int fd, long offset, unsigned int size)
175 lseek(fd, offset, SEEK_SET);
176 if (read(fd, data, size) != size) {
183 int strstart(const char *str, const char *val, const char **ptr)
201 /* generate op code */
202 void gen_code(const char *name, unsigned long offset, unsigned long size,
203 FILE *outfile, uint8_t *text, void *relocs, int nb_relocs, int reloc_sh_type,
204 Elf32_Sym *symtab, char *strtab, int gen_switch)
207 uint8_t *p_start, *p_end;
209 uint8_t args_present[MAX_ARGS];
210 const char *sym_name, *p;
212 /* compute exact size excluding return instruction */
213 p_start = text + offset;
214 p_end = p_start + size;
221 error("empty code for %s", name);
223 error("ret expected at the end of %s", name);
224 copy_size = p - p_start;
230 p = (void *)(p_end - 4);
232 while (p > p_start && get32((uint32_t *)p) != 0x4e800020)
234 /* skip double ret */
235 if (p > p_start && get32((uint32_t *)(p - 4)) == 0x4e800020)
238 error("empty code for %s", name);
239 copy_size = p - p_start;
243 error("unsupported CPU (%d)", e_machine);
246 /* compute the number of arguments by looking at the relocations */
247 for(i = 0;i < MAX_ARGS; i++)
250 if (reloc_sh_type == SHT_REL) {
253 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
254 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
255 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
256 if (strstart(sym_name, "__op_param", &p)) {
257 n = strtoul(p, NULL, 10);
259 error("too many arguments in %s", name);
260 args_present[n - 1] = 1;
267 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
268 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
269 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
270 if (strstart(sym_name, "__op_param", &p)) {
271 n = strtoul(p, NULL, 10);
273 error("too many arguments in %s", name);
274 args_present[n - 1] = 1;
281 while (nb_args < MAX_ARGS && args_present[nb_args])
283 for(i = nb_args; i < MAX_ARGS; i++) {
285 error("inconsistent argument numbering in %s", name);
291 fprintf(outfile, "case INDEX_%s: {\n", name);
293 fprintf(outfile, " long ");
294 for(i = 0; i < nb_args; i++) {
296 fprintf(outfile, ", ");
297 fprintf(outfile, "param%d", i + 1);
299 fprintf(outfile, ";\n");
301 fprintf(outfile, " extern void %s();\n", name);
303 if (reloc_sh_type == SHT_REL) {
305 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
306 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
307 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
308 if (!strstart(sym_name, "__op_param", &p)) {
309 fprintf(outfile, "extern char %s;\n", sym_name);
315 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
316 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
317 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
318 if (!strstart(sym_name, "__op_param", &p)) {
319 fprintf(outfile, "extern char %s;\n", sym_name);
325 fprintf(outfile, " memcpy(gen_code_ptr, &%s, %d);\n", name, copy_size);
326 for(i = 0; i < nb_args; i++) {
327 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
330 /* patch relocations */
338 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
339 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
340 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
341 if (strstart(sym_name, "__op_param", &p)) {
342 snprintf(name, sizeof(name), "param%s", p);
344 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
346 type = ELF32_R_TYPE(rel->r_info);
347 addend = get32((uint32_t *)(text + rel->r_offset));
350 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
351 rel->r_offset - offset, name, addend);
354 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s - (long)(gen_code_ptr + %ld) + %ld;\n",
355 rel->r_offset - offset, name, rel->r_offset - offset, addend);
358 error("unsupported i386 relocation (%d)", type);
365 error("unsupported CPU for relocations (%d)", e_machine);
367 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
368 fprintf(outfile, "}\n");
369 fprintf(outfile, "break;\n\n");
371 fprintf(outfile, "static inline void gen_%s(", name);
373 fprintf(outfile, "void");
375 for(i = 0; i < nb_args; i++) {
377 fprintf(outfile, ", ");
378 fprintf(outfile, "long param%d", i + 1);
381 fprintf(outfile, ")\n");
382 fprintf(outfile, "{\n");
383 for(i = 0; i < nb_args; i++) {
384 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
386 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
387 fprintf(outfile, "}\n\n");
391 /* load an elf object file */
392 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
396 Elf32_Shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
398 Elf32_Sym *symtab, *sym;
399 const char *cpu_name;
400 char *shstr, *strtab;
403 int nb_relocs, reloc_sh_type;
405 fd = open(filename, O_RDONLY);
407 error("can't open file '%s'", filename);
409 /* Read ELF header. */
410 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
411 error("unable to read file header");
413 /* Check ELF identification. */
414 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
415 || ehdr.e_ident[EI_MAG1] != ELFMAG1
416 || ehdr.e_ident[EI_MAG2] != ELFMAG2
417 || ehdr.e_ident[EI_MAG3] != ELFMAG3
418 || ehdr.e_ident[EI_CLASS] != ELFCLASS32
419 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
420 error("bad ELF header");
423 do_swap = elf_must_swap(&ehdr);
425 elf_swap_ehdr(&ehdr);
426 if (ehdr.e_type != ET_REL)
427 error("ELF object file expected");
428 if (ehdr.e_version != EV_CURRENT)
429 error("Invalid ELF version");
430 e_machine = ehdr.e_machine;
432 /* read section headers */
433 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(Elf32_Shdr));
435 for(i = 0; i < ehdr.e_shnum; i++) {
436 elf_swap_shdr(&shdr[i]);
440 sec = &shdr[ehdr.e_shstrndx];
441 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
445 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
447 error("could not find .text section");
448 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
450 /* find text relocations, if any */
454 for(i = 0; i < ehdr.e_shnum; i++) {
456 if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) &&
457 sec->sh_info == (text_sec - shdr)) {
458 reloc_sh_type = sec->sh_type;
459 relocs = load_data(fd, sec->sh_offset, sec->sh_size);
460 nb_relocs = sec->sh_size / sec->sh_entsize;
462 if (sec->sh_type == SHT_REL) {
463 Elf32_Rel *rel = relocs;
464 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
465 swab32s(&rel->r_offset);
466 swab32s(&rel->r_info);
469 Elf32_Rela *rel = relocs;
470 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
471 swab32s(&rel->r_offset);
472 swab32s(&rel->r_info);
473 swab32s(&rel->r_addend);
481 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
483 error("could not find .symtab section");
484 strtab_sec = &shdr[symtab_sec->sh_link];
486 symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size);
487 strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size);
489 nb_syms = symtab_sec->sh_size / sizeof(Elf32_Sym);
491 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
492 swab32s(&sym->st_name);
493 swab32s(&sym->st_value);
494 swab32s(&sym->st_size);
495 swab16s(&sym->st_shndx);
516 error("unsupported CPU (e_machine=%d)", e_machine);
520 fprintf(outfile, "DEF(end)\n");
521 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
522 const char *name, *p;
523 name = strtab + sym->st_name;
524 if (strstart(name, OP_PREFIX, &p)) {
525 fprintf(outfile, "DEF(%s)\n", p);
529 /* generate big code generation switch */
531 "int dyngen_code(uint8_t *gen_code_buf,\n"
532 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
534 " uint8_t *gen_code_ptr;\n"
535 " const uint16_t *opc_ptr;\n"
536 " const uint32_t *opparam_ptr;\n"
537 " gen_code_ptr = gen_code_buf;\n"
538 " opc_ptr = opc_buf;\n"
539 " opparam_ptr = opparam_buf;\n"
541 " switch(*opc_ptr++) {\n"
544 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
546 name = strtab + sym->st_name;
547 if (strstart(name, OP_PREFIX, NULL)) {
549 printf("%4d: %s pos=0x%08x len=%d\n",
550 i, name, sym->st_value, sym->st_size);
552 if (sym->st_shndx != (text_sec - shdr))
553 error("invalid section for opcode (0x%x)", sym->st_shndx);
554 gen_code(name, sym->st_value, sym->st_size, outfile,
555 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 1);
567 /* generate a return */
570 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
573 error("no return generation for cpu '%s'", cpu_name);
576 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
577 fprintf(outfile, "}\n\n");
579 /* generate gen_xxx functions */
580 /* XXX: suppress the use of these functions to simplify code */
581 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
583 name = strtab + sym->st_name;
584 if (strstart(name, OP_PREFIX, NULL)) {
585 if (sym->st_shndx != (text_sec - shdr))
586 error("invalid section for opcode (0x%x)", sym->st_shndx);
587 gen_code(name, sym->st_value, sym->st_size, outfile,
588 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 0);
599 printf("dyngen (c) 2003 Fabrice Bellard\n"
600 "usage: dyngen [-o outfile] [-c] objfile\n"
601 "Generate a dynamic code generator from an object file\n"
602 "-c output enum of operations\n"
607 int main(int argc, char **argv)
609 int c, do_print_enum;
610 const char *filename, *outfilename;
613 outfilename = "out.c";
616 c = getopt(argc, argv, "ho:c");
624 outfilename = optarg;
633 filename = argv[optind];
634 outfile = fopen(outfilename, "w");
636 error("could not open '%s'", outfilename);
637 load_elf(filename, outfile, do_print_enum);