public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* elf-attrs.c memory allocation fail
@ 2023-09-19 22:58 Alan Modra
  0 siblings, 0 replies; only message in thread
From: Alan Modra @ 2023-09-19 22:58 UTC (permalink / raw)
  To: binutils

Report errors rather than segfaulting.

bfd/
	* elf-attrs.c (elf_new_obj_attr): Return NULL on bfd_alloc fail.
	(bfd_elf_add_obj_attr_int): Handle NULL return from the above,
	and propagate return to callers.
	(elf_add_obj_attr_string, elf_add_obj_attr_int_string): Likewise.
	(bfd_elf_add_obj_attr_string): Similarly.
	(_bfd_elf_copy_obj_attributes): Report error on alloc fails.
	(_bfd_elf_parse_attributes): Likewise.
	* elf-bfd.h (bfd_elf_add_obj_attr_int): Update prototype.
	(bfd_elf_add_obj_attr_string): Likewise.
	(bfd_elf_add_obj_attr_int_string): Likewise.
gas/
	* config/obj-elf.c (obj_elf_vendor_attribute): Report fatal
	error on out of memory from bfd attribute functions.
	* config/tc-arc.c (arc_set_attribute_int): Likewise.
	(arc_set_attribute_string, arc_set_public_attributes): Likewise.
	* config/tc-arm.c (aeabi_set_attribute_int): Likewise.
	(aeabi_set_attribute_string): Likewise.
	* config/tc-mips.c (mips_md_finish): Likewise.
	* config/tc-msp430.c (msp430_md_finish): Likewise.
	* config/tc-riscv.c (riscv_write_out_attrs): Likewise.
	* config/tc-sparc.c (sparc_md_finish): Likewise.
	* config/tc-tic6x.c (tic6x_set_attribute_int): Likewise.
	* config/tc-csky.c (md_begin): Likewise.
	(set_csky_attribute): Return ok status.

diff --git a/bfd/elf-attrs.c b/bfd/elf-attrs.c
index 9b19d4afa3a..9a8ef23c78b 100644
--- a/bfd/elf-attrs.c
+++ b/bfd/elf-attrs.c
@@ -247,6 +247,8 @@ elf_new_obj_attr (bfd *abfd, int vendor, unsigned int tag)
       /* Create a new tag.  */
       list = (obj_attribute_list *)
 	bfd_alloc (abfd, sizeof (obj_attribute_list));
+      if (list == NULL)
+	return NULL;
       memset (list, 0, sizeof (obj_attribute_list));
       list->tag = tag;
       /* Keep the tag list in order.  */
@@ -292,14 +294,18 @@ bfd_elf_get_obj_attr_int (bfd *abfd, int vendor, unsigned int tag)
 }
 
 /* Add an integer object attribute.  */
-void
+obj_attribute *
 bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, unsigned int tag, unsigned int i)
 {
   obj_attribute *attr;
 
   attr = elf_new_obj_attr (abfd, vendor, tag);
-  attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
-  attr->i = i;
+  if (attr != NULL)
+    {
+      attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
+      attr->i = i;
+    }
+  return attr;
 }
 
 /* Duplicate an object attribute string value.  */
@@ -330,42 +336,54 @@ _bfd_elf_attr_strdup (bfd *abfd, const char *s)
 }
 
 /* Add a string object attribute.  */
-static void
+static obj_attribute *
 elf_add_obj_attr_string (bfd *abfd, int vendor, unsigned int tag,
 			 const char *s, const char *end)
 {
   obj_attribute *attr;
 
   attr = elf_new_obj_attr (abfd, vendor, tag);
-  attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
-  attr->s = elf_attr_strdup (abfd, s, end);
+  if (attr != NULL)
+    {
+      attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
+      attr->s = elf_attr_strdup (abfd, s, end);
+      if (attr->s == NULL)
+	return NULL;
+    }
+  return attr;
 }
 
-void
+obj_attribute *
 bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, unsigned int tag,
 			     const char *s)
 {
-  elf_add_obj_attr_string (abfd, vendor, tag, s, NULL);
+  return elf_add_obj_attr_string (abfd, vendor, tag, s, NULL);
 }
 
 /* Add a int+string object attribute.  */
