public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: tbaeder@redhat.com
To: elfutils-devel@sourceware.org
Subject: [PATCH 2/2] elf-from-memory: Refactor to get rid of nested function
Date: Fri,  8 Jan 2021 09:09:56 +0100	[thread overview]
Message-ID: <20210108080956.2201985-3-tbaeder@redhat.com> (raw)
In-Reply-To: <20210108080956.2201985-1-tbaeder@redhat.com>

From: Timm Bäder <tbaeder@redhat.com>

Try to unify the 32/64 bit code paths and get rid of the nested
handle_segment() this way.

Signed-off-by: Timm Bäder <tbaeder@redhat.com>
---
 libdwfl/elf-from-memory.c | 115 +++++++++++++++++---------------------
 1 file changed, 50 insertions(+), 65 deletions(-)

diff --git a/libdwfl/elf-from-memory.c b/libdwfl/elf-from-memory.c
index 933ab864..a0ef0014 100644
--- a/libdwfl/elf-from-memory.c
+++ b/libdwfl/elf-from-memory.c
@@ -292,80 +292,65 @@ elf_from_remote_memory (GElf_Addr ehdr_vma,
       goto no_memory;
     }
 
-  switch (ehdr.e32.e_ident[EI_CLASS])
+  for (uint_fast16_t i = 0; i < phnum; ++i)
     {
-      /* Reads the given segment.  Returns true if reading fails,
-	 false otherwise.  */
-      inline bool handle_segment (GElf_Addr vaddr, GElf_Off offset,
-				  GElf_Xword filesz)
-	{
-	  GElf_Off start = offset & -pagesize;
-	  GElf_Off end = (offset + filesz + pagesize - 1) & -pagesize;
-	  if (end > (GElf_Off) contents_size)
-	    end = contents_size;
-	  nread = (*read_memory) (arg, buffer + start,
-				  (loadbase + vaddr) & -pagesize,
-				  end - start, end - start);
-	  return nread <= 0;
-	}
+      GElf_Word type = class32 ? (*p32)[i].p_type : (*p64)[i].p_type;
 
-    case ELFCLASS32:
-      for (uint_fast16_t i = 0; i < phnum; ++i)
-	if ((*p32)[i].p_type == PT_LOAD)
-	  if (handle_segment ((*p32)[i].p_vaddr, (*p32)[i].p_offset,
-			      (*p32)[i].p_filesz))
-	    goto read_error;
-
-      /* If the segments visible in memory didn't include the section
-	 headers, then clear them from the file header.  */
-      if (contents_size < shdrs_end)
-	{
-	  ehdr.e32.e_shoff = 0;
-	  ehdr.e32.e_shnum = 0;
-	  ehdr.e32.e_shstrndx = 0;
-	}
+      if (type != PT_LOAD)
+        continue;
+
+      GElf_Addr vaddr = class32 ? (*p32)[i].p_vaddr : (*p64)[i].p_vaddr;
+      GElf_Off offset = class32 ? (*p32)[i].p_offset : (*p64)[i].p_offset;
+      GElf_Xword filesz = class32 ? (*p32)[i].p_filesz : (*p64)[i].p_filesz;
+
+      GElf_Off start = offset & -pagesize;
+      GElf_Off end = (offset + filesz + pagesize - 1) & -pagesize;
+      if (end > (GElf_Off) contents_size)
+        end = contents_size;
+      nread = (*read_memory) (arg, buffer + start,
+                              (loadbase + vaddr) & -pagesize,
+                              end - start, end - start);
+      if (nread <= 0)
+        goto read_error;
+    }
+
+  /* If the segments visible in memory didn't include the section
+     headers, then clear them from the file header.  */
+  if (contents_size < shdrs_end)
+    {
+      if (class32)
+        {
+          ehdr.e32.e_shoff = 0;
+          ehdr.e32.e_shnum = 0;
+          ehdr.e32.e_shstrndx = 0;
+        }
+      else
+        {
+          ehdr.e64.e_shoff = 0;
+          ehdr.e64.e_shnum = 0;
+          ehdr.e64.e_shstrndx = 0;
+        }
+    }
 
-      /* This will normally have been in the first PT_LOAD segment.  But it
-	 conceivably could be missing, and we might have just changed it.  */
-      xlatefrom.d_type = xlateto.d_type = ELF_T_EHDR;
+  /* This will normally have been in the first PT_LOAD segment.  But it
+     conceivably could be missing, and we might have just changed it.  */
+  xlatefrom.d_type = xlateto.d_type = ELF_T_EHDR;
+  xlateto.d_buf = buffer;
+  if (class32)
+    {
       xlatefrom.d_size = xlateto.d_size = sizeof ehdr.e32;
       xlatefrom.d_buf = &ehdr.e32;
-      xlateto.d_buf = buffer;
       if (elf32_xlatetof (&xlateto, &xlatefrom,
-			  ehdr.e32.e_ident[EI_DATA]) == NULL)
-	goto libelf_error;
-      break;
-
-    case ELFCLASS64:
-      for (uint_fast16_t i = 0; i < phnum; ++i)
-	if ((*p64)[i].p_type == PT_LOAD)
-	  if (handle_segment ((*p64)[i].p_vaddr, (*p64)[i].p_offset,
-			      (*p64)[i].p_filesz))
-	    goto read_error;
-
-      /* If the segments visible in memory didn't include the section
-	 headers, then clear them from the file header.  */
-      if (contents_size < shdrs_end)
-	{
-	  ehdr.e64.e_shoff = 0;
-	  ehdr.e64.e_shnum = 0;
-	  ehdr.e64.e_shstrndx = 0;
-	}
-
-      /* This will normally have been in the first PT_LOAD segment.  But it
-	 conceivably could be missing, and we might have just changed it.  */
-      xlatefrom.d_type = xlateto.d_type = ELF_T_EHDR;
+                          ehdr.e32.e_ident[EI_DATA]) == NULL)
+        goto libelf_error;
+    }
+  else
+    {
       xlatefrom.d_size = xlateto.d_size = sizeof ehdr.e64;
       xlatefrom.d_buf = &ehdr.e64;
-      xlateto.d_buf = buffer;
       if (elf64_xlatetof (&xlateto, &xlatefrom,
-			  ehdr.e64.e_ident[EI_DATA]) == NULL)
-	goto libelf_error;
-      break;
-
-    default:
-      abort ();
-      break;
+                          ehdr.e64.e_ident[EI_DATA]) == NULL)
+        goto libelf_error;
     }
 
   free (phdrsp);
-- 
2.26.2


  parent reply	other threads:[~2021-01-08  8:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-08  8:09 Remove nested functions from elf-from-memory.c tbaeder
2021-01-08  8:09 ` [PATCH 1/2] elf-from-memory: Restructure code to get rid of nested handle_segment() tbaeder
2021-01-28 21:08   ` Mark Wielaard
2021-01-08  8:09 ` tbaeder [this message]
2021-01-28 22:58   ` [PATCH 2/2] elf-from-memory: Refactor to get rid of nested function Mark Wielaard

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=20210108080956.2201985-3-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).