0b119dd
diff -rup binutils.orig/binutils/ar.c binutils-2.36.1/binutils/ar.c
0b119dd
--- binutils.orig/binutils/ar.c	2021-02-19 16:46:54.037875215 +0000
0b119dd
+++ binutils-2.36.1/binutils/ar.c	2021-02-19 16:54:24.412453329 +0000
0b119dd
@@ -25,7 +25,6 @@
0b119dd
 
0b119dd
 #include "sysdep.h"
0b119dd
 #include "bfd.h"
0b119dd
-#include "libbfd.h"
0b119dd
 #include "libiberty.h"
0b119dd
 #include "progress.h"
0b119dd
 #include "getopt.h"
0b119dd
@@ -1255,8 +1254,7 @@ write_archive (bfd *iarch)
0b119dd
   bfd *contents_head = iarch->archive_next;
0b119dd
   int ofd = -1;
0b119dd
 
0b119dd
-  old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
0b119dd
-  strcpy (old_name, bfd_get_filename (iarch));
0b119dd
+  old_name = xstrdup (bfd_get_filename (iarch));
0b119dd
   new_name = make_tempname (old_name, &ofd;;
0b119dd
 
0b119dd
   if (new_name == NULL)
0b119dd
@@ -1308,7 +1306,7 @@ write_archive (bfd *iarch)
0b119dd
   /* We don't care if this fails; we might be creating the archive.  */
0b119dd
   bfd_close (iarch);
0b119dd
 
0b119dd
-  if (smart_rename (new_name, old_name, 0) != 0)
0b119dd
+  if (smart_rename (new_name, old_name, NULL) != 0)
0b119dd
     xexit (1);
0b119dd
   free (old_name);
0b119dd
   free (new_name);
0b119dd
diff -rup binutils.orig/binutils/arsup.c binutils-2.36.1/binutils/arsup.c
0b119dd
--- binutils.orig/binutils/arsup.c	2021-02-19 16:46:54.043875196 +0000
0b119dd
+++ binutils-2.36.1/binutils/arsup.c	2021-02-19 16:53:30.988621989 +0000
0b119dd
@@ -42,6 +42,8 @@ extern int deterministic;
0b119dd
 
0b119dd
 static bfd *obfd;
0b119dd
 static char *real_name;
0b119dd
+static char *temp_name;
0b119dd
+static int real_ofd;
0b119dd
 static FILE *outfile;
0b119dd
 
0b119dd
 static void
0b119dd
@@ -149,27 +151,24 @@ maybequit (void)
0b119dd
 void
0b119dd
 ar_open (char *name, int t)
0b119dd
 {
0b119dd
-  char *tname;
0b119dd
-  const char *bname = lbasename (name);
0b119dd
-  real_name = name;
0b119dd
-
0b119dd
-  /* Prepend tmp- to the beginning, to avoid file-name clashes after
0b119dd
-     truncation on filesystems with limited namespaces (DOS).  */
0b119dd
-  if (asprintf (&tname, "%.*stmp-%s", (int) (bname - name), name, bname) == -1)
0b119dd
+  real_name = xstrdup (name);
0b119dd
+  temp_name = make_tempname (real_name, &real_ofd);
0b119dd
+
0b119dd
+  if (temp_name == NULL)
0b119dd
     {
0b119dd
-      fprintf (stderr, _("%s: Can't allocate memory for temp name (%s)\n"),
0b119dd
+      fprintf (stderr, _("%s: Can't open temporary file (%s)\n"),
0b119dd
 	       program_name, strerror(errno));
0b119dd
       maybequit ();
0b119dd
       return;
0b119dd
     }
0b119dd
 
0b119dd
-  obfd = bfd_openw (tname, NULL);
0b119dd
+  obfd = bfd_fdopenw (temp_name, NULL, real_ofd);
0b119dd
 
0b119dd
   if (!obfd)
0b119dd
     {
0b119dd
       fprintf (stderr,
0b119dd
 	       _("%s: Can't open output archive %s\n"),
0b119dd
-	       program_name,  tname);
0b119dd
+	       program_name, temp_name);
0b119dd
 
0b119dd
       maybequit ();
0b119dd
     }
0b119dd
@@ -344,16 +343,31 @@ ar_save (void)
0b119dd
     }
0b119dd
   else
0b119dd
     {
0b119dd
-      char *ofilename = xstrdup (bfd_get_filename (obfd));
0b119dd
+      struct stat target_stat;
0b119dd
 
0b119dd
       if (deterministic > 0)
0b119dd
         obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
0b119dd
 
0b119dd
       bfd_close (obfd);
0b119dd
 
0b119dd
-      smart_rename (ofilename, real_name, 0);
0b119dd
-      obfd = 0;
0b119dd
-      free (ofilename);
0b119dd
+      if (stat (real_name, &target_stat) != 0)
0b119dd
+	{
0b119dd
+	  /* The temp file created in ar_open has mode 0600 as per mkstemp.
0b119dd
+	     Create the real empty output file here so smart_rename will
0b119dd
+	     update the mode according to the process umask.  */
0b119dd
+	  obfd = bfd_openw (real_name, NULL);
0b119dd
+	  if (obfd != NULL)
0b119dd
+	    {
0b119dd
+	      bfd_set_format (obfd, bfd_archive);
0b119dd
+	      bfd_close (obfd);
0b119dd
+	    }
0b119dd
+	}
0b119dd
+
0b119dd
+      smart_rename (temp_name, real_name, NULL);
0b119dd
+      obfd = NULL;
0b119dd
+      free (temp_name);
0b119dd
+      free (real_name);
0b119dd
+      temp_name = real_name = NULL;
0b119dd
     }
0b119dd
 }
