Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH v3] gdb, configure: Add disable-formats option for configure
@ 2025-03-07 11:58 Guinevere Larsen
  2025-03-07 12:09 ` Eli Zaretskii
                   ` (4 more replies)
  0 siblings, 5 replies; 25+ messages in thread
From: Guinevere Larsen @ 2025-03-07 11:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen

GDB has support for many binary file formats, some which might be very
unlikely to be found in some situations (such as the COFF format in
linux). This commit introduces the option for a user to choose which
formats GDB will support at build configuration time.

This is especially useful to avoid possible security concerns with
readers that aren't expected to be used at all, as they are one of
the simplest vectors for an attacker to try and hit GDB with.  This
change can also reduce the size of the final binary, if that is a
concern.

This commit adds a switch to the configure script allowing a user to
only enable selected file formats, called --enable-binary-file-formats.
The default behavior when the switch is omitted is to compile all file
formats, keeping the original behavior of the script. At the time of
this commit, the valid options for this option are: dbx, coff (which
includes coff-pe), xcoff, mips, elf, macho and all. All is treated
especially, activating all supported readers.

A few targets may require specific binary file format support, as they
directly call functions defined by the file reader. Specifically,
windows targets require coff support, and rs6000 aix and lynx178 targets
require xcoff support. Considering that those formats are the main - or
only - one available in those targets, I believe it makes sense to
re-enable those readers. If that happens, the script will emit the
following warning:

  FOO is required to support one or more requested targets. Adding it

Users aren't able to force the disabling of those formats, since GDB
will not compile without those readers. Ideally we'd like to be able
to disable even those formats, in case a user wants to build GDB only
to examine remote files for example, but the current infrastructure
for the file format readers doesn't allow us to do it.

Mach-O and elf support are also dependent on BFD support being compiled
in.  In case one of those was requested and BFD does not support them,
the following error is emitted:

    FOO was requested, but BFD does not support it.

Finally, this configure switch is also printed by the "show
configuration" command in GDB.
---
 gdb/Makefile.in      | 13 +++----
 gdb/NEWS             | 11 ++++++
 gdb/README           | 24 +++++++++++++
 gdb/config.in        |  3 ++
 gdb/configure        | 81 +++++++++++++++++++++++++++++++++++++++++---
 gdb/configure.ac     | 65 +++++++++++++++++++++++++++++++++--
 gdb/configure.format | 51 ++++++++++++++++++++++++++++
 gdb/configure.tgt    | 20 +++++++++--
 gdb/doc/gdb.texinfo  |  8 +++++
 gdb/top.c            |  5 +++
 10 files changed, 264 insertions(+), 17 deletions(-)
 create mode 100644 gdb/configure.format

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index a24c86fcff8..778ed54090f 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -904,13 +904,15 @@ ALL_TARGET_OBS = \
 	vax-tdep.o \
 	windows-tdep.o \
 	x86-tdep.o \
-	xcoffread.o \
 	xstormy16-tdep.o \
 	xtensa-config.o \
 	xtensa-linux-tdep.o \
 	xtensa-tdep.o \
 	z80-tdep.o
 
+# Object files for reading specific types of debug information.
+FORMAT_OBS = @FORMAT_OBS@
+
 # The following native-target dependent variables are defined on
 # configure.nat.
 NAT_FILE = @NAT_FILE@
@@ -1067,8 +1069,6 @@ COMMON_SFILES = \
 	c-varobj.c \
 	charset.c \
 	cli-out.c \
-	coff-pe-read.c \
-	coffread.c \
 	complaints.c \
 	completer.c \
 	copying.c \
@@ -1082,7 +1082,6 @@ COMMON_SFILES = \
 	d-lang.c \
 	d-namespace.c \
 	d-valprint.c \
-	dbxread.c \
 	dcache.c \
 	debug.c \
 	debuginfod-support.c \
@@ -1174,7 +1173,6 @@ COMMON_SFILES = \
 	memtag.c \
 	minidebug.c \
 	minsyms.c \
-	mipsread.c \
 	namespace.c \
 	objc-lang.c \
 	objfiles.c \
@@ -1267,7 +1265,6 @@ SFILES = \
 	d-exp.y \
 	dtrace-probe.c \
 	elf-none-tdep.c \
-	elfread.c \
 	f-exp.y \
 	gcore-elf.c \
 	gdb.c \
@@ -1887,7 +1884,6 @@ ALLDEPFILES = \
 	x86-gnu-nat.c \
 	x86-nat.c \
 	x86-tdep.c \
-	xcoffread.c \
 	xstormy16-tdep.c \
 	xtensa-config.c \
 	xtensa-linux-nat.c \
@@ -1909,7 +1905,8 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	$(SUBDIR_CLI_OBS) \
 	$(SUBDIR_MI_OBS) \
 	$(SUBDIR_TARGET_OBS) \
-	$(SUBDIR_GCC_COMPILE_OBS)
+	$(SUBDIR_GCC_COMPILE_OBS) \
+	$(FORMAT_OBS)
 
 SUBDIRS = doc @subdirs@ data-directory
 CLEANDIRS = $(SUBDIRS)
diff --git a/gdb/NEWS b/gdb/NEWS
index f5dbf5c3350..2a916d356ef 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -85,6 +85,17 @@ qXfer:threads:read
 * Support for stabs debugging format and the a.out/dbx object format is
   deprecated, and will be removed in GDB 18.
 
+* Configure changes
+
+enable-binary-file-formats=[FORMAT,...]
+enable-binary-file-formats=all
+  A user can now decide to only compile support for certain file formats.
+  The available formats at this point are: dbx, coff, xcoff, elf, mach-o
+  and mips.  Some targets require specific file formats to be available,
+  and in such cases, the configure script will warn the user and add
+  support anyway.  By default, all formats will be compiled in, to
+  continue the behavior from before adding the switch.
+
 *** Changes in GDB 16
 
 * Support for Nios II targets has been removed as this architecture
diff --git a/gdb/README b/gdb/README
index b6eb3f12c56..0168afe7cd1 100644
--- a/gdb/README
+++ b/gdb/README
@@ -417,6 +417,30 @@ more obscure GDB `configure' options are not listed here.
      There is no convenient way to generate a list of all available
      targets.
 
+`--enable-binary-file-formats=FORMAT,FORMAT,...'
+`--enable-binary-file-formats=all'
+    Configure GDB to only be be able to read selected file formats.
+    The special value "all" causes all file formats to be compiled
+    in, and is the the default behavior of the option.  This option
+    is meant for advanced users who are sure of what they expect,
+    if you are unsure which options you will need on your debugging
+    sessions, we recommend that you not use this feature.  The
+    accepted options are:
+      * coff: Main format on windows systems. This is required to
+        compile with windows target support;
+      * dbx (also known as a.out): is a legacy file format, this
+        is recommended if you know you will be dealing with this
+        file format;
+      * elf: Main format of linux systems. This is heavily
+        recommended when compiling with linux support;
+      * macho: Main format on MacOS systems, this is heavily
+        recommended when compiling for those targets;
+      * mips (also known as ecoff): this is the main file format
+        for targets running on MIPS CPUs. It is heavily recommended
+        when supporting those targets.
+      * xcoff: Main format of AIX systems, this is required to
+        compile for AIX targets and rs6000 CPUs.
+
 `--with-gdb-datadir=PATH'
      Set the GDB-specific data directory.  GDB will look here for
      certain supporting files or scripts.  This defaults to the `gdb'
diff --git a/gdb/config.in b/gdb/config.in
index db63aeaec75..248ebaa68af 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -733,6 +733,9 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+/* Which file formats were requested at configure time. */
+#undef SUPPORTED_FORMATS
+
 /* automatically load a system-wide gdbinit file */
 #undef SYSTEM_GDBINIT
 
diff --git a/gdb/configure b/gdb/configure
index bdc405e01b3..8229ccaa588 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -706,6 +706,7 @@ LIBGUI
 LTLIBLZMA
 LIBLZMA
 HAVE_LIBLZMA
+FORMAT_OBS
 SER_HARDWIRE
 WERROR_CFLAGS
 WARN_CFLAGS
@@ -934,6 +935,7 @@ with_relocated_sources
 with_auto_load_dir
 with_auto_load_safe_path
 enable_targets
+enable_binary_file_formats
 enable_64_bit_bfd
 with_amd_dbgapi
 enable_tui
@@ -1645,6 +1647,9 @@ Optional Features:
   --disable-nls           do not use Native Language Support
   --enable-targets=TARGETS
                           alternative target configurations
+  --enable-binary-file-formats=FILE_FORMATS
+                          enable support for selected file formats (default
+                          'all')
   --enable-64-bit-bfd     64-bit support (on hosts with narrower word sizes)
   --enable-tui            enable full-screen terminal user interface (TUI)
   --enable-gdbtk          enable gdbtk graphical user interface (GUI)
@@ -11500,7 +11505,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11503 "configure"
+#line 11508 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11606,7 +11611,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11609 "configure"
+#line 11614 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -24875,6 +24880,18 @@ esac
 fi
 
 
+# Check whether --enable-binary_file_formats was given.
+if test "${enable_binary_file_formats+set}" = set; then :
+  enableval=$enable_binary_file_formats; case "${enableval}" in
+ yes | "") as_fn_error $? "enable-formats option must specify file formats or 'all'" "$LINENO" 5
+            ;;
+  no)       enable_binary_file_formats= ;;
+  *)        enable_binary_file_formats=$enableval ;;
+esac
+else
+  enable_binary_file_formats=all
+fi
+
 
 # Check whether --enable-64-bit-bfd was given.
 if test "${enable_64_bit_bfd+set}" = set; then :
@@ -24958,11 +24975,20 @@ fi
 TARGET_OBS=
 all_targets=
 HAVE_NATIVE_GCORE_TARGET=
+# File formats that will be enabled based on the selected
+# target(s).  These are chosen because they are required to
+# compile one or more of the selected targets.
+target_formats=
+# If all targets were requested, this is all formats that should
+# accompany them.  These are just the ones required for compilation
+# to succeed, not the formats suggested based on targets.
+all_target_formats="coff xcoff"
 
 for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
 do
   if test "$targ_alias" = "all"; then
     all_targets=true
+    target_formats=$all_target_formats
   else
     # Canonicalize the secondary target names.
     result=`$ac_config_sub $targ_alias 2>/dev/null`
@@ -31553,6 +31579,10 @@ fi
 # Note that WIN32APILIBS is set by GDB_AC_COMMON.
 WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
 
+# Object files to be used when building with support for all file formats.
+all_binary_format_obs="dbxread.o mipsread.o coffread.o coff-pe-read.o xcoffread.o"
+
+support_elf=no
 # Add ELF support to GDB, but only if BFD includes ELF support.
 
   OLD_CFLAGS=$CFLAGS
@@ -31605,7 +31635,7 @@ $as_echo "$gdb_cv_var_elf" >&6; }
   LDFLAGS=$OLD_LDFLAGS
   LIBS=$OLD_LIBS
 if test "$gdb_cv_var_elf" = yes; then
-  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
+  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
 		gcore-elf.o elf-none-tdep.o"
 
 $as_echo "#define HAVE_ELF 1" >>confdefs.h
@@ -31669,9 +31699,12 @@ if test "$ac_res" != no; then :
 fi
 
   fi
+  support_elf=yes
+  all_binary_format_obs="$all_binary_format_obs elfread.o"
 fi
 
 # Add macho support to GDB, but only if BFD includes it.
+support_macho=no
 
   OLD_CFLAGS=$CFLAGS
   OLD_LDFLAGS=$LDFLAGS
@@ -31723,9 +31756,49 @@ $as_echo "$gdb_cv_var_macho" >&6; }
   LDFLAGS=$OLD_LDFLAGS
   LIBS=$OLD_LIBS
 if test "$gdb_cv_var_macho" = yes; then
-  CONFIG_OBS="$CONFIG_OBS machoread.o"
+  support_macho=yes
+  all_binary_format_obs="$all_binary_format_obs machoread.o"
+fi
+
+FORMAT_OBS=
+
+if test "$enable_binary_file_formats" != "all"; then
+    # Formats that are required by some requested target(s).
+    # Warn users that they are added, so no one is surprised.
+    for req in $target_formats; do
+	if ! echo "$enable_binary_file_formats" | grep -wq "$req"; then
+	    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&5
+$as_echo "$as_me: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&2;}
+	    enable_binary_file_formats="${enable_binary_file_formats},$req"
+	fi
+    done
+
+
+cat >>confdefs.h <<_ACEOF
+#define SUPPORTED_FORMATS "$enable_binary_file_formats"
+_ACEOF
+
 fi
 
+enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
+
+for format in $enable_binary_file_formats
+do
+    if test "$format" = "elf"; then
+	if test "$support_elf" != "yes"; then
+	    as_fn_error but BFD does not support it." "\"elf support was requested" "$LINENO" 5;
+	fi
+    elif test "$format" = "macho"; then
+	if test "$support_macho" != "yes"; then
+	    as_fn_error but BFD does not support it." "\"Mach-O support was requested" "$LINENO" 5;
+	fi
+    fi
+
+    . ${srcdir}/configure.format
+done
+
+
+
 # Add any host-specific objects to GDB.
 CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
 
diff --git a/gdb/configure.ac b/gdb/configure.ac
index fb77e79e73b..c5e774962ce 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -191,6 +191,15 @@ AS_HELP_STRING([--enable-targets=TARGETS], [alternative target configurations]),
 	;;
 esac])
 
+AC_ARG_ENABLE(binary_file_formats,
+	      AS_HELP_STRING([--enable-binary-file-formats=FILE_FORMATS],
+			     [enable support for selected file formats (default 'all')]),
+[case "${enableval}" in
+ yes | "") AC_MSG_ERROR(enable-formats option must specify file formats or 'all')
+            ;;
+  no)       enable_binary_file_formats= ;;
+  *)        enable_binary_file_formats=$enableval ;;
+esac], [enable_binary_file_formats=all])
 
 BFD_64_BIT
 
@@ -211,11 +220,20 @@ fi
 TARGET_OBS=
 all_targets=
 HAVE_NATIVE_GCORE_TARGET=
+# File formats that will be enabled based on the selected
+# target(s).  These are chosen because they are required to
+# compile one or more of the selected targets.
+target_formats=
+# If all targets were requested, this is all formats that should
+# accompany them.  These are just the ones required for compilation
+# to succeed, not the formats suggested based on targets.
+all_target_formats="coff xcoff"
 
 for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
 do
   if test "$targ_alias" = "all"; then
     all_targets=true
+    target_formats=$all_target_formats
   else
     # Canonicalize the secondary target names.
     result=`$ac_config_sub $targ_alias 2>/dev/null`
@@ -1903,11 +1921,15 @@ fi
 # Note that WIN32APILIBS is set by GDB_AC_COMMON.
 WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
 
+# Object files to be used when building with support for all file formats.
+all_binary_format_obs="dbxread.o mipsread.o coffread.o coff-pe-read.o xcoffread.o"
+
+support_elf=no
 # Add ELF support to GDB, but only if BFD includes ELF support.
 GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_elf,
                  [bfd_get_elf_phdr_upper_bound (NULL)], elf-bfd.h)
 if test "$gdb_cv_var_elf" = yes; then
-  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
+  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
 		gcore-elf.o elf-none-tdep.o"
   AC_DEFINE(HAVE_ELF, 1,
 	    [Define if ELF support should be included.])
@@ -1915,15 +1937,54 @@ if test "$gdb_cv_var_elf" = yes; then
   if test "$plugins" = "yes"; then
     AC_SEARCH_LIBS(dlopen, dl)
   fi
+  support_elf=yes
+  all_binary_format_obs="$all_binary_format_obs elfread.o"
 fi
 
 # Add macho support to GDB, but only if BFD includes it.
+support_macho=no
 GDB_AC_CHECK_BFD([for Mach-O support in BFD], gdb_cv_var_macho,
                  [bfd_mach_o_lookup_command (NULL, 0, NULL)], mach-o.h)
 if test "$gdb_cv_var_macho" = yes; then
-  CONFIG_OBS="$CONFIG_OBS machoread.o"
+  support_macho=yes
+  all_binary_format_obs="$all_binary_format_obs machoread.o"
+fi
+
+FORMAT_OBS=
+
+if test "$enable_binary_file_formats" != "all"; then
+    # Formats that are required by some requested target(s).
+    # Warn users that they are added, so no one is surprised.
+    for req in $target_formats; do
+	if ! echo "$enable_binary_file_formats" | grep -wq "$req"; then
+	    AC_MSG_WARN("$req is required to support one or more requested targets.  Adding it")
+	    enable_binary_file_formats="${enable_binary_file_formats},$req"
+	fi
+    done
+
+    AC_DEFINE_UNQUOTED(SUPPORTED_FORMATS, "$enable_binary_file_formats",
+		       Which file formats were requested at configure time. )
 fi
 
+enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
+
+for format in $enable_binary_file_formats
+do
+    if test "$format" = "elf"; then
+	if test "$support_elf" != "yes"; then
+	    AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
+	fi
+    elif test "$format" = "macho"; then
+	if test "$support_macho" != "yes"; then
+	    AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
+	fi
+    fi
+
+    . ${srcdir}/configure.format
+done
+
+AC_SUBST(FORMAT_OBS)
+
 # Add any host-specific objects to GDB.
 CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
 
diff --git a/gdb/configure.format b/gdb/configure.format
new file mode 100644
index 00000000000..5ac86359037
--- /dev/null
+++ b/gdb/configure.format
@@ -0,0 +1,51 @@
+# Copyright (C) 2024-2025 Free Software Foundation, Inc.
+#
+# This file is part of GDB.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# This file is used to decide which files need to be compiled to support
+# the requested file formats
+
+case $format in
+    xcoff)
+	FORMAT_OBS="$FORMAT_OBS xcoffread.o"
+	;;
+
+    # Despite the naming convention implying coff-pe to be a separate
+    # reader, it is in fact just a helper for coffread;
+    coff)
+	FORMAT_OBS="$FORMAT_OBS coffread.o coff-pe-read.o"
+	;;
+
+    dbx)
+	FORMAT_OBS="$FORMAT_OBS dbxread.o"
+	;;
+
+    elf)
+	FORMAT_OBS="$FORMAT_OBS elfread.o"
+	;;
+
+    macho)
+	FORMAT_OBS="$FORMAT_OBS machoread.o"
+	;;
+
+    mips)
+	FORMAT_OBS="$FORMAT_OBS mipsread.o"
+	;;
+
+    all)
+	FORMAT_OBS="$all_binary_format_obs"
+	;;
+esac
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index 18a15c032c3..064deddc97b 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -496,7 +496,7 @@ powerpc-*-openbsd*)
 	;;
 powerpc-*-aix* | rs6000-*-* | powerpc64-*-aix*)
 	# Target: PowerPC running AIX
-	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o xcoffread.o \
+	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o \
 			ppc-sysv-tdep.o solib-aix.o \
 			ravenscar-thread.o ppc-ravenscar-thread.o"
 	;;
@@ -512,8 +512,8 @@ powerpc*-*-linux*)
 powerpc-*-lynx*178)
 	# Target: PowerPC running Lynx178.
 	gdb_target_obs="rs6000-tdep.o rs6000-lynx178-tdep.o \
-			xcoffread.o ppc-sysv-tdep.o \
-			ravenscar-thread.o ppc-ravenscar-thread.o"
+			ppc-sysv-tdep.o ravenscar-thread.o \
+			ppc-ravenscar-thread.o"
 	;;
 powerpc*-*-*)
 	# Target: PowerPC running eabi
@@ -817,3 +817,17 @@ for t in x ${gdb_target_obs}; do
     gdb_have_gcore=true
   fi
 done
+
+# Decide which file formats are absolutely required based on
+# the requested targets.  Warn later that they are added, in
+# case the user didn't manually request them, or all readers.
+# It's fine to add the same format multiple times since the
+# loop that reads the options to FORMAT_OBS will ensure that
+# they are only added once.
+for i in $gdb_target_obs; do
+    case "${i}" in
+    *"windows-tdep.o" ) target_formats="${target_formats} coff";;
+    "rs6000-aix-tdep.o" ) target_formats="${target_formats} xcoff";;
+    "rs6000-lynx178-tdep.o" ) target_formats="${target_formats} xcoff";;
+    esac
+done
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 95a881e9dff..26434183769 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -41258,6 +41258,14 @@ Configure @value{GDBN} for cross-debugging programs running on the
 specified list of targets.  The special value @samp{all} configures
 @value{GDBN} for debugging programs running on any target it supports.
 
+@item --enable-binary-file-formats=@r{[}@var{format}@r{]}@dots{}
+@itemx --enable-binary-file-formats=all
+Configure @value{GDBN} to support certain binary file formats.  If a
+format is the main (or only) file format for a given target, the
+configure script may add support to it anyway, and warn the user.
+If not given, all file formats that @value{GDBN} supports are compiled
+in.
+
 @item --with-gdb-datadir=@var{path}
 Set the @value{GDBN}-specific data directory.  @value{GDBN} will look
 here for certain supporting files or scripts.  This defaults to the
diff --git a/gdb/top.c b/gdb/top.c
index b2575bc2b58..9342085b3a9 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1595,6 +1595,11 @@ This GDB was configured as follows:\n\
 	     --with-system-gdbinit-dir=%s%s\n\
 "), SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE ? " (relocatable)" : "");
 
+#ifdef SUPPORTED_FORMATS
+  gdb_printf (stream, _("\
+	     --enable-binary-file-formats=%s\n"), SUPPORTED_FORMATS);
+#endif
+
   /* We assume "relocatable" will be printed at least once, thus we always
      print this text.  It's a reasonably safe assumption for now.  */
   gdb_printf (stream, _("\n\
-- 
2.48.1


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v3] gdb, configure: Add disable-formats option for configure
  2025-03-07 11:58 [PATCH v3] gdb, configure: Add disable-formats option for configure Guinevere Larsen
@ 2025-03-07 12:09 ` Eli Zaretskii
  2025-03-07 12:49   ` Guinevere Larsen
  2025-03-27 20:21 ` [PING][PATCH " Guinevere Larsen
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 25+ messages in thread
From: Eli Zaretskii @ 2025-03-07 12:09 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: gdb-patches

> From: Guinevere Larsen <guinevere@redhat.com>
> Cc: Guinevere Larsen <guinevere@redhat.com>
> Date: Fri,  7 Mar 2025 08:58:21 -0300
> 
> GDB has support for many binary file formats, some which might be very
> unlikely to be found in some situations (such as the COFF format in
> linux). This commit introduces the option for a user to choose which
> formats GDB will support at build configuration time.
> 
> This is especially useful to avoid possible security concerns with
> readers that aren't expected to be used at all, as they are one of
> the simplest vectors for an attacker to try and hit GDB with.  This
> change can also reduce the size of the final binary, if that is a
> concern.
> 
> This commit adds a switch to the configure script allowing a user to
> only enable selected file formats, called --enable-binary-file-formats.
> The default behavior when the switch is omitted is to compile all file
> formats, keeping the original behavior of the script. At the time of
> this commit, the valid options for this option are: dbx, coff (which
> includes coff-pe), xcoff, mips, elf, macho and all. All is treated
> especially, activating all supported readers.
> 
> A few targets may require specific binary file format support, as they
> directly call functions defined by the file reader. Specifically,
> windows targets require coff support, and rs6000 aix and lynx178 targets
> require xcoff support. Considering that those formats are the main - or
> only - one available in those targets, I believe it makes sense to
> re-enable those readers. If that happens, the script will emit the
> following warning:
> 
>   FOO is required to support one or more requested targets. Adding it
> 
> Users aren't able to force the disabling of those formats, since GDB
> will not compile without those readers. Ideally we'd like to be able
> to disable even those formats, in case a user wants to build GDB only
> to examine remote files for example, but the current infrastructure
> for the file format readers doesn't allow us to do it.
> 
> Mach-O and elf support are also dependent on BFD support being compiled
> in.  In case one of those was requested and BFD does not support them,
> the following error is emitted:
> 
>     FOO was requested, but BFD does not support it.
> 
> Finally, this configure switch is also printed by the "show
> configuration" command in GDB.
> ---
>  gdb/Makefile.in      | 13 +++----
>  gdb/NEWS             | 11 ++++++
>  gdb/README           | 24 +++++++++++++
>  gdb/config.in        |  3 ++
>  gdb/configure        | 81 +++++++++++++++++++++++++++++++++++++++++---
>  gdb/configure.ac     | 65 +++++++++++++++++++++++++++++++++--
>  gdb/configure.format | 51 ++++++++++++++++++++++++++++
>  gdb/configure.tgt    | 20 +++++++++--
>  gdb/doc/gdb.texinfo  |  8 +++++
>  gdb/top.c            |  5 +++
>  10 files changed, 264 insertions(+), 17 deletions(-)
>  create mode 100644 gdb/configure.format

Thanks, but can you describe the changes sine v2, so I could determine
whether I need to review the documentation parts of this?  TIA.

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v3] gdb, configure: Add disable-formats option for configure
  2025-03-07 12:09 ` Eli Zaretskii
@ 2025-03-07 12:49   ` Guinevere Larsen
  2025-03-07 13:03     ` Eli Zaretskii
  0 siblings, 1 reply; 25+ messages in thread
From: Guinevere Larsen @ 2025-03-07 12:49 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

On 3/7/25 9:09 AM, Eli Zaretskii wrote:
>> From: Guinevere Larsen <guinevere@redhat.com>
>> Cc: Guinevere Larsen <guinevere@redhat.com>
>> Date: Fri,  7 Mar 2025 08:58:21 -0300
>>
>> GDB has support for many binary file formats, some which might be very
>> unlikely to be found in some situations (such as the COFF format in
>> linux). This commit introduces the option for a user to choose which
>> formats GDB will support at build configuration time.
>>
>> This is especially useful to avoid possible security concerns with
>> readers that aren't expected to be used at all, as they are one of
>> the simplest vectors for an attacker to try and hit GDB with.  This
>> change can also reduce the size of the final binary, if that is a
>> concern.
>>
>> This commit adds a switch to the configure script allowing a user to
>> only enable selected file formats, called --enable-binary-file-formats.
>> The default behavior when the switch is omitted is to compile all file
>> formats, keeping the original behavior of the script. At the time of
>> this commit, the valid options for this option are: dbx, coff (which
>> includes coff-pe), xcoff, mips, elf, macho and all. All is treated
>> especially, activating all supported readers.
>>
>> A few targets may require specific binary file format support, as they
>> directly call functions defined by the file reader. Specifically,
>> windows targets require coff support, and rs6000 aix and lynx178 targets
>> require xcoff support. Considering that those formats are the main - or
>> only - one available in those targets, I believe it makes sense to
>> re-enable those readers. If that happens, the script will emit the
>> following warning:
>>
>>    FOO is required to support one or more requested targets. Adding it
>>
>> Users aren't able to force the disabling of those formats, since GDB
>> will not compile without those readers. Ideally we'd like to be able
>> to disable even those formats, in case a user wants to build GDB only
>> to examine remote files for example, but the current infrastructure
>> for the file format readers doesn't allow us to do it.
>>
>> Mach-O and elf support are also dependent on BFD support being compiled
>> in.  In case one of those was requested and BFD does not support them,
>> the following error is emitted:
>>
>>      FOO was requested, but BFD does not support it.
>>
>> Finally, this configure switch is also printed by the "show
>> configuration" command in GDB.
>> ---
>>   gdb/Makefile.in      | 13 +++----
>>   gdb/NEWS             | 11 ++++++
>>   gdb/README           | 24 +++++++++++++
>>   gdb/config.in        |  3 ++
>>   gdb/configure        | 81 +++++++++++++++++++++++++++++++++++++++++---
>>   gdb/configure.ac     | 65 +++++++++++++++++++++++++++++++++--
>>   gdb/configure.format | 51 ++++++++++++++++++++++++++++
>>   gdb/configure.tgt    | 20 +++++++++--
>>   gdb/doc/gdb.texinfo  |  8 +++++
>>   gdb/top.c            |  5 +++
>>   10 files changed, 264 insertions(+), 17 deletions(-)
>>   create mode 100644 gdb/configure.format
> Thanks, but can you describe the changes sine v2, so I could determine
> whether I need to review the documentation parts of this?  TIA.
>
oh sorry, I knew I had forgotten something.

The change is pretty trivial, I just updated the documentation to have 
the correct  flag. I believe there should be nothing for you to review I 
just forgot to add your tag.

-- 
Cheers,
Guinevere Larsen
She/Her/Hers


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v3] gdb, configure: Add disable-formats option for configure
  2025-03-07 12:49   ` Guinevere Larsen
@ 2025-03-07 13:03     ` Eli Zaretskii
  0 siblings, 0 replies; 25+ messages in thread
From: Eli Zaretskii @ 2025-03-07 13:03 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: gdb-patches

> Date: Fri, 7 Mar 2025 09:49:14 -0300
> Cc: gdb-patches@sourceware.org
> From: Guinevere Larsen <guinevere@redhat.com>
> 
> The change is pretty trivial, I just updated the documentation to have 
> the correct  flag. I believe there should be nothing for you to review I 
> just forgot to add your tag.

Thanks.

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [PING][PATCH v3] gdb, configure: Add disable-formats option for configure
  2025-03-07 11:58 [PATCH v3] gdb, configure: Add disable-formats option for configure Guinevere Larsen
  2025-03-07 12:09 ` Eli Zaretskii
@ 2025-03-27 20:21 ` Guinevere Larsen
  2025-04-03 17:35 ` [PATCH " Tom Tromey
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 25+ messages in thread
From: Guinevere Larsen @ 2025-03-27 20:21 UTC (permalink / raw)
  To: gdb-patches, guinevere

Ping :)

-- 
Cheers,
Guinevere Larsen
She/Her/Hers


On 3/7/25 8:58 AM, Guinevere Larsen wrote:
> GDB has support for many binary file formats, some which might be very
> unlikely to be found in some situations (such as the COFF format in
> linux). This commit introduces the option for a user to choose which
> formats GDB will support at build configuration time.
>
> This is especially useful to avoid possible security concerns with
> readers that aren't expected to be used at all, as they are one of
> the simplest vectors for an attacker to try and hit GDB with.  This
> change can also reduce the size of the final binary, if that is a
> concern.
>
> This commit adds a switch to the configure script allowing a user to
> only enable selected file formats, called --enable-binary-file-formats.
> The default behavior when the switch is omitted is to compile all file
> formats, keeping the original behavior of the script. At the time of
> this commit, the valid options for this option are: dbx, coff (which
> includes coff-pe), xcoff, mips, elf, macho and all. All is treated
> especially, activating all supported readers.
>
> A few targets may require specific binary file format support, as they
> directly call functions defined by the file reader. Specifically,
> windows targets require coff support, and rs6000 aix and lynx178 targets
> require xcoff support. Considering that those formats are the main - or
> only - one available in those targets, I believe it makes sense to
> re-enable those readers. If that happens, the script will emit the
> following warning:
>
>    FOO is required to support one or more requested targets. Adding it
>
> Users aren't able to force the disabling of those formats, since GDB
> will not compile without those readers. Ideally we'd like to be able
> to disable even those formats, in case a user wants to build GDB only
> to examine remote files for example, but the current infrastructure
> for the file format readers doesn't allow us to do it.
>
> Mach-O and elf support are also dependent on BFD support being compiled
> in.  In case one of those was requested and BFD does not support them,
> the following error is emitted:
>
>      FOO was requested, but BFD does not support it.
>
> Finally, this configure switch is also printed by the "show
> configuration" command in GDB.
> ---
>   gdb/Makefile.in      | 13 +++----
>   gdb/NEWS             | 11 ++++++
>   gdb/README           | 24 +++++++++++++
>   gdb/config.in        |  3 ++
>   gdb/configure        | 81 +++++++++++++++++++++++++++++++++++++++++---
>   gdb/configure.ac     | 65 +++++++++++++++++++++++++++++++++--
>   gdb/configure.format | 51 ++++++++++++++++++++++++++++
>   gdb/configure.tgt    | 20 +++++++++--
>   gdb/doc/gdb.texinfo  |  8 +++++
>   gdb/top.c            |  5 +++
>   10 files changed, 264 insertions(+), 17 deletions(-)
>   create mode 100644 gdb/configure.format
>
> diff --git a/gdb/Makefile.in b/gdb/Makefile.in
> index a24c86fcff8..778ed54090f 100644
> --- a/gdb/Makefile.in
> +++ b/gdb/Makefile.in
> @@ -904,13 +904,15 @@ ALL_TARGET_OBS = \
>   	vax-tdep.o \
>   	windows-tdep.o \
>   	x86-tdep.o \
> -	xcoffread.o \
>   	xstormy16-tdep.o \
>   	xtensa-config.o \
>   	xtensa-linux-tdep.o \
>   	xtensa-tdep.o \
>   	z80-tdep.o
>   
> +# Object files for reading specific types of debug information.
> +FORMAT_OBS = @FORMAT_OBS@
> +
>   # The following native-target dependent variables are defined on
>   # configure.nat.
>   NAT_FILE = @NAT_FILE@
> @@ -1067,8 +1069,6 @@ COMMON_SFILES = \
>   	c-varobj.c \
>   	charset.c \
>   	cli-out.c \
> -	coff-pe-read.c \
> -	coffread.c \
>   	complaints.c \
>   	completer.c \
>   	copying.c \
> @@ -1082,7 +1082,6 @@ COMMON_SFILES = \
>   	d-lang.c \
>   	d-namespace.c \
>   	d-valprint.c \
> -	dbxread.c \
>   	dcache.c \
>   	debug.c \
>   	debuginfod-support.c \
> @@ -1174,7 +1173,6 @@ COMMON_SFILES = \
>   	memtag.c \
>   	minidebug.c \
>   	minsyms.c \
> -	mipsread.c \
>   	namespace.c \
>   	objc-lang.c \
>   	objfiles.c \
> @@ -1267,7 +1265,6 @@ SFILES = \
>   	d-exp.y \
>   	dtrace-probe.c \
>   	elf-none-tdep.c \
> -	elfread.c \
>   	f-exp.y \
>   	gcore-elf.c \
>   	gdb.c \
> @@ -1887,7 +1884,6 @@ ALLDEPFILES = \
>   	x86-gnu-nat.c \
>   	x86-nat.c \
>   	x86-tdep.c \
> -	xcoffread.c \
>   	xstormy16-tdep.c \
>   	xtensa-config.c \
>   	xtensa-linux-nat.c \
> @@ -1909,7 +1905,8 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
>   	$(SUBDIR_CLI_OBS) \
>   	$(SUBDIR_MI_OBS) \
>   	$(SUBDIR_TARGET_OBS) \
> -	$(SUBDIR_GCC_COMPILE_OBS)
> +	$(SUBDIR_GCC_COMPILE_OBS) \
> +	$(FORMAT_OBS)
>   
>   SUBDIRS = doc @subdirs@ data-directory
>   CLEANDIRS = $(SUBDIRS)
> diff --git a/gdb/NEWS b/gdb/NEWS
> index f5dbf5c3350..2a916d356ef 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -85,6 +85,17 @@ qXfer:threads:read
>   * Support for stabs debugging format and the a.out/dbx object format is
>     deprecated, and will be removed in GDB 18.
>   
> +* Configure changes
> +
> +enable-binary-file-formats=[FORMAT,...]
> +enable-binary-file-formats=all
> +  A user can now decide to only compile support for certain file formats.
> +  The available formats at this point are: dbx, coff, xcoff, elf, mach-o
> +  and mips.  Some targets require specific file formats to be available,
> +  and in such cases, the configure script will warn the user and add
> +  support anyway.  By default, all formats will be compiled in, to
> +  continue the behavior from before adding the switch.
> +
>   *** Changes in GDB 16
>   
>   * Support for Nios II targets has been removed as this architecture
> diff --git a/gdb/README b/gdb/README
> index b6eb3f12c56..0168afe7cd1 100644
> --- a/gdb/README
> +++ b/gdb/README
> @@ -417,6 +417,30 @@ more obscure GDB `configure' options are not listed here.
>        There is no convenient way to generate a list of all available
>        targets.
>   
> +`--enable-binary-file-formats=FORMAT,FORMAT,...'
> +`--enable-binary-file-formats=all'
> +    Configure GDB to only be be able to read selected file formats.
> +    The special value "all" causes all file formats to be compiled
> +    in, and is the the default behavior of the option.  This option
> +    is meant for advanced users who are sure of what they expect,
> +    if you are unsure which options you will need on your debugging
> +    sessions, we recommend that you not use this feature.  The
> +    accepted options are:
> +      * coff: Main format on windows systems. This is required to
> +        compile with windows target support;
> +      * dbx (also known as a.out): is a legacy file format, this
> +        is recommended if you know you will be dealing with this
> +        file format;
> +      * elf: Main format of linux systems. This is heavily
> +        recommended when compiling with linux support;
> +      * macho: Main format on MacOS systems, this is heavily
> +        recommended when compiling for those targets;
> +      * mips (also known as ecoff): this is the main file format
> +        for targets running on MIPS CPUs. It is heavily recommended
> +        when supporting those targets.
> +      * xcoff: Main format of AIX systems, this is required to
> +        compile for AIX targets and rs6000 CPUs.
> +
>   `--with-gdb-datadir=PATH'
>        Set the GDB-specific data directory.  GDB will look here for
>        certain supporting files or scripts.  This defaults to the `gdb'
> diff --git a/gdb/config.in b/gdb/config.in
> index db63aeaec75..248ebaa68af 100644
> --- a/gdb/config.in
> +++ b/gdb/config.in
> @@ -733,6 +733,9 @@
>   /* Define to 1 if you have the ANSI C header files. */
>   #undef STDC_HEADERS
>   
> +/* Which file formats were requested at configure time. */
> +#undef SUPPORTED_FORMATS
> +
>   /* automatically load a system-wide gdbinit file */
>   #undef SYSTEM_GDBINIT
>   
> diff --git a/gdb/configure b/gdb/configure
> index bdc405e01b3..8229ccaa588 100755
> --- a/gdb/configure
> +++ b/gdb/configure
> @@ -706,6 +706,7 @@ LIBGUI
>   LTLIBLZMA
>   LIBLZMA
>   HAVE_LIBLZMA
> +FORMAT_OBS
>   SER_HARDWIRE
>   WERROR_CFLAGS
>   WARN_CFLAGS
> @@ -934,6 +935,7 @@ with_relocated_sources
>   with_auto_load_dir
>   with_auto_load_safe_path
>   enable_targets
> +enable_binary_file_formats
>   enable_64_bit_bfd
>   with_amd_dbgapi
>   enable_tui
> @@ -1645,6 +1647,9 @@ Optional Features:
>     --disable-nls           do not use Native Language Support
>     --enable-targets=TARGETS
>                             alternative target configurations
> +  --enable-binary-file-formats=FILE_FORMATS
> +                          enable support for selected file formats (default
> +                          'all')
>     --enable-64-bit-bfd     64-bit support (on hosts with narrower word sizes)
>     --enable-tui            enable full-screen terminal user interface (TUI)
>     --enable-gdbtk          enable gdbtk graphical user interface (GUI)
> @@ -11500,7 +11505,7 @@ else
>     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
>     lt_status=$lt_dlunknown
>     cat > conftest.$ac_ext <<_LT_EOF
> -#line 11503 "configure"
> +#line 11508 "configure"
>   #include "confdefs.h"
>   
>   #if HAVE_DLFCN_H
> @@ -11606,7 +11611,7 @@ else
>     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
>     lt_status=$lt_dlunknown
>     cat > conftest.$ac_ext <<_LT_EOF
> -#line 11609 "configure"
> +#line 11614 "configure"
>   #include "confdefs.h"
>   
>   #if HAVE_DLFCN_H
> @@ -24875,6 +24880,18 @@ esac
>   fi
>   
>   
> +# Check whether --enable-binary_file_formats was given.
> +if test "${enable_binary_file_formats+set}" = set; then :
> +  enableval=$enable_binary_file_formats; case "${enableval}" in
> + yes | "") as_fn_error $? "enable-formats option must specify file formats or 'all'" "$LINENO" 5
> +            ;;
> +  no)       enable_binary_file_formats= ;;
> +  *)        enable_binary_file_formats=$enableval ;;
> +esac
> +else
> +  enable_binary_file_formats=all
> +fi
> +
>   
>   # Check whether --enable-64-bit-bfd was given.
>   if test "${enable_64_bit_bfd+set}" = set; then :
> @@ -24958,11 +24975,20 @@ fi
>   TARGET_OBS=
>   all_targets=
>   HAVE_NATIVE_GCORE_TARGET=
> +# File formats that will be enabled based on the selected
> +# target(s).  These are chosen because they are required to
> +# compile one or more of the selected targets.
> +target_formats=
> +# If all targets were requested, this is all formats that should
> +# accompany them.  These are just the ones required for compilation
> +# to succeed, not the formats suggested based on targets.
> +all_target_formats="coff xcoff"
>   
>   for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
>   do
>     if test "$targ_alias" = "all"; then
>       all_targets=true
> +    target_formats=$all_target_formats
>     else
>       # Canonicalize the secondary target names.
>       result=`$ac_config_sub $targ_alias 2>/dev/null`
> @@ -31553,6 +31579,10 @@ fi
>   # Note that WIN32APILIBS is set by GDB_AC_COMMON.
>   WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
>   
> +# Object files to be used when building with support for all file formats.
> +all_binary_format_obs="dbxread.o mipsread.o coffread.o coff-pe-read.o xcoffread.o"
> +
> +support_elf=no
>   # Add ELF support to GDB, but only if BFD includes ELF support.
>   
>     OLD_CFLAGS=$CFLAGS
> @@ -31605,7 +31635,7 @@ $as_echo "$gdb_cv_var_elf" >&6; }
>     LDFLAGS=$OLD_LDFLAGS
>     LIBS=$OLD_LIBS
>   if test "$gdb_cv_var_elf" = yes; then
> -  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
> +  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
>   		gcore-elf.o elf-none-tdep.o"
>   
>   $as_echo "#define HAVE_ELF 1" >>confdefs.h
> @@ -31669,9 +31699,12 @@ if test "$ac_res" != no; then :
>   fi
>   
>     fi
> +  support_elf=yes
> +  all_binary_format_obs="$all_binary_format_obs elfread.o"
>   fi
>   
>   # Add macho support to GDB, but only if BFD includes it.
> +support_macho=no
>   
>     OLD_CFLAGS=$CFLAGS
>     OLD_LDFLAGS=$LDFLAGS
> @@ -31723,9 +31756,49 @@ $as_echo "$gdb_cv_var_macho" >&6; }
>     LDFLAGS=$OLD_LDFLAGS
>     LIBS=$OLD_LIBS
>   if test "$gdb_cv_var_macho" = yes; then
> -  CONFIG_OBS="$CONFIG_OBS machoread.o"
> +  support_macho=yes
> +  all_binary_format_obs="$all_binary_format_obs machoread.o"
> +fi
> +
> +FORMAT_OBS=
> +
> +if test "$enable_binary_file_formats" != "all"; then
> +    # Formats that are required by some requested target(s).
> +    # Warn users that they are added, so no one is surprised.
> +    for req in $target_formats; do
> +	if ! echo "$enable_binary_file_formats" | grep -wq "$req"; then
> +	    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&5
> +$as_echo "$as_me: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&2;}
> +	    enable_binary_file_formats="${enable_binary_file_formats},$req"
> +	fi
> +    done
> +
> +
> +cat >>confdefs.h <<_ACEOF
> +#define SUPPORTED_FORMATS "$enable_binary_file_formats"
> +_ACEOF
> +
>   fi
>   
> +enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
> +
> +for format in $enable_binary_file_formats
> +do
> +    if test "$format" = "elf"; then
> +	if test "$support_elf" != "yes"; then
> +	    as_fn_error but BFD does not support it." "\"elf support was requested" "$LINENO" 5;
> +	fi
> +    elif test "$format" = "macho"; then
> +	if test "$support_macho" != "yes"; then
> +	    as_fn_error but BFD does not support it." "\"Mach-O support was requested" "$LINENO" 5;
> +	fi
> +    fi
> +
> +    . ${srcdir}/configure.format
> +done
> +
> +
> +
>   # Add any host-specific objects to GDB.
>   CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
>   
> diff --git a/gdb/configure.ac b/gdb/configure.ac
> index fb77e79e73b..c5e774962ce 100644
> --- a/gdb/configure.ac
> +++ b/gdb/configure.ac
> @@ -191,6 +191,15 @@ AS_HELP_STRING([--enable-targets=TARGETS], [alternative target configurations]),
>   	;;
>   esac])
>   
> +AC_ARG_ENABLE(binary_file_formats,
> +	      AS_HELP_STRING([--enable-binary-file-formats=FILE_FORMATS],
> +			     [enable support for selected file formats (default 'all')]),
> +[case "${enableval}" in
> + yes | "") AC_MSG_ERROR(enable-formats option must specify file formats or 'all')
> +            ;;
> +  no)       enable_binary_file_formats= ;;
> +  *)        enable_binary_file_formats=$enableval ;;
> +esac], [enable_binary_file_formats=all])
>   
>   BFD_64_BIT
>   
> @@ -211,11 +220,20 @@ fi
>   TARGET_OBS=
>   all_targets=
>   HAVE_NATIVE_GCORE_TARGET=
> +# File formats that will be enabled based on the selected
> +# target(s).  These are chosen because they are required to
> +# compile one or more of the selected targets.
> +target_formats=
> +# If all targets were requested, this is all formats that should
> +# accompany them.  These are just the ones required for compilation
> +# to succeed, not the formats suggested based on targets.
> +all_target_formats="coff xcoff"
>   
>   for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
>   do
>     if test "$targ_alias" = "all"; then
>       all_targets=true
> +    target_formats=$all_target_formats
>     else
>       # Canonicalize the secondary target names.
>       result=`$ac_config_sub $targ_alias 2>/dev/null`
> @@ -1903,11 +1921,15 @@ fi
>   # Note that WIN32APILIBS is set by GDB_AC_COMMON.
>   WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
>   
> +# Object files to be used when building with support for all file formats.
> +all_binary_format_obs="dbxread.o mipsread.o coffread.o coff-pe-read.o xcoffread.o"
> +
> +support_elf=no
>   # Add ELF support to GDB, but only if BFD includes ELF support.
>   GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_elf,
>                    [bfd_get_elf_phdr_upper_bound (NULL)], elf-bfd.h)
>   if test "$gdb_cv_var_elf" = yes; then
> -  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
> +  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
>   		gcore-elf.o elf-none-tdep.o"
>     AC_DEFINE(HAVE_ELF, 1,
>   	    [Define if ELF support should be included.])
> @@ -1915,15 +1937,54 @@ if test "$gdb_cv_var_elf" = yes; then
>     if test "$plugins" = "yes"; then
>       AC_SEARCH_LIBS(dlopen, dl)
>     fi
> +  support_elf=yes
> +  all_binary_format_obs="$all_binary_format_obs elfread.o"
>   fi
>   
>   # Add macho support to GDB, but only if BFD includes it.
> +support_macho=no
>   GDB_AC_CHECK_BFD([for Mach-O support in BFD], gdb_cv_var_macho,
>                    [bfd_mach_o_lookup_command (NULL, 0, NULL)], mach-o.h)
>   if test "$gdb_cv_var_macho" = yes; then
> -  CONFIG_OBS="$CONFIG_OBS machoread.o"
> +  support_macho=yes
> +  all_binary_format_obs="$all_binary_format_obs machoread.o"
> +fi
> +
> +FORMAT_OBS=
> +
> +if test "$enable_binary_file_formats" != "all"; then
> +    # Formats that are required by some requested target(s).
> +    # Warn users that they are added, so no one is surprised.
> +    for req in $target_formats; do
> +	if ! echo "$enable_binary_file_formats" | grep -wq "$req"; then
> +	    AC_MSG_WARN("$req is required to support one or more requested targets.  Adding it")
> +	    enable_binary_file_formats="${enable_binary_file_formats},$req"
> +	fi
> +    done
> +
> +    AC_DEFINE_UNQUOTED(SUPPORTED_FORMATS, "$enable_binary_file_formats",
> +		       Which file formats were requested at configure time. )
>   fi
>   
> +enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
> +
> +for format in $enable_binary_file_formats
> +do
> +    if test "$format" = "elf"; then
> +	if test "$support_elf" != "yes"; then
> +	    AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
> +	fi
> +    elif test "$format" = "macho"; then
> +	if test "$support_macho" != "yes"; then
> +	    AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
> +	fi
> +    fi
> +
> +    . ${srcdir}/configure.format
> +done
> +
> +AC_SUBST(FORMAT_OBS)
> +
>   # Add any host-specific objects to GDB.
>   CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
>   
> diff --git a/gdb/configure.format b/gdb/configure.format
> new file mode 100644
> index 00000000000..5ac86359037
> --- /dev/null
> +++ b/gdb/configure.format
> @@ -0,0 +1,51 @@
> +# Copyright (C) 2024-2025 Free Software Foundation, Inc.
> +#
> +# This file is part of GDB.
> +#
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +
> +# This file is used to decide which files need to be compiled to support
> +# the requested file formats
> +
> +case $format in
> +    xcoff)
> +	FORMAT_OBS="$FORMAT_OBS xcoffread.o"
> +	;;
> +
> +    # Despite the naming convention implying coff-pe to be a separate
> +    # reader, it is in fact just a helper for coffread;
> +    coff)
> +	FORMAT_OBS="$FORMAT_OBS coffread.o coff-pe-read.o"
> +	;;
> +
> +    dbx)
> +	FORMAT_OBS="$FORMAT_OBS dbxread.o"
> +	;;
> +
> +    elf)
> +	FORMAT_OBS="$FORMAT_OBS elfread.o"
> +	;;
> +
> +    macho)
> +	FORMAT_OBS="$FORMAT_OBS machoread.o"
> +	;;
> +
> +    mips)
> +	FORMAT_OBS="$FORMAT_OBS mipsread.o"
> +	;;
> +
> +    all)
> +	FORMAT_OBS="$all_binary_format_obs"
> +	;;
> +esac
> diff --git a/gdb/configure.tgt b/gdb/configure.tgt
> index 18a15c032c3..064deddc97b 100644
> --- a/gdb/configure.tgt
> +++ b/gdb/configure.tgt
> @@ -496,7 +496,7 @@ powerpc-*-openbsd*)
>   	;;
>   powerpc-*-aix* | rs6000-*-* | powerpc64-*-aix*)
>   	# Target: PowerPC running AIX
> -	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o xcoffread.o \
> +	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o \
>   			ppc-sysv-tdep.o solib-aix.o \
>   			ravenscar-thread.o ppc-ravenscar-thread.o"
>   	;;
> @@ -512,8 +512,8 @@ powerpc*-*-linux*)
>   powerpc-*-lynx*178)
>   	# Target: PowerPC running Lynx178.
>   	gdb_target_obs="rs6000-tdep.o rs6000-lynx178-tdep.o \
> -			xcoffread.o ppc-sysv-tdep.o \
> -			ravenscar-thread.o ppc-ravenscar-thread.o"
> +			ppc-sysv-tdep.o ravenscar-thread.o \
> +			ppc-ravenscar-thread.o"
>   	;;
>   powerpc*-*-*)
>   	# Target: PowerPC running eabi
> @@ -817,3 +817,17 @@ for t in x ${gdb_target_obs}; do
>       gdb_have_gcore=true
>     fi
>   done
> +
> +# Decide which file formats are absolutely required based on
> +# the requested targets.  Warn later that they are added, in
> +# case the user didn't manually request them, or all readers.
> +# It's fine to add the same format multiple times since the
> +# loop that reads the options to FORMAT_OBS will ensure that
> +# they are only added once.
> +for i in $gdb_target_obs; do
> +    case "${i}" in
> +    *"windows-tdep.o" ) target_formats="${target_formats} coff";;
> +    "rs6000-aix-tdep.o" ) target_formats="${target_formats} xcoff";;
> +    "rs6000-lynx178-tdep.o" ) target_formats="${target_formats} xcoff";;
> +    esac
> +done
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index 95a881e9dff..26434183769 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -41258,6 +41258,14 @@ Configure @value{GDBN} for cross-debugging programs running on the
>   specified list of targets.  The special value @samp{all} configures
>   @value{GDBN} for debugging programs running on any target it supports.
>   
> +@item --enable-binary-file-formats=@r{[}@var{format}@r{]}@dots{}
> +@itemx --enable-binary-file-formats=all
> +Configure @value{GDBN} to support certain binary file formats.  If a
> +format is the main (or only) file format for a given target, the
> +configure script may add support to it anyway, and warn the user.
> +If not given, all file formats that @value{GDBN} supports are compiled
> +in.
> +
>   @item --with-gdb-datadir=@var{path}
>   Set the @value{GDBN}-specific data directory.  @value{GDBN} will look
>   here for certain supporting files or scripts.  This defaults to the
> diff --git a/gdb/top.c b/gdb/top.c
> index b2575bc2b58..9342085b3a9 100644
> --- a/gdb/top.c
> +++ b/gdb/top.c
> @@ -1595,6 +1595,11 @@ This GDB was configured as follows:\n\
>   	     --with-system-gdbinit-dir=%s%s\n\
>   "), SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE ? " (relocatable)" : "");
>   
> +#ifdef SUPPORTED_FORMATS
> +  gdb_printf (stream, _("\
> +	     --enable-binary-file-formats=%s\n"), SUPPORTED_FORMATS);
> +#endif
> +
>     /* We assume "relocatable" will be printed at least once, thus we always
>        print this text.  It's a reasonably safe assumption for now.  */
>     gdb_printf (stream, _("\n\


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v3] gdb, configure: Add disable-formats option for configure
  2025-03-07 11:58 [PATCH v3] gdb, configure: Add disable-formats option for configure Guinevere Larsen
  2025-03-07 12:09 ` Eli Zaretskii
  2025-03-27 20:21 ` [PING][PATCH " Guinevere Larsen
@ 2025-04-03 17:35 ` Tom Tromey
  2025-04-07 17:48   ` Guinevere Larsen
  2025-04-08 13:47 ` [PATCH v4] " Guinevere Larsen
  2025-05-27 11:32 ` [PATCH v3] gdb, configure: Add disable-formats " Pedro Alves
  4 siblings, 1 reply; 25+ messages in thread
From: Tom Tromey @ 2025-04-03 17:35 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: gdb-patches

>>>>> "Guinevere" == Guinevere Larsen <guinevere@redhat.com> writes:

Guinevere> GDB has support for many binary file formats, some which might be very
Guinevere> unlikely to be found in some situations (such as the COFF format in
Guinevere> linux). This commit introduces the option for a user to choose which
Guinevere> formats GDB will support at build configuration time.

Guinevere> +* Configure changes
Guinevere> +
Guinevere> +enable-binary-file-formats=[FORMAT,...]
Guinevere> +enable-binary-file-formats=all

Add the leading "--" here?
I didn't look to see what other NEWS entries do.

Guinevere> +# Object files to be used when building with support for all file formats.
Guinevere> +all_binary_format_obs="dbxread.o mipsread.o coffread.o coff-pe-read.o xcoffread.o"

I would much rather have this kind of information in the Makefile.  I
know gdb doesn't always do that right now -- but the current way is kind
of bad IMO.

We can use GNU make features, so it's not hard to transform a list of
formats like "elf xcoff" into a list of variable references like
"$(elf_OBJS) $(xcoff_OBJS)"

Guinevere> diff --git a/gdb/configure.format b/gdb/configure.format

Then we wouldn't need this file, either.

Tom

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v3] gdb, configure: Add disable-formats option for configure
  2025-04-03 17:35 ` [PATCH " Tom Tromey
@ 2025-04-07 17:48   ` Guinevere Larsen
  2025-04-07 19:23     ` Tom Tromey
  0 siblings, 1 reply; 25+ messages in thread
From: Guinevere Larsen @ 2025-04-07 17:48 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1725 bytes --]

