62547e
From f87abe1ef14e80731249ebe9fe1bea569a68e9b4 Mon Sep 17 00:00:00 2001
62547e
From: Janosch Frank <frankja@linux.ibm.com>
62547e
Date: Wed, 30 Mar 2022 12:36:01 +0000
62547e
Subject: [PATCH 19/42] dump: Consolidate phdr note writes
62547e
MIME-Version: 1.0
62547e
Content-Type: text/plain; charset=UTF-8
62547e
Content-Transfer-Encoding: 8bit
62547e
62547e
RH-Author: Cédric Le Goater <clg@redhat.com>
62547e
RH-MergeRequest: 226: s390: Enhanced Interpretation for PCI Functions and Secure Execution guest dump
62547e
RH-Bugzilla: 1664378 2043909
62547e
RH-Acked-by: Thomas Huth <thuth@redhat.com>
62547e
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
62547e
RH-Acked-by: Jon Maloy <jmaloy@redhat.com>
62547e
RH-Commit: [19/41] 180c4c0ab4941a0bf366dc7f32ee035e03daa6c0
62547e
62547e
There's no need to have two write functions. Let's rather have two
62547e
functions that set the data for elf 32/64 and then write it in a
62547e
common function.
62547e
62547e
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
62547e
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
62547e
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
62547e
Message-Id: <20220330123603.107120-8-frankja@linux.ibm.com>
62547e
(cherry picked from commit bc7d558017e6700f9a05c61b0b638a8994945f0d)
62547e
Signed-off-by: Cédric Le Goater <clg@redhat.com>
62547e
---
62547e
 dump/dump.c | 94 +++++++++++++++++++++++++++--------------------------
62547e
 1 file changed, 48 insertions(+), 46 deletions(-)
62547e
62547e
diff --git a/dump/dump.c b/dump/dump.c
62547e
index 6394e94023..823ca32883 100644
62547e
--- a/dump/dump.c
62547e
+++ b/dump/dump.c
62547e
@@ -246,24 +246,15 @@ static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
62547e
     }
62547e
 }
62547e
 
62547e
-static void write_elf64_note(DumpState *s, Error **errp)
62547e
+static void write_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
62547e
 {
62547e
-    Elf64_Phdr phdr;
62547e
-    int ret;
62547e
-
62547e
-    memset(&phdr, 0, sizeof(Elf64_Phdr));
62547e
-    phdr.p_type = cpu_to_dump32(s, PT_NOTE);
62547e
-    phdr.p_offset = cpu_to_dump64(s, s->note_offset);
62547e
-    phdr.p_paddr = 0;
62547e
-    phdr.p_filesz = cpu_to_dump64(s, s->note_size);
62547e
-    phdr.p_memsz = cpu_to_dump64(s, s->note_size);
62547e
-    phdr.p_vaddr = 0;
62547e
-
62547e
-    ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
62547e
-    if (ret < 0) {
62547e
-        error_setg_errno(errp, -ret,
62547e
-                         "dump: failed to write program header table");
62547e
-    }
62547e
+    memset(phdr, 0, sizeof(*phdr));
62547e
+    phdr->p_type = cpu_to_dump32(s, PT_NOTE);
62547e
+    phdr->p_offset = cpu_to_dump64(s, s->note_offset);
62547e
+    phdr->p_paddr = 0;
62547e
+    phdr->p_filesz = cpu_to_dump64(s, s->note_size);
62547e
+    phdr->p_memsz = cpu_to_dump64(s, s->note_size);
62547e
+    phdr->p_vaddr = 0;
62547e
 }
62547e
 
62547e
 static inline int cpu_index(CPUState *cpu)
62547e
@@ -311,24 +302,15 @@ static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
62547e
     write_guest_note(f, s, errp);
62547e
 }
62547e
 