0b119dd
 
0b119dd
diff -rup binutils.orig/binutils/bucomm.c binutils-2.36.1/binutils/bucomm.c
0b119dd
--- binutils.orig/binutils/bucomm.c	2021-02-19 16:46:54.052875168 +0000
0b119dd
+++ binutils-2.36.1/binutils/bucomm.c	2021-02-19 16:56:01.837145730 +0000
0b119dd
@@ -623,6 +623,21 @@ get_file_size (const char * file_name)
0b119dd
   else if (statbuf.st_size < 0)
0b119dd
     non_fatal (_("Warning: '%s' has negative size, probably it is too large"),
0b119dd
                file_name);
0b119dd
+#if defined (_WIN32) && !defined (__CYGWIN__)
0b119dd
+  else if (statbuf.st_size == 0)
0b119dd
+    {
0b119dd
+      /* MS-Windows 'stat' reports the null device as a regular file;
0b119dd
+	 fix that.  */
0b119dd
+      int fd = open (file_name, O_RDONLY | O_BINARY);
0b119dd
+      if (isatty (fd))
0b119dd
+	{
0b119dd
+	  close (fd);
0b119dd
+	  non_fatal (_("Warning: '%s' is not an ordinary file"),
0b119dd
+		     /* libtool wants to see /dev/null in the output.  */
0b119dd
+		     strcasecmp (file_name, "nul") ? file_name : "/dev/null");
0b119dd
+	}
0b119dd
+    }
0b119dd
+#endif
0b119dd
   else
0b119dd
     return statbuf.st_size;
0b119dd
 
0b119dd
diff -rup binutils.orig/binutils/bucomm.h binutils-2.36.1/binutils/bucomm.h
0b119dd
--- binutils.orig/binutils/bucomm.h	2021-02-19 16:46:54.043875196 +0000
0b119dd
+++ binutils-2.36.1/binutils/bucomm.h	2021-02-19 16:55:22.653269446 +0000
0b119dd
@@ -71,7 +71,7 @@ extern void print_version (const char *)
0b119dd
 /* In rename.c.  */
0b119dd
 extern void set_times (const char *, const struct stat *);
0b119dd
 
0b119dd
-extern int smart_rename (const char *, const char *, int);
0b119dd
+extern int smart_rename (const char *, const char *, struct stat *);
0b119dd
 
0b119dd
 /* In libiberty.  */
0b119dd
 void *xmalloc (size_t);
0b119dd
diff -rup binutils.orig/binutils/objcopy.c binutils-2.36.1/binutils/objcopy.c
0b119dd
--- binutils.orig/binutils/objcopy.c	2021-02-19 16:46:54.052875168 +0000
0b119dd
+++ binutils-2.36.1/binutils/objcopy.c	2021-02-19 16:57:30.156866883 +0000
0b119dd
@@ -20,7 +20,6 @@
0b119dd
 
0b119dd
 #include "sysdep.h"
0b119dd
 #include "bfd.h"
0b119dd
-#include "libbfd.h"
0b119dd
 #include "progress.h"
0b119dd
 #include "getopt.h"
0b119dd
 #include "libiberty.h"
0b119dd
@@ -2798,8 +2797,7 @@ copy_object (bfd *ibfd, bfd *obfd, const
0b119dd
 	pe->timestamp = pe_data (ibfd)->coff.timestamp;
0b119dd
     }
0b119dd
 
0b119dd
-  if (isympp)
0b119dd
-    free (isympp);
0b119dd
+  free (isympp);
0b119dd
 
0b119dd
   if (osympp != isympp)
0b119dd
     free (osympp);
0b119dd
@@ -4617,8 +4615,7 @@ mark_symbols_used_in_relocations (bfd *i
0b119dd
 	(*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
0b119dd
     }
0b119dd
 
0b119dd
-  if (relpp != NULL)
0b119dd
-    free (relpp);
0b119dd
+  free (relpp);
0b119dd
 }
0b119dd
 
0b119dd
 /* Write out debugging information.  */
