diff --git a/configure b/configure
index 4cc938ebb7d..1af7c8b7219 100755
--- a/configure
+++ b/configure
@@ -3789,6 +3789,18 @@ case "${target}" in
     ;;
   *-*-vxworks*)
     ;;
+  aarch64*-*-solaris2*)
+    noconfigdirs="$noconfigdirs target-newlib target-libgloss ${libgcj}"
+    noconfigdirs="$noconfigdirs target-libiberty"
+    noconfigdirs="$noconfigdirs target-libmudflap target-libgomp target-libitm"
+    case "${host}" in
+        aarch64*-*-solaris2*)
+        ;;
+        *)
+            noconfigdirs="$noconfigdirs target-libstdc++-v3"
+        ;;
+    esac
+    ;;
   alpha*-dec-osf*)
     # ld works, but does not support shared libraries.
     # gas doesn't generate exception information.
@@ -3797,6 +3809,18 @@ case "${target}" in
   alpha*-*-*vms*)
     noconfigdirs="$noconfigdirs gdb target-newlib target-libgloss"
     ;;
+  alpha*-*-solaris2*)
+    noconfigdirs="$noconfigdirs target-newlib target-libgloss ${libgcj}"
+    noconfigdirs="$noconfigdirs target-libiberty"
+    noconfigdirs="$noconfigdirs target-libmudflap target-libgomp target-libitm"
+    case "${host}" in
+        alpha*-*-solaris2*)
+        ;;
+        *)
+            noconfigdirs="$noconfigdirs target-libstdc++-v3"
+        ;;
+    esac
+    ;;
   alpha*-*-*)
     # newlib is not 64 bit ready
     noconfigdirs="$noconfigdirs target-newlib target-libgloss"
@@ -3953,6 +3977,18 @@ case "${target}" in
   powerpc-*-beos*)
     noconfigdirs="$noconfigdirs gdb"
     ;;
+  riscv64-*-solaris2*)
+    noconfigdirs="$noconfigdirs target-newlib target-libgloss ${libgcj}"
+    noconfigdirs="$noconfigdirs target-libiberty"
+    noconfigdirs="$noconfigdirs target-libmudflap target-libgomp target-libitm"
+    case "${host}" in
+        riscv*-*-solaris2*)
+        ;;
+        *)
+            noconfigdirs="$noconfigdirs target-libstdc++-v3"
+        ;;
+    esac
+    ;;
   rs6000-*-lynxos*)
     noconfigdirs="$noconfigdirs gprof"
     ;;
diff --git a/configure.ac b/configure.ac
index c78d9cbea62..5621814f675 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1064,6 +1064,18 @@ case "${target}" in
     ;;
   *-*-vxworks*)
     ;;
+  aarch64*-*-solaris2*)
+    noconfigdirs="$noconfigdirs target-newlib target-libgloss ${libgcj}"
+    noconfigdirs="$noconfigdirs target-libiberty"
+    noconfigdirs="$noconfigdirs target-libmudflap target-libgomp target-libitm"
+    case "${host}" in
+        aarch64*-*-solaris2*)
+        ;;
+        *)
+            noconfigdirs="$noconfigdirs target-libstdc++-v3"
+        ;;
+    esac
+    ;;
   alpha*-dec-osf*)
     # ld works, but does not support shared libraries.
     # gas doesn't generate exception information.
@@ -1072,6 +1084,18 @@ case "${target}" in
   alpha*-*-*vms*)
     noconfigdirs="$noconfigdirs gdb target-newlib target-libgloss"
     ;;
+  alpha*-*-solaris2*)
+    noconfigdirs="$noconfigdirs target-newlib target-libgloss ${libgcj}"
+    noconfigdirs="$noconfigdirs target-libiberty"
+    noconfigdirs="$noconfigdirs target-libmudflap target-libgomp target-libitm"
+    case "${host}" in
+        alpha*-*-solaris2*)
+        ;;
+        *)
+            noconfigdirs="$noconfigdirs target-libstdc++-v3"
+        ;;
+    esac
+    ;;
   alpha*-*-*)
     # newlib is not 64 bit ready
     noconfigdirs="$noconfigdirs target-newlib target-libgloss"
@@ -1228,6 +1252,18 @@ case "${target}" in
   powerpc-*-beos*)
     noconfigdirs="$noconfigdirs gdb"
     ;;
+  riscv64-*-solaris2*)
+    noconfigdirs="$noconfigdirs target-newlib target-libgloss ${libgcj}"
+    noconfigdirs="$noconfigdirs target-libiberty"
+    noconfigdirs="$noconfigdirs target-libmudflap target-libgomp target-libitm"
+    case "${host}" in
+        riscv*-*-solaris2*)
+        ;;
+        *)
+            noconfigdirs="$noconfigdirs target-libstdc++-v3"
+        ;;
+    esac
+    ;;
   rs6000-*-lynxos*)
     noconfigdirs="$noconfigdirs gprof"
     ;;
diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 543b477ff18..f1023bbb54a 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -2928,11 +2928,11 @@ s-omp-device-properties-h: @omp_device_property_deps@
 # any system header is included.
 gengtype-lex.c : gengtype-lex.l
 	-$(FLEX) $(FLEXFLAGS) -o$@ $< && { \
-	  echo '#ifdef HOST_GENERATOR_FILE' > $@.tmp; \
-	  echo '#include "config.h"'       >> $@.tmp; \
-	  echo '#else'                     >> $@.tmp; \
-	  echo '#include "bconfig.h"'      >> $@.tmp; \
-	  echo '#endif'                    >> $@.tmp; \
+	  echo '#ifdef GENERATOR_FILE' > $@.tmp; \
+	  echo '#include "bconfig.h"' >> $@.tmp; \
+	  echo '#else' >> $@.tmp; \
+	  echo '#include "config.h"' >> $@.tmp; \
+	  echo '#endif' >> $@.tmp; \
 	  cat $@ >> $@.tmp; \
 	  mv $@.tmp $@; \
 	}
