]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
refactoring _bfd_elf_get_property
authorMatthieu Longo <matthieu.longo@arm.com>
Mon, 24 Feb 2025 18:32:08 +0000 (18:32 +0000)
committerMatthieu Longo <matthieu.longo@arm.com>
Tue, 4 Mar 2025 11:02:03 +0000 (11:02 +0000)
- 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
bfd/elf-properties.c

index 52f8d53c116f93eec9c07a09ba63b8ee652cf235..5903d857faad2a15afe9320276e6f62275258890 100644 (file)
@@ -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
index 5c2dd018cc9f220eee4a2fe7b82fbbade856ec0c..0dea9d53c45dea7009b42003907e88e1884950bc 100644 (file)
 #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;
 }