diff options
author | Matthieu Longo <matthieu.longo@arm.com> | 2025-02-24 18:32:08 +0000 |
---|---|---|
committer | Matthieu Longo <matthieu.longo@arm.com> | 2025-03-04 11:02:03 +0000 |
commit | b2bc643e097774c14507852ea74824c2f11a7b9d (patch) | |
tree | 9b420880bd78b8da9c6645b572fb6fbb92a2a338 | |
parent | 005cd152baa9aa4e56797d816cfd1f77105f385b (diff) | |
download | binutils-b2bc643e097774c14507852ea74824c2f11a7b9d.zip binutils-b2bc643e097774c14507852ea74824c2f11a7b9d.tar.gz binutils-b2bc643e097774c14507852ea74824c2f11a7b9d.tar.bz2 |
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.
-rw-r--r-- | bfd/elf-bfd.h | 2 | ||||
-rw-r--r-- | bfd/elf-properties.c | 74 |
2 files changed, 58 insertions, 18 deletions
diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index 52f8d53..5903d85 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 5c2dd01..0dea9d5 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; } |