Statistics
| Branch: | Revision:

root / dyngen.c @ 630be16f

History | View | Annotate | Download (48.6 kB)

1
/*
2
 *  Generic Dynamic compiler generator
3
 * 
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 */
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <string.h>
23
#include <stdarg.h>
24
#include <inttypes.h>
25
#include <unistd.h>
26
#include <fcntl.h>
27

    
28
#include "config-host.h"
29

    
30
/* elf format definitions. We use these macros to test the CPU to
31
   allow cross compilation (this tool must be ran on the build
32
   platform) */
33
#if defined(HOST_I386)
34

    
35
#define ELF_CLASS        ELFCLASS32
36
#define ELF_ARCH        EM_386
37
#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
38
#undef ELF_USES_RELOCA
39

    
40
#elif defined(HOST_PPC)
41

    
42
#define ELF_CLASS        ELFCLASS32
43
#define ELF_ARCH        EM_PPC
44
#define elf_check_arch(x) ((x) == EM_PPC)
45
#define ELF_USES_RELOCA
46

    
47
#elif defined(HOST_S390)
48

    
49
#define ELF_CLASS        ELFCLASS32
50
#define ELF_ARCH        EM_S390
51
#define elf_check_arch(x) ((x) == EM_S390)
52
#define ELF_USES_RELOCA
53

    
54
#elif defined(HOST_ALPHA)
55

    
56
#define ELF_CLASS        ELFCLASS64
57
#define ELF_ARCH        EM_ALPHA
58
#define elf_check_arch(x) ((x) == EM_ALPHA)
59
#define ELF_USES_RELOCA
60

    
61
#elif defined(HOST_IA64)
62

    
63
#define ELF_CLASS        ELFCLASS64
64
#define ELF_ARCH        EM_IA_64
65
#define elf_check_arch(x) ((x) == EM_IA_64)
66
#define ELF_USES_RELOCA
67

    
68
#elif defined(HOST_SPARC)
69

    
70
#define ELF_CLASS        ELFCLASS32
71
#define ELF_ARCH        EM_SPARC
72
#define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
73
#define ELF_USES_RELOCA
74

    
75
#elif defined(HOST_SPARC64)
76

    
77
#define ELF_CLASS        ELFCLASS64
78
#define ELF_ARCH        EM_SPARCV9
79
#define elf_check_arch(x) ((x) == EM_SPARCV9)
80
#define ELF_USES_RELOCA
81

    
82
#elif defined(HOST_ARM)
83

    
84
#define ELF_CLASS        ELFCLASS32
85
#define ELF_ARCH        EM_ARM
86
#define elf_check_arch(x) ((x) == EM_ARM)
87
#define ELF_USES_RELOC
88

    
89
#elif defined(HOST_M68K)
90

    
91
#define ELF_CLASS        ELFCLASS32
92
#define ELF_ARCH        EM_68K
93
#define elf_check_arch(x) ((x) == EM_68K)
94
#define ELF_USES_RELOCA
95

    
96
#else
97
#error unsupported CPU - please update the code
98
#endif
99

    
100
#include "elf.h"
101

    
102
#if ELF_CLASS == ELFCLASS32
103
typedef int32_t host_long;
104
typedef uint32_t host_ulong;
105
#define swabls(x) swab32s(x)
106
#else
107
typedef int64_t host_long;
108
typedef uint64_t host_ulong;
109
#define swabls(x) swab64s(x)
110
#endif
111

    
112
#ifdef ELF_USES_RELOCA
113
#define SHT_RELOC SHT_RELA
114
#else
115
#define SHT_RELOC SHT_REL
116
#endif
117

    
118
#include "bswap.h"
119

    
120
enum {
121
    OUT_GEN_OP,
122
    OUT_CODE,
123
    OUT_INDEX_OP,
124
};
125

    
126
/* all dynamically generated functions begin with this code */
127
#define OP_PREFIX "op_"
128

    
129
int elf_must_swap(struct elfhdr *h)
130
{
131
  union {
132
      uint32_t i;
133
      uint8_t b[4];
134
  } swaptest;
135

    
136
  swaptest.i = 1;
137
  return (h->e_ident[EI_DATA] == ELFDATA2MSB) != 
138
      (swaptest.b[0] == 0);
139
}
140
  
