]>
Commit | Line | Data |
---|---|---|
7d13299d FB |
1 | /* |
2 | * Generic Dynamic compiler generator | |
5fafdf24 | 3 | * |
7d13299d FB |
4 | * Copyright (c) 2003 Fabrice Bellard |
5 | * | |
67b915a5 FB |
6 | * The COFF object format support was extracted from Kazu's QEMU port |
7 | * to Win32. | |
8 | * | |
82eec0a1 FB |
9 | * Mach-O Support by Matt Reda and Pierre d'Herbemont |
10 | * | |
7d13299d FB |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | */ | |
367e86e8 FB |
25 | #include <stdlib.h> |
26 | #include <stdio.h> | |
04369ff2 | 27 | #include <string.h> |
367e86e8 FB |
28 | #include <stdarg.h> |
29 | #include <inttypes.h> | |
367e86e8 FB |
30 | #include <unistd.h> |
31 | #include <fcntl.h> | |
32 | ||
abcd5da7 | 33 | #include "config-host.h" |
ce11fedc | 34 | |
11d9f695 FB |
35 | /* NOTE: we test CONFIG_WIN32 instead of _WIN32 to enabled cross |
36 | compilation */ | |
37 | #if defined(CONFIG_WIN32) | |
67b915a5 | 38 | #define CONFIG_FORMAT_COFF |
82eec0a1 FB |
39 | #elif defined(CONFIG_DARWIN) |
40 | #define CONFIG_FORMAT_MACH | |
67b915a5 FB |
41 | #else |
42 | #define CONFIG_FORMAT_ELF | |
43 | #endif | |
44 | ||
45 | #ifdef CONFIG_FORMAT_ELF | |
46 | ||
ce11fedc FB |
47 | /* elf format definitions. We use these macros to test the CPU to |
48 | allow cross compilation (this tool must be ran on the build | |
49 | platform) */ | |
50 | #if defined(HOST_I386) | |
51 | ||
52 | #define ELF_CLASS ELFCLASS32 | |
53 | #define ELF_ARCH EM_386 | |
54 | #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) ) | |
55 | #undef ELF_USES_RELOCA | |
56 | ||
c4687878 | 57 | #elif defined(HOST_X86_64) |
bc51c5c9 FB |
58 | |
59 | #define ELF_CLASS ELFCLASS64 | |
60 | #define ELF_ARCH EM_X86_64 | |
61 | #define elf_check_arch(x) ((x) == EM_X86_64) | |
62 | #define ELF_USES_RELOCA | |
63 | ||
ce11fedc FB |
64 | #elif defined(HOST_PPC) |
65 | ||
66 | #define ELF_CLASS ELFCLASS32 | |
67 | #define ELF_ARCH EM_PPC | |
68 | #define elf_check_arch(x) ((x) == EM_PPC) | |
69 | #define ELF_USES_RELOCA | |
70 | ||
810260a8 | 71 | #elif defined(HOST_PPC64) |
72 | ||
73 | #define ELF_CLASS ELFCLASS64 | |
74 | #define ELF_ARCH EM_PPC64 | |
75 | #define elf_check_arch(x) ((x) == EM_PPC64) | |
76 | #define ELF_USES_RELOCA | |
77 | ||
ce11fedc FB |
78 | #elif defined(HOST_S390) |
79 | ||
80 | #define ELF_CLASS ELFCLASS32 | |
81 | #define ELF_ARCH EM_S390 | |
82 | #define elf_check_arch(x) ((x) == EM_S390) | |
83 | #define ELF_USES_RELOCA | |
367e86e8 | 84 | |
ce11fedc FB |
85 | #elif defined(HOST_ALPHA) |
86 | ||
87 | #define ELF_CLASS ELFCLASS64 | |
88 | #define ELF_ARCH EM_ALPHA | |
89 | #define elf_check_arch(x) ((x) == EM_ALPHA) | |
90 | #define ELF_USES_RELOCA | |
91 | ||
efdea7bf FB |
92 | #elif defined(HOST_IA64) |
93 | ||
94 | #define ELF_CLASS ELFCLASS64 | |
95 | #define ELF_ARCH EM_IA_64 | |
96 | #define elf_check_arch(x) ((x) == EM_IA_64) | |
97 | #define ELF_USES_RELOCA | |
98 | ||
d014c98c FB |
99 | #elif defined(HOST_SPARC) |
100 | ||
101 | #define ELF_CLASS ELFCLASS32 | |
102 | #define ELF_ARCH EM_SPARC | |
103 | #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS) | |
104 | #define ELF_USES_RELOCA | |
105 | ||
106 | #elif defined(HOST_SPARC64) | |
107 | ||
108 | #define ELF_CLASS ELFCLASS64 | |
109 | #define ELF_ARCH EM_SPARCV9 | |
110 | #define elf_check_arch(x) ((x) == EM_SPARCV9) | |
111 | #define ELF_USES_RELOCA | |
112 | ||
ff1f20a3 FB |
113 | #elif defined(HOST_ARM) |
114 | ||
115 | #define ELF_CLASS ELFCLASS32 | |
116 | #define ELF_ARCH EM_ARM | |
117 | #define elf_check_arch(x) ((x) == EM_ARM) | |
118 | #define ELF_USES_RELOC | |
119 | ||
38e584a0 FB |
120 | #elif defined(HOST_M68K) |
121 | ||
122 | #define ELF_CLASS ELFCLASS32 | |
123 | #define ELF_ARCH EM_68K | |
124 | #define elf_check_arch(x) ((x) == EM_68K) | |
125 | #define ELF_USES_RELOCA | |
126 | ||
f54b3f92 AJ |
127 | #elif defined(HOST_HPPA) |
128 | ||
129 | #define ELF_CLASS ELFCLASS32 | |
130 | #define ELF_ARCH EM_PARISC | |
131 | #define elf_check_arch(x) ((x) == EM_PARISC) | |
132 | #define ELF_USES_RELOCA | |
133 | ||
c4b89d18 TS |
134 | #elif defined(HOST_MIPS) |
135 | ||
136 | #define ELF_CLASS ELFCLASS32 | |
137 | #define ELF_ARCH EM_MIPS | |
138 | #define elf_check_arch(x) ((x) == EM_MIPS) | |
139 | #define ELF_USES_RELOC | |
140 | ||
9617efe8 TS |
141 | #elif defined(HOST_MIPS64) |
142 | ||
143 | /* Assume n32 ABI here, which is ELF32. */ | |
144 | #define ELF_CLASS ELFCLASS32 | |
145 | #define ELF_ARCH EM_MIPS | |
146 | #define elf_check_arch(x) ((x) == EM_MIPS) | |
147 | #define ELF_USES_RELOCA | |
148 | ||
ce11fedc FB |
149 | #else |
150 | #error unsupported CPU - please update the code | |
151 | #endif | |
152 | ||
efdea7bf FB |
153 | #include "elf.h" |
154 | ||
ce11fedc FB |
155 | #if ELF_CLASS == ELFCLASS32 |
156 | typedef int32_t host_long; | |
157 | typedef uint32_t host_ulong; | |
efdea7bf | 158 | #define swabls(x) swab32s(x) |
a86c8f29 | 159 | #define swablss(x) swab32ss(x) |
ce11fedc FB |
160 | #else |
161 | typedef int64_t host_long; | |
162 | typedef uint64_t host_ulong; | |
efdea7bf | 163 | #define swabls(x) swab64s(x) |
a86c8f29 | 164 | #define swablss(x) swab64ss(x) |
fb3e5849 FB |
165 | #endif |
166 | ||
fe319756 FB |
167 | #ifdef ELF_USES_RELOCA |
168 | #define SHT_RELOC SHT_RELA | |
169 | #else | |
170 | #define SHT_RELOC SHT_REL | |
171 | #endif | |
172 | ||
67b915a5 FB |
173 | #define EXE_RELOC ELF_RELOC |
174 | #define EXE_SYM ElfW(Sym) | |
175 | ||
176 | #endif /* CONFIG_FORMAT_ELF */ | |
177 | ||
178 | #ifdef CONFIG_FORMAT_COFF | |
179 | ||
67b915a5 FB |
180 | typedef int32_t host_long; |
181 | typedef uint32_t host_ulong; | |
182 | ||
7a2d6d96 PB |
183 | #include "a.out.h" |
184 | ||
67b915a5 FB |
185 | #define FILENAMELEN 256 |
186 | ||
187 | typedef struct coff_sym { | |
188 | struct external_syment *st_syment; | |
189 | char st_name[FILENAMELEN]; | |
190 | uint32_t st_value; | |
191 | int st_size; | |
192 | uint8_t st_type; | |
193 | uint8_t st_shndx; | |
194 | } coff_Sym; | |
195 | ||
196 | typedef struct coff_rel { | |
197 | struct external_reloc *r_reloc; | |
198 | int r_offset; | |
199 | uint8_t r_type; | |
200 | } coff_Rel; | |
201 | ||
202 | #define EXE_RELOC struct coff_rel | |
203 | #define EXE_SYM struct coff_sym | |
204 | ||
205 | #endif /* CONFIG_FORMAT_COFF */ | |
206 | ||
82eec0a1 FB |
207 | #ifdef CONFIG_FORMAT_MACH |
208 | ||
209 | #include <mach-o/loader.h> | |
210 | #include <mach-o/nlist.h> | |
211 | #include <mach-o/reloc.h> | |
212 | #include <mach-o/ppc/reloc.h> | |
213 | ||
214 | # define check_mach_header(x) (x.magic == MH_MAGIC) | |
215 | typedef int32_t host_long; | |
216 | typedef uint32_t host_ulong; | |
217 | ||
218 | struct nlist_extended | |
219 | { | |
220 | union { | |
5fafdf24 TS |
221 | char *n_name; |
222 | long n_strx; | |
82eec0a1 | 223 | } n_un; |
5fafdf24 TS |
224 | unsigned char n_type; |
225 | unsigned char n_sect; | |
82eec0a1 FB |
226 | short st_desc; |
227 | unsigned long st_value; | |
228 | unsigned long st_size; | |
229 | }; | |
230 | ||
231 | #define EXE_RELOC struct relocation_info | |
232 | #define EXE_SYM struct nlist_extended | |
233 | ||
234 | #endif /* CONFIG_FORMAT_MACH */ | |
235 | ||
abcd5da7 | 236 | #include "bswap.h" |
ce11fedc | 237 | |
d219f7e7 FB |
238 | enum { |
239 | OUT_GEN_OP, | |
240 | OUT_CODE, | |
241 | OUT_INDEX_OP, | |
242 | }; | |
243 | ||
367e86e8 | 244 | /* all dynamically generated functions begin with this code */ |
dc99065b | 245 | #define OP_PREFIX "op_" |
367e86e8 | 246 | |
67b915a5 FB |
247 | int do_swap; |
248 | ||
9596ebb7 | 249 | static void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...) |
367e86e8 | 250 | { |
67b915a5 FB |
251 | va_list ap; |
252 | va_start(ap, fmt); | |
253 | fprintf(stderr, "dyngen: "); | |
254 | vfprintf(stderr, fmt, ap); | |
255 | fprintf(stderr, "\n"); | |
256 | va_end(ap); | |
257 | exit(1); | |
258 | } | |
367e86e8 | 259 | |
9596ebb7 | 260 | static void *load_data(int fd, long offset, unsigned int size) |
67b915a5 FB |
261 | { |
262 | char *data; | |
263 | ||
264 | data = malloc(size); | |
265 | if (!data) | |
266 | return NULL; | |
267 | lseek(fd, offset, SEEK_SET); | |
268 | if (read(fd, data, size) != size) { | |
269 | free(data); | |
270 | return NULL; | |
271 | } | |
272 | return data; | |
367e86e8 | 273 | } |
67b915a5 | 274 | |
8fcd3692 | 275 | static int strstart(const char *str, const char *val, const char **ptr) |
67b915a5 FB |
276 | { |
277 | const char *p, *q; | |
278 | p = str; | |
279 | q = val; | |
280 | while (*q != '\0') { | |
281 | if (*p != *q) | |
282 | return 0; | |
283 | p++; | |
284 | q++; | |
285 | } | |
286 | if (ptr) | |
287 | *ptr = p; | |
288 | return 1; | |
289 | } | |
290 | ||
8fcd3692 | 291 | static void pstrcpy(char *buf, int buf_size, const char *str) |
67b915a5 FB |
292 | { |
293 | int c; | |
294 | char *q = buf; | |
295 | ||
296 | if (buf_size <= 0) | |
297 | return; | |
298 | ||
299 | for(;;) { | |
300 | c = *str++; | |
301 | if (c == 0 || q >= buf + buf_size - 1) | |
302 | break; | |
303 | *q++ = c; | |
304 | } | |
305 | *q = '\0'; | |
306 | } | |
307 | ||
8fcd3692 | 308 | static void swab16s(uint16_t *p) |
367e86e8 FB |
309 | { |
310 | *p = bswap16(*p); | |
311 | } | |
312 | ||
8fcd3692 | 313 | static void swab32s(uint32_t *p) |
367e86e8 FB |
314 | { |
315 | *p = bswap32(*p); | |
316 | } | |
317 | ||
8fcd3692 | 318 | static void swab32ss(int32_t *p) |
a86c8f29 TS |
319 | { |
320 | *p = bswap32(*p); | |
321 | } | |
322 | ||
8fcd3692 | 323 | static void swab64s(uint64_t *p) |
367e86e8 FB |
324 | { |
325 | *p = bswap64(*p); | |
326 | } | |
327 | ||
8fcd3692 | 328 | static void swab64ss(int64_t *p) |
a86c8f29 TS |
329 | { |
330 | *p = bswap64(*p); | |
331 | } | |
332 | ||
8fcd3692 | 333 | static uint16_t get16(uint16_t *p) |
67b915a5 FB |
334 | { |
335 | uint16_t val; | |
336 | val = *p; | |
337 | if (do_swap) | |
338 | val = bswap16(val); | |
339 | return val; | |
340 | } | |
341 | ||
8fcd3692 | 342 | static uint32_t get32(uint32_t *p) |
67b915a5 FB |
343 | { |
344 | uint32_t val; | |
345 | val = *p; | |
346 | if (do_swap) | |
347 | val = bswap32(val); | |
348 | return val; | |
349 | } | |
350 | ||
8fcd3692 | 351 | static void put16(uint16_t *p, uint16_t val) |
67b915a5 FB |
352 | { |
353 | if (do_swap) | |
354 | val = bswap16(val); | |
355 | *p = val; | |
356 | } | |
357 | ||
8fcd3692 | 358 | static void put32(uint32_t *p, uint32_t val) |
67b915a5 FB |
359 | { |
360 | if (do_swap) | |
361 | val = bswap32(val); | |
362 | *p = val; | |
363 | } | |
364 | ||
365 | /* executable information */ | |
366 | EXE_SYM *symtab; | |
367 | int nb_syms; | |
368 | int text_shndx; | |
369 | uint8_t *text; | |
370 | EXE_RELOC *relocs; | |
371 | int nb_relocs; | |
372 | ||
373 | #ifdef CONFIG_FORMAT_ELF | |
374 | ||
375 | /* ELF file info */ | |
376 | struct elf_shdr *shdr; | |
377 | uint8_t **sdata; | |
378 | struct elfhdr ehdr; | |
379 | char *strtab; | |
380 | ||
8fcd3692 | 381 | static int elf_must_swap(struct elfhdr *h) |
67b915a5 FB |
382 | { |
383 | union { | |
384 | uint32_t i; | |
385 | uint8_t b[4]; | |
386 | } swaptest; | |
387 | ||
388 | swaptest.i = 1; | |
5fafdf24 | 389 | return (h->e_ident[EI_DATA] == ELFDATA2MSB) != |
67b915a5 FB |
390 | (swaptest.b[0] == 0); |
391 | } | |
3b46e624 | 392 | |
8fcd3692 | 393 | static void elf_swap_ehdr(struct elfhdr *h) |
367e86e8 FB |
394 | { |
395 | swab16s(&h->e_type); /* Object file type */ | |
396 | swab16s(&h-> e_machine); /* Architecture */ | |
397 | swab32s(&h-> e_version); /* Object file version */ | |
ce11fedc FB |
398 | swabls(&h-> e_entry); /* Entry point virtual address */ |
399 | swabls(&h-> e_phoff); /* Program header table file offset */ | |
400 | swabls(&h-> e_shoff); /* Section header table file offset */ | |
367e86e8 FB |
401 | swab32s(&h-> e_flags); /* Processor-specific flags */ |
402 | swab16s(&h-> e_ehsize); /* ELF header size in bytes */ | |
403 | swab16s(&h-> e_phentsize); /* Program header table entry size */ | |
404 | swab16s(&h-> e_phnum); /* Program header table entry count */ | |
405 | swab16s(&h-> e_shentsize); /* Section header table entry size */ | |
406 | swab16s(&h-> e_shnum); /* Section header table entry count */ | |
407 | swab16s(&h-> e_shstrndx); /* Section header string table index */ | |
408 | } | |
409 | ||
8fcd3692 | 410 | static void elf_swap_shdr(struct elf_shdr *h) |
367e86e8 FB |
411 | { |
412 | swab32s(&h-> sh_name); /* Section name (string tbl index) */ | |
413 | swab32s(&h-> sh_type); /* Section type */ | |
ce11fedc FB |
414 | swabls(&h-> sh_flags); /* Section flags */ |
415 | swabls(&h-> sh_addr); /* Section virtual addr at execution */ | |
416 | swabls(&h-> sh_offset); /* Section file offset */ | |
417 | swabls(&h-> sh_size); /* Section size in bytes */ | |
367e86e8 FB |
418 | swab32s(&h-> sh_link); /* Link to another section */ |
419 | swab32s(&h-> sh_info); /* Additional section information */ | |
ce11fedc FB |
420 | swabls(&h-> sh_addralign); /* Section alignment */ |
421 | swabls(&h-> sh_entsize); /* Entry size if section holds table */ | |
367e86e8 FB |
422 | } |
423 | ||
8fcd3692 | 424 | static void elf_swap_phdr(struct elf_phdr *h) |
367e86e8 FB |
425 | { |
426 | swab32s(&h->p_type); /* Segment type */ | |
ce11fedc FB |
427 | swabls(&h->p_offset); /* Segment file offset */ |
428 | swabls(&h->p_vaddr); /* Segment virtual address */ | |
429 | swabls(&h->p_paddr); /* Segment physical address */ | |
430 | swabls(&h->p_filesz); /* Segment size in file */ | |
431 | swabls(&h->p_memsz); /* Segment size in memory */ | |
367e86e8 | 432 | swab32s(&h->p_flags); /* Segment flags */ |
ce11fedc | 433 | swabls(&h->p_align); /* Segment alignment */ |
367e86e8 FB |
434 | } |
435 | ||
8fcd3692 | 436 | static void elf_swap_rel(ELF_RELOC *rel) |
fe319756 FB |
437 | { |
438 | swabls(&rel->r_offset); | |
439 | swabls(&rel->r_info); | |
440 | #ifdef ELF_USES_RELOCA | |
a86c8f29 | 441 | swablss(&rel->r_addend); |
fe319756 FB |
442 | #endif |
443 | } | |
444 | ||
8fcd3692 BS |
445 | static struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, |
446 | const char *shstr, const char *name) | |
67b915a5 FB |
447 | { |
448 | int i; | |
449 | const char *shname; | |
450 | struct elf_shdr *sec; | |
367e86e8 | 451 | |
67b915a5 FB |
452 | for(i = 0; i < shnum; i++) { |
453 | sec = &shdr[i]; | |
454 | if (!sec->sh_name) | |
455 | continue; | |
456 | shname = shstr + sec->sh_name; | |
457 | if (!strcmp(shname, name)) | |
458 | return sec; | |
459 | } | |
460 | return NULL; | |
461 | } | |
462 | ||
8fcd3692 | 463 | static int find_reloc(int sh_index) |
367e86e8 | 464 | { |
67b915a5 FB |
465 | struct elf_shdr *sec; |
466 | int i; | |
467 | ||
468 | for(i = 0; i < ehdr.e_shnum; i++) { | |
469 | sec = &shdr[i]; | |
5fafdf24 | 470 | if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index) |
67b915a5 FB |
471 | return i; |
472 | } | |
473 | return 0; | |
367e86e8 FB |
474 | } |
475 | ||
82eec0a1 FB |
476 | static host_ulong get_rel_offset(EXE_RELOC *rel) |
477 | { | |
478 | return rel->r_offset; | |
479 | } | |
480 | ||
67b915a5 | 481 | static char *get_rel_sym_name(EXE_RELOC *rel) |
367e86e8 | 482 | { |
67b915a5 FB |
483 | return strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
484 | } | |
485 | ||
486 | static char *get_sym_name(EXE_SYM *sym) | |
487 | { | |
488 | return strtab + sym->st_name; | |
489 | } | |
490 | ||
491 | /* load an elf object file */ | |
8fcd3692 | 492 | static int load_object(const char *filename) |
67b915a5 FB |
493 | { |
494 | int fd; | |
495 | struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec; | |
496 | int i, j; | |
497 | ElfW(Sym) *sym; | |
498 | char *shstr; | |
499 | ELF_RELOC *rel; | |
3b46e624 | 500 | |
67b915a5 | 501 | fd = open(filename, O_RDONLY); |
5fafdf24 | 502 | if (fd < 0) |
67b915a5 | 503 | error("can't open file '%s'", filename); |
3b46e624 | 504 | |
67b915a5 FB |
505 | /* Read ELF header. */ |
506 | if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr)) | |
507 | error("unable to read file header"); | |
508 | ||
509 | /* Check ELF identification. */ | |
510 | if (ehdr.e_ident[EI_MAG0] != ELFMAG0 | |
511 | || ehdr.e_ident[EI_MAG1] != ELFMAG1 | |
512 | || ehdr.e_ident[EI_MAG2] != ELFMAG2 | |
513 | || ehdr.e_ident[EI_MAG3] != ELFMAG3 | |
514 | || ehdr.e_ident[EI_VERSION] != EV_CURRENT) { | |
515 | error("bad ELF header"); | |
516 | } | |
517 | ||
518 | do_swap = elf_must_swap(&ehdr); | |
367e86e8 | 519 | if (do_swap) |
67b915a5 FB |
520 | elf_swap_ehdr(&ehdr); |
521 | if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) | |
522 | error("Unsupported ELF class"); | |
523 | if (ehdr.e_type != ET_REL) | |
524 | error("ELF object file expected"); | |
525 | if (ehdr.e_version != EV_CURRENT) | |
526 | error("Invalid ELF version"); | |
527 | if (!elf_check_arch(ehdr.e_machine)) | |
528 | error("Unsupported CPU (e_machine=%d)", ehdr.e_machine); | |
529 | ||
530 | /* read section headers */ | |
531 | shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr)); | |
532 | if (do_swap) { | |
533 | for(i = 0; i < ehdr.e_shnum; i++) { | |
534 | elf_swap_shdr(&shdr[i]); | |
535 | } | |
536 | } | |
537 | ||
538 | /* read all section data */ | |
539 | sdata = malloc(sizeof(void *) * ehdr.e_shnum); | |
540 | memset(sdata, 0, sizeof(void *) * ehdr.e_shnum); | |
3b46e624 | 541 | |
67b915a5 FB |
542 | for(i = 0;i < ehdr.e_shnum; i++) { |
543 | sec = &shdr[i]; | |
544 | if (sec->sh_type != SHT_NOBITS) | |
545 | sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size); | |
546 | } | |
547 | ||
548 | sec = &shdr[ehdr.e_shstrndx]; | |
a86c8f29 | 549 | shstr = (char *)sdata[ehdr.e_shstrndx]; |
67b915a5 FB |
550 | |
551 | /* swap relocations */ | |
552 | for(i = 0; i < ehdr.e_shnum; i++) { | |
553 | sec = &shdr[i]; | |
554 | if (sec->sh_type == SHT_RELOC) { | |
555 | nb_relocs = sec->sh_size / sec->sh_entsize; | |
556 | if (do_swap) { | |
557 | for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++) | |
558 | elf_swap_rel(rel); | |
559 | } | |
560 | } | |
561 | } | |
562 | /* text section */ | |
563 | ||
564 | text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text"); | |
565 | if (!text_sec) | |
566 | error("could not find .text section"); | |
567 | text_shndx = text_sec - shdr; | |
568 | text = sdata[text_shndx]; | |
569 | ||
570 | /* find text relocations, if any */ | |
571 | relocs = NULL; | |
572 | nb_relocs = 0; | |
573 | i = find_reloc(text_shndx); | |
574 | if (i != 0) { | |
575 | relocs = (ELF_RELOC *)sdata[i]; | |
576 | nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize; | |
577 | } | |
578 | ||
579 | symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab"); | |
580 | if (!symtab_sec) | |
581 | error("could not find .symtab section"); | |
582 | strtab_sec = &shdr[symtab_sec->sh_link]; | |
583 | ||
584 | symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr]; | |
a86c8f29 | 585 | strtab = (char *)sdata[symtab_sec->sh_link]; |
3b46e624 | 586 | |
67b915a5 FB |
587 | nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym)); |
588 | if (do_swap) { | |
589 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { | |
590 | swab32s(&sym->st_name); | |
591 | swabls(&sym->st_value); | |
592 | swabls(&sym->st_size); | |
593 | swab16s(&sym->st_shndx); | |
594 | } | |
595 | } | |
596 | close(fd); | |
597 | return 0; | |
598 | } | |
599 | ||
600 | #endif /* CONFIG_FORMAT_ELF */ | |
601 | ||
602 | #ifdef CONFIG_FORMAT_COFF | |
603 | ||
604 | /* COFF file info */ | |
605 | struct external_scnhdr *shdr; | |
606 | uint8_t **sdata; | |
607 | struct external_filehdr fhdr; | |
608 | struct external_syment *coff_symtab; | |
609 | char *strtab; | |
610 | int coff_text_shndx, coff_data_shndx; | |
611 | ||
612 | int data_shndx; | |
613 | ||
614 | #define STRTAB_SIZE 4 | |
615 | ||
616 | #define DIR32 0x06 | |
617 | #define DISP32 0x14 | |
618 | ||
619 | #define T_FUNCTION 0x20 | |
620 | #define C_EXTERNAL 2 | |
621 | ||
622 | void sym_ent_name(struct external_syment *ext_sym, EXE_SYM *sym) | |
623 | { | |
624 | char *q; | |
625 | int c, i, len; | |
3b46e624 | 626 | |
67b915a5 FB |
627 | if (ext_sym->e.e.e_zeroes != 0) { |
628 | q = sym->st_name; | |
629 | for(i = 0; i < 8; i++) { | |
630 | c = ext_sym->e.e_name[i]; | |
631 | if (c == '\0') | |
632 | break; | |
633 | *q++ = c; | |
634 | } | |
635 | *q = '\0'; | |
636 | } else { | |
637 | pstrcpy(sym->st_name, sizeof(sym->st_name), strtab + ext_sym->e.e.e_offset); | |
638 | } | |
639 | ||
640 | /* now convert the name to a C name (suppress the leading '_') */ | |
641 | if (sym->st_name[0] == '_') { | |
642 | len = strlen(sym->st_name); | |
643 | memmove(sym->st_name, sym->st_name + 1, len - 1); | |
644 | sym->st_name[len - 1] = '\0'; | |
645 | } | |
367e86e8 FB |
646 | } |
647 | ||
67b915a5 | 648 | char *name_for_dotdata(struct coff_rel *rel) |
367e86e8 | 649 | { |
67b915a5 FB |
650 | int i; |
651 | struct coff_sym *sym; | |
652 | uint32_t text_data; | |
653 | ||
654 | text_data = *(uint32_t *)(text + rel->r_offset); | |
655 | ||
656 | for (i = 0, sym = symtab; i < nb_syms; i++, sym++) { | |
657 | if (sym->st_syment->e_scnum == data_shndx && | |
658 | text_data >= sym->st_value && | |
659 | text_data < sym->st_value + sym->st_size) { | |
3b46e624 | 660 | |
67b915a5 FB |
661 | return sym->st_name; |
662 | ||
663 | } | |
664 | } | |
665 | return NULL; | |
367e86e8 FB |
666 | } |
667 | ||
67b915a5 | 668 | static char *get_sym_name(EXE_SYM *sym) |
367e86e8 | 669 | { |
67b915a5 | 670 | return sym->st_name; |
367e86e8 FB |
671 | } |
672 | ||
67b915a5 | 673 | static char *get_rel_sym_name(EXE_RELOC *rel) |
367e86e8 | 674 | { |
67b915a5 FB |
675 | char *name; |
676 | name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx)); | |
677 | if (!strcmp(name, ".data")) | |
678 | name = name_for_dotdata(rel); | |
b48a8bb6 FB |
679 | if (name[0] == '.') |
680 | return NULL; | |
67b915a5 | 681 | return name; |
367e86e8 FB |
682 | } |
683 | ||
82eec0a1 FB |
684 | static host_ulong get_rel_offset(EXE_RELOC *rel) |
685 | { | |
686 | return rel->r_offset; | |
687 | } | |
688 | ||
67b915a5 | 689 | struct external_scnhdr *find_coff_section(struct external_scnhdr *shdr, int shnum, const char *name) |
367e86e8 FB |
690 | { |
691 | int i; | |
692 | const char *shname; | |
67b915a5 | 693 | struct external_scnhdr *sec; |
367e86e8 FB |
694 | |
695 | for(i = 0; i < shnum; i++) { | |
696 | sec = &shdr[i]; | |
67b915a5 | 697 | if (!sec->s_name) |
367e86e8 | 698 | continue; |
67b915a5 | 699 | shname = sec->s_name; |
367e86e8 FB |
700 | if (!strcmp(shname, name)) |
701 | return sec; | |
702 | } | |
703 | return NULL; | |
704 | } | |
705 | ||
67b915a5 FB |
706 | /* load a coff object file */ |
707 | int load_object(const char *filename) | |
fe319756 | 708 | { |
67b915a5 FB |
709 | int fd; |
710 | struct external_scnhdr *sec, *text_sec, *data_sec; | |
fe319756 | 711 | int i; |
67b915a5 FB |
712 | struct external_syment *ext_sym; |
713 | struct external_reloc *coff_relocs; | |
714 | struct external_reloc *ext_rel; | |
715 | uint32_t *n_strtab; | |
716 | EXE_SYM *sym; | |
717 | EXE_RELOC *rel; | |
2331d91e FB |
718 | const char *p; |
719 | int aux_size, j; | |
5fafdf24 TS |
720 | |
721 | fd = open(filename, O_RDONLY | |
67b915a5 FB |
722 | #ifdef _WIN32 |
723 | | O_BINARY | |
724 | #endif | |
725 | ); | |
5fafdf24 | 726 | if (fd < 0) |
67b915a5 | 727 | error("can't open file '%s'", filename); |
3b46e624 | 728 | |
67b915a5 FB |
729 | /* Read COFF header. */ |
730 | if (read(fd, &fhdr, sizeof (fhdr)) != sizeof (fhdr)) | |
731 | error("unable to read file header"); | |
fe319756 | 732 | |
67b915a5 FB |
733 | /* Check COFF identification. */ |
734 | if (fhdr.f_magic != I386MAGIC) { | |
735 | error("bad COFF header"); | |
736 | } | |
737 | do_swap = 0; | |
738 | ||
739 | /* read section headers */ | |
740 | shdr = load_data(fd, sizeof(struct external_filehdr) + fhdr.f_opthdr, fhdr.f_nscns * sizeof(struct external_scnhdr)); | |
5fafdf24 | 741 | |
67b915a5 FB |
742 | /* read all section data */ |
743 | sdata = malloc(sizeof(void *) * fhdr.f_nscns); | |
744 | memset(sdata, 0, sizeof(void *) * fhdr.f_nscns); | |
3b46e624 | 745 | |
67b915a5 | 746 | for(i = 0;i < fhdr.f_nscns; i++) { |
fe319756 | 747 | sec = &shdr[i]; |
67b915a5 FB |
748 | if (!strstart(sec->s_name, ".bss", &p)) |
749 | sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size); | |
fe319756 | 750 | } |
fe319756 | 751 | |
367e86e8 | 752 | |
67b915a5 FB |
753 | /* text section */ |
754 | text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text"); | |
755 | if (!text_sec) | |
756 | error("could not find .text section"); | |
757 | coff_text_shndx = text_sec - shdr; | |
758 | text = sdata[coff_text_shndx]; | |
759 | ||
760 | /* data section */ | |
761 | data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data"); | |
762 | if (!data_sec) | |
763 | error("could not find .data section"); | |
764 | coff_data_shndx = data_sec - shdr; | |
3b46e624 | 765 | |
67b915a5 FB |
766 | coff_symtab = load_data(fd, fhdr.f_symptr, fhdr.f_nsyms*SYMESZ); |
767 | for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) { | |
768 | for(i=0;i<8;i++) | |
769 | printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]); | |
770 | printf("\n"); | |
367e86e8 | 771 | } |
367e86e8 | 772 | |
67b915a5 FB |
773 | |
774 | n_strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), STRTAB_SIZE); | |
5fafdf24 | 775 | strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), *n_strtab); |
3b46e624 | 776 | |
67b915a5 FB |
777 | nb_syms = fhdr.f_nsyms; |
778 | ||
779 | for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) { | |
780 | if (strstart(ext_sym->e.e_name, ".text", NULL)) | |
781 | text_shndx = ext_sym->e_scnum; | |
782 | if (strstart(ext_sym->e.e_name, ".data", NULL)) | |
783 | data_shndx = ext_sym->e_scnum; | |
367e86e8 | 784 | } |
67b915a5 FB |
785 | |
786 | /* set coff symbol */ | |
787 | symtab = malloc(sizeof(struct coff_sym) * nb_syms); | |
788 | ||
67b915a5 FB |
789 | for (i = 0, ext_sym = coff_symtab, sym = symtab; i < nb_syms; i++, ext_sym++, sym++) { |
790 | memset(sym, 0, sizeof(*sym)); | |
791 | sym->st_syment = ext_sym; | |
792 | sym_ent_name(ext_sym, sym); | |
793 | sym->st_value = ext_sym->e_value; | |
794 | ||
795 | aux_size = *(int8_t *)ext_sym->e_numaux; | |
796 | if (ext_sym->e_scnum == text_shndx && ext_sym->e_type == T_FUNCTION) { | |
797 | for (j = aux_size + 1; j < nb_syms - i; j++) { | |
798 | if ((ext_sym + j)->e_scnum == text_shndx && | |
799 | (ext_sym + j)->e_type == T_FUNCTION ){ | |
800 | sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value; | |
801 | break; | |
802 | } else if (j == nb_syms - i - 1) { | |
803 | sec = &shdr[coff_text_shndx]; | |
804 | sym->st_size = sec->s_size - ext_sym->e_value; | |
805 | break; | |
806 | } | |
807 | } | |
808 | } else if (ext_sym->e_scnum == data_shndx && *(uint8_t *)ext_sym->e_sclass == C_EXTERNAL) { | |
809 | for (j = aux_size + 1; j < nb_syms - i; j++) { | |
810 | if ((ext_sym + j)->e_scnum == data_shndx) { | |
811 | sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value; | |
812 | break; | |
813 | } else if (j == nb_syms - i - 1) { | |
814 | sec = &shdr[coff_data_shndx]; | |
815 | sym->st_size = sec->s_size - ext_sym->e_value; | |
816 | break; | |
817 | } | |
818 | } | |
819 | } else { | |
820 | sym->st_size = 0; | |
821 | } | |
3b46e624 | 822 | |
67b915a5 FB |
823 | sym->st_type = ext_sym->e_type; |
824 | sym->st_shndx = ext_sym->e_scnum; | |
825 | } | |
826 | ||
3b46e624 | 827 | |
67b915a5 FB |
828 | /* find text relocations, if any */ |
829 | sec = &shdr[coff_text_shndx]; | |
830 | coff_relocs = load_data(fd, sec->s_relptr, sec->s_nreloc*RELSZ); | |
831 | nb_relocs = sec->s_nreloc; | |
832 | ||
833 | /* set coff relocation */ | |
834 | relocs = malloc(sizeof(struct coff_rel) * nb_relocs); | |
5fafdf24 | 835 | for (i = 0, ext_rel = coff_relocs, rel = relocs; i < nb_relocs; |
67b915a5 FB |
836 | i++, ext_rel++, rel++) { |
837 | memset(rel, 0, sizeof(*rel)); | |
838 | rel->r_reloc = ext_rel; | |
839 | rel->r_offset = *(uint32_t *)ext_rel->r_vaddr; | |
840 | rel->r_type = *(uint16_t *)ext_rel->r_type; | |
841 | } | |
842 | return 0; | |
367e86e8 FB |
843 | } |
844 | ||
67b915a5 FB |
845 | #endif /* CONFIG_FORMAT_COFF */ |
846 | ||
82eec0a1 FB |
847 | #ifdef CONFIG_FORMAT_MACH |
848 | ||
849 | /* File Header */ | |
850 | struct mach_header mach_hdr; | |
851 | ||
852 | /* commands */ | |
853 | struct segment_command *segment = 0; | |
854 | struct dysymtab_command *dysymtabcmd = 0; | |
855 | struct symtab_command *symtabcmd = 0; | |
856 | ||
857 | /* section */ | |
858 | struct section *section_hdr; | |
859 | struct section *text_sec_hdr; | |
860 | uint8_t **sdata; | |
861 | ||
862 | /* relocs */ | |
863 | struct relocation_info *relocs; | |
5fafdf24 | 864 | |
82eec0a1 FB |
865 | /* symbols */ |
866 | EXE_SYM *symtab; | |
867 | struct nlist *symtab_std; | |
868 | char *strtab; | |
869 | ||
870 | /* indirect symbols */ | |
871 | uint32_t *tocdylib; | |
872 | ||
873 | /* Utility functions */ | |
874 | ||
875 | static inline char *find_str_by_index(int index) | |
876 | { | |
877 | return strtab+index; | |
878 | } | |
879 | ||
880 | /* Used by dyngen common code */ | |
881 | static char *get_sym_name(EXE_SYM *sym) | |
882 | { | |
883 | char *name = find_str_by_index(sym->n_un.n_strx); | |
5fafdf24 | 884 | |
82eec0a1 FB |
885 | if ( sym->n_type & N_STAB ) /* Debug symbols are ignored */ |
886 | return "debug"; | |
3b46e624 | 887 | |
82eec0a1 FB |
888 | if(!name) |
889 | return name; | |
890 | if(name[0]=='_') | |
891 | return name + 1; | |
892 | else | |
893 | return name; | |
894 | } | |
895 | ||
896 | /* find a section index given its segname, sectname */ | |
5fafdf24 | 897 | static int find_mach_sec_index(struct section *section_hdr, int shnum, const char *segname, |
82eec0a1 FB |
898 | const char *sectname) |
899 | { | |
900 | int i; | |
901 | struct section *sec = section_hdr; | |
902 | ||
903 | for(i = 0; i < shnum; i++, sec++) { | |
904 | if (!sec->segname || !sec->sectname) | |
905 | continue; | |
906 | if (!strcmp(sec->sectname, sectname) && !strcmp(sec->segname, segname)) | |
907 | return i; | |
908 | } | |
909 | return -1; | |
910 | } | |
911 | ||
912 | /* find a section header given its segname, sectname */ | |
5fafdf24 | 913 | struct section *find_mach_sec_hdr(struct section *section_hdr, int shnum, const char *segname, |
82eec0a1 FB |
914 | const char *sectname) |
915 | { | |
916 | int index = find_mach_sec_index(section_hdr, shnum, segname, sectname); | |
917 | if(index == -1) | |
918 | return NULL; | |
919 | return section_hdr+index; | |
920 | } | |
921 | ||
922 | ||
923 | static inline void fetch_next_pair_value(struct relocation_info * rel, unsigned int *value) | |
924 | { | |
925 | struct scattered_relocation_info * scarel; | |
5fafdf24 | 926 | |
82eec0a1 FB |
927 | if(R_SCATTERED & rel->r_address) { |
928 | scarel = (struct scattered_relocation_info*)rel; | |
929 | if(scarel->r_type != PPC_RELOC_PAIR) | |
930 | error("fetch_next_pair_value: looking for a pair which was not found (1)"); | |
931 | *value = scarel->r_value; | |
932 | } else { | |
933 | if(rel->r_type != PPC_RELOC_PAIR) | |
934 | error("fetch_next_pair_value: looking for a pair which was not found (2)"); | |
935 | *value = rel->r_address; | |
936 | } | |
937 | } | |
938 | ||
939 | /* find a sym name given its value, in a section number */ | |
940 | static const char * find_sym_with_value_and_sec_number( int value, int sectnum, int * offset ) | |
941 | { | |
942 | int i, ret = -1; | |
5fafdf24 | 943 | |
82eec0a1 FB |
944 | for( i = 0 ; i < nb_syms; i++ ) |
945 | { | |
946 | if( !(symtab[i].n_type & N_STAB) && (symtab[i].n_type & N_SECT) && | |
947 | (symtab[i].n_sect == sectnum) && (symtab[i].st_value <= value) ) | |
948 | { | |
949 | if( (ret<0) || (symtab[i].st_value >= symtab[ret].st_value) ) | |
950 | ret = i; | |
951 | } | |
952 | } | |
953 | if( ret < 0 ) { | |
954 | *offset = 0; | |
955 | return 0; | |
956 | } else { | |
957 | *offset = value - symtab[ret].st_value; | |
958 | return get_sym_name(&symtab[ret]); | |
959 | } | |
960 | } | |
961 | ||
5fafdf24 TS |
962 | /* |
963 | * Find symbol name given a (virtual) address, and a section which is of type | |
82eec0a1 FB |
964 | * S_NON_LAZY_SYMBOL_POINTERS or S_LAZY_SYMBOL_POINTERS or S_SYMBOL_STUBS |
965 | */ | |
966 | static const char * find_reloc_name_in_sec_ptr(int address, struct section * sec_hdr) | |
967 | { | |
968 | unsigned int tocindex, symindex, size; | |
969 | const char *name = 0; | |
3b46e624 | 970 | |
82eec0a1 FB |
971 | /* Sanity check */ |
972 | if(!( address >= sec_hdr->addr && address < (sec_hdr->addr + sec_hdr->size) ) ) | |
973 | return (char*)0; | |
3b46e624 | 974 | |
82eec0a1 FB |
975 | if( sec_hdr->flags & S_SYMBOL_STUBS ){ |
976 | size = sec_hdr->reserved2; | |
977 | if(size == 0) | |
978 | error("size = 0"); | |
3b46e624 | 979 | |
82eec0a1 FB |
980 | } |
981 | else if( sec_hdr->flags & S_LAZY_SYMBOL_POINTERS || | |
982 | sec_hdr->flags & S_NON_LAZY_SYMBOL_POINTERS) | |
983 | size = sizeof(unsigned long); | |
984 | else | |
985 | return 0; | |
3b46e624 | 986 | |
82eec0a1 FB |
987 | /* Compute our index in toc */ |
988 | tocindex = (address - sec_hdr->addr)/size; | |
989 | symindex = tocdylib[sec_hdr->reserved1 + tocindex]; | |
5fafdf24 | 990 | |
82eec0a1 FB |
991 | name = get_sym_name(&symtab[symindex]); |
992 | ||
993 | return name; | |
994 | } | |
995 | ||
996 | static const char * find_reloc_name_given_its_address(int address) | |
997 | { | |
998 | unsigned int i; | |
999 | for(i = 0; i < segment->nsects ; i++) | |
1000 | { | |
1001 | const char * name = find_reloc_name_in_sec_ptr(address, §ion_hdr[i]); | |
1002 | if((long)name != -1) | |
1003 | return name; | |
1004 | } | |
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | static const char * get_reloc_name(EXE_RELOC * rel, int * sslide) | |
1009 | { | |
1010 | char * name = 0; | |
1011 | struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel; | |
1012 | int sectnum = rel->r_symbolnum; | |
1013 | int sectoffset; | |
1014 | int other_half=0; | |
5fafdf24 | 1015 | |
82eec0a1 FB |
1016 | /* init the slide value */ |
1017 | *sslide = 0; | |
5fafdf24 | 1018 | |
82eec0a1 FB |
1019 | if(R_SCATTERED & rel->r_address) |
1020 | return (char *)find_reloc_name_given_its_address(sca_rel->r_value); | |
1021 | ||
1022 | if(rel->r_extern) | |
1023 | { | |
1024 | /* ignore debug sym */ | |
5fafdf24 | 1025 | if ( symtab[rel->r_symbolnum].n_type & N_STAB ) |
82eec0a1 FB |
1026 | return 0; |
1027 | return get_sym_name(&symtab[rel->r_symbolnum]); | |
1028 | } | |
1029 | ||
1030 | /* Intruction contains an offset to the symbols pointed to, in the rel->r_symbolnum section */ | |
1031 | sectoffset = *(uint32_t *)(text + rel->r_address) & 0xffff; | |
3b46e624 | 1032 | |
82eec0a1 FB |
1033 | if(sectnum==0xffffff) |
1034 | return 0; | |
1035 | ||
1036 | /* Sanity Check */ | |
1037 | if(sectnum > segment->nsects) | |
1038 | error("sectnum > segment->nsects"); | |
1039 | ||
1040 | switch(rel->r_type) | |
1041 | { | |
91aa5d49 | 1042 | case PPC_RELOC_LO16: fetch_next_pair_value(rel+1, &other_half); sectoffset |= (other_half << 16); |
82eec0a1 | 1043 | break; |
91aa5d49 | 1044 | case PPC_RELOC_HI16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset << 16) | (uint16_t)(other_half & 0xffff); |
82eec0a1 | 1045 | break; |
91aa5d49 | 1046 | case PPC_RELOC_HA16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset << 16) + (int16_t)(other_half & 0xffff); |
82eec0a1 FB |
1047 | break; |
1048 | case PPC_RELOC_BR24: | |
1049 | sectoffset = ( *(uint32_t *)(text + rel->r_address) & 0x03fffffc ); | |
1050 | if (sectoffset & 0x02000000) sectoffset |= 0xfc000000; | |
1051 | break; | |
1052 | default: | |
1053 | error("switch(rel->type) not found"); | |
1054 | } | |
1055 | ||
1056 | if(rel->r_pcrel) | |
1057 | sectoffset += rel->r_address; | |
3b46e624 | 1058 | |
82eec0a1 FB |
1059 | if (rel->r_type == PPC_RELOC_BR24) |
1060 | name = (char *)find_reloc_name_in_sec_ptr((int)sectoffset, §ion_hdr[sectnum-1]); | |
1061 | ||
1062 | /* search it in the full symbol list, if not found */ | |
1063 | if(!name) | |
1064 | name = (char *)find_sym_with_value_and_sec_number(sectoffset, sectnum, sslide); | |
5fafdf24 | 1065 | |
82eec0a1 FB |
1066 | return name; |
1067 | } | |
1068 | ||
1069 | /* Used by dyngen common code */ | |
1070 | static const char * get_rel_sym_name(EXE_RELOC * rel) | |
1071 | { | |
1072 | int sslide; | |
1073 | return get_reloc_name( rel, &sslide); | |
1074 | } | |
1075 | ||
1076 | /* Used by dyngen common code */ | |
1077 | static host_ulong get_rel_offset(EXE_RELOC *rel) | |
1078 | { | |
1079 | struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel; | |
1080 | if(R_SCATTERED & rel->r_address) | |
1081 | return sca_rel->r_address; | |
1082 | else | |
1083 | return rel->r_address; | |
1084 | } | |
1085 | ||
1086 | /* load a mach-o object file */ | |
1087 | int load_object(const char *filename) | |
1088 | { | |
1089 | int fd; | |
1090 | unsigned int offset_to_segment = 0; | |
1091 | unsigned int offset_to_dysymtab = 0; | |
1092 | unsigned int offset_to_symtab = 0; | |
1093 | struct load_command lc; | |
1094 | unsigned int i, j; | |
1095 | EXE_SYM *sym; | |
1096 | struct nlist *syment; | |
3b46e624 | 1097 | |
82eec0a1 | 1098 | fd = open(filename, O_RDONLY); |
5fafdf24 | 1099 | if (fd < 0) |
82eec0a1 | 1100 | error("can't open file '%s'", filename); |
3b46e624 | 1101 | |
82eec0a1 FB |
1102 | /* Read Mach header. */ |
1103 | if (read(fd, &mach_hdr, sizeof (mach_hdr)) != sizeof (mach_hdr)) | |
1104 | error("unable to read file header"); | |
1105 | ||
1106 | /* Check Mach identification. */ | |
1107 | if (!check_mach_header(mach_hdr)) { | |
1108 | error("bad Mach header"); | |
1109 | } | |
3b46e624 | 1110 | |
82eec0a1 FB |
1111 | if (mach_hdr.cputype != CPU_TYPE_POWERPC) |
1112 | error("Unsupported CPU"); | |
3b46e624 | 1113 | |
82eec0a1 FB |
1114 | if (mach_hdr.filetype != MH_OBJECT) |
1115 | error("Unsupported Mach Object"); | |
3b46e624 | 1116 | |
82eec0a1 FB |
1117 | /* read segment headers */ |
1118 | for(i=0, j=sizeof(mach_hdr); i<mach_hdr.ncmds ; i++) | |
1119 | { | |
1120 | if(read(fd, &lc, sizeof(struct load_command)) != sizeof(struct load_command)) | |
1121 | error("unable to read load_command"); | |
1122 | if(lc.cmd == LC_SEGMENT) | |
1123 | { | |
1124 | offset_to_segment = j; | |
1125 | lseek(fd, offset_to_segment, SEEK_SET); | |
1126 | segment = malloc(sizeof(struct segment_command)); | |
1127 | if(read(fd, segment, sizeof(struct segment_command)) != sizeof(struct segment_command)) | |
1128 | error("unable to read LC_SEGMENT"); | |
1129 | } | |
1130 | if(lc.cmd == LC_DYSYMTAB) | |
1131 | { | |
1132 | offset_to_dysymtab = j; | |
1133 | lseek(fd, offset_to_dysymtab, SEEK_SET); | |
1134 | dysymtabcmd = malloc(sizeof(struct dysymtab_command)); | |
1135 | if(read(fd, dysymtabcmd, sizeof(struct dysymtab_command)) != sizeof(struct dysymtab_command)) | |
1136 | error("unable to read LC_DYSYMTAB"); | |
1137 | } | |
1138 | if(lc.cmd == LC_SYMTAB) | |
1139 | { | |
1140 | offset_to_symtab = j; | |
1141 | lseek(fd, offset_to_symtab, SEEK_SET); | |
1142 | symtabcmd = malloc(sizeof(struct symtab_command)); | |
1143 | if(read(fd, symtabcmd, sizeof(struct symtab_command)) != sizeof(struct symtab_command)) | |
1144 | error("unable to read LC_SYMTAB"); | |
1145 | } | |
1146 | j+=lc.cmdsize; | |
1147 | ||
1148 | lseek(fd, j, SEEK_SET); | |
1149 | } | |
1150 | ||
1151 | if(!segment) | |
1152 | error("unable to find LC_SEGMENT"); | |
1153 | ||
1154 | /* read section headers */ | |
1155 | section_hdr = load_data(fd, offset_to_segment + sizeof(struct segment_command), segment->nsects * sizeof(struct section)); | |
1156 | ||
1157 | /* read all section data */ | |
1158 | sdata = (uint8_t **)malloc(sizeof(void *) * segment->nsects); | |
1159 | memset(sdata, 0, sizeof(void *) * segment->nsects); | |
3b46e624 | 1160 | |
82eec0a1 FB |
1161 | /* Load the data in section data */ |
1162 | for(i = 0; i < segment->nsects; i++) { | |
1163 | sdata[i] = load_data(fd, section_hdr[i].offset, section_hdr[i].size); | |
1164 | } | |
5fafdf24 | 1165 | |
82eec0a1 FB |
1166 | /* text section */ |
1167 | text_sec_hdr = find_mach_sec_hdr(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT); | |
1168 | i = find_mach_sec_index(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT); | |
1169 | if (i == -1 || !text_sec_hdr) | |
1170 | error("could not find __TEXT,__text section"); | |
1171 | text = sdata[i]; | |
5fafdf24 | 1172 | |
82eec0a1 FB |
1173 | /* Make sure dysym was loaded */ |
1174 | if(!(int)dysymtabcmd) | |
1175 | error("could not find __DYSYMTAB segment"); | |
3b46e624 | 1176 | |
82eec0a1 FB |
1177 | /* read the table of content of the indirect sym */ |
1178 | tocdylib = load_data( fd, dysymtabcmd->indirectsymoff, dysymtabcmd->nindirectsyms * sizeof(uint32_t) ); | |
3b46e624 | 1179 | |
82eec0a1 FB |
1180 | /* Make sure symtab was loaded */ |
1181 | if(!(int)symtabcmd) | |
1182 | error("could not find __SYMTAB segment"); | |
1183 | nb_syms = symtabcmd->nsyms; | |
1184 | ||
1185 | symtab_std = load_data(fd, symtabcmd->symoff, symtabcmd->nsyms * sizeof(struct nlist)); | |
1186 | strtab = load_data(fd, symtabcmd->stroff, symtabcmd->strsize); | |
5fafdf24 | 1187 | |
82eec0a1 | 1188 | symtab = malloc(sizeof(EXE_SYM) * nb_syms); |
5fafdf24 | 1189 | |
82eec0a1 FB |
1190 | /* Now transform the symtab, to an extended version, with the sym size, and the C name */ |
1191 | for(i = 0, sym = symtab, syment = symtab_std; i < nb_syms; i++, sym++, syment++) { | |
82eec0a1 FB |
1192 | struct nlist *sym_follow, *sym_next = 0; |
1193 | unsigned int j; | |
82eec0a1 | 1194 | memset(sym, 0, sizeof(*sym)); |
3b46e624 | 1195 | |
91aa5d49 | 1196 | if ( syment->n_type & N_STAB ) /* Debug symbols are skipped */ |
82eec0a1 | 1197 | continue; |
3b46e624 | 1198 | |
82eec0a1 | 1199 | memcpy(sym, syment, sizeof(*syment)); |
3b46e624 | 1200 | |
82eec0a1 FB |
1201 | /* Find the following symbol in order to get the current symbol size */ |
1202 | for(j = 0, sym_follow = symtab_std; j < nb_syms; j++, sym_follow++) { | |
1203 | if ( sym_follow->n_sect != 1 || sym_follow->n_type & N_STAB || !(sym_follow->n_value > sym->st_value)) | |
1204 | continue; | |
1205 | if(!sym_next) { | |
1206 | sym_next = sym_follow; | |
1207 | continue; | |
1208 | } | |
1209 | if(!(sym_next->n_value > sym_follow->n_value)) | |
1210 | continue; | |
1211 | sym_next = sym_follow; | |
1212 | } | |
1213 | if(sym_next) | |
1214 | sym->st_size = sym_next->n_value - sym->st_value; | |
1215 | else | |
1216 | sym->st_size = text_sec_hdr->size - sym->st_value; | |
1217 | } | |
5fafdf24 | 1218 | |
82eec0a1 FB |
1219 | /* Find Reloc */ |
1220 | relocs = load_data(fd, text_sec_hdr->reloff, text_sec_hdr->nreloc * sizeof(struct relocation_info)); | |
1221 | nb_relocs = text_sec_hdr->nreloc; | |
1222 | ||
1223 | close(fd); | |
1224 | return 0; | |
1225 | } | |
1226 | ||
1227 | #endif /* CONFIG_FORMAT_MACH */ | |
1228 | ||
57fec1fe | 1229 | /* return true if the expression is a label reference */ |
8fcd3692 | 1230 | static int get_reloc_expr(char *name, int name_size, const char *sym_name) |
bef79c34 FB |
1231 | { |
1232 | const char *p; | |
1233 | ||
1234 | if (strstart(sym_name, "__op_param", &p)) { | |
1235 | snprintf(name, name_size, "param%s", p); | |
1236 | } else if (strstart(sym_name, "__op_gen_label", &p)) { | |
57fec1fe FB |
1237 | snprintf(name, name_size, "param%s", p); |
1238 | return 1; | |
bef79c34 | 1239 | } else { |
f54b3f92 | 1240 | #if defined(HOST_SPARC) || defined(HOST_HPPA) |
bef79c34 | 1241 | if (sym_name[0] == '.') |
4bb3973f | 1242 | snprintf(name, name_size, |
bef79c34 FB |
1243 | "(long)(&__dot_%s)", |
1244 | sym_name + 1); | |
1245 | else | |
1246 | #endif | |
1247 | snprintf(name, name_size, "(long)(&%s)", sym_name); | |
1248 | } | |
57fec1fe | 1249 | return 0; |
bef79c34 FB |
1250 | } |
1251 | ||
b8076a74 FB |
1252 | #ifdef HOST_IA64 |
1253 | ||
1254 | #define PLT_ENTRY_SIZE 16 /* 1 bundle containing "brl" */ | |
1255 | ||
1256 | struct plt_entry { | |
1257 | struct plt_entry *next; | |
1258 | const char *name; | |
1259 | unsigned long addend; | |
1260 | } *plt_list; | |
1261 | ||
1262 | static int | |
1263 | get_plt_index (const char *name, unsigned long addend) | |
1264 | { | |
1265 | struct plt_entry *plt, *prev= NULL; | |
1266 | int index = 0; | |
1267 | ||
1268 | /* see if we already have an entry for this target: */ | |
1269 | for (plt = plt_list; plt; ++index, prev = plt, plt = plt->next) | |
1270 | if (strcmp(plt->name, name) == 0 && plt->addend == addend) | |
1271 | return index; | |
1272 | ||
1273 | /* nope; create a new PLT entry: */ | |
1274 | ||
1275 | plt = malloc(sizeof(*plt)); | |
1276 | if (!plt) { | |
1277 | perror("malloc"); | |
1278 | exit(1); | |
1279 | } | |
1280 | memset(plt, 0, sizeof(*plt)); | |
1281 | plt->name = strdup(name); | |
1282 | plt->addend = addend; | |
1283 | ||
1284 | /* append to plt-list: */ | |
1285 | if (prev) | |
1286 | prev->next = plt; | |
1287 | else | |
1288 | plt_list = plt; | |
1289 | return index; | |
1290 | } | |
1291 | ||
1292 | #endif | |
1293 | ||
367e86e8 FB |
1294 | #define MAX_ARGS 3 |
1295 | ||
1296 | /* generate op code */ | |
8fcd3692 BS |
1297 | static void gen_code(const char *name, host_ulong offset, host_ulong size, |
1298 | FILE *outfile, int gen_switch) | |
367e86e8 FB |
1299 | { |
1300 | int copy_size = 0; | |
1301 | uint8_t *p_start, *p_end; | |
ae228531 | 1302 | host_ulong start_offset; |
ce11fedc | 1303 | int nb_args, i, n; |
367e86e8 FB |
1304 | uint8_t args_present[MAX_ARGS]; |
1305 | const char *sym_name, *p; | |
67b915a5 | 1306 | EXE_RELOC *rel; |
367e86e8 | 1307 | |
ae228531 FB |
1308 | /* Compute exact size excluding prologue and epilogue instructions. |
1309 | * Increment start_offset to skip epilogue instructions, then compute | |
1310 | * copy_size the indicate the size of the remaining instructions (in | |
1311 | * bytes). | |
1312 | */ | |
367e86e8 FB |
1313 | p_start = text + offset; |
1314 | p_end = p_start + size; | |
ae228531 | 1315 | start_offset = offset; |
c4687878 | 1316 | #if defined(HOST_I386) || defined(HOST_X86_64) |
67b915a5 FB |
1317 | #ifdef CONFIG_FORMAT_COFF |
1318 | { | |
1319 | uint8_t *p; | |
1320 | p = p_end - 1; | |
1321 | if (p == p_start) | |
1322 | error("empty code for %s", name); | |
1323 | while (*p != 0xc3) { | |
1324 | p--; | |
1325 | if (p <= p_start) | |
d4e8164f | 1326 | error("ret or jmp expected at the end of %s", name); |
367e86e8 | 1327 | } |
67b915a5 FB |
1328 | copy_size = p - p_start; |
1329 | } | |
1330 | #else | |
1331 | { | |
1332 | int len; | |
1333 | len = p_end - p_start; | |
1334 | if (len == 0) | |
1335 | error("empty code for %s", name); | |
1336 | if (p_end[-1] == 0xc3) { | |
1337 | len--; | |
1338 | } else { | |
1339 | error("ret or jmp expected at the end of %s", name); | |
367e86e8 | 1340 | } |
67b915a5 FB |
1341 | copy_size = len; |
1342 | } | |
3b46e624 | 1343 | #endif |
67b915a5 FB |
1344 | #elif defined(HOST_PPC) |
1345 | { | |
1346 | uint8_t *p; | |
1347 | p = (void *)(p_end - 4); | |
1348 | if (p == p_start) | |
1349 | error("empty code for %s", name); | |
1350 | if (get32((uint32_t *)p) != 0x4e800020) | |
1351 | error("blr expected at the end of %s", name); | |
1352 | copy_size = p - p_start; | |
1353 | } | |
1354 | #elif defined(HOST_S390) | |
1355 | { | |
1356 | uint8_t *p; | |
1357 | p = (void *)(p_end - 2); | |
1358 | if (p == p_start) | |
1359 | error("empty code for %s", name); | |
76d83bde TS |
1360 | if ((get16((uint16_t *)p) & 0xfff0) != 0x07f0) |
1361 | error("br expected at the end of %s", name); | |
67b915a5 FB |
1362 | copy_size = p - p_start; |
1363 | } | |
1364 | #elif defined(HOST_ALPHA) | |
1365 | { | |
1366 | uint8_t *p; | |
1367 | p = p_end - 4; | |
630be16f | 1368 | #if 0 |
67b915a5 FB |
1369 | /* XXX: check why it occurs */ |
1370 | if (p == p_start) | |
1371 | error("empty code for %s", name); | |
630be16f | 1372 | #endif |
67b915a5 FB |
1373 | if (get32((uint32_t *)p) != 0x6bfa8001) |
1374 | error("ret expected at the end of %s", name); | |
3b46e624 | 1375 | copy_size = p - p_start; |
67b915a5 FB |
1376 | } |
1377 | #elif defined(HOST_IA64) | |
1378 | { | |
1379 | uint8_t *p; | |
1380 | p = (void *)(p_end - 4); | |
1381 | if (p == p_start) | |
1382 | error("empty code for %s", name); | |
1383 | /* br.ret.sptk.many b0;; */ | |
1384 | /* 08 00 84 00 */ | |
1385 | if (get32((uint32_t *)p) != 0x00840008) | |
1386 | error("br.ret.sptk.many b0;; expected at the end of %s", name); | |
b8076a74 | 1387 | copy_size = p_end - p_start; |
67b915a5 FB |
1388 | } |
1389 | #elif defined(HOST_SPARC) | |
1390 | { | |
fdbb4691 FB |
1391 | #define INSN_SAVE 0x9de3a000 |
1392 | #define INSN_RET 0x81c7e008 | |
74ccb34e | 1393 | #define INSN_RETL 0x81c3e008 |
fdbb4691 FB |
1394 | #define INSN_RESTORE 0x81e80000 |
1395 | #define INSN_RETURN 0x81cfe008 | |
1396 | #define INSN_NOP 0x01000000 | |
74ccb34e FB |
1397 | #define INSN_ADD_SP 0x9c03a000 // add %sp, nn, %sp |
1398 | #define INSN_SUB_SP 0x9c23a000 // sub %sp, nn, %sp | |
fdbb4691 | 1399 | |
67b915a5 FB |
1400 | uint32_t start_insn, end_insn1, end_insn2; |
1401 | uint8_t *p; | |
1402 | p = (void *)(p_end - 8); | |
1403 | if (p <= p_start) | |
1404 | error("empty code for %s", name); | |
1405 | start_insn = get32((uint32_t *)(p_start + 0x0)); | |
1406 | end_insn1 = get32((uint32_t *)(p + 0x0)); | |
1407 | end_insn2 = get32((uint32_t *)(p + 0x4)); | |
74ccb34e FB |
1408 | if (((start_insn & ~0x1fff) == INSN_SAVE) || |
1409 | (start_insn & ~0x1fff) == INSN_ADD_SP) { | |
67b915a5 FB |
1410 | p_start += 0x4; |
1411 | start_offset += 0x4; | |
fdbb4691 FB |
1412 | if (end_insn1 == INSN_RET && end_insn2 == INSN_RESTORE) |
1413 | /* SPARC v7: ret; restore; */ ; | |
1414 | else if (end_insn1 == INSN_RETURN && end_insn2 == INSN_NOP) | |
1415 | /* SPARC v9: return; nop; */ ; | |
74ccb34e FB |
1416 | else if (end_insn1 == INSN_RETL && (end_insn2 & ~0x1fff) == INSN_SUB_SP) |
1417 | /* SPARC v7: retl; sub %sp, nn, %sp; */ ; | |
fdbb4691 FB |
1418 | else |
1419 | ||
67b915a5 | 1420 | error("ret; restore; not found at end of %s", name); |
74ccb34e FB |
1421 | } else if (end_insn1 == INSN_RETL && end_insn2 == INSN_NOP) { |
1422 | ; | |
67b915a5 FB |
1423 | } else { |
1424 | error("No save at the beginning of %s", name); | |
1425 | } | |
ff1f20a3 | 1426 | #if 0 |
67b915a5 FB |
1427 | /* Skip a preceeding nop, if present. */ |
1428 | if (p > p_start) { | |
1429 | skip_insn = get32((uint32_t *)(p - 0x4)); | |
fdbb4691 | 1430 | if (skip_insn == INSN_NOP) |
67b915a5 FB |
1431 | p -= 4; |
1432 | } | |
ff1f20a3 | 1433 | #endif |
67b915a5 FB |
1434 | copy_size = p - p_start; |
1435 | } | |
1436 | #elif defined(HOST_SPARC64) | |
1437 | { | |
74ccb34e FB |
1438 | #define INSN_SAVE 0x9de3a000 |
1439 | #define INSN_RET 0x81c7e008 | |
1440 | #define INSN_RETL 0x81c3e008 | |
1441 | #define INSN_RESTORE 0x81e80000 | |
1442 | #define INSN_RETURN 0x81cfe008 | |
1443 | #define INSN_NOP 0x01000000 | |
1444 | #define INSN_ADD_SP 0x9c03a000 // add %sp, nn, %sp | |
1445 | #define INSN_SUB_SP 0x9c23a000 // sub %sp, nn, %sp | |
1446 | ||
67b915a5 FB |
1447 | uint32_t start_insn, end_insn1, end_insn2, skip_insn; |
1448 | uint8_t *p; | |
1449 | p = (void *)(p_end - 8); | |
74ccb34e FB |
1450 | #if 0 |
1451 | /* XXX: check why it occurs */ | |
67b915a5 FB |
1452 | if (p <= p_start) |
1453 | error("empty code for %s", name); | |
74ccb34e | 1454 | #endif |
67b915a5 FB |
1455 | start_insn = get32((uint32_t *)(p_start + 0x0)); |
1456 | end_insn1 = get32((uint32_t *)(p + 0x0)); | |
1457 | end_insn2 = get32((uint32_t *)(p + 0x4)); | |
74ccb34e FB |
1458 | if (((start_insn & ~0x1fff) == INSN_SAVE) || |
1459 | (start_insn & ~0x1fff) == INSN_ADD_SP) { | |
67b915a5 FB |
1460 | p_start += 0x4; |
1461 | start_offset += 0x4; | |
74ccb34e FB |
1462 | if (end_insn1 == INSN_RET && end_insn2 == INSN_RESTORE) |
1463 | /* SPARC v7: ret; restore; */ ; | |
1464 | else if (end_insn1 == INSN_RETURN && end_insn2 == INSN_NOP) | |
1465 | /* SPARC v9: return; nop; */ ; | |
1466 | else if (end_insn1 == INSN_RETL && (end_insn2 & ~0x1fff) == INSN_SUB_SP) | |
1467 | /* SPARC v7: retl; sub %sp, nn, %sp; */ ; | |
1468 | else | |
1469 | ||
67b915a5 | 1470 | error("ret; restore; not found at end of %s", name); |
74ccb34e FB |
1471 | } else if (end_insn1 == INSN_RETL && end_insn2 == INSN_NOP) { |
1472 | ; | |
67b915a5 FB |
1473 | } else { |
1474 | error("No save at the beginning of %s", name); | |
1475 | } | |
3b46e624 | 1476 | |
8289b279 | 1477 | #if 0 |
67b915a5 FB |
1478 | /* Skip a preceeding nop, if present. */ |
1479 | if (p > p_start) { | |
1480 | skip_insn = get32((uint32_t *)(p - 0x4)); | |
1481 | if (skip_insn == 0x01000000) | |
1482 | p -= 4; | |
1483 | } | |
8289b279 | 1484 | #endif |
3b46e624 | 1485 | |
67b915a5 FB |
1486 | copy_size = p - p_start; |
1487 | } | |
67b915a5 FB |
1488 | #elif defined(HOST_M68K) |
1489 | { | |
1490 | uint8_t *p; | |
1491 | p = (void *)(p_end - 2); | |
1492 | if (p == p_start) | |
1493 | error("empty code for %s", name); | |
1494 | // remove NOP's, probably added for alignment | |
1495 | while ((get16((uint16_t *)p) == 0x4e71) && | |
5fafdf24 | 1496 | (p>p_start)) |
67b915a5 FB |
1497 | p -= 2; |
1498 | if (get16((uint16_t *)p) != 0x4e75) | |
1499 | error("rts expected at the end of %s", name); | |
1500 | copy_size = p - p_start; | |
1501 | } | |
f54b3f92 AJ |
1502 | #elif defined(HOST_HPPA) |
1503 | { | |
1504 | uint8_t *p; | |
1505 | p = p_start; | |
1506 | while (p < p_end) { | |
1507 | uint32_t insn = get32((uint32_t *)p); | |
1508 | if (insn == 0x6bc23fd9 || /* stw rp,-14(sp) */ | |
1509 | insn == 0x08030241 || /* copy r3,r1 */ | |
1510 | insn == 0x081e0243 || /* copy sp,r3 */ | |
1511 | (insn & 0xffffc000) == 0x37de0000 || /* ldo x(sp),sp */ | |
1512 | (insn & 0xffffc000) == 0x6fc10000) /* stwm r1,x(sp) */ | |
1513 | p += 4; | |
1514 | else | |
1515 | break; | |
1516 | } | |
1517 | start_offset += p - p_start; | |
1518 | p_start = p; | |
1519 | p = p_end - 4; | |
1520 | ||
1521 | while (p > p_start) { | |
1522 | uint32_t insn = get32((uint32_t *)p); | |
1523 | if ((insn & 0xffffc000) == 0x347e0000 || /* ldo x(r3),sp */ | |
1524 | (insn & 0xffe0c000) == 0x4fc00000 || /* ldwm x(sp),rx */ | |
1525 | (insn & 0xffffc000) == 0x37de0000 || /* ldo x(sp),sp */ | |
1526 | insn == 0x48623fd9 || /* ldw -14(r3),rp */ | |
1527 | insn == 0xe840c000 || /* bv r0(rp) */ | |
1528 | insn == 0xe840c002) /* bv,n r0(rp) */ | |
1529 | p -= 4; | |
1530 | else | |
1531 | break; | |
1532 | } | |
1533 | p += 4; | |
1534 | if (p <= p_start) | |
1535 | error("empty code for %s", name); | |
1536 | ||
1537 | copy_size = p - p_start; | |
1538 | } | |
9617efe8 | 1539 | #elif defined(HOST_MIPS) || defined(HOST_MIPS64) |
c4b89d18 TS |
1540 | { |
1541 | #define INSN_RETURN 0x03e00008 | |
1542 | #define INSN_NOP 0x00000000 | |
1543 | ||
1544 | uint8_t *p = p_end; | |
1545 | ||
1546 | if (p < (p_start + 0x8)) { | |
1547 | error("empty code for %s", name); | |
1548 | } else { | |
1549 | uint32_t end_insn1, end_insn2; | |
1550 | ||
1551 | p -= 0x8; | |
1552 | end_insn1 = get32((uint32_t *)(p + 0x0)); | |
1553 | end_insn2 = get32((uint32_t *)(p + 0x4)); | |
1554 | if (end_insn1 != INSN_RETURN && end_insn2 != INSN_NOP) | |
1555 | error("jr ra not found at end of %s", name); | |
1556 | } | |
1557 | copy_size = p - p_start; | |
1558 | } | |
a2a64a1f AZ |
1559 | #elif defined(HOST_ARM) |
1560 | error("dyngen targets not supported on ARM"); | |
810260a8 | 1561 | #elif defined(HOST_PPC64) |
1562 | error("dyngen targets not supported on PPC64"); | |
67b915a5 FB |
1563 | #else |
1564 | #error unsupported CPU | |
1565 | #endif | |
367e86e8 FB |
1566 | |
1567 | /* compute the number of arguments by looking at the relocations */ | |
1568 | for(i = 0;i < MAX_ARGS; i++) | |
1569 | args_present[i] = 0; | |
1570 | ||
ce11fedc | 1571 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
82eec0a1 FB |
1572 | host_ulong offset = get_rel_offset(rel); |
1573 | if (offset >= start_offset && | |
1574 | offset < start_offset + (p_end - p_start)) { | |
67b915a5 | 1575 | sym_name = get_rel_sym_name(rel); |
82eec0a1 FB |
1576 | if(!sym_name) |
1577 | continue; | |
c4687878 FB |
1578 | if (strstart(sym_name, "__op_param", &p) || |
1579 | strstart(sym_name, "__op_gen_label", &p)) { | |
ce11fedc | 1580 | n = strtoul(p, NULL, 10); |
d4e8164f | 1581 | if (n > MAX_ARGS) |
ce11fedc FB |
1582 | error("too many arguments in %s", name); |
1583 | args_present[n - 1] = 1; | |
367e86e8 FB |
1584 | } |
1585 | } | |
1586 | } | |
3b46e624 | 1587 | |
367e86e8 FB |
1588 | nb_args = 0; |
1589 | while (nb_args < MAX_ARGS && args_present[nb_args]) | |
1590 | nb_args++; | |
1591 | for(i = nb_args; i < MAX_ARGS; i++) { | |
1592 | if (args_present[i]) | |
1593 | error("inconsistent argument numbering in %s", name); | |
1594 | } | |
1595 | ||
0ea00c9a | 1596 | if (gen_switch == 2) { |
b29d8ae4 AJ |
1597 | |
1598 | #if defined(HOST_HPPA) | |
1599 | int op_size = copy_size; | |
1600 | int has_stubs = 0; | |
1601 | char relname[256]; | |
1602 | int type, is_label; | |
1603 | ||
1604 | for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) { | |
1605 | if (rel->r_offset >= start_offset && | |
1606 | rel->r_offset < start_offset + copy_size) { | |
1607 | sym_name = get_rel_sym_name(rel); | |
1608 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; | |
1609 | is_label = get_reloc_expr(relname, sizeof(relname), sym_name); | |
1610 | type = ELF32_R_TYPE(rel->r_info); | |
1611 | ||
1612 | if (!is_label && type == R_PARISC_PCREL17F) { | |
1613 | has_stubs = 1; | |
1614 | op_size += 8; /* ldil and be,n instructions */ | |
1615 | } | |
1616 | } | |
1617 | } | |
1618 | ||
1619 | if (has_stubs) | |
1620 | op_size += 4; /* b,l,n instruction, to skip past the stubs */ | |
1621 | ||
1622 | fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, op_size); | |
1623 | #else | |
1624 | fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size); | |
1625 | #endif | |
1626 | ||
0ea00c9a | 1627 | } else if (gen_switch == 1) { |
dc99065b FB |
1628 | |
1629 | /* output C code */ | |
1630 | fprintf(outfile, "case INDEX_%s: {\n", name); | |
1631 | if (nb_args > 0) { | |
1632 | fprintf(outfile, " long "); | |
1633 | for(i = 0; i < nb_args; i++) { | |
1634 | if (i != 0) | |
1635 | fprintf(outfile, ", "); | |
1636 | fprintf(outfile, "param%d", i + 1); | |
1637 | } | |
1638 | fprintf(outfile, ";\n"); | |
367e86e8 | 1639 | } |
b8076a74 FB |
1640 | #if defined(HOST_IA64) |
1641 | fprintf(outfile, " extern char %s;\n", name); | |
1642 | #else | |
dc99065b | 1643 | fprintf(outfile, " extern void %s();\n", name); |
b8076a74 | 1644 | #endif |
dc99065b | 1645 | |
ce11fedc | 1646 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
82eec0a1 FB |
1647 | host_ulong offset = get_rel_offset(rel); |
1648 | if (offset >= start_offset && | |
1649 | offset < start_offset + (p_end - p_start)) { | |
67b915a5 | 1650 | sym_name = get_rel_sym_name(rel); |
82eec0a1 FB |
1651 | if(!sym_name) |
1652 | continue; | |
5fafdf24 | 1653 | if (*sym_name && |
d4e8164f | 1654 | !strstart(sym_name, "__op_param", NULL) && |
c4687878 FB |
1655 | !strstart(sym_name, "__op_jmp", NULL) && |
1656 | !strstart(sym_name, "__op_gen_label", NULL)) { | |
f54b3f92 | 1657 | #if defined(HOST_SPARC) || defined(HOST_HPPA) |
d014c98c FB |
1658 | if (sym_name[0] == '.') { |
1659 | fprintf(outfile, | |
1660 | "extern char __dot_%s __asm__(\"%s\");\n", | |
1661 | sym_name+1, sym_name); | |
1662 | continue; | |
1663 | } | |
1664 | #endif | |
b8076a74 | 1665 | #if defined(__APPLE__) |
85028e4d TS |
1666 | /* Set __attribute((unused)) on darwin because we |
1667 | want to avoid warning when we don't use the symbol. */ | |
1668 | fprintf(outfile, " extern char %s __attribute__((unused));\n", sym_name); | |
b8076a74 FB |
1669 | #elif defined(HOST_IA64) |
1670 | if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B) | |
1671 | /* | |
1672 | * PCREL21 br.call targets generally | |
1673 | * are out of range and need to go | |
1674 | * through an "import stub". | |
1675 | */ | |
1676 | fprintf(outfile, " extern char %s;\n", | |
1677 | sym_name); | |
82eec0a1 | 1678 | #else |
ce11fedc | 1679 | fprintf(outfile, "extern char %s;\n", sym_name); |
82eec0a1 | 1680 | #endif |
dc99065b FB |
1681 | } |
1682 | } | |
1683 | } | |
1684 | ||
f54b3f92 AJ |
1685 | #ifdef __hppa__ |
1686 | fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)__canonicalize_funcptr_for_compare(%s)+%d), %d);\n", | |
1687 | name, (int)(start_offset - offset), copy_size); | |
1688 | #else | |
82eec0a1 FB |
1689 | fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", |
1690 | name, (int)(start_offset - offset), copy_size); | |
f54b3f92 | 1691 | #endif |
d4e8164f FB |
1692 | |
1693 | /* emit code offset information */ | |
1694 | { | |
67b915a5 | 1695 | EXE_SYM *sym; |
d4e8164f | 1696 | const char *sym_name, *p; |
7a2d6d96 | 1697 | host_ulong val; |
d4e8164f FB |
1698 | int n; |
1699 | ||
1700 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { | |
67b915a5 | 1701 | sym_name = get_sym_name(sym); |
d4e8164f | 1702 | if (strstart(sym_name, "__op_label", &p)) { |
c1e42a13 | 1703 | uint8_t *ptr; |
fe319756 | 1704 | unsigned long offset; |
3b46e624 | 1705 | |
d4e8164f FB |
1706 | /* test if the variable refers to a label inside |
1707 | the code we are generating */ | |
67b915a5 FB |
1708 | #ifdef CONFIG_FORMAT_COFF |
1709 | if (sym->st_shndx == text_shndx) { | |
1710 | ptr = sdata[coff_text_shndx]; | |
1711 | } else if (sym->st_shndx == data_shndx) { | |
1712 | ptr = sdata[coff_data_shndx]; | |
1713 | } else { | |
1714 | ptr = NULL; | |
1715 | } | |
82eec0a1 FB |
1716 | #elif defined(CONFIG_FORMAT_MACH) |
1717 | if(!sym->n_sect) | |
1718 | continue; | |
1719 | ptr = sdata[sym->n_sect-1]; | |
67b915a5 | 1720 | #else |
fe319756 | 1721 | ptr = sdata[sym->st_shndx]; |
67b915a5 | 1722 | #endif |
fe319756 FB |
1723 | if (!ptr) |
1724 | error("__op_labelN in invalid section"); | |
1725 | offset = sym->st_value; | |
82eec0a1 FB |
1726 | #ifdef CONFIG_FORMAT_MACH |
1727 | offset -= section_hdr[sym->n_sect-1].addr; | |
1728 | #endif | |
7a2d6d96 | 1729 | val = *(host_ulong *)(ptr + offset); |
fe319756 FB |
1730 | #ifdef ELF_USES_RELOCA |
1731 | { | |
1732 | int reloc_shndx, nb_relocs1, j; | |
1733 | ||
1734 | /* try to find a matching relocation */ | |
1735 | reloc_shndx = find_reloc(sym->st_shndx); | |
1736 | if (reloc_shndx) { | |
5fafdf24 | 1737 | nb_relocs1 = shdr[reloc_shndx].sh_size / |
fe319756 FB |
1738 | shdr[reloc_shndx].sh_entsize; |
1739 | rel = (ELF_RELOC *)sdata[reloc_shndx]; | |
1740 | for(j = 0; j < nb_relocs1; j++) { | |
1741 | if (rel->r_offset == offset) { | |
039de852 | 1742 | val = rel->r_addend; |
fe319756 FB |
1743 | break; |
1744 | } | |
1745 | rel++; | |
1746 | } | |
1747 | } | |
1748 | } | |
3b46e624 | 1749 | #endif |
c4687878 | 1750 | if (val >= start_offset && val <= start_offset + copy_size) { |
d4e8164f | 1751 | n = strtol(p, NULL, 10); |
3442e896 | 1752 | fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, (long)(val - start_offset)); |
d4e8164f FB |
1753 | } |
1754 | } | |
1755 | } | |
1756 | } | |
1757 | ||
85028e4d | 1758 | /* load parameters in variables */ |
dc99065b FB |
1759 | for(i = 0; i < nb_args; i++) { |
1760 | fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1); | |
1761 | } | |
1762 | ||
1763 | /* patch relocations */ | |
ce11fedc | 1764 | #if defined(HOST_I386) |
dc99065b | 1765 | { |
4a1a1707 | 1766 | char relname[256]; |
57fec1fe | 1767 | int type, is_label; |
ce11fedc | 1768 | int addend; |
3442e896 | 1769 | int reloc_offset; |
dc99065b | 1770 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
ae228531 FB |
1771 | if (rel->r_offset >= start_offset && |
1772 | rel->r_offset < start_offset + copy_size) { | |
67b915a5 | 1773 | sym_name = get_rel_sym_name(rel); |
b48a8bb6 FB |
1774 | if (!sym_name) |
1775 | continue; | |
3442e896 | 1776 | reloc_offset = rel->r_offset - start_offset; |
ecd854fd FB |
1777 | if (strstart(sym_name, "__op_jmp", &p)) { |
1778 | int n; | |
1779 | n = strtol(p, NULL, 10); | |
1780 | /* __op_jmp relocations are done at | |
1781 | runtime to do translated block | |
1782 | chaining: the offset of the instruction | |
1783 | needs to be stored */ | |
1784 | fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", | |
3442e896 | 1785 | n, reloc_offset); |
ecd854fd FB |
1786 | continue; |
1787 | } | |
3442e896 | 1788 | |
57fec1fe | 1789 | is_label = get_reloc_expr(relname, sizeof(relname), sym_name); |
367e86e8 | 1790 | addend = get32((uint32_t *)(text + rel->r_offset)); |
67b915a5 FB |
1791 | #ifdef CONFIG_FORMAT_ELF |
1792 | type = ELF32_R_TYPE(rel->r_info); | |
57fec1fe FB |
1793 | if (is_label) { |
1794 | switch(type) { | |
1795 | case R_386_32: | |
1796 | case R_386_PC32: | |
1797 | fprintf(outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n", | |
1798 | reloc_offset, type, relname, addend); | |
1799 | break; | |
1800 | default: | |
1801 | error("unsupported i386 relocation (%d)", type); | |
1802 | } | |
1803 | } else { | |
1804 | switch(type) { | |
1805 | case R_386_32: | |
1806 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", | |
1807 | reloc_offset, relname, addend); | |
1808 | break; | |
1809 | case R_386_PC32: | |
1810 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n", | |
1811 | reloc_offset, relname, reloc_offset, addend); | |
1812 | break; | |
1813 | default: | |
1814 | error("unsupported i386 relocation (%d)", type); | |
1815 | } | |
367e86e8 | 1816 | } |
67b915a5 | 1817 | #elif defined(CONFIG_FORMAT_COFF) |
40c3bac3 FB |
1818 | { |
1819 | char *temp_name; | |
1820 | int j; | |
1821 | EXE_SYM *sym; | |
1822 | temp_name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx)); | |
1823 | if (!strcmp(temp_name, ".data")) { | |
1824 | for (j = 0, sym = symtab; j < nb_syms; j++, sym++) { | |
1825 | if (strstart(sym->st_name, sym_name, NULL)) { | |
1826 | addend -= sym->st_value; | |
1827 | } | |
1828 | } | |
1829 | } | |
1830 | } | |
67b915a5 | 1831 | type = rel->r_type; |
a4d8670f FB |
1832 | if (is_label) { |
1833 | /* TCG uses elf relocation constants */ | |
1834 | #define R_386_32 1 | |
1835 | #define R_386_PC32 2 | |
1836 | switch(type) { | |
1837 | case DIR32: | |
1838 | type = R_386_32; | |
1839 | goto do_reloc; | |
1840 | case DISP32: | |
1841 | type = R_386_PC32; | |
1842 | addend -= 4; | |
1843 | do_reloc: | |
1844 | fprintf(outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n", | |
1845 | reloc_offset, type, relname, addend); | |
1846 | break; | |
1847 | default: | |
1848 | error("unsupported i386 relocation (%d)", type); | |
1849 | } | |
1850 | } else { | |
1851 | switch(type) { | |
1852 | case DIR32: | |
1853 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", | |
1854 | reloc_offset, relname, addend); | |
1855 | break; | |
1856 | case DISP32: | |
1857 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n", | |
1858 | reloc_offset, relname, reloc_offset, addend); | |
1859 | break; | |
1860 | default: | |
1861 | error("unsupported i386 relocation (%d)", type); | |
1862 | } | |
67b915a5 FB |
1863 | } |
1864 | #else | |
1865 | #error unsupport object format | |
1866 | #endif | |
367e86e8 | 1867 | } |
dc99065b FB |
1868 | } |
1869 | } | |
c4687878 | 1870 | #elif defined(HOST_X86_64) |
bc51c5c9 | 1871 | { |
4a1a1707 | 1872 | char relname[256]; |
57fec1fe | 1873 | int type, is_label; |
bc51c5c9 | 1874 | int addend; |
3442e896 | 1875 | int reloc_offset; |
bc51c5c9 FB |
1876 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1877 | if (rel->r_offset >= start_offset && | |
1878 | rel->r_offset < start_offset + copy_size) { | |
1879 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; | |
57fec1fe | 1880 | is_label = get_reloc_expr(relname, sizeof(relname), sym_name); |
bc51c5c9 FB |
1881 | type = ELF32_R_TYPE(rel->r_info); |
1882 | addend = rel->r_addend; | |
3442e896 | 1883 | reloc_offset = rel->r_offset - start_offset; |
57fec1fe FB |
1884 | if (is_label) { |
1885 | switch(type) { | |
1886 | case R_X86_64_32: | |
1887 | case R_X86_64_32S: | |
1888 | case R_X86_64_PC32: | |
1889 | fprintf(outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n", | |
1890 | reloc_offset, type, relname, addend); | |
1891 | break; | |
1892 | default: | |
1893 | error("unsupported X86_64 relocation (%d)", type); | |
1894 | } | |
1895 | } else { | |
1896 | switch(type) { | |
1897 | case R_X86_64_32: | |
1898 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n", | |
1899 | reloc_offset, relname, addend); | |
1900 | break; | |
1901 | case R_X86_64_32S: | |
1902 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n", | |
1903 | reloc_offset, relname, addend); | |
1904 | break; | |
1905 | case R_X86_64_PC32: | |
1906 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n", | |
1907 | reloc_offset, relname, reloc_offset, addend); | |
1908 | break; | |
1909 | default: | |
1910 | error("unsupported X86_64 relocation (%d)", type); | |
1911 | } | |
bc51c5c9 FB |
1912 | } |
1913 | } | |
1914 | } | |
1915 | } | |
ce11fedc | 1916 | #elif defined(HOST_PPC) |
04369ff2 | 1917 | { |
82eec0a1 | 1918 | #ifdef CONFIG_FORMAT_ELF |
4a1a1707 | 1919 | char relname[256]; |
04369ff2 | 1920 | int type; |
ce11fedc | 1921 | int addend; |
90426a4e | 1922 | int is_label; |
3442e896 | 1923 | int reloc_offset; |
04369ff2 | 1924 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
ae228531 | 1925 | if (rel->r_offset >= start_offset && |
90426a4e | 1926 | rel->r_offset < start_offset + copy_size) { |
efdea7bf | 1927 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
3442e896 | 1928 | reloc_offset = rel->r_offset - start_offset; |
d4e8164f FB |
1929 | if (strstart(sym_name, "__op_jmp", &p)) { |
1930 | int n; | |
1931 | n = strtol(p, NULL, 10); | |
1932 | /* __op_jmp relocations are done at | |
1933 | runtime to do translated block | |
1934 | chaining: the offset of the instruction | |
1935 | needs to be stored */ | |
1936 | fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", | |
3442e896 | 1937 | n, reloc_offset); |
d4e8164f FB |
1938 | continue; |
1939 | } | |
3b46e624 | 1940 | |
4a1a1707 | 1941 | get_reloc_expr(relname, sizeof(relname), sym_name); |
04369ff2 | 1942 | type = ELF32_R_TYPE(rel->r_info); |
90426a4e | 1943 | is_label = get_reloc_expr(relname, sizeof(relname), sym_name); |
04369ff2 | 1944 | addend = rel->r_addend; |
90426a4e FB |
1945 | if (is_label) { |
1946 | switch (type) { | |
1947 | case R_PPC_REL24: | |
1948 | fprintf (outfile, " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n", | |
1949 | reloc_offset, type, relname, addend); | |
1950 | break; | |
1951 | default: | |
1952 | error ("unsupported ppc relocation (%d)", type); | |
1953 | } | |
1954 | } | |
1955 | else { | |
1956 | switch(type) { | |
1957 | case R_PPC_ADDR32: | |
1958 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", | |
1959 | reloc_offset, relname, addend); | |
1960 | break; | |
1961 | case R_PPC_ADDR16_LO: | |
1962 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n", | |
1963 | reloc_offset, relname, addend); | |
1964 | break; | |
1965 | case R_PPC_ADDR16_HI: | |
1966 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n", | |
1967 | reloc_offset, relname, addend); | |
1968 | break; | |
1969 | case R_PPC_ADDR16_HA: | |
1970 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n", | |
1971 | reloc_offset, relname, addend); | |
1972 | break; | |
1973 | case R_PPC_REL24: | |
1974 | /* warning: must be at 32 MB distancy */ | |
5be16762 | 1975 | fprintf(outfile, "{\n" |
1976 | " long disp = (%s - (long)(gen_code_ptr + %d) + %d);\n" | |
1977 | " if ((disp << 6) >> 6 != disp) {;\n" | |
1978 | " fprintf(stderr, \"Branch target is too far away\\n\");" | |
1979 | " abort();\n" | |
1980 | " }\n" | |
1981 | "}\n", | |
1982 | relname, reloc_offset, addend); | |
90426a4e FB |
1983 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((%s - (long)(gen_code_ptr + %d) + %d) & 0x03fffffc);\n", |
1984 | reloc_offset, reloc_offset, relname, reloc_offset, addend); | |
1985 | break; | |
1986 | default: | |
1987 | error("unsupported powerpc relocation (%d)", type); | |
1988 | } | |
04369ff2 FB |
1989 | } |
1990 | } | |
1991 | } | |
82eec0a1 | 1992 | #elif defined(CONFIG_FORMAT_MACH) |
a029baa4 TS |
1993 | struct scattered_relocation_info *scarel; |
1994 | struct relocation_info * rel; | |
1995 | char final_sym_name[256]; | |
1996 | const char *sym_name; | |
1997 | const char *p; | |
1998 | int slide, sslide; | |
1999 | int i; | |
2000 | ||
2001 | for(i = 0, rel = relocs; i < nb_relocs; i++, rel++) { | |
2002 | unsigned int offset, length, value = 0; | |
2003 | unsigned int type, pcrel, isym = 0; | |
2004 | unsigned int usesym = 0; | |
2005 | ||
2006 | if(R_SCATTERED & rel->r_address) { | |
2007 | scarel = (struct scattered_relocation_info*)rel; | |
2008 | offset = (unsigned int)scarel->r_address; | |
2009 | length = scarel->r_length; | |
2010 | pcrel = scarel->r_pcrel; | |
2011 | type = scarel->r_type; | |
2012 | value = scarel->r_value; | |
2013 | } else { | |
2014 | value = isym = rel->r_symbolnum; | |
2015 | usesym = (rel->r_extern); | |
2016 | offset = rel->r_address; | |
2017 | length = rel->r_length; | |
2018 | pcrel = rel->r_pcrel; | |
2019 | type = rel->r_type; | |
2020 | } | |
2021 | ||
2022 | slide = offset - start_offset; | |
2023 | ||
5fafdf24 | 2024 | if (!(offset >= start_offset && offset < start_offset + size)) |
a029baa4 TS |
2025 | continue; /* not in our range */ |
2026 | ||
2027 | sym_name = get_reloc_name(rel, &sslide); | |
2028 | ||
2029 | if(usesym && symtab[isym].n_type & N_STAB) | |
2030 | continue; /* don't handle STAB (debug sym) */ | |
2031 | ||
2032 | if (sym_name && strstart(sym_name, "__op_jmp", &p)) { | |
2033 | int n; | |
2034 | n = strtol(p, NULL, 10); | |
2035 | fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n", | |
2036 | n, slide); | |
2037 | continue; /* Nothing more to do */ | |
2038 | } | |
2039 | ||
2040 | if(!sym_name) { | |
2041 | fprintf(outfile, "/* #warning relocation not handled in %s (value 0x%x, %s, offset 0x%x, length 0x%x, %s, type 0x%x) */\n", | |
2042 | name, value, usesym ? "use sym" : "don't use sym", offset, length, pcrel ? "pcrel":"", type); | |
2043 | continue; /* dunno how to handle without final_sym_name */ | |
2044 | } | |
2045 | ||
5fafdf24 | 2046 | get_reloc_expr(final_sym_name, sizeof(final_sym_name), |
a029baa4 TS |
2047 | sym_name); |
2048 | switch(type) { | |
2049 | case PPC_RELOC_BR24: | |
2050 | if (!strstart(sym_name,"__op_gen_label",&p)) { | |
2051 | fprintf(outfile, "{\n"); | |
2052 | fprintf(outfile, " uint32_t imm = *(uint32_t *)(gen_code_ptr + %d) & 0x3fffffc;\n", slide); | |
2053 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | ((imm + ((long)%s - (long)gen_code_ptr) + %d) & 0x03fffffc);\n", | |
2054 | slide, slide, name, sslide); | |
2055 | fprintf(outfile, "}\n"); | |
2056 | } else { | |
2057 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (*(uint32_t *)(gen_code_ptr + %d) & ~0x03fffffc) | (((long)%s - (long)gen_code_ptr - %d) & 0x03fffffc);\n", | |
2058 | slide, slide, final_sym_name, slide); | |
2059 | } | |
82eec0a1 | 2060 | break; |
a029baa4 | 2061 | case PPC_RELOC_HI16: |
5fafdf24 | 2062 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d) >> 16;\n", |
a029baa4 TS |
2063 | slide, final_sym_name, sslide); |
2064 | break; | |
2065 | case PPC_RELOC_LO16: | |
5fafdf24 | 2066 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d);\n", |
a029baa4 TS |
2067 | slide, final_sym_name, sslide); |
2068 | break; | |
2069 | case PPC_RELOC_HA16: | |
5fafdf24 | 2070 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d + 0x8000) >> 16;\n", |
a029baa4 TS |
2071 | slide, final_sym_name, sslide); |
2072 | break; | |
2073 | default: | |
2074 | error("unsupported powerpc relocation (%d)", type); | |
2075 | } | |
2076 | } | |
82eec0a1 FB |
2077 | #else |
2078 | #error unsupport object format | |
2079 | #endif | |
04369ff2 | 2080 | } |
ce11fedc | 2081 | #elif defined(HOST_S390) |
fb3e5849 | 2082 | { |
4a1a1707 | 2083 | char relname[256]; |
fb3e5849 | 2084 | int type; |
ce11fedc | 2085 | int addend; |
3442e896 | 2086 | int reloc_offset; |
fb3e5849 | 2087 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
ae228531 FB |
2088 | if (rel->r_offset >= start_offset && |
2089 | rel->r_offset < start_offset + copy_size) { | |
efdea7bf | 2090 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
4a1a1707 | 2091 | get_reloc_expr(relname, sizeof(relname), sym_name); |
fb3e5849 FB |
2092 | type = ELF32_R_TYPE(rel->r_info); |
2093 | addend = rel->r_addend; | |
3442e896 | 2094 | reloc_offset = rel->r_offset - start_offset; |
fb3e5849 FB |
2095 | switch(type) { |
2096 | case R_390_32: | |
5fafdf24 | 2097 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", |
4a1a1707 | 2098 | reloc_offset, relname, addend); |
fb3e5849 FB |
2099 | break; |
2100 | case R_390_16: | |
5fafdf24 | 2101 | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n", |
4a1a1707 | 2102 | reloc_offset, relname, addend); |
fb3e5849 FB |
2103 | break; |
2104 | case R_390_8: | |
5fafdf24 | 2105 | fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n", |
4a1a1707 | 2106 | reloc_offset, relname, addend); |
fb3e5849 | 2107 | break; |
76d83bde TS |
2108 | case R_390_PC32DBL: |
2109 | if (ELF32_ST_TYPE(symtab[ELFW(R_SYM)(rel->r_info)].st_info) == STT_SECTION) { | |
2110 | fprintf(outfile, | |
2111 | " *(uint32_t *)(gen_code_ptr + %d) += " | |
2112 | "((long)&%s - (long)gen_code_ptr) >> 1;\n", | |
2113 | reloc_offset, name); | |
2114 | } | |
2115 | else | |
2116 | fprintf(outfile, | |
2117 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2118 | "(%s + %d - ((uint32_t)gen_code_ptr + %d)) >> 1;\n", | |
2119 | reloc_offset, relname, addend, reloc_offset); | |
2120 | break; | |
fb3e5849 FB |
2121 | default: |
2122 | error("unsupported s390 relocation (%d)", type); | |
2123 | } | |
2124 | } | |
2125 | } | |
2126 | } | |
efdea7bf FB |
2127 | #elif defined(HOST_ALPHA) |
2128 | { | |
2129 | for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) { | |
ae228531 | 2130 | if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) { |
efdea7bf | 2131 | int type; |
3442e896 | 2132 | long reloc_offset; |
74c95119 | 2133 | |
efdea7bf | 2134 | type = ELF64_R_TYPE(rel->r_info); |
74c95119 | 2135 | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
3442e896 | 2136 | reloc_offset = rel->r_offset - start_offset; |
efdea7bf FB |
2137 | switch (type) { |
2138 | case R_ALPHA_GPDISP: | |
74c95119 FB |
2139 | /* The gp is just 32 bit, and never changes, so it's easiest to emit it |
2140 | as an immediate instead of constructing it from the pv or ra. */ | |
2141 | fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n", | |
3442e896 | 2142 | reloc_offset); |
74c95119 | 2143 | fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n", |
3442e896 | 2144 | reloc_offset + (int)rel->r_addend); |
efdea7bf FB |
2145 | break; |
2146 | case R_ALPHA_LITUSE: | |
2147 | /* jsr to literal hint. Could be used to optimize to bsr. Ignore for | |
2148 | now, since some called functions (libc) need pv to be set up. */ | |
2149 | break; | |
2150 | case R_ALPHA_HINT: | |
2151 | /* Branch target prediction hint. Ignore for now. Should be already | |
2152 | correct for in-function jumps. */ | |
2153 | break; | |
2154 | case R_ALPHA_LITERAL: | |
74c95119 FB |
2155 | /* Load a literal from the GOT relative to the gp. Since there's only a |
2156 | single gp, nothing is to be done. */ | |
2157 | break; | |
2158 | case R_ALPHA_GPRELHIGH: | |
2159 | /* Handle fake relocations against __op_param symbol. Need to emit the | |
2160 | high part of the immediate value instead. Other symbols need no | |
2161 | special treatment. */ | |
2162 | if (strstart(sym_name, "__op_param", &p)) | |
2163 | fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n", | |
3442e896 | 2164 | reloc_offset, p); |
74c95119 FB |
2165 | break; |
2166 | case R_ALPHA_GPRELLOW: | |
2167 | if (strstart(sym_name, "__op_param", &p)) | |
2168 | fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n", | |
3442e896 | 2169 | reloc_offset, p); |
74c95119 FB |
2170 | break; |
2171 | case R_ALPHA_BRSGP: | |
2172 | /* PC-relative jump. Tweak offset to skip the two instructions that try to | |
2173 | set up the gp from the pv. */ | |
2f87c607 | 2174 | fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n", |
3442e896 | 2175 | reloc_offset, sym_name, reloc_offset); |
efdea7bf FB |
2176 | break; |
2177 | default: | |
2178 | error("unsupported Alpha relocation (%d)", type); | |
2179 | } | |
2180 | } | |
2181 | } | |
2182 | } | |
2183 | #elif defined(HOST_IA64) | |
2184 | { | |
b8076a74 FB |
2185 | unsigned long sym_idx; |
2186 | long code_offset; | |
4a1a1707 | 2187 | char relname[256]; |
efdea7bf | 2188 | int type; |
b8076a74 FB |
2189 | long addend; |
2190 | ||
efdea7bf | 2191 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
b8076a74 FB |
2192 | sym_idx = ELF64_R_SYM(rel->r_info); |
2193 | if (rel->r_offset < start_offset | |
2194 | || rel->r_offset >= start_offset + copy_size) | |
2195 | continue; | |
2196 | sym_name = (strtab + symtab[sym_idx].st_name); | |
3442e896 | 2197 | code_offset = rel->r_offset - start_offset; |
b8076a74 FB |
2198 | if (strstart(sym_name, "__op_jmp", &p)) { |
2199 | int n; | |
2200 | n = strtol(p, NULL, 10); | |
2201 | /* __op_jmp relocations are done at | |
2202 | runtime to do translated block | |
2203 | chaining: the offset of the instruction | |
2204 | needs to be stored */ | |
2205 | fprintf(outfile, " jmp_offsets[%d] =" | |
2206 | "%ld + (gen_code_ptr - gen_code_buf);\n", | |
3442e896 | 2207 | n, code_offset); |
b8076a74 FB |
2208 | continue; |
2209 | } | |
4a1a1707 | 2210 | get_reloc_expr(relname, sizeof(relname), sym_name); |
b8076a74 FB |
2211 | type = ELF64_R_TYPE(rel->r_info); |
2212 | addend = rel->r_addend; | |
b8076a74 FB |
2213 | switch(type) { |
2214 | case R_IA64_IMM64: | |
2215 | fprintf(outfile, | |
2216 | " ia64_imm64(gen_code_ptr + %ld, " | |
2217 | "%s + %ld);\n", | |
4a1a1707 | 2218 | code_offset, relname, addend); |
b8076a74 FB |
2219 | break; |
2220 | case R_IA64_LTOFF22X: | |
2221 | case R_IA64_LTOFF22: | |
2222 | fprintf(outfile, " IA64_LTOFF(gen_code_ptr + %ld," | |
2223 | " %s + %ld, %d);\n", | |
4a1a1707 | 2224 | code_offset, relname, addend, |
b8076a74 FB |
2225 | (type == R_IA64_LTOFF22X)); |
2226 | break; | |
2227 | case R_IA64_LDXMOV: | |
2228 | fprintf(outfile, | |
2229 | " ia64_ldxmov(gen_code_ptr + %ld," | |
4a1a1707 | 2230 | " %s + %ld);\n", code_offset, relname, addend); |
b8076a74 FB |
2231 | break; |
2232 | ||
2233 | case R_IA64_PCREL21B: | |
2234 | if (strstart(sym_name, "__op_gen_label", NULL)) { | |
2235 | fprintf(outfile, | |
2236 | " ia64_imm21b(gen_code_ptr + %ld," | |
2237 | " (long) (%s + %ld -\n\t\t" | |
2238 | "((long) gen_code_ptr + %ld)) >> 4);\n", | |
4a1a1707 | 2239 | code_offset, relname, addend, |
b8076a74 FB |
2240 | code_offset & ~0xfUL); |
2241 | } else { | |
2242 | fprintf(outfile, | |
2243 | " IA64_PLT(gen_code_ptr + %ld, " | |
2244 | "%d);\t/* %s + %ld */\n", | |
2245 | code_offset, | |
2246 | get_plt_index(sym_name, addend), | |
2247 | sym_name, addend); | |
2248 | } | |
2249 | break; | |
2250 | default: | |
2251 | error("unsupported ia64 relocation (0x%x)", | |
2252 | type); | |
2253 | } | |
efdea7bf | 2254 | } |
b8076a74 FB |
2255 | fprintf(outfile, " ia64_nop_b(gen_code_ptr + %d);\n", |
2256 | copy_size - 16 + 2); | |
efdea7bf | 2257 | } |
d014c98c FB |
2258 | #elif defined(HOST_SPARC) |
2259 | { | |
4a1a1707 | 2260 | char relname[256]; |
d014c98c FB |
2261 | int type; |
2262 | int addend; | |
3442e896 | 2263 | int reloc_offset; |
d014c98c | 2264 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
ae228531 FB |
2265 | if (rel->r_offset >= start_offset && |
2266 | rel->r_offset < start_offset + copy_size) { | |
d014c98c | 2267 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; |
4a1a1707 | 2268 | get_reloc_expr(relname, sizeof(relname), sym_name); |
d014c98c FB |
2269 | type = ELF32_R_TYPE(rel->r_info); |
2270 | addend = rel->r_addend; | |
3442e896 | 2271 | reloc_offset = rel->r_offset - start_offset; |
d014c98c FB |
2272 | switch(type) { |
2273 | case R_SPARC_32: | |
5fafdf24 | 2274 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", |
4a1a1707 | 2275 | reloc_offset, relname, addend); |
d014c98c FB |
2276 | break; |
2277 | case R_SPARC_HI22: | |
2278 | fprintf(outfile, | |
2279 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2280 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2281 | " & ~0x3fffff) " | |
ae228531 | 2282 | " | (((%s + %d) >> 10) & 0x3fffff);\n", |
4a1a1707 | 2283 | reloc_offset, reloc_offset, relname, addend); |
d014c98c FB |
2284 | break; |
2285 | case R_SPARC_LO10: | |
2286 | fprintf(outfile, | |
2287 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2288 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2289 | " & ~0x3ff) " | |
2290 | " | ((%s + %d) & 0x3ff);\n", | |
4a1a1707 | 2291 | reloc_offset, reloc_offset, relname, addend); |
d014c98c FB |
2292 | break; |
2293 | case R_SPARC_WDISP30: | |
2294 | fprintf(outfile, | |
2295 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2296 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2297 | " & ~0x3fffffff) " | |
ae228531 | 2298 | " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) " |
d014c98c | 2299 | " & 0x3fffffff);\n", |
4a1a1707 | 2300 | reloc_offset, reloc_offset, relname, addend, |
3442e896 | 2301 | reloc_offset); |
d014c98c | 2302 | break; |
fdbb4691 FB |
2303 | case R_SPARC_WDISP22: |
2304 | fprintf(outfile, | |
2305 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2306 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2307 | " & ~0x3fffff) " | |
2308 | " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) " | |
2309 | " & 0x3fffff);\n", | |
2310 | rel->r_offset - start_offset, | |
2311 | rel->r_offset - start_offset, | |
4a1a1707 | 2312 | relname, addend, |
fdbb4691 FB |
2313 | rel->r_offset - start_offset); |
2314 | break; | |
d014c98c FB |
2315 | default: |
2316 | error("unsupported sparc relocation (%d)", type); | |
2317 | } | |
2318 | } | |
2319 | } | |
2320 | } | |
2321 | #elif defined(HOST_SPARC64) | |
2322 | { | |
4a1a1707 | 2323 | char relname[256]; |
d014c98c FB |
2324 | int type; |
2325 | int addend; | |
3442e896 | 2326 | int reloc_offset; |
d014c98c | 2327 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
ae228531 FB |
2328 | if (rel->r_offset >= start_offset && |
2329 | rel->r_offset < start_offset + copy_size) { | |
d014c98c | 2330 | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
4a1a1707 | 2331 | get_reloc_expr(relname, sizeof(relname), sym_name); |
74ccb34e | 2332 | type = ELF32_R_TYPE(rel->r_info); |
d014c98c | 2333 | addend = rel->r_addend; |
3442e896 | 2334 | reloc_offset = rel->r_offset - start_offset; |
d014c98c FB |
2335 | switch(type) { |
2336 | case R_SPARC_32: | |
2337 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", | |
4a1a1707 | 2338 | reloc_offset, relname, addend); |
d014c98c FB |
2339 | break; |
2340 | case R_SPARC_HI22: | |
2341 | fprintf(outfile, | |
2342 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2343 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2344 | " & ~0x3fffff) " | |
ae228531 | 2345 | " | (((%s + %d) >> 10) & 0x3fffff);\n", |
4a1a1707 | 2346 | reloc_offset, reloc_offset, relname, addend); |
d014c98c FB |
2347 | break; |
2348 | case R_SPARC_LO10: | |
2349 | fprintf(outfile, | |
2350 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2351 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2352 | " & ~0x3ff) " | |
2353 | " | ((%s + %d) & 0x3ff);\n", | |
4a1a1707 | 2354 | reloc_offset, reloc_offset, relname, addend); |
d014c98c | 2355 | break; |
74ccb34e FB |
2356 | case R_SPARC_OLO10: |
2357 | addend += ELF64_R_TYPE_DATA (rel->r_info); | |
2358 | fprintf(outfile, | |
2359 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2360 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2361 | " & ~0x3ff) " | |
2362 | " | ((%s + %d) & 0x3ff);\n", | |
4a1a1707 | 2363 | reloc_offset, reloc_offset, relname, addend); |
74ccb34e | 2364 | break; |
d014c98c FB |
2365 | case R_SPARC_WDISP30: |
2366 | fprintf(outfile, | |
2367 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2368 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2369 | " & ~0x3fffffff) " | |
ae228531 | 2370 | " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) " |
d014c98c | 2371 | " & 0x3fffffff);\n", |
4a1a1707 | 2372 | reloc_offset, reloc_offset, relname, addend, |
3442e896 | 2373 | reloc_offset); |
d014c98c | 2374 | break; |
74ccb34e FB |
2375 | case R_SPARC_WDISP22: |
2376 | fprintf(outfile, | |
2377 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2378 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2379 | " & ~0x3fffff) " | |
2380 | " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) " | |
2381 | " & 0x3fffff);\n", | |
4a1a1707 | 2382 | reloc_offset, reloc_offset, relname, addend, |
74ccb34e FB |
2383 | reloc_offset); |
2384 | break; | |
b80029ca TS |
2385 | case R_SPARC_HH22: |
2386 | fprintf(outfile, | |
2387 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2388 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2389 | " & ~0x00000000) " | |
2390 | " | (((%s + %d) >> 42) & 0x00000000);\n", | |
4a1a1707 | 2391 | reloc_offset, reloc_offset, relname, addend); |
b80029ca TS |
2392 | break; |
2393 | ||
2394 | case R_SPARC_LM22: | |
2395 | fprintf(outfile, | |
2396 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2397 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2398 | " & ~0x00000000) " | |
2399 | " | (((%s + %d) >> 10) & 0x00000000);\n", | |
4a1a1707 | 2400 | reloc_offset, reloc_offset, relname, addend); |
b80029ca TS |
2401 | break; |
2402 | ||
2403 | case R_SPARC_HM10: | |
2404 | fprintf(outfile, | |
2405 | " *(uint32_t *)(gen_code_ptr + %d) = " | |
2406 | "((*(uint32_t *)(gen_code_ptr + %d)) " | |
2407 | " & ~0x00000000) " | |
2408 | " | ((((%s + %d) >> 32 & 0x3ff)) & 0x00000000);\n", | |
4a1a1707 | 2409 | reloc_offset, reloc_offset, relname, addend); |
b80029ca TS |
2410 | break; |
2411 | ||
d014c98c | 2412 | default: |
4a1a1707 | 2413 | error("unsupported sparc64 relocation (%d) for symbol %s", type, relname); |
d014c98c FB |
2414 | } |
2415 | } | |
2416 | } | |
2417 | } | |
38e584a0 FB |
2418 | #elif defined(HOST_M68K) |
2419 | { | |
4a1a1707 | 2420 | char relname[256]; |
38e584a0 FB |
2421 | int type; |
2422 | int addend; | |
3442e896 | 2423 | int reloc_offset; |
38e584a0 FB |
2424 | Elf32_Sym *sym; |
2425 | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
2426 | if (rel->r_offset >= start_offset && | |
2427 | rel->r_offset < start_offset + copy_size) { | |
2428 | sym = &(symtab[ELFW(R_SYM)(rel->r_info)]); | |
2429 | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; | |
4a1a1707 | 2430 | get_reloc_expr(relname, sizeof(relname), sym_name); |
38e584a0 FB |
2431 | type = ELF32_R_TYPE(rel->r_info); |
2432 | addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend; | |
3442e896 | 2433 | reloc_offset = rel->r_offset - start_offset; |
38e584a0 FB |
2434 | switch(type) { |
2435 | case R_68K_32: | |
2436 | fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ; | |
5fafdf24 | 2437 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n", |
4a1a1707 | 2438 | reloc_offset, relname, addend ); |
38e584a0 FB |
2439 | break; |
2440 | case R_68K_PC32: | |
2441 | fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset); | |
5fafdf24 | 2442 | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n", |
4a1a1707 | 2443 | reloc_offset, relname, reloc_offset, /*sym->st_value+*/ addend); |
38e584a0 FB |
2444 | break; |
2445 | default: | |
2446 | error("unsupported m68k relocation (%d)", type); | |
2447 | } | |
2448 | } | |
2449 | } | |
ff1f20a3 | 2450 | } |
f54b3f92 AJ |
2451 | #elif defined(HOST_HPPA) |
2452 | { | |
2453 | char relname[256]; | |
2454 | int type, is_label; | |
2455 | int addend; | |
2456 | int reloc_offset; | |
2457 | for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) { | |
2458 | if (rel->r_offset >= start_offset && | |
2459 | rel->r_offset < start_offset + copy_size) { | |
2460 | sym_name = get_rel_sym_name(rel); | |
2461 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; | |
2462 | is_label = get_reloc_expr(relname, sizeof(relname), sym_name); | |
2463 | type = ELF32_R_TYPE(rel->r_info); | |
2464 | addend = rel->r_addend; | |
2465 | reloc_offset = rel->r_offset - start_offset; | |
2466 | ||
2467 | if (is_label) { | |
2468 | switch (type) { | |
2469 | case R_PARISC_PCREL17F: | |
2470 | fprintf(outfile, | |
2471 | " tcg_out_reloc(s, gen_code_ptr + %d, %d, %s, %d);\n", | |
2472 | reloc_offset, type, relname, addend); | |
2473 | break; | |
2474 | default: | |
2475 | error("unsupported hppa label relocation (%d)", type); | |
2476 | } | |
2477 | } else { | |
2478 | switch (type) { | |
2479 | case R_PARISC_DIR21L: | |
2480 | fprintf(outfile, | |
2481 | " hppa_patch21l((uint32_t *)(gen_code_ptr + %d), %s, %d);\n", | |
2482 | reloc_offset, relname, addend); | |
2483 | break; | |
2484 | case R_PARISC_DIR14R: | |
2485 | fprintf(outfile, | |
2486 | " hppa_patch14r((uint32_t *)(gen_code_ptr + %d), %s, %d);\n", | |
2487 | reloc_offset, relname, addend); | |
2488 | break; | |
2489 | case R_PARISC_PCREL17F: | |
2490 | if (strstart(sym_name, "__op_gen_label", NULL)) { | |
2491 | fprintf(outfile, | |
2492 | " hppa_patch17f((uint32_t *)(gen_code_ptr + %d), %s, %d);\n", | |
2493 | reloc_offset, relname, addend); | |
2494 | } else { | |
2495 | fprintf(outfile, | |
2496 | " HPPA_RECORD_BRANCH(hppa_stubs, (uint32_t *)(gen_code_ptr + %d), %s);\n", | |
2497 | reloc_offset, relname); | |
2498 | } | |
2499 | break; | |
2500 | case R_PARISC_DPREL21L: | |
2501 | if (strstart(sym_name, "__op_param", &p)) | |
2502 | fprintf(outfile, | |
2503 | " hppa_load_imm21l((uint32_t *)(gen_code_ptr + %d), param%s, %d);\n", | |
2504 | reloc_offset, p, addend); | |
2505 | else | |
2506 | fprintf(outfile, | |
2507 | " hppa_patch21l_dprel((uint32_t *)(gen_code_ptr + %d), %s, %d);\n", | |
2508 | reloc_offset, relname, addend); | |
2509 | break; | |
2510 | case R_PARISC_DPREL14R: | |
2511 | if (strstart(sym_name, "__op_param", &p)) | |
2512 | fprintf(outfile, | |
2513 | " hppa_load_imm14r((uint32_t *)(gen_code_ptr + %d), param%s, %d);\n", | |
2514 | reloc_offset, p, addend); | |
2515 | else | |
2516 | fprintf(outfile, | |
2517 | " hppa_patch14r_dprel((uint32_t *)(gen_code_ptr + %d), %s, %d);\n", | |
2518 | reloc_offset, relname, addend); | |
2519 | break; | |
2520 | default: | |
2521 | error("unsupported hppa relocation (%d)", type); | |
2522 | } | |
2523 | } | |
2524 | } | |
2525 | } | |
2526 | } | |
9617efe8 | 2527 | #elif defined(HOST_MIPS) || defined(HOST_MIPS64) |
c4b89d18 TS |
2528 | { |
2529 | for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) { | |
2530 | if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) { | |
4a1a1707 | 2531 | char relname[256]; |
c4b89d18 TS |
2532 | int type; |
2533 | int addend; | |
2534 | int reloc_offset; | |
2535 | ||
2536 | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; | |
2537 | /* the compiler leave some unnecessary references to the code */ | |
2538 | if (sym_name[0] == '\0') | |
2539 | continue; | |
4a1a1707 | 2540 | get_reloc_expr(relname, sizeof(relname), sym_name); |
c4b89d18 TS |
2541 | type = ELF32_R_TYPE(rel->r_info); |
2542 | addend = get32((uint32_t *)(text + rel->r_offset)); | |
2543 | reloc_offset = rel->r_offset - start_offset; | |
2544 | switch (type) { | |
9617efe8 TS |
2545 | case R_MIPS_26: |
2546 | fprintf(outfile, " /* R_MIPS_26 RELOC, offset 0x%x, name %s */\n", | |
2547 | rel->r_offset, sym_name); | |
2548 | fprintf(outfile, | |
2549 | " *(uint32_t *)(gen_code_ptr + 0x%x) = " | |
2550 | "(0x%x & ~0x3fffff) " | |
2551 | "| ((0x%x + ((%s - (*(uint32_t *)(gen_code_ptr + 0x%x))) >> 2)) " | |
2552 | " & 0x3fffff);\n", | |
4a1a1707 | 2553 | reloc_offset, addend, addend, relname, reloc_offset); |
9617efe8 | 2554 | break; |
c4b89d18 TS |
2555 | case R_MIPS_HI16: |
2556 | fprintf(outfile, " /* R_MIPS_HI16 RELOC, offset 0x%x, name %s */\n", | |
2557 | rel->r_offset, sym_name); | |
2558 | fprintf(outfile, | |
2559 | " *(uint32_t *)(gen_code_ptr + 0x%x) = " | |
2560 | "((*(uint32_t *)(gen_code_ptr + 0x%x)) " | |
2561 | " & ~0xffff) " | |
2562 | " | (((%s - 0x8000) >> 16) & 0xffff);\n", | |
4a1a1707 | 2563 | reloc_offset, reloc_offset, relname); |
c4b89d18 TS |
2564 | break; |
2565 | case R_MIPS_LO16: | |
2566 | fprintf(outfile, " /* R_MIPS_LO16 RELOC, offset 0x%x, name %s */\n", | |
2567 | rel->r_offset, sym_name); | |
2568 | fprintf(outfile, | |
2569 | " *(uint32_t *)(gen_code_ptr + 0x%x) = " | |
2570 | "((*(uint32_t *)(gen_code_ptr + 0x%x)) " | |
2571 | " & ~0xffff) " | |
2572 | " | (%s & 0xffff);\n", | |
4a1a1707 | 2573 | reloc_offset, reloc_offset, relname); |
c4b89d18 TS |
2574 | break; |
2575 | case R_MIPS_PC16: | |
2576 | fprintf(outfile, " /* R_MIPS_PC16 RELOC, offset 0x%x, name %s */\n", | |
2577 | rel->r_offset, sym_name); | |
2578 | fprintf(outfile, | |
2579 | " *(uint32_t *)(gen_code_ptr + 0x%x) = " | |
2580 | "(0x%x & ~0xffff) " | |
2581 | "| ((0x%x + ((%s - (*(uint32_t *)(gen_code_ptr + 0x%x))) >> 2)) " | |
2582 | " & 0xffff);\n", | |
4a1a1707 | 2583 | reloc_offset, addend, addend, relname, reloc_offset); |
c4b89d18 TS |
2584 | break; |
2585 | case R_MIPS_GOT16: | |
2586 | case R_MIPS_CALL16: | |
2587 | fprintf(outfile, " /* R_MIPS_GOT16 RELOC, offset 0x%x, name %s */\n", | |
2588 | rel->r_offset, sym_name); | |
2589 | fprintf(outfile, | |
2590 | " *(uint32_t *)(gen_code_ptr + 0x%x) = " | |
2591 | "((*(uint32_t *)(gen_code_ptr + 0x%x)) " | |
2592 | " & ~0xffff) " | |
2593 | " | (((%s - 0x8000) >> 16) & 0xffff);\n", | |
4a1a1707 | 2594 | reloc_offset, reloc_offset, relname); |
c4b89d18 TS |
2595 | break; |
2596 | default: | |
2597 | error("unsupported MIPS relocation (%d)", type); | |
2598 | } | |
2599 | } | |
2600 | } | |
2601 | } | |
a2a64a1f AZ |
2602 | #elif defined(HOST_ARM) |
2603 | error("dyngen targets not supported on ARM"); | |
810260a8 | 2604 | #elif defined(HOST_PPC64) |
2605 | error("dyngen targets not supported on PPC64"); | |
ce11fedc FB |
2606 | #else |
2607 | #error unsupported CPU | |
2608 | #endif | |
dc99065b FB |
2609 | fprintf(outfile, " gen_code_ptr += %d;\n", copy_size); |
2610 | fprintf(outfile, "}\n"); | |
2611 | fprintf(outfile, "break;\n\n"); | |
2612 | } else { | |
2613 | fprintf(outfile, "static inline void gen_%s(", name); | |
2614 | if (nb_args == 0) { | |
2615 | fprintf(outfile, "void"); | |
2616 | } else { | |
2617 | for(i = 0; i < nb_args; i++) { | |
2618 | if (i != 0) | |
2619 | fprintf(outfile, ", "); | |
2620 | fprintf(outfile, "long param%d", i + 1); | |
367e86e8 FB |
2621 | } |
2622 | } | |
dc99065b FB |
2623 | fprintf(outfile, ")\n"); |
2624 | fprintf(outfile, "{\n"); | |
2625 | for(i = 0; i < nb_args; i++) { | |
2626 | fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1); | |
2627 | } | |
2628 | fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name); | |
2629 | fprintf(outfile, "}\n\n"); | |
367e86e8 | 2630 | } |
367e86e8 FB |
2631 | } |
2632 | ||
8fcd3692 | 2633 | static int gen_file(FILE *outfile, int out_type) |
367e86e8 | 2634 | { |
67b915a5 FB |
2635 | int i; |
2636 | EXE_SYM *sym; | |
367e86e8 | 2637 | |
d219f7e7 | 2638 | if (out_type == OUT_INDEX_OP) { |
dc99065b | 2639 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
82eec0a1 | 2640 | const char *name; |
67b915a5 | 2641 | name = get_sym_name(sym); |
82eec0a1 | 2642 | if (strstart(name, OP_PREFIX, NULL)) { |
67b915a5 | 2643 | gen_code(name, sym->st_value, sym->st_size, outfile, 2); |
dc99065b FB |
2644 | } |
2645 | } | |
d219f7e7 FB |
2646 | } else if (out_type == OUT_GEN_OP) { |
2647 | /* generate gen_xxx functions */ | |
d219f7e7 FB |
2648 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
2649 | const char *name; | |
67b915a5 | 2650 | name = get_sym_name(sym); |
d219f7e7 | 2651 | if (strstart(name, OP_PREFIX, NULL)) { |
82eec0a1 | 2652 | #if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF) |
67b915a5 | 2653 | if (sym->st_shndx != text_shndx) |
d219f7e7 | 2654 | error("invalid section for opcode (0x%x)", sym->st_shndx); |
82eec0a1 | 2655 | #endif |
67b915a5 | 2656 | gen_code(name, sym->st_value, sym->st_size, outfile, 0); |
d219f7e7 FB |
2657 | } |
2658 | } | |
3b46e624 | 2659 | |
dc99065b FB |
2660 | } else { |
2661 | /* generate big code generation switch */ | |
46152182 PB |
2662 | |
2663 | #ifdef HOST_ARM | |
a2a64a1f | 2664 | error("dyngen targets not supported on ARM"); |
ff1f20a3 | 2665 | #endif |
b8076a74 | 2666 | #ifdef HOST_IA64 |
57fec1fe | 2667 | #error broken |
b8076a74 FB |
2668 | { |
2669 | long addend, not_first = 0; | |
2670 | unsigned long sym_idx; | |
2671 | int index, max_index; | |
2672 | const char *sym_name; | |
2673 | EXE_RELOC *rel; | |
2674 | ||
2675 | max_index = -1; | |
2676 | for (i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
2677 | sym_idx = ELF64_R_SYM(rel->r_info); | |
2678 | sym_name = (strtab + symtab[sym_idx].st_name); | |
2679 | if (strstart(sym_name, "__op_gen_label", NULL)) | |
2680 | continue; | |
2681 | if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B) | |
2682 | continue; | |
2683 | ||
2684 | addend = rel->r_addend; | |
2685 | index = get_plt_index(sym_name, addend); | |
2686 | if (index <= max_index) | |
2687 | continue; | |
2688 | max_index = index; | |
2689 | fprintf(outfile, " extern void %s(void);\n", sym_name); | |
2690 | } | |
2691 | ||
2692 | fprintf(outfile, | |
2693 | " struct ia64_fixup *plt_fixes = NULL, " | |
2694 | "*ltoff_fixes = NULL;\n" | |
2695 | " static long plt_target[] = {\n\t"); | |
2696 | ||
2697 | max_index = -1; | |
2698 | for (i = 0, rel = relocs;i < nb_relocs; i++, rel++) { | |
2699 | sym_idx = ELF64_R_SYM(rel->r_info); | |
2700 | sym_name = (strtab + symtab[sym_idx].st_name); | |
2701 | if (strstart(sym_name, "__op_gen_label", NULL)) | |
2702 | continue; | |
2703 | if (ELF64_R_TYPE(rel->r_info) != R_IA64_PCREL21B) | |
2704 | continue; | |
2705 | ||
2706 | addend = rel->r_addend; | |
2707 | index = get_plt_index(sym_name, addend); | |
2708 | if (index <= max_index) | |
2709 | continue; | |
2710 | max_index = index; | |
2711 | ||
2712 | if (not_first) | |
2713 | fprintf(outfile, ",\n\t"); | |
2714 | not_first = 1; | |
2715 | if (addend) | |
2716 | fprintf(outfile, "(long) &%s + %ld", sym_name, addend); | |
2717 | else | |
2718 | fprintf(outfile, "(long) &%s", sym_name); | |
2719 | } | |
2720 | fprintf(outfile, "\n };\n" | |
2721 | " unsigned int plt_offset[%u] = { 0 };\n", max_index + 1); | |
2722 | } | |
2723 | #endif | |
ff1f20a3 | 2724 | |
dc99065b FB |
2725 | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
2726 | const char *name; | |
67b915a5 | 2727 | name = get_sym_name(sym); |
dc99065b | 2728 | if (strstart(name, OP_PREFIX, NULL)) { |
367e86e8 | 2729 | #if 0 |
5fafdf24 | 2730 | printf("%4d: %s pos=0x%08x len=%d\n", |
dc99065b | 2731 | i, name, sym->st_value, sym->st_size); |
367e86e8 | 2732 | #endif |
82eec0a1 | 2733 | #if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF) |
67b915a5 | 2734 | if (sym->st_shndx != text_shndx) |
dc99065b | 2735 | error("invalid section for opcode (0x%x)", sym->st_shndx); |
82eec0a1 | 2736 | #endif |
67b915a5 | 2737 | gen_code(name, sym->st_value, sym->st_size, outfile, 1); |
dc99065b FB |
2738 | } |
2739 | } | |
367e86e8 FB |
2740 | } |
2741 | ||
367e86e8 FB |
2742 | return 0; |
2743 | } | |
2744 | ||
8fcd3692 | 2745 | static void usage(void) |
367e86e8 FB |
2746 | { |
2747 | printf("dyngen (c) 2003 Fabrice Bellard\n" | |
dc99065b FB |
2748 | "usage: dyngen [-o outfile] [-c] objfile\n" |
2749 | "Generate a dynamic code generator from an object file\n" | |
2750 | "-c output enum of operations\n" | |
d219f7e7 | 2751 | "-g output gen_op_xx() functions\n" |
dc99065b | 2752 | ); |
367e86e8 FB |
2753 | exit(1); |
2754 | } | |
2755 | ||
2756 | int main(int argc, char **argv) | |
2757 | { | |
d219f7e7 | 2758 | int c, out_type; |
367e86e8 FB |
2759 | const char *filename, *outfilename; |
2760 | FILE *outfile; | |
2761 | ||
2762 | outfilename = "out.c"; | |
d219f7e7 | 2763 | out_type = OUT_CODE; |
367e86e8 | 2764 | for(;;) { |
d219f7e7 | 2765 | c = getopt(argc, argv, "ho:cg"); |
367e86e8 FB |
2766 | if (c == -1) |
2767 | break; | |
2768 | switch(c) { | |
2769 | case 'h': | |
2770 | usage(); | |
2771 | break; | |
2772 | case 'o': | |
2773 | outfilename = optarg; | |
2774 | break; | |
dc99065b | 2775 | case 'c': |
d219f7e7 FB |
2776 | out_type = OUT_INDEX_OP; |
2777 | break; | |
2778 | case 'g': | |
2779 | out_type = OUT_GEN_OP; | |
dc99065b | 2780 | break; |
367e86e8 FB |
2781 | } |
2782 | } | |
2783 | if (optind >= argc) | |
2784 | usage(); | |
2785 | filename = argv[optind]; | |
2786 | outfile = fopen(outfilename, "w"); | |
2787 | if (!outfile) | |
2788 | error("could not open '%s'", outfilename); | |
67b915a5 FB |
2789 | |
2790 | load_object(filename); | |
2791 | gen_file(outfile, out_type); | |
367e86e8 FB |
2792 | fclose(outfile); |
2793 | return 0; | |
2794 | } |