0b119dd
@@ -4866,12 +4863,10 @@ strip_main (int argc, char *argv[])
0b119dd
 		 output_target, NULL);
0b119dd
       if (status == 0)
0b119dd
 	{
0b119dd
-	  if (preserve_dates)
0b119dd
-	    set_times (tmpname, &statbuf);
0b119dd
 	  if (output_file != tmpname)
0b119dd
 	    status = (smart_rename (tmpname,
0b119dd
 				    output_file ? output_file : argv[i],
0b119dd
-				    preserve_dates) != 0);
0b119dd
+				    preserve_dates ? &statbuf : NULL) != 0);
0b119dd
 	  if (status == 0)
0b119dd
 	    status = hold_status;
0b119dd
 	}
0b119dd
@@ -5936,11 +5931,9 @@ copy_main (int argc, char *argv[])
0b119dd
 	     output_target, input_arch);
0b119dd
   if (status == 0)
0b119dd
     {
0b119dd
-      if (preserve_dates)
0b119dd
-	set_times (tmpname, &statbuf);
0b119dd
       if (tmpname != output_filename)
0b119dd
 	status = (smart_rename (tmpname, input_filename,
0b119dd
-				preserve_dates) != 0);
0b119dd
+				preserve_dates ? &statbuf : NULL) != 0);
0b119dd
     }
0b119dd
   else
0b119dd
     unlink_if_ordinary (tmpname);
0b119dd
@@ -5987,26 +5980,13 @@ copy_main (int argc, char *argv[])
0b119dd
 	}
0b119dd
     }
0b119dd
 
0b119dd
-  if (strip_specific_buffer)
0b119dd
-    free (strip_specific_buffer);
0b119dd
-
0b119dd
-  if (strip_unneeded_buffer)
0b119dd
-    free (strip_unneeded_buffer);
0b119dd
-
0b119dd
-  if (keep_specific_buffer)
0b119dd
-    free (keep_specific_buffer);
0b119dd
-
0b119dd
-  if (localize_specific_buffer)
0b119dd
-    free (localize_specific_buffer);
0b119dd
-
0b119dd
-  if (globalize_specific_buffer)
0b119dd
-    free (globalize_specific_buffer);
0b119dd
-
0b119dd
-  if (keepglobal_specific_buffer)
0b119dd
-    free (keepglobal_specific_buffer);
0b119dd
-
0b119dd
-  if (weaken_specific_buffer)
0b119dd
-    free (weaken_specific_buffer);
0b119dd
+  free (strip_specific_buffer);
0b119dd
+  free (strip_unneeded_buffer);
0b119dd
+  free (keep_specific_buffer);
0b119dd
+  free (localize_specific_buffer);
0b119dd
+  free (globalize_specific_buffer);
0b119dd
+  free (keepglobal_specific_buffer);
0b119dd
+  free (weaken_specific_buffer);
0b119dd
 
0b119dd
   return 0;
0b119dd
 }
0b119dd
diff -rup binutils.orig/binutils/rename.c binutils-2.36.1/binutils/rename.c
0b119dd
--- binutils.orig/binutils/rename.c	2021-02-19 16:46:54.052875168 +0000
0b119dd
+++ binutils-2.36.1/binutils/rename.c	2021-02-19 16:58:27.771684984 +0000
0b119dd
@@ -122,26 +122,19 @@ set_times (const char *destination, cons
0b119dd
     non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
0b119dd
 }
0b119dd
 
0b119dd
-#ifndef S_ISLNK
0b119dd
-#ifdef S_IFLNK
0b119dd
-#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
0b119dd
-#else
0b119dd
-#define S_ISLNK(m) 0
0b119dd
-#define lstat stat
0b119dd
-#endif
0b119dd
-#endif
0b119dd
-
0b119dd
-/* Rename FROM to TO, copying if TO is a link.
0b119dd
-   Return 0 if ok, -1 if error.  */
0b119dd
+/* Rename FROM to TO, copying if TO exists.  TARGET_STAT has the file status
0b119dd
+   that, if non-NULL, is used to fix up timestamps after rename.  Return 0 if
0b119dd
+   ok, -1 if error.  */
0b119dd
 
0b119dd
 int
