xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/compile/compile-cplus-symbols.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17f2ac410Schristos /* Convert symbols from GDB to GCC
27f2ac410Schristos 
3*6881a400Schristos    Copyright (C) 2014-2023 Free Software Foundation, Inc.
47f2ac410Schristos 
57f2ac410Schristos    This file is part of GDB.
67f2ac410Schristos 
77f2ac410Schristos    This program is free software; you can redistribute it and/or modify
87f2ac410Schristos    it under the terms of the GNU General Public License as published by
97f2ac410Schristos    the Free Software Foundation; either version 3 of the License, or
107f2ac410Schristos    (at your option) any later version.
117f2ac410Schristos 
127f2ac410Schristos    This program is distributed in the hope that it will be useful,
137f2ac410Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
147f2ac410Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
157f2ac410Schristos    GNU General Public License for more details.
167f2ac410Schristos 
177f2ac410Schristos    You should have received a copy of the GNU General Public License
187f2ac410Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
197f2ac410Schristos 
207f2ac410Schristos 
217f2ac410Schristos #include "defs.h"
227f2ac410Schristos #include "compile-internal.h"
237f2ac410Schristos #include "compile-cplus.h"
247d62b00eSchristos #include "gdbsupport/gdb_assert.h"
257f2ac410Schristos #include "symtab.h"
267f2ac410Schristos #include "parser-defs.h"
277f2ac410Schristos #include "block.h"
287f2ac410Schristos #include "objfiles.h"
297f2ac410Schristos #include "compile.h"
307f2ac410Schristos #include "value.h"
317f2ac410Schristos #include "exceptions.h"
327f2ac410Schristos #include "gdbtypes.h"
337d62b00eSchristos #include "dwarf2/loc.h"
347f2ac410Schristos #include "cp-support.h"
357f2ac410Schristos #include "gdbcmd.h"
367f2ac410Schristos #include "compile-c.h"
377f2ac410Schristos 
387f2ac410Schristos /* Convert a given symbol, SYM, to the compiler's representation.
397f2ac410Schristos    INSTANCE is the compiler instance.  IS_GLOBAL is true if the
407f2ac410Schristos    symbol came from the global scope.  IS_LOCAL is true if the symbol
417f2ac410Schristos    came from a local scope.  (Note that the two are not strictly
427f2ac410Schristos    inverses because the symbol might have come from the static
437f2ac410Schristos    scope.)  */
447f2ac410Schristos 
457f2ac410Schristos static void
467f2ac410Schristos convert_one_symbol (compile_cplus_instance *instance,
477f2ac410Schristos 		    struct block_symbol sym, bool is_global, bool is_local)
487f2ac410Schristos {
497f2ac410Schristos   /* Squash compiler warning.  */
507f2ac410Schristos   gcc_type sym_type = 0;
51*6881a400Schristos   const char *filename = sym.symbol->symtab ()->filename;
52*6881a400Schristos   unsigned short line = sym.symbol->line ();
537f2ac410Schristos 
547f2ac410Schristos   instance->error_symbol_once (sym.symbol);
557f2ac410Schristos 
56*6881a400Schristos   if (sym.symbol->aclass () == LOC_LABEL)
577f2ac410Schristos     sym_type = 0;
587f2ac410Schristos   else
59*6881a400Schristos     sym_type = instance->convert_type (sym.symbol->type ());
607f2ac410Schristos 
61*6881a400Schristos   if (sym.symbol->domain () == STRUCT_DOMAIN)
627f2ac410Schristos     {
637f2ac410Schristos       /* Nothing to do.  */
647f2ac410Schristos     }
657f2ac410Schristos   else
667f2ac410Schristos     {
677f2ac410Schristos       /* Squash compiler warning.  */
687f2ac410Schristos       gcc_cp_symbol_kind_flags kind = GCC_CP_FLAG_BASE;
697f2ac410Schristos       CORE_ADDR addr = 0;
707f2ac410Schristos       std::string name;
717f2ac410Schristos       gdb::unique_xmalloc_ptr<char> symbol_name;
727f2ac410Schristos 
73*6881a400Schristos       switch (sym.symbol->aclass ())
747f2ac410Schristos 	{
757f2ac410Schristos 	case LOC_TYPEDEF:
76*6881a400Schristos 	  if (sym.symbol->type ()->code () == TYPE_CODE_TYPEDEF)
777f2ac410Schristos 	    kind = GCC_CP_SYMBOL_TYPEDEF;
78*6881a400Schristos 	  else  if (sym.symbol->type ()->code () == TYPE_CODE_NAMESPACE)
797f2ac410Schristos 	    return;
807f2ac410Schristos 	  break;
817f2ac410Schristos 
827f2ac410Schristos 	case LOC_LABEL:
837f2ac410Schristos 	  kind = GCC_CP_SYMBOL_LABEL;
84*6881a400Schristos 	  addr = sym.symbol->value_address ();
857f2ac410Schristos 	  break;
867f2ac410Schristos 
877f2ac410Schristos 	case LOC_BLOCK:
887f2ac410Schristos 	  {
897f2ac410Schristos 	    kind = GCC_CP_SYMBOL_FUNCTION;
90*6881a400Schristos 	    addr = sym.symbol->value_block()->start ();
91*6881a400Schristos 	    if (is_global && sym.symbol->type ()->is_gnu_ifunc ())
927f2ac410Schristos 	      addr = gnu_ifunc_resolve_addr (target_gdbarch (), addr);
937f2ac410Schristos 	  }
947f2ac410Schristos 	  break;
957f2ac410Schristos 
967f2ac410Schristos 	case LOC_CONST:
97*6881a400Schristos 	  if (sym.symbol->type ()->code () == TYPE_CODE_ENUM)
987f2ac410Schristos 	    {
997f2ac410Schristos 	      /* Already handled by convert_enum.  */
1007f2ac410Schristos 	      return;
1017f2ac410Schristos 	    }
1027f2ac410Schristos 	  instance->plugin ().build_constant
1037d62b00eSchristos 	    (sym_type, sym.symbol->natural_name (),
104*6881a400Schristos 	     sym.symbol->value_longest (), filename, line);
1057f2ac410Schristos 	  return;
1067f2ac410Schristos 
1077f2ac410Schristos 	case LOC_CONST_BYTES:
1087f2ac410Schristos 	  error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."),
1097d62b00eSchristos 		 sym.symbol->print_name ());
1107f2ac410Schristos 
1117f2ac410Schristos 	case LOC_UNDEF:
112*6881a400Schristos 	  internal_error (_("LOC_UNDEF found for \"%s\"."),
1137d62b00eSchristos 			  sym.symbol->print_name ());
1147f2ac410Schristos 
1157f2ac410Schristos 	case LOC_COMMON_BLOCK:
1167f2ac410Schristos 	  error (_("Fortran common block is unsupported for compilation "
1177f2ac410Schristos 		   "evaluaton of symbol \"%s\"."),
1187d62b00eSchristos 		 sym.symbol->print_name ());
1197f2ac410Schristos 
1207f2ac410Schristos 	case LOC_OPTIMIZED_OUT:
1217f2ac410Schristos 	  error (_("Symbol \"%s\" cannot be used for compilation evaluation "
1227f2ac410Schristos 		   "as it is optimized out."),
1237d62b00eSchristos 		 sym.symbol->print_name ());
1247f2ac410Schristos 
1257f2ac410Schristos 	case LOC_COMPUTED:
1267f2ac410Schristos 	  if (is_local)
1277f2ac410Schristos 	    goto substitution;
1287f2ac410Schristos 	  /* Probably TLS here.  */
1297f2ac410Schristos 	  warning (_("Symbol \"%s\" is thread-local and currently can only "
1307f2ac410Schristos 		     "be referenced from the current thread in "
1317f2ac410Schristos 		     "compiled code."),
1327d62b00eSchristos 		   sym.symbol->print_name ());
1337f2ac410Schristos 	  /* FALLTHROUGH */
1347f2ac410Schristos 	case LOC_UNRESOLVED:
1357f2ac410Schristos 	  /* 'symbol_name' cannot be used here as that one is used only for
1367f2ac410Schristos 	     local variables from compile_dwarf_expr_to_c.
1377f2ac410Schristos 	     Global variables can be accessed by GCC only by their address, not
1387f2ac410Schristos 	     by their name.  */
1397f2ac410Schristos 	  {
1407f2ac410Schristos 	    struct value *val;
141*6881a400Schristos 	    frame_info_ptr frame = nullptr;
1427f2ac410Schristos 
1437f2ac410Schristos 	    if (symbol_read_needs_frame (sym.symbol))
1447f2ac410Schristos 	      {
1457f2ac410Schristos 		frame = get_selected_frame (nullptr);
1467f2ac410Schristos 		if (frame == nullptr)
1477f2ac410Schristos 		  error (_("Symbol \"%s\" cannot be used because "
1487f2ac410Schristos 			   "there is no selected frame"),
1497d62b00eSchristos 			 sym.symbol->print_name ());
1507f2ac410Schristos 	      }
1517f2ac410Schristos 
1527f2ac410Schristos 	    val = read_var_value (sym.symbol, sym.block, frame);
1537f2ac410Schristos 	    if (VALUE_LVAL (val) != lval_memory)
1547f2ac410Schristos 	      error (_("Symbol \"%s\" cannot be used for compilation "
1557f2ac410Schristos 		       "evaluation as its address has not been found."),
1567d62b00eSchristos 		     sym.symbol->print_name ());
1577f2ac410Schristos 
1587f2ac410Schristos 	    kind = GCC_CP_SYMBOL_VARIABLE;
1597f2ac410Schristos 	    addr = value_address (val);
1607f2ac410Schristos 	  }
1617f2ac410Schristos 	  break;
1627f2ac410Schristos 
1637f2ac410Schristos 
1647f2ac410Schristos 	case LOC_REGISTER:
1657f2ac410Schristos 	case LOC_ARG:
1667f2ac410Schristos 	case LOC_REF_ARG:
1677f2ac410Schristos 	case LOC_REGPARM_ADDR:
1687f2ac410Schristos 	case LOC_LOCAL:
1697f2ac410Schristos 	substitution:
1707f2ac410Schristos 	  kind = GCC_CP_SYMBOL_VARIABLE;
1717f2ac410Schristos 	  symbol_name = c_symbol_substitution_name (sym.symbol);
1727f2ac410Schristos 	  break;
1737f2ac410Schristos 
1747f2ac410Schristos 	case LOC_STATIC:
1757f2ac410Schristos 	  kind = GCC_CP_SYMBOL_VARIABLE;
176*6881a400Schristos 	  addr = sym.symbol->value_address ();
1777f2ac410Schristos 	  break;
1787f2ac410Schristos 
1797f2ac410Schristos 	case LOC_FINAL_VALUE:
1807f2ac410Schristos 	default:
1817f2ac410Schristos 	  gdb_assert_not_reached ("Unreachable case in convert_one_symbol.");
1827f2ac410Schristos 	}
1837f2ac410Schristos 
1847f2ac410Schristos       /* Don't emit local variable decls for a raw expression.  */
1857f2ac410Schristos       if (instance->scope () != COMPILE_I_RAW_SCOPE || symbol_name == nullptr)
1867f2ac410Schristos 	{
1877f2ac410Schristos 	  /* For non-local symbols, create/push a new scope so that the
1887f2ac410Schristos 	     symbol is properly scoped to the plug-in.  */
1897f2ac410Schristos 	  if (!is_local)
1907f2ac410Schristos 	    {
1917f2ac410Schristos 	      compile_scope scope
1927d62b00eSchristos 		= instance->new_scope (sym.symbol->natural_name (),
193*6881a400Schristos 				       sym.symbol->type ());
1947f2ac410Schristos 	      if (scope.nested_type () != GCC_TYPE_NONE)
1957f2ac410Schristos 		{
1967f2ac410Schristos 		  /* We found a symbol for this type that was defined inside
1977d62b00eSchristos 		     some other symbol, e.g., a class typedef defined.  */
1987f2ac410Schristos 		  return;
1997f2ac410Schristos 		}
2007f2ac410Schristos 
2017f2ac410Schristos 	      instance->enter_scope (std::move (scope));
2027f2ac410Schristos 	    }
2037f2ac410Schristos 
2047f2ac410Schristos 	  /* Get the `raw' name of the symbol.  */
2057d62b00eSchristos 	  if (name.empty () && sym.symbol->natural_name () != nullptr)
2067f2ac410Schristos 	    name = compile_cplus_instance::decl_name
2077d62b00eSchristos 	      (sym.symbol->natural_name ()).get ();
2087f2ac410Schristos 
2097f2ac410Schristos 	  /* Define the decl.  */
2107f2ac410Schristos 	  instance->plugin ().build_decl
211*6881a400Schristos 	    ("variable", name.c_str (), kind.raw (), sym_type,
2127f2ac410Schristos 	     symbol_name.get (), addr, filename, line);
2137f2ac410Schristos 
2147f2ac410Schristos 	  /* Pop scope for non-local symbols.  */
2157f2ac410Schristos 	  if (!is_local)
2167f2ac410Schristos 	    instance->leave_scope ();
2177f2ac410Schristos 	}
2187f2ac410Schristos     }
2197f2ac410Schristos }
2207f2ac410Schristos 
2217f2ac410Schristos /* Convert a full symbol to its gcc form.  CONTEXT is the compiler to
2227f2ac410Schristos    use, IDENTIFIER is the name of the symbol, SYM is the symbol
2237f2ac410Schristos    itself, and DOMAIN is the domain which was searched.  */
2247f2ac410Schristos 
2257f2ac410Schristos static void
2267f2ac410Schristos convert_symbol_sym (compile_cplus_instance *instance,
2277f2ac410Schristos 		    const char *identifier, struct block_symbol sym,
2287f2ac410Schristos 		    domain_enum domain)
2297f2ac410Schristos {
2307f2ac410Schristos   /* If we found a symbol and it is not in the  static or global
2317f2ac410Schristos      scope, then we should first convert any static or global scope
2327f2ac410Schristos      symbol of the same name.  This lets this unusual case work:
2337f2ac410Schristos 
2347f2ac410Schristos      int x; // Global.
2357f2ac410Schristos      int func(void)
2367f2ac410Schristos      {
2377f2ac410Schristos      int x;
2387f2ac410Schristos      // At this spot, evaluate "extern int x; x"
2397f2ac410Schristos      }
2407f2ac410Schristos   */
2417f2ac410Schristos 
2427f2ac410Schristos   const struct block *static_block = block_static_block (sym.block);
2437f2ac410Schristos   /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block.  */
2447f2ac410Schristos   bool is_local_symbol = (sym.block != static_block && static_block != nullptr);
2457f2ac410Schristos   if (is_local_symbol)
2467f2ac410Schristos     {
2477f2ac410Schristos       struct block_symbol global_sym;
2487f2ac410Schristos 
2497f2ac410Schristos       global_sym = lookup_symbol (identifier, nullptr, domain, nullptr);
2507f2ac410Schristos       /* If the outer symbol is in the static block, we ignore it, as
2517f2ac410Schristos 	 it cannot be referenced.  */
2527f2ac410Schristos       if (global_sym.symbol != nullptr
2537f2ac410Schristos 	  && global_sym.block != block_static_block (global_sym.block))
2547f2ac410Schristos 	{
2557f2ac410Schristos 	  if (compile_debug)
256*6881a400Schristos 	    gdb_printf (gdb_stdlog,
2577f2ac410Schristos 			"gcc_convert_symbol \"%s\": global symbol\n",
2587f2ac410Schristos 			identifier);
2597f2ac410Schristos 	  convert_one_symbol (instance, global_sym, true, false);
2607f2ac410Schristos 	}
2617f2ac410Schristos     }
2627f2ac410Schristos 
2637f2ac410Schristos   if (compile_debug)
264*6881a400Schristos     gdb_printf (gdb_stdlog,
2657f2ac410Schristos 		"gcc_convert_symbol \"%s\": local symbol\n",
2667f2ac410Schristos 		identifier);
2677f2ac410Schristos   convert_one_symbol (instance, sym, false, is_local_symbol);
2687f2ac410Schristos }
2697f2ac410Schristos 
2707f2ac410Schristos /* Convert a minimal symbol to its gcc form.  CONTEXT is the compiler
2717f2ac410Schristos    to use and BMSYM is the minimal symbol to convert.  */
2727f2ac410Schristos 
2737f2ac410Schristos static void
2747f2ac410Schristos convert_symbol_bmsym (compile_cplus_instance *instance,
2757f2ac410Schristos 		      struct bound_minimal_symbol bmsym)
2767f2ac410Schristos {
2777f2ac410Schristos   struct minimal_symbol *msym = bmsym.minsym;
2787f2ac410Schristos   struct objfile *objfile = bmsym.objfile;
2797f2ac410Schristos   struct type *type;
2807f2ac410Schristos   gcc_cp_symbol_kind_flags kind;
2817f2ac410Schristos   gcc_type sym_type;
2827f2ac410Schristos   CORE_ADDR addr;
2837f2ac410Schristos 
284*6881a400Schristos   addr = msym->value_address (objfile);
2857f2ac410Schristos 
2867f2ac410Schristos   /* Conversion copied from write_exp_msymbol.  */
287*6881a400Schristos   switch (msym->type ())
2887f2ac410Schristos     {
2897f2ac410Schristos     case mst_text:
2907f2ac410Schristos     case mst_file_text:
2917f2ac410Schristos     case mst_solib_trampoline:
2927f2ac410Schristos       type = objfile_type (objfile)->nodebug_text_symbol;
2937f2ac410Schristos       kind = GCC_CP_SYMBOL_FUNCTION;
2947f2ac410Schristos       break;
2957f2ac410Schristos 
2967f2ac410Schristos     case mst_text_gnu_ifunc:
2977f2ac410Schristos       /* nodebug_text_gnu_ifunc_symbol would cause:
2987f2ac410Schristos 	 function return type cannot be function  */
2997f2ac410Schristos       type = objfile_type (objfile)->nodebug_text_symbol;
3007f2ac410Schristos       kind = GCC_CP_SYMBOL_FUNCTION;
3017f2ac410Schristos       addr = gnu_ifunc_resolve_addr (target_gdbarch (), addr);
3027f2ac410Schristos       break;
3037f2ac410Schristos 
3047f2ac410Schristos     case mst_data:
3057f2ac410Schristos     case mst_file_data:
3067f2ac410Schristos     case mst_bss:
3077f2ac410Schristos     case mst_file_bss:
3087f2ac410Schristos       type = objfile_type (objfile)->nodebug_data_symbol;
3097f2ac410Schristos       kind = GCC_CP_SYMBOL_VARIABLE;
3107f2ac410Schristos       break;
3117f2ac410Schristos 
3127f2ac410Schristos     case mst_slot_got_plt:
3137f2ac410Schristos       type = objfile_type (objfile)->nodebug_got_plt_symbol;
3147f2ac410Schristos       kind = GCC_CP_SYMBOL_FUNCTION;
3157f2ac410Schristos       break;
3167f2ac410Schristos 
3177f2ac410Schristos     default:
3187f2ac410Schristos       type = objfile_type (objfile)->nodebug_unknown_symbol;
3197f2ac410Schristos       kind = GCC_CP_SYMBOL_VARIABLE;
3207f2ac410Schristos       break;
3217f2ac410Schristos     }
3227f2ac410Schristos 
3237f2ac410Schristos   sym_type = instance->convert_type (type);
3247f2ac410Schristos   instance->plugin ().push_namespace ("");
3257f2ac410Schristos   instance->plugin ().build_decl
326*6881a400Schristos     ("minsym", msym->natural_name (), kind.raw (), sym_type, nullptr, addr,
3277f2ac410Schristos      nullptr, 0);
3287f2ac410Schristos   instance->plugin ().pop_binding_level ("");
3297f2ac410Schristos }
3307f2ac410Schristos 
3317f2ac410Schristos /* See compile-cplus.h.  */
3327f2ac410Schristos 
3337f2ac410Schristos void
3347f2ac410Schristos gcc_cplus_convert_symbol (void *datum,
3357f2ac410Schristos 			  struct gcc_cp_context *gcc_context,
3367f2ac410Schristos 			  enum gcc_cp_oracle_request request,
3377f2ac410Schristos 			  const char *identifier)
3387f2ac410Schristos {
3397f2ac410Schristos   if (compile_debug)
340*6881a400Schristos     gdb_printf (gdb_stdlog,
3417f2ac410Schristos 		"got oracle request for \"%s\"\n", identifier);
3427f2ac410Schristos 
3437f2ac410Schristos   bool found = false;
3447f2ac410Schristos   compile_cplus_instance *instance = (compile_cplus_instance *) datum;
3457f2ac410Schristos 
3467d62b00eSchristos   try
3477f2ac410Schristos     {
3487f2ac410Schristos       /* Symbol searching is a three part process unfortunately.  */
3497f2ac410Schristos 
3507f2ac410Schristos       /* First do a "standard" lookup, converting any found symbols.
3517f2ac410Schristos 	 This will find variables in the current scope.  */
3527f2ac410Schristos 
3537f2ac410Schristos       struct block_symbol sym
3547f2ac410Schristos 	= lookup_symbol (identifier, instance->block (), VAR_DOMAIN, nullptr);
3557f2ac410Schristos 
3567f2ac410Schristos       if (sym.symbol != nullptr)
3577f2ac410Schristos 	{
3587f2ac410Schristos 	  found = true;
3597f2ac410Schristos 	  convert_symbol_sym (instance, identifier, sym, VAR_DOMAIN);
3607f2ac410Schristos 	}
3617f2ac410Schristos 
3627f2ac410Schristos       /* Then use linespec.c's multi-symbol search.  This should find
3637f2ac410Schristos 	 all non-variable symbols for which we have debug info.  */
3647f2ac410Schristos 
3657f2ac410Schristos       symbol_searcher searcher;
3667f2ac410Schristos       searcher.find_all_symbols (identifier, current_language,
3677f2ac410Schristos 				 ALL_DOMAIN, nullptr, nullptr);
3687f2ac410Schristos 
3697f2ac410Schristos       /* Convert any found symbols.  */
3707f2ac410Schristos       for (const auto &it : searcher.matching_symbols ())
3717f2ac410Schristos 	{
3727f2ac410Schristos 	  /* Don't convert the symbol found above, if any, twice!  */
3737f2ac410Schristos 	  if (it.symbol != sym.symbol)
3747f2ac410Schristos 	    {
3757f2ac410Schristos 	      found = true;
3767f2ac410Schristos 	      convert_symbol_sym (instance, identifier, it,
377*6881a400Schristos 				  it.symbol->domain ());
3787f2ac410Schristos 	    }
3797f2ac410Schristos 	}
3807f2ac410Schristos 
3817f2ac410Schristos       /* Finally, if no symbols have been found, fall back to minsyms.  */
3827f2ac410Schristos       if (!found)
3837f2ac410Schristos 	{
3847f2ac410Schristos 	  for (const auto &it : searcher.matching_msymbols ())
3857f2ac410Schristos 	    {
3867f2ac410Schristos 	      found = true;
3877f2ac410Schristos 	      convert_symbol_bmsym (instance, it);
3887f2ac410Schristos 	    }
3897f2ac410Schristos 	}
3907f2ac410Schristos     }
3917d62b00eSchristos   catch (const gdb_exception &e)
3927f2ac410Schristos     {
3937f2ac410Schristos       /* We can't allow exceptions to escape out of this callback.  Safest
3947f2ac410Schristos 	 is to simply emit a gcc error.  */
3957d62b00eSchristos       instance->plugin ().error (e.what ());
3967f2ac410Schristos     }
3977f2ac410Schristos 
3987f2ac410Schristos   if (compile_debug && !found)
399*6881a400Schristos     gdb_printf (gdb_stdlog,
4007f2ac410Schristos 		"gcc_convert_symbol \"%s\": lookup_symbol failed\n",
4017f2ac410Schristos 		identifier);
4027f2ac410Schristos 
4037f2ac410Schristos   if (compile_debug)
4047f2ac410Schristos     {
4057f2ac410Schristos       if (found)
406*6881a400Schristos 	gdb_printf (gdb_stdlog, "found type for %s\n", identifier);
4077f2ac410Schristos       else
4087f2ac410Schristos 	{
409*6881a400Schristos 	  gdb_printf (gdb_stdlog, "did not find type for %s\n",
4107f2ac410Schristos 		      identifier);
4117f2ac410Schristos 	}
4127f2ac410Schristos     }
4137f2ac410Schristos 
4147f2ac410Schristos   return;
4157f2ac410Schristos }
4167f2ac410Schristos 
4177f2ac410Schristos /* See compile-cplus.h.  */
4187f2ac410Schristos 
4197f2ac410Schristos gcc_address
4207f2ac410Schristos gcc_cplus_symbol_address (void *datum, struct gcc_cp_context *gcc_context,
4217f2ac410Schristos 			  const char *identifier)
4227f2ac410Schristos {
4237f2ac410Schristos   compile_cplus_instance *instance = (compile_cplus_instance *) datum;
4247f2ac410Schristos   gcc_address result = 0;
4257f2ac410Schristos   int found = 0;
4267f2ac410Schristos 
4277f2ac410Schristos   if (compile_debug)
428*6881a400Schristos     gdb_printf (gdb_stdlog,
4297f2ac410Schristos 		"got oracle request for address of %s\n", identifier);
4307f2ac410Schristos 
4317f2ac410Schristos   /* We can't allow exceptions to escape out of this callback.  Safest
4327f2ac410Schristos      is to simply emit a gcc error.  */
4337d62b00eSchristos   try
4347f2ac410Schristos     {
4357f2ac410Schristos       struct symbol *sym
4367f2ac410Schristos 	= lookup_symbol (identifier, nullptr, VAR_DOMAIN, nullptr).symbol;
4377f2ac410Schristos 
438*6881a400Schristos       if (sym != nullptr && sym->aclass () == LOC_BLOCK)
4397f2ac410Schristos 	{
4407f2ac410Schristos 	  if (compile_debug)
441*6881a400Schristos 	    gdb_printf (gdb_stdlog,
4427f2ac410Schristos 			"gcc_symbol_address \"%s\": full symbol\n",
4437f2ac410Schristos 			identifier);
444*6881a400Schristos 	  result = sym->value_block ()->start ();
445*6881a400Schristos 	  if (sym->type ()->is_gnu_ifunc ())
4467f2ac410Schristos 	    result = gnu_ifunc_resolve_addr (target_gdbarch (), result);
4477f2ac410Schristos 	  found = 1;
4487f2ac410Schristos 	}
4497f2ac410Schristos       else
4507f2ac410Schristos 	{
4517f2ac410Schristos 	  struct bound_minimal_symbol msym;
4527f2ac410Schristos 
4537f2ac410Schristos 	  msym = lookup_bound_minimal_symbol (identifier);
4547f2ac410Schristos 	  if (msym.minsym != nullptr)
4557f2ac410Schristos 	    {
4567f2ac410Schristos 	      if (compile_debug)
457*6881a400Schristos 		gdb_printf (gdb_stdlog,
4587f2ac410Schristos 			    "gcc_symbol_address \"%s\": minimal "
4597f2ac410Schristos 			    "symbol\n",
4607f2ac410Schristos 			    identifier);
461*6881a400Schristos 	      result = msym.value_address ();
462*6881a400Schristos 	      if (msym.minsym->type () == mst_text_gnu_ifunc)
4637f2ac410Schristos 		result = gnu_ifunc_resolve_addr (target_gdbarch (), result);
4647f2ac410Schristos 	      found = 1;
4657f2ac410Schristos 	    }
4667f2ac410Schristos 	}
4677f2ac410Schristos     }
4687f2ac410Schristos 
4697d62b00eSchristos   catch (const gdb_exception_error &e)
4707f2ac410Schristos     {
4717d62b00eSchristos       instance->plugin ().error (e.what ());
4727f2ac410Schristos     }
4737f2ac410Schristos 
4747f2ac410Schristos   if (compile_debug && !found)
475*6881a400Schristos     gdb_printf (gdb_stdlog,
4767f2ac410Schristos 		"gcc_symbol_address \"%s\": failed\n",
4777f2ac410Schristos 		identifier);
4787f2ac410Schristos 
4797f2ac410Schristos   if (compile_debug)
4807f2ac410Schristos     {
4817f2ac410Schristos       if (found)
482*6881a400Schristos 	gdb_printf (gdb_stdlog, "found address for %s!\n", identifier);
4837f2ac410Schristos       else
484*6881a400Schristos 	gdb_printf (gdb_stdlog,
4857f2ac410Schristos 		    "did not find address for %s\n", identifier);
4867f2ac410Schristos     }
4877f2ac410Schristos 
4887f2ac410Schristos   return result;
4897f2ac410Schristos }
490