2e9b04
diff -rup binutils.orig/bfd/bfd-in2.h binutils-2.35.1/bfd/bfd-in2.h
2e9b04
--- binutils.orig/bfd/bfd-in2.h	2021-01-29 11:14:51.848568548 +0000
2e9b04
+++ binutils-2.35.1/bfd/bfd-in2.h	2021-01-29 11:15:33.431322133 +0000
2e9b04
@@ -583,6 +583,8 @@ bfd *bfd_openr (const char *filename, co
2e9b04
 
2e9b04
 bfd *bfd_fdopenr (const char *filename, const char *target, int fd);
2e9b04
 
2e9b04
+bfd *bfd_fdopenw (const char *filename, const char *target, int fd);
2e9b04
+
2e9b04
 bfd *bfd_openstreamr (const char * filename, const char * target,
2e9b04
     void * stream);
2e9b04
 
2e9b04
diff -rup binutils.orig/bfd/opncls.c binutils-2.35.1/bfd/opncls.c
2e9b04
--- binutils.orig/bfd/opncls.c	2021-01-29 11:14:51.846568560 +0000
2e9b04
+++ binutils-2.35.1/bfd/opncls.c	2021-01-29 11:15:33.431322133 +0000
2e9b04
@@ -395,6 +395,39 @@ bfd_fdopenr (const char *filename, const
2e9b04
 
2e9b04
 /*
2e9b04
 FUNCTION
2e9b04
+	bfd_fdopenw
2e9b04
+
2e9b04
+SYNOPSIS
2e9b04
+	bfd *bfd_fdopenw (const char *filename, const char *target, int fd);
2e9b04
+
2e9b04
+DESCRIPTION
2e9b04
+	<<bfd_fdopenw>> is exactly like <<bfd_fdopenr>> with the exception that
2e9b04
+	the resulting BFD is suitable for output.
2e9b04
+*/
2e9b04
+
2e9b04
+bfd *
2e9b04
+bfd_fdopenw (const char *filename, const char *target, int fd)
2e9b04
+{
2e9b04
+  bfd *out = bfd_fdopenr (filename, target, fd);
2e9b04
+
2e9b04
+  if (out != NULL)
2e9b04
+    {
2e9b04
+      if (!bfd_write_p (out))
2e9b04
+	{
2e9b04
+	  close (fd);
2e9b04
+	  _bfd_delete_bfd (out);
2e9b04
+	  out = NULL;
2e9b04
+	  bfd_set_error (bfd_error_invalid_operation);
2e9b04
+	}
2e9b04
+      else
2e9b04
+	out->direction = write_direction;
2e9b04
+    }
2e9b04
+
2e9b04
+  return out;
2e9b04
+}
2e9b04
+
2e9b04
+/*
2e9b04
+FUNCTION
2e9b04
 	bfd_openstreamr
2e9b04
 
2e9b04
 SYNOPSIS
2e9b04
diff -rup binutils.orig/binutils/ar.c binutils-2.35.1/binutils/ar.c
2e9b04
--- binutils.orig/binutils/ar.c	2021-01-29 11:14:51.344571539 +0000
2e9b04
+++ binutils-2.35.1/binutils/ar.c	2021-01-29 11:15:56.174187367 +0000
2e9b04
@@ -25,6 +25,7 @@
2e9b04
 
2e9b04
 #include "sysdep.h"
2e9b04
 #include "bfd.h"
2e9b04
+#include "libbfd.h"
2e9b04
 #include "libiberty.h"
2e9b04
 #include "progress.h"
2e9b04
 #include "getopt.h"
2e9b04
@@ -1195,20 +1196,26 @@ write_archive (bfd *iarch)
2e9b04
   bfd *obfd;
2e9b04
   char *old_name, *new_name;
2e9b04
   bfd *contents_head = iarch->archive_next;
2e9b04
+  int ofd = -1;
2e9b04
+  struct stat target_stat;
2e9b04
+  bfd_boolean skip_stat = FALSE;
2e9b04
 
2e9b04
   old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
2e9b04
   strcpy (old_name, bfd_get_filename (iarch));
2e9b04
-  new_name = make_tempname (old_name);
2e9b04
+  new_name = make_tempname (old_name, &ofd;;
2e9b04
 
2e9b04
   if (new_name == NULL)
2e9b04
     bfd_fatal (_("could not create temporary file whilst writing archive"));
2e9b04
 
2e9b04
   output_filename = new_name;
2e9b04
 
2e9b04
-  obfd = bfd_openw (new_name, bfd_get_target (iarch));
2e9b04
+  obfd = bfd_fdopenw (new_name, bfd_get_target (iarch), ofd);
2e9b04
 
2e9b04
   if (obfd == NULL)
2e9b04
-    bfd_fatal (old_name);
2e9b04
+    {
2e9b04
+      close (ofd);
2e9b04
+      bfd_fatal (old_name);
2e9b04
+    }
2e9b04
 
2e9b04
   output_bfd = obfd;
2e9b04
 
2e9b04
@@ -1237,6 +1244,14 @@ write_archive (bfd *iarch)
2e9b04
   if (!bfd_set_archive_head (obfd, contents_head))
2e9b04
     bfd_fatal (old_name);
2e9b04
 
2e9b04
+#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
+  ofd = dup (ofd);
2e9b04
+  if (iarch == NULL || iarch->iostream == NULL)
2e9b04
+    skip_stat = TRUE;
2e9b04
+  else if (ofd == -1 || fstat (fileno (iarch->iostream), &target_stat) != 0)
2e9b04
+    bfd_fatal (old_name);
2e9b04
+#endif
2e9b04
+
2e9b04
   if (!bfd_close (obfd))
2e9b04
     bfd_fatal (old_name);
2e9b04
 
2e9b04
@@ -1246,7 +1261,7 @@ write_archive (bfd *iarch)
2e9b04
   /* We don't care if this fails; we might be creating the archive.  */
2e9b04
   bfd_close (iarch);
2e9b04
 
2e9b04
-  if (smart_rename (new_name, old_name, 0) != 0)
2e9b04
+  if (smart_rename (new_name, old_name, ofd, skip_stat ? NULL : &target_stat, 0) != 0)
2e9b04
     xexit (1);
2e9b04
   free (old_name);
2e9b04
   free (new_name);
2e9b04
diff -rup binutils.orig/binutils/arsup.c binutils-2.35.1/binutils/arsup.c
2e9b04
--- binutils.orig/binutils/arsup.c	2021-01-29 11:14:51.350571503 +0000
2e9b04
+++ binutils-2.35.1/binutils/arsup.c	2021-01-29 11:15:56.174187367 +0000
2e9b04
@@ -345,13 +345,25 @@ ar_save (void)
2e9b04
   else
2e9b04
     {
2e9b04
       char *ofilename = xstrdup (bfd_get_filename (obfd));
2e9b04
+      bfd_boolean skip_stat = FALSE;
2e9b04
+      struct stat target_stat;
2e9b04
+      int ofd = -1;
2e9b04
 
2e9b04
       if (deterministic > 0)
2e9b04
         obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
2e9b04
 
2e9b04
+#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
+      /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
2e9b04
+         copy fallback to write the output.  */
2e9b04
+      ofd = dup (fileno (obfd->iostream));
2e9b04
+      if (lstat (real_name, &target_stat) != 0)
2e9b04
+	skip_stat = TRUE;
2e9b04
+#endif
2e9b04
+
2e9b04
       bfd_close (obfd);
2e9b04
 
2e9b04
-      smart_rename (ofilename, real_name, 0);
2e9b04
+      smart_rename (ofilename, real_name, ofd,
2e9b04
+		    skip_stat ? NULL : &target_stat, 0);
2e9b04
       obfd = 0;
2e9b04
       free (ofilename);
2e9b04
     }
2e9b04
diff -rup binutils.orig/binutils/bucomm.c binutils-2.35.1/binutils/bucomm.c
2e9b04
--- binutils.orig/binutils/bucomm.c	2021-01-29 11:14:51.422571073 +0000
2e9b04
+++ binutils-2.35.1/binutils/bucomm.c	2021-01-29 11:15:33.431322133 +0000
2e9b04
@@ -532,7 +532,7 @@ template_in_dir (const char *path)
2e9b04
    as FILENAME.  */
2e9b04
 
2e9b04
 char *
2e9b04
-make_tempname (const char *filename)
2e9b04
+make_tempname (const char *filename, int *ofd)
2e9b04
 {
2e9b04
   char *tmpname = template_in_dir (filename);
2e9b04
   int fd;
2e9b04
@@ -550,7 +550,7 @@ make_tempname (const char *filename)
2e9b04
       free (tmpname);
2e9b04
       return NULL;
2e9b04
     }
2e9b04
-  close (fd);
2e9b04
+  *ofd = fd;
2e9b04
   return tmpname;
2e9b04
 }
2e9b04
 
2e9b04
diff -rup binutils.orig/binutils/bucomm.h binutils-2.35.1/binutils/bucomm.h
2e9b04
--- binutils.orig/binutils/bucomm.h	2021-01-29 11:14:51.350571503 +0000
2e9b04
+++ binutils-2.35.1/binutils/bucomm.h	2021-01-29 11:15:56.174187367 +0000
2e9b04
@@ -51,7 +51,7 @@ int display_info (void);
2e9b04
 
2e9b04
 void print_arelt_descr (FILE *, bfd *, bfd_boolean, bfd_boolean);
2e9b04
 
2e9b04
-char *make_tempname (const char *);
2e9b04
+char *make_tempname (const char *, int *);
2e9b04
 char *make_tempdir (const char *);
2e9b04
 
2e9b04
 bfd_vma parse_vma (const char *, const char *);
2e9b04
@@ -71,7 +71,8 @@ extern void print_version (const char *)
2e9b04
 /* In rename.c.  */
2e9b04
 extern void set_times (const char *, const struct stat *);
2e9b04
 
2e9b04
-extern int smart_rename (const char *, const char *, int);
2e9b04
+extern int smart_rename (const char *, const char *, int, struct stat *, int);
2e9b04
+
2e9b04
 
2e9b04
 /* In libiberty.  */
2e9b04
 void *xmalloc (size_t);
2e9b04
diff -rup binutils.orig/binutils/objcopy.c binutils-2.35.1/binutils/objcopy.c
2e9b04
--- binutils.orig/binutils/objcopy.c	2021-01-29 11:14:51.342571551 +0000
2e9b04
+++ binutils-2.35.1/binutils/objcopy.c	2021-01-29 11:15:56.175187361 +0000
2e9b04
@@ -20,6 +20,7 @@
2e9b04
 
2e9b04
 #include "sysdep.h"
2e9b04
 #include "bfd.h"
2e9b04
+#include "libbfd.h"
2e9b04
 #include "progress.h"
2e9b04
 #include "getopt.h"
2e9b04
 #include "libiberty.h"
2e9b04
@@ -3711,9 +3712,9 @@ set_long_section_mode (bfd *output_bfd,
2e9b04
 /* The top-level control.  */
2e9b04
 
2e9b04
 static void
2e9b04
-copy_file (const char *input_filename, const char *output_filename,
2e9b04
-	   const char *input_target,   const char *output_target,
2e9b04
-	   const bfd_arch_info_type *input_arch)
2e9b04
+copy_file (const char *input_filename, const char *output_filename, int ofd,
2e9b04
+	   struct stat *in_stat, const char *input_target,
2e9b04
+	   const char *output_target, const bfd_arch_info_type *input_arch)
2e9b04
 {
2e9b04
   bfd *ibfd;
2e9b04
   char **obj_matching;
2e9b04
@@ -3732,7 +3733,7 @@ copy_file (const char *input_filename, c
2e9b04
   /* To allow us to do "strip *" without dying on the first
2e9b04
      non-object file, failures are nonfatal.  */
2e9b04
   ibfd = bfd_openr (input_filename, input_target);
2e9b04
-  if (ibfd == NULL)
2e9b04
+  if (ibfd == NULL || fstat (fileno (ibfd->iostream), in_stat) != 0)
2e9b04
     {
2e9b04
       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2e9b04
       status = 1;
2e9b04
@@ -3786,9 +3787,14 @@ copy_file (const char *input_filename, c
2e9b04
       else
2e9b04
 	force_output_target = TRUE;
2e9b04
 
2e9b04
-      obfd = bfd_openw (output_filename, output_target);
2e9b04
+      if (ofd >= 0)
2e9b04
+	obfd = bfd_fdopenw (output_filename, output_target, ofd);
2e9b04
+      else
2e9b04
+	obfd = bfd_openw (output_filename, output_target);
2e9b04
+
2e9b04
       if (obfd == NULL)
2e9b04
 	{
2e9b04
+	  close (ofd);
2e9b04
 	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2e9b04
 	  status = 1;
2e9b04
 	  return;
2e9b04
@@ -3816,13 +3822,19 @@ copy_file (const char *input_filename, c
2e9b04
       if (output_target == NULL)
2e9b04
 	output_target = bfd_get_target (ibfd);
2e9b04
 
2e9b04
-      obfd = bfd_openw (output_filename, output_target);
2e9b04
+      if (ofd >= 0)
2e9b04
+	obfd = bfd_fdopenw (output_filename, output_target, ofd);
2e9b04
+      else
2e9b04
+	obfd = bfd_openw (output_filename, output_target);
2e9b04
+
2e9b04
       if (obfd == NULL)
2e9b04
  	{
2e9b04
+	  close (ofd);
2e9b04
  	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2e9b04
  	  status = 1;
2e9b04
  	  return;
2e9b04
  	}
2e9b04
+
2e9b04
       /* This is a no-op on non-Coff targets.  */
2e9b04
       set_long_section_mode (obfd, ibfd, long_section_names);
2e9b04
 
2e9b04
@@ -4786,6 +4798,8 @@ strip_main (int argc, char *argv[])
2e9b04
       int hold_status = status;
2e9b04
       struct stat statbuf;
2e9b04
       char *tmpname;
2e9b04
+      int tmpfd = -1;
2e9b04
+      int copyfd = -1;
2e9b04
 
2e9b04
       if (get_file_size (argv[i]) < 1)
2e9b04
 	{
2e9b04
@@ -4793,18 +4807,18 @@ strip_main (int argc, char *argv[])
2e9b04
 	  continue;
2e9b04
 	}
2e9b04
 
2e9b04
-      if (preserve_dates)
2e9b04
-	/* No need to check the return value of stat().
2e9b04
-	   It has already been checked in get_file_size().  */
2e9b04
-	stat (argv[i], &statbuf);
2e9b04
-
2e9b04
       if (output_file == NULL
2e9b04
 	  || filename_cmp (argv[i], output_file) == 0)
2e9b04
-	tmpname = make_tempname (argv[i]);
2e9b04
+	tmpname = make_tempname (argv[i], &tmpfd);
2e9b04
       else
2e9b04
 	tmpname = output_file;
2e9b04
 
2e9b04
-      if (tmpname == NULL)
2e9b04
+      if (tmpname == NULL
2e9b04
+#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
+	  /* Retain a copy of TMPFD since we will need it for SMART_RENAME.  */
2e9b04
+	  || (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
2e9b04
+#endif
2e9b04
+      )
2e9b04
 	{
2e9b04
 	  bfd_nonfatal_message (argv[i], NULL, NULL,
2e9b04
 				_("could not create temporary file to hold stripped copy"));
2e9b04
@@ -4813,7 +4827,8 @@ strip_main (int argc, char *argv[])
2e9b04
 	}
2e9b04
 
2e9b04
       status = 0;
2e9b04
-      copy_file (argv[i], tmpname, input_target, output_target, NULL);
2e9b04
+      copy_file (argv[i], tmpname, tmpfd, &statbuf, input_target,
2e9b04
+		 output_target, NULL);
2e9b04
       if (status == 0)
2e9b04
 	{
2e9b04
 	  if (preserve_dates)
2e9b04
@@ -4821,12 +4836,18 @@ strip_main (int argc, char *argv[])
2e9b04
 	  if (output_file != tmpname)
2e9b04
 	    status = (smart_rename (tmpname,
2e9b04
 				    output_file ? output_file : argv[i],
2e9b04
-				    preserve_dates) != 0);
2e9b04
+				    copyfd, &statbuf, preserve_dates) != 0);
2e9b04
 	  if (status == 0)
2e9b04
 	    status = hold_status;
2e9b04
 	}
2e9b04
       else
2e9b04
-	unlink_if_ordinary (tmpname);
2e9b04
+	{
2e9b04
+#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
+	  if (copyfd >= 0)
2e9b04
+	    close (copyfd);
2e9b04
+#endif
2e9b04
+	  unlink_if_ordinary (tmpname);
2e9b04
+	}
2e9b04
       if (output_file != tmpname)
2e9b04
 	free (tmpname);
2e9b04
     }
2e9b04
@@ -5033,7 +5054,8 @@ copy_main (int argc, char *argv[])
2e9b04
   bfd_boolean formats_info = FALSE;
2e9b04
   bfd_boolean use_globalize = FALSE;
2e9b04
   bfd_boolean use_keep_global = FALSE;
2e9b04
-  int c;
2e9b04
+  int c, tmpfd = -1;
2e9b04
+  int copyfd = -1;
2e9b04
   struct stat statbuf;
2e9b04
   const bfd_arch_info_type *input_arch = NULL;
2e9b04
 
2e9b04
@@ -5870,34 +5892,43 @@ copy_main (int argc, char *argv[])
2e9b04
       convert_efi_target (efi);
2e9b04
     }
2e9b04
 
2e9b04
-  if (preserve_dates)
2e9b04
-    if (stat (input_filename, & statbuf) < 0)
2e9b04
-      fatal (_("warning: could not locate '%s'.  System error message: %s"),
2e9b04
-	     input_filename, strerror (errno));
2e9b04
-
2e9b04
   /* If there is no destination file, or the source and destination files
2e9b04
      are the same, then create a temp and rename the result into the input.  */
2e9b04
   if (output_filename == NULL
2e9b04
       || filename_cmp (input_filename, output_filename) == 0)
2e9b04
-    tmpname = make_tempname (input_filename);
2e9b04
+    tmpname = make_tempname (input_filename, &tmpfd);
2e9b04
   else
2e9b04
     tmpname = output_filename;
2e9b04
 
2e9b04
-  if (tmpname == NULL)
2e9b04
-    fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
2e9b04
-	   input_filename, strerror (errno));
2e9b04
+  if (tmpname == NULL
2e9b04
+#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
+      /* Retain a copy of TMPFD since we will need it for SMART_RENAME.  */
2e9b04
+      || (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
2e9b04
+#endif
2e9b04
+  )
2e9b04
+    {
2e9b04
+      fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
2e9b04
+	     input_filename, strerror (errno));
2e9b04
+    }
2e9b04
 
2e9b04
-  copy_file (input_filename, tmpname, input_target, output_target, input_arch);
2e9b04
+  copy_file (input_filename, tmpname, tmpfd, &statbuf, input_target,
2e9b04
+	     output_target, input_arch);
2e9b04
   if (status == 0)
2e9b04
     {
2e9b04
       if (preserve_dates)
2e9b04
 	set_times (tmpname, &statbuf);
2e9b04
       if (tmpname != output_filename)
2e9b04
-	status = (smart_rename (tmpname, input_filename,
2e9b04
+	status = (smart_rename (tmpname, input_filename, copyfd, &statbuf,
2e9b04
 				preserve_dates) != 0);
2e9b04
     }
2e9b04
   else
2e9b04
-    unlink_if_ordinary (tmpname);
2e9b04
+    {
2e9b04
+#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
+      if (copyfd >= 0)
2e9b04
+	close (copyfd);
2e9b04
+#endif
2e9b04
+      unlink_if_ordinary (tmpname);
2e9b04
+    }
2e9b04
 
2e9b04
   if (tmpname != output_filename)
2e9b04
     free (tmpname);
2e9b04
diff -rup binutils.orig/binutils/rename.c binutils-2.35.1/binutils/rename.c
2e9b04
--- binutils.orig/binutils/rename.c	2021-01-29 11:14:51.422571073 +0000
2e9b04
+++ binutils-2.35.1/binutils/rename.c	2021-01-29 11:15:56.175187361 +0000
2e9b04
@@ -131,17 +131,55 @@ set_times (const char *destination, cons
2e9b04
 #endif
2e9b04
 #endif
2e9b04
 
2e9b04
-/* Rename FROM to TO, copying if TO is a link.
2e9b04
-   Return 0 if ok, -1 if error.  */
2e9b04
+#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
+/* Try to preserve the permission bits and ownership of an existing file when
2e9b04
+   rename overwrites it.  FD is the file being renamed and TARGET_STAT has the
2e9b04
+   status of the file that was overwritten.  */
2e9b04
+static void
2e9b04
+try_preserve_permissions (int fd, struct stat *target_stat)
2e9b04
+{
2e9b04
+  struct stat from_stat;
2e9b04
+  int ret = 0;
2e9b04
+
2e9b04
+  if (fstat (fd, &from_stat) != 0)
2e9b04
+    return;
2e9b04
+
2e9b04
+  int from_mode = from_stat.st_mode & 0777;
2e9b04
+  int to_mode = target_stat->st_mode & 0777;
2e9b04
+
2e9b04
+  /* Fix up permissions before we potentially lose ownership with fchown.
2e9b04
+     Clear the setxid bits because in case the fchown below fails then we don't
2e9b04
+     want to end up with a sxid file owned by the invoking user.  If the user
2e9b04
+     hasn't changed or if fchown succeeded, we add back the sxid bits at the
2e9b04
+     end.  */
2e9b04
+  if (from_mode != to_mode)
2e9b04
+    fchmod (fd, to_mode);
2e9b04
+
2e9b04
+  /* Fix up ownership, this will clear the setxid bits.  */
2e9b04
+  if (from_stat.st_uid != target_stat->st_uid
2e9b04
+      || from_stat.st_gid != target_stat->st_gid)
2e9b04
+    ret = fchown (fd, target_stat->st_uid, target_stat->st_gid);
2e9b04
+
2e9b04
+  /* Fix up the sxid bits if either the fchown wasn't needed or it
2e9b04
+     succeeded.  */
2e9b04
+  if (ret == 0)
2e9b04
+    fchmod (fd, target_stat->st_mode & 07777);
2e9b04
+}
2e9b04
+#endif
2e9b04
+
2e9b04
+/* Rename FROM to TO, copying if TO is either a link or is not a regular file.
2e9b04
+   FD is an open file descriptor pointing to FROM that we can use to safely fix
2e9b04
+   up permissions of the file after renaming.  TARGET_STAT has the file status
2e9b04
+   that is used to fix up permissions and timestamps after rename.  Return 0 if
2e9b04
+   ok, -1 if error and FD is closed before returning.  */
2e9b04
 
2e9b04
 int
2e9b04
-smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
2e9b04
+smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
2e9b04
+	      struct stat *target_stat ATTRIBUTE_UNUSED,
2e9b04
+	      int preserve_dates ATTRIBUTE_UNUSED)
2e9b04
 {
2e9b04
-  bfd_boolean exists;
2e9b04
-  struct stat s;
2e9b04
   int ret = 0;
2e9b04
-
2e9b04
-  exists = lstat (to, &s) == 0;
2e9b04
+  bfd_boolean exists = target_stat != NULL;
2e9b04
 
2e9b04
 #if defined (_WIN32) && !defined (__CYGWIN32__)
2e9b04
   /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
2e9b04
@@ -158,36 +196,35 @@ smart_rename (const char *from, const ch
2e9b04
       unlink (from);
2e9b04
     }
2e9b04
 #else
2e9b04
-  /* Use rename only if TO is not a symbolic link and has
2e9b04
-     only one hard link, and we have permission to write to it.  */
2e9b04
+  /* Avoid a full copy and use rename if we can fix up permissions of the
2e9b04
+     file after renaming, i.e.:
2e9b04
+
2e9b04
+     - TO is not a symbolic link
2e9b04
+     - TO is a regular file with only one hard link
2e9b04
+     - We have permission to write to TO
2e9b04
+     - FD is available to safely fix up permissions to be the same as the file
2e9b04
+       we overwrote with the rename.
2e9b04
+
2e9b04
+     Note though that the actual file on disk that TARGET_STAT describes may
2e9b04
+     have changed and we're only trying to preserve the status we know about.
2e9b04
+     At no point do we try to interact with the new file changes, so there can
2e9b04
+     only be two outcomes, i.e. either the external file change survives
2e9b04
+     without knowledge of our change (if it happens after the rename syscall)
2e9b04
+     or our rename and permissions fixup survive without any knowledge of the
2e9b04
+     external change.  */
2e9b04
   if (! exists
2e9b04
-      || (!S_ISLNK (s.st_mode)
2e9b04
-	  && S_ISREG (s.st_mode)
2e9b04
-	  && (s.st_mode & S_IWUSR)
2e9b04
-	  && s.st_nlink == 1)
2e9b04
+      || (fd >= 0
2e9b04
+	  && !S_ISLNK (target_stat->st_mode)
2e9b04
+	  && S_ISREG (target_stat->st_mode)
2e9b04
+	  && (target_stat->st_mode & S_IWUSR)
2e9b04
+	  && target_stat->st_nlink == 1)
2e9b04
       )
2e9b04
     {
2e9b04
       ret = rename (from, to);
2e9b04
       if (ret == 0)
2e9b04
 	{
2e9b04
 	  if (exists)
2e9b04
-	    {
2e9b04
-	      /* Try to preserve the permission bits and ownership of
2e9b04
-		 TO.  First get the mode right except for the setuid
2e9b04
-		 bit.  Then change the ownership.  Then fix the setuid
2e9b04
-		 bit.  We do the chmod before the chown because if the
2e9b04
-		 chown succeeds, and we are a normal user, we won't be
2e9b04
-		 able to do the chmod afterward.  We don't bother to
2e9b04
-		 fix the setuid bit first because that might introduce
2e9b04
-		 a fleeting security problem, and because the chown
2e9b04
-		 will clear the setuid bit anyhow.  We only fix the
2e9b04
-		 setuid bit if the chown succeeds, because we don't
2e9b04
-		 want to introduce an unexpected setuid file owned by
2e9b04
-		 the user running objcopy.  */
2e9b04
-	      chmod (to, s.st_mode & 0777);
2e9b04
-	      if (chown (to, s.st_uid, s.st_gid) >= 0)
2e9b04
-		chmod (to, s.st_mode & 07777);
2e9b04
-	    }
2e9b04
+	    try_preserve_permissions (fd, target_stat);
2e9b04
 	}
2e9b04
       else
2e9b04
 	{
2e9b04
@@ -203,9 +240,11 @@ smart_rename (const char *from, const ch
2e9b04
 	non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
2e9b04
 
2e9b04
       if (preserve_dates)
2e9b04
-	set_times (to, &s);
2e9b04
+	set_times (to, target_stat);
2e9b04
       unlink (from);
2e9b04
     }
2e9b04
+  if (fd >= 0)
2e9b04
+    close (fd);
2e9b04
 #endif /* _WIN32 && !__CYGWIN32__ */
2e9b04
 
2e9b04
   return ret;
2e9b04
diff -rup binutils.orig/binutils/ar.c binutils-2.35.1/binutils/ar.c
2e9b04
--- binutils.orig/binutils/ar.c	2021-02-02 13:01:42.257734944 +0000
2e9b04
+++ binutils-2.35.1/binutils/ar.c	2021-02-02 13:11:13.340958352 +0000
2e9b04
@@ -25,7 +25,6 @@
2e9b04
 
2e9b04
 #include "sysdep.h"
2e9b04
 #include "bfd.h"
2e9b04
-#include "libbfd.h"
2e9b04
 #include "libiberty.h"
2e9b04
 #include "progress.h"
2e9b04
 #include "getopt.h"
2e9b04
@@ -1082,7 +1081,7 @@ open_output_file (bfd * abfd)
2e9b04
 		 output_filename, base);
2e9b04
       output_filename = base;
2e9b04
     }
2e9b04
-  
2e9b04
+
2e9b04
   if (output_dir)
2e9b04
     {
2e9b04
       size_t len = strlen (output_dir);
2e9b04
@@ -1099,7 +1098,7 @@ open_output_file (bfd * abfd)
2e9b04
 
2e9b04
   if (verbose)
2e9b04
     printf ("x - %s\n", output_filename);
2e9b04
-  
2e9b04
+
2e9b04
   FILE * ostream = fopen (output_filename, FOPEN_WB);
2e9b04
   if (ostream == NULL)
2e9b04
     {
2e9b04
@@ -1198,10 +1197,8 @@ write_archive (bfd *iarch)
2e9b04
   bfd *contents_head = iarch->archive_next;
2e9b04
   int ofd = -1;
2e9b04
   struct stat target_stat;
2e9b04
-  bfd_boolean skip_stat = FALSE;
2e9b04
 
2e9b04
-  old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
2e9b04
-  strcpy (old_name, bfd_get_filename (iarch));
2e9b04
+  old_name = xstrdup (bfd_get_filename (iarch));
2e9b04
   new_name = make_tempname (old_name, &ofd;;
2e9b04
 
2e9b04
   if (new_name == NULL)
2e9b04
@@ -1246,11 +1243,9 @@ write_archive (bfd *iarch)
2e9b04
 
2e9b04
 #if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
   ofd = dup (ofd);
2e9b04
-  if (iarch == NULL || iarch->iostream == NULL)
2e9b04
-    skip_stat = TRUE;
2e9b04
-  else if (ofd == -1 || fstat (fileno (iarch->iostream), &target_stat) != 0)
2e9b04
-    bfd_fatal (old_name);
2e9b04
 #endif
2e9b04
+  if (ofd == -1 || bfd_stat (iarch, &target_stat) != 0)
2e9b04
+    bfd_fatal (old_name);
2e9b04
 
2e9b04
   if (!bfd_close (obfd))
2e9b04
     bfd_fatal (old_name);
2e9b04
@@ -1261,7 +1256,7 @@ write_archive (bfd *iarch)
2e9b04
   /* We don't care if this fails; we might be creating the archive.  */
2e9b04
   bfd_close (iarch);
2e9b04
 
2e9b04
-  if (smart_rename (new_name, old_name, ofd, skip_stat ? NULL : &target_stat, 0) != 0)
2e9b04
+  if (smart_rename (new_name, old_name, ofd, &target_stat, 0) != 0)
2e9b04
     xexit (1);
2e9b04
   free (old_name);
2e9b04
   free (new_name);
2e9b04
Only in binutils-2.35.1/binutils/: ar.c.orig
2e9b04
Only in binutils-2.35.1/binutils/: ar.c.rej
2e9b04
diff -rup binutils.orig/binutils/arsup.c binutils-2.35.1/binutils/arsup.c
2e9b04
--- binutils.orig/binutils/arsup.c	2021-02-02 13:01:42.208735269 +0000
2e9b04
+++ binutils-2.35.1/binutils/arsup.c	2021-02-02 13:11:55.725678308 +0000
2e9b04
@@ -42,6 +42,8 @@ extern int deterministic;
2e9b04
 
2e9b04
 static bfd *obfd;
2e9b04
 static char *real_name;
2e9b04
+static char *temp_name;
2e9b04
+static int real_ofd;
2e9b04
 static FILE *outfile;
2e9b04
 
2e9b04
 static void
2e9b04
@@ -149,27 +151,24 @@ maybequit (void)
2e9b04
 void
2e9b04
 ar_open (char *name, int t)
2e9b04
 {
2e9b04
-  char *tname;
2e9b04
-  const char *bname = lbasename (name);
2e9b04
-  real_name = name;
2e9b04
-
2e9b04
-  /* Prepend tmp- to the beginning, to avoid file-name clashes after
2e9b04
-     truncation on filesystems with limited namespaces (DOS).  */
2e9b04
-  if (asprintf (&tname, "%.*stmp-%s", (int) (bname - name), name, bname) == -1)
2e9b04
+  real_name = xstrdup (name);
2e9b04
+  temp_name = make_tempname (real_name, &real_ofd);
2e9b04
+
2e9b04
+  if (temp_name == NULL)
2e9b04
     {
2e9b04
-      fprintf (stderr, _("%s: Can't allocate memory for temp name (%s)\n"),
2e9b04
+      fprintf (stderr, _("%s: Can't open temporary file (%s)\n"),
2e9b04
 	       program_name, strerror(errno));
2e9b04
       maybequit ();
2e9b04
       return;
2e9b04
     }
2e9b04
 
2e9b04
-  obfd = bfd_openw (tname, NULL);
2e9b04
+  obfd = bfd_fdopenw (temp_name, NULL, real_ofd);
2e9b04
 
2e9b04
   if (!obfd)
2e9b04
     {
2e9b04
       fprintf (stderr,
2e9b04
 	       _("%s: Can't open output archive %s\n"),
2e9b04
-	       program_name,  tname);
2e9b04
+	       program_name, temp_name);
2e9b04
 
2e9b04
       maybequit ();
2e9b04
     }
2e9b04
@@ -344,10 +343,9 @@ ar_save (void)
2e9b04
     }
2e9b04
   else
2e9b04
     {
2e9b04
-      char *ofilename = xstrdup (bfd_get_filename (obfd));
2e9b04
       bfd_boolean skip_stat = FALSE;
2e9b04
       struct stat target_stat;
2e9b04
-      int ofd = -1;
2e9b04
+      int ofd = real_ofd;
2e9b04
 
2e9b04
       if (deterministic > 0)
2e9b04
         obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
2e9b04
@@ -355,17 +353,18 @@ ar_save (void)
2e9b04
 #if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
       /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
2e9b04
          copy fallback to write the output.  */
2e9b04
-      ofd = dup (fileno (obfd->iostream));
2e9b04
-      if (lstat (real_name, &target_stat) != 0)
2e9b04
-	skip_stat = TRUE;
2e9b04
+      ofd = dup (ofd);
2e9b04
 #endif
2e9b04
-
2e9b04
       bfd_close (obfd);
2e9b04
 
2e9b04
-      smart_rename (ofilename, real_name, ofd,
2e9b04
+      if (ofd == -1 || fstat (ofd, &target_stat) != 0)
2e9b04
+	skip_stat = TRUE;
2e9b04
+
2e9b04
+      smart_rename (temp_name, real_name, ofd,
2e9b04
 		    skip_stat ? NULL : &target_stat, 0);
2e9b04
       obfd = 0;
2e9b04
-      free (ofilename);
2e9b04
+      free (temp_name);
2e9b04
+      free (real_name);
2e9b04
     }
2e9b04
 }
2e9b04
 
2e9b04
Only in binutils-2.35.1/binutils/: arsup.c.orig
2e9b04
Only in binutils-2.35.1/binutils/: arsup.c.rej
2e9b04
diff -rup binutils.orig/binutils/objcopy.c binutils-2.35.1/binutils/objcopy.c
2e9b04
--- binutils.orig/binutils/objcopy.c	2021-02-02 13:01:42.214735229 +0000
2e9b04
+++ binutils-2.35.1/binutils/objcopy.c	2021-02-02 13:13:27.613071192 +0000
2e9b04
@@ -20,7 +20,6 @@
2e9b04
 
2e9b04
 #include "sysdep.h"
2e9b04
 #include "bfd.h"
2e9b04
-#include "libbfd.h"
2e9b04
 #include "progress.h"
2e9b04
 #include "getopt.h"
2e9b04
 #include "libiberty.h"
2e9b04
@@ -3733,7 +3732,7 @@ copy_file (const char *input_filename, c
2e9b04
   /* To allow us to do "strip *" without dying on the first
2e9b04
      non-object file, failures are nonfatal.  */
2e9b04
   ibfd = bfd_openr (input_filename, input_target);
2e9b04
-  if (ibfd == NULL || fstat (fileno (ibfd->iostream), in_stat) != 0)
2e9b04
+  if (ibfd == NULL || bfd_stat (ibfd, in_stat) != 0)
2e9b04
     {
2e9b04
       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2e9b04
       status = 1;
2e9b04
Only in binutils-2.35.1/binutils/: objcopy.c.orig
2e9b04
Only in binutils-2.35.1/binutils/: objcopy.c.rej
2e9b04
--- binutils.orig/binutils/arsup.c	2021-02-04 10:42:03.265729780 +0000
2e9b04
+++ binutils-2.35.1/binutils/arsup.c	2021-02-04 10:45:48.439166658 +0000
2e9b04
@@ -357,8 +357,21 @@ ar_save (void)
2e9b04
 #endif
2e9b04
       bfd_close (obfd);
2e9b04
 
2e9b04
-      if (ofd == -1 || fstat (ofd, &target_stat) != 0)
2e9b04
-	skip_stat = TRUE;
2e9b04
+      if (lstat (real_name, &target_stat) != 0)
2e9b04
+	{
2e9b04
+	  /* The temp file created in ar_open has mode 0600 as per mkstemp.
2e9b04
+	     Create the real empty output file here so smart_rename will
2e9b04
+	     update the mode according to the process umask.  */
2e9b04
+	  obfd = bfd_openw (real_name, NULL);
2e9b04
+	  if (obfd == NULL
2e9b04
+	      || bfd_stat (obfd, &target_stat) != 0)
2e9b04
+	    skip_stat = TRUE;
2e9b04
+	  if (obfd != NULL)
2e9b04
+	    {
2e9b04
+	      bfd_set_format (obfd, bfd_archive);
2e9b04
+	      bfd_close (obfd);
2e9b04
+	    }
2e9b04
+	}
2e9b04
 
2e9b04
       smart_rename (temp_name, real_name, ofd,
2e9b04
 		    skip_stat ? NULL : &target_stat, 0);
2e9b04
--- binutils.orig/binutils/rename.c	2021-02-08 11:02:58.767933783 +0000
2e9b04
+++ binutils-2.35.1/binutils/rename.c	2021-02-08 11:20:37.539179363 +0000
2e9b04
@@ -179,7 +179,10 @@ smart_rename (const char *from, const ch
2e9b04
 	      int preserve_dates ATTRIBUTE_UNUSED)
2e9b04
 {
2e9b04
   int ret = 0;
2e9b04
-  bfd_boolean exists = target_stat != NULL;
2e9b04
+  struct stat to_stat;
2e9b04
+  bfd_boolean exists;
2e9b04
+
2e9b04
+  exists = lstat (to, &to_stat) == 0;
2e9b04
 
2e9b04
 #if defined (_WIN32) && !defined (__CYGWIN32__)
2e9b04
   /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
2e9b04
@@ -214,16 +217,16 @@ smart_rename (const char *from, const ch
2e9b04
      external change.  */
2e9b04
   if (! exists
2e9b04
       || (fd >= 0
2e9b04
-	  && !S_ISLNK (target_stat->st_mode)
2e9b04
-	  && S_ISREG (target_stat->st_mode)
2e9b04
-	  && (target_stat->st_mode & S_IWUSR)
2e9b04
-	  && target_stat->st_nlink == 1)
2e9b04
+	  && !S_ISLNK (to_stat.st_mode)
2e9b04
+	  && S_ISREG (to_stat.st_mode)
2e9b04
+	  && (to_stat.st_mode & S_IWUSR)
2e9b04
+	  && to_stat.st_nlink == 1)
2e9b04
       )
2e9b04
     {
2e9b04
       ret = rename (from, to);
2e9b04
       if (ret == 0)
2e9b04
 	{
2e9b04
-	  if (exists)
2e9b04
+	  if (exists && target_stat != NULL)
2e9b04
 	    try_preserve_permissions (fd, target_stat);
2e9b04
 	}
2e9b04
       else
2e9b04
@@ -239,7 +242,7 @@ smart_rename (const char *from, const ch
2e9b04
       if (ret != 0)
2e9b04
 	non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
2e9b04
 
2e9b04
-      if (preserve_dates)
2e9b04
+      if (preserve_dates && target_stat != NULL)
2e9b04
 	set_times (to, target_stat);
2e9b04
       unlink (from);
2e9b04
     }
2e9b04
diff -rup binutils.orig/binutils/ar.c binutils-2.35.1/binutils/ar.c
2e9b04
--- binutils.orig/binutils/ar.c	2021-03-11 12:38:18.183422774 +0000
2e9b04
+++ binutils-2.35.1/binutils/ar.c	2021-03-11 12:45:09.279716067 +0000
2e9b04
@@ -1195,22 +1195,21 @@ write_archive (bfd *iarch)
2e9b04
   bfd *obfd;
2e9b04
   char *old_name, *new_name;
2e9b04
   bfd *contents_head = iarch->archive_next;
2e9b04
-  int ofd = -1;
2e9b04
-  struct stat target_stat;
2e9b04
+  int tmpfd = -1;
2e9b04
 
2e9b04
   old_name = xstrdup (bfd_get_filename (iarch));
2e9b04
-  new_name = make_tempname (old_name, &ofd;;
2e9b04
+  new_name = make_tempname (old_name, &tmpfd);
2e9b04
 
2e9b04
   if (new_name == NULL)
2e9b04
     bfd_fatal (_("could not create temporary file whilst writing archive"));
2e9b04
 
2e9b04
   output_filename = new_name;
2e9b04
 
2e9b04
-  obfd = bfd_fdopenw (new_name, bfd_get_target (iarch), ofd);
2e9b04
+  obfd = bfd_fdopenw (new_name, bfd_get_target (iarch), tmpfd);
2e9b04
 
2e9b04
   if (obfd == NULL)
2e9b04
     {
2e9b04
-      close (ofd);
2e9b04
+      close (tmpfd);
2e9b04
       bfd_fatal (old_name);
2e9b04
     }
2e9b04
 
2e9b04
@@ -1241,12 +1240,7 @@ write_archive (bfd *iarch)
2e9b04
   if (!bfd_set_archive_head (obfd, contents_head))
2e9b04
     bfd_fatal (old_name);
2e9b04
 
2e9b04
-#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
-  ofd = dup (ofd);
2e9b04
-#endif
2e9b04
-  if (ofd == -1 || bfd_stat (iarch, &target_stat) != 0)
2e9b04
-    bfd_fatal (old_name);
2e9b04
-
2e9b04
+  tmpfd = dup (tmpfd);
2e9b04
   if (!bfd_close (obfd))
2e9b04
     bfd_fatal (old_name);
2e9b04
 
2e9b04
@@ -1256,7 +1250,7 @@ write_archive (bfd *iarch)
2e9b04
   /* We don't care if this fails; we might be creating the archive.  */
2e9b04
   bfd_close (iarch);
2e9b04
 
2e9b04
-  if (smart_rename (new_name, old_name, ofd, &target_stat, 0) != 0)
2e9b04
+  if (smart_rename (new_name, old_name, tmpfd, NULL, FALSE) != 0)
2e9b04
     xexit (1);
2e9b04
   free (old_name);
2e9b04
   free (new_name);
2e9b04
diff -rup binutils.orig/binutils/arsup.c binutils-2.35.1/binutils/arsup.c
2e9b04
--- binutils.orig/binutils/arsup.c	2021-03-11 12:38:18.182422781 +0000
2e9b04
+++ binutils-2.35.1/binutils/arsup.c	2021-03-11 12:47:43.246702325 +0000
2e9b04
@@ -43,7 +43,7 @@ extern int deterministic;
2e9b04
 static bfd *obfd;
2e9b04
 static char *real_name;
2e9b04
 static char *temp_name;
2e9b04
-static int real_ofd;
2e9b04
+static int temp_fd;
2e9b04
 static FILE *outfile;
2e9b04
 
2e9b04
 static void
2e9b04
@@ -152,7 +152,7 @@ void
2e9b04
 ar_open (char *name, int t)
2e9b04
 {
2e9b04
   real_name = xstrdup (name);
2e9b04
-  temp_name = make_tempname (real_name, &real_ofd);
2e9b04
+  temp_name = make_tempname (real_name, &temp_fd);
2e9b04
 
2e9b04
   if (temp_name == NULL)
2e9b04
     {
2e9b04
@@ -162,7 +162,7 @@ ar_open (char *name, int t)
2e9b04
       return;
2e9b04
     }
2e9b04
 
2e9b04
-  obfd = bfd_fdopenw (temp_name, NULL, real_ofd);
2e9b04
+  obfd = bfd_fdopenw (temp_name, NULL, temp_fd);
2e9b04
 
2e9b04
   if (!obfd)
2e9b04
     {
2e9b04
@@ -343,29 +343,20 @@ ar_save (void)
2e9b04
     }
2e9b04
   else
2e9b04
     {
2e9b04
-      bfd_boolean skip_stat = FALSE;
2e9b04
       struct stat target_stat;
2e9b04
-      int ofd = real_ofd;
2e9b04
 
2e9b04
       if (deterministic > 0)
2e9b04
         obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
2e9b04
 
2e9b04
-#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
-      /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
2e9b04
-         copy fallback to write the output.  */
2e9b04
-      ofd = dup (ofd);
2e9b04
-#endif
2e9b04
+      temp_fd = dup (temp_fd);
2e9b04
       bfd_close (obfd);
2e9b04
 
2e9b04
-      if (lstat (real_name, &target_stat) != 0)
2e9b04
+      if (stat (real_name, &target_stat) != 0)
2e9b04
 	{
2e9b04
 	  /* The temp file created in ar_open has mode 0600 as per mkstemp.
2e9b04
 	     Create the real empty output file here so smart_rename will
2e9b04
 	     update the mode according to the process umask.  */
2e9b04
 	  obfd = bfd_openw (real_name, NULL);
2e9b04
-	  if (obfd == NULL
2e9b04
-	      || bfd_stat (obfd, &target_stat) != 0)
2e9b04
-	    skip_stat = TRUE;
2e9b04
 	  if (obfd != NULL)
2e9b04
 	    {
2e9b04
 	      bfd_set_format (obfd, bfd_archive);
2e9b04
@@ -373,9 +364,8 @@ ar_save (void)
2e9b04
 	    }
2e9b04
 	}
2e9b04
 
2e9b04
-      smart_rename (temp_name, real_name, ofd,
2e9b04
-		    skip_stat ? NULL : &target_stat, 0);
2e9b04
-      obfd = 0;
2e9b04
+      smart_rename (temp_name, real_name, temp_fd, NULL, FALSE);
2e9b04
+      obfd = NULL;
2e9b04
       free (temp_name);
2e9b04
       free (real_name);
2e9b04
     }
2e9b04
diff -rup binutils.orig/binutils/bucomm.h binutils-2.35.1/binutils/bucomm.h
2e9b04
--- binutils.orig/binutils/bucomm.h	2021-03-11 12:38:18.183422774 +0000
2e9b04
+++ binutils-2.35.1/binutils/bucomm.h	2021-03-11 12:42:22.320815334 +0000
2e9b04
@@ -71,7 +71,8 @@ extern void print_version (const char *)
2e9b04
 /* In rename.c.  */
2e9b04
 extern void set_times (const char *, const struct stat *);
2e9b04
 
2e9b04
-extern int smart_rename (const char *, const char *, int, struct stat *, int);
2e9b04
+extern int smart_rename (const char *, const char *, int,
2e9b04
+			 struct stat *, bfd_boolean);
2e9b04
 
2e9b04
 
2e9b04
 /* In libiberty.  */
2e9b04
diff -rup binutils.orig/binutils/objcopy.c binutils-2.35.1/binutils/objcopy.c
2e9b04
--- binutils.orig/binutils/objcopy.c	2021-03-11 12:38:18.181422787 +0000
2e9b04
+++ binutils-2.35.1/binutils/objcopy.c	2021-03-11 12:51:09.486344417 +0000
2e9b04
@@ -4802,12 +4802,7 @@ strip_main (int argc, char *argv[])
2e9b04
       else
2e9b04
 	tmpname = output_file;
2e9b04
 
2e9b04
-      if (tmpname == NULL
2e9b04
-#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
-	  /* Retain a copy of TMPFD since we will need it for SMART_RENAME.  */
2e9b04
-	  || (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
2e9b04
-#endif
2e9b04
-      )
2e9b04
+      if (tmpname == NULL)
2e9b04
 	{
2e9b04
 	  bfd_nonfatal_message (argv[i], NULL, NULL,
2e9b04
 				_("could not create temporary file to hold stripped copy"));
2e9b04
@@ -4820,21 +4815,17 @@ strip_main (int argc, char *argv[])
2e9b04
 		 output_target, NULL);
2e9b04
       if (status == 0)
2e9b04
 	{
2e9b04
-	  if (preserve_dates)
2e9b04
-	    set_times (tmpname, &statbuf);
2e9b04
 	  if (output_file != tmpname)
2e9b04
-	    status = (smart_rename (tmpname,
2e9b04
-				    output_file ? output_file : argv[i],
2e9b04
-				    copyfd, &statbuf, preserve_dates) != 0);
2e9b04
+	    status = smart_rename (tmpname,
2e9b04
+				   output_file ? output_file : argv[i],
2e9b04
+				   copyfd, &statbuf, preserve_dates) != 0;
2e9b04
 	  if (status == 0)
2e9b04
 	    status = hold_status;
2e9b04
 	}
2e9b04
       else
2e9b04
 	{
2e9b04
-#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
 	  if (copyfd >= 0)
2e9b04
 	    close (copyfd);
2e9b04
-#endif
2e9b04
 	  unlink_if_ordinary (tmpname);
2e9b04
 	}
2e9b04
       if (output_file != tmpname)
2e9b04
@@ -5043,8 +5034,9 @@ copy_main (int argc, char *argv[])
2e9b04
   bfd_boolean formats_info = FALSE;
2e9b04
   bfd_boolean use_globalize = FALSE;
2e9b04
   bfd_boolean use_keep_global = FALSE;
2e9b04
-  int c, tmpfd = -1;
2e9b04
-  int copyfd = -1;
2e9b04
+  int c;
2e9b04
+  int tmpfd = -1;
2e9b04
+  int copyfd;
2e9b04
   struct stat statbuf;
2e9b04
   const bfd_arch_info_type *input_arch = NULL;
2e9b04
 
2e9b04
@@ -5882,19 +5874,19 @@ copy_main (int argc, char *argv[])
2e9b04
     }
2e9b04
 
2e9b04
   /* If there is no destination file, or the source and destination files
2e9b04
-     are the same, then create a temp and rename the result into the input.  */
2e9b04
+     are the same, then create a temp and copy the result into the input.  */
2e9b04
+  copyfd = -1;
2e9b04
   if (output_filename == NULL
2e9b04
       || filename_cmp (input_filename, output_filename) == 0)
2e9b04
-    tmpname = make_tempname (input_filename, &tmpfd);
2e9b04
+    {
2e9b04
+      tmpname = make_tempname (input_filename, &tmpfd);
2e9b04
+      if (tmpfd >= 0)
2e9b04
+	copyfd = dup (tmpfd);
2e9b04
+    }
2e9b04
   else
2e9b04
     tmpname = output_filename;
2e9b04
 
2e9b04
-  if (tmpname == NULL
2e9b04
-#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
-      /* Retain a copy of TMPFD since we will need it for SMART_RENAME.  */
2e9b04
-      || (tmpfd >= 0 && (copyfd = dup (tmpfd)) == -1)
2e9b04
-#endif
2e9b04
-  )
2e9b04
+  if (tmpname == NULL)
2e9b04
     {
2e9b04
       fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
2e9b04
 	     input_filename, strerror (errno));
2e9b04
@@ -5904,18 +5896,14 @@ copy_main (int argc, char *argv[])
2e9b04
 	     output_target, input_arch);
2e9b04
   if (status == 0)
2e9b04
     {
2e9b04
-      if (preserve_dates)
2e9b04
-	set_times (tmpname, &statbuf);
2e9b04
       if (tmpname != output_filename)
2e9b04
-	status = (smart_rename (tmpname, input_filename, copyfd, &statbuf,
2e9b04
-				preserve_dates) != 0);
2e9b04
+	status = smart_rename (tmpname, input_filename, copyfd,
2e9b04
+			       &statbuf, preserve_dates) != 0;
2e9b04
     }
2e9b04
   else
2e9b04
     {
2e9b04
-#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
       if (copyfd >= 0)
2e9b04
 	close (copyfd);
2e9b04
-#endif
2e9b04
       unlink_if_ordinary (tmpname);
2e9b04
     }
2e9b04
 
2e9b04
diff -rup binutils.orig/binutils/rename.c binutils-2.35.1/binutils/rename.c
2e9b04
--- binutils.orig/binutils/rename.c	2021-03-11 12:38:18.183422774 +0000
2e9b04
+++ binutils-2.35.1/binutils/rename.c	2021-03-11 12:41:41.824081969 +0000
2e9b04
@@ -30,30 +30,25 @@
2e9b04
 #endif /* HAVE_UTIMES */
2e9b04
 #endif /* ! HAVE_GOOD_UTIME_H */
2e9b04
 
2e9b04
-#if ! defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
-static int simple_copy (const char *, const char *);
2e9b04
-
2e9b04
 /* The number of bytes to copy at once.  */
2e9b04
 #define COPY_BUF 8192
2e9b04
 
2e9b04
-/* Copy file FROM to file TO, performing no translations.
2e9b04
+/* Copy file FROMFD to file TO, performing no translations.
2e9b04
    Return 0 if ok, -1 if error.  */
2e9b04
 
2e9b04
 static int
2e9b04
-simple_copy (const char *from, const char *to)
2e9b04
+simple_copy (int fromfd, const char *to,
2e9b04
+	     struct stat *target_stat ATTRIBUTE_UNUSED)
2e9b04
 {
2e9b04
-  int fromfd, tofd, nread;
2e9b04
+  int tofd, nread;
2e9b04
   int saved;
2e9b04
   char buf[COPY_BUF];
2e9b04
 
2e9b04
-  fromfd = open (from, O_RDONLY | O_BINARY);
2e9b04
-  if (fromfd < 0)
2e9b04
+  if (fromfd < 0
2e9b04
+      || lseek (fromfd, 0, SEEK_SET) != 0)
2e9b04
     return -1;
2e9b04
-#ifdef O_CREAT
2e9b04
-  tofd = open (to, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0777);
2e9b04
-#else
2e9b04
-  tofd = creat (to, 0777);
2e9b04
-#endif
2e9b04
+
2e9b04
+  tofd = open (to, O_WRONLY | O_TRUNC | O_BINARY);
2e9b04
   if (tofd < 0)
2e9b04
     {
2e9b04
       saved = errno;
2e9b04
@@ -61,6 +56,7 @@ simple_copy (const char *from, const cha
2e9b04
       errno = saved;
2e9b04
       return -1;
2e9b04
     }
2e9b04
+
2e9b04
   while ((nread = read (fromfd, buf, sizeof buf)) > 0)
2e9b04
     {
2e9b04
       if (write (tofd, buf, nread) != nread)
2e9b04
@@ -72,7 +68,16 @@ simple_copy (const char *from, const cha
2e9b04
 	  return -1;
2e9b04
 	}
2e9b04
     }
2e9b04
+
2e9b04
   saved = errno;
2e9b04
+
2e9b04
+#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
+  /* Writing to a setuid/setgid file may clear S_ISUID and S_ISGID.
2e9b04
+     Try to restore them, ignoring failure.  */
2e9b04
+  if (target_stat != NULL)
2e9b04
+    fchmod (tofd, target_stat->st_mode);
2e9b04
+#endif
2e9b04
+
2e9b04
   close (fromfd);
2e9b04
   close (tofd);
2e9b04
   if (nread < 0)
2e9b04
@@ -82,7 +87,6 @@ simple_copy (const char *from, const cha
2e9b04
     }
2e9b04
   return 0;
2e9b04
 }
2e9b04
-#endif /* __CYGWIN32__ or not _WIN32 */
2e9b04
 
2e9b04
 /* Set the times of the file DESTINATION to be the same as those in
2e9b04
    STATBUF.  */
2e9b04
@@ -91,164 +95,52 @@ void
2e9b04
 set_times (const char *destination, const struct stat *statbuf)
2e9b04
 {
2e9b04
   int result;
2e9b04
-
2e9b04
-  {
2e9b04
 #ifdef HAVE_GOOD_UTIME_H
2e9b04
-    struct utimbuf tb;
2e9b04
+  struct utimbuf tb;
2e9b04
 
2e9b04
-    tb.actime = statbuf->st_atime;
2e9b04
-    tb.modtime = statbuf->st_mtime;
2e9b04
-    result = utime (destination, &tb);
2e9b04
-#else /* ! HAVE_GOOD_UTIME_H */
2e9b04
-#ifndef HAVE_UTIMES
2e9b04
-    long tb[2];
2e9b04
-
2e9b04
-    tb[0] = statbuf->st_atime;
2e9b04
-    tb[1] = statbuf->st_mtime;
2e9b04
-    result = utime (destination, tb);
2e9b04
-#else /* HAVE_UTIMES */
2e9b04
-    struct timeval tv[2];
2e9b04
-
2e9b04
-    tv[0].tv_sec = statbuf->st_atime;
2e9b04
-    tv[0].tv_usec = 0;
2e9b04
-    tv[1].tv_sec = statbuf->st_mtime;
2e9b04
-    tv[1].tv_usec = 0;
2e9b04
-    result = utimes (destination, tv);
2e9b04
-#endif /* HAVE_UTIMES */
2e9b04
-#endif /* ! HAVE_GOOD_UTIME_H */
2e9b04
-  }
2e9b04
-
2e9b04
-  if (result != 0)
2e9b04
-    non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
2e9b04
-}
2e9b04
-
2e9b04
-#ifndef S_ISLNK
2e9b04
-#ifdef S_IFLNK
2e9b04
-#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
2e9b04
+  tb.actime = statbuf->st_atime;
2e9b04
+  tb.modtime = statbuf->st_mtime;
2e9b04
+  result = utime (destination, &tb);
2e9b04
+#elif defined HAVE_UTIMES
2e9b04
+  struct timeval tv[2];
2e9b04
+
2e9b04
+  tv[0].tv_sec = statbuf->st_atime;
2e9b04
+  tv[0].tv_usec = 0;
2e9b04
+  tv[1].tv_sec = statbuf->st_mtime;
2e9b04
+  tv[1].tv_usec = 0;
2e9b04
+  result = utimes (destination, tv);
2e9b04
 #else
2e9b04
-#define S_ISLNK(m) 0
2e9b04
-#define lstat stat
2e9b04
-#endif
2e9b04
-#endif
2e9b04
-
2e9b04
-#if !defined (_WIN32) || defined (__CYGWIN32__)
2e9b04
-/* Try to preserve the permission bits and ownership of an existing file when
2e9b04
-   rename overwrites it.  FD is the file being renamed and TARGET_STAT has the
2e9b04
-   status of the file that was overwritten.  */
2e9b04
-static void
2e9b04
-try_preserve_permissions (int fd, struct stat *target_stat)
2e9b04
-{
2e9b04
-  struct stat from_stat;
2e9b04
-  int ret = 0;
2e9b04
-
2e9b04
-  if (fstat (fd, &from_stat) != 0)
2e9b04
-    return;
2e9b04
+  long tb[2];
2e9b04
 
2e9b04
-  int from_mode = from_stat.st_mode & 0777;
2e9b04
-  int to_mode = target_stat->st_mode & 0777;
2e9b04
+  tb[0] = statbuf->st_atime;
2e9b04
+  tb[1] = statbuf->st_mtime;
2e9b04
+  result = utime (destination, tb);
2e9b04
+#endif
2e9b04
 
2e9b04
-  /* Fix up permissions before we potentially lose ownership with fchown.
2e9b04
-     Clear the setxid bits because in case the fchown below fails then we don't
2e9b04
-     want to end up with a sxid file owned by the invoking user.  If the user
2e9b04
-     hasn't changed or if fchown succeeded, we add back the sxid bits at the
2e9b04
-     end.  */
2e9b04
-  if (from_mode != to_mode)
2e9b04
-    fchmod (fd, to_mode);
2e9b04
-
2e9b04
-  /* Fix up ownership, this will clear the setxid bits.  */
2e9b04
-  if (from_stat.st_uid != target_stat->st_uid
2e9b04
-      || from_stat.st_gid != target_stat->st_gid)
2e9b04
-    ret = fchown (fd, target_stat->st_uid, target_stat->st_gid);
2e9b04
-
2e9b04
-  /* Fix up the sxid bits if either the fchown wasn't needed or it
2e9b04
-     succeeded.  */
2e9b04
-  if (ret == 0)
2e9b04
-    fchmod (fd, target_stat->st_mode & 07777);
2e9b04
+  if (result != 0)
2e9b04
+    non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
2e9b04
 }
2e9b04
-#endif
2e9b04
 
2e9b04
-/* Rename FROM to TO, copying if TO is either a link or is not a regular file.
2e9b04
-   FD is an open file descriptor pointing to FROM that we can use to safely fix
2e9b04
-   up permissions of the file after renaming.  TARGET_STAT has the file status
2e9b04
-   that is used to fix up permissions and timestamps after rename.  Return 0 if
2e9b04
-   ok, -1 if error and FD is closed before returning.  */
2e9b04
+/* Copy FROM to TO.  TARGET_STAT has the file status that, if non-NULL,
2e9b04
+   is used to fix up timestamps.  Return 0 if ok, -1 if error.
2e9b04
+   At one time this function renamed files, but file permissions are
2e9b04
+   tricky to update given the number of different schemes used by
2e9b04
+   various systems.  So now we just copy.  */
2e9b04
 
2e9b04
 int
2e9b04
-smart_rename (const char *from, const char *to, int fd ATTRIBUTE_UNUSED,
2e9b04
-	      struct stat *target_stat ATTRIBUTE_UNUSED,
2e9b04
-	      int preserve_dates ATTRIBUTE_UNUSED)
2e9b04
+smart_rename (const char *from, const char *to, int fromfd,
2e9b04
+	      struct stat *target_stat, bfd_boolean preserve_dates)
2e9b04
 {
2e9b04
-  int ret = 0;
2e9b04
-  struct stat to_stat;
2e9b04
-  bfd_boolean exists;
2e9b04
-
2e9b04
-  exists = lstat (to, &to_stat) == 0;
2e9b04
-
2e9b04
-#if defined (_WIN32) && !defined (__CYGWIN32__)
2e9b04
-  /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
2e9b04
-     fail instead.  Also, chown is not present.  */
2e9b04
+  int ret;
2e9b04
 
2e9b04
-  if (exists)
2e9b04
-    remove (to);
2e9b04
-
2e9b04
-  ret = rename (from, to);
2e9b04
+  ret = simple_copy (fromfd, to, target_stat);
2e9b04
   if (ret != 0)
2e9b04
-    {
2e9b04
-      /* We have to clean up here.  */
2e9b04
-      non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
2e9b04
-      unlink (from);
2e9b04
-    }
2e9b04
-#else
2e9b04
-  /* Avoid a full copy and use rename if we can fix up permissions of the
2e9b04
-     file after renaming, i.e.:
2e9b04
+    non_fatal (_("unable to copy file '%s'; reason: %s"),
2e9b04
+	       to, strerror (errno));
2e9b04
 
2e9b04
-     - TO is not a symbolic link
2e9b04
-     - TO is a regular file with only one hard link
2e9b04
-     - We have permission to write to TO
2e9b04
-     - FD is available to safely fix up permissions to be the same as the file
2e9b04
-       we overwrote with the rename.
2e9b04
-
2e9b04
-     Note though that the actual file on disk that TARGET_STAT describes may
2e9b04
-     have changed and we're only trying to preserve the status we know about.
2e9b04
-     At no point do we try to interact with the new file changes, so there can
2e9b04
-     only be two outcomes, i.e. either the external file change survives
2e9b04
-     without knowledge of our change (if it happens after the rename syscall)
2e9b04
-     or our rename and permissions fixup survive without any knowledge of the
2e9b04
-     external change.  */
2e9b04
-  if (! exists
2e9b04
-      || (fd >= 0
2e9b04
-	  && !S_ISLNK (to_stat.st_mode)
2e9b04
-	  && S_ISREG (to_stat.st_mode)
2e9b04
-	  && (to_stat.st_mode & S_IWUSR)
2e9b04
-	  && to_stat.st_nlink == 1)
2e9b04
-      )
2e9b04
-    {
2e9b04
-      ret = rename (from, to);
2e9b04
-      if (ret == 0)
2e9b04
-	{
2e9b04
-	  if (exists && target_stat != NULL)
2e9b04
-	    try_preserve_permissions (fd, target_stat);
2e9b04
-	}
2e9b04
-      else
2e9b04
-	{
2e9b04
-	  /* We have to clean up here.  */
2e9b04
-	  non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
2e9b04
-	  unlink (from);
2e9b04
-	}
2e9b04
-    }
2e9b04
-  else
2e9b04
-    {
2e9b04
-      ret = simple_copy (from, to);
2e9b04
-      if (ret != 0)
2e9b04
-	non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
2e9b04
-
2e9b04
-      if (preserve_dates && target_stat != NULL)
2e9b04
-	set_times (to, target_stat);
2e9b04
-      unlink (from);
2e9b04
-    }
2e9b04
-  if (fd >= 0)
2e9b04
-    close (fd);
2e9b04
-#endif /* _WIN32 && !__CYGWIN32__ */
2e9b04
+  if (preserve_dates)
2e9b04
+    set_times (to, target_stat);
2e9b04
+  unlink (from);
2e9b04
 
2e9b04
   return ret;
2e9b04
 }
2e9b04
diff -rup binutils.orig/binutils/objcopy.c binutils-2.35.1/binutils/objcopy.c
2e9b04
--- binutils.orig/binutils/objcopy.c	2021-03-11 13:21:44.780223078 +0000
2e9b04
+++ binutils-2.35.1/binutils/objcopy.c	2021-03-11 13:23:01.041718818 +0000
2e9b04
@@ -4798,7 +4798,11 @@ strip_main (int argc, char *argv[])
2e9b04
 
2e9b04
       if (output_file == NULL
2e9b04
 	  || filename_cmp (argv[i], output_file) == 0)
2e9b04
-	tmpname = make_tempname (argv[i], &tmpfd);
2e9b04
+	{
2e9b04
+	  tmpname = make_tempname (argv[i], &tmpfd);
2e9b04
+	  if (tmpfd >= 0)
2e9b04
+	    copyfd = dup (tmpfd);
2e9b04
+	}
2e9b04
       else
2e9b04
 	tmpname = output_file;
2e9b04