diff --git a/gcc/config.in b/gcc/config.in
index 7fcabbe5061..eb6024dfc90 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -379,6 +379,12 @@
 #endif
 
 
+/* Define if your assembler supports gcs build attributes. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_AS_BUILD_ATTRIBUTES_GCS
+#endif
+
+
 /* Define to the level of your assembler's compressed debug section support.
    */
 #ifndef USED_FOR_TARGET
diff --git a/gcc/config/aarch64/a.out b/gcc/config/aarch64/a.out
new file mode 100644
index 0000000000000000000000000000000000000000..dd7982f2db625be166d33548dc5d00c7e7601629
GIT binary patch
diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 8709f2b0cfb..2cff5317fc1 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -24679,6 +24679,33 @@ aarch64_post_cfi_startproc (FILE *f, tree ignored ATTRIBUTE_UNUSED)
 	asm_fprintf (f, "\t.cfi_b_key_frame\n");
 }
 
+/* This function is used to emit an AEABI attribute with tag and its associated
+   value.  We emit the numerical value of the tag and the textual tags as
+   comment so that anyone reading the assembler output will know which tag is
+   being set.
+   example: .aeabi_attribute 3, 1 // Tag_Feature_GCS  */
+
+void
+aarch64_emit_aeabi_attribute (const char *name, uint8_t num, uint8_t val)
+{
+  asm_fprintf (asm_out_file, "\t.aeabi_attribute %u, %u", num, val);
+  if (flag_debug_asm)
+    asm_fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, name);
+  asm_fprintf (asm_out_file, "\n");
+}
+
+/* This function is used to emit an AEABI subsection with vendor name,
+   optional status and value type.
+   example: .aeabi_subsection .aeabi-feature-and-bits, 1, 0  */
+
+void
+aarch64_emit_aeabi_subsection (const char *name, uint8_t num, uint8_t val)
+{
+  asm_fprintf (asm_out_file, "\t.aeabi_subsection %s, %u, %u\n",
+	       name, num, val);
+}
+
+
 /* Implements TARGET_ASM_FILE_START.  Output the assembly header.  */
 
 static void
@@ -24699,6 +24726,20 @@ aarch64_start_file (void)
    asm_fprintf (asm_out_file, "\t.arch %s\n",
 		aarch64_last_printed_arch_string.c_str ());
 
+/* Emit gcs build attributes only when building a native AArch64-hosted
+   compiler.  */
+#if defined(__aarch64__)
+  /* Check the current assembly supports gcs build attributes, if not
+     fallback to .note.gnu.property section.  */
+  #ifdef HAVE_AS_BUILD_ATTRIBUTES_GCS
+    if (aarch64_gcs_enabled ())
+      {
+	aarch64_emit_aeabi_subsection (".aeabi-feature-and-bits", 1, 0);
+	aarch64_emit_aeabi_attribute ("Tag_Feature_GCS", 3, 1);
+      }
+  #endif
+#endif
+
    default_file_start ();
 }
 
@@ -29105,8 +29146,10 @@ aarch64_file_end_indicate_exec_stack ()
   if (aarch_ra_sign_scope != AARCH_FUNCTION_NONE)
     feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_PAC;
 
+#ifndef HAVE_AS_BUILD_ATTRIBUTES_GCS
   if (aarch64_gcs_enabled ())
     feature_1_and |= GNU_PROPERTY_AARCH64_FEATURE_1_GCS;
