public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: "Timm Bäder" <tbaeder@redhat.com>
To: elfutils-devel@sourceware.org
Subject: [PATCH 14/14] segment_report_module: Inline consider_phdr() into only caller
Date: Thu, 12 Nov 2020 16:04:12 +0100	[thread overview]
Message-ID: <20201112150412.2137981-15-tbaeder@redhat.com> (raw)
In-Reply-To: <20201112150412.2137981-1-tbaeder@redhat.com>

Get rid of the nested function this way

Signed-off-by: Timm Bäder <tbaeder@redhat.com>
---
 libdwfl/dwfl_segment_report_module.c | 142 +++++++++++++--------------
 1 file changed, 66 insertions(+), 76 deletions(-)

diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c
index 0679453e..f93c60e2 100644
--- a/libdwfl/dwfl_segment_report_module.c
+++ b/libdwfl/dwfl_segment_report_module.c
@@ -475,7 +475,7 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
       /* NOTE if the number of sections is > 0xff00 then e_shnum
 	 is zero and the actual number would come from the section
 	 zero sh_size field. We ignore this here because getting shdrs
-	 is just a nice bonus (see below in consider_phdr PT_LOAD
+	 is just a nice bonus (see below in the type == PT_LOAD case
 	 where we trim the last segment).  */
       shdrs_end = ehdr.e32.e_shoff + ehdr.e32.e_shnum * ehdr.e32.e_shentsize;
       break;
@@ -561,80 +561,6 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
   build_id.len = 0;
   build_id.vaddr =0;
 
