[v1,13/14] refactoring _bfd_elf_get_property

Message ID 20250227104811.178094-14-matthieu.longo@arm.com
State New
Headers
Series Several clean-ups and refactorings before a long patch series on AArch64 build attributes |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

Matthieu Longo Feb. 27, 2025, 10:47 a.m. UTC
  - Extract _bfd_elf_find_property and _bfd_elf_insert_property from the
  function's body to improve the code readability.
- Export _bfd_elf_find_property's symbol as it will be used in a later
  commit.
---
 bfd/elf-bfd.h        |  2 ++
 bfd/elf-properties.c | 74 +++++++++++++++++++++++++++++++++-----------
 2 files changed, 58 insertions(+), 18 deletions(-)
  

Patch

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 52f8d53c116..5903d857faa 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -3085,6 +3085,8 @@  extern bool elf_read_notes (bfd *, file_ptr, bfd_size_type, size_t);
 
 extern bool _bfd_elf_parse_gnu_properties
   (bfd *, Elf_Internal_Note *);
+extern elf_property_list * _bfd_elf_find_property
+  (elf_property_list *, unsigned int, elf_property_list **);
 extern elf_property * _bfd_elf_get_property
   (bfd *, unsigned int, unsigned int);
 extern bfd *_bfd_elf_link_setup_gnu_properties
diff --git a/bfd/elf-properties.c b/bfd/elf-properties.c
index 5c2dd018cc9..0dea9d53c45 100644
--- a/bfd/elf-properties.c
+++ b/bfd/elf-properties.c
@@ -28,37 +28,72 @@ 
 #include "libbfd.h"
 #include "elf-bfd.h"
 
+/* Find a property.  */
+elf_property_list *
+_bfd_elf_find_property (elf_property_list *l,
+			unsigned int type,
+			elf_property_list **prev)
+{
+  if (prev != NULL)
+    *prev = NULL;
+
+  /* The properties are supposed to be sorted in the list.  */
+  for (elf_property_list *n = l; n != NULL; n = n->next)
+    {
+      if (type == n->property.pr_type)
+	return n;
+      else if (type < n->property.pr_type)
+	break;
+      else if (prev != NULL)
+	*prev = n;
+    }
+  return NULL;
+}
+
+/* Insert a property into the list after prev.  */
+static elf_property_list *
+_bfd_elf_insert_property (elf_property_list *l,
+			  elf_property_list *what,
+			  elf_property_list *prev)
+{
+  if (l == NULL) // First node.
+    return what;
+
+  if (prev == NULL) // Prepend.
+    {
+      what->next = l;
+      return what;
+    }
+
+  what->next = prev->next;
+  prev->next = what;
+  return l;
+}
+
 /* Get a property, allocate a new one if needed.  */
 
 elf_property *
 _bfd_elf_get_property (bfd *abfd, unsigned int type, unsigned int datasz)
 {
-  elf_property_list *p, **lastp;
-
   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
     {
       /* Never should happen.  */
       abort ();
     }
 
-  /* Keep the property list in order of type.  */
-  lastp = &elf_properties (abfd);
-  for (p = *lastp; p; p = p->next)
+  elf_property_list *prev;
+  elf_property_list *p =
+    _bfd_elf_find_property (elf_properties (abfd), type, &prev);
+  if (p != NULL)  /* Reuse the existing entry.  */
     {
-      /* Reuse the existing entry.  */
-      if (type == p->property.pr_type)
+      if (datasz > p->property.pr_datasz)
 	{
-	  if (datasz > p->property.pr_datasz)
-	    {
-	      /* This can happen when mixing 32-bit and 64-bit objects.  */
-	      p->property.pr_datasz = datasz;
-	    }
-	  return &p->property;
+	  /* This can happen when mixing 32-bit and 64-bit objects.  */
+	  p->property.pr_datasz = datasz;
 	}
-      else if (type < p->property.pr_type)
-	break;
-      lastp = &p->next;
+      return &p->property;
     }
+
   p = (elf_property_list *) bfd_alloc (abfd, sizeof (*p));
   if (p == NULL)
     {
@@ -66,11 +101,14 @@  _bfd_elf_get_property (bfd *abfd, unsigned int type, unsigned int datasz)
 			  abfd);
       _exit (EXIT_FAILURE);
     }
+
   memset (p, 0, sizeof (*p));
   p->property.pr_type = type;
   p->property.pr_datasz = datasz;
-  p->next = *lastp;
-  *lastp = p;
+
+  elf_properties (abfd) =
+    _bfd_elf_insert_property (elf_properties (abfd), p, prev);
+
   return &p->property;
 }