On 4/3/25 2:35 PM, Tom Tromey wrote:
>>>>>> "Guinevere" == Guinevere Larsen<guinevere@redhat.com> writes:
> Guinevere> GDB has support for many binary file formats, some which might be very
> Guinevere> unlikely to be found in some situations (such as the COFF format in
> Guinevere> linux). This commit introduces the option for a user to choose which
> Guinevere> formats GDB will support at build configuration time.
>
> Guinevere> +* Configure changes
> Guinevere> +
> Guinevere> +enable-binary-file-formats=[FORMAT,...]
> Guinevere> +enable-binary-file-formats=all
>
> Add the leading "--" here?
> I didn't look to see what other NEWS entries do.
I double checked the other NEWS entries, and you're right, I should add 
"--" to the option.
> Guinevere> +# Object files to be used when building with support for all file formats.
> Guinevere> +all_binary_format_obs="dbxread.o mipsread.o coffread.o coff-pe-read.o xcoffread.o"
>
> I would much rather have this kind of information in the Makefile.  I
> know gdb doesn't always do that right now -- but the current way is kind
> of bad IMO.
>
> We can use GNU make features, so it's not hard to transform a list of
> formats like "elf xcoff" into a list of variable references like
> "$(elf_OBJS) $(xcoff_OBJS)"

I'm fine with changing this. I'm only unsure what GNU make feature you 
mean. I suppose you mean the makefile should have:

elf_OBJS=elfread.c
xcoff_OBJS=xcoff.c
all_format_OBJS="$(elf_OBJS) $(xcoff_OBJS) ..."

and there is some way to parse --enable-binary-file-formats in the makefile?

>
> Guinevere> diff --git a/gdb/configure.format b/gdb/configure.format
>
> Then we wouldn't need this file, either.
>
> Tom
>

-- 
Cheers,
Guinevere Larsen
She/Her/Hers

[-- Attachment #2: Type: text/html, Size: 2906 bytes --]

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v3] gdb, configure: Add disable-formats option for configure
  2025-04-07 17:48   ` Guinevere Larsen
@ 2025-04-07 19:23     ` Tom Tromey
  0 siblings, 0 replies; 25+ messages in thread
From: Tom Tromey @ 2025-04-07 19:23 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: Tom Tromey, gdb-patches

Guinevere> I'm fine with changing this. I'm only unsure what GNU make
Guinevere> feature you mean. I suppose you mean the makefile should
Guinevere> have:

Guinevere> elf_OBJS=elfread.c
Guinevere> xcoff_OBJS=xcoff.c
Guinevere> all_format_OBJS="$(elf_OBJS) $(xcoff_OBJS) ..."

Yeah.

Guinevere> and there is some way to parse --enable-binary-file-formats
Guinevere> in the makefile?

This bit would still have to be in configure.  configure would just
select which object keywords end up in some list.

Basically I don't really like that the configure script has to be edited
when shuffling source files around.  Decisions about what to include or
exclude can be in configure, but decisions about the files themselves
should just be in makefile.

Tom

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [PATCH v4] gdb, configure: Add disable-formats option for configure
  2025-03-07 11:58 [PATCH v3] gdb, configure: Add disable-formats option for configure Guinevere Larsen
                   ` (2 preceding siblings ...)
  2025-04-03 17:35 ` [PATCH " Tom Tromey
@ 2025-04-08 13:47 ` Guinevere Larsen
  2025-04-08 14:06   ` Andreas Schwab
  2025-05-26 17:11   ` [PATCH v5 1/1] " Guinevere Larsen
  2025-05-27 11:32 ` [PATCH v3] gdb, configure: Add disable-formats " Pedro Alves
  4 siblings, 2 replies; 25+ messages in thread
From: Guinevere Larsen @ 2025-04-08 13:47 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen, Eli Zaretskii

GDB has support for many binary file formats, some which might be very
unlikely to be found in some situations (such as the COFF format in
linux). This commit introduces the option for a user to choose which
formats GDB will support at build configuration time.

This is especially useful to avoid possible security concerns with
readers that aren't expected to be used at all, as they are one of
the simplest vectors for an attacker to try and hit GDB with.  This
change can also reduce the size of the final binary, if that is a
concern.

This commit adds a switch to the configure script allowing a user to
only enable selected file formats, called --enable-binary-file-formats.
The default behavior when the switch is omitted is to compile all file
formats, keeping the original behavior of the script. At the time of
this commit, the valid options for this option are: dbx, coff (which
includes coff-pe), xcoff, mips, elf, macho and all. All is treated
especially, activating all supported readers.

A few targets may require specific binary file format support, as they
directly call functions defined by the file reader. Specifically,
windows targets require coff support, and rs6000 aix and lynx178 targets
require xcoff support. Considering that those formats are the main - or
only - one available in those targets, I believe it makes sense to
re-enable those readers. If that happens, the script will emit the
following warning:

  FOO is required to support one or more requested targets. Adding it

Users aren't able to force the disabling of those formats, since GDB
will not compile without those readers. Ideally we'd like to be able
to disable even those formats, in case a user wants to build GDB only
to examine remote files for example, but the current infrastructure
for the file format readers doesn't allow us to do it.

Mach-O and elf support are also dependent on BFD support being compiled
in.  In case one of those was requested and BFD does not support them,
the following error is emitted:

    FOO was requested, but BFD does not support it.

Finally, this configure switch is also printed by the "show
configuration" command in GDB.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---

Changes for v4:
* Removed configure.format
* Moved .c files back to the Makefile

---
 gdb/Makefile.in     | 20 ++++++----
 gdb/NEWS            | 11 ++++++
 gdb/README          | 24 ++++++++++++
 gdb/config.in       |  3 ++
 gdb/configure       | 89 +++++++++++++++++++++++++++++++++++++++++++--
 gdb/configure.ac    | 73 ++++++++++++++++++++++++++++++++++++-
 gdb/configure.tgt   | 20 ++++++++--
 gdb/doc/gdb.texinfo |  8 ++++
 gdb/top.c           |  5 +++
 9 files changed, 236 insertions(+), 17 deletions(-)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 0c4102d2565..eee9f243755 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -904,13 +904,22 @@ ALL_TARGET_OBS = \
 	vax-tdep.o \
 	windows-tdep.o \
 	x86-tdep.o \
-	xcoffread.o \
 	xstormy16-tdep.o \
 	xtensa-config.o \
 	xtensa-linux-tdep.o \
 	xtensa-tdep.o \
 	z80-tdep.o
 
+# Object files for reading specific types of debug information.
+coff_SRCS = coffread.c coff-pe-read.c
+dbx_ARCS = dbxread.c
+elf_SRCS = elfread.c
+macho_SRCS = machoread.c
+mips_SRCS = mipsread.c
+xcoff_SRCS = xcoffread.c
+FORMAT_SRCS = @FORMAT_SRCS@
+FORMAT_OBS = $(patsubst %.c,%.o,$(FORMAT_SRCS))
+
 # The following native-target dependent variables are defined on
 # configure.nat.
 NAT_FILE = @NAT_FILE@
@@ -1067,8 +1076,6 @@ COMMON_SFILES = \
 	c-varobj.c \
 	charset.c \
 	cli-out.c \
-	coff-pe-read.c \
-	coffread.c \
 	complaints.c \
 	completer.c \
 	copying.c \
@@ -1082,7 +1089,6 @@ COMMON_SFILES = \
 	d-lang.c \
 	d-namespace.c \
 	d-valprint.c \
-	dbxread.c \
 	dcache.c \
 	debug.c \
 	debuginfod-support.c \
@@ -1178,7 +1184,6 @@ COMMON_SFILES = \
 	memtag.c \
 	minidebug.c \
 	minsyms.c \
-	mipsread.c \
 	namespace.c \
 	objc-lang.c \
 	objfiles.c \
@@ -1271,7 +1276,6 @@ SFILES = \
 	d-exp.y \
 	dtrace-probe.c \
 	elf-none-tdep.c \
-	elfread.c \
 	f-exp.y \
 	gcore-elf.c \
 	gdb.c \
@@ -1895,7 +1899,6 @@ ALLDEPFILES = \
 	x86-gnu-nat.c \
 	x86-nat.c \
 	x86-tdep.c \
-	xcoffread.c \
 	xstormy16-tdep.c \
 	xtensa-config.c \
 	xtensa-linux-nat.c \
@@ -1916,7 +1919,8 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	$(patsubst %.c,%.o,$(COMMON_SFILES)) \
 	$(SUBDIR_CLI_OBS) \
 	$(SUBDIR_MI_OBS) \
-	$(SUBDIR_TARGET_OBS)
+	$(SUBDIR_TARGET_OBS) \
+	$(FORMAT_OBS)
 
 SUBDIRS = doc @subdirs@ data-directory
 CLEANDIRS = $(SUBDIRS)
diff --git a/gdb/NEWS b/gdb/NEWS
index 99ec392d4c4..df9f1653d64 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -117,6 +117,17 @@ qXfer:threads:read
 * Support for stabs debugging format and the a.out/dbx object format is
   deprecated, and will be removed in GDB 18.
 
+* Configure changes
+
+--enable-binary-file-formats=[FORMAT,...]
+--enable-binary-file-formats=all
+  A user can now decide to only compile support for certain file formats.
+  The available formats at this point are: dbx, coff, xcoff, elf, mach-o
+  and mips.  Some targets require specific file formats to be available,
+  and in such cases, the configure script will warn the user and add
+  support anyway.  By default, all formats will be compiled in, to
+  continue the behavior from before adding the switch.
+
 * A new configure option was added, allowing support for the compile
   subsystem to be disabled at configure time, in the form of
   --disable-gdb-compile.
diff --git a/gdb/README b/gdb/README
index 8836120d7ca..e104cf93fad 100644
--- a/gdb/README
+++ b/gdb/README
@@ -417,6 +417,30 @@ more obscure GDB `configure' options are not listed here.
      There is no convenient way to generate a list of all available
      targets.
 
+`--enable-binary-file-formats=FORMAT,FORMAT,...'
+`--enable-binary-file-formats=all'
+    Configure GDB to only be be able to read selected file formats.
+    The special value "all" causes all file formats to be compiled
+    in, and is the the default behavior of the option.  This option
+    is meant for advanced users who are sure of what they expect,
+    if you are unsure which options you will need on your debugging
+    sessions, we recommend that you not use this feature.  The
+    accepted options are:
+      * coff: Main format on windows systems. This is required to
+        compile with windows target support;
+      * dbx (also known as a.out): is a legacy file format, this
+        is recommended if you know you will be dealing with this
+        file format;
+      * elf: Main format of linux systems. This is heavily
+        recommended when compiling with linux support;
+      * macho: Main format on MacOS systems, this is heavily
+        recommended when compiling for those targets;
+      * mips (also known as ecoff): this is the main file format
+        for targets running on MIPS CPUs. It is heavily recommended
+        when supporting those targets.
+      * xcoff: Main format of AIX systems, this is required to
+        compile for AIX targets and rs6000 CPUs.
+
 `--with-gdb-datadir=PATH'
      Set the GDB-specific data directory.  GDB will look here for
      certain supporting files or scripts.  This defaults to the `gdb'
diff --git a/gdb/config.in b/gdb/config.in
index 86ff67d2898..68afb89201f 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -736,6 +736,9 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+/* Which file formats were requested at configure time. */
+#undef SUPPORTED_FORMATS
+
 /* automatically load a system-wide gdbinit file */
 #undef SYSTEM_GDBINIT
 
diff --git a/gdb/configure b/gdb/configure
index e8a649f2ef0..434afa25c9a 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -706,6 +706,7 @@ LIBGUI
 LTLIBLZMA
 LIBLZMA
 HAVE_LIBLZMA
+FORMAT_SRCS
 SER_HARDWIRE
 WERROR_CFLAGS
 WARN_CFLAGS
@@ -934,6 +935,7 @@ with_relocated_sources
 with_auto_load_dir
 with_auto_load_safe_path
 enable_targets
+enable_binary_file_formats
 enable_64_bit_bfd
 with_amd_dbgapi
 enable_tui
@@ -1646,6 +1648,9 @@ Optional Features:
   --disable-nls           do not use Native Language Support
   --enable-targets=TARGETS
                           alternative target configurations
+  --enable-binary-file-formats=FILE_FORMATS
+                          enable support for selected file formats (default
+                          'all')
   --enable-64-bit-bfd     64-bit support (on hosts with narrower word sizes)
   --enable-tui            enable full-screen terminal user interface (TUI)
   --enable-gdbtk          enable gdbtk graphical user interface (GUI)
@@ -11503,7 +11508,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11506 "configure"
+#line 11511 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11609,7 +11614,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11612 "configure"
+#line 11617 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -24878,6 +24883,18 @@ esac
 fi
 
 
+# Check whether --enable-binary_file_formats was given.
+if test "${enable_binary_file_formats+set}" = set; then :
+  enableval=$enable_binary_file_formats; case "${enableval}" in
+ yes | "") as_fn_error $? "enable-formats option must specify file formats or 'all'" "$LINENO" 5
+            ;;
+  no)       enable_binary_file_formats= ;;
+  *)        enable_binary_file_formats=$enableval ;;
+esac
+else
+  enable_binary_file_formats=all
+fi
+
 
 # Check whether --enable-64-bit-bfd was given.
 if test "${enable_64_bit_bfd+set}" = set; then :
@@ -24961,11 +24978,20 @@ fi
 TARGET_OBS=
 all_targets=
 HAVE_NATIVE_GCORE_TARGET=
+# File formats that will be enabled based on the selected
+# target(s).  These are chosen because they are required to
+# compile one or more of the selected targets.
+target_formats=
+# If all targets were requested, this is all formats that should
+# accompany them.  These are just the ones required for compilation
+# to succeed, not the formats suggested based on targets.
+all_target_formats="coff xcoff"
 
 for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
 do
   if test "$targ_alias" = "all"; then
     all_targets=true
+    target_formats=$all_target_formats
   else
     # Canonicalize the secondary target names.
     result=`$ac_config_sub $targ_alias 2>/dev/null`
@@ -31602,6 +31628,10 @@ fi
 # Note that WIN32APILIBS is set by GDB_AC_COMMON.
 WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
 
+# Object files to be used when building with support for all file formats.
+all_binary_format_obs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
+
+support_elf=no
 # Add ELF support to GDB, but only if BFD includes ELF support.
 
   OLD_CFLAGS=$CFLAGS
@@ -31654,7 +31684,7 @@ $as_echo "$gdb_cv_var_elf" >&6; }
   LDFLAGS=$OLD_LDFLAGS
   LIBS=$OLD_LIBS
 if test "$gdb_cv_var_elf" = yes; then
-  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
+  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
 		gcore-elf.o elf-none-tdep.o"
 
 $as_echo "#define HAVE_ELF 1" >>confdefs.h
@@ -31718,9 +31748,12 @@ if test "$ac_res" != no; then :
 fi
 
   fi
+  support_elf=yes
+  all_binary_format_obs="$all_binary_format_obs \$(elf_SRCS)"
 fi
 
 # Add macho support to GDB, but only if BFD includes it.
+support_macho=no
 
   OLD_CFLAGS=$CFLAGS
   OLD_LDFLAGS=$LDFLAGS
@@ -31772,9 +31805,57 @@ $as_echo "$gdb_cv_var_macho" >&6; }
   LDFLAGS=$OLD_LDFLAGS
   LIBS=$OLD_LIBS
 if test "$gdb_cv_var_macho" = yes; then
-  CONFIG_OBS="$CONFIG_OBS machoread.o"
+  support_macho=yes
+  all_binary_format_obs="$all_binary_format_obs \$(macho_SRCS)"
+fi
+
+FORMAT_SRCS=
+
+if test "$enable_binary_file_formats" != "all"; then
+    # Formats that are required by some requested target(s).
+    # Warn users that they are added, so no one is surprised.
+    for req in $target_formats; do
+	if ! echo "$enable_binary_file_formats" | grep -wq "$req"; then
+	    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&5
+$as_echo "$as_me: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&2;}
+	    enable_binary_file_formats="${enable_binary_file_formats},$req"
+	fi
+    done
+
+
+cat >>confdefs.h <<_ACEOF
+#define SUPPORTED_FORMATS "$enable_binary_file_formats"
+_ACEOF
+
 fi
 
+enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
+
+for format in $enable_binary_file_formats
+do
+    if test "$format" = "elf"; then
+	if test "$support_elf" != "yes"; then
+	    as_fn_error but BFD does not support it." "\"elf support was requested" "$LINENO" 5;
+	fi
+    elif test "$format" = "macho"; then
+	if test "$support_macho" != "yes"; then
+	    as_fn_error but BFD does not support it." "\"Mach-O support was requested" "$LINENO" 5;
+	fi
+    fi
+
+    if test "$format" = "all"; then
+	FORMAT_SRCS="$all_binary_format_obs"
+	# We don't break here in case the user requested Mach-O or ELF, but
+	# BFD is not configured to support it.  If we were to break, we would
+	# silently drop the requested support instead of erroring out.
+    else
+	fmt=$(echo "$format _SRCS" | sed 's/ //')
+	FORMAT_SRCS="$FORMAT_SRCS \$($fmt)"
+    fi
+done
+
+
+
 # Add any host-specific objects to GDB.
 CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
 
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 2411b10ca40..ca4f50f2644 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -191,6 +191,15 @@ AS_HELP_STRING([--enable-targets=TARGETS], [alternative target configurations]),
 	;;
 esac])
 
+AC_ARG_ENABLE(binary_file_formats,
+	      AS_HELP_STRING([--enable-binary-file-formats=FILE_FORMATS],
+			     [enable support for selected file formats (default 'all')]),
+[case "${enableval}" in
+ yes | "") AC_MSG_ERROR(enable-formats option must specify file formats or 'all')
+            ;;
+  no)       enable_binary_file_formats= ;;
+  *)        enable_binary_file_formats=$enableval ;;
+esac], [enable_binary_file_formats=all])
 
 BFD_64_BIT
 
@@ -211,11 +220,20 @@ fi
 TARGET_OBS=
 all_targets=
 HAVE_NATIVE_GCORE_TARGET=
+# File formats that will be enabled based on the selected
+# target(s).  These are chosen because they are required to
+# compile one or more of the selected targets.
+target_formats=
+# If all targets were requested, this is all formats that should
+# accompany them.  These are just the ones required for compilation
+# to succeed, not the formats suggested based on targets.
+all_target_formats="coff xcoff"
 
 for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
 do
   if test "$targ_alias" = "all"; then
     all_targets=true
+    target_formats=$all_target_formats
   else
     # Canonicalize the secondary target names.
     result=`$ac_config_sub $targ_alias 2>/dev/null`
@@ -1923,11 +1941,15 @@ fi
 # Note that WIN32APILIBS is set by GDB_AC_COMMON.
 WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
 
+# Object files to be used when building with support for all file formats.
+all_binary_format_obs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
+
+support_elf=no
 # Add ELF support to GDB, but only if BFD includes ELF support.
 GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_elf,
                  [bfd_get_elf_phdr_upper_bound (NULL)], elf-bfd.h)
 if test "$gdb_cv_var_elf" = yes; then
-  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
+  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
 		gcore-elf.o elf-none-tdep.o"
   AC_DEFINE(HAVE_ELF, 1,
 	    [Define if ELF support should be included.])
@@ -1935,15 +1957,62 @@ if test "$gdb_cv_var_elf" = yes; then
   if test "$plugins" = "yes"; then
     AC_SEARCH_LIBS(dlopen, dl)
   fi
+  support_elf=yes
+  all_binary_format_obs="$all_binary_format_obs \$(elf_SRCS)"
 fi
 
 # Add macho support to GDB, but only if BFD includes it.
+support_macho=no
 GDB_AC_CHECK_BFD([for Mach-O support in BFD], gdb_cv_var_macho,
                  [bfd_mach_o_lookup_command (NULL, 0, NULL)], mach-o.h)
 if test "$gdb_cv_var_macho" = yes; then
-  CONFIG_OBS="$CONFIG_OBS machoread.o"
+  support_macho=yes
+  all_binary_format_obs="$all_binary_format_obs \$(macho_SRCS)"
+fi
+
+FORMAT_SRCS=
+
+if test "$enable_binary_file_formats" != "all"; then
+    # Formats that are required by some requested target(s).
+    # Warn users that they are added, so no one is surprised.
+    for req in $target_formats; do
+	if ! echo "$enable_binary_file_formats" | grep -wq "$req"; then
+	    AC_MSG_WARN("$req is required to support one or more requested targets.  Adding it")
+	    enable_binary_file_formats="${enable_binary_file_formats},$req"
+	fi
+    done
+
+    AC_DEFINE_UNQUOTED(SUPPORTED_FORMATS, "$enable_binary_file_formats",
+		       Which file formats were requested at configure time. )
 fi
 
+enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
+
+for format in $enable_binary_file_formats
+do
+    if test "$format" = "elf"; then
+	if test "$support_elf" != "yes"; then
+	    AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
+	fi
+    elif test "$format" = "macho"; then
+	if test "$support_macho" != "yes"; then
+	    AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
+	fi
+    fi
+
+    if test "$format" = "all"; then
+	FORMAT_SRCS="$all_binary_format_obs"
+	# We don't break here in case the user requested Mach-O or ELF, but
+	# BFD is not configured to support it.  If we were to break, we would
+	# silently drop the requested support instead of erroring out.
+    else
+	fmt=$(echo "$format _SRCS" | sed 's/ //')
+	FORMAT_SRCS="$FORMAT_SRCS \$($fmt)"
+    fi
+done
+
+AC_SUBST(FORMAT_SRCS)
+
 # Add any host-specific objects to GDB.
 CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
 
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index 18a15c032c3..064deddc97b 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -496,7 +496,7 @@ powerpc-*-openbsd*)
 	;;
 powerpc-*-aix* | rs6000-*-* | powerpc64-*-aix*)
 	# Target: PowerPC running AIX
-	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o xcoffread.o \
+	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o \
 			ppc-sysv-tdep.o solib-aix.o \
 			ravenscar-thread.o ppc-ravenscar-thread.o"
 	;;
@@ -512,8 +512,8 @@ powerpc*-*-linux*)
 powerpc-*-lynx*178)
 	# Target: PowerPC running Lynx178.
 	gdb_target_obs="rs6000-tdep.o rs6000-lynx178-tdep.o \
-			xcoffread.o ppc-sysv-tdep.o \
-			ravenscar-thread.o ppc-ravenscar-thread.o"
+			ppc-sysv-tdep.o ravenscar-thread.o \
+			ppc-ravenscar-thread.o"
 	;;
 powerpc*-*-*)
 	# Target: PowerPC running eabi
@@ -817,3 +817,17 @@ for t in x ${gdb_target_obs}; do
     gdb_have_gcore=true
   fi
 done
+
+# Decide which file formats are absolutely required based on
+# the requested targets.  Warn later that they are added, in
+# case the user didn't manually request them, or all readers.
+# It's fine to add the same format multiple times since the
+# loop that reads the options to FORMAT_OBS will ensure that
+# they are only added once.
+for i in $gdb_target_obs; do
+    case "${i}" in
+    *"windows-tdep.o" ) target_formats="${target_formats} coff";;
+    "rs6000-aix-tdep.o" ) target_formats="${target_formats} xcoff";;
+    "rs6000-lynx178-tdep.o" ) target_formats="${target_formats} xcoff";;
+    esac
+done
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 4e4509a4649..ce26b26fe96 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -41269,6 +41269,14 @@ Configure @value{GDBN} for cross-debugging programs running on the
 specified list of targets.  The special value @samp{all} configures
 @value{GDBN} for debugging programs running on any target it supports.
 
+@item --enable-binary-file-formats=@r{[}@var{format}@r{]}@dots{}
+@itemx --enable-binary-file-formats=all
+Configure @value{GDBN} to support certain binary file formats.  If a
+format is the main (or only) file format for a given target, the
+configure script may add support to it anyway, and warn the user.
+If not given, all file formats that @value{GDBN} supports are compiled
+in.
+
 @item --with-gdb-datadir=@var{path}
 Set the @value{GDBN}-specific data directory.  @value{GDBN} will look
 here for certain supporting files or scripts.  This defaults to the
diff --git a/gdb/top.c b/gdb/top.c
index 80d0ec5e679..a14781df973 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1595,6 +1595,11 @@ This GDB was configured as follows:\n\
 	     --with-system-gdbinit-dir=%s%s\n\
 "), SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE ? " (relocatable)" : "");
 
+#ifdef SUPPORTED_FORMATS
+  gdb_printf (stream, _("\
+	     --enable-binary-file-formats=%s\n"), SUPPORTED_FORMATS);
+#endif
+
   /* We assume "relocatable" will be printed at least once, thus we always
      print this text.  It's a reasonably safe assumption for now.  */
   gdb_printf (stream, _("\n\

base-commit: d8c3e4901650e35ddf5083f693b8a21b9d24fcc6
-- 
2.49.0


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4] gdb, configure: Add disable-formats option for configure
  2025-04-08 13:47 ` [PATCH v4] " Guinevere Larsen
@ 2025-04-08 14:06   ` Andreas Schwab
  2025-04-08 14:24     ` Guinevere Larsen
  2025-05-26 17:11   ` [PATCH v5 1/1] " Guinevere Larsen
  1 sibling, 1 reply; 25+ messages in thread
From: Andreas Schwab @ 2025-04-08 14:06 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: gdb-patches, Eli Zaretskii

On Apr 08 2025, Guinevere Larsen wrote:

> +	if ! echo "$enable_binary_file_formats" | grep -wq "$req"; then

grep -w is not portable.

You can use case ,$enable_binary_file_formats, in *[ ,]$req[ ,]*) ... to
make it portable.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4] gdb, configure: Add disable-formats option for configure
  2025-04-08 14:06   ` Andreas Schwab
@ 2025-04-08 14:24     ` Guinevere Larsen
  2025-04-08 14:44       ` Andreas Schwab
  0 siblings, 1 reply; 25+ messages in thread
From: Guinevere Larsen @ 2025-04-08 14:24 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: gdb-patches, Eli Zaretskii

On 4/8/25 11:06 AM, Andreas Schwab wrote:
> On Apr 08 2025, Guinevere Larsen wrote:
>
>> +	if ! echo "$enable_binary_file_formats" | grep -wq "$req"; then
> grep -w is not portable.
>
> You can use case ,$enable_binary_file_formats, in *[ ,]$req[ ,]*) ... to
> make it portable.
>
ah, thanks for the heads up, I'll try to fix that up.

