VirtualBox

source: vbox/trunk/src/recompiler/dyngen.c@ 204

最後變更 在這個檔案從204是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
檔案大小: 83.3 KB
 
1/*
2 * Generic Dynamic compiler generator
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * The COFF object format support was extracted from Kazu's QEMU port
7 * to Win32.
8 *
9 * Mach-O Support by Matt Reda and Pierre d'Herbemont
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <stdarg.h>
29#include <inttypes.h>
30#include <unistd.h>
31#include <fcntl.h>
32
33#include "config-host.h"
34
35/* NOTE: we test CONFIG_WIN32 instead of _WIN32 to enabled cross
36 compilation */
37#if defined(CONFIG_WIN32)
38#define CONFIG_FORMAT_COFF
39#elif defined(CONFIG_DARWIN)
40#define CONFIG_FORMAT_MACH
41#elif defined(CONFIG_OS2)
42#define CONFIG_FORMAT_AOUT
43#else
44#define CONFIG_FORMAT_ELF
45#endif
46
47#ifdef CONFIG_FORMAT_ELF
48
49/* elf format definitions. We use these macros to test the CPU to
50 allow cross compilation (this tool must be ran on the build
51 platform) */
52#if defined(HOST_I386)
53
54#define ELF_CLASS ELFCLASS32
55#define ELF_ARCH EM_386
56#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
57#undef ELF_USES_RELOCA
58
59#elif defined(HOST_X86_64)
60
61#define ELF_CLASS ELFCLASS64
62#define ELF_ARCH EM_X86_64
63#define elf_check_arch(x) ((x) == EM_X86_64)
64#define ELF_USES_RELOCA
65
66#elif defined(HOST_PPC)
67
68#define ELF_CLASS ELFCLASS32
69#define ELF_ARCH EM_PPC
70#define elf_check_arch(x) ((x) == EM_PPC)
71#define ELF_USES_RELOCA
72
73#elif defined(HOST_S390)
74
75#define ELF_CLASS ELFCLASS32
76#define ELF_ARCH EM_S390
77#define elf_check_arch(x) ((x) == EM_S390)
78#define ELF_USES_RELOCA
79
80#elif defined(HOST_ALPHA)
81
82#define ELF_CLASS ELFCLASS64
83#define ELF_ARCH EM_ALPHA
84#define elf_check_arch(x) ((x) == EM_ALPHA)
85#define ELF_USES_RELOCA
86
87#elif defined(HOST_IA64)
88
89#define ELF_CLASS ELFCLASS64
90#define ELF_ARCH EM_IA_64
91#define elf_check_arch(x) ((x) == EM_IA_64)
92#define ELF_USES_RELOCA
93
94#elif defined(HOST_SPARC)
95
96#define ELF_CLASS ELFCLASS32
97#define ELF_ARCH EM_SPARC
98#define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
99#define ELF_USES_RELOCA
100
101#elif defined(HOST_SPARC64)
102
103#define ELF_CLASS ELFCLASS64
104#define ELF_ARCH EM_SPARCV9
105#define elf_check_arch(x) ((x) == EM_SPARCV9)
106#define ELF_USES_RELOCA
107
108#elif defined(HOST_ARM)
109
110#define ELF_CLASS ELFCLASS32
111#define ELF_ARCH EM_ARM
112#define elf_check_arch(x) ((x) == EM_ARM)
113#define ELF_USES_RELOC
114
115#elif defined(HOST_M68K)
116
117#define ELF_CLASS ELFCLASS32
118#define ELF_ARCH EM_68K
119#define elf_check_arch(x) ((x) == EM_68K)
120#define ELF_USES_RELOCA
121
122#else
123#error unsupported CPU - please update the code
124#endif
125
126#include "elf.h"
127
128#if ELF_CLASS == ELFCLASS32
129typedef int32_t host_long;
130typedef uint32_t host_ulong;
131#define swabls(x) swab32s(x)
132#else
133typedef int64_t host_long;
134typedef uint64_t host_ulong;
135#define swabls(x) swab64s(x)
136#endif
137
138#ifdef ELF_USES_RELOCA
139#define SHT_RELOC SHT_RELA
140#else
141#define SHT_RELOC SHT_REL
142#endif
143
144#define EXE_RELOC ELF_RELOC
145#define EXE_SYM ElfW(Sym)
146
147#endif /* CONFIG_FORMAT_ELF */
148
149#ifdef CONFIG_FORMAT_COFF
150
151#include "a.out.h"
152
153typedef int32_t host_long;
154typedef uint32_t host_ulong;
155
156#define FILENAMELEN 256
157
158typedef struct coff_sym {
159 struct external_syment *st_syment;
160 char st_name[FILENAMELEN];
161 uint32_t st_value;
162 int st_size;
163 uint8_t st_type;
164 uint8_t st_shndx;
165} coff_Sym;
166
167typedef struct coff_rel {
168 struct external_reloc *r_reloc;
169 int r_offset;
170 uint8_t r_type;
171} coff_Rel;
172
173#define EXE_RELOC struct coff_rel
174#define EXE_SYM struct coff_sym
175
176#endif /* CONFIG_FORMAT_COFF */
177
178#ifdef CONFIG_FORMAT_MACH
179
180#include <mach-o/loader.h>
181#include <mach-o/nlist.h>
182#include <mach-o/reloc.h>
183#include <mach-o/ppc/reloc.h>
184
185# define check_mach_header(x) (x.magic == MH_MAGIC)
186typedef int32_t host_long;
187typedef uint32_t host_ulong;
188
189struct nlist_extended
190{
191 union {
192 char *n_name;
193 long n_strx;
194 } n_un;
195 unsigned char n_type;
196 unsigned char n_sect;
197 short st_desc;
198 unsigned long st_value;
199 unsigned long st_size;
200};
201
202#define EXE_RELOC struct relocation_info
203#define EXE_SYM struct nlist_extended
204
205#endif /* CONFIG_FORMAT_MACH */
206
207#ifdef CONFIG_FORMAT_AOUT
208
209#include "a_out.h"
210
211typedef int32_t host_long;
212typedef uint32_t host_ulong;
213
214struct nlist_extended
215{
216 union {
217 char *n_name;
218 struct nlist *n_next;
219 long n_strx;
220 } n_un;
221 unsigned char n_type;
222 char n_other;
223 short n_desc;
224 unsigned long st_value; // n_value -> st_value
225 unsigned long st_size; // added
226};
227
228#define EXE_RELOC struct relocation_info
229#define EXE_SYM struct nlist_extended
230#define r_offset r_address
231
232#endif /* CONFIG_FORMAT_AOUT */
233
234#include "bswap.h"
235
236enum {
237 OUT_GEN_OP,
238 OUT_CODE,
239 OUT_INDEX_OP,
240};
241
242/* all dynamically generated functions begin with this code */
243#define OP_PREFIX "op_"
244
245int do_swap;
246
247void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
248{
249 va_list ap;
250 va_start(ap, fmt);
251 fprintf(stderr, "dyngen: ");
252 vfprintf(stderr, fmt, ap);
253 fprintf(stderr, "\n");
254 va_end(ap);
255 exit(1);
256}
257
258void *load_data(int fd, long offset, unsigned int size)
259{
260 char *data;
261
262 data = malloc(size);
263 if (!data)
264 return NULL;
265 lseek(fd, offset, SEEK_SET);
266 if (read(fd, data, size) != size) {
267 free(data);
268 return NULL;
269 }
270 return data;
271}
272
273int strstart(const char *str, const char *val, const char **ptr)
274{
275 const char *p, *q;
276 p = str;
277 q = val;
278 while (*q != '\0') {
279 if (*p != *q)
280 return 0;
281 p++;
282 q++;
283 }
284 if (ptr)
285 *ptr = p;
286 return 1;
287}
288
289void pstrcpy(char *buf, int buf_size, const char *str)
290{
291 int c;
292 char *q = buf;
293
294 if (buf_size <= 0)
295 return;
296
297 for(;;) {
298 c = *str++;
299 if (c == 0 || q >= buf + buf_size - 1)
300 break;
301 *q++ = c;
302 }
303 *q = '\0';
304}
305
306void swab16s(uint16_t *p)
307{
308 *p = bswap16(*p);
309}
310
311void swab32s(uint32_t *p)
312{
313 *p = bswap32(*p);
314}
315
316void swab64s(uint64_t *p)
317{
318 *p = bswap64(*p);
319}
320
321uint16_t get16(uint16_t *p)
322{
323 uint16_t val;
324 val = *p;
325 if (do_swap)
326 val = bswap16(val);
327 return val;
328}
329
330uint32_t get32(uint32_t *p)
331{
332 uint32_t val;
333 val = *p;
334 if (do_swap)
335 val = bswap32(val);
336 return val;
337}
338
339void put16(uint16_t *p, uint16_t val)
340{
341 if (do_swap)
342 val = bswap16(val);
343 *p = val;
344}
345
346void put32(uint32_t *p, uint32_t val)
347{
348 if (do_swap)
349 val = bswap32(val);
350 *p = val;
351}
352
353/* executable information */
354EXE_SYM *symtab;
355int nb_syms;
356int text_shndx;
357uint8_t *text;
358EXE_RELOC *relocs;
359int nb_relocs;
360
361#ifdef CONFIG_FORMAT_ELF
362
363/* ELF file info */
364struct elf_shdr *shdr;
365uint8_t **sdata;
366struct elfhdr ehdr;
367char *strtab;
368
369int elf_must_swap(struct elfhdr *h)
370{
371 union {
372 uint32_t i;
373 uint8_t b[4];
374 } swaptest;
375
376 swaptest.i = 1;
377 return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
378 (swaptest.b[0] == 0);
379}
380
381void elf_swap_ehdr(struct elfhdr *h)
382{
383 swab16s(&h->e_type); /* Object file type */
384 swab16s(&h-> e_machine); /* Architecture */
385 swab32s(&h-> e_version); /* Object file version */
386 swabls(&h-> e_entry); /* Entry point virtual address */
387 swabls(&h-> e_phoff); /* Program header table file offset */
388 swabls(&h-> e_shoff); /* Section header table file offset */
389 swab32s(&h-> e_flags); /* Processor-specific flags */
390 swab16s(&h-> e_ehsize); /* ELF header size in bytes */
391 swab16s(&h-> e_phentsize); /* Program header table entry size */
392 swab16s(&h-> e_phnum); /* Program header table entry count */
393 swab16s(&h-> e_shentsize); /* Section header table entry size */
394 swab16s(&h-> e_shnum); /* Section header table entry count */
395 swab16s(&h-> e_shstrndx); /* Section header string table index */
396}
397
398void elf_swap_shdr(struct elf_shdr *h)
399{
400 swab32s(&h-> sh_name); /* Section name (string tbl index) */
401 swab32s(&h-> sh_type); /* Section type */
402 swabls(&h-> sh_flags); /* Section flags */
403 swabls(&h-> sh_addr); /* Section virtual addr at execution */
404 swabls(&h-> sh_offset); /* Section file offset */
405 swabls(&h-> sh_size); /* Section size in bytes */
406 swab32s(&h-> sh_link); /* Link to another section */
407 swab32s(&h-> sh_info); /* Additional section information */
408 swabls(&h-> sh_addralign); /* Section alignment */
409 swabls(&h-> sh_entsize); /* Entry size if section holds table */
410}
411
412void elf_swap_phdr(struct elf_phdr *h)
413{
414 swab32s(&h->p_type); /* Segment type */
415 swabls(&h->p_offset); /* Segment file offset */
416 swabls(&h->p_vaddr); /* Segment virtual address */
417 swabls(&h->p_paddr); /* Segment physical address */
418 swabls(&h->p_filesz); /* Segment size in file */
419 swabls(&h->p_memsz); /* Segment size in memory */
420 swab32s(&h->p_flags); /* Segment flags */
421 swabls(&h->p_align); /* Segment alignment */
422}
423
424void elf_swap_rel(ELF_RELOC *rel)
425{
426 swabls(&rel->r_offset);
427 swabls(&rel->r_info);
428#ifdef ELF_USES_RELOCA
429 swabls(&rel->r_addend);
430#endif
431}
432
433struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
434 const char *name)
435{
436 int i;
437 const char *shname;
438 struct elf_shdr *sec;
439
440 for(i = 0; i < shnum; i++) {
441 sec = &shdr[i];
442 if (!sec->sh_name)
443 continue;
444 shname = shstr + sec->sh_name;
445 if (!strcmp(shname, name))
446 return sec;
447 }
448 return NULL;
449}
450
451int find_reloc(int sh_index)
452{
453 struct elf_shdr *sec;
454 int i;
455
456 for(i = 0; i < ehdr.e_shnum; i++) {
457 sec = &shdr[i];
458 if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
459 return i;
460 }
461 return 0;
462}
463
464static host_ulong get_rel_offset(EXE_RELOC *rel)
465{
466 return rel->r_offset;
467}
468
469static char *get_rel_sym_name(EXE_RELOC *rel)
470{
471 return strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
472}
473
474static char *get_sym_name(EXE_SYM *sym)
475{
476 return strtab + sym->st_name;
477}
478
479/* load an elf object file */
480int load_object(const char *filename)
481{
482 int fd;
483 struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
484 int i, j;
485 ElfW(Sym) *sym;
486 char *shstr;
487 ELF_RELOC *rel;
488
489 fd = open(filename, O_RDONLY);
490 if (fd < 0)
491 error("can't open file '%s'", filename);
492
493 /* Read ELF header. */
494 if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
495 error("unable to read file header");
496
497 /* Check ELF identification. */
498 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
499 || ehdr.e_ident[EI_MAG1] != ELFMAG1
500 || ehdr.e_ident[EI_MAG2] != ELFMAG2
501 || ehdr.e_ident[EI_MAG3] != ELFMAG3
502 || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
503 error("bad ELF header");
504 }
505
506 do_swap = elf_must_swap(&ehdr);
507 if (do_swap)
508 elf_swap_ehdr(&ehdr);
509 if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
510 error("Unsupported ELF class");
511 if (ehdr.e_type != ET_REL)
512 error("ELF object file expected");
513 if (ehdr.e_version != EV_CURRENT)
514 error("Invalid ELF version");
515 if (!elf_check_arch(ehdr.e_machine))
516 error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
517
518 /* read section headers */
519 shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
520 if (do_swap) {
521 for(i = 0; i < ehdr.e_shnum; i++) {
522 elf_swap_shdr(&shdr[i]);
523 }
524 }
525
526 /* read all section data */
527 sdata = malloc(sizeof(void *) * ehdr.e_shnum);
528 memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
529
530 for(i = 0;i < ehdr.e_shnum; i++) {
531 sec = &shdr[i];
532 if (sec->sh_type != SHT_NOBITS)
533 sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
534 }
535
536 sec = &shdr[ehdr.e_shstrndx];
537 shstr = sdata[ehdr.e_shstrndx];
538
539 /* swap relocations */
540 for(i = 0; i < ehdr.e_shnum; i++) {
541 sec = &shdr[i];
542 if (sec->sh_type == SHT_RELOC) {
543 nb_relocs = sec->sh_size / sec->sh_entsize;
544 if (do_swap) {
545 for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
546 elf_swap_rel(rel);
547 }
548 }
549 }
550 /* text section */
551
552 text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
553 if (!text_sec)
554 error("could not find .text section");
555 text_shndx = text_sec - shdr;
556 text = sdata[text_shndx];
557
558 /* find text relocations, if any */
559 relocs = NULL;
560 nb_relocs = 0;
561 i = find_reloc(text_shndx);
562 if (i != 0) {
563 relocs = (ELF_RELOC *)sdata[i];
564 nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
565 }
566
567 symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
568 if (!symtab_sec)
569 error("could not find .symtab section");
570 strtab_sec = &shdr[symtab_sec->sh_link];
571
572 symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
573 strtab = sdata[symtab_sec->sh_link];
574
575 nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
576 if (do_swap) {
577 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
578 swab32s(&sym->st_name);
579 swabls(&sym->st_value);
580 swabls(&sym->st_size);
581 swab16s(&sym->st_shndx);
582 }
583 }
584 close(fd);
585 return 0;
586}
587
588#endif /* CONFIG_FORMAT_ELF */
589
590#ifdef CONFIG_FORMAT_COFF
591
592/* COFF file info */
593struct external_scnhdr *shdr;
594uint8_t **sdata;
595struct external_filehdr fhdr;
596struct external_syment *coff_symtab;
597char *strtab;
598int coff_text_shndx, coff_data_shndx;
599
600int data_shndx;
601
602#define STRTAB_SIZE 4
603
604#define DIR32 0x06
605#define DISP32 0x14
606
607#define T_FUNCTION 0x20
608#define C_EXTERNAL 2
609
610void sym_ent_name(struct external_syment *ext_sym, EXE_SYM *sym)
611{
612 char *q;
613 int c, i, len;
614
615 if (ext_sym->e.e.e_zeroes != 0) {
616 q = sym->st_name;
617 for(i = 0; i < 8; i++) {
618 c = ext_sym->e.e_name[i];
619 if (c == '\0')
620 break;
621 *q++ = c;
622 }
623 *q = '\0';
624 } else {
625 pstrcpy(sym->st_name, sizeof(sym->st_name), strtab + ext_sym->e.e.e_offset);
626 }
627
628 /* now convert the name to a C name (suppress the leading '_') */
629 if (sym->st_name[0] == '_') {
630 len = strlen(sym->st_name);
631 memmove(sym->st_name, sym->st_name + 1, len - 1);
632 sym->st_name[len - 1] = '\0';
633 }
634}
635
636char *name_for_dotdata(struct coff_rel *rel)
637{
638 int i;
639 struct coff_sym *sym;
640 uint32_t text_data;
641
642 text_data = *(uint32_t *)(text + rel->r_offset);
643
644 for (i = 0, sym = symtab; i < nb_syms; i++, sym++) {
645 if (sym->st_syment->e_scnum == data_shndx &&
646 text_data >= sym->st_value &&
647 text_data < sym->st_value + sym->st_size) {
648
649 return sym->st_name;
650
651 }
652 }
653 return NULL;
654}
655
656static char *get_sym_name(EXE_SYM *sym)
657{
658 return sym->st_name;
659}
660
661static char *get_rel_sym_name(EXE_RELOC *rel)
662{
663 char *name;
664 name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx));
665 if (!strcmp(name, ".data"))
666 name = name_for_dotdata(rel);
667 return name;
668}
669
670static host_ulong get_rel_offset(EXE_RELOC *rel)
671{
672 return rel->r_offset;
673}
674
675struct external_scnhdr *find_coff_section(struct external_scnhdr *shdr, int shnum, const char *name)
676{
677 int i;
678 const char *shname;
679 struct external_scnhdr *sec;
680
681 for(i = 0; i < shnum; i++) {
682 sec = &shdr[i];
683 if (!sec->s_name)
684 continue;
685 shname = sec->s_name;
686 if (!strcmp(shname, name))
687 return sec;
688 }
689 return NULL;
690}
691
692/* load a coff object file */
693int load_object(const char *filename)
694{
695 int fd;
696 struct external_scnhdr *sec, *text_sec, *data_sec;
697 int i;
698 struct external_syment *ext_sym;
699 struct external_reloc *coff_relocs;
700 struct external_reloc *ext_rel;
701 uint32_t *n_strtab;
702 EXE_SYM *sym;
703 EXE_RELOC *rel;
704
705 fd = open(filename, O_RDONLY
706#ifdef _WIN32
707 | O_BINARY
708#endif
709 );
710 if (fd < 0)
711 error("can't open file '%s'", filename);
712
713 /* Read COFF header. */
714 if (read(fd, &fhdr, sizeof (fhdr)) != sizeof (fhdr))
715 error("unable to read file header");
716
717 /* Check COFF identification. */
718 if (fhdr.f_magic != I386MAGIC) {
719 error("bad COFF header");
720 }
721 do_swap = 0;
722
723 /* read section headers */
724 shdr = load_data(fd, sizeof(struct external_filehdr) + fhdr.f_opthdr, fhdr.f_nscns * sizeof(struct external_scnhdr));
725
726 /* read all section data */
727 sdata = malloc(sizeof(void *) * fhdr.f_nscns);
728 memset(sdata, 0, sizeof(void *) * fhdr.f_nscns);
729
730 const char *p;
731 for(i = 0;i < fhdr.f_nscns; i++) {
732 sec = &shdr[i];
733 if (!strstart(sec->s_name, ".bss", &p))
734 sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size);
735 }
736
737
738 /* text section */
739 text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text");
740 if (!text_sec)
741 error("could not find .text section");
742 coff_text_shndx = text_sec - shdr;
743 text = sdata[coff_text_shndx];
744
745 /* data section */
746 data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data");
747 if (!data_sec)
748 error("could not find .data section");
749 coff_data_shndx = data_sec - shdr;
750
751 coff_symtab = load_data(fd, fhdr.f_symptr, fhdr.f_nsyms*SYMESZ);
752 for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
753 for(i=0;i<8;i++)
754 printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]);
755 printf("\n");
756 }
757
758
759 n_strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), STRTAB_SIZE);
760 strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), *n_strtab);
761
762 nb_syms = fhdr.f_nsyms;
763
764 for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
765 if (strstart(ext_sym->e.e_name, ".text", NULL))
766 text_shndx = ext_sym->e_scnum;
767 if (strstart(ext_sym->e.e_name, ".data", NULL))
768 data_shndx = ext_sym->e_scnum;
769 }
770
771 /* set coff symbol */
772 symtab = malloc(sizeof(struct coff_sym) * nb_syms);
773
774 int aux_size, j;
775 for (i = 0, ext_sym = coff_symtab, sym = symtab; i < nb_syms; i++, ext_sym++, sym++) {
776 memset(sym, 0, sizeof(*sym));
777 sym->st_syment = ext_sym;
778 sym_ent_name(ext_sym, sym);
779 sym->st_value = ext_sym->e_value;
780
781 aux_size = *(int8_t *)ext_sym->e_numaux;
782 if (ext_sym->e_scnum == text_shndx && ext_sym->e_type == T_FUNCTION) {
783 for (j = aux_size + 1; j < nb_syms - i; j++) {
784 if ((ext_sym + j)->e_scnum == text_shndx &&
785 (ext_sym + j)->e_type == T_FUNCTION ){
786 sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
787 break;
788 } else if (j == nb_syms - i - 1) {
789 sec = &shdr[coff_text_shndx];
790 sym->st_size = sec->s_size - ext_sym->e_value;
791 break;
792 }
793 }
794 } else if (ext_sym->e_scnum == data_shndx && *(uint8_t *)ext_sym->e_sclass == C_EXTERNAL) {
795 for (j = aux_size + 1; j < nb_syms - i; j++) {
796 if ((ext_sym + j)->e_scnum == data_shndx) {
797 sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
798 break;
799 } else if (j == nb_syms - i - 1) {
800 sec = &shdr[coff_data_shndx];
801 sym->st_size = sec->s_size - ext_sym->e_value;
802 break;
803 }
804 }
805 } else {
806 sym->st_size = 0;
807 }
808
809 sym->st_type = ext_sym->e_type;
810 sym->st_shndx = ext_sym->e_scnum;
811 }
812
813
814 /* find text relocations, if any */
815 sec = &shdr[coff_text_shndx];
816 coff_relocs = load_data(fd, sec->s_relptr, sec->s_nreloc*RELSZ);
817 nb_relocs = sec->s_nreloc;
818
819 /* set coff relocation */
820 relocs = malloc(sizeof(struct coff_rel) * nb_relocs);
821 for (i = 0, ext_rel = coff_relocs, rel = relocs; i < nb_relocs;
822 i++, ext_rel++, rel++) {
823 memset(rel, 0, sizeof(*rel));
824 rel->r_reloc = ext_rel;
825 rel->r_offset = *(uint32_t *)ext_rel->r_vaddr;
826 rel->r_type = *(uint16_t *)ext_rel->r_type;
827 }
828 return 0;
829}
830
831#endif /* CONFIG_FORMAT_COFF */
832
833#ifdef CONFIG_FORMAT_MACH
834
835/* File Header */
836struct mach_header mach_hdr;
837
838/* commands */
839struct segment_command *segment = 0;
840struct dysymtab_command *dysymtabcmd = 0;
841struct symtab_command *symtabcmd = 0;
842
843/* section */
844struct section *section_hdr;
845struct section *text_sec_hdr;
846uint8_t **sdata;
847
848/* relocs */
849struct relocation_info *relocs;
850
851/* symbols */
852EXE_SYM *symtab;
853struct nlist *symtab_std;
854char *strtab;
855
856/* indirect symbols */
857uint32_t *tocdylib;
858
859/* Utility functions */
860
861static inline char *find_str_by_index(int index)
862{
863 return strtab+index;
864}
865
866/* Used by dyngen common code */
867static char *get_sym_name(EXE_SYM *sym)
868{
869 char *name = find_str_by_index(sym->n_un.n_strx);
870
871 if ( sym->n_type & N_STAB ) /* Debug symbols are ignored */
872 return "debug";
873
874 if(!name)
875 return name;
876 if(name[0]=='_')
877 return name + 1;
878 else
879 return name;
880}
881
882/* find a section index given its segname, sectname */
883static int find_mach_sec_index(struct section *section_hdr, int shnum, const char *segname,
884 const char *sectname)
885{
886 int i;
887 struct section *sec = section_hdr;
888
889 for(i = 0; i < shnum; i++, sec++) {
890 if (!sec->segname || !sec->sectname)
891 continue;
892 if (!strcmp(sec->sectname, sectname) && !strcmp(sec->segname, segname))
893 return i;
894 }
895 return -1;
896}
897
898/* find a section header given its segname, sectname */
899struct section *find_mach_sec_hdr(struct section *section_hdr, int shnum, const char *segname,
900 const char *sectname)
901{
902 int index = find_mach_sec_index(section_hdr, shnum, segname, sectname);
903 if(index == -1)
904 return NULL;
905 return section_hdr+index;
906}
907
908
909static inline void fetch_next_pair_value(struct relocation_info * rel, unsigned int *value)
910{
911 struct scattered_relocation_info * scarel;
912
913 if(R_SCATTERED & rel->r_address) {
914 scarel = (struct scattered_relocation_info*)rel;
915 if(scarel->r_type != PPC_RELOC_PAIR)
916 error("fetch_next_pair_value: looking for a pair which was not found (1)");
917 *value = scarel->r_value;
918 } else {
919 if(rel->r_type != PPC_RELOC_PAIR)
920 error("fetch_next_pair_value: looking for a pair which was not found (2)");
921 *value = rel->r_address;
922 }
923}
924
925/* find a sym name given its value, in a section number */
926static const char * find_sym_with_value_and_sec_number( int value, int sectnum, int * offset )
927{
928 int i, ret = -1;
929
930 for( i = 0 ; i < nb_syms; i++ )
931 {
932 if( !(symtab[i].n_type & N_STAB) && (symtab[i].n_type & N_SECT) &&
933 (symtab[i].n_sect == sectnum) && (symtab[i].st_value <= value) )
934 {
935 if( (ret<0) || (symtab[i].st_value >= symtab[ret].st_value) )
936 ret = i;
937 }
938 }
939 if( ret < 0 ) {
940 *offset = 0;
941 return 0;
942 } else {
943 *offset = value - symtab[ret].st_value;
944 return get_sym_name(&symtab[ret]);
945 }
946}
947
948/*
949 * Find symbol name given a (virtual) address, and a section which is of type
950 * S_NON_LAZY_SYMBOL_POINTERS or S_LAZY_SYMBOL_POINTERS or S_SYMBOL_STUBS
951 */
952static const char * find_reloc_name_in_sec_ptr(int address, struct section * sec_hdr)
953{
954 unsigned int tocindex, symindex, size;
955 const char *name = 0;
956
957 /* Sanity check */
958 if(!( address >= sec_hdr->addr && address < (sec_hdr->addr + sec_hdr->size) ) )
959 return (char*)0;
960
961 if( sec_hdr->flags & S_SYMBOL_STUBS ){
962 size = sec_hdr->reserved2;
963 if(size == 0)
964 error("size = 0");
965
966 }
967 else if( sec_hdr->flags & S_LAZY_SYMBOL_POINTERS ||
968 sec_hdr->flags & S_NON_LAZY_SYMBOL_POINTERS)
969 size = sizeof(unsigned long);
970 else
971 return 0;
972
973 /* Compute our index in toc */
974 tocindex = (address - sec_hdr->addr)/size;
975 symindex = tocdylib[sec_hdr->reserved1 + tocindex];
976
977 name = get_sym_name(&symtab[symindex]);
978
979 return name;
980}
981
982static const char * find_reloc_name_given_its_address(int address)
983{
984 unsigned int i;
985 for(i = 0; i < segment->nsects ; i++)
986 {
987 const char * name = find_reloc_name_in_sec_ptr(address, &section_hdr[i]);
988 if((long)name != -1)
989 return name;
990 }
991 return 0;
992}
993
994static const char * get_reloc_name(EXE_RELOC * rel, int * sslide)
995{
996 char * name = 0;
997 struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
998 int sectnum = rel->r_symbolnum;
999 int sectoffset;
1000 int other_half=0;
1001
1002 /* init the slide value */
1003 *sslide = 0;
1004
1005 if(R_SCATTERED & rel->r_address)
1006 return (char *)find_reloc_name_given_its_address(sca_rel->r_value);
1007
1008 if(rel->r_extern)
1009 {
1010 /* ignore debug sym */
1011 if ( symtab[rel->r_symbolnum].n_type & N_STAB )
1012 return 0;
1013 return get_sym_name(&symtab[rel->r_symbolnum]);
1014 }
1015
1016 /* Intruction contains an offset to the symbols pointed to, in the rel->r_symbolnum section */
1017 sectoffset = *(uint32_t *)(text + rel->r_address) & 0xffff;
1018
1019 if(sectnum==0xffffff)
1020 return 0;
1021
1022 /* Sanity Check */
1023 if(sectnum > segment->nsects)
1024 error("sectnum > segment->nsects");
1025
1026 switch(rel->r_type)
1027 {
1028 case PPC_RELOC_LO16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset & 0xffff);
1029 break;
1030 case PPC_RELOC_HI16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (other_half & 0xffff);
1031 break;
1032 case PPC_RELOC_HA16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (other_half & 0xffff);
1033 break;
1034 case PPC_RELOC_BR24:
1035 sectoffset = ( *(uint32_t *)(text + rel->r_address) & 0x03fffffc );
1036 if (sectoffset & 0x02000000) sectoffset |= 0xfc000000;
1037 break;
1038 default:
1039 error("switch(rel->type) not found");
1040 }
1041
1042 if(rel->r_pcrel)
1043 sectoffset += rel->r_address;
1044
1045 if (rel->r_type == PPC_RELOC_BR24)
1046 name = (char *)find_reloc_name_in_sec_ptr((int)sectoffset, &section_hdr[sectnum-1]);
1047
1048 /* search it in the full symbol list, if not found */
1049 if(!name)
1050 name = (char *)find_sym_with_value_and_sec_number(sectoffset, sectnum, sslide);
1051
1052 return name;
1053}
1054
1055/* Used by dyngen common code */
1056static const char * get_rel_sym_name(EXE_RELOC * rel)
1057{
1058 int sslide;
1059 return get_reloc_name( rel, &sslide);
1060}
1061
1062/* Used by dyngen common code */
1063static host_ulong get_rel_offset(EXE_RELOC *rel)
1064{
1065 struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
1066 if(R_SCATTERED & rel->r_address)
1067 return sca_rel->r_address;
1068 else
1069 return rel->r_address;
1070}
1071
1072/* load a mach-o object file */
1073int load_object(const char *filename)
1074{
1075 int fd;
1076 unsigned int offset_to_segment = 0;
1077 unsigned int offset_to_dysymtab = 0;
1078 unsigned int offset_to_symtab = 0;
1079 struct load_command lc;
1080 unsigned int i, j;
1081 EXE_SYM *sym;
1082 struct nlist *syment;
1083
1084 fd = open(filename, O_RDONLY);
1085 if (fd < 0)
1086 error("can't open file '%s'", filename);
1087
1088 /* Read Mach header. */
1089 if (read(fd, &mach_hdr, sizeof (mach_hdr)) != sizeof (mach_hdr))
1090 error("unable to read file header");
1091
1092 /* Check Mach identification. */
1093 if (!check_mach_header(mach_hdr)) {
1094 error("bad Mach header");
1095 }
1096
1097 if (mach_hdr.cputype != CPU_TYPE_POWERPC)
1098 error("Unsupported CPU");
1099
1100 if (mach_hdr.filetype != MH_OBJECT)
1101 error("Unsupported Mach Object");
1102
1103 /* read segment headers */
1104 for(i=0, j=sizeof(mach_hdr); i<mach_hdr.ncmds ; i++)
1105 {
1106 if(read(fd, &lc, sizeof(struct load_command)) != sizeof(struct load_command))
1107 error("unable to read load_command");
1108 if(lc.cmd == LC_SEGMENT)
1109 {
1110 offset_to_segment = j;
1111 lseek(fd, offset_to_segment, SEEK_SET);
1112 segment = malloc(sizeof(struct segment_command));
1113 if(read(fd, segment, sizeof(struct segment_command)) != sizeof(struct segment_command))
1114 error("unable to read LC_SEGMENT");
1115 }
1116 if(lc.cmd == LC_DYSYMTAB)
1117 {
1118 offset_to_dysymtab = j;
1119 lseek(fd, offset_to_dysymtab, SEEK_SET);
1120 dysymtabcmd = malloc(sizeof(struct dysymtab_command));
1121 if(read(fd, dysymtabcmd, sizeof(struct dysymtab_command)) != sizeof(struct dysymtab_command))
1122 error("unable to read LC_DYSYMTAB");
1123 }
1124 if(lc.cmd == LC_SYMTAB)
1125 {
1126 offset_to_symtab = j;
1127 lseek(fd, offset_to_symtab, SEEK_SET);
1128 symtabcmd = malloc(sizeof(struct symtab_command));
1129 if(read(fd, symtabcmd, sizeof(struct symtab_command)) != sizeof(struct symtab_command))
1130 error("unable to read LC_SYMTAB");
1131 }
1132 j+=lc.cmdsize;
1133
1134 lseek(fd, j, SEEK_SET);
1135 }
1136
1137 if(!segment)
1138 error("unable to find LC_SEGMENT");
1139
1140 /* read section headers */
1141 section_hdr = load_data(fd, offset_to_segment + sizeof(struct segment_command), segment->nsects * sizeof(struct section));
1142
1143 /* read all section data */
1144 sdata = (uint8_t **)malloc(sizeof(void *) * segment->nsects);
1145 memset(sdata, 0, sizeof(void *) * segment->nsects);
1146
1147 /* Load the data in section data */
1148 for(i = 0; i < segment->nsects; i++) {
1149 sdata[i] = load_data(fd, section_hdr[i].offset, section_hdr[i].size);
1150 }
1151
1152 /* text section */
1153 text_sec_hdr = find_mach_sec_hdr(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT);
1154 i = find_mach_sec_index(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT);
1155 if (i == -1 || !text_sec_hdr)
1156 error("could not find __TEXT,__text section");
1157 text = sdata[i];
1158
1159 /* Make sure dysym was loaded */
1160 if(!(int)dysymtabcmd)
1161 error("could not find __DYSYMTAB segment");
1162
1163 /* read the table of content of the indirect sym */
1164 tocdylib = load_data( fd, dysymtabcmd->indirectsymoff, dysymtabcmd->nindirectsyms * sizeof(uint32_t) );
1165
1166 /* Make sure symtab was loaded */
1167 if(!(int)symtabcmd)
1168 error("could not find __SYMTAB segment");
1169 nb_syms = symtabcmd->nsyms;
1170
1171 symtab_std = load_data(fd, symtabcmd->symoff, symtabcmd->nsyms * sizeof(struct nlist));
1172 strtab = load_data(fd, symtabcmd->stroff, symtabcmd->strsize);
1173
1174 symtab = malloc(sizeof(EXE_SYM) * nb_syms);
1175
1176 /* Now transform the symtab, to an extended version, with the sym size, and the C name */
1177 for(i = 0, sym = symtab, syment = symtab_std; i < nb_syms; i++, sym++, syment++) {
1178 const char *name;
1179 struct nlist *sym_follow, *sym_next = 0;
1180 unsigned int j;
1181 name = find_str_by_index(sym->n_un.n_strx);
1182 memset(sym, 0, sizeof(*sym));
1183
1184 if ( sym->n_type & N_STAB ) /* Debug symbols are skipped */
1185 continue;
1186
1187 memcpy(sym, syment, sizeof(*syment));
1188
1189 /* Find the following symbol in order to get the current symbol size */
1190 for(j = 0, sym_follow = symtab_std; j < nb_syms; j++, sym_follow++) {
1191 if ( sym_follow->n_sect != 1 || sym_follow->n_type & N_STAB || !(sym_follow->n_value > sym->st_value))
1192 continue;
1193 if(!sym_next) {
1194 sym_next = sym_follow;
1195 continue;
1196 }
1197 if(!(sym_next->n_value > sym_follow->n_value))
1198 continue;
1199 sym_next = sym_follow;
1200 }
1201 if(sym_next)
1202 sym->st_size = sym_next->n_value - sym->st_value;
1203 else
1204 sym->st_size = text_sec_hdr->size - sym->st_value;
1205 }
1206
1207 /* Find Reloc */
1208 relocs = load_data(fd, text_sec_hdr->reloff, text_sec_hdr->nreloc * sizeof(struct relocation_info));
1209 nb_relocs = text_sec_hdr->nreloc;
1210
1211 close(fd);
1212 return 0;
1213}
1214
1215#endif /* CONFIG_FORMAT_MACH */
1216
1217#ifdef CONFIG_FORMAT_AOUT
1218
1219struct exec *aout_hdr;
1220struct nlist *symtab_std;
1221char *strtab;
1222
1223
1224/* Utility functions */
1225
1226static inline char *find_str_by_index(int index)
1227{
1228 return strtab+index;
1229}
1230
1231/* Used by dyngen common code */
1232static char *get_sym_name(EXE_SYM *sym)
1233{
1234 char *name = find_str_by_index(sym->n_un.n_strx);
1235
1236 if (sym->n_type & N_STAB) /* Debug symbols are ignored */
1237 return "debug";
1238 if (name && name[0] == '_')
1239 return name + 1;
1240 return name;
1241}
1242
1243static int type_to_sec_number(unsigned type)
1244{
1245 switch (type)
1246 {
1247 case 0: case 0 |N_EXT: case N_WEAKU: return N_UNDF;
1248 case N_ABS: case N_ABS |N_EXT: case N_WEAKA: return N_ABS;
1249 case N_TEXT: case N_TEXT|N_EXT: case N_WEAKT: return N_TEXT;
1250 case N_DATA: case N_DATA|N_EXT: case N_WEAKD: return N_DATA;
1251 case N_BSS: case N_BSS |N_EXT: case N_WEAKB: return N_BSS;
1252 case N_SETA: case N_SETA|N_EXT: return N_SETA;
1253 case N_SETT: case N_SETT|N_EXT: return N_SETT;
1254 case N_SETD: case N_SETD|N_EXT: return N_SETD;
1255
1256 default:
1257 return type;
1258 }
1259}
1260
1261/* find a sym name given its value, in a section number */
1262static const char *find_sym_with_value_and_sec_number(long value, int sec, int *offset)
1263{
1264 int i, ret = -1;
1265
1266 for (i = 0; i < nb_syms; i++) {
1267 if ( !(symtab[i].n_type & N_STAB)
1268 && type_to_sec_number(symtab[i].n_type) == sec
1269 && symtab[i].st_value <= value
1270 && ( ret < 0
1271 || symtab[i].st_value >= symtab[ret].st_value)) {
1272 ret = i;
1273 }
1274 }
1275 if (ret < 0) {
1276 *offset = 0;
1277 return 0;
1278 }
1279 *offset = value - symtab[ret].st_value;
1280 return get_sym_name(&symtab[ret]);
1281}
1282
1283static const char *get_rel_sym_name_and_addend(EXE_RELOC *rel, int *sslide)
1284{
1285 int sec;
1286 int off;
1287
1288 *sslide = 0;
1289
1290 if (rel->r_extern)
1291 {
1292 /* ignore debug sym */
1293 if (symtab[rel->r_symbolnum].n_type & N_STAB)
1294 return 0;
1295
1296 /* The intruction contains the addend. */
1297 off = *(uint32_t *)(text + rel->r_address);
1298 if (rel->r_pcrel)
1299 off += rel->r_address;
1300 *sslide = off;
1301 return get_sym_name(&symtab[rel->r_symbolnum]);
1302 }
1303 if (rel->r_symbolnum == 0xffffff)
1304 return 0;
1305
1306 sec = rel->r_symbolnum & ~N_EXT;
1307 /* sanity */
1308 switch (sec)
1309 {
1310 case N_TEXT: case N_DATA: case N_BSS: case N_ABS: break;
1311 default: error("invalid section %d", sec);
1312 }
1313
1314 /* The intruction contains the addend. */
1315 off = *(uint32_t *)(text + rel->r_address);
1316 if (rel->r_pcrel)
1317 off += rel->r_address;
1318
1319 /* search it in the full symbol list, if not found */
1320 return find_sym_with_value_and_sec_number(off, sec, sslide);
1321}
1322
1323/* Used by dyngen common code */
1324static const char * get_rel_sym_name(EXE_RELOC *rel)
1325{
1326 int ignored;
1327 return get_rel_sym_name_and_addend(rel, &ignored);
1328}
1329
1330/* Used by dyngen common code */
1331static host_ulong get_rel_offset(EXE_RELOC *rel)
1332{
1333 return rel->r_address;
1334}
1335
1336/* load a a.out object file */
1337int load_object(const char *filename)
1338{
1339 FILE *pf;
1340 long file_size;
1341 unsigned i;
1342 EXE_SYM *dst_sym;
1343 struct nlist *src_sym;
1344
1345 /*
1346 * Open the file and validate the header.
1347 */
1348 pf = fopen(filename, "rb");
1349 if (!pf)
1350 error("can't open file '%s'", filename);
1351
1352 /* we're optimistic, read the entire file first. */
1353 if (fseek(pf, 0, SEEK_END) != 0)
1354 error("Input file '%s' is not seekable", filename);
1355 file_size = ftell(pf);
1356 fseek(pf, 0L, SEEK_SET);
1357
1358 aout_hdr = malloc(file_size + 1);
1359 if (!aout_hdr)
1360 error("malloc(%ld) failed", file_size + 1);
1361 if (fread(aout_hdr, 1, file_size, pf) != file_size)
1362 error("error reading '%s'", filename);
1363 fclose(pf);
1364
1365 /* validate the header. */
1366 if (N_MAGIC(*aout_hdr) != OMAGIC)
1367 error("unknown magic: %lo", N_MAGIC(*aout_hdr));
1368 if (N_MACHTYPE(*aout_hdr) != M_386 && N_MACHTYPE(*aout_hdr) != 0)
1369 error("unsupported machtype: %d", N_MACHTYPE(*aout_hdr));
1370
1371 /* setup globals. */
1372 strtab = (char *)((uint8_t *)aout_hdr + N_STROFF(*aout_hdr));
1373 symtab_std = (struct nlist *)((uint8_t *)aout_hdr + N_SYMOFF(*aout_hdr));
1374
1375 relocs = (struct relocation_info *)((uint8_t *)aout_hdr + N_TRELOFF(*aout_hdr));
1376 nb_syms = aout_hdr->a_syms / sizeof(struct nlist);
1377 text_shndx = 1;
1378 text = (uint8_t *)aout_hdr + N_TXTOFF(*aout_hdr);
1379 nb_relocs = aout_hdr->a_trsize / sizeof(relocs[0]);
1380
1381 /*
1382 * Now transform the symtab, to an extended version, with the sym size, and the C name
1383 */
1384 src_sym = symtab_std;
1385 dst_sym = symtab = malloc(sizeof(EXE_SYM) * nb_syms);
1386 if (!dst_sym)
1387 error("malloc(%zd) failed", sizeof(EXE_SYM) * nb_syms);
1388 for (i = 0; i < nb_syms; i++, src_sym++, dst_sym++) {
1389 struct nlist *sym_next = NULL;
1390 struct nlist *sym_cur;
1391 unsigned sec;
1392 unsigned j;
1393
1394 /* copy the symbol and find the name. */
1395 dst_sym->n_un.n_strx = src_sym->n_un.n_strx;
1396 dst_sym->n_type = src_sym->n_type;
1397 dst_sym->n_other = src_sym->n_other;
1398 dst_sym->n_desc = src_sym->n_desc;
1399 dst_sym->st_value = src_sym->n_value;
1400 dst_sym->st_size = 0;
1401 if (src_sym->n_type & N_STAB)
1402 continue; /* skip debug symbols. */
1403
1404 /* Find the following symbol in order to get the current symbol size */
1405 sec = type_to_sec_number(dst_sym->n_type);
1406 for (j = 0, sym_cur = symtab_std; j < nb_syms; j++, sym_cur++) {
1407 if ( type_to_sec_number(sym_cur->n_type) != sec
1408 || (sym_cur->n_type & N_STAB)
1409 || sym_cur->n_value <= dst_sym->st_value)
1410 continue;
1411 if ( sym_next
1412 && sym_next->n_value <= sym_cur->n_value)
1413 continue;
1414 /* good one */
1415 sym_next = sym_cur;
1416 }
1417 if (sym_next)
1418 dst_sym->st_size = sym_next->n_value - dst_sym->st_value;
1419 else
1420 dst_sym->st_size = aout_hdr->a_text - dst_sym->st_value;
1421 }
1422
1423 return 0;
1424}
1425
1426#endif /* CONFIG_FORMAT_AOUT */
1427
1428
1429void get_reloc_expr(char *name, int name_size, const char *sym_name)
1430{
1431 const char *p;
1432
1433 if (strstart(sym_name, "__op_param", &p)) {
1434 snprintf(name, name_size, "param%s", p);
1435 } else if (strstart(sym_name, "__op_gen_label", &p)) {
1436 snprintf(name, name_size, "gen_labels[param%s]", p);
1437 } else {
1438#ifdef HOST_SPARC
1439 if (sym_name[0] == '.')
1440 snprintf(name, sizeof(name),
1441 "(long)(&__dot_%s)",
1442 sym_name + 1);
1443 else
1444#endif
1445 snprintf(name, name_size, "(long)(&%s)", sym_name);
1446 }
1447}
1448
1449#ifdef HOST_ARM
1450
1451int arm_emit_ldr_info(const char *name, unsigned long start_offset,
1452 FILE *outfile, uint8_t *p_start, uint8_t *p_end,
1453 ELF_RELOC *relocs, int nb_relocs)
1454{
1455 uint8_t *p;
1456 uint32_t insn;
1457 int offset, min_offset, pc_offset, data_size;
1458 uint8_t data_allocated[1024];
1459 unsigned int data_index;
1460
1461 memset(data_allocated, 0, sizeof(data_allocated));
1462
1463 p = p_start;
1464 min_offset = p_end - p_start;
1465 while (p < p_start + min_offset) {
1466 insn = get32((uint32_t *)p);
1467 if ((insn & 0x0d5f0000) == 0x051f0000) {
1468 /* ldr reg, [pc, #im] */
1469 offset = insn & 0xfff;
1470 if (!(insn & 0x00800000))
1471 offset = -offset;
1472 if ((offset & 3) !=0)
1473 error("%s:%04x: ldr pc offset must be 32 bit aligned",
1474 name, start_offset + p - p_start);
1475 pc_offset = p - p_start + offset + 8;
1476 if (pc_offset <= (p - p_start) ||
1477 pc_offset >= (p_end - p_start))
1478 error("%s:%04x: ldr pc offset must point inside the function code",
1479 name, start_offset + p - p_start);
1480 if (pc_offset < min_offset)
1481 min_offset = pc_offset;
1482 if (outfile) {
1483 /* ldr position */
1484 fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
1485 p - p_start);
1486 /* ldr data index */
1487 data_index = ((p_end - p_start) - pc_offset - 4) >> 2;
1488 fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
1489 data_index);
1490 fprintf(outfile, " arm_ldr_ptr++;\n");
1491 if (data_index >= sizeof(data_allocated))
1492 error("%s: too many data", name);
1493 if (!data_allocated[data_index]) {
1494 ELF_RELOC *rel;
1495 int i, addend, type;
1496 const char *sym_name, *p;
1497 char relname[1024];
1498
1499 data_allocated[data_index] = 1;
1500
1501 /* data value */
1502 addend = get32((uint32_t *)(p_start + pc_offset));
1503 relname[0] = '\0';
1504 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1505 if (rel->r_offset == (pc_offset + start_offset)) {
1506 sym_name = get_rel_sym_name(rel);
1507 /* the compiler leave some unnecessary references to the code */
1508 get_reloc_expr(relname, sizeof(relname), sym_name);
1509 type = ELF32_R_TYPE(rel->r_info);
1510 if (type != R_ARM_ABS32)
1511 error("%s: unsupported data relocation", name);
1512 break;
1513 }
1514 }
1515 fprintf(outfile, " arm_data_ptr[%d] = 0x%x",
1516 data_index, addend);
1517 if (relname[0] != '\0')
1518 fprintf(outfile, " + %s", relname);
1519 fprintf(outfile, ";\n");
1520 }
1521 }
1522 }
1523 p += 4;
1524 }
1525 data_size = (p_end - p_start) - min_offset;
1526 if (data_size > 0 && outfile) {
1527 fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2);
1528 }
1529
1530 /* the last instruction must be a mov pc, lr */
1531 if (p == p_start)
1532 goto arm_ret_error;
1533 p -= 4;
1534 insn = get32((uint32_t *)p);
1535 if ((insn & 0xffff0000) != 0xe91b0000) {
1536 arm_ret_error:
1537 if (!outfile)
1538 printf("%s: invalid epilog\n", name);
1539 }
1540 return p - p_start;
1541}
1542#endif
1543
1544
1545#define MAX_ARGS 3
1546
1547/* generate op code */
1548void gen_code(const char *name, host_ulong offset, host_ulong size,
1549 FILE *outfile, int gen_switch)
1550{
1551 int copy_size = 0;
1552 uint8_t *p_start, *p_end;
1553 host_ulong start_offset;
1554 int nb_args, i, n;
1555 uint8_t args_present[MAX_ARGS];
1556 const char *sym_name, *p;
1557 EXE_RELOC *rel;
1558
1559 /* Compute exact size excluding prologue and epilogue instructions.
1560 * Increment start_offset to skip epilogue instructions, then compute
1561 * copy_size the indicate the size of the remaining instructions (in
1562 * bytes).
1563 */
1564 p_start = text + offset;
1565 p_end = p_start + size;
1566 start_offset = offset;
1567#if defined(HOST_I386) || defined(HOST_X86_64)
1568#if defined(CONFIG_FORMAT_COFF) || defined(CONFIG_FORMAT_AOUT)
1569 {
1570 uint8_t *p;
1571 p = p_end - 1;
1572 if (p == p_start)
1573 error("empty code for %s", name);
1574 while (*p != 0xc3) {
1575 p--;
1576 if (p <= p_start)
1577 error("ret or jmp expected at the end of %s", name);
1578 }
1579 copy_size = p - p_start;
1580 }
1581#else
1582 {
1583 int len;
1584 len = p_end - p_start;
1585 if (len == 0)
1586 error("empty code for %s", name);
1587 if (p_end[-1] == 0xc3) {
1588 len--;
1589 } else {
1590 error("ret or jmp expected at the end of %s", name);
1591 }
1592 copy_size = len;
1593 }
1594#endif
1595#elif defined(HOST_PPC)
1596 {
1597 uint8_t *p;
1598 p = (void *)(p_end - 4);
1599 if (p == p_start)
1600 error("empty code for %s", name);
1601 if (get32((uint32_t *)p) != 0x4e800020)
1602 error("blr expected at the end of %s", name);
1603 copy_size = p - p_start;
1604 }
1605#elif defined(HOST_S390)
1606 {
1607 uint8_t *p;
1608 p = (void *)(p_end - 2);
1609 if (p == p_start)
1610 error("empty code for %s", name);
1611 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
1612 error("br %%r14 expected at the end of %s", name);
1613 copy_size = p - p_start;
1614 }
1615#elif defined(HOST_ALPHA)
1616 {
1617 uint8_t *p;
1618 p = p_end - 4;
1619#if 0
1620 /* XXX: check why it occurs */
1621 if (p == p_start)
1622 error("empty code for %s", name);
1623#endif
1624 if (get32((uint32_t *)p) != 0x6bfa8001)
1625 error("ret expected at the end of %s", name);
1626 copy_size = p - p_start;
1627 }
1628#elif defined(HOST_IA64)
1629 {
1630 uint8_t *p;
1631 p = (void *)(p_end - 4);
1632 if (p == p_start)
1633 error("empty code for %s", name);
1634 /* br.ret.sptk.many b0;; */
1635 /* 08 00 84 00 */
1636 if (get32((uint32_t *)p) != 0x00840008)
1637 error("br.ret.sptk.many b0;; expected at the end of %s", name);
1638 copy_size = p - p_start;
1639 }
1640#elif defined(HOST_SPARC)
1641 {
1642 uint32_t start_insn, end_insn1, end_insn2;
1643 uint8_t *p;
1644 p = (void *)(p_end - 8);
1645 if (p <= p_start)
1646 error("empty code for %s", name);
1647 start_insn = get32((uint32_t *)(p_start + 0x0));
1648 end_insn1 = get32((uint32_t *)(p + 0x0));
1649 end_insn2 = get32((uint32_t *)(p + 0x4));
1650 if ((start_insn & ~0x1fff) == 0x9de3a000) {
1651 p_start += 0x4;
1652 start_offset += 0x4;
1653 if ((int)(start_insn | ~0x1fff) < -128)
1654 error("Found bogus save at the start of %s", name);
1655 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
1656 error("ret; restore; not found at end of %s", name);
1657 } else {
1658 error("No save at the beginning of %s", name);
1659 }
1660#if 0
1661 /* Skip a preceeding nop, if present. */
1662 if (p > p_start) {
1663 skip_insn = get32((uint32_t *)(p - 0x4));
1664 if (skip_insn == 0x01000000)
1665 p -= 4;
1666 }
1667#endif
1668 copy_size = p - p_start;
1669 }
1670#elif defined(HOST_SPARC64)
1671 {
1672 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
1673 uint8_t *p;
1674 p = (void *)(p_end - 8);
1675 if (p <= p_start)
1676 error("empty code for %s", name);
1677 start_insn = get32((uint32_t *)(p_start + 0x0));
1678 end_insn1 = get32((uint32_t *)(p + 0x0));
1679 end_insn2 = get32((uint32_t *)(p + 0x4));
1680 if ((start_insn & ~0x1fff) == 0x9de3a000) {
1681 p_start += 0x4;
1682 start_offset += 0x4;
1683 if ((int)(start_insn | ~0x1fff) < -256)
1684 error("Found bogus save at the start of %s", name);
1685 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
1686 error("ret; restore; not found at end of %s", name);
1687 } else {
1688 error("No save at the beginning of %s", name);
1689 }
1690
1691 /* Skip a preceeding nop, if present. */
1692 if (p > p_start) {
1693 skip_insn = get32((uint32_t *)(p - 0x4));
1694 if (skip_insn == 0x01000000)
1695 p -= 4;
1696 }
1697
1698 copy_size = p - p_start;
1699 }
1700#elif defined(HOST_ARM)
1701 {
1702 if ((p_end - p_start) <= 16)
1703 error("%s: function too small", name);
1704 if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
1705 (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
1706 get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
1707 error("%s: invalid prolog", name);
1708 p_start += 12;
1709 start_offset += 12;
1710 copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end,
1711 relocs, nb_relocs);
1712 }
1713#elif defined(HOST_M68K)
1714 {
1715 uint8_t *p;
1716 p = (void *)(p_end - 2);
1717 if (p == p_start)
1718 error("empty code for %s", name);
1719 // remove NOP's, probably added for alignment
1720 while ((get16((uint16_t *)p) == 0x4e71) &&
1721 (p>p_start))
1722 p -= 2;
1723 if (get16((uint16_t *)p) != 0x4e75)
1724 error("rts expected at the end of %s", name);
1725 copy_size = p - p_start;
1726 }
1727#else
1728#error unsupported CPU
1729#endif
1730
1731 /* compute the number of arguments by looking at the relocations */
1732 for(i = 0;i < MAX_ARGS; i++)
1733 args_present[i] = 0;
1734
1735 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1736 host_ulong offset = get_rel_offset(rel);
1737 if (offset >= start_offset &&
1738 offset < start_offset + (p_end - p_start)) {
1739 sym_name = get_rel_sym_name(rel);
1740 if(!sym_name)
1741 continue;
1742 if (strstart(sym_name, "__op_param", &p) ||
1743 strstart(sym_name, "__op_gen_label", &p)) {
1744 n = strtoul(p, NULL, 10);
1745 if (n > MAX_ARGS)
1746 error("too many arguments in %s", name);
1747 args_present[n - 1] = 1;
1748 }
1749 }
1750 }
1751
1752 nb_args = 0;
1753 while (nb_args < MAX_ARGS && args_present[nb_args])
1754 nb_args++;
1755 for(i = nb_args; i < MAX_ARGS; i++) {
1756 if (args_present[i])
1757 error("inconsistent argument numbering in %s", name);
1758 }
1759
1760 if (gen_switch == 2) {
1761 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
1762 } else if (gen_switch == 1) {
1763
1764 /* output C code */
1765 fprintf(outfile, "case INDEX_%s: {\n", name);
1766 if (nb_args > 0) {
1767 fprintf(outfile, " long ");
1768 for(i = 0; i < nb_args; i++) {
1769 if (i != 0)
1770 fprintf(outfile, ", ");
1771 fprintf(outfile, "param%d", i + 1);
1772 }
1773 fprintf(outfile, ";\n");
1774 }
1775 fprintf(outfile, " extern void %s();\n", name);
1776
1777 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1778 host_ulong offset = get_rel_offset(rel);
1779 if (offset >= start_offset &&
1780 offset < start_offset + (p_end - p_start)) {
1781 sym_name = get_rel_sym_name(rel);
1782 if(!sym_name)
1783 continue;
1784 if (*sym_name &&
1785 !strstart(sym_name, "__op_param", NULL) &&
1786 !strstart(sym_name, "__op_jmp", NULL) &&
1787 !strstart(sym_name, "__op_gen_label", NULL)) {
1788#if defined(HOST_SPARC)
1789 if (sym_name[0] == '.') {
1790 fprintf(outfile,
1791 "extern char __dot_%s __asm__(\"%s\");\n",
1792 sym_name+1, sym_name);
1793 continue;
1794 }
1795#endif
1796#ifdef VBOX
1797 if ( strcmp(sym_name, "remR3PhysWriteBytes")
1798 && strcmp(sym_name, "remR3PhysReadBytes")
1799 && strcmp(sym_name, "remR3PhysReadUByte")
1800 && strcmp(sym_name, "remR3PhysReadSByte")
1801 && strcmp(sym_name, "remR3PhysReadUWord")
1802 && strcmp(sym_name, "remR3PhysReadSWord")
1803 && strcmp(sym_name, "remR3PhysReadULong")
1804 && strcmp(sym_name, "remR3PhysReadSLong")
1805 && strcmp(sym_name, "remR3PhysWriteByte")
1806 && strcmp(sym_name, "remR3PhysWriteWord")
1807 && strcmp(sym_name, "remR3PhysWriteDword"))
1808#endif /* VBOX */
1809#ifdef __APPLE__
1810/* set __attribute((unused)) on darwin because we wan't to avoid warning when we don't use the symbol */
1811 fprintf(outfile, "extern char %s __attribute__((unused));\n", sym_name);
1812#else
1813 fprintf(outfile, "extern char %s;\n", sym_name);
1814#endif
1815 }
1816 }
1817 }
1818
1819 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n",
1820 name, (int)(start_offset - offset), copy_size);
1821
1822 /* emit code offset information */
1823 {
1824 EXE_SYM *sym;
1825 const char *sym_name, *p;
1826 unsigned long val;
1827 int n;
1828
1829 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1830 sym_name = get_sym_name(sym);
1831 if (strstart(sym_name, "__op_label", &p)) {
1832 uint8_t *ptr;
1833 unsigned long offset;
1834
1835 /* test if the variable refers to a label inside
1836 the code we are generating */
1837#ifdef CONFIG_FORMAT_COFF
1838 if (sym->st_shndx == text_shndx) {
1839 ptr = sdata[coff_text_shndx];
1840 } else if (sym->st_shndx == data_shndx) {
1841 ptr = sdata[coff_data_shndx];
1842 } else {
1843 ptr = NULL;
1844 }
1845#elif defined(CONFIG_FORMAT_MACH)
1846 if(!sym->n_sect)
1847 continue;
1848 ptr = sdata[sym->n_sect-1];
1849#elif defined(CONFIG_FORMAT_AOUT)
1850 switch (type_to_sec_number(sym->n_type))
1851 {
1852 case N_TEXT: ptr = (uint8_t *)aout_hdr + N_TXTOFF(*aout_hdr); break;
1853 case N_DATA: ptr = (uint8_t *)aout_hdr + N_DATOFF(*aout_hdr); break;
1854 default: ptr = NULL;
1855 }
1856#else
1857 ptr = sdata[sym->st_shndx];
1858#endif
1859 if (!ptr)
1860 error("__op_labelN in invalid section");
1861 offset = sym->st_value;
1862#ifdef CONFIG_FORMAT_MACH
1863 offset -= section_hdr[sym->n_sect-1].addr;
1864#elif defined(CONFIG_FORMAT_AOUT)
1865 if (type_to_sec_number(sym->n_type) == N_DATA)
1866 offset -= aout_hdr->a_text;
1867#endif
1868 val = *(unsigned long *)(ptr + offset);
1869#ifdef ELF_USES_RELOCA
1870 {
1871 int reloc_shndx, nb_relocs1, j;
1872
1873 /* try to find a matching relocation */
1874 reloc_shndx = find_reloc(sym->st_shndx);
1875 if (reloc_shndx) {
1876 nb_relocs1 = shdr[reloc_shndx].sh_size /
1877 shdr[reloc_shndx].sh_entsize;
1878 rel = (ELF_RELOC *)sdata[reloc_shndx];
1879 for(j = 0; j < nb_relocs1; j++) {
1880 if (rel->r_offset == offset) {
1881 val = rel->r_addend;
1882 break;
1883 }
1884 rel++;
1885 }
1886 }
1887 }
1888#endif
1889 if (val >= start_offset && val <= start_offset + copy_size) {
1890 n = strtol(p, NULL, 10);
1891 fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
1892 }
1893 }
1894 }
1895 }
1896
1897 /* load parameres in variables */
1898 for(i = 0; i < nb_args; i++) {
1899 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
1900 }
1901
1902 /* patch relocations */
1903#if defined(HOST_I386)
1904 {
1905 char name[256];
1906 int type;
1907 int addend;
1908 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1909 if (rel->r_offset >= start_offset &&
1910 rel->r_offset < start_offset + copy_size) {
1911#ifdef CONFIG_FORMAT_AOUT
1912 sym_name = get_rel_sym_name_and_addend(rel, &addend);
1913#else
1914 sym_name = get_rel_sym_name(rel);
1915#endif
1916 if (strstart(sym_name, "__op_jmp", &p)) {
1917 int n;
1918 n = strtol(p, NULL, 10);
1919 /* __op_jmp relocations are done at
1920 runtime to do translated block
1921 chaining: the offset of the instruction
1922 needs to be stored */
1923 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1924 n, rel->r_offset - start_offset);
1925 continue;
1926 }
1927
1928 get_reloc_expr(name, sizeof(name), sym_name);
1929#ifndef CONFIG_FORMAT_AOUT
1930 addend = get32((uint32_t *)(text + rel->r_offset));
1931#endif
1932#ifdef CONFIG_FORMAT_ELF
1933 type = ELF32_R_TYPE(rel->r_info);
1934 switch(type) {
1935 case R_386_32:
1936 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1937 rel->r_offset - start_offset, name, addend);
1938 break;
1939 case R_386_PC32:
1940 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1941 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1942 break;
1943 default:
1944 error("unsupported i386 relocation (%d)", type);
1945 }
1946#elif defined(CONFIG_FORMAT_COFF)
1947 {
1948 char *temp_name;
1949 int j;
1950 EXE_SYM *sym;
1951 temp_name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx));
1952 if (!strcmp(temp_name, ".data")) {
1953 for (j = 0, sym = symtab; j < nb_syms; j++, sym++) {
1954 if (strstart(sym->st_name, sym_name, NULL)) {
1955 addend -= sym->st_value;
1956 }
1957 }
1958 }
1959 }
1960 type = rel->r_type;
1961 switch(type) {
1962 case DIR32:
1963 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1964 rel->r_offset - start_offset, name, addend);
1965 break;
1966 case DISP32:
1967 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n",
1968 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1969 break;
1970 default:
1971 error("unsupported i386 relocation (%d)", type);
1972 }
1973#elif defined(CONFIG_FORMAT_AOUT)
1974 if (rel->r_pcrel) {
1975 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1976 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1977 } else {
1978 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1979 rel->r_offset - start_offset, name, addend);
1980 }
1981 (void)type;
1982#else
1983#error unsupport object format
1984#endif
1985 }
1986 }
1987 }
1988#elif defined(HOST_X86_64)
1989 {
1990 char name[256];
1991 int type;
1992 int addend;
1993 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1994 if (rel->r_offset >= start_offset &&
1995 rel->r_offset < start_offset + copy_size) {
1996 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1997 get_reloc_expr(name, sizeof(name), sym_name);
1998 type = ELF32_R_TYPE(rel->r_info);
1999 addend = rel->r_addend;
2000 switch(type) {
2001 case R_X86_64_32:
2002 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
2003 rel->r_offset - start_offset, name, addend);
2004 break;
2005 case R_X86_64_32S:
2006 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
2007 rel->r_offset - start_offset, name, addend);
2008 break;
2009 case R_X86_64_PC32:
2010 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
2011 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
2012 break;
2013 default:
2014 error("unsupported X86_64 relocation (%d)", type);
2015 }
2016 }
2017 }
2018 }
2019#elif defined(HOST_PPC)
2020 {
2021#ifdef CONFIG_FORMAT_ELF
2022 char name[256];
2023 int type;
2024 int addend;
2025 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2026 if (rel->r_offset >= start_offset &&
2027 rel->r_offset < start_offset + copy_size) {
2028 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
2029 if (strstart(sym_name, "__op_jmp", &p)) {
2030 int n;
2031 n = strtol(p, NULL, 10);
2032 /* __op_jmp relocations are done at
2033 runtime to do translated block
2034 chaining: the offset of the instruction
2035 needs to be stored */
2036 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
2037 n, rel->r_offset - start_offset);
2038 continue;
2039 }
2040
2041 get_reloc_expr(name, sizeof(name), sym_name);
2042 type = ELF32_R_TYPE(rel->r_info);
2043 addend = rel->r_addend;
2044 switch(type) {
2045 case R_PPC_ADDR32:
2046 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2047 rel->r_offset - start_offset, name, addend);
2048 break;
2049 case R_PPC_ADDR16_LO:
2050 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
2051 rel->r_offset - start_offset, name, addend);
2052 break;
2053 case R_PPC_ADDR16_HI:
2054 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
2055 rel->r_offset - start_offset, name, addend);
2056 break;
2057 case R_PPC_ADDR16_HA:
2058 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
2059 rel->r_offset - start_offset, name, addend);
2060 break;
2061 case R_PPC_REL24:
2062 /* warning: must be at 32 MB distancy */
2063 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n",
2064 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
2065 break;
2066 default:
2067 error("unsupported powerpc relocation (%d)", type);
2068 }
2069 }
2070 }
2071#elif defined(CONFIG_FORMAT_MACH)
2072 struct scattered_relocation_info *scarel;
2073 struct relocation_info * rel;
2074 char final_sym_name[256];
2075 const char *sym_name;
2076 const char *p;
2077 int slide, sslide;
2078 int i;
2079
2080 for(i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
2081 unsigned int offset, length, value = 0;
2082 unsigned int type, pcrel, isym = 0;
2083 unsigned int usesym = 0;
2084
2085 if(R_SCATTERED & rel->r_address) {
2086 scarel = (struct scattered_relocation_info*)rel;
2087 offset = (unsigned int)scarel->r_address;
2088 length = scarel->r_length;
2089 pcrel = scarel->r_pcrel;
2090 type = scarel->r_type;
2091 value = scarel->r_value;
2092 } else {
2093 value = isym = rel->r_symbolnum;
2094 usesym = (rel->r_extern);
2095 offset = rel->r_address;
2096 length = rel->r_length;
2097 pcrel = rel->r_pcrel;
2098 type = rel->r_type;
2099 }
2100
2101 slide = offset - start_offset;
2102
2103 if (!(offset >= start_offset && offset < start_offset + size))
2104 continue; /* not in our range */
2105
2106 sym_name = get_reloc_name(rel, &sslide);
2107
2108 if(usesym && symtab[isym].n_type & N_STAB)
2109 continue; /* don't handle STAB (debug sym) */
2110
2111 if (sym_name && strstart(sym_name, "__op_jmp", &p)) {
2112 int n;
2113 n = strtol(p, NULL, 10);
2114 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
2115 n, slide);
2116 continue; /* Nothing more to do */
2117 }
2118
2119 if(!sym_name)
2120 {
2121 fprintf(outfile, "/* #warning relocation not handled in %s (value 0x%x, %s, offset 0x%x, length 0x%x, %s, type 0x%x) */\n",
2122 name, value, usesym ? "use sym" : "don't use sym", offset, length, pcrel ? "pcrel":"", type);
2123 continue; /* dunno how to handle without final_sym_name */
2124 }
2125
2126 get_reloc_expr(final_sym_name, sizeof(final_sym_name),
2127 sym_name);
2128 switch(type) {
2129 case PPC_RELOC_BR24:
2130 fprintf(outfile, "{\n");
2131 fprintf(outfile, " uint32_t imm = *(uint32_t *)(gen_code_ptr + %d) & 0x3fffffc;\n", slide);
2132 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((imm + ((long)%s - (long)gen_code_ptr) + %d) & 0x03fffffc);\n",
2133 slide, slide, name, sslide );
2134 fprintf(outfile, "}\n");
2135 break;
2136 case PPC_RELOC_HI16:
2137 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d) >> 16;\n",
2138 slide, final_sym_name, sslide);
2139 break;
2140 case PPC_RELOC_LO16:
2141 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d);\n",
2142 slide, final_sym_name, sslide);
2143 break;
2144 case PPC_RELOC_HA16:
2145 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d + 0x8000) >> 16;\n",
2146 slide, final_sym_name, sslide);
2147 break;
2148 default:
2149 error("unsupported powerpc relocation (%d)", type);
2150 }
2151 }
2152#else
2153#error unsupport object format
2154#endif
2155 }
2156#elif defined(HOST_S390)
2157 {
2158 char name[256];
2159 int type;
2160 int addend;
2161 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2162 if (rel->r_offset >= start_offset &&
2163 rel->r_offset < start_offset + copy_size) {
2164 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
2165 get_reloc_expr(name, sizeof(name), sym_name);
2166 type = ELF32_R_TYPE(rel->r_info);
2167 addend = rel->r_addend;
2168 switch(type) {
2169 case R_390_32:
2170 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2171 rel->r_offset - start_offset, name, addend);
2172 break;
2173 case R_390_16:
2174 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
2175 rel->r_offset - start_offset, name, addend);
2176 break;
2177 case R_390_8:
2178 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
2179 rel->r_offset - start_offset, name, addend);
2180 break;
2181 default:
2182 error("unsupported s390 relocation (%d)", type);
2183 }
2184 }
2185 }
2186 }
2187#elif defined(HOST_ALPHA)
2188 {
2189 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
2190 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
2191 int type;
2192
2193 type = ELF64_R_TYPE(rel->r_info);
2194 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
2195 switch (type) {
2196 case R_ALPHA_GPDISP:
2197 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
2198 as an immediate instead of constructing it from the pv or ra. */
2199 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
2200 rel->r_offset - start_offset);
2201 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
2202 rel->r_offset - start_offset + rel->r_addend);
2203 break;
2204 case R_ALPHA_LITUSE:
2205 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
2206 now, since some called functions (libc) need pv to be set up. */
2207 break;
2208 case R_ALPHA_HINT:
2209 /* Branch target prediction hint. Ignore for now. Should be already
2210 correct for in-function jumps. */
2211 break;
2212 case R_ALPHA_LITERAL:
2213 /* Load a literal from the GOT relative to the gp. Since there's only a
2214 single gp, nothing is to be done. */
2215 break;
2216 case R_ALPHA_GPRELHIGH:
2217 /* Handle fake relocations against __op_param symbol. Need to emit the
2218 high part of the immediate value instead. Other symbols need no
2219 special treatment. */
2220 if (strstart(sym_name, "__op_param", &p))
2221 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
2222 rel->r_offset - start_offset, p);
2223 break;
2224 case R_ALPHA_GPRELLOW:
2225 if (strstart(sym_name, "__op_param", &p))
2226 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
2227 rel->r_offset - start_offset, p);
2228 break;
2229 case R_ALPHA_BRSGP:
2230 /* PC-relative jump. Tweak offset to skip the two instructions that try to
2231 set up the gp from the pv. */
2232 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
2233 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
2234 break;
2235 default:
2236 error("unsupported Alpha relocation (%d)", type);
2237 }
2238 }
2239 }
2240 }
2241#elif defined(HOST_IA64)
2242 {
2243 char name[256];
2244 int type;
2245 int addend;
2246 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2247 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
2248 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
2249 get_reloc_expr(name, sizeof(name), sym_name);
2250 type = ELF64_R_TYPE(rel->r_info);
2251 addend = rel->r_addend;
2252 switch(type) {
2253 case R_IA64_LTOFF22:
2254 error("must implemnt R_IA64_LTOFF22 relocation");
2255 case R_IA64_PCREL21B:
2256 error("must implemnt R_IA64_PCREL21B relocation");
2257 default:
2258 error("unsupported ia64 relocation (%d)", type);
2259 }
2260 }
2261 }
2262 }
2263#elif defined(HOST_SPARC)
2264 {
2265 char name[256];
2266 int type;
2267 int addend;
2268 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2269 if (rel->r_offset >= start_offset &&
2270 rel->r_offset < start_offset + copy_size) {
2271 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
2272 get_reloc_expr(name, sizeof(name), sym_name);
2273 type = ELF32_R_TYPE(rel->r_info);
2274 addend = rel->r_addend;
2275 switch(type) {
2276 case R_SPARC_32:
2277 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2278 rel->r_offset - start_offset, name, addend);
2279 break;
2280 case R_SPARC_HI22:
2281 fprintf(outfile,
2282 " *(uint32_t *)(gen_code_ptr + %d) = "
2283 "((*(uint32_t *)(gen_code_ptr + %d)) "
2284 " & ~0x3fffff) "
2285 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2286 rel->r_offset - start_offset,
2287 rel->r_offset - start_offset,
2288 name, addend);
2289 break;
2290 case R_SPARC_LO10:
2291 fprintf(outfile,
2292 " *(uint32_t *)(gen_code_ptr + %d) = "
2293 "((*(uint32_t *)(gen_code_ptr + %d)) "
2294 " & ~0x3ff) "
2295 " | ((%s + %d) & 0x3ff);\n",
2296 rel->r_offset - start_offset,
2297 rel->r_offset - start_offset,
2298 name, addend);
2299 break;
2300 case R_SPARC_WDISP30:
2301 fprintf(outfile,
2302 " *(uint32_t *)(gen_code_ptr + %d) = "
2303 "((*(uint32_t *)(gen_code_ptr + %d)) "
2304 " & ~0x3fffffff) "
2305 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2306 " & 0x3fffffff);\n",
2307 rel->r_offset - start_offset,
2308 rel->r_offset - start_offset,
2309 name, addend,
2310 rel->r_offset - start_offset);
2311 break;
2312 default:
2313 error("unsupported sparc relocation (%d)", type);
2314 }
2315 }
2316 }
2317 }
2318#elif defined(HOST_SPARC64)
2319 {
2320 char name[256];
2321 int type;
2322 int addend;
2323 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2324 if (rel->r_offset >= start_offset &&
2325 rel->r_offset < start_offset + copy_size) {
2326 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
2327 get_reloc_expr(name, sizeof(name), sym_name);
2328 type = ELF64_R_TYPE(rel->r_info);
2329 addend = rel->r_addend;
2330 switch(type) {
2331 case R_SPARC_32:
2332 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2333 rel->r_offset - start_offset, name, addend);
2334 break;
2335 case R_SPARC_HI22:
2336 fprintf(outfile,
2337 " *(uint32_t *)(gen_code_ptr + %d) = "
2338 "((*(uint32_t *)(gen_code_ptr + %d)) "
2339 " & ~0x3fffff) "
2340 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2341 rel->r_offset - start_offset,
2342 rel->r_offset - start_offset,
2343 name, addend);
2344 break;
2345 case R_SPARC_LO10:
2346 fprintf(outfile,
2347 " *(uint32_t *)(gen_code_ptr + %d) = "
2348 "((*(uint32_t *)(gen_code_ptr + %d)) "
2349 " & ~0x3ff) "
2350 " | ((%s + %d) & 0x3ff);\n",
2351 rel->r_offset - start_offset,
2352 rel->r_offset - start_offset,
2353 name, addend);
2354 break;
2355 case R_SPARC_WDISP30:
2356 fprintf(outfile,
2357 " *(uint32_t *)(gen_code_ptr + %d) = "
2358 "((*(uint32_t *)(gen_code_ptr + %d)) "
2359 " & ~0x3fffffff) "
2360 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
2361 " & 0x3fffffff);\n",
2362 rel->r_offset - start_offset,
2363 rel->r_offset - start_offset,
2364 name, addend,
2365 rel->r_offset - start_offset);
2366 break;
2367 default:
2368 error("unsupported sparc64 relocation (%d)", type);
2369 }
2370 }
2371 }
2372 }
2373#elif defined(HOST_ARM)
2374 {
2375 char name[256];
2376 int type;
2377 int addend;
2378
2379 arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
2380 relocs, nb_relocs);
2381
2382 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2383 if (rel->r_offset >= start_offset &&
2384 rel->r_offset < start_offset + copy_size) {
2385 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
2386 /* the compiler leave some unnecessary references to the code */
2387 if (sym_name[0] == '\0')
2388 continue;
2389 get_reloc_expr(name, sizeof(name), sym_name);
2390 type = ELF32_R_TYPE(rel->r_info);
2391 addend = get32((uint32_t *)(text + rel->r_offset));
2392 switch(type) {
2393 case R_ARM_ABS32:
2394 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2395 rel->r_offset - start_offset, name, addend);
2396 break;
2397 case R_ARM_PC24:
2398 fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
2399 rel->r_offset - start_offset, addend, name);
2400 break;
2401 default:
2402 error("unsupported arm relocation (%d)", type);
2403 }
2404 }
2405 }
2406 }
2407#elif defined(HOST_M68K)
2408 {
2409 char name[256];
2410 int type;
2411 int addend;
2412 Elf32_Sym *sym;
2413 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2414 if (rel->r_offset >= start_offset &&
2415 rel->r_offset < start_offset + copy_size) {
2416 sym = &(symtab[ELFW(R_SYM)(rel->r_info)]);
2417 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
2418 get_reloc_expr(name, sizeof(name), sym_name);
2419 type = ELF32_R_TYPE(rel->r_info);
2420 addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend;
2421 switch(type) {
2422 case R_68K_32:
2423 fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ;
2424 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
2425 rel->r_offset - start_offset, name, addend );
2426 break;
2427 case R_68K_PC32:
2428 fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset);
2429 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
2430 rel->r_offset - start_offset, name, rel->r_offset - start_offset, /*sym->st_value+*/ addend);
2431 break;
2432 default:
2433 error("unsupported m68k relocation (%d)", type);
2434 }
2435 }
2436 }
2437 }
2438#else
2439#error unsupported CPU
2440#endif
2441 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
2442 fprintf(outfile, "}\n");
2443 fprintf(outfile, "break;\n\n");
2444 } else {
2445 fprintf(outfile, "static inline void gen_%s(", name);
2446 if (nb_args == 0) {
2447 fprintf(outfile, "void");
2448 } else {
2449 for(i = 0; i < nb_args; i++) {
2450 if (i != 0)
2451 fprintf(outfile, ", ");
2452 fprintf(outfile, "long param%d", i + 1);
2453 }
2454 }
2455 fprintf(outfile, ")\n");
2456 fprintf(outfile, "{\n");
2457 for(i = 0; i < nb_args; i++) {
2458 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
2459 }
2460 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
2461 fprintf(outfile, "}\n\n");
2462 }
2463}
2464
2465int gen_file(FILE *outfile, int out_type)
2466{
2467 int i;
2468 EXE_SYM *sym;
2469
2470 if (out_type == OUT_INDEX_OP) {
2471 fprintf(outfile, "DEF(end, 0, 0)\n");
2472 fprintf(outfile, "DEF(nop, 0, 0)\n");
2473 fprintf(outfile, "DEF(nop1, 1, 0)\n");
2474 fprintf(outfile, "DEF(nop2, 2, 0)\n");
2475 fprintf(outfile, "DEF(nop3, 3, 0)\n");
2476 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2477 const char *name;
2478 name = get_sym_name(sym);
2479 if (strstart(name, OP_PREFIX, NULL)) {
2480 gen_code(name, sym->st_value, sym->st_size, outfile, 2);
2481 }
2482 }
2483 } else if (out_type == OUT_GEN_OP) {
2484 /* generate gen_xxx functions */
2485 fprintf(outfile, "#include \"dyngen-op.h\"\n");
2486 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2487 const char *name;
2488 name = get_sym_name(sym);
2489 if (strstart(name, OP_PREFIX, NULL)) {
2490#if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
2491 if (sym->st_shndx != text_shndx)
2492 error("invalid section for opcode (0x%x)", sym->st_shndx);
2493#endif
2494 gen_code(name, sym->st_value, sym->st_size, outfile, 0);
2495 }
2496 }
2497
2498 } else {
2499 /* generate big code generation switch */
2500fprintf(outfile,
2501"int dyngen_code(uint8_t *gen_code_buf,\n"
2502" uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
2503" const uint16_t *opc_buf, const uint32_t *opparam_buf, const long *gen_labels)\n"
2504"{\n"
2505" uint8_t *gen_code_ptr;\n"
2506" const uint16_t *opc_ptr;\n"
2507" const uint32_t *opparam_ptr;\n");
2508
2509#ifdef HOST_ARM
2510fprintf(outfile,
2511" uint8_t *last_gen_code_ptr = gen_code_buf;\n"
2512" LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
2513" uint32_t *arm_data_ptr = arm_data_table;\n");
2514#endif
2515
2516fprintf(outfile,
2517"\n"
2518" gen_code_ptr = gen_code_buf;\n"
2519" opc_ptr = opc_buf;\n"
2520" opparam_ptr = opparam_buf;\n");
2521
2522 /* Generate prologue, if needed. */
2523
2524fprintf(outfile,
2525" for(;;) {\n"
2526" switch(*opc_ptr++) {\n"
2527);
2528
2529 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2530 const char *name;
2531 name = get_sym_name(sym);
2532 if (strstart(name, OP_PREFIX, NULL)) {
2533#if 0
2534 printf("%4d: %s pos=0x%08x len=%d\n",
2535 i, name, sym->st_value, sym->st_size);
2536#endif
2537#if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
2538 if (sym->st_shndx != text_shndx)
2539 error("invalid section for opcode (0x%x)", sym->st_shndx);
2540#endif
2541 gen_code(name, sym->st_value, sym->st_size, outfile, 1);
2542 }
2543 }
2544
2545fprintf(outfile,
2546" case INDEX_op_nop:\n"
2547" break;\n"
2548" case INDEX_op_nop1:\n"
2549" opparam_ptr++;\n"
2550" break;\n"
2551" case INDEX_op_nop2:\n"
2552" opparam_ptr += 2;\n"
2553" break;\n"
2554" case INDEX_op_nop3:\n"
2555" opparam_ptr += 3;\n"
2556" break;\n"
2557" default:\n"
2558" goto the_end;\n"
2559" }\n");
2560
2561#ifdef HOST_ARM
2562/* generate constant table if needed */
2563fprintf(outfile,
2564" if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
2565" gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
2566" last_gen_code_ptr = gen_code_ptr;\n"
2567" arm_ldr_ptr = arm_ldr_table;\n"
2568" arm_data_ptr = arm_data_table;\n"
2569" }\n");
2570#endif
2571
2572
2573fprintf(outfile,
2574" }\n"
2575" the_end:\n"
2576);
2577
2578/* generate some code patching */
2579#ifdef HOST_ARM
2580fprintf(outfile, "gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 0);\n");
2581#endif
2582 /* flush instruction cache */
2583 fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
2584
2585 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
2586 fprintf(outfile, "}\n\n");
2587
2588 }
2589
2590 return 0;
2591}
2592
2593void usage(void)
2594{
2595 printf("dyngen (c) 2003 Fabrice Bellard\n"
2596 "usage: dyngen [-o outfile] [-c] objfile\n"
2597 "Generate a dynamic code generator from an object file\n"
2598 "-c output enum of operations\n"
2599 "-g output gen_op_xx() functions\n"
2600 );
2601 exit(1);
2602}
2603
2604int main(int argc, char **argv)
2605{
2606 int c, out_type;
2607 const char *filename, *outfilename;
2608 FILE *outfile;
2609
2610 outfilename = "out.c";
2611 out_type = OUT_CODE;
2612 for(;;) {
2613 c = getopt(argc, argv, "ho:cg");
2614 if (c == -1)
2615 break;
2616 switch(c) {
2617 case 'h':
2618 usage();
2619 break;
2620 case 'o':
2621 outfilename = optarg;
2622 break;
2623 case 'c':
2624 out_type = OUT_INDEX_OP;
2625 break;
2626 case 'g':
2627 out_type = OUT_GEN_OP;
2628 break;
2629 }
2630 }
2631 if (optind >= argc)
2632 usage();
2633 filename = argv[optind];
2634 outfile = fopen(outfilename, "w");
2635 if (!outfile)
2636 error("could not open '%s'", outfilename);
2637
2638 load_object(filename);
2639 gen_file(outfile, out_type);
2640 fclose(outfile);
2641 return 0;
2642}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette