From 776e5d41d7a8e9da2bab67d6e9f10c34fc11a6a9 Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Wed, 17 Nov 2021 15:01:06 +0100 Subject: [PATCH] zdump: dfi_elf: factor out some ELF functionalities MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes them reuseable and testable. Signed-off-by: Marc Hartmayer Reviewed-by: Steffen Eiden Reviewed-by: Alexander Egorenkov Signed-off-by: Jan Höppner --- zdump/df_elf.c | 16 ++++++++++++++++ zdump/df_elf.h | 16 ++++++++++++++++ zdump/dfi_elf.c | 7 +++---- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/zdump/df_elf.c b/zdump/df_elf.c index 013385d7..69965979 100644 --- a/zdump/df_elf.c +++ b/zdump/df_elf.c @@ -34,6 +34,22 @@ void *ehdr_init(Elf64_Ehdr *ehdr, Elf64_Half phnum) return ehdr + 1; } +bool ehdr_is_elf_object(const Elf64_Ehdr *ehdr) +{ + return (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) == 0); +} + +bool ehdr_is_vmcore(const Elf64_Ehdr *ehdr) +{ + return ehdr->e_type == ET_CORE; +} + +bool ehdr_is_s390x(const Elf64_Ehdr *ehdr) +{ + return ehdr->e_machine == EM_S390 && + ehdr->e_ident[EI_CLASS] == ELFCLASS64; +} + void *nt_init(void *buf, Elf64_Word type, const void *desc, int d_len, const char *name) { diff --git a/zdump/df_elf.h b/zdump/df_elf.h index 7b5cfc75..6862fc32 100644 --- a/zdump/df_elf.h +++ b/zdump/df_elf.h @@ -14,6 +14,7 @@ #include #include +#include #include "lib/zt_common.h" @@ -127,6 +128,21 @@ static inline void df_elf_ensure_s390x(void) */ void *ehdr_init(Elf64_Ehdr *ehdr, Elf64_Half phnum); +/* + * Checks whether @ehdr is a ELF object + */ +bool ehdr_is_elf_object(const Elf64_Ehdr *ehdr); + +/* + * Checks whether @ehdr is a vmcore + */ +bool ehdr_is_vmcore(const Elf64_Ehdr *ehdr); + +/* + * Checks whether @ehdr is a s390x header + */ +bool ehdr_is_s390x(const Elf64_Ehdr *ehdr); + /* * Initialize ELF note */ diff --git a/zdump/dfi_elf.c b/zdump/dfi_elf.c index 63d531fb..0067984d 100644 --- a/zdump/dfi_elf.c +++ b/zdump/dfi_elf.c @@ -289,11 +289,10 @@ static int read_elf_hdr(Elf64_Ehdr *ehdr) if (zg_size(g.fh) < sizeof(*ehdr)) return -ENODEV; zg_read(g.fh, ehdr, sizeof(*ehdr), ZG_CHECK); - if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) + + if (!ehdr_is_elf_object(ehdr) || !ehdr_is_vmcore(ehdr)) return -ENODEV; - if (ehdr->e_type != ET_CORE) - return -ENODEV; - if (ehdr->e_machine != EM_S390 || ehdr->e_ident[EI_CLASS] != ELFCLASS64) + if (!ehdr_is_s390x(ehdr)) ERR_EXIT("Only s390x (64 bit) core dump files are supported"); return 0; }