The problem is that I want to reverse the case, I want to runt that code 
if req is not in enable_binary_file_formats. How would I do that with 
the portable syntax?

-- 
Cheers,
Guinevere Larsen
She/Her/Hers


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v4] gdb, configure: Add disable-formats option for configure
  2025-04-08 14:24     ` Guinevere Larsen
@ 2025-04-08 14:44       ` Andreas Schwab
  0 siblings, 0 replies; 25+ messages in thread
From: Andreas Schwab @ 2025-04-08 14:44 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: gdb-patches, Eli Zaretskii

On Apr 08 2025, Guinevere Larsen wrote:

> The problem is that I want to reverse the case, I want to runt that code
> if req is not in enable_binary_file_formats. How would I do that with the
> portable syntax?

Just put it in the default case.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [PATCH v5 1/1] gdb, configure: Add disable-formats option for configure
  2025-04-08 13:47 ` [PATCH v4] " Guinevere Larsen
  2025-04-08 14:06   ` Andreas Schwab
@ 2025-05-26 17:11   ` Guinevere Larsen
  2025-07-07 20:16     ` [PATCH v6] " Guinevere Larsen
  1 sibling, 1 reply; 25+ messages in thread
From: Guinevere Larsen @ 2025-05-26 17:11 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen, Eli Zaretskii

GDB has support for many binary file formats, some which might be very
unlikely to be found in some situations (such as the COFF format in
linux). This commit introduces the option for a user to choose which
formats GDB will support at build configuration time.

This is especially useful to avoid possible security concerns with
readers that aren't expected to be used at all, as they are one of
the simplest vectors for an attacker to try and hit GDB with.  This
change can also reduce the size of the final binary, if that is a
concern.

This commit adds a switch to the configure script allowing a user to
only enable selected file formats, called --enable-binary-file-formats.
The default behavior when the switch is omitted is to compile all file
formats, keeping the original behavior of the script. At the time of
this commit, the valid options for this option are: dbx, coff (which
includes coff-pe), xcoff, mips, elf, macho and all. All is treated
especially, activating all supported readers.

A few targets may require specific binary file format support, as they
directly call functions defined by the file reader. Specifically,
windows targets require coff support, and rs6000 aix and lynx178 targets
require xcoff support. Considering that those formats are the main - or
only - one available in those targets, I believe it makes sense to
re-enable those readers. If that happens, the script will emit the
following warning:

  FOO is required to support one or more requested targets. Adding it

Users aren't able to force the disabling of those formats, since GDB
will not compile without those readers. Ideally we'd like to be able
to disable even those formats, in case a user wants to build GDB only
to examine remote files for example, but the current infrastructure
for the file format readers doesn't allow us to do it.

Mach-O and elf support are also dependent on BFD support being compiled
in.  In case one of those was requested and BFD does not support them,
the following error is emitted:

    FOO was requested, but BFD does not support it.

Finally, this configure switch is also printed by the "show
configuration" command in GDB.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---

Sorry about the delay, this patch escaped my mind as other priorities
took over. For v5, the only change was to remove my usage of grep -w, as
it isn't portable (thanks Andreas).

---
 gdb/Makefile.in     | 20 ++++++----
 gdb/NEWS            | 11 ++++++
 gdb/README          | 24 ++++++++++++
 gdb/config.in       |  3 ++
 gdb/configure       | 96 +++++++++++++++++++++++++++++++++++++++++++--
 gdb/configure.ac    | 80 ++++++++++++++++++++++++++++++++++++-
 gdb/configure.tgt   | 20 ++++++++--
 gdb/doc/gdb.texinfo |  8 ++++
 gdb/top.c           |  5 +++
 9 files changed, 250 insertions(+), 17 deletions(-)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 1b867666991..57449cc6e62 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -906,13 +906,22 @@ ALL_TARGET_OBS = \
 	vax-tdep.o \
 	windows-tdep.o \
 	x86-tdep.o \
-	xcoffread.o \
 	xstormy16-tdep.o \
 	xtensa-config.o \
 	xtensa-linux-tdep.o \
 	xtensa-tdep.o \
 	z80-tdep.o
 
+# Object files for reading specific types of debug information.
+coff_SRCS = coffread.c coff-pe-read.c
+dbx_SRCS = dbxread.c
+elf_SRCS = elfread.c
+macho_SRCS = machoread.c
+mips_SRCS = mipsread.c
+xcoff_SRCS = xcoffread.c
+FORMAT_SRCS = @FORMAT_SRCS@
+FORMAT_OBS = $(patsubst %.c,%.o,$(FORMAT_SRCS))
+
 # The following native-target dependent variables are defined on
 # configure.nat.
 NAT_FILE = @NAT_FILE@
@@ -1069,8 +1078,6 @@ COMMON_SFILES = \
 	c-varobj.c \
 	charset.c \
 	cli-out.c \
-	coff-pe-read.c \
-	coffread.c \
 	complaints.c \
 	completer.c \
 	copying.c \
@@ -1084,7 +1091,6 @@ COMMON_SFILES = \
 	d-lang.c \
 	d-namespace.c \
 	d-valprint.c \
-	dbxread.c \
 	dcache.c \
 	debug.c \
 	debuginfod-support.c \
@@ -1180,7 +1186,6 @@ COMMON_SFILES = \
 	memtag.c \
 	minidebug.c \
 	minsyms.c \
-	mipsread.c \
 	namespace.c \
 	objc-lang.c \
 	objfiles.c \
@@ -1273,7 +1278,6 @@ SFILES = \
 	d-exp.y \
 	dtrace-probe.c \
 	elf-none-tdep.c \
-	elfread.c \
 	f-exp.y \
 	gcore-elf.c \
 	gdb.c \
@@ -1899,7 +1903,6 @@ ALLDEPFILES = \
 	x86-gnu-nat.c \
 	x86-nat.c \
 	x86-tdep.c \
-	xcoffread.c \
 	xstormy16-tdep.c \
 	xtensa-config.c \
 	xtensa-linux-nat.c \
@@ -1920,7 +1923,8 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	$(patsubst %.c,%.o,$(COMMON_SFILES)) \
 	$(SUBDIR_CLI_OBS) \
 	$(SUBDIR_MI_OBS) \
-	$(SUBDIR_TARGET_OBS)
+	$(SUBDIR_TARGET_OBS) \
+	$(FORMAT_OBS)
 
 SUBDIRS = doc @subdirs@ data-directory
 CLEANDIRS = $(SUBDIRS)
diff --git a/gdb/NEWS b/gdb/NEWS
index 2e48a00df5a..527908928a9 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -191,6 +191,17 @@ qXfer:threads:read
 * Support for stabs debugging format and the a.out/dbx object format is
   deprecated, and will be removed in GDB 18.
 
+* Configure changes
+
+--enable-binary-file-formats=[FORMAT,...]
+--enable-binary-file-formats=all
+  A user can now decide to only compile support for certain file formats.
+  The available formats at this point are: dbx, coff, xcoff, elf, mach-o
+  and mips.  Some targets require specific file formats to be available,
+  and in such cases, the configure script will warn the user and add
+  support anyway.  By default, all formats will be compiled in, to
+  continue the behavior from before adding the switch.
+
 * A new configure option was added, allowing support for the compile
   subsystem to be disabled at configure time, in the form of
   --disable-gdb-compile.
diff --git a/gdb/README b/gdb/README
index 5a0030511d6..48dbc046045 100644
--- a/gdb/README
+++ b/gdb/README
@@ -417,6 +417,30 @@ more obscure GDB `configure' options are not listed here.
      There is no convenient way to generate a list of all available
      targets.
 
+`--enable-binary-file-formats=FORMAT,FORMAT,...'
+`--enable-binary-file-formats=all'
+    Configure GDB to only be be able to read selected file formats.
+    The special value "all" causes all file formats to be compiled
+    in, and is the the default behavior of the option.  This option
+    is meant for advanced users who are sure of what they expect,
+    if you are unsure which options you will need on your debugging
+    sessions, we recommend that you not use this feature.  The
+    accepted options are:
+      * coff: Main format on windows systems. This is required to
+        compile with windows target support;
+      * dbx (also known as a.out): is a legacy file format, this
+        is recommended if you know you will be dealing with this
+        file format;
+      * elf: Main format of linux systems. This is heavily
+        recommended when compiling with linux support;
+      * macho: Main format on MacOS systems, this is heavily
+        recommended when compiling for those targets;
+      * mips (also known as ecoff): this is the main file format
+        for targets running on MIPS CPUs. It is heavily recommended
+        when supporting those targets.
+      * xcoff: Main format of AIX systems, this is required to
+        compile for AIX targets and rs6000 CPUs.
+
 `--with-gdb-datadir=PATH'
      Set the GDB-specific data directory.  GDB will look here for
      certain supporting files or scripts.  This defaults to the `gdb'
diff --git a/gdb/config.in b/gdb/config.in
index 426947e6b2d..f0a0edb137c 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -733,6 +733,9 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+/* Which file formats were requested at configure time. */
+#undef SUPPORTED_FORMATS
+
 /* automatically load a system-wide gdbinit file */
 #undef SYSTEM_GDBINIT
 
diff --git a/gdb/configure b/gdb/configure
index 683fcaef9fe..c1ef0fae499 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -706,6 +706,7 @@ LIBGUI
 LTLIBLZMA
 LIBLZMA
 HAVE_LIBLZMA
+FORMAT_SRCS
 SER_HARDWIRE
 WERROR_CFLAGS
 WARN_CFLAGS
@@ -932,6 +933,7 @@ with_relocated_sources
 with_auto_load_dir
 with_auto_load_safe_path
 enable_targets
+enable_binary_file_formats
 with_amd_dbgapi
 enable_tui
 enable_gdbtk
@@ -1643,6 +1645,9 @@ Optional Features:
   --disable-nls           do not use Native Language Support
   --enable-targets=TARGETS
                           alternative target configurations
+  --enable-binary-file-formats=FILE_FORMATS
+                          enable support for selected file formats (default
+                          'all')
   --enable-tui            enable full-screen terminal user interface (TUI)
   --enable-gdbtk          enable gdbtk graphical user interface (GUI)
   --enable-profiling      enable profiling of GDB
@@ -11499,7 +11504,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11502 "configure"
+#line 11507 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11605,7 +11610,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11608 "configure"
+#line 11613 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -24874,6 +24879,18 @@ esac
 fi
 
 
+# Check whether --enable-binary_file_formats was given.
+if test "${enable_binary_file_formats+set}" = set; then :
+  enableval=$enable_binary_file_formats; case "${enableval}" in
+ yes | "") as_fn_error $? "enable-formats option must specify file formats or 'all'" "$LINENO" 5
+            ;;
+  no)       enable_binary_file_formats= ;;
+  *)        enable_binary_file_formats=$enableval ;;
+esac
+else
+  enable_binary_file_formats=all
+fi
+
 
 # See whether 64-bit bfd lib has been enabled.
 OLD_CPPFLAGS=$CPPFLAGS
@@ -24927,11 +24944,20 @@ fi
 TARGET_OBS=
 all_targets=
 HAVE_NATIVE_GCORE_TARGET=
+# File formats that will be enabled based on the selected
+# target(s).  These are chosen because they are required to
+# compile one or more of the selected targets.
+target_formats=
+# If all targets were requested, this is all formats that should
+# accompany them.  These are just the ones required for compilation
+# to succeed, not the formats suggested based on targets.
+all_target_formats="coff xcoff"
 
 for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
 do
   if test "$targ_alias" = "all"; then
     all_targets=true
+    target_formats=$all_target_formats
   else
     # Canonicalize the secondary target names.
     result=`$ac_config_sub $targ_alias 2>/dev/null`
@@ -31559,6 +31585,12 @@ fi
 # Note that WIN32APILIBS is set by GDB_AC_COMMON.
 WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
 
+# Object files to be used when building with support for all file formats.
+# This should not have elf or macho, as support for those formats depends
+# on BFD enabling them as well.
+all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
+
+support_elf=no
 # Add ELF support to GDB, but only if BFD includes ELF support.
 
   OLD_CFLAGS=$CFLAGS
@@ -31611,7 +31643,7 @@ $as_echo "$gdb_cv_var_elf" >&6; }
   LDFLAGS=$OLD_LDFLAGS
   LIBS=$OLD_LIBS
 if test "$gdb_cv_var_elf" = yes; then
-  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
+  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
 		gcore-elf.o elf-none-tdep.o"
 
 $as_echo "#define HAVE_ELF 1" >>confdefs.h
@@ -31675,9 +31707,12 @@ if test "$ac_res" != no; then :
 fi
 
   fi
+  support_elf=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(elf_SRCS)"
 fi
 
 # Add macho support to GDB, but only if BFD includes it.
+support_macho=no
 
   OLD_CFLAGS=$CFLAGS
   OLD_LDFLAGS=$LDFLAGS
@@ -31729,9 +31764,62 @@ $as_echo "$gdb_cv_var_macho" >&6; }
   LDFLAGS=$OLD_LDFLAGS
   LIBS=$OLD_LIBS
 if test "$gdb_cv_var_macho" = yes; then
-  CONFIG_OBS="$CONFIG_OBS machoread.o"
+  support_macho=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(macho_SRCS)"
 fi
 
+FORMAT_SRCS=
+
+if test "$enable_binary_file_formats" != "all"; then
+    # Formats that are required by some requested target(s).
+    # Warn users that they are added, so no one is surprised.
+    for req in $target_formats; do
+	case ,$enable_binary_file_formats, in
+	*,$req,*)
+	    # Do nothing.
+	    ;;
+	*)
+	    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&5
+$as_echo "$as_me: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&2;}
+	    enable_binary_file_formats="${enable_binary_file_formats},$req"
+	    ;;
+	esac
+    done
+
+
+cat >>confdefs.h <<_ACEOF
+#define SUPPORTED_FORMATS "$enable_binary_file_formats"
+_ACEOF
+
+fi
+
+enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
+
+for format in $enable_binary_file_formats
+do
+    if test "$format" = "elf"; then
+	if test "$support_elf" != "yes"; then
+	    as_fn_error but BFD does not support it." "\"elf support was requested" "$LINENO" 5;
+	fi
+    elif test "$format" = "macho"; then
+	if test "$support_macho" != "yes"; then
+	    as_fn_error but BFD does not support it." "\"Mach-O support was requested" "$LINENO" 5;
+	fi
+    fi
+
+    if test "$format" = "all"; then
+	FORMAT_SRCS="$all_binary_file_srcs"
+	# We don't break here in case the user requested Mach-O or ELF, but
+	# BFD is not configured to support it.  If we were to break, we would
+	# silently drop the requested support instead of erroring out.
+    else
+	fmt=$(echo "$format _SRCS" | sed 's/ //')
+	FORMAT_SRCS="$FORMAT_SRCS \$($fmt)"
+    fi
+done
+
+
+
 # Add any host-specific objects to GDB.
 CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
 
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 052d0319a04..8ce336c9b92 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -191,6 +191,15 @@ AS_HELP_STRING([--enable-targets=TARGETS], [alternative target configurations]),
 	;;
 esac])
 
+AC_ARG_ENABLE(binary_file_formats,
+	      AS_HELP_STRING([--enable-binary-file-formats=FILE_FORMATS],
+			     [enable support for selected file formats (default 'all')]),
+[case "${enableval}" in
+ yes | "") AC_MSG_ERROR(enable-formats option must specify file formats or 'all')
+            ;;
+  no)       enable_binary_file_formats= ;;
+  *)        enable_binary_file_formats=$enableval ;;
+esac], [enable_binary_file_formats=all])
 
 BFD_64_BIT
 
@@ -211,11 +220,20 @@ fi
 TARGET_OBS=
 all_targets=
 HAVE_NATIVE_GCORE_TARGET=
+# File formats that will be enabled based on the selected
+# target(s).  These are chosen because they are required to
+# compile one or more of the selected targets.
+target_formats=
+# If all targets were requested, this is all formats that should
+# accompany them.  These are just the ones required for compilation
+# to succeed, not the formats suggested based on targets.
+all_target_formats="coff xcoff"
 
 for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
 do
   if test "$targ_alias" = "all"; then
     all_targets=true
+    target_formats=$all_target_formats
   else
     # Canonicalize the secondary target names.
     result=`$ac_config_sub $targ_alias 2>/dev/null`
@@ -1914,11 +1932,17 @@ fi
 # Note that WIN32APILIBS is set by GDB_AC_COMMON.
 WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
 
+# Object files to be used when building with support for all file formats.
+# This should not have elf or macho, as support for those formats depends
+# on BFD enabling them as well.
+all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
+
+support_elf=no
 # Add ELF support to GDB, but only if BFD includes ELF support.
 GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_elf,
                  [bfd_get_elf_phdr_upper_bound (NULL)], elf-bfd.h)
 if test "$gdb_cv_var_elf" = yes; then
-  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
+  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
 		gcore-elf.o elf-none-tdep.o"
   AC_DEFINE(HAVE_ELF, 1,
 	    [Define if ELF support should be included.])
@@ -1926,15 +1950,67 @@ if test "$gdb_cv_var_elf" = yes; then
   if test "$plugins" = "yes"; then
     AC_SEARCH_LIBS(dlopen, dl)
   fi
+  support_elf=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(elf_SRCS)"
 fi
 
 # Add macho support to GDB, but only if BFD includes it.
+support_macho=no
 GDB_AC_CHECK_BFD([for Mach-O support in BFD], gdb_cv_var_macho,
                  [bfd_mach_o_lookup_command (NULL, 0, NULL)], mach-o.h)
 if test "$gdb_cv_var_macho" = yes; then
-  CONFIG_OBS="$CONFIG_OBS machoread.o"
+  support_macho=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(macho_SRCS)"
+fi
+
+FORMAT_SRCS=
+
+if test "$enable_binary_file_formats" != "all"; then
+    # Formats that are required by some requested target(s).
+    # Warn users that they are added, so no one is surprised.
+    for req in $target_formats; do
+	case ,$enable_binary_file_formats, in
+	*,$req,*)
+	    # Do nothing.
+	    ;;
+	*)
+	    AC_MSG_WARN("$req is required to support one or more requested targets.  Adding it")
+	    enable_binary_file_formats="${enable_binary_file_formats},$req"
+	    ;;
+	esac
+    done
+
+    AC_DEFINE_UNQUOTED(SUPPORTED_FORMATS, "$enable_binary_file_formats",
+		       Which file formats were requested at configure time. )
 fi
 
+enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
+
+for format in $enable_binary_file_formats
+do
+    if test "$format" = "elf"; then
+	if test "$support_elf" != "yes"; then
+	    AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
+	fi
+    elif test "$format" = "macho"; then
+	if test "$support_macho" != "yes"; then
+	    AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
+	fi
+    fi
+
+    if test "$format" = "all"; then
+	FORMAT_SRCS="$all_binary_file_srcs"
+	# We don't break here in case the user requested Mach-O or ELF, but
+	# BFD is not configured to support it.  If we were to break, we would
+	# silently drop the requested support instead of erroring out.
+    else
+	fmt=$(echo "$format _SRCS" | sed 's/ //')
+	FORMAT_SRCS="$FORMAT_SRCS \$($fmt)"
+    fi
+done
+
+AC_SUBST(FORMAT_SRCS)
+
 # Add any host-specific objects to GDB.
 CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
 
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index e9b306809dc..bae150ffdd4 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -496,7 +496,7 @@ powerpc-*-openbsd*)
 	;;
 powerpc-*-aix* | rs6000-*-* | powerpc64-*-aix*)
 	# Target: PowerPC running AIX
-	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o xcoffread.o \
+	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o \
 			ppc-sysv-tdep.o solib-aix.o \
 			ravenscar-thread.o ppc-ravenscar-thread.o"
 	;;
@@ -512,8 +512,8 @@ powerpc*-*-linux*)
 powerpc-*-lynx*178)
 	# Target: PowerPC running Lynx178.
 	gdb_target_obs="rs6000-tdep.o rs6000-lynx178-tdep.o \
-			xcoffread.o ppc-sysv-tdep.o \
-			ravenscar-thread.o ppc-ravenscar-thread.o"
+			ppc-sysv-tdep.o ravenscar-thread.o \
+			ppc-ravenscar-thread.o"
 	;;
 powerpc*-*-*)
 	# Target: PowerPC running eabi
@@ -819,3 +819,17 @@ for t in x ${gdb_target_obs}; do
     gdb_have_gcore=true
   fi
 done
+
+# Decide which file formats are absolutely required based on
+# the requested targets.  Warn later that they are added, in
+# case the user didn't manually request them, or all readers.
+# It's fine to add the same format multiple times since the
+# loop that reads the options to FORMAT_OBS will ensure that
+# they are only added once.
+for i in $gdb_target_obs; do
+    case "${i}" in
+    *"windows-tdep.o" ) target_formats="${target_formats} coff";;
+    "rs6000-aix-tdep.o" ) target_formats="${target_formats} xcoff";;
+    "rs6000-lynx178-tdep.o" ) target_formats="${target_formats} xcoff";;
+    esac
+done
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 05f550233fe..7c2724695b5 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -41379,6 +41379,14 @@ Configure @value{GDBN} for cross-debugging programs running on the
 specified list of targets.  The special value @samp{all} configures
 @value{GDBN} for debugging programs running on any target it supports.
 
+@item --enable-binary-file-formats=@r{[}@var{format}@r{]}@dots{}
+@itemx --enable-binary-file-formats=all
+Configure @value{GDBN} to support certain binary file formats.  If a
+format is the main (or only) file format for a given target, the
+configure script may add support to it anyway, and warn the user.
+If not given, all file formats that @value{GDBN} supports are compiled
+in.
+
 @item --with-gdb-datadir=@var{path}
 Set the @value{GDBN}-specific data directory.  @value{GDBN} will look
 here for certain supporting files or scripts.  This defaults to the
diff --git a/gdb/top.c b/gdb/top.c
index 6adef467b90..fff6308c7e9 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1595,6 +1595,11 @@ This GDB was configured as follows:\n\
 	     --with-system-gdbinit-dir=%s%s\n\
 "), SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE ? " (relocatable)" : "");
 
+#ifdef SUPPORTED_FORMATS
+  gdb_printf (stream, _("\
+	     --enable-binary-file-formats=%s\n"), SUPPORTED_FORMATS);
+#endif
+
   /* We assume "relocatable" will be printed at least once, thus we always
      print this text.  It's a reasonably safe assumption for now.  */
   gdb_printf (stream, _("\n\
-- 
2.49.0


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v3] gdb, configure: Add disable-formats option for configure
  2025-03-07 11:58 [PATCH v3] gdb, configure: Add disable-formats option for configure Guinevere Larsen
                   ` (3 preceding siblings ...)
  2025-04-08 13:47 ` [PATCH v4] " Guinevere Larsen
@ 2025-05-27 11:32 ` Pedro Alves
  2025-05-27 16:26   ` Guinevere Larsen
  4 siblings, 1 reply; 25+ messages in thread
From: Pedro Alves @ 2025-05-27 11:32 UTC (permalink / raw)
  To: Guinevere Larsen, gdb-patches

On 2025-03-07 11:58, Guinevere Larsen wrote:
> GDB has support for many binary file formats, some which might be very
> unlikely to be found in some situations (such as the COFF format in
> linux). 

FYI, COFF isn't the best example here, as COFF/PE is used as format of UEFI boot images.


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v3] gdb, configure: Add disable-formats option for configure
  2025-05-27 11:32 ` [PATCH v3] gdb, configure: Add disable-formats " Pedro Alves
