bf143f
From eb763bec53d6b9aea7a6b60b0cf8c5d8b5f1b35c Mon Sep 17 00:00:00 2001
bf143f
From: Janosch Frank <frankja@linux.ibm.com>
bf143f
Date: Thu, 7 Apr 2022 09:48:24 +0000
bf143f
Subject: [PATCH 14/42] dump: Remove the sh_info variable
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: [14/41] 24af12b78c8f5a02cf85df2f6b1d64249f9499c9
bf143f
bf143f
There's no need to have phdr_num and sh_info at the same time. We can
bf143f
make phdr_num 32 bit and set PN_XNUM when we write the header if
bf143f
phdr_num >= PN_XNUM.
bf143f
bf143f
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
bf143f
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
bf143f
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
bf143f
Message-Id: <20220407094824.5074-1-frankja@linux.ibm.com>
bf143f
(cherry picked from commit 046bc4160bc780eaacc2d702a2589f1a7a01188d)
bf143f
Signed-off-by: Cédric Le Goater <clg@redhat.com>
bf143f
---
bf143f
 dump/dump.c           | 44 +++++++++++++++++++++++--------------------
bf143f
 include/sysemu/dump.h |  3 +--
bf143f
 2 files changed, 25 insertions(+), 22 deletions(-)
bf143f
bf143f
diff --git a/dump/dump.c b/dump/dump.c
bf143f
index 9876123f2e..7236b167cc 100644
bf143f
--- a/dump/dump.c
bf143f
+++ b/dump/dump.c
bf143f
@@ -124,6 +124,12 @@ static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
bf143f
 
bf143f
 static void write_elf64_header(DumpState *s, Error **errp)
