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