62547e
-static void write_elf32_note(DumpState *s, Error **errp)
62547e
+static void write_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
62547e
 {
62547e
-    Elf32_Phdr phdr;
62547e
-    int ret;
62547e
-
62547e
-    memset(&phdr, 0, sizeof(Elf32_Phdr));
62547e
-    phdr.p_type = cpu_to_dump32(s, PT_NOTE);
62547e
-    phdr.p_offset = cpu_to_dump32(s, s->note_offset);
62547e
-    phdr.p_paddr = 0;
62547e
-    phdr.p_filesz = cpu_to_dump32(s, s->note_size);
62547e
-    phdr.p_memsz = cpu_to_dump32(s, s->note_size);
62547e
-    phdr.p_vaddr = 0;
62547e
-
62547e
-    ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
62547e
-    if (ret < 0) {
62547e
-        error_setg_errno(errp, -ret,
62547e
-                         "dump: failed to write program header table");
62547e
-    }
62547e
+    memset(phdr, 0, sizeof(*phdr));
62547e
+    phdr->p_type = cpu_to_dump32(s, PT_NOTE);
62547e
+    phdr->p_offset = cpu_to_dump32(s, s->note_offset);
62547e
+    phdr->p_paddr = 0;
62547e
+    phdr->p_filesz = cpu_to_dump32(s, s->note_size);
62547e
+    phdr->p_memsz = cpu_to_dump32(s, s->note_size);
62547e
+    phdr->p_vaddr = 0;
62547e
 }
62547e
 
62547e
 static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
62547e
@@ -358,6 +340,32 @@ static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
62547e
     write_guest_note(f, s, errp);
62547e
 }
62547e
 
62547e
+static void write_elf_phdr_note(DumpState *s, Error **errp)
62547e
+{
62547e
+    ERRP_GUARD();
62547e
+    Elf32_Phdr phdr32;
62547e
+    Elf64_Phdr phdr64;
62547e
+    void *phdr;
62547e
+    size_t size;
62547e
+    int ret;
62547e
+
62547e
+    if (dump_is_64bit(s)) {
62547e
+        write_elf64_phdr_note(s, &phdr64);
62547e
+        size = sizeof(phdr64);
62547e
+        phdr = &phdr64;
62547e
+    } else {
62547e
+        write_elf32_phdr_note(s, &phdr32);
62547e
+        size = sizeof(phdr32);
62547e
+        phdr = &phdr32;
62547e
+    }
62547e
+
62547e
+    ret = fd_write_vmcore(phdr, size, s);
62547e
+    if (ret < 0) {
62547e
+        error_setg_errno(errp, -ret,
62547e
+                         "dump: failed to write program header table");
62547e
+    }
62547e
+}
62547e
+
62547e
 static void write_elf_section(DumpState *s, int type, Error **errp)
62547e
 {
62547e
     Elf32_Shdr shdr32;
62547e
@@ -551,13 +559,13 @@ static void dump_begin(DumpState *s, Error **errp)
62547e
         return;
62547e
     }
62547e
 
62547e
-    if (dump_is_64bit(s)) {
62547e
-        /* write PT_NOTE to vmcore */
62547e
-        write_elf64_note(s, errp);
62547e
-        if (*errp) {
62547e
-            return;
62547e
-        }
62547e
+    /* write PT_NOTE to vmcore */
62547e
+    write_elf_phdr_note(s, errp);
62547e
+    if (*errp) {
62547e
+        return;
62547e
+    }
62547e
 
62547e
+    if (dump_is_64bit(s)) {
62547e
         /* write all PT_LOAD to vmcore */
62547e
         write_elf_loads(s, errp);
62547e
         if (*errp) {
62547e
@@ -578,12 +586,6 @@ static void dump_begin(DumpState *s, Error **errp)
62547e
             return;
62547e
         }
62547e
     } else {
62547e
-        /* write PT_NOTE to vmcore */
62547e
-        write_elf32_note(s, errp);
62547e
-        if (*errp) {
62547e
-            return;
62547e
-        }
62547e
-
62547e
         /* write all PT_LOAD to vmcore */
62547e
         write_elf_loads(s, errp);
62547e
         if (*errp) {
62547e
-- 
62547e
2.37.3
62547e