public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: tbaeder@redhat.com
To: elfutils-devel@sourceware.org
Subject: [PATCH 4/4] strip: Remove no_symtab_updates() function
Date: Fri,  8 Jan 2021 09:04:49 +0100	[thread overview]
Message-ID: <20210108080449.2201310-5-tbaeder@redhat.com> (raw)
In-Reply-To: <20210108080449.2201310-1-tbaeder@redhat.com>

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

The no_symtab_updates() function was being called at the beginning of
all case labels in this switch, so we can just call it once before the
switch. Then it only has one call-site, so inline this short function
there.

Signed-off-by: Timm Bäder <tbaeder@redhat.com>
---
 src/strip.c | 152 ++++++++++++++++++++++++----------------------------
 1 file changed, 69 insertions(+), 83 deletions(-)

diff --git a/src/strip.c b/src/strip.c
index e608dc5e..dd1e27ac 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -2175,98 +2175,91 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
     /* Find all relocation sections which use this symbol table.  */
     for (cnt = 1; cnt <= shdridx; ++cnt)
       {
-	if (shdr_info[cnt].idx == 0 && debug_fname == NULL)
+	struct shdr_info *info = &shdr_info[cnt];
+	if (info->idx == 0 && debug_fname == NULL)
 	  /* Ignore sections which are discarded.  When we are saving a
 	     relocation section in a separate debug file, we must fix up
 	     the symbol table references.  */
 	  continue;
 
-	const Elf32_Word symtabidx = shdr_info[cnt].old_sh_link;
+	const Elf32_Word symtabidx = info->old_sh_link;
 	elf_assert (symtabidx < shnum + 2);
 	const Elf32_Word *const newsymidx = shdr_info[symtabidx].newsymidx;
-	switch (shdr_info[cnt].shdr.sh_type)
-	  {
-	    inline bool no_symtab_updates (void)
-	    {
-	      /* If the symbol table hasn't changed, do not do anything.  */
-	      if (shdr_info[symtabidx].newsymidx == NULL)
-		return true;
-
-	      /* If the symbol table is not discarded, but additionally
-		 duplicated in the separate debug file and this section
-		 is discarded, don't adjust anything.  */
-	      return (shdr_info[cnt].idx == 0
-		      && shdr_info[symtabidx].debug_data != NULL);
-	    }
 
+	/* If the symbol table hasn't changed, do not do anything.  */
+	if (newsymidx == NULL)
+	  continue;
+
+	/* If the symbol table is not discarded, but additionally
+	   duplicated in the separate debug file and this section
+	   is discarded, don't adjust anything.  */
+	if (info->idx == 0 && shdr_info[symtabidx].debug_data != NULL)
+	  continue;
+
+	switch (info->shdr.sh_type)
+	  {
 	  case SHT_REL:
 	  case SHT_RELA:
-	    if (no_symtab_updates ())
-	      break;
-
-	    Elf_Data *d = elf_getdata (shdr_info[cnt].idx == 0
-				       ? elf_getscn (debugelf, cnt)
-				       : elf_getscn (newelf,
-						     shdr_info[cnt].idx),
-				       NULL);
-	    elf_assert (d != NULL && d->d_buf != NULL
-			&& shdr_info[cnt].shdr.sh_entsize != 0);
-	    size_t nrels = (shdr_info[cnt].shdr.sh_size
-			    / shdr_info[cnt].shdr.sh_entsize);
-
-	    size_t symsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
-	    const Elf32_Word symidxn = (shdr_info[symtabidx].data->d_size
-					/ symsize);
-	    if (shdr_info[cnt].shdr.sh_type == SHT_REL)
-	      for (size_t relidx = 0; relidx < nrels; ++relidx)
-		{
-		  GElf_Rel rel_mem;
-		  if (gelf_getrel (d, relidx, &rel_mem) == NULL)
-		    INTERNAL_ERROR (fname);
+	    {
+	      Elf_Data *d = elf_getdata (info->idx == 0
+					 ? elf_getscn (debugelf, cnt)
+					 : elf_getscn (newelf, info->idx),
+					 NULL);
+	      elf_assert (d != NULL && d->d_buf != NULL
+			  && info->shdr.sh_entsize != 0);
+	      size_t nrels = (info->shdr.sh_size / info->shdr.sh_entsize);
+
+	      size_t symsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
+	      const Elf32_Word symidxn = (shdr_info[symtabidx].data->d_size
+					  / symsize);
+	      if (info->shdr.sh_type == SHT_REL)
+		for (size_t relidx = 0; relidx < nrels; ++relidx)
+		  {
+		    GElf_Rel rel_mem;
+		    if (gelf_getrel (d, relidx, &rel_mem) == NULL)
+		      INTERNAL_ERROR (fname);
 
-		  size_t symidx = GELF_R_SYM (rel_mem.r_info);
-		  elf_assert (symidx < symidxn);
-		  if (newsymidx[symidx] != symidx)
-		    {
-		      rel_mem.r_info
-			= GELF_R_INFO (newsymidx[symidx],
-				       GELF_R_TYPE (rel_mem.r_info));
+		    size_t symidx = GELF_R_SYM (rel_mem.r_info);
+		    elf_assert (symidx < symidxn);
+		    if (newsymidx[symidx] != symidx)
+		      {
+			rel_mem.r_info
+			  = GELF_R_INFO (newsymidx[symidx],
+					 GELF_R_TYPE (rel_mem.r_info));
 
-		      if (gelf_update_rel (d, relidx, &rel_mem) == 0)
-			INTERNAL_ERROR (fname);
-		    }
-		}
-	    else
-	      for (size_t relidx = 0; relidx < nrels; ++relidx)
-		{
-		  GElf_Rela rel_mem;
-		  if (gelf_getrela (d, relidx, &rel_mem) == NULL)
-		    INTERNAL_ERROR (fname);
+			if (gelf_update_rel (d, relidx, &rel_mem) == 0)
+			  INTERNAL_ERROR (fname);
+		      }
+		  }
+	      else
+		for (size_t relidx = 0; relidx < nrels; ++relidx)
+		  {
+		    GElf_Rela rel_mem;
+		    if (gelf_getrela (d, relidx, &rel_mem) == NULL)
+		      INTERNAL_ERROR (fname);
 
-		  size_t symidx = GELF_R_SYM (rel_mem.r_info);
-		  elf_assert (symidx < symidxn);
-		  if (newsymidx[symidx] != symidx)
-		    {
-		      rel_mem.r_info
-			= GELF_R_INFO (newsymidx[symidx],
-				       GELF_R_TYPE (rel_mem.r_info));
+		    size_t symidx = GELF_R_SYM (rel_mem.r_info);
+		    elf_assert (symidx < symidxn);
+		    if (newsymidx[symidx] != symidx)
+		      {
+			rel_mem.r_info
+			  = GELF_R_INFO (newsymidx[symidx],
+					 GELF_R_TYPE (rel_mem.r_info));
 
-		      if (gelf_update_rela (d, relidx, &rel_mem) == 0)
-			INTERNAL_ERROR (fname);
-		    }
-		}
+			if (gelf_update_rela (d, relidx, &rel_mem) == 0)
+			  INTERNAL_ERROR (fname);
+		      }
+		  }
+	      }
 	    break;
 
 	  case SHT_HASH:
-	    if (no_symtab_updates ())
-	      break;
-
 	    /* We have to recompute the hash table.  */
 
-	    elf_assert (shdr_info[cnt].idx > 0);
+	    elf_assert (info->idx > 0);
 
 	    /* The hash section in the new file.  */
-	    scn = elf_getscn (newelf, shdr_info[cnt].idx);
+	    scn = elf_getscn (newelf, info->idx);
 
 	    /* The symbol table data.  */
 	    Elf_Data *symd = elf_getdata (elf_getscn (newelf,
@@ -2278,7 +2271,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
 	    Elf_Data *hashd = elf_getdata (scn, NULL);
 	    elf_assert (hashd != NULL && hashd->d_buf != NULL);
 
-	    if (shdr_info[cnt].shdr.sh_entsize == sizeof (Elf32_Word))
+	    if (info->shdr.sh_entsize == sizeof (Elf32_Word))
 	      {
 		/* Sane arches first.  */
 		elf_assert (hashd->d_size >= 2 * sizeof (Elf32_Word));
@@ -2339,8 +2332,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
 	    else
 	      {
 		/* Alpha and S390 64-bit use 64-bit SHT_HASH entries.  */
-		elf_assert (shdr_info[cnt].shdr.sh_entsize
-			    == sizeof (Elf64_Xword));
+		elf_assert (info->shdr.sh_entsize == sizeof (Elf64_Xword));
 
 		Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
 
@@ -2402,13 +2394,10 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
 
 	  case SHT_GNU_versym:
 	    /* If the symbol table changed we have to adjust the entries.  */
-	    if (no_symtab_updates ())
-	      break;
-
-	    elf_assert (shdr_info[cnt].idx > 0);
+	    elf_assert (info->idx > 0);
 
 	    /* The symbol version section in the new file.  */
-	    scn = elf_getscn (newelf, shdr_info[cnt].idx);
+	    scn = elf_getscn (newelf, info->idx);
 
 	    /* The symbol table data.  */
 	    symd = elf_getdata (elf_getscn (newelf, shdr_info[symtabidx].idx),
@@ -2444,12 +2433,9 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
 	    break;
 
 	  case SHT_GROUP:
-	    if (no_symtab_updates ())
-	      break;
-
 	    /* Yes, the symbol table changed.
 	       Update the section header of the section group.  */
-	    scn = elf_getscn (newelf, shdr_info[cnt].idx);
+	    scn = elf_getscn (newelf, info->idx);
 	    GElf_Shdr shdr_mem;
 	    GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
 	    elf_assert (shdr != NULL);
-- 
2.26.2


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

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-08  8:04 Remove nested functions from src/strip.c tbaeder
2021-01-08  8:04 ` [PATCH 1/4] strip: Replace nested check_preserved function with loop tbaeder
2021-01-28 12:17   ` Mark Wielaard
2021-01-08  8:04 ` [PATCH 2/4] strip: Pull relocate() info file scope tbaeder
2021-01-28 12:50   ` Mark Wielaard
2021-01-08  8:04 ` [PATCH 3/4] strip: Pull update_section_size() into " tbaeder
2021-01-28 12:59   ` Mark Wielaard
2021-01-08  8:04 ` tbaeder [this message]
2021-01-28 13:29   ` [PATCH 4/4] strip: Remove no_symtab_updates() 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=20210108080449.2201310-5-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).