On AArch64, some attributes are set globally when software protections
options such as BTI, PAC or GCS are enabled. These attributes are then
used as the frozen set during the attributes merge.
In the current implementation of bfd_elfNN_aarch64_set_options, the
subsection is created before linker options are examined. If relevant
options are provided, the subsection is appended to the frozen set;
otherwise, it is left unused and leaked. This issue was detected by
LeakSanitizer (see the stack trace below).
This patch fixes the memory leak by handling the case where no attributes
are recorded. An 'else' is added to free the empty subsection when it is
not added to the frozen set.
==ERROR: LeakSanitizer: detected memory leaks
Indirect leak of 23 byte(s) in 1 object(s) allocated from:
#2 xstrdup ../../libiberty/xstrdup.c:34
#3 bfd_elf64_aarch64_set_options ../../bfd/elfnn-aarch64.c:5046
#4 aarch64_elf_create_output_section_statements <build-dir>/ld/eaarch64linux.c:358
#5 ldemul_create_output_section_statements ../../ld/ldemul.c:130
#6 lang_process ../../ld/ldlang.c:8367
#7 main ../../ld/ldmain.c:958
if (attrs_subsection->size > 0)
LINKED_LIST_APPEND (obj_attr_subsection_v2_t)
(&elf_obj_attr_subsections (output_bfd), attrs_subsection);
+ else
+ _bfd_elf_obj_attr_subsection_v2_free (attrs_subsection);
elf_aarch64_tdata (output_bfd)->gnu_property_aarch64_feature_1_and
= gnu_property_aarch64_feature_1_and;