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 short 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 (VALUE_LVAL (val) != 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 = value_address (val);
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 = block_static_block (sym.block);
243 /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block. */
244 bool is_local_symbol = (sym.block != static_block && static_block != nullptr);
245 if (is_local_symbol)
246 {
247 struct block_symbol global_sym;
248
249 global_sym = lookup_symbol (identifier, nullptr, domain, nullptr);
250 /* If the outer symbol is in the static block, we ignore it, as
251 it cannot be referenced. */
252 if (global_sym.symbol != nullptr
253 && global_sym.block != block_static_block (global_sym.block))
254 {
255 if (compile_debug)
257 "gcc_convert_symbol \"%s\": global symbol\n",
258 identifier);
259 convert_one_symbol (instance, global_sym, true, false);
260 }
261 }
262
263 if (compile_debug)
265 "gcc_convert_symbol \"%s\": local symbol\n",
266 identifier);
267 convert_one_symbol (instance, sym, false, is_local_symbol);
268}
269
270/* Convert a minimal symbol to its gcc form. CONTEXT is the compiler
271 to use and BMSYM is the minimal symbol to convert. */
272
273static void
275 struct bound_minimal_symbol bmsym)
276{
277 struct minimal_symbol *msym = bmsym.minsym;
278 struct objfile *objfile = bmsym.objfile;
279 struct type *type;
280 gcc_cp_symbol_kind_flags kind;
281 gcc_type sym_type;
282 CORE_ADDR addr;
283
284 addr = msym->value_address (objfile);
285
286 /* Conversion copied from write_exp_msymbol. */
287 switch (msym->type ())
288 {
289 case mst_text:
290 case mst_file_text:
293 kind = GCC_CP_SYMBOL_FUNCTION;
294 break;
295
297 /* nodebug_text_gnu_ifunc_symbol would cause:
298 function return type cannot be function */
300 kind = GCC_CP_SYMBOL_FUNCTION;
301 addr = gnu_ifunc_resolve_addr (target_gdbarch (), addr);
302 break;
303
304 case mst_data:
305 case mst_file_data:
306 case mst_bss:
307 case mst_file_bss:
309 kind = GCC_CP_SYMBOL_VARIABLE;
310 break;
311
312 case mst_slot_got_plt:
314 kind = GCC_CP_SYMBOL_FUNCTION;
315 break;
316
317 default:
319 kind = GCC_CP_SYMBOL_VARIABLE;
320 break;
321 }
322
323 sym_type = instance->convert_type (type);
324 instance->plugin ().push_namespace ("");
325 instance->plugin ().build_decl
326 ("minsym", msym->natural_name (), kind.raw (), sym_type, nullptr, addr,
327 nullptr, 0);
328 instance->plugin ().pop_binding_level ("");
329}
330
331/* See compile-cplus.h. */
332
333void
335 struct gcc_cp_context *gcc_context,
336 enum gcc_cp_oracle_request request,
337 const char *identifier)
338{
339 if (compile_debug)
341 "got oracle request for \"%s\"\n", identifier);
342
343 bool found = false;
345
346 try
347 {
348 /* Symbol searching is a three part process unfortunately. */
349
350 /* First do a "standard" lookup, converting any found symbols.
351 This will find variables in the current scope. */
352
353 struct block_symbol sym
354 = lookup_symbol (identifier, instance->block (), VAR_DOMAIN, nullptr);
355
356 if (sym.symbol != nullptr)
357 {
358 found = true;
359 convert_symbol_sym (instance, identifier, sym, VAR_DOMAIN);
360 }
361
362 /* Then use linespec.c's multi-symbol search. This should find
363 all non-variable symbols for which we have debug info. */
364
365 symbol_searcher searcher;
366 searcher.find_all_symbols (identifier, current_language,
367 ALL_DOMAIN, nullptr, nullptr);
368
369 /* Convert any found symbols. */
370 for (const auto &it : searcher.matching_symbols ())
371 {
372 /* Don't convert the symbol found above, if any, twice! */
373 if (it.symbol != sym.symbol)
374 {
375 found = true;
376 convert_symbol_sym (instance, identifier, it,
377 it.symbol->domain ());
378 }
379 }
380
381 /* Finally, if no symbols have been found, fall back to minsyms. */
382 if (!found)
383 {
384 for (const auto &it : searcher.matching_msymbols ())
385 {
386 found = true;
387 convert_symbol_bmsym (instance, it);
388 }
389 }
390 }
391 catch (const gdb_exception &e)
392 {
393 /* We can't allow exceptions to escape out of this callback. Safest
394 is to simply emit a gcc error. */
395 instance->plugin ().error (e.what ());
396 }
397
398 if (compile_debug && !found)
400 "gcc_convert_symbol \"%s\": lookup_symbol failed\n",
401 identifier);
402
403 if (compile_debug)
404 {
405 if (found)
406 gdb_printf (gdb_stdlog, "found type for %s\n", identifier);
407 else
408 {
409 gdb_printf (gdb_stdlog, "did not find type for %s\n",
410 identifier);
411 }
412 }
413
414 return;
415}
416
417/* See compile-cplus.h. */
418
419gcc_address
420gcc_cplus_symbol_address (void *datum, struct gcc_cp_context *gcc_context,
421 const char *identifier)
422{
424 gcc_address result = 0;
425 int found = 0;
426
427 if (compile_debug)
429 "got oracle request for address of %s\n", identifier);
430
431 /* We can't allow exceptions to escape out of this callback. Safest
432 is to simply emit a gcc error. */
433 try
434 {
435 struct symbol *sym
436 = lookup_symbol (identifier, nullptr, VAR_DOMAIN, nullptr).symbol;
437
438 if (sym != nullptr && sym->aclass () == LOC_BLOCK)
439 {
440 if (compile_debug)
442 "gcc_symbol_address \"%s\": full symbol\n",
443 identifier);
444 result = sym->value_block ()->start ();
445 if (sym->type ()->is_gnu_ifunc ())
446 result = gnu_ifunc_resolve_addr (target_gdbarch (), result);
447 found = 1;
448 }
449 else
450 {
451 struct bound_minimal_symbol msym;
452
453 msym = lookup_bound_minimal_symbol (identifier);
454 if (msym.minsym != nullptr)
455 {
456 if (compile_debug)
458 "gcc_symbol_address \"%s\": minimal "
459 "symbol\n",
460 identifier);
461 result = msym.value_address ();
462 if (msym.minsym->type () == mst_text_gnu_ifunc)
463 result = gnu_ifunc_resolve_addr (target_gdbarch (), result);
464 found = 1;
465 }
466 }
467 }
468
469 catch (const gdb_exception_error &e)
470 {
471 instance->plugin ().error (e.what ());
472 }
473
474 if (compile_debug && !found)
476 "gcc_symbol_address \"%s\": failed\n",
477 identifier);
478
479 if (compile_debug)
480 {
481 if (found)
482 gdb_printf (gdb_stdlog, "found address for %s!\n", identifier);
483 else
485 "did not find address for %s\n", identifier);
486 }
487
488 return result;
489}
const char *const name
Definition: aarch64-tdep.c:67
struct gdbarch * target_gdbarch(void)
Definition: arch-utils.c:1453
const struct block * block_static_block(const struct block *block)
Definition: block.c:361
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:212
enum compile_i_scope_types scope() const
const struct block * block() const
gcc_type nested_type()
Definition: compile-cplus.h:79
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:2766
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:3745
const std::vector< bound_minimal_symbol > & matching_msymbols() const
Definition: symtab.h:2773
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:83
@ lval_memory
Definition: defs.h:364
int symbol_read_needs_frame(struct symbol *sym)
Definition: findvar.c:389
struct value * read_var_value(struct symbol *var, const struct block *var_block, frame_info_ptr frame)
Definition: findvar.c:787
frame_info_ptr get_selected_frame(const char *message)
Definition: frame.c:1813
const struct language_defn * current_language
Definition: language.c:83
struct bound_minimal_symbol lookup_bound_minimal_symbol(const char *name)
Definition: minsyms.c:481
const struct block * block
Definition: symtab.h:1498
struct symbol * symbol
Definition: symtab.h:1494
Definition: block.h:109
CORE_ADDR start() const
Definition: block.h:111
struct objfile * objfile
Definition: minsyms.h:54
CORE_ADDR value_address() const
Definition: minsyms.h:41
struct minimal_symbol * minsym
Definition: minsyms.h:49
const char * natural_name() const
Definition: symtab.c:1027
const char * print_name() const
Definition: symtab.h:474
minimal_symbol_type type() const
Definition: symtab.h:742
CORE_ADDR value_address(objfile *objfile) const
Definition: symtab.c:421
struct type * nodebug_unknown_symbol
Definition: gdbtypes.h:2369
struct type * nodebug_got_plt_symbol
Definition: gdbtypes.h:2367
struct type * nodebug_text_symbol
Definition: gdbtypes.h:2365
struct type * nodebug_data_symbol
Definition: gdbtypes.h:2368
const block * value_block() const
Definition: symtab.h:1348
address_class aclass() const
Definition: symtab.h:1235
struct type * type() const
Definition: symtab.h:1285
domain_enum domain() const
Definition: symtab.h:1240
LONGEST value_longest() const
Definition: symtab.h:1305
CORE_ADDR value_address() const
Definition: symtab.h:1315
struct symtab * symtab
Definition: symtab.h:1414
unsigned short line() const
Definition: symtab.h:1295
const char * filename
Definition: symtab.h:1651
Definition: gdbtypes.h:922
type_code code() const
Definition: gdbtypes.h:927
bool is_gnu_ifunc() const
Definition: gdbtypes.h:1179
Definition: value.c:181
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:1967
@ ALL_DOMAIN
Definition: symtab.h:931
@ LOC_STATIC
Definition: symtab.h:950
@ LOC_BLOCK
Definition: symtab.h:999
@ LOC_LABEL
Definition: symtab.h:993
@ LOC_REGISTER
Definition: symtab.h:964
@ LOC_REF_ARG
Definition: symtab.h:972
@ LOC_UNRESOLVED
Definition: symtab.h:1028
@ LOC_LOCAL
Definition: symtab.h:984
@ LOC_CONST
Definition: symtab.h:946
@ LOC_CONST_BYTES
Definition: symtab.h:1004
@ LOC_UNDEF
Definition: symtab.h:942
@ LOC_OPTIMIZED_OUT
Definition: symtab.h:1033
@ LOC_TYPEDEF
Definition: symtab.h:989
@ LOC_FINAL_VALUE
Definition: symtab.h:1044
@ LOC_REGPARM_ADDR
Definition: symtab.h:980
@ LOC_COMMON_BLOCK
Definition: symtab.h:1041
@ LOC_COMPUTED
Definition: symtab.h:1037
@ LOC_ARG
Definition: symtab.h:968
@ mst_bss
Definition: symtab.h:678
@ mst_data
Definition: symtab.h:677
@ mst_solib_trampoline
Definition: symtab.h:688
@ mst_file_text
Definition: symtab.h:691
@ mst_slot_got_plt
Definition: symtab.h:676
@ mst_file_data
Definition: symtab.h:692
@ mst_text
Definition: symtab.h:655
@ mst_text_gnu_ifunc
Definition: symtab.h:664
@ mst_file_bss
Definition: symtab.h:693
domain_enum
Definition: symtab.h:871
@ VAR_DOMAIN
Definition: symtab.h:881
@ STRUCT_DOMAIN
Definition: symtab.h:887
#define gnu_ifunc_resolve_addr
Definition: symtab.h:2248
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition: utils.c:1865
#define gdb_stdlog
Definition: utils.h:196
CORE_ADDR value_address(const struct value *value)
Definition: value.c:1607
#define VALUE_LVAL(val)
Definition: value.h:438