11 /* all dynamically generated functions begin with this code */
12 #define OP_PREFIX "op"
14 int elf_must_swap(Elf32_Ehdr *h)
22 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
26 void swab16s(uint16_t *p)
31 void swab32s(uint32_t *p)
36 void swab64s(uint32_t *p)
41 void elf_swap_ehdr(Elf32_Ehdr *h)
43 swab16s(&h->e_type); /* Object file type */
44 swab16s(&h-> e_machine); /* Architecture */
45 swab32s(&h-> e_version); /* Object file version */
46 swab32s(&h-> e_entry); /* Entry point virtual address */
47 swab32s(&h-> e_phoff); /* Program header table file offset */
48 swab32s(&h-> e_shoff); /* Section header table file offset */
49 swab32s(&h-> e_flags); /* Processor-specific flags */
50 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
51 swab16s(&h-> e_phentsize); /* Program header table entry size */
52 swab16s(&h-> e_phnum); /* Program header table entry count */
53 swab16s(&h-> e_shentsize); /* Section header table entry size */
54 swab16s(&h-> e_shnum); /* Section header table entry count */
55 swab16s(&h-> e_shstrndx); /* Section header string table index */
58 void elf_swap_shdr(Elf32_Shdr *h)
60 swab32s(&h-> sh_name); /* Section name (string tbl index) */
61 swab32s(&h-> sh_type); /* Section type */
62 swab32s(&h-> sh_flags); /* Section flags */
63 swab32s(&h-> sh_addr); /* Section virtual addr at execution */
64 swab32s(&h-> sh_offset); /* Section file offset */
65 swab32s(&h-> sh_size); /* Section size in bytes */
66 swab32s(&h-> sh_link); /* Link to another section */
67 swab32s(&h-> sh_info); /* Additional section information */
68 swab32s(&h-> sh_addralign); /* Section alignment */
69 swab32s(&h-> sh_entsize); /* Entry size if section holds table */
72 void elf_swap_phdr(Elf32_Phdr *h)
74 swab32s(&h->p_type); /* Segment type */
75 swab32s(&h->p_offset); /* Segment file offset */
76 swab32s(&h->p_vaddr); /* Segment virtual address */
77 swab32s(&h->p_paddr); /* Segment physical address */
78 swab32s(&h->p_filesz); /* Segment size in file */
79 swab32s(&h->p_memsz); /* Segment size in memory */
80 swab32s(&h->p_flags); /* Segment flags */
81 swab32s(&h->p_align); /* Segment alignment */
87 uint16_t get16(uint16_t *p)
96 uint32_t get32(uint32_t *p)
105 void put16(uint16_t *p, uint16_t val)
112 void put32(uint32_t *p, uint32_t val)
119 void __attribute__((noreturn)) error(const char *fmt, ...)
123 fprintf(stderr, "dyngen: ");
124 vfprintf(stderr, fmt, ap);
125 fprintf(stderr, "\n");
131 Elf32_Shdr *find_elf_section(Elf32_Shdr *shdr, int shnum, const char *shstr,
138 for(i = 0; i < shnum; i++) {
142 shname = shstr + sec->sh_name;
143 if (!strcmp(shname, name))
149 void *load_data(int fd, long offset, unsigned int size)
156 lseek(fd, offset, SEEK_SET);
157 if (read(fd, data, size) != size) {
164 int strstart(const char *str, const char *val, const char **ptr)
182 /* generate op code */
183 void gen_code(const char *name, unsigned long offset, unsigned long size,
184 FILE *outfile, uint8_t *text, void *relocs, int nb_relocs, int reloc_sh_type,
185 Elf32_Sym *symtab, char *strtab)
188 uint8_t *p_start, *p_end;
190 uint8_t args_present[MAX_ARGS];
191 const char *sym_name, *p;
193 /* compute exact size excluding return instruction */
194 p_start = text + offset;
195 p_end = p_start + size;
202 while (p > p_start && *p != 0xc3)
204 /* skip double ret */
205 if (p > p_start && p[-1] == 0xc3)
208 error("empty code for %s", name);
209 copy_size = p - p_start;
215 p = (void *)(p_end - 4);
217 while (p > p_start && get32((uint32_t *)p) != 0x4e800020)
219 /* skip double ret */
220 if (p > p_start && get32((uint32_t *)(p - 4)) == 0x4e800020)
223 error("empty code for %s", name);
224 copy_size = p - p_start;
228 error("unsupported CPU (%d)", e_machine);
231 /* compute the number of arguments by looking at the relocations */
232 for(i = 0;i < MAX_ARGS; i++)
235 if (reloc_sh_type == SHT_REL) {
238 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
239 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
240 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
241 if (strstart(sym_name, "__op_param", &p)) {
242 n = strtoul(p, NULL, 10);
244 error("too many arguments in %s", name);
245 args_present[n - 1] = 1;
247 fprintf(outfile, "extern char %s;\n", sym_name);
254 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
255 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
256 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
257 if (strstart(sym_name, "__op_param", &p)) {
258 n = strtoul(p, NULL, 10);
260 error("too many arguments in %s", name);
261 args_present[n - 1] = 1;
263 fprintf(outfile, "extern char %s;\n", sym_name);
270 while (nb_args < MAX_ARGS && args_present[nb_args])
272 for(i = nb_args; i < MAX_ARGS; i++) {
274 error("inconsistent argument numbering in %s", name);
278 fprintf(outfile, "extern void %s();\n", name);
279 fprintf(outfile, "static inline void gen_%s(", name);
281 fprintf(outfile, "void");
283 for(i = 0; i < nb_args; i++) {
285 fprintf(outfile, ", ");
286 fprintf(outfile, "long param%d", i + 1);
289 fprintf(outfile, ")\n");
290 fprintf(outfile, "{\n");
291 fprintf(outfile, " memcpy(gen_code_ptr, &%s, %d);\n", name, copy_size);
293 /* patch relocations */
301 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
302 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
303 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
304 if (strstart(sym_name, "__op_param", &p)) {
305 snprintf(name, sizeof(name), "param%s", p);
307 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
309 type = ELF32_R_TYPE(rel->r_info);
310 addend = get32((uint32_t *)(text + rel->r_offset));
313 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n",
314 rel->r_offset - offset, name, addend);
317 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s - (long)(gen_code_ptr + %ld) + %ld;\n",
318 rel->r_offset - offset, name, rel->r_offset - offset, addend);
321 error("unsupported i386 relocation (%d)", type);
328 error("unsupported CPU for relocations (%d)", e_machine);
332 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
333 fprintf(outfile, "}\n\n");
336 /* load an elf object file */
337 int load_elf(const char *filename, FILE *outfile)
341 Elf32_Shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
343 Elf32_Sym *symtab, *sym;
344 const char *cpu_name;
345 char *shstr, *strtab;
348 int nb_relocs, reloc_sh_type;
350 fd = open(filename, O_RDONLY);
352 error("can't open file '%s'", filename);
354 /* Read ELF header. */
355 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
356 error("unable to read file header");
358 /* Check ELF identification. */
359 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
360 || ehdr.e_ident[EI_MAG1] != ELFMAG1
361 || ehdr.e_ident[EI_MAG2] != ELFMAG2
362 || ehdr.e_ident[EI_MAG3] != ELFMAG3
363 || ehdr.e_ident[EI_CLASS] != ELFCLASS32
364 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
365 error("bad ELF header");
368 do_swap = elf_must_swap(&ehdr);
370 elf_swap_ehdr(&ehdr);
371 if (ehdr.e_type != ET_REL)
372 error("ELF object file expected");
373 if (ehdr.e_version != EV_CURRENT)
374 error("Invalid ELF version");
375 e_machine = ehdr.e_machine;
377 /* read section headers */
378 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(Elf32_Shdr));
380 for(i = 0; i < ehdr.e_shnum; i++) {
381 elf_swap_shdr(&shdr[i]);
385 sec = &shdr[ehdr.e_shstrndx];
386 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
390 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
392 error("could not find .text section");
393 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
395 /* find text relocations, if any */
399 for(i = 0; i < ehdr.e_shnum; i++) {
401 if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) &&
402 sec->sh_info == (text_sec - shdr)) {
403 reloc_sh_type = sec->sh_type;
404 relocs = load_data(fd, sec->sh_offset, sec->sh_size);
405 nb_relocs = sec->sh_size / sec->sh_entsize;
407 if (sec->sh_type == SHT_REL) {
408 Elf32_Rel *rel = relocs;
409 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
410 swab32s(&rel->r_offset);
411 swab32s(&rel->r_info);
414 Elf32_Rela *rel = relocs;
415 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
416 swab32s(&rel->r_offset);
417 swab32s(&rel->r_info);
418 swab32s(&rel->r_addend);
426 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
428 error("could not find .symtab section");
429 strtab_sec = &shdr[symtab_sec->sh_link];
431 symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size);
432 strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size);
434 nb_syms = symtab_sec->sh_size / sizeof(Elf32_Sym);
436 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
437 swab32s(&sym->st_name);
438 swab32s(&sym->st_value);
439 swab32s(&sym->st_size);
440 swab16s(&sym->st_shndx);
461 error("unsupported CPU (e_machine=%d)", e_machine);
464 fprintf(outfile, "#include \"gen-%s.h\"\n\n", cpu_name);
466 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
468 name = strtab + sym->st_name;
469 if (strstart(name, "op_", NULL) ||
470 strstart(name, "op1_", NULL) ||
471 strstart(name, "op2_", NULL) ||
472 strstart(name, "op3_", NULL)) {
474 printf("%4d: %s pos=0x%08x len=%d\n",
475 i, name, sym->st_value, sym->st_size);
477 if (sym->st_shndx != (text_sec - shdr))
478 error("invalid section for opcode (0x%x)", sym->st_shndx);
479 gen_code(name, sym->st_value, sym->st_size, outfile,
480 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab);
490 printf("dyngen (c) 2003 Fabrice Bellard\n"
491 "usage: dyngen [-o outfile] objfile\n"
492 "Generate a dynamic code generator from an object file\n");
496 int main(int argc, char **argv)
499 const char *filename, *outfilename;
502 outfilename = "out.c";
504 c = getopt(argc, argv, "ho:");
512 outfilename = optarg;
518 filename = argv[optind];
519 outfile = fopen(outfilename, "w");
521 error("could not open '%s'", outfilename);
522 load_elf(filename, outfile);