-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathelf.c
2490 lines (2239 loc) · 70.3 KB
/
elf.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
/* General layout: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format */
/* 32-bit layout: https://refspecs.linuxbase.org/elf/elf.pdf */
/* 64-bit layout: https://www.uclibc.org/docs/elf-64-gen.pdf */
/* x86_64 Relocation types: https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/chapter7-2/index.html */
/* PPC64 extension: http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.pdf */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "capstone/capstone.h"
#include "backend.h"
#include "config.h"
#pragma pack(1)
#ifdef DEBUG
#define DEBUG_PRINT printf
#else
#define DEBUG_PRINT //
#endif
#define ALIGN(_x, _y) ((_x + (_y-1)) & ~(_y-1))
#define ELF_MAGIC "\x7F\x45\x4c\x46"
#define MAGIC_SIZE 4
#define SYMBOL_MAX_LENGTH 127
#define ELF_SYMBOL_GLOBAL 0x10
#define ELF_SYMBOL_WEAK 0x20
#define ELF_SECTION_UNDEF 0
#define ELF_SECTION_ABS 0xFFF1
#define ELF_SECTION_COMMON 0xFFF2
#define ELF_SYM_TYPE(_x) (_x & 0xF) // lower 4 bits from the info field
//#define ELF_SYM_SCOPE(_x) ((_x>>4) & 0xF) // upper 4 bits from the info field
#define ELF32_R_SYM(_x) (_x>>8)
#define ELF32_R_TYPE(_x) (unsigned int)(_x & 0xFF)
#define ELF32_R_INFO(_s, _t) (unsigned int)((_s) << 8 | (_t && 0xFF))
#define ELF64_R_SYM(_x) (unsigned int)(_x>>32)
#define ELF64_R_TYPE(_x) (unsigned int)(_x & 0xFFFFFFFFL)
#define ELF64_R_INFO(_x, _t) (unsigned long)((unsigned long)(_x) << 32 | _t & ELF64_R_TYPE(_t))
#define IS_NULL_SYMBOL(_x) (\
(_x->type == SYMBOL_TYPE_NONE) && \
(_x->size == 0) && \
(_x->val == 0) && \
(_x->name[0] == 0))
/*
enum elf_sections
{
SECTION_INDEX_NULL, // NULL
SECTION_INDEX_TEXT, // .text
SECTION_INDEX_RELA, // .rela.text
SECTION_INDEX_DATA, // .data
SECTION_INDEX_RODATA, // .rodata
SECTION_INDEX_BSS, // .bss
SECTION_INDEX_SYMTAB, // .symtab
SECTION_INDEX_STRTAB, // .strtab
SECTION_INDEX_SHSTRTAB, // .shstrtab
SECTION_COUNT
};
*/
typedef enum elf_machine
{
ELF_ISA_UNSPECIFIED,
ELF_ISA_M32, // AT&T WE 32100
ELF_ISA_SPARC,
ELF_ISA_X86,
ELF_ISA_68K,
ELF_ISA_88K,
ELF_ISA_860,
ELF_ISA_MIPS,
ELF_ISA_POWERPC = 0x14,
ELF_ISA_POWERPC_LE,
ELF_ISA_S390,
ELF_ISA_ARM = 0x28,
ELF_ISA_SUPERH = 0x2A,
ELF_ISA_IA64 = 0x32,
ELF_ISA_X86_64 = 0x3E,
ELF_ISA_AARCH64 = 0xB7,
ELF_ISA_RISCV = 0xF3,
} elf_machine;
// OS
typedef enum elf_os
{
ELF_OS_SYSTEM_V,
ELF_OS_HP_UX,
ELF_OS_NET_BSD,
ELF_OS_LINUX,
ELF_OS_GNU_HURD,
ELF_OS_SOLARIS,
ELF_OS_AIX,
ELF_OS_IRIX,
ELF_OS_FREE_BSD,
ELF_OS_TRU64,
ELF_OS_NOVELL,
ELF_OS_OPEN_BSD,
ELF_OS_OPEN_VMS,
ELF_OS_NONSTOP,
ELF_OS_AROS,
ELF_OS_FENIX,
ELF_OS_CLOUD_ABI,
ELF_OS_SORTIX = 0x53,
ELF_OS_NONE = 0xff
} elf_os;
typedef enum elf_type
{
ELF_TYPE_NONE,
ELF_TYPE_RELOC,
ELF_TYPE_EXEC,
ELF_TYPE_SHARED,
ELF_TYPE_CORE
} elf_type;
typedef enum section_type
{
SHT_NULL,
SHT_PROGBITS,
SHT_SYMTAB,
SHT_STRTAB,
SHT_RELA,
SHT_HASH,
SHT_DYNAMIC,
SHT_NOTE,
SHT_NOBITS,
SHT_REL,
SHT_SHLIB,
SHT_DYNSYM,
} section_type;
// shift values for flags
#define SHF_WRITE 0
#define SHF_ALLOC 1
#define SHF_EXECINSTR 2
// 3 = x
// 4 = M (merge)
// 5 = S (strings)
#define SHF_INFO 6
// L
// G
// T
// E
enum elf_symbol_type
{
ELF_ST_NOTYPE,
ELF_ST_OBJECT,
ELF_ST_FUNC,
ELF_ST_SECTION,
ELF_ST_FILE,
ELF_ST_LOOS = 10,
ELF_ST_HIOS = 12,
ELF_ST_LOPROC = 13,
ELF_ST_HIPROC = 15
};
typedef enum elf_x86_64_reloc_type
{
R_AMD64_NONE,
R_AMD64_64,
R_AMD64_PC32,
R_AMD64_GOT32,
R_AMD64_PLT32,
R_AMD64_COPY,
R_AMD64_GLOB_DAT,
R_AMD64_JUMP_SLOT,
R_AMD64_RELATIVE,
R_AMD64_GOTPCREL,
R_AMD64_32,
R_AMD64_32S,
R_AMD64_16,
R_AMD64_PC16,
R_AMD64_8,
R_AMD64_PC8,
R_AMD64_PC64,
R_AMD64_GOTOFF64,
R_AMD64_GOTPC32,
R_AMD64_SIZE32,
R_AMD64_SIZE64
} elf_x86_64_reloc_type;
typedef enum elf_x86_reloc_type
{
R_386_NONE,
R_386_32,
R_386_PC32,
R_386_GOT32,
R_386_PLT32,
R_386_COPY,
R_386_GLOB_DAT,
R_386_JMP_SLOT,
R_386_RELATIVE,
R_386_GOTOFF,
R_386_GOTPC
} elf_x86_reloc_type;
enum dynamic_tag
{
DT_NULL,
DT_NEEDED,
DT_PLTRELSZ,
DT_PLTGOT,
DT_HASH,
DT_STRTAB,
DT_SYMTAB,
DT_RELA,
DT_RELASZ,
DT_RELAENT,
DT_STRSZ, // 10
DT_SYMENT,
DT_INIT,
DT_FINI,
DT_SONAME,
DT_RPATH,
DT_SYMBOLIC,
DT_REL,
DT_RELSZ,
DT_RELENT,
DT_PLTREL, // 20
DT_DEBUG,
DT_TEXTREL,
DT_JMPREL,
DT_BINDNOW,
DT_INIT_ARRAY,
DT_FINI_ARRAY,
DT_INIT_ARRAYSZ,
DT_FINI_ARRAYSZ,
DT_RUNPATH,
DT_FLAGS, // 30
DT_ENCODING,
DT_PREINIT_ARRAY,
DT_PREINIT_ARRAYSZ,
DT_MAXPOSTARGS,
DT_LOOS=0x60000000,
DT_LOPROC=0x70000000,
DT_VERDEF=0x6ffffffc,
DT_VERDEFNUM,
DT_VERNEED,
DT_VERNEEDNUM,
};
typedef struct elf32_header
{
char magic[4];
char classtype; // 1=32 bit, 2=64 bit
char endian; // 1=little, 2=big
char h_version;
char os; // see ELF_OS_
char abi;
char padding[7];
short type; // see ELF_TYPE_
short machine; // see ELF_MACHINE_
unsigned int version;
unsigned int entry;
unsigned int ph_off; // program header offset
unsigned int sh_off; // section header offset
unsigned int flags;
short eh_size;
short phent_size;
short ph_num;
short shent_size;
short sh_num;
short sh_str_index;
} elf32_header;
typedef struct elf64_header
{
char magic[4];
char size; // 1=32 bit, 2=64 bit
char endian; // 1=little, 2=big
char h_version;
char os; // see ELF_OS_
char abi;
char padding[7];
short type; // see ELF_TYPE_
short machine; // see ELF_MACHINE_
unsigned int version;
unsigned long entry;
unsigned long ph_off; // program header offset
unsigned long sh_off; // section header offset
unsigned int flags;
short eh_size; // size of this struct in bytes = sizeof(elf64_header)
short phent_size; // size of a program header
short ph_num; // number of program headers
short shent_size; // size of a section header = sizeof(elf64_section)
short sh_num; // number of section headers
short sh_str_index; // index of the string table section header
} elf64_header;
typedef struct elf32_section
{
unsigned int name; // offset into the shstrtab string table
unsigned int type; // see SHT_
unsigned int flags; // see SHF_
unsigned int addr;
unsigned int offset; // offset of the section data in the file
unsigned int size;
unsigned int link;
unsigned int info;
unsigned int addralign;
unsigned int entsize;
} elf32_section;
typedef struct elf32_symbol
{
unsigned int name;
unsigned int value;
unsigned int size;
unsigned char info; // see ELF_ST_
unsigned char other;
short section_index;
} elf32_symbol;
typedef struct elf32_rela
{
unsigned int addr;
unsigned int info; // see ELF_R_
int addend;
} elf32_rela;
typedef struct elf64_section
{
unsigned int name; // offset into the shstrtab string table
unsigned int type; // see SHT_
unsigned long flags; // see SHF_
unsigned long addr;
unsigned long offset; // offset of the section data in the file
unsigned long size;
unsigned int link;
unsigned int info;
unsigned long addralign;
unsigned long entsize;
} elf64_section;
typedef struct elf64_symbol
{
unsigned int name;
unsigned char info; // see ELF_ST_
unsigned char other;
short section_index;
unsigned long value;
unsigned long size;
} elf64_symbol;
typedef struct elf64_rela
{
unsigned long addr;
unsigned long info; // see ELF64_R_
long addend;
} elf64_rela;
typedef struct elf_verneed_header
{
unsigned short version; // version number of this struct (must be 1)
unsigned short count;
unsigned int file;
unsigned int aux;
unsigned int next;
} elf_verneed_header;
typedef struct elf_verneed_aux
{
unsigned int hash;
unsigned short flags;
unsigned short other;
unsigned int name;
unsigned int next;
} elf_verneed_aux;
struct item_name
{
unsigned short id;
char name[31+1];
};
typedef struct elf64_dyn
{
unsigned long d_tag;
union
{
unsigned long d_val;
unsigned long d_ptr;
};
} elf64_dyn;
static const struct item_name machine_lookup[] =
{
{ ELF_ISA_UNSPECIFIED, "Unknown machine" },
{ ELF_ISA_M32, "AT&T WE 32100" },
{ ELF_ISA_SPARC, "SPARC" },
{ ELF_ISA_X86, "x86" },
{ ELF_ISA_68K, "Motorola 68000" },
{ ELF_ISA_88K, "Motorola 88000" },
{ ELF_ISA_860, "Intel 80860" },
{ ELF_ISA_MIPS, "MIPS RS3000" },
{ ELF_ISA_POWERPC, "PowerPC" },
{ ELF_ISA_POWERPC_LE, "PowerPC LE" },
{ ELF_ISA_S390, "IBM S390" },
{ ELF_ISA_ARM, "ARM" },
{ ELF_ISA_SUPERH, "SuperH" },
{ ELF_ISA_IA64, "IA 64" },
{ ELF_ISA_X86_64, "x86-64" },
{ ELF_ISA_AARCH64, "AArch64" },
{ ELF_ISA_RISCV, "RISC-V" },
};
static const struct item_name os_lookup[] =
{
{ ELF_OS_SYSTEM_V, "SystemV" },
{ ELF_OS_HP_UX, "HP-UX" },
{ ELF_OS_NET_BSD, "NetBSD" },
{ ELF_OS_LINUX, "Linux" } ,
{ ELF_OS_GNU_HURD, "GNU Hurd" },
{ ELF_OS_SOLARIS, "Solaris" },
{ ELF_OS_AIX, "AIX" },
{ ELF_OS_IRIX, "IRIX" },
{ ELF_OS_FREE_BSD, "FreeBSD" },
{ ELF_OS_TRU64, "Tru64" },
{ ELF_OS_NOVELL, "Novell Modesto" },
{ ELF_OS_OPEN_BSD, "OpenBSD" },
{ ELF_OS_OPEN_VMS, "VMS" },
{ ELF_OS_NONSTOP, "Nonstop Kernel" },
{ ELF_OS_AROS, "AROS" },
{ ELF_OS_FENIX, "Fenix OS" },
{ ELF_OS_CLOUD_ABI, "CloudABI" },
{ ELF_OS_SORTIX, "Sortix" },
{ ELF_OS_NONE, "No OS" },
};
static const struct item_name type_lookup[] =
{
{ ELF_TYPE_NONE, "None" },
{ ELF_TYPE_RELOC, "Relocatable" },
{ ELF_TYPE_EXEC, "Executable" },
{ ELF_TYPE_SHARED, "Shared" },
{ ELF_TYPE_CORE, "Core" },
};
static const struct item_name section_type_lookup[] =
{
{ SHT_NULL, "Bad section" },
{ SHT_PROGBITS, "Program" },
{ SHT_SYMTAB, "Symbol table" },
{ SHT_STRTAB, "String table" },
{ SHT_RELA, "Relocations" },
{ SHT_HASH, "Hash table" },
{ SHT_DYNAMIC, "Dynamic links" },
{ SHT_NOTE, "Notes" },
{ SHT_NOBITS, "No bits" },
{ SHT_REL, "Reclocations" },
{ SHT_SHLIB, "Weirdo" },
{ SHT_DYNSYM, "Dynamic symbols" },
};
static const char* flags_lookup[] =
{
"Does not contain base relocations and must therefore be loaded at its preferred base address",
};
static const char* section_flags_lookup[] =
{
"",
};
#define __lookup(_item, _table) \
for (int i=0; i < sizeof(_table)/sizeof(struct item_name); i++) \
if (_table[i].id == _item) \
return _table[i].name; \
return _table[0].name;
static const char* elf_lookup_type(enum elf_type type)
{
__lookup(type, type_lookup);
}
static const char* elf_lookup_os(enum elf_os os)
{
__lookup(os, os_lookup);
}
static const char* elf_lookup_machine(unsigned short machine)
{
__lookup(machine, machine_lookup);
}
static const char* elf_lookup_section_type(unsigned short type)
{
__lookup(type, section_type_lookup);
}
#ifdef DEBUG
void dump_elf_header(const char* buf)
{
elf64_header *e64 = (elf64_header*)buf;
if (e64->size == 1)
{
fprintf(stderr, "32-bit ELF header\n");
}
else if (e64->size == 2)
{
fprintf(stderr, "64-bit ELF header\n");
if (e64->endian == 1)
fprintf(stderr, "Little endian\n");
else if (e64->endian == 2)
fprintf(stderr, "Big endian\n");
else
fprintf(stderr, "Unknown endian %i\n", e64->endian);
fprintf(stderr, "Version: %i\n", e64->version);
}
else
{
fprintf(stderr, "%i is not a known ELF size\n", e64->size);
}
fprintf(stderr, "OS: %s\n", elf_lookup_os((elf_os)e64->os));
fprintf(stderr, "Type: %s\n", elf_lookup_type((elf_type)e64->type));
fprintf(stderr, "Machine: %s (%i)\n", elf_lookup_machine(e64->machine), e64->machine);
fprintf(stderr, "Entry point: 0x%lx\n", e64->entry);
fprintf(stderr, "Number of program headers: %i\n", e64->ph_num);
}
void dump_elf64_section(elf64_section* s, const char* strtab)
{
printf("Name: %s\n", strtab + s->name);
printf("Type: %s\n", elf_lookup_section_type(s->type));
printf("Flags: 0x%lx\n", s->flags);
printf("Load address: 0x%lx\n", s->addr);
printf("File Offset: 0x%lx\n", s->offset);
printf("Size: 0x%lx\n", s->size);
printf("Alignment: %i (%lu)\n", 2 << s->addralign, s->addralign);
printf("Entry size: %lu\n\n", s->entsize);
}
void dump_elf64_symbol(elf64_symbol *s, const unsigned char *strtab)
{
printf("Name: %s\n", strtab + s->name);
printf("Info: %u\n", s->info);
printf("Other: %u\n", s->other);
printf("Section index: %i\n", s->section_index);
printf("Value: 0x%lx\n", s->value);
printf("Size: 0x%lx\n", s->value);
}
void dump_rela(elf64_rela *rela)
{
printf("Addr: 0x%lx\n", rela->addr);
printf("Info: 0x%lx\n", rela->info);
printf(" Type: 0x%x\n", ELF64_R_TYPE(rela->info));
printf(" Sym: 0x%x\n", ELF64_R_SYM(rela->info));
printf("Addend: 0x%lx\n", rela->addend);
}
#endif // DEBUG
const char* elf32_name(void)
{
return "elf32";
}
const char* elf64_name(void)
{
return "elf64";
}
/* identifies the file format we can read/write */
backend_type elf32_format(void)
{
return OBJECT_TYPE_ELF32;
}
backend_type elf64_format(void)
{
return OBJECT_TYPE_ELF64;
}
backend_symbol_type elf_to_backend_sym_type(unsigned char info)
{
switch(info & 0x0F)
{
case ELF_ST_NOTYPE:
return SYMBOL_TYPE_NONE;
case ELF_ST_OBJECT:
return SYMBOL_TYPE_OBJECT;
case ELF_ST_FUNC:
return SYMBOL_TYPE_FUNCTION;
case ELF_ST_SECTION:
return SYMBOL_TYPE_SECTION;
case ELF_ST_FILE:
return SYMBOL_TYPE_FILE;
}
return SYMBOL_TYPE_NONE;
}
unsigned char backend_to_elf_sym_type(backend_symbol_type t)
{
switch(t)
{
case SYMBOL_TYPE_NONE:
return ELF_ST_NOTYPE;
case SYMBOL_TYPE_OBJECT:
return ELF_ST_OBJECT;
case SYMBOL_TYPE_FUNCTION:
return ELF_ST_FUNC;
case SYMBOL_TYPE_SECTION:
return ELF_ST_SECTION;
case SYMBOL_TYPE_FILE:
return ELF_ST_FILE;
}
return ELF_ST_NOTYPE;
}
elf_x86_reloc_type backend_to_elf32_reloc_type(backend_reloc_type t)
{
switch(t)
{
case RELOC_TYPE_OFFSET:
return R_386_32;
case RELOC_TYPE_PC_RELATIVE:
return R_386_PC32;
}
return R_386_NONE;
}
elf_x86_64_reloc_type backend_to_elf64_reloc_type(backend_reloc_type t)
{
switch(t)
{
case RELOC_TYPE_OFFSET:
return R_AMD64_32;
case RELOC_TYPE_PC_RELATIVE:
return R_AMD64_PC32;
case RELOC_TYPE_PLT:
return R_AMD64_PLT32;
}
return R_AMD64_NONE;
}
backend_section_type elf_to_backend_section_type(section_type t)
{
switch(t)
{
case SHT_NULL:
return SECTION_TYPE_NULL;
case SHT_PROGBITS:
return SECTION_TYPE_PROG;
case SHT_SYMTAB:
return SECTION_TYPE_SYMTAB;
case SHT_STRTAB:
return SECTION_TYPE_STRTAB;
case SHT_RELA:
return SECTION_TYPE_RELA;
case SHT_HASH:
return SECTION_TYPE_NULL;
case SHT_DYNAMIC:
return SECTION_TYPE_DYNSYM;
case SHT_NOTE:
return SECTION_TYPE_NOTE;
case SHT_NOBITS:
return SECTION_TYPE_NOBITS;
case SHT_REL:
return SECTION_TYPE_REL;
case SHT_SHLIB:
return SECTION_TYPE_NULL;
case SHT_DYNSYM:
return SECTION_TYPE_SYMTAB;
}
return SECTION_TYPE_NULL;
}
int elf_reloc_addend(elf_x86_64_reloc_type t)
{
if (t == R_AMD64_PC32)
return -4;
return 0;
}
// this should be moved to the backend - it has nothing to do with elf
static unsigned long decode_plt_entry_x86_64(csh cs_dis, cs_insn *cs_ins, const unsigned char *pc, uint64_t pc_addr, unsigned long entry_size)
{
cs_disasm_iter(cs_dis, &pc, &entry_size, &pc_addr, cs_ins);
//printf("id: %u %s\n", cs_ins->id, cs_ins->mnemonic);
if (cs_ins->id == X86_INS_ENDBR64)
{
cs_disasm_iter(cs_dis, &pc, &entry_size, &pc_addr, cs_ins);
//printf("id: %u %s\n", cs_ins->id, cs_ins->mnemonic);
if (cs_ins->id == X86_INS_PUSH)
{
return 0;
}
else if (cs_ins->id == X86_INS_JMP)
{
// extract the target address that should point to the GOT
// The jump instruction is actually relative to the PC, so we must add
// the current instruction address when looking it up.
unsigned long target = *(unsigned int*)&cs_ins->bytes[3];
target += pc_addr;
return target;
}
}
return 0;
}
// read the section headers sequentially from the file, looking for a specific section name
int elf64_find_section(FILE* f, const elf64_header* h, const char* name, const char* strtab, elf64_section* s)
{
for (int i=0; i < h->sh_num; i++)
{
fseek(f, h->sh_off + h->shent_size * i, SEEK_SET);
if (fread(s, h->shent_size, 1, f) != 1)
return -2;
if (strcmp(strtab + s->name, name) == 0)
return i;
}
return -1;
}
/* Used to compare two symbols when sorting symbol table
in order to write them to an ELF object file */
static int elfcmp(void* item_a, void* item_b)
{
backend_symbol *a = (backend_symbol*)item_a;
backend_symbol *b = (backend_symbol*)item_b;
// ELF symbol ordering is like this:
// 1. A null symbol (singleton)
// 2. File symbol for this object file (singleton)
// 3. Section symbols (local)
// 4. Other locals (can sometimes intermingle with sections)
// 5. global functions
// 6. other globals
// check to see if it is a null symbol. If so, it doesn't matter
// what B is, because null symbols are all the same (actually there
// should only be 1) and they must come before any other symbol.
if (IS_NULL_SYMBOL(a))
return 0;
// if A is a file symbol, it must come after any null symbols,
// but before anything else.
else if (a->type == SYMBOL_TYPE_FILE)
{
if (IS_NULL_SYMBOL(b))
return 1;
else if (b->type == SYMBOL_TYPE_FILE)
return 0;
else
return -1;
}
// All section symbols come after the file symbol. They are
// considered equal, and sometimes their indices are out of
// order (which is legal, but bothers me).
else if (a->type == SYMBOL_TYPE_SECTION)
{
if (IS_NULL_SYMBOL(b))
return 1;
else if (b->type == SYMBOL_TYPE_FILE)
return 1;
else if (b->type == SYMBOL_TYPE_SECTION)
{
return 0;
}
else
return -1;
}
else if (!(a->flags & SYMBOL_FLAG_GLOBAL))
{
if (!(b->flags & SYMBOL_FLAG_GLOBAL))
return 0;
return -1;
}
else
{
if (!(b->flags & SYMBOL_FLAG_GLOBAL))
return 1;
if (a->type == SYMBOL_TYPE_FUNCTION)
{
if (b->type == SYMBOL_TYPE_FUNCTION)
return 0;
else
return -1;
}
return -1;
}
return 0;
}
static backend_object* elf32_read_file(FILE* f, elf32_header* h)
{
char sym_name[SYMBOL_MAX_LENGTH+1];
backend_arch be_arch;
elf32_section in_sec;
backend_section* sec_symtab;
backend_section* sec_dynsym;
backend_section* sec_dynstr;
backend_section* sec_versym;
backend_section* sec_versymr;
backend_section* sec_text;
backend_section* sec_rela;
backend_section* sec_relaplt;
backend_section* sec_strtab = NULL;
elf32_symbol* dsym;
elf32_symbol* sym;
elf32_rela* rela;
unsigned short* ver;
elf_verneed_header* versymr;
elf_verneed_aux* verent;
char* section_strtab = NULL;
char *src_file = NULL;
printf("elf32_read_file\n");
backend_object* obj = backend_create();
if (!obj)
return 0;
backend_set_type(obj, OBJECT_TYPE_ELF32);
switch (h->machine)
{
case ELF_ISA_X86:
be_arch = OBJECT_ARCH_X86;
break;
case ELF_ISA_ARM:
be_arch = OBJECT_ARCH_ARM;
break;
default:
be_arch = OBJECT_ARCH_UNKNOWN;
}
if (config.verbose)
fprintf(stderr, "Arch %i\n", be_arch);
backend_set_arch(obj, be_arch);
if (config.verbose)
{
fprintf(stderr, "Number of section headers: %i\n", h->sh_num);
fprintf(stderr, "Size of section headers: %i\n", h->shent_size);
fprintf(stderr, "String table index: %i\n", h->sh_str_index);
}
backend_set_entry_point(obj, h->entry);
// validate the size of the section entry struct
if (h->shent_size != sizeof(elf32_section))
{
printf("Size mismatch in section: read %i expected %lu\n",
h->shent_size, sizeof(elf32_section));
}
// first, preload the section header string table
fseek(f, h->sh_off + h->shent_size * h->sh_str_index, SEEK_SET);
if (fread(&in_sec, h->shent_size, 1, f) != 1)
{
fprintf(stderr, "Error loading string table\n");
goto error;
}
section_strtab = (char*)malloc(in_sec.size);
fseek(f, in_sec.offset, SEEK_SET);
if (fread(section_strtab, in_sec.size, 1, f) != 1)
goto error_strtab;
// load sections
for (int i=1; i < h->sh_num; i++)
{
fseek(f, h->sh_off + h->shent_size * i, SEEK_SET);
if (fread(&in_sec, h->shent_size, 1, f) != 1)
goto error_strtab;
// if a section with this name doesn't already exist, add it
char* name = section_strtab + in_sec.name;
if (!backend_get_section_by_name(obj, name))
{
unsigned long flags=0;
unsigned char* data = NULL;
// load the section data unless it is marked as unloadable
if (in_sec.type != SHT_NOBITS)
{
data = (unsigned char*)malloc(in_sec.size);
fseek(f, in_sec.offset, SEEK_SET);
if (fread(data, in_sec.size, 1, f) != 1)
{
fprintf(stderr, "Error loading section %s data\n", name);
free(data);
goto error_strtab;
}
}
// set flags for known sections by name
backend_section_type t;
if (strcmp(name, ".text") == 0)
flags = SECTION_FLAG_EXECUTE;
else if (strcmp(name, ".init") == 0)
flags = SECTION_FLAG_EXECUTE;
else if (strcmp(name, ".data") == 0)
flags = SECTION_FLAG_INIT_DATA;
else if (strcmp(name, ".rodata") == 0)
flags = SECTION_FLAG_INIT_DATA;
else if (strcmp(name, ".bss") == 0)
flags = SECTION_FLAG_UNINIT_DATA;
else
{
if (in_sec.flags & SHF_EXECINSTR)
flags = SECTION_FLAG_EXECUTE;
if (in_sec.flags & SHF_ALLOC && !(in_sec.flags & SHF_EXECINSTR) && (!in_sec.flags & SHF_WRITE))
flags = SECTION_FLAG_INIT_DATA;
if (in_sec.flags & SHF_ALLOC && !(in_sec.flags & SHF_EXECINSTR)) // not exactly accurate - better to set these flags according to section name
flags = SECTION_FLAG_UNINIT_DATA;
}
backend_section *s = backend_add_section(obj, name, in_sec.size, in_sec.addr, data, in_sec.entsize, in_sec.addralign, flags);
backend_section_set_type(s, elf_to_backend_section_type((section_type)in_sec.type));
}
}
// now that we have the raw data, try to format it as objects the backend can understand (strings, symbols, sections, relocs, etc)
sec_strtab = backend_get_section_by_name(obj, ".strtab");
if (!sec_strtab)
{
sec_strtab = backend_get_section_by_type(obj, SECTION_TYPE_STRTAB);
if (!sec_strtab)
printf("Warning: can't find string table section!\n");
//goto done;
}
// create symbols
sec_symtab = backend_get_section_by_name(obj, ".symtab");
if (!sec_symtab)
{
sec_symtab = backend_get_section_by_type(obj, SECTION_TYPE_SYMTAB);
if (!sec_symtab)
printf("Warning: can't find symbol table section!\n");
goto dynsym;
}
// Add each symbol to the backend object
sym = (elf32_symbol*)sec_symtab->data;
for (int i=0; i < sec_symtab->size/sec_symtab->entry_size; i++, sym++)
{
const char* name = NULL;
backend_section* sec = NULL;
backend_symbol *s;
int symbol_flags=0;
// get the symbol name
if (sym->name && sec_strtab)
name = (char*)sec_strtab->data + sym->name;
switch (ELF_SYM_TYPE(sym->info))
{
case ELF_ST_NOTYPE:
// The first symbol in an ELF file must have no name and no type
//printf("Skipping symbol with no type\n");
if (!backend_add_symbol(obj, name, 0, elf_to_backend_sym_type(sym->info), 0, 0, sec_symtab))
printf("Failed adding untyped symbol\n");
continue;
case ELF_ST_SECTION:
sec = backend_get_section_by_index(obj, sym->section_index);
if (!backend_add_symbol(obj, sec->name, 0, elf_to_backend_sym_type(sym->info), 0, 0, sec) || !sec)
printf("Failed adding section symbol\n");
//printf("Adding section symbol %s (%i)\n", sec->name, sym->section_index);
continue;
case ELF_ST_FILE:
// set this as the owning file for subsequent symbols
if (src_file)
free(src_file);
if (!name)
{
//printf("Found unnamed file symbol\n");
name = "_global.c";
}
//else
// printf("Found file symbol %s\n", name);
src_file = strdup(name);
break;
}
// try to determine the section that this symbol belongs to
if (sym->section_index <= 0 ||
sym->section_index == ELF_SECTION_ABS ||