-
Notifications
You must be signed in to change notification settings - Fork 2
/
elfy.c
2522 lines (2273 loc) · 87.3 KB
/
elfy.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
#include "elf.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <libelf.h>
#include <gelf.h>
#define ELFY_VERSION "0.2.0"
#define TAB " "
#define C_RED "\033[31m"
#define C_GREEN "\033[32m"
#define C_YELLOW "\033[33m"
#define C_END "\033[0m"
#define print_error(...) fprintf(stderr, "elfy: " __VA_ARGS__);
int file_header_opt,
program_headers_opt,
section_headers_opt,
dynamic_section_opt,
symtab_opt,
dynamic_symtab_opt,
no_color_opt,
help_opt,
version_opt,
all_opt;
const struct option long_opts[] = {
{"file-header", no_argument, &file_header_opt, 1},
{"program-headers", no_argument, &program_headers_opt, 1},
{"section-headers", no_argument, §ion_headers_opt, 1},
{"dynamic", no_argument, &dynamic_section_opt, 1},
{"symtab", no_argument, &symtab_opt, 1},
{"dyn-syms", no_argument, &dynamic_symtab_opt, 1},
{"all", no_argument, &all_opt, 1},
{"no-color", no_argument, &no_color_opt, 1},
{"help", no_argument, &help_opt, 1},
{"version", no_argument, &version_opt, 1},
{0, 0, 0, 0}
};
// length of the longest field name (used by print_field)
int field_max_len = 0;
// add color to the title when needed
void print_title(char *title, ...) {
va_list args;
va_start(args, title);
if(!no_color_opt)
printf("%s", C_YELLOW);
vprintf(title, args);
if(!no_color_opt)
printf("%s", C_END);
putchar('\n');
va_end(args);
}
// print field name and its value
// add spaces between name and value based on field_max_len
// don't print value if value is NULL
void print_field(const char *field, char *value, ...) {
int len = strlen(field);
if(!no_color_opt)
printf(C_RED "%s" C_END TAB "%*s", field, field_max_len - len, "");
else
printf("%s" TAB "%*s", field, field_max_len - len, "");
if(value) {
va_list args;
va_start(args, value);
if(!no_color_opt)
printf("%s", C_GREEN);
vprintf(value, args);
if(!no_color_opt)
printf("%s", C_END);
putchar('\n');
va_end(args);
}
}
// print field value and its info inside parentheses
// e.g.: ET_DYN (shared object file)
void print_field_info(char *value, char *info) {
if(!no_color_opt)
printf(C_GREEN "%s" C_END " (%s)\n", value, info);
else
printf("%s (%s)\n", value, info);
}
// display the elf file header (option -h)
void show_file_header(Elf *elf) {
GElf_Ehdr ehdr;
print_title("File Header\n");
// strlen("EI_ABIVERSION")
field_max_len = 13;
// get the elf file header
if(!gelf_getehdr(elf, &ehdr)) {
print_error("gelf_getehdr() failed: %s", elf_errmsg(-1));
exit(EXIT_FAILURE);
}
print_title("Elf_Ehdr");
// magic number and other info
print_field("e_ident", NULL);
if(!no_color_opt)
printf("%s", C_GREEN);
for(int i = 0; i < EI_NIDENT; i++) {
if(i + 1 != EI_NIDENT)
printf("%2.2x ", ehdr.e_ident[i]);
else
printf("%2.2x\n", ehdr.e_ident[i]);
}
if(!no_color_opt)
printf("%s", C_END);
// object file type
print_field("e_type", NULL);
switch(ehdr.e_type) {
case ET_NONE:
print_field_info("ET_NONE", "unknown type");
break;
case ET_REL:
print_field_info("ET_REL", "relocatable file");
break;
case ET_EXEC:
print_field_info("ET_EXEC", "executable file");
break;
case ET_DYN:
print_field_info("ET_DYN", "shared object file");
break;
case ET_CORE:
print_field_info("ET_CORE", "core file");
break;
default:
if(!no_color_opt)
printf(C_GREEN "%#x" C_END, ehdr.e_type);
else
printf("%#x", ehdr.e_type);
if((ehdr.e_type >= ET_LOOS) && (ehdr.e_type <= ET_HIOS))
puts(" (os-specific)");
else if(ehdr.e_type >= ET_LOPROC)
puts(" (processor-specific)");
else
puts(" (unknown)");
}
// architecture
print_field("e_machine", NULL);
switch(ehdr.e_machine) {
case EM_NONE:
print_field_info("EM_NONE", "no machine");
break;
case EM_M32:
print_field_info("EM_M32", "AT&T WE 32100");
break;
case EM_SPARC:
print_field_info("EM_SPARC", "SUN SPARC");
break;
case EM_386:
print_field_info("EM_386", "Intel 80386");
break;
case EM_68K:
print_field_info("EM_68K", "Motorola m68k family");
break;
case EM_88K:
print_field_info("EM_88K", "Motorola m88k family");
break;
case EM_IAMCU:
print_field_info("EM_IAMCU", "Intel MCU");
break;
case EM_860:
print_field_info("EM_860", "Intel 80860");
break;
case EM_MIPS:
print_field_info("EM_MIPS", "MIPS R3000 big-endian");
break;
case EM_S370:
print_field_info("EM_S370", "IBM System/370");
break;
case EM_MIPS_RS3_LE:
print_field_info("EM_MIPS_RS3_LE", "MIPS R3000 little-endian");
break;
case EM_PARISC:
print_field_info("EM_PARISC", "HPPA");
break;
case EM_VPP500:
print_field_info("EM_VPP500", "Fujitsu VPP500");
break;
case EM_SPARC32PLUS:
print_field_info("EM_SPARC32PLUS", "Sun's \"v8plus\"");
break;
case EM_960:
print_field_info("EM_960", "Intel 80960");
break;
case EM_PPC:
print_field_info("EM_PPC", "PowerPC");
break;
case EM_PPC64:
print_field_info("EM_PPC64", "PowerPC 64-bit");
break;
case EM_S390:
print_field_info("EM_S390", "IBM S390");
break;
case EM_SPU:
print_field_info("EM_SPU", "IBM SPU/SPC");
break;
case EM_V800:
print_field_info("EM_V800", "NEC V800 series");
break;
case EM_FR20:
print_field_info("EM_FR20", "Fujitsu FR20");
break;
case EM_RH32:
print_field_info("EM_RH32", "TRW RH-32");
break;
case EM_RCE:
print_field_info("EM_RCE", "Motorola RCE");
break;
case EM_ARM:
print_field_info("EM_ARM", "ARM");
break;
case EM_FAKE_ALPHA:
print_field_info("EM_FAKE_ALPHA", "Digital Alpha");
break;
case EM_SH:
print_field_info("EM_SH", "Hitachi SH");
break;
case EM_SPARCV9:
print_field_info("EM_SPARCV9", "SPARC v9 64-bit");
break;
case EM_TRICORE:
print_field_info("EM_TRICORE", "Siemens Tricore");
break;
case EM_ARC:
print_field_info("EM_ARC", "Argonaut RISC Core");
break;
case EM_H8_300:
print_field_info("EM_H8_300", "Hitachi H8/300");
break;
case EM_H8_300H:
print_field_info("EM_H8_300H", "Hitachi H8/300H");
break;
case EM_H8S:
print_field_info("EM_H8S", "Hitachi H8S");
break;
case EM_H8_500:
print_field_info("EM_H8_500", "Hitachi H8/500");
break;
case EM_IA_64:
print_field_info("EM_IA_64", "Intel Merced");
break;
case EM_MIPS_X:
print_field_info("EM_MIPS_X", "Stanford MIPS-X");
break;
case EM_COLDFIRE:
print_field_info("EM_COLDFIRE", "Motorola Coldfire");
break;
case EM_68HC12:
print_field_info("EM_68HC12", "Motorola M68HC12");
break;
case EM_MMA:
print_field_info("EM_MMA", "Fujitsu MMA Multimedia Accelerator");
break;
case EM_PCP:
print_field_info("EM_PCP", "Siemens PCP");
break;
case EM_NCPU:
print_field_info("EM_NCPU", "Sony nCPU embeeded RISC");
break;
case EM_NDR1:
print_field_info("EM_NDR1", "Denso NDR1 microprocessor");
break;
case EM_STARCORE:
print_field_info("EM_STARCORE", "Motorola Start*Core processor");
break;
case EM_ME16:
print_field_info("EM_ME16", "Toyota ME16 processor");
break;
case EM_ST100:
print_field_info("EM_ST100", "STMicroelectronic ST100 processor");
break;
case EM_TINYJ:
print_field_info("EM_TINYJ", "Advanced Logic Corp. Tinyj emb.fam");
break;
case EM_X86_64:
print_field_info("EM_X86_64", "AMD x86-64 architecture");
break;
case EM_PDSP:
print_field_info("EM_PDSP", "Sony DSP Processor");
break;
case EM_PDP10:
print_field_info("EM_PDP10", "Digital PDP-10");
break;
case EM_PDP11:
print_field_info("EM_PDP11", "Digital PDP-11");
break;
case EM_FX66:
print_field_info("EM_FX66", "Siemens FX66 microcontroller");
break;
case EM_ST9PLUS:
print_field_info("EM_ST9PLUS", "STMicroelectronics ST9+ 8/16 mc");
break;
case EM_ST7:
print_field_info("EM_ST7", "STmicroelectronics ST7 8 bit mc");
break;
case EM_68HC16:
print_field_info("EM_68HC16", "Motorola MC68HC16 microcontroller");
break;
case EM_68HC11:
print_field_info("EM_68HC11", "Motorola MC68HC11 microcontroller");
break;
case EM_68HC08:
print_field_info("EM_68HC08", "Motorola MC68HC08 microcontroller");
break;
case EM_68HC05:
print_field_info("EM_68HC05", "Motorola MC68HC05 microcontroller");
break;
case EM_SVX:
print_field_info("EM_SVX", "Silicon Graphics SVx");
break;
case EM_ST19:
print_field_info("EM_ST19", "STMicroelectronics ST19 8 bit mc");
break;
case EM_VAX:
print_field_info("EM_VAX", "Digital VAX");
break;
case EM_CRIS:
print_field_info("EM_CRIS", "Axis Communications 32-bit emb.proc");
break;
case EM_JAVELIN:
print_field_info("EM_JAVELIN", "Infineon Technologies 32-bit emb.proc");
break;
case EM_FIREPATH:
print_field_info("EM_FIREPATH", "Element 14 64-bit DSP Processor");
break;
case EM_ZSP:
print_field_info("EM_ZSP", "LSI Logic 16-bit DSP Processor");
break;
case EM_MMIX:
print_field_info("EM_MMIX", "Donald Knuth's educational 64-bit proc");
break;
case EM_HUANY:
print_field_info("EM_HUANY", "Harvard University machine-independent object files");
break;
case EM_PRISM:
print_field_info("EM_PRISM", "SiTera Prism");
break;
case EM_AVR:
print_field_info("EM_AVR", "Atmel AVR 8-bit microcontroller");
break;
case EM_FR30:
print_field_info("EM_FR30", "Fujitsu FR30");
break;
case EM_D10V:
print_field_info("EM_D10V", "Mitsubishi D10V");
break;
case EM_D30V:
print_field_info("EM_D30V", "Mitsubishi D30V");
break;
case EM_V850:
print_field_info("EM_V850", "NEC v850");
break;
case EM_M32R:
print_field_info("EM_M32R", "Mitsubishi M32R");
break;
case EM_MN10300:
print_field_info("EM_MN10300", "Matsushita MN10300");
break;
case EM_MN10200:
print_field_info("EM_MN10200", "Matsushita MN10200");
break;
case EM_PJ:
print_field_info("EM_PJ", "picoJava");
break;
case EM_OPENRISC:
print_field_info("EM_OPENRISC", "OpenRISC 32-bit embedded processor");
break;
case EM_ARC_COMPACT:
print_field_info("EM_ARC_COMPACT", "ARC International ARCompact");
break;
case EM_XTENSA:
print_field_info("EM_XTENSA", "Tensilica Xtensa Architecture");
break;
case EM_VIDEOCORE:
print_field_info("EM_VIDEOCORE", "Alphamosaic VideoCore");
break;
case EM_TMM_GPP:
print_field_info("EM_TMM_GPP", "Thompson Multimedia General Purpose Proc");
break;
case EM_NS32K:
print_field_info("EM_NS32K", "National Semi. 32000");
break;
case EM_TPC:
print_field_info("EM_TPC", "Tenor Network TPC");
break;
case EM_SNP1K:
print_field_info("EM_SNP1K", "Trebia SNP 1000");
break;
case EM_ST200:
print_field_info("EM_ST200", "STMicroelectronics ST200");
break;
case EM_IP2K:
print_field_info("EM_IP2K", "Ubicom IP2xxx");
break;
case EM_MAX:
print_field_info("EM_MAX", "MAX processor");
break;
case EM_CR:
print_field_info("EM_CR", "National Semi. CompactRISC");
break;
case EM_F2MC16:
print_field_info("EM_F2MC16", "Fujitsu F2MC16");
break;
case EM_MSP430:
print_field_info("EM_MSP430", "Texas Instruments msp430");
break;
case EM_BLACKFIN:
print_field_info("EM_BLACKFIN", "Analog Devices Blackfin DSP");
break;
case EM_SE_C33:
print_field_info("EM_SE_C33", "Seiko Epson S1C33 family");
break;
case EM_SEP:
print_field_info("EM_SEP", "Sharp embedded microprocessor");
break;
case EM_ARCA:
print_field_info("EM_ARCA", "Arca RISC");
break;
case EM_UNICORE:
print_field_info("EM_UNICORE", "PKU-Unity & MPRC Peking Uni. mc series");
break;
case EM_EXCESS:
print_field_info("EM_EXCESS", "eXcess configurable cpu");
break;
case EM_DXP:
print_field_info("EM_DXP", "Icera Semi. Deep Execution Processor");
break;
case EM_ALTERA_NIOS2:
print_field_info("EM_ALTERA_NIOS2", "Altera Nios II");
break;
case EM_CRX:
print_field_info("EM_CRX", "National Semi. CompactRISC CRX");
break;
case EM_XGATE:
print_field_info("EM_XGATE", "Motorola XGATE");
break;
case EM_C166:
print_field_info("EM_C166", "Infineon C16x/XC16x");
break;
case EM_M16C:
print_field_info("EM_M16C", "Renesas M16C");
break;
case EM_DSPIC30F:
print_field_info("EM_DSPIC30F", "Microchip Technology dsPIC30F");
break;
case EM_CE:
print_field_info("EM_CE", "Freescale Communication Engine RISC");
break;
case EM_M32C:
print_field_info("EM_M32C", "Renesas M32C");
break;
case EM_TSK3000:
print_field_info("EM_TSK3000", "Altium TSK3000");
break;
case EM_RS08:
print_field_info("EM_RS08", "Freescale RS08");
break;
case EM_SHARC:
print_field_info("EM_SHARC", "Analog Devices SHARC family");
break;
case EM_ECOG2:
print_field_info("EM_ECOG2", "Cyan Technology eCOG2");
break;
case EM_SCORE7:
print_field_info("EM_SCORE7", "Sunplus S+core7 RISC");
break;
case EM_DSP24:
print_field_info("EM_DSP24", "New Japan Radio (NJR) 24-bit DSP");
break;
case EM_VIDEOCORE3:
print_field_info("EM_VIDEOCORE3", "Broadcom VideoCore III");
break;
case EM_LATTICEMICO32:
print_field_info("EM_LATTICEMICO32", "RISC for Lattice FPGA");
break;
case EM_SE_C17:
print_field_info("EM_SE_C17", "Seiko Epson C17");
break;
case EM_TI_C6000:
print_field_info("EM_TI_C6000", "Texas Instruments TMS320C6000 DSP");
break;
case EM_TI_C2000:
print_field_info("EM_TI_C2000", "Texas Instruments TMS320C2000 DSP");
break;
case EM_TI_C5500:
print_field_info("EM_TI_C5500", "Texas Instruments TMS320C55x DSP");
break;
case EM_TI_ARP32:
print_field_info("EM_TI_ARP32", "Texas Instruments App. Specific RISC");
break;
case EM_TI_PRU:
print_field_info("EM_TI_PRU", "Texas Instruments Prog. Realtime Unit");
break;
case EM_MMDSP_PLUS:
print_field_info("EM_MMDSP_PLUS", "STMicroelectronics 64bit VLIW DSP");
break;
case EM_CYPRESS_M8C:
print_field_info("EM_CYPRESS_M8C", "Cypress M8C");
break;
case EM_R32C:
print_field_info("EM_R32C", "Renesas R32C");
break;
case EM_TRIMEDIA:
print_field_info("EM_TRIMEDIA", "NXP Semi. TriMedia");
break;
case EM_QDSP6:
print_field_info("EM_QDSP6", "QUALCOMM DSP6");
break;
case EM_8051:
print_field_info("EM_8051", "Intel 8051 and variants");
break;
case EM_STXP7X:
print_field_info("EM_STXP7X", "STMicroelectronics STxP7x");
break;
case EM_NDS32:
print_field_info("EM_NDS32", "Andes Tech. compact code emb. RISC");
break;
case EM_ECOG1X:
print_field_info("EM_ECOG1X", "Cyan Technology eCOG1X");
break;
case EM_MAXQ30:
print_field_info("EM_MAXQ30", "Dallas Semi. MAXQ30 mc");
break;
case EM_XIMO16:
print_field_info("EM_XIMO16", "New Japan Radio (NJR) 16-bit DSP");
break;
case EM_MANIK:
print_field_info("EM_MANIK", "M2000 Reconfigurable RISC");
break;
case EM_CRAYNV2:
print_field_info("EM_CRAYNV2", "Cray NV2 vector architecture");
break;
case EM_RX:
print_field_info("EM_RX", "Renesas RX");
break;
case EM_METAG:
print_field_info("EM_METAG", "Imagination Tech. META");
break;
case EM_MCST_ELBRUS:
print_field_info("EM_MCST_ELBRUS", "MCST Elbrus");
break;
case EM_ECOG16:
print_field_info("EM_ECOG16", "Cyan Technology eCOG16");
break;
case EM_CR16:
print_field_info("EM_CR16", "National Semi. CompactRISC CR16");
break;
case EM_ETPU:
print_field_info("EM_ETPU", "Freescale Extended Time Processing Unit");
break;
case EM_SLE9X:
print_field_info("EM_SLE9X", "Infineon Tech. SLE9X");
break;
case EM_L10M:
print_field_info("EM_L10M", "Intel L10M");
break;
case EM_K10M:
print_field_info("EM_K10M", "Intel K10M");
break;
case EM_AARCH64:
print_field_info("EM_AARCH64", "ARM AARCH64");
break;
case EM_AVR32:
print_field_info("EM_AVR32", "Amtel 32-bit microprocessor");
break;
case EM_STM8:
print_field_info("EM_STM8", "STMicroelectronics STM8");
break;
case EM_TILE64:
print_field_info("EM_TILE64", "Tilera TILE64");
break;
case EM_TILEPRO:
print_field_info("EM_TILEPRO", "Tilera TILEPro");
break;
case EM_MICROBLAZE:
print_field_info("EM_MICROBLAZE", "Xilinx MicroBlaze");
break;
case EM_CUDA:
print_field_info("EM_CUDA", "NVIDIA CUDA");
break;
case EM_TILEGX:
print_field_info("EM_TILEGX", "Tilera TILE-Gx");
break;
case EM_CLOUDSHIELD:
print_field_info("EM_CLOUDSHIELD", "CloudShield");
break;
case EM_COREA_1ST:
print_field_info("EM_COREA_1ST", "KIPO-KAIST Core-A 1st gen");
break;
case EM_COREA_2ND:
print_field_info("EM_COREA_2ND", "KIPO-KAIST Core-A 2nd gen");
break;
case EM_ARCV2:
print_field_info("EM_ARCV2", "Synopsys ARCv2 ISA");
break;
case EM_OPEN8:
print_field_info("EM_OPEN8", "Open8 RISC");
break;
case EM_RL78:
print_field_info("EM_RL78", "Renesas RL78");
break;
case EM_VIDEOCORE5:
print_field_info("EM_VIDEOCORE5", "Broadcom VideoCore V");
break;
case EM_78KOR:
print_field_info("EM_78KOR", "Renesas 78KOR");
break;
case EM_56800EX:
print_field_info("EM_56800EX", "Freescale 56800EX DSC");
break;
case EM_BA1:
print_field_info("EM_BA1", "Beyond BA1");
break;
case EM_BA2:
print_field_info("EM_BA2", "Beyond BA2");
break;
case EM_XCORE:
print_field_info("EM_XCORE", "XMOS xCORE");
break;
case EM_MCHP_PIC:
print_field_info("EM_MCHP_PIC", "Microchip 8-bit PIC(r)");
break;
case EM_INTELGT:
print_field_info("EM_INTELGT", "Intel Graphics Technology");
break;
case EM_KM32:
print_field_info("EM_KM32", "KM211 KM32");
break;
case EM_KMX32:
print_field_info("EM_KMX32", "KM211 KMX32");
break;
case EM_EMX16:
print_field_info("EM_EMX16", "KM211 KMX16");
break;
case EM_EMX8:
print_field_info("EM_EMX8", "KM211 KMX8");
break;
case EM_KVARC:
print_field_info("EM_KVARC", "KM211 KVARC");
break;
case EM_CDP:
print_field_info("EM_CDP", "Paneve CDP");
break;
case EM_COGE:
print_field_info("EM_COGE", "Cognitive Smart Memory Processor");
break;
case EM_COOL:
print_field_info("EM_COOL", "Bluechip CoolEngine");
break;
case EM_NORC:
print_field_info("EM_NORC", "Nanoradio Optimized RISC");
break;
case EM_CSR_KALIMBA:
print_field_info("EM_CSR_KALIMBA", "CSR Kalimba");
break;
case EM_Z80:
print_field_info("EM_Z80", "Zilog Z80");
break;
case EM_VISIUM:
print_field_info("EM_VISIUM", "Controls and Data Services VISIUMcore");
break;
case EM_FT32:
print_field_info("EM_FT32", "FTDI Chip FT32");
break;
case EM_MOXIE:
print_field_info("EM_MOXIE", "Moxie processor");
break;
case EM_AMDGPU:
print_field_info("EM_AMDGPU", "AMD GPU");
break;
case EM_RISCV:
print_field_info("EM_RISCV", "RISC-V");
break;
case EM_BPF:
print_field_info("EM_BPF", "Linux BPF -- in-kernel virtual machine");
break;
case EM_CSKY:
print_field_info("EM_CSKY", "C-SKY");
break;
case EM_ALPHA:
print_field_info("EM_ALPHA", "Alpha");
break;
default:
if(!no_color_opt)
printf(C_GREEN "%#x" C_END, ehdr.e_machine);
else
printf("%#x", ehdr.e_machine);
puts(" (unknown)");
}
// object file version
print_field("e_version", "%x", ehdr.e_version);
// entry point virtual address
print_field("e_entry", "%#lx", ehdr.e_entry);
// program header table file offset
print_field("e_phoff", "%#lx", ehdr.e_phoff);
// section header table file offset
print_field("e_shoff", "%#lx", ehdr.e_shoff);
// processor-specific flags
print_field("e_flags", "%#x", ehdr.e_flags);
// ELF header size in bytes
print_field("e_ehsize", "%d", ehdr.e_ehsize);
// program header table entry size
print_field("e_phentsize", "%d", ehdr.e_phentsize);
// program header table entry count
print_field("e_phnum", NULL);
if(!no_color_opt)
printf("%s", C_GREEN);
// handle when phnum is too large to fit into e_phnum
if(ehdr.e_phnum == PN_XNUM) {
Elf_Scn *section = NULL;
GElf_Shdr shdr;
section = elf_getscn(elf, 0);
if(!section) {
print_error("elf_getscn() failed: %s", elf_errmsg(-1));
exit(EXIT_FAILURE);
}
if(!gelf_getshdr(section, &shdr)) {
print_error("gelf_getshdr() failed: %s", elf_errmsg(-1));
exit(EXIT_FAILURE);
}
printf("%d\n", shdr.sh_info);
} else
printf("%d\n", ehdr.e_phnum);
if(!no_color_opt)
printf("%s", C_END);
// section header table entry size
print_field("e_shentsize", "%d", ehdr.e_shentsize);
// section header table entry count
print_field("e_shnum", "%d", ehdr.e_shnum);
// section header string table index
print_field("e_shstrndx", "%d", ehdr.e_shstrndx);
putchar('\n');
// display the e_ident array in details
print_title("Elf_Ehdr.e_ident");
// file identification byte 0..3 index
print_field("EI_MAG0", "%#x", ehdr.e_ident[EI_MAG0]);
print_field("EI_MAG1", "%c", ehdr.e_ident[EI_MAG1]);
print_field("EI_MAG2", "%c", ehdr.e_ident[EI_MAG2]);
print_field("EI_MAG3", "%c", ehdr.e_ident[EI_MAG3]);
// file class byte index
print_field("EI_CLASS", NULL);
switch(ehdr.e_ident[EI_CLASS]) {
case ELFCLASSNONE:
print_field_info("ELFCLASSNONE", "invalid class");
break;
case ELFCLASS32:
print_field_info("ELFCLASS32", "32-bit object");
break;
case ELFCLASS64:
print_field_info("ELFCLASS64", "64-bit object");
break;
default:
if(!no_color_opt)
printf(C_GREEN "%#x" C_END, ehdr.e_ident[EI_CLASS]);
else
printf("%#x", ehdr.e_ident[EI_CLASS]);
puts(" (unknown)");
}
// data encoding byte index
print_field("EI_DATA", NULL);
switch(ehdr.e_ident[EI_DATA]) {
case ELFDATANONE:
print_field_info("ELFDATANONE", "invalid data encoding");
break;
case ELFDATA2LSB:
print_field_info("ELFDATA2LSB", "2's complement, little endian");
break;
case ELFDATA2MSB:
print_field_info("ELFDATA2MSB", "2's complement, big endian");
break;
default:
if(!no_color_opt)
printf(C_GREEN "%#x" C_END, ehdr.e_ident[EI_DATA]);
else
printf("%#x", ehdr.e_ident[EI_DATA]);
puts(" (unknown)");
}
// file version byte index
print_field("EI_VERSION", NULL);
switch(ehdr.e_ident[EI_VERSION]) {
case EV_NONE:
print_field_info("EV_NONE", "invalid ELF version");
break;
case EV_CURRENT:
print_field_info("EV_CURRENT", "current version");
break;
default:
if(!no_color_opt)
printf(C_GREEN "%#x" C_END, ehdr.e_ident[EI_VERSION]);
else
printf("%#x", ehdr.e_ident[EI_VERSION]);
puts(" (unknown)");
}
// OS ABI identification
print_field("EI_OSABI", NULL);
switch(ehdr.e_ident[EI_OSABI]) {
case ELFOSABI_SYSV:
print_field_info("ELFOSABI_SYSV", "UNIX System V");
break;
case ELFOSABI_HPUX:
print_field_info("ELFOSABI_HPUX", "HP-UX");
break;
case ELFOSABI_NETBSD:
print_field_info("ELFOSABI_NETBSD", "NetBSD");
break;
case ELFOSABI_GNU:
print_field_info("ELFOSABI_GNU", "object uses GNU ELF extensions");
break;
case ELFOSABI_SOLARIS:
print_field_info("ELFOSABI_SOLARIS", "Sun Solaris");
break;
case ELFOSABI_AIX:
print_field_info("ELFOSABI_AIX", "IBM AIX");
break;
case ELFOSABI_IRIX:
print_field_info("ELFOSABI_IRIX", "SGI Irix");
break;
case ELFOSABI_FREEBSD:
print_field_info("ELFOSABI_FREEBSD", "FreeBSD");
break;
case ELFOSABI_TRU64:
print_field_info("ELFOSABI_TRU64", "Compaq TRU64 UNIX");
break;
case ELFOSABI_MODESTO:
print_field_info("ELFOSABI_MODESTO", "Novell Modesto");
break;
case ELFOSABI_OPENBSD:
print_field_info("ELFOSABI_OPENBSD", "OpenBSD");
break;
case ELFOSABI_ARM_AEABI:
print_field_info("ELFOSABI_ARM_AEABI", "ARM EABI");
break;
case ELFOSABI_ARM:
print_field_info("ELFOSABI_ARM", "ARM");
break;
case ELFOSABI_STANDALONE:
print_field_info("ELFOSABI_STANDALONE", "standalone (embedded) application");
break;
default:
if(!no_color_opt)
printf(C_GREEN "%#x" C_END, ehdr.e_ident[EI_OSABI]);
else
printf("%#x", ehdr.e_ident[EI_OSABI]);
puts(" (unknown)");
}
// ABI version
print_field("EI_ABIVERSION", "%#x", ehdr.e_ident[EI_ABIVERSION]);
// byte index of padding bytes
print_field("EI_PAD", "%#x", ehdr.e_ident[EI_PAD]);
}
// display the program headers (option -p)
void show_program_headers(Elf *elf) {
size_t num;
print_title("Program Headers\n");
// strlen("p_filesz")
field_max_len = 8;
// get the number of program headers
if(elf_getphdrnum(elf, &num) != 0) {
print_error("elf_getphdrnum() failed: %s", elf_errmsg(-1));
exit(EXIT_FAILURE);
}
for(size_t i = 0; i < num; i++) {
GElf_Phdr phdr;
// get the program header
if(!gelf_getphdr(elf, i, &phdr)) {
print_error("gelf_getphdr() failed: %s", elf_errmsg(-1));
exit(EXIT_FAILURE);
}
print_title("Elf_Phdr %zu", i);
// segment type
print_field("p_type", NULL);
switch(phdr.p_type) {
case PT_LOAD:
print_field_info("PT_LOAD", "loadable program segment");
break;
case PT_DYNAMIC:
print_field_info("PT_DYNAMIC", "dynamic linking information");
break;
case PT_INTERP:
print_field_info("PT_INTERP", "program interpreter");
break;
case PT_NOTE:
print_field_info("PT_NOTE", "auxiliary information");
break;
case PT_SHLIB:
print_field_info("PT_SHLIB", "reserved");
break;
case PT_PHDR:
print_field_info("PT_PHDR", "entry for the header table itself");
break;
case PT_TLS:
print_field_info("PT_TLS", "thread-local storage segment");
break;
case PT_GNU_EH_FRAME:
print_field_info("PT_GNU_EH_FRAME", "GCC .eh_frame_hdr segment");
break;
case PT_GNU_STACK:
print_field_info("PT_GNU_STACK", "indicates stack executability");
break;
case PT_GNU_RELRO:
print_field_info("PT_GNU_RELRO", "read-only after relocation");
break;
case PT_GNU_PROPERTY:
print_field_info("PT_GNU_PROPERTY", "GNU property");
break;
default:
if(!no_color_opt)
printf(C_GREEN "%#x" C_END, phdr.p_type);
else
printf("%#x", phdr.p_type);
if((phdr.p_type >= PT_LOOS) && (phdr.p_type <= PT_HIOS))
puts(" (os-specific)");
else if(phdr.p_type >= PT_LOPROC)
puts(" (processor-specific)");
else
puts(" (unknown)");
}
// segment flags
print_field("p_flags", NULL);
switch(phdr.p_flags) {
case PF_R:
print_field_info("PF_R", "segment is readable");
break;