diff --git a/gcc/config.gcc b/gcc/config.gcc
index cf1a87e2efd..c69579b9bd1 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -1111,6 +1111,10 @@ aarch64*-*-linux*)
 	done
 	TM_MULTILIB_CONFIG=`echo $TM_MULTILIB_CONFIG | sed 's/^,//'`
 	;;
+aarch64*-*-solaris2*)
+	tm_file="${tm_file} elfos.h sol2.h aarch64/aarch64-elf.h aarch64/sol2.h"
+	tmake_file="${tmake_file} aarch64/t-aarch64 aarch64/t-sol2"
+	;;
 aarch64*-wrs-vxworks*)
         tm_file="${tm_file} elfos.h aarch64/aarch64-elf.h"
         tm_file="${tm_file} vx-common.h vxworks.h aarch64/aarch64-vxworks.h"
@@ -1121,6 +1125,11 @@ alpha*-*-linux*)
 	tmake_file="${tmake_file} alpha/t-linux alpha/t-alpha"
 	extra_options="${extra_options} alpha/elf.opt"
 	;;
+alpha*-*-solaris2*)
+	tm_file="elfos.h ${tm_file} alpha/elf.h sol2.h alpha/sol2.h"
+	extra_options="${extra_options} alpha/elf.opt"
+	tmake_file="${tmake_file} alpha/t-alpha alpha/t-ieee alpha/t-sol2"
+	;;
 alpha*-*-netbsd*)
 	tm_file="elfos.h ${tm_file} ${nbsd_tm_file} alpha/elf.h alpha/netbsd.h"
 	tmake_file="${tmake_file} alpha/t-alpha"
@@ -2428,6 +2437,18 @@ microblaze*-*-elf)
 	cxx_target_objs="${cxx_target_objs} microblaze-c.o"
 	tmake_file="${tmake_file} microblaze/t-microblaze"
         ;;
+riscv64-*-solaris2*)
+	tm_file="${tm_file} elfos.h sol2.h riscv/sol2.h"
+	case "x${enable_multilib}" in
+	xno) ;;
+	xyes) tmake_file="${tmake_file} riscv/t-sol2-multilib" ;;
+	*) echo "Unknown value for enable_multilib"; exit 1
+	esac
+	tmake_file="${tmake_file} riscv/t-riscv riscv/t-sol2"
+	gnu_ld=yes
+	gas=yes
+	gcc_cv_initfini_array=yes
+	;;
 riscv*-*-linux*)
 	tm_file="elfos.h gnu-user.h linux.h glibc-stdint.h ${tm_file} riscv/linux.h"
 	case "x${enable_multilib}" in
diff --git a/gcc/config.host b/gcc/config.host
index 84f0433e2ad..9eb5cc50293 100644
--- a/gcc/config.host
+++ b/gcc/config.host
@@ -99,7 +99,7 @@ case ${host} in
 esac
 
 case ${host} in
-  aarch64*-*-freebsd* | aarch64*-*-linux* | aarch64*-*-fuchsia*)
+  aarch64*-*-freebsd* | aarch64*-*-linux* | aarch64*-*-fuchsia* | aarch64*-*-solaris*)
     case ${target} in
       aarch64*-*-*)
 	host_extra_gcc_objs="driver-aarch64.o"
