]>
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 | ||
abcd5da7 | 28 | #include "config-host.h" |
ce11fedc FB |
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 | ||
ff1f20a3 FB |
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 | ||
38e584a0 FB |
89 | #elif defined(HOST_M68K) |
90 | ||
91 | #define ELF_CLASS ELFCLASS32 | |
92 | #define ELF_ARCH EM_68K | |
93 | #define elf_check_arch(x) ((x) == EM_68K) | |
94 | #define ELF_USES_RELOCA | |
95 | ||
ce11fedc FB |
96 | #else |
97 | #error unsupported CPU - please update the code | |
98 | #endif | |
99 | ||
efdea7bf FB |
100 | #include "elf.h" |
101 | ||
ce11fedc FB |
102 | #if ELF_CLASS == ELFCLASS32 |
103 | typedef int32_t host_long; | |
104 | typedef uint32_t host_ulong; | |
efdea7bf | 105 | #define swabls(x) swab32s(x) |
ce11fedc FB |
106 | #else |
107 | typedef int64_t host_long; | |
108 | typedef uint64_t host_ulong; | |
efdea7bf | 109 | #define swabls(x) swab64s(x) |
fb3e5849 FB |
110 | #endif |
111 | ||
fe319756 FB |
112 | #ifdef ELF_USES_RELOCA |
113 | #define SHT_RELOC SHT_RELA | |
114 | #else | |
115 | #define SHT_RELOC SHT_REL | |
116 | #endif | |
117 | ||
abcd5da7 | 118 | #include "bswap.h" |
ce11fedc | 119 | |
d219f7e7 FB |
120 | enum { |
121 | OUT_GEN_OP, | |
122 | OUT_CODE, | |
123 | OUT_INDEX_OP, | |
124 | }; | |
125 | ||
367e86e8 | 126 | /* all dynamically generated functions begin with this code */ |
dc99065b | 127 | #define OP_PREFIX "op_" |
367e86e8 | 128 | |
ce11fedc | 129 | int elf_must_swap(struct elfhdr *h) |
367e86e8 FB |
130 | { |
131 | union { | |
132 | uint32_t i; | |
133 | uint8_t b[4]; | |
134 | } swaptest; | |
135 | ||
136 | swaptest.i = 1; | |
137 | return (h->e_ident[EI_DATA] == ELFDATA2MSB) != | |
138 | (swaptest.b[0] == 0); | |
139 | } | |
140 | ||
141 | void swab16s(uint16_t *p) | |
142 | { | |
143 | *p = bswap16(*p); | |
144 | } | |
145 | ||
146 | void swab32s(uint32_t *p) | |
147 | { | |
148 | *p = bswap32(*p); | |
149 | } | |
150 | ||
ce11fedc | 151 | void swab64s(uint64_t *p) |
367e86e8 FB |
152 | { |
153 | *p = bswap64(*p); | |
154 | } | |
155 | ||
ce11fedc | 156 | void elf_swap_ehdr(struct elfhdr *h) |
367e86e8 FB |
157 | { |
158 | swab16s(&h->e_type); /* Object file type */ | |
159 | swab16s(&h-> e_machine); /* Architecture */ | |
160 | swab32s(&h-> e_version); /* Object file version */ | |
ce11fedc FB |
161 | swabls(&h-> e_entry); /* Entry point virtual address */ |
162 | swabls(&h-> e_phoff); /* Program header table file offset */ | |
163 | swabls(&h-> e_shoff); /* Section header table file offset */ | |
367e86e8 FB |
164 | swab32s(&h-> e_flags); /* Processor-specific flags */ |
165 | swab16s(&h-> e_ehsize); /* ELF header size in bytes */ | |
166 | swab16s(&h-> e_phentsize); /* Program header table entry size */ | |
167 | swab16s(&h-> e_phnum); /* Program header table entry count */ | |
168 | swab16s(&h-> e_shentsize); /* Section header table entry size */ | |
169 | swab16s(&h-> e_shnum); /* Section header table entry count */ | |
170 | swab16s(&h-> e_shstrndx); /* Section header string table index */ | |
171 | } | |
172 | ||
ce11fedc | 173 | void elf_swap_shdr(struct elf_shdr *h) |
367e86e8 FB |
174 | { |
175 | swab32s(&h-> sh_name); /* Section name (string tbl index) */ | |
176 | swab32s(&h-> sh_type); /* Section type */ | |
ce11fedc FB |
177 | swabls(&h-> sh_flags); /* Section flags */ |
178 | swabls(&h-> sh_addr); /* Section virtual addr at execution */ | |
179 | swabls(&h-> sh_offset); /* Section file offset */ | |
180 | swabls(&h-> sh_size); /* Section size in bytes */ | |
367e86e8 FB |
181 | swab32s(&h-> sh_link); /* Link to another section */ |
182 | swab32s(&h-> sh_info); /* Additional section information */ | |
ce11fedc FB |
183 | swabls(&h-> sh_addralign); /* Section alignment */ |
184 | swabls(&h-> sh_entsize); /* Entry size if section holds table */ | |
367e86e8 FB |
185 | } |
186 | ||
ce11fedc | 187 | void elf_swap_phdr(struct elf_phdr *h) |
367e86e8 FB |
188 | { |
189 | swab32s(&h->p_type); /* Segment type */ | |
ce11fedc FB |
190 | swabls(&h->p_offset); /* Segment file offset */ |
191 | swabls(&h->p_vaddr); /* Segment virtual address */ | |
192 | swabls(&h->p_paddr); /* Segment physical address */ | |
193 | swabls(&h->p_filesz); /* Segment size in file */ | |
194 | swabls(&h->p_memsz); /* Segment size in memory */ | |
367e86e8 | 195 | swab32s(&h->p_flags); /* Segment flags */ |
ce11fedc | 196 | swabls(&h->p_align); /* Segment alignment */ |
367e86e8 FB |
197 | } |
198 | ||
fe319756 FB |
199 | void elf_swap_rel(ELF_RELOC *rel) |
200 | { | |
201 | swabls(&rel->r_offset); | |
202 | swabls(&rel->r_info); | |
203 | #ifdef ELF_USES_RELOCA | |
204 | swabls(&rel->r_addend); | |
205 | #endif | |
206 | } | |
207 | ||
d4e8164f | 208 | /* ELF file info */ |
367e86e8 | 209 | int do_swap; |
d4e8164f | 210 | struct elf_shdr *shdr; |
fe319756 | 211 | uint8_t **sdata; |
d4e8164f FB |
212 | struct elfhdr ehdr; |
213 | ElfW(Sym) *symtab; | |
214 | int nb_syms; | |
215 | char *strtab; | |
fe319756 | 216 | int text_shndx; |
367e86e8 FB |
217 | |
218 | uint16_t get16(uint16_t *p) | |
219 | { | |
220 | uint16_t val; | |
221 | val = *p; | |
222 | if (do_swap) | |
223 | val = bswap16(val); | |
224 | return val; | |
225 | } | |
226 | ||
227 | uint32_t get32(uint32_t *p) | |
228 | { | |
229 | uint32_t val; | |
230 | val = *p; | |
231 | if (do_swap) | |
232 | val = bswap32(val); | |
233 | return val; | |
234 | } | |
235 | ||
236 | void put16(uint16_t *p, uint16_t val) | |
237 | { | |
238 | if (do_swap) | |
239 | val = bswap16(val); | |
240 | *p = val; | |
241 | } | |
242 | ||
243 | void put32(uint32_t *p, uint32_t val) | |
244 | { | |
245 | if (do_swap) | |
246 | val = bswap32(val); | |
247 | *p = val; | |
248 | } | |
249 | ||
efdea7bf | 250 | void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...) |
367e86e8 FB |
251 | { |
252 | va_list ap; | |
253 | va_start(ap, fmt); | |
254 | fprintf(stderr, "dyngen: "); | |
255 | vfprintf(stderr, fmt, ap); | |
256 | fprintf(stderr, "\n"); | |
257 | va_end(ap); | |
258 | exit(1); | |
259 | } | |
260 | ||
261 | ||
ce11fedc FB |
262 | struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr, |
263 | const char *name) | |
367e86e8 FB |
264 | { |
265 | int i; | |
266 | const char *shname; | |
ce11fedc | 267 | struct elf_shdr *sec; |
367e86e8 FB |
268 | |
269 | for(i = 0; i < shnum; i++) { | |
270 | sec = &shdr[i]; | |
271 | if (!sec->sh_name) | |
272 | continue; | |
273 | shname = shstr + sec->sh_name; | |
274 | if (!strcmp(shname, name)) | |
275 | return sec; | |
276 | } | |
277 | return NULL; | |
278 | } | |
279 | ||
fe319756 FB |
280 | int find_reloc(int sh_index) |
281 | { | |
282 | struct elf_shdr *sec; | |
283 | int i; | |
284 | ||
285 | for(i = 0; i < ehdr.e_shnum; i++) { | |
286 | sec = &shdr[i]; | |
287 | if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index) | |
288 | return i; | |
289 | } | |
290 | return 0; | |
291 | } | |
292 | ||
367e86e8 FB |
293 | void *load_data(int fd, long offset, unsigned int size) |
294 | { | |
295 | char *data; | |
296 | ||
297 | data = malloc(size); | |
298 | if (!data) | |
299 | return NULL; | |
300 | lseek(fd, offset, SEEK_SET); | |
301 | if (read(fd, data, size) != size) { | |
302 | free(data); | |
303 | return NULL; | |
304 | } | |
305 | return data; | |
306 | } | |
307 | ||
308 | int strstart(const char *str, const char *val, const char **ptr) | |
309 | { | |
310 | const char *p, *q; | |
311 | p = str; | |
312 | q = val; | |
313 | while (*q != '\0') { | |
314 | if (*p != *q) | |
315 | return 0; | |
316 | p++; | |
317 | q++; | |
318 | } | |
319 | if (ptr) | |
320 | *ptr = p; | |
321 | return 1; | |
322 | } | |
323 | ||
ff1f20a3 FB |
324 | #ifdef HOST_ARM |
325 | ||
326 | int arm_emit_ldr_info(const char *name, unsigned long start_offset, | |
327 | FILE *outfile, uint8_t *p_start, uint8_t *p_end, | |
328 | ELF_RELOC *relocs, int nb_relocs) | |
329 | { | |
330 | uint8_t *p; | |
331 | uint32_t insn; | |
332 | int offset, min_offset, pc_offset, data_size; | |
333 | uint8_t data_allocated[1024]; | |
334 | unsigned int data_index; | |
335 | ||
336 | memset(data_allocated, 0, sizeof(data_allocated)); | |
337 | ||
338 | p = p_start; | |
339 | min_offset = p_end - p_start; | |
340 | while (p < p_start + min_offset) { | |
341 | insn = get32((uint32_t *)p); | |
342 | if ((insn & 0x0d5f0000) == 0x051f0000) { | |
343 | /* ldr reg, [pc, #im] */ | |
344 | offset = insn & 0xfff; | |
345 | if (!(insn & 0x00800000)) | |
346 | offset = -offset; | |
347 | if ((offset & 3) !=0) | |
348 | error("%s:%04x: ldr pc offset must be 32 bit aligned", | |
349 | name, start_offset + p - p_start); | |
350 | pc_offset = p - p_start + offset + 8; | |
351 | if (pc_offset <= (p - p_start) || | |
352 | pc_offset >= (p_end - p_start)) | |
353 | error("%s:%04x: ldr pc offset must point inside the function code", | |
354 | name, start_offset + p - p_start); | |
355 | if (pc_offset < min_offset) | |
356 | min_offset = pc_offset; | |
357 | if (outfile) { | |
358 | /* ldr position */ | |
359 | fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n", | |
360 | p - p_start); | |
361 | /* ldr data index */ | |
362 | data_index = ((p_end - p_start) - pc_offset - 4) >> 2; | |
363 | fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n", | |
364 | data_index); | |
365 | fprintf(outfile, " arm_ldr_ptr++;\n"); | |
366 | if (data_index >= sizeof(data_allocated)) | |
367 | error("%s: too many data", name); | |
368 | if (!data_allocated[data_index]) { | |
369 | ELF_RELOC *rel; | |
370 | int i, addend, type; | |
371 | const char *sym_name, *p; | |
372 | char relname[1024]; | |
373 | ||
374 | data_allocated[data_index] = 1; | |
375 | ||
376 | /* data value */ | |
377 | addend = get32((uint32_t *)(p_start + pc_offset)); | |
378 | relname[0] = '\0'; | |
379 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
380 | if (rel->r_offset == (pc_offset + start_offset)) { | |
381 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; | |
382 | /* the compiler leave some unnecessary references to the code */ | |
383 | if (strstart(sym_name, "__op_param", &p)) { | |
384 | snprintf(relname, sizeof(relname), "param%s", p); | |
385 | } else { | |
386 | snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name); | |
387 | } | |
388 | type = ELF32_R_TYPE(rel->r_info); | |
389 | if (type != R_ARM_ABS32) | |
390 | error("%s: unsupported data relocation", name); | |
391 | break; | |
392 | } | |
393 | } | |
394 | fprintf(outfile, " arm_data_ptr[%d] = 0x%x", | |
395 | data_index, addend); | |
396 | if (relname[0] != '\0') | |
397 | fprintf(outfile, " + %s", relname); | |
398 | fprintf(outfile, ";\n"); | |
399 | } | |
400 | } | |
401 | } | |
402 | p += 4; | |
403 | } | |
404 | data_size = (p_end - p_start) - min_offset; | |
405 | if (data_size > 0 && outfile) { | |
406 | fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2); | |
407 | } | |
408 | ||
409 | /* the last instruction must be a mov pc, lr */ | |
410 | if (p == p_start) | |
411 | goto arm_ret_error; | |
412 | p -= 4; | |
413 | insn = get32((uint32_t *)p); | |
414 | if ((insn & 0xffff0000) != 0xe91b0000) { | |
415 | arm_ret_error: | |
416 | if (!outfile) | |
417 | printf("%s: invalid epilog\n", name); | |
418 | } | |
419 | return p - p_start; | |
420 | } | |
421 | #endif | |
422 | ||
423 | ||
367e86e8 FB |
424 | #define MAX_ARGS 3 |
425 | ||
426 | /* generate op code */ | |
ce11fedc | 427 | void gen_code(const char *name, host_ulong offset, host_ulong size, |
fe319756 | 428 | FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs, |
d4e8164f | 429 | int gen_switch) |
367e86e8 FB |
430 | { |
431 | int copy_size = 0; | |
432 | uint8_t *p_start, *p_end; | |
ae228531 | 433 | host_ulong start_offset; |
ce11fedc | 434 | int nb_args, i, n; |
367e86e8 FB |
435 | uint8_t args_present[MAX_ARGS]; |
436 | const char *sym_name, *p; | |
ce11fedc | 437 | ELF_RELOC *rel; |
367e86e8 | 438 | |
ae228531 FB |
439 | /* Compute exact size excluding prologue and epilogue instructions. |
440 | * Increment start_offset to skip epilogue instructions, then compute | |
441 | * copy_size the indicate the size of the remaining instructions (in | |
442 | * bytes). | |
443 | */ | |
367e86e8 FB |
444 | p_start = text + offset; |
445 | p_end = p_start + size; | |
ae228531 | 446 | start_offset = offset; |
ce11fedc | 447 | switch(ELF_ARCH) { |
367e86e8 FB |
448 | case EM_386: |
449 | { | |
d4e8164f FB |
450 | int len; |
451 | len = p_end - p_start; | |
452 | if (len == 0) | |
367e86e8 | 453 | error("empty code for %s", name); |
d4e8164f FB |
454 | if (p_end[-1] == 0xc3) { |
455 | len--; | |
456 | } else { | |
457 | error("ret or jmp expected at the end of %s", name); | |
458 | } | |
459 | copy_size = len; | |
367e86e8 FB |
460 | } |
461 | break; | |
462 | case EM_PPC: | |
463 | { | |
464 | uint8_t *p; | |
465 | p = (void *)(p_end - 4); | |
367e86e8 FB |
466 | if (p == p_start) |
467 | error("empty code for %s", name); | |
04369ff2 FB |
468 | if (get32((uint32_t *)p) != 0x4e800020) |
469 | error("blr expected at the end of %s", name); | |
367e86e8 FB |
470 | copy_size = p - p_start; |
471 | } | |
472 | break; | |
fb3e5849 FB |
473 | case EM_S390: |
474 | { | |
475 | uint8_t *p; | |
476 | p = (void *)(p_end - 2); | |
477 | if (p == p_start) | |
478 | error("empty code for %s", name); | |
479 | if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4) | |
efdea7bf | 480 | error("br %%r14 expected at the end of %s", name); |
fb3e5849 FB |
481 | copy_size = p - p_start; |
482 | } | |
483 | break; | |
efdea7bf FB |
484 | case EM_ALPHA: |
485 | { | |
486 | uint8_t *p; | |
487 | p = p_end - 4; | |
488 | if (p == p_start) | |
489 | error("empty code for %s", name); | |
490 | if (get32((uint32_t *)p) != 0x6bfa8001) | |
491 | error("ret expected at the end of %s", name); | |
492 | copy_size = p - p_start; | |
493 | } | |
494 | break; | |
495 | case EM_IA_64: | |
496 | { | |
497 | uint8_t *p; | |
498 | p = (void *)(p_end - 4); | |
499 | if (p == p_start) | |
500 | error("empty code for %s", name); | |
501 | /* br.ret.sptk.many b0;; */ | |
502 | /* 08 00 84 00 */ | |
503 | if (get32((uint32_t *)p) != 0x00840008) | |
504 | error("br.ret.sptk.many b0;; expected at the end of %s", name); | |
505 | copy_size = p - p_start; | |
506 | } | |
507 | break; | |
d014c98c FB |
508 | case EM_SPARC: |
509 | case EM_SPARC32PLUS: | |
510 | { | |
ff1f20a3 | 511 | uint32_t start_insn, end_insn1, end_insn2; |
d014c98c FB |
512 | uint8_t *p; |
513 | p = (void *)(p_end - 8); | |
514 | if (p <= p_start) | |
515 | error("empty code for %s", name); | |
ae228531 FB |
516 | start_insn = get32((uint32_t *)(p_start + 0x0)); |
517 | end_insn1 = get32((uint32_t *)(p + 0x0)); | |
518 | end_insn2 = get32((uint32_t *)(p + 0x4)); | |
519 | if ((start_insn & ~0x1fff) == 0x9de3a000) { | |
520 | p_start += 0x4; | |
521 | start_offset += 0x4; | |
522 | if ((int)(start_insn | ~0x1fff) < -128) | |
523 | error("Found bogus save at the start of %s", name); | |
524 | if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000) | |
525 | error("ret; restore; not found at end of %s", name); | |
526 | } else { | |
527 | error("No save at the beginning of %s", name); | |
528 | } | |
ff1f20a3 | 529 | #if 0 |
ae228531 FB |
530 | /* Skip a preceeding nop, if present. */ |
531 | if (p > p_start) { | |
532 | skip_insn = get32((uint32_t *)(p - 0x4)); | |
533 | if (skip_insn == 0x01000000) | |
534 | p -= 4; | |
535 | } | |
ff1f20a3 | 536 | #endif |
d014c98c FB |
537 | copy_size = p - p_start; |
538 | } | |
539 | break; | |
540 | case EM_SPARCV9: | |
541 | { | |
ae228531 | 542 | uint32_t start_insn, end_insn1, end_insn2, skip_insn; |
d014c98c FB |
543 | uint8_t *p; |
544 | p = (void *)(p_end - 8); | |
545 | if (p <= p_start) | |
546 | error("empty code for %s", name); | |
ae228531 FB |
547 | start_insn = get32((uint32_t *)(p_start + 0x0)); |
548 | end_insn1 = get32((uint32_t *)(p + 0x0)); | |
549 | end_insn2 = get32((uint32_t *)(p + 0x4)); | |
550 | if ((start_insn & ~0x1fff) == 0x9de3a000) { | |
551 | p_start += 0x4; | |
552 | start_offset += 0x4; | |
553 | if ((int)(start_insn | ~0x1fff) < -256) | |
554 | error("Found bogus save at the start of %s", name); | |
555 | if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000) | |
556 | error("ret; restore; not found at end of %s", name); | |
557 | } else { | |
558 | error("No save at the beginning of %s", name); | |
559 | } | |
560 | ||
561 | /* Skip a preceeding nop, if present. */ | |
562 | if (p > p_start) { | |
563 | skip_insn = get32((uint32_t *)(p - 0x4)); | |
564 | if (skip_insn == 0x01000000) | |
565 | p -= 4; | |
566 | } | |
567 | ||
d014c98c FB |
568 | copy_size = p - p_start; |
569 | } | |
570 | break; | |
ff1f20a3 FB |
571 | #ifdef HOST_ARM |
572 | case EM_ARM: | |
573 | if ((p_end - p_start) <= 16) | |
574 | error("%s: function too small", name); | |
575 | if (get32((uint32_t *)p_start) != 0xe1a0c00d || | |
576 | (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 || | |
577 | get32((uint32_t *)(p_start + 8)) != 0xe24cb004) | |
578 | error("%s: invalid prolog", name); | |
579 | p_start += 12; | |
580 | start_offset += 12; | |
581 | copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end, | |
582 | relocs, nb_relocs); | |
583 | break; | |
584 | #endif | |
38e584a0 FB |
585 | case EM_68K: |
586 | { | |
587 | uint8_t *p; | |
588 | p = (void *)(p_end - 2); | |
589 | if (p == p_start) | |
590 | error("empty code for %s", name); | |
591 | // remove NOP's, probably added for alignment | |
592 | while ((get16((uint16_t *)p) == 0x4e71) && | |
593 | (p>p_start)) | |
594 | p -= 2; | |
595 | if (get16((uint16_t *)p) != 0x4e75) | |
596 | error("rts expected at the end of %s", name); | |
597 | copy_size = p - p_start; | |
598 | } | |
599 | break; | |
efdea7bf FB |
600 | default: |
601 | error("unknown ELF architecture"); | |
367e86e8 FB |
602 | } |
603 | ||
604 | /* compute the number of arguments by looking at the relocations */ | |
605 | for(i = 0;i < MAX_ARGS; i++) | |
606 | args_present[i] = 0; | |
607 | ||
ce11fedc | 608 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
ae228531 | 609 | if (rel->r_offset >= start_offset && |
ff1f20a3 | 610 | rel->r_offset < start_offset + (p_end - p_start)) { |
ce11fedc FB |
611 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
612 | if (strstart(sym_name, "__op_param", &p)) { | |
613 | n = strtoul(p, NULL, 10); | |
d4e8164f | 614 | if (n > MAX_ARGS) |
ce11fedc FB |
615 | error("too many arguments in %s", name); |
616 | args_present[n - 1] = 1; | |
367e86e8 FB |
617 | } |
618 | } | |
619 | } | |
620 | ||
621 | nb_args = 0; | |
622 | while (nb_args < MAX_ARGS && args_present[nb_args]) | |
623 | nb_args++; | |
624 | for(i = nb_args; i < MAX_ARGS; i++) { | |
625 | if (args_present[i]) | |
626 | error("inconsistent argument numbering in %s", name); | |
627 | } | |
628 | ||
0ea00c9a | 629 | if (gen_switch == 2) { |
a513fe19 | 630 | fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size); |
0ea00c9a | 631 | } else if (gen_switch == 1) { |
dc99065b FB |
632 | |
633 | /* output C code */ | |
634 | fprintf(outfile, "case INDEX_%s: {\n", name); | |
635 | if (nb_args > 0) { | |
636 | fprintf(outfile, " long "); | |
637 | for(i = 0; i < nb_args; i++) { | |
638 | if (i != 0) | |
639 | fprintf(outfile, ", "); | |
640 | fprintf(outfile, "param%d", i + 1); | |
641 | } | |
642 | fprintf(outfile, ";\n"); | |
367e86e8 | 643 | } |
dc99065b FB |
644 | fprintf(outfile, " extern void %s();\n", name); |
645 | ||
ce11fedc | 646 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
ae228531 | 647 | if (rel->r_offset >= start_offset && |
ff1f20a3 | 648 | rel->r_offset < start_offset + (p_end - p_start)) { |
efdea7bf | 649 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
d4e8164f FB |
650 | if (*sym_name && |
651 | !strstart(sym_name, "__op_param", NULL) && | |
652 | !strstart(sym_name, "__op_jmp", NULL)) { | |
d014c98c FB |
653 | #if defined(HOST_SPARC) |
654 | if (sym_name[0] == '.') { | |
655 | fprintf(outfile, | |
656 | "extern char __dot_%s __asm__(\"%s\");\n", | |
657 | sym_name+1, sym_name); | |
658 | continue; | |
659 | } | |
660 | #endif | |
ce11fedc | 661 | fprintf(outfile, "extern char %s;\n", sym_name); |
dc99065b FB |
662 | } |
663 | } | |
664 | } | |
665 | ||
ae228531 | 666 | fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size); |
d4e8164f FB |
667 | |
668 | /* emit code offset information */ | |
669 | { | |
670 | ElfW(Sym) *sym; | |
671 | const char *sym_name, *p; | |
abcd5da7 | 672 | unsigned long val; |
d4e8164f FB |
673 | int n; |
674 | ||
675 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { | |
676 | sym_name = strtab + sym->st_name; | |
677 | if (strstart(sym_name, "__op_label", &p)) { | |
c1e42a13 | 678 | uint8_t *ptr; |
fe319756 FB |
679 | unsigned long offset; |
680 | ||
d4e8164f FB |
681 | /* test if the variable refers to a label inside |
682 | the code we are generating */ | |
fe319756 FB |
683 | ptr = sdata[sym->st_shndx]; |
684 | if (!ptr) | |
685 | error("__op_labelN in invalid section"); | |
686 | offset = sym->st_value; | |
abcd5da7 | 687 | val = *(unsigned long *)(ptr + offset); |
fe319756 FB |
688 | #ifdef ELF_USES_RELOCA |
689 | { | |
690 | int reloc_shndx, nb_relocs1, j; | |
691 | ||
692 | /* try to find a matching relocation */ | |
693 | reloc_shndx = find_reloc(sym->st_shndx); | |
694 | if (reloc_shndx) { | |
695 | nb_relocs1 = shdr[reloc_shndx].sh_size / | |
696 | shdr[reloc_shndx].sh_entsize; | |
697 | rel = (ELF_RELOC *)sdata[reloc_shndx]; | |
698 | for(j = 0; j < nb_relocs1; j++) { | |
699 | if (rel->r_offset == offset) { | |
039de852 | 700 | val = rel->r_addend; |
fe319756 FB |
701 | break; |
702 | } | |
703 | rel++; | |
704 | } | |
705 | } | |
706 | } | |
707 | #endif | |
fe319756 | 708 | |
d4e8164f FB |
709 | if (val >= start_offset && val < start_offset + copy_size) { |
710 | n = strtol(p, NULL, 10); | |
abcd5da7 | 711 | fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset); |
d4e8164f FB |
712 | } |
713 | } | |
714 | } | |
715 | } | |
716 | ||
717 | /* load parameres in variables */ | |
dc99065b FB |
718 | for(i = 0; i < nb_args; i++) { |
719 | fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1); | |
720 | } | |
721 | ||
722 | /* patch relocations */ | |
ce11fedc | 723 | #if defined(HOST_I386) |
dc99065b | 724 | { |
dc99065b FB |
725 | char name[256]; |
726 | int type; | |
ce11fedc | 727 | int addend; |
dc99065b | 728 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
ae228531 FB |
729 | if (rel->r_offset >= start_offset && |
730 | rel->r_offset < start_offset + copy_size) { | |
efdea7bf | 731 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
367e86e8 FB |
732 | if (strstart(sym_name, "__op_param", &p)) { |
733 | snprintf(name, sizeof(name), "param%s", p); | |
734 | } else { | |
735 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
736 | } | |
737 | type = ELF32_R_TYPE(rel->r_info); | |
738 | addend = get32((uint32_t *)(text + rel->r_offset)); | |
739 | switch(type) { | |
740 | case R_386_32: | |
ce11fedc | 741 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", |
ae228531 | 742 | rel->r_offset - start_offset, name, addend); |
367e86e8 FB |
743 | break; |
744 | case R_386_PC32: | |
ce11fedc | 745 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n", |
ae228531 | 746 | rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
367e86e8 FB |
747 | break; |
748 | default: | |
749 | error("unsupported i386 relocation (%d)", type); | |
750 | } | |
751 | } | |
dc99065b FB |
752 | } |
753 | } | |
ce11fedc | 754 | #elif defined(HOST_PPC) |
04369ff2 | 755 | { |
04369ff2 FB |
756 | char name[256]; |
757 | int type; | |
ce11fedc | 758 | int addend; |
04369ff2 | 759 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
ae228531 FB |
760 | if (rel->r_offset >= start_offset && |
761 | rel->r_offset < start_offset + copy_size) { | |
efdea7bf | 762 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
d4e8164f FB |
763 | if (strstart(sym_name, "__op_jmp", &p)) { |
764 | int n; | |
765 | n = strtol(p, NULL, 10); | |
766 | /* __op_jmp relocations are done at | |
767 | runtime to do translated block | |
768 | chaining: the offset of the instruction | |
769 | needs to be stored */ | |
770 | fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", | |
771 | n, rel->r_offset - start_offset); | |
772 | continue; | |
773 | } | |
774 | ||
04369ff2 FB |
775 | if (strstart(sym_name, "__op_param", &p)) { |
776 | snprintf(name, sizeof(name), "param%s", p); | |
777 | } else { | |
778 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
779 | } | |
780 | type = ELF32_R_TYPE(rel->r_info); | |
781 | addend = rel->r_addend; | |
782 | switch(type) { | |
783 | case R_PPC_ADDR32: | |
ce11fedc | 784 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", |
ae228531 | 785 | rel->r_offset - start_offset, name, addend); |
04369ff2 FB |
786 | break; |
787 | case R_PPC_ADDR16_LO: | |
ce11fedc | 788 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n", |
ae228531 | 789 | rel->r_offset - start_offset, name, addend); |
04369ff2 FB |
790 | break; |
791 | case R_PPC_ADDR16_HI: | |
ce11fedc | 792 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n", |
ae228531 | 793 | rel->r_offset - start_offset, name, addend); |
04369ff2 FB |
794 | break; |
795 | case R_PPC_ADDR16_HA: | |
ce11fedc | 796 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n", |
ae228531 | 797 | rel->r_offset - start_offset, name, addend); |
04369ff2 FB |
798 | break; |
799 | case R_PPC_REL24: | |
800 | /* warning: must be at 32 MB distancy */ | |
ce11fedc | 801 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n", |
ae228531 | 802 | rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
04369ff2 FB |
803 | break; |
804 | default: | |
805 | error("unsupported powerpc relocation (%d)", type); | |
806 | } | |
807 | } | |
808 | } | |
809 | } | |
ce11fedc | 810 | #elif defined(HOST_S390) |
fb3e5849 | 811 | { |
fb3e5849 FB |
812 | char name[256]; |
813 | int type; | |
ce11fedc | 814 | int addend; |
fb3e5849 | 815 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
ae228531 FB |
816 | if (rel->r_offset >= start_offset && |
817 | rel->r_offset < start_offset + copy_size) { | |
efdea7bf | 818 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
fb3e5849 FB |
819 | if (strstart(sym_name, "__op_param", &p)) { |
820 | snprintf(name, sizeof(name), "param%s", p); | |
821 | } else { | |
822 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
823 | } | |
824 | type = ELF32_R_TYPE(rel->r_info); | |
825 | addend = rel->r_addend; | |
826 | switch(type) { | |
827 | case R_390_32: | |
ce11fedc | 828 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", |
ae228531 | 829 | rel->r_offset - start_offset, name, addend); |
fb3e5849 FB |
830 | break; |
831 | case R_390_16: | |
ce11fedc | 832 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n", |
ae228531 | 833 | rel->r_offset - start_offset, name, addend); |
fb3e5849 FB |
834 | break; |
835 | case R_390_8: | |
ce11fedc | 836 | fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n", |
ae228531 | 837 | rel->r_offset - start_offset, name, addend); |
fb3e5849 FB |
838 | break; |
839 | default: | |
840 | error("unsupported s390 relocation (%d)", type); | |
841 | } | |
842 | } | |
843 | } | |
844 | } | |
efdea7bf FB |
845 | #elif defined(HOST_ALPHA) |
846 | { | |
847 | for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) { | |
ae228531 | 848 | if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) { |
efdea7bf | 849 | int type; |
74c95119 | 850 | |
efdea7bf | 851 | type = ELF64_R_TYPE(rel->r_info); |
74c95119 | 852 | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
efdea7bf FB |
853 | switch (type) { |
854 | case R_ALPHA_GPDISP: | |
74c95119 FB |
855 | /* The gp is just 32 bit, and never changes, so it's easiest to emit it |
856 | as an immediate instead of constructing it from the pv or ra. */ | |
857 | fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n", | |
ae228531 | 858 | rel->r_offset - start_offset); |
74c95119 | 859 | fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n", |
ae228531 | 860 | rel->r_offset - start_offset + rel->r_addend); |
efdea7bf FB |
861 | break; |
862 | case R_ALPHA_LITUSE: | |
863 | /* jsr to literal hint. Could be used to optimize to bsr. Ignore for | |
864 | now, since some called functions (libc) need pv to be set up. */ | |
865 | break; | |
866 | case R_ALPHA_HINT: | |
867 | /* Branch target prediction hint. Ignore for now. Should be already | |
868 | correct for in-function jumps. */ | |
869 | break; | |
870 | case R_ALPHA_LITERAL: | |
74c95119 FB |
871 | /* Load a literal from the GOT relative to the gp. Since there's only a |
872 | single gp, nothing is to be done. */ | |
873 | break; | |
874 | case R_ALPHA_GPRELHIGH: | |
875 | /* Handle fake relocations against __op_param symbol. Need to emit the | |
876 | high part of the immediate value instead. Other symbols need no | |
877 | special treatment. */ | |
878 | if (strstart(sym_name, "__op_param", &p)) | |
879 | fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n", | |
ae228531 | 880 | rel->r_offset - start_offset, p); |
74c95119 FB |
881 | break; |
882 | case R_ALPHA_GPRELLOW: | |
883 | if (strstart(sym_name, "__op_param", &p)) | |
884 | fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n", | |
ae228531 | 885 | rel->r_offset - start_offset, p); |
74c95119 FB |
886 | break; |
887 | case R_ALPHA_BRSGP: | |
888 | /* PC-relative jump. Tweak offset to skip the two instructions that try to | |
889 | set up the gp from the pv. */ | |
2f87c607 | 890 | fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n", |
ae228531 | 891 | rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset); |
efdea7bf FB |
892 | break; |
893 | default: | |
894 | error("unsupported Alpha relocation (%d)", type); | |
895 | } | |
896 | } | |
897 | } | |
898 | } | |
899 | #elif defined(HOST_IA64) | |
900 | { | |
901 | char name[256]; | |
902 | int type; | |
903 | int addend; | |
904 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
ae228531 | 905 | if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) { |
efdea7bf FB |
906 | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
907 | if (strstart(sym_name, "__op_param", &p)) { | |
908 | snprintf(name, sizeof(name), "param%s", p); | |
909 | } else { | |
910 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
911 | } | |
912 | type = ELF64_R_TYPE(rel->r_info); | |
913 | addend = rel->r_addend; | |
914 | switch(type) { | |
915 | case R_IA64_LTOFF22: | |
916 | error("must implemnt R_IA64_LTOFF22 relocation"); | |
917 | case R_IA64_PCREL21B: | |
918 | error("must implemnt R_IA64_PCREL21B relocation"); | |
919 | default: | |
920 | error("unsupported ia64 relocation (%d)", type); | |
921 | } | |
922 | } | |
923 | } | |
924 | } | |
d014c98c FB |
925 | #elif defined(HOST_SPARC) |
926 | { | |
927 | char name[256]; | |
928 | int type; | |
929 | int addend; | |
930 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
ae228531 FB |
931 | if (rel->r_offset >= start_offset && |
932 | rel->r_offset < start_offset + copy_size) { | |
d014c98c FB |
933 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; |
934 | if (strstart(sym_name, "__op_param", &p)) { | |
935 | snprintf(name, sizeof(name), "param%s", p); | |
936 | } else { | |
937 | if (sym_name[0] == '.') | |
938 | snprintf(name, sizeof(name), | |
939 | "(long)(&__dot_%s)", | |
940 | sym_name + 1); | |
941 | else | |
942 | snprintf(name, sizeof(name), | |
943 | "(long)(&%s)", sym_name); | |
944 | } | |
945 | type = ELF32_R_TYPE(rel->r_info); | |
946 | addend = rel->r_addend; | |
947 | switch(type) { | |
948 | case R_SPARC_32: | |
949 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", | |
ae228531 | 950 | rel->r_offset - start_offset, name, addend); |
d014c98c FB |
951 | break; |
952 | case R_SPARC_HI22: | |
953 | fprintf(outfile, | |
954 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
955 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
956 | " & ~0x3fffff) " | |
ae228531 FB |
957 | " | (((%s + %d) >> 10) & 0x3fffff);\n", |
958 | rel->r_offset - start_offset, | |
959 | rel->r_offset - start_offset, | |
d014c98c FB |
960 | name, addend); |
961 | break; | |
962 | case R_SPARC_LO10: | |
963 | fprintf(outfile, | |
964 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
965 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
966 | " & ~0x3ff) " | |
967 | " | ((%s + %d) & 0x3ff);\n", | |
ae228531 FB |
968 | rel->r_offset - start_offset, |
969 | rel->r_offset - start_offset, | |
d014c98c FB |
970 | name, addend); |
971 | break; | |
972 | case R_SPARC_WDISP30: | |
973 | fprintf(outfile, | |
974 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
975 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
976 | " & ~0x3fffffff) " | |
ae228531 | 977 | " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) " |
d014c98c | 978 | " & 0x3fffffff);\n", |
ae228531 FB |
979 | rel->r_offset - start_offset, |
980 | rel->r_offset - start_offset, | |
981 | name, addend, | |
982 | rel->r_offset - start_offset); | |
d014c98c FB |
983 | break; |
984 | default: | |
985 | error("unsupported sparc relocation (%d)", type); | |
986 | } | |
987 | } | |
988 | } | |
989 | } | |
990 | #elif defined(HOST_SPARC64) | |
991 | { | |
992 | char name[256]; | |
993 | int type; | |
994 | int addend; | |
995 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
ae228531 FB |
996 | if (rel->r_offset >= start_offset && |
997 | rel->r_offset < start_offset + copy_size) { | |
d014c98c FB |
998 | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
999 | if (strstart(sym_name, "__op_param", &p)) { | |
1000 | snprintf(name, sizeof(name), "param%s", p); | |
1001 | } else { | |
1002 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
1003 | } | |
1004 | type = ELF64_R_TYPE(rel->r_info); | |
1005 | addend = rel->r_addend; | |
1006 | switch(type) { | |
1007 | case R_SPARC_32: | |
1008 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", | |
ae228531 | 1009 | rel->r_offset - start_offset, name, addend); |
d014c98c FB |
1010 | break; |
1011 | case R_SPARC_HI22: | |
1012 | fprintf(outfile, | |
1013 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
1014 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
1015 | " & ~0x3fffff) " | |
ae228531 FB |
1016 | " | (((%s + %d) >> 10) & 0x3fffff);\n", |
1017 | rel->r_offset - start_offset, | |
1018 | rel->r_offset - start_offset, | |
d014c98c FB |
1019 | name, addend); |
1020 | break; | |
1021 | case R_SPARC_LO10: | |
1022 | fprintf(outfile, | |
1023 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
1024 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
1025 | " & ~0x3ff) " | |
1026 | " | ((%s + %d) & 0x3ff);\n", | |
ae228531 FB |
1027 | rel->r_offset - start_offset, |
1028 | rel->r_offset - start_offset, | |
d014c98c FB |
1029 | name, addend); |
1030 | break; | |
1031 | case R_SPARC_WDISP30: | |
1032 | fprintf(outfile, | |
1033 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
1034 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
1035 | " & ~0x3fffffff) " | |
ae228531 | 1036 | " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) " |
d014c98c | 1037 | " & 0x3fffffff);\n", |
ae228531 FB |
1038 | rel->r_offset - start_offset, |
1039 | rel->r_offset - start_offset, | |
1040 | name, addend, | |
1041 | rel->r_offset - start_offset); | |
d014c98c FB |
1042 | break; |
1043 | default: | |
1044 | error("unsupported sparc64 relocation (%d)", type); | |
1045 | } | |
1046 | } | |
1047 | } | |
1048 | } | |
ff1f20a3 FB |
1049 | #elif defined(HOST_ARM) |
1050 | { | |
1051 | char name[256]; | |
1052 | int type; | |
1053 | int addend; | |
1054 | ||
1055 | arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end, | |
1056 | relocs, nb_relocs); | |
1057 | ||
1058 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
1059 | if (rel->r_offset >= start_offset && | |
1060 | rel->r_offset < start_offset + copy_size) { | |
1061 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; | |
1062 | /* the compiler leave some unnecessary references to the code */ | |
1063 | if (sym_name[0] == '\0') | |
1064 | continue; | |
1065 | if (strstart(sym_name, "__op_param", &p)) { | |
1066 | snprintf(name, sizeof(name), "param%s", p); | |
1067 | } else { | |
1068 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
1069 | } | |
1070 | type = ELF32_R_TYPE(rel->r_info); | |
1071 | addend = get32((uint32_t *)(text + rel->r_offset)); | |
1072 | switch(type) { | |
1073 | case R_ARM_ABS32: | |
1074 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", | |
1075 | rel->r_offset - start_offset, name, addend); | |
1076 | break; | |
1077 | case R_ARM_PC24: | |
1078 | fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n", | |
1079 | rel->r_offset - start_offset, addend, name); | |
1080 | break; | |
1081 | default: | |
1082 | error("unsupported arm relocation (%d)", type); | |
1083 | } | |
1084 | } | |
1085 | } | |
38e584a0 FB |
1086 | } |
1087 | #elif defined(HOST_M68K) | |
1088 | { | |
1089 | char name[256]; | |
1090 | int type; | |
1091 | int addend; | |
1092 | Elf32_Sym *sym; | |
1093 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
1094 | if (rel->r_offset >= start_offset && | |
1095 | rel->r_offset < start_offset + copy_size) { | |
1096 | sym = &(symtab[ELFW(R_SYM)(rel->r_info)]); | |
1097 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; | |
1098 | if (strstart(sym_name, "__op_param", &p)) { | |
1099 | snprintf(name, sizeof(name), "param%s", p); | |
1100 | } else { | |
1101 | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); | |
1102 | } | |
1103 | type = ELF32_R_TYPE(rel->r_info); | |
1104 | addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend; | |
1105 | switch(type) { | |
1106 | case R_68K_32: | |
1107 | fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ; | |
1108 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n", | |
1109 | rel->r_offset - start_offset, name, addend ); | |
1110 | break; | |
1111 | case R_68K_PC32: | |
1112 | fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset); | |
1113 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n", | |
1114 | rel->r_offset - start_offset, name, rel->r_offset - start_offset, /*sym->st_value+*/ addend); | |
1115 | break; | |
1116 | default: | |
1117 | error("unsupported m68k relocation (%d)", type); | |
1118 | } | |
1119 | } | |
1120 | } | |
ff1f20a3 | 1121 | } |
ce11fedc FB |
1122 | #else |
1123 | #error unsupported CPU | |
1124 | #endif | |
dc99065b FB |
1125 | fprintf(outfile, " gen_code_ptr += %d;\n", copy_size); |
1126 | fprintf(outfile, "}\n"); | |
1127 | fprintf(outfile, "break;\n\n"); | |
1128 | } else { | |
1129 | fprintf(outfile, "static inline void gen_%s(", name); | |
1130 | if (nb_args == 0) { | |
1131 | fprintf(outfile, "void"); | |
1132 | } else { | |
1133 | for(i = 0; i < nb_args; i++) { | |
1134 | if (i != 0) | |
1135 | fprintf(outfile, ", "); | |
1136 | fprintf(outfile, "long param%d", i + 1); | |
367e86e8 FB |
1137 | } |
1138 | } | |
dc99065b FB |
1139 | fprintf(outfile, ")\n"); |
1140 | fprintf(outfile, "{\n"); | |
1141 | for(i = 0; i < nb_args; i++) { | |
1142 | fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1); | |
1143 | } | |
1144 | fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name); | |
1145 | fprintf(outfile, "}\n\n"); | |
367e86e8 | 1146 | } |
367e86e8 FB |
1147 | } |
1148 | ||
1149 | /* load an elf object file */ | |
d219f7e7 | 1150 | int load_elf(const char *filename, FILE *outfile, int out_type) |
367e86e8 FB |
1151 | { |
1152 | int fd; | |
d4e8164f FB |
1153 | struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec; |
1154 | int i, j; | |
1155 | ElfW(Sym) *sym; | |
c1e42a13 | 1156 | char *shstr; |
367e86e8 | 1157 | uint8_t *text; |
fe319756 FB |
1158 | ELF_RELOC *relocs; |
1159 | int nb_relocs; | |
1160 | ELF_RELOC *rel; | |
367e86e8 FB |
1161 | |
1162 | fd = open(filename, O_RDONLY); | |
1163 | if (fd < 0) | |
1164 | error("can't open file '%s'", filename); | |
1165 | ||
1166 | /* Read ELF header. */ | |
1167 | if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr)) | |
1168 | error("unable to read file header"); | |
1169 | ||
1170 | /* Check ELF identification. */ | |
1171 | if (ehdr.e_ident[EI_MAG0] != ELFMAG0 | |
1172 | || ehdr.e_ident[EI_MAG1] != ELFMAG1 | |
1173 | || ehdr.e_ident[EI_MAG2] != ELFMAG2 | |
1174 | || ehdr.e_ident[EI_MAG3] != ELFMAG3 | |
367e86e8 FB |
1175 | || ehdr.e_ident[EI_VERSION] != EV_CURRENT) { |
1176 | error("bad ELF header"); | |
1177 | } | |
1178 | ||
1179 | do_swap = elf_must_swap(&ehdr); | |
1180 | if (do_swap) | |
1181 | elf_swap_ehdr(&ehdr); | |
ce11fedc FB |
1182 | if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) |
1183 | error("Unsupported ELF class"); | |
367e86e8 FB |
1184 | if (ehdr.e_type != ET_REL) |
1185 | error("ELF object file expected"); | |
1186 | if (ehdr.e_version != EV_CURRENT) | |
1187 | error("Invalid ELF version"); | |
ce11fedc FB |
1188 | if (!elf_check_arch(ehdr.e_machine)) |
1189 | error("Unsupported CPU (e_machine=%d)", ehdr.e_machine); | |
367e86e8 FB |
1190 | |
1191 | /* read section headers */ | |
ce11fedc | 1192 | shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr)); |
367e86e8 FB |
1193 | if (do_swap) { |
1194 | for(i = 0; i < ehdr.e_shnum; i++) { | |
1195 | elf_swap_shdr(&shdr[i]); | |
1196 | } | |
1197 | } | |
1198 | ||
fe319756 FB |
1199 | /* read all section data */ |
1200 | sdata = malloc(sizeof(void *) * ehdr.e_shnum); | |
1201 | memset(sdata, 0, sizeof(void *) * ehdr.e_shnum); | |
1202 | ||
1203 | for(i = 0;i < ehdr.e_shnum; i++) { | |
1204 | sec = &shdr[i]; | |
1205 | if (sec->sh_type != SHT_NOBITS) | |
1206 | sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size); | |
1207 | } | |
1208 | ||
367e86e8 | 1209 | sec = &shdr[ehdr.e_shstrndx]; |
fe319756 | 1210 | shstr = sdata[ehdr.e_shstrndx]; |
367e86e8 | 1211 | |
fe319756 FB |
1212 | /* swap relocations */ |
1213 | for(i = 0; i < ehdr.e_shnum; i++) { | |
1214 | sec = &shdr[i]; | |
1215 | if (sec->sh_type == SHT_RELOC) { | |
1216 | nb_relocs = sec->sh_size / sec->sh_entsize; | |
1217 | if (do_swap) { | |
1218 | for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++) | |
1219 | elf_swap_rel(rel); | |
1220 | } | |
1221 | } | |
1222 | } | |
367e86e8 FB |
1223 | /* text section */ |
1224 | ||
1225 | text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text"); | |
1226 | if (!text_sec) | |
1227 | error("could not find .text section"); | |
fe319756 FB |
1228 | text_shndx = text_sec - shdr; |
1229 | text = sdata[text_shndx]; | |
367e86e8 FB |
1230 | |
1231 | /* find text relocations, if any */ | |
367e86e8 | 1232 | relocs = NULL; |
fe319756 FB |
1233 | nb_relocs = 0; |
1234 | i = find_reloc(text_shndx); | |
1235 | if (i != 0) { | |
1236 | relocs = (ELF_RELOC *)sdata[i]; | |
1237 | nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize; | |
367e86e8 FB |
1238 | } |
1239 | ||
1240 | symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab"); | |
1241 | if (!symtab_sec) | |
1242 | error("could not find .symtab section"); | |
1243 | strtab_sec = &shdr[symtab_sec->sh_link]; | |
1244 | ||
fe319756 FB |
1245 | symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr]; |
1246 | strtab = sdata[symtab_sec->sh_link]; | |
367e86e8 | 1247 | |
efdea7bf | 1248 | nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym)); |
367e86e8 FB |
1249 | if (do_swap) { |
1250 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { | |
1251 | swab32s(&sym->st_name); | |
ce11fedc FB |
1252 | swabls(&sym->st_value); |
1253 | swabls(&sym->st_size); | |
367e86e8 FB |
1254 | swab16s(&sym->st_shndx); |
1255 | } | |
1256 | } | |
1257 | ||
d219f7e7 | 1258 | if (out_type == OUT_INDEX_OP) { |
a513fe19 | 1259 | fprintf(outfile, "DEF(end, 0, 0)\n"); |
e477b8b8 FB |
1260 | fprintf(outfile, "DEF(nop, 0, 0)\n"); |
1261 | fprintf(outfile, "DEF(nop1, 1, 0)\n"); | |
1262 | fprintf(outfile, "DEF(nop2, 2, 0)\n"); | |
1263 | fprintf(outfile, "DEF(nop3, 3, 0)\n"); | |
dc99065b FB |
1264 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
1265 | const char *name, *p; | |
1266 | name = strtab + sym->st_name; | |
1267 | if (strstart(name, OP_PREFIX, &p)) { | |
0ea00c9a | 1268 | gen_code(name, sym->st_value, sym->st_size, outfile, |
fe319756 | 1269 | text, relocs, nb_relocs, 2); |
dc99065b FB |
1270 | } |
1271 | } | |
d219f7e7 FB |
1272 | } else if (out_type == OUT_GEN_OP) { |
1273 | /* generate gen_xxx functions */ | |
1274 | ||
1275 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { | |
1276 | const char *name; | |
1277 | name = strtab + sym->st_name; | |
1278 | if (strstart(name, OP_PREFIX, NULL)) { | |
1279 | if (sym->st_shndx != (text_sec - shdr)) | |
1280 | error("invalid section for opcode (0x%x)", sym->st_shndx); | |
1281 | gen_code(name, sym->st_value, sym->st_size, outfile, | |
1282 | text, relocs, nb_relocs, 0); | |
1283 | } | |
1284 | } | |
1285 | ||
dc99065b FB |
1286 | } else { |
1287 | /* generate big code generation switch */ | |
1288 | fprintf(outfile, | |
1289 | "int dyngen_code(uint8_t *gen_code_buf,\n" | |
d4e8164f | 1290 | " uint16_t *label_offsets, uint16_t *jmp_offsets,\n" |
dc99065b FB |
1291 | " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n" |
1292 | "{\n" | |
1293 | " uint8_t *gen_code_ptr;\n" | |
1294 | " const uint16_t *opc_ptr;\n" | |
ff1f20a3 FB |
1295 | " const uint32_t *opparam_ptr;\n"); |
1296 | ||
1297 | #ifdef HOST_ARM | |
1298 | fprintf(outfile, | |
1299 | " uint8_t *last_gen_code_ptr = gen_code_buf;\n" | |
1300 | " LDREntry *arm_ldr_ptr = arm_ldr_table;\n" | |
1301 | " uint32_t *arm_data_ptr = arm_data_table;\n"); | |
1302 | #endif | |
1303 | ||
1304 | fprintf(outfile, | |
1305 | "\n" | |
dc99065b FB |
1306 | " gen_code_ptr = gen_code_buf;\n" |
1307 | " opc_ptr = opc_buf;\n" | |
ae228531 FB |
1308 | " opparam_ptr = opparam_buf;\n"); |
1309 | ||
1310 | /* Generate prologue, if needed. */ | |
ae228531 FB |
1311 | |
1312 | fprintf(outfile, | |
dc99065b FB |
1313 | " for(;;) {\n" |
1314 | " switch(*opc_ptr++) {\n" | |
1315 | ); | |
367e86e8 | 1316 | |
dc99065b FB |
1317 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
1318 | const char *name; | |
1319 | name = strtab + sym->st_name; | |
1320 | if (strstart(name, OP_PREFIX, NULL)) { | |
367e86e8 | 1321 | #if 0 |
dc99065b FB |
1322 | printf("%4d: %s pos=0x%08x len=%d\n", |
1323 | i, name, sym->st_value, sym->st_size); | |
367e86e8 | 1324 | #endif |
dc99065b FB |
1325 | if (sym->st_shndx != (text_sec - shdr)) |
1326 | error("invalid section for opcode (0x%x)", sym->st_shndx); | |
1327 | gen_code(name, sym->st_value, sym->st_size, outfile, | |
fe319756 | 1328 | text, relocs, nb_relocs, 1); |
dc99065b FB |
1329 | } |
1330 | } | |
1331 | ||
1332 | fprintf(outfile, | |
8ef9a8ec FB |
1333 | " case INDEX_op_nop:\n" |
1334 | " break;\n" | |
1335 | " case INDEX_op_nop1:\n" | |
1336 | " opparam_ptr++;\n" | |
1337 | " break;\n" | |
1338 | " case INDEX_op_nop2:\n" | |
1339 | " opparam_ptr += 2;\n" | |
1340 | " break;\n" | |
1341 | " case INDEX_op_nop3:\n" | |
1342 | " opparam_ptr += 3;\n" | |
1343 | " break;\n" | |
dc99065b FB |
1344 | " default:\n" |
1345 | " goto the_end;\n" | |
ff1f20a3 FB |
1346 | " }\n"); |
1347 | ||
1348 | #ifdef HOST_ARM | |
1349 | /* generate constant table if needed */ | |
1350 | fprintf(outfile, | |
1351 | " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n" | |
1352 | " gen_code_ptr = arm_flush_ldr(gen_code_ptr, arm_ldr_table, arm_ldr_ptr, arm_data_table, arm_data_ptr, 1);\n" | |
1353 | " last_gen_code_ptr = gen_code_ptr;\n" | |
1354 | " arm_ldr_ptr = arm_ldr_table;\n" | |
1355 | " arm_data_ptr = arm_data_table;\n" | |
1356 | " }\n"); | |
1357 | #endif | |
1358 | ||
1359 | ||
1360 | fprintf(outfile, | |
dc99065b FB |
1361 | " }\n" |
1362 | " the_end:\n" | |
1363 | ); | |
1364 | ||
9621339d FB |
1365 | /* generate some code patching */ |
1366 | #ifdef HOST_ARM | |
1367 | 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"); | |
1368 | #endif | |
d219f7e7 FB |
1369 | /* flush instruction cache */ |
1370 | fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n"); | |
1371 | ||
dc99065b FB |
1372 | fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n"); |
1373 | fprintf(outfile, "}\n\n"); | |
1374 | ||
367e86e8 FB |
1375 | } |
1376 | ||
1377 | close(fd); | |
1378 | return 0; | |
1379 | } | |
1380 | ||
1381 | void usage(void) | |
1382 | { | |
1383 | printf("dyngen (c) 2003 Fabrice Bellard\n" | |
dc99065b FB |
1384 | "usage: dyngen [-o outfile] [-c] objfile\n" |
1385 | "Generate a dynamic code generator from an object file\n" | |
1386 | "-c output enum of operations\n" | |
d219f7e7 | 1387 | "-g output gen_op_xx() functions\n" |
dc99065b | 1388 | ); |
367e86e8 FB |
1389 | exit(1); |
1390 | } | |
1391 | ||
1392 | int main(int argc, char **argv) | |
1393 | { | |
d219f7e7 | 1394 | int c, out_type; |
367e86e8 FB |
1395 | const char *filename, *outfilename; |
1396 | FILE *outfile; | |
1397 | ||
1398 | outfilename = "out.c"; | |
d219f7e7 | 1399 | out_type = OUT_CODE; |
367e86e8 | 1400 | for(;;) { |
d219f7e7 | 1401 | c = getopt(argc, argv, "ho:cg"); |
367e86e8 FB |
1402 | if (c == -1) |
1403 | break; | |
1404 | switch(c) { | |
1405 | case 'h': | |
1406 | usage(); | |
1407 | break; | |
1408 | case 'o': | |
1409 | outfilename = optarg; | |
1410 | break; | |
dc99065b | 1411 | case 'c': |
d219f7e7 FB |
1412 | out_type = OUT_INDEX_OP; |
1413 | break; | |
1414 | case 'g': | |
1415 | out_type = OUT_GEN_OP; | |
dc99065b | 1416 | break; |
367e86e8 FB |
1417 | } |
1418 | } | |
1419 | if (optind >= argc) | |
1420 | usage(); | |
1421 | filename = argv[optind]; | |
1422 | outfile = fopen(outfilename, "w"); | |
1423 | if (!outfile) | |
1424 | error("could not open '%s'", outfilename); | |
d219f7e7 | 1425 | load_elf(filename, outfile, out_type); |
367e86e8 FB |
1426 | fclose(outfile); |
1427 | return 0; | |
1428 | } |