-static void
+static obj_attribute *
 elf_add_obj_attr_int_string (bfd *abfd, int vendor, unsigned int tag,
 			     unsigned int i, const char *s, const char *end)
 {
   obj_attribute *attr;
 
   attr = elf_new_obj_attr (abfd, vendor, tag);
-  attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
-  attr->i = i;
-  attr->s = elf_attr_strdup (abfd, s, end);
+  if (attr != NULL)
+    {
+      attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
+      attr->i = i;
+      attr->s = elf_attr_strdup (abfd, s, end);
+      if (attr->s == NULL)
+	return NULL;
+    }
+  return attr;
 }
 
-void
+obj_attribute *
 bfd_elf_add_obj_attr_int_string (bfd *abfd, int vendor, unsigned int tag,
 				 unsigned int i, const char *s)
 {
-  elf_add_obj_attr_int_string (abfd, vendor, tag, i, s, NULL);
+  return elf_add_obj_attr_int_string (abfd, vendor, tag, i, s, NULL);
 }
 
 /* Copy the object attributes from IBFD to OBFD.  */
@@ -393,7 +411,11 @@ _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd)
 	  out_attr->type = in_attr->type;
 	  out_attr->i = in_attr->i;
 	  if (in_attr->s && *in_attr->s)
-	    out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s);
+	    {
+	      out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s);
+	      if (out_attr->s == NULL)
+		bfd_perror (_("error adding attribute"));
+	    }
 	  in_attr++;
 	  out_attr++;
 	}
@@ -402,23 +424,27 @@ _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd)
 	   list;
 	   list = list->next)
 	{
+	  bool ok = false;
 	  in_attr = &list->attr;
 	  switch (in_attr->type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL))
 	    {
 	    case ATTR_TYPE_FLAG_INT_VAL:
-	      bfd_elf_add_obj_attr_int (obfd, vendor, list->tag, in_attr->i);
+	      ok = bfd_elf_add_obj_attr_int (obfd, vendor,
+					     list->tag, in_attr->i);
 	      break;
 	    case ATTR_TYPE_FLAG_STR_VAL:
-	      bfd_elf_add_obj_attr_string (obfd, vendor, list->tag,
-					   in_attr->s);
+	      ok = bfd_elf_add_obj_attr_string (obfd, vendor, list->tag,
+						in_attr->s);
 	      break;
 	    case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
-	      bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag,
-					       in_attr->i, in_attr->s);
+	      ok = bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag,
+						    in_attr->i, in_attr->s);
 	      break;
 	    default:
 	      abort ();
 	    }
+	  if (!ok)
+	    bfd_perror (_("error adding attribute"));
 	}
     }
 }
@@ -563,6 +589,7 @@ _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr)
 		  while (p < end)
 		    {
 		      int type;
+		      bool ok = false;
 
 		      tag = _bfd_safe_read_leb128 (abfd, &p, false, end);
 		      type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
@@ -570,28 +597,30 @@ _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr)
 			{
 			case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL:
 			  val = _bfd_safe_read_leb128 (abfd, &p, false, end);
-			  elf_add_obj_attr_int_string (abfd, vendor, tag, val,
-						       (char *) p,
-						       (char *) end);
+			  ok = elf_add_obj_attr_int_string (abfd, vendor, tag,
+							    val, (char *) p,
+							    (char *) end);
 			  p += strnlen ((char *) p, end - p);
 			  if (p < end)
 			    p++;
 			  break;
 			case ATTR_TYPE_FLAG_STR_VAL:
-			  elf_add_obj_attr_string (abfd, vendor, tag,
-						   (char *) p,
-						   (char *) end);
+			  ok = elf_add_obj_attr_string (abfd, vendor, tag,
+							(char *) p,
+							(char *) end);
 			  p += strnlen ((char *) p, end - p);
 			  if (p < end)
 			    p++;
 			  break;
 			case ATTR_TYPE_FLAG_INT_VAL:
 			  val = _bfd_safe_read_leb128 (abfd, &p, false, end);
-			  bfd_elf_add_obj_attr_int (abfd, vendor, tag, val);
+			  ok = bfd_elf_add_obj_attr_int (abfd, vendor, tag, val);
 			  break;
 			default:
 			  abort ();
 			}
