]> git.proxmox.com Git - qemu.git/blob - dyngen.c
initial x86-64 host support (Gwenole Beauchesne)
[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-host.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_AMD64)
41
42 #define ELF_CLASS ELFCLASS64
43 #define ELF_ARCH EM_X86_64
44 #define elf_check_arch(x) ((x) == EM_X86_64)
45 #define ELF_USES_RELOCA
46
47 #elif defined(HOST_PPC)
48
49 #define ELF_CLASS ELFCLASS32
50 #define ELF_ARCH EM_PPC
51 #define elf_check_arch(x) ((x) == EM_PPC)
52 #define ELF_USES_RELOCA
53
54 #elif defined(HOST_S390)
55
56 #define ELF_CLASS ELFCLASS32
57 #define ELF_ARCH EM_S390
58 #define elf_check_arch(x) ((x) == EM_S390)
59 #define ELF_USES_RELOCA
60
61 #elif defined(HOST_ALPHA)
62
63 #define ELF_CLASS ELFCLASS64
64 #define ELF_ARCH EM_ALPHA
65 #define elf_check_arch(x) ((x) == EM_ALPHA)
66 #define ELF_USES_RELOCA
67
68 #elif defined(HOST_IA64)
69
70 #define ELF_CLASS ELFCLASS64
71 #define ELF_ARCH EM_IA_64
72 #define elf_check_arch(x) ((x) == EM_IA_64)
73 #define ELF_USES_RELOCA
74
75 #elif defined(HOST_SPARC)
76
77 #define ELF_CLASS ELFCLASS32
78 #define ELF_ARCH EM_SPARC
79 #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
80 #define ELF_USES_RELOCA
81
82 #elif defined(HOST_SPARC64)
83
84 #define ELF_CLASS ELFCLASS64
85 #define ELF_ARCH EM_SPARCV9
86 #define elf_check_arch(x) ((x) == EM_SPARCV9)
87 #define ELF_USES_RELOCA
88
89 #elif defined(HOST_ARM)
90
91 #define ELF_CLASS ELFCLASS32
92 #define ELF_ARCH EM_ARM
93 #define elf_check_arch(x) ((x) == EM_ARM)
94 #define ELF_USES_RELOC
95
96 #elif defined(HOST_M68K)
97
98 #define ELF_CLASS ELFCLASS32
99 #define ELF_ARCH EM_68K
100 #define elf_check_arch(x) ((x) == EM_68K)
101 #define ELF_USES_RELOCA
102
103 #else
104 #error unsupported CPU - please update the code
105 #endif
106
107 #include "elf.h"
108
109 #if ELF_CLASS == ELFCLASS32
110 typedef int32_t host_long;
111 typedef uint32_t host_ulong;
112 #define swabls(x) swab32s(x)
113 #else
114 typedef int64_t host_long;
115 typedef uint64_t host_ulong;
116 #define swabls(x) swab64s(x)
117 #endif
118
119 #ifdef ELF_USES_RELOCA
120 #define SHT_RELOC SHT_RELA
121 #else
122 #define SHT_RELOC SHT_REL
123 #endif
124
125 #include "bswap.h"
126
127 enum {
128 OUT_GEN_OP,
129 OUT_CODE,
130 OUT_INDEX_OP,
131 };
132
133 /* all dynamically generated functions begin with this code */
134 #define OP_PREFIX "op_"
135
136 int elf_must_swap(struct elfhdr *h)
137 {
138 union {
139 uint32_t i;
140 uint8_t b[4];
141 } swaptest;
142
143 swaptest.i = 1;
144 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
145 (swaptest.b[0] == 0);
146 }
147
148 void swab16s(uint16_t *p)
149 {
150 *p = bswap16(*p);
151 }
152
153 void swab32s(uint32_t *p)
154 {
155 *p = bswap32(*p);
156 }
157
158 void swab64s(uint64_t *p)
159 {
160 *p = bswap64(*p);
161 }
162
163 void elf_swap_ehdr(struct elfhdr *h)
164 {
165 swab16s(&h->e_type); /* Object file type */
166 swab16s(&h-> e_machine); /* Architecture */
167 swab32s(&h-> e_version); /* Object file version */
168 swabls(&h-> e_entry); /* Entry point virtual address */
169 swabls(&h-> e_phoff); /* Program header table file offset */
170 swabls(&h-> e_shoff); /* Section header table file offset */
171 swab32s(&h-> e_flags); /* Processor-specific flags */
172 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
173 swab16s(&h-> e_phentsize); /* Program header table entry size */
174 swab16s(&h-> e_phnum); /* Program header table entry count */
175 swab16s(&h-> e_shentsize); /* Section header table entry size */
176 swab16s(&h-> e_shnum); /* Section header table entry count */
177 swab16s(&h-> e_shstrndx); /* Section header string table index */
178 }
179
180 void elf_swap_shdr(struct elf_shdr *h)
181 {
182 swab32s(&h-> sh_name); /* Section name (string tbl index) */
183 swab32s(&h-> sh_type); /* Section type */
184 swabls(&h-> sh_flags); /* Section flags */
185 swabls(&h-> sh_addr); /* Section virtual addr at execution */
186 swabls(&h-> sh_offset); /* Section file offset */
187 swabls(&h-> sh_size); /* Section size in bytes */
188 swab32s(&h-> sh_link); /* Link to another section */
189 swab32s(&h-> sh_info); /* Additional section information */
190 swabls(&h-> sh_addralign); /* Section alignment */
191 swabls(&h-> sh_entsize); /* Entry size if section holds table */
192 }
193
194 void elf_swap_phdr(struct elf_phdr *h)
195 {
196 swab32s(&h->p_type); /* Segment type */
197 swabls(&h->p_offset); /* Segment file offset */
198 swabls(&h->p_vaddr); /* Segment virtual address */
199 swabls(&h->p_paddr); /* Segment physical address */
200 swabls(&h->p_filesz); /* Segment size in file */
201 swabls(&h->p_memsz); /* Segment size in memory */
202 swab32s(&h->p_flags); /* Segment flags */
203 swabls(&h->p_align); /* Segment alignment */
204 }
205
206 void elf_swap_rel(ELF_RELOC *rel)
207 {
208 swabls(&rel->r_offset);
209 swabls(&rel->r_info);
210 #ifdef ELF_USES_RELOCA
211 swabls(&rel->r_addend);
212 #endif
213 }
214
215 /* ELF file info */
216 int do_swap;
217 struct elf_shdr *shdr;
218 uint8_t **sdata;
219 struct elfhdr ehdr;
220 ElfW(Sym) *symtab;
221 int nb_syms;
222 char *strtab;
223 int text_shndx;
224
225 uint16_t get16(uint16_t *p)
226 {
227 uint16_t val;
228 val = *p;
229 if (do_swap)
230 val = bswap16(val);
231 return val;
232 }
233
234 uint32_t get32(uint32_t *p)
235 {
236 uint32_t val;
237 val = *p;
238 if (do_swap)
239 val = bswap32(val);
240 return val;
241 }
242
243 void put16(uint16_t *p, uint16_t val)
244 {
245 if (do_swap)
246 val = bswap16(val);
247 *p = val;
248 }
249
250 void put32(uint32_t *p, uint32_t val)
251 {
252 if (do_swap)
253 val = bswap32(val);
254 *p = val;
255 }
256
257 void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
258 {
259 va_list ap;
260 va_start(ap, fmt);
261 fprintf(stderr, "dyngen: ");
262 vfprintf(stderr, fmt, ap);
263 fprintf(stderr, "\n");
264 va_end(ap);
265 exit(1);
266 }
267
268
269 struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
270 const char *name)
271 {
272 int i;
273 const char *shname;
274 struct elf_shdr *sec;
275
276 for(i = 0; i < shnum; i++) {
277 sec = &shdr[i];
278 if (!sec->sh_name)
279 continue;
280 shname = shstr + sec->sh_name;
281 if (!strcmp(shname, name))
282 return sec;
283 }
284 return NULL;
285 }
286
287 int find_reloc(int sh_index)
288 {
289 struct elf_shdr *sec;
290 int i;
291
292 for(i = 0; i < ehdr.e_shnum; i++) {
293 sec = &shdr[i];
294 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
295 return i;
296 }
297 return 0;
298 }
299
300 void *load_data(int fd, long offset, unsigned int size)
301 {
302 char *data;
303
304 data = malloc(size);
305 if (!data)
306 return NULL;
307 lseek(fd, offset, SEEK_SET);
308 if (read(fd, data, size) != size) {
309 free(data);
310 return NULL;
311 }
312 return data;
313 }
314
315 int strstart(const char *str, const char *val, const char **ptr)
316 {
317 const char *p, *q;
318 p = str;
319 q = val;
320 while (*q != '\0') {
321 if (*p != *q)
322 return 0;
323 p++;
324 q++;
325 }
326 if (ptr)
327 *ptr = p;
328 return 1;
329 }
330
331 #ifdef HOST_ARM
332
333 int arm_emit_ldr_info(const char *name, unsigned long start_offset,
334 FILE *outfile, uint8_t *p_start, uint8_t *p_end,
335 ELF_RELOC *relocs, int nb_relocs)
336 {
337 uint8_t *p;
338 uint32_t insn;
339 int offset, min_offset, pc_offset, data_size;
340 uint8_t data_allocated[1024];
341 unsigned int data_index;
342
343 memset(data_allocated, 0, sizeof(data_allocated));
344
345 p = p_start;
346 min_offset = p_end - p_start;
347 while (p < p_start + min_offset) {
348 insn = get32((uint32_t *)p);
349 if ((insn & 0x0d5f0000) == 0x051f0000) {
350 /* ldr reg, [pc, #im] */
351 offset = insn & 0xfff;
352 if (!(insn & 0x00800000))
353 offset = -offset;
354 if ((offset & 3) !=0)
355 error("%s:%04x: ldr pc offset must be 32 bit aligned",
356 name, start_offset + p - p_start);
357 pc_offset = p - p_start + offset + 8;
358 if (pc_offset <= (p - p_start) ||
359 pc_offset >= (p_end - p_start))
360 error("%s:%04x: ldr pc offset must point inside the function code",
361 name, start_offset + p - p_start);
362 if (pc_offset < min_offset)
363 min_offset = pc_offset;
364 if (outfile) {
365 /* ldr position */
366 fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
367 p - p_start);
368 /* ldr data index */
369 data_index = ((p_end - p_start) - pc_offset - 4) >> 2;
370 fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
371 data_index);
372 fprintf(outfile, " arm_ldr_ptr++;\n");
373 if (data_index >= sizeof(data_allocated))
374 error("%s: too many data", name);
375 if (!data_allocated[data_index]) {
376 ELF_RELOC *rel;
377 int i, addend, type;
378 const char *sym_name, *p;
379 char relname[1024];
380
381 data_allocated[data_index] = 1;
382
383 /* data value */
384 addend = get32((uint32_t *)(p_start + pc_offset));
385 relname[0] = '\0';
386 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
387 if (rel->r_offset == (pc_offset + start_offset)) {
388 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
389 /* the compiler leave some unnecessary references to the code */
390 if (strstart(sym_name, "__op_param", &p)) {
391 snprintf(relname, sizeof(relname), "param%s", p);
392 } else {
393 snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name);
394 }
395 type = ELF32_R_TYPE(rel->r_info);
396 if (type != R_ARM_ABS32)
397 error("%s: unsupported data relocation", name);
398 break;
399 }
400 }
401 fprintf(outfile, " arm_data_ptr[%d] = 0x%x",
402 data_index, addend);
403 if (relname[0] != '\0')
404 fprintf(outfile, " + %s", relname);
405 fprintf(outfile, ";\n");
406 }
407 }
408 }
409 p += 4;
410 }
411 data_size = (p_end - p_start) - min_offset;
412 if (data_size > 0 && outfile) {
413 fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2);
414 }
415
416 /* the last instruction must be a mov pc, lr */
417 if (p == p_start)
418 goto arm_ret_error;
419 p -= 4;
420 insn = get32((uint32_t *)p);
421 if ((insn & 0xffff0000) != 0xe91b0000) {
422 arm_ret_error:
423 if (!outfile)
424 printf("%s: invalid epilog\n", name);
425 }
426 return p - p_start;
427 }
428 #endif
429
430
431 #define MAX_ARGS 3
432
433 /* generate op code */
434 void gen_code(const char *name, host_ulong offset, host_ulong size,
435 FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs,
436 int gen_switch)
437 {
438 int copy_size = 0;
439 uint8_t *p_start, *p_end;
440 host_ulong start_offset;
441 int nb_args, i, n;
442 uint8_t args_present[MAX_ARGS];
443 const char *sym_name, *p;
444 ELF_RELOC *rel;
445
446 /* Compute exact size excluding prologue and epilogue instructions.
447 * Increment start_offset to skip epilogue instructions, then compute
448 * copy_size the indicate the size of the remaining instructions (in
449 * bytes).
450 */
451 p_start = text + offset;
452 p_end = p_start + size;
453 start_offset = offset;
454 switch(ELF_ARCH) {
455 case EM_386:
456 case EM_X86_64:
457 {
458 int len;
459 len = p_end - p_start;
460 if (len == 0)
461 error("empty code for %s", name);
462 if (p_end[-1] == 0xc3) {
463 len--;
464 } else {
465 error("ret or jmp expected at the end of %s", name);
466 }
467 copy_size = len;
468 }
469 break;
470 case EM_PPC:
471 {
472 uint8_t *p;
473 p = (void *)(p_end - 4);
474 if (p == p_start)
475 error("empty code for %s", name);
476 if (get32((uint32_t *)p) != 0x4e800020)
477 error("blr expected at the end of %s", name);
478 copy_size = p - p_start;
479 }
480 break;
481 case EM_S390:
482 {
483 uint8_t *p;
484 p = (void *)(p_end - 2);
485 if (p == p_start)
486 error("empty code for %s", name);
487 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
488 error("br %%r14 expected at the end of %s", name);
489 copy_size = p - p_start;
490 }
491 break;
492 case EM_ALPHA:
493 {
494 uint8_t *p;
495 p = p_end - 4;
496 #if 0
497 /* XXX: check why it occurs */
498 if (p == p_start)
499 error("empty code for %s", name);
500 #endif
501 if (get32((uint32_t *)p) != 0x6bfa8001)
502 error("ret expected at the end of %s", name);
503 copy_size = p - p_start;
504 }
505 break;
506 case EM_IA_64:
507 {
508 uint8_t *p;
509 p = (void *)(p_end - 4);
510 if (p == p_start)
511 error("empty code for %s", name);
512 /* br.ret.sptk.many b0;; */
513 /* 08 00 84 00 */
514 if (get32((uint32_t *)p) != 0x00840008)
515 error("br.ret.sptk.many b0;; expected at the end of %s", name);
516 copy_size = p - p_start;
517 }
518 break;
519 case EM_SPARC:
520 case EM_SPARC32PLUS:
521 {
522 uint32_t start_insn, end_insn1, end_insn2;
523 uint8_t *p;
524 p = (void *)(p_end - 8);
525 if (p <= p_start)
526 error("empty code for %s", name);
527 start_insn = get32((uint32_t *)(p_start + 0x0));
528 end_insn1 = get32((uint32_t *)(p + 0x0));
529 end_insn2 = get32((uint32_t *)(p + 0x4));
530 if ((start_insn & ~0x1fff) == 0x9de3a000) {
531 p_start += 0x4;
532 start_offset += 0x4;
533 if ((int)(start_insn | ~0x1fff) < -128)
534 error("Found bogus save at the start of %s", name);
535 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
536 error("ret; restore; not found at end of %s", name);
537 } else {
538 error("No save at the beginning of %s", name);
539 }
540 #if 0
541 /* Skip a preceeding nop, if present. */
542 if (p > p_start) {
543 skip_insn = get32((uint32_t *)(p - 0x4));
544 if (skip_insn == 0x01000000)
545 p -= 4;
546 }
547 #endif
548 copy_size = p - p_start;
549 }
550 break;
551 case EM_SPARCV9:
552 {
553 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
554 uint8_t *p;
555 p = (void *)(p_end - 8);
556 if (p <= p_start)
557 error("empty code for %s", name);
558 start_insn = get32((uint32_t *)(p_start + 0x0));
559 end_insn1 = get32((uint32_t *)(p + 0x0));
560 end_insn2 = get32((uint32_t *)(p + 0x4));
561 if ((start_insn & ~0x1fff) == 0x9de3a000) {
562 p_start += 0x4;
563 start_offset += 0x4;
564 if ((int)(start_insn | ~0x1fff) < -256)
565 error("Found bogus save at the start of %s", name);
566 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
567 error("ret; restore; not found at end of %s", name);
568 } else {
569 error("No save at the beginning of %s", name);
570 }
571
572 /* Skip a preceeding nop, if present. */
573 if (p > p_start) {
574 skip_insn = get32((uint32_t *)(p - 0x4));
575 if (skip_insn == 0x01000000)
576 p -= 4;
577 }
578
579 copy_size = p - p_start;
580 }
581 break;
582 #ifdef HOST_ARM
583 case EM_ARM:
584 if ((p_end - p_start) <= 16)
585 error("%s: function too small", name);
586 if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
587 (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
588 get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
589 error("%s: invalid prolog", name);
590 p_start += 12;
591 start_offset += 12;
592 copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end,
593 relocs, nb_relocs);
594 break;
595 #endif
596 case EM_68K:
597 {
598 uint8_t *p;
599 p = (void *)(p_end - 2);
600 if (p == p_start)
601 error("empty code for %s", name);
602 // remove NOP's, probably added for alignment
603 while ((get16((uint16_t *)p) == 0x4e71) &&
604 (p>p_start))
605 p -= 2;
606 if (get16((uint16_t *)p) != 0x4e75)
607 error("rts expected at the end of %s", name);
608 copy_size = p - p_start;
609 }
610 break;
611 default:
612 error("unknown ELF architecture");
613 }
614
615 /* compute the number of arguments by looking at the relocations */
616 for(i = 0;i < MAX_ARGS; i++)
617 args_present[i] = 0;
618
619 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
620 if (rel->r_offset >= start_offset &&
621 rel->r_offset < start_offset + (p_end - p_start)) {
622 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
623 if (strstart(sym_name, "__op_param", &p)) {
624 n = strtoul(p, NULL, 10);
625 if (n > MAX_ARGS)
626 error("too many arguments in %s", name);
627 args_present[n - 1] = 1;
628 }
629 }
630 }
631
632 nb_args = 0;
633 while (nb_args < MAX_ARGS && args_present[nb_args])
634 nb_args++;
635 for(i = nb_args; i < MAX_ARGS; i++) {
636 if (args_present[i])
637 error("inconsistent argument numbering in %s", name);
638 }
639
640 if (gen_switch == 2) {
641 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
642 } else if (gen_switch == 1) {
643
644 /* output C code */
645 fprintf(outfile, "case INDEX_%s: {\n", name);
646 if (nb_args > 0) {
647 fprintf(outfile, " long ");
648 for(i = 0; i < nb_args; i++) {
649 if (i != 0)
650 fprintf(outfile, ", ");
651 fprintf(outfile, "param%d", i + 1);
652 }
653 fprintf(outfile, ";\n");
654 }
655 fprintf(outfile, " extern void %s();\n", name);
656
657 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
658 if (rel->r_offset >= start_offset &&
659 rel->r_offset < start_offset + (p_end - p_start)) {
660 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
661 if (*sym_name &&
662 !strstart(sym_name, "__op_param", NULL) &&
663 !strstart(sym_name, "__op_jmp", NULL)) {
664 #if defined(HOST_SPARC)
665 if (sym_name[0] == '.') {
666 fprintf(outfile,
667 "extern char __dot_%s __asm__(\"%s\");\n",
668 sym_name+1, sym_name);
669 continue;
670 }
671 #endif
672 fprintf(outfile, "extern char %s;\n", sym_name);
673 }
674 }
675 }
676
677 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
678
679 /* emit code offset information */
680 {
681 ElfW(Sym) *sym;
682 const char *sym_name, *p;
683 unsigned long val;
684 int n;
685
686 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
687 sym_name = strtab + sym->st_name;
688 if (strstart(sym_name, "__op_label", &p)) {
689 uint8_t *ptr;
690 unsigned long offset;
691
692 /* test if the variable refers to a label inside
693 the code we are generating */
694 ptr = sdata[sym->st_shndx];
695 if (!ptr)
696 error("__op_labelN in invalid section");
697 offset = sym->st_value;
698 val = *(unsigned long *)(ptr + offset);
699 #ifdef ELF_USES_RELOCA
700 {
701 int reloc_shndx, nb_relocs1, j;
702
703 /* try to find a matching relocation */
704 reloc_shndx = find_reloc(sym->st_shndx);
705 if (reloc_shndx) {
706 nb_relocs1 = shdr[reloc_shndx].sh_size /
707 shdr[reloc_shndx].sh_entsize;
708 rel = (ELF_RELOC *)sdata[reloc_shndx];
709 for(j = 0; j < nb_relocs1; j++) {
710 if (rel->r_offset == offset) {
711 val = rel->r_addend;
712 break;
713 }
714 rel++;
715 }
716 }
717 }
718 #endif
719
720 if (val >= start_offset && val < start_offset + copy_size) {
721 n = strtol(p, NULL, 10);
722 fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
723 }
724 }
725 }
726 }
727
728 /* load parameres in variables */
729 for(i = 0; i < nb_args; i++) {
730 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
731 }
732
733 /* patch relocations */
734 #if defined(HOST_I386)
735 {
736 char name[256];
737 int type;
738 int addend;
739 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
740 if (rel->r_offset >= start_offset &&
741 rel->r_offset < start_offset + copy_size) {
742 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
743 if (strstart(sym_name, "__op_jmp", &p)) {
744 int n;
745 n = strtol(p, NULL, 10);
746 /* __op_jmp relocations are done at
747 runtime to do translated block
748 chaining: the offset of the instruction
749 needs to be stored */
750 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
751 n, rel->r_offset - start_offset);
752 continue;
753 }
754
755 if (strstart(sym_name, "__op_param", &p)) {
756 snprintf(name, sizeof(name), "param%s", p);
757 } else {
758 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
759 }
760 type = ELF32_R_TYPE(rel->r_info);
761 addend = get32((uint32_t *)(text + rel->r_offset));
762 switch(type) {
763 case R_386_32:
764 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
765 rel->r_offset - start_offset, name, addend);
766 break;
767 case R_386_PC32:
768 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
769 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
770 break;
771 default:
772 error("unsupported i386 relocation (%d)", type);
773 }
774 }
775 }
776 }
777 #elif defined(HOST_AMD64)
778 {
779 char name[256];
780 int type;
781 int addend;
782 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
783 if (rel->r_offset >= start_offset &&
784 rel->r_offset < start_offset + copy_size) {
785 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
786 if (strstart(sym_name, "__op_param", &p)) {
787 snprintf(name, sizeof(name), "param%s", p);
788 } else {
789 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
790 }
791 type = ELF32_R_TYPE(rel->r_info);
792 addend = rel->r_addend;
793 switch(type) {
794 case R_X86_64_32:
795 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
796 rel->r_offset - start_offset, name, addend);
797 break;
798 case R_X86_64_32S:
799 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
800 rel->r_offset - start_offset, name, addend);
801 break;
802 case R_X86_64_PC32:
803 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
804 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
805 break;
806 default:
807 error("unsupported AMD64 relocation (%d)", type);
808 }
809 }
810 }
811 }
812 #elif defined(HOST_PPC)
813 {
814 char name[256];
815 int type;
816 int addend;
817 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
818 if (rel->r_offset >= start_offset &&
819 rel->r_offset < start_offset + copy_size) {
820 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
821 if (strstart(sym_name, "__op_jmp", &p)) {
822 int n;
823 n = strtol(p, NULL, 10);
824 /* __op_jmp relocations are done at
825 runtime to do translated block
826 chaining: the offset of the instruction
827 needs to be stored */
828 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
829 n, rel->r_offset - start_offset);
830 continue;
831 }
832
833 if (strstart(sym_name, "__op_param", &p)) {
834 snprintf(name, sizeof(name), "param%s", p);
835 } else {
836 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
837 }
838 type = ELF32_R_TYPE(rel->r_info);
839 addend = rel->r_addend;
840 switch(type) {
841 case R_PPC_ADDR32:
842 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
843 rel->r_offset - start_offset, name, addend);
844 break;
845 case R_PPC_ADDR16_LO:
846 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
847 rel->r_offset - start_offset, name, addend);
848 break;
849 case R_PPC_ADDR16_HI:
850 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
851 rel->r_offset - start_offset, name, addend);
852 break;
853 case R_PPC_ADDR16_HA:
854 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
855 rel->r_offset - start_offset, name, addend);
856 break;
857 case R_PPC_REL24:
858 /* warning: must be at 32 MB distancy */
859 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
860 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
861 break;
862 default:
863 error("unsupported powerpc relocation (%d)", type);
864 }
865 }
866 }
867 }
868 #elif defined(HOST_S390)
869 {
870 char name[256];
871 int type;
872 int addend;
873 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
874 if (rel->r_offset >= start_offset &&
875 rel->r_offset < start_offset + copy_size) {
876 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
877 if (strstart(sym_name, "__op_param", &p)) {
878 snprintf(name, sizeof(name), "param%s", p);
879 } else {
880 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
881 }
882 type = ELF32_R_TYPE(rel->r_info);
883 addend = rel->r_addend;
884 switch(type) {
885 case R_390_32:
886 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
887 rel->r_offset - start_offset, name, addend);
888 break;
889 case R_390_16:
890 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
891 rel->r_offset - start_offset, name, addend);
892 break;
893 case R_390_8:
894 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
895 rel->r_offset - start_offset, name, addend);
896 break;
897 default:
898 error("unsupported s390 relocation (%d)", type);
899 }
900 }
901 }
902 }
903 #elif defined(HOST_ALPHA)
904 {
905 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
906 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
907 int type;
908
909 type = ELF64_R_TYPE(rel->r_info);
910 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
911 switch (type) {
912 case R_ALPHA_GPDISP:
913 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
914 as an immediate instead of constructing it from the pv or ra. */
915 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
916 rel->r_offset - start_offset);
917 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
918 rel->r_offset - start_offset + rel->r_addend);
919 break;
920 case R_ALPHA_LITUSE:
921 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
922 now, since some called functions (libc) need pv to be set up. */
923 break;
924 case R_ALPHA_HINT:
925 /* Branch target prediction hint. Ignore for now. Should be already
926 correct for in-function jumps. */
927 break;
928 case R_ALPHA_LITERAL:
929 /* Load a literal from the GOT relative to the gp. Since there's only a
930 single gp, nothing is to be done. */
931 break;
932 case R_ALPHA_GPRELHIGH:
933 /* Handle fake relocations against __op_param symbol. Need to emit the
934 high part of the immediate value instead. Other symbols need no
935 special treatment. */
936 if (strstart(sym_name, "__op_param", &p))
937 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
938 rel->r_offset - start_offset, p);
939 break;
940 case R_ALPHA_GPRELLOW:
941 if (strstart(sym_name, "__op_param", &p))
942 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
943 rel->r_offset - start_offset, p);
944 break;
945 case R_ALPHA_BRSGP:
946 /* PC-relative jump. Tweak offset to skip the two instructions that try to
947 set up the gp from the pv. */
948 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
949 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
950 break;
951 default:
952 error("unsupported Alpha relocation (%d)", type);
953 }
954 }
955 }
956 }
957 #elif defined(HOST_IA64)
958 {
959 char name[256];
960 int type;
961 int addend;
962 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
963 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
964 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
965 if (strstart(sym_name, "__op_param", &p)) {
966 snprintf(name, sizeof(name), "param%s", p);
967 } else {
968 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
969 }
970 type = ELF64_R_TYPE(rel->r_info);
971 addend = rel->r_addend;
972 switch(type) {
973 case R_IA64_LTOFF22:
974 error("must implemnt R_IA64_LTOFF22 relocation");
975 case R_IA64_PCREL21B:
976 error("must implemnt R_IA64_PCREL21B relocation");
977 default:
978 error("unsupported ia64 relocation (%d)", type);
979 }
980 }
981 }
982 }
983 #elif defined(HOST_SPARC)
984 {
985 char name[256];
986 int type;
987 int addend;
988 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
989 if (rel->r_offset >= start_offset &&
990 rel->r_offset < start_offset + copy_size) {
991 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
992 if (strstart(sym_name, "__op_param", &p)) {
993 snprintf(name, sizeof(name), "param%s", p);
994 } else {
995 if (sym_name[0] == '.')
996 snprintf(name, sizeof(name),
997 "(long)(&__dot_%s)",
998 sym_name + 1);
999 else
1000 snprintf(name, sizeof(name),
1001 "(long)(&%s)", sym_name);
1002 }
1003 type = ELF32_R_TYPE(rel->r_info);
1004 addend = rel->r_addend;
1005 switch(type) {
1006 case R_SPARC_32:
1007 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1008 rel->r_offset - start_offset, name, addend);
1009 break;
1010 case R_SPARC_HI22:
1011 fprintf(outfile,
1012 " *(uint32_t *)(gen_code_ptr + %d) = "
1013 "((*(uint32_t *)(gen_code_ptr + %d)) "
1014 " & ~0x3fffff) "
1015 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1016 rel->r_offset - start_offset,
1017 rel->r_offset - start_offset,
1018 name, addend);
1019 break;
1020 case R_SPARC_LO10:
1021 fprintf(outfile,
1022 " *(uint32_t *)(gen_code_ptr + %d) = "
1023 "((*(uint32_t *)(gen_code_ptr + %d)) "
1024 " & ~0x3ff) "
1025 " | ((%s + %d) & 0x3ff);\n",
1026 rel->r_offset - start_offset,
1027 rel->r_offset - start_offset,
1028 name, addend);
1029 break;
1030 case R_SPARC_WDISP30:
1031 fprintf(outfile,
1032 " *(uint32_t *)(gen_code_ptr + %d) = "
1033 "((*(uint32_t *)(gen_code_ptr + %d)) "
1034 " & ~0x3fffffff) "
1035 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1036 " & 0x3fffffff);\n",
1037 rel->r_offset - start_offset,
1038 rel->r_offset - start_offset,
1039 name, addend,
1040 rel->r_offset - start_offset);
1041 break;
1042 default:
1043 error("unsupported sparc relocation (%d)", type);
1044 }
1045 }
1046 }
1047 }
1048 #elif defined(HOST_SPARC64)
1049 {
1050 char name[256];
1051 int type;
1052 int addend;
1053 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1054 if (rel->r_offset >= start_offset &&
1055 rel->r_offset < start_offset + copy_size) {
1056 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
1057 if (strstart(sym_name, "__op_param", &p)) {
1058 snprintf(name, sizeof(name), "param%s", p);
1059 } else {
1060 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1061 }
1062 type = ELF64_R_TYPE(rel->r_info);
1063 addend = rel->r_addend;
1064 switch(type) {
1065 case R_SPARC_32:
1066 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1067 rel->r_offset - start_offset, name, addend);
1068 break;
1069 case R_SPARC_HI22:
1070 fprintf(outfile,
1071 " *(uint32_t *)(gen_code_ptr + %d) = "
1072 "((*(uint32_t *)(gen_code_ptr + %d)) "
1073 " & ~0x3fffff) "
1074 " | (((%s + %d) >> 10) & 0x3fffff);\n",
1075 rel->r_offset - start_offset,
1076 rel->r_offset - start_offset,
1077 name, addend);
1078 break;
1079 case R_SPARC_LO10:
1080 fprintf(outfile,
1081 " *(uint32_t *)(gen_code_ptr + %d) = "
1082 "((*(uint32_t *)(gen_code_ptr + %d)) "
1083 " & ~0x3ff) "
1084 " | ((%s + %d) & 0x3ff);\n",
1085 rel->r_offset - start_offset,
1086 rel->r_offset - start_offset,
1087 name, addend);
1088 break;
1089 case R_SPARC_WDISP30:
1090 fprintf(outfile,
1091 " *(uint32_t *)(gen_code_ptr + %d) = "
1092 "((*(uint32_t *)(gen_code_ptr + %d)) "
1093 " & ~0x3fffffff) "
1094 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1095 " & 0x3fffffff);\n",
1096 rel->r_offset - start_offset,
1097 rel->r_offset - start_offset,
1098 name, addend,
1099 rel->r_offset - start_offset);
1100 break;
1101 default:
1102 error("unsupported sparc64 relocation (%d)", type);
1103 }
1104 }
1105 }
1106 }
1107 #elif defined(HOST_ARM)
1108 {
1109 char name[256];
1110 int type;
1111 int addend;
1112
1113 arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
1114 relocs, nb_relocs);
1115
1116 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1117 if (rel->r_offset >= start_offset &&
1118 rel->r_offset < start_offset + copy_size) {
1119 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1120 /* the compiler leave some unnecessary references to the code */
1121 if (sym_name[0] == '\0')
1122 continue;
1123 if (strstart(sym_name, "__op_param", &p)) {
1124 snprintf(name, sizeof(name), "param%s", p);
1125 } else {
1126 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1127 }
1128 type = ELF32_R_TYPE(rel->r_info);
1129 addend = get32((uint32_t *)(text + rel->r_offset));
1130 switch(type) {
1131 case R_ARM_ABS32:
1132 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1133 rel->r_offset - start_offset, name, addend);
1134 break;
1135 case R_ARM_PC24:
1136 fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
1137 rel->r_offset - start_offset, addend, name);
1138 break;
1139 default:
1140 error("unsupported arm relocation (%d)", type);
1141 }
1142 }
1143 }
1144 }
1145 #elif defined(HOST_M68K)
1146 {
1147 char name[256];
1148 int type;
1149 int addend;
1150 Elf32_Sym *sym;
1151 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1152 if (rel->r_offset >= start_offset &&
1153 rel->r_offset < start_offset + copy_size) {
1154 sym = &(symtab[ELFW(R_SYM)(rel->r_info)]);
1155 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1156 if (strstart(sym_name, "__op_param", &p)) {
1157 snprintf(name, sizeof(name), "param%s", p);
1158 } else {
1159 snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1160 }
1161 type = ELF32_R_TYPE(rel->r_info);
1162 addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend;
1163 switch(type) {
1164 case R_68K_32:
1165 fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ;
1166 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
1167 rel->r_offset - start_offset, name, addend );
1168 break;
1169 case R_68K_PC32:
1170 fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset);
1171 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
1172 rel->r_offset - start_offset, name, rel->r_offset - start_offset, /*sym->st_value+*/ addend);
1173 break;
1174 default:
1175 error("unsupported m68k relocation (%d)", type);
1176 }
1177 }
1178 }
1179 }
1180 #else
1181 #error unsupported CPU
1182 #endif
1183 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
1184 fprintf(outfile, "}\n");
1185 fprintf(outfile, "break;\n\n");
1186 } else {
1187 fprintf(outfile, "static inline void gen_%s(", name);
1188 if (nb_args == 0) {
1189 fprintf(outfile, "void");
1190 } else {
1191 for(i = 0; i < nb_args; i++) {
1192 if (i != 0)
1193 fprintf(outfile, ", ");
1194 fprintf(outfile, "long param%d", i + 1);
1195 }
1196 }
1197 fprintf(outfile, ")\n");
1198 fprintf(outfile, "{\n");
1199 for(i = 0; i < nb_args; i++) {
1200 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
1201 }
1202 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
1203 fprintf(outfile, "}\n\n");
1204 }
1205 }
1206
1207 /* load an elf object file */
1208 int load_elf(const char *filename, FILE *outfile, int out_type)
1209 {
1210 int fd;
1211 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
1212 int i, j;
1213 ElfW(Sym) *sym;
1214 char *shstr;
1215 uint8_t *text;
1216 ELF_RELOC *relocs;
1217 int nb_relocs;
1218 ELF_RELOC *rel;
1219
1220 fd = open(filename, O_RDONLY);
1221 if (fd < 0)
1222 error("can't open file '%s'", filename);
1223
1224 /* Read ELF header. */
1225 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
1226 error("unable to read file header");
1227
1228 /* Check ELF identification. */
1229 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
1230 || ehdr.e_ident[EI_MAG1] != ELFMAG1
1231 || ehdr.e_ident[EI_MAG2] != ELFMAG2
1232 || ehdr.e_ident[EI_MAG3] != ELFMAG3
1233 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
1234 error("bad ELF header");
1235 }
1236
1237 do_swap = elf_must_swap(&ehdr);
1238 if (do_swap)
1239 elf_swap_ehdr(&ehdr);
1240 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
1241 error("Unsupported ELF class");
1242 if (ehdr.e_type != ET_REL)
1243 error("ELF object file expected");
1244 if (ehdr.e_version != EV_CURRENT)
1245 error("Invalid ELF version");
1246 if (!elf_check_arch(ehdr.e_machine))
1247 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
1248
1249 /* read section headers */
1250 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
1251 if (do_swap) {
1252 for(i = 0; i < ehdr.e_shnum; i++) {
1253 elf_swap_shdr(&shdr[i]);
1254 }
1255 }
1256
1257 /* read all section data */
1258 sdata = malloc(sizeof(void *) * ehdr.e_shnum);
1259 memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
1260
1261 for(i = 0;i < ehdr.e_shnum; i++) {
1262 sec = &shdr[i];
1263 if (sec->sh_type != SHT_NOBITS)
1264 sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
1265 }
1266
1267 sec = &shdr[ehdr.e_shstrndx];
1268 shstr = sdata[ehdr.e_shstrndx];
1269
1270 /* swap relocations */
1271 for(i = 0; i < ehdr.e_shnum; i++) {
1272 sec = &shdr[i];
1273 if (sec->sh_type == SHT_RELOC) {
1274 nb_relocs = sec->sh_size / sec->sh_entsize;
1275 if (do_swap) {
1276 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
1277 elf_swap_rel(rel);
1278 }
1279 }
1280 }
1281 /* text section */
1282
1283 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
1284 if (!text_sec)
1285 error("could not find .text section");
1286 text_shndx = text_sec - shdr;
1287 text = sdata[text_shndx];
1288
1289 /* find text relocations, if any */
1290 relocs = NULL;
1291 nb_relocs = 0;
1292 i = find_reloc(text_shndx);
1293 if (i != 0) {
1294 relocs = (ELF_RELOC *)sdata[i];
1295 nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
1296 }
1297
1298 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
1299 if (!symtab_sec)
1300 error("could not find .symtab section");
1301 strtab_sec = &shdr[symtab_sec->sh_link];
1302
1303 symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
1304 strtab = sdata[symtab_sec->sh_link];
1305
1306 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
1307 if (do_swap) {
1308 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1309 swab32s(&sym->st_name);
1310 swabls(&sym->st_value);
1311 swabls(&sym->st_size);
1312 swab16s(&sym->st_shndx);
1313 }
1314 }
1315
1316 if (out_type == OUT_INDEX_OP) {
1317 fprintf(outfile, "DEF(end, 0, 0)\n");
1318 fprintf(outfile, "DEF(nop, 0, 0)\n");
1319 fprintf(outfile, "DEF(nop1, 1, 0)\n");
1320 fprintf(outfile, "DEF(nop2, 2, 0)\n");
1321 fprintf(outfile, "DEF(nop3, 3, 0)\n");
1322 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1323 const char *name, *p;
1324 name = strtab + sym->st_name;
1325 if (strstart(name, OP_PREFIX, &p)) {
1326 gen_code(name, sym->st_value, sym->st_size, outfile,
1327 text, relocs, nb_relocs, 2);
1328 }
1329 }
1330 } else if (out_type == OUT_GEN_OP) {
1331 /* generate gen_xxx functions */
1332
1333 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1334 const char *name;
1335 name = strtab + sym->st_name;
1336 if (strstart(name, OP_PREFIX, NULL)) {
1337 if (sym->st_shndx != (text_sec - shdr))
1338 error("invalid section for opcode (0x%x)", sym->st_shndx);
1339 gen_code(name, sym->st_value, sym->st_size, outfile,
1340 text, relocs, nb_relocs, 0);
1341 }
1342 }
1343
1344 } else {
1345 /* generate big code generation switch */
1346 fprintf(outfile,
1347 "int dyngen_code(uint8_t *gen_code_buf,\n"
1348 " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1349 " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1350 "{\n"
1351 " uint8_t *gen_code_ptr;\n"
1352 " const uint16_t *opc_ptr;\n"
1353 " const uint32_t *opparam_ptr;\n");
1354
1355 #ifdef HOST_ARM
1356 fprintf(outfile,
1357 " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1358 " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1359 " uint32_t *arm_data_ptr = arm_data_table;\n");
1360 #endif
1361
1362 fprintf(outfile,
1363 "\n"
1364 " gen_code_ptr = gen_code_buf;\n"
1365 " opc_ptr = opc_buf;\n"
1366 " opparam_ptr = opparam_buf;\n");
1367
1368 /* Generate prologue, if needed. */
1369
1370 fprintf(outfile,
1371 " for(;;) {\n"
1372 " switch(*opc_ptr++) {\n"
1373 );
1374
1375 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1376 const char *name;
1377 name = strtab + sym->st_name;
1378 if (strstart(name, OP_PREFIX, NULL)) {
1379 #if 0
1380 printf("%4d: %s pos=0x%08x len=%d\n",
1381 i, name, sym->st_value, sym->st_size);
1382 #endif
1383 if (sym->st_shndx != (text_sec - shdr))
1384 error("invalid section for opcode (0x%x)", sym->st_shndx);
1385 gen_code(name, sym->st_value, sym->st_size, outfile,
1386 text, relocs, nb_relocs, 1);
1387 }
1388 }
1389
1390 fprintf(outfile,
1391 " case INDEX_op_nop:\n"
1392 " break;\n"
1393 " case INDEX_op_nop1:\n"
1394 " opparam_ptr++;\n"
1395 " break;\n"
1396 " case INDEX_op_nop2:\n"
1397 " opparam_ptr += 2;\n"
1398 " break;\n"
1399 " case INDEX_op_nop3:\n"
1400 " opparam_ptr += 3;\n"
1401 " break;\n"
1402 " default:\n"
1403 " goto the_end;\n"
1404 " }\n");
1405
1406 #ifdef HOST_ARM
1407 /* generate constant table if needed */
1408 fprintf(outfile,
1409 " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1410 " gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1411 " last_gen_code_ptr = gen_code_ptr;\n"
1412 " arm_ldr_ptr = arm_ldr_table;\n"
1413 " arm_data_ptr = arm_data_table;\n"
1414 " }\n");
1415 #endif
1416
1417
1418 fprintf(outfile,
1419 " }\n"
1420 " the_end:\n"
1421 );
1422
1423 /* generate some code patching */
1424 #ifdef HOST_ARM
1425 fprintf(outfile, "gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 0);\n");
1426 #endif
1427 /* flush instruction cache */
1428 fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
1429
1430 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
1431 fprintf(outfile, "}\n\n");
1432
1433 }
1434
1435 close(fd);
1436 return 0;
1437 }
1438
1439 void usage(void)
1440 {
1441 printf("dyngen (c) 2003 Fabrice Bellard\n"
1442 "usage: dyngen [-o outfile] [-c] objfile\n"
1443 "Generate a dynamic code generator from an object file\n"
1444 "-c output enum of operations\n"
1445 "-g output gen_op_xx() functions\n"
1446 );
1447 exit(1);
1448 }
1449
1450 int main(int argc, char **argv)
1451 {
1452 int c, out_type;
1453 const char *filename, *outfilename;
1454 FILE *outfile;
1455
1456 outfilename = "out.c";
1457 out_type = OUT_CODE;
1458 for(;;) {
1459 c = getopt(argc, argv, "ho:cg");
1460 if (c == -1)
1461 break;
1462 switch(c) {
1463 case 'h':
1464 usage();
1465 break;
1466 case 'o':
1467 outfilename = optarg;
1468 break;
1469 case 'c':
1470 out_type = OUT_INDEX_OP;
1471 break;
1472 case 'g':
1473 out_type = OUT_GEN_OP;
1474 break;
1475 }
1476 }
1477 if (optind >= argc)
1478 usage();
1479 filename = argv[optind];
1480 outfile = fopen(outfilename, "w");
1481 if (!outfile)
1482 error("could not open '%s'", outfilename);
1483 load_elf(filename, outfile, out_type);
1484 fclose(outfile);
1485 return 0;
1486 }