thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
bf143f
From d1e147a3133d4d31d4b0c02c05916366fadd9c30 Mon Sep 17 00:00:00 2001
bf143f
From: Janosch Frank <frankja@linux.ibm.com>
bf143f
Date: Thu, 11 Aug 2022 12:11:00 +0000
bf143f
Subject: [PATCH 28/42] dump: Split elf header functions into prepare and write
bf143f
MIME-Version: 1.0
bf143f
Content-Type: text/plain; charset=UTF-8
bf143f
Content-Transfer-Encoding: 8bit
bf143f
bf143f
RH-Author: Cédric Le Goater <clg@redhat.com>
bf143f
RH-MergeRequest: 226: s390: Enhanced Interpretation for PCI Functions and Secure Execution guest dump
bf143f
RH-Bugzilla: 1664378 2043909
bf143f
RH-Acked-by: Thomas Huth <thuth@redhat.com>
bf143f
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
bf143f
RH-Acked-by: Jon Maloy <jmaloy@redhat.com>
bf143f
RH-Commit: [28/41] f70a13ad443835e7f46b7c5e176e372d370ac797
bf143f
bf143f
Let's split the write from the modification of the elf header so we
bf143f
can consolidate the write of the data in one function.
bf143f
bf143f
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
bf143f
Reviewed-by: Janis Schoetterl-Glausch <scgl@linux.ibm.com>
bf143f
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
bf143f
Message-Id: <20220811121111.9878-8-frankja@linux.ibm.com>
bf143f
(cherry picked from commit 670e76998a61ca171200fcded3865b294a2d1243)
bf143f
Signed-off-by: Cédric Le Goater <clg@redhat.com>
bf143f
---
bf143f
 dump/dump.c | 100 ++++++++++++++++++++++++++++------------------------
bf143f
 1 file changed, 53 insertions(+), 47 deletions(-)
bf143f
bf143f
diff --git a/dump/dump.c b/dump/dump.c
bf143f
index 902a85ef8e..8d5226f861 100644
bf143f
--- a/dump/dump.c
bf143f
+++ b/dump/dump.c
bf143f
@@ -132,7 +132,7 @@ static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
bf143f
     return 0;
bf143f
 }
bf143f
 
bf143f
-static void write_elf64_header(DumpState *s, Error **errp)
bf143f
+static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
bf143f
 {
bf143f
     /*
bf143f
      * phnum in the elf header is 16 bit, if we have more segments we
bf143f
@@ -140,34 +140,27 @@ static void write_elf64_header(DumpState *s, Error **errp)
bf143f
      * special section.
bf143f
      */
bf143f
     uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
bf143f
-    Elf64_Ehdr elf_header;
bf143f
-    int ret;
bf143f
 
bf143f
-    memset(&elf_header, 0, sizeof(Elf64_Ehdr));
bf143f
-    memcpy(&elf_header, ELFMAG, SELFMAG);
bf143f
-    elf_header.e_ident[EI_CLASS] = ELFCLASS64;
bf143f
-    elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
bf143f
-    elf_header.e_ident[EI_VERSION] = EV_CURRENT;
bf143f
-    elf_header.e_type = cpu_to_dump16(s, ET_CORE);
bf143f
-    elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
bf143f
-    elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
bf143f
-    elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
bf143f
-    elf_header.e_phoff = cpu_to_dump64(s, s->phdr_offset);
bf143f
-    elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
bf143f
-    elf_header.e_phnum = cpu_to_dump16(s, phnum);
bf143f
+    memset(elf_header, 0, sizeof(Elf64_Ehdr));
bf143f
+    memcpy(elf_header, ELFMAG, SELFMAG);
bf143f
+    elf_header->e_ident[EI_CLASS] = ELFCLASS64;
bf143f
+    elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
bf143f
+    elf_header->e_ident[EI_VERSION] = EV_CURRENT;
bf143f
+    elf_header->e_type = cpu_to_dump16(s, ET_CORE);
bf143f
+    elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
bf143f
+    elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
bf143f
+    elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
bf143f
+    elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
bf143f
+    elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
bf143f
+    elf_header->e_phnum = cpu_to_dump16(s, phnum);
bf143f
     if (s->shdr_num) {
bf143f
-        elf_header.e_shoff = cpu_to_dump64(s, s->shdr_offset);
bf143f
-        elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
bf143f
-        elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
bf143f
-    }
bf143f
-
bf143f
-    ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
bf143f
-    if (ret < 0) {
bf143f
-        error_setg_errno(errp, -ret, "dump: failed to write elf header");
bf143f
+        elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
bf143f
+        elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
bf143f
+        elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
bf143f
     }
bf143f
 }
