[7/8,Arm] Emit build attributes for PACBTI target feature. [ Was RE: [Patch 6/7, Arm, GCC] Emit build attributes for PACBTI target feature.]

Message ID PAXPR08MB7075DA07D1D24635F114DEC3EA869@PAXPR08MB7075.eurprd08.prod.outlook.com
State Superseded
Headers
Series [1/8,Arm,AArch64] Refactor mbranch-protection option parsing and make it common to AArch32 and AArch64 backends. [Was RE: [Patch 2/7, Arm, GCC] Add option -mbranch-protection.] |

Commit Message

Tejas Belagod Oct. 28, 2021, 11:43 a.m. UTC
  > -----Original Message-----
> From: Gcc-patches <gcc-patches-
> bounces+belagod=gcc.gnu.org@gcc.gnu.org> On Behalf Of Tejas Belagod via
> Gcc-patches
> Sent: Friday, October 8, 2021 1:19 PM
> To: gcc-patches@gcc.gnu.org
> Subject: [Patch 6/7, Arm, GCC] Emit build attributes for PACBTI target
> feature.
> 
> Hi,
> 
> This patch emits assembler directives for PACBTI build attributes as defined
> by the ABI. (https://github.com/ARM-software/abi-
> aa/releases/download/2021Q1/addenda32.pdf)
> 
> Tested on arm-none-eabi.
> 
> 2021-10-04  Tejas Belagod  <tbelagod@arm.com>
> 
> gcc/ChangeLog:
> 
> 	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
> 	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use,
> TAG_PACRET_use.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
> 	* gcc.target/arm/acle/pacbti-m-predef-3: New test.
> 	* gcc.target/arm/acle/pacbti-m-predef-6.c: New test.


This patch emits assembler directives for PACBTI build attributes
as defined by the ABI.
https://github.com/ARM-software/abi-aa/releases/download/2021Q1/addenda32.pdf

2021-10-25  Tejas Belagod  <tbelagod@arm.com>

gcc/ChangeLog:

	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use, TAG_PACRET_use.

gcc/testsuite/ChangeLog:

	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
	* gcc.target/arm/acle/pacbti-m-predef-3: New test.
	* gcc.target/arm/acle/pacbti-m-predef-6.c: New test.

Tested the following configurations, OK for trunk?

-mthumb/-march=armv8.1-m.main+pacbti/-mfloat-abi=soft
-marm/-march=armv7-a/-mfpu=vfpv3-d16/-mfloat-abi=softfp
mcmodel=small and tiny
aarch64-none-linux-gnu native test and bootstrap

Thanks,
Tejas.
  

Comments

Richard Earnshaw Dec. 7, 2021, 2:48 p.m. UTC | #1
On 28/10/2021 12:43, Tejas Belagod via Gcc-patches wrote:
> 
> 
>> -----Original Message-----
>> From: Gcc-patches <gcc-patches-
>> bounces+belagod=gcc.gnu.org@gcc.gnu.org> On Behalf Of Tejas Belagod via
>> Gcc-patches
>> Sent: Friday, October 8, 2021 1:19 PM
>> To: gcc-patches@gcc.gnu.org
>> Subject: [Patch 6/7, Arm, GCC] Emit build attributes for PACBTI target
>> feature.
>>
>> Hi,
>>
>> This patch emits assembler directives for PACBTI build attributes as defined
>> by the ABI. (https://github.com/ARM-software/abi-
>> aa/releases/download/2021Q1/addenda32.pdf)
>>
>> Tested on arm-none-eabi.
>>
>> 2021-10-04  Tejas Belagod  <tbelagod@arm.com>
>>
>> gcc/ChangeLog:
>>
>> 	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
>> 	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use,
>> TAG_PACRET_use.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
>> 	* gcc.target/arm/acle/pacbti-m-predef-3: New test.
>> 	* gcc.target/arm/acle/pacbti-m-predef-6.c: New test.
> 
> 
> This patch emits assembler directives for PACBTI build attributes
> as defined by the ABI.
> https://github.com/ARM-software/abi-aa/releases/download/2021Q1/addenda32.pdf
> 
> 2021-10-25  Tejas Belagod  <tbelagod@arm.com>
> 
> gcc/ChangeLog:
> 
> 	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
> 	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use, TAG_PACRET_use.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
> 	* gcc.target/arm/acle/pacbti-m-predef-3: New test.
> 	* gcc.target/arm/acle/pacbti-m-predef-6.c: New test.

