[PATCH GOLD] [6/N mingw host] Add support for not keeping the files open on execute

Andrew Pinski Andrew_Pinski@playstation.sony.com
Wed Oct 7 19:21:00 GMT 2009


Hi,
  On mingw, FD_CLOEXEC does not exist but we can use the Win32 APIs to
say we don't want the file to be inherited on execute.

This is the first patch which I could not figure out how to do without
a #ifdef _WIN32.

Built and tested on i686-linux-gnu including with the LTO plugin.

Thanks,
Andrew Pinski

ChangeLog:

* descriptors.cc: Include windows.h on Win32 hosts.
(Descriptors::open): Use SetHandleInformation with HANDLE_FLAG_INHERIT
instead of calling fcntl with FD_CLOEXEC.
-------------- next part --------------
Index: descriptors.cc
===================================================================
RCS file: /cvs/src/src/gold/descriptors.cc,v
retrieving revision 1.7
diff -u -p -r1.7 descriptors.cc
--- descriptors.cc	24 Mar 2009 04:50:32 -0000	1.7
+++ descriptors.cc	7 Oct 2009 19:19:49 -0000
@@ -27,6 +27,10 @@
 #include <fcntl.h>
 #include <unistd.h>
 
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
 #include "parameters.h"
 #include "options.h"
 #include "gold-threads.h"
@@ -129,7 +133,19 @@ Descriptors::open(int descriptor, const 
 	  if (O_CLOEXEC == 0
 	      && parameters->options_valid()
 	      && parameters->options().has_plugins())
-	    fcntl(new_descriptor, F_SETFD, FD_CLOEXEC);
+	    {
+#ifdef _WIN32
+	      // Unset the inherit flag on execute on the file which
+	      // is the same as the FD_CLOEXEC under UNIX.
+	      HANDLE h = (HANDLE) _get_osfhandle(new_descriptor);
+
+	      gold_assert(h != INVALID_HANDLE_VALUE);
+
+	      SetHandleInformation(h, HANDLE_FLAG_INHERIT, 0);
+#else
+	      fcntl(new_descriptor, F_SETFD, FD_CLOEXEC);
+#endif
+	    }
 
 	  {
 	    Hold_optional_lock hl(this->lock_);


More information about the Binutils mailing list