[PATCH v3 12/26] gas: use common code for object attribute v1 & v2 parsing

Matthieu Longo matthieu.longo@arm.com
Fri May 9 15:13:02 GMT 2025


Since the previous patch added all the code to be able to parse both
OAv1 and OAv2 directives, this patch switches OAv1 to use this common
code.
Additionally to the common code in obj-elf.c, the following backends
using a custom object attribute directive were impacted.
  - ARC
  - Arm
  - m68k
  - PowerPC
  - RISC-V
  - TI C6X
A parsing test for Arm had to be adapted to the error messages of the
new parser.

The gas and ld test suites were successfully run for the following
backends: S390, ARC, Arm, CSky, m68k, msp430, PowerPC, TI C6X, RISC-V,
AArch64, MIPS, SPARC.
---
 gas/config/obj-elf.c                |   3 -
 gas/config/tc-arc.c                 |   2 +-
 gas/config/tc-arm.c                 |   2 +-
 gas/config/tc-m68k.c                |   2 +-
 gas/config/tc-ppc.c                 |   2 +-
 gas/config/tc-riscv.c               |   2 +-
 gas/config/tc-tic6x.c               |   2 +-
 gas/obj-attr.c                      | 114 ----------------------------
 gas/obj-attr.h                      |   2 -
 gas/testsuite/gas/arm/attr-syntax.d |   6 +-
 10 files changed, 11 insertions(+), 126 deletions(-)

diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c
index c7fdf8971b6..8db8d1d6bc8 100644
--- a/gas/config/obj-elf.c
+++ b/gas/config/obj-elf.c
@@ -2062,9 +2062,6 @@ obj_elf_vtable_entry (int ignore ATTRIBUTE_UNUSED)
 static void
 obj_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
 {
-  obj_attr_version_t version = elf_obj_attr_version (stdoutput);
-  if (version < OBJ_ATTR_V2)
-    obj_attr_v1_process_attribute (OBJ_ATTR_GNU);
   obj_attr_process_attribute (OBJ_ATTR_GNU);
 }
 