I'm not sure what the value of making these executable tests is.  It 
means that they can only be used when the test model has PAC/BTI 
available.  But they don't really test the PAC/BTI generation, so that 
seems rather pointless.

Better, IMO to make them simple compile/scan-assembler tests that check 
the build attributes are correct.

R.

> 
> Tested the following configurations, OK for trunk?
> 
> -mthumb/-march=armv8.1-m.main+pacbti/-mfloat-abi=soft
> -marm/-march=armv7-a/-mfpu=vfpv3-d16/-mfloat-abi=softfp
> mcmodel=small and tiny
> aarch64-none-linux-gnu native test and bootstrap
> 
> Thanks,
> Tejas.
>
  
Andrea Corallo Dec. 10, 2021, 4:36 p.m. UTC | #2
Richard Earnshaw via Gcc-patches <gcc-patches@gcc.gnu.org> writes:

> On 28/10/2021 12:43, Tejas Belagod via Gcc-patches wrote:
>> 
>>> -----Original Message-----
>>> From: Gcc-patches <gcc-patches-
>>> bounces+belagod=gcc.gnu.org@gcc.gnu.org> On Behalf Of Tejas Belagod via
>>> Gcc-patches
>>> Sent: Friday, October 8, 2021 1:19 PM
>>> To: gcc-patches@gcc.gnu.org
>>> Subject: [Patch 6/7, Arm, GCC] Emit build attributes for PACBTI target
>>> feature.
>>>
>>> Hi,
>>>
>>> This patch emits assembler directives for PACBTI build attributes as defined
>>> by the ABI. (https://github.com/ARM-software/abi-
>>> aa/releases/download/2021Q1/addenda32.pdf)
>>>
>>> Tested on arm-none-eabi.
>>>
>>> 2021-10-04  Tejas Belagod  <tbelagod@arm.com>
>>>
>>> gcc/ChangeLog:
>>>
>>> 	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
>>> 	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use,
>>> TAG_PACRET_use.
>>>
>>> gcc/testsuite/ChangeLog:
>>>
>>> 	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
>>> 	* gcc.target/arm/acle/pacbti-m-predef-3: New test.
>>> 	* gcc.target/arm/acle/pacbti-m-predef-6.c: New test.
>> This patch emits assembler directives for PACBTI build attributes
>> as defined by the ABI.
>> https://github.com/ARM-software/abi-aa/releases/download/2021Q1/addenda32.pdf
>> 2021-10-25  Tejas Belagod  <tbelagod@arm.com>
>> gcc/ChangeLog:
>> 	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
>> 	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use, TAG_PACRET_use.
>> gcc/testsuite/ChangeLog:
>> 	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
>> 	* gcc.target/arm/acle/pacbti-m-predef-3: New test.
>> 	* gcc.target/arm/acle/pacbti-m-predef-6.c: New test.
>
> I'm not sure what the value of making these executable tests is.  It
> means that they can only be used when the test model has PAC/BTI
> available.  But they don't really test the PAC/BTI generation, so that
> seems rather pointless.
>
> Better, IMO to make them simple compile/scan-assembler tests that
> check the build attributes are correct.
>
> R.

Hi Richard,

agreed.  Please find attached the updated version of the patch.

Thanks for reviewing

  Andrea
From 6e54ecc3e20a3af8f72a38e55f086a33490353a0 Mon Sep 17 00:00:00 2001
From: Andrea Corallo <andrea.corallo@arm.com>
Date: Mon, 6 Dec 2021 11:42:24 +0100
Subject: [PATCH] Emit build attributes for PACBTI target feature.

gcc/ChangeLog:

	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use, TAG_PACRET_use.

gcc/testsuite/ChangeLog:

	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
	* gcc.target/arm/acle/pacbti-m-predef-3: Likewise.
	* gcc.target/arm/acle/pacbti-m-predef-6.c: Likewise.
	* gcc.target/arm/acle/pacbti-m-predef-7.c: Likewise.

