Statistics
| Branch: | Revision:

root / dyngen.c @ 9621339d

History | View | Annotate | Download (45.7 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.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
#else
90
#error unsupported CPU - please update the code
91
#endif
92

    
93
#include "elf.h"
94

    
95
#if ELF_CLASS == ELFCLASS32
96
typedef int32_t host_long;
97
typedef uint32_t host_ulong;
98
#define swabls(x) swab32s(x)
99
#else
100
typedef int64_t host_long;
101
typedef uint64_t host_ulong;
102
#define swabls(x) swab64s(x)
103
#endif
104

    
105
#ifdef ELF_USES_RELOCA
106
#define SHT_RELOC SHT_RELA
107
#else
108
#define SHT_RELOC SHT_REL
109
#endif
110

    
111
#define NO_THUNK_TYPE_SIZE
112
#include "thunk.h"
113

    
114
enum {
115
    OUT_GEN_OP,
116
    OUT_CODE,
117
    OUT_INDEX_OP,
118
};
119

    
120
/* all dynamically generated functions begin with this code */
121
#define OP_PREFIX "op_"
122

    
123
int elf_must_swap(struct elfhdr *h)
124
{
125
  union {
126
      uint32_t i;
127
      uint8_t b[4];
128
  } swaptest;
129

    
130
  swaptest.i = 1;
131
  return (h->e_ident[EI_DATA] == ELFDATA2MSB) != 
132
      (swaptest.b[0] == 0);
133
}
134
  
135
void swab16s(uint16_t *p)
136
{
137
    *p = bswap16(*p);
138
}
139

    
140
void swab32s(uint32_t *p)
141
{
142
    *p = bswap32(*p);
143
}
144

    
145
void swab64s(uint64_t *p)
146
{
147
    *p = bswap64(*p);
148
}
149

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

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

    
181
void elf_swap_phdr(struct elf_phdr *h)
182
{
183
    swab32s(&h->p_type);                        /* Segment type */
184
    swabls(&h->p_offset);                /* Segment file offset */
185
    swabls(&h->p_vaddr);                /* Segment virtual address */
186
    swabls(&h->p_paddr);                /* Segment physical address */
187
    swabls(&h->p_filesz);                /* Segment size in file */
188
    swabls(&h->p_memsz);                /* Segment size in memory */
189
    swab32s(&h->p_flags);                /* Segment flags */
190
    swabls(&h->p_align);                /* Segment alignment */
191
}
192

    
193
void elf_swap_rel(ELF_RELOC *rel)
194
{
195
    swabls(&rel->r_offset);
196
    swabls(&rel->r_info);
197
#ifdef ELF_USES_RELOCA
198
    swabls(&rel->r_addend);
199
#endif
200
}
201

    
202
/* ELF file info */
203
int do_swap;
204
struct elf_shdr *shdr;
205
uint8_t **sdata;
206
struct elfhdr ehdr;
207
ElfW(Sym) *symtab;
208
int nb_syms;
209
char *strtab;
210
int text_shndx;
211

    
212
uint16_t get16(uint16_t *p)
213
{
214
    uint16_t val;
215
    val = *p;
216
    if (do_swap)
217
        val = bswap16(val);
218
    return val;
219
}
220

    
221
uint32_t get32(uint32_t *p)
222
{
223
    uint32_t val;
224
    val = *p;
225
    if (do_swap)
226
        val = bswap32(val);
227
    return val;
228
}
229

    
230
void put16(uint16_t *p, uint16_t val)
231
{
232
    if (do_swap)
233
        val = bswap16(val);
234
    *p = val;
235
}
236

    
237
void put32(uint32_t *p, uint32_t val)
238
{
239
    if (do_swap)
240
        val = bswap32(val);
241
    *p = val;
242
}
243

    
244
void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
245
{
246
    va_list ap;
247
    va_start(ap, fmt);
248
    fprintf(stderr, "dyngen: ");
249
    vfprintf(stderr, fmt, ap);
250
    fprintf(stderr, "\n");
251
    va_end(ap);
252
    exit(1);
253
}
254

    
255

    
256
struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr, 
257
                                  const char *name)
