[PATCH v2 6/9] Code cleanup: compile: func_addr -> func_sym

Jan Kratochvil
2015-04-06T17:27:15Z

Hi,

currently the code fetches _gdb_expr address/types at multiple places, guessing
its parameters at multiple places etc.

Fetch it once, verify it has expected type and then rely on it.

While the patch tries to clean up the code it is still horrible due to the
missing C++ sub-classing.

Jan

gdb/ChangeLog
2015-04-06 Jan Kratochvil <jan.kratochvil@redhat.com>

* compile/compile-object-load.c (get_regs_type): Add parameter func_sym.
Rely on its parameter count.
(compile_object_load): Replace lookup_minimal_symbol_text by
lookup_global_symbol_from_objfile. Verify FUNC_SYM. Set it in the
return value.
* compile/compile-object-load.h (struct compile_module): Replace
func_addr by func_sym.
* compile/compile-object-run.c: Include block.h.
(compile_object_run): Reset module variable after it is freed. Use
FUNC_SYM instead of FUNC_ADDR. Rely on it.
---
gdb/compile/compile-object-load.c | 81 +++++++++++++++++++++----------------
gdb/compile/compile-object-load.h | 4 +-
gdb/compile/compile-object-run.c | 43 ++++++++++++--------
3 files changed, 76 insertions(+), 52 deletions(-)

diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c
index ed2cad4..3c07840 100644
--- a/gdb/compile/compile-object-load.c
+++ b/gdb/compile/compile-object-load.c
@@ -354,40 +354,19 @@ copy_sections (bfd *abfd, asection *sect, void *data)
do_cleanups (cleanups);
}

-/* Fetch the type of first parameter of GCC_FE_WRAPPER_FUNCTION.
- Return NULL if GCC_FE_WRAPPER_FUNCTION has no parameters.
- Throw an error otherwise. */
+/* Fetch the type of first parameter of FUNC_SYM.
+ Return NULL if FUNC_SYM has no parameters. Throw an error otherwise. */

