public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: tbaeder@redhat.com
To: elfutils-devel@sourceware.org
Subject: [PATCH 3/3] segment_report_module: Inline consider_notes() into only caller
Date: Thu, 26 Nov 2020 15:10:48 +0100	[thread overview]
Message-ID: <20201126141048.506050-4-tbaeder@redhat.com> (raw)
In-Reply-To: <20201126141048.506050-1-tbaeder@redhat.com>

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

Get rid of a nested function this way.
---
 libdwfl/dwfl_segment_report_module.c | 162 +++++++++++++--------------
 1 file changed, 80 insertions(+), 82 deletions(-)

diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c
index a082886a..c48d9ab7 100644
--- a/libdwfl/dwfl_segment_report_module.c
+++ b/libdwfl/dwfl_segment_report_module.c
@@ -463,87 +463,6 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
   build_id.len = 0;
   build_id.vaddr =0;
 
-  /* Consider a PT_NOTE we've found in the image.  */
-  inline void consider_notes (GElf_Addr vaddr, GElf_Xword filesz,
-			      GElf_Xword align)
-  {
-    /* If we have already seen a build ID, we don't care any more.  */
-    if (build_id.memory != NULL || filesz == 0)
-      return;
-
-    void *data;
-    size_t data_size;
-    if (read_portion (&read_state, &data, &data_size, start, segment, vaddr, filesz))
-      return;
-
-    /* data_size will be zero if we got everything from the initial
-       buffer, otherwise it will be the size of the new buffer that
-       could be read.  */
-    if (data_size != 0)
-      filesz = data_size;
-
-    assert (sizeof (Elf32_Nhdr) == sizeof (Elf64_Nhdr));
-
-    void *notes;
-    if (ei_data == MY_ELFDATA)
-      notes = data;
-    else
-      {
-	notes = malloc (filesz);
-	if (unlikely (notes == NULL))
-	  return;
-	xlatefrom.d_type = xlateto.d_type = (align == 8
-					     ? ELF_T_NHDR8 : ELF_T_NHDR);
-	xlatefrom.d_buf = (void *) data;
-	xlatefrom.d_size = filesz;
-	xlateto.d_buf = notes;
-	xlateto.d_size = filesz;
-	if (elf32_xlatetom (&xlateto, &xlatefrom,
-			    ehdr.e32.e_ident[EI_DATA]) == NULL)
-	  goto done;
-      }
-
-    const GElf_Nhdr *nh = notes;
-    size_t len = 0;
-    while (filesz > len + sizeof (*nh))
-      {
-	const void *note_name;
-	const void *note_desc;
-
-	len += sizeof (*nh);
-	note_name = notes + len;
-
-	len += nh->n_namesz;
-	len = align == 8 ? NOTE_ALIGN8 (len) : NOTE_ALIGN4 (len);
-	note_desc = notes + len;
-
-	if (unlikely (filesz < len + nh->n_descsz))
-	  break;
-
-        if (nh->n_type == NT_GNU_BUILD_ID
-	    && nh->n_descsz > 0
-	    && nh->n_namesz == sizeof "GNU"
-	    && !memcmp (note_name, "GNU", sizeof "GNU"))
-	  {
-	    build_id.vaddr = note_desc - (const void *) notes + vaddr;
-	    build_id.len = nh->n_descsz;
-	    build_id.memory = malloc (build_id.len);
-	    if (likely (build_id.memory != NULL))
-	      memcpy (build_id.memory, note_desc, build_id.len);
-	    break;
-	  }
-
-	len += nh->n_descsz;
-	len = align == 8 ? NOTE_ALIGN8 (len) : NOTE_ALIGN4 (len);
-	nh = (void *) notes + len;
-      }
-
-  done:
-    if (notes != data)
-      free (notes);
-    finish_portion (&read_state, &data, &data_size);
-  }
-
   Elf32_Phdr *p32 = phdrsp;
   Elf64_Phdr *p64 = phdrsp;
   if ((ei_class == ELFCLASS32
@@ -573,9 +492,88 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
             }
           else if (type == PT_NOTE)
             {
+              /* If we have already seen a build ID, we don't care any more.  */
+              if (build_id.memory != NULL || filesz == 0)
+                continue; /* Next header */
+
               /* We calculate from the p_offset of the note segment,
                because we don't yet know the bias for its p_vaddr.  */
-              consider_notes ( start + offset, filesz, align);
+              const size_t note_vaddr = start + offset;
+              void *data;
+              size_t data_size;
+              if (read_portion (&read_state, &data, &data_size, start, segment, note_vaddr, filesz))
+                continue; /* Next header */
+
+              /* data_size will be zero if we got everything from the initial
+                 buffer, otherwise it will be the size of the new buffer that
+                 could be read.  */
+              if (data_size != 0)
+                filesz = data_size;
+
+              assert (sizeof (Elf32_Nhdr) == sizeof (Elf64_Nhdr));
+
+              void *notes;
+              if (ei_data == MY_ELFDATA)
+                notes = data;
+              else
+                {
+                  const unsigned int xencoding = ehdr.e32.e_ident[EI_DATA];
+
+                  notes = malloc (filesz);
+                  if (unlikely (notes == NULL))
+                    continue; /* Next header */
+                  xlatefrom.d_type = xlateto.d_type = (align == 8
+                                                       ? ELF_T_NHDR8 : ELF_T_NHDR);
+                  xlatefrom.d_buf = (void *) data;
+                  xlatefrom.d_size = filesz;
+                  xlateto.d_buf = notes;
+                  xlateto.d_size = filesz;
+                  if (elf32_xlatetom (&xlateto, &xlatefrom, xencoding) == NULL)
+                    {
+                      free (notes);
+                      finish_portion (&read_state, &data, &data_size);
+                      continue;
+                    }
+                }
+
+              const GElf_Nhdr *nh = notes;
+              size_t len = 0;
+              while (filesz > len + sizeof (*nh))
+                {
+                  const void *note_name;
+                  const void *note_desc;
+
+                  len += sizeof (*nh);
+                  note_name = notes + len;
+
+                  len += nh->n_namesz;
+                  len = align == 8 ? NOTE_ALIGN8 (len) : NOTE_ALIGN4 (len);
+                  note_desc = notes + len;
+
+                  if (unlikely (filesz < len + nh->n_descsz))
+                    break;
+
+                  if (nh->n_type == NT_GNU_BUILD_ID
+                      && nh->n_descsz > 0
+                      && nh->n_namesz == sizeof "GNU"
+                      && !memcmp (note_name, "GNU", sizeof "GNU"))
+                    {
+                      build_id.vaddr = note_desc - (const void *) notes + note_vaddr;
+                      build_id.len = nh->n_descsz;
+                      build_id.memory = malloc (build_id.len);
+                      if (likely (build_id.memory != NULL))
+                        memcpy (build_id.memory, note_desc, build_id.len);
+                      break;
+                    }
+
+                  len += nh->n_descsz;
+                  len = align == 8 ? NOTE_ALIGN8 (len) : NOTE_ALIGN4 (len);
+                  nh = (void *) notes + len;
+                }
+
+              if (notes != data)
+                free (notes);
+              finish_portion (&read_state, &data, &data_size);
             }
           else if (type == PT_LOAD)
             {
-- 
2.26.2


  parent reply	other threads:[~2020-11-26 14:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-26 14:10 Remove remaining nested functions from libdwfl tbaeder
2020-11-26 14:10 ` [PATCH 1/3] segment_report_module: Pull finish_portion() into file scope tbaeder
2020-11-26 14:10 ` [PATCH 2/3] segment_report_module: Pull read_portion() " tbaeder
2020-11-26 14:10 ` tbaeder [this message]
2020-11-28  1:02 ` Remove remaining nested functions from libdwfl 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=20201126141048.506050-4-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).