258
{
259
    int i;
260
    const char *shname;
261
    struct elf_shdr *sec;
262

    
263
    for(i = 0; i < shnum; i++) {
264
        sec = &shdr[i];
265
        if (!sec->sh_name)
266
            continue;
267
        shname = shstr + sec->sh_name;
268
        if (!strcmp(shname, name))
269
            return sec;
270
    }
271
    return NULL;
272
}
273

    
274
int find_reloc(int sh_index)
275
{
276
    struct elf_shdr *sec;
277
    int i;
278

    
279
    for(i = 0; i < ehdr.e_shnum; i++) {
280
        sec = &shdr[i];
281
        if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index) 
282
            return i;
283
    }
284
    return 0;
285
}
286

    
287
void *load_data(int fd, long offset, unsigned int size)
288
{
289
    char *data;
290

    
291
    data = malloc(size);
292
    if (!data)
293
        return NULL;
294
    lseek(fd, offset, SEEK_SET);
295
    if (read(fd, data, size) != size) {
296
        free(data);
297
        return NULL;
298
    }
299
    return data;
300
}
301

    
302
int strstart(const char *str, const char *val, const char **ptr)
303
{
304
    const char *p, *q;
305
    p = str;
306
    q = val;
307
    while (*q != '\0') {
308
        if (*p != *q)
309
            return 0;
310
        p++;
311
        q++;
312
    }
313
    if (ptr)
314
        *ptr = p;
315
    return 1;
316
}
317

    
318
#ifdef HOST_ARM
319

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

    
368
                    data_allocated[data_index] = 1;
369

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

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

    
417

    
418
#define MAX_ARGS 3
419

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

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

    
555
            /* Skip a preceeding nop, if present.  */
556
            if (p > p_start) {
557
                skip_insn = get32((uint32_t *)(p - 0x4));
558
                if (skip_insn == 0x01000000)
559
                    p -= 4;
560
            }
561

    
562
            copy_size = p - p_start;
563
        }
564
        break;
565
#ifdef HOST_ARM
566
    case EM_ARM:
567
        if ((p_end - p_start) <= 16)
568
            error("%s: function too small", name);
569
        if (get32((uint32_t *)p_start) != 0xe1a0c00d ||
570
            (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 ||
571
            get32((uint32_t *)(p_start + 8)) != 0xe24cb004)
572
            error("%s: invalid prolog", name);
573
        p_start += 12;
574
        start_offset += 12;
575
        copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end, 
576
                                      relocs, nb_relocs);
577
        break;
578
#endif
579
    default:
580
        error("unknown ELF architecture");
581
    }
582

    
583
    /* compute the number of arguments by looking at the relocations */
584
    for(i = 0;i < MAX_ARGS; i++)
585
        args_present[i] = 0;
586

    
587
    for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
588
        if (rel->r_offset >= start_offset &&
589
            rel->r_offset < start_offset + (p_end - p_start)) {
590
            sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
591
            if (strstart(sym_name, "__op_param", &p)) {
592
                n = strtoul(p, NULL, 10);
593
                if (n > MAX_ARGS)
594
                    error("too many arguments in %s", name);
595
                args_present[n - 1] = 1;
596
            }
597
        }
598
    }
599
    
600
    nb_args = 0;
601
    while (nb_args < MAX_ARGS && args_present[nb_args])
602
        nb_args++;
603
    for(i = nb_args; i < MAX_ARGS; i++) {
604
        if (args_present[i])
605
            error("inconsistent argument numbering in %s", name);
606
    }
607

    
608
    if (gen_switch == 2) {
609
        fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size);
