Mirror of GNU binutils
  • C 50.5%
  • Makefile 22.6%
  • Assembly 13.2%
  • C++ 6%
  • Roff 1.5%
  • Other 5.6%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Simon Marchi 6974e8c3bf 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=<optimized out>) at /home/simark/src/binutils-gdb/gdbsupport/new-op.cc:108
    #7  operator delete (p=<optimized out>) at /home/simark/src/binutils-gdb/gdbsupport/new-op.cc:114
    #8  operator delete (p=<optimized out>) at /home/simark/src/binutils-gdb/gdbsupport/new-op.cc:120
    #9  std::__new_allocator<char>::deallocate (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>, __p=<optimized out>, __n=<optimized out>) at /usr/include/c++/16/bits/new_allocator.h:183
    #10 std::allocator<char>::deallocate (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>, __p=<optimized out>, __n=<optimized out>) at /usr/include/c++/16/bits/allocator.h:218
    #11 std::allocator_traits<std::allocator<char> >::deallocate (__a=..., __p=<optimized out>, __n=<optimized out>) at /usr/include/c++/16/bits/alloc_traits.h:689
    #12 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_destroy (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>, __size=<optimized out>) at /usr/include/c++/16/bits/basic_string.h:304
    #13 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_dispose (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>) at /usr/include/c++/16/bits/basic_string.h:298
    #14 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>, this=<optimized out>) at /usr/include/c++/16/bits/basic_string.h:920
    #15 demangle_for_lookup_info::~demangle_for_lookup_info (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>, this=<optimized out>) at /home/simark/src/binutils-gdb/gdb/symtab.h:183
    #16 std::_Optional_payload_base<demangle_for_lookup_info>::_M_destroy (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>) at /usr/include/c++/16/optional:325
    #17 std::_Optional_payload_base<demangle_for_lookup_info>::_M_reset (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>) at /usr/include/c++/16/optional:361
    #18 std::_Optional_payload<demangle_for_lookup_info, false, false, false>::~_Optional_payload (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>, this=<optimized out>) at /usr/include/c++/16/optional:484
    #19 std::_Optional_base<demangle_for_lookup_info, false, false>::~_Optional_base (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>, this=<optimized out>) at /usr/include/c++/16/optional:508
    #20 std::optional<demangle_for_lookup_info>::~optional (this=0x555555caa4f8 <completion_list_add_fields(completion_tracker&, symbol*, lookup_name_info const&, char const*, char const*)+136>, this=<optimized out>) at /usr/include/c++/16/optional:812
    #21 lookup_name_info::~lookup_name_info (this=0x555555caa440 <gdb::function_view<void (char const*, macro_definition const*, macro_source_file*, int)>::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}>(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=<optimized out>) at /home/simark/src/binutils-gdb/gdb/symtab.h:210
    #22 0x000055555569ca89 in default_collect_symbol_completion_matches_break_on (tracker=..., mode=<optimized out>, name_match_type=<optimized out>, text=<optimized out>, word=<optimized out>, break_on=<optimized out>, code=<optimized out>) at /home/simark/src/binutils-gdb/gdb/symtab.c:6111
    #23 0x0000555555826c22 in language_defn::collect_symbol_completion_matches (this=<optimized out>, tracker=..., mode=<optimized out>, name_match_type=<optimized out>, text=<optimized out>, word=<optimized out>, code=<optimized out>, this=<optimized out>, tracker=..., mode=<optimized out>, name_match_type=<optimized out>, text=<optimized out>, word=<optimized out>, code=<optimized out>) 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=<optimized out>, word=<optimized out>) at /home/simark/src/binutils-gdb/gdb/symtab.c:6122
    #25 symbol_completer (ignore=<optimized out>, tracker=..., text=<optimized out>, word=<optimized out>) 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=<optimized out>, 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=<optimized out>, 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=<optimized out>, point=<optimized out>, reason=handle_completions) at /home/simark/src/binutils-gdb/gdb/completer.c:1862
    #29 complete_line (tracker=..., text=0x7fffffffe7e2 "", line_buffer=<optimized out>, point=<optimized out>) at /home/simark/src/binutils-gdb/gdb/completer.c:2157
    #30 complete_line (tracker=..., text=0x7fffffffe7e2 "", line_buffer=<optimized out>, point=<optimized out>) 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=<optimized out>) at /home/simark/src/binutils-gdb/gdb/completer.c:2126
    #32 0x00005555557e16db in complete_command (arg=0x7fffffffe7d5 "macro expand ", from_tty=<optimized out>) 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 <[email protected]>
