]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: add configure option to disable compile
authorGuinevere Larsen <guinevere@redhat.com>
Thu, 13 Feb 2025 20:18:18 +0000 (17:18 -0300)
committerGuinevere Larsen <guinevere@redhat.com>
Wed, 26 Mar 2025 14:15:59 +0000 (11:15 -0300)
GDB's compile subsystem is deeply tied to GDB's ability to understand
DWARF. A future patch will add the option to disable DWARF at configure
time, but for that to work, the compile subsystem will need to be
entirely disabled as well, so this patch adds that possibility.

I also think there is motive for a security conscious user to disable
compile for it's own sake. Considering that the code is quite
unmaintained, and depends on an equally unmaintained gcc plugin, there
is a case to be made that this is an unnecessary increase in the attack
surface if a user knows they won't use the subsystem. Additionally, this
can make compilation slightly faster and the final binary is around 3Mb
smaller. But these are all secondary to the main goal of being able to
disable dwarf at configure time.

To be able to achieve optional compilation, some of the code that
interfaces with compile had to be changed. All parts that directly
called compile things have been wrapped by ifdefs checking for compile
support.  The file compile/compile.c has been setup in a similar way to
how python's and guile's main file has been setup, still being compiled
but only for with placeholder command.

Finally, to avoid several new errors, a new TCL proc was introduced to
gdb.exp, allow_compile_tests, which checks if the "compile" command is
recognized before the inferior is started and otherwise skips the compile
tests. All tests in the gdb.compile subfolder have been updated to use
that, and the test gdb.base/filename-completion also uses this. The proc
skip_compile_feature_tests to recognize when the subsystem has been
disabled at compile time.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
28 files changed:
gdb/Makefile.in
gdb/NEWS
gdb/README
gdb/cli/cli-script.c
gdb/compile/compile.c
gdb/config.in
gdb/configure
gdb/configure.ac
gdb/dwarf2/loc.c
gdb/testsuite/gdb.base/filename-completion.exp
gdb/testsuite/gdb.compile/compile-cplus-anonymous.exp
gdb/testsuite/gdb.compile/compile-cplus-array-decay.exp
gdb/testsuite/gdb.compile/compile-cplus-inherit.exp
gdb/testsuite/gdb.compile/compile-cplus-member.exp
gdb/testsuite/gdb.compile/compile-cplus-method.exp
gdb/testsuite/gdb.compile/compile-cplus-namespace.exp
gdb/testsuite/gdb.compile/compile-cplus-nested.exp
gdb/testsuite/gdb.compile/compile-cplus-print.exp
gdb/testsuite/gdb.compile/compile-cplus-virtual.exp
gdb/testsuite/gdb.compile/compile-cplus.exp
gdb/testsuite/gdb.compile/compile-ifunc.exp
gdb/testsuite/gdb.compile/compile-ops.exp
gdb/testsuite/gdb.compile/compile-print.exp
gdb/testsuite/gdb.compile/compile-setjmp.exp
gdb/testsuite/gdb.compile/compile-tls.exp
gdb/testsuite/gdb.compile/compile.exp
gdb/testsuite/lib/compile-support.exp
gdb/testsuite/lib/gdb.exp

index 8f6df5ba7b6ce2d107cf3e5ddbe9e3258f6c2d3e..6203bcf606fd8e53712c436183f82fc51ffb6fc2 100644 (file)
@@ -1912,8 +1912,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
        $(patsubst %.c,%.o,$(COMMON_SFILES)) \
        $(SUBDIR_CLI_OBS) \
        $(SUBDIR_MI_OBS) \
-       $(SUBDIR_TARGET_OBS) \
-       $(SUBDIR_GCC_COMPILE_OBS)
+       $(SUBDIR_TARGET_OBS)
 
 SUBDIRS = doc @subdirs@ data-directory
 CLEANDIRS = $(SUBDIRS)
index 2fdb849a81b8ab64ec8a9b7fb12d2964b05e3d46..40b0bcf72488ca33b969f9a7236f32e1332f32f8 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -108,6 +108,10 @@ qXfer:threads:read
 * Support for stabs debugging format and the a.out/dbx object format is
   deprecated, and will be removed in GDB 18.
 
+* 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.
+
 *** Changes in GDB 16
 
 * Support for Nios II targets has been removed as this architecture
