This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: [PATCH] Free symbol buffers only if they are no longer in use
On Mon, Sep 17, 2018 at 8:21 AM, Nick Clifton <nickc@redhat.com> wrote:
> Hi H.J.
>
>> PR binutils/23633
>> * objcopy.c (strip_specific_buffer): New.
>> (strip_unneeded_buffer): Likewise.
>> (keep_specific_buffer): Likewise.
>> (localize_specific_buffer): Likewise.
>> (globalize_specific_buffer): Likewise.
>> (keepglobal_specific_buffer): Likewise.
>> (weaken_specific_buffer): Likewise.
>> (add_specific_symbols): Add an argument to return pointer to
>> allocated buffer. Don't free allocated buffer.
>> (copy_main): Update add_specific_symbols to update pointers to
>> allocated buffer. Free pointers to allocated buffer before
>> return.
>> * testsuite/binutils-all/objcopy.exp: Run pr23633.
>> * testsuite/binutils-all/pr23633.d: New file.
>> * testsuite/binutils-all/pr23633.list: Likewise.
>> * testsuite/binutils-all/pr23633.s: Likewise.
>
> Approved - please apply.
>
The testcase part has been checked into master. Here is the rest of the patch
I am checking in.
Thanks.
--
H.J.
From 73a3709c85fe42b228d9d511f6b226021d6f585a Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 17 Sep 2018 08:50:42 -0700
Subject: [PATCH] Free symbol buffers if they are no longer in use
add_specific_symbols allocates a buffer to hold symbols. It should be
freed only if it is no longer in use.
PR binutils/23633
* objcopy.c (strip_specific_buffer): New.
(strip_unneeded_buffer): Likewise.
(keep_specific_buffer): Likewise.
(localize_specific_buffer): Likewise.
(globalize_specific_buffer): Likewise.
(keepglobal_specific_buffer): Likewise.
(weaken_specific_buffer): Likewise.
(add_specific_symbols): Add an argument to return pointer to
allocated buffer. Don't free allocated buffer.
(copy_main): Update add_specific_symbols to update pointers to
allocated buffer. Free pointers to allocated buffer before
return.
* testsuite/binutils-all/objcopy.exp: Run pr23633.
* testsuite/binutils-all/pr23633.d: New file.
* testsuite/binutils-all/pr23633.list: Likewise.
* testsuite/binutils-all/pr23633.s: Likewise.
---
binutils/ChangeLog | 15 +++++++++++++
binutils/objcopy.c | 53 +++++++++++++++++++++++++++++++++++++++-------
2 files changed, 60 insertions(+), 8 deletions(-)
diff --git a/binutils/ChangeLog b/binutils/ChangeLog
index 5207db8193..8c7a9e6fac 100644
--- a/binutils/ChangeLog
+++ b/binutils/ChangeLog
@@ -1,3 +1,18 @@
+2018-09-17 H.J. Lu <hongjiu.lu@intel.com>
+
+ * objcopy.c (strip_specific_buffer): New.
+ (strip_unneeded_buffer): Likewise.
+ (keep_specific_buffer): Likewise.
+ (localize_specific_buffer): Likewise.
+ (globalize_specific_buffer): Likewise.
+ (keepglobal_specific_buffer): Likewise.
+ (weaken_specific_buffer): Likewise.
+ (add_specific_symbols): Add an argument to return pointer to
+ allocated buffer.
+ (copy_main): Update add_specific_symbols to update pointers to
+ allocated buffer. Free pointers to allocated buffer before
+ return.
+
2018-09-17 Alan Modra <amodra@gmail.com>
* objcopy.c (handle_remove_section_option): Don't require a dot
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index 2e40b42da3..9af3c1eb1b 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -253,6 +253,14 @@ static htab_t redefine_specific_reverse_htab = NULL;
static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list;
static int add_symbols = 0;
+static char *strip_specific_buffer = NULL;
+static char *strip_unneeded_buffer = NULL;
+static char *keep_specific_buffer = NULL;
+static char *localize_specific_buffer = NULL;
+static char *globalize_specific_buffer = NULL;
+static char *keepglobal_specific_buffer = NULL;
+static char *weaken_specific_buffer = NULL;
+
/* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
static bfd_boolean weaken = FALSE;
@@ -1034,7 +1042,7 @@ add_specific_symbol_node (const void *node, htab_t htab)
#define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
static void
-add_specific_symbols (const char *filename, htab_t htab)
+add_specific_symbols (const char *filename, htab_t htab, char **buffer_p)
{
off_t size;
FILE * f;
@@ -1145,6 +1153,7 @@ add_specific_symbols (const char *filename, htab_t htab)
/* Do not free the buffer. Parts of it will have been referenced
in the calls to add_specific_symbol. */
+ *buffer_p = buffer;
}
/* See whether a symbol should be stripped or kept
@@ -5262,15 +5271,18 @@ copy_main (int argc, char *argv[])
break;
case OPTION_STRIP_SYMBOLS:
- add_specific_symbols (optarg, strip_specific_htab);
+ add_specific_symbols (optarg, strip_specific_htab,
+ &strip_specific_buffer);
break;
case OPTION_STRIP_UNNEEDED_SYMBOLS:
- add_specific_symbols (optarg, strip_unneeded_htab);
+ add_specific_symbols (optarg, strip_unneeded_htab,
+ &strip_unneeded_buffer);
break;
case OPTION_KEEP_SYMBOLS:
- add_specific_symbols (optarg, keep_specific_htab);
+ add_specific_symbols (optarg, keep_specific_htab,
+ &keep_specific_buffer);
break;
case OPTION_LOCALIZE_HIDDEN:
@@ -5278,7 +5290,8 @@ copy_main (int argc, char *argv[])
break;
case OPTION_LOCALIZE_SYMBOLS:
- add_specific_symbols (optarg, localize_specific_htab);
+ add_specific_symbols (optarg, localize_specific_htab,
+ &localize_specific_buffer);
break;
case OPTION_LONG_SECTION_NAMES:
@@ -5293,15 +5306,18 @@ copy_main (int argc, char *argv[])
break;
case OPTION_GLOBALIZE_SYMBOLS:
- add_specific_symbols (optarg, globalize_specific_htab);
+ add_specific_symbols (optarg, globalize_specific_htab,
+ &globalize_specific_buffer);
break;
case OPTION_KEEPGLOBAL_SYMBOLS:
- add_specific_symbols (optarg, keepglobal_specific_htab);
+ add_specific_symbols (optarg, keepglobal_specific_htab,
+ &keepglobal_specific_buffer);
break;
case OPTION_WEAKEN_SYMBOLS:
- add_specific_symbols (optarg, weaken_specific_htab);
+ add_specific_symbols (optarg, weaken_specific_htab,
+ &weaken_specific_buffer);
break;
case OPTION_ALT_MACH_CODE:
@@ -5588,6 +5604,27 @@ copy_main (int argc, char *argv[])
}
}
+ if (strip_specific_buffer)
+ free (strip_specific_buffer);
+
+ if (strip_unneeded_buffer)
+ free (strip_unneeded_buffer);
+
+ if (keep_specific_buffer)
+ free (keep_specific_buffer);
+
+ if (localize_specific_buffer)
+ free (globalize_specific_buffer);
+
+ if (globalize_specific_buffer)
+ free (globalize_specific_buffer);
+
+ if (keepglobal_specific_buffer)
+ free (keepglobal_specific_buffer);
+
+ if (weaken_specific_buffer)
+ free (weaken_specific_buffer);
+
return 0;
}
--
2.17.1