xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/compile/compile-c-symbols.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /* Convert symbols from GDB to GCC
2 
3    Copyright (C) 2014-2019 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 
35 
36 /* Compute the name of the pointer representing a local symbol's
37    address.  */
38 
39 gdb::unique_xmalloc_ptr<char>
40 c_symbol_substitution_name (struct symbol *sym)
41 {
42   return gdb::unique_xmalloc_ptr<char>
43     (concat ("__", SYMBOL_NATURAL_NAME (sym), "_ptr", (char *) NULL));
44 }
45 
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
51    scope.)  */
52 
53 static void
54 convert_one_symbol (compile_c_instance *context,
55 		    struct block_symbol sym,
56 		    int is_global,
57 		    int is_local)
58 {
59   gcc_type sym_type;
60   const char *filename = symbol_symtab (sym.symbol)->filename;
61   unsigned short line = SYMBOL_LINE (sym.symbol);
62 
63   context->error_symbol_once (sym.symbol);
64 
65   if (SYMBOL_CLASS (sym.symbol) == LOC_LABEL)
66     sym_type = 0;
67   else
68     sym_type = context->convert_type (SYMBOL_TYPE (sym.symbol));
69 
70   if (SYMBOL_DOMAIN (sym.symbol) == STRUCT_DOMAIN)
71     {
72       /* Binding a tag, so we don't need to build a decl.  */
73       context->plugin ().tagbind (SYMBOL_NATURAL_NAME (sym.symbol),
74 				  sym_type, filename, line);
75     }
76   else
77     {
78       gcc_decl decl;
79       enum gcc_c_symbol_kind kind;
80       CORE_ADDR addr = 0;
81       gdb::unique_xmalloc_ptr<char> symbol_name;
82 
83       switch (SYMBOL_CLASS (sym.symbol))
84 	{
85 	case LOC_TYPEDEF:
86 	  kind = GCC_C_SYMBOL_TYPEDEF;
87 	  break;
88 
89 	case LOC_LABEL:
90 	  kind = GCC_C_SYMBOL_LABEL;
91 	  addr = SYMBOL_VALUE_ADDRESS (sym.symbol);
92 	  break;
93 
94 	case LOC_BLOCK:
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);
99 	  break;
100 
101 	case LOC_CONST:
102 	  if (TYPE_CODE (SYMBOL_TYPE (sym.symbol)) == TYPE_CODE_ENUM)
103 	    {
104 	      /* Already handled by convert_enum.  */
105 	      return;
106 	    }
107 	  context->plugin ().build_constant
108 	    (sym_type, SYMBOL_NATURAL_NAME (sym.symbol),
109 	     SYMBOL_VALUE (sym.symbol),
110 	     filename, line);
111 	  return;
112 
113 	case LOC_CONST_BYTES:
114 	  error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."),
115 		 SYMBOL_PRINT_NAME (sym.symbol));
116 
117 	case LOC_UNDEF:
118 	  internal_error (__FILE__, __LINE__, _("LOC_UNDEF found for \"%s\"."),
119 			  SYMBOL_PRINT_NAME (sym.symbol));
120 
121 	case LOC_COMMON_BLOCK:
122 	  error (_("Fortran common block is unsupported for compilation "
123 		   "evaluaton of symbol \"%s\"."),
124 		 SYMBOL_PRINT_NAME (sym.symbol));
125 
126 	case LOC_OPTIMIZED_OUT:
127 	  error (_("Symbol \"%s\" cannot be used for compilation evaluation "
128 		   "as it is optimized out."),
129 		 SYMBOL_PRINT_NAME (sym.symbol));
130 
131 	case LOC_COMPUTED:
132 	  if (is_local)
133 	    goto substitution;
134 	  /* Probably TLS here.  */
135 	  warning (_("Symbol \"%s\" is thread-local and currently can only "
136 		     "be referenced from the current thread in "
137 		     "compiled code."),
138 		   SYMBOL_PRINT_NAME (sym.symbol));
139 	  /* FALLTHROUGH */
140 	case LOC_UNRESOLVED:
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
144 	     by their name.  */
145 	  {
146 	    struct value *val;
147 	    struct frame_info *frame = NULL;
148 
149 	    if (symbol_read_needs_frame (sym.symbol))
150 	      {
151 		frame = get_selected_frame (NULL);
152 		if (frame == NULL)
153 		  error (_("Symbol \"%s\" cannot be used because "
154 			   "there is no selected frame"),
155 			 SYMBOL_PRINT_NAME (sym.symbol));
156 	      }
157 
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 		     SYMBOL_PRINT_NAME (sym.symbol));
163 
164 	    kind = GCC_C_SYMBOL_VARIABLE;
165 	    addr = value_address (val);
166 	  }
167 	  break;
168 
169 
170 	case LOC_REGISTER:
171 	case LOC_ARG:
172 	case LOC_REF_ARG:
173 	case LOC_REGPARM_ADDR:
174 	case LOC_LOCAL:
175 	substitution:
176 	  kind = GCC_C_SYMBOL_VARIABLE;
177 	  symbol_name = c_symbol_substitution_name (sym.symbol);
178 	  break;
179 
180 	case LOC_STATIC:
181 	  kind = GCC_C_SYMBOL_VARIABLE;
182 	  addr = SYMBOL_VALUE_ADDRESS (sym.symbol);
183 	  break;
184 
185 	case LOC_FINAL_VALUE:
186 	default:
187 	  gdb_assert_not_reached ("Unreachable case in convert_one_symbol.");
188 
189 	}
190 
191       /* Don't emit local variable decls for a raw expression.  */
192       if (context->scope () != COMPILE_I_RAW_SCOPE
193 	  || symbol_name == NULL)
194 	{
195 	  decl = context->plugin ().build_decl
196 	    (SYMBOL_NATURAL_NAME (sym.symbol),
197 	     kind,
198 	     sym_type,
199 	     symbol_name.get (), addr,
200 	     filename, line);
201 
202 	  context->plugin ().bind (decl, is_global);
203 	}
204     }
205 }
206 
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.  */
210 
211 static void
212 convert_symbol_sym (compile_c_instance *context, const char *identifier,
213 		    struct block_symbol sym, domain_enum domain)
214 {
215   const struct block *static_block;
216   int is_local_symbol;
217 
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:
221 
222      int x; // Global.
223      int func(void)
224      {
225      int x;
226      // At this spot, evaluate "extern int x; x"
227      }
228   */
229 
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);
233   if (is_local_symbol)
234     {
235       struct block_symbol global_sym;
236 
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))
242 	{
243 	  if (compile_debug)
244 	    fprintf_unfiltered (gdb_stdlog,
245 				"gcc_convert_symbol \"%s\": global symbol\n",
246 				identifier);
247 	  convert_one_symbol (context, global_sym, 1, 0);
248 	}
249     }
250 
251   if (compile_debug)
252     fprintf_unfiltered (gdb_stdlog,
253 			"gcc_convert_symbol \"%s\": local symbol\n",
254 			identifier);
255   convert_one_symbol (context, sym, 0, is_local_symbol);
256 }
257 
258 /* Convert a minimal symbol to its gcc form.  CONTEXT is the compiler
259    to use and BMSYM is the minimal symbol to convert.  */
260 
261 static void
262 convert_symbol_bmsym (compile_c_instance *context,
263 		      struct bound_minimal_symbol bmsym)
264 {
265   struct minimal_symbol *msym = bmsym.minsym;
266   struct objfile *objfile = bmsym.objfile;
267   struct type *type;
268   enum gcc_c_symbol_kind kind;
269   gcc_type sym_type;
270   gcc_decl decl;
271   CORE_ADDR addr;
272 
273   addr = MSYMBOL_VALUE_ADDRESS (objfile, msym);
274 
275   /* Conversion copied from write_exp_msymbol.  */
276   switch (MSYMBOL_TYPE (msym))
277     {
278     case mst_text:
279     case mst_file_text:
280     case mst_solib_trampoline:
281       type = objfile_type (objfile)->nodebug_text_symbol;
282       kind = GCC_C_SYMBOL_FUNCTION;
283       break;
284 
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);
289       break;
290 
291     case mst_data:
292     case mst_file_data:
293     case mst_bss:
294     case mst_file_bss:
295       type = objfile_type (objfile)->nodebug_data_symbol;
296       kind = GCC_C_SYMBOL_VARIABLE;
297       break;
298 
299     case mst_slot_got_plt:
300       type = objfile_type (objfile)->nodebug_got_plt_symbol;
301       kind = GCC_C_SYMBOL_FUNCTION;
302       break;
303 
304     default:
305       type = objfile_type (objfile)->nodebug_unknown_symbol;
306       kind = GCC_C_SYMBOL_VARIABLE;
307       break;
308     }
309 
310   sym_type = context->convert_type (type);
311   decl = context->plugin ().build_decl (MSYMBOL_NATURAL_NAME (msym),
312 					kind, sym_type, NULL, addr,
313 					NULL, 0);
314   context->plugin ().bind (decl, 1 /* is_global */);
315 }
316 
317 /* See compile-internal.h.  */
318 
319 void
320 gcc_convert_symbol (void *datum,
321 		    struct gcc_c_context *gcc_context,
322 		    enum gcc_c_oracle_request request,
323 		    const char *identifier)
324 {
325   compile_c_instance *context
326     = static_cast<compile_c_instance *> (datum);
327   domain_enum domain;
328   int found = 0;
329 
330   switch (request)
331     {
332     case GCC_C_ORACLE_SYMBOL:
333       domain = VAR_DOMAIN;
334       break;
335     case GCC_C_ORACLE_TAG:
336       domain = STRUCT_DOMAIN;
337       break;
338     case GCC_C_ORACLE_LABEL:
339       domain = LABEL_DOMAIN;
340       break;
341     default:
342       gdb_assert_not_reached ("Unrecognized oracle request.");
343     }
344 
345   /* We can't allow exceptions to escape out of this callback.  Safest
346      is to simply emit a gcc error.  */
347   TRY
348     {
349       struct block_symbol sym;
350 
351       sym = lookup_symbol (identifier, context->block (), domain, NULL);
352       if (sym.symbol != NULL)
353 	{
354 	  convert_symbol_sym (context, identifier, sym, domain);
355 	  found = 1;
356 	}
357       else if (domain == VAR_DOMAIN)
358 	{
359 	  struct bound_minimal_symbol bmsym;
360 
361 	  bmsym = lookup_minimal_symbol (identifier, NULL, NULL);
362 	  if (bmsym.minsym != NULL)
363 	    {
364 	      convert_symbol_bmsym (context, bmsym);
365 	      found = 1;
366 	    }
367 	}
368     }
369 
370   CATCH (e, RETURN_MASK_ALL)
371     {
372       context->plugin ().error (e.message);
373     }
374   END_CATCH
375 
376   if (compile_debug && !found)
377     fprintf_unfiltered (gdb_stdlog,
378 			"gcc_convert_symbol \"%s\": lookup_symbol failed\n",
379 			identifier);
380   return;
381 }
382 
383 /* See compile-internal.h.  */
384 
385 gcc_address
386 gcc_symbol_address (void *datum, struct gcc_c_context *gcc_context,
387 		    const char *identifier)
388 {
389   compile_c_instance *context
390     = static_cast<compile_c_instance *> (datum);
391   gcc_address result = 0;
392   int found = 0;
393 
394   /* We can't allow exceptions to escape out of this callback.  Safest
395      is to simply emit a gcc error.  */
396   TRY
397     {
398       struct symbol *sym;
399 
400       /* We only need global functions here.  */
401       sym = lookup_symbol (identifier, NULL, VAR_DOMAIN, NULL).symbol;
402       if (sym != NULL && SYMBOL_CLASS (sym) == LOC_BLOCK)
403 	{
404 	  if (compile_debug)
405 	    fprintf_unfiltered (gdb_stdlog,
406 				"gcc_symbol_address \"%s\": full symbol\n",
407 				identifier);
408 	  result = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym));
409 	  if (TYPE_GNU_IFUNC (SYMBOL_TYPE (sym)))
410 	    result = gnu_ifunc_resolve_addr (target_gdbarch (), result);
411 	  found = 1;
412 	}
413       else
414 	{
415 	  struct bound_minimal_symbol msym;
416 
417 	  msym = lookup_bound_minimal_symbol (identifier);
418 	  if (msym.minsym != NULL)
419 	    {
420 	      if (compile_debug)
421 		fprintf_unfiltered (gdb_stdlog,
422 				    "gcc_symbol_address \"%s\": minimal "
423 				    "symbol\n",
424 				    identifier);
425 	      result = BMSYMBOL_VALUE_ADDRESS (msym);
426 	      if (MSYMBOL_TYPE (msym.minsym) == mst_text_gnu_ifunc)
427 		result = gnu_ifunc_resolve_addr (target_gdbarch (), result);
428 	      found = 1;
429 	    }
430 	}
431     }
432 
433   CATCH (e, RETURN_MASK_ERROR)
434     {
435       context->plugin ().error (e.message);
436     }
437   END_CATCH
438 
439   if (compile_debug && !found)
440     fprintf_unfiltered (gdb_stdlog,
441 			"gcc_symbol_address \"%s\": failed\n",
442 			identifier);
443   return result;
444 }
445 
446 
447 
448 /* A hash function for symbol names.  */
449 
450 static hashval_t
451 hash_symname (const void *a)
452 {
453   const struct symbol *sym = (const struct symbol *) a;
454 
455   return htab_hash_string (SYMBOL_NATURAL_NAME (sym));
456 }
457 
458 /* A comparison function for hash tables that just looks at symbol
459    names.  */
460 
461 static int
462 eq_symname (const void *a, const void *b)
463 {
464   const struct symbol *syma = (const struct symbol *) a;
465   const struct symbol *symb = (const struct symbol *) b;
466 
467   return strcmp (SYMBOL_NATURAL_NAME (syma), SYMBOL_NATURAL_NAME (symb)) == 0;
468 }
469 
470 /* If a symbol with the same name as SYM is already in HASHTAB, return
471    1.  Otherwise, add SYM to HASHTAB and return 0.  */
472 
473 static int
474 symbol_seen (htab_t hashtab, struct symbol *sym)
475 {
476   void **slot;
477 
478   slot = htab_find_slot (hashtab, sym, INSERT);
479   if (*slot != NULL)
480     return 1;
481 
482   *slot = sym;
483   return 0;
484 }
485 
486 /* Generate C code to compute the length of a VLA.  */
487 
488 static void
489 generate_vla_size (compile_instance *compiler,
490 		   string_file *stream,
491 		   struct gdbarch *gdbarch,
492 		   unsigned char *registers_used,
493 		   CORE_ADDR pc,
494 		   struct type *type,
495 		   struct symbol *sym)
496 {
497   type = check_typedef (type);
498 
499   if (TYPE_IS_REFERENCE (type))
500     type = check_typedef (TYPE_TARGET_TYPE (type));
501 
502   switch (TYPE_CODE (type))
503     {
504     case TYPE_CODE_RANGE:
505       {
506 	if (TYPE_HIGH_BOUND_KIND (type) == PROP_LOCEXPR
507 	    || TYPE_HIGH_BOUND_KIND (type) == PROP_LOCLIST)
508 	  {
509 	    const struct dynamic_prop *prop = &TYPE_RANGE_DATA (type)->high;
510 	    std::string name = c_get_range_decl_name (prop);
511 
512 	    dwarf2_compile_property_to_c (stream, name.c_str (),
513 					  gdbarch, registers_used,
514 					  prop, pc, sym);
515 	  }
516       }
517       break;
518 
519     case TYPE_CODE_ARRAY:
520       generate_vla_size (compiler, stream, gdbarch, registers_used, pc,
521 			 TYPE_INDEX_TYPE (type), sym);
522       generate_vla_size (compiler, stream, gdbarch, registers_used, pc,
523 			 TYPE_TARGET_TYPE (type), sym);
524       break;
525 
526     case TYPE_CODE_UNION:
527     case TYPE_CODE_STRUCT:
528       {
529 	int i;
530 
531 	for (i = 0; i < TYPE_NFIELDS (type); ++i)
532 	  if (!field_is_static (&TYPE_FIELD (type, i)))
533 	    generate_vla_size (compiler, stream, gdbarch, registers_used, pc,
534 			       TYPE_FIELD_TYPE (type, i), sym);
535       }
536       break;
537     }
538 }
539 
540 /* Generate C code to compute the address of SYM.  */
541 
542 static void
543 generate_c_for_for_one_variable (compile_instance *compiler,
544 				 string_file *stream,
545 				 struct gdbarch *gdbarch,
546 				 unsigned char *registers_used,
547 				 CORE_ADDR pc,
548 				 struct symbol *sym)
549 {
550 
551   TRY
552     {
553       if (is_dynamic_type (SYMBOL_TYPE (sym)))
554 	{
555 	  /* We need to emit to a temporary buffer in case an error
556 	     occurs in the middle.  */
557 	  string_file local_file;
558 
559 	  generate_vla_size (compiler, &local_file, gdbarch, registers_used, pc,
560 			     SYMBOL_TYPE (sym), sym);
561 
562 	  stream->write (local_file.c_str (), local_file.size ());
563 	}
564 
565       if (SYMBOL_COMPUTED_OPS (sym) != NULL)
566 	{
567 	  gdb::unique_xmalloc_ptr<char> generated_name
568 	    = c_symbol_substitution_name (sym);
569 	  /* We need to emit to a temporary buffer in case an error
570 	     occurs in the middle.  */
571 	  string_file local_file;
572 
573 	  SYMBOL_COMPUTED_OPS (sym)->generate_c_location (sym, &local_file,
574 							  gdbarch,
575 							  registers_used,
576 							  pc,
577 							  generated_name.get ());
578 	  stream->write (local_file.c_str (), local_file.size ());
579 	}
580       else
581 	{
582 	  switch (SYMBOL_CLASS (sym))
583 	    {
584 	    case LOC_REGISTER:
585 	    case LOC_ARG:
586 	    case LOC_REF_ARG:
587 	    case LOC_REGPARM_ADDR:
588 	    case LOC_LOCAL:
589 	      error (_("Local symbol unhandled when generating C code."));
590 
591 	    case LOC_COMPUTED:
592 	      gdb_assert_not_reached (_("LOC_COMPUTED variable "
593 					"missing a method."));
594 
595 	    default:
596 	      /* Nothing to do for all other cases, as they don't represent
597 		 local variables.  */
598 	      break;
599 	    }
600 	}
601     }
602 
603   CATCH (e, RETURN_MASK_ERROR)
604     {
605       compiler->insert_symbol_error (sym, e.message);
606     }
607   END_CATCH
608 }
609 
610 /* See compile-c.h.  */
611 
612 gdb::unique_xmalloc_ptr<unsigned char>
613 generate_c_for_variable_locations (compile_instance *compiler,
614 				   string_file *stream,
615 				   struct gdbarch *gdbarch,
616 				   const struct block *block,
617 				   CORE_ADDR pc)
618 {
619   const struct block *static_block = block_static_block (block);
620 
621   /* If we're already in the static or global block, there is nothing
622      to write.  */
623   if (static_block == NULL || block == static_block)
624     return NULL;
625 
626   gdb::unique_xmalloc_ptr<unsigned char> registers_used
627     (XCNEWVEC (unsigned char, gdbarch_num_regs (gdbarch)));
628 
629   /* Ensure that a given name is only entered once.  This reflects the
630      reality of shadowing.  */
631   htab_up symhash (htab_create_alloc (1, hash_symname, eq_symname, NULL,
632 				      xcalloc, xfree));
633 
634   while (1)
635     {
636       struct symbol *sym;
637       struct block_iterator iter;
638 
639       /* Iterate over symbols in this block, generating code to
640 	 compute the location of each local variable.  */
641       for (sym = block_iterator_first (block, &iter);
642 	   sym != NULL;
643 	   sym = block_iterator_next (&iter))
644 	{
645 	  if (!symbol_seen (symhash.get (), sym))
646 	    generate_c_for_for_one_variable (compiler, stream, gdbarch,
647 					     registers_used.get (), pc, sym);
648 	}
649 
650       /* If we just finished the outermost block of a function, we're
651 	 done.  */
652       if (BLOCK_FUNCTION (block) != NULL)
653 	break;
654       block = BLOCK_SUPERBLOCK (block);
655     }
656 
657   return registers_used;
658 }
659