+		      if (!ok)
+			bfd_perror (_("error adding attribute"));
 		    }
 		  break;
 		case Tag_Section:
diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index d9ab5bd16cc..52d4d39b705 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -3009,14 +3009,16 @@ extern bfd *_bfd_elf64_bfd_from_remote_memory
 extern bfd_vma bfd_elf_obj_attr_size (bfd *);
 extern void bfd_elf_set_obj_attr_contents (bfd *, bfd_byte *, bfd_vma);
 extern int bfd_elf_get_obj_attr_int (bfd *, int, unsigned int);
-extern void bfd_elf_add_obj_attr_int (bfd *, int, unsigned int, unsigned int);
+extern obj_attribute *bfd_elf_add_obj_attr_int
+  (bfd *, int, unsigned int, unsigned int);
 #define bfd_elf_add_proc_attr_int(BFD, TAG, VALUE) \
   bfd_elf_add_obj_attr_int ((BFD), OBJ_ATTR_PROC, (TAG), (VALUE))
-extern void bfd_elf_add_obj_attr_string (bfd *, int, unsigned int, const char *);
+extern obj_attribute *bfd_elf_add_obj_attr_string
+  (bfd *, int, unsigned int, const char *);
 #define bfd_elf_add_proc_attr_string(BFD, TAG, VALUE) \
   bfd_elf_add_obj_attr_string ((BFD), OBJ_ATTR_PROC, (TAG), (VALUE))
-extern void bfd_elf_add_obj_attr_int_string (bfd *, int, unsigned int,
-					     unsigned int, const char *);
+extern obj_attribute *bfd_elf_add_obj_attr_int_string
+  (bfd *, int, unsigned int, unsigned int, const char *);
 #define bfd_elf_add_proc_attr_int_string(BFD, TAG, INTVAL, STRVAL) \
   bfd_elf_add_obj_attr_int_string ((BFD), OBJ_ATTR_PROC, (TAG), \
 				   (INTVAL), (STRVAL))
diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c
index 681e75f9a48..eaf2d6a2f29 100644
--- a/gas/config/obj-elf.c
+++ b/gas/config/obj-elf.c
@@ -2100,20 +2100,24 @@ obj_elf_vendor_attribute (int vendor)
     }
 
   record_attribute (vendor, tag);
+  bool ok = false;
   switch (type & 3)
     {
     case 3:
-      bfd_elf_add_obj_attr_int_string (stdoutput, vendor, tag, i, s);
+      ok = bfd_elf_add_obj_attr_int_string (stdoutput, vendor, tag, i, s);
       break;
     case 2:
-      bfd_elf_add_obj_attr_string (stdoutput, vendor, tag, s);
+      ok = bfd_elf_add_obj_attr_string (stdoutput, vendor, tag, s);
       break;
     case 1:
-      bfd_elf_add_obj_attr_int (stdoutput, vendor, tag, i);
+      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;
diff --git a/gas/config/tc-arc.c b/gas/config/tc-arc.c
index d9b13a0388e..5f6f34631b0 100644
--- a/gas/config/tc-arc.c
+++ b/gas/config/tc-arc.c
@@ -4941,7 +4941,9 @@ arc_set_attribute_int (int tag, int value)
   if (tag < 1
       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
       || !attributes_set_explicitly[tag])
-    bfd_elf_add_proc_attr_int (stdoutput, tag, value);
+    if (!bfd_elf_add_proc_attr_int (stdoutput, tag, value))
+      as_fatal (_("error adding attribute: %s"),
+		bfd_errmsg (bfd_get_error ()));
 }
 
 static void
@@ -4950,7 +4952,9 @@ arc_set_attribute_string (int tag, const char *value)
   if (tag < 1
       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
       || !attributes_set_explicitly[tag])