Co-Authored-By: Tejas Belagod  <tbelagod@arm.com>
---
 gcc/config/arm/arm.c                          | 20 +++++++++++++++++++
 .../gcc.target/arm/acle/pacbti-m-predef-1.c   | 16 +++++++++++++++
 .../gcc.target/arm/acle/pacbti-m-predef-3.c   | 16 +++++++++++++++
 .../gcc.target/arm/acle/pacbti-m-predef-6.c   | 15 ++++++++++++++
 .../gcc.target/arm/acle/pacbti-m-predef-7.c   | 16 +++++++++++++++
 5 files changed, 83 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c
 create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c
 create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c
 create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-7.c

diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c
index ee22acddee5..a493523fe30 100644
--- a/gcc/config/arm/arm.c
+++ b/gcc/config/arm/arm.c
@@ -28221,6 +28221,8 @@ static void
 arm_file_start (void)
 {
   int val;
+  bool pac = (aarch_ra_sign_scope != AARCH_FUNCTION_NONE);
+  bool bti = (aarch_enable_bti == 1);
 
   arm_print_asm_arch_directives
     (asm_out_file, TREE_TARGET_OPTION (target_option_default_node));
@@ -28291,6 +28293,24 @@ arm_file_start (void)
 	arm_emit_eabi_attribute ("Tag_ABI_FP_16bit_format", 38,
 			     (int) arm_fp16_format);
 
+      if (TARGET_HAVE_PACBTI)
+	{
+	  arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 2);
+	  arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 2);
+	  arm_emit_eabi_attribute ("TAG_BTI_use", 74, bti);
+	  arm_emit_eabi_attribute ("TAG_PACRET_use", 76, pac);
+	}
+      else
+	{
+	  if (pac || bti)
+	    {
+	      arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 1);
+	      arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 1);
+	      arm_emit_eabi_attribute ("TAG_BTI_use", 74, bti);
+	      arm_emit_eabi_attribute ("TAG_PACRET_use", 76, pac);
+	    }
+	}
+
       if (arm_lang_output_object_attributes_hook)
 	arm_lang_output_object_attributes_hook();
     }
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c
new file mode 100644
index 00000000000..75d3e00ef64
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-additional-options " -mbranch-protection=pac-ret+bti --save-temps" } */
+
+#if !defined (__ARM_FEATURE_BTI_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be defined."
+#endif
+
+#if !defined (__ARM_FEATURE_PAC_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be defined."
+#endif
+
+/* { dg-final { scan-assembler-not "\.arch_extension pacbti" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 50, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 52, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 74, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 76, 1" } } */
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c
new file mode 100644
index 00000000000..54fd42f16a3
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-additional-options " -mbranch-protection=pac-ret+leaf --save-temps" } */
+
+#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
+
+/* { dg-final { scan-assembler-not "\.arch_extension pacbti" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 50, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 52, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 74, 0" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 76, 1" } } */
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c
new file mode 100644
index 00000000000..309910f7c55
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-additional-options " -mbranch-protection=bti --save-temps" } */
+
+#if !defined (__ARM_FEATURE_BTI_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be defined."
+#endif
+
+#if defined (__ARM_FEATURE_PAC_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be undefined."
+#endif
+/* { dg-final { scan-assembler-not "\.arch_extension pacbti" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 50, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 52, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 74, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 76, 0" } } */
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 00000000000..bdf9131c142
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-7.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-march=armv8.1-m.main+pacbti --save-temps" } */
+
+#if defined (__ARM_FEATURE_BTI_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be defined."
+#endif
+
+#if defined (__ARM_FEATURE_PAC_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be undefined."
+#endif
+
+/* { dg-final { scan-assembler "\.arch_extension pacbti" } } */
+/* { 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, 0" } } */
  
