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