15e098073Schristos /* Call module for 'compile' command.
25e098073Schristos 
3*1424dfb3Schristos    Copyright (C) 2014-2020 Free Software Foundation, Inc.
45e098073Schristos 
55e098073Schristos    This file is part of GDB.
65e098073Schristos 
75e098073Schristos    This program is free software; you can redistribute it and/or modify
85e098073Schristos    it under the terms of the GNU General Public License as published by
95e098073Schristos    the Free Software Foundation; either version 3 of the License, or
105e098073Schristos    (at your option) any later version.
115e098073Schristos 
125e098073Schristos    This program is distributed in the hope that it will be useful,
135e098073Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
145e098073Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155e098073Schristos    GNU General Public License for more details.
165e098073Schristos 
175e098073Schristos    You should have received a copy of the GNU General Public License
185e098073Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195e098073Schristos 
205e098073Schristos #include "defs.h"
215e098073Schristos #include "compile-object-run.h"
225e098073Schristos #include "value.h"
235e098073Schristos #include "infcall.h"
245e098073Schristos #include "objfiles.h"
255e098073Schristos #include "compile-internal.h"
265e098073Schristos #include "dummy-frame.h"
27ed6a76a9Schristos #include "block.h"
28ed6a76a9Schristos #include "valprint.h"
29ed6a76a9Schristos #include "compile.h"
305e098073Schristos 
315e098073Schristos /* Helper for do_module_cleanup.  */
325e098073Schristos 
335e098073Schristos struct do_module_cleanup
345e098073Schristos {
355e098073Schristos   /* Boolean to set true upon a call of do_module_cleanup.
365e098073Schristos      The pointer may be NULL.  */
375e098073Schristos   int *executedp;
385e098073Schristos 
395e098073Schristos   /* .c file OBJFILE was built from.  It needs to be xfree-d.  */
405e098073Schristos   char *source_file;
415e098073Schristos 
42ed6a76a9Schristos   /* Copy from struct compile_module.  */
43ed6a76a9Schristos   enum compile_i_scope_types scope;
44ed6a76a9Schristos   void *scope_data;
45ed6a76a9Schristos 
46ed6a76a9Schristos   /* Copy from struct compile_module.  */
47ed6a76a9Schristos   struct type *out_value_type;
48ed6a76a9Schristos   CORE_ADDR out_value_addr;
49ed6a76a9Schristos 
50ed6a76a9Schristos   /* Copy from struct compile_module.  */
51ed6a76a9Schristos   struct munmap_list *munmap_list_head;
52ed6a76a9Schristos 
535e098073Schristos   /* objfile_name of our objfile.  */
545e098073Schristos   char objfile_name_string[1];
555e098073Schristos };
565e098073Schristos 
575e098073Schristos /* Cleanup everything after the inferior function dummy frame gets
585e098073Schristos    discarded.  */
595e098073Schristos 
605e098073Schristos static dummy_frame_dtor_ftype do_module_cleanup;
615e098073Schristos static void
do_module_cleanup(void * arg,int registers_valid)62ed6a76a9Schristos do_module_cleanup (void *arg, int registers_valid)
635e098073Schristos {
64c03b94e9Schristos   struct do_module_cleanup *data = (struct do_module_cleanup *) arg;
655e098073Schristos 
665e098073Schristos   if (data->executedp != NULL)
67ed6a76a9Schristos     {
685e098073Schristos       *data->executedp = 1;
695e098073Schristos 
70ed6a76a9Schristos       /* This code cannot be in compile_object_run as OUT_VALUE_TYPE
71ed6a76a9Schristos 	 no longer exists there.  */
72ed6a76a9Schristos       if (data->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
73ed6a76a9Schristos 	  || data->scope == COMPILE_I_PRINT_VALUE_SCOPE)
74ed6a76a9Schristos 	{
75ed6a76a9Schristos 	  struct value *addr_value;
76ed6a76a9Schristos 	  struct type *ptr_type = lookup_pointer_type (data->out_value_type);
77ed6a76a9Schristos 
78ed6a76a9Schristos 	  addr_value = value_from_pointer (ptr_type, data->out_value_addr);
79ed6a76a9Schristos 
80*1424dfb3Schristos 	  /* SCOPE_DATA would be stale unless EXECUTEDP != NULL.  */
81ed6a76a9Schristos 	  compile_print_value (value_ind (addr_value), data->scope_data);
82ed6a76a9Schristos 	}
83ed6a76a9Schristos     }
84ed6a76a9Schristos 
8507163879Schristos   for (objfile *objfile : current_program_space->objfiles ())
865e098073Schristos     if ((objfile->flags & OBJF_USERLOADED) == 0
875e098073Schristos         && (strcmp (objfile_name (objfile), data->objfile_name_string) == 0))
885e098073Schristos       {
89*1424dfb3Schristos 	objfile->unlink ();
905e098073Schristos 
915e098073Schristos 	/* It may be a bit too pervasive in this dummy_frame dtor callback.  */
925e098073Schristos 	clear_symtab_users (0);
935e098073Schristos 
945e098073Schristos 	break;
955e098073Schristos       }
965e098073Schristos 
975e098073Schristos   /* Delete the .c file.  */
985e098073Schristos   unlink (data->source_file);
995e098073Schristos   xfree (data->source_file);
1005e098073Schristos 
10107163879Schristos   delete data->munmap_list_head;
102ed6a76a9Schristos 
1035e098073Schristos   /* Delete the .o file.  */
1045e098073Schristos   unlink (data->objfile_name_string);
1055e098073Schristos   xfree (data);
1065e098073Schristos }
1075e098073Schristos 
1085e098073Schristos /* Perform inferior call of MODULE.  This function may throw an error.
1095e098073Schristos    This function may leave files referenced by MODULE on disk until
1105e098073Schristos    the inferior call dummy frame is discarded.  This function may throw errors.
1115e098073Schristos    Thrown errors and left MODULE files are unrelated events.  Caller must no
1125e098073Schristos    longer touch MODULE's memory after this function has been called.  */
1135e098073Schristos 
1145e098073Schristos void
compile_object_run(struct compile_module * module)1155e098073Schristos compile_object_run (struct compile_module *module)
1165e098073Schristos {
1175e098073Schristos   struct value *func_val;
1185e098073Schristos   struct do_module_cleanup *data;
1195e098073Schristos   const char *objfile_name_s = objfile_name (module->objfile);
1205e098073Schristos   int dtor_found, executed = 0;
121ed6a76a9Schristos   struct symbol *func_sym = module->func_sym;
1225e098073Schristos   CORE_ADDR regs_addr = module->regs_addr;
123ed6a76a9Schristos   struct objfile *objfile = module->objfile;
1245e098073Schristos 
125c03b94e9Schristos   data = (struct do_module_cleanup *) xmalloc (sizeof (*data)
126c03b94e9Schristos 					       + strlen (objfile_name_s));
1275e098073Schristos   data->executedp = &executed;
1285e098073Schristos   data->source_file = xstrdup (module->source_file);
1295e098073Schristos   strcpy (data->objfile_name_string, objfile_name_s);
130ed6a76a9Schristos   data->scope = module->scope;
131ed6a76a9Schristos   data->scope_data = module->scope_data;
132ed6a76a9Schristos   data->out_value_type = module->out_value_type;
133ed6a76a9Schristos   data->out_value_addr = module->out_value_addr;
134ed6a76a9Schristos   data->munmap_list_head = module->munmap_list_head;
1355e098073Schristos 
1365e098073Schristos   xfree (module->source_file);
1375e098073Schristos   xfree (module);
138ed6a76a9Schristos   module = NULL;
1395e098073Schristos 
140*1424dfb3Schristos   try
1415e098073Schristos     {
142ed6a76a9Schristos       struct type *func_type = SYMBOL_TYPE (func_sym);
143ed6a76a9Schristos       htab_t copied_types;
144ed6a76a9Schristos       int current_arg = 0;
145ed6a76a9Schristos       struct value **vargs;
1465e098073Schristos 
147ed6a76a9Schristos       /* OBJFILE may disappear while FUNC_TYPE still will be in use.  */
148ed6a76a9Schristos       copied_types = create_copied_types_hash (objfile);
149ed6a76a9Schristos       func_type = copy_type_recursive (objfile, func_type, copied_types);
150ed6a76a9Schristos       htab_delete (copied_types);
151ed6a76a9Schristos 
152*1424dfb3Schristos       gdb_assert (func_type->code () == TYPE_CODE_FUNC);
153ed6a76a9Schristos       func_val = value_from_pointer (lookup_pointer_type (func_type),
15407163879Schristos 				   BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func_sym)));
155ed6a76a9Schristos 
156*1424dfb3Schristos       vargs = XALLOCAVEC (struct value *, func_type->num_fields ());
157*1424dfb3Schristos       if (func_type->num_fields () >= 1)
1585e098073Schristos 	{
159ed6a76a9Schristos 	  gdb_assert (regs_addr != 0);
160ed6a76a9Schristos 	  vargs[current_arg] = value_from_pointer
161*1424dfb3Schristos 			  (func_type->field (current_arg).type (), regs_addr);
162ed6a76a9Schristos 	  ++current_arg;
163ed6a76a9Schristos 	}
164*1424dfb3Schristos       if (func_type->num_fields () >= 2)
165ed6a76a9Schristos 	{
166ed6a76a9Schristos 	  gdb_assert (data->out_value_addr != 0);
167ed6a76a9Schristos 	  vargs[current_arg] = value_from_pointer
168*1424dfb3Schristos 	       (func_type->field (current_arg).type (), data->out_value_addr);
169ed6a76a9Schristos 	  ++current_arg;
170ed6a76a9Schristos 	}
171*1424dfb3Schristos       gdb_assert (current_arg == func_type->num_fields ());
172*1424dfb3Schristos       auto args = gdb::make_array_view (vargs, func_type->num_fields ());
17307163879Schristos       call_function_by_hand_dummy (func_val, NULL, args,
1745e098073Schristos 				   do_module_cleanup, data);
1755e098073Schristos     }
176*1424dfb3Schristos   catch (const gdb_exception_error &ex)
177ed6a76a9Schristos     {
178ed6a76a9Schristos       /* In the case of DTOR_FOUND or in the case of EXECUTED nothing
179ed6a76a9Schristos 	 needs to be done.  */
1805e098073Schristos       dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
1815e098073Schristos       if (!executed)
1825e098073Schristos 	data->executedp = NULL;
1835e098073Schristos       gdb_assert (!(dtor_found && executed));
1845e098073Schristos       if (!dtor_found && !executed)
185ed6a76a9Schristos 	do_module_cleanup (data, 0);
186*1424dfb3Schristos       throw;
1875e098073Schristos     }
188ed6a76a9Schristos 
189ed6a76a9Schristos   dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
190ed6a76a9Schristos   gdb_assert (!dtor_found && executed);
1915e098073Schristos }
192