bf143f
 {
bf143f
+    /*
bf143f
+     * phnum in the elf header is 16 bit, if we have more segments we
bf143f
+     * set phnum to PN_XNUM and write the real number of segments to a
bf143f
+     * special section.
bf143f
+     */
bf143f
+    uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
bf143f
     Elf64_Ehdr elf_header;
bf143f
     int ret;
bf143f
 
bf143f
@@ -138,9 +144,9 @@ static void write_elf64_header(DumpState *s, Error **errp)
bf143f
     elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
bf143f
     elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
bf143f
     elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
bf143f
-    elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
bf143f
+    elf_header.e_phnum = cpu_to_dump16(s, phnum);
bf143f
     if (s->have_section) {
bf143f
-        uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
bf143f
+        uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->phdr_num;
bf143f
 
bf143f
         elf_header.e_shoff = cpu_to_dump64(s, shoff);
bf143f
         elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
bf143f
@@ -155,6 +161,12 @@ static void write_elf64_header(DumpState *s, Error **errp)
bf143f
 
bf143f
 static void write_elf32_header(DumpState *s, Error **errp)
bf143f
 {
bf143f
+    /*
bf143f
+     * phnum in the elf header is 16 bit, if we have more segments we
bf143f
+     * set phnum to PN_XNUM and write the real number of segments to a
bf143f
+     * special section.
bf143f
+     */
bf143f
+    uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
bf143f
     Elf32_Ehdr elf_header;
bf143f
     int ret;
bf143f
 
bf143f
@@ -169,9 +181,9 @@ static void write_elf32_header(DumpState *s, Error **errp)
bf143f
     elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
bf143f
     elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
bf143f
     elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
bf143f
-    elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
bf143f
+    elf_header.e_phnum = cpu_to_dump16(s, phnum);
bf143f
     if (s->have_section) {
bf143f
-        uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
bf143f
+        uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->phdr_num;
bf143f
 
bf143f
         elf_header.e_shoff = cpu_to_dump32(s, shoff);
bf143f
         elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
bf143f
@@ -358,12 +370,12 @@ static void write_elf_section(DumpState *s, int type, Error **errp)
bf143f
     if (type == 0) {
bf143f
         shdr_size = sizeof(Elf32_Shdr);
bf143f
         memset(&shdr32, 0, shdr_size);
bf143f
-        shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
bf143f
+        shdr32.sh_info = cpu_to_dump32(s, s->phdr_num);
bf143f
         shdr = &shdr32;
bf143f
     } else {
bf143f
         shdr_size = sizeof(Elf64_Shdr);
bf143f
         memset(&shdr64, 0, shdr_size);
bf143f
-        shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
bf143f
+        shdr64.sh_info = cpu_to_dump32(s, s->phdr_num);
bf143f
         shdr = &shdr64;
bf143f
     }
bf143f
 
bf143f
@@ -478,13 +490,6 @@ static void write_elf_loads(DumpState *s, Error **errp)
bf143f
     hwaddr offset, filesz;
bf143f
     MemoryMapping *memory_mapping;
bf143f
     uint32_t phdr_index = 1;
bf143f
-    uint32_t max_index;
bf143f
-
bf143f
-    if (s->have_section) {
bf143f
-        max_index = s->sh_info;
bf143f
-    } else {
bf143f
-        max_index = s->phdr_num;
bf143f
-    }
bf143f
 
bf143f
     QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
bf143f
         get_offset_range(memory_mapping->phys_addr,
bf143f
@@ -502,7 +507,7 @@ static void write_elf_loads(DumpState *s, Error **errp)
bf143f
             return;
bf143f
         }
bf143f
 
bf143f
-        if (phdr_index >= max_index) {
bf143f
+        if (phdr_index >= s->phdr_num) {
bf143f
             break;
bf143f
         }
bf143f
     }
bf143f
@@ -1809,22 +1814,21 @@ static void dump_init(DumpState *s, int fd, bool has_format,
bf143f
         s->phdr_num += s->list.num;
bf143f
         s->have_section = false;
bf143f
     } else {
bf143f
+        /* sh_info of section 0 holds the real number of phdrs */
bf143f
         s->have_section = true;
bf143f
-        s->phdr_num = PN_XNUM;
bf143f
-        s->sh_info = 1; /* PT_NOTE */
bf143f
 
bf143f
         /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
bf143f
         if (s->list.num <= UINT32_MAX - 1) {
bf143f
-            s->sh_info += s->list.num;
bf143f
+            s->phdr_num += s->list.num;
bf143f
         } else {
bf143f
-            s->sh_info = UINT32_MAX;
bf143f
+            s->phdr_num = UINT32_MAX;
bf143f
         }
bf143f
     }
bf143f
 
bf143f
     if (s->dump_info.d_class == ELFCLASS64) {
bf143f
         if (s->have_section) {
bf143f
             s->memory_offset = sizeof(Elf64_Ehdr) +
bf143f
-                               sizeof(Elf64_Phdr) * s->sh_info +
bf143f
+                               sizeof(Elf64_Phdr) * s->phdr_num +
bf143f
                                sizeof(Elf64_Shdr) + s->note_size;
bf143f
         } else {
bf143f
             s->memory_offset = sizeof(Elf64_Ehdr) +
bf143f
@@ -1833,7 +1837,7 @@ static void dump_init(DumpState *s, int fd, bool has_format,
bf143f
     } else {
bf143f
         if (s->have_section) {
bf143f
             s->memory_offset = sizeof(Elf32_Ehdr) +
bf143f
-                               sizeof(Elf32_Phdr) * s->sh_info +
bf143f
+                               sizeof(Elf32_Phdr) * s->phdr_num +
bf143f
                                sizeof(Elf32_Shdr) + s->note_size;
bf143f
         } else {
bf143f
             s->memory_offset = sizeof(Elf32_Ehdr) +
bf143f
diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
bf143f
index 250143cb5a..b463fc9c02 100644
bf143f
--- a/include/sysemu/dump.h
bf143f
+++ b/include/sysemu/dump.h
bf143f
@@ -154,8 +154,7 @@ typedef struct DumpState {
bf143f
     GuestPhysBlockList guest_phys_blocks;
bf143f
     ArchDumpInfo dump_info;
bf143f
     MemoryMappingList list;
bf143f
-    uint16_t phdr_num;
bf143f
-    uint32_t sh_info;
bf143f
+    uint32_t phdr_num;
bf143f
     bool have_section;
bf143f
     bool resume;
bf143f
     bool detached;
bf143f
-- 
bf143f
2.37.3
bf143f