@ 2025-05-27 16:26   ` Guinevere Larsen
  0 siblings, 0 replies; 25+ messages in thread
From: Guinevere Larsen @ 2025-05-27 16:26 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 5/27/25 8:32 AM, Pedro Alves wrote:
> On 2025-03-07 11:58, Guinevere Larsen wrote:
>> GDB has support for many binary file formats, some which might be very
>> unlikely to be found in some situations (such as the COFF format in
>> linux).
> FYI, COFF isn't the best example here, as COFF/PE is used as format of UEFI boot images.
>
ah, good point. I'll change to "xcoff in x86 systems" instead.

-- 
Cheers,
Guinevere Larsen
She/Her/Hers


^ permalink raw reply	[flat|nested] 25+ messages in thread

* [PATCH v6] gdb, configure: Add disable-formats option for configure
  2025-05-26 17:11   ` [PATCH v5 1/1] " Guinevere Larsen
@ 2025-07-07 20:16     ` Guinevere Larsen
  2025-07-31 18:42       ` [PING][PATCH " Guinevere Larsen
                         ` (2 more replies)
  0 siblings, 3 replies; 25+ messages in thread
From: Guinevere Larsen @ 2025-07-07 20:16 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen, Eli Zaretskii

GDB has support for many binary file formats, some which might be very
unlikely to be found in some situations (such as the XCOFF format in
an x86 system). This commit introduces the option for a user to choose
which formats GDB will support at build configuration time.

This is especially useful to avoid possible security concerns with
readers that aren't expected to be used at all, as they are one of
the simplest vectors for an attacker to try and hit GDB with.  This
change can also reduce the size of the final binary, if that is a
concern.

This commit adds a switch to the configure script allowing a user to
only enable selected file formats, called --enable-binary-file-formats.
The default behavior when the switch is omitted is to compile all file
formats, keeping the original behavior of the script. At the time of
this commit, the valid options for this option are: dbx, coff (which
includes coff-pe), xcoff, mips, elf, macho and all. All is treated
especially, activating all supported readers.

A few targets may require specific binary file format support, as they
directly call functions defined by the file reader. Specifically,
windows targets require coff support, and rs6000 aix and lynx178 targets
require xcoff support. Considering that those formats are the main - or
only - one available in those targets, I believe it makes sense to
re-enable those readers. If that happens, the script will emit the
following warning:

  FOO is required to support one or more requested targets. Adding it

Users aren't able to force the disabling of those formats, since GDB
will not compile without those readers. Ideally we'd like to be able
to disable even those formats, in case a user wants to build GDB only
to examine remote files for example, but the current infrastructure
for the file format readers doesn't allow us to do it.

Mach-O and elf support are also dependent on BFD support being compiled
in.  In case one of those was requested and BFD does not support them,
the following error is emitted:

    FOO was requested, but BFD does not support it.

Finally, this configure switch is also printed by the "show
configuration" command in GDB.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---

This is rebased on current master, there was a bit of a rebase conflict,
but nothing complicated.

---
 gdb/Makefile.in     | 20 ++++++----
 gdb/NEWS            | 11 ++++++
 gdb/README          | 24 ++++++++++++
 gdb/config.in       |  3 ++
 gdb/configure       | 96 +++++++++++++++++++++++++++++++++++++++++++--
 gdb/configure.ac    | 80 ++++++++++++++++++++++++++++++++++++-
 gdb/configure.tgt   | 20 ++++++++--
 gdb/doc/gdb.texinfo |  8 ++++
 gdb/top.c           |  5 +++
 9 files changed, 250 insertions(+), 17 deletions(-)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index fc0c56564c2..294c9e19421 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -907,13 +907,22 @@ ALL_TARGET_OBS = \
 	vax-tdep.o \
 	windows-tdep.o \
 	x86-tdep.o \
-	xcoffread.o \
 	xstormy16-tdep.o \
 	xtensa-config.o \
 	xtensa-linux-tdep.o \
 	xtensa-tdep.o \
 	z80-tdep.o
 
+# Object files for reading specific types of debug information.
+coff_SRCS = coffread.c coff-pe-read.c
+dbx_SRCS = dbxread.c
+elf_SRCS = elfread.c
+macho_SRCS = machoread.c
+mips_SRCS = mipsread.c
+xcoff_SRCS = xcoffread.c
+FORMAT_SRCS = @FORMAT_SRCS@
+FORMAT_OBS = $(patsubst %.c,%.o,$(FORMAT_SRCS))
+
 # The following native-target dependent variables are defined on
 # configure.nat.
 NAT_FILE = @NAT_FILE@
@@ -1070,8 +1079,6 @@ COMMON_SFILES = \
 	c-varobj.c \
 	charset.c \
 	cli-out.c \
-	coff-pe-read.c \
-	coffread.c \
 	complaints.c \
 	completer.c \
 	copying.c \
@@ -1085,7 +1092,6 @@ COMMON_SFILES = \
 	d-lang.c \
 	d-namespace.c \
 	d-valprint.c \
-	dbxread.c \
 	dcache.c \
 	debug.c \
 	debuginfod-support.c \
@@ -1150,7 +1156,6 @@ COMMON_SFILES = \
 	memtag.c \
 	minidebug.c \
 	minsyms.c \
-	mipsread.c \
 	namespace.c \
 	objc-lang.c \
 	objfiles.c \
@@ -1243,7 +1248,6 @@ SFILES = \
 	d-exp.y \
 	dtrace-probe.c \
 	elf-none-tdep.c \
-	elfread.c \
 	f-exp.y \
 	gcore-elf.c \
 	gdb.c \
@@ -1870,7 +1874,6 @@ ALLDEPFILES = \
 	x86-gnu-nat.c \
 	x86-nat.c \
 	x86-tdep.c \
-	xcoffread.c \
 	xstormy16-tdep.c \
 	xtensa-config.c \
 	xtensa-linux-nat.c \
@@ -1925,7 +1928,8 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	$(patsubst %.c,%.o,$(COMMON_SFILES)) \
 	$(SUBDIR_CLI_OBS) \
 	$(SUBDIR_MI_OBS) \
-	$(SUBDIR_TARGET_OBS)
+	$(SUBDIR_TARGET_OBS) \
+	$(FORMAT_OBS)
 
 SUBDIRS = doc @subdirs@ data-directory
 CLEANDIRS = $(SUBDIRS)
diff --git a/gdb/NEWS b/gdb/NEWS
index 4c9aed421bb..a8c17318d7e 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -224,6 +224,17 @@ vFile:stat
 * Support for stabs debugging format and the a.out/dbx object format is
   deprecated, and will be removed in GDB 18.
 
+* Configure changes
+
+--enable-binary-file-formats=[FORMAT,...]
+--enable-binary-file-formats=all
+  A user can now decide to only compile support for certain file formats.
+  The available formats at this point are: dbx, coff, xcoff, elf, mach-o
+  and mips.  Some targets require specific file formats to be available,
+  and in such cases, the configure script will warn the user and add
+  support anyway.  By default, all formats will be compiled in, to
+  continue the behavior from before adding the switch.
+
 * A new configure option was added, allowing support for the compile
   subsystem to be disabled at configure time, in the form of
   --disable-gdb-compile.
diff --git a/gdb/README b/gdb/README
index eca4181c751..d45e6d37c4a 100644
--- a/gdb/README
+++ b/gdb/README
@@ -417,6 +417,30 @@ more obscure GDB `configure' options are not listed here.
      There is no convenient way to generate a list of all available
      targets.
 
+`--enable-binary-file-formats=FORMAT,FORMAT,...'
+`--enable-binary-file-formats=all'
+    Configure GDB to only be be able to read selected file formats.
+    The special value "all" causes all file formats to be compiled
+    in, and is the the default behavior of the option.  This option
+    is meant for advanced users who are sure of what they expect,
+    if you are unsure which options you will need on your debugging
+    sessions, we recommend that you not use this feature.  The
+    accepted options are:
+      * coff: Main format on windows systems. This is required to
+        compile with windows target support;
+      * dbx (also known as a.out): is a legacy file format, this
+        is recommended if you know you will be dealing with this
+        file format;
+      * elf: Main format of linux systems. This is heavily
+        recommended when compiling with linux support;
+      * macho: Main format on MacOS systems, this is heavily
+        recommended when compiling for those targets;
+      * mips (also known as ecoff): this is the main file format
+        for targets running on MIPS CPUs. It is heavily recommended
+        when supporting those targets.
+      * xcoff: Main format of AIX systems, this is required to
+        compile for AIX targets and rs6000 CPUs.
+
 `--with-gdb-datadir=PATH'
      Set the GDB-specific data directory.  GDB will look here for
      certain supporting files or scripts.  This defaults to the `gdb'
diff --git a/gdb/config.in b/gdb/config.in
index 149aeaf979b..9208b21946e 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -745,6 +745,9 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+/* Which file formats were requested at configure time. */
+#undef SUPPORTED_FORMATS
+
 /* automatically load a system-wide gdbinit file */
 #undef SYSTEM_GDBINIT
 
diff --git a/gdb/configure b/gdb/configure
index 8fc3b04efbf..c8e22e0eabc 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -706,6 +706,7 @@ LIBGUI
 LTLIBLZMA
 LIBLZMA
 HAVE_LIBLZMA
+FORMAT_SRCS
 SER_HARDWIRE
 WERROR_CFLAGS
 WARN_CFLAGS
@@ -932,6 +933,7 @@ with_relocated_sources
 with_auto_load_dir
 with_auto_load_safe_path
 enable_targets
+enable_binary_file_formats
 enable_gdb_mdebug_support
 enable_gdb_dwarf_support
 with_amd_dbgapi
@@ -1645,6 +1647,9 @@ Optional Features:
   --disable-nls           do not use Native Language Support
   --enable-targets=TARGETS
                           alternative target configurations
+  --enable-binary-file-formats=FILE_FORMATS
+                          enable support for selected file formats (default
+                          'all')
   --enable-gdb-mdebug-support
                           Enable support for the mdebug debuginfo format
                           (default 'yes')
@@ -11507,7 +11512,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11510 "configure"
+#line 11515 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11613,7 +11618,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11616 "configure"
+#line 11621 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -24882,6 +24887,18 @@ esac
 fi
 
 
+# Check whether --enable-binary_file_formats was given.
+if test "${enable_binary_file_formats+set}" = set; then :
+  enableval=$enable_binary_file_formats; case "${enableval}" in
+ yes | "") as_fn_error $? "enable-formats option must specify file formats or 'all'" "$LINENO" 5
+            ;;
+  no)       enable_binary_file_formats= ;;
+  *)        enable_binary_file_formats=$enableval ;;
+esac
+else
+  enable_binary_file_formats=all
+fi
+
 
 # Check whether to support mdebug/ecoff debug information.
 # Check whether --enable-gdb-mdebug-support was given.
@@ -24985,11 +25002,20 @@ fi
 TARGET_OBS=
 all_targets=
 HAVE_NATIVE_GCORE_TARGET=
+# File formats that will be enabled based on the selected
+# target(s).  These are chosen because they are required to
+# compile one or more of the selected targets.
+target_formats=
+# If all targets were requested, this is all formats that should
+# accompany them.  These are just the ones required for compilation
+# to succeed, not the formats suggested based on targets.
+all_target_formats="coff xcoff"
 
 for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
 do
   if test "$targ_alias" = "all"; then
     all_targets=true
+    target_formats=$all_target_formats
   else
     # Canonicalize the secondary target names.
     result=`$ac_config_sub $targ_alias 2>/dev/null`
@@ -31628,6 +31654,12 @@ fi
 # Note that WIN32APILIBS is set by GDB_AC_COMMON.
 WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
 
+# Object files to be used when building with support for all file formats.
+# This should not have elf or macho, as support for those formats depends
+# on BFD enabling them as well.
+all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
+
+support_elf=no
 # Add ELF support to GDB, but only if BFD includes ELF support.
 
   OLD_CFLAGS=$CFLAGS
@@ -31680,7 +31712,7 @@ $as_echo "$gdb_cv_var_elf" >&6; }
   LDFLAGS=$OLD_LDFLAGS
   LIBS=$OLD_LIBS
 if test "$gdb_cv_var_elf" = yes; then
-  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
+  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
 		gcore-elf.o elf-none-tdep.o"
 
 $as_echo "#define HAVE_ELF 1" >>confdefs.h
@@ -31744,9 +31776,12 @@ if test "$ac_res" != no; then :
 fi
 
   fi
+  support_elf=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(elf_SRCS)"
 fi
 
 # Add macho support to GDB, but only if BFD includes it.
+support_macho=no
 
   OLD_CFLAGS=$CFLAGS
   OLD_LDFLAGS=$LDFLAGS
@@ -31798,9 +31833,62 @@ $as_echo "$gdb_cv_var_macho" >&6; }
   LDFLAGS=$OLD_LDFLAGS
   LIBS=$OLD_LIBS
 if test "$gdb_cv_var_macho" = yes; then
-  CONFIG_OBS="$CONFIG_OBS machoread.o"
+  support_macho=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(macho_SRCS)"
 fi
 
+FORMAT_SRCS=
+
+if test "$enable_binary_file_formats" != "all"; then
+    # Formats that are required by some requested target(s).
+    # Warn users that they are added, so no one is surprised.
+    for req in $target_formats; do
+	case ,$enable_binary_file_formats, in
+	*,$req,*)
+	    # Do nothing.
+	    ;;
+	*)
+	    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&5
+$as_echo "$as_me: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&2;}
+	    enable_binary_file_formats="${enable_binary_file_formats},$req"
+	    ;;
+	esac
+    done
+
+
+cat >>confdefs.h <<_ACEOF
+#define SUPPORTED_FORMATS "$enable_binary_file_formats"
+_ACEOF
+
+fi
+
+enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
+
+for format in $enable_binary_file_formats
+do
+    if test "$format" = "elf"; then
+	if test "$support_elf" != "yes"; then
+	    as_fn_error but BFD does not support it." "\"elf support was requested" "$LINENO" 5;
+	fi
+    elif test "$format" = "macho"; then
+	if test "$support_macho" != "yes"; then
+	    as_fn_error but BFD does not support it." "\"Mach-O support was requested" "$LINENO" 5;
+	fi
+    fi
+
+    if test "$format" = "all"; then
+	FORMAT_SRCS="$all_binary_file_srcs"
+	# We don't break here in case the user requested Mach-O or ELF, but
+	# BFD is not configured to support it.  If we were to break, we would
+	# silently drop the requested support instead of erroring out.
+    else
+	fmt=$(echo "$format _SRCS" | sed 's/ //')
+	FORMAT_SRCS="$FORMAT_SRCS \$($fmt)"
+    fi
+done
+
+
+
 # Add any host-specific objects to GDB.
 CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
 
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 226e27e4fe5..edc6102c6da 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -191,6 +191,15 @@ AS_HELP_STRING([--enable-targets=TARGETS], [alternative target configurations]),
 	;;
 esac])
 
+AC_ARG_ENABLE(binary_file_formats,
+	      AS_HELP_STRING([--enable-binary-file-formats=FILE_FORMATS],
+			     [enable support for selected file formats (default 'all')]),
+[case "${enableval}" in
+ yes | "") AC_MSG_ERROR(enable-formats option must specify file formats or 'all')
+            ;;
+  no)       enable_binary_file_formats= ;;
+  *)        enable_binary_file_formats=$enableval ;;
+esac], [enable_binary_file_formats=all])
 
 # Check whether to support mdebug/ecoff debug information.
 AC_ARG_ENABLE(gdb-mdebug-support,
@@ -239,11 +248,20 @@ fi
 TARGET_OBS=
 all_targets=
 HAVE_NATIVE_GCORE_TARGET=
+# File formats that will be enabled based on the selected
+# target(s).  These are chosen because they are required to
+# compile one or more of the selected targets.
+target_formats=
+# If all targets were requested, this is all formats that should
+# accompany them.  These are just the ones required for compilation
+# to succeed, not the formats suggested based on targets.
+all_target_formats="coff xcoff"
 
 for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
 do
   if test "$targ_alias" = "all"; then
     all_targets=true
+    target_formats=$all_target_formats
   else
     # Canonicalize the secondary target names.
     result=`$ac_config_sub $targ_alias 2>/dev/null`
@@ -1952,11 +1970,17 @@ fi
 # Note that WIN32APILIBS is set by GDB_AC_COMMON.
 WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
 
+# Object files to be used when building with support for all file formats.
+# This should not have elf or macho, as support for those formats depends
+# on BFD enabling them as well.
+all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
+
+support_elf=no
 # Add ELF support to GDB, but only if BFD includes ELF support.
 GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_elf,
                  [bfd_get_elf_phdr_upper_bound (NULL)], elf-bfd.h)
 if test "$gdb_cv_var_elf" = yes; then
-  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
+  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
 		gcore-elf.o elf-none-tdep.o"
   AC_DEFINE(HAVE_ELF, 1,
 	    [Define if ELF support should be included.])
@@ -1964,15 +1988,67 @@ if test "$gdb_cv_var_elf" = yes; then
   if test "$plugins" = "yes"; then
     AC_SEARCH_LIBS(dlopen, dl)
   fi
+  support_elf=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(elf_SRCS)"
 fi
 
 # Add macho support to GDB, but only if BFD includes it.
+support_macho=no
 GDB_AC_CHECK_BFD([for Mach-O support in BFD], gdb_cv_var_macho,
                  [bfd_mach_o_lookup_command (NULL, 0, NULL)], mach-o.h)
 if test "$gdb_cv_var_macho" = yes; then
-  CONFIG_OBS="$CONFIG_OBS machoread.o"
+  support_macho=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(macho_SRCS)"
+fi
+
+FORMAT_SRCS=
+
+if test "$enable_binary_file_formats" != "all"; then
+    # Formats that are required by some requested target(s).
+    # Warn users that they are added, so no one is surprised.
+    for req in $target_formats; do
+	case ,$enable_binary_file_formats, in
+	*,$req,*)
+	    # Do nothing.
+	    ;;
+	*)
+	    AC_MSG_WARN("$req is required to support one or more requested targets.  Adding it")
+	    enable_binary_file_formats="${enable_binary_file_formats},$req"
+	    ;;
+	esac
+    done
+
+    AC_DEFINE_UNQUOTED(SUPPORTED_FORMATS, "$enable_binary_file_formats",
+		       Which file formats were requested at configure time. )
 fi
 
+enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
+
+for format in $enable_binary_file_formats
+do
+    if test "$format" = "elf"; then
+	if test "$support_elf" != "yes"; then
+	    AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
+	fi
+    elif test "$format" = "macho"; then
+	if test "$support_macho" != "yes"; then
+	    AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
+	fi
+    fi
+
+    if test "$format" = "all"; then
+	FORMAT_SRCS="$all_binary_file_srcs"
+	# We don't break here in case the user requested Mach-O or ELF, but
+	# BFD is not configured to support it.  If we were to break, we would
+	# silently drop the requested support instead of erroring out.
+    else
+	fmt=$(echo "$format _SRCS" | sed 's/ //')
+	FORMAT_SRCS="$FORMAT_SRCS \$($fmt)"
+    fi
+done
+
+AC_SUBST(FORMAT_SRCS)
+
 # Add any host-specific objects to GDB.
 CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
 
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index 255c77e9f8c..332f5b7513c 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -505,7 +505,7 @@ powerpc-*-openbsd*)
 	;;
 powerpc-*-aix* | rs6000-*-* | powerpc64-*-aix*)
 	# Target: PowerPC running AIX
-	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o xcoffread.o \
+	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o \
 			ppc-sysv-tdep.o solib-aix.o \
 			ravenscar-thread.o ppc-ravenscar-thread.o"
 	;;
@@ -522,8 +522,8 @@ powerpc*-*-linux*)
 powerpc-*-lynx*178)
 	# Target: PowerPC running Lynx178.
 	gdb_target_obs="rs6000-tdep.o rs6000-lynx178-tdep.o \
-			xcoffread.o ppc-sysv-tdep.o \
-			ravenscar-thread.o ppc-ravenscar-thread.o"
+			ppc-sysv-tdep.o ravenscar-thread.o \
+			ppc-ravenscar-thread.o"
 	;;
 powerpc*-*-*)
 	# Target: PowerPC running eabi
@@ -835,3 +835,17 @@ for t in x ${gdb_target_obs}; do
     gdb_have_gcore=true
   fi
 done
+
+# Decide which file formats are absolutely required based on
+# the requested targets.  Warn later that they are added, in
+# case the user didn't manually request them, or all readers.
+# It's fine to add the same format multiple times since the
+# loop that reads the options to FORMAT_OBS will ensure that
+# they are only added once.
+for i in $gdb_target_obs; do
+    case "${i}" in
+    *"windows-tdep.o" ) target_formats="${target_formats} coff";;
+    "rs6000-aix-tdep.o" ) target_formats="${target_formats} xcoff";;
+    "rs6000-lynx178-tdep.o" ) target_formats="${target_formats} xcoff";;
+    esac
+done
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 35b770f8138..bcd88b7b6a5 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -41267,6 +41267,14 @@ Configure @value{GDBN} for cross-debugging programs running on the
 specified list of targets.  The special value @samp{all} configures
 @value{GDBN} for debugging programs running on any target it supports.
 
+@item --enable-binary-file-formats=@r{[}@var{format}@r{]}@dots{}
+@itemx --enable-binary-file-formats=all
+Configure @value{GDBN} to support certain binary file formats.  If a
+format is the main (or only) file format for a given target, the
+configure script may add support to it anyway, and warn the user.
+If not given, all file formats that @value{GDBN} supports are compiled
+in.
+
 @item --with-gdb-datadir=@var{path}
 Set the @value{GDBN}-specific data directory.  @value{GDBN} will look
 here for certain supporting files or scripts.  This defaults to the
diff --git a/gdb/top.c b/gdb/top.c
index 72d19530070..be54d30be49 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1595,6 +1595,11 @@ This GDB was configured as follows:\n\
 	     --with-system-gdbinit-dir=%s%s\n\
 "), SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE ? " (relocatable)" : "");
 
