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