610
    } else if (gen_switch == 1) {
611

    
612
        /* output C code */
613
        fprintf(outfile, "case INDEX_%s: {\n", name);
614
        if (nb_args > 0) {
615
            fprintf(outfile, "    long ");
616
            for(i = 0; i < nb_args; i++) {
617
                if (i != 0)
618
                    fprintf(outfile, ", ");
619
                fprintf(outfile, "param%d", i + 1);
620
            }
621
            fprintf(outfile, ";\n");
622
        }
623
        fprintf(outfile, "    extern void %s();\n", name);
624

    
625
        for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
626
            if (rel->r_offset >= start_offset &&
627
                rel->r_offset < start_offset + (p_end - p_start)) {
628
                sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
629
                if (*sym_name && 
630
                    !strstart(sym_name, "__op_param", NULL) &&
631
                    !strstart(sym_name, "__op_jmp", NULL)) {
632
#if defined(HOST_SPARC)
633
                    if (sym_name[0] == '.') {
634
                        fprintf(outfile,
635
                                "extern char __dot_%s __asm__(\"%s\");\n",
636
                                sym_name+1, sym_name);
637
                        continue;
638
                    }
639
#endif
640
                    fprintf(outfile, "extern char %s;\n", sym_name);
641
                }
642
            }
643
        }
644

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

    
647
        /* emit code offset information */
648
        {
649
            ElfW(Sym) *sym;
650
            const char *sym_name, *p;
651
            target_ulong val;
652
            int n;
653

    
654
            for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
655
                sym_name = strtab + sym->st_name;
656
                if (strstart(sym_name, "__op_label", &p)) {
657
                    uint8_t *ptr;
658
                    unsigned long offset;
659
                    
660
                    /* test if the variable refers to a label inside
661
                       the code we are generating */
662
                    ptr = sdata[sym->st_shndx];
663
                    if (!ptr)
664
                        error("__op_labelN in invalid section");
665
                    offset = sym->st_value;
666
                    val = *(target_ulong *)(ptr + offset);
667
#ifdef ELF_USES_RELOCA
668
                    {
669
                        int reloc_shndx, nb_relocs1, j;
670

    
671
                        /* try to find a matching relocation */
672
                        reloc_shndx = find_reloc(sym->st_shndx);
673
                        if (reloc_shndx) {
674
                            nb_relocs1 = shdr[reloc_shndx].sh_size / 
675
                                shdr[reloc_shndx].sh_entsize;
676
                            rel = (ELF_RELOC *)sdata[reloc_shndx];
677
                            for(j = 0; j < nb_relocs1; j++) {
678
                                if (rel->r_offset == offset) {
679
                                    val = rel->r_addend;
680
                                    break;
681
                                }
682
                                rel++;
683
                            }
684
                        }
685
                    }
686
#endif                    
687

    
688
                    if (val >= start_offset && val < start_offset + copy_size) {
689
                        n = strtol(p, NULL, 10);
690
                        fprintf(outfile, "    label_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
691
                    }
692
                }
693
            }
694
        }
695

    
696
        /* load parameres in variables */
697
        for(i = 0; i < nb_args; i++) {
698
            fprintf(outfile, "    param%d = *opparam_ptr++;\n", i + 1);
699
        }
700

    
701
        /* patch relocations */
702
#if defined(HOST_I386)
703
            {
704
                char name[256];
705
                int type;
706
                int addend;
707
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
708
                if (rel->r_offset >= start_offset &&
709
                    rel->r_offset < start_offset + copy_size) {
710
                    sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
711
                    if (strstart(sym_name, "__op_param", &p)) {
712
                        snprintf(name, sizeof(name), "param%s", p);
713
                    } else {
714
                        snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
715
                    }
716
                    type = ELF32_R_TYPE(rel->r_info);
717
                    addend = get32((uint32_t *)(text + rel->r_offset));
718
                    switch(type) {
719
                    case R_386_32:
720
                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
721
                                rel->r_offset - start_offset, name, addend);
722
                        break;
723
                    case R_386_PC32:
724
                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n", 
725
                                rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
726
                        break;
727
                    default:
728
                        error("unsupported i386 relocation (%d)", type);
729
                    }
730
                }
731
                }
732
            }
