[binutils-gdb] RISC-V/bfd: warn about non-power-of-2 stack-align attribute

Jan Beulich via Binutils-cvs <[email protected]> Fri, 31 Jul 2026 11:54:44 +0000 (GMT)
Newsgroups gmane.comp.gnu.binutils.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D787a99680e72=
c076ad87b580f3de1a37b48598eb

commit 787a99680e72c076ad87b580f3de1a37b48598eb
Author: Jan Beulich <[email protected]>
Date:   Fri Jul 31 13:50:58 2026 +0200

    RISC-V/bfd: warn about non-power-of-2 stack-align attribute
   =20
    Only power-of-2 values are sensible for alignment. Reject other values.
   =20
    While there also drop a redundant part of a related conditional.
   =20
    Reviewed-by: Jiawei [email protected]

Diff:
---
 bfd/elfxx-riscv.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/bfd/elfxx-riscv.c b/bfd/elfxx-riscv.c
index 98ea23fda97..73924a73207 100644
--- a/bfd/elfxx-riscv.c
+++ b/bfd/elfxx-riscv.c
@@ -3906,6 +3906,17 @@ riscv_merge_attributes (bfd *ibfd, struct bfd_link_i=
nfo *info,
 		 ibfd);
 	      out_attr[i].i =3D 1;
 	      break;
+
+	    case Tag_RISCV_stack_align:
+	      if (!(out_attr[i].i & (out_attr[i].i - 1)))
+		break;
+
+	      _bfd_error_handler
+		(_("warning: %pB uses non-power-of-2 `stack_align' attribute; "
+		   "ignoring"),
+		 ibfd);
+	      out_attr[i].i =3D 0;
+	      break;
 	    }
 	}
=20
@@ -4006,10 +4017,14 @@ riscv_merge_attributes (bfd *ibfd, struct bfd_link_=
info *info,
 	break;
=20
       case Tag_RISCV_stack_align:
-	if (out_attr[i].i =3D=3D 0)
+	if ((in_attr[i].i & (in_attr[i].i - 1)) !=3D 0)
+	  _bfd_error_handler
+	    (_("warning: %pB uses non-power-of-2 `stack_align' attribute; "
+	       "ignoring"),
+	     ibfd);
+	else if (out_attr[i].i =3D=3D 0)
 	  out_attr[i].i =3D in_attr[i].i;
 	else if (in_attr[i].i !=3D 0
-		 && out_attr[i].i !=3D 0
 		 && out_attr[i].i !=3D in_attr[i].i)
 	  {
 	    _bfd_error_handler