]> git.proxmox.com Git - mirror_qemu.git/blob - dyngen.c
Alpha update (Falk Hueffner)
[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 /* ELF file info */
174 int do_swap;
175 struct elf_shdr *shdr;
176 struct elfhdr ehdr;
177 ElfW(Sym) *symtab;
178 int nb_syms;
179 char *strtab;
180 /* data section */
181 uint8_t *data_data, *sdata_data;
182 int data_shndx, sdata_shndx;
183
184 uint16_t get16(uint16_t *p)
185 {
186 uint16_t val;
187 val = *p;
188 if (do_swap)
189 val = bswap16(val);
190 return val;
191 }
192
193 uint32_t get32(uint32_t *p)
194 {
195 uint32_t val;
196 val = *p;
197 if (do_swap)
198 val = bswap32(val);
199 return val;
200 }
201
202 void put16(uint16_t *p, uint16_t val)
203 {
204 if (do_swap)
205 val = bswap16(val);
206 *p = val;
207 }
208
209 void put32(uint32_t *p, uint32_t val)
210 {
211 if (do_swap)
212 val = bswap32(val);
213 *p = val;
214 }
215
216 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
217 {
218 va_list ap;
219 va_start(ap, fmt);
220 fprintf(stderr, "dyngen: ");
221 vfprintf(stderr, fmt, ap);
222 fprintf(stderr, "\n");
223 va_end(ap);
224 exit(1);
225 }
226
227
228 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
229 const char *name)
230 {
231 int i;
232 const char *shname;
233 struct elf_shdr *sec;
234
235 for(i = 0; i < shnum; i++) {
236 sec = &shdr[i];
237 if (!sec->sh_name)
238 continue;
239 shname = shstr + sec->sh_name;
240 if (!strcmp(shname, name))
241 return sec;
242 }
243 return NULL;
244 }
245
246 void *load_data(int fd, long offset, unsigned int size)
247 {
248 char *data;
249
250 data = malloc(size);
251 if (!data)
252 return NULL;
253 lseek(fd, offset, SEEK_SET);
254 if (read(fd, data, size) != size) {
255 free(data);
256 return NULL;
257 }
258 return data;
259 }
260
261 int strstart(const char *str, const char *val, const char **ptr)
262 {
263 const char *p, *q;
264 p = str;
265 q = val;
266 while (*q != '\0') {
267 if (*p != *q)
268 return 0;
269 p++;
270 q++;
271 }
272 if (ptr)
273 *ptr = p;
274 return 1;
275 }
276
277 #define MAX_ARGS 3
278
279 /* generate op code */
280 void gen_code(const char *name, host_ulong offset, host_ulong size,
281 FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs, int reloc_sh_type,
282 int gen_switch)
283 {
284 int copy_size = 0;
285 uint8_t *p_start, *p_end;
286 host_ulong start_offset;
287 int nb_args, i, n;
288 uint8_t args_present[MAX_ARGS];
289 const char *sym_name, *p;
290 ELF_RELOC *rel;
291
292 /* Compute exact size excluding prologue and epilogue instructions.
293 * Increment start_offset to skip epilogue instructions, then compute
294 * copy_size the indicate the size of the remaining instructions (in
295 * bytes).
296 */
297 p_start = text + offset;
298 p_end = p_start + size;
299 start_offset = offset;
300 switch(ELF_ARCH) {
301 case EM_386:
302 {
303 int len;
304 len = p_end - p_start;
305 if (len == 0)
306 error("empty code for %s", name);
307 if (p_end[-1] == 0xc3) {
308 len--;
309 } else {
310 error("ret or jmp expected at the end of %s", name);
311 }
312 copy_size = len;
313 }
314 break;
315 case EM_PPC:
316 {
317 uint8_t *p;
318 p = (void *)(p_end - 4);
319 if (p == p_start)
320 error("empty code for %s", name);
321 if (get32((uint32_t *)p) != 0x4e800020)
322 error("blr expected at the end of %s", name);
323 copy_size = p - p_start;
324 }
325 break;
326 case EM_S390:
327 {
328 uint8_t *p;
329 p = (void *)(p_end - 2);
330 if (p == p_start)
331 error("empty code for %s", name);
332 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
333 error("br %%r14 expected at the end of %s", name);
334 copy_size = p - p_start;
335 }
336 break;
337 case EM_ALPHA:
338 {
339 uint8_t *p;
340 p = p_end - 4;
341 if (p == p_start)
342 error("empty code for %s", name);
343 if (get32((uint32_t *)p) != 0x6bfa8001)
344 error("ret expected at the end of %s", name);
345 copy_size = p - p_start;
346 }
347 break;
348 case EM_IA_64:
349 {
350 uint8_t *p;
351 p = (void *)(p_end - 4);
352 if (p == p_start)
353 error("empty code for %s", name);
354 /* br.ret.sptk.many b0;; */
355 /* 08 00 84 00 */
356 if (get32((uint32_t *)p) != 0x00840008)
357 error("br.ret.sptk.many b0;; expected at the end of %s", name);
358 copy_size = p - p_start;
359 }
360 break;
361 case EM_SPARC:
362 case EM_SPARC32PLUS:
363 {
364 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
365 uint8_t *p;
366 p = (void *)(p_end - 8);
367 if (p <= p_start)
368 error("empty code for %s", name);
369 start_insn = get32((uint32_t *)(p_start + 0x0));
370 end_insn1 = get32((uint32_t *)(p + 0x0));
371 end_insn2 = get32((uint32_t *)(p + 0x4));
372 if ((start_insn & ~0x1fff) == 0x9de3a000) {
373 p_start += 0x4;
374 start_offset += 0x4;
375 if ((int)(start_insn | ~0x1fff) < -128)
376 error("Found bogus save at the start of %s", name);
377 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
378 error("ret; restore; not found at end of %s", name);
379 } else {
380 error("No save at the beginning of %s", name);
381 }
382
383 /* Skip a preceeding nop, if present. */
384 if (p > p_start) {
385 skip_insn = get32((uint32_t *)(p - 0x4));
386 if (skip_insn == 0x01000000)
387 p -= 4;
388 }
389
390 copy_size = p - p_start;
391 }
392 break;
393 case EM_SPARCV9:
394 {
395 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
396 uint8_t *p;
397 p = (void *)(p_end - 8);
398 if (p <= p_start)
399 error("empty code for %s", name);
400 start_insn = get32((uint32_t *)(p_start + 0x0));
401 end_insn1 = get32((uint32_t *)(p + 0x0));
402 end_insn2 = get32((uint32_t *)(p + 0x4));
403 if ((start_insn & ~0x1fff) == 0x9de3a000) {
404 p_start += 0x4;
405 start_offset += 0x4;
406 if ((int)(start_insn | ~0x1fff) < -256)
407 error("Found bogus save at the start of %s", name);
408 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
409 error("ret; restore; not found at end of %s", name);
410 } else {
411 error("No save at the beginning of %s", name);
412 }
413
414 /* Skip a preceeding nop, if present. */
415 if (p > p_start) {
416 skip_insn = get32((uint32_t *)(p - 0x4));
417 if (skip_insn == 0x01000000)
418 p -= 4;
419 }
420
421 copy_size = p - p_start;
422 }
423 break;
424 default:
425 error("unknown ELF architecture");
426 }
427
428 /* compute the number of arguments by looking at the relocations */
429 for(i = 0;i < MAX_ARGS; i++)
430 args_present[i] = 0;
431
432 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
433 if (rel->r_offset >= start_offset &&
434 rel->r_offset < start_offset + copy_size) {
435 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
436 if (strstart(sym_name, "__op_param", &p)) {
437 n = strtoul(p, NULL, 10);
438 if (n > MAX_ARGS)
439 error("too many arguments in %s", name);
440 args_present[n - 1] = 1;
441 }
442 }
443 }
444
445 nb_args = 0;
446 while (nb_args < MAX_ARGS && args_present[nb_args])
447 nb_args++;
448 for(i = nb_args; i < MAX_ARGS; i++) {
449 if (args_present[i])
450 error("inconsistent argument numbering in %s", name);
451 }
452
453 if (gen_switch == 2) {
454 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
455 } else if (gen_switch == 1) {
456
457 /* output C code */
458 fprintf(outfile, "case INDEX_%s: {\n", name);
459 if (nb_args > 0) {
460 fprintf(outfile, " long ");
461 for(i = 0; i < nb_args; i++) {
462 if (i != 0)
463 fprintf(outfile, ", ");
464 fprintf(outfile, "param%d", i + 1);
465 }
466 fprintf(outfile, ";\n");
467 }
468 fprintf(outfile, " extern void %s();\n", name);
469
470 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
471 if (rel->r_offset >= start_offset &&
472 rel->r_offset < start_offset + copy_size) {
473 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
474 if (*sym_name &&
475 !strstart(sym_name, "__op_param", NULL) &&
476 !strstart(sym_name, "__op_jmp", NULL)) {
477 #if defined(HOST_SPARC)
478 if (sym_name[0] == '.') {
479 fprintf(outfile,
480 "extern char __dot_%s __asm__(\"%s\");\n",
481 sym_name+1, sym_name);
482 continue;
483 }
484 #endif
485 fprintf(outfile, "extern char %s;\n", sym_name);
486 }
487 }
488 }
489
490 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
491
492 /* emit code offset information */
493 {
494 ElfW(Sym) *sym;
495 const char *sym_name, *p;
496 target_ulong val;
497 int n;
498
499 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
500 sym_name = strtab + sym->st_name;
501 if (strstart(sym_name, "__op_label", &p)) {
502 uint8_t *ptr;
503
504 /* test if the variable refers to a label inside
505 the code we are generating */
506 if (sym->st_shndx == data_shndx)
507 ptr = data_data;
508 else if (sym->st_shndx == sdata_shndx)
509 ptr = sdata_data;
510 else
511 error("__op_labelN symbols must be in .data or .sdata section");
512 val = *(target_ulong *)(ptr + sym->st_value);
513 if (val >= start_offset && val < start_offset + copy_size) {
514 n = strtol(p, NULL, 10);
515 fprintf(outfile, " label_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
516 }
517 }
518 }
519 }
520
521 /* load parameres in variables */
522 for(i = 0; i < nb_args; i++) {
523 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
524 }
525
526 /* patch relocations */
527 #if defined(HOST_I386)
528 {
529 char name[256];
530 int type;
531 int addend;
532 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
533 if (rel->r_offset >= start_offset &&
534 rel->r_offset < start_offset + copy_size) {
535 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
536 if (strstart(sym_name, "__op_param", &p)) {
537 snprintf(name, sizeof(name), "param%s", p);
538 } else {
539 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
540 }
541 type = ELF32_R_TYPE(rel->r_info);
542 addend = get32((uint32_t *)(text + rel->r_offset));
543 switch(type) {
544 case R_386_32:
545 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
546 rel->r_offset - start_offset, name, addend);
547 break;
548 case R_386_PC32:
549 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
550 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
551 break;
552 default:
553 error("unsupported i386 relocation (%d)", type);
554 }
555 }
556 }
557 }
558 #elif defined(HOST_PPC)
559 {
560 char name[256];
561 int type;
562 int addend;
563 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
564 if (rel->r_offset >= start_offset &&
565 rel->r_offset < start_offset + copy_size) {
566 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
567 if (strstart(sym_name, "__op_jmp", &p)) {
568 int n;
569 n = strtol(p, NULL, 10);
570 /* __op_jmp relocations are done at
571 runtime to do translated block
572 chaining: the offset of the instruction
573 needs to be stored */
574 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
575 n, rel->r_offset - start_offset);
576 continue;
577 }
578
579 if (strstart(sym_name, "__op_param", &p)) {
580 snprintf(name, sizeof(name), "param%s", p);
581 } else {
582 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
583 }
584 type = ELF32_R_TYPE(rel->r_info);
585 addend = rel->r_addend;
586 switch(type) {
587 case R_PPC_ADDR32:
588 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
589 rel->r_offset - start_offset, name, addend);
590 break;
591 case R_PPC_ADDR16_LO:
592 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
593 rel->r_offset - start_offset, name, addend);
594 break;
595 case R_PPC_ADDR16_HI:
596 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
597 rel->r_offset - start_offset, name, addend);
598 break;
599 case R_PPC_ADDR16_HA:
600 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
601 rel->r_offset - start_offset, name, addend);
602 break;
603 case R_PPC_REL24:
604 /* warning: must be at 32 MB distancy */
605 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
606 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
607 break;
608 default:
609 error("unsupported powerpc relocation (%d)", type);
610 }
611 }
612 }
613 }
614 #elif defined(HOST_S390)
615 {
616 char name[256];
617 int type;
618 int addend;
619 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
620 if (rel->r_offset >= start_offset &&
621 rel->r_offset < start_offset + copy_size) {
622 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
623 if (strstart(sym_name, "__op_param", &p)) {
624 snprintf(name, sizeof(name), "param%s", p);
625 } else {
626 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
627 }
628 type = ELF32_R_TYPE(rel->r_info);
629 addend = rel->r_addend;
630 switch(type) {
631 case R_390_32:
632 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
633 rel->r_offset - start_offset, name, addend);
634 break;
635 case R_390_16:
636 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
637 rel->r_offset - start_offset, name, addend);
638 break;
639 case R_390_8:
640 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
641 rel->r_offset - start_offset, name, addend);
642 break;
643 default:
644 error("unsupported s390 relocation (%d)", type);
645 }
646 }
647 }
648 }
649 #elif defined(HOST_ALPHA)
650 {
651 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
652 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
653 int type;
654
655 type = ELF64_R_TYPE(rel->r_info);
656 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
657 switch (type) {
658 case R_ALPHA_GPDISP:
659 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
660 as an immediate instead of constructing it from the pv or ra. */
661 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
662 rel->r_offset - start_offset);
663 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
664 rel->r_offset - start_offset + rel->r_addend);
665 break;
666 case R_ALPHA_LITUSE:
667 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
668 now, since some called functions (libc) need pv to be set up. */
669 break;
670 case R_ALPHA_HINT:
671 /* Branch target prediction hint. Ignore for now. Should be already
672 correct for in-function jumps. */
673 break;
674 case R_ALPHA_LITERAL:
675 /* Load a literal from the GOT relative to the gp. Since there's only a
676 single gp, nothing is to be done. */
677 break;
678 case R_ALPHA_GPRELHIGH:
679 /* Handle fake relocations against __op_param symbol. Need to emit the
680 high part of the immediate value instead. Other symbols need no
681 special treatment. */
682 if (strstart(sym_name, "__op_param", &p))
683 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
684 rel->r_offset - start_offset, p);
685 break;
686 case R_ALPHA_GPRELLOW:
687 if (strstart(sym_name, "__op_param", &p))
688 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
689 rel->r_offset - start_offset, p);
690 break;
691 case R_ALPHA_BRSGP:
692 /* PC-relative jump. Tweak offset to skip the two instructions that try to
693 set up the gp from the pv. */
694 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
695 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
696 break;
697 default:
698 error("unsupported Alpha relocation (%d)", type);
699 }
700 }
701 }
702 }
703 #elif defined(HOST_IA64)
704 {
705 char name[256];
706 int type;
707 int addend;
708 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
709 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
710 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
711 if (strstart(sym_name, "__op_param", &p)) {
712 snprintf(name, sizeof(name), "param%s", p);
713 } else {
714 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
715 }
716 type = ELF64_R_TYPE(rel->r_info);
717 addend = rel->r_addend;
718 switch(type) {
719 case R_IA64_LTOFF22:
720 error("must implemnt R_IA64_LTOFF22 relocation");
721 case R_IA64_PCREL21B:
722 error("must implemnt R_IA64_PCREL21B relocation");
723 default:
724 error("unsupported ia64 relocation (%d)", type);
725 }
726 }
727 }
728 }
729 #elif defined(HOST_SPARC)
730 {
731 char name[256];
732 int type;
733 int addend;
734 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
735 if (rel->r_offset >= start_offset &&
736 rel->r_offset < start_offset + copy_size) {
737 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
738 if (strstart(sym_name, "__op_param", &p)) {
739 snprintf(name, sizeof(name), "param%s", p);
740 } else {
741 if (sym_name[0] == '.')
742 snprintf(name, sizeof(name),
743 "(long)(&__dot_%s)",
744 sym_name + 1);
745 else
746 snprintf(name, sizeof(name),
747 "(long)(&%s)", sym_name);
748 }
749 type = ELF32_R_TYPE(rel->r_info);
750 addend = rel->r_addend;
751 switch(type) {
752 case R_SPARC_32:
753 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
754 rel->r_offset - start_offset, name, addend);
755 break;
756 case R_SPARC_HI22:
757 fprintf(outfile,
758 " *(uint32_t *)(gen_code_ptr + %d) = "
759 "((*(uint32_t *)(gen_code_ptr + %d)) "
760 " & ~0x3fffff) "
761 " | (((%s + %d) >> 10) & 0x3fffff);\n",
762 rel->r_offset - start_offset,
763 rel->r_offset - start_offset,
764 name, addend);
765 break;
766 case R_SPARC_LO10:
767 fprintf(outfile,
768 " *(uint32_t *)(gen_code_ptr + %d) = "
769 "((*(uint32_t *)(gen_code_ptr + %d)) "
770 " & ~0x3ff) "
771 " | ((%s + %d) & 0x3ff);\n",
772 rel->r_offset - start_offset,
773 rel->r_offset - start_offset,
774 name, addend);
775 break;
776 case R_SPARC_WDISP30:
777 fprintf(outfile,
778 " *(uint32_t *)(gen_code_ptr + %d) = "
779 "((*(uint32_t *)(gen_code_ptr + %d)) "
780 " & ~0x3fffffff) "
781 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
782 " & 0x3fffffff);\n",
783 rel->r_offset - start_offset,
784 rel->r_offset - start_offset,
785 name, addend,
786 rel->r_offset - start_offset);
787 break;
788 default:
789 error("unsupported sparc relocation (%d)", type);
790 }
791 }
792 }
793 }
794 #elif defined(HOST_SPARC64)
795 {
796 char name[256];
797 int type;
798 int addend;
799 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
800 if (rel->r_offset >= start_offset &&
801 rel->r_offset < start_offset + copy_size) {
802 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
803 if (strstart(sym_name, "__op_param", &p)) {
804 snprintf(name, sizeof(name), "param%s", p);
805 } else {
806 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
807 }
808 type = ELF64_R_TYPE(rel->r_info);
809 addend = rel->r_addend;
810 switch(type) {
811 case R_SPARC_32:
812 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
813 rel->r_offset - start_offset, name, addend);
814 break;
815 case R_SPARC_HI22:
816 fprintf(outfile,
817 " *(uint32_t *)(gen_code_ptr + %d) = "
818 "((*(uint32_t *)(gen_code_ptr + %d)) "
819 " & ~0x3fffff) "
820 " | (((%s + %d) >> 10) & 0x3fffff);\n",
821 rel->r_offset - start_offset,
822 rel->r_offset - start_offset,
823 name, addend);
824 break;
825 case R_SPARC_LO10:
826 fprintf(outfile,
827 " *(uint32_t *)(gen_code_ptr + %d) = "
828 "((*(uint32_t *)(gen_code_ptr + %d)) "
829 " & ~0x3ff) "
830 " | ((%s + %d) & 0x3ff);\n",
831 rel->r_offset - start_offset,
832 rel->r_offset - start_offset,
833 name, addend);
834 break;
835 case R_SPARC_WDISP30:
836 fprintf(outfile,
837 " *(uint32_t *)(gen_code_ptr + %d) = "
838 "((*(uint32_t *)(gen_code_ptr + %d)) "
839 " & ~0x3fffffff) "
840 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
841 " & 0x3fffffff);\n",
842 rel->r_offset - start_offset,
843 rel->r_offset - start_offset,
844 name, addend,
845 rel->r_offset - start_offset);
846 break;
847 default:
848 error("unsupported sparc64 relocation (%d)", type);
849 }
850 }
851 }
852 }
853 #else
854 #error unsupported CPU
855 #endif
856 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
857 fprintf(outfile, "}\n");
858 fprintf(outfile, "break;\n\n");
859 } else {
860 fprintf(outfile, "static inline void gen_%s(", name);
861 if (nb_args == 0) {
862 fprintf(outfile, "void");
863 } else {
864 for(i = 0; i < nb_args; i++) {
865 if (i != 0)
866 fprintf(outfile, ", ");
867 fprintf(outfile, "long param%d", i + 1);
868 }
869 }
870 fprintf(outfile, ")\n");
871 fprintf(outfile, "{\n");
872 for(i = 0; i < nb_args; i++) {
873 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
874 }
875 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
876 fprintf(outfile, "}\n\n");
877 }
878 }
879
880 /* load an elf object file */
881 int load_elf(const char *filename, FILE *outfile, int do_print_enum)
882 {
883 int fd;
884 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
885 int i, j;
886 ElfW(Sym) *sym;
887 char *shstr;
888 uint8_t *text;
889 void *relocs;
890 int nb_relocs, reloc_sh_type;
891
892 fd = open(filename, O_RDONLY);
893 if (fd < 0)
894 error("can't open file '%s'", filename);
895
896 /* Read ELF header. */
897 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
898 error("unable to read file header");
899
900 /* Check ELF identification. */
901 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
902 || ehdr.e_ident[EI_MAG1] != ELFMAG1
903 || ehdr.e_ident[EI_MAG2] != ELFMAG2
904 || ehdr.e_ident[EI_MAG3] != ELFMAG3
905 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
906 error("bad ELF header");
907 }
908
909 do_swap = elf_must_swap(&ehdr);
910 if (do_swap)
911 elf_swap_ehdr(&ehdr);
912 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
913 error("Unsupported ELF class");
914 if (ehdr.e_type != ET_REL)
915 error("ELF object file expected");
916 if (ehdr.e_version != EV_CURRENT)
917 error("Invalid ELF version");
918 if (!elf_check_arch(ehdr.e_machine))
919 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
920
921 /* read section headers */
922 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
923 if (do_swap) {
924 for(i = 0; i < ehdr.e_shnum; i++) {
925 elf_swap_shdr(&shdr[i]);
926 }
927 }
928
929 sec = &shdr[ehdr.e_shstrndx];
930 shstr = load_data(fd, sec->sh_offset, sec->sh_size);
931
932 /* text section */
933
934 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
935 if (!text_sec)
936 error("could not find .text section");
937 text = load_data(fd, text_sec->sh_offset, text_sec->sh_size);
938
939 data_shndx = -1;
940 sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".data");
941 if (sec) {
942 data_shndx = sec - shdr;
943 data_data = load_data(fd, sec->sh_offset, sec->sh_size);
944 }
945 sdata_shndx = -1;
946 sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".sdata");
947 if (sec) {
948 sdata_shndx = sec - shdr;
949 sdata_data = load_data(fd, sec->sh_offset, sec->sh_size);
950 }
951
952 /* find text relocations, if any */
953 nb_relocs = 0;
954 relocs = NULL;
955 reloc_sh_type = 0;
956 for(i = 0; i < ehdr.e_shnum; i++) {
957 sec = &shdr[i];
958 if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) &&
959 sec->sh_info == (text_sec - shdr)) {
960 reloc_sh_type = sec->sh_type;
961 relocs = load_data(fd, sec->sh_offset, sec->sh_size);
962 nb_relocs = sec->sh_size / sec->sh_entsize;
963 if (do_swap) {
964 if (sec->sh_type == SHT_REL) {
965 ElfW(Rel) *rel = relocs;
966 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
967 swabls(&rel->r_offset);
968 swabls(&rel->r_info);
969 }
970 } else {
971 ElfW(Rela) *rel = relocs;
972 for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) {
973 swabls(&rel->r_offset);
974 swabls(&rel->r_info);
975 swabls(&rel->r_addend);
976 }
977 }
978 }
979 break;
980 }
981 }
982
983 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
984 if (!symtab_sec)
985 error("could not find .symtab section");
986 strtab_sec = &shdr[symtab_sec->sh_link];
987
988 symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size);
989 strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size);
990
991 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
992 if (do_swap) {
993 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
994 swab32s(&sym->st_name);
995 swabls(&sym->st_value);
996 swabls(&sym->st_size);
997 swab16s(&sym->st_shndx);
998 }
999 }
1000
1001 if (do_print_enum) {
1002 fprintf(outfile, "DEF(end, 0, 0)\n");
1003 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1004 const char *name, *p;
1005 name = strtab + sym->st_name;
1006 if (strstart(name, OP_PREFIX, &p)) {
1007 gen_code(name, sym->st_value, sym->st_size, outfile,
1008 text, relocs, nb_relocs, reloc_sh_type, 2);
1009 }
1010 }
1011 } else {
1012 /* generate big code generation switch */
1013 #ifdef HOST_ALPHA
1014 fprintf(outfile,
1015 "register int gp asm(\"$29\");\n"
1016 "static inline void immediate_ldah(void *p, int val) {\n"
1017 " uint32_t *dest = p;\n"
1018 " long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff;\n"
1019 "\n"
1020 " *dest &= ~0xffff;\n"
1021 " *dest |= high;\n"
1022 " *dest |= 31 << 16;\n"
1023 "}\n"
1024 "static inline void immediate_lda(void *dest, int val) {\n"
1025 " *(uint16_t *) dest = val;\n"
1026 "}\n"
1027 "void fix_bsr(void *p, int offset) {\n"
1028 " uint32_t *dest = p;\n"
1029 " *dest &= ~((1 << 21) - 1);\n"
1030 " *dest |= (offset >> 2) & ((1 << 21) - 1);\n"
1031 "}\n");
1032 #endif
1033 fprintf(outfile,
1034 "int dyngen_code(uint8_t *gen_code_buf,\n"
1035 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1036 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1037 "{\n"
1038 " uint8_t *gen_code_ptr;\n"
1039 " const uint16_t *opc_ptr;\n"
1040 " const uint32_t *opparam_ptr;\n"
1041 " gen_code_ptr = gen_code_buf;\n"
1042 " opc_ptr = opc_buf;\n"
1043 " opparam_ptr = opparam_buf;\n");
1044
1045 /* Generate prologue, if needed. */
1046 switch(ELF_ARCH) {
1047 case EM_SPARC:
1048 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c23a080; /* sub %%sp, 128, %%sp */\n");
1049 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc27a080; /* sub %%fp, 128, %%fp */\n");
1050 break;
1051
1052 case EM_SPARCV9:
1053 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c23a100; /* sub %%sp, 256, %%sp */\n");
1054 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc27a100; /* sub %%fp, 256, %%fp */\n");
1055 break;
1056 };
1057
1058 fprintf(outfile,
1059 " for(;;) {\n"
1060 " switch(*opc_ptr++) {\n"
1061 );
1062
1063 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1064 const char *name;
1065 name = strtab + sym->st_name;
1066 if (strstart(name, OP_PREFIX, NULL)) {
1067 #if 0
1068 printf("%4d: %s pos=0x%08x len=%d\n",
1069 i, name, sym->st_value, sym->st_size);
1070 #endif
1071 if (sym->st_shndx != (text_sec - shdr))
1072 error("invalid section for opcode (0x%x)", sym->st_shndx);
1073 gen_code(name, sym->st_value, sym->st_size, outfile,
1074 text, relocs, nb_relocs, reloc_sh_type, 1);
1075 }
1076 }
1077
1078 fprintf(outfile,
1079 " default:\n"
1080 " goto the_end;\n"
1081 " }\n"
1082 " }\n"
1083 " the_end:\n"
1084 );
1085
1086 /* generate epilogue */
1087 switch(ELF_ARCH) {
1088 case EM_386:
1089 fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n");
1090 break;
1091 case EM_PPC:
1092 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n");
1093 break;
1094 case EM_S390:
1095 fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n");
1096 break;
1097 case EM_ALPHA:
1098 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n");
1099 break;
1100 case EM_IA_64:
1101 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n");
1102 break;
1103 case EM_SPARC:
1104 case EM_SPARC32PLUS:
1105 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0xbc07a080; /* add %%fp, 256, %%fp */\n");
1106 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c62008; /* jmpl %%i0 + 8, %%g0 */\n");
1107 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x9c03a080; /* add %%sp, 256, %%sp */\n");
1108 break;
1109 case EM_SPARCV9:
1110 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81c7e008; /* ret */\n");
1111 fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x81e80000; /* restore */\n");
1112 break;
1113 default:
1114 error("unknown ELF architecture");
1115 }
1116
1117 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
1118 fprintf(outfile, "}\n\n");
1119
1120 /* generate gen_xxx functions */
1121 /* XXX: suppress the use of these functions to simplify code */
1122 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1123 const char *name;
1124 name = strtab + sym->st_name;
1125 if (strstart(name, OP_PREFIX, NULL)) {
1126 if (sym->st_shndx != (text_sec - shdr))
1127 error("invalid section for opcode (0x%x)", sym->st_shndx);
1128 gen_code(name, sym->st_value, sym->st_size, outfile,
1129 text, relocs, nb_relocs, reloc_sh_type, 0);
1130 }
1131 }
1132 }
1133
1134 close(fd);
1135 return 0;
1136 }
1137
1138 void usage(void)
1139 {
1140 printf("dyngen (c) 2003 Fabrice Bellard\n"
1141 "usage: dyngen [-o outfile] [-c] objfile\n"
1142 "Generate a dynamic code generator from an object file\n"
1143 "-c output enum of operations\n"
1144 );
1145 exit(1);
1146 }
1147
1148 int main(int argc, char **argv)
1149 {
1150 int c, do_print_enum;
1151 const char *filename, *outfilename;
1152 FILE *outfile;
1153
1154 outfilename = "out.c";
1155 do_print_enum = 0;
1156 for(;;) {
1157 c = getopt(argc, argv, "ho:c");
1158 if (c == -1)
1159 break;
1160 switch(c) {
1161 case 'h':
1162 usage();
1163 break;
1164 case 'o':
1165 outfilename = optarg;
1166 break;
1167 case 'c':
1168 do_print_enum = 1;
1169 break;
1170 }
1171 }
1172 if (optind >= argc)
1173 usage();
1174 filename = argv[optind];
1175 outfile = fopen(outfilename, "w");
1176 if (!outfile)
1177 error("could not open '%s'", outfilename);
1178 load_elf(filename, outfile, do_print_enum);
1179 fclose(outfile);
1180 return 0;
1181 }