1 /* Convert symbols from GDB to GCC
3 Copyright (C) 2014-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 #include "compile-internal.h"
23 #include "compile-c.h"
25 #include "parser-defs.h"
30 #include "exceptions.h"
32 #include "dwarf2/loc.h"
36 /* Compute the name of the pointer representing a local symbol's
39 gdb::unique_xmalloc_ptr
<char>
40 c_symbol_substitution_name (struct symbol
*sym
)
42 return gdb::unique_xmalloc_ptr
<char>
43 (concat ("__", sym
->natural_name (), "_ptr", (char *) NULL
));
46 /* Convert a given symbol, SYM, to the compiler's representation.
47 CONTEXT is the compiler instance. IS_GLOBAL is true if the
48 symbol came from the global scope. IS_LOCAL is true if the symbol
49 came from a local scope. (Note that the two are not strictly
50 inverses because the symbol might have come from the static
54 convert_one_symbol (compile_c_instance
*context
,
55 struct block_symbol sym
,
60 const char *filename
= symbol_symtab (sym
.symbol
)->filename
;
61 unsigned short line
= SYMBOL_LINE (sym
.symbol
);
63 context
->error_symbol_once (sym
.symbol
);
65 if (SYMBOL_CLASS (sym
.symbol
) == LOC_LABEL
)
68 sym_type
= context
->convert_type (SYMBOL_TYPE (sym
.symbol
));
70 if (SYMBOL_DOMAIN (sym
.symbol
) == STRUCT_DOMAIN
)
72 /* Binding a tag, so we don't need to build a decl. */
73 context
->plugin ().tagbind (sym
.symbol
->natural_name (),
74 sym_type
, filename
, line
);
79 enum gcc_c_symbol_kind kind
;
81 gdb::unique_xmalloc_ptr
<char> symbol_name
;
83 switch (SYMBOL_CLASS (sym
.symbol
))
86 kind
= GCC_C_SYMBOL_TYPEDEF
;
90 kind
= GCC_C_SYMBOL_LABEL
;
91 addr
= SYMBOL_VALUE_ADDRESS (sym
.symbol
);
95 kind
= GCC_C_SYMBOL_FUNCTION
;
96 addr
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
.symbol
));
97 if (is_global
&& TYPE_GNU_IFUNC (SYMBOL_TYPE (sym
.symbol
)))
98 addr
= gnu_ifunc_resolve_addr (target_gdbarch (), addr
);
102 if (SYMBOL_TYPE (sym
.symbol
)->code () == TYPE_CODE_ENUM
)
104 /* Already handled by convert_enum. */
107 context
->plugin ().build_constant
108 (sym_type
, sym
.symbol
->natural_name (),
109 SYMBOL_VALUE (sym
.symbol
),
113 case LOC_CONST_BYTES
:
114 error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."),
115 sym
.symbol
->print_name ());
118 internal_error (__FILE__
, __LINE__
, _("LOC_UNDEF found for \"%s\"."),
119 sym
.symbol
->print_name ());
121 case LOC_COMMON_BLOCK
:
122 error (_("Fortran common block is unsupported for compilation "
123 "evaluaton of symbol \"%s\"."),
124 sym
.symbol
->print_name ());
126 case LOC_OPTIMIZED_OUT
:
127 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
128 "as it is optimized out."),
129 sym
.symbol
->print_name ());
134 /* Probably TLS here. */
135 warning (_("Symbol \"%s\" is thread-local and currently can only "
136 "be referenced from the current thread in "
138 sym
.symbol
->print_name ());
141 /* 'symbol_name' cannot be used here as that one is used only for
142 local variables from compile_dwarf_expr_to_c.
143 Global variables can be accessed by GCC only by their address, not
147 struct frame_info
*frame
= NULL
;
149 if (symbol_read_needs_frame (sym
.symbol
))
151 frame
= get_selected_frame (NULL
);
153 error (_("Symbol \"%s\" cannot be used because "
154 "there is no selected frame"),
155 sym
.symbol
->print_name ());
158 val
= read_var_value (sym
.symbol
, sym
.block
, frame
);
159 if (VALUE_LVAL (val
) != lval_memory
)
160 error (_("Symbol \"%s\" cannot be used for compilation "
161 "evaluation as its address has not been found."),
162 sym
.symbol
->print_name ());
164 kind
= GCC_C_SYMBOL_VARIABLE
;
165 addr
= value_address (val
);
173 case LOC_REGPARM_ADDR
:
176 kind
= GCC_C_SYMBOL_VARIABLE
;
177 symbol_name
= c_symbol_substitution_name (sym
.symbol
);
181 kind
= GCC_C_SYMBOL_VARIABLE
;
182 addr
= SYMBOL_VALUE_ADDRESS (sym
.symbol
);
185 case LOC_FINAL_VALUE
:
187 gdb_assert_not_reached ("Unreachable case in convert_one_symbol.");
191 /* Don't emit local variable decls for a raw expression. */
192 if (context
->scope () != COMPILE_I_RAW_SCOPE
193 || symbol_name
== NULL
)
195 decl
= context
->plugin ().build_decl
196 (sym
.symbol
->natural_name (),
199 symbol_name
.get (), addr
,
202 context
->plugin ().bind (decl
, is_global
);
207 /* Convert a full symbol to its gcc form. CONTEXT is the compiler to
208 use, IDENTIFIER is the name of the symbol, SYM is the symbol
209 itself, and DOMAIN is the domain which was searched. */
212 convert_symbol_sym (compile_c_instance
*context
, const char *identifier
,
213 struct block_symbol sym
, domain_enum domain
)
215 const struct block
*static_block
;
218 /* If we found a symbol and it is not in the static or global
219 scope, then we should first convert any static or global scope
220 symbol of the same name. This lets this unusual case work:
226 // At this spot, evaluate "extern int x; x"
230 static_block
= block_static_block (sym
.block
);
231 /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block. */
232 is_local_symbol
= (sym
.block
!= static_block
&& static_block
!= NULL
);
235 struct block_symbol global_sym
;
237 global_sym
= lookup_symbol (identifier
, NULL
, domain
, NULL
);
238 /* If the outer symbol is in the static block, we ignore it, as
239 it cannot be referenced. */
240 if (global_sym
.symbol
!= NULL
241 && global_sym
.block
!= block_static_block (global_sym
.block
))
244 fprintf_unfiltered (gdb_stdlog
,
245 "gcc_convert_symbol \"%s\": global symbol\n",
247 convert_one_symbol (context
, global_sym
, 1, 0);
252 fprintf_unfiltered (gdb_stdlog
,
253 "gcc_convert_symbol \"%s\": local symbol\n",
255 convert_one_symbol (context
, sym
, 0, is_local_symbol
);
258 /* Convert a minimal symbol to its gcc form. CONTEXT is the compiler
259 to use and BMSYM is the minimal symbol to convert. */
262 convert_symbol_bmsym (compile_c_instance
*context
,
263 struct bound_minimal_symbol bmsym
)
265 struct minimal_symbol
*msym
= bmsym
.minsym
;
266 struct objfile
*objfile
= bmsym
.objfile
;
268 enum gcc_c_symbol_kind kind
;
273 addr
= MSYMBOL_VALUE_ADDRESS (objfile
, msym
);
275 /* Conversion copied from write_exp_msymbol. */
276 switch (MSYMBOL_TYPE (msym
))
280 case mst_solib_trampoline
:
281 type
= objfile_type (objfile
)->nodebug_text_symbol
;
282 kind
= GCC_C_SYMBOL_FUNCTION
;
285 case mst_text_gnu_ifunc
:
286 type
= objfile_type (objfile
)->nodebug_text_gnu_ifunc_symbol
;
287 kind
= GCC_C_SYMBOL_FUNCTION
;
288 addr
= gnu_ifunc_resolve_addr (target_gdbarch (), addr
);
295 type
= objfile_type (objfile
)->nodebug_data_symbol
;
296 kind
= GCC_C_SYMBOL_VARIABLE
;
299 case mst_slot_got_plt
:
300 type
= objfile_type (objfile
)->nodebug_got_plt_symbol
;
301 kind
= GCC_C_SYMBOL_FUNCTION
;
305 type
= objfile_type (objfile
)->nodebug_unknown_symbol
;
306 kind
= GCC_C_SYMBOL_VARIABLE
;
310 sym_type
= context
->convert_type (type
);
311 decl
= context
->plugin ().build_decl (msym
->natural_name (),
312 kind
, sym_type
, NULL
, addr
,
314 context
->plugin ().bind (decl
, 1 /* is_global */);
317 /* See compile-internal.h. */
320 gcc_convert_symbol (void *datum
,
321 struct gcc_c_context
*gcc_context
,
322 enum gcc_c_oracle_request request
,
323 const char *identifier
)
325 compile_c_instance
*context
326 = static_cast<compile_c_instance
*> (datum
);
332 case GCC_C_ORACLE_SYMBOL
:
335 case GCC_C_ORACLE_TAG
:
336 domain
= STRUCT_DOMAIN
;
338 case GCC_C_ORACLE_LABEL
:
339 domain
= LABEL_DOMAIN
;
342 gdb_assert_not_reached ("Unrecognized oracle request.");
345 /* We can't allow exceptions to escape out of this callback. Safest
346 is to simply emit a gcc error. */
349 struct block_symbol sym
;
351 sym
= lookup_symbol (identifier
, context
->block (), domain
, NULL
);
352 if (sym
.symbol
!= NULL
)
354 convert_symbol_sym (context
, identifier
, sym
, domain
);
357 else if (domain
== VAR_DOMAIN
)
359 struct bound_minimal_symbol bmsym
;
361 bmsym
= lookup_minimal_symbol (identifier
, NULL
, NULL
);
362 if (bmsym
.minsym
!= NULL
)
364 convert_symbol_bmsym (context
, bmsym
);
370 catch (const gdb_exception
&e
)
372 context
->plugin ().error (e
.what ());
375 if (compile_debug
&& !found
)
376 fprintf_unfiltered (gdb_stdlog
,
377 "gcc_convert_symbol \"%s\": lookup_symbol failed\n",
382 /* See compile-internal.h. */
385 gcc_symbol_address (void *datum
, struct gcc_c_context
*gcc_context
,
386 const char *identifier
)
388 compile_c_instance
*context
389 = static_cast<compile_c_instance
*> (datum
);
390 gcc_address result
= 0;
393 /* We can't allow exceptions to escape out of this callback. Safest
394 is to simply emit a gcc error. */
399 /* We only need global functions here. */
400 sym
= lookup_symbol (identifier
, NULL
, VAR_DOMAIN
, NULL
).symbol
;
401 if (sym
!= NULL
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
404 fprintf_unfiltered (gdb_stdlog
,
405 "gcc_symbol_address \"%s\": full symbol\n",
407 result
= BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
));
408 if (TYPE_GNU_IFUNC (SYMBOL_TYPE (sym
)))
409 result
= gnu_ifunc_resolve_addr (target_gdbarch (), result
);
414 struct bound_minimal_symbol msym
;
416 msym
= lookup_bound_minimal_symbol (identifier
);
417 if (msym
.minsym
!= NULL
)
420 fprintf_unfiltered (gdb_stdlog
,
421 "gcc_symbol_address \"%s\": minimal "
424 result
= BMSYMBOL_VALUE_ADDRESS (msym
);
425 if (MSYMBOL_TYPE (msym
.minsym
) == mst_text_gnu_ifunc
)
426 result
= gnu_ifunc_resolve_addr (target_gdbarch (), result
);
432 catch (const gdb_exception_error
&e
)
434 context
->plugin ().error (e
.what ());
437 if (compile_debug
&& !found
)
438 fprintf_unfiltered (gdb_stdlog
,
439 "gcc_symbol_address \"%s\": failed\n",
446 /* A hash function for symbol names. */
449 hash_symname (const void *a
)
451 const struct symbol
*sym
= (const struct symbol
*) a
;
453 return htab_hash_string (sym
->natural_name ());
456 /* A comparison function for hash tables that just looks at symbol
460 eq_symname (const void *a
, const void *b
)
462 const struct symbol
*syma
= (const struct symbol
*) a
;
463 const struct symbol
*symb
= (const struct symbol
*) b
;
465 return strcmp (syma
->natural_name (), symb
->natural_name ()) == 0;
468 /* If a symbol with the same name as SYM is already in HASHTAB, return
469 1. Otherwise, add SYM to HASHTAB and return 0. */
472 symbol_seen (htab_t hashtab
, struct symbol
*sym
)
476 slot
= htab_find_slot (hashtab
, sym
, INSERT
);
484 /* Generate C code to compute the length of a VLA. */
487 generate_vla_size (compile_instance
*compiler
,
489 struct gdbarch
*gdbarch
,
490 unsigned char *registers_used
,
495 type
= check_typedef (type
);
497 if (TYPE_IS_REFERENCE (type
))
498 type
= check_typedef (TYPE_TARGET_TYPE (type
));
500 switch (type
->code ())
502 case TYPE_CODE_RANGE
:
504 if (TYPE_HIGH_BOUND_KIND (type
) == PROP_LOCEXPR
505 || TYPE_HIGH_BOUND_KIND (type
) == PROP_LOCLIST
)
507 const struct dynamic_prop
*prop
= &TYPE_RANGE_DATA (type
)->high
;
508 std::string name
= c_get_range_decl_name (prop
);
510 dwarf2_compile_property_to_c (stream
, name
.c_str (),
511 gdbarch
, registers_used
,
517 case TYPE_CODE_ARRAY
:
518 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
519 TYPE_INDEX_TYPE (type
), sym
);
520 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
521 TYPE_TARGET_TYPE (type
), sym
);
524 case TYPE_CODE_UNION
:
525 case TYPE_CODE_STRUCT
:
529 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
530 if (!field_is_static (&TYPE_FIELD (type
, i
)))
531 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
532 TYPE_FIELD_TYPE (type
, i
), sym
);
538 /* Generate C code to compute the address of SYM. */
541 generate_c_for_for_one_variable (compile_instance
*compiler
,
543 struct gdbarch
*gdbarch
,
544 unsigned char *registers_used
,
551 if (is_dynamic_type (SYMBOL_TYPE (sym
)))
553 /* We need to emit to a temporary buffer in case an error
554 occurs in the middle. */
555 string_file local_file
;
557 generate_vla_size (compiler
, &local_file
, gdbarch
, registers_used
, pc
,
558 SYMBOL_TYPE (sym
), sym
);
560 stream
->write (local_file
.c_str (), local_file
.size ());
563 if (SYMBOL_COMPUTED_OPS (sym
) != NULL
)
565 gdb::unique_xmalloc_ptr
<char> generated_name
566 = c_symbol_substitution_name (sym
);
567 /* We need to emit to a temporary buffer in case an error
568 occurs in the middle. */
569 string_file local_file
;
571 SYMBOL_COMPUTED_OPS (sym
)->generate_c_location (sym
, &local_file
,
575 generated_name
.get ());
576 stream
->write (local_file
.c_str (), local_file
.size ());
580 switch (SYMBOL_CLASS (sym
))
585 case LOC_REGPARM_ADDR
:
587 error (_("Local symbol unhandled when generating C code."));
590 gdb_assert_not_reached (_("LOC_COMPUTED variable "
591 "missing a method."));
594 /* Nothing to do for all other cases, as they don't represent
601 catch (const gdb_exception_error
&e
)
603 compiler
->insert_symbol_error (sym
, e
.what ());
607 /* See compile-c.h. */
609 gdb::unique_xmalloc_ptr
<unsigned char>
610 generate_c_for_variable_locations (compile_instance
*compiler
,
612 struct gdbarch
*gdbarch
,
613 const struct block
*block
,
616 const struct block
*static_block
= block_static_block (block
);
618 /* If we're already in the static or global block, there is nothing
620 if (static_block
== NULL
|| block
== static_block
)
623 gdb::unique_xmalloc_ptr
<unsigned char> registers_used
624 (XCNEWVEC (unsigned char, gdbarch_num_regs (gdbarch
)));
626 /* Ensure that a given name is only entered once. This reflects the
627 reality of shadowing. */
628 htab_up
symhash (htab_create_alloc (1, hash_symname
, eq_symname
, NULL
,
634 struct block_iterator iter
;
636 /* Iterate over symbols in this block, generating code to
637 compute the location of each local variable. */
638 for (sym
= block_iterator_first (block
, &iter
);
640 sym
= block_iterator_next (&iter
))
642 if (!symbol_seen (symhash
.get (), sym
))
643 generate_c_for_for_one_variable (compiler
, stream
, gdbarch
,
644 registers_used
.get (), pc
, sym
);
647 /* If we just finished the outermost block of a function, we're
649 if (BLOCK_FUNCTION (block
) != NULL
)
651 block
= BLOCK_SUPERBLOCK (block
);
654 return registers_used
;