+#endif
 
   if (feature_1_and)
     {
diff --git a/gcc/configure b/gcc/configure
index ec6124cb361..bec606f61b3 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -28036,6 +28036,41 @@ if test $gcc_cv_as_aarch64_picreloc = yes; then
 
 $as_echo "#define HAVE_AS_SMALL_PIC_RELOCS 1" >>confdefs.h
 
+fi
+
+    # Check if we have binutils support for gcs build attributes.
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for gcs build attributes support" >&5
+$as_echo_n "checking assembler for gcs build attributes support... " >&6; }
+if ${gcc_cv_as_aarch64_gcs_build_attributes+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  gcc_cv_as_aarch64_gcs_build_attributes=no
+  if test x$gcc_cv_as != x; then
+    $as_echo '
+	.aeabi_subsection .aeabi-feature-and-bits, 1, 0
+	.aeabi_attribute 3, 1
+    ' > conftest.s
+    if { ac_try='$gcc_cv_as $gcc_cv_as_flags  -o conftest.o conftest.s >&5'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+    then
+	gcc_cv_as_aarch64_gcs_build_attributes=yes
+    else
+      echo "configure: failed program was" >&5
+      cat conftest.s >&5
+    fi
+    rm -f conftest.o conftest.s
+  fi
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_as_aarch64_gcs_build_attributes" >&5
+$as_echo "$gcc_cv_as_aarch64_gcs_build_attributes" >&6; }
+if test $gcc_cv_as_aarch64_gcs_build_attributes = yes; then
+
+$as_echo "#define HAVE_AS_BUILD_ATTRIBUTES_GCS 1" >>confdefs.h
+
 fi
 
     # Enable Branch Target Identification Mechanism and Return Address
diff --git a/gcc/configure.ac b/gcc/configure.ac
index d0b9865fc91..51b07417153 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -4388,6 +4388,13 @@ case "$target" in
 	ldr     x0, [[x2, #:gotpage_lo15:globalsym]]
     ],,[AC_DEFINE(HAVE_AS_SMALL_PIC_RELOCS, 1,
 	[Define if your assembler supports relocs needed by -fpic.])])
+    # Check if we have binutils support for gcs build attributes.
+    gcc_GAS_CHECK_FEATURE([gcs build attributes support], gcc_cv_as_aarch64_gcs_build_attributes,,
+    [
+	.aeabi_subsection .aeabi-feature-and-bits, 1, 0
+	.aeabi_attribute 3, 1
+    ],,[AC_DEFINE(HAVE_AS_BUILD_ATTRIBUTES_GCS, 1,
+	[Define if your assembler supports gcs build attributes.])])
     # Enable Branch Target Identification Mechanism and Return Address
     # Signing by default.
     AC_ARG_ENABLE(standard-branch-protection,
diff --git a/gcc/testsuite/gcc.target/aarch64/build-attribute-gcs.c b/gcc/testsuite/gcc.target/aarch64/build-attribute-gcs.c
new file mode 100644
index 00000000000..eb15772757e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/build-attribute-gcs.c
@@ -0,0 +1,24 @@
+/* { dg-do compile { target aarch64*-*-linux* } } */
+
+int main()
+{
+  return 0;
+}
+
+/* { dg-options "-mbranch-protection=gcs" } */
+/* { dg-final { scan-assembler-not "\.aeabi_subsection \.aeabi-feature-and-bits, 1, 0" } } */
+/* { dg-final { scan-assembler-not "\.aeabi_attribute 3, 1\t\/\/ Tag_Feature_GCS" } } */
+/* { dg-final { scan-assembler ".note.gnu.property" } } */
+
+/* { dg-options "-mbranch-protection=bti" } */
+/* { dg-final { scan-assembler ".note.gnu.property" } } */
+
+
+/* { dg-options "-mbranch-protection=pac-ret" } */
+/* { dg-final { scan-assembler ".note.gnu.property" } } */
+
+
+/* { dg-options "-mbranch-protection=standard" } */
+/* { dg-final { scan-assembler-not "\.aeabi_subsection \.aeabi-feature-and-bits, 1, 0" } } */
+/* { dg-final { scan-assembler-not "\.aeabi_attribute 3, 1\t\/\/ Tag_Feature_GCS" } } */
+/* { dg-final { scan-assembler ".note.gnu.property" } } */
