diff mbox series

[v3,7/9] dump: Consolidate phdr note writes

Message ID 20220330123603.107120-8-frankja@linux.ibm.com (mailing list archive)
State New, archived
Headers show
Series dump: Cleanup and consolidation | expand

Commit Message

Janosch Frank March 30, 2022, 12:36 p.m. UTC
There's no need to have two write functions. Let's rather have two
functions that set the data for elf 32/64 and then write it in a
common function.

Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 dump/dump.c | 94 +++++++++++++++++++++++++++--------------------------
 1 file changed, 48 insertions(+), 46 deletions(-)

Comments

Marc-André Lureau March 31, 2022, 8:56 a.m. UTC | #1
On Wed, Mar 30, 2022 at 4:49 PM Janosch Frank <frankja@linux.ibm.com> wrote:

> There's no need to have two write functions. Let's rather have two
> functions that set the data for elf 32/64 and then write it in a
> common function.
>
> Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>



> ---
>  dump/dump.c | 94 +++++++++++++++++++++++++++--------------------------
>  1 file changed, 48 insertions(+), 46 deletions(-)
>
> diff --git a/dump/dump.c b/dump/dump.c
> index a7cf112d8f..365798f5a1 100644
> --- a/dump/dump.c
> +++ b/dump/dump.c
> @@ -236,24 +236,15 @@ static void write_elf32_load(DumpState *s,
> MemoryMapping *memory_mapping,
>      }
>  }
>
> -static void write_elf64_note(DumpState *s, Error **errp)
> +static void write_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
>  {
> -    Elf64_Phdr phdr;
> -    int ret;
> -
> -    memset(&phdr, 0, sizeof(Elf64_Phdr));
> -    phdr.p_type = cpu_to_dump32(s, PT_NOTE);
> -    phdr.p_offset = cpu_to_dump64(s, s->note_offset);
> -    phdr.p_paddr = 0;
> -    phdr.p_filesz = cpu_to_dump64(s, s->note_size);
> -    phdr.p_memsz = cpu_to_dump64(s, s->note_size);
> -    phdr.p_vaddr = 0;
> -
> -    ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
> -    if (ret < 0) {
> -        error_setg_errno(errp, -ret,
> -                         "dump: failed to write program header table");
> -    }
> +    memset(phdr, 0, sizeof(*phdr));
> +    phdr->p_type = cpu_to_dump32(s, PT_NOTE);
> +    phdr->p_offset = cpu_to_dump64(s, s->note_offset);
> +    phdr->p_paddr = 0;
> +    phdr->p_filesz = cpu_to_dump64(s, s->note_size);
> +    phdr->p_memsz = cpu_to_dump64(s, s->note_size);
> +    phdr->p_vaddr = 0;
>  }
>
>  static inline int cpu_index(CPUState *cpu)
> @@ -301,24 +292,15 @@ static void write_elf64_notes(WriteCoreDumpFunction
> f, DumpState *s,
>      write_guest_note(f, s, errp);
>  }
>
> -static void write_elf32_note(DumpState *s, Error **errp)
> +static void write_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
>  {
> -    Elf32_Phdr phdr;
> -    int ret;
> -
> -    memset(&phdr, 0, sizeof(Elf32_Phdr));
> -    phdr.p_type = cpu_to_dump32(s, PT_NOTE);
> -    phdr.p_offset = cpu_to_dump32(s, s->note_offset);
> -    phdr.p_paddr = 0;
> -    phdr.p_filesz = cpu_to_dump32(s, s->note_size);
> -    phdr.p_memsz = cpu_to_dump32(s, s->note_size);
> -    phdr.p_vaddr = 0;
> -
> -    ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
> -    if (ret < 0) {
> -        error_setg_errno(errp, -ret,
> -                         "dump: failed to write program header table");
> -    }
> +    memset(phdr, 0, sizeof(*phdr));
> +    phdr->p_type = cpu_to_dump32(s, PT_NOTE);
> +    phdr->p_offset = cpu_to_dump32(s, s->note_offset);
> +    phdr->p_paddr = 0;
> +    phdr->p_filesz = cpu_to_dump32(s, s->note_size);
> +    phdr->p_memsz = cpu_to_dump32(s, s->note_size);
> +    phdr->p_vaddr = 0;
>  }
>
>  static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
> @@ -348,6 +330,32 @@ static void write_elf32_notes(WriteCoreDumpFunction
> f, DumpState *s,
>      write_guest_note(f, s, errp);
>  }
>
> +static void write_elf_phdr_note(DumpState *s, Error **errp)
> +{
> +    ERRP_GUARD();
> +    Elf32_Phdr phdr32;
> +    Elf64_Phdr phdr64;
> +    void *phdr;
> +    size_t size;
> +    int ret;
> +
> +    if (dump_is_64bit(s)) {
> +        write_elf64_phdr_note(s, &phdr64);
> +        size = sizeof(phdr64);
> +        phdr = &phdr64;
> +    } else {
> +        write_elf32_phdr_note(s, &phdr32);
> +        size = sizeof(phdr32);
> +        phdr = &phdr32;
> +    }
> +
> +    ret = fd_write_vmcore(phdr, size, s);
> +    if (ret < 0) {
> +        error_setg_errno(errp, -ret,
> +                         "dump: failed to write program header table");
> +    }
> +}
> +
>  static void write_elf_section(DumpState *s, int type, Error **errp)
>  {
>      Elf32_Shdr shdr32;
> @@ -541,13 +549,13 @@ static void dump_begin(DumpState *s, Error **errp)
>          return;
>      }
>
> -    if (dump_is_64bit(s)) {
> -        /* write PT_NOTE to vmcore */
> -        write_elf64_note(s, errp);
> -        if (*errp) {
> -            return;
> -        }
> +    /* write PT_NOTE to vmcore */
> +    write_elf_phdr_note(s, errp);
> +    if (*errp) {
> +        return;
> +    }
>
> +    if (dump_is_64bit(s)) {
>          /* write all PT_LOAD to vmcore */
>          write_elf_loads(s, errp);
>          if (*errp) {
> @@ -568,12 +576,6 @@ static void dump_begin(DumpState *s, Error **errp)
>              return;
>          }
>      } else {
> -        /* write PT_NOTE to vmcore */
> -        write_elf32_note(s, errp);
> -        if (*errp) {
> -            return;
> -        }
> -
>          /* write all PT_LOAD to vmcore */
>          write_elf_loads(s, errp);
>          if (*errp) {
> --
> 2.32.0
>
>
>
diff mbox series

Patch

diff --git a/dump/dump.c b/dump/dump.c
index a7cf112d8f..365798f5a1 100644
--- a/dump/dump.c
+++ b/dump/dump.c
@@ -236,24 +236,15 @@  static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
     }
 }
 
-static void write_elf64_note(DumpState *s, Error **errp)
+static void write_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
 {
-    Elf64_Phdr phdr;
-    int ret;
-
-    memset(&phdr, 0, sizeof(Elf64_Phdr));
-    phdr.p_type = cpu_to_dump32(s, PT_NOTE);
-    phdr.p_offset = cpu_to_dump64(s, s->note_offset);
-    phdr.p_paddr = 0;
-    phdr.p_filesz = cpu_to_dump64(s, s->note_size);
-    phdr.p_memsz = cpu_to_dump64(s, s->note_size);
-    phdr.p_vaddr = 0;
-
-    ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
-    if (ret < 0) {
-        error_setg_errno(errp, -ret,
-                         "dump: failed to write program header table");
-    }
+    memset(phdr, 0, sizeof(*phdr));
+    phdr->p_type = cpu_to_dump32(s, PT_NOTE);
+    phdr->p_offset = cpu_to_dump64(s, s->note_offset);
+    phdr->p_paddr = 0;
+    phdr->p_filesz = cpu_to_dump64(s, s->note_size);
+    phdr->p_memsz = cpu_to_dump64(s, s->note_size);
+    phdr->p_vaddr = 0;
 }
 
 static inline int cpu_index(CPUState *cpu)
@@ -301,24 +292,15 @@  static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
     write_guest_note(f, s, errp);
 }
 
