[v2,18/16] RISC-V: warn about non-power-of-2 stack-align attribute

Message ID c47d3248-b5e3-4c60-9c87-0d5c7705a8d6@suse.com
State New
Headers
Series RISC-V: assorted fixes and (hopefully) improvements |

Commit Message

Jan Beulich June 5, 2026, 9:29 a.m. UTC
  Only power-of-2 values are sensible for alignment.
---
Should libbfd's riscv_merge_attributes() also emit a warning?

Should we reject changing an already set non-zero value, like
riscv_merge_attributes() does?

Should we permit 0 as an explicit indication of "attribute not set"?
---
v2: New.
  

Comments

Jiawei June 8, 2026, 12:28 p.m. UTC | #1
LGTM.

At 2026/6/5 17:29, Jan Beulich wrote:
> Only power-of-2 values are sensible for alignment.
> ---
> Should libbfd's riscv_merge_attributes() also emit a warning?
>
> Should we reject changing an already set non-zero value, like
> riscv_merge_attributes() does?
>
> Should we permit 0 as an explicit indication of "attribute not set"?
> ---
> v2: New.

Warning for non-power-of-2 stack_align values looks right to me.  I also 
think
explicit stack_align = 0 should be warned about; if the attribute is 
meant to
be unset, it should be omitted rather than emitted with a zero value.

For the follow-up questions, I think libbfd should probably diagnose invalid
stack_align values too, and repeated changes of an already set non-zero 
value
should at least warn, since the linker treats different non-zero stack
alignment requirements as incompatible.
>
> --- a/gas/config/tc-riscv.c
> +++ b/gas/config/tc-riscv.c
> @@ -5891,6 +5891,13 @@ s_riscv_attribute (int ignored ATTRIBUTE
>   		   "any instructions"));
>         break;
>   
> +    case Tag_RISCV_stack_align:
> +      if (!attr[Tag_RISCV_stack_align].i
> +	  || (attr[Tag_RISCV_stack_align].i &
> +	      (attr[Tag_RISCV_stack_align].i - 1)))
> +	as_warn (_("`stack_align' attribute with non-power-of-2 value"));
> +      break;
> +
>       case Tag_RISCV_unaligned_access:
>         if (attr[Tag_RISCV_unaligned_access].i > 1)
>   	as_warn (_("`unaligned_access' attribute with non-boolean value"));
> --- a/gas/testsuite/gas/riscv/attribute-warn.d
> +++ b/gas/testsuite/gas/riscv/attribute-warn.d
> @@ -4,4 +4,5 @@
>   
>   Attribute Section: riscv
>   File Attributes
> +  Tag_RISCV_stack_align: 65536-bytes
>     Tag_RISCV_arch: [a-zA-Z0-9_\"].*
> --- a/gas/testsuite/gas/riscv/attribute-warn.l
> +++ b/gas/testsuite/gas/riscv/attribute-warn.l
> @@ -1,2 +1,4 @@
>   .*: Assembler messages:
>   .*:2: Warning: `unaligned_access' attribute with non-boolean value
> +.*:5: Warning: `stack_align' attribute with non-power-of-2 value
> +.*:6: Warning: `stack_align' attribute with non-power-of-2 value
> --- a/gas/testsuite/gas/riscv/attribute-warn.s
> +++ b/gas/testsuite/gas/riscv/attribute-warn.s
> @@ -1,3 +1,7 @@
>   	.attribute unaligned_access, 1
>   	.attribute unaligned_access, 0x1234
>   	.attribute unaligned_access, 0
> +
> +	.attribute stack_align, 0
> +	.attribute stack_align, 0x4444
> +	.attribute stack_align, 0x10000
Reviewed-by: Jiawei <jiawei@iscas.ac.cn>
  

Patch

--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -5891,6 +5891,13 @@  s_riscv_attribute (int ignored ATTRIBUTE
 		   "any instructions"));
       break;
 
+    case Tag_RISCV_stack_align:
+      if (!attr[Tag_RISCV_stack_align].i
+	  || (attr[Tag_RISCV_stack_align].i &
+	      (attr[Tag_RISCV_stack_align].i - 1)))
+	as_warn (_("`stack_align' attribute with non-power-of-2 value"));
+      break;
+
     case Tag_RISCV_unaligned_access:
       if (attr[Tag_RISCV_unaligned_access].i > 1)
 	as_warn (_("`unaligned_access' attribute with non-boolean value"));
--- a/gas/testsuite/gas/riscv/attribute-warn.d
+++ b/gas/testsuite/gas/riscv/attribute-warn.d
@@ -4,4 +4,5 @@ 
 
 Attribute Section: riscv
 File Attributes
+  Tag_RISCV_stack_align: 65536-bytes
   Tag_RISCV_arch: [a-zA-Z0-9_\"].*
--- a/gas/testsuite/gas/riscv/attribute-warn.l
+++ b/gas/testsuite/gas/riscv/attribute-warn.l
@@ -1,2 +1,4 @@ 
 .*: Assembler messages:
 .*:2: Warning: `unaligned_access' attribute with non-boolean value
+.*:5: Warning: `stack_align' attribute with non-power-of-2 value
+.*:6: Warning: `stack_align' attribute with non-power-of-2 value
--- a/gas/testsuite/gas/riscv/attribute-warn.s
+++ b/gas/testsuite/gas/riscv/attribute-warn.s
@@ -1,3 +1,7 @@ 
 	.attribute unaligned_access, 1
 	.attribute unaligned_access, 0x1234
 	.attribute unaligned_access, 0
+
+	.attribute stack_align, 0
+	.attribute stack_align, 0x4444
+	.attribute stack_align, 0x10000