diff --git a/gcc/config/aarch64/aarch64-c.c b/gcc/config/aarch64/aarch64-c.c
index e1c1cd415dc..6657483bc2a 100644
--- a/gcc/config/aarch64/aarch64-c.c
+++ b/gcc/config/aarch64/aarch64-c.c
@@ -53,6 +53,7 @@ static void
 aarch64_define_unconditional_macros (cpp_reader *pfile)
 {
   builtin_define ("__aarch64__");
+  builtin_define ("__aarch64");
   builtin_define ("__ARM_64BIT_STATE");
 
   builtin_define ("__ARM_ARCH_ISA_A64");
diff --git a/gcc/config/aarch64/aarch64-elf.h b/gcc/config/aarch64/aarch64-elf.h
index 07685657c5e..044b29257ad 100644
--- a/gcc/config/aarch64/aarch64-elf.h
+++ b/gcc/config/aarch64/aarch64-elf.h
@@ -108,6 +108,7 @@
 #define ENDIAN_SPEC "-mlittle-endian"
 #endif
 
+#undef MULTILIB_DEFAULTS
 #if TARGET_DATA_MODEL == 1
 #define ABI_SPEC  "-mabi=lp64"
 #define MULTILIB_DEFAULTS { "mabi=lp64" }
diff --git a/gcc/config/aarch64/aarch64-protos.h b/gcc/config/aarch64/aarch64-protos.h
index 9e43adb7db0..a408f470777 100644
--- a/gcc/config/aarch64/aarch64-protos.h
+++ b/gcc/config/aarch64/aarch64-protos.h
@@ -743,6 +743,8 @@ extern bool aarch64_classify_address (struct aarch64_address_info *, rtx,
 				      machine_mode, bool,
 				      aarch64_addr_query_type = ADDR_QUERY_M);
 
+extern void aarch64_print_operand (FILE *f, rtx x, int code);
+
 /* Defined in common/config/aarch64-common.c.  */
 bool aarch64_handle_option (struct gcc_options *, struct gcc_options *,
 			     const struct cl_decoded_option *, location_t);
diff --git a/gcc/config/aarch64/aarch64.c b/gcc/config/aarch64/aarch64.c
index 409ca8d9519..c60111273f3 100644
--- a/gcc/config/aarch64/aarch64.c
+++ b/gcc/config/aarch64/aarch64.c
@@ -1424,6 +1424,7 @@ static const struct attribute_spec aarch64_attribute_table[] =
 {
   /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
        affects_type_identity, handler, exclude } */
+  SUBTARGET_ATTRIBUTE_TABLE,
   { "aarch64_vector_pcs", 0, 0, false, true,  true,  true,
 			  handle_aarch64_vector_pcs_attribute, NULL },
   { "arm_sve_vector_bits", 1, 1, false, true,  false, true,
@@ -9953,7 +9954,7 @@ sizetochar (int size)
 			pattern (so the mode needs to be adjusted).
      'z':		Output address of a typical LDP or STP.  */
 
-static void
+void
 aarch64_print_operand (FILE *f, rtx x, int code)
 {
   rtx elt;
@@ -23230,6 +23231,16 @@ aarch64_libgcc_floating_mode_supported_p
 #define TARGET_PREFERRED_ELSE_VALUE \
   aarch64_preferred_else_value
 
+#ifdef SUBTARGET_INSERT_ATTRIBUTES
+#undef TARGET_INSERT_ATTRIBUTES
+#define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
+#endif
+
+#ifdef SUBTARGET_ATTRIBUTE_TABLE
+#undef TARGET_ATTRIBUTE_TABLE
+#define TARGET_ATTRIBUTE_TABLE aarch64_attribute_table
+#endif
+
 #undef TARGET_INIT_LIBFUNCS
 #define TARGET_INIT_LIBFUNCS aarch64_init_libfuncs
 
diff --git a/gcc/config/aarch64/sol2.h b/gcc/config/aarch64/sol2.h
new file mode 100644
index 00000000000..b7df9fbecbb
--- /dev/null
+++ b/gcc/config/aarch64/sol2.h
@@ -0,0 +1,109 @@
+/* Definitions of target machine for GCC, for SPARC running Solaris 2
+   Copyright 1992, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005,
+   2006, 2007, 2008, 2010 Free Software Foundation, Inc.
+   Contributed by Ron Guilmette (rfg@netcom.com).
+   Additional changes by David V. Henkel-Wallace (gumby@cygnus.com).
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#define CPP_SPEC "%{pthread:-D_REENTRANT}"
+#define GNU_USER_DYNAMIC_LINKER "/lib/ld.so.1"
+
+#ifdef TARGET_FIX_ERR_A53_835769_DEFAULT
+#define CA53_ERR_835769_SPEC \
+  " %{!mno-fix-cortex-a53-835769:--fix-cortex-a53-835769}"
+#else
+#define CA53_ERR_835769_SPEC \
+  " %{mfix-cortex-a53-835769:--fix-cortex-a53-835769}"
+#endif
+
+#ifdef TARGET_FIX_ERR_A53_843419_DEFAULT
+#define CA53_ERR_843419_SPEC \
+  " %{!mno-fix-cortex-a53-843419:--fix-cortex-a53-843419}"
+#else
+#define CA53_ERR_843419_SPEC \
+  " %{mfix-cortex-a53-843419:--fix-cortex-a53-843419}"
+#endif
+
+#undef LINK_SPEC
+#define LINK_SPEC "%{G*} %{R*} %{h*}	\
+   %{static:-Bstatic}					\
+   %{shared:-shared}					\
+   %{symbolic:-Bsymbolic}				\
+   %{!static:%{rdynamic:-export-dynamic}}	\
+   -dynamic-linker " GNU_USER_DYNAMIC_LINKER "	\
+   %{mbig-endian:-EB} %{mlittle-endian:-EL} -X	\
+  -maarch64elf%{mabi=ilp32*:32}%{mbig-endian:b}" \
+  CA53_ERR_835769_SPEC \
+  CA53_ERR_843419_SPEC
+
+#undef TARGET_SUN_TLS
+#define TARGET_SUN_TLS 1
+
+#define ASM_OUTPUT_CALL(FILE, FN)			        \
+  do								\
+    {								\
+      fprintf (FILE, "\tbl\t");					\
+      aarch64_print_operand (FILE, XEXP (DECL_RTL (FN), 0), 0);	\
+      fprintf (FILE, "\n");					\
+    }								\
+  while (0)
+
+#undef DEFAULT_SIGNED_CHAR
+#define DEFAULT_SIGNED_CHAR 1
+
+#undef WCHAR_TYPE
+#define WCHAR_TYPE "int"
+#undef WCHAR_TYPE_SIZE
+#define WCHAR_TYPE_SIZE 32
+
+#undef	WINT_TYPE
+#define	WINT_TYPE "int"
+#undef	WINT_TYPE_SIZE
+#define	WINT_TYPE_SIZE 32
+
+#undef	CC1_SPEC
+#define CC1_SPEC "%{!fno-PIC: -fPIC}%{!moutline-atomics: -mno-outline-atomics}"
+
+#undef LIB_SPEC
+#define LIB_SPEC  "%{shared:-lc -lgcc}%{!shared:%{profile:-lc -lgcc}%{!profile:-lc -lgcc}} "
+
+#define SUBTARGET_CPU_EXTRA_SPECS
+
+/* Register the Solaris-specific #pragma directives.  */
+#undef REGISTER_TARGET_PRAGMAS
+#define REGISTER_TARGET_PRAGMAS() do { solaris_register_pragmas (); } while (0)
+
+#undef INIT_SECTION_ASM_OP
+#define INIT_SECTION_ASM_OP	"\t.section\t.init, \"ax\""
+
+#undef FINI_SECTION_ASM_OP
+#define FINI_SECTION_ASM_OP	"\t.section\t.fini, \"ax\""
+
+#undef EXTRA_SPECS
+#define EXTRA_SPECS \
+  { "asm_cpu_spec",		ASM_CPU_SPEC },	\
+  { "startfile_arch",	 	STARTFILE_ARCH_SPEC },		\
+  { "startfile_crtbegin",	STARTFILE_CRTBEGIN_SPEC },	\
+  { "endfile_arch",	 	ENDFILE_ARCH_SPEC },		\
+  { "endfile_crtend",		ENDFILE_CRTEND_SPEC },		\
+  SUBTARGET_CPU_EXTRA_SPECS
+
+#undef SUBTARGET_EXTRA_SPECS
+#define SUBTARGET_EXTRA_SPECS
+#define ENDFILE_ARCH_SPEC ""
+
diff --git a/gcc/config/aarch64/t-sol2 b/gcc/config/aarch64/t-sol2
new file mode 100644
index 00000000000..5aa36dffda0
--- /dev/null
+++ b/gcc/config/aarch64/t-sol2
@@ -0,0 +1,25 @@
+# Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# Compile crtbeginS.o and crtendS.o with pic.
+CRTSTUFF_T_CFLAGS_S = -fPIC
+
+# Compile libgcc2.a with pic.
+TARGET_LIBGCC2_CFLAGS += -fPIC
+
+EXTRA_MULTILIB_PARTS =
diff --git a/gcc/config/alpha/alpha-protos.h b/gcc/config/alpha/alpha-protos.h
index ba4cb3bf9cd..1ffa4cd32c7 100644
--- a/gcc/config/alpha/alpha-protos.h
+++ b/gcc/config/alpha/alpha-protos.h
@@ -117,3 +117,6 @@ class rtl_opt_pass;
 
 extern rtl_opt_pass *make_pass_handle_trap_shadows (gcc::context *);
 extern rtl_opt_pass *make_pass_align_insns (gcc::context *);
+
+extern void alpha_print_operand (FILE *file, rtx x, int code);
+
diff --git a/gcc/config/alpha/alpha.c b/gcc/config/alpha/alpha.c
index 7e5d1cbb0c4..c50f163ebe5 100644
--- a/gcc/config/alpha/alpha.c
+++ b/gcc/config/alpha/alpha.c
@@ -5057,7 +5057,7 @@ alpha_print_operand_punct_valid_p (unsigned char code)
 /* Implement TARGET_PRINT_OPERAND.  The alpha-specific
    operand codes are documented below.  */
 
-static void
+void
 alpha_print_operand (FILE *file, rtx x, int code)
 {
   int i;
@@ -9847,6 +9847,27 @@ alpha_can_change_mode_class (machine_mode from, machine_mode to,
 }
 
 /* Initialize the GCC target structure.  */
+#ifdef SUBTARGET_ATTRIBUTE_TABLE
+/* Table of valid machine attributes.  */
+static const struct attribute_spec alpha_attribute_table[] =
+{
+  /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
+       affects_type_identity, handler, exclude } */
+  SUBTARGET_ATTRIBUTE_TABLE,
+  { NULL, 0, 0, false, false, false, false, NULL, NULL }
+};
+#endif
+
+#ifdef SUBTARGET_INSERT_ATTRIBUTES
+#undef TARGET_INSERT_ATTRIBUTES
+#define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
+#endif
+
+#ifdef SUBTARGET_ATTRIBUTE_TABLE
+#undef TARGET_ATTRIBUTE_TABLE
+#define TARGET_ATTRIBUTE_TABLE alpha_attribute_table
+#endif
+
 #if TARGET_ABI_OPEN_VMS
 # undef TARGET_ATTRIBUTE_TABLE
 # define TARGET_ATTRIBUTE_TABLE vms_attribute_table
diff --git a/gcc/config/alpha/sol2.h b/gcc/config/alpha/sol2.h
new file mode 100644
index 00000000000..cccadfddcca
--- /dev/null
+++ b/gcc/config/alpha/sol2.h
@@ -0,0 +1,113 @@
+/* Definitions of target machine for GCC, for SPARC running Solaris 2
+   Copyright 1992, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2005,
+   2006, 2007, 2008, 2010 Free Software Foundation, Inc.
+   Contributed by Ron Guilmette (rfg@netcom.com).
+   Additional changes by David V. Henkel-Wallace (gumby@cygnus.com).
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+/* We are compiling for Solaris 2 now.  */
+#undef TARGET_DEFAULT
+#define TARGET_DEFAULT MASK_FPREGS
+
+/* Show that we need a GP when profiling.  */
+#undef TARGET_PROFILING_NEEDS_GP
+#define TARGET_PROFILING_NEEDS_GP 1
+
+/* This macro closes up a function definition for the assembler.  */
+#undef ASM_DECLARE_FUNCTION_SIZE
+#define ASM_DECLARE_FUNCTION_SIZE(FILE, NAME, DECL)		\
+  do								\
+    {								\
+      alpha_end_function(FILE,NAME,DECL);			\
+      solaris_output_init_fini (FILE, DECL);			\
+    }								\
+  while (0)
+
+#define CPP_SPEC "%{pthread:-D_REENTRANT}"
+
+#define GNU_USER_DYNAMIC_LINKER "/lib/ld.so.1"
+
+#undef LINK_SPEC
+#define LINK_SPEC  "%{G*} %{R*}  %{h*}		\
+   %{static:-Bstatic}				\
+   %{shared:-shared}				\
+   %{symbolic:-Bsymbolic}			\
+   %{rdynamic:-export-dynamic}			\
+   -dynamic-linker " GNU_USER_DYNAMIC_LINKER "	\
+   %{relax:-relax}				\
+   -X						\
+   -melf64alpha"
+
+#undef TARGET_SUN_TLS
+#define TARGET_SUN_TLS 1
+
+#define ASM_OUTPUT_CALL(FILE, FN)			        \
+  do								\
+    {								\
+      fprintf (FILE, "\t.set macro\n");				\
+      fprintf (FILE, "\tjsr\t$26, ");				\
+      alpha_print_operand (FILE, XEXP (DECL_RTL (FN), 0), 0);	\
+      fprintf (FILE, "\n");					\
+      fprintf (FILE, "\tldgp\t$29, 0($26)\n");			\
+      fprintf (FILE, "\t.set nomacro\n");			\
+    }								\
+  while (0)
+
+#undef DEFAULT_SIGNED_CHAR
+#define DEFAULT_SIGNED_CHAR 1
+
+#undef WCHAR_TYPE
+#define WCHAR_TYPE "int"
+#undef WCHAR_TYPE_SIZE
+#define WCHAR_TYPE_SIZE 32
+
+#undef	WINT_TYPE
+#define	WINT_TYPE "int"
+#undef	WINT_TYPE_SIZE
+#define	WINT_TYPE_SIZE 32
+
+#undef	CC1_SPEC
+#define CC1_SPEC "%{!fno-PIC:-fPIC}"
+
+#undef LIB_SPEC
+#define LIB_SPEC  "%{shared:-lc -lgcc}%{!shared:%{profile:-lc -lgcc}%{!profile:-lc -lgcc}} "
+
+#define SUBTARGET_CPU_EXTRA_SPECS
+
+/* Register the Solaris-specific #pragma directives.  */
+#undef REGISTER_TARGET_PRAGMAS
+#define REGISTER_TARGET_PRAGMAS() do { solaris_register_pragmas (); } while (0)
+
+#undef INIT_SECTION_ASM_OP
+#define INIT_SECTION_ASM_OP	"\t.section\t.init, \"ax\""
+
+#undef FINI_SECTION_ASM_OP
+#define FINI_SECTION_ASM_OP	"\t.section\t.fini, \"ax\""
+
+#undef EXTRA_SPECS
+#define EXTRA_SPECS \
+  { "startfile_arch",	 	STARTFILE_ARCH_SPEC },		\
+  { "startfile_crtbegin",	STARTFILE_CRTBEGIN_SPEC },	\
+  { "endfile_arch",	 	ENDFILE_ARCH_SPEC },		\
+  { "endfile_crtend",		ENDFILE_CRTEND_SPEC },		\
+  SUBTARGET_CPU_EXTRA_SPECS
+
+#undef SUBTARGET_EXTRA_SPECS
+#define SUBTARGET_EXTRA_SPECS
+#define ENDFILE_ARCH_SPEC ""
+
diff --git a/gcc/config/alpha/t-sol2 b/gcc/config/alpha/t-sol2
new file mode 100644
index 00000000000..5aa36dffda0
--- /dev/null
+++ b/gcc/config/alpha/t-sol2
@@ -0,0 +1,25 @@
+# Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# Compile crtbeginS.o and crtendS.o with pic.
+CRTSTUFF_T_CFLAGS_S = -fPIC
+
+# Compile libgcc2.a with pic.
+TARGET_LIBGCC2_CFLAGS += -fPIC
+
+EXTRA_MULTILIB_PARTS =
diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h
index 8cf9137b5e7..4dc372529d9 100644
--- a/gcc/config/riscv/riscv-protos.h
+++ b/gcc/config/riscv/riscv-protos.h
@@ -74,6 +74,8 @@ extern rtx riscv_function_value (const_tree, const_tree, enum machine_mode);
 extern bool riscv_expand_block_move (rtx, rtx, rtx);
 extern bool riscv_store_data_bypass_p (rtx_insn *, rtx_insn *);
 
+extern void riscv_print_operand (FILE*, rtx, int);
+
 /* Routines implemented in riscv-c.c.  */
 void riscv_cpu_cpp_builtins (cpp_reader *);
 
diff --git a/gcc/config/riscv/riscv.c b/gcc/config/riscv/riscv.c
index 94b5ac01762..2cffb2fe91b 100644
--- a/gcc/config/riscv/riscv.c
+++ b/gcc/config/riscv/riscv.c
@@ -321,6 +321,7 @@ static const struct attribute_spec riscv_attribute_table[] =
   /* Syntax: { name, min_len, max_len, decl_required, type_required,
 	       function_type_required, affects_type_identity, handler,
 	       exclude } */
+  SUBTARGET_ATTRIBUTE_TABLE,
 
   /* The attribute telling no prologue/epilogue.  */
   { "naked",	0,  0, true, false, false, false,
@@ -3278,7 +3279,7 @@ riscv_memmodel_needs_release_fence (enum memmodel model)
    'z'	Print x0 if OP is zero, otherwise print OP normally.
    'i'	Print i if the operand is not a register.  */
 
-static void
+void
 riscv_print_operand (FILE *file, rtx op, int letter)
 {
   machine_mode mode = GET_MODE (op);
@@ -5213,6 +5214,11 @@ riscv_hard_regno_rename_ok (unsigned from_regno ATTRIBUTE_UNUSED,
 #undef TARGET_MERGE_DECL_ATTRIBUTES
 #define TARGET_MERGE_DECL_ATTRIBUTES riscv_merge_decl_attributes
 
+#ifdef SUBTARGET_INSERT_ATTRIBUTES
+#undef TARGET_INSERT_ATTRIBUTES
+#define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
+#endif
+
 #undef TARGET_ATTRIBUTE_TABLE
 #define TARGET_ATTRIBUTE_TABLE riscv_attribute_table
 
diff --git a/gcc/config/riscv/sol2.h b/gcc/config/riscv/sol2.h
new file mode 100644
index 00000000000..5c92bfaffa2
--- /dev/null
+++ b/gcc/config/riscv/sol2.h
@@ -0,0 +1,92 @@
+/* Definitions for RISC-V GNU/Linux systems with ELF format.
+   Copyright (C) 1998-2018 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+
+#define CPP_SPEC "%{pthread:-D_REENTRANT}"
+#define GNU_USER_DYNAMIC_LINKER "/lib/ld.so.1"
+
+#undef LINK_SPEC
+#define LINK_SPEC "%{G*} %{R*} %{h*}	\
+   %{mno-relax:--no-relax}				\
+   %{static:-Bstatic}					\
+   %{shared:-shared}					\
+   %{symbolic:-Bsymbolic}				\
+   %{!static:%{rdynamic:-export-dynamic}}	\
+   -dynamic-linker " GNU_USER_DYNAMIC_LINKER "	\
+  -melf" XLEN_SPEC "lriscv"
+
+#undef TARGET_SUN_TLS
+#define TARGET_SUN_TLS 1
+
+#define ASM_OUTPUT_CALL(FILE, FN)			        \
+  do								\
+    {								\
+      fprintf (FILE, "\tcall\t");				\
+      riscv_print_operand (FILE, XEXP (DECL_RTL (FN), 0), 0);	\
+      fprintf (FILE, "\n");					\
+    }								\
+  while (0)
+
+#undef DEFAULT_SIGNED_CHAR
+#define DEFAULT_SIGNED_CHAR 1
+
+#undef WCHAR_TYPE
+#define WCHAR_TYPE "int"
+#undef WCHAR_TYPE_SIZE
+#define WCHAR_TYPE_SIZE 32
+
+#undef	WINT_TYPE
+#define	WINT_TYPE "int"
+#undef	WINT_TYPE_SIZE
+#define	WINT_TYPE_SIZE 32
+
+#undef	CC1_SPEC
+#define CC1_SPEC "%{!fno-PIC:-fPIC}"
+
+#undef LIB_SPEC
+#define LIB_SPEC  "%{shared:-lc -lgcc}%{!shared:%{profile:-lc -lgcc}%{!profile:-lc -lgcc}} "
+
+#define SUBTARGET_CPU_EXTRA_SPECS
+
+/* Register the Solaris-specific #pragma directives.  */
+#undef REGISTER_TARGET_PRAGMAS
+#define REGISTER_TARGET_PRAGMAS() do { solaris_register_pragmas (); } while (0)
+
+#undef INIT_SECTION_ASM_OP
+#define INIT_SECTION_ASM_OP	"\t.section\t.init, \"ax\",@progbits"
+
+#undef FINI_SECTION_ASM_OP
+#define FINI_SECTION_ASM_OP	"\t.section\t.fini, \"ax\",@progbits"
+
+#undef EXTRA_SPECS
+#define EXTRA_SPECS \
+  { "startfile_arch",	 	STARTFILE_ARCH_SPEC },		\
+  { "startfile_crtbegin",	STARTFILE_CRTBEGIN_SPEC },	\
+  { "endfile_arch",	 	ENDFILE_ARCH_SPEC },		\
+  { "endfile_crtend",		ENDFILE_CRTEND_SPEC },		\
+  SUBTARGET_CPU_EXTRA_SPECS
+
+#undef SUBTARGET_EXTRA_SPECS
+#define SUBTARGET_EXTRA_SPECS
+#define ENDFILE_ARCH_SPEC ""
+
+#define ICACHE_FLUSH_FUNC "__riscv_flush_icache"
+
+#undef MULTILIB_DEFAULTS
+#define MULTILIB_DEFAULTS { "march=rv64gc", "mabi=lp64d" }
diff --git a/gcc/config/riscv/t-sol2 b/gcc/config/riscv/t-sol2
new file mode 100644
index 00000000000..5aa36dffda0
--- /dev/null
+++ b/gcc/config/riscv/t-sol2
@@ -0,0 +1,25 @@
+# Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# Compile crtbeginS.o and crtendS.o with pic.
+CRTSTUFF_T_CFLAGS_S = -fPIC
+
+# Compile libgcc2.a with pic.
+TARGET_LIBGCC2_CFLAGS += -fPIC
+
+EXTRA_MULTILIB_PARTS =
diff --git a/gcc/config/riscv/t-sol2-multilib b/gcc/config/riscv/t-sol2-multilib
new file mode 100644
index 00000000000..7c20fc503c7
--- /dev/null
+++ b/gcc/config/riscv/t-sol2-multilib
@@ -0,0 +1,10 @@
+# This file was generated by multilib-generator with the command:
+#  ./multilib-generator rv64imac-lp64-- rv64imafdc-lp64d--
+MULTILIB_OPTIONS = march=rv64imac/march=rv64imafdc/march=rv64gc mabi=lp64/mabi=lp64d
+MULTILIB_DIRNAMES = rv64imac \
+rv64imafdc \
+rv64gc lp64 \
+lp64d
+MULTILIB_REQUIRED = march=rv64imac/mabi=lp64 \
+march=rv64imafdc/mabi=lp64d
+MULTILIB_REUSE = march.rv64imafdc/mabi.lp64d=march.rv64gc/mabi.lp64d
diff --git a/gcc/config/sol2-c.c b/gcc/config/sol2-c.c
index 9eaf43c4c98..91a56758db8 100644
--- a/gcc/config/sol2-c.c
+++ b/gcc/config/sol2-c.c
@@ -260,6 +260,11 @@ solaris_pragma_fini (cpp_reader *pfile ATTRIBUTE_UNUSED)
     }
 }
 
+static void
+solaris_pragma_ident (cpp_reader *pfile ATTRIBUTE_UNUSED)
+{
+}
+
 /* Register Solaris-specific #pragma directives.  */
 
 void
@@ -268,4 +273,5 @@ solaris_register_pragmas (void)
   c_register_pragma_with_expansion (0, "align", solaris_pragma_align);
   c_register_pragma (0, "init", solaris_pragma_init);
   c_register_pragma (0, "fini", solaris_pragma_fini);
+  c_register_pragma (0, "ident", solaris_pragma_ident);
 }
diff --git a/gcc/config/sol2.h b/gcc/config/sol2.h
index 91b4602b49d..cd1dac6601e 100644
--- a/gcc/config/sol2.h
+++ b/gcc/config/sol2.h
@@ -454,6 +454,7 @@ along with GCC; see the file COPYING3.  If not see
 
 /* collect2.c can only parse GNU nm -n output.  Solaris nm needs -png to
    produce the same format.  */
+#undef NM_FLAGS
 #define NM_FLAGS "-png"
 
 #define STDC_0_IN_SYSTEM_HEADERS 1
diff --git a/gcc/configure b/gcc/configure
index cd3d9516fce..81f3dd28153 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -24682,6 +24682,14 @@ foo:	.long	25
 	tls_first_major=2
 	tls_first_minor=13
 	tls_as_opt=--fatal-warnings
+	case "$target" in
+		alpha*-*-solaris2.*)
+			on_solaris=yes
+			tga_func=__tls_get_addr
+		;;
+		*)
+		;;
+	esac
 	;;
   arc*-*-*)
     conftest_s='