-static void write_elf32_note(DumpState *s, Error **errp)
+static void write_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
 {
-    Elf32_Phdr phdr;
-    int ret;
-
-    memset(&phdr, 0, sizeof(Elf32_Phdr));
-    phdr.p_type = cpu_to_dump32(s, PT_NOTE);
-    phdr.p_offset = cpu_to_dump32(s, s->note_offset);
-    phdr.p_paddr = 0;
-    phdr.p_filesz = cpu_to_dump32(s, s->note_size);
-    phdr.p_memsz = cpu_to_dump32(s, s->note_size);
-    phdr.p_vaddr = 0;
-
-    ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
-    if (ret < 0) {
-        error_setg_errno(errp, -ret,
-                         "dump: failed to write program header table");
-    }
+    memset(phdr, 0, sizeof(*phdr));
+    phdr->p_type = cpu_to_dump32(s, PT_NOTE);
+    phdr->p_offset = cpu_to_dump32(s, s->note_offset);
+    phdr->p_paddr = 0;
+    phdr->p_filesz = cpu_to_dump32(s, s->note_size);
+    phdr->p_memsz = cpu_to_dump32(s, s->note_size);
+    phdr->p_vaddr = 0;
 }
 
 static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
@@ -348,6 +330,32 @@  static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
     write_guest_note(f, s, errp);
 }
 
+static void write_elf_phdr_note(DumpState *s, Error **errp)
+{
+    ERRP_GUARD();
+    Elf32_Phdr phdr32;
+    Elf64_Phdr phdr64;
+    void *phdr;
+    size_t size;
+    int ret;
+
+    if (dump_is_64bit(s)) {
+        write_elf64_phdr_note(s, &phdr64);
+        size = sizeof(phdr64);
+        phdr = &phdr64;
+    } else {
+        write_elf32_phdr_note(s, &phdr32);
+        size = sizeof(phdr32);
+        phdr = &phdr32;
+    }
+
+    ret = fd_write_vmcore(phdr, size, s);
+    if (ret < 0) {
+        error_setg_errno(errp, -ret,
+                         "dump: failed to write program header table");
+    }
+}
+
 static void write_elf_section(DumpState *s, int type, Error **errp)
 {
     Elf32_Shdr shdr32;
@@ -541,13 +549,13 @@  static void dump_begin(DumpState *s, Error **errp)
         return;
     }
 
-    if (dump_is_64bit(s)) {
-        /* write PT_NOTE to vmcore */
-        write_elf64_note(s, errp);
-        if (*errp) {
-            return;
-        }
+    /* write PT_NOTE to vmcore */
+    write_elf_phdr_note(s, errp);
+    if (*errp) {
+        return;
+    }
 
+    if (dump_is_64bit(s)) {
         /* write all PT_LOAD to vmcore */
         write_elf_loads(s, errp);
         if (*errp) {
@@ -568,12 +576,6 @@  static void dump_begin(DumpState *s, Error **errp)
             return;
         }
     } else {
-        /* write PT_NOTE to vmcore */
-        write_elf32_note(s, errp);
-        if (*errp) {
-            return;
-        }
-
         /* write all PT_LOAD to vmcore */
         write_elf_loads(s, errp);
         if (*errp) {