Blame SOURCES/kvm-dump-Use-a-buffer-for-ELF-section-data-and-headers.patch

62547e
From a918c7305ec7c68e8bc37b449f71e75d84124cd0 Mon Sep 17 00:00:00 2001
62547e
From: Janosch Frank <frankja@linux.ibm.com>
62547e
Date: Mon, 17 Oct 2022 08:38:13 +0000
62547e
Subject: [PATCH 32/42] dump: Use a buffer for ELF section data and headers
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: [32/41] e1a03e202e67764581e486f37e13e479200e5846
62547e
62547e
Currently we're writing the NULL section header if we overflow the
62547e
physical header number in the ELF header. But in the future we'll add
62547e
custom section headers AND section data.
62547e
62547e
To facilitate this we need to rearange section handling a bit. As with
62547e
the other ELF headers we split the code into a prepare and a write
62547e
step.
62547e
62547e
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
62547e
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
62547e
Message-Id: <20221017083822.43118-2-frankja@linux.ibm.com>
62547e
(cherry picked from commit e41ed29bcee5cb16715317bcf290f6b5c196eb0a)
62547e
Signed-off-by: Cédric Le Goater <clg@redhat.com>
62547e
---
62547e
 dump/dump.c           | 75 +++++++++++++++++++++++++++++--------------
62547e
 include/sysemu/dump.h |  2 ++
62547e
 2 files changed, 53 insertions(+), 24 deletions(-)
62547e
62547e
diff --git a/dump/dump.c b/dump/dump.c
62547e
index 88177fa886..4142b4cc0c 100644
62547e
--- a/dump/dump.c
62547e
+++ b/dump/dump.c
62547e
@@ -381,31 +381,60 @@ static void write_elf_phdr_note(DumpState *s, Error **errp)
62547e
     }
62547e
 }
62547e
 
62547e
-static void write_elf_section(DumpState *s, int type, Error **errp)
62547e
+static void prepare_elf_section_hdr_zero(DumpState *s)
62547e
 {
62547e
-    Elf32_Shdr shdr32;
62547e
-    Elf64_Shdr shdr64;
62547e
-    int shdr_size;
62547e
-    void *shdr;
62547e
-    int ret;
62547e
+    if (dump_is_64bit(s)) {
62547e
+        Elf64_Shdr *shdr64 = s->elf_section_hdrs;
62547e
 
62547e
-    if (type == 0) {
62547e
-        shdr_size = sizeof(Elf32_Shdr);
62547e
-        memset(&shdr32, 0, shdr_size);
62547e
-        shdr32.sh_info = cpu_to_dump32(s, s->phdr_num);
62547e
-        shdr = &shdr32;
62547e
+        shdr64->sh_info = cpu_to_dump32(s, s->phdr_num);
62547e
     } else {
62547e
-        shdr_size = sizeof(Elf64_Shdr);
62547e
-        memset(&shdr64, 0, shdr_size);
62547e
-        shdr64.sh_info = cpu_to_dump32(s, s->phdr_num);
62547e
-        shdr = &shdr64;
62547e
+        Elf32_Shdr *shdr32 = s->elf_section_hdrs;
62547e
+
62547e
+        shdr32->sh_info = cpu_to_dump32(s, s->phdr_num);
62547e
+    }
62547e
+}
62547e
+
62547e
+static void prepare_elf_section_hdrs(DumpState *s)
62547e
+{
62547e
+    size_t len, sizeof_shdr;
62547e
+
62547e
+    /*
62547e
+     * Section ordering:
62547e
+     * - HDR zero
62547e
+     */
62547e
+    sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
62547e
+    len = sizeof_shdr * s->shdr_num;
62547e
+    s->elf_section_hdrs = g_malloc0(len);
62547e
+
62547e
+    /*
62547e
+     * The first section header is ALWAYS a special initial section
62547e
+     * header.
62547e
+     *
62547e
+     * The header should be 0 with one exception being that if
62547e
+     * phdr_num is PN_XNUM then the sh_info field contains the real
62547e
+     * number of segment entries.
62547e
+     *
62547e
+     * As we zero allocate the buffer we will only need to modify
62547e
+     * sh_info for the PN_XNUM case.
62547e
+     */
62547e
+    if (s->phdr_num >= PN_XNUM) {
62547e
+        prepare_elf_section_hdr_zero(s);
62547e
     }
62547e
+}
62547e
 
62547e
-    ret = fd_write_vmcore(shdr, shdr_size, s);
62547e
+static void write_elf_section_headers(DumpState *s, Error **errp)
62547e
+{
62547e
+    size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
62547e
+    int ret;
62547e
+
62547e
+    prepare_elf_section_hdrs(s);
62547e
+
62547e
+    ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
62547e
     if (ret < 0) {
62547e
-        error_setg_errno(errp, -ret,
62547e
-                         "dump: failed to write section header table");
62547e
+        error_setg_errno(errp, -ret, "dump: failed to write section headers");
62547e
     }
62547e
+
62547e
+    g_free(s->elf_section_hdrs);
62547e
 }
62547e
 
62547e
 static void write_data(DumpState *s, void *buf, int length, Error **errp)
62547e
@@ -592,12 +621,10 @@ static void dump_begin(DumpState *s, Error **errp)
62547e
         return;
62547e
     }
62547e
 
62547e
-    /* write section to vmcore */
62547e
-    if (s->shdr_num) {
62547e
-        write_elf_section(s, 1, errp);
62547e
-        if (*errp) {
62547e
-            return;
62547e
-        }
62547e
+    /* write section headers to vmcore */
62547e
+    write_elf_section_headers(s, errp);
62547e
+    if (*errp) {
62547e
+        return;
62547e
     }
62547e
 
62547e
     /* write notes to vmcore */
62547e
diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
62547e
index b62513d87d..9995f65dc8 100644
62547e
--- a/include/sysemu/dump.h
62547e
+++ b/include/sysemu/dump.h
62547e
@@ -177,6 +177,8 @@ typedef struct DumpState {
62547e
     int64_t filter_area_begin;  /* Start address of partial guest memory area */
62547e
     int64_t filter_area_length; /* Length of partial guest memory area */
62547e
 
62547e
+    void *elf_section_hdrs;     /* Pointer to section header buffer */
62547e
+
62547e
     uint8_t *note_buf;          /* buffer for notes */
62547e
     size_t note_buf_offset;     /* the writing place in note_buf */
62547e
     uint32_t nr_cpus;           /* number of guest's cpu */
62547e
-- 
62547e
2.37.3
62547e