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