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