Richard Earnshaw Dec. 10, 2021, 4:48 p.m. UTC | #3
On 10/12/2021 16:36, Andrea Corallo via Gcc-patches wrote:
> Richard Earnshaw via Gcc-patches <gcc-patches@gcc.gnu.org> writes:
> 
>> On 28/10/2021 12:43, Tejas Belagod via Gcc-patches wrote:
>>>
>>>> -----Original Message-----
>>>> From: Gcc-patches <gcc-patches-
>>>> bounces+belagod=gcc.gnu.org@gcc.gnu.org> On Behalf Of Tejas Belagod via
>>>> Gcc-patches
>>>> Sent: Friday, October 8, 2021 1:19 PM
>>>> To: gcc-patches@gcc.gnu.org
>>>> Subject: [Patch 6/7, Arm, GCC] Emit build attributes for PACBTI target
>>>> feature.
>>>>
>>>> Hi,
>>>>
>>>> This patch emits assembler directives for PACBTI build attributes as defined
>>>> by the ABI. (https://github.com/ARM-software/abi-
>>>> aa/releases/download/2021Q1/addenda32.pdf)
>>>>
>>>> Tested on arm-none-eabi.
>>>>
>>>> 2021-10-04  Tejas Belagod  <tbelagod@arm.com>
>>>>
>>>> gcc/ChangeLog:
>>>>
>>>> 	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
>>>> 	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use,
>>>> TAG_PACRET_use.
>>>>
>>>> gcc/testsuite/ChangeLog:
>>>>
>>>> 	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
>>>> 	* gcc.target/arm/acle/pacbti-m-predef-3: New test.
>>>> 	* gcc.target/arm/acle/pacbti-m-predef-6.c: New test.
>>> This patch emits assembler directives for PACBTI build attributes
>>> as defined by the ABI.
>>> https://github.com/ARM-software/abi-aa/releases/download/2021Q1/addenda32.pdf
>>> 2021-10-25  Tejas Belagod  <tbelagod@arm.com>
>>> gcc/ChangeLog:
>>> 	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
>>> 	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use, TAG_PACRET_use.
>>> gcc/testsuite/ChangeLog:
>>> 	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
>>> 	* gcc.target/arm/acle/pacbti-m-predef-3: New test.
>>> 	* gcc.target/arm/acle/pacbti-m-predef-6.c: New test.
>>
>> I'm not sure what the value of making these executable tests is.  It
>> means that they can only be used when the test model has PAC/BTI
>> available.  But they don't really test the PAC/BTI generation, so that
>> seems rather pointless.
>>
>> Better, IMO to make them simple compile/scan-assembler tests that
>> check the build attributes are correct.
>>
>> R.
> 
> Hi Richard,
> 
> agreed.  Please find attached the updated version of the patch.
> 
> Thanks for reviewing
> 
>    Andrea
> 

Sorry, I've just been looking at this again and noted the following:

+      if (TARGET_HAVE_PACBTI)
+	{
+	  arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 2);
+	  arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 2);
+	  arm_emit_eabi_attribute ("TAG_BTI_use", 74, bti);
+	  arm_emit_eabi_attribute ("TAG_PACRET_use", 76, pac);
+	}
+      else
+	{
+	  if (pac || bti)
+	    {
+	      arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 1);
+	      arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 1);
+	      arm_emit_eabi_attribute ("TAG_BTI_use", 74, bti);
+	      arm_emit_eabi_attribute ("TAG_PACRET_use", 76, pac);
+	    }
+	}
+

Firstly, the if subclause inside the else can be lifted out to an 'else 
if'; secondly, we don't want to emit TAG_BTI_use or TAG_PACRET_use if 
the value is zero (since the default in the absence of the tag is zero). 
  So better to have this as:

       if (TARGET_HAVE_PACBTI)
	{
	  arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 2);
	  arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 2);
	}
       else if (pac || bti)
	{
	  arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 1);
	  arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 1);
	}

       if (bti)
         arm_emit_eabi_attribute ("TAG_BTI_use", 74, 1);
       if (pac)
	arm_emit_eabi_attribute ("TAG_PACRET_use", 76, 1);

And then adjust the tests for the zero case to use scan-assembler-not.

R.
  
