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