From nobody Mon Feb 9 08:28:30 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 98B522737EB for ; Mon, 19 Jan 2026 03:27:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768793230; cv=none; b=DZWZTTjOCLL8IhL2bFHLy7JOuVc7evI0OdCk0U8MsJRklBQZNDO6InErfVkKWvc98Qq1NitFuHfunDi/HNnbVXEj0wZWLntSRl1ReUCuMgDnVGL/Y8JNYfApVgZl94no6Z+6TeYS3vIdeoc0AOsd+eg4+hqCEy+d9HqiwZ6xaSQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768793230; c=relaxed/simple; bh=uW+F2QtSidWZpv+WLcnvnCho2KgCi3lEi0l2DCvw9Sg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UxI/uuOxXnyNskfeEM9icrA2u89r89rBZKWIzi289GjQnJ6AuvybTv/EuujLVOYYLoEErux208hnLmJVlp/YVhI3OBGWvDi9T7qUecLJU2sf1nnlujE7tGruNuvBJdmQDFaz05+/bJj2F/rJks2gbi+v5DNHwQB/Pgm0ZTibEiw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=JyS3oYVC; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JyS3oYVC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768793227; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d/X41VcnHuOXqT84jMlB3N9HZaOq9CdFFA8bWA8RbTY=; b=JyS3oYVCe+QBU2skYWQ5DrS/euVnq3vtAvnBY7E4Dq+2Gcx7MyydVd4DojVS9/HjP+gqmd ivhEaZ+aSQtiseq9RZyGx4cqItN6g8j/AxzcID762L/U11wdoR/q3NXQWi3y76U9JayNmb +DJLvxu9s/7EQAKWJBmvyJ2R1TVUMts= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-434-c3b4zj0LNDC0uhWE-SElyQ-1; Sun, 18 Jan 2026 22:27:03 -0500 X-MC-Unique: c3b4zj0LNDC0uhWE-SElyQ-1 X-Mimecast-MFC-AGG-ID: c3b4zj0LNDC0uhWE-SElyQ_1768793221 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6197B1955F34; Mon, 19 Jan 2026 03:27:01 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.72.112.74]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C90D11955F22; Mon, 19 Jan 2026 03:26:50 +0000 (UTC) From: Pingfan Liu To: kexec@lists.infradead.org Cc: Pingfan Liu , "David S. Miller" , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , Jeremy Linton , Catalin Marinas , Will Deacon , Ard Biesheuvel , Simon Horman , Gerd Hoffmann , Vitaly Kuznetsov , Philipp Rudo , Viktor Malik , Jan Hendrik Farr , Baoquan He , Dave Young , Andrew Morton , bpf@vger.kernel.org, systemd-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCHv6 09/13] kexec_file: Factor out routine to find a symbol in ELF Date: Mon, 19 Jan 2026 11:24:20 +0800 Message-ID: <20260119032424.10781-10-piliu@redhat.com> In-Reply-To: <20260119032424.10781-1-piliu@redhat.com> References: <20260119032424.10781-1-piliu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" The routine to search a symbol in ELF can be shared, so split it out. Signed-off-by: Pingfan Liu Cc: Baoquan He Cc: Dave Young Cc: Andrew Morton Cc: Philipp Rudo To: kexec@lists.infradead.org --- kernel/kexec_file.c | 86 ++++++++++++++++++++++------------------- kernel/kexec_internal.h | 1 + 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index f9674bb5bd8db..9731019ba2a1a 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -889,6 +889,51 @@ static int kexec_calculate_store_digests(struct kimage= *image) return ret; } =20 +#if defined(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY) || defined(CONFIG_KEXEC_= BPF) +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 =3D (void *)ehdr + ehdr->e_shoff; + + for (i =3D 0; i < ehdr->e_shnum; i++) { + if (sechdrs[i].sh_type !=3D SHT_SYMTAB) + continue; + + if (sechdrs[i].sh_link >=3D ehdr->e_shnum) + /* Invalid strtab section number */ + continue; + strtab =3D (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset; + syms =3D (void *)ehdr + sechdrs[i].sh_offset; + + /* Go through symbols for a match */ + for (k =3D 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) { + if (ELF_ST_BIND(syms[k].st_info) !=3D STB_GLOBAL) + continue; + + if (strcmp(strtab + syms[k].st_name, name) !=3D 0) + continue; + + if (syms[k].st_shndx =3D=3D SHN_UNDEF || + syms[k].st_shndx >=3D 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. @@ -1146,49 +1191,10 @@ int kexec_load_purgatory(struct kimage *image, stru= ct kexec_buf *kbuf) static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *p= i, 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; =20 - ehdr =3D pi->ehdr; - sechdrs =3D (void *)ehdr + ehdr->e_shoff; - - for (i =3D 0; i < ehdr->e_shnum; i++) { - if (sechdrs[i].sh_type !=3D SHT_SYMTAB) - continue; - - if (sechdrs[i].sh_link >=3D ehdr->e_shnum) - /* Invalid strtab section number */ - continue; - strtab =3D (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset; - syms =3D (void *)ehdr + sechdrs[i].sh_offset; - - /* Go through symbols for a match */ - for (k =3D 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) { - if (ELF_ST_BIND(syms[k].st_info) !=3D STB_GLOBAL) - continue; - - if (strcmp(strtab + syms[k].st_name, name) !=3D 0) - continue; - - if (syms[k].st_shndx =3D=3D SHN_UNDEF || - syms[k].st_shndx >=3D 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); } =20 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *na= me) diff --git a/kernel/kexec_internal.h b/kernel/kexec_internal.h index ee01d0c8bb377..2e8e0fedbe2a9 100644 --- a/kernel/kexec_internal.h +++ b/kernel/kexec_internal.h @@ -40,6 +40,7 @@ extern bool pe_has_bpf_section(const char *file_buf, unsi= gned long pe_sz); extern int pe_get_section(const char *file_buf, const char *sect_name, char **sect_start, unsigned long *sect_sz); extern int decompose_kexec_image(struct kimage *image, int extended_fd); +extern const Elf_Sym *elf_find_symbol(const Elf_Ehdr *ehdr, const char *na= me); #else /* CONFIG_KEXEC_FILE */ static inline void kimage_file_post_load_cleanup(struct kimage *image) { } #endif /* CONFIG_KEXEC_FILE */ --=20 2.49.0