]> git.ipfire.org Git - thirdparty/elfutils.git/commit
src/strip.c: Unconditionally re-iterate when preserving sh_link/sh_info
authorAaron Merey <amerey@redhat.com>
Sun, 10 May 2026 20:13:02 +0000 (16:13 -0400)
committerAaron Merey <amerey@redhat.com>
Wed, 13 May 2026 14:57:07 +0000 (10:57 -0400)
commit033757c9ec69eb0ac6e2235b59df33da5ddf14a8
tree6b476d3fb0fc0a8ff8d8fb0e4921cfc8060ed03c
parentbfd519cc58e190544a6785d3f0a27fcfaf7d8da3
src/strip.c: Unconditionally re-iterate when preserving sh_link/sh_info

handle_elf may perform multiple iterations over section headers to
ensure that if a section is included in the debug file, then its
sh_link and sh_info sections are also included in the debug file.

handle_elf contains an optimization where an additional iteration may
be avoided if the sh_link or sh_info of a section marked for inclusion
in the main elf file have yet to be investigated by the current iteration.
The additional iteration is scheduled only if the loop index 'cnt' has
already passed the sh_link or sh_info.

This optimization can cause a bug where the sh_link or sh_info of
a section in the debug file may not be present in the debug file.

sh_link/sh_info cross references between .symtab and .rel.* sections
might be ordered such that .symtab is marked for stripping while iterating
over all the .rel.debug_* sections that reference .symtab in their sh_link,
resulting in no additional debug-preservation handling for .symtab.
However, it's possible later in the loop for a .rel.* section to refer
to an allocated section via sh_info and .symtab via sh_link.  In this case,
.symtab gets marked for preservation in the main elf file.  If no other
.rel.debug_* sections are encountered for the rest of the iteration, then
debug-preservation isn't triggered for .symtab.  This results in the debug
file containing a NOBITS .symtab despite .rel.debug_* sections referencing
.symtab in their sh_link.  If --reloc-debug-sections is given, a segfault
or error may occur when attempting to resolve these relocations.

Fix this by unconditionally scheduling another iteration across section
headers when an sh_link or sh_info gets flagged for inclusion in the main
elf file.

For the above case, the additional iteration will trigger debug-preservation
for .symtab upon detection that a .rel.debug_* sh_link is being kept in the
main elf file.  .symtab will then be present in both the main elf file and
debug file.

https://sourceware.org/bugzilla/show_bug.cgi?id=34097

Signed-off-by: Aaron Merey <amerey@redhat.com>
src/strip.c