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