[patch ld]: Close BFDs before linker-plugin's atexit routine is called

Kai Tietz ktietz70@googlemail.com
Mon Feb 14 09:22:00 GMT 2011


2011/2/14 Kai Tietz <ktietz70@googlemail.com>:
> 2011/2/14 Alan Modra <amodra@gmail.com>:
>> On Wed, Feb 09, 2011 at 07:51:58PM +0100, Kai Tietz wrote:
>>> Hello,
>>>
>>> This is patch addresses the unlink call when lto linker-plugin is
>>> used.  As windows
>>> native doesn't support to unlink still opened files, it fails to do so
>>> as file-descriptors
>>> of bfds aren't closed before atexit routine of plugin gets called.
>>>
>>> 2011-02-09  Kai Tietz
>>>
>>>       * ldmain.c (remove_output): Set output_bfd
>>>       of link_info to nil and close all cached bfds.
>>>       (main): Close output_bfd of link_info and set
>>>       it to nil. Additionally close all cached bfds.
>
> Hi Alan,
>
>> The fact that you need to patch three places to fix one problem
>> says to me that this isn't the best fix..
> Well, here I am not that sure about. I thought about fixing it just
> within the atexit-handler of plugin, but well, next one trying to
> opearate on files in a similar way, will have the same issues again. I
> think it is simply bad style to exit a program without even try to
> cleanup  opened file descriptors and used memory. This could even help
> to use tools like valgrind on ld ...
>
>>  Does a single
>> bfd_cache_close_all at the start of plugin_call_cleanup fix your
>> problem?
> Yes, it does.

I reworked patch a bit. It takes care that in ldmain's local
atexit-handler BFDs are closed, and that for case of plugin the bfds
getting closed before it tries to unlink
files.

Changelog

2011-02-14  Kai Tietz

        * ldmain.c (ld_close_bfds): New.
        (remove_output): Call ld_close_bfds.
        * plugin.c (plugin_call_cleanup): Likewise.
        * ldmain.h (ld_close_bfds): Add prototype.

Tested for x86_64-pc-linux-gnu, and x86_64-w64-mingw32. Ok for apply?

Regards,
Kai
-------------- next part --------------
Index: src/ld/ldmain.c
===================================================================
--- src.orig/ld/ldmain.c	2011-02-10 09:47:59.000000000 +0100
+++ src/ld/ldmain.c	2011-02-14 10:13:39.624526400 +0100
@@ -173,13 +173,22 @@ static struct bfd_link_callbacks link_ca
 
 struct bfd_link_info link_info;
 

+void
+ld_close_bfds (void)
+{
+  if (link_info.output_bfd)
+    bfd_cache_close (link_info.output_bfd);
+  link_info.output_bfd = NULL;
+  bfd_cache_close_all ();
+}
+
 static void
 remove_output (void)
 {
+  ld_close_bfds ();
+
   if (output_filename)
     {
-      if (link_info.output_bfd)
-	bfd_cache_close (link_info.output_bfd);
       if (delete_output_file_on_failure)
 	unlink_if_ordinary (output_filename);
     }
Index: src/ld/ldmain.h
===================================================================
--- src.orig/ld/ldmain.h	2009-12-14 10:33:59.000000000 +0100
+++ src/ld/ldmain.h	2011-02-14 10:14:04.499526400 +0100
@@ -45,5 +45,6 @@ extern int overflow_cutoff_limit;
 extern void add_ysym (const char *);
 extern void add_wrap (const char *);
 extern void add_keepsyms_file (const char *);
+extern void ld_close_bfds (void);
 
 #endif
Index: src/ld/plugin.c
===================================================================
--- src.orig/ld/plugin.c	2011-02-10 09:47:59.000000000 +0100
+++ src/ld/plugin.c	2011-02-14 10:15:38.608901400 +0100
@@ -824,6 +824,10 @@ static void
 plugin_call_cleanup (void)
 {
   plugin_t *curplug = plugins_list;
+
+  /* Close all BFDs, so that unlink can operate on all targets.  */
+  ld_close_bfds ();
+
   while (curplug)
     {
       if (curplug->cleanup_handler && !curplug->cleanup_done)


More information about the Binutils mailing list