-  /* Consider each of the program headers we've read from the image.  */
-  inline void consider_phdr (GElf_Word type,
-			     GElf_Addr vaddr, GElf_Xword memsz,
-			     GElf_Off offset, GElf_Xword filesz,
-			     GElf_Xword align)
-  {
-    switch (type)
-      {
-      case PT_DYNAMIC:
-	dyn_vaddr = vaddr;
-	dyn_filesz = filesz;
-	break;
-
-      case PT_NOTE:
-          /* We calculate from the p_offset of the note segment,
-           because we don't yet know the bias for its p_vaddr.  */
-          consider_notes (dwfl, memory_callback, memory_callback_arg,
-                          start + offset, filesz, align,
-                          buffer, buffer_available, start, segment,
-                          ei_data, &build_id,
-                          &xlatefrom, &xlateto,
-                          ehdr.e32.e_ident[EI_DATA]);
-	break;
-
-      case PT_LOAD:
-	align = dwfl->segment_align > 1 ? dwfl->segment_align : align ?: 1;
-
-	GElf_Addr vaddr_end = (vaddr + memsz + align - 1) & -align;
-	GElf_Addr filesz_vaddr = filesz < memsz ? vaddr + filesz : vaddr_end;
-	GElf_Off filesz_offset = filesz_vaddr - vaddr + offset;
-
-	if (file_trimmed_end < offset + filesz)
-	  {
-	    file_trimmed_end = offset + filesz;
-
-	    /* Trim the last segment so we don't bother with zeros
-	       in the last page that are off the end of the file.
-	       However, if the extra bit in that page includes the
-	       section headers, keep them.  */
-	    if (shdrs_end <= filesz_offset && shdrs_end > file_trimmed_end)
-	      {
-		filesz += shdrs_end - file_trimmed_end;
-		file_trimmed_end = shdrs_end;
-	      }
-	  }
-
-	total_filesz += filesz;
-
-	if (file_end < filesz_offset)
-	  {
-	    file_end = filesz_offset;
-	    if (filesz_vaddr - start == filesz_offset)
-	      contiguous = file_end;
-	  }
-
-	if (!found_bias && (offset & -align) == 0
-	    && likely (filesz_offset >= phoff + phnum * phentsize))
-	  {
-	    bias = start - vaddr;
-	    found_bias = true;
-	  }
-
-	if ((vaddr & -align) < module_start)
-	  {
-	    module_start = vaddr & -align;
-	    module_address_sync = vaddr + memsz;
-	  }
-
-	if (module_end < vaddr_end)
-	  module_end = vaddr_end;
-	break;
-      }
-  }
-
   Elf32_Phdr *p32 = phdrsp;
   Elf64_Phdr *p64 = phdrsp;
   if ((ei_class == ELFCLASS32 &&
@@ -646,6 +572,7 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
     }
   else
     {
+      /* Consider each of the program headers we've read from the image.  */
       for (uint_fast16_t i = 0; i < phnum; ++i)
         {
           bool is32 = (ei_class == ELFCLASS32);
@@ -656,7 +583,70 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
           GElf_Xword filesz = is32 ? p32[i].p_filesz : p64[i].p_filesz;
           GElf_Xword align = is32 ? p32[i].p_align : p64[i].p_align;
 
-          consider_phdr (type, vaddr, memsz, offset, filesz, align);
+          if (type == PT_DYNAMIC)
+            {
+              dyn_vaddr = vaddr;
+              dyn_filesz = filesz;
+            }
+          else if (type == PT_NOTE)
+            {
+              /* We calculate from the p_offset of the note segment,
+               because we don't yet know the bias for its p_vaddr.  */
+              consider_notes (dwfl, memory_callback, memory_callback_arg,
+                              start + offset, filesz, align,
+                              buffer, buffer_available, start, segment,
+                              ei_data, &build_id,
+                              &xlatefrom, &xlateto,
+                              ehdr.e32.e_ident[EI_DATA]);
+            }
+          else if (type == PT_LOAD)
+            {
+              align = dwfl->segment_align > 1 ? dwfl->segment_align : align ?: 1;
+
+              GElf_Addr vaddr_end = (vaddr + memsz + align - 1) & -align;
+              GElf_Addr filesz_vaddr = filesz < memsz ? vaddr + filesz : vaddr_end;
+              GElf_Off filesz_offset = filesz_vaddr - vaddr + offset;
+
+              if (file_trimmed_end < offset + filesz)
+                {
+                  file_trimmed_end = offset + filesz;
+
+                  /* Trim the last segment so we don't bother with zeros
+                     in the last page that are off the end of the file.
+                     However, if the extra bit in that page includes the
+                     section headers, keep them.  */
+                  if (shdrs_end <= filesz_offset && shdrs_end > file_trimmed_end)
+                    {
+                      filesz += shdrs_end - file_trimmed_end;
+                      file_trimmed_end = shdrs_end;
+                    }
+                }
+
+              total_filesz += filesz;
+
+              if (file_end < filesz_offset)
+                {
+                  file_end = filesz_offset;
+                  if (filesz_vaddr - start == filesz_offset)
+                    contiguous = file_end;
+                }
+
+              if (!found_bias && (offset & -align) == 0
+                  && likely (filesz_offset >= phoff + phnum * phentsize))
+                {
+                  bias = start - vaddr;
+                  found_bias = true;
+                }
+
+              if ((vaddr & -align) < module_start)
+                {
+                  module_start = vaddr & -align;
+                  module_address_sync = vaddr + memsz;
+                }
+
+              if (module_end < vaddr_end)
+                module_end = vaddr_end;
+            }
         }
     }
 
-- 
2.26.2


  parent reply	other threads:[~2020-11-12 15:04 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-12 15:03 Removing gnu99 constructs from elfutils Timm Bäder
2020-11-12 15:03 ` [PATCH 01/14] segment_report_module: Get rid of variable-length arrays Timm Bäder
2020-11-18 23:04   ` Mark Wielaard
2020-11-12 15:04 ` [PATCH 02/14] segment_report_module: Pull segment_read into file scope Timm Bäder
2020-11-18 23:18   ` Mark Wielaard
2020-11-12 15:04 ` [PATCH 03/14] segment_report_module: Remove nested finish() function Timm Bäder
2020-11-19 11:18   ` Mark Wielaard
2020-11-12 15:04 ` [PATCH 04/14] segment_report_module: Remove nested release_buffer() function Timm Bäder
2020-11-19 11:24   ` Mark Wielaard
2020-11-12 15:04 ` [PATCH 05/14] segment_report_module: Pull finish_portion() info file scope Timm Bäder
2020-11-19 11:48   ` Mark Wielaard
2020-11-12 15:04 ` [PATCH 06/14] segment_report_module: Pull read_portion() into " Timm Bäder
2020-11-12 15:04 ` [PATCH 07/14] segment_report_module: Use a struct for build id information Timm Bäder
2020-11-12 15:04 ` [PATCH 08/14] segment_report_module: Pull consider_notes() into file scope Timm Bäder
2020-11-12 15:04 ` [PATCH 09/14] segment_report_module: Get rid of nested final_read() function Timm Bäder
2020-11-12 15:04 ` [PATCH 10/14] segment_report_module: Use one loop for p32/p64 arrays Timm Bäder
2020-11-12 15:04 ` [PATCH 11/14] segment_report_module: Inline read_phdr() into only caller Timm Bäder
2020-11-12 15:04 ` [PATCH 12/14] segment_report_module: Unify d32/d64 loops Timm Bäder
2020-11-12 15:04 ` [PATCH 13/14] segment_report_module: Inline consider_dyn() into only caller Timm Bäder
2020-11-12 15:04 ` Timm Bäder [this message]
2020-11-12 16:52   ` [PATCH 14/14] segment_report_module: Inline consider_phdr() " Navin P
2020-11-13  7:41     ` Timm Bäder
2020-11-13  8:00       ` Navin P
2020-11-13 12:38 ` Removing gnu99 constructs from elfutils Mark Wielaard
2020-11-13 14:09   ` Timm Bäder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201112150412.2137981-15-tbaeder@redhat.com \
    --to=tbaeder@redhat.com \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).