141
void swab16s(uint16_t *p)
142
{
143
    *p = bswap16(*p);
144
}
145

    
146
void swab32s(uint32_t *p)
147
{
148
    *p = bswap32(*p);
149
}
150

    
151
void swab64s(uint64_t *p)
152
{
153
    *p = bswap64(*p);
154
}
155

    
156
void elf_swap_ehdr(struct elfhdr *h)
157
{
158
    swab16s(&h->e_type);                        /* Object file type */
159
    swab16s(&h->        e_machine);                /* Architecture */
160
    swab32s(&h->        e_version);                /* Object file version */
161
    swabls(&h->        e_entry);                /* Entry point virtual address */
162
    swabls(&h->        e_phoff);                /* Program header table file offset */
163
    swabls(&h->        e_shoff);                /* Section header table file offset */
164
    swab32s(&h->        e_flags);                /* Processor-specific flags */
165
    swab16s(&h->        e_ehsize);                /* ELF header size in bytes */
166
    swab16s(&h->        e_phentsize);                /* Program header table entry size */
167
    swab16s(&h->        e_phnum);                /* Program header table entry count */
168
    swab16s(&h->        e_shentsize);                /* Section header table entry size */
169
    swab16s(&h->        e_shnum);                /* Section header table entry count */
170
    swab16s(&h->        e_shstrndx);                /* Section header string table index */
171
}
172

    
173
void elf_swap_shdr(struct elf_shdr *h)
174
{
175
  swab32s(&h->        sh_name);                /* Section name (string tbl index) */
176
  swab32s(&h->        sh_type);                /* Section type */
177
  swabls(&h->        sh_flags);                /* Section flags */
178
  swabls(&h->        sh_addr);                /* Section virtual addr at execution */
179
  swabls(&h->        sh_offset);                /* Section file offset */
180
  swabls(&h->        sh_size);                /* Section size in bytes */
181
  swab32s(&h->        sh_link);                /* Link to another section */
182
  swab32s(&h->        sh_info);                /* Additional section information */
183
  swabls(&h->        sh_addralign);                /* Section alignment */
184
  swabls(&h->        sh_entsize);                /* Entry size if section holds table */
185
}
186

    
187
void elf_swap_phdr(struct elf_phdr *h)
188
{
189
    swab32s(&h->p_type);                        /* Segment type */
190
    swabls(&h->p_offset);                /* Segment file offset */
191
    swabls(&h->p_vaddr);                /* Segment virtual address */
192
    swabls(&h->p_paddr);                /* Segment physical address */
193
    swabls(&h->p_filesz);                /* Segment size in file */
194
    swabls(&h->p_memsz);                /* Segment size in memory */
195
    swab32s(&h->p_flags);                /* Segment flags */
196
    swabls(&h->p_align);                /* Segment alignment */
197
}
198

    
199
void elf_swap_rel(ELF_RELOC *rel)
200
{
201
    swabls(&rel->r_offset);
202
    swabls(&rel->r_info);
203
#ifdef ELF_USES_RELOCA
204
    swabls(&rel->r_addend);
205
#endif
206
}
207

    
208
/* ELF file info */
209
int do_swap;
210
struct elf_shdr *shdr;
211
uint8_t **sdata;
212
struct elfhdr ehdr;
213
ElfW(Sym) *symtab;
214
int nb_syms;
215
char *strtab;
216
int text_shndx;
217

    
218
uint16_t get16(uint16_t *p)
219
{
220
    uint16_t val;
221
    val = *p;
222
    if (do_swap)
223
        val = bswap16(val);
224
    return val;
225
}
226

    
227
uint32_t get32(uint32_t *p)
228
{
229
    uint32_t val;
230
    val = *p;
231
    if (do_swap)
232
        val = bswap32(val);
233
    return val;
234
}
235

    
236
void put16(uint16_t *p, uint16_t val)
237
{
238
    if (do_swap)
239
        val = bswap16(val);
240
    *p = val;
241
}
242

    
243
void put32(uint32_t *p, uint32_t val)
244
{
245
    if (do_swap)
246
        val = bswap32(val);
247
    *p = val;
248
}
249

    
250
void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
251
{
252
    va_list ap;
253
    va_start(ap, fmt);
254
    fprintf(stderr, "dyngen: ");
255
    vfprintf(stderr, fmt, ap);
256
    fprintf(stderr, "\n");
257
    va_end(ap);
258
    exit(1);
259
}
260

    
261

    
262
struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr, 
263
                                  const char *name)
264
{
265
    int i;
266
    const char *shname;
267
    struct elf_shdr *sec;
268

    
269
    for(i = 0; i < shnum; i++) {
270
        sec = &shdr[i];
271
        if (!sec->sh_name)
272
            continue;
273
        shname = shstr + sec->sh_name;
274
        if (!strcmp(shname, name))
275
            return sec;
276
    }
277
    return NULL;
278
}
279

    
280
int find_reloc(int sh_index)
281
{
282
    struct elf_shdr *sec;
283
    int i;
284

    
285
    for(i = 0; i < ehdr.e_shnum; i++) {
286
        sec = &shdr[i];
287
        if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index) 
288
            return i;
289
    }
290
    return 0;
291
}
292

    
293
void *load_data(int fd, long offset, unsigned int size)
294
{
295
    char *data;
296

    
297
    data = malloc(size);
298
    if (!data)
299
        return NULL;
300
    lseek(fd, offset, SEEK_SET);
301
    if (read(fd, data, size) != size) {
302
        free(data);
303
        return NULL;
304
    }
305
    return data;
306
}
307

    
308
int strstart(const char *str, const char *val, const char **ptr)
309
{
310
    const char *p, *q;
311
    p = str;
312
    q = val;
313
    while (*q != '\0') {
314
        if (*p != *q)
315
            return 0;
316
        p++;
317
        q++;
318
    }
319
    if (ptr)
320
        *ptr = p;
321
    return 1;
322
}
323

    
324
#ifdef HOST_ARM
325

    
326
int arm_emit_ldr_info(const char *name, unsigned long start_offset,
327
                      FILE *outfile, uint8_t *p_start, uint8_t *p_end,
328
                      ELF_RELOC *relocs, int nb_relocs)
329
{
330
    uint8_t *p;
331
    uint32_t insn;
332
    int offset, min_offset, pc_offset, data_size;
333
    uint8_t data_allocated[1024];
334
    unsigned int data_index;
335
    
336
    memset(data_allocated, 0, sizeof(data_allocated));
337
    
338
    p = p_start;
339
    min_offset = p_end - p_start;
340
    while (p < p_start + min_offset) {
341
        insn = get32((uint32_t *)p);
342
        if ((insn & 0x0d5f0000) == 0x051f0000) {
343
            /* ldr reg, [pc, #im] */
344
            offset = insn & 0xfff;
345
            if (!(insn & 0x00800000))
346
                        offset = -offset;
347
            if ((offset & 3) !=0)
348
                error("%s:%04x: ldr pc offset must be 32 bit aligned", 
349
                      name, start_offset + p - p_start);
350
            pc_offset = p - p_start + offset + 8;
351
            if (pc_offset <= (p - p_start) || 
352
                pc_offset >= (p_end - p_start))
353
                error("%s:%04x: ldr pc offset must point inside the function code", 
354
                      name, start_offset + p - p_start);
355
            if (pc_offset < min_offset)
356
                min_offset = pc_offset;
357
            if (outfile) {
358
                /* ldr position */
359
                fprintf(outfile, "    arm_ldr_ptr->ptr = gen_code_ptr + %d;\n", 
360
                        p - p_start);
361
                /* ldr data index */
362
                data_index = ((p_end - p_start) - pc_offset - 4) >> 2;
363
                fprintf(outfile, "    arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n", 
364
                        data_index);
365
                fprintf(outfile, "    arm_ldr_ptr++;\n");
366
                if (data_index >= sizeof(data_allocated))
367
                    error("%s: too many data", name);
368
                if (!data_allocated[data_index]) {
369
                    ELF_RELOC *rel;
370
                    int i, addend, type;
371
                    const char *sym_name, *p;
372
                    char relname[1024];
373

    
374
                    data_allocated[data_index] = 1;
375

    
376
                    /* data value */
377
                    addend = get32((uint32_t *)(p_start + pc_offset));
378
                    relname[0] = '\0';
379
                    for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
380
                        if (rel->r_offset == (pc_offset + start_offset)) {
381
                            sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
382
                            /* the compiler leave some unnecessary references to the code */
383
                            if (strstart(sym_name, "__op_param", &p)) {
384
                                snprintf(relname, sizeof(relname), "param%s", p);
385
                            } else {
386
                                snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name);
387
                            }
388
                            type = ELF32_R_TYPE(rel->r_info);
389
                            if (type != R_ARM_ABS32)
390
                                error("%s: unsupported data relocation", name);
391
                            break;
392
                        }
393
                    }
394
                    fprintf(outfile, "    arm_data_ptr[%d] = 0x%x",
395
                            data_index, addend);
396
                    if (relname[0] != '\0')
397
                        fprintf(outfile, " + %s", relname);
398
                    fprintf(outfile, ";\n");
399
                }
400
            }
401
        }
