From f93f437f8b4da434f11e6ab1ffe253defa85cd38 Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Mon, 11 Apr 2022 14:26:41 +0200 Subject: [PATCH] zdump: df_elf: `read_elf_hdr`: return Elf64_Ehdr struct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes the API of `read_elf_hdr` consistent with `read_elf_phdrs`. Signed-off-by: Marc Hartmayer Reviewed-by: Alexander Egorenkov Reviewed-by: Steffen Eiden Signed-off-by: Jan Höppner --- zdump/df_elf.c | 8 +++++--- zdump/df_elf.h | 2 +- zdump/dfi_elf.c | 31 +++++++++++++++++++------------ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/zdump/df_elf.c b/zdump/df_elf.c index cc830f74..7d02a608 100644 --- a/zdump/df_elf.c +++ b/zdump/df_elf.c @@ -52,15 +52,17 @@ bool ehdr_is_s390x(const Elf64_Ehdr *ehdr) ehdr->e_ident[EI_CLASS] == ELFCLASS64; } -int read_elf_hdr(const struct zg_fh *fh, Elf64_Ehdr *ehdr) +Elf64_Ehdr *read_elf_hdr(const struct zg_fh *fh) { + Elf64_Ehdr *ehdr; const size_t ehdr_size = sizeof(*ehdr); if (zg_size(fh) < ehdr_size) - return -1; + return NULL; + ehdr = util_malloc(ehdr_size); zg_read(fh, ehdr, ehdr_size, ZG_CHECK); - return 0; + return ehdr; } Elf64_Phdr *read_elf_phdrs(const struct zg_fh *fh, const Elf64_Ehdr *ehdr, unsigned int *phdr_count) diff --git a/zdump/df_elf.h b/zdump/df_elf.h index ca8e447e..e0fa7474 100644 --- a/zdump/df_elf.h +++ b/zdump/df_elf.h @@ -146,7 +146,7 @@ bool ehdr_is_s390x(const Elf64_Ehdr *ehdr); /* * Read ELF header at current offset */ -int read_elf_hdr(const struct zg_fh *fh, Elf64_Ehdr *ehdr); +Elf64_Ehdr *read_elf_hdr(const struct zg_fh *fh); /* * Read ELF program headers diff --git a/zdump/dfi_elf.c b/zdump/dfi_elf.c index 8aef2883..102a4a4d 100644 --- a/zdump/dfi_elf.c +++ b/zdump/dfi_elf.c @@ -267,21 +267,23 @@ static int dfi_elf_init(void) { unsigned int phnum, i; Elf64_Phdr *phdrs; - Elf64_Ehdr ehdr; + Elf64_Ehdr *ehdr; + int rc = -ENODEV; util_log_print(UTIL_LOG_DEBUG, "DFI ELF initialization\n"); - if (read_elf_hdr(g.fh, &ehdr) != 0) + ehdr = read_elf_hdr(g.fh); + if (!ehdr) return -ENODEV; - if (check_elf_hdr(&ehdr) < 0) - return -ENODEV; + if (check_elf_hdr(ehdr) < 0) + goto free_ehdr; df_elf_ensure_s390x(); dfi_arch_set(DFI_ARCH_64); dfi_cpu_info_init(DFI_CPU_CONTENT_ALL); - phdrs = read_elf_phdrs(g.fh, &ehdr, &phnum); + phdrs = read_elf_phdrs(g.fh, ehdr, &phnum); util_log_print(UTIL_LOG_DEBUG, "DFI ELF e_phnum %u\n", phnum); for (i = 0; i < phnum; i++) { const Elf64_Phdr *phdr = &phdrs[i]; @@ -290,24 +292,29 @@ static int dfi_elf_init(void) switch (phdr->p_type) { case PT_LOAD: if (pt_load_add(phdr)) { - free(phdrs); - return -EINVAL; + rc = -EINVAL; + goto free_phdrs; } break; case PT_NOTE: if (pt_notes_add(phdr)) { - free(phdrs); - return -EINVAL; + rc = -EINVAL; + goto free_phdrs; } break; default: break; } } - free(phdrs); - dfi_attr_version_set(ehdr.e_ident[EI_VERSION]); - return 0; + dfi_attr_version_set(ehdr->e_ident[EI_VERSION]); + rc = 0; + +free_phdrs: + free(phdrs); +free_ehdr: + free(ehdr); + return rc; } /*