From 5bb334f6665f54b1a618c0075ab46d8658fc8855 Mon Sep 17 00:00:00 2001
From: David Edelsohn <dje.gcc@gmail.com>
Date: Tue, 26 May 2020 17:30:21 -0400
Subject: [PATCH] rs6000: libgcc multilib

    libgcc/
	* config.host (extra_parts): Add crtcxa_64 and crtdbase_64.
	* config/rs6000/t-aix-cxa: Explicitly compile 32 bit with -maix32
	and 64 bit with -maix64.
	* config/rs6000/t-slibgcc-aix: Remove extra @multilib_dir@ level.
	Build and install AIX-style FAT libraries.

    gcc/
	* config/rs6000/aix72.h (STARTFILE_SPEC): Add 64 bit crtcxa.
---
 gcc/config/rs6000/aix72.h          |  3 +-
 libgcc/config.host                 |  2 +-
 libgcc/config/rs6000/t-aix-cxa     | 15 +++++--
 libgcc/config/rs6000/t-slibgcc-aix | 69 +++++++++++++++++++++---------
 4 files changed, 64 insertions(+), 25 deletions(-)

diff --git a/gcc/config/rs6000/aix72.h b/gcc/config/rs6000/aix72.h
index 95f36118e74..373c3c87866 100644
--- a/gcc/config/rs6000/aix72.h
+++ b/gcc/config/rs6000/aix72.h
@@ -177,7 +177,8 @@ do {									\
    %{maix64:%{pg:gcrt0_64%O%s;:%{p:mcrt0_64%O%s;:crt0_64%O%s}};:\
      %{pthread:%{pg:gcrt0_r%O%s;:%{p:mcrt0_r%O%s;:crt0_r%O%s}};:\
        %{pg:gcrt0%O%s;:%{p:mcrt0%O%s;:crt0%O%s}}}}}\
-   %{shared:crtcxa_s%O%s;:crtcxa%O%s} crtdbase%O%s"
+   %{maix64:%{shared:crtcxa_64_s%O%s;:crtcxa_64%O%s} crtdbase_64%O%s;:\
+     %{shared:crtcxa_s%O%s;:crtcxa%O%s} crtdbase%O%s}"
 
 /* AIX V5 typedefs ptrdiff_t as "long" while earlier releases used "int".  */
 
diff --git a/libgcc/config.host b/libgcc/config.host
index 2cd42097167..fb963bed57d 100644
--- a/libgcc/config.host
+++ b/libgcc/config.host
@@ -1267,7 +1267,7 @@ rs6000-ibm-aix5.1.* | powerpc-ibm-aix5.1.*)
 rs6000-ibm-aix[56789].* | powerpc-ibm-aix[56789].*)
 	md_unwind_header=rs6000/aix-unwind.h
 	tmake_file="t-fdpbit rs6000/t-ppc64-fp rs6000/t-slibgcc-aix rs6000/t-ibm-ldouble rs6000/t-aix-cxa"
-	extra_parts="crtcxa.o crtcxa_s.o crtdbase.o"
+	extra_parts="crtcxa.o crtcxa_s.o crtdbase.o crtcxa_64.o crtcxa_64_s.o crtdbase_64.o"
 	;;
 rl78-*-elf)
 	tmake_file="$tm_file t-fdpbit rl78/t-rl78"
diff --git a/libgcc/config/rs6000/t-aix-cxa b/libgcc/config/rs6000/t-aix-cxa
index d7e66386619..0e1bccb1525 100644
--- a/libgcc/config/rs6000/t-aix-cxa
+++ b/libgcc/config/rs6000/t-aix-cxa
@@ -8,10 +8,19 @@ LIB2ADD_ST += $(srcdir)/config/rs6000/aixinitfini.c
 SHLIB_MAPFILES += $(srcdir)/config/rs6000/libgcc-aix-cxa.ver
 
 crtdbase.o: $(srcdir)/config/rs6000/crtdbase.S
-	$(crt_compile) -c $<
+	$(crt_compile) -maix32 -c $<
 
 crtcxa.o: $(srcdir)/config/rs6000/crtcxa.c
-	$(crt_compile) -c $<
+	$(crt_compile) -maix32 -c $<
 
 crtcxa_s.o: $(srcdir)/config/rs6000/crtcxa.c
