#! /bin/sh -e

# DP: Backport of PR17684 to the gcc-3.3 branch

dir=
if [ $# -eq 3 -a "$2" = '-d' ]; then
    pdir="-d $3"
    dir="$3/"
elif [ $# -ne 1 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch)
        patch $pdir -f --no-backup-if-mismatch -p0 < $0
        ;;
    -unpatch)
        patch $pdir -f --no-backup-if-mismatch -R -p0 < $0
        ;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1
esac
exit 0

2004-09-28  H.J. Lu  <hongjiu.lu@intel.com>

	PR bootstrap/17684
	* Makefile.in (clean): Remove libgcc_s$(SHLIB_EXT).1.stage?.
	(stage1-start): Remove and copy libunwind.a and
	libunwind*$(SHLIB_EXT) instead of libunwind*.
	(stage2-start): Likewise.
	(stage3-start): Likewise.
	(stage4-start): Likewise.
	(stageprofile-start): Likewise.
	(stagefeedback-start): Likewise.

	* config/alpha/t-osf4 (SHLIB_LINK): Use a temporary file for
	the shared library to be created and don't remove the existing
	shared library.
	* config/arm/t-netbsd (SHLIB_LINK): Likewise.
	* config/mips/t-iris5-6 (SHLIB_LINK): Likewise.
	* config/pa/t-hpux-shlib (SHLIB_LINK): Likewise.
	* config/sh/t-linux (SHLIB_LINK): Likewise.
	* config/t-libunwind-elf (SHLIBUNWIND_LINK): Likewise.
	* config/t-slibgcc-darwin (SHLIB_LINK): Likewise.
	* config/t-slibgcc-elf-ver (SHLIB_LINK): Likewise.
	* config/t-slibgcc-sld (SHLIB_LINK): Likewise.

	* mklibgcc.in (libgcc-stage-start): Also move "*${objext}s"
	files.

diff -urN gcc.old/Makefile.in gcc/Makefile.in
--- gcc.old/Makefile.in	2004-12-10 13:28:49.000000000 +0100
+++ gcc/Makefile.in	2004-12-10 14:36:38.000000000 +0100
@@ -2678,8 +2678,9 @@
 # that don't exist in the distribution.
 INTL_CLEAN = intl.clean
 clean: mostlyclean $(INTL_CLEAN) lang.clean
-	-rm -f libgcc.a libgcc_eh.a libgcc_s$(SHLIB_EXT) libgcc_s$(SHLIB_EXT).1
+	-rm -f libgcc.a libgcc_eh.a
 	-rm -f libunwind*
+	-rm -f libgcc_s*
 	-rm -f config.h tconfig.h hconfig.h tm_p.h
 	-rm -f cs-*
 	-rm -rf libgcc
@@ -3576,7 +3577,7 @@
 	-if [ -f ld$(exeext) ] ; then (cd stage1 && $(LN_S) ../ld$(exeext) .) ; else true ; fi
 	-if [ -f collect-ld$(exeext) ] ; then (cd stage1 && $(LN_S) ../collect-ld$(exeext) .) ; else true ; fi
 	-rm -f stage1/libgcc.a stage1/libgcc_eh.a stage1/libgcc_s*$(SHLIB_EXT)
-	-rm -f stage1/libunwind*
+	-rm -f stage1/libunwind.a stage1/libunwind*$(SHLIB_EXT)
 	-cp libgcc.a stage1
 	-if $(RANLIB_TEST_FOR_TARGET) ; then \
 	  $(RANLIB_FOR_TARGET) stage1/libgcc.a; \
@@ -3586,7 +3587,7 @@
 	   $(RANLIB_FOR_TARGET) stage1/libgcc_eh.a; \
 	  else true; fi; fi
 	-cp libgcc_s*$(SHLIB_EXT) stage1/
-	-cp libunwind* stage1/
+	-cp libunwind.a libunwind*$(SHLIB_EXT) stage1
 	-for f in .. $(EXTRA_MULTILIB_PARTS); do if [ x$${f} != x.. ]; then \
 	  cp stage1/$${f} . ; \
 	else true; \
@@ -3608,7 +3609,7 @@
 	-if [ -f ld$(exeext) ] ; then (cd stage2 && $(LN_S) ../ld$(exeext) .) ; else true ; fi
 	-if [ -f collect-ld$(exeext) ] ; then (cd stage2 && $(LN_S) ../collect-ld$(exeext) .) ; else true ; fi
 	-rm -f stage2/libgcc.a stage2/libgcc_eh.a stage2/libgcc_s*$(SHLIB_EXT)
-	-rm -f stage2/libunwind*
+	-rm -f stage2/libunwind.a stage2/libunwind*$(SHLIB_EXT)
 	-cp libgcc.a stage2
 	-if $(RANLIB_TEST_FOR_TARGET) ; then \
 	  $(RANLIB_FOR_TARGET) stage2/libgcc.a; \
@@ -3618,7 +3619,7 @@
 	   $(RANLIB_FOR_TARGET) stage2/libgcc_eh.a; \
 	  else true; fi; fi
 	-cp libgcc_s*$(SHLIB_EXT) stage2/
-	-cp libunwind* stage2/
+	-cp libunwind.a libunwind*$(SHLIB_EXT) stage2
 	-for f in .. $(EXTRA_MULTILIB_PARTS); do if [ x$${f} != x.. ]; then \
 	  cp stage2/$${f} . ; \
 	else true; \
@@ -3640,7 +3641,7 @@
 	-if [ -f ld$(exeext) ] ; then (cd stage3 && $(LN_S) ../ld$(exeext) .) ; else true ; fi
 	-if [ -f collect-ld$(exeext) ] ; then (cd stage3 && $(LN_S) ../collect-ld$(exeext) .) ; else true ; fi
 	-rm -f stage3/libgcc.a stage3/libgcc_eh.a stage3/libgcc_s*$(SHLIB_EXT)
-	-rm -f stage3/libunwind*
+	-rm -f stage3/libunwind.a stage3/libunwind*$(SHLIB_EXT)
 	-cp libgcc.a stage3
 	-if $(RANLIB_TEST_FOR_TARGET) ; then \
 	  $(RANLIB_FOR_TARGET) stage3/libgcc.a; \
@@ -3650,7 +3651,7 @@
 	   $(RANLIB_FOR_TARGET) stage3/libgcc_eh.a; \
 	  else true; fi; fi
 	-cp libgcc_s*$(SHLIB_EXT) stage3/
-	-cp libunwind* stage3/
+	-cp libunwind.a libunwind*$(SHLIB_EXT) stage3
 	-for f in .. $(EXTRA_MULTILIB_PARTS); do if [ x$${f} != x.. ]; then \
 	  cp stage3/$${f} . ; \
 	else true; \
@@ -3672,7 +3673,7 @@
 	-if [ -f ld$(exeext) ] ; then (cd stage4 && $(LN_S) ../ld$(exeext) .) ; else true ; fi
 	-if [ -f collect-ld$(exeext) ] ; then (cd stage4 && $(LN_S) ../collect-ld$(exeext) .) ; else true ; fi
 	-rm -f stage4/libgcc.a stage4/libgcc_eh.a stage4/libgcc_s*$(SHLIB_EXT)
-	-rm -f stage4/libunwind*
+	-rm -f stage4/libunwind.a stage4/libunwind*$(SHLIB_EXT)
 	-cp libgcc.a stage4
 	-if $(RANLIB_TEST_FOR_TARGET) ; then \
 	  $(RANLIB_FOR_TARGET) stage4/libgcc.a; \
@@ -3682,7 +3683,7 @@
 	   $(RANLIB_FOR_TARGET) stage4/libgcc_eh.a; \
 	  else true; fi; fi
 	-cp libgcc_s*$(SHLIB_EXT) stage4/
-	-cp libunwind* stage4/
+	-cp libunwind.a libunwind*$(SHLIB_EXT) stage4
 	-for f in .. $(EXTRA_MULTILIB_PARTS); do if [ x$${f} != x.. ]; then \
 	  cp stage4/$${f} . ; \
 	else true; \
diff -urN gcc.old/config/alpha/t-osf4 gcc/config/alpha/t-osf4
--- gcc.old/config/alpha/t-osf4	2003-01-26 12:35:07.000000000 +0100
+++ gcc/config/alpha/t-osf4	2004-12-10 14:37:33.000000000 +0100
@@ -11,9 +11,13 @@
 SHLIB_OBJS = @shlib_objs@
 
 SHLIB_LINK = $(GCC_FOR_TARGET) $(LIBGCC2_CFLAGS) -shared -nodefaultlibs \
-	-Wl,-msym -Wl,-set_version,gcc.1 -Wl,-soname,$(SHLIB_SONAME) \
+	-Wl,-msym -Wl,-set_version,gcc.1 -Wl,-soname,$(SHLIB_SONAME).tmp \
 	-o $(SHLIB_NAME) @multilib_flags@ $(SHLIB_OBJS) -lc && \
 	rm -f $(SHLIB_SONAME) && \
+	if [ -f $(SHLIB_NAME) ]; then \
+	  mv -f $(SHLIB_NAME) $(SHLIB_NAME).`basename $(STAGE_PREFIX)`; \
+	else true; fi && \
+	mv $(SHLIB_NAME).tmp $(SHLIB_NAME) && \
 	$(LN_S) $(SHLIB_NAME) $(SHLIB_SONAME)
 # $(slibdir) double quoted to protect it from expansion while building
 # libgcc.mk.  We want this delayed until actual install time.
diff -urN gcc.old/config/arm/t-netbsd gcc/config/arm/t-netbsd
--- gcc.old/config/arm/t-netbsd	2003-01-26 12:35:08.000000000 +0100
+++ gcc/config/arm/t-netbsd	2004-12-10 14:31:15.000000000 +0100
@@ -11,8 +11,12 @@
 
 SHLIB_LINK = $(GCC_FOR_TARGET) $(LIBGCC2_CFLAGS) -shared -nodefaultlibs \
 	-Wl,-soname,$(SHLIB_SONAME) \
-	-o $(SHLIB_NAME) @multilib_flags@ $(SHLIB_OBJS) -lc && \
+	-o $(SHLIB_NAME).tmp @multilib_flags@ $(SHLIB_OBJS) -lc && \
 	rm -f $(SHLIB_SONAME) && \
+	if [ -f $(SHLIB_NAME) ]; then \
+	  mv -f $(SHLIB_NAME) $(SHLIB_NAME).`basename $(STAGE_PREFIX)`; \
+	else true; fi && \
+	mv $(SHLIB_NAME).tmp $(SHLIB_NAME) && \
 	$(LN_S) $(SHLIB_NAME) $(SHLIB_SONAME)
 # $(slibdir) double quoted to protect it from expansion while building
 # libgcc.mk.  We want this delayed until actual install time.
diff -urN gcc.old/config/mips/t-iris5-6 gcc/config/mips/t-iris5-6
--- gcc.old/config/mips/t-iris5-6	2003-01-26 12:35:08.000000000 +0100
+++ gcc/config/mips/t-iris5-6	2004-12-10 14:31:15.000000000 +0100
@@ -9,8 +9,12 @@
 
 SHLIB_LINK = $(GCC_FOR_TARGET) $(LIBGCC2_CFLAGS) -shared -nodefaultlibs \
         -Wl,-soname,$(SHLIB_SONAME) \
-	-o $(SHLIB_NAME) @multilib_flags@ $(SHLIB_OBJS) -lc && \
+	-o $(SHLIB_NAME).tmp @multilib_flags@ $(SHLIB_OBJS) -lc && \
 	rm -f $(SHLIB_SOLINK) && \
+	if [ -f $(SHLIB_NAME) ]; then \
+	  mv -f $(SHLIB_NAME) $(SHLIB_NAME).`basename $(STAGE_PREFIX)`; \
+	else true; fi && \
+	mv $(SHLIB_NAME).tmp $(SHLIB_NAME) && \
 	$(LN_S) $(SHLIB_NAME) $(SHLIB_SOLINK)
 # ??? Irix 6.5 seems to eat the option fine (if we somehow remove the
 # -hidden_symbol option, which is documented to be ignored in conjunction
diff -urN gcc.old/config/pa/t-hpux-shlib gcc/config/pa/t-hpux-shlib
--- gcc.old/config/pa/t-hpux-shlib	2003-01-26 12:35:08.000000000 +0100
+++ gcc/config/pa/t-hpux-shlib	2004-12-10 14:31:15.000000000 +0100
@@ -5,8 +5,12 @@
 SHLIB_OBJS = @shlib_objs@
 
 SHLIB_LINK = $(GCC_FOR_TARGET) $(LIBGCC2_CFLAGS) -shared  -nodefaultlibs \
-	-o $(SHLIB_NAME) @multilib_flags@ $(SHLIB_OBJS) -lc && \
+	-o $(SHLIB_NAME).tmp @multilib_flags@ $(SHLIB_OBJS) -lc && \
         rm -f $(SHLIB_SONAME) && \
+	if [ -f $(SHLIB_NAME) ]; then \
+	  mv -f $(SHLIB_NAME) $(SHLIB_NAME).`basename $(STAGE_PREFIX)`; \
+	else true; fi && \
+	mv $(SHLIB_NAME).tmp $(SHLIB_NAME) && \
         $(LN_S) $(SHLIB_NAME) $(SHLIB_SONAME)
 
 
diff -urN gcc.old/config/sh/t-linux gcc/config/sh/t-linux
--- gcc.old/config/sh/t-linux	2004-07-26 02:47:35.000000000 +0200
+++ gcc/config/sh/t-linux	2004-12-10 14:31:15.000000000 +0100
@@ -25,8 +25,12 @@
 SHLIB_LINK = $(GCC_FOR_TARGET) $(LIBGCC2_CFLAGS) -shared -nodefaultlibs \
 	-Wl,--soname=$(SHLIB_SONAME) \
 	-Wl,--version-script=$(SHLIB_MAP) \
-	-o $(SHLIB_NAME) @multilib_flags@ $(SHLIB_OBJS) $(SHLIB_LC) && \
+	-o $(SHLIB_NAME).tmp @multilib_flags@ $(SHLIB_OBJS) $(SHLIB_LC) && \
 	rm -f $(SHLIB_SOLINK) && \
+	if [ -f $(SHLIB_NAME) ]; then \
+	  mv -f $(SHLIB_NAME) $(SHLIB_NAME).`basename $(STAGE_PREFIX)`; \
+	else true; fi && \
+	mv $(SHLIB_NAME).tmp $(SHLIB_NAME) && \
 	(echo "/* GNU ld script"; \
 	 echo "   Use the shared library, but some functions are only in"; \
 	 echo "   the static library.  */"; \
diff -urN gcc.old/config/t-libunwind-elf gcc/config/t-libunwind-elf
--- gcc.old/config/t-libunwind-elf	2004-12-10 13:17:36.000000000 +0100
+++ gcc/config/t-libunwind-elf	2004-12-10 14:31:15.000000000 +0100
@@ -10,9 +10,14 @@
 
 SHLIBUNWIND_LINK = $(GCC_FOR_TARGET) $(LIBGCC2_CFLAGS) -shared \
 	-nodefaultlibs -Wl,-h,$(SHLIBUNWIND_SONAME) \
-	-Wl,-z,text -Wl,-z,defs \
-	-o $(SHLIBUNWIND_NAME) @multilib_flags@ $(SHLIB_OBJS) -lc && \
+	-Wl,-z,text -Wl,-z,defs -o $(SHLIBUNWIND_NAME).tmp \
+	@multilib_flags@ $(SHLIB_OBJS) -lc && \
 	rm -f $(SHLIB_SOLINK) && \
+	if [ -f $(SHLIBUNWIND_NAME) ]; then \
+	  mv -f $(SHLIBUNWIND_NAME) \
+	     $(SHLIBUNWIND_NAME).`basename $(STAGE_PREFIX)`; \
+	else true; fi && \
+	mv $(SHLIBUNWIND_NAME).tmp $(SHLIBUNWIND_NAME) && \
 	$(LN_S) $(SHLIBUNWIND_NAME) $(SHLIB_SOLINK)
 
 # $(slibdir) double quoted to protect it from expansion while building
diff -urN gcc.old/config/t-slibgcc-elf-ver gcc/config/t-slibgcc-elf-ver
--- gcc.old/config/t-slibgcc-elf-ver	2003-01-26 12:35:07.000000000 +0100
+++ gcc/config/t-slibgcc-elf-ver	2004-12-10 14:31:18.000000000 +0100
@@ -13,8 +13,12 @@
 SHLIB_LINK = $(GCC_FOR_TARGET) $(LIBGCC2_CFLAGS) -shared -nodefaultlibs \
 	-Wl,--soname=$(SHLIB_SONAME) \
 	-Wl,--version-script=$(SHLIB_MAP) \
-	-o $(SHLIB_NAME) @multilib_flags@ $(SHLIB_OBJS) $(SHLIB_LC) && \
+	-o $(SHLIB_NAME).tmp @multilib_flags@ $(SHLIB_OBJS) $(SHLIB_LC) && \
 	rm -f $(SHLIB_SOLINK) && \
+	if [ -f $(SHLIB_NAME) ]; then \
+	  mv -f $(SHLIB_NAME) $(SHLIB_NAME).`basename $(STAGE_PREFIX)`; \
+	else true; fi && \
+	mv $(SHLIB_NAME).tmp $(SHLIB_NAME) && \
 	$(LN_S) $(SHLIB_NAME) $(SHLIB_SOLINK)
 # $(slibdir) double quoted to protect it from expansion while building
 # libgcc.mk.  We want this delayed until actual install time.
diff -urN gcc.old/config/t-slibgcc-sld gcc/config/t-slibgcc-sld
--- gcc.old/config/t-slibgcc-sld	2003-01-26 12:35:07.000000000 +0100
+++ gcc/config/t-slibgcc-sld	2004-12-10 14:31:18.000000000 +0100
@@ -10,9 +10,13 @@
 
 SHLIB_LINK = $(GCC_FOR_TARGET) $(LIBGCC2_CFLAGS) -shared -nodefaultlibs \
 	-Wl,-h,$(SHLIB_SONAME) -Wl,-z,text -Wl,-z,defs \
-	-Wl,-M,$(SHLIB_MAP) -o $(SHLIB_NAME) \
+	-Wl,-M,$(SHLIB_MAP) -o $(SHLIB_NAME).tmp \
 	@multilib_flags@ $(SHLIB_OBJS) -lc && \
 	rm -f $(SHLIB_SOLINK) && \
+	if [ -f $(SHLIB_NAME) ]; then \
+	  mv -f $(SHLIB_NAME) $(SHLIB_NAME).`basename $(STAGE_PREFIX)`; \
+	else true; fi && \
+	mv $(SHLIB_NAME).tmp $(SHLIB_NAME) && \
 	$(LN_S) $(SHLIB_NAME) $(SHLIB_SOLINK)
 # $(slibdir) double quoted to protect it from expansion while building
 # libgcc.mk.  We want this delayed until actual install time.
diff -urN gcc.old/mklibgcc.in gcc/mklibgcc.in
--- gcc.old/mklibgcc.in	2004-12-10 14:19:28.000000000 +0100
+++ gcc/mklibgcc.in	2004-12-10 14:31:18.000000000 +0100
@@ -503,6 +503,7 @@
 echo '	done'
 echo '	-for dir in '"${dirs}"'; do \'
 echo '	  mv $$dir/*'"${objext}"' $(stage)/$$dir; \'
+echo '	  mv $$dir/*'"${objext}s"' $(stage)/$$dir || true; \'
 echo '	  test ! -f $$dir/stacknote.s || mv $$dir/stacknote.s $(stage)/$$dir; \'
 echo '	done'
 