402
        p += 4;
403
    }
404
    data_size = (p_end - p_start) - min_offset;
405
    if (data_size > 0 && outfile) {
406
        fprintf(outfile, "    arm_data_ptr += %d;\n", data_size >> 2);
407
    }
408

    
409
    /* the last instruction must be a mov pc, lr */
410
    if (p == p_start)
411
        goto arm_ret_error;
412
    p -= 4;
413
    insn = get32((uint32_t *)p);
414
    if ((insn & 0xffff0000) != 0xe91b0000) {
415
    arm_ret_error:
416
        if (!outfile)
417
            printf("%s: invalid epilog\n", name);
418
    }
419
    return p - p_start;            
420
}
421
#endif
422

    
423

    
424
#define MAX_ARGS 3
425

    
426
/* generate op code */
427
void gen_code(const char *name, host_ulong offset, host_ulong size, 
428
              FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs,
429
              int gen_switch)
430
{
431
    int copy_size = 0;
432
    uint8_t *p_start, *p_end;
433
    host_ulong start_offset;
434
    int nb_args, i, n;
435
    uint8_t args_present[MAX_ARGS];
436
    const char *sym_name, *p;
437
    ELF_RELOC *rel;
438

    
439
    /* Compute exact size excluding prologue and epilogue instructions.
440
     * Increment start_offset to skip epilogue instructions, then compute
441
     * copy_size the indicate the size of the remaining instructions (in
442
     * bytes).
443
     */
444
    p_start = text + offset;
445
    p_end = p_start + size;
446
    start_offset = offset;
447
    switch(ELF_ARCH) {
448
    case EM_386:
449
        {
450
            int len;
451
            len = p_end - p_start;
452
            if (len == 0)
453
                error("empty code for %s", name);
454
            if (p_end[-1] == 0xc3) {
455
                len--;
456
            } else {
457
                error("ret or jmp expected at the end of %s", name);
458
            }
459
            copy_size = len;
460
        }
461
        break;
462
    case EM_PPC:
463
        {
464
            uint8_t *p;
465
            p = (void *)(p_end - 4);
466
            if (p == p_start)
467
                error("empty code for %s", name);
468
            if (get32((uint32_t *)p) != 0x4e800020)
469
                error("blr expected at the end of %s", name);
470
            copy_size = p - p_start;
471
        }
472
        break;
473
    case EM_S390:
474
        {
475
            uint8_t *p;
476
            p = (void *)(p_end - 2);
477
            if (p == p_start)
478
                error("empty code for %s", name);
479
            if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4)
480
                error("br %%r14 expected at the end of %s", name);
481
            copy_size = p - p_start;
482
        }
483
        break;
484
    case EM_ALPHA:
485
        {
486
            uint8_t *p;
487
            p = p_end - 4;
488
#if 0
489
            /* XXX: check why it occurs */
490
            if (p == p_start)
491
                error("empty code for %s", name);
492
#endif
493
            if (get32((uint32_t *)p) != 0x6bfa8001)
494
                error("ret expected at the end of %s", name);
495
            copy_size = p - p_start;            
496
        }
497
        break;
498
    case EM_IA_64:
499
        {
500
            uint8_t *p;
501
            p = (void *)(p_end - 4);
502
            if (p == p_start)
503
                error("empty code for %s", name);
504
            /* br.ret.sptk.many b0;; */
505
            /* 08 00 84 00 */
506
            if (get32((uint32_t *)p) != 0x00840008)
507
                error("br.ret.sptk.many b0;; expected at the end of %s", name);
508
            copy_size = p - p_start;
509
        }
510
        break;
511
    case EM_SPARC:
512
    case EM_SPARC32PLUS:
513
        {
514
            uint32_t start_insn, end_insn1, end_insn2;
515
            uint8_t *p;
516
            p = (void *)(p_end - 8);
517
            if (p <= p_start)
518
                error("empty code for %s", name);
519
            start_insn = get32((uint32_t *)(p_start + 0x0));
520
            end_insn1 = get32((uint32_t *)(p + 0x0));
521
            end_insn2 = get32((uint32_t *)(p + 0x4));
522
            if ((start_insn & ~0x1fff) == 0x9de3a000) {
523
                p_start += 0x4;
524
                start_offset += 0x4;
525
                if ((int)(start_insn | ~0x1fff) < -128)
526
                    error("Found bogus save at the start of %s", name);
527
                if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
528
                    error("ret; restore; not found at end of %s", name);
529
            } else {
530
                error("No save at the beginning of %s", name);
531
            }
532
#if 0
533
            /* Skip a preceeding nop, if present.  */
534
            if (p > p_start) {
535
                skip_insn = get32((uint32_t *)(p - 0x4));
536
                if (skip_insn == 0x01000000)
537
                    p -= 4;
538
            }
539
#endif
540
            copy_size = p - p_start;
541
        }
542
        break;
543
    case EM_SPARCV9:
544
        {
545
            uint32_t start_insn, end_insn1, end_insn2, skip_insn;
546
            uint8_t *p;
547
            p = (void *)(p_end - 8);
548
            if (p <= p_start)
549
                error("empty code for %s", name);
550
            start_insn = get32((uint32_t *)(p_start + 0x0));
551
            end_insn1 = get32((uint32_t *)(p + 0x0));
552
            end_insn2 = get32((uint32_t *)(p + 0x4));
553
            if ((start_insn & ~0x1fff) == 0x9de3a000) {
554
                p_start += 0x4;
555
                start_offset += 0x4;
556
                if ((int)(start_insn | ~0x1fff) < -256)
557
                    error("Found bogus save at the start of %s", name);
558
                if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000)
559
                    error("ret; restore; not found at end of %s", name);
560
            } else {
561
                error("No save at the beginning of %s", name);
562
            }
563

    
564
            /* Skip a preceeding nop, if present.  */
565
            if (p > p_start) {
566
                skip_insn = get32((uint32_t *)(p - 0x4));
567
                if (skip_insn == 0x01000000)
568
                    p -= 4;
569
            }
570

    
571
            copy_size = p - p_start;
572
        }
