1 /* Convert symbols from GDB to GCC
3 Copyright (C) 2014-2017 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"
24 #include "parser-defs.h"
29 #include "exceptions.h"
31 #include "dwarf2loc.h"
35 /* Object of this type are stored in the compiler's symbol_err_map. */
41 const struct symbol
*sym
;
43 /* The error message to emit. This is malloc'd and owned by the
49 /* Hash function for struct symbol_error. */
52 hash_symbol_error (const void *a
)
54 const struct symbol_error
*se
= (const struct symbol_error
*) a
;
56 return htab_hash_pointer (se
->sym
);
59 /* Equality function for struct symbol_error. */
62 eq_symbol_error (const void *a
, const void *b
)
64 const struct symbol_error
*sea
= (const struct symbol_error
*) a
;
65 const struct symbol_error
*seb
= (const struct symbol_error
*) b
;
67 return sea
->sym
== seb
->sym
;
70 /* Deletion function for struct symbol_error. */
73 del_symbol_error (void *a
)
75 struct symbol_error
*se
= (struct symbol_error
*) a
;
81 /* Associate SYMBOL with some error text. */
84 insert_symbol_error (htab_t hash
, const struct symbol
*sym
, const char *text
)
86 struct symbol_error e
;
90 slot
= htab_find_slot (hash
, &e
, INSERT
);
93 struct symbol_error
*e
= XNEW (struct symbol_error
);
96 e
->message
= xstrdup (text
);
101 /* Emit the error message corresponding to SYM, if one exists, and
102 arrange for it not to be emitted again. */
105 error_symbol_once (struct compile_c_instance
*context
,
106 const struct symbol
*sym
)
108 struct symbol_error search
;
109 struct symbol_error
*err
;
111 if (context
->symbol_err_map
== NULL
)
115 err
= (struct symbol_error
*) htab_find (context
->symbol_err_map
, &search
);
116 if (err
== NULL
|| err
->message
== NULL
)
119 gdb::unique_xmalloc_ptr
<char> message (err
->message
);
121 error (_("%s"), message
.get ());
126 /* Compute the name of the pointer representing a local symbol's
129 static gdb::unique_xmalloc_ptr
<char>
130 symbol_substitution_name (struct symbol
*sym
)
132 return gdb::unique_xmalloc_ptr
<char>
133 (concat ("__", SYMBOL_NATURAL_NAME (sym
), "_ptr", (char *) NULL
));
136 /* Convert a given symbol, SYM, to the compiler's representation.
137 CONTEXT is the compiler instance. IS_GLOBAL is true if the
138 symbol came from the global scope. IS_LOCAL is true if the symbol
139 came from a local scope. (Note that the two are not strictly
140 inverses because the symbol might have come from the static
144 convert_one_symbol (struct compile_c_instance
*context
,
145 struct block_symbol sym
,
150 const char *filename
= symbol_symtab (sym
.symbol
)->filename
;
151 unsigned short line
= SYMBOL_LINE (sym
.symbol
);
153 error_symbol_once (context
, sym
.symbol
);
155 if (SYMBOL_CLASS (sym
.symbol
) == LOC_LABEL
)
158 sym_type
= convert_type (context
, SYMBOL_TYPE (sym
.symbol
));
160 if (SYMBOL_DOMAIN (sym
.symbol
) == STRUCT_DOMAIN
)
162 /* Binding a tag, so we don't need to build a decl. */
163 C_CTX (context
)->c_ops
->tagbind (C_CTX (context
),
164 SYMBOL_NATURAL_NAME (sym
.symbol
),
165 sym_type
, filename
, line
);
170 enum gcc_c_symbol_kind kind
;
172 gdb::unique_xmalloc_ptr
<char> symbol_name
;
174 switch (SYMBOL_CLASS (sym
.symbol
))
177 kind
= GCC_C_SYMBOL_TYPEDEF
;
181 kind
= GCC_C_SYMBOL_LABEL
;
182 addr
= SYMBOL_VALUE_ADDRESS (sym
.symbol
);
186 kind
= GCC_C_SYMBOL_FUNCTION
;
187 addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
.symbol
));
188 if (is_global
&& TYPE_GNU_IFUNC (SYMBOL_TYPE (sym
.symbol
)))
189 addr
= gnu_ifunc_resolve_addr (target_gdbarch (), addr
);
193 if (TYPE_CODE (SYMBOL_TYPE (sym
.symbol
)) == TYPE_CODE_ENUM
)
195 /* Already handled by convert_enum. */
198 C_CTX (context
)->c_ops
->build_constant
200 sym_type
, SYMBOL_NATURAL_NAME (sym
.symbol
),
201 SYMBOL_VALUE (sym
.symbol
),
205 case LOC_CONST_BYTES
:
206 error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."),
207 SYMBOL_PRINT_NAME (sym
.symbol
));
210 internal_error (__FILE__
, __LINE__
, _("LOC_UNDEF found for \"%s\"."),
211 SYMBOL_PRINT_NAME (sym
.symbol
));
213 case LOC_COMMON_BLOCK
:
214 error (_("Fortran common block is unsupported for compilation "
215 "evaluaton of symbol \"%s\"."),
216 SYMBOL_PRINT_NAME (sym
.symbol
));
218 case LOC_OPTIMIZED_OUT
:
219 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
220 "as it is optimized out."),
221 SYMBOL_PRINT_NAME (sym
.symbol
));
226 /* Probably TLS here. */
227 warning (_("Symbol \"%s\" is thread-local and currently can only "
228 "be referenced from the current thread in "
230 SYMBOL_PRINT_NAME (sym
.symbol
));
233 /* 'symbol_name' cannot be used here as that one is used only for
234 local variables from compile_dwarf_expr_to_c.
235 Global variables can be accessed by GCC only by their address, not
239 struct frame_info
*frame
= NULL
;
241 if (symbol_read_needs_frame (sym
.symbol
))
243 frame
= get_selected_frame (NULL
);
245 error (_("Symbol \"%s\" cannot be used because "
246 "there is no selected frame"),
247 SYMBOL_PRINT_NAME (sym
.symbol
));
250 val
= read_var_value (sym
.symbol
, sym
.block
, frame
);
251 if (VALUE_LVAL (val
) != lval_memory
)
252 error (_("Symbol \"%s\" cannot be used for compilation "
253 "evaluation as its address has not been found."),
254 SYMBOL_PRINT_NAME (sym
.symbol
));
256 kind
= GCC_C_SYMBOL_VARIABLE
;
257 addr
= value_address (val
);
265 case LOC_REGPARM_ADDR
:
268 kind
= GCC_C_SYMBOL_VARIABLE
;
269 symbol_name
= symbol_substitution_name (sym
.symbol
);
273 kind
= GCC_C_SYMBOL_VARIABLE
;
274 addr
= SYMBOL_VALUE_ADDRESS (sym
.symbol
);
277 case LOC_FINAL_VALUE
:
279 gdb_assert_not_reached ("Unreachable case in convert_one_symbol.");
283 /* Don't emit local variable decls for a raw expression. */
284 if (context
->base
.scope
!= COMPILE_I_RAW_SCOPE
285 || symbol_name
== NULL
)
287 decl
= C_CTX (context
)->c_ops
->build_decl
289 SYMBOL_NATURAL_NAME (sym
.symbol
),
292 symbol_name
.get (), addr
,
295 C_CTX (context
)->c_ops
->bind (C_CTX (context
), decl
, is_global
);
300 /* Convert a full symbol to its gcc form. CONTEXT is the compiler to
301 use, IDENTIFIER is the name of the symbol, SYM is the symbol
302 itself, and DOMAIN is the domain which was searched. */
305 convert_symbol_sym (struct compile_c_instance
*context
, const char *identifier
,
306 struct block_symbol sym
, domain_enum domain
)
308 const struct block
*static_block
;
311 /* If we found a symbol and it is not in the static or global
312 scope, then we should first convert any static or global scope
313 symbol of the same name. This lets this unusual case work:
319 // At this spot, evaluate "extern int x; x"
323 static_block
= block_static_block (sym
.block
);
324 /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block. */
325 is_local_symbol
= (sym
.block
!= static_block
&& static_block
!= NULL
);
328 struct block_symbol global_sym
;
330 global_sym
= lookup_symbol (identifier
, NULL
, domain
, NULL
);
331 /* If the outer symbol is in the static block, we ignore it, as
332 it cannot be referenced. */
333 if (global_sym
.symbol
!= NULL
334 && global_sym
.block
!= block_static_block (global_sym
.block
))
337 fprintf_unfiltered (gdb_stdlog
,
338 "gcc_convert_symbol \"%s\": global symbol\n",
340 convert_one_symbol (context
, global_sym
, 1, 0);
345 fprintf_unfiltered (gdb_stdlog
,
346 "gcc_convert_symbol \"%s\": local symbol\n",
348 convert_one_symbol (context
, sym
, 0, is_local_symbol
);
351 /* Convert a minimal symbol to its gcc form. CONTEXT is the compiler
352 to use and BMSYM is the minimal symbol to convert. */
355 convert_symbol_bmsym (struct compile_c_instance
*context
,
356 struct bound_minimal_symbol bmsym
)
358 struct minimal_symbol
*msym
= bmsym
.minsym
;
359 struct objfile
*objfile
= bmsym
.objfile
;
361 enum gcc_c_symbol_kind kind
;
366 addr
= MSYMBOL_VALUE_ADDRESS (objfile
, msym
);
368 /* Conversion copied from write_exp_msymbol. */
369 switch (MSYMBOL_TYPE (msym
))
373 case mst_solib_trampoline
:
374 type
= objfile_type (objfile
)->nodebug_text_symbol
;
375 kind
= GCC_C_SYMBOL_FUNCTION
;
378 case mst_text_gnu_ifunc
:
379 /* nodebug_text_gnu_ifunc_symbol would cause:
380 function return type cannot be function */
381 type
= objfile_type (objfile
)->nodebug_text_symbol
;
382 kind
= GCC_C_SYMBOL_FUNCTION
;
383 addr
= gnu_ifunc_resolve_addr (target_gdbarch (), addr
);
390 type
= objfile_type (objfile
)->nodebug_data_symbol
;
391 kind
= GCC_C_SYMBOL_VARIABLE
;
394 case mst_slot_got_plt
:
395 type
= objfile_type (objfile
)->nodebug_got_plt_symbol
;
396 kind
= GCC_C_SYMBOL_FUNCTION
;
400 type
= objfile_type (objfile
)->nodebug_unknown_symbol
;
401 kind
= GCC_C_SYMBOL_VARIABLE
;
405 sym_type
= convert_type (context
, type
);
406 decl
= C_CTX (context
)->c_ops
->build_decl (C_CTX (context
),
407 MSYMBOL_NATURAL_NAME (msym
),
408 kind
, sym_type
, NULL
, addr
,
410 C_CTX (context
)->c_ops
->bind (C_CTX (context
), decl
, 1 /* is_global */);
413 /* See compile-internal.h. */
416 gcc_convert_symbol (void *datum
,
417 struct gcc_c_context
*gcc_context
,
418 enum gcc_c_oracle_request request
,
419 const char *identifier
)
421 struct compile_c_instance
*context
= (struct compile_c_instance
*) datum
;
427 case GCC_C_ORACLE_SYMBOL
:
430 case GCC_C_ORACLE_TAG
:
431 domain
= STRUCT_DOMAIN
;
433 case GCC_C_ORACLE_LABEL
:
434 domain
= LABEL_DOMAIN
;
437 gdb_assert_not_reached ("Unrecognized oracle request.");
440 /* We can't allow exceptions to escape out of this callback. Safest
441 is to simply emit a gcc error. */
444 struct block_symbol sym
;
446 sym
= lookup_symbol (identifier
, context
->base
.block
, domain
, NULL
);
447 if (sym
.symbol
!= NULL
)
449 convert_symbol_sym (context
, identifier
, sym
, domain
);
452 else if (domain
== VAR_DOMAIN
)
454 struct bound_minimal_symbol bmsym
;
456 bmsym
= lookup_minimal_symbol (identifier
, NULL
, NULL
);
457 if (bmsym
.minsym
!= NULL
)
459 convert_symbol_bmsym (context
, bmsym
);
465 CATCH (e
, RETURN_MASK_ALL
)
467 C_CTX (context
)->c_ops
->error (C_CTX (context
), e
.message
);
471 if (compile_debug
&& !found
)
472 fprintf_unfiltered (gdb_stdlog
,
473 "gcc_convert_symbol \"%s\": lookup_symbol failed\n",
478 /* See compile-internal.h. */
481 gcc_symbol_address (void *datum
, struct gcc_c_context
*gcc_context
,
482 const char *identifier
)
484 struct compile_c_instance
*context
= (struct compile_c_instance
*) datum
;
485 gcc_address result
= 0;
488 /* We can't allow exceptions to escape out of this callback. Safest
489 is to simply emit a gcc error. */
494 /* We only need global functions here. */
495 sym
= lookup_symbol (identifier
, NULL
, VAR_DOMAIN
, NULL
).symbol
;
496 if (sym
!= NULL
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
499 fprintf_unfiltered (gdb_stdlog
,
500 "gcc_symbol_address \"%s\": full symbol\n",
502 result
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
503 if (TYPE_GNU_IFUNC (SYMBOL_TYPE (sym
)))
504 result
= gnu_ifunc_resolve_addr (target_gdbarch (), result
);
509 struct bound_minimal_symbol msym
;
511 msym
= lookup_bound_minimal_symbol (identifier
);
512 if (msym
.minsym
!= NULL
)
515 fprintf_unfiltered (gdb_stdlog
,
516 "gcc_symbol_address \"%s\": minimal "
519 result
= BMSYMBOL_VALUE_ADDRESS (msym
);
520 if (MSYMBOL_TYPE (msym
.minsym
) == mst_text_gnu_ifunc
)
521 result
= gnu_ifunc_resolve_addr (target_gdbarch (), result
);
527 CATCH (e
, RETURN_MASK_ERROR
)
529 C_CTX (context
)->c_ops
->error (C_CTX (context
), e
.message
);
533 if (compile_debug
&& !found
)
534 fprintf_unfiltered (gdb_stdlog
,
535 "gcc_symbol_address \"%s\": failed\n",
542 /* A hash function for symbol names. */
545 hash_symname (const void *a
)
547 const struct symbol
*sym
= (const struct symbol
*) a
;
549 return htab_hash_string (SYMBOL_NATURAL_NAME (sym
));
552 /* A comparison function for hash tables that just looks at symbol
556 eq_symname (const void *a
, const void *b
)
558 const struct symbol
*syma
= (const struct symbol
*) a
;
559 const struct symbol
*symb
= (const struct symbol
*) b
;
561 return strcmp (SYMBOL_NATURAL_NAME (syma
), SYMBOL_NATURAL_NAME (symb
)) == 0;
564 /* If a symbol with the same name as SYM is already in HASHTAB, return
565 1. Otherwise, add SYM to HASHTAB and return 0. */
568 symbol_seen (htab_t hashtab
, struct symbol
*sym
)
572 slot
= htab_find_slot (hashtab
, sym
, INSERT
);
580 /* Generate C code to compute the length of a VLA. */
583 generate_vla_size (struct compile_c_instance
*compiler
,
585 struct gdbarch
*gdbarch
,
586 unsigned char *registers_used
,
591 type
= check_typedef (type
);
593 if (TYPE_IS_REFERENCE (type
))
594 type
= check_typedef (TYPE_TARGET_TYPE (type
));
596 switch (TYPE_CODE (type
))
598 case TYPE_CODE_RANGE
:
600 if (TYPE_HIGH_BOUND_KIND (type
) == PROP_LOCEXPR
601 || TYPE_HIGH_BOUND_KIND (type
) == PROP_LOCLIST
)
603 const struct dynamic_prop
*prop
= &TYPE_RANGE_DATA (type
)->high
;
604 std::string name
= c_get_range_decl_name (prop
);
606 dwarf2_compile_property_to_c (stream
, name
.c_str (),
607 gdbarch
, registers_used
,
613 case TYPE_CODE_ARRAY
:
614 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
615 TYPE_INDEX_TYPE (type
), sym
);
616 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
617 TYPE_TARGET_TYPE (type
), sym
);
620 case TYPE_CODE_UNION
:
621 case TYPE_CODE_STRUCT
:
625 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
626 if (!field_is_static (&TYPE_FIELD (type
, i
)))
627 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
628 TYPE_FIELD_TYPE (type
, i
), sym
);
634 /* Generate C code to compute the address of SYM. */
637 generate_c_for_for_one_variable (struct compile_c_instance
*compiler
,
639 struct gdbarch
*gdbarch
,
640 unsigned char *registers_used
,
647 if (is_dynamic_type (SYMBOL_TYPE (sym
)))
649 /* We need to emit to a temporary buffer in case an error
650 occurs in the middle. */
651 string_file local_file
;
653 generate_vla_size (compiler
, local_file
, gdbarch
, registers_used
, pc
,
654 SYMBOL_TYPE (sym
), sym
);
656 stream
.write (local_file
.c_str (), local_file
.size ());
659 if (SYMBOL_COMPUTED_OPS (sym
) != NULL
)
661 gdb::unique_xmalloc_ptr
<char> generated_name
662 = symbol_substitution_name (sym
);
663 /* We need to emit to a temporary buffer in case an error
664 occurs in the middle. */
665 string_file local_file
;
667 SYMBOL_COMPUTED_OPS (sym
)->generate_c_location (sym
, local_file
,
671 generated_name
.get ());
672 stream
.write (local_file
.c_str (), local_file
.size ());
676 switch (SYMBOL_CLASS (sym
))
681 case LOC_REGPARM_ADDR
:
683 error (_("Local symbol unhandled when generating C code."));
686 gdb_assert_not_reached (_("LOC_COMPUTED variable "
687 "missing a method."));
690 /* Nothing to do for all other cases, as they don't represent
697 CATCH (e
, RETURN_MASK_ERROR
)
699 if (compiler
->symbol_err_map
== NULL
)
700 compiler
->symbol_err_map
= htab_create_alloc (10,
706 insert_symbol_error (compiler
->symbol_err_map
, sym
, e
.message
);
711 /* See compile-internal.h. */
714 generate_c_for_variable_locations (struct compile_c_instance
*compiler
,
716 struct gdbarch
*gdbarch
,
717 const struct block
*block
,
720 struct cleanup
*outer
;
721 const struct block
*static_block
= block_static_block (block
);
722 unsigned char *registers_used
;
724 /* If we're already in the static or global block, there is nothing
726 if (static_block
== NULL
|| block
== static_block
)
729 registers_used
= XCNEWVEC (unsigned char, gdbarch_num_regs (gdbarch
));
730 outer
= make_cleanup (xfree
, registers_used
);
732 /* Ensure that a given name is only entered once. This reflects the
733 reality of shadowing. */
734 htab_up
symhash (htab_create_alloc (1, hash_symname
, eq_symname
, NULL
,
740 struct block_iterator iter
;
742 /* Iterate over symbols in this block, generating code to
743 compute the location of each local variable. */
744 for (sym
= block_iterator_first (block
, &iter
);
746 sym
= block_iterator_next (&iter
))
748 if (!symbol_seen (symhash
.get (), sym
))
749 generate_c_for_for_one_variable (compiler
, stream
, gdbarch
,
750 registers_used
, pc
, sym
);
753 /* If we just finished the outermost block of a function, we're
755 if (BLOCK_FUNCTION (block
) != NULL
)
757 block
= BLOCK_SUPERBLOCK (block
);
760 discard_cleanups (outer
);
761 return registers_used
;