@@ -25562,7 +25570,7 @@ fi
     ;;
 
   # All TARGET_ABI_OSF targets.
-  alpha*-*-linux* | alpha*-*-*bsd*)
+  alpha*-*-linux* | alpha*-*-*bsd* | alpha*-*-solaris2*)
     { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for explicit relocation support" >&5
 $as_echo_n "checking assembler for explicit relocation support... " >&6; }
 if ${gcc_cv_as_alpha_explicit_relocs+:} false; then :
@@ -29924,7 +29932,8 @@ case "$target" in
   *-*-solaris2*)
     # <link.h> needs both a dl_iterate_phdr declaration and support for
     # compilation with largefile support.
-    if grep dl_iterate_phdr $target_header_dir/link.h > /dev/null 2>&1 \
+    if test x"$gnu_ld" = xno \
+      && grep dl_iterate_phdr $target_header_dir/link.h > /dev/null 2>&1 \
       && grep 'large file capable' $target_header_dir/link.h > /dev/null 2>&1; then
       gcc_cv_target_dl_iterate_phdr=yes
     else
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 0de3b4bf97b..87d1aa0d2e7 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -3446,6 +3446,14 @@ foo:	.long	25
 	tls_first_major=2
 	tls_first_minor=13
 	tls_as_opt=--fatal-warnings
