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 /* elf format definitions. We use these macros to test the CPU to
31 allow cross compilation (this tool must be ran on the build
33 #if defined(HOST_I386)
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
40 #elif defined(HOST_PPC)
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
47 #elif defined(HOST_S390)
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
54 #elif defined(HOST_ALPHA)
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
62 #error unsupported CPU - please update the code
65 #if ELF_CLASS == ELFCLASS32
66 typedef int32_t host_long;
67 typedef uint32_t host_ulong;
69 typedef int64_t host_long;
70 typedef uint64_t host_ulong;
77 /* all dynamically generated functions begin with this code */
78 #define OP_PREFIX "op_"
80 int elf_must_swap(struct elfhdr *h)
88 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
92 void swab16s(uint16_t *p)
97 void swab32s(uint32_t *p)
102 void swab64s(uint64_t *p)
107 #if ELF_CLASS == ELFCLASS32
108 #define swabls(x) swab32s(x)
110 #define swabls(x) swab64s(x)
113 void elf_swap_ehdr(struct elfhdr *h)
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 */
130 void elf_swap_shdr(struct elf_shdr *h)
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 */
144 void elf_swap_phdr(struct elf_phdr *h)
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 */
158 uint16_t get16(uint16_t *p)
167 uint32_t get32(uint32_t *p)
176 void put16(uint16_t *p, uint16_t val)
183 void put32(uint32_t *p, uint32_t val)
190 void __attribute__((noreturn)) error(const char *fmt, ...)
194 fprintf(stderr, "dyngen: ");
195 vfprintf(stderr, fmt, ap);
196 fprintf(stderr, "\n");
202 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
207 struct elf_shdr *sec;
209 for(i = 0; i < shnum; i++) {
213 shname = shstr + sec->sh_name;
214 if (!strcmp(shname, name))
220 void *load_data(int fd, long offset, unsigned int size)
227 lseek(fd, offset, SEEK_SET);
228 if (read(fd, data, size) != size) {
235 int strstart(const char *str, const char *val, const char **ptr)
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)
259 uint8_t *p_start, *p_end;
261 uint8_t args_present[MAX_ARGS];
262 const char *sym_name, *p;
265 /* compute exact size excluding return instruction */
266 p_start = text + offset;
267 p_end = p_start + size;
274 error("empty code for %s", name);
276 error("ret expected at the end of %s", name);
277 copy_size = p - p_start;
283 p = (void *)(p_end - 4);
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;
294 p = (void *)(p_end - 2);
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;
304 /* compute the number of arguments by looking at the relocations */
305 for(i = 0;i < MAX_ARGS; i++)
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);
314 error("too many arguments in %s", name);
315 args_present[n - 1] = 1;
321 while (nb_args < MAX_ARGS && args_present[nb_args])
323 for(i = nb_args; i < MAX_ARGS; i++) {
325 error("inconsistent argument numbering in %s", name);
328 if (gen_switch == 2) {
329 fprintf(outfile, "DEF(%s, %d)\n", name + 3, nb_args);
330 } else if (gen_switch == 1) {
333 fprintf(outfile, "case INDEX_%s: {\n", name);
335 fprintf(outfile, " long ");
336 for(i = 0; i < nb_args; i++) {
338 fprintf(outfile, ", ");
339 fprintf(outfile, "param%d", i + 1);
341 fprintf(outfile, ";\n");
343 fprintf(outfile, " extern void %s();\n", name);
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);
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);
359 /* patch relocations */
360 #if defined(HOST_I386)
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);
371 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
373 type = ELF32_R_TYPE(rel->r_info);
374 addend = get32((uint32_t *)(text + rel->r_offset));
377 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
378 rel->r_offset - offset, name, addend);
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);
385 error("unsupported i386 relocation (%d)", type);
390 #elif defined(HOST_PPC)
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);
401 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
403 type = ELF32_R_TYPE(rel->r_info);
404 addend = rel->r_addend;
407 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
408 rel->r_offset - offset, name, addend);
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);
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);
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);
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);
428 error("unsupported powerpc relocation (%d)", type);
433 #elif defined(HOST_S390)
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);
444 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
446 type = ELF32_R_TYPE(rel->r_info);
447 addend = rel->r_addend;
450 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
451 rel->r_offset - offset, name, addend);
454 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
455 rel->r_offset - offset, name, addend);
458 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
459 rel->r_offset - offset, name, addend);
462 error("unsupported s390 relocation (%d)", type);
468 #error unsupported CPU
470 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
471 fprintf(outfile, "}\n");
472 fprintf(outfile, "break;\n\n");
474 fprintf(outfile, "static inline void gen_%s(", name);
476 fprintf(outfile, "void");
478 for(i = 0; i < nb_args; i++) {
480 fprintf(outfile, ", ");
481 fprintf(outfile, "long param%d", i + 1);
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);
489 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
490 fprintf(outfile, "}\n\n");
494 /* load an elf object file */
495 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
499 struct elf_shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
501 ElfW(Sym) *symtab, *sym;
502 char *shstr, *strtab;
505 int nb_relocs, reloc_sh_type;
507 fd = open(filename, O_RDONLY);
509 error("can't open file '%s'", filename);
511 /* Read ELF header. */
512 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
513 error("unable to read file header");
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");
524 do_swap = elf_must_swap(&ehdr);
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);
536 /* read section headers */
537 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
539 for(i = 0; i < ehdr.e_shnum; i++) {
540 elf_swap_shdr(&shdr[i]);
544 sec = &shdr[ehdr.e_shstrndx];
545 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
549 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
551 error("could not find .text section");
552 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
554 /* find text relocations, if any */
558 for(i = 0; i < ehdr.e_shnum; 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;
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);
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);
585 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
587 error("could not find .symtab section");
588 strtab_sec = &shdr[symtab_sec->sh_link];
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);
593 nb_syms = symtab_sec->sh_size / sizeof(Elf32_Sym);
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);
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);
614 /* generate big code generation switch */
616 "int dyngen_code(uint8_t *gen_code_buf,\n"
617 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\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"
626 " switch(*opc_ptr++) {\n"
629 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
631 name = strtab + sym->st_name;
632 if (strstart(name, OP_PREFIX, NULL)) {
634 printf("%4d: %s pos=0x%08x len=%d\n",
635 i, name, sym->st_value, sym->st_size);
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);
652 /* generate a return */
655 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
658 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
661 fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
665 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
666 fprintf(outfile, "}\n\n");
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++) {
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);
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"
696 int main(int argc, char **argv)
698 int c, do_print_enum;
699 const char *filename, *outfilename;
702 outfilename = "out.c";
705 c = getopt(argc, argv, "ho:c");
713 outfilename = optarg;
722 filename = argv[optind];
723 outfile = fopen(outfilename, "w");
725 error("could not open '%s'", outfilename);
726 load_elf(filename, outfile, do_print_enum);