]>
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> | |
367e86e8 FB |
25 | #include <unistd.h> |
26 | #include <fcntl.h> | |
27 | ||
ce11fedc FB |
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 | |
367e86e8 | 53 | |
ce11fedc FB |
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 | ||
efdea7bf FB |
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 | ||
d014c98c FB |
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 | ||
ce11fedc FB |
82 | #else |
83 | #error unsupported CPU - please update the code | |
84 | #endif | |
85 | ||
efdea7bf FB |
86 | #include "elf.h" |
87 | ||
ce11fedc FB |
88 | #if ELF_CLASS == ELFCLASS32 |
89 | typedef int32_t host_long; | |
90 | typedef uint32_t host_ulong; | |
efdea7bf | 91 | #define swabls(x) swab32s(x) |
ce11fedc FB |
92 | #else |
93 | typedef int64_t host_long; | |
94 | typedef uint64_t host_ulong; | |
efdea7bf | 95 | #define swabls(x) swab64s(x) |
fb3e5849 FB |
96 | #endif |
97 | ||
ce11fedc FB |
98 | #include "thunk.h" |
99 | ||
367e86e8 | 100 | /* all dynamically generated functions begin with this code */ |
dc99065b | 101 | #define OP_PREFIX "op_" |
367e86e8 | 102 | |
ce11fedc | 103 | int elf_must_swap(struct elfhdr *h) |
367e86e8 FB |
104 | { |
105 | union { | |
106 | uint32_t i; | |
107 | uint8_t b[4]; | |
108 | } swaptest; | |
109 | ||
110 | swaptest.i = 1; | |
111 | return (h->e_ident[EI_DATA] == ELFDATA2MSB) != | |
112 | (swaptest.b[0] == 0); | |
113 | } | |
114 | ||
115 | void swab16s(uint16_t *p) | |
116 | { | |
117 | *p = bswap16(*p); | |
118 | } | |
119 | ||
120 | void swab32s(uint32_t *p) | |
121 | { | |
122 | *p = bswap32(*p); | |
123 | } | |
124 | ||
ce11fedc | 125 | void swab64s(uint64_t *p) |
367e86e8 FB |
126 | { |
127 | *p = bswap64(*p); | |
128 | } | |
129 | ||
ce11fedc | 130 | void elf_swap_ehdr(struct elfhdr *h) |
367e86e8 FB |
131 | { |
132 | swab16s(&h->e_type); /* Object file type */ | |
133 | swab16s(&h-> e_machine); /* Architecture */ | |
134 | swab32s(&h-> e_version); /* Object file version */ | |
ce11fedc FB |
135 | swabls(&h-> e_entry); /* Entry point virtual address */ |
136 | swabls(&h-> e_phoff); /* Program header table file offset */ | |
137 | swabls(&h-> e_shoff); /* Section header table file offset */ | |
367e86e8 FB |
138 | swab32s(&h-> e_flags); /* Processor-specific flags */ |
139 | swab16s(&h-> e_ehsize); /* ELF header size in bytes */ | |
140 | swab16s(&h-> e_phentsize); /* Program header table entry size */ | |
141 | swab16s(&h-> e_phnum); /* Program header table entry count */ | |
142 | swab16s(&h-> e_shentsize); /* Section header table entry size */ | |
143 | swab16s(&h-> e_shnum); /* Section header table entry count */ | |
144 | swab16s(&h-> e_shstrndx); /* Section header string table index */ | |
145 | } | |
146 | ||
ce11fedc | 147 | void elf_swap_shdr(struct elf_shdr *h) |
367e86e8 FB |
148 | { |
149 | swab32s(&h-> sh_name); /* Section name (string tbl index) */ | |
150 | swab32s(&h-> sh_type); /* Section type */ | |
ce11fedc FB |
151 | swabls(&h-> sh_flags); /* Section flags */ |
152 | swabls(&h-> sh_addr); /* Section virtual addr at execution */ | |
153 | swabls(&h-> sh_offset); /* Section file offset */ | |
154 | swabls(&h-> sh_size); /* Section size in bytes */ | |
367e86e8 FB |
155 | swab32s(&h-> sh_link); /* Link to another section */ |
156 | swab32s(&h-> sh_info); /* Additional section information */ | |
ce11fedc FB |
157 | swabls(&h-> sh_addralign); /* Section alignment */ |
158 | swabls(&h-> sh_entsize); /* Entry size if section holds table */ | |
367e86e8 FB |
159 | } |
160 | ||
ce11fedc | 161 | void elf_swap_phdr(struct elf_phdr *h) |
367e86e8 FB |
162 | { |
163 | swab32s(&h->p_type); /* Segment type */ | |
ce11fedc FB |
164 | swabls(&h->p_offset); /* Segment file offset */ |
165 | swabls(&h->p_vaddr); /* Segment virtual address */ | |
166 | swabls(&h->p_paddr); /* Segment physical address */ | |
167 | swabls(&h->p_filesz); /* Segment size in file */ | |
168 | swabls(&h->p_memsz); /* Segment size in memory */ | |
367e86e8 | 169 | swab32s(&h->p_flags); /* Segment flags */ |
ce11fedc | 170 | swabls(&h->p_align); /* Segment alignment */ |
367e86e8 FB |
171 | } |
172 | ||
173 | int do_swap; | |
367e86e8 FB |
174 | |
175 | uint16_t get16(uint16_t *p) | |
176 | { | |
177 | uint16_t val; | |
178 | val = *p; | |
179 | if (do_swap) | |
180 | val = bswap16(val); | |
181 | return val; | |
182 | } | |
183 | ||
184 | uint32_t get32(uint32_t *p) | |
185 | { | |
186 | uint32_t val; | |
187 | val = *p; | |
188 | if (do_swap) | |
189 | val = bswap32(val); | |
190 | return val; | |
191 | } | |
192 | ||
193 | void put16(uint16_t *p, uint16_t val) | |
194 | { | |
195 | if (do_swap) | |
196 | val = bswap16(val); | |
197 | *p = val; | |
198 | } | |
199 | ||
200 | void put32(uint32_t *p, uint32_t val) | |
201 | { | |
202 | if (do_swap) | |
203 | val = bswap32(val); | |
204 | *p = val; | |
205 | } | |
206 | ||
efdea7bf | 207 | void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...) |
367e86e8 FB |
208 | { |
209 | va_list ap; | |
210 | va_start(ap, fmt); | |
211 | fprintf(stderr, "dyngen: "); | |
212 | vfprintf(stderr, fmt, ap); | |
213 | fprintf(stderr, "\n"); | |
214 | va_end(ap); | |
215 | exit(1); | |
216 | } | |
217 | ||
218 | ||
ce11fedc FB |
219 | struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr, |
220 | const char *name) | |
367e86e8 FB |
221 | { |
222 | int i; | |
223 | const char *shname; | |
ce11fedc | 224 | struct elf_shdr *sec; |
367e86e8 FB |
225 | |
226 | for(i = 0; i < shnum; i++) { | |
227 | sec = &shdr[i]; | |
228 | if (!sec->sh_name) | |
229 | continue; | |
230 | shname = shstr + sec->sh_name; | |
231 | if (!strcmp(shname, name)) | |
232 | return sec; | |
233 | } | |
234 | return NULL; | |
235 | } | |
236 | ||
237 | void *load_data(int fd, long offset, unsigned int size) | |
238 | { | |
239 | char *data; | |
240 | ||
241 | data = malloc(size); | |
242 | if (!data) | |
243 | return NULL; | |
244 | lseek(fd, offset, SEEK_SET); | |
245 | if (read(fd, data, size) != size) { | |
246 | free(data); | |
247 | return NULL; | |
248 | } | |
249 | return data; | |
250 | } | |
251 | ||
252 | int strstart(const char *str, const char *val, const char **ptr) | |
253 | { | |
254 | const char *p, *q; | |
255 | p = str; | |
256 | q = val; | |
257 | while (*q != '\0') { | |
258 | if (*p != *q) | |
259 | return 0; | |
260 | p++; | |
261 | q++; | |
262 | } | |
263 | if (ptr) | |
264 | *ptr = p; | |
265 | return 1; | |
266 | } | |
267 | ||
268 | #define MAX_ARGS 3 | |
269 | ||
270 | /* generate op code */ | |
ce11fedc FB |
271 | void gen_code(const char *name, host_ulong offset, host_ulong size, |
272 | FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs, int reloc_sh_type, | |
273 | ElfW(Sym) *symtab, char *strtab, int gen_switch) | |
367e86e8 FB |
274 | { |
275 | int copy_size = 0; | |
276 | uint8_t *p_start, *p_end; | |
ce11fedc | 277 | int nb_args, i, n; |
367e86e8 FB |
278 | uint8_t args_present[MAX_ARGS]; |
279 | const char *sym_name, *p; | |
ce11fedc | 280 | ELF_RELOC *rel; |
367e86e8 FB |
281 | |
282 | /* compute exact size excluding return instruction */ | |
283 | p_start = text + offset; | |
284 | p_end = p_start + size; | |
ce11fedc | 285 | switch(ELF_ARCH) { |
367e86e8 FB |
286 | case EM_386: |
287 | { | |
288 | uint8_t *p; | |
289 | p = p_end - 1; | |
367e86e8 FB |
290 | if (p == p_start) |
291 | error("empty code for %s", name); | |
4b74fe1f FB |
292 | if (p[0] != 0xc3) |
293 | error("ret expected at the end of %s", name); | |
367e86e8 FB |
294 | copy_size = p - p_start; |
295 | } | |
296 | break; | |
297 | case EM_PPC: | |
298 | { | |
299 | uint8_t *p; | |
300 | p = (void *)(p_end - 4); | |
367e86e8 FB |
301 | if (p == p_start) |
302 | error("empty code for %s", name); | |
04369ff2 FB |
303 | if (get32((uint32_t *)p) != 0x4e800020) |
304 | error("blr expected at the end of %s", name); | |
367e86e8 FB |
305 | copy_size = p - p_start; |
306 | } | |
307 | break; | |
fb3e5849 FB |
308 | case EM_S390: |
309 | { | |
310 | uint8_t *p; | |
311 | p = (void *)(p_end - 2); | |
312 | if (p == p_start) | |
313 | error("empty code for %s", name); | |
314 | if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4) | |
efdea7bf | 315 | error("br %%r14 expected at the end of %s", name); |
fb3e5849 FB |
316 | copy_size = p - p_start; |
317 | } | |
318 | break; | |
efdea7bf FB |
319 | case EM_ALPHA: |
320 | { | |
321 | uint8_t *p; | |
322 | p = p_end - 4; | |
323 | if (p == p_start) | |
324 | error("empty code for %s", name); | |
325 | if (get32((uint32_t *)p) != 0x6bfa8001) | |
326 | error("ret expected at the end of %s", name); | |
327 | copy_size = p - p_start; | |
328 | } | |
329 | break; | |
330 | case EM_IA_64: | |
331 | { | |
332 | uint8_t *p; | |
333 | p = (void *)(p_end - 4); | |
334 | if (p == p_start) | |
335 | error("empty code for %s", name); | |
336 | /* br.ret.sptk.many b0;; */ | |
337 | /* 08 00 84 00 */ | |
338 | if (get32((uint32_t *)p) != 0x00840008) | |
339 | error("br.ret.sptk.many b0;; expected at the end of %s", name); | |
340 | copy_size = p - p_start; | |
341 | } | |
342 | break; | |
d014c98c FB |
343 | case EM_SPARC: |
344 | case EM_SPARC32PLUS: | |
345 | { | |
346 | uint8_t *p; | |
347 | p = (void *)(p_end - 8); | |
348 | if (p <= p_start) | |
349 | error("empty code for %s", name); | |
350 | if (get32((uint32_t *)(p_start + 0x0)) != 0x9de3bf98) | |
351 | error("save %%sp,-104,%%sp expected at the start of %s " | |
352 | "found [%08x]", | |
353 | name, get32((uint32_t *)(p_start + 0x0))); | |
354 | if (get32((uint32_t *)(p + 0x0)) != 0x81c7e008 || | |
355 | get32((uint32_t *)(p + 0x4)) != 0x81e80000) | |
356 | error("ret; restore; expected at the end of %s found [%08x:%08x]", | |
357 | name, | |
358 | get32((uint32_t *)(p + 0x0)), | |
359 | get32((uint32_t *)(p + 0x4))); | |
360 | ||
361 | copy_size = p - p_start; | |
362 | } | |
363 | break; | |
364 | case EM_SPARCV9: | |
365 | { | |
366 | uint8_t *p; | |
367 | p = (void *)(p_end - 8); | |
368 | if (p <= p_start) | |
369 | error("empty code for %s", name); | |
370 | if (get32((uint32_t *)(p_start + 0x0)) != 0x9de3bf40) | |
371 | error("save %%sp,-192,%%sp expected at the start of %s " | |
372 | "found [%08x]", | |
373 | name, get32((uint32_t *)(p_start + 0x0))); | |
374 | if (get32((uint32_t *)(p + 0x0)) != 0x81cfe008 || | |
375 | get32((uint32_t *)(p + 0x4)) != 0x01000000) | |
376 | error("rett %%i7+8; nop; expected at the end of %s " | |
377 | "found [%08x:%08x]", | |
378 | name, | |
379 | get32((uint32_t *)(p + 0x0)), | |
380 | get32((uint32_t *)(p + 0x4))); | |
381 | copy_size = p - p_start; | |
382 | } | |
383 | break; | |
efdea7bf FB |
384 | default: |
385 | error("unknown ELF architecture"); | |
367e86e8 FB |
386 | } |
387 | ||
388 | /* compute the number of arguments by looking at the relocations */ | |
389 | for(i = 0;i < MAX_ARGS; i++) | |
390 | args_present[i] = 0; | |
391 | ||
ce11fedc FB |
392 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
393 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
394 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; | |
395 | if (strstart(sym_name, "__op_param", &p)) { | |
396 | n = strtoul(p, NULL, 10); | |
397 | if (n >= MAX_ARGS) | |
398 | error("too many arguments in %s", name); | |
399 | args_present[n - 1] = 1; | |
367e86e8 FB |
400 | } |
401 | } | |
402 | } | |
403 | ||
404 | nb_args = 0; | |
405 | while (nb_args < MAX_ARGS && args_present[nb_args]) | |
406 | nb_args++; | |
407 | for(i = nb_args; i < MAX_ARGS; i++) { | |
408 | if (args_present[i]) | |
409 | error("inconsistent argument numbering in %s", name); | |
410 | } | |
411 | ||
0ea00c9a FB |
412 | if (gen_switch == 2) { |
413 | fprintf(outfile, "DEF(%s, %d)\n", name + 3, nb_args); | |
414 | } else if (gen_switch == 1) { | |
dc99065b FB |
415 | |
416 | /* output C code */ | |
417 | fprintf(outfile, "case INDEX_%s: {\n", name); | |
418 | if (nb_args > 0) { | |
419 | fprintf(outfile, " long "); | |
420 | for(i = 0; i < nb_args; i++) { | |
421 | if (i != 0) | |
422 | fprintf(outfile, ", "); | |
423 | fprintf(outfile, "param%d", i + 1); | |
424 | } | |
425 | fprintf(outfile, ";\n"); | |
367e86e8 | 426 | } |
dc99065b FB |
427 | fprintf(outfile, " extern void %s();\n", name); |
428 | ||
ce11fedc FB |
429 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
430 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
efdea7bf | 431 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
74c95119 | 432 | if (*sym_name && !strstart(sym_name, "__op_param", &p)) { |
d014c98c FB |
433 | #if defined(HOST_SPARC) |
434 | if (sym_name[0] == '.') { | |
435 | fprintf(outfile, | |
436 | "extern char __dot_%s __asm__(\"%s\");\n", | |
437 | sym_name+1, sym_name); | |
438 | continue; | |
439 | } | |
440 | #endif | |
ce11fedc | 441 | fprintf(outfile, "extern char %s;\n", sym_name); |
dc99065b FB |
442 | } |
443 | } | |
444 | } | |
445 | ||
446 | fprintf(outfile, " memcpy(gen_code_ptr, &%s, %d);\n", name, copy_size); | |
447 | for(i = 0; i < nb_args; i++) { | |
448 | fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1); | |
449 | } | |
450 | ||
451 | /* patch relocations */ | |
ce11fedc | 452 | #if defined(HOST_I386) |
dc99065b | 453 | { |
dc99065b FB |
454 | char name[256]; |
455 | int type; | |
ce11fedc | 456 | int addend; |
dc99065b | 457 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
367e86e8 | 458 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { |
efdea7bf | 459 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
367e86e8 FB |
460 | if (strstart(sym_name, "__op_param", &p)) { |
461 | snprintf(name, sizeof(name), "param%s", p); | |
462 | } else { | |
463 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
464 | } | |
465 | type = ELF32_R_TYPE(rel->r_info); | |
466 | addend = get32((uint32_t *)(text + rel->r_offset)); | |
467 | switch(type) { | |
468 | case R_386_32: | |
ce11fedc | 469 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", |
367e86e8 FB |
470 | rel->r_offset - offset, name, addend); |
471 | break; | |
472 | case R_386_PC32: | |
ce11fedc | 473 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n", |
367e86e8 FB |
474 | rel->r_offset - offset, name, rel->r_offset - offset, addend); |
475 | break; | |
476 | default: | |
477 | error("unsupported i386 relocation (%d)", type); | |
478 | } | |
479 | } | |
dc99065b FB |
480 | } |
481 | } | |
ce11fedc | 482 | #elif defined(HOST_PPC) |
04369ff2 | 483 | { |
04369ff2 FB |
484 | char name[256]; |
485 | int type; | |
ce11fedc | 486 | int addend; |
04369ff2 FB |
487 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
488 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
efdea7bf | 489 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
04369ff2 FB |
490 | if (strstart(sym_name, "__op_param", &p)) { |
491 | snprintf(name, sizeof(name), "param%s", p); | |
492 | } else { | |
493 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
494 | } | |
495 | type = ELF32_R_TYPE(rel->r_info); | |
496 | addend = rel->r_addend; | |
497 | switch(type) { | |
498 | case R_PPC_ADDR32: | |
ce11fedc | 499 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", |
04369ff2 FB |
500 | rel->r_offset - offset, name, addend); |
501 | break; | |
502 | case R_PPC_ADDR16_LO: | |
ce11fedc | 503 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n", |
04369ff2 FB |
504 | rel->r_offset - offset, name, addend); |
505 | break; | |
506 | case R_PPC_ADDR16_HI: | |
ce11fedc | 507 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n", |
04369ff2 FB |
508 | rel->r_offset - offset, name, addend); |
509 | break; | |
510 | case R_PPC_ADDR16_HA: | |
ce11fedc | 511 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n", |
04369ff2 FB |
512 | rel->r_offset - offset, name, addend); |
513 | break; | |
514 | case R_PPC_REL24: | |
515 | /* warning: must be at 32 MB distancy */ | |
ce11fedc | 516 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n", |
04369ff2 FB |
517 | rel->r_offset - offset, rel->r_offset - offset, name, rel->r_offset - offset, addend); |
518 | break; | |
519 | default: | |
520 | error("unsupported powerpc relocation (%d)", type); | |
521 | } | |
522 | } | |
523 | } | |
524 | } | |
ce11fedc | 525 | #elif defined(HOST_S390) |
fb3e5849 | 526 | { |
fb3e5849 FB |
527 | char name[256]; |
528 | int type; | |
ce11fedc | 529 | int addend; |
fb3e5849 FB |
530 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
531 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
efdea7bf | 532 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
fb3e5849 FB |
533 | if (strstart(sym_name, "__op_param", &p)) { |
534 | snprintf(name, sizeof(name), "param%s", p); | |
535 | } else { | |
536 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
537 | } | |
538 | type = ELF32_R_TYPE(rel->r_info); | |
539 | addend = rel->r_addend; | |
540 | switch(type) { | |
541 | case R_390_32: | |
ce11fedc | 542 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", |
fb3e5849 FB |
543 | rel->r_offset - offset, name, addend); |
544 | break; | |
545 | case R_390_16: | |
ce11fedc | 546 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n", |
fb3e5849 FB |
547 | rel->r_offset - offset, name, addend); |
548 | break; | |
549 | case R_390_8: | |
ce11fedc | 550 | fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n", |
fb3e5849 FB |
551 | rel->r_offset - offset, name, addend); |
552 | break; | |
553 | default: | |
554 | error("unsupported s390 relocation (%d)", type); | |
555 | } | |
556 | } | |
557 | } | |
558 | } | |
efdea7bf FB |
559 | #elif defined(HOST_ALPHA) |
560 | { | |
561 | for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) { | |
562 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
563 | int type; | |
74c95119 | 564 | |
efdea7bf | 565 | type = ELF64_R_TYPE(rel->r_info); |
74c95119 | 566 | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
efdea7bf FB |
567 | switch (type) { |
568 | case R_ALPHA_GPDISP: | |
74c95119 FB |
569 | /* The gp is just 32 bit, and never changes, so it's easiest to emit it |
570 | as an immediate instead of constructing it from the pv or ra. */ | |
571 | fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n", | |
efdea7bf | 572 | rel->r_offset - offset); |
74c95119 FB |
573 | fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n", |
574 | rel->r_offset - offset + rel->r_addend); | |
efdea7bf FB |
575 | break; |
576 | case R_ALPHA_LITUSE: | |
577 | /* jsr to literal hint. Could be used to optimize to bsr. Ignore for | |
578 | now, since some called functions (libc) need pv to be set up. */ | |
579 | break; | |
580 | case R_ALPHA_HINT: | |
581 | /* Branch target prediction hint. Ignore for now. Should be already | |
582 | correct for in-function jumps. */ | |
583 | break; | |
584 | case R_ALPHA_LITERAL: | |
74c95119 FB |
585 | /* Load a literal from the GOT relative to the gp. Since there's only a |
586 | single gp, nothing is to be done. */ | |
587 | break; | |
588 | case R_ALPHA_GPRELHIGH: | |
589 | /* Handle fake relocations against __op_param symbol. Need to emit the | |
590 | high part of the immediate value instead. Other symbols need no | |
591 | special treatment. */ | |
592 | if (strstart(sym_name, "__op_param", &p)) | |
593 | fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n", | |
594 | rel->r_offset - offset, p); | |
595 | break; | |
596 | case R_ALPHA_GPRELLOW: | |
597 | if (strstart(sym_name, "__op_param", &p)) | |
598 | fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n", | |
599 | rel->r_offset - offset, p); | |
600 | break; | |
601 | case R_ALPHA_BRSGP: | |
602 | /* PC-relative jump. Tweak offset to skip the two instructions that try to | |
603 | set up the gp from the pv. */ | |
604 | fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld) + 4);\n", | |
605 | rel->r_offset - offset, sym_name, rel->r_offset - offset); | |
efdea7bf FB |
606 | break; |
607 | default: | |
608 | error("unsupported Alpha relocation (%d)", type); | |
609 | } | |
610 | } | |
611 | } | |
612 | } | |
613 | #elif defined(HOST_IA64) | |
614 | { | |
615 | char name[256]; | |
616 | int type; | |
617 | int addend; | |
618 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
619 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
620 | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; | |
621 | if (strstart(sym_name, "__op_param", &p)) { | |
622 | snprintf(name, sizeof(name), "param%s", p); | |
623 | } else { | |
624 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
625 | } | |
626 | type = ELF64_R_TYPE(rel->r_info); | |
627 | addend = rel->r_addend; | |
628 | switch(type) { | |
629 | case R_IA64_LTOFF22: | |
630 | error("must implemnt R_IA64_LTOFF22 relocation"); | |
631 | case R_IA64_PCREL21B: | |
632 | error("must implemnt R_IA64_PCREL21B relocation"); | |
633 | default: | |
634 | error("unsupported ia64 relocation (%d)", type); | |
635 | } | |
636 | } | |
637 | } | |
638 | } | |
d014c98c FB |
639 | #elif defined(HOST_SPARC) |
640 | { | |
641 | char name[256]; | |
642 | int type; | |
643 | int addend; | |
644 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
645 | if (rel->r_offset >= offset && rel->r_offset < offset + copy_size) { | |
646 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; | |
647 | if (strstart(sym_name, "__op_param", &p)) { | |
648 | snprintf(name, sizeof(name), "param%s", p); | |
649 | } else { | |
650 | if (sym_name[0] == '.') | |
651 | snprintf(name, sizeof(name), | |
652 | "(long)(&__dot_%s)", | |
653 | sym_name + 1); | |
654 | else | |
655 | snprintf(name, sizeof(name), | |
656 | "(long)(&%s)", sym_name); | |
657 | } | |
658 | type = ELF32_R_TYPE(rel->r_info); | |
659 | addend = rel->r_addend; | |
660 | switch(type) { | |
661 | case R_SPARC_32: | |
662 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", | |
663 | rel->r_offset - offset, name, addend); | |
664 | break; | |
665 | case R_SPARC_HI22: | |
666 | fprintf(outfile, | |
667 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
668 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
669 | " & ~0x3fffff) " | |
670 | " | ((%s + %d) & 0x3fffff);\n", | |
671 | rel->r_offset - offset, | |
672 | rel->r_offset - offset, | |
673 | name, addend); | |
674 | break; | |
675 | case R_SPARC_LO10: | |
676 | fprintf(outfile, | |
677 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
678 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
679 | " & ~0x3ff) " | |
680 | " | ((%s + %d) & 0x3ff);\n", | |
681 | rel->r_offset - offset, | |
682 | rel->r_offset - offset, | |
683 | name, addend); | |
684 | break; | |
685 | case R_SPARC_WDISP30: | |
686 | fprintf(outfile, | |
687 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
688 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
689 | " & ~0x3fffffff) " | |
690 | " | ((((%s + %d) - (long)gen_code_ptr)>>2) " | |
691 | " & 0x3fffffff);\n", | |
692 | rel->r_offset - offset, | |
693 | rel->r_offset - offset, | |
694 | name, addend); | |
695 | break; | |
696 | default: | |
697 | error("unsupported sparc relocation (%d)", type); | |
698 | } | |
699 | } | |
700 | } | |
701 | } | |
702 | #elif defined(HOST_SPARC64) | |
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 >= offset && rel->r_offset < offset + copy_size) { | |
709 | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; | |
710 | if (strstart(sym_name, "__op_param", &p)) { | |
711 | snprintf(name, sizeof(name), "param%s", p); | |
712 | } else { | |
713 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
714 | } | |
715 | type = ELF64_R_TYPE(rel->r_info); | |
716 | addend = rel->r_addend; | |
717 | switch(type) { | |
718 | case R_SPARC_32: | |
719 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", | |
720 | rel->r_offset - offset, name, addend); | |
721 | break; | |
722 | case R_SPARC_HI22: | |
723 | fprintf(outfile, | |
724 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
725 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
726 | " & ~0x3fffff) " | |
727 | " | ((%s + %d) & 0x3fffff);\n", | |
728 | rel->r_offset - offset, | |
729 | rel->r_offset - offset, | |
730 | name, addend); | |
731 | break; | |
732 | case R_SPARC_LO10: | |
733 | fprintf(outfile, | |
734 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
735 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
736 | " & ~0x3ff) " | |
737 | " | ((%s + %d) & 0x3ff);\n", | |
738 | rel->r_offset - offset, | |
739 | rel->r_offset - offset, | |
740 | name, addend); | |
741 | break; | |
742 | case R_SPARC_WDISP30: | |
743 | fprintf(outfile, | |
744 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
745 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
746 | " & ~0x3fffffff) " | |
747 | " | ((((%s + %d) - (long)gen_code_ptr)>>2) " | |
748 | " & 0x3fffffff);\n", | |
749 | rel->r_offset - offset, | |
750 | rel->r_offset - offset, | |
751 | name, addend); | |
752 | break; | |
753 | default: | |
754 | error("unsupported sparc64 relocation (%d)", type); | |
755 | } | |
756 | } | |
757 | } | |
758 | } | |
ce11fedc FB |
759 | #else |
760 | #error unsupported CPU | |
761 | #endif | |
dc99065b FB |
762 | fprintf(outfile, " gen_code_ptr += %d;\n", copy_size); |
763 | fprintf(outfile, "}\n"); | |
764 | fprintf(outfile, "break;\n\n"); | |
765 | } else { | |
766 | fprintf(outfile, "static inline void gen_%s(", name); | |
767 | if (nb_args == 0) { | |
768 | fprintf(outfile, "void"); | |
769 | } else { | |
770 | for(i = 0; i < nb_args; i++) { | |
771 | if (i != 0) | |
772 | fprintf(outfile, ", "); | |
773 | fprintf(outfile, "long param%d", i + 1); | |
367e86e8 FB |
774 | } |
775 | } | |
dc99065b FB |
776 | fprintf(outfile, ")\n"); |
777 | fprintf(outfile, "{\n"); | |
778 | for(i = 0; i < nb_args; i++) { | |
779 | fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1); | |
780 | } | |
781 | fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name); | |
782 | fprintf(outfile, "}\n\n"); | |
367e86e8 | 783 | } |
367e86e8 FB |
784 | } |
785 | ||
786 | /* load an elf object file */ | |
dc99065b | 787 | int load_elf(const char *filename, FILE *outfile, int do_print_enum) |
367e86e8 FB |
788 | { |
789 | int fd; | |
ce11fedc FB |
790 | struct elfhdr ehdr; |
791 | struct elf_shdr *sec, *shdr, *symtab_sec, *strtab_sec, *text_sec; | |
367e86e8 | 792 | int i, j, nb_syms; |
ce11fedc | 793 | ElfW(Sym) *symtab, *sym; |
367e86e8 FB |
794 | char *shstr, *strtab; |
795 | uint8_t *text; | |
796 | void *relocs; | |
797 | int nb_relocs, reloc_sh_type; | |
798 | ||
799 | fd = open(filename, O_RDONLY); | |
800 | if (fd < 0) | |
801 | error("can't open file '%s'", filename); | |
802 | ||
803 | /* Read ELF header. */ | |
804 | if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr)) | |
805 | error("unable to read file header"); | |
806 | ||
807 | /* Check ELF identification. */ | |
808 | if (ehdr.e_ident[EI_MAG0] != ELFMAG0 | |
809 | || ehdr.e_ident[EI_MAG1] != ELFMAG1 | |
810 | || ehdr.e_ident[EI_MAG2] != ELFMAG2 | |
811 | || ehdr.e_ident[EI_MAG3] != ELFMAG3 | |
367e86e8 FB |
812 | || ehdr.e_ident[EI_VERSION] != EV_CURRENT) { |
813 | error("bad ELF header"); | |
814 | } | |
815 | ||
816 | do_swap = elf_must_swap(&ehdr); | |
817 | if (do_swap) | |
818 | elf_swap_ehdr(&ehdr); | |
ce11fedc FB |
819 | if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) |
820 | error("Unsupported ELF class"); | |
367e86e8 FB |
821 | if (ehdr.e_type != ET_REL) |
822 | error("ELF object file expected"); | |
823 | if (ehdr.e_version != EV_CURRENT) | |
824 | error("Invalid ELF version"); | |
ce11fedc FB |
825 | if (!elf_check_arch(ehdr.e_machine)) |
826 | error("Unsupported CPU (e_machine=%d)", ehdr.e_machine); | |
367e86e8 FB |
827 | |
828 | /* read section headers */ | |
ce11fedc | 829 | shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr)); |
367e86e8 FB |
830 | if (do_swap) { |
831 | for(i = 0; i < ehdr.e_shnum; i++) { | |
832 | elf_swap_shdr(&shdr[i]); | |
833 | } | |
834 | } | |
835 | ||
836 | sec = &shdr[ehdr.e_shstrndx]; | |
837 | shstr = load_data(fd, sec->sh_offset, sec->sh_size); | |
838 | ||
839 | /* text section */ | |
840 | ||
841 | text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text"); | |
842 | if (!text_sec) | |
843 | error("could not find .text section"); | |
844 | text = load_data(fd, text_sec->sh_offset, text_sec->sh_size); | |
845 | ||
846 | /* find text relocations, if any */ | |
847 | nb_relocs = 0; | |
848 | relocs = NULL; | |
849 | reloc_sh_type = 0; | |
850 | for(i = 0; i < ehdr.e_shnum; i++) { | |
851 | sec = &shdr[i]; | |
852 | if ((sec->sh_type == SHT_REL || sec->sh_type == SHT_RELA) && | |
853 | sec->sh_info == (text_sec - shdr)) { | |
854 | reloc_sh_type = sec->sh_type; | |
855 | relocs = load_data(fd, sec->sh_offset, sec->sh_size); | |
856 | nb_relocs = sec->sh_size / sec->sh_entsize; | |
857 | if (do_swap) { | |
858 | if (sec->sh_type == SHT_REL) { | |
efdea7bf | 859 | ElfW(Rel) *rel = relocs; |
367e86e8 | 860 | for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) { |
efdea7bf FB |
861 | swabls(&rel->r_offset); |
862 | swabls(&rel->r_info); | |
367e86e8 FB |
863 | } |
864 | } else { | |
efdea7bf | 865 | ElfW(Rela) *rel = relocs; |
367e86e8 | 866 | for(j = 0, rel = relocs; j < nb_relocs; j++, rel++) { |
efdea7bf FB |
867 | swabls(&rel->r_offset); |
868 | swabls(&rel->r_info); | |
869 | swabls(&rel->r_addend); | |
367e86e8 FB |
870 | } |
871 | } | |
872 | } | |
873 | break; | |
874 | } | |
875 | } | |
876 | ||
877 | symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab"); | |
878 | if (!symtab_sec) | |
879 | error("could not find .symtab section"); | |
880 | strtab_sec = &shdr[symtab_sec->sh_link]; | |
881 | ||
882 | symtab = load_data(fd, symtab_sec->sh_offset, symtab_sec->sh_size); | |
883 | strtab = load_data(fd, strtab_sec->sh_offset, strtab_sec->sh_size); | |
884 | ||
efdea7bf | 885 | nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym)); |
367e86e8 FB |
886 | if (do_swap) { |
887 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { | |
888 | swab32s(&sym->st_name); | |
ce11fedc FB |
889 | swabls(&sym->st_value); |
890 | swabls(&sym->st_size); | |
367e86e8 FB |
891 | swab16s(&sym->st_shndx); |
892 | } | |
893 | } | |
894 | ||
dc99065b | 895 | if (do_print_enum) { |
0ea00c9a | 896 | fprintf(outfile, "DEF(end, 0)\n"); |
dc99065b FB |
897 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
898 | const char *name, *p; | |
899 | name = strtab + sym->st_name; | |
900 | if (strstart(name, OP_PREFIX, &p)) { | |
0ea00c9a FB |
901 | gen_code(name, sym->st_value, sym->st_size, outfile, |
902 | text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 2); | |
dc99065b FB |
903 | } |
904 | } | |
905 | } else { | |
906 | /* generate big code generation switch */ | |
efdea7bf | 907 | #ifdef HOST_ALPHA |
74c95119 FB |
908 | fprintf(outfile, |
909 | "register int gp asm(\"$29\");\n" | |
910 | "static inline void immediate_ldah(void *p, int val) {\n" | |
911 | " uint32_t *dest = p;\n" | |
912 | " long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff;\n" | |
913 | "\n" | |
914 | " *dest &= ~0xffff;\n" | |
915 | " *dest |= high;\n" | |
916 | " *dest |= 31 << 16;\n" | |
917 | "}\n" | |
918 | "static inline void immediate_lda(void *dest, int val) {\n" | |
919 | " *(uint16_t *) dest = val;\n" | |
920 | "}\n" | |
921 | "void fix_bsr(void *p, int offset) {\n" | |
922 | " uint32_t *dest = p;\n" | |
923 | " *dest &= ~((1 << 21) - 1);\n" | |
924 | " *dest |= (offset >> 2) & ((1 << 21) - 1);\n" | |
925 | "}\n"); | |
efdea7bf | 926 | #endif |
dc99065b FB |
927 | fprintf(outfile, |
928 | "int dyngen_code(uint8_t *gen_code_buf,\n" | |
929 | " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n" | |
930 | "{\n" | |
931 | " uint8_t *gen_code_ptr;\n" | |
932 | " const uint16_t *opc_ptr;\n" | |
933 | " const uint32_t *opparam_ptr;\n" | |
934 | " gen_code_ptr = gen_code_buf;\n" | |
935 | " opc_ptr = opc_buf;\n" | |
936 | " opparam_ptr = opparam_buf;\n" | |
937 | " for(;;) {\n" | |
938 | " switch(*opc_ptr++) {\n" | |
939 | ); | |
367e86e8 | 940 | |
dc99065b FB |
941 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
942 | const char *name; | |
943 | name = strtab + sym->st_name; | |
944 | if (strstart(name, OP_PREFIX, NULL)) { | |
367e86e8 | 945 | #if 0 |
dc99065b FB |
946 | printf("%4d: %s pos=0x%08x len=%d\n", |
947 | i, name, sym->st_value, sym->st_size); | |
367e86e8 | 948 | #endif |
dc99065b FB |
949 | if (sym->st_shndx != (text_sec - shdr)) |
950 | error("invalid section for opcode (0x%x)", sym->st_shndx); | |
951 | gen_code(name, sym->st_value, sym->st_size, outfile, | |
952 | text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 1); | |
953 | } | |
954 | } | |
955 | ||
956 | fprintf(outfile, | |
957 | " default:\n" | |
958 | " goto the_end;\n" | |
959 | " }\n" | |
960 | " }\n" | |
961 | " the_end:\n" | |
962 | ); | |
963 | ||
964 | /* generate a return */ | |
ce11fedc | 965 | switch(ELF_ARCH) { |
dc99065b FB |
966 | case EM_386: |
967 | fprintf(outfile, "*gen_code_ptr++ = 0xc3; /* ret */\n"); | |
968 | break; | |
04369ff2 FB |
969 | case EM_PPC: |
970 | fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x4e800020; /* blr */\n"); | |
971 | break; | |
fb3e5849 FB |
972 | case EM_S390: |
973 | fprintf(outfile, "*((uint16_t *)gen_code_ptr)++ = 0x07fe; /* br %%r14 */\n"); | |
974 | break; | |
efdea7bf FB |
975 | case EM_ALPHA: |
976 | fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x6bfa8001; /* ret */\n"); | |
977 | break; | |
978 | case EM_IA_64: | |
979 | fprintf(outfile, "*((uint32_t *)gen_code_ptr)++ = 0x00840008; /* br.ret.sptk.many b0;; */\n"); | |
980 | break; | |
d014c98c FB |
981 | case EM_SPARC: |
982 | case EM_SPARC32PLUS: | |
983 | case EM_SPARCV9: | |
984 | /* Fill the delay slot. */ | |
985 | fprintf(outfile, "*((uint32_t *)gen_code_ptr) = *((uint32_t *)gen_code_ptr - 1); /* delay slot */\n"); | |
986 | fprintf(outfile, "*((uint32_t *)gen_code_ptr - 1) = 0x81c3e008; /* retl */\n"); | |
987 | fprintf(outfile, "gen_code_ptr++;\n"); | |
988 | break; | |
efdea7bf FB |
989 | default: |
990 | error("unknown ELF architecture"); | |
dc99065b FB |
991 | } |
992 | ||
993 | fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n"); | |
994 | fprintf(outfile, "}\n\n"); | |
995 | ||
996 | /* generate gen_xxx functions */ | |
997 | /* XXX: suppress the use of these functions to simplify code */ | |
998 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { | |
999 | const char *name; | |
1000 | name = strtab + sym->st_name; | |
1001 | if (strstart(name, OP_PREFIX, NULL)) { | |
1002 | if (sym->st_shndx != (text_sec - shdr)) | |
1003 | error("invalid section for opcode (0x%x)", sym->st_shndx); | |
1004 | gen_code(name, sym->st_value, sym->st_size, outfile, | |
1005 | text, relocs, nb_relocs, reloc_sh_type, symtab, strtab, 0); | |
1006 | } | |
367e86e8 FB |
1007 | } |
1008 | } | |
1009 | ||
1010 | close(fd); | |
1011 | return 0; | |
1012 | } | |
1013 | ||
1014 | void usage(void) | |
1015 | { | |
1016 | printf("dyngen (c) 2003 Fabrice Bellard\n" | |
dc99065b FB |
1017 | "usage: dyngen [-o outfile] [-c] objfile\n" |
1018 | "Generate a dynamic code generator from an object file\n" | |
1019 | "-c output enum of operations\n" | |
1020 | ); | |
367e86e8 FB |
1021 | exit(1); |
1022 | } | |
1023 | ||
1024 | int main(int argc, char **argv) | |
1025 | { | |
dc99065b | 1026 | int c, do_print_enum; |
367e86e8 FB |
1027 | const char *filename, *outfilename; |
1028 | FILE *outfile; | |
1029 | ||
1030 | outfilename = "out.c"; | |
dc99065b | 1031 | do_print_enum = 0; |
367e86e8 | 1032 | for(;;) { |
dc99065b | 1033 | c = getopt(argc, argv, "ho:c"); |
367e86e8 FB |
1034 | if (c == -1) |
1035 | break; | |
1036 | switch(c) { | |
1037 | case 'h': | |
1038 | usage(); | |
1039 | break; | |
1040 | case 'o': | |
1041 | outfilename = optarg; | |
1042 | break; | |
dc99065b FB |
1043 | case 'c': |
1044 | do_print_enum = 1; | |
1045 | break; | |
367e86e8 FB |
1046 | } |
1047 | } | |
1048 | if (optind >= argc) | |
1049 | usage(); | |
1050 | filename = argv[optind]; | |
1051 | outfile = fopen(outfilename, "w"); | |
1052 | if (!outfile) | |
1053 | error("could not open '%s'", outfilename); | |
dc99065b | 1054 | load_elf(filename, outfile, do_print_enum); |
367e86e8 FB |
1055 | fclose(outfile); |
1056 | return 0; | |
1057 | } |