The routine to search a symbol in ELF can be shared, so split it out.

Signed-off-by: Pingfan Liu <pi...@redhat.com>
Cc: Baoquan He <b...@redhat.com>
Cc: Dave Young <dyo...@redhat.com>
Cc: Andrew Morton <a...@linux-foundation.org>
Cc: Philipp Rudo <pr...@redhat.com>
To: kexec@lists.infradead.org
---
 include/linux/kexec.h |  8 ++++
 kernel/kexec_file.c   | 86 +++++++++++++++++++++++--------------------
 2 files changed, 54 insertions(+), 40 deletions(-)

diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index 8f7322c932fb5..2998d8da09d86 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -23,6 +23,10 @@
 #include <uapi/linux/kexec.h>
 #include <linux/verification.h>
 
+#if defined(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY) || 
defined(CONFIG_KEXEC_PE_IMAGE)
+#include <linux/module.h>
+#endif
+
 extern note_buf_t __percpu *crash_notes;
 
 #ifdef CONFIG_CRASH_DUMP
@@ -550,6 +554,10 @@ void set_kexec_sig_enforced(void);
 static inline void set_kexec_sig_enforced(void) {}
 #endif
 
+#if defined(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY) || 
defined(CONFIG_KEXEC_PE_IMAGE)
+const Elf_Sym *elf_find_symbol(const Elf_Ehdr *ehdr, const char *name);
+#endif
+
 #endif /* !defined(__ASSEBMLY__) */
 
 #endif /* LINUX_KEXEC_H */
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 4780d8aae24e7..137049e7e2410 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -880,6 +880,51 @@ static int kexec_calculate_store_digests(struct kimage 
*image)
        return ret;
 }
 
+#if defined(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY) || 
defined(CONFIG_KEXEC_PE_IMAGE)
+const Elf_Sym *elf_find_symbol(const Elf_Ehdr *ehdr, const char *name)
+{
+       const Elf_Shdr *sechdrs;
+       const Elf_Sym *syms;
+       const char *strtab;
+       int i, k;
+
+       sechdrs = (void *)ehdr + ehdr->e_shoff;
+
+       for (i = 0; i < ehdr->e_shnum; i++) {
+               if (sechdrs[i].sh_type != SHT_SYMTAB)
+                       continue;
+
+               if (sechdrs[i].sh_link >= ehdr->e_shnum)
+                       /* Invalid strtab section number */
+                       continue;
+               strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
+               syms = (void *)ehdr + sechdrs[i].sh_offset;
+
+               /* Go through symbols for a match */
+               for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
+                       if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
+                               continue;
+
+                       if (strcmp(strtab + syms[k].st_name, name) != 0)
+                               continue;
+
+                       if (syms[k].st_shndx == SHN_UNDEF ||
+                           syms[k].st_shndx >= ehdr->e_shnum) {
+                               pr_debug("Symbol: %s has bad section index 
%d.\n",
+                                               name, syms[k].st_shndx);
+                               return NULL;
+                       }
+
+                       /* Found the symbol we are looking for */
+                       return &syms[k];
+               }
+       }
+
+       return NULL;
+}
+
+#endif
+
 #ifdef CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY
 /*
  * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
@@ -1137,49 +1182,10 @@ int kexec_load_purgatory(struct kimage *image, struct 
kexec_buf *kbuf)
 static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
                                                  const char *name)
 {
-       const Elf_Shdr *sechdrs;
-       const Elf_Ehdr *ehdr;
-       const Elf_Sym *syms;
-       const char *strtab;
-       int i, k;
-
        if (!pi->ehdr)
                return NULL;
 
-       ehdr = pi->ehdr;
-       sechdrs = (void *)ehdr + ehdr->e_shoff;
-
-       for (i = 0; i < ehdr->e_shnum; i++) {
-               if (sechdrs[i].sh_type != SHT_SYMTAB)
-                       continue;
-
-               if (sechdrs[i].sh_link >= ehdr->e_shnum)
-                       /* Invalid strtab section number */
-                       continue;
-               strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
-               syms = (void *)ehdr + sechdrs[i].sh_offset;
-
-               /* Go through symbols for a match */
-               for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
-                       if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
-                               continue;
-
-                       if (strcmp(strtab + syms[k].st_name, name) != 0)
-                               continue;
-
-                       if (syms[k].st_shndx == SHN_UNDEF ||
-                           syms[k].st_shndx >= ehdr->e_shnum) {
-                               pr_debug("Symbol: %s has bad section index 
%d.\n",
-                                               name, syms[k].st_shndx);
-                               return NULL;
-                       }
-
-                       /* Found the symbol we are looking for */
-                       return &syms[k];
-               }
-       }
-
-       return NULL;
+       return elf_find_symbol(pi->ehdr, name);
 }
 
 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
-- 
2.49.0


Reply via email to