From 06eac9d714d1daeee15696e84181646cf24cc71d Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Sun, 1 Nov 2026 15:01:19 +0100 Subject: [PATCH] dev-debug/gdb: fix crash when max completions reached exception unwinds stack through C frames Bug: https://sourceware.org/PR34876 Closes: https://bugs.gentoo.org/984278 Signed-off-by: Boris Staletic Part-of: https://codeberg.org/gentoo/gentoo/pulls/2277 Merges: https://codeberg.org/gentoo/gentoo/pulls/2277 Signed-off-by: Sam James --- ...rash-when-unwinding-through-c-frames.patch | 294 +++++++++++++++ dev-debug/gdb/gdb-18.1-r2.ebuild | 357 ++++++++++++++++++ 2 files changed, 651 insertions(+) create mode 100644 dev-debug/gdb/files/gdb-18.1-avoid-crash-when-unwinding-through-c-frames.patch create mode 100644 dev-debug/gdb/gdb-18.1-r2.ebuild diff --git a/dev-debug/gdb/files/gdb-18.1-avoid-crash-when-unwinding-through-c-frames.patch b/dev-debug/gdb/files/gdb-18.1-avoid-crash-when-unwinding-through-c-frames.patch new file mode 100644 index 0000000000000..8fc2976d7f7dd --- /dev/null +++ b/dev-debug/gdb/files/gdb-18.1-avoid-crash-when-unwinding-through-c-frames.patch @@ -0,0 +1,294 @@ +From 6974e8c3bf4a9bde98f1e24b882675412f7de9cf Mon Sep 17 00:00:00 2001 +From: Simon Marchi +Date: Mon, 26 Oct 2026 10:45:56 -0400 +Subject: [PATCH] gdb/macrotab: catch exception and rethrow around + splay_tree_foreach + +Bug 34876 talks about a crash that happens with "-O3 -flto", when +completing and reaching the maximum number of completions. + +It can be reproduced by building GDB in this configuration: + + $ ./configure CFLAGS="-march=x86-64-v3 -flto -O3" CXXFLAGS="-march=x86-64-v3 -flto -O3" --disable-nls + +Then, using a dummy C program compiled with "gcc -g3", this crashes GDB: + + $ ./gdb -q -nx --data-directory data-directory ./a.out -ex list -ex "complete macro expand " + +The crash manifests as an invalid free, reported by glibc: + + free(): invalid pointer + +It is reported here: + + #0 0x00007ffff6a9a11c in ?? () from /usr/lib/libc.so.6 + #1 0x00007ffff6a3e5d0 in raise () from /usr/lib/libc.so.6 + #2 0x00007ffff6a25685 in abort () from /usr/lib/libc.so.6 + #3 0x00007ffff6a26704 in ?? () from /usr/lib/libc.so.6 + #4 0x00007ffff6aa47ec in ?? () from /usr/lib/libc.so.6 + #5 0x00007ffff6aa4804 in ?? () from /usr/lib/libc.so.6 + #6 0x00005555556e07ac in operator delete (p=) at /home/simark/src/binutils-gdb/gdbsupport/new-op.cc:108 + #7 operator delete (p=) at /home/simark/src/binutils-gdb/gdbsupport/new-op.cc:114 + #8 operator delete (p=) at /home/simark/src/binutils-gdb/gdbsupport/new-op.cc:120 + #9 std::__new_allocator::deallocate (this=0x555555caa4f8 , __p=, __n=) at /usr/include/c++/16/bits/new_allocator.h:183 + #10 std::allocator::deallocate (this=0x555555caa4f8 , __p=, __n=) at /usr/include/c++/16/bits/allocator.h:218 + #11 std::allocator_traits >::deallocate (__a=..., __p=, __n=) at /usr/include/c++/16/bits/alloc_traits.h:689 + #12 std::__cxx11::basic_string, std::allocator >::_M_destroy (this=0x555555caa4f8 , __size=) at /usr/include/c++/16/bits/basic_string.h:304 + #13 std::__cxx11::basic_string, std::allocator >::_M_dispose (this=0x555555caa4f8 ) at /usr/include/c++/16/bits/basic_string.h:298 + #14 std::__cxx11::basic_string, std::allocator >::~basic_string (this=0x555555caa4f8 , this=) at /usr/include/c++/16/bits/basic_string.h:920 + #15 demangle_for_lookup_info::~demangle_for_lookup_info (this=0x555555caa4f8 , this=) at /home/simark/src/binutils-gdb/gdb/symtab.h:183 + #16 std::_Optional_payload_base::_M_destroy (this=0x555555caa4f8 ) at /usr/include/c++/16/optional:325 + #17 std::_Optional_payload_base::_M_reset (this=0x555555caa4f8 ) at /usr/include/c++/16/optional:361 + #18 std::_Optional_payload::~_Optional_payload (this=0x555555caa4f8 , this=) at /usr/include/c++/16/optional:484 + #19 std::_Optional_base::~_Optional_base (this=0x555555caa4f8 , this=) at /usr/include/c++/16/optional:508 + #20 std::optional::~optional (this=0x555555caa4f8 , this=) at /usr/include/c++/16/optional:812 + #21 lookup_name_info::~lookup_name_info (this=0x555555caa440 ::bind(default_collect_symbol_completion_matches_break_on(completion_tracker&, complete_symbol_mode, symbol_name_match_type, char const*, char const*, char const*, type_code)::{lambda(char const*, macro_definition const*, macro_source_file*, int)#1}&)::{lambda(gdb::fv_detail::erased_callable, char const*, macro_definition const*, macro_source_file*, int)#1}::_FUN(gdb::fv_detail::erased_callable, char const*, macro_definition const*, macro_source_file*, int)>, this=) at /home/simark/src/binutils-gdb/gdb/symtab.h:210 + #22 0x000055555569ca89 in default_collect_symbol_completion_matches_break_on (tracker=..., mode=, name_match_type=, text=, word=, break_on=, code=) at /home/simark/src/binutils-gdb/gdb/symtab.c:6111 + #23 0x0000555555826c22 in language_defn::collect_symbol_completion_matches (this=, tracker=..., mode=, name_match_type=, text=, word=, code=, this=, tracker=..., mode=, name_match_type=, text=, word=, code=) at /home/simark/src/binutils-gdb/gdb/language.h:477 + #24 collect_symbol_completion_matches (tracker=..., mode=complete_symbol_mode::EXPRESSION, name_match_type=symbol_name_match_type::EXPRESSION, text=, word=) at /home/simark/src/binutils-gdb/gdb/symtab.c:6122 + #25 symbol_completer (ignore=, tracker=..., text=, word=) at /home/simark/src/binutils-gdb/gdb/completer.c:1533 + #26 0x000055555583ef85 in complete_line_internal_normal_command (command=0x7fffffffd880 "macro expand ", tracker=..., word=0x7fffffffd88d "", cmd_args=, reason=handle_completions, c=0x5555563b02c0) at /home/simark/src/binutils-gdb/gdb/completer.c:1620 + #27 complete_line_internal_1 (tracker=..., text=text@entry=0x7fffffffe7e2 "", line_buffer=line_buffer@entry=0x7fffffffe7d5 "macro expand ", point=, reason=reason@entry=handle_completions) at /home/simark/src/binutils-gdb/gdb/completer.c:1843 + #28 0x000055555583fe81 in complete_line_internal (tracker=..., text=0x7fffffffe7e2 "", line_buffer=, point=, reason=handle_completions) at /home/simark/src/binutils-gdb/gdb/completer.c:1862 + #29 complete_line (tracker=..., text=0x7fffffffe7e2 "", line_buffer=, point=) at /home/simark/src/binutils-gdb/gdb/completer.c:2157 + #30 complete_line (tracker=..., text=0x7fffffffe7e2 "", line_buffer=, point=) at /home/simark/src/binutils-gdb/gdb/completer.c:2152 + #31 complete(char const*, char const**, int*) [clone .constprop.0] (line=0x7fffffffe7d5 "macro expand ", word=0x7fffffffdb88, quote_char=) at /home/simark/src/binutils-gdb/gdb/completer.c:2126 + #32 0x00005555557e16db in complete_command (arg=0x7fffffffe7d5 "macro expand ", from_tty=) at /home/simark/src/binutils-gdb/gdb/cli/cli-cmds.c:425 + +I asked Claude to find the root cause, and it came up with the following +explanation: + + 1. With -batch, or after 200 matches interactively, + completion_tracker::add_completion throws + MAX_COMPLETIONS_REACHED_ERROR. + + 2. That throw happens inside the add_macro_name callback, which is called + from libiberty's splay_tree_foreach (C code) via + foreach_macro_in_scope in gdb/macrotab.c. + + 3. libiberty is compiled as C without -fexceptions, so GCC treats + splay_tree_foreach as nothrow. With LTO, GCC can see that across + translation units, so it decides macro_for_each_in_scope can't throw + either. It then compiles the call at symtab.c:6052 as non-throwing. + + 4. At runtime the exception still unwinds, because x86-64 has CFI + everywhere. But it lands in a landing pad that doesn't belong to that + call site. That pad runs ~lookup_name_info() with this = %rbx. At the + throwing call, %rbx actually holds the function_view invoker address + (0x555555b32780, a code address), so it destroys garbage and hands a + bogus pointer to free(). + +I can't tell for sure that it's correct, but it sounds very plausible to +me (the address 0x555555b32780 is from another run on another computer, not +related to the backtrace above). + +On x86-64, the psABI mandates that all programs and libraries have an async +unwind table (.eh_frame), so the exception is able to go through the C +frames unnoticed most of the time. But on ARM, for example, programs don't +have unwind tables by default. I was able to reproduce the bug on my old +Raspberry Pi running an outdated Debian: + + (gdb) show version + GNU gdb (Raspbian 10.1-1.7) 10.1.90.20210103-git + ... + (gdb) list + 1 int main() + 2 { + 3 } + (gdb) complete macro expand + terminate called after throwing an instance of 'gdb_exception_error' + Aborted + +There, the exception causes std::terminate to be called as soon as it +reaches the libiberty C frame. + +This patch updates the two callbacks passed to splay_tree_foreach in +this file (foreach_macro and foreach_macro_in_scope) to catch any +exception that would escape, stash it in macro_for_each_data::exception, +and return 1 to stop the splay tree iteration. The caller of +splay_tree_foreach then rethrows the exception if there is one. + +Since I'm re-indenting the code anyway, I took the opportunity to +modernize it a bit. + +Improve gdb.base/macscp.exp to kind of cover this. The test won't catch +the bug on a regular x86-64 build, but it would in an LTO build like in +the bug report, or on ARM without unwind tables. It would also catch it +if we just added "noexcept" to the callbacks. + +There is another latent instance of this bug in addrmap.c, where we call +splay_tree_foreach and the callback can throw. And probably more when +using other APIs. As future work, I propose that we go over all the +callbacks that we pass to a C API to mark them "noexcept", so that these +cases of exceptions crossing C frames consistently crash GDB on any +configuration. And fix them if necessary to catch and rethrow +exceptions like done in this patch. + +Change-Id: Ie7c4515a4dee1c93b04271f1d5057be23c96c039 +Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34876 +Approved-By: Andrew Burgess +--- + gdb/macrotab.c | 75 ++++++++++++++++++++----------- + gdb/testsuite/gdb.base/macscp.exp | 27 +++++++++++ + 2 files changed, 76 insertions(+), 26 deletions(-) + +diff --git a/gdb/macrotab.c b/gdb/macrotab.c +index 078eb5cb..ef5b700b 100644 +--- a/gdb/macrotab.c ++++ b/gdb/macrotab.c +@@ -945,22 +945,31 @@ struct macro_for_each_data + gdb::function_view fn; + struct macro_source_file *file; + int line; ++ std::exception_ptr exception; + }; + + /* Helper function for macro_for_each. */ + static int +-foreach_macro (splay_tree_node node, void *arg) ++foreach_macro (splay_tree_node node, void *arg) noexcept + { +- struct macro_for_each_data *datum = (struct macro_for_each_data *) arg; +- struct macro_key *key = (struct macro_key *) node->key; +- struct macro_definition *def; ++ auto datum = static_cast (arg); + +- std::string key_fullname = macro_source_fullname (key->start_file); +- def = fixup_definition (key_fullname.c_str (), key->start_line, +- (struct macro_definition *) node->value); ++ try ++ { ++ auto key = reinterpret_cast (node->key); ++ auto value = reinterpret_cast (node->value); ++ std::string key_fullname = macro_source_fullname (key->start_file); ++ macro_definition *def = fixup_definition (key_fullname.c_str (), ++ key->start_line, value); + +- datum->fn (key->name, def, key->start_file, key->start_line); +- return 0; ++ datum->fn (key->name, def, key->start_file, key->start_line); ++ return 0; ++ } ++ catch (...) ++ { ++ datum->exception = std::current_exception (); ++ return 1; ++ } + } + + /* Call FN for every macro in TABLE. */ +@@ -971,31 +980,42 @@ macro_for_each (struct macro_table *table, + struct macro_for_each_data datum; + + datum.fn = fn; +- datum.file = NULL; ++ datum.file = nullptr; + datum.line = 0; + splay_tree_foreach (table->definitions, foreach_macro, &datum); ++ ++ if (datum.exception != nullptr) ++ std::rethrow_exception (datum.exception); + } + + static int +-foreach_macro_in_scope (splay_tree_node node, void *info) ++foreach_macro_in_scope (splay_tree_node node, void *info) noexcept + { +- struct macro_for_each_data *datum = (struct macro_for_each_data *) info; +- struct macro_key *key = (struct macro_key *) node->key; +- struct macro_definition *def; ++ auto datum = static_cast (info); + +- std::string datum_fullname = macro_source_fullname (datum->file); +- def = fixup_definition (datum_fullname.c_str (), datum->line, +- (struct macro_definition *) node->value); ++ try ++ { ++ auto key = reinterpret_cast (node->key); ++ auto value = reinterpret_cast (node->value); ++ std::string datum_fullname = macro_source_fullname (datum->file); ++ macro_definition *def = fixup_definition (datum_fullname.c_str (), ++ datum->line, value); + +- /* See if this macro is defined before the passed-in line, and +- extends past that line. */ +- if (compare_locations (key->start_file, key->start_line, +- datum->file, datum->line) < 0 +- && (!key->end_file +- || compare_locations (key->end_file, key->end_line, +- datum->file, datum->line) >= 0)) +- datum->fn (key->name, def, key->start_file, key->start_line); +- return 0; ++ /* See if this macro is defined before the passed-in line, and ++ extends past that line. */ ++ if (compare_locations (key->start_file, key->start_line, ++ datum->file, datum->line) < 0 ++ && (key->end_file == nullptr ++ || compare_locations (key->end_file, key->end_line, ++ datum->file, datum->line) >= 0)) ++ datum->fn (key->name, def, key->start_file, key->start_line); ++ return 0; ++ } ++ catch (...) ++ { ++ datum->exception = std::current_exception (); ++ return 1; ++ } + } + + /* Call FN for every macro is visible in SCOPE. */ +@@ -1010,6 +1030,9 @@ macro_for_each_in_scope (struct macro_source_file *file, int line, + datum.line = line; + splay_tree_foreach (file->table->definitions, + foreach_macro_in_scope, &datum); ++ ++ if (datum.exception != nullptr) ++ std::rethrow_exception (datum.exception); + } + + +diff --git a/gdb/testsuite/gdb.base/macscp.exp b/gdb/testsuite/gdb.base/macscp.exp +index e40bfb63..e0bcddb9 100644 +--- a/gdb/testsuite/gdb.base/macscp.exp ++++ b/gdb/testsuite/gdb.base/macscp.exp +@@ -14,6 +14,7 @@ + # You should have received a copy of the GNU General Public License + # along with this program. If not, see . + ++load_lib completion-support.exp + + standard_testfile macscp1.c + set objfile [standard_output_file ${testfile}.o] +@@ -750,3 +751,29 @@ gdb_test "macro expand siginfo.si_addr" \ + gdb_test "print __FILE__" " = \".*macscp1.c\"" + gdb_test "print __LINE__" \ + " = [gdb_get_line_number {stopping point for line test}]" ++ ++# Regression test for bug 34876. ++ ++with_test_prefix "max-completions reached" { ++ with_set max-completions 2 { ++ # Macros from the program, visited by foreach_macro_in_scope. All ++ # the BEFORE_ macros are in scope at this point. The macros ++ # are visited in alphabetical order. ++ test_gdb_complete_multiple "macro expand " "BEFORE_" "MACSCP1_" { ++ "BEFORE_MACSCP1_1" ++ "BEFORE_MACSCP1_2" ++ } "" "" true "program macros" ++ ++ # User-defined macros, visited by foreach_macro. ++ gdb_test_no_output "macro define USER_MACRO_A 1" ++ gdb_test_no_output "macro define USER_MACRO_B 2" ++ gdb_test_no_output "macro define USER_MACRO_C 3" ++ test_gdb_complete_multiple "macro expand " "USER_MACRO_" "" { ++ "USER_MACRO_A" ++ "USER_MACRO_B" ++ } "" "" true "user macros" ++ gdb_test_no_output "macro undef USER_MACRO_A" ++ gdb_test_no_output "macro undef USER_MACRO_B" ++ gdb_test_no_output "macro undef USER_MACRO_C" ++ } ++} +-- +2.56.0 + diff --git a/dev-debug/gdb/gdb-18.1-r2.ebuild b/dev-debug/gdb/gdb-18.1-r2.ebuild new file mode 100644 index 0000000000000..8d80e558bf504 --- /dev/null +++ b/dev-debug/gdb/gdb-18.1-r2.ebuild @@ -0,0 +1,357 @@ +# Copyright 1999-2026 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +EAPI=8 + +# See https://sourceware.org/gdb/wiki/DistroAdvice for general packaging +# tips & notes. + +GUILE_COMPAT=( 2-2 3-0 ) +GUILE_REQ_USE="deprecated" +PYTHON_COMPAT=( python3_{11..14} ) +inherit dot-a flag-o-matic guile-single linux-info python-single-r1 strip-linguas toolchain-funcs + +export CTARGET=${CTARGET:-${CHOST}} + +if [[ ${CTARGET} == ${CHOST} ]] ; then + if [[ ${CATEGORY} == cross-* ]] ; then + export CTARGET=${CATEGORY#cross-} + fi +fi + +is_cross() { [[ ${CHOST} != ${CTARGET} ]] ; } + +case ${PV} in + 9999*) + # live git tree + EGIT_REPO_URI=" + https://sourceware.org/git/binutils-gdb.git + https://git.sr.ht/~sourceware/binutils-gdb + https://gitlab.com/x86-binutils/binutils-gdb.git + " + inherit git-r3 + SRC_URI="" + ;; + *.*.50_p2???????|*.*.90_p2???????) + # Weekly snapshots + MY_PV="${PV/_p/.}" + SRC_URI=" + https://sourceware.org/pub/gdb/snapshots/branch/gdb-weekly-${MY_PV}.tar.xz + https://sourceware.org/pub/gdb/snapshots/current/gdb-weekly-${MY_PV}.tar.xz + https://distfiles.gentoo.org/pub/proj/toolchain/gdb/snapshots/gdb-weekly-${MY_PV}.tar.xz + " + S="${WORKDIR}/${PN}-${MY_PV}" + + # e.g. 13.1.90_p20230325 is a snapshot on the stable branch, so it's fine + if [[ ${PV} == *.[123456789].9?_p2??????? ]] ; then + REGULAR_RELEASE=1 + fi + ;; + *.*.9?) + # Prereleases + MY_PV="${PV/_p/.}" + SRC_URI=" + https://sourceware.org/pub/gdb/snapshots/branch/gdb-${MY_PV}.tar.xz + https://distfiles.gentoo.org/pub/proj/toolchain/gdb/snapshots/gdb-${MY_PV}.tar.xz + " + S="${WORKDIR}/${PN}-${MY_PV}" + ;; + *) + # Normal upstream release + SRC_URI=" + mirror://gnu/gdb/${P}.tar.xz + https://sourceware.org/pub/gdb/releases/${P}.tar.xz + " + + REGULAR_RELEASE=1 +esac + +PATCH_DEV="" +PATCH_VER="" +DESCRIPTION="GNU debugger" +HOMEPAGE="https://sourceware.org/gdb/" +SRC_URI=" + ${SRC_URI} + ${PATCH_DEV:+https://distfiles.gentoo.org/pub/proj/toolchain/gdb/patches/${P}-patches-${PATCH_VER}.tar.xz} +" + +LICENSE="GPL-3+ LGPL-2.1+" +SLOT="0" +IUSE="cet +debuginfod guile lzma multitarget nls +python rocm +server sim source-highlight test vanilla +xml xxhash zstd" +if [[ -n ${REGULAR_RELEASE} ]] ; then + KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~loong ~m68k ~mips ~ppc ~ppc64 ~riscv ~s390 ~sparc ~x86 ~x64-macos ~x64-solaris" +fi +REQUIRED_USE=" + guile? ( ${GUILE_REQUIRED_USE} ) + python? ( ${PYTHON_REQUIRED_USE} ) + rocm? ( multitarget ) +" +RESTRICT="!test? ( test )" + +RDEPEND=" + dev-libs/mpfr:= + dev-libs/gmp:= + >=sys-libs/ncurses-5.2-r2:= + >=sys-libs/readline-7:= + virtual/zlib:= + debuginfod? ( + dev-libs/elfutils[debuginfod(-)] + ) + lzma? ( app-arch/xz-utils ) + python? ( ${PYTHON_DEPS} ) + guile? ( ${GUILE_DEPS} ) + xml? ( dev-libs/expat ) + rocm? ( >=dev-libs/rocdbgapi-6.3 ) + source-highlight? ( + dev-util/source-highlight + ) + xxhash? ( + dev-libs/xxhash + ) + zstd? ( app-arch/zstd:= ) +" +DEPEND="${RDEPEND}" +BDEPEND=" + app-arch/xz-utils + sys-apps/texinfo + app-alternatives/yacc + nls? ( sys-devel/gettext ) + source-highlight? ( virtual/pkgconfig ) + test? ( dev-util/dejagnu ) +" + +QA_CONFIG_IMPL_DECL_SKIP=( + MIN # gnulib FP (bug #898688) +) + +QA_PREBUILT="usr/share/gdb/guile/*" + +PATCHES=( + "${FILESDIR}"/${PN}-8.3.1-verbose-build.patch + "${FILESDIR}"/${PN}-18.1-rust-split_name.patch + "${FILESDIR}"/${PN}-18.1-avoid-crash-when-unwinding-through-c-frames.patch +) + +pkg_setup() { + local CONFIG_CHECK + + if [[ ${CHOST} == *-linux-* ]] ; then + if kernel_is -ge 6.11.3 ; then + # https://forums.gentoo.org/viewtopic-p-8846891.html + # See also PR31520. + # + # Either CONFIG_PROC_MEM_ALWAYS_FORCE or CONFIG_PROC_MEM_FORCE_PTRACE + # should be okay, but not CONFIG_PROC_MEM_NO_FORCE. + CONFIG_CHECK+=" + ~!PROC_MEM_NO_FORCE + " + fi + fi + + linux-info_pkg_setup + + use guile && guile-single_pkg_setup + use python && python-single-r1_pkg_setup +} + +src_prepare() { + default + + use guile && guile_bump_sources + + strip-linguas -u bfd/po opcodes/po + + # Avoid using ancient termcap from host on Prefix systems + sed -i -e 's/termcap tinfow/tinfow/g' \ + gdb/configure{.ac,} || die +} + +gdb_branding() { + printf "Gentoo ${PV} " + + if ! use vanilla && [[ -n ${PATCH_VER} ]] ; then + printf "p${PATCH_VER}" + else + printf "vanilla" + fi + + [[ -n ${EGIT_COMMIT} ]] && printf " ${EGIT_COMMIT}" +} + +src_configure() { + strip-unsupported-flags + use sim && use multitarget && lto-guarantee-fat + + # See https://www.gnu.org/software/make/manual/html_node/Parallel-Output.html + # Avoid really confusing logs from subconfigure spam, makes logs far + # more legible. + MAKEOPTS="--output-sync=line ${MAKEOPTS}" + + local myconf=( + # portage's econf() does not detect presence of --d-d-t + # because it greps only top-level ./configure. But not + # libiberty's or gdb's configure. + --disable-dependency-tracking + --disable-silent-rules + + --with-pkgversion="$(gdb_branding)" + --with-bugurl='https://bugs.gentoo.org/' + --disable-werror + # Disable modules that are in a combined binutils/gdb tree. bug #490566 + --disable-{binutils,etc,gas,gold,gprof,gprofng,ld} + + $(use_with debuginfod) + + $(use_enable test unit-tests) + + # Allow user to opt into CET for host libraries. + # Ideally we would like automagic-or-disabled here. + # But the check does not quite work on i686: bug #760926. + $(use_enable cet) + + # Helps when cross-compiling. Not to be confused with --with-sysroot. + --with-build-sysroot="${ESYSROOT}" + ) + + is_cross && myconf+=( + --with-sysroot="\${prefix}/${CTARGET}" + --includedir="\${prefix}/include/${CTARGET}" + --with-gdb-datadir="\${datadir}/gdb/${CTARGET}" + ) + + # gdbserver only works for native targets (CHOST==CTARGET). + # it also doesn't support all targets, so rather than duplicate + # the target list (which changes between versions), use the + # "auto" value when things are turned on, which is triggered + # whenever no --enable or --disable is given + if is_cross || use !server ; then + myconf+=( --disable-gdbserver ) + fi + + myconf+=( + --enable-64-bit-bfd + --disable-install-libbfd + --disable-install-libiberty + --enable-obsolete + # This only disables building in the readline subdir. + # For gdb itself, it'll use the system version. + --disable-readline + --with-system-readline + # This only disables building in the zlib subdir. + # For gdb itself, it'll use the system version. + --without-zlib + --with-system-zlib + --with-separate-debug-dir="${EPREFIX}"/usr/lib/debug + --with-amd-dbgapi=$(usex rocm) + $(use_with xml expat) + $(use_with lzma) + $(use_enable nls) + $(use_enable sim) + $(use_enable source-highlight) + $(use multitarget && echo --enable-targets=all) + $(use_with python python "${EPYTHON}") + $(use_with xxhash) + $(use_with guile) + $(use_with zstd) + + # Find libraries using the toolchain sysroot rather than the configured + # prefix. Needed when cross-compiling. + # + # Check which libraries to apply this to with: + # "${S}"/gdb/configure --help | grep without-lib | sort + --without-lib{expat,gmp,iconv,ipt,lzma,mpfr,xxhash}-prefix + ) + + # source-highlight is detected with pkg-config: bug #716558 + export ac_cv_path_pkg_config_prog_path="$(tc-getPKG_CONFIG)" + + export CC_FOR_BUILD="$(tc-getBUILD_CC)" + + # ensure proper compiler is detected for Clang builds: bug #831202 + export GCC_FOR_TARGET="${CC_FOR_TARGET:-$(tc-getCC)}" + + econf "${myconf[@]}" +} + +src_test() { + # Run the unittests (nabbed invocation from Fedora's spec file) at least + emake -k -C gdb run GDBFLAGS='-batch -ex "maintenance selftest"' + + # Too many failures + # In fact, gdb's test suite needs some work to get passing. + # See e.g. https://sourceware.org/gdb/wiki/TestingGDB. + # As of 11.2, on amd64: "# of unexpected failures 8600" + # Also, ia64 kernel crashes when gdb testsuite is running. + #emake -k check +} + +src_install() { + emake DESTDIR="${D}" install + + find "${ED}"/usr -name libiberty.a -delete || die + + use sim && use multitarget && strip-lto-bytecode + + # Delete translations that conflict with binutils-libs. bug #528088 + # Note: Should figure out how to store these in an internal gdb dir. + if use nls ; then + find "${ED}" \ + -regextype posix-extended -regex '.*/(bfd|opcodes)[.]g?mo$' \ + -delete || die + fi + + # Don't install docs when building a cross-gdb + if [[ ${CTARGET} != ${CHOST} ]] ; then + rm -rf "${ED}"/usr/share/{doc,info,locale} || die + local f + for f in "${ED}"/usr/share/man/*/* ; do + if [[ ${f##*/} != ${CTARGET}-* ]] ; then + mv "${f}" "${f%/*}/${CTARGET}-${f##*/}" || die + fi + done + return 0 + fi + + # Install it by hand for now: + # https://sourceware.org/ml/gdb-patches/2011-12/msg00915.html + # Only install if it exists due to the twisted behavior (see + # notes in src_configure above). + [[ -e gdbserver/gdbreplay ]] && dobin gdbserver/gdbreplay + + docinto gdb + dodoc gdb/CONTRIBUTE gdb/README gdb/MAINTAINERS \ + gdb/NEWS gdb/PROBLEMS + docinto sim + dodoc sim/{MAINTAINERS,README-HACKING} + + if use server ; then + docinto gdbserver + dodoc gdbserver/README + fi + + # Remove shared info pages + rm -f "${ED}"/usr/share/info/{annotate,bfd,configure,ctf-spec,standards}.info* + + use guile && guile_unstrip_ccache + + if use python ; then + python_optimize "${ED}"/usr/share/gdb/python/gdb + fi + + if use kernel_Hurd ; then + mv "${ED}"/usr/bin/gcore "${ED}"/usr/bin/gcore.gdb || die + fi +} + +pkg_postinst() { + # Portage doesn't unmerge files in /etc + rm -vf "${EROOT}"/etc/skel/.gdbinit + + if use prefix && [[ ${CHOST} == *-darwin* ]] ; then + ewarn "gdb is unable to get a mach task port when installed by Prefix" + ewarn "Portage, unprivileged. To make gdb fully functional you'll" + ewarn "have to perform the following steps:" + ewarn " % sudo chgrp procmod ${EPREFIX}/usr/bin/gdb" + ewarn " % sudo chmod g+s ${EPREFIX}/usr/bin/gdb" + fi +}