Skip to content

Commit bd79947

Browse files
Pingfan LiuKernel Patches Daemon
authored andcommitted
kexec: Factor out routine to find a symbol in ELF
The routine to search a symbol in ELF can be shared, so split it out. Signed-off-by: Pingfan Liu <[email protected]> Cc: Baoquan He <[email protected]> Cc: Dave Young <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Philipp Rudo <[email protected]> To: [email protected]
1 parent f7d522b commit bd79947

File tree

2 files changed

+54
-40
lines changed

2 files changed

+54
-40
lines changed

include/linux/kexec.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include <uapi/linux/kexec.h>
2424
#include <linux/verification.h>
2525

26+
#if defined(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY) || defined(CONFIG_KEXEC_PE_IMAGE)
27+
#include <linux/module.h>
28+
#endif
29+
2630
extern note_buf_t __percpu *crash_notes;
2731

2832
#ifdef CONFIG_CRASH_DUMP
@@ -550,6 +554,10 @@ void set_kexec_sig_enforced(void);
550554
static inline void set_kexec_sig_enforced(void) {}
551555
#endif
552556

557+
#if defined(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY) || defined(CONFIG_KEXEC_PE_IMAGE)
558+
const Elf_Sym *elf_find_symbol(const Elf_Ehdr *ehdr, const char *name);
559+
#endif
560+
553561
#endif /* !defined(__ASSEBMLY__) */
554562

555563
#endif /* LINUX_KEXEC_H */

kernel/kexec_file.c

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,51 @@ static int kexec_calculate_store_digests(struct kimage *image)
880880
return ret;
881881
}
882882

883+
#if defined(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY) || defined(CONFIG_KEXEC_PE_IMAGE)
884+
const Elf_Sym *elf_find_symbol(const Elf_Ehdr *ehdr, const char *name)
885+
{
886+
const Elf_Shdr *sechdrs;
887+
const Elf_Sym *syms;
888+
const char *strtab;
889+
int i, k;
890+
891+
sechdrs = (void *)ehdr + ehdr->e_shoff;
892+
893+
for (i = 0; i < ehdr->e_shnum; i++) {
894+
if (sechdrs[i].sh_type != SHT_SYMTAB)
895+
continue;
896+
897+
if (sechdrs[i].sh_link >= ehdr->e_shnum)
898+
/* Invalid strtab section number */
899+
continue;
900+
strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
901+
syms = (void *)ehdr + sechdrs[i].sh_offset;
902+
903+
/* Go through symbols for a match */
904+
for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
905+
if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
906+
continue;
907+
908+
if (strcmp(strtab + syms[k].st_name, name) != 0)
909+
continue;
910+
911+
if (syms[k].st_shndx == SHN_UNDEF ||
912+
syms[k].st_shndx >= ehdr->e_shnum) {
913+
pr_debug("Symbol: %s has bad section index %d.\n",
914+
name, syms[k].st_shndx);
915+
return NULL;
916+
}
917+
918+
/* Found the symbol we are looking for */
919+
return &syms[k];
920+
}
921+
}
922+
923+
return NULL;
924+
}
925+
926+
#endif
927+
883928
#ifdef CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY
884929
/*
885930
* kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
@@ -1137,49 +1182,10 @@ int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf)
11371182
static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
11381183
const char *name)
11391184
{
1140-
const Elf_Shdr *sechdrs;
1141-
const Elf_Ehdr *ehdr;
1142-
const Elf_Sym *syms;
1143-
const char *strtab;
1144-
int i, k;
1145-
11461185
if (!pi->ehdr)
11471186
return NULL;
11481187

1149-
ehdr = pi->ehdr;
1150-
sechdrs = (void *)ehdr + ehdr->e_shoff;
1151-
1152-
for (i = 0; i < ehdr->e_shnum; i++) {
1153-
if (sechdrs[i].sh_type != SHT_SYMTAB)
1154-
continue;
1155-
1156-
if (sechdrs[i].sh_link >= ehdr->e_shnum)
1157-
/* Invalid strtab section number */
1158-
continue;
1159-
strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
1160-
syms = (void *)ehdr + sechdrs[i].sh_offset;
1161-
1162-
/* Go through symbols for a match */
1163-
for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
1164-
if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
1165-
continue;
1166-
1167-
if (strcmp(strtab + syms[k].st_name, name) != 0)
1168-
continue;
1169-
1170-
if (syms[k].st_shndx == SHN_UNDEF ||
1171-
syms[k].st_shndx >= ehdr->e_shnum) {
1172-
pr_debug("Symbol: %s has bad section index %d.\n",
1173-
name, syms[k].st_shndx);
1174-
return NULL;
1175-
}
1176-
1177-
/* Found the symbol we are looking for */
1178-
return &syms[k];
1179-
}
1180-
}
1181-
1182-
return NULL;
1188+
return elf_find_symbol(pi->ehdr, name);
11831189
}
11841190

11851191
void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)

0 commit comments

Comments
 (0)