1 /* Convert symbols from GDB to GCC
3 Copyright (C) 2014-2018 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 "dwarf2loc.h"
36 /* Object of this type are stored in the compiler's symbol_err_map. */
42 const struct symbol
*sym
;
44 /* The error message to emit. This is malloc'd and owned by the
50 /* Hash function for struct symbol_error. */
53 hash_symbol_error (const void *a
)
55 const struct symbol_error
*se
= (const struct symbol_error
*) a
;
57 return htab_hash_pointer (se
->sym
);
60 /* Equality function for struct symbol_error. */
63 eq_symbol_error (const void *a
, const void *b
)
65 const struct symbol_error
*sea
= (const struct symbol_error
*) a
;
66 const struct symbol_error
*seb
= (const struct symbol_error
*) b
;
68 return sea
->sym
== seb
->sym
;
71 /* Deletion function for struct symbol_error. */
74 del_symbol_error (void *a
)
76 struct symbol_error
*se
= (struct symbol_error
*) a
;
82 /* See compile-internal.h. */
85 compile_instance::insert_symbol_error (const struct symbol
*sym
,
88 struct symbol_error e
;
91 if (m_symbol_err_map
== NULL
)
93 m_symbol_err_map
= htab_create_alloc (10,
102 slot
= htab_find_slot (m_symbol_err_map
, &e
, INSERT
);
105 struct symbol_error
*e
= XNEW (struct symbol_error
);
108 e
->message
= xstrdup (text
);
113 /* See compile-internal.h. */
116 compile_instance::error_symbol_once (const struct symbol
*sym
)
118 struct symbol_error search
;
119 struct symbol_error
*err
;
121 if (m_symbol_err_map
== NULL
)
125 err
= (struct symbol_error
*) htab_find (m_symbol_err_map
, &search
);
126 if (err
== NULL
|| err
->message
== NULL
)
129 gdb::unique_xmalloc_ptr
<char> message (err
->message
);
131 error (_("%s"), message
.get ());
136 /* Compute the name of the pointer representing a local symbol's
139 static gdb::unique_xmalloc_ptr
<char>
140 c_symbol_substitution_name (struct symbol
*sym
)
142 return gdb::unique_xmalloc_ptr
<char>
143 (concat ("__", SYMBOL_NATURAL_NAME (sym
), "_ptr", (char *) NULL
));
146 /* Convert a given symbol, SYM, to the compiler's representation.
147 CONTEXT is the compiler instance. IS_GLOBAL is true if the
148 symbol came from the global scope. IS_LOCAL is true if the symbol
149 came from a local scope. (Note that the two are not strictly
150 inverses because the symbol might have come from the static
154 convert_one_symbol (compile_c_instance
*context
,
155 struct block_symbol sym
,
160 const char *filename
= symbol_symtab (sym
.symbol
)->filename
;
161 unsigned short line
= SYMBOL_LINE (sym
.symbol
);
163 context
->error_symbol_once (sym
.symbol
);
165 if (SYMBOL_CLASS (sym
.symbol
) == LOC_LABEL
)
168 sym_type
= context
->convert_type (SYMBOL_TYPE (sym
.symbol
));
170 if (SYMBOL_DOMAIN (sym
.symbol
) == STRUCT_DOMAIN
)
172 /* Binding a tag, so we don't need to build a decl. */
173 context
->plugin ().tagbind (SYMBOL_NATURAL_NAME (sym
.symbol
),
174 sym_type
, filename
, line
);
179 enum gcc_c_symbol_kind kind
;
181 gdb::unique_xmalloc_ptr
<char> symbol_name
;
183 switch (SYMBOL_CLASS (sym
.symbol
))
186 kind
= GCC_C_SYMBOL_TYPEDEF
;
190 kind
= GCC_C_SYMBOL_LABEL
;
191 addr
= SYMBOL_VALUE_ADDRESS (sym
.symbol
);
195 kind
= GCC_C_SYMBOL_FUNCTION
;
196 addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
.symbol
));
197 if (is_global
&& TYPE_GNU_IFUNC (SYMBOL_TYPE (sym
.symbol
)))
198 addr
= gnu_ifunc_resolve_addr (target_gdbarch (), addr
);
202 if (TYPE_CODE (SYMBOL_TYPE (sym
.symbol
)) == TYPE_CODE_ENUM
)
204 /* Already handled by convert_enum. */
207 context
->plugin ().build_constant
208 (sym_type
, SYMBOL_NATURAL_NAME (sym
.symbol
),
209 SYMBOL_VALUE (sym
.symbol
),
213 case LOC_CONST_BYTES
:
214 error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."),
215 SYMBOL_PRINT_NAME (sym
.symbol
));
218 internal_error (__FILE__
, __LINE__
, _("LOC_UNDEF found for \"%s\"."),
219 SYMBOL_PRINT_NAME (sym
.symbol
));
221 case LOC_COMMON_BLOCK
:
222 error (_("Fortran common block is unsupported for compilation "
223 "evaluaton of symbol \"%s\"."),
224 SYMBOL_PRINT_NAME (sym
.symbol
));
226 case LOC_OPTIMIZED_OUT
:
227 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
228 "as it is optimized out."),
229 SYMBOL_PRINT_NAME (sym
.symbol
));
234 /* Probably TLS here. */
235 warning (_("Symbol \"%s\" is thread-local and currently can only "
236 "be referenced from the current thread in "
238 SYMBOL_PRINT_NAME (sym
.symbol
));
241 /* 'symbol_name' cannot be used here as that one is used only for
242 local variables from compile_dwarf_expr_to_c.
243 Global variables can be accessed by GCC only by their address, not
247 struct frame_info
*frame
= NULL
;
249 if (symbol_read_needs_frame (sym
.symbol
))
251 frame
= get_selected_frame (NULL
);
253 error (_("Symbol \"%s\" cannot be used because "
254 "there is no selected frame"),
255 SYMBOL_PRINT_NAME (sym
.symbol
));
258 val
= read_var_value (sym
.symbol
, sym
.block
, frame
);
259 if (VALUE_LVAL (val
) != lval_memory
)
260 error (_("Symbol \"%s\" cannot be used for compilation "
261 "evaluation as its address has not been found."),
262 SYMBOL_PRINT_NAME (sym
.symbol
));
264 kind
= GCC_C_SYMBOL_VARIABLE
;
265 addr
= value_address (val
);
273 case LOC_REGPARM_ADDR
:
276 kind
= GCC_C_SYMBOL_VARIABLE
;
277 symbol_name
= c_symbol_substitution_name (sym
.symbol
);
281 kind
= GCC_C_SYMBOL_VARIABLE
;
282 addr
= SYMBOL_VALUE_ADDRESS (sym
.symbol
);
285 case LOC_FINAL_VALUE
:
287 gdb_assert_not_reached ("Unreachable case in convert_one_symbol.");
291 /* Don't emit local variable decls for a raw expression. */
292 if (context
->scope () != COMPILE_I_RAW_SCOPE
293 || symbol_name
== NULL
)
295 decl
= context
->plugin ().build_decl
296 (SYMBOL_NATURAL_NAME (sym
.symbol
),
299 symbol_name
.get (), addr
,
302 context
->plugin ().bind (decl
, is_global
);
307 /* Convert a full symbol to its gcc form. CONTEXT is the compiler to
308 use, IDENTIFIER is the name of the symbol, SYM is the symbol
309 itself, and DOMAIN is the domain which was searched. */
312 convert_symbol_sym (compile_c_instance
*context
, const char *identifier
,
313 struct block_symbol sym
, domain_enum domain
)
315 const struct block
*static_block
;
318 /* If we found a symbol and it is not in the static or global
319 scope, then we should first convert any static or global scope
320 symbol of the same name. This lets this unusual case work:
326 // At this spot, evaluate "extern int x; x"
330 static_block
= block_static_block (sym
.block
);
331 /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block. */
332 is_local_symbol
= (sym
.block
!= static_block
&& static_block
!= NULL
);
335 struct block_symbol global_sym
;
337 global_sym
= lookup_symbol (identifier
, NULL
, domain
, NULL
);
338 /* If the outer symbol is in the static block, we ignore it, as
339 it cannot be referenced. */
340 if (global_sym
.symbol
!= NULL
341 && global_sym
.block
!= block_static_block (global_sym
.block
))
344 fprintf_unfiltered (gdb_stdlog
,
345 "gcc_convert_symbol \"%s\": global symbol\n",
347 convert_one_symbol (context
, global_sym
, 1, 0);
352 fprintf_unfiltered (gdb_stdlog
,
353 "gcc_convert_symbol \"%s\": local symbol\n",
355 convert_one_symbol (context
, sym
, 0, is_local_symbol
);
358 /* Convert a minimal symbol to its gcc form. CONTEXT is the compiler
359 to use and BMSYM is the minimal symbol to convert. */
362 convert_symbol_bmsym (compile_c_instance
*context
,
363 struct bound_minimal_symbol bmsym
)
365 struct minimal_symbol
*msym
= bmsym
.minsym
;
366 struct objfile
*objfile
= bmsym
.objfile
;
368 enum gcc_c_symbol_kind kind
;
373 addr
= MSYMBOL_VALUE_ADDRESS (objfile
, msym
);
375 /* Conversion copied from write_exp_msymbol. */
376 switch (MSYMBOL_TYPE (msym
))
380 case mst_solib_trampoline
:
381 type
= objfile_type (objfile
)->nodebug_text_symbol
;
382 kind
= GCC_C_SYMBOL_FUNCTION
;
385 case mst_text_gnu_ifunc
:
386 type
= objfile_type (objfile
)->nodebug_text_gnu_ifunc_symbol
;
387 kind
= GCC_C_SYMBOL_FUNCTION
;
388 addr
= gnu_ifunc_resolve_addr (target_gdbarch (), addr
);
395 type
= objfile_type (objfile
)->nodebug_data_symbol
;
396 kind
= GCC_C_SYMBOL_VARIABLE
;
399 case mst_slot_got_plt
:
400 type
= objfile_type (objfile
)->nodebug_got_plt_symbol
;
401 kind
= GCC_C_SYMBOL_FUNCTION
;
405 type
= objfile_type (objfile
)->nodebug_unknown_symbol
;
406 kind
= GCC_C_SYMBOL_VARIABLE
;
410 sym_type
= context
->convert_type (type
);
411 decl
= context
->plugin ().build_decl (MSYMBOL_NATURAL_NAME (msym
),
412 kind
, sym_type
, NULL
, addr
,
414 context
->plugin ().bind (decl
, 1 /* is_global */);
417 /* See compile-internal.h. */
420 gcc_convert_symbol (void *datum
,
421 struct gcc_c_context
*gcc_context
,
422 enum gcc_c_oracle_request request
,
423 const char *identifier
)
425 compile_c_instance
*context
426 = static_cast<compile_c_instance
*> (datum
);
432 case GCC_C_ORACLE_SYMBOL
:
435 case GCC_C_ORACLE_TAG
:
436 domain
= STRUCT_DOMAIN
;
438 case GCC_C_ORACLE_LABEL
:
439 domain
= LABEL_DOMAIN
;
442 gdb_assert_not_reached ("Unrecognized oracle request.");
445 /* We can't allow exceptions to escape out of this callback. Safest
446 is to simply emit a gcc error. */
449 struct block_symbol sym
;
451 sym
= lookup_symbol (identifier
, context
->block (), domain
, NULL
);
452 if (sym
.symbol
!= NULL
)
454 convert_symbol_sym (context
, identifier
, sym
, domain
);
457 else if (domain
== VAR_DOMAIN
)
459 struct bound_minimal_symbol bmsym
;
461 bmsym
= lookup_minimal_symbol (identifier
, NULL
, NULL
);
462 if (bmsym
.minsym
!= NULL
)
464 convert_symbol_bmsym (context
, bmsym
);
470 CATCH (e
, RETURN_MASK_ALL
)
472 context
->plugin ().error (e
.message
);
476 if (compile_debug
&& !found
)
477 fprintf_unfiltered (gdb_stdlog
,
478 "gcc_convert_symbol \"%s\": lookup_symbol failed\n",
483 /* See compile-internal.h. */
486 gcc_symbol_address (void *datum
, struct gcc_c_context
*gcc_context
,
487 const char *identifier
)
489 compile_c_instance
*context
490 = static_cast<compile_c_instance
*> (datum
);
491 gcc_address result
= 0;
494 /* We can't allow exceptions to escape out of this callback. Safest
495 is to simply emit a gcc error. */
500 /* We only need global functions here. */
501 sym
= lookup_symbol (identifier
, NULL
, VAR_DOMAIN
, NULL
).symbol
;
502 if (sym
!= NULL
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
505 fprintf_unfiltered (gdb_stdlog
,
506 "gcc_symbol_address \"%s\": full symbol\n",
508 result
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
509 if (TYPE_GNU_IFUNC (SYMBOL_TYPE (sym
)))
510 result
= gnu_ifunc_resolve_addr (target_gdbarch (), result
);
515 struct bound_minimal_symbol msym
;
517 msym
= lookup_bound_minimal_symbol (identifier
);
518 if (msym
.minsym
!= NULL
)
521 fprintf_unfiltered (gdb_stdlog
,
522 "gcc_symbol_address \"%s\": minimal "
525 result
= BMSYMBOL_VALUE_ADDRESS (msym
);
526 if (MSYMBOL_TYPE (msym
.minsym
) == mst_text_gnu_ifunc
)
527 result
= gnu_ifunc_resolve_addr (target_gdbarch (), result
);
533 CATCH (e
, RETURN_MASK_ERROR
)
535 context
->plugin ().error (e
.message
);
539 if (compile_debug
&& !found
)
540 fprintf_unfiltered (gdb_stdlog
,
541 "gcc_symbol_address \"%s\": failed\n",
548 /* A hash function for symbol names. */
551 hash_symname (const void *a
)
553 const struct symbol
*sym
= (const struct symbol
*) a
;
555 return htab_hash_string (SYMBOL_NATURAL_NAME (sym
));
558 /* A comparison function for hash tables that just looks at symbol
562 eq_symname (const void *a
, const void *b
)
564 const struct symbol
*syma
= (const struct symbol
*) a
;
565 const struct symbol
*symb
= (const struct symbol
*) b
;
567 return strcmp (SYMBOL_NATURAL_NAME (syma
), SYMBOL_NATURAL_NAME (symb
)) == 0;
570 /* If a symbol with the same name as SYM is already in HASHTAB, return
571 1. Otherwise, add SYM to HASHTAB and return 0. */
574 symbol_seen (htab_t hashtab
, struct symbol
*sym
)
578 slot
= htab_find_slot (hashtab
, sym
, INSERT
);
586 /* Generate C code to compute the length of a VLA. */
589 generate_vla_size (compile_instance
*compiler
,
591 struct gdbarch
*gdbarch
,
592 unsigned char *registers_used
,
597 type
= check_typedef (type
);
599 if (TYPE_IS_REFERENCE (type
))
600 type
= check_typedef (TYPE_TARGET_TYPE (type
));
602 switch (TYPE_CODE (type
))
604 case TYPE_CODE_RANGE
:
606 if (TYPE_HIGH_BOUND_KIND (type
) == PROP_LOCEXPR
607 || TYPE_HIGH_BOUND_KIND (type
) == PROP_LOCLIST
)
609 const struct dynamic_prop
*prop
= &TYPE_RANGE_DATA (type
)->high
;
610 std::string name
= c_get_range_decl_name (prop
);
612 dwarf2_compile_property_to_c (stream
, name
.c_str (),
613 gdbarch
, registers_used
,
619 case TYPE_CODE_ARRAY
:
620 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
621 TYPE_INDEX_TYPE (type
), sym
);
622 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
623 TYPE_TARGET_TYPE (type
), sym
);
626 case TYPE_CODE_UNION
:
627 case TYPE_CODE_STRUCT
:
631 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
632 if (!field_is_static (&TYPE_FIELD (type
, i
)))
633 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
634 TYPE_FIELD_TYPE (type
, i
), sym
);
640 /* Generate C code to compute the address of SYM. */
643 generate_c_for_for_one_variable (compile_instance
*compiler
,
645 struct gdbarch
*gdbarch
,
646 unsigned char *registers_used
,
653 if (is_dynamic_type (SYMBOL_TYPE (sym
)))
655 /* We need to emit to a temporary buffer in case an error
656 occurs in the middle. */
657 string_file local_file
;
659 generate_vla_size (compiler
, local_file
, gdbarch
, registers_used
, pc
,
660 SYMBOL_TYPE (sym
), sym
);
662 stream
.write (local_file
.c_str (), local_file
.size ());
665 if (SYMBOL_COMPUTED_OPS (sym
) != NULL
)
667 gdb::unique_xmalloc_ptr
<char> generated_name
668 = c_symbol_substitution_name (sym
);
669 /* We need to emit to a temporary buffer in case an error
670 occurs in the middle. */
671 string_file local_file
;
673 SYMBOL_COMPUTED_OPS (sym
)->generate_c_location (sym
, local_file
,
677 generated_name
.get ());
678 stream
.write (local_file
.c_str (), local_file
.size ());
682 switch (SYMBOL_CLASS (sym
))
687 case LOC_REGPARM_ADDR
:
689 error (_("Local symbol unhandled when generating C code."));
692 gdb_assert_not_reached (_("LOC_COMPUTED variable "
693 "missing a method."));
696 /* Nothing to do for all other cases, as they don't represent
703 CATCH (e
, RETURN_MASK_ERROR
)
705 compiler
->insert_symbol_error (sym
, e
.message
);
710 /* See compile-c.h. */
712 gdb::unique_xmalloc_ptr
<unsigned char>
713 generate_c_for_variable_locations (compile_instance
*compiler
,
715 struct gdbarch
*gdbarch
,
716 const struct block
*block
,
719 const struct block
*static_block
= block_static_block (block
);
721 /* If we're already in the static or global block, there is nothing
723 if (static_block
== NULL
|| block
== static_block
)
726 gdb::unique_xmalloc_ptr
<unsigned char> registers_used
727 (XCNEWVEC (unsigned char, gdbarch_num_regs (gdbarch
)));
729 /* Ensure that a given name is only entered once. This reflects the
730 reality of shadowing. */
731 htab_up
symhash (htab_create_alloc (1, hash_symname
, eq_symname
, NULL
,
737 struct block_iterator iter
;
739 /* Iterate over symbols in this block, generating code to
740 compute the location of each local variable. */
741 for (sym
= block_iterator_first (block
, &iter
);
743 sym
= block_iterator_next (&iter
))
745 if (!symbol_seen (symhash
.get (), sym
))
746 generate_c_for_for_one_variable (compiler
, stream
, gdbarch
,
747 registers_used
.get (), pc
, sym
);
750 /* If we just finished the outermost block of a function, we're
752 if (BLOCK_FUNCTION (block
) != NULL
)
754 block
= BLOCK_SUPERBLOCK (block
);
757 return registers_used
;