bf143f
 
bf143f
-static void write_elf32_header(DumpState *s, Error **errp)
bf143f
+static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
bf143f
 {
bf143f
     /*
bf143f
      * phnum in the elf header is 16 bit, if we have more segments we
bf143f
@@ -175,28 +168,45 @@ static void write_elf32_header(DumpState *s, Error **errp)
bf143f
      * special section.
bf143f
      */
bf143f
     uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
bf143f
-    Elf32_Ehdr elf_header;
bf143f
-    int ret;
bf143f
 
bf143f
-    memset(&elf_header, 0, sizeof(Elf32_Ehdr));
bf143f
-    memcpy(&elf_header, ELFMAG, SELFMAG);
bf143f
-    elf_header.e_ident[EI_CLASS] = ELFCLASS32;
bf143f
-    elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
bf143f
-    elf_header.e_ident[EI_VERSION] = EV_CURRENT;
bf143f
-    elf_header.e_type = cpu_to_dump16(s, ET_CORE);
bf143f
-    elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
bf143f
-    elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
bf143f
-    elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
bf143f
-    elf_header.e_phoff = cpu_to_dump32(s, s->phdr_offset);
bf143f
-    elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
bf143f
-    elf_header.e_phnum = cpu_to_dump16(s, phnum);
bf143f
+    memset(elf_header, 0, sizeof(Elf32_Ehdr));
bf143f
+    memcpy(elf_header, ELFMAG, SELFMAG);
bf143f
+    elf_header->e_ident[EI_CLASS] = ELFCLASS32;
bf143f
+    elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
bf143f
+    elf_header->e_ident[EI_VERSION] = EV_CURRENT;
bf143f
+    elf_header->e_type = cpu_to_dump16(s, ET_CORE);
bf143f
+    elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
bf143f
+    elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
bf143f
+    elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
bf143f
+    elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset);
bf143f
+    elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
bf143f
+    elf_header->e_phnum = cpu_to_dump16(s, phnum);
bf143f
     if (s->shdr_num) {
bf143f
-        elf_header.e_shoff = cpu_to_dump32(s, s->shdr_offset);
bf143f
-        elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
bf143f
-        elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
bf143f
+        elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
bf143f
+        elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
bf143f
+        elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
bf143f
     }
bf143f
+}
bf143f
 
bf143f
-    ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
bf143f
+static void write_elf_header(DumpState *s, Error **errp)
bf143f
+{
bf143f
+    Elf32_Ehdr elf32_header;
bf143f
+    Elf64_Ehdr elf64_header;
bf143f
+    size_t header_size;
bf143f
+    void *header_ptr;
bf143f
+    int ret;
bf143f
+
bf143f
+    if (dump_is_64bit(s)) {
bf143f
+        prepare_elf64_header(s, &elf64_header);
bf143f
+        header_size = sizeof(elf64_header);
bf143f
+        header_ptr = &elf64_header;
bf143f
+    } else {
bf143f
+        prepare_elf32_header(s, &elf32_header);
bf143f
+        header_size = sizeof(elf32_header);
bf143f
+        header_ptr = &elf32_header;
bf143f
+    }
bf143f
+
bf143f
+    ret = fd_write_vmcore(header_ptr, header_size, s);
bf143f
     if (ret < 0) {
bf143f
         error_setg_errno(errp, -ret, "dump: failed to write elf header");
bf143f
     }
bf143f
@@ -565,11 +575,7 @@ static void dump_begin(DumpState *s, Error **errp)
bf143f
      */
bf143f
 
bf143f
     /* write elf header to vmcore */
bf143f
-    if (dump_is_64bit(s)) {
bf143f
-        write_elf64_header(s, errp);
bf143f
-    } else {
bf143f
-        write_elf32_header(s, errp);
bf143f
-    }
bf143f
+    write_elf_header(s, errp);
bf143f
     if (*errp) {
bf143f
         return;
bf143f
     }
bf143f
-- 
bf143f
2.37.3
bf143f