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