]>
Commit | Line | Data |
---|---|---|
7d13299d FB |
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 | */ | |
367e86e8 FB |
20 | #include <stdlib.h> |
21 | #include <stdio.h> | |
04369ff2 | 22 | #include <string.h> |
367e86e8 FB |
23 | #include <stdarg.h> |
24 | #include <inttypes.h> | |
25 | #include <elf.h> | |
26 | #include <unistd.h> | |
27 | #include <fcntl.h> | |
28 | ||
29 | #include "thunk.h" | |
30 | ||
31 | /* all dynamically generated functions begin with this code */ | |
dc99065b | 32 | #define OP_PREFIX "op_" |
367e86e8 FB |
33 | |
34 | int elf_must_swap(Elf32_Ehdr *h) | |
35 | { | |
36 | union { | |
37 | uint32_t i; | |
38 | uint8_t b[4]; | |
39 | } swaptest; | |
40 | ||
41 | swaptest.i = 1; | |
42 | return (h->e_ident[EI_DATA] == ELFDATA2MSB) != | |
43 | (swaptest.b[0] == 0); | |
44 | } | |
45 | ||
46 | void swab16s(uint16_t *p) | |
47 | { | |
48 | *p = bswap16(*p); | |
49 | } | |
50 | ||
51 | void swab32s(uint32_t *p) | |
52 | { | |
53 | *p = bswap32(*p); | |
54 | } | |
55 | ||
56 | void swab64s(uint32_t *p) | |
57 | { | |
58 | *p = bswap64(*p); | |
59 | } | |
60 | ||
61 | void elf_swap_ehdr(Elf32_Ehdr *h) | |
62 | { | |
63 | swab16s(&h->e_type); /* Object file type */ | |
64 | swab16s(&h-> e_machine); /* Architecture */ | |
65 | swab32s(&h-> e_version); /* Object file version */ | |
66 | swab32s(&h-> e_entry); /* Entry point virtual address */ | |
67 | swab32s(&h-> e_phoff); /* Program header table file offset */ | |
68 | swab32s(&h-> e_shoff); /* Section header table file offset */ | |
69 | swab32s(&h-> e_flags); /* Processor-specific flags */ | |
70 | swab16s(&h-> e_ehsize); /* ELF header size in bytes */ | |
71 | swab16s(&h-> e_phentsize); /* Program header table entry size */ | |
72 | swab16s(&h-> e_phnum); /* Program header table entry count */ | |
73 | swab16s(&h-> e_shentsize); /* Section header table entry size */ | |
74 | swab16s(&h-> e_shnum); /* Section header table entry count */ | |
75 | swab16s(&h-> e_shstrndx); /* Section header string table index */ | |
76 | } | |
77 | ||
78 | void elf_swap_shdr(Elf32_Shdr *h) | |
79 | { | |
80 | swab32s(&h-> sh_name); /* Section name (string tbl index) */ | |
81 | swab32s(&h-> sh_type); /* Section type */ | |
82 | swab32s(&h-> sh_flags); /* Section flags */ | |
83 | swab32s(&h-> sh_addr); /* Section virtual addr at execution */ | |
84 | swab32s(&h-> sh_offset); /* Section file offset */ | |
85 | swab32s(&h-> sh_size); /* Section size in bytes */ | |
86 | swab32s(&h-> sh_link); /* Link to another section */ | |
87 | swab32s(&h-> sh_info); /* Additional section information */ | |
88 | swab32s(&h-> sh_addralign); /* Section alignment */ | |
89 | swab32s(&h-> sh_entsize); /* Entry size if section holds table */ | |
90 | } | |
91 | ||
92 | void elf_swap_phdr(Elf32_Phdr *h) | |
93 | { | |
94 | swab32s(&h->p_type); /* Segment type */ | |
95 | swab32s(&h->p_offset); /* Segment file offset */ | |
96 | swab32s(&h->p_vaddr); /* Segment virtual address */ | |
97 | swab32s(&h->p_paddr); /* Segment physical address */ | |
98 | swab32s(&h->p_filesz); /* Segment size in file */ | |
99 | swab32s(&h->p_memsz); /* Segment size in memory */ | |
100 | swab32s(&h->p_flags); /* Segment flags */ | |
101 | swab32s(&h->p_align); /* Segment alignment */ | |
102 | } | |
103 | ||
104 | int do_swap; | |
105 | int e_machine; | |
106 | ||
107 | uint16_t get16(uint16_t *p) | |
108 | { | |
109 | uint16_t val; | |
110 | val = *p; | |
111 | if (do_swap) | |
112 | val = bswap16(val); | |
113 | return val; | |
114 | } | |
115 | ||
116 | uint32_t get32(uint32_t *p) | |
117 | { | |
118 | uint32_t val; | |
119 | val = *p; | |
120 | if (do_swap) | |
121 | val = bswap32(val); | |
122 | return val; | |
123 | } | |
124 | ||
125 | void put16(uint16_t *p, uint16_t val) | |
126 | { | |
127 | if (do_swap) | |
128 | val = bswap16(val); | |
129 | *p = val; | |
130 | } | |
131 | ||
132 | void put32(uint32_t *p, uint32_t val) | |
133 | { | |
134 | if (do_swap) | |
135 | val = bswap32(val); | |
136 | *p = val; | |
137 | } | |
138 | ||
139 | void __attribute__((noreturn)) error(const char *fmt, ...) | |
140 | { | |
141 | va_list ap; | |
142 | va_start(ap, fmt); | |
143 | fprintf(stderr, "dyngen: "); | |
144 | vfprintf(stderr, fmt, ap); | |
145 | fprintf(stderr, "\n"); | |
146 | va_end(ap); | |
147 | exit(1); | |
148 | } | |
149 | ||
150 | ||
151 | Elf32_Shdr *find_elf_section(Elf32_Shdr *shdr, int shnum, const char *shstr, | |
152 | const char *name) | |
153 | { | |
154 | int i; | |
155 | const char *shname; | |
156 | Elf32_Shdr *sec; | |
157 | ||
158 | for(i = 0; i < shnum; i++) { | |
159 | sec = &shdr[i]; | |
160 | if (!sec->sh_name) | |
161 | continue; | |
162 | shname = shstr + sec->sh_name; | |
163 | if (!strcmp(shname, name)) | |
164 | return sec; | |
165 | } | |
166 | return NULL; | |
167 | } | |
168 | ||
169 | void *load_data(int fd, long offset, unsigned int size) | |
170 | { | |
171 | char *data; | |
172 | ||
173 | data = malloc(size); | |
174 | if (!data) | |
175 | return NULL; | |
176 | lseek(fd, offset, SEEK_SET); | |
177 | if (read(fd, data, size) != size) { | |
178 | free(data); | |
179 | return NULL; | |
180 | } | |
181 | return data; | |
182 | } | |
183 | ||
184 | int strstart(const char *str, const char *val, const char **ptr) | |
185 | { | |
186 | const char *p, *q; | |
187 | p = str; | |
188 | q = val; | |
189 | while (*q != '\0') { | |
190 | if (*p != *q) | |
191 | return 0; | |
192 | p++; | |
193 | q++; | |
194 | } | |
195 | if (ptr) | |
196 | *ptr = p; | |
197 | return 1; | |
198 | } | |
199 | ||
200 | #define MAX_ARGS 3 | |
201 | ||
202 | /* generate op code */ | |
203 | void gen_code(const char *name, unsigned long offset, unsigned long size, | |
204 | FILE *outfile, uint8_t *text, void *relocs, int nb_relocs, int reloc_sh_type, | |
dc99065b | 205 | Elf32_Sym *symtab, char *strtab, int gen_switch) |
367e86e8 FB |
206 | { |
207 | int copy_size = 0; | |
208 | uint8_t *p_start, *p_end; | |
209 | int nb_args, i; | |
210 | uint8_t args_present[MAX_ARGS]; | |
211 | const char *sym_name, *p; | |
212 | ||
213 | /* compute exact size excluding return instruction */ | |
214 | p_start = text + offset; | |
215 | p_end = p_start + size; | |
216 | switch(e_machine) { | |
217 | case EM_386: | |
218 | { | |
219 | uint8_t *p; | |
220 | p = p_end - 1; | |
367e86e8 FB |
221 | if (p == p_start) |
222 | error("empty code for %s", name); | |
4b74fe1f FB |
223 | if (p[0] != 0xc3) |
224 | error("ret expected at the end of %s", name); | |
367e86e8 FB |
225 | copy_size = p - p_start; |
226 | } | |
227 | break; | |
228 | case EM_PPC: | |
229 | { | |
230 | uint8_t *p; | |
231 | p = (void *)(p_end - 4); | |
367e86e8 FB |
232 | if (p == p_start) |
233 | error("empty code for %s", name); | |
04369ff2 FB |
234 | if (get32((uint32_t *)p) != 0x4e800020) |
235 | error("blr expected at the end of %s", name); | |
367e86e8 FB |
236 | copy_size = p - p_start; |
237 | } | |
238 | break; | |
239 | default: | |
240 | error("unsupported CPU (%d)", e_machine); | |
241 | } | |
242 | ||
243 | /* compute the number of arguments by looking at the relocations */ | |
244 | for(i = 0;i < MAX_ARGS; i++) | |
245 | args_present[i] = 0; | |
246 | ||
247 | if (reloc_sh_type == SHT_REL) { | |
248 | Elf32_Rel *rel; | |
249 | int n; | |
250 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
251 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
252 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; | |
253 | if (strstart(sym_name, "__op_param", &p)) { | |
254 | n = strtoul(p, NULL, 10); | |
255 | if (n >= MAX_ARGS) | |
256 | error("too many arguments in %s", name); | |
257 | args_present[n - 1] = 1; | |
258 | } | |
259 | } | |
260 | } | |
261 | } else { | |
262 | Elf32_Rela *rel; | |
263 | int n; | |
264 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
265 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
266 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; | |
267 | if (strstart(sym_name, "__op_param", &p)) { | |
268 | n = strtoul(p, NULL, 10); | |
269 | if (n >= MAX_ARGS) | |
270 | error("too many arguments in %s", name); | |
271 | args_present[n - 1] = 1; | |
272 | } | |
273 | } | |
274 | } | |
275 | } | |
276 | ||
277 | nb_args = 0; | |
278 | while (nb_args < MAX_ARGS && args_present[nb_args]) | |
279 | nb_args++; | |
280 | for(i = nb_args; i < MAX_ARGS; i++) { | |
281 | if (args_present[i]) | |
282 | error("inconsistent argument numbering in %s", name); | |
283 | } | |
284 | ||
0ea00c9a FB |
285 | if (gen_switch == 2) { |
286 | fprintf(outfile, "DEF(%s, %d)\n", name + 3, nb_args); | |
287 | } else if (gen_switch == 1) { | |
dc99065b FB |
288 | |
289 | /* output C code */ | |
290 | fprintf(outfile, "case INDEX_%s: {\n", name); | |
291 | if (nb_args > 0) { | |
292 | fprintf(outfile, " long "); | |
293 | for(i = 0; i < nb_args; i++) { | |
294 | if (i != 0) | |
295 | fprintf(outfile, ", "); | |
296 | fprintf(outfile, "param%d", i + 1); | |
297 | } | |
298 | fprintf(outfile, ";\n"); | |
367e86e8 | 299 | } |
dc99065b FB |
300 | fprintf(outfile, " extern void %s();\n", name); |
301 | ||
302 | if (reloc_sh_type == SHT_REL) { | |
367e86e8 | 303 | Elf32_Rel *rel; |
367e86e8 | 304 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
dc99065b FB |
305 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { |
306 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; | |
307 | if (!strstart(sym_name, "__op_param", &p)) { | |
308 | fprintf(outfile, "extern char %s;\n", sym_name); | |
309 | } | |
310 | } | |
311 | } | |
312 | } else { | |
313 | Elf32_Rela *rel; | |
314 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
315 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
316 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; | |
317 | if (!strstart(sym_name, "__op_param", &p)) { | |
318 | fprintf(outfile, "extern char %s;\n", sym_name); | |
319 | } | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
324 | fprintf(outfile, " memcpy(gen_code_ptr, &%s, %d);\n", name, copy_size); | |
325 | for(i = 0; i < nb_args; i++) { | |
326 | fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1); | |
327 | } | |
328 | ||
329 | /* patch relocations */ | |
330 | switch(e_machine) { | |
331 | case EM_386: | |
332 | { | |
333 | Elf32_Rel *rel; | |
334 | char name[256]; | |
335 | int type; | |
336 | long addend; | |
337 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
367e86e8 FB |
338 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { |
339 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; | |
340 | if (strstart(sym_name, "__op_param", &p)) { | |
341 | snprintf(name, sizeof(name), "param%s", p); | |
342 | } else { | |
343 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
344 | } | |
345 | type = ELF32_R_TYPE(rel->r_info); | |
346 | addend = get32((uint32_t *)(text + rel->r_offset)); | |
347 | switch(type) { | |
348 | case R_386_32: | |
349 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n", | |
350 | rel->r_offset - offset, name, addend); | |
351 | break; | |
352 | case R_386_PC32: | |
353 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s - (long)(gen_code_ptr + %ld) + %ld;\n", | |
354 | rel->r_offset - offset, name, rel->r_offset - offset, addend); | |
355 | break; | |
356 | default: | |
357 | error("unsupported i386 relocation (%d)", type); | |
358 | } | |
359 | } | |
dc99065b FB |
360 | } |
361 | } | |
362 | break; | |
04369ff2 FB |
363 | case EM_PPC: |
364 | { | |
365 | Elf32_Rela *rel; | |
366 | char name[256]; | |
367 | int type; | |
368 | long addend; | |
369 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
370 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
371 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; | |
372 | if (strstart(sym_name, "__op_param", &p)) { | |
373 | snprintf(name, sizeof(name), "param%s", p); | |
374 | } else { | |
375 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
376 | } | |
377 | type = ELF32_R_TYPE(rel->r_info); | |
378 | addend = rel->r_addend; | |
379 | switch(type) { | |
380 | case R_PPC_ADDR32: | |
381 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = %s + %ld;\n", | |
382 | rel->r_offset - offset, name, addend); | |
383 | break; | |
384 | case R_PPC_ADDR16_LO: | |
385 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld);\n", | |
386 | rel->r_offset - offset, name, addend); | |
387 | break; | |
388 | case R_PPC_ADDR16_HI: | |
389 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld) >> 16;\n", | |
390 | rel->r_offset - offset, name, addend); | |
391 | break; | |
392 | case R_PPC_ADDR16_HA: | |
393 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %ld) = (%s + %ld + 0x8000) >> 16;\n", | |
394 | rel->r_offset - offset, name, addend); | |
395 | break; | |
396 | case R_PPC_REL24: | |
397 | /* warning: must be at 32 MB distancy */ | |
398 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %ld) = (*(uint32_t *)(gen_code_ptr + %ld) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %ld) + %ld) & 0x03fffffc);\n", | |
399 | rel->r_offset - offset, rel->r_offset - offset, name, rel->r_offset - offset, addend); | |
400 | break; | |
401 | default: | |
402 | error("unsupported powerpc relocation (%d)", type); | |
403 | } | |
404 | } | |
405 | } | |
406 | } | |
407 | break; | |
dc99065b FB |
408 | default: |
409 | error("unsupported CPU for relocations (%d)", e_machine); | |
410 | } | |
411 | fprintf(outfile, " gen_code_ptr += %d;\n", copy_size); | |
412 | fprintf(outfile, "}\n"); | |
413 | fprintf(outfile, "break;\n\n"); | |
414 | } else { | |
415 | fprintf(outfile, "static inline void gen_%s(", name); | |
416 | if (nb_args == 0) { | |
417 | fprintf(outfile, "void"); | |
418 | } else { | |
419 | for(i = 0; i < nb_args; i++) { | |
420 | if (i != 0) | |
421 | fprintf(outfile, ", "); | |
422 | fprintf(outfile, "long param%d", i + 1); | |
367e86e8 FB |
423 | } |
424 | } | |
dc99065b FB |
425 | fprintf(outfile, ")\n"); |
426 | fprintf(outfile, "{\n"); | |
427 | for(i = 0; i < nb_args; i++) { | |
428 | fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1); | |
429 | } | |
430 | fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name); | |
431 | fprintf(outfile, "}\n\n"); | |
367e86e8 | 432 | } |
367e86e8 FB |
433 | } |
434 | ||
435 | /* load an elf object file */ | |
dc99065b | 436 | int load_elf(const char *filename, FILE *outfile, int do_print_enum) |
367e86e8 FB |
437 | { |
438 | int fd; | |
439 | Elf32_Ehdr ehdr; | |
440 | Elf32_Shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec; | |
441 | int i, j, nb_syms; | |
442 | Elf32_Sym *symtab, *sym; | |
443 | const char *cpu_name; | |
444 | char *shstr, *strtab; | |
445 | uint8_t *text; | |
446 | void *relocs; | |
447 | int nb_relocs, reloc_sh_type; | |
448 | ||
449 | fd = open(filename, O_RDONLY); | |
450 | if (fd < 0) | |
451 | error("can't open file '%s'", filename); | |
452 | ||
453 | /* Read ELF header. */ | |
454 | if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr)) | |
455 | error("unable to read file header"); | |
456 | ||
457 | /* Check ELF identification. */ | |
458 | if (ehdr.e_ident[EI_MAG0] != ELFMAG0 | |
459 | || ehdr.e_ident[EI_MAG1] != ELFMAG1 | |
460 | || ehdr.e_ident[EI_MAG2] != ELFMAG2 | |
461 | || ehdr.e_ident[EI_MAG3] != ELFMAG3 | |
462 | || ehdr.e_ident[EI_CLASS] != ELFCLASS32 | |
463 | || ehdr.e_ident[EI_VERSION] != EV_CURRENT) { | |
464 | error("bad ELF header"); | |
465 | } | |
466 | ||
467 | do_swap = elf_must_swap(&ehdr); | |
468 | if (do_swap) | |
469 | elf_swap_ehdr(&ehdr); | |
470 | if (ehdr.e_type != ET_REL) | |
471 | error("ELF object file expected"); | |
472 | if (ehdr.e_version != EV_CURRENT) | |
473 | error("Invalid ELF version"); | |
474 | e_machine = ehdr.e_machine; | |
475 | ||
476 | /* read section headers */ | |
477 | shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(Elf32_Shdr)); | |
478 | if (do_swap) { | |
479 | for(i = 0; i < ehdr.e_shnum; i++) { | |
480 | elf_swap_shdr(&shdr[i]); | |
481 | } | |
482 | } | |
483 | ||
484 | sec = &shdr[ehdr.e_shstrndx]; | |
485 | shstr = load_data(fd, sec->sh_offset, sec->sh_size); | |
486 | ||
487 | /* text section */ | |
488 | ||
489 | text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text"); | |
490 | if (!text_sec) | |
491 | error("could not find .text section"); | |
492 | text = load_data(fd, text_sec->sh_offset, text_sec->sh_size); | |
493 | ||
494 | /* find text relocations, if any */ | |
495 | nb_relocs = 0; | |
496 | relocs = NULL; | |
497 | reloc_sh_type = 0; | |
498 | for(i = 0; i < ehdr.e_shnum; i++) { | |
499 | sec = &shdr[i]; | |
500 | if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) && | |
501 | sec->sh_info == (text_sec - shdr)) { | |
502 | reloc_sh_type = sec->sh_type; | |
503 | relocs = load_data(fd, sec->sh_offset, sec->sh_size); | |
504 | nb_relocs = sec->sh_size / sec->sh_entsize; | |
505 | if (do_swap) { | |
506 | if (sec->sh_type == SHT_REL) { | |
507 | Elf32_Rel *rel = relocs; | |
508 | for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) { | |
509 | swab32s(&rel->r_offset); | |
510 | swab32s(&rel->r_info); | |
511 | } | |
512 | } else { | |
513 | Elf32_Rela *rel = relocs; | |
514 | for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) { | |
515 | swab32s(&rel->r_offset); | |
516 | swab32s(&rel->r_info); | |
517 | swab32s(&rel->r_addend); | |
518 | } | |
519 | } | |
520 | } | |
521 | break; | |
522 | } | |
523 | } | |
524 | ||
525 | symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab"); | |
526 | if (!symtab_sec) | |
527 | error("could not find .symtab section"); | |
528 | strtab_sec = &shdr[symtab_sec->sh_link]; | |
529 | ||
530 | symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size); | |
531 | strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size); | |
532 | ||
533 | nb_syms = symtab_sec->sh_size / sizeof(Elf32_Sym); | |
534 | if (do_swap) { | |
535 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { | |
536 | swab32s(&sym->st_name); | |
537 | swab32s(&sym->st_value); | |
538 | swab32s(&sym->st_size); | |
539 | swab16s(&sym->st_shndx); | |
540 | } | |
541 | } | |
542 | ||
543 | switch(e_machine) { | |
544 | case EM_386: | |
545 | cpu_name = "i386"; | |
546 | break; | |
547 | case EM_PPC: | |
548 | cpu_name = "ppc"; | |
549 | break; | |
550 | case EM_MIPS: | |
551 | cpu_name = "mips"; | |
552 | break; | |
553 | case EM_ARM: | |
554 | cpu_name = "arm"; | |
555 | break; | |
556 | case EM_SPARC: | |
557 | cpu_name = "sparc"; | |
558 | break; | |
559 | default: | |
560 | error("unsupported CPU (e_machine=%d)", e_machine); | |
561 | } | |
562 | ||
dc99065b | 563 | if (do_print_enum) { |
0ea00c9a | 564 | fprintf(outfile, "DEF(end, 0)\n"); |
dc99065b FB |
565 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
566 | const char *name, *p; | |
567 | name = strtab + sym->st_name; | |
568 | if (strstart(name, OP_PREFIX, &p)) { | |
0ea00c9a FB |
569 | gen_code(name, sym->st_value, sym->st_size, outfile, |
570 | text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 2); | |
dc99065b FB |
571 | } |
572 | } | |
573 | } else { | |
574 | /* generate big code generation switch */ | |
575 | fprintf(outfile, | |
576 | "int dyngen_code(uint8_t *gen_code_buf,\n" | |
577 | " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n" | |
578 | "{\n" | |
579 | " uint8_t *gen_code_ptr;\n" | |
580 | " const uint16_t *opc_ptr;\n" | |
581 | " const uint32_t *opparam_ptr;\n" | |
582 | " gen_code_ptr = gen_code_buf;\n" | |
583 | " opc_ptr = opc_buf;\n" | |
584 | " opparam_ptr = opparam_buf;\n" | |
585 | " for(;;) {\n" | |
586 | " switch(*opc_ptr++) {\n" | |
587 | ); | |
367e86e8 | 588 | |
dc99065b FB |
589 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
590 | const char *name; | |
591 | name = strtab + sym->st_name; | |
592 | if (strstart(name, OP_PREFIX, NULL)) { | |
367e86e8 | 593 | #if 0 |
dc99065b FB |
594 | printf("%4d: %s pos=0x%08x len=%d\n", |
595 | i, name, sym->st_value, sym->st_size); | |
367e86e8 | 596 | #endif |
dc99065b FB |
597 | if (sym->st_shndx != (text_sec - shdr)) |
598 | error("invalid section for opcode (0x%x)", sym->st_shndx); | |
599 | gen_code(name, sym->st_value, sym->st_size, outfile, | |
600 | text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 1); | |
601 | } | |
602 | } | |
603 | ||
604 | fprintf(outfile, | |
605 | " default:\n" | |
606 | " goto the_end;\n" | |
607 | " }\n" | |
608 | " }\n" | |
609 | " the_end:\n" | |
610 | ); | |
611 | ||
612 | /* generate a return */ | |
613 | switch(e_machine) { | |
614 | case EM_386: | |
615 | fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n"); | |
616 | break; | |
04369ff2 FB |
617 | case EM_PPC: |
618 | fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n"); | |
619 | break; | |
dc99065b FB |
620 | default: |
621 | error("no return generation for cpu '%s'", cpu_name); | |
622 | } | |
623 | ||
624 | fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n"); | |
625 | fprintf(outfile, "}\n\n"); | |
626 | ||
627 | /* generate gen_xxx functions */ | |
628 | /* XXX: suppress the use of these functions to simplify code */ | |
629 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { | |
630 | const char *name; | |
631 | name = strtab + sym->st_name; | |
632 | if (strstart(name, OP_PREFIX, NULL)) { | |
633 | if (sym->st_shndx != (text_sec - shdr)) | |
634 | error("invalid section for opcode (0x%x)", sym->st_shndx); | |
635 | gen_code(name, sym->st_value, sym->st_size, outfile, | |
636 | text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 0); | |
637 | } | |
367e86e8 FB |
638 | } |
639 | } | |
640 | ||
641 | close(fd); | |
642 | return 0; | |
643 | } | |
644 | ||
645 | void usage(void) | |
646 | { | |
647 | printf("dyngen (c) 2003 Fabrice Bellard\n" | |
dc99065b FB |
648 | "usage: dyngen [-o outfile] [-c] objfile\n" |
649 | "Generate a dynamic code generator from an object file\n" | |
650 | "-c output enum of operations\n" | |
651 | ); | |
367e86e8 FB |
652 | exit(1); |
653 | } | |
654 | ||
655 | int main(int argc, char **argv) | |
656 | { | |
dc99065b | 657 | int c, do_print_enum; |
367e86e8 FB |
658 | const char *filename, *outfilename; |
659 | FILE *outfile; | |
660 | ||
661 | outfilename = "out.c"; | |
dc99065b | 662 | do_print_enum = 0; |
367e86e8 | 663 | for(;;) { |
dc99065b | 664 | c = getopt(argc, argv, "ho:c"); |
367e86e8 FB |
665 | if (c == -1) |
666 | break; | |
667 | switch(c) { | |
668 | case 'h': | |
669 | usage(); | |
670 | break; | |
671 | case 'o': | |
672 | outfilename = optarg; | |
673 | break; | |
dc99065b FB |
674 | case 'c': |
675 | do_print_enum = 1; | |
676 | break; | |
367e86e8 FB |
677 | } |
678 | } | |
679 | if (optind >= argc) | |
680 | usage(); | |
681 | filename = argv[optind]; | |
682 | outfile = fopen(outfilename, "w"); | |
683 | if (!outfile) | |
684 | error("could not open '%s'", outfilename); | |
dc99065b | 685 | load_elf(filename, outfile, do_print_enum); |
367e86e8 FB |
686 | fclose(outfile); |
687 | return 0; | |
688 | } |