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