From 36cfdc18ed1fea1132cc4145e32b3645fcfb132b Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 29 Feb 2024 19:44:19 -0800 Subject: [PATCH] libbacktrace: read symbol table of debuginfo file These become another symbol table to search. This is needed if people use --strip-all rather than --strip-debug when adding a debuglink section. Patch is from GitHub user pickard1. Fixes #113 * elf.c (elf_add): Add the symbol table from a debuginfo file. * Makefile.am (MAKETESTS): Add buildidfull and gnudebuglinkfull variants of buildid and gnudebuglink tests. (%_gnudebuglinkfull, %_buildidfull): New patterns. * Makefile.in: Regenerate. --- Makefile.am | 16 +++++++++++++--- Makefile.in | 37 ++++++++++++++++++++++++++++++++++--- elf.c | 2 +- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Makefile.am b/Makefile.am index 5f1d0f8..3d67909 100644 --- a/Makefile.am +++ b/Makefile.am @@ -258,7 +258,7 @@ b2test_LDFLAGS = -Wl,--build-id b2test_LDADD = libbacktrace_elf_for_test.la check_PROGRAMS += b2test -MAKETESTS += b2test_buildid +MAKETESTS += b2test_buildid b2test_buildidfull if HAVE_DWZ @@ -268,7 +268,7 @@ b3test_LDFLAGS = -Wl,--build-id b3test_LDADD = libbacktrace_elf_for_test.la check_PROGRAMS += b3test -MAKETESTS += b3test_dwz_buildid +MAKETESTS += b3test_dwz_buildid b3test_dwz_buildidfull endif HAVE_DWZ @@ -444,12 +444,16 @@ endif HAVE_PTHREAD if HAVE_OBJCOPY_DEBUGLINK -MAKETESTS += btest_gnudebuglink +MAKETESTS += btest_gnudebuglink btest_gnudebuglinkfull %_gnudebuglink: % $(OBJCOPY) --only-keep-debug $< $@.debug $(OBJCOPY) --strip-debug --add-gnu-debuglink=$@.debug $< $@ +%_gnudebuglinkfull: % + $(OBJCOPY) --only-keep-debug $< $@.debug + $(OBJCOPY) --strip-all --add-gnu-debuglink=$@.debug $< $@ + endif HAVE_OBJCOPY_DEBUGLINK %_buildid: % @@ -458,6 +462,12 @@ endif HAVE_OBJCOPY_DEBUGLINK $< $(OBJCOPY) --strip-debug $< $@ +%_buildidfull: % + ./install-debuginfo-for-buildid.sh \ + "$(TEST_BUILD_ID_DIR)" \ + $< + $(OBJCOPY) --strip-all $< $@ + if HAVE_COMPRESSED_DEBUG ctestg_SOURCES = btest.c testlib.c diff --git a/Makefile.in b/Makefile.in index f5ea83f..effdc27 100644 --- a/Makefile.in +++ b/Makefile.in @@ -135,9 +135,9 @@ TESTS = $(am__append_4) $(MAKETESTS) $(am__EXEEXT_16) @NATIVE_TRUE@@USE_DSYMUTIL_TRUE@ stest.dSYM stest_alloc.dSYM \ @NATIVE_TRUE@@USE_DSYMUTIL_TRUE@ edtest.dSYM edtest_alloc.dSYM @HAVE_BUILDID_TRUE@@HAVE_ELF_TRUE@@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@am__append_6 = b2test -@HAVE_BUILDID_TRUE@@HAVE_ELF_TRUE@@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@am__append_7 = b2test_buildid +@HAVE_BUILDID_TRUE@@HAVE_ELF_TRUE@@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@am__append_7 = b2test_buildid b2test_buildidfull @HAVE_BUILDID_TRUE@@HAVE_DWZ_TRUE@@HAVE_ELF_TRUE@@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@am__append_8 = b3test -@HAVE_BUILDID_TRUE@@HAVE_DWZ_TRUE@@HAVE_ELF_TRUE@@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@am__append_9 = b3test_dwz_buildid +@HAVE_BUILDID_TRUE@@HAVE_DWZ_TRUE@@HAVE_ELF_TRUE@@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@am__append_9 = b3test_dwz_buildid b3test_dwz_buildidfull @HAVE_ELF_TRUE@@NATIVE_TRUE@am__append_10 = btest_lto @NATIVE_TRUE@am__append_11 = btest_alloc stest stest_alloc @HAVE_DWZ_TRUE@@NATIVE_TRUE@am__append_12 = btest_dwz @@ -153,7 +153,7 @@ TESTS = $(am__append_4) $(MAKETESTS) $(am__EXEEXT_16) @HAVE_PTHREAD_TRUE@@NATIVE_TRUE@@USE_DSYMUTIL_TRUE@am__append_21 = \ @HAVE_PTHREAD_TRUE@@NATIVE_TRUE@@USE_DSYMUTIL_TRUE@ ttest.dSYM \ @HAVE_PTHREAD_TRUE@@NATIVE_TRUE@@USE_DSYMUTIL_TRUE@ ttest_alloc.dSYM -@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@am__append_22 = btest_gnudebuglink +@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@am__append_22 = btest_gnudebuglink btest_gnudebuglinkfull @HAVE_COMPRESSED_DEBUG_TRUE@@NATIVE_TRUE@am__append_23 = ctestg ctesta @HAVE_COMPRESSED_DEBUG_TRUE@@HAVE_COMPRESSED_DEBUG_ZSTD_TRUE@@NATIVE_TRUE@am__append_24 = ctestzstd @HAVE_COMPRESSED_DEBUG_TRUE@@NATIVE_TRUE@am__append_25 = ctestg_alloc ctesta_alloc @@ -2137,6 +2137,13 @@ b2test_buildid.log: b2test_buildid --log-file $$b.log --trs-file $$b.trs \ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ "$$tst" $(AM_TESTS_FD_REDIRECT) +b2test_buildidfull.log: b2test_buildidfull + @p='b2test_buildidfull'; \ + b='b2test_buildidfull'; \ + $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \ + --log-file $$b.log --trs-file $$b.trs \ + $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ + "$$tst" $(AM_TESTS_FD_REDIRECT) b3test_dwz_buildid.log: b3test_dwz_buildid @p='b3test_dwz_buildid'; \ b='b3test_dwz_buildid'; \ @@ -2144,6 +2151,13 @@ b3test_dwz_buildid.log: b3test_dwz_buildid --log-file $$b.log --trs-file $$b.trs \ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ "$$tst" $(AM_TESTS_FD_REDIRECT) +b3test_dwz_buildidfull.log: b3test_dwz_buildidfull + @p='b3test_dwz_buildidfull'; \ + b='b3test_dwz_buildidfull'; \ + $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \ + --log-file $$b.log --trs-file $$b.trs \ + $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ + "$$tst" $(AM_TESTS_FD_REDIRECT) btest_dwz.log: btest_dwz @p='btest_dwz'; \ b='btest_dwz'; \ @@ -2165,6 +2179,13 @@ btest_gnudebuglink.log: btest_gnudebuglink --log-file $$b.log --trs-file $$b.trs \ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ "$$tst" $(AM_TESTS_FD_REDIRECT) +btest_gnudebuglinkfull.log: btest_gnudebuglinkfull + @p='btest_gnudebuglinkfull'; \ + b='btest_gnudebuglinkfull'; \ + $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \ + --log-file $$b.log --trs-file $$b.trs \ + $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ + "$$tst" $(AM_TESTS_FD_REDIRECT) mtest_minidebug.log: mtest_minidebug @p='mtest_minidebug'; \ b='mtest_minidebug'; \ @@ -2604,12 +2625,22 @@ uninstall-am: uninstall-includeHEADERS uninstall-libLTLIBRARIES @HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@ $(OBJCOPY) --only-keep-debug $< $@.debug @HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@ $(OBJCOPY) --strip-debug --add-gnu-debuglink=$@.debug $< $@ +@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@%_gnudebuglinkfull: % +@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@ $(OBJCOPY) --only-keep-debug $< $@.debug +@HAVE_OBJCOPY_DEBUGLINK_TRUE@@NATIVE_TRUE@ $(OBJCOPY) --strip-all --add-gnu-debuglink=$@.debug $< $@ + @NATIVE_TRUE@%_buildid: % @NATIVE_TRUE@ ./install-debuginfo-for-buildid.sh \ @NATIVE_TRUE@ "$(TEST_BUILD_ID_DIR)" \ @NATIVE_TRUE@ $< @NATIVE_TRUE@ $(OBJCOPY) --strip-debug $< $@ +@NATIVE_TRUE@%_buildidfull: % +@NATIVE_TRUE@ ./install-debuginfo-for-buildid.sh \ +@NATIVE_TRUE@ "$(TEST_BUILD_ID_DIR)" \ +@NATIVE_TRUE@ $< +@NATIVE_TRUE@ $(OBJCOPY) --strip-all $< $@ + @HAVE_MINIDEBUG_TRUE@@NATIVE_TRUE@%_minidebug: % @HAVE_MINIDEBUG_TRUE@@NATIVE_TRUE@ $(NM) -D $< -P --defined-only | $(AWK) '{ print $$1 }' | sort > $<.dsyms @HAVE_MINIDEBUG_TRUE@@NATIVE_TRUE@ $(NM) $< -P --defined-only | $(AWK) '{ if ($$2 == "T" || $$2 == "t" || $$2 == "D") print $$1 }' | sort > $<.fsyms diff --git a/elf.c b/elf.c index f0450cb..e35a5da 100644 --- a/elf.c +++ b/elf.c @@ -6872,7 +6872,7 @@ elf_add (struct backtrace_state *state, const char *filename, int descriptor, if (symtab_shndx == 0) symtab_shndx = dynsym_shndx; - if (symtab_shndx != 0 && !debuginfo) + if (symtab_shndx != 0) { const b_elf_shdr *symtab_shdr; unsigned int strtab_shndx;