thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
bf143f
From bee31226b87d0b05faae84e88cce3af1b8dabbfd Mon Sep 17 00:00:00 2001
bf143f
From: Janosch Frank <frankja@linux.ibm.com>
bf143f
Date: Wed, 30 Mar 2022 12:35:59 +0000
bf143f
Subject: [PATCH 17/42] dump: Add more offset variables
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: [17/41] fbe629e1476e8a0e039f989af6e1f4707075ba01
bf143f
bf143f
Offset calculations are easy enough to get wrong. Let's add a few
bf143f
variables to make moving around elf headers and data sections easier.
bf143f
bf143f
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
bf143f
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
bf143f
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
bf143f
Message-Id: <20220330123603.107120-6-frankja@linux.ibm.com>
bf143f
(cherry picked from commit e71d353360bb09a8e784e35d78370c691f6ea185)
bf143f
Signed-off-by: Cédric Le Goater <clg@redhat.com>
bf143f
---
bf143f
 dump/dump.c           | 35 +++++++++++++++--------------------
bf143f
 include/sysemu/dump.h |  4 ++++
bf143f
 2 files changed, 19 insertions(+), 20 deletions(-)
bf143f
bf143f
diff --git a/dump/dump.c b/dump/dump.c
bf143f
index 5cc2322325..85a402b38c 100644
bf143f
--- a/dump/dump.c
bf143f
+++ b/dump/dump.c
bf143f
@@ -142,13 +142,11 @@ static void write_elf64_header(DumpState *s, Error **errp)
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, sizeof(Elf64_Ehdr));
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
-        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_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
@@ -179,13 +177,11 @@ static void write_elf32_header(DumpState *s, Error **errp)
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, sizeof(Elf32_Ehdr));
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
-        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_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
@@ -248,12 +244,11 @@ static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
bf143f
 static void write_elf64_note(DumpState *s, Error **errp)
bf143f
 {
bf143f
     Elf64_Phdr phdr;
bf143f
-    hwaddr begin = s->memory_offset - s->note_size;
bf143f
     int ret;
bf143f
 
bf143f
     memset(&phdr, 0, sizeof(Elf64_Phdr));
bf143f
     phdr.p_type = cpu_to_dump32(s, PT_NOTE);
bf143f
-    phdr.p_offset = cpu_to_dump64(s, begin);
bf143f
+    phdr.p_offset = cpu_to_dump64(s, s->note_offset);
bf143f
     phdr.p_paddr = 0;
bf143f
     phdr.p_filesz = cpu_to_dump64(s, s->note_size);
bf143f
     phdr.p_memsz = cpu_to_dump64(s, s->note_size);
bf143f
@@ -313,13 +308,12 @@ static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
bf143f
 
bf143f
 static void write_elf32_note(DumpState *s, Error **errp)
bf143f
 {
bf143f
-    hwaddr begin = s->memory_offset - s->note_size;
bf143f
     Elf32_Phdr phdr;
bf143f
     int ret;
bf143f
 
bf143f
     memset(&phdr, 0, sizeof(Elf32_Phdr));
bf143f
     phdr.p_type = cpu_to_dump32(s, PT_NOTE);
bf143f
-    phdr.p_offset = cpu_to_dump32(s, begin);
bf143f
+    phdr.p_offset = cpu_to_dump32(s, s->note_offset);
bf143f
     phdr.p_paddr = 0;
bf143f
     phdr.p_filesz = cpu_to_dump32(s, s->note_size);
bf143f
     phdr.p_memsz = cpu_to_dump32(s, s->note_size);
bf143f
@@ -1826,15 +1820,16 @@ static void dump_init(DumpState *s, int fd, bool has_format,
bf143f
     }
bf143f
 
bf143f
     if (s->dump_info.d_class == ELFCLASS64) {
bf143f
-        s->memory_offset = sizeof(Elf64_Ehdr) +
bf143f
-                           sizeof(Elf64_Phdr) * s->phdr_num +
bf143f
-                           sizeof(Elf64_Shdr) * s->shdr_num +
bf143f
-                           s->note_size;
bf143f
+        s->phdr_offset = sizeof(Elf64_Ehdr);
bf143f
+        s->shdr_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
bf143f
+        s->note_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
bf143f
+        s->memory_offset = s->note_offset + s->note_size;
bf143f
     } else {
bf143f
-        s->memory_offset = sizeof(Elf32_Ehdr) +
bf143f
-                           sizeof(Elf32_Phdr) * s->phdr_num +
bf143f
-                           sizeof(Elf32_Shdr) * s->shdr_num +
bf143f
-                           s->note_size;
bf143f
+
bf143f
+        s->phdr_offset = sizeof(Elf32_Ehdr);
bf143f
+        s->shdr_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
bf143f
+        s->note_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
bf143f
+        s->memory_offset = s->note_offset + s->note_size;
bf143f
     }
bf143f
 
bf143f
     return;
bf143f
diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
bf143f
index 19458bffbd..ffc2ea1072 100644
bf143f
--- a/include/sysemu/dump.h
bf143f
+++ b/include/sysemu/dump.h
bf143f
@@ -159,6 +159,10 @@ typedef struct DumpState {
bf143f
     bool resume;
bf143f
     bool detached;
bf143f
     ssize_t note_size;
bf143f
+    hwaddr shdr_offset;
bf143f
+    hwaddr phdr_offset;
bf143f
+    hwaddr section_offset;
bf143f
+    hwaddr note_offset;
bf143f
     hwaddr memory_offset;
bf143f
     int fd;
bf143f
 
bf143f
-- 
bf143f
2.37.3
bf143f