573
        break;
574
#ifdef HOST_ARM
575
    case EM_ARM:
576
        if ((p_end - p_start) <= 16)
577
            error("%s: function too small", name);
578
        if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
579
            (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
580
            get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
581
            error("%s: invalid prolog", name);
582
        p_start += 12;
583
        start_offset += 12;
584
        copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end, 
585
                                      relocs, nb_relocs);
586
        break;
587
#endif
588
    case EM_68K:
589
        {
590
            uint8_t *p;
591
            p = (void *)(p_end - 2);
592
            if (p == p_start)
593
                error("empty code for %s", name);
594
            // remove NOP's, probably added for alignment
595
            while ((get16((uint16_t *)p) == 0x4e71) &&
596
                   (p>p_start)) 
597
              p -= 2;
598
            if (get16((uint16_t *)p) != 0x4e75)
599
                error("rts expected at the end of %s", name);
600
            copy_size = p - p_start;
601
        }
602
        break;
603
    default:
604
        error("unknown ELF architecture");
605
    }
606

    
607
    /* compute the number of arguments by looking at the relocations */
608
    for(i = 0;i < MAX_ARGS; i++)
609
        args_present[i] = 0;
610

    
611
    for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
612
        if (rel->r_offset >= start_offset &&
613
            rel->r_offset < start_offset + (p_end - p_start)) {
614
            sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
615
            if (strstart(sym_name, "__op_param", &p)) {
616
                n = strtoul(p, NULL, 10);
617
                if (n > MAX_ARGS)
618
                    error("too many arguments in %s", name);
619
                args_present[n - 1] = 1;
620
            }
621
        }
622
    }
623
    
624
    nb_args = 0;
625
    while (nb_args < MAX_ARGS && args_present[nb_args])
626
        nb_args++;
627
    for(i = nb_args; i < MAX_ARGS; i++) {
628
        if (args_present[i])
629
            error("inconsistent argument numbering in %s", name);
630
    }
631

    
632
    if (gen_switch == 2) {
633
        fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
634
    } else if (gen_switch == 1) {
635

    
636
        /* output C code */
637
        fprintf(outfile, "case INDEX_%s: {\n", name);
638
        if (nb_args > 0) {
639
            fprintf(outfile, "    long ");
640
            for(i = 0; i < nb_args; i++) {
641
                if (i != 0)
642
                    fprintf(outfile, ", ");
643
                fprintf(outfile, "param%d", i + 1);
644
            }
645
            fprintf(outfile, ";\n");
646
        }
647
        fprintf(outfile, "    extern void %s();\n", name);
648

    
649
        for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
650
            if (rel->r_offset >= start_offset &&
651
                rel->r_offset < start_offset + (p_end - p_start)) {
652
                sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
653
                if (*sym_name && 
654
                    !strstart(sym_name, "__op_param", NULL) &&
655
                    !strstart(sym_name, "__op_jmp", NULL)) {
656
#if defined(HOST_SPARC)
657
                    if (sym_name[0] == '.') {
658
                        fprintf(outfile,
659
                                "extern char __dot_%s __asm__(\"%s\");\n",
660
                                sym_name+1, sym_name);
661
                        continue;
662
                    }
663
#endif
664
                    fprintf(outfile, "extern char %s;\n", sym_name);
665
                }
666
            }
667
        }
668

    
669
        fprintf(outfile, "    memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
670

    
671
        /* emit code offset information */
672
        {
673
            ElfW(Sym) *sym;
674
            const char *sym_name, *p;
675
            unsigned long val;
676
            int n;
677

    
678
            for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
679
                sym_name = strtab + sym->st_name;
680
                if (strstart(sym_name, "__op_label", &p)) {
681
                    uint8_t *ptr;
682
                    unsigned long offset;
683
                    
684
                    /* test if the variable refers to a label inside
685
                       the code we are generating */
686
                    ptr = sdata[sym->st_shndx];
687
                    if (!ptr)
688
                        error("__op_labelN in invalid section");
689
                    offset = sym->st_value;
690
                    val = *(unsigned long *)(ptr + offset);
691
#ifdef ELF_USES_RELOCA
692
                    {
693
                        int reloc_shndx, nb_relocs1, j;
694

    
695
                        /* try to find a matching relocation */
696
                        reloc_shndx = find_reloc(sym->st_shndx);
697
                        if (reloc_shndx) {
698
                            nb_relocs1 = shdr[reloc_shndx].sh_size / 
699
                                shdr[reloc_shndx].sh_entsize;
700
                            rel = (ELF_RELOC *)sdata[reloc_shndx];
701
                            for(j = 0; j < nb_relocs1; j++) {
702
                                if (rel->r_offset == offset) {
703
                                    val = rel->r_addend;
704
                                    break;
705
                                }
706
                                rel++;
707
                            }
708
                        }
709
                    }
710
#endif                    
711

    
712
                    if (val >= start_offset && val < start_offset + copy_size) {
713
                        n = strtol(p, NULL, 10);
714
                        fprintf(outfile, "    label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
715
                    }
716
                }
717
            }
718
        }
719

    
720
        /* load parameres in variables */
721
        for(i = 0; i < nb_args; i++) {
722
            fprintf(outfile, "    param%d = *opparam_ptr++;\n", i + 1);
723
        }
724

    
725
        /* patch relocations */
726
#if defined(HOST_I386)
727
            {
728
                char name[256];
729
                int type;
730
                int addend;
731
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
732
                if (rel->r_offset >= start_offset &&
733
                    rel->r_offset < start_offset + copy_size) {
734
                    sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
735
                    if (strstart(sym_name, "__op_jmp", &p)) {
736
                        int n;
737
                        n = strtol(p, NULL, 10);
738
                        /* __op_jmp relocations are done at
739
                           runtime to do translated block
740
                           chaining: the offset of the instruction
741
                           needs to be stored */
742
                        fprintf(outfile, "    jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
743
                                n, rel->r_offset - start_offset);
744
                        continue;
745
                    }
746
                        
747
                    if (strstart(sym_name, "__op_param", &p)) {
748
                        snprintf(name, sizeof(name), "param%s", p);
749
                    } else {
750
                        snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
751
                    }
752
                    type = ELF32_R_TYPE(rel->r_info);
753
                    addend = get32((uint32_t *)(text + rel->r_offset));
754
                    switch(type) {
755
                    case R_386_32:
756
                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
757
                                rel->r_offset - start_offset, name, addend);
758
                        break;
759
                    case R_386_PC32:
760
                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n", 
761
                                rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
762
                        break;
763
                    default:
764
                        error("unsupported i386 relocation (%d)", type);
765
                    }
766
                }
767
                }