+#ifdef SUPPORTED_FORMATS
+  gdb_printf (stream, _("\
+	     --enable-binary-file-formats=%s\n"), SUPPORTED_FORMATS);
+#endif
+
   /* We assume "relocatable" will be printed at least once, thus we always
      print this text.  It's a reasonably safe assumption for now.  */
   gdb_printf (stream, _("\n\

base-commit: 0a082f5cf8a5e42b7c947b377c117ec3451dcce8
-- 
2.50.0


^ permalink raw reply	[flat|nested] 25+ messages in thread

* [PING][PATCH v6] gdb, configure: Add disable-formats option for configure
  2025-07-07 20:16     ` [PATCH v6] " Guinevere Larsen
@ 2025-07-31 18:42       ` Guinevere Larsen
  2025-08-05 16:03       ` [PATCH " Simon Marchi
  2025-08-06 14:17       ` [PATCH v7 1/1] gdb, configure: Add enable-binary-file-format " Guinevere Larsen
  2 siblings, 0 replies; 25+ messages in thread
From: Guinevere Larsen @ 2025-07-31 18:42 UTC (permalink / raw)
  To: gdb-patches, guinevere; +Cc: Eli Zaretskii

Ping :)


-- 
Cheers,
Guinevere Larsen
She/Her/Hers

On 7/7/25 5:16 PM, Guinevere Larsen wrote:
> GDB has support for many binary file formats, some which might be very
> unlikely to be found in some situations (such as the XCOFF format in
> an x86 system). This commit introduces the option for a user to choose
> which formats GDB will support at build configuration time.
>
> This is especially useful to avoid possible security concerns with
> readers that aren't expected to be used at all, as they are one of
> the simplest vectors for an attacker to try and hit GDB with.  This
> change can also reduce the size of the final binary, if that is a
> concern.
>
> This commit adds a switch to the configure script allowing a user to
> only enable selected file formats, called --enable-binary-file-formats.
> The default behavior when the switch is omitted is to compile all file
> formats, keeping the original behavior of the script. At the time of
> this commit, the valid options for this option are: dbx, coff (which
> includes coff-pe), xcoff, mips, elf, macho and all. All is treated
> especially, activating all supported readers.
>
> A few targets may require specific binary file format support, as they
> directly call functions defined by the file reader. Specifically,
> windows targets require coff support, and rs6000 aix and lynx178 targets
> require xcoff support. Considering that those formats are the main - or
> only - one available in those targets, I believe it makes sense to
> re-enable those readers. If that happens, the script will emit the
> following warning:
>
>    FOO is required to support one or more requested targets. Adding it
>
> Users aren't able to force the disabling of those formats, since GDB
> will not compile without those readers. Ideally we'd like to be able
> to disable even those formats, in case a user wants to build GDB only
> to examine remote files for example, but the current infrastructure
> for the file format readers doesn't allow us to do it.
>
> Mach-O and elf support are also dependent on BFD support being compiled
> in.  In case one of those was requested and BFD does not support them,
> the following error is emitted:
>
>      FOO was requested, but BFD does not support it.
>
> Finally, this configure switch is also printed by the "show
> configuration" command in GDB.
>
> Reviewed-By: Eli Zaretskii <eliz@gnu.org>
> ---
>
> This is rebased on current master, there was a bit of a rebase conflict,
> but nothing complicated.
>
> ---
>   gdb/Makefile.in     | 20 ++++++----
>   gdb/NEWS            | 11 ++++++
>   gdb/README          | 24 ++++++++++++
>   gdb/config.in       |  3 ++
>   gdb/configure       | 96 +++++++++++++++++++++++++++++++++++++++++++--
>   gdb/configure.ac    | 80 ++++++++++++++++++++++++++++++++++++-
>   gdb/configure.tgt   | 20 ++++++++--
>   gdb/doc/gdb.texinfo |  8 ++++
>   gdb/top.c           |  5 +++
>   9 files changed, 250 insertions(+), 17 deletions(-)
>
> diff --git a/gdb/Makefile.in b/gdb/Makefile.in
> index fc0c56564c2..294c9e19421 100644
> --- a/gdb/Makefile.in
> +++ b/gdb/Makefile.in
> @@ -907,13 +907,22 @@ ALL_TARGET_OBS = \
>   	vax-tdep.o \
>   	windows-tdep.o \
>   	x86-tdep.o \
> -	xcoffread.o \
>   	xstormy16-tdep.o \
>   	xtensa-config.o \
>   	xtensa-linux-tdep.o \
>   	xtensa-tdep.o \
>   	z80-tdep.o
>   
> +# Object files for reading specific types of debug information.
> +coff_SRCS = coffread.c coff-pe-read.c
> +dbx_SRCS = dbxread.c
> +elf_SRCS = elfread.c
> +macho_SRCS = machoread.c
> +mips_SRCS = mipsread.c
> +xcoff_SRCS = xcoffread.c
> +FORMAT_SRCS = @FORMAT_SRCS@
> +FORMAT_OBS = $(patsubst %.c,%.o,$(FORMAT_SRCS))
> +
>   # The following native-target dependent variables are defined on
>   # configure.nat.
>   NAT_FILE = @NAT_FILE@
> @@ -1070,8 +1079,6 @@ COMMON_SFILES = \
>   	c-varobj.c \
>   	charset.c \
>   	cli-out.c \
> -	coff-pe-read.c \
> -	coffread.c \
>   	complaints.c \
>   	completer.c \
>   	copying.c \
> @@ -1085,7 +1092,6 @@ COMMON_SFILES = \
>   	d-lang.c \
>   	d-namespace.c \
>   	d-valprint.c \
> -	dbxread.c \
>   	dcache.c \
>   	debug.c \
>   	debuginfod-support.c \
> @@ -1150,7 +1156,6 @@ COMMON_SFILES = \
>   	memtag.c \
>   	minidebug.c \
>   	minsyms.c \
> -	mipsread.c \
>   	namespace.c \
>   	objc-lang.c \
>   	objfiles.c \
> @@ -1243,7 +1248,6 @@ SFILES = \
>   	d-exp.y \
>   	dtrace-probe.c \
>   	elf-none-tdep.c \
> -	elfread.c \
>   	f-exp.y \
>   	gcore-elf.c \
>   	gdb.c \
> @@ -1870,7 +1874,6 @@ ALLDEPFILES = \
>   	x86-gnu-nat.c \
>   	x86-nat.c \
>   	x86-tdep.c \
> -	xcoffread.c \
>   	xstormy16-tdep.c \
>   	xtensa-config.c \
>   	xtensa-linux-nat.c \
> @@ -1925,7 +1928,8 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
>   	$(patsubst %.c,%.o,$(COMMON_SFILES)) \
>   	$(SUBDIR_CLI_OBS) \
>   	$(SUBDIR_MI_OBS) \
> -	$(SUBDIR_TARGET_OBS)
> +	$(SUBDIR_TARGET_OBS) \
> +	$(FORMAT_OBS)
>   
>   SUBDIRS = doc @subdirs@ data-directory
>   CLEANDIRS = $(SUBDIRS)
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 4c9aed421bb..a8c17318d7e 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -224,6 +224,17 @@ vFile:stat
>   * Support for stabs debugging format and the a.out/dbx object format is
>     deprecated, and will be removed in GDB 18.
>   
> +* Configure changes
> +
> +--enable-binary-file-formats=[FORMAT,...]
> +--enable-binary-file-formats=all
> +  A user can now decide to only compile support for certain file formats.
> +  The available formats at this point are: dbx, coff, xcoff, elf, mach-o
> +  and mips.  Some targets require specific file formats to be available,
> +  and in such cases, the configure script will warn the user and add
> +  support anyway.  By default, all formats will be compiled in, to
> +  continue the behavior from before adding the switch.
> +
>   * A new configure option was added, allowing support for the compile
>     subsystem to be disabled at configure time, in the form of
>     --disable-gdb-compile.
> diff --git a/gdb/README b/gdb/README
> index eca4181c751..d45e6d37c4a 100644
> --- a/gdb/README
> +++ b/gdb/README
> @@ -417,6 +417,30 @@ more obscure GDB `configure' options are not listed here.
>        There is no convenient way to generate a list of all available
>        targets.
>   
> +`--enable-binary-file-formats=FORMAT,FORMAT,...'
> +`--enable-binary-file-formats=all'
> +    Configure GDB to only be be able to read selected file formats.
> +    The special value "all" causes all file formats to be compiled
> +    in, and is the the default behavior of the option.  This option
> +    is meant for advanced users who are sure of what they expect,
> +    if you are unsure which options you will need on your debugging
> +    sessions, we recommend that you not use this feature.  The
> +    accepted options are:
> +      * coff: Main format on windows systems. This is required to
> +        compile with windows target support;
> +      * dbx (also known as a.out): is a legacy file format, this
> +        is recommended if you know you will be dealing with this
> +        file format;
> +      * elf: Main format of linux systems. This is heavily
> +        recommended when compiling with linux support;
> +      * macho: Main format on MacOS systems, this is heavily
> +        recommended when compiling for those targets;
> +      * mips (also known as ecoff): this is the main file format
> +        for targets running on MIPS CPUs. It is heavily recommended
> +        when supporting those targets.
> +      * xcoff: Main format of AIX systems, this is required to
> +        compile for AIX targets and rs6000 CPUs.
> +
>   `--with-gdb-datadir=PATH'
>        Set the GDB-specific data directory.  GDB will look here for
>        certain supporting files or scripts.  This defaults to the `gdb'
> diff --git a/gdb/config.in b/gdb/config.in
> index 149aeaf979b..9208b21946e 100644
> --- a/gdb/config.in
> +++ b/gdb/config.in
> @@ -745,6 +745,9 @@
>   /* Define to 1 if you have the ANSI C header files. */
>   #undef STDC_HEADERS
>   
> +/* Which file formats were requested at configure time. */
> +#undef SUPPORTED_FORMATS
> +
>   /* automatically load a system-wide gdbinit file */
>   #undef SYSTEM_GDBINIT
>   
> diff --git a/gdb/configure b/gdb/configure
> index 8fc3b04efbf..c8e22e0eabc 100755
> --- a/gdb/configure
> +++ b/gdb/configure
> @@ -706,6 +706,7 @@ LIBGUI
>   LTLIBLZMA
>   LIBLZMA
>   HAVE_LIBLZMA
> +FORMAT_SRCS
>   SER_HARDWIRE
>   WERROR_CFLAGS
>   WARN_CFLAGS
> @@ -932,6 +933,7 @@ with_relocated_sources
>   with_auto_load_dir
>   with_auto_load_safe_path
>   enable_targets
> +enable_binary_file_formats
>   enable_gdb_mdebug_support
>   enable_gdb_dwarf_support
>   with_amd_dbgapi
> @@ -1645,6 +1647,9 @@ Optional Features:
>     --disable-nls           do not use Native Language Support
>     --enable-targets=TARGETS
>                             alternative target configurations
> +  --enable-binary-file-formats=FILE_FORMATS
> +                          enable support for selected file formats (default
> +                          'all')
>     --enable-gdb-mdebug-support
>                             Enable support for the mdebug debuginfo format
>                             (default 'yes')
> @@ -11507,7 +11512,7 @@ else
>     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
>     lt_status=$lt_dlunknown
>     cat > conftest.$ac_ext <<_LT_EOF
> -#line 11510 "configure"
> +#line 11515 "configure"
>   #include "confdefs.h"
>   
>   #if HAVE_DLFCN_H
> @@ -11613,7 +11618,7 @@ else
>     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
>     lt_status=$lt_dlunknown
>     cat > conftest.$ac_ext <<_LT_EOF
> -#line 11616 "configure"
> +#line 11621 "configure"
>   #include "confdefs.h"
>   
>   #if HAVE_DLFCN_H
> @@ -24882,6 +24887,18 @@ esac
>   fi
>   
>   
> +# Check whether --enable-binary_file_formats was given.
> +if test "${enable_binary_file_formats+set}" = set; then :
> +  enableval=$enable_binary_file_formats; case "${enableval}" in
> + yes | "") as_fn_error $? "enable-formats option must specify file formats or 'all'" "$LINENO" 5
> +            ;;
> +  no)       enable_binary_file_formats= ;;
> +  *)        enable_binary_file_formats=$enableval ;;
> +esac
> +else
> +  enable_binary_file_formats=all
> +fi
> +
>   
>   # Check whether to support mdebug/ecoff debug information.
>   # Check whether --enable-gdb-mdebug-support was given.
> @@ -24985,11 +25002,20 @@ fi
>   TARGET_OBS=
>   all_targets=
>   HAVE_NATIVE_GCORE_TARGET=
> +# File formats that will be enabled based on the selected
> +# target(s).  These are chosen because they are required to
> +# compile one or more of the selected targets.
> +target_formats=
> +# If all targets were requested, this is all formats that should
> +# accompany them.  These are just the ones required for compilation
> +# to succeed, not the formats suggested based on targets.
> +all_target_formats="coff xcoff"
>   
>   for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
>   do
>     if test "$targ_alias" = "all"; then
>       all_targets=true
> +    target_formats=$all_target_formats
>     else
>       # Canonicalize the secondary target names.
>       result=`$ac_config_sub $targ_alias 2>/dev/null`
> @@ -31628,6 +31654,12 @@ fi
>   # Note that WIN32APILIBS is set by GDB_AC_COMMON.
>   WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
>   
> +# Object files to be used when building with support for all file formats.
> +# This should not have elf or macho, as support for those formats depends
> +# on BFD enabling them as well.
> +all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
> +
> +support_elf=no
>   # Add ELF support to GDB, but only if BFD includes ELF support.
>   
>     OLD_CFLAGS=$CFLAGS
> @@ -31680,7 +31712,7 @@ $as_echo "$gdb_cv_var_elf" >&6; }
>     LDFLAGS=$OLD_LDFLAGS
>     LIBS=$OLD_LIBS
>   if test "$gdb_cv_var_elf" = yes; then
> -  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
> +  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
>   		gcore-elf.o elf-none-tdep.o"
>   
>   $as_echo "#define HAVE_ELF 1" >>confdefs.h
> @@ -31744,9 +31776,12 @@ if test "$ac_res" != no; then :
>   fi
>   
>     fi
> +  support_elf=yes
> +  all_binary_file_srcs="$all_binary_file_srcs \$(elf_SRCS)"
>   fi
>   
>   # Add macho support to GDB, but only if BFD includes it.
> +support_macho=no
>   
>     OLD_CFLAGS=$CFLAGS
>     OLD_LDFLAGS=$LDFLAGS
> @@ -31798,9 +31833,62 @@ $as_echo "$gdb_cv_var_macho" >&6; }
>     LDFLAGS=$OLD_LDFLAGS
>     LIBS=$OLD_LIBS
>   if test "$gdb_cv_var_macho" = yes; then
> -  CONFIG_OBS="$CONFIG_OBS machoread.o"
> +  support_macho=yes
> +  all_binary_file_srcs="$all_binary_file_srcs \$(macho_SRCS)"
>   fi
>   
> +FORMAT_SRCS=
> +
> +if test "$enable_binary_file_formats" != "all"; then
> +    # Formats that are required by some requested target(s).
> +    # Warn users that they are added, so no one is surprised.
> +    for req in $target_formats; do
> +	case ,$enable_binary_file_formats, in
> +	*,$req,*)
> +	    # Do nothing.
> +	    ;;
> +	*)
> +	    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&5
> +$as_echo "$as_me: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&2;}
> +	    enable_binary_file_formats="${enable_binary_file_formats},$req"
> +	    ;;
> +	esac
> +    done
> +
> +
> +cat >>confdefs.h <<_ACEOF
> +#define SUPPORTED_FORMATS "$enable_binary_file_formats"
> +_ACEOF
> +
> +fi
> +
> +enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
> +
> +for format in $enable_binary_file_formats
> +do
> +    if test "$format" = "elf"; then
> +	if test "$support_elf" != "yes"; then
> +	    as_fn_error but BFD does not support it." "\"elf support was requested" "$LINENO" 5;
> +	fi
> +    elif test "$format" = "macho"; then
> +	if test "$support_macho" != "yes"; then
> +	    as_fn_error but BFD does not support it." "\"Mach-O support was requested" "$LINENO" 5;
> +	fi
> +    fi
> +
> +    if test "$format" = "all"; then
> +	FORMAT_SRCS="$all_binary_file_srcs"
> +	# We don't break here in case the user requested Mach-O or ELF, but
> +	# BFD is not configured to support it.  If we were to break, we would
> +	# silently drop the requested support instead of erroring out.
> +    else
> +	fmt=$(echo "$format _SRCS" | sed 's/ //')
> +	FORMAT_SRCS="$FORMAT_SRCS \$($fmt)"
> +    fi
> +done
> +
> +
> +
>   # Add any host-specific objects to GDB.
>   CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
>   
> diff --git a/gdb/configure.ac b/gdb/configure.ac
> index 226e27e4fe5..edc6102c6da 100644
> --- a/gdb/configure.ac
> +++ b/gdb/configure.ac
> @@ -191,6 +191,15 @@ AS_HELP_STRING([--enable-targets=TARGETS], [alternative target configurations]),
>   	;;
>   esac])
>   
> +AC_ARG_ENABLE(binary_file_formats,
> +	      AS_HELP_STRING([--enable-binary-file-formats=FILE_FORMATS],
> +			     [enable support for selected file formats (default 'all')]),
> +[case "${enableval}" in
> + yes | "") AC_MSG_ERROR(enable-formats option must specify file formats or 'all')
> +            ;;
> +  no)       enable_binary_file_formats= ;;
> +  *)        enable_binary_file_formats=$enableval ;;
> +esac], [enable_binary_file_formats=all])
>   
>   # Check whether to support mdebug/ecoff debug information.
>   AC_ARG_ENABLE(gdb-mdebug-support,
> @@ -239,11 +248,20 @@ fi
>   TARGET_OBS=
>   all_targets=
>   HAVE_NATIVE_GCORE_TARGET=
> +# File formats that will be enabled based on the selected
> +# target(s).  These are chosen because they are required to
> +# compile one or more of the selected targets.
> +target_formats=
> +# If all targets were requested, this is all formats that should
> +# accompany them.  These are just the ones required for compilation
> +# to succeed, not the formats suggested based on targets.
> +all_target_formats="coff xcoff"
>   
>   for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
>   do
>     if test "$targ_alias" = "all"; then
>       all_targets=true
> +    target_formats=$all_target_formats
>     else
>       # Canonicalize the secondary target names.
>       result=`$ac_config_sub $targ_alias 2>/dev/null`
> @@ -1952,11 +1970,17 @@ fi
>   # Note that WIN32APILIBS is set by GDB_AC_COMMON.
>   WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
>   
> +# Object files to be used when building with support for all file formats.
> +# This should not have elf or macho, as support for those formats depends
> +# on BFD enabling them as well.
> +all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
> +
> +support_elf=no
>   # Add ELF support to GDB, but only if BFD includes ELF support.
>   GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_elf,
>                    [bfd_get_elf_phdr_upper_bound (NULL)], elf-bfd.h)
>   if test "$gdb_cv_var_elf" = yes; then
> -  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
> +  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
>   		gcore-elf.o elf-none-tdep.o"
>     AC_DEFINE(HAVE_ELF, 1,
>   	    [Define if ELF support should be included.])
> @@ -1964,15 +1988,67 @@ if test "$gdb_cv_var_elf" = yes; then
>     if test "$plugins" = "yes"; then
>       AC_SEARCH_LIBS(dlopen, dl)
>     fi
> +  support_elf=yes
> +  all_binary_file_srcs="$all_binary_file_srcs \$(elf_SRCS)"
>   fi
>   
>   # Add macho support to GDB, but only if BFD includes it.
> +support_macho=no
>   GDB_AC_CHECK_BFD([for Mach-O support in BFD], gdb_cv_var_macho,
>                    [bfd_mach_o_lookup_command (NULL, 0, NULL)], mach-o.h)
>   if test "$gdb_cv_var_macho" = yes; then
> -  CONFIG_OBS="$CONFIG_OBS machoread.o"
> +  support_macho=yes
> +  all_binary_file_srcs="$all_binary_file_srcs \$(macho_SRCS)"
> +fi
> +
> +FORMAT_SRCS=
> +
> +if test "$enable_binary_file_formats" != "all"; then
> +    # Formats that are required by some requested target(s).
> +    # Warn users that they are added, so no one is surprised.
> +    for req in $target_formats; do
> +	case ,$enable_binary_file_formats, in
> +	*,$req,*)
> +	    # Do nothing.
> +	    ;;
> +	*)
> +	    AC_MSG_WARN("$req is required to support one or more requested targets.  Adding it")
> +	    enable_binary_file_formats="${enable_binary_file_formats},$req"
> +	    ;;
> +	esac
> +    done
> +
> +    AC_DEFINE_UNQUOTED(SUPPORTED_FORMATS, "$enable_binary_file_formats",
> +		       Which file formats were requested at configure time. )
>   fi
>   
> +enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
> +
> +for format in $enable_binary_file_formats
> +do
> +    if test "$format" = "elf"; then
> +	if test "$support_elf" != "yes"; then
> +	    AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
> +	fi
> +    elif test "$format" = "macho"; then
> +	if test "$support_macho" != "yes"; then
> +	    AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
> +	fi
> +    fi
> +
> +    if test "$format" = "all"; then
> +	FORMAT_SRCS="$all_binary_file_srcs"
> +	# We don't break here in case the user requested Mach-O or ELF, but
> +	# BFD is not configured to support it.  If we were to break, we would
> +	# silently drop the requested support instead of erroring out.
> +    else
> +	fmt=$(echo "$format _SRCS" | sed 's/ //')
> +	FORMAT_SRCS="$FORMAT_SRCS \$($fmt)"
> +    fi
> +done
> +
> +AC_SUBST(FORMAT_SRCS)
> +
>   # Add any host-specific objects to GDB.
>   CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
>   
> diff --git a/gdb/configure.tgt b/gdb/configure.tgt
> index 255c77e9f8c..332f5b7513c 100644
> --- a/gdb/configure.tgt
> +++ b/gdb/configure.tgt
> @@ -505,7 +505,7 @@ powerpc-*-openbsd*)
>   	;;
>   powerpc-*-aix* | rs6000-*-* | powerpc64-*-aix*)
>   	# Target: PowerPC running AIX
> -	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o xcoffread.o \
> +	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o \
>   			ppc-sysv-tdep.o solib-aix.o \
>   			ravenscar-thread.o ppc-ravenscar-thread.o"
>   	;;
> @@ -522,8 +522,8 @@ powerpc*-*-linux*)
>   powerpc-*-lynx*178)
>   	# Target: PowerPC running Lynx178.
>   	gdb_target_obs="rs6000-tdep.o rs6000-lynx178-tdep.o \
> -			xcoffread.o ppc-sysv-tdep.o \
> -			ravenscar-thread.o ppc-ravenscar-thread.o"
> +			ppc-sysv-tdep.o ravenscar-thread.o \
> +			ppc-ravenscar-thread.o"
>   	;;
>   powerpc*-*-*)
>   	# Target: PowerPC running eabi
> @@ -835,3 +835,17 @@ for t in x ${gdb_target_obs}; do
>       gdb_have_gcore=true
>     fi
>   done
> +
> +# Decide which file formats are absolutely required based on
> +# the requested targets.  Warn later that they are added, in
> +# case the user didn't manually request them, or all readers.
> +# It's fine to add the same format multiple times since the
> +# loop that reads the options to FORMAT_OBS will ensure that
> +# they are only added once.
> +for i in $gdb_target_obs; do
> +    case "${i}" in
> +    *"windows-tdep.o" ) target_formats="${target_formats} coff";;
> +    "rs6000-aix-tdep.o" ) target_formats="${target_formats} xcoff";;
> +    "rs6000-lynx178-tdep.o" ) target_formats="${target_formats} xcoff";;
> +    esac
> +done
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index 35b770f8138..bcd88b7b6a5 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -41267,6 +41267,14 @@ Configure @value{GDBN} for cross-debugging programs running on the
>   specified list of targets.  The special value @samp{all} configures
>   @value{GDBN} for debugging programs running on any target it supports.
>   
> +@item --enable-binary-file-formats=@r{[}@var{format}@r{]}@dots{}
> +@itemx --enable-binary-file-formats=all
> +Configure @value{GDBN} to support certain binary file formats.  If a
> +format is the main (or only) file format for a given target, the
> +configure script may add support to it anyway, and warn the user.
> +If not given, all file formats that @value{GDBN} supports are compiled
> +in.
> +
>   @item --with-gdb-datadir=@var{path}
>   Set the @value{GDBN}-specific data directory.  @value{GDBN} will look
>   here for certain supporting files or scripts.  This defaults to the
> diff --git a/gdb/top.c b/gdb/top.c
> index 72d19530070..be54d30be49 100644
> --- a/gdb/top.c
> +++ b/gdb/top.c
> @@ -1595,6 +1595,11 @@ This GDB was configured as follows:\n\
>   	     --with-system-gdbinit-dir=%s%s\n\
>   "), SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE ? " (relocatable)" : "");
>   
> +#ifdef SUPPORTED_FORMATS
> +  gdb_printf (stream, _("\
> +	     --enable-binary-file-formats=%s\n"), SUPPORTED_FORMATS);
> +#endif
> +
>     /* We assume "relocatable" will be printed at least once, thus we always
>        print this text.  It's a reasonably safe assumption for now.  */
>     gdb_printf (stream, _("\n\
>
> base-commit: 0a082f5cf8a5e42b7c947b377c117ec3451dcce8


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v6] gdb, configure: Add disable-formats option for configure
  2025-07-07 20:16     ` [PATCH v6] " Guinevere Larsen
  2025-07-31 18:42       ` [PING][PATCH " Guinevere Larsen
@ 2025-08-05 16:03       ` Simon Marchi
  2025-08-05 19:06         ` Eli Zaretskii
  2025-08-05 20:07         ` Guinevere Larsen
  2025-08-06 14:17       ` [PATCH v7 1/1] gdb, configure: Add enable-binary-file-format " Guinevere Larsen
  2 siblings, 2 replies; 25+ messages in thread
From: Simon Marchi @ 2025-08-05 16:03 UTC (permalink / raw)
  To: Guinevere Larsen, gdb-patches; +Cc: Eli Zaretskii

On 7/7/25 4:16 PM, Guinevere Larsen wrote:
> GDB has support for many binary file formats, some which might be very
> unlikely to be found in some situations (such as the XCOFF format in
> an x86 system). This commit introduces the option for a user to choose
> which formats GDB will support at build configuration time.
> 
> This is especially useful to avoid possible security concerns with
> readers that aren't expected to be used at all, as they are one of
> the simplest vectors for an attacker to try and hit GDB with.  This
> change can also reduce the size of the final binary, if that is a
> concern.
> 
> This commit adds a switch to the configure script allowing a user to
> only enable selected file formats, called --enable-binary-file-formats.
> The default behavior when the switch is omitted is to compile all file
> formats, keeping the original behavior of the script. At the time of
> this commit, the valid options for this option are: dbx, coff (which
> includes coff-pe), xcoff, mips, elf, macho and all. All is treated
> especially, activating all supported readers.
> 
> A few targets may require specific binary file format support, as they
> directly call functions defined by the file reader. Specifically,
> windows targets require coff support, and rs6000 aix and lynx178 targets
> require xcoff support. Considering that those formats are the main - or
> only - one available in those targets, I believe it makes sense to
> re-enable those readers. If that happens, the script will emit the
> following warning:
> 
>   FOO is required to support one or more requested targets. Adding it
> 
> Users aren't able to force the disabling of those formats, since GDB
> will not compile without those readers. Ideally we'd like to be able
> to disable even those formats, in case a user wants to build GDB only
> to examine remote files for example, but the current infrastructure
> for the file format readers doesn't allow us to do it.
> 
> Mach-O and elf support are also dependent on BFD support being compiled
> in.  In case one of those was requested and BFD does not support them,
> the following error is emitted:
> 
>     FOO was requested, but BFD does not support it.
> 
> Finally, this configure switch is also printed by the "show
> configuration" command in GDB.
> 
> Reviewed-By: Eli Zaretskii <eliz@gnu.org>
> ---
> 
> This is rebased on current master, there was a bit of a rebase conflict,
> but nothing complicated.

I think the git commit subject needs to be updated to reflect the new
option name.

> diff --git a/gdb/README b/gdb/README
> index eca4181c751..d45e6d37c4a 100644
> --- a/gdb/README
> +++ b/gdb/README
> @@ -417,6 +417,30 @@ more obscure GDB `configure' options are not listed here.
>       There is no convenient way to generate a list of all available
>       targets.
>  
> +`--enable-binary-file-formats=FORMAT,FORMAT,...'
> +`--enable-binary-file-formats=all'
> +    Configure GDB to only be be able to read selected file formats.
> +    The special value "all" causes all file formats to be compiled
> +    in, and is the the default behavior of the option.  This option
> +    is meant for advanced users who are sure of what they expect,
> +    if you are unsure which options you will need on your debugging
> +    sessions, we recommend that you not use this feature.  The
> +    accepted options are:

In the list below, a few instances of "two spaces after period".

> +      * coff: Main format on windows systems. This is required to
> +        compile with windows target support;

windows -> Windows

> +      * dbx (also known as a.out): is a legacy file format, this
> +        is recommended if you know you will be dealing with this
> +        file format;

Missing "This" at the beginning, to be like the other entries.

> +      * elf: Main format of linux systems. This is heavily
> +        recommended when compiling with linux support;

linux -> Linux

For consistency: in all entries except this one, the "This is heavily"
is just a phrase following a comma.  I would change this one to match.
In general if you can make the form consistent across items, it would be
nice.

> +      * macho: Main format on MacOS systems, this is heavily
> +        recommended when compiling for those targets;

MacOS -> macOS

> @@ -191,6 +191,15 @@ AS_HELP_STRING([--enable-targets=TARGETS], [alternative target configurations]),
>  	;;
>  esac])
>  
> +AC_ARG_ENABLE(binary_file_formats,
> +	      AS_HELP_STRING([--enable-binary-file-formats=FILE_FORMATS],

Question mostly for Eli, should the metavar use an underscore or dash
("FILE-FORMATS")?

> +			     [enable support for selected file formats (default 'all')]),

Since the list of valide values is finite and not too long, it might be
nice to list the possible values right there in the help message.

> +[case "${enableval}" in
> + yes | "") AC_MSG_ERROR(enable-formats option must specify file formats or 'all')

enable-formats -> enable-binary-file-formats

I think you are also missing a space before "yes".

> @@ -239,11 +248,20 @@ fi
>  TARGET_OBS=
>  all_targets=
>  HAVE_NATIVE_GCORE_TARGET=
> +# File formats that will be enabled based on the selected
> +# target(s).  These are chosen because they are required to
> +# compile one or more of the selected targets.
> +target_formats=
> +# If all targets were requested, this is all formats that should
> +# accompany them.  These are just the ones required for compilation
> +# to succeed, not the formats suggested based on targets.
> +all_target_formats="coff xcoff"

Please add empty lines before each comment above.

> @@ -1952,11 +1970,17 @@ fi
>  # Note that WIN32APILIBS is set by GDB_AC_COMMON.
>  WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
>  
> +# Object files to be used when building with support for all file formats.
> +# This should not have elf or macho, as support for those formats depends
> +# on BFD enabling them as well.
> +all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
> +
> +support_elf=no
>  # Add ELF support to GDB, but only if BFD includes ELF support.
>  GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_elf,
>                   [bfd_get_elf_phdr_upper_bound (NULL)], elf-bfd.h)
>  if test "$gdb_cv_var_elf" = yes; then
> -  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
> +  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
>  		gcore-elf.o elf-none-tdep.o"

