xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/dwarf2/loc.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* DWARF 2 location expression support for GDB.
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 2003-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
67d62b00eSchristos 
77d62b00eSchristos    This file is part of GDB.
87d62b00eSchristos 
97d62b00eSchristos    This program is free software; you can redistribute it and/or modify
107d62b00eSchristos    it under the terms of the GNU General Public License as published by
117d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
127d62b00eSchristos    (at your option) any later version.
137d62b00eSchristos 
147d62b00eSchristos    This program is distributed in the hope that it will be useful,
157d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
167d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
177d62b00eSchristos    GNU General Public License for more details.
187d62b00eSchristos 
197d62b00eSchristos    You should have received a copy of the GNU General Public License
207d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
217d62b00eSchristos 
227d62b00eSchristos #include "defs.h"
237d62b00eSchristos #include "ui-out.h"
247d62b00eSchristos #include "value.h"
257d62b00eSchristos #include "frame.h"
267d62b00eSchristos #include "gdbcore.h"
277d62b00eSchristos #include "target.h"
287d62b00eSchristos #include "inferior.h"
297d62b00eSchristos #include "ax.h"
307d62b00eSchristos #include "ax-gdb.h"
317d62b00eSchristos #include "regcache.h"
327d62b00eSchristos #include "objfiles.h"
337d62b00eSchristos #include "block.h"
347d62b00eSchristos #include "gdbcmd.h"
357d62b00eSchristos #include "complaints.h"
367d62b00eSchristos #include "dwarf2.h"
377d62b00eSchristos #include "dwarf2/expr.h"
387d62b00eSchristos #include "dwarf2/loc.h"
397d62b00eSchristos #include "dwarf2/read.h"
407d62b00eSchristos #include "dwarf2/frame.h"
417d62b00eSchristos #include "dwarf2/leb.h"
427d62b00eSchristos #include "compile/compile.h"
437d62b00eSchristos #include "gdbsupport/selftest.h"
447d62b00eSchristos #include <algorithm>
457d62b00eSchristos #include <vector>
467d62b00eSchristos #include <unordered_set>
477d62b00eSchristos #include "gdbsupport/underlying.h"
487d62b00eSchristos #include "gdbsupport/byte-vector.h"
497d62b00eSchristos 
507d62b00eSchristos static struct value *dwarf2_evaluate_loc_desc_full
51*6881a400Schristos   (struct type *type, frame_info_ptr frame, const gdb_byte *data,
527d62b00eSchristos    size_t size, dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
53*6881a400Schristos    struct type *subobj_type, LONGEST subobj_byte_offset, bool as_lval = true);
547d62b00eSchristos 
557d62b00eSchristos /* Until these have formal names, we define these here.
567d62b00eSchristos    ref: http://gcc.gnu.org/wiki/DebugFission
577d62b00eSchristos    Each entry in .debug_loc.dwo begins with a byte that describes the entry,
587d62b00eSchristos    and is then followed by data specific to that entry.  */
597d62b00eSchristos 
607d62b00eSchristos enum debug_loc_kind
617d62b00eSchristos {
627d62b00eSchristos   /* Indicates the end of the list of entries.  */
637d62b00eSchristos   DEBUG_LOC_END_OF_LIST = 0,
647d62b00eSchristos 
657d62b00eSchristos   /* This is followed by an unsigned LEB128 number that is an index into
667d62b00eSchristos      .debug_addr and specifies the base address for all following entries.  */
677d62b00eSchristos   DEBUG_LOC_BASE_ADDRESS = 1,
687d62b00eSchristos 
697d62b00eSchristos   /* This is followed by two unsigned LEB128 numbers that are indices into
707d62b00eSchristos      .debug_addr and specify the beginning and ending addresses, and then
717d62b00eSchristos      a normal location expression as in .debug_loc.  */
727d62b00eSchristos   DEBUG_LOC_START_END = 2,
737d62b00eSchristos 
747d62b00eSchristos   /* This is followed by an unsigned LEB128 number that is an index into
757d62b00eSchristos      .debug_addr and specifies the beginning address, and a 4 byte unsigned
767d62b00eSchristos      number that specifies the length, and then a normal location expression
777d62b00eSchristos      as in .debug_loc.  */
787d62b00eSchristos   DEBUG_LOC_START_LENGTH = 3,
797d62b00eSchristos 
807d62b00eSchristos   /* This is followed by two unsigned LEB128 operands. The values of these
817d62b00eSchristos      operands are the starting and ending offsets, respectively, relative to
827d62b00eSchristos      the applicable base address.  */
837d62b00eSchristos   DEBUG_LOC_OFFSET_PAIR = 4,
847d62b00eSchristos 
857d62b00eSchristos   /* An internal value indicating there is insufficient data.  */
867d62b00eSchristos   DEBUG_LOC_BUFFER_OVERFLOW = -1,
877d62b00eSchristos 
887d62b00eSchristos   /* An internal value indicating an invalid kind of entry was found.  */
897d62b00eSchristos   DEBUG_LOC_INVALID_ENTRY = -2
907d62b00eSchristos };
917d62b00eSchristos 
927d62b00eSchristos /* Helper function which throws an error if a synthetic pointer is
937d62b00eSchristos    invalid.  */
947d62b00eSchristos 
95*6881a400Schristos void
967d62b00eSchristos invalid_synthetic_pointer (void)
977d62b00eSchristos {
987d62b00eSchristos   error (_("access outside bounds of object "
997d62b00eSchristos 	   "referenced via synthetic pointer"));
1007d62b00eSchristos }
1017d62b00eSchristos 
1027d62b00eSchristos /* Decode the addresses in a non-dwo .debug_loc entry.
1037d62b00eSchristos    A pointer to the next byte to examine is returned in *NEW_PTR.
1047d62b00eSchristos    The encoded low,high addresses are return in *LOW,*HIGH.
1057d62b00eSchristos    The result indicates the kind of entry found.  */
1067d62b00eSchristos 
1077d62b00eSchristos static enum debug_loc_kind
1087d62b00eSchristos decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
1097d62b00eSchristos 			    const gdb_byte **new_ptr,
1107d62b00eSchristos 			    CORE_ADDR *low, CORE_ADDR *high,
1117d62b00eSchristos 			    enum bfd_endian byte_order,
1127d62b00eSchristos 			    unsigned int addr_size,
1137d62b00eSchristos 			    int signed_addr_p)
1147d62b00eSchristos {
1157d62b00eSchristos   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
1167d62b00eSchristos 
1177d62b00eSchristos   if (buf_end - loc_ptr < 2 * addr_size)
1187d62b00eSchristos     return DEBUG_LOC_BUFFER_OVERFLOW;
1197d62b00eSchristos 
1207d62b00eSchristos   if (signed_addr_p)
1217d62b00eSchristos     *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
1227d62b00eSchristos   else
1237d62b00eSchristos     *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1247d62b00eSchristos   loc_ptr += addr_size;
1257d62b00eSchristos 
1267d62b00eSchristos   if (signed_addr_p)
1277d62b00eSchristos     *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
1287d62b00eSchristos   else
1297d62b00eSchristos     *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1307d62b00eSchristos   loc_ptr += addr_size;
1317d62b00eSchristos 
1327d62b00eSchristos   *new_ptr = loc_ptr;
1337d62b00eSchristos 
1347d62b00eSchristos   /* A base-address-selection entry.  */
1357d62b00eSchristos   if ((*low & base_mask) == base_mask)
1367d62b00eSchristos     return DEBUG_LOC_BASE_ADDRESS;
1377d62b00eSchristos 
1387d62b00eSchristos   /* An end-of-list entry.  */
1397d62b00eSchristos   if (*low == 0 && *high == 0)
1407d62b00eSchristos     return DEBUG_LOC_END_OF_LIST;
1417d62b00eSchristos 
142*6881a400Schristos   /* We want the caller to apply the base address, so we must return
143*6881a400Schristos      DEBUG_LOC_OFFSET_PAIR here.  */
144*6881a400Schristos   return DEBUG_LOC_OFFSET_PAIR;
1457d62b00eSchristos }
1467d62b00eSchristos 
1477d62b00eSchristos /* Decode the addresses in .debug_loclists entry.
1487d62b00eSchristos    A pointer to the next byte to examine is returned in *NEW_PTR.
1497d62b00eSchristos    The encoded low,high addresses are return in *LOW,*HIGH.
1507d62b00eSchristos    The result indicates the kind of entry found.  */
1517d62b00eSchristos 
1527d62b00eSchristos static enum debug_loc_kind
1537d62b00eSchristos decode_debug_loclists_addresses (dwarf2_per_cu_data *per_cu,
1547d62b00eSchristos 				 dwarf2_per_objfile *per_objfile,
1557d62b00eSchristos 				 const gdb_byte *loc_ptr,
1567d62b00eSchristos 				 const gdb_byte *buf_end,
1577d62b00eSchristos 				 const gdb_byte **new_ptr,
1587d62b00eSchristos 				 CORE_ADDR *low, CORE_ADDR *high,
1597d62b00eSchristos 				 enum bfd_endian byte_order,
1607d62b00eSchristos 				 unsigned int addr_size,
1617d62b00eSchristos 				 int signed_addr_p)
1627d62b00eSchristos {
1637d62b00eSchristos   uint64_t u64;
1647d62b00eSchristos 
1657d62b00eSchristos   if (loc_ptr == buf_end)
1667d62b00eSchristos     return DEBUG_LOC_BUFFER_OVERFLOW;
1677d62b00eSchristos 
1687d62b00eSchristos   switch (*loc_ptr++)
1697d62b00eSchristos     {
1707d62b00eSchristos     case DW_LLE_base_addressx:
1717d62b00eSchristos       *low = 0;
1727d62b00eSchristos       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
1737d62b00eSchristos       if (loc_ptr == NULL)
1747d62b00eSchristos 	 return DEBUG_LOC_BUFFER_OVERFLOW;
1757d62b00eSchristos 
1767d62b00eSchristos       *high = dwarf2_read_addr_index (per_cu, per_objfile, u64);
1777d62b00eSchristos       *new_ptr = loc_ptr;
1787d62b00eSchristos       return DEBUG_LOC_BASE_ADDRESS;
1797d62b00eSchristos 
1807d62b00eSchristos     case DW_LLE_startx_length:
1817d62b00eSchristos       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
1827d62b00eSchristos       if (loc_ptr == NULL)
1837d62b00eSchristos 	 return DEBUG_LOC_BUFFER_OVERFLOW;
1847d62b00eSchristos 
1857d62b00eSchristos       *low = dwarf2_read_addr_index (per_cu, per_objfile, u64);
1867d62b00eSchristos       *high = *low;
1877d62b00eSchristos       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
1887d62b00eSchristos       if (loc_ptr == NULL)
1897d62b00eSchristos 	 return DEBUG_LOC_BUFFER_OVERFLOW;
1907d62b00eSchristos 
1917d62b00eSchristos       *high += u64;
1927d62b00eSchristos       *new_ptr = loc_ptr;
1937d62b00eSchristos       return DEBUG_LOC_START_LENGTH;
1947d62b00eSchristos 
1957d62b00eSchristos     case DW_LLE_start_length:
1967d62b00eSchristos       if (buf_end - loc_ptr < addr_size)
1977d62b00eSchristos 	 return DEBUG_LOC_BUFFER_OVERFLOW;
1987d62b00eSchristos 
1997d62b00eSchristos       if (signed_addr_p)
2007d62b00eSchristos 	 *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
2017d62b00eSchristos       else
2027d62b00eSchristos 	 *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2037d62b00eSchristos 
2047d62b00eSchristos       loc_ptr += addr_size;
2057d62b00eSchristos       *high = *low;
2067d62b00eSchristos 
2077d62b00eSchristos       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
2087d62b00eSchristos       if (loc_ptr == NULL)
2097d62b00eSchristos 	 return DEBUG_LOC_BUFFER_OVERFLOW;
2107d62b00eSchristos 
2117d62b00eSchristos       *high += u64;
2127d62b00eSchristos       *new_ptr = loc_ptr;
2137d62b00eSchristos       return DEBUG_LOC_START_LENGTH;
2147d62b00eSchristos 
2157d62b00eSchristos     case DW_LLE_end_of_list:
2167d62b00eSchristos       *new_ptr = loc_ptr;
2177d62b00eSchristos       return DEBUG_LOC_END_OF_LIST;
2187d62b00eSchristos 
2197d62b00eSchristos     case DW_LLE_base_address:
2207d62b00eSchristos       if (loc_ptr + addr_size > buf_end)
2217d62b00eSchristos 	return DEBUG_LOC_BUFFER_OVERFLOW;
2227d62b00eSchristos 
2237d62b00eSchristos       if (signed_addr_p)
2247d62b00eSchristos 	*high = extract_signed_integer (loc_ptr, addr_size, byte_order);
2257d62b00eSchristos       else
2267d62b00eSchristos 	*high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2277d62b00eSchristos 
2287d62b00eSchristos       loc_ptr += addr_size;
2297d62b00eSchristos       *new_ptr = loc_ptr;
2307d62b00eSchristos       return DEBUG_LOC_BASE_ADDRESS;
2317d62b00eSchristos 
2327d62b00eSchristos     case DW_LLE_offset_pair:
2337d62b00eSchristos       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
2347d62b00eSchristos       if (loc_ptr == NULL)
2357d62b00eSchristos 	return DEBUG_LOC_BUFFER_OVERFLOW;
2367d62b00eSchristos 
2377d62b00eSchristos       *low = u64;
2387d62b00eSchristos       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
2397d62b00eSchristos       if (loc_ptr == NULL)
2407d62b00eSchristos 	return DEBUG_LOC_BUFFER_OVERFLOW;
2417d62b00eSchristos 
2427d62b00eSchristos       *high = u64;
2437d62b00eSchristos       *new_ptr = loc_ptr;
2447d62b00eSchristos       return DEBUG_LOC_OFFSET_PAIR;
2457d62b00eSchristos 
246*6881a400Schristos     case DW_LLE_start_end:
247*6881a400Schristos       if (loc_ptr + 2 * addr_size > buf_end)
248*6881a400Schristos 	return DEBUG_LOC_BUFFER_OVERFLOW;
249*6881a400Schristos 
250*6881a400Schristos       if (signed_addr_p)
251*6881a400Schristos 	*low = extract_signed_integer (loc_ptr, addr_size, byte_order);
252*6881a400Schristos       else
253*6881a400Schristos 	*low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
254*6881a400Schristos 
255*6881a400Schristos       loc_ptr += addr_size;
256*6881a400Schristos       if (signed_addr_p)
257*6881a400Schristos 	*high = extract_signed_integer (loc_ptr, addr_size, byte_order);
258*6881a400Schristos       else
259*6881a400Schristos 	*high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
260*6881a400Schristos 
261*6881a400Schristos       loc_ptr += addr_size;
262*6881a400Schristos       *new_ptr = loc_ptr;
263*6881a400Schristos       return DEBUG_LOC_START_END;
264*6881a400Schristos 
2657d62b00eSchristos     /* Following cases are not supported yet.  */
2667d62b00eSchristos     case DW_LLE_startx_endx:
2677d62b00eSchristos     case DW_LLE_default_location:
2687d62b00eSchristos     default:
2697d62b00eSchristos       return DEBUG_LOC_INVALID_ENTRY;
2707d62b00eSchristos     }
2717d62b00eSchristos }
2727d62b00eSchristos 
2737d62b00eSchristos /* Decode the addresses in .debug_loc.dwo entry.
2747d62b00eSchristos    A pointer to the next byte to examine is returned in *NEW_PTR.
2757d62b00eSchristos    The encoded low,high addresses are return in *LOW,*HIGH.
2767d62b00eSchristos    The result indicates the kind of entry found.  */
2777d62b00eSchristos 
2787d62b00eSchristos static enum debug_loc_kind
2797d62b00eSchristos decode_debug_loc_dwo_addresses (dwarf2_per_cu_data *per_cu,
2807d62b00eSchristos 				dwarf2_per_objfile *per_objfile,
2817d62b00eSchristos 				const gdb_byte *loc_ptr,
2827d62b00eSchristos 				const gdb_byte *buf_end,
2837d62b00eSchristos 				const gdb_byte **new_ptr,
2847d62b00eSchristos 				CORE_ADDR *low, CORE_ADDR *high,
2857d62b00eSchristos 				enum bfd_endian byte_order)
2867d62b00eSchristos {
2877d62b00eSchristos   uint64_t low_index, high_index;
2887d62b00eSchristos 
2897d62b00eSchristos   if (loc_ptr == buf_end)
2907d62b00eSchristos     return DEBUG_LOC_BUFFER_OVERFLOW;
2917d62b00eSchristos 
2927d62b00eSchristos   switch (*loc_ptr++)
2937d62b00eSchristos     {
2947d62b00eSchristos     case DW_LLE_GNU_end_of_list_entry:
2957d62b00eSchristos       *new_ptr = loc_ptr;
2967d62b00eSchristos       return DEBUG_LOC_END_OF_LIST;
2977d62b00eSchristos 
2987d62b00eSchristos     case DW_LLE_GNU_base_address_selection_entry:
2997d62b00eSchristos       *low = 0;
3007d62b00eSchristos       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
3017d62b00eSchristos       if (loc_ptr == NULL)
3027d62b00eSchristos 	return DEBUG_LOC_BUFFER_OVERFLOW;
3037d62b00eSchristos 
3047d62b00eSchristos       *high = dwarf2_read_addr_index (per_cu, per_objfile, high_index);
3057d62b00eSchristos       *new_ptr = loc_ptr;
3067d62b00eSchristos       return DEBUG_LOC_BASE_ADDRESS;
3077d62b00eSchristos 
3087d62b00eSchristos     case DW_LLE_GNU_start_end_entry:
3097d62b00eSchristos       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
3107d62b00eSchristos       if (loc_ptr == NULL)
3117d62b00eSchristos 	return DEBUG_LOC_BUFFER_OVERFLOW;
3127d62b00eSchristos 
3137d62b00eSchristos       *low = dwarf2_read_addr_index (per_cu, per_objfile, low_index);
3147d62b00eSchristos       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
3157d62b00eSchristos       if (loc_ptr == NULL)
3167d62b00eSchristos 	return DEBUG_LOC_BUFFER_OVERFLOW;
3177d62b00eSchristos 
3187d62b00eSchristos       *high = dwarf2_read_addr_index (per_cu, per_objfile, high_index);
3197d62b00eSchristos       *new_ptr = loc_ptr;
3207d62b00eSchristos       return DEBUG_LOC_START_END;
3217d62b00eSchristos 
3227d62b00eSchristos     case DW_LLE_GNU_start_length_entry:
3237d62b00eSchristos       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
3247d62b00eSchristos       if (loc_ptr == NULL)
3257d62b00eSchristos 	return DEBUG_LOC_BUFFER_OVERFLOW;
3267d62b00eSchristos 
3277d62b00eSchristos       *low = dwarf2_read_addr_index (per_cu, per_objfile, low_index);
3287d62b00eSchristos       if (loc_ptr + 4 > buf_end)
3297d62b00eSchristos 	return DEBUG_LOC_BUFFER_OVERFLOW;
3307d62b00eSchristos 
3317d62b00eSchristos       *high = *low;
3327d62b00eSchristos       *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
3337d62b00eSchristos       *new_ptr = loc_ptr + 4;
3347d62b00eSchristos       return DEBUG_LOC_START_LENGTH;
3357d62b00eSchristos 
3367d62b00eSchristos     default:
3377d62b00eSchristos       return DEBUG_LOC_INVALID_ENTRY;
3387d62b00eSchristos     }
3397d62b00eSchristos }
3407d62b00eSchristos 
3417d62b00eSchristos /* A function for dealing with location lists.  Given a
3427d62b00eSchristos    symbol baton (BATON) and a pc value (PC), find the appropriate
3437d62b00eSchristos    location expression, set *LOCEXPR_LENGTH, and return a pointer
3447d62b00eSchristos    to the beginning of the expression.  Returns NULL on failure.
3457d62b00eSchristos 
3467d62b00eSchristos    For now, only return the first matching location expression; there
3477d62b00eSchristos    can be more than one in the list.  */
3487d62b00eSchristos 
3497d62b00eSchristos const gdb_byte *
3507d62b00eSchristos dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
3517d62b00eSchristos 				 size_t *locexpr_length, CORE_ADDR pc)
3527d62b00eSchristos {
3537d62b00eSchristos   dwarf2_per_objfile *per_objfile = baton->per_objfile;
3547d62b00eSchristos   struct objfile *objfile = per_objfile->objfile;
3557d62b00eSchristos   struct gdbarch *gdbarch = objfile->arch ();
3567d62b00eSchristos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3577d62b00eSchristos   unsigned int addr_size = baton->per_cu->addr_size ();
358*6881a400Schristos   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd.get ());
359*6881a400Schristos   /* Adjustment for relocatable objects.  */
360*6881a400Schristos   CORE_ADDR text_offset = baton->per_objfile->objfile->text_section_offset ();
361*6881a400Schristos   CORE_ADDR base_address = baton->base_address;
3627d62b00eSchristos   const gdb_byte *loc_ptr, *buf_end;
3637d62b00eSchristos 
3647d62b00eSchristos   loc_ptr = baton->data;
3657d62b00eSchristos   buf_end = baton->data + baton->size;
3667d62b00eSchristos 
3677d62b00eSchristos   while (1)
3687d62b00eSchristos     {
3697d62b00eSchristos       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
3707d62b00eSchristos       int length;
3717d62b00eSchristos       enum debug_loc_kind kind;
3727d62b00eSchristos       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
3737d62b00eSchristos 
3747d62b00eSchristos       if (baton->per_cu->version () < 5 && baton->from_dwo)
3757d62b00eSchristos 	kind = decode_debug_loc_dwo_addresses (baton->per_cu,
3767d62b00eSchristos 					       baton->per_objfile,
3777d62b00eSchristos 					       loc_ptr, buf_end, &new_ptr,
3787d62b00eSchristos 					       &low, &high, byte_order);
3797d62b00eSchristos       else if (baton->per_cu->version () < 5)
3807d62b00eSchristos 	kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
3817d62b00eSchristos 					   &low, &high,
3827d62b00eSchristos 					   byte_order, addr_size,
3837d62b00eSchristos 					   signed_addr_p);
3847d62b00eSchristos       else
3857d62b00eSchristos 	kind = decode_debug_loclists_addresses (baton->per_cu,
3867d62b00eSchristos 						baton->per_objfile,
3877d62b00eSchristos 						loc_ptr, buf_end, &new_ptr,
3887d62b00eSchristos 						&low, &high, byte_order,
3897d62b00eSchristos 						addr_size, signed_addr_p);
3907d62b00eSchristos 
3917d62b00eSchristos       loc_ptr = new_ptr;
3927d62b00eSchristos       switch (kind)
3937d62b00eSchristos 	{
3947d62b00eSchristos 	case DEBUG_LOC_END_OF_LIST:
3957d62b00eSchristos 	  *locexpr_length = 0;
3967d62b00eSchristos 	  return NULL;
3977d62b00eSchristos 
3987d62b00eSchristos 	case DEBUG_LOC_BASE_ADDRESS:
399*6881a400Schristos 	  base_address = high;
4007d62b00eSchristos 	  continue;
4017d62b00eSchristos 
4027d62b00eSchristos 	case DEBUG_LOC_START_END:
4037d62b00eSchristos 	case DEBUG_LOC_START_LENGTH:
4047d62b00eSchristos 	case DEBUG_LOC_OFFSET_PAIR:
4057d62b00eSchristos 	  break;
4067d62b00eSchristos 
4077d62b00eSchristos 	case DEBUG_LOC_BUFFER_OVERFLOW:
4087d62b00eSchristos 	case DEBUG_LOC_INVALID_ENTRY:
4097d62b00eSchristos 	  error (_("dwarf2_find_location_expression: "
4107d62b00eSchristos 		   "Corrupted DWARF expression."));
4117d62b00eSchristos 
4127d62b00eSchristos 	default:
4137d62b00eSchristos 	  gdb_assert_not_reached ("bad debug_loc_kind");
4147d62b00eSchristos 	}
4157d62b00eSchristos 
4167d62b00eSchristos       /* Otherwise, a location expression entry.
4177d62b00eSchristos 	 If the entry is from a DWO, don't add base address: the entry is from
4187d62b00eSchristos 	 .debug_addr which already has the DWARF "base address". We still add
419*6881a400Schristos 	 text offset in case we're debugging a PIE executable. However, if the
4207d62b00eSchristos 	 entry is DW_LLE_offset_pair from a DWO, add the base address as the
421*6881a400Schristos 	 operands are offsets relative to the applicable base address.
422*6881a400Schristos 	 If the entry is DW_LLE_start_end or DW_LLE_start_length, then
423*6881a400Schristos 	 it already is an address, and we don't need to add the base.  */
424*6881a400Schristos       low += text_offset;
425*6881a400Schristos       high += text_offset;
426*6881a400Schristos       if (!baton->from_dwo && kind == DEBUG_LOC_OFFSET_PAIR)
4277d62b00eSchristos 	{
4287d62b00eSchristos 	  low += base_address;
4297d62b00eSchristos 	  high += base_address;
4307d62b00eSchristos 	}
4317d62b00eSchristos 
4327d62b00eSchristos       if (baton->per_cu->version () < 5)
4337d62b00eSchristos 	{
4347d62b00eSchristos 	  length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4357d62b00eSchristos 	  loc_ptr += 2;
4367d62b00eSchristos 	}
4377d62b00eSchristos       else
4387d62b00eSchristos 	{
4397d62b00eSchristos 	  unsigned int bytes_read;
4407d62b00eSchristos 
4417d62b00eSchristos 	  length = read_unsigned_leb128 (NULL, loc_ptr, &bytes_read);
4427d62b00eSchristos 	  loc_ptr += bytes_read;
4437d62b00eSchristos 	}
4447d62b00eSchristos 
4457d62b00eSchristos       if (low == high && pc == low)
4467d62b00eSchristos 	{
4477d62b00eSchristos 	  /* This is entry PC record present only at entry point
4487d62b00eSchristos 	     of a function.  Verify it is really the function entry point.  */
4497d62b00eSchristos 
4507d62b00eSchristos 	  const struct block *pc_block = block_for_pc (pc);
4517d62b00eSchristos 	  struct symbol *pc_func = NULL;
4527d62b00eSchristos 
4537d62b00eSchristos 	  if (pc_block)
4547d62b00eSchristos 	    pc_func = block_linkage_function (pc_block);
4557d62b00eSchristos 
456*6881a400Schristos 	  if (pc_func && pc == pc_func->value_block ()->entry_pc ())
4577d62b00eSchristos 	    {
4587d62b00eSchristos 	      *locexpr_length = length;
4597d62b00eSchristos 	      return loc_ptr;
4607d62b00eSchristos 	    }
4617d62b00eSchristos 	}
4627d62b00eSchristos 
4637d62b00eSchristos       if (pc >= low && pc < high)
4647d62b00eSchristos 	{
4657d62b00eSchristos 	  *locexpr_length = length;
4667d62b00eSchristos 	  return loc_ptr;
4677d62b00eSchristos 	}
4687d62b00eSchristos 
4697d62b00eSchristos       loc_ptr += length;
4707d62b00eSchristos     }
4717d62b00eSchristos }
4727d62b00eSchristos 
4737d62b00eSchristos /* Implement find_frame_base_location method for LOC_BLOCK functions using
4747d62b00eSchristos    DWARF expression for its DW_AT_frame_base.  */
4757d62b00eSchristos 
4767d62b00eSchristos static void
4777d62b00eSchristos locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
4787d62b00eSchristos 				  const gdb_byte **start, size_t *length)
4797d62b00eSchristos {
4807d62b00eSchristos   struct dwarf2_locexpr_baton *symbaton
4817d62b00eSchristos     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
4827d62b00eSchristos 
4837d62b00eSchristos   *length = symbaton->size;
4847d62b00eSchristos   *start = symbaton->data;
4857d62b00eSchristos }
4867d62b00eSchristos 
4877d62b00eSchristos /* Implement the struct symbol_block_ops::get_frame_base method for
4887d62b00eSchristos    LOC_BLOCK functions using a DWARF expression as its DW_AT_frame_base.  */
4897d62b00eSchristos 
4907d62b00eSchristos static CORE_ADDR
491*6881a400Schristos locexpr_get_frame_base (struct symbol *framefunc, frame_info_ptr frame)
4927d62b00eSchristos {
4937d62b00eSchristos   struct gdbarch *gdbarch;
4947d62b00eSchristos   struct type *type;
4957d62b00eSchristos   struct dwarf2_locexpr_baton *dlbaton;
4967d62b00eSchristos   const gdb_byte *start;
4977d62b00eSchristos   size_t length;
4987d62b00eSchristos   struct value *result;
4997d62b00eSchristos 
5007d62b00eSchristos   /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
5017d62b00eSchristos      Thus, it's supposed to provide the find_frame_base_location method as
5027d62b00eSchristos      well.  */
5037d62b00eSchristos   gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
5047d62b00eSchristos 
5057d62b00eSchristos   gdbarch = get_frame_arch (frame);
5067d62b00eSchristos   type = builtin_type (gdbarch)->builtin_data_ptr;
5077d62b00eSchristos   dlbaton = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
5087d62b00eSchristos 
5097d62b00eSchristos   SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
5107d62b00eSchristos     (framefunc, get_frame_pc (frame), &start, &length);
5117d62b00eSchristos   result = dwarf2_evaluate_loc_desc (type, frame, start, length,
5127d62b00eSchristos 				     dlbaton->per_cu, dlbaton->per_objfile);
5137d62b00eSchristos 
5147d62b00eSchristos   /* The DW_AT_frame_base attribute contains a location description which
5157d62b00eSchristos      computes the base address itself.  However, the call to
5167d62b00eSchristos      dwarf2_evaluate_loc_desc returns a value representing a variable at
5177d62b00eSchristos      that address.  The frame base address is thus this variable's
5187d62b00eSchristos      address.  */
5197d62b00eSchristos   return value_address (result);
5207d62b00eSchristos }
5217d62b00eSchristos 
5227d62b00eSchristos /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
5237d62b00eSchristos    function uses DWARF expression for its DW_AT_frame_base.  */
5247d62b00eSchristos 
5257d62b00eSchristos const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
5267d62b00eSchristos {
5277d62b00eSchristos   locexpr_find_frame_base_location,
5287d62b00eSchristos   locexpr_get_frame_base
5297d62b00eSchristos };
5307d62b00eSchristos 
5317d62b00eSchristos /* Implement find_frame_base_location method for LOC_BLOCK functions using
5327d62b00eSchristos    DWARF location list for its DW_AT_frame_base.  */
5337d62b00eSchristos 
5347d62b00eSchristos static void
5357d62b00eSchristos loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
5367d62b00eSchristos 				  const gdb_byte **start, size_t *length)
5377d62b00eSchristos {
5387d62b00eSchristos   struct dwarf2_loclist_baton *symbaton
5397d62b00eSchristos     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
5407d62b00eSchristos 
5417d62b00eSchristos   *start = dwarf2_find_location_expression (symbaton, length, pc);
5427d62b00eSchristos }
5437d62b00eSchristos 
5447d62b00eSchristos /* Implement the struct symbol_block_ops::get_frame_base method for
5457d62b00eSchristos    LOC_BLOCK functions using a DWARF location list as its DW_AT_frame_base.  */
5467d62b00eSchristos 
5477d62b00eSchristos static CORE_ADDR
548*6881a400Schristos loclist_get_frame_base (struct symbol *framefunc, frame_info_ptr frame)
5497d62b00eSchristos {
5507d62b00eSchristos   struct gdbarch *gdbarch;
5517d62b00eSchristos   struct type *type;
5527d62b00eSchristos   struct dwarf2_loclist_baton *dlbaton;
5537d62b00eSchristos   const gdb_byte *start;
5547d62b00eSchristos   size_t length;
5557d62b00eSchristos   struct value *result;
5567d62b00eSchristos 
5577d62b00eSchristos   /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
5587d62b00eSchristos      Thus, it's supposed to provide the find_frame_base_location method as
5597d62b00eSchristos      well.  */
5607d62b00eSchristos   gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
5617d62b00eSchristos 
5627d62b00eSchristos   gdbarch = get_frame_arch (frame);
5637d62b00eSchristos   type = builtin_type (gdbarch)->builtin_data_ptr;
5647d62b00eSchristos   dlbaton = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
5657d62b00eSchristos 
5667d62b00eSchristos   SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
5677d62b00eSchristos     (framefunc, get_frame_pc (frame), &start, &length);
5687d62b00eSchristos   result = dwarf2_evaluate_loc_desc (type, frame, start, length,
5697d62b00eSchristos 				     dlbaton->per_cu, dlbaton->per_objfile);
5707d62b00eSchristos 
5717d62b00eSchristos   /* The DW_AT_frame_base attribute contains a location description which
5727d62b00eSchristos      computes the base address itself.  However, the call to
5737d62b00eSchristos      dwarf2_evaluate_loc_desc returns a value representing a variable at
5747d62b00eSchristos      that address.  The frame base address is thus this variable's
5757d62b00eSchristos      address.  */
5767d62b00eSchristos   return value_address (result);
5777d62b00eSchristos }
5787d62b00eSchristos 
5797d62b00eSchristos /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
5807d62b00eSchristos    function uses DWARF location list for its DW_AT_frame_base.  */
5817d62b00eSchristos 
5827d62b00eSchristos const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
5837d62b00eSchristos {
5847d62b00eSchristos   loclist_find_frame_base_location,
5857d62b00eSchristos   loclist_get_frame_base
5867d62b00eSchristos };
5877d62b00eSchristos 
588*6881a400Schristos /* See dwarf2/loc.h.  */
5897d62b00eSchristos 
5907d62b00eSchristos void
5917d62b00eSchristos func_get_frame_base_dwarf_block (struct symbol *framefunc, CORE_ADDR pc,
5927d62b00eSchristos 				 const gdb_byte **start, size_t *length)
5937d62b00eSchristos {
5947d62b00eSchristos   if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
5957d62b00eSchristos     {
5967d62b00eSchristos       const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
5977d62b00eSchristos 
5987d62b00eSchristos       ops_block->find_frame_base_location (framefunc, pc, start, length);
5997d62b00eSchristos     }
6007d62b00eSchristos   else
6017d62b00eSchristos     *length = 0;
6027d62b00eSchristos 
6037d62b00eSchristos   if (*length == 0)
6047d62b00eSchristos     error (_("Could not find the frame base for \"%s\"."),
6057d62b00eSchristos 	   framefunc->natural_name ());
6067d62b00eSchristos }
6077d62b00eSchristos 
608*6881a400Schristos /* See loc.h.  */
609*6881a400Schristos 
610*6881a400Schristos value *
611*6881a400Schristos compute_var_value (const char *name)
6127d62b00eSchristos {
613*6881a400Schristos   struct block_symbol sym = lookup_symbol (name, nullptr, VAR_DOMAIN,
614*6881a400Schristos 					   nullptr);
615*6881a400Schristos   if (sym.symbol != nullptr)
616*6881a400Schristos     return value_of_variable (sym.symbol, sym.block);
617*6881a400Schristos   return nullptr;
6187d62b00eSchristos }
6197d62b00eSchristos 
620*6881a400Schristos /* See dwarf2/loc.h.  */
6217d62b00eSchristos 
6227d62b00eSchristos unsigned int entry_values_debug = 0;
6237d62b00eSchristos 
6247d62b00eSchristos /* Helper to set entry_values_debug.  */
6257d62b00eSchristos 
6267d62b00eSchristos static void
6277d62b00eSchristos show_entry_values_debug (struct ui_file *file, int from_tty,
6287d62b00eSchristos 			 struct cmd_list_element *c, const char *value)
6297d62b00eSchristos {
630*6881a400Schristos   gdb_printf (file,
6317d62b00eSchristos 	      _("Entry values and tail call frames debugging is %s.\n"),
6327d62b00eSchristos 	      value);
6337d62b00eSchristos }
6347d62b00eSchristos 
635*6881a400Schristos /* See gdbtypes.h.  */
6367d62b00eSchristos 
637*6881a400Schristos void
638*6881a400Schristos call_site_target::iterate_over_addresses
639*6881a400Schristos      (struct gdbarch *call_site_gdbarch,
640*6881a400Schristos       const struct call_site *call_site,
641*6881a400Schristos       frame_info_ptr caller_frame,
642*6881a400Schristos       iterate_ftype callback) const
6437d62b00eSchristos {
644*6881a400Schristos   switch (m_loc_kind)
6457d62b00eSchristos     {
646*6881a400Schristos     case call_site_target::DWARF_BLOCK:
6477d62b00eSchristos       {
6487d62b00eSchristos 	struct dwarf2_locexpr_baton *dwarf_block;
6497d62b00eSchristos 	struct value *val;
6507d62b00eSchristos 	struct type *caller_core_addr_type;
6517d62b00eSchristos 	struct gdbarch *caller_arch;
6527d62b00eSchristos 
653*6881a400Schristos 	dwarf_block = m_loc.dwarf_block;
6547d62b00eSchristos 	if (dwarf_block == NULL)
6557d62b00eSchristos 	  {
6567d62b00eSchristos 	    struct bound_minimal_symbol msym;
6577d62b00eSchristos 
658*6881a400Schristos 	    msym = lookup_minimal_symbol_by_pc (call_site->pc () - 1);
6597d62b00eSchristos 	    throw_error (NO_ENTRY_VALUE_ERROR,
6607d62b00eSchristos 			 _("DW_AT_call_target is not specified at %s in %s"),
661*6881a400Schristos 			 paddress (call_site_gdbarch, call_site->pc ()),
6627d62b00eSchristos 			 (msym.minsym == NULL ? "???"
6637d62b00eSchristos 			  : msym.minsym->print_name ()));
6647d62b00eSchristos 
6657d62b00eSchristos 	  }
6667d62b00eSchristos 	if (caller_frame == NULL)
6677d62b00eSchristos 	  {
6687d62b00eSchristos 	    struct bound_minimal_symbol msym;
6697d62b00eSchristos 
670*6881a400Schristos 	    msym = lookup_minimal_symbol_by_pc (call_site->pc () - 1);
6717d62b00eSchristos 	    throw_error (NO_ENTRY_VALUE_ERROR,
6727d62b00eSchristos 			 _("DW_AT_call_target DWARF block resolving "
6737d62b00eSchristos 			   "requires known frame which is currently not "
6747d62b00eSchristos 			   "available at %s in %s"),
675*6881a400Schristos 			 paddress (call_site_gdbarch, call_site->pc ()),
6767d62b00eSchristos 			 (msym.minsym == NULL ? "???"
6777d62b00eSchristos 			  : msym.minsym->print_name ()));
6787d62b00eSchristos 
6797d62b00eSchristos 	  }
6807d62b00eSchristos 	caller_arch = get_frame_arch (caller_frame);
6817d62b00eSchristos 	caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
6827d62b00eSchristos 	val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
6837d62b00eSchristos 					dwarf_block->data, dwarf_block->size,
6847d62b00eSchristos 					dwarf_block->per_cu,
6857d62b00eSchristos 					dwarf_block->per_objfile);
6867d62b00eSchristos 	/* DW_AT_call_target is a DWARF expression, not a DWARF location.  */
6877d62b00eSchristos 	if (VALUE_LVAL (val) == lval_memory)
688*6881a400Schristos 	  callback (value_address (val));
6897d62b00eSchristos 	else
690*6881a400Schristos 	  callback (value_as_address (val));
6917d62b00eSchristos       }
692*6881a400Schristos       break;
6937d62b00eSchristos 
694*6881a400Schristos     case call_site_target::PHYSNAME:
6957d62b00eSchristos       {
6967d62b00eSchristos 	const char *physname;
6977d62b00eSchristos 	struct bound_minimal_symbol msym;
6987d62b00eSchristos 
699*6881a400Schristos 	physname = m_loc.physname;
7007d62b00eSchristos 
7017d62b00eSchristos 	/* Handle both the mangled and demangled PHYSNAME.  */
7027d62b00eSchristos 	msym = lookup_minimal_symbol (physname, NULL, NULL);
7037d62b00eSchristos 	if (msym.minsym == NULL)
7047d62b00eSchristos 	  {
705*6881a400Schristos 	    msym = lookup_minimal_symbol_by_pc (call_site->pc () - 1);
7067d62b00eSchristos 	    throw_error (NO_ENTRY_VALUE_ERROR,
7077d62b00eSchristos 			 _("Cannot find function \"%s\" for a call site target "
7087d62b00eSchristos 			   "at %s in %s"),
709*6881a400Schristos 			 physname, paddress (call_site_gdbarch, call_site->pc ()),
7107d62b00eSchristos 			 (msym.minsym == NULL ? "???"
7117d62b00eSchristos 			  : msym.minsym->print_name ()));
7127d62b00eSchristos 
7137d62b00eSchristos 	  }
714*6881a400Schristos 	callback (msym.value_address ());
7157d62b00eSchristos       }
716*6881a400Schristos       break;
7177d62b00eSchristos 
718*6881a400Schristos     case call_site_target::PHYSADDR:
719*6881a400Schristos       {
720*6881a400Schristos 	dwarf2_per_objfile *per_objfile = call_site->per_objfile;
721*6881a400Schristos 	compunit_symtab *cust = per_objfile->get_symtab (call_site->per_cu);
722*6881a400Schristos 	int sect_idx = cust->block_line_section ();
723*6881a400Schristos 	CORE_ADDR delta = per_objfile->objfile->section_offsets[sect_idx];
724*6881a400Schristos 
725*6881a400Schristos 	callback (m_loc.physaddr + delta);
726*6881a400Schristos       }
727*6881a400Schristos       break;
728*6881a400Schristos 
729*6881a400Schristos     case call_site_target::ADDRESSES:
730*6881a400Schristos       {
731*6881a400Schristos 	dwarf2_per_objfile *per_objfile = call_site->per_objfile;
732*6881a400Schristos 	compunit_symtab *cust = per_objfile->get_symtab (call_site->per_cu);
733*6881a400Schristos 	int sect_idx = cust->block_line_section ();
734*6881a400Schristos 	CORE_ADDR delta = per_objfile->objfile->section_offsets[sect_idx];
735*6881a400Schristos 
736*6881a400Schristos 	for (unsigned i = 0; i < m_loc.addresses.length; ++i)
737*6881a400Schristos 	  callback (m_loc.addresses.values[i] + delta);
738*6881a400Schristos       }
739*6881a400Schristos       break;
7407d62b00eSchristos 
7417d62b00eSchristos     default:
742*6881a400Schristos       internal_error (_("invalid call site target kind"));
7437d62b00eSchristos     }
7447d62b00eSchristos }
7457d62b00eSchristos 
7467d62b00eSchristos /* Convert function entry point exact address ADDR to the function which is
7477d62b00eSchristos    compliant with TAIL_CALL_LIST_COMPLETE condition.  Throw
7487d62b00eSchristos    NO_ENTRY_VALUE_ERROR otherwise.  */
7497d62b00eSchristos 
7507d62b00eSchristos static struct symbol *
7517d62b00eSchristos func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
7527d62b00eSchristos {
7537d62b00eSchristos   struct symbol *sym = find_pc_function (addr);
7547d62b00eSchristos   struct type *type;
7557d62b00eSchristos 
756*6881a400Schristos   if (sym == NULL || sym->value_block ()->entry_pc () != addr)
7577d62b00eSchristos     throw_error (NO_ENTRY_VALUE_ERROR,
7587d62b00eSchristos 		 _("DW_TAG_call_site resolving failed to find function "
7597d62b00eSchristos 		   "name for address %s"),
7607d62b00eSchristos 		 paddress (gdbarch, addr));
7617d62b00eSchristos 
762*6881a400Schristos   type = sym->type ();
7637d62b00eSchristos   gdb_assert (type->code () == TYPE_CODE_FUNC);
7647d62b00eSchristos   gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
7657d62b00eSchristos 
7667d62b00eSchristos   return sym;
7677d62b00eSchristos }
7687d62b00eSchristos 
7697d62b00eSchristos /* Verify function with entry point exact address ADDR can never call itself
7707d62b00eSchristos    via its tail calls (incl. transitively).  Throw NO_ENTRY_VALUE_ERROR if it
7717d62b00eSchristos    can call itself via tail calls.
7727d62b00eSchristos 
7737d62b00eSchristos    If a funtion can tail call itself its entry value based parameters are
7747d62b00eSchristos    unreliable.  There is no verification whether the value of some/all
7757d62b00eSchristos    parameters is unchanged through the self tail call, we expect if there is
7767d62b00eSchristos    a self tail call all the parameters can be modified.  */
7777d62b00eSchristos 
7787d62b00eSchristos static void
7797d62b00eSchristos func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
7807d62b00eSchristos {
7817d62b00eSchristos   CORE_ADDR addr;
7827d62b00eSchristos 
7837d62b00eSchristos   /* The verification is completely unordered.  Track here function addresses
7847d62b00eSchristos      which still need to be iterated.  */
7857d62b00eSchristos   std::vector<CORE_ADDR> todo;
7867d62b00eSchristos 
7877d62b00eSchristos   /* Track here CORE_ADDRs which were already visited.  */
7887d62b00eSchristos   std::unordered_set<CORE_ADDR> addr_hash;
7897d62b00eSchristos 
7907d62b00eSchristos   todo.push_back (verify_addr);
7917d62b00eSchristos   while (!todo.empty ())
7927d62b00eSchristos     {
7937d62b00eSchristos       struct symbol *func_sym;
7947d62b00eSchristos       struct call_site *call_site;
7957d62b00eSchristos 
7967d62b00eSchristos       addr = todo.back ();
7977d62b00eSchristos       todo.pop_back ();
7987d62b00eSchristos 
7997d62b00eSchristos       func_sym = func_addr_to_tail_call_list (gdbarch, addr);
8007d62b00eSchristos 
801*6881a400Schristos       for (call_site = TYPE_TAIL_CALL_LIST (func_sym->type ());
8027d62b00eSchristos 	   call_site; call_site = call_site->tail_call_next)
8037d62b00eSchristos 	{
8047d62b00eSchristos 	  /* CALLER_FRAME with registers is not available for tail-call jumped
8057d62b00eSchristos 	     frames.  */
806*6881a400Schristos 	  call_site->iterate_over_addresses (gdbarch, nullptr,
807*6881a400Schristos 					     [&] (CORE_ADDR target_addr)
808*6881a400Schristos 	    {
8097d62b00eSchristos 	      if (target_addr == verify_addr)
8107d62b00eSchristos 		{
8117d62b00eSchristos 		  struct bound_minimal_symbol msym;
8127d62b00eSchristos 
8137d62b00eSchristos 		  msym = lookup_minimal_symbol_by_pc (verify_addr);
8147d62b00eSchristos 		  throw_error (NO_ENTRY_VALUE_ERROR,
8157d62b00eSchristos 			       _("DW_OP_entry_value resolving has found "
8167d62b00eSchristos 				 "function \"%s\" at %s can call itself via tail "
8177d62b00eSchristos 				 "calls"),
8187d62b00eSchristos 			       (msym.minsym == NULL ? "???"
8197d62b00eSchristos 				: msym.minsym->print_name ()),
8207d62b00eSchristos 			       paddress (gdbarch, verify_addr));
8217d62b00eSchristos 		}
8227d62b00eSchristos 
8237d62b00eSchristos 	      if (addr_hash.insert (target_addr).second)
8247d62b00eSchristos 		todo.push_back (target_addr);
825*6881a400Schristos 	    });
8267d62b00eSchristos 	}
8277d62b00eSchristos     }
8287d62b00eSchristos }
8297d62b00eSchristos 
8307d62b00eSchristos /* Print user readable form of CALL_SITE->PC to gdb_stdlog.  Used only for
8317d62b00eSchristos    ENTRY_VALUES_DEBUG.  */
8327d62b00eSchristos 
8337d62b00eSchristos static void
8347d62b00eSchristos tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
8357d62b00eSchristos {
836*6881a400Schristos   CORE_ADDR addr = call_site->pc ();
8377d62b00eSchristos   struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
8387d62b00eSchristos 
839*6881a400Schristos   gdb_printf (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
8407d62b00eSchristos 	      (msym.minsym == NULL ? "???"
8417d62b00eSchristos 	       : msym.minsym->print_name ()));
8427d62b00eSchristos 
8437d62b00eSchristos }
8447d62b00eSchristos 
8457d62b00eSchristos /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
8467d62b00eSchristos    only top callers and bottom callees which are present in both.  GDBARCH is
8477d62b00eSchristos    used only for ENTRY_VALUES_DEBUG.  RESULTP is NULL after return if there are
8487d62b00eSchristos    no remaining possibilities to provide unambiguous non-trivial result.
8497d62b00eSchristos    RESULTP should point to NULL on the first (initialization) call.  Caller is
8507d62b00eSchristos    responsible for xfree of any RESULTP data.  */
8517d62b00eSchristos 
8527d62b00eSchristos static void
8537d62b00eSchristos chain_candidate (struct gdbarch *gdbarch,
8547d62b00eSchristos 		 gdb::unique_xmalloc_ptr<struct call_site_chain> *resultp,
855*6881a400Schristos 		 const std::vector<struct call_site *> &chain)
8567d62b00eSchristos {
857*6881a400Schristos   long length = chain.size ();
8587d62b00eSchristos   int callers, callees, idx;
8597d62b00eSchristos 
8607d62b00eSchristos   if (*resultp == NULL)
8617d62b00eSchristos     {
8627d62b00eSchristos       /* Create the initial chain containing all the passed PCs.  */
8637d62b00eSchristos 
8647d62b00eSchristos       struct call_site_chain *result
8657d62b00eSchristos 	= ((struct call_site_chain *)
8667d62b00eSchristos 	   xmalloc (sizeof (*result)
8677d62b00eSchristos 		    + sizeof (*result->call_site) * (length - 1)));
8687d62b00eSchristos       result->length = length;
8697d62b00eSchristos       result->callers = result->callees = length;
870*6881a400Schristos       if (!chain.empty ())
871*6881a400Schristos 	memcpy (result->call_site, chain.data (),
8727d62b00eSchristos 		sizeof (*result->call_site) * length);
8737d62b00eSchristos       resultp->reset (result);
8747d62b00eSchristos 
8757d62b00eSchristos       if (entry_values_debug)
8767d62b00eSchristos 	{
877*6881a400Schristos 	  gdb_printf (gdb_stdlog, "tailcall: initial:");
8787d62b00eSchristos 	  for (idx = 0; idx < length; idx++)
8797d62b00eSchristos 	    tailcall_dump (gdbarch, result->call_site[idx]);
880*6881a400Schristos 	  gdb_putc ('\n', gdb_stdlog);
8817d62b00eSchristos 	}
8827d62b00eSchristos 
8837d62b00eSchristos       return;
8847d62b00eSchristos     }
8857d62b00eSchristos 
8867d62b00eSchristos   if (entry_values_debug)
8877d62b00eSchristos     {
888*6881a400Schristos       gdb_printf (gdb_stdlog, "tailcall: compare:");
8897d62b00eSchristos       for (idx = 0; idx < length; idx++)
890*6881a400Schristos 	tailcall_dump (gdbarch, chain[idx]);
891*6881a400Schristos       gdb_putc ('\n', gdb_stdlog);
8927d62b00eSchristos     }
8937d62b00eSchristos 
8947d62b00eSchristos   /* Intersect callers.  */
8957d62b00eSchristos 
8967d62b00eSchristos   callers = std::min ((long) (*resultp)->callers, length);
8977d62b00eSchristos   for (idx = 0; idx < callers; idx++)
898*6881a400Schristos     if ((*resultp)->call_site[idx] != chain[idx])
8997d62b00eSchristos       {
9007d62b00eSchristos 	(*resultp)->callers = idx;
9017d62b00eSchristos 	break;
9027d62b00eSchristos       }
9037d62b00eSchristos 
9047d62b00eSchristos   /* Intersect callees.  */
9057d62b00eSchristos 
9067d62b00eSchristos   callees = std::min ((long) (*resultp)->callees, length);
9077d62b00eSchristos   for (idx = 0; idx < callees; idx++)
9087d62b00eSchristos     if ((*resultp)->call_site[(*resultp)->length - 1 - idx]
909*6881a400Schristos 	!= chain[length - 1 - idx])
9107d62b00eSchristos       {
9117d62b00eSchristos 	(*resultp)->callees = idx;
9127d62b00eSchristos 	break;
9137d62b00eSchristos       }
9147d62b00eSchristos 
9157d62b00eSchristos   if (entry_values_debug)
9167d62b00eSchristos     {
917*6881a400Schristos       gdb_printf (gdb_stdlog, "tailcall: reduced:");
9187d62b00eSchristos       for (idx = 0; idx < (*resultp)->callers; idx++)
9197d62b00eSchristos 	tailcall_dump (gdbarch, (*resultp)->call_site[idx]);
920*6881a400Schristos       gdb_puts (" |", gdb_stdlog);
9217d62b00eSchristos       for (idx = 0; idx < (*resultp)->callees; idx++)
9227d62b00eSchristos 	tailcall_dump (gdbarch,
9237d62b00eSchristos 		       (*resultp)->call_site[(*resultp)->length
9247d62b00eSchristos 					     - (*resultp)->callees + idx]);
925*6881a400Schristos       gdb_putc ('\n', gdb_stdlog);
9267d62b00eSchristos     }
9277d62b00eSchristos 
9287d62b00eSchristos   if ((*resultp)->callers == 0 && (*resultp)->callees == 0)
9297d62b00eSchristos     {
9307d62b00eSchristos       /* There are no common callers or callees.  It could be also a direct
9317d62b00eSchristos 	 call (which has length 0) with ambiguous possibility of an indirect
9327d62b00eSchristos 	 call - CALLERS == CALLEES == 0 is valid during the first allocation
9337d62b00eSchristos 	 but any subsequence processing of such entry means ambiguity.  */
9347d62b00eSchristos       resultp->reset (NULL);
9357d62b00eSchristos       return;
9367d62b00eSchristos     }
9377d62b00eSchristos 
9387d62b00eSchristos   /* See call_site_find_chain_1 why there is no way to reach the bottom callee
9397d62b00eSchristos      PC again.  In such case there must be two different code paths to reach
9407d62b00eSchristos      it.  CALLERS + CALLEES equal to LENGTH in the case of self tail-call.  */
9417d62b00eSchristos   gdb_assert ((*resultp)->callers + (*resultp)->callees <= (*resultp)->length);
9427d62b00eSchristos }
9437d62b00eSchristos 
944*6881a400Schristos /* Recursively try to construct the call chain.  GDBARCH, RESULTP, and
945*6881a400Schristos    CHAIN are passed to chain_candidate.  ADDR_HASH tracks which
946*6881a400Schristos    addresses have already been seen along the current chain.
947*6881a400Schristos    CALL_SITE is the call site to visit, and CALLEE_PC is the PC we're
948*6881a400Schristos    trying to "reach".  Returns false if an error has already been
949*6881a400Schristos    detected and so an early return can be done.  If it makes sense to
950*6881a400Schristos    keep trying (even if no answer has yet been found), returns
951*6881a400Schristos    true.  */
952*6881a400Schristos 
953*6881a400Schristos static bool
954*6881a400Schristos call_site_find_chain_2
955*6881a400Schristos      (struct gdbarch *gdbarch,
956*6881a400Schristos       gdb::unique_xmalloc_ptr<struct call_site_chain> *resultp,
957*6881a400Schristos       std::vector<struct call_site *> &chain,
958*6881a400Schristos       std::unordered_set<CORE_ADDR> &addr_hash,
959*6881a400Schristos       struct call_site *call_site,
960*6881a400Schristos       CORE_ADDR callee_pc)
961*6881a400Schristos {
962*6881a400Schristos   std::vector<CORE_ADDR> addresses;
963*6881a400Schristos   bool found_exact = false;
964*6881a400Schristos   call_site->iterate_over_addresses (gdbarch, nullptr,
965*6881a400Schristos 				     [&] (CORE_ADDR addr)
966*6881a400Schristos     {
967*6881a400Schristos       if (addr == callee_pc)
968*6881a400Schristos 	found_exact = true;
969*6881a400Schristos       else
970*6881a400Schristos 	addresses.push_back (addr);
971*6881a400Schristos     });
972*6881a400Schristos 
973*6881a400Schristos   if (found_exact)
974*6881a400Schristos     {
975*6881a400Schristos       chain_candidate (gdbarch, resultp, chain);
976*6881a400Schristos       /* If RESULTP was reset, then chain_candidate failed, and so we
977*6881a400Schristos 	 can tell our callers to early-return.  */
978*6881a400Schristos       return *resultp != nullptr;
979*6881a400Schristos     }
980*6881a400Schristos 
981*6881a400Schristos   for (CORE_ADDR target_func_addr : addresses)
982*6881a400Schristos     {
983*6881a400Schristos       struct symbol *target_func
984*6881a400Schristos 	= func_addr_to_tail_call_list (gdbarch, target_func_addr);
985*6881a400Schristos       for (struct call_site *target_call_site
986*6881a400Schristos 	     = TYPE_TAIL_CALL_LIST (target_func->type ());
987*6881a400Schristos 	   target_call_site != nullptr;
988*6881a400Schristos 	   target_call_site = target_call_site->tail_call_next)
989*6881a400Schristos 	{
990*6881a400Schristos 	  if (addr_hash.insert (target_call_site->pc ()).second)
991*6881a400Schristos 	    {
992*6881a400Schristos 	      /* Successfully entered TARGET_CALL_SITE.  */
993*6881a400Schristos 	      chain.push_back (target_call_site);
994*6881a400Schristos 
995*6881a400Schristos 	      if (!call_site_find_chain_2 (gdbarch, resultp, chain,
996*6881a400Schristos 					   addr_hash, target_call_site,
997*6881a400Schristos 					   callee_pc))
998*6881a400Schristos 		return false;
999*6881a400Schristos 
1000*6881a400Schristos 	      size_t removed = addr_hash.erase (target_call_site->pc ());
1001*6881a400Schristos 	      gdb_assert (removed == 1);
1002*6881a400Schristos 	      chain.pop_back ();
1003*6881a400Schristos 	    }
1004*6881a400Schristos 	}
1005*6881a400Schristos     }
1006*6881a400Schristos 
1007*6881a400Schristos   return true;
1008*6881a400Schristos }
1009*6881a400Schristos 
1010*6881a400Schristos /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All
1011*6881a400Schristos    the assumed frames between them use GDBARCH.  Any unreliability
1012*6881a400Schristos    results in thrown NO_ENTRY_VALUE_ERROR.  */
10137d62b00eSchristos 
10147d62b00eSchristos static gdb::unique_xmalloc_ptr<call_site_chain>
10157d62b00eSchristos call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
10167d62b00eSchristos 			CORE_ADDR callee_pc)
10177d62b00eSchristos {
10187d62b00eSchristos   CORE_ADDR save_callee_pc = callee_pc;
10197d62b00eSchristos   gdb::unique_xmalloc_ptr<struct call_site_chain> retval;
10207d62b00eSchristos   struct call_site *call_site;
10217d62b00eSchristos 
10227d62b00eSchristos   /* CHAIN contains only the intermediate CALL_SITEs.  Neither CALLER_PC's
10237d62b00eSchristos      call_site nor any possible call_site at CALLEE_PC's function is there.
10247d62b00eSchristos      Any CALL_SITE in CHAIN will be iterated to its siblings - via
10257d62b00eSchristos      TAIL_CALL_NEXT.  This is inappropriate for CALLER_PC's call_site.  */
10267d62b00eSchristos   std::vector<struct call_site *> chain;
10277d62b00eSchristos 
1028*6881a400Schristos   /* A given call site may have multiple associated addresses.  This
1029*6881a400Schristos      can happen if, e.g., the caller is split by hot/cold
1030*6881a400Schristos      partitioning.  This vector tracks the ones we haven't visited
1031*6881a400Schristos      yet.  */
1032*6881a400Schristos   std::vector<std::vector<CORE_ADDR>> unvisited_addresses;
1033*6881a400Schristos 
10347d62b00eSchristos   /* We are not interested in the specific PC inside the callee function.  */
10357d62b00eSchristos   callee_pc = get_pc_function_start (callee_pc);
10367d62b00eSchristos   if (callee_pc == 0)
10377d62b00eSchristos     throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
10387d62b00eSchristos 		 paddress (gdbarch, save_callee_pc));
10397d62b00eSchristos 
10407d62b00eSchristos   /* Mark CALL_SITEs so we do not visit the same ones twice.  */
10417d62b00eSchristos   std::unordered_set<CORE_ADDR> addr_hash;
10427d62b00eSchristos 
10437d62b00eSchristos   /* Do not push CALL_SITE to CHAIN.  Push there only the first tail call site
10447d62b00eSchristos      at the target's function.  All the possible tail call sites in the
10457d62b00eSchristos      target's function will get iterated as already pushed into CHAIN via their
10467d62b00eSchristos      TAIL_CALL_NEXT.  */
10477d62b00eSchristos   call_site = call_site_for_pc (gdbarch, caller_pc);
1048*6881a400Schristos   /* No need to check the return value here, because we no longer care
1049*6881a400Schristos      about possible early returns.  */
1050*6881a400Schristos   call_site_find_chain_2 (gdbarch, &retval, chain, addr_hash, call_site,
1051*6881a400Schristos 			  callee_pc);
10527d62b00eSchristos 
10537d62b00eSchristos   if (retval == NULL)
10547d62b00eSchristos     {
10557d62b00eSchristos       struct bound_minimal_symbol msym_caller, msym_callee;
10567d62b00eSchristos 
10577d62b00eSchristos       msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
10587d62b00eSchristos       msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
10597d62b00eSchristos       throw_error (NO_ENTRY_VALUE_ERROR,
10607d62b00eSchristos 		   _("There are no unambiguously determinable intermediate "
10617d62b00eSchristos 		     "callers or callees between caller function \"%s\" at %s "
10627d62b00eSchristos 		     "and callee function \"%s\" at %s"),
10637d62b00eSchristos 		   (msym_caller.minsym == NULL
10647d62b00eSchristos 		    ? "???" : msym_caller.minsym->print_name ()),
10657d62b00eSchristos 		   paddress (gdbarch, caller_pc),
10667d62b00eSchristos 		   (msym_callee.minsym == NULL
10677d62b00eSchristos 		    ? "???" : msym_callee.minsym->print_name ()),
10687d62b00eSchristos 		   paddress (gdbarch, callee_pc));
10697d62b00eSchristos     }
10707d62b00eSchristos 
10717d62b00eSchristos   return retval;
10727d62b00eSchristos }
10737d62b00eSchristos 
10747d62b00eSchristos /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
10757d62b00eSchristos    assumed frames between them use GDBARCH.  If valid call_site_chain cannot be
10767d62b00eSchristos    constructed return NULL.  */
10777d62b00eSchristos 
10787d62b00eSchristos gdb::unique_xmalloc_ptr<call_site_chain>
10797d62b00eSchristos call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
10807d62b00eSchristos 		      CORE_ADDR callee_pc)
10817d62b00eSchristos {
10827d62b00eSchristos   gdb::unique_xmalloc_ptr<call_site_chain> retval;
10837d62b00eSchristos 
10847d62b00eSchristos   try
10857d62b00eSchristos     {
10867d62b00eSchristos       retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
10877d62b00eSchristos     }
10887d62b00eSchristos   catch (const gdb_exception_error &e)
10897d62b00eSchristos     {
10907d62b00eSchristos       if (e.error == NO_ENTRY_VALUE_ERROR)
10917d62b00eSchristos 	{
10927d62b00eSchristos 	  if (entry_values_debug)
10937d62b00eSchristos 	    exception_print (gdb_stdout, e);
10947d62b00eSchristos 
10957d62b00eSchristos 	  return NULL;
10967d62b00eSchristos 	}
10977d62b00eSchristos       else
10987d62b00eSchristos 	throw;
10997d62b00eSchristos     }
11007d62b00eSchristos 
11017d62b00eSchristos   return retval;
11027d62b00eSchristos }
11037d62b00eSchristos 
11047d62b00eSchristos /* Return 1 if KIND and KIND_U match PARAMETER.  Return 0 otherwise.  */
11057d62b00eSchristos 
11067d62b00eSchristos static int
11077d62b00eSchristos call_site_parameter_matches (struct call_site_parameter *parameter,
11087d62b00eSchristos 			     enum call_site_parameter_kind kind,
11097d62b00eSchristos 			     union call_site_parameter_u kind_u)
11107d62b00eSchristos {
11117d62b00eSchristos   if (kind == parameter->kind)
11127d62b00eSchristos     switch (kind)
11137d62b00eSchristos       {
11147d62b00eSchristos       case CALL_SITE_PARAMETER_DWARF_REG:
11157d62b00eSchristos 	return kind_u.dwarf_reg == parameter->u.dwarf_reg;
11167d62b00eSchristos 
11177d62b00eSchristos       case CALL_SITE_PARAMETER_FB_OFFSET:
11187d62b00eSchristos 	return kind_u.fb_offset == parameter->u.fb_offset;
11197d62b00eSchristos 
11207d62b00eSchristos       case CALL_SITE_PARAMETER_PARAM_OFFSET:
11217d62b00eSchristos 	return kind_u.param_cu_off == parameter->u.param_cu_off;
11227d62b00eSchristos       }
11237d62b00eSchristos   return 0;
11247d62b00eSchristos }
11257d62b00eSchristos 
1126*6881a400Schristos /* See loc.h.  */
11277d62b00eSchristos 
1128*6881a400Schristos struct call_site_parameter *
1129*6881a400Schristos dwarf_expr_reg_to_entry_parameter (frame_info_ptr frame,
11307d62b00eSchristos 				   enum call_site_parameter_kind kind,
11317d62b00eSchristos 				   union call_site_parameter_u kind_u,
11327d62b00eSchristos 				   dwarf2_per_cu_data **per_cu_return,
11337d62b00eSchristos 				   dwarf2_per_objfile **per_objfile_return)
11347d62b00eSchristos {
11357d62b00eSchristos   CORE_ADDR func_addr, caller_pc;
11367d62b00eSchristos   struct gdbarch *gdbarch;
1137*6881a400Schristos   frame_info_ptr caller_frame;
11387d62b00eSchristos   struct call_site *call_site;
11397d62b00eSchristos   int iparams;
11407d62b00eSchristos   /* Initialize it just to avoid a GCC false warning.  */
11417d62b00eSchristos   struct call_site_parameter *parameter = NULL;
11427d62b00eSchristos   CORE_ADDR target_addr;
11437d62b00eSchristos 
11447d62b00eSchristos   while (get_frame_type (frame) == INLINE_FRAME)
11457d62b00eSchristos     {
11467d62b00eSchristos       frame = get_prev_frame (frame);
11477d62b00eSchristos       gdb_assert (frame != NULL);
11487d62b00eSchristos     }
11497d62b00eSchristos 
11507d62b00eSchristos   func_addr = get_frame_func (frame);
11517d62b00eSchristos   gdbarch = get_frame_arch (frame);
11527d62b00eSchristos   caller_frame = get_prev_frame (frame);
11537d62b00eSchristos   if (gdbarch != frame_unwind_arch (frame))
11547d62b00eSchristos     {
11557d62b00eSchristos       struct bound_minimal_symbol msym
11567d62b00eSchristos 	= lookup_minimal_symbol_by_pc (func_addr);
11577d62b00eSchristos       struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
11587d62b00eSchristos 
11597d62b00eSchristos       throw_error (NO_ENTRY_VALUE_ERROR,
11607d62b00eSchristos 		   _("DW_OP_entry_value resolving callee gdbarch %s "
11617d62b00eSchristos 		     "(of %s (%s)) does not match caller gdbarch %s"),
11627d62b00eSchristos 		   gdbarch_bfd_arch_info (gdbarch)->printable_name,
11637d62b00eSchristos 		   paddress (gdbarch, func_addr),
11647d62b00eSchristos 		   (msym.minsym == NULL ? "???"
11657d62b00eSchristos 		    : msym.minsym->print_name ()),
11667d62b00eSchristos 		   gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
11677d62b00eSchristos     }
11687d62b00eSchristos 
11697d62b00eSchristos   if (caller_frame == NULL)
11707d62b00eSchristos     {
11717d62b00eSchristos       struct bound_minimal_symbol msym
11727d62b00eSchristos 	= lookup_minimal_symbol_by_pc (func_addr);
11737d62b00eSchristos 
11747d62b00eSchristos       throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_entry_value resolving "
11757d62b00eSchristos 					   "requires caller of %s (%s)"),
11767d62b00eSchristos 		   paddress (gdbarch, func_addr),
11777d62b00eSchristos 		   (msym.minsym == NULL ? "???"
11787d62b00eSchristos 		    : msym.minsym->print_name ()));
11797d62b00eSchristos     }
11807d62b00eSchristos   caller_pc = get_frame_pc (caller_frame);
11817d62b00eSchristos   call_site = call_site_for_pc (gdbarch, caller_pc);
11827d62b00eSchristos 
1183*6881a400Schristos   bool found = false;
1184*6881a400Schristos   unsigned count = 0;
1185*6881a400Schristos   call_site->iterate_over_addresses (gdbarch, caller_frame,
1186*6881a400Schristos 				     [&] (CORE_ADDR addr)
1187*6881a400Schristos     {
1188*6881a400Schristos       /* Preserve any address.  */
1189*6881a400Schristos       target_addr = addr;
1190*6881a400Schristos       ++count;
1191*6881a400Schristos       if (addr == func_addr)
1192*6881a400Schristos 	found = true;
1193*6881a400Schristos     });
1194*6881a400Schristos   if (!found)
11957d62b00eSchristos     {
11967d62b00eSchristos       struct minimal_symbol *target_msym, *func_msym;
11977d62b00eSchristos 
11987d62b00eSchristos       target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
11997d62b00eSchristos       func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
12007d62b00eSchristos       throw_error (NO_ENTRY_VALUE_ERROR,
1201*6881a400Schristos 		   _("DW_OP_entry_value resolving expects callee %s at %s %s"
12027d62b00eSchristos 		     "but the called frame is for %s at %s"),
12037d62b00eSchristos 		   (target_msym == NULL ? "???"
12047d62b00eSchristos 					: target_msym->print_name ()),
12057d62b00eSchristos 		   paddress (gdbarch, target_addr),
1206*6881a400Schristos 		   (count > 0
1207*6881a400Schristos 		    ? _("(but note there are multiple addresses not listed)")
1208*6881a400Schristos 		    : ""),
12097d62b00eSchristos 		   func_msym == NULL ? "???" : func_msym->print_name (),
12107d62b00eSchristos 		   paddress (gdbarch, func_addr));
12117d62b00eSchristos     }
12127d62b00eSchristos 
12137d62b00eSchristos   /* No entry value based parameters would be reliable if this function can
12147d62b00eSchristos      call itself via tail calls.  */
12157d62b00eSchristos   func_verify_no_selftailcall (gdbarch, func_addr);
12167d62b00eSchristos 
12177d62b00eSchristos   for (iparams = 0; iparams < call_site->parameter_count; iparams++)
12187d62b00eSchristos     {
12197d62b00eSchristos       parameter = &call_site->parameter[iparams];
12207d62b00eSchristos       if (call_site_parameter_matches (parameter, kind, kind_u))
12217d62b00eSchristos 	break;
12227d62b00eSchristos     }
12237d62b00eSchristos   if (iparams == call_site->parameter_count)
12247d62b00eSchristos     {
12257d62b00eSchristos       struct minimal_symbol *msym
12267d62b00eSchristos 	= lookup_minimal_symbol_by_pc (caller_pc).minsym;
12277d62b00eSchristos 
12287d62b00eSchristos       /* DW_TAG_call_site_parameter will be missing just if GCC could not
12297d62b00eSchristos 	 determine its value.  */
12307d62b00eSchristos       throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
12317d62b00eSchristos 					   "at DW_TAG_call_site %s at %s"),
12327d62b00eSchristos 		   paddress (gdbarch, caller_pc),
12337d62b00eSchristos 		   msym == NULL ? "???" : msym->print_name ());
12347d62b00eSchristos     }
12357d62b00eSchristos 
12367d62b00eSchristos   *per_cu_return = call_site->per_cu;
12377d62b00eSchristos   *per_objfile_return = call_site->per_objfile;
12387d62b00eSchristos   return parameter;
12397d62b00eSchristos }
12407d62b00eSchristos 
12417d62b00eSchristos /* Return value for PARAMETER matching DEREF_SIZE.  If DEREF_SIZE is -1, return
12427d62b00eSchristos    the normal DW_AT_call_value block.  Otherwise return the
12437d62b00eSchristos    DW_AT_call_data_value (dereferenced) block.
12447d62b00eSchristos 
12457d62b00eSchristos    TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
12467d62b00eSchristos    struct value.
12477d62b00eSchristos 
12487d62b00eSchristos    Function always returns non-NULL, non-optimized out value.  It throws
12497d62b00eSchristos    NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason.  */
12507d62b00eSchristos 
12517d62b00eSchristos static struct value *
12527d62b00eSchristos dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
12537d62b00eSchristos 				CORE_ADDR deref_size, struct type *type,
1254*6881a400Schristos 				frame_info_ptr caller_frame,
12557d62b00eSchristos 				dwarf2_per_cu_data *per_cu,
12567d62b00eSchristos 				dwarf2_per_objfile *per_objfile)
12577d62b00eSchristos {
12587d62b00eSchristos   const gdb_byte *data_src;
12597d62b00eSchristos   size_t size;
12607d62b00eSchristos 
12617d62b00eSchristos   data_src = deref_size == -1 ? parameter->value : parameter->data_value;
12627d62b00eSchristos   size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
12637d62b00eSchristos 
12647d62b00eSchristos   /* DEREF_SIZE size is not verified here.  */
12657d62b00eSchristos   if (data_src == NULL)
12667d62b00eSchristos     throw_error (NO_ENTRY_VALUE_ERROR,
12677d62b00eSchristos 		 _("Cannot resolve DW_AT_call_data_value"));
12687d62b00eSchristos 
1269*6881a400Schristos   return dwarf2_evaluate_loc_desc (type, caller_frame, data_src, size, per_cu,
1270*6881a400Schristos 				   per_objfile, false);
12717d62b00eSchristos }
12727d62b00eSchristos 
12737d62b00eSchristos /* VALUE must be of type lval_computed with entry_data_value_funcs.  Perform
12747d62b00eSchristos    the indirect method on it, that is use its stored target value, the sole
12757d62b00eSchristos    purpose of entry_data_value_funcs..  */
12767d62b00eSchristos 
12777d62b00eSchristos static struct value *
12787d62b00eSchristos entry_data_value_coerce_ref (const struct value *value)
12797d62b00eSchristos {
12807d62b00eSchristos   struct type *checked_type = check_typedef (value_type (value));
12817d62b00eSchristos   struct value *target_val;
12827d62b00eSchristos 
12837d62b00eSchristos   if (!TYPE_IS_REFERENCE (checked_type))
12847d62b00eSchristos     return NULL;
12857d62b00eSchristos 
12867d62b00eSchristos   target_val = (struct value *) value_computed_closure (value);
12877d62b00eSchristos   value_incref (target_val);
12887d62b00eSchristos   return target_val;
12897d62b00eSchristos }
12907d62b00eSchristos 
12917d62b00eSchristos /* Implement copy_closure.  */
12927d62b00eSchristos 
12937d62b00eSchristos static void *
12947d62b00eSchristos entry_data_value_copy_closure (const struct value *v)
12957d62b00eSchristos {
12967d62b00eSchristos   struct value *target_val = (struct value *) value_computed_closure (v);
12977d62b00eSchristos 
12987d62b00eSchristos   value_incref (target_val);
12997d62b00eSchristos   return target_val;
13007d62b00eSchristos }
13017d62b00eSchristos 
13027d62b00eSchristos /* Implement free_closure.  */
13037d62b00eSchristos 
13047d62b00eSchristos static void
13057d62b00eSchristos entry_data_value_free_closure (struct value *v)
13067d62b00eSchristos {
13077d62b00eSchristos   struct value *target_val = (struct value *) value_computed_closure (v);
13087d62b00eSchristos 
13097d62b00eSchristos   value_decref (target_val);
13107d62b00eSchristos }
13117d62b00eSchristos 
13127d62b00eSchristos /* Vector for methods for an entry value reference where the referenced value
13137d62b00eSchristos    is stored in the caller.  On the first dereference use
13147d62b00eSchristos    DW_AT_call_data_value in the caller.  */
13157d62b00eSchristos 
13167d62b00eSchristos static const struct lval_funcs entry_data_value_funcs =
13177d62b00eSchristos {
13187d62b00eSchristos   NULL,	/* read */
13197d62b00eSchristos   NULL,	/* write */
1320*6881a400Schristos   nullptr,
13217d62b00eSchristos   NULL,	/* indirect */
13227d62b00eSchristos   entry_data_value_coerce_ref,
13237d62b00eSchristos   NULL,	/* check_synthetic_pointer */
13247d62b00eSchristos   entry_data_value_copy_closure,
13257d62b00eSchristos   entry_data_value_free_closure
13267d62b00eSchristos };
13277d62b00eSchristos 
1328*6881a400Schristos /* See dwarf2/loc.h.  */
1329*6881a400Schristos struct value *
1330*6881a400Schristos value_of_dwarf_reg_entry (struct type *type, frame_info_ptr frame,
13317d62b00eSchristos 			  enum call_site_parameter_kind kind,
13327d62b00eSchristos 			  union call_site_parameter_u kind_u)
13337d62b00eSchristos {
13347d62b00eSchristos   struct type *checked_type = check_typedef (type);
1335*6881a400Schristos   struct type *target_type = checked_type->target_type ();
1336*6881a400Schristos   frame_info_ptr caller_frame = get_prev_frame (frame);
13377d62b00eSchristos   struct value *outer_val, *target_val, *val;
13387d62b00eSchristos   struct call_site_parameter *parameter;
13397d62b00eSchristos   dwarf2_per_cu_data *caller_per_cu;
13407d62b00eSchristos   dwarf2_per_objfile *caller_per_objfile;
13417d62b00eSchristos 
13427d62b00eSchristos   parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
13437d62b00eSchristos 						 &caller_per_cu,
13447d62b00eSchristos 						 &caller_per_objfile);
13457d62b00eSchristos 
13467d62b00eSchristos   outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
13477d62b00eSchristos 					      type, caller_frame,
13487d62b00eSchristos 					      caller_per_cu,
13497d62b00eSchristos 					      caller_per_objfile);
13507d62b00eSchristos 
13517d62b00eSchristos   /* Check if DW_AT_call_data_value cannot be used.  If it should be
13527d62b00eSchristos      used and it is not available do not fall back to OUTER_VAL - dereferencing
13537d62b00eSchristos      TYPE_CODE_REF with non-entry data value would give current value - not the
13547d62b00eSchristos      entry value.  */
13557d62b00eSchristos 
13567d62b00eSchristos   if (!TYPE_IS_REFERENCE (checked_type)
1357*6881a400Schristos       || checked_type->target_type () == NULL)
13587d62b00eSchristos     return outer_val;
13597d62b00eSchristos 
13607d62b00eSchristos   target_val = dwarf_entry_parameter_to_value (parameter,
1361*6881a400Schristos 					       target_type->length (),
13627d62b00eSchristos 					       target_type, caller_frame,
13637d62b00eSchristos 					       caller_per_cu,
13647d62b00eSchristos 					       caller_per_objfile);
13657d62b00eSchristos 
13667d62b00eSchristos   val = allocate_computed_value (type, &entry_data_value_funcs,
13677d62b00eSchristos 				 release_value (target_val).release ());
13687d62b00eSchristos 
13697d62b00eSchristos   /* Copy the referencing pointer to the new computed value.  */
1370*6881a400Schristos   memcpy (value_contents_raw (val).data (),
1371*6881a400Schristos 	  value_contents_raw (outer_val).data (),
1372*6881a400Schristos 	  checked_type->length ());
13737d62b00eSchristos   set_value_lazy (val, 0);
13747d62b00eSchristos 
13757d62b00eSchristos   return val;
13767d62b00eSchristos }
13777d62b00eSchristos 
13787d62b00eSchristos /* Read parameter of TYPE at (callee) FRAME's function entry.  DATA and
13797d62b00eSchristos    SIZE are DWARF block used to match DW_AT_location at the caller's
13807d62b00eSchristos    DW_TAG_call_site_parameter.
13817d62b00eSchristos 
13827d62b00eSchristos    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
13837d62b00eSchristos    cannot resolve the parameter for any reason.  */
13847d62b00eSchristos 
13857d62b00eSchristos static struct value *
1386*6881a400Schristos value_of_dwarf_block_entry (struct type *type, frame_info_ptr frame,
13877d62b00eSchristos 			    const gdb_byte *block, size_t block_len)
13887d62b00eSchristos {
13897d62b00eSchristos   union call_site_parameter_u kind_u;
13907d62b00eSchristos 
13917d62b00eSchristos   kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
13927d62b00eSchristos   if (kind_u.dwarf_reg != -1)
13937d62b00eSchristos     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
13947d62b00eSchristos 				     kind_u);
13957d62b00eSchristos 
13967d62b00eSchristos   if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
13977d62b00eSchristos     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
13987d62b00eSchristos 				     kind_u);
13997d62b00eSchristos 
14007d62b00eSchristos   /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
14017d62b00eSchristos      suppressed during normal operation.  The expression can be arbitrary if
14027d62b00eSchristos      there is no caller-callee entry value binding expected.  */
14037d62b00eSchristos   throw_error (NO_ENTRY_VALUE_ERROR,
14047d62b00eSchristos 	       _("DWARF-2 expression error: DW_OP_entry_value is supported "
14057d62b00eSchristos 		 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
14067d62b00eSchristos }
14077d62b00eSchristos 
14087d62b00eSchristos /* Fetch a DW_AT_const_value through a synthetic pointer.  */
14097d62b00eSchristos 
14107d62b00eSchristos static struct value *
14117d62b00eSchristos fetch_const_value_from_synthetic_pointer (sect_offset die, LONGEST byte_offset,
14127d62b00eSchristos 					  dwarf2_per_cu_data *per_cu,
14137d62b00eSchristos 					  dwarf2_per_objfile *per_objfile,
14147d62b00eSchristos 					  struct type *type)
14157d62b00eSchristos {
14167d62b00eSchristos   struct value *result = NULL;
14177d62b00eSchristos   const gdb_byte *bytes;
14187d62b00eSchristos   LONGEST len;
14197d62b00eSchristos 
14207d62b00eSchristos   auto_obstack temp_obstack;
14217d62b00eSchristos   bytes = dwarf2_fetch_constant_bytes (die, per_cu, per_objfile,
14227d62b00eSchristos 				       &temp_obstack, &len);
14237d62b00eSchristos 
14247d62b00eSchristos   if (bytes != NULL)
14257d62b00eSchristos     {
14267d62b00eSchristos       if (byte_offset >= 0
1427*6881a400Schristos 	  && byte_offset + type->target_type ()->length () <= len)
14287d62b00eSchristos 	{
14297d62b00eSchristos 	  bytes += byte_offset;
1430*6881a400Schristos 	  result = value_from_contents (type->target_type (), bytes);
14317d62b00eSchristos 	}
14327d62b00eSchristos       else
14337d62b00eSchristos 	invalid_synthetic_pointer ();
14347d62b00eSchristos     }
14357d62b00eSchristos   else
1436*6881a400Schristos     result = allocate_optimized_out_value (type->target_type ());
14377d62b00eSchristos 
14387d62b00eSchristos   return result;
14397d62b00eSchristos }
14407d62b00eSchristos 
1441*6881a400Schristos /* See loc.h.  */
14427d62b00eSchristos 
1443*6881a400Schristos struct value *
14447d62b00eSchristos indirect_synthetic_pointer (sect_offset die, LONGEST byte_offset,
14457d62b00eSchristos 			    dwarf2_per_cu_data *per_cu,
14467d62b00eSchristos 			    dwarf2_per_objfile *per_objfile,
1447*6881a400Schristos 			    frame_info_ptr frame, struct type *type,
14487d62b00eSchristos 			    bool resolve_abstract_p)
14497d62b00eSchristos {
14507d62b00eSchristos   /* Fetch the location expression of the DIE we're pointing to.  */
14517d62b00eSchristos   auto get_frame_address_in_block_wrapper = [frame] ()
14527d62b00eSchristos     {
14537d62b00eSchristos      return get_frame_address_in_block (frame);
14547d62b00eSchristos     };
14557d62b00eSchristos   struct dwarf2_locexpr_baton baton
14567d62b00eSchristos     = dwarf2_fetch_die_loc_sect_off (die, per_cu, per_objfile,
14577d62b00eSchristos 				     get_frame_address_in_block_wrapper,
14587d62b00eSchristos 				     resolve_abstract_p);
14597d62b00eSchristos 
14607d62b00eSchristos   /* Get type of pointed-to DIE.  */
14617d62b00eSchristos   struct type *orig_type = dwarf2_fetch_die_type_sect_off (die, per_cu,
14627d62b00eSchristos 							   per_objfile);
14637d62b00eSchristos   if (orig_type == NULL)
14647d62b00eSchristos     invalid_synthetic_pointer ();
14657d62b00eSchristos 
14667d62b00eSchristos   /* If pointed-to DIE has a DW_AT_location, evaluate it and return the
14677d62b00eSchristos      resulting value.  Otherwise, it may have a DW_AT_const_value instead,
14687d62b00eSchristos      or it may've been optimized out.  */
14697d62b00eSchristos   if (baton.data != NULL)
14707d62b00eSchristos     return dwarf2_evaluate_loc_desc_full (orig_type, frame, baton.data,
14717d62b00eSchristos 					  baton.size, baton.per_cu,
14727d62b00eSchristos 					  baton.per_objfile,
1473*6881a400Schristos 					  type->target_type (),
14747d62b00eSchristos 					  byte_offset);
14757d62b00eSchristos   else
14767d62b00eSchristos     return fetch_const_value_from_synthetic_pointer (die, byte_offset, per_cu,
14777d62b00eSchristos 						     per_objfile, type);
14787d62b00eSchristos }
14797d62b00eSchristos 
14807d62b00eSchristos /* Evaluate a location description, starting at DATA and with length
14817d62b00eSchristos    SIZE, to find the current location of variable of TYPE in the
14827d62b00eSchristos    context of FRAME.  If SUBOBJ_TYPE is non-NULL, return instead the
14837d62b00eSchristos    location of the subobject of type SUBOBJ_TYPE at byte offset
14847d62b00eSchristos    SUBOBJ_BYTE_OFFSET within the variable of type TYPE.  */
14857d62b00eSchristos 
14867d62b00eSchristos static struct value *
1487*6881a400Schristos dwarf2_evaluate_loc_desc_full (struct type *type, frame_info_ptr frame,
14887d62b00eSchristos 			       const gdb_byte *data, size_t size,
14897d62b00eSchristos 			       dwarf2_per_cu_data *per_cu,
14907d62b00eSchristos 			       dwarf2_per_objfile *per_objfile,
14917d62b00eSchristos 			       struct type *subobj_type,
1492*6881a400Schristos 			       LONGEST subobj_byte_offset,
1493*6881a400Schristos 			       bool as_lval)
14947d62b00eSchristos {
14957d62b00eSchristos   if (subobj_type == NULL)
14967d62b00eSchristos     {
14977d62b00eSchristos       subobj_type = type;
14987d62b00eSchristos       subobj_byte_offset = 0;
14997d62b00eSchristos     }
15007d62b00eSchristos   else if (subobj_byte_offset < 0)
15017d62b00eSchristos     invalid_synthetic_pointer ();
15027d62b00eSchristos 
15037d62b00eSchristos   if (size == 0)
15047d62b00eSchristos     return allocate_optimized_out_value (subobj_type);
15057d62b00eSchristos 
1506*6881a400Schristos   dwarf_expr_context ctx (per_objfile, per_cu->addr_size ());
15077d62b00eSchristos 
1508*6881a400Schristos   value *retval;
15097d62b00eSchristos   scoped_value_mark free_values;
15107d62b00eSchristos 
15117d62b00eSchristos   try
15127d62b00eSchristos     {
1513*6881a400Schristos       retval = ctx.evaluate (data, size, as_lval, per_cu, frame, nullptr,
1514*6881a400Schristos 			     type, subobj_type, subobj_byte_offset);
15157d62b00eSchristos     }
15167d62b00eSchristos   catch (const gdb_exception_error &ex)
15177d62b00eSchristos     {
15187d62b00eSchristos       if (ex.error == NOT_AVAILABLE_ERROR)
15197d62b00eSchristos 	{
15207d62b00eSchristos 	  free_values.free_to_mark ();
15217d62b00eSchristos 	  retval = allocate_value (subobj_type);
15227d62b00eSchristos 	  mark_value_bytes_unavailable (retval, 0,
1523*6881a400Schristos 					subobj_type->length ());
15247d62b00eSchristos 	  return retval;
15257d62b00eSchristos 	}
15267d62b00eSchristos       else if (ex.error == NO_ENTRY_VALUE_ERROR)
15277d62b00eSchristos 	{
15287d62b00eSchristos 	  if (entry_values_debug)
15297d62b00eSchristos 	    exception_print (gdb_stdout, ex);
15307d62b00eSchristos 	  free_values.free_to_mark ();
15317d62b00eSchristos 	  return allocate_optimized_out_value (subobj_type);
15327d62b00eSchristos 	}
15337d62b00eSchristos       else
15347d62b00eSchristos 	throw;
15357d62b00eSchristos     }
15367d62b00eSchristos 
1537*6881a400Schristos   /* We need to clean up all the values that are not needed any more.
1538*6881a400Schristos      The problem with a value_ref_ptr class is that it disconnects the
1539*6881a400Schristos      RETVAL from the value garbage collection, so we need to make
1540*6881a400Schristos      a copy of that value on the stack to keep everything consistent.
1541*6881a400Schristos      The value_ref_ptr will clean up after itself at the end of this block.  */
1542*6881a400Schristos   value_ref_ptr value_holder = value_ref_ptr::new_reference (retval);
15437d62b00eSchristos   free_values.free_to_mark ();
15447d62b00eSchristos 
1545*6881a400Schristos   return value_copy (retval);
15467d62b00eSchristos }
15477d62b00eSchristos 
15487d62b00eSchristos /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
15497d62b00eSchristos    passes 0 as the byte_offset.  */
15507d62b00eSchristos 
15517d62b00eSchristos struct value *
1552*6881a400Schristos dwarf2_evaluate_loc_desc (struct type *type, frame_info_ptr frame,
15537d62b00eSchristos 			  const gdb_byte *data, size_t size,
15547d62b00eSchristos 			  dwarf2_per_cu_data *per_cu,
1555*6881a400Schristos 			  dwarf2_per_objfile *per_objfile, bool as_lval)
15567d62b00eSchristos {
15577d62b00eSchristos   return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu,
1558*6881a400Schristos 					per_objfile, NULL, 0, as_lval);
15597d62b00eSchristos }
15607d62b00eSchristos 
15617d62b00eSchristos /* Evaluates a dwarf expression and stores the result in VAL,
15627d62b00eSchristos    expecting that the dwarf expression only produces a single
15637d62b00eSchristos    CORE_ADDR.  FRAME is the frame in which the expression is
15647d62b00eSchristos    evaluated.  ADDR_STACK is a context (location of a variable) and
15657d62b00eSchristos    might be needed to evaluate the location expression.
1566*6881a400Schristos 
1567*6881a400Schristos    PUSH_VALUES is an array of values to be pushed to the expression stack
1568*6881a400Schristos    before evaluation starts.  PUSH_VALUES[0] is pushed first, then
1569*6881a400Schristos    PUSH_VALUES[1], and so on.
1570*6881a400Schristos 
1571*6881a400Schristos    Returns 1 on success, 0 otherwise.  */
15727d62b00eSchristos 
15737d62b00eSchristos static int
15747d62b00eSchristos dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton *dlbaton,
1575*6881a400Schristos 			   frame_info_ptr frame,
15767d62b00eSchristos 			   const struct property_addr_info *addr_stack,
15777d62b00eSchristos 			   CORE_ADDR *valp,
1578*6881a400Schristos 			   gdb::array_view<CORE_ADDR> push_values,
1579*6881a400Schristos 			   bool *is_reference)
15807d62b00eSchristos {
15817d62b00eSchristos   if (dlbaton == NULL || dlbaton->size == 0)
15827d62b00eSchristos     return 0;
15837d62b00eSchristos 
15847d62b00eSchristos   dwarf2_per_objfile *per_objfile = dlbaton->per_objfile;
1585*6881a400Schristos   dwarf2_per_cu_data *per_cu = dlbaton->per_cu;
1586*6881a400Schristos   dwarf_expr_context ctx (per_objfile, per_cu->addr_size ());
15877d62b00eSchristos 
1588*6881a400Schristos   value *result;
1589*6881a400Schristos   scoped_value_mark free_values;
15907d62b00eSchristos 
1591*6881a400Schristos   /* Place any initial values onto the expression stack.  */
1592*6881a400Schristos   for (const auto &val : push_values)
1593*6881a400Schristos     ctx.push_address (val, false);
15947d62b00eSchristos 
15957d62b00eSchristos   try
15967d62b00eSchristos     {
1597*6881a400Schristos       result = ctx.evaluate (dlbaton->data, dlbaton->size,
1598*6881a400Schristos 			     true, per_cu, frame, addr_stack);
15997d62b00eSchristos     }
16007d62b00eSchristos   catch (const gdb_exception_error &ex)
16017d62b00eSchristos     {
16027d62b00eSchristos       if (ex.error == NOT_AVAILABLE_ERROR)
16037d62b00eSchristos 	{
16047d62b00eSchristos 	  return 0;
16057d62b00eSchristos 	}
16067d62b00eSchristos       else if (ex.error == NO_ENTRY_VALUE_ERROR)
16077d62b00eSchristos 	{
16087d62b00eSchristos 	  if (entry_values_debug)
16097d62b00eSchristos 	    exception_print (gdb_stdout, ex);
16107d62b00eSchristos 	  return 0;
16117d62b00eSchristos 	}
16127d62b00eSchristos       else
16137d62b00eSchristos 	throw;
16147d62b00eSchristos     }
16157d62b00eSchristos 
1616*6881a400Schristos   if (value_optimized_out (result))
16177d62b00eSchristos     return 0;
1618*6881a400Schristos 
1619*6881a400Schristos   if (VALUE_LVAL (result) == lval_memory)
1620*6881a400Schristos     *valp = value_address (result);
1621*6881a400Schristos   else
1622*6881a400Schristos     {
1623*6881a400Schristos       if (VALUE_LVAL (result) == not_lval)
1624*6881a400Schristos 	*is_reference = false;
1625*6881a400Schristos 
1626*6881a400Schristos       *valp = value_as_address (result);
16277d62b00eSchristos     }
16287d62b00eSchristos 
1629*6881a400Schristos   return 1;
1630*6881a400Schristos }
1631*6881a400Schristos 
1632*6881a400Schristos /* See dwarf2/loc.h.  */
16337d62b00eSchristos 
16347d62b00eSchristos bool
16357d62b00eSchristos dwarf2_evaluate_property (const struct dynamic_prop *prop,
1636*6881a400Schristos 			  frame_info_ptr frame,
16377d62b00eSchristos 			  const struct property_addr_info *addr_stack,
16387d62b00eSchristos 			  CORE_ADDR *value,
1639*6881a400Schristos 			  gdb::array_view<CORE_ADDR> push_values)
16407d62b00eSchristos {
16417d62b00eSchristos   if (prop == NULL)
16427d62b00eSchristos     return false;
16437d62b00eSchristos 
16447d62b00eSchristos   if (frame == NULL && has_stack_frames ())
16457d62b00eSchristos     frame = get_selected_frame (NULL);
16467d62b00eSchristos 
16477d62b00eSchristos   switch (prop->kind ())
16487d62b00eSchristos     {
16497d62b00eSchristos     case PROP_LOCEXPR:
16507d62b00eSchristos       {
16517d62b00eSchristos 	const struct dwarf2_property_baton *baton
16527d62b00eSchristos 	  = (const struct dwarf2_property_baton *) prop->baton ();
16537d62b00eSchristos 	gdb_assert (baton->property_type != NULL);
16547d62b00eSchristos 
1655*6881a400Schristos 	bool is_reference = baton->locexpr.is_reference;
16567d62b00eSchristos 	if (dwarf2_locexpr_baton_eval (&baton->locexpr, frame, addr_stack,
1657*6881a400Schristos 				       value, push_values, &is_reference))
16587d62b00eSchristos 	  {
1659*6881a400Schristos 	    if (is_reference)
16607d62b00eSchristos 	      {
16617d62b00eSchristos 		struct value *val = value_at (baton->property_type, *value);
16627d62b00eSchristos 		*value = value_as_address (val);
16637d62b00eSchristos 	      }
16647d62b00eSchristos 	    else
16657d62b00eSchristos 	      {
16667d62b00eSchristos 		gdb_assert (baton->property_type != NULL);
16677d62b00eSchristos 
16687d62b00eSchristos 		struct type *type = check_typedef (baton->property_type);
1669*6881a400Schristos 		if (type->length () < sizeof (CORE_ADDR)
1670*6881a400Schristos 		    && !type->is_unsigned ())
16717d62b00eSchristos 		  {
16727d62b00eSchristos 		    /* If we have a valid return candidate and it's value
16737d62b00eSchristos 		       is signed, we have to sign-extend the value because
16747d62b00eSchristos 		       CORE_ADDR on 64bit machine has 8 bytes but address
16757d62b00eSchristos 		       size of an 32bit application is bytes.  */
16767d62b00eSchristos 		    const int addr_size
16777d62b00eSchristos 		      = (baton->locexpr.per_cu->addr_size ()
16787d62b00eSchristos 			 * TARGET_CHAR_BIT);
16797d62b00eSchristos 		    const CORE_ADDR neg_mask
16807d62b00eSchristos 		      = (~((CORE_ADDR) 0) <<  (addr_size - 1));
16817d62b00eSchristos 
16827d62b00eSchristos 		    /* Check if signed bit is set and sign-extend values.  */
16837d62b00eSchristos 		    if (*value & neg_mask)
16847d62b00eSchristos 		      *value |= neg_mask;
16857d62b00eSchristos 		  }
16867d62b00eSchristos 	      }
16877d62b00eSchristos 	    return true;
16887d62b00eSchristos 	  }
16897d62b00eSchristos       }
16907d62b00eSchristos       break;
16917d62b00eSchristos 
16927d62b00eSchristos     case PROP_LOCLIST:
16937d62b00eSchristos       {
16947d62b00eSchristos 	struct dwarf2_property_baton *baton
16957d62b00eSchristos 	  = (struct dwarf2_property_baton *) prop->baton ();
16967d62b00eSchristos 	CORE_ADDR pc;
16977d62b00eSchristos 	const gdb_byte *data;
16987d62b00eSchristos 	struct value *val;
16997d62b00eSchristos 	size_t size;
17007d62b00eSchristos 
17017d62b00eSchristos 	if (frame == NULL
17027d62b00eSchristos 	    || !get_frame_address_in_block_if_available (frame, &pc))
17037d62b00eSchristos 	  return false;
17047d62b00eSchristos 
17057d62b00eSchristos 	data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
17067d62b00eSchristos 	if (data != NULL)
17077d62b00eSchristos 	  {
17087d62b00eSchristos 	    val = dwarf2_evaluate_loc_desc (baton->property_type, frame, data,
17097d62b00eSchristos 					    size, baton->loclist.per_cu,
17107d62b00eSchristos 					    baton->loclist.per_objfile);
17117d62b00eSchristos 	    if (!value_optimized_out (val))
17127d62b00eSchristos 	      {
17137d62b00eSchristos 		*value = value_as_address (val);
17147d62b00eSchristos 		return true;
17157d62b00eSchristos 	      }
17167d62b00eSchristos 	  }
17177d62b00eSchristos       }
17187d62b00eSchristos       break;
17197d62b00eSchristos 
17207d62b00eSchristos     case PROP_CONST:
17217d62b00eSchristos       *value = prop->const_val ();
17227d62b00eSchristos       return true;
17237d62b00eSchristos 
17247d62b00eSchristos     case PROP_ADDR_OFFSET:
17257d62b00eSchristos       {
17267d62b00eSchristos 	struct dwarf2_property_baton *baton
17277d62b00eSchristos 	  = (struct dwarf2_property_baton *) prop->baton ();
17287d62b00eSchristos 	const struct property_addr_info *pinfo;
17297d62b00eSchristos 	struct value *val;
17307d62b00eSchristos 
17317d62b00eSchristos 	for (pinfo = addr_stack; pinfo != NULL; pinfo = pinfo->next)
17327d62b00eSchristos 	  {
17337d62b00eSchristos 	    /* This approach lets us avoid checking the qualifiers.  */
17347d62b00eSchristos 	    if (TYPE_MAIN_TYPE (pinfo->type)
17357d62b00eSchristos 		== TYPE_MAIN_TYPE (baton->property_type))
17367d62b00eSchristos 	      break;
17377d62b00eSchristos 	  }
17387d62b00eSchristos 	if (pinfo == NULL)
17397d62b00eSchristos 	  error (_("cannot find reference address for offset property"));
17407d62b00eSchristos 	if (pinfo->valaddr.data () != NULL)
17417d62b00eSchristos 	  val = value_from_contents
17427d62b00eSchristos 		  (baton->offset_info.type,
17437d62b00eSchristos 		   pinfo->valaddr.data () + baton->offset_info.offset);
17447d62b00eSchristos 	else
17457d62b00eSchristos 	  val = value_at (baton->offset_info.type,
17467d62b00eSchristos 			  pinfo->addr + baton->offset_info.offset);
17477d62b00eSchristos 	*value = value_as_address (val);
17487d62b00eSchristos 	return true;
17497d62b00eSchristos       }
1750*6881a400Schristos 
1751*6881a400Schristos     case PROP_VARIABLE_NAME:
1752*6881a400Schristos       {
1753*6881a400Schristos 	struct value *val = compute_var_value (prop->variable_name ());
1754*6881a400Schristos 	if (val != nullptr)
1755*6881a400Schristos 	  {
1756*6881a400Schristos 	    *value = value_as_long (val);
1757*6881a400Schristos 	    return true;
1758*6881a400Schristos 	  }
1759*6881a400Schristos       }
1760*6881a400Schristos       break;
17617d62b00eSchristos     }
17627d62b00eSchristos 
17637d62b00eSchristos   return false;
17647d62b00eSchristos }
17657d62b00eSchristos 
1766*6881a400Schristos /* See dwarf2/loc.h.  */
17677d62b00eSchristos 
17687d62b00eSchristos void
17697d62b00eSchristos dwarf2_compile_property_to_c (string_file *stream,
17707d62b00eSchristos 			      const char *result_name,
17717d62b00eSchristos 			      struct gdbarch *gdbarch,
1772*6881a400Schristos 			      std::vector<bool> &registers_used,
17737d62b00eSchristos 			      const struct dynamic_prop *prop,
17747d62b00eSchristos 			      CORE_ADDR pc,
17757d62b00eSchristos 			      struct symbol *sym)
17767d62b00eSchristos {
17777d62b00eSchristos   struct dwarf2_property_baton *baton
17787d62b00eSchristos     = (struct dwarf2_property_baton *) prop->baton ();
17797d62b00eSchristos   const gdb_byte *data;
17807d62b00eSchristos   size_t size;
17817d62b00eSchristos   dwarf2_per_cu_data *per_cu;
17827d62b00eSchristos   dwarf2_per_objfile *per_objfile;
17837d62b00eSchristos 
17847d62b00eSchristos   if (prop->kind () == PROP_LOCEXPR)
17857d62b00eSchristos     {
17867d62b00eSchristos       data = baton->locexpr.data;
17877d62b00eSchristos       size = baton->locexpr.size;
17887d62b00eSchristos       per_cu = baton->locexpr.per_cu;
17897d62b00eSchristos       per_objfile = baton->locexpr.per_objfile;
17907d62b00eSchristos     }
17917d62b00eSchristos   else
17927d62b00eSchristos     {
17937d62b00eSchristos       gdb_assert (prop->kind () == PROP_LOCLIST);
17947d62b00eSchristos 
17957d62b00eSchristos       data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
17967d62b00eSchristos       per_cu = baton->loclist.per_cu;
17977d62b00eSchristos       per_objfile = baton->loclist.per_objfile;
17987d62b00eSchristos     }
17997d62b00eSchristos 
18007d62b00eSchristos   compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
18017d62b00eSchristos 			     gdbarch, registers_used,
18027d62b00eSchristos 			     per_cu->addr_size (),
18037d62b00eSchristos 			     data, data + size, per_cu, per_objfile);
18047d62b00eSchristos }
18057d62b00eSchristos 
18067d62b00eSchristos /* Compute the correct symbol_needs_kind value for the location
1807*6881a400Schristos    expression in EXPR.
1808*6881a400Schristos 
1809*6881a400Schristos    Implemented by traversing the logical control flow graph of the
1810*6881a400Schristos    expression.  */
18117d62b00eSchristos 
18127d62b00eSchristos static enum symbol_needs_kind
1813*6881a400Schristos dwarf2_get_symbol_read_needs (gdb::array_view<const gdb_byte> expr,
18147d62b00eSchristos 			      dwarf2_per_cu_data *per_cu,
1815*6881a400Schristos 			      dwarf2_per_objfile *per_objfile,
1816*6881a400Schristos 			      bfd_endian byte_order,
1817*6881a400Schristos 			      int addr_size,
1818*6881a400Schristos 			      int ref_addr_size,
1819*6881a400Schristos 			      int depth = 0)
18207d62b00eSchristos {
1821*6881a400Schristos   enum symbol_needs_kind symbol_needs = SYMBOL_NEEDS_NONE;
18227d62b00eSchristos 
1823*6881a400Schristos   /* If the expression is empty, we have nothing to do.  */
1824*6881a400Schristos   if (expr.empty ())
1825*6881a400Schristos     return symbol_needs;
18267d62b00eSchristos 
1827*6881a400Schristos   const gdb_byte *expr_end = expr.data () + expr.size ();
18287d62b00eSchristos 
1829*6881a400Schristos   /* List of operations to visit.  Operations in this list are not visited yet,
1830*6881a400Schristos      so are not in VISITED_OPS (and vice-versa).  */
1831*6881a400Schristos   std::vector<const gdb_byte *> ops_to_visit;
18327d62b00eSchristos 
1833*6881a400Schristos   /* Operations already visited.  */
1834*6881a400Schristos   std::unordered_set<const gdb_byte *> visited_ops;
18357d62b00eSchristos 
1836*6881a400Schristos   /* Insert OP in OPS_TO_VISIT if it is within the expression's range and
1837*6881a400Schristos      hasn't been visited yet.  */
1838*6881a400Schristos   auto insert_in_ops_to_visit
1839*6881a400Schristos     = [expr_end, &visited_ops, &ops_to_visit] (const gdb_byte *op_ptr)
1840*6881a400Schristos       {
1841*6881a400Schristos 	if (op_ptr >= expr_end)
1842*6881a400Schristos 	  return;
18437d62b00eSchristos 
1844*6881a400Schristos 	if (visited_ops.find (op_ptr) != visited_ops.end ())
1845*6881a400Schristos 	  return;
18467d62b00eSchristos 
1847*6881a400Schristos 	ops_to_visit.push_back (op_ptr);
1848*6881a400Schristos       };
1849*6881a400Schristos 
1850*6881a400Schristos   /* Expressions can invoke other expressions with DW_OP_call*.  Protect against
1851*6881a400Schristos      a loop of calls.  */
1852*6881a400Schristos   const int max_depth = 256;
1853*6881a400Schristos 
1854*6881a400Schristos   if (depth > max_depth)
1855*6881a400Schristos     error (_("DWARF-2 expression error: Loop detected."));
1856*6881a400Schristos 
1857*6881a400Schristos   depth++;
1858*6881a400Schristos 
1859*6881a400Schristos   /* Initialize the to-visit list with the first operation.  */
1860*6881a400Schristos   insert_in_ops_to_visit (&expr[0]);
1861*6881a400Schristos 
1862*6881a400Schristos   while (!ops_to_visit.empty ())
1863*6881a400Schristos     {
1864*6881a400Schristos       /* Pop one op to visit, mark it as visited.  */
1865*6881a400Schristos       const gdb_byte *op_ptr = ops_to_visit.back ();
1866*6881a400Schristos       ops_to_visit.pop_back ();
1867*6881a400Schristos       gdb_assert (visited_ops.find (op_ptr) == visited_ops.end ());
1868*6881a400Schristos       visited_ops.insert (op_ptr);
1869*6881a400Schristos 
1870*6881a400Schristos       dwarf_location_atom op = (dwarf_location_atom) *op_ptr;
1871*6881a400Schristos 
1872*6881a400Schristos       /* Most operations have a single possible following operation
1873*6881a400Schristos 	 (they are not conditional branches).  The code below updates
1874*6881a400Schristos 	 OP_PTR to point to that following operation, which is pushed
1875*6881a400Schristos 	 back to OPS_TO_VISIT, if needed, at the bottom.  Here, leave
1876*6881a400Schristos 	 OP_PTR pointing just after the operand.  */
1877*6881a400Schristos       op_ptr++;
1878*6881a400Schristos 
1879*6881a400Schristos       /* The DWARF expression might have a bug causing an infinite
1880*6881a400Schristos 	 loop.  In that case, quitting is the only way out.  */
1881*6881a400Schristos       QUIT;
1882*6881a400Schristos 
1883*6881a400Schristos       switch (op)
1884*6881a400Schristos 	{
1885*6881a400Schristos 	case DW_OP_lit0:
1886*6881a400Schristos 	case DW_OP_lit1:
1887*6881a400Schristos 	case DW_OP_lit2:
1888*6881a400Schristos 	case DW_OP_lit3:
1889*6881a400Schristos 	case DW_OP_lit4:
1890*6881a400Schristos 	case DW_OP_lit5:
1891*6881a400Schristos 	case DW_OP_lit6:
1892*6881a400Schristos 	case DW_OP_lit7:
1893*6881a400Schristos 	case DW_OP_lit8:
1894*6881a400Schristos 	case DW_OP_lit9:
1895*6881a400Schristos 	case DW_OP_lit10:
1896*6881a400Schristos 	case DW_OP_lit11:
1897*6881a400Schristos 	case DW_OP_lit12:
1898*6881a400Schristos 	case DW_OP_lit13:
1899*6881a400Schristos 	case DW_OP_lit14:
1900*6881a400Schristos 	case DW_OP_lit15:
1901*6881a400Schristos 	case DW_OP_lit16:
1902*6881a400Schristos 	case DW_OP_lit17:
1903*6881a400Schristos 	case DW_OP_lit18:
1904*6881a400Schristos 	case DW_OP_lit19:
1905*6881a400Schristos 	case DW_OP_lit20:
1906*6881a400Schristos 	case DW_OP_lit21:
1907*6881a400Schristos 	case DW_OP_lit22:
1908*6881a400Schristos 	case DW_OP_lit23:
1909*6881a400Schristos 	case DW_OP_lit24:
1910*6881a400Schristos 	case DW_OP_lit25:
1911*6881a400Schristos 	case DW_OP_lit26:
1912*6881a400Schristos 	case DW_OP_lit27:
1913*6881a400Schristos 	case DW_OP_lit28:
1914*6881a400Schristos 	case DW_OP_lit29:
1915*6881a400Schristos 	case DW_OP_lit30:
1916*6881a400Schristos 	case DW_OP_lit31:
1917*6881a400Schristos 	case DW_OP_stack_value:
1918*6881a400Schristos 	case DW_OP_dup:
1919*6881a400Schristos 	case DW_OP_drop:
1920*6881a400Schristos 	case DW_OP_swap:
1921*6881a400Schristos 	case DW_OP_over:
1922*6881a400Schristos 	case DW_OP_rot:
1923*6881a400Schristos 	case DW_OP_deref:
1924*6881a400Schristos 	case DW_OP_abs:
1925*6881a400Schristos 	case DW_OP_neg:
1926*6881a400Schristos 	case DW_OP_not:
1927*6881a400Schristos 	case DW_OP_and:
1928*6881a400Schristos 	case DW_OP_div:
1929*6881a400Schristos 	case DW_OP_minus:
1930*6881a400Schristos 	case DW_OP_mod:
1931*6881a400Schristos 	case DW_OP_mul:
1932*6881a400Schristos 	case DW_OP_or:
1933*6881a400Schristos 	case DW_OP_plus:
1934*6881a400Schristos 	case DW_OP_shl:
1935*6881a400Schristos 	case DW_OP_shr:
1936*6881a400Schristos 	case DW_OP_shra:
1937*6881a400Schristos 	case DW_OP_xor:
1938*6881a400Schristos 	case DW_OP_le:
1939*6881a400Schristos 	case DW_OP_ge:
1940*6881a400Schristos 	case DW_OP_eq:
1941*6881a400Schristos 	case DW_OP_lt:
1942*6881a400Schristos 	case DW_OP_gt:
1943*6881a400Schristos 	case DW_OP_ne:
1944*6881a400Schristos 	case DW_OP_GNU_push_tls_address:
1945*6881a400Schristos 	case DW_OP_nop:
1946*6881a400Schristos 	case DW_OP_GNU_uninit:
1947*6881a400Schristos 	case DW_OP_push_object_address:
1948*6881a400Schristos 	  break;
1949*6881a400Schristos 
1950*6881a400Schristos 	case DW_OP_form_tls_address:
1951*6881a400Schristos 	  if (symbol_needs <= SYMBOL_NEEDS_REGISTERS)
1952*6881a400Schristos 	    symbol_needs = SYMBOL_NEEDS_REGISTERS;
1953*6881a400Schristos 	  break;
1954*6881a400Schristos 
1955*6881a400Schristos 	case DW_OP_convert:
1956*6881a400Schristos 	case DW_OP_GNU_convert:
1957*6881a400Schristos 	case DW_OP_reinterpret:
1958*6881a400Schristos 	case DW_OP_GNU_reinterpret:
1959*6881a400Schristos 	case DW_OP_addrx:
1960*6881a400Schristos 	case DW_OP_GNU_addr_index:
1961*6881a400Schristos 	case DW_OP_GNU_const_index:
1962*6881a400Schristos 	case DW_OP_constu:
1963*6881a400Schristos 	case DW_OP_plus_uconst:
1964*6881a400Schristos 	case DW_OP_piece:
1965*6881a400Schristos 	  op_ptr = safe_skip_leb128 (op_ptr, expr_end);
1966*6881a400Schristos 	  break;
1967*6881a400Schristos 
1968*6881a400Schristos 	case DW_OP_consts:
1969*6881a400Schristos 	  op_ptr = safe_skip_leb128 (op_ptr, expr_end);
1970*6881a400Schristos 	  break;
1971*6881a400Schristos 
1972*6881a400Schristos 	case DW_OP_bit_piece:
1973*6881a400Schristos 	  op_ptr = safe_skip_leb128 (op_ptr, expr_end);
1974*6881a400Schristos 	  op_ptr = safe_skip_leb128 (op_ptr, expr_end);
1975*6881a400Schristos 	  break;
1976*6881a400Schristos 
1977*6881a400Schristos 	case DW_OP_deref_type:
1978*6881a400Schristos 	case DW_OP_GNU_deref_type:
1979*6881a400Schristos 	  op_ptr++;
1980*6881a400Schristos 	  op_ptr = safe_skip_leb128 (op_ptr, expr_end);
1981*6881a400Schristos 	  break;
1982*6881a400Schristos 
1983*6881a400Schristos 	case DW_OP_addr:
1984*6881a400Schristos 	  op_ptr += addr_size;
1985*6881a400Schristos 	  break;
1986*6881a400Schristos 
1987*6881a400Schristos 	case DW_OP_const1u:
1988*6881a400Schristos 	case DW_OP_const1s:
1989*6881a400Schristos 	  op_ptr += 1;
1990*6881a400Schristos 	  break;
1991*6881a400Schristos 
1992*6881a400Schristos 	case DW_OP_const2u:
1993*6881a400Schristos 	case DW_OP_const2s:
1994*6881a400Schristos 	  op_ptr += 2;
1995*6881a400Schristos 	  break;
1996*6881a400Schristos 
1997*6881a400Schristos 	case DW_OP_const4s:
1998*6881a400Schristos 	case DW_OP_const4u:
1999*6881a400Schristos 	  op_ptr += 4;
2000*6881a400Schristos 	  break;
2001*6881a400Schristos 
2002*6881a400Schristos 	case DW_OP_const8s:
2003*6881a400Schristos 	case DW_OP_const8u:
2004*6881a400Schristos 	  op_ptr += 8;
2005*6881a400Schristos 	  break;
2006*6881a400Schristos 
2007*6881a400Schristos 	case DW_OP_reg0:
2008*6881a400Schristos 	case DW_OP_reg1:
2009*6881a400Schristos 	case DW_OP_reg2:
2010*6881a400Schristos 	case DW_OP_reg3:
2011*6881a400Schristos 	case DW_OP_reg4:
2012*6881a400Schristos 	case DW_OP_reg5:
2013*6881a400Schristos 	case DW_OP_reg6:
2014*6881a400Schristos 	case DW_OP_reg7:
2015*6881a400Schristos 	case DW_OP_reg8:
2016*6881a400Schristos 	case DW_OP_reg9:
2017*6881a400Schristos 	case DW_OP_reg10:
2018*6881a400Schristos 	case DW_OP_reg11:
2019*6881a400Schristos 	case DW_OP_reg12:
2020*6881a400Schristos 	case DW_OP_reg13:
2021*6881a400Schristos 	case DW_OP_reg14:
2022*6881a400Schristos 	case DW_OP_reg15:
2023*6881a400Schristos 	case DW_OP_reg16:
2024*6881a400Schristos 	case DW_OP_reg17:
2025*6881a400Schristos 	case DW_OP_reg18:
2026*6881a400Schristos 	case DW_OP_reg19:
2027*6881a400Schristos 	case DW_OP_reg20:
2028*6881a400Schristos 	case DW_OP_reg21:
2029*6881a400Schristos 	case DW_OP_reg22:
2030*6881a400Schristos 	case DW_OP_reg23:
2031*6881a400Schristos 	case DW_OP_reg24:
2032*6881a400Schristos 	case DW_OP_reg25:
2033*6881a400Schristos 	case DW_OP_reg26:
2034*6881a400Schristos 	case DW_OP_reg27:
2035*6881a400Schristos 	case DW_OP_reg28:
2036*6881a400Schristos 	case DW_OP_reg29:
2037*6881a400Schristos 	case DW_OP_reg30:
2038*6881a400Schristos 	case DW_OP_reg31:
2039*6881a400Schristos 	case DW_OP_regx:
2040*6881a400Schristos 	case DW_OP_breg0:
2041*6881a400Schristos 	case DW_OP_breg1:
2042*6881a400Schristos 	case DW_OP_breg2:
2043*6881a400Schristos 	case DW_OP_breg3:
2044*6881a400Schristos 	case DW_OP_breg4:
2045*6881a400Schristos 	case DW_OP_breg5:
2046*6881a400Schristos 	case DW_OP_breg6:
2047*6881a400Schristos 	case DW_OP_breg7:
2048*6881a400Schristos 	case DW_OP_breg8:
2049*6881a400Schristos 	case DW_OP_breg9:
2050*6881a400Schristos 	case DW_OP_breg10:
2051*6881a400Schristos 	case DW_OP_breg11:
2052*6881a400Schristos 	case DW_OP_breg12:
2053*6881a400Schristos 	case DW_OP_breg13:
2054*6881a400Schristos 	case DW_OP_breg14:
2055*6881a400Schristos 	case DW_OP_breg15:
2056*6881a400Schristos 	case DW_OP_breg16:
2057*6881a400Schristos 	case DW_OP_breg17:
2058*6881a400Schristos 	case DW_OP_breg18:
2059*6881a400Schristos 	case DW_OP_breg19:
2060*6881a400Schristos 	case DW_OP_breg20:
2061*6881a400Schristos 	case DW_OP_breg21:
2062*6881a400Schristos 	case DW_OP_breg22:
2063*6881a400Schristos 	case DW_OP_breg23:
2064*6881a400Schristos 	case DW_OP_breg24:
2065*6881a400Schristos 	case DW_OP_breg25:
2066*6881a400Schristos 	case DW_OP_breg26:
2067*6881a400Schristos 	case DW_OP_breg27:
2068*6881a400Schristos 	case DW_OP_breg28:
2069*6881a400Schristos 	case DW_OP_breg29:
2070*6881a400Schristos 	case DW_OP_breg30:
2071*6881a400Schristos 	case DW_OP_breg31:
2072*6881a400Schristos 	case DW_OP_bregx:
2073*6881a400Schristos 	case DW_OP_fbreg:
2074*6881a400Schristos 	case DW_OP_call_frame_cfa:
2075*6881a400Schristos 	case DW_OP_entry_value:
2076*6881a400Schristos 	case DW_OP_GNU_entry_value:
2077*6881a400Schristos 	case DW_OP_GNU_parameter_ref:
2078*6881a400Schristos 	case DW_OP_regval_type:
2079*6881a400Schristos 	case DW_OP_GNU_regval_type:
2080*6881a400Schristos 	  symbol_needs = SYMBOL_NEEDS_FRAME;
2081*6881a400Schristos 	  break;
2082*6881a400Schristos 
2083*6881a400Schristos 	case DW_OP_implicit_value:
2084*6881a400Schristos 	  {
2085*6881a400Schristos 	    uint64_t uoffset;
2086*6881a400Schristos 	    op_ptr = safe_read_uleb128 (op_ptr, expr_end, &uoffset);
2087*6881a400Schristos 	    op_ptr += uoffset;
2088*6881a400Schristos 	    break;
2089*6881a400Schristos 	  }
2090*6881a400Schristos 
2091*6881a400Schristos 	case DW_OP_implicit_pointer:
2092*6881a400Schristos 	case DW_OP_GNU_implicit_pointer:
2093*6881a400Schristos 	  op_ptr += ref_addr_size;
2094*6881a400Schristos 	  op_ptr = safe_skip_leb128 (op_ptr, expr_end);
2095*6881a400Schristos 	  break;
2096*6881a400Schristos 
2097*6881a400Schristos 	case DW_OP_deref_size:
2098*6881a400Schristos 	case DW_OP_pick:
2099*6881a400Schristos 	  op_ptr++;
2100*6881a400Schristos 	  break;
2101*6881a400Schristos 
2102*6881a400Schristos 	case DW_OP_skip:
2103*6881a400Schristos 	  {
2104*6881a400Schristos 	    int64_t offset = extract_signed_integer (op_ptr, 2, byte_order);
2105*6881a400Schristos 	    op_ptr += 2;
2106*6881a400Schristos 	    op_ptr += offset;
2107*6881a400Schristos 	    break;
2108*6881a400Schristos 	  }
2109*6881a400Schristos 
2110*6881a400Schristos 	case DW_OP_bra:
2111*6881a400Schristos 	  {
2112*6881a400Schristos 	    /* This is the only operation that pushes two operations in
2113*6881a400Schristos 	       the to-visit list, so handle it all here.  */
2114*6881a400Schristos 	    LONGEST offset = extract_signed_integer (op_ptr, 2, byte_order);
2115*6881a400Schristos 	    op_ptr += 2;
2116*6881a400Schristos 
2117*6881a400Schristos 	    insert_in_ops_to_visit (op_ptr + offset);
2118*6881a400Schristos 	    insert_in_ops_to_visit (op_ptr);
2119*6881a400Schristos 	    continue;
2120*6881a400Schristos 	  }
2121*6881a400Schristos 
2122*6881a400Schristos 	case DW_OP_call2:
2123*6881a400Schristos 	case DW_OP_call4:
2124*6881a400Schristos 	  {
2125*6881a400Schristos 	    unsigned int len = op == DW_OP_call2 ? 2 : 4;
2126*6881a400Schristos 	    cu_offset cu_off
2127*6881a400Schristos 	      = (cu_offset) extract_unsigned_integer (op_ptr, len, byte_order);
2128*6881a400Schristos 	    op_ptr += len;
2129*6881a400Schristos 
2130*6881a400Schristos 	    auto get_frame_pc = [&symbol_needs] ()
2131*6881a400Schristos 	      {
2132*6881a400Schristos 		symbol_needs = SYMBOL_NEEDS_FRAME;
2133*6881a400Schristos 		return 0;
2134*6881a400Schristos 	      };
2135*6881a400Schristos 
2136*6881a400Schristos 	    struct dwarf2_locexpr_baton baton
2137*6881a400Schristos 	      = dwarf2_fetch_die_loc_cu_off (cu_off, per_cu,
2138*6881a400Schristos 					     per_objfile,
2139*6881a400Schristos 					     get_frame_pc);
2140*6881a400Schristos 
2141*6881a400Schristos 	    /* If SYMBOL_NEEDS_FRAME is returned from the previous call,
2142*6881a400Schristos 	       we dont have to check the baton content.  */
2143*6881a400Schristos 	    if (symbol_needs != SYMBOL_NEEDS_FRAME)
2144*6881a400Schristos 	      {
2145*6881a400Schristos 		gdbarch *arch = baton.per_objfile->objfile->arch ();
2146*6881a400Schristos 		gdb::array_view<const gdb_byte> sub_expr (baton.data,
2147*6881a400Schristos 							  baton.size);
2148*6881a400Schristos 		symbol_needs
2149*6881a400Schristos 		  = dwarf2_get_symbol_read_needs (sub_expr,
2150*6881a400Schristos 						  baton.per_cu,
2151*6881a400Schristos 						  baton.per_objfile,
2152*6881a400Schristos 						  gdbarch_byte_order (arch),
2153*6881a400Schristos 						  baton.per_cu->addr_size (),
2154*6881a400Schristos 						  baton.per_cu->ref_addr_size (),
2155*6881a400Schristos 						  depth);
2156*6881a400Schristos 	      }
2157*6881a400Schristos 	    break;
2158*6881a400Schristos 	  }
2159*6881a400Schristos 
2160*6881a400Schristos 	case DW_OP_GNU_variable_value:
2161*6881a400Schristos 	  {
2162*6881a400Schristos 	    sect_offset sect_off
2163*6881a400Schristos 	      = (sect_offset) extract_unsigned_integer (op_ptr,
2164*6881a400Schristos 							ref_addr_size,
2165*6881a400Schristos 							byte_order);
2166*6881a400Schristos 	    op_ptr += ref_addr_size;
2167*6881a400Schristos 
2168*6881a400Schristos 	    struct type *die_type
2169*6881a400Schristos 	      = dwarf2_fetch_die_type_sect_off (sect_off, per_cu,
2170*6881a400Schristos 						per_objfile);
2171*6881a400Schristos 
2172*6881a400Schristos 	    if (die_type == NULL)
2173*6881a400Schristos 	      error (_("Bad DW_OP_GNU_variable_value DIE."));
2174*6881a400Schristos 
2175*6881a400Schristos 	    /* Note: Things still work when the following test is
2176*6881a400Schristos 	       removed.  This test and error is here to conform to the
2177*6881a400Schristos 	       proposed specification.  */
2178*6881a400Schristos 	    if (die_type->code () != TYPE_CODE_INT
2179*6881a400Schristos 	       && die_type->code () != TYPE_CODE_PTR)
2180*6881a400Schristos 	      error (_("Type of DW_OP_GNU_variable_value DIE must be "
2181*6881a400Schristos 		       "an integer or pointer."));
2182*6881a400Schristos 
2183*6881a400Schristos 	    auto get_frame_pc = [&symbol_needs] ()
2184*6881a400Schristos 	      {
2185*6881a400Schristos 		symbol_needs = SYMBOL_NEEDS_FRAME;
2186*6881a400Schristos 		return 0;
2187*6881a400Schristos 	      };
2188*6881a400Schristos 
2189*6881a400Schristos 	    struct dwarf2_locexpr_baton baton
2190*6881a400Schristos 	      = dwarf2_fetch_die_loc_sect_off (sect_off, per_cu,
2191*6881a400Schristos 					       per_objfile,
2192*6881a400Schristos 					       get_frame_pc, true);
2193*6881a400Schristos 
2194*6881a400Schristos 	    /* If SYMBOL_NEEDS_FRAME is returned from the previous call,
2195*6881a400Schristos 	       we dont have to check the baton content.  */
2196*6881a400Schristos 	    if (symbol_needs != SYMBOL_NEEDS_FRAME)
2197*6881a400Schristos 	      {
2198*6881a400Schristos 		gdbarch *arch = baton.per_objfile->objfile->arch ();
2199*6881a400Schristos 		gdb::array_view<const gdb_byte> sub_expr (baton.data,
2200*6881a400Schristos 							  baton.size);
2201*6881a400Schristos 		symbol_needs
2202*6881a400Schristos 		  = dwarf2_get_symbol_read_needs (sub_expr,
2203*6881a400Schristos 						  baton.per_cu,
2204*6881a400Schristos 						  baton.per_objfile,
2205*6881a400Schristos 						  gdbarch_byte_order (arch),
2206*6881a400Schristos 						  baton.per_cu->addr_size (),
2207*6881a400Schristos 						  baton.per_cu->ref_addr_size (),
2208*6881a400Schristos 						  depth);
2209*6881a400Schristos 	      }
2210*6881a400Schristos 	    break;
2211*6881a400Schristos 	  }
2212*6881a400Schristos 
2213*6881a400Schristos 	case DW_OP_const_type:
2214*6881a400Schristos 	case DW_OP_GNU_const_type:
2215*6881a400Schristos 	  {
2216*6881a400Schristos 	    uint64_t uoffset;
2217*6881a400Schristos 	    op_ptr = safe_read_uleb128 (op_ptr, expr_end, &uoffset);
2218*6881a400Schristos 	    gdb_byte offset = *op_ptr++;
2219*6881a400Schristos 	    op_ptr += offset;
2220*6881a400Schristos 	    break;
2221*6881a400Schristos 	  }
2222*6881a400Schristos 
2223*6881a400Schristos 	default:
2224*6881a400Schristos 	  error (_("Unhandled DWARF expression opcode 0x%x"), op);
2225*6881a400Schristos 	}
2226*6881a400Schristos 
2227*6881a400Schristos       /* If it is known that a frame information is
2228*6881a400Schristos 	 needed we can stop parsing the expression.  */
2229*6881a400Schristos       if (symbol_needs == SYMBOL_NEEDS_FRAME)
2230*6881a400Schristos 	break;
2231*6881a400Schristos 
2232*6881a400Schristos       insert_in_ops_to_visit (op_ptr);
2233*6881a400Schristos     }
2234*6881a400Schristos 
2235*6881a400Schristos   return symbol_needs;
22367d62b00eSchristos }
22377d62b00eSchristos 
22387d62b00eSchristos /* A helper function that throws an unimplemented error mentioning a
22397d62b00eSchristos    given DWARF operator.  */
22407d62b00eSchristos 
22417d62b00eSchristos static void ATTRIBUTE_NORETURN
22427d62b00eSchristos unimplemented (unsigned int op)
22437d62b00eSchristos {
22447d62b00eSchristos   const char *name = get_DW_OP_name (op);
22457d62b00eSchristos 
22467d62b00eSchristos   if (name)
22477d62b00eSchristos     error (_("DWARF operator %s cannot be translated to an agent expression"),
22487d62b00eSchristos 	   name);
22497d62b00eSchristos   else
22507d62b00eSchristos     error (_("Unknown DWARF operator 0x%02x cannot be translated "
22517d62b00eSchristos 	     "to an agent expression"),
22527d62b00eSchristos 	   op);
22537d62b00eSchristos }
22547d62b00eSchristos 
2255*6881a400Schristos /* See dwarf2/loc.h.
22567d62b00eSchristos 
22577d62b00eSchristos    This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
22587d62b00eSchristos    can issue a complaint, which is better than having every target's
22597d62b00eSchristos    implementation of dwarf2_reg_to_regnum do it.  */
22607d62b00eSchristos 
22617d62b00eSchristos int
22627d62b00eSchristos dwarf_reg_to_regnum (struct gdbarch *arch, int dwarf_reg)
22637d62b00eSchristos {
22647d62b00eSchristos   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
22657d62b00eSchristos 
22667d62b00eSchristos   if (reg == -1)
22677d62b00eSchristos     {
22687d62b00eSchristos       complaint (_("bad DWARF register number %d"), dwarf_reg);
22697d62b00eSchristos     }
22707d62b00eSchristos   return reg;
22717d62b00eSchristos }
22727d62b00eSchristos 
22737d62b00eSchristos /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
22747d62b00eSchristos    Throw an error because DWARF_REG is bad.  */
22757d62b00eSchristos 
22767d62b00eSchristos static void
22777d62b00eSchristos throw_bad_regnum_error (ULONGEST dwarf_reg)
22787d62b00eSchristos {
22797d62b00eSchristos   /* Still want to print -1 as "-1".
22807d62b00eSchristos      We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
22817d62b00eSchristos      but that's overkill for now.  */
22827d62b00eSchristos   if ((int) dwarf_reg == dwarf_reg)
22837d62b00eSchristos     error (_("Unable to access DWARF register number %d"), (int) dwarf_reg);
22847d62b00eSchristos   error (_("Unable to access DWARF register number %s"),
22857d62b00eSchristos 	 pulongest (dwarf_reg));
22867d62b00eSchristos }
22877d62b00eSchristos 
2288*6881a400Schristos /* See dwarf2/loc.h.  */
22897d62b00eSchristos 
22907d62b00eSchristos int
22917d62b00eSchristos dwarf_reg_to_regnum_or_error (struct gdbarch *arch, ULONGEST dwarf_reg)
22927d62b00eSchristos {
22937d62b00eSchristos   int reg;
22947d62b00eSchristos 
22957d62b00eSchristos   if (dwarf_reg > INT_MAX)
22967d62b00eSchristos     throw_bad_regnum_error (dwarf_reg);
22977d62b00eSchristos   /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
22987d62b00eSchristos      bad, but that's ok.  */
22997d62b00eSchristos   reg = dwarf_reg_to_regnum (arch, (int) dwarf_reg);
23007d62b00eSchristos   if (reg == -1)
23017d62b00eSchristos     throw_bad_regnum_error (dwarf_reg);
23027d62b00eSchristos   return reg;
23037d62b00eSchristos }
23047d62b00eSchristos 
23057d62b00eSchristos /* A helper function that emits an access to memory.  ARCH is the
23067d62b00eSchristos    target architecture.  EXPR is the expression which we are building.
23077d62b00eSchristos    NBITS is the number of bits we want to read.  This emits the
23087d62b00eSchristos    opcodes needed to read the memory and then extract the desired
23097d62b00eSchristos    bits.  */
23107d62b00eSchristos 
23117d62b00eSchristos static void
23127d62b00eSchristos access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
23137d62b00eSchristos {
23147d62b00eSchristos   ULONGEST nbytes = (nbits + 7) / 8;
23157d62b00eSchristos 
23167d62b00eSchristos   gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
23177d62b00eSchristos 
23187d62b00eSchristos   if (expr->tracing)
23197d62b00eSchristos     ax_trace_quick (expr, nbytes);
23207d62b00eSchristos 
23217d62b00eSchristos   if (nbits <= 8)
23227d62b00eSchristos     ax_simple (expr, aop_ref8);
23237d62b00eSchristos   else if (nbits <= 16)
23247d62b00eSchristos     ax_simple (expr, aop_ref16);
23257d62b00eSchristos   else if (nbits <= 32)
23267d62b00eSchristos     ax_simple (expr, aop_ref32);
23277d62b00eSchristos   else
23287d62b00eSchristos     ax_simple (expr, aop_ref64);
23297d62b00eSchristos 
23307d62b00eSchristos   /* If we read exactly the number of bytes we wanted, we're done.  */
23317d62b00eSchristos   if (8 * nbytes == nbits)
23327d62b00eSchristos     return;
23337d62b00eSchristos 
23347d62b00eSchristos   if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
23357d62b00eSchristos     {
23367d62b00eSchristos       /* On a bits-big-endian machine, we want the high-order
23377d62b00eSchristos 	 NBITS.  */
23387d62b00eSchristos       ax_const_l (expr, 8 * nbytes - nbits);
23397d62b00eSchristos       ax_simple (expr, aop_rsh_unsigned);
23407d62b00eSchristos     }
23417d62b00eSchristos   else
23427d62b00eSchristos     {
23437d62b00eSchristos       /* On a bits-little-endian box, we want the low-order NBITS.  */
23447d62b00eSchristos       ax_zero_ext (expr, nbits);
23457d62b00eSchristos     }
23467d62b00eSchristos }
23477d62b00eSchristos 
23487d62b00eSchristos /* Compile a DWARF location expression to an agent expression.
23497d62b00eSchristos 
23507d62b00eSchristos    EXPR is the agent expression we are building.
23517d62b00eSchristos    LOC is the agent value we modify.
23527d62b00eSchristos    ARCH is the architecture.
23537d62b00eSchristos    ADDR_SIZE is the size of addresses, in bytes.
23547d62b00eSchristos    OP_PTR is the start of the location expression.
23557d62b00eSchristos    OP_END is one past the last byte of the location expression.
23567d62b00eSchristos 
23577d62b00eSchristos    This will throw an exception for various kinds of errors -- for
23587d62b00eSchristos    example, if the expression cannot be compiled, or if the expression
23597d62b00eSchristos    is invalid.  */
23607d62b00eSchristos 
23617d62b00eSchristos static void
23627d62b00eSchristos dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
23637d62b00eSchristos 			   unsigned int addr_size, const gdb_byte *op_ptr,
23647d62b00eSchristos 			   const gdb_byte *op_end,
23657d62b00eSchristos 			   dwarf2_per_cu_data *per_cu,
23667d62b00eSchristos 			   dwarf2_per_objfile *per_objfile)
23677d62b00eSchristos {
23687d62b00eSchristos   gdbarch *arch = expr->gdbarch;
23697d62b00eSchristos   std::vector<int> dw_labels, patches;
23707d62b00eSchristos   const gdb_byte * const base = op_ptr;
23717d62b00eSchristos   const gdb_byte *previous_piece = op_ptr;
23727d62b00eSchristos   enum bfd_endian byte_order = gdbarch_byte_order (arch);
23737d62b00eSchristos   ULONGEST bits_collected = 0;
23747d62b00eSchristos   unsigned int addr_size_bits = 8 * addr_size;
23757d62b00eSchristos   bool bits_big_endian = byte_order == BFD_ENDIAN_BIG;
23767d62b00eSchristos 
23777d62b00eSchristos   std::vector<int> offsets (op_end - op_ptr, -1);
23787d62b00eSchristos 
23797d62b00eSchristos   /* By default we are making an address.  */
23807d62b00eSchristos   loc->kind = axs_lvalue_memory;
23817d62b00eSchristos 
23827d62b00eSchristos   while (op_ptr < op_end)
23837d62b00eSchristos     {
23847d62b00eSchristos       enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
23857d62b00eSchristos       uint64_t uoffset, reg;
23867d62b00eSchristos       int64_t offset;
23877d62b00eSchristos       int i;
23887d62b00eSchristos 
23897d62b00eSchristos       offsets[op_ptr - base] = expr->len;
23907d62b00eSchristos       ++op_ptr;
23917d62b00eSchristos 
23927d62b00eSchristos       /* Our basic approach to code generation is to map DWARF
23937d62b00eSchristos 	 operations directly to AX operations.  However, there are
23947d62b00eSchristos 	 some differences.
23957d62b00eSchristos 
23967d62b00eSchristos 	 First, DWARF works on address-sized units, but AX always uses
23977d62b00eSchristos 	 LONGEST.  For most operations we simply ignore this
23987d62b00eSchristos 	 difference; instead we generate sign extensions as needed
23997d62b00eSchristos 	 before division and comparison operations.  It would be nice
24007d62b00eSchristos 	 to omit the sign extensions, but there is no way to determine
24017d62b00eSchristos 	 the size of the target's LONGEST.  (This code uses the size
24027d62b00eSchristos 	 of the host LONGEST in some cases -- that is a bug but it is
24037d62b00eSchristos 	 difficult to fix.)
24047d62b00eSchristos 
24057d62b00eSchristos 	 Second, some DWARF operations cannot be translated to AX.
24067d62b00eSchristos 	 For these we simply fail.  See
24077d62b00eSchristos 	 http://sourceware.org/bugzilla/show_bug.cgi?id=11662.  */
24087d62b00eSchristos       switch (op)
24097d62b00eSchristos 	{
24107d62b00eSchristos 	case DW_OP_lit0:
24117d62b00eSchristos 	case DW_OP_lit1:
24127d62b00eSchristos 	case DW_OP_lit2:
24137d62b00eSchristos 	case DW_OP_lit3:
24147d62b00eSchristos 	case DW_OP_lit4:
24157d62b00eSchristos 	case DW_OP_lit5:
24167d62b00eSchristos 	case DW_OP_lit6:
24177d62b00eSchristos 	case DW_OP_lit7:
24187d62b00eSchristos 	case DW_OP_lit8:
24197d62b00eSchristos 	case DW_OP_lit9:
24207d62b00eSchristos 	case DW_OP_lit10:
24217d62b00eSchristos 	case DW_OP_lit11:
24227d62b00eSchristos 	case DW_OP_lit12:
24237d62b00eSchristos 	case DW_OP_lit13:
24247d62b00eSchristos 	case DW_OP_lit14:
24257d62b00eSchristos 	case DW_OP_lit15:
24267d62b00eSchristos 	case DW_OP_lit16:
24277d62b00eSchristos 	case DW_OP_lit17:
24287d62b00eSchristos 	case DW_OP_lit18:
24297d62b00eSchristos 	case DW_OP_lit19:
24307d62b00eSchristos 	case DW_OP_lit20:
24317d62b00eSchristos 	case DW_OP_lit21:
24327d62b00eSchristos 	case DW_OP_lit22:
24337d62b00eSchristos 	case DW_OP_lit23:
24347d62b00eSchristos 	case DW_OP_lit24:
24357d62b00eSchristos 	case DW_OP_lit25:
24367d62b00eSchristos 	case DW_OP_lit26:
24377d62b00eSchristos 	case DW_OP_lit27:
24387d62b00eSchristos 	case DW_OP_lit28:
24397d62b00eSchristos 	case DW_OP_lit29:
24407d62b00eSchristos 	case DW_OP_lit30:
24417d62b00eSchristos 	case DW_OP_lit31:
24427d62b00eSchristos 	  ax_const_l (expr, op - DW_OP_lit0);
24437d62b00eSchristos 	  break;
24447d62b00eSchristos 
24457d62b00eSchristos 	case DW_OP_addr:
24467d62b00eSchristos 	  uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
24477d62b00eSchristos 	  op_ptr += addr_size;
24487d62b00eSchristos 	  /* Some versions of GCC emit DW_OP_addr before
24497d62b00eSchristos 	     DW_OP_GNU_push_tls_address.  In this case the value is an
24507d62b00eSchristos 	     index, not an address.  We don't support things like
24517d62b00eSchristos 	     branching between the address and the TLS op.  */
24527d62b00eSchristos 	  if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
24537d62b00eSchristos 	    uoffset += per_objfile->objfile->text_section_offset ();
24547d62b00eSchristos 	  ax_const_l (expr, uoffset);
24557d62b00eSchristos 	  break;
24567d62b00eSchristos 
24577d62b00eSchristos 	case DW_OP_const1u:
24587d62b00eSchristos 	  ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
24597d62b00eSchristos 	  op_ptr += 1;
24607d62b00eSchristos 	  break;
24617d62b00eSchristos 
24627d62b00eSchristos 	case DW_OP_const1s:
24637d62b00eSchristos 	  ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
24647d62b00eSchristos 	  op_ptr += 1;
24657d62b00eSchristos 	  break;
24667d62b00eSchristos 
24677d62b00eSchristos 	case DW_OP_const2u:
24687d62b00eSchristos 	  ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
24697d62b00eSchristos 	  op_ptr += 2;
24707d62b00eSchristos 	  break;
24717d62b00eSchristos 
24727d62b00eSchristos 	case DW_OP_const2s:
24737d62b00eSchristos 	  ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
24747d62b00eSchristos 	  op_ptr += 2;
24757d62b00eSchristos 	  break;
24767d62b00eSchristos 
24777d62b00eSchristos 	case DW_OP_const4u:
24787d62b00eSchristos 	  ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
24797d62b00eSchristos 	  op_ptr += 4;
24807d62b00eSchristos 	  break;
24817d62b00eSchristos 
24827d62b00eSchristos 	case DW_OP_const4s:
24837d62b00eSchristos 	  ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
24847d62b00eSchristos 	  op_ptr += 4;
24857d62b00eSchristos 	  break;
24867d62b00eSchristos 
24877d62b00eSchristos 	case DW_OP_const8u:
24887d62b00eSchristos 	  ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
24897d62b00eSchristos 	  op_ptr += 8;
24907d62b00eSchristos 	  break;
24917d62b00eSchristos 
24927d62b00eSchristos 	case DW_OP_const8s:
24937d62b00eSchristos 	  ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
24947d62b00eSchristos 	  op_ptr += 8;
24957d62b00eSchristos 	  break;
24967d62b00eSchristos 
24977d62b00eSchristos 	case DW_OP_constu:
24987d62b00eSchristos 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
24997d62b00eSchristos 	  ax_const_l (expr, uoffset);
25007d62b00eSchristos 	  break;
25017d62b00eSchristos 
25027d62b00eSchristos 	case DW_OP_consts:
25037d62b00eSchristos 	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
25047d62b00eSchristos 	  ax_const_l (expr, offset);
25057d62b00eSchristos 	  break;
25067d62b00eSchristos 
25077d62b00eSchristos 	case DW_OP_reg0:
25087d62b00eSchristos 	case DW_OP_reg1:
25097d62b00eSchristos 	case DW_OP_reg2:
25107d62b00eSchristos 	case DW_OP_reg3:
25117d62b00eSchristos 	case DW_OP_reg4:
25127d62b00eSchristos 	case DW_OP_reg5:
25137d62b00eSchristos 	case DW_OP_reg6:
25147d62b00eSchristos 	case DW_OP_reg7:
25157d62b00eSchristos 	case DW_OP_reg8:
25167d62b00eSchristos 	case DW_OP_reg9:
25177d62b00eSchristos 	case DW_OP_reg10:
25187d62b00eSchristos 	case DW_OP_reg11:
25197d62b00eSchristos 	case DW_OP_reg12:
25207d62b00eSchristos 	case DW_OP_reg13:
25217d62b00eSchristos 	case DW_OP_reg14:
25227d62b00eSchristos 	case DW_OP_reg15:
25237d62b00eSchristos 	case DW_OP_reg16:
25247d62b00eSchristos 	case DW_OP_reg17:
25257d62b00eSchristos 	case DW_OP_reg18:
25267d62b00eSchristos 	case DW_OP_reg19:
25277d62b00eSchristos 	case DW_OP_reg20:
25287d62b00eSchristos 	case DW_OP_reg21:
25297d62b00eSchristos 	case DW_OP_reg22:
25307d62b00eSchristos 	case DW_OP_reg23:
25317d62b00eSchristos 	case DW_OP_reg24:
25327d62b00eSchristos 	case DW_OP_reg25:
25337d62b00eSchristos 	case DW_OP_reg26:
25347d62b00eSchristos 	case DW_OP_reg27:
25357d62b00eSchristos 	case DW_OP_reg28:
25367d62b00eSchristos 	case DW_OP_reg29:
25377d62b00eSchristos 	case DW_OP_reg30:
25387d62b00eSchristos 	case DW_OP_reg31:
25397d62b00eSchristos 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
25407d62b00eSchristos 	  loc->u.reg = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
25417d62b00eSchristos 	  loc->kind = axs_lvalue_register;
25427d62b00eSchristos 	  break;
25437d62b00eSchristos 
25447d62b00eSchristos 	case DW_OP_regx:
25457d62b00eSchristos 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
25467d62b00eSchristos 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
25477d62b00eSchristos 	  loc->u.reg = dwarf_reg_to_regnum_or_error (arch, reg);
25487d62b00eSchristos 	  loc->kind = axs_lvalue_register;
25497d62b00eSchristos 	  break;
25507d62b00eSchristos 
25517d62b00eSchristos 	case DW_OP_implicit_value:
25527d62b00eSchristos 	  {
25537d62b00eSchristos 	    uint64_t len;
25547d62b00eSchristos 
25557d62b00eSchristos 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
25567d62b00eSchristos 	    if (op_ptr + len > op_end)
25577d62b00eSchristos 	      error (_("DW_OP_implicit_value: too few bytes available."));
25587d62b00eSchristos 	    if (len > sizeof (ULONGEST))
25597d62b00eSchristos 	      error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
25607d62b00eSchristos 		     (int) len);
25617d62b00eSchristos 
25627d62b00eSchristos 	    ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
25637d62b00eSchristos 							byte_order));
25647d62b00eSchristos 	    op_ptr += len;
25657d62b00eSchristos 	    dwarf_expr_require_composition (op_ptr, op_end,
25667d62b00eSchristos 					    "DW_OP_implicit_value");
25677d62b00eSchristos 
25687d62b00eSchristos 	    loc->kind = axs_rvalue;
25697d62b00eSchristos 	  }
25707d62b00eSchristos 	  break;
25717d62b00eSchristos 
25727d62b00eSchristos 	case DW_OP_stack_value:
25737d62b00eSchristos 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
25747d62b00eSchristos 	  loc->kind = axs_rvalue;
25757d62b00eSchristos 	  break;
25767d62b00eSchristos 
25777d62b00eSchristos 	case DW_OP_breg0:
25787d62b00eSchristos 	case DW_OP_breg1:
25797d62b00eSchristos 	case DW_OP_breg2:
25807d62b00eSchristos 	case DW_OP_breg3:
25817d62b00eSchristos 	case DW_OP_breg4:
25827d62b00eSchristos 	case DW_OP_breg5:
25837d62b00eSchristos 	case DW_OP_breg6:
25847d62b00eSchristos 	case DW_OP_breg7:
25857d62b00eSchristos 	case DW_OP_breg8:
25867d62b00eSchristos 	case DW_OP_breg9:
25877d62b00eSchristos 	case DW_OP_breg10:
25887d62b00eSchristos 	case DW_OP_breg11:
25897d62b00eSchristos 	case DW_OP_breg12:
25907d62b00eSchristos 	case DW_OP_breg13:
25917d62b00eSchristos 	case DW_OP_breg14:
25927d62b00eSchristos 	case DW_OP_breg15:
25937d62b00eSchristos 	case DW_OP_breg16:
25947d62b00eSchristos 	case DW_OP_breg17:
25957d62b00eSchristos 	case DW_OP_breg18:
25967d62b00eSchristos 	case DW_OP_breg19:
25977d62b00eSchristos 	case DW_OP_breg20:
25987d62b00eSchristos 	case DW_OP_breg21:
25997d62b00eSchristos 	case DW_OP_breg22:
26007d62b00eSchristos 	case DW_OP_breg23:
26017d62b00eSchristos 	case DW_OP_breg24:
26027d62b00eSchristos 	case DW_OP_breg25:
26037d62b00eSchristos 	case DW_OP_breg26:
26047d62b00eSchristos 	case DW_OP_breg27:
26057d62b00eSchristos 	case DW_OP_breg28:
26067d62b00eSchristos 	case DW_OP_breg29:
26077d62b00eSchristos 	case DW_OP_breg30:
26087d62b00eSchristos 	case DW_OP_breg31:
26097d62b00eSchristos 	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
26107d62b00eSchristos 	  i = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
26117d62b00eSchristos 	  ax_reg (expr, i);
26127d62b00eSchristos 	  if (offset != 0)
26137d62b00eSchristos 	    {
26147d62b00eSchristos 	      ax_const_l (expr, offset);
26157d62b00eSchristos 	      ax_simple (expr, aop_add);
26167d62b00eSchristos 	    }
26177d62b00eSchristos 	  break;
26187d62b00eSchristos 
26197d62b00eSchristos 	case DW_OP_bregx:
26207d62b00eSchristos 	  {
26217d62b00eSchristos 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
26227d62b00eSchristos 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
26237d62b00eSchristos 	    i = dwarf_reg_to_regnum_or_error (arch, reg);
26247d62b00eSchristos 	    ax_reg (expr, i);
26257d62b00eSchristos 	    if (offset != 0)
26267d62b00eSchristos 	      {
26277d62b00eSchristos 		ax_const_l (expr, offset);
26287d62b00eSchristos 		ax_simple (expr, aop_add);
26297d62b00eSchristos 	      }
26307d62b00eSchristos 	  }
26317d62b00eSchristos 	  break;
26327d62b00eSchristos 
26337d62b00eSchristos 	case DW_OP_fbreg:
26347d62b00eSchristos 	  {
26357d62b00eSchristos 	    const gdb_byte *datastart;
26367d62b00eSchristos 	    size_t datalen;
26377d62b00eSchristos 	    const struct block *b;
26387d62b00eSchristos 	    struct symbol *framefunc;
26397d62b00eSchristos 
26407d62b00eSchristos 	    b = block_for_pc (expr->scope);
26417d62b00eSchristos 
26427d62b00eSchristos 	    if (!b)
26437d62b00eSchristos 	      error (_("No block found for address"));
26447d62b00eSchristos 
26457d62b00eSchristos 	    framefunc = block_linkage_function (b);
26467d62b00eSchristos 
26477d62b00eSchristos 	    if (!framefunc)
26487d62b00eSchristos 	      error (_("No function found for block"));
26497d62b00eSchristos 
26507d62b00eSchristos 	    func_get_frame_base_dwarf_block (framefunc, expr->scope,
26517d62b00eSchristos 					     &datastart, &datalen);
26527d62b00eSchristos 
26537d62b00eSchristos 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
26547d62b00eSchristos 	    dwarf2_compile_expr_to_ax (expr, loc, addr_size, datastart,
26557d62b00eSchristos 				       datastart + datalen, per_cu,
26567d62b00eSchristos 				       per_objfile);
26577d62b00eSchristos 	    if (loc->kind == axs_lvalue_register)
26587d62b00eSchristos 	      require_rvalue (expr, loc);
26597d62b00eSchristos 
26607d62b00eSchristos 	    if (offset != 0)
26617d62b00eSchristos 	      {
26627d62b00eSchristos 		ax_const_l (expr, offset);
26637d62b00eSchristos 		ax_simple (expr, aop_add);
26647d62b00eSchristos 	      }
26657d62b00eSchristos 
26667d62b00eSchristos 	    loc->kind = axs_lvalue_memory;
26677d62b00eSchristos 	  }
26687d62b00eSchristos 	  break;
26697d62b00eSchristos 
26707d62b00eSchristos 	case DW_OP_dup:
26717d62b00eSchristos 	  ax_simple (expr, aop_dup);
26727d62b00eSchristos 	  break;
26737d62b00eSchristos 
26747d62b00eSchristos 	case DW_OP_drop:
26757d62b00eSchristos 	  ax_simple (expr, aop_pop);
26767d62b00eSchristos 	  break;
26777d62b00eSchristos 
26787d62b00eSchristos 	case DW_OP_pick:
26797d62b00eSchristos 	  offset = *op_ptr++;
26807d62b00eSchristos 	  ax_pick (expr, offset);
26817d62b00eSchristos 	  break;
26827d62b00eSchristos 
26837d62b00eSchristos 	case DW_OP_swap:
26847d62b00eSchristos 	  ax_simple (expr, aop_swap);
26857d62b00eSchristos 	  break;
26867d62b00eSchristos 
26877d62b00eSchristos 	case DW_OP_over:
26887d62b00eSchristos 	  ax_pick (expr, 1);
26897d62b00eSchristos 	  break;
26907d62b00eSchristos 
26917d62b00eSchristos 	case DW_OP_rot:
26927d62b00eSchristos 	  ax_simple (expr, aop_rot);
26937d62b00eSchristos 	  break;
26947d62b00eSchristos 
26957d62b00eSchristos 	case DW_OP_deref:
26967d62b00eSchristos 	case DW_OP_deref_size:
26977d62b00eSchristos 	  {
26987d62b00eSchristos 	    int size;
26997d62b00eSchristos 
27007d62b00eSchristos 	    if (op == DW_OP_deref_size)
27017d62b00eSchristos 	      size = *op_ptr++;
27027d62b00eSchristos 	    else
27037d62b00eSchristos 	      size = addr_size;
27047d62b00eSchristos 
27057d62b00eSchristos 	    if (size != 1 && size != 2 && size != 4 && size != 8)
27067d62b00eSchristos 	      error (_("Unsupported size %d in %s"),
27077d62b00eSchristos 		     size, get_DW_OP_name (op));
27087d62b00eSchristos 	    access_memory (arch, expr, size * TARGET_CHAR_BIT);
27097d62b00eSchristos 	  }
27107d62b00eSchristos 	  break;
27117d62b00eSchristos 
27127d62b00eSchristos 	case DW_OP_abs:
27137d62b00eSchristos 	  /* Sign extend the operand.  */
27147d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
27157d62b00eSchristos 	  ax_simple (expr, aop_dup);
27167d62b00eSchristos 	  ax_const_l (expr, 0);
27177d62b00eSchristos 	  ax_simple (expr, aop_less_signed);
27187d62b00eSchristos 	  ax_simple (expr, aop_log_not);
27197d62b00eSchristos 	  i = ax_goto (expr, aop_if_goto);
27207d62b00eSchristos 	  /* We have to emit 0 - X.  */
27217d62b00eSchristos 	  ax_const_l (expr, 0);
27227d62b00eSchristos 	  ax_simple (expr, aop_swap);
27237d62b00eSchristos 	  ax_simple (expr, aop_sub);
27247d62b00eSchristos 	  ax_label (expr, i, expr->len);
27257d62b00eSchristos 	  break;
27267d62b00eSchristos 
27277d62b00eSchristos 	case DW_OP_neg:
27287d62b00eSchristos 	  /* No need to sign extend here.  */
27297d62b00eSchristos 	  ax_const_l (expr, 0);
27307d62b00eSchristos 	  ax_simple (expr, aop_swap);
27317d62b00eSchristos 	  ax_simple (expr, aop_sub);
27327d62b00eSchristos 	  break;
27337d62b00eSchristos 
27347d62b00eSchristos 	case DW_OP_not:
27357d62b00eSchristos 	  /* Sign extend the operand.  */
27367d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
27377d62b00eSchristos 	  ax_simple (expr, aop_bit_not);
27387d62b00eSchristos 	  break;
27397d62b00eSchristos 
27407d62b00eSchristos 	case DW_OP_plus_uconst:
27417d62b00eSchristos 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
27427d62b00eSchristos 	  /* It would be really weird to emit `DW_OP_plus_uconst 0',
27437d62b00eSchristos 	     but we micro-optimize anyhow.  */
27447d62b00eSchristos 	  if (reg != 0)
27457d62b00eSchristos 	    {
27467d62b00eSchristos 	      ax_const_l (expr, reg);
27477d62b00eSchristos 	      ax_simple (expr, aop_add);
27487d62b00eSchristos 	    }
27497d62b00eSchristos 	  break;
27507d62b00eSchristos 
27517d62b00eSchristos 	case DW_OP_and:
27527d62b00eSchristos 	  ax_simple (expr, aop_bit_and);
27537d62b00eSchristos 	  break;
27547d62b00eSchristos 
27557d62b00eSchristos 	case DW_OP_div:
27567d62b00eSchristos 	  /* Sign extend the operands.  */
27577d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
27587d62b00eSchristos 	  ax_simple (expr, aop_swap);
27597d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
27607d62b00eSchristos 	  ax_simple (expr, aop_swap);
27617d62b00eSchristos 	  ax_simple (expr, aop_div_signed);
27627d62b00eSchristos 	  break;
27637d62b00eSchristos 
27647d62b00eSchristos 	case DW_OP_minus:
27657d62b00eSchristos 	  ax_simple (expr, aop_sub);
27667d62b00eSchristos 	  break;
27677d62b00eSchristos 
27687d62b00eSchristos 	case DW_OP_mod:
27697d62b00eSchristos 	  ax_simple (expr, aop_rem_unsigned);
27707d62b00eSchristos 	  break;
27717d62b00eSchristos 
27727d62b00eSchristos 	case DW_OP_mul:
27737d62b00eSchristos 	  ax_simple (expr, aop_mul);
27747d62b00eSchristos 	  break;
27757d62b00eSchristos 
27767d62b00eSchristos 	case DW_OP_or:
27777d62b00eSchristos 	  ax_simple (expr, aop_bit_or);
27787d62b00eSchristos 	  break;
27797d62b00eSchristos 
27807d62b00eSchristos 	case DW_OP_plus:
27817d62b00eSchristos 	  ax_simple (expr, aop_add);
27827d62b00eSchristos 	  break;
27837d62b00eSchristos 
27847d62b00eSchristos 	case DW_OP_shl:
27857d62b00eSchristos 	  ax_simple (expr, aop_lsh);
27867d62b00eSchristos 	  break;
27877d62b00eSchristos 
27887d62b00eSchristos 	case DW_OP_shr:
27897d62b00eSchristos 	  ax_simple (expr, aop_rsh_unsigned);
27907d62b00eSchristos 	  break;
27917d62b00eSchristos 
27927d62b00eSchristos 	case DW_OP_shra:
27937d62b00eSchristos 	  ax_simple (expr, aop_rsh_signed);
27947d62b00eSchristos 	  break;
27957d62b00eSchristos 
27967d62b00eSchristos 	case DW_OP_xor:
27977d62b00eSchristos 	  ax_simple (expr, aop_bit_xor);
27987d62b00eSchristos 	  break;
27997d62b00eSchristos 
28007d62b00eSchristos 	case DW_OP_le:
28017d62b00eSchristos 	  /* Sign extend the operands.  */
28027d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28037d62b00eSchristos 	  ax_simple (expr, aop_swap);
28047d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28057d62b00eSchristos 	  /* Note no swap here: A <= B is !(B < A).  */
28067d62b00eSchristos 	  ax_simple (expr, aop_less_signed);
28077d62b00eSchristos 	  ax_simple (expr, aop_log_not);
28087d62b00eSchristos 	  break;
28097d62b00eSchristos 
28107d62b00eSchristos 	case DW_OP_ge:
28117d62b00eSchristos 	  /* Sign extend the operands.  */
28127d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28137d62b00eSchristos 	  ax_simple (expr, aop_swap);
28147d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28157d62b00eSchristos 	  ax_simple (expr, aop_swap);
28167d62b00eSchristos 	  /* A >= B is !(A < B).  */
28177d62b00eSchristos 	  ax_simple (expr, aop_less_signed);
28187d62b00eSchristos 	  ax_simple (expr, aop_log_not);
28197d62b00eSchristos 	  break;
28207d62b00eSchristos 
28217d62b00eSchristos 	case DW_OP_eq:
28227d62b00eSchristos 	  /* Sign extend the operands.  */
28237d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28247d62b00eSchristos 	  ax_simple (expr, aop_swap);
28257d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28267d62b00eSchristos 	  /* No need for a second swap here.  */
28277d62b00eSchristos 	  ax_simple (expr, aop_equal);
28287d62b00eSchristos 	  break;
28297d62b00eSchristos 
28307d62b00eSchristos 	case DW_OP_lt:
28317d62b00eSchristos 	  /* Sign extend the operands.  */
28327d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28337d62b00eSchristos 	  ax_simple (expr, aop_swap);
28347d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28357d62b00eSchristos 	  ax_simple (expr, aop_swap);
28367d62b00eSchristos 	  ax_simple (expr, aop_less_signed);
28377d62b00eSchristos 	  break;
28387d62b00eSchristos 
28397d62b00eSchristos 	case DW_OP_gt:
28407d62b00eSchristos 	  /* Sign extend the operands.  */
28417d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28427d62b00eSchristos 	  ax_simple (expr, aop_swap);
28437d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28447d62b00eSchristos 	  /* Note no swap here: A > B is B < A.  */
28457d62b00eSchristos 	  ax_simple (expr, aop_less_signed);
28467d62b00eSchristos 	  break;
28477d62b00eSchristos 
28487d62b00eSchristos 	case DW_OP_ne:
28497d62b00eSchristos 	  /* Sign extend the operands.  */
28507d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28517d62b00eSchristos 	  ax_simple (expr, aop_swap);
28527d62b00eSchristos 	  ax_ext (expr, addr_size_bits);
28537d62b00eSchristos 	  /* No need for a swap here.  */
28547d62b00eSchristos 	  ax_simple (expr, aop_equal);
28557d62b00eSchristos 	  ax_simple (expr, aop_log_not);
28567d62b00eSchristos 	  break;
28577d62b00eSchristos 
28587d62b00eSchristos 	case DW_OP_call_frame_cfa:
28597d62b00eSchristos 	  {
28607d62b00eSchristos 	    int regnum;
28617d62b00eSchristos 	    CORE_ADDR text_offset;
28627d62b00eSchristos 	    LONGEST off;
28637d62b00eSchristos 	    const gdb_byte *cfa_start, *cfa_end;
28647d62b00eSchristos 
28657d62b00eSchristos 	    if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
28667d62b00eSchristos 				       &regnum, &off,
28677d62b00eSchristos 				       &text_offset, &cfa_start, &cfa_end))
28687d62b00eSchristos 	      {
28697d62b00eSchristos 		/* Register.  */
28707d62b00eSchristos 		ax_reg (expr, regnum);
28717d62b00eSchristos 		if (off != 0)
28727d62b00eSchristos 		  {
28737d62b00eSchristos 		    ax_const_l (expr, off);
28747d62b00eSchristos 		    ax_simple (expr, aop_add);
28757d62b00eSchristos 		  }
28767d62b00eSchristos 	      }
28777d62b00eSchristos 	    else
28787d62b00eSchristos 	      {
28797d62b00eSchristos 		/* Another expression.  */
28807d62b00eSchristos 		ax_const_l (expr, text_offset);
28817d62b00eSchristos 		dwarf2_compile_expr_to_ax (expr, loc, addr_size, cfa_start,
28827d62b00eSchristos 					   cfa_end, per_cu, per_objfile);
28837d62b00eSchristos 	      }
28847d62b00eSchristos 
28857d62b00eSchristos 	    loc->kind = axs_lvalue_memory;
28867d62b00eSchristos 	  }
28877d62b00eSchristos 	  break;
28887d62b00eSchristos 
28897d62b00eSchristos 	case DW_OP_GNU_push_tls_address:
28907d62b00eSchristos 	case DW_OP_form_tls_address:
28917d62b00eSchristos 	  unimplemented (op);
28927d62b00eSchristos 	  break;
28937d62b00eSchristos 
28947d62b00eSchristos 	case DW_OP_push_object_address:
28957d62b00eSchristos 	  unimplemented (op);
28967d62b00eSchristos 	  break;
28977d62b00eSchristos 
28987d62b00eSchristos 	case DW_OP_skip:
28997d62b00eSchristos 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
29007d62b00eSchristos 	  op_ptr += 2;
29017d62b00eSchristos 	  i = ax_goto (expr, aop_goto);
29027d62b00eSchristos 	  dw_labels.push_back (op_ptr + offset - base);
29037d62b00eSchristos 	  patches.push_back (i);
29047d62b00eSchristos 	  break;
29057d62b00eSchristos 
29067d62b00eSchristos 	case DW_OP_bra:
29077d62b00eSchristos 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
29087d62b00eSchristos 	  op_ptr += 2;
29097d62b00eSchristos 	  /* Zero extend the operand.  */
29107d62b00eSchristos 	  ax_zero_ext (expr, addr_size_bits);
29117d62b00eSchristos 	  i = ax_goto (expr, aop_if_goto);
29127d62b00eSchristos 	  dw_labels.push_back (op_ptr + offset - base);
29137d62b00eSchristos 	  patches.push_back (i);
29147d62b00eSchristos 	  break;
29157d62b00eSchristos 
29167d62b00eSchristos 	case DW_OP_nop:
29177d62b00eSchristos 	  break;
29187d62b00eSchristos 
29197d62b00eSchristos 	case DW_OP_piece:
29207d62b00eSchristos 	case DW_OP_bit_piece:
29217d62b00eSchristos 	  {
29227d62b00eSchristos 	    uint64_t size;
29237d62b00eSchristos 
29247d62b00eSchristos 	    if (op_ptr - 1 == previous_piece)
29257d62b00eSchristos 	      error (_("Cannot translate empty pieces to agent expressions"));
29267d62b00eSchristos 	    previous_piece = op_ptr - 1;
29277d62b00eSchristos 
29287d62b00eSchristos 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
29297d62b00eSchristos 	    if (op == DW_OP_piece)
29307d62b00eSchristos 	      {
29317d62b00eSchristos 		size *= 8;
29327d62b00eSchristos 		uoffset = 0;
29337d62b00eSchristos 	      }
29347d62b00eSchristos 	    else
29357d62b00eSchristos 	      op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
29367d62b00eSchristos 
29377d62b00eSchristos 	    if (bits_collected + size > 8 * sizeof (LONGEST))
29387d62b00eSchristos 	      error (_("Expression pieces exceed word size"));
29397d62b00eSchristos 
29407d62b00eSchristos 	    /* Access the bits.  */
29417d62b00eSchristos 	    switch (loc->kind)
29427d62b00eSchristos 	      {
29437d62b00eSchristos 	      case axs_lvalue_register:
29447d62b00eSchristos 		ax_reg (expr, loc->u.reg);
29457d62b00eSchristos 		break;
29467d62b00eSchristos 
29477d62b00eSchristos 	      case axs_lvalue_memory:
29487d62b00eSchristos 		/* Offset the pointer, if needed.  */
29497d62b00eSchristos 		if (uoffset > 8)
29507d62b00eSchristos 		  {
29517d62b00eSchristos 		    ax_const_l (expr, uoffset / 8);
29527d62b00eSchristos 		    ax_simple (expr, aop_add);
29537d62b00eSchristos 		    uoffset %= 8;
29547d62b00eSchristos 		  }
29557d62b00eSchristos 		access_memory (arch, expr, size);
29567d62b00eSchristos 		break;
29577d62b00eSchristos 	      }
29587d62b00eSchristos 
29597d62b00eSchristos 	    /* For a bits-big-endian target, shift up what we already
29607d62b00eSchristos 	       have.  For a bits-little-endian target, shift up the
29617d62b00eSchristos 	       new data.  Note that there is a potential bug here if
29627d62b00eSchristos 	       the DWARF expression leaves multiple values on the
29637d62b00eSchristos 	       stack.  */
29647d62b00eSchristos 	    if (bits_collected > 0)
29657d62b00eSchristos 	      {
29667d62b00eSchristos 		if (bits_big_endian)
29677d62b00eSchristos 		  {
29687d62b00eSchristos 		    ax_simple (expr, aop_swap);
29697d62b00eSchristos 		    ax_const_l (expr, size);
29707d62b00eSchristos 		    ax_simple (expr, aop_lsh);
29717d62b00eSchristos 		    /* We don't need a second swap here, because
29727d62b00eSchristos 		       aop_bit_or is symmetric.  */
29737d62b00eSchristos 		  }
29747d62b00eSchristos 		else
29757d62b00eSchristos 		  {
29767d62b00eSchristos 		    ax_const_l (expr, size);
29777d62b00eSchristos 		    ax_simple (expr, aop_lsh);
29787d62b00eSchristos 		  }
29797d62b00eSchristos 		ax_simple (expr, aop_bit_or);
29807d62b00eSchristos 	      }
29817d62b00eSchristos 
29827d62b00eSchristos 	    bits_collected += size;
29837d62b00eSchristos 	    loc->kind = axs_rvalue;
29847d62b00eSchristos 	  }
29857d62b00eSchristos 	  break;
29867d62b00eSchristos 
29877d62b00eSchristos 	case DW_OP_GNU_uninit:
29887d62b00eSchristos 	  unimplemented (op);
29897d62b00eSchristos 
29907d62b00eSchristos 	case DW_OP_call2:
29917d62b00eSchristos 	case DW_OP_call4:
29927d62b00eSchristos 	  {
29937d62b00eSchristos 	    struct dwarf2_locexpr_baton block;
29947d62b00eSchristos 	    int size = (op == DW_OP_call2 ? 2 : 4);
29957d62b00eSchristos 
29967d62b00eSchristos 	    uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
29977d62b00eSchristos 	    op_ptr += size;
29987d62b00eSchristos 
29997d62b00eSchristos 	    auto get_frame_pc_from_expr = [expr] ()
30007d62b00eSchristos 	      {
30017d62b00eSchristos 		return expr->scope;
30027d62b00eSchristos 	      };
30037d62b00eSchristos 	    cu_offset cuoffset = (cu_offset) uoffset;
30047d62b00eSchristos 	    block = dwarf2_fetch_die_loc_cu_off (cuoffset, per_cu, per_objfile,
30057d62b00eSchristos 						 get_frame_pc_from_expr);
30067d62b00eSchristos 
30077d62b00eSchristos 	    /* DW_OP_call_ref is currently not supported.  */
30087d62b00eSchristos 	    gdb_assert (block.per_cu == per_cu);
30097d62b00eSchristos 
30107d62b00eSchristos 	    dwarf2_compile_expr_to_ax (expr, loc, addr_size, block.data,
30117d62b00eSchristos 				       block.data + block.size, per_cu,
30127d62b00eSchristos 				       per_objfile);
30137d62b00eSchristos 	  }
30147d62b00eSchristos 	  break;
30157d62b00eSchristos 
30167d62b00eSchristos 	case DW_OP_call_ref:
30177d62b00eSchristos 	  unimplemented (op);
30187d62b00eSchristos 
30197d62b00eSchristos 	case DW_OP_GNU_variable_value:
30207d62b00eSchristos 	  unimplemented (op);
30217d62b00eSchristos 
30227d62b00eSchristos 	default:
30237d62b00eSchristos 	  unimplemented (op);
30247d62b00eSchristos 	}
30257d62b00eSchristos     }
30267d62b00eSchristos 
30277d62b00eSchristos   /* Patch all the branches we emitted.  */
30287d62b00eSchristos   for (int i = 0; i < patches.size (); ++i)
30297d62b00eSchristos     {
30307d62b00eSchristos       int targ = offsets[dw_labels[i]];
30317d62b00eSchristos       if (targ == -1)
3032*6881a400Schristos 	internal_error (_("invalid label"));
30337d62b00eSchristos       ax_label (expr, patches[i], targ);
30347d62b00eSchristos     }
30357d62b00eSchristos }
30367d62b00eSchristos 
30377d62b00eSchristos 
30387d62b00eSchristos /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
30397d62b00eSchristos    evaluator to calculate the location.  */
30407d62b00eSchristos static struct value *
3041*6881a400Schristos locexpr_read_variable (struct symbol *symbol, frame_info_ptr frame)
30427d62b00eSchristos {
30437d62b00eSchristos   struct dwarf2_locexpr_baton *dlbaton
30447d62b00eSchristos     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
30457d62b00eSchristos   struct value *val;
30467d62b00eSchristos 
3047*6881a400Schristos   val = dwarf2_evaluate_loc_desc (symbol->type (), frame, dlbaton->data,
30487d62b00eSchristos 				  dlbaton->size, dlbaton->per_cu,
30497d62b00eSchristos 				  dlbaton->per_objfile);
30507d62b00eSchristos 
30517d62b00eSchristos   return val;
30527d62b00eSchristos }
30537d62b00eSchristos 
30547d62b00eSchristos /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
30557d62b00eSchristos    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
30567d62b00eSchristos    will be thrown.  */
30577d62b00eSchristos 
30587d62b00eSchristos static struct value *
3059*6881a400Schristos locexpr_read_variable_at_entry (struct symbol *symbol, frame_info_ptr frame)
30607d62b00eSchristos {
30617d62b00eSchristos   struct dwarf2_locexpr_baton *dlbaton
30627d62b00eSchristos     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
30637d62b00eSchristos 
3064*6881a400Schristos   return value_of_dwarf_block_entry (symbol->type (), frame, dlbaton->data,
30657d62b00eSchristos 				     dlbaton->size);
30667d62b00eSchristos }
30677d62b00eSchristos 
30687d62b00eSchristos /* Implementation of get_symbol_read_needs from
30697d62b00eSchristos    symbol_computed_ops.  */
30707d62b00eSchristos 
30717d62b00eSchristos static enum symbol_needs_kind
30727d62b00eSchristos locexpr_get_symbol_read_needs (struct symbol *symbol)
30737d62b00eSchristos {
30747d62b00eSchristos   struct dwarf2_locexpr_baton *dlbaton
30757d62b00eSchristos     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
30767d62b00eSchristos 
3077*6881a400Schristos   gdbarch *arch = dlbaton->per_objfile->objfile->arch ();
3078*6881a400Schristos   gdb::array_view<const gdb_byte> expr (dlbaton->data, dlbaton->size);
3079*6881a400Schristos 
3080*6881a400Schristos   return dwarf2_get_symbol_read_needs (expr,
30817d62b00eSchristos 				       dlbaton->per_cu,
3082*6881a400Schristos 				       dlbaton->per_objfile,
3083*6881a400Schristos 				       gdbarch_byte_order (arch),
3084*6881a400Schristos 				       dlbaton->per_cu->addr_size (),
3085*6881a400Schristos 				       dlbaton->per_cu->ref_addr_size ());
30867d62b00eSchristos }
30877d62b00eSchristos 
30887d62b00eSchristos /* Return true if DATA points to the end of a piece.  END is one past
30897d62b00eSchristos    the last byte in the expression.  */
30907d62b00eSchristos 
30917d62b00eSchristos static int
30927d62b00eSchristos piece_end_p (const gdb_byte *data, const gdb_byte *end)
30937d62b00eSchristos {
30947d62b00eSchristos   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
30957d62b00eSchristos }
30967d62b00eSchristos 
30977d62b00eSchristos /* Helper for locexpr_describe_location_piece that finds the name of a
30987d62b00eSchristos    DWARF register.  */
30997d62b00eSchristos 
31007d62b00eSchristos static const char *
31017d62b00eSchristos locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
31027d62b00eSchristos {
31037d62b00eSchristos   int regnum;
31047d62b00eSchristos 
31057d62b00eSchristos   /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
31067d62b00eSchristos      We'd rather print *something* here than throw an error.  */
31077d62b00eSchristos   regnum = dwarf_reg_to_regnum (gdbarch, dwarf_regnum);
31087d62b00eSchristos   /* gdbarch_register_name may just return "", return something more
31097d62b00eSchristos      descriptive for bad register numbers.  */
31107d62b00eSchristos   if (regnum == -1)
31117d62b00eSchristos     {
31127d62b00eSchristos       /* The text is output as "$bad_register_number".
31137d62b00eSchristos 	 That is why we use the underscores.  */
31147d62b00eSchristos       return _("bad_register_number");
31157d62b00eSchristos     }
31167d62b00eSchristos   return gdbarch_register_name (gdbarch, regnum);
31177d62b00eSchristos }
31187d62b00eSchristos 
31197d62b00eSchristos /* Nicely describe a single piece of a location, returning an updated
31207d62b00eSchristos    position in the bytecode sequence.  This function cannot recognize
31217d62b00eSchristos    all locations; if a location is not recognized, it simply returns
31227d62b00eSchristos    DATA.  If there is an error during reading, e.g. we run off the end
31237d62b00eSchristos    of the buffer, an error is thrown.  */
31247d62b00eSchristos 
31257d62b00eSchristos static const gdb_byte *
31267d62b00eSchristos locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
31277d62b00eSchristos 				 CORE_ADDR addr, dwarf2_per_cu_data *per_cu,
31287d62b00eSchristos 				 dwarf2_per_objfile *per_objfile,
31297d62b00eSchristos 				 const gdb_byte *data, const gdb_byte *end,
31307d62b00eSchristos 				 unsigned int addr_size)
31317d62b00eSchristos {
31327d62b00eSchristos   objfile *objfile = per_objfile->objfile;
31337d62b00eSchristos   struct gdbarch *gdbarch = objfile->arch ();
31347d62b00eSchristos   size_t leb128_size;
31357d62b00eSchristos 
31367d62b00eSchristos   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
31377d62b00eSchristos     {
3138*6881a400Schristos       gdb_printf (stream, _("a variable in $%s"),
31397d62b00eSchristos 		  locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
31407d62b00eSchristos       data += 1;
31417d62b00eSchristos     }
31427d62b00eSchristos   else if (data[0] == DW_OP_regx)
31437d62b00eSchristos     {
31447d62b00eSchristos       uint64_t reg;
31457d62b00eSchristos 
31467d62b00eSchristos       data = safe_read_uleb128 (data + 1, end, &reg);
3147*6881a400Schristos       gdb_printf (stream, _("a variable in $%s"),
31487d62b00eSchristos 		  locexpr_regname (gdbarch, reg));
31497d62b00eSchristos     }
31507d62b00eSchristos   else if (data[0] == DW_OP_fbreg)
31517d62b00eSchristos     {
31527d62b00eSchristos       const struct block *b;
31537d62b00eSchristos       struct symbol *framefunc;
31547d62b00eSchristos       int frame_reg = 0;
31557d62b00eSchristos       int64_t frame_offset;
31567d62b00eSchristos       const gdb_byte *base_data, *new_data, *save_data = data;
31577d62b00eSchristos       size_t base_size;
31587d62b00eSchristos       int64_t base_offset = 0;
31597d62b00eSchristos 
31607d62b00eSchristos       new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
31617d62b00eSchristos       if (!piece_end_p (new_data, end))
31627d62b00eSchristos 	return data;
31637d62b00eSchristos       data = new_data;
31647d62b00eSchristos 
31657d62b00eSchristos       b = block_for_pc (addr);
31667d62b00eSchristos 
31677d62b00eSchristos       if (!b)
31687d62b00eSchristos 	error (_("No block found for address for symbol \"%s\"."),
31697d62b00eSchristos 	       symbol->print_name ());
31707d62b00eSchristos 
31717d62b00eSchristos       framefunc = block_linkage_function (b);
31727d62b00eSchristos 
31737d62b00eSchristos       if (!framefunc)
31747d62b00eSchristos 	error (_("No function found for block for symbol \"%s\"."),
31757d62b00eSchristos 	       symbol->print_name ());
31767d62b00eSchristos 
31777d62b00eSchristos       func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
31787d62b00eSchristos 
31797d62b00eSchristos       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
31807d62b00eSchristos 	{
31817d62b00eSchristos 	  const gdb_byte *buf_end;
31827d62b00eSchristos 
31837d62b00eSchristos 	  frame_reg = base_data[0] - DW_OP_breg0;
31847d62b00eSchristos 	  buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
31857d62b00eSchristos 				       &base_offset);
31867d62b00eSchristos 	  if (buf_end != base_data + base_size)
31877d62b00eSchristos 	    error (_("Unexpected opcode after "
31887d62b00eSchristos 		     "DW_OP_breg%u for symbol \"%s\"."),
31897d62b00eSchristos 		   frame_reg, symbol->print_name ());
31907d62b00eSchristos 	}
31917d62b00eSchristos       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
31927d62b00eSchristos 	{
31937d62b00eSchristos 	  /* The frame base is just the register, with no offset.  */
31947d62b00eSchristos 	  frame_reg = base_data[0] - DW_OP_reg0;
31957d62b00eSchristos 	  base_offset = 0;
31967d62b00eSchristos 	}
31977d62b00eSchristos       else
31987d62b00eSchristos 	{
31997d62b00eSchristos 	  /* We don't know what to do with the frame base expression,
32007d62b00eSchristos 	     so we can't trace this variable; give up.  */
32017d62b00eSchristos 	  return save_data;
32027d62b00eSchristos 	}
32037d62b00eSchristos 
3204*6881a400Schristos       gdb_printf (stream,
32057d62b00eSchristos 		  _("a variable at frame base reg $%s offset %s+%s"),
32067d62b00eSchristos 		  locexpr_regname (gdbarch, frame_reg),
32077d62b00eSchristos 		  plongest (base_offset), plongest (frame_offset));
32087d62b00eSchristos     }
32097d62b00eSchristos   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
32107d62b00eSchristos 	   && piece_end_p (data, end))
32117d62b00eSchristos     {
32127d62b00eSchristos       int64_t offset;
32137d62b00eSchristos 
32147d62b00eSchristos       data = safe_read_sleb128 (data + 1, end, &offset);
32157d62b00eSchristos 
3216*6881a400Schristos       gdb_printf (stream,
32177d62b00eSchristos 		  _("a variable at offset %s from base reg $%s"),
32187d62b00eSchristos 		  plongest (offset),
32197d62b00eSchristos 		  locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
32207d62b00eSchristos     }
32217d62b00eSchristos 
32227d62b00eSchristos   /* The location expression for a TLS variable looks like this (on a
32237d62b00eSchristos      64-bit LE machine):
32247d62b00eSchristos 
32257d62b00eSchristos      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
32267d62b00eSchristos 			(DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
32277d62b00eSchristos 
32287d62b00eSchristos      0x3 is the encoding for DW_OP_addr, which has an operand as long
32297d62b00eSchristos      as the size of an address on the target machine (here is 8
32307d62b00eSchristos      bytes).  Note that more recent version of GCC emit DW_OP_const4u
32317d62b00eSchristos      or DW_OP_const8u, depending on address size, rather than
32327d62b00eSchristos      DW_OP_addr.  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
32337d62b00eSchristos      The operand represents the offset at which the variable is within
32347d62b00eSchristos      the thread local storage.  */
32357d62b00eSchristos 
32367d62b00eSchristos   else if (data + 1 + addr_size < end
32377d62b00eSchristos 	   && (data[0] == DW_OP_addr
32387d62b00eSchristos 	       || (addr_size == 4 && data[0] == DW_OP_const4u)
32397d62b00eSchristos 	       || (addr_size == 8 && data[0] == DW_OP_const8u))
32407d62b00eSchristos 	   && (data[1 + addr_size] == DW_OP_GNU_push_tls_address
32417d62b00eSchristos 	       || data[1 + addr_size] == DW_OP_form_tls_address)
32427d62b00eSchristos 	   && piece_end_p (data + 2 + addr_size, end))
32437d62b00eSchristos     {
32447d62b00eSchristos       ULONGEST offset;
32457d62b00eSchristos       offset = extract_unsigned_integer (data + 1, addr_size,
32467d62b00eSchristos 					 gdbarch_byte_order (gdbarch));
32477d62b00eSchristos 
3248*6881a400Schristos       gdb_printf (stream,
32497d62b00eSchristos 		  _("a thread-local variable at offset 0x%s "
32507d62b00eSchristos 		    "in the thread-local storage for `%s'"),
32517d62b00eSchristos 		  phex_nz (offset, addr_size), objfile_name (objfile));
32527d62b00eSchristos 
32537d62b00eSchristos       data += 1 + addr_size + 1;
32547d62b00eSchristos     }
32557d62b00eSchristos 
32567d62b00eSchristos   /* With -gsplit-dwarf a TLS variable can also look like this:
32577d62b00eSchristos      DW_AT_location    : 3 byte block: fc 4 e0
32587d62b00eSchristos 			(DW_OP_GNU_const_index: 4;
32597d62b00eSchristos 			 DW_OP_GNU_push_tls_address)  */
32607d62b00eSchristos   else if (data + 3 <= end
32617d62b00eSchristos 	   && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
32627d62b00eSchristos 	   && data[0] == DW_OP_GNU_const_index
32637d62b00eSchristos 	   && leb128_size > 0
32647d62b00eSchristos 	   && (data[1 + leb128_size] == DW_OP_GNU_push_tls_address
32657d62b00eSchristos 	       || data[1 + leb128_size] == DW_OP_form_tls_address)
32667d62b00eSchristos 	   && piece_end_p (data + 2 + leb128_size, end))
32677d62b00eSchristos     {
32687d62b00eSchristos       uint64_t offset;
32697d62b00eSchristos 
32707d62b00eSchristos       data = safe_read_uleb128 (data + 1, end, &offset);
32717d62b00eSchristos       offset = dwarf2_read_addr_index (per_cu, per_objfile, offset);
3272*6881a400Schristos       gdb_printf (stream,
32737d62b00eSchristos 		  _("a thread-local variable at offset 0x%s "
32747d62b00eSchristos 		    "in the thread-local storage for `%s'"),
32757d62b00eSchristos 		  phex_nz (offset, addr_size), objfile_name (objfile));
32767d62b00eSchristos       ++data;
32777d62b00eSchristos     }
32787d62b00eSchristos 
32797d62b00eSchristos   else if (data[0] >= DW_OP_lit0
32807d62b00eSchristos 	   && data[0] <= DW_OP_lit31
32817d62b00eSchristos 	   && data + 1 < end
32827d62b00eSchristos 	   && data[1] == DW_OP_stack_value)
32837d62b00eSchristos     {
3284*6881a400Schristos       gdb_printf (stream, _("the constant %d"), data[0] - DW_OP_lit0);
32857d62b00eSchristos       data += 2;
32867d62b00eSchristos     }
32877d62b00eSchristos 
32887d62b00eSchristos   return data;
32897d62b00eSchristos }
32907d62b00eSchristos 
32917d62b00eSchristos /* Disassemble an expression, stopping at the end of a piece or at the
32927d62b00eSchristos    end of the expression.  Returns a pointer to the next unread byte
32937d62b00eSchristos    in the input expression.  If ALL is nonzero, then this function
32947d62b00eSchristos    will keep going until it reaches the end of the expression.
32957d62b00eSchristos    If there is an error during reading, e.g. we run off the end
32967d62b00eSchristos    of the buffer, an error is thrown.  */
32977d62b00eSchristos 
32987d62b00eSchristos static const gdb_byte *
32997d62b00eSchristos disassemble_dwarf_expression (struct ui_file *stream,
33007d62b00eSchristos 			      struct gdbarch *arch, unsigned int addr_size,
33017d62b00eSchristos 			      int offset_size, const gdb_byte *start,
33027d62b00eSchristos 			      const gdb_byte *data, const gdb_byte *end,
33037d62b00eSchristos 			      int indent, int all,
33047d62b00eSchristos 			      dwarf2_per_cu_data *per_cu,
33057d62b00eSchristos 			      dwarf2_per_objfile *per_objfile)
33067d62b00eSchristos {
33077d62b00eSchristos   while (data < end
33087d62b00eSchristos 	 && (all
33097d62b00eSchristos 	     || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
33107d62b00eSchristos     {
33117d62b00eSchristos       enum dwarf_location_atom op = (enum dwarf_location_atom) *data++;
33127d62b00eSchristos       uint64_t ul;
33137d62b00eSchristos       int64_t l;
33147d62b00eSchristos       const char *name;
33157d62b00eSchristos 
33167d62b00eSchristos       name = get_DW_OP_name (op);
33177d62b00eSchristos 
33187d62b00eSchristos       if (!name)
33197d62b00eSchristos 	error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
33207d62b00eSchristos 	       op, (long) (data - 1 - start));
3321*6881a400Schristos       gdb_printf (stream, "  %*ld: %s", indent + 4,
33227d62b00eSchristos 		  (long) (data - 1 - start), name);
33237d62b00eSchristos 
33247d62b00eSchristos       switch (op)
33257d62b00eSchristos 	{
33267d62b00eSchristos 	case DW_OP_addr:
33277d62b00eSchristos 	  ul = extract_unsigned_integer (data, addr_size,
33287d62b00eSchristos 					 gdbarch_byte_order (arch));
33297d62b00eSchristos 	  data += addr_size;
3330*6881a400Schristos 	  gdb_printf (stream, " 0x%s", phex_nz (ul, addr_size));
33317d62b00eSchristos 	  break;
33327d62b00eSchristos 
33337d62b00eSchristos 	case DW_OP_const1u:
33347d62b00eSchristos 	  ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
33357d62b00eSchristos 	  data += 1;
3336*6881a400Schristos 	  gdb_printf (stream, " %s", pulongest (ul));
33377d62b00eSchristos 	  break;
33387d62b00eSchristos 
33397d62b00eSchristos 	case DW_OP_const1s:
33407d62b00eSchristos 	  l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
33417d62b00eSchristos 	  data += 1;
3342*6881a400Schristos 	  gdb_printf (stream, " %s", plongest (l));
33437d62b00eSchristos 	  break;
33447d62b00eSchristos 
33457d62b00eSchristos 	case DW_OP_const2u:
33467d62b00eSchristos 	  ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
33477d62b00eSchristos 	  data += 2;
3348*6881a400Schristos 	  gdb_printf (stream, " %s", pulongest (ul));
33497d62b00eSchristos 	  break;
33507d62b00eSchristos 
33517d62b00eSchristos 	case DW_OP_const2s:
33527d62b00eSchristos 	  l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
33537d62b00eSchristos 	  data += 2;
3354*6881a400Schristos 	  gdb_printf (stream, " %s", plongest (l));
33557d62b00eSchristos 	  break;
33567d62b00eSchristos 
33577d62b00eSchristos 	case DW_OP_const4u:
33587d62b00eSchristos 	  ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
33597d62b00eSchristos 	  data += 4;
3360*6881a400Schristos 	  gdb_printf (stream, " %s", pulongest (ul));
33617d62b00eSchristos 	  break;
33627d62b00eSchristos 
33637d62b00eSchristos 	case DW_OP_const4s:
33647d62b00eSchristos 	  l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
33657d62b00eSchristos 	  data += 4;
3366*6881a400Schristos 	  gdb_printf (stream, " %s", plongest (l));
33677d62b00eSchristos 	  break;
33687d62b00eSchristos 
33697d62b00eSchristos 	case DW_OP_const8u:
33707d62b00eSchristos 	  ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
33717d62b00eSchristos 	  data += 8;
3372*6881a400Schristos 	  gdb_printf (stream, " %s", pulongest (ul));
33737d62b00eSchristos 	  break;
33747d62b00eSchristos 
33757d62b00eSchristos 	case DW_OP_const8s:
33767d62b00eSchristos 	  l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
33777d62b00eSchristos 	  data += 8;
3378*6881a400Schristos 	  gdb_printf (stream, " %s", plongest (l));
33797d62b00eSchristos 	  break;
33807d62b00eSchristos 
33817d62b00eSchristos 	case DW_OP_constu:
33827d62b00eSchristos 	  data = safe_read_uleb128 (data, end, &ul);
3383*6881a400Schristos 	  gdb_printf (stream, " %s", pulongest (ul));
33847d62b00eSchristos 	  break;
33857d62b00eSchristos 
33867d62b00eSchristos 	case DW_OP_consts:
33877d62b00eSchristos 	  data = safe_read_sleb128 (data, end, &l);
3388*6881a400Schristos 	  gdb_printf (stream, " %s", plongest (l));
33897d62b00eSchristos 	  break;
33907d62b00eSchristos 
33917d62b00eSchristos 	case DW_OP_reg0:
33927d62b00eSchristos 	case DW_OP_reg1:
33937d62b00eSchristos 	case DW_OP_reg2:
33947d62b00eSchristos 	case DW_OP_reg3:
33957d62b00eSchristos 	case DW_OP_reg4:
33967d62b00eSchristos 	case DW_OP_reg5:
33977d62b00eSchristos 	case DW_OP_reg6:
33987d62b00eSchristos 	case DW_OP_reg7:
33997d62b00eSchristos 	case DW_OP_reg8:
34007d62b00eSchristos 	case DW_OP_reg9:
34017d62b00eSchristos 	case DW_OP_reg10:
34027d62b00eSchristos 	case DW_OP_reg11:
34037d62b00eSchristos 	case DW_OP_reg12:
34047d62b00eSchristos 	case DW_OP_reg13:
34057d62b00eSchristos 	case DW_OP_reg14:
34067d62b00eSchristos 	case DW_OP_reg15:
34077d62b00eSchristos 	case DW_OP_reg16:
34087d62b00eSchristos 	case DW_OP_reg17:
34097d62b00eSchristos 	case DW_OP_reg18:
34107d62b00eSchristos 	case DW_OP_reg19:
34117d62b00eSchristos 	case DW_OP_reg20:
34127d62b00eSchristos 	case DW_OP_reg21:
34137d62b00eSchristos 	case DW_OP_reg22:
34147d62b00eSchristos 	case DW_OP_reg23:
34157d62b00eSchristos 	case DW_OP_reg24:
34167d62b00eSchristos 	case DW_OP_reg25:
34177d62b00eSchristos 	case DW_OP_reg26:
34187d62b00eSchristos 	case DW_OP_reg27:
34197d62b00eSchristos 	case DW_OP_reg28:
34207d62b00eSchristos 	case DW_OP_reg29:
34217d62b00eSchristos 	case DW_OP_reg30:
34227d62b00eSchristos 	case DW_OP_reg31:
3423*6881a400Schristos 	  gdb_printf (stream, " [$%s]",
34247d62b00eSchristos 		      locexpr_regname (arch, op - DW_OP_reg0));
34257d62b00eSchristos 	  break;
34267d62b00eSchristos 
34277d62b00eSchristos 	case DW_OP_regx:
34287d62b00eSchristos 	  data = safe_read_uleb128 (data, end, &ul);
3429*6881a400Schristos 	  gdb_printf (stream, " %s [$%s]", pulongest (ul),
34307d62b00eSchristos 		      locexpr_regname (arch, (int) ul));
34317d62b00eSchristos 	  break;
34327d62b00eSchristos 
34337d62b00eSchristos 	case DW_OP_implicit_value:
34347d62b00eSchristos 	  data = safe_read_uleb128 (data, end, &ul);
34357d62b00eSchristos 	  data += ul;
3436*6881a400Schristos 	  gdb_printf (stream, " %s", pulongest (ul));
34377d62b00eSchristos 	  break;
34387d62b00eSchristos 
34397d62b00eSchristos 	case DW_OP_breg0:
34407d62b00eSchristos 	case DW_OP_breg1:
34417d62b00eSchristos 	case DW_OP_breg2:
34427d62b00eSchristos 	case DW_OP_breg3:
34437d62b00eSchristos 	case DW_OP_breg4:
34447d62b00eSchristos 	case DW_OP_breg5:
34457d62b00eSchristos 	case DW_OP_breg6:
34467d62b00eSchristos 	case DW_OP_breg7:
34477d62b00eSchristos 	case DW_OP_breg8:
34487d62b00eSchristos 	case DW_OP_breg9:
34497d62b00eSchristos 	case DW_OP_breg10:
34507d62b00eSchristos 	case DW_OP_breg11:
34517d62b00eSchristos 	case DW_OP_breg12:
34527d62b00eSchristos 	case DW_OP_breg13:
34537d62b00eSchristos 	case DW_OP_breg14:
34547d62b00eSchristos 	case DW_OP_breg15:
34557d62b00eSchristos 	case DW_OP_breg16:
34567d62b00eSchristos 	case DW_OP_breg17:
34577d62b00eSchristos 	case DW_OP_breg18:
34587d62b00eSchristos 	case DW_OP_breg19:
34597d62b00eSchristos 	case DW_OP_breg20:
34607d62b00eSchristos 	case DW_OP_breg21:
34617d62b00eSchristos 	case DW_OP_breg22:
34627d62b00eSchristos 	case DW_OP_breg23:
34637d62b00eSchristos 	case DW_OP_breg24:
34647d62b00eSchristos 	case DW_OP_breg25:
34657d62b00eSchristos 	case DW_OP_breg26:
34667d62b00eSchristos 	case DW_OP_breg27:
34677d62b00eSchristos 	case DW_OP_breg28:
34687d62b00eSchristos 	case DW_OP_breg29:
34697d62b00eSchristos 	case DW_OP_breg30:
34707d62b00eSchristos 	case DW_OP_breg31:
34717d62b00eSchristos 	  data = safe_read_sleb128 (data, end, &l);
3472*6881a400Schristos 	  gdb_printf (stream, " %s [$%s]", plongest (l),
34737d62b00eSchristos 		      locexpr_regname (arch, op - DW_OP_breg0));
34747d62b00eSchristos 	  break;
34757d62b00eSchristos 
34767d62b00eSchristos 	case DW_OP_bregx:
34777d62b00eSchristos 	  data = safe_read_uleb128 (data, end, &ul);
34787d62b00eSchristos 	  data = safe_read_sleb128 (data, end, &l);
3479*6881a400Schristos 	  gdb_printf (stream, " register %s [$%s] offset %s",
34807d62b00eSchristos 		      pulongest (ul),
34817d62b00eSchristos 		      locexpr_regname (arch, (int) ul),
34827d62b00eSchristos 		      plongest (l));
34837d62b00eSchristos 	  break;
34847d62b00eSchristos 
34857d62b00eSchristos 	case DW_OP_fbreg:
34867d62b00eSchristos 	  data = safe_read_sleb128 (data, end, &l);
3487*6881a400Schristos 	  gdb_printf (stream, " %s", plongest (l));
34887d62b00eSchristos 	  break;
34897d62b00eSchristos 
34907d62b00eSchristos 	case DW_OP_xderef_size:
34917d62b00eSchristos 	case DW_OP_deref_size:
34927d62b00eSchristos 	case DW_OP_pick:
3493*6881a400Schristos 	  gdb_printf (stream, " %d", *data);
34947d62b00eSchristos 	  ++data;
34957d62b00eSchristos 	  break;
34967d62b00eSchristos 
34977d62b00eSchristos 	case DW_OP_plus_uconst:
34987d62b00eSchristos 	  data = safe_read_uleb128 (data, end, &ul);
3499*6881a400Schristos 	  gdb_printf (stream, " %s", pulongest (ul));
35007d62b00eSchristos 	  break;
35017d62b00eSchristos 
35027d62b00eSchristos 	case DW_OP_skip:
35037d62b00eSchristos 	  l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
35047d62b00eSchristos 	  data += 2;
3505*6881a400Schristos 	  gdb_printf (stream, " to %ld",
35067d62b00eSchristos 		      (long) (data + l - start));
35077d62b00eSchristos 	  break;
35087d62b00eSchristos 
35097d62b00eSchristos 	case DW_OP_bra:
35107d62b00eSchristos 	  l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
35117d62b00eSchristos 	  data += 2;
3512*6881a400Schristos 	  gdb_printf (stream, " %ld",
35137d62b00eSchristos 		      (long) (data + l - start));
35147d62b00eSchristos 	  break;
35157d62b00eSchristos 
35167d62b00eSchristos 	case DW_OP_call2:
35177d62b00eSchristos 	  ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
35187d62b00eSchristos 	  data += 2;
3519*6881a400Schristos 	  gdb_printf (stream, " offset %s", phex_nz (ul, 2));
35207d62b00eSchristos 	  break;
35217d62b00eSchristos 
35227d62b00eSchristos 	case DW_OP_call4:
35237d62b00eSchristos 	  ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
35247d62b00eSchristos 	  data += 4;
3525*6881a400Schristos 	  gdb_printf (stream, " offset %s", phex_nz (ul, 4));
35267d62b00eSchristos 	  break;
35277d62b00eSchristos 
35287d62b00eSchristos 	case DW_OP_call_ref:
35297d62b00eSchristos 	  ul = extract_unsigned_integer (data, offset_size,
35307d62b00eSchristos 					 gdbarch_byte_order (arch));
35317d62b00eSchristos 	  data += offset_size;
3532*6881a400Schristos 	  gdb_printf (stream, " offset %s", phex_nz (ul, offset_size));
35337d62b00eSchristos 	  break;
35347d62b00eSchristos 
35357d62b00eSchristos 	case DW_OP_piece:
35367d62b00eSchristos 	  data = safe_read_uleb128 (data, end, &ul);
3537*6881a400Schristos 	  gdb_printf (stream, " %s (bytes)", pulongest (ul));
35387d62b00eSchristos 	  break;
35397d62b00eSchristos 
35407d62b00eSchristos 	case DW_OP_bit_piece:
35417d62b00eSchristos 	  {
35427d62b00eSchristos 	    uint64_t offset;
35437d62b00eSchristos 
35447d62b00eSchristos 	    data = safe_read_uleb128 (data, end, &ul);
35457d62b00eSchristos 	    data = safe_read_uleb128 (data, end, &offset);
3546*6881a400Schristos 	    gdb_printf (stream, " size %s offset %s (bits)",
35477d62b00eSchristos 			pulongest (ul), pulongest (offset));
35487d62b00eSchristos 	  }
35497d62b00eSchristos 	  break;
35507d62b00eSchristos 
35517d62b00eSchristos 	case DW_OP_implicit_pointer:
35527d62b00eSchristos 	case DW_OP_GNU_implicit_pointer:
35537d62b00eSchristos 	  {
35547d62b00eSchristos 	    ul = extract_unsigned_integer (data, offset_size,
35557d62b00eSchristos 					   gdbarch_byte_order (arch));
35567d62b00eSchristos 	    data += offset_size;
35577d62b00eSchristos 
35587d62b00eSchristos 	    data = safe_read_sleb128 (data, end, &l);
35597d62b00eSchristos 
3560*6881a400Schristos 	    gdb_printf (stream, " DIE %s offset %s",
35617d62b00eSchristos 			phex_nz (ul, offset_size),
35627d62b00eSchristos 			plongest (l));
35637d62b00eSchristos 	  }
35647d62b00eSchristos 	  break;
35657d62b00eSchristos 
35667d62b00eSchristos 	case DW_OP_deref_type:
35677d62b00eSchristos 	case DW_OP_GNU_deref_type:
35687d62b00eSchristos 	  {
35697d62b00eSchristos 	    int deref_addr_size = *data++;
35707d62b00eSchristos 	    struct type *type;
35717d62b00eSchristos 
35727d62b00eSchristos 	    data = safe_read_uleb128 (data, end, &ul);
35737d62b00eSchristos 	    cu_offset offset = (cu_offset) ul;
35747d62b00eSchristos 	    type = dwarf2_get_die_type (offset, per_cu, per_objfile);
3575*6881a400Schristos 	    gdb_printf (stream, "<");
35767d62b00eSchristos 	    type_print (type, "", stream, -1);
3577*6881a400Schristos 	    gdb_printf (stream, " [0x%s]> %d",
35787d62b00eSchristos 			phex_nz (to_underlying (offset), 0),
35797d62b00eSchristos 			deref_addr_size);
35807d62b00eSchristos 	  }
35817d62b00eSchristos 	  break;
35827d62b00eSchristos 
35837d62b00eSchristos 	case DW_OP_const_type:
35847d62b00eSchristos 	case DW_OP_GNU_const_type:
35857d62b00eSchristos 	  {
35867d62b00eSchristos 	    struct type *type;
35877d62b00eSchristos 
35887d62b00eSchristos 	    data = safe_read_uleb128 (data, end, &ul);
35897d62b00eSchristos 	    cu_offset type_die = (cu_offset) ul;
35907d62b00eSchristos 	    type = dwarf2_get_die_type (type_die, per_cu, per_objfile);
3591*6881a400Schristos 	    gdb_printf (stream, "<");
35927d62b00eSchristos 	    type_print (type, "", stream, -1);
3593*6881a400Schristos 	    gdb_printf (stream, " [0x%s]>",
35947d62b00eSchristos 			phex_nz (to_underlying (type_die), 0));
35957d62b00eSchristos 
35967d62b00eSchristos 	    int n = *data++;
3597*6881a400Schristos 	    gdb_printf (stream, " %d byte block:", n);
35987d62b00eSchristos 	    for (int i = 0; i < n; ++i)
3599*6881a400Schristos 	      gdb_printf (stream, " %02x", data[i]);
36007d62b00eSchristos 	    data += n;
36017d62b00eSchristos 	  }
36027d62b00eSchristos 	  break;
36037d62b00eSchristos 
36047d62b00eSchristos 	case DW_OP_regval_type:
36057d62b00eSchristos 	case DW_OP_GNU_regval_type:
36067d62b00eSchristos 	  {
36077d62b00eSchristos 	    uint64_t reg;
36087d62b00eSchristos 	    struct type *type;
36097d62b00eSchristos 
36107d62b00eSchristos 	    data = safe_read_uleb128 (data, end, &reg);
36117d62b00eSchristos 	    data = safe_read_uleb128 (data, end, &ul);
36127d62b00eSchristos 	    cu_offset type_die = (cu_offset) ul;
36137d62b00eSchristos 
36147d62b00eSchristos 	    type = dwarf2_get_die_type (type_die, per_cu, per_objfile);
3615*6881a400Schristos 	    gdb_printf (stream, "<");
36167d62b00eSchristos 	    type_print (type, "", stream, -1);
3617*6881a400Schristos 	    gdb_printf (stream, " [0x%s]> [$%s]",
36187d62b00eSchristos 			phex_nz (to_underlying (type_die), 0),
36197d62b00eSchristos 			locexpr_regname (arch, reg));
36207d62b00eSchristos 	  }
36217d62b00eSchristos 	  break;
36227d62b00eSchristos 
36237d62b00eSchristos 	case DW_OP_convert:
36247d62b00eSchristos 	case DW_OP_GNU_convert:
36257d62b00eSchristos 	case DW_OP_reinterpret:
36267d62b00eSchristos 	case DW_OP_GNU_reinterpret:
36277d62b00eSchristos 	  {
36287d62b00eSchristos 	    data = safe_read_uleb128 (data, end, &ul);
36297d62b00eSchristos 	    cu_offset type_die = (cu_offset) ul;
36307d62b00eSchristos 
36317d62b00eSchristos 	    if (to_underlying (type_die) == 0)
3632*6881a400Schristos 	      gdb_printf (stream, "<0>");
36337d62b00eSchristos 	    else
36347d62b00eSchristos 	      {
36357d62b00eSchristos 		struct type *type;
36367d62b00eSchristos 
36377d62b00eSchristos 		type = dwarf2_get_die_type (type_die, per_cu, per_objfile);
3638*6881a400Schristos 		gdb_printf (stream, "<");
36397d62b00eSchristos 		type_print (type, "", stream, -1);
3640*6881a400Schristos 		gdb_printf (stream, " [0x%s]>",
36417d62b00eSchristos 			    phex_nz (to_underlying (type_die), 0));
36427d62b00eSchristos 	      }
36437d62b00eSchristos 	  }
36447d62b00eSchristos 	  break;
36457d62b00eSchristos 
36467d62b00eSchristos 	case DW_OP_entry_value:
36477d62b00eSchristos 	case DW_OP_GNU_entry_value:
36487d62b00eSchristos 	  data = safe_read_uleb128 (data, end, &ul);
3649*6881a400Schristos 	  gdb_putc ('\n', stream);
36507d62b00eSchristos 	  disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
36517d62b00eSchristos 					start, data, data + ul, indent + 2,
36527d62b00eSchristos 					all, per_cu, per_objfile);
36537d62b00eSchristos 	  data += ul;
36547d62b00eSchristos 	  continue;
36557d62b00eSchristos 
36567d62b00eSchristos 	case DW_OP_GNU_parameter_ref:
36577d62b00eSchristos 	  ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
36587d62b00eSchristos 	  data += 4;
3659*6881a400Schristos 	  gdb_printf (stream, " offset %s", phex_nz (ul, 4));
36607d62b00eSchristos 	  break;
36617d62b00eSchristos 
36627d62b00eSchristos 	case DW_OP_addrx:
36637d62b00eSchristos 	case DW_OP_GNU_addr_index:
36647d62b00eSchristos 	  data = safe_read_uleb128 (data, end, &ul);
36657d62b00eSchristos 	  ul = dwarf2_read_addr_index (per_cu, per_objfile, ul);
3666*6881a400Schristos 	  gdb_printf (stream, " 0x%s", phex_nz (ul, addr_size));
36677d62b00eSchristos 	  break;
36687d62b00eSchristos 
36697d62b00eSchristos 	case DW_OP_GNU_const_index:
36707d62b00eSchristos 	  data = safe_read_uleb128 (data, end, &ul);
36717d62b00eSchristos 	  ul = dwarf2_read_addr_index (per_cu, per_objfile, ul);
3672*6881a400Schristos 	  gdb_printf (stream, " %s", pulongest (ul));
36737d62b00eSchristos 	  break;
36747d62b00eSchristos 
36757d62b00eSchristos 	case DW_OP_GNU_variable_value:
36767d62b00eSchristos 	  ul = extract_unsigned_integer (data, offset_size,
36777d62b00eSchristos 					 gdbarch_byte_order (arch));
36787d62b00eSchristos 	  data += offset_size;
3679*6881a400Schristos 	  gdb_printf (stream, " offset %s", phex_nz (ul, offset_size));
36807d62b00eSchristos 	  break;
36817d62b00eSchristos 	}
36827d62b00eSchristos 
3683*6881a400Schristos       gdb_printf (stream, "\n");
36847d62b00eSchristos     }
36857d62b00eSchristos 
36867d62b00eSchristos   return data;
36877d62b00eSchristos }
36887d62b00eSchristos 
36897d62b00eSchristos static bool dwarf_always_disassemble;
36907d62b00eSchristos 
36917d62b00eSchristos static void
36927d62b00eSchristos show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
36937d62b00eSchristos 			       struct cmd_list_element *c, const char *value)
36947d62b00eSchristos {
3695*6881a400Schristos   gdb_printf (file,
36967d62b00eSchristos 	      _("Whether to always disassemble "
36977d62b00eSchristos 		"DWARF expressions is %s.\n"),
36987d62b00eSchristos 	      value);
36997d62b00eSchristos }
37007d62b00eSchristos 
37017d62b00eSchristos /* Describe a single location, which may in turn consist of multiple
37027d62b00eSchristos    pieces.  */
37037d62b00eSchristos 
37047d62b00eSchristos static void
37057d62b00eSchristos locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
37067d62b00eSchristos 			     struct ui_file *stream,
37077d62b00eSchristos 			     const gdb_byte *data, size_t size,
37087d62b00eSchristos 			     unsigned int addr_size,
37097d62b00eSchristos 			     int offset_size, dwarf2_per_cu_data *per_cu,
37107d62b00eSchristos 			     dwarf2_per_objfile *per_objfile)
37117d62b00eSchristos {
37127d62b00eSchristos   const gdb_byte *end = data + size;
37137d62b00eSchristos   int first_piece = 1, bad = 0;
37147d62b00eSchristos   objfile *objfile = per_objfile->objfile;
37157d62b00eSchristos 
37167d62b00eSchristos   while (data < end)
37177d62b00eSchristos     {
37187d62b00eSchristos       const gdb_byte *here = data;
37197d62b00eSchristos       int disassemble = 1;
37207d62b00eSchristos 
37217d62b00eSchristos       if (first_piece)
37227d62b00eSchristos 	first_piece = 0;
37237d62b00eSchristos       else
3724*6881a400Schristos 	gdb_printf (stream, _(", and "));
37257d62b00eSchristos 
37267d62b00eSchristos       if (!dwarf_always_disassemble)
37277d62b00eSchristos 	{
37287d62b00eSchristos 	  data = locexpr_describe_location_piece (symbol, stream,
37297d62b00eSchristos 						  addr, per_cu, per_objfile,
37307d62b00eSchristos 						  data, end, addr_size);
37317d62b00eSchristos 	  /* If we printed anything, or if we have an empty piece,
37327d62b00eSchristos 	     then don't disassemble.  */
37337d62b00eSchristos 	  if (data != here
37347d62b00eSchristos 	      || data[0] == DW_OP_piece
37357d62b00eSchristos 	      || data[0] == DW_OP_bit_piece)
37367d62b00eSchristos 	    disassemble = 0;
37377d62b00eSchristos 	}
37387d62b00eSchristos       if (disassemble)
37397d62b00eSchristos 	{
3740*6881a400Schristos 	  gdb_printf (stream, _("a complex DWARF expression:\n"));
37417d62b00eSchristos 	  data = disassemble_dwarf_expression (stream,
37427d62b00eSchristos 					       objfile->arch (),
37437d62b00eSchristos 					       addr_size, offset_size, data,
37447d62b00eSchristos 					       data, end, 0,
37457d62b00eSchristos 					       dwarf_always_disassemble,
37467d62b00eSchristos 					       per_cu, per_objfile);
37477d62b00eSchristos 	}
37487d62b00eSchristos 
37497d62b00eSchristos       if (data < end)
37507d62b00eSchristos 	{
37517d62b00eSchristos 	  int empty = data == here;
37527d62b00eSchristos 
37537d62b00eSchristos 	  if (disassemble)
3754*6881a400Schristos 	    gdb_printf (stream, "   ");
37557d62b00eSchristos 	  if (data[0] == DW_OP_piece)
37567d62b00eSchristos 	    {
37577d62b00eSchristos 	      uint64_t bytes;
37587d62b00eSchristos 
37597d62b00eSchristos 	      data = safe_read_uleb128 (data + 1, end, &bytes);
37607d62b00eSchristos 
37617d62b00eSchristos 	      if (empty)
3762*6881a400Schristos 		gdb_printf (stream, _("an empty %s-byte piece"),
37637d62b00eSchristos 			    pulongest (bytes));
37647d62b00eSchristos 	      else
3765*6881a400Schristos 		gdb_printf (stream, _(" [%s-byte piece]"),
37667d62b00eSchristos 			    pulongest (bytes));
37677d62b00eSchristos 	    }
37687d62b00eSchristos 	  else if (data[0] == DW_OP_bit_piece)
37697d62b00eSchristos 	    {
37707d62b00eSchristos 	      uint64_t bits, offset;
37717d62b00eSchristos 
37727d62b00eSchristos 	      data = safe_read_uleb128 (data + 1, end, &bits);
37737d62b00eSchristos 	      data = safe_read_uleb128 (data, end, &offset);
37747d62b00eSchristos 
37757d62b00eSchristos 	      if (empty)
3776*6881a400Schristos 		gdb_printf (stream,
37777d62b00eSchristos 			    _("an empty %s-bit piece"),
37787d62b00eSchristos 			    pulongest (bits));
37797d62b00eSchristos 	      else
3780*6881a400Schristos 		gdb_printf (stream,
37817d62b00eSchristos 			    _(" [%s-bit piece, offset %s bits]"),
37827d62b00eSchristos 			    pulongest (bits), pulongest (offset));
37837d62b00eSchristos 	    }
37847d62b00eSchristos 	  else
37857d62b00eSchristos 	    {
37867d62b00eSchristos 	      bad = 1;
37877d62b00eSchristos 	      break;
37887d62b00eSchristos 	    }
37897d62b00eSchristos 	}
37907d62b00eSchristos     }
37917d62b00eSchristos 
37927d62b00eSchristos   if (bad || data > end)
37937d62b00eSchristos     error (_("Corrupted DWARF2 expression for \"%s\"."),
37947d62b00eSchristos 	   symbol->print_name ());
37957d62b00eSchristos }
37967d62b00eSchristos 
37977d62b00eSchristos /* Print a natural-language description of SYMBOL to STREAM.  This
37987d62b00eSchristos    version is for a symbol with a single location.  */
37997d62b00eSchristos 
38007d62b00eSchristos static void
38017d62b00eSchristos locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
38027d62b00eSchristos 			   struct ui_file *stream)
38037d62b00eSchristos {
38047d62b00eSchristos   struct dwarf2_locexpr_baton *dlbaton
38057d62b00eSchristos     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
38067d62b00eSchristos   unsigned int addr_size = dlbaton->per_cu->addr_size ();
38077d62b00eSchristos   int offset_size = dlbaton->per_cu->offset_size ();
38087d62b00eSchristos 
38097d62b00eSchristos   locexpr_describe_location_1 (symbol, addr, stream,
38107d62b00eSchristos 			       dlbaton->data, dlbaton->size,
38117d62b00eSchristos 			       addr_size, offset_size,
38127d62b00eSchristos 			       dlbaton->per_cu, dlbaton->per_objfile);
38137d62b00eSchristos }
38147d62b00eSchristos 
38157d62b00eSchristos /* Describe the location of SYMBOL as an agent value in VALUE, generating
38167d62b00eSchristos    any necessary bytecode in AX.  */
38177d62b00eSchristos 
38187d62b00eSchristos static void
38197d62b00eSchristos locexpr_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
38207d62b00eSchristos 			    struct axs_value *value)
38217d62b00eSchristos {
38227d62b00eSchristos   struct dwarf2_locexpr_baton *dlbaton
38237d62b00eSchristos     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
38247d62b00eSchristos   unsigned int addr_size = dlbaton->per_cu->addr_size ();
38257d62b00eSchristos 
38267d62b00eSchristos   if (dlbaton->size == 0)
38277d62b00eSchristos     value->optimized_out = 1;
38287d62b00eSchristos   else
38297d62b00eSchristos     dwarf2_compile_expr_to_ax (ax, value, addr_size, dlbaton->data,
38307d62b00eSchristos 			       dlbaton->data + dlbaton->size, dlbaton->per_cu,
38317d62b00eSchristos 			       dlbaton->per_objfile);
38327d62b00eSchristos }
38337d62b00eSchristos 
38347d62b00eSchristos /* symbol_computed_ops 'generate_c_location' method.  */
38357d62b00eSchristos 
38367d62b00eSchristos static void
38377d62b00eSchristos locexpr_generate_c_location (struct symbol *sym, string_file *stream,
38387d62b00eSchristos 			     struct gdbarch *gdbarch,
3839*6881a400Schristos 			     std::vector<bool> &registers_used,
38407d62b00eSchristos 			     CORE_ADDR pc, const char *result_name)
38417d62b00eSchristos {
38427d62b00eSchristos   struct dwarf2_locexpr_baton *dlbaton
38437d62b00eSchristos     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
38447d62b00eSchristos   unsigned int addr_size = dlbaton->per_cu->addr_size ();
38457d62b00eSchristos 
38467d62b00eSchristos   if (dlbaton->size == 0)
38477d62b00eSchristos     error (_("symbol \"%s\" is optimized out"), sym->natural_name ());
38487d62b00eSchristos 
38497d62b00eSchristos   compile_dwarf_expr_to_c (stream, result_name,
38507d62b00eSchristos 			   sym, pc, gdbarch, registers_used, addr_size,
38517d62b00eSchristos 			   dlbaton->data, dlbaton->data + dlbaton->size,
38527d62b00eSchristos 			   dlbaton->per_cu, dlbaton->per_objfile);
38537d62b00eSchristos }
38547d62b00eSchristos 
38557d62b00eSchristos /* The set of location functions used with the DWARF-2 expression
38567d62b00eSchristos    evaluator.  */
38577d62b00eSchristos const struct symbol_computed_ops dwarf2_locexpr_funcs = {
38587d62b00eSchristos   locexpr_read_variable,
38597d62b00eSchristos   locexpr_read_variable_at_entry,
38607d62b00eSchristos   locexpr_get_symbol_read_needs,
38617d62b00eSchristos   locexpr_describe_location,
38627d62b00eSchristos   0,	/* location_has_loclist */
38637d62b00eSchristos   locexpr_tracepoint_var_ref,
38647d62b00eSchristos   locexpr_generate_c_location
38657d62b00eSchristos };
38667d62b00eSchristos 
38677d62b00eSchristos 
38687d62b00eSchristos /* Wrapper functions for location lists.  These generally find
38697d62b00eSchristos    the appropriate location expression and call something above.  */
38707d62b00eSchristos 
38717d62b00eSchristos /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
38727d62b00eSchristos    evaluator to calculate the location.  */
38737d62b00eSchristos static struct value *
3874*6881a400Schristos loclist_read_variable (struct symbol *symbol, frame_info_ptr frame)
38757d62b00eSchristos {
38767d62b00eSchristos   struct dwarf2_loclist_baton *dlbaton
38777d62b00eSchristos     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
38787d62b00eSchristos   struct value *val;
38797d62b00eSchristos   const gdb_byte *data;
38807d62b00eSchristos   size_t size;
38817d62b00eSchristos   CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
38827d62b00eSchristos 
38837d62b00eSchristos   data = dwarf2_find_location_expression (dlbaton, &size, pc);
3884*6881a400Schristos   val = dwarf2_evaluate_loc_desc (symbol->type (), frame, data, size,
38857d62b00eSchristos 				  dlbaton->per_cu, dlbaton->per_objfile);
38867d62b00eSchristos 
38877d62b00eSchristos   return val;
38887d62b00eSchristos }
38897d62b00eSchristos 
38907d62b00eSchristos /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
38917d62b00eSchristos    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
38927d62b00eSchristos    will be thrown.
38937d62b00eSchristos 
38947d62b00eSchristos    Function always returns non-NULL value, it may be marked optimized out if
38957d62b00eSchristos    inferior frame information is not available.  It throws NO_ENTRY_VALUE_ERROR
38967d62b00eSchristos    if it cannot resolve the parameter for any reason.  */
38977d62b00eSchristos 
38987d62b00eSchristos static struct value *
3899*6881a400Schristos loclist_read_variable_at_entry (struct symbol *symbol, frame_info_ptr frame)
39007d62b00eSchristos {
39017d62b00eSchristos   struct dwarf2_loclist_baton *dlbaton
39027d62b00eSchristos     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
39037d62b00eSchristos   const gdb_byte *data;
39047d62b00eSchristos   size_t size;
39057d62b00eSchristos   CORE_ADDR pc;
39067d62b00eSchristos 
39077d62b00eSchristos   if (frame == NULL || !get_frame_func_if_available (frame, &pc))
3908*6881a400Schristos     return allocate_optimized_out_value (symbol->type ());
39097d62b00eSchristos 
39107d62b00eSchristos   data = dwarf2_find_location_expression (dlbaton, &size, pc);
39117d62b00eSchristos   if (data == NULL)
3912*6881a400Schristos     return allocate_optimized_out_value (symbol->type ());
39137d62b00eSchristos 
3914*6881a400Schristos   return value_of_dwarf_block_entry (symbol->type (), frame, data, size);
39157d62b00eSchristos }
39167d62b00eSchristos 
39177d62b00eSchristos /* Implementation of get_symbol_read_needs from
39187d62b00eSchristos    symbol_computed_ops.  */
39197d62b00eSchristos 
39207d62b00eSchristos static enum symbol_needs_kind
39217d62b00eSchristos loclist_symbol_needs (struct symbol *symbol)
39227d62b00eSchristos {
39237d62b00eSchristos   /* If there's a location list, then assume we need to have a frame
39247d62b00eSchristos      to choose the appropriate location expression.  With tracking of
39257d62b00eSchristos      global variables this is not necessarily true, but such tracking
39267d62b00eSchristos      is disabled in GCC at the moment until we figure out how to
39277d62b00eSchristos      represent it.  */
39287d62b00eSchristos 
39297d62b00eSchristos   return SYMBOL_NEEDS_FRAME;
39307d62b00eSchristos }
39317d62b00eSchristos 
39327d62b00eSchristos /* Print a natural-language description of SYMBOL to STREAM.  This
39337d62b00eSchristos    version applies when there is a list of different locations, each
39347d62b00eSchristos    with a specified address range.  */
39357d62b00eSchristos 
39367d62b00eSchristos static void
39377d62b00eSchristos loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
39387d62b00eSchristos 			   struct ui_file *stream)
39397d62b00eSchristos {
39407d62b00eSchristos   struct dwarf2_loclist_baton *dlbaton
39417d62b00eSchristos     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
39427d62b00eSchristos   const gdb_byte *loc_ptr, *buf_end;
39437d62b00eSchristos   dwarf2_per_objfile *per_objfile = dlbaton->per_objfile;
39447d62b00eSchristos   struct objfile *objfile = per_objfile->objfile;
39457d62b00eSchristos   struct gdbarch *gdbarch = objfile->arch ();
39467d62b00eSchristos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
39477d62b00eSchristos   unsigned int addr_size = dlbaton->per_cu->addr_size ();
39487d62b00eSchristos   int offset_size = dlbaton->per_cu->offset_size ();
3949*6881a400Schristos   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd.get ());
3950*6881a400Schristos   /* Adjustment for relocatable objects.  */
3951*6881a400Schristos   CORE_ADDR text_offset = objfile->text_section_offset ();
3952*6881a400Schristos   CORE_ADDR base_address = dlbaton->base_address;
39537d62b00eSchristos   int done = 0;
39547d62b00eSchristos 
39557d62b00eSchristos   loc_ptr = dlbaton->data;
39567d62b00eSchristos   buf_end = dlbaton->data + dlbaton->size;
39577d62b00eSchristos 
3958*6881a400Schristos   gdb_printf (stream, _("multi-location:\n"));
39597d62b00eSchristos 
39607d62b00eSchristos   /* Iterate through locations until we run out.  */
39617d62b00eSchristos   while (!done)
39627d62b00eSchristos     {
39637d62b00eSchristos       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
39647d62b00eSchristos       int length;
39657d62b00eSchristos       enum debug_loc_kind kind;
39667d62b00eSchristos       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
39677d62b00eSchristos 
39687d62b00eSchristos       if (dlbaton->per_cu->version () < 5 && dlbaton->from_dwo)
39697d62b00eSchristos 	kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
39707d62b00eSchristos 					       dlbaton->per_objfile,
39717d62b00eSchristos 					       loc_ptr, buf_end, &new_ptr,
39727d62b00eSchristos 					       &low, &high, byte_order);
39737d62b00eSchristos       else if (dlbaton->per_cu->version () < 5)
39747d62b00eSchristos 	kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
39757d62b00eSchristos 					   &low, &high,
39767d62b00eSchristos 					   byte_order, addr_size,
39777d62b00eSchristos 					   signed_addr_p);
39787d62b00eSchristos       else
39797d62b00eSchristos 	kind = decode_debug_loclists_addresses (dlbaton->per_cu,
39807d62b00eSchristos 						dlbaton->per_objfile,
39817d62b00eSchristos 						loc_ptr, buf_end, &new_ptr,
39827d62b00eSchristos 						&low, &high, byte_order,
39837d62b00eSchristos 						addr_size, signed_addr_p);
39847d62b00eSchristos       loc_ptr = new_ptr;
39857d62b00eSchristos       switch (kind)
39867d62b00eSchristos 	{
39877d62b00eSchristos 	case DEBUG_LOC_END_OF_LIST:
39887d62b00eSchristos 	  done = 1;
39897d62b00eSchristos 	  continue;
39907d62b00eSchristos 
39917d62b00eSchristos 	case DEBUG_LOC_BASE_ADDRESS:
3992*6881a400Schristos 	  base_address = high;
3993*6881a400Schristos 	  gdb_printf (stream, _("  Base address %s"),
39947d62b00eSchristos 		      paddress (gdbarch, base_address));
39957d62b00eSchristos 	  continue;
39967d62b00eSchristos 
39977d62b00eSchristos 	case DEBUG_LOC_START_END:
39987d62b00eSchristos 	case DEBUG_LOC_START_LENGTH:
39997d62b00eSchristos 	case DEBUG_LOC_OFFSET_PAIR:
40007d62b00eSchristos 	  break;
40017d62b00eSchristos 
40027d62b00eSchristos 	case DEBUG_LOC_BUFFER_OVERFLOW:
40037d62b00eSchristos 	case DEBUG_LOC_INVALID_ENTRY:
40047d62b00eSchristos 	  error (_("Corrupted DWARF expression for symbol \"%s\"."),
40057d62b00eSchristos 		 symbol->print_name ());
40067d62b00eSchristos 
40077d62b00eSchristos 	default:
40087d62b00eSchristos 	  gdb_assert_not_reached ("bad debug_loc_kind");
40097d62b00eSchristos 	}
40107d62b00eSchristos 
40117d62b00eSchristos       /* Otherwise, a location expression entry.  */
4012*6881a400Schristos       low += text_offset;
4013*6881a400Schristos       high += text_offset;
4014*6881a400Schristos       if (!dlbaton->from_dwo && kind == DEBUG_LOC_OFFSET_PAIR)
4015*6881a400Schristos 	{
40167d62b00eSchristos 	  low += base_address;
40177d62b00eSchristos 	  high += base_address;
4018*6881a400Schristos 	}
40197d62b00eSchristos 
40207d62b00eSchristos       low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
40217d62b00eSchristos       high = gdbarch_adjust_dwarf2_addr (gdbarch, high);
40227d62b00eSchristos 
40237d62b00eSchristos       if (dlbaton->per_cu->version () < 5)
40247d62b00eSchristos 	 {
40257d62b00eSchristos 	   length = extract_unsigned_integer (loc_ptr, 2, byte_order);
40267d62b00eSchristos 	   loc_ptr += 2;
40277d62b00eSchristos 	 }
40287d62b00eSchristos       else
40297d62b00eSchristos 	 {
40307d62b00eSchristos 	   unsigned int bytes_read;
40317d62b00eSchristos 	   length = read_unsigned_leb128 (NULL, loc_ptr, &bytes_read);
40327d62b00eSchristos 	   loc_ptr += bytes_read;
40337d62b00eSchristos 	 }
40347d62b00eSchristos 
40357d62b00eSchristos       /* (It would improve readability to print only the minimum
40367d62b00eSchristos 	 necessary digits of the second number of the range.)  */
4037*6881a400Schristos       gdb_printf (stream, _("  Range %s-%s: "),
40387d62b00eSchristos 		  paddress (gdbarch, low), paddress (gdbarch, high));
40397d62b00eSchristos 
40407d62b00eSchristos       /* Now describe this particular location.  */
40417d62b00eSchristos       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
40427d62b00eSchristos 				   addr_size, offset_size,
40437d62b00eSchristos 				   dlbaton->per_cu, dlbaton->per_objfile);
40447d62b00eSchristos 
4045*6881a400Schristos       gdb_printf (stream, "\n");
40467d62b00eSchristos 
40477d62b00eSchristos       loc_ptr += length;
40487d62b00eSchristos     }
40497d62b00eSchristos }
40507d62b00eSchristos 
40517d62b00eSchristos /* Describe the location of SYMBOL as an agent value in VALUE, generating
40527d62b00eSchristos    any necessary bytecode in AX.  */
40537d62b00eSchristos static void
40547d62b00eSchristos loclist_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
40557d62b00eSchristos 			    struct axs_value *value)
40567d62b00eSchristos {
40577d62b00eSchristos   struct dwarf2_loclist_baton *dlbaton
40587d62b00eSchristos     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
40597d62b00eSchristos   const gdb_byte *data;
40607d62b00eSchristos   size_t size;
40617d62b00eSchristos   unsigned int addr_size = dlbaton->per_cu->addr_size ();
40627d62b00eSchristos 
40637d62b00eSchristos   data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
40647d62b00eSchristos   if (size == 0)
40657d62b00eSchristos     value->optimized_out = 1;
40667d62b00eSchristos   else
40677d62b00eSchristos     dwarf2_compile_expr_to_ax (ax, value, addr_size, data, data + size,
40687d62b00eSchristos 			       dlbaton->per_cu, dlbaton->per_objfile);
40697d62b00eSchristos }
40707d62b00eSchristos 
40717d62b00eSchristos /* symbol_computed_ops 'generate_c_location' method.  */
40727d62b00eSchristos 
40737d62b00eSchristos static void
40747d62b00eSchristos loclist_generate_c_location (struct symbol *sym, string_file *stream,
40757d62b00eSchristos 			     struct gdbarch *gdbarch,
4076*6881a400Schristos 			     std::vector<bool> &registers_used,
40777d62b00eSchristos 			     CORE_ADDR pc, const char *result_name)
40787d62b00eSchristos {
40797d62b00eSchristos   struct dwarf2_loclist_baton *dlbaton
40807d62b00eSchristos     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
40817d62b00eSchristos   unsigned int addr_size = dlbaton->per_cu->addr_size ();
40827d62b00eSchristos   const gdb_byte *data;
40837d62b00eSchristos   size_t size;
40847d62b00eSchristos 
40857d62b00eSchristos   data = dwarf2_find_location_expression (dlbaton, &size, pc);
40867d62b00eSchristos   if (size == 0)
40877d62b00eSchristos     error (_("symbol \"%s\" is optimized out"), sym->natural_name ());
40887d62b00eSchristos 
40897d62b00eSchristos   compile_dwarf_expr_to_c (stream, result_name,
40907d62b00eSchristos 			   sym, pc, gdbarch, registers_used, addr_size,
40917d62b00eSchristos 			   data, data + size,
40927d62b00eSchristos 			   dlbaton->per_cu,
40937d62b00eSchristos 			   dlbaton->per_objfile);
40947d62b00eSchristos }
40957d62b00eSchristos 
40967d62b00eSchristos /* The set of location functions used with the DWARF-2 expression
40977d62b00eSchristos    evaluator and location lists.  */
40987d62b00eSchristos const struct symbol_computed_ops dwarf2_loclist_funcs = {
40997d62b00eSchristos   loclist_read_variable,
41007d62b00eSchristos   loclist_read_variable_at_entry,
41017d62b00eSchristos   loclist_symbol_needs,
41027d62b00eSchristos   loclist_describe_location,
41037d62b00eSchristos   1,	/* location_has_loclist */
41047d62b00eSchristos   loclist_tracepoint_var_ref,
41057d62b00eSchristos   loclist_generate_c_location
41067d62b00eSchristos };
41077d62b00eSchristos 
41087d62b00eSchristos void _initialize_dwarf2loc ();
41097d62b00eSchristos void
41107d62b00eSchristos _initialize_dwarf2loc ()
41117d62b00eSchristos {
41127d62b00eSchristos   add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
41137d62b00eSchristos 			     &entry_values_debug,
41147d62b00eSchristos 			     _("Set entry values and tail call frames "
41157d62b00eSchristos 			       "debugging."),
41167d62b00eSchristos 			     _("Show entry values and tail call frames "
41177d62b00eSchristos 			       "debugging."),
41187d62b00eSchristos 			     _("When non-zero, the process of determining "
41197d62b00eSchristos 			       "parameter values from function entry point "
41207d62b00eSchristos 			       "and tail call frames will be printed."),
41217d62b00eSchristos 			     NULL,
41227d62b00eSchristos 			     show_entry_values_debug,
41237d62b00eSchristos 			     &setdebuglist, &showdebuglist);
41247d62b00eSchristos 
41257d62b00eSchristos   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
41267d62b00eSchristos 			   &dwarf_always_disassemble, _("\
41277d62b00eSchristos Set whether `info address' always disassembles DWARF expressions."), _("\
41287d62b00eSchristos Show whether `info address' always disassembles DWARF expressions."), _("\
41297d62b00eSchristos When enabled, DWARF expressions are always printed in an assembly-like\n\
41307d62b00eSchristos syntax.  When disabled, expressions will be printed in a more\n\
41317d62b00eSchristos conversational style, when possible."),
41327d62b00eSchristos 			   NULL,
41337d62b00eSchristos 			   show_dwarf_always_disassemble,
41347d62b00eSchristos 			   &set_dwarf_cmdlist,
41357d62b00eSchristos 			   &show_dwarf_cmdlist);
41367d62b00eSchristos }
4137