+	case "$target" in
+		alpha*-*-solaris2.*)
+			on_solaris=yes
+			tga_func=__tls_get_addr
+		;;
+		*)
+		;;
+	esac
 	;;
   arc*-*-*)
     conftest_s='
@@ -4210,7 +4218,7 @@ AS_HELP_STRING([--disable-fix-cortex-a53-843419],
     ;;
 
   # All TARGET_ABI_OSF targets.
-  alpha*-*-linux* | alpha*-*-*bsd*)
+  alpha*-*-linux* | alpha*-*-*bsd* | alpha*-*-solaris2*)
     gcc_GAS_CHECK_FEATURE([explicit relocation support],
 	gcc_cv_as_alpha_explicit_relocs, [2,12,0],,
 [	.set nomacro
@@ -6305,7 +6313,8 @@ case "$target" in
   *-*-solaris2*)
     # <link.h> needs both a dl_iterate_phdr declaration and support for
     # compilation with largefile support.
-    if grep dl_iterate_phdr $target_header_dir/link.h > /dev/null 2>&1 \
+    if test x"$gnu_ld" = xno \
+      && grep dl_iterate_phdr $target_header_dir/link.h > /dev/null 2>&1 \
       && grep 'large file capable' $target_header_dir/link.h > /dev/null 2>&1; then
       gcc_cv_target_dl_iterate_phdr=yes
     else
diff --git a/gcc/genmddeps.c b/gcc/genmddeps.c
index 7e9da41e1f9..8a6c75b42c1 100644
--- a/gcc/genmddeps.c
+++ b/gcc/genmddeps.c
@@ -15,7 +15,11 @@
    along with this program; see the file COPYING3.  If not see
    <http://www.gnu.org/licenses/>.  */
 
+#ifdef GENERATOR_FILE
 #include "bconfig.h"
+#else
+#include "config.h"
+#endif
 #include "system.h"
 #include "coretypes.h"
 #include "errors.h"
diff --git a/libgcc/config.host b/libgcc/config.host
index 1ff41592337..391d22af192 100644
--- a/libgcc/config.host
+++ b/libgcc/config.host
@@ -297,6 +297,9 @@ case ${host} in
     extra_parts="$extra_parts crtbeginS.o crtendS.o"
   else
     case ${host} in
+      aarch64*-*-solaris2* | alpha*-*-solaris2* | riscv64-*-solaris2*)
+        extra_parts="crtbegin.o crtend.o"
+        ;;
       i?86-*-solaris2* | x86_64-*-solaris2*)
         # Solaris 10+/x86 provides crt1.o, crti.o, crtn.o, and gcrt1.o as
         # part of the base system.
