forked from ntddk/temu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyngen.c
3669 lines (3318 loc) · 118 KB
/
dyngen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Generic Dynamic compiler generator
*
* Copyright (c) 2003 Fabrice Bellard
*
* The COFF object format support was extracted from Kazu's QEMU port
* to Win32.
*
* Mach-O Support by Matt Reda and Pierre d'Herbemont
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <inttypes.h>
#include <unistd.h>
#include <fcntl.h>
#include "config-host.h"
//#define DEBUG_OP
/* NOTE: we test CONFIG_WIN32 instead of _WIN32 to enabled cross
compilation */
#if defined(CONFIG_WIN32)
#define CONFIG_FORMAT_COFF
#elif defined(CONFIG_DARWIN)
#define CONFIG_FORMAT_MACH
#else
#define CONFIG_FORMAT_ELF
#endif
#ifdef CONFIG_FORMAT_ELF
/* elf format definitions. We use these macros to test the CPU to
allow cross compilation (this tool must be ran on the build
platform) */
#if defined(HOST_I386)
#define ELF_CLASS ELFCLASS32
#define ELF_ARCH EM_386
#define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
#undef ELF_USES_RELOCA
#elif defined(HOST_X86_64)
#define ELF_CLASS ELFCLASS64
#define ELF_ARCH EM_X86_64
#define elf_check_arch(x) ((x) == EM_X86_64)
#define ELF_USES_RELOCA
#elif defined(HOST_PPC)
#define ELF_CLASS ELFCLASS32
#define ELF_ARCH EM_PPC
#define elf_check_arch(x) ((x) == EM_PPC)
#define ELF_USES_RELOCA
#elif defined(HOST_S390)
#define ELF_CLASS ELFCLASS32
#define ELF_ARCH EM_S390
#define elf_check_arch(x) ((x) == EM_S390)
#define ELF_USES_RELOCA
#elif defined(HOST_ALPHA)
#define ELF_CLASS ELFCLASS64
#define ELF_ARCH EM_ALPHA
#define elf_check_arch(x) ((x) == EM_ALPHA)
#define ELF_USES_RELOCA
#elif defined(HOST_IA64)
#define ELF_CLASS ELFCLASS64
#define ELF_ARCH EM_IA_64
#define elf_check_arch(x) ((x) == EM_IA_64)
#define ELF_USES_RELOCA
#elif defined(HOST_SPARC)
#define ELF_CLASS ELFCLASS32
#define ELF_ARCH EM_SPARC
#define elf_check_arch(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
#define ELF_USES_RELOCA
#elif defined(HOST_SPARC64)
#define ELF_CLASS ELFCLASS64
#define ELF_ARCH EM_SPARCV9
#define elf_check_arch(x) ((x) == EM_SPARCV9)
#define ELF_USES_RELOCA
#elif defined(HOST_ARM)
#define ELF_CLASS ELFCLASS32
#define ELF_ARCH EM_ARM
#define elf_check_arch(x) ((x) == EM_ARM)
#define ELF_USES_RELOC
#elif defined(HOST_M68K)
#define ELF_CLASS ELFCLASS32
#define ELF_ARCH EM_68K
#define elf_check_arch(x) ((x) == EM_68K)
#define ELF_USES_RELOCA
#elif defined(HOST_MIPS)
#define ELF_CLASS ELFCLASS32
#define ELF_ARCH EM_MIPS
#define elf_check_arch(x) ((x) == EM_MIPS)
#define ELF_USES_RELOC
#elif defined(HOST_MIPS64)
/* Assume n32 ABI here, which is ELF32. */
#define ELF_CLASS ELFCLASS32
#define ELF_ARCH EM_MIPS
#define elf_check_arch(x) ((x) == EM_MIPS)
#define ELF_USES_RELOCA
#else
#error unsupported CPU - please update the code
#endif
#include "elf.h"
#if ELF_CLASS == ELFCLASS32
typedef int32_t host_long;
typedef uint32_t host_ulong;
#define swabls(x) swab32s(x)
#define swablss(x) swab32ss(x)
#else
typedef int64_t host_long;
typedef uint64_t host_ulong;
#define swabls(x) swab64s(x)
#define swablss(x) swab64ss(x)
#endif
#ifdef ELF_USES_RELOCA
#define SHT_RELOC SHT_RELA
#else
#define SHT_RELOC SHT_REL
#endif
#define EXE_RELOC ELF_RELOC
#define EXE_SYM ElfW(Sym)
#endif /* CONFIG_FORMAT_ELF */
#ifdef CONFIG_FORMAT_COFF
typedef int32_t host_long;
typedef uint32_t host_ulong;
#include "a.out.h"
#define FILENAMELEN 256
typedef struct coff_sym {
struct external_syment *st_syment;
char st_name[FILENAMELEN];
uint32_t st_value;
int st_size;
uint8_t st_type;
uint8_t st_shndx;
} coff_Sym;
typedef struct coff_rel {
struct external_reloc *r_reloc;
int r_offset;
uint8_t r_type;
} coff_Rel;
#define EXE_RELOC struct coff_rel
#define EXE_SYM struct coff_sym
#endif /* CONFIG_FORMAT_COFF */
#ifdef CONFIG_FORMAT_MACH
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#include <mach-o/reloc.h>
#include <mach-o/ppc/reloc.h>
# define check_mach_header(x) (x.magic == MH_MAGIC)
typedef int32_t host_long;
typedef uint32_t host_ulong;
struct nlist_extended
{
union {
char *n_name;
long n_strx;
} n_un;
unsigned char n_type;
unsigned char n_sect;
short st_desc;
unsigned long st_value;
unsigned long st_size;
};
#define EXE_RELOC struct relocation_info
#define EXE_SYM struct nlist_extended
#endif /* CONFIG_FORMAT_MACH */
#include "bswap.h"
enum {
OUT_GEN_OP,
OUT_CODE,
OUT_INDEX_OP,
};
/* all dynamically generated functions begin with this code */
#define OP_PREFIX "op_"
int do_swap;
static void __attribute__((noreturn)) __attribute__((format (printf, 1, 2))) error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "dyngen: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
static void *load_data(int fd, long offset, unsigned int size)
{
char *data;
data = malloc(size);
if (!data)
return NULL;
lseek(fd, offset, SEEK_SET);
if (read(fd, data, size) != size) {
free(data);
return NULL;
}
return data;
}
int strstart(const char *str, const char *val, const char **ptr)
{
const char *p, *q;
p = str;
q = val;
while (*q != '\0') {
if (*p != *q)
return 0;
p++;
q++;
}
if (ptr)
*ptr = p;
return 1;
}
void pstrcpy(char *buf, int buf_size, const char *str)
{
int c;
char *q = buf;
if (buf_size <= 0)
return;
for(;;) {
c = *str++;
if (c == 0 || q >= buf + buf_size - 1)
break;
*q++ = c;
}
*q = '\0';
}
void swab16s(uint16_t *p)
{
*p = bswap16(*p);
}
void swab32s(uint32_t *p)
{
*p = bswap32(*p);
}
void swab32ss(int32_t *p)
{
*p = bswap32(*p);
}
void swab64s(uint64_t *p)
{
*p = bswap64(*p);
}
void swab64ss(int64_t *p)
{
*p = bswap64(*p);
}
uint16_t get16(uint16_t *p)
{
uint16_t val;
val = *p;
if (do_swap)
val = bswap16(val);
return val;
}
uint32_t get32(uint32_t *p)
{
uint32_t val;
val = *p;
if (do_swap)
val = bswap32(val);
return val;
}
void put16(uint16_t *p, uint16_t val)
{
if (do_swap)
val = bswap16(val);
*p = val;
}
void put32(uint32_t *p, uint32_t val)
{
if (do_swap)
val = bswap32(val);
*p = val;
}
/* executable information */
EXE_SYM *symtab;
int nb_syms;
int text_shndx;
uint8_t *text;
EXE_RELOC *relocs;
int nb_relocs;
#ifdef CONFIG_FORMAT_ELF
/* ELF file info */
struct elf_shdr *shdr;
uint8_t **sdata;
struct elfhdr ehdr;
char *strtab;
int elf_must_swap(struct elfhdr *h)
{
union {
uint32_t i;
uint8_t b[4];
} swaptest;
swaptest.i = 1;
return (h->e_ident[EI_DATA] == ELFDATA2MSB) !=
(swaptest.b[0] == 0);
}
void elf_swap_ehdr(struct elfhdr *h)
{
swab16s(&h->e_type); /* Object file type */
swab16s(&h-> e_machine); /* Architecture */
swab32s(&h-> e_version); /* Object file version */
swabls(&h-> e_entry); /* Entry point virtual address */
swabls(&h-> e_phoff); /* Program header table file offset */
swabls(&h-> e_shoff); /* Section header table file offset */
swab32s(&h-> e_flags); /* Processor-specific flags */
swab16s(&h-> e_ehsize); /* ELF header size in bytes */
swab16s(&h-> e_phentsize); /* Program header table entry size */
swab16s(&h-> e_phnum); /* Program header table entry count */
swab16s(&h-> e_shentsize); /* Section header table entry size */
swab16s(&h-> e_shnum); /* Section header table entry count */
swab16s(&h-> e_shstrndx); /* Section header string table index */
}
void elf_swap_shdr(struct elf_shdr *h)
{
swab32s(&h-> sh_name); /* Section name (string tbl index) */
swab32s(&h-> sh_type); /* Section type */
swabls(&h-> sh_flags); /* Section flags */
swabls(&h-> sh_addr); /* Section virtual addr at execution */
swabls(&h-> sh_offset); /* Section file offset */
swabls(&h-> sh_size); /* Section size in bytes */
swab32s(&h-> sh_link); /* Link to another section */
swab32s(&h-> sh_info); /* Additional section information */
swabls(&h-> sh_addralign); /* Section alignment */
swabls(&h-> sh_entsize); /* Entry size if section holds table */
}
void elf_swap_phdr(struct elf_phdr *h)
{
swab32s(&h->p_type); /* Segment type */
swabls(&h->p_offset); /* Segment file offset */
swabls(&h->p_vaddr); /* Segment virtual address */
swabls(&h->p_paddr); /* Segment physical address */
swabls(&h->p_filesz); /* Segment size in file */
swabls(&h->p_memsz); /* Segment size in memory */
swab32s(&h->p_flags); /* Segment flags */
swabls(&h->p_align); /* Segment alignment */
}
void elf_swap_rel(ELF_RELOC *rel)
{
swabls(&rel->r_offset);
swabls(&rel->r_info);
#ifdef ELF_USES_RELOCA
swablss(&rel->r_addend);
#endif
}
struct elf_shdr *find_elf_section(struct elf_shdr *shdr, int shnum, const char *shstr,
const char *name)
{
int i;
const char *shname;
struct elf_shdr *sec;
for(i = 0; i < shnum; i++) {
sec = &shdr[i];
if (!sec->sh_name)
continue;
shname = shstr + sec->sh_name;
if (!strcmp(shname, name))
return sec;
}
return NULL;
}
int find_reloc(int sh_index)
{
struct elf_shdr *sec;
int i;
for(i = 0; i < ehdr.e_shnum; i++) {
sec = &shdr[i];
if (sec->sh_type == SHT_RELOC && sec->sh_info == sh_index)
return i;
}
return 0;
}
static host_ulong get_rel_offset(EXE_RELOC *rel)
{
return rel->r_offset;
}
static char *get_rel_sym_name(EXE_RELOC *rel)
{
return strtab + symtab[ELFW(R_SYM)(rel->r_info)].st_name;
}
static char *get_sym_name(EXE_SYM *sym)
{
return strtab + sym->st_name;
}
/* load an elf object file */
int load_object(const char *filename)
{
int fd;
struct elf_shdr *sec, *symtab_sec, *strtab_sec, *text_sec;
int i, j;
ElfW(Sym) *sym;
char *shstr;
ELF_RELOC *rel;
fd = open(filename, O_RDONLY);
if (fd < 0)
error("can't open file '%s'", filename);
/* Read ELF header. */
if (read(fd, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
error("unable to read file header");
/* Check ELF identification. */
if (ehdr.e_ident[EI_MAG0] != ELFMAG0
|| ehdr.e_ident[EI_MAG1] != ELFMAG1
|| ehdr.e_ident[EI_MAG2] != ELFMAG2
|| ehdr.e_ident[EI_MAG3] != ELFMAG3
|| ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
error("bad ELF header");
}
do_swap = elf_must_swap(&ehdr);
if (do_swap)
elf_swap_ehdr(&ehdr);
if (ehdr.e_ident[EI_CLASS] != ELF_CLASS)
error("Unsupported ELF class");
if (ehdr.e_type != ET_REL)
error("ELF object file expected");
if (ehdr.e_version != EV_CURRENT)
error("Invalid ELF version");
if (!elf_check_arch(ehdr.e_machine))
error("Unsupported CPU (e_machine=%d)", ehdr.e_machine);
/* read section headers */
shdr = load_data(fd, ehdr.e_shoff, ehdr.e_shnum * sizeof(struct elf_shdr));
if (do_swap) {
for(i = 0; i < ehdr.e_shnum; i++) {
elf_swap_shdr(&shdr[i]);
}
}
/* read all section data */
sdata = malloc(sizeof(void *) * ehdr.e_shnum);
memset(sdata, 0, sizeof(void *) * ehdr.e_shnum);
for(i = 0;i < ehdr.e_shnum; i++) {
sec = &shdr[i];
if (sec->sh_type != SHT_NOBITS)
sdata[i] = load_data(fd, sec->sh_offset, sec->sh_size);
}
sec = &shdr[ehdr.e_shstrndx];
shstr = (char *)sdata[ehdr.e_shstrndx];
/* swap relocations */
for(i = 0; i < ehdr.e_shnum; i++) {
sec = &shdr[i];
if (sec->sh_type == SHT_RELOC) {
nb_relocs = sec->sh_size / sec->sh_entsize;
if (do_swap) {
for(j = 0, rel = (ELF_RELOC *)sdata[i]; j < nb_relocs; j++, rel++)
elf_swap_rel(rel);
}
}
}
/* text section */
text_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".text");
if (!text_sec)
error("could not find .text section");
text_shndx = text_sec - shdr;
text = sdata[text_shndx];
/* find text relocations, if any */
relocs = NULL;
nb_relocs = 0;
i = find_reloc(text_shndx);
if (i != 0) {
relocs = (ELF_RELOC *)sdata[i];
nb_relocs = shdr[i].sh_size / shdr[i].sh_entsize;
}
symtab_sec = find_elf_section(shdr, ehdr.e_shnum, shstr, ".symtab");
if (!symtab_sec)
error("could not find .symtab section");
strtab_sec = &shdr[symtab_sec->sh_link];
symtab = (ElfW(Sym) *)sdata[symtab_sec - shdr];
strtab = (char *)sdata[symtab_sec->sh_link];
nb_syms = symtab_sec->sh_size / sizeof(ElfW(Sym));
if (do_swap) {
for(i = 0, sym = symtab; i < nb_syms; i++, sym++) {
swab32s(&sym->st_name);
swabls(&sym->st_value);
swabls(&sym->st_size);
swab16s(&sym->st_shndx);
}
}
close(fd);
return 0;
}
#endif /* CONFIG_FORMAT_ELF */
#ifdef CONFIG_FORMAT_COFF
/* COFF file info */
struct external_scnhdr *shdr;
uint8_t **sdata;
struct external_filehdr fhdr;
struct external_syment *coff_symtab;
char *strtab;
int coff_text_shndx, coff_data_shndx;
int data_shndx;
#define STRTAB_SIZE 4
#define DIR32 0x06
#define DISP32 0x14
#define T_FUNCTION 0x20
#define C_EXTERNAL 2
void sym_ent_name(struct external_syment *ext_sym, EXE_SYM *sym)
{
char *q;
int c, i, len;
if (ext_sym->e.e.e_zeroes != 0) {
q = sym->st_name;
for(i = 0; i < 8; i++) {
c = ext_sym->e.e_name[i];
if (c == '\0')
break;
*q++ = c;
}
*q = '\0';
} else {
pstrcpy(sym->st_name, sizeof(sym->st_name), strtab + ext_sym->e.e.e_offset);
}
/* now convert the name to a C name (suppress the leading '_') */
if (sym->st_name[0] == '_') {
len = strlen(sym->st_name);
memmove(sym->st_name, sym->st_name + 1, len - 1);
sym->st_name[len - 1] = '\0';
}
}
char *name_for_dotdata(struct coff_rel *rel)
{
int i;
struct coff_sym *sym;
uint32_t text_data;
text_data = *(uint32_t *)(text + rel->r_offset);
for (i = 0, sym = symtab; i < nb_syms; i++, sym++) {
if (sym->st_syment->e_scnum == data_shndx &&
text_data >= sym->st_value &&
text_data < sym->st_value + sym->st_size) {
return sym->st_name;
}
}
return NULL;
}
static char *get_sym_name(EXE_SYM *sym)
{
return sym->st_name;
}
static char *get_rel_sym_name(EXE_RELOC *rel)
{
char *name;
name = get_sym_name(symtab + *(uint32_t *)(rel->r_reloc->r_symndx));
if (!strcmp(name, ".data"))
name = name_for_dotdata(rel);
if (name[0] == '.')
return NULL;
return name;
}
static host_ulong get_rel_offset(EXE_RELOC *rel)
{
return rel->r_offset;
}
struct external_scnhdr *find_coff_section(struct external_scnhdr *shdr, int shnum, const char *name)
{
int i;
const char *shname;
struct external_scnhdr *sec;
for(i = 0; i < shnum; i++) {
sec = &shdr[i];
if (!sec->s_name)
continue;
shname = sec->s_name;
if (!strcmp(shname, name))
return sec;
}
return NULL;
}
/* load a coff object file */
int load_object(const char *filename)
{
int fd;
struct external_scnhdr *sec, *text_sec, *data_sec;
int i;
struct external_syment *ext_sym;
struct external_reloc *coff_relocs;
struct external_reloc *ext_rel;
uint32_t *n_strtab;
EXE_SYM *sym;
EXE_RELOC *rel;
const char *p;
int aux_size, j;
fd = open(filename, O_RDONLY
#ifdef _WIN32
| O_BINARY
#endif
);
if (fd < 0)
error("can't open file '%s'", filename);
/* Read COFF header. */
if (read(fd, &fhdr, sizeof (fhdr)) != sizeof (fhdr))
error("unable to read file header");
/* Check COFF identification. */
if (fhdr.f_magic != I386MAGIC) {
error("bad COFF header");
}
do_swap = 0;
/* read section headers */
shdr = load_data(fd, sizeof(struct external_filehdr) + fhdr.f_opthdr, fhdr.f_nscns * sizeof(struct external_scnhdr));
/* read all section data */
sdata = malloc(sizeof(void *) * fhdr.f_nscns);
memset(sdata, 0, sizeof(void *) * fhdr.f_nscns);
for(i = 0;i < fhdr.f_nscns; i++) {
sec = &shdr[i];
if (!strstart(sec->s_name, ".bss", &p))
sdata[i] = load_data(fd, sec->s_scnptr, sec->s_size);
}
/* text section */
text_sec = find_coff_section(shdr, fhdr.f_nscns, ".text");
if (!text_sec)
error("could not find .text section");
coff_text_shndx = text_sec - shdr;
text = sdata[coff_text_shndx];
/* data section */
data_sec = find_coff_section(shdr, fhdr.f_nscns, ".data");
if (!data_sec)
error("could not find .data section");
coff_data_shndx = data_sec - shdr;
coff_symtab = load_data(fd, fhdr.f_symptr, fhdr.f_nsyms*SYMESZ);
for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
for(i=0;i<8;i++)
printf(" %02x", ((uint8_t *)ext_sym->e.e_name)[i]);
printf("\n");
}
n_strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), STRTAB_SIZE);
strtab = load_data(fd, (fhdr.f_symptr + fhdr.f_nsyms*SYMESZ), *n_strtab);
nb_syms = fhdr.f_nsyms;
for (i = 0, ext_sym = coff_symtab; i < nb_syms; i++, ext_sym++) {
if (strstart(ext_sym->e.e_name, ".text", NULL))
text_shndx = ext_sym->e_scnum;
if (strstart(ext_sym->e.e_name, ".data", NULL))
data_shndx = ext_sym->e_scnum;
}
/* set coff symbol */
symtab = malloc(sizeof(struct coff_sym) * nb_syms);
for (i = 0, ext_sym = coff_symtab, sym = symtab; i < nb_syms; i++, ext_sym++, sym++) {
memset(sym, 0, sizeof(*sym));
sym->st_syment = ext_sym;
sym_ent_name(ext_sym, sym);
sym->st_value = ext_sym->e_value;
aux_size = *(int8_t *)ext_sym->e_numaux;
if (ext_sym->e_scnum == text_shndx && ext_sym->e_type == T_FUNCTION) {
for (j = aux_size + 1; j < nb_syms - i; j++) {
if ((ext_sym + j)->e_scnum == text_shndx &&
(ext_sym + j)->e_type == T_FUNCTION ){
sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
break;
} else if (j == nb_syms - i - 1) {
sec = &shdr[coff_text_shndx];
sym->st_size = sec->s_size - ext_sym->e_value;
break;
}
}
} else if (ext_sym->e_scnum == data_shndx && *(uint8_t *)ext_sym->e_sclass == C_EXTERNAL) {
for (j = aux_size + 1; j < nb_syms - i; j++) {
if ((ext_sym + j)->e_scnum == data_shndx) {
sym->st_size = (ext_sym + j)->e_value - ext_sym->e_value;
break;
} else if (j == nb_syms - i - 1) {
sec = &shdr[coff_data_shndx];
sym->st_size = sec->s_size - ext_sym->e_value;
break;
}
}
} else {
sym->st_size = 0;
}
sym->st_type = ext_sym->e_type;
sym->st_shndx = ext_sym->e_scnum;
}
/* find text relocations, if any */
sec = &shdr[coff_text_shndx];
coff_relocs = load_data(fd, sec->s_relptr, sec->s_nreloc*RELSZ);
nb_relocs = sec->s_nreloc;
/* set coff relocation */
relocs = malloc(sizeof(struct coff_rel) * nb_relocs);
for (i = 0, ext_rel = coff_relocs, rel = relocs; i < nb_relocs;
i++, ext_rel++, rel++) {
memset(rel, 0, sizeof(*rel));
rel->r_reloc = ext_rel;
rel->r_offset = *(uint32_t *)ext_rel->r_vaddr;
rel->r_type = *(uint16_t *)ext_rel->r_type;
}
return 0;
}
#endif /* CONFIG_FORMAT_COFF */
#ifdef CONFIG_FORMAT_MACH
/* File Header */
struct mach_header mach_hdr;
/* commands */
struct segment_command *segment = 0;
struct dysymtab_command *dysymtabcmd = 0;
struct symtab_command *symtabcmd = 0;
/* section */
struct section *section_hdr;
struct section *text_sec_hdr;
uint8_t **sdata;
/* relocs */
struct relocation_info *relocs;
/* symbols */
EXE_SYM *symtab;
struct nlist *symtab_std;
char *strtab;
/* indirect symbols */
uint32_t *tocdylib;
/* Utility functions */
static inline char *find_str_by_index(int index)
{
return strtab+index;
}
/* Used by dyngen common code */
static char *get_sym_name(EXE_SYM *sym)
{
char *name = find_str_by_index(sym->n_un.n_strx);
if ( sym->n_type & N_STAB ) /* Debug symbols are ignored */
return "debug";
if(!name)
return name;
if(name[0]=='_')
return name + 1;
else
return name;
}
/* find a section index given its segname, sectname */
static int find_mach_sec_index(struct section *section_hdr, int shnum, const char *segname,
const char *sectname)
{
int i;
struct section *sec = section_hdr;
for(i = 0; i < shnum; i++, sec++) {
if (!sec->segname || !sec->sectname)
continue;
if (!strcmp(sec->sectname, sectname) && !strcmp(sec->segname, segname))
return i;
}
return -1;
}
/* find a section header given its segname, sectname */
struct section *find_mach_sec_hdr(struct section *section_hdr, int shnum, const char *segname,
const char *sectname)
{
int index = find_mach_sec_index(section_hdr, shnum, segname, sectname);
if(index == -1)
return NULL;
return section_hdr+index;
}
static inline void fetch_next_pair_value(struct relocation_info * rel, unsigned int *value)
{
struct scattered_relocation_info * scarel;
if(R_SCATTERED & rel->r_address) {
scarel = (struct scattered_relocation_info*)rel;
if(scarel->r_type != PPC_RELOC_PAIR)
error("fetch_next_pair_value: looking for a pair which was not found (1)");
*value = scarel->r_value;
} else {
if(rel->r_type != PPC_RELOC_PAIR)
error("fetch_next_pair_value: looking for a pair which was not found (2)");
*value = rel->r_address;
}
}
/* find a sym name given its value, in a section number */
static const char * find_sym_with_value_and_sec_number( int value, int sectnum, int * offset )
{
int i, ret = -1;
for( i = 0 ; i < nb_syms; i++ )
{
if( !(symtab[i].n_type & N_STAB) && (symtab[i].n_type & N_SECT) &&
(symtab[i].n_sect == sectnum) && (symtab[i].st_value <= value) )
{
if( (ret<0) || (symtab[i].st_value >= symtab[ret].st_value) )
ret = i;
}
}
if( ret < 0 ) {
*offset = 0;
return 0;
} else {
*offset = value - symtab[ret].st_value;
return get_sym_name(&symtab[ret]);
}
}
/*
* Find symbol name given a (virtual) address, and a section which is of type
* S_NON_LAZY_SYMBOL_POINTERS or S_LAZY_SYMBOL_POINTERS or S_SYMBOL_STUBS
*/
static const char * find_reloc_name_in_sec_ptr(int address, struct section * sec_hdr)
{
unsigned int tocindex, symindex, size;
const char *name = 0;
/* Sanity check */
if(!( address >= sec_hdr->addr && address < (sec_hdr->addr + sec_hdr->size) ) )
return (char*)0;
if( sec_hdr->flags & S_SYMBOL_STUBS ){
size = sec_hdr->reserved2;
if(size == 0)
error("size = 0");
}
else if( sec_hdr->flags & S_LAZY_SYMBOL_POINTERS ||
sec_hdr->flags & S_NON_LAZY_SYMBOL_POINTERS)
size = sizeof(unsigned long);
else
return 0;
/* Compute our index in toc */
tocindex = (address - sec_hdr->addr)/size;
symindex = tocdylib[sec_hdr->reserved1 + tocindex];
name = get_sym_name(&symtab[symindex]);
return name;
}
static const char * find_reloc_name_given_its_address(int address)
{
unsigned int i;
for(i = 0; i < segment->nsects ; i++)
{
const char * name = find_reloc_name_in_sec_ptr(address, §ion_hdr[i]);
if((long)name != -1)
return name;
}
return 0;
}
static const char * get_reloc_name(EXE_RELOC * rel, int * sslide)
{
char * name = 0;
struct scattered_relocation_info * sca_rel = (struct scattered_relocation_info*)rel;
int sectnum = rel->r_symbolnum;