Re: [Patch 9/9, GCC, Arm] Implement arm Function target attribute 'branch-protection'.

2021-12-16 Thread Andrea Corallo via Gcc-patches
Tejas Belagod via Gcc-patches  writes:

> Hi,
>
> This patch is part of the series of PACBTI-M patches posted earlier 
> https://gcc.gnu.org/pipermail/gcc-patches/2021-October/582773.html
>
> This change adds the target function attribute 'branch-protection'.  The
> options that it can take are the same the command-line option
> 'mbranch-protection'.  The function attribute options override the command-
> line options for the function scope.
>
> Regression tested for arm-none-eabi. OK for trunk?
>
> 2021-11-11  Tejas Belagod  
>
> gcc/ChangeLog:
>   * config/arm/arm.c (arm_valid_target_attribute_rec): Add ARM function
>   attribute 'branch-protection' and parse its options.
>   * doc/extend.texi: Document ARM Function attribute 'branch-protection'.
>
> gcc/testsuite/
>   * gcc.target/arm/acle/pacbti-m-predef-7.c: New test.
>
> Thanks,
> Tejas.

[...]

Ping

  Andrea


[Patch 9/9, GCC, Arm] Implement arm Function target attribute 'branch-protection'.

2021-11-12 Thread Tejas Belagod via Gcc-patches
Hi,

This patch is part of the series of PACBTI-M patches posted earlier 
https://gcc.gnu.org/pipermail/gcc-patches/2021-October/582773.html

This change adds the target function attribute 'branch-protection'.  The
options that it can take are the same the command-line option
'mbranch-protection'.  The function attribute options override the command-
line options for the function scope.

Regression tested for arm-none-eabi. OK for trunk?

2021-11-11  Tejas Belagod  

gcc/ChangeLog:
* config/arm/arm.c (arm_valid_target_attribute_rec): Add ARM function
attribute 'branch-protection' and parse its options.
* doc/extend.texi: Document ARM Function attribute 'branch-protection'.

gcc/testsuite/
* gcc.target/arm/acle/pacbti-m-predef-7.c: New test.

Thanks,
Tejas.
diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c
index 
a87bcb298f9e6d7b2f3fd61b4586e291f46b0f81..64253f3814786b302f8fea573fdfc4213da439ce
 100644
--- a/gcc/config/arm/arm.c
+++ b/gcc/config/arm/arm.c
@@ -33147,6 +33147,22 @@ arm_valid_target_attribute_rec (tree args, struct 
gcc_options *opts)
 
  opts->x_arm_arch_string = xstrndup (arch, strlen (arch));
}
+  else if (startswith (q, "branch-protection="))
+   {
+ char *bp_str = q + strlen ("branch-protection=");
+
+ opts->x_arm_branch_protection_string
+   = xstrndup (bp_str, strlen (bp_str));
+
+ /* Capture values from target attribute.  */
+ aarch_validate_mbranch_protection
+   (opts->x_arm_branch_protection_string);
+
+ /* Init function target attr values.  */
+ opts->x_aarch_ra_sign_scope = aarch_ra_sign_scope;
+ opts->x_aarch_enable_bti = aarch_enable_bti;
+
+   }
   else if (q[0] == '+')
{
  opts->x_arm_arch_string
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 
eee4c6737bbfa9529fd613a0197d513121d058ec..c605adf665754804735e244006fb39f02705c74e
 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -4767,6 +4767,12 @@ In this example @code{target("+crc+nocrypto")} enables 
the @code{crc}
 extension and disables the @code{crypto} extension for the function @code{foo}
 without modifying an existing @option{-march=} or @option{-mcpu} option.
 
+@item branch-protection=
+@cindex @code{branch-protection=} function attribute, arm
+Select the function scope on which branch protection will be applied.  The
+behavior and permissible arguments are the same as for the command-line option
+@option{-mbranch-protection=}.  The default value is @code{none}.
+
 @end table
 
 @end table
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-7.c 
b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-7.c
new file mode 100644
index 
..ccf3e1cb9ae6cbed77844142e94641548b75c918
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-7.c
@@ -0,0 +1,30 @@
+/* { dg-do run } */
+/* { dg-require-effective-target arm_pacbti_hw } */
+/* { dg-additional-options " -mbranch-protection=pac-ret+leaf --save-temps" } 
*/
+
+/* { dg-final { scan-assembler "\.eabi_attribute 50, 2" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 52, 2" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 74, 0" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 76, 1" } } */
+
+#if defined (__ARM_FEATURE_BTI_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be undefined."
+#endif
+
+#if !defined (__ARM_FEATURE_PAC_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be defined."
+#endif
+
+__attribute__((target("branch-protection=pac-ret+bti"), noinline))
+void foo ()
+{
+  if (__ARM_FEATURE_PAC_DEFAULT != 5)
+__builtin_abort ();
+}
+
+int
+main()
+{
+  foo ();
+  return 0;
+}