public inbox for binutils-cvs@sourceware.org
help / color / mirror / Atom feed
From: Alan Modra <amodra@sourceware.org>
To: bfd-cvs@sourceware.org
Subject: [binutils-gdb] objcopy: bfd_alloc orelocation
Date: Mon,  4 Jul 2022 13:25:05 +0000 (GMT)	[thread overview]
Message-ID: <20220704132505.2D06C385C321@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0772daccb3ebaf513badf4266e1948454b4455c1

commit 0772daccb3ebaf513badf4266e1948454b4455c1
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Jul 4 11:24:22 2022 +0930

    objcopy: bfd_alloc orelocation
    
    This fixes an inconsequential objcopy memory leak.  I'd normally
    ignore reports of leaks like this one, that are merely one block or
    fewer per section processed, since objcopy soon exits and frees all
    memory.  However I thought it worth providing support for allocating
    memory on a bfd objalloc in objcopy and other utils.
    
            PR 29233
            * bucomm.c (bfd_xalloc): New function.
            * bucomm.h (bfd_xalloc): Declare.
            * objcopy.c (copy_relocations_in_section): Use it to allocate
            array of reloc pointers.  Rewrite code stripping relocs to do
            without extra memory allocation.

Diff:
---
 binutils/bucomm.c  | 13 +++++++++++++
 binutils/bucomm.h  |  2 ++
 binutils/objcopy.c | 37 +++++++++++++------------------------
 3 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/binutils/bucomm.c b/binutils/bucomm.c
index 4395cb9f7f5..58341905587 100644
--- a/binutils/bucomm.c
+++ b/binutils/bucomm.c
@@ -142,6 +142,19 @@ non_fatal (const char *format, ...)
   va_end (args);
 }
 
+/* Like xmalloc except that ABFD's objalloc memory is returned.
+   Use objalloc_free_block to free this memory and all more recently
+   allocated, or more usually, leave it to bfd_close to free.  */
+
+void *
+bfd_xalloc (bfd *abfd, size_t size)
+{
+  void *ret = bfd_alloc (abfd, size);
+  if (ret == NULL)
+    bfd_fatal (NULL);
+  return ret;
+}
+
 /* Set the default BFD target based on the configured target.  Doing
    this permits the binutils to be configured for a particular target,
    and linked against a shared BFD library which was configured for a
diff --git a/binutils/bucomm.h b/binutils/bucomm.h
index 48b2c50aced..a1814cbbcc2 100644
--- a/binutils/bucomm.h
+++ b/binutils/bucomm.h
@@ -39,6 +39,8 @@ void fatal (const char *, ...) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
 
 void non_fatal (const char *, ...) ATTRIBUTE_PRINTF_1;
 
+void *bfd_xalloc (bfd *, size_t);
+
 void set_default_bfd_target (void);
 
 void list_matching_formats (char **);
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index df87712df98..37435733d76 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -4336,14 +4336,13 @@ copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
 	}
       else
 	{
-	  relpp = (arelent **) xmalloc (relsize);
+	  relpp = bfd_xalloc (obfd, relsize);
 	  relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
 	  if (relcount < 0)
 	    {
 	      status = 1;
 	      bfd_nonfatal_message (NULL, ibfd, isection,
 				    _("relocation count is negative"));
-	      free (relpp);
 	      return;
 	    }
 	}
@@ -4352,34 +4351,24 @@ copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
 	{
 	  /* Remove relocations which are not in
 	     keep_strip_specific_list.  */
-	  arelent **temp_relpp;
-	  long temp_relcount = 0;
+	  arelent **w_relpp;
 	  long i;
 
-	  temp_relpp = (arelent **) xmalloc (relsize);
-	  for (i = 0; i < relcount; i++)
-	    {
-	      /* PR 17512: file: 9e907e0c.  */
-	      if (relpp[i]->sym_ptr_ptr
-		  /* PR 20096 */
-		  && * relpp[i]->sym_ptr_ptr)
-		if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
-					 keep_specific_htab))
-		  temp_relpp [temp_relcount++] = relpp [i];
-	    }
-	  relcount = temp_relcount;
-	  if (relpp != isection->orelocation)
-	    free (relpp);
-	  relpp = temp_relpp;
+	  for (w_relpp = relpp, i = 0; i < relcount; i++)
+	    /* PR 17512: file: 9e907e0c.  */
+	    if (relpp[i]->sym_ptr_ptr
+		/* PR 20096 */
+		&& *relpp[i]->sym_ptr_ptr
+		&& is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
+					keep_specific_htab))
+	      *w_relpp++ = relpp[i];
+	  relcount = w_relpp - relpp;
+	  *w_relpp = 0;
 	}
 
       bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
       if (relcount == 0)
-	{
-	  osection->flags &= ~SEC_RELOC;
-	  if (relpp != isection->orelocation)
-	    free (relpp);
-	}
+	osection->flags &= ~SEC_RELOC;
     }
 }


                 reply	other threads:[~2022-07-04 13:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220704132505.2D06C385C321@sourceware.org \
    --to=amodra@sourceware.org \
    --cc=bfd-cvs@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).