Richard Earnshaw Dec. 10, 2021, 4:51 p.m. UTC | #4
On 10/12/2021 16:36, Andrea Corallo via Gcc-patches wrote:
> Richard Earnshaw via Gcc-patches <gcc-patches@gcc.gnu.org> writes:
> 
>> On 28/10/2021 12:43, Tejas Belagod via Gcc-patches wrote:
>>>
>>>> -----Original Message-----
>>>> From: Gcc-patches <gcc-patches-
>>>> bounces+belagod=gcc.gnu.org@gcc.gnu.org> On Behalf Of Tejas Belagod via
>>>> Gcc-patches
>>>> Sent: Friday, October 8, 2021 1:19 PM
>>>> To: gcc-patches@gcc.gnu.org
>>>> Subject: [Patch 6/7, Arm, GCC] Emit build attributes for PACBTI target
>>>> feature.
>>>>
>>>> Hi,
>>>>
>>>> This patch emits assembler directives for PACBTI build attributes as defined
>>>> by the ABI. (https://github.com/ARM-software/abi-
>>>> aa/releases/download/2021Q1/addenda32.pdf)
>>>>
>>>> Tested on arm-none-eabi.
>>>>
>>>> 2021-10-04  Tejas Belagod  <tbelagod@arm.com>
>>>>
>>>> gcc/ChangeLog:
>>>>
>>>> 	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
>>>> 	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use,
>>>> TAG_PACRET_use.
>>>>
>>>> gcc/testsuite/ChangeLog:
>>>>
>>>> 	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
>>>> 	* gcc.target/arm/acle/pacbti-m-predef-3: New test.
>>>> 	* gcc.target/arm/acle/pacbti-m-predef-6.c: New test.
>>> This patch emits assembler directives for PACBTI build attributes
>>> as defined by the ABI.
>>> https://github.com/ARM-software/abi-aa/releases/download/2021Q1/addenda32.pdf
>>> 2021-10-25  Tejas Belagod  <tbelagod@arm.com>
>>> gcc/ChangeLog:
>>> 	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
>>> 	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use, TAG_PACRET_use.
>>> gcc/testsuite/ChangeLog:
>>> 	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
>>> 	* gcc.target/arm/acle/pacbti-m-predef-3: New test.
>>> 	* gcc.target/arm/acle/pacbti-m-predef-6.c: New test.
>>
>> I'm not sure what the value of making these executable tests is.  It
>> means that they can only be used when the test model has PAC/BTI
>> available.  But they don't really test the PAC/BTI generation, so that
>> seems rather pointless.
>>
>> Better, IMO to make them simple compile/scan-assembler tests that
>> check the build attributes are correct.
>>
>> R.
> 
> Hi Richard,
> 
> agreed.  Please find attached the updated version of the patch.
> 
> Thanks for reviewing
> 
>    Andrea
> 

Oh, another one:

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 00000000000..bdf9131c142
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-7.c
@@ -0,0 +1,16 @@
+#if defined (__ARM_FEATURE_BTI_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be defined."
+#endif
+
+#if defined (__ARM_FEATURE_PAC_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be undefined."
+#endif

Shouldn't the first error message be 'undefined'?
  
Andrea Corallo Dec. 13, 2021, 4:52 p.m. UTC | #5
Richard Earnshaw <Richard.Earnshaw@foss.arm.com> writes:

[...]

>
> Sorry, I've just been looking at this again and noted the following:
>
> +      if (TARGET_HAVE_PACBTI)
> +	{
> +	  arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 2);
> +	  arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 2);
> +	  arm_emit_eabi_attribute ("TAG_BTI_use", 74, bti);
> +	  arm_emit_eabi_attribute ("TAG_PACRET_use", 76, pac);
> +	}
> +      else
> +	{
> +	  if (pac || bti)
> +	    {
> +	      arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 1);
> +	      arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 1);
> +	      arm_emit_eabi_attribute ("TAG_BTI_use", 74, bti);
> +	      arm_emit_eabi_attribute ("TAG_PACRET_use", 76, pac);
> +	    }
> +	}
> +
>
> Firstly, the if subclause inside the else can be lifted out to an
> 'else if'; secondly, we don't want to emit TAG_BTI_use or
> TAG_PACRET_use if the value is zero (since the default in the absence
> of the tag is zero).   So better to have this as:
>
>       if (TARGET_HAVE_PACBTI)
> 	{
> 	  arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 2);
> 	  arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 2);
> 	}
>       else if (pac || bti)
> 	{
> 	  arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 1);
> 	  arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 1);
> 	}
>
>       if (bti)
>         arm_emit_eabi_attribute ("TAG_BTI_use", 74, 1);
>       if (pac)
> 	arm_emit_eabi_attribute ("TAG_PACRET_use", 76, 1);
>
> And then adjust the tests for the zero case to use scan-assembler-not.
>
> R.

Hi Richard,

