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