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