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