libbacktrace could get an infinite recursion in an odd case in which a
.gnu_debugdata section was added to a debug file, and mini_debuginfo
was put into the debug file, and the debug file was put into a
/usr/lib/debug directory to be found by build ID.  This combination
doesn't really make sense but we shouldn't get an infinite recursion.
This patch fixes the problem.  Bootstrapped and ran libbacktrace tests
on x86_64-pc-linux-gnu.  Committed to mainline.

Ian

            * elf.c (elf_add): Don't use .gnu_debugdata if we are already
            reading a debuginfo file.
            * Makefile.am (m2test_*): New test targets.
            (CHECK_PROGRAMS): Add m2test.
            (MAKETESTS): Add m2test_minidebug2.
            (%_minidebug2): New pattern.
            (CLEANFILES): Remove minidebug2 files.
            * Makefile.in: Regenerate.
8a3a34665210105a89b14f380c3bec780c209046
diff --git a/libbacktrace/Makefile.am b/libbacktrace/Makefile.am
index bed42c29329..8215cfd9bd5 100644
--- a/libbacktrace/Makefile.am
+++ b/libbacktrace/Makefile.am
@@ -594,6 +594,39 @@ MAKETESTS += mtest_minidebug
        $(OBJCOPY) --add-section .gnu_debugdata=$<.mdbg.xz $<.strip
        mv $<.strip $@
 
+if HAVE_ELF
+if HAVE_BUILDID
+if HAVE_OBJCOPY_DEBUGLINK
+
+m2test_SOURCES = $(mtest_SOURCES)
+m2test_CFLAGS = $(libbacktrace_TEST_CFLAGS) -O
+m2test_LDFLAGS = -Wl,--build-id $(libbacktrace_testing_ldflags)
+m2test_LDADD = libbacktrace_elf_for_test.la
+
+check_PROGRAMS += m2test
+MAKETESTS += m2test_minidebug2
+
+# minidebug2 is like minidebug but also adds the gnu_debugdata section
+# to the debug file, and uses a build ID file.  There is no reason to do
+# this but it was causing an infinite recursion.
+%_minidebug2: %
+       $(NM) -D $< -P --defined-only | $(AWK) '{ print $$1 }' | sort > 
$<.dsyms2
+       $(NM) $< -P --defined-only | $(AWK) '{ if ($$2 == "T" || $$2 == "t" || 
$$2 == "D") print $$1 }' | sort > $<.fsyms2
+       $(COMM) -13 $<.dsyms2 $<.fsyms2 > $<.keepsyms2
+       $(OBJCOPY) --only-keep-debug $< $<.dbg2
+       $(OBJCOPY) -S --remove-section .gdb_index --remove-section .comment 
--keep-symbols=$<.keepsyms2 $<.dbg2 $<.mdbg2
+       $(OBJCOPY) --strip-all --remove-section ..comment $< $<.strip2
+       rm -f $<.mdbg2.xz
+       $(XZ) $<.mdbg2
+       $(OBJCOPY) --add-section .gnu_debugdata=$<.mdbg2.xz $<.dbg2
+       $(OBJCOPY) --add-section .gnu_debugdata=$<.mdbg2.xz $<.strip2
+       $(SHELL) ./install-debuginfo-for-buildid.sh $(TEST_BUILD_ID_DIR) $<.dbg2
+       mv $<.strip2 $@
+
+endif HAVE_OBJCOPY_DEBUGLINK
+endif HAVE_BUILDID
+endif HAVE_ELF
+
 endif HAVE_MINIDEBUG
 
 endif NATIVE
@@ -629,7 +662,8 @@ TESTS += $(MAKETESTS) $(BUILDTESTS)
 CLEANFILES = \
        $(MAKETESTS) $(BUILDTESTS) *.debug elf_for_test.c edtest2_build.c \
        gen_edtest2_build \
-       *.dsyms *.fsyms *.keepsyms *.dbg *.mdbg *.mdbg.xz *.strip
+       *.dsyms *.fsyms *.keepsyms *.dbg *.mdbg *.mdbg.xz *.strip \
+       *.dsyms2 *.fsyms2 *.keepsyms2 *.dbg2 *.mdbg2 *.mdbg2.xz *.strip2
 
 clean-local:
        -rm -rf usr
diff --git a/libbacktrace/elf.c b/libbacktrace/elf.c
index e6a66c0db90..107c96892a0 100644
--- a/libbacktrace/elf.c
+++ b/libbacktrace/elf.c
@@ -6841,7 +6841,8 @@ elf_add (struct backtrace_state *state, const char 
*filename, int descriptor,
            }
        }
 
-      if (!gnu_debugdata_view_valid
+      if (!debuginfo
+         && !gnu_debugdata_view_valid
          && strcmp (name, ".gnu_debugdata") == 0)
        {
          if (!elf_get_view (state, descriptor, memory, memory_size,

Reply via email to