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