Does it make sense (or is it useful) to compile these other files (other
than elfread.o) if ELF support is not enabled?  Could we just list them
all in elf_SRCS so that they are not built if the user doesn't want ELF
support?  If you don't have an ELF reader.

If I understand correctly, "support_elf" means that BFD supports ELF,
not that GDB will support ELF, right?  If so, could you rename it to
"bfd_supports_elf"?

I also wouldn't mind if you renamed the conf var gdb_cv_var_elf to
something like gdb_cv_var_bfd_elf.

>    AC_DEFINE(HAVE_ELF, 1,
>  	    [Define if ELF support should be included.])
> @@ -1964,15 +1988,67 @@ if test "$gdb_cv_var_elf" = yes; then
>    if test "$plugins" = "yes"; then
>      AC_SEARCH_LIBS(dlopen, dl)
>    fi
> +  support_elf=yes
> +  all_binary_file_srcs="$all_binary_file_srcs \$(elf_SRCS)"
>  fi
>  
>  # Add macho support to GDB, but only if BFD includes it.
> +support_macho=no

Likewise for support_macho.

>  GDB_AC_CHECK_BFD([for Mach-O support in BFD], gdb_cv_var_macho,
>                   [bfd_mach_o_lookup_command (NULL, 0, NULL)], mach-o.h)
>  if test "$gdb_cv_var_macho" = yes; then
> -  CONFIG_OBS="$CONFIG_OBS machoread.o"
> +  support_macho=yes
> +  all_binary_file_srcs="$all_binary_file_srcs \$(macho_SRCS)"
> +fi
> +
> +FORMAT_SRCS=
> +
> +if test "$enable_binary_file_formats" != "all"; then
> +    # Formats that are required by some requested target(s).
> +    # Warn users that they are added, so no one is surprised.
> +    for req in $target_formats; do
> +	case ,$enable_binary_file_formats, in
> +	*,$req,*)
> +	    # Do nothing.
> +	    ;;

Does this work if $req is first or last in the list?

> +	*)
> +	    AC_MSG_WARN("$req is required to support one or more requested targets.  Adding it")
> +	    enable_binary_file_formats="${enable_binary_file_formats},$req"
> +	    ;;
> +	esac
> +    done
> +
> +    AC_DEFINE_UNQUOTED(SUPPORTED_FORMATS, "$enable_binary_file_formats",
> +		       Which file formats were requested at configure time. )

I think that all variables related to this should be renamed to include
"binary file".  For instance, SUPPORTED_FORMATS ->
SUPPORTED_BINARY_FILE_FORMATS.  There are a few others in the configure
script or Makefile.

Also, "Which file formats" -> "Which binary file formats".

>  fi
>  
> +enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
> +
> +for format in $enable_binary_file_formats
> +do

Can you write a comment above that loop to indicate what it does?

> +    if test "$format" = "elf"; then
> +	if test "$support_elf" != "yes"; then
> +	    AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
> +	fi
> +    elif test "$format" = "macho"; then
> +	if test "$support_macho" != "yes"; then
> +	    AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
> +	fi
> +    fi

Can you write the above as

    if test "$format" = "elf" && "$support_elf" != "yes"; then
	AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
    fi

    if test "$format" = "macho" && "$support_macho" != "yes"; then
	AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
    fi

?  I find it a bit easier to read (less boilerplate).

Simon

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v6] gdb, configure: Add disable-formats option for configure
  2025-08-05 16:03       ` [PATCH " Simon Marchi
@ 2025-08-05 19:06         ` Eli Zaretskii
  2025-08-05 19:36           ` Simon Marchi
  2025-08-05 20:07         ` Guinevere Larsen
  1 sibling, 1 reply; 25+ messages in thread
From: Eli Zaretskii @ 2025-08-05 19:06 UTC (permalink / raw)
  To: Simon Marchi; +Cc: guinevere, gdb-patches