768
            }
769
#elif defined(HOST_PPC)
770
            {
771
                char name[256];
772
                int type;
773
                int addend;
774
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
775
                    if (rel->r_offset >= start_offset &&
776
                        rel->r_offset < start_offset + copy_size) {
777
                        sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
778
                        if (strstart(sym_name, "__op_jmp", &p)) {
779
                            int n;
780
                            n = strtol(p, NULL, 10);
781
                            /* __op_jmp relocations are done at
782
                               runtime to do translated block
783
                               chaining: the offset of the instruction
784
                               needs to be stored */
785
                            fprintf(outfile, "    jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
786
                                    n, rel->r_offset - start_offset);
787
                            continue;
788
                        }
789
                        
790
                        if (strstart(sym_name, "__op_param", &p)) {
791
                            snprintf(name, sizeof(name), "param%s", p);
792
                        } else {
793
                            snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
794
                        }
795
                        type = ELF32_R_TYPE(rel->r_info);
796
                        addend = rel->r_addend;
797
                        switch(type) {
798
                        case R_PPC_ADDR32:
799
                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
800
                                    rel->r_offset - start_offset, name, addend);
801
                            break;
802
                        case R_PPC_ADDR16_LO:
803
                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n", 
804
                                    rel->r_offset - start_offset, name, addend);
805
                            break;
806
                        case R_PPC_ADDR16_HI:
807
                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n", 
808
                                    rel->r_offset - start_offset, name, addend);
809
                            break;
810
                        case R_PPC_ADDR16_HA:
811
                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n", 
812
                                    rel->r_offset - start_offset, name, addend);
813
                            break;
814
                        case R_PPC_REL24:
815
                            /* warning: must be at 32 MB distancy */
816
                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n", 
817
                                    rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
818
                            break;
819
                        default:
820
                            error("unsupported powerpc relocation (%d)", type);
821
                        }
822
                    }
823
                }
824
            }
825
#elif defined(HOST_S390)
826
            {
827
                char name[256];
828
                int type;
829
                int addend;
830
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
831
                    if (rel->r_offset >= start_offset &&
832
                        rel->r_offset < start_offset + copy_size) {
833
                        sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
834
                        if (strstart(sym_name, "__op_param", &p)) {
835
                            snprintf(name, sizeof(name), "param%s", p);
836
                        } else {
837
                            snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
838
                        }
839
                        type = ELF32_R_TYPE(rel->r_info);
840
                        addend = rel->r_addend;
841
                        switch(type) {
842
                        case R_390_32:
843
                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
844
                                    rel->r_offset - start_offset, name, addend);
845
                            break;
846
                        case R_390_16:
847
                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n", 
848
                                    rel->r_offset - start_offset, name, addend);
849
                            break;
850
                        case R_390_8:
851
                            fprintf(outfile, "    *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n", 
852
                                    rel->r_offset - start_offset, name, addend);
853
                            break;
854
                        default:
855
                            error("unsupported s390 relocation (%d)", type);
856
                        }
857
                    }
858
                }
859
            }
860
#elif defined(HOST_ALPHA)
861
            {
862
                for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
863
                    if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
864
                        int type;
865

    
866
                        type = ELF64_R_TYPE(rel->r_info);
867
                        sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
868
                        switch (type) {
869
                        case R_ALPHA_GPDISP:
870
                            /* The gp is just 32 bit, and never changes, so it's easiest to emit it
871
                               as an immediate instead of constructing it from the pv or ra.  */
872
                            fprintf(outfile, "    immediate_ldah(gen_code_ptr + %ld, gp);\n",
873
                                    rel->r_offset - start_offset);
874
                            fprintf(outfile, "    immediate_lda(gen_code_ptr + %ld, gp);\n",
875
                                    rel->r_offset - start_offset + rel->r_addend);
876
                            break;
877
                        case R_ALPHA_LITUSE:
878
                            /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
879
                               now, since some called functions (libc) need pv to be set up.  */
880
                            break;
881
                        case R_ALPHA_HINT:
882
                            /* Branch target prediction hint. Ignore for now.  Should be already
883
                               correct for in-function jumps.  */
884
                            break;
885
                        case R_ALPHA_LITERAL:
886
                            /* Load a literal from the GOT relative to the gp.  Since there's only a
887
                               single gp, nothing is to be done.  */
888
                            break;
889
                        case R_ALPHA_GPRELHIGH:
890
                            /* Handle fake relocations against __op_param symbol.  Need to emit the
891
                               high part of the immediate value instead.  Other symbols need no
892
                               special treatment.  */
893
                            if (strstart(sym_name, "__op_param", &p))
894
                                fprintf(outfile, "    immediate_ldah(gen_code_ptr + %ld, param%s);\n",
895
                                        rel->r_offset - start_offset, p);
896
                            break;
897
                        case R_ALPHA_GPRELLOW:
898
                            if (strstart(sym_name, "__op_param", &p))
899
                                fprintf(outfile, "    immediate_lda(gen_code_ptr + %ld, param%s);\n",
900
                                        rel->r_offset - start_offset, p);
901
                            break;
902
                        case R_ALPHA_BRSGP:
903
                            /* PC-relative jump. Tweak offset to skip the two instructions that try to
904
                               set up the gp from the pv.  */
905
                            fprintf(outfile, "    fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
906
                                    rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset);
907
                            break;
908
                        default:
909
                            error("unsupported Alpha relocation (%d)", type);
910
                        }
911
                    }
912
                }
913
            }
914
#elif defined(HOST_IA64)
915
            {
916
                char name[256];
917
                int type;
918
                int addend;
919
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
920
                    if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
921
                        sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
922
                        if (strstart(sym_name, "__op_param", &p)) {
923
                            snprintf(name, sizeof(name), "param%s", p);
924
                        } else {
925
                            snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
926
                        }
927
                        type = ELF64_R_TYPE(rel->r_info);
928
                        addend = rel->r_addend;
929
                        switch(type) {
930
                        case R_IA64_LTOFF22:
931
                            error("must implemnt R_IA64_LTOFF22 relocation");
932
                        case R_IA64_PCREL21B:
933
                            error("must implemnt R_IA64_PCREL21B relocation");
934
                        default:
935
                            error("unsupported ia64 relocation (%d)", type);
936
                        }
937
                    }
