GDB (xrefs)
Loading...
Searching...
No Matches
compile-cplus-symbols.c
Go to the documentation of this file.
1/* Convert symbols from GDB to GCC
2
3 Copyright (C) 2014-2023 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
21#include "defs.h"
22#include "compile-internal.h"
23#include "compile-cplus.h"
24#include "gdbsupport/gdb_assert.h"
25#include "symtab.h"
26#include "parser-defs.h"
27#include "block.h"
28#include "objfiles.h"
29#include "compile.h"
30#include "value.h"
31#include "exceptions.h"
32#include "gdbtypes.h"
33#include "dwarf2/loc.h"
34#include "cp-support.h"
35#include "gdbcmd.h"
36#include "compile-c.h"
37
38/* Convert a given symbol, SYM, to the compiler's representation.
39 INSTANCE is the compiler instance. IS_GLOBAL is true if the
40 symbol came from the global scope. IS_LOCAL is true if the symbol
41 came from a local scope. (Note that the two are not strictly
42 inverses because the symbol might have come from the static
43 scope.) */
44
45static void
47 struct block_symbol sym, bool is_global, bool is_local)
48{
49 /* Squash compiler warning. */
50 gcc_type sym_type = 0;
51 const char *filename = sym.symbol->symtab ()->filename;
52 unsigned int line = sym.symbol->line ();
53
54 instance->error_symbol_once (sym.symbol);
55
56 if (sym.symbol->aclass () == LOC_LABEL)
57 sym_type = 0;
58 else
59 sym_type = instance->convert_type (sym.symbol->type ());
60
61 if (sym.symbol->domain () == STRUCT_DOMAIN)
62 {
63 /* Nothing to do. */
64 }
65 else
66 {
67 /* Squash compiler warning. */
68 gcc_cp_symbol_kind_flags kind = GCC_CP_FLAG_BASE;
69 CORE_ADDR addr = 0;
70 std::string name;
71 gdb::unique_xmalloc_ptr<char> symbol_name;
72
73 switch (sym.symbol->aclass ())
74 {
75 case LOC_TYPEDEF:
76 if (sym.symbol->type ()->code () == TYPE_CODE_TYPEDEF)
77 kind = GCC_CP_SYMBOL_TYPEDEF;
78 else if (sym.symbol->type ()->code () == TYPE_CODE_NAMESPACE)
79 return;
80 break;
81
82 case LOC_LABEL:
83 kind = GCC_CP_SYMBOL_LABEL;
84 addr = sym.symbol->value_address ();
85 break;
86
87 case LOC_BLOCK:
88 {
89 kind = GCC_CP_SYMBOL_FUNCTION;
90 addr = sym.symbol->value_block()->start ();
91 if (is_global && sym.symbol->type ()->is_gnu_ifunc ())
92 addr = gnu_ifunc_resolve_addr (target_gdbarch (), addr);
93 }
94 break;
95
96 case LOC_CONST:
97 if (sym.symbol->type ()->code () == TYPE_CODE_ENUM)
98 {
99 /* Already handled by convert_enum. */
100 return;
101 }
102 instance->plugin ().build_constant
103 (sym_type, sym.symbol->natural_name (),
104 sym.symbol->value_longest (), filename, line);
105 return;
106
107 case LOC_CONST_BYTES:
108 error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."),
109 sym.symbol->print_name ());
110
111 case LOC_UNDEF:
112 internal_error (_("LOC_UNDEF found for \"%s\"."),
113 sym.symbol->print_name ());
114
115 case LOC_COMMON_BLOCK:
116 error (_("Fortran common block is unsupported for compilation "
117 "evaluaton of symbol \"%s\"."),
118 sym.symbol->print_name ());
119
121 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
122 "as it is optimized out."),
123 sym.symbol->print_name ());
124
125 case LOC_COMPUTED:
126 if (is_local)
127 goto substitution;
128 /* Probably TLS here. */
129 warning (_("Symbol \"%s\" is thread-local and currently can only "
130 "be referenced from the current thread in "
131 "compiled code."),
132 sym.symbol->print_name ());
133 /* FALLTHROUGH */
134 case LOC_UNRESOLVED:
135 /* 'symbol_name' cannot be used here as that one is used only for
136 local variables from compile_dwarf_expr_to_c.
137 Global variables can be accessed by GCC only by their address, not
138 by their name. */
139 {
140 struct value *val;
141 frame_info_ptr frame = nullptr;
142
144 {
145 frame = get_selected_frame (nullptr);
146 if (frame == nullptr)
147 error (_("Symbol \"%s\" cannot be used because "
148 "there is no selected frame"),
149 sym.symbol->print_name ());
150 }
151
152 val = read_var_value (sym.symbol, sym.block, frame);
153 if (val->lval () != lval_memory)
154 error (_("Symbol \"%s\" cannot be used for compilation "
155 "evaluation as its address has not been found."),
156 sym.symbol->print_name ());
157
158 kind = GCC_CP_SYMBOL_VARIABLE;
159 addr = val->address ();
160 }
161 break;
162
163
164 case LOC_REGISTER:
165 case LOC_ARG:
166 case LOC_REF_ARG:
167 case LOC_REGPARM_ADDR:
168 case LOC_LOCAL:
169 substitution:
170 kind = GCC_CP_SYMBOL_VARIABLE;
171 symbol_name = c_symbol_substitution_name (sym.symbol);
172 break;
173
174 case LOC_STATIC:
175 kind = GCC_CP_SYMBOL_VARIABLE;
176 addr = sym.symbol->value_address ();
177 break;
178
179 case LOC_FINAL_VALUE:
180 default:
181 gdb_assert_not_reached ("Unreachable case in convert_one_symbol.");
182 }
183
184 /* Don't emit local variable decls for a raw expression. */
185 if (instance->scope () != COMPILE_I_RAW_SCOPE || symbol_name == nullptr)
186 {
187 /* For non-local symbols, create/push a new scope so that the
188 symbol is properly scoped to the plug-in. */
189 if (!is_local)
190 {
191 compile_scope scope
192 = instance->new_scope (sym.symbol->natural_name (),
193 sym.symbol->type ());
194 if (scope.nested_type () != GCC_TYPE_NONE)
195 {
196 /* We found a symbol for this type that was defined inside
197 some other symbol, e.g., a class typedef defined. */
198 return;
199 }
200
201 instance->enter_scope (std::move (scope));
202 }
203
204 /* Get the `raw' name of the symbol. */
205 if (name.empty () && sym.symbol->natural_name () != nullptr)
207 (sym.symbol->natural_name ()).get ();
208
209 /* Define the decl. */
210 instance->plugin ().build_decl
211 ("variable", name.c_str (), kind.raw (), sym_type,
212 symbol_name.get (), addr, filename, line);
213
214 /* Pop scope for non-local symbols. */
215 if (!is_local)
216 instance->leave_scope ();
217 }
218 }
219}
220
221/* Convert a full symbol to its gcc form. CONTEXT is the compiler to
222 use, IDENTIFIER is the name of the symbol, SYM is the symbol
223 itself, and DOMAIN is the domain which was searched. */
224
225static void
227 const char *identifier, struct block_symbol sym,
228 domain_enum domain)
229{
230 /* If we found a symbol and it is not in the static or global
231 scope, then we should first convert any static or global scope
232 symbol of the same name. This lets this unusual case work:
233
234 int x; // Global.
235 int func(void)
236 {
237 int x;
238 // At this spot, evaluate "extern int x; x"
239 }
240 */
241
242 const struct block *static_block = nullptr;
243 if (sym.block != nullptr)
245 /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block. */
246 bool is_local_symbol = (sym.block != static_block && static_block != nullptr);
247 if (is_local_symbol)
248 {
249 struct block_symbol global_sym;
250
251 global_sym = lookup_symbol (identifier, nullptr, domain, nullptr);
252 /* If the outer symbol is in the static block, we ignore it, as
253 it cannot be referenced. */
254 if (global_sym.symbol != nullptr
255 && global_sym.block != global_sym.block->static_block ())
256 {
257 if (compile_debug)
259 "gcc_convert_symbol \"%s\": global symbol\n",
260 identifier);
261 convert_one_symbol (instance, global_sym, true, false);
262 }
263 }
264
265 if (compile_debug)
267 "gcc_convert_symbol \"%s\": local symbol\n",
268 identifier);
269 convert_one_symbol (instance, sym, false, is_local_symbol);
270}
271
272/* Convert a minimal symbol to its gcc form. CONTEXT is the compiler
273 to use and BMSYM is the minimal symbol to convert. */
274
275static void
277 struct bound_minimal_symbol bmsym)
278{
279 struct minimal_symbol *msym = bmsym.minsym;
280 struct objfile *objfile = bmsym.objfile;
281 struct type *type;
282 gcc_cp_symbol_kind_flags kind;
283 gcc_type sym_type;
284 CORE_ADDR addr;
285
286 addr = msym->value_address (objfile);
287
288 /* Conversion copied from write_exp_msymbol. */
289 switch (msym->type ())
290 {
291 case mst_text:
292 case mst_file_text:
295 kind = GCC_CP_SYMBOL_FUNCTION;
296 break;
297
299 /* nodebug_text_gnu_ifunc_symbol would cause:
300 function return type cannot be function */
302 kind = GCC_CP_SYMBOL_FUNCTION;
303 addr = gnu_ifunc_resolve_addr (target_gdbarch (), addr);
304 break;
305
306 case mst_data:
307 case mst_file_data:
308 case mst_bss:
309 case mst_file_bss:
311 kind = GCC_CP_SYMBOL_VARIABLE;
312 break;
313
314 case mst_slot_got_plt:
316 kind = GCC_CP_SYMBOL_FUNCTION;
317 break;
318
319 default:
321 kind = GCC_CP_SYMBOL_VARIABLE;
322 break;
323 }
324
325 sym_type = instance->convert_type (type);
326 instance->plugin ().push_namespace ("");
327 instance->plugin ().build_decl
328 ("minsym", msym->natural_name (), kind.raw (), sym_type, nullptr, addr,
329 nullptr, 0);
330 instance->plugin ().pop_binding_level ("");
331}
332
333/* See compile-cplus.h. */
334
335void
337 struct gcc_cp_context *gcc_context,
338 enum gcc_cp_oracle_request request,
339 const char *identifier)
340{
341 if (compile_debug)
343 "got oracle request for \"%s\"\n", identifier);
344
345 bool found = false;
347
348 try
349 {
350 /* Symbol searching is a three part process unfortunately. */
351
352 /* First do a "standard" lookup, converting any found symbols.
353 This will find variables in the current scope. */
354
355 struct block_symbol sym
356 = lookup_symbol (identifier, instance->block (), VAR_DOMAIN, nullptr);
357
358 if (sym.symbol != nullptr)
359 {
360 found = true;
361 convert_symbol_sym (instance, identifier, sym, VAR_DOMAIN);
362 }
363
364 /* Then use linespec.c's multi-symbol search. This should find
365 all non-variable symbols for which we have debug info. */
366
367 symbol_searcher searcher;
368 searcher.find_all_symbols (identifier, current_language,
369 ALL_DOMAIN, nullptr, nullptr);
370
371 /* Convert any found symbols. */
372 for (const auto &it : searcher.matching_symbols ())
373 {
374 /* Don't convert the symbol found above, if any, twice! */
375 if (it.symbol != sym.symbol)
376 {
377 found = true;
378 convert_symbol_sym (instance, identifier, it,
379 it.symbol->domain ());
380 }
381 }
382
383 /* Finally, if no symbols have been found, fall back to minsyms. */
384 if (!found)
385 {
386 for (const auto &it : searcher.matching_msymbols ())
387 {
388 found = true;
389 convert_symbol_bmsym (instance, it);
390 }
391 }
392 }
393 catch (const gdb_exception &e)
394 {
395 /* We can't allow exceptions to escape out of this callback. Safest
396 is to simply emit a gcc error. */
397 instance->plugin ().error (e.what ());
398 }
399
400 if (compile_debug && !found)
402 "gcc_convert_symbol \"%s\": lookup_symbol failed\n",
403 identifier);
404
405 if (compile_debug)
406 {
407 if (found)
408 gdb_printf (gdb_stdlog, "found type for %s\n", identifier);
409 else
410 {
411 gdb_printf (gdb_stdlog, "did not find type for %s\n",
412 identifier);
413 }
414 }
415
416 return;
417}
418
419/* See compile-cplus.h. */
420
421gcc_address
422gcc_cplus_symbol_address (void *datum, struct gcc_cp_context *gcc_context,
423 const char *identifier)
424{
426 gcc_address result = 0;
427 int found = 0;
428
429 if (compile_debug)
431 "got oracle request for address of %s\n", identifier);
432
433 /* We can't allow exceptions to escape out of this callback. Safest
434 is to simply emit a gcc error. */
435 try
436 {
437 struct symbol *sym
438 = lookup_symbol (identifier, nullptr, VAR_DOMAIN, nullptr).symbol;
439
440 if (sym != nullptr && sym->aclass () == LOC_BLOCK)
441 {
442 if (compile_debug)
444 "gcc_symbol_address \"%s\": full symbol\n",
445 identifier);
446 result = sym->value_block ()->start ();
447 if (sym->type ()->is_gnu_ifunc ())
448 result = gnu_ifunc_resolve_addr (target_gdbarch (), result);
449 found = 1;
450 }
451 else
452 {
453 struct bound_minimal_symbol msym;
454
455 msym = lookup_bound_minimal_symbol (identifier);
456 if (msym.minsym != nullptr)
457 {
458 if (compile_debug)
460 "gcc_symbol_address \"%s\": minimal "
461 "symbol\n",
462 identifier);
463 result = msym.value_address ();
464 if (msym.minsym->type () == mst_text_gnu_ifunc)
465 result = gnu_ifunc_resolve_addr (target_gdbarch (), result);
466 found = 1;
467 }
468 }
469 }
470
471 catch (const gdb_exception_error &e)
472 {
473 instance->plugin ().error (e.what ());
474 }
475
476 if (compile_debug && !found)
478 "gcc_symbol_address \"%s\": failed\n",
479 identifier);
480
481 if (compile_debug)
482 {
483 if (found)
484 gdb_printf (gdb_stdlog, "found address for %s!\n", identifier);
485 else
487 "did not find address for %s\n", identifier);
488 }
489
490 return result;
491}
const char *const name
struct gdbarch * target_gdbarch(void)
compile_scope new_scope(const char *type_name, struct type *type)
gcc_type convert_type(struct type *type, enum gcc_cp_symbol_kind nested_access=GCC_CP_ACCESS_NONE)
static gdb::unique_xmalloc_ptr< char > decl_name(const char *natural)
void enter_scope(compile_scope &&scope)
gcc_cp_plugin & plugin()
void error_symbol_once(const struct symbol *sym)
Definition compile.c:225
enum compile_i_scope_types scope() const
Definition compile.h:105
const struct block * block() const
Definition compile.h:117
gcc_type nested_type()
int pop_binding_level(const char *debug_name)
gcc_expr build_decl(const char *debug_decltype, const char *name, enum gcc_cp_symbol_kind sym_kind, gcc_type sym_type, const char *substitution_name, gcc_address address, const char *filename, unsigned int line_number)
const std::vector< block_symbol > & matching_symbols() const
Definition symtab.h:2832
void find_all_symbols(const std::string &name, const struct language_defn *language, enum search_domain search_domain, std::vector< symtab * > *search_symtabs, struct program_space *search_pspace)
Definition linespec.c:3746
const std::vector< bound_minimal_symbol > & matching_msymbols() const
Definition symtab.h:2839
gdb::unique_xmalloc_ptr< char > c_symbol_substitution_name(struct symbol *sym)
static void convert_symbol_bmsym(compile_cplus_instance *instance, struct bound_minimal_symbol bmsym)
static void convert_one_symbol(compile_cplus_instance *instance, struct block_symbol sym, bool is_global, bool is_local)
static void convert_symbol_sym(compile_cplus_instance *instance, const char *identifier, struct block_symbol sym, domain_enum domain)
gcc_cp_oracle_function gcc_cplus_convert_symbol
gcc_cp_symbol_address_function gcc_cplus_symbol_address
const gcc_type GCC_TYPE_NONE
bool compile_debug
Definition compile.c:61
@ COMPILE_I_RAW_SCOPE
Definition defs.h:81
@ lval_memory
Definition defs.h:363
int symbol_read_needs_frame(struct symbol *sym)
Definition findvar.c:388
struct value * read_var_value(struct symbol *var, const struct block *var_block, frame_info_ptr frame)
Definition findvar.c:739
frame_info_ptr get_selected_frame(const char *message)
Definition frame.c:1888
const struct builtin_type * builtin_type(struct gdbarch *gdbarch)
Definition gdbtypes.c:6168
const struct language_defn * current_language
Definition language.c:82
struct bound_minimal_symbol lookup_bound_minimal_symbol(const char *name)
Definition minsyms.c:481
enum var_types type
Definition scm-param.c:142
const struct block * block
Definition symtab.h:1537
struct symbol * symbol
Definition symtab.h:1533
Definition block.h:109
CORE_ADDR start() const
Definition block.h:111
const struct block * static_block() const
Definition block.c:354
struct objfile * objfile
Definition minsyms.h:54
CORE_ADDR value_address() const
Definition minsyms.h:41
struct minimal_symbol * minsym
Definition minsyms.h:49
struct type * nodebug_got_plt_symbol
Definition gdbtypes.h:2172
struct type * nodebug_data_symbol
Definition gdbtypes.h:2173
struct type * nodebug_unknown_symbol
Definition gdbtypes.h:2174
struct type * nodebug_text_symbol
Definition gdbtypes.h:2170
const char * natural_name() const
Definition symtab.c:1056
const char * print_name() const
Definition symtab.h:475
minimal_symbol_type type() const
Definition symtab.h:770
CORE_ADDR value_address(objfile *objfile) const
Definition symtab.c:439
const block * value_block() const
Definition symtab.h:1549
address_class aclass() const
Definition symtab.h:1274
struct type * type() const
Definition symtab.h:1331
domain_enum domain() const
Definition symtab.h:1286
LONGEST value_longest() const
Definition symtab.h:1351
unsigned int line() const
Definition symtab.h:1341
CORE_ADDR value_address() const
Definition symtab.h:1361
struct symtab * symtab
Definition symtab.h:1457
const char * filename
Definition symtab.h:1725
type_code code() const
Definition gdbtypes.h:956
bool is_gnu_ifunc() const
Definition gdbtypes.h:1216
Definition value.h:130
enum lval_type lval() const
Definition value.h:332
CORE_ADDR address
Definition value.h:658
struct block_symbol lookup_symbol(const char *name, const struct block *block, domain_enum domain, struct field_of_this_result *is_a_field_of_this)
Definition symtab.c:1964
@ ALL_DOMAIN
Definition symtab.h:960
@ LOC_STATIC
Definition symtab.h:979
@ LOC_BLOCK
Definition symtab.h:1028
@ LOC_LABEL
Definition symtab.h:1022
@ LOC_REGISTER
Definition symtab.h:993
@ LOC_REF_ARG
Definition symtab.h:1001
@ LOC_UNRESOLVED
Definition symtab.h:1057
@ LOC_LOCAL
Definition symtab.h:1013
@ LOC_CONST
Definition symtab.h:975
@ LOC_CONST_BYTES
Definition symtab.h:1033
@ LOC_UNDEF
Definition symtab.h:971
@ LOC_OPTIMIZED_OUT
Definition symtab.h:1062
@ LOC_TYPEDEF
Definition symtab.h:1018
@ LOC_FINAL_VALUE
Definition symtab.h:1073
@ LOC_REGPARM_ADDR
Definition symtab.h:1009
@ LOC_COMMON_BLOCK
Definition symtab.h:1070
@ LOC_COMPUTED
Definition symtab.h:1066
@ LOC_ARG
Definition symtab.h:997
@ mst_bss
Definition symtab.h:695
@ mst_data
Definition symtab.h:694
@ mst_solib_trampoline
Definition symtab.h:705
@ mst_file_text
Definition symtab.h:708
@ mst_slot_got_plt
Definition symtab.h:693
@ mst_file_data
Definition symtab.h:709
@ mst_text
Definition symtab.h:672
@ mst_text_gnu_ifunc
Definition symtab.h:681
@ mst_file_bss
Definition symtab.h:710
domain_enum
Definition symtab.h:900
@ VAR_DOMAIN
Definition symtab.h:910
@ STRUCT_DOMAIN
Definition symtab.h:916
#define gnu_ifunc_resolve_addr
Definition symtab.h:2313
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stdlog
Definition utils.h:190