0b119dd
-smart_rename (const char *from, const char *to, int preserve_dates ATTRIBUTE_UNUSED)
0b119dd
+smart_rename (const char *from, const char *to,
0b119dd
+	      struct stat *target_stat ATTRIBUTE_UNUSED)
0b119dd
 {
0b119dd
-  bfd_boolean exists;
0b119dd
-  struct stat s;
0b119dd
   int ret = 0;
0b119dd
+  struct stat to_stat;
0b119dd
+  bfd_boolean exists;
0b119dd
 
0b119dd
-  exists = lstat (to, &s) == 0;
0b119dd
+  exists = lstat (to, &to_stat) == 0;
0b119dd
 
0b119dd
 #if defined (_WIN32) && !defined (__CYGWIN32__)
0b119dd
   /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
0b119dd
@@ -158,38 +151,10 @@ smart_rename (const char *from, const ch
0b119dd
       unlink (from);
0b119dd
     }
0b119dd
 #else
0b119dd
-  /* Use rename only if TO is not a symbolic link and has
0b119dd
-     only one hard link, and we have permission to write to it.  */
0b119dd
-  if (! exists
0b119dd
-      || (!S_ISLNK (s.st_mode)
0b119dd
-	  && S_ISREG (s.st_mode)
0b119dd
-	  && (s.st_mode & S_IWUSR)
0b119dd
-	  && s.st_nlink == 1)
0b119dd
-      )
0b119dd
+  /* Avoid a full copy and use rename if TO does not exist.  */
0b119dd
+  if (!exists)
0b119dd
     {
0b119dd
-      ret = rename (from, to);
0b119dd
-      if (ret == 0)
0b119dd
-	{
0b119dd
-	  if (exists)
0b119dd
-	    {
0b119dd
-	      /* Try to preserve the permission bits and ownership of
0b119dd
-		 TO.  First get the mode right except for the setuid
0b119dd
-		 bit.  Then change the ownership.  Then fix the setuid
0b119dd
-		 bit.  We do the chmod before the chown because if the
0b119dd
-		 chown succeeds, and we are a normal user, we won't be
0b119dd
-		 able to do the chmod afterward.  We don't bother to
0b119dd
-		 fix the setuid bit first because that might introduce
0b119dd
-		 a fleeting security problem, and because the chown
0b119dd
-		 will clear the setuid bit anyhow.  We only fix the
0b119dd
-		 setuid bit if the chown succeeds, because we don't
0b119dd
-		 want to introduce an unexpected setuid file owned by
0b119dd
-		 the user running objcopy.  */
0b119dd
-	      chmod (to, s.st_mode & 0777);
0b119dd
-	      if (chown (to, s.st_uid, s.st_gid) >= 0)
0b119dd
-		chmod (to, s.st_mode & 07777);
0b119dd
-	    }
0b119dd
-	}
0b119dd
-      else
0b119dd
+      if ((ret = rename (from, to)) != 0)
0b119dd
 	{
0b119dd
 	  /* We have to clean up here.  */
0b119dd
 	  non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
0b119dd
@@ -202,8 +167,8 @@ smart_rename (const char *from, const ch
0b119dd
       if (ret != 0)
0b119dd
 	non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
0b119dd
 
0b119dd
-      if (preserve_dates)
0b119dd
-	set_times (to, &s);
0b119dd
+      if (target_stat != NULL)
0b119dd
+	set_times (to, target_stat);
0b119dd
       unlink (from);
0b119dd
     }
0b119dd
 #endif /* _WIN32 && !__CYGWIN32__ */
ac55f50
diff -rup binutils.orig/binutils/ar.c binutils-2.36.1/binutils/ar.c
ac55f50
--- binutils.orig/binutils/ar.c	2021-03-11 12:57:40.206766885 +0000
ac55f50
+++ binutils-2.36.1/binutils/ar.c	2021-03-11 12:59:42.874957119 +0000
ac55f50
@@ -1252,21 +1252,21 @@ write_archive (bfd *iarch)
ac55f50
   bfd *obfd;
ac55f50
   char *old_name, *new_name;
ac55f50
   bfd *contents_head = iarch->archive_next;
ac55f50
-  int ofd = -1;
ac55f50
+  int tmpfd = -1;
ac55f50
 
ac55f50
   old_name = xstrdup (bfd_get_filename (iarch));
ac55f50
-  new_name = make_tempname (old_name, &ofd;;
ac55f50
+  new_name = make_tempname (old_name, &tmpfd);
ac55f50
 
ac55f50
   if (new_name == NULL)
ac55f50
     bfd_fatal (_("could not create temporary file whilst writing archive"));
ac55f50
 
ac55f50
   output_filename = new_name;
ac55f50
 
ac55f50
-  obfd = bfd_fdopenw (new_name, bfd_get_target (iarch), ofd);
ac55f50
+  obfd = bfd_fdopenw (new_name, bfd_get_target (iarch), tmpfd);
ac55f50
 
ac55f50
   if (obfd == NULL)
ac55f50
     {
ac55f50
-      close (ofd);
ac55f50
+      close (tmpfd);
ac55f50
       bfd_fatal (old_name);
ac55f50
     }
ac55f50
 
ac55f50
@@ -1297,6 +1297,7 @@ write_archive (bfd *iarch)
ac55f50
   if (!bfd_set_archive_head (obfd, contents_head))
ac55f50
     bfd_fatal (old_name);
ac55f50
 
ac55f50
+  tmpfd = dup (tmpfd);
ac55f50
   if (!bfd_close (obfd))
ac55f50
     bfd_fatal (old_name);
ac55f50
 
ac55f50
@@ -1306,7 +1307,7 @@ write_archive (bfd *iarch)
ac55f50
   /* We don't care if this fails; we might be creating the archive.  */
ac55f50
   bfd_close (iarch);
ac55f50
 
ac55f50
-  if (smart_rename (new_name, old_name, NULL) != 0)
ac55f50
+  if (smart_rename (new_name, old_name, tmpfd, NULL, FALSE) != 0)
ac55f50
     xexit (1);
ac55f50
   free (old_name);
ac55f50
   free (new_name);
ac55f50
diff -rup binutils.orig/binutils/arsup.c binutils-2.36.1/binutils/arsup.c
ac55f50
--- binutils.orig/binutils/arsup.c	2021-03-11 12:57:40.190766990 +0000
ac55f50
+++ binutils-2.36.1/binutils/arsup.c	2021-03-11 13:00:35.897607109 +0000
ac55f50
@@ -43,7 +43,7 @@ extern int deterministic;
ac55f50
 static bfd *obfd;
ac55f50
 static char *real_name;
ac55f50
 static char *temp_name;
ac55f50
-static int real_ofd;
ac55f50
+static int temp_fd;
ac55f50
 static FILE *outfile;
ac55f50
 
ac55f50
 static void
ac55f50
@@ -152,7 +152,7 @@ void
ac55f50
 ar_open (char *name, int t)
ac55f50
 {
ac55f50
   real_name = xstrdup (name);
ac55f50
-  temp_name = make_tempname (real_name, &real_ofd);
ac55f50
+  temp_name = make_tempname (real_name, &temp_fd);
ac55f50
 
ac55f50
   if (temp_name == NULL)
ac55f50
     {
ac55f50
@@ -162,7 +162,7 @@ ar_open (char *name, int t)
ac55f50
       return;
ac55f50
     }
ac55f50
 
ac55f50
-  obfd = bfd_fdopenw (temp_name, NULL, real_ofd);
ac55f50
+  obfd = bfd_fdopenw (temp_name, NULL, temp_fd);
ac55f50
 
ac55f50
   if (!obfd)
ac55f50
     {
ac55f50
@@ -348,6 +348,7 @@ ar_save (void)
ac55f50
       if (deterministic > 0)
ac55f50
         obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
ac55f50
 
ac55f50
+      temp_fd = dup (temp_fd);
ac55f50
       bfd_close (obfd);
ac55f50
 
ac55f50
       if (stat (real_name, &target_stat) != 0)
ac55f50
@@ -363,11 +364,10 @@ ar_save (void)
ac55f50
 	    }
ac55f50
 	}
ac55f50
 
ac55f50
-      smart_rename (temp_name, real_name, NULL);
ac55f50
+      smart_rename (temp_name, real_name, temp_fd, NULL, FALSE);
ac55f50
       obfd = NULL;
ac55f50
       free (temp_name);
ac55f50
       free (real_name);
ac55f50
-      temp_name = real_name = NULL;
ac55f50
     }
ac55f50
 }
ac55f50
 
ac55f50
diff -rup binutils.orig/binutils/bucomm.h binutils-2.36.1/binutils/bucomm.h
ac55f50
--- binutils.orig/binutils/bucomm.h	2021-03-11 12:57:40.205766891 +0000
ac55f50
+++ binutils-2.36.1/binutils/bucomm.h	2021-03-11 12:59:03.082219804 +0000
ac55f50
@@ -71,7 +71,8 @@ extern void print_version (const char *)
ac55f50
 /* In rename.c.  */
ac55f50
 extern void set_times (const char *, const struct stat *);
ac55f50
 
ac55f50
-extern int smart_rename (const char *, const char *, struct stat *);
ac55f50
+extern int smart_rename (const char *, const char *, int,
ac55f50
+			 struct stat *, bfd_boolean);
ac55f50
 
ac55f50
 /* In libiberty.  */
ac55f50
 void *xmalloc (size_t);
ac55f50
diff -rup binutils.orig/binutils/objcopy.c binutils-2.36.1/binutils/objcopy.c
ac55f50
--- binutils.orig/binutils/objcopy.c	2021-03-11 12:57:40.196766951 +0000
ac55f50
+++ binutils-2.36.1/binutils/objcopy.c	2021-03-11 13:02:43.321765939 +0000
ac55f50
@@ -4822,6 +4822,7 @@ strip_main (int argc, char *argv[])
ac55f50
       struct stat statbuf;
ac55f50
       char *tmpname;
ac55f50
       int tmpfd = -1;
ac55f50
+      int copyfd = -1;
ac55f50
 
ac55f50
       if (get_file_size (argv[i]) < 1)
ac55f50
 	{
ac55f50
@@ -4831,7 +4832,11 @@ strip_main (int argc, char *argv[])
ac55f50
 
ac55f50
       if (output_file == NULL
ac55f50
 	  || filename_cmp (argv[i], output_file) == 0)
ac55f50
-	tmpname = make_tempname (argv[i], &tmpfd);
ac55f50
+	{
ac55f50
+	  tmpname = make_tempname (argv[i], &tmpfd);
ac55f50
+	  if (tmpfd >= 0)
ac55f50
+	    copyfd = dup (tmpfd);
ac55f50
+	}
ac55f50
       else
ac55f50
 	tmpname = output_file;
ac55f50
 
ac55f50
@@ -4849,14 +4854,18 @@ strip_main (int argc, char *argv[])
ac55f50
       if (status == 0)
ac55f50
 	{
ac55f50
 	  if (output_file != tmpname)
ac55f50
-	    status = (smart_rename (tmpname,
ac55f50
-				    output_file ? output_file : argv[i],
ac55f50
-				    preserve_dates ? &statbuf : NULL) != 0);
ac55f50
+	    status = smart_rename (tmpname,
ac55f50
+				   output_file ? output_file : argv[i],
ac55f50
+				   copyfd, &statbuf, preserve_dates) != 0;
ac55f50
 	  if (status == 0)
ac55f50
 	    status = hold_status;
ac55f50
 	}
ac55f50
       else
ac55f50
-	unlink_if_ordinary (tmpname);
ac55f50
+	{
ac55f50
+	  if (copyfd >= 0)
ac55f50
+	    close (copyfd);
ac55f50
+	  unlink_if_ordinary (tmpname);
ac55f50
+	}
ac55f50
       if (output_file != tmpname)
ac55f50
 	free (tmpname);
ac55f50
     }
ac55f50
@@ -5063,7 +5072,9 @@ copy_main (int argc, char *argv[])
ac55f50
   bfd_boolean formats_info = FALSE;
ac55f50
   bfd_boolean use_globalize = FALSE;
ac55f50
   bfd_boolean use_keep_global = FALSE;
ac55f50
-  int c, tmpfd = -1;
ac55f50
+  int c;
ac55f50
+  int tmpfd = -1;
ac55f50
+  int copyfd;
ac55f50
   struct stat statbuf;
ac55f50
   const bfd_arch_info_type *input_arch = NULL;
ac55f50
 
ac55f50
@@ -5901,27 +5912,38 @@ copy_main (int argc, char *argv[])
ac55f50
     }
ac55f50
 
ac55f50
   /* If there is no destination file, or the source and destination files
ac55f50
-     are the same, then create a temp and rename the result into the input.  */
ac55f50
+     are the same, then create a temp and copy the result into the input.  */
ac55f50
+  copyfd = -1;
ac55f50
   if (output_filename == NULL
ac55f50
       || filename_cmp (input_filename, output_filename) == 0)
ac55f50
-    tmpname = make_tempname (input_filename, &tmpfd);
ac55f50
+    {
ac55f50
+      tmpname = make_tempname (input_filename, &tmpfd);
ac55f50
+      if (tmpfd >= 0)
ac55f50
+	copyfd = dup (tmpfd);
ac55f50
+    }
ac55f50
   else
ac55f50
     tmpname = output_filename;
ac55f50
 
ac55f50
   if (tmpname == NULL)
ac55f50
-    fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
ac55f50
-	   input_filename, strerror (errno));
ac55f50
+    {
ac55f50
+      fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
ac55f50
+	     input_filename, strerror (errno));
ac55f50
+    }
ac55f50
 