938
                }
939
            }
940
#elif defined(HOST_SPARC)
941
            {
942
                char name[256];
943
                int type;
944
                int addend;
945
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
946
                    if (rel->r_offset >= start_offset &&
947
                        rel->r_offset < start_offset + copy_size) {
948
                        sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name;
949
                        if (strstart(sym_name, "__op_param", &p)) {
950
                            snprintf(name, sizeof(name), "param%s", p);
951
                        } else {
952
                                if (sym_name[0] == '.')
953
                                        snprintf(name, sizeof(name),
954
                                                 "(long)(&__dot_%s)",
955
                                                 sym_name + 1);
956
                                else
957
                                        snprintf(name, sizeof(name),
958
                                                 "(long)(&%s)", sym_name);
959
                        }
960
                        type = ELF32_R_TYPE(rel->r_info);
961
                        addend = rel->r_addend;
962
                        switch(type) {
963
                        case R_SPARC_32:
964
                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
965
                                    rel->r_offset - start_offset, name, addend);
966
                            break;
967
                        case R_SPARC_HI22:
968
                            fprintf(outfile,
969
                                    "    *(uint32_t *)(gen_code_ptr + %d) = "
970
                                    "((*(uint32_t *)(gen_code_ptr + %d)) "
971
                                    " & ~0x3fffff) "
972
                                    " | (((%s + %d) >> 10) & 0x3fffff);\n",
973
                                    rel->r_offset - start_offset,
974
                                    rel->r_offset - start_offset,
975
                                    name, addend);
976
                            break;
977
                        case R_SPARC_LO10:
978
                            fprintf(outfile,
979
                                    "    *(uint32_t *)(gen_code_ptr + %d) = "
980
                                    "((*(uint32_t *)(gen_code_ptr + %d)) "
981
                                    " & ~0x3ff) "
982
                                    " | ((%s + %d) & 0x3ff);\n",
983
                                    rel->r_offset - start_offset,
984
                                    rel->r_offset - start_offset,
985
                                    name, addend);
986
                            break;
987
                        case R_SPARC_WDISP30:
988
                            fprintf(outfile,
989
                                    "    *(uint32_t *)(gen_code_ptr + %d) = "
990
                                    "((*(uint32_t *)(gen_code_ptr + %d)) "
991
                                    " & ~0x3fffffff) "
992
                                    " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
993
                                    "    & 0x3fffffff);\n",
994
                                    rel->r_offset - start_offset,
995
                                    rel->r_offset - start_offset,
996
                                    name, addend,
997
                                    rel->r_offset - start_offset);
998
                            break;
999
                        default:
1000
                            error("unsupported sparc relocation (%d)", type);
1001
                        }
1002
                    }
1003
                }
1004
            }
1005
#elif defined(HOST_SPARC64)
1006
            {
1007
                char name[256];
1008
                int type;
1009
                int addend;
1010
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1011
                    if (rel->r_offset >= start_offset &&
1012
                        rel->r_offset < start_offset + copy_size) {
1013
                        sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name;
1014
                        if (strstart(sym_name, "__op_param", &p)) {
1015
                            snprintf(name, sizeof(name), "param%s", p);
1016
                        } else {
1017
                            snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1018
                        }
1019
                        type = ELF64_R_TYPE(rel->r_info);
1020
                        addend = rel->r_addend;
1021
                        switch(type) {
1022
                        case R_SPARC_32:
1023
                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
1024
                                    rel->r_offset - start_offset, name, addend);
1025
                            break;
1026
                        case R_SPARC_HI22:
1027
                            fprintf(outfile,
1028
                                    "    *(uint32_t *)(gen_code_ptr + %d) = "
1029
                                    "((*(uint32_t *)(gen_code_ptr + %d)) "
1030
                                    " & ~0x3fffff) "
1031
                                    " | (((%s + %d) >> 10) & 0x3fffff);\n",
1032
                                    rel->r_offset - start_offset,
1033
                                    rel->r_offset - start_offset,
1034
                                    name, addend);
1035
                            break;
1036
                        case R_SPARC_LO10:
1037
                            fprintf(outfile,
1038
                                    "    *(uint32_t *)(gen_code_ptr + %d) = "
1039
                                    "((*(uint32_t *)(gen_code_ptr + %d)) "
1040
                                    " & ~0x3ff) "
1041
                                    " | ((%s + %d) & 0x3ff);\n",
1042
                                    rel->r_offset - start_offset,
1043
                                    rel->r_offset - start_offset,
1044
                                    name, addend);
1045
                            break;
1046
                        case R_SPARC_WDISP30:
1047
                            fprintf(outfile,
1048
                                    "    *(uint32_t *)(gen_code_ptr + %d) = "
1049
                                    "((*(uint32_t *)(gen_code_ptr + %d)) "
1050
                                    " & ~0x3fffffff) "
1051
                                    " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
1052
                                    "    & 0x3fffffff);\n",
1053
                                    rel->r_offset - start_offset,
1054
                                    rel->r_offset - start_offset,
1055
                                    name, addend,
1056
                                    rel->r_offset - start_offset);
1057
                            break;
1058
                        default:
1059
                            error("unsupported sparc64 relocation (%d)", type);
1060
                        }
1061
                    }
1062
                }
1063
            }
1064
#elif defined(HOST_ARM)
1065
            {
1066
                char name[256];
1067
                int type;
1068
                int addend;
1069

    
1070
                arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
1071
                                  relocs, nb_relocs);
1072

    
1073
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1074
                if (rel->r_offset >= start_offset &&
1075
                    rel->r_offset < start_offset + copy_size) {
1076
                    sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1077
                    /* the compiler leave some unnecessary references to the code */
1078
                    if (sym_name[0] == '\0')
1079
                        continue;
1080
                    if (strstart(sym_name, "__op_param", &p)) {
1081
                        snprintf(name, sizeof(name), "param%s", p);
1082
                    } else {
1083
                        snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1084
                    }
1085
                    type = ELF32_R_TYPE(rel->r_info);
1086
                    addend = get32((uint32_t *)(text + rel->r_offset));
1087
                    switch(type) {
1088
                    case R_ARM_ABS32:
1089
                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
1090
                                rel->r_offset - start_offset, name, addend);
1091
                        break;
1092
                    case R_ARM_PC24:
1093
                        fprintf(outfile, "    arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n", 
1094
                                rel->r_offset - start_offset, addend, name);
1095
                        break;
1096
                    default:
1097
                        error("unsupported arm relocation (%d)", type);
1098
                    }