733
#elif defined(HOST_PPC)
734
            {
735
                char name[256];
736
                int type;
737
                int addend;
738
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
739
                    if (rel->r_offset >= start_offset &&
740
                        rel->r_offset < start_offset + copy_size) {
741
                        sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
742
                        if (strstart(sym_name, "__op_jmp", &p)) {
743
                            int n;
744
                            n = strtol(p, NULL, 10);
745
                            /* __op_jmp relocations are done at
746
                               runtime to do translated block
747
                               chaining: the offset of the instruction
748
                               needs to be stored */
749
                            fprintf(outfile, "    jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
750
                                    n, rel->r_offset - start_offset);
751
                            continue;
752
                        }
753
                        
754
                        if (strstart(sym_name, "__op_param", &p)) {
755
                            snprintf(name, sizeof(name), "param%s", p);
756
                        } else {
757
                            snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
758
                        }
759
                        type = ELF32_R_TYPE(rel->r_info);
760
                        addend = rel->r_addend;
761
                        switch(type) {
762
                        case R_PPC_ADDR32:
763
                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
764
                                    rel->r_offset - start_offset, name, addend);
765
                            break;
766
                        case R_PPC_ADDR16_LO:
767
                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n", 
768
                                    rel->r_offset - start_offset, name, addend);
769
                            break;
770
                        case R_PPC_ADDR16_HI:
771
                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n", 
772
                                    rel->r_offset - start_offset, name, addend);
773
                            break;
774
                        case R_PPC_ADDR16_HA:
775
                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n", 
776
                                    rel->r_offset - start_offset, name, addend);
777
                            break;
778
                        case R_PPC_REL24:
779
                            /* warning: must be at 32 MB distancy */
780
                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n", 
781
                                    rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend);
782
                            break;
783
                        default:
784
                            error("unsupported powerpc relocation (%d)", type);
785
                        }
786
                    }
787
                }
788
            }
789
#elif defined(HOST_S390)
790
            {
791
                char name[256];
792
                int type;
793
                int addend;
794
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
795
                    if (rel->r_offset >= start_offset &&
796
                        rel->r_offset < start_offset + copy_size) {
797
                        sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
798
                        if (strstart(sym_name, "__op_param", &p)) {
799
                            snprintf(name, sizeof(name), "param%s", p);
800
                        } else {
801
                            snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
802
                        }
803
                        type = ELF32_R_TYPE(rel->r_info);
804
                        addend = rel->r_addend;
805
                        switch(type) {
806
                        case R_390_32:
807
                            fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
808
                                    rel->r_offset - start_offset, name, addend);
809
                            break;
810
                        case R_390_16:
811
                            fprintf(outfile, "    *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n", 
812
                                    rel->r_offset - start_offset, name, addend);
813
                            break;
814
                        case R_390_8:
815
                            fprintf(outfile, "    *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n", 
816
                                    rel->r_offset - start_offset, name, addend);
817
                            break;
818
                        default:
819
                            error("unsupported s390 relocation (%d)", type);
820
                        }
821
                    }
822
                }
823
            }
824
#elif defined(HOST_ALPHA)
825
            {
826
                for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) {
827
                    if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
828
                        int type;
829

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

    
1034
                arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end,
1035
                                  relocs, nb_relocs);
1036

    
1037
                for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) {
1038
                if (rel->r_offset >= start_offset &&
1039
                    rel->r_offset < start_offset + copy_size) {
1040
                    sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
1041
                    /* the compiler leave some unnecessary references to the code */
1042
                    if (sym_name[0] == '\0')
1043
                        continue;
1044
                    if (strstart(sym_name, "__op_param", &p)) {
1045
                        snprintf(name, sizeof(name), "param%s", p);
1046
                    } else {
1047
                        snprintf(name, sizeof(name), "(long)(&%s)", sym_name);
1048
                    }
1049
                    type = ELF32_R_TYPE(rel->r_info);
1050
                    addend = get32((uint32_t *)(text + rel->r_offset));
1051
                    switch(type) {
1052
                    case R_ARM_ABS32:
1053
                        fprintf(outfile, "    *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", 
1054
                                rel->r_offset - start_offset, name, addend);
1055
                        break;
1056
                    case R_ARM_PC24:
1057
                        fprintf(outfile, "    arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n", 
1058
                                rel->r_offset - start_offset, addend, name);
1059
                        break;
1060
                    default:
1061
                        error("unsupported arm relocation (%d)", type);
1062
                    }
1063
                }
1064
                }