ac55f50
   copy_file (input_filename, tmpname, tmpfd, &statbuf, input_target,
ac55f50
 	     output_target, input_arch);
ac55f50
   if (status == 0)
ac55f50
     {
ac55f50
       if (tmpname != output_filename)
ac55f50
-	status = (smart_rename (tmpname, input_filename,
ac55f50
-				preserve_dates ? &statbuf : NULL) != 0);
ac55f50
+	status = smart_rename (tmpname, input_filename, copyfd,
ac55f50
+			       &statbuf, preserve_dates) != 0;
ac55f50
     }
ac55f50
   else
ac55f50
-    unlink_if_ordinary (tmpname);
ac55f50
+    {
ac55f50
+      if (copyfd >= 0)
ac55f50
+	close (copyfd);
ac55f50
+      unlink_if_ordinary (tmpname);
ac55f50
+    }
ac55f50
 
ac55f50
   if (tmpname != output_filename)
ac55f50
     free (tmpname);
ac55f50
diff -rup binutils.orig/binutils/rename.c binutils-2.36.1/binutils/rename.c
ac55f50
--- binutils.orig/binutils/rename.c	2021-03-11 12:57:40.206766885 +0000
ac55f50
+++ binutils-2.36.1/binutils/rename.c	2021-03-11 12:58:47.306323943 +0000
ac55f50
@@ -24,36 +24,29 @@
ac55f50
 
