]> git.proxmox.com Git - qemu.git/blame - dyngen.c
use mask in C0_status (Ralf Baechle)
[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);
b48a8bb6
FB
638 if (name[0] == '.')
639 return NULL;
67b915a5 640 return name;
367e86e8
FB
641}
642
82eec0a1
FB
643static host_ulong get_rel_offset(EXE_RELOC *rel)
644{
645 return rel->r_offset;
646}
647
67b915a5 648struct external_scnhdr *find_coff_section(struct external_scnhdr *shdr, int shnum, const char *name)
367e86e8
FB
649{
650 int i;
651 const char *shname;
67b915a5 652 struct external_scnhdr *sec;
367e86e8
FB
653
654 for(i = 0; i < shnum; i++) {
655 sec = &shdr[i];
67b915a5 656 if (!sec->s_name)
367e86e8 657 continue;
67b915a5 658 shname = sec->s_name;
367e86e8
FB
659 if (!strcmp(shname, name))
660 return sec;
661 }
662 return NULL;
663}
664
67b915a5
FB
665/* load a coff object file */
666int load_object(const char *filename)
fe319756 667{
67b915a5
FB
668 int fd;
669 struct external_scnhdr *sec, *text_sec, *data_sec;
fe319756 670 int i;
67b915a5
FB
671 struct external_syment *ext_sym;
672 struct external_reloc *coff_relocs;
673 struct external_reloc *ext_rel;
674 uint32_t *n_strtab;
675 EXE_SYM *sym;
676 EXE_RELOC *rel;
677
678 fd = open(filename, O_RDONLY
679#ifdef _WIN32
680 | O_BINARY
681#endif
682 );
683 if (fd < 0)
684 error("can't open file '%s'", filename);
685
686 /* Read COFF header. */
687 if (read(fd, &fhdr, sizeof (fhdr)) != sizeof (fhdr))
688 error("unable to read file header");
fe319756 689
67b915a5
FB
690 /* Check COFF identification. */
691 if (fhdr.f_magic != I386MAGIC) {
692 error("bad COFF header");
693 }
694 do_swap = 0;
695
696 /* read section headers */
697 shdr = load_data(fd, sizeof(struct external_filehdr) + fhdr.f_opthdr, fhdr.f_nscns * sizeof(struct external_scnhdr));
698
699 /* read all section data */
700 sdata = malloc(sizeof(void *) * fhdr.f_nscns);
701 memset(sdata, 0, sizeof(void *) * fhdr.f_nscns);
702
703 const char *p;
704 for(i = 0;i < fhdr.f_nscns; i++) {
fe319756 705 sec = &shdr[i];
67b915a5
FB
706 if (!strstart(sec->s_name, ".bss", &p))
707 sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size);
fe319756 708 }
fe319756 709
367e86e8 710
67b915a5
FB
711 /* text section */
712 text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text");
713 if (!text_sec)
714 error("could not find .text section");
715 coff_text_shndx = text_sec - shdr;
716 text = sdata[coff_text_shndx];
717
718 /* data section */
719 data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data");
720 if (!data_sec)
721 error("could not find .data section");
722 coff_data_shndx = data_sec - shdr;
723
724 coff_symtab = load_data(fd, fhdr.f_symptr, fhdr.f_nsyms*SYMESZ);
725 for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
726 for(i=0;i<8;i++)
727 printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]);
728 printf("\n");
367e86e8 729 }
367e86e8 730
67b915a5
FB
731
732 n_strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), STRTAB_SIZE);
733 strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), *n_strtab);
734
735 nb_syms = fhdr.f_nsyms;
736
737 for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
738 if (strstart(ext_sym->e.e_name, ".text", NULL))
739 text_shndx = ext_sym->e_scnum;
740 if (strstart(ext_sym->e.e_name, ".data", NULL))
741 data_shndx = ext_sym->e_scnum;
367e86e8 742 }
67b915a5
FB
743
744 /* set coff symbol */
745 symtab = malloc(sizeof(struct coff_sym) * nb_syms);
746
747 int aux_size, j;
748 for (i = 0, ext_sym = coff_symtab, sym = symtab; i < nb_syms; i++, ext_sym++, sym++) {
749 memset(sym, 0, sizeof(*sym));
750 sym->st_syment = ext_sym;
751 sym_ent_name(ext_sym, sym);
752 sym->st_value = ext_sym->e_value;
753
754 aux_size = *(int8_t *)ext_sym->e_numaux;
755 if (ext_sym->e_scnum == text_shndx && ext_sym->e_type == T_FUNCTION) {
756 for (j = aux_size + 1; j < nb_syms - i; j++) {
757 if ((ext_sym + j)->e_scnum == text_shndx &&
758 (ext_sym + j)->e_type == T_FUNCTION ){
759 sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
760 break;
761 } else if (j == nb_syms - i - 1) {
762 sec = &shdr[coff_text_shndx];
763 sym->st_size = sec->s_size - ext_sym->e_value;
764 break;
765 }
766 }
767 } else if (ext_sym->e_scnum == data_shndx && *(uint8_t *)ext_sym->e_sclass == C_EXTERNAL) {
768 for (j = aux_size + 1; j < nb_syms - i; j++) {
769 if ((ext_sym + j)->e_scnum == data_shndx) {
770 sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
771 break;
772 } else if (j == nb_syms - i - 1) {
773 sec = &shdr[coff_data_shndx];
774 sym->st_size = sec->s_size - ext_sym->e_value;
775 break;
776 }
777 }
778 } else {
779 sym->st_size = 0;
780 }
781
782 sym->st_type = ext_sym->e_type;
783 sym->st_shndx = ext_sym->e_scnum;
784 }
785
786
787 /* find text relocations, if any */
788 sec = &shdr[coff_text_shndx];
789 coff_relocs = load_data(fd, sec->s_relptr, sec->s_nreloc*RELSZ);
790 nb_relocs = sec->s_nreloc;
791
792 /* set coff relocation */
793 relocs = malloc(sizeof(struct coff_rel) * nb_relocs);
794 for (i = 0, ext_rel = coff_relocs, rel = relocs; i < nb_relocs;
795 i++, ext_rel++, rel++) {
796 memset(rel, 0, sizeof(*rel));
797 rel->r_reloc = ext_rel;
798 rel->r_offset = *(uint32_t *)ext_rel->r_vaddr;
799 rel->r_type = *(uint16_t *)ext_rel->r_type;
800 }
801 return 0;
367e86e8
FB
802}
803
67b915a5
FB
804#endif /* CONFIG_FORMAT_COFF */
805
82eec0a1
FB
806#ifdef CONFIG_FORMAT_MACH
807
808/* File Header */
809struct mach_header mach_hdr;
810
811/* commands */
812struct segment_command *segment = 0;
813struct dysymtab_command *dysymtabcmd = 0;
814struct symtab_command *symtabcmd = 0;
815
816/* section */
817struct section *section_hdr;
818struct section *text_sec_hdr;
819uint8_t **sdata;
820
821/* relocs */
822struct relocation_info *relocs;
823
824/* symbols */
825EXE_SYM *symtab;
826struct nlist *symtab_std;
827char *strtab;
828
829/* indirect symbols */
830uint32_t *tocdylib;
831
832/* Utility functions */
833
834static inline char *find_str_by_index(int index)
835{
836 return strtab+index;
837}
838
839/* Used by dyngen common code */
840static char *get_sym_name(EXE_SYM *sym)
841{
842 char *name = find_str_by_index(sym->n_un.n_strx);
843
844 if ( sym->n_type & N_STAB ) /* Debug symbols are ignored */
845 return "debug";
846
847 if(!name)
848 return name;
849 if(name[0]=='_')
850 return name + 1;
851 else
852 return name;
853}
854
855/* find a section index given its segname, sectname */
856static int find_mach_sec_index(struct section *section_hdr, int shnum, const char *segname,
857 const char *sectname)
858{
859 int i;
860 struct section *sec = section_hdr;
861
862 for(i = 0; i < shnum; i++, sec++) {
863 if (!sec->segname || !sec->sectname)
864 continue;
865 if (!strcmp(sec->sectname, sectname) && !strcmp(sec->segname, segname))
866 return i;
867 }
868 return -1;
869}
870
871/* find a section header given its segname, sectname */
872struct section *find_mach_sec_hdr(struct section *section_hdr, int shnum, const char *segname,
873 const char *sectname)
874{
875 int index = find_mach_sec_index(section_hdr, shnum, segname, sectname);
876 if(index == -1)
877 return NULL;
878 return section_hdr+index;
879}
880
881
882static inline void fetch_next_pair_value(struct relocation_info * rel, unsigned int *value)
883{
884 struct scattered_relocation_info * scarel;
885
886 if(R_SCATTERED & rel->r_address) {
887 scarel = (struct scattered_relocation_info*)rel;
888 if(scarel->r_type != PPC_RELOC_PAIR)
889 error("fetch_next_pair_value: looking for a pair which was not found (1)");
890 *value = scarel->r_value;
891 } else {
892 if(rel->r_type != PPC_RELOC_PAIR)
893 error("fetch_next_pair_value: looking for a pair which was not found (2)");
894 *value = rel->r_address;
895 }
896}
897
898/* find a sym name given its value, in a section number */
899static const char * find_sym_with_value_and_sec_number( int value, int sectnum, int * offset )
900{
901 int i, ret = -1;
902
903 for( i = 0 ; i < nb_syms; i++ )
904 {
905 if( !(symtab[i].n_type & N_STAB) && (symtab[i].n_type & N_SECT) &&
906 (symtab[i].n_sect == sectnum) && (symtab[i].st_value <= value) )
907 {
908 if( (ret<0) || (symtab[i].st_value >= symtab[ret].st_value) )
909 ret = i;
910 }
911 }
912 if( ret < 0 ) {
913 *offset = 0;
914 return 0;
915 } else {
916 *offset = value - symtab[ret].st_value;
917 return get_sym_name(&symtab[ret]);
918 }
919}
920
921/*
922 * Find symbol name given a (virtual) address, and a section which is of type
923 * S_NON_LAZY_SYMBOL_POINTERS or S_LAZY_SYMBOL_POINTERS or S_SYMBOL_STUBS
924 */
925static const char * find_reloc_name_in_sec_ptr(int address, struct section * sec_hdr)
926{
927 unsigned int tocindex, symindex, size;
928 const char *name = 0;
929
930 /* Sanity check */
931 if(!( address >= sec_hdr->addr && address < (sec_hdr->addr + sec_hdr->size) ) )
932 return (char*)0;
933
934 if( sec_hdr->flags & S_SYMBOL_STUBS ){
935 size = sec_hdr->reserved2;
936 if(size == 0)
937 error("size = 0");
938
939 }
940 else if( sec_hdr->flags & S_LAZY_SYMBOL_POINTERS ||
941 sec_hdr->flags & S_NON_LAZY_SYMBOL_POINTERS)
942 size = sizeof(unsigned long);
943 else
944 return 0;
945
946 /* Compute our index in toc */
947 tocindex = (address - sec_hdr->addr)/size;
948 symindex = tocdylib[sec_hdr->reserved1 + tocindex];
949
950 name = get_sym_name(&symtab[symindex]);
951
952 return name;
953}
954
955static const char * find_reloc_name_given_its_address(int address)
956{
957 unsigned int i;
958 for(i = 0; i < segment->nsects ; i++)
959 {
960 const char * name = find_reloc_name_in_sec_ptr(address, &section_hdr[i]);
961 if((long)name != -1)
962 return name;
963 }
964 return 0;
965}
966
967static const char * get_reloc_name(EXE_RELOC * rel, int * sslide)
968{
969 char * name = 0;
970 struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
971 int sectnum = rel->r_symbolnum;
972 int sectoffset;
973 int other_half=0;
974
975 /* init the slide value */
976 *sslide = 0;
977
978 if(R_SCATTERED & rel->r_address)
979 return (char *)find_reloc_name_given_its_address(sca_rel->r_value);
980
981 if(rel->r_extern)
982 {
983 /* ignore debug sym */
984 if ( symtab[rel->r_symbolnum].n_type & N_STAB )
985 return 0;
986 return get_sym_name(&symtab[rel->r_symbolnum]);
987 }
988
989 /* Intruction contains an offset to the symbols pointed to, in the rel->r_symbolnum section */
990 sectoffset = *(uint32_t *)(text + rel->r_address) & 0xffff;
991
992 if(sectnum==0xffffff)
993 return 0;
994
995 /* Sanity Check */
996 if(sectnum > segment->nsects)
997 error("sectnum > segment->nsects");
998
999 switch(rel->r_type)
1000 {
91aa5d49 1001 case PPC_RELOC_LO16: fetch_next_pair_value(rel+1, &other_half); sectoffset |= (other_half << 16);
82eec0a1 1002 break;
91aa5d49 1003 case PPC_RELOC_HI16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset << 16) | (uint16_t)(other_half & 0xffff);
82eec0a1 1004 break;
91aa5d49 1005 case PPC_RELOC_HA16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset << 16) + (int16_t)(other_half & 0xffff);
82eec0a1
FB
1006 break;
1007 case PPC_RELOC_BR24:
1008 sectoffset = ( *(uint32_t *)(text + rel->r_address) & 0x03fffffc );
1009 if (sectoffset & 0x02000000) sectoffset |= 0xfc000000;
1010 break;
1011 default:
1012 error("switch(rel->type) not found");
1013 }
1014
1015 if(rel->r_pcrel)
1016 sectoffset += rel->r_address;
1017
1018 if (rel->r_type == PPC_RELOC_BR24)
1019 name = (char *)find_reloc_name_in_sec_ptr((int)sectoffset, &section_hdr[sectnum-1]);
1020
1021 /* search it in the full symbol list, if not found */
1022 if(!name)
1023 name = (char *)find_sym_with_value_and_sec_number(sectoffset, sectnum, sslide);
1024
1025 return name;
1026}
1027
1028/* Used by dyngen common code */
1029static const char * get_rel_sym_name(EXE_RELOC * rel)
1030{
1031 int sslide;
1032 return get_reloc_name( rel, &sslide);
1033}
1034
1035/* Used by dyngen common code */
1036static host_ulong get_rel_offset(EXE_RELOC *rel)
1037{
1038 struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
1039 if(R_SCATTERED & rel->r_address)
1040 return sca_rel->r_address;
1041 else
1042 return rel->r_address;
1043}
1044
1045/* load a mach-o object file */
1046int load_object(const char *filename)
1047{
1048 int fd;
1049 unsigned int offset_to_segment = 0;
1050 unsigned int offset_to_dysymtab = 0;
1051 unsigned int offset_to_symtab = 0;
1052 struct load_command lc;
1053 unsigned int i, j;
1054 EXE_SYM *sym;
1055 struct nlist *syment;
1056
1057 fd = open(filename, O_RDONLY);
1058 if (fd < 0)
1059 error("can't open file '%s'", filename);
1060
1061 /* Read Mach header. */
1062 if (read(fd, &mach_hdr, sizeof (mach_hdr)) != sizeof (mach_hdr))
1063 error("unable to read file header");
1064
1065 /* Check Mach identification. */
1066 if (!check_mach_header(mach_hdr)) {
1067 error("bad Mach header");
1068 }
1069
1070 if (mach_hdr.cputype != CPU_TYPE_POWERPC)
1071 error("Unsupported CPU");
1072
1073 if (mach_hdr.filetype != MH_OBJECT)
1074 error("Unsupported Mach Object");
1075
1076 /* read segment headers */
1077 for(i=0, j=sizeof(mach_hdr); i<mach_hdr.ncmds ; i++)
1078 {
1079 if(read(fd, &lc, sizeof(struct load_command)) != sizeof(struct load_command))
1080 error("unable to read load_command");
1081 if(lc.cmd == LC_SEGMENT)
1082 {
1083 offset_to_segment = j;
1084 lseek(fd, offset_to_segment, SEEK_SET);
1085 segment = malloc(sizeof(struct segment_command));
1086 if(read(fd, segment, sizeof(struct segment_command)) != sizeof(struct segment_command))
1087 error("unable to read LC_SEGMENT");
1088 }
1089 if(lc.cmd == LC_DYSYMTAB)
1090 {
1091 offset_to_dysymtab = j;
1092 lseek(fd, offset_to_dysymtab, SEEK_SET);
1093 dysymtabcmd = malloc(sizeof(struct dysymtab_command));
1094 if(read(fd, dysymtabcmd, sizeof(struct dysymtab_command)) != sizeof(struct dysymtab_command))
1095 error("unable to read LC_DYSYMTAB");
1096 }
1097 if(lc.cmd == LC_SYMTAB)
1098 {
1099 offset_to_symtab = j;
1100 lseek(fd, offset_to_symtab, SEEK_SET);
1101 symtabcmd = malloc(sizeof(struct symtab_command));
1102 if(read(fd, symtabcmd, sizeof(struct symtab_command)) != sizeof(struct symtab_command))
1103 error("unable to read LC_SYMTAB");
1104 }
1105 j+=lc.cmdsize;
1106
1107 lseek(fd, j, SEEK_SET);
1108 }
1109
1110 if(!segment)
1111 error("unable to find LC_SEGMENT");
1112
1113 /* read section headers */
1114 section_hdr = load_data(fd, offset_to_segment + sizeof(struct segment_command), segment->nsects * sizeof(struct section));
1115
1116 /* read all section data */
1117 sdata = (uint8_t **)malloc(sizeof(void *) * segment->nsects);
1118 memset(sdata, 0, sizeof(void *) * segment->nsects);
1119
1120 /* Load the data in section data */
1121 for(i = 0; i < segment->nsects; i++) {
1122 sdata[i] = load_data(fd, section_hdr[i].offset, section_hdr[i].size);
1123 }
1124
1125 /* text section */
1126 text_sec_hdr = find_mach_sec_hdr(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT);
1127 i = find_mach_sec_index(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT);
1128 if (i == -1 || !text_sec_hdr)
1129 error("could not find __TEXT,__text section");
1130 text = sdata[i];
1131
1132 /* Make sure dysym was loaded */
1133 if(!(int)dysymtabcmd)
1134 error("could not find __DYSYMTAB segment");
1135
1136 /* read the table of content of the indirect sym */
1137 tocdylib = load_data( fd, dysymtabcmd->indirectsymoff, dysymtabcmd->nindirectsyms * sizeof(uint32_t) );
1138
1139 /* Make sure symtab was loaded */
1140 if(!(int)symtabcmd)
1141 error("could not find __SYMTAB segment");
1142 nb_syms = symtabcmd->nsyms;
1143
1144 symtab_std = load_data(fd, symtabcmd->symoff, symtabcmd->nsyms * sizeof(struct nlist));
1145 strtab = load_data(fd, symtabcmd->stroff, symtabcmd->strsize);
1146
1147 symtab = malloc(sizeof(EXE_SYM) * nb_syms);
1148
1149 /* Now transform the symtab, to an extended version, with the sym size, and the C name */
1150 for(i = 0, sym = symtab, syment = symtab_std; i < nb_syms; i++, sym++, syment++) {
82eec0a1
FB
1151 struct nlist *sym_follow, *sym_next = 0;
1152 unsigned int j;
82eec0a1
FB
1153 memset(sym, 0, sizeof(*sym));
1154
91aa5d49 1155 if ( syment->n_type & N_STAB ) /* Debug symbols are skipped */
82eec0a1
FB
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
bef79c34
FB
1188void get_reloc_expr(char *name, int name_size, const char *sym_name)
1189{
1190 const char *p;
1191
1192 if (strstart(sym_name, "__op_param", &p)) {
1193 snprintf(name, name_size, "param%s", p);
1194 } else if (strstart(sym_name, "__op_gen_label", &p)) {
1195 snprintf(name, name_size, "gen_labels[param%s]", p);
1196 } else {
1197#ifdef HOST_SPARC
1198 if (sym_name[0] == '.')
1199 snprintf(name, sizeof(name),
1200 "(long)(&__dot_%s)",
1201 sym_name + 1);
1202 else
1203#endif
1204 snprintf(name, name_size, "(long)(&%s)", sym_name);
1205 }
1206}
1207
b8076a74
FB
1208#ifdef HOST_IA64
1209
1210#define PLT_ENTRY_SIZE 16 /* 1 bundle containing "brl" */
1211
1212struct plt_entry {
1213 struct plt_entry *next;
1214 const char *name;
1215 unsigned long addend;
1216} *plt_list;
1217
1218static int
1219get_plt_index (const char *name, unsigned long addend)
1220{
1221 struct plt_entry *plt, *prev= NULL;
1222 int index = 0;
1223
1224 /* see if we already have an entry for this target: */
1225 for (plt = plt_list; plt; ++index, prev = plt, plt = plt->next)
1226 if (strcmp(plt->name, name) == 0 && plt->addend == addend)
1227 return index;
1228
1229 /* nope; create a new PLT entry: */
1230
1231 plt = malloc(sizeof(*plt));
1232 if (!plt) {
1233 perror("malloc");
1234 exit(1);
1235 }
1236 memset(plt, 0, sizeof(*plt));
1237 plt->name = strdup(name);
1238 plt->addend = addend;
1239
1240 /* append to plt-list: */
1241 if (prev)
1242 prev->next = plt;
1243 else
1244 plt_list = plt;
1245 return index;
1246}
1247
1248#endif
1249
ff1f20a3
FB
1250#ifdef HOST_ARM
1251
1252int arm_emit_ldr_info(const char *name, unsigned long start_offset,
1253 FILE *outfile, uint8_t *p_start, uint8_t *p_end,
1254 ELF_RELOC *relocs, int nb_relocs)
1255{
1256 uint8_t *p;
1257 uint32_t insn;
1258 int offset, min_offset, pc_offset, data_size;
1259 uint8_t data_allocated[1024];
1260 unsigned int data_index;
1261
1262 memset(data_allocated, 0, sizeof(data_allocated));
1263
1264 p = p_start;
1265 min_offset = p_end - p_start;
1266 while (p < p_start + min_offset) {
1267 insn = get32((uint32_t *)p);
1268 if ((insn & 0x0d5f0000) == 0x051f0000) {
1269 /* ldr reg, [pc, #im] */
1270 offset = insn & 0xfff;
1271 if (!(insn & 0x00800000))
1272 offset = -offset;
1273 if ((offset & 3) !=0)
1274 error("%s:%04x: ldr pc offset must be 32 bit aligned",
1275 name, start_offset + p - p_start);
1276 pc_offset = p - p_start + offset + 8;
1277 if (pc_offset <= (p - p_start) ||
1278 pc_offset >= (p_end - p_start))
1279 error("%s:%04x: ldr pc offset must point inside the function code",
1280 name, start_offset + p - p_start);
1281 if (pc_offset < min_offset)
1282 min_offset = pc_offset;
1283 if (outfile) {
1284 /* ldr position */
1285 fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
1286 p - p_start);
1287 /* ldr data index */
1288 data_index = ((p_end - p_start) - pc_offset - 4) >> 2;
1289 fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
1290 data_index);
1291 fprintf(outfile, " arm_ldr_ptr++;\n");
1292 if (data_index >= sizeof(data_allocated))
1293 error("%s: too many data", name);
1294 if (!data_allocated[data_index]) {
1295 ELF_RELOC *rel;
1296 int i, addend, type;
1297 const char *sym_name, *p;
1298 char relname[1024];
1299
1300 data_allocated[data_index] = 1;
1301
1302 /* data value */
1303 addend = get32((uint32_t *)(p_start + pc_offset));
1304 relname[0] = '\0';
1305 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1306 if (rel->r_offset == (pc_offset + start_offset)) {
67b915a5 1307 sym_name = get_rel_sym_name(rel);
ff1f20a3 1308 /* the compiler leave some unnecessary references to the code */
bef79c34 1309 get_reloc_expr(relname, sizeof(relname), sym_name);
ff1f20a3
FB
1310 type = ELF32_R_TYPE(rel->r_info);
1311 if (type != R_ARM_ABS32)
1312 error("%s: unsupported data relocation", name);
1313 break;
1314 }
1315 }
1316 fprintf(outfile, " arm_data_ptr[%d] = 0x%x",
1317 data_index, addend);
1318 if (relname[0] != '\0')
1319 fprintf(outfile, " + %s", relname);
1320 fprintf(outfile, ";\n");
1321 }
1322 }
1323 }
1324 p += 4;
1325 }
1326 data_size = (p_end - p_start) - min_offset;
1327 if (data_size > 0 && outfile) {
1328 fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2);
1329 }
1330
1331 /* the last instruction must be a mov pc, lr */
1332 if (p == p_start)
1333 goto arm_ret_error;
1334 p -= 4;
1335 insn = get32((uint32_t *)p);
1336 if ((insn & 0xffff0000) != 0xe91b0000) {
1337 arm_ret_error:
1338 if (!outfile)
1339 printf("%s: invalid epilog\n", name);
1340 }
1341 return p - p_start;
1342}
1343#endif
1344
1345
367e86e8
FB
1346#define MAX_ARGS 3
1347
1348/* generate op code */
ce11fedc 1349void gen_code(const char *name, host_ulong offset, host_ulong size,
67b915a5 1350 FILE *outfile, int gen_switch)
367e86e8
FB
1351{
1352 int copy_size = 0;
1353 uint8_t *p_start, *p_end;
ae228531 1354 host_ulong start_offset;
ce11fedc 1355 int nb_args, i, n;
367e86e8
FB
1356 uint8_t args_present[MAX_ARGS];
1357 const char *sym_name, *p;
67b915a5 1358 EXE_RELOC *rel;
367e86e8 1359
ae228531
FB
1360 /* Compute exact size excluding prologue and epilogue instructions.
1361 * Increment start_offset to skip epilogue instructions, then compute
1362 * copy_size the indicate the size of the remaining instructions (in
1363 * bytes).
1364 */
367e86e8
FB
1365 p_start = text + offset;
1366 p_end = p_start + size;
ae228531 1367 start_offset = offset;
c4687878 1368#if defined(HOST_I386) || defined(HOST_X86_64)
67b915a5
FB
1369#ifdef CONFIG_FORMAT_COFF
1370 {
1371 uint8_t *p;
1372 p = p_end - 1;
1373 if (p == p_start)
1374 error("empty code for %s", name);
1375 while (*p != 0xc3) {
1376 p--;
1377 if (p <= p_start)
d4e8164f 1378 error("ret or jmp expected at the end of %s", name);
367e86e8 1379 }
67b915a5
FB
1380 copy_size = p - p_start;
1381 }
1382#else
1383 {
1384 int len;
1385 len = p_end - p_start;
1386 if (len == 0)
1387 error("empty code for %s", name);
1388 if (p_end[-1] == 0xc3) {
1389 len--;
1390 } else {
1391 error("ret or jmp expected at the end of %s", name);
367e86e8 1392 }
67b915a5
FB
1393 copy_size = len;
1394 }
1395#endif
1396#elif defined(HOST_PPC)
1397 {
1398 uint8_t *p;
1399 p = (void *)(p_end - 4);
1400 if (p == p_start)
1401 error("empty code for %s", name);
1402 if (get32((uint32_t *)p) != 0x4e800020)
1403 error("blr expected at the end of %s", name);
1404 copy_size = p - p_start;
1405 }
1406#elif defined(HOST_S390)
1407 {
1408 uint8_t *p;
1409 p = (void *)(p_end - 2);
1410 if (p == p_start)
1411 error("empty code for %s", name);
1412 if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
1413 error("br %%r14 expected at the end of %s", name);
1414 copy_size = p - p_start;
1415 }
1416#elif defined(HOST_ALPHA)
1417 {
1418 uint8_t *p;
1419 p = p_end - 4;
630be16f 1420#if 0
67b915a5
FB
1421 /* XXX: check why it occurs */
1422 if (p == p_start)
1423 error("empty code for %s", name);
630be16f 1424#endif
67b915a5
FB
1425 if (get32((uint32_t *)p) != 0x6bfa8001)
1426 error("ret expected at the end of %s", name);
1427 copy_size = p - p_start;
1428 }
1429#elif defined(HOST_IA64)
1430 {
1431 uint8_t *p;
1432 p = (void *)(p_end - 4);
1433 if (p == p_start)
1434 error("empty code for %s", name);
1435 /* br.ret.sptk.many b0;; */
1436 /* 08 00 84 00 */
1437 if (get32((uint32_t *)p) != 0x00840008)
1438 error("br.ret.sptk.many b0;; expected at the end of %s", name);
b8076a74 1439 copy_size = p_end - p_start;
67b915a5
FB
1440 }
1441#elif defined(HOST_SPARC)
1442 {
1443 uint32_t start_insn, end_insn1, end_insn2;
1444 uint8_t *p;
1445 p = (void *)(p_end - 8);
1446 if (p <= p_start)
1447 error("empty code for %s", name);
1448 start_insn = get32((uint32_t *)(p_start + 0x0));
1449 end_insn1 = get32((uint32_t *)(p + 0x0));
1450 end_insn2 = get32((uint32_t *)(p + 0x4));
1451 if ((start_insn & ~0x1fff) == 0x9de3a000) {
1452 p_start += 0x4;
1453 start_offset += 0x4;
1454 if ((int)(start_insn | ~0x1fff) < -128)
1455 error("Found bogus save at the start of %s", name);
1456 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
1457 error("ret; restore; not found at end of %s", name);
1458 } else {
1459 error("No save at the beginning of %s", name);
1460 }
ff1f20a3 1461#if 0
67b915a5
FB
1462 /* Skip a preceeding nop, if present. */
1463 if (p > p_start) {
1464 skip_insn = get32((uint32_t *)(p - 0x4));
1465 if (skip_insn == 0x01000000)
1466 p -= 4;
1467 }
ff1f20a3 1468#endif
67b915a5
FB
1469 copy_size = p - p_start;
1470 }
1471#elif defined(HOST_SPARC64)
1472 {
1473 uint32_t start_insn, end_insn1, end_insn2, skip_insn;
1474 uint8_t *p;
1475 p = (void *)(p_end - 8);
1476 if (p <= p_start)
1477 error("empty code for %s", name);
1478 start_insn = get32((uint32_t *)(p_start + 0x0));
1479 end_insn1 = get32((uint32_t *)(p + 0x0));
1480 end_insn2 = get32((uint32_t *)(p + 0x4));
1481 if ((start_insn & ~0x1fff) == 0x9de3a000) {
1482 p_start += 0x4;
1483 start_offset += 0x4;
1484 if ((int)(start_insn | ~0x1fff) < -256)
1485 error("Found bogus save at the start of %s", name);
1486 if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
1487 error("ret; restore; not found at end of %s", name);
1488 } else {
1489 error("No save at the beginning of %s", name);
1490 }
1491
1492 /* Skip a preceeding nop, if present. */
1493 if (p > p_start) {
1494 skip_insn = get32((uint32_t *)(p - 0x4));
1495 if (skip_insn == 0x01000000)
1496 p -= 4;
1497 }
1498
1499 copy_size = p - p_start;
1500 }
1501#elif defined(HOST_ARM)
1502 {
ff1f20a3
FB
1503 if ((p_end - p_start) <= 16)
1504 error("%s: function too small", name);
1505 if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
1506 (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
1507 get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
1508 error("%s: invalid prolog", name);
1509 p_start += 12;
1510 start_offset += 12;
1511 copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end,
1512 relocs, nb_relocs);
367e86e8 1513 }
67b915a5
FB
1514#elif defined(HOST_M68K)
1515 {
1516 uint8_t *p;
1517 p = (void *)(p_end - 2);
1518 if (p == p_start)
1519 error("empty code for %s", name);
1520 // remove NOP's, probably added for alignment
1521 while ((get16((uint16_t *)p) == 0x4e71) &&
1522 (p>p_start))
1523 p -= 2;
1524 if (get16((uint16_t *)p) != 0x4e75)
1525 error("rts expected at the end of %s", name);
1526 copy_size = p - p_start;
1527 }
1528#else
1529#error unsupported CPU
1530#endif
367e86e8
FB
1531
1532 /* compute the number of arguments by looking at the relocations */
1533 for(i = 0;i < MAX_ARGS; i++)
1534 args_present[i] = 0;
1535
ce11fedc 1536 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
82eec0a1
FB
1537 host_ulong offset = get_rel_offset(rel);
1538 if (offset >= start_offset &&
1539 offset < start_offset + (p_end - p_start)) {
67b915a5 1540 sym_name = get_rel_sym_name(rel);
82eec0a1
FB
1541 if(!sym_name)
1542 continue;
c4687878
FB
1543 if (strstart(sym_name, "__op_param", &p) ||
1544 strstart(sym_name, "__op_gen_label", &p)) {
ce11fedc 1545 n = strtoul(p, NULL, 10);
d4e8164f 1546 if (n > MAX_ARGS)
ce11fedc
FB
1547 error("too many arguments in %s", name);
1548 args_present[n - 1] = 1;
367e86e8
FB
1549 }
1550 }
1551 }
1552
1553 nb_args = 0;
1554 while (nb_args < MAX_ARGS && args_present[nb_args])
1555 nb_args++;
1556 for(i = nb_args; i < MAX_ARGS; i++) {
1557 if (args_present[i])
1558 error("inconsistent argument numbering in %s", name);
1559 }
1560
0ea00c9a 1561 if (gen_switch == 2) {
a513fe19 1562 fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
0ea00c9a 1563 } else if (gen_switch == 1) {
dc99065b
FB
1564
1565 /* output C code */
1566 fprintf(outfile, "case INDEX_%s: {\n", name);
1567 if (nb_args > 0) {
1568 fprintf(outfile, " long ");
1569 for(i = 0; i < nb_args; i++) {
1570 if (i != 0)
1571 fprintf(outfile, ", ");
1572 fprintf(outfile, "param%d", i + 1);
1573 }
1574 fprintf(outfile, ";\n");
367e86e8 1575 }
b8076a74
FB
1576#if defined(HOST_IA64)
1577 fprintf(outfile, " extern char %s;\n", name);
1578#else
dc99065b 1579 fprintf(outfile, " extern void %s();\n", name);
b8076a74 1580#endif
dc99065b 1581
ce11fedc 1582 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
82eec0a1
FB
1583 host_ulong offset = get_rel_offset(rel);
1584 if (offset >= start_offset &&
1585 offset < start_offset + (p_end - p_start)) {
67b915a5 1586 sym_name = get_rel_sym_name(rel);
82eec0a1
FB
1587 if(!sym_name)
1588 continue;
d4e8164f
FB
1589 if (*sym_name &&
1590 !strstart(sym_name, "__op_param", NULL) &&
c4687878
FB
1591 !strstart(sym_name, "__op_jmp", NULL) &&
1592 !strstart(sym_name, "__op_gen_label", NULL)) {
d014c98c
FB
1593#if defined(HOST_SPARC)
1594 if (sym_name[0] == '.') {
1595 fprintf(outfile,
1596 "extern char __dot_%s __asm__(\"%s\");\n",
1597 sym_name+1, sym_name);
1598 continue;
1599 }
1600#endif
b8076a74 1601#if defined(__APPLE__)
82eec0a1
FB
1602/* set __attribute((unused)) on darwin because we wan't to avoid warning when we don't use the symbol */
1603 fprintf(outfile, "extern char %s __attribute__((unused));\n", sym_name);
b8076a74
FB
1604#elif defined(HOST_IA64)
1605 if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B)
1606 /*
1607 * PCREL21 br.call targets generally
1608 * are out of range and need to go
1609 * through an "import stub".
1610 */
1611 fprintf(outfile, " extern char %s;\n",
1612 sym_name);
82eec0a1 1613#else
ce11fedc 1614 fprintf(outfile, "extern char %s;\n", sym_name);
82eec0a1 1615#endif
dc99065b
FB
1616 }
1617 }
1618 }
1619
82eec0a1
FB
1620 fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n",
1621 name, (int)(start_offset - offset), copy_size);
d4e8164f
FB
1622
1623 /* emit code offset information */
1624 {
67b915a5 1625 EXE_SYM *sym;
d4e8164f 1626 const char *sym_name, *p;
abcd5da7 1627 unsigned long val;
d4e8164f
FB
1628 int n;
1629
1630 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
67b915a5 1631 sym_name = get_sym_name(sym);
d4e8164f 1632 if (strstart(sym_name, "__op_label", &p)) {
c1e42a13 1633 uint8_t *ptr;
fe319756
FB
1634 unsigned long offset;
1635
d4e8164f
FB
1636 /* test if the variable refers to a label inside
1637 the code we are generating */
67b915a5
FB
1638#ifdef CONFIG_FORMAT_COFF
1639 if (sym->st_shndx == text_shndx) {
1640 ptr = sdata[coff_text_shndx];
1641 } else if (sym->st_shndx == data_shndx) {
1642 ptr = sdata[coff_data_shndx];
1643 } else {
1644 ptr = NULL;
1645 }
82eec0a1
FB
1646#elif defined(CONFIG_FORMAT_MACH)
1647 if(!sym->n_sect)
1648 continue;
1649 ptr = sdata[sym->n_sect-1];
67b915a5 1650#else
fe319756 1651 ptr = sdata[sym->st_shndx];
67b915a5 1652#endif
fe319756
FB
1653 if (!ptr)
1654 error("__op_labelN in invalid section");
1655 offset = sym->st_value;
82eec0a1
FB
1656#ifdef CONFIG_FORMAT_MACH
1657 offset -= section_hdr[sym->n_sect-1].addr;
1658#endif
abcd5da7 1659 val = *(unsigned long *)(ptr + offset);
fe319756
FB
1660#ifdef ELF_USES_RELOCA
1661 {
1662 int reloc_shndx, nb_relocs1, j;
1663
1664 /* try to find a matching relocation */
1665 reloc_shndx = find_reloc(sym->st_shndx);
1666 if (reloc_shndx) {
1667 nb_relocs1 = shdr[reloc_shndx].sh_size /
1668 shdr[reloc_shndx].sh_entsize;
1669 rel = (ELF_RELOC *)sdata[reloc_shndx];
1670 for(j = 0; j < nb_relocs1; j++) {
1671 if (rel->r_offset == offset) {
039de852 1672 val = rel->r_addend;
fe319756
FB
1673 break;
1674 }
1675 rel++;
1676 }
1677 }
1678 }
1679#endif
c4687878 1680 if (val >= start_offset && val <= start_offset + copy_size) {
d4e8164f 1681 n = strtol(p, NULL, 10);
abcd5da7 1682 fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
d4e8164f
FB
1683 }
1684 }
1685 }
1686 }
1687
1688 /* load parameres in variables */
dc99065b
FB
1689 for(i = 0; i < nb_args; i++) {
1690 fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1);
1691 }
1692
1693 /* patch relocations */
ce11fedc 1694#if defined(HOST_I386)
dc99065b 1695 {
dc99065b
FB
1696 char name[256];
1697 int type;
ce11fedc 1698 int addend;
dc99065b 1699 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
ae228531
FB
1700 if (rel->r_offset >= start_offset &&
1701 rel->r_offset < start_offset + copy_size) {
67b915a5 1702 sym_name = get_rel_sym_name(rel);
b48a8bb6
FB
1703 if (!sym_name)
1704 continue;
ecd854fd
FB
1705 if (strstart(sym_name, "__op_jmp", &p)) {
1706 int n;
1707 n = strtol(p, NULL, 10);
1708 /* __op_jmp relocations are done at
1709 runtime to do translated block
1710 chaining: the offset of the instruction
1711 needs to be stored */
1712 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1713 n, rel->r_offset - start_offset);
1714 continue;
1715 }
1716
bef79c34 1717 get_reloc_expr(name, sizeof(name), sym_name);
367e86e8 1718 addend = get32((uint32_t *)(text + rel->r_offset));
67b915a5
FB
1719#ifdef CONFIG_FORMAT_ELF
1720 type = ELF32_R_TYPE(rel->r_info);
367e86e8
FB
1721 switch(type) {
1722 case R_386_32:
ce11fedc 1723 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
ae228531 1724 rel->r_offset - start_offset, name, addend);
367e86e8
FB
1725 break;
1726 case R_386_PC32:
ce11fedc 1727 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
ae228531 1728 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
367e86e8
FB
1729 break;
1730 default:
1731 error("unsupported i386 relocation (%d)", type);
1732 }
67b915a5 1733#elif defined(CONFIG_FORMAT_COFF)
40c3bac3
FB
1734 {
1735 char *temp_name;
1736 int j;
1737 EXE_SYM *sym;
1738 temp_name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx));
1739 if (!strcmp(temp_name, ".data")) {
1740 for (j = 0, sym = symtab; j < nb_syms; j++, sym++) {
1741 if (strstart(sym->st_name, sym_name, NULL)) {
1742 addend -= sym->st_value;
1743 }
1744 }
1745 }
1746 }
67b915a5
FB
1747 type = rel->r_type;
1748 switch(type) {
1749 case DIR32:
1750 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1751 rel->r_offset - start_offset, name, addend);
1752 break;
1753 case DISP32:
1754 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n",
1755 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1756 break;
1757 default:
1758 error("unsupported i386 relocation (%d)", type);
1759 }
1760#else
1761#error unsupport object format
1762#endif
367e86e8 1763 }
dc99065b
FB
1764 }
1765 }
c4687878 1766#elif defined(HOST_X86_64)
bc51c5c9
FB
1767 {
1768 char name[256];
1769 int type;
1770 int addend;
1771 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1772 if (rel->r_offset >= start_offset &&
1773 rel->r_offset < start_offset + copy_size) {
1774 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
bef79c34 1775 get_reloc_expr(name, sizeof(name), sym_name);
bc51c5c9
FB
1776 type = ELF32_R_TYPE(rel->r_info);
1777 addend = rel->r_addend;
1778 switch(type) {
1779 case R_X86_64_32:
1780 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
1781 rel->r_offset - start_offset, name, addend);
1782 break;
1783 case R_X86_64_32S:
1784 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
1785 rel->r_offset - start_offset, name, addend);
1786 break;
1787 case R_X86_64_PC32:
1788 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
1789 rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
1790 break;
1791 default:
c4687878 1792 error("unsupported X86_64 relocation (%d)", type);
bc51c5c9
FB
1793 }
1794 }
1795 }
1796 }
ce11fedc 1797#elif defined(HOST_PPC)
04369ff2 1798 {
82eec0a1 1799#ifdef CONFIG_FORMAT_ELF
04369ff2
FB
1800 char name[256];
1801 int type;
ce11fedc 1802 int addend;
04369ff2 1803 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
ae228531
FB
1804 if (rel->r_offset >= start_offset &&
1805 rel->r_offset < start_offset + copy_size) {
efdea7bf 1806 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
d4e8164f
FB
1807 if (strstart(sym_name, "__op_jmp", &p)) {
1808 int n;
1809 n = strtol(p, NULL, 10);
1810 /* __op_jmp relocations are done at
1811 runtime to do translated block
1812 chaining: the offset of the instruction
1813 needs to be stored */
1814 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1815 n, rel->r_offset - start_offset);
1816 continue;
1817 }
1818
bef79c34 1819 get_reloc_expr(name, sizeof(name), sym_name);
04369ff2
FB
1820 type = ELF32_R_TYPE(rel->r_info);
1821 addend = rel->r_addend;
1822 switch(type) {
1823 case R_PPC_ADDR32:
ce11fedc 1824 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
ae228531 1825 rel->r_offset - start_offset, name, addend);
04369ff2
FB
1826 break;
1827 case R_PPC_ADDR16_LO:
ce11fedc 1828 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
ae228531 1829 rel->r_offset - start_offset, name, addend);
04369ff2
FB
1830 break;
1831 case R_PPC_ADDR16_HI:
ce11fedc 1832 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
ae228531 1833 rel->r_offset - start_offset, name, addend);
04369ff2
FB
1834 break;
1835 case R_PPC_ADDR16_HA:
ce11fedc 1836 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
ae228531 1837 rel->r_offset - start_offset, name, addend);
04369ff2
FB
1838 break;
1839 case R_PPC_REL24:
1840 /* warning: must be at 32 MB distancy */
ce11fedc 1841 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 1842 rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
04369ff2
FB
1843 break;
1844 default:
1845 error("unsupported powerpc relocation (%d)", type);
1846 }
1847 }
1848 }
82eec0a1
FB
1849#elif defined(CONFIG_FORMAT_MACH)
1850 struct scattered_relocation_info *scarel;
1851 struct relocation_info * rel;
1852 char final_sym_name[256];
1853 const char *sym_name;
1854 const char *p;
1855 int slide, sslide;
1856 int i;
1857
1858 for(i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
1859 unsigned int offset, length, value = 0;
1860 unsigned int type, pcrel, isym = 0;
1861 unsigned int usesym = 0;
1862
1863 if(R_SCATTERED & rel->r_address) {
1864 scarel = (struct scattered_relocation_info*)rel;
1865 offset = (unsigned int)scarel->r_address;
1866 length = scarel->r_length;
1867 pcrel = scarel->r_pcrel;
1868 type = scarel->r_type;
1869 value = scarel->r_value;
1870 } else {
1871 value = isym = rel->r_symbolnum;
1872 usesym = (rel->r_extern);
1873 offset = rel->r_address;
1874 length = rel->r_length;
1875 pcrel = rel->r_pcrel;
1876 type = rel->r_type;
1877 }
1878
1879 slide = offset - start_offset;
1880
1881 if (!(offset >= start_offset && offset < start_offset + size))
1882 continue; /* not in our range */
1883
1884 sym_name = get_reloc_name(rel, &sslide);
1885
1886 if(usesym && symtab[isym].n_type & N_STAB)
1887 continue; /* don't handle STAB (debug sym) */
1888
1889 if (sym_name && strstart(sym_name, "__op_jmp", &p)) {
1890 int n;
1891 n = strtol(p, NULL, 10);
1892 fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
1893 n, slide);
1894 continue; /* Nothing more to do */
1895 }
1896
1897 if(!sym_name)
1898 {
1899 fprintf(outfile, "/* #warning relocation not handled in %s (value 0x%x, %s, offset 0x%x, length 0x%x, %s, type 0x%x) */\n",
1900 name, value, usesym ? "use sym" : "don't use sym", offset, length, pcrel ? "pcrel":"", type);
1901 continue; /* dunno how to handle without final_sym_name */
1902 }
1903
bef79c34
FB
1904 get_reloc_expr(final_sym_name, sizeof(final_sym_name),
1905 sym_name);
82eec0a1
FB
1906 switch(type) {
1907 case PPC_RELOC_BR24:
91aa5d49
FB
1908 if (!strstart(sym_name,"__op_gen_label",&p)) {
1909 fprintf(outfile, "{\n");
1910 fprintf(outfile, " uint32_t imm = *(uint32_t *)(gen_code_ptr + %d) & 0x3fffffc;\n", slide);
1911 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",
82eec0a1 1912 slide, slide, name, sslide );
91aa5d49
FB
1913 fprintf(outfile, "}\n");
1914 } else {
1915 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | (((long)%s - (long)gen_code_ptr - %d) & 0x03fffffc);\n",
1916 slide, slide, final_sym_name, slide);
1917 }
82eec0a1
FB
1918 break;
1919 case PPC_RELOC_HI16:
1920 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d) >> 16;\n",
1921 slide, final_sym_name, sslide);
1922 break;
1923 case PPC_RELOC_LO16:
1924 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d);\n",
1925 slide, final_sym_name, sslide);
1926 break;
1927 case PPC_RELOC_HA16:
1928 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d + 0x8000) >> 16;\n",
1929 slide, final_sym_name, sslide);
1930 break;
1931 default:
1932 error("unsupported powerpc relocation (%d)", type);
1933 }
1934 }
1935#else
1936#error unsupport object format
1937#endif
04369ff2 1938 }
ce11fedc 1939#elif defined(HOST_S390)
fb3e5849 1940 {
fb3e5849
FB
1941 char name[256];
1942 int type;
ce11fedc 1943 int addend;
fb3e5849 1944 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
ae228531
FB
1945 if (rel->r_offset >= start_offset &&
1946 rel->r_offset < start_offset + copy_size) {
efdea7bf 1947 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
bef79c34 1948 get_reloc_expr(name, sizeof(name), sym_name);
fb3e5849
FB
1949 type = ELF32_R_TYPE(rel->r_info);
1950 addend = rel->r_addend;
1951 switch(type) {
1952 case R_390_32:
ce11fedc 1953 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
ae228531 1954 rel->r_offset - start_offset, name, addend);
fb3e5849
FB
1955 break;
1956 case R_390_16:
ce11fedc 1957 fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
ae228531 1958 rel->r_offset - start_offset, name, addend);
fb3e5849
FB
1959 break;
1960 case R_390_8:
ce11fedc 1961 fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
ae228531 1962 rel->r_offset - start_offset, name, addend);
fb3e5849
FB
1963 break;
1964 default:
1965 error("unsupported s390 relocation (%d)", type);
1966 }
1967 }
1968 }
1969 }
efdea7bf
FB
1970#elif defined(HOST_ALPHA)
1971 {
1972 for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
ae228531 1973 if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
efdea7bf 1974 int type;
74c95119 1975
efdea7bf 1976 type = ELF64_R_TYPE(rel->r_info);
74c95119 1977 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
efdea7bf
FB
1978 switch (type) {
1979 case R_ALPHA_GPDISP:
74c95119
FB
1980 /* The gp is just 32 bit, and never changes, so it's easiest to emit it
1981 as an immediate instead of constructing it from the pv or ra. */
1982 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
ae228531 1983 rel->r_offset - start_offset);
74c95119 1984 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
ae228531 1985 rel->r_offset - start_offset + rel->r_addend);
efdea7bf
FB
1986 break;
1987 case R_ALPHA_LITUSE:
1988 /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
1989 now, since some called functions (libc) need pv to be set up. */
1990 break;
1991 case R_ALPHA_HINT:
1992 /* Branch target prediction hint. Ignore for now. Should be already
1993 correct for in-function jumps. */
1994 break;
1995 case R_ALPHA_LITERAL:
74c95119
FB
1996 /* Load a literal from the GOT relative to the gp. Since there's only a
1997 single gp, nothing is to be done. */
1998 break;
1999 case R_ALPHA_GPRELHIGH:
2000 /* Handle fake relocations against __op_param symbol. Need to emit the
2001 high part of the immediate value instead. Other symbols need no
2002 special treatment. */
2003 if (strstart(sym_name, "__op_param", &p))
2004 fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
ae228531 2005 rel->r_offset - start_offset, p);
74c95119
FB
2006 break;
2007 case R_ALPHA_GPRELLOW:
2008 if (strstart(sym_name, "__op_param", &p))
2009 fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
ae228531 2010 rel->r_offset - start_offset, p);
74c95119
FB
2011 break;
2012 case R_ALPHA_BRSGP:
2013 /* PC-relative jump. Tweak offset to skip the two instructions that try to
2014 set up the gp from the pv. */
2f87c607 2015 fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
ae228531 2016 rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
efdea7bf
FB
2017 break;
2018 default:
2019 error("unsupported Alpha relocation (%d)", type);
2020 }
2021 }
2022 }
2023 }
2024#elif defined(HOST_IA64)
2025 {
b8076a74
FB
2026 unsigned long sym_idx;
2027 long code_offset;
efdea7bf
FB
2028 char name[256];
2029 int type;
b8076a74
FB
2030 long addend;
2031
efdea7bf 2032 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
b8076a74
FB
2033 sym_idx = ELF64_R_SYM(rel->r_info);
2034 if (rel->r_offset < start_offset
2035 || rel->r_offset >= start_offset + copy_size)
2036 continue;
2037 sym_name = (strtab + symtab[sym_idx].st_name);
2038 if (strstart(sym_name, "__op_jmp", &p)) {
2039 int n;
2040 n = strtol(p, NULL, 10);
2041 /* __op_jmp relocations are done at
2042 runtime to do translated block
2043 chaining: the offset of the instruction
2044 needs to be stored */
2045 fprintf(outfile, " jmp_offsets[%d] ="
2046 "%ld + (gen_code_ptr - gen_code_buf);\n",
2047 n, rel->r_offset - start_offset);
2048 continue;
2049 }
2050 get_reloc_expr(name, sizeof(name), sym_name);
2051 type = ELF64_R_TYPE(rel->r_info);
2052 addend = rel->r_addend;
2053 code_offset = rel->r_offset - start_offset;
2054 switch(type) {
2055 case R_IA64_IMM64:
2056 fprintf(outfile,
2057 " ia64_imm64(gen_code_ptr + %ld, "
2058 "%s + %ld);\n",
2059 code_offset, name, addend);
2060 break;
2061 case R_IA64_LTOFF22X:
2062 case R_IA64_LTOFF22:
2063 fprintf(outfile, " IA64_LTOFF(gen_code_ptr + %ld,"
2064 " %s + %ld, %d);\n",
2065 code_offset, name, addend,
2066 (type == R_IA64_LTOFF22X));
2067 break;
2068 case R_IA64_LDXMOV:
2069 fprintf(outfile,
2070 " ia64_ldxmov(gen_code_ptr + %ld,"
2071 " %s + %ld);\n", code_offset, name, addend);
2072 break;
2073
2074 case R_IA64_PCREL21B:
2075 if (strstart(sym_name, "__op_gen_label", NULL)) {
2076 fprintf(outfile,
2077 " ia64_imm21b(gen_code_ptr + %ld,"
2078 " (long) (%s + %ld -\n\t\t"
2079 "((long) gen_code_ptr + %ld)) >> 4);\n",
2080 code_offset, name, addend,
2081 code_offset & ~0xfUL);
2082 } else {
2083 fprintf(outfile,
2084 " IA64_PLT(gen_code_ptr + %ld, "
2085 "%d);\t/* %s + %ld */\n",
2086 code_offset,
2087 get_plt_index(sym_name, addend),
2088 sym_name, addend);
2089 }
2090 break;
2091 default:
2092 error("unsupported ia64 relocation (0x%x)",
2093 type);
2094 }
efdea7bf 2095 }
b8076a74
FB
2096 fprintf(outfile, " ia64_nop_b(gen_code_ptr + %d);\n",
2097 copy_size - 16 + 2);
efdea7bf 2098 }
d014c98c
FB
2099#elif defined(HOST_SPARC)
2100 {
2101 char name[256];
2102 int type;
2103 int addend;
2104 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
ae228531
FB
2105 if (rel->r_offset >= start_offset &&
2106 rel->r_offset < start_offset + copy_size) {
d014c98c 2107 sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
bef79c34 2108 get_reloc_expr(name, sizeof(name), sym_name);
d014c98c
FB
2109 type = ELF32_R_TYPE(rel->r_info);
2110 addend = rel->r_addend;
2111 switch(type) {
2112 case R_SPARC_32:
2113 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
ae228531 2114 rel->r_offset - start_offset, name, addend);
d014c98c
FB
2115 break;
2116 case R_SPARC_HI22:
2117 fprintf(outfile,
2118 " *(uint32_t *)(gen_code_ptr + %d) = "
2119 "((*(uint32_t *)(gen_code_ptr + %d)) "
2120 " & ~0x3fffff) "
ae228531
FB
2121 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2122 rel->r_offset - start_offset,
2123 rel->r_offset - start_offset,
d014c98c
FB
2124 name, addend);
2125 break;
2126 case R_SPARC_LO10:
2127 fprintf(outfile,
2128 " *(uint32_t *)(gen_code_ptr + %d) = "
2129 "((*(uint32_t *)(gen_code_ptr + %d)) "
2130 " & ~0x3ff) "
2131 " | ((%s + %d) & 0x3ff);\n",
ae228531
FB
2132 rel->r_offset - start_offset,
2133 rel->r_offset - start_offset,
d014c98c
FB
2134 name, addend);
2135 break;
2136 case R_SPARC_WDISP30:
2137 fprintf(outfile,
2138 " *(uint32_t *)(gen_code_ptr + %d) = "
2139 "((*(uint32_t *)(gen_code_ptr + %d)) "
2140 " & ~0x3fffffff) "
ae228531 2141 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
d014c98c 2142 " & 0x3fffffff);\n",
ae228531
FB
2143 rel->r_offset - start_offset,
2144 rel->r_offset - start_offset,
2145 name, addend,
2146 rel->r_offset - start_offset);
d014c98c
FB
2147 break;
2148 default:
2149 error("unsupported sparc relocation (%d)", type);
2150 }
2151 }
2152 }
2153 }
2154#elif defined(HOST_SPARC64)
2155 {
2156 char name[256];
2157 int type;
2158 int addend;
2159 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
ae228531
FB
2160 if (rel->r_offset >= start_offset &&
2161 rel->r_offset < start_offset + copy_size) {
d014c98c 2162 sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
bef79c34 2163 get_reloc_expr(name, sizeof(name), sym_name);
d014c98c
FB
2164 type = ELF64_R_TYPE(rel->r_info);
2165 addend = rel->r_addend;
2166 switch(type) {
2167 case R_SPARC_32:
2168 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
ae228531 2169 rel->r_offset - start_offset, name, addend);
d014c98c
FB
2170 break;
2171 case R_SPARC_HI22:
2172 fprintf(outfile,
2173 " *(uint32_t *)(gen_code_ptr + %d) = "
2174 "((*(uint32_t *)(gen_code_ptr + %d)) "
2175 " & ~0x3fffff) "
ae228531
FB
2176 " | (((%s + %d) >> 10) & 0x3fffff);\n",
2177 rel->r_offset - start_offset,
2178 rel->r_offset - start_offset,
d014c98c
FB
2179 name, addend);
2180 break;
2181 case R_SPARC_LO10:
2182 fprintf(outfile,
2183 " *(uint32_t *)(gen_code_ptr + %d) = "
2184 "((*(uint32_t *)(gen_code_ptr + %d)) "
2185 " & ~0x3ff) "
2186 " | ((%s + %d) & 0x3ff);\n",
ae228531
FB
2187 rel->r_offset - start_offset,
2188 rel->r_offset - start_offset,
d014c98c
FB
2189 name, addend);
2190 break;
2191 case R_SPARC_WDISP30:
2192 fprintf(outfile,
2193 " *(uint32_t *)(gen_code_ptr + %d) = "
2194 "((*(uint32_t *)(gen_code_ptr + %d)) "
2195 " & ~0x3fffffff) "
ae228531 2196 " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
d014c98c 2197 " & 0x3fffffff);\n",
ae228531
FB
2198 rel->r_offset - start_offset,
2199 rel->r_offset - start_offset,
2200 name, addend,
2201 rel->r_offset - start_offset);
d014c98c
FB
2202 break;
2203 default:
2204 error("unsupported sparc64 relocation (%d)", type);
2205 }
2206 }
2207 }
2208 }
ff1f20a3
FB
2209#elif defined(HOST_ARM)
2210 {
2211 char name[256];
2212 int type;
2213 int addend;
2214
2215 arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
2216 relocs, nb_relocs);
2217
2218 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2219 if (rel->r_offset >= start_offset &&
2220 rel->r_offset < start_offset + copy_size) {
2221 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
2222 /* the compiler leave some unnecessary references to the code */
2223 if (sym_name[0] == '\0')
2224 continue;
bef79c34 2225 get_reloc_expr(name, sizeof(name), sym_name);
ff1f20a3
FB
2226 type = ELF32_R_TYPE(rel->r_info);
2227 addend = get32((uint32_t *)(text + rel->r_offset));
2228 switch(type) {
2229 case R_ARM_ABS32:
2230 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
2231 rel->r_offset - start_offset, name, addend);
2232 break;
2233 case R_ARM_PC24:
2234 fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
2235 rel->r_offset - start_offset, addend, name);
2236 break;
2237 default:
2238 error("unsupported arm relocation (%d)", type);
2239 }
2240 }
2241 }
38e584a0
FB
2242 }
2243#elif defined(HOST_M68K)
2244 {
2245 char name[256];
2246 int type;
2247 int addend;
2248 Elf32_Sym *sym;
2249 for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2250 if (rel->r_offset >= start_offset &&
2251 rel->r_offset < start_offset + copy_size) {
2252 sym = &(symtab[ELFW(R_SYM)(rel->r_info)]);
2253 sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
bef79c34 2254 get_reloc_expr(name, sizeof(name), sym_name);
38e584a0
FB
2255 type = ELF32_R_TYPE(rel->r_info);
2256 addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend;
2257 switch(type) {
2258 case R_68K_32:
2259 fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ;
2260 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
2261 rel->r_offset - start_offset, name, addend );
2262 break;
2263 case R_68K_PC32:
2264 fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset);
2265 fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
2266 rel->r_offset - start_offset, name, rel->r_offset - start_offset, /*sym->st_value+*/ addend);
2267 break;
2268 default:
2269 error("unsupported m68k relocation (%d)", type);
2270 }
2271 }
2272 }
ff1f20a3 2273 }
ce11fedc
FB
2274#else
2275#error unsupported CPU
2276#endif
dc99065b
FB
2277 fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
2278 fprintf(outfile, "}\n");
2279 fprintf(outfile, "break;\n\n");
2280 } else {
2281 fprintf(outfile, "static inline void gen_%s(", name);
2282 if (nb_args == 0) {
2283 fprintf(outfile, "void");
2284 } else {
2285 for(i = 0; i < nb_args; i++) {
2286 if (i != 0)
2287 fprintf(outfile, ", ");
2288 fprintf(outfile, "long param%d", i + 1);
367e86e8
FB
2289 }
2290 }
dc99065b
FB
2291 fprintf(outfile, ")\n");
2292 fprintf(outfile, "{\n");
2293 for(i = 0; i < nb_args; i++) {
2294 fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1);
2295 }
2296 fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
2297 fprintf(outfile, "}\n\n");
367e86e8 2298 }
367e86e8
FB
2299}
2300
67b915a5 2301int gen_file(FILE *outfile, int out_type)
367e86e8 2302{
67b915a5
FB
2303 int i;
2304 EXE_SYM *sym;
367e86e8 2305
d219f7e7 2306 if (out_type == OUT_INDEX_OP) {
a513fe19 2307 fprintf(outfile, "DEF(end, 0, 0)\n");
e477b8b8
FB
2308 fprintf(outfile, "DEF(nop, 0, 0)\n");
2309 fprintf(outfile, "DEF(nop1, 1, 0)\n");
2310 fprintf(outfile, "DEF(nop2, 2, 0)\n");
2311 fprintf(outfile, "DEF(nop3, 3, 0)\n");
dc99065b 2312 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
82eec0a1 2313 const char *name;
67b915a5 2314 name = get_sym_name(sym);
82eec0a1 2315 if (strstart(name, OP_PREFIX, NULL)) {
67b915a5 2316 gen_code(name, sym->st_value, sym->st_size, outfile, 2);
dc99065b
FB
2317 }
2318 }
d219f7e7
FB
2319 } else if (out_type == OUT_GEN_OP) {
2320 /* generate gen_xxx functions */
c4687878 2321 fprintf(outfile, "#include \"dyngen-op.h\"\n");
d219f7e7
FB
2322 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2323 const char *name;
67b915a5 2324 name = get_sym_name(sym);
d219f7e7 2325 if (strstart(name, OP_PREFIX, NULL)) {
82eec0a1 2326#if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
67b915a5 2327 if (sym->st_shndx != text_shndx)
d219f7e7 2328 error("invalid section for opcode (0x%x)", sym->st_shndx);
82eec0a1 2329#endif
67b915a5 2330 gen_code(name, sym->st_value, sym->st_size, outfile, 0);
d219f7e7
FB
2331 }
2332 }
2333
dc99065b
FB
2334 } else {
2335 /* generate big code generation switch */
2336fprintf(outfile,
2337"int dyngen_code(uint8_t *gen_code_buf,\n"
d4e8164f 2338" uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
c4687878 2339" const uint16_t *opc_buf, const uint32_t *opparam_buf, const long *gen_labels)\n"
dc99065b
FB
2340"{\n"
2341" uint8_t *gen_code_ptr;\n"
2342" const uint16_t *opc_ptr;\n"
ff1f20a3
FB
2343" const uint32_t *opparam_ptr;\n");
2344
2345#ifdef HOST_ARM
2346fprintf(outfile,
2347" uint8_t *last_gen_code_ptr = gen_code_buf;\n"
2348" LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
2349" uint32_t *arm_data_ptr = arm_data_table;\n");
2350#endif
b8076a74
FB
2351#ifdef HOST_IA64
2352 {
2353 long addend, not_first = 0;
2354 unsigned long sym_idx;
2355 int index, max_index;
2356 const char *sym_name;
2357 EXE_RELOC *rel;
2358
2359 max_index = -1;
2360 for (i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2361 sym_idx = ELF64_R_SYM(rel->r_info);
2362 sym_name = (strtab + symtab[sym_idx].st_name);
2363 if (strstart(sym_name, "__op_gen_label", NULL))
2364 continue;
2365 if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B)
2366 continue;
2367
2368 addend = rel->r_addend;
2369 index = get_plt_index(sym_name, addend);
2370 if (index <= max_index)
2371 continue;
2372 max_index = index;
2373 fprintf(outfile, " extern void %s(void);\n", sym_name);
2374 }
2375
2376 fprintf(outfile,
2377 " struct ia64_fixup *plt_fixes = NULL, "
2378 "*ltoff_fixes = NULL;\n"
2379 " static long plt_target[] = {\n\t");
2380
2381 max_index = -1;
2382 for (i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
2383 sym_idx = ELF64_R_SYM(rel->r_info);
2384 sym_name = (strtab + symtab[sym_idx].st_name);
2385 if (strstart(sym_name, "__op_gen_label", NULL))
2386 continue;
2387 if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B)
2388 continue;
2389
2390 addend = rel->r_addend;
2391 index = get_plt_index(sym_name, addend);
2392 if (index <= max_index)
2393 continue;
2394 max_index = index;
2395
2396 if (not_first)
2397 fprintf(outfile, ",\n\t");
2398 not_first = 1;
2399 if (addend)
2400 fprintf(outfile, "(long) &%s + %ld", sym_name, addend);
2401 else
2402 fprintf(outfile, "(long) &%s", sym_name);
2403 }
2404 fprintf(outfile, "\n };\n"
2405 " unsigned int plt_offset[%u] = { 0 };\n", max_index + 1);
2406 }
2407#endif
ff1f20a3
FB
2408
2409fprintf(outfile,
2410"\n"
dc99065b
FB
2411" gen_code_ptr = gen_code_buf;\n"
2412" opc_ptr = opc_buf;\n"
ae228531
FB
2413" opparam_ptr = opparam_buf;\n");
2414
2415 /* Generate prologue, if needed. */
ae228531
FB
2416
2417fprintf(outfile,
dc99065b
FB
2418" for(;;) {\n"
2419" switch(*opc_ptr++) {\n"
2420);
367e86e8 2421
dc99065b
FB
2422 for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
2423 const char *name;
67b915a5 2424 name = get_sym_name(sym);
dc99065b 2425 if (strstart(name, OP_PREFIX, NULL)) {
367e86e8 2426#if 0
dc99065b
FB
2427 printf("%4d: %s pos=0x%08x len=%d\n",
2428 i, name, sym->st_value, sym->st_size);
367e86e8 2429#endif
82eec0a1 2430#if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
67b915a5 2431 if (sym->st_shndx != text_shndx)
dc99065b 2432 error("invalid section for opcode (0x%x)", sym->st_shndx);
82eec0a1 2433#endif
67b915a5 2434 gen_code(name, sym->st_value, sym->st_size, outfile, 1);
dc99065b
FB
2435 }
2436 }
2437
2438fprintf(outfile,
8ef9a8ec
FB
2439" case INDEX_op_nop:\n"
2440" break;\n"
2441" case INDEX_op_nop1:\n"
2442" opparam_ptr++;\n"
2443" break;\n"
2444" case INDEX_op_nop2:\n"
2445" opparam_ptr += 2;\n"
2446" break;\n"
2447" case INDEX_op_nop3:\n"
2448" opparam_ptr += 3;\n"
2449" break;\n"
dc99065b
FB
2450" default:\n"
2451" goto the_end;\n"
ff1f20a3
FB
2452" }\n");
2453
2454#ifdef HOST_ARM
2455/* generate constant table if needed */
2456fprintf(outfile,
2457" if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
2458" gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
2459" last_gen_code_ptr = gen_code_ptr;\n"
2460" arm_ldr_ptr = arm_ldr_table;\n"
2461" arm_data_ptr = arm_data_table;\n"
2462" }\n");
2463#endif
2464
2465
2466fprintf(outfile,
dc99065b
FB
2467" }\n"
2468" the_end:\n"
2469);
b8076a74
FB
2470#ifdef HOST_IA64
2471 fprintf(outfile,
2472 " ia64_apply_fixes(&gen_code_ptr, ltoff_fixes, "
2473 "(uint64_t) code_gen_buffer + 2*(1<<20), plt_fixes,\n\t\t\t"
2474 "sizeof(plt_target)/sizeof(plt_target[0]),\n\t\t\t"
2475 "plt_target, plt_offset);\n");
2476#endif
dc99065b 2477
9621339d
FB
2478/* generate some code patching */
2479#ifdef HOST_ARM
2480fprintf(outfile, "gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 0);\n");
2481#endif
d219f7e7
FB
2482 /* flush instruction cache */
2483 fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
2484
dc99065b
FB
2485 fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
2486 fprintf(outfile, "}\n\n");
2487
367e86e8
FB
2488 }
2489
367e86e8
FB
2490 return 0;
2491}
2492
2493void usage(void)
2494{
2495 printf("dyngen (c) 2003 Fabrice Bellard\n"
dc99065b
FB
2496 "usage: dyngen [-o outfile] [-c] objfile\n"
2497 "Generate a dynamic code generator from an object file\n"
2498 "-c output enum of operations\n"
d219f7e7 2499 "-g output gen_op_xx() functions\n"
dc99065b 2500 );
367e86e8
FB
2501 exit(1);
2502}
2503
2504int main(int argc, char **argv)
2505{
d219f7e7 2506 int c, out_type;
367e86e8
FB
2507 const char *filename, *outfilename;
2508 FILE *outfile;
2509
2510 outfilename = "out.c";
d219f7e7 2511 out_type = OUT_CODE;
367e86e8 2512 for(;;) {
d219f7e7 2513 c = getopt(argc, argv, "ho:cg");
367e86e8
FB
2514 if (c == -1)
2515 break;
2516 switch(c) {
2517 case 'h':
2518 usage();
2519 break;
2520 case 'o':
2521 outfilename = optarg;
2522 break;
dc99065b 2523 case 'c':
d219f7e7
FB
2524 out_type = OUT_INDEX_OP;
2525 break;
2526 case 'g':
2527 out_type = OUT_GEN_OP;
dc99065b 2528 break;
367e86e8
FB
2529 }
2530 }
2531 if (optind >= argc)
2532 usage();
2533 filename = argv[optind];
2534 outfile = fopen(outfilename, "w");
2535 if (!outfile)
2536 error("could not open '%s'", outfilename);
67b915a5
FB
2537
2538 load_object(filename);
2539 gen_file(outfile, out_type);
367e86e8
FB
2540 fclose(outfile);
2541 return 0;
2542}