1099
                }
1100
                }
1101
            }
1102
#elif defined(HOST_M68K)
1103
            {
1104
                char name[256];
1105
                int type;
1106
                int addend;
1107
                Elf32_Sym *sym;
1108
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1109
                if (rel->r_offset >= start_offset &&
1110
                    rel->r_offset < start_offset + copy_size) {
1111
                    sym = &(symtab[ELFW(R_SYM)(rel->r_info)]);
1112
                    sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1113
                    if (strstart(sym_name, "__op_param", &p)) {
1114
                        snprintf(name, sizeof(name), "param%s", p);
1115
                    } else {
1116
                        snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1117
                    }
1118
                    type = ELF32_R_TYPE(rel->r_info);
1119
                    addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend;
1120
                    switch(type) {
1121
                    case R_68K_32:
1122
                        fprintf(outfile, "    /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ;
1123
                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n", 
1124
                                rel->r_offset - start_offset, name, addend );
1125
                        break;
1126
                    case R_68K_PC32:
1127
                        fprintf(outfile, "    /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset);
1128
                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n", 
1129
                                rel->r_offset - start_offset, name, rel->r_offset - start_offset, /*sym->st_value+*/ addend);
1130
                        break;
1131
                    default:
1132
                        error("unsupported m68k relocation (%d)", type);
1133
                    }
1134
                }
1135
                }
1136
            }
1137
#else
1138
#error unsupported CPU
1139
#endif
1140
        fprintf(outfile, "    gen_code_ptr += %d;\n", copy_size);
1141
        fprintf(outfile, "}\n");
1142
        fprintf(outfile, "break;\n\n");
1143
    } else {
1144
        fprintf(outfile, "static inline void gen_%s(", name);
1145
        if (nb_args == 0) {
1146
            fprintf(outfile, "void");
1147
        } else {
1148
            for(i = 0; i < nb_args; i++) {
1149
                if (i != 0)
1150
                    fprintf(outfile, ", ");
1151
                fprintf(outfile, "long param%d", i + 1);
1152
            }
1153
        }
1154
        fprintf(outfile, ")\n");
1155
        fprintf(outfile, "{\n");
1156
        for(i = 0; i < nb_args; i++) {
1157
            fprintf(outfile, "    *gen_opparam_ptr++ = param%d;\n", i + 1);
1158
        }
1159
        fprintf(outfile, "    *gen_opc_ptr++ = INDEX_%s;\n", name);
1160
        fprintf(outfile, "}\n\n");
1161
    }
1162
}
1163

    
1164
/* load an elf object file */
1165
int load_elf(const char *filename, FILE *outfile, int out_type)
1166
{
1167
    int fd;
1168
    struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
1169
    int i, j;
1170
    ElfW(Sym) *sym;
1171
    char *shstr;
1172
    uint8_t *text;
1173
    ELF_RELOC *relocs;
1174
    int nb_relocs;
1175
    ELF_RELOC *rel;
1176
    
1177
    fd = open(filename, O_RDONLY);
1178
    if (fd < 0) 
1179
        error("can't open file '%s'", filename);
1180
    
1181
    /* Read ELF header.  */
1182
    if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
1183
        error("unable to read file header");
1184

    
1185
    /* Check ELF identification.  */
1186
    if (ehdr.e_ident[EI_MAG0] != ELFMAG0
1187
     || ehdr.e_ident[EI_MAG1] != ELFMAG1
1188
     || ehdr.e_ident[EI_MAG2] != ELFMAG2
1189
     || ehdr.e_ident[EI_MAG3] != ELFMAG3
1190
     || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
1191
        error("bad ELF header");
1192
    }
1193

    
1194
    do_swap = elf_must_swap(&ehdr);
1195
    if (do_swap)
1196
        elf_swap_ehdr(&ehdr);
1197
    if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
1198
        error("Unsupported ELF class");
1199
    if (ehdr.e_type != ET_REL)
1200
        error("ELF object file expected");
1201
    if (ehdr.e_version != EV_CURRENT)
1202
        error("Invalid ELF version");
1203
    if (!elf_check_arch(ehdr.e_machine))
1204
        error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
1205

    
1206
    /* read section headers */
1207
    shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
1208
    if (do_swap) {
1209
        for(i = 0; i < ehdr.e_shnum; i++) {
1210
            elf_swap_shdr(&shdr[i]);
1211
        }
1212
    }
1213

    
1214
    /* read all section data */
1215
    sdata = malloc(sizeof(void *) * ehdr.e_shnum);
1216
    memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
1217
    
1218
    for(i = 0;i < ehdr.e_shnum; i++) {
1219
        sec = &shdr[i];
1220
        if (sec->sh_type != SHT_NOBITS)
1221
            sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
1222
    }
1223

    
1224
    sec = &shdr[ehdr.e_shstrndx];
1225
    shstr = sdata[ehdr.e_shstrndx];
1226

    
1227
    /* swap relocations */
1228
    for(i = 0; i < ehdr.e_shnum; i++) {
1229
        sec = &shdr[i];
1230
        if (sec->sh_type == SHT_RELOC) {
1231
            nb_relocs = sec->sh_size / sec->sh_entsize;
1232
            if (do_swap) {
1233
                for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
1234
                    elf_swap_rel(rel);
1235
            }
1236
        }
1237
    }
1238
    /* text section */
1239

    
1240
    text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
1241
    if (!text_sec)
1242
        error("could not find .text section");
1243
    text_shndx = text_sec - shdr;
1244
    text = sdata[text_shndx];
1245

    
1246
    /* find text relocations, if any */
1247
    relocs = NULL;
1248
    nb_relocs = 0;
1249
    i = find_reloc(text_shndx);
1250
    if (i != 0) {
1251
        relocs = (ELF_RELOC *)sdata[i];
1252
        nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
1253
    }
1254

    
1255
    symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
1256
    if (!symtab_sec)
1257
        error("could not find .symtab section");
1258
    strtab_sec = &shdr[symtab_sec->sh_link];
1259

    
1260
    symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
1261
    strtab = sdata[symtab_sec->sh_link];
1262
    
1263
    nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
1264
    if (do_swap) {
1265
        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1266
            swab32s(&sym->st_name);
1267
            swabls(&sym->st_value);
1268
            swabls(&sym->st_size);
1269
            swab16s(&sym->st_shndx);
1270
        }
1271
    }