-    bfd_elf_add_proc_attr_string (stdoutput, tag, value);
+    if (!bfd_elf_add_proc_attr_string (stdoutput, tag, value))
+      as_fatal (_("error adding attribute: %s"),
+		bfd_errmsg (bfd_get_error ()));
 }
 
 /* Allocate and concatenate two strings.  s1 can be NULL but not
@@ -5020,7 +5024,9 @@ arc_set_public_attributes (void)
       && (base != bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
 					    Tag_ARC_CPU_base)))
     as_warn (_("Overwrite explicitly set Tag_ARC_CPU_base"));
-  bfd_elf_add_proc_attr_int (stdoutput, Tag_ARC_CPU_base, base);
+  if (!bfd_elf_add_proc_attr_int (stdoutput, Tag_ARC_CPU_base, base))
+    as_fatal (_("error adding attribute: %s"),
+	      bfd_errmsg (bfd_get_error ()));
 
   /* Tag_ARC_ABI_osver.  */
   if (attributes_set_explicitly[Tag_ARC_ABI_osver])
@@ -5069,7 +5075,9 @@ arc_set_public_attributes (void)
     {
       as_warn (_("Overwrite explicitly set Tag_ARC_ABI_rf16 to full "
 		 "register file"));
-      bfd_elf_add_proc_attr_int (stdoutput, Tag_ARC_ABI_rf16, 0);
+      if (!bfd_elf_add_proc_attr_int (stdoutput, Tag_ARC_ABI_rf16, 0))
+	as_fatal (_("error adding attribute: %s"),
+		  bfd_errmsg (bfd_get_error ()));
     }
 }
 
diff --git a/gas/config/tc-arm.c b/gas/config/tc-arm.c
index f912fb234b6..56345eacebf 100644
--- a/gas/config/tc-arm.c
+++ b/gas/config/tc-arm.c
@@ -32984,7 +32984,9 @@ aeabi_set_attribute_int (int tag, int value)
   if (tag < 1
       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
       || !attributes_set_explicitly[tag])
-    bfd_elf_add_proc_attr_int (stdoutput, tag, value);
+    if (!bfd_elf_add_proc_attr_int (stdoutput, tag, value))
+      as_fatal (_("error adding attribute: %s"),
+		bfd_errmsg (bfd_get_error ()));
 }
 
 static void
@@ -32993,7 +32995,9 @@ aeabi_set_attribute_string (int tag, const char *value)
   if (tag < 1
       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES
       || !attributes_set_explicitly[tag])
-    bfd_elf_add_proc_attr_string (stdoutput, tag, value);
+    if (!bfd_elf_add_proc_attr_string (stdoutput, tag, value))
+      as_fatal (_("error adding attribute: %s"),
+		bfd_errmsg (bfd_get_error ()));
 }
 
 /* Return whether features in the *NEEDED feature set are available via
diff --git a/gas/config/tc-csky.c b/gas/config/tc-csky.c
index 41205e7c791..8f2d29f2a70 100644
--- a/gas/config/tc-csky.c
+++ b/gas/config/tc-csky.c
@@ -1541,79 +1541,95 @@ md_show_usage (FILE *fp)
   -mvdsp			enable vector DSP instructions\n"));
 }
 
-static void set_csky_attribute (void)
+static bool
+set_csky_attribute (void)
 {
   if (mach_flag & CSKY_ARCH_DSP)
     {
       if (dsp_flag & CSKY_DSP_FLAG_V2)
 	{
 	  /* Set DSPV2.  */
-	  bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-				    Tag_CSKY_DSP_VERSION,
-				    VAL_CSKY_DSP_VERSION_2);
+	  if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+					 Tag_CSKY_DSP_VERSION,
+					 VAL_CSKY_DSP_VERSION_2))
+	    return false;
 	}
       else if (isa_flag & CSKY_ISA_DSP)
 	{
 	  /* Set DSP extension.  */
-	  bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-				    Tag_CSKY_DSP_VERSION,
-				    VAL_CSKY_DSP_VERSION_EXTENSION);
+	  if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+					 Tag_CSKY_DSP_VERSION,
+					 VAL_CSKY_DSP_VERSION_EXTENSION))
+	    return false;
 	}
       /* Set VDSP attribute.  */
       if (isa_flag & CSKY_ISA_VDSP)
