]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
free ldfile search paths
authorAlan Modra <amodra@gmail.com>
Sat, 11 Jan 2025 05:35:06 +0000 (16:05 +1030)
committerAlan Modra <amodra@gmail.com>
Thu, 16 Jan 2025 22:05:12 +0000 (08:35 +1030)
* ldfile.c (ldfile_remap_input_free): Make static, call from..
(ldfile_free): ..here.  New function.
(ldfile_library_path_free, ldfile_script_free),
( ldfile_arch_free): New functions.
(ldfile_find_command_file): Free script_dir.  Move
script_search to file scope.
(ldfile_open_command_file_1): Delete FIXME comment.
* ldfile.h (ldfile_remap_input_free): Delete.
(ldfile_free): Declare.
* ldlang.c (lang_finish): Update.

ld/ldfile.c
ld/ldfile.h
ld/ldlang.c

index 5a4c7c2659077b36307c9e8e1e14a81f61dbd089..12551504ae63899ca5427012c6fb0f3fd5e9f8ce 100644 (file)
@@ -63,6 +63,7 @@ typedef struct search_arch
 } search_arch_type;
 
 static search_dirs_type **search_tail_ptr = &search_head;
+static search_dirs_type *script_search;
 static search_arch_type *search_arch_head;
 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
 
@@ -109,7 +110,7 @@ ldfile_add_remap (const char * pattern, const char * renamed)
     }
 }
 
-void
+static void
 ldfile_remap_input_free (void)
 {
   while (input_remaps != NULL)
@@ -329,6 +330,18 @@ ldfile_add_library_path (const char *name, bool cmdline)
     new_dirs->name = xstrdup (name);
 }
 
+static void
+ldfile_library_path_free (search_dirs_type **root)
+{
+  search_dirs_type *ent;
+  while ((ent = *root) != NULL)
+    {
+      *root = ent->next;
+      free ((void *) ent->name);
+      free (ent);
+    }
+}
+
 /* Try to open a BFD for a lang_input_statement.  */
 
 bool
@@ -835,7 +848,6 @@ ldfile_find_command_file (const char *name,
   search_dirs_type *search;
   FILE *result = NULL;
   char *path;
-  static search_dirs_type *script_search;
 
   if (!default_only)
     {
@@ -854,6 +866,7 @@ ldfile_find_command_file (const char *name,
          search_tail_ptr = &script_search;
          ldfile_add_library_path (script_dir, true);
          search_tail_ptr = save_tail_ptr;
+         free (script_dir);
        }
     }
 
@@ -903,9 +916,6 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
        }
     }
 
-  /* FIXME: This memory is never freed, but that should not really matter.
-     It will be released when the linker exits, and it is unlikely to ever
-     be more than a few tens of bytes.  */
   len = strlen (name);
   script = xmalloc (sizeof (*script) + len);
   script->next = processed_scripts;
@@ -930,6 +940,17 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
   saved_script_handle = ldlex_input_stack;
 }
 
+static void
+ldfile_script_free (struct script_name_list **root)
+{
+  struct script_name_list *ent;
+  while ((ent = *root) != NULL)
+    {
+      *root = ent->next;
+      free (ent);
+    }
+}
+
 /* Open command file NAME in the current directory, -L directories,
    the default script location, in that order.  */
 
@@ -974,6 +995,18 @@ ldfile_add_arch (const char *in_name)
 
 }
 
+static void
+ldfile_arch_free (search_arch_type **root)
+{
+  search_arch_type *ent;
+  while ((ent = *root) != NULL)
+    {
+      *root = ent->next;
+      free (ent->name);
+      free (ent);
+    }
+}
+
 /* Set the output architecture.  */
 
 void
@@ -992,3 +1025,17 @@ ldfile_set_output_arch (const char *string, enum bfd_architecture defarch)
   else
     einfo (_("%F%P: cannot represent machine `%s'\n"), string);
 }
+
+/* Tidy up memory.  */
+
+void
+ldfile_free (void)
+{
+  ldfile_remap_input_free ();
+  ldfile_library_path_free (&script_search);
+  search_tail_ptr = &search_head;
+  ldfile_library_path_free (&search_head);
+  search_arch_tail_ptr = &search_arch_head;
+  ldfile_arch_free (&search_arch_head);
+  ldfile_script_free (&processed_scripts);
+}
index 4ee14fc8eeab920a1db4f8b2758e3a749ed62bf4..813dca511dfa84dfca6b9e4cf63a4e6d05491ef9 100644 (file)
@@ -81,7 +81,7 @@ extern void ldfile_add_remap
   (const char *, const char *);
 extern bool ldfile_add_remap_file
   (const char *);
-extern void ldfile_remap_input_free
+extern void ldfile_free
   (void);
 extern const char * ldfile_possibly_remap_input
   (const char *);
index 634a68434165badfbcfbdb40feb88b68958d1ed1..b59a079b529417c71d592f35b619910d284fd621 100644 (file)
@@ -1398,7 +1398,7 @@ void
 lang_finish (void)
 {
   output_section_statement_table_free ();
-  ldfile_remap_input_free ();
+  ldfile_free ();
 }
 
 /*----------------------------------------------------------------------