-	$(crt_compile) -DSHARED -c $<
+	$(crt_compile) -maix32 -DSHARED -c $<
+
+crtdbase_64.o: $(srcdir)/config/rs6000/crtdbase.S
+	$(crt_compile) -maix64 -c $<
+
+crtcxa_64.o: $(srcdir)/config/rs6000/crtcxa.c
+	$(crt_compile) -maix64 -c $<
+
+crtcxa_64_s.o: $(srcdir)/config/rs6000/crtcxa.c
+	$(crt_compile) -maix64 -DSHARED -c $<
diff --git a/libgcc/config/rs6000/t-slibgcc-aix b/libgcc/config/rs6000/t-slibgcc-aix
index 11db9efa0e9..e5f8a029ff8 100644
--- a/libgcc/config/rs6000/t-slibgcc-aix
+++ b/libgcc/config/rs6000/t-slibgcc-aix
@@ -37,48 +37,44 @@ SHLIB_SONAME = @shlib_base_name@.so.$(SHLIB_SOVERSION)
 SHLIB_LINK = \
 	if test svr4 != $(with_aix_soname) ; then \
 	  $(CC) $(LIBGCC2_CFLAGS) -shared -Wl,-bnortl -nodefaultlibs \
-	  -Wl,-bE:@shlib_map_file@ -o @multilib_dir@/shr.o \
+	  -Wl,-bE:@shlib_map_file@ -o shr.o \
 	  @multilib_flags@ @shlib_objs@ -lc \
 	  `case @multilib_dir@ in \
 	  *pthread*) echo -L$(TARGET_SYSTEM_ROOT)/usr/lib/threads -lpthreads -lc_r $(TARGET_SYSTEM_ROOT)/usr/lib/libc.a ;; \
 	  *) echo -lc ;; esac` ; \
-	  rm -f @multilib_dir@/tmp-@shlib_base_name@.a ; \
-	  $(AR_CREATE_FOR_TARGET) @multilib_dir@/tmp-@shlib_base_name@.a \
-	    @multilib_dir@/shr.o ; \
-	  mv @multilib_dir@/tmp-@shlib_base_name@.a \
-	     @multilib_dir@/@shlib_base_name@.a ; \
-	  rm -f @multilib_dir@/shr.o ; \
+	  rm -f tmp-@shlib_base_name@.a ; \
+	  $(AR_CREATE_FOR_TARGET) tmp-@shlib_base_name@.a shr.o ; \
+	  mv tmp-@shlib_base_name@.a @shlib_base_name@.a ; \
+	  rm -f shr.o ; \
 	fi ; \
 	if test aix != $(with_aix_soname) ; then \
 	  case @multilib_dir@ in *64*) shr='shr_64' ;; *) shr='shr' ;; esac ; \
 	  $(CC) $(LIBGCC2_CFLAGS) -shared -Wl,-G -nodefaultlibs \
-	  -Wl,-bE:@shlib_map_file@ -o @multilib_dir@/$$shr.o \
+	  -Wl,-bE:@shlib_map_file@ -o $$shr.o \
 	  @multilib_flags@ @shlib_objs@ -lc \
 	  `case @multilib_dir@ in \
 	  *pthread*) echo -L$(TARGET_SYSTEM_ROOT)/usr/lib/threads -lpthreads -lc_r $(TARGET_SYSTEM_ROOT)/usr/lib/libc.a ;; \
 	  *) echo -lc ;; esac` ; \
-	  $(STRIP_FOR_TARGET) -X32_64 -e @multilib_dir@/$$shr.o ; \
+	  $(STRIP_FOR_TARGET) -X32_64 -e $$shr.o ; \
 	  { echo "\#! $(SHLIB_SONAME)($$shr.o)" ; \
 	    case @multilib_dir@ in *64*) echo '\# 64' ;; *) echo '\# 32' ;; esac ; \
 	    cat @shlib_map_file@ ; \
-	  } > @multilib_dir@/$$shr.imp ; \
-	  rm -f @multilib_dir@/tmp-$(SHLIB_SONAME) ; \
-	  $(AR_CREATE_FOR_TARGET) @multilib_dir@/tmp-$(SHLIB_SONAME) \
-	    @multilib_dir@/$$shr.imp @multilib_dir@/$$shr.o ; \
-	  mv @multilib_dir@/tmp-$(SHLIB_SONAME) \
-	     @multilib_dir@/$(SHLIB_SONAME) ; \
-	  rm -f @multilib_dir@/@shlib_base_name@.so ; \
-	  $(LN_S) $(SHLIB_SONAME) @multilib_dir@/@shlib_base_name@.so ; \
-	  rm -f @multilib_dir@/$$shr.imp @multilib_dir@/$$shr.o ; \
+	  } > $$shr.imp ; \
+	  rm -f tmp-$(SHLIB_SONAME) ; \
+	  $(AR_CREATE_FOR_TARGET) tmp-$(SHLIB_SONAME) $$shr.imp $$shr.o ; \
+	  mv tmp-$(SHLIB_SONAME) $(SHLIB_SONAME) ; \
+	  rm -f @shlib_base_name@.so ; \
+	  $(LN_S) $(SHLIB_SONAME) @shlib_base_name@.so ; \
+	  rm -f $$shr.imp $$shr.o ; \
 	fi
 SHLIB_INSTALL = \
 	$(mkinstalldirs) $(DESTDIR)$(slibdir)@shlib_slibdir_qual@; \
 	if test svr4 != $(with_aix_soname) ; then \
-	  $(INSTALL_DATA) @multilib_dir@/@shlib_base_name@.a \
+	  $(INSTALL_DATA) @shlib_base_name@.a \
 		  $(DESTDIR)$(slibdir)@shlib_slibdir_qual@/ ; \
 	fi ; \
 	if test aix != $(with_aix_soname) ; then \
-	  $(INSTALL_DATA) @multilib_dir@/$(SHLIB_SONAME) \
+	  $(INSTALL_DATA) $(SHLIB_SONAME) \
 		  $(DESTDIR)$(slibdir)@shlib_slibdir_qual@/ ; \
 	  rm -f $(DESTDIR)$(slibdir)@shlib_slibdir_qual@/@shlib_base_name@.so ; \
 	  $(LN_S) $(SHLIB_SONAME) \
@@ -91,3 +87,36 @@ SHLIB_NM_FLAGS = -Bpg -X32_64
 
 # Either 32-bit and 64-bit objects in archives.
 AR_FLAGS_FOR_TARGET = -X32_64
+
+# Build and install AIX-style FAT libraries
+ifeq ($(MULTIBUILDTOP),)
+BITS=$(shell if test -z "`ar -X64 t libgcc_s.a`" ; then \
+    echo '64'; else echo '32'; fi)
+all: all-multi
+ifeq ($(enable_shared),yes)
+	ar -X$(BITS) x ../ppc$(BITS)/libgcc/libgcc_s.a shr.o
+	ar -X$(BITS) rc ./libgcc_s.a shr.o
+	rm -f shr.o
+	ar -X$(BITS) x ../pthread/ppc$(BITS)/libgcc/libgcc_s.a shr.o
+	ar -X$(BITS) rc ../pthread/libgcc/libgcc_s.a shr.o
+	rm -f shr.o
+endif
+	ar -X$(BITS) rc libgcc.a \
+	    $(addprefix ../ppc$(BITS)/libgcc/,$(libgcc-objects))
+	ar -X$(BITS) rc libgcc_eh.a \
+	    $(addprefix ../ppc$(BITS)/libgcc/,$(libgcc-eh-objects))
+	ar -X$(BITS) rc ../pthread/libgcc/libgcc.a \
+	    $(addprefix ../pthread/ppc$(BITS)/libgcc/,$(libgcc-objects))
+	ar -X$(BITS) rc ../pthread/libgcc/libgcc_eh.a \
+	    $(addprefix ../pthread/ppc$(BITS)/libgcc/,$(libgcc-eh-objects))
+ifeq ($(enable_gcov),yes)
+	ar -X$(BITS) rc libgcov.a \
+	    $(addprefix ../ppc$(BITS)/libgcc/,$(libgcov-objects))
+	ar -X$(BITS) rc ../pthread/libgcc/libgcov.a \
+	    $(addprefix ../pthread/ppc$(BITS)/libgcc/,$(libgcov-objects))
+endif
+	$(MAKE) install-leaf DESTDIR=$(gcc_objdir) \
+	    slibdir= libsubdir= MULTIOSDIR=$(MULTIDIR)
+	cp ../pthread/libgcc/libgcc_s.a ../../gcc/pthread/
+	cp ../pthread/libgcc/libgcc_eh.a ../../gcc/pthread/
+endif
-- 
2.20.1

