-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymbol.c
1708 lines (1615 loc) · 51.6 KB
/
symbol.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 2023 [email protected]. All rights reserved.
// Use of this source code is governed by a Anti-996 style
// license that can be found in the LICENSE file.
//
//===----------------------------------------------------------------------===//
#include "symbol.h"
/* each of the exe_* functions have to be reentrant and thread-safe */
static int symbol_convert_type(int info) {
int value = ELF_ST_TYPE(info);
if (value == STT_FUNC) {
return SYMBOL_IS_FUNCTION;
} else if (value == STT_FILE) {
return SYMBOL_IS_FILENAME;
} else if (value == STT_SECTION) {
return SYMBOL_IS_SECTION;
} else if (value == STT_OBJECT) {
return SYMBOL_IS_OBJECT;
} else {
return SYMBOL_IS_UNKNOWN;
}
}
static long symbol_get_file_size(const char* filename) {
long filesize = 0;
struct stat statbuff;
if (stat(filename, &statbuff) < 0) {
return filesize;
} else {
filesize = statbuff.st_size;
}
return filesize;
}
int symbol_open_filename(const char* filename) {
int fd = -1;
fd = open(filename, O_RDONLY);
if (fd < 0) {
LOG(LOG_ERR, "open file(%s) : %s", filename, strerror(errno));
}
return fd;
}
static int symbol_elf_symbol_cmpqsort(const void* p1, const void* p2) {
return strcmp(((const struct symbol_elf_sym*)p1)->name,
((const struct symbol_elf_sym*)p2)->name);
}
static int symbol_elf_identify(unsigned char* e_ident, size_t size) {
if (e_ident && size > 0) {
if ((e_ident[EI_MAG0] == ELFMAG0) && (e_ident[EI_MAG1] == ELFMAG1) &&
(e_ident[EI_MAG2] == ELFMAG2) && (e_ident[EI_MAG3] == ELFMAG3)) {
int is64 = ELF_IS_NEITHER;
/* magic number says this is an ELF file */
switch (e_ident[EI_CLASS]) {
case ELFCLASS32:
is64 = ELF_IS_32BIT;
/* LOG(LOG_DEBUG, "File is 32-bit ELF."); */
break;
case ELFCLASS64:
is64 = ELF_IS_64BIT;
/* LOG(LOG_DEBUG, "File is 64-bit ELF."); */
break;
case ELFCLASSNONE:
default:
is64 = ELF_IS_NEITHER;
/* LOG(LOG_DEBUG, "File is not ELF."); */
break;
}
if (is64 != ELF_IS_NEITHER) {
int isbigendian = -1;
int iscurrent = 0;
int islinux = 0;
switch (e_ident[EI_DATA]) {
case ELFDATA2LSB:
isbigendian = 0;
/* LOG(LOG_DEBUG, "File is Little endian format."); */
break;
case ELFDATA2MSB:
isbigendian = 1;
/* LOG(LOG_DEBUG, "File is Big endian format."); */
break;
case ELFDATANONE:
default:
isbigendian = -1;
/* LOG(LOG_DEBUG, "File is Unknown endian format."); */
break;
}
if (e_ident[EI_VERSION] == EV_CURRENT) {
iscurrent = 1;
/* LOG(LOG_DEBUG, "File is Current ELF format."); */
}
/* LOG(LOG_DEBUG, "File ELFOSABI: %d.", e_ident[EI_OSABI]); */
if (e_ident[EI_OSABI] == ELFOSABI_LINUX ||
e_ident[EI_OSABI] == ELFOSABI_SYSV) {
islinux = 1;
/* LOG(LOG_DEBUG, "File OS ABI is Linux."); */
}
if (islinux && isbigendian == 0 && iscurrent) {
return is64;
}
LOG(LOG_ERR, "Not an acceptable header.");
}
} else {
LOG(LOG_ERR, "This is not an ELF file format.");
}
}
return ELF_IS_NEITHER;
}
void* symbol_elf_load_section_strings(struct symbol_elf_internals* ei,
Elf_Shdr* strh, size_t* outtotalsize) {
char* strsymtbl = NULL;
size_t strsymtbl_size = 0;
if (!ei || !strh || !outtotalsize) {
return NULL;
}
if (strh->sh_size <= 0) {
LOG(LOG_ERR, "Read strings section error totalsize<%d>", strh->sh_size);
return NULL;
}
strsymtbl_size = strh->sh_size + 0;
strsymtbl = malloc(strh->sh_size);
if (!strsymtbl) {
LOG(LOG_ERR, "malloc error: size %d, %s", strh->sh_size, strerror(errno));
return NULL;
}
if (lseek(ei->fd, strh->sh_offset, SEEK_SET) < 0) {
LOG(LOG_ERR, "lseek error: fd %d, %s", ei->fd, strerror(errno));
free(strsymtbl);
strsymtbl = NULL;
return NULL;
}
if (read(ei->fd, strsymtbl, strh->sh_size) < 0) {
LOG(LOG_ERR, "read error: fd %d, %s", ei->fd, strerror(errno));
free(strsymtbl);
strsymtbl = NULL;
return NULL;
}
*outtotalsize = strsymtbl_size;
return strsymtbl;
}
void* symbol_elf_load_section_tables(struct symbol_elf_internals* ei,
Elf_Shdr* sechdr, size_t* outnum) {
void* r_tabs = NULL;
int rc = 0;
if (!ei || !sechdr || !outnum) {
return NULL;
}
if (sechdr->sh_entsize <= 0 || sechdr->sh_size <= 0) {
LOG(LOG_ERR, "Read section tables error entsize<%d> totalsize<%d>",
sechdr->sh_entsize, sechdr->sh_size);
return NULL;
}
do {
size_t sym_num = sechdr->sh_size / sechdr->sh_entsize;
r_tabs = malloc(sechdr->sh_size);
if (!r_tabs) {
LOG(LOG_ERR, "malloc error: size %d, %s", sechdr->sh_size,
strerror(errno));
rc = -1;
break;
}
if (lseek(ei->fd, sechdr->sh_offset, SEEK_SET) < 0) {
LOG(LOG_ERR, "lseek error: fd %d, %s", ei->fd, strerror(errno));
free(r_tabs);
r_tabs = NULL;
rc = -1;
break;
}
if (read(ei->fd, r_tabs, sechdr->sh_size) < 0) {
LOG(LOG_ERR, "read error: fd %d, %s", ei->fd, strerror(errno));
free(r_tabs);
r_tabs = NULL;
rc = -1;
break;
}
*outnum = sym_num;
} while (0);
if (rc < 0 || !r_tabs) {
return NULL;
}
return r_tabs;
}
static int symbol_elf_load_sym_table(struct symbol_elf_internals* ei,
Elf_Shdr* symh, Elf_Shdr* strh) {
char* strsymtbl = NULL;
while (ei && symh && strh) {
/* LOG(LOG_DEBUG, "Retrieving symbol table."); */
if (lseek(ei->fd, strh->sh_offset, SEEK_SET) < 0) {
LOG(LOG_ERR, "lseek error: fd %d, %s", ei->fd, strerror(errno));
break;
}
strsymtbl = malloc(strh->sh_size);
if (!strsymtbl) {
LOG(LOG_ERR, "malloc error: size %d, %s", strh->sh_size, strerror(errno));
break;
}
if (read(ei->fd, strsymtbl, strh->sh_size) < 0) {
LOG(LOG_ERR, "read error: fd %d, %s", ei->fd, strerror(errno));
free(strsymtbl);
strsymtbl = NULL;
break;
}
if (symh->sh_entsize > 0 && symh->sh_size > 0) {
size_t idx;
size_t sym_num = symh->sh_size / symh->sh_entsize;
Elf_Sym* syms = malloc(symh->sh_size);
if (!syms) {
LOG(LOG_ERR, "malloc error: size %d, %s", symh->sh_size,
strerror(errno));
break;
}
if (lseek(ei->fd, symh->sh_offset, SEEK_SET) < 0) {
LOG(LOG_ERR, "lseek error: fd %d, %s", ei->fd, strerror(errno));
free(syms);
break;
}
if (read(ei->fd, syms, symh->sh_size) < 0) {
LOG(LOG_ERR, "read error: fd %d, %s", ei->fd, strerror(errno));
free(syms);
break;
}
/* there might already exist symbols from another section.
* hence using realloc() takes care of that.
* */
ei->symbols = realloc(ei->symbols,
(sym_num + ei->symbols_num) * sizeof(*ei->symbols));
if (!ei->symbols) {
LOG(LOG_ERR, "malloc error: size %d, %s", strh->sh_size,
strerror(errno));
break;
}
memset(&ei->symbols[ei->symbols_num], 0, sizeof(*ei->symbols) * sym_num);
/* LOG(LOG_DEBUG, "Symbol-sechdr size<%d>, entsize<%d>, offset<%d>",
* symh->sh_size, symh->sh_entsize, symh->sh_offset); */
/* index 0 is always NULL */
for (idx = 1; idx < sym_num; ++idx) {
if (syms[idx].st_shndx == SHN_UNDEF) {
continue;
}
/*
LOG(LOG_DEBUG, "Symbol-info idx<%d> info<%d>, nameidx<%d> symb<%p>",
idx, syms[idx].st_info, syms[idx].st_name, &syms[idx]);
*/
const char* name =
syms[idx].st_name > 0 ? &strsymtbl[syms[idx].st_name] : "";
if (name) {
char* name2;
int symtype = symbol_convert_type(syms[idx].st_info);
/*
LOG(LOG_DEBUG,"Symbol %u is %s at %p type %d size %u",
idx, name, (void *)syms[idx].st_value, symtype,
syms[idx].st_size);
*/
name2 = strdup(name);
if (!name2) {
LOG(LOG_ERR, "malloc error: size %d, %s", strh->sh_size,
strerror(errno));
continue;
}
ei->symbols[ei->symbols_num].name = name2;
ei->symbols[ei->symbols_num].address = (uintptr_t)syms[idx].st_value;
ei->symbols[ei->symbols_num].size = (size_t)syms[idx].st_size;
ei->symbols[ei->symbols_num].type = symtype;
ei->symbols[ei->symbols_num].sym = syms[idx];
ei->symbols_num++;
}
}
free(syms);
if (strsymtbl) {
free(strsymtbl);
}
return 0;
}
}
if (strsymtbl) {
free(strsymtbl);
}
return -1;
}
static int symbol_elf_load_section_headers_and_symtabs(
struct symbol_elf_internals* ei) {
Elf_Shdr* strsectblhdr = NULL;
Elf_Shdr* sechdrs = NULL;
size_t idx = 0;
ssize_t symtab = -1;
ssize_t strtab = -1;
if (!ei || ei->sechdr_offset == 0 || ei->sechdr_size == 0) {
LOG(LOG_ERR, "param error!");
return -1;
}
/* LOG(LOG_DEBUG, "Retrieving section headers."); */
ei->sechdrs = malloc(ei->sechdr_size);
if (!ei->sechdrs) {
LOG(LOG_ERR, "malloc error: size %d, %s", ei->sechdr_size, strerror(errno));
return -1;
}
memset(ei->sechdrs, 0, ei->sechdr_size);
/* LOG(LOG_DEBUG, "Reading section header offset at %u",
* (size_t)ei->sechdr_offset); */
if (lseek(ei->fd, ei->sechdr_offset, SEEK_SET) < 0) {
LOG(LOG_ERR, "lseek error: fd %d, %s", ei->fd, strerror(errno));
return -1;
}
if (read(ei->fd, ei->sechdrs, ei->sechdr_size) < 0) {
LOG(LOG_ERR, "read error: fd %d, %s", ei->fd, strerror(errno));
return -1;
}
sechdrs = (Elf_Shdr*)ei->sechdrs;
strsectblhdr = &sechdrs[ei->secnametbl_idx];
if (lseek(ei->fd, strsectblhdr->sh_offset, SEEK_SET) < 0) {
LOG(LOG_ERR, "lseek error: fd %d, %s", ei->fd, strerror(errno));
return -1;
}
ei->strsectbl = malloc(strsectblhdr->sh_size);
if (!ei->strsectbl) {
LOG(LOG_ERR, "malloc error: size %d, %s", ei->sechdr_size, strerror(errno));
return -1;
}
ei->strsectbl_size = strsectblhdr->sh_size + 0;
if (read(ei->fd, ei->strsectbl, strsectblhdr->sh_size) < 0) {
LOG(LOG_ERR, "read error: fd %d, %s", ei->fd, strerror(errno));
return -1;
}
ei->sechdrs_types = malloc(sizeof(size_t) * ei->sechdr_num);
if (!ei->sechdrs_types) {
LOG(LOG_ERR, "malloc error: size %d, %s", sizeof(size_t) * ei->sechdr_num,
strerror(errno));
return -1;
}
memset(ei->sechdrs_types, 0, sizeof(size_t) * ei->sechdr_num);
/* LOG(LOG_DEBUG, "Number of sections: %u", ei->sechdr_num); */
for (idx = 0; idx < ei->sechdr_num; ++idx) {
const char* name = &ei->strsectbl[sechdrs[idx].sh_name];
/*
if (name)
{
LOG(LOG_DEBUG, "Section name: %s Addr: %p Len: %u Idx: %u", name,
(void *)sechdrs[idx].sh_offset, sechdrs[idx].sh_size, idx);
}
else
{
LOG(LOG_DEBUG, "Section name: %s Addr: %p Len: %u Idx: %u", "N/A",
(void *)sechdrs[idx].sh_offset, sechdrs[idx].sh_size, idx);
}
*/
ei->sechdrs_types[idx] = ELF_SECT_UNSUPPORT;
switch (sechdrs[idx].sh_type) {
case SHT_SYMTAB:
case SHT_DYNSYM:
symtab = idx;
/*
LOG(LOG_DEBUG, "Symbol table offset: %u size: %u entsize: %u entries:
%u", sechdrs[idx].sh_offset, sechdrs[idx].sh_size,
sechdrs[idx].sh_entsize, (sechdrs[idx].sh_entsize > 0 ?
sechdrs[idx].sh_size / sechdrs[idx].sh_entsize : 0));
*/
break;
case SHT_STRTAB:
if (idx != ei->secnametbl_idx) {
strtab = idx;
/* LOG(LOG_DEBUG, "Reading symbol table from %s", name); */
if (symtab >= 0 && symbol_elf_load_sym_table(ei, &sechdrs[symtab],
&sechdrs[strtab]) < 0) {
LOG(LOG_ERR, "Failed to retrieve symbol table.", name);
}
symtab = -1;
}
break;
case SHT_NOBITS:
/* if(ei->type == ET_REL) { */
if (!ei->sechdr_idx_bss) {
ei->sechdr_idx_bss = idx; /* .bss */
}
ei->sechdrs_types[idx] = ELF_SECT_BSS;
/* } */
break;
case SHT_NUM: /* == SHT_COMDAT == 12*/
case SHT_PROGBITS:
/* if(ei->type == ET_REL) { */
if ((sechdrs[idx].sh_flags & SHF_ALLOC) &&
(sechdrs[idx].sh_flags & SHF_EXECINSTR)) {
if (!ei->sechdr_idx_text) {
ei->sechdr_idx_text = idx; /* .text */
}
ei->sechdrs_types[idx] = ELF_SECT_TEXT;
} else if ((sechdrs[idx].sh_flags & SHF_ALLOC) &&
(sechdrs[idx].sh_flags & SHF_WRITE)) {
if (!strcmp(".got.plt", name)) {
ei->sechdr_idx_got_plt = idx;
ei->sechdrs_types[idx] = ELF_SECT_GOT_PLT;
} else if (!strcmp(".got", name)) {
ei->sechdr_idx_got = idx;
ei->sechdrs_types[idx] = ELF_SECT_GOT;
} else {
if (!ei->sechdr_idx_data) {
ei->sechdr_idx_data = idx; /* .data */
}
ei->sechdrs_types[idx] = ELF_SECT_DATA;
}
} else if ((sechdrs[idx].sh_flags & SHF_ALLOC)) {
if (!ei->sechdr_idx_rodata) {
ei->sechdr_idx_rodata = idx; /* .rodata */
}
ei->sechdrs_types[idx] = ELF_SECT_RODATA;
}
/* } */
break;
default:
break;
}
}
if (ei->type == ET_REL) {
LOG(LOG_DEBUG,
"Find linkable sections bss_idx(%u) text_idx(%u) data_idx(%u) "
"rodata_idx(%u) ",
ei->sechdr_idx_bss, ei->sechdr_idx_text, ei->sechdr_idx_data,
ei->sechdr_idx_rodata);
}
return 0;
}
static int symbol_elf_load_program_headers(struct symbol_elf_internals* ei) {
Elf_Phdr* proghdrs = NULL;
size_t idx = 0;
int rc = 0;
int cnt_LOAD = 0;
if (!ei || ei->proghdr_offset == 0 || ei->proghdr_size == 0) {
return -1;
}
ei->proghdrs = malloc(ei->proghdr_size);
if (!ei->proghdrs) {
LOG(LOG_ERR, "malloc error: size %d, %s", ei->sechdr_size, strerror(errno));
return -1;
}
memset(ei->proghdrs, 0, ei->proghdr_size);
if (lseek(ei->fd, ei->proghdr_offset, SEEK_SET) < 0) {
LOG(LOG_ERR, "lseek error: fd %d, %s", ei->fd, strerror(errno));
return -1;
}
if (read(ei->fd, ei->proghdrs, ei->proghdr_size) < 0) {
LOG(LOG_ERR, "read error: fd %d, %s", ei->fd, strerror(errno));
return -1;
}
/* LOG(LOG_DEBUG, "Number of segments: %u", ei->proghdr_num); */
proghdrs = (Elf_Phdr*)ei->proghdrs;
for (idx = 0; idx < ei->proghdr_num; ++idx) {
rc = 0;
/*
LOG(LOG_DEBUG,"Prog-header %u: Type: %d VAddr: %p PAddr: %p FileSz: %u
MemSz: %u", idx, proghdrs[idx].p_type, (void *)proghdrs[idx].p_vaddr, (void
*)proghdrs[idx].p_paddr, proghdrs[idx].p_filesz, proghdrs[idx].p_memsz);
*/
if (proghdrs[idx].p_type == PT_INTERP) {
/* LOG(LOG_DEBUG, "PT_INTERP section found"); */
if (proghdrs[idx].p_filesz == 0) {
continue;
}
if (lseek(ei->fd, proghdrs[idx].p_offset, SEEK_SET) < 0) {
LOG(LOG_ERR, "lseek error: fd %d, %s", ei->fd, strerror(errno));
rc = -1;
break;
}
if (ei->interp.name) {
free(ei->interp.name);
memset(&ei->interp, 0, sizeof(ei->interp));
}
ei->interp.name = malloc(proghdrs[idx].p_filesz);
if (!ei->interp.name) {
LOG(LOG_ERR, "malloc error: size %d, %s", ei->sechdr_size,
strerror(errno));
rc = -1;
break;
}
if (read(ei->fd, ei->interp.name, proghdrs[idx].p_filesz) < 0) {
LOG(LOG_ERR, "read error: fd %d, %s", ei->fd, strerror(errno));
rc = -1;
break;
}
ei->interp.length = proghdrs[idx].p_filesz;
ei->interp.ph_addr = proghdrs[idx].p_vaddr;
/* LOG(LOG_DEBUG, "Found %s at V-Addr %p", ei->interp.name,
* ei->interp.ph_addr); */
} else if (proghdrs[idx].p_type == PT_DYNAMIC) {
/* LOG(LOG_DEBUG, "PT_DYNAMIC section found"); */
} else if (proghdrs[idx].p_type == PT_LOAD) {
/* LOG(LOG_DEBUG, "PT_LOAD section found"); */
if (cnt_LOAD == 0) {
if (ei->type == ET_EXEC) {
ei->base_adjust = 0;
} else if (ei->type == ET_DYN) {
ei->base_adjust = -1;
if (proghdrs[idx].p_offset != 0 || proghdrs[idx].p_vaddr != 0) {
LOG(LOG_ERR,
"ELF file is not support: ET_DYN with none zero PT_LOAD[0]");
rc = -1;
break;
}
}
}
cnt_LOAD++;
}
}
return rc;
}
void symbol_elf_ei_destory(struct symbol_elf_internals* ei,
int needfreesymbol) {
if (!ei) {
return;
}
if (ei->fd >= 0) {
close(ei->fd);
}
ei->fd = -1;
ei->strsectbl_size = 0;
if (ei->sechdrs_types) {
free(ei->sechdrs_types);
ei->sechdrs_types = NULL;
}
if (ei->strsectbl) {
free(ei->strsectbl);
ei->strsectbl = NULL;
}
if (ei->sechdrs) {
free(ei->sechdrs);
ei->sechdrs = NULL;
}
if (ei->proghdrs) {
free(ei->proghdrs);
ei->proghdrs = NULL;
}
if (needfreesymbol) {
/* LOG(LOG_DEBUG, "Free symbols..."); */
if (ei->interp.name) {
free(ei->interp.name);
}
ei->interp.name = NULL;
if (ei->symbols) {
size_t idx;
for (idx = 0; idx < ei->symbols_num; ++idx) {
free(ei->symbols[idx].name);
ei->symbols[idx].name = NULL;
}
free(ei->symbols);
}
ei->symbols = NULL;
ei->symbols_num = 0;
} else {
/* LOG(LOG_DEBUG, "No need free symbols."); */
}
return;
}
int symbol_elf_ei_create_hdrs_symtabs(struct symbol_elf_internals* ei) {
Elf_Ehdr hdr;
int fd = -1;
if (!ei) {
return -1;
}
fd = ei->fd;
memset(&hdr, 0, sizeof(hdr));
if (lseek(fd, 0, SEEK_SET) < 0) {
LOG(LOG_ERR, "lseek error: fd %d, %s", fd, strerror(errno));
return -1;
}
if (read(fd, &hdr, sizeof(hdr)) < 0) {
LOG(LOG_ERR, "read error: fd %d, %s", fd, strerror(errno));
return -1;
}
/* LOG(LOG_DEBUG, "Reading Elf header."); */
ei->is64 = symbol_elf_identify(hdr.e_ident, EI_NIDENT);
switch (ei->is64) {
case ELF_IS_64BIT:
#if __WORDSIZE != 64
LOG(LOG_ERR, "64-bit valid exe, is not 32-bit.");
return -1;
#endif
break;
case ELF_IS_32BIT:
#if __WORDSIZE == 64
LOG(LOG_ERR, "32bit valid exe, is not 64-bit.");
return -1;
#endif
break;
case ELF_IS_NEITHER:
default:
return -1;
}
/* LOG(LOG_DEBUG, "Object file type %d", hdr.e_type); */
ei->type = hdr.e_type;
/* LOG(LOG_DEBUG, "Entry point %p", (void *)hdr.e_entry); */
ei->entry_point = (uintptr_t)hdr.e_entry;
/* LOG(LOG_DEBUG, "Machine %d", hdr.e_machine); */
ei->machine = hdr.e_machine;
if (hdr.e_machine != EM_X86_64 && hdr.e_machine != EM_386) {
LOG(LOG_ERR, "ERROR: unsupported processor!");
return -1;
}
/*Object file has section-header-table.*/
if (hdr.e_shoff > 0) {
ei->sechdr_offset = 0 + hdr.e_shoff;
ei->sechdr_num = 0 + hdr.e_shnum;
ei->sechdr_size = 0 + hdr.e_shnum * hdr.e_shentsize;
ei->secnametbl_idx = 0 + hdr.e_shstrndx;
}
/*Exe file has program-header-table.*/
if (hdr.e_phoff > 0) {
ei->proghdr_offset = 0 + hdr.e_phoff;
ei->proghdr_num = 0 + hdr.e_phnum;
ei->proghdr_size = 0 + hdr.e_phnum * hdr.e_phentsize;
}
if (hdr.e_shoff > 0) {
/*Load section-header-table and symbol tables.*/
if (symbol_elf_load_section_headers_and_symtabs(ei) < 0) {
LOG(LOG_ERR, "ERROR in loading section headers");
return -1;
}
}
if (hdr.e_phoff > 0) {
/*Load program-header-table.*/
if (symbol_elf_load_program_headers(ei) < 0) {
LOG(LOG_ERR, "ERROR in loading section headers");
return -1;
}
}
return 0;
}
static struct symbol_elf_sym* symbol_elf_load_file(
const char* filename, size_t* symbols_num, uintptr_t* entry_point,
uintptr_t* base_adjust, struct symbol_elf_interp* interp,
enum symbol_elf_bit* is64) {
int rc = 0;
struct symbol_elf_sym* symbols = NULL;
struct symbol_elf_internals ei;
memset(&ei, 0, sizeof(ei));
if (entry_point) {
*entry_point = 0;
}
if (base_adjust) {
*base_adjust = 0;
}
ei.fd = symbol_open_filename(filename);
if (ei.fd < 0) {
return NULL;
}
/* LOG(LOG_INFO, "Begin to load Elf details for %s", filename); */
if ((rc = symbol_elf_ei_create_hdrs_symtabs(&ei)) < 0) {
LOG(LOG_ERR, "Unable to load Elf details for %s", filename);
}
/* LOG(LOG_INFO, "Freeing internal structure for %s", filename); */
if (rc < 0) {
symbol_elf_ei_destory(&ei, TRUE);
} else {
/* LOG(LOG_DEBUG, "Readying return values."); */
symbols = ei.symbols;
if (symbols_num) {
*symbols_num = ei.symbols_num;
}
if (interp) {
interp->name = ei.interp.name;
interp->length = ei.interp.length;
interp->ph_addr = ei.interp.ph_addr;
} else {
if (ei.interp.name) {
free(ei.interp.name);
}
ei.interp.name = NULL;
}
if (is64) {
*is64 = ei.is64;
}
if (entry_point) {
*entry_point = ei.entry_point;
}
if (base_adjust) {
*base_adjust = ei.base_adjust;
}
symbol_elf_ei_destory(&ei, FALSE);
}
return symbols;
}
#if 0
static void symbol_ld_procmaps_dump(struct symbol_ld_procmaps* pm)
{
if (!pm)
{ return; }
LOG(LOG_DEBUG, "Pathname: %s", pm->pathname ? pm->pathname : "Unknown");
LOG(LOG_DEBUG, "Address Start: %p End: %p Valid: %d Offset: %u", pm->addr_begin, pm->addr_end, pm->addr_valid, (size_t)pm->offset);
LOG(LOG_DEBUG, "Device Major: %d Minor: %d", pm->device_major, pm->device_minor);
LOG(LOG_DEBUG, "Inode: %u", (size_t)pm->inode);
LOG(LOG_DEBUG, "Permissions: Read(%d) Write(%d) Execute(%d) Private(%d) Shared(%d)",
(pm->permissions & PROCMAPS_PERMS_READ) ? 1 : 0,
(pm->permissions & PROCMAPS_PERMS_WRITE) ? 1 : 0,
(pm->permissions & PROCMAPS_PERMS_EXEC) ? 1 : 0,
(pm->permissions & PROCMAPS_PERMS_PRIVATE) ? 1 : 0,
(pm->permissions & PROCMAPS_PERMS_SHARED) ? 1 : 0
);
LOG(LOG_DEBUG, "Pathname length: %u", pm->pathname_sz);
LOG(LOG_DEBUG, "Filetype: %d", pm->filetype);
}
#endif
static int symbol_ld_procmaps_parse(char* buf, size_t bufsz,
struct symbol_ld_procmaps* pm,
const char* appname) {
if (!buf || !pm) {
LOG(LOG_ERR, "Invalid arguments.");
return -1;
}
/* this is hardcoded parsing of the maps file */
do {
char* token = NULL;
char* save = NULL;
int idx, err;
memset(pm, 0, sizeof(*pm));
token = strtok_r(buf, "-", &save);
if (!token) {
break;
}
errno = 0;
pm->addr_begin = (uintptr_t)strtoul(token, NULL, 16);
err = errno;
pm->addr_valid = (err == ERANGE || err == EINVAL) ? FALSE : TRUE;
if (!pm->addr_valid) {
LOG(LOG_DEBUG, "Strtoul error(%s) in parsing %s", strerror(err), token);
}
token = strtok_r(NULL, " ", &save);
if (!token) {
break;
}
errno = 0;
pm->addr_end = (intptr_t)strtoul(token, NULL, 16);
err = errno;
pm->addr_valid = (err == ERANGE || err == EINVAL) ? FALSE : TRUE;
if (!pm->addr_valid) {
LOG(LOG_DEBUG, "[%s:%d] Strtoul error(%s) in parsing %s", strerror(err),
token);
}
token = strtok_r(NULL, " ", &save);
if (!token) {
break;
}
pm->permissions = PROCMAPS_PERMS_NONE;
for (idx = strlen(token) - 1; idx >= 0; --idx) {
switch (token[idx]) {
case 'r':
pm->permissions |= PROCMAPS_PERMS_READ;
break;
case 'w':
pm->permissions |= PROCMAPS_PERMS_WRITE;
break;
case 'x':
pm->permissions |= PROCMAPS_PERMS_EXEC;
break;
case 'p':
pm->permissions |= PROCMAPS_PERMS_PRIVATE;
break;
case 's':
pm->permissions |= PROCMAPS_PERMS_SHARED;
break;
case '-':
break;
default:
LOG(LOG_DEBUG, "Unknown flag: %c", token[idx]);
break;
}
}
token = strtok_r(NULL, " ", &save);
if (!token) {
break;
}
errno = 0;
pm->offset = (off_t)strtoul(token, NULL, 16);
err = errno;
if (err == ERANGE || err == EINVAL) {
LOG(LOG_DEBUG, "Strtoul error(%s) in parsing %s", strerror(err), token);
}
token = strtok_r(NULL, ":", &save);
if (!token) {
break;
}
pm->device_major = (int)strtol(token, NULL, 10);
token = strtok_r(NULL, " ", &save);
if (!token) {
break;
}
pm->device_minor = (int)strtol(token, NULL, 10);
token = strtok_r(NULL, " ", &save);
if (!token) {
break;
}
pm->inode = (ino_t)strtoul(token, NULL, 10);
token = strtok_r(NULL, "\n", &save);
if (!token) {
break;
}
pm->pathname_sz = strlen(token);
pm->pathname = calloc(sizeof(char), pm->pathname_sz + 1);
if (!pm->pathname) {
LOG(LOG_ERR, "malloc error: size %d, %s", pm->pathname_sz + 1,
strerror(errno));
pm->pathname = NULL;
pm->pathname_sz = 0;
break;
}
/* trim the extra spaces out */
save = token;
/* find the real path names */
if ((token = strchr(save, '/'))) {
memcpy(pm->pathname, token, strlen(token));
if (strstr(pm->pathname, ".so") || strstr(pm->pathname, ".so.")) {
pm->filetype = PROCMAPS_FILETYPE_LIB;
} else {
struct stat statbuf;
pm->filetype = PROCMAPS_FILETYPE_DATA;
memset(&statbuf, 0, sizeof(statbuf));
if (stat(pm->pathname, &statbuf) >= 0) {
ino_t inode1 = statbuf.st_ino;
memset(&statbuf, 0, sizeof(statbuf));
if (stat(appname, &statbuf) >= 0) {
if (statbuf.st_ino == inode1) {
pm->filetype = PROCMAPS_FILETYPE_EXE;
}
}
} else {
int err = errno;
LOG(LOG_DEBUG, "Unable to stat file %s. Error: %s", pm->pathname,
strerror(err));
}
}
} else if ((token = strchr(save, '['))) {
memcpy(pm->pathname, token, strlen(token));
if (strstr(pm->pathname, "[heap]")) {
pm->filetype = PROCMAPS_FILETYPE_HEAP;
} else if (strstr(pm->pathname, "[stack]")) {
pm->filetype = PROCMAPS_FILETYPE_STACK;
} else if (strstr(pm->pathname, "[vdso]")) {
pm->filetype = PROCMAPS_FILETYPE_VDSO;
} else if (strstr(pm->pathname, "[vsyscall]")) {
pm->filetype = PROCMAPS_FILETYPE_SYSCALL;
} else if (strstr(pm->pathname, "[vvar]")) {
pm->filetype = PROCMAPS_FILETYPE_VVAR;
} else {
LOG(LOG_ERR, "Unknown memory map: %s", pm->pathname);
pm->filetype = PROCMAPS_FILETYPE_UNKNOWN;
}
} else {
memcpy(pm->pathname, token, strlen(token));
pm->filetype = PROCMAPS_FILETYPE_UNKNOWN;
}
} while (0);
return 0;
}
static struct symbol_ld_procmaps* symbol_ld_load_maps(pid_t pid, size_t* num,
uintptr_t* base_adjust) {
char filename[MAX_BUFFER];
char appname[MAX_BUFFER];
FILE* ff = NULL;
const size_t bufsz = 4096;
char* buf = NULL;
int need_adjust = 0;
int cnt_LOAD = 0;
size_t mapmax = 0;
size_t mapnum = 0;
struct symbol_ld_procmaps* maps = NULL;
if (pid == 0) {
LOG(LOG_ERR, "invalid pid %d.", pid);
return NULL;
}
if (base_adjust && *base_adjust == -1) {
need_adjust = 1;
}
snprintf(filename, MAX_BUFFER, "/proc/%d/maps", pid);
snprintf(appname, MAX_BUFFER, "/proc/%d/exe", pid);
LOG(LOG_DEBUG, "Using Proc Maps from %s", filename);
LOG(LOG_DEBUG, "Using Proc Exe from %s", appname);
do {
buf = calloc(sizeof(char), bufsz);
if (!buf) {
LOG(LOG_ERR, "malloc error: size %d, %s", bufsz, strerror(errno));
break;
}
ff = fopen(filename, "r");
if (!ff) {
LOG(LOG_ERR, "open file(%s) : %s", filename, strerror(errno));
break;
}
while (fgets(buf, bufsz, ff)) {
mapmax++;
}
LOG(LOG_DEBUG, "Max number of mappings present: %u", mapmax);
fseek(ff, 0L, SEEK_SET);
maps = calloc(sizeof(*maps), mapmax);
if (!maps) {
LOG(LOG_ERR, "malloc error: size %d, %s", mapmax * sizeof(*maps),
strerror(errno));
break;
}
LOG(LOG_DEBUG, "Allocated memory to load proc maps.");
memset(buf, 0, bufsz);
mapnum = 0;
while (fgets(buf, bufsz, ff)) {
struct symbol_ld_procmaps* pm = &maps[mapnum];
trimstr(buf);
/* LOG(LOG_DEBUG, "Parsing %s", buf); */
if (symbol_ld_procmaps_parse(buf, bufsz, pm, appname) < 0) {
LOG(LOG_INFO, "Parsing failure. Ignoring.");
continue;
}
if (need_adjust == 1 && cnt_LOAD == 0) {
if (pm->filetype == PROCMAPS_FILETYPE_EXE) {
if (!pm->addr_valid) {
LOG(LOG_ERR, "/proc/%d/maps addr invalid for %s", pid,
pm->pathname);
break;
}
*base_adjust = pm->addr_begin;
cnt_LOAD++;
}
}
/* symbol_ld_procmaps_dump(pm); */
mapnum++;
}
if (num) {
*num = mapnum;
} else {
LOG(LOG_ERR, "Cannot return size of maps object.");
}
if (need_adjust == 1 && *base_adjust == -1) {
LOG(LOG_ERR, "can't find exe's first LOAD in /proc/%d/maps", pid);
break;
}
} while (0);
if (buf) {
free(buf);
}
if (ff) {
fclose(ff);
}
return maps;
}
static void symbol_ld_free_maps(struct symbol_ld_procmaps* maps, size_t num) {
if (maps && num > 0) {
size_t idx;
for (idx = 0; idx < num; ++idx) {
if (maps[idx].pathname) {
free(maps[idx].pathname);
}
maps[idx].pathname = NULL;
}
free(maps);
maps = NULL;
}
}
static int symbol_ld_find_library(const struct symbol_ld_procmaps* maps,
const size_t mapnum, const char* libpath,
int inode_match,
struct symbol_ld_library* lib) {
if (!maps && !libpath) {
LOG(LOG_ERR, "Invalid arguments.");