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