Free symbol buffers if they are no longer in use
authorH.J. Lu <hjl.tools@gmail.com>
Mon, 17 Sep 2018 15:50:42 +0000 (08:50 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Mon, 17 Sep 2018 15:53:29 +0000 (08:53 -0700)
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.
(copy_main): Update add_specific_symbols to update pointers to
allocated buffer.  Free pointers to allocated buffer before
return.

binutils/ChangeLog
binutils/objcopy.c

index 5207db81931775e569ac7c286fe8a657d1343472..8c7a9e6fac983afe60e7c2f65d5269f33c5d79a8 100644 (file)
@@ -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
index 2e40b42da318e5bcfbbb04567d2816b43068016a..9af3c1eb1b3cd377417a9c560385d5ce5966ec86 100644 (file)
@@ -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;
 }