-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathelf.c
2055 lines (1662 loc) · 48.8 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
// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include <ctype.h>
#include <myst/buf.h>
#include <myst/elf.h>
#include <myst/round.h>
#include <myst/strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define STATIC_ASSERT(COND) _Static_assert(COND, "")
#define ADD_OVERFLOW(A, B, C) __builtin_add_overflow(A, B, C)
#define GOTO(LABEL) \
do \
{ \
fprintf(stderr, "GOTO: %s(%u)\n", __FILE__, __LINE__); \
goto LABEL; \
} while (0)
int elf_test_header(const elf_ehdr_t* ehdr)
{
if (!ehdr)
return -1;
if (ehdr->e_ident[EI_MAG0] != 0x7f)
return -1;
if (ehdr->e_ident[EI_MAG1] != 'E')
return -1;
if (ehdr->e_ident[EI_MAG2] != 'L')
return -1;
if (ehdr->e_ident[EI_MAG3] != 'F')
return -1;
if (ehdr->e_ident[EI_CLASS] != ELFCLASS64)
return -1;
if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
return -1;
if (ehdr->e_machine != EM_X86_64)
return -1;
if (ehdr->e_ehsize != sizeof(elf_ehdr_t))
return -1;
if (ehdr->e_phentsize != sizeof(elf_phdr_t))
return -1;
if (ehdr->e_shentsize != sizeof(elf_shdr_t))
return -1;
/* If there is no section header table, then the index should be 0. */
if (ehdr->e_shnum == 0 && ehdr->e_shstrndx != 0)
return -1;
/* If there is a section header table, then the index shouldn't overrun. */
if (ehdr->e_shnum > 0 && ehdr->e_shstrndx >= ehdr->e_shnum)
return -1;
return 0;
}
static bool _is_valid_elf64(const elf_t* elf)
{
if (!elf || elf->size < sizeof(elf_ehdr_t))
return false;
elf_ehdr_t* header = (elf_ehdr_t*)elf->data;
if (elf_test_header(header) != 0)
return false;
/* Ensure that multiplying header size and num entries won't overflow. */
STATIC_ASSERT(
sizeof(uint64_t) >=
sizeof(header->e_phentsize) + sizeof(header->e_phnum));
STATIC_ASSERT(
sizeof(uint64_t) >=
sizeof(header->e_shentsize) + sizeof(header->e_shnum));
uint64_t size = (uint64_t)header->e_phentsize * header->e_phnum;
uint64_t end;
/* Check that offsets don't overflow. */
if (ADD_OVERFLOW(header->e_phoff, size, &end))
return false;
if (elf->size < end)
return false;
size = (uint64_t)header->e_shentsize * header->e_shnum;
if (ADD_OVERFLOW(header->e_shoff, size, &end))
return false;
if (elf->size < end)
return false;
return true;
}
static elf_ehdr_t* _get_header(const elf_t* elf)
{
return (elf_ehdr_t*)elf->data;
}
static elf_shdr_t* _get_shdr(const elf_t* elf, size_t index)
{
elf_ehdr_t* header = _get_header(elf);
elf_shdr_t* shdr_start =
(elf_shdr_t*)((uint8_t*)elf->data + header->e_shoff);
if (index >= header->e_shnum)
return NULL;
elf_shdr_t* shdr = &shdr_start[index];
/* Check section header segment is within the elf executable.
* There are no bounds checking for `SHT_NULL` and `SHT_NOBITS`,
* because they don't have meaningful offset or size values. */
if (shdr->sh_type == SHT_NULL || shdr->sh_type == SHT_NOBITS)
return shdr;
uint64_t end;
if (ADD_OVERFLOW(shdr->sh_offset, shdr->sh_size, &end))
return NULL;
return (end <= elf->size) ? shdr : NULL;
}
static elf_phdr_t* _get_phdr(const elf_t* elf, size_t index)
{
elf_ehdr_t* header = _get_header(elf);
elf_phdr_t* phdr_start =
(elf_phdr_t*)((uint8_t*)elf->data + header->e_phoff);
if (index >= header->e_phnum)
return NULL;
elf_phdr_t* phdr = &phdr_start[index];
/* Check program header segment section is within the elf executable.
* There are no bounds checks for `PT_NULL`, because it doesn't have
* a meaningful offset or size value. */
if (phdr->p_type == PT_NULL)
return phdr;
uint64_t end;
if (ADD_OVERFLOW(phdr->p_offset, phdr->p_filesz, &end))
return NULL;
return (end <= elf->size) ? phdr : NULL;
}
static void* _get_section(const elf_t* elf, size_t index)
{
const elf_shdr_t* sh = _get_shdr(elf, index);
if (sh == NULL)
return NULL;
return (sh->sh_type == SHT_NULL || sh->sh_type == SHT_NOBITS)
? NULL
: (uint8_t*)elf->data + sh->sh_offset;
}
static void* _get_segment(const elf_t* elf, size_t index)
{
const elf_phdr_t* ph = _get_phdr(elf, index);
if (ph == NULL)
return NULL;
return ph->p_type == PT_NULL ? NULL : (uint8_t*)elf->data + ph->p_offset;
}
elf_ehdr_t* elf_get_header(const elf_t* elf)
{
if (!_is_valid_elf64(elf))
return NULL;
return _get_header(elf);
}
void* elf_get_segment(const elf_t* elf, size_t index)
{
if (!_is_valid_elf64(elf))
return NULL;
return _get_segment(elf, index);
}
elf_shdr_t* elf_get_section_header(const elf_t* elf, size_t index)
{
if (!_is_valid_elf64(elf))
return NULL;
return _get_shdr(elf, index);
}
elf_phdr_t* elf_get_program_header(const elf_t* elf, size_t index)
{
if (!_is_valid_elf64(elf))
return NULL;
return _get_phdr(elf, index);
}
/* Add adjustment value to all section offsets greater than OFFSET */
static void _adjust_section_header_offsets(
elf_t* elf,
size_t offset,
ssize_t adjustment)
{
size_t i;
elf_ehdr_t* ehdr = _get_header(elf);
/* Adjust section header offset */
if (ehdr->e_shoff >= offset)
ehdr->e_shoff += (uint64_t)adjustment;
elf_shdr_t* shdrs = (elf_shdr_t*)((uint8_t*)elf->data + ehdr->e_shoff);
/* Adjust offsets to individual headers */
for (i = 0; i < ehdr->e_shnum; i++)
{
elf_shdr_t* sh = &shdrs[i];
if (sh->sh_offset >= offset)
sh->sh_offset += (uint64_t)adjustment;
}
}
static int _reset_buffer(
elf_t* elf,
myst_buf_t* buf,
size_t offset,
ssize_t adjustment)
{
/* Reset the pointers to the ELF image */
elf->data = buf->data;
elf->size = buf->size;
/* Adjust section header offsets greater than offset */
_adjust_section_header_offsets(elf, offset, adjustment);
return 0;
}
int elf_from_buffer(void* buffer, size_t buffer_length, elf_t* elf)
{
int rc = -1;
if (!buffer || !elf)
{
goto done;
}
memset(elf, 0, sizeof(elf_t));
elf->size = buffer_length;
elf->data = buffer;
/* Validate the ELF file. */
if (!_is_valid_elf64(elf))
goto done;
/* Set the magic number */
elf->magic = ELF_MAGIC;
rc = 0;
done:
return rc;
}
int elf_load(const char* path, elf_t* elf)
{
int rc = -1;
FILE* is = NULL;
int fd = -1;
#if defined(_MSC_VER)
struct __stat64 statbuf;
#else
struct stat statbuf;
#endif
if (elf)
memset(elf, 0, sizeof(elf_t));
if (!path || !elf)
goto done;
/* Open input file */
if (!(is = fopen(path, "rb")))
goto done;
#if defined(_MSC_VER)
fd = _fileno(is);
if (fd == -1 || _fstat64(fd, &statbuf) != 0)
goto done;
if ((statbuf.st_mode & _S_IFREG) == 0)
goto done;
#else
fd = fileno(is);
if (fd == -1 || fstat(fd, &statbuf) != 0)
goto done;
/* Reject non-regular files */
if (!S_ISREG(statbuf.st_mode))
goto done;
#endif
/* Store the size of this file */
elf->size = (size_t)statbuf.st_size;
/* Allocate the data to hold this image */
if (!(elf->data = malloc(elf->size)))
goto done;
/* Read the file into memory */
if (fread(elf->data, 1, elf->size, is) != elf->size)
goto done;
/* Validate the ELF file. */
if (!_is_valid_elf64(elf))
goto done;
/* Set the magic number */
elf->magic = ELF_MAGIC;
rc = 0;
done:
if (is)
fclose(is);
if (rc != 0 && elf)
{
free(elf->data);
memset(elf, 0, sizeof(elf_t));
}
return rc;
}
int elf_unload(elf_t* elf)
{
int rc = -1;
if (!_is_valid_elf64(elf))
goto done;
free(elf->data);
rc = 0;
done:
return rc;
}
static size_t _find_shdr(const elf_t* elf, const char* name)
{
size_t result = (size_t)-1;
size_t i;
for (i = 0; i < _get_header(elf)->e_shnum; i++)
{
const elf_shdr_t* sh = _get_shdr(elf, i);
if (sh == NULL)
goto done;
const char* s = elf_get_string_from_shstrtab(elf, sh->sh_name);
if (s && strcmp(name, s) == 0)
{
result = i;
goto done;
}
}
done:
return result;
}
static inline bool _is_valid_string_table(const char* table, size_t size)
{
/* The ELF spec requires the string table to have the first and last byte
* equal to 0. Checking if the last byte is 0 also ensures that any pointer
* within the table will be null terminated, because the table itself is
* null terminated. */
return size >= 2 && table[0] == '\0' && table[size - 1] == '\0';
}
static const char* _get_string_from_section_index(
const elf_t* elf,
size_t index,
elf_word_t offset)
{
const elf_shdr_t* sh;
const char* result = NULL;
if (index == 0 || index >= _get_header(elf)->e_shnum)
goto done;
sh = _get_shdr(elf, index);
if (sh == NULL || offset >= sh->sh_size)
goto done;
/* If the section is null, the elf file is corrupted, since only `SHT_NULL`
* and `SHT_NOBITS` can have nonexistent sections. */
result = (const char*)_get_section(elf, index);
if (result == NULL)
goto done;
if (!_is_valid_string_table(result, sh->sh_size))
goto done;
result += offset;
done:
return result;
}
const char* elf_get_string_from_strtab(const elf_t* elf, elf_word_t offset)
{
size_t index;
if (!_is_valid_elf64(elf))
return NULL;
if ((index = _find_shdr(elf, ".strtab")) == (size_t)-1)
return NULL;
return _get_string_from_section_index(elf, index, offset);
}
const char* elf_get_string_from_shstrtab(const elf_t* elf, elf_word_t offset)
{
size_t index;
if (!_is_valid_elf64(elf))
return NULL;
index = _get_header(elf)->e_shstrndx;
return _get_string_from_section_index(elf, index, offset);
}
int elf_find_symbol_by_name(const elf_t* elf, const char* name, elf_sym_t* sym)
{
int rc = -1;
size_t index;
const elf_shdr_t* sh;
const elf_sym_t* symtab;
size_t n;
size_t i;
const char* SECTIONNAME = ".symtab";
const elf_word_t SH_TYPE = SHT_SYMTAB;
if (!_is_valid_elf64(elf) || !name || !sym)
goto done;
/* Find the symbol table section header */
if ((index = _find_shdr(elf, SECTIONNAME)) == (size_t)-1)
goto done;
if (index == 0 || index >= _get_header(elf)->e_shnum)
goto done;
/* Set pointer to section header */
if (!(sh = _get_shdr(elf, index)))
goto done;
/* If this is not a symbol table */
if (sh->sh_type != SH_TYPE)
goto done;
/* Sanity check */
if (sh->sh_entsize != sizeof(elf_sym_t))
goto done;
/* Set pointer to symbol table section */
if (!(symtab = (const elf_sym_t*)_get_section(elf, index)))
goto done;
/* Calculate number of symbol table entries */
n = sh->sh_size / sh->sh_entsize;
for (i = 1; i < n; i++)
{
const elf_sym_t* p = &symtab[i];
const char* s;
/* Skip empty names */
if (p->st_name == 0)
continue;
/* If illegal name */
if (!(s = elf_get_string_from_strtab(elf, p->st_name)))
goto done;
/* If found */
if (strcmp(name, s) == 0)
{
*sym = *p;
rc = 0;
goto done;
}
}
done:
return rc;
}
int elf_get_dynamic_symbol_table(
const elf_t* elf,
const elf_sym_t** symtab,
size_t* size)
{
int rc = -1;
size_t index;
const elf_shdr_t* sh;
const char* SECTIONNAME = ".dynsym";
const elf_word_t SH_TYPE = SHT_DYNSYM;
if (!_is_valid_elf64(elf) || !symtab || !size)
goto done;
*symtab = NULL;
*size = 0;
/* Find the symbol table section header */
if ((index = _find_shdr(elf, SECTIONNAME)) == (size_t)-1)
goto done;
if (index == 0 || index >= _get_header(elf)->e_shnum)
goto done;
/* Set pointer to section header */
if (!(sh = _get_shdr(elf, index)))
goto done;
/* If this is not a symbol table */
if (sh->sh_type != SH_TYPE)
goto done;
/* Sanity check */
if (sh->sh_entsize != sizeof(elf_sym_t))
goto done;
/* Set pointer to symbol table section */
if (!(*symtab = (const elf_sym_t*)_get_section(elf, index)))
goto done;
/* Calculate number of symbol table entries */
*size = sh->sh_size / sh->sh_entsize;
rc = 0;
done:
return rc;
}
const char* elf_get_string_from_dynstr(const elf_t* elf, elf_word_t offset)
{
size_t index;
if (!_is_valid_elf64(elf))
return NULL;
if ((index = _find_shdr(elf, ".dynstr")) == (size_t)-1)
return NULL;
return _get_string_from_section_index(elf, index, offset);
}
int elf_find_dynamic_symbol_by_name(
const elf_t* elf,
const char* name,
elf_sym_t* sym)
{
int rc = -1;
size_t index;
const elf_shdr_t* sh;
const elf_sym_t* symtab;
size_t n;
size_t i;
const char* SECTIONNAME = ".dynsym";
const elf_word_t SH_TYPE = SHT_DYNSYM;
if (!_is_valid_elf64(elf) || !name || !sym)
goto done;
/* Find the symbol table section header */
if ((index = _find_shdr(elf, SECTIONNAME)) == (size_t)-1)
goto done;
if (index == 0 || index >= _get_header(elf)->e_shnum)
goto done;
/* Set pointer to section header */
if (!(sh = _get_shdr(elf, index)))
goto done;
/* If this is not a symbol table */
if (sh->sh_type != SH_TYPE)
goto done;
/* Sanity check */
if (sh->sh_entsize != sizeof(elf_sym_t))
goto done;
/* Set pointer to symbol table section */
if (!(symtab = (const elf_sym_t*)_get_section(elf, index)))
goto done;
/* Calculate number of symbol table entries */
n = sh->sh_size / sh->sh_entsize;
for (i = 1; i < n; i++)
{
const elf_sym_t* p = &symtab[i];
const char* s;
/* Skip empty names */
if (p->st_name == 0)
continue;
/* If illegal name */
if (!(s = elf_get_string_from_dynstr(elf, p->st_name)))
goto done;
/* If found */
if (strcmp(name, s) == 0)
{
*sym = *p;
rc = 0;
goto done;
}
}
done:
return rc;
}
int elf_find_symbol_by_address(
const elf_t* elf,
elf_addr_t addr,
unsigned int type,
elf_sym_t* sym)
{
int rc = -1;
size_t index;
const elf_shdr_t* sh;
const elf_sym_t* symtab;
size_t n;
size_t i;
const char* SECTIONNAME = ".symtab";
const elf_word_t SH_TYPE = SHT_SYMTAB;
if (!_is_valid_elf64(elf) || !sym)
goto done;
/* Find the symbol table section header */
if ((index = _find_shdr(elf, SECTIONNAME)) == (size_t)-1)
goto done;
if (index == 0 || index >= _get_header(elf)->e_shnum)
goto done;
/* Set pointer to section header */
if (!(sh = _get_shdr(elf, index)))
goto done;
/* If this is not a symbol table */
if (sh->sh_type != SH_TYPE)
goto done;
/* Sanity check */
if (sh->sh_entsize != sizeof(elf_sym_t))
goto done;
/* Set pointer to symbol table section */
if (!(symtab = (const elf_sym_t*)_get_section(elf, index)))
goto done;
/* Calculate number of symbol table entries */
n = sh->sh_size / sh->sh_entsize;
for (i = 1; i < n; i++)
{
const elf_sym_t* p = &symtab[i];
unsigned int stt = (p->st_info & 0x0F);
if (stt != type)
continue;
if (p->st_value == addr)
{
*sym = *p;
rc = 0;
goto done;
}
}
done:
return rc;
}
int elf_find_dynamic_symbol_by_address(
const elf_t* elf,
elf_addr_t addr,
unsigned int type,
elf_sym_t* sym)
{
int rc = -1;
size_t index;
const elf_shdr_t* sh;
const elf_sym_t* symtab;
size_t n;
size_t i;
const char* SECTIONNAME = ".dynsym";
const elf_word_t SH_TYPE = SHT_DYNSYM;
if (!_is_valid_elf64(elf) || !sym)
goto done;
/* Find the symbol table section header */
if ((index = _find_shdr(elf, SECTIONNAME)) == (size_t)-1)
goto done;
if (index == 0 || index >= _get_header(elf)->e_shnum)
goto done;
/* Set pointer to section header */
if (!(sh = _get_shdr(elf, index)))
goto done;
/* If this is not a symbol table */
if (sh->sh_type != SH_TYPE)
goto done;
/* Sanity check */
if (sh->sh_entsize != sizeof(elf_sym_t))
goto done;
/* Set pointer to symbol table section */
if (!(symtab = (const elf_sym_t*)_get_section(elf, index)))
goto done;
/* Calculate number of symbol table entries */
n = sh->sh_size / sh->sh_entsize;
for (i = 1; i < n; i++)
{
const elf_sym_t* p = &symtab[i];
unsigned int stt = (p->st_info & 0x0F);
if (stt != type)
continue;
if (p->st_value == addr)
{
*sym = *p;
rc = 0;
goto done;
}
}
done:
return rc;
}
void elf_dump_header(const elf_ehdr_t* h)
{
if (!h || elf_test_header(h) != 0)
return;
printf("=== elf_ehdr_t:\n");
/* Print e_ident[] */
printf("e_ident[EI_MAG0]=%02x\n", h->e_ident[EI_MAG0]);
printf("e_ident[EI_MAG1]=%c\n", h->e_ident[EI_MAG1]);
printf("e_ident[EI_MAG2]=%c\n", h->e_ident[EI_MAG2]);
printf("e_ident[EI_MAG3]=%c\n", h->e_ident[EI_MAG3]);
switch (h->e_ident[EI_CLASS])
{
case ELFCLASSNONE:
printf("e_ident[EI_CLASS]=ELFCLASSNONE\n");
break;
case ELFCLASS32:
printf("e_ident[EI_CLASS]=ELFCLASS32\n");
break;
case ELFCLASS64:
printf("e_ident[EI_CLASS]=ELFCLASS64\n");
break;
default:
printf("e_ident[EI_CLASS]=%02x\n", h->e_ident[EI_CLASS]);
break;
}
switch (h->e_ident[EI_DATA])
{
case ELFDATANONE:
printf("e_ident[EI_DATA]=ELFDATANONE\n");
break;
case ELFDATA2LSB:
printf("e_ident[EI_DATA]=ELFDATA2LSB\n");
break;
case ELFDATA2MSB:
printf("e_ident[EI_DATA]=ELFDATA2MSB\n");
break;
default:
printf("e_ident[EI_DATA]=%02x\n", h->e_ident[EI_DATA]);
break;
}
printf("e_ident[EI_VERSION]=%02x\n", h->e_ident[EI_VERSION]);
printf("e_ident[EI_PAD]=%02x\n", h->e_ident[EI_PAD]);
switch (h->e_type)
{
case ET_NONE:
printf("e_type=ET_NONE\n");
break;
case ET_REL:
printf("e_type=ET_REL\n");
break;
case ET_EXEC:
printf("e_type=ET_EXEC\n");
break;
case ET_DYN:
printf("e_type=ET_DYN\n");
break;
case ET_CORE:
printf("e_type=ET_CORE\n");
break;
case ET_LOPROC:
printf("e_type=ET_LOPROC\n");
break;
case ET_HIPROC:
printf("e_type=ET_HIPROC\n");
break;
default:
printf("e_type=%02x\n", h->e_type);
break;
}
switch (h->e_machine)
{
case EM_NONE:
printf("e_machine=EM_NONE\n");
break;
case EM_M32:
printf("e_machine=EM_M32\n");
break;
case EM_SPARC:
printf("e_machine=EM_SPARC\n");
break;
case EM_386:
printf("e_machine=EM_386\n");
break;
case EM_68K:
printf("e_machine=EM_68K\n");
break;
case EM_88K:
printf("e_machine=EM_88K\n");
break;
case EM_860:
printf("e_machine=EM_860\n");
break;
case EM_MIPS:
printf("e_machine=EM_MIPS\n");
break;
case EM_X86_64:
printf("e_machine=EM_X86_64\n");
break;
default:
printf("e_machine=%u\n", h->e_machine);
break;
}
printf("e_version=%u\n", h->e_version);
printf("e_entry=%lx\n", h->e_entry);
printf("e_phoff=%lu\n", h->e_phoff);
printf("e_shoff=%lu\n", h->e_shoff);
printf("e_flags=%u\n", h->e_flags);
printf("e_ehsize=%u\n", h->e_ehsize);
printf("e_phentsize=%u\n", h->e_phentsize);
printf("e_phnum=%u\n", h->e_phnum);
printf("e_shentsize=%u\n", h->e_shentsize);
printf("e_shnum=%u\n", h->e_shnum);
printf("e_shstrndx=%u\n", h->e_shstrndx);
printf("\n");
}
void elf_dump_shdr(const elf_shdr_t* sh, size_t index)
{
if (!sh)
return;
printf("=== elf_shdr_t[%03zu]:\n", index);
printf("sh_name=%u\n", sh->sh_name);
printf("sh_type=%u\n", sh->sh_type);
{
size_t n = 0;
printf("sh_flags=");
if (sh->sh_flags & SHF_ALLOC)
{
if (n++)
printf("|");
printf("SHF_ALLOC");
}
if (sh->sh_flags & SHF_EXECINSTR)
{
if (n++)
printf("|");
printf("SHF_EXECINSTR");
}
if (sh->sh_flags & SHF_MASKOS)
{
if (n++)
printf("|");
printf("SHF_MASKOS");
}
if (sh->sh_flags & SHF_MASKPROC)
{
if (n++)
printf("|");
printf("SHF_MASKPROC");
}
printf("\n");
}
printf("sh_addr=%lu\n", sh->sh_addr);
printf("sh_offset=%lu\n", sh->sh_offset);
printf("sh_size=%lu\n", sh->sh_size);
printf("sh_link=%u\n", sh->sh_link);
printf("sh_info=%u\n", sh->sh_info);
printf("sh_addralign=%lu\n", sh->sh_addralign);
printf("sh_entsize=%lu\n", sh->sh_entsize);
printf("\n");
}
static size_t _find_segment_for(const elf_t* elf, const elf_shdr_t* sh)
{
size_t i = 0;
for (i = 0; i < _get_header(elf)->e_phnum; i++)
{
const elf_phdr_t* ph = _get_phdr(elf, i);
if (ph == NULL)
return (size_t)-1;
if (sh->sh_offset >= ph->p_offset && sh->sh_size <= ph->p_memsz)
return i;
}
return (size_t)-1;
}
int elf_dump_sections(const elf_t* elf)
{
int rc = -1;
size_t i;
if (!_is_valid_elf64(elf))
goto done;
if (elf_test_header(_get_header(elf)) != 0)
goto done;
puts("Num Name Offset Size "
"Seg ");
puts("====================================================================="
"=");
for (i = 0; i < _get_header(elf)->e_shnum; i++)
{
const elf_shdr_t* sh = _get_shdr(elf, i);
if (sh == NULL)
{
printf("[%03zu]: Invalid Section.\n", i);