2026-10-28 15:24:02 -04:00
archive [gdb] Move ChangeLog files to archive dir 2026-07-13 13:33:42 +02:00
bfd [RiscV] Alphabetize ISA X extensions 2026-10-28 08:35:50 -04:00
binutils RISC-V: add an "arch=" disassembler option 2026-10-27 21:57:48 +08:00
config configure: Sync zstd detection with GCC 2026-10-21 17:25:12 +10:30
contrib contrib: sync dg-extract-results.{sh,py} with GCC 2026-09-04 13:11:20 +01:00
cpu Re: bfd/ELF: fold BFD_RELOC_<arch>_GOTOFF* 2026-07-12 21:43:19 +09:30
elfcpp gas, bfd, gold: Rename Arm v8/v9 architecture tags 2026-05-28 11:33:39 +00:00
etc etc: import changes to texi2pod.pl from gcc 2026-08-24 21:33:23 +01:00
gas MIPS: Add the m6201 architecture 2026-10-28 18:15:30 +00:00
gdb gdb/macrotab: catch exception and rethrow around splay_tree_foreach 2026-10-28 15:24:02 -04:00
gdbserver gdb, gdbserver: Add Intel APX register support 2026-10-21 22:13:02 +02:00
gdbsupport gdb, gdbserver: Add Intel APX register support 2026-10-21 22:13:02 +02:00
gnulib gdb: remove O_NOCTTY fallback definitions 2026-10-22 12:56:36 -04:00
gold configure: Sync zstd detection with GCC 2026-10-21 17:25:12 +10:30
gprof config: Sync from GCC 2026-10-20 21:46:12 -04:00
gprofng config: Sync from GCC 2026-10-20 21:46:12 -04:00
include [RiscV] Alphabetize ISA X extensions 2026-10-28 08:35:50 -04:00
ld configure: Sync zstd detection with GCC 2026-10-21 17:25:12 +10:30
libbacktrace config: Sync from GCC 2026-10-20 21:46:12 -04:00
libctf config: Sync from GCC 2026-10-20 21:46:12 -04:00
libdecnumber regen config 2023-08-12 10:27:57 +09:30
libiberty libiberty d_compact_number signed integer overflow 2026-07-01 10:57:51 +09:30
libsframe libsframe testsuite format mismatches on 32-bit host 2026-04-06 10:46:06 +09:30
opcodes RISC-V: add an "arch=" disassembler option 2026-10-27 21:57:48 +08:00
readline readline/tcap.h: Update definitions for C23 2025-05-02 12:00:05 -06:00
sim sim/bpf: remove unused bpf_program_section variable 2026-10-21 09:29:04 -04:00
texinfo
zlib config: Sync from GCC 2026-10-20 21:46:12 -04:00
.clang-format Remove ARI 2026-06-26 09:17:29 -06:00
.cvsignore add autom4te.cache to .cvsignore 2007-02-13 15:25:58 +00:00
.editorconfig toplevel: unify the GCC and GDB/binutils .editorconfig files 2025-10-01 16:20:29 +01:00
.gitattributes binutils-gdb/git: Handle *.ac and *.m4 files in .gitattributes 2025-12-18 10:00:54 +01:00
.gitignore Add .clang-format 2026-02-11 07:53:01 -07:00
.pre-commit-config.yaml pre-commit: Update black: 26.10.0 -> 26.10.1 2026-10-12 08:11:38 +02:00
ar-lib Bump to autoconf 2.69 and automake 1.15.1 2018-06-19 16:55:06 -04:00
ChangeLog Import the following commits from the master config sources: a2287c3041a3f2a204eb942e09c015eab00dc7dd 973e3e6af23b73a4f1b8d95680454cae22593bf8 3a71dc102953608d4592ec401b519837c28a672a f91a544533876c70f43b9fd51064b2bcf3fa7382 484648c73f3843b256dd011bd415e81594300a0a 7f4149527babe92cb5da1032734f5cb90cefdac5 9f6e0fe8ce04628bbd4a455118ff3f8309a1aef9 2026-01-08 11:20:38 +00:00
compile Update from upstream Automake 2014-11-16 13:43:48 +01:00
config-ml.in MSP430: Add -fno-exceptions multilib 2023-08-12 10:24:26 +09:30
config.guess Import the following commits from the master config sources: a2287c3041a3f2a204eb942e09c015eab00dc7dd 973e3e6af23b73a4f1b8d95680454cae22593bf8 3a71dc102953608d4592ec401b519837c28a672a f91a544533876c70f43b9fd51064b2bcf3fa7382 484648c73f3843b256dd011bd415e81594300a0a 7f4149527babe92cb5da1032734f5cb90cefdac5 9f6e0fe8ce04628bbd4a455118ff3f8309a1aef9 2026-01-08 11:20:38 +00:00
config.rpath Remove freebsd1 from libtool.m4 macros and config.rpath. 2011-02-13 21:00:14 +00:00
config.sub Import the following commits from the master config sources: a2287c3041a3f2a204eb942e09c015eab00dc7dd 973e3e6af23b73a4f1b8d95680454cae22593bf8 3a71dc102953608d4592ec401b519837c28a672a f91a544533876c70f43b9fd51064b2bcf3fa7382 484648c73f3843b256dd011bd415e81594300a0a 7f4149527babe92cb5da1032734f5cb90cefdac5 9f6e0fe8ce04628bbd4a455118ff3f8309a1aef9 2026-01-08 11:20:38 +00:00
configure config: Sync from GCC 2026-10-20 21:46:12 -04:00
configure.ac AMDGCN: Disable subdirectory configuration for unsupported GAS and LD 2026-02-18 17:32:32 +00:00
COPYING 2005-07-14 Kelley Cook <[email protected]> 2005-07-14 01:24:56 +00:00
COPYING.LIB 2005-07-16 Kelley Cook <[email protected]> 2005-07-16 02:41:34 +00:00
COPYING.LIBGLOSS Update the address of the FSF in the copyright notice of files which were using the old address. 2017-12-14 12:48:55 +00:00
COPYING.NEWLIB 2013-10-01 Jeff Johnston <[email protected]> 2013-10-01 18:14:04 +00:00
COPYING3 * COPYING3: New file. Contains version 3 of the GNU General Public License. 2007-07-17 13:50:23 +00:00
COPYING3.LIB * COPYING3: New file. Contains version 3 of the GNU General Public License. 2007-07-17 13:50:23 +00:00
depcomp Update from upstream Automake 2014-11-16 13:43:48 +01:00
djunpack.bat * djunpack.bat: Use ".." quoting in Sed command, for the sake of 2009-03-27 13:37:09 +00:00
install-sh Update from upstream Automake 2014-11-16 13:43:48 +01:00
libtool.m4 Binutils/GCC: Add clang LTO support to AR, NM and RANLIB 2025-09-25 12:30:13 +08:00
ltgcc.m4 * libtool.m4: Update to libtool 2.2.6. 2008-09-29 15:28:14 +00:00
ltmain.sh ltmain.sh: allow more flags at link-time 2024-09-25 19:06:10 +01:00
ltoptions.m4 Sync Libtool from GCC. 2010-01-09 21:11:44 +00:00
ltsugar.m4 * libtool.m4: Update to libtool 2.2.6. 2008-09-29 15:28:14 +00:00
ltversion.m4 Sync Libtool from GCC. 2010-01-09 21:11:44 +00:00
lt~obsolete.m4 Sync Libtool from GCC. 2010-01-09 21:11:44 +00:00
MAINTAINERS Fix compiling bfd/vms-lib.c for a 32-bit host. 2024-03-18 10:26:16 +00:00
Makefile.def Sync top-level with GCC 2025-11-03 09:53:04 +00:00
Makefile.in Sync toplevel files from gcc 2025-10-02 07:42:18 +08:00
Makefile.tpl Sync toplevel files from gcc 2025-10-02 07:42:18 +08:00
makefile.vms 19990502 sourceware import 1999-05-03 07:29:11 +00:00
missing Update from upstream Automake 2014-11-16 13:43:48 +01:00
mkdep * mkdep: New file. 1999-08-08 17:46:02 +00:00
mkinstalldirs Update from upstream Automake 2014-11-16 13:43:48 +01:00
move-if-change Update `move-if-change' from gnulib 2014-11-16 17:04:02 +01:00
multilib.am Merge autoconf / automake update changes from GCC. 2018-10-31 17:10:56 +00:00
README README: don't talk about libg++ 2025-09-07 04:06:01 +01:00
README-maintainer-mode Note that at least dejagnu version 1.5.3 is required in order to be ale to run the testsuites. 2022-10-04 10:54:19 +01:00
SECURITY.txt Add a SECURITY.txt file describing the GNU Binutils' project's stance on security related bugs. 2023-04-20 16:52:11 +01:00
setup.com 2009-09-01 Tristan Gingold <[email protected]> 2009-09-01 13:38:26 +00:00
src-release.sh Rename 'binutils' to 'binutils_with_gold'. Rename 'bin_no_gold' to 'binutils'. Add 'gold' 2025-02-04 11:50:02 +00:00
symlink-tree 2005-07-14 Kelley Cook <[email protected]> 2005-07-14 01:24:56 +00:00
test-driver Bump to autoconf 2.69 and automake 1.15.1 2018-06-19 16:55:06 -04:00
ylwrap Update from upstream Automake 2014-11-16 13:43:48 +01:00

		   README for GNU development tools

This directory contains various GNU compilers, assemblers, linkers, 
debuggers, etc., plus their support routines, definitions, and documentation.

If you are receiving this as part of a GDB release, see the file gdb/README.
If with a binutils release, see binutils/README, and so on. That'll give you
info about this package -- supported targets, how to use it, how to report
bugs, etc.

It is now possible to automatically configure and build a variety of
tools with one command.  To build all of the tools contained herein,
run the ``configure'' script here, e.g.:

	./configure 
	make

To install them (by default in /usr/local/bin, /usr/local/lib, etc),
then do:
	make install

(If the configure script can't determine your type of computer, give it
the name as an argument, for instance ``./configure sun4''.  You can
use the script ``config.sub'' to test whether a name is recognized; if
it is, config.sub translates it to a triplet specifying CPU, vendor,
and OS.)

If you have more than one compiler on your system, it is often best to
explicitly set CC in the environment before running configure, and to
also set CC when running make.  For example (assuming sh/bash/ksh):

	CC=gcc ./configure
	make

A similar example using csh:

	setenv CC gcc
	./configure
	make

Much of the code and documentation enclosed is copyright by
the Free Software Foundation, Inc.  See the file COPYING or
COPYING.LIB in the various directories, for a description of the
GNU General Public License terms under which you can copy the files.

REPORTING BUGS: Again, see gdb/README, binutils/README, etc., for info
on where and how to report problems.