-	bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-				  Tag_CSKY_VDSP_VERSION,
-				  VAL_CSKY_VDSP_VERSION_1);
-
+	{
+	  if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+					 Tag_CSKY_VDSP_VERSION,
+					 VAL_CSKY_VDSP_VERSION_1))
+	    return false;
+	}
       else if (isa_flag & CSKY_ISA_VDSP_2)
-	bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-				  Tag_CSKY_VDSP_VERSION,
-				  VAL_CSKY_VDSP_VERSION_2);
-
+	{
+	  if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+					 Tag_CSKY_VDSP_VERSION,
+					 VAL_CSKY_VDSP_VERSION_2))
+	    return false;
+	}
     }
 
   if (mach_flag & CSKY_ARCH_FLOAT)
     {
       unsigned int val = VAL_CSKY_FPU_HARDFP_SINGLE;
-      if (IS_CSKY_ARCH_V1 (mach_flag)) {
-	bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-				  Tag_CSKY_FPU_VERSION,
-				  VAL_CSKY_FPU_VERSION_1);
-      }
+      if (IS_CSKY_ARCH_V1 (mach_flag))
+	{
+	  if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+					 Tag_CSKY_FPU_VERSION,
+					 VAL_CSKY_FPU_VERSION_1))
+	    return false;
+	}
       else
 	{
 	  if (isa_flag & CSKY_ISA_FLOAT_3E4)
 	    {
-	      bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-					Tag_CSKY_FPU_VERSION,
-					VAL_CSKY_FPU_VERSION_2);
+	      if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+					     Tag_CSKY_FPU_VERSION,
+					     VAL_CSKY_FPU_VERSION_2))
+		return false;
 	      val |= VAL_CSKY_FPU_HARDFP_DOUBLE;
 	    }
 	  else
 	    {
-	      bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-					Tag_CSKY_FPU_VERSION,
-					VAL_CSKY_FPU_VERSION_2);
+	      if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+					     Tag_CSKY_FPU_VERSION,
+					     VAL_CSKY_FPU_VERSION_2))
+		return false;
 	    }
 
-	  bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-				    Tag_CSKY_FPU_HARDFP,
-				    val);
-	  bfd_elf_add_obj_attr_string (stdoutput, OBJ_ATTR_PROC,
-				    Tag_CSKY_FPU_NUMBER_MODULE,
-				    "IEEE 754");
-	  bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-				    Tag_CSKY_FPU_ABI,
-				    float_abi);
+	  if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+					 Tag_CSKY_FPU_HARDFP, val))
+	    return false;
+	  if (!bfd_elf_add_obj_attr_string (stdoutput, OBJ_ATTR_PROC,
+					    Tag_CSKY_FPU_NUMBER_MODULE,
+					    "IEEE 754"))
+	    return false;
+	  if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+					 Tag_CSKY_FPU_ABI,
+					 float_abi))
+	    return false;
 	}
     }
 
+  if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+				 Tag_CSKY_ISA_FLAGS, isa_flag))
+    return false;
 
-  bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-			    Tag_CSKY_ISA_FLAGS, isa_flag);
+  if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+				 Tag_CSKY_ISA_EXT_FLAGS, (isa_flag >> 32)))
+    return false;
 
-  bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
-			    Tag_CSKY_ISA_EXT_FLAGS, (isa_flag >> 32));
+  return true;
 }
 
 /* Target-specific initialization and option handling.  */
@@ -1676,8 +1692,10 @@ md_begin (void)
   for (p_arch = csky_archs; p_arch->arch_flag != 0; p_arch++)
     if ((mach_flag & CSKY_ARCH_MASK) == (p_arch->arch_flag & CSKY_ARCH_MASK))
       {
-	bfd_elf_add_obj_attr_string (stdoutput, OBJ_ATTR_PROC,
-				     Tag_CSKY_ARCH_NAME, p_arch->name);
+	if (!bfd_elf_add_obj_attr_string (stdoutput, OBJ_ATTR_PROC,
+					  Tag_CSKY_ARCH_NAME, p_arch->name))
+	  as_fatal (_("error adding attribute: %s"),
+		    bfd_errmsg (bfd_get_error ()));
 	bfd_mach_flag =  p_arch->bfd_mach_flag;
 	break;
       }