please find attached the updated patch.

Thanks for reviewing!

  Andrea
From 4fcb3b37445b3a81037029709e8c1aaa903049ea Mon Sep 17 00:00:00 2001
From: Andrea Corallo <andrea.corallo@arm.com>
Date: Mon, 6 Dec 2021 11:42:24 +0100
Subject: [PATCH] Emit build attributes for PACBTI target feature.

gcc/ChangeLog:

	* config/arm/arm.c (arm_file_start): Emit EABI attributes for
	Tag_PAC_extension, Tag_BTI_extension, TAG_BTI_use, TAG_PACRET_use.

gcc/testsuite/ChangeLog:

	* gcc.target/arm/acle/pacbti-m-predef-1.c: New test.
	* gcc.target/arm/acle/pacbti-m-predef-3: Likewise.
	* gcc.target/arm/acle/pacbti-m-predef-6.c: Likewise.
	* gcc.target/arm/acle/pacbti-m-predef-7.c: Likewise.

Co-Authored-By: Tejas Belagod  <tbelagod@arm.com>
---
 gcc/config/arm/arm.c                           | 18 ++++++++++++++++++
 .../gcc.target/arm/acle/pacbti-m-predef-1.c    | 16 ++++++++++++++++
 .../gcc.target/arm/acle/pacbti-m-predef-3.c    | 16 ++++++++++++++++
 .../gcc.target/arm/acle/pacbti-m-predef-6.c    | 15 +++++++++++++++
 .../gcc.target/arm/acle/pacbti-m-predef-7.c    | 16 ++++++++++++++++
 5 files changed, 81 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c
 create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c
 create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c
 create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-7.c

diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c
index ee22acddee5..e0b13e9a23e 100644
--- a/gcc/config/arm/arm.c
+++ b/gcc/config/arm/arm.c
@@ -28221,6 +28221,8 @@ static void
 arm_file_start (void)
 {
   int val;
+  bool pac = (aarch_ra_sign_scope != AARCH_FUNCTION_NONE);
+  bool bti = (aarch_enable_bti == 1);
 
   arm_print_asm_arch_directives
     (asm_out_file, TREE_TARGET_OPTION (target_option_default_node));
@@ -28291,6 +28293,22 @@ arm_file_start (void)
 	arm_emit_eabi_attribute ("Tag_ABI_FP_16bit_format", 38,
 			     (int) arm_fp16_format);
 
+      if (TARGET_HAVE_PACBTI)
+	{
+	  arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 2);
+	  arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 2);
+	}
+      else if (pac || bti)
+	{
+	  arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 1);
+	  arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 1);
+	}
+
+      if (bti)
+        arm_emit_eabi_attribute ("TAG_BTI_use", 74, 1);
+      if (pac)
+	arm_emit_eabi_attribute ("TAG_PACRET_use", 76, 1);
+
       if (arm_lang_output_object_attributes_hook)
 	arm_lang_output_object_attributes_hook();
     }
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c
new file mode 100644
index 00000000000..75d3e00ef64
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-additional-options " -mbranch-protection=pac-ret+bti --save-temps" } */
+
+#if !defined (__ARM_FEATURE_BTI_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be defined."
+#endif
+
+#if !defined (__ARM_FEATURE_PAC_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be defined."
+#endif
+
+/* { dg-final { scan-assembler-not "\.arch_extension pacbti" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 50, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 52, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 74, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 76, 1" } } */
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c
new file mode 100644
index 00000000000..bf6c3ba90c9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-additional-options " -mbranch-protection=pac-ret+leaf --save-temps" } */
+
+#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
+
+/* { dg-final { scan-assembler-not "\.arch_extension pacbti" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 50, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 52, 1" } } */
+/* { dg-final { scan-assembler-not "\.eabi_attribute 74" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 76, 1" } } */
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c
new file mode 100644
index 00000000000..82af11ee407
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-additional-options " -mbranch-protection=bti --save-temps" } */
+
+#if !defined (__ARM_FEATURE_BTI_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be defined."
+#endif
+
+#if defined (__ARM_FEATURE_PAC_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be undefined."
+#endif
+/* { dg-final { scan-assembler-not "\.arch_extension pacbti" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 50, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 52, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 74, 1" } } */
+/* { dg-final { scan-assembler-not "\.eabi_attribute 76" } } */
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 00000000000..48a40e64c11
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-7.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-march=armv8.1-m.main+pacbti --save-temps" } */
+
+#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 undefined."
+#endif
+
+/* { dg-final { scan-assembler "\.arch_extension pacbti" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 50, 2" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 52, 2" } } */
+/* { dg-final { scan-assembler-not "\.eabi_attribute 74" } } */
+/* { dg-final { scan-assembler-not "\.eabi_attribute 76" } } */
  

