Revision 67b915a5 dyngen.c
b/dyngen.c | ||
---|---|---|
3 | 3 |
* |
4 | 4 |
* Copyright (c) 2003 Fabrice Bellard |
5 | 5 |
* |
6 |
* The COFF object format support was extracted from Kazu's QEMU port |
|
7 |
* to Win32. |
|
8 |
* |
|
6 | 9 |
* This program is free software; you can redistribute it and/or modify |
7 | 10 |
* it under the terms of the GNU General Public License as published by |
8 | 11 |
* the Free Software Foundation; either version 2 of the License, or |
... | ... | |
27 | 30 |
|
28 | 31 |
#include "config-host.h" |
29 | 32 |
|
33 |
#if defined(_WIN32) |
|
34 |
#define CONFIG_FORMAT_COFF |
|
35 |
#else |
|
36 |
#define CONFIG_FORMAT_ELF |
|
37 |
#endif |
|
38 |
|
|
39 |
#ifdef CONFIG_FORMAT_ELF |
|
40 |
|
|
30 | 41 |
/* elf format definitions. We use these macros to test the CPU to |
31 | 42 |
allow cross compilation (this tool must be ran on the build |
32 | 43 |
platform) */ |
... | ... | |
122 | 133 |
#define SHT_RELOC SHT_REL |
123 | 134 |
#endif |
124 | 135 |
|
136 |
#define EXE_RELOC ELF_RELOC |
|
137 |
#define EXE_SYM ElfW(Sym) |
|
138 |
|
|
139 |
#endif /* CONFIG_FORMAT_ELF */ |
|
140 |
|
|
141 |
#ifdef CONFIG_FORMAT_COFF |
|
142 |
|
|
143 |
#include "a.out.h" |
|
144 |
|
|
145 |
typedef int32_t host_long; |
|
146 |
typedef uint32_t host_ulong; |
|
147 |
|
|
148 |
#define FILENAMELEN 256 |
|
149 |
|
|
150 |
typedef struct coff_sym { |
|
151 |
struct external_syment *st_syment; |
|
152 |
char st_name[FILENAMELEN]; |
|
153 |
uint32_t st_value; |
|
154 |
int st_size; |
|
155 |
uint8_t st_type; |
|
156 |
uint8_t st_shndx; |
|
157 |
} coff_Sym; |
|
158 |
|
|
159 |
typedef struct coff_rel { |
|
160 |
struct external_reloc *r_reloc; |
|
161 |
int r_offset; |
|
162 |
uint8_t r_type; |
|
163 |
} coff_Rel; |
|
164 |
|
|
165 |
#define EXE_RELOC struct coff_rel |
|
166 |
#define EXE_SYM struct coff_sym |
|
167 |
|
|
168 |
#endif /* CONFIG_FORMAT_COFF */ |
|
169 |
|
|
125 | 170 |
#include "bswap.h" |
126 | 171 |
|
127 | 172 |
enum { |
... | ... | |
133 | 178 |
/* all dynamically generated functions begin with this code */ |
134 | 179 |
#define OP_PREFIX "op_" |
135 | 180 |
|
136 |
int elf_must_swap(struct elfhdr *h) |
|
181 |
int do_swap; |
|
182 |
|
|
183 |
void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...) |
|
137 | 184 |
{ |
138 |
union { |
|
139 |
uint32_t i; |
|
140 |
uint8_t b[4]; |
|
141 |
} swaptest; |
|
185 |
va_list ap; |
|
186 |
va_start(ap, fmt); |
|
187 |
fprintf(stderr, "dyngen: "); |
|
188 |
vfprintf(stderr, fmt, ap); |
|
189 |
fprintf(stderr, "\n"); |
|
190 |
va_end(ap); |
|
191 |
exit(1); |
|
192 |
} |
|
142 | 193 |
|
143 |
swaptest.i = 1; |
|
144 |
return (h->e_ident[EI_DATA] == ELFDATA2MSB) != |
|
145 |
(swaptest.b[0] == 0); |
|
194 |
void *load_data(int fd, long offset, unsigned int size) |
|
195 |
{ |
|
196 |
char *data; |
|
197 |
|
|
198 |
data = malloc(size); |
|
199 |
if (!data) |
|
200 |
return NULL; |
|
201 |
lseek(fd, offset, SEEK_SET); |
|
202 |
if (read(fd, data, size) != size) { |
|
203 |
free(data); |
|
204 |
return NULL; |
|
205 |
} |
|
206 |
return data; |
|
146 | 207 |
} |
147 |
|
|
208 |
|
|
209 |
int strstart(const char *str, const char *val, const char **ptr) |
|
210 |
{ |
|
211 |
const char *p, *q; |
|
212 |
p = str; |
|
213 |
q = val; |
|
214 |
while (*q != '\0') { |
|
215 |
if (*p != *q) |
|
216 |
return 0; |
|
217 |
p++; |
|
218 |
q++; |
|
219 |
} |
|
220 |
if (ptr) |
|
221 |
*ptr = p; |
|
222 |
return 1; |
|
223 |
} |
|
224 |
|
|
225 |
void pstrcpy(char *buf, int buf_size, const char *str) |
|
226 |
{ |
|
227 |
int c; |
|
228 |
char *q = buf; |
|
229 |
|
|
230 |
if (buf_size <= 0) |
|
231 |
return; |
|
232 |
|
|
233 |
for(;;) { |
|
234 |
c = *str++; |
|
235 |
if (c == 0 || q >= buf + buf_size - 1) |
|
236 |
break; |
|
237 |
*q++ = c; |
|
238 |
} |
|
239 |
*q = '\0'; |
|
240 |
} |
|
241 |
|
|
148 | 242 |
void swab16s(uint16_t *p) |
149 | 243 |
{ |
150 | 244 |
*p = bswap16(*p); |
... | ... | |
160 | 254 |
*p = bswap64(*p); |
161 | 255 |
} |
162 | 256 |
|
257 |
uint16_t get16(uint16_t *p) |
|
258 |
{ |
|
259 |
uint16_t val; |
|
260 |
val = *p; |
|
261 |
if (do_swap) |
|
262 |
val = bswap16(val); |
|
263 |
return val; |
|
264 |
} |
|
265 |
|
|
266 |
uint32_t get32(uint32_t *p) |
|
267 |
{ |
|
268 |
uint32_t val; |
|
269 |
val = *p; |
|
270 |
if (do_swap) |
|
271 |
val = bswap32(val); |
|
272 |
return val; |
|
273 |
} |
|
274 |
|
|
275 |
void put16(uint16_t *p, uint16_t val) |
|
276 |
{ |
|
277 |
if (do_swap) |
|
278 |
val = bswap16(val); |
|
279 |
*p = val; |
|
280 |
} |
|
281 |
|
|
282 |
void put32(uint32_t *p, uint32_t val) |
|
283 |
{ |
|
284 |
if (do_swap) |
|
285 |
val = bswap32(val); |
|
286 |
*p = val; |
|
287 |
} |
|
288 |
|
|
289 |
/* executable information */ |
|
290 |
EXE_SYM *symtab; |
|
291 |
int nb_syms; |
|
292 |
int text_shndx; |
|
293 |
uint8_t *text; |
|
294 |
EXE_RELOC *relocs; |
|
295 |
int nb_relocs; |
|
296 |
|
|
297 |
#ifdef CONFIG_FORMAT_ELF |
|
298 |
|
|
299 |
/* ELF file info */ |
|
300 |
struct elf_shdr *shdr; |
|
301 |
uint8_t **sdata; |
|
302 |
struct elfhdr ehdr; |
|
303 |
char *strtab; |
|
304 |
|
|
305 |
int elf_must_swap(struct elfhdr *h) |
|
306 |
{ |
|
307 |
union { |
|
308 |
uint32_t i; |
|
309 |
uint8_t b[4]; |
|
310 |
} swaptest; |
|
311 |
|
|
312 |
swaptest.i = 1; |
|
313 |
return (h->e_ident[EI_DATA] == ELFDATA2MSB) != |
|
314 |
(swaptest.b[0] == 0); |
|
315 |
} |
|
316 |
|
|
163 | 317 |
void elf_swap_ehdr(struct elfhdr *h) |
164 | 318 |
{ |
165 | 319 |
swab16s(&h->e_type); /* Object file type */ |
... | ... | |
212 | 366 |
#endif |
213 | 367 |
} |
214 | 368 |
|
215 |
/* ELF file info */ |
|
216 |
int do_swap; |
|
217 |
struct elf_shdr *shdr; |
|
218 |
uint8_t **sdata; |
|
219 |
struct elfhdr ehdr; |
|
220 |
ElfW(Sym) *symtab; |
|
221 |
int nb_syms; |
|
222 |
char *strtab; |
|
223 |
int text_shndx; |
|
369 |
struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr, |
|
370 |
const char *name) |
|
371 |
{ |
|
372 |
int i; |
|
373 |
const char *shname; |
|
374 |
struct elf_shdr *sec; |
|
224 | 375 |
|
225 |
uint16_t get16(uint16_t *p) |
|
376 |
for(i = 0; i < shnum; i++) { |
|
377 |
sec = &shdr[i]; |
|
378 |
if (!sec->sh_name) |
|
379 |
continue; |
|
380 |
shname = shstr + sec->sh_name; |
|
381 |
if (!strcmp(shname, name)) |
|
382 |
return sec; |
|
383 |
} |
|
384 |
return NULL; |
|
385 |
} |
|
386 |
|
|
387 |
int find_reloc(int sh_index) |
|
226 | 388 |
{ |
227 |
uint16_t val; |
|
228 |
val = *p; |
|
229 |
if (do_swap) |
|
230 |
val = bswap16(val); |
|
231 |
return val; |
|
389 |
struct elf_shdr *sec; |
|
390 |
int i; |
|
391 |
|
|
392 |
for(i = 0; i < ehdr.e_shnum; i++) { |
|
393 |
sec = &shdr[i]; |
|
394 |
if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index) |
|
395 |
return i; |
|
396 |
} |
|
397 |
return 0; |
|
232 | 398 |
} |
233 | 399 |
|
234 |
uint32_t get32(uint32_t *p)
|
|
400 |
static char *get_rel_sym_name(EXE_RELOC *rel)
|
|
235 | 401 |
{ |
236 |
uint32_t val; |
|
237 |
val = *p; |
|
402 |
return strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name; |
|
403 |
} |
|
404 |
|
|
405 |
static char *get_sym_name(EXE_SYM *sym) |
|
406 |
{ |
|
407 |
return strtab + sym->st_name; |
|
408 |
} |
|
409 |
|
|
410 |
/* load an elf object file */ |
|
411 |
int load_object(const char *filename) |
|
412 |
{ |
|
413 |
int fd; |
|
414 |
struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec; |
|
415 |
int i, j; |
|
416 |
ElfW(Sym) *sym; |
|
417 |
char *shstr; |
|
418 |
ELF_RELOC *rel; |
|
419 |
|
|
420 |
fd = open(filename, O_RDONLY); |
|
421 |
if (fd < 0) |
|
422 |
error("can't open file '%s'", filename); |
|
423 |
|
|
424 |
/* Read ELF header. */ |
|
425 |
if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr)) |
|
426 |
error("unable to read file header"); |
|
427 |
|
|
428 |
/* Check ELF identification. */ |
|
429 |
if (ehdr.e_ident[EI_MAG0] != ELFMAG0 |
|
430 |
|| ehdr.e_ident[EI_MAG1] != ELFMAG1 |
|
431 |
|| ehdr.e_ident[EI_MAG2] != ELFMAG2 |
|
432 |
|| ehdr.e_ident[EI_MAG3] != ELFMAG3 |
|
433 |
|| ehdr.e_ident[EI_VERSION] != EV_CURRENT) { |
|
434 |
error("bad ELF header"); |
|
435 |
} |
|
436 |
|
|
437 |
do_swap = elf_must_swap(&ehdr); |
|
238 | 438 |
if (do_swap) |
239 |
val = bswap32(val); |
|
240 |
return val; |
|
439 |
elf_swap_ehdr(&ehdr); |
|
440 |
if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) |
|
441 |
error("Unsupported ELF class"); |
|
442 |
if (ehdr.e_type != ET_REL) |
|
443 |
error("ELF object file expected"); |
|
444 |
if (ehdr.e_version != EV_CURRENT) |
|
445 |
error("Invalid ELF version"); |
|
446 |
if (!elf_check_arch(ehdr.e_machine)) |
|
447 |
error("Unsupported CPU (e_machine=%d)", ehdr.e_machine); |
|
448 |
|
|
449 |
/* read section headers */ |
|
450 |
shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr)); |
|
451 |
if (do_swap) { |
|
452 |
for(i = 0; i < ehdr.e_shnum; i++) { |
|
453 |
elf_swap_shdr(&shdr[i]); |
|
454 |
} |
|
455 |
} |
|
456 |
|
|
457 |
/* read all section data */ |
|
458 |
sdata = malloc(sizeof(void *) * ehdr.e_shnum); |
|
459 |
memset(sdata, 0, sizeof(void *) * ehdr.e_shnum); |
|
460 |
|
|
461 |
for(i = 0;i < ehdr.e_shnum; i++) { |
|
462 |
sec = &shdr[i]; |
|
463 |
if (sec->sh_type != SHT_NOBITS) |
|
464 |
sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size); |
|
465 |
} |
|
466 |
|
|
467 |
sec = &shdr[ehdr.e_shstrndx]; |
|
468 |
shstr = sdata[ehdr.e_shstrndx]; |
|
469 |
|
|
470 |
/* swap relocations */ |
|
471 |
for(i = 0; i < ehdr.e_shnum; i++) { |
|
472 |
sec = &shdr[i]; |
|
473 |
if (sec->sh_type == SHT_RELOC) { |
|
474 |
nb_relocs = sec->sh_size / sec->sh_entsize; |
|
475 |
if (do_swap) { |
|
476 |
for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++) |
|
477 |
elf_swap_rel(rel); |
|
478 |
} |
|
479 |
} |
|
480 |
} |
|
481 |
/* text section */ |
|
482 |
|
|
483 |
text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text"); |
|
484 |
if (!text_sec) |
|
485 |
error("could not find .text section"); |
|
486 |
text_shndx = text_sec - shdr; |
|
487 |
text = sdata[text_shndx]; |
|
488 |
|
|
489 |
/* find text relocations, if any */ |
|
490 |
relocs = NULL; |
|
491 |
nb_relocs = 0; |
|
492 |
i = find_reloc(text_shndx); |
|
493 |
if (i != 0) { |
|
494 |
relocs = (ELF_RELOC *)sdata[i]; |
|
495 |
nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize; |
|
496 |
} |
|
497 |
|
|
498 |
symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab"); |
|
499 |
if (!symtab_sec) |
|
500 |
error("could not find .symtab section"); |
|
501 |
strtab_sec = &shdr[symtab_sec->sh_link]; |
|
502 |
|
|
503 |
symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr]; |
|
504 |
strtab = sdata[symtab_sec->sh_link]; |
|
505 |
|
|
506 |
nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym)); |
|
507 |
if (do_swap) { |
|
508 |
for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
|
509 |
swab32s(&sym->st_name); |
|
510 |
swabls(&sym->st_value); |
|
511 |
swabls(&sym->st_size); |
|
512 |
swab16s(&sym->st_shndx); |
|
513 |
} |
|
514 |
} |
|
515 |
close(fd); |
|
516 |
return 0; |
|
517 |
} |
|
518 |
|
|
519 |
#endif /* CONFIG_FORMAT_ELF */ |
|
520 |
|
|
521 |
#ifdef CONFIG_FORMAT_COFF |
|
522 |
|
|
523 |
/* COFF file info */ |
|
524 |
struct external_scnhdr *shdr; |
|
525 |
uint8_t **sdata; |
|
526 |
struct external_filehdr fhdr; |
|
527 |
struct external_syment *coff_symtab; |
|
528 |
char *strtab; |
|
529 |
int coff_text_shndx, coff_data_shndx; |
|
530 |
|
|
531 |
int data_shndx; |
|
532 |
|
|
533 |
#define STRTAB_SIZE 4 |
|
534 |
|
|
535 |
#define DIR32 0x06 |
|
536 |
#define DISP32 0x14 |
|
537 |
|
|
538 |
#define T_FUNCTION 0x20 |
|
539 |
#define C_EXTERNAL 2 |
|
540 |
|
|
541 |
void sym_ent_name(struct external_syment *ext_sym, EXE_SYM *sym) |
|
542 |
{ |
|
543 |
char *q; |
|
544 |
int c, i, len; |
|
545 |
|
|
546 |
if (ext_sym->e.e.e_zeroes != 0) { |
|
547 |
q = sym->st_name; |
|
548 |
for(i = 0; i < 8; i++) { |
|
549 |
c = ext_sym->e.e_name[i]; |
|
550 |
if (c == '\0') |
|
551 |
break; |
|
552 |
*q++ = c; |
|
553 |
} |
|
554 |
*q = '\0'; |
|
555 |
} else { |
|
556 |
pstrcpy(sym->st_name, sizeof(sym->st_name), strtab + ext_sym->e.e.e_offset); |
|
557 |
} |
|
558 |
|
|
559 |
/* now convert the name to a C name (suppress the leading '_') */ |
|
560 |
if (sym->st_name[0] == '_') { |
|
561 |
len = strlen(sym->st_name); |
|
562 |
memmove(sym->st_name, sym->st_name + 1, len - 1); |
|
563 |
sym->st_name[len - 1] = '\0'; |
|
564 |
} |
|
241 | 565 |
} |
242 | 566 |
|
243 |
void put16(uint16_t *p, uint16_t val)
|
|
567 |
char *name_for_dotdata(struct coff_rel *rel)
|
|
244 | 568 |
{ |
245 |
if (do_swap) |
|
246 |
val = bswap16(val); |
|
247 |
*p = val; |
|
569 |
int i; |
|
570 |
struct coff_sym *sym; |
|
571 |
uint32_t text_data; |
|
572 |
|
|
573 |
text_data = *(uint32_t *)(text + rel->r_offset); |
|
574 |
|
|
575 |
for (i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
|
576 |
if (sym->st_syment->e_scnum == data_shndx && |
|
577 |
text_data >= sym->st_value && |
|
578 |
text_data < sym->st_value + sym->st_size) { |
|
579 |
|
|
580 |
return sym->st_name; |
|
581 |
|
|
582 |
} |
|
583 |
} |
|
584 |
return NULL; |
|
248 | 585 |
} |
249 | 586 |
|
250 |
void put32(uint32_t *p, uint32_t val)
|
|
587 |
static char *get_sym_name(EXE_SYM *sym)
|
|
251 | 588 |
{ |
252 |
if (do_swap) |
|
253 |
val = bswap32(val); |
|
254 |
*p = val; |
|
589 |
return sym->st_name; |
|
255 | 590 |
} |
256 | 591 |
|
257 |
void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
|
|
592 |
static char *get_rel_sym_name(EXE_RELOC *rel)
|
|
258 | 593 |
{ |
259 |
va_list ap; |
|
260 |
va_start(ap, fmt); |
|
261 |
fprintf(stderr, "dyngen: "); |
|
262 |
vfprintf(stderr, fmt, ap); |
|
263 |
fprintf(stderr, "\n"); |
|
264 |
va_end(ap); |
|
265 |
exit(1); |
|
594 |
char *name; |
|
595 |
name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx)); |
|
596 |
if (!strcmp(name, ".data")) |
|
597 |
name = name_for_dotdata(rel); |
|
598 |
return name; |
|
266 | 599 |
} |
267 | 600 |
|
268 |
|
|
269 |
struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr, |
|
270 |
const char *name) |
|
601 |
struct external_scnhdr *find_coff_section(struct external_scnhdr *shdr, int shnum, const char *name) |
|
271 | 602 |
{ |
272 | 603 |
int i; |
273 | 604 |
const char *shname; |
274 |
struct elf_shdr *sec;
|
|
605 |
struct external_scnhdr *sec;
|
|
275 | 606 |
|
276 | 607 |
for(i = 0; i < shnum; i++) { |
277 | 608 |
sec = &shdr[i]; |
278 |
if (!sec->sh_name)
|
|
609 |
if (!sec->s_name) |
|
279 | 610 |
continue; |
280 |
shname = shstr + sec->sh_name;
|
|
611 |
shname = sec->s_name;
|
|
281 | 612 |
if (!strcmp(shname, name)) |
282 | 613 |
return sec; |
283 | 614 |
} |
284 | 615 |
return NULL; |
285 | 616 |
} |
286 | 617 |
|
287 |
int find_reloc(int sh_index) |
|
618 |
/* load a coff object file */ |
|
619 |
int load_object(const char *filename) |
|
288 | 620 |
{ |
289 |
struct elf_shdr *sec; |
|
621 |
int fd; |
|
622 |
struct external_scnhdr *sec, *text_sec, *data_sec; |
|
290 | 623 |
int i; |
624 |
struct external_syment *ext_sym; |
|
625 |
struct external_reloc *coff_relocs; |
|
626 |
struct external_reloc *ext_rel; |
|
627 |
uint32_t *n_strtab; |
|
628 |
EXE_SYM *sym; |
|
629 |
EXE_RELOC *rel; |
|
630 |
|
|
631 |
fd = open(filename, O_RDONLY |
|
632 |
#ifdef _WIN32 |
|
633 |
| O_BINARY |
|
634 |
#endif |
|
635 |
); |
|
636 |
if (fd < 0) |
|
637 |
error("can't open file '%s'", filename); |
|
638 |
|
|
639 |
/* Read COFF header. */ |
|
640 |
if (read(fd, &fhdr, sizeof (fhdr)) != sizeof (fhdr)) |
|
641 |
error("unable to read file header"); |
|
291 | 642 |
|
292 |
for(i = 0; i < ehdr.e_shnum; i++) { |
|
643 |
/* Check COFF identification. */ |
|
644 |
if (fhdr.f_magic != I386MAGIC) { |
|
645 |
error("bad COFF header"); |
|
646 |
} |
|
647 |
do_swap = 0; |
|
648 |
|
|
649 |
/* read section headers */ |
|
650 |
shdr = load_data(fd, sizeof(struct external_filehdr) + fhdr.f_opthdr, fhdr.f_nscns * sizeof(struct external_scnhdr)); |
|
651 |
|
|
652 |
/* read all section data */ |
|
653 |
sdata = malloc(sizeof(void *) * fhdr.f_nscns); |
|
654 |
memset(sdata, 0, sizeof(void *) * fhdr.f_nscns); |
|
655 |
|
|
656 |
const char *p; |
|
657 |
for(i = 0;i < fhdr.f_nscns; i++) { |
|
293 | 658 |
sec = &shdr[i]; |
294 |
if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
|
|
295 |
return i;
|
|
659 |
if (!strstart(sec->s_name, ".bss", &p))
|
|
660 |
sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size);
|
|
296 | 661 |
} |
297 |
return 0; |
|
298 |
} |
|
299 | 662 |
|
300 |
void *load_data(int fd, long offset, unsigned int size) |
|
301 |
{ |
|
302 |
char *data; |
|
303 | 663 |
|
304 |
data = malloc(size); |
|
305 |
if (!data) |
|
306 |
return NULL; |
|
307 |
lseek(fd, offset, SEEK_SET); |
|
308 |
if (read(fd, data, size) != size) { |
|
309 |
free(data); |
|
310 |
return NULL; |
|
664 |
/* text section */ |
|
665 |
text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text"); |
|
666 |
if (!text_sec) |
|
667 |
error("could not find .text section"); |
|
668 |
coff_text_shndx = text_sec - shdr; |
|
669 |
text = sdata[coff_text_shndx]; |
|
670 |
|
|
671 |
/* data section */ |
|
672 |
data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data"); |
|
673 |
if (!data_sec) |
|
674 |
error("could not find .data section"); |
|
675 |
coff_data_shndx = data_sec - shdr; |
|
676 |
|
|
677 |
coff_symtab = load_data(fd, fhdr.f_symptr, fhdr.f_nsyms*SYMESZ); |
|
678 |
for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) { |
|
679 |
for(i=0;i<8;i++) |
|
680 |
printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]); |
|
681 |
printf("\n"); |
|
311 | 682 |
} |
312 |
return data; |
|
313 |
} |
|
314 | 683 |
|
315 |
int strstart(const char *str, const char *val, const char **ptr) |
|
316 |
{ |
|
317 |
const char *p, *q; |
|
318 |
p = str; |
|
319 |
q = val; |
|
320 |
while (*q != '\0') { |
|
321 |
if (*p != *q) |
|
322 |
return 0; |
|
323 |
p++; |
|
324 |
q++; |
|
684 |
|
|
685 |
n_strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), STRTAB_SIZE); |
|
686 |
strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), *n_strtab); |
|
687 |
|
|
688 |
nb_syms = fhdr.f_nsyms; |
|
689 |
|
|
690 |
for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) { |
|
691 |
if (strstart(ext_sym->e.e_name, ".text", NULL)) |
|
692 |
text_shndx = ext_sym->e_scnum; |
|
693 |
if (strstart(ext_sym->e.e_name, ".data", NULL)) |
|
694 |
data_shndx = ext_sym->e_scnum; |
|
325 | 695 |
} |
326 |
if (ptr) |
|
327 |
*ptr = p; |
|
328 |
return 1; |
|
696 |
|
|
697 |
/* set coff symbol */ |
|
698 |
symtab = malloc(sizeof(struct coff_sym) * nb_syms); |
|
699 |
|
|
700 |
int aux_size, j; |
|
701 |
for (i = 0, ext_sym = coff_symtab, sym = symtab; i < nb_syms; i++, ext_sym++, sym++) { |
|
702 |
memset(sym, 0, sizeof(*sym)); |
|
703 |
sym->st_syment = ext_sym; |
|
704 |
sym_ent_name(ext_sym, sym); |
|
705 |
sym->st_value = ext_sym->e_value; |
|
706 |
|
|
707 |
aux_size = *(int8_t *)ext_sym->e_numaux; |
|
708 |
if (ext_sym->e_scnum == text_shndx && ext_sym->e_type == T_FUNCTION) { |
|
709 |
for (j = aux_size + 1; j < nb_syms - i; j++) { |
|
710 |
if ((ext_sym + j)->e_scnum == text_shndx && |
|
711 |
(ext_sym + j)->e_type == T_FUNCTION ){ |
|
712 |
sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value; |
|
713 |
break; |
|
714 |
} else if (j == nb_syms - i - 1) { |
|
715 |
sec = &shdr[coff_text_shndx]; |
|
716 |
sym->st_size = sec->s_size - ext_sym->e_value; |
|
717 |
break; |
|
718 |
} |
|
719 |
} |
|
720 |
} else if (ext_sym->e_scnum == data_shndx && *(uint8_t *)ext_sym->e_sclass == C_EXTERNAL) { |
|
721 |
for (j = aux_size + 1; j < nb_syms - i; j++) { |
|
722 |
if ((ext_sym + j)->e_scnum == data_shndx) { |
|
723 |
sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value; |
|
724 |
break; |
|
725 |
} else if (j == nb_syms - i - 1) { |
|
726 |
sec = &shdr[coff_data_shndx]; |
|
727 |
sym->st_size = sec->s_size - ext_sym->e_value; |
|
728 |
break; |
|
729 |
} |
|
730 |
} |
|
731 |
} else { |
|
732 |
sym->st_size = 0; |
|
733 |
} |
|
734 |
|
|
735 |
sym->st_type = ext_sym->e_type; |
|
736 |
sym->st_shndx = ext_sym->e_scnum; |
|
737 |
} |
|
738 |
|
|
739 |
|
|
740 |
/* find text relocations, if any */ |
|
741 |
sec = &shdr[coff_text_shndx]; |
|
742 |
coff_relocs = load_data(fd, sec->s_relptr, sec->s_nreloc*RELSZ); |
|
743 |
nb_relocs = sec->s_nreloc; |
|
744 |
|
|
745 |
/* set coff relocation */ |
|
746 |
relocs = malloc(sizeof(struct coff_rel) * nb_relocs); |
|
747 |
for (i = 0, ext_rel = coff_relocs, rel = relocs; i < nb_relocs; |
|
748 |
i++, ext_rel++, rel++) { |
|
749 |
memset(rel, 0, sizeof(*rel)); |
|
750 |
rel->r_reloc = ext_rel; |
|
751 |
rel->r_offset = *(uint32_t *)ext_rel->r_vaddr; |
|
752 |
rel->r_type = *(uint16_t *)ext_rel->r_type; |
|
753 |
} |
|
754 |
return 0; |
|
329 | 755 |
} |
330 | 756 |
|
757 |
#endif /* CONFIG_FORMAT_COFF */ |
|
758 |
|
|
331 | 759 |
#ifdef HOST_ARM |
332 | 760 |
|
333 | 761 |
int arm_emit_ldr_info(const char *name, unsigned long start_offset, |
... | ... | |
385 | 813 |
relname[0] = '\0'; |
386 | 814 |
for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
387 | 815 |
if (rel->r_offset == (pc_offset + start_offset)) { |
388 |
sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
|
|
816 |
sym_name = get_rel_sym_name(rel);
|
|
389 | 817 |
/* the compiler leave some unnecessary references to the code */ |
390 | 818 |
if (strstart(sym_name, "__op_param", &p)) { |
391 | 819 |
snprintf(relname, sizeof(relname), "param%s", p); |
... | ... | |
432 | 860 |
|
433 | 861 |
/* generate op code */ |
434 | 862 |
void gen_code(const char *name, host_ulong offset, host_ulong size, |
435 |
FILE *outfile, uint8_t *text, ELF_RELOC *relocs, int nb_relocs, |
|
436 |
int gen_switch) |
|
863 |
FILE *outfile, int gen_switch) |
|
437 | 864 |
{ |
438 | 865 |
int copy_size = 0; |
439 | 866 |
uint8_t *p_start, *p_end; |
... | ... | |
441 | 868 |
int nb_args, i, n; |
442 | 869 |
uint8_t args_present[MAX_ARGS]; |
443 | 870 |
const char *sym_name, *p; |
444 |
ELF_RELOC *rel;
|
|
871 |
EXE_RELOC *rel;
|
|
445 | 872 |
|
446 | 873 |
/* Compute exact size excluding prologue and epilogue instructions. |
447 | 874 |
* Increment start_offset to skip epilogue instructions, then compute |
... | ... | |
451 | 878 |
p_start = text + offset; |
452 | 879 |
p_end = p_start + size; |
453 | 880 |
start_offset = offset; |
454 |
switch(ELF_ARCH) { |
|
455 |
case EM_386: |
|
456 |
case EM_X86_64: |
|
457 |
{ |
|
458 |
int len; |
|
459 |
len = p_end - p_start; |
|
460 |
if (len == 0) |
|
461 |
error("empty code for %s", name); |
|
462 |
if (p_end[-1] == 0xc3) { |
|
463 |
len--; |
|
464 |
} else { |
|
881 |
#if defined(HOST_I386) || defined(HOST_AMD64) |
|
882 |
#ifdef CONFIG_FORMAT_COFF |
|
883 |
{ |
|
884 |
uint8_t *p; |
|
885 |
p = p_end - 1; |
|
886 |
if (p == p_start) |
|
887 |
error("empty code for %s", name); |
|
888 |
while (*p != 0xc3) { |
|
889 |
p--; |
|
890 |
if (p <= p_start) |
|
465 | 891 |
error("ret or jmp expected at the end of %s", name); |
466 |
} |
|
467 |
copy_size = len; |
|
468 | 892 |
} |
469 |
break; |
|
470 |
case EM_PPC: |
|
471 |
{ |
|
472 |
uint8_t *p; |
|
473 |
p = (void *)(p_end - 4); |
|
474 |
if (p == p_start) |
|
475 |
error("empty code for %s", name); |
|
476 |
if (get32((uint32_t *)p) != 0x4e800020) |
|
477 |
error("blr expected at the end of %s", name); |
|
478 |
copy_size = p - p_start; |
|
893 |
copy_size = p - p_start; |
|
894 |
} |
|
895 |
#else |
|
896 |
{ |
|
897 |
int len; |
|
898 |
len = p_end - p_start; |
|
899 |
if (len == 0) |
|
900 |
error("empty code for %s", name); |
|
901 |
if (p_end[-1] == 0xc3) { |
|
902 |
len--; |
|
903 |
} else { |
|
904 |
error("ret or jmp expected at the end of %s", name); |
|
479 | 905 |
} |
480 |
break; |
|
481 |
case EM_S390: |
|
482 |
{ |
|
483 |
uint8_t *p; |
|
484 |
p = (void *)(p_end - 2); |
|
485 |
if (p == p_start) |
|
486 |
error("empty code for %s", name); |
|
487 |
if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4) |
|
488 |
error("br %%r14 expected at the end of %s", name); |
|
489 |
copy_size = p - p_start; |
|
490 |
} |
|
491 |
break; |
|
492 |
case EM_ALPHA: |
|
493 |
{ |
|
494 |
uint8_t *p; |
|
495 |
p = p_end - 4; |
|
906 |
copy_size = len; |
|
907 |
} |
|
908 |
#endif |
|
909 |
#elif defined(HOST_PPC) |
|
910 |
{ |
|
911 |
uint8_t *p; |
|
912 |
p = (void *)(p_end - 4); |
|
913 |
if (p == p_start) |
|
914 |
error("empty code for %s", name); |
|
915 |
if (get32((uint32_t *)p) != 0x4e800020) |
|
916 |
error("blr expected at the end of %s", name); |
|
917 |
copy_size = p - p_start; |
|
918 |
} |
|
919 |
#elif defined(HOST_S390) |
|
920 |
{ |
|
921 |
uint8_t *p; |
|
922 |
p = (void *)(p_end - 2); |
|
923 |
if (p == p_start) |
|
924 |
error("empty code for %s", name); |
|
925 |
if (get16((uint16_t *)p) != 0x07fe && get16((uint16_t *)p) != 0x07f4) |
|
926 |
error("br %%r14 expected at the end of %s", name); |
|
927 |
copy_size = p - p_start; |
|
928 |
} |
|
929 |
#elif defined(HOST_ALPHA) |
|
930 |
{ |
|
931 |
uint8_t *p; |
|
932 |
p = p_end - 4; |
|
496 | 933 |
#if 0 |
497 |
/* XXX: check why it occurs */
|
|
498 |
if (p == p_start)
|
|
499 |
error("empty code for %s", name);
|
|
934 |
/* XXX: check why it occurs */ |
|
935 |
if (p == p_start)
|
|
936 |
error("empty code for %s", name);
|
|
500 | 937 |
#endif |
501 |
if (get32((uint32_t *)p) != 0x6bfa8001) |
|
502 |
error("ret expected at the end of %s", name); |
|
503 |
copy_size = p - p_start; |
|
504 |
} |
|
505 |
break; |
|
506 |
case EM_IA_64: |
|
507 |
{ |
|
508 |
uint8_t *p; |
|
509 |
p = (void *)(p_end - 4); |
|
510 |
if (p == p_start) |
|
511 |
error("empty code for %s", name); |
|
512 |
/* br.ret.sptk.many b0;; */ |
|
513 |
/* 08 00 84 00 */ |
|
514 |
if (get32((uint32_t *)p) != 0x00840008) |
|
515 |
error("br.ret.sptk.many b0;; expected at the end of %s", name); |
|
516 |
copy_size = p - p_start; |
|
517 |
} |
|
518 |
break; |
|
519 |
case EM_SPARC: |
|
520 |
case EM_SPARC32PLUS: |
|
521 |
{ |
|
522 |
uint32_t start_insn, end_insn1, end_insn2; |
|
523 |
uint8_t *p; |
|
524 |
p = (void *)(p_end - 8); |
|
525 |
if (p <= p_start) |
|
526 |
error("empty code for %s", name); |
|
527 |
start_insn = get32((uint32_t *)(p_start + 0x0)); |
|
528 |
end_insn1 = get32((uint32_t *)(p + 0x0)); |
|
529 |
end_insn2 = get32((uint32_t *)(p + 0x4)); |
|
530 |
if ((start_insn & ~0x1fff) == 0x9de3a000) { |
|
531 |
p_start += 0x4; |
|
532 |
start_offset += 0x4; |
|
533 |
if ((int)(start_insn | ~0x1fff) < -128) |
|
534 |
error("Found bogus save at the start of %s", name); |
|
535 |
if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000) |
|
536 |
error("ret; restore; not found at end of %s", name); |
|
537 |
} else { |
|
538 |
error("No save at the beginning of %s", name); |
|
539 |
} |
|
938 |
if (get32((uint32_t *)p) != 0x6bfa8001) |
|
939 |
error("ret expected at the end of %s", name); |
|
940 |
copy_size = p - p_start; |
|
941 |
} |
|
942 |
#elif defined(HOST_IA64) |
|
943 |
{ |
|
944 |
uint8_t *p; |
|
945 |
p = (void *)(p_end - 4); |
|
946 |
if (p == p_start) |
|
947 |
error("empty code for %s", name); |
|
948 |
/* br.ret.sptk.many b0;; */ |
|
949 |
/* 08 00 84 00 */ |
|
950 |
if (get32((uint32_t *)p) != 0x00840008) |
|
951 |
error("br.ret.sptk.many b0;; expected at the end of %s", name); |
|
952 |
copy_size = p - p_start; |
|
953 |
} |
|
954 |
#elif defined(HOST_SPARC) |
|
955 |
{ |
|
956 |
uint32_t start_insn, end_insn1, end_insn2; |
|
957 |
uint8_t *p; |
|
958 |
p = (void *)(p_end - 8); |
|
959 |
if (p <= p_start) |
|
960 |
error("empty code for %s", name); |
|
961 |
start_insn = get32((uint32_t *)(p_start + 0x0)); |
|
962 |
end_insn1 = get32((uint32_t *)(p + 0x0)); |
|
963 |
end_insn2 = get32((uint32_t *)(p + 0x4)); |
|
964 |
if ((start_insn & ~0x1fff) == 0x9de3a000) { |
|
965 |
p_start += 0x4; |
|
966 |
start_offset += 0x4; |
|
967 |
if ((int)(start_insn | ~0x1fff) < -128) |
|
968 |
error("Found bogus save at the start of %s", name); |
|
969 |
if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000) |
|
970 |
error("ret; restore; not found at end of %s", name); |
|
971 |
} else { |
|
972 |
error("No save at the beginning of %s", name); |
|
973 |
} |
|
540 | 974 |
#if 0 |
541 |
/* Skip a preceeding nop, if present. */
|
|
542 |
if (p > p_start) {
|
|
543 |
skip_insn = get32((uint32_t *)(p - 0x4));
|
|
544 |
if (skip_insn == 0x01000000)
|
|
545 |
p -= 4;
|
|
546 |
}
|
|
975 |
/* Skip a preceeding nop, if present. */
|
|
976 |
if (p > p_start) {
|
|
977 |
skip_insn = get32((uint32_t *)(p - 0x4));
|
|
978 |
if (skip_insn == 0x01000000)
|
|
979 |
p -= 4;
|
|
980 |
}
|
|
547 | 981 |
#endif |
548 |
copy_size = p - p_start; |
|
549 |
} |
|
550 |
break; |
|
551 |
case EM_SPARCV9: |
|
552 |
{ |
|
553 |
uint32_t start_insn, end_insn1, end_insn2, skip_insn; |
|
554 |
uint8_t *p; |
|
555 |
p = (void *)(p_end - 8); |
|
556 |
if (p <= p_start) |
|
557 |
error("empty code for %s", name); |
|
558 |
start_insn = get32((uint32_t *)(p_start + 0x0)); |
|
559 |
end_insn1 = get32((uint32_t *)(p + 0x0)); |
|
560 |
end_insn2 = get32((uint32_t *)(p + 0x4)); |
|
561 |
if ((start_insn & ~0x1fff) == 0x9de3a000) { |
|
562 |
p_start += 0x4; |
|
563 |
start_offset += 0x4; |
|
564 |
if ((int)(start_insn | ~0x1fff) < -256) |
|
565 |
error("Found bogus save at the start of %s", name); |
|
566 |
if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000) |
|
567 |
error("ret; restore; not found at end of %s", name); |
|
568 |
} else { |
|
569 |
error("No save at the beginning of %s", name); |
|
570 |
} |
|
571 |
|
|
572 |
/* Skip a preceeding nop, if present. */ |
|
573 |
if (p > p_start) { |
|
574 |
skip_insn = get32((uint32_t *)(p - 0x4)); |
|
575 |
if (skip_insn == 0x01000000) |
|
576 |
p -= 4; |
|
577 |
} |
|
578 |
|
|
579 |
copy_size = p - p_start; |
|
580 |
} |
|
581 |
break; |
|
582 |
#ifdef HOST_ARM |
|
583 |
case EM_ARM: |
|
982 |
copy_size = p - p_start; |
|
983 |
} |
|
984 |
#elif defined(HOST_SPARC64) |
|
985 |
{ |
|
986 |
uint32_t start_insn, end_insn1, end_insn2, skip_insn; |
|
987 |
uint8_t *p; |
|
988 |
p = (void *)(p_end - 8); |
|
989 |
if (p <= p_start) |
|
990 |
error("empty code for %s", name); |
|
991 |
start_insn = get32((uint32_t *)(p_start + 0x0)); |
|
992 |
end_insn1 = get32((uint32_t *)(p + 0x0)); |
|
993 |
end_insn2 = get32((uint32_t *)(p + 0x4)); |
|
994 |
if ((start_insn & ~0x1fff) == 0x9de3a000) { |
|
995 |
p_start += 0x4; |
|
996 |
start_offset += 0x4; |
|
997 |
if ((int)(start_insn | ~0x1fff) < -256) |
|
998 |
error("Found bogus save at the start of %s", name); |
|
999 |
if (end_insn1 != 0x81c7e008 || end_insn2 != 0x81e80000) |
|
1000 |
error("ret; restore; not found at end of %s", name); |
|
1001 |
} else { |
|
1002 |
error("No save at the beginning of %s", name); |
|
1003 |
} |
|
1004 |
|
|
1005 |
/* Skip a preceeding nop, if present. */ |
|
1006 |
if (p > p_start) { |
|
1007 |
skip_insn = get32((uint32_t *)(p - 0x4)); |
|
1008 |
if (skip_insn == 0x01000000) |
|
1009 |
p -= 4; |
|
1010 |
} |
|
1011 |
|
|
1012 |
copy_size = p - p_start; |
|
1013 |
} |
|
1014 |
#elif defined(HOST_ARM) |
|
1015 |
{ |
|
584 | 1016 |
if ((p_end - p_start) <= 16) |
585 | 1017 |
error("%s: function too small", name); |
586 | 1018 |
if (get32((uint32_t *)p_start) != 0xe1a0c00d || |
... | ... | |
591 | 1023 |
start_offset += 12; |
592 | 1024 |
copy_size = arm_emit_ldr_info(name, start_offset, NULL, p_start, p_end, |
593 | 1025 |
relocs, nb_relocs); |
594 |
break; |
|
595 |
#endif |
|
596 |
case EM_68K: |
|
597 |
{ |
|
598 |
uint8_t *p; |
|
599 |
p = (void *)(p_end - 2); |
|
600 |
if (p == p_start) |
|
601 |
error("empty code for %s", name); |
|
602 |
// remove NOP's, probably added for alignment |
|
603 |
while ((get16((uint16_t *)p) == 0x4e71) && |
|
604 |
(p>p_start)) |
|
605 |
p -= 2; |
|
606 |
if (get16((uint16_t *)p) != 0x4e75) |
|
607 |
error("rts expected at the end of %s", name); |
|
608 |
copy_size = p - p_start; |
|
609 |
} |
|
610 |
break; |
|
611 |
default: |
|
612 |
error("unknown ELF architecture"); |
|
613 | 1026 |
} |
1027 |
#elif defined(HOST_M68K) |
|
1028 |
{ |
|
1029 |
uint8_t *p; |
|
1030 |
p = (void *)(p_end - 2); |
|
1031 |
if (p == p_start) |
|
1032 |
error("empty code for %s", name); |
|
1033 |
// remove NOP's, probably added for alignment |
|
1034 |
while ((get16((uint16_t *)p) == 0x4e71) && |
|
1035 |
(p>p_start)) |
|
1036 |
p -= 2; |
|
1037 |
if (get16((uint16_t *)p) != 0x4e75) |
|
1038 |
error("rts expected at the end of %s", name); |
|
1039 |
copy_size = p - p_start; |
|
1040 |
} |
|
1041 |
#else |
|
1042 |
#error unsupported CPU |
|
1043 |
#endif |
|
614 | 1044 |
|
615 | 1045 |
/* compute the number of arguments by looking at the relocations */ |
616 | 1046 |
for(i = 0;i < MAX_ARGS; i++) |
... | ... | |
619 | 1049 |
for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
620 | 1050 |
if (rel->r_offset >= start_offset && |
621 | 1051 |
rel->r_offset < start_offset + (p_end - p_start)) { |
622 |
sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
|
|
1052 |
sym_name = get_rel_sym_name(rel);
|
|
623 | 1053 |
if (strstart(sym_name, "__op_param", &p)) { |
624 | 1054 |
n = strtoul(p, NULL, 10); |
625 | 1055 |
if (n > MAX_ARGS) |
... | ... | |
657 | 1087 |
for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
658 | 1088 |
if (rel->r_offset >= start_offset && |
659 | 1089 |
rel->r_offset < start_offset + (p_end - p_start)) { |
660 |
sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
|
|
1090 |
sym_name = get_rel_sym_name(rel);
|
|
661 | 1091 |
if (*sym_name && |
662 | 1092 |
!strstart(sym_name, "__op_param", NULL) && |
663 | 1093 |
!strstart(sym_name, "__op_jmp", NULL)) { |
... | ... | |
678 | 1108 |
|
679 | 1109 |
/* emit code offset information */ |
680 | 1110 |
{ |
681 |
ElfW(Sym) *sym;
|
|
1111 |
EXE_SYM *sym;
|
|
682 | 1112 |
const char *sym_name, *p; |
683 | 1113 |
unsigned long val; |
684 | 1114 |
int n; |
685 | 1115 |
|
686 | 1116 |
for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
687 |
sym_name = strtab + sym->st_name;
|
|
1117 |
sym_name = get_sym_name(sym);
|
|
688 | 1118 |
if (strstart(sym_name, "__op_label", &p)) { |
689 | 1119 |
uint8_t *ptr; |
690 | 1120 |
unsigned long offset; |
691 | 1121 |
|
692 | 1122 |
/* test if the variable refers to a label inside |
693 | 1123 |
the code we are generating */ |
1124 |
#ifdef CONFIG_FORMAT_COFF |
|
1125 |
if (sym->st_shndx == text_shndx) { |
|
1126 |
ptr = sdata[coff_text_shndx]; |
|
1127 |
} else if (sym->st_shndx == data_shndx) { |
|
1128 |
ptr = sdata[coff_data_shndx]; |
|
1129 |
} else { |
|
1130 |
ptr = NULL; |
|
1131 |
} |
|
1132 |
#else |
|
694 | 1133 |
ptr = sdata[sym->st_shndx]; |
1134 |
#endif |
|
695 | 1135 |
if (!ptr) |
696 | 1136 |
error("__op_labelN in invalid section"); |
697 | 1137 |
offset = sym->st_value; |
... | ... | |
739 | 1179 |
for(i = 0, rel = relocs;i < nb_relocs; i++, rel++) { |
740 | 1180 |
if (rel->r_offset >= start_offset && |
741 | 1181 |
rel->r_offset < start_offset + copy_size) { |
742 |
sym_name = strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
|
|
1182 |
sym_name = get_rel_sym_name(rel);
|
|
743 | 1183 |
if (strstart(sym_name, "__op_jmp", &p)) { |
744 | 1184 |
int n; |
745 | 1185 |
n = strtol(p, NULL, 10); |
... | ... | |
757 | 1197 |
} else { |
758 | 1198 |
snprintf(name, sizeof(name), "(long)(&%s)", sym_name); |
759 | 1199 |
} |
760 |
type = ELF32_R_TYPE(rel->r_info); |
|
761 | 1200 |
addend = get32((uint32_t *)(text + rel->r_offset)); |
1201 |
#ifdef CONFIG_FORMAT_ELF |
|
1202 |
type = ELF32_R_TYPE(rel->r_info); |
|
762 | 1203 |
switch(type) { |
763 | 1204 |
case R_386_32: |
764 | 1205 |
fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", |
... | ... | |
771 | 1212 |
default: |
772 | 1213 |
error("unsupported i386 relocation (%d)", type); |
773 | 1214 |
} |
1215 |
#elif defined(CONFIG_FORMAT_COFF) |
|
1216 |
type = rel->r_type; |
|
1217 |
switch(type) { |
|
1218 |
case DIR32: |
|
1219 |
fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s + %d;\n", |
|
1220 |
rel->r_offset - start_offset, name, addend); |
|
1221 |
break; |
|
1222 |
case DISP32: |
|
1223 |
fprintf(outfile, " *(uint32_t *)(gen_code_ptr + %d) = %s - (long)(gen_code_ptr + %d) + %d -4;\n", |
|
1224 |
rel->r_offset - start_offset, name, rel->r_offset - start_offset, addend); |
|
1225 |
break; |
|
1226 |
default: |
|
1227 |
error("unsupported i386 relocation (%d)", type); |
|
1228 |
} |
|
1229 |
#else |
|
1230 |
#error unsupport object format |
|
1231 |
#endif |
|
774 | 1232 |
} |
775 | 1233 |
} |
776 | 1234 |
} |
... | ... | |
1204 | 1662 |
} |
1205 | 1663 |
} |
1206 | 1664 |
|
1207 |
/* load an elf object file */ |
|
1208 |
int load_elf(const char *filename, FILE *outfile, int out_type) |
|
1665 |
int gen_file(FILE *outfile, int out_type) |
|
1209 | 1666 |
{ |
1210 |
int fd; |
|
1211 |
struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec; |
|
1212 |
int i, j; |
|
1213 |
ElfW(Sym) *sym; |
|
1214 |
char *shstr; |
|
1215 |
uint8_t *text; |
|
1216 |
ELF_RELOC *relocs; |
|
1217 |
int nb_relocs; |
|
1218 |
ELF_RELOC *rel; |
|
1219 |
|
|
1220 |
fd = open(filename, O_RDONLY); |
|
1221 |
if (fd < 0) |
|
1222 |
error("can't open file '%s'", filename); |
|
1223 |
|
|
1224 |
/* Read ELF header. */ |
|
1225 |
if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr)) |
|
1226 |
error("unable to read file header"); |
|
1227 |
|
|
1228 |
/* Check ELF identification. */ |
|
1229 |
if (ehdr.e_ident[EI_MAG0] != ELFMAG0 |
|
1230 |
|| ehdr.e_ident[EI_MAG1] != ELFMAG1 |
|
1231 |
|| ehdr.e_ident[EI_MAG2] != ELFMAG2 |
|
1232 |
|| ehdr.e_ident[EI_MAG3] != ELFMAG3 |
|
1233 |
|| ehdr.e_ident[EI_VERSION] != EV_CURRENT) { |
|
1234 |
error("bad ELF header"); |
|
1235 |
} |
|
1236 |
|
|
1237 |
do_swap = elf_must_swap(&ehdr); |
|
1238 |
if (do_swap) |
|
1239 |
elf_swap_ehdr(&ehdr); |
|
1240 |
if (ehdr.e_ident[EI_CLASS] != ELF_CLASS) |
|
1241 |
error("Unsupported ELF class"); |
|
1242 |
if (ehdr.e_type != ET_REL) |
|
1243 |
error("ELF object file expected"); |
|
1244 |
if (ehdr.e_version != EV_CURRENT) |
|
1245 |
error("Invalid ELF version"); |
|
1246 |
if (!elf_check_arch(ehdr.e_machine)) |
|
1247 |
error("Unsupported CPU (e_machine=%d)", ehdr.e_machine); |
|
1248 |
|
|
1249 |
/* read section headers */ |
|
1250 |
shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr)); |
|
1251 |
if (do_swap) { |
|
1252 |
for(i = 0; i < ehdr.e_shnum; i++) { |
|
1253 |
elf_swap_shdr(&shdr[i]); |
|
1254 |
} |
|
1255 |
} |
|
1256 |
|
|
1257 |
/* read all section data */ |
|
1258 |
sdata = malloc(sizeof(void *) * ehdr.e_shnum); |
|
1259 |
memset(sdata, 0, sizeof(void *) * ehdr.e_shnum); |
|
1260 |
|
|
1261 |
for(i = 0;i < ehdr.e_shnum; i++) { |
|
1262 |
sec = &shdr[i]; |
|
1263 |
if (sec->sh_type != SHT_NOBITS) |
|
1264 |
sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size); |
|
1265 |
} |
|
1266 |
|
|
1267 |
sec = &shdr[ehdr.e_shstrndx]; |
|
1268 |
shstr = sdata[ehdr.e_shstrndx]; |
|
1269 |
|
|
1270 |
/* swap relocations */ |
|
1271 |
for(i = 0; i < ehdr.e_shnum; i++) { |
|
1272 |
sec = &shdr[i]; |
|
1273 |
if (sec->sh_type == SHT_RELOC) { |
|
1274 |
nb_relocs = sec->sh_size / sec->sh_entsize; |
|
1275 |
if (do_swap) { |
|
1276 |
for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++) |
|
1277 |
elf_swap_rel(rel); |
|
1278 |
} |
|
1279 |
} |
|
1280 |
} |
|
1281 |
/* text section */ |
|
1282 |
|
|
1283 |
text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text"); |
|
1284 |
if (!text_sec) |
|
1285 |
error("could not find .text section"); |
|
1286 |
text_shndx = text_sec - shdr; |
|
1287 |
text = sdata[text_shndx]; |
|
1288 |
|
|
1289 |
/* find text relocations, if any */ |
|
1290 |
relocs = NULL; |
|
1291 |
nb_relocs = 0; |
|
1292 |
i = find_reloc(text_shndx); |
|
1293 |
if (i != 0) { |
|
1294 |
relocs = (ELF_RELOC *)sdata[i]; |
|
1295 |
nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize; |
|
1296 |
} |
|
1297 |
|
|
1298 |
symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab"); |
|
1299 |
if (!symtab_sec) |
|
1300 |
error("could not find .symtab section"); |
|
1301 |
strtab_sec = &shdr[symtab_sec->sh_link]; |
|
1302 |
|
|
1303 |
symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr]; |
|
1304 |
strtab = sdata[symtab_sec->sh_link]; |
|
1305 |
|
|
1306 |
nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym)); |
|
1307 |
if (do_swap) { |
|
1308 |
for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
|
1309 |
swab32s(&sym->st_name); |
|
1310 |
swabls(&sym->st_value); |
|
1311 |
swabls(&sym->st_size); |
|
1312 |
swab16s(&sym->st_shndx); |
|
1313 |
} |
|
1314 |
} |
|
1667 |
int i; |
|
1668 |
EXE_SYM *sym; |
|
1315 | 1669 |
|
1316 | 1670 |
if (out_type == OUT_INDEX_OP) { |
1317 | 1671 |
fprintf(outfile, "DEF(end, 0, 0)\n"); |
... | ... | |
1321 | 1675 |
fprintf(outfile, "DEF(nop3, 3, 0)\n"); |
1322 | 1676 |
for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
1323 | 1677 |
const char *name, *p; |
1324 |
name = strtab + sym->st_name;
|
|
1678 |
name = get_sym_name(sym);
|
|
1325 | 1679 |
if (strstart(name, OP_PREFIX, &p)) { |
1326 |
gen_code(name, sym->st_value, sym->st_size, outfile, |
|
1327 |
text, relocs, nb_relocs, 2); |
|
1680 |
gen_code(name, sym->st_value, sym->st_size, outfile, 2); |
|
1328 | 1681 |
} |
1329 | 1682 |
} |
1330 | 1683 |
} else if (out_type == OUT_GEN_OP) { |
... | ... | |
1332 | 1685 |
|
1333 | 1686 |
for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
1334 | 1687 |
const char *name; |
1335 |
name = strtab + sym->st_name;
|
|
1688 |
name = get_sym_name(sym);
|
|
1336 | 1689 |
if (strstart(name, OP_PREFIX, NULL)) { |
1337 |
if (sym->st_shndx != (text_sec - shdr))
|
|
1690 |
if (sym->st_shndx != text_shndx)
|
|
1338 | 1691 |
error("invalid section for opcode (0x%x)", sym->st_shndx); |
1339 |
gen_code(name, sym->st_value, sym->st_size, outfile, |
|
1340 |
text, relocs, nb_relocs, 0); |
|
1692 |
gen_code(name, sym->st_value, sym->st_size, outfile, 0); |
|
1341 | 1693 |
} |
1342 | 1694 |
} |
1343 | 1695 |
|
... | ... | |
1374 | 1726 |
|
1375 | 1727 |
for(i = 0, sym = symtab; i < nb_syms; i++, sym++) { |
1376 | 1728 |
const char *name; |
1377 |
name = strtab + sym->st_name;
|
|
1729 |
name = get_sym_name(sym);
|
|
1378 | 1730 |
if (strstart(name, OP_PREFIX, NULL)) { |
1379 | 1731 |
#if 0 |
1380 | 1732 |
printf("%4d: %s pos=0x%08x len=%d\n", |
1381 | 1733 |
i, name, sym->st_value, sym->st_size); |
1382 | 1734 |
#endif |
1383 |
if (sym->st_shndx != (text_sec - shdr))
|
|
1735 |
if (sym->st_shndx != text_shndx)
|
|
1384 | 1736 |
error("invalid section for opcode (0x%x)", sym->st_shndx); |
1385 |
gen_code(name, sym->st_value, sym->st_size, outfile, |
|
1386 |
text, relocs, nb_relocs, 1); |
|
1737 |
gen_code(name, sym->st_value, sym->st_size, outfile, 1); |
|
1387 | 1738 |
} |
1388 | 1739 |
} |
1389 | 1740 |
|
... | ... | |
1432 | 1783 |
|
1433 | 1784 |
} |
1434 | 1785 |
|
1435 |
close(fd); |
|
1436 | 1786 |
return 0; |
1437 | 1787 |
} |
1438 | 1788 |
|
... | ... | |
1480 | 1830 |
outfile = fopen(outfilename, "w"); |
1481 | 1831 |
if (!outfile) |
1482 | 1832 |
error("could not open '%s'", outfilename); |
1483 |
load_elf(filename, outfile, out_type); |
|
1833 |
|
|
1834 |
load_object(filename); |
|
1835 |
gen_file(outfile, out_type); |
|
1484 | 1836 |
fclose(outfile); |
1485 | 1837 |
return 0; |
1486 | 1838 |
} |
Also available in: Unified diff