[v4,08/12] RISC-V/gas: .attribute vs .insn

Message ID 79efa0c6-7c9a-4b95-8404-d983e62790ed@suse.com
State New
Headers
Series RISC-V: assorted fixes and (hopefully) improvements |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Jan Beulich July 24, 2026, 10:05 a.m. UTC
  "... before any instruction", as the diagnostic from s_riscv_attribute()
says, presumably ought to include also insns resulting from .insn. Make a
small helper function.

Reviewed-by: Jiawei <jiawei@iscas.ac.cn>
---
RFC: I can't quite figure why this constraint exists, so there may also be
     the option of relaxing things (up to fully dropping start_assemble).
---
v2: Style correction.
  

Comments

Nelson Chu July 27, 2026, 3:40 a.m. UTC | #1
On Fri, Jul 24, 2026 at 6:05 PM Jan Beulich <jbeulich@suse.com> wrote:
>
> "... before any instruction", as the diagnostic from s_riscv_attribute()
> says, presumably ought to include also insns resulting from .insn. Make a
> small helper function.
>
> Reviewed-by: Jiawei <jiawei@iscas.ac.cn>
> ---
> RFC: I can't quite figure why this constraint exists, so there may also be
>      the option of relaxing things (up to fully dropping start_assemble).
> ---

If I remember correctly, at that time we didn't support mapping
symbols, so all architecture related stuff (header rvc flag, xlen, elf
attribute...) should be set before starting assembly.  Now we have, so
probably can relax this limitation.  If we relax this limitation, then
we should make sure mappin symbols can also be generated for the
corresponding elf arch attributes while assembling instructions like
.option arch directives.  However, elf attributes are file-level
settings, while mapping symbols are function-level settings.
File-level attributes can be changed after assembly begins, meaning
that without any function-level .option directives, a section of code
might not be based on the final file-level setting.  In other words,
maintaining the validity of file-level settings - unless overridden at
the function level directives - simplifies things.  I don't have a
strong opinion on this, maybe we should ask psABI people to clarify
the behaviors.

Nelson

> v2: Style correction.
>
> --- a/gas/config/tc-riscv.c
> +++ b/gas/config/tc-riscv.c
> @@ -4650,6 +4650,18 @@ riscv_ip_hardcode (char *str,
>    return NULL;
>  }
>
> +/* The architecture and privileged elf attributes should be set before
> +   assembling.  */
> +static bool
> +start_assembly (void)
> +{
> +  start_assemble = true;
> +
> +  riscv_set_abi_by_arch ();
> +
> +  return riscv_set_default_priv_spec (NULL);
> +}
> +
>  void
>  md_assemble (char *str)
>  {
> @@ -4657,16 +4669,8 @@ md_assemble (char *str)
>    expressionS imm_expr;
>    bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
>
> -  /* The architecture and privileged elf attributes should be set
> -     before assembling.  */
> -  if (!start_assemble)
> -    {
> -      start_assemble = true;
> -
> -      riscv_set_abi_by_arch ();
> -      if (!riscv_set_default_priv_spec (NULL))
> -       return;
> -    }
> +  if (!start_assemble && !start_assembly())
> +    return;
>
>    riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
>
> @@ -5875,6 +5879,9 @@ s_riscv_insn (int x ATTRIBUTE_UNUSED)
>    bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
>    char save_c;
>
> +  if (!start_assemble && !start_assembly())
> +    return;
> +
>    while (!is_end_of_stmt (*input_line_pointer))
>      ++input_line_pointer;
>
>
  

Patch

--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -4650,6 +4650,18 @@  riscv_ip_hardcode (char *str,
   return NULL;
 }
 
+/* The architecture and privileged elf attributes should be set before
+   assembling.  */
+static bool
+start_assembly (void)
+{
+  start_assemble = true;
+
+  riscv_set_abi_by_arch ();
+
+  return riscv_set_default_priv_spec (NULL);
+}
+
 void
 md_assemble (char *str)
 {
@@ -4657,16 +4669,8 @@  md_assemble (char *str)
   expressionS imm_expr;
   bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
 
-  /* The architecture and privileged elf attributes should be set
-     before assembling.  */
-  if (!start_assemble)
-    {
-      start_assemble = true;
-
-      riscv_set_abi_by_arch ();
-      if (!riscv_set_default_priv_spec (NULL))
-       return;
-    }
+  if (!start_assemble && !start_assembly())
+    return;
 
   riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
 
@@ -5875,6 +5879,9 @@  s_riscv_insn (int x ATTRIBUTE_UNUSED)
   bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
   char save_c;
 
+  if (!start_assemble && !start_assembly())
+    return;
+
   while (!is_end_of_stmt (*input_line_pointer))
     ++input_line_pointer;