[PATCH v10 16/28] OAv2 merge: create a section for object attributes

Matthieu Longo matthieu.longo@arm.com
Thu Nov 20 17:59:07 GMT 2025
bfd copies object attributes to the output BFD only if the input BFD —
used to store the merge result— contains an object attribute section.
bfd_linear_find_first_with_obj_attrs() returns an input BFD that may or
may not already include such a section.  If no object attributes section
is found, the merge process must create one.

This patch implements create_object_attributes_section(), which constructs
the missing section with the appropriate flags, type, and alignment.  The
implementation is adapted from _bfd_elf_link_create_gnu_property_sec() in
bfd/elf-properties.c.  The code preserves the different alignment
requirements for 32-bit and 64-bit ELF formats, although the rationale for
this distinction remains unclear in the original source and patch history.
---
 bfd/elf-attrs.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/bfd/elf-attrs.c b/bfd/elf-attrs.c
index c34d4b21daf..f432412e45e 100644
--- a/bfd/elf-attrs.c
+++ b/bfd/elf-attrs.c
@@ -812,10 +812,35 @@ static asection *
 create_object_attributes_section (struct bfd_link_info *info,
 				  bfd *abfd)
 {
-  (void) info;
-  (void) abfd;
-  /* TO IMPLEMENT */
-  return NULL;
+  asection *sec;
+  const char *sec_name = get_elf_backend_data (abfd)->obj_attrs_section;
+  sec = bfd_make_section_with_flags (abfd,
+				     sec_name,
+				     (SEC_READONLY
+				      | SEC_HAS_CONTENTS
+				      | SEC_DATA));
+  if (sec == NULL)
+    info->callbacks->fatal (_("%P: failed to create %s section\n"), sec_name);
+
+  /* It is unclear why the format requires a different alignment for
+     32- vs 64-bit ELF.  This code was copy-pasted from
+     _bfd_elf_link_create_gnu_property_sec() in bfd/elf-properties.c.  The
+     original patch which introduced it, does not provide any reason and no
+     discussion during the patch review focussed on this part of the code.
+     https://inbox.sourceware.org/binutils/20210624132411.1993105-1-hjl.tools
+     @gmail.com/  */
+  unsigned align
+    = (get_elf_backend_data (info->output_bfd)->s->elfclass == ELFCLASS64
+       ? 3
+       : 2);
+  if (!bfd_set_section_alignment (sec, align))
+    info->callbacks->fatal (_("%pA: failed to align section\n"), sec);
+
+  elf_section_type (sec) = get_elf_backend_data (abfd)->obj_attrs_section_type;
+
+  bfd_set_section_size (sec, bfd_elf_obj_attr_size (abfd));
+
+  return sec;
 }
 
 /* Translate GNU properties that have object attributes v2 equivalents.  */
-- 
2.52.0



More information about the Binutils mailing list