1 /* Convert symbols from GDB to GCC
3 Copyright (C) 2014-2015 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
= 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
= a
;
65 const struct symbol_error
*seb
= 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
= 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
;
112 if (context
->symbol_err_map
== NULL
)
116 err
= htab_find (context
->symbol_err_map
, &search
);
117 if (err
== NULL
|| err
->message
== NULL
)
120 message
= err
->message
;
122 make_cleanup (xfree
, message
);
123 error (_("%s"), message
);
128 /* Compute the name of the pointer representing a local symbol's
132 symbol_substitution_name (struct symbol
*sym
)
134 return concat ("__", SYMBOL_NATURAL_NAME (sym
), "_ptr", (char *) NULL
);
137 /* Convert a given symbol, SYM, to the compiler's representation.
138 CONTEXT is the compiler instance. IS_GLOBAL is true if the
139 symbol came from the global scope. IS_LOCAL is true if the symbol
140 came from a local scope. (Note that the two are not strictly
141 inverses because the symbol might have come from the static
145 convert_one_symbol (struct compile_c_instance
*context
,
151 const char *filename
= symbol_symtab (sym
)->filename
;
152 unsigned short line
= SYMBOL_LINE (sym
);
154 error_symbol_once (context
, sym
);
156 if (SYMBOL_CLASS (sym
) == LOC_LABEL
)
159 sym_type
= convert_type (context
, SYMBOL_TYPE (sym
));
161 if (SYMBOL_DOMAIN (sym
) == STRUCT_DOMAIN
)
163 /* Binding a tag, so we don't need to build a decl. */
164 C_CTX (context
)->c_ops
->tagbind (C_CTX (context
),
165 SYMBOL_NATURAL_NAME (sym
),
166 sym_type
, filename
, line
);
171 enum gcc_c_symbol_kind kind
;
173 char *symbol_name
= NULL
;
175 switch (SYMBOL_CLASS (sym
))
178 kind
= GCC_C_SYMBOL_TYPEDEF
;
182 kind
= GCC_C_SYMBOL_LABEL
;
183 addr
= SYMBOL_VALUE_ADDRESS (sym
);
187 kind
= GCC_C_SYMBOL_FUNCTION
;
188 addr
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
189 if (is_global
&& TYPE_GNU_IFUNC (SYMBOL_TYPE (sym
)))
190 addr
= gnu_ifunc_resolve_addr (target_gdbarch (), addr
);
194 if (TYPE_CODE (SYMBOL_TYPE (sym
)) == TYPE_CODE_ENUM
)
196 /* Already handled by convert_enum. */
199 C_CTX (context
)->c_ops
->build_constant (C_CTX (context
), sym_type
,
200 SYMBOL_NATURAL_NAME (sym
),
205 case LOC_CONST_BYTES
:
206 error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."),
207 SYMBOL_PRINT_NAME (sym
));
210 internal_error (__FILE__
, __LINE__
, _("LOC_UNDEF found for \"%s\"."),
211 SYMBOL_PRINT_NAME (sym
));
213 case LOC_COMMON_BLOCK
:
214 error (_("Fortran common block is unsupported for compilation "
215 "evaluaton of symbol \"%s\"."),
216 SYMBOL_PRINT_NAME (sym
));
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
));
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
));
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
))
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
));
250 val
= read_var_value (sym
, 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
));
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
);
273 kind
= GCC_C_SYMBOL_VARIABLE
;
274 addr
= SYMBOL_VALUE_ADDRESS (sym
);
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 (C_CTX (context
),
288 SYMBOL_NATURAL_NAME (sym
),
294 C_CTX (context
)->c_ops
->bind (C_CTX (context
), decl
, is_global
);
301 /* Convert a full symbol to its gcc form. CONTEXT is the compiler to
302 use, IDENTIFIER is the name of the symbol, SYM is the symbol
303 itself, and DOMAIN is the domain which was searched. */
306 convert_symbol_sym (struct compile_c_instance
*context
, const char *identifier
,
307 struct block_symbol sym
, domain_enum domain
)
309 const struct block
*static_block
;
312 /* If we found a symbol and it is not in the static or global
313 scope, then we should first convert any static or global scope
314 symbol of the same name. This lets this unusual case work:
320 // At this spot, evaluate "extern int x; x"
324 static_block
= block_static_block (sym
.block
);
325 /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block. */
326 is_local_symbol
= (sym
.block
!= static_block
&& static_block
!= NULL
);
329 struct block_symbol global_sym
;
331 global_sym
= lookup_symbol (identifier
, NULL
, domain
, NULL
);
332 /* If the outer symbol is in the static block, we ignore it, as
333 it cannot be referenced. */
334 if (global_sym
.symbol
!= NULL
335 && global_sym
.block
!= block_static_block (global_sym
.block
))
338 fprintf_unfiltered (gdb_stdlog
,
339 "gcc_convert_symbol \"%s\": global symbol\n",
341 convert_one_symbol (context
, global_sym
.symbol
, 1, 0);
346 fprintf_unfiltered (gdb_stdlog
,
347 "gcc_convert_symbol \"%s\": local symbol\n",
349 convert_one_symbol (context
, sym
.symbol
, 0, is_local_symbol
);
352 /* Convert a minimal symbol to its gcc form. CONTEXT is the compiler
353 to use and BMSYM is the minimal symbol to convert. */
356 convert_symbol_bmsym (struct compile_c_instance
*context
,
357 struct bound_minimal_symbol bmsym
)
359 struct minimal_symbol
*msym
= bmsym
.minsym
;
360 struct objfile
*objfile
= bmsym
.objfile
;
362 enum gcc_c_symbol_kind kind
;
367 addr
= MSYMBOL_VALUE_ADDRESS (objfile
, msym
);
369 /* Conversion copied from write_exp_msymbol. */
370 switch (MSYMBOL_TYPE (msym
))
374 case mst_solib_trampoline
:
375 type
= objfile_type (objfile
)->nodebug_text_symbol
;
376 kind
= GCC_C_SYMBOL_FUNCTION
;
379 case mst_text_gnu_ifunc
:
380 /* nodebug_text_gnu_ifunc_symbol would cause:
381 function return type cannot be function */
382 type
= objfile_type (objfile
)->nodebug_text_symbol
;
383 kind
= GCC_C_SYMBOL_FUNCTION
;
384 addr
= gnu_ifunc_resolve_addr (target_gdbarch (), addr
);
391 type
= objfile_type (objfile
)->nodebug_data_symbol
;
392 kind
= GCC_C_SYMBOL_VARIABLE
;
395 case mst_slot_got_plt
:
396 type
= objfile_type (objfile
)->nodebug_got_plt_symbol
;
397 kind
= GCC_C_SYMBOL_FUNCTION
;
401 type
= objfile_type (objfile
)->nodebug_unknown_symbol
;
402 kind
= GCC_C_SYMBOL_VARIABLE
;
406 sym_type
= convert_type (context
, type
);
407 decl
= C_CTX (context
)->c_ops
->build_decl (C_CTX (context
),
408 MSYMBOL_NATURAL_NAME (msym
),
409 kind
, sym_type
, NULL
, addr
,
411 C_CTX (context
)->c_ops
->bind (C_CTX (context
), decl
, 1 /* is_global */);
414 /* See compile-internal.h. */
417 gcc_convert_symbol (void *datum
,
418 struct gcc_c_context
*gcc_context
,
419 enum gcc_c_oracle_request request
,
420 const char *identifier
)
422 struct compile_c_instance
*context
= datum
;
428 case GCC_C_ORACLE_SYMBOL
:
431 case GCC_C_ORACLE_TAG
:
432 domain
= STRUCT_DOMAIN
;
434 case GCC_C_ORACLE_LABEL
:
435 domain
= LABEL_DOMAIN
;
438 gdb_assert_not_reached ("Unrecognized oracle request.");
441 /* We can't allow exceptions to escape out of this callback. Safest
442 is to simply emit a gcc error. */
445 struct block_symbol sym
;
447 sym
= lookup_symbol (identifier
, context
->base
.block
, domain
, NULL
);
448 if (sym
.symbol
!= NULL
)
450 convert_symbol_sym (context
, identifier
, sym
, domain
);
453 else if (domain
== VAR_DOMAIN
)
455 struct bound_minimal_symbol bmsym
;
457 bmsym
= lookup_minimal_symbol (identifier
, NULL
, NULL
);
458 if (bmsym
.minsym
!= NULL
)
460 convert_symbol_bmsym (context
, bmsym
);
466 CATCH (e
, RETURN_MASK_ALL
)
468 C_CTX (context
)->c_ops
->error (C_CTX (context
), e
.message
);
472 if (compile_debug
&& !found
)
473 fprintf_unfiltered (gdb_stdlog
,
474 "gcc_convert_symbol \"%s\": lookup_symbol failed\n",
479 /* See compile-internal.h. */
482 gcc_symbol_address (void *datum
, struct gcc_c_context
*gcc_context
,
483 const char *identifier
)
485 struct compile_c_instance
*context
= datum
;
486 gcc_address result
= 0;
489 /* We can't allow exceptions to escape out of this callback. Safest
490 is to simply emit a gcc error. */
495 /* We only need global functions here. */
496 sym
= lookup_symbol (identifier
, NULL
, VAR_DOMAIN
, NULL
).symbol
;
497 if (sym
!= NULL
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
500 fprintf_unfiltered (gdb_stdlog
,
501 "gcc_symbol_address \"%s\": full symbol\n",
503 result
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
504 if (TYPE_GNU_IFUNC (SYMBOL_TYPE (sym
)))
505 result
= gnu_ifunc_resolve_addr (target_gdbarch (), result
);
510 struct bound_minimal_symbol msym
;
512 msym
= lookup_bound_minimal_symbol (identifier
);
513 if (msym
.minsym
!= NULL
)
516 fprintf_unfiltered (gdb_stdlog
,
517 "gcc_symbol_address \"%s\": minimal "
520 result
= BMSYMBOL_VALUE_ADDRESS (msym
);
521 if (MSYMBOL_TYPE (msym
.minsym
) == mst_text_gnu_ifunc
)
522 result
= gnu_ifunc_resolve_addr (target_gdbarch (), result
);
528 CATCH (e
, RETURN_MASK_ERROR
)
530 C_CTX (context
)->c_ops
->error (C_CTX (context
), e
.message
);
534 if (compile_debug
&& !found
)
535 fprintf_unfiltered (gdb_stdlog
,
536 "gcc_symbol_address \"%s\": failed\n",
543 /* A hash function for symbol names. */
546 hash_symname (const void *a
)
548 const struct symbol
*sym
= a
;
550 return htab_hash_string (SYMBOL_NATURAL_NAME (sym
));
553 /* A comparison function for hash tables that just looks at symbol
557 eq_symname (const void *a
, const void *b
)
559 const struct symbol
*syma
= a
;
560 const struct symbol
*symb
= b
;
562 return strcmp (SYMBOL_NATURAL_NAME (syma
), SYMBOL_NATURAL_NAME (symb
)) == 0;
565 /* If a symbol with the same name as SYM is already in HASHTAB, return
566 1. Otherwise, add SYM to HASHTAB and return 0. */
569 symbol_seen (htab_t hashtab
, struct symbol
*sym
)
573 slot
= htab_find_slot (hashtab
, sym
, INSERT
);
581 /* Generate C code to compute the length of a VLA. */
584 generate_vla_size (struct compile_c_instance
*compiler
,
585 struct ui_file
*stream
,
586 struct gdbarch
*gdbarch
,
587 unsigned char *registers_used
,
592 type
= check_typedef (type
);
594 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
595 type
= check_typedef (TYPE_TARGET_TYPE (type
));
597 switch (TYPE_CODE (type
))
599 case TYPE_CODE_RANGE
:
601 if (TYPE_HIGH_BOUND_KIND (type
) == PROP_LOCEXPR
602 || TYPE_HIGH_BOUND_KIND (type
) == PROP_LOCLIST
)
604 const struct dynamic_prop
*prop
= &TYPE_RANGE_DATA (type
)->high
;
605 char *name
= c_get_range_decl_name (prop
);
606 struct cleanup
*cleanup
= make_cleanup (xfree
, name
);
608 dwarf2_compile_property_to_c (stream
, name
,
609 gdbarch
, registers_used
,
611 do_cleanups (cleanup
);
616 case TYPE_CODE_ARRAY
:
617 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
618 TYPE_INDEX_TYPE (type
), sym
);
619 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
620 TYPE_TARGET_TYPE (type
), sym
);
623 case TYPE_CODE_UNION
:
624 case TYPE_CODE_STRUCT
:
628 for (i
= 0; i
< TYPE_NFIELDS (type
); ++i
)
629 if (!field_is_static (&TYPE_FIELD (type
, i
)))
630 generate_vla_size (compiler
, stream
, gdbarch
, registers_used
, pc
,
631 TYPE_FIELD_TYPE (type
, i
), sym
);
637 /* Generate C code to compute the address of SYM. */
640 generate_c_for_for_one_variable (struct compile_c_instance
*compiler
,
641 struct ui_file
*stream
,
642 struct gdbarch
*gdbarch
,
643 unsigned char *registers_used
,
650 if (is_dynamic_type (SYMBOL_TYPE (sym
)))
652 struct ui_file
*size_file
= mem_fileopen ();
653 struct cleanup
*cleanup
= make_cleanup_ui_file_delete (size_file
);
655 generate_vla_size (compiler
, size_file
, gdbarch
, registers_used
, pc
,
656 SYMBOL_TYPE (sym
), sym
);
657 ui_file_put (size_file
, ui_file_write_for_put
, stream
);
659 do_cleanups (cleanup
);
662 if (SYMBOL_COMPUTED_OPS (sym
) != NULL
)
664 char *generated_name
= symbol_substitution_name (sym
);
665 struct cleanup
*cleanup
= make_cleanup (xfree
, generated_name
);
666 /* We need to emit to a temporary buffer in case an error
667 occurs in the middle. */
668 struct ui_file
*local_file
= mem_fileopen ();
670 make_cleanup_ui_file_delete (local_file
);
671 SYMBOL_COMPUTED_OPS (sym
)->generate_c_location (sym
, local_file
,
675 ui_file_put (local_file
, ui_file_write_for_put
, stream
);
677 do_cleanups (cleanup
);
681 switch (SYMBOL_CLASS (sym
))
686 case LOC_REGPARM_ADDR
:
688 error (_("Local symbol unhandled when generating C code."));
691 gdb_assert_not_reached (_("LOC_COMPUTED variable "
692 "missing a method."));
695 /* Nothing to do for all other cases, as they don't represent
702 CATCH (e
, RETURN_MASK_ERROR
)
704 if (compiler
->symbol_err_map
== NULL
)
705 compiler
->symbol_err_map
= htab_create_alloc (10,
711 insert_symbol_error (compiler
->symbol_err_map
, sym
, e
.message
);
716 /* See compile-internal.h. */
719 generate_c_for_variable_locations (struct compile_c_instance
*compiler
,
720 struct ui_file
*stream
,
721 struct gdbarch
*gdbarch
,
722 const struct block
*block
,
725 struct cleanup
*cleanup
, *outer
;
727 const struct block
*static_block
= block_static_block (block
);
728 unsigned char *registers_used
;
730 /* If we're already in the static or global block, there is nothing
732 if (static_block
== NULL
|| block
== static_block
)
735 registers_used
= XCNEWVEC (unsigned char, gdbarch_num_regs (gdbarch
));
736 outer
= make_cleanup (xfree
, registers_used
);
738 /* Ensure that a given name is only entered once. This reflects the
739 reality of shadowing. */
740 symhash
= htab_create_alloc (1, hash_symname
, eq_symname
, NULL
,
742 cleanup
= make_cleanup_htab_delete (symhash
);
747 struct block_iterator iter
;
749 /* Iterate over symbols in this block, generating code to
750 compute the location of each local variable. */
751 for (sym
= block_iterator_first (block
, &iter
);
753 sym
= block_iterator_next (&iter
))
755 if (!symbol_seen (symhash
, sym
))
756 generate_c_for_for_one_variable (compiler
, stream
, gdbarch
,
757 registers_used
, pc
, sym
);
760 /* If we just finished the outermost block of a function, we're
762 if (BLOCK_FUNCTION (block
) != NULL
)
764 block
= BLOCK_SUPERBLOCK (block
);
767 do_cleanups (cleanup
);
768 discard_cleanups (outer
);
769 return registers_used
;