gdb: add configure option to disable compile

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>
This commit is contained in:
Guinevere Larsen
2025-02-13 17:18:18 -03:00
parent 62c95db2a1
commit c4375bc51c
28 changed files with 166 additions and 16 deletions

View 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)

View File

@@ -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

View 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.

View 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:

View File

@@ -46,16 +46,17 @@
#include "gdbsupport/scoped_ignore_signal.h"
#include "gdbsupport/buildargv.h"
/* Hold "compile" commands. */
static struct cmd_list_element *compile_command_list;
#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 */
}

View File

@@ -104,6 +104,9 @@
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

39
gdb/configure vendored
View File

@@ -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. #
# ---------------------------- #

View 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. #
# ---------------------------- #

View 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

View 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 \
set all_cmds { 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" } {
"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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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
}

View 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
}

View 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

View 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

View 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 $" {
}
}

View 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.