1065
            }
1066
#else
1067
#error unsupported CPU
1068
#endif
1069
        fprintf(outfile, "    gen_code_ptr += %d;\n", copy_size);
1070
        fprintf(outfile, "}\n");
1071
        fprintf(outfile, "break;\n\n");
1072
    } else {
1073
        fprintf(outfile, "static inline void gen_%s(", name);
1074
        if (nb_args == 0) {
1075
            fprintf(outfile, "void");
1076
        } else {
1077
            for(i = 0; i < nb_args; i++) {
1078
                if (i != 0)
1079
                    fprintf(outfile, ", ");
1080
                fprintf(outfile, "long param%d", i + 1);
1081
            }
1082
        }
1083
        fprintf(outfile, ")\n");
1084
        fprintf(outfile, "{\n");
1085
        for(i = 0; i < nb_args; i++) {
1086
            fprintf(outfile, "    *gen_opparam_ptr++ = param%d;\n", i + 1);
1087
        }
1088
        fprintf(outfile, "    *gen_opc_ptr++ = INDEX_%s;\n", name);
1089
        fprintf(outfile, "}\n\n");
1090
    }
1091
}
1092

    
1093
/* load an elf object file */
1094
int load_elf(const char *filename, FILE *outfile, int out_type)
1095
{
1096
    int fd;
1097
    struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
1098
    int i, j;
1099
    ElfW(Sym) *sym;
1100
    char *shstr;
1101
    uint8_t *text;
1102
    ELF_RELOC *relocs;
1103
    int nb_relocs;
1104
    ELF_RELOC *rel;
1105
    
1106
    fd = open(filename, O_RDONLY);
1107
    if (fd < 0) 
1108
        error("can't open file '%s'", filename);
1109
    
1110
    /* Read ELF header.  */
1111
    if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
1112
        error("unable to read file header");
1113

    
1114
    /* Check ELF identification.  */
1115
    if (ehdr.e_ident[EI_MAG0] != ELFMAG0
1116
     || ehdr.e_ident[EI_MAG1] != ELFMAG1
1117
     || ehdr.e_ident[EI_MAG2] != ELFMAG2
1118
     || ehdr.e_ident[EI_MAG3] != ELFMAG3
1119
     || ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
1120
        error("bad ELF header");
1121
    }
1122

    
1123
    do_swap = elf_must_swap(&ehdr);
1124
    if (do_swap)
1125
        elf_swap_ehdr(&ehdr);
1126
    if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
1127
        error("Unsupported ELF class");
1128
    if (ehdr.e_type != ET_REL)
1129
        error("ELF object file expected");
1130
    if (ehdr.e_version != EV_CURRENT)
1131
        error("Invalid ELF version");
1132
    if (!elf_check_arch(ehdr.e_machine))
1133
        error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
1134

    
1135
    /* read section headers */
1136
    shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
1137
    if (do_swap) {
1138
        for(i = 0; i < ehdr.e_shnum; i++) {
1139
            elf_swap_shdr(&shdr[i]);
1140
        }
1141
    }
1142

    
1143
    /* read all section data */
1144
    sdata = malloc(sizeof(void *) * ehdr.e_shnum);
1145
    memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
1146
    
1147
    for(i = 0;i < ehdr.e_shnum; i++) {
1148
        sec = &shdr[i];
1149
        if (sec->sh_type != SHT_NOBITS)
1150
            sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
1151
    }
1152

    
1153
    sec = &shdr[ehdr.e_shstrndx];
1154
    shstr = sdata[ehdr.e_shstrndx];
1155

    
1156
    /* swap relocations */
1157
    for(i = 0; i < ehdr.e_shnum; i++) {
1158
        sec = &shdr[i];
1159
        if (sec->sh_type == SHT_RELOC) {
1160
            nb_relocs = sec->sh_size / sec->sh_entsize;
1161
            if (do_swap) {
1162
                for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
1163
                    elf_swap_rel(rel);
1164
            }
1165
        }
1166
    }
1167
    /* text section */
1168

    
1169
    text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
1170
    if (!text_sec)
1171
        error("could not find .text section");
1172
    text_shndx = text_sec - shdr;
1173
    text = sdata[text_shndx];
1174

    
1175
    /* find text relocations, if any */
1176
    relocs = NULL;
1177
    nb_relocs = 0;
1178
    i = find_reloc(text_shndx);
1179
    if (i != 0) {
1180
        relocs = (ELF_RELOC *)sdata[i];
1181
        nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
1182
    }
1183

    
1184
    symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
1185
    if (!symtab_sec)
1186
        error("could not find .symtab section");
1187
    strtab_sec = &shdr[symtab_sec->sh_link];
1188

    
1189
    symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
1190
    strtab = sdata[symtab_sec->sh_link];
1191
    
1192
    nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
1193
    if (do_swap) {
1194
        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1195
            swab32s(&sym->st_name);
1196
            swabls(&sym->st_value);
1197
            swabls(&sym->st_size);
1198
            swab16s(&sym->st_shndx);
1199
        }
1200
    }
