diff mbox series

[1/7] dump: Introduce shdr_num to decrease complexity

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

Commit Message

Janosch Frank March 1, 2022, 2:22 p.m. UTC
Let's move from a boolean to a int variable which will later enable us
to store the number of sections that are in the dump file.

Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
---
 dump/dump.c           | 43 ++++++++++++++++++-------------------------
 include/sysemu/dump.h |  2 +-
 2 files changed, 19 insertions(+), 26 deletions(-)

Comments

Marc-André Lureau March 1, 2022, 2:42 p.m. UTC | #1
Hi

On Tue, Mar 1, 2022 at 6:33 PM Janosch Frank <frankja@linux.ibm.com> wrote:

> Let's move from a boolean to a int variable which will later enable us
> to store the number of sections that are in the dump file.
>
> Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
> ---
>  dump/dump.c           | 43 ++++++++++++++++++-------------------------
>  include/sysemu/dump.h |  2 +-
>  2 files changed, 19 insertions(+), 26 deletions(-)
>
> diff --git a/dump/dump.c b/dump/dump.c
> index a84d8b1598..6696d9819a 100644
> --- a/dump/dump.c
> +++ b/dump/dump.c
> @@ -139,12 +139,12 @@ static void write_elf64_header(DumpState *s, Error
> **errp)
>      elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
>      elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
>      elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
> -    if (s->have_section) {
> +    if (s->shdr_num) {
>          uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) *
> s->sh_info;
>
>          elf_header.e_shoff = cpu_to_dump64(s, shoff);
>          elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
> -        elf_header.e_shnum = cpu_to_dump16(s, 1);
> +        elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
>      }
>
>      ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
> @@ -170,12 +170,12 @@ static void write_elf32_header(DumpState *s, Error
> **errp)
>      elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
>      elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
>      elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
> -    if (s->have_section) {
> +    if (s->shdr_num) {
>          uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) *
> s->sh_info;
>
>          elf_header.e_shoff = cpu_to_dump32(s, shoff);
>          elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
> -        elf_header.e_shnum = cpu_to_dump16(s, 1);
> +        elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
>      }
>
>      ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
> @@ -482,7 +482,7 @@ static void write_elf_loads(DumpState *s, Error **errp)
>      uint32_t max_index;
>      Error *local_err = NULL;
>
> -    if (s->have_section) {
> +    if (s->shdr_num) {
>          max_index = s->sh_info;
>      } else {
>          max_index = s->phdr_num;
> @@ -567,7 +567,7 @@ static void dump_begin(DumpState *s, Error **errp)
>          }
>
>          /* write section to vmcore */
> -        if (s->have_section) {
> +        if (s->shdr_num) {
>              write_elf_section(s, 1, &local_err);
>              if (local_err) {
>                  error_propagate(errp, local_err);
> @@ -597,7 +597,7 @@ static void dump_begin(DumpState *s, Error **errp)
>          }
>
>          /* write section to vmcore */
> -        if (s->have_section) {
> +        if (s->shdr_num) {
>              write_elf_section(s, 0, &local_err);
>              if (local_err) {
>                  error_propagate(errp, local_err);
> @@ -1818,11 +1818,12 @@ static void dump_init(DumpState *s, int fd, bool
> has_format,
>       */
>      s->phdr_num = 1; /* PT_NOTE */
>      if (s->list.num < UINT16_MAX - 2) {
> +        s->shdr_num = 0;
>          s->phdr_num += s->list.num;
> -        s->have_section = false;
>      } else {
> -        s->have_section = true;
> +        /* sh_info of section 0 holds the real number of phdrs */
>

...


>          s->phdr_num = PN_XNUM;
> +        s->shdr_num = 1;
>          s->sh_info = 1; /* PT_NOTE */
>
>          /* the type of shdr->sh_info is uint32_t, so we should avoid
> overflow */
> @@ -1834,23 +1835,15 @@ static void dump_init(DumpState *s, int fd, bool
> has_format,
>      }
>
>      if (s->dump_info.d_class == ELFCLASS64) {
> -        if (s->have_section) {
> -            s->memory_offset = sizeof(Elf64_Ehdr) +
> -                               sizeof(Elf64_Phdr) * s->sh_info +
> -                               sizeof(Elf64_Shdr) + s->note_size;
> -        } else {
> -            s->memory_offset = sizeof(Elf64_Ehdr) +
> -                               sizeof(Elf64_Phdr) * s->phdr_num +
> s->note_size;
> -        }
> +        s->memory_offset = sizeof(Elf64_Ehdr) +
> +                           sizeof(Elf64_Phdr) * s->sh_info +
>

The change "removing/replacing sizeof(Elf64_Phdr) * s->phdr_num by *
s->sh_info" should be done as a preliminary step, with more comments.

+                           sizeof(Elf64_Shdr) * s->shdr_num +
> +                           s->note_size;
>      } else {
> -        if (s->have_section) {
> -            s->memory_offset = sizeof(Elf32_Ehdr) +
> -                               sizeof(Elf32_Phdr) * s->sh_info +
> -                               sizeof(Elf32_Shdr) + s->note_size;
> -        } else {
> -            s->memory_offset = sizeof(Elf32_Ehdr) +
> -                               sizeof(Elf32_Phdr) * s->phdr_num +
> s->note_size;
> -        }
> +        s->memory_offset = sizeof(Elf32_Ehdr) +
> +                           sizeof(Elf32_Phdr) * s->sh_info +
> +                           sizeof(Elf32_Shdr) * s->shdr_num +
> +                           s->note_size;
>      }
>
>      return;
> diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
> index 250143cb5a..854341da0d 100644
> --- a/include/sysemu/dump.h
> +++ b/include/sysemu/dump.h
> @@ -155,8 +155,8 @@ typedef struct DumpState {
>      ArchDumpInfo dump_info;
>      MemoryMappingList list;
>      uint16_t phdr_num;
> +    uint32_t shdr_num;
>      uint32_t sh_info;
> -    bool have_section;
>      bool resume;
>      bool detached;
>      ssize_t note_size;
> --
> 2.32.0
>
>
>
Janosch Frank March 8, 2022, 1:16 p.m. UTC | #2
On 3/1/22 15:42, Marc-André Lureau wrote:
> Hi
> 
> On Tue, Mar 1, 2022 at 6:33 PM Janosch Frank <frankja@linux.ibm.com> wrote:
> 
>> Let's move from a boolean to a int variable which will later enable us
>> to store the number of sections that are in the dump file.
>>
>> Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
>> ---
>>   dump/dump.c           | 43 ++++++++++++++++++-------------------------
>>   include/sysemu/dump.h |  2 +-
>>   2 files changed, 19 insertions(+), 26 deletions(-)
>>
>> diff --git a/dump/dump.c b/dump/dump.c
>> index a84d8b1598..6696d9819a 100644
>> --- a/dump/dump.c
>> +++ b/dump/dump.c
>> @@ -139,12 +139,12 @@ static void write_elf64_header(DumpState *s, Error
>> **errp)
>>       elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
>>       elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
>>       elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
>> -    if (s->have_section) {
>> +    if (s->shdr_num) {
>>           uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) *
>> s->sh_info;
>>
>>           elf_header.e_shoff = cpu_to_dump64(s, shoff);
>>           elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
>> -        elf_header.e_shnum = cpu_to_dump16(s, 1);
>> +        elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
>>       }
>>
>>       ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
>> @@ -170,12 +170,12 @@ static void write_elf32_header(DumpState *s, Error
>> **errp)
>>       elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
>>       elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
>>       elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
>> -    if (s->have_section) {
>> +    if (s->shdr_num) {
>>           uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) *
>> s->sh_info;
>>
>>           elf_header.e_shoff = cpu_to_dump32(s, shoff);
>>           elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
>> -        elf_header.e_shnum = cpu_to_dump16(s, 1);
>> +        elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
>>       }
>>
>>       ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
>> @@ -482,7 +482,7 @@ static void write_elf_loads(DumpState *s, Error **errp)
>>       uint32_t max_index;
>>       Error *local_err = NULL;
>>
>> -    if (s->have_section) {
>> +    if (s->shdr_num) {
>>           max_index = s->sh_info;
>>       } else {
>>           max_index = s->phdr_num;
>> @@ -567,7 +567,7 @@ static void dump_begin(DumpState *s, Error **errp)
>>           }
>>
>>           /* write section to vmcore */
>> -        if (s->have_section) {
>> +        if (s->shdr_num) {
>>               write_elf_section(s, 1, &local_err);
>>               if (local_err) {
>>                   error_propagate(errp, local_err);
>> @@ -597,7 +597,7 @@ static void dump_begin(DumpState *s, Error **errp)
>>           }
>>
>>           /* write section to vmcore */
>> -        if (s->have_section) {
>> +        if (s->shdr_num) {
>>               write_elf_section(s, 0, &local_err);
>>               if (local_err) {
>>                   error_propagate(errp, local_err);
>> @@ -1818,11 +1818,12 @@ static void dump_init(DumpState *s, int fd, bool
>> has_format,
>>        */
>>       s->phdr_num = 1; /* PT_NOTE */
>>       if (s->list.num < UINT16_MAX - 2) {
>> +        s->shdr_num = 0;
>>           s->phdr_num += s->list.num;
>> -        s->have_section = false;
>>       } else {
>> -        s->have_section = true;
>> +        /* sh_info of section 0 holds the real number of phdrs */
>>
> 
> ...
> 
> 
>>           s->phdr_num = PN_XNUM;
>> +        s->shdr_num = 1;
>>           s->sh_info = 1; /* PT_NOTE */
>>
>>           /* the type of shdr->sh_info is uint32_t, so we should avoid
>> overflow */
>> @@ -1834,23 +1835,15 @@ static void dump_init(DumpState *s, int fd, bool
>> has_format,
>>       }
>>
>>       if (s->dump_info.d_class == ELFCLASS64) {
>> -        if (s->have_section) {
>> -            s->memory_offset = sizeof(Elf64_Ehdr) +
>> -                               sizeof(Elf64_Phdr) * s->sh_info +
>> -                               sizeof(Elf64_Shdr) + s->note_size;
>> -        } else {
>> -            s->memory_offset = sizeof(Elf64_Ehdr) +
>> -                               sizeof(Elf64_Phdr) * s->phdr_num +
>> s->note_size;
>> -        }
>> +        s->memory_offset = sizeof(Elf64_Ehdr) +
>> +                           sizeof(Elf64_Phdr) * s->sh_info +
>>
> 
> The change "removing/replacing sizeof(Elf64_Phdr) * s->phdr_num by *
> s->sh_info" should be done as a preliminary step, with more comments.

I had another look at this and this patch won't work without the next 
patch anyway. I'll try to recombine and split the patches in a way that 
makes this easier to read.


> 
> +                           sizeof(Elf64_Shdr) * s->shdr_num +
>> +                           s->note_size;
>>       } else {
>> -        if (s->have_section) {
>> -            s->memory_offset = sizeof(Elf32_Ehdr) +
>> -                               sizeof(Elf32_Phdr) * s->sh_info +
>> -                               sizeof(Elf32_Shdr) + s->note_size;
>> -        } else {
>> -            s->memory_offset = sizeof(Elf32_Ehdr) +
>> -                               sizeof(Elf32_Phdr) * s->phdr_num +
>> s->note_size;
>> -        }
>> +        s->memory_offset = sizeof(Elf32_Ehdr) +
>> +                           sizeof(Elf32_Phdr) * s->sh_info +
>> +                           sizeof(Elf32_Shdr) * s->shdr_num +
>> +                           s->note_size;
>>       }
>>
>>       return;
>> diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
>> index 250143cb5a..854341da0d 100644
>> --- a/include/sysemu/dump.h
>> +++ b/include/sysemu/dump.h
>> @@ -155,8 +155,8 @@ typedef struct DumpState {
>>       ArchDumpInfo dump_info;
>>       MemoryMappingList list;
>>       uint16_t phdr_num;
>> +    uint32_t shdr_num;
>>       uint32_t sh_info;
>> -    bool have_section;
>>       bool resume;
>>       bool detached;
>>       ssize_t note_size;
>> --
>> 2.32.0
>>
>>
>>
>
diff mbox series

Patch

diff --git a/dump/dump.c b/dump/dump.c
index a84d8b1598..6696d9819a 100644
--- a/dump/dump.c
+++ b/dump/dump.c
@@ -139,12 +139,12 @@  static void write_elf64_header(DumpState *s, Error **errp)
     elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
     elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
     elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
-    if (s->have_section) {
+    if (s->shdr_num) {
         uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
 
         elf_header.e_shoff = cpu_to_dump64(s, shoff);
         elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
-        elf_header.e_shnum = cpu_to_dump16(s, 1);
+        elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
     }
 
     ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
@@ -170,12 +170,12 @@  static void write_elf32_header(DumpState *s, Error **errp)
     elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
     elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
     elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
-    if (s->have_section) {
+    if (s->shdr_num) {
         uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
 
         elf_header.e_shoff = cpu_to_dump32(s, shoff);
         elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
-        elf_header.e_shnum = cpu_to_dump16(s, 1);
+        elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
     }
 
     ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
@@ -482,7 +482,7 @@  static void write_elf_loads(DumpState *s, Error **errp)
     uint32_t max_index;
     Error *local_err = NULL;
 
-    if (s->have_section) {
+    if (s->shdr_num) {
         max_index = s->sh_info;
     } else {
         max_index = s->phdr_num;
@@ -567,7 +567,7 @@  static void dump_begin(DumpState *s, Error **errp)
         }
 
         /* write section to vmcore */
-        if (s->have_section) {
+        if (s->shdr_num) {
             write_elf_section(s, 1, &local_err);
             if (local_err) {
                 error_propagate(errp, local_err);
@@ -597,7 +597,7 @@  static void dump_begin(DumpState *s, Error **errp)
         }
 
         /* write section to vmcore */
-        if (s->have_section) {
+        if (s->shdr_num) {
             write_elf_section(s, 0, &local_err);
             if (local_err) {
                 error_propagate(errp, local_err);
@@ -1818,11 +1818,12 @@  static void dump_init(DumpState *s, int fd, bool has_format,
      */
     s->phdr_num = 1; /* PT_NOTE */
     if (s->list.num < UINT16_MAX - 2) {
+        s->shdr_num = 0;
         s->phdr_num += s->list.num;
-        s->have_section = false;
     } else {
-        s->have_section = true;
+        /* sh_info of section 0 holds the real number of phdrs */
         s->phdr_num = PN_XNUM;
+        s->shdr_num = 1;
         s->sh_info = 1; /* PT_NOTE */
 
         /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
@@ -1834,23 +1835,15 @@  static void dump_init(DumpState *s, int fd, bool has_format,
     }
 
     if (s->dump_info.d_class == ELFCLASS64) {
-        if (s->have_section) {
-            s->memory_offset = sizeof(Elf64_Ehdr) +
-                               sizeof(Elf64_Phdr) * s->sh_info +
-                               sizeof(Elf64_Shdr) + s->note_size;
-        } else {
-            s->memory_offset = sizeof(Elf64_Ehdr) +
-                               sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
-        }
+        s->memory_offset = sizeof(Elf64_Ehdr) +
+                           sizeof(Elf64_Phdr) * s->sh_info +
+                           sizeof(Elf64_Shdr) * s->shdr_num +
+                           s->note_size;
     } else {
-        if (s->have_section) {
-            s->memory_offset = sizeof(Elf32_Ehdr) +
-                               sizeof(Elf32_Phdr) * s->sh_info +
-                               sizeof(Elf32_Shdr) + s->note_size;
-        } else {
-            s->memory_offset = sizeof(Elf32_Ehdr) +
-                               sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
-        }
+        s->memory_offset = sizeof(Elf32_Ehdr) +
+                           sizeof(Elf32_Phdr) * s->sh_info +
+                           sizeof(Elf32_Shdr) * s->shdr_num +
+                           s->note_size;
     }
 
     return;
diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
index 250143cb5a..854341da0d 100644
--- a/include/sysemu/dump.h
+++ b/include/sysemu/dump.h
@@ -155,8 +155,8 @@  typedef struct DumpState {
     ArchDumpInfo dump_info;
     MemoryMappingList list;
     uint16_t phdr_num;
+    uint32_t shdr_num;
     uint32_t sh_info;
-    bool have_section;
     bool resume;
     bool detached;
     ssize_t note_size;