static struct type *
-get_regs_type (struct objfile *objfile)
+get_regs_type (struct symbol *func_sym, struct objfile *objfile)
{
- struct symbol *func_sym;
- struct type *func_type, *regsp_type, *regs_type;
-
- func_sym = lookup_global_symbol_from_objfile (objfile,
- GCC_FE_WRAPPER_FUNCTION,
- VAR_DOMAIN);
- if (func_sym == NULL)
- error (_("Cannot find function \"%s\" in compiled module \"%s\"."),
- GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
-
- func_type = SYMBOL_TYPE (func_sym);
- if (TYPE_CODE (func_type) != TYPE_CODE_FUNC)
- error (_("Invalid type code %d of function \"%s\" in compiled "
- "module \"%s\"."),
- TYPE_CODE (func_type), GCC_FE_WRAPPER_FUNCTION,
- objfile_name (objfile));
+ struct type *func_type = SYMBOL_TYPE (func_sym);
+ struct type *regsp_type, *regs_type;

/* No register parameter present. */
if (TYPE_NFIELDS (func_type) == 0)
return NULL;

- if (TYPE_NFIELDS (func_type) != 1)
- error (_("Invalid %d parameters of function \"%s\" in compiled "
- "module \"%s\"."),
- TYPE_NFIELDS (func_type), GCC_FE_WRAPPER_FUNCTION,
- objfile_name (objfile));
-
regsp_type = check_typedef (TYPE_FIELD_TYPE (func_type, 0));
if (TYPE_CODE (regsp_type) != TYPE_CODE_PTR)
error (_("Invalid type code %d of first parameter of function \"%s\" "
@@ -470,7 +449,9 @@ compile_object_load (const char *object_file, const char *source_file,
struct cleanup *cleanups, *cleanups_free_objfile;
bfd *abfd;
struct setup_sections_data setup_sections_data;
- CORE_ADDR addr, func_addr, regs_addr;
+ CORE_ADDR addr, regs_addr;
+ struct symbol *func_sym;
+ struct type *func_type;
struct bound_minimal_symbol bmsym;
long storage_needed;
asymbol **symbol_table, **symp;
@@ -481,6 +462,8 @@ compile_object_load (const char *object_file, const char *source_file,
struct type *regs_type;
char *filename, **matching;
struct objfile *objfile;
+ int expect_parameters;
+ struct type *expect_return_type;

filename = tilde_expand (object_file);
cleanups = make_cleanup (xfree, filename);
@@ -515,11 +498,41 @@ compile_object_load (const char *object_file, const char *source_file,
objfile = symbol_file_add_from_bfd (abfd, filename, 0, NULL, 0, NULL);
cleanups_free_objfile = make_cleanup_free_objfile (objfile);

- bmsym = lookup_minimal_symbol_text (GCC_FE_WRAPPER_FUNCTION, objfile);
- if (bmsym.minsym == NULL || MSYMBOL_TYPE (bmsym.minsym) == mst_file_text)
- error (_("Could not find symbol \"%s\" of compiled module \"%s\"."),
- GCC_FE_WRAPPER_FUNCTION, filename);
- func_addr = BMSYMBOL_VALUE_ADDRESS (bmsym);
+ func_sym = lookup_global_symbol_from_objfile (objfile,
+ GCC_FE_WRAPPER_FUNCTION,
+ VAR_DOMAIN);
+ if (func_sym == NULL)
+ error (_("Cannot find function \"%s\" in compiled module \"%s\"."),
+ GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
+ func_type = SYMBOL_TYPE (func_sym);
+ if (TYPE_CODE (func_type) != TYPE_CODE_FUNC)
+ error (_("Invalid type code %d of function \"%s\" in compiled "
+ "module \"%s\"."),
+ TYPE_CODE (func_type), GCC_FE_WRAPPER_FUNCTION,
+ objfile_name (objfile));
+
+ switch (scope)
+ {
+ case COMPILE_I_SIMPLE_SCOPE:
+ expect_parameters = 1;
+ expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
+ break;
+ case COMPILE_I_RAW_SCOPE:
+ expect_parameters = 0;
+ expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
+ break;
+ default:
+ internal_error (__FILE__, __LINE__, _("invalid scope %d"), scope);
+ }
+ if (TYPE_NFIELDS (func_type) != expect_parameters)
+ error (_("Invalid %d parameters of function \"%s\" in compiled "
+ "module \"%s\"."),
+ TYPE_NFIELDS (func_type), GCC_FE_WRAPPER_FUNCTION,
+ objfile_name (objfile));
+ if (!types_deeply_equal (expect_return_type, TYPE_TARGET_TYPE (func_type)))
+ error (_("Invalid return type of function \"%s\" in compiled "
+ "module \"%s\"."),
+ GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));

/* The memory may be later needed
by bfd_generic_get_relocated_section_contents
@@ -571,7 +584,7 @@ compile_object_load (const char *object_file, const char *source_file,

bfd_map_over_sections (abfd, copy_sections, symbol_table);

- regs_type = get_regs_type (objfile);
+ regs_type = get_regs_type (func_sym, objfile);
if (regs_type == NULL)
regs_addr = 0;
else
@@ -590,7 +603,7 @@ compile_object_load (const char *object_file, const char *source_file,
retval = xmalloc (sizeof (*retval));
retval->objfile = objfile;
retval->source_file = xstrdup (source_file);
- retval->func_addr = func_addr;
+ retval->func_sym = func_sym;
retval->regs_addr = regs_addr;
retval->scope = scope;
retval->scope_data = scope_data;
diff --git a/gdb/compile/compile-object-load.h b/gdb/compile/compile-object-load.h
index 4f9493b..94f77be 100644
--- a/gdb/compile/compile-object-load.h
+++ b/gdb/compile/compile-object-load.h
@@ -25,8 +25,8 @@ struct compile_module
/* .c file OBJFILE was built from. It needs to be xfree-d. */
char *source_file;

- /* Inferior function address. */
- CORE_ADDR func_addr;
+ /* Inferior function GCC_FE_WRAPPER_FUNCTION. */
+ struct symbol *func_sym;

/* Inferior registers address or NULL if the inferior function does not
require any. */
diff --git a/gdb/compile/compile-object-run.c b/gdb/compile/compile-object-run.c
index bfd2473..c454bd1 100644
--- a/gdb/compile/compile-object-run.c
+++ b/gdb/compile/compile-object-run.c
@@ -24,6 +24,7 @@
#include "objfiles.h"
#include "compile-internal.h"
#include "dummy-frame.h"
+#include "block.h"

/* Helper for do_module_cleanup. */

@@ -93,8 +94,9 @@ compile_object_run (struct compile_module *module)
struct do_module_cleanup *data;
const char *objfile_name_s = objfile_name (module->objfile);
int dtor_found, executed = 0;
- CORE_ADDR func_addr = module->func_addr;
+ struct symbol *func_sym = module->func_sym;
CORE_ADDR regs_addr = module->regs_addr;
+ struct objfile *objfile = module->objfile;

data = xmalloc (sizeof (*data) + strlen (objfile_name_s));
data->executedp = &executed;
@@ -105,26 +107,35 @@ compile_object_run (struct compile_module *module)

xfree (module->source_file);
xfree (module);
+ module = NULL;

TRY
{
- func_val = value_from_pointer
- (builtin_type (target_gdbarch ())->builtin_func_ptr,
- func_addr);
-
- if (regs_addr == 0)
- call_function_by_hand_dummy (func_val, 0, NULL,
- do_module_cleanup, data);
- else
+ struct type *func_type = SYMBOL_TYPE (func_sym);
+ htab_t copied_types;
+ int current_arg = 0;
+ struct value **vargs;
+
+ /* OBJFILE may disappear while FUNC_TYPE still will be in use. */
+ copied_types = create_copied_types_hash (objfile);
+ func_type = copy_type_recursive (objfile, func_type, copied_types);
+ htab_delete (copied_types);
+
+ gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
+ func_val = value_from_pointer (lookup_pointer_type (func_type),
+ BLOCK_START (SYMBOL_BLOCK_VALUE (func_sym)));
+
+ vargs = alloca (sizeof (*vargs) * TYPE_NFIELDS (func_type));
+ if (TYPE_NFIELDS (func_type) >= 1)
{
- struct value *arg_val;
-
- arg_val = value_from_pointer
- (builtin_type (target_gdbarch ())->builtin_func_ptr,
- regs_addr);
- call_function_by_hand_dummy (func_val, 1, &arg_val,
- do_module_cleanup, data);
+ gdb_assert (regs_addr != 0);
+ vargs[current_arg] = value_from_pointer
+ (TYPE_FIELD_TYPE (func_type, current_arg), regs_addr);
+ ++current_arg;
}
+ gdb_assert (current_arg == TYPE_NFIELDS (func_type));
+ call_function_by_hand_dummy (func_val, TYPE_NFIELDS (func_type), vargs,
+ do_module_cleanup, data);
}
CATCH (ex, RETURN_MASK_ERROR)
{