xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/dwarf2/loc.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /* DWARF 2 location expression support for GDB.
2 
3    Copyright (C) 2003-2020 Free Software Foundation, Inc.
4 
5    Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
6 
7    This file is part of GDB.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21 
22 #include "defs.h"
23 #include "ui-out.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "inferior.h"
29 #include "ax.h"
30 #include "ax-gdb.h"
31 #include "regcache.h"
32 #include "objfiles.h"
33 #include "block.h"
34 #include "gdbcmd.h"
35 #include "complaints.h"
36 #include "dwarf2.h"
37 #include "dwarf2/expr.h"
38 #include "dwarf2/loc.h"
39 #include "dwarf2/read.h"
40 #include "dwarf2/frame.h"
41 #include "dwarf2/leb.h"
42 #include "compile/compile.h"
43 #include "gdbsupport/selftest.h"
44 #include <algorithm>
45 #include <vector>
46 #include <unordered_set>
47 #include "gdbsupport/underlying.h"
48 #include "gdbsupport/byte-vector.h"
49 
50 static struct value *dwarf2_evaluate_loc_desc_full
51   (struct type *type, struct frame_info *frame, const gdb_byte *data,
52    size_t size, dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
53    struct type *subobj_type, LONGEST subobj_byte_offset);
54 
55 static struct call_site_parameter *dwarf_expr_reg_to_entry_parameter
56     (struct frame_info *frame,
57      enum call_site_parameter_kind kind,
58      union call_site_parameter_u kind_u,
59      dwarf2_per_cu_data **per_cu_return,
60      dwarf2_per_objfile **per_objfile_return);
61 
62 static struct value *indirect_synthetic_pointer
63   (sect_offset die, LONGEST byte_offset,
64    dwarf2_per_cu_data *per_cu,
65    dwarf2_per_objfile *per_objfile,
66    struct frame_info *frame,
67    struct type *type, bool resolve_abstract_p = false);
68 
69 /* Until these have formal names, we define these here.
70    ref: http://gcc.gnu.org/wiki/DebugFission
71    Each entry in .debug_loc.dwo begins with a byte that describes the entry,
72    and is then followed by data specific to that entry.  */
73 
74 enum debug_loc_kind
75 {
76   /* Indicates the end of the list of entries.  */
77   DEBUG_LOC_END_OF_LIST = 0,
78 
79   /* This is followed by an unsigned LEB128 number that is an index into
80      .debug_addr and specifies the base address for all following entries.  */
81   DEBUG_LOC_BASE_ADDRESS = 1,
82 
83   /* This is followed by two unsigned LEB128 numbers that are indices into
84      .debug_addr and specify the beginning and ending addresses, and then
85      a normal location expression as in .debug_loc.  */
86   DEBUG_LOC_START_END = 2,
87 
88   /* This is followed by an unsigned LEB128 number that is an index into
89      .debug_addr and specifies the beginning address, and a 4 byte unsigned
90      number that specifies the length, and then a normal location expression
91      as in .debug_loc.  */
92   DEBUG_LOC_START_LENGTH = 3,
93 
94   /* This is followed by two unsigned LEB128 operands. The values of these
95      operands are the starting and ending offsets, respectively, relative to
96      the applicable base address.  */
97   DEBUG_LOC_OFFSET_PAIR = 4,
98 
99   /* An internal value indicating there is insufficient data.  */
100   DEBUG_LOC_BUFFER_OVERFLOW = -1,
101 
102   /* An internal value indicating an invalid kind of entry was found.  */
103   DEBUG_LOC_INVALID_ENTRY = -2
104 };
105 
106 /* Helper function which throws an error if a synthetic pointer is
107    invalid.  */
108 
109 static void
110 invalid_synthetic_pointer (void)
111 {
112   error (_("access outside bounds of object "
113 	   "referenced via synthetic pointer"));
114 }
115 
116 /* Decode the addresses in a non-dwo .debug_loc entry.
117    A pointer to the next byte to examine is returned in *NEW_PTR.
118    The encoded low,high addresses are return in *LOW,*HIGH.
119    The result indicates the kind of entry found.  */
120 
121 static enum debug_loc_kind
122 decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
123 			    const gdb_byte **new_ptr,
124 			    CORE_ADDR *low, CORE_ADDR *high,
125 			    enum bfd_endian byte_order,
126 			    unsigned int addr_size,
127 			    int signed_addr_p)
128 {
129   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
130 
131   if (buf_end - loc_ptr < 2 * addr_size)
132     return DEBUG_LOC_BUFFER_OVERFLOW;
133 
134   if (signed_addr_p)
135     *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
136   else
137     *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
138   loc_ptr += addr_size;
139 
140   if (signed_addr_p)
141     *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
142   else
143     *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
144   loc_ptr += addr_size;
145 
146   *new_ptr = loc_ptr;
147 
148   /* A base-address-selection entry.  */
149   if ((*low & base_mask) == base_mask)
150     return DEBUG_LOC_BASE_ADDRESS;
151 
152   /* An end-of-list entry.  */
153   if (*low == 0 && *high == 0)
154     return DEBUG_LOC_END_OF_LIST;
155 
156   return DEBUG_LOC_START_END;
157 }
158 
159 /* Decode the addresses in .debug_loclists entry.
160    A pointer to the next byte to examine is returned in *NEW_PTR.
161    The encoded low,high addresses are return in *LOW,*HIGH.
162    The result indicates the kind of entry found.  */
163 
164 static enum debug_loc_kind
165 decode_debug_loclists_addresses (dwarf2_per_cu_data *per_cu,
166 				 dwarf2_per_objfile *per_objfile,
167 				 const gdb_byte *loc_ptr,
168 				 const gdb_byte *buf_end,
169 				 const gdb_byte **new_ptr,
170 				 CORE_ADDR *low, CORE_ADDR *high,
171 				 enum bfd_endian byte_order,
172 				 unsigned int addr_size,
173 				 int signed_addr_p)
174 {
175   uint64_t u64;
176 
177   if (loc_ptr == buf_end)
178     return DEBUG_LOC_BUFFER_OVERFLOW;
179 
180   switch (*loc_ptr++)
181     {
182     case DW_LLE_base_addressx:
183       *low = 0;
184       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
185       if (loc_ptr == NULL)
186 	 return DEBUG_LOC_BUFFER_OVERFLOW;
187 
188       *high = dwarf2_read_addr_index (per_cu, per_objfile, u64);
189       *new_ptr = loc_ptr;
190       return DEBUG_LOC_BASE_ADDRESS;
191 
192     case DW_LLE_startx_length:
193       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
194       if (loc_ptr == NULL)
195 	 return DEBUG_LOC_BUFFER_OVERFLOW;
196 
197       *low = dwarf2_read_addr_index (per_cu, per_objfile, u64);
198       *high = *low;
199       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
200       if (loc_ptr == NULL)
201 	 return DEBUG_LOC_BUFFER_OVERFLOW;
202 
203       *high += u64;
204       *new_ptr = loc_ptr;
205       return DEBUG_LOC_START_LENGTH;
206 
207     case DW_LLE_start_length:
208       if (buf_end - loc_ptr < addr_size)
209 	 return DEBUG_LOC_BUFFER_OVERFLOW;
210 
211       if (signed_addr_p)
212 	 *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
213       else
214 	 *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
215 
216       loc_ptr += addr_size;
217       *high = *low;
218 
219       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
220       if (loc_ptr == NULL)
221 	 return DEBUG_LOC_BUFFER_OVERFLOW;
222 
223       *high += u64;
224       *new_ptr = loc_ptr;
225       return DEBUG_LOC_START_LENGTH;
226 
227     case DW_LLE_end_of_list:
228       *new_ptr = loc_ptr;
229       return DEBUG_LOC_END_OF_LIST;
230 
231     case DW_LLE_base_address:
232       if (loc_ptr + addr_size > buf_end)
233 	return DEBUG_LOC_BUFFER_OVERFLOW;
234 
235       if (signed_addr_p)
236 	*high = extract_signed_integer (loc_ptr, addr_size, byte_order);
237       else
238 	*high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
239 
240       loc_ptr += addr_size;
241       *new_ptr = loc_ptr;
242       return DEBUG_LOC_BASE_ADDRESS;
243 
244     case DW_LLE_offset_pair:
245       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
246       if (loc_ptr == NULL)
247 	return DEBUG_LOC_BUFFER_OVERFLOW;
248 
249       *low = u64;
250       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
251       if (loc_ptr == NULL)
252 	return DEBUG_LOC_BUFFER_OVERFLOW;
253 
254       *high = u64;
255       *new_ptr = loc_ptr;
256       return DEBUG_LOC_OFFSET_PAIR;
257 
258     /* Following cases are not supported yet.  */
259     case DW_LLE_startx_endx:
260     case DW_LLE_start_end:
261     case DW_LLE_default_location:
262     default:
263       return DEBUG_LOC_INVALID_ENTRY;
264     }
265 }
266 
267 /* Decode the addresses in .debug_loc.dwo entry.
268    A pointer to the next byte to examine is returned in *NEW_PTR.
269    The encoded low,high addresses are return in *LOW,*HIGH.
270    The result indicates the kind of entry found.  */
271 
272 static enum debug_loc_kind
273 decode_debug_loc_dwo_addresses (dwarf2_per_cu_data *per_cu,
274 				dwarf2_per_objfile *per_objfile,
275 				const gdb_byte *loc_ptr,
276 				const gdb_byte *buf_end,
277 				const gdb_byte **new_ptr,
278 				CORE_ADDR *low, CORE_ADDR *high,
279 				enum bfd_endian byte_order)
280 {
281   uint64_t low_index, high_index;
282 
283   if (loc_ptr == buf_end)
284     return DEBUG_LOC_BUFFER_OVERFLOW;
285 
286   switch (*loc_ptr++)
287     {
288     case DW_LLE_GNU_end_of_list_entry:
289       *new_ptr = loc_ptr;
290       return DEBUG_LOC_END_OF_LIST;
291 
292     case DW_LLE_GNU_base_address_selection_entry:
293       *low = 0;
294       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
295       if (loc_ptr == NULL)
296 	return DEBUG_LOC_BUFFER_OVERFLOW;
297 
298       *high = dwarf2_read_addr_index (per_cu, per_objfile, high_index);
299       *new_ptr = loc_ptr;
300       return DEBUG_LOC_BASE_ADDRESS;
301 
302     case DW_LLE_GNU_start_end_entry:
303       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
304       if (loc_ptr == NULL)
305 	return DEBUG_LOC_BUFFER_OVERFLOW;
306 
307       *low = dwarf2_read_addr_index (per_cu, per_objfile, low_index);
308       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
309       if (loc_ptr == NULL)
310 	return DEBUG_LOC_BUFFER_OVERFLOW;
311 
312       *high = dwarf2_read_addr_index (per_cu, per_objfile, high_index);
313       *new_ptr = loc_ptr;
314       return DEBUG_LOC_START_END;
315 
316     case DW_LLE_GNU_start_length_entry:
317       loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
318       if (loc_ptr == NULL)
319 	return DEBUG_LOC_BUFFER_OVERFLOW;
320 
321       *low = dwarf2_read_addr_index (per_cu, per_objfile, low_index);
322       if (loc_ptr + 4 > buf_end)
323 	return DEBUG_LOC_BUFFER_OVERFLOW;
324 
325       *high = *low;
326       *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
327       *new_ptr = loc_ptr + 4;
328       return DEBUG_LOC_START_LENGTH;
329 
330     default:
331       return DEBUG_LOC_INVALID_ENTRY;
332     }
333 }
334 
335 /* A function for dealing with location lists.  Given a
336    symbol baton (BATON) and a pc value (PC), find the appropriate
337    location expression, set *LOCEXPR_LENGTH, and return a pointer
338    to the beginning of the expression.  Returns NULL on failure.
339 
340    For now, only return the first matching location expression; there
341    can be more than one in the list.  */
342 
343 const gdb_byte *
344 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
345 				 size_t *locexpr_length, CORE_ADDR pc)
346 {
347   dwarf2_per_objfile *per_objfile = baton->per_objfile;
348   struct objfile *objfile = per_objfile->objfile;
349   struct gdbarch *gdbarch = objfile->arch ();
350   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
351   unsigned int addr_size = baton->per_cu->addr_size ();
352   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
353   /* Adjust base_address for relocatable objects.  */
354   CORE_ADDR base_offset = baton->per_objfile->objfile->text_section_offset ();
355   CORE_ADDR base_address = baton->base_address + base_offset;
356   const gdb_byte *loc_ptr, *buf_end;
357 
358   loc_ptr = baton->data;
359   buf_end = baton->data + baton->size;
360 
361   while (1)
362     {
363       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
364       int length;
365       enum debug_loc_kind kind;
366       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
367 
368       if (baton->per_cu->version () < 5 && baton->from_dwo)
369 	kind = decode_debug_loc_dwo_addresses (baton->per_cu,
370 					       baton->per_objfile,
371 					       loc_ptr, buf_end, &new_ptr,
372 					       &low, &high, byte_order);
373       else if (baton->per_cu->version () < 5)
374 	kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
375 					   &low, &high,
376 					   byte_order, addr_size,
377 					   signed_addr_p);
378       else
379 	kind = decode_debug_loclists_addresses (baton->per_cu,
380 						baton->per_objfile,
381 						loc_ptr, buf_end, &new_ptr,
382 						&low, &high, byte_order,
383 						addr_size, signed_addr_p);
384 
385       loc_ptr = new_ptr;
386       switch (kind)
387 	{
388 	case DEBUG_LOC_END_OF_LIST:
389 	  *locexpr_length = 0;
390 	  return NULL;
391 
392 	case DEBUG_LOC_BASE_ADDRESS:
393 	  base_address = high + base_offset;
394 	  continue;
395 
396 	case DEBUG_LOC_START_END:
397 	case DEBUG_LOC_START_LENGTH:
398 	case DEBUG_LOC_OFFSET_PAIR:
399 	  break;
400 
401 	case DEBUG_LOC_BUFFER_OVERFLOW:
402 	case DEBUG_LOC_INVALID_ENTRY:
403 	  error (_("dwarf2_find_location_expression: "
404 		   "Corrupted DWARF expression."));
405 
406 	default:
407 	  gdb_assert_not_reached ("bad debug_loc_kind");
408 	}
409 
410       /* Otherwise, a location expression entry.
411 	 If the entry is from a DWO, don't add base address: the entry is from
412 	 .debug_addr which already has the DWARF "base address". We still add
413 	 base_offset in case we're debugging a PIE executable. However, if the
414 	 entry is DW_LLE_offset_pair from a DWO, add the base address as the
415 	 operands are offsets relative to the applicable base address.  */
416       if (baton->from_dwo && kind != DEBUG_LOC_OFFSET_PAIR)
417 	{
418 	  low += base_offset;
419 	  high += base_offset;
420 	}
421       else
422 	{
423 	  low += base_address;
424 	  high += base_address;
425 	}
426 
427       if (baton->per_cu->version () < 5)
428 	{
429 	  length = extract_unsigned_integer (loc_ptr, 2, byte_order);
430 	  loc_ptr += 2;
431 	}
432       else
433 	{
434 	  unsigned int bytes_read;
435 
436 	  length = read_unsigned_leb128 (NULL, loc_ptr, &bytes_read);
437 	  loc_ptr += bytes_read;
438 	}
439 
440       if (low == high && pc == low)
441 	{
442 	  /* This is entry PC record present only at entry point
443 	     of a function.  Verify it is really the function entry point.  */
444 
445 	  const struct block *pc_block = block_for_pc (pc);
446 	  struct symbol *pc_func = NULL;
447 
448 	  if (pc_block)
449 	    pc_func = block_linkage_function (pc_block);
450 
451 	  if (pc_func && pc == BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (pc_func)))
452 	    {
453 	      *locexpr_length = length;
454 	      return loc_ptr;
455 	    }
456 	}
457 
458       if (pc >= low && pc < high)
459 	{
460 	  *locexpr_length = length;
461 	  return loc_ptr;
462 	}
463 
464       loc_ptr += length;
465     }
466 }
467 
468 /* Implement find_frame_base_location method for LOC_BLOCK functions using
469    DWARF expression for its DW_AT_frame_base.  */
470 
471 static void
472 locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
473 				  const gdb_byte **start, size_t *length)
474 {
475   struct dwarf2_locexpr_baton *symbaton
476     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
477 
478   *length = symbaton->size;
479   *start = symbaton->data;
480 }
481 
482 /* Implement the struct symbol_block_ops::get_frame_base method for
483    LOC_BLOCK functions using a DWARF expression as its DW_AT_frame_base.  */
484 
485 static CORE_ADDR
486 locexpr_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
487 {
488   struct gdbarch *gdbarch;
489   struct type *type;
490   struct dwarf2_locexpr_baton *dlbaton;
491   const gdb_byte *start;
492   size_t length;
493   struct value *result;
494 
495   /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
496      Thus, it's supposed to provide the find_frame_base_location method as
497      well.  */
498   gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
499 
500   gdbarch = get_frame_arch (frame);
501   type = builtin_type (gdbarch)->builtin_data_ptr;
502   dlbaton = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
503 
504   SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
505     (framefunc, get_frame_pc (frame), &start, &length);
506   result = dwarf2_evaluate_loc_desc (type, frame, start, length,
507 				     dlbaton->per_cu, dlbaton->per_objfile);
508 
509   /* The DW_AT_frame_base attribute contains a location description which
510      computes the base address itself.  However, the call to
511      dwarf2_evaluate_loc_desc returns a value representing a variable at
512      that address.  The frame base address is thus this variable's
513      address.  */
514   return value_address (result);
515 }
516 
517 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
518    function uses DWARF expression for its DW_AT_frame_base.  */
519 
520 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
521 {
522   locexpr_find_frame_base_location,
523   locexpr_get_frame_base
524 };
525 
526 /* Implement find_frame_base_location method for LOC_BLOCK functions using
527    DWARF location list for its DW_AT_frame_base.  */
528 
529 static void
530 loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
531 				  const gdb_byte **start, size_t *length)
532 {
533   struct dwarf2_loclist_baton *symbaton
534     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
535 
536   *start = dwarf2_find_location_expression (symbaton, length, pc);
537 }
538 
539 /* Implement the struct symbol_block_ops::get_frame_base method for
540    LOC_BLOCK functions using a DWARF location list as its DW_AT_frame_base.  */
541 
542 static CORE_ADDR
543 loclist_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
544 {
545   struct gdbarch *gdbarch;
546   struct type *type;
547   struct dwarf2_loclist_baton *dlbaton;
548   const gdb_byte *start;
549   size_t length;
550   struct value *result;
551 
552   /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
553      Thus, it's supposed to provide the find_frame_base_location method as
554      well.  */
555   gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
556 
557   gdbarch = get_frame_arch (frame);
558   type = builtin_type (gdbarch)->builtin_data_ptr;
559   dlbaton = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
560 
561   SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
562     (framefunc, get_frame_pc (frame), &start, &length);
563   result = dwarf2_evaluate_loc_desc (type, frame, start, length,
564 				     dlbaton->per_cu, dlbaton->per_objfile);
565 
566   /* The DW_AT_frame_base attribute contains a location description which
567      computes the base address itself.  However, the call to
568      dwarf2_evaluate_loc_desc returns a value representing a variable at
569      that address.  The frame base address is thus this variable's
570      address.  */
571   return value_address (result);
572 }
573 
574 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
575    function uses DWARF location list for its DW_AT_frame_base.  */
576 
577 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
578 {
579   loclist_find_frame_base_location,
580   loclist_get_frame_base
581 };
582 
583 /* See dwarf2loc.h.  */
584 
585 void
586 func_get_frame_base_dwarf_block (struct symbol *framefunc, CORE_ADDR pc,
587 				 const gdb_byte **start, size_t *length)
588 {
589   if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
590     {
591       const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
592 
593       ops_block->find_frame_base_location (framefunc, pc, start, length);
594     }
595   else
596     *length = 0;
597 
598   if (*length == 0)
599     error (_("Could not find the frame base for \"%s\"."),
600 	   framefunc->natural_name ());
601 }
602 
603 static void
604 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
605 		   dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile)
606 {
607   struct dwarf2_locexpr_baton block;
608 
609   auto get_frame_pc_from_ctx = [ctx] ()
610     {
611       return ctx->get_frame_pc ();
612     };
613 
614   block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu, per_objfile,
615 				       get_frame_pc_from_ctx);
616 
617   /* DW_OP_call_ref is currently not supported.  */
618   gdb_assert (block.per_cu == per_cu);
619 
620   ctx->eval (block.data, block.size);
621 }
622 
623 /* Given context CTX, section offset SECT_OFF, and compilation unit
624    data PER_CU, execute the "variable value" operation on the DIE
625    found at SECT_OFF.  */
626 
627 static struct value *
628 sect_variable_value (struct dwarf_expr_context *ctx, sect_offset sect_off,
629 		     dwarf2_per_cu_data *per_cu,
630 		     dwarf2_per_objfile *per_objfile)
631 {
632   struct type *die_type
633     = dwarf2_fetch_die_type_sect_off (sect_off, per_cu, per_objfile);
634 
635   if (die_type == NULL)
636     error (_("Bad DW_OP_GNU_variable_value DIE."));
637 
638   /* Note: Things still work when the following test is removed.  This
639      test and error is here to conform to the proposed specification.  */
640   if (die_type->code () != TYPE_CODE_INT
641       && die_type->code () != TYPE_CODE_PTR)
642     error (_("Type of DW_OP_GNU_variable_value DIE must be an integer or pointer."));
643 
644   struct type *type = lookup_pointer_type (die_type);
645   struct frame_info *frame = get_selected_frame (_("No frame selected."));
646   return indirect_synthetic_pointer (sect_off, 0, per_cu, per_objfile, frame,
647 				     type, true);
648 }
649 
650 class dwarf_evaluate_loc_desc : public dwarf_expr_context
651 {
652 public:
653   dwarf_evaluate_loc_desc (dwarf2_per_objfile *per_objfile)
654     : dwarf_expr_context (per_objfile)
655   {}
656 
657   struct frame_info *frame;
658   struct dwarf2_per_cu_data *per_cu;
659   CORE_ADDR obj_address;
660 
661   /* Helper function for dwarf2_evaluate_loc_desc.  Computes the CFA for
662      the frame in BATON.  */
663 
664   CORE_ADDR get_frame_cfa () override
665   {
666     return dwarf2_frame_cfa (frame);
667   }
668 
669   /* Helper function for dwarf2_evaluate_loc_desc.  Computes the PC for
670      the frame in BATON.  */
671 
672   CORE_ADDR get_frame_pc () override
673   {
674     return get_frame_address_in_block (frame);
675   }
676 
677   /* Using the objfile specified in BATON, find the address for the
678      current thread's thread-local storage with offset OFFSET.  */
679   CORE_ADDR get_tls_address (CORE_ADDR offset) override
680   {
681     return target_translate_tls_address (per_objfile->objfile, offset);
682   }
683 
684   /* Helper interface of per_cu_dwarf_call for
685      dwarf2_evaluate_loc_desc.  */
686 
687   void dwarf_call (cu_offset die_offset) override
688   {
689     per_cu_dwarf_call (this, die_offset, per_cu, per_objfile);
690   }
691 
692   /* Helper interface of sect_variable_value for
693      dwarf2_evaluate_loc_desc.  */
694 
695   struct value *dwarf_variable_value (sect_offset sect_off) override
696   {
697     return sect_variable_value (this, sect_off, per_cu, per_objfile);
698   }
699 
700   struct type *get_base_type (cu_offset die_offset, int size) override
701   {
702     struct type *result = dwarf2_get_die_type (die_offset, per_cu, per_objfile);
703     if (result == NULL)
704       error (_("Could not find type for DW_OP_const_type"));
705     if (size != 0 && TYPE_LENGTH (result) != size)
706       error (_("DW_OP_const_type has different sizes for type and data"));
707     return result;
708   }
709 
710   /* Callback function for dwarf2_evaluate_loc_desc.
711      Fetch the address indexed by DW_OP_addrx or DW_OP_GNU_addr_index.  */
712 
713   CORE_ADDR get_addr_index (unsigned int index) override
714   {
715     return dwarf2_read_addr_index (per_cu, per_objfile, index);
716   }
717 
718   /* Callback function for get_object_address. Return the address of the VLA
719      object.  */
720 
721   CORE_ADDR get_object_address () override
722   {
723     if (obj_address == 0)
724       error (_("Location address is not set."));
725     return obj_address;
726   }
727 
728   /* Execute DWARF block of call_site_parameter which matches KIND and
729      KIND_U.  Choose DEREF_SIZE value of that parameter.  Search
730      caller of this objects's frame.
731 
732      The caller can be from a different CU - per_cu_dwarf_call
733      implementation can be more simple as it does not support cross-CU
734      DWARF executions.  */
735 
736   void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind,
737 				   union call_site_parameter_u kind_u,
738 				   int deref_size) override
739   {
740     struct frame_info *caller_frame;
741     dwarf2_per_cu_data *caller_per_cu;
742     dwarf2_per_objfile *caller_per_objfile;
743     struct call_site_parameter *parameter;
744     const gdb_byte *data_src;
745     size_t size;
746 
747     caller_frame = get_prev_frame (frame);
748 
749     parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
750 						   &caller_per_cu,
751 						   &caller_per_objfile);
752     data_src = deref_size == -1 ? parameter->value : parameter->data_value;
753     size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
754 
755     /* DEREF_SIZE size is not verified here.  */
756     if (data_src == NULL)
757       throw_error (NO_ENTRY_VALUE_ERROR,
758 		   _("Cannot resolve DW_AT_call_data_value"));
759 
760     /* We are about to evaluate an expression in the context of the caller
761        of the current frame.  This evaluation context may be different from
762        the current (callee's) context), so temporarily set the caller's context.
763 
764        It is possible for the caller to be from a different objfile from the
765        callee if the call is made through a function pointer.  */
766     scoped_restore save_frame = make_scoped_restore (&this->frame,
767 						     caller_frame);
768     scoped_restore save_per_cu = make_scoped_restore (&this->per_cu,
769 						      caller_per_cu);
770     scoped_restore save_obj_addr = make_scoped_restore (&this->obj_address,
771 							(CORE_ADDR) 0);
772     scoped_restore save_per_objfile = make_scoped_restore (&this->per_objfile,
773 							   caller_per_objfile);
774 
775     scoped_restore save_arch = make_scoped_restore (&this->gdbarch);
776     this->gdbarch = this->per_objfile->objfile->arch ();
777     scoped_restore save_addr_size = make_scoped_restore (&this->addr_size);
778     this->addr_size = this->per_cu->addr_size ();
779 
780     this->eval (data_src, size);
781   }
782 
783   /* Using the frame specified in BATON, find the location expression
784      describing the frame base.  Return a pointer to it in START and
785      its length in LENGTH.  */
786   void get_frame_base (const gdb_byte **start, size_t * length) override
787   {
788     /* FIXME: cagney/2003-03-26: This code should be using
789        get_frame_base_address(), and then implement a dwarf2 specific
790        this_base method.  */
791     struct symbol *framefunc;
792     const struct block *bl = get_frame_block (frame, NULL);
793 
794     if (bl == NULL)
795       error (_("frame address is not available."));
796 
797     /* Use block_linkage_function, which returns a real (not inlined)
798        function, instead of get_frame_function, which may return an
799        inlined function.  */
800     framefunc = block_linkage_function (bl);
801 
802     /* If we found a frame-relative symbol then it was certainly within
803        some function associated with a frame. If we can't find the frame,
804        something has gone wrong.  */
805     gdb_assert (framefunc != NULL);
806 
807     func_get_frame_base_dwarf_block (framefunc,
808 				     get_frame_address_in_block (frame),
809 				     start, length);
810   }
811 
812   /* Read memory at ADDR (length LEN) into BUF.  */
813 
814   void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t len) override
815   {
816     read_memory (addr, buf, len);
817   }
818 
819   /* Using the frame specified in BATON, return the value of register
820      REGNUM, treated as a pointer.  */
821   CORE_ADDR read_addr_from_reg (int dwarf_regnum) override
822   {
823     struct gdbarch *gdbarch = get_frame_arch (frame);
824     int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
825 
826     return address_from_register (regnum, frame);
827   }
828 
829   /* Implement "get_reg_value" callback.  */
830 
831   struct value *get_reg_value (struct type *type, int dwarf_regnum) override
832   {
833     struct gdbarch *gdbarch = get_frame_arch (frame);
834     int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
835 
836     return value_from_register (type, regnum, frame);
837   }
838 };
839 
840 /* See dwarf2loc.h.  */
841 
842 unsigned int entry_values_debug = 0;
843 
844 /* Helper to set entry_values_debug.  */
845 
846 static void
847 show_entry_values_debug (struct ui_file *file, int from_tty,
848 			 struct cmd_list_element *c, const char *value)
849 {
850   fprintf_filtered (file,
851 		    _("Entry values and tail call frames debugging is %s.\n"),
852 		    value);
853 }
854 
855 /* Find DW_TAG_call_site's DW_AT_call_target address.
856    CALLER_FRAME (for registers) can be NULL if it is not known.  This function
857    always returns valid address or it throws NO_ENTRY_VALUE_ERROR.  */
858 
859 static CORE_ADDR
860 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
861 			  struct call_site *call_site,
862 			  struct frame_info *caller_frame)
863 {
864   switch (FIELD_LOC_KIND (call_site->target))
865     {
866     case FIELD_LOC_KIND_DWARF_BLOCK:
867       {
868 	struct dwarf2_locexpr_baton *dwarf_block;
869 	struct value *val;
870 	struct type *caller_core_addr_type;
871 	struct gdbarch *caller_arch;
872 
873 	dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
874 	if (dwarf_block == NULL)
875 	  {
876 	    struct bound_minimal_symbol msym;
877 
878 	    msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
879 	    throw_error (NO_ENTRY_VALUE_ERROR,
880 			 _("DW_AT_call_target is not specified at %s in %s"),
881 			 paddress (call_site_gdbarch, call_site->pc),
882 			 (msym.minsym == NULL ? "???"
883 			  : msym.minsym->print_name ()));
884 
885 	  }
886 	if (caller_frame == NULL)
887 	  {
888 	    struct bound_minimal_symbol msym;
889 
890 	    msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
891 	    throw_error (NO_ENTRY_VALUE_ERROR,
892 			 _("DW_AT_call_target DWARF block resolving "
893 			   "requires known frame which is currently not "
894 			   "available at %s in %s"),
895 			 paddress (call_site_gdbarch, call_site->pc),
896 			 (msym.minsym == NULL ? "???"
897 			  : msym.minsym->print_name ()));
898 
899 	  }
900 	caller_arch = get_frame_arch (caller_frame);
901 	caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
902 	val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
903 					dwarf_block->data, dwarf_block->size,
904 					dwarf_block->per_cu,
905 					dwarf_block->per_objfile);
906 	/* DW_AT_call_target is a DWARF expression, not a DWARF location.  */
907 	if (VALUE_LVAL (val) == lval_memory)
908 	  return value_address (val);
909 	else
910 	  return value_as_address (val);
911       }
912 
913     case FIELD_LOC_KIND_PHYSNAME:
914       {
915 	const char *physname;
916 	struct bound_minimal_symbol msym;
917 
918 	physname = FIELD_STATIC_PHYSNAME (call_site->target);
919 
920 	/* Handle both the mangled and demangled PHYSNAME.  */
921 	msym = lookup_minimal_symbol (physname, NULL, NULL);
922 	if (msym.minsym == NULL)
923 	  {
924 	    msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
925 	    throw_error (NO_ENTRY_VALUE_ERROR,
926 			 _("Cannot find function \"%s\" for a call site target "
927 			   "at %s in %s"),
928 			 physname, paddress (call_site_gdbarch, call_site->pc),
929 			 (msym.minsym == NULL ? "???"
930 			  : msym.minsym->print_name ()));
931 
932 	  }
933 	return BMSYMBOL_VALUE_ADDRESS (msym);
934       }
935 
936     case FIELD_LOC_KIND_PHYSADDR:
937       return FIELD_STATIC_PHYSADDR (call_site->target);
938 
939     default:
940       internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
941     }
942 }
943 
944 /* Convert function entry point exact address ADDR to the function which is
945    compliant with TAIL_CALL_LIST_COMPLETE condition.  Throw
946    NO_ENTRY_VALUE_ERROR otherwise.  */
947 
948 static struct symbol *
949 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
950 {
951   struct symbol *sym = find_pc_function (addr);
952   struct type *type;
953 
954   if (sym == NULL || BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)) != addr)
955     throw_error (NO_ENTRY_VALUE_ERROR,
956 		 _("DW_TAG_call_site resolving failed to find function "
957 		   "name for address %s"),
958 		 paddress (gdbarch, addr));
959 
960   type = SYMBOL_TYPE (sym);
961   gdb_assert (type->code () == TYPE_CODE_FUNC);
962   gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
963 
964   return sym;
965 }
966 
967 /* Verify function with entry point exact address ADDR can never call itself
968    via its tail calls (incl. transitively).  Throw NO_ENTRY_VALUE_ERROR if it
969    can call itself via tail calls.
970 
971    If a funtion can tail call itself its entry value based parameters are
972    unreliable.  There is no verification whether the value of some/all
973    parameters is unchanged through the self tail call, we expect if there is
974    a self tail call all the parameters can be modified.  */
975 
976 static void
977 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
978 {
979   CORE_ADDR addr;
980 
981   /* The verification is completely unordered.  Track here function addresses
982      which still need to be iterated.  */
983   std::vector<CORE_ADDR> todo;
984 
985   /* Track here CORE_ADDRs which were already visited.  */
986   std::unordered_set<CORE_ADDR> addr_hash;
987 
988   todo.push_back (verify_addr);
989   while (!todo.empty ())
990     {
991       struct symbol *func_sym;
992       struct call_site *call_site;
993 
994       addr = todo.back ();
995       todo.pop_back ();
996 
997       func_sym = func_addr_to_tail_call_list (gdbarch, addr);
998 
999       for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
1000 	   call_site; call_site = call_site->tail_call_next)
1001 	{
1002 	  CORE_ADDR target_addr;
1003 
1004 	  /* CALLER_FRAME with registers is not available for tail-call jumped
1005 	     frames.  */
1006 	  target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
1007 
1008 	  if (target_addr == verify_addr)
1009 	    {
1010 	      struct bound_minimal_symbol msym;
1011 
1012 	      msym = lookup_minimal_symbol_by_pc (verify_addr);
1013 	      throw_error (NO_ENTRY_VALUE_ERROR,
1014 			   _("DW_OP_entry_value resolving has found "
1015 			     "function \"%s\" at %s can call itself via tail "
1016 			     "calls"),
1017 			   (msym.minsym == NULL ? "???"
1018 			    : msym.minsym->print_name ()),
1019 			   paddress (gdbarch, verify_addr));
1020 	    }
1021 
1022 	  if (addr_hash.insert (target_addr).second)
1023 	    todo.push_back (target_addr);
1024 	}
1025     }
1026 }
1027 
1028 /* Print user readable form of CALL_SITE->PC to gdb_stdlog.  Used only for
1029    ENTRY_VALUES_DEBUG.  */
1030 
1031 static void
1032 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
1033 {
1034   CORE_ADDR addr = call_site->pc;
1035   struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
1036 
1037   fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
1038 		      (msym.minsym == NULL ? "???"
1039 		       : msym.minsym->print_name ()));
1040 
1041 }
1042 
1043 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
1044    only top callers and bottom callees which are present in both.  GDBARCH is
1045    used only for ENTRY_VALUES_DEBUG.  RESULTP is NULL after return if there are
1046    no remaining possibilities to provide unambiguous non-trivial result.
1047    RESULTP should point to NULL on the first (initialization) call.  Caller is
1048    responsible for xfree of any RESULTP data.  */
1049 
1050 static void
1051 chain_candidate (struct gdbarch *gdbarch,
1052 		 gdb::unique_xmalloc_ptr<struct call_site_chain> *resultp,
1053 		 std::vector<struct call_site *> *chain)
1054 {
1055   long length = chain->size ();
1056   int callers, callees, idx;
1057 
1058   if (*resultp == NULL)
1059     {
1060       /* Create the initial chain containing all the passed PCs.  */
1061 
1062       struct call_site_chain *result
1063 	= ((struct call_site_chain *)
1064 	   xmalloc (sizeof (*result)
1065 		    + sizeof (*result->call_site) * (length - 1)));
1066       result->length = length;
1067       result->callers = result->callees = length;
1068       if (!chain->empty ())
1069 	memcpy (result->call_site, chain->data (),
1070 		sizeof (*result->call_site) * length);
1071       resultp->reset (result);
1072 
1073       if (entry_values_debug)
1074 	{
1075 	  fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
1076 	  for (idx = 0; idx < length; idx++)
1077 	    tailcall_dump (gdbarch, result->call_site[idx]);
1078 	  fputc_unfiltered ('\n', gdb_stdlog);
1079 	}
1080 
1081       return;
1082     }
1083 
1084   if (entry_values_debug)
1085     {
1086       fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
1087       for (idx = 0; idx < length; idx++)
1088 	tailcall_dump (gdbarch, chain->at (idx));
1089       fputc_unfiltered ('\n', gdb_stdlog);
1090     }
1091 
1092   /* Intersect callers.  */
1093 
1094   callers = std::min ((long) (*resultp)->callers, length);
1095   for (idx = 0; idx < callers; idx++)
1096     if ((*resultp)->call_site[idx] != chain->at (idx))
1097       {
1098 	(*resultp)->callers = idx;
1099 	break;
1100       }
1101 
1102   /* Intersect callees.  */
1103 
1104   callees = std::min ((long) (*resultp)->callees, length);
1105   for (idx = 0; idx < callees; idx++)
1106     if ((*resultp)->call_site[(*resultp)->length - 1 - idx]
1107 	!= chain->at (length - 1 - idx))
1108       {
1109 	(*resultp)->callees = idx;
1110 	break;
1111       }
1112 
1113   if (entry_values_debug)
1114     {
1115       fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
1116       for (idx = 0; idx < (*resultp)->callers; idx++)
1117 	tailcall_dump (gdbarch, (*resultp)->call_site[idx]);
1118       fputs_unfiltered (" |", gdb_stdlog);
1119       for (idx = 0; idx < (*resultp)->callees; idx++)
1120 	tailcall_dump (gdbarch,
1121 		       (*resultp)->call_site[(*resultp)->length
1122 					     - (*resultp)->callees + idx]);
1123       fputc_unfiltered ('\n', gdb_stdlog);
1124     }
1125 
1126   if ((*resultp)->callers == 0 && (*resultp)->callees == 0)
1127     {
1128       /* There are no common callers or callees.  It could be also a direct
1129 	 call (which has length 0) with ambiguous possibility of an indirect
1130 	 call - CALLERS == CALLEES == 0 is valid during the first allocation
1131 	 but any subsequence processing of such entry means ambiguity.  */
1132       resultp->reset (NULL);
1133       return;
1134     }
1135 
1136   /* See call_site_find_chain_1 why there is no way to reach the bottom callee
1137      PC again.  In such case there must be two different code paths to reach
1138      it.  CALLERS + CALLEES equal to LENGTH in the case of self tail-call.  */
1139   gdb_assert ((*resultp)->callers + (*resultp)->callees <= (*resultp)->length);
1140 }
1141 
1142 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
1143    assumed frames between them use GDBARCH.  Use depth first search so we can
1144    keep single CHAIN of call_site's back to CALLER_PC.  Function recursion
1145    would have needless GDB stack overhead.  Any unreliability results
1146    in thrown NO_ENTRY_VALUE_ERROR.  */
1147 
1148 static gdb::unique_xmalloc_ptr<call_site_chain>
1149 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
1150 			CORE_ADDR callee_pc)
1151 {
1152   CORE_ADDR save_callee_pc = callee_pc;
1153   gdb::unique_xmalloc_ptr<struct call_site_chain> retval;
1154   struct call_site *call_site;
1155 
1156   /* CHAIN contains only the intermediate CALL_SITEs.  Neither CALLER_PC's
1157      call_site nor any possible call_site at CALLEE_PC's function is there.
1158      Any CALL_SITE in CHAIN will be iterated to its siblings - via
1159      TAIL_CALL_NEXT.  This is inappropriate for CALLER_PC's call_site.  */
1160   std::vector<struct call_site *> chain;
1161 
1162   /* We are not interested in the specific PC inside the callee function.  */
1163   callee_pc = get_pc_function_start (callee_pc);
1164   if (callee_pc == 0)
1165     throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
1166 		 paddress (gdbarch, save_callee_pc));
1167 
1168   /* Mark CALL_SITEs so we do not visit the same ones twice.  */
1169   std::unordered_set<CORE_ADDR> addr_hash;
1170 
1171   /* Do not push CALL_SITE to CHAIN.  Push there only the first tail call site
1172      at the target's function.  All the possible tail call sites in the
1173      target's function will get iterated as already pushed into CHAIN via their
1174      TAIL_CALL_NEXT.  */
1175   call_site = call_site_for_pc (gdbarch, caller_pc);
1176 
1177   while (call_site)
1178     {
1179       CORE_ADDR target_func_addr;
1180       struct call_site *target_call_site;
1181 
1182       /* CALLER_FRAME with registers is not available for tail-call jumped
1183 	 frames.  */
1184       target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
1185 
1186       if (target_func_addr == callee_pc)
1187 	{
1188 	  chain_candidate (gdbarch, &retval, &chain);
1189 	  if (retval == NULL)
1190 	    break;
1191 
1192 	  /* There is no way to reach CALLEE_PC again as we would prevent
1193 	     entering it twice as being already marked in ADDR_HASH.  */
1194 	  target_call_site = NULL;
1195 	}
1196       else
1197 	{
1198 	  struct symbol *target_func;
1199 
1200 	  target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
1201 	  target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
1202 	}
1203 
1204       do
1205 	{
1206 	  /* Attempt to visit TARGET_CALL_SITE.  */
1207 
1208 	  if (target_call_site)
1209 	    {
1210 	      if (addr_hash.insert (target_call_site->pc).second)
1211 		{
1212 		  /* Successfully entered TARGET_CALL_SITE.  */
1213 
1214 		  chain.push_back (target_call_site);
1215 		  break;
1216 		}
1217 	    }
1218 
1219 	  /* Backtrack (without revisiting the originating call_site).  Try the
1220 	     callers's sibling; if there isn't any try the callers's callers's
1221 	     sibling etc.  */
1222 
1223 	  target_call_site = NULL;
1224 	  while (!chain.empty ())
1225 	    {
1226 	      call_site = chain.back ();
1227 	      chain.pop_back ();
1228 
1229 	      size_t removed = addr_hash.erase (call_site->pc);
1230 	      gdb_assert (removed == 1);
1231 
1232 	      target_call_site = call_site->tail_call_next;
1233 	      if (target_call_site)
1234 		break;
1235 	    }
1236 	}
1237       while (target_call_site);
1238 
1239       if (chain.empty ())
1240 	call_site = NULL;
1241       else
1242 	call_site = chain.back ();
1243     }
1244 
1245   if (retval == NULL)
1246     {
1247       struct bound_minimal_symbol msym_caller, msym_callee;
1248 
1249       msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
1250       msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
1251       throw_error (NO_ENTRY_VALUE_ERROR,
1252 		   _("There are no unambiguously determinable intermediate "
1253 		     "callers or callees between caller function \"%s\" at %s "
1254 		     "and callee function \"%s\" at %s"),
1255 		   (msym_caller.minsym == NULL
1256 		    ? "???" : msym_caller.minsym->print_name ()),
1257 		   paddress (gdbarch, caller_pc),
1258 		   (msym_callee.minsym == NULL
1259 		    ? "???" : msym_callee.minsym->print_name ()),
1260 		   paddress (gdbarch, callee_pc));
1261     }
1262 
1263   return retval;
1264 }
1265 
1266 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC.  All the
1267    assumed frames between them use GDBARCH.  If valid call_site_chain cannot be
1268    constructed return NULL.  */
1269 
1270 gdb::unique_xmalloc_ptr<call_site_chain>
1271 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
1272 		      CORE_ADDR callee_pc)
1273 {
1274   gdb::unique_xmalloc_ptr<call_site_chain> retval;
1275 
1276   try
1277     {
1278       retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
1279     }
1280   catch (const gdb_exception_error &e)
1281     {
1282       if (e.error == NO_ENTRY_VALUE_ERROR)
1283 	{
1284 	  if (entry_values_debug)
1285 	    exception_print (gdb_stdout, e);
1286 
1287 	  return NULL;
1288 	}
1289       else
1290 	throw;
1291     }
1292 
1293   return retval;
1294 }
1295 
1296 /* Return 1 if KIND and KIND_U match PARAMETER.  Return 0 otherwise.  */
1297 
1298 static int
1299 call_site_parameter_matches (struct call_site_parameter *parameter,
1300 			     enum call_site_parameter_kind kind,
1301 			     union call_site_parameter_u kind_u)
1302 {
1303   if (kind == parameter->kind)
1304     switch (kind)
1305       {
1306       case CALL_SITE_PARAMETER_DWARF_REG:
1307 	return kind_u.dwarf_reg == parameter->u.dwarf_reg;
1308 
1309       case CALL_SITE_PARAMETER_FB_OFFSET:
1310 	return kind_u.fb_offset == parameter->u.fb_offset;
1311 
1312       case CALL_SITE_PARAMETER_PARAM_OFFSET:
1313 	return kind_u.param_cu_off == parameter->u.param_cu_off;
1314       }
1315   return 0;
1316 }
1317 
1318 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
1319    FRAME is for callee.
1320 
1321    Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
1322    otherwise.  */
1323 
1324 static struct call_site_parameter *
1325 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
1326 				   enum call_site_parameter_kind kind,
1327 				   union call_site_parameter_u kind_u,
1328 				   dwarf2_per_cu_data **per_cu_return,
1329 				   dwarf2_per_objfile **per_objfile_return)
1330 {
1331   CORE_ADDR func_addr, caller_pc;
1332   struct gdbarch *gdbarch;
1333   struct frame_info *caller_frame;
1334   struct call_site *call_site;
1335   int iparams;
1336   /* Initialize it just to avoid a GCC false warning.  */
1337   struct call_site_parameter *parameter = NULL;
1338   CORE_ADDR target_addr;
1339 
1340   while (get_frame_type (frame) == INLINE_FRAME)
1341     {
1342       frame = get_prev_frame (frame);
1343       gdb_assert (frame != NULL);
1344     }
1345 
1346   func_addr = get_frame_func (frame);
1347   gdbarch = get_frame_arch (frame);
1348   caller_frame = get_prev_frame (frame);
1349   if (gdbarch != frame_unwind_arch (frame))
1350     {
1351       struct bound_minimal_symbol msym
1352 	= lookup_minimal_symbol_by_pc (func_addr);
1353       struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
1354 
1355       throw_error (NO_ENTRY_VALUE_ERROR,
1356 		   _("DW_OP_entry_value resolving callee gdbarch %s "
1357 		     "(of %s (%s)) does not match caller gdbarch %s"),
1358 		   gdbarch_bfd_arch_info (gdbarch)->printable_name,
1359 		   paddress (gdbarch, func_addr),
1360 		   (msym.minsym == NULL ? "???"
1361 		    : msym.minsym->print_name ()),
1362 		   gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
1363     }
1364 
1365   if (caller_frame == NULL)
1366     {
1367       struct bound_minimal_symbol msym
1368 	= lookup_minimal_symbol_by_pc (func_addr);
1369 
1370       throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_entry_value resolving "
1371 					   "requires caller of %s (%s)"),
1372 		   paddress (gdbarch, func_addr),
1373 		   (msym.minsym == NULL ? "???"
1374 		    : msym.minsym->print_name ()));
1375     }
1376   caller_pc = get_frame_pc (caller_frame);
1377   call_site = call_site_for_pc (gdbarch, caller_pc);
1378 
1379   target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
1380   if (target_addr != func_addr)
1381     {
1382       struct minimal_symbol *target_msym, *func_msym;
1383 
1384       target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
1385       func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
1386       throw_error (NO_ENTRY_VALUE_ERROR,
1387 		   _("DW_OP_entry_value resolving expects callee %s at %s "
1388 		     "but the called frame is for %s at %s"),
1389 		   (target_msym == NULL ? "???"
1390 					: target_msym->print_name ()),
1391 		   paddress (gdbarch, target_addr),
1392 		   func_msym == NULL ? "???" : func_msym->print_name (),
1393 		   paddress (gdbarch, func_addr));
1394     }
1395 
1396   /* No entry value based parameters would be reliable if this function can
1397      call itself via tail calls.  */
1398   func_verify_no_selftailcall (gdbarch, func_addr);
1399 
1400   for (iparams = 0; iparams < call_site->parameter_count; iparams++)
1401     {
1402       parameter = &call_site->parameter[iparams];
1403       if (call_site_parameter_matches (parameter, kind, kind_u))
1404 	break;
1405     }
1406   if (iparams == call_site->parameter_count)
1407     {
1408       struct minimal_symbol *msym
1409 	= lookup_minimal_symbol_by_pc (caller_pc).minsym;
1410 
1411       /* DW_TAG_call_site_parameter will be missing just if GCC could not
1412 	 determine its value.  */
1413       throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
1414 					   "at DW_TAG_call_site %s at %s"),
1415 		   paddress (gdbarch, caller_pc),
1416 		   msym == NULL ? "???" : msym->print_name ());
1417     }
1418 
1419   *per_cu_return = call_site->per_cu;
1420   *per_objfile_return = call_site->per_objfile;
1421   return parameter;
1422 }
1423 
1424 /* Return value for PARAMETER matching DEREF_SIZE.  If DEREF_SIZE is -1, return
1425    the normal DW_AT_call_value block.  Otherwise return the
1426    DW_AT_call_data_value (dereferenced) block.
1427 
1428    TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1429    struct value.
1430 
1431    Function always returns non-NULL, non-optimized out value.  It throws
1432    NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason.  */
1433 
1434 static struct value *
1435 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
1436 				CORE_ADDR deref_size, struct type *type,
1437 				struct frame_info *caller_frame,
1438 				dwarf2_per_cu_data *per_cu,
1439 				dwarf2_per_objfile *per_objfile)
1440 {
1441   const gdb_byte *data_src;
1442   gdb_byte *data;
1443   size_t size;
1444 
1445   data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1446   size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1447 
1448   /* DEREF_SIZE size is not verified here.  */
1449   if (data_src == NULL)
1450     throw_error (NO_ENTRY_VALUE_ERROR,
1451 		 _("Cannot resolve DW_AT_call_data_value"));
1452 
1453   /* DW_AT_call_value is a DWARF expression, not a DWARF
1454      location.  Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1455      DWARF block.  */
1456   data = (gdb_byte *) alloca (size + 1);
1457   memcpy (data, data_src, size);
1458   data[size] = DW_OP_stack_value;
1459 
1460   return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu,
1461 				   per_objfile);
1462 }
1463 
1464 /* VALUE must be of type lval_computed with entry_data_value_funcs.  Perform
1465    the indirect method on it, that is use its stored target value, the sole
1466    purpose of entry_data_value_funcs..  */
1467 
1468 static struct value *
1469 entry_data_value_coerce_ref (const struct value *value)
1470 {
1471   struct type *checked_type = check_typedef (value_type (value));
1472   struct value *target_val;
1473 
1474   if (!TYPE_IS_REFERENCE (checked_type))
1475     return NULL;
1476 
1477   target_val = (struct value *) value_computed_closure (value);
1478   value_incref (target_val);
1479   return target_val;
1480 }
1481 
1482 /* Implement copy_closure.  */
1483 
1484 static void *
1485 entry_data_value_copy_closure (const struct value *v)
1486 {
1487   struct value *target_val = (struct value *) value_computed_closure (v);
1488 
1489   value_incref (target_val);
1490   return target_val;
1491 }
1492 
1493 /* Implement free_closure.  */
1494 
1495 static void
1496 entry_data_value_free_closure (struct value *v)
1497 {
1498   struct value *target_val = (struct value *) value_computed_closure (v);
1499 
1500   value_decref (target_val);
1501 }
1502 
1503 /* Vector for methods for an entry value reference where the referenced value
1504    is stored in the caller.  On the first dereference use
1505    DW_AT_call_data_value in the caller.  */
1506 
1507 static const struct lval_funcs entry_data_value_funcs =
1508 {
1509   NULL,	/* read */
1510   NULL,	/* write */
1511   NULL,	/* indirect */
1512   entry_data_value_coerce_ref,
1513   NULL,	/* check_synthetic_pointer */
1514   entry_data_value_copy_closure,
1515   entry_data_value_free_closure
1516 };
1517 
1518 /* Read parameter of TYPE at (callee) FRAME's function entry.  KIND and KIND_U
1519    are used to match DW_AT_location at the caller's
1520    DW_TAG_call_site_parameter.
1521 
1522    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
1523    cannot resolve the parameter for any reason.  */
1524 
1525 static struct value *
1526 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1527 			  enum call_site_parameter_kind kind,
1528 			  union call_site_parameter_u kind_u)
1529 {
1530   struct type *checked_type = check_typedef (type);
1531   struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1532   struct frame_info *caller_frame = get_prev_frame (frame);
1533   struct value *outer_val, *target_val, *val;
1534   struct call_site_parameter *parameter;
1535   dwarf2_per_cu_data *caller_per_cu;
1536   dwarf2_per_objfile *caller_per_objfile;
1537 
1538   parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1539 						 &caller_per_cu,
1540 						 &caller_per_objfile);
1541 
1542   outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1543 					      type, caller_frame,
1544 					      caller_per_cu,
1545 					      caller_per_objfile);
1546 
1547   /* Check if DW_AT_call_data_value cannot be used.  If it should be
1548      used and it is not available do not fall back to OUTER_VAL - dereferencing
1549      TYPE_CODE_REF with non-entry data value would give current value - not the
1550      entry value.  */
1551 
1552   if (!TYPE_IS_REFERENCE (checked_type)
1553       || TYPE_TARGET_TYPE (checked_type) == NULL)
1554     return outer_val;
1555 
1556   target_val = dwarf_entry_parameter_to_value (parameter,
1557 					       TYPE_LENGTH (target_type),
1558 					       target_type, caller_frame,
1559 					       caller_per_cu,
1560 					       caller_per_objfile);
1561 
1562   val = allocate_computed_value (type, &entry_data_value_funcs,
1563 				 release_value (target_val).release ());
1564 
1565   /* Copy the referencing pointer to the new computed value.  */
1566   memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1567 	  TYPE_LENGTH (checked_type));
1568   set_value_lazy (val, 0);
1569 
1570   return val;
1571 }
1572 
1573 /* Read parameter of TYPE at (callee) FRAME's function entry.  DATA and
1574    SIZE are DWARF block used to match DW_AT_location at the caller's
1575    DW_TAG_call_site_parameter.
1576 
1577    Function always returns non-NULL value.  It throws NO_ENTRY_VALUE_ERROR if it
1578    cannot resolve the parameter for any reason.  */
1579 
1580 static struct value *
1581 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1582 			    const gdb_byte *block, size_t block_len)
1583 {
1584   union call_site_parameter_u kind_u;
1585 
1586   kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1587   if (kind_u.dwarf_reg != -1)
1588     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
1589 				     kind_u);
1590 
1591   if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
1592     return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
1593                                      kind_u);
1594 
1595   /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1596      suppressed during normal operation.  The expression can be arbitrary if
1597      there is no caller-callee entry value binding expected.  */
1598   throw_error (NO_ENTRY_VALUE_ERROR,
1599 	       _("DWARF-2 expression error: DW_OP_entry_value is supported "
1600 		 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1601 }
1602 
1603 struct piece_closure
1604 {
1605   /* Reference count.  */
1606   int refc = 0;
1607 
1608   /* The objfile from which this closure's expression came.  */
1609   dwarf2_per_objfile *per_objfile = nullptr;
1610 
1611   /* The CU from which this closure's expression came.  */
1612   struct dwarf2_per_cu_data *per_cu = NULL;
1613 
1614   /* The pieces describing this variable.  */
1615   std::vector<dwarf_expr_piece> pieces;
1616 
1617   /* Frame ID of frame to which a register value is relative, used
1618      only by DWARF_VALUE_REGISTER.  */
1619   struct frame_id frame_id;
1620 };
1621 
1622 /* Allocate a closure for a value formed from separately-described
1623    PIECES.  */
1624 
1625 static struct piece_closure *
1626 allocate_piece_closure (dwarf2_per_cu_data *per_cu,
1627 			dwarf2_per_objfile *per_objfile,
1628 			std::vector<dwarf_expr_piece> &&pieces,
1629 			struct frame_info *frame)
1630 {
1631   struct piece_closure *c = new piece_closure;
1632 
1633   c->refc = 1;
1634   /* We must capture this here due to sharing of DWARF state.  */
1635   c->per_objfile = per_objfile;
1636   c->per_cu = per_cu;
1637   c->pieces = std::move (pieces);
1638   if (frame == NULL)
1639     c->frame_id = null_frame_id;
1640   else
1641     c->frame_id = get_frame_id (frame);
1642 
1643   for (dwarf_expr_piece &piece : c->pieces)
1644     if (piece.location == DWARF_VALUE_STACK)
1645       value_incref (piece.v.value);
1646 
1647   return c;
1648 }
1649 
1650 /* Return the number of bytes overlapping a contiguous chunk of N_BITS
1651    bits whose first bit is located at bit offset START.  */
1652 
1653 static size_t
1654 bits_to_bytes (ULONGEST start, ULONGEST n_bits)
1655 {
1656   return (start % 8 + n_bits + 7) / 8;
1657 }
1658 
1659 /* Read or write a pieced value V.  If FROM != NULL, operate in "write
1660    mode": copy FROM into the pieces comprising V.  If FROM == NULL,
1661    operate in "read mode": fetch the contents of the (lazy) value V by
1662    composing it from its pieces.  */
1663 
1664 static void
1665 rw_pieced_value (struct value *v, struct value *from)
1666 {
1667   int i;
1668   LONGEST offset = 0, max_offset;
1669   ULONGEST bits_to_skip;
1670   gdb_byte *v_contents;
1671   const gdb_byte *from_contents;
1672   struct piece_closure *c
1673     = (struct piece_closure *) value_computed_closure (v);
1674   gdb::byte_vector buffer;
1675   bool bits_big_endian = type_byte_order (value_type (v)) == BFD_ENDIAN_BIG;
1676 
1677   if (from != NULL)
1678     {
1679       from_contents = value_contents (from);
1680       v_contents = NULL;
1681     }
1682   else
1683     {
1684       if (value_type (v) != value_enclosing_type (v))
1685 	internal_error (__FILE__, __LINE__,
1686 			_("Should not be able to create a lazy value with "
1687 			  "an enclosing type"));
1688       v_contents = value_contents_raw (v);
1689       from_contents = NULL;
1690     }
1691 
1692   bits_to_skip = 8 * value_offset (v);
1693   if (value_bitsize (v))
1694     {
1695       bits_to_skip += (8 * value_offset (value_parent (v))
1696 		       + value_bitpos (v));
1697       if (from != NULL
1698 	  && (type_byte_order (value_type (from))
1699 	      == BFD_ENDIAN_BIG))
1700 	{
1701 	  /* Use the least significant bits of FROM.  */
1702 	  max_offset = 8 * TYPE_LENGTH (value_type (from));
1703 	  offset = max_offset - value_bitsize (v);
1704 	}
1705       else
1706 	max_offset = value_bitsize (v);
1707     }
1708   else
1709     max_offset = 8 * TYPE_LENGTH (value_type (v));
1710 
1711   /* Advance to the first non-skipped piece.  */
1712   for (i = 0; i < c->pieces.size () && bits_to_skip >= c->pieces[i].size; i++)
1713     bits_to_skip -= c->pieces[i].size;
1714 
1715   for (; i < c->pieces.size () && offset < max_offset; i++)
1716     {
1717       struct dwarf_expr_piece *p = &c->pieces[i];
1718       size_t this_size_bits, this_size;
1719 
1720       this_size_bits = p->size - bits_to_skip;
1721       if (this_size_bits > max_offset - offset)
1722 	this_size_bits = max_offset - offset;
1723 
1724       switch (p->location)
1725 	{
1726 	case DWARF_VALUE_REGISTER:
1727 	  {
1728 	    struct frame_info *frame = frame_find_by_id (c->frame_id);
1729 	    struct gdbarch *arch = get_frame_arch (frame);
1730 	    int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1731 	    ULONGEST reg_bits = 8 * register_size (arch, gdb_regnum);
1732 	    int optim, unavail;
1733 
1734 	    if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1735 		&& p->offset + p->size < reg_bits)
1736 	      {
1737 		/* Big-endian, and we want less than full size.  */
1738 		bits_to_skip += reg_bits - (p->offset + p->size);
1739 	      }
1740 	    else
1741 	      bits_to_skip += p->offset;
1742 
1743 	    this_size = bits_to_bytes (bits_to_skip, this_size_bits);
1744 	    buffer.resize (this_size);
1745 
1746 	    if (from == NULL)
1747 	      {
1748 		/* Read mode.  */
1749 		if (!get_frame_register_bytes (frame, gdb_regnum,
1750 					       bits_to_skip / 8,
1751 					       this_size, buffer.data (),
1752 					       &optim, &unavail))
1753 		  {
1754 		    if (optim)
1755 		      mark_value_bits_optimized_out (v, offset,
1756 						     this_size_bits);
1757 		    if (unavail)
1758 		      mark_value_bits_unavailable (v, offset,
1759 						   this_size_bits);
1760 		    break;
1761 		  }
1762 
1763 		copy_bitwise (v_contents, offset,
1764 			      buffer.data (), bits_to_skip % 8,
1765 			      this_size_bits, bits_big_endian);
1766 	      }
1767 	    else
1768 	      {
1769 		/* Write mode.  */
1770 		if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
1771 		  {
1772 		    /* Data is copied non-byte-aligned into the register.
1773 		       Need some bits from original register value.  */
1774 		    get_frame_register_bytes (frame, gdb_regnum,
1775 					      bits_to_skip / 8,
1776 					      this_size, buffer.data (),
1777 					      &optim, &unavail);
1778 		    if (optim)
1779 		      throw_error (OPTIMIZED_OUT_ERROR,
1780 				   _("Can't do read-modify-write to "
1781 				     "update bitfield; containing word "
1782 				     "has been optimized out"));
1783 		    if (unavail)
1784 		      throw_error (NOT_AVAILABLE_ERROR,
1785 				   _("Can't do read-modify-write to "
1786 				     "update bitfield; containing word "
1787 				     "is unavailable"));
1788 		  }
1789 
1790 		copy_bitwise (buffer.data (), bits_to_skip % 8,
1791 			      from_contents, offset,
1792 			      this_size_bits, bits_big_endian);
1793 		put_frame_register_bytes (frame, gdb_regnum,
1794 					  bits_to_skip / 8,
1795 					  this_size, buffer.data ());
1796 	      }
1797 	  }
1798 	  break;
1799 
1800 	case DWARF_VALUE_MEMORY:
1801 	  {
1802 	    bits_to_skip += p->offset;
1803 
1804 	    CORE_ADDR start_addr = p->v.mem.addr + bits_to_skip / 8;
1805 
1806 	    if (bits_to_skip % 8 == 0 && this_size_bits % 8 == 0
1807 		&& offset % 8 == 0)
1808 	      {
1809 		/* Everything is byte-aligned; no buffer needed.  */
1810 		if (from != NULL)
1811 		  write_memory_with_notification (start_addr,
1812 						  (from_contents
1813 						   + offset / 8),
1814 						  this_size_bits / 8);
1815 		else
1816 		  read_value_memory (v, offset,
1817 				     p->v.mem.in_stack_memory,
1818 				     p->v.mem.addr + bits_to_skip / 8,
1819 				     v_contents + offset / 8,
1820 				     this_size_bits / 8);
1821 		break;
1822 	      }
1823 
1824 	    this_size = bits_to_bytes (bits_to_skip, this_size_bits);
1825 	    buffer.resize (this_size);
1826 
1827 	    if (from == NULL)
1828 	      {
1829 		/* Read mode.  */
1830 		read_value_memory (v, offset,
1831 				   p->v.mem.in_stack_memory,
1832 				   p->v.mem.addr + bits_to_skip / 8,
1833 				   buffer.data (), this_size);
1834 		copy_bitwise (v_contents, offset,
1835 			      buffer.data (), bits_to_skip % 8,
1836 			      this_size_bits, bits_big_endian);
1837 	      }
1838 	    else
1839 	      {
1840 		/* Write mode.  */
1841 		if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
1842 		  {
1843 		    if (this_size <= 8)
1844 		      {
1845 			/* Perform a single read for small sizes.  */
1846 			read_memory (start_addr, buffer.data (),
1847 				     this_size);
1848 		      }
1849 		    else
1850 		      {
1851 			/* Only the first and last bytes can possibly have
1852 			   any bits reused.  */
1853 			read_memory (start_addr, buffer.data (), 1);
1854 			read_memory (start_addr + this_size - 1,
1855 				     &buffer[this_size - 1], 1);
1856 		      }
1857 		  }
1858 
1859 		copy_bitwise (buffer.data (), bits_to_skip % 8,
1860 			      from_contents, offset,
1861 			      this_size_bits, bits_big_endian);
1862 		write_memory_with_notification (start_addr,
1863 						buffer.data (),
1864 						this_size);
1865 	      }
1866 	  }
1867 	  break;
1868 
1869 	case DWARF_VALUE_STACK:
1870 	  {
1871 	    if (from != NULL)
1872 	      {
1873 		mark_value_bits_optimized_out (v, offset, this_size_bits);
1874 		break;
1875 	      }
1876 
1877 	    gdbarch *objfile_gdbarch = c->per_objfile->objfile->arch ();
1878 	    ULONGEST stack_value_size_bits
1879 	      = 8 * TYPE_LENGTH (value_type (p->v.value));
1880 
1881 	    /* Use zeroes if piece reaches beyond stack value.  */
1882 	    if (p->offset + p->size > stack_value_size_bits)
1883 	      break;
1884 
1885 	    /* Piece is anchored at least significant bit end.  */
1886 	    if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
1887 	      bits_to_skip += stack_value_size_bits - p->offset - p->size;
1888 	    else
1889 	      bits_to_skip += p->offset;
1890 
1891 	    copy_bitwise (v_contents, offset,
1892 			  value_contents_all (p->v.value),
1893 			  bits_to_skip,
1894 			  this_size_bits, bits_big_endian);
1895 	  }
1896 	  break;
1897 
1898 	case DWARF_VALUE_LITERAL:
1899 	  {
1900 	    if (from != NULL)
1901 	      {
1902 		mark_value_bits_optimized_out (v, offset, this_size_bits);
1903 		break;
1904 	      }
1905 
1906 	    ULONGEST literal_size_bits = 8 * p->v.literal.length;
1907 	    size_t n = this_size_bits;
1908 
1909 	    /* Cut off at the end of the implicit value.  */
1910 	    bits_to_skip += p->offset;
1911 	    if (bits_to_skip >= literal_size_bits)
1912 	      break;
1913 	    if (n > literal_size_bits - bits_to_skip)
1914 	      n = literal_size_bits - bits_to_skip;
1915 
1916 	    copy_bitwise (v_contents, offset,
1917 			  p->v.literal.data, bits_to_skip,
1918 			  n, bits_big_endian);
1919 	  }
1920 	  break;
1921 
1922 	case DWARF_VALUE_IMPLICIT_POINTER:
1923 	    if (from != NULL)
1924 	      {
1925 		mark_value_bits_optimized_out (v, offset, this_size_bits);
1926 		break;
1927 	      }
1928 
1929 	  /* These bits show up as zeros -- but do not cause the value to
1930 	     be considered optimized-out.  */
1931 	  break;
1932 
1933 	case DWARF_VALUE_OPTIMIZED_OUT:
1934 	  mark_value_bits_optimized_out (v, offset, this_size_bits);
1935 	  break;
1936 
1937 	default:
1938 	  internal_error (__FILE__, __LINE__, _("invalid location type"));
1939 	}
1940 
1941       offset += this_size_bits;
1942       bits_to_skip = 0;
1943     }
1944 }
1945 
1946 
1947 static void
1948 read_pieced_value (struct value *v)
1949 {
1950   rw_pieced_value (v, NULL);
1951 }
1952 
1953 static void
1954 write_pieced_value (struct value *to, struct value *from)
1955 {
1956   rw_pieced_value (to, from);
1957 }
1958 
1959 /* An implementation of an lval_funcs method to see whether a value is
1960    a synthetic pointer.  */
1961 
1962 static int
1963 check_pieced_synthetic_pointer (const struct value *value, LONGEST bit_offset,
1964 				int bit_length)
1965 {
1966   struct piece_closure *c
1967     = (struct piece_closure *) value_computed_closure (value);
1968   int i;
1969 
1970   bit_offset += 8 * value_offset (value);
1971   if (value_bitsize (value))
1972     bit_offset += value_bitpos (value);
1973 
1974   for (i = 0; i < c->pieces.size () && bit_length > 0; i++)
1975     {
1976       struct dwarf_expr_piece *p = &c->pieces[i];
1977       size_t this_size_bits = p->size;
1978 
1979       if (bit_offset > 0)
1980 	{
1981 	  if (bit_offset >= this_size_bits)
1982 	    {
1983 	      bit_offset -= this_size_bits;
1984 	      continue;
1985 	    }
1986 
1987 	  bit_length -= this_size_bits - bit_offset;
1988 	  bit_offset = 0;
1989 	}
1990       else
1991 	bit_length -= this_size_bits;
1992 
1993       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1994 	return 0;
1995     }
1996 
1997   return 1;
1998 }
1999 
2000 /* Fetch a DW_AT_const_value through a synthetic pointer.  */
2001 
2002 static struct value *
2003 fetch_const_value_from_synthetic_pointer (sect_offset die, LONGEST byte_offset,
2004 					  dwarf2_per_cu_data *per_cu,
2005 					  dwarf2_per_objfile *per_objfile,
2006 					  struct type *type)
2007 {
2008   struct value *result = NULL;
2009   const gdb_byte *bytes;
2010   LONGEST len;
2011 
2012   auto_obstack temp_obstack;
2013   bytes = dwarf2_fetch_constant_bytes (die, per_cu, per_objfile,
2014 				       &temp_obstack, &len);
2015 
2016   if (bytes != NULL)
2017     {
2018       if (byte_offset >= 0
2019 	  && byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) <= len)
2020 	{
2021 	  bytes += byte_offset;
2022 	  result = value_from_contents (TYPE_TARGET_TYPE (type), bytes);
2023 	}
2024       else
2025 	invalid_synthetic_pointer ();
2026     }
2027   else
2028     result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type));
2029 
2030   return result;
2031 }
2032 
2033 /* Fetch the value pointed to by a synthetic pointer.  */
2034 
2035 static struct value *
2036 indirect_synthetic_pointer (sect_offset die, LONGEST byte_offset,
2037 			    dwarf2_per_cu_data *per_cu,
2038 			    dwarf2_per_objfile *per_objfile,
2039 			    struct frame_info *frame, struct type *type,
2040 			    bool resolve_abstract_p)
2041 {
2042   /* Fetch the location expression of the DIE we're pointing to.  */
2043   auto get_frame_address_in_block_wrapper = [frame] ()
2044     {
2045      return get_frame_address_in_block (frame);
2046     };
2047   struct dwarf2_locexpr_baton baton
2048     = dwarf2_fetch_die_loc_sect_off (die, per_cu, per_objfile,
2049 				     get_frame_address_in_block_wrapper,
2050 				     resolve_abstract_p);
2051 
2052   /* Get type of pointed-to DIE.  */
2053   struct type *orig_type = dwarf2_fetch_die_type_sect_off (die, per_cu,
2054 							   per_objfile);
2055   if (orig_type == NULL)
2056     invalid_synthetic_pointer ();
2057 
2058   /* If pointed-to DIE has a DW_AT_location, evaluate it and return the
2059      resulting value.  Otherwise, it may have a DW_AT_const_value instead,
2060      or it may've been optimized out.  */
2061   if (baton.data != NULL)
2062     return dwarf2_evaluate_loc_desc_full (orig_type, frame, baton.data,
2063 					  baton.size, baton.per_cu,
2064 					  baton.per_objfile,
2065 					  TYPE_TARGET_TYPE (type),
2066 					  byte_offset);
2067   else
2068     return fetch_const_value_from_synthetic_pointer (die, byte_offset, per_cu,
2069 						     per_objfile, type);
2070 }
2071 
2072 /* An implementation of an lval_funcs method to indirect through a
2073    pointer.  This handles the synthetic pointer case when needed.  */
2074 
2075 static struct value *
2076 indirect_pieced_value (struct value *value)
2077 {
2078   struct piece_closure *c
2079     = (struct piece_closure *) value_computed_closure (value);
2080   struct type *type;
2081   struct frame_info *frame;
2082   int i, bit_length;
2083   LONGEST bit_offset;
2084   struct dwarf_expr_piece *piece = NULL;
2085   LONGEST byte_offset;
2086   enum bfd_endian byte_order;
2087 
2088   type = check_typedef (value_type (value));
2089   if (type->code () != TYPE_CODE_PTR)
2090     return NULL;
2091 
2092   bit_length = 8 * TYPE_LENGTH (type);
2093   bit_offset = 8 * value_offset (value);
2094   if (value_bitsize (value))
2095     bit_offset += value_bitpos (value);
2096 
2097   for (i = 0; i < c->pieces.size () && bit_length > 0; i++)
2098     {
2099       struct dwarf_expr_piece *p = &c->pieces[i];
2100       size_t this_size_bits = p->size;
2101 
2102       if (bit_offset > 0)
2103 	{
2104 	  if (bit_offset >= this_size_bits)
2105 	    {
2106 	      bit_offset -= this_size_bits;
2107 	      continue;
2108 	    }
2109 
2110 	  bit_length -= this_size_bits - bit_offset;
2111 	  bit_offset = 0;
2112 	}
2113       else
2114 	bit_length -= this_size_bits;
2115 
2116       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2117 	return NULL;
2118 
2119       if (bit_length != 0)
2120 	error (_("Invalid use of DW_OP_implicit_pointer"));
2121 
2122       piece = p;
2123       break;
2124     }
2125 
2126   gdb_assert (piece != NULL);
2127   frame = get_selected_frame (_("No frame selected."));
2128 
2129   /* This is an offset requested by GDB, such as value subscripts.
2130      However, due to how synthetic pointers are implemented, this is
2131      always presented to us as a pointer type.  This means we have to
2132      sign-extend it manually as appropriate.  Use raw
2133      extract_signed_integer directly rather than value_as_address and
2134      sign extend afterwards on architectures that would need it
2135      (mostly everywhere except MIPS, which has signed addresses) as
2136      the later would go through gdbarch_pointer_to_address and thus
2137      return a CORE_ADDR with high bits set on architectures that
2138      encode address spaces and other things in CORE_ADDR.  */
2139   byte_order = gdbarch_byte_order (get_frame_arch (frame));
2140   byte_offset = extract_signed_integer (value_contents (value),
2141 					TYPE_LENGTH (type), byte_order);
2142   byte_offset += piece->v.ptr.offset;
2143 
2144   return indirect_synthetic_pointer (piece->v.ptr.die_sect_off,
2145 				     byte_offset, c->per_cu,
2146 				     c->per_objfile, frame, type);
2147 }
2148 
2149 /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
2150    references.  */
2151 
2152 static struct value *
2153 coerce_pieced_ref (const struct value *value)
2154 {
2155   struct type *type = check_typedef (value_type (value));
2156 
2157   if (value_bits_synthetic_pointer (value, value_embedded_offset (value),
2158 				    TARGET_CHAR_BIT * TYPE_LENGTH (type)))
2159     {
2160       const struct piece_closure *closure
2161 	= (struct piece_closure *) value_computed_closure (value);
2162       struct frame_info *frame
2163 	= get_selected_frame (_("No frame selected."));
2164 
2165       /* gdb represents synthetic pointers as pieced values with a single
2166 	 piece.  */
2167       gdb_assert (closure != NULL);
2168       gdb_assert (closure->pieces.size () == 1);
2169 
2170       return indirect_synthetic_pointer
2171 	(closure->pieces[0].v.ptr.die_sect_off,
2172 	 closure->pieces[0].v.ptr.offset,
2173 	 closure->per_cu, closure->per_objfile, frame, type);
2174     }
2175   else
2176     {
2177       /* Else: not a synthetic reference; do nothing.  */
2178       return NULL;
2179     }
2180 }
2181 
2182 static void *
2183 copy_pieced_value_closure (const struct value *v)
2184 {
2185   struct piece_closure *c
2186     = (struct piece_closure *) value_computed_closure (v);
2187 
2188   ++c->refc;
2189   return c;
2190 }
2191 
2192 static void
2193 free_pieced_value_closure (struct value *v)
2194 {
2195   struct piece_closure *c
2196     = (struct piece_closure *) value_computed_closure (v);
2197 
2198   --c->refc;
2199   if (c->refc == 0)
2200     {
2201       for (dwarf_expr_piece &p : c->pieces)
2202 	if (p.location == DWARF_VALUE_STACK)
2203 	  value_decref (p.v.value);
2204 
2205       delete c;
2206     }
2207 }
2208 
2209 /* Functions for accessing a variable described by DW_OP_piece.  */
2210 static const struct lval_funcs pieced_value_funcs = {
2211   read_pieced_value,
2212   write_pieced_value,
2213   indirect_pieced_value,
2214   coerce_pieced_ref,
2215   check_pieced_synthetic_pointer,
2216   copy_pieced_value_closure,
2217   free_pieced_value_closure
2218 };
2219 
2220 /* Evaluate a location description, starting at DATA and with length
2221    SIZE, to find the current location of variable of TYPE in the
2222    context of FRAME.  If SUBOBJ_TYPE is non-NULL, return instead the
2223    location of the subobject of type SUBOBJ_TYPE at byte offset
2224    SUBOBJ_BYTE_OFFSET within the variable of type TYPE.  */
2225 
2226 static struct value *
2227 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2228 			       const gdb_byte *data, size_t size,
2229 			       dwarf2_per_cu_data *per_cu,
2230 			       dwarf2_per_objfile *per_objfile,
2231 			       struct type *subobj_type,
2232 			       LONGEST subobj_byte_offset)
2233 {
2234   struct value *retval;
2235 
2236   if (subobj_type == NULL)
2237     {
2238       subobj_type = type;
2239       subobj_byte_offset = 0;
2240     }
2241   else if (subobj_byte_offset < 0)
2242     invalid_synthetic_pointer ();
2243 
2244   if (size == 0)
2245     return allocate_optimized_out_value (subobj_type);
2246 
2247   dwarf_evaluate_loc_desc ctx (per_objfile);
2248   ctx.frame = frame;
2249   ctx.per_cu = per_cu;
2250   ctx.obj_address = 0;
2251 
2252   scoped_value_mark free_values;
2253 
2254   ctx.gdbarch = per_objfile->objfile->arch ();
2255   ctx.addr_size = per_cu->addr_size ();
2256   ctx.ref_addr_size = per_cu->ref_addr_size ();
2257 
2258   try
2259     {
2260       ctx.eval (data, size);
2261     }
2262   catch (const gdb_exception_error &ex)
2263     {
2264       if (ex.error == NOT_AVAILABLE_ERROR)
2265 	{
2266 	  free_values.free_to_mark ();
2267 	  retval = allocate_value (subobj_type);
2268 	  mark_value_bytes_unavailable (retval, 0,
2269 					TYPE_LENGTH (subobj_type));
2270 	  return retval;
2271 	}
2272       else if (ex.error == NO_ENTRY_VALUE_ERROR)
2273 	{
2274 	  if (entry_values_debug)
2275 	    exception_print (gdb_stdout, ex);
2276 	  free_values.free_to_mark ();
2277 	  return allocate_optimized_out_value (subobj_type);
2278 	}
2279       else
2280 	throw;
2281     }
2282 
2283   if (ctx.pieces.size () > 0)
2284     {
2285       struct piece_closure *c;
2286       ULONGEST bit_size = 0;
2287 
2288       for (dwarf_expr_piece &piece : ctx.pieces)
2289 	bit_size += piece.size;
2290       /* Complain if the expression is larger than the size of the
2291 	 outer type.  */
2292       if (bit_size > 8 * TYPE_LENGTH (type))
2293 	invalid_synthetic_pointer ();
2294 
2295       c = allocate_piece_closure (per_cu, per_objfile, std::move (ctx.pieces),
2296 				  frame);
2297       /* We must clean up the value chain after creating the piece
2298 	 closure but before allocating the result.  */
2299       free_values.free_to_mark ();
2300       retval = allocate_computed_value (subobj_type,
2301 					&pieced_value_funcs, c);
2302       set_value_offset (retval, subobj_byte_offset);
2303     }
2304   else
2305     {
2306       switch (ctx.location)
2307 	{
2308 	case DWARF_VALUE_REGISTER:
2309 	  {
2310 	    struct gdbarch *arch = get_frame_arch (frame);
2311 	    int dwarf_regnum
2312 	      = longest_to_int (value_as_long (ctx.fetch (0)));
2313 	    int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, dwarf_regnum);
2314 
2315 	    if (subobj_byte_offset != 0)
2316 	      error (_("cannot use offset on synthetic pointer to register"));
2317 	    free_values.free_to_mark ();
2318 	    retval = value_from_register (subobj_type, gdb_regnum, frame);
2319 	    if (value_optimized_out (retval))
2320 	      {
2321 		struct value *tmp;
2322 
2323 		/* This means the register has undefined value / was
2324 		   not saved.  As we're computing the location of some
2325 		   variable etc. in the program, not a value for
2326 		   inspecting a register ($pc, $sp, etc.), return a
2327 		   generic optimized out value instead, so that we show
2328 		   <optimized out> instead of <not saved>.  */
2329 		tmp = allocate_value (subobj_type);
2330 		value_contents_copy (tmp, 0, retval, 0,
2331 				     TYPE_LENGTH (subobj_type));
2332 		retval = tmp;
2333 	      }
2334 	  }
2335 	  break;
2336 
2337 	case DWARF_VALUE_MEMORY:
2338 	  {
2339 	    struct type *ptr_type;
2340 	    CORE_ADDR address = ctx.fetch_address (0);
2341 	    bool in_stack_memory = ctx.fetch_in_stack_memory (0);
2342 
2343 	    /* DW_OP_deref_size (and possibly other operations too) may
2344 	       create a pointer instead of an address.  Ideally, the
2345 	       pointer to address conversion would be performed as part
2346 	       of those operations, but the type of the object to
2347 	       which the address refers is not known at the time of
2348 	       the operation.  Therefore, we do the conversion here
2349 	       since the type is readily available.  */
2350 
2351 	    switch (subobj_type->code ())
2352 	      {
2353 		case TYPE_CODE_FUNC:
2354 		case TYPE_CODE_METHOD:
2355 		  ptr_type = builtin_type (ctx.gdbarch)->builtin_func_ptr;
2356 		  break;
2357 		default:
2358 		  ptr_type = builtin_type (ctx.gdbarch)->builtin_data_ptr;
2359 		  break;
2360 	      }
2361 	    address = value_as_address (value_from_pointer (ptr_type, address));
2362 
2363 	    free_values.free_to_mark ();
2364 	    retval = value_at_lazy (subobj_type,
2365 				    address + subobj_byte_offset);
2366 	    if (in_stack_memory)
2367 	      set_value_stack (retval, 1);
2368 	  }
2369 	  break;
2370 
2371 	case DWARF_VALUE_STACK:
2372 	  {
2373 	    struct value *value = ctx.fetch (0);
2374 	    size_t n = TYPE_LENGTH (value_type (value));
2375 	    size_t len = TYPE_LENGTH (subobj_type);
2376 	    size_t max = TYPE_LENGTH (type);
2377 	    gdbarch *objfile_gdbarch = per_objfile->objfile->arch ();
2378 
2379 	    if (subobj_byte_offset + len > max)
2380 	      invalid_synthetic_pointer ();
2381 
2382 	    /* Preserve VALUE because we are going to free values back
2383 	       to the mark, but we still need the value contents
2384 	       below.  */
2385 	    value_ref_ptr value_holder = value_ref_ptr::new_reference (value);
2386 	    free_values.free_to_mark ();
2387 
2388 	    retval = allocate_value (subobj_type);
2389 
2390 	    /* The given offset is relative to the actual object.  */
2391 	    if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2392 	      subobj_byte_offset += n - max;
2393 
2394 	    memcpy (value_contents_raw (retval),
2395 		    value_contents_all (value) + subobj_byte_offset, len);
2396 	  }
2397 	  break;
2398 
2399 	case DWARF_VALUE_LITERAL:
2400 	  {
2401 	    bfd_byte *contents;
2402 	    size_t n = TYPE_LENGTH (subobj_type);
2403 
2404 	    if (subobj_byte_offset + n > ctx.len)
2405 	      invalid_synthetic_pointer ();
2406 
2407 	    free_values.free_to_mark ();
2408 	    retval = allocate_value (subobj_type);
2409 	    contents = value_contents_raw (retval);
2410 	    memcpy (contents, ctx.data + subobj_byte_offset, n);
2411 	  }
2412 	  break;
2413 
2414 	case DWARF_VALUE_OPTIMIZED_OUT:
2415 	  free_values.free_to_mark ();
2416 	  retval = allocate_optimized_out_value (subobj_type);
2417 	  break;
2418 
2419 	  /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2420 	     operation by execute_stack_op.  */
2421 	case DWARF_VALUE_IMPLICIT_POINTER:
2422 	  /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2423 	     it can only be encountered when making a piece.  */
2424 	default:
2425 	  internal_error (__FILE__, __LINE__, _("invalid location type"));
2426 	}
2427     }
2428 
2429   set_value_initialized (retval, ctx.initialized);
2430 
2431   return retval;
2432 }
2433 
2434 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2435    passes 0 as the byte_offset.  */
2436 
2437 struct value *
2438 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2439 			  const gdb_byte *data, size_t size,
2440 			  dwarf2_per_cu_data *per_cu,
2441 			  dwarf2_per_objfile *per_objfile)
2442 {
2443   return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu,
2444 					per_objfile, NULL, 0);
2445 }
2446 
2447 /* A specialization of dwarf_evaluate_loc_desc that is used by
2448    dwarf2_locexpr_baton_eval.  This subclass exists to handle the case
2449    where a caller of dwarf2_locexpr_baton_eval passes in some data,
2450    but with the address being 0.  In this situation, we arrange for
2451    memory reads to come from the passed-in buffer.  */
2452 
2453 struct evaluate_for_locexpr_baton : public dwarf_evaluate_loc_desc
2454 {
2455   evaluate_for_locexpr_baton (dwarf2_per_objfile *per_objfile)
2456     : dwarf_evaluate_loc_desc (per_objfile)
2457   {}
2458 
2459   /* The data that was passed in.  */
2460   gdb::array_view<const gdb_byte> data_view;
2461 
2462   CORE_ADDR get_object_address () override
2463   {
2464     if (data_view.data () == nullptr && obj_address == 0)
2465       error (_("Location address is not set."));
2466     return obj_address;
2467   }
2468 
2469   void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t len) override
2470   {
2471     if (len == 0)
2472       return;
2473 
2474     /* Prefer the passed-in memory, if it exists.  */
2475     CORE_ADDR offset = addr - obj_address;
2476     if (offset < data_view.size () && offset + len <= data_view.size ())
2477       {
2478 	memcpy (buf, data_view.data (), len);
2479 	return;
2480       }
2481 
2482     read_memory (addr, buf, len);
2483   }
2484 };
2485 
2486 /* Evaluates a dwarf expression and stores the result in VAL,
2487    expecting that the dwarf expression only produces a single
2488    CORE_ADDR.  FRAME is the frame in which the expression is
2489    evaluated.  ADDR_STACK is a context (location of a variable) and
2490    might be needed to evaluate the location expression.
2491    PUSH_INITIAL_VALUE is true if the address (either from ADDR_STACK,
2492    or the default of 0) should be pushed on the DWARF expression
2493    evaluation stack before evaluating the expression; this is required
2494    by certain forms of DWARF expression.  Returns 1 on success, 0
2495    otherwise.  */
2496 
2497 static int
2498 dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton *dlbaton,
2499 			   struct frame_info *frame,
2500 			   const struct property_addr_info *addr_stack,
2501 			   CORE_ADDR *valp,
2502 			   bool push_initial_value)
2503 {
2504   if (dlbaton == NULL || dlbaton->size == 0)
2505     return 0;
2506 
2507   dwarf2_per_objfile *per_objfile = dlbaton->per_objfile;
2508   evaluate_for_locexpr_baton ctx (per_objfile);
2509 
2510   ctx.frame = frame;
2511   ctx.per_cu = dlbaton->per_cu;
2512   if (addr_stack == nullptr)
2513     ctx.obj_address = 0;
2514   else
2515     {
2516       ctx.obj_address = addr_stack->addr;
2517       ctx.data_view = addr_stack->valaddr;
2518     }
2519 
2520   ctx.gdbarch = per_objfile->objfile->arch ();
2521   ctx.addr_size = dlbaton->per_cu->addr_size ();
2522   ctx.ref_addr_size = dlbaton->per_cu->ref_addr_size ();
2523 
2524   if (push_initial_value)
2525     ctx.push_address (ctx.obj_address, false);
2526 
2527   try
2528     {
2529       ctx.eval (dlbaton->data, dlbaton->size);
2530     }
2531   catch (const gdb_exception_error &ex)
2532     {
2533       if (ex.error == NOT_AVAILABLE_ERROR)
2534 	{
2535 	  return 0;
2536 	}
2537       else if (ex.error == NO_ENTRY_VALUE_ERROR)
2538 	{
2539 	  if (entry_values_debug)
2540 	    exception_print (gdb_stdout, ex);
2541 	  return 0;
2542 	}
2543       else
2544 	throw;
2545     }
2546 
2547   switch (ctx.location)
2548     {
2549     case DWARF_VALUE_REGISTER:
2550     case DWARF_VALUE_MEMORY:
2551     case DWARF_VALUE_STACK:
2552       *valp = ctx.fetch_address (0);
2553       if (ctx.location == DWARF_VALUE_REGISTER)
2554 	*valp = ctx.read_addr_from_reg (*valp);
2555       return 1;
2556     case DWARF_VALUE_LITERAL:
2557       *valp = extract_signed_integer (ctx.data, ctx.len,
2558 				      gdbarch_byte_order (ctx.gdbarch));
2559       return 1;
2560       /* Unsupported dwarf values.  */
2561     case DWARF_VALUE_OPTIMIZED_OUT:
2562     case DWARF_VALUE_IMPLICIT_POINTER:
2563       break;
2564     }
2565 
2566   return 0;
2567 }
2568 
2569 /* See dwarf2loc.h.  */
2570 
2571 bool
2572 dwarf2_evaluate_property (const struct dynamic_prop *prop,
2573 			  struct frame_info *frame,
2574 			  const struct property_addr_info *addr_stack,
2575 			  CORE_ADDR *value,
2576 			  bool push_initial_value)
2577 {
2578   if (prop == NULL)
2579     return false;
2580 
2581   if (frame == NULL && has_stack_frames ())
2582     frame = get_selected_frame (NULL);
2583 
2584   switch (prop->kind ())
2585     {
2586     case PROP_LOCEXPR:
2587       {
2588 	const struct dwarf2_property_baton *baton
2589 	  = (const struct dwarf2_property_baton *) prop->baton ();
2590 	gdb_assert (baton->property_type != NULL);
2591 
2592 	if (dwarf2_locexpr_baton_eval (&baton->locexpr, frame, addr_stack,
2593 				       value, push_initial_value))
2594 	  {
2595 	    if (baton->locexpr.is_reference)
2596 	      {
2597 		struct value *val = value_at (baton->property_type, *value);
2598 		*value = value_as_address (val);
2599 	      }
2600 	    else
2601 	      {
2602 		gdb_assert (baton->property_type != NULL);
2603 
2604 		struct type *type = check_typedef (baton->property_type);
2605 		if (TYPE_LENGTH (type) < sizeof (CORE_ADDR)
2606 		    && !TYPE_UNSIGNED (type))
2607 		  {
2608 		    /* If we have a valid return candidate and it's value
2609 		       is signed, we have to sign-extend the value because
2610 		       CORE_ADDR on 64bit machine has 8 bytes but address
2611 		       size of an 32bit application is bytes.  */
2612 		    const int addr_size
2613 		      = (baton->locexpr.per_cu->addr_size ()
2614 			 * TARGET_CHAR_BIT);
2615 		    const CORE_ADDR neg_mask
2616 		      = (~((CORE_ADDR) 0) <<  (addr_size - 1));
2617 
2618 		    /* Check if signed bit is set and sign-extend values.  */
2619 		    if (*value & neg_mask)
2620 		      *value |= neg_mask;
2621 		  }
2622 	      }
2623 	    return true;
2624 	  }
2625       }
2626       break;
2627 
2628     case PROP_LOCLIST:
2629       {
2630 	struct dwarf2_property_baton *baton
2631 	  = (struct dwarf2_property_baton *) prop->baton ();
2632 	CORE_ADDR pc;
2633 	const gdb_byte *data;
2634 	struct value *val;
2635 	size_t size;
2636 
2637 	if (frame == NULL
2638 	    || !get_frame_address_in_block_if_available (frame, &pc))
2639 	  return false;
2640 
2641 	data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2642 	if (data != NULL)
2643 	  {
2644 	    val = dwarf2_evaluate_loc_desc (baton->property_type, frame, data,
2645 					    size, baton->loclist.per_cu,
2646 					    baton->loclist.per_objfile);
2647 	    if (!value_optimized_out (val))
2648 	      {
2649 		*value = value_as_address (val);
2650 		return true;
2651 	      }
2652 	  }
2653       }
2654       break;
2655 
2656     case PROP_CONST:
2657       *value = prop->const_val ();
2658       return true;
2659 
2660     case PROP_ADDR_OFFSET:
2661       {
2662 	struct dwarf2_property_baton *baton
2663 	  = (struct dwarf2_property_baton *) prop->baton ();
2664 	const struct property_addr_info *pinfo;
2665 	struct value *val;
2666 
2667 	for (pinfo = addr_stack; pinfo != NULL; pinfo = pinfo->next)
2668 	  {
2669 	    /* This approach lets us avoid checking the qualifiers.  */
2670 	    if (TYPE_MAIN_TYPE (pinfo->type)
2671 		== TYPE_MAIN_TYPE (baton->property_type))
2672 	      break;
2673 	  }
2674 	if (pinfo == NULL)
2675 	  error (_("cannot find reference address for offset property"));
2676 	if (pinfo->valaddr.data () != NULL)
2677 	  val = value_from_contents
2678 		  (baton->offset_info.type,
2679 		   pinfo->valaddr.data () + baton->offset_info.offset);
2680 	else
2681 	  val = value_at (baton->offset_info.type,
2682 			  pinfo->addr + baton->offset_info.offset);
2683 	*value = value_as_address (val);
2684 	return true;
2685       }
2686     }
2687 
2688   return false;
2689 }
2690 
2691 /* See dwarf2loc.h.  */
2692 
2693 void
2694 dwarf2_compile_property_to_c (string_file *stream,
2695 			      const char *result_name,
2696 			      struct gdbarch *gdbarch,
2697 			      unsigned char *registers_used,
2698 			      const struct dynamic_prop *prop,
2699 			      CORE_ADDR pc,
2700 			      struct symbol *sym)
2701 {
2702   struct dwarf2_property_baton *baton
2703     = (struct dwarf2_property_baton *) prop->baton ();
2704   const gdb_byte *data;
2705   size_t size;
2706   dwarf2_per_cu_data *per_cu;
2707   dwarf2_per_objfile *per_objfile;
2708 
2709   if (prop->kind () == PROP_LOCEXPR)
2710     {
2711       data = baton->locexpr.data;
2712       size = baton->locexpr.size;
2713       per_cu = baton->locexpr.per_cu;
2714       per_objfile = baton->locexpr.per_objfile;
2715     }
2716   else
2717     {
2718       gdb_assert (prop->kind () == PROP_LOCLIST);
2719 
2720       data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2721       per_cu = baton->loclist.per_cu;
2722       per_objfile = baton->loclist.per_objfile;
2723     }
2724 
2725   compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
2726 			     gdbarch, registers_used,
2727 			     per_cu->addr_size (),
2728 			     data, data + size, per_cu, per_objfile);
2729 }
2730 
2731 
2732 /* Helper functions and baton for dwarf2_loc_desc_get_symbol_read_needs.  */
2733 
2734 class symbol_needs_eval_context : public dwarf_expr_context
2735 {
2736 public:
2737   symbol_needs_eval_context (dwarf2_per_objfile *per_objfile)
2738     : dwarf_expr_context (per_objfile)
2739   {}
2740 
2741   enum symbol_needs_kind needs;
2742   struct dwarf2_per_cu_data *per_cu;
2743 
2744   /* Reads from registers do require a frame.  */
2745   CORE_ADDR read_addr_from_reg (int regnum) override
2746   {
2747     needs = SYMBOL_NEEDS_FRAME;
2748     return 1;
2749   }
2750 
2751   /* "get_reg_value" callback: Reads from registers do require a
2752      frame.  */
2753 
2754   struct value *get_reg_value (struct type *type, int regnum) override
2755   {
2756     needs = SYMBOL_NEEDS_FRAME;
2757     return value_zero (type, not_lval);
2758   }
2759 
2760   /* Reads from memory do not require a frame.  */
2761   void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t len) override
2762   {
2763     memset (buf, 0, len);
2764   }
2765 
2766   /* Frame-relative accesses do require a frame.  */
2767   void get_frame_base (const gdb_byte **start, size_t *length) override
2768   {
2769     static gdb_byte lit0 = DW_OP_lit0;
2770 
2771     *start = &lit0;
2772     *length = 1;
2773 
2774     needs = SYMBOL_NEEDS_FRAME;
2775   }
2776 
2777   /* CFA accesses require a frame.  */
2778   CORE_ADDR get_frame_cfa () override
2779   {
2780     needs = SYMBOL_NEEDS_FRAME;
2781     return 1;
2782   }
2783 
2784   CORE_ADDR get_frame_pc () override
2785   {
2786     needs = SYMBOL_NEEDS_FRAME;
2787     return 1;
2788   }
2789 
2790   /* Thread-local accesses require registers, but not a frame.  */
2791   CORE_ADDR get_tls_address (CORE_ADDR offset) override
2792   {
2793     if (needs <= SYMBOL_NEEDS_REGISTERS)
2794       needs = SYMBOL_NEEDS_REGISTERS;
2795     return 1;
2796   }
2797 
2798   /* Helper interface of per_cu_dwarf_call for
2799      dwarf2_loc_desc_get_symbol_read_needs.  */
2800 
2801   void dwarf_call (cu_offset die_offset) override
2802   {
2803     per_cu_dwarf_call (this, die_offset, per_cu, per_objfile);
2804   }
2805 
2806   /* Helper interface of sect_variable_value for
2807      dwarf2_loc_desc_get_symbol_read_needs.  */
2808 
2809   struct value *dwarf_variable_value (sect_offset sect_off) override
2810   {
2811     return sect_variable_value (this, sect_off, per_cu, per_objfile);
2812   }
2813 
2814   /* DW_OP_entry_value accesses require a caller, therefore a
2815      frame.  */
2816 
2817   void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind,
2818 				   union call_site_parameter_u kind_u,
2819 				   int deref_size) override
2820   {
2821     needs = SYMBOL_NEEDS_FRAME;
2822 
2823     /* The expression may require some stub values on DWARF stack.  */
2824     push_address (0, 0);
2825   }
2826 
2827   /* DW_OP_addrx and DW_OP_GNU_addr_index doesn't require a frame.  */
2828 
2829   CORE_ADDR get_addr_index (unsigned int index) override
2830   {
2831     /* Nothing to do.  */
2832     return 1;
2833   }
2834 
2835   /* DW_OP_push_object_address has a frame already passed through.  */
2836 
2837   CORE_ADDR get_object_address () override
2838   {
2839     /* Nothing to do.  */
2840     return 1;
2841   }
2842 };
2843 
2844 /* Compute the correct symbol_needs_kind value for the location
2845    expression at DATA (length SIZE).  */
2846 
2847 static enum symbol_needs_kind
2848 dwarf2_loc_desc_get_symbol_read_needs (const gdb_byte *data, size_t size,
2849 				       dwarf2_per_cu_data *per_cu,
2850 				       dwarf2_per_objfile *per_objfile)
2851 {
2852   scoped_value_mark free_values;
2853 
2854   symbol_needs_eval_context ctx (per_objfile);
2855 
2856   ctx.needs = SYMBOL_NEEDS_NONE;
2857   ctx.per_cu = per_cu;
2858   ctx.gdbarch = per_objfile->objfile->arch ();
2859   ctx.addr_size = per_cu->addr_size ();
2860   ctx.ref_addr_size = per_cu->ref_addr_size ();
2861 
2862   ctx.eval (data, size);
2863 
2864   bool in_reg = ctx.location == DWARF_VALUE_REGISTER;
2865 
2866   /* If the location has several pieces, and any of them are in
2867      registers, then we will need a frame to fetch them from.  */
2868   for (dwarf_expr_piece &p : ctx.pieces)
2869     if (p.location == DWARF_VALUE_REGISTER)
2870       in_reg = true;
2871 
2872   if (in_reg)
2873     ctx.needs = SYMBOL_NEEDS_FRAME;
2874 
2875   return ctx.needs;
2876 }
2877 
2878 /* A helper function that throws an unimplemented error mentioning a
2879    given DWARF operator.  */
2880 
2881 static void ATTRIBUTE_NORETURN
2882 unimplemented (unsigned int op)
2883 {
2884   const char *name = get_DW_OP_name (op);
2885 
2886   if (name)
2887     error (_("DWARF operator %s cannot be translated to an agent expression"),
2888 	   name);
2889   else
2890     error (_("Unknown DWARF operator 0x%02x cannot be translated "
2891 	     "to an agent expression"),
2892 	   op);
2893 }
2894 
2895 /* See dwarf2loc.h.
2896 
2897    This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2898    can issue a complaint, which is better than having every target's
2899    implementation of dwarf2_reg_to_regnum do it.  */
2900 
2901 int
2902 dwarf_reg_to_regnum (struct gdbarch *arch, int dwarf_reg)
2903 {
2904   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2905 
2906   if (reg == -1)
2907     {
2908       complaint (_("bad DWARF register number %d"), dwarf_reg);
2909     }
2910   return reg;
2911 }
2912 
2913 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2914    Throw an error because DWARF_REG is bad.  */
2915 
2916 static void
2917 throw_bad_regnum_error (ULONGEST dwarf_reg)
2918 {
2919   /* Still want to print -1 as "-1".
2920      We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2921      but that's overkill for now.  */
2922   if ((int) dwarf_reg == dwarf_reg)
2923     error (_("Unable to access DWARF register number %d"), (int) dwarf_reg);
2924   error (_("Unable to access DWARF register number %s"),
2925 	 pulongest (dwarf_reg));
2926 }
2927 
2928 /* See dwarf2loc.h.  */
2929 
2930 int
2931 dwarf_reg_to_regnum_or_error (struct gdbarch *arch, ULONGEST dwarf_reg)
2932 {
2933   int reg;
2934 
2935   if (dwarf_reg > INT_MAX)
2936     throw_bad_regnum_error (dwarf_reg);
2937   /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2938      bad, but that's ok.  */
2939   reg = dwarf_reg_to_regnum (arch, (int) dwarf_reg);
2940   if (reg == -1)
2941     throw_bad_regnum_error (dwarf_reg);
2942   return reg;
2943 }
2944 
2945 /* A helper function that emits an access to memory.  ARCH is the
2946    target architecture.  EXPR is the expression which we are building.
2947    NBITS is the number of bits we want to read.  This emits the
2948    opcodes needed to read the memory and then extract the desired
2949    bits.  */
2950 
2951 static void
2952 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2953 {
2954   ULONGEST nbytes = (nbits + 7) / 8;
2955 
2956   gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
2957 
2958   if (expr->tracing)
2959     ax_trace_quick (expr, nbytes);
2960 
2961   if (nbits <= 8)
2962     ax_simple (expr, aop_ref8);
2963   else if (nbits <= 16)
2964     ax_simple (expr, aop_ref16);
2965   else if (nbits <= 32)
2966     ax_simple (expr, aop_ref32);
2967   else
2968     ax_simple (expr, aop_ref64);
2969 
2970   /* If we read exactly the number of bytes we wanted, we're done.  */
2971   if (8 * nbytes == nbits)
2972     return;
2973 
2974   if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
2975     {
2976       /* On a bits-big-endian machine, we want the high-order
2977 	 NBITS.  */
2978       ax_const_l (expr, 8 * nbytes - nbits);
2979       ax_simple (expr, aop_rsh_unsigned);
2980     }
2981   else
2982     {
2983       /* On a bits-little-endian box, we want the low-order NBITS.  */
2984       ax_zero_ext (expr, nbits);
2985     }
2986 }
2987 
2988 /* Compile a DWARF location expression to an agent expression.
2989 
2990    EXPR is the agent expression we are building.
2991    LOC is the agent value we modify.
2992    ARCH is the architecture.
2993    ADDR_SIZE is the size of addresses, in bytes.
2994    OP_PTR is the start of the location expression.
2995    OP_END is one past the last byte of the location expression.
2996 
2997    This will throw an exception for various kinds of errors -- for
2998    example, if the expression cannot be compiled, or if the expression
2999    is invalid.  */
3000 
3001 static void
3002 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
3003 			   unsigned int addr_size, const gdb_byte *op_ptr,
3004 			   const gdb_byte *op_end,
3005 			   dwarf2_per_cu_data *per_cu,
3006 			   dwarf2_per_objfile *per_objfile)
3007 {
3008   gdbarch *arch = expr->gdbarch;
3009   std::vector<int> dw_labels, patches;
3010   const gdb_byte * const base = op_ptr;
3011   const gdb_byte *previous_piece = op_ptr;
3012   enum bfd_endian byte_order = gdbarch_byte_order (arch);
3013   ULONGEST bits_collected = 0;
3014   unsigned int addr_size_bits = 8 * addr_size;
3015   bool bits_big_endian = byte_order == BFD_ENDIAN_BIG;
3016 
3017   std::vector<int> offsets (op_end - op_ptr, -1);
3018 
3019   /* By default we are making an address.  */
3020   loc->kind = axs_lvalue_memory;
3021 
3022   while (op_ptr < op_end)
3023     {
3024       enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
3025       uint64_t uoffset, reg;
3026       int64_t offset;
3027       int i;
3028 
3029       offsets[op_ptr - base] = expr->len;
3030       ++op_ptr;
3031 
3032       /* Our basic approach to code generation is to map DWARF
3033 	 operations directly to AX operations.  However, there are
3034 	 some differences.
3035 
3036 	 First, DWARF works on address-sized units, but AX always uses
3037 	 LONGEST.  For most operations we simply ignore this
3038 	 difference; instead we generate sign extensions as needed
3039 	 before division and comparison operations.  It would be nice
3040 	 to omit the sign extensions, but there is no way to determine
3041 	 the size of the target's LONGEST.  (This code uses the size
3042 	 of the host LONGEST in some cases -- that is a bug but it is
3043 	 difficult to fix.)
3044 
3045 	 Second, some DWARF operations cannot be translated to AX.
3046 	 For these we simply fail.  See
3047 	 http://sourceware.org/bugzilla/show_bug.cgi?id=11662.  */
3048       switch (op)
3049 	{
3050 	case DW_OP_lit0:
3051 	case DW_OP_lit1:
3052 	case DW_OP_lit2:
3053 	case DW_OP_lit3:
3054 	case DW_OP_lit4:
3055 	case DW_OP_lit5:
3056 	case DW_OP_lit6:
3057 	case DW_OP_lit7:
3058 	case DW_OP_lit8:
3059 	case DW_OP_lit9:
3060 	case DW_OP_lit10:
3061 	case DW_OP_lit11:
3062 	case DW_OP_lit12:
3063 	case DW_OP_lit13:
3064 	case DW_OP_lit14:
3065 	case DW_OP_lit15:
3066 	case DW_OP_lit16:
3067 	case DW_OP_lit17:
3068 	case DW_OP_lit18:
3069 	case DW_OP_lit19:
3070 	case DW_OP_lit20:
3071 	case DW_OP_lit21:
3072 	case DW_OP_lit22:
3073 	case DW_OP_lit23:
3074 	case DW_OP_lit24:
3075 	case DW_OP_lit25:
3076 	case DW_OP_lit26:
3077 	case DW_OP_lit27:
3078 	case DW_OP_lit28:
3079 	case DW_OP_lit29:
3080 	case DW_OP_lit30:
3081 	case DW_OP_lit31:
3082 	  ax_const_l (expr, op - DW_OP_lit0);
3083 	  break;
3084 
3085 	case DW_OP_addr:
3086 	  uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
3087 	  op_ptr += addr_size;
3088 	  /* Some versions of GCC emit DW_OP_addr before
3089 	     DW_OP_GNU_push_tls_address.  In this case the value is an
3090 	     index, not an address.  We don't support things like
3091 	     branching between the address and the TLS op.  */
3092 	  if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
3093 	    uoffset += per_objfile->objfile->text_section_offset ();
3094 	  ax_const_l (expr, uoffset);
3095 	  break;
3096 
3097 	case DW_OP_const1u:
3098 	  ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
3099 	  op_ptr += 1;
3100 	  break;
3101 
3102 	case DW_OP_const1s:
3103 	  ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
3104 	  op_ptr += 1;
3105 	  break;
3106 
3107 	case DW_OP_const2u:
3108 	  ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
3109 	  op_ptr += 2;
3110 	  break;
3111 
3112 	case DW_OP_const2s:
3113 	  ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
3114 	  op_ptr += 2;
3115 	  break;
3116 
3117 	case DW_OP_const4u:
3118 	  ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
3119 	  op_ptr += 4;
3120 	  break;
3121 
3122 	case DW_OP_const4s:
3123 	  ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
3124 	  op_ptr += 4;
3125 	  break;
3126 
3127 	case DW_OP_const8u:
3128 	  ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
3129 	  op_ptr += 8;
3130 	  break;
3131 
3132 	case DW_OP_const8s:
3133 	  ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
3134 	  op_ptr += 8;
3135 	  break;
3136 
3137 	case DW_OP_constu:
3138 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3139 	  ax_const_l (expr, uoffset);
3140 	  break;
3141 
3142 	case DW_OP_consts:
3143 	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3144 	  ax_const_l (expr, offset);
3145 	  break;
3146 
3147 	case DW_OP_reg0:
3148 	case DW_OP_reg1:
3149 	case DW_OP_reg2:
3150 	case DW_OP_reg3:
3151 	case DW_OP_reg4:
3152 	case DW_OP_reg5:
3153 	case DW_OP_reg6:
3154 	case DW_OP_reg7:
3155 	case DW_OP_reg8:
3156 	case DW_OP_reg9:
3157 	case DW_OP_reg10:
3158 	case DW_OP_reg11:
3159 	case DW_OP_reg12:
3160 	case DW_OP_reg13:
3161 	case DW_OP_reg14:
3162 	case DW_OP_reg15:
3163 	case DW_OP_reg16:
3164 	case DW_OP_reg17:
3165 	case DW_OP_reg18:
3166 	case DW_OP_reg19:
3167 	case DW_OP_reg20:
3168 	case DW_OP_reg21:
3169 	case DW_OP_reg22:
3170 	case DW_OP_reg23:
3171 	case DW_OP_reg24:
3172 	case DW_OP_reg25:
3173 	case DW_OP_reg26:
3174 	case DW_OP_reg27:
3175 	case DW_OP_reg28:
3176 	case DW_OP_reg29:
3177 	case DW_OP_reg30:
3178 	case DW_OP_reg31:
3179 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3180 	  loc->u.reg = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
3181 	  loc->kind = axs_lvalue_register;
3182 	  break;
3183 
3184 	case DW_OP_regx:
3185 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3186 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3187 	  loc->u.reg = dwarf_reg_to_regnum_or_error (arch, reg);
3188 	  loc->kind = axs_lvalue_register;
3189 	  break;
3190 
3191 	case DW_OP_implicit_value:
3192 	  {
3193 	    uint64_t len;
3194 
3195 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
3196 	    if (op_ptr + len > op_end)
3197 	      error (_("DW_OP_implicit_value: too few bytes available."));
3198 	    if (len > sizeof (ULONGEST))
3199 	      error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3200 		     (int) len);
3201 
3202 	    ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
3203 							byte_order));
3204 	    op_ptr += len;
3205 	    dwarf_expr_require_composition (op_ptr, op_end,
3206 					    "DW_OP_implicit_value");
3207 
3208 	    loc->kind = axs_rvalue;
3209 	  }
3210 	  break;
3211 
3212 	case DW_OP_stack_value:
3213 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
3214 	  loc->kind = axs_rvalue;
3215 	  break;
3216 
3217 	case DW_OP_breg0:
3218 	case DW_OP_breg1:
3219 	case DW_OP_breg2:
3220 	case DW_OP_breg3:
3221 	case DW_OP_breg4:
3222 	case DW_OP_breg5:
3223 	case DW_OP_breg6:
3224 	case DW_OP_breg7:
3225 	case DW_OP_breg8:
3226 	case DW_OP_breg9:
3227 	case DW_OP_breg10:
3228 	case DW_OP_breg11:
3229 	case DW_OP_breg12:
3230 	case DW_OP_breg13:
3231 	case DW_OP_breg14:
3232 	case DW_OP_breg15:
3233 	case DW_OP_breg16:
3234 	case DW_OP_breg17:
3235 	case DW_OP_breg18:
3236 	case DW_OP_breg19:
3237 	case DW_OP_breg20:
3238 	case DW_OP_breg21:
3239 	case DW_OP_breg22:
3240 	case DW_OP_breg23:
3241 	case DW_OP_breg24:
3242 	case DW_OP_breg25:
3243 	case DW_OP_breg26:
3244 	case DW_OP_breg27:
3245 	case DW_OP_breg28:
3246 	case DW_OP_breg29:
3247 	case DW_OP_breg30:
3248 	case DW_OP_breg31:
3249 	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3250 	  i = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
3251 	  ax_reg (expr, i);
3252 	  if (offset != 0)
3253 	    {
3254 	      ax_const_l (expr, offset);
3255 	      ax_simple (expr, aop_add);
3256 	    }
3257 	  break;
3258 
3259 	case DW_OP_bregx:
3260 	  {
3261 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3262 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3263 	    i = dwarf_reg_to_regnum_or_error (arch, reg);
3264 	    ax_reg (expr, i);
3265 	    if (offset != 0)
3266 	      {
3267 		ax_const_l (expr, offset);
3268 		ax_simple (expr, aop_add);
3269 	      }
3270 	  }
3271 	  break;
3272 
3273 	case DW_OP_fbreg:
3274 	  {
3275 	    const gdb_byte *datastart;
3276 	    size_t datalen;
3277 	    const struct block *b;
3278 	    struct symbol *framefunc;
3279 
3280 	    b = block_for_pc (expr->scope);
3281 
3282 	    if (!b)
3283 	      error (_("No block found for address"));
3284 
3285 	    framefunc = block_linkage_function (b);
3286 
3287 	    if (!framefunc)
3288 	      error (_("No function found for block"));
3289 
3290 	    func_get_frame_base_dwarf_block (framefunc, expr->scope,
3291 					     &datastart, &datalen);
3292 
3293 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3294 	    dwarf2_compile_expr_to_ax (expr, loc, addr_size, datastart,
3295 				       datastart + datalen, per_cu,
3296 				       per_objfile);
3297 	    if (loc->kind == axs_lvalue_register)
3298 	      require_rvalue (expr, loc);
3299 
3300 	    if (offset != 0)
3301 	      {
3302 		ax_const_l (expr, offset);
3303 		ax_simple (expr, aop_add);
3304 	      }
3305 
3306 	    loc->kind = axs_lvalue_memory;
3307 	  }
3308 	  break;
3309 
3310 	case DW_OP_dup:
3311 	  ax_simple (expr, aop_dup);
3312 	  break;
3313 
3314 	case DW_OP_drop:
3315 	  ax_simple (expr, aop_pop);
3316 	  break;
3317 
3318 	case DW_OP_pick:
3319 	  offset = *op_ptr++;
3320 	  ax_pick (expr, offset);
3321 	  break;
3322 
3323 	case DW_OP_swap:
3324 	  ax_simple (expr, aop_swap);
3325 	  break;
3326 
3327 	case DW_OP_over:
3328 	  ax_pick (expr, 1);
3329 	  break;
3330 
3331 	case DW_OP_rot:
3332 	  ax_simple (expr, aop_rot);
3333 	  break;
3334 
3335 	case DW_OP_deref:
3336 	case DW_OP_deref_size:
3337 	  {
3338 	    int size;
3339 
3340 	    if (op == DW_OP_deref_size)
3341 	      size = *op_ptr++;
3342 	    else
3343 	      size = addr_size;
3344 
3345 	    if (size != 1 && size != 2 && size != 4 && size != 8)
3346 	      error (_("Unsupported size %d in %s"),
3347 		     size, get_DW_OP_name (op));
3348 	    access_memory (arch, expr, size * TARGET_CHAR_BIT);
3349 	  }
3350 	  break;
3351 
3352 	case DW_OP_abs:
3353 	  /* Sign extend the operand.  */
3354 	  ax_ext (expr, addr_size_bits);
3355 	  ax_simple (expr, aop_dup);
3356 	  ax_const_l (expr, 0);
3357 	  ax_simple (expr, aop_less_signed);
3358 	  ax_simple (expr, aop_log_not);
3359 	  i = ax_goto (expr, aop_if_goto);
3360 	  /* We have to emit 0 - X.  */
3361 	  ax_const_l (expr, 0);
3362 	  ax_simple (expr, aop_swap);
3363 	  ax_simple (expr, aop_sub);
3364 	  ax_label (expr, i, expr->len);
3365 	  break;
3366 
3367 	case DW_OP_neg:
3368 	  /* No need to sign extend here.  */
3369 	  ax_const_l (expr, 0);
3370 	  ax_simple (expr, aop_swap);
3371 	  ax_simple (expr, aop_sub);
3372 	  break;
3373 
3374 	case DW_OP_not:
3375 	  /* Sign extend the operand.  */
3376 	  ax_ext (expr, addr_size_bits);
3377 	  ax_simple (expr, aop_bit_not);
3378 	  break;
3379 
3380 	case DW_OP_plus_uconst:
3381 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3382 	  /* It would be really weird to emit `DW_OP_plus_uconst 0',
3383 	     but we micro-optimize anyhow.  */
3384 	  if (reg != 0)
3385 	    {
3386 	      ax_const_l (expr, reg);
3387 	      ax_simple (expr, aop_add);
3388 	    }
3389 	  break;
3390 
3391 	case DW_OP_and:
3392 	  ax_simple (expr, aop_bit_and);
3393 	  break;
3394 
3395 	case DW_OP_div:
3396 	  /* Sign extend the operands.  */
3397 	  ax_ext (expr, addr_size_bits);
3398 	  ax_simple (expr, aop_swap);
3399 	  ax_ext (expr, addr_size_bits);
3400 	  ax_simple (expr, aop_swap);
3401 	  ax_simple (expr, aop_div_signed);
3402 	  break;
3403 
3404 	case DW_OP_minus:
3405 	  ax_simple (expr, aop_sub);
3406 	  break;
3407 
3408 	case DW_OP_mod:
3409 	  ax_simple (expr, aop_rem_unsigned);
3410 	  break;
3411 
3412 	case DW_OP_mul:
3413 	  ax_simple (expr, aop_mul);
3414 	  break;
3415 
3416 	case DW_OP_or:
3417 	  ax_simple (expr, aop_bit_or);
3418 	  break;
3419 
3420 	case DW_OP_plus:
3421 	  ax_simple (expr, aop_add);
3422 	  break;
3423 
3424 	case DW_OP_shl:
3425 	  ax_simple (expr, aop_lsh);
3426 	  break;
3427 
3428 	case DW_OP_shr:
3429 	  ax_simple (expr, aop_rsh_unsigned);
3430 	  break;
3431 
3432 	case DW_OP_shra:
3433 	  ax_simple (expr, aop_rsh_signed);
3434 	  break;
3435 
3436 	case DW_OP_xor:
3437 	  ax_simple (expr, aop_bit_xor);
3438 	  break;
3439 
3440 	case DW_OP_le:
3441 	  /* Sign extend the operands.  */
3442 	  ax_ext (expr, addr_size_bits);
3443 	  ax_simple (expr, aop_swap);
3444 	  ax_ext (expr, addr_size_bits);
3445 	  /* Note no swap here: A <= B is !(B < A).  */
3446 	  ax_simple (expr, aop_less_signed);
3447 	  ax_simple (expr, aop_log_not);
3448 	  break;
3449 
3450 	case DW_OP_ge:
3451 	  /* Sign extend the operands.  */
3452 	  ax_ext (expr, addr_size_bits);
3453 	  ax_simple (expr, aop_swap);
3454 	  ax_ext (expr, addr_size_bits);
3455 	  ax_simple (expr, aop_swap);
3456 	  /* A >= B is !(A < B).  */
3457 	  ax_simple (expr, aop_less_signed);
3458 	  ax_simple (expr, aop_log_not);
3459 	  break;
3460 
3461 	case DW_OP_eq:
3462 	  /* Sign extend the operands.  */
3463 	  ax_ext (expr, addr_size_bits);
3464 	  ax_simple (expr, aop_swap);
3465 	  ax_ext (expr, addr_size_bits);
3466 	  /* No need for a second swap here.  */
3467 	  ax_simple (expr, aop_equal);
3468 	  break;
3469 
3470 	case DW_OP_lt:
3471 	  /* Sign extend the operands.  */
3472 	  ax_ext (expr, addr_size_bits);
3473 	  ax_simple (expr, aop_swap);
3474 	  ax_ext (expr, addr_size_bits);
3475 	  ax_simple (expr, aop_swap);
3476 	  ax_simple (expr, aop_less_signed);
3477 	  break;
3478 
3479 	case DW_OP_gt:
3480 	  /* Sign extend the operands.  */
3481 	  ax_ext (expr, addr_size_bits);
3482 	  ax_simple (expr, aop_swap);
3483 	  ax_ext (expr, addr_size_bits);
3484 	  /* Note no swap here: A > B is B < A.  */
3485 	  ax_simple (expr, aop_less_signed);
3486 	  break;
3487 
3488 	case DW_OP_ne:
3489 	  /* Sign extend the operands.  */
3490 	  ax_ext (expr, addr_size_bits);
3491 	  ax_simple (expr, aop_swap);
3492 	  ax_ext (expr, addr_size_bits);
3493 	  /* No need for a swap here.  */
3494 	  ax_simple (expr, aop_equal);
3495 	  ax_simple (expr, aop_log_not);
3496 	  break;
3497 
3498 	case DW_OP_call_frame_cfa:
3499 	  {
3500 	    int regnum;
3501 	    CORE_ADDR text_offset;
3502 	    LONGEST off;
3503 	    const gdb_byte *cfa_start, *cfa_end;
3504 
3505 	    if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
3506 				       &regnum, &off,
3507 				       &text_offset, &cfa_start, &cfa_end))
3508 	      {
3509 		/* Register.  */
3510 		ax_reg (expr, regnum);
3511 		if (off != 0)
3512 		  {
3513 		    ax_const_l (expr, off);
3514 		    ax_simple (expr, aop_add);
3515 		  }
3516 	      }
3517 	    else
3518 	      {
3519 		/* Another expression.  */
3520 		ax_const_l (expr, text_offset);
3521 		dwarf2_compile_expr_to_ax (expr, loc, addr_size, cfa_start,
3522 					   cfa_end, per_cu, per_objfile);
3523 	      }
3524 
3525 	    loc->kind = axs_lvalue_memory;
3526 	  }
3527 	  break;
3528 
3529 	case DW_OP_GNU_push_tls_address:
3530 	case DW_OP_form_tls_address:
3531 	  unimplemented (op);
3532 	  break;
3533 
3534 	case DW_OP_push_object_address:
3535 	  unimplemented (op);
3536 	  break;
3537 
3538 	case DW_OP_skip:
3539 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
3540 	  op_ptr += 2;
3541 	  i = ax_goto (expr, aop_goto);
3542 	  dw_labels.push_back (op_ptr + offset - base);
3543 	  patches.push_back (i);
3544 	  break;
3545 
3546 	case DW_OP_bra:
3547 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
3548 	  op_ptr += 2;
3549 	  /* Zero extend the operand.  */
3550 	  ax_zero_ext (expr, addr_size_bits);
3551 	  i = ax_goto (expr, aop_if_goto);
3552 	  dw_labels.push_back (op_ptr + offset - base);
3553 	  patches.push_back (i);
3554 	  break;
3555 
3556 	case DW_OP_nop:
3557 	  break;
3558 
3559         case DW_OP_piece:
3560 	case DW_OP_bit_piece:
3561 	  {
3562 	    uint64_t size;
3563 
3564 	    if (op_ptr - 1 == previous_piece)
3565 	      error (_("Cannot translate empty pieces to agent expressions"));
3566 	    previous_piece = op_ptr - 1;
3567 
3568             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3569 	    if (op == DW_OP_piece)
3570 	      {
3571 		size *= 8;
3572 		uoffset = 0;
3573 	      }
3574 	    else
3575 	      op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3576 
3577 	    if (bits_collected + size > 8 * sizeof (LONGEST))
3578 	      error (_("Expression pieces exceed word size"));
3579 
3580 	    /* Access the bits.  */
3581 	    switch (loc->kind)
3582 	      {
3583 	      case axs_lvalue_register:
3584 		ax_reg (expr, loc->u.reg);
3585 		break;
3586 
3587 	      case axs_lvalue_memory:
3588 		/* Offset the pointer, if needed.  */
3589 		if (uoffset > 8)
3590 		  {
3591 		    ax_const_l (expr, uoffset / 8);
3592 		    ax_simple (expr, aop_add);
3593 		    uoffset %= 8;
3594 		  }
3595 		access_memory (arch, expr, size);
3596 		break;
3597 	      }
3598 
3599 	    /* For a bits-big-endian target, shift up what we already
3600 	       have.  For a bits-little-endian target, shift up the
3601 	       new data.  Note that there is a potential bug here if
3602 	       the DWARF expression leaves multiple values on the
3603 	       stack.  */
3604 	    if (bits_collected > 0)
3605 	      {
3606 		if (bits_big_endian)
3607 		  {
3608 		    ax_simple (expr, aop_swap);
3609 		    ax_const_l (expr, size);
3610 		    ax_simple (expr, aop_lsh);
3611 		    /* We don't need a second swap here, because
3612 		       aop_bit_or is symmetric.  */
3613 		  }
3614 		else
3615 		  {
3616 		    ax_const_l (expr, size);
3617 		    ax_simple (expr, aop_lsh);
3618 		  }
3619 		ax_simple (expr, aop_bit_or);
3620 	      }
3621 
3622 	    bits_collected += size;
3623 	    loc->kind = axs_rvalue;
3624 	  }
3625 	  break;
3626 
3627 	case DW_OP_GNU_uninit:
3628 	  unimplemented (op);
3629 
3630 	case DW_OP_call2:
3631 	case DW_OP_call4:
3632 	  {
3633 	    struct dwarf2_locexpr_baton block;
3634 	    int size = (op == DW_OP_call2 ? 2 : 4);
3635 
3636 	    uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3637 	    op_ptr += size;
3638 
3639 	    auto get_frame_pc_from_expr = [expr] ()
3640 	      {
3641 		return expr->scope;
3642 	      };
3643 	    cu_offset cuoffset = (cu_offset) uoffset;
3644 	    block = dwarf2_fetch_die_loc_cu_off (cuoffset, per_cu, per_objfile,
3645 						 get_frame_pc_from_expr);
3646 
3647 	    /* DW_OP_call_ref is currently not supported.  */
3648 	    gdb_assert (block.per_cu == per_cu);
3649 
3650 	    dwarf2_compile_expr_to_ax (expr, loc, addr_size, block.data,
3651 				       block.data + block.size, per_cu,
3652 				       per_objfile);
3653 	  }
3654 	  break;
3655 
3656 	case DW_OP_call_ref:
3657 	  unimplemented (op);
3658 
3659 	case DW_OP_GNU_variable_value:
3660 	  unimplemented (op);
3661 
3662 	default:
3663 	  unimplemented (op);
3664 	}
3665     }
3666 
3667   /* Patch all the branches we emitted.  */
3668   for (int i = 0; i < patches.size (); ++i)
3669     {
3670       int targ = offsets[dw_labels[i]];
3671       if (targ == -1)
3672 	internal_error (__FILE__, __LINE__, _("invalid label"));
3673       ax_label (expr, patches[i], targ);
3674     }
3675 }
3676 
3677 
3678 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3679    evaluator to calculate the location.  */
3680 static struct value *
3681 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3682 {
3683   struct dwarf2_locexpr_baton *dlbaton
3684     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3685   struct value *val;
3686 
3687   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3688 				  dlbaton->size, dlbaton->per_cu,
3689 				  dlbaton->per_objfile);
3690 
3691   return val;
3692 }
3693 
3694 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3695    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3696    will be thrown.  */
3697 
3698 static struct value *
3699 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3700 {
3701   struct dwarf2_locexpr_baton *dlbaton
3702     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3703 
3704   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3705 				     dlbaton->size);
3706 }
3707 
3708 /* Implementation of get_symbol_read_needs from
3709    symbol_computed_ops.  */
3710 
3711 static enum symbol_needs_kind
3712 locexpr_get_symbol_read_needs (struct symbol *symbol)
3713 {
3714   struct dwarf2_locexpr_baton *dlbaton
3715     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3716 
3717   return dwarf2_loc_desc_get_symbol_read_needs (dlbaton->data, dlbaton->size,
3718 						dlbaton->per_cu,
3719 						dlbaton->per_objfile);
3720 }
3721 
3722 /* Return true if DATA points to the end of a piece.  END is one past
3723    the last byte in the expression.  */
3724 
3725 static int
3726 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3727 {
3728   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3729 }
3730 
3731 /* Helper for locexpr_describe_location_piece that finds the name of a
3732    DWARF register.  */
3733 
3734 static const char *
3735 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3736 {
3737   int regnum;
3738 
3739   /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3740      We'd rather print *something* here than throw an error.  */
3741   regnum = dwarf_reg_to_regnum (gdbarch, dwarf_regnum);
3742   /* gdbarch_register_name may just return "", return something more
3743      descriptive for bad register numbers.  */
3744   if (regnum == -1)
3745     {
3746       /* The text is output as "$bad_register_number".
3747 	 That is why we use the underscores.  */
3748       return _("bad_register_number");
3749     }
3750   return gdbarch_register_name (gdbarch, regnum);
3751 }
3752 
3753 /* Nicely describe a single piece of a location, returning an updated
3754    position in the bytecode sequence.  This function cannot recognize
3755    all locations; if a location is not recognized, it simply returns
3756    DATA.  If there is an error during reading, e.g. we run off the end
3757    of the buffer, an error is thrown.  */
3758 
3759 static const gdb_byte *
3760 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3761 				 CORE_ADDR addr, dwarf2_per_cu_data *per_cu,
3762 				 dwarf2_per_objfile *per_objfile,
3763 				 const gdb_byte *data, const gdb_byte *end,
3764 				 unsigned int addr_size)
3765 {
3766   objfile *objfile = per_objfile->objfile;
3767   struct gdbarch *gdbarch = objfile->arch ();
3768   size_t leb128_size;
3769 
3770   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3771     {
3772       fprintf_filtered (stream, _("a variable in $%s"),
3773 			locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3774       data += 1;
3775     }
3776   else if (data[0] == DW_OP_regx)
3777     {
3778       uint64_t reg;
3779 
3780       data = safe_read_uleb128 (data + 1, end, &reg);
3781       fprintf_filtered (stream, _("a variable in $%s"),
3782 			locexpr_regname (gdbarch, reg));
3783     }
3784   else if (data[0] == DW_OP_fbreg)
3785     {
3786       const struct block *b;
3787       struct symbol *framefunc;
3788       int frame_reg = 0;
3789       int64_t frame_offset;
3790       const gdb_byte *base_data, *new_data, *save_data = data;
3791       size_t base_size;
3792       int64_t base_offset = 0;
3793 
3794       new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3795       if (!piece_end_p (new_data, end))
3796 	return data;
3797       data = new_data;
3798 
3799       b = block_for_pc (addr);
3800 
3801       if (!b)
3802 	error (_("No block found for address for symbol \"%s\"."),
3803 	       symbol->print_name ());
3804 
3805       framefunc = block_linkage_function (b);
3806 
3807       if (!framefunc)
3808 	error (_("No function found for block for symbol \"%s\"."),
3809 	       symbol->print_name ());
3810 
3811       func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
3812 
3813       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3814 	{
3815 	  const gdb_byte *buf_end;
3816 
3817 	  frame_reg = base_data[0] - DW_OP_breg0;
3818 	  buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3819 				       &base_offset);
3820 	  if (buf_end != base_data + base_size)
3821 	    error (_("Unexpected opcode after "
3822 		     "DW_OP_breg%u for symbol \"%s\"."),
3823 		   frame_reg, symbol->print_name ());
3824 	}
3825       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3826 	{
3827 	  /* The frame base is just the register, with no offset.  */
3828 	  frame_reg = base_data[0] - DW_OP_reg0;
3829 	  base_offset = 0;
3830 	}
3831       else
3832 	{
3833 	  /* We don't know what to do with the frame base expression,
3834 	     so we can't trace this variable; give up.  */
3835 	  return save_data;
3836 	}
3837 
3838       fprintf_filtered (stream,
3839 			_("a variable at frame base reg $%s offset %s+%s"),
3840 			locexpr_regname (gdbarch, frame_reg),
3841 			plongest (base_offset), plongest (frame_offset));
3842     }
3843   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3844 	   && piece_end_p (data, end))
3845     {
3846       int64_t offset;
3847 
3848       data = safe_read_sleb128 (data + 1, end, &offset);
3849 
3850       fprintf_filtered (stream,
3851 			_("a variable at offset %s from base reg $%s"),
3852 			plongest (offset),
3853 			locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3854     }
3855 
3856   /* The location expression for a TLS variable looks like this (on a
3857      64-bit LE machine):
3858 
3859      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3860                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3861 
3862      0x3 is the encoding for DW_OP_addr, which has an operand as long
3863      as the size of an address on the target machine (here is 8
3864      bytes).  Note that more recent version of GCC emit DW_OP_const4u
3865      or DW_OP_const8u, depending on address size, rather than
3866      DW_OP_addr.  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3867      The operand represents the offset at which the variable is within
3868      the thread local storage.  */
3869 
3870   else if (data + 1 + addr_size < end
3871 	   && (data[0] == DW_OP_addr
3872 	       || (addr_size == 4 && data[0] == DW_OP_const4u)
3873 	       || (addr_size == 8 && data[0] == DW_OP_const8u))
3874 	   && (data[1 + addr_size] == DW_OP_GNU_push_tls_address
3875 	       || data[1 + addr_size] == DW_OP_form_tls_address)
3876 	   && piece_end_p (data + 2 + addr_size, end))
3877     {
3878       ULONGEST offset;
3879       offset = extract_unsigned_integer (data + 1, addr_size,
3880 					 gdbarch_byte_order (gdbarch));
3881 
3882       fprintf_filtered (stream,
3883 			_("a thread-local variable at offset 0x%s "
3884 			  "in the thread-local storage for `%s'"),
3885 			phex_nz (offset, addr_size), objfile_name (objfile));
3886 
3887       data += 1 + addr_size + 1;
3888     }
3889 
3890   /* With -gsplit-dwarf a TLS variable can also look like this:
3891      DW_AT_location    : 3 byte block: fc 4 e0
3892                         (DW_OP_GNU_const_index: 4;
3893 			 DW_OP_GNU_push_tls_address)  */
3894   else if (data + 3 <= end
3895 	   && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3896 	   && data[0] == DW_OP_GNU_const_index
3897 	   && leb128_size > 0
3898 	   && (data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3899 	       || data[1 + leb128_size] == DW_OP_form_tls_address)
3900 	   && piece_end_p (data + 2 + leb128_size, end))
3901     {
3902       uint64_t offset;
3903 
3904       data = safe_read_uleb128 (data + 1, end, &offset);
3905       offset = dwarf2_read_addr_index (per_cu, per_objfile, offset);
3906       fprintf_filtered (stream,
3907 			_("a thread-local variable at offset 0x%s "
3908 			  "in the thread-local storage for `%s'"),
3909 			phex_nz (offset, addr_size), objfile_name (objfile));
3910       ++data;
3911     }
3912 
3913   else if (data[0] >= DW_OP_lit0
3914 	   && data[0] <= DW_OP_lit31
3915 	   && data + 1 < end
3916 	   && data[1] == DW_OP_stack_value)
3917     {
3918       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3919       data += 2;
3920     }
3921 
3922   return data;
3923 }
3924 
3925 /* Disassemble an expression, stopping at the end of a piece or at the
3926    end of the expression.  Returns a pointer to the next unread byte
3927    in the input expression.  If ALL is nonzero, then this function
3928    will keep going until it reaches the end of the expression.
3929    If there is an error during reading, e.g. we run off the end
3930    of the buffer, an error is thrown.  */
3931 
3932 static const gdb_byte *
3933 disassemble_dwarf_expression (struct ui_file *stream,
3934 			      struct gdbarch *arch, unsigned int addr_size,
3935 			      int offset_size, const gdb_byte *start,
3936 			      const gdb_byte *data, const gdb_byte *end,
3937 			      int indent, int all,
3938 			      dwarf2_per_cu_data *per_cu,
3939 			      dwarf2_per_objfile *per_objfile)
3940 {
3941   while (data < end
3942 	 && (all
3943 	     || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3944     {
3945       enum dwarf_location_atom op = (enum dwarf_location_atom) *data++;
3946       uint64_t ul;
3947       int64_t l;
3948       const char *name;
3949 
3950       name = get_DW_OP_name (op);
3951 
3952       if (!name)
3953 	error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3954 	       op, (long) (data - 1 - start));
3955       fprintf_filtered (stream, "  %*ld: %s", indent + 4,
3956 			(long) (data - 1 - start), name);
3957 
3958       switch (op)
3959 	{
3960 	case DW_OP_addr:
3961 	  ul = extract_unsigned_integer (data, addr_size,
3962 					 gdbarch_byte_order (arch));
3963 	  data += addr_size;
3964 	  fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3965 	  break;
3966 
3967 	case DW_OP_const1u:
3968 	  ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3969 	  data += 1;
3970 	  fprintf_filtered (stream, " %s", pulongest (ul));
3971 	  break;
3972 
3973 	case DW_OP_const1s:
3974 	  l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3975 	  data += 1;
3976 	  fprintf_filtered (stream, " %s", plongest (l));
3977 	  break;
3978 
3979 	case DW_OP_const2u:
3980 	  ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3981 	  data += 2;
3982 	  fprintf_filtered (stream, " %s", pulongest (ul));
3983 	  break;
3984 
3985 	case DW_OP_const2s:
3986 	  l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3987 	  data += 2;
3988 	  fprintf_filtered (stream, " %s", plongest (l));
3989 	  break;
3990 
3991 	case DW_OP_const4u:
3992 	  ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3993 	  data += 4;
3994 	  fprintf_filtered (stream, " %s", pulongest (ul));
3995 	  break;
3996 
3997 	case DW_OP_const4s:
3998 	  l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3999 	  data += 4;
4000 	  fprintf_filtered (stream, " %s", plongest (l));
4001 	  break;
4002 
4003 	case DW_OP_const8u:
4004 	  ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
4005 	  data += 8;
4006 	  fprintf_filtered (stream, " %s", pulongest (ul));
4007 	  break;
4008 
4009 	case DW_OP_const8s:
4010 	  l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
4011 	  data += 8;
4012 	  fprintf_filtered (stream, " %s", plongest (l));
4013 	  break;
4014 
4015 	case DW_OP_constu:
4016 	  data = safe_read_uleb128 (data, end, &ul);
4017 	  fprintf_filtered (stream, " %s", pulongest (ul));
4018 	  break;
4019 
4020 	case DW_OP_consts:
4021 	  data = safe_read_sleb128 (data, end, &l);
4022 	  fprintf_filtered (stream, " %s", plongest (l));
4023 	  break;
4024 
4025 	case DW_OP_reg0:
4026 	case DW_OP_reg1:
4027 	case DW_OP_reg2:
4028 	case DW_OP_reg3:
4029 	case DW_OP_reg4:
4030 	case DW_OP_reg5:
4031 	case DW_OP_reg6:
4032 	case DW_OP_reg7:
4033 	case DW_OP_reg8:
4034 	case DW_OP_reg9:
4035 	case DW_OP_reg10:
4036 	case DW_OP_reg11:
4037 	case DW_OP_reg12:
4038 	case DW_OP_reg13:
4039 	case DW_OP_reg14:
4040 	case DW_OP_reg15:
4041 	case DW_OP_reg16:
4042 	case DW_OP_reg17:
4043 	case DW_OP_reg18:
4044 	case DW_OP_reg19:
4045 	case DW_OP_reg20:
4046 	case DW_OP_reg21:
4047 	case DW_OP_reg22:
4048 	case DW_OP_reg23:
4049 	case DW_OP_reg24:
4050 	case DW_OP_reg25:
4051 	case DW_OP_reg26:
4052 	case DW_OP_reg27:
4053 	case DW_OP_reg28:
4054 	case DW_OP_reg29:
4055 	case DW_OP_reg30:
4056 	case DW_OP_reg31:
4057 	  fprintf_filtered (stream, " [$%s]",
4058 			    locexpr_regname (arch, op - DW_OP_reg0));
4059 	  break;
4060 
4061 	case DW_OP_regx:
4062 	  data = safe_read_uleb128 (data, end, &ul);
4063 	  fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
4064 			    locexpr_regname (arch, (int) ul));
4065 	  break;
4066 
4067 	case DW_OP_implicit_value:
4068 	  data = safe_read_uleb128 (data, end, &ul);
4069 	  data += ul;
4070 	  fprintf_filtered (stream, " %s", pulongest (ul));
4071 	  break;
4072 
4073 	case DW_OP_breg0:
4074 	case DW_OP_breg1:
4075 	case DW_OP_breg2:
4076 	case DW_OP_breg3:
4077 	case DW_OP_breg4:
4078 	case DW_OP_breg5:
4079 	case DW_OP_breg6:
4080 	case DW_OP_breg7:
4081 	case DW_OP_breg8:
4082 	case DW_OP_breg9:
4083 	case DW_OP_breg10:
4084 	case DW_OP_breg11:
4085 	case DW_OP_breg12:
4086 	case DW_OP_breg13:
4087 	case DW_OP_breg14:
4088 	case DW_OP_breg15:
4089 	case DW_OP_breg16:
4090 	case DW_OP_breg17:
4091 	case DW_OP_breg18:
4092 	case DW_OP_breg19:
4093 	case DW_OP_breg20:
4094 	case DW_OP_breg21:
4095 	case DW_OP_breg22:
4096 	case DW_OP_breg23:
4097 	case DW_OP_breg24:
4098 	case DW_OP_breg25:
4099 	case DW_OP_breg26:
4100 	case DW_OP_breg27:
4101 	case DW_OP_breg28:
4102 	case DW_OP_breg29:
4103 	case DW_OP_breg30:
4104 	case DW_OP_breg31:
4105 	  data = safe_read_sleb128 (data, end, &l);
4106 	  fprintf_filtered (stream, " %s [$%s]", plongest (l),
4107 			    locexpr_regname (arch, op - DW_OP_breg0));
4108 	  break;
4109 
4110 	case DW_OP_bregx:
4111 	  data = safe_read_uleb128 (data, end, &ul);
4112 	  data = safe_read_sleb128 (data, end, &l);
4113 	  fprintf_filtered (stream, " register %s [$%s] offset %s",
4114 			    pulongest (ul),
4115 			    locexpr_regname (arch, (int) ul),
4116 			    plongest (l));
4117 	  break;
4118 
4119 	case DW_OP_fbreg:
4120 	  data = safe_read_sleb128 (data, end, &l);
4121 	  fprintf_filtered (stream, " %s", plongest (l));
4122 	  break;
4123 
4124 	case DW_OP_xderef_size:
4125 	case DW_OP_deref_size:
4126 	case DW_OP_pick:
4127 	  fprintf_filtered (stream, " %d", *data);
4128 	  ++data;
4129 	  break;
4130 
4131 	case DW_OP_plus_uconst:
4132 	  data = safe_read_uleb128 (data, end, &ul);
4133 	  fprintf_filtered (stream, " %s", pulongest (ul));
4134 	  break;
4135 
4136 	case DW_OP_skip:
4137 	  l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4138 	  data += 2;
4139 	  fprintf_filtered (stream, " to %ld",
4140 			    (long) (data + l - start));
4141 	  break;
4142 
4143 	case DW_OP_bra:
4144 	  l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4145 	  data += 2;
4146 	  fprintf_filtered (stream, " %ld",
4147 			    (long) (data + l - start));
4148 	  break;
4149 
4150 	case DW_OP_call2:
4151 	  ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
4152 	  data += 2;
4153 	  fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
4154 	  break;
4155 
4156 	case DW_OP_call4:
4157 	  ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4158 	  data += 4;
4159 	  fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4160 	  break;
4161 
4162 	case DW_OP_call_ref:
4163 	  ul = extract_unsigned_integer (data, offset_size,
4164 					 gdbarch_byte_order (arch));
4165 	  data += offset_size;
4166 	  fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4167 	  break;
4168 
4169         case DW_OP_piece:
4170 	  data = safe_read_uleb128 (data, end, &ul);
4171 	  fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
4172 	  break;
4173 
4174 	case DW_OP_bit_piece:
4175 	  {
4176 	    uint64_t offset;
4177 
4178 	    data = safe_read_uleb128 (data, end, &ul);
4179 	    data = safe_read_uleb128 (data, end, &offset);
4180 	    fprintf_filtered (stream, " size %s offset %s (bits)",
4181 			      pulongest (ul), pulongest (offset));
4182 	  }
4183 	  break;
4184 
4185 	case DW_OP_implicit_pointer:
4186 	case DW_OP_GNU_implicit_pointer:
4187 	  {
4188 	    ul = extract_unsigned_integer (data, offset_size,
4189 					   gdbarch_byte_order (arch));
4190 	    data += offset_size;
4191 
4192 	    data = safe_read_sleb128 (data, end, &l);
4193 
4194 	    fprintf_filtered (stream, " DIE %s offset %s",
4195 			      phex_nz (ul, offset_size),
4196 			      plongest (l));
4197 	  }
4198 	  break;
4199 
4200 	case DW_OP_deref_type:
4201 	case DW_OP_GNU_deref_type:
4202 	  {
4203 	    int deref_addr_size = *data++;
4204 	    struct type *type;
4205 
4206 	    data = safe_read_uleb128 (data, end, &ul);
4207 	    cu_offset offset = (cu_offset) ul;
4208 	    type = dwarf2_get_die_type (offset, per_cu, per_objfile);
4209 	    fprintf_filtered (stream, "<");
4210 	    type_print (type, "", stream, -1);
4211 	    fprintf_filtered (stream, " [0x%s]> %d",
4212 			      phex_nz (to_underlying (offset), 0),
4213 			      deref_addr_size);
4214 	  }
4215 	  break;
4216 
4217 	case DW_OP_const_type:
4218 	case DW_OP_GNU_const_type:
4219 	  {
4220 	    struct type *type;
4221 
4222 	    data = safe_read_uleb128 (data, end, &ul);
4223 	    cu_offset type_die = (cu_offset) ul;
4224 	    type = dwarf2_get_die_type (type_die, per_cu, per_objfile);
4225 	    fprintf_filtered (stream, "<");
4226 	    type_print (type, "", stream, -1);
4227 	    fprintf_filtered (stream, " [0x%s]>",
4228 			      phex_nz (to_underlying (type_die), 0));
4229 
4230 	    int n = *data++;
4231 	    fprintf_filtered (stream, " %d byte block:", n);
4232 	    for (int i = 0; i < n; ++i)
4233 	      fprintf_filtered (stream, " %02x", data[i]);
4234 	    data += n;
4235 	  }
4236 	  break;
4237 
4238 	case DW_OP_regval_type:
4239 	case DW_OP_GNU_regval_type:
4240 	  {
4241 	    uint64_t reg;
4242 	    struct type *type;
4243 
4244 	    data = safe_read_uleb128 (data, end, &reg);
4245 	    data = safe_read_uleb128 (data, end, &ul);
4246 	    cu_offset type_die = (cu_offset) ul;
4247 
4248 	    type = dwarf2_get_die_type (type_die, per_cu, per_objfile);
4249 	    fprintf_filtered (stream, "<");
4250 	    type_print (type, "", stream, -1);
4251 	    fprintf_filtered (stream, " [0x%s]> [$%s]",
4252 			      phex_nz (to_underlying (type_die), 0),
4253 			      locexpr_regname (arch, reg));
4254 	  }
4255 	  break;
4256 
4257 	case DW_OP_convert:
4258 	case DW_OP_GNU_convert:
4259 	case DW_OP_reinterpret:
4260 	case DW_OP_GNU_reinterpret:
4261 	  {
4262 	    data = safe_read_uleb128 (data, end, &ul);
4263 	    cu_offset type_die = (cu_offset) ul;
4264 
4265 	    if (to_underlying (type_die) == 0)
4266 	      fprintf_filtered (stream, "<0>");
4267 	    else
4268 	      {
4269 		struct type *type;
4270 
4271 		type = dwarf2_get_die_type (type_die, per_cu, per_objfile);
4272 		fprintf_filtered (stream, "<");
4273 		type_print (type, "", stream, -1);
4274 		fprintf_filtered (stream, " [0x%s]>",
4275 				  phex_nz (to_underlying (type_die), 0));
4276 	      }
4277 	  }
4278 	  break;
4279 
4280 	case DW_OP_entry_value:
4281 	case DW_OP_GNU_entry_value:
4282 	  data = safe_read_uleb128 (data, end, &ul);
4283 	  fputc_filtered ('\n', stream);
4284 	  disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
4285 					start, data, data + ul, indent + 2,
4286 					all, per_cu, per_objfile);
4287 	  data += ul;
4288 	  continue;
4289 
4290 	case DW_OP_GNU_parameter_ref:
4291 	  ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4292 	  data += 4;
4293 	  fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4294 	  break;
4295 
4296 	case DW_OP_addrx:
4297 	case DW_OP_GNU_addr_index:
4298 	  data = safe_read_uleb128 (data, end, &ul);
4299 	  ul = dwarf2_read_addr_index (per_cu, per_objfile, ul);
4300 	  fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
4301 	  break;
4302 
4303 	case DW_OP_GNU_const_index:
4304 	  data = safe_read_uleb128 (data, end, &ul);
4305 	  ul = dwarf2_read_addr_index (per_cu, per_objfile, ul);
4306 	  fprintf_filtered (stream, " %s", pulongest (ul));
4307 	  break;
4308 
4309 	case DW_OP_GNU_variable_value:
4310 	  ul = extract_unsigned_integer (data, offset_size,
4311 					 gdbarch_byte_order (arch));
4312 	  data += offset_size;
4313 	  fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4314 	  break;
4315 	}
4316 
4317       fprintf_filtered (stream, "\n");
4318     }
4319 
4320   return data;
4321 }
4322 
4323 static bool dwarf_always_disassemble;
4324 
4325 static void
4326 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
4327 			       struct cmd_list_element *c, const char *value)
4328 {
4329   fprintf_filtered (file,
4330 		    _("Whether to always disassemble "
4331 		      "DWARF expressions is %s.\n"),
4332 		    value);
4333 }
4334 
4335 /* Describe a single location, which may in turn consist of multiple
4336    pieces.  */
4337 
4338 static void
4339 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
4340 			     struct ui_file *stream,
4341 			     const gdb_byte *data, size_t size,
4342 			     unsigned int addr_size,
4343 			     int offset_size, dwarf2_per_cu_data *per_cu,
4344 			     dwarf2_per_objfile *per_objfile)
4345 {
4346   const gdb_byte *end = data + size;
4347   int first_piece = 1, bad = 0;
4348   objfile *objfile = per_objfile->objfile;
4349 
4350   while (data < end)
4351     {
4352       const gdb_byte *here = data;
4353       int disassemble = 1;
4354 
4355       if (first_piece)
4356 	first_piece = 0;
4357       else
4358 	fprintf_filtered (stream, _(", and "));
4359 
4360       if (!dwarf_always_disassemble)
4361 	{
4362 	  data = locexpr_describe_location_piece (symbol, stream,
4363 						  addr, per_cu, per_objfile,
4364 						  data, end, addr_size);
4365 	  /* If we printed anything, or if we have an empty piece,
4366 	     then don't disassemble.  */
4367 	  if (data != here
4368 	      || data[0] == DW_OP_piece
4369 	      || data[0] == DW_OP_bit_piece)
4370 	    disassemble = 0;
4371 	}
4372       if (disassemble)
4373 	{
4374 	  fprintf_filtered (stream, _("a complex DWARF expression:\n"));
4375 	  data = disassemble_dwarf_expression (stream,
4376 					       objfile->arch (),
4377 					       addr_size, offset_size, data,
4378 					       data, end, 0,
4379 					       dwarf_always_disassemble,
4380 					       per_cu, per_objfile);
4381 	}
4382 
4383       if (data < end)
4384 	{
4385 	  int empty = data == here;
4386 
4387 	  if (disassemble)
4388 	    fprintf_filtered (stream, "   ");
4389 	  if (data[0] == DW_OP_piece)
4390 	    {
4391 	      uint64_t bytes;
4392 
4393 	      data = safe_read_uleb128 (data + 1, end, &bytes);
4394 
4395 	      if (empty)
4396 		fprintf_filtered (stream, _("an empty %s-byte piece"),
4397 				  pulongest (bytes));
4398 	      else
4399 		fprintf_filtered (stream, _(" [%s-byte piece]"),
4400 				  pulongest (bytes));
4401 	    }
4402 	  else if (data[0] == DW_OP_bit_piece)
4403 	    {
4404 	      uint64_t bits, offset;
4405 
4406 	      data = safe_read_uleb128 (data + 1, end, &bits);
4407 	      data = safe_read_uleb128 (data, end, &offset);
4408 
4409 	      if (empty)
4410 		fprintf_filtered (stream,
4411 				  _("an empty %s-bit piece"),
4412 				  pulongest (bits));
4413 	      else
4414 		fprintf_filtered (stream,
4415 				  _(" [%s-bit piece, offset %s bits]"),
4416 				  pulongest (bits), pulongest (offset));
4417 	    }
4418 	  else
4419 	    {
4420 	      bad = 1;
4421 	      break;
4422 	    }
4423 	}
4424     }
4425 
4426   if (bad || data > end)
4427     error (_("Corrupted DWARF2 expression for \"%s\"."),
4428 	   symbol->print_name ());
4429 }
4430 
4431 /* Print a natural-language description of SYMBOL to STREAM.  This
4432    version is for a symbol with a single location.  */
4433 
4434 static void
4435 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
4436 			   struct ui_file *stream)
4437 {
4438   struct dwarf2_locexpr_baton *dlbaton
4439     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4440   unsigned int addr_size = dlbaton->per_cu->addr_size ();
4441   int offset_size = dlbaton->per_cu->offset_size ();
4442 
4443   locexpr_describe_location_1 (symbol, addr, stream,
4444 			       dlbaton->data, dlbaton->size,
4445 			       addr_size, offset_size,
4446 			       dlbaton->per_cu, dlbaton->per_objfile);
4447 }
4448 
4449 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4450    any necessary bytecode in AX.  */
4451 
4452 static void
4453 locexpr_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
4454 			    struct axs_value *value)
4455 {
4456   struct dwarf2_locexpr_baton *dlbaton
4457     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4458   unsigned int addr_size = dlbaton->per_cu->addr_size ();
4459 
4460   if (dlbaton->size == 0)
4461     value->optimized_out = 1;
4462   else
4463     dwarf2_compile_expr_to_ax (ax, value, addr_size, dlbaton->data,
4464 			       dlbaton->data + dlbaton->size, dlbaton->per_cu,
4465 			       dlbaton->per_objfile);
4466 }
4467 
4468 /* symbol_computed_ops 'generate_c_location' method.  */
4469 
4470 static void
4471 locexpr_generate_c_location (struct symbol *sym, string_file *stream,
4472 			     struct gdbarch *gdbarch,
4473 			     unsigned char *registers_used,
4474 			     CORE_ADDR pc, const char *result_name)
4475 {
4476   struct dwarf2_locexpr_baton *dlbaton
4477     = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
4478   unsigned int addr_size = dlbaton->per_cu->addr_size ();
4479 
4480   if (dlbaton->size == 0)
4481     error (_("symbol \"%s\" is optimized out"), sym->natural_name ());
4482 
4483   compile_dwarf_expr_to_c (stream, result_name,
4484 			   sym, pc, gdbarch, registers_used, addr_size,
4485 			   dlbaton->data, dlbaton->data + dlbaton->size,
4486 			   dlbaton->per_cu, dlbaton->per_objfile);
4487 }
4488 
4489 /* The set of location functions used with the DWARF-2 expression
4490    evaluator.  */
4491 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4492   locexpr_read_variable,
4493   locexpr_read_variable_at_entry,
4494   locexpr_get_symbol_read_needs,
4495   locexpr_describe_location,
4496   0,	/* location_has_loclist */
4497   locexpr_tracepoint_var_ref,
4498   locexpr_generate_c_location
4499 };
4500 
4501 
4502 /* Wrapper functions for location lists.  These generally find
4503    the appropriate location expression and call something above.  */
4504 
4505 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4506    evaluator to calculate the location.  */
4507 static struct value *
4508 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4509 {
4510   struct dwarf2_loclist_baton *dlbaton
4511     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4512   struct value *val;
4513   const gdb_byte *data;
4514   size_t size;
4515   CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
4516 
4517   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4518   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4519 				  dlbaton->per_cu, dlbaton->per_objfile);
4520 
4521   return val;
4522 }
4523 
4524 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4525    entry.  SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4526    will be thrown.
4527 
4528    Function always returns non-NULL value, it may be marked optimized out if
4529    inferior frame information is not available.  It throws NO_ENTRY_VALUE_ERROR
4530    if it cannot resolve the parameter for any reason.  */
4531 
4532 static struct value *
4533 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4534 {
4535   struct dwarf2_loclist_baton *dlbaton
4536     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4537   const gdb_byte *data;
4538   size_t size;
4539   CORE_ADDR pc;
4540 
4541   if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4542     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4543 
4544   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4545   if (data == NULL)
4546     return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4547 
4548   return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4549 }
4550 
4551 /* Implementation of get_symbol_read_needs from
4552    symbol_computed_ops.  */
4553 
4554 static enum symbol_needs_kind
4555 loclist_symbol_needs (struct symbol *symbol)
4556 {
4557   /* If there's a location list, then assume we need to have a frame
4558      to choose the appropriate location expression.  With tracking of
4559      global variables this is not necessarily true, but such tracking
4560      is disabled in GCC at the moment until we figure out how to
4561      represent it.  */
4562 
4563   return SYMBOL_NEEDS_FRAME;
4564 }
4565 
4566 /* Print a natural-language description of SYMBOL to STREAM.  This
4567    version applies when there is a list of different locations, each
4568    with a specified address range.  */
4569 
4570 static void
4571 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4572 			   struct ui_file *stream)
4573 {
4574   struct dwarf2_loclist_baton *dlbaton
4575     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4576   const gdb_byte *loc_ptr, *buf_end;
4577   dwarf2_per_objfile *per_objfile = dlbaton->per_objfile;
4578   struct objfile *objfile = per_objfile->objfile;
4579   struct gdbarch *gdbarch = objfile->arch ();
4580   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4581   unsigned int addr_size = dlbaton->per_cu->addr_size ();
4582   int offset_size = dlbaton->per_cu->offset_size ();
4583   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4584   /* Adjust base_address for relocatable objects.  */
4585   CORE_ADDR base_offset = objfile->text_section_offset ();
4586   CORE_ADDR base_address = dlbaton->base_address + base_offset;
4587   int done = 0;
4588 
4589   loc_ptr = dlbaton->data;
4590   buf_end = dlbaton->data + dlbaton->size;
4591 
4592   fprintf_filtered (stream, _("multi-location:\n"));
4593 
4594   /* Iterate through locations until we run out.  */
4595   while (!done)
4596     {
4597       CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4598       int length;
4599       enum debug_loc_kind kind;
4600       const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4601 
4602       if (dlbaton->per_cu->version () < 5 && dlbaton->from_dwo)
4603 	kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4604 					       dlbaton->per_objfile,
4605 					       loc_ptr, buf_end, &new_ptr,
4606 					       &low, &high, byte_order);
4607       else if (dlbaton->per_cu->version () < 5)
4608 	kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4609 					   &low, &high,
4610 					   byte_order, addr_size,
4611 					   signed_addr_p);
4612       else
4613 	kind = decode_debug_loclists_addresses (dlbaton->per_cu,
4614 						dlbaton->per_objfile,
4615 						loc_ptr, buf_end, &new_ptr,
4616 						&low, &high, byte_order,
4617 						addr_size, signed_addr_p);
4618       loc_ptr = new_ptr;
4619       switch (kind)
4620 	{
4621 	case DEBUG_LOC_END_OF_LIST:
4622 	  done = 1;
4623 	  continue;
4624 
4625 	case DEBUG_LOC_BASE_ADDRESS:
4626 	  base_address = high + base_offset;
4627 	  fprintf_filtered (stream, _("  Base address %s"),
4628 			    paddress (gdbarch, base_address));
4629 	  continue;
4630 
4631 	case DEBUG_LOC_START_END:
4632 	case DEBUG_LOC_START_LENGTH:
4633 	case DEBUG_LOC_OFFSET_PAIR:
4634 	  break;
4635 
4636 	case DEBUG_LOC_BUFFER_OVERFLOW:
4637 	case DEBUG_LOC_INVALID_ENTRY:
4638 	  error (_("Corrupted DWARF expression for symbol \"%s\"."),
4639 		 symbol->print_name ());
4640 
4641 	default:
4642 	  gdb_assert_not_reached ("bad debug_loc_kind");
4643 	}
4644 
4645       /* Otherwise, a location expression entry.  */
4646       low += base_address;
4647       high += base_address;
4648 
4649       low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
4650       high = gdbarch_adjust_dwarf2_addr (gdbarch, high);
4651 
4652       if (dlbaton->per_cu->version () < 5)
4653 	 {
4654 	   length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4655 	   loc_ptr += 2;
4656 	 }
4657       else
4658 	 {
4659 	   unsigned int bytes_read;
4660 	   length = read_unsigned_leb128 (NULL, loc_ptr, &bytes_read);
4661 	   loc_ptr += bytes_read;
4662 	 }
4663 
4664       /* (It would improve readability to print only the minimum
4665 	 necessary digits of the second number of the range.)  */
4666       fprintf_filtered (stream, _("  Range %s-%s: "),
4667 			paddress (gdbarch, low), paddress (gdbarch, high));
4668 
4669       /* Now describe this particular location.  */
4670       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4671 				   addr_size, offset_size,
4672 				   dlbaton->per_cu, dlbaton->per_objfile);
4673 
4674       fprintf_filtered (stream, "\n");
4675 
4676       loc_ptr += length;
4677     }
4678 }
4679 
4680 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4681    any necessary bytecode in AX.  */
4682 static void
4683 loclist_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
4684 			    struct axs_value *value)
4685 {
4686   struct dwarf2_loclist_baton *dlbaton
4687     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4688   const gdb_byte *data;
4689   size_t size;
4690   unsigned int addr_size = dlbaton->per_cu->addr_size ();
4691 
4692   data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4693   if (size == 0)
4694     value->optimized_out = 1;
4695   else
4696     dwarf2_compile_expr_to_ax (ax, value, addr_size, data, data + size,
4697 			       dlbaton->per_cu, dlbaton->per_objfile);
4698 }
4699 
4700 /* symbol_computed_ops 'generate_c_location' method.  */
4701 
4702 static void
4703 loclist_generate_c_location (struct symbol *sym, string_file *stream,
4704 			     struct gdbarch *gdbarch,
4705 			     unsigned char *registers_used,
4706 			     CORE_ADDR pc, const char *result_name)
4707 {
4708   struct dwarf2_loclist_baton *dlbaton
4709     = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
4710   unsigned int addr_size = dlbaton->per_cu->addr_size ();
4711   const gdb_byte *data;
4712   size_t size;
4713 
4714   data = dwarf2_find_location_expression (dlbaton, &size, pc);
4715   if (size == 0)
4716     error (_("symbol \"%s\" is optimized out"), sym->natural_name ());
4717 
4718   compile_dwarf_expr_to_c (stream, result_name,
4719 			   sym, pc, gdbarch, registers_used, addr_size,
4720 			   data, data + size,
4721 			   dlbaton->per_cu,
4722 			   dlbaton->per_objfile);
4723 }
4724 
4725 /* The set of location functions used with the DWARF-2 expression
4726    evaluator and location lists.  */
4727 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4728   loclist_read_variable,
4729   loclist_read_variable_at_entry,
4730   loclist_symbol_needs,
4731   loclist_describe_location,
4732   1,	/* location_has_loclist */
4733   loclist_tracepoint_var_ref,
4734   loclist_generate_c_location
4735 };
4736 
4737 void _initialize_dwarf2loc ();
4738 void
4739 _initialize_dwarf2loc ()
4740 {
4741   add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4742 			     &entry_values_debug,
4743 			     _("Set entry values and tail call frames "
4744 			       "debugging."),
4745 			     _("Show entry values and tail call frames "
4746 			       "debugging."),
4747 			     _("When non-zero, the process of determining "
4748 			       "parameter values from function entry point "
4749 			       "and tail call frames will be printed."),
4750 			     NULL,
4751 			     show_entry_values_debug,
4752 			     &setdebuglist, &showdebuglist);
4753 
4754   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
4755 			   &dwarf_always_disassemble, _("\
4756 Set whether `info address' always disassembles DWARF expressions."), _("\
4757 Show whether `info address' always disassembles DWARF expressions."), _("\
4758 When enabled, DWARF expressions are always printed in an assembly-like\n\
4759 syntax.  When disabled, expressions will be printed in a more\n\
4760 conversational style, when possible."),
4761 			   NULL,
4762 			   show_dwarf_always_disassemble,
4763 			   &set_dwarf_cmdlist,
4764 			   &show_dwarf_cmdlist);
4765 }
4766