Change compile_instance/compile_c_instance into classes
[deliverable/binutils-gdb.git] / gdb / compile / compile-c-symbols.c
1 /* Convert symbols from GDB to GCC
2
3 Copyright (C) 2014-2018 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-c.h"
24 #include "symtab.h"
25 #include "parser-defs.h"
26 #include "block.h"
27 #include "objfiles.h"
28 #include "compile.h"
29 #include "value.h"
30 #include "exceptions.h"
31 #include "gdbtypes.h"
32 #include "dwarf2loc.h"
33
34 \f
35
36 /* Object of this type are stored in the compiler's symbol_err_map. */
37
38 struct symbol_error
39 {
40 /* The symbol. */
41
42 const struct symbol *sym;
43
44 /* The error message to emit. This is malloc'd and owned by the
45 hash table. */
46
47 char *message;
48 };
49
50 /* Hash function for struct symbol_error. */
51
52 static hashval_t
53 hash_symbol_error (const void *a)
54 {
55 const struct symbol_error *se = (const struct symbol_error *) a;
56
57 return htab_hash_pointer (se->sym);
58 }
59
60 /* Equality function for struct symbol_error. */
61
62 static int
63 eq_symbol_error (const void *a, const void *b)
64 {
65 const struct symbol_error *sea = (const struct symbol_error *) a;
66 const struct symbol_error *seb = (const struct symbol_error *) b;
67
68 return sea->sym == seb->sym;
69 }
70
71 /* Deletion function for struct symbol_error. */
72
73 static void
74 del_symbol_error (void *a)
75 {
76 struct symbol_error *se = (struct symbol_error *) a;
77
78 xfree (se->message);
79 xfree (se);
80 }
81
82 /* See compile-internal.h. */
83
84 void
85 compile_instance::insert_symbol_error (const struct symbol *sym,
86 const char *text)
87 {
88 struct symbol_error e;
89 void **slot;
90
91 if (m_symbol_err_map == NULL)
92 {
93 m_symbol_err_map = htab_create_alloc (10,
94 hash_symbol_error,
95 eq_symbol_error,
96 del_symbol_error,
97 xcalloc,
98 xfree);
99 }
100
101 e.sym = sym;
102 slot = htab_find_slot (m_symbol_err_map, &e, INSERT);
103 if (*slot == NULL)
104 {
105 struct symbol_error *e = XNEW (struct symbol_error);
106
107 e->sym = sym;
108 e->message = xstrdup (text);
109 *slot = e;
110 }
111 }
112
113 /* See compile-internal.h. */
114
115 void
116 compile_instance::error_symbol_once (const struct symbol *sym)
117 {
118 struct symbol_error search;
119 struct symbol_error *err;
120
121 if (m_symbol_err_map == NULL)
122 return;
123
124 search.sym = sym;
125 err = (struct symbol_error *) htab_find (m_symbol_err_map, &search);
126 if (err == NULL || err->message == NULL)
127 return;
128
129 gdb::unique_xmalloc_ptr<char> message (err->message);
130 err->message = NULL;
131 error (_("%s"), message.get ());
132 }
133
134 \f
135
136 /* Compute the name of the pointer representing a local symbol's
137 address. */
138
139 static gdb::unique_xmalloc_ptr<char>
140 c_symbol_substitution_name (struct symbol *sym)
141 {
142 return gdb::unique_xmalloc_ptr<char>
143 (concat ("__", SYMBOL_NATURAL_NAME (sym), "_ptr", (char *) NULL));
144 }
145
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
151 scope.) */
152
153 static void
154 convert_one_symbol (compile_c_instance *context,
155 struct block_symbol sym,
156 int is_global,
157 int is_local)
158 {
159 gcc_type sym_type;
160 const char *filename = symbol_symtab (sym.symbol)->filename;
161 unsigned short line = SYMBOL_LINE (sym.symbol);
162
163 context->error_symbol_once (sym.symbol);
164
165 if (SYMBOL_CLASS (sym.symbol) == LOC_LABEL)
166 sym_type = 0;
167 else
168 sym_type = context->convert_type (SYMBOL_TYPE (sym.symbol));
169
170 if (SYMBOL_DOMAIN (sym.symbol) == STRUCT_DOMAIN)
171 {
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);
175 }
176 else
177 {
178 gcc_decl decl;
179 enum gcc_c_symbol_kind kind;
180 CORE_ADDR addr = 0;
181 gdb::unique_xmalloc_ptr<char> symbol_name;
182
183 switch (SYMBOL_CLASS (sym.symbol))
184 {
185 case LOC_TYPEDEF:
186 kind = GCC_C_SYMBOL_TYPEDEF;
187 break;
188
189 case LOC_LABEL:
190 kind = GCC_C_SYMBOL_LABEL;
191 addr = SYMBOL_VALUE_ADDRESS (sym.symbol);
192 break;
193
194 case LOC_BLOCK:
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);
199 break;
200
201 case LOC_CONST:
202 if (TYPE_CODE (SYMBOL_TYPE (sym.symbol)) == TYPE_CODE_ENUM)
203 {
204 /* Already handled by convert_enum. */
205 return;
206 }
207 context->plugin ().build_constant
208 (sym_type, SYMBOL_NATURAL_NAME (sym.symbol),
209 SYMBOL_VALUE (sym.symbol),
210 filename, line);
211 return;
212
213 case LOC_CONST_BYTES:
214 error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."),
215 SYMBOL_PRINT_NAME (sym.symbol));
216
217 case LOC_UNDEF:
218 internal_error (__FILE__, __LINE__, _("LOC_UNDEF found for \"%s\"."),
219 SYMBOL_PRINT_NAME (sym.symbol));
220
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));
225
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));
230
231 case LOC_COMPUTED:
232 if (is_local)
233 goto substitution;
234 /* Probably TLS here. */
235 warning (_("Symbol \"%s\" is thread-local and currently can only "
236 "be referenced from the current thread in "
237 "compiled code."),
238 SYMBOL_PRINT_NAME (sym.symbol));
239 /* FALLTHROUGH */
240 case LOC_UNRESOLVED:
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
244 by their name. */
245 {
246 struct value *val;
247 struct frame_info *frame = NULL;
248
249 if (symbol_read_needs_frame (sym.symbol))
250 {
251 frame = get_selected_frame (NULL);
252 if (frame == NULL)
253 error (_("Symbol \"%s\" cannot be used because "
254 "there is no selected frame"),
255 SYMBOL_PRINT_NAME (sym.symbol));
256 }
257
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));
263
264 kind = GCC_C_SYMBOL_VARIABLE;
265 addr = value_address (val);
266 }
267 break;
268
269
270 case LOC_REGISTER:
271 case LOC_ARG:
272 case LOC_REF_ARG:
273 case LOC_REGPARM_ADDR:
274 case LOC_LOCAL:
275 substitution:
276 kind = GCC_C_SYMBOL_VARIABLE;
277 symbol_name = c_symbol_substitution_name (sym.symbol);
278 break;
279
280 case LOC_STATIC:
281 kind = GCC_C_SYMBOL_VARIABLE;
282 addr = SYMBOL_VALUE_ADDRESS (sym.symbol);
283 break;
284
285 case LOC_FINAL_VALUE:
286 default:
287 gdb_assert_not_reached ("Unreachable case in convert_one_symbol.");
288
289 }
290
291 /* Don't emit local variable decls for a raw expression. */
292 if (context->scope () != COMPILE_I_RAW_SCOPE
293 || symbol_name == NULL)
294 {
295 decl = context->plugin ().build_decl
296 (SYMBOL_NATURAL_NAME (sym.symbol),
297 kind,
298 sym_type,
299 symbol_name.get (), addr,
300 filename, line);
301
302 context->plugin ().bind (decl, is_global);
303 }
304 }
305 }
306
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. */
310
311 static void
312 convert_symbol_sym (compile_c_instance *context, const char *identifier,
313 struct block_symbol sym, domain_enum domain)
314 {
315 const struct block *static_block;
316 int is_local_symbol;
317
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:
321
322 int x; // Global.
323 int func(void)
324 {
325 int x;
326 // At this spot, evaluate "extern int x; x"
327 }
328 */
329
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);
333 if (is_local_symbol)
334 {
335 struct block_symbol global_sym;
336
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))
342 {
343 if (compile_debug)
344 fprintf_unfiltered (gdb_stdlog,
345 "gcc_convert_symbol \"%s\": global symbol\n",
346 identifier);
347 convert_one_symbol (context, global_sym, 1, 0);
348 }
349 }
350
351 if (compile_debug)
352 fprintf_unfiltered (gdb_stdlog,
353 "gcc_convert_symbol \"%s\": local symbol\n",
354 identifier);
355 convert_one_symbol (context, sym, 0, is_local_symbol);
356 }
357
358 /* Convert a minimal symbol to its gcc form. CONTEXT is the compiler
359 to use and BMSYM is the minimal symbol to convert. */
360
361 static void
362 convert_symbol_bmsym (compile_c_instance *context,
363 struct bound_minimal_symbol bmsym)
364 {
365 struct minimal_symbol *msym = bmsym.minsym;
366 struct objfile *objfile = bmsym.objfile;
367 struct type *type;
368 enum gcc_c_symbol_kind kind;
369 gcc_type sym_type;
370 gcc_decl decl;
371 CORE_ADDR addr;
372
373 addr = MSYMBOL_VALUE_ADDRESS (objfile, msym);
374
375 /* Conversion copied from write_exp_msymbol. */
376 switch (MSYMBOL_TYPE (msym))
377 {
378 case mst_text:
379 case mst_file_text:
380 case mst_solib_trampoline:
381 type = objfile_type (objfile)->nodebug_text_symbol;
382 kind = GCC_C_SYMBOL_FUNCTION;
383 break;
384
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);
389 break;
390
391 case mst_data:
392 case mst_file_data:
393 case mst_bss:
394 case mst_file_bss:
395 type = objfile_type (objfile)->nodebug_data_symbol;
396 kind = GCC_C_SYMBOL_VARIABLE;
397 break;
398
399 case mst_slot_got_plt:
400 type = objfile_type (objfile)->nodebug_got_plt_symbol;
401 kind = GCC_C_SYMBOL_FUNCTION;
402 break;
403
404 default:
405 type = objfile_type (objfile)->nodebug_unknown_symbol;
406 kind = GCC_C_SYMBOL_VARIABLE;
407 break;
408 }
409
410 sym_type = context->convert_type (type);
411 decl = context->plugin ().build_decl (MSYMBOL_NATURAL_NAME (msym),
412 kind, sym_type, NULL, addr,
413 NULL, 0);
414 context->plugin ().bind (decl, 1 /* is_global */);
415 }
416
417 /* See compile-internal.h. */
418
419 void
420 gcc_convert_symbol (void *datum,
421 struct gcc_c_context *gcc_context,
422 enum gcc_c_oracle_request request,
423 const char *identifier)
424 {
425 compile_c_instance *context
426 = static_cast<compile_c_instance *> (datum);
427 domain_enum domain;
428 int found = 0;
429
430 switch (request)
431 {
432 case GCC_C_ORACLE_SYMBOL:
433 domain = VAR_DOMAIN;
434 break;
435 case GCC_C_ORACLE_TAG:
436 domain = STRUCT_DOMAIN;
437 break;
438 case GCC_C_ORACLE_LABEL:
439 domain = LABEL_DOMAIN;
440 break;
441 default:
442 gdb_assert_not_reached ("Unrecognized oracle request.");
443 }
444
445 /* We can't allow exceptions to escape out of this callback. Safest
446 is to simply emit a gcc error. */
447 TRY
448 {
449 struct block_symbol sym;
450
451 sym = lookup_symbol (identifier, context->block (), domain, NULL);
452 if (sym.symbol != NULL)
453 {
454 convert_symbol_sym (context, identifier, sym, domain);
455 found = 1;
456 }
457 else if (domain == VAR_DOMAIN)
458 {
459 struct bound_minimal_symbol bmsym;
460
461 bmsym = lookup_minimal_symbol (identifier, NULL, NULL);
462 if (bmsym.minsym != NULL)
463 {
464 convert_symbol_bmsym (context, bmsym);
465 found = 1;
466 }
467 }
468 }
469
470 CATCH (e, RETURN_MASK_ALL)
471 {
472 context->plugin ().error (e.message);
473 }
474 END_CATCH
475
476 if (compile_debug && !found)
477 fprintf_unfiltered (gdb_stdlog,
478 "gcc_convert_symbol \"%s\": lookup_symbol failed\n",
479 identifier);
480 return;
481 }
482
483 /* See compile-internal.h. */
484
485 gcc_address
486 gcc_symbol_address (void *datum, struct gcc_c_context *gcc_context,
487 const char *identifier)
488 {
489 compile_c_instance *context
490 = static_cast<compile_c_instance *> (datum);
491 gcc_address result = 0;
492 int found = 0;
493
494 /* We can't allow exceptions to escape out of this callback. Safest
495 is to simply emit a gcc error. */
496 TRY
497 {
498 struct symbol *sym;
499
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)
503 {
504 if (compile_debug)
505 fprintf_unfiltered (gdb_stdlog,
506 "gcc_symbol_address \"%s\": full symbol\n",
507 identifier);
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);
511 found = 1;
512 }
513 else
514 {
515 struct bound_minimal_symbol msym;
516
517 msym = lookup_bound_minimal_symbol (identifier);
518 if (msym.minsym != NULL)
519 {
520 if (compile_debug)
521 fprintf_unfiltered (gdb_stdlog,
522 "gcc_symbol_address \"%s\": minimal "
523 "symbol\n",
524 identifier);
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);
528 found = 1;
529 }
530 }
531 }
532
533 CATCH (e, RETURN_MASK_ERROR)
534 {
535 context->plugin ().error (e.message);
536 }
537 END_CATCH
538
539 if (compile_debug && !found)
540 fprintf_unfiltered (gdb_stdlog,
541 "gcc_symbol_address \"%s\": failed\n",
542 identifier);
543 return result;
544 }
545
546 \f
547
548 /* A hash function for symbol names. */
549
550 static hashval_t
551 hash_symname (const void *a)
552 {
553 const struct symbol *sym = (const struct symbol *) a;
554
555 return htab_hash_string (SYMBOL_NATURAL_NAME (sym));
556 }
557
558 /* A comparison function for hash tables that just looks at symbol
559 names. */
560
561 static int
562 eq_symname (const void *a, const void *b)
563 {
564 const struct symbol *syma = (const struct symbol *) a;
565 const struct symbol *symb = (const struct symbol *) b;
566
567 return strcmp (SYMBOL_NATURAL_NAME (syma), SYMBOL_NATURAL_NAME (symb)) == 0;
568 }
569
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. */
572
573 static int
574 symbol_seen (htab_t hashtab, struct symbol *sym)
575 {
576 void **slot;
577
578 slot = htab_find_slot (hashtab, sym, INSERT);
579 if (*slot != NULL)
580 return 1;
581
582 *slot = sym;
583 return 0;
584 }
585
586 /* Generate C code to compute the length of a VLA. */
587
588 static void
589 generate_vla_size (compile_instance *compiler,
590 string_file &stream,
591 struct gdbarch *gdbarch,
592 unsigned char *registers_used,
593 CORE_ADDR pc,
594 struct type *type,
595 struct symbol *sym)
596 {
597 type = check_typedef (type);
598
599 if (TYPE_IS_REFERENCE (type))
600 type = check_typedef (TYPE_TARGET_TYPE (type));
601
602 switch (TYPE_CODE (type))
603 {
604 case TYPE_CODE_RANGE:
605 {
606 if (TYPE_HIGH_BOUND_KIND (type) == PROP_LOCEXPR
607 || TYPE_HIGH_BOUND_KIND (type) == PROP_LOCLIST)
608 {
609 const struct dynamic_prop *prop = &TYPE_RANGE_DATA (type)->high;
610 std::string name = c_get_range_decl_name (prop);
611
612 dwarf2_compile_property_to_c (stream, name.c_str (),
613 gdbarch, registers_used,
614 prop, pc, sym);
615 }
616 }
617 break;
618
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);
624 break;
625
626 case TYPE_CODE_UNION:
627 case TYPE_CODE_STRUCT:
628 {
629 int i;
630
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);
635 }
636 break;
637 }
638 }
639
640 /* Generate C code to compute the address of SYM. */
641
642 static void
643 generate_c_for_for_one_variable (compile_instance *compiler,
644 string_file &stream,
645 struct gdbarch *gdbarch,
646 unsigned char *registers_used,
647 CORE_ADDR pc,
648 struct symbol *sym)
649 {
650
651 TRY
652 {
653 if (is_dynamic_type (SYMBOL_TYPE (sym)))
654 {
655 /* We need to emit to a temporary buffer in case an error
656 occurs in the middle. */
657 string_file local_file;
658
659 generate_vla_size (compiler, local_file, gdbarch, registers_used, pc,
660 SYMBOL_TYPE (sym), sym);
661
662 stream.write (local_file.c_str (), local_file.size ());
663 }
664
665 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
666 {
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;
672
673 SYMBOL_COMPUTED_OPS (sym)->generate_c_location (sym, local_file,
674 gdbarch,
675 registers_used,
676 pc,
677 generated_name.get ());
678 stream.write (local_file.c_str (), local_file.size ());
679 }
680 else
681 {
682 switch (SYMBOL_CLASS (sym))
683 {
684 case LOC_REGISTER:
685 case LOC_ARG:
686 case LOC_REF_ARG:
687 case LOC_REGPARM_ADDR:
688 case LOC_LOCAL:
689 error (_("Local symbol unhandled when generating C code."));
690
691 case LOC_COMPUTED:
692 gdb_assert_not_reached (_("LOC_COMPUTED variable "
693 "missing a method."));
694
695 default:
696 /* Nothing to do for all other cases, as they don't represent
697 local variables. */
698 break;
699 }
700 }
701 }
702
703 CATCH (e, RETURN_MASK_ERROR)
704 {
705 compiler->insert_symbol_error (sym, e.message);
706 }
707 END_CATCH
708 }
709
710 /* See compile-c.h. */
711
712 gdb::unique_xmalloc_ptr<unsigned char>
713 generate_c_for_variable_locations (compile_instance *compiler,
714 string_file &stream,
715 struct gdbarch *gdbarch,
716 const struct block *block,
717 CORE_ADDR pc)
718 {
719 const struct block *static_block = block_static_block (block);
720
721 /* If we're already in the static or global block, there is nothing
722 to write. */
723 if (static_block == NULL || block == static_block)
724 return NULL;
725
726 gdb::unique_xmalloc_ptr<unsigned char> registers_used
727 (XCNEWVEC (unsigned char, gdbarch_num_regs (gdbarch)));
728
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,
732 xcalloc, xfree));
733
734 while (1)
735 {
736 struct symbol *sym;
737 struct block_iterator iter;
738
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);
742 sym != NULL;
743 sym = block_iterator_next (&iter))
744 {
745 if (!symbol_seen (symhash.get (), sym))
746 generate_c_for_for_one_variable (compiler, stream, gdbarch,
747 registers_used.get (), pc, sym);
748 }
749
750 /* If we just finished the outermost block of a function, we're
751 done. */
752 if (BLOCK_FUNCTION (block) != NULL)
753 break;
754 block = BLOCK_SUPERBLOCK (block);
755 }
756
757 return registers_used;
758 }
This page took 0.048212 seconds and 4 git commands to generate.