diff options
Diffstat (limited to 'gdb/compile')
-rw-r--r-- | gdb/compile/compile-c-support.c | 10 | ||||
-rw-r--r-- | gdb/compile/compile-c-symbols.c | 68 | ||||
-rw-r--r-- | gdb/compile/compile-c-types.c | 2 | ||||
-rw-r--r-- | gdb/compile/compile-c.h | 8 | ||||
-rw-r--r-- | gdb/compile/compile-cplus-symbols.c | 9 | ||||
-rw-r--r-- | gdb/compile/compile-cplus-types.c | 6 | ||||
-rw-r--r-- | gdb/compile/compile-cplus.h | 8 | ||||
-rw-r--r-- | gdb/compile/compile-internal.h | 47 | ||||
-rw-r--r-- | gdb/compile/compile-loc2c.c | 10 | ||||
-rw-r--r-- | gdb/compile/compile-object-load.c | 39 | ||||
-rw-r--r-- | gdb/compile/compile-object-load.h | 8 | ||||
-rw-r--r-- | gdb/compile/compile-object-run.c | 8 | ||||
-rw-r--r-- | gdb/compile/compile-object-run.h | 8 | ||||
-rw-r--r-- | gdb/compile/compile.c | 256 | ||||
-rw-r--r-- | gdb/compile/compile.h | 25 | ||||
-rw-r--r-- | gdb/compile/gcc-c-plugin.h | 8 | ||||
-rw-r--r-- | gdb/compile/gcc-cp-plugin.h | 8 |
17 files changed, 218 insertions, 310 deletions
diff --git a/gdb/compile/compile-c-support.c b/gdb/compile/compile-c-support.c index a152e5a..ac8888f 100644 --- a/gdb/compile/compile-c-support.c +++ b/gdb/compile/compile-c-support.c @@ -1,6 +1,6 @@ /* C/C++ language support for compilation. - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -185,18 +185,18 @@ static void write_macro_definitions (const struct block *block, CORE_ADDR pc, struct ui_file *file) { - gdb::unique_xmalloc_ptr<struct macro_scope> scope; + macro_scope scope; if (block != NULL) scope = sal_macro_scope (find_pc_line (pc, 0)); else scope = default_macro_scope (); - if (scope == NULL) + if (!scope.is_valid ()) scope = user_macro_scope (); - if (scope != NULL && scope->file != NULL && scope->file->table != NULL) + if (scope.is_valid () && scope.file->table != nullptr) { - macro_for_each_in_scope (scope->file, scope->line, + macro_for_each_in_scope (scope.file, scope.line, [&] (const char *name, const macro_definition *macro, macro_source_file *source, diff --git a/gdb/compile/compile-c-symbols.c b/gdb/compile/compile-c-symbols.c index 20e8550..b31592f 100644 --- a/gdb/compile/compile-c-symbols.c +++ b/gdb/compile/compile-c-symbols.c @@ -1,6 +1,6 @@ /* Convert symbols from GDB to GCC - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -30,7 +30,7 @@ #include "gdbtypes.h" #include "dwarf2/loc.h" #include "inferior.h" - +#include "gdbsupport/unordered_set.h" /* Compute the name of the pointer representing a local symbol's address. */ @@ -259,8 +259,7 @@ convert_symbol_sym (compile_c_instance *context, const char *identifier, to use and BMSYM is the minimal symbol to convert. */ static void -convert_symbol_bmsym (compile_c_instance *context, - struct bound_minimal_symbol bmsym) +convert_symbol_bmsym (compile_c_instance *context, bound_minimal_symbol bmsym) { struct minimal_symbol *msym = bmsym.minsym; struct objfile *objfile = bmsym.objfile; @@ -356,9 +355,8 @@ gcc_convert_symbol (void *datum, } else if (request == GCC_C_ORACLE_SYMBOL) { - struct bound_minimal_symbol bmsym; - - bmsym = lookup_minimal_symbol (identifier, NULL, NULL); + bound_minimal_symbol bmsym + = lookup_minimal_symbol (current_program_space, identifier); if (bmsym.minsym != NULL) { convert_symbol_bmsym (context, bmsym); @@ -413,9 +411,8 @@ gcc_symbol_address (void *datum, struct gcc_c_context *gcc_context, } else { - struct bound_minimal_symbol msym; - - msym = lookup_bound_minimal_symbol (identifier); + bound_minimal_symbol msym + = lookup_minimal_symbol (current_program_space, identifier); if (msym.minsym != NULL) { if (compile_debug) @@ -444,46 +441,6 @@ gcc_symbol_address (void *datum, struct gcc_c_context *gcc_context, return result; } - - -/* A hash function for symbol names. */ - -static hashval_t -hash_symname (const void *a) -{ - const struct symbol *sym = (const struct symbol *) a; - - return htab_hash_string (sym->natural_name ()); -} - -/* A comparison function for hash tables that just looks at symbol - names. */ - -static int -eq_symname (const void *a, const void *b) -{ - const struct symbol *syma = (const struct symbol *) a; - const struct symbol *symb = (const struct symbol *) b; - - return strcmp (syma->natural_name (), symb->natural_name ()) == 0; -} - -/* If a symbol with the same name as SYM is already in HASHTAB, return - 1. Otherwise, add SYM to HASHTAB and return 0. */ - -static int -symbol_seen (htab_t hashtab, struct symbol *sym) -{ - void **slot; - - slot = htab_find_slot (hashtab, sym, INSERT); - if (*slot != NULL) - return 1; - - *slot = sym; - return 0; -} - /* Generate C code to compute the length of a VLA. */ static void @@ -629,19 +586,16 @@ generate_c_for_variable_locations (compile_instance *compiler, /* Ensure that a given name is only entered once. This reflects the reality of shadowing. */ - htab_up symhash (htab_create_alloc (1, hash_symname, eq_symname, NULL, - xcalloc, xfree)); + gdb::unordered_set<std::string_view> symset; while (1) { /* Iterate over symbols in this block, generating code to compute the location of each local variable. */ for (struct symbol *sym : block_iterator_range (block)) - { - if (!symbol_seen (symhash.get (), sym)) - generate_c_for_for_one_variable (compiler, stream, gdbarch, - registers_used, pc, sym); - } + if (symset.insert (sym->natural_name ()).second) + generate_c_for_for_one_variable (compiler, stream, gdbarch, + registers_used, pc, sym); /* If we just finished the outermost block of a function, we're done. */ diff --git a/gdb/compile/compile-c-types.c b/gdb/compile/compile-c-types.c index 6407f12..bf1fea5 100644 --- a/gdb/compile/compile-c-types.c +++ b/gdb/compile/compile-c-types.c @@ -1,6 +1,6 @@ /* Convert types from GDB to GCC - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. This file is part of GDB. diff --git a/gdb/compile/compile-c.h b/gdb/compile/compile-c.h index 4670d67..1a6b056 100644 --- a/gdb/compile/compile-c.h +++ b/gdb/compile/compile-c.h @@ -1,5 +1,5 @@ /* Header file for GDB compile C-language support. - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. 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 @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef COMPILE_COMPILE_C_H -#define COMPILE_COMPILE_C_H +#ifndef GDB_COMPILE_COMPILE_C_H +#define GDB_COMPILE_COMPILE_C_H #include "compile/compile.h" #include "gdbsupport/enum-flags.h" @@ -93,4 +93,4 @@ extern std::string c_get_range_decl_name (const struct dynamic_prop *prop); extern gdb::unique_xmalloc_ptr<char> c_symbol_substitution_name (struct symbol *sym); -#endif /* COMPILE_COMPILE_C_H */ +#endif /* GDB_COMPILE_COMPILE_C_H */ diff --git a/gdb/compile/compile-cplus-symbols.c b/gdb/compile/compile-cplus-symbols.c index 9b95cdd..b03c4b9 100644 --- a/gdb/compile/compile-cplus-symbols.c +++ b/gdb/compile/compile-cplus-symbols.c @@ -1,6 +1,6 @@ /* Convert symbols from GDB to GCC - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -275,7 +275,7 @@ convert_symbol_sym (compile_cplus_instance *instance, static void convert_symbol_bmsym (compile_cplus_instance *instance, - struct bound_minimal_symbol bmsym) + bound_minimal_symbol bmsym) { struct minimal_symbol *msym = bmsym.minsym; struct objfile *objfile = bmsym.objfile; @@ -453,9 +453,8 @@ gcc_cplus_symbol_address (void *datum, struct gcc_cp_context *gcc_context, } else { - struct bound_minimal_symbol msym; - - msym = lookup_bound_minimal_symbol (identifier); + bound_minimal_symbol msym + = lookup_minimal_symbol (current_program_space, identifier); if (msym.minsym != nullptr) { if (compile_debug) diff --git a/gdb/compile/compile-cplus-types.c b/gdb/compile/compile-cplus-types.c index 8d14114..cf70fe4 100644 --- a/gdb/compile/compile-cplus-types.c +++ b/gdb/compile/compile-cplus-types.c @@ -1,6 +1,6 @@ /* Convert types from GDB to GCC - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -1395,9 +1395,7 @@ gcc_cp_plugin::pop_binding_level (const char *debug_name) return pop_binding_level (); } -void _initialize_compile_cplus_types (); -void -_initialize_compile_cplus_types () +INIT_GDB_FILE (compile_cplus_types) { add_setshow_boolean_cmd ("compile-cplus-types", no_class, &debug_compile_cplus_types, _("\ diff --git a/gdb/compile/compile-cplus.h b/gdb/compile/compile-cplus.h index a714b6d..d08feea 100644 --- a/gdb/compile/compile-cplus.h +++ b/gdb/compile/compile-cplus.h @@ -1,5 +1,5 @@ /* Header file for GDB compile C++ language support. - Copyright (C) 2016-2024 Free Software Foundation, Inc. + Copyright (C) 2016-2025 Free Software Foundation, Inc. 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 @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef COMPILE_COMPILE_CPLUS_H -#define COMPILE_COMPILE_CPLUS_H +#ifndef GDB_COMPILE_COMPILE_CPLUS_H +#define GDB_COMPILE_COMPILE_CPLUS_H #include "compile/compile.h" #include "gdbsupport/enum-flags.h" @@ -204,4 +204,4 @@ private: enum gcc_cp_symbol_kind get_method_access_flag (const struct type *type, int fni, int num); -#endif /* COMPILE_COMPILE_CPLUS_H */ +#endif /* GDB_COMPILE_COMPILE_CPLUS_H */ diff --git a/gdb/compile/compile-internal.h b/gdb/compile/compile-internal.h index 666406b..46a8ac2 100644 --- a/gdb/compile/compile-internal.h +++ b/gdb/compile/compile-internal.h @@ -1,5 +1,5 @@ /* Header file for GDB compile command and supporting functions. - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. 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 @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef COMPILE_COMPILE_INTERNAL_H -#define COMPILE_COMPILE_INTERNAL_H +#ifndef GDB_COMPILE_COMPILE_INTERNAL_H +#define GDB_COMPILE_COMPILE_INTERNAL_H #include "gcc-c-interface.h" #include "gdbsupport/gdb-hashtab.h" @@ -80,4 +80,43 @@ private: std::string m_object_file; }; -#endif /* COMPILE_COMPILE_INTERNAL_H */ +struct compile_instance; + +/* Create a new instance of the C compiler and return it. This + function never returns NULL, but rather throws an exception on + failure. This is suitable for use as the + language_defn::get_compile_instance method. */ + +extern std::unique_ptr<compile_instance> c_get_compile_context (); + +/* Create a new instance of the C++ compiler and return it. This + function never returns NULL, but rather throws an exception on + failure. This is suitable for use as the + language_defn::get_compile_instance method. */ + +extern std::unique_ptr<compile_instance> cplus_get_compile_context (); + +/* This takes the user-supplied text and returns a new bit of code to + compile. + + This is used as the compute_program language method; see that + for a description of the arguments. */ + +extern std::string c_compute_program (compile_instance *inst, + const char *input, + struct gdbarch *gdbarch, + const struct block *expr_block, + CORE_ADDR expr_pc); + +/* This takes the user-supplied text and returns a new bit of code to compile. + + This is used as the compute_program language method; see that + for a description of the arguments. */ + +extern std::string cplus_compute_program (compile_instance *inst, + const char *input, + struct gdbarch *gdbarch, + const struct block *expr_block, + CORE_ADDR expr_pc); + +#endif /* GDB_COMPILE_COMPILE_INTERNAL_H */ diff --git a/gdb/compile/compile-loc2c.c b/gdb/compile/compile-loc2c.c index fbcc3d6..31fd8e2 100644 --- a/gdb/compile/compile-loc2c.c +++ b/gdb/compile/compile-loc2c.c @@ -1,6 +1,6 @@ /* Convert a DWARF location expression to C - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -29,9 +29,9 @@ #include "compile.h" #include "block.h" #include "dwarf2/frame.h" -#include "gdbsupport/gdb_vecs.h" #include "value.h" #include "gdbarch.h" +#include "extract-store-integer.h" @@ -597,7 +597,7 @@ do_compile_dwarf_expr_to_c (int indent, string_file *stream, unsigned int addr_size, const gdb_byte *op_ptr, const gdb_byte *op_end, CORE_ADDR *initial, - dwarf2_per_cu_data *per_cu, + dwarf2_per_cu *per_cu, dwarf2_per_objfile *per_objfile) { /* We keep a counter so that labels and other objects we create have @@ -1147,7 +1147,7 @@ compile_dwarf_expr_to_c (string_file *stream, const char *result_name, std::vector<bool> ®isters_used, unsigned int addr_size, const gdb_byte *op_ptr, const gdb_byte *op_end, - dwarf2_per_cu_data *per_cu, + dwarf2_per_cu *per_cu, dwarf2_per_objfile *per_objfile) { do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc, @@ -1166,7 +1166,7 @@ compile_dwarf_bounds_to_c (string_file *stream, std::vector<bool> ®isters_used, unsigned int addr_size, const gdb_byte *op_ptr, const gdb_byte *op_end, - dwarf2_per_cu_data *per_cu, + dwarf2_per_cu *per_cu, dwarf2_per_objfile *per_objfile) { do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name, diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c index 08e30be..49b967a 100644 --- a/gdb/compile/compile-object-load.c +++ b/gdb/compile/compile-object-load.c @@ -1,6 +1,6 @@ /* Load module for 'compile' command. - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -299,6 +299,7 @@ static const struct bfd_link_callbacks link_callbacks = link_callbacks_reloc_dangerous, /* reloc_dangerous */ link_callbacks_unattached_reloc, /* unattached_reloc */ NULL, /* notice */ + NULL, /* fatal */ link_callbacks_einfo, /* einfo */ NULL, /* info */ NULL, /* minfo */ @@ -604,10 +605,7 @@ compile_object_load (const compile_file_names &file_names, CORE_ADDR regs_addr, out_value_addr = 0; struct symbol *func_sym; struct type *func_type; - struct bound_minimal_symbol bmsym; - long storage_needed; - asymbol **symbol_table, **symp; - long number_of_symbols, missing_symbols; + long missing_symbols; struct type *regs_type, *out_value_type = NULL; char **matching; struct objfile *objfile; @@ -635,16 +633,11 @@ compile_object_load (const compile_file_names &file_names, setup_sections_data.setup_one_section (sect); setup_sections_data.setup_one_section (nullptr); - storage_needed = bfd_get_symtab_upper_bound (abfd.get ()); - if (storage_needed < 0) - error (_("Cannot read symbols of compiled module \"%s\": %s"), - filename.get (), bfd_errmsg (bfd_get_error ())); - /* SYMFILE_VERBOSE is not passed even if FROM_TTY, user is not interested in "Reading symbols from ..." message for automatically generated file. */ - objfile_up objfile_holder (symbol_file_add_from_bfd (abfd, - filename.get (), - 0, NULL, 0, NULL)); + scoped_objfile_unlinker objfile_holder (symbol_file_add_from_bfd + (abfd, filename.get (), + 0, NULL, 0, NULL)); objfile = objfile_holder.get (); func_sym = lookup_global_symbol_from_objfile (objfile, @@ -692,21 +685,12 @@ compile_object_load (const compile_file_names &file_names, "module \"%s\"."), GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile)); - /* The memory may be later needed - by bfd_generic_get_relocated_section_contents - called from default_symfile_relocate. */ - symbol_table = (asymbol **) obstack_alloc (&objfile->objfile_obstack, - storage_needed); - number_of_symbols = bfd_canonicalize_symtab (abfd.get (), symbol_table); - if (number_of_symbols < 0) - error (_("Cannot parse symbols of compiled module \"%s\": %s"), - filename.get (), bfd_errmsg (bfd_get_error ())); + gdb::array_view<asymbol *> symbol_table + = gdb_bfd_canonicalize_symtab (abfd.get ()); missing_symbols = 0; - for (symp = symbol_table; symp < symbol_table + number_of_symbols; symp++) + for (asymbol *sym : symbol_table) { - asymbol *sym = *symp; - if (sym->flags != 0) continue; sym->flags = BSF_GLOBAL; @@ -765,7 +749,8 @@ compile_object_load (const compile_file_names &file_names, continue; } - bmsym = lookup_minimal_symbol (sym->name, NULL, NULL); + bound_minimal_symbol bmsym + = lookup_minimal_symbol (current_program_space, sym->name); switch (bmsym.minsym == NULL ? mst_unknown : bmsym.minsym->type ()) { @@ -799,7 +784,7 @@ compile_object_load (const compile_file_names &file_names, if (missing_symbols) error (_("%ld symbols were missing, cannot continue."), missing_symbols); - bfd_map_over_sections (abfd.get (), copy_sections, symbol_table); + bfd_map_over_sections (abfd.get (), copy_sections, symbol_table.data ()); regs_type = get_regs_type (func_sym, objfile); if (regs_type == NULL) diff --git a/gdb/compile/compile-object-load.h b/gdb/compile/compile-object-load.h index 83665b5..5122764 100644 --- a/gdb/compile/compile-object-load.h +++ b/gdb/compile/compile-object-load.h @@ -1,5 +1,5 @@ /* Header file to load module for 'compile' command. - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. 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 @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef COMPILE_COMPILE_OBJECT_LOAD_H -#define COMPILE_COMPILE_OBJECT_LOAD_H +#ifndef GDB_COMPILE_COMPILE_OBJECT_LOAD_H +#define GDB_COMPILE_COMPILE_OBJECT_LOAD_H #include "compile-internal.h" #include "compile.h" @@ -95,4 +95,4 @@ extern compile_module_up compile_object_load (const compile_file_names &fnames, enum compile_i_scope_types scope, void *scope_data); -#endif /* COMPILE_COMPILE_OBJECT_LOAD_H */ +#endif /* GDB_COMPILE_COMPILE_OBJECT_LOAD_H */ diff --git a/gdb/compile/compile-object-run.c b/gdb/compile/compile-object-run.c index 5a810d5..30b45dc 100644 --- a/gdb/compile/compile-object-run.c +++ b/gdb/compile/compile-object-run.c @@ -1,6 +1,6 @@ /* Call module for 'compile' command. - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -105,9 +105,9 @@ do_module_cleanup (void *arg, int registers_valid) static type * create_copied_type_recursive (objfile *objfile, type *func_type) { - htab_up copied_types = create_copied_types_hash (); - func_type = copy_type_recursive (func_type, copied_types.get ()); - return func_type; + copied_types_hash_t copied_types; + + return copy_type_recursive (func_type, copied_types); } /* Perform inferior call of MODULE. This function may throw an error. diff --git a/gdb/compile/compile-object-run.h b/gdb/compile/compile-object-run.h index 5d9f23c..7515b70 100644 --- a/gdb/compile/compile-object-run.h +++ b/gdb/compile/compile-object-run.h @@ -1,5 +1,5 @@ /* Header file to call module for 'compile' command. - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. 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 @@ -14,11 +14,11 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef COMPILE_COMPILE_OBJECT_RUN_H -#define COMPILE_COMPILE_OBJECT_RUN_H +#ifndef GDB_COMPILE_COMPILE_OBJECT_RUN_H +#define GDB_COMPILE_COMPILE_OBJECT_RUN_H #include "compile-object-load.h" extern void compile_object_run (compile_module_up &&module); -#endif /* COMPILE_COMPILE_OBJECT_RUN_H */ +#endif /* GDB_COMPILE_COMPILE_OBJECT_RUN_H */ diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c index 88531a2..229e155 100644 --- a/gdb/compile/compile.c +++ b/gdb/compile/compile.c @@ -1,6 +1,6 @@ /* General Compile and inject code - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "progspace.h" #include "ui.h" #include "ui-out.h" #include "command.h" @@ -45,126 +46,30 @@ #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; -/* Object of this type are stored in the compiler's symbol_err_map. */ - -struct symbol_error -{ - /* The symbol. */ - - const struct symbol *sym; - - /* The error message to emit. This is malloc'd and owned by the - hash table. */ - - char *message; -}; - -/* An object that maps a gdb type to a gcc type. */ - -struct type_map_instance -{ - /* The gdb type. */ - - struct type *type; - - /* The corresponding gcc type handle. */ - - gcc_type gcc_type_handle; -}; - -/* Hash a type_map_instance. */ - -static hashval_t -hash_type_map_instance (const void *p) -{ - const struct type_map_instance *inst = (const struct type_map_instance *) p; - - return htab_hash_pointer (inst->type); -} - -/* Check two type_map_instance objects for equality. */ - -static int -eq_type_map_instance (const void *a, const void *b) -{ - const struct type_map_instance *insta = (const struct type_map_instance *) a; - const struct type_map_instance *instb = (const struct type_map_instance *) b; - - return insta->type == instb->type; -} - -/* Hash function for struct symbol_error. */ - -static hashval_t -hash_symbol_error (const void *a) -{ - const struct symbol_error *se = (const struct symbol_error *) a; - - return htab_hash_pointer (se->sym); -} - -/* Equality function for struct symbol_error. */ - -static int -eq_symbol_error (const void *a, const void *b) -{ - const struct symbol_error *sea = (const struct symbol_error *) a; - const struct symbol_error *seb = (const struct symbol_error *) b; - - return sea->sym == seb->sym; -} - -/* Deletion function for struct symbol_error. */ - -static void -del_symbol_error (void *a) -{ - struct symbol_error *se = (struct symbol_error *) a; - - xfree (se->message); - xfree (se); -} - -/* Constructor for compile_instance. */ - -compile_instance::compile_instance (struct gcc_base_context *gcc_fe, - const char *options) - : m_gcc_fe (gcc_fe), m_gcc_target_options (options), - m_type_map (htab_create_alloc (10, hash_type_map_instance, - eq_type_map_instance, - xfree, xcalloc, xfree)), - m_symbol_err_map (htab_create_alloc (10, hash_symbol_error, - eq_symbol_error, del_symbol_error, - xcalloc, xfree)) -{ -} - /* See compile-internal.h. */ bool compile_instance::get_cached_type (struct type *type, gcc_type *ret) const { - struct type_map_instance inst, *found; - - inst.type = type; - found = (struct type_map_instance *) htab_find (m_type_map.get (), &inst); - if (found != NULL) + if (auto iter = m_type_map.find (type); + iter != m_type_map.end ()) { - *ret = found->gcc_type_handle; + *ret = iter->second; return true; } @@ -176,25 +81,12 @@ compile_instance::get_cached_type (struct type *type, gcc_type *ret) const void compile_instance::insert_type (struct type *type, gcc_type gcc_type) { - struct type_map_instance inst, *add; - void **slot; - - inst.type = type; - inst.gcc_type_handle = gcc_type; - slot = htab_find_slot (m_type_map.get (), &inst, INSERT); + auto [it, inserted] = m_type_map.emplace (type, gcc_type); - add = (struct type_map_instance *) *slot; /* The type might have already been inserted in order to handle recursive types. */ - if (add != NULL && add->gcc_type_handle != gcc_type) + if (!inserted && it->second != gcc_type) error (_("Unexpected type id from GCC, check you use recent enough GCC.")); - - if (add == NULL) - { - add = XNEW (struct type_map_instance); - *add = inst; - *slot = add; - } } /* See compile-internal.h. */ @@ -203,19 +95,7 @@ void compile_instance::insert_symbol_error (const struct symbol *sym, const char *text) { - struct symbol_error e; - void **slot; - - e.sym = sym; - slot = htab_find_slot (m_symbol_err_map.get (), &e, INSERT); - if (*slot == NULL) - { - struct symbol_error *ep = XNEW (struct symbol_error); - - ep->sym = sym; - ep->message = xstrdup (text); - *slot = ep; - } + m_symbol_err_map.emplace (sym, text); } /* See compile-internal.h. */ @@ -223,20 +103,12 @@ compile_instance::insert_symbol_error (const struct symbol *sym, void compile_instance::error_symbol_once (const struct symbol *sym) { - struct symbol_error search; - struct symbol_error *err; - - if (m_symbol_err_map == NULL) - return; - - search.sym = sym; - err = (struct symbol_error *) htab_find (m_symbol_err_map.get (), &search); - if (err == NULL || err->message == NULL) - return; - - gdb::unique_xmalloc_ptr<char> message (err->message); - err->message = NULL; - error (_("%s"), message.get ()); + if (auto iter = m_symbol_err_map.find (sym); + iter != m_symbol_err_map.end () && !iter->second.empty ()) + { + std::string message = std::move (iter->second); + error (_("%s"), message.c_str ()); + } } /* Implement "show debug compile". */ @@ -302,14 +174,13 @@ compile_file_command (const char *args, int from_tty) enum compile_i_scope_types scope = options.raw ? COMPILE_I_RAW_SCOPE : COMPILE_I_SIMPLE_SCOPE; - args = skip_spaces (args); + std::string filename = extract_single_filename_arg (args); /* After processing options, check whether we have a filename. */ - if (args == nullptr || args[0] == '\0') + if (filename.empty ()) error (_("You must provide a filename for this command.")); - args = skip_spaces (args); - std::string abspath = gdb_abspath (args); + std::string abspath = gdb_abspath (filename.c_str ()); std::string buffer = string_printf ("#include \"%s\"\n", abspath.c_str ()); eval_compile_command (NULL, buffer.c_str (), scope, NULL); } @@ -327,8 +198,8 @@ compile_file_command_completer (struct cmd_list_element *ignore, (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group)) return; - word = advance_to_filename_complete_word_point (tracker, text); - filename_completer (ignore, tracker, text, word); + word = advance_to_filename_maybe_quoted_complete_word_point (tracker, text); + filename_maybe_quoted_completer (ignore, tracker, text, word); } /* Handle the input from the 'compile code' command. The @@ -487,7 +358,8 @@ get_expr_block_and_pc (CORE_ADDR *pc) if (block == NULL) { - struct symtab_and_line cursal = get_current_source_symtab_and_line (); + symtab_and_line cursal + = get_current_source_symtab_and_line (current_program_space); if (cursal.symtab) block = cursal.symtab->compunit ()->blockvector ()->static_block (); @@ -656,6 +528,41 @@ print_callback (void *ignore, const char *message) gdb_puts (message, gdb_stderr); } +/* Helper for compile_to_object, to find the compile context + based on the current language. */ +static std::unique_ptr<compile_instance> +get_language_compile_context () +{ + switch (current_language->la_language) + { + case language_c: + return c_get_compile_context (); + case language_cplus: + return cplus_get_compile_context (); + default: + return {}; + } +} + +/* Helper for compile_to_object, to call the correct + compute_program based on the current language. */ +static std::string +compute_program_language (compile_instance *inst, const char *input, + struct gdbarch *gdbarch, + const struct block *block, + CORE_ADDR pc) +{ + switch (current_language->la_language) + { + case language_c: + return c_compute_program (inst, input, gdbarch, block, pc); + case language_cplus: + return cplus_compute_program (inst, input, gdbarch, block, pc); + default: + gdb_assert_not_reached ("Unsupported language"); + } +} + /* Process the compilation request. On success it returns the object and source file names. On an error condition, error () is called. */ @@ -679,7 +586,8 @@ compile_to_object (struct command_line *cmd, const char *cmd_string, /* Set up instance and context for the compiler. */ std::unique_ptr<compile_instance> compiler - = current_language->get_compile_instance (); + = get_language_compile_context (); + if (compiler == nullptr) error (_("No compiler support for language %s."), current_language->name ()); @@ -711,8 +619,8 @@ compile_to_object (struct command_line *cmd, const char *cmd_string, error (_("Neither a simple expression, or a multi-line specified.")); std::string code - = current_language->compute_program (compiler.get (), input, gdbarch, - expr_block, expr_pc); + = compute_program_language (compiler.get (), input, gdbarch, + expr_block, expr_pc); if (compile_debug) gdb_printf (gdb_stdlog, "debug output:\n\n%s", code.c_str ()); @@ -945,21 +853,42 @@ 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; -void _initialize_compile (); -void -_initialize_compile () +INIT_GDB_FILE (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 @@ -1066,4 +995,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 */ } diff --git a/gdb/compile/compile.h b/gdb/compile/compile.h index 4be6f50..9308623 100644 --- a/gdb/compile/compile.h +++ b/gdb/compile/compile.h @@ -1,6 +1,6 @@ /* Header file for Compile and inject module. - Copyright (C) 2014-2024 Free Software Foundation, Inc. + Copyright (C) 2014-2025 Free Software Foundation, Inc. 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 @@ -15,15 +15,15 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef COMPILE_COMPILE_H -#define COMPILE_COMPILE_H +#ifndef GDB_COMPILE_COMPILE_H +#define GDB_COMPILE_COMPILE_H #include "gcc-c-interface.h" -#include "gdbsupport/gdb-hashtab.h" +#include "gdbsupport/unordered_map.h" struct ui_file; struct gdbarch; -struct dwarf2_per_cu_data; +struct dwarf2_per_cu; struct dwarf2_per_objfile; struct symbol; struct dynamic_prop; @@ -61,7 +61,10 @@ enum compile_i_scope_types class compile_instance { public: - compile_instance (struct gcc_base_context *gcc_fe, const char *options); + compile_instance (struct gcc_base_context *gcc_fe, const char *options) + : m_gcc_fe (gcc_fe), + m_gcc_target_options (options) + {} virtual ~compile_instance () { @@ -163,10 +166,10 @@ protected: std::string m_gcc_target_options; /* Map from gdb types to gcc types. */ - htab_up m_type_map; + gdb::unordered_map<type *, gcc_type> m_type_map; /* Map from gdb symbols to gcc error messages to emit. */ - htab_up m_symbol_err_map; + gdb::unordered_map<const symbol *, std::string> m_symbol_err_map; }; /* Public function that is called from compile_control case in the @@ -212,7 +215,7 @@ extern void compile_dwarf_expr_to_c (string_file *stream, unsigned int addr_size, const gdb_byte *op_ptr, const gdb_byte *op_end, - dwarf2_per_cu_data *per_cu, + dwarf2_per_cu *per_cu, dwarf2_per_objfile *per_objfile); /* Compile a DWARF bounds expression to C, suitable for use by the @@ -251,7 +254,7 @@ extern void compile_dwarf_bounds_to_c (string_file *stream, unsigned int addr_size, const gdb_byte *op_ptr, const gdb_byte *op_end, - dwarf2_per_cu_data *per_cu, + dwarf2_per_cu *per_cu, dwarf2_per_objfile *per_objfile); extern void compile_print_value (struct value *val, void *data_voidp); @@ -259,4 +262,4 @@ extern void compile_print_value (struct value *val, void *data_voidp); /* Command element for the 'compile' command. */ extern cmd_list_element *compile_cmd_element; -#endif /* COMPILE_COMPILE_H */ +#endif /* GDB_COMPILE_COMPILE_H */ diff --git a/gdb/compile/gcc-c-plugin.h b/gdb/compile/gcc-c-plugin.h index 4008807..bc6710e 100644 --- a/gdb/compile/gcc-c-plugin.h +++ b/gdb/compile/gcc-c-plugin.h @@ -1,6 +1,6 @@ /* GCC C plug-in wrapper for GDB. - Copyright (C) 2018-2024 Free Software Foundation, Inc. + Copyright (C) 2018-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -17,8 +17,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef COMPILE_GCC_C_PLUGIN_H -#define COMPILE_GCC_C_PLUGIN_H +#ifndef GDB_COMPILE_GCC_C_PLUGIN_H +#define GDB_COMPILE_GCC_C_PLUGIN_H #include "compile-internal.h" @@ -68,4 +68,4 @@ private: struct gcc_c_context *m_context; }; -#endif /* COMPILE_GCC_C_PLUGIN_H */ +#endif /* GDB_COMPILE_GCC_C_PLUGIN_H */ diff --git a/gdb/compile/gcc-cp-plugin.h b/gdb/compile/gcc-cp-plugin.h index cf8ccd9..d926355 100644 --- a/gdb/compile/gcc-cp-plugin.h +++ b/gdb/compile/gcc-cp-plugin.h @@ -1,6 +1,6 @@ /* GCC C++ plug-in wrapper for GDB. - Copyright (C) 2018-2024 Free Software Foundation, Inc. + Copyright (C) 2018-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -17,8 +17,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef COMPILE_GCC_CP_PLUGIN_H -#define COMPILE_GCC_CP_PLUGIN_H +#ifndef GDB_COMPILE_GCC_CP_PLUGIN_H +#define GDB_COMPILE_GCC_CP_PLUGIN_H /* A class representing the GCC C++ plug-in. */ @@ -87,4 +87,4 @@ private: struct gcc_cp_context *m_context; }; -#endif /* COMPILE_GCC_CP_PLUGIN_H */ +#endif /* GDB_COMPILE_GCC_CP_PLUGIN_H */ |