1201

    
1202
    if (out_type == OUT_INDEX_OP) {
1203
        fprintf(outfile, "DEF(end, 0, 0)\n");
1204
        fprintf(outfile, "DEF(nop, 0, 0)\n");
1205
        fprintf(outfile, "DEF(nop1, 1, 0)\n");
1206
        fprintf(outfile, "DEF(nop2, 2, 0)\n");
1207
        fprintf(outfile, "DEF(nop3, 3, 0)\n");
1208
        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1209
            const char *name, *p;
1210
            name = strtab + sym->st_name;
1211
            if (strstart(name, OP_PREFIX, &p)) {
1212
                gen_code(name, sym->st_value, sym->st_size, outfile, 
1213
                         text, relocs, nb_relocs, 2);
1214
            }
1215
        }
1216
    } else if (out_type == OUT_GEN_OP) {
1217
        /* generate gen_xxx functions */
1218

    
1219
        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1220
            const char *name;
1221
            name = strtab + sym->st_name;
1222
            if (strstart(name, OP_PREFIX, NULL)) {
1223
                if (sym->st_shndx != (text_sec - shdr))
1224
                    error("invalid section for opcode (0x%x)", sym->st_shndx);
1225
                gen_code(name, sym->st_value, sym->st_size, outfile, 
1226
                         text, relocs, nb_relocs, 0);
1227
            }
1228
        }
1229
        
1230
    } else {
1231
        /* generate big code generation switch */
1232
fprintf(outfile,
1233
"int dyngen_code(uint8_t *gen_code_buf,\n"
1234
"                uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
1235
"                const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
1236
"{\n"
1237
"    uint8_t *gen_code_ptr;\n"
1238
"    const uint16_t *opc_ptr;\n"
1239
"    const uint32_t *opparam_ptr;\n");
1240

    
1241
#ifdef HOST_ARM
1242
fprintf(outfile,
1243
"    uint8_t *last_gen_code_ptr = gen_code_buf;\n"
1244
"    LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
1245
"    uint32_t *arm_data_ptr = arm_data_table;\n");
1246
#endif
1247

    
1248
fprintf(outfile,
1249
"\n"
1250
"    gen_code_ptr = gen_code_buf;\n"
1251
"    opc_ptr = opc_buf;\n"
1252
"    opparam_ptr = opparam_buf;\n");
1253

    
1254
        /* Generate prologue, if needed. */ 
1255

    
1256
fprintf(outfile,
1257
"    for(;;) {\n"
1258
"        switch(*opc_ptr++) {\n"
1259
);
1260

    
1261
        for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
1262
            const char *name;
1263
            name = strtab + sym->st_name;
1264
            if (strstart(name, OP_PREFIX, NULL)) {
1265
#if 0
1266
                printf("%4d: %s pos=0x%08x len=%d\n", 
1267
                       i, name, sym->st_value, sym->st_size);
1268
#endif
1269
                if (sym->st_shndx != (text_sec - shdr))
1270
                    error("invalid section for opcode (0x%x)", sym->st_shndx);
1271
                gen_code(name, sym->st_value, sym->st_size, outfile, 
1272
                         text, relocs, nb_relocs, 1);
1273
            }
1274
        }
