]> git.proxmox.com Git - mirror_qemu.git/blob - dyngen.c
added vm86.c
[mirror_qemu.git] / dyngen.c
1 /*
2 * Generic Dynamic compiler generator
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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.
19 */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27
28 #include "config.h"
29
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
32 platform) */
33 #if defined(HOST_I386)
34
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
39
40 #elif defined(HOST_PPC)
41
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
46
47 #elif defined(HOST_S390)
48
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
53
54 #elif defined(HOST_ALPHA)
55
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
60
61 #elif defined(HOST_IA64)
62
63 #define ELF_CLASS ELFCLASS64
64 #define ELF_ARCH EM_IA_64
65 #define elf_check_arch(x) ((x) == EM_IA_64)
66 #define ELF_USES_RELOCA
67
68 #elif defined(HOST_SPARC)
69
70 #define ELF_CLASS ELFCLASS32
71 #define ELF_ARCH EM_SPARC
72 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
73 #define ELF_USES_RELOCA
74
75 #elif defined(HOST_SPARC64)
76
77 #define ELF_CLASS ELFCLASS64
78 #define ELF_ARCH EM_SPARCV9
79 #define elf_check_arch(x) ((x) == EM_SPARCV9)
80 #define ELF_USES_RELOCA
81
82 #else
83 #error unsupported CPU - please update the code
84 #endif
85
86 #include "elf.h"
87
88 #if ELF_CLASS == ELFCLASS32
89 typedef int32_t host_long;
90 typedef uint32_t host_ulong;
91 #define swabls(x) swab32s(x)
92 #else
93 typedef int64_t host_long;
94 typedef uint64_t host_ulong;
95 #define swabls(x) swab64s(x)
96 #endif
97
98 #include "thunk.h"
99
100 /* all dynamically generated functions begin with this code */
101 #define OP_PREFIX "op_"
102
103 int elf_must_swap(struct elfhdr *h)
104 {
105 union {
106 uint32_t i;
107 uint8_t b[4];
108 } swaptest;
109
110 swaptest.i = 1;
111 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
112 (swaptest.b[0] == 0);
113 }
114
115 void swab16s(uint16_t *p)
116 {
117 *p = bswap16(*p);
118 }
119
120 void swab32s(uint32_t *p)
121 {
122 *p = bswap32(*p);
123 }
124
125 void swab64s(uint64_t *p)
126 {
127 *p = bswap64(*p);
128 }
129
130 void elf_swap_ehdr(struct elfhdr *h)
131 {
132 swab16s(&h->e_type); /* Object file type */
133 swab16s(&h-> e_machine); /* Architecture */
134 swab32s(&h-> e_version); /* Object file version */
135 swabls(&h-> e_entry); /* Entry point virtual address */
136 swabls(&h-> e_phoff); /* Program header table file offset */
137 swabls(&h-> e_shoff); /* Section header table file offset */
138 swab32s(&h-> e_flags); /* Processor-specific flags */
139 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
140 swab16s(&h-> e_phentsize); /* Program header table entry size */
141 swab16s(&h-> e_phnum); /* Program header table entry count */
142 swab16s(&h-> e_shentsize); /* Section header table entry size */
143 swab16s(&h-> e_shnum); /* Section header table entry count */
144 swab16s(&h-> e_shstrndx); /* Section header string table index */
145 }
146
147 void elf_swap_shdr(struct elf_shdr *h)
148 {
149 swab32s(&h-> sh_name); /* Section name (string tbl index) */
150 swab32s(&h-> sh_type); /* Section type */
151 swabls(&h-> sh_flags); /* Section flags */
152 swabls(&h-> sh_addr); /* Section virtual addr at execution */
153 swabls(&h-> sh_offset); /* Section file offset */
154 swabls(&h-> sh_size); /* Section size in bytes */
155 swab32s(&h-> sh_link); /* Link to another section */
156 swab32s(&h-> sh_info); /* Additional section information */
157 swabls(&h-> sh_addralign); /* Section alignment */
158 swabls(&h-> sh_entsize); /* Entry size if section holds table */
159 }
160
161 void elf_swap_phdr(struct elf_phdr *h)
162 {
163 swab32s(&h->p_type); /* Segment type */
164 swabls(&h->p_offset); /* Segment file offset */
165 swabls(&h->p_vaddr); /* Segment virtual address */
166 swabls(&h->p_paddr); /* Segment physical address */
167 swabls(&h->p_filesz); /* Segment size in file */
168 swabls(&h->p_memsz); /* Segment size in memory */
169 swab32s(&h->p_flags); /* Segment flags */
170 swabls(&h->p_align); /* Segment alignment */
171 }
172
173 int do_swap;
174
175 uint16_t get16(uint16_t *p)
176 {
177 uint16_t val;
178 val = *p;
179 if (do_swap)
180 val = bswap16(val);
181 return val;
182 }
183
184 uint32_t get32(uint32_t *p)
185 {
186 uint32_t val;
187 val = *p;
188 if (do_swap)
189 val = bswap32(val);
190 return val;
191 }
192
193 void put16(uint16_t *p, uint16_t val)
194 {
195 if (do_swap)
196 val = bswap16(val);
197 *p = val;
198 }
199
200 void put32(uint32_t *p, uint32_t val)
201 {
202 if (do_swap)
203 val = bswap32(val);
204 *p = val;
205 }
206
207 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
208 {
209 va_list ap;
210 va_start(ap, fmt);
211 fprintf(stderr, "dyngen: ");
212 vfprintf(stderr, fmt, ap);
213 fprintf(stderr, "\n");
214 va_end(ap);
215 exit(1);
216 }
217
218
219 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
220 const char *name)
221 {
222 int i;
223 const char *shname;
224 struct elf_shdr *sec;
225
226 for(i = 0; i < shnum; i++) {
227 sec = &shdr[i];
228 if (!sec->sh_name)
229 continue;
230 shname = shstr + sec->sh_name;
231 if (!strcmp(shname, name))
232 return sec;
233 }
234 return NULL;
235 }
236
237 void *load_data(int fd, long offset, unsigned int size)
238 {
239 char *data;
240
241 data = malloc(size);
242 if (!data)
243 return NULL;
244 lseek(fd, offset, SEEK_SET);
245 if (read(fd, data, size) != size) {
246 free(data);
247 return NULL;
248 }
249 return data;
250 }
251
252 int strstart(const char *str, const char *val, const char **ptr)
253 {
254 const char *p, *q;
255 p = str;
256 q = val;
257 while (*q != '\0') {
258 if (*p != *q)
259 return 0;
260 p++;
261 q++;
262 }
263 if (ptr)
264 *ptr = p;
265 return 1;
266 }
267
268 #define MAX_ARGS 3
269
270 /* generate op code */
271 void gen_code(const char *name, host_ulong offset, host_ulong size,
272 FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs, int reloc_sh_type,
273 ElfW(Sym) *symtab, char *strtab, int gen_switch)
274 {
275 int copy_size = 0;
276 uint8_t *p_start, *p_end;
277 int nb_args, i, n;
278 uint8_t args_present[MAX_ARGS];
279 const char *sym_name, *p;
280 ELF_RELOC *rel;
281
282 /* compute exact size excluding return instruction */
283 p_start = text + offset;
284 p_end = p_start + size;
285 switch(ELF_ARCH) {
286 case EM_386:
287 {
288 uint8_t *p;
289 p = p_end - 1;
290 if (p == p_start)
291 error("empty code for %s", name);
292 if (p[0] != 0xc3)
293 error("ret expected at the end of %s", name);
294 copy_size = p - p_start;
295 }
296 break;
297 case EM_PPC:
298 {
299 uint8_t *p;
300 p = (void *)(p_end - 4);
301 if (p == p_start)
302 error("empty code for %s", name);
303 if (get32((uint32_t *)p) != 0x4e800020)
304 error("blr expected at the end of %s", name);
305 copy_size = p - p_start;
306 }
307 break;
308 case EM_S390:
309 {
310 uint8_t *p;
311 p = (void *)(p_end - 2);
312 if (p == p_start)
313 error("empty code for %s", name);
314 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
315 error("br %%r14 expected at the end of %s", name);
316 copy_size = p - p_start;
317 }
318 break;
319 case EM_ALPHA:
320 {
321 uint8_t *p;
322 p = p_end - 4;
323 if (p == p_start)
324 error("empty code for %s", name);
325 if (get32((uint32_t *)p) != 0x6bfa8001)
326 error("ret expected at the end of %s", name);
327 copy_size = p - p_start;
328 }
329 break;
330 case EM_IA_64:
331 {
332 uint8_t *p;
333 p = (void *)(p_end - 4);
334 if (p == p_start)
335 error("empty code for %s", name);
336 /* br.ret.sptk.many b0;; */
337 /* 08 00 84 00 */
338 if (get32((uint32_t *)p) != 0x00840008)
339 error("br.ret.sptk.many b0;; expected at the end of %s", name);
340 copy_size = p - p_start;
341 }
342 break;
343 case EM_SPARC:
344 case EM_SPARC32PLUS:
345 {
346 uint8_t *p;
347 p = (void *)(p_end - 8);
348 if (p <= p_start)
349 error("empty code for %s", name);
350 if (get32((uint32_t *)(p_start + 0x0)) != 0x9de3bf98)
351 error("save %%sp,-104,%%sp expected at the start of %s "
352 "found [%08x]",
353 name, get32((uint32_t *)(p_start + 0x0)));
354 if (get32((uint32_t *)(p + 0x0)) != 0x81c7e008 ||
355 get32((uint32_t *)(p + 0x4)) != 0x81e80000)
356 error("ret; restore; expected at the end of %s found [%08x:%08x]",
357 name,
358 get32((uint32_t *)(p + 0x0)),
359 get32((uint32_t *)(p + 0x4)));
360
361 copy_size = p - p_start;
362 }
363 break;
364 case EM_SPARCV9:
365 {
366 uint8_t *p;
367 p = (void *)(p_end - 8);
368 if (p <= p_start)
369 error("empty code for %s", name);
370 if (get32((uint32_t *)(p_start + 0x0)) != 0x9de3bf40)
371 error("save %%sp,-192,%%sp expected at the start of %s "
372 "found [%08x]",
373 name, get32((uint32_t *)(p_start + 0x0)));
374 if (get32((uint32_t *)(p + 0x0)) != 0x81cfe008 ||
375 get32((uint32_t *)(p + 0x4)) != 0x01000000)
376 error("rett %%i7+8; nop; expected at the end of %s "
377 "found [%08x:%08x]",
378 name,
379 get32((uint32_t *)(p + 0x0)),
380 get32((uint32_t *)(p + 0x4)));
381 copy_size = p - p_start;
382 }
383 break;
384 default:
385 error("unknown ELF architecture");
386 }
387
388 /* compute the number of arguments by looking at the relocations */
389 for(i = 0;i < MAX_ARGS; i++)
390 args_present[i] = 0;
391
392 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
393 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
394 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
395 if (strstart(sym_name, "__op_param", &p)) {
396 n = strtoul(p, NULL, 10);
397 if (n >= MAX_ARGS)
398 error("too many arguments in %s", name);
399 args_present[n - 1] = 1;
400 }
401 }
402 }
403
404 nb_args = 0;
405 while (nb_args < MAX_ARGS && args_present[nb_args])
406 nb_args++;
407 for(i = nb_args; i < MAX_ARGS; i++) {
408 if (args_present[i])
409 error("inconsistent argument numbering in %s", name);
410 }
411
412 if (gen_switch == 2) {
413 fprintf(outfile, "DEF(%s, %d)\n", name + 3, nb_args);
414 } else if (gen_switch == 1) {
415
416 /* output C code */
417 fprintf(outfile, "case INDEX_%s: {\n", name);
418 if (nb_args > 0) {
419 fprintf(outfile, " long ");
420 for(i = 0; i < nb_args; i++) {
421 if (i != 0)
422 fprintf(outfile, ", ");
423 fprintf(outfile, "param%d", i + 1);
424 }
425 fprintf(outfile, ";\n");
426 }
427 fprintf(outfile, " extern void %s();\n", name);
428
429 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
430 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
431 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
432 if (!strstart(sym_name, "__op_param", &p)) {
433 #if defined(HOST_SPARC)
434 if (sym_name[0] == '.') {
435 fprintf(outfile,
436 "extern char __dot_%s __asm__(\"%s\");\n",
437 sym_name+1, sym_name);
438 continue;
439 }
440 #endif
441 fprintf(outfile, "extern char %s;\n", sym_name);
442 }
443 }
444 }
445
446 fprintf(outfile, " memcpy(gen_code_ptr, &%s, %d);\n", name, copy_size);
447 for(i = 0; i < nb_args; i++) {
448 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
449 }
450
451 /* patch relocations */
452 #if defined(HOST_I386)
453 {
454 char name[256];
455 int type;
456 int addend;
457 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
458 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
459 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
460 if (strstart(sym_name, "__op_param", &p)) {
461 snprintf(name, sizeof(name), "param%s", p);
462 } else {
463 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
464 }
465 type = ELF32_R_TYPE(rel->r_info);
466 addend = get32((uint32_t *)(text + rel->r_offset));
467 switch(type) {
468 case R_386_32:
469 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
470 rel->r_offset - offset, name, addend);
471 break;
472 case R_386_PC32:
473 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
474 rel->r_offset - offset, name, rel->r_offset - offset, addend);
475 break;
476 default:
477 error("unsupported i386 relocation (%d)", type);
478 }
479 }
480 }
481 }
482 #elif defined(HOST_PPC)
483 {
484 char name[256];
485 int type;
486 int addend;
487 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
488 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
489 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
490 if (strstart(sym_name, "__op_param", &p)) {
491 snprintf(name, sizeof(name), "param%s", p);
492 } else {
493 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
494 }
495 type = ELF32_R_TYPE(rel->r_info);
496 addend = rel->r_addend;
497 switch(type) {
498 case R_PPC_ADDR32:
499 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
500 rel->r_offset - offset, name, addend);
501 break;
502 case R_PPC_ADDR16_LO:
503 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
504 rel->r_offset - offset, name, addend);
505 break;
506 case R_PPC_ADDR16_HI:
507 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
508 rel->r_offset - offset, name, addend);
509 break;
510 case R_PPC_ADDR16_HA:
511 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
512 rel->r_offset - offset, name, addend);
513 break;
514 case R_PPC_REL24:
515 /* warning: must be at 32 MB distancy */
516 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
517 rel->r_offset - offset, rel->r_offset - offset, name, rel->r_offset - offset, addend);
518 break;
519 default:
520 error("unsupported powerpc relocation (%d)", type);
521 }
522 }
523 }
524 }
525 #elif defined(HOST_S390)
526 {
527 char name[256];
528 int type;
529 int addend;
530 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
531 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
532 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
533 if (strstart(sym_name, "__op_param", &p)) {
534 snprintf(name, sizeof(name), "param%s", p);
535 } else {
536 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
537 }
538 type = ELF32_R_TYPE(rel->r_info);
539 addend = rel->r_addend;
540 switch(type) {
541 case R_390_32:
542 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
543 rel->r_offset - offset, name, addend);
544 break;
545 case R_390_16:
546 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
547 rel->r_offset - offset, name, addend);
548 break;
549 case R_390_8:
550 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
551 rel->r_offset - offset, name, addend);
552 break;
553 default:
554 error("unsupported s390 relocation (%d)", type);
555 }
556 }
557 }
558 }
559 #elif defined(HOST_ALPHA)
560 {
561 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
562 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
563 int type;
564 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
565
566 type = ELF64_R_TYPE(rel->r_info);
567 switch (type) {
568 case R_ALPHA_GPDISP:
569 /* Instructions to set up the gp can be nopped, since we keep it current
570 all the time. FIXME assert that target is really gp */
571 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = 0x2ffe0000; /* unop */\n",
572 rel->r_offset - offset);
573 break;
574 case R_ALPHA_LITUSE:
575 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
576 now, since some called functions (libc) need pv to be set up. */
577 break;
578 case R_ALPHA_HINT:
579 /* Branch target prediction hint. Ignore for now. Should be already
580 correct for in-function jumps. */
581 break;
582 case R_ALPHA_LITERAL:
583 /* Load a literal from the GOT relative to the gp. Need to patch the
584 16-bit immediate offset. */
585 fprintf(outfile, " *(int16_t *)(gen_code_ptr + %d) = gp - (long)(&%s);\n",
586 rel->r_offset - offset, name);
587 break;
588 default:
589 error("unsupported Alpha relocation (%d)", type);
590 }
591 }
592 }
593 }
594 #elif defined(HOST_IA64)
595 {
596 char name[256];
597 int type;
598 int addend;
599 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
600 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
601 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
602 if (strstart(sym_name, "__op_param", &p)) {
603 snprintf(name, sizeof(name), "param%s", p);
604 } else {
605 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
606 }
607 type = ELF64_R_TYPE(rel->r_info);
608 addend = rel->r_addend;
609 switch(type) {
610 case R_IA64_LTOFF22:
611 error("must implemnt R_IA64_LTOFF22 relocation");
612 case R_IA64_PCREL21B:
613 error("must implemnt R_IA64_PCREL21B relocation");
614 default:
615 error("unsupported ia64 relocation (%d)", type);
616 }
617 }
618 }
619 }
620 #elif defined(HOST_SPARC)
621 {
622 char name[256];
623 int type;
624 int addend;
625 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
626 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
627 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
628 if (strstart(sym_name, "__op_param", &p)) {
629 snprintf(name, sizeof(name), "param%s", p);
630 } else {
631 if (sym_name[0] == '.')
632 snprintf(name, sizeof(name),
633 "(long)(&__dot_%s)",
634 sym_name + 1);
635 else
636 snprintf(name, sizeof(name),
637 "(long)(&%s)", sym_name);
638 }
639 type = ELF32_R_TYPE(rel->r_info);
640 addend = rel->r_addend;
641 switch(type) {
642 case R_SPARC_32:
643 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
644 rel->r_offset - offset, name, addend);
645 break;
646 case R_SPARC_HI22:
647 fprintf(outfile,
648 " *(uint32_t *)(gen_code_ptr + %d) = "
649 "((*(uint32_t *)(gen_code_ptr + %d)) "
650 " & ~0x3fffff) "
651 " | ((%s + %d) & 0x3fffff);\n",
652 rel->r_offset - offset,
653 rel->r_offset - offset,
654 name, addend);
655 break;
656 case R_SPARC_LO10:
657 fprintf(outfile,
658 " *(uint32_t *)(gen_code_ptr + %d) = "
659 "((*(uint32_t *)(gen_code_ptr + %d)) "
660 " & ~0x3ff) "
661 " | ((%s + %d) & 0x3ff);\n",
662 rel->r_offset - offset,
663 rel->r_offset - offset,
664 name, addend);
665 break;
666 case R_SPARC_WDISP30:
667 fprintf(outfile,
668 " *(uint32_t *)(gen_code_ptr + %d) = "
669 "((*(uint32_t *)(gen_code_ptr + %d)) "
670 " & ~0x3fffffff) "
671 " | ((((%s + %d) - (long)gen_code_ptr)>>2) "
672 " & 0x3fffffff);\n",
673 rel->r_offset - offset,
674 rel->r_offset - offset,
675 name, addend);
676 break;
677 default:
678 error("unsupported sparc relocation (%d)", type);
679 }
680 }
681 }
682 }
683 #elif defined(HOST_SPARC64)
684 {
685 char name[256];
686 int type;
687 int addend;
688 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
689 if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) {
690 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
691 if (strstart(sym_name, "__op_param", &p)) {
692 snprintf(name, sizeof(name), "param%s", p);
693 } else {
694 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
695 }
696 type = ELF64_R_TYPE(rel->r_info);
697 addend = rel->r_addend;
698 switch(type) {
699 case R_SPARC_32:
700 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
701 rel->r_offset - offset, name, addend);
702 break;
703 case R_SPARC_HI22:
704 fprintf(outfile,
705 " *(uint32_t *)(gen_code_ptr + %d) = "
706 "((*(uint32_t *)(gen_code_ptr + %d)) "
707 " & ~0x3fffff) "
708 " | ((%s + %d) & 0x3fffff);\n",
709 rel->r_offset - offset,
710 rel->r_offset - offset,
711 name, addend);
712 break;
713 case R_SPARC_LO10:
714 fprintf(outfile,
715 " *(uint32_t *)(gen_code_ptr + %d) = "
716 "((*(uint32_t *)(gen_code_ptr + %d)) "
717 " & ~0x3ff) "
718 " | ((%s + %d) & 0x3ff);\n",
719 rel->r_offset - offset,
720 rel->r_offset - offset,
721 name, addend);
722 break;
723 case R_SPARC_WDISP30:
724 fprintf(outfile,
725 " *(uint32_t *)(gen_code_ptr + %d) = "
726 "((*(uint32_t *)(gen_code_ptr + %d)) "
727 " & ~0x3fffffff) "
728 " | ((((%s + %d) - (long)gen_code_ptr)>>2) "
729 " & 0x3fffffff);\n",
730 rel->r_offset - offset,
731 rel->r_offset - offset,
732 name, addend);
733 break;
734 default:
735 error("unsupported sparc64 relocation (%d)", type);
736 }
737 }
738 }
739 }
740 #else
741 #error unsupported CPU
742 #endif
743 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
744 fprintf(outfile, "}\n");
745 fprintf(outfile, "break;\n\n");
746 } else {
747 fprintf(outfile, "static inline void gen_%s(", name);
748 if (nb_args == 0) {
749 fprintf(outfile, "void");
750 } else {
751 for(i = 0; i < nb_args; i++) {
752 if (i != 0)
753 fprintf(outfile, ", ");
754 fprintf(outfile, "long param%d", i + 1);
755 }
756 }
757 fprintf(outfile, ")\n");
758 fprintf(outfile, "{\n");
759 for(i = 0; i < nb_args; i++) {
760 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
761 }
762 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
763 fprintf(outfile, "}\n\n");
764 }
765 }
766
767 /* load an elf object file */
768 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
769 {
770 int fd;
771 struct elfhdr ehdr;
772 struct elf_shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec;
773 int i, j, nb_syms;
774 ElfW(Sym) *symtab, *sym;
775 char *shstr, *strtab;
776 uint8_t *text;
777 void *relocs;
778 int nb_relocs, reloc_sh_type;
779
780 fd = open(filename, O_RDONLY);
781 if (fd < 0)
782 error("can't open file '%s'", filename);
783
784 /* Read ELF header. */
785 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
786 error("unable to read file header");
787
788 /* Check ELF identification. */
789 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
790 || ehdr.e_ident[EI_MAG1] != ELFMAG1
791 || ehdr.e_ident[EI_MAG2] != ELFMAG2
792 || ehdr.e_ident[EI_MAG3] != ELFMAG3
793 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
794 error("bad ELF header");
795 }
796
797 do_swap = elf_must_swap(&ehdr);
798 if (do_swap)
799 elf_swap_ehdr(&ehdr);
800 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
801 error("Unsupported ELF class");
802 if (ehdr.e_type != ET_REL)
803 error("ELF object file expected");
804 if (ehdr.e_version != EV_CURRENT)
805 error("Invalid ELF version");
806 if (!elf_check_arch(ehdr.e_machine))
807 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
808
809 /* read section headers */
810 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
811 if (do_swap) {
812 for(i = 0; i < ehdr.e_shnum; i++) {
813 elf_swap_shdr(&shdr[i]);
814 }
815 }
816
817 sec = &shdr[ehdr.e_shstrndx];
818 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
819
820 /* text section */
821
822 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
823 if (!text_sec)
824 error("could not find .text section");
825 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
826
827 /* find text relocations, if any */
828 nb_relocs = 0;
829 relocs = NULL;
830 reloc_sh_type = 0;
831 for(i = 0; i < ehdr.e_shnum; i++) {
832 sec = &shdr[i];
833 if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) &&
834 sec->sh_info == (text_sec - shdr)) {
835 reloc_sh_type = sec->sh_type;
836 relocs = load_data(fd, sec->sh_offset, sec->sh_size);
837 nb_relocs = sec->sh_size / sec->sh_entsize;
838 if (do_swap) {
839 if (sec->sh_type == SHT_REL) {
840 ElfW(Rel) *rel = relocs;
841 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
842 swabls(&rel->r_offset);
843 swabls(&rel->r_info);
844 }
845 } else {
846 ElfW(Rela) *rel = relocs;
847 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
848 swabls(&rel->r_offset);
849 swabls(&rel->r_info);
850 swabls(&rel->r_addend);
851 }
852 }
853 }
854 break;
855 }
856 }
857
858 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
859 if (!symtab_sec)
860 error("could not find .symtab section");
861 strtab_sec = &shdr[symtab_sec->sh_link];
862
863 symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size);
864 strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size);
865
866 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
867 if (do_swap) {
868 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
869 swab32s(&sym->st_name);
870 swabls(&sym->st_value);
871 swabls(&sym->st_size);
872 swab16s(&sym->st_shndx);
873 }
874 }
875
876 if (do_print_enum) {
877 fprintf(outfile, "DEF(end, 0)\n");
878 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
879 const char *name, *p;
880 name = strtab + sym->st_name;
881 if (strstart(name, OP_PREFIX, &p)) {
882 gen_code(name, sym->st_value, sym->st_size, outfile,
883 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 2);
884 }
885 }
886 } else {
887 /* generate big code generation switch */
888 #ifdef HOST_ALPHA
889 fprintf(outfile, "register long gp asm(\"%%$29\");\n");
890 #endif
891 fprintf(outfile,
892 "int dyngen_code(uint8_t *gen_code_buf,\n"
893 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
894 "{\n"
895 " uint8_t *gen_code_ptr;\n"
896 " const uint16_t *opc_ptr;\n"
897 " const uint32_t *opparam_ptr;\n"
898 " gen_code_ptr = gen_code_buf;\n"
899 " opc_ptr = opc_buf;\n"
900 " opparam_ptr = opparam_buf;\n"
901 " for(;;) {\n"
902 " switch(*opc_ptr++) {\n"
903 );
904
905 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
906 const char *name;
907 name = strtab + sym->st_name;
908 if (strstart(name, OP_PREFIX, NULL)) {
909 #if 0
910 printf("%4d: %s pos=0x%08x len=%d\n",
911 i, name, sym->st_value, sym->st_size);
912 #endif
913 if (sym->st_shndx != (text_sec - shdr))
914 error("invalid section for opcode (0x%x)", sym->st_shndx);
915 gen_code(name, sym->st_value, sym->st_size, outfile,
916 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 1);
917 }
918 }
919
920 fprintf(outfile,
921 " default:\n"
922 " goto the_end;\n"
923 " }\n"
924 " }\n"
925 " the_end:\n"
926 );
927
928 /* generate a return */
929 switch(ELF_ARCH) {
930 case EM_386:
931 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
932 break;
933 case EM_PPC:
934 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
935 break;
936 case EM_S390:
937 fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
938 break;
939 case EM_ALPHA:
940 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n");
941 break;
942 case EM_IA_64:
943 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n");
944 break;
945 case EM_SPARC:
946 case EM_SPARC32PLUS:
947 case EM_SPARCV9:
948 /* Fill the delay slot. */
949 fprintf(outfile, "*((uint32_t *)gen_code_ptr) = *((uint32_t *)gen_code_ptr - 1); /* delay slot */\n");
950 fprintf(outfile, "*((uint32_t *)gen_code_ptr - 1) = 0x81c3e008; /* retl */\n");
951 fprintf(outfile, "gen_code_ptr++;\n");
952 break;
953 default:
954 error("unknown ELF architecture");
955 }
956
957 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
958 fprintf(outfile, "}\n\n");
959
960 /* generate gen_xxx functions */
961 /* XXX: suppress the use of these functions to simplify code */
962 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
963 const char *name;
964 name = strtab + sym->st_name;
965 if (strstart(name, OP_PREFIX, NULL)) {
966 if (sym->st_shndx != (text_sec - shdr))
967 error("invalid section for opcode (0x%x)", sym->st_shndx);
968 gen_code(name, sym->st_value, sym->st_size, outfile,
969 text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 0);
970 }
971 }
972 }
973
974 close(fd);
975 return 0;
976 }
977
978 void usage(void)
979 {
980 printf("dyngen (c) 2003 Fabrice Bellard\n"
981 "usage: dyngen [-o outfile] [-c] objfile\n"
982 "Generate a dynamic code generator from an object file\n"
983 "-c output enum of operations\n"
984 );
985 exit(1);
986 }
987
988 int main(int argc, char **argv)
989 {
990 int c, do_print_enum;
991 const char *filename, *outfilename;
992 FILE *outfile;
993
994 outfilename = "out.c";
995 do_print_enum = 0;
996 for(;;) {
997 c = getopt(argc, argv, "ho:c");
998 if (c == -1)
999 break;
1000 switch(c) {
1001 case 'h':
1002 usage();
1003 break;
1004 case 'o':
1005 outfilename = optarg;
1006 break;
1007 case 'c':
1008 do_print_enum = 1;
1009 break;
1010 }
1011 }
1012 if (optind >= argc)
1013 usage();
1014 filename = argv[optind];
1015 outfile = fopen(outfilename, "w");
1016 if (!outfile)
1017 error("could not open '%s'", outfilename);
1018 load_elf(filename, outfile, do_print_enum);
1019 fclose(outfile);
1020 return 0;
1021 }