@@ -343,6 +346,9 @@ arm-*-vxworks*)
 esac
 
 case ${host} in
+aarch64*-*-solaris2* | alpha*-*-solaris2* | riscv64-*-solaris2*)
+  enable_execute_stack=enable-execute-stack-empty.c;
+  ;;
 *-*-darwin* | *-*-dragonfly* | *-*-freebsd* | *-*-netbsd* | *-*-openbsd* | \
   *-*-solaris2*)
   enable_execute_stack=enable-execute-stack-mprotect.c
@@ -398,11 +404,21 @@ aarch64*-*-vxworks7*)
 	tmake_file="${tmake_file} ${cpu_type}/t-aarch64"
 	tmake_file="${tmake_file} ${cpu_type}/t-softfp t-softfp t-crtfm"
 	;;
+aarch64*-*-solaris2*)
+	tmake_file="${tmake_file} ${cpu_type}/t-aarch64 aarch64/t-sol2"
+	tmake_file="${tmake_file} ${cpu_type}/t-softfp t-softfp t-crtfm"
+	extra_parts="$extra_parts crtfastmath.o crtbeginS.o crtendS.o"
+	md_unwind_header=aarch64/aarch64-unwind.h
+	;;
 alpha*-*-linux*)
 	tmake_file="${tmake_file} alpha/t-alpha alpha/t-ieee t-crtfm alpha/t-linux"
 	extra_parts="$extra_parts crtfastmath.o"
 	md_unwind_header=alpha/linux-unwind.h
 	;;