@@ -1686,8 +1704,10 @@ md_begin (void)
   for (p_cpu = csky_cpus; p_cpu->arch_flag != 0; p_cpu++)
     if ((mach_flag & CPU_ARCH_MASK) == p_cpu->arch_flag)
       {
-	bfd_elf_add_obj_attr_string (stdoutput, OBJ_ATTR_PROC,
-				     Tag_CSKY_CPU_NAME, p_cpu->name);
+	if (!bfd_elf_add_obj_attr_string (stdoutput, OBJ_ATTR_PROC,
+					  Tag_CSKY_CPU_NAME, p_cpu->name))
+	  as_fatal (_("error adding attribute: %s"),
+		    bfd_errmsg (bfd_get_error ()));
 	isa_flag |= p_cpu->isa_flag;
 	break;
       }
@@ -1856,7 +1876,9 @@ md_begin (void)
   /* Set bfd_mach to bfd backend data.  */
   bfd_set_arch_mach (stdoutput, bfd_arch_csky, bfd_mach_flag);
 
-  set_csky_attribute ();
+  if (!set_csky_attribute ())
+    as_fatal (_("error adding attribute: %s"),
+	      bfd_errmsg (bfd_get_error ()));
 }
 
 /* The C-SKY assembler emits mapping symbols $t and $d to mark the
diff --git a/gas/config/tc-mips.c b/gas/config/tc-mips.c
index d6aae660abf..24cef908c86 100644
--- a/gas/config/tc-mips.c
+++ b/gas/config/tc-mips.c
@@ -20626,8 +20626,10 @@ mips_md_finish (void)
 	    }
 	}
 
-      bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
-				Tag_GNU_MIPS_ABI_FP, fpabi);
+      if (!bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
+				     Tag_GNU_MIPS_ABI_FP, fpabi))
+	as_fatal (_("error adding attribute: %s"),
+		  bfd_errmsg (bfd_get_error ()));
     }
 }
 
diff --git a/gas/config/tc-msp430.c b/gas/config/tc-msp430.c
index c1604517f67..e3c66a3ba7c 100644
--- a/gas/config/tc-msp430.c
+++ b/gas/config/tc-msp430.c
@@ -5130,24 +5130,27 @@ msp430_md_finish (void)
   /* We have already emitted an error if any of the following attributes
      disagree with the attributes in the input assembly file.  See
      msp430_object_attribute.  */
-  bfd_elf_add_proc_attr_int (stdoutput, OFBA_MSPABI_Tag_ISA,
-			     target_is_430x () ? OFBA_MSPABI_Val_ISA_MSP430X
-			     : OFBA_MSPABI_Val_ISA_MSP430);
-
-  bfd_elf_add_proc_attr_int (stdoutput, OFBA_MSPABI_Tag_Code_Model,
-			     large_model ? OFBA_MSPABI_Val_Code_Model_LARGE
-			     : OFBA_MSPABI_Val_Code_Model_SMALL);
-
-  bfd_elf_add_proc_attr_int (stdoutput, OFBA_MSPABI_Tag_Data_Model,
-			     large_model ? OFBA_MSPABI_Val_Code_Model_LARGE
-			     : OFBA_MSPABI_Val_Code_Model_SMALL);
-
+  if (!bfd_elf_add_proc_attr_int (stdoutput, OFBA_MSPABI_Tag_ISA,
+				  target_is_430x ()
+				  ? OFBA_MSPABI_Val_ISA_MSP430X
+				  : OFBA_MSPABI_Val_ISA_MSP430)
+      || !bfd_elf_add_proc_attr_int (stdoutput, OFBA_MSPABI_Tag_Code_Model,
+				     large_model
+				     ? OFBA_MSPABI_Val_Code_Model_LARGE
+				     : OFBA_MSPABI_Val_Code_Model_SMALL)
+      || !bfd_elf_add_proc_attr_int (stdoutput, OFBA_MSPABI_Tag_Data_Model,
+				     large_model
+				     ? OFBA_MSPABI_Val_Code_Model_LARGE
+				     : OFBA_MSPABI_Val_Code_Model_SMALL)
   /* The data region GNU attribute is ignored for the small memory model.  */
-  if (large_model)
-    bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
-			      Tag_GNU_MSP430_Data_Region, lower_data_region_only
-			      ? Val_GNU_MSP430_Data_Region_Lower
-			      : Val_GNU_MSP430_Data_Region_Any);
+      || (large_model
+	  && !bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
+					Tag_GNU_MSP430_Data_Region,
+					lower_data_region_only
+					? Val_GNU_MSP430_Data_Region_Lower
+					: Val_GNU_MSP430_Data_Region_Any)))
+    as_fatal (_("error adding attribute: %s"),
+	      bfd_errmsg (bfd_get_error ()));
 }
 
 /* Returns FALSE if there is a msp430 specific reason why the
diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index 3b520ad208b..0b8ecf96681 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -4902,7 +4902,9 @@ riscv_write_out_attrs (void)
 
   /* Re-write architecture elf attribute.  */
   arch_str = riscv_rps_as.subset_list->arch_str;
