root / dyngen.c @ fcc941fe
History | View | Annotate | Download (76 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 | bc51c5c9 | bellard | #elif defined(HOST_AMD64)
|
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 | 67b915a5 | bellard | return name;
|
639 | 367e86e8 | bellard | } |
640 | 367e86e8 | bellard | |
641 | 82eec0a1 | bellard | static host_ulong get_rel_offset(EXE_RELOC *rel)
|
642 | 82eec0a1 | bellard | { |
643 | 82eec0a1 | bellard | return rel->r_offset;
|
644 | 82eec0a1 | bellard | } |
645 | 82eec0a1 | bellard | |
646 | 67b915a5 | bellard | struct external_scnhdr *find_coff_section(struct external_scnhdr *shdr, int shnum, const char *name) |
647 | 367e86e8 | bellard | { |
648 | 367e86e8 | bellard | int i;
|
649 | 367e86e8 | bellard | const char *shname; |
650 | 67b915a5 | bellard | struct external_scnhdr *sec;
|
651 | 367e86e8 | bellard | |
652 | 367e86e8 | bellard | for(i = 0; i < shnum; i++) { |
653 | 367e86e8 | bellard | sec = &shdr[i]; |
654 | 67b915a5 | bellard | if (!sec->s_name)
|
655 | 367e86e8 | bellard | continue;
|
656 | 67b915a5 | bellard | shname = sec->s_name; |
657 | 367e86e8 | bellard | if (!strcmp(shname, name))
|
658 | 367e86e8 | bellard | return sec;
|
659 | 367e86e8 | bellard | } |
660 | 367e86e8 | bellard | return NULL; |
661 | 367e86e8 | bellard | } |
662 | 367e86e8 | bellard | |
663 | 67b915a5 | bellard | /* load a coff object file */
|
664 | 67b915a5 | bellard | int load_object(const char *filename) |
665 | fe319756 | bellard | { |
666 | 67b915a5 | bellard | int fd;
|
667 | 67b915a5 | bellard | struct external_scnhdr *sec, *text_sec, *data_sec;
|
668 | fe319756 | bellard | int i;
|
669 | 67b915a5 | bellard | struct external_syment *ext_sym;
|
670 | 67b915a5 | bellard | struct external_reloc *coff_relocs;
|
671 | 67b915a5 | bellard | struct external_reloc *ext_rel;
|
672 | 67b915a5 | bellard | uint32_t *n_strtab; |
673 | 67b915a5 | bellard | EXE_SYM *sym; |
674 | 67b915a5 | bellard | EXE_RELOC *rel; |
675 | 67b915a5 | bellard | |
676 | 67b915a5 | bellard | fd = open(filename, O_RDONLY |
677 | 67b915a5 | bellard | #ifdef _WIN32
|
678 | 67b915a5 | bellard | | O_BINARY |
679 | 67b915a5 | bellard | #endif
|
680 | 67b915a5 | bellard | ); |
681 | 67b915a5 | bellard | if (fd < 0) |
682 | 67b915a5 | bellard | error("can't open file '%s'", filename);
|
683 | 67b915a5 | bellard | |
684 | 67b915a5 | bellard | /* Read COFF header. */
|
685 | 67b915a5 | bellard | if (read(fd, &fhdr, sizeof (fhdr)) != sizeof (fhdr)) |
686 | 67b915a5 | bellard | error("unable to read file header");
|
687 | fe319756 | bellard | |
688 | 67b915a5 | bellard | /* Check COFF identification. */
|
689 | 67b915a5 | bellard | if (fhdr.f_magic != I386MAGIC) {
|
690 | 67b915a5 | bellard | error("bad COFF header");
|
691 | 67b915a5 | bellard | } |
692 | 67b915a5 | bellard | do_swap = 0;
|
693 | 67b915a5 | bellard | |
694 | 67b915a5 | bellard | /* read section headers */
|
695 | 67b915a5 | bellard | shdr = load_data(fd, sizeof(struct external_filehdr) + fhdr.f_opthdr, fhdr.f_nscns * sizeof(struct external_scnhdr)); |
696 | 67b915a5 | bellard | |
697 | 67b915a5 | bellard | /* read all section data */
|
698 | 67b915a5 | bellard | sdata = malloc(sizeof(void *) * fhdr.f_nscns); |
699 | 67b915a5 | bellard | memset(sdata, 0, sizeof(void *) * fhdr.f_nscns); |
700 | 67b915a5 | bellard | |
701 | 67b915a5 | bellard | const char *p; |
702 | 67b915a5 | bellard | for(i = 0;i < fhdr.f_nscns; i++) { |
703 | fe319756 | bellard | sec = &shdr[i]; |
704 | 67b915a5 | bellard | if (!strstart(sec->s_name, ".bss", &p)) |
705 | 67b915a5 | bellard | sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size); |
706 | fe319756 | bellard | } |
707 | fe319756 | bellard | |
708 | 367e86e8 | bellard | |
709 | 67b915a5 | bellard | /* text section */
|
710 | 67b915a5 | bellard | text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text");
|
711 | 67b915a5 | bellard | if (!text_sec)
|
712 | 67b915a5 | bellard | error("could not find .text section");
|
713 | 67b915a5 | bellard | coff_text_shndx = text_sec - shdr; |
714 | 67b915a5 | bellard | text = sdata[coff_text_shndx]; |
715 | 67b915a5 | bellard | |
716 | 67b915a5 | bellard | /* data section */
|
717 | 67b915a5 | bellard | data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data");
|
718 | 67b915a5 | bellard | if (!data_sec)
|
719 | 67b915a5 | bellard | error("could not find .data section");
|
720 | 67b915a5 | bellard | coff_data_shndx = data_sec - shdr; |
721 | 67b915a5 | bellard | |
722 | 67b915a5 | bellard | coff_symtab = load_data(fd, fhdr.f_symptr, fhdr.f_nsyms*SYMESZ); |
723 | 67b915a5 | bellard | for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) { |
724 | 67b915a5 | bellard | for(i=0;i<8;i++) |
725 | 67b915a5 | bellard | printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]);
|
726 | 67b915a5 | bellard | printf("\n");
|
727 | 367e86e8 | bellard | } |
728 | 367e86e8 | bellard | |
729 | 67b915a5 | bellard | |
730 | 67b915a5 | bellard | n_strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), STRTAB_SIZE); |
731 | 67b915a5 | bellard | strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), *n_strtab); |
732 | 67b915a5 | bellard | |
733 | 67b915a5 | bellard | nb_syms = fhdr.f_nsyms; |
734 | 67b915a5 | bellard | |
735 | 67b915a5 | bellard | for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) { |
736 | 67b915a5 | bellard | if (strstart(ext_sym->e.e_name, ".text", NULL)) |
737 | 67b915a5 | bellard | text_shndx = ext_sym->e_scnum; |
738 | 67b915a5 | bellard | if (strstart(ext_sym->e.e_name, ".data", NULL)) |
739 | 67b915a5 | bellard | data_shndx = ext_sym->e_scnum; |
740 | 367e86e8 | bellard | } |
741 | 67b915a5 | bellard | |
742 | 67b915a5 | bellard | /* set coff symbol */
|
743 | 67b915a5 | bellard | symtab = malloc(sizeof(struct coff_sym) * nb_syms); |
744 | 67b915a5 | bellard | |
745 | 67b915a5 | bellard | int aux_size, j;
|
746 | 67b915a5 | bellard | for (i = 0, ext_sym = coff_symtab, sym = symtab; i < nb_syms; i++, ext_sym++, sym++) { |
747 | 67b915a5 | bellard | memset(sym, 0, sizeof(*sym)); |
748 | 67b915a5 | bellard | sym->st_syment = ext_sym; |
749 | 67b915a5 | bellard | sym_ent_name(ext_sym, sym); |
750 | 67b915a5 | bellard | sym->st_value = ext_sym->e_value; |
751 | 67b915a5 | bellard | |
752 | 67b915a5 | bellard | aux_size = *(int8_t *)ext_sym->e_numaux; |
753 | 67b915a5 | bellard | if (ext_sym->e_scnum == text_shndx && ext_sym->e_type == T_FUNCTION) {
|
754 | 67b915a5 | bellard | for (j = aux_size + 1; j < nb_syms - i; j++) { |
755 | 67b915a5 | bellard | if ((ext_sym + j)->e_scnum == text_shndx &&
|
756 | 67b915a5 | bellard | (ext_sym + j)->e_type == T_FUNCTION ){ |
757 | 67b915a5 | bellard | sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value; |
758 | 67b915a5 | bellard | break;
|
759 | 67b915a5 | bellard | } else if (j == nb_syms - i - 1) { |
760 | 67b915a5 | bellard | sec = &shdr[coff_text_shndx]; |
761 | 67b915a5 | bellard | sym->st_size = sec->s_size - ext_sym->e_value; |
762 | 67b915a5 | bellard | break;
|
763 | 67b915a5 | bellard | } |
764 | 67b915a5 | bellard | } |
765 | 67b915a5 | bellard | } else if (ext_sym->e_scnum == data_shndx && *(uint8_t *)ext_sym->e_sclass == C_EXTERNAL) { |
766 | 67b915a5 | bellard | for (j = aux_size + 1; j < nb_syms - i; j++) { |
767 | 67b915a5 | bellard | if ((ext_sym + j)->e_scnum == data_shndx) {
|
768 | 67b915a5 | bellard | sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value; |
769 | 67b915a5 | bellard | break;
|
770 | 67b915a5 | bellard | } else if (j == nb_syms - i - 1) { |
771 | 67b915a5 | bellard | sec = &shdr[coff_data_shndx]; |
772 | 67b915a5 | bellard | sym->st_size = sec->s_size - ext_sym->e_value; |
773 | 67b915a5 | bellard | break;
|
774 | 67b915a5 | bellard | } |
775 | 67b915a5 | bellard | } |
776 | 67b915a5 | bellard | } else {
|
777 | 67b915a5 | bellard | sym->st_size = 0;
|
778 | 67b915a5 | bellard | } |
779 | 67b915a5 | bellard | |
780 | 67b915a5 | bellard | sym->st_type = ext_sym->e_type; |
781 | 67b915a5 | bellard | sym->st_shndx = ext_sym->e_scnum; |
782 | 67b915a5 | bellard | } |
783 | 67b915a5 | bellard | |
784 | 67b915a5 | bellard | |
785 | 67b915a5 | bellard | /* find text relocations, if any */
|
786 | 67b915a5 | bellard | sec = &shdr[coff_text_shndx]; |
787 | 67b915a5 | bellard | coff_relocs = load_data(fd, sec->s_relptr, sec->s_nreloc*RELSZ); |
788 | 67b915a5 | bellard | nb_relocs = sec->s_nreloc; |
789 | 67b915a5 | bellard | |
790 | 67b915a5 | bellard | /* set coff relocation */
|
791 | 67b915a5 | bellard | relocs = malloc(sizeof(struct coff_rel) * nb_relocs); |
792 | 67b915a5 | bellard | for (i = 0, ext_rel = coff_relocs, rel = relocs; i < nb_relocs; |
793 | 67b915a5 | bellard | i++, ext_rel++, rel++) { |
794 | 67b915a5 | bellard | memset(rel, 0, sizeof(*rel)); |
795 | 67b915a5 | bellard | rel->r_reloc = ext_rel; |
796 | 67b915a5 | bellard | rel->r_offset = *(uint32_t *)ext_rel->r_vaddr; |
797 | 67b915a5 | bellard | rel->r_type = *(uint16_t *)ext_rel->r_type; |
798 | 67b915a5 | bellard | } |
799 | 67b915a5 | bellard | return 0; |
800 | 367e86e8 | bellard | } |
801 | 367e86e8 | bellard | |
802 | 67b915a5 | bellard | #endif /* CONFIG_FORMAT_COFF */ |
803 | 67b915a5 | bellard | |
804 | 82eec0a1 | bellard | #ifdef CONFIG_FORMAT_MACH
|
805 | 82eec0a1 | bellard | |
806 | 82eec0a1 | bellard | /* File Header */
|
807 | 82eec0a1 | bellard | struct mach_header mach_hdr;
|
808 | 82eec0a1 | bellard | |
809 | 82eec0a1 | bellard | /* commands */
|
810 | 82eec0a1 | bellard | struct segment_command *segment = 0; |
811 | 82eec0a1 | bellard | struct dysymtab_command *dysymtabcmd = 0; |
812 | 82eec0a1 | bellard | struct symtab_command *symtabcmd = 0; |
813 | 82eec0a1 | bellard | |
814 | 82eec0a1 | bellard | /* section */
|
815 | 82eec0a1 | bellard | struct section *section_hdr;
|
816 | 82eec0a1 | bellard | struct section *text_sec_hdr;
|
817 | 82eec0a1 | bellard | uint8_t **sdata; |
818 | 82eec0a1 | bellard | |
819 | 82eec0a1 | bellard | /* relocs */
|
820 | 82eec0a1 | bellard | struct relocation_info *relocs;
|
821 | 82eec0a1 | bellard | |
822 | 82eec0a1 | bellard | /* symbols */
|
823 | 82eec0a1 | bellard | EXE_SYM *symtab; |
824 | 82eec0a1 | bellard | struct nlist *symtab_std;
|
825 | 82eec0a1 | bellard | char *strtab;
|
826 | 82eec0a1 | bellard | |
827 | 82eec0a1 | bellard | /* indirect symbols */
|
828 | 82eec0a1 | bellard | uint32_t *tocdylib; |
829 | 82eec0a1 | bellard | |
830 | 82eec0a1 | bellard | /* Utility functions */
|
831 | 82eec0a1 | bellard | |
832 | 82eec0a1 | bellard | static inline char *find_str_by_index(int index) |
833 | 82eec0a1 | bellard | { |
834 | 82eec0a1 | bellard | return strtab+index;
|
835 | 82eec0a1 | bellard | } |
836 | 82eec0a1 | bellard | |
837 | 82eec0a1 | bellard | /* Used by dyngen common code */
|
838 | 82eec0a1 | bellard | static char *get_sym_name(EXE_SYM *sym) |
839 | 82eec0a1 | bellard | { |
840 | 82eec0a1 | bellard | char *name = find_str_by_index(sym->n_un.n_strx);
|
841 | 82eec0a1 | bellard | |
842 | 82eec0a1 | bellard | if ( sym->n_type & N_STAB ) /* Debug symbols are ignored */ |
843 | 82eec0a1 | bellard | return "debug"; |
844 | 82eec0a1 | bellard | |
845 | 82eec0a1 | bellard | if(!name)
|
846 | 82eec0a1 | bellard | return name;
|
847 | 82eec0a1 | bellard | if(name[0]=='_') |
848 | 82eec0a1 | bellard | return name + 1; |
849 | 82eec0a1 | bellard | else
|
850 | 82eec0a1 | bellard | return name;
|
851 | 82eec0a1 | bellard | } |
852 | 82eec0a1 | bellard | |
853 | 82eec0a1 | bellard | /* find a section index given its segname, sectname */
|
854 | 82eec0a1 | bellard | static int find_mach_sec_index(struct section *section_hdr, int shnum, const char *segname, |
855 | 82eec0a1 | bellard | const char *sectname) |
856 | 82eec0a1 | bellard | { |
857 | 82eec0a1 | bellard | int i;
|
858 | 82eec0a1 | bellard | struct section *sec = section_hdr;
|
859 | 82eec0a1 | bellard | |
860 | 82eec0a1 | bellard | for(i = 0; i < shnum; i++, sec++) { |
861 | 82eec0a1 | bellard | if (!sec->segname || !sec->sectname)
|
862 | 82eec0a1 | bellard | continue;
|
863 | 82eec0a1 | bellard | if (!strcmp(sec->sectname, sectname) && !strcmp(sec->segname, segname))
|
864 | 82eec0a1 | bellard | return i;
|
865 | 82eec0a1 | bellard | } |
866 | 82eec0a1 | bellard | return -1; |
867 | 82eec0a1 | bellard | } |
868 | 82eec0a1 | bellard | |
869 | 82eec0a1 | bellard | /* find a section header given its segname, sectname */
|
870 | 82eec0a1 | bellard | struct section *find_mach_sec_hdr(struct section *section_hdr, int shnum, const char *segname, |
871 | 82eec0a1 | bellard | const char *sectname) |
872 | 82eec0a1 | bellard | { |
873 | 82eec0a1 | bellard | int index = find_mach_sec_index(section_hdr, shnum, segname, sectname);
|
874 | 82eec0a1 | bellard | if(index == -1) |
875 | 82eec0a1 | bellard | return NULL; |
876 | 82eec0a1 | bellard | return section_hdr+index;
|
877 | 82eec0a1 | bellard | } |
878 | 82eec0a1 | bellard | |
879 | 82eec0a1 | bellard | |
880 | 82eec0a1 | bellard | static inline void fetch_next_pair_value(struct relocation_info * rel, unsigned int *value) |
881 | 82eec0a1 | bellard | { |
882 | 82eec0a1 | bellard | struct scattered_relocation_info * scarel;
|
883 | 82eec0a1 | bellard | |
884 | 82eec0a1 | bellard | if(R_SCATTERED & rel->r_address) {
|
885 | 82eec0a1 | bellard | scarel = (struct scattered_relocation_info*)rel;
|
886 | 82eec0a1 | bellard | if(scarel->r_type != PPC_RELOC_PAIR)
|
887 | 82eec0a1 | bellard | error("fetch_next_pair_value: looking for a pair which was not found (1)");
|
888 | 82eec0a1 | bellard | *value = scarel->r_value; |
889 | 82eec0a1 | bellard | } else {
|
890 | 82eec0a1 | bellard | if(rel->r_type != PPC_RELOC_PAIR)
|
891 | 82eec0a1 | bellard | error("fetch_next_pair_value: looking for a pair which was not found (2)");
|
892 | 82eec0a1 | bellard | *value = rel->r_address; |
893 | 82eec0a1 | bellard | } |
894 | 82eec0a1 | bellard | } |
895 | 82eec0a1 | bellard | |
896 | 82eec0a1 | bellard | /* find a sym name given its value, in a section number */
|
897 | 82eec0a1 | bellard | static const char * find_sym_with_value_and_sec_number( int value, int sectnum, int * offset ) |
898 | 82eec0a1 | bellard | { |
899 | 82eec0a1 | bellard | int i, ret = -1; |
900 | 82eec0a1 | bellard | |
901 | 82eec0a1 | bellard | for( i = 0 ; i < nb_syms; i++ ) |
902 | 82eec0a1 | bellard | { |
903 | 82eec0a1 | bellard | if( !(symtab[i].n_type & N_STAB) && (symtab[i].n_type & N_SECT) &&
|
904 | 82eec0a1 | bellard | (symtab[i].n_sect == sectnum) && (symtab[i].st_value <= value) ) |
905 | 82eec0a1 | bellard | { |
906 | 82eec0a1 | bellard | if( (ret<0) || (symtab[i].st_value >= symtab[ret].st_value) ) |
907 | 82eec0a1 | bellard | ret = i; |
908 | 82eec0a1 | bellard | } |
909 | 82eec0a1 | bellard | } |
910 | 82eec0a1 | bellard | if( ret < 0 ) { |
911 | 82eec0a1 | bellard | *offset = 0;
|
912 | 82eec0a1 | bellard | return 0; |
913 | 82eec0a1 | bellard | } else {
|
914 | 82eec0a1 | bellard | *offset = value - symtab[ret].st_value; |
915 | 82eec0a1 | bellard | return get_sym_name(&symtab[ret]);
|
916 | 82eec0a1 | bellard | } |
917 | 82eec0a1 | bellard | } |
918 | 82eec0a1 | bellard | |
919 | 82eec0a1 | bellard | /*
|
920 | 82eec0a1 | bellard | * Find symbol name given a (virtual) address, and a section which is of type
|
921 | 82eec0a1 | bellard | * S_NON_LAZY_SYMBOL_POINTERS or S_LAZY_SYMBOL_POINTERS or S_SYMBOL_STUBS
|
922 | 82eec0a1 | bellard | */
|
923 | 82eec0a1 | bellard | static const char * find_reloc_name_in_sec_ptr(int address, struct section * sec_hdr) |
924 | 82eec0a1 | bellard | { |
925 | 82eec0a1 | bellard | unsigned int tocindex, symindex, size; |
926 | 82eec0a1 | bellard | const char *name = 0; |
927 | 82eec0a1 | bellard | |
928 | 82eec0a1 | bellard | /* Sanity check */
|
929 | 82eec0a1 | bellard | if(!( address >= sec_hdr->addr && address < (sec_hdr->addr + sec_hdr->size) ) )
|
930 | 82eec0a1 | bellard | return (char*)0; |
931 | 82eec0a1 | bellard | |
932 | 82eec0a1 | bellard | if( sec_hdr->flags & S_SYMBOL_STUBS ){
|
933 | 82eec0a1 | bellard | size = sec_hdr->reserved2; |
934 | 82eec0a1 | bellard | if(size == 0) |
935 | 82eec0a1 | bellard | error("size = 0");
|
936 | 82eec0a1 | bellard | |
937 | 82eec0a1 | bellard | } |
938 | 82eec0a1 | bellard | else if( sec_hdr->flags & S_LAZY_SYMBOL_POINTERS || |
939 | 82eec0a1 | bellard | sec_hdr->flags & S_NON_LAZY_SYMBOL_POINTERS) |
940 | 82eec0a1 | bellard | size = sizeof(unsigned long); |
941 | 82eec0a1 | bellard | else
|
942 | 82eec0a1 | bellard | return 0; |
943 | 82eec0a1 | bellard | |
944 | 82eec0a1 | bellard | /* Compute our index in toc */
|
945 | 82eec0a1 | bellard | tocindex = (address - sec_hdr->addr)/size; |
946 | 82eec0a1 | bellard | symindex = tocdylib[sec_hdr->reserved1 + tocindex]; |
947 | 82eec0a1 | bellard | |
948 | 82eec0a1 | bellard | name = get_sym_name(&symtab[symindex]); |
949 | 82eec0a1 | bellard | |
950 | 82eec0a1 | bellard | return name;
|
951 | 82eec0a1 | bellard | } |
952 | 82eec0a1 | bellard | |
953 | 82eec0a1 | bellard | static const char * find_reloc_name_given_its_address(int address) |
954 | 82eec0a1 | bellard | { |
955 | 82eec0a1 | bellard | unsigned int i; |
956 | 82eec0a1 | bellard | for(i = 0; i < segment->nsects ; i++) |
957 | 82eec0a1 | bellard | { |
958 | 82eec0a1 | bellard | const char * name = find_reloc_name_in_sec_ptr(address, §ion_hdr[i]); |
959 | 82eec0a1 | bellard | if((long)name != -1) |
960 | 82eec0a1 | bellard | return name;
|
961 | 82eec0a1 | bellard | } |
962 | 82eec0a1 | bellard | return 0; |
963 | 82eec0a1 | bellard | } |
964 | 82eec0a1 | bellard | |
965 | 82eec0a1 | bellard | static const char * get_reloc_name(EXE_RELOC * rel, int * sslide) |
966 | 82eec0a1 | bellard | { |
967 | 82eec0a1 | bellard | char * name = 0; |
968 | 82eec0a1 | bellard | struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel; |
969 | 82eec0a1 | bellard | int sectnum = rel->r_symbolnum;
|
970 | 82eec0a1 | bellard | int sectoffset;
|
971 | 82eec0a1 | bellard | int other_half=0; |
972 | 82eec0a1 | bellard | |
973 | 82eec0a1 | bellard | /* init the slide value */
|
974 | 82eec0a1 | bellard | *sslide = 0;
|
975 | 82eec0a1 | bellard | |
976 | 82eec0a1 | bellard | if(R_SCATTERED & rel->r_address)
|
977 | 82eec0a1 | bellard | return (char *)find_reloc_name_given_its_address(sca_rel->r_value); |
978 | 82eec0a1 | bellard | |
979 | 82eec0a1 | bellard | if(rel->r_extern)
|
980 | 82eec0a1 | bellard | { |
981 | 82eec0a1 | bellard | /* ignore debug sym */
|
982 | 82eec0a1 | bellard | if ( symtab[rel->r_symbolnum].n_type & N_STAB )
|
983 | 82eec0a1 | bellard | return 0; |
984 | 82eec0a1 | bellard | return get_sym_name(&symtab[rel->r_symbolnum]);
|
985 | 82eec0a1 | bellard | } |
986 | 82eec0a1 | bellard | |
987 | 82eec0a1 | bellard | /* Intruction contains an offset to the symbols pointed to, in the rel->r_symbolnum section */
|
988 | 82eec0a1 | bellard | sectoffset = *(uint32_t *)(text + rel->r_address) & 0xffff;
|
989 | 82eec0a1 | bellard | |
990 | 82eec0a1 | bellard | if(sectnum==0xffffff) |
991 | 82eec0a1 | bellard | return 0; |
992 | 82eec0a1 | bellard | |
993 | 82eec0a1 | bellard | /* Sanity Check */
|
994 | 82eec0a1 | bellard | if(sectnum > segment->nsects)
|
995 | 82eec0a1 | bellard | error("sectnum > segment->nsects");
|
996 | 82eec0a1 | bellard | |
997 | 82eec0a1 | bellard | switch(rel->r_type)
|
998 | 82eec0a1 | bellard | { |
999 | 82eec0a1 | bellard | case PPC_RELOC_LO16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (sectoffset & 0xffff); |
1000 | 82eec0a1 | bellard | break;
|
1001 | 82eec0a1 | bellard | case PPC_RELOC_HI16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (other_half & 0xffff); |
1002 | 82eec0a1 | bellard | break;
|
1003 | 82eec0a1 | bellard | case PPC_RELOC_HA16: fetch_next_pair_value(rel+1, &other_half); sectoffset = (other_half & 0xffff); |
1004 | 82eec0a1 | bellard | break;
|
1005 | 82eec0a1 | bellard | case PPC_RELOC_BR24:
|
1006 | 82eec0a1 | bellard | sectoffset = ( *(uint32_t *)(text + rel->r_address) & 0x03fffffc );
|
1007 | 82eec0a1 | bellard | if (sectoffset & 0x02000000) sectoffset |= 0xfc000000; |
1008 | 82eec0a1 | bellard | break;
|
1009 | 82eec0a1 | bellard | default:
|
1010 | 82eec0a1 | bellard | error("switch(rel->type) not found");
|
1011 | 82eec0a1 | bellard | } |
1012 | 82eec0a1 | bellard | |
1013 | 82eec0a1 | bellard | if(rel->r_pcrel)
|
1014 | 82eec0a1 | bellard | sectoffset += rel->r_address; |
1015 | 82eec0a1 | bellard | |
1016 | 82eec0a1 | bellard | if (rel->r_type == PPC_RELOC_BR24)
|
1017 | 82eec0a1 | bellard | name = (char *)find_reloc_name_in_sec_ptr((int)sectoffset, §ion_hdr[sectnum-1]); |
1018 | 82eec0a1 | bellard | |
1019 | 82eec0a1 | bellard | /* search it in the full symbol list, if not found */
|
1020 | 82eec0a1 | bellard | if(!name)
|
1021 | 82eec0a1 | bellard | name = (char *)find_sym_with_value_and_sec_number(sectoffset, sectnum, sslide);
|
1022 | 82eec0a1 | bellard | |
1023 | 82eec0a1 | bellard | return name;
|
1024 | 82eec0a1 | bellard | } |
1025 | 82eec0a1 | bellard | |
1026 | 82eec0a1 | bellard | /* Used by dyngen common code */
|
1027 | 82eec0a1 | bellard | static const char * get_rel_sym_name(EXE_RELOC * rel) |
1028 | 82eec0a1 | bellard | { |
1029 | 82eec0a1 | bellard | int sslide;
|
1030 | 82eec0a1 | bellard | return get_reloc_name( rel, &sslide);
|
1031 | 82eec0a1 | bellard | } |
1032 | 82eec0a1 | bellard | |
1033 | 82eec0a1 | bellard | /* Used by dyngen common code */
|
1034 | 82eec0a1 | bellard | static host_ulong get_rel_offset(EXE_RELOC *rel)
|
1035 | 82eec0a1 | bellard | { |
1036 | 82eec0a1 | bellard | struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel; |
1037 | 82eec0a1 | bellard | if(R_SCATTERED & rel->r_address)
|
1038 | 82eec0a1 | bellard | return sca_rel->r_address;
|
1039 | 82eec0a1 | bellard | else
|
1040 | 82eec0a1 | bellard | return rel->r_address;
|
1041 | 82eec0a1 | bellard | } |
1042 | 82eec0a1 | bellard | |
1043 | 82eec0a1 | bellard | /* load a mach-o object file */
|
1044 | 82eec0a1 | bellard | int load_object(const char *filename) |
1045 | 82eec0a1 | bellard | { |
1046 | 82eec0a1 | bellard | int fd;
|
1047 | 82eec0a1 | bellard | unsigned int offset_to_segment = 0; |
1048 | 82eec0a1 | bellard | unsigned int offset_to_dysymtab = 0; |
1049 | 82eec0a1 | bellard | unsigned int offset_to_symtab = 0; |
1050 | 82eec0a1 | bellard | struct load_command lc;
|
1051 | 82eec0a1 | bellard | unsigned int i, j; |
1052 | 82eec0a1 | bellard | EXE_SYM *sym; |
1053 | 82eec0a1 | bellard | struct nlist *syment;
|
1054 | 82eec0a1 | bellard | |
1055 | 82eec0a1 | bellard | fd = open(filename, O_RDONLY); |
1056 | 82eec0a1 | bellard | if (fd < 0) |
1057 | 82eec0a1 | bellard | error("can't open file '%s'", filename);
|
1058 | 82eec0a1 | bellard | |
1059 | 82eec0a1 | bellard | /* Read Mach header. */
|
1060 | 82eec0a1 | bellard | if (read(fd, &mach_hdr, sizeof (mach_hdr)) != sizeof (mach_hdr)) |
1061 | 82eec0a1 | bellard | error("unable to read file header");
|
1062 | 82eec0a1 | bellard | |
1063 | 82eec0a1 | bellard | /* Check Mach identification. */
|
1064 | 82eec0a1 | bellard | if (!check_mach_header(mach_hdr)) {
|
1065 | 82eec0a1 | bellard | error("bad Mach header");
|
1066 | 82eec0a1 | bellard | } |
1067 | 82eec0a1 | bellard | |
1068 | 82eec0a1 | bellard | if (mach_hdr.cputype != CPU_TYPE_POWERPC)
|
1069 | 82eec0a1 | bellard | error("Unsupported CPU");
|
1070 | 82eec0a1 | bellard | |
1071 | 82eec0a1 | bellard | if (mach_hdr.filetype != MH_OBJECT)
|
1072 | 82eec0a1 | bellard | error("Unsupported Mach Object");
|
1073 | 82eec0a1 | bellard | |
1074 | 82eec0a1 | bellard | /* read segment headers */
|
1075 | 82eec0a1 | bellard | for(i=0, j=sizeof(mach_hdr); i<mach_hdr.ncmds ; i++) |
1076 | 82eec0a1 | bellard | { |
1077 | 82eec0a1 | bellard | if(read(fd, &lc, sizeof(struct load_command)) != sizeof(struct load_command)) |
1078 | 82eec0a1 | bellard | error("unable to read load_command");
|
1079 | 82eec0a1 | bellard | if(lc.cmd == LC_SEGMENT)
|
1080 | 82eec0a1 | bellard | { |
1081 | 82eec0a1 | bellard | offset_to_segment = j; |
1082 | 82eec0a1 | bellard | lseek(fd, offset_to_segment, SEEK_SET); |
1083 | 82eec0a1 | bellard | segment = malloc(sizeof(struct segment_command)); |
1084 | 82eec0a1 | bellard | if(read(fd, segment, sizeof(struct segment_command)) != sizeof(struct segment_command)) |
1085 | 82eec0a1 | bellard | error("unable to read LC_SEGMENT");
|
1086 | 82eec0a1 | bellard | } |
1087 | 82eec0a1 | bellard | if(lc.cmd == LC_DYSYMTAB)
|
1088 | 82eec0a1 | bellard | { |
1089 | 82eec0a1 | bellard | offset_to_dysymtab = j; |
1090 | 82eec0a1 | bellard | lseek(fd, offset_to_dysymtab, SEEK_SET); |
1091 | 82eec0a1 | bellard | dysymtabcmd = malloc(sizeof(struct dysymtab_command)); |
1092 | 82eec0a1 | bellard | if(read(fd, dysymtabcmd, sizeof(struct dysymtab_command)) != sizeof(struct dysymtab_command)) |
1093 | 82eec0a1 | bellard | error("unable to read LC_DYSYMTAB");
|
1094 | 82eec0a1 | bellard | } |
1095 | 82eec0a1 | bellard | if(lc.cmd == LC_SYMTAB)
|
1096 | 82eec0a1 | bellard | { |
1097 | 82eec0a1 | bellard | offset_to_symtab = j; |
1098 | 82eec0a1 | bellard | lseek(fd, offset_to_symtab, SEEK_SET); |
1099 | 82eec0a1 | bellard | symtabcmd = malloc(sizeof(struct symtab_command)); |
1100 | 82eec0a1 | bellard | if(read(fd, symtabcmd, sizeof(struct symtab_command)) != sizeof(struct symtab_command)) |
1101 | 82eec0a1 | bellard | error("unable to read LC_SYMTAB");
|
1102 | 82eec0a1 | bellard | } |
1103 | 82eec0a1 | bellard | j+=lc.cmdsize; |
1104 | 82eec0a1 | bellard | |
1105 | 82eec0a1 | bellard | lseek(fd, j, SEEK_SET); |
1106 | 82eec0a1 | bellard | } |
1107 | 82eec0a1 | bellard | |
1108 | 82eec0a1 | bellard | if(!segment)
|
1109 | 82eec0a1 | bellard | error("unable to find LC_SEGMENT");
|
1110 | 82eec0a1 | bellard | |
1111 | 82eec0a1 | bellard | /* read section headers */
|
1112 | 82eec0a1 | bellard | section_hdr = load_data(fd, offset_to_segment + sizeof(struct segment_command), segment->nsects * sizeof(struct section)); |
1113 | 82eec0a1 | bellard | |
1114 | 82eec0a1 | bellard | /* read all section data */
|
1115 | 82eec0a1 | bellard | sdata = (uint8_t **)malloc(sizeof(void *) * segment->nsects); |
1116 | 82eec0a1 | bellard | memset(sdata, 0, sizeof(void *) * segment->nsects); |
1117 | 82eec0a1 | bellard | |
1118 | 82eec0a1 | bellard | /* Load the data in section data */
|
1119 | 82eec0a1 | bellard | for(i = 0; i < segment->nsects; i++) { |
1120 | 82eec0a1 | bellard | sdata[i] = load_data(fd, section_hdr[i].offset, section_hdr[i].size); |
1121 | 82eec0a1 | bellard | } |
1122 | 82eec0a1 | bellard | |
1123 | 82eec0a1 | bellard | /* text section */
|
1124 | 82eec0a1 | bellard | text_sec_hdr = find_mach_sec_hdr(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT); |
1125 | 82eec0a1 | bellard | i = find_mach_sec_index(section_hdr, segment->nsects, SEG_TEXT, SECT_TEXT); |
1126 | 82eec0a1 | bellard | if (i == -1 || !text_sec_hdr) |
1127 | 82eec0a1 | bellard | error("could not find __TEXT,__text section");
|
1128 | 82eec0a1 | bellard | text = sdata[i]; |
1129 | 82eec0a1 | bellard | |
1130 | 82eec0a1 | bellard | /* Make sure dysym was loaded */
|
1131 | 82eec0a1 | bellard | if(!(int)dysymtabcmd) |
1132 | 82eec0a1 | bellard | error("could not find __DYSYMTAB segment");
|
1133 | 82eec0a1 | bellard | |
1134 | 82eec0a1 | bellard | /* read the table of content of the indirect sym */
|
1135 | 82eec0a1 | bellard | tocdylib = load_data( fd, dysymtabcmd->indirectsymoff, dysymtabcmd->nindirectsyms * sizeof(uint32_t) );
|
1136 | 82eec0a1 | bellard | |
1137 | 82eec0a1 | bellard | /* Make sure symtab was loaded */
|
1138 | 82eec0a1 | bellard | if(!(int)symtabcmd) |
1139 | 82eec0a1 | bellard | error("could not find __SYMTAB segment");
|
1140 | 82eec0a1 | bellard | nb_syms = symtabcmd->nsyms; |
1141 | 82eec0a1 | bellard | |
1142 | 82eec0a1 | bellard | symtab_std = load_data(fd, symtabcmd->symoff, symtabcmd->nsyms * sizeof(struct nlist)); |
1143 | 82eec0a1 | bellard | strtab = load_data(fd, symtabcmd->stroff, symtabcmd->strsize); |
1144 | 82eec0a1 | bellard | |
1145 | 82eec0a1 | bellard | symtab = malloc(sizeof(EXE_SYM) * nb_syms);
|
1146 | 82eec0a1 | bellard | |
1147 | 82eec0a1 | bellard | /* Now transform the symtab, to an extended version, with the sym size, and the C name */
|
1148 | 82eec0a1 | bellard | for(i = 0, sym = symtab, syment = symtab_std; i < nb_syms; i++, sym++, syment++) { |
1149 | 82eec0a1 | bellard | const char *name; |
1150 | 82eec0a1 | bellard | struct nlist *sym_follow, *sym_next = 0; |
1151 | 82eec0a1 | bellard | unsigned int j; |
1152 | 82eec0a1 | bellard | name = find_str_by_index(sym->n_un.n_strx); |
1153 | 82eec0a1 | bellard | memset(sym, 0, sizeof(*sym)); |
1154 | 82eec0a1 | bellard | |
1155 | 82eec0a1 | bellard | if ( sym->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 | ff1f20a3 | bellard | #ifdef HOST_ARM
|
1189 | ff1f20a3 | bellard | |
1190 | ff1f20a3 | bellard | int arm_emit_ldr_info(const char *name, unsigned long start_offset, |
1191 | ff1f20a3 | bellard | FILE *outfile, uint8_t *p_start, uint8_t *p_end, |
1192 | ff1f20a3 | bellard | ELF_RELOC *relocs, int nb_relocs)
|
1193 | ff1f20a3 | bellard | { |
1194 | ff1f20a3 | bellard | uint8_t *p; |
1195 | ff1f20a3 | bellard | uint32_t insn; |
1196 | ff1f20a3 | bellard | int offset, min_offset, pc_offset, data_size;
|
1197 | ff1f20a3 | bellard | uint8_t data_allocated[1024];
|
1198 | ff1f20a3 | bellard | unsigned int data_index; |
1199 | ff1f20a3 | bellard | |
1200 | ff1f20a3 | bellard | memset(data_allocated, 0, sizeof(data_allocated)); |
1201 | ff1f20a3 | bellard | |
1202 | ff1f20a3 | bellard | p = p_start; |
1203 | ff1f20a3 | bellard | min_offset = p_end - p_start; |
1204 | ff1f20a3 | bellard | while (p < p_start + min_offset) {
|
1205 | ff1f20a3 | bellard | insn = get32((uint32_t *)p); |
1206 | ff1f20a3 | bellard | if ((insn & 0x0d5f0000) == 0x051f0000) { |
1207 | ff1f20a3 | bellard | /* ldr reg, [pc, #im] */
|
1208 | ff1f20a3 | bellard | offset = insn & 0xfff;
|
1209 | ff1f20a3 | bellard | if (!(insn & 0x00800000)) |
1210 | ff1f20a3 | bellard | offset = -offset; |
1211 | ff1f20a3 | bellard | if ((offset & 3) !=0) |
1212 | ff1f20a3 | bellard | error("%s:%04x: ldr pc offset must be 32 bit aligned",
|
1213 | ff1f20a3 | bellard | name, start_offset + p - p_start); |
1214 | ff1f20a3 | bellard | pc_offset = p - p_start + offset + 8;
|
1215 | ff1f20a3 | bellard | if (pc_offset <= (p - p_start) ||
|
1216 | ff1f20a3 | bellard | pc_offset >= (p_end - p_start)) |
1217 | ff1f20a3 | bellard | error("%s:%04x: ldr pc offset must point inside the function code",
|
1218 | ff1f20a3 | bellard | name, start_offset + p - p_start); |
1219 | ff1f20a3 | bellard | if (pc_offset < min_offset)
|
1220 | ff1f20a3 | bellard | min_offset = pc_offset; |
1221 | ff1f20a3 | bellard | if (outfile) {
|
1222 | ff1f20a3 | bellard | /* ldr position */
|
1223 | ff1f20a3 | bellard | fprintf(outfile, " arm_ldr_ptr->ptr = gen_code_ptr + %d;\n",
|
1224 | ff1f20a3 | bellard | p - p_start); |
1225 | ff1f20a3 | bellard | /* ldr data index */
|
1226 | ff1f20a3 | bellard | data_index = ((p_end - p_start) - pc_offset - 4) >> 2; |
1227 | ff1f20a3 | bellard | fprintf(outfile, " arm_ldr_ptr->data_ptr = arm_data_ptr + %d;\n",
|
1228 | ff1f20a3 | bellard | data_index); |
1229 | ff1f20a3 | bellard | fprintf(outfile, " arm_ldr_ptr++;\n");
|
1230 | ff1f20a3 | bellard | if (data_index >= sizeof(data_allocated)) |
1231 | ff1f20a3 | bellard | error("%s: too many data", name);
|
1232 | ff1f20a3 | bellard | if (!data_allocated[data_index]) {
|
1233 | ff1f20a3 | bellard | ELF_RELOC *rel; |
1234 | ff1f20a3 | bellard | int i, addend, type;
|
1235 | ff1f20a3 | bellard | const char *sym_name, *p; |
1236 | ff1f20a3 | bellard | char relname[1024]; |
1237 | ff1f20a3 | bellard | |
1238 | ff1f20a3 | bellard | data_allocated[data_index] = 1;
|
1239 | ff1f20a3 | bellard | |
1240 | ff1f20a3 | bellard | /* data value */
|
1241 | ff1f20a3 | bellard | addend = get32((uint32_t *)(p_start + pc_offset)); |
1242 | ff1f20a3 | bellard | relname[0] = '\0'; |
1243 | ff1f20a3 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1244 | ff1f20a3 | bellard | if (rel->r_offset == (pc_offset + start_offset)) {
|
1245 | 67b915a5 | bellard | sym_name = get_rel_sym_name(rel); |
1246 | ff1f20a3 | bellard | /* the compiler leave some unnecessary references to the code */
|
1247 | ff1f20a3 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1248 | ff1f20a3 | bellard | snprintf(relname, sizeof(relname), "param%s", p); |
1249 | ff1f20a3 | bellard | } else {
|
1250 | ff1f20a3 | bellard | snprintf(relname, sizeof(relname), "(long)(&%s)", sym_name); |
1251 | ff1f20a3 | bellard | } |
1252 | ff1f20a3 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1253 | ff1f20a3 | bellard | if (type != R_ARM_ABS32)
|
1254 | ff1f20a3 | bellard | error("%s: unsupported data relocation", name);
|
1255 | ff1f20a3 | bellard | break;
|
1256 | ff1f20a3 | bellard | } |
1257 | ff1f20a3 | bellard | } |
1258 | ff1f20a3 | bellard | fprintf(outfile, " arm_data_ptr[%d] = 0x%x",
|
1259 | ff1f20a3 | bellard | data_index, addend); |
1260 | ff1f20a3 | bellard | if (relname[0] != '\0') |
1261 | ff1f20a3 | bellard | fprintf(outfile, " + %s", relname);
|
1262 | ff1f20a3 | bellard | fprintf(outfile, ";\n");
|
1263 | ff1f20a3 | bellard | } |
1264 | ff1f20a3 | bellard | } |
1265 | ff1f20a3 | bellard | } |
1266 | ff1f20a3 | bellard | p += 4;
|
1267 | ff1f20a3 | bellard | } |
1268 | ff1f20a3 | bellard | data_size = (p_end - p_start) - min_offset; |
1269 | ff1f20a3 | bellard | if (data_size > 0 && outfile) { |
1270 | ff1f20a3 | bellard | fprintf(outfile, " arm_data_ptr += %d;\n", data_size >> 2); |
1271 | ff1f20a3 | bellard | } |
1272 | ff1f20a3 | bellard | |
1273 | ff1f20a3 | bellard | /* the last instruction must be a mov pc, lr */
|
1274 | ff1f20a3 | bellard | if (p == p_start)
|
1275 | ff1f20a3 | bellard | goto arm_ret_error;
|
1276 | ff1f20a3 | bellard | p -= 4;
|
1277 | ff1f20a3 | bellard | insn = get32((uint32_t *)p); |
1278 | ff1f20a3 | bellard | if ((insn & 0xffff0000) != 0xe91b0000) { |
1279 | ff1f20a3 | bellard | arm_ret_error:
|
1280 | ff1f20a3 | bellard | if (!outfile)
|
1281 | ff1f20a3 | bellard | printf("%s: invalid epilog\n", name);
|
1282 | ff1f20a3 | bellard | } |
1283 | ff1f20a3 | bellard | return p - p_start;
|
1284 | ff1f20a3 | bellard | } |
1285 | ff1f20a3 | bellard | #endif
|
1286 | ff1f20a3 | bellard | |
1287 | ff1f20a3 | bellard | |
1288 | 367e86e8 | bellard | #define MAX_ARGS 3 |
1289 | 367e86e8 | bellard | |
1290 | 367e86e8 | bellard | /* generate op code */
|
1291 | ce11fedc | bellard | void gen_code(const char *name, host_ulong offset, host_ulong size, |
1292 | 67b915a5 | bellard | FILE *outfile, int gen_switch)
|
1293 | 367e86e8 | bellard | { |
1294 | 367e86e8 | bellard | int copy_size = 0; |
1295 | 367e86e8 | bellard | uint8_t *p_start, *p_end; |
1296 | ae228531 | bellard | host_ulong start_offset; |
1297 | ce11fedc | bellard | int nb_args, i, n;
|
1298 | 367e86e8 | bellard | uint8_t args_present[MAX_ARGS]; |
1299 | 367e86e8 | bellard | const char *sym_name, *p; |
1300 | 67b915a5 | bellard | EXE_RELOC *rel; |
1301 | 367e86e8 | bellard | |
1302 | ae228531 | bellard | /* Compute exact size excluding prologue and epilogue instructions.
|
1303 | ae228531 | bellard | * Increment start_offset to skip epilogue instructions, then compute
|
1304 | ae228531 | bellard | * copy_size the indicate the size of the remaining instructions (in
|
1305 | ae228531 | bellard | * bytes).
|
1306 | ae228531 | bellard | */
|
1307 | 367e86e8 | bellard | p_start = text + offset; |
1308 | 367e86e8 | bellard | p_end = p_start + size; |
1309 | ae228531 | bellard | start_offset = offset; |
1310 | 67b915a5 | bellard | #if defined(HOST_I386) || defined(HOST_AMD64)
|
1311 | 67b915a5 | bellard | #ifdef CONFIG_FORMAT_COFF
|
1312 | 67b915a5 | bellard | { |
1313 | 67b915a5 | bellard | uint8_t *p; |
1314 | 67b915a5 | bellard | p = p_end - 1;
|
1315 | 67b915a5 | bellard | if (p == p_start)
|
1316 | 67b915a5 | bellard | error("empty code for %s", name);
|
1317 | 67b915a5 | bellard | while (*p != 0xc3) { |
1318 | 67b915a5 | bellard | p--; |
1319 | 67b915a5 | bellard | if (p <= p_start)
|
1320 | d4e8164f | bellard | error("ret or jmp expected at the end of %s", name);
|
1321 | 367e86e8 | bellard | } |
1322 | 67b915a5 | bellard | copy_size = p - p_start; |
1323 | 67b915a5 | bellard | } |
1324 | 67b915a5 | bellard | #else
|
1325 | 67b915a5 | bellard | { |
1326 | 67b915a5 | bellard | int len;
|
1327 | 67b915a5 | bellard | len = p_end - p_start; |
1328 | 67b915a5 | bellard | if (len == 0) |
1329 | 67b915a5 | bellard | error("empty code for %s", name);
|
1330 | 67b915a5 | bellard | if (p_end[-1] == 0xc3) { |
1331 | 67b915a5 | bellard | len--; |
1332 | 67b915a5 | bellard | } else {
|
1333 | 67b915a5 | bellard | error("ret or jmp expected at the end of %s", name);
|
1334 | 367e86e8 | bellard | } |
1335 | 67b915a5 | bellard | copy_size = len; |
1336 | 67b915a5 | bellard | } |
1337 | 67b915a5 | bellard | #endif
|
1338 | 67b915a5 | bellard | #elif defined(HOST_PPC)
|
1339 | 67b915a5 | bellard | { |
1340 | 67b915a5 | bellard | uint8_t *p; |
1341 | 67b915a5 | bellard | p = (void *)(p_end - 4); |
1342 | 67b915a5 | bellard | if (p == p_start)
|
1343 | 67b915a5 | bellard | error("empty code for %s", name);
|
1344 | 67b915a5 | bellard | if (get32((uint32_t *)p) != 0x4e800020) |
1345 | 67b915a5 | bellard | error("blr expected at the end of %s", name);
|
1346 | 67b915a5 | bellard | copy_size = p - p_start; |
1347 | 67b915a5 | bellard | } |
1348 | 67b915a5 | bellard | #elif defined(HOST_S390)
|
1349 | 67b915a5 | bellard | { |
1350 | 67b915a5 | bellard | uint8_t *p; |
1351 | 67b915a5 | bellard | p = (void *)(p_end - 2); |
1352 | 67b915a5 | bellard | if (p == p_start)
|
1353 | 67b915a5 | bellard | error("empty code for %s", name);
|
1354 | 67b915a5 | bellard | if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4) |
1355 | 67b915a5 | bellard | error("br %%r14 expected at the end of %s", name);
|
1356 | 67b915a5 | bellard | copy_size = p - p_start; |
1357 | 67b915a5 | bellard | } |
1358 | 67b915a5 | bellard | #elif defined(HOST_ALPHA)
|
1359 | 67b915a5 | bellard | { |
1360 | 67b915a5 | bellard | uint8_t *p; |
1361 | 67b915a5 | bellard | p = p_end - 4;
|
1362 | 630be16f | bellard | #if 0
|
1363 | 67b915a5 | bellard | /* XXX: check why it occurs */
|
1364 | 67b915a5 | bellard | if (p == p_start)
|
1365 | 67b915a5 | bellard | error("empty code for %s", name);
|
1366 | 630be16f | bellard | #endif
|
1367 | 67b915a5 | bellard | if (get32((uint32_t *)p) != 0x6bfa8001) |
1368 | 67b915a5 | bellard | error("ret expected at the end of %s", name);
|
1369 | 67b915a5 | bellard | copy_size = p - p_start; |
1370 | 67b915a5 | bellard | } |
1371 | 67b915a5 | bellard | #elif defined(HOST_IA64)
|
1372 | 67b915a5 | bellard | { |
1373 | 67b915a5 | bellard | uint8_t *p; |
1374 | 67b915a5 | bellard | p = (void *)(p_end - 4); |
1375 | 67b915a5 | bellard | if (p == p_start)
|
1376 | 67b915a5 | bellard | error("empty code for %s", name);
|
1377 | 67b915a5 | bellard | /* br.ret.sptk.many b0;; */
|
1378 | 67b915a5 | bellard | /* 08 00 84 00 */
|
1379 | 67b915a5 | bellard | if (get32((uint32_t *)p) != 0x00840008) |
1380 | 67b915a5 | bellard | error("br.ret.sptk.many b0;; expected at the end of %s", name);
|
1381 | 67b915a5 | bellard | copy_size = p - p_start; |
1382 | 67b915a5 | bellard | } |
1383 | 67b915a5 | bellard | #elif defined(HOST_SPARC)
|
1384 | 67b915a5 | bellard | { |
1385 | 67b915a5 | bellard | uint32_t start_insn, end_insn1, end_insn2; |
1386 | 67b915a5 | bellard | uint8_t *p; |
1387 | 67b915a5 | bellard | p = (void *)(p_end - 8); |
1388 | 67b915a5 | bellard | if (p <= p_start)
|
1389 | 67b915a5 | bellard | error("empty code for %s", name);
|
1390 | 67b915a5 | bellard | start_insn = get32((uint32_t *)(p_start + 0x0));
|
1391 | 67b915a5 | bellard | end_insn1 = get32((uint32_t *)(p + 0x0));
|
1392 | 67b915a5 | bellard | end_insn2 = get32((uint32_t *)(p + 0x4));
|
1393 | 67b915a5 | bellard | if ((start_insn & ~0x1fff) == 0x9de3a000) { |
1394 | 67b915a5 | bellard | p_start += 0x4;
|
1395 | 67b915a5 | bellard | start_offset += 0x4;
|
1396 | 67b915a5 | bellard | if ((int)(start_insn | ~0x1fff) < -128) |
1397 | 67b915a5 | bellard | error("Found bogus save at the start of %s", name);
|
1398 | 67b915a5 | bellard | if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000) |
1399 | 67b915a5 | bellard | error("ret; restore; not found at end of %s", name);
|
1400 | 67b915a5 | bellard | } else {
|
1401 | 67b915a5 | bellard | error("No save at the beginning of %s", name);
|
1402 | 67b915a5 | bellard | } |
1403 | ff1f20a3 | bellard | #if 0
|
1404 | 67b915a5 | bellard | /* Skip a preceeding nop, if present. */
|
1405 | 67b915a5 | bellard | if (p > p_start) {
|
1406 | 67b915a5 | bellard | skip_insn = get32((uint32_t *)(p - 0x4));
|
1407 | 67b915a5 | bellard | if (skip_insn == 0x01000000)
|
1408 | 67b915a5 | bellard | p -= 4;
|
1409 | 67b915a5 | bellard | }
|
1410 | ff1f20a3 | bellard | #endif
|
1411 | 67b915a5 | bellard | copy_size = p - p_start; |
1412 | 67b915a5 | bellard | } |
1413 | 67b915a5 | bellard | #elif defined(HOST_SPARC64)
|
1414 | 67b915a5 | bellard | { |
1415 | 67b915a5 | bellard | uint32_t start_insn, end_insn1, end_insn2, skip_insn; |
1416 | 67b915a5 | bellard | uint8_t *p; |
1417 | 67b915a5 | bellard | p = (void *)(p_end - 8); |
1418 | 67b915a5 | bellard | if (p <= p_start)
|
1419 | 67b915a5 | bellard | error("empty code for %s", name);
|
1420 | 67b915a5 | bellard | start_insn = get32((uint32_t *)(p_start + 0x0));
|
1421 | 67b915a5 | bellard | end_insn1 = get32((uint32_t *)(p + 0x0));
|
1422 | 67b915a5 | bellard | end_insn2 = get32((uint32_t *)(p + 0x4));
|
1423 | 67b915a5 | bellard | if ((start_insn & ~0x1fff) == 0x9de3a000) { |
1424 | 67b915a5 | bellard | p_start += 0x4;
|
1425 | 67b915a5 | bellard | start_offset += 0x4;
|
1426 | 67b915a5 | bellard | if ((int)(start_insn | ~0x1fff) < -256) |
1427 | 67b915a5 | bellard | error("Found bogus save at the start of %s", name);
|
1428 | 67b915a5 | bellard | if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000) |
1429 | 67b915a5 | bellard | error("ret; restore; not found at end of %s", name);
|
1430 | 67b915a5 | bellard | } else {
|
1431 | 67b915a5 | bellard | error("No save at the beginning of %s", name);
|
1432 | 67b915a5 | bellard | } |
1433 | 67b915a5 | bellard | |
1434 | 67b915a5 | bellard | /* Skip a preceeding nop, if present. */
|
1435 | 67b915a5 | bellard | if (p > p_start) {
|
1436 | 67b915a5 | bellard | skip_insn = get32((uint32_t *)(p - 0x4));
|
1437 | 67b915a5 | bellard | if (skip_insn == 0x01000000) |
1438 | 67b915a5 | bellard | p -= 4;
|
1439 | 67b915a5 | bellard | } |
1440 | 67b915a5 | bellard | |
1441 | 67b915a5 | bellard | copy_size = p - p_start; |
1442 | 67b915a5 | bellard | } |
1443 | 67b915a5 | bellard | #elif defined(HOST_ARM)
|
1444 | 67b915a5 | bellard | { |
1445 | ff1f20a3 | bellard | if ((p_end - p_start) <= 16) |
1446 | ff1f20a3 | bellard | error("%s: function too small", name);
|
1447 | ff1f20a3 | bellard | if (get32((uint32_t *)p_start) != 0xe1a0c00d || |
1448 | ff1f20a3 | bellard | (get32((uint32_t *)(p_start + 4)) & 0xffff0000) != 0xe92d0000 || |
1449 | ff1f20a3 | bellard | get32((uint32_t *)(p_start + 8)) != 0xe24cb004) |
1450 | ff1f20a3 | bellard | error("%s: invalid prolog", name);
|
1451 | ff1f20a3 | bellard | p_start += 12;
|
1452 | ff1f20a3 | bellard | start_offset += 12;
|
1453 | ff1f20a3 | bellard | copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end,
|
1454 | ff1f20a3 | bellard | relocs, nb_relocs); |
1455 | 367e86e8 | bellard | } |
1456 | 67b915a5 | bellard | #elif defined(HOST_M68K)
|
1457 | 67b915a5 | bellard | { |
1458 | 67b915a5 | bellard | uint8_t *p; |
1459 | 67b915a5 | bellard | p = (void *)(p_end - 2); |
1460 | 67b915a5 | bellard | if (p == p_start)
|
1461 | 67b915a5 | bellard | error("empty code for %s", name);
|
1462 | 67b915a5 | bellard | // remove NOP's, probably added for alignment
|
1463 | 67b915a5 | bellard | while ((get16((uint16_t *)p) == 0x4e71) && |
1464 | 67b915a5 | bellard | (p>p_start)) |
1465 | 67b915a5 | bellard | p -= 2;
|
1466 | 67b915a5 | bellard | if (get16((uint16_t *)p) != 0x4e75) |
1467 | 67b915a5 | bellard | error("rts expected at the end of %s", name);
|
1468 | 67b915a5 | bellard | copy_size = p - p_start; |
1469 | 67b915a5 | bellard | } |
1470 | 67b915a5 | bellard | #else
|
1471 | 67b915a5 | bellard | #error unsupported CPU
|
1472 | 67b915a5 | bellard | #endif
|
1473 | 367e86e8 | bellard | |
1474 | 367e86e8 | bellard | /* compute the number of arguments by looking at the relocations */
|
1475 | 367e86e8 | bellard | for(i = 0;i < MAX_ARGS; i++) |
1476 | 367e86e8 | bellard | args_present[i] = 0;
|
1477 | 367e86e8 | bellard | |
1478 | ce11fedc | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1479 | 82eec0a1 | bellard | host_ulong offset = get_rel_offset(rel); |
1480 | 82eec0a1 | bellard | if (offset >= start_offset &&
|
1481 | 82eec0a1 | bellard | offset < start_offset + (p_end - p_start)) { |
1482 | 67b915a5 | bellard | sym_name = get_rel_sym_name(rel); |
1483 | 82eec0a1 | bellard | if(!sym_name)
|
1484 | 82eec0a1 | bellard | continue;
|
1485 | ce11fedc | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1486 | ce11fedc | bellard | n = strtoul(p, NULL, 10); |
1487 | d4e8164f | bellard | if (n > MAX_ARGS)
|
1488 | ce11fedc | bellard | error("too many arguments in %s", name);
|
1489 | ce11fedc | bellard | args_present[n - 1] = 1; |
1490 | 367e86e8 | bellard | } |
1491 | 367e86e8 | bellard | } |
1492 | 367e86e8 | bellard | } |
1493 | 367e86e8 | bellard | |
1494 | 367e86e8 | bellard | nb_args = 0;
|
1495 | 367e86e8 | bellard | while (nb_args < MAX_ARGS && args_present[nb_args])
|
1496 | 367e86e8 | bellard | nb_args++; |
1497 | 367e86e8 | bellard | for(i = nb_args; i < MAX_ARGS; i++) {
|
1498 | 367e86e8 | bellard | if (args_present[i])
|
1499 | 367e86e8 | bellard | error("inconsistent argument numbering in %s", name);
|
1500 | 367e86e8 | bellard | } |
1501 | 367e86e8 | bellard | |
1502 | 0ea00c9a | bellard | if (gen_switch == 2) { |
1503 | a513fe19 | bellard | fprintf(outfile, "DEF(%s, %d, %d)\n", name + 3, nb_args, copy_size); |
1504 | 0ea00c9a | bellard | } else if (gen_switch == 1) { |
1505 | dc99065b | bellard | |
1506 | dc99065b | bellard | /* output C code */
|
1507 | dc99065b | bellard | fprintf(outfile, "case INDEX_%s: {\n", name);
|
1508 | dc99065b | bellard | if (nb_args > 0) { |
1509 | dc99065b | bellard | fprintf(outfile, " long ");
|
1510 | dc99065b | bellard | for(i = 0; i < nb_args; i++) { |
1511 | dc99065b | bellard | if (i != 0) |
1512 | dc99065b | bellard | fprintf(outfile, ", ");
|
1513 | dc99065b | bellard | fprintf(outfile, "param%d", i + 1); |
1514 | dc99065b | bellard | } |
1515 | dc99065b | bellard | fprintf(outfile, ";\n");
|
1516 | 367e86e8 | bellard | } |
1517 | dc99065b | bellard | fprintf(outfile, " extern void %s();\n", name);
|
1518 | dc99065b | bellard | |
1519 | ce11fedc | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1520 | 82eec0a1 | bellard | host_ulong offset = get_rel_offset(rel); |
1521 | 82eec0a1 | bellard | if (offset >= start_offset &&
|
1522 | 82eec0a1 | bellard | offset < start_offset + (p_end - p_start)) { |
1523 | 67b915a5 | bellard | sym_name = get_rel_sym_name(rel); |
1524 | 82eec0a1 | bellard | if(!sym_name)
|
1525 | 82eec0a1 | bellard | continue;
|
1526 | d4e8164f | bellard | if (*sym_name &&
|
1527 | d4e8164f | bellard | !strstart(sym_name, "__op_param", NULL) && |
1528 | d4e8164f | bellard | !strstart(sym_name, "__op_jmp", NULL)) { |
1529 | d014c98c | bellard | #if defined(HOST_SPARC)
|
1530 | d014c98c | bellard | if (sym_name[0] == '.') { |
1531 | d014c98c | bellard | fprintf(outfile, |
1532 | d014c98c | bellard | "extern char __dot_%s __asm__(\"%s\");\n",
|
1533 | d014c98c | bellard | sym_name+1, sym_name);
|
1534 | d014c98c | bellard | continue;
|
1535 | d014c98c | bellard | } |
1536 | d014c98c | bellard | #endif
|
1537 | 82eec0a1 | bellard | #ifdef __APPLE__
|
1538 | 82eec0a1 | bellard | /* set __attribute((unused)) on darwin because we wan't to avoid warning when we don't use the symbol */
|
1539 | 82eec0a1 | bellard | fprintf(outfile, "extern char %s __attribute__((unused));\n", sym_name);
|
1540 | 82eec0a1 | bellard | #else
|
1541 | ce11fedc | bellard | fprintf(outfile, "extern char %s;\n", sym_name);
|
1542 | 82eec0a1 | bellard | #endif
|
1543 | dc99065b | bellard | } |
1544 | dc99065b | bellard | } |
1545 | dc99065b | bellard | } |
1546 | dc99065b | bellard | |
1547 | 82eec0a1 | bellard | fprintf(outfile, " memcpy(gen_code_ptr, (void *)((char *)&%s+%d), %d);\n",
|
1548 | 82eec0a1 | bellard | name, (int)(start_offset - offset), copy_size);
|
1549 | d4e8164f | bellard | |
1550 | d4e8164f | bellard | /* emit code offset information */
|
1551 | d4e8164f | bellard | { |
1552 | 67b915a5 | bellard | EXE_SYM *sym; |
1553 | d4e8164f | bellard | const char *sym_name, *p; |
1554 | abcd5da7 | bellard | unsigned long val; |
1555 | d4e8164f | bellard | int n;
|
1556 | d4e8164f | bellard | |
1557 | d4e8164f | bellard | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
1558 | 67b915a5 | bellard | sym_name = get_sym_name(sym); |
1559 | d4e8164f | bellard | if (strstart(sym_name, "__op_label", &p)) { |
1560 | c1e42a13 | bellard | uint8_t *ptr; |
1561 | fe319756 | bellard | unsigned long offset; |
1562 | fe319756 | bellard | |
1563 | d4e8164f | bellard | /* test if the variable refers to a label inside
|
1564 | d4e8164f | bellard | the code we are generating */
|
1565 | 67b915a5 | bellard | #ifdef CONFIG_FORMAT_COFF
|
1566 | 67b915a5 | bellard | if (sym->st_shndx == text_shndx) {
|
1567 | 67b915a5 | bellard | ptr = sdata[coff_text_shndx]; |
1568 | 67b915a5 | bellard | } else if (sym->st_shndx == data_shndx) { |
1569 | 67b915a5 | bellard | ptr = sdata[coff_data_shndx]; |
1570 | 67b915a5 | bellard | } else {
|
1571 | 67b915a5 | bellard | ptr = NULL;
|
1572 | 67b915a5 | bellard | } |
1573 | 82eec0a1 | bellard | #elif defined(CONFIG_FORMAT_MACH)
|
1574 | 82eec0a1 | bellard | if(!sym->n_sect)
|
1575 | 82eec0a1 | bellard | continue;
|
1576 | 82eec0a1 | bellard | ptr = sdata[sym->n_sect-1];
|
1577 | 67b915a5 | bellard | #else
|
1578 | fe319756 | bellard | ptr = sdata[sym->st_shndx]; |
1579 | 67b915a5 | bellard | #endif
|
1580 | fe319756 | bellard | if (!ptr)
|
1581 | fe319756 | bellard | error("__op_labelN in invalid section");
|
1582 | fe319756 | bellard | offset = sym->st_value; |
1583 | 82eec0a1 | bellard | #ifdef CONFIG_FORMAT_MACH
|
1584 | 82eec0a1 | bellard | offset -= section_hdr[sym->n_sect-1].addr;
|
1585 | 82eec0a1 | bellard | #endif
|
1586 | abcd5da7 | bellard | val = *(unsigned long *)(ptr + offset); |
1587 | fe319756 | bellard | #ifdef ELF_USES_RELOCA
|
1588 | fe319756 | bellard | { |
1589 | fe319756 | bellard | int reloc_shndx, nb_relocs1, j;
|
1590 | fe319756 | bellard | |
1591 | fe319756 | bellard | /* try to find a matching relocation */
|
1592 | fe319756 | bellard | reloc_shndx = find_reloc(sym->st_shndx); |
1593 | fe319756 | bellard | if (reloc_shndx) {
|
1594 | fe319756 | bellard | nb_relocs1 = shdr[reloc_shndx].sh_size / |
1595 | fe319756 | bellard | shdr[reloc_shndx].sh_entsize; |
1596 | fe319756 | bellard | rel = (ELF_RELOC *)sdata[reloc_shndx]; |
1597 | fe319756 | bellard | for(j = 0; j < nb_relocs1; j++) { |
1598 | fe319756 | bellard | if (rel->r_offset == offset) {
|
1599 | 039de852 | bellard | val = rel->r_addend; |
1600 | fe319756 | bellard | break;
|
1601 | fe319756 | bellard | } |
1602 | fe319756 | bellard | rel++; |
1603 | fe319756 | bellard | } |
1604 | fe319756 | bellard | } |
1605 | fe319756 | bellard | } |
1606 | fe319756 | bellard | #endif
|
1607 | fe319756 | bellard | |
1608 | d4e8164f | bellard | if (val >= start_offset && val < start_offset + copy_size) {
|
1609 | d4e8164f | bellard | n = strtol(p, NULL, 10); |
1610 | abcd5da7 | bellard | fprintf(outfile, " label_offsets[%d] = %ld + (gen_code_ptr - gen_code_buf);\n", n, val - start_offset);
|
1611 | d4e8164f | bellard | } |
1612 | d4e8164f | bellard | } |
1613 | d4e8164f | bellard | } |
1614 | d4e8164f | bellard | } |
1615 | d4e8164f | bellard | |
1616 | d4e8164f | bellard | /* load parameres in variables */
|
1617 | dc99065b | bellard | for(i = 0; i < nb_args; i++) { |
1618 | dc99065b | bellard | fprintf(outfile, " param%d = *opparam_ptr++;\n", i + 1); |
1619 | dc99065b | bellard | } |
1620 | dc99065b | bellard | |
1621 | dc99065b | bellard | /* patch relocations */
|
1622 | ce11fedc | bellard | #if defined(HOST_I386)
|
1623 | dc99065b | bellard | { |
1624 | dc99065b | bellard | char name[256]; |
1625 | dc99065b | bellard | int type;
|
1626 | ce11fedc | bellard | int addend;
|
1627 | dc99065b | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1628 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1629 | ae228531 | bellard | rel->r_offset < start_offset + copy_size) { |
1630 | 67b915a5 | bellard | sym_name = get_rel_sym_name(rel); |
1631 | ecd854fd | bellard | if (strstart(sym_name, "__op_jmp", &p)) { |
1632 | ecd854fd | bellard | int n;
|
1633 | ecd854fd | bellard | n = strtol(p, NULL, 10); |
1634 | ecd854fd | bellard | /* __op_jmp relocations are done at
|
1635 | ecd854fd | bellard | runtime to do translated block
|
1636 | ecd854fd | bellard | chaining: the offset of the instruction
|
1637 | ecd854fd | bellard | needs to be stored */
|
1638 | ecd854fd | bellard | fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
|
1639 | ecd854fd | bellard | n, rel->r_offset - start_offset); |
1640 | ecd854fd | bellard | continue;
|
1641 | ecd854fd | bellard | } |
1642 | ecd854fd | bellard | |
1643 | 367e86e8 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1644 | 367e86e8 | bellard | snprintf(name, sizeof(name), "param%s", p); |
1645 | 367e86e8 | bellard | } else {
|
1646 | 367e86e8 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1647 | 367e86e8 | bellard | } |
1648 | 367e86e8 | bellard | addend = get32((uint32_t *)(text + rel->r_offset)); |
1649 | 67b915a5 | bellard | #ifdef CONFIG_FORMAT_ELF
|
1650 | 67b915a5 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1651 | 367e86e8 | bellard | switch(type) {
|
1652 | 367e86e8 | bellard | case R_386_32:
|
1653 | ce11fedc | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1654 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1655 | 367e86e8 | bellard | break;
|
1656 | 367e86e8 | bellard | case R_386_PC32:
|
1657 | ce11fedc | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
|
1658 | ae228531 | bellard | rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
1659 | 367e86e8 | bellard | break;
|
1660 | 367e86e8 | bellard | default:
|
1661 | 367e86e8 | bellard | error("unsupported i386 relocation (%d)", type);
|
1662 | 367e86e8 | bellard | } |
1663 | 67b915a5 | bellard | #elif defined(CONFIG_FORMAT_COFF)
|
1664 | 40c3bac3 | bellard | { |
1665 | 40c3bac3 | bellard | char *temp_name;
|
1666 | 40c3bac3 | bellard | int j;
|
1667 | 40c3bac3 | bellard | EXE_SYM *sym; |
1668 | 40c3bac3 | bellard | temp_name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx)); |
1669 | 40c3bac3 | bellard | if (!strcmp(temp_name, ".data")) { |
1670 | 40c3bac3 | bellard | for (j = 0, sym = symtab; j < nb_syms; j++, sym++) { |
1671 | 40c3bac3 | bellard | if (strstart(sym->st_name, sym_name, NULL)) { |
1672 | 40c3bac3 | bellard | addend -= sym->st_value; |
1673 | 40c3bac3 | bellard | } |
1674 | 40c3bac3 | bellard | } |
1675 | 40c3bac3 | bellard | } |
1676 | 40c3bac3 | bellard | } |
1677 | 67b915a5 | bellard | type = rel->r_type; |
1678 | 67b915a5 | bellard | switch(type) {
|
1679 | 67b915a5 | bellard | case DIR32:
|
1680 | 67b915a5 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1681 | 67b915a5 | bellard | rel->r_offset - start_offset, name, addend); |
1682 | 67b915a5 | bellard | break;
|
1683 | 67b915a5 | bellard | case DISP32:
|
1684 | 67b915a5 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n",
|
1685 | 67b915a5 | bellard | rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
1686 | 67b915a5 | bellard | break;
|
1687 | 67b915a5 | bellard | default:
|
1688 | 67b915a5 | bellard | error("unsupported i386 relocation (%d)", type);
|
1689 | 67b915a5 | bellard | } |
1690 | 67b915a5 | bellard | #else
|
1691 | 67b915a5 | bellard | #error unsupport object format
|
1692 | 67b915a5 | bellard | #endif
|
1693 | 367e86e8 | bellard | } |
1694 | dc99065b | bellard | } |
1695 | dc99065b | bellard | } |
1696 | bc51c5c9 | bellard | #elif defined(HOST_AMD64)
|
1697 | bc51c5c9 | bellard | { |
1698 | bc51c5c9 | bellard | char name[256]; |
1699 | bc51c5c9 | bellard | int type;
|
1700 | bc51c5c9 | bellard | int addend;
|
1701 | bc51c5c9 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1702 | bc51c5c9 | bellard | if (rel->r_offset >= start_offset &&
|
1703 | bc51c5c9 | bellard | rel->r_offset < start_offset + copy_size) { |
1704 | bc51c5c9 | bellard | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
1705 | bc51c5c9 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1706 | bc51c5c9 | bellard | snprintf(name, sizeof(name), "param%s", p); |
1707 | bc51c5c9 | bellard | } else {
|
1708 | bc51c5c9 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1709 | bc51c5c9 | bellard | } |
1710 | bc51c5c9 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1711 | bc51c5c9 | bellard | addend = rel->r_addend; |
1712 | bc51c5c9 | bellard | switch(type) {
|
1713 | bc51c5c9 | bellard | case R_X86_64_32:
|
1714 | bc51c5c9 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (uint32_t)%s + %d;\n",
|
1715 | bc51c5c9 | bellard | rel->r_offset - start_offset, name, addend); |
1716 | bc51c5c9 | bellard | break;
|
1717 | bc51c5c9 | bellard | case R_X86_64_32S:
|
1718 | bc51c5c9 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = (int32_t)%s + %d;\n",
|
1719 | bc51c5c9 | bellard | rel->r_offset - start_offset, name, addend); |
1720 | bc51c5c9 | bellard | break;
|
1721 | bc51c5c9 | bellard | case R_X86_64_PC32:
|
1722 | bc51c5c9 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d;\n",
|
1723 | bc51c5c9 | bellard | rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
1724 | bc51c5c9 | bellard | break;
|
1725 | bc51c5c9 | bellard | default:
|
1726 | bc51c5c9 | bellard | error("unsupported AMD64 relocation (%d)", type);
|
1727 | bc51c5c9 | bellard | } |
1728 | bc51c5c9 | bellard | } |
1729 | bc51c5c9 | bellard | } |
1730 | bc51c5c9 | bellard | } |
1731 | ce11fedc | bellard | #elif defined(HOST_PPC)
|
1732 | 04369ff2 | bellard | { |
1733 | 82eec0a1 | bellard | #ifdef CONFIG_FORMAT_ELF
|
1734 | 04369ff2 | bellard | char name[256]; |
1735 | 04369ff2 | bellard | int type;
|
1736 | ce11fedc | bellard | int addend;
|
1737 | 04369ff2 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1738 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1739 | ae228531 | bellard | rel->r_offset < start_offset + copy_size) { |
1740 | efdea7bf | bellard | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
1741 | d4e8164f | bellard | if (strstart(sym_name, "__op_jmp", &p)) { |
1742 | d4e8164f | bellard | int n;
|
1743 | d4e8164f | bellard | n = strtol(p, NULL, 10); |
1744 | d4e8164f | bellard | /* __op_jmp relocations are done at
|
1745 | d4e8164f | bellard | runtime to do translated block
|
1746 | d4e8164f | bellard | chaining: the offset of the instruction
|
1747 | d4e8164f | bellard | needs to be stored */
|
1748 | d4e8164f | bellard | fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
|
1749 | d4e8164f | bellard | n, rel->r_offset - start_offset); |
1750 | d4e8164f | bellard | continue;
|
1751 | d4e8164f | bellard | } |
1752 | d4e8164f | bellard | |
1753 | 04369ff2 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1754 | 04369ff2 | bellard | snprintf(name, sizeof(name), "param%s", p); |
1755 | 04369ff2 | bellard | } else {
|
1756 | 04369ff2 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1757 | 04369ff2 | bellard | } |
1758 | 04369ff2 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1759 | 04369ff2 | bellard | addend = rel->r_addend; |
1760 | 04369ff2 | bellard | switch(type) {
|
1761 | 04369ff2 | bellard | case R_PPC_ADDR32:
|
1762 | ce11fedc | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1763 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1764 | 04369ff2 | bellard | break;
|
1765 | 04369ff2 | bellard | case R_PPC_ADDR16_LO:
|
1766 | ce11fedc | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d);\n",
|
1767 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1768 | 04369ff2 | bellard | break;
|
1769 | 04369ff2 | bellard | case R_PPC_ADDR16_HI:
|
1770 | ce11fedc | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d) >> 16;\n",
|
1771 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1772 | 04369ff2 | bellard | break;
|
1773 | 04369ff2 | bellard | case R_PPC_ADDR16_HA:
|
1774 | ce11fedc | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = (%s + %d + 0x8000) >> 16;\n",
|
1775 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1776 | 04369ff2 | bellard | break;
|
1777 | 04369ff2 | bellard | case R_PPC_REL24:
|
1778 | 04369ff2 | bellard | /* warning: must be at 32 MB distancy */
|
1779 | 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",
|
1780 | ae228531 | bellard | rel->r_offset - start_offset, rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
1781 | 04369ff2 | bellard | break;
|
1782 | 04369ff2 | bellard | default:
|
1783 | 04369ff2 | bellard | error("unsupported powerpc relocation (%d)", type);
|
1784 | 04369ff2 | bellard | } |
1785 | 04369ff2 | bellard | } |
1786 | 04369ff2 | bellard | } |
1787 | 82eec0a1 | bellard | #elif defined(CONFIG_FORMAT_MACH)
|
1788 | 82eec0a1 | bellard | struct scattered_relocation_info *scarel;
|
1789 | 82eec0a1 | bellard | struct relocation_info * rel;
|
1790 | 82eec0a1 | bellard | char final_sym_name[256]; |
1791 | 82eec0a1 | bellard | const char *sym_name; |
1792 | 82eec0a1 | bellard | const char *p; |
1793 | 82eec0a1 | bellard | int slide, sslide;
|
1794 | 82eec0a1 | bellard | int i;
|
1795 | 82eec0a1 | bellard | |
1796 | 82eec0a1 | bellard | for(i = 0, rel = relocs; i < nb_relocs; i++, rel++) { |
1797 | 82eec0a1 | bellard | unsigned int offset, length, value = 0; |
1798 | 82eec0a1 | bellard | unsigned int type, pcrel, isym = 0; |
1799 | 82eec0a1 | bellard | unsigned int usesym = 0; |
1800 | 82eec0a1 | bellard | |
1801 | 82eec0a1 | bellard | if(R_SCATTERED & rel->r_address) {
|
1802 | 82eec0a1 | bellard | scarel = (struct scattered_relocation_info*)rel;
|
1803 | 82eec0a1 | bellard | offset = (unsigned int)scarel->r_address; |
1804 | 82eec0a1 | bellard | length = scarel->r_length; |
1805 | 82eec0a1 | bellard | pcrel = scarel->r_pcrel; |
1806 | 82eec0a1 | bellard | type = scarel->r_type; |
1807 | 82eec0a1 | bellard | value = scarel->r_value; |
1808 | 82eec0a1 | bellard | } else {
|
1809 | 82eec0a1 | bellard | value = isym = rel->r_symbolnum; |
1810 | 82eec0a1 | bellard | usesym = (rel->r_extern); |
1811 | 82eec0a1 | bellard | offset = rel->r_address; |
1812 | 82eec0a1 | bellard | length = rel->r_length; |
1813 | 82eec0a1 | bellard | pcrel = rel->r_pcrel; |
1814 | 82eec0a1 | bellard | type = rel->r_type; |
1815 | 82eec0a1 | bellard | } |
1816 | 82eec0a1 | bellard | |
1817 | 82eec0a1 | bellard | slide = offset - start_offset; |
1818 | 82eec0a1 | bellard | |
1819 | 82eec0a1 | bellard | if (!(offset >= start_offset && offset < start_offset + size))
|
1820 | 82eec0a1 | bellard | continue; /* not in our range */ |
1821 | 82eec0a1 | bellard | |
1822 | 82eec0a1 | bellard | sym_name = get_reloc_name(rel, &sslide); |
1823 | 82eec0a1 | bellard | |
1824 | 82eec0a1 | bellard | if(usesym && symtab[isym].n_type & N_STAB)
|
1825 | 82eec0a1 | bellard | continue; /* don't handle STAB (debug sym) */ |
1826 | 82eec0a1 | bellard | |
1827 | 82eec0a1 | bellard | if (sym_name && strstart(sym_name, "__op_jmp", &p)) { |
1828 | 82eec0a1 | bellard | int n;
|
1829 | 82eec0a1 | bellard | n = strtol(p, NULL, 10); |
1830 | 82eec0a1 | bellard | fprintf(outfile, " jmp_offsets[%d] = %d + (gen_code_ptr - gen_code_buf);\n",
|
1831 | 82eec0a1 | bellard | n, slide); |
1832 | 82eec0a1 | bellard | continue; /* Nothing more to do */ |
1833 | 82eec0a1 | bellard | } |
1834 | 82eec0a1 | bellard | |
1835 | 82eec0a1 | bellard | if(!sym_name)
|
1836 | 82eec0a1 | bellard | { |
1837 | 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",
|
1838 | 82eec0a1 | bellard | name, value, usesym ? "use sym" : "don't use sym", offset, length, pcrel ? "pcrel":"", type); |
1839 | 82eec0a1 | bellard | continue; /* dunno how to handle without final_sym_name */ |
1840 | 82eec0a1 | bellard | } |
1841 | 82eec0a1 | bellard | |
1842 | 82eec0a1 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1843 | 82eec0a1 | bellard | snprintf(final_sym_name, sizeof(final_sym_name), "param%s", p); |
1844 | 82eec0a1 | bellard | } else {
|
1845 | 82eec0a1 | bellard | snprintf(final_sym_name, sizeof(final_sym_name), "(long)(&%s)", sym_name); |
1846 | 82eec0a1 | bellard | } |
1847 | 82eec0a1 | bellard | |
1848 | 82eec0a1 | bellard | switch(type) {
|
1849 | 82eec0a1 | bellard | case PPC_RELOC_BR24:
|
1850 | 82eec0a1 | bellard | fprintf(outfile, "{\n");
|
1851 | 82eec0a1 | bellard | fprintf(outfile, " uint32_t imm = *(uint32_t *)(gen_code_ptr + %d) & 0x3fffffc;\n", slide);
|
1852 | 82eec0a1 | 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",
|
1853 | 82eec0a1 | bellard | slide, slide, name, sslide ); |
1854 | 82eec0a1 | bellard | fprintf(outfile, "}\n");
|
1855 | 82eec0a1 | bellard | break;
|
1856 | 82eec0a1 | bellard | case PPC_RELOC_HI16:
|
1857 | 82eec0a1 | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d) >> 16;\n",
|
1858 | 82eec0a1 | bellard | slide, final_sym_name, sslide); |
1859 | 82eec0a1 | bellard | break;
|
1860 | 82eec0a1 | bellard | case PPC_RELOC_LO16:
|
1861 | 82eec0a1 | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d);\n",
|
1862 | 82eec0a1 | bellard | slide, final_sym_name, sslide); |
1863 | 82eec0a1 | bellard | break;
|
1864 | 82eec0a1 | bellard | case PPC_RELOC_HA16:
|
1865 | 82eec0a1 | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d + 2) = (%s + %d + 0x8000) >> 16;\n",
|
1866 | 82eec0a1 | bellard | slide, final_sym_name, sslide); |
1867 | 82eec0a1 | bellard | break;
|
1868 | 82eec0a1 | bellard | default:
|
1869 | 82eec0a1 | bellard | error("unsupported powerpc relocation (%d)", type);
|
1870 | 82eec0a1 | bellard | } |
1871 | 82eec0a1 | bellard | } |
1872 | 82eec0a1 | bellard | #else
|
1873 | 82eec0a1 | bellard | #error unsupport object format
|
1874 | 82eec0a1 | bellard | #endif
|
1875 | 04369ff2 | bellard | } |
1876 | ce11fedc | bellard | #elif defined(HOST_S390)
|
1877 | fb3e5849 | bellard | { |
1878 | fb3e5849 | bellard | char name[256]; |
1879 | fb3e5849 | bellard | int type;
|
1880 | ce11fedc | bellard | int addend;
|
1881 | fb3e5849 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1882 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1883 | ae228531 | bellard | rel->r_offset < start_offset + copy_size) { |
1884 | efdea7bf | bellard | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
1885 | fb3e5849 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1886 | fb3e5849 | bellard | snprintf(name, sizeof(name), "param%s", p); |
1887 | fb3e5849 | bellard | } else {
|
1888 | fb3e5849 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1889 | fb3e5849 | bellard | } |
1890 | fb3e5849 | bellard | type = ELF32_R_TYPE(rel->r_info); |
1891 | fb3e5849 | bellard | addend = rel->r_addend; |
1892 | fb3e5849 | bellard | switch(type) {
|
1893 | fb3e5849 | bellard | case R_390_32:
|
1894 | ce11fedc | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1895 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1896 | fb3e5849 | bellard | break;
|
1897 | fb3e5849 | bellard | case R_390_16:
|
1898 | ce11fedc | bellard | fprintf(outfile, " *(uint16_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1899 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1900 | fb3e5849 | bellard | break;
|
1901 | fb3e5849 | bellard | case R_390_8:
|
1902 | ce11fedc | bellard | fprintf(outfile, " *(uint8_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
1903 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
1904 | fb3e5849 | bellard | break;
|
1905 | fb3e5849 | bellard | default:
|
1906 | fb3e5849 | bellard | error("unsupported s390 relocation (%d)", type);
|
1907 | fb3e5849 | bellard | } |
1908 | fb3e5849 | bellard | } |
1909 | fb3e5849 | bellard | } |
1910 | fb3e5849 | bellard | } |
1911 | efdea7bf | bellard | #elif defined(HOST_ALPHA)
|
1912 | efdea7bf | bellard | { |
1913 | efdea7bf | bellard | for (i = 0, rel = relocs; i < nb_relocs; i++, rel++) { |
1914 | ae228531 | bellard | if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
|
1915 | efdea7bf | bellard | int type;
|
1916 | 74c95119 | bellard | |
1917 | efdea7bf | bellard | type = ELF64_R_TYPE(rel->r_info); |
1918 | 74c95119 | bellard | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
1919 | efdea7bf | bellard | switch (type) {
|
1920 | efdea7bf | bellard | case R_ALPHA_GPDISP:
|
1921 | 74c95119 | bellard | /* The gp is just 32 bit, and never changes, so it's easiest to emit it
|
1922 | 74c95119 | bellard | as an immediate instead of constructing it from the pv or ra. */
|
1923 | 74c95119 | bellard | fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, gp);\n",
|
1924 | ae228531 | bellard | rel->r_offset - start_offset); |
1925 | 74c95119 | bellard | fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, gp);\n",
|
1926 | ae228531 | bellard | rel->r_offset - start_offset + rel->r_addend); |
1927 | efdea7bf | bellard | break;
|
1928 | efdea7bf | bellard | case R_ALPHA_LITUSE:
|
1929 | efdea7bf | bellard | /* jsr to literal hint. Could be used to optimize to bsr. Ignore for
|
1930 | efdea7bf | bellard | now, since some called functions (libc) need pv to be set up. */
|
1931 | efdea7bf | bellard | break;
|
1932 | efdea7bf | bellard | case R_ALPHA_HINT:
|
1933 | efdea7bf | bellard | /* Branch target prediction hint. Ignore for now. Should be already
|
1934 | efdea7bf | bellard | correct for in-function jumps. */
|
1935 | efdea7bf | bellard | break;
|
1936 | efdea7bf | bellard | case R_ALPHA_LITERAL:
|
1937 | 74c95119 | bellard | /* Load a literal from the GOT relative to the gp. Since there's only a
|
1938 | 74c95119 | bellard | single gp, nothing is to be done. */
|
1939 | 74c95119 | bellard | break;
|
1940 | 74c95119 | bellard | case R_ALPHA_GPRELHIGH:
|
1941 | 74c95119 | bellard | /* Handle fake relocations against __op_param symbol. Need to emit the
|
1942 | 74c95119 | bellard | high part of the immediate value instead. Other symbols need no
|
1943 | 74c95119 | bellard | special treatment. */
|
1944 | 74c95119 | bellard | if (strstart(sym_name, "__op_param", &p)) |
1945 | 74c95119 | bellard | fprintf(outfile, " immediate_ldah(gen_code_ptr + %ld, param%s);\n",
|
1946 | ae228531 | bellard | rel->r_offset - start_offset, p); |
1947 | 74c95119 | bellard | break;
|
1948 | 74c95119 | bellard | case R_ALPHA_GPRELLOW:
|
1949 | 74c95119 | bellard | if (strstart(sym_name, "__op_param", &p)) |
1950 | 74c95119 | bellard | fprintf(outfile, " immediate_lda(gen_code_ptr + %ld, param%s);\n",
|
1951 | ae228531 | bellard | rel->r_offset - start_offset, p); |
1952 | 74c95119 | bellard | break;
|
1953 | 74c95119 | bellard | case R_ALPHA_BRSGP:
|
1954 | 74c95119 | bellard | /* PC-relative jump. Tweak offset to skip the two instructions that try to
|
1955 | 74c95119 | bellard | set up the gp from the pv. */
|
1956 | 2f87c607 | bellard | fprintf(outfile, " fix_bsr(gen_code_ptr + %ld, (uint8_t *) &%s - (gen_code_ptr + %ld + 4) + 8);\n",
|
1957 | ae228531 | bellard | rel->r_offset - start_offset, sym_name, rel->r_offset - start_offset); |
1958 | efdea7bf | bellard | break;
|
1959 | efdea7bf | bellard | default:
|
1960 | efdea7bf | bellard | error("unsupported Alpha relocation (%d)", type);
|
1961 | efdea7bf | bellard | } |
1962 | efdea7bf | bellard | } |
1963 | efdea7bf | bellard | } |
1964 | efdea7bf | bellard | } |
1965 | efdea7bf | bellard | #elif defined(HOST_IA64)
|
1966 | efdea7bf | bellard | { |
1967 | efdea7bf | bellard | char name[256]; |
1968 | efdea7bf | bellard | int type;
|
1969 | efdea7bf | bellard | int addend;
|
1970 | efdea7bf | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1971 | ae228531 | bellard | if (rel->r_offset >= start_offset && rel->r_offset < start_offset + copy_size) {
|
1972 | efdea7bf | bellard | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
1973 | efdea7bf | bellard | if (strstart(sym_name, "__op_param", &p)) { |
1974 | efdea7bf | bellard | snprintf(name, sizeof(name), "param%s", p); |
1975 | efdea7bf | bellard | } else {
|
1976 | efdea7bf | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
1977 | efdea7bf | bellard | } |
1978 | efdea7bf | bellard | type = ELF64_R_TYPE(rel->r_info); |
1979 | efdea7bf | bellard | addend = rel->r_addend; |
1980 | efdea7bf | bellard | switch(type) {
|
1981 | efdea7bf | bellard | case R_IA64_LTOFF22:
|
1982 | efdea7bf | bellard | error("must implemnt R_IA64_LTOFF22 relocation");
|
1983 | efdea7bf | bellard | case R_IA64_PCREL21B:
|
1984 | efdea7bf | bellard | error("must implemnt R_IA64_PCREL21B relocation");
|
1985 | efdea7bf | bellard | default:
|
1986 | efdea7bf | bellard | error("unsupported ia64 relocation (%d)", type);
|
1987 | efdea7bf | bellard | } |
1988 | efdea7bf | bellard | } |
1989 | efdea7bf | bellard | } |
1990 | efdea7bf | bellard | } |
1991 | d014c98c | bellard | #elif defined(HOST_SPARC)
|
1992 | d014c98c | bellard | { |
1993 | d014c98c | bellard | char name[256]; |
1994 | d014c98c | bellard | int type;
|
1995 | d014c98c | bellard | int addend;
|
1996 | d014c98c | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
1997 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
1998 | ae228531 | bellard | rel->r_offset < start_offset + copy_size) { |
1999 | d014c98c | bellard | sym_name = strtab + symtab[ELF32_R_SYM(rel->r_info)].st_name; |
2000 | d014c98c | bellard | if (strstart(sym_name, "__op_param", &p)) { |
2001 | d014c98c | bellard | snprintf(name, sizeof(name), "param%s", p); |
2002 | d014c98c | bellard | } else {
|
2003 | d014c98c | bellard | if (sym_name[0] == '.') |
2004 | d014c98c | bellard | snprintf(name, sizeof(name),
|
2005 | d014c98c | bellard | "(long)(&__dot_%s)",
|
2006 | d014c98c | bellard | sym_name + 1);
|
2007 | d014c98c | bellard | else
|
2008 | d014c98c | bellard | snprintf(name, sizeof(name),
|
2009 | d014c98c | bellard | "(long)(&%s)", sym_name);
|
2010 | d014c98c | bellard | } |
2011 | d014c98c | bellard | type = ELF32_R_TYPE(rel->r_info); |
2012 | d014c98c | bellard | addend = rel->r_addend; |
2013 | d014c98c | bellard | switch(type) {
|
2014 | d014c98c | bellard | case R_SPARC_32:
|
2015 | d014c98c | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
2016 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
2017 | d014c98c | bellard | break;
|
2018 | d014c98c | bellard | case R_SPARC_HI22:
|
2019 | d014c98c | bellard | fprintf(outfile, |
2020 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
2021 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
2022 | d014c98c | bellard | " & ~0x3fffff) "
|
2023 | ae228531 | bellard | " | (((%s + %d) >> 10) & 0x3fffff);\n",
|
2024 | ae228531 | bellard | rel->r_offset - start_offset, |
2025 | ae228531 | bellard | rel->r_offset - start_offset, |
2026 | d014c98c | bellard | name, addend); |
2027 | d014c98c | bellard | break;
|
2028 | d014c98c | bellard | case R_SPARC_LO10:
|
2029 | d014c98c | bellard | fprintf(outfile, |
2030 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
2031 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
2032 | d014c98c | bellard | " & ~0x3ff) "
|
2033 | d014c98c | bellard | " | ((%s + %d) & 0x3ff);\n",
|
2034 | ae228531 | bellard | rel->r_offset - start_offset, |
2035 | ae228531 | bellard | rel->r_offset - start_offset, |
2036 | d014c98c | bellard | name, addend); |
2037 | d014c98c | bellard | break;
|
2038 | d014c98c | bellard | case R_SPARC_WDISP30:
|
2039 | d014c98c | bellard | fprintf(outfile, |
2040 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
2041 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
2042 | d014c98c | bellard | " & ~0x3fffffff) "
|
2043 | ae228531 | bellard | " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
|
2044 | d014c98c | bellard | " & 0x3fffffff);\n",
|
2045 | ae228531 | bellard | rel->r_offset - start_offset, |
2046 | ae228531 | bellard | rel->r_offset - start_offset, |
2047 | ae228531 | bellard | name, addend, |
2048 | ae228531 | bellard | rel->r_offset - start_offset); |
2049 | d014c98c | bellard | break;
|
2050 | d014c98c | bellard | default:
|
2051 | d014c98c | bellard | error("unsupported sparc relocation (%d)", type);
|
2052 | d014c98c | bellard | } |
2053 | d014c98c | bellard | } |
2054 | d014c98c | bellard | } |
2055 | d014c98c | bellard | } |
2056 | d014c98c | bellard | #elif defined(HOST_SPARC64)
|
2057 | d014c98c | bellard | { |
2058 | d014c98c | bellard | char name[256]; |
2059 | d014c98c | bellard | int type;
|
2060 | d014c98c | bellard | int addend;
|
2061 | d014c98c | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
2062 | ae228531 | bellard | if (rel->r_offset >= start_offset &&
|
2063 | ae228531 | bellard | rel->r_offset < start_offset + copy_size) { |
2064 | d014c98c | bellard | sym_name = strtab + symtab[ELF64_R_SYM(rel->r_info)].st_name; |
2065 | d014c98c | bellard | if (strstart(sym_name, "__op_param", &p)) { |
2066 | d014c98c | bellard | snprintf(name, sizeof(name), "param%s", p); |
2067 | d014c98c | bellard | } else {
|
2068 | d014c98c | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
2069 | d014c98c | bellard | } |
2070 | d014c98c | bellard | type = ELF64_R_TYPE(rel->r_info); |
2071 | d014c98c | bellard | addend = rel->r_addend; |
2072 | d014c98c | bellard | switch(type) {
|
2073 | d014c98c | bellard | case R_SPARC_32:
|
2074 | d014c98c | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
2075 | ae228531 | bellard | rel->r_offset - start_offset, name, addend); |
2076 | d014c98c | bellard | break;
|
2077 | d014c98c | bellard | case R_SPARC_HI22:
|
2078 | d014c98c | bellard | fprintf(outfile, |
2079 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
2080 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
2081 | d014c98c | bellard | " & ~0x3fffff) "
|
2082 | ae228531 | bellard | " | (((%s + %d) >> 10) & 0x3fffff);\n",
|
2083 | ae228531 | bellard | rel->r_offset - start_offset, |
2084 | ae228531 | bellard | rel->r_offset - start_offset, |
2085 | d014c98c | bellard | name, addend); |
2086 | d014c98c | bellard | break;
|
2087 | d014c98c | bellard | case R_SPARC_LO10:
|
2088 | d014c98c | bellard | fprintf(outfile, |
2089 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
2090 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
2091 | d014c98c | bellard | " & ~0x3ff) "
|
2092 | d014c98c | bellard | " | ((%s + %d) & 0x3ff);\n",
|
2093 | ae228531 | bellard | rel->r_offset - start_offset, |
2094 | ae228531 | bellard | rel->r_offset - start_offset, |
2095 | d014c98c | bellard | name, addend); |
2096 | d014c98c | bellard | break;
|
2097 | d014c98c | bellard | case R_SPARC_WDISP30:
|
2098 | d014c98c | bellard | fprintf(outfile, |
2099 | d014c98c | bellard | " *(uint32_t *)(gen_code_ptr + %d) = "
|
2100 | d014c98c | bellard | "((*(uint32_t *)(gen_code_ptr + %d)) "
|
2101 | d014c98c | bellard | " & ~0x3fffffff) "
|
2102 | ae228531 | bellard | " | ((((%s + %d) - (long)(gen_code_ptr + %d))>>2) "
|
2103 | d014c98c | bellard | " & 0x3fffffff);\n",
|
2104 | ae228531 | bellard | rel->r_offset - start_offset, |
2105 | ae228531 | bellard | rel->r_offset - start_offset, |
2106 | ae228531 | bellard | name, addend, |
2107 | ae228531 | bellard | rel->r_offset - start_offset); |
2108 | d014c98c | bellard | break;
|
2109 | d014c98c | bellard | default:
|
2110 | d014c98c | bellard | error("unsupported sparc64 relocation (%d)", type);
|
2111 | d014c98c | bellard | } |
2112 | d014c98c | bellard | } |
2113 | d014c98c | bellard | } |
2114 | d014c98c | bellard | } |
2115 | ff1f20a3 | bellard | #elif defined(HOST_ARM)
|
2116 | ff1f20a3 | bellard | { |
2117 | ff1f20a3 | bellard | char name[256]; |
2118 | ff1f20a3 | bellard | int type;
|
2119 | ff1f20a3 | bellard | int addend;
|
2120 | ff1f20a3 | bellard | |
2121 | ff1f20a3 | bellard | arm_emit_ldr_info(name, start_offset, outfile, p_start, p_end, |
2122 | ff1f20a3 | bellard | relocs, nb_relocs); |
2123 | ff1f20a3 | bellard | |
2124 | ff1f20a3 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
2125 | ff1f20a3 | bellard | if (rel->r_offset >= start_offset &&
|
2126 | ff1f20a3 | bellard | rel->r_offset < start_offset + copy_size) { |
2127 | ff1f20a3 | bellard | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
2128 | ff1f20a3 | bellard | /* the compiler leave some unnecessary references to the code */
|
2129 | ff1f20a3 | bellard | if (sym_name[0] == '\0') |
2130 | ff1f20a3 | bellard | continue;
|
2131 | ff1f20a3 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
2132 | ff1f20a3 | bellard | snprintf(name, sizeof(name), "param%s", p); |
2133 | ff1f20a3 | bellard | } else {
|
2134 | ff1f20a3 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
2135 | ff1f20a3 | bellard | } |
2136 | ff1f20a3 | bellard | type = ELF32_R_TYPE(rel->r_info); |
2137 | ff1f20a3 | bellard | addend = get32((uint32_t *)(text + rel->r_offset)); |
2138 | ff1f20a3 | bellard | switch(type) {
|
2139 | ff1f20a3 | bellard | case R_ARM_ABS32:
|
2140 | ff1f20a3 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n",
|
2141 | ff1f20a3 | bellard | rel->r_offset - start_offset, name, addend); |
2142 | ff1f20a3 | bellard | break;
|
2143 | ff1f20a3 | bellard | case R_ARM_PC24:
|
2144 | ff1f20a3 | bellard | fprintf(outfile, " arm_reloc_pc24((uint32_t *)(gen_code_ptr + %d), 0x%x, %s);\n",
|
2145 | ff1f20a3 | bellard | rel->r_offset - start_offset, addend, name); |
2146 | ff1f20a3 | bellard | break;
|
2147 | ff1f20a3 | bellard | default:
|
2148 | ff1f20a3 | bellard | error("unsupported arm relocation (%d)", type);
|
2149 | ff1f20a3 | bellard | } |
2150 | ff1f20a3 | bellard | } |
2151 | ff1f20a3 | bellard | } |
2152 | 38e584a0 | bellard | } |
2153 | 38e584a0 | bellard | #elif defined(HOST_M68K)
|
2154 | 38e584a0 | bellard | { |
2155 | 38e584a0 | bellard | char name[256]; |
2156 | 38e584a0 | bellard | int type;
|
2157 | 38e584a0 | bellard | int addend;
|
2158 | 38e584a0 | bellard | Elf32_Sym *sym; |
2159 | 38e584a0 | bellard | for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
2160 | 38e584a0 | bellard | if (rel->r_offset >= start_offset &&
|
2161 | 38e584a0 | bellard | rel->r_offset < start_offset + copy_size) { |
2162 | 38e584a0 | bellard | sym = &(symtab[ELFW(R_SYM)(rel->r_info)]); |
2163 | 38e584a0 | bellard | sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
2164 | 38e584a0 | bellard | if (strstart(sym_name, "__op_param", &p)) { |
2165 | 38e584a0 | bellard | snprintf(name, sizeof(name), "param%s", p); |
2166 | 38e584a0 | bellard | } else {
|
2167 | 38e584a0 | bellard | snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
2168 | 38e584a0 | bellard | } |
2169 | 38e584a0 | bellard | type = ELF32_R_TYPE(rel->r_info); |
2170 | 38e584a0 | bellard | addend = get32((uint32_t *)(text + rel->r_offset)) + rel->r_addend; |
2171 | 38e584a0 | bellard | switch(type) {
|
2172 | 38e584a0 | bellard | case R_68K_32:
|
2173 | 38e584a0 | bellard | fprintf(outfile, " /* R_68K_32 RELOC, offset %x */\n", rel->r_offset) ;
|
2174 | 38e584a0 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %#x;\n",
|
2175 | 38e584a0 | bellard | rel->r_offset - start_offset, name, addend ); |
2176 | 38e584a0 | bellard | break;
|
2177 | 38e584a0 | bellard | case R_68K_PC32:
|
2178 | 38e584a0 | bellard | fprintf(outfile, " /* R_68K_PC32 RELOC, offset %x */\n", rel->r_offset);
|
2179 | 38e584a0 | bellard | fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %#x) + %#x;\n",
|
2180 | 38e584a0 | bellard | rel->r_offset - start_offset, name, rel->r_offset - start_offset, /*sym->st_value+*/ addend);
|
2181 | 38e584a0 | bellard | break;
|
2182 | 38e584a0 | bellard | default:
|
2183 | 38e584a0 | bellard | error("unsupported m68k relocation (%d)", type);
|
2184 | 38e584a0 | bellard | } |
2185 | 38e584a0 | bellard | } |
2186 | 38e584a0 | bellard | } |
2187 | ff1f20a3 | bellard | } |
2188 | ce11fedc | bellard | #else
|
2189 | ce11fedc | bellard | #error unsupported CPU
|
2190 | ce11fedc | bellard | #endif
|
2191 | dc99065b | bellard | fprintf(outfile, " gen_code_ptr += %d;\n", copy_size);
|
2192 | dc99065b | bellard | fprintf(outfile, "}\n");
|
2193 | dc99065b | bellard | fprintf(outfile, "break;\n\n");
|
2194 | dc99065b | bellard | } else {
|
2195 | dc99065b | bellard | fprintf(outfile, "static inline void gen_%s(", name);
|
2196 | dc99065b | bellard | if (nb_args == 0) { |
2197 | dc99065b | bellard | fprintf(outfile, "void");
|
2198 | dc99065b | bellard | } else {
|
2199 | dc99065b | bellard | for(i = 0; i < nb_args; i++) { |
2200 | dc99065b | bellard | if (i != 0) |
2201 | dc99065b | bellard | fprintf(outfile, ", ");
|
2202 | dc99065b | bellard | fprintf(outfile, "long param%d", i + 1); |
2203 | 367e86e8 | bellard | } |
2204 | 367e86e8 | bellard | } |
2205 | dc99065b | bellard | fprintf(outfile, ")\n");
|
2206 | dc99065b | bellard | fprintf(outfile, "{\n");
|
2207 | dc99065b | bellard | for(i = 0; i < nb_args; i++) { |
2208 | dc99065b | bellard | fprintf(outfile, " *gen_opparam_ptr++ = param%d;\n", i + 1); |
2209 | dc99065b | bellard | } |
2210 | dc99065b | bellard | fprintf(outfile, " *gen_opc_ptr++ = INDEX_%s;\n", name);
|
2211 | dc99065b | bellard | fprintf(outfile, "}\n\n");
|
2212 | 367e86e8 | bellard | } |
2213 | 367e86e8 | bellard | } |
2214 | 367e86e8 | bellard | |
2215 | 67b915a5 | bellard | int gen_file(FILE *outfile, int out_type) |
2216 | 367e86e8 | bellard | { |
2217 | 67b915a5 | bellard | int i;
|
2218 | 67b915a5 | bellard | EXE_SYM *sym; |
2219 | 367e86e8 | bellard | |
2220 | d219f7e7 | bellard | if (out_type == OUT_INDEX_OP) {
|
2221 | a513fe19 | bellard | fprintf(outfile, "DEF(end, 0, 0)\n");
|
2222 | e477b8b8 | bellard | fprintf(outfile, "DEF(nop, 0, 0)\n");
|
2223 | e477b8b8 | bellard | fprintf(outfile, "DEF(nop1, 1, 0)\n");
|
2224 | e477b8b8 | bellard | fprintf(outfile, "DEF(nop2, 2, 0)\n");
|
2225 | e477b8b8 | bellard | fprintf(outfile, "DEF(nop3, 3, 0)\n");
|
2226 | dc99065b | bellard | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
2227 | 82eec0a1 | bellard | const char *name; |
2228 | 67b915a5 | bellard | name = get_sym_name(sym); |
2229 | 82eec0a1 | bellard | if (strstart(name, OP_PREFIX, NULL)) { |
2230 | 67b915a5 | bellard | gen_code(name, sym->st_value, sym->st_size, outfile, 2);
|
2231 | dc99065b | bellard | } |
2232 | dc99065b | bellard | } |
2233 | d219f7e7 | bellard | } else if (out_type == OUT_GEN_OP) { |
2234 | d219f7e7 | bellard | /* generate gen_xxx functions */
|
2235 | d219f7e7 | bellard | |
2236 | d219f7e7 | bellard | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
2237 | d219f7e7 | bellard | const char *name; |
2238 | 67b915a5 | bellard | name = get_sym_name(sym); |
2239 | d219f7e7 | bellard | if (strstart(name, OP_PREFIX, NULL)) { |
2240 | 82eec0a1 | bellard | #if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
|
2241 | 67b915a5 | bellard | if (sym->st_shndx != text_shndx)
|
2242 | d219f7e7 | bellard | error("invalid section for opcode (0x%x)", sym->st_shndx);
|
2243 | 82eec0a1 | bellard | #endif
|
2244 | 67b915a5 | bellard | gen_code(name, sym->st_value, sym->st_size, outfile, 0);
|
2245 | d219f7e7 | bellard | } |
2246 | d219f7e7 | bellard | } |
2247 | d219f7e7 | bellard | |
2248 | dc99065b | bellard | } else {
|
2249 | dc99065b | bellard | /* generate big code generation switch */
|
2250 | dc99065b | bellard | fprintf(outfile, |
2251 | dc99065b | bellard | "int dyngen_code(uint8_t *gen_code_buf,\n"
|
2252 | d4e8164f | bellard | " uint16_t *label_offsets, uint16_t *jmp_offsets,\n"
|
2253 | dc99065b | bellard | " const uint16_t *opc_buf, const uint32_t *opparam_buf)\n"
|
2254 | dc99065b | bellard | "{\n"
|
2255 | dc99065b | bellard | " uint8_t *gen_code_ptr;\n"
|
2256 | dc99065b | bellard | " const uint16_t *opc_ptr;\n"
|
2257 | ff1f20a3 | bellard | " const uint32_t *opparam_ptr;\n");
|
2258 | ff1f20a3 | bellard | |
2259 | ff1f20a3 | bellard | #ifdef HOST_ARM
|
2260 | ff1f20a3 | bellard | fprintf(outfile, |
2261 | ff1f20a3 | bellard | " uint8_t *last_gen_code_ptr = gen_code_buf;\n"
|
2262 | ff1f20a3 | bellard | " LDREntry *arm_ldr_ptr = arm_ldr_table;\n"
|
2263 | ff1f20a3 | bellard | " uint32_t *arm_data_ptr = arm_data_table;\n");
|
2264 | ff1f20a3 | bellard | #endif
|
2265 | ff1f20a3 | bellard | |
2266 | ff1f20a3 | bellard | fprintf(outfile, |
2267 | ff1f20a3 | bellard | "\n"
|
2268 | dc99065b | bellard | " gen_code_ptr = gen_code_buf;\n"
|
2269 | dc99065b | bellard | " opc_ptr = opc_buf;\n"
|
2270 | ae228531 | bellard | " opparam_ptr = opparam_buf;\n");
|
2271 | ae228531 | bellard | |
2272 | ae228531 | bellard | /* Generate prologue, if needed. */
|
2273 | ae228531 | bellard | |
2274 | ae228531 | bellard | fprintf(outfile, |
2275 | dc99065b | bellard | " for(;;) {\n"
|
2276 | dc99065b | bellard | " switch(*opc_ptr++) {\n"
|
2277 | dc99065b | bellard | ); |
2278 | 367e86e8 | bellard | |
2279 | dc99065b | bellard | for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
2280 | dc99065b | bellard | const char *name; |
2281 | 67b915a5 | bellard | name = get_sym_name(sym); |
2282 | dc99065b | bellard | if (strstart(name, OP_PREFIX, NULL)) { |
2283 | 367e86e8 | bellard | #if 0
|
2284 | dc99065b | bellard | printf("%4d: %s pos=0x%08x len=%d\n",
|
2285 | dc99065b | bellard | i, name, sym->st_value, sym->st_size);
|
2286 | 367e86e8 | bellard | #endif
|
2287 | 82eec0a1 | bellard | #if defined(CONFIG_FORMAT_ELF) || defined(CONFIG_FORMAT_COFF)
|
2288 | 67b915a5 | bellard | if (sym->st_shndx != text_shndx)
|
2289 | dc99065b | bellard | error("invalid section for opcode (0x%x)", sym->st_shndx);
|
2290 | 82eec0a1 | bellard | #endif
|
2291 | 67b915a5 | bellard | gen_code(name, sym->st_value, sym->st_size, outfile, 1);
|
2292 | dc99065b | bellard | } |
2293 | dc99065b | bellard | } |
2294 | dc99065b | bellard | |
2295 | dc99065b | bellard | fprintf(outfile, |
2296 | 8ef9a8ec | bellard | " case INDEX_op_nop:\n"
|
2297 | 8ef9a8ec | bellard | " break;\n"
|
2298 | 8ef9a8ec | bellard | " case INDEX_op_nop1:\n"
|
2299 | 8ef9a8ec | bellard | " opparam_ptr++;\n"
|
2300 | 8ef9a8ec | bellard | " break;\n"
|
2301 | 8ef9a8ec | bellard | " case INDEX_op_nop2:\n"
|
2302 | 8ef9a8ec | bellard | " opparam_ptr += 2;\n"
|
2303 | 8ef9a8ec | bellard | " break;\n"
|
2304 | 8ef9a8ec | bellard | " case INDEX_op_nop3:\n"
|
2305 | 8ef9a8ec | bellard | " opparam_ptr += 3;\n"
|
2306 | 8ef9a8ec | bellard | " break;\n"
|
2307 | dc99065b | bellard | " default:\n"
|
2308 | dc99065b | bellard | " goto the_end;\n"
|
2309 | ff1f20a3 | bellard | " }\n");
|
2310 | ff1f20a3 | bellard | |
2311 | ff1f20a3 | bellard | #ifdef HOST_ARM
|
2312 | ff1f20a3 | bellard | /* generate constant table if needed */
|
2313 | ff1f20a3 | bellard | fprintf(outfile, |
2314 | ff1f20a3 | bellard | " if ((gen_code_ptr - last_gen_code_ptr) >= (MAX_FRAG_SIZE - MAX_OP_SIZE)) {\n"
|
2315 | 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"
|
2316 | ff1f20a3 | bellard | " last_gen_code_ptr = gen_code_ptr;\n"
|
2317 | ff1f20a3 | bellard | " arm_ldr_ptr = arm_ldr_table;\n"
|
2318 | ff1f20a3 | bellard | " arm_data_ptr = arm_data_table;\n"
|
2319 | ff1f20a3 | bellard | " }\n");
|
2320 | ff1f20a3 | bellard | #endif
|
2321 | ff1f20a3 | bellard | |
2322 | ff1f20a3 | bellard | |
2323 | ff1f20a3 | bellard | fprintf(outfile, |
2324 | dc99065b | bellard | " }\n"
|
2325 | dc99065b | bellard | " the_end:\n"
|
2326 | dc99065b | bellard | ); |
2327 | dc99065b | bellard | |
2328 | 9621339d | bellard | /* generate some code patching */
|
2329 | 9621339d | bellard | #ifdef HOST_ARM
|
2330 | 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");
|
2331 | 9621339d | bellard | #endif
|
2332 | d219f7e7 | bellard | /* flush instruction cache */
|
2333 | d219f7e7 | bellard | fprintf(outfile, "flush_icache_range((unsigned long)gen_code_buf, (unsigned long)gen_code_ptr);\n");
|
2334 | d219f7e7 | bellard | |
2335 | dc99065b | bellard | fprintf(outfile, "return gen_code_ptr - gen_code_buf;\n");
|
2336 | dc99065b | bellard | fprintf(outfile, "}\n\n");
|
2337 | dc99065b | bellard | |
2338 | 367e86e8 | bellard | } |
2339 | 367e86e8 | bellard | |
2340 | 367e86e8 | bellard | return 0; |
2341 | 367e86e8 | bellard | } |
2342 | 367e86e8 | bellard | |
2343 | 367e86e8 | bellard | void usage(void) |
2344 | 367e86e8 | bellard | { |
2345 | 367e86e8 | bellard | printf("dyngen (c) 2003 Fabrice Bellard\n"
|
2346 | dc99065b | bellard | "usage: dyngen [-o outfile] [-c] objfile\n"
|
2347 | dc99065b | bellard | "Generate a dynamic code generator from an object file\n"
|
2348 | dc99065b | bellard | "-c output enum of operations\n"
|
2349 | d219f7e7 | bellard | "-g output gen_op_xx() functions\n"
|
2350 | dc99065b | bellard | ); |
2351 | 367e86e8 | bellard | exit(1);
|
2352 | 367e86e8 | bellard | } |
2353 | 367e86e8 | bellard | |
2354 | 367e86e8 | bellard | int main(int argc, char **argv) |
2355 | 367e86e8 | bellard | { |
2356 | d219f7e7 | bellard | int c, out_type;
|
2357 | 367e86e8 | bellard | const char *filename, *outfilename; |
2358 | 367e86e8 | bellard | FILE *outfile; |
2359 | 367e86e8 | bellard | |
2360 | 367e86e8 | bellard | outfilename = "out.c";
|
2361 | d219f7e7 | bellard | out_type = OUT_CODE; |
2362 | 367e86e8 | bellard | for(;;) {
|
2363 | d219f7e7 | bellard | c = getopt(argc, argv, "ho:cg");
|
2364 | 367e86e8 | bellard | if (c == -1) |
2365 | 367e86e8 | bellard | break;
|
2366 | 367e86e8 | bellard | switch(c) {
|
2367 | 367e86e8 | bellard | case 'h': |
2368 | 367e86e8 | bellard | usage(); |
2369 | 367e86e8 | bellard | break;
|
2370 | 367e86e8 | bellard | case 'o': |
2371 | 367e86e8 | bellard | outfilename = optarg; |
2372 | 367e86e8 | bellard | break;
|
2373 | dc99065b | bellard | case 'c': |
2374 | d219f7e7 | bellard | out_type = OUT_INDEX_OP; |
2375 | d219f7e7 | bellard | break;
|
2376 | d219f7e7 | bellard | case 'g': |
2377 | d219f7e7 | bellard | out_type = OUT_GEN_OP; |
2378 | dc99065b | bellard | break;
|
2379 | 367e86e8 | bellard | } |
2380 | 367e86e8 | bellard | } |
2381 | 367e86e8 | bellard | if (optind >= argc)
|
2382 | 367e86e8 | bellard | usage(); |
2383 | 367e86e8 | bellard | filename = argv[optind]; |
2384 | 367e86e8 | bellard | outfile = fopen(outfilename, "w");
|
2385 | 367e86e8 | bellard | if (!outfile)
|
2386 | 367e86e8 | bellard | error("could not open '%s'", outfilename);
|
2387 | 67b915a5 | bellard | |
2388 | 67b915a5 | bellard | load_object(filename); |
2389 | 67b915a5 | bellard | gen_file(outfile, out_type); |
2390 | 367e86e8 | bellard | fclose(outfile); |
2391 | 367e86e8 | bellard | return 0; |
2392 | 367e86e8 | bellard | } |