blob: 953f13bd0a3fb36f32d13f860a413b70b01c0f32 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001From d29f37bb6e9114aba96c606103b110f511bee9a1 Mon Sep 17 00:00:00 2001
2From: Pratyush Anand <panand@redhat.com>
3Date: Wed, 2 Nov 2016 15:05:25 +0530
4Subject: [PATCH 2/9] kexec: generalize and rename get_kernel_stext_sym()
5
6get_kernel_stext_sym() has been defined for both arm and i386. Other
7architecture might need some other kernel symbol address. Therefore rewrite
8this function as generic function to get any kernel symbol address.
9
10More over, kallsyms is not arch specific representation, therefore have
11common function for all arches.
12
13Upstream-Status: Backport [https://git.linaro.org/people/takahiro.akashi/kexec-tools.git]
14
15Signed-off-by: Pratyush Anand <panand@redhat.com>
16[created symbols.c]
17Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
18Signed-off-by: He Zhe <zhe.he@windriver.com>
19---
20 kexec/Makefile | 1 +
21 kexec/arch/arm/crashdump-arm.c | 40 +---------------------------------------
22 kexec/arch/i386/crashdump-x86.c | 29 -----------------------------
23 kexec/kexec.h | 2 ++
24 kexec/symbols.c | 41 +++++++++++++++++++++++++++++++++++++++++
25 5 files changed, 45 insertions(+), 68 deletions(-)
26 create mode 100644 kexec/symbols.c
27
28diff --git a/kexec/Makefile b/kexec/Makefile
29index 39f365f..2b4fb3d 100644
30--- a/kexec/Makefile
31+++ b/kexec/Makefile
32@@ -26,6 +26,7 @@ KEXEC_SRCS_base += kexec/kernel_version.c
33 KEXEC_SRCS_base += kexec/lzma.c
34 KEXEC_SRCS_base += kexec/zlib.c
35 KEXEC_SRCS_base += kexec/kexec-xen.c
36+KEXEC_SRCS_base += kexec/symbols.c
37
38 KEXEC_GENERATED_SRCS += $(PURGATORY_HEX_C)
39
40diff --git a/kexec/arch/arm/crashdump-arm.c b/kexec/arch/arm/crashdump-arm.c
41index 4a89b5e..245c21a 100644
42--- a/kexec/arch/arm/crashdump-arm.c
43+++ b/kexec/arch/arm/crashdump-arm.c
44@@ -73,48 +73,10 @@ static struct crash_elf_info elf_info = {
45
46 extern unsigned long long user_page_offset;
47
48-/* Retrieve kernel _stext symbol virtual address from /proc/kallsyms */
49-static unsigned long long get_kernel_stext_sym(void)
50-{
51- const char *kallsyms = "/proc/kallsyms";
52- const char *stext = "_stext";
53- char sym[128];
54- char line[128];
55- FILE *fp;
56- unsigned long long vaddr = 0;
57- char type;
58-
59- fp = fopen(kallsyms, "r");
60- if (!fp) {
61- fprintf(stderr, "Cannot open %s\n", kallsyms);
62- return 0;
63- }
64-
65- while(fgets(line, sizeof(line), fp) != NULL) {
66- unsigned long long addr;
67-
68- if (sscanf(line, "%Lx %c %s", &addr, &type, sym) != 3)
69- continue;
70-
71- if (strcmp(sym, stext) == 0) {
72- dbgprintf("kernel symbol %s vaddr = %#llx\n", stext, addr);
73- vaddr = addr;
74- break;
75- }
76- }
77-
78- fclose(fp);
79-
80- if (vaddr == 0)
81- fprintf(stderr, "Cannot get kernel %s symbol address\n", stext);
82-
83- return vaddr;
84-}
85-
86 static int get_kernel_page_offset(struct kexec_info *info,
87 struct crash_elf_info *elf_info)
88 {
89- unsigned long long stext_sym_addr = get_kernel_stext_sym();
90+ unsigned long long stext_sym_addr = get_kernel_sym("_stext");
91 if (stext_sym_addr == 0) {
92 if (user_page_offset != (-1ULL)) {
93 elf_info->page_offset = user_page_offset;
94diff --git a/kexec/arch/i386/crashdump-x86.c b/kexec/arch/i386/crashdump-x86.c
95index ab833d4..abf82a5 100644
96--- a/kexec/arch/i386/crashdump-x86.c
97+++ b/kexec/arch/i386/crashdump-x86.c
98@@ -102,35 +102,6 @@ static int get_kernel_paddr(struct kexec_info *UNUSED(info),
99 return -1;
100 }
101
102-/* Retrieve kernel symbol virtual address from /proc/kallsyms */
103-static unsigned long long get_kernel_sym(const char *symbol)
104-{
105- const char *kallsyms = "/proc/kallsyms";
106- char sym[128];
107- char line[128];
108- FILE *fp;
109- unsigned long long vaddr;
110- char type;
111-
112- fp = fopen(kallsyms, "r");
113- if (!fp) {
114- fprintf(stderr, "Cannot open %s\n", kallsyms);
115- return 0;
116- }
117-
118- while(fgets(line, sizeof(line), fp) != NULL) {
119- if (sscanf(line, "%Lx %c %s", &vaddr, &type, sym) != 3)
120- continue;
121- if (strcmp(sym, symbol) == 0) {
122- dbgprintf("kernel symbol %s vaddr = %16llx\n", symbol, vaddr);
123- return vaddr;
124- }
125- }
126-
127- fprintf(stderr, "Cannot get kernel %s symbol address\n", symbol);
128- return 0;
129-}
130-
131 /* Retrieve info regarding virtual address kernel has been compiled for and
132 * size of the kernel from /proc/kcore. Current /proc/kcore parsing from
133 * from kexec-tools fails because of malformed elf notes. A kernel patch has
134diff --git a/kexec/kexec.h b/kexec/kexec.h
135index 9194f1c..b4fafad 100644
136--- a/kexec/kexec.h
137+++ b/kexec/kexec.h
138@@ -312,4 +312,6 @@ int xen_kexec_load(struct kexec_info *info);
139 int xen_kexec_unload(uint64_t kexec_flags);
140 void xen_kexec_exec(void);
141
142+extern unsigned long long get_kernel_sym(const char *text);
143+
144 #endif /* KEXEC_H */
145diff --git a/kexec/symbols.c b/kexec/symbols.c
146new file mode 100644
147index 0000000..ea6e327
148--- /dev/null
149+++ b/kexec/symbols.c
150@@ -0,0 +1,41 @@
151+#include <stdio.h>
152+#include <string.h>
153+#include "kexec.h"
154+
155+/* Retrieve kernel symbol virtual address from /proc/kallsyms */
156+unsigned long long get_kernel_sym(const char *text)
157+{
158+ const char *kallsyms = "/proc/kallsyms";
159+ char sym[128];
160+ char line[128];
161+ FILE *fp;
162+ unsigned long long vaddr = 0;
163+ char type;
164+
165+ fp = fopen(kallsyms, "r");
166+ if (!fp) {
167+ fprintf(stderr, "Cannot open %s\n", kallsyms);
168+ return 0;
169+ }
170+
171+ while (fgets(line, sizeof(line), fp) != NULL) {
172+ unsigned long long addr;
173+
174+ if (sscanf(line, "%Lx %c %s", &addr, &type, sym) != 3)
175+ continue;
176+
177+ if (strcmp(sym, text) == 0) {
178+ dbgprintf("kernel symbol %s vaddr = %#llx\n",
179+ text, addr);
180+ vaddr = addr;
181+ break;
182+ }
183+ }
184+
185+ fclose(fp);
186+
187+ if (vaddr == 0)
188+ fprintf(stderr, "Cannot get kernel %s symbol address\n", text);
189+
190+ return vaddr;
191+}
192--
1931.9.1
194