forked from magneticfieldcurrency/crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.c
13368 lines (11617 loc) · 378 KB
/
symbols.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
/* symbols.c - core analysis suite
*
* Copyright (C) 1999, 2000, 2001, 2002 Mission Critical Linux, Inc.
* Copyright (C) 2002-2019 David Anderson
* Copyright (C) 2002-2019 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "defs.h"
#include <elf.h>
#ifdef GDB_7_6
#define __CONFIG_H__ 1
#include "config.h"
#endif
#include "bfd.h"
static void store_symbols(bfd *, int, void *, long, unsigned int);
static void store_sysmap_symbols(void);
static ulong relocate(ulong, char *, int);
static int relocate_force(ulong, char *);
static void kaslr_init(void);
static void strip_module_symbol_end(char *s);
static int compare_syms(const void *, const void *);
static int compare_mods(const void *, const void *);
static int compare_prios(const void *v1, const void *v2);
static int compare_size_name(const void *, const void *);
struct type_request;
static void append_struct_symbol (struct type_request *, struct gnu_request *);
static void request_types(ulong, ulong, char *);
static asection *get_kernel_section(char *);
static char * get_section(ulong vaddr, char *buf);
static void symbol_dump(ulong, char *);
static void check_for_dups(struct load_module *);
static struct syment *kallsyms_module_symbol(struct load_module *, symbol_info *);
static int kallsyms_module_function_size(struct syment *, struct load_module *, ulong *);
static void store_load_module_symbols \
(bfd *, int, void *, long, uint, ulong, char *);
static int load_module_index(struct syment *);
static void section_header_info(bfd *, asection *, void *);
static void store_section_data(struct load_module *, bfd *, asection *);
static void calculate_load_order_v1(struct load_module *, bfd *);
static void calculate_load_order_v2(struct load_module *, bfd *, int,
void *, long, unsigned int);
static void check_insmod_builtin(struct load_module *, int, ulong *);
static int is_insmod_builtin(struct load_module *, struct syment *);
struct load_module;
static int add_symbol_file(struct load_module *);
static int add_symbol_file_kallsyms(struct load_module *, struct gnu_request *);
static void find_mod_etext(struct load_module *);
static long rodata_search(ulong *, ulong);
static int ascii_long(ulong word);
static int is_bfd_format(char *);
static int is_binary_stripped(char *);
static int namespace_ctl(int, struct symbol_namespace *, void *, void *);
static void symval_hash_init(void);
static struct syment *symval_hash_search(ulong);
static void symname_hash_init(void);
static void symname_hash_install(struct syment *);
static struct syment *symname_hash_search(char *);
static void gnu_qsort(bfd *, void *, long, unsigned int, asymbol *, asymbol *);
static int check_gnu_debuglink(bfd *);
static int separate_debug_file_exists(const char *, unsigned long, int *);
static int store_module_kallsyms_v1(struct load_module *, int, int, char *);
static int store_module_kallsyms_v2(struct load_module *, int, int, char *);
static void datatype_error(void **, char *, char *, char *, int);
static char *get_thisfile(void);
struct elf_common;
static void Elf32_Sym_to_common(Elf32_Sym *, struct elf_common *);
static void Elf64_Sym_to_common(Elf64_Sym *, struct elf_common *);
static void cmd_datatype_common(ulong);
static void do_datatype_addr(struct datatype_member *, ulong, int,
ulong, char **, int);
static void process_gdb_output(char *, unsigned, const char *, int);
static char *expr_type_name(const char *);
static int display_per_cpu_info(struct syment *, int, char *);
static struct load_module *get_module_percpu_sym_owner(struct syment *);
static int is_percpu_symbol(struct syment *);
static void dump_percpu_symbols(struct load_module *);
static void print_struct_with_dereference(ulong, struct datatype_member *, ulong);
static int dereference_pointer(ulong, struct datatype_member *, ulong);
#define KERNEL_SECTIONS (void *)(1)
#define MODULE_SECTIONS (void *)(2)
#define VERIFY_SECTIONS (void *)(3)
#define EV_DWARFEXTRACT 101010101
#define PARSE_FOR_DATA (1)
#define PARSE_FOR_DECLARATION (2)
static void parse_for_member(struct datatype_member *, ulong);
static int show_member_offset(FILE *, struct datatype_member *, char *);
struct struct_elem;
static void free_structure(struct struct_elem *);
static unsigned char is_right_brace(const char *);
static struct struct_elem *find_node(struct struct_elem *, char *);
static void dump_node(struct struct_elem *, char *, unsigned char, unsigned char);
/*
* structure/union printing stuff
*/
#define UINT8 (0x1)
#define INT8 (0x2)
#define UINT16 (0x4)
#define INT16 (0x8)
#define UINT32 (0x10)
#define INT32 (0x20)
#define UINT64 (0x40)
#define INT64 (0x80)
#define POINTER (0x100)
#define FUNCTION (0x200)
#define UNION_REQUEST (0x400)
#define STRUCT_REQUEST (0x800)
#define ARRAY (0x1000)
#define ENUM (0x2000)
#define TYPEDEF (0x4000)
#define STRUCT_VERBOSE (0x8000)
#define SHOW_OFFSET (0x10000)
#define IN_UNION (0x20000)
#define IN_STRUCT (0x40000)
#define DATATYPE_QUERY (0x80000)
#define ANON_MEMBER_QUERY (0x100000)
#define SHOW_RAW_DATA (0x200000)
#define DEREF_POINTERS (0x400000)
#define INTEGER_TYPE (UINT8|INT8|UINT16|INT16|UINT32|INT32|UINT64|INT64)
#define INITIAL_INDENT (4)
#define INDENT_INCR (2)
static void whatis_datatype(char *, ulong, FILE *);
static void whatis_variable(struct syment *);
static void print_struct(char *, ulong);
static void print_union(char *, ulong);
static void dump_datatype_member(FILE *, struct datatype_member *);
static void dump_datatype_flags(ulong, FILE *);
static long anon_member_offset(char *, char *);
static int gdb_whatis(char *);
static void do_datatype_declaration(struct datatype_member *, ulong);
static int member_to_datatype(char *, struct datatype_member *, ulong);
#define DEBUGINFO_ERROR_MESSAGE1 \
"the use of a System.map file requires that the accompanying namelist\nargument is a kernel file built with the -g CFLAG. The namelist argument\nsupplied in this case is a debuginfo file, which must be accompanied by the\nkernel file from which it was derived.\n"
#define DEBUGINFO_ERROR_MESSAGE2 \
"The namelist argument supplied in this case is a debuginfo file,\nwhich must be accompanied by the kernel file from which it was derived.\n"
/*
* This routine scours the namelist for kernel text and data symbols,
* sorts, and stores, them in a static table for quick reference.
*/
void
symtab_init(void)
{
char **matching;
long symcount;
void *minisyms;
unsigned int size;
asymbol *sort_x;
asymbol *sort_y;
if ((st->bfd = bfd_openr(pc->namelist, NULL)) == NULL)
error(FATAL, "cannot open object file: %s\n", pc->namelist);
if (!bfd_check_format_matches(st->bfd, bfd_object, &matching))
error(FATAL, "cannot determine object file format: %s\n",
pc->namelist);
/*
* Check whether the namelist is a kerntypes file built by
* dwarfextract, which places a magic number in e_version.
*/
if (file_elf_version(pc->namelist) == EV_DWARFEXTRACT)
pc->flags |= KERNTYPES;
if (pc->flags & SYSMAP) {
bfd_map_over_sections(st->bfd, section_header_info,
VERIFY_SECTIONS);
if ((st->flags & (NO_SEC_LOAD|NO_SEC_CONTENTS)) ==
(NO_SEC_LOAD|NO_SEC_CONTENTS)) {
error(INFO, "%s: no text and data contents\n",
pc->namelist);
error(FATAL, pc->flags & SYSMAP_ARG ?
DEBUGINFO_ERROR_MESSAGE1 :
DEBUGINFO_ERROR_MESSAGE2);
}
store_sysmap_symbols();
return;
} else if (LKCD_KERNTYPES())
error(FATAL, "%s: use of kerntypes requires a system map\n",
pc->namelist);
/*
* Pull a bait-and-switch on st->bfd if we've got a separate
* .gnu_debuglink file that matches the CRC. Not done for kerntypes.
*/
if (!(LKCD_KERNTYPES()) &&
!(bfd_get_file_flags(st->bfd) & HAS_SYMS)) {
if (!check_gnu_debuglink(st->bfd))
no_debugging_data(FATAL);
}
/*
* Gather references to the kernel sections.
*/
if ((st->sections = (struct sec *)
malloc(st->bfd->section_count * sizeof(struct sec *))) == NULL)
error(FATAL, "symbol table section array malloc: %s\n",
strerror(errno));
BZERO(st->sections, st->bfd->section_count * sizeof(struct sec *));
st->first_section_start = st->last_section_end = 0;
bfd_map_over_sections(st->bfd, section_header_info, KERNEL_SECTIONS);
if ((st->flags & (NO_SEC_LOAD|NO_SEC_CONTENTS)) ==
(NO_SEC_LOAD|NO_SEC_CONTENTS)) {
if (!pc->namelist_debug && !pc->debuginfo_file) {
error(INFO, "%s: no text and data contents\n",
pc->namelist);
error(FATAL, DEBUGINFO_ERROR_MESSAGE2);
}
}
symcount = bfd_read_minisymbols(st->bfd, FALSE, &minisyms, &size);
if (symcount <= 0)
no_debugging_data(FATAL);
sort_x = bfd_make_empty_symbol(st->bfd);
sort_y = bfd_make_empty_symbol(st->bfd);
if (sort_x == NULL || sort_y == NULL)
error(FATAL, "bfd_make_empty_symbol() failed\n");
kaslr_init();
gnu_qsort(st->bfd, minisyms, symcount, size, sort_x, sort_y);
store_symbols(st->bfd, FALSE, minisyms, symcount, size);
free(minisyms);
symname_hash_init();
symval_hash_init();
}
/*
* Adapted from gdb's get_debug_link_info()
*
* Look in: current directory
* basename-of-namelist/.debug directory
* /usr/lib/debug/boot (since we know it's a Red Hat kernel)
*/
static int
check_gnu_debuglink(bfd *bfd)
{
int i, exists, found;
asection *sect;
bfd_size_type debuglink_size;
char *contents;
int crc_offset;
unsigned long crc32;
char *dirname;
char *namelist_debug;
char **matching;
sect = bfd_get_section_by_name(bfd, ".gnu_debuglink");
if (!sect) {
error(INFO, "%s: no .gnu_debuglink section\n", pc->namelist);
return FALSE;
}
debuglink_size = bfd_section_size(bfd, sect);
contents = GETBUF(debuglink_size);
bfd_get_section_contents(bfd, sect, contents,
(file_ptr)0, (bfd_size_type)debuglink_size);
crc_offset = strlen (contents) + 1;
crc_offset = (crc_offset + 3) & ~3;
crc32 = bfd_get_32(bfd, (bfd_byte *)(contents + crc_offset));
if (CRASHDEBUG(1))
error(NOTE, "gnu_debuglink file: %s\ncrc32: %lx\n",
contents, crc32);
if ((pc->debuginfo_file = (char *)
malloc(((strlen(pc->namelist) + strlen("/.debug/") +
+ strlen(".debug") + strlen(" /usr/lib/debug/boot/ "))*10)
+ strlen(pc->namelist_debug ? pc->namelist_debug : " "))) == NULL)
error(FATAL, "debuginfo file name malloc: %s\n",
strerror(errno));
dirname = GETBUF(strlen(pc->namelist)+1);
strcpy(dirname, pc->namelist);
for (i = strlen(dirname)-1; i >= 0; i--)
{
if (dirname[i] == '/')
break;
}
dirname[i+1] = NULLCHAR;
if (!strlen(dirname))
sprintf(dirname, ".");
namelist_debug = NULL;
if (pc->namelist_debug) {
sprintf(pc->debuginfo_file, "%s", pc->namelist_debug);
if (separate_debug_file_exists(pc->debuginfo_file,
crc32, &exists)) {
if (CRASHDEBUG(1))
fprintf(fp, "%s: CRC matches\n",
pc->debuginfo_file);
st->flags |= CRC_MATCHES;
goto reset_bfd;
} else {
if ((st->flags & FORCE_DEBUGINFO) && exists) {
error(WARNING,
"%s:\n CRC value does not match\n\n",
pc->debuginfo_file);
goto reset_bfd;
} else
error(INFO, "%s:\n CRC value does not match\n\n",
pc->debuginfo_file);
namelist_debug = pc->namelist_debug;
pc->namelist_debug = NULL;
}
}
found = 0;
sprintf(pc->debuginfo_file, "%s/%s", dirname, contents);
if (separate_debug_file_exists(pc->debuginfo_file, crc32, &exists)) {
if (CRASHDEBUG(1))
fprintf(fp, "%s: CRC matches\n", pc->debuginfo_file);
st->flags |= CRC_MATCHES;
goto reset_bfd;
} else {
if (CRASHDEBUG(1))
fprintf(fp, "%s: %s\n", pc->debuginfo_file, exists ?
"CRC does not match" : "not readable/found");
if (exists) {
error(INFO, "%s: CRC does not match\n\n",
pc->debuginfo_file);
found++;
}
}
sprintf(pc->debuginfo_file, "%s/.debug/%s", dirname, contents);
if (separate_debug_file_exists(pc->debuginfo_file, crc32, &exists)) {
if (CRASHDEBUG(1))
fprintf(fp, "%s: CRC matches\n", pc->debuginfo_file);
st->flags |= CRC_MATCHES;
goto reset_bfd;
} else {
if (CRASHDEBUG(1))
fprintf(fp, "%s: %s\n", pc->debuginfo_file, exists ?
"CRC does not match" : "not readable/found");
if (exists) {
error(INFO, "%s: CRC does not match\n\n",
pc->debuginfo_file);
found++;
}
}
sprintf(pc->debuginfo_file, "/usr/lib/debug/boot/%s", contents);
if (separate_debug_file_exists(pc->debuginfo_file, crc32, &exists)) {
if (CRASHDEBUG(1))
fprintf(fp, "%s: CRC matches\n", pc->debuginfo_file);
st->flags |= CRC_MATCHES;
goto reset_bfd;
} else {
if (CRASHDEBUG(1))
fprintf(fp, "%s: %s\n", pc->debuginfo_file, exists ?
"CRC does not match" : "not readable/found");
if (exists) {
error(INFO, "%s: CRC does not match\n\n",
pc->debuginfo_file);
found++;
}
}
if (!found && namelist_debug) {
error(INFO,
"%s:\n use of -f option may suffice, or may fail miserably\n",
namelist_debug);
}
if (!found && !namelist_debug) {
no_debugging_data(INFO);
error(INFO, "%s: debuginfo file not found\n", contents);
error(FATAL,
"either install the appropriate kernel debuginfo package, or\n copy %s to this machine", contents);
}
return FALSE;
reset_bfd:
if ((st->bfd = bfd_openr(pc->debuginfo_file, NULL)) == NULL)
error(FATAL, "cannot open object file: %s\n",
pc->debuginfo_file);
if (!bfd_check_format_matches(st->bfd, bfd_object, &matching))
error(FATAL, "cannot determine object file format: %s\n",
pc->debuginfo_file);
FREEBUF(contents);
FREEBUF(dirname);
return TRUE;
}
/*
* Based upon gdb's separate_debug_file_exists().
*/
static int
separate_debug_file_exists(const char *name, unsigned long crc, int *exists)
{
unsigned long file_crc = 0;
int fd;
char buffer[8*1024];
size_t count;
fd = open(name, O_RDONLY);
if (fd < 0) {
*exists = FALSE;
return 0;
}
*exists = TRUE;
while ((count = read(fd, buffer, sizeof(buffer))) > 0)
#ifdef GDB_5_3
file_crc = calc_crc32(file_crc, buffer, count);
#else
#ifdef GDB_7_6
file_crc = bfd_calc_gnu_debuglink_crc32(file_crc,
(unsigned char *)buffer, count);
#else
file_crc = gnu_debuglink_crc32(file_crc,
(unsigned char *)buffer, count);
#endif
#endif
close (fd);
return crc == file_crc;
}
/*
* Callback for gdb to use a specified vmlinux.debug file.
*/
char *
check_specified_kernel_debug_file()
{
if (pc->flags & GDB_INIT)
return NULL;
return (pc->namelist_debug ? pc->namelist_debug : NULL);
}
/*
* Common bailout/warning routine when running against non-debug kernels.
*
* INFO: used when this routine should return.
* FATAL: kills function if runtime, or kills program if during init.
* WARNING: called by gdb_session_init() only, in an attempt to at least
* get by with built-in debug data; if not possible the program
* is killed.
*/
void
no_debugging_data(int error_type)
{
switch (error_type)
{
case INFO:
error(INFO, "%s: no debugging data available\n", pc->namelist);
break;
case FATAL:
error(FATAL, "%s%s: no debugging data available\n",
pc->flags & RUNTIME ? "" : "\n", pc->namelist);
clean_exit(1);
case WARNING:
error(FATAL, "\n%s: no debugging data available\n",
pc->namelist);
clean_exit(1);
}
}
/*
* Get the address space formerly used as init-time text. While there
* get the boundaries of the kernel .rodata section so that it won't
* be confused with text.
*
* This is done indirectly by the call-back to section_header_info().
*/
void
get_text_init_space(void)
{
asection *section = NULL;
if (pc->flags & SYSMAP)
return;
if (machine_type("ARM"))
section = get_kernel_section(".init");
if (!section && !(section = get_kernel_section(".text.init")))
section = get_kernel_section(".init.text");
if (!section) {
error(WARNING, "cannot determine text init space\n");
return;
}
kt->stext_init = (ulong)bfd_get_section_vma(st->bfd, section);
kt->etext_init = kt->stext_init +
(ulong)bfd_section_size(st->bfd, section);
if (kt->relocate) {
kt->stext_init -= kt->relocate;
kt->etext_init -= kt->relocate;
}
}
/*
* Strip gcc-generated cloned text symbol name endings.
*/
static char *
strip_symbol_end(const char *name, char *buf)
{
char *p;
if (st->flags & NO_STRIP)
return (char *)name;
if ((p = strstr(name, ".isra."))) {
if (buf) {
strcpy(buf, name);
buf[p-name] = NULLCHAR;
return buf;
} else {
*p = NULLCHAR;
return (char *)name;
}
}
if ((p = strstr(name, ".part."))) {
if (buf) {
strcpy(buf, name);
buf[p-name] = NULLCHAR;
return buf;
} else {
*p = NULLCHAR;
return (char *)name;
}
}
return (char *)name;
}
/*
* Gather the relevant information from the dumpfile or live system
* and determine whether to derive the KASLR offset.
*
* Setting st->_stext_vmlinux to UNINITIALIZED will trigger the
* search for "_stext" from the vmlinux file during the initial
* symbol sort operation.
*
* Setting RELOC_AUTO will ensure that derive_kaslr_offset() is
* called after the sorting operation has captured the vmlinux
* file's "_stext" symbol value -- which it will compare to the
* relocated "_stext" value found in either a dumpfile's vmcoreinfo
* or in /proc/kallsyms on a live system.
*
* Setting KASLR_CHECK will trigger a search for "module_load_offset"
* during the initial symbol sort operation, and if found, will
* set (RELOC_AUTO|KASLR). On live systems, the search is done
* here by checking /proc/kallsyms.
*/
static void
kaslr_init(void)
{
char *string;
if ((!machine_type("X86_64") && !machine_type("ARM64") && !machine_type("X86") &&
!machine_type("S390X")) || (kt->flags & RELOC_SET))
return;
/*
* --kaslr=auto
*/
if ((kt->flags2 & (RELOC_AUTO|KASLR)) == (RELOC_AUTO|KASLR))
st->_stext_vmlinux = UNINITIALIZED;
if (ACTIVE() && /* Linux 3.15 */
(symbol_value_from_proc_kallsyms("module_load_offset") != BADVAL)) {
kt->flags2 |= (RELOC_AUTO|KASLR);
st->_stext_vmlinux = UNINITIALIZED;
}
if (machine_type("S390X") && /* Linux 5.2 */
(symbol_value_from_proc_kallsyms("__kaslr_offset") != BADVAL)) {
kt->flags2 |= (RELOC_AUTO|KASLR);
st->_stext_vmlinux = UNINITIALIZED;
}
if (QEMU_MEM_DUMP_NO_VMCOREINFO()) {
if (KDUMP_DUMPFILE() && kdump_kaslr_check()) {
kt->flags2 |= KASLR_CHECK;
} else if (DISKDUMP_DUMPFILE() && diskdump_kaslr_check()) {
kt->flags2 |= KASLR_CHECK;
}
} else if (KDUMP_DUMPFILE() || DISKDUMP_DUMPFILE()) {
if ((string = pc->read_vmcoreinfo("SYMBOL(_stext)"))) {
kt->vmcoreinfo._stext_SYMBOL =
htol(string, RETURN_ON_ERROR, NULL);
free(string);
}
/* Linux 3.14 */
if ((string = pc->read_vmcoreinfo("KERNELOFFSET"))) {
free(string);
kt->flags2 |= KASLR_CHECK;
st->_stext_vmlinux = UNINITIALIZED;
}
}
if (SADUMP_DUMPFILE() || VMSS_DUMPFILE())
kt->flags2 |= KASLR_CHECK;
}
/*
* Derives the kernel aslr offset by comparing the _stext symbol from the
* the vmcoreinfo in the dump file to the _stext symbol in the vmlinux file.
*/
static void
derive_kaslr_offset(bfd *abfd, int dynamic, bfd_byte *start, bfd_byte *end,
unsigned int size, asymbol *store)
{
unsigned long relocate;
ulong _stext_relocated;
if (SADUMP_DUMPFILE() || QEMU_MEM_DUMP_NO_VMCOREINFO() || VMSS_DUMPFILE()) {
ulong kaslr_offset = 0;
ulong phys_base = 0;
calc_kaslr_offset(&kaslr_offset, &phys_base);
if (kaslr_offset) {
kt->relocate = kaslr_offset * -1;
kt->flags |= RELOC_SET;
}
if (phys_base) {
if (SADUMP_DUMPFILE())
sadump_set_phys_base(phys_base);
else if (KDUMP_DUMPFILE())
kdump_set_phys_base(phys_base);
else if (DISKDUMP_DUMPFILE())
diskdump_set_phys_base(phys_base);
else if (VMSS_DUMPFILE())
vmware_vmss_set_phys_base(phys_base);
}
return;
}
if (ACTIVE()) {
_stext_relocated = symbol_value_from_proc_kallsyms("_stext");
if (_stext_relocated == BADVAL)
return;
} else {
_stext_relocated = kt->vmcoreinfo._stext_SYMBOL;
if (_stext_relocated == 0)
return;
}
/*
* To avoid mistaking an mismatched kernel version with
* a kaslr offset, we make sure that the offset is
* aligned by 0x1000, as it always will be for kaslr.
*/
if (st->_stext_vmlinux && (st->_stext_vmlinux != UNINITIALIZED)) {
relocate = st->_stext_vmlinux - _stext_relocated;
if (relocate && !(relocate & 0xfff)) {
kt->relocate = relocate;
kt->flags |= RELOC_SET;
}
}
if (CRASHDEBUG(1) && (kt->flags & RELOC_SET)) {
fprintf(fp, "KASLR:\n");
fprintf(fp, " _stext from %s: %lx\n",
basename(pc->namelist), st->_stext_vmlinux);
fprintf(fp, " _stext from %s: %lx\n",
ACTIVE() ? "/proc/kallsyms" : "vmcoreinfo",
_stext_relocated);
fprintf(fp, " relocate: %lx (%ldMB)\n",
kt->relocate * -1, (kt->relocate * -1) >> 20);
}
}
/*
* Store the symbols gathered by symtab_init(). The symbols are stored
* in increasing numerical order.
*/
static void
store_symbols(bfd *abfd, int dynamic, void *minisyms, long symcount,
unsigned int size)
{
asymbol *store;
asymbol *sym;
bfd_byte *from, *fromend;
symbol_info syminfo;
struct syment *sp;
char buf[BUFSIZE];
char *name;
int first;
if ((store = bfd_make_empty_symbol(abfd)) == NULL)
error(FATAL, "bfd_make_empty_symbol() failed\n");
if ((st->symtable = (struct syment *)
calloc(symcount, sizeof(struct syment))) == NULL)
error(FATAL, "symbol table syment space malloc: %s\n",
strerror(errno));
if (!namespace_ctl(NAMESPACE_INIT, &st->kernel_namespace,
(void *)symcount, NULL))
error(FATAL, "symbol table namespace malloc: %s\n",
strerror(errno));
st->syment_size = symcount * sizeof(struct syment);
st->symcnt = 0;
sp = st->symtable;
first = 0;
from = (bfd_byte *) minisyms;
fromend = from + symcount * size;
if (machine_type("X86")) {
if (kt->flags2 & KASLR) {
if ((kt->flags2 & RELOC_AUTO) && !(kt->flags & RELOC_SET))
derive_kaslr_offset(abfd, dynamic, from,
fromend, size, store);
} else if (!(kt->flags & RELOC_SET))
kt->flags |= RELOC_FORCE;
} else if (machine_type("X86_64") || machine_type("ARM64") ||
machine_type("S390X")) {
if ((kt->flags2 & RELOC_AUTO) && !(kt->flags & RELOC_SET))
derive_kaslr_offset(abfd, dynamic, from,
fromend, size, store);
} else
kt->flags &= ~RELOC_SET;
for (; from < fromend; from += size)
{
if ((sym = bfd_minisymbol_to_symbol(abfd, dynamic, from, store))
== NULL)
error(FATAL, "bfd_minisymbol_to_symbol() failed\n");
bfd_get_symbol_info(abfd, sym, &syminfo);
name = strip_symbol_end(syminfo.name, buf);
if (machdep->verify_symbol(name, syminfo.value,
syminfo.type)) {
if (kt->flags & (RELOC_SET|RELOC_FORCE))
sp->value = relocate(syminfo.value,
(char *)syminfo.name, !(first++));
else
sp->value = syminfo.value;
sp->type = syminfo.type;
namespace_ctl(NAMESPACE_INSTALL, &st->kernel_namespace,
sp, name);
sp++;
st->symcnt++;
}
}
st->symend = &st->symtable[st->symcnt];
st->flags |= KERNEL_SYMS;
namespace_ctl(NAMESPACE_COMPLETE, &st->kernel_namespace,
st->symtable, st->symend);
}
/*
* Store the symbols from the designated System.map. The symbols are stored
* in increasing numerical order.
*/
static void
store_sysmap_symbols(void)
{
int c, first;
long symcount;
char buf[BUFSIZE];
char name[BUFSIZE];
FILE *map;
char *mapitems[MAXARGS];
struct syment *sp, syment;
if ((map = fopen(pc->system_map, "r")) == NULL)
error(FATAL, "cannot open %s\n", pc->system_map);
symcount = 0;
while (fgets(buf, BUFSIZE, map))
symcount++;
if ((st->symtable = (struct syment *)
calloc(symcount, sizeof(struct syment))) == NULL)
error(FATAL, "symbol table syment space malloc: %s\n",
strerror(errno));
if (!namespace_ctl(NAMESPACE_INIT, &st->kernel_namespace,
(void *)symcount, NULL))
error(FATAL, "symbol table namespace malloc: %s\n",
strerror(errno));
if (!machine_type("X86") && !machine_type("X86_64") &&
!machine_type("ARM64") && !machine_type("S390X"))
kt->flags &= ~RELOC_SET;
first = 0;
st->syment_size = symcount * sizeof(struct syment);
st->symcnt = 0;
sp = st->symtable;
rewind(map);
while (fgets(buf, BUFSIZE, map)) {
if ((c = parse_line(buf, mapitems)) != 3)
continue;
syment.value = htol(mapitems[0], FAULT_ON_ERROR, NULL);
syment.type = mapitems[1][0];
syment.name = mapitems[2];
strcpy(name, syment.name);
strip_symbol_end(name, NULL);
if (machdep->verify_symbol(name, syment.value,
syment.type)) {
if (kt->flags & RELOC_SET)
sp->value = relocate(syment.value,
syment.name, !(first++));
else
sp->value = syment.value;
sp->type = syment.type;
namespace_ctl(NAMESPACE_INSTALL, &st->kernel_namespace,
sp, name);
sp++;
st->symcnt++;
}
}
fclose(map);
st->symend = &st->symtable[st->symcnt];
st->flags |= KERNEL_SYMS;
namespace_ctl(NAMESPACE_COMPLETE, &st->kernel_namespace,
st->symtable, st->symend);
symname_hash_init();
symval_hash_init();
}
/*
* Handle x86/arm64 kernels configured such that the vmlinux symbols
* are not as loaded into the kernel (not unity-mapped).
*/
static ulong
relocate(ulong symval, char *symname, int first_symbol)
{
if (XEN_HYPER_MODE()) {
kt->flags &= ~(RELOC_SET|RELOC_FORCE);
return symval;
}
switch (kt->flags & (RELOC_SET|RELOC_FORCE))
{
case RELOC_SET:
break;
case RELOC_FORCE:
if (first_symbol && !relocate_force(symval, symname))
kt->flags &= ~RELOC_FORCE;
break;
}
if (machine_type("X86_64")) {
/*
* There are some symbols which are outside of any section
* either because they are offsets or because they are absolute
* addresses. These should not be relocated.
*/
if (symval >= st->first_section_start &&
symval <= st->last_section_end) {
return symval - kt->relocate;
} else {
return symval;
}
} else
return symval - kt->relocate;
}
/*
* If no --reloc argument was passed, try to figure it out
* by comparing the first vmlinux kernel symbol with the
* first /proc/kallsyms symbol. (should be "_text")
*
* Live system only (at least for now).
*/
static int
relocate_force(ulong symval, char *symname)
{
int count, found;
FILE *kp;
char buf[BUFSIZE];
char *kallsyms[MAXARGS];
ulong kallsym;
if (!ACTIVE() || !file_exists("/proc/kallsyms", NULL)) {
if (CRASHDEBUG(1))
fprintf(fp,
"cannot determine relocation value: %s\n",
!ACTIVE() ? "not a live system" :
"/proc/kallsyms does not exist");
return FALSE;
}
if ((kp = fopen("/proc/kallsyms", "r")) == NULL) {
if (CRASHDEBUG(1))
fprintf(fp,
"cannot open /proc/kallsyms to determine relocation\n");
return FALSE;
}
if (CRASHDEBUG(1))
fprintf(fp,
"relocate from: %s\n"
" %s @ %lx\n"
"relocate to: /proc/kallsyms\n",
pc->namelist, symname, symval);
found = FALSE;
count = kallsym = 0;
while (!found && fgets(buf, BUFSIZE, kp) &&
(parse_line(buf, kallsyms) == 3) &&
hexadecimal(kallsyms[0], 0)) {
if (STREQ(kallsyms[2], symname)) {
kallsym = htol(kallsyms[0], RETURN_ON_ERROR, NULL);
found = TRUE;
}
count++;
if (CRASHDEBUG(1))
fprintf(fp,
" %s @ %s %s\n",
kallsyms[2], kallsyms[0],
STREQ(kallsyms[2], symname) ?
"(match!)" : "");
}
fclose(kp);
/*
* If the symbols match and have different values,
* force the relocation.
*/
if (found) {
if (symval != kallsym) {
kt->relocate = symval - kallsym;
return TRUE;
}
}
if (CRASHDEBUG(1))
fprintf(fp,
"cannot determine relocation value from"
" %d symbols in /proc/kallsyms\n", count);
return FALSE;