> Date: Tue, 5 Aug 2025 12:03:29 -0400
> Cc: Eli Zaretskii <eliz@gnu.org>
> From: Simon Marchi <simark@simark.ca>
> 
> > +AC_ARG_ENABLE(binary_file_formats,
> > +	      AS_HELP_STRING([--enable-binary-file-formats=FILE_FORMATS],
> 
> Question mostly for Eli, should the metavar use an underscore or dash
> ("FILE-FORMATS")?

I'd just use FORMATS and be done with that.  But if we must have 2
words, then underscore is better here.

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v6] gdb, configure: Add disable-formats option for configure
  2025-08-05 19:06         ` Eli Zaretskii
@ 2025-08-05 19:36           ` Simon Marchi
  0 siblings, 0 replies; 25+ messages in thread
From: Simon Marchi @ 2025-08-05 19:36 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: guinevere, gdb-patches

On 8/5/25 3:06 PM, Eli Zaretskii wrote:
>> Date: Tue, 5 Aug 2025 12:03:29 -0400
>> Cc: Eli Zaretskii <eliz@gnu.org>
>> From: Simon Marchi <simark@simark.ca>
>>
>>> +AC_ARG_ENABLE(binary_file_formats,
>>> +	      AS_HELP_STRING([--enable-binary-file-formats=FILE_FORMATS],
>>
>> Question mostly for Eli, should the metavar use an underscore or dash
>> ("FILE-FORMATS")?
> 
> I'd just use FORMATS and be done with that.  But if we must have 2
> words, then underscore is better here.

I agree that just FORMATS is good.  The option name makes it clear what
it is.

Simon

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v6] gdb, configure: Add disable-formats option for configure
  2025-08-05 16:03       ` [PATCH " Simon Marchi
  2025-08-05 19:06         ` Eli Zaretskii
@ 2025-08-05 20:07         ` Guinevere Larsen
  2025-08-06 13:53           ` Guinevere Larsen
  1 sibling, 1 reply; 25+ messages in thread
From: Guinevere Larsen @ 2025-08-05 20:07 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Eli Zaretskii

On 8/5/25 1:03 PM, Simon Marchi wrote:
> On 7/7/25 4:16 PM, Guinevere Larsen wrote:
>> GDB has support for many binary file formats, some which might be very
>> unlikely to be found in some situations (such as the XCOFF format in
>> an x86 system). This commit introduces the option for a user to choose
>> which formats GDB will support at build configuration time.
>>
>> This is especially useful to avoid possible security concerns with
>> readers that aren't expected to be used at all, as they are one of
>> the simplest vectors for an attacker to try and hit GDB with.  This
>> change can also reduce the size of the final binary, if that is a
>> concern.
>>
>> This commit adds a switch to the configure script allowing a user to
>> only enable selected file formats, called --enable-binary-file-formats.
>> The default behavior when the switch is omitted is to compile all file
>> formats, keeping the original behavior of the script. At the time of
>> this commit, the valid options for this option are: dbx, coff (which
>> includes coff-pe), xcoff, mips, elf, macho and all. All is treated
>> especially, activating all supported readers.
>>
>> A few targets may require specific binary file format support, as they
>> directly call functions defined by the file reader. Specifically,
>> windows targets require coff support, and rs6000 aix and lynx178 targets
>> require xcoff support. Considering that those formats are the main - or
>> only - one available in those targets, I believe it makes sense to
>> re-enable those readers. If that happens, the script will emit the
>> following warning:
>>
>>    FOO is required to support one or more requested targets. Adding it
>>
>> Users aren't able to force the disabling of those formats, since GDB
>> will not compile without those readers. Ideally we'd like to be able
>> to disable even those formats, in case a user wants to build GDB only
>> to examine remote files for example, but the current infrastructure
>> for the file format readers doesn't allow us to do it.
>>
>> Mach-O and elf support are also dependent on BFD support being compiled
>> in.  In case one of those was requested and BFD does not support them,
>> the following error is emitted:
>>
>>      FOO was requested, but BFD does not support it.
>>
>> Finally, this configure switch is also printed by the "show
>> configuration" command in GDB.
>>
>> Reviewed-By: Eli Zaretskii <eliz@gnu.org>
>> ---
>>
>> This is rebased on current master, there was a bit of a rebase conflict,
>> but nothing complicated.
> I think the git commit subject needs to be updated to reflect the new
> option name.
Fixed
>
>> diff --git a/gdb/README b/gdb/README
>> index eca4181c751..d45e6d37c4a 100644
>> --- a/gdb/README
>> +++ b/gdb/README
>> @@ -417,6 +417,30 @@ more obscure GDB `configure' options are not listed here.
>>        There is no convenient way to generate a list of all available
>>        targets.
>>   
>> +`--enable-binary-file-formats=FORMAT,FORMAT,...'
>> +`--enable-binary-file-formats=all'
>> +    Configure GDB to only be be able to read selected file formats.
>> +    The special value "all" causes all file formats to be compiled
>> +    in, and is the the default behavior of the option.  This option
>> +    is meant for advanced users who are sure of what they expect,
>> +    if you are unsure which options you will need on your debugging
>> +    sessions, we recommend that you not use this feature.  The
>> +    accepted options are:
> In the list below, a few instances of "two spaces after period".
For consistency, I ended up changing all periods to commas, so this 
ended up being a non-issue
>
>> +      * coff: Main format on windows systems. This is required to
>> +        compile with windows target support;
> windows -> Windows
>
>> +      * dbx (also known as a.out): is a legacy file format, this
>> +        is recommended if you know you will be dealing with this
>> +        file format;
> Missing "This" at the beginning, to be like the other entries.

For consistency, I actually changed this to just say

"Legacy file format, this is (...)"

Since the other options will all read "Main format on (...)".

>
>> +      * elf: Main format of linux systems. This is heavily
>> +        recommended when compiling with linux support;
> linux -> Linux
>
> For consistency: in all entries except this one, the "This is heavily"
> is just a phrase following a comma.  I would change this one to match.
> In general if you can make the form consistent across items, it would be
> nice.
>
>> +      * macho: Main format on MacOS systems, this is heavily
>> +        recommended when compiling for those targets;
> MacOS -> macOS
>
>> @@ -191,6 +191,15 @@ AS_HELP_STRING([--enable-targets=TARGETS], [alternative target configurations]),
>>   	;;
>>   esac])
>>   
>> +AC_ARG_ENABLE(binary_file_formats,
>> +	      AS_HELP_STRING([--enable-binary-file-formats=FILE_FORMATS],
> Question mostly for Eli, should the metavar use an underscore or dash
> ("FILE-FORMATS")?
I saw Eli's response, I'll change this to FORMATS.
>
>> +			     [enable support for selected file formats (default 'all')]),
> Since the list of valide values is finite and not too long, it might be
> nice to list the possible values right there in the help message.

done. This now reads as:

enable support for selected file formats (default 'all')
available formats: coff, dbx, elf, macho, mips, xcoff

>
>> +[case "${enableval}" in
>> + yes | "") AC_MSG_ERROR(enable-formats option must specify file formats or 'all')
> enable-formats -> enable-binary-file-formats
>
> I think you are also missing a space before "yes".
done both
>
>> @@ -239,11 +248,20 @@ fi
>>   TARGET_OBS=
>>   all_targets=
>>   HAVE_NATIVE_GCORE_TARGET=
>> +# File formats that will be enabled based on the selected
>> +# target(s).  These are chosen because they are required to
>> +# compile one or more of the selected targets.
>> +target_formats=
>> +# If all targets were requested, this is all formats that should
>> +# accompany them.  These are just the ones required for compilation
>> +# to succeed, not the formats suggested based on targets.
>> +all_target_formats="coff xcoff"
> Please add empty lines before each comment above.
done
>
>> @@ -1952,11 +1970,17 @@ fi
>>   # Note that WIN32APILIBS is set by GDB_AC_COMMON.
>>   WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
>>   
>> +# Object files to be used when building with support for all file formats.
>> +# This should not have elf or macho, as support for those formats depends
>> +# on BFD enabling them as well.
>> +all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
>> +
>> +support_elf=no
>>   # Add ELF support to GDB, but only if BFD includes ELF support.
>>   GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_elf,
>>                    [bfd_get_elf_phdr_upper_bound (NULL)], elf-bfd.h)
>>   if test "$gdb_cv_var_elf" = yes; then
>> -  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
>> +  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
>>   		gcore-elf.o elf-none-tdep.o"
> Does it make sense (or is it useful) to compile these other files (other
> than elfread.o) if ELF support is not enabled?  Could we just list them
> all in elf_SRCS so that they are not built if the user doesn't want ELF
> support?  If you don't have an ELF reader.

I don't know. I took the path of least changes to implementing this, 
made worse by me not having a convenient way to test a non-ELF target.

I'll try to find a way to test if those are necessary and update the 
patch if they aren't

> If I understand correctly, "support_elf" means that BFD supports ELF,
> not that GDB will support ELF, right?  If so, could you rename it to
> "bfd_supports_elf"?
yes, you're correct. I'll update the name
> I also wouldn't mind if you renamed the conf var gdb_cv_var_elf to
> something like gdb_cv_var_bfd_elf.
Sure, might as well.
>>     AC_DEFINE(HAVE_ELF, 1,
>>   	    [Define if ELF support should be included.])
>> @@ -1964,15 +1988,67 @@ if test "$gdb_cv_var_elf" = yes; then
>>     if test "$plugins" = "yes"; then
>>       AC_SEARCH_LIBS(dlopen, dl)
>>     fi
>> +  support_elf=yes
>> +  all_binary_file_srcs="$all_binary_file_srcs \$(elf_SRCS)"
>>   fi
>>   
>>   # Add macho support to GDB, but only if BFD includes it.
>> +support_macho=no
> Likewise for support_macho.
done
>
>>   GDB_AC_CHECK_BFD([for Mach-O support in BFD], gdb_cv_var_macho,
>>                    [bfd_mach_o_lookup_command (NULL, 0, NULL)], mach-o.h)
>>   if test "$gdb_cv_var_macho" = yes; then
>> -  CONFIG_OBS="$CONFIG_OBS machoread.o"
>> +  support_macho=yes
>> +  all_binary_file_srcs="$all_binary_file_srcs \$(macho_SRCS)"
>> +fi
>> +
>> +FORMAT_SRCS=
>> +
>> +if test "$enable_binary_file_formats" != "all"; then
>> +    # Formats that are required by some requested target(s).
>> +    # Warn users that they are added, so no one is surprised.
>> +    for req in $target_formats; do
>> +	case ,$enable_binary_file_formats, in
>> +	*,$req,*)
>> +	    # Do nothing.
>> +	    ;;
> Does this work if $req is first or last in the list?
yes, because the case checks for ",$enable_binary_file_formats,", so 
even if there is only one option the commas will be there.
>
>> +	*)
>> +	    AC_MSG_WARN("$req is required to support one or more requested targets.  Adding it")
>> +	    enable_binary_file_formats="${enable_binary_file_formats},$req"
>> +	    ;;
>> +	esac
>> +    done
>> +
>> +    AC_DEFINE_UNQUOTED(SUPPORTED_FORMATS, "$enable_binary_file_formats",
>> +		       Which file formats were requested at configure time. )
> I think that all variables related to this should be renamed to include
> "binary file".  For instance, SUPPORTED_FORMATS ->
> SUPPORTED_BINARY_FILE_FORMATS.  There are a few others in the configure
> script or Makefile.
>
> Also, "Which file formats" -> "Which binary file formats".
Alright, fixed.
>
>>   fi
>>   
>> +enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
>> +
>> +for format in $enable_binary_file_formats
>> +do
> Can you write a comment above that loop to indicate what it does?

sure, added a small comment like so:

     Go through all requested and required binary file formats to compile
     GDB, and double check that we can compile them.

>> +    if test "$format" = "elf"; then
>> +	if test "$support_elf" != "yes"; then
>> +	    AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
>> +	fi
>> +    elif test "$format" = "macho"; then
>> +	if test "$support_macho" != "yes"; then
>> +	    AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
>> +	fi
>> +    fi
> Can you write the above as
>
>      if test "$format" = "elf" && "$support_elf" != "yes"; then
> 	AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
>      fi
>
>      if test "$format" = "macho" && "$support_macho" != "yes"; then
> 	AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
>      fi
>
> ?  I find it a bit easier to read (less boilerplate).
Sure, no problem. I don't really know or enjoy bash, so I go with 
whatever is easiest or I already know, so I wasn't sure this was viable.
>
> Simon
>

-- 
Cheers,
Guinevere Larsen
She/it


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v6] gdb, configure: Add disable-formats option for configure
  2025-08-05 20:07         ` Guinevere Larsen
@ 2025-08-06 13:53           ` Guinevere Larsen
  0 siblings, 0 replies; 25+ messages in thread
From: Guinevere Larsen @ 2025-08-06 13:53 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Eli Zaretskii

[-- Attachment #1: Type: text/plain, Size: 1984 bytes --]

On 8/5/25 5:07 PM, Guinevere Larsen wrote:
>>> @@ -1952,11 +1970,17 @@ fi
>>>   # Note that WIN32APILIBS is set by GDB_AC_COMMON.
>>>   WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
>>>   +# Object files to be used when building with support for all file 
>>> formats.
>>> +# This should not have elf or macho, as support for those formats 
>>> depends
>>> +# on BFD enabling them as well.
>>> +all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) 
>>> \$(xcoff_SRCS)"
>>> +
>>> +support_elf=no
>>>   # Add ELF support to GDB, but only if BFD includes ELF support.
>>>   GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_elf,
>>>                    [bfd_get_elf_phdr_upper_bound (NULL)], elf-bfd.h)
>>>   if test "$gdb_cv_var_elf" = yes; then
>>> -  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
>>> +  CONFIG_OBS="$CONFIG_OBS stap-probe.o dtrace-probe.o \
>>>           gcore-elf.o elf-none-tdep.o"
>> Does it make sense (or is it useful) to compile these other files (other
>> than elfread.o) if ELF support is not enabled?  Could we just list them
>> all in elf_SRCS so that they are not built if the user doesn't want ELF
>> support?  If you don't have an ELF reader.
>
> I don't know. I took the path of least changes to implementing this, 
> made worse by me not having a convenient way to test a non-ELF target.
>
> I'll try to find a way to test if those are necessary and update the 
> patch if they aren't 

ah, turns out I could've just tried compiling with no elf support, oops. 
stap-probe and dtrace-probe can go

gcore-elf and elf-none-tdep are required to compile with 
--enable-targets=all, and since these aren't enough to consider elf 
supported, I think it's easier to just leave them here than figuring out 
which targets require them... I did notice that the arm-tdep.c does need 
them, but didn't go far into anything...

I'll submit a v7 with that update shortly

-- 
Cheers,
Guinevere Larsen
She/it

[-- Attachment #2: Type: text/html, Size: 3088 bytes --]

^ permalink raw reply	[flat|nested] 25+ messages in thread

* [PATCH v7 1/1] gdb, configure: Add enable-binary-file-format option for configure
  2025-07-07 20:16     ` [PATCH v6] " Guinevere Larsen
  2025-07-31 18:42       ` [PING][PATCH " Guinevere Larsen
  2025-08-05 16:03       ` [PATCH " Simon Marchi
@ 2025-08-06 14:17       ` Guinevere Larsen
  2025-08-14 15:39         ` Tom Tromey
  2 siblings, 1 reply; 25+ messages in thread
From: Guinevere Larsen @ 2025-08-06 14:17 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen, Eli Zaretskii

GDB has support for many binary file formats, some which might be very
unlikely to be found in some situations (such as the XCOFF format in
an x86 system). This commit introduces the option for a user to choose
which formats GDB will support at build configuration time.

This is especially useful to avoid possible security concerns with
readers that aren't expected to be used at all, as they are one of
the simplest vectors for an attacker to try and hit GDB with.  This
change can also reduce the size of the final binary, if that is a
concern.

This commit adds a switch to the configure script allowing a user to
only enable selected file formats, called --enable-binary-file-formats.
The default behavior when the switch is omitted is to compile all file
formats, keeping the original behavior of the script. At the time of
this commit, the valid options for this option are: dbx, coff (which
includes coff-pe), xcoff, mips, elf, macho and all. All is treated
especially, activating all supported readers.

A few targets may require specific binary file format support, as they
directly call functions defined by the file reader. Specifically,
windows targets require coff support, and rs6000 aix and lynx178 targets
require xcoff support. Considering that those formats are the main - or
only - one available in those targets, I believe it makes sense to
re-enable those readers. If that happens, the script will emit the
following warning:

  FOO is required to support one or more requested targets. Adding it

Users aren't able to force the disabling of those formats, since GDB
will not compile without those readers. Ideally we'd like to be able
to disable even those formats, in case a user wants to build GDB only
to examine remote files for example, but the current infrastructure
for the file format readers doesn't allow us to do it.

Mach-O and elf support are also dependent on BFD support being compiled
in.  In case one of those was requested and BFD does not support them,
the following error is emitted:

    FOO was requested, but BFD does not support it.

Finally, this configure switch is also printed by the "show
configuration" command in GDB.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
---

Changes for v7:
* Rebased on master
* Removed stap-probe and dtrace-probe files when ELF is not selected
* small formatting changes requested by Simon

---
 gdb/Makefile.in     |  20 ++++---
 gdb/NEWS            |  11 ++++
 gdb/README          |  24 +++++++++
 gdb/config.in       |   3 ++
 gdb/configure       | 124 +++++++++++++++++++++++++++++++++++++-------
 gdb/configure.ac    |  92 +++++++++++++++++++++++++++++---
 gdb/configure.tgt   |  20 +++++--
 gdb/doc/gdb.texinfo |   8 +++
 gdb/top.c           |   5 ++
 9 files changed, 270 insertions(+), 37 deletions(-)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index fc0c56564c2..7654fb1aa04 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -907,13 +907,22 @@ ALL_TARGET_OBS = \
 	vax-tdep.o \
 	windows-tdep.o \
 	x86-tdep.o \
-	xcoffread.o \
 	xstormy16-tdep.o \
 	xtensa-config.o \
 	xtensa-linux-tdep.o \
 	xtensa-tdep.o \
 	z80-tdep.o
 
+# Object files for reading specific types of debug information.
+coff_SRCS = coffread.c coff-pe-read.c
+dbx_SRCS = dbxread.c
+elf_SRCS = elfread.c stap-probe.c dtrace-probe.c
+macho_SRCS = machoread.c
+mips_SRCS = mipsread.c
+xcoff_SRCS = xcoffread.c
+FORMAT_SRCS = @FORMAT_SRCS@
+FORMAT_OBS = $(patsubst %.c,%.o,$(FORMAT_SRCS))
+
 # The following native-target dependent variables are defined on
 # configure.nat.
 NAT_FILE = @NAT_FILE@
@@ -1070,8 +1079,6 @@ COMMON_SFILES = \
 	c-varobj.c \
 	charset.c \
 	cli-out.c \
-	coff-pe-read.c \
-	coffread.c \
 	complaints.c \
 	completer.c \
 	copying.c \
@@ -1085,7 +1092,6 @@ COMMON_SFILES = \
 	d-lang.c \
 	d-namespace.c \
 	d-valprint.c \
-	dbxread.c \
 	dcache.c \
 	debug.c \
 	debuginfod-support.c \
@@ -1150,7 +1156,6 @@ COMMON_SFILES = \
 	memtag.c \
 	minidebug.c \
 	minsyms.c \
-	mipsread.c \
 	namespace.c \
 	objc-lang.c \
 	objfiles.c \
@@ -1243,7 +1248,6 @@ SFILES = \
 	d-exp.y \
 	dtrace-probe.c \
 	elf-none-tdep.c \
-	elfread.c \
 	f-exp.y \
 	gcore-elf.c \
 	gdb.c \
@@ -1870,7 +1874,6 @@ ALLDEPFILES = \
 	x86-gnu-nat.c \
 	x86-nat.c \
 	x86-tdep.c \
-	xcoffread.c \
 	xstormy16-tdep.c \
 	xtensa-config.c \
 	xtensa-linux-nat.c \
@@ -1925,7 +1928,8 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	$(patsubst %.c,%.o,$(COMMON_SFILES)) \
 	$(SUBDIR_CLI_OBS) \
 	$(SUBDIR_MI_OBS) \
-	$(SUBDIR_TARGET_OBS)
+	$(SUBDIR_TARGET_OBS) \
+	$(FORMAT_OBS)
 
 SUBDIRS = doc @subdirs@ data-directory
 CLEANDIRS = $(SUBDIRS)
diff --git a/gdb/NEWS b/gdb/NEWS
index 4c9aed421bb..a8c17318d7e 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -224,6 +224,17 @@ vFile:stat
 * Support for stabs debugging format and the a.out/dbx object format is
   deprecated, and will be removed in GDB 18.
 
+* Configure changes
+
+--enable-binary-file-formats=[FORMAT,...]
+--enable-binary-file-formats=all
+  A user can now decide to only compile support for certain file formats.
+  The available formats at this point are: dbx, coff, xcoff, elf, mach-o
+  and mips.  Some targets require specific file formats to be available,
+  and in such cases, the configure script will warn the user and add
+  support anyway.  By default, all formats will be compiled in, to
+  continue the behavior from before adding the switch.
+
 * A new configure option was added, allowing support for the compile
   subsystem to be disabled at configure time, in the form of
   --disable-gdb-compile.
diff --git a/gdb/README b/gdb/README
index eca4181c751..5dc2e7e7ad3 100644
--- a/gdb/README
+++ b/gdb/README
@@ -417,6 +417,30 @@ more obscure GDB `configure' options are not listed here.
      There is no convenient way to generate a list of all available
      targets.
 
+`--enable-binary-file-formats=FORMAT,FORMAT,...'
+`--enable-binary-file-formats=all'
+    Configure GDB to only be be able to read selected file formats.
+    The special value "all" causes all file formats to be compiled
+    in, and is the the default behavior of the option.  This option
+    is meant for advanced users who are sure of what they expect,
+    if you are unsure which options you will need on your debugging
+    sessions, we recommend that you not use this feature.  The
+    accepted options are:
+      * coff: Main format on Windows systems, this is required to
+        compile with windows target support;
+      * dbx (also known as a.out): Legacy file format, this is
+        recommended if you know you will be dealing with this
+        file format;
+      * elf: Main format on Linux systems, this is heavily
+        recommended when compiling with linux support;
+      * macho: Main format on MacOS systems, this is heavily
+        recommended when compiling for those targets;
+      * mips (also known as ecoff): Main file format for targets
+        running on MIPS CPUs, this is heavily recommended when
+        supporting those targets;
+      * xcoff: Main format on AIX systems, this is required to
+        compile for AIX targets and rs6000 CPUs.
+
 `--with-gdb-datadir=PATH'
      Set the GDB-specific data directory.  GDB will look here for
      certain supporting files or scripts.  This defaults to the `gdb'
diff --git a/gdb/config.in b/gdb/config.in
index 149aeaf979b..efc3100cb9e 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -745,6 +745,9 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+/* Which binary file formats were requested at configure time. */
+#undef SUPPORTED_BINARY_FILE_FORMATS
+
 /* automatically load a system-wide gdbinit file */
 #undef SYSTEM_GDBINIT
 
diff --git a/gdb/configure b/gdb/configure
index 8fc3b04efbf..cdea00f8675 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -706,6 +706,7 @@ LIBGUI
 LTLIBLZMA
 LIBLZMA
 HAVE_LIBLZMA
+FORMAT_SRCS
 SER_HARDWIRE
 WERROR_CFLAGS
 WARN_CFLAGS
@@ -932,6 +933,7 @@ with_relocated_sources
 with_auto_load_dir
 with_auto_load_safe_path
 enable_targets
+enable_binary_file_formats
 enable_gdb_mdebug_support
 enable_gdb_dwarf_support
 with_amd_dbgapi
@@ -1645,6 +1647,10 @@ Optional Features:
   --disable-nls           do not use Native Language Support
   --enable-targets=TARGETS
                           alternative target configurations
+  --enable-binary-file-formats=FORMATS
+                          enable support for selected file formats (default
+                          'all') available formats: coff, dbx, elf, macho,
+                          mips, xcoff, all
   --enable-gdb-mdebug-support
                           Enable support for the mdebug debuginfo format
                           (default 'yes')
@@ -11507,7 +11513,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11510 "configure"
+#line 11516 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11613,7 +11619,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11616 "configure"
+#line 11622 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -24882,6 +24888,18 @@ esac
 fi
 
 
+# Check whether --enable-binary_file_formats was given.
+if test "${enable_binary_file_formats+set}" = set; then :
+  enableval=$enable_binary_file_formats; case "${enableval}" in
+ yes | "") as_fn_error $? "enable-binary-file-formats option must specify file formats or 'all'" "$LINENO" 5
+            ;;
+  no)       enable_binary_file_formats= ;;
+  *)        enable_binary_file_formats=$enableval ;;
+esac
+else
+  enable_binary_file_formats=all
+fi
+
 
 # Check whether to support mdebug/ecoff debug information.
 # Check whether --enable-gdb-mdebug-support was given.
@@ -24986,10 +25004,21 @@ TARGET_OBS=
 all_targets=
 HAVE_NATIVE_GCORE_TARGET=
 
+# File formats that will be enabled based on the selected
+# target(s).  These are chosen because they are required to
+# compile one or more of the selected targets.
+target_formats=
+
+# If all targets were requested, this is all formats that should
+# accompany them.  These are just the ones required for compilation
+# to succeed, not the formats suggested based on targets.
+all_target_formats="coff xcoff"
+
 for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
 do
   if test "$targ_alias" = "all"; then
     all_targets=true
+    target_formats=$all_target_formats
   else
     # Canonicalize the secondary target names.
     result=`$ac_config_sub $targ_alias 2>/dev/null`
@@ -31628,6 +31657,12 @@ fi
 # Note that WIN32APILIBS is set by GDB_AC_COMMON.
 WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
 
+# Object files to be used when building with support for all file formats.
+# This should not have elf or macho, as support for those formats depends
+# on BFD enabling them as well.
+all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
+
+bfd_supports_elf=no
 # Add ELF support to GDB, but only if BFD includes ELF support.
 
   OLD_CFLAGS=$CFLAGS
@@ -31645,7 +31680,7 @@ WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
   CC="./libtool --quiet --mode=link $CC"
   { $as_echo "$as_me:${as_lineno-$LINENO}: checking for ELF support in BFD" >&5
 $as_echo_n "checking for ELF support in BFD... " >&6; }
-if ${gdb_cv_var_elf+:} false; then :
+if ${gdb_cv_var_bfd_elf+:} false; then :
   $as_echo_n "(cached) " >&6
 else
   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
@@ -31664,24 +31699,23 @@ return bfd_get_elf_phdr_upper_bound (NULL);
 }
 _ACEOF
 if ac_fn_c_try_link "$LINENO"; then :
-  gdb_cv_var_elf=yes
+  gdb_cv_var_bfd_elf=yes
 else
-  gdb_cv_var_elf=no
+  gdb_cv_var_bfd_elf=no
 
 fi
 rm -f core conftest.err conftest.$ac_objext \
     conftest$ac_exeext conftest.$ac_ext
 
 fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gdb_cv_var_elf" >&5
-$as_echo "$gdb_cv_var_elf" >&6; }
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gdb_cv_var_bfd_elf" >&5
+$as_echo "$gdb_cv_var_bfd_elf" >&6; }
   CC=$OLD_CC
   CFLAGS=$OLD_CFLAGS
   LDFLAGS=$OLD_LDFLAGS
   LIBS=$OLD_LIBS
-if test "$gdb_cv_var_elf" = yes; then
-  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
-		gcore-elf.o elf-none-tdep.o"
+if test "$gdb_cv_var_bfd_elf" = yes; then
+  CONFIG_OBS="$CONFIG_OBS gcore-elf.o elf-none-tdep.o"
 
 $as_echo "#define HAVE_ELF 1" >>confdefs.h
 
@@ -31744,9 +31778,12 @@ if test "$ac_res" != no; then :
 fi
 
   fi
+  bfd_supports_elf=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(elf_SRCS)"
 fi
 
 # Add macho support to GDB, but only if BFD includes it.
+bfd_supports_macho=no
 
   OLD_CFLAGS=$CFLAGS
   OLD_LDFLAGS=$LDFLAGS
@@ -31763,7 +31800,7 @@ fi
   CC="./libtool --quiet --mode=link $CC"
   { $as_echo "$as_me:${as_lineno-$LINENO}: checking for Mach-O support in BFD" >&5
 $as_echo_n "checking for Mach-O support in BFD... " >&6; }
-if ${gdb_cv_var_macho+:} false; then :
+if ${gdb_cv_var_bfd_macho+:} false; then :
   $as_echo_n "(cached) " >&6
 else
   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
@@ -31782,30 +31819,81 @@ return bfd_mach_o_lookup_command (NULL, 0, NULL);
 }
 _ACEOF
 if ac_fn_c_try_link "$LINENO"; then :
-  gdb_cv_var_macho=yes
+  gdb_cv_var_bfd_macho=yes
 else
-  gdb_cv_var_macho=no
+  gdb_cv_var_bfd_macho=no
 
 fi
 rm -f core conftest.err conftest.$ac_objext \
     conftest$ac_exeext conftest.$ac_ext
 
 fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gdb_cv_var_macho" >&5
-$as_echo "$gdb_cv_var_macho" >&6; }
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gdb_cv_var_bfd_macho" >&5
+$as_echo "$gdb_cv_var_bfd_macho" >&6; }
   CC=$OLD_CC
   CFLAGS=$OLD_CFLAGS
   LDFLAGS=$OLD_LDFLAGS
   LIBS=$OLD_LIBS
-if test "$gdb_cv_var_macho" = yes; then
-  CONFIG_OBS="$CONFIG_OBS machoread.o"
+if test "$gdb_cv_var_bfd_macho" = yes; then
+  bfd_supports_macho=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(macho_SRCS)"
 fi
 
+FORMAT_SRCS=
+
+if test "$enable_binary_file_formats" != "all"; then
+    # Formats that are required by some requested target(s).
+    # Warn users that they are added, so no one is surprised.
+    for req in $target_formats; do
+	case ,$enable_binary_file_formats, in
+	*,$req,*)
+	    # Do nothing.
+	    ;;
+	*)
+	    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&5
+$as_echo "$as_me: WARNING: \"$req is required to support one or more requested targets.  Adding it\"" >&2;}
+	    enable_binary_file_formats="${enable_binary_file_formats},$req"
+	    ;;
+	esac
+    done
+
+
+cat >>confdefs.h <<_ACEOF
+#define SUPPORTED_BINARY_FILE_FORMATS "$enable_binary_file_formats"
+_ACEOF
+
+fi
+
+enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
+
+# Go through all requested and required binary file formats to compile
+# GDB, and double check that we can compile them.
+for format in $enable_binary_file_formats
+do
+    if test "$format" = "elf" && test "$bfd_supports_elf" != "yes"; then
+	    as_fn_error but BFD does not support it." "\"elf support was requested" "$LINENO" 5;
+    elif test "$format" = "macho" && test "$bfd_supports_macho" != "yes"; then
+	    as_fn_error but BFD does not support it." "\"Mach-O support was requested" "$LINENO" 5;
+    fi
+
+    if test "$format" = "all"; then
+	FORMAT_SRCS="$all_binary_file_srcs"
+	# We don't break here in case the user requested Mach-O or ELF, but
+	# BFD is not configured to support it.  If we were to break, we would
+	# silently drop the requested support instead of erroring out.
+    else
+	fmt=$(echo "$format _SRCS" | sed 's/ //')
+	FORMAT_SRCS="$FORMAT_SRCS \$($fmt)"
+    fi
+done
+
+
+
 # Add any host-specific objects to GDB.
 CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
 
 # If building on ELF, look for lzma support for embedded compressed debug info.
-if test "$gdb_cv_var_elf" = yes; then
+if test "$gdb_cv_var_bfd_elf" = yes; then
 
 # Check whether --with-lzma was given.
 if test "${with_lzma+set}" = set; then :
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 226e27e4fe5..2ef53fe5c56 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -191,6 +191,16 @@ AS_HELP_STRING([--enable-targets=TARGETS], [alternative target configurations]),
 	;;
 esac])
 
+AC_ARG_ENABLE(binary_file_formats,
+	      AS_HELP_STRING([--enable-binary-file-formats=FORMATS],
+			     [enable support for selected file formats (default 'all')
+			      available formats: coff, dbx, elf, macho, mips, xcoff, all]),
+[case "${enableval}" in
+ yes | "") AC_MSG_ERROR(enable-binary-file-formats option must specify file formats or 'all')
+            ;;
+  no)       enable_binary_file_formats= ;;
+  *)        enable_binary_file_formats=$enableval ;;
+esac], [enable_binary_file_formats=all])
 
 # Check whether to support mdebug/ecoff debug information.
 AC_ARG_ENABLE(gdb-mdebug-support,
@@ -240,10 +250,21 @@ TARGET_OBS=
 all_targets=
 HAVE_NATIVE_GCORE_TARGET=
 
+# File formats that will be enabled based on the selected
+# target(s).  These are chosen because they are required to
+# compile one or more of the selected targets.
+target_formats=
+
+# If all targets were requested, this is all formats that should
+# accompany them.  These are just the ones required for compilation
+# to succeed, not the formats suggested based on targets.
+all_target_formats="coff xcoff"
+
 for targ_alias in `echo $target_alias $enable_targets | sed 's/,/ /g'`
 do
   if test "$targ_alias" = "all"; then
     all_targets=true
+    target_formats=$all_target_formats
   else
     # Canonicalize the secondary target names.
     result=`$ac_config_sub $targ_alias 2>/dev/null`
@@ -1952,32 +1973,87 @@ fi
 # Note that WIN32APILIBS is set by GDB_AC_COMMON.
 WIN32LIBS="$WIN32LIBS $WIN32APILIBS"
 
+# Object files to be used when building with support for all file formats.
+# This should not have elf or macho, as support for those formats depends
+# on BFD enabling them as well.
+all_binary_file_srcs="\$(dbx_SRCS) \$(mips_SRCS) \$(coff_SRCS) \$(xcoff_SRCS)"
+
+bfd_supports_elf=no
 # Add ELF support to GDB, but only if BFD includes ELF support.
-GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_elf,
+GDB_AC_CHECK_BFD([for ELF support in BFD], gdb_cv_var_bfd_elf,
                  [bfd_get_elf_phdr_upper_bound (NULL)], elf-bfd.h)
-if test "$gdb_cv_var_elf" = yes; then
-  CONFIG_OBS="$CONFIG_OBS elfread.o stap-probe.o dtrace-probe.o \
-		gcore-elf.o elf-none-tdep.o"
+if test "$gdb_cv_var_bfd_elf" = yes; then
+  CONFIG_OBS="$CONFIG_OBS gcore-elf.o elf-none-tdep.o"
   AC_DEFINE(HAVE_ELF, 1,
 	    [Define if ELF support should be included.])
   # -ldl is provided by bfd/Makefile.am (LIBDL) <PLUGINS>.
   if test "$plugins" = "yes"; then
     AC_SEARCH_LIBS(dlopen, dl)
   fi
+  bfd_supports_elf=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(elf_SRCS)"
 fi
 
 # Add macho support to GDB, but only if BFD includes it.
-GDB_AC_CHECK_BFD([for Mach-O support in BFD], gdb_cv_var_macho,
+bfd_supports_macho=no
+GDB_AC_CHECK_BFD([for Mach-O support in BFD], gdb_cv_var_bfd_macho,
                  [bfd_mach_o_lookup_command (NULL, 0, NULL)], mach-o.h)
-if test "$gdb_cv_var_macho" = yes; then
-  CONFIG_OBS="$CONFIG_OBS machoread.o"
+if test "$gdb_cv_var_bfd_macho" = yes; then
+  bfd_supports_macho=yes
+  all_binary_file_srcs="$all_binary_file_srcs \$(macho_SRCS)"
+fi
+
+FORMAT_SRCS=
+
+if test "$enable_binary_file_formats" != "all"; then
+    # Formats that are required by some requested target(s).
+    # Warn users that they are added, so no one is surprised.
+    for req in $target_formats; do
+	case ,$enable_binary_file_formats, in
+	*,$req,*)
+	    # Do nothing.
+	    ;;
+	*)
+	    AC_MSG_WARN("$req is required to support one or more requested targets.  Adding it")
+	    enable_binary_file_formats="${enable_binary_file_formats},$req"
+	    ;;
+	esac
+    done
+
+    AC_DEFINE_UNQUOTED(SUPPORTED_BINARY_FILE_FORMATS, "$enable_binary_file_formats",
+		       Which binary file formats were requested at configure time. )
 fi
 
+enable_binary_file_formats=$(echo $enable_binary_file_formats | sed 's/,/ /g')
+
+# Go through all requested and required binary file formats to compile
+# GDB, and double check that we can compile them.
+for format in $enable_binary_file_formats
+do
+    if test "$format" = "elf" && test "$bfd_supports_elf" != "yes"; then
+	    AC_MSG_ERROR("elf support was requested, but BFD does not support it.");
+    elif test "$format" = "macho" && test "$bfd_supports_macho" != "yes"; then
+	    AC_MSG_ERROR("Mach-O support was requested, but BFD does not support it.");
+    fi
+
+    if test "$format" = "all"; then
+	FORMAT_SRCS="$all_binary_file_srcs"
+	# We don't break here in case the user requested Mach-O or ELF, but
+	# BFD is not configured to support it.  If we were to break, we would
+	# silently drop the requested support instead of erroring out.
+    else
+	fmt=$(echo "$format _SRCS" | sed 's/ //')
+	FORMAT_SRCS="$FORMAT_SRCS \$($fmt)"
+    fi
+done
+
+AC_SUBST(FORMAT_SRCS)
+
 # Add any host-specific objects to GDB.
 CONFIG_OBS="${CONFIG_OBS} ${gdb_host_obs}"
 
 # If building on ELF, look for lzma support for embedded compressed debug info.
-if test "$gdb_cv_var_elf" = yes; then
+if test "$gdb_cv_var_bfd_elf" = yes; then
   AC_ARG_WITH(lzma,
     AS_HELP_STRING([--with-lzma], [support lzma compression (auto/yes/no)]),
     [], [with_lzma=auto])
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index 255c77e9f8c..332f5b7513c 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -505,7 +505,7 @@ powerpc-*-openbsd*)
 	;;
 powerpc-*-aix* | rs6000-*-* | powerpc64-*-aix*)
 	# Target: PowerPC running AIX
-	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o xcoffread.o \
+	gdb_target_obs="rs6000-tdep.o rs6000-aix-tdep.o \
 			ppc-sysv-tdep.o solib-aix.o \
 			ravenscar-thread.o ppc-ravenscar-thread.o"
 	;;
@@ -522,8 +522,8 @@ powerpc*-*-linux*)
 powerpc-*-lynx*178)
 	# Target: PowerPC running Lynx178.
 	gdb_target_obs="rs6000-tdep.o rs6000-lynx178-tdep.o \
-			xcoffread.o ppc-sysv-tdep.o \
-			ravenscar-thread.o ppc-ravenscar-thread.o"
+			ppc-sysv-tdep.o ravenscar-thread.o \
+			ppc-ravenscar-thread.o"
 	;;
 powerpc*-*-*)
 	# Target: PowerPC running eabi
@@ -835,3 +835,17 @@ for t in x ${gdb_target_obs}; do
     gdb_have_gcore=true
   fi
 done
+
+# Decide which file formats are absolutely required based on
+# the requested targets.  Warn later that they are added, in
+# case the user didn't manually request them, or all readers.
+# It's fine to add the same format multiple times since the
+# loop that reads the options to FORMAT_OBS will ensure that
+# they are only added once.
+for i in $gdb_target_obs; do
+    case "${i}" in
+    *"windows-tdep.o" ) target_formats="${target_formats} coff";;
+    "rs6000-aix-tdep.o" ) target_formats="${target_formats} xcoff";;
+    "rs6000-lynx178-tdep.o" ) target_formats="${target_formats} xcoff";;
+    esac
+done
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 2bbaf14c584..d4a5a632c24 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -41277,6 +41277,14 @@ Configure @value{GDBN} for cross-debugging programs running on the
 specified list of targets.  The special value @samp{all} configures
 @value{GDBN} for debugging programs running on any target it supports.
 
+@item --enable-binary-file-formats=@r{[}@var{format}@r{]}@dots{}
+@itemx --enable-binary-file-formats=all
+Configure @value{GDBN} to support certain binary file formats.  If a
+format is the main (or only) file format for a given target, the
+configure script may add support to it anyway, and warn the user.
+If not given, all file formats that @value{GDBN} supports are compiled
+in.
+
 @item --with-gdb-datadir=@var{path}
 Set the @value{GDBN}-specific data directory.  @value{GDBN} will look
 here for certain supporting files or scripts.  This defaults to the
diff --git a/gdb/top.c b/gdb/top.c
index b097683e613..933efa03f90 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -1596,6 +1596,11 @@ This GDB was configured as follows:\n\
 	     --with-system-gdbinit-dir=%s%s\n\
 "), SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE ? " (relocatable)" : "");
 
+#ifdef SUPPORTED_BINARY_FILE_FORMATS
+  gdb_printf (stream, _("\
+	     --enable-binary-file-formats=%s\n"), SUPPORTED_BINARY_FILE_FORMATS);
+#endif
+
   /* We assume "relocatable" will be printed at least once, thus we always
      print this text.  It's a reasonably safe assumption for now.  */
   gdb_printf (stream, _("\n\
-- 
2.50.1


^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v7 1/1] gdb, configure: Add enable-binary-file-format option for configure
  2025-08-06 14:17       ` [PATCH v7 1/1] gdb, configure: Add enable-binary-file-format " Guinevere Larsen
@ 2025-08-14 15:39         ` Tom Tromey
  2025-08-14 16:23           ` Guinevere Larsen
  0 siblings, 1 reply; 25+ messages in thread
From: Tom Tromey @ 2025-08-14 15:39 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: gdb-patches, Eli Zaretskii

>>>>> "Guinevere" == Guinevere Larsen <guinevere@redhat.com> writes:

Guinevere> GDB has support for many binary file formats, some which might be very
Guinevere> unlikely to be found in some situations (such as the XCOFF format in
Guinevere> an x86 system). This commit introduces the option for a user to choose
Guinevere> which formats GDB will support at build configuration time.

I think you addressed all the review comments.
This looks fine to me.

Approved-By: Tom Tromey <tom@tromey.com>

Guinevere> Mach-O and elf support are also dependent on BFD support being compiled
Guinevere> in.  In case one of those was requested and BFD does not support them,
Guinevere> the following error is emitted:

I don't recall - was there a reason this isn't entirely BFD-centric?
Like, some option to disable formats in BFD and then gdb would just
adapt to this?

thanks,
Tom

^ permalink raw reply	[flat|nested] 25+ messages in thread

* Re: [PATCH v7 1/1] gdb, configure: Add enable-binary-file-format option for configure
  2025-08-14 15:39         ` Tom Tromey
@ 2025-08-14 16:23           ` Guinevere Larsen
  0 siblings, 0 replies; 25+ messages in thread
From: Guinevere Larsen @ 2025-08-14 16:23 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches, Eli Zaretskii

On 8/14/25 12:39 PM, Tom Tromey wrote:
>>>>>> "Guinevere" == Guinevere Larsen <guinevere@redhat.com> writes:
> Guinevere> GDB has support for many binary file formats, some which might be very
> Guinevere> unlikely to be found in some situations (such as the XCOFF format in
> Guinevere> an x86 system). This commit introduces the option for a user to choose
> Guinevere> which formats GDB will support at build configuration time.
>
> I think you addressed all the review comments.
> This looks fine to me.
>
> Approved-By: Tom Tromey <tom@tromey.com>
Thank you! pushed!
>
> Guinevere> Mach-O and elf support are also dependent on BFD support being compiled
> Guinevere> in.  In case one of those was requested and BFD does not support them,
> Guinevere> the following error is emitted:
>
> I don't recall - was there a reason this isn't entirely BFD-centric?
> Like, some option to disable formats in BFD and then gdb would just
> adapt to this?

Mainly because it wouldn't work with the format of 
--enable-binary-file-format (I think anyway).

These attack surface patches were my first experience with autotools, so 
I wanted to make things as simple as possible while remaining consistent.

-- 
Cheers,
Guinevere Larsen
She/it


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2025-08-14 16:24 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-07 11:58 [PATCH v3] gdb, configure: Add disable-formats option for configure Guinevere Larsen
2025-03-07 12:09 ` Eli Zaretskii
2025-03-07 12:49   ` Guinevere Larsen
2025-03-07 13:03     ` Eli Zaretskii
2025-03-27 20:21 ` [PING][PATCH " Guinevere Larsen
2025-04-03 17:35 ` [PATCH " Tom Tromey
2025-04-07 17:48   ` Guinevere Larsen
2025-04-07 19:23     ` Tom Tromey
2025-04-08 13:47 ` [PATCH v4] " Guinevere Larsen
2025-04-08 14:06   ` Andreas Schwab
2025-04-08 14:24     ` Guinevere Larsen
2025-04-08 14:44       ` Andreas Schwab
2025-05-26 17:11   ` [PATCH v5 1/1] " Guinevere Larsen
2025-07-07 20:16     ` [PATCH v6] " Guinevere Larsen
2025-07-31 18:42       ` [PING][PATCH " Guinevere Larsen
2025-08-05 16:03       ` [PATCH " Simon Marchi
2025-08-05 19:06         ` Eli Zaretskii
2025-08-05 19:36           ` Simon Marchi
2025-08-05 20:07         ` Guinevere Larsen
2025-08-06 13:53           ` Guinevere Larsen
2025-08-06 14:17       ` [PATCH v7 1/1] gdb, configure: Add enable-binary-file-format " Guinevere Larsen
2025-08-14 15:39         ` Tom Tromey
2025-08-14 16:23           ` Guinevere Larsen
2025-05-27 11:32 ` [PATCH v3] gdb, configure: Add disable-formats " Pedro Alves
2025-05-27 16:26   ` Guinevere Larsen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox