free ldfile search paths

Alan Modra amodra@gmail.com
Thu Jan 16 22:02:08 GMT 2025


	* 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.

diff --git a/ld/ldfile.c b/ld/ldfile.c
index 5a4c7c26590..12551504ae6 100644
--- a/ld/ldfile.c
+++ b/ld/ldfile.c
@@ -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);
+}
diff --git a/ld/ldfile.h b/ld/ldfile.h
index 4ee14fc8eea..813dca511df 100644
--- a/ld/ldfile.h
+++ b/ld/ldfile.h
@@ -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 *);
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 634a6843416..b59a079b529 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -1398,7 +1398,7 @@ void
 lang_finish (void)
 {
   output_section_statement_table_free ();
-  ldfile_remap_input_free ();
+  ldfile_free ();
 }
 
 /*----------------------------------------------------------------------

-- 
Alan Modra


More information about the Binutils mailing list