+alpha*-*-solaris2*)
+	tmake_file="${tmake_file} alpha/t-alpha alpha/t-ieee t-crtfm alpha/t-sol2"
+	extra_parts="$extra_parts crtfastmath.o crtbeginS.o crtendS.o"
+	;;
 alpha*-*-freebsd*)
 	tmake_file="${tmake_file} alpha/t-alpha alpha/t-ieee t-crtfm"
 	extra_parts="$extra_parts crtbeginT.o crtfastmath.o"
@@ -1252,6 +1268,11 @@ pru-*-*)
 	tmake_file="${tmake_file} t-softfp-sfdf t-softfp-excl t-softfp t-gnu-prefix pru/t-pru"
 	tm_file="$tm_file pru/pru-abi.h"
 	;;
+riscv64-*-solaris2*)
+	tmake_file="${tmake_file} riscv/t-softfp${host_address} t-softfp riscv/t-elf riscv/t-elf${host_address}"
+	tmake_file="${tmake_file} riscv/t-sol2"
+	extra_parts="$extra_parts crtfastmath.o crtbeginS.o crtendS.o"
+	;;
 riscv*-*-linux*)
 	tmake_file="${tmake_file} riscv/t-softfp${host_address} t-softfp riscv/t-elf riscv/t-elf${host_address} t-slibgcc-libgcc"
 	extra_parts="$extra_parts crtbegin.o crtend.o crti.o crtn.o crtendS.o crtbeginT.o"