index b6eb3f12c56ea3273da31418fbc512533de9bc0a..8836120d7ca780df7cf94196d03ccc8821e4aa20 100644 (file)
@@ -442,6 +442,9 @@ more obscure GDB `configure' options are not listed here.
      Requires a curses library (ncurses and cursesX are also
      supported).
 
+`--disable-gdb-compile'
+    Build GDB without support for the 'compile' command.
+
 `--with-curses'
      Use the curses library instead of the termcap library, for
      text-mode terminal operations.
index 5decf3b9d5c3803977468ea8bef52985384b3ddd..c14480f84ed6dfa73dce5e656a497be7ebb5d968 100644 (file)
@@ -660,9 +660,13 @@ execute_control_command_1 (struct command_line *cmd, int from_tty)
       }
 
     case compile_control:
+#if defined(HAVE_COMPILE)
       eval_compile_command (cmd, NULL, cmd->control_u.compile.scope,
                            cmd->control_u.compile.scope_data);
       ret = simple_control;
+#else
+      error (_("compile support has not been compiled into gdb"));
+#endif
       break;
 
     case define_control:
index 43987f9b2b70f28667693b6bd752edf1f9ffbc5e..01f43ad103d0aed8768bee3cde890e8924def11c 100644 (file)
 #include "gdbsupport/scoped_ignore_signal.h"
 #include "gdbsupport/buildargv.h"
 
+/* Hold "compile" commands.  */
+
+static struct cmd_list_element *compile_command_list;
+
 \f
+#ifdef HAVE_COMPILE
 
 /* Initial filename for temporary files.  */
 
 #define TMP_PREFIX "/tmp/gdbobj-"
 
-/* Hold "compile" commands.  */
-
-static struct cmd_list_element *compile_command_list;
-
 /* Debug flag for "compile" commands.  */
 
 bool compile_debug;
@@ -852,6 +853,18 @@ compile_instance::compile (const char *filename, int verbose_level)
 
 #undef FORWARD
 
+#else /* HAVE_COMPILE */
+
+/* The "compile" prefix command, when support was disabled.  */
+
+static void
+compile_command (const char *args, int from_tty)
+{
+  error (_("This command is not supported."));
+}
+
+#endif /* HAVE_COMPILE */
+
 /* See compile.h.  */
 cmd_list_element *compile_cmd_element = nullptr;
 
@@ -859,14 +872,25 @@ void _initialize_compile ();
 void
 _initialize_compile ()
 {
-  struct cmd_list_element *c = NULL;
-
   compile_cmd_element = add_prefix_cmd ("compile", class_obscure,
-                                       compile_command, _("\
+                                       compile_command,
+#ifdef HAVE_COMPILE
+                                       _("\
 Command to compile source code and inject it into the inferior."),
+#else /* HAVE_COMPILE */
+                                       _("\
+Command to compile source code and inject it into the inferior.\n\
+\n\
+Code compilation and injection is not supported in this copy of GDB.\n\
+This command is only a placeholder."),
+#endif /* HAVE_COMPILE */
                  &compile_command_list, 1, &cmdlist);
   add_com_alias ("expression", compile_cmd_element, class_obscure, 0);
 
+#ifdef HAVE_COMPILE
+
+  struct cmd_list_element *c = NULL;
+
   const auto compile_opts = make_compile_options_def_group (nullptr);
 
   static const std::string compile_code_help
@@ -973,4 +997,5 @@ It should be absolute filename of the gcc executable.\n\
 If empty the default target triplet will be searched in $PATH."),
                                     NULL, show_compile_gcc, &setlist,
                                     &showlist);
+#endif /* HAVE_COMPILE */
 }
index db63aeaec75a69573d21ec5da102eb422e58e9e8..86ff67d2898883261e5d87a83a1b416183004c38 100644 (file)
    the CoreFoundation framework. */
 #undef HAVE_CFPREFERENCESCOPYAPPVALUE
 
+/* Define if compiling support to gdb compile. */
+#undef HAVE_COMPILE
+
 /* Define to 1 if you have the <cursesX.h> header file. */
 #undef HAVE_CURSESX_H
 
index 3080413fdc95c287be91ccc13121d5e29ac5acde..e8a649f2ef0461cd62fd7b6c823227a5dfe9326b 100755 (executable)
@@ -956,6 +956,7 @@ with_libexpat_type
 with_python
 with_python_libdir
 with_guile
+enable_gdb_compile
 enable_source_highlight
 with_sysroot
 with_system_gdbinit
@@ -1650,6 +1651,8 @@ Optional Features:
   --enable-gdbtk          enable gdbtk graphical user interface (GUI)
   --enable-profiling      enable profiling of GDB
   --enable-codesign=CERT  sign gdb with 'codesign -s CERT'
+  --enable-gdb-compile    enable support for the compile subsystem, default
+                          'yes'
   --enable-source-highlight
                           enable source-highlight for source listings
   --enable-werror         treat compile warnings as errors
@@ -11500,7 +11503,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 11506 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11606,7 +11609,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 11612 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -28968,6 +28971,38 @@ else
 fi
 
 
+# ---------------------------- #
+# Check for compile support.   #
+# ---------------------------- #
+
+# Check whether --enable-gdb-compile was given.
+if test "${enable_gdb_compile+set}" = set; then :
+  enableval=$enable_gdb_compile;
+          case $enableval in
+            yes | no)
+              ;;
+            *)
+              as_fn_error $? "bad value $enableval for --enable-gdb-compile" "$LINENO" 5
+              ;;
+          esac
+
+else
+  enable_gdb_compile=yes
+fi
+
+
+if test "${enable_gdb_compile}" == yes; then
+
+$as_echo "#define HAVE_COMPILE 1" >>confdefs.h
+
+    CONFIG_OBS="$CONFIG_OBS \$(SUBDIR_GCC_COMPILE_OBS)"
+else
+    # Even if compile support is not enabled, we need this file to define
+    # the "compile" command.
+    CONFIG_OBS="$CONFIG_OBS compile/compile.o"
+    CONFIG_SRCS="$CONFIG_SRCS compile/compile.c"
+fi
+
 # ---------------------------- #
 # Check for source highlight.  #
 # ---------------------------- #
index eafbf5ac358becc0e2b196054def809c8e4a0798..2411b10ca407d64e2d9d5bdd71505621df35e8d2 100644 (file)
@@ -1221,6 +1221,26 @@ AC_SUBST(GUILE_CPPFLAGS)
 AC_SUBST(GUILE_LIBS)
 AM_CONDITIONAL(HAVE_GUILE, test "${have_libguile}" != no)
 
+# ---------------------------- #
+# Check for compile support.   #
+# ---------------------------- #
+
+AC_ARG_ENABLE([gdb-compile],
+    AS_HELP_STRING([--enable-gdb-compile],
+             [enable support for the compile subsystem, default 'yes']),
+             [GDB_CHECK_YES_NO_VAL([$enableval], [--enable-gdb-compile])],
+             [enable_gdb_compile=yes])
+
+if test "${enable_gdb_compile}" == yes; then
+    AC_DEFINE(HAVE_COMPILE, 1, [Define if compiling support to gdb compile.])
+    CONFIG_OBS="$CONFIG_OBS \$(SUBDIR_GCC_COMPILE_OBS)"
+else
+    # Even if compile support is not enabled, we need this file to define
+    # the "compile" command.
+    CONFIG_OBS="$CONFIG_OBS compile/compile.o"
+    CONFIG_SRCS="$CONFIG_SRCS compile/compile.c"
+fi
+
 # ---------------------------- #
 # Check for source highlight.  #
 # ---------------------------- #
index 7c12c0d4d4ddc1520c3fa1716de39470ece31406..8f666945122abe5efa36ead7dfb40adee223e99e 100644 (file)
@@ -1784,6 +1784,7 @@ dwarf2_compile_property_to_c (string_file *stream,
                              CORE_ADDR pc,
                              struct symbol *sym)
 {
+#if defined (HAVE_COMPILE)
   const dwarf2_property_baton *baton = prop->baton ();
   const gdb_byte *data;
   size_t size;
@@ -1810,6 +1811,9 @@ dwarf2_compile_property_to_c (string_file *stream,
                             gdbarch, registers_used,
                             per_cu->addr_size (),
                             data, data + size, per_cu, per_objfile);
+#else
+  gdb_assert_not_reached ("Compile support was disabled");
+#endif
 }
 
 /* Compute the correct symbol_needs_kind value for the location
@@ -3852,6 +3856,7 @@ locexpr_generate_c_location (struct symbol *sym, string_file *stream,
                             std::vector<bool> &registers_used,
                             CORE_ADDR pc, const char *result_name)
 {
+#if defined (HAVE_COMPILE)
   struct dwarf2_locexpr_baton *dlbaton
     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
   unsigned int addr_size = dlbaton->per_cu->addr_size ();
@@ -3863,6 +3868,9 @@ locexpr_generate_c_location (struct symbol *sym, string_file *stream,
                           sym, pc, gdbarch, registers_used, addr_size,
                           dlbaton->data, dlbaton->data + dlbaton->size,
                           dlbaton->per_cu, dlbaton->per_objfile);
+#else
+  gdb_assert_not_reached ("Compile support was disabled");
+#endif
 }
 
 /* The set of location functions used with the DWARF-2 expression
@@ -4088,6 +4096,7 @@ loclist_generate_c_location (struct symbol *sym, string_file *stream,
                             std::vector<bool> &registers_used,
                             CORE_ADDR pc, const char *result_name)
 {
+#if defined (HAVE_COMPILE)
   struct dwarf2_loclist_baton *dlbaton
     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
   unsigned int addr_size = dlbaton->per_cu->addr_size ();
@@ -4103,6 +4112,9 @@ loclist_generate_c_location (struct symbol *sym, string_file *stream,
                           data, data + size,
                           dlbaton->per_cu,
                           dlbaton->per_objfile);
+#else
+  gdb_assert_not_reached ("Compile support was disabled");
+#endif
 }
 
 /* The set of location functions used with the DWARF-2 expression
index 03ead592f0bc61f6d98cf7cfb92e0544fe0f4e6f..a1dd9746c202f030c56583498cefba0eddfebbf7 100644 (file)
@@ -381,11 +381,15 @@ proc run_mid_line_completion_tests { root cmd } {
 proc run_quoting_and_escaping_tests { root } {
     # Test all the commands which allow quoting of filenames, and
     # which require whitespace to be escaped in unquoted filenames.
-    foreach_with_prefix cmd { file exec-file symbol-file add-symbol-file \
-                                 remove-symbol-file \
-                                 "target core" "target exec" "target tfile" \
-                                 "maint print c-tdesc" "compile file" \
-                                 "save gdb-index" "save gdb-index -dwarf-5" } {
+    set all_cmds { file exec-file symbol-file add-symbol-file \
+                     remove-symbol-file \
+                     "target core" "target exec" "target tfile" \
+                     "maint print c-tdesc" "save gdb-index"
+                     "save gdb-index -dwarf-5" }
+    if { [allow_compile_tests] } {
+       lappend all_cmds "compile file"
+    }
+    foreach_with_prefix cmd $all_cmds {
        # Try each test placing the filename as the first argument
        # then again with a quoted string immediately after the
        # command.  This works because the filename completer will
index 3e76d38dc1024d32ad0e36c7081bf8f59745c1ac..ddad6281ebf3a80e4ccca7945a08aa09271b0c09 100644 (file)
@@ -23,6 +23,8 @@ require allow_cplus_tests
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if {[prepare_for_testing $testfile $testfile $srcfile \
         {debug nowarnings c++}]} {
     return -1
index 505a4e12eb350066772600133a85bfb681a1b7e2..c7d15ce0913a8f16cf815729e2f9b217f119e74b 100644 (file)
@@ -23,6 +23,8 @@ require allow_cplus_tests
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if {[prepare_for_testing $testfile $testfile $srcfile \
         {debug nowarnings c++ additional_flags=-std=c++11}]} {
     return -1
index 1a5f60a75dc3a306988fb1136504c8edcc5f13ce..9ef1e832f3dcf2c0a774d85de346573a1d1ef88d 100644 (file)
@@ -23,6 +23,8 @@ require allow_cplus_tests
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if {[prepare_for_testing $testfile $testfile $srcfile \
         {debug nowarnings c++}]} {
     return -1
index 5ffbb30df82d0a9a82e664ba9de8c1bba327183f..ac9111c83e47b28cee048414958fe5902923ce15 100644 (file)
@@ -23,6 +23,8 @@ require allow_cplus_tests
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if {[prepare_for_testing $testfile $testfile $srcfile \
         {debug nowarnings c++}]} {
     return -1
index 0a0e0fa18a39c4ec689e73a574b89172a286d73b..bcbfbb05e4bfe56858eb44fc53778da7c003a2c1 100644 (file)
@@ -23,6 +23,8 @@ require allow_cplus_tests
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if {[prepare_for_testing $testfile $testfile $srcfile \
         {debug nowarnings c++}]} {
     return -1
index 3ab8ece4bf109855da1fc84918f0be862239ed82..2abc3663d1c8b2cc807e5624cf6d0581b6bf2e86 100644 (file)
@@ -23,6 +23,8 @@ require allow_cplus_tests
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if {[prepare_for_testing $testfile $testfile $srcfile \
         {debug nowarnings c++}]} {
     return -1
index 19efd4f75066e88bb1a507f015659669d60a9f9b..247d270a231d737d12efb56ee4065af1391764a2 100644 (file)
@@ -23,6 +23,8 @@ require allow_cplus_tests
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if {[prepare_for_testing $testfile $testfile $srcfile \
         {debug nowarnings c++}]} {
     return -1
index 594f94a6ac984d5d2860aeaf42b7cb5ffd68e996..e4413f01d0e0cd9aa61c26828384fefc4c59b1a1 100644 (file)
@@ -19,6 +19,8 @@ standard_testfile
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 set options {}
 if [test_compiler_info gcc*] {
     lappend options additional_flags=-g3
index 8761df56db529fd54d2174ef2045f3844811bc01..a7702084133251b2ebdca9cd640fa66c8516a778 100644 (file)
@@ -23,6 +23,8 @@ require allow_cplus_tests
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if {[prepare_for_testing $testfile $testfile $srcfile \
         {debug nowarnings c++}]} {
     return -1
index 711f299574d73c95cd00e4f577fee854631b2013..35ae692acc771f0a79090c342828e66c90b9617b 100644 (file)
@@ -19,6 +19,8 @@ standard_testfile .c compile-shlib.c compile-constvar.S compile-nodebug.c
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 set options {}
 if { [test_compiler_info gcc*] || [test_compiler_info clang*] } {
     lappend options additional_flags=-g3
index b004bd7bb610941881afc9b896e1743b7909cdbb..e490890c5d6b6058fcb57d8edf46dac56022fd79 100644 (file)
@@ -17,6 +17,8 @@ load_lib compile-support.exp
 
 require allow_ifunc_tests
 
+require allow_compile_tests
+
 standard_testfile
 
 require is_c_compiler_gcc
index f75e02cedf3e321618aae80937d8ed868102dc9f..cfbe2b0356c253c4c0be6b9fe7a79ee200714525 100644 (file)
@@ -22,6 +22,8 @@ load_lib dwarf.exp
 # This test can only be run on targets which support DWARF-2 and use gas.
 require dwarf2_support
 
+require allow_compile_tests
+
 require is_c_compiler_gcc
 
 standard_testfile .c -dbg.S
index f8f22973e3e178ff9fcc3fc48b05e088d51eb15c..61ce7501df7023237431bfbe6c51988cc5608483 100644 (file)
@@ -19,6 +19,8 @@ standard_testfile
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if { [prepare_for_testing "failed to prepare" "$testfile"] } {
     return -1
 }
index f387a054ea94819b2e1ad84fa69e9e22484d9009..ad8732b773dd698d9933e7b55e7256a8a8629c00 100644 (file)
@@ -19,6 +19,8 @@ standard_testfile .c compile-setjmp-mod.c
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if { [prepare_for_testing "failed to prepare" $testfile] } {
     return -1
 }
index 2f8dc5ade2c1e36bbd65f7b329ca6611abf9e5f7..45e290e808e7c83ca52932efb5465b11e4a18adc 100644 (file)
@@ -19,6 +19,8 @@ standard_testfile .c
 
 require is_c_compiler_gcc
 
+require allow_compile_tests
+
 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
         executable {debug}] != "" } {
     return -1
index 2c2e3217b19dc611321ac58ed45e06c59053245f..5128dc62444d277cbfb8003d76ad27cdc61d3fc0 100644 (file)
@@ -15,6 +15,8 @@
 
 load_lib compile-support.exp
 
+require allow_compile_tests
+
 standard_testfile .c compile-shlib.c compile-constvar.S compile-nodebug.c
 
 require is_c_compiler_gcc
index aa8aaf3b7e307a07f59c5e45f6a31652fd3885ed..6d7a4ce13efbc330cb54fb09e87d0af2629749df 100644 (file)
@@ -45,6 +45,9 @@ proc _do_check_compile {expr} {
            # This appears to be a bug in the compiler plugin.
            set result "apparent compiler plugin bug"
        }
+       -re "This command is not supported." {
+           set result "compiler disabled at configure time"
+       }
        -re "\r\n$gdb_prompt $" {
        }
     }
index 3349da7a263e18d5c556ff13b4c3026e42c2ddcf..8486ff05e57464ef5c89ab94eae70b01c2e0cbf4 100644 (file)
@@ -2799,6 +2799,12 @@ gdb_caching_proc allow_python_tests {} {
     return [expr {[string first "--with-python" $output] != -1}]
 }
 
+# Return a 1 if GDB was configured to support compile commands.
+gdb_caching_proc allow_compile_tests {} {
+    set output [remote_exec host $::GDB "$::INTERNAL_GDBFLAGS -ex \"compile int x = 1\" -batch"]
+    return [expr {[string first "The program must be running" $output] != -1}]
+}
+
 # Return a 1 for configurations that use system readline rather than the
 # in-repo copy.