From d08b53644aeafdfac9e6a0032b11ebba572ad424 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Chigot?= <clement.chigot@atos.net>
Date: Fri, 24 Jul 2020 10:47:29 +0200
Subject: [PATCH] gcc: add GCC64 configuration for AIX 7.1

This patch adds the support to build 64bit GCC applications on AIX 7.1

Changelog:
2020-07-24 Clement Chigot <clement.chigot@atos.net>

       * config.gcc: Use t-aix64, biarch64 and default64 for cpu_is_64bit.
       * config/rs6000/aix71.h (ASM_SPEC): Remove aix64 option.
       (ASM_SPEC32): New.
       (ASM_SPEC64): New.
       (ASM_CPU_SPEC): Remove vsx and altivec options.
       (CPP_SPEC_COMMON): Rename from CPP_SPEC.
       (CPP_SPEC32): New.
       (CPP_SPEC64): New.
       (CPLUSPLUS_CPP_SPEC): Rename to CPLUSPLUS_CPP_SPEC_COMMON..
       (TARGET_DEFAULT): Only define if not BIARCH.
       (LIB_SPEC_COMMON): Rename from LIB_SPEC.
       (LIB_SPEC32): New.
       (LIB_SPEC64): New.
       (LINK_SPEC_COMMON): Rename from LINK_SPEC.
       (LINK_SPEC32): New.
       (LINK_SPEC64): New.
       (STARTFILE_SPEC): Add 64 bit version of crtcxa and crtdbase.
       (ASM_SPEC): Define 32 and 64 bit alternatives using DEFAULT_ARCH64_P.
       (CPP_SPEC): Same.
       (CPLUSPLUS_CPP_SPEC): Same.
       (LIB_SPEC): Same.
       (LINK_SPEC): Same.
       (SUBTARGET_EXTRA_SPECS): Add new 32/64 specs.
---
 gcc/config.gcc            |  6 ++-
 gcc/config/rs6000/aix71.h | 88 +++++++++++++++++++++++++++++----------
 2 files changed, 72 insertions(+), 22 deletions(-)

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 30b51c3dc81..cd1ff98505c 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -3031,8 +3031,12 @@ rs6000-ibm-aix6.* | powerpc-ibm-aix6.*)
 	default_use_cxa_atexit=yes
 	;;
 rs6000-ibm-aix7.1.* | powerpc-ibm-aix7.1.*)
-	tm_file="${tm_file} rs6000/aix.h rs6000/aix71.h rs6000/xcoff.h rs6000/aix-stdint.h"
 	tmake_file="rs6000/t-aix52 t-slibgcc"
+	if test x$cpu_is_64bit = xyes; then
+	    tm_file="${tm_file} rs6000/biarch64.h rs6000/defaultaix64.h"
+	    tmake_file="rs6000/t-aix64 t-slibgcc"
+	fi
+	tm_file="${tm_file} rs6000/aix.h rs6000/aix71.h rs6000/xcoff.h rs6000/aix-stdint.h"
 	extra_options="${extra_options} rs6000/aix64.opt"
 	use_collect2=yes
 	thread_file='aix'
diff --git a/gcc/config/rs6000/aix71.h b/gcc/config/rs6000/aix71.h
index 1d1c0bc9d9b..8bbfacb204f 100644
--- a/gcc/config/rs6000/aix71.h
+++ b/gcc/config/rs6000/aix71.h
@@ -64,8 +64,9 @@ do {									\
     }									\
 } while (0)
 
-#undef ASM_SPEC
-#define ASM_SPEC "-u %{maix64:-a64} %(asm_cpu)"
+#define ASM_SPEC32 "-a32"
+#define ASM_SPEC64 "-a64"
+#define ASM_SPEC_COMMON "-u %(asm_cpu)"
 
 /* Common ASM definitions used by ASM_SPEC amongst the various targets for
    handling -mcpu=xxx switches.  There is a parallel list in driver-rs6000.c to
@@ -91,10 +92,7 @@ do {									\
   mcpu=620: -m620; \
   mcpu=630: -m620; \
   mcpu=970|mcpu=G5: -m970; \
-  !mcpu*: %{mvsx: -mpwr6; \
-	    maltivec: -m970; \
-	    maix64|mpowerpc64: -mppc64; \
-	    : %(asm_default)}} \
+  !mcpu*: %(asm_default)} \
 -many"
 
 #undef	ASM_DEFAULT_SPEC
@@ -114,19 +112,17 @@ do {									\
     }                                \
   while (0)
 
-#undef CPP_SPEC
-#define CPP_SPEC "%{posix: -D_POSIX_SOURCE}	\
+#define CPP_SPEC32 ""
+#define CPP_SPEC64 "-D__64BIT__"
+#define CPP_SPEC_COMMON "%{posix: -D_POSIX_SOURCE} \
   %{ansi: -D_ANSI_C_SOURCE}			\
-  %{maix64: -D__64BIT__}			\
   %{mpe: -I%R/usr/lpp/ppe.poe/include}		\
   %{pthread: -D_THREAD_SAFE}"
 
 /* The GNU C++ standard library requires that these macros be 
    defined.  Synchronize with libstdc++ os_defines.h.  */
-#undef CPLUSPLUS_CPP_SPEC                       
-#define CPLUSPLUS_CPP_SPEC			\
+#define CPLUSPLUS_CPP_SPEC_COMMON				\
   "-D_ALL_SOURCE -D__COMPATMATH__		\
-   %{maix64: -D__64BIT__}			\
    %{mpe: -I%R/usr/lpp/ppe.poe/include}		\
    %{pthread: -D_THREAD_SAFE}"
 
@@ -134,8 +130,10 @@ do {									\
 #include "rs6000-cpus.def"
 #undef RS6000_CPU
 
+#ifndef RS6000_BI_ARCH
 #undef  TARGET_DEFAULT
 #define TARGET_DEFAULT (MASK_PPC_GPOPT | MASK_PPC_GFXOPT | MASK_MFCRF)
+#endif
 
 #undef  PROCESSOR_DEFAULT
 #define PROCESSOR_DEFAULT PROCESSOR_POWER7
@@ -154,29 +152,77 @@ do {									\
    the target makefile fragment or if none of the options listed in
    `MULTILIB_OPTIONS' are set by default.  *Note Target Fragment::.  */
 
-#undef	MULTILIB_DEFAULTS
+#undef MULTILIB_DEFAULTS
 
-#undef LIB_SPEC
-#define LIB_SPEC "%{pg:-L%R/lib/profiled -L%R/usr/lib/profiled}\
+#define DEFAULT_ARCH64_P (TARGET_DEFAULT & MASK_64BIT)
+
+#define LIB_SPEC32 "%{!shared:%{g*:-lg}}"
+#define LIB_SPEC64 ""
+#define LIB_SPEC_COMMON "%{pg:-L%R/lib/profiled -L%R/usr/lib/profiled}\
    %{p:-L%R/lib/profiled -L%R/usr/lib/profiled}\
-   %{!maix64:%{!shared:%{g*:-lg}}}\
    %{fprofile-arcs|fprofile-generate*|coverage:-lpthreads}\
    %{mpe:-L%R/usr/lpp/ppe.poe/lib -lmpi -lvtd}\
    %{mlong-double-128:-lc128}\
    %{pthread:-lpthreads} -lc -L/opt/freeware/lib"
 
-#undef LINK_SPEC
-#define LINK_SPEC "-bpT:0x10000000 -bpD:0x20000000 %{!r:-btextro}\
+#define LINK_SPEC32 "%{!shared:%{g*: %(link_libg) }} -b32"
+#define LINK_SPEC64 "-b64"
+#define LINK_SPEC_COMMON "-bpT:0x10000000 -bpD:0x20000000 %{!r:-btextro}\
    %{static:-bnso %(link_syscalls) } %{shared:-bM:SRE %{!e:-bnoentry}}\
-   %{!maix64:%{!shared:%{g*: %(link_libg) }}} %{maix64:-b64}\
-   %{mpe:-binitfini:poe_remote_main}"
+   %{mpe:-binitfini:poe_remote_main} "
 
 #undef STARTFILE_SPEC
+#if DEFAULT_ARCH64_P
+#define STARTFILE_SPEC "%{!shared:\
+   %{!maix32:%{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}}}}}\
+   %{!maix32:%{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}"
+#else
 #define STARTFILE_SPEC "%{!shared:\
    %{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}"
+#endif
+
+#undef ASM_SPEC
+#undef CPP_SPEC
+#undef CPLUSPLUS_CPP_SPEC
+#undef LIB_SPEC
+#undef LINK_SPEC
+
+#if DEFAULT_ARCH64_P
+#define ASM_SPEC "%{maix32:%(asm_spec32);:%(asm_spec64)} %(asm_spec_common)"
+#define CPP_SPEC "%{maix32:%(cpp_spec32);:%(cpp_spec64)} %(cpp_spec_common)"
+#define CPLUSPLUS_CPP_SPEC "%{maix32:%(cpp_spec32);:%(cpp_spec64)} %(cplusplus_cpp_spec_common)"
+#define LIB_SPEC "%{maix32:%(lib_spec32);:%(lib_spec64)} %(lib_spec_common)"
+#define LINK_SPEC "%{maix32:%(link_spec32);:%(link_spec64)} %(link_spec_common)"
+#else
+#define ASM_SPEC "%{maix64:%(asm_spec64);:%(asm_spec32)} %(asm_spec_common)"
+#define CPP_SPEC "%{maix64:%(cpp_spec64);:%(cpp_spec32)} %(cpp_spec_common)"
+#define CPLUSPLUS_CPP_SPEC "%{maix64:%(cpp_spec64);:%(cpp_spec32)} %(cplusplus_cpp_spec_common)"
+#define LIB_SPEC "%{maix64:%(lib_spec64);:%(lib_spec32)} %(lib_spec_common)"
+#define LINK_SPEC "%{maix64:%(link_spec64);:%(link_spec32)} %(link_spec_common)"
+#endif
+
+#undef SUBTARGET_EXTRA_SPECS
+#define SUBTARGET_EXTRA_SPECS					\
+  { "asm_spec_common",		ASM_SPEC_COMMON },		\
+  { "asm_spec32",		ASM_SPEC32 },			\
+  { "asm_spec64",		ASM_SPEC64 },			\
+  { "cpp_spec_common",		CPP_SPEC_COMMON },		\
+  { "cplusplus_cpp_spec_common", CPLUSPLUS_CPP_SPEC_COMMON },	\
+  { "cpp_spec32",		CPP_SPEC32 },			\
+  { "cpp_spec64",		CPP_SPEC64 },			\
+  { "lib_spec_common",		LIB_SPEC_COMMON },		\
+  { "lib_spec32",		LIB_SPEC32 },			\
+  { "lib_spec64",		LIB_SPEC64 },			\
+  { "link_spec_common",		LINK_SPEC_COMMON },		\
+  { "link_spec32",		LINK_SPEC32 },			\
+  { "link_spec64",		LINK_SPEC64 },
 
 /* AIX V5 typedefs ptrdiff_t as "long" while earlier releases used "int".  */
 
-- 
2.25.0

