Since this is an RFC, it would probably be more helpful to get review
comments about the design or the adherence to the spec. I'll need to
look into things a bit more for that, though, so I'm afraid the below
is more implementation trivia.
Matthieu Longo <matthieu.longo@arm.com> writes:
> [...]
> diff --git a/gcc/config.in b/gcc/config.in
> index 7fcabbe5061..1309ba2b133 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
How about making this HAVE_AS_BUILD_AEABI_ATTRIBUTE? Or is the idea
that each feature that uses build attributes would need its own
configure check?
> [...]
> @@ -24697,6 +24724,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__)
Why is this restricted to native compilers? We should avoid that
if at all possible.
> + /* Check the current assembly supports gcs build attributes, if not
"Check whether the current assembler supports..."
> + fallback to .note.gnu.property section. */
> + #ifdef HAVE_AS_BUILD_ATTRIBUTES_GCS
It would be good to add:
#ifndef HAVE_AS_BUILD_ATTRIBUTES_GCS
#define HAVE_AS_BUILD_ATTRIBUTES_GCS 0
#endif
somewhere, so that we can use HAVE_AS_BUILD_ATTRIBUTES_GCS in C++
conditions as well as preprocessor conditions.
> + 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 ();
> }
> [...]
> diff --git a/gcc/testsuite/gcc.target/aarch64/build-attributes/build-attributes.exp b/gcc/testsuite/gcc.target/aarch64/build-attributes/build-attributes.exp
> new file mode 100644
> index 00000000000..ea47e209227
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/aarch64/build-attributes/build-attributes.exp
> @@ -0,0 +1,46 @@
> +# Copyright (C) 2024 Free Software Foundation, Inc.
> +
> +# This program 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 of the License, or
> +# (at your option) any later version.
> +#
> +# This program 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/>.
> +
> +# GCC testsuite that uses the `dg.exp' driver.
> +
> +# Exit immediately if this isn't an AArch64 target.
> +if ![istarget aarch64*-*-*] then {
> + return
> +}
> +
> +# Load support procs.
> +load_lib gcc-dg.exp
> +
> +# Initialize `dg'.
> +dg-init
> +
> +proc check_effective_target_gas_has_build_attributes { } {
> + return [check_no_compiler_messages gas_has_build_attributes object {
> + /* Assembly */
> + .set ATTR_TYPE_uleb128, 0
> + .set ATTR_TYPE_asciz, 1
> + .set Tag_Feature_GCS, 3
> + .aeabi_subsection .aeabi-feature-and-bits, 1, ATTR_TYPE_uleb128
> + .aeabi_attribute Tag_Feature_GCS, 1
> + }]
> +}
This should go in lib/target-supports.exp instead, probably as
"aarch64_gas_has...".
> +
> +# Main loop.
> +dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.\[cCS\]]] \
> + "" ""
> +
> +# All done.
> +dg-finish
> diff --git a/gcc/testsuite/gcc.target/aarch64/build-attributes/no-build-attribute-bti.c b/gcc/testsuite/gcc.target/aarch64/build-attributes/no-build-attribute-bti.c
> new file mode 100644
> index 00000000000..a8712d949f0
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/aarch64/build-attributes/no-build-attribute-bti.c
> @@ -0,0 +1,12 @@
> +/* { dg-do compile { target { aarch64*-*-linux* && { ! gas_has_build_attributes } } } } */
> +/* { dg-options "-mbranch-protection=bti -dA" } */
> +
> +int main()
> +{
> + return 0;
> +}
> +
> +/* { dg-final { scan-assembler-not "\.aeabi_subsection \.aeabi-feature-and-bits, 1, 0" } } */
> +/* { dg-final { scan-assembler-not "\.aeabi_attribute 1, 1\t\/\/ Tag_Feature_BTI" } } */
For -not tests, it probably makes sense to test only for:
/* { dg-final { scan-assembler-not "\.aeabi_subsection " } } */
/* { dg-final { scan-assembler-not "\.aeabi_attribute " } } */
That is, we want to avoid emitting these directives at all, rather than
avoid emitting specific arguments/operands.
Thanks,
Richard
> +/* { dg-final { scan-assembler "\.section\t\.note\.gnu\.property" } } */
> +/* { dg-final { scan-assembler "\.word\t1\t\/\/ GNU_PROPERTY_AARCH64_FEATURE_1_AND \\(BTI\\)" } } */
> \ No newline at end of file
@@ -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
@@ -24677,6 +24677,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
@@ -24697,6 +24724,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 ();
}
@@ -28036,6 +28036,44 @@ 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 '
+ .set ATTR_TYPE_uleb128, 0
+ .set ATTR_TYPE_asciz, 1
+ .set Tag_Feature_GCS, 3
+ .aeabi_subsection .aeabi-feature-and-bits, 1, ATTR_TYPE_uleb128
+ .aeabi_attribute Tag_Feature_GCS, 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
@@ -4388,6 +4388,16 @@ 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,,
+ [
+ .set ATTR_TYPE_uleb128, 0
+ .set ATTR_TYPE_asciz, 1
+ .set Tag_Feature_GCS, 3
+ .aeabi_subsection .aeabi-feature-and-bits, 1, ATTR_TYPE_uleb128
+ .aeabi_attribute Tag_Feature_GCS, 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,
new file mode 100644
@@ -0,0 +1,12 @@
+/* { dg-do compile { target { aarch64*-*-linux* && { gas_has_build_attributes } } } } */
+/* { dg-options "-mbranch-protection=gcs -dA" } */
+
+int main()
+{
+ return 0;
+}
+
+/* { dg-final { scan-assembler "\.aeabi_subsection \.aeabi-feature-and-bits, 1, 0" } } */
+/* { dg-final { scan-assembler "\.aeabi_attribute 3, 1\t\/\/ Tag_Feature_GCS" } } */
+/* { dg-final { scan-assembler "\.section\t\.note\.gnu\.property" } } */
+/* { dg-final { scan-assembler "\.word\t4\t\/\/ GNU_PROPERTY_AARCH64_FEATURE_1_AND \\(GCS\\)" } } */
\ No newline at end of file
new file mode 100644
@@ -0,0 +1,12 @@
+/* { dg-do compile { target { aarch64*-*-linux* && gas_has_build_attributes } } } */
+/* { dg-options "-mbranch-protection=standard -dA" } */
+
+int main()
+{
+ return 0;
+}
+
+/* { dg-final { scan-assembler "\.aeabi_subsection \.aeabi-feature-and-bits, 1, 0" } } */
+/* { dg-final { scan-assembler "\.aeabi_attribute 3, 1\t\/\/ Tag_Feature_GCS" } } */
+/* { dg-final { scan-assembler "\.section\t\.note\.gnu\.property" } } */
+/* { dg-final { scan-assembler "\.word\t7\t\/\/ GNU_PROPERTY_AARCH64_FEATURE_1_AND \\(BTI, PAC, GCS\\)" } } */
\ No newline at end of file
new file mode 100644
@@ -0,0 +1,46 @@
+# Copyright (C) 2024 Free Software Foundation, Inc.
+
+# This program 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 of the License, or
+# (at your option) any later version.
+#
+# This program 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/>.
+
+# GCC testsuite that uses the `dg.exp' driver.
+
+# Exit immediately if this isn't an AArch64 target.
+if ![istarget aarch64*-*-*] then {
+ return
+}
+
+# Load support procs.
+load_lib gcc-dg.exp
+
+# Initialize `dg'.
+dg-init
+
+proc check_effective_target_gas_has_build_attributes { } {
+ return [check_no_compiler_messages gas_has_build_attributes object {
+ /* Assembly */
+ .set ATTR_TYPE_uleb128, 0
+ .set ATTR_TYPE_asciz, 1
+ .set Tag_Feature_GCS, 3
+ .aeabi_subsection .aeabi-feature-and-bits, 1, ATTR_TYPE_uleb128
+ .aeabi_attribute Tag_Feature_GCS, 1
+ }]
+}
+
+# Main loop.
+dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.\[cCS\]]] \
+ "" ""
+
+# All done.
+dg-finish
new file mode 100644
@@ -0,0 +1,12 @@
+/* { dg-do compile { target { aarch64*-*-linux* && { ! gas_has_build_attributes } } } } */
+/* { dg-options "-mbranch-protection=bti -dA" } */
+
+int main()
+{
+ return 0;
+}
+
+/* { dg-final { scan-assembler-not "\.aeabi_subsection \.aeabi-feature-and-bits, 1, 0" } } */
+/* { dg-final { scan-assembler-not "\.aeabi_attribute 1, 1\t\/\/ Tag_Feature_BTI" } } */
+/* { dg-final { scan-assembler "\.section\t\.note\.gnu\.property" } } */
+/* { dg-final { scan-assembler "\.word\t1\t\/\/ GNU_PROPERTY_AARCH64_FEATURE_1_AND \\(BTI\\)" } } */
\ No newline at end of file
new file mode 100644
@@ -0,0 +1,12 @@
+/* { dg-do compile { target { aarch64*-*-linux* && { ! gas_has_build_attributes } } } } */
+/* { dg-options "-mbranch-protection=gcs -dA" } */
+
+int main()
+{
+ return 0;
+}
+
+/* { 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 "\.section\t\.note\.gnu\.property" } } */
+/* { dg-final { scan-assembler "\.word\t4\t\/\/ GNU_PROPERTY_AARCH64_FEATURE_1_AND \\(GCS\\)" } } */
\ No newline at end of file
new file mode 100644
@@ -0,0 +1,12 @@
+/* { dg-do compile { target { aarch64*-*-linux* && { ! gas_has_build_attributes } } } } */
+/* { dg-options "-mbranch-protection=pac-ret -dA" } */
+
+int main()
+{
+ return 0;
+}
+
+/* { dg-final { scan-assembler-not "\.aeabi_subsection \.aeabi-feature-and-bits, 1, 0" } } */
+/* { dg-final { scan-assembler-not "\.aeabi_attribute 2, 1\t\/\/ Tag_Feature_PAC" } } */
+/* { dg-final { scan-assembler "\.section\t\.note\.gnu\.property" } } */
+/* { dg-final { scan-assembler "\.word\t2\t\/\/ GNU_PROPERTY_AARCH64_FEATURE_1_AND \\(PAC\\)" } } */
\ No newline at end of file
new file mode 100644
@@ -0,0 +1,12 @@
+/* { dg-do compile { target { aarch64*-*-linux* && { ! gas_has_build_attributes } } } } */
+/* { dg-options "-mbranch-protection=standard -dA" } */
+
+int main()
+{
+ return 0;
+}
+
+/* { 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 "\.section\t\.note\.gnu\.property" } } */
+/* { dg-final { scan-assembler "\.word\t7\t\/\/ GNU_PROPERTY_AARCH64_FEATURE_1_AND \\(BTI, PAC, GCS\\)" } } */
\ No newline at end of file