1272

    
1273
    if (out_type == OUT_INDEX_OP) {
1274
        fprintf(outfile, "DEF(end, 0, 0)\n");
1275
        fprintf(outfile, "DEF(nop, 0, 0)\n");
1276
        fprintf(outfile, "DEF(nop1, 1, 0)\n");
1277
        fprintf(outfile, "DEF(nop2, 2, 0)\n");
1278
        fprintf(outfile, "DEF(nop3, 3, 0)\n");
1279
        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1280
            const char *name, *p;
1281
            name = strtab + sym->st_name;
1282
            if (strstart(name, OP_PREFIX, &p)) {
1283
                gen_code(name, sym->st_value, sym->st_size, outfile, 
1284
                         text, relocs, nb_relocs, 2);
1285
            }
1286
        }
1287
    } else if (out_type == OUT_GEN_OP) {
1288
        /* generate gen_xxx functions */
1289

    
1290
        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1291
            const char *name;
1292
            name = strtab + sym->st_name;
1293
            if (strstart(name, OP_PREFIX, NULL)) {
1294
                if (sym->st_shndx != (text_sec - shdr))
1295
                    error("invalid section for opcode (0x%x)", sym->st_shndx);
1296
                gen_code(name, sym->st_value, sym->st_size, outfile, 
1297
                         text, relocs, nb_relocs, 0);
1298
            }
1299
        }
1300
        
1301
    } else {
1302
        /* generate big code generation switch */
1303
fprintf(outfile,
1304
"int dyngen_code(uint8_t *gen_code_buf,\n"
1305
"                uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1306
"                const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1307
"{\n"
1308
"    uint8_t *gen_code_ptr;\n"
1309
"    const uint16_t *opc_ptr;\n"
1310
"    const uint32_t *opparam_ptr;\n");
1311

    
1312
#ifdef HOST_ARM
1313
fprintf(outfile,
1314
"    uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1315
"    LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1316
"    uint32_t *arm_data_ptr = arm_data_table;\n");
1317
#endif
1318

    
1319
fprintf(outfile,
1320
"\n"
1321
"    gen_code_ptr = gen_code_buf;\n"
1322
"    opc_ptr = opc_buf;\n"
1323
"    opparam_ptr = opparam_buf;\n");
1324

    
1325
        /* Generate prologue, if needed. */ 
1326

    
1327
fprintf(outfile,
1328
"    for(;;) {\n"
1329
"        switch(*opc_ptr++) {\n"
1330
);
1331

    
1332
        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1333
            const char *name;
1334
            name = strtab + sym->st_name;
1335
            if (strstart(name, OP_PREFIX, NULL)) {
1336
#if 0
1337
                printf("%4d: %s pos=0x%08x len=%d\n", 
1338
                       i, name, sym->st_value, sym->st_size);
1339
#endif
1340
                if (sym->st_shndx != (text_sec - shdr))
1341
                    error("invalid section for opcode (0x%x)", sym->st_shndx);
1342
                gen_code(name, sym->st_value, sym->st_size, outfile, 
1343
                         text, relocs, nb_relocs, 1);
1344
            }
1345
        }
1346

    
1347
fprintf(outfile,
1348
"        case INDEX_op_nop:\n"
1349
"            break;\n"
1350
"        case INDEX_op_nop1:\n"
1351
"            opparam_ptr++;\n"
1352
"            break;\n"
1353
"        case INDEX_op_nop2:\n"
1354
"            opparam_ptr += 2;\n"
1355
"            break;\n"
1356
"        case INDEX_op_nop3:\n"
1357
"            opparam_ptr += 3;\n"
1358
"            break;\n"
1359
"        default:\n"
1360
"            goto the_end;\n"
1361
"        }\n");
1362

    
1363
#ifdef HOST_ARM
1364
/* generate constant table if needed */
1365
fprintf(outfile,
1366
"        if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1367
"            gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1368
"            last_gen_code_ptr = gen_code_ptr;\n"
1369
"            arm_ldr_ptr = arm_ldr_table;\n"
1370
"            arm_data_ptr = arm_data_table;\n"
1371
"        }\n");         
1372
#endif
1373

    
1374

    
1375
fprintf(outfile,
1376
"    }\n"
1377
" the_end:\n"
1378
);
1379

    
1380
/* generate some code patching */ 
1381
#ifdef HOST_ARM
1382
fprintf(outfile, "gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 0);\n");
1383
#endif
1384
    /* flush instruction cache */
1385
    fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
1386

    
1387
    fprintf(outfile, "return gen_code_ptr -  gen_code_buf;\n");
1388
    fprintf(outfile, "}\n\n");
1389

    
1390
    }
1391

    
1392
    close(fd);
1393
    return 0;
1394
}
1395

    
1396
void usage(void)
1397
{
1398
    printf("dyngen (c) 2003 Fabrice Bellard\n"
1399
           "usage: dyngen [-o outfile] [-c] objfile\n"
1400
           "Generate a dynamic code generator from an object file\n"
1401
           "-c     output enum of operations\n"
1402
           "-g     output gen_op_xx() functions\n"
1403
           );
1404
    exit(1);
1405
}
1406

    
1407
int main(int argc, char **argv)
1408
{
1409
    int c, out_type;
1410
    const char *filename, *outfilename;
1411
    FILE *outfile;
1412

    
1413
    outfilename = "out.c";
1414
    out_type = OUT_CODE;
1415
    for(;;) {
1416
        c = getopt(argc, argv, "ho:cg");
1417
        if (c == -1)
1418
            break;
1419
        switch(c) {
1420
        case 'h':
1421
            usage();
1422
            break;
1423
        case 'o':
1424
            outfilename = optarg;
1425
            break;
1426
        case 'c':
1427
            out_type = OUT_INDEX_OP;
1428
            break;
1429
        case 'g':
1430
            out_type = OUT_GEN_OP;
1431
            break;
1432
        }
1433
    }
1434
    if (optind >= argc)
1435
        usage();
1436
    filename = argv[optind];
1437
    outfile = fopen(outfilename, "w");
1438
    if (!outfile)
1439
        error("could not open '%s'", outfilename);
1440
    load_elf(filename, outfile, out_type);
1441
    fclose(outfile);
1442
    return 0;
1443
}