]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/compile/compile-object-run.c
f355624d91c27180e894713c975c723a2151eec2
[thirdparty/binutils-gdb.git] / gdb / compile / compile-object-run.c
1 /* Call module for 'compile' command.
2
3 Copyright (C) 2014-2020 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "compile-object-run.h"
22 #include "value.h"
23 #include "infcall.h"
24 #include "objfiles.h"
25 #include "compile-internal.h"
26 #include "dummy-frame.h"
27 #include "block.h"
28 #include "valprint.h"
29 #include "compile.h"
30
31 /* Helper for do_module_cleanup. */
32
33 struct do_module_cleanup
34 {
35 do_module_cleanup (int *ptr, compile_module_up &&mod)
36 : executedp (ptr),
37 module (std::move (mod))
38 {
39 }
40
41 DISABLE_COPY_AND_ASSIGN (do_module_cleanup);
42
43 /* Boolean to set true upon a call of do_module_cleanup.
44 The pointer may be NULL. */
45 int *executedp;
46
47 /* The compile module. */
48 compile_module_up module;
49 };
50
51 /* Cleanup everything after the inferior function dummy frame gets
52 discarded. */
53
54 static dummy_frame_dtor_ftype do_module_cleanup;
55 static void
56 do_module_cleanup (void *arg, int registers_valid)
57 {
58 struct do_module_cleanup *data = (struct do_module_cleanup *) arg;
59
60 if (data->executedp != NULL)
61 {
62 *data->executedp = 1;
63
64 /* This code cannot be in compile_object_run as OUT_VALUE_TYPE
65 no longer exists there. */
66 if (data->module->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
67 || data->module->scope == COMPILE_I_PRINT_VALUE_SCOPE)
68 {
69 struct value *addr_value;
70 struct type *ptr_type
71 = lookup_pointer_type (data->module->out_value_type);
72
73 addr_value = value_from_pointer (ptr_type,
74 data->module->out_value_addr);
75
76 /* SCOPE_DATA would be stale unless EXECUTEDP != NULL. */
77 compile_print_value (value_ind (addr_value),
78 data->module->scope_data);
79 }
80 }
81
82 /* We have to make a copy of the name so that we can unlink the
83 underlying file -- removing the objfile will cause the name to be
84 freed, so we can't simply keep a reference to it. */
85 std::string objfile_name_s = objfile_name (data->module->objfile);
86 for (objfile *objfile : current_program_space->objfiles ())
87 if ((objfile->flags & OBJF_USERLOADED) == 0
88 && objfile_name_s == objfile_name (objfile))
89 {
90 objfile->unlink ();
91
92 /* It may be a bit too pervasive in this dummy_frame dtor callback. */
93 clear_symtab_users (0);
94
95 break;
96 }
97
98 /* Delete the .c file. */
99 unlink (data->module->source_file.c_str ());
100
101 /* Delete the .o file. */
102 unlink (objfile_name_s.c_str ());
103
104 delete data;
105 }
106
107 /* Create a copy of FUNC_TYPE that is independent of OBJFILE. */
108
109 static type *
110 create_copied_type_recursive (objfile *objfile, type *func_type)
111 {
112 htab_up copied_types = create_copied_types_hash (objfile);
113 func_type = copy_type_recursive (objfile, func_type, copied_types.get ());
114 return func_type;
115 }
116
117 /* Perform inferior call of MODULE. This function may throw an error.
118 This function may leave files referenced by MODULE on disk until
119 the inferior call dummy frame is discarded. This function may throw errors.
120 Thrown errors and left MODULE files are unrelated events. Caller must no
121 longer touch MODULE's memory after this function has been called. */
122
123 void
124 compile_object_run (compile_module_up &&module)
125 {
126 struct value *func_val;
127 struct do_module_cleanup *data;
128 int dtor_found, executed = 0;
129 struct symbol *func_sym = module->func_sym;
130 CORE_ADDR regs_addr = module->regs_addr;
131 struct objfile *objfile = module->objfile;
132
133 data = new struct do_module_cleanup (&executed, std::move (module));
134
135 try
136 {
137 struct type *func_type = SYMBOL_TYPE (func_sym);
138 int current_arg = 0;
139 struct value **vargs;
140
141 /* OBJFILE may disappear while FUNC_TYPE is still in use as a
142 result of the call to DO_MODULE_CLEANUP below, so we need a copy
143 that does not depend on the objfile in anyway. */
144 func_type = create_copied_type_recursive (objfile, func_type);
145
146 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
147 func_val = value_from_pointer (lookup_pointer_type (func_type),
148 BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (func_sym)));
149
150 vargs = XALLOCAVEC (struct value *, func_type->num_fields ());
151 if (func_type->num_fields () >= 1)
152 {
153 gdb_assert (regs_addr != 0);
154 vargs[current_arg] = value_from_pointer
155 (func_type->field (current_arg).type (), regs_addr);
156 ++current_arg;
157 }
158 if (func_type->num_fields () >= 2)
159 {
160 gdb_assert (data->module->out_value_addr != 0);
161 vargs[current_arg] = value_from_pointer
162 (func_type->field (current_arg).type (),
163 data->module->out_value_addr);
164 ++current_arg;
165 }
166 gdb_assert (current_arg == func_type->num_fields ());
167 auto args = gdb::make_array_view (vargs, func_type->num_fields ());
168 call_function_by_hand_dummy (func_val, NULL, args,
169 do_module_cleanup, data);
170 }
171 catch (const gdb_exception_error &ex)
172 {
173 /* In the case of DTOR_FOUND or in the case of EXECUTED nothing
174 needs to be done. */
175 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
176 if (!executed)
177 data->executedp = NULL;
178 gdb_assert (!(dtor_found && executed));
179 if (!dtor_found && !executed)
180 do_module_cleanup (data, 0);
181 throw;
182 }
183
184 dtor_found = find_dummy_frame_dtor (do_module_cleanup, data);
185 gdb_assert (!dtor_found && executed);
186 }