Patch

diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c
index 946841526ee127105396097d143e755bdfc756f5..a87bcb298f9e6d7b2f3fd61b4586e291f46b0f81 100644
--- a/gcc/config/arm/arm.c
+++ b/gcc/config/arm/arm.c
@@ -28200,6 +28200,8 @@  static void
 arm_file_start (void)
 {
   int val;
+  bool pac = (aarch_ra_sign_scope != AARCH_FUNCTION_NONE);
+  bool bti = (aarch_enable_bti == 1);
 
   arm_print_asm_arch_directives
     (asm_out_file, TREE_TARGET_OPTION (target_option_default_node));
@@ -28270,6 +28272,24 @@  arm_file_start (void)
 	arm_emit_eabi_attribute ("Tag_ABI_FP_16bit_format", 38,
 			     (int) arm_fp16_format);
 
+      if (TARGET_HAVE_PACBTI)
+	{
+	  arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 2);
+	  arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 2);
+	  arm_emit_eabi_attribute ("TAG_BTI_use", 74, bti);
+	  arm_emit_eabi_attribute ("TAG_PACRET_use", 76, pac);
+	}
+      else
+	{
+	  if (pac || bti)
+	    {
+	      arm_emit_eabi_attribute ("Tag_PAC_extension", 50, 1);
+	      arm_emit_eabi_attribute ("Tag_BTI_extension", 52, 1);
+	      arm_emit_eabi_attribute ("TAG_BTI_use", 74, bti);
+	      arm_emit_eabi_attribute ("TAG_PACRET_use", 76, pac);
+	    }
+	}
+
       if (arm_lang_output_object_attributes_hook)
 	arm_lang_output_object_attributes_hook();
     }
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c
new file mode 100644
index 0000000000000000000000000000000000000000..cc88380731ae81dd27c0a343518252a172f8f3ef
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-1.c
@@ -0,0 +1,30 @@ 
+
+/* { dg-do run } */
+/* { dg-require-effective-target arm_pacbti_hw } */
+/* { dg-additional-options " -mbranch-protection=pac-ret+bti --save-temps" } */
+
+/* { dg-final { scan-assembler "\.arch_extension pacbti" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 50, 2" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 52, 2" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 74, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 76, 1" } } */
+
+#if !defined (__ARM_FEATURE_BTI_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be defined."
+#endif
+
+#if !defined (__ARM_FEATURE_PAC_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be defined."
+#endif
+
+int
+main()
+{
+  if (__ARM_FEATURE_BTI_DEFAULT != 1)
+    __builtin_abort ();
+
+  if (__ARM_FEATURE_PAC_DEFAULT != 1)
+    __builtin_abort ();
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c
new file mode 100644
index 0000000000000000000000000000000000000000..8bebd995b170df953e13f86d2276576d5ab34e93
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-3.c
@@ -0,0 +1,26 @@ 
+
+/* { 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
+
+int
+main()
+{
+  if (__ARM_FEATURE_PAC_DEFAULT != 5)
+    __builtin_abort ();
+
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c
new file mode 100644
index 0000000000000000000000000000000000000000..13990a310fdd045832bdfa96ab61b9b8ae11efa4
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-6.c
@@ -0,0 +1,26 @@ 
+
+/* { dg-do run } */
+/* { dg-require-effective-target arm_pacbti_hw } */
+/* { dg-additional-options " -mbranch-protection=bti --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, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 76, 0" } } */
+
+#if !defined (__ARM_FEATURE_BTI_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be defined."
+#endif
+
+#if defined (__ARM_FEATURE_PAC_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be undefined."
+#endif
+
+int
+main()
+{
+  if (__ARM_FEATURE_BTI_DEFAULT != 1)
+    __builtin_abort ();
+
+  return 0;
+}