ac55f50
 #ifdef HAVE_GOOD_UTIME_H
ac55f50
 #include <utime.h>
ac55f50
-#else /* ! HAVE_GOOD_UTIME_H */
ac55f50
-#ifdef HAVE_UTIMES
ac55f50
+#elif defined HAVE_UTIMES
ac55f50
 #include <sys/time.h>
ac55f50
-#endif /* HAVE_UTIMES */
ac55f50
-#endif /* ! HAVE_GOOD_UTIME_H */
ac55f50
-
ac55f50
-#if ! defined (_WIN32) || defined (__CYGWIN32__)
ac55f50
-static int simple_copy (const char *, const char *);
ac55f50
+#endif
ac55f50
 
ac55f50
 /* The number of bytes to copy at once.  */
ac55f50
 #define COPY_BUF 8192
ac55f50
 
ac55f50
-/* Copy file FROM to file TO, performing no translations.
ac55f50
+/* Copy file FROMFD to file TO, performing no translations.
ac55f50
    Return 0 if ok, -1 if error.  */
ac55f50
 
ac55f50
 static int
ac55f50
-simple_copy (const char *from, const char *to)
ac55f50
+simple_copy (int fromfd, const char *to,
ac55f50
+	     struct stat *target_stat ATTRIBUTE_UNUSED)
ac55f50
 {
ac55f50
-  int fromfd, tofd, nread;
ac55f50
+  int tofd, nread;
ac55f50
   int saved;
ac55f50
   char buf[COPY_BUF];
ac55f50
 
ac55f50
-  fromfd = open (from, O_RDONLY | O_BINARY);
ac55f50
-  if (fromfd < 0)
ac55f50
+  if (fromfd < 0
ac55f50
+      || lseek (fromfd, 0, SEEK_SET) != 0)
ac55f50
     return -1;
ac55f50
-#ifdef O_CREAT
ac55f50
-  tofd = open (to, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0777);
ac55f50
-#else
ac55f50
-  tofd = creat (to, 0777);
ac55f50
-#endif
ac55f50
+
ac55f50
+  tofd = open (to, O_WRONLY | O_TRUNC | O_BINARY);
ac55f50
   if (tofd < 0)
ac55f50
     {
ac55f50
       saved = errno;
ac55f50
@@ -61,6 +54,7 @@ simple_copy (const char *from, const cha
ac55f50
       errno = saved;
ac55f50
       return -1;
ac55f50
     }
ac55f50
+
ac55f50
   while ((nread = read (fromfd, buf, sizeof buf)) > 0)
ac55f50
     {
ac55f50
       if (write (tofd, buf, nread) != nread)
ac55f50
@@ -72,7 +66,16 @@ simple_copy (const char *from, const cha
ac55f50
 	  return -1;
ac55f50
 	}
ac55f50
     }
ac55f50
+
ac55f50
   saved = errno;
ac55f50
+
ac55f50
+#if !defined (_WIN32) || defined (__CYGWIN32__)
ac55f50
+  /* Writing to a setuid/setgid file may clear S_ISUID and S_ISGID.
ac55f50
+     Try to restore them, ignoring failure.  */
ac55f50
+  if (target_stat != NULL)
ac55f50
+    fchmod (tofd, target_stat->st_mode);
ac55f50
+#endif
ac55f50
+
ac55f50
   close (fromfd);
ac55f50
   close (tofd);
ac55f50
   if (nread < 0)
ac55f50
@@ -82,7 +85,6 @@ simple_copy (const char *from, const cha
ac55f50
     }
ac55f50
   return 0;
ac55f50
 }
ac55f50
-#endif /* __CYGWIN32__ or not _WIN32 */
ac55f50
 
ac55f50
 /* Set the times of the file DESTINATION to be the same as those in
ac55f50
    STATBUF.  */
ac55f50
@@ -91,87 +93,52 @@ void
ac55f50
 set_times (const char *destination, const struct stat *statbuf)
ac55f50
 {
ac55f50
   int result;
ac55f50
-
ac55f50
-  {
ac55f50
 #ifdef HAVE_GOOD_UTIME_H
ac55f50
-    struct utimbuf tb;
ac55f50
+  struct utimbuf tb;
ac55f50
 
ac55f50
-    tb.actime = statbuf->st_atime;
ac55f50
-    tb.modtime = statbuf->st_mtime;
ac55f50
-    result = utime (destination, &tb);
ac55f50
-#else /* ! HAVE_GOOD_UTIME_H */
ac55f50
-#ifndef HAVE_UTIMES
ac55f50
-    long tb[2];
ac55f50
-
ac55f50
-    tb[0] = statbuf->st_atime;
ac55f50
-    tb[1] = statbuf->st_mtime;
ac55f50
-    result = utime (destination, tb);
ac55f50
-#else /* HAVE_UTIMES */
ac55f50
-    struct timeval tv[2];
ac55f50
-
ac55f50
-    tv[0].tv_sec = statbuf->st_atime;
ac55f50
-    tv[0].tv_usec = 0;
ac55f50
-    tv[1].tv_sec = statbuf->st_mtime;
ac55f50
-    tv[1].tv_usec = 0;
ac55f50
-    result = utimes (destination, tv);
ac55f50
-#endif /* HAVE_UTIMES */
ac55f50
-#endif /* ! HAVE_GOOD_UTIME_H */
ac55f50
-  }
ac55f50
+  tb.actime = statbuf->st_atime;
ac55f50
+  tb.modtime = statbuf->st_mtime;
ac55f50
+  result = utime (destination, &tb);
ac55f50
+#elif defined HAVE_UTIMES
ac55f50
+  struct timeval tv[2];
ac55f50
+
ac55f50
+  tv[0].tv_sec = statbuf->st_atime;
ac55f50
+  tv[0].tv_usec = 0;
ac55f50
+  tv[1].tv_sec = statbuf->st_mtime;
ac55f50
+  tv[1].tv_usec = 0;
ac55f50
+  result = utimes (destination, tv);
ac55f50
+#else
ac55f50
+  long tb[2];
ac55f50
+
ac55f50
+  tb[0] = statbuf->st_atime;
ac55f50
+  tb[1] = statbuf->st_mtime;
ac55f50
+  result = utime (destination, tb);
ac55f50
+#endif
ac55f50
 
ac55f50
   if (result != 0)
ac55f50
     non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
ac55f50
 }
ac55f50
 
ac55f50
-/* Rename FROM to TO, copying if TO exists.  TARGET_STAT has the file status
ac55f50
-   that, if non-NULL, is used to fix up timestamps after rename.  Return 0 if
ac55f50
-   ok, -1 if error.  */
ac55f50
+/* Copy FROM to TO.  TARGET_STAT has the file status that, if non-NULL,
ac55f50
+   is used to fix up timestamps.  Return 0 if ok, -1 if error.
ac55f50
+   At one time this function renamed files, but file permissions are
ac55f50
+   tricky to update given the number of different schemes used by
ac55f50
+   various systems.  So now we just copy.  */
ac55f50
 
ac55f50
 int
ac55f50
-smart_rename (const char *from, const char *to,
ac55f50
-	      struct stat *target_stat ATTRIBUTE_UNUSED)
ac55f50
+smart_rename (const char *from, const char *to, int fromfd,
ac55f50
+	      struct stat *target_stat, bfd_boolean preserve_dates)
ac55f50
 {
ac55f50
-  int ret = 0;
ac55f50
-  struct stat to_stat;
ac55f50
-  bfd_boolean exists;
ac55f50
+  int ret;
ac55f50
 
ac55f50
-  exists = lstat (to, &to_stat) == 0;
ac55f50
-
ac55f50
-#if defined (_WIN32) && !defined (__CYGWIN32__)
ac55f50
-  /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
ac55f50
-     fail instead.  Also, chown is not present.  */
ac55f50
-
ac55f50
-  if (exists)
ac55f50
-    remove (to);
ac55f50
-
ac55f50
-  ret = rename (from, to);
ac55f50
+  ret = simple_copy (fromfd, to, target_stat);
ac55f50
   if (ret != 0)
ac55f50
-    {
ac55f50
-      /* We have to clean up here.  */
ac55f50
-      non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
ac55f50
-      unlink (from);
ac55f50
-    }
ac55f50
-#else
ac55f50
-  /* Avoid a full copy and use rename if TO does not exist.  */
ac55f50
-  if (!exists)
ac55f50
-    {
ac55f50
-      if ((ret = rename (from, to)) != 0)
ac55f50
-	{
ac55f50
-	  /* We have to clean up here.  */
ac55f50
-	  non_fatal (_("unable to rename '%s'; reason: %s"), to, strerror (errno));
ac55f50
-	  unlink (from);
ac55f50
-	}
ac55f50
-    }
ac55f50
-  else
ac55f50
-    {
ac55f50
-      ret = simple_copy (from, to);
ac55f50
-      if (ret != 0)
ac55f50
-	non_fatal (_("unable to copy file '%s'; reason: %s"), to, strerror (errno));
ac55f50
-
ac55f50
-      if (target_stat != NULL)
ac55f50
-	set_times (to, target_stat);
ac55f50
-      unlink (from);
ac55f50
-    }
ac55f50
-#endif /* _WIN32 && !__CYGWIN32__ */
ac55f50
+    non_fatal (_("unable to copy file '%s'; reason: %s"),
ac55f50
+	       to, strerror (errno));
ac55f50
+
ac55f50
+  if (preserve_dates)
ac55f50
+    set_times (to, target_stat);
ac55f50
+  unlink (from);
ac55f50
 
ac55f50
   return ret;
ac55f50
 }