root / dyngen.c @ a8c490cd
History | View | Annotate | Download (60.2 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 | 67b915a5 | bellard | * The COFF object format support was extracted from Kazu's QEMU port
|
7 | 67b915a5 | bellard | * to Win32.
|
8 | 67b915a5 | bellard | *
|
9 | 7d13299d | bellard | * This program is free software; you can redistribute it and/or modify
|
10 | 7d13299d | bellard | * it under the terms of the GNU General Public License as published by
|
11 | 7d13299d | bellard | * the Free Software Foundation; either version 2 of the License, or
|
12 | 7d13299d | bellard | * (at your option) any later version.
|
13 | 7d13299d | bellard | *
|
14 | 7d13299d | bellard | * This program is distributed in the hope that it will be useful,
|
15 | 7d13299d | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 | 7d13299d | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 | 7d13299d | bellard | * GNU General Public License for more details.
|
18 | 7d13299d | bellard | *
|
19 | 7d13299d | bellard | * You should have received a copy of the GNU General Public License
|
20 | 7d13299d | bellard | * along with this program; if not, write to the Free Software
|
21 | 7d13299d | bellard | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
22 | 7d13299d | bellard | */
|
23 | 367e86e8 | bellard | #include <stdlib.h> |
24 | 367e86e8 | bellard | #include <stdio.h> |
25 | 04369ff2 | bellard | #include <string.h> |
26 | 367e86e8 | bellard | #include <stdarg.h> |
27 | 367e86e8 | bellard | #include <inttypes.h> |
28 | 367e86e8 | bellard | #include <unistd.h> |
29 | 367e86e8 | bellard | #include <fcntl.h> |
30 | 367e86e8 | bellard | |
31 | abcd5da7 | bellard | #include "config-host.h" |
32 | ce11fedc | bellard | |
33 | 11d9f695 | bellard | /* NOTE: we test CONFIG_WIN32 instead of _WIN32 to enabled cross
|
34 | 11d9f695 | bellard | compilation */
|
35 | 11d9f695 | bellard | #if defined(CONFIG_WIN32)
|
36 | 67b915a5 | bellard | #define CONFIG_FORMAT_COFF
|
37 | 67b915a5 | bellard | #else
|
38 | 67b915a5 | bellard | #define CONFIG_FORMAT_ELF
|
39 | 67b915a5 | bellard | #endif
|
40 | 67b915a5 | bellard | |
41 | 67b915a5 | bellard | #ifdef CONFIG_FORMAT_ELF
|
42 | 67b915a5 | bellard | |
43 | ce11fedc | bellard | /* elf format definitions. We use these macros to test the CPU to
|
44 | ce11fedc | bellard | allow cross compilation (this tool must be ran on the build
|
45 | ce11fedc | bellard | platform) */
|
46 | ce11fedc | bellard | #if defined(HOST_I386)
|
47 | ce11fedc | bellard | |
48 | ce11fedc | bellard | #define ELF_CLASS ELFCLASS32
|
49 | ce11fedc | bellard | #define ELF_ARCH EM_386
|
50 | ce11fedc | bellard | #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
|
51 | ce11fedc | bellard | #undef ELF_USES_RELOCA
|
52 | ce11fedc | bellard | |
53 | bc51c5c9 | bellard | #elif defined(HOST_AMD64)
|
54 | bc51c5c9 | bellard | |
55 | bc51c5c9 | bellard | #define ELF_CLASS ELFCLASS64
|
56 | bc51c5c9 | bellard | #define ELF_ARCH EM_X86_64
|
57 | bc51c5c9 | bellard | #define elf_check_arch(x) ((x) == EM_X86_64)
|
58 | bc51c5c9 | bellard | #define ELF_USES_RELOCA
|
59 | bc51c5c9 | bellard | |
60 | ce11fedc | bellard | #elif defined(HOST_PPC)
|
61 | ce11fedc | bellard | |
62 | ce11fedc | bellard | #define ELF_CLASS ELFCLASS32
|
63 | ce11fedc | bellard | #define ELF_ARCH EM_PPC
|
64 | ce11fedc | bellard | #define elf_check_arch(x) ((x) == EM_PPC)
|
65 | ce11fedc | bellard | #define ELF_USES_RELOCA
|
66 | ce11fedc | bellard | |
67 | ce11fedc | bellard | #elif defined(HOST_S390)
|
68 | ce11fedc | bellard | |
69 | ce11fedc | bellard | #define ELF_CLASS ELFCLASS32
|
70 | ce11fedc | bellard | #define ELF_ARCH EM_S390
|
71 | ce11fedc | bellard | #define elf_check_arch(x) ((x) == EM_S390)
|
72 | ce11fedc | bellard | #define ELF_USES_RELOCA
|
73 | 367e86e8 | bellard | |
74 | ce11fedc | bellard | #elif defined(HOST_ALPHA)
|
75 | ce11fedc | bellard | |
76 | ce11fedc | bellard | #define ELF_CLASS ELFCLASS64
|
77 | ce11fedc | bellard | #define ELF_ARCH EM_ALPHA
|
78 | ce11fedc | bellard | #define elf_check_arch(x) ((x) == EM_ALPHA)
|
79 | ce11fedc | bellard | #define ELF_USES_RELOCA
|
80 | ce11fedc | bellard | |
81 | efdea7bf | bellard | #elif defined(HOST_IA64)
|
82 | efdea7bf | bellard | |
83 | efdea7bf | bellard | #define ELF_CLASS ELFCLASS64
|
84 | efdea7bf | bellard | #define ELF_ARCH EM_IA_64
|
85 | efdea7bf | bellard | #define elf_check_arch(x) ((x) == EM_IA_64)
|
86 | efdea7bf | bellard | #define ELF_USES_RELOCA
|
87 | efdea7bf | bellard | |
88 | d014c98c | bellard | #elif defined(HOST_SPARC)
|
89 | d014c98c | bellard | |
90 | d014c98c | bellard | #define ELF_CLASS ELFCLASS32
|
91 | d014c98c | bellard | #define ELF_ARCH EM_SPARC
|
92 | d014c98c | bellard | #define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
|
93 | d014c98c | bellard | #define ELF_USES_RELOCA
|
94 | d014c98c | bellard | |
95 | d014c98c | bellard | #elif defined(HOST_SPARC64)
|
96 | d014c98c | bellard | |
97 | d014c98c | bellard | #define ELF_CLASS ELFCLASS64
|
98 | d014c98c | bellard | #define ELF_ARCH EM_SPARCV9
|
99 | d014c98c | bellard | #define elf_check_arch(x) ((x) == EM_SPARCV9)
|
100 | d014c98c | bellard | #define ELF_USES_RELOCA
|
101 | d014c98c | bellard | |
102 | ff1f20a3 | bellard | #elif defined(HOST_ARM)
|
103 | ff1f20a3 | bellard | |
104 | ff1f20a3 | bellard | #define ELF_CLASS ELFCLASS32
|
105 | ff1f20a3 | bellard | #define ELF_ARCH EM_ARM
|
106 | ff1f20a3 | bellard | #define elf_check_arch(x) ((x) == EM_ARM)
|
107 | ff1f20a3 | bellard | #define ELF_USES_RELOC
|
108 | ff1f20a3 | bellard | |
109 | 38e584a0 | bellard | #elif defined(HOST_M68K)
|
110 | 38e584a0 | bellard | |
111 | 38e584a0 | bellard | #define ELF_CLASS ELFCLASS32
|
112 | 38e584a0 | bellard | #define ELF_ARCH EM_68K
|
113 | 38e584a0 | bellard | #define elf_check_arch(x) ((x) == EM_68K)
|
114 | 38e584a0 | bellard | #define ELF_USES_RELOCA
|
115 | 38e584a0 | bellard | |
116 | ce11fedc | bellard | #else
|
117 | ce11fedc | bellard | #error unsupported CPU - please update the code
|
118 | ce11fedc | bellard | #endif
|
119 | ce11fedc | bellard | |
120 | efdea7bf | bellard | #include "elf.h" |
121 | efdea7bf | bellard | |
122 | ce11fedc | bellard | #if ELF_CLASS == ELFCLASS32
|
123 | ce11fedc | bellard | typedef int32_t host_long;
|
124 | ce11fedc | bellard | typedef uint32_t host_ulong;
|
125 | efdea7bf | bellard | #define swabls(x) swab32s(x)
|
126 | ce11fedc | bellard | #else
|
127 | ce11fedc | bellard | typedef int64_t host_long;
|
128 | ce11fedc | bellard | typedef uint64_t host_ulong;
|
129 | efdea7bf | bellard | #define swabls(x) swab64s(x)
|
130 | fb3e5849 | bellard | #endif
|
131 | fb3e5849 | bellard | |
132 | fe319756 | bellard | #ifdef ELF_USES_RELOCA
|
133 | fe319756 | bellard | #define SHT_RELOC SHT_RELA
|
134 | fe319756 | bellard | #else
|
135 | fe319756 | bellard | #define SHT_RELOC SHT_REL
|
136 | fe319756 | bellard | #endif
|
137 | fe319756 | bellard | |
138 | 67b915a5 | bellard | #define EXE_RELOC ELF_RELOC
|
139 | 67b915a5 | bellard | #define EXE_SYM ElfW(Sym)
|
140 | 67b915a5 | bellard | |
141 | 67b915a5 | bellard | #endif /* CONFIG_FORMAT_ELF */ |
142 | 67b915a5 | bellard | |
143 | 67b915a5 | bellard | #ifdef CONFIG_FORMAT_COFF
|
144 | 67b915a5 | bellard | |
145 | 67b915a5 | bellard | #include "a.out.h" |
146 | 67b915a5 | bellard | |
147 | 67b915a5 | bellard | typedef int32_t host_long;
|
148 | 67b915a5 | bellard | typedef uint32_t host_ulong;
|
149 | 67b915a5 | bellard | |
150 | 67b915a5 | bellard | #define FILENAMELEN 256 |
151 | 67b915a5 | bellard | |
152 | 67b915a5 | bellard | typedef struct coff_sym { |
153 | 67b915a5 | bellard | struct external_syment *st_syment;
|
154 | 67b915a5 | bellard | char st_name[FILENAMELEN];
|
155 | 67b915a5 | bellard | uint32_t st_value; |
156 | 67b915a5 | bellard | int st_size;
|
157 | 67b915a5 | bellard | uint8_t st_type; |
158 | 67b915a5 | bellard | uint8_t st_shndx; |
159 | 67b915a5 | bellard | } coff_Sym; |
160 | 67b915a5 | bellard | |
161 | 67b915a5 | bellard | typedef struct coff_rel { |
162 | 67b915a5 | bellard | struct external_reloc *r_reloc;
|
163 | 67b915a5 | bellard | int r_offset;
|
164 | 67b915a5 | bellard | uint8_t r_type; |
165 | 67b915a5 | bellard | } coff_Rel; |
166 | 67b915a5 | bellard | |
167 | 67b915a5 | bellard | #define EXE_RELOC struct coff_rel |
168 | 67b915a5 | bellard | #define EXE_SYM struct coff_sym |
169 | 67b915a5 | bellard | |
170 | 67b915a5 | bellard | #endif /* CONFIG_FORMAT_COFF */ |
171 | 67b915a5 | bellard | |
172 | abcd5da7 | bellard | #include "bswap.h" |
173 | ce11fedc | bellard | |
174 | d219f7e7 | bellard | enum {
|
175 | d219f7e7 | bellard | OUT_GEN_OP, |
176 | d219f7e7 | bellard | OUT_CODE, |
177 | d219f7e7 | bellard | OUT_INDEX_OP, |
178 | d219f7e7 | bellard | }; |
179 | d219f7e7 | bellard | |
180 | 367e86e8 | bellard | /* all dynamically generated functions begin with this code */
|
181 | dc99065b | bellard | #define OP_PREFIX "op_" |
182 | 367e86e8 | bellard | |
183 | 67b915a5 | bellard | int do_swap;
|
184 | 67b915a5 | bellard | |
185 | 67b915a5 | bellard | void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...) |
186 | 367e86e8 | bellard | { |
187 | 67b915a5 | bellard | va_list ap; |
188 | 67b915a5 | bellard | va_start(ap, fmt); |
189 | 67b915a5 | bellard | fprintf(stderr, "dyngen: ");
|
190 | 67b915a5 | bellard | vfprintf(stderr, fmt, ap); |
191 | 67b915a5 | bellard | fprintf(stderr, "\n");
|
192 | 67b915a5 | bellard | va_end(ap); |
193 | 67b915a5 | bellard | exit(1);
|
194 | 67b915a5 | bellard | } |
195 | 367e86e8 | bellard | |
196 | 67b915a5 | bellard | void *load_data(int fd, long offset, unsigned int size) |
197 | 67b915a5 | bellard | { |
198 | 67b915a5 | bellard | char *data;
|
199 | 67b915a5 | bellard | |
200 | 67b915a5 | bellard | data = malloc(size); |
201 | 67b915a5 | bellard | if (!data)
|
202 | 67b915a5 | bellard | return NULL; |
203 | 67b915a5 | bellard | lseek(fd, offset, SEEK_SET); |
204 | 67b915a5 | bellard | if (read(fd, data, size) != size) {
|
205 | 67b915a5 | bellard | free(data); |
206 | 67b915a5 | bellard | return NULL; |
207 | 67b915a5 | bellard | } |
208 | 67b915a5 | bellard | return data;
|
209 | 367e86e8 | bellard | } |
210 | 67b915a5 | bellard | |
211 | 67b915a5 | bellard | int strstart(const char *str, const char *val, const char **ptr) |
212 | 67b915a5 | bellard | { |
213 | 67b915a5 | bellard | const char *p, *q; |
214 | 67b915a5 | bellard | p = str; |
215 | 67b915a5 | bellard | q = val; |
216 | 67b915a5 | bellard | while (*q != '\0') { |
217 | 67b915a5 | bellard | if (*p != *q)
|
218 | 67b915a5 | bellard | return 0; |
219 | 67b915a5 | bellard | p++; |
220 | 67b915a5 | bellard | q++; |
221 | 67b915a5 | bellard | } |
222 | 67b915a5 | bellard | if (ptr)
|
223 | 67b915a5 | bellard | *ptr = p; |
224 | 67b915a5 | bellard | return 1; |
225 | 67b915a5 | bellard | } |
226 | 67b915a5 | bellard | |
227 | 67b915a5 | bellard | void pstrcpy(char *buf, int buf_size, const char *str) |
228 | 67b915a5 | bellard | { |
229 | 67b915a5 | bellard | int c;
|
230 | 67b915a5 | bellard | char *q = buf;
|
231 | 67b915a5 | bellard | |
232 | 67b915a5 | bellard | if (buf_size <= 0) |
233 | 67b915a5 | bellard | return;
|
234 | 67b915a5 | bellard | |
235 | 67b915a5 | bellard | for(;;) {
|
236 | 67b915a5 | bellard | c = *str++; |
237 | 67b915a5 | bellard | if (c == 0 || q >= buf + buf_size - 1) |
238 | 67b915a5 | bellard | break;
|
239 | 67b915a5 | bellard | *q++ = c; |
240 | 67b915a5 | bellard | } |
241 | 67b915a5 | bellard | *q = '\0';
|
242 | 67b915a5 | bellard | } |
243 | 67b915a5 | bellard | |
244 | 367e86e8 | bellard | void swab16s(uint16_t *p)
|
245 | 367e86e8 | bellard | { |
246 | 367e86e8 | bellard | *p = bswap16(*p); |
247 | 367e86e8 | bellard | } |
248 | 367e86e8 | bellard | |
249 | 367e86e8 | bellard | void swab32s(uint32_t *p)
|
250 | 367e86e8 | bellard | { |
251 | 367e86e8 | bellard | *p = bswap32(*p); |
252 | 367e86e8 | bellard | } |
253 | 367e86e8 | bellard | |
254 | ce11fedc | bellard | void swab64s(uint64_t *p)
|
255 | 367e86e8 | bellard | { |
256 | 367e86e8 | bellard | *p = bswap64(*p); |
257 | 367e86e8 | bellard | } |
258 | 367e86e8 | bellard | |
259 | 67b915a5 | bellard | uint16_t get16(uint16_t *p) |
260 | 67b915a5 | bellard | { |
261 | 67b915a5 | bellard | uint16_t val; |
262 | 67b915a5 | bellard | val = *p; |
263 | 67b915a5 | bellard | if (do_swap)
|
264 | 67b915a5 | bellard | val = bswap16(val); |
265 | 67b915a5 | bellard | return val;
|
266 | 67b915a5 | bellard | } |
267 | 67b915a5 | bellard | |
268 | 67b915a5 | bellard | uint32_t get32(uint32_t *p) |
269 | 67b915a5 | bellard | { |
270 | 67b915a5 | bellard | uint32_t val; |
271 | 67b915a5 | bellard | val = *p; |
272 | 67b915a5 | bellard | if (do_swap)
|
273 | 67b915a5 | bellard | val = bswap32(val); |
274 | 67b915a5 | bellard | return val;
|
275 | 67b915a5 | bellard | } |
276 | 67b915a5 | bellard | |
277 | 67b915a5 | bellard | void put16(uint16_t *p, uint16_t val)
|
278 | 67b915a5 | bellard | { |
279 | 67b915a5 | bellard | if (do_swap)
|
280 | 67b915a5 | bellard | val = bswap16(val); |
281 | 67b915a5 | bellard | *p = val; |
282 | 67b915a5 | bellard | } |
283 | 67b915a5 | bellard | |
284 | 67b915a5 | bellard | void put32(uint32_t *p, uint32_t val)
|
285 | 67b915a5 | bellard | { |
286 | 67b915a5 | bellard | if (do_swap)
|
287 | 67b915a5 | bellard | val = bswap32(val); |
288 | 67b915a5 | bellard | *p = val; |
289 | 67b915a5 | bellard | } |
290 | 67b915a5 | bellard | |
291 | 67b915a5 | bellard | /* executable information */
|
292 | 67b915a5 | bellard | EXE_SYM *symtab; |
293 | 67b915a5 | bellard | int nb_syms;
|
294 | 67b915a5 | bellard | int text_shndx;
|
295 | 67b915a5 | bellard | uint8_t *text; |
296 | 67b915a5 | bellard | EXE_RELOC *relocs; |
297 | 67b915a5 | bellard | int nb_relocs;
|
298 | 67b915a5 | bellard | |
299 | 67b915a5 | bellard | #ifdef CONFIG_FORMAT_ELF
|
300 | 67b915a5 | bellard | |
301 | 67b915a5 | bellard | /* ELF file info */
|
302 | 67b915a5 | bellard | struct elf_shdr *shdr;
|
303 | 67b915a5 | bellard | uint8_t **sdata; |
304 | 67b915a5 | bellard | struct elfhdr ehdr;
|
305 | 67b915a5 | bellard | char *strtab;
|
306 | 67b915a5 | bellard | |
307 | 67b915a5 | bellard | int elf_must_swap(struct elfhdr *h) |
308 | 67b915a5 | bellard | { |
309 | 67b915a5 | bellard | union {
|
310 | 67b915a5 | bellard | uint32_t i; |
311 | 67b915a5 | bellard | uint8_t b[4];
|
312 | 67b915a5 | bellard | } swaptest; |
313 | 67b915a5 | bellard | |
314 | 67b915a5 | bellard | swaptest.i = 1;
|
315 | 67b915a5 | bellard | return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
|
316 | 67b915a5 | bellard | (swaptest.b[0] == 0); |
317 | 67b915a5 | bellard | } |
318 | 67b915a5 | bellard | |
319 | ce11fedc | bellard | void elf_swap_ehdr(struct elfhdr *h) |
320 | 367e86e8 | bellard | { |
321 | 367e86e8 | bellard | swab16s(&h->e_type); /* Object file type */
|
322 | 367e86e8 | bellard | swab16s(&h-> e_machine); /* Architecture */
|
323 | 367e86e8 | bellard | swab32s(&h-> e_version); /* Object file version */
|
324 | ce11fedc | bellard | swabls(&h-> e_entry); /* Entry point virtual address */
|
325 | ce11fedc | bellard | swabls(&h-> e_phoff); /* Program header table file offset */
|
326 | ce11fedc | bellard | swabls(&h-> e_shoff); /* Section header table file offset */
|
327 | 367e86e8 | bellard | swab32s(&h-> e_flags); /* Processor-specific flags */
|
328 | 367e86e8 | bellard | swab16s(&h-> e_ehsize); /* ELF header size in bytes */
|
329 | 367e86e8 | bellard | swab16s(&h-> e_phentsize); /* Program header table entry size */
|
330 | 367e86e8 | bellard | swab16s(&h-> e_phnum); /* Program header table entry count */
|
331 | 367e86e8 | bellard | swab16s(&h-> e_shentsize); /* Section header table entry size */
|
332 | 367e86e8 | bellard | swab16s(&h-> e_shnum); /* Section header table entry count */
|
333 | 367e86e8 | bellard | swab16s(&h-> e_shstrndx); /* Section header string table index */
|
334 | 367e86e8 | bellard | } |
335 | 367e86e8 | bellard | |
336 | ce11fedc | bellard | void elf_swap_shdr(struct elf_shdr *h) |
337 | 367e86e8 | bellard | { |
338 | 367e86e8 | bellard | swab32s(&h-> sh_name); /* Section name (string tbl index) */
|
339 | 367e86e8 | bellard | swab32s(&h-> sh_type); /* Section type */
|
340 | ce11fedc | bellard | swabls(&h-> sh_flags); /* Section flags */
|
341 | ce11fedc | bellard | swabls(&h-> sh_addr); /* Section virtual addr at execution */
|
342 | ce11fedc | bellard | swabls(&h-> sh_offset); /* Section file offset */
|
343 | ce11fedc | bellard | swabls(&h-> sh_size); /* Section size in bytes */
|
344 | 367e86e8 | bellard | swab32s(&h-> sh_link); /* Link to another section */
|
345 | 367e86e8 | bellard | swab32s(&h-> sh_info); /* Additional section information */
|
346 | ce11fedc | bellard | swabls(&h-> sh_addralign); /* Section alignment */
|
347 | ce11fedc | bellard | swabls(&h-> sh_entsize); /* Entry size if section holds table */
|
348 | 367e86e8 | bellard | } |
349 | 367e86e8 | bellard | |
350 | ce11fedc | bellard | void elf_swap_phdr(struct elf_phdr *h) |
351 | 367e86e8 | bellard | { |
352 | 367e86e8 | bellard | swab32s(&h->p_type); /* Segment type */
|
353 | ce11fedc | bellard | swabls(&h->p_offset); /* Segment file offset */
|
354 | ce11fedc | bellard | swabls(&h->p_vaddr); /* Segment virtual address */
|
355 | ce11fedc | bellard | swabls(&h->p_paddr); /* Segment physical address */
|
356 | ce11fedc | bellard | swabls(&h->p_filesz); /* Segment size in file */
|
357 | ce11fedc | bellard | swabls(&h->p_memsz); /* Segment size in memory */
|
358 | 367e86e8 | bellard | swab32s(&h->p_flags); /* Segment flags */
|
359 | ce11fedc | bellard | swabls(&h->p_align); /* Segment alignment */
|
360 | 367e86e8 | bellard | } |
361 | 367e86e8 | bellard | |
362 | fe319756 | bellard | void elf_swap_rel(ELF_RELOC *rel)
|
363 | fe319756 | bellard | { |
364 | fe319756 | bellard | swabls(&rel->r_offset); |
365 | fe319756 | bellard | swabls(&rel->r_info); |
366 | fe319756 | bellard | #ifdef ELF_USES_RELOCA
|
367 | fe319756 | bellard | swabls(&rel->r_addend); |
368 | fe319756 | bellard | #endif
|
369 | fe319756 | bellard | } |
370 | fe319756 | bellard | |
371 | 67b915a5 | bellard | struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr, |
372 | 67b915a5 | bellard | const char *name) |
373 | 67b915a5 | bellard | { |
374 | 67b915a5 | bellard | int i;
|
375 | 67b915a5 | bellard | const char *shname; |
376 | 67b915a5 | bellard | struct elf_shdr *sec;
|
377 | 367e86e8 | bellard | |
378 | 67b915a5 | bellard | for(i = 0; i < shnum; i++) { |
379 | 67b915a5 | bellard | sec = &shdr[i]; |
380 | 67b915a5 | bellard | if (!sec->sh_name)
|
381 | 67b915a5 | bellard | continue;
|
382 | 67b915a5 | bellard | shname = shstr + sec->sh_name; |
383 | 67b915a5 | bellard | if (!strcmp(shname, name))
|
384 | 67b915a5 | bellard | return sec;
|
385 | 67b915a5 | bellard | } |
386 | 67b915a5 | bellard | return NULL; |
387 | 67b915a5 | bellard | } |
388 | 67b915a5 | bellard | |
389 | 67b915a5 | bellard | int find_reloc(int sh_index) |
390 | 367e86e8 | bellard | { |
391 | 67b915a5 | bellard | struct elf_shdr *sec;
|
392 | 67b915a5 | bellard | int i;
|
393 | 67b915a5 | bellard | |
394 | 67b915a5 | bellard | for(i = 0; i < ehdr.e_shnum; i++) { |
395 | 67b915a5 | bellard | sec = &shdr[i]; |
396 | 67b915a5 | bellard | if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
|
397 | 67b915a5 | bellard | return i;
|
398 | 67b915a5 | bellard | } |
399 | 67b915a5 | bellard | return 0; |
400 | 367e86e8 | bellard | } |
401 | 367e86e8 | bellard | |
402 | 67b915a5 | bellard | static char *get_rel_sym_name(EXE_RELOC *rel) |
403 | 367e86e8 | bellard | { |
404 | 67b915a5 | bellard | return strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
|
405 | 67b915a5 | bellard | } |
406 | 67b915a5 | bellard | |
407 | 67b915a5 | bellard | static char *get_sym_name(EXE_SYM *sym) |
408 | 67b915a5 | bellard | { |
409 | 67b915a5 | bellard | return strtab + sym->st_name;
|
410 | 67b915a5 | bellard | } |
411 | 67b915a5 | bellard | |
412 | 67b915a5 | bellard | /* load an elf object file */
|
413 | 67b915a5 | bellard | int load_object(const char *filename) |
414 | 67b915a5 | bellard | { |
415 | 67b915a5 | bellard | int fd;
|
416 | 67b915a5 | bellard | struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
|
417 | 67b915a5 | bellard | int i, j;
|
418 | 67b915a5 | bellard | ElfW(Sym) *sym; |
419 | 67b915a5 | bellard | char *shstr;
|
420 | 67b915a5 | bellard | ELF_RELOC *rel; |
421 | 67b915a5 | bellard | |
422 | 67b915a5 | bellard | fd = open(filename, O_RDONLY); |
423 | 67b915a5 | bellard | if (fd < 0) |
424 | 67b915a5 | bellard | error("can't open file '%s'", filename);
|
425 | 67b915a5 | bellard | |
426 | 67b915a5 | bellard | /* Read ELF header. */
|
427 | 67b915a5 | bellard | if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr)) |
428 | 67b915a5 | bellard | error("unable to read file header");
|
429 | 67b915a5 | bellard | |
430 | 67b915a5 | bellard | /* Check ELF identification. */
|
431 | 67b915a5 | bellard | if (ehdr.e_ident[EI_MAG0] != ELFMAG0
|
432 | 67b915a5 | bellard | || ehdr.e_ident[EI_MAG1] != ELFMAG1 |
433 | 67b915a5 | bellard | || ehdr.e_ident[EI_MAG2] != ELFMAG2 |
434 | 67b915a5 | bellard | || ehdr.e_ident[EI_MAG3] != ELFMAG3 |
435 | 67b915a5 | bellard | || ehdr.e_ident[EI_VERSION] != EV_CURRENT) { |
436 | 67b915a5 | bellard | error("bad ELF header");
|
437 | 67b915a5 | bellard | } |
438 | 67b915a5 | bellard | |
439 | 67b915a5 | bellard | do_swap = elf_must_swap(&ehdr); |
440 | 367e86e8 | bellard | if (do_swap)
|
441 | 67b915a5 | bellard | elf_swap_ehdr(&ehdr); |
442 | 67b915a5 | bellard | if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
|
443 | 67b915a5 | bellard | error("Unsupported ELF class");
|
444 | 67b915a5 | bellard | if (ehdr.e_type != ET_REL)
|
445 | 67b915a5 | bellard | error("ELF object file expected");
|
446 | 67b915a5 | bellard | if (ehdr.e_version != EV_CURRENT)
|
447 | 67b915a5 | bellard | error("Invalid ELF version");
|
448 | 67b915a5 | bellard | if (!elf_check_arch(ehdr.e_machine))
|
449 | 67b915a5 | bellard | error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
|
450 | 67b915a5 | bellard | |
451 | 67b915a5 | bellard | /* read section headers */
|
452 | 67b915a5 | bellard | shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr)); |
453 | 67b915a5 | bellard | if (do_swap) {
|
454 | 67b915a5 | bellard | for(i = 0; i < ehdr.e_shnum; i++) { |
455 | 67b915a5 | bellard | elf_swap_shdr(&shdr[i]); |
456 | 67b915a5 | bellard | } |
457 | 67b915a5 | bellard | } |
458 | 67b915a5 | bellard | |
459 | 67b915a5 | bellard | /* read all section data */
|
460 | 67b915a5 | bellard | sdata = malloc(sizeof(void *) * ehdr.e_shnum); |
461 | 67b915a5 | bellard | memset(sdata, 0, sizeof(void *) * ehdr.e_shnum); |
462 | 67b915a5 | bellard | |
463 | 67b915a5 | bellard | for(i = 0;i < ehdr.e_shnum; i++) { |
464 | 67b915a5 | bellard | sec = &shdr[i]; |
465 | 67b915a5 | bellard | if (sec->sh_type != SHT_NOBITS)
|
466 | 67b915a5 | bellard | sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size); |
467 | 67b915a5 | bellard | } |
468 | 67b915a5 | bellard | |
469 | 67b915a5 | bellard | sec = &shdr[ehdr.e_shstrndx]; |
470 | 67b915a5 | bellard | shstr = sdata[ehdr.e_shstrndx]; |
471 | 67b915a5 | bellard | |
472 | 67b915a5 | bellard | /* swap relocations */
|
473 | 67b915a5 | bellard | for(i = 0; i < ehdr.e_shnum; i++) { |
474 | 67b915a5 | bellard | sec = &shdr[i]; |
475 | 67b915a5 | bellard | if (sec->sh_type == SHT_RELOC) {
|
476 | 67b915a5 | bellard | nb_relocs = sec->sh_size / sec->sh_entsize; |
477 | 67b915a5 | bellard | if (do_swap) {
|
478 | 67b915a5 | bellard | for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++) |
479 | 67b915a5 | bellard | elf_swap_rel(rel); |
480 | 67b915a5 | bellard | } |
481 | 67b915a5 | bellard | } |
482 | 67b915a5 | bellard | } |
483 | 67b915a5 | bellard | /* text section */
|
484 | 67b915a5 | bellard | |
485 | 67b915a5 | bellard | text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
|
486 | 67b915a5 | bellard | if (!text_sec)
|
487 | 67b915a5 | bellard | error("could not find .text section");
|
488 | 67b915a5 | bellard | text_shndx = text_sec - shdr; |
489 | 67b915a5 | bellard | text = sdata[text_shndx]; |
490 | 67b915a5 | bellard | |
491 | 67b915a5 | bellard | /* find text relocations, if any */
|
492 | 67b915a5 | bellard | relocs = NULL;
|
493 | 67b915a5 | bellard | nb_relocs = 0;
|
494 | 67b915a5 | bellard | i = find_reloc(text_shndx); |
495 | 67b915a5 | bellard | if (i != 0) { |
496 | 67b915a5 | bellard | relocs = (ELF_RELOC *)sdata[i]; |
497 | 67b915a5 | bellard | nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize; |
498 | 67b915a5 | bellard | } |
499 | 67b915a5 | bellard | |
500 | 67b915a5 | bellard | symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
|
501 | 67b915a5 | bellard | if (!symtab_sec)
|
502 | 67b915a5 | bellard | error("could not find .symtab section");
|
503 | 67b915a5 | bellard | strtab_sec = &shdr[symtab_sec->sh_link]; |
504 | 67b915a5 | bellard | |
505 | 67b915a5 | bellard | symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr]; |
506 | 67b915a5 | bellard | strtab = sdata[symtab_sec->sh_link]; |
507 | 67b915a5 | bellard | |
508 | 67b915a5 | bellard | nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
|
509 | 67b915a5 | bellard | if (do_swap) {
|
510 | 67b915a5 | bellard | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
511 | 67b915a5 | bellard | swab32s(&sym->st_name); |
512 | 67b915a5 | bellard | swabls(&sym->st_value); |
513 | 67b915a5 | bellard | swabls(&sym->st_size); |
514 | 67b915a5 | bellard | swab16s(&sym->st_shndx); |
515 | 67b915a5 | bellard | } |
516 | 67b915a5 | bellard | } |
517 | 67b915a5 | bellard | close(fd); |
518 | 67b915a5 | bellard | return 0; |
519 | 67b915a5 | bellard | } |
520 | 67b915a5 | bellard | |
521 | 67b915a5 | bellard | #endif /* CONFIG_FORMAT_ELF */ |
522 | 67b915a5 | bellard | |
523 | 67b915a5 | bellard | #ifdef CONFIG_FORMAT_COFF
|
524 | 67b915a5 | bellard | |
525 | 67b915a5 | bellard | /* COFF file info */
|
526 | 67b915a5 | bellard | struct external_scnhdr *shdr;
|
527 | 67b915a5 | bellard | uint8_t **sdata; |
528 | 67b915a5 | bellard | struct external_filehdr fhdr;
|
529 | 67b915a5 | bellard | struct external_syment *coff_symtab;
|
530 | 67b915a5 | bellard | char *strtab;
|
531 | 67b915a5 | bellard | int coff_text_shndx, coff_data_shndx;
|
532 | 67b915a5 | bellard | |
533 | 67b915a5 | bellard | int data_shndx;
|
534 | 67b915a5 | bellard | |
535 | 67b915a5 | bellard | #define STRTAB_SIZE 4 |
536 | 67b915a5 | bellard | |
537 | 67b915a5 | bellard | #define DIR32 0x06 |
538 | 67b915a5 | bellard | #define DISP32 0x14 |
539 | 67b915a5 | bellard | |
540 | 67b915a5 | bellard | #define T_FUNCTION 0x20 |
541 | 67b915a5 | bellard | #define C_EXTERNAL 2 |
542 | 67b915a5 | bellard | |
543 | 67b915a5 | bellard | void sym_ent_name(struct external_syment *ext_sym, EXE_SYM *sym) |
544 | 67b915a5 | bellard | { |
545 | 67b915a5 | bellard | char *q;
|
546 | 67b915a5 | bellard | int c, i, len;
|
547 | 67b915a5 | bellard | |
548 | 67b915a5 | bellard | if (ext_sym->e.e.e_zeroes != 0) { |
549 | 67b915a5 | bellard | q = sym->st_name; |
550 | 67b915a5 | bellard | for(i = 0; i < 8; i++) { |
551 | 67b915a5 | bellard | c = ext_sym->e.e_name[i]; |
552 | 67b915a5 | bellard | if (c == '\0') |
553 | 67b915a5 | bellard | break;
|
554 | 67b915a5 | bellard | *q++ = c; |
555 | 67b915a5 | bellard | } |
556 | 67b915a5 | bellard | *q = '\0';
|
557 | 67b915a5 | bellard | } else {
|
558 | 67b915a5 | bellard | pstrcpy(sym->st_name, sizeof(sym->st_name), strtab + ext_sym->e.e.e_offset);
|
559 | 67b915a5 | bellard | } |
560 | 67b915a5 | bellard | |
561 | 67b915a5 | bellard | /* now convert the name to a C name (suppress the leading '_') */
|
562 | 67b915a5 | bellard | if (sym->st_name[0] == '_') { |
563 | 67b915a5 | bellard | len = strlen(sym->st_name); |
564 | 67b915a5 | bellard | memmove(sym->st_name, sym->st_name + 1, len - 1); |
565 | 67b915a5 | bellard | sym->st_name[len - 1] = '\0'; |
566 | 67b915a5 | bellard | } |
567 | 367e86e8 | bellard | } |
568 | 367e86e8 | bellard | |
569 | 67b915a5 | bellard | char *name_for_dotdata(struct coff_rel *rel) |
570 | 367e86e8 | bellard | { |
571 | 67b915a5 | bellard | int i;
|
572 | 67b915a5 | bellard | struct coff_sym *sym;
|
573 | 67b915a5 | bellard | uint32_t text_data; |
574 | 67b915a5 | bellard | |
575 | 67b915a5 | bellard | text_data = *(uint32_t *)(text + rel->r_offset); |
576 | 67b915a5 | bellard | |
577 | 67b915a5 | bellard | for (i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
578 | 67b915a5 | bellard | if (sym->st_syment->e_scnum == data_shndx &&
|
579 | 67b915a5 | bellard | text_data >= sym->st_value && |
580 | 67b915a5 | bellard | text_data < sym->st_value + sym->st_size) { |
581 | 67b915a5 | bellard | |
582 | 67b915a5 | bellard | return sym->st_name;
|
583 | 67b915a5 | bellard | |
584 | 67b915a5 | bellard | } |
585 | 67b915a5 | bellard | } |
586 | 67b915a5 | bellard | return NULL; |
587 | 367e86e8 | bellard | } |
588 | 367e86e8 | bellard | |
589 | 67b915a5 | bellard | static char *get_sym_name(EXE_SYM *sym) |
590 | 367e86e8 | bellard | { |
591 | 67b915a5 | bellard | return sym->st_name;
|
592 | 367e86e8 | bellard | } |
593 | 367e86e8 | bellard | |
594 | 67b915a5 | bellard | static char *get_rel_sym_name(EXE_RELOC *rel) |
595 | 367e86e8 | bellard | { |
596 | 67b915a5 | bellard | char *name;
|
597 | 67b915a5 | bellard | name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx)); |
598 | 67b915a5 | bellard | if (!strcmp(name, ".data")) |
599 | 67b915a5 | bellard | name = name_for_dotdata(rel); |
600 | 67b915a5 | bellard | return name;
|
601 | 367e86e8 | bellard | } |
602 | 367e86e8 | bellard | |
603 | 67b915a5 | bellard | struct external_scnhdr *find_coff_section(struct external_scnhdr *shdr, int shnum, const char *name) |
604 | 367e86e8 | bellard | { |
605 | 367e86e8 | bellard | int i;
|
606 | 367e86e8 | bellard | const char *shname; |
607 | 67b915a5 | bellard | struct external_scnhdr *sec;
|
608 | 367e86e8 | bellard | |
609 | 367e86e8 | bellard | for(i = 0; i < shnum; i++) { |
610 | 367e86e8 | bellard | sec = &shdr[i]; |
611 | 67b915a5 | bellard | if (!sec->s_name)
|
612 | 367e86e8 | bellard | continue;
|
613 | 67b915a5 | bellard | shname = sec->s_name; |
614 | 367e86e8 | bellard | if (!strcmp(shname, name))
|
615 | 367e86e8 | bellard | return sec;
|
616 | 367e86e8 | bellard | } |
617 | 367e86e8 | bellard | return NULL; |
618 | 367e86e8 | bellard | } |
619 | 367e86e8 | bellard | |
620 | 67b915a5 | bellard | /* load a coff object file */
|
621 | 67b915a5 | bellard | int load_object(const char *filename) |
622 | fe319756 | bellard | { |
623 | 67b915a5 | bellard | int fd;
|
624 | 67b915a5 | bellard | struct external_scnhdr *sec, *text_sec, *data_sec;
|
625 | fe319756 | bellard | int i;
|
626 | 67b915a5 | bellard | struct external_syment *ext_sym;
|
627 | 67b915a5 | bellard | struct external_reloc *coff_relocs;
|
628 | 67b915a5 | bellard | struct external_reloc *ext_rel;
|
629 | 67b915a5 | bellard | uint32_t *n_strtab; |
630 | 67b915a5 | bellard | EXE_SYM *sym; |
631 | 67b915a5 | bellard | EXE_RELOC *rel; |
632 | 67b915a5 | bellard | |
633 | 67b915a5 | bellard | fd = open(filename, O_RDONLY |
634 | 67b915a5 | bellard | #ifdef _WIN32
|
635 | 67b915a5 | bellard | | O_BINARY |
636 | 67b915a5 | bellard | #endif
|
637 | 67b915a5 | bellard | ); |
638 | 67b915a5 | bellard | if (fd < 0) |
639 | 67b915a5 | bellard | error("can't open file '%s'", filename);
|
640 | 67b915a5 | bellard | |
641 | 67b915a5 | bellard | /* Read COFF header. */
|
642 | 67b915a5 | bellard | if (read(fd, &fhdr, sizeof (fhdr)) != sizeof (fhdr)) |
643 | 67b915a5 | bellard | error("unable to read file header");
|
644 | fe319756 | bellard | |
645 | 67b915a5 | bellard | /* Check COFF identification. */
|
646 | 67b915a5 | bellard | if (fhdr.f_magic != I386MAGIC) {
|
647 | 67b915a5 | bellard | error("bad COFF header");
|
648 | 67b915a5 | bellard | } |
649 | 67b915a5 | bellard | do_swap = 0;
|
650 | 67b915a5 | bellard | |
651 | 67b915a5 | bellard | /* read section headers */
|
652 | 67b915a5 | bellard | shdr = load_data(fd, sizeof(struct external_filehdr) + fhdr.f_opthdr, fhdr.f_nscns * sizeof(struct external_scnhdr)); |
653 | 67b915a5 | bellard | |
654 | 67b915a5 | bellard | /* read all section data */
|
655 | 67b915a5 | bellard | sdata = malloc(sizeof(void *) * fhdr.f_nscns); |
656 | 67b915a5 | bellard | memset(sdata, 0, sizeof(void *) * fhdr.f_nscns); |
657 | 67b915a5 | bellard | |
658 | 67b915a5 | bellard | const char *p; |
659 | 67b915a5 | bellard | for(i = 0;i < fhdr.f_nscns; i++) { |
660 | fe319756 | bellard | sec = &shdr[i]; |
661 | 67b915a5 | bellard | if (!strstart(sec->s_name, ".bss", &p)) |
662 | 67b915a5 | bellard | sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size); |
663 | fe319756 | bellard | } |
664 | fe319756 | bellard | |
665 | 367e86e8 | bellard | |
666 | 67b915a5 | bellard | /* text section */
|
667 | 67b915a5 | bellard | text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text");
|
668 | 67b915a5 | bellard | if (!text_sec)
|
669 | 67b915a5 | bellard | error("could not find .text section");
|
670 | 67b915a5 | bellard | coff_text_shndx = text_sec - shdr; |
671 | 67b915a5 | bellard | text = sdata[coff_text_shndx]; |
672 | 67b915a5 | bellard | |
673 | 67b915a5 | bellard | /* data section */
|
674 | 67b915a5 | bellard | data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data");
|
675 | 67b915a5 | bellard | if (!data_sec)
|
676 | 67b915a5 | bellard | error("could not find .data section");
|
677 | 67b915a5 | bellard | coff_data_shndx = data_sec - shdr; |
678 | 67b915a5 | bellard | |
679 | 67b915a5 | bellard | coff_symtab = load_data(fd, fhdr.f_symptr, fhdr.f_nsyms*SYMESZ); |
680 | 67b915a5 | bellard | for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) { |
681 | 67b915a5 | bellard | for(i=0;i<8;i++) |
682 | 67b915a5 | bellard | printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]);
|
683 | 67b915a5 | bellard | printf("\n");
|
684 | 367e86e8 | bellard | } |
685 | 367e86e8 | bellard | |
686 | 67b915a5 | bellard | |
687 | 67b915a5 | bellard | n_strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), STRTAB_SIZE); |
688 | 67b915a5 | bellard | strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), *n_strtab); |
689 | 67b915a5 | bellard | |
690 | 67b915a5 | bellard | nb_syms = fhdr.f_nsyms; |
691 | 67b915a5 | bellard | |
692 | 67b915a5 | bellard | for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) { |
693 | 67b915a5 | bellard | if (strstart(ext_sym->e.e_name, ".text", NULL)) |
694 | 67b915a5 | bellard | text_shndx = ext_sym->e_scnum; |
695 | 67b915a5 | bellard | if (strstart(ext_sym->e.e_name, ".data", NULL)) |
696 | 67b915a5 | bellard | data_shndx = ext_sym->e_scnum; |
697 | 367e86e8 | bellard | } |
698 | 67b915a5 | bellard | |
699 | 67b915a5 | bellard | /* set coff symbol */
|
700 | 67b915a5 | bellard | symtab = malloc(sizeof(struct coff_sym) * nb_syms); |
701 | 67b915a5 | bellard | |
702 | 67b915a5 | bellard | int aux_size, j;
|
703 | 67b915a5 | bellard | for (i = 0, ext_sym = coff_symtab, sym = symtab; i < nb_syms; i++, ext_sym++, sym++) { |
704 | 67b915a5 | bellard | memset(sym, 0, sizeof(*sym)); |
705 | 67b915a5 | bellard | sym->st_syment = ext_sym; |
706 | 67b915a5 | bellard | sym_ent_name(ext_sym, sym); |
707 | 67b915a5 | bellard | sym->st_value = ext_sym->e_value; |
708 | 67b915a5 | bellard | |
709 | 67b915a5 | bellard | aux_size = *(int8_t *)ext_sym->e_numaux; |
710 | 67b915a5 | bellard | if (ext_sym->e_scnum == text_shndx && ext_sym->e_type == T_FUNCTION) {
|
711 | 67b915a5 | bellard | for (j = aux_size + 1; j < nb_syms - i; j++) { |
712 | 67b915a5 | bellard | if ((ext_sym + j)->e_scnum == text_shndx &&
|
713 | 67b915a5 | bellard | (ext_sym + j)->e_type == T_FUNCTION ){ |
714 | 67b915a5 | bellard | sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value; |
715 | 67b915a5 | bellard | break;
|
716 | 67b915a5 | bellard | } else if (j == nb_syms - i - 1) { |
717 | 67b915a5 | bellard | sec = &shdr[coff_text_shndx]; |
718 | 67b915a5 | bellard | sym->st_size = sec->s_size - ext_sym->e_value; |
719 | 67b915a5 | bellard | break;
|
720 | 67b915a5 | bellard | } |
721 | 67b915a5 | bellard | } |
722 | 67b915a5 | bellard | } else if (ext_sym->e_scnum == data_shndx && *(uint8_t *)ext_sym->e_sclass == C_EXTERNAL) { |
723 | 67b915a5 | bellard | for (j = aux_size + 1; j < nb_syms - i; j++) { |
724 | 67b915a5 | bellard | if ((ext_sym + j)->e_scnum == data_shndx) {
|
725 | 67b915a5 | bellard | sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value; |
726 | 67b915a5 | bellard | break;
|
727 | 67b915a5 | bellard | } else if (j == nb_syms - i - 1) { |
728 | 67b915a5 | bellard | sec = &shdr[coff_data_shndx]; |
729 | 67b915a5 | bellard | sym->st_size = sec->s_size - ext_sym->e_value; |
730 | 67b915a5 | bellard | break;
|
731 | 67b915a5 | bellard | } |
732 | 67b915a5 | bellard | } |
733 | 67b915a5 | bellard | } else {
|
734 | 67b915a5 | bellard | sym->st_size = 0;
|
735 | 67b915a5 | bellard | } |
736 | 67b915a5 | bellard | |
737 | 67b915a5 | bellard | sym->st_type = ext_sym->e_type; |
738 | 67b915a5 | bellard | sym->st_shndx = ext_sym->e_scnum; |
739 | 67b915a5 | bellard | } |
740 | 67b915a5 | bellard | |
741 | 67b915a5 | bellard | |
742 | 67b915a5 | bellard | /* find text relocations, if any */
|
743 | 67b915a5 | bellard | sec = &shdr[coff_text_shndx]; |
744 | 67b915a5 | bellard | coff_relocs = load_data(fd, sec->s_relptr, sec->s_nreloc*RELSZ); |
745 | 67b915a5 | bellard | nb_relocs = sec->s_nreloc; |
746 | 67b915a5 | bellard | |
747 | 67b915a5 | bellard | /* set coff relocation */
|
748 | 67b915a5 | bellard | relocs = malloc(sizeof(struct coff_rel) * nb_relocs); |
749 | 67b915a5 | bellard | for (i = 0, ext_rel = coff_relocs, rel = relocs; i < nb_relocs; |
750 | 67b915a5 | bellard | i++, ext_rel++, rel++) { |
751 | 67b915a5 | bellard | memset(rel, 0, sizeof(*rel)); |
752 | 67b915a5 | bellard | rel->r_reloc = ext_rel; |
753 | 67b915a5 | bellard | rel->r_offset = *(uint32_t *)ext_rel->r_vaddr; |
754 | 67b915a5 | bellard | rel->r_type = *(uint16_t *)ext_rel->r_type; |
755 | 67b915a5 | bellard | } |
756 | 67b915a5 | bellard | return 0; |
757 | 367e86e8 | bellard | } |
758 | 367e86e8 | bellard | |
759 | 67b915a5 | bellard | #endif /* CONFIG_FORMAT_COFF */ |
760 | 67b915a5 | bellard | |
761 | ff1f20a3 | bellard | #ifdef HOST_ARM
|
762 | ff1f20a3 | bellard | |
763 | ff1f20a3 | bellard | int arm_emit_ldr_info(const char *name, unsigned long start_offset, |
764 | ff1f20a3 | bellard | FILE *outfile, uint8_t *p_start, uint8_t *p_end, |
765 | ff1f20a3 | bellard | ELF_RELOC *relocs, int nb_relocs)
|
766 | ff1f20a3 | bellard | { |
767 | ff1f20a3 | bellard | uint8_t *p; |
768 | ff1f20a3 | bellard | uint32_t insn; |
769 | ff1f20a3 | bellard | int offset, min_offset, pc_offset, data_size;
|
770 | ff1f20a3 | bellard | uint8_t data_allocated[1024];
|
771 | ff1f20a3 | bellard | unsigned int data_index; |
772 | ff1f20a3 | bellard | |
773 | ff1f20a3 | bellard | memset(data_allocated, 0, sizeof(data_allocated)); |
774 | ff1f20a3 | bellard | |
775 | ff1f20a3 | bellard | p = p_start; |
776 | ff1f20a3 | bellard | min_offset = p_end - p_start; |
777 | ff1f20a3 | bellard | while (p < p_start + min_offset) {
|
778 | ff1f20a3 | bellard | insn = get32((uint32_t *)p); |
779 | ff1f20a3 | bellard | if ((insn & 0x0d5f0000) == 0x051f0000) { |
780 | ff1f20a3 | bellard | /* ldr reg, [pc, #im] */
|
781 | ff1f20a3 | bellard | offset = insn & 0xfff;
|
782 | ff1f20a3 | bellard | if (!(insn & 0x00800000)) |
783 | ff1f20a3 | bellard | offset = -offset; |
784 | ff1f20a3 | bellard | if ((offset & 3) !=0) |
785 | ff1f20a3 | bellard | error("%s:%04x: ldr pc offset must be 32 bit aligned",
|
786 | ff1f20a3 | bellard | name, start_offset + p - p_start); |
787 | ff1f20a3 | bellard | pc_offset = p - p_start + offset + 8;
|
788 | ff1f20a3 | bellard | if (pc_offset <= (p - p_start) ||
|
789 | ff1f20a3 | bellard | pc_offset >= (p_end - p_start)) |
790 | ff1f20a3 | bellard | error("%s:%04x: ldr pc offset must point inside the function code",
|
791 | ff1f20a3 | bellard | name, start_offset + p - p_start); |
792 | ff1f20a3 | bellard | if (pc_offset < min_offset)
|
793 | ff1f20a3 | bellard | min_offset = pc_offset; |
794 | ff1f20a3 | bellard | if (outfile) {
|
795 | ff1f20a3 | bellard | /* ldr position */
|
796 | ff1f20a3 | bellard | fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
|
797 | ff1f20a3 | bellard | p - p_start); |
798 | ff1f20a3 | bellard | /* ldr data index */
|
799 | ff1f20a3 | bellard | data_index = ((p_end - p_start) - pc_offset - 4) >> 2; |
800 | ff1f20a3 | bellard | fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
|
801 | ff1f20a3 | bellard | data_index); |
802 | ff1f20a3 | bellard | fprintf(outfile, " arm_ldr_ptr++;\n");
|
803 | ff1f20a3 | bellard | if (data_index >= sizeof(data_allocated)) |
804 | ff1f20a3 | bellard | error("%s: too many data", name);
|
805 | ff1f20a3 | bellard | if (!data_allocated[data_index]) {
|
806 | ff1f20a3 | bellard | ELF_RELOC *rel; |
807 | ff1f20a3 | bellard | int i, addend, type;
|
808 | ff1f20a3 | bellard | const char *sym_name, *p; |
809 | ff1f20a3 | bellard | char relname[1024]; |
810 | ff1f20a3 | bellard | |
811 | ff1f20a3 | bellard | data_allocated[data_index] = 1;
|
812 | ff1f20a3 | bellard | |
813 | ff1f20a3 | bellard | /* data value */
|
814 | ff1f20a3 | bellard | addend = get32((uint32_t *)(p_start + pc_offset)); |
815 | ff1f20a3 | bellard | relname[0] = '\0'; |
816 | ff1f20a3 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
817 | ff1f20a3 | bellard | if (rel->r_offset == (pc_offset + start_offset)) {
|
818 | 67b915a5 | bellard | sym_name = get_rel_sym_name(rel); |
819 | ff1f20a3 | bellard | /* the compiler leave some unnecessary references to the code */
|
820 | ff1f20a3 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
821 | ff1f20a3 | bellard | snprintf(relname, sizeof(relname), "param%s", p); |
822 | ff1f20a3 | bellard | } else {
|
823 | ff1f20a3 | bellard | snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name); |
824 | ff1f20a3 | bellard | } |
825 | ff1f20a3 | bellard | type = ELF32_R_TYPE(rel->r_info); |
826 | ff1f20a3 | bellard | if (type != R_ARM_ABS32)
|
827 | ff1f20a3 | bellard | error("%s: unsupported data relocation", name);
|
828 | ff1f20a3 | bellard | break;
|
829 | ff1f20a3 | bellard | } |
830 | ff1f20a3 | bellard | } |
831 | ff1f20a3 | bellard | fprintf(outfile, " arm_data_ptr[%d] = 0x%x",
|
832 | ff1f20a3 | bellard | data_index, addend); |
833 | ff1f20a3 | bellard | if (relname[0] != '\0') |
834 | ff1f20a3 | bellard | fprintf(outfile, " + %s", relname);
|
835 | ff1f20a3 | bellard | fprintf(outfile, ";\n");
|
836 | ff1f20a3 | bellard | } |
837 | ff1f20a3 | bellard | } |
838 | ff1f20a3 | bellard | } |
839 | ff1f20a3 | bellard | p += 4;
|
840 | ff1f20a3 | bellard | } |
841 | ff1f20a3 | bellard | data_size = (p_end - p_start) - min_offset; |
842 | ff1f20a3 | bellard | if (data_size > 0 && outfile) { |
843 | ff1f20a3 | bellard | fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2); |
844 | ff1f20a3 | bellard | } |
845 | ff1f20a3 | bellard | |
846 | ff1f20a3 | bellard | /* the last instruction must be a mov pc, lr */
|
847 | ff1f20a3 | bellard | if (p == p_start)
|
848 | ff1f20a3 | bellard | goto arm_ret_error;
|
849 | ff1f20a3 | bellard | p -= 4;
|
850 | ff1f20a3 | bellard | insn = get32((uint32_t *)p); |
851 | ff1f20a3 | bellard | if ((insn & 0xffff0000) != 0xe91b0000) { |
852 | ff1f20a3 | bellard | arm_ret_error:
|
853 | ff1f20a3 | bellard | if (!outfile)
|
854 | ff1f20a3 | bellard | printf("%s: invalid epilog\n", name);
|
855 | ff1f20a3 | bellard | } |
856 | ff1f20a3 | bellard | return p - p_start;
|
857 | ff1f20a3 | bellard | } |
858 | ff1f20a3 | bellard | #endif
|
859 | ff1f20a3 | bellard | |
860 | ff1f20a3 | bellard | |
861 | 367e86e8 | bellard | #define MAX_ARGS 3 |
862 | 367e86e8 | bellard | |
863 | 367e86e8 | bellard | /* generate op code */
|
864 | ce11fedc | bellard | void gen_code(const char *name, host_ulong offset, host_ulong size, |
865 | 67b915a5 | bellard | FILE *outfile, int gen_switch)
|
866 | 367e86e8 | bellard | { |
867 | 367e86e8 | bellard | int copy_size = 0; |
868 | 367e86e8 | bellard | uint8_t *p_start, *p_end; |
869 | ae228531 | bellard | host_ulong start_offset; |
870 | ce11fedc | bellard | int nb_args, i, n;
|
871 | 367e86e8 | bellard | uint8_t args_present[MAX_ARGS]; |
872 | 367e86e8 | bellard | const char *sym_name, *p; |
873 | 67b915a5 | bellard | EXE_RELOC *rel; |
874 | 367e86e8 | bellard | |
875 | ae228531 | bellard | /* Compute exact size excluding prologue and epilogue instructions.
|
876 | ae228531 | bellard | * Increment start_offset to skip epilogue instructions, then compute
|
877 | ae228531 | bellard | * copy_size the indicate the size of the remaining instructions (in
|
878 | ae228531 | bellard | * bytes).
|
879 | ae228531 | bellard | */
|
880 | 367e86e8 | bellard | p_start = text + offset; |
881 | 367e86e8 | bellard | p_end = p_start + size; |
882 | ae228531 | bellard | start_offset = offset; |
883 | 67b915a5 | bellard | #if defined(HOST_I386) || defined(HOST_AMD64)
|
884 | 67b915a5 | bellard | #ifdef CONFIG_FORMAT_COFF
|
885 | 67b915a5 | bellard | { |
886 | 67b915a5 | bellard | uint8_t *p; |
887 | 67b915a5 | bellard | p = p_end - 1;
|
888 | 67b915a5 | bellard | if (p == p_start)
|
889 | 67b915a5 | bellard | error("empty code for %s", name);
|
890 | 67b915a5 | bellard | while (*p != 0xc3) { |
891 | 67b915a5 | bellard | p--; |
892 | 67b915a5 | bellard | if (p <= p_start)
|
893 | d4e8164f | bellard | error("ret or jmp expected at the end of %s", name);
|
894 | 367e86e8 | bellard | } |
895 | 67b915a5 | bellard | copy_size = p - p_start; |
896 | 67b915a5 | bellard | } |
897 | 67b915a5 | bellard | #else
|
898 | 67b915a5 | bellard | { |
899 | 67b915a5 | bellard | int len;
|
900 | 67b915a5 | bellard | len = p_end - p_start; |
901 | 67b915a5 | bellard | if (len == 0) |
902 | 67b915a5 | bellard | error("empty code for %s", name);
|
903 | 67b915a5 | bellard | if (p_end[-1] == 0xc3) { |
904 | 67b915a5 | bellard | len--; |
905 | 67b915a5 | bellard | } else {
|
906 | 67b915a5 | bellard | error("ret or jmp expected at the end of %s", name);
|
907 | 367e86e8 | bellard | } |
908 | 67b915a5 | bellard | copy_size = len; |
909 | 67b915a5 | bellard | } |
910 | 67b915a5 | bellard | #endif
|
911 | 67b915a5 | bellard | #elif defined(HOST_PPC)
|
912 | 67b915a5 | bellard | { |
913 | 67b915a5 | bellard | uint8_t *p; |
914 | 67b915a5 | bellard | p = (void *)(p_end - 4); |
915 | 67b915a5 | bellard | if (p == p_start)
|
916 | 67b915a5 | bellard | error("empty code for %s", name);
|
917 | 67b915a5 | bellard | if (get32((uint32_t *)p) != 0x4e800020) |
918 | 67b915a5 | bellard | error("blr expected at the end of %s", name);
|
919 | 67b915a5 | bellard | copy_size = p - p_start; |
920 | 67b915a5 | bellard | } |
921 | 67b915a5 | bellard | #elif defined(HOST_S390)
|
922 | 67b915a5 | bellard | { |
923 | 67b915a5 | bellard | uint8_t *p; |
924 | 67b915a5 | bellard | p = (void *)(p_end - 2); |
925 | 67b915a5 | bellard | if (p == p_start)
|
926 | 67b915a5 | bellard | error("empty code for %s", name);
|
927 | 67b915a5 | bellard | if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4) |
928 | 67b915a5 | bellard | error("br %%r14 expected at the end of %s", name);
|
929 | 67b915a5 | bellard | copy_size = p - p_start; |
930 | 67b915a5 | bellard | } |
931 | 67b915a5 | bellard | #elif defined(HOST_ALPHA)
|
932 | 67b915a5 | bellard | { |
933 | 67b915a5 | bellard | uint8_t *p; |
934 | 67b915a5 | bellard | p = p_end - 4;
|
935 | 630be16f | bellard | #if 0
|
936 | 67b915a5 | bellard | /* XXX: check why it occurs */
|
937 | 67b915a5 | bellard | if (p == p_start)
|
938 | 67b915a5 | bellard | error("empty code for %s", name);
|
939 | 630be16f | bellard | #endif
|
940 | 67b915a5 | bellard | if (get32((uint32_t *)p) != 0x6bfa8001) |
941 | 67b915a5 | bellard | error("ret expected at the end of %s", name);
|
942 | 67b915a5 | bellard | copy_size = p - p_start; |
943 | 67b915a5 | bellard | } |
944 | 67b915a5 | bellard | #elif defined(HOST_IA64)
|
945 | 67b915a5 | bellard | { |
946 | 67b915a5 | bellard | uint8_t *p; |
947 | 67b915a5 | bellard | p = (void *)(p_end - 4); |
948 | 67b915a5 | bellard | if (p == p_start)
|
949 | 67b915a5 | bellard | error("empty code for %s", name);
|
950 | 67b915a5 | bellard | /* br.ret.sptk.many b0;; */
|
951 | 67b915a5 | bellard | /* 08 00 84 00 */
|
952 | 67b915a5 | bellard | if (get32((uint32_t *)p) != 0x00840008) |
953 | 67b915a5 | bellard | error("br.ret.sptk.many b0;; expected at the end of %s", name);
|
954 | 67b915a5 | bellard | copy_size = p - p_start; |
955 | 67b915a5 | bellard | } |
956 | 67b915a5 | bellard | #elif defined(HOST_SPARC)
|
957 | 67b915a5 | bellard | { |
958 | 67b915a5 | bellard | uint32_t start_insn, end_insn1, end_insn2; |
959 | 67b915a5 | bellard | uint8_t *p; |
960 | 67b915a5 | bellard | p = (void *)(p_end - 8); |
961 | 67b915a5 | bellard | if (p <= p_start)
|
962 | 67b915a5 | bellard | error("empty code for %s", name);
|
963 | 67b915a5 | bellard | start_insn = get32((uint32_t *)(p_start + 0x0));
|
964 | 67b915a5 | bellard | end_insn1 = get32((uint32_t *)(p + 0x0));
|
965 | 67b915a5 | bellard | end_insn2 = get32((uint32_t *)(p + 0x4));
|
966 | 67b915a5 | bellard | if ((start_insn & ~0x1fff) == 0x9de3a000) { |
967 | 67b915a5 | bellard | p_start += 0x4;
|
968 | 67b915a5 | bellard | start_offset += 0x4;
|
969 | 67b915a5 | bellard | if ((int)(start_insn | ~0x1fff) < -128) |
970 | 67b915a5 | bellard | error("Found bogus save at the start of %s", name);
|
971 | 67b915a5 | bellard | if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000) |
972 | 67b915a5 | bellard | error("ret; restore; not found at end of %s", name);
|
973 | 67b915a5 | bellard | } else {
|
974 | 67b915a5 | bellard | error("No save at the beginning of %s", name);
|
975 | 67b915a5 | bellard | } |
976 | ff1f20a3 | bellard | #if 0
|
977 | 67b915a5 | bellard | /* Skip a preceeding nop, if present. */
|
978 | 67b915a5 | bellard | if (p > p_start) {
|
979 | 67b915a5 | bellard | skip_insn = get32((uint32_t *)(p - 0x4));
|
980 | 67b915a5 | bellard | if (skip_insn == 0x01000000)
|
981 | 67b915a5 | bellard | p -= 4;
|
982 | 67b915a5 | bellard | }
|
983 | ff1f20a3 | bellard | #endif
|
984 | 67b915a5 | bellard | copy_size = p - p_start; |
985 | 67b915a5 | bellard | } |
986 | 67b915a5 | bellard | #elif defined(HOST_SPARC64)
|
987 | 67b915a5 | bellard | { |
988 | 67b915a5 | bellard | uint32_t start_insn, end_insn1, end_insn2, skip_insn; |
989 | 67b915a5 | bellard | uint8_t *p; |
990 | 67b915a5 | bellard | p = (void *)(p_end - 8); |
991 | 67b915a5 | bellard | if (p <= p_start)
|
992 | 67b915a5 | bellard | error("empty code for %s", name);
|
993 | 67b915a5 | bellard | start_insn = get32((uint32_t *)(p_start + 0x0));
|
994 | 67b915a5 | bellard | end_insn1 = get32((uint32_t *)(p + 0x0));
|
995 | 67b915a5 | bellard | end_insn2 = get32((uint32_t *)(p + 0x4));
|
996 | 67b915a5 | bellard | if ((start_insn & ~0x1fff) == 0x9de3a000) { |
997 | 67b915a5 | bellard | p_start += 0x4;
|
998 | 67b915a5 | bellard | start_offset += 0x4;
|
999 | 67b915a5 | bellard | if ((int)(start_insn | ~0x1fff) < -256) |
1000 | 67b915a5 | bellard | error("Found bogus save at the start of %s", name);
|
1001 | 67b915a5 | bellard | if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000) |
1002 | 67b915a5 | bellard | error("ret; restore; not found at end of %s", name);
|
1003 | 67b915a5 | bellard | } else {
|
1004 | 67b915a5 | bellard | error("No save at the beginning of %s", name);
|
1005 | 67b915a5 | bellard | } |
1006 | 67b915a5 | bellard | |
1007 | 67b915a5 | bellard | /* Skip a preceeding nop, if present. */
|
1008 | 67b915a5 | bellard | if (p > p_start) {
|
1009 | 67b915a5 | bellard | skip_insn = get32((uint32_t *)(p - 0x4));
|
1010 | 67b915a5 | bellard | if (skip_insn == 0x01000000) |
1011 | 67b915a5 | bellard | p -= 4;
|
1012 | 67b915a5 | bellard | } |
1013 | 67b915a5 | bellard | |
1014 | 67b915a5 | bellard | copy_size = p - p_start; |
1015 | 67b915a5 | bellard | } |
1016 | 67b915a5 | bellard | #elif defined(HOST_ARM)
|
1017 | 67b915a5 | bellard | { |
1018 | ff1f20a3 | bellard | if ((p_end - p_start) <= 16) |
1019 | ff1f20a3 | bellard | error("%s: function too small", name);
|
1020 | ff1f20a3 | bellard | if (get32((uint32_t *)p_start) != 0xe1a0c00d || |
1021 | ff1f20a3 | bellard | (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 || |
1022 | ff1f20a3 | bellard | get32((uint32_t *)(p_start + 8)) != 0xe24cb004) |
1023 | ff1f20a3 | bellard | error("%s: invalid prolog", name);
|
1024 | ff1f20a3 | bellard | p_start += 12;
|
1025 | ff1f20a3 | bellard | start_offset += 12;
|
1026 | ff1f20a3 | bellard | copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end,
|
1027 | ff1f20a3 | bellard | relocs, nb_relocs); |
1028 | 367e86e8 | bellard | } |
1029 | 67b915a5 | bellard | #elif defined(HOST_M68K)
|
1030 | 67b915a5 | bellard | { |
1031 | 67b915a5 | bellard | uint8_t *p; |
1032 | 67b915a5 | bellard | p = (void *)(p_end - 2); |
1033 | 67b915a5 | bellard | if (p == p_start)
|
1034 | 67b915a5 | bellard | error("empty code for %s", name);
|
1035 | 67b915a5 | bellard | // remove NOP's, probably added for alignment
|
1036 | 67b915a5 | bellard | while ((get16((uint16_t *)p) == 0x4e71) && |
1037 | 67b915a5 | bellard | (p>p_start)) |
1038 | 67b915a5 | bellard | p -= 2;
|
1039 | 67b915a5 | bellard | if (get16((uint16_t *)p) != 0x4e75) |
1040 | 67b915a5 | bellard | error("rts expected at the end of %s", name);
|
1041 | 67b915a5 | bellard | copy_size = p - p_start; |
1042 | 67b915a5 | bellard | } |
1043 | 67b915a5 | bellard | #else
|
1044 | 67b915a5 | bellard | #error unsupported CPU
|
1045 | 67b915a5 | bellard | #endif
|
1046 | 367e86e8 | bellard | |
1047 | 367e86e8 | bellard | /* compute the number of arguments by looking at the relocations */
|
1048 | 367e86e8 | bellard | for(i = 0;i < MAX_ARGS; i++) |
1049 | 367e86e8 | bellard | args_present[i] = 0;
|
1050 | 367e86e8 | bellard | |
1051 | ce11fedc | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1052 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1053 | ff1f20a3 | bellard | rel->r_offset < start_offset + (p_end - p_start)) { |
1054 | 67b915a5 | bellard | sym_name = get_rel_sym_name(rel); |
1055 | ce11fedc | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1056 | ce11fedc | bellard | n = strtoul(p, NULL, 10); |
1057 | d4e8164f | bellard | if (n > MAX_ARGS)
|
1058 | ce11fedc | bellard | error("too many arguments in %s", name);
|
1059 | ce11fedc | bellard | args_present[n - 1] = 1; |
1060 | 367e86e8 | bellard | } |
1061 | 367e86e8 | bellard | } |
1062 | 367e86e8 | bellard | } |
1063 | 367e86e8 | bellard | |
1064 | 367e86e8 | bellard | nb_args = 0;
|
1065 | 367e86e8 | bellard | while (nb_args < MAX_ARGS && args_present[nb_args])
|
1066 | 367e86e8 | bellard | nb_args++; |
1067 | 367e86e8 | bellard | for(i = nb_args; i < MAX_ARGS; i++) {
|
1068 | 367e86e8 | bellard | if (args_present[i])
|
1069 | 367e86e8 | bellard | error("inconsistent argument numbering in %s", name);
|
1070 | 367e86e8 | bellard | } |
1071 | 367e86e8 | bellard | |
1072 | 0ea00c9a | bellard | if (gen_switch == 2) { |
1073 | a513fe19 | bellard | fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size); |
1074 | 0ea00c9a | bellard | } else if (gen_switch == 1) { |
1075 | dc99065b | bellard | |
1076 | dc99065b | bellard | /* output C code */
|
1077 | dc99065b | bellard | fprintf(outfile, "case INDEX_%s: {\n", name);
|
1078 | dc99065b | bellard | if (nb_args > 0) { |
1079 | dc99065b | bellard | fprintf(outfile, " long ");
|
1080 | dc99065b | bellard | for(i = 0; i < nb_args; i++) { |
1081 | dc99065b | bellard | if (i != 0) |
1082 | dc99065b | bellard | fprintf(outfile, ", ");
|
1083 | dc99065b | bellard | fprintf(outfile, "param%d", i + 1); |
1084 | dc99065b | bellard | } |
1085 | dc99065b | bellard | fprintf(outfile, ";\n");
|
1086 | 367e86e8 | bellard | } |
1087 | dc99065b | bellard | fprintf(outfile, " extern void %s();\n", name);
|
1088 | dc99065b | bellard | |
1089 | ce11fedc | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1090 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1091 | ff1f20a3 | bellard | rel->r_offset < start_offset + (p_end - p_start)) { |
1092 | 67b915a5 | bellard | sym_name = get_rel_sym_name(rel); |
1093 | d4e8164f | bellard | if (*sym_name &&
|
1094 | d4e8164f | bellard | !strstart(sym_name, "__op_param", NULL) && |
1095 | d4e8164f | bellard | !strstart(sym_name, "__op_jmp", NULL)) { |
1096 | d014c98c | bellard | #if defined(HOST_SPARC)
|
1097 | d014c98c | bellard | if (sym_name[0] == '.') { |
1098 | d014c98c | bellard | fprintf(outfile, |
1099 | d014c98c | bellard | "extern char __dot_%s __asm__(\"%s\");\n",
|
1100 | d014c98c | bellard | sym_name+1, sym_name);
|
1101 | d014c98c | bellard | continue;
|
1102 | d014c98c | bellard | } |
1103 | d014c98c | bellard | #endif
|
1104 | ce11fedc | bellard | fprintf(outfile, "extern char %s;\n", sym_name);
|
1105 | dc99065b | bellard | } |
1106 | dc99065b | bellard | } |
1107 | dc99065b | bellard | } |
1108 | dc99065b | bellard | |
1109 | ae228531 | bellard | fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n", name, start_offset - offset, copy_size);
|
1110 | d4e8164f | bellard | |
1111 | d4e8164f | bellard | /* emit code offset information */
|
1112 | d4e8164f | bellard | { |
1113 | 67b915a5 | bellard | EXE_SYM *sym; |
1114 | d4e8164f | bellard | const char *sym_name, *p; |
1115 | abcd5da7 | bellard | unsigned long val; |
1116 | d4e8164f | bellard | int n;
|
1117 | d4e8164f | bellard | |
1118 | d4e8164f | bellard | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
1119 | 67b915a5 | bellard | sym_name = get_sym_name(sym); |
1120 | d4e8164f | bellard | if (strstart(sym_name, "__op_label", &p)) { |
1121 | c1e42a13 | bellard | uint8_t *ptr; |
1122 | fe319756 | bellard | unsigned long offset; |
1123 | fe319756 | bellard | |
1124 | d4e8164f | bellard | /* test if the variable refers to a label inside
|
1125 | d4e8164f | bellard | the code we are generating */
|
1126 | 67b915a5 | bellard | #ifdef CONFIG_FORMAT_COFF
|
1127 | 67b915a5 | bellard | if (sym->st_shndx == text_shndx) {
|
1128 | 67b915a5 | bellard | ptr = sdata[coff_text_shndx]; |
1129 | 67b915a5 | bellard | } else if (sym->st_shndx == data_shndx) { |
1130 | 67b915a5 | bellard | ptr = sdata[coff_data_shndx]; |
1131 | 67b915a5 | bellard | } else {
|
1132 | 67b915a5 | bellard | ptr = NULL;
|
1133 | 67b915a5 | bellard | } |
1134 | 67b915a5 | bellard | #else
|
1135 | fe319756 | bellard | ptr = sdata[sym->st_shndx]; |
1136 | 67b915a5 | bellard | #endif
|
1137 | fe319756 | bellard | if (!ptr)
|
1138 | fe319756 | bellard | error("__op_labelN in invalid section");
|
1139 | fe319756 | bellard | offset = sym->st_value; |
1140 | abcd5da7 | bellard | val = *(unsigned long *)(ptr + offset); |
1141 | fe319756 | bellard | #ifdef ELF_USES_RELOCA
|
1142 | fe319756 | bellard | { |
1143 | fe319756 | bellard | int reloc_shndx, nb_relocs1, j;
|
1144 | fe319756 | bellard | |
1145 | fe319756 | bellard | /* try to find a matching relocation */
|
1146 | fe319756 | bellard | reloc_shndx = find_reloc(sym->st_shndx); |
1147 | fe319756 | bellard | if (reloc_shndx) {
|
1148 | fe319756 | bellard | nb_relocs1 = shdr[reloc_shndx].sh_size / |
1149 | fe319756 | bellard | shdr[reloc_shndx].sh_entsize; |
1150 | fe319756 | bellard | rel = (ELF_RELOC *)sdata[reloc_shndx]; |
1151 | fe319756 | bellard | for(j = 0; j < nb_relocs1; j++) { |
1152 | fe319756 | bellard | if (rel->r_offset == offset) {
|
1153 | 039de852 | bellard | val = rel->r_addend; |
1154 | fe319756 | bellard | break;
|
1155 | fe319756 | bellard | } |
1156 | fe319756 | bellard | rel++; |
1157 | fe319756 | bellard | } |
1158 | fe319756 | bellard | } |
1159 | fe319756 | bellard | } |
1160 | fe319756 | bellard | #endif
|
1161 | fe319756 | bellard | |
1162 | d4e8164f | bellard | if (val >= start_offset && val < start_offset + copy_size) {
|
1163 | d4e8164f | bellard | n = strtol(p, NULL, 10); |
1164 | abcd5da7 | bellard | fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
|
1165 | d4e8164f | bellard | } |
1166 | d4e8164f | bellard | } |
1167 | d4e8164f | bellard | } |
1168 | d4e8164f | bellard | } |
1169 | d4e8164f | bellard | |
1170 | d4e8164f | bellard | /* load parameres in variables */
|
1171 | dc99065b | bellard | for(i = 0; i < nb_args; i++) { |
1172 | dc99065b | bellard | fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1); |
1173 | dc99065b | bellard | } |
1174 | dc99065b | bellard | |
1175 | dc99065b | bellard | /* patch relocations */
|
1176 | ce11fedc | bellard | #if defined(HOST_I386)
|
1177 | dc99065b | bellard | { |
1178 | dc99065b | bellard | char name[256]; |
1179 | dc99065b | bellard | int type;
|
1180 | ce11fedc | bellard | int addend;
|
1181 | dc99065b | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1182 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1183 | ae228531 | bellard | rel->r_offset < start_offset + copy_size) { |
1184 | 67b915a5 | bellard | sym_name = get_rel_sym_name(rel); |
1185 | ecd854fd | bellard | if (strstart(sym_name, "__op_jmp", &p)) { |
1186 | ecd854fd | bellard | int n;
|
1187 | ecd854fd | bellard | n = strtol(p, NULL, 10); |
1188 | ecd854fd | bellard | /* __op_jmp relocations are done at
|
1189 | ecd854fd | bellard | runtime to do translated block
|
1190 | ecd854fd | bellard | chaining: the offset of the instruction
|
1191 | ecd854fd | bellard | needs to be stored */
|
1192 | ecd854fd | bellard | fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
|
1193 | ecd854fd | bellard | n, rel->r_offset - start_offset); |
1194 | ecd854fd | bellard | continue;
|
1195 | ecd854fd | bellard | } |
1196 | ecd854fd | bellard | |
1197 | 367e86e8 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1198 | 367e86e8 | bellard | snprintf(name, sizeof(name), "param%s", p); |
1199 | 367e86e8 | bellard | } else {
|
1200 | 367e86e8 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1201 | 367e86e8 | bellard | } |
1202 | 367e86e8 | bellard | addend = get32((uint32_t *)(text + rel->r_offset)); |
1203 | 67b915a5 | bellard | #ifdef CONFIG_FORMAT_ELF
|
1204 | 67b915a5 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1205 | 367e86e8 | bellard | switch(type) {
|
1206 | 367e86e8 | bellard | case R_386_32:
|
1207 | ce11fedc | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1208 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1209 | 367e86e8 | bellard | break;
|
1210 | 367e86e8 | bellard | case R_386_PC32:
|
1211 | ce11fedc | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
|
1212 | ae228531 | bellard | rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
1213 | 367e86e8 | bellard | break;
|
1214 | 367e86e8 | bellard | default:
|
1215 | 367e86e8 | bellard | error("unsupported i386 relocation (%d)", type);
|
1216 | 367e86e8 | bellard | } |
1217 | 67b915a5 | bellard | #elif defined(CONFIG_FORMAT_COFF)
|
1218 | 40c3bac3 | bellard | { |
1219 | 40c3bac3 | bellard | char *temp_name;
|
1220 | 40c3bac3 | bellard | int j;
|
1221 | 40c3bac3 | bellard | EXE_SYM *sym; |
1222 | 40c3bac3 | bellard | temp_name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx)); |
1223 | 40c3bac3 | bellard | if (!strcmp(temp_name, ".data")) { |
1224 | 40c3bac3 | bellard | for (j = 0, sym = symtab; j < nb_syms; j++, sym++) { |
1225 | 40c3bac3 | bellard | if (strstart(sym->st_name, sym_name, NULL)) { |
1226 | 40c3bac3 | bellard | addend -= sym->st_value; |
1227 | 40c3bac3 | bellard | } |
1228 | 40c3bac3 | bellard | } |
1229 | 40c3bac3 | bellard | } |
1230 | 40c3bac3 | bellard | } |
1231 | 67b915a5 | bellard | type = rel->r_type; |
1232 | 67b915a5 | bellard | switch(type) {
|
1233 | 67b915a5 | bellard | case DIR32:
|
1234 | 67b915a5 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1235 | 67b915a5 | bellard | rel->r_offset - start_offset, name, addend); |
1236 | 67b915a5 | bellard | break;
|
1237 | 67b915a5 | bellard | case DISP32:
|
1238 | 67b915a5 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n",
|
1239 | 67b915a5 | bellard | rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
1240 | 67b915a5 | bellard | break;
|
1241 | 67b915a5 | bellard | default:
|
1242 | 67b915a5 | bellard | error("unsupported i386 relocation (%d)", type);
|
1243 | 67b915a5 | bellard | } |
1244 | 67b915a5 | bellard | #else
|
1245 | 67b915a5 | bellard | #error unsupport object format
|
1246 | 67b915a5 | bellard | #endif
|
1247 | 367e86e8 | bellard | } |
1248 | dc99065b | bellard | } |
1249 | dc99065b | bellard | } |
1250 | bc51c5c9 | bellard | #elif defined(HOST_AMD64)
|
1251 | bc51c5c9 | bellard | { |
1252 | bc51c5c9 | bellard | char name[256]; |
1253 | bc51c5c9 | bellard | int type;
|
1254 | bc51c5c9 | bellard | int addend;
|
1255 | bc51c5c9 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1256 | bc51c5c9 | bellard | if (rel->r_offset >= start_offset &&
|
1257 | bc51c5c9 | bellard | rel->r_offset < start_offset + copy_size) { |
1258 | bc51c5c9 | bellard | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
1259 | bc51c5c9 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1260 | bc51c5c9 | bellard | snprintf(name, sizeof(name), "param%s", p); |
1261 | bc51c5c9 | bellard | } else {
|
1262 | bc51c5c9 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1263 | bc51c5c9 | bellard | } |
1264 | bc51c5c9 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1265 | bc51c5c9 | bellard | addend = rel->r_addend; |
1266 | bc51c5c9 | bellard | switch(type) {
|
1267 | bc51c5c9 | bellard | case R_X86_64_32:
|
1268 | bc51c5c9 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
|
1269 | bc51c5c9 | bellard | rel->r_offset - start_offset, name, addend); |
1270 | bc51c5c9 | bellard | break;
|
1271 | bc51c5c9 | bellard | case R_X86_64_32S:
|
1272 | bc51c5c9 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
|
1273 | bc51c5c9 | bellard | rel->r_offset - start_offset, name, addend); |
1274 | bc51c5c9 | bellard | break;
|
1275 | bc51c5c9 | bellard | case R_X86_64_PC32:
|
1276 | bc51c5c9 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
|
1277 | bc51c5c9 | bellard | rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
1278 | bc51c5c9 | bellard | break;
|
1279 | bc51c5c9 | bellard | default:
|
1280 | bc51c5c9 | bellard | error("unsupported AMD64 relocation (%d)", type);
|
1281 | bc51c5c9 | bellard | } |
1282 | bc51c5c9 | bellard | } |
1283 | bc51c5c9 | bellard | } |
1284 | bc51c5c9 | bellard | } |
1285 | ce11fedc | bellard | #elif defined(HOST_PPC)
|
1286 | 04369ff2 | bellard | { |
1287 | 04369ff2 | bellard | char name[256]; |
1288 | 04369ff2 | bellard | int type;
|
1289 | ce11fedc | bellard | int addend;
|
1290 | 04369ff2 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1291 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1292 | ae228531 | bellard | rel->r_offset < start_offset + copy_size) { |
1293 | efdea7bf | bellard | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
1294 | d4e8164f | bellard | if (strstart(sym_name, "__op_jmp", &p)) { |
1295 | d4e8164f | bellard | int n;
|
1296 | d4e8164f | bellard | n = strtol(p, NULL, 10); |
1297 | d4e8164f | bellard | /* __op_jmp relocations are done at
|
1298 | d4e8164f | bellard | runtime to do translated block
|
1299 | d4e8164f | bellard | chaining: the offset of the instruction
|
1300 | d4e8164f | bellard | needs to be stored */
|
1301 | d4e8164f | bellard | fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
|
1302 | d4e8164f | bellard | n, rel->r_offset - start_offset); |
1303 | d4e8164f | bellard | continue;
|
1304 | d4e8164f | bellard | } |
1305 | d4e8164f | bellard | |
1306 | 04369ff2 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1307 | 04369ff2 | bellard | snprintf(name, sizeof(name), "param%s", p); |
1308 | 04369ff2 | bellard | } else {
|
1309 | 04369ff2 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1310 | 04369ff2 | bellard | } |
1311 | 04369ff2 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1312 | 04369ff2 | bellard | addend = rel->r_addend; |
1313 | 04369ff2 | bellard | switch(type) {
|
1314 | 04369ff2 | bellard | case R_PPC_ADDR32:
|
1315 | ce11fedc | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1316 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1317 | 04369ff2 | bellard | break;
|
1318 | 04369ff2 | bellard | case R_PPC_ADDR16_LO:
|
1319 | ce11fedc | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
|
1320 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1321 | 04369ff2 | bellard | break;
|
1322 | 04369ff2 | bellard | case R_PPC_ADDR16_HI:
|
1323 | ce11fedc | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
|
1324 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1325 | 04369ff2 | bellard | break;
|
1326 | 04369ff2 | bellard | case R_PPC_ADDR16_HA:
|
1327 | ce11fedc | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
|
1328 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1329 | 04369ff2 | bellard | break;
|
1330 | 04369ff2 | bellard | case R_PPC_REL24:
|
1331 | 04369ff2 | bellard | /* warning: must be at 32 MB distancy */
|
1332 | 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",
|
1333 | ae228531 | bellard | rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
1334 | 04369ff2 | bellard | break;
|
1335 | 04369ff2 | bellard | default:
|
1336 | 04369ff2 | bellard | error("unsupported powerpc relocation (%d)", type);
|
1337 | 04369ff2 | bellard | } |
1338 | 04369ff2 | bellard | } |
1339 | 04369ff2 | bellard | } |
1340 | 04369ff2 | bellard | } |
1341 | ce11fedc | bellard | #elif defined(HOST_S390)
|
1342 | fb3e5849 | bellard | { |
1343 | fb3e5849 | bellard | char name[256]; |
1344 | fb3e5849 | bellard | int type;
|
1345 | ce11fedc | bellard | int addend;
|
1346 | fb3e5849 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1347 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1348 | ae228531 | bellard | rel->r_offset < start_offset + copy_size) { |
1349 | efdea7bf | bellard | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
1350 | fb3e5849 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1351 | fb3e5849 | bellard | snprintf(name, sizeof(name), "param%s", p); |
1352 | fb3e5849 | bellard | } else {
|
1353 | fb3e5849 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1354 | fb3e5849 | bellard | } |
1355 | fb3e5849 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1356 | fb3e5849 | bellard | addend = rel->r_addend; |
1357 | fb3e5849 | bellard | switch(type) {
|
1358 | fb3e5849 | bellard | case R_390_32:
|
1359 | ce11fedc | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1360 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1361 | fb3e5849 | bellard | break;
|
1362 | fb3e5849 | bellard | case R_390_16:
|
1363 | ce11fedc | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1364 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1365 | fb3e5849 | bellard | break;
|
1366 | fb3e5849 | bellard | case R_390_8:
|
1367 | ce11fedc | bellard | fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1368 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1369 | fb3e5849 | bellard | break;
|
1370 | fb3e5849 | bellard | default:
|
1371 | fb3e5849 | bellard | error("unsupported s390 relocation (%d)", type);
|
1372 | fb3e5849 | bellard | } |
1373 | fb3e5849 | bellard | } |
1374 | fb3e5849 | bellard | } |
1375 | fb3e5849 | bellard | } |
1376 | efdea7bf | bellard | #elif defined(HOST_ALPHA)
|
1377 | efdea7bf | bellard | { |
1378 | efdea7bf | bellard | for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) { |
1379 | ae228531 | bellard | if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
|
1380 | efdea7bf | bellard | int type;
|
1381 | 74c95119 | bellard | |
1382 | efdea7bf | bellard | type = ELF64_R_TYPE(rel->r_info); |
1383 | 74c95119 | bellard | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
1384 | efdea7bf | bellard | switch (type) {
|
1385 | efdea7bf | bellard | case R_ALPHA_GPDISP:
|
1386 | 74c95119 | bellard | /* The gp is just 32 bit, and never changes, so it's easiest to emit it
|
1387 | 74c95119 | bellard | as an immediate instead of constructing it from the pv or ra. */
|
1388 | 74c95119 | bellard | fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
|
1389 | ae228531 | bellard | rel->r_offset - start_offset); |
1390 | 74c95119 | bellard | fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
|
1391 | ae228531 | bellard | rel->r_offset - start_offset + rel->r_addend); |
1392 | efdea7bf | bellard | break;
|
1393 | efdea7bf | bellard | case R_ALPHA_LITUSE:
|
1394 | efdea7bf | bellard | /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
|
1395 | efdea7bf | bellard | now, since some called functions (libc) need pv to be set up. */
|
1396 | efdea7bf | bellard | break;
|
1397 | efdea7bf | bellard | case R_ALPHA_HINT:
|
1398 | efdea7bf | bellard | /* Branch target prediction hint. Ignore for now. Should be already
|
1399 | efdea7bf | bellard | correct for in-function jumps. */
|
1400 | efdea7bf | bellard | break;
|
1401 | efdea7bf | bellard | case R_ALPHA_LITERAL:
|
1402 | 74c95119 | bellard | /* Load a literal from the GOT relative to the gp. Since there's only a
|
1403 | 74c95119 | bellard | single gp, nothing is to be done. */
|
1404 | 74c95119 | bellard | break;
|
1405 | 74c95119 | bellard | case R_ALPHA_GPRELHIGH:
|
1406 | 74c95119 | bellard | /* Handle fake relocations against __op_param symbol. Need to emit the
|
1407 | 74c95119 | bellard | high part of the immediate value instead. Other symbols need no
|
1408 | 74c95119 | bellard | special treatment. */
|
1409 | 74c95119 | bellard | if (strstart(sym_name, "__op_param", &p)) |
1410 | 74c95119 | bellard | fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
|
1411 | ae228531 | bellard | rel->r_offset - start_offset, p); |
1412 | 74c95119 | bellard | break;
|
1413 | 74c95119 | bellard | case R_ALPHA_GPRELLOW:
|
1414 | 74c95119 | bellard | if (strstart(sym_name, "__op_param", &p)) |
1415 | 74c95119 | bellard | fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
|
1416 | ae228531 | bellard | rel->r_offset - start_offset, p); |
1417 | 74c95119 | bellard | break;
|
1418 | 74c95119 | bellard | case R_ALPHA_BRSGP:
|
1419 | 74c95119 | bellard | /* PC-relative jump. Tweak offset to skip the two instructions that try to
|
1420 | 74c95119 | bellard | set up the gp from the pv. */
|
1421 | 2f87c607 | bellard | fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
|
1422 | ae228531 | bellard | rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset); |
1423 | efdea7bf | bellard | break;
|
1424 | efdea7bf | bellard | default:
|
1425 | efdea7bf | bellard | error("unsupported Alpha relocation (%d)", type);
|
1426 | efdea7bf | bellard | } |
1427 | efdea7bf | bellard | } |
1428 | efdea7bf | bellard | } |
1429 | efdea7bf | bellard | } |
1430 | efdea7bf | bellard | #elif defined(HOST_IA64)
|
1431 | efdea7bf | bellard | { |
1432 | efdea7bf | bellard | char name[256]; |
1433 | efdea7bf | bellard | int type;
|
1434 | efdea7bf | bellard | int addend;
|
1435 | efdea7bf | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1436 | ae228531 | bellard | if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
|
1437 | efdea7bf | bellard | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
1438 | efdea7bf | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1439 | efdea7bf | bellard | snprintf(name, sizeof(name), "param%s", p); |
1440 | efdea7bf | bellard | } else {
|
1441 | efdea7bf | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1442 | efdea7bf | bellard | } |
1443 | efdea7bf | bellard | type = ELF64_R_TYPE(rel->r_info); |
1444 | efdea7bf | bellard | addend = rel->r_addend; |
1445 | efdea7bf | bellard | switch(type) {
|
1446 | efdea7bf | bellard | case R_IA64_LTOFF22:
|
1447 | efdea7bf | bellard | error("must implemnt R_IA64_LTOFF22 relocation");
|
1448 | efdea7bf | bellard | case R_IA64_PCREL21B:
|
1449 | efdea7bf | bellard | error("must implemnt R_IA64_PCREL21B relocation");
|
1450 | efdea7bf | bellard | default:
|
1451 | efdea7bf | bellard | error("unsupported ia64 relocation (%d)", type);
|
1452 | efdea7bf | bellard | } |
1453 | efdea7bf | bellard | } |
1454 | efdea7bf | bellard | } |
1455 | efdea7bf | bellard | } |
1456 | d014c98c | bellard | #elif defined(HOST_SPARC)
|
1457 | d014c98c | bellard | { |
1458 | d014c98c | bellard | char name[256]; |
1459 | d014c98c | bellard | int type;
|
1460 | d014c98c | bellard | int addend;
|
1461 | d014c98c | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1462 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1463 | ae228531 | bellard | rel->r_offset < start_offset + copy_size) { |
1464 | d014c98c | bellard | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; |
1465 | d014c98c | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1466 | d014c98c | bellard | snprintf(name, sizeof(name), "param%s", p); |
1467 | d014c98c | bellard | } else {
|
1468 | d014c98c | bellard | if (sym_name[0] == '.') |
1469 | d014c98c | bellard | snprintf(name, sizeof(name),
|
1470 | d014c98c | bellard | "(long)(&__dot_%s)",
|
1471 | d014c98c | bellard | sym_name + 1);
|
1472 | d014c98c | bellard | else
|
1473 | d014c98c | bellard | snprintf(name, sizeof(name),
|
1474 | d014c98c | bellard | "(long)(&%s)", sym_name);
|
1475 | d014c98c | bellard | } |
1476 | d014c98c | bellard | type = ELF32_R_TYPE(rel->r_info); |
1477 | d014c98c | bellard | addend = rel->r_addend; |
1478 | d014c98c | bellard | switch(type) {
|
1479 | d014c98c | bellard | case R_SPARC_32:
|
1480 | d014c98c | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1481 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1482 | d014c98c | bellard | break;
|
1483 | d014c98c | bellard | case R_SPARC_HI22:
|
1484 | d014c98c | bellard | fprintf(outfile, |
1485 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
1486 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
1487 | d014c98c | bellard | " & ~0x3fffff) "
|
1488 | ae228531 | bellard | " | (((%s + %d) >> 10) & 0x3fffff);\n",
|
1489 | ae228531 | bellard | rel->r_offset - start_offset, |
1490 | ae228531 | bellard | rel->r_offset - start_offset, |
1491 | d014c98c | bellard | name, addend); |
1492 | d014c98c | bellard | break;
|
1493 | d014c98c | bellard | case R_SPARC_LO10:
|
1494 | d014c98c | bellard | fprintf(outfile, |
1495 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
1496 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
1497 | d014c98c | bellard | " & ~0x3ff) "
|
1498 | d014c98c | bellard | " | ((%s + %d) & 0x3ff);\n",
|
1499 | ae228531 | bellard | rel->r_offset - start_offset, |
1500 | ae228531 | bellard | rel->r_offset - start_offset, |
1501 | d014c98c | bellard | name, addend); |
1502 | d014c98c | bellard | break;
|
1503 | d014c98c | bellard | case R_SPARC_WDISP30:
|
1504 | d014c98c | bellard | fprintf(outfile, |
1505 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
1506 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
1507 | d014c98c | bellard | " & ~0x3fffffff) "
|
1508 | ae228531 | bellard | " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
|
1509 | d014c98c | bellard | " & 0x3fffffff);\n",
|
1510 | ae228531 | bellard | rel->r_offset - start_offset, |
1511 | ae228531 | bellard | rel->r_offset - start_offset, |
1512 | ae228531 | bellard | name, addend, |
1513 | ae228531 | bellard | rel->r_offset - start_offset); |
1514 | d014c98c | bellard | break;
|
1515 | d014c98c | bellard | default:
|
1516 | d014c98c | bellard | error("unsupported sparc relocation (%d)", type);
|
1517 | d014c98c | bellard | } |
1518 | d014c98c | bellard | } |
1519 | d014c98c | bellard | } |
1520 | d014c98c | bellard | } |
1521 | d014c98c | bellard | #elif defined(HOST_SPARC64)
|
1522 | d014c98c | bellard | { |
1523 | d014c98c | bellard | char name[256]; |
1524 | d014c98c | bellard | int type;
|
1525 | d014c98c | bellard | int addend;
|
1526 | d014c98c | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1527 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1528 | ae228531 | bellard | rel->r_offset < start_offset + copy_size) { |
1529 | d014c98c | bellard | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
1530 | d014c98c | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1531 | d014c98c | bellard | snprintf(name, sizeof(name), "param%s", p); |
1532 | d014c98c | bellard | } else {
|
1533 | d014c98c | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1534 | d014c98c | bellard | } |
1535 | d014c98c | bellard | type = ELF64_R_TYPE(rel->r_info); |
1536 | d014c98c | bellard | addend = rel->r_addend; |
1537 | d014c98c | bellard | switch(type) {
|
1538 | d014c98c | bellard | case R_SPARC_32:
|
1539 | d014c98c | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1540 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1541 | d014c98c | bellard | break;
|
1542 | d014c98c | bellard | case R_SPARC_HI22:
|
1543 | d014c98c | bellard | fprintf(outfile, |
1544 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
1545 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
1546 | d014c98c | bellard | " & ~0x3fffff) "
|
1547 | ae228531 | bellard | " | (((%s + %d) >> 10) & 0x3fffff);\n",
|
1548 | ae228531 | bellard | rel->r_offset - start_offset, |
1549 | ae228531 | bellard | rel->r_offset - start_offset, |
1550 | d014c98c | bellard | name, addend); |
1551 | d014c98c | bellard | break;
|
1552 | d014c98c | bellard | case R_SPARC_LO10:
|
1553 | d014c98c | bellard | fprintf(outfile, |
1554 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
1555 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
1556 | d014c98c | bellard | " & ~0x3ff) "
|
1557 | d014c98c | bellard | " | ((%s + %d) & 0x3ff);\n",
|
1558 | ae228531 | bellard | rel->r_offset - start_offset, |
1559 | ae228531 | bellard | rel->r_offset - start_offset, |
1560 | d014c98c | bellard | name, addend); |
1561 | d014c98c | bellard | break;
|
1562 | d014c98c | bellard | case R_SPARC_WDISP30:
|
1563 | d014c98c | bellard | fprintf(outfile, |
1564 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
1565 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
1566 | d014c98c | bellard | " & ~0x3fffffff) "
|
1567 | ae228531 | bellard | " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
|
1568 | d014c98c | bellard | " & 0x3fffffff);\n",
|
1569 | ae228531 | bellard | rel->r_offset - start_offset, |
1570 | ae228531 | bellard | rel->r_offset - start_offset, |
1571 | ae228531 | bellard | name, addend, |
1572 | ae228531 | bellard | rel->r_offset - start_offset); |
1573 | d014c98c | bellard | break;
|
1574 | d014c98c | bellard | default:
|
1575 | d014c98c | bellard | error("unsupported sparc64 relocation (%d)", type);
|
1576 | d014c98c | bellard | } |
1577 | d014c98c | bellard | } |
1578 | d014c98c | bellard | } |
1579 | d014c98c | bellard | } |
1580 | ff1f20a3 | bellard | #elif defined(HOST_ARM)
|
1581 | ff1f20a3 | bellard | { |
1582 | ff1f20a3 | bellard | char name[256]; |
1583 | ff1f20a3 | bellard | int type;
|
1584 | ff1f20a3 | bellard | int addend;
|
1585 | ff1f20a3 | bellard | |
1586 | ff1f20a3 | bellard | arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end, |
1587 | ff1f20a3 | bellard | relocs, nb_relocs); |
1588 | ff1f20a3 | bellard | |
1589 | ff1f20a3 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1590 | ff1f20a3 | bellard | if (rel->r_offset >= start_offset &&
|
1591 | ff1f20a3 | bellard | rel->r_offset < start_offset + copy_size) { |
1592 | ff1f20a3 | bellard | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
1593 | ff1f20a3 | bellard | /* the compiler leave some unnecessary references to the code */
|
1594 | ff1f20a3 | bellard | if (sym_name[0] == '\0') |
1595 | ff1f20a3 | bellard | continue;
|
1596 | ff1f20a3 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1597 | ff1f20a3 | bellard | snprintf(name, sizeof(name), "param%s", p); |
1598 | ff1f20a3 | bellard | } else {
|
1599 | ff1f20a3 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1600 | ff1f20a3 | bellard | } |
1601 | ff1f20a3 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1602 | ff1f20a3 | bellard | addend = get32((uint32_t *)(text + rel->r_offset)); |
1603 | ff1f20a3 | bellard | switch(type) {
|
1604 | ff1f20a3 | bellard | case R_ARM_ABS32:
|
1605 | ff1f20a3 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1606 | ff1f20a3 | bellard | rel->r_offset - start_offset, name, addend); |
1607 | ff1f20a3 | bellard | break;
|
1608 | ff1f20a3 | bellard | case R_ARM_PC24:
|
1609 | ff1f20a3 | bellard | fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
|
1610 | ff1f20a3 | bellard | rel->r_offset - start_offset, addend, name); |
1611 | ff1f20a3 | bellard | break;
|
1612 | ff1f20a3 | bellard | default:
|
1613 | ff1f20a3 | bellard | error("unsupported arm relocation (%d)", type);
|
1614 | ff1f20a3 | bellard | } |
1615 | ff1f20a3 | bellard | } |
1616 | ff1f20a3 | bellard | } |
1617 | 38e584a0 | bellard | } |
1618 | 38e584a0 | bellard | #elif defined(HOST_M68K)
|
1619 | 38e584a0 | bellard | { |
1620 | 38e584a0 | bellard | char name[256]; |
1621 | 38e584a0 | bellard | int type;
|
1622 | 38e584a0 | bellard | int addend;
|
1623 | 38e584a0 | bellard | Elf32_Sym *sym; |
1624 | 38e584a0 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1625 | 38e584a0 | bellard | if (rel->r_offset >= start_offset &&
|
1626 | 38e584a0 | bellard | rel->r_offset < start_offset + copy_size) { |
1627 | 38e584a0 | bellard | sym = &(symtab[ELFW(R_SYM)(rel->r_info)]); |
1628 | 38e584a0 | bellard | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
1629 | 38e584a0 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1630 | 38e584a0 | bellard | snprintf(name, sizeof(name), "param%s", p); |
1631 | 38e584a0 | bellard | } else {
|
1632 | 38e584a0 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1633 | 38e584a0 | bellard | } |
1634 | 38e584a0 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1635 | 38e584a0 | bellard | addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend; |
1636 | 38e584a0 | bellard | switch(type) {
|
1637 | 38e584a0 | bellard | case R_68K_32:
|
1638 | 38e584a0 | bellard | fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ;
|
1639 | 38e584a0 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
|
1640 | 38e584a0 | bellard | rel->r_offset - start_offset, name, addend ); |
1641 | 38e584a0 | bellard | break;
|
1642 | 38e584a0 | bellard | case R_68K_PC32:
|
1643 | 38e584a0 | bellard | fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset);
|
1644 | 38e584a0 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
|
1645 | 38e584a0 | bellard | rel->r_offset - start_offset, name, rel->r_offset - start_offset, /*sym->st_value+*/ addend);
|
1646 | 38e584a0 | bellard | break;
|
1647 | 38e584a0 | bellard | default:
|
1648 | 38e584a0 | bellard | error("unsupported m68k relocation (%d)", type);
|
1649 | 38e584a0 | bellard | } |
1650 | 38e584a0 | bellard | } |
1651 | 38e584a0 | bellard | } |
1652 | ff1f20a3 | bellard | } |
1653 | ce11fedc | bellard | #else
|
1654 | ce11fedc | bellard | #error unsupported CPU
|
1655 | ce11fedc | bellard | #endif
|
1656 | dc99065b | bellard | fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
|
1657 | dc99065b | bellard | fprintf(outfile, "}\n");
|
1658 | dc99065b | bellard | fprintf(outfile, "break;\n\n");
|
1659 | dc99065b | bellard | } else {
|
1660 | dc99065b | bellard | fprintf(outfile, "static inline void gen_%s(", name);
|
1661 | dc99065b | bellard | if (nb_args == 0) { |
1662 | dc99065b | bellard | fprintf(outfile, "void");
|
1663 | dc99065b | bellard | } else {
|
1664 | dc99065b | bellard | for(i = 0; i < nb_args; i++) { |
1665 | dc99065b | bellard | if (i != 0) |
1666 | dc99065b | bellard | fprintf(outfile, ", ");
|
1667 | dc99065b | bellard | fprintf(outfile, "long param%d", i + 1); |
1668 | 367e86e8 | bellard | } |
1669 | 367e86e8 | bellard | } |
1670 | dc99065b | bellard | fprintf(outfile, ")\n");
|
1671 | dc99065b | bellard | fprintf(outfile, "{\n");
|
1672 | dc99065b | bellard | for(i = 0; i < nb_args; i++) { |
1673 | dc99065b | bellard | fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1); |
1674 | dc99065b | bellard | } |
1675 | dc99065b | bellard | fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
|
1676 | dc99065b | bellard | fprintf(outfile, "}\n\n");
|
1677 | 367e86e8 | bellard | } |
1678 | 367e86e8 | bellard | } |
1679 | 367e86e8 | bellard | |
1680 | 67b915a5 | bellard | int gen_file(FILE *outfile, int out_type) |
1681 | 367e86e8 | bellard | { |
1682 | 67b915a5 | bellard | int i;
|
1683 | 67b915a5 | bellard | EXE_SYM *sym; |
1684 | 367e86e8 | bellard | |
1685 | d219f7e7 | bellard | if (out_type == OUT_INDEX_OP) {
|
1686 | a513fe19 | bellard | fprintf(outfile, "DEF(end, 0, 0)\n");
|
1687 | e477b8b8 | bellard | fprintf(outfile, "DEF(nop, 0, 0)\n");
|
1688 | e477b8b8 | bellard | fprintf(outfile, "DEF(nop1, 1, 0)\n");
|
1689 | e477b8b8 | bellard | fprintf(outfile, "DEF(nop2, 2, 0)\n");
|
1690 | e477b8b8 | bellard | fprintf(outfile, "DEF(nop3, 3, 0)\n");
|
1691 | dc99065b | bellard | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
1692 | dc99065b | bellard | const char *name, *p; |
1693 | 67b915a5 | bellard | name = get_sym_name(sym); |
1694 | dc99065b | bellard | if (strstart(name, OP_PREFIX, &p)) {
|
1695 | 67b915a5 | bellard | gen_code(name, sym->st_value, sym->st_size, outfile, 2);
|
1696 | dc99065b | bellard | } |
1697 | dc99065b | bellard | } |
1698 | d219f7e7 | bellard | } else if (out_type == OUT_GEN_OP) { |
1699 | d219f7e7 | bellard | /* generate gen_xxx functions */
|
1700 | d219f7e7 | bellard | |
1701 | d219f7e7 | bellard | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
1702 | d219f7e7 | bellard | const char *name; |
1703 | 67b915a5 | bellard | name = get_sym_name(sym); |
1704 | d219f7e7 | bellard | if (strstart(name, OP_PREFIX, NULL)) { |
1705 | 67b915a5 | bellard | if (sym->st_shndx != text_shndx)
|
1706 | d219f7e7 | bellard | error("invalid section for opcode (0x%x)", sym->st_shndx);
|
1707 | 67b915a5 | bellard | gen_code(name, sym->st_value, sym->st_size, outfile, 0);
|
1708 | d219f7e7 | bellard | } |
1709 | d219f7e7 | bellard | } |
1710 | d219f7e7 | bellard | |
1711 | dc99065b | bellard | } else {
|
1712 | dc99065b | bellard | /* generate big code generation switch */
|
1713 | dc99065b | bellard | fprintf(outfile, |
1714 | dc99065b | bellard | "int dyngen_code(uint8_t *gen_code_buf,\n"
|
1715 | d4e8164f | bellard | " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
|
1716 | dc99065b | bellard | " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
|
1717 | dc99065b | bellard | "{\n"
|
1718 | dc99065b | bellard | " uint8_t *gen_code_ptr;\n"
|
1719 | dc99065b | bellard | " const uint16_t *opc_ptr;\n"
|
1720 | ff1f20a3 | bellard | " const uint32_t *opparam_ptr;\n");
|
1721 | ff1f20a3 | bellard | |
1722 | ff1f20a3 | bellard | #ifdef HOST_ARM
|
1723 | ff1f20a3 | bellard | fprintf(outfile, |
1724 | ff1f20a3 | bellard | " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
|
1725 | ff1f20a3 | bellard | " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
|
1726 | ff1f20a3 | bellard | " uint32_t *arm_data_ptr = arm_data_table;\n");
|
1727 | ff1f20a3 | bellard | #endif
|
1728 | ff1f20a3 | bellard | |
1729 | ff1f20a3 | bellard | fprintf(outfile, |
1730 | ff1f20a3 | bellard | "\n"
|
1731 | dc99065b | bellard | " gen_code_ptr = gen_code_buf;\n"
|
1732 | dc99065b | bellard | " opc_ptr = opc_buf;\n"
|
1733 | ae228531 | bellard | " opparam_ptr = opparam_buf;\n");
|
1734 | ae228531 | bellard | |
1735 | ae228531 | bellard | /* Generate prologue, if needed. */
|
1736 | ae228531 | bellard | |
1737 | ae228531 | bellard | fprintf(outfile, |
1738 | dc99065b | bellard | " for(;;) {\n"
|
1739 | dc99065b | bellard | " switch(*opc_ptr++) {\n"
|
1740 | dc99065b | bellard | ); |
1741 | 367e86e8 | bellard | |
1742 | dc99065b | bellard | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
1743 | dc99065b | bellard | const char *name; |
1744 | 67b915a5 | bellard | name = get_sym_name(sym); |
1745 | dc99065b | bellard | if (strstart(name, OP_PREFIX, NULL)) { |
1746 | 367e86e8 | bellard | #if 0
|
1747 | dc99065b | bellard | printf("%4d: %s pos=0x%08x len=%d\n",
|
1748 | dc99065b | bellard | i, name, sym->st_value, sym->st_size);
|
1749 | 367e86e8 | bellard | #endif
|
1750 | 67b915a5 | bellard | if (sym->st_shndx != text_shndx)
|
1751 | dc99065b | bellard | error("invalid section for opcode (0x%x)", sym->st_shndx);
|
1752 | 67b915a5 | bellard | gen_code(name, sym->st_value, sym->st_size, outfile, 1);
|
1753 | dc99065b | bellard | } |
1754 | dc99065b | bellard | } |
1755 | dc99065b | bellard | |
1756 | dc99065b | bellard | fprintf(outfile, |
1757 | 8ef9a8ec | bellard | " case INDEX_op_nop:\n"
|
1758 | 8ef9a8ec | bellard | " break;\n"
|
1759 | 8ef9a8ec | bellard | " case INDEX_op_nop1:\n"
|
1760 | 8ef9a8ec | bellard | " opparam_ptr++;\n"
|
1761 | 8ef9a8ec | bellard | " break;\n"
|
1762 | 8ef9a8ec | bellard | " case INDEX_op_nop2:\n"
|
1763 | 8ef9a8ec | bellard | " opparam_ptr += 2;\n"
|
1764 | 8ef9a8ec | bellard | " break;\n"
|
1765 | 8ef9a8ec | bellard | " case INDEX_op_nop3:\n"
|
1766 | 8ef9a8ec | bellard | " opparam_ptr += 3;\n"
|
1767 | 8ef9a8ec | bellard | " break;\n"
|
1768 | dc99065b | bellard | " default:\n"
|
1769 | dc99065b | bellard | " goto the_end;\n"
|
1770 | ff1f20a3 | bellard | " }\n");
|
1771 | ff1f20a3 | bellard | |
1772 | ff1f20a3 | bellard | #ifdef HOST_ARM
|
1773 | ff1f20a3 | bellard | /* generate constant table if needed */
|
1774 | ff1f20a3 | bellard | fprintf(outfile, |
1775 | ff1f20a3 | bellard | " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
|
1776 | 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"
|
1777 | ff1f20a3 | bellard | " last_gen_code_ptr = gen_code_ptr;\n"
|
1778 | ff1f20a3 | bellard | " arm_ldr_ptr = arm_ldr_table;\n"
|
1779 | ff1f20a3 | bellard | " arm_data_ptr = arm_data_table;\n"
|
1780 | ff1f20a3 | bellard | " }\n");
|
1781 | ff1f20a3 | bellard | #endif
|
1782 | ff1f20a3 | bellard | |
1783 | ff1f20a3 | bellard | |
1784 | ff1f20a3 | bellard | fprintf(outfile, |
1785 | dc99065b | bellard | " }\n"
|
1786 | dc99065b | bellard | " the_end:\n"
|
1787 | dc99065b | bellard | ); |
1788 | dc99065b | bellard | |
1789 | 9621339d | bellard | /* generate some code patching */
|
1790 | 9621339d | bellard | #ifdef HOST_ARM
|
1791 | 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");
|
1792 | 9621339d | bellard | #endif
|
1793 | d219f7e7 | bellard | /* flush instruction cache */
|
1794 | d219f7e7 | bellard | fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
|
1795 | d219f7e7 | bellard | |
1796 | dc99065b | bellard | fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
|
1797 | dc99065b | bellard | fprintf(outfile, "}\n\n");
|
1798 | dc99065b | bellard | |
1799 | 367e86e8 | bellard | } |
1800 | 367e86e8 | bellard | |
1801 | 367e86e8 | bellard | return 0; |
1802 | 367e86e8 | bellard | } |
1803 | 367e86e8 | bellard | |
1804 | 367e86e8 | bellard | void usage(void) |
1805 | 367e86e8 | bellard | { |
1806 | 367e86e8 | bellard | printf("dyngen (c) 2003 Fabrice Bellard\n"
|
1807 | dc99065b | bellard | "usage: dyngen [-o outfile] [-c] objfile\n"
|
1808 | dc99065b | bellard | "Generate a dynamic code generator from an object file\n"
|
1809 | dc99065b | bellard | "-c output enum of operations\n"
|
1810 | d219f7e7 | bellard | "-g output gen_op_xx() functions\n"
|
1811 | dc99065b | bellard | ); |
1812 | 367e86e8 | bellard | exit(1);
|
1813 | 367e86e8 | bellard | } |
1814 | 367e86e8 | bellard | |
1815 | 367e86e8 | bellard | int main(int argc, char **argv) |
1816 | 367e86e8 | bellard | { |
1817 | d219f7e7 | bellard | int c, out_type;
|
1818 | 367e86e8 | bellard | const char *filename, *outfilename; |
1819 | 367e86e8 | bellard | FILE *outfile; |
1820 | 367e86e8 | bellard | |
1821 | 367e86e8 | bellard | outfilename = "out.c";
|
1822 | d219f7e7 | bellard | out_type = OUT_CODE; |
1823 | 367e86e8 | bellard | for(;;) {
|
1824 | d219f7e7 | bellard | c = getopt(argc, argv, "ho:cg");
|
1825 | 367e86e8 | bellard | if (c == -1) |
1826 | 367e86e8 | bellard | break;
|
1827 | 367e86e8 | bellard | switch(c) {
|
1828 | 367e86e8 | bellard | case 'h': |
1829 | 367e86e8 | bellard | usage(); |
1830 | 367e86e8 | bellard | break;
|
1831 | 367e86e8 | bellard | case 'o': |
1832 | 367e86e8 | bellard | outfilename = optarg; |
1833 | 367e86e8 | bellard | break;
|
1834 | dc99065b | bellard | case 'c': |
1835 | d219f7e7 | bellard | out_type = OUT_INDEX_OP; |
1836 | d219f7e7 | bellard | break;
|
1837 | d219f7e7 | bellard | case 'g': |
1838 | d219f7e7 | bellard | out_type = OUT_GEN_OP; |
1839 | dc99065b | bellard | break;
|
1840 | 367e86e8 | bellard | } |
1841 | 367e86e8 | bellard | } |
1842 | 367e86e8 | bellard | if (optind >= argc)
|
1843 | 367e86e8 | bellard | usage(); |
1844 | 367e86e8 | bellard | filename = argv[optind]; |
1845 | 367e86e8 | bellard | outfile = fopen(outfilename, "w");
|
1846 | 367e86e8 | bellard | if (!outfile)
|
1847 | 367e86e8 | bellard | error("could not open '%s'", outfilename);
|
1848 | 67b915a5 | bellard | |
1849 | 67b915a5 | bellard | load_object(filename); |
1850 | 67b915a5 | bellard | gen_file(outfile, out_type); |
1851 | 367e86e8 | bellard | fclose(outfile); |
1852 | 367e86e8 | bellard | return 0; |
1853 | 367e86e8 | bellard | } |