diff --git a/libgcc/config/alpha/t-sol2 b/libgcc/config/alpha/t-sol2
new file mode 100644
index 00000000000..ea3fa63a272
--- /dev/null
+++ b/libgcc/config/alpha/t-sol2
@@ -0,0 +1,6 @@
+# We need to use -fPIC when we are using gcc to compile the routines in
+# crtstuff.c.  This is only really needed when we are going to use gcc/g++
+# to produce a shared library, but since we don't know ahead of time when
+# we will be doing that, we just always use -fPIC when compiling the
+# routines in crtstuff.c.
+CRTSTUFF_T_CFLAGS = $(PICFLAG)
diff --git a/libgcc/config/riscv/atomic.c b/libgcc/config/riscv/atomic.c
index e556e343362..cb9ed374f49 100644
--- a/libgcc/config/riscv/atomic.c
+++ b/libgcc/config/riscv/atomic.c
@@ -96,6 +96,30 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     return __sync_val_compare_and_swap(p, o, n) == o;			\
   }
 
+#define GENERATE_TEST_AND_SET(type, size)				\
+  type __sync_lock_test_and_set_ ## size (type *p, type n)		\
+  {									\
+    unsigned long aligned_addr = ((unsigned long) p) & ~3UL;		\
+    int shift = (((unsigned long) p) & 3) * 8;				\
+    unsigned mask = ((1U << ((sizeof n) * 8)) - 1) << shift;		\
+    unsigned old, tmp1;							\
+									\
+    asm volatile ("1:\n\t"						\
+		  "lr.w.aq %[old], %[mem]\n\t"				\
+		  "and %[tmp1], %[old], %[not_mask]\n\t"		\
+		  "or %[tmp1], %[tmp1], %[n]\n\t"			\
+		  "sc.w.rl %[tmp1], %[tmp1], %[mem]\n\t"		\
+		  "bnez %[tmp1], 1b\n\t"				\
+		  "1:"							\
+		  : [old] "=&r" (old),					\
+		    [mem] "+A" (*(volatile unsigned*) aligned_addr),	\
+		    [tmp1] "=&r" (tmp1)					\
+		  : [n] "r" ((((unsigned) n) << shift) & mask),		\
+		    [not_mask] "r" (~mask));				\
+									\
+    return (type) (old >> shift);					\
+  }
+
 #define GENERATE_ALL(type, size)					\
   GENERATE_FETCH_AND_OP(type, size, add, add, DONT_INVERT, o + v)	\
   GENERATE_FETCH_AND_OP(type, size, sub, sub, DONT_INVERT, o - v)	\
@@ -103,7 +127,8 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   GENERATE_FETCH_AND_OP(type, size, xor, xor, DONT_INVERT, o ^ v)	\
   GENERATE_FETCH_AND_OP(type, size, or, or, DONT_INVERT, o | v)		\
   GENERATE_FETCH_AND_OP(type, size, nand, and, INVERT, ~(o & v))	\
-  GENERATE_COMPARE_AND_SWAP(type, size)
+  GENERATE_COMPARE_AND_SWAP(type, size)					\
+  GENERATE_TEST_AND_SET(type, size)
 
 GENERATE_ALL(unsigned char, 1)
 GENERATE_ALL(unsigned short, 2)
diff --git a/libstdc++-v3/src/c++98/locale_init.cc b/libstdc++-v3/src/c++98/locale_init.cc
index c3841ccbd3c..7c7d07b8e59 100644
--- a/libstdc++-v3/src/c++98/locale_init.cc
+++ b/libstdc++-v3/src/c++98/locale_init.cc
@@ -69,12 +69,12 @@ namespace
 
   using namespace std;
 
-  typedef char fake_locale_Impl[sizeof(locale::_Impl)]
-  __attribute__ ((aligned(__alignof__(locale::_Impl))));
+  typedef char fake_locale_Impl[sizeof(std::locale::_Impl)]
+  __attribute__ ((aligned(__alignof__(std::locale::_Impl))));
   fake_locale_Impl c_locale_impl;
 
-  typedef char fake_locale[sizeof(locale)]
-  __attribute__ ((aligned(__alignof__(locale))));
+  typedef char fake_locale[sizeof(std::locale)]
+  __attribute__ ((aligned(__alignof__(std::locale))));
   fake_locale c_locale;
 
   typedef char fake_name_vec[sizeof(char*)]
@@ -219,7 +219,7 @@ namespace
   fake_codecvt_c32_c8 codecvt_c32_c8;
 #endif
 
-  // Storage for "C" locale caches.
+  // Storage for "C" std::locale caches.
   typedef char fake_num_cache_c[sizeof(std::__numpunct_cache<char>)]
   __attribute__ ((aligned(__alignof__(std::__numpunct_cache<char>))));
   fake_num_cache_c numpunct_cache_c;