diff --git a/gas/config/tc-arc.c b/gas/config/tc-arc.c
index d12924818ea..d7bcf165ae1 100644
--- a/gas/config/tc-arc.c
+++ b/gas/config/tc-arc.c
@@ -4928,7 +4928,7 @@ arc_extcorereg (int opertype)
 static void
 arc_attribute (int ignored ATTRIBUTE_UNUSED)
 {
-  uint32_t tag = obj_attr_v1_process_attribute (OBJ_ATTR_PROC);
+  uint32_t tag = obj_attr_process_attribute (OBJ_ATTR_PROC);
 
   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
     attributes_set_explicitly[tag] = true;
diff --git a/gas/config/tc-arm.c b/gas/config/tc-arm.c
index f881e538a67..a01210de277 100644
--- a/gas/config/tc-arm.c
+++ b/gas/config/tc-arm.c
@@ -4974,7 +4974,7 @@ s_arm_unwind_raw (int ignored ATTRIBUTE_UNUSED)
 static void
 s_arm_eabi_attribute (int ignored ATTRIBUTE_UNUSED)
 {
-  uint32_t tag = obj_attr_v1_process_attribute (OBJ_ATTR_PROC);
+  uint32_t tag = obj_attr_process_attribute (OBJ_ATTR_PROC);
 
   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
     attributes_set_explicitly[tag] = 1;
diff --git a/gas/config/tc-m68k.c b/gas/config/tc-m68k.c
index 8914bbc6456..ea5234e4363 100644
--- a/gas/config/tc-m68k.c
+++ b/gas/config/tc-m68k.c
@@ -7917,7 +7917,7 @@ m68k_elf_cons (int nbytes /* 4=.long */)
 static void
 m68k_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
 {
-  uint32_t tag = obj_attr_v1_process_attribute (OBJ_ATTR_GNU);
+  uint32_t tag = obj_attr_process_attribute (OBJ_ATTR_GNU);
 
   /* Check validity of defined m68k tags.  */
   if (tag == Tag_GNU_M68K_ABI_FP)
diff --git a/gas/config/tc-ppc.c b/gas/config/tc-ppc.c
index 9dc00b81264..7b087dc7155 100644
--- a/gas/config/tc-ppc.c
+++ b/gas/config/tc-ppc.c
@@ -2577,7 +2577,7 @@ ppc_elf_abiversion (int ignore ATTRIBUTE_UNUSED)
 static void
 ppc_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
 {
-  uint32_t tag = obj_attr_v1_process_attribute (OBJ_ATTR_GNU);
+  uint32_t tag = obj_attr_process_attribute (OBJ_ATTR_GNU);
 
   /* Check validity of defined powerpc tags.  */
   if (tag == Tag_GNU_Power_ABI_FP
diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index 1eb03878216..81920513e26 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -5789,7 +5789,7 @@ riscv_convert_symbolic_attribute (const char *name)
 static void
 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED)
 {
-  uint32_t tag = obj_attr_v1_process_attribute (OBJ_ATTR_PROC);
+  uint32_t tag = obj_attr_process_attribute (OBJ_ATTR_PROC);
   unsigned old_xlen;
   obj_attribute *attr;
 
diff --git a/gas/config/tc-tic6x.c b/gas/config/tc-tic6x.c
index 719e7d6ff96..670923e8633 100644
--- a/gas/config/tc-tic6x.c
+++ b/gas/config/tc-tic6x.c
@@ -689,7 +689,7 @@ static bool tic6x_attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
 static void
 s_tic6x_c6xabi_attribute (int ignored ATTRIBUTE_UNUSED)
 {
-  uint32_t tag = obj_attr_v1_process_attribute (OBJ_ATTR_PROC);
+  uint32_t tag = obj_attr_process_attribute (OBJ_ATTR_PROC);
 
   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
     tic6x_attributes_set_explicitly[tag] = true;
diff --git a/gas/obj-attr.c b/gas/obj-attr.c
index ac92999a81f..91d3693f090 100644
--- a/gas/obj-attr.c
+++ b/gas/obj-attr.c
@@ -983,117 +983,3 @@ obj_attr_process_subsection ()
 
   args_list_free (args, N_ARGS);
 }
-
-/* Parse an attribute directive for VENDOR.
-   Returns the attribute number read, or zero on error.  */
-
-uint32_t
-obj_attr_v1_process_attribute (obj_attr_vendor vendor)
-{
-  expressionS exp;
-  int type;
-  int tag;
-  unsigned int i = 0;
-  char *s = NULL;
-
-  /* Read the first number or name.  */
-  skip_whitespace (input_line_pointer);
-  s = input_line_pointer;
-  if (ISDIGIT (*input_line_pointer))
-    {
-      expression (& exp);
-      if (exp.X_op != O_constant)
-	goto bad;
-      tag = exp.X_add_number;
-    }
-  else
-    {
-      char *name;
-
-      /* A name may contain '_', but no other punctuation.  */
-      for (; ISALNUM (*input_line_pointer) || *input_line_pointer == '_';
-	   ++input_line_pointer)
-	i++;
-      if (i == 0)
-	goto bad;
-
-      name = xmemdup0 (s, i);
-
-#ifndef CONVERT_SYMBOLIC_ATTRIBUTE
-#define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
-#endif
-
-      tag = CONVERT_SYMBOLIC_ATTRIBUTE (name);
-      if (tag == -1)
-	{
-	  as_bad (_("Attribute name not recognised: %s"), name);
-	  ignore_rest_of_line ();
-	  free (name);
-	  return 0;
-	}
-      free (name);
-    }
-
-  type = _bfd_elf_obj_attrs_arg_type (stdoutput, vendor, tag);
-
-  if (skip_past_comma (&input_line_pointer) == -1)
-    goto bad;
-  if (type & 1)
-    {
-      expression (& exp);
-      if (exp.X_op != O_constant)
-	{
-	  as_bad (_("expected numeric constant"));
-	  ignore_rest_of_line ();
-	  return 0;
-	}
-      i = exp.X_add_number;
-    }
-  if ((type & 3) == 3
-      && skip_past_comma (&input_line_pointer) == -1)
-    {
-      as_bad (_("expected comma"));
-      ignore_rest_of_line ();
-      return 0;
-    }
-  if (type & 2)
-    {
-      int len;
-
-      skip_whitespace (input_line_pointer);
-      if (*input_line_pointer != '"')
-	goto bad_string;
-      s = demand_copy_C_string (&len);
-    }
-
-  obj_attr_v1_rai_mark_as_seen (vendor, tag);
-  bool ok = false;
-  switch (type & 3)
-    {
-    case 3:
-      ok = bfd_elf_add_obj_attr_int_string (stdoutput, vendor, tag, i, s);
-      break;
-    case 2:
-      ok = bfd_elf_add_obj_attr_string (stdoutput, vendor, tag, s);
-      break;
-    case 1:
-      ok = bfd_elf_add_obj_attr_int (stdoutput, vendor, tag, i);
-      break;
-    default:
-      abort ();
-    }
-  if (!ok)
-    as_fatal (_("error adding attribute: %s"),
-	      bfd_errmsg (bfd_get_error ()));
-
-  demand_empty_rest_of_line ();
-  return tag;
- bad_string:
-  as_bad (_("bad string constant"));
-  ignore_rest_of_line ();
-  return 0;
- bad:
-  as_bad (_("expected <tag> , <value>"));
-  ignore_rest_of_line ();
-  return 0;
-}
diff --git a/gas/obj-attr.h b/gas/obj-attr.h
index f630b046107..51662ab82df 100644
--- a/gas/obj-attr.h
+++ b/gas/obj-attr.h
@@ -30,8 +30,6 @@ extern bool obj_attr_v1_rai_seen (obj_attr_vendor, uint32_t);
 
 /* Object attributes parsers.  */
 
-/* Object attributes v1 only.  */
-extern uint32_t obj_attr_v1_process_attribute (obj_attr_vendor);
 /* Object attributes v1 & v2.  */
 extern uint32_t obj_attr_process_attribute (obj_attr_vendor);
 /* Object attributes v2 only.  */
diff --git a/gas/testsuite/gas/arm/attr-syntax.d b/gas/testsuite/gas/arm/attr-syntax.d
index b21e5eb61e5..737cf7208d4 100644
--- a/gas/testsuite/gas/arm/attr-syntax.d
+++ b/gas/testsuite/gas/arm/attr-syntax.d
@@ -1,4 +1,8 @@
 #source: attr-syntax.s
 #notarget: *-*-pe
 #as:
-#error: :1: Error: Attribute name not recognised: made_up_tag.*:3: Error: expected <tag> , <value>.*:5: Error: expected <tag> , <value>
+#error: \A[^\n]*\.s: Assembler messages:\n
+#error: [^\n]*\.s:[0-9]+: Error: unknown identifier 'made_up_tag'\n
+#error: [^\n]*\.s:[0-9]+: Error: could not parse attribute key\n
+#error: [^\n]*\.s:[0-9]+: Error: unexpected comma before parameter 1\n
+#error: [^\n]*\.s:[0-9]+: Error: missing comma after parameter 1
-- 
2.49.0



More information about the Binutils mailing list