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