refactoring _bfd_elf_get_property

- 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.
This commit is contained in:
Matthieu Longo
2025-02-24 18:32:08 +00:00
parent 005cd152ba
commit b2bc643e09
2 changed files with 58 additions and 18 deletions

View 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

View File

@@ -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;
}