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