-  bfd_elf_add_proc_attr_string (stdoutput, Tag_RISCV_arch, arch_str);
+  if (!bfd_elf_add_proc_attr_string (stdoutput, Tag_RISCV_arch, arch_str))
+    as_fatal (_("error adding attribute: %s"),
+	      bfd_errmsg (bfd_get_error ()));
 
   /* For the file without any instruction, we don't set the default_priv_spec
      according to the privileged elf attributes since the md_assemble isn't
@@ -4937,9 +4939,14 @@ riscv_write_out_attrs (void)
   versions[i] = number;
 
   /* Re-write privileged elf attributes.  */
-  bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec, versions[0]);
-  bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_minor, versions[1]);
-  bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_revision, versions[2]);
+  if (!bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec,
+				  versions[0])
+      || !bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_minor,
+				     versions[1])
+      || !bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_revision,
+				     versions[2]))
+    as_fatal (_("error adding attribute: %s"),
+	      bfd_errmsg (bfd_get_error ()));
 }
 
 /* Add the default contents for the .riscv.attributes section.  */
diff --git a/gas/config/tc-sparc.c b/gas/config/tc-sparc.c
index c273bd75c08..587c1594cb1 100644
--- a/gas/config/tc-sparc.c
+++ b/gas/config/tc-sparc.c
@@ -1125,10 +1125,14 @@ sparc_md_finish (void)
   hwcaps = hwcap_seen & U0xffffffff;
   hwcaps2 = hwcap_seen >> 32;
 
-  if (hwcaps)
-    bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU, Tag_GNU_Sparc_HWCAPS, hwcaps);
-  if (hwcaps2)
-    bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU, Tag_GNU_Sparc_HWCAPS2, hwcaps2);
+  if ((hwcaps
+       && !bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
+				     Tag_GNU_Sparc_HWCAPS, hwcaps))
+      || (hwcaps2
+	  && !bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
+					Tag_GNU_Sparc_HWCAPS2, hwcaps2)))
+    as_fatal (_("error adding attribute: %s"),
+	      bfd_errmsg (bfd_get_error ()));
 #endif
 }
 \f
diff --git a/gas/config/tc-tic6x.c b/gas/config/tc-tic6x.c
index 7cf4d94b473..6ed61ea49bc 100644
--- a/gas/config/tc-tic6x.c
+++ b/gas/config/tc-tic6x.c
@@ -4381,7 +4381,9 @@ tic6x_set_attribute_int (int tag, int value)
       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES)
     abort ();
   if (!tic6x_attributes_set_explicitly[tag])
-    bfd_elf_add_proc_attr_int (stdoutput, tag, value);
+    if (!bfd_elf_add_proc_attr_int (stdoutput, tag, value))
+      as_fatal (_("error adding attribute: %s"),
+		bfd_errmsg (bfd_get_error ()));
 }
 
 /* Set object attributes deduced from the input file and command line

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-09-19 22:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-19 22:58 elf-attrs.c memory allocation fail Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).