1275

    
1276
fprintf(outfile,
1277
"        case INDEX_op_nop:\n"
1278
"            break;\n"
1279
"        case INDEX_op_nop1:\n"
1280
"            opparam_ptr++;\n"
1281
"            break;\n"
1282
"        case INDEX_op_nop2:\n"
1283
"            opparam_ptr += 2;\n"
1284
"            break;\n"
1285
"        case INDEX_op_nop3:\n"
1286
"            opparam_ptr += 3;\n"
1287
"            break;\n"
1288
"        default:\n"
1289
"            goto the_end;\n"
1290
"        }\n");
1291

    
1292
#ifdef HOST_ARM
1293
/* generate constant table if needed */
1294
fprintf(outfile,
1295
"        if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
1296
"            gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n"
1297
"            last_gen_code_ptr = gen_code_ptr;\n"
1298
"            arm_ldr_ptr = arm_ldr_table;\n"
1299
"            arm_data_ptr = arm_data_table;\n"
1300
"        }\n");         
1301
#endif
1302

    
1303

    
1304
fprintf(outfile,
1305
"    }\n"
1306
" the_end:\n"
1307
);
1308

    
1309
/* generate some code patching */ 
1310
#ifdef HOST_ARM
1311
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");
1312
#endif
1313
    /* flush instruction cache */
1314
    fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
1315

    
1316
    fprintf(outfile, "return gen_code_ptr -  gen_code_buf;\n");
1317
    fprintf(outfile, "}\n\n");
1318

    
1319
    }
1320

    
1321
    close(fd);
1322
    return 0;
1323
}
1324

    
1325
void usage(void)
1326
{
1327
    printf("dyngen (c) 2003 Fabrice Bellard\n"
1328
           "usage: dyngen [-o outfile] [-c] objfile\n"
1329
           "Generate a dynamic code generator from an object file\n"
1330
           "-c     output enum of operations\n"
1331
           "-g     output gen_op_xx() functions\n"
1332
           );
1333
    exit(1);
1334
}
1335

    
1336
int main(int argc, char **argv)
1337
{
1338
    int c, out_type;
1339
    const char *filename, *outfilename;
1340
    FILE *outfile;
1341

    
1342
    outfilename = "out.c";
1343
    out_type = OUT_CODE;
1344
    for(;;) {
1345
        c = getopt(argc, argv, "ho:cg");
1346
        if (c == -1)
1347
            break;
1348
        switch(c) {
1349
        case 'h':
1350
            usage();
1351
            break;
1352
        case 'o':
1353
            outfilename = optarg;
1354
            break;
1355
        case 'c':
1356
            out_type = OUT_INDEX_OP;
1357
            break;
1358
        case 'g':
1359
            out_type = OUT_GEN_OP;
1360
            break;
1361
        }
1362
    }
1363
    if (optind >= argc)
1364
        usage();
1365
    filename = argv[optind];
1366
    outfile = fopen(outfilename, "w");
1367
    if (!outfile)
1368
        error("could not open '%s'", outfilename);
1369
    load_elf(filename, outfile, out_type);
1370
    fclose(outfile);
1371
    return 0;
1372
}