1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003-2021 Free Software Foundation, Inc.
5 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7 This file is part of GDB.
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.
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.
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/>. */
35 #include "complaints.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"
46 #include <unordered_set>
47 #include "gdbsupport/underlying.h"
48 #include "gdbsupport/byte-vector.h"
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
);
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. */
62 /* Indicates the end of the list of entries. */
63 DEBUG_LOC_END_OF_LIST
= 0,
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,
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,
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
78 DEBUG_LOC_START_LENGTH
= 3,
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,
85 /* An internal value indicating there is insufficient data. */
86 DEBUG_LOC_BUFFER_OVERFLOW
= -1,
88 /* An internal value indicating an invalid kind of entry was found. */
89 DEBUG_LOC_INVALID_ENTRY
= -2
92 /* Helper function which throws an error if a synthetic pointer is
96 invalid_synthetic_pointer (void)
98 error (_("access outside bounds of object "
99 "referenced via synthetic pointer"));
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. */
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
,
115 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
117 if (buf_end
- loc_ptr
< 2 * addr_size
)
118 return DEBUG_LOC_BUFFER_OVERFLOW
;
121 *low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
123 *low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
124 loc_ptr
+= addr_size
;
127 *high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
129 *high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
130 loc_ptr
+= addr_size
;
134 /* A base-address-selection entry. */
135 if ((*low
& base_mask
) == base_mask
)
136 return DEBUG_LOC_BASE_ADDRESS
;
138 /* An end-of-list entry. */
139 if (*low
== 0 && *high
== 0)
140 return DEBUG_LOC_END_OF_LIST
;
142 return DEBUG_LOC_START_END
;
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. */
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
,
163 if (loc_ptr
== buf_end
)
164 return DEBUG_LOC_BUFFER_OVERFLOW
;
168 case DW_LLE_base_addressx
:
170 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
172 return DEBUG_LOC_BUFFER_OVERFLOW
;
174 *high
= dwarf2_read_addr_index (per_cu
, per_objfile
, u64
);
176 return DEBUG_LOC_BASE_ADDRESS
;
178 case DW_LLE_startx_length
:
179 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
181 return DEBUG_LOC_BUFFER_OVERFLOW
;
183 *low
= dwarf2_read_addr_index (per_cu
, per_objfile
, u64
);
185 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
187 return DEBUG_LOC_BUFFER_OVERFLOW
;
191 return DEBUG_LOC_START_LENGTH
;
193 case DW_LLE_start_length
:
194 if (buf_end
- loc_ptr
< addr_size
)
195 return DEBUG_LOC_BUFFER_OVERFLOW
;
198 *low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
200 *low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
202 loc_ptr
+= addr_size
;
205 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
207 return DEBUG_LOC_BUFFER_OVERFLOW
;
211 return DEBUG_LOC_START_LENGTH
;
213 case DW_LLE_end_of_list
:
215 return DEBUG_LOC_END_OF_LIST
;
217 case DW_LLE_base_address
:
218 if (loc_ptr
+ addr_size
> buf_end
)
219 return DEBUG_LOC_BUFFER_OVERFLOW
;
222 *high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
224 *high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
226 loc_ptr
+= addr_size
;
228 return DEBUG_LOC_BASE_ADDRESS
;
230 case DW_LLE_offset_pair
:
231 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
233 return DEBUG_LOC_BUFFER_OVERFLOW
;
236 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
238 return DEBUG_LOC_BUFFER_OVERFLOW
;
242 return DEBUG_LOC_OFFSET_PAIR
;
244 case DW_LLE_start_end
:
245 if (loc_ptr
+ 2 * addr_size
> buf_end
)
246 return DEBUG_LOC_BUFFER_OVERFLOW
;
249 *low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
251 *low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
253 loc_ptr
+= addr_size
;
255 *high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
257 *high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
259 loc_ptr
+= addr_size
;
261 return DEBUG_LOC_START_END
;
263 /* Following cases are not supported yet. */
264 case DW_LLE_startx_endx
:
265 case DW_LLE_default_location
:
267 return DEBUG_LOC_INVALID_ENTRY
;
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. */
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
)
285 uint64_t low_index
, high_index
;
287 if (loc_ptr
== buf_end
)
288 return DEBUG_LOC_BUFFER_OVERFLOW
;
292 case DW_LLE_GNU_end_of_list_entry
:
294 return DEBUG_LOC_END_OF_LIST
;
296 case DW_LLE_GNU_base_address_selection_entry
:
298 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &high_index
);
300 return DEBUG_LOC_BUFFER_OVERFLOW
;
302 *high
= dwarf2_read_addr_index (per_cu
, per_objfile
, high_index
);
304 return DEBUG_LOC_BASE_ADDRESS
;
306 case DW_LLE_GNU_start_end_entry
:
307 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &low_index
);
309 return DEBUG_LOC_BUFFER_OVERFLOW
;
311 *low
= dwarf2_read_addr_index (per_cu
, per_objfile
, low_index
);
312 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &high_index
);
314 return DEBUG_LOC_BUFFER_OVERFLOW
;
316 *high
= dwarf2_read_addr_index (per_cu
, per_objfile
, high_index
);
318 return DEBUG_LOC_START_END
;
320 case DW_LLE_GNU_start_length_entry
:
321 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &low_index
);
323 return DEBUG_LOC_BUFFER_OVERFLOW
;
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
;
330 *high
+= extract_unsigned_integer (loc_ptr
, 4, byte_order
);
331 *new_ptr
= loc_ptr
+ 4;
332 return DEBUG_LOC_START_LENGTH
;
335 return DEBUG_LOC_INVALID_ENTRY
;
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.
344 For now, only return the first matching location expression; there
345 can be more than one in the list. */
348 dwarf2_find_location_expression (struct dwarf2_loclist_baton
*baton
,
349 size_t *locexpr_length
, CORE_ADDR pc
)
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
;
362 loc_ptr
= baton
->data
;
363 buf_end
= baton
->data
+ baton
->size
;
367 CORE_ADDR low
= 0, high
= 0; /* init for gcc -Wall */
369 enum debug_loc_kind kind
;
370 const gdb_byte
*new_ptr
= NULL
; /* init for gcc -Wall */
372 if (baton
->per_cu
->version () < 5 && baton
->from_dwo
)
373 kind
= decode_debug_loc_dwo_addresses (baton
->per_cu
,
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
,
380 byte_order
, addr_size
,
383 kind
= decode_debug_loclists_addresses (baton
->per_cu
,
385 loc_ptr
, buf_end
, &new_ptr
,
386 &low
, &high
, byte_order
,
387 addr_size
, signed_addr_p
);
392 case DEBUG_LOC_END_OF_LIST
:
396 case DEBUG_LOC_BASE_ADDRESS
:
397 base_address
= high
+ base_offset
;
400 case DEBUG_LOC_START_END
:
401 case DEBUG_LOC_START_LENGTH
:
402 case DEBUG_LOC_OFFSET_PAIR
:
405 case DEBUG_LOC_BUFFER_OVERFLOW
:
406 case DEBUG_LOC_INVALID_ENTRY
:
407 error (_("dwarf2_find_location_expression: "
408 "Corrupted DWARF expression."));
411 gdb_assert_not_reached ("bad debug_loc_kind");
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
)
428 high
+= base_address
;
431 if (baton
->per_cu
->version () < 5)
433 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
438 unsigned int bytes_read
;
440 length
= read_unsigned_leb128 (NULL
, loc_ptr
, &bytes_read
);
441 loc_ptr
+= bytes_read
;
444 if (low
== high
&& pc
== low
)
446 /* This is entry PC record present only at entry point
447 of a function. Verify it is really the function entry point. */
449 const struct block
*pc_block
= block_for_pc (pc
);
450 struct symbol
*pc_func
= NULL
;
453 pc_func
= block_linkage_function (pc_block
);
455 if (pc_func
&& pc
== BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (pc_func
)))
457 *locexpr_length
= length
;
462 if (pc
>= low
&& pc
< high
)
464 *locexpr_length
= length
;
472 /* Implement find_frame_base_location method for LOC_BLOCK functions using
473 DWARF expression for its DW_AT_frame_base. */
476 locexpr_find_frame_base_location (struct symbol
*framefunc
, CORE_ADDR pc
,
477 const gdb_byte
**start
, size_t *length
)
479 struct dwarf2_locexpr_baton
*symbaton
480 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
482 *length
= symbaton
->size
;
483 *start
= symbaton
->data
;
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. */
490 locexpr_get_frame_base (struct symbol
*framefunc
, struct frame_info
*frame
)
492 struct gdbarch
*gdbarch
;
494 struct dwarf2_locexpr_baton
*dlbaton
;
495 const gdb_byte
*start
;
497 struct value
*result
;
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
502 gdb_assert (SYMBOL_BLOCK_OPS (framefunc
)->find_frame_base_location
!= NULL
);
504 gdbarch
= get_frame_arch (frame
);
505 type
= builtin_type (gdbarch
)->builtin_data_ptr
;
506 dlbaton
= (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
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
);
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
518 return value_address (result
);
521 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
522 function uses DWARF expression for its DW_AT_frame_base. */
524 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs
=
526 locexpr_find_frame_base_location
,
527 locexpr_get_frame_base
530 /* Implement find_frame_base_location method for LOC_BLOCK functions using
531 DWARF location list for its DW_AT_frame_base. */
534 loclist_find_frame_base_location (struct symbol
*framefunc
, CORE_ADDR pc
,
535 const gdb_byte
**start
, size_t *length
)
537 struct dwarf2_loclist_baton
*symbaton
538 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
540 *start
= dwarf2_find_location_expression (symbaton
, length
, pc
);
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. */
547 loclist_get_frame_base (struct symbol
*framefunc
, struct frame_info
*frame
)
549 struct gdbarch
*gdbarch
;
551 struct dwarf2_loclist_baton
*dlbaton
;
552 const gdb_byte
*start
;
554 struct value
*result
;
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
559 gdb_assert (SYMBOL_BLOCK_OPS (framefunc
)->find_frame_base_location
!= NULL
);
561 gdbarch
= get_frame_arch (frame
);
562 type
= builtin_type (gdbarch
)->builtin_data_ptr
;
563 dlbaton
= (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
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
);
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
575 return value_address (result
);
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. */
581 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs
=
583 loclist_find_frame_base_location
,
584 loclist_get_frame_base
587 /* See dwarf2loc.h. */
590 func_get_frame_base_dwarf_block (struct symbol
*framefunc
, CORE_ADDR pc
,
591 const gdb_byte
**start
, size_t *length
)
593 if (SYMBOL_BLOCK_OPS (framefunc
) != NULL
)
595 const struct symbol_block_ops
*ops_block
= SYMBOL_BLOCK_OPS (framefunc
);
597 ops_block
->find_frame_base_location (framefunc
, pc
, start
, length
);
603 error (_("Could not find the frame base for \"%s\"."),
604 framefunc
->natural_name ());
610 compute_var_value (const char *name
)
612 struct block_symbol sym
= lookup_symbol (name
, nullptr, VAR_DOMAIN
,
614 if (sym
.symbol
!= nullptr)
615 return value_of_variable (sym
.symbol
, sym
.block
);
619 /* See dwarf2loc.h. */
621 unsigned int entry_values_debug
= 0;
623 /* Helper to set entry_values_debug. */
626 show_entry_values_debug (struct ui_file
*file
, int from_tty
,
627 struct cmd_list_element
*c
, const char *value
)
629 fprintf_filtered (file
,
630 _("Entry values and tail call frames debugging is %s.\n"),
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. */
639 call_site_to_target_addr (struct gdbarch
*call_site_gdbarch
,
640 struct call_site
*call_site
,
641 struct frame_info
*caller_frame
)
643 switch (FIELD_LOC_KIND (call_site
->target
))
645 case FIELD_LOC_KIND_DWARF_BLOCK
:
647 struct dwarf2_locexpr_baton
*dwarf_block
;
649 struct type
*caller_core_addr_type
;
650 struct gdbarch
*caller_arch
;
652 dwarf_block
= FIELD_DWARF_BLOCK (call_site
->target
);
653 if (dwarf_block
== NULL
)
655 struct bound_minimal_symbol msym
;
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 ()));
665 if (caller_frame
== NULL
)
667 struct bound_minimal_symbol msym
;
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 ()));
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
,
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
);
689 return value_as_address (val
);
692 case FIELD_LOC_KIND_PHYSNAME
:
694 const char *physname
;
695 struct bound_minimal_symbol msym
;
697 physname
= FIELD_STATIC_PHYSNAME (call_site
->target
);
699 /* Handle both the mangled and demangled PHYSNAME. */
700 msym
= lookup_minimal_symbol (physname
, NULL
, NULL
);
701 if (msym
.minsym
== NULL
)
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 "
707 physname
, paddress (call_site_gdbarch
, call_site
->pc
),
708 (msym
.minsym
== NULL
? "???"
709 : msym
.minsym
->print_name ()));
712 return BMSYMBOL_VALUE_ADDRESS (msym
);
715 case FIELD_LOC_KIND_PHYSADDR
:
716 return FIELD_STATIC_PHYSADDR (call_site
->target
);
719 internal_error (__FILE__
, __LINE__
, _("invalid call site target kind"));
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. */
727 static struct symbol
*
728 func_addr_to_tail_call_list (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
730 struct symbol
*sym
= find_pc_function (addr
);
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
));
739 type
= SYMBOL_TYPE (sym
);
740 gdb_assert (type
->code () == TYPE_CODE_FUNC
);
741 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FUNC
);
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.
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. */
756 func_verify_no_selftailcall (struct gdbarch
*gdbarch
, CORE_ADDR verify_addr
)
760 /* The verification is completely unordered. Track here function addresses
761 which still need to be iterated. */
762 std::vector
<CORE_ADDR
> todo
;
764 /* Track here CORE_ADDRs which were already visited. */
765 std::unordered_set
<CORE_ADDR
> addr_hash
;
767 todo
.push_back (verify_addr
);
768 while (!todo
.empty ())
770 struct symbol
*func_sym
;
771 struct call_site
*call_site
;
776 func_sym
= func_addr_to_tail_call_list (gdbarch
, addr
);
778 for (call_site
= TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym
));
779 call_site
; call_site
= call_site
->tail_call_next
)
781 CORE_ADDR target_addr
;
783 /* CALLER_FRAME with registers is not available for tail-call jumped
785 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, NULL
);
787 if (target_addr
== verify_addr
)
789 struct bound_minimal_symbol msym
;
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 "
796 (msym
.minsym
== NULL
? "???"
797 : msym
.minsym
->print_name ()),
798 paddress (gdbarch
, verify_addr
));
801 if (addr_hash
.insert (target_addr
).second
)
802 todo
.push_back (target_addr
);
807 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
808 ENTRY_VALUES_DEBUG. */
811 tailcall_dump (struct gdbarch
*gdbarch
, const struct call_site
*call_site
)
813 CORE_ADDR addr
= call_site
->pc
;
814 struct bound_minimal_symbol msym
= lookup_minimal_symbol_by_pc (addr
- 1);
816 fprintf_unfiltered (gdb_stdlog
, " %s(%s)", paddress (gdbarch
, addr
),
817 (msym
.minsym
== NULL
? "???"
818 : msym
.minsym
->print_name ()));
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. */
830 chain_candidate (struct gdbarch
*gdbarch
,
831 gdb::unique_xmalloc_ptr
<struct call_site_chain
> *resultp
,
832 std::vector
<struct call_site
*> *chain
)
834 long length
= chain
->size ();
835 int callers
, callees
, idx
;
837 if (*resultp
== NULL
)
839 /* Create the initial chain containing all the passed PCs. */
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
);
852 if (entry_values_debug
)
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
);
863 if (entry_values_debug
)
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
);
871 /* Intersect callers. */
873 callers
= std::min ((long) (*resultp
)->callers
, length
);
874 for (idx
= 0; idx
< callers
; idx
++)
875 if ((*resultp
)->call_site
[idx
] != chain
->at (idx
))
877 (*resultp
)->callers
= idx
;
881 /* Intersect callees. */
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
))
888 (*resultp
)->callees
= idx
;
892 if (entry_values_debug
)
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
);
905 if ((*resultp
)->callers
== 0 && (*resultp
)->callees
== 0)
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
);
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
);
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. */
927 static gdb::unique_xmalloc_ptr
<call_site_chain
>
928 call_site_find_chain_1 (struct gdbarch
*gdbarch
, CORE_ADDR caller_pc
,
931 CORE_ADDR save_callee_pc
= callee_pc
;
932 gdb::unique_xmalloc_ptr
<struct call_site_chain
> retval
;
933 struct call_site
*call_site
;
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
;
941 /* We are not interested in the specific PC inside the callee function. */
942 callee_pc
= get_pc_function_start (callee_pc
);
944 throw_error (NO_ENTRY_VALUE_ERROR
, _("Unable to find function for PC %s"),
945 paddress (gdbarch
, save_callee_pc
));
947 /* Mark CALL_SITEs so we do not visit the same ones twice. */
948 std::unordered_set
<CORE_ADDR
> addr_hash
;
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
954 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
958 CORE_ADDR target_func_addr
;
959 struct call_site
*target_call_site
;
961 /* CALLER_FRAME with registers is not available for tail-call jumped
963 target_func_addr
= call_site_to_target_addr (gdbarch
, call_site
, NULL
);
965 if (target_func_addr
== callee_pc
)
967 chain_candidate (gdbarch
, &retval
, &chain
);
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
;
977 struct symbol
*target_func
;
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
));
985 /* Attempt to visit TARGET_CALL_SITE. */
987 if (target_call_site
)
989 if (addr_hash
.insert (target_call_site
->pc
).second
)
991 /* Successfully entered TARGET_CALL_SITE. */
993 chain
.push_back (target_call_site
);
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
1002 target_call_site
= NULL
;
1003 while (!chain
.empty ())
1005 call_site
= chain
.back ();
1008 size_t removed
= addr_hash
.erase (call_site
->pc
);
1009 gdb_assert (removed
== 1);
1011 target_call_site
= call_site
->tail_call_next
;
1012 if (target_call_site
)
1016 while (target_call_site
);
1021 call_site
= chain
.back ();
1026 struct bound_minimal_symbol msym_caller
, msym_callee
;
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
));
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. */
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
)
1053 gdb::unique_xmalloc_ptr
<call_site_chain
> retval
;
1057 retval
= call_site_find_chain_1 (gdbarch
, caller_pc
, callee_pc
);
1059 catch (const gdb_exception_error
&e
)
1061 if (e
.error
== NO_ENTRY_VALUE_ERROR
)
1063 if (entry_values_debug
)
1064 exception_print (gdb_stdout
, e
);
1075 /* Return 1 if KIND and KIND_U match PARAMETER. Return 0 otherwise. */
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
)
1082 if (kind
== parameter
->kind
)
1085 case CALL_SITE_PARAMETER_DWARF_REG
:
1086 return kind_u
.dwarf_reg
== parameter
->u
.dwarf_reg
;
1088 case CALL_SITE_PARAMETER_FB_OFFSET
:
1089 return kind_u
.fb_offset
== parameter
->u
.fb_offset
;
1091 case CALL_SITE_PARAMETER_PARAM_OFFSET
:
1092 return kind_u
.param_cu_off
== parameter
->u
.param_cu_off
;
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
)
1106 CORE_ADDR func_addr
, caller_pc
;
1107 struct gdbarch
*gdbarch
;
1108 struct frame_info
*caller_frame
;
1109 struct call_site
*call_site
;
1111 /* Initialize it just to avoid a GCC false warning. */
1112 struct call_site_parameter
*parameter
= NULL
;
1113 CORE_ADDR target_addr
;
1115 while (get_frame_type (frame
) == INLINE_FRAME
)
1117 frame
= get_prev_frame (frame
);
1118 gdb_assert (frame
!= NULL
);
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
))
1126 struct bound_minimal_symbol msym
1127 = lookup_minimal_symbol_by_pc (func_addr
);
1128 struct gdbarch
*caller_gdbarch
= frame_unwind_arch (frame
);
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
);
1140 if (caller_frame
== NULL
)
1142 struct bound_minimal_symbol msym
1143 = lookup_minimal_symbol_by_pc (func_addr
);
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 ()));
1151 caller_pc
= get_frame_pc (caller_frame
);
1152 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
1154 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, caller_frame
);
1155 if (target_addr
!= func_addr
)
1157 struct minimal_symbol
*target_msym
, *func_msym
;
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
));
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
);
1175 for (iparams
= 0; iparams
< call_site
->parameter_count
; iparams
++)
1177 parameter
= &call_site
->parameter
[iparams
];
1178 if (call_site_parameter_matches (parameter
, kind
, kind_u
))
1181 if (iparams
== call_site
->parameter_count
)
1183 struct minimal_symbol
*msym
1184 = lookup_minimal_symbol_by_pc (caller_pc
).minsym
;
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 ());
1194 *per_cu_return
= call_site
->per_cu
;
1195 *per_objfile_return
= call_site
->per_objfile
;
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.
1203 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
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. */
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
)
1216 const gdb_byte
*data_src
;
1220 data_src
= deref_size
== -1 ? parameter
->value
: parameter
->data_value
;
1221 size
= deref_size
== -1 ? parameter
->value_size
: parameter
->data_value_size
;
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"));
1228 /* DW_AT_call_value is a DWARF expression, not a DWARF
1229 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1231 data
= (gdb_byte
*) alloca (size
+ 1);
1232 memcpy (data
, data_src
, size
);
1233 data
[size
] = DW_OP_stack_value
;
1235 return dwarf2_evaluate_loc_desc (type
, caller_frame
, data
, size
+ 1, per_cu
,
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.. */
1243 static struct value
*
1244 entry_data_value_coerce_ref (const struct value
*value
)
1246 struct type
*checked_type
= check_typedef (value_type (value
));
1247 struct value
*target_val
;
1249 if (!TYPE_IS_REFERENCE (checked_type
))
1252 target_val
= (struct value
*) value_computed_closure (value
);
1253 value_incref (target_val
);
1257 /* Implement copy_closure. */
1260 entry_data_value_copy_closure (const struct value
*v
)
1262 struct value
*target_val
= (struct value
*) value_computed_closure (v
);
1264 value_incref (target_val
);
1268 /* Implement free_closure. */
1271 entry_data_value_free_closure (struct value
*v
)
1273 struct value
*target_val
= (struct value
*) value_computed_closure (v
);
1275 value_decref (target_val
);
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. */
1282 static const struct lval_funcs entry_data_value_funcs
=
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
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.
1297 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1298 cannot resolve the parameter for any reason. */
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
)
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
;
1313 parameter
= dwarf_expr_reg_to_entry_parameter (frame
, kind
, kind_u
,
1315 &caller_per_objfile
);
1317 outer_val
= dwarf_entry_parameter_to_value (parameter
, -1 /* deref_size */,
1320 caller_per_objfile
);
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
1327 if (!TYPE_IS_REFERENCE (checked_type
)
1328 || TYPE_TARGET_TYPE (checked_type
) == NULL
)
1331 target_val
= dwarf_entry_parameter_to_value (parameter
,
1332 TYPE_LENGTH (target_type
),
1333 target_type
, caller_frame
,
1335 caller_per_objfile
);
1337 val
= allocate_computed_value (type
, &entry_data_value_funcs
,
1338 release_value (target_val
).release ());
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);
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.
1352 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1353 cannot resolve the parameter for any reason. */
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
)
1359 union call_site_parameter_u kind_u
;
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
,
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
,
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(*)"));
1378 /* Fetch a DW_AT_const_value through a synthetic pointer. */
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
,
1386 struct value
*result
= NULL
;
1387 const gdb_byte
*bytes
;
1390 auto_obstack temp_obstack
;
1391 bytes
= dwarf2_fetch_constant_bytes (die
, per_cu
, per_objfile
,
1392 &temp_obstack
, &len
);
1396 if (byte_offset
>= 0
1397 && byte_offset
+ TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) <= len
)
1399 bytes
+= byte_offset
;
1400 result
= value_from_contents (TYPE_TARGET_TYPE (type
), bytes
);
1403 invalid_synthetic_pointer ();
1406 result
= allocate_optimized_out_value (TYPE_TARGET_TYPE (type
));
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
)
1420 /* Fetch the location expression of the DIE we're pointing to. */
1421 auto get_frame_address_in_block_wrapper
= [frame
] ()
1423 return get_frame_address_in_block (frame
);
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
);
1430 /* Get type of pointed-to DIE. */
1431 struct type
*orig_type
= dwarf2_fetch_die_type_sect_off (die
, per_cu
,
1433 if (orig_type
== NULL
)
1434 invalid_synthetic_pointer ();
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
,
1443 TYPE_TARGET_TYPE (type
),
1446 return fetch_const_value_from_synthetic_pointer (die
, byte_offset
, per_cu
,
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. */
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
)
1464 struct value
*retval
;
1466 if (subobj_type
== NULL
)
1469 subobj_byte_offset
= 0;
1471 else if (subobj_byte_offset
< 0)
1472 invalid_synthetic_pointer ();
1475 return allocate_optimized_out_value (subobj_type
);
1477 dwarf_expr_context
ctx (per_objfile
);
1479 ctx
.per_cu
= per_cu
;
1480 ctx
.obj_address
= 0;
1482 scoped_value_mark free_values
;
1484 ctx
.gdbarch
= per_objfile
->objfile
->arch ();
1485 ctx
.addr_size
= per_cu
->addr_size ();
1489 ctx
.eval (data
, size
);
1491 catch (const gdb_exception_error
&ex
)
1493 if (ex
.error
== NOT_AVAILABLE_ERROR
)
1495 free_values
.free_to_mark ();
1496 retval
= allocate_value (subobj_type
);
1497 mark_value_bytes_unavailable (retval
, 0,
1498 TYPE_LENGTH (subobj_type
));
1501 else if (ex
.error
== NO_ENTRY_VALUE_ERROR
)
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
);
1512 if (ctx
.pieces
.size () > 0)
1514 struct piece_closure
*c
;
1515 ULONGEST bit_size
= 0;
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
1521 if (bit_size
> 8 * TYPE_LENGTH (type
))
1522 invalid_synthetic_pointer ();
1524 c
= allocate_piece_closure (per_cu
, per_objfile
, std::move (ctx
.pieces
),
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
);
1535 switch (ctx
.location
)
1537 case DWARF_VALUE_REGISTER
:
1539 struct gdbarch
*arch
= get_frame_arch (frame
);
1541 = longest_to_int (value_as_long (ctx
.fetch (0)));
1542 int gdb_regnum
= dwarf_reg_to_regnum_or_error (arch
, dwarf_regnum
);
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
))
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
));
1566 case DWARF_VALUE_MEMORY
:
1568 struct type
*ptr_type
;
1569 CORE_ADDR address
= ctx
.fetch_address (0);
1570 bool in_stack_memory
= ctx
.fetch_in_stack_memory (0);
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. */
1580 switch (subobj_type
->code ())
1582 case TYPE_CODE_FUNC
:
1583 case TYPE_CODE_METHOD
:
1584 ptr_type
= builtin_type (ctx
.gdbarch
)->builtin_func_ptr
;
1587 ptr_type
= builtin_type (ctx
.gdbarch
)->builtin_data_ptr
;
1590 address
= value_as_address (value_from_pointer (ptr_type
, address
));
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);
1600 case DWARF_VALUE_STACK
:
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 ();
1608 if (subobj_byte_offset
+ len
> max
)
1609 invalid_synthetic_pointer ();
1611 /* Preserve VALUE because we are going to free values back
1612 to the mark, but we still need the value contents
1614 value_ref_ptr value_holder
= value_ref_ptr::new_reference (value
);
1615 free_values
.free_to_mark ();
1617 retval
= allocate_value (subobj_type
);
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
;
1623 memcpy (value_contents_raw (retval
),
1624 value_contents_all (value
) + subobj_byte_offset
, len
);
1628 case DWARF_VALUE_LITERAL
:
1631 size_t n
= TYPE_LENGTH (subobj_type
);
1633 if (subobj_byte_offset
+ n
> ctx
.len
)
1634 invalid_synthetic_pointer ();
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
);
1643 case DWARF_VALUE_OPTIMIZED_OUT
:
1644 free_values
.free_to_mark ();
1645 retval
= allocate_optimized_out_value (subobj_type
);
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. */
1654 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
1658 set_value_initialized (retval
, ctx
.initialized
);
1663 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
1664 passes 0 as the byte_offset. */
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
)
1672 return dwarf2_evaluate_loc_desc_full (type
, frame
, data
, size
, per_cu
,
1673 per_objfile
, NULL
, 0);
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
1688 dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton
*dlbaton
,
1689 struct frame_info
*frame
,
1690 const struct property_addr_info
*addr_stack
,
1692 bool push_initial_value
,
1695 if (dlbaton
== NULL
|| dlbaton
->size
== 0)
1698 dwarf2_per_objfile
*per_objfile
= dlbaton
->per_objfile
;
1699 dwarf_expr_context
ctx (per_objfile
);
1702 ctx
.per_cu
= dlbaton
->per_cu
;
1703 if (addr_stack
!= nullptr)
1705 ctx
.obj_address
= addr_stack
->addr
;
1706 ctx
.data_view
= addr_stack
->valaddr
;
1709 ctx
.gdbarch
= per_objfile
->objfile
->arch ();
1710 ctx
.addr_size
= dlbaton
->per_cu
->addr_size ();
1712 if (push_initial_value
)
1713 ctx
.push_address (ctx
.obj_address
, false);
1717 ctx
.eval (dlbaton
->data
, dlbaton
->size
);
1719 catch (const gdb_exception_error
&ex
)
1721 if (ex
.error
== NOT_AVAILABLE_ERROR
)
1725 else if (ex
.error
== NO_ENTRY_VALUE_ERROR
)
1727 if (entry_values_debug
)
1728 exception_print (gdb_stdout
, ex
);
1735 switch (ctx
.location
)
1737 case DWARF_VALUE_STACK
:
1738 *is_reference
= false;
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
);
1747 case DWARF_VALUE_LITERAL
:
1748 *valp
= extract_signed_integer (ctx
.data
, ctx
.len
,
1749 gdbarch_byte_order (ctx
.gdbarch
));
1751 /* Unsupported dwarf values. */
1752 case DWARF_VALUE_OPTIMIZED_OUT
:
1753 case DWARF_VALUE_IMPLICIT_POINTER
:
1760 /* See dwarf2loc.h. */
1763 dwarf2_evaluate_property (const struct dynamic_prop
*prop
,
1764 struct frame_info
*frame
,
1765 const struct property_addr_info
*addr_stack
,
1767 bool push_initial_value
)
1772 if (frame
== NULL
&& has_stack_frames ())
1773 frame
= get_selected_frame (NULL
);
1775 switch (prop
->kind ())
1779 const struct dwarf2_property_baton
*baton
1780 = (const struct dwarf2_property_baton
*) prop
->baton ();
1781 gdb_assert (baton
->property_type
!= NULL
);
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
))
1789 struct value
*val
= value_at (baton
->property_type
, *value
);
1790 *value
= value_as_address (val
);
1794 gdb_assert (baton
->property_type
!= NULL
);
1796 struct type
*type
= check_typedef (baton
->property_type
);
1797 if (TYPE_LENGTH (type
) < sizeof (CORE_ADDR
)
1798 && !type
->is_unsigned ())
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. */
1805 = (baton
->locexpr
.per_cu
->addr_size ()
1807 const CORE_ADDR neg_mask
1808 = (~((CORE_ADDR
) 0) << (addr_size
- 1));
1810 /* Check if signed bit is set and sign-extend values. */
1811 if (*value
& neg_mask
)
1822 struct dwarf2_property_baton
*baton
1823 = (struct dwarf2_property_baton
*) prop
->baton ();
1825 const gdb_byte
*data
;
1830 || !get_frame_address_in_block_if_available (frame
, &pc
))
1833 data
= dwarf2_find_location_expression (&baton
->loclist
, &size
, pc
);
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
))
1841 *value
= value_as_address (val
);
1849 *value
= prop
->const_val ();
1852 case PROP_ADDR_OFFSET
:
1854 struct dwarf2_property_baton
*baton
1855 = (struct dwarf2_property_baton
*) prop
->baton ();
1856 const struct property_addr_info
*pinfo
;
1859 for (pinfo
= addr_stack
; pinfo
!= NULL
; pinfo
= pinfo
->next
)
1861 /* This approach lets us avoid checking the qualifiers. */
1862 if (TYPE_MAIN_TYPE (pinfo
->type
)
1863 == TYPE_MAIN_TYPE (baton
->property_type
))
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
);
1873 val
= value_at (baton
->offset_info
.type
,
1874 pinfo
->addr
+ baton
->offset_info
.offset
);
1875 *value
= value_as_address (val
);
1879 case PROP_VARIABLE_NAME
:
1881 struct value
*val
= compute_var_value (prop
->variable_name ());
1884 *value
= value_as_long (val
);
1894 /* See dwarf2loc.h. */
1897 dwarf2_compile_property_to_c (string_file
*stream
,
1898 const char *result_name
,
1899 struct gdbarch
*gdbarch
,
1900 std::vector
<bool> ®isters_used
,
1901 const struct dynamic_prop
*prop
,
1905 struct dwarf2_property_baton
*baton
1906 = (struct dwarf2_property_baton
*) prop
->baton ();
1907 const gdb_byte
*data
;
1909 dwarf2_per_cu_data
*per_cu
;
1910 dwarf2_per_objfile
*per_objfile
;
1912 if (prop
->kind () == PROP_LOCEXPR
)
1914 data
= baton
->locexpr
.data
;
1915 size
= baton
->locexpr
.size
;
1916 per_cu
= baton
->locexpr
.per_cu
;
1917 per_objfile
= baton
->locexpr
.per_objfile
;
1921 gdb_assert (prop
->kind () == PROP_LOCLIST
);
1923 data
= dwarf2_find_location_expression (&baton
->loclist
, &size
, pc
);
1924 per_cu
= baton
->loclist
.per_cu
;
1925 per_objfile
= baton
->loclist
.per_objfile
;
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
);
1934 /* Compute the correct symbol_needs_kind value for the location
1937 Implemented by traversing the logical control flow graph of the
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
,
1949 enum symbol_needs_kind symbol_needs
= SYMBOL_NEEDS_NONE
;
1951 /* If the expression is empty, we have nothing to do. */
1953 return symbol_needs
;
1955 const gdb_byte
*expr_end
= expr
.data () + expr
.size ();
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
;
1961 /* Operations already visited. */
1962 std::unordered_set
<const gdb_byte
*> visited_ops
;
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
)
1969 if (op_ptr
>= expr_end
)
1972 if (visited_ops
.find (op_ptr
) != visited_ops
.end ())
1975 ops_to_visit
.push_back (op_ptr
);
1978 /* Expressions can invoke other expressions with DW_OP_call*. Protect against
1980 const int max_depth
= 256;
1982 if (depth
> max_depth
)
1983 error (_("DWARF-2 expression error: Loop detected."));
1987 /* Initialize the to-visit list with the first operation. */
1988 insert_in_ops_to_visit (&expr
[0]);
1990 while (!ops_to_visit
.empty ())
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
);
1998 dwarf_location_atom op
= (dwarf_location_atom
) *op_ptr
;
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. */
2007 /* The DWARF expression might have a bug causing an infinite
2008 loop. In that case, quitting is the only way out. */
2045 case DW_OP_stack_value
:
2072 case DW_OP_GNU_push_tls_address
:
2074 case DW_OP_GNU_uninit
:
2075 case DW_OP_push_object_address
:
2078 case DW_OP_form_tls_address
:
2079 if (symbol_needs
<= SYMBOL_NEEDS_REGISTERS
)
2080 symbol_needs
= SYMBOL_NEEDS_REGISTERS
;
2084 case DW_OP_GNU_convert
:
2085 case DW_OP_reinterpret
:
2086 case DW_OP_GNU_reinterpret
:
2088 case DW_OP_GNU_addr_index
:
2089 case DW_OP_GNU_const_index
:
2091 case DW_OP_plus_uconst
:
2093 op_ptr
= safe_skip_leb128 (op_ptr
, expr_end
);
2097 op_ptr
= safe_skip_leb128 (op_ptr
, expr_end
);
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
);
2105 case DW_OP_deref_type
:
2106 case DW_OP_GNU_deref_type
:
2108 op_ptr
= safe_skip_leb128 (op_ptr
, expr_end
);
2112 op_ptr
+= addr_size
;
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
;
2211 case DW_OP_implicit_value
:
2214 op_ptr
= safe_read_uleb128 (op_ptr
, expr_end
, &uoffset
);
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
);
2225 case DW_OP_deref_size
:
2232 int64_t offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
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
);
2245 insert_in_ops_to_visit (op_ptr
+ offset
);
2246 insert_in_ops_to_visit (op_ptr
);
2253 unsigned int len
= op
== DW_OP_call2
? 2 : 4;
2255 = (cu_offset
) extract_unsigned_integer (op_ptr
, len
, byte_order
);
2258 auto get_frame_pc
= [&symbol_needs
] ()
2260 symbol_needs
= SYMBOL_NEEDS_FRAME
;
2264 struct dwarf2_locexpr_baton baton
2265 = dwarf2_fetch_die_loc_cu_off (cu_off
, per_cu
,
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
)
2273 gdbarch
*arch
= baton
.per_objfile
->objfile
->arch ();
2274 gdb::array_view
<const gdb_byte
> sub_expr (baton
.data
,
2277 = dwarf2_get_symbol_read_needs (sub_expr
,
2280 gdbarch_byte_order (arch
),
2281 baton
.per_cu
->addr_size (),
2282 baton
.per_cu
->ref_addr_size (),
2288 case DW_OP_GNU_variable_value
:
2290 sect_offset sect_off
2291 = (sect_offset
) extract_unsigned_integer (op_ptr
,
2294 op_ptr
+= ref_addr_size
;
2296 struct type
*die_type
2297 = dwarf2_fetch_die_type_sect_off (sect_off
, per_cu
,
2300 if (die_type
== NULL
)
2301 error (_("Bad DW_OP_GNU_variable_value DIE."));
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."));
2311 auto get_frame_pc
= [&symbol_needs
] ()
2313 symbol_needs
= SYMBOL_NEEDS_FRAME
;
2317 struct dwarf2_locexpr_baton baton
2318 = dwarf2_fetch_die_loc_sect_off (sect_off
, per_cu
,
2320 get_frame_pc
, true);
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
)
2326 gdbarch
*arch
= baton
.per_objfile
->objfile
->arch ();
2327 gdb::array_view
<const gdb_byte
> sub_expr (baton
.data
,
2330 = dwarf2_get_symbol_read_needs (sub_expr
,
2333 gdbarch_byte_order (arch
),
2334 baton
.per_cu
->addr_size (),
2335 baton
.per_cu
->ref_addr_size (),
2341 case DW_OP_const_type
:
2342 case DW_OP_GNU_const_type
:
2345 op_ptr
= safe_read_uleb128 (op_ptr
, expr_end
, &uoffset
);
2346 gdb_byte offset
= *op_ptr
++;
2352 error (_("Unhandled DWARF expression opcode 0x%x"), op
);
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
)
2360 insert_in_ops_to_visit (op_ptr
);
2363 return symbol_needs
;
2366 /* A helper function that throws an unimplemented error mentioning a
2367 given DWARF operator. */
2369 static void ATTRIBUTE_NORETURN
2370 unimplemented (unsigned int op
)
2372 const char *name
= get_DW_OP_name (op
);
2375 error (_("DWARF operator %s cannot be translated to an agent expression"),
2378 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2379 "to an agent expression"),
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. */
2390 dwarf_reg_to_regnum (struct gdbarch
*arch
, int dwarf_reg
)
2392 int reg
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_reg
);
2396 complaint (_("bad DWARF register number %d"), dwarf_reg
);
2401 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2402 Throw an error because DWARF_REG is bad. */
2405 throw_bad_regnum_error (ULONGEST dwarf_reg
)
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
));
2416 /* See dwarf2loc.h. */
2419 dwarf_reg_to_regnum_or_error (struct gdbarch
*arch
, ULONGEST dwarf_reg
)
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
);
2429 throw_bad_regnum_error (dwarf_reg
);
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
2440 access_memory (struct gdbarch
*arch
, struct agent_expr
*expr
, ULONGEST nbits
)
2442 ULONGEST nbytes
= (nbits
+ 7) / 8;
2444 gdb_assert (nbytes
> 0 && nbytes
<= sizeof (LONGEST
));
2447 ax_trace_quick (expr
, nbytes
);
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
);
2456 ax_simple (expr
, aop_ref64
);
2458 /* If we read exactly the number of bytes we wanted, we're done. */
2459 if (8 * nbytes
== nbits
)
2462 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
)
2464 /* On a bits-big-endian machine, we want the high-order
2466 ax_const_l (expr
, 8 * nbytes
- nbits
);
2467 ax_simple (expr
, aop_rsh_unsigned
);
2471 /* On a bits-little-endian box, we want the low-order NBITS. */
2472 ax_zero_ext (expr
, nbits
);
2476 /* Compile a DWARF location expression to an agent expression.
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.
2485 This will throw an exception for various kinds of errors -- for
2486 example, if the expression cannot be compiled, or if the expression
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
)
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
;
2505 std::vector
<int> offsets (op_end
- op_ptr
, -1);
2507 /* By default we are making an address. */
2508 loc
->kind
= axs_lvalue_memory
;
2510 while (op_ptr
< op_end
)
2512 enum dwarf_location_atom op
= (enum dwarf_location_atom
) *op_ptr
;
2513 uint64_t uoffset
, reg
;
2517 offsets
[op_ptr
- base
] = expr
->len
;
2520 /* Our basic approach to code generation is to map DWARF
2521 operations directly to AX operations. However, there are
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
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. */
2570 ax_const_l (expr
, op
- DW_OP_lit0
);
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
);
2586 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 1, byte_order
));
2591 ax_const_l (expr
, extract_signed_integer (op_ptr
, 1, byte_order
));
2596 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 2, byte_order
));
2601 ax_const_l (expr
, extract_signed_integer (op_ptr
, 2, byte_order
));
2606 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 4, byte_order
));
2611 ax_const_l (expr
, extract_signed_integer (op_ptr
, 4, byte_order
));
2616 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 8, byte_order
));
2621 ax_const_l (expr
, extract_signed_integer (op_ptr
, 8, byte_order
));
2626 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
2627 ax_const_l (expr
, uoffset
);
2631 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
2632 ax_const_l (expr
, offset
);
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
;
2673 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
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
;
2679 case DW_OP_implicit_value
:
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"),
2690 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, len
,
2693 dwarf_expr_require_composition (op_ptr
, op_end
,
2694 "DW_OP_implicit_value");
2696 loc
->kind
= axs_rvalue
;
2700 case DW_OP_stack_value
:
2701 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
2702 loc
->kind
= axs_rvalue
;
2737 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
2738 i
= dwarf_reg_to_regnum_or_error (arch
, op
- DW_OP_breg0
);
2742 ax_const_l (expr
, offset
);
2743 ax_simple (expr
, aop_add
);
2749 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
2750 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
2751 i
= dwarf_reg_to_regnum_or_error (arch
, reg
);
2755 ax_const_l (expr
, offset
);
2756 ax_simple (expr
, aop_add
);
2763 const gdb_byte
*datastart
;
2765 const struct block
*b
;
2766 struct symbol
*framefunc
;
2768 b
= block_for_pc (expr
->scope
);
2771 error (_("No block found for address"));
2773 framefunc
= block_linkage_function (b
);
2776 error (_("No function found for block"));
2778 func_get_frame_base_dwarf_block (framefunc
, expr
->scope
,
2779 &datastart
, &datalen
);
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
,
2785 if (loc
->kind
== axs_lvalue_register
)
2786 require_rvalue (expr
, loc
);
2790 ax_const_l (expr
, offset
);
2791 ax_simple (expr
, aop_add
);
2794 loc
->kind
= axs_lvalue_memory
;
2799 ax_simple (expr
, aop_dup
);
2803 ax_simple (expr
, aop_pop
);
2808 ax_pick (expr
, offset
);
2812 ax_simple (expr
, aop_swap
);
2820 ax_simple (expr
, aop_rot
);
2824 case DW_OP_deref_size
:
2828 if (op
== DW_OP_deref_size
)
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
);
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
);
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
);
2863 /* Sign extend the operand. */
2864 ax_ext (expr
, addr_size_bits
);
2865 ax_simple (expr
, aop_bit_not
);
2868 case DW_OP_plus_uconst
:
2869 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
2870 /* It would be really weird to emit `DW_OP_plus_uconst 0',
2871 but we micro-optimize anyhow. */
2874 ax_const_l (expr
, reg
);
2875 ax_simple (expr
, aop_add
);
2880 ax_simple (expr
, aop_bit_and
);
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
);
2893 ax_simple (expr
, aop_sub
);
2897 ax_simple (expr
, aop_rem_unsigned
);
2901 ax_simple (expr
, aop_mul
);
2905 ax_simple (expr
, aop_bit_or
);
2909 ax_simple (expr
, aop_add
);
2913 ax_simple (expr
, aop_lsh
);
2917 ax_simple (expr
, aop_rsh_unsigned
);
2921 ax_simple (expr
, aop_rsh_signed
);
2925 ax_simple (expr
, aop_bit_xor
);
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
);
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
);
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
);
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
);
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
);
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
);
2986 case DW_OP_call_frame_cfa
:
2989 CORE_ADDR text_offset
;
2991 const gdb_byte
*cfa_start
, *cfa_end
;
2993 if (dwarf2_fetch_cfa_info (arch
, expr
->scope
, per_cu
,
2995 &text_offset
, &cfa_start
, &cfa_end
))
2998 ax_reg (expr
, regnum
);
3001 ax_const_l (expr
, off
);
3002 ax_simple (expr
, aop_add
);
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
);
3013 loc
->kind
= axs_lvalue_memory
;
3017 case DW_OP_GNU_push_tls_address
:
3018 case DW_OP_form_tls_address
:
3022 case DW_OP_push_object_address
:
3027 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
3029 i
= ax_goto (expr
, aop_goto
);
3030 dw_labels
.push_back (op_ptr
+ offset
- base
);
3031 patches
.push_back (i
);
3035 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
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
);
3048 case DW_OP_bit_piece
:
3052 if (op_ptr
- 1 == previous_piece
)
3053 error (_("Cannot translate empty pieces to agent expressions"));
3054 previous_piece
= op_ptr
- 1;
3056 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
3057 if (op
== DW_OP_piece
)
3063 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
3065 if (bits_collected
+ size
> 8 * sizeof (LONGEST
))
3066 error (_("Expression pieces exceed word size"));
3068 /* Access the bits. */
3071 case axs_lvalue_register
:
3072 ax_reg (expr
, loc
->u
.reg
);
3075 case axs_lvalue_memory
:
3076 /* Offset the pointer, if needed. */
3079 ax_const_l (expr
, uoffset
/ 8);
3080 ax_simple (expr
, aop_add
);
3083 access_memory (arch
, expr
, size
);
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
3092 if (bits_collected
> 0)
3094 if (bits_big_endian
)
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. */
3104 ax_const_l (expr
, size
);
3105 ax_simple (expr
, aop_lsh
);
3107 ax_simple (expr
, aop_bit_or
);
3110 bits_collected
+= size
;
3111 loc
->kind
= axs_rvalue
;
3115 case DW_OP_GNU_uninit
:
3121 struct dwarf2_locexpr_baton block
;
3122 int size
= (op
== DW_OP_call2
? 2 : 4);
3124 uoffset
= extract_unsigned_integer (op_ptr
, size
, byte_order
);
3127 auto get_frame_pc_from_expr
= [expr
] ()
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
);
3135 /* DW_OP_call_ref is currently not supported. */
3136 gdb_assert (block
.per_cu
== per_cu
);
3138 dwarf2_compile_expr_to_ax (expr
, loc
, addr_size
, block
.data
,
3139 block
.data
+ block
.size
, per_cu
,
3144 case DW_OP_call_ref
:
3147 case DW_OP_GNU_variable_value
:
3155 /* Patch all the branches we emitted. */
3156 for (int i
= 0; i
< patches
.size (); ++i
)
3158 int targ
= offsets
[dw_labels
[i
]];
3160 internal_error (__FILE__
, __LINE__
, _("invalid label"));
3161 ax_label (expr
, patches
[i
], targ
);
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
)
3171 struct dwarf2_locexpr_baton
*dlbaton
3172 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
3175 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
3176 dlbaton
->size
, dlbaton
->per_cu
,
3177 dlbaton
->per_objfile
);
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
3186 static struct value
*
3187 locexpr_read_variable_at_entry (struct symbol
*symbol
, struct frame_info
*frame
)
3189 struct dwarf2_locexpr_baton
*dlbaton
3190 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
3192 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
3196 /* Implementation of get_symbol_read_needs from
3197 symbol_computed_ops. */
3199 static enum symbol_needs_kind
3200 locexpr_get_symbol_read_needs (struct symbol
*symbol
)
3202 struct dwarf2_locexpr_baton
*dlbaton
3203 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
3205 gdbarch
*arch
= dlbaton
->per_objfile
->objfile
->arch ();
3206 gdb::array_view
<const gdb_byte
> expr (dlbaton
->data
, dlbaton
->size
);
3208 return dwarf2_get_symbol_read_needs (expr
,
3210 dlbaton
->per_objfile
,
3211 gdbarch_byte_order (arch
),
3212 dlbaton
->per_cu
->addr_size (),
3213 dlbaton
->per_cu
->ref_addr_size ());
3216 /* Return true if DATA points to the end of a piece. END is one past
3217 the last byte in the expression. */
3220 piece_end_p (const gdb_byte
*data
, const gdb_byte
*end
)
3222 return data
== end
|| data
[0] == DW_OP_piece
|| data
[0] == DW_OP_bit_piece
;
3225 /* Helper for locexpr_describe_location_piece that finds the name of a
3229 locexpr_regname (struct gdbarch
*gdbarch
, int dwarf_regnum
)
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. */
3240 /* The text is output as "$bad_register_number".
3241 That is why we use the underscores. */
3242 return _("bad_register_number");
3244 return gdbarch_register_name (gdbarch
, regnum
);
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. */
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
)
3260 objfile
*objfile
= per_objfile
->objfile
;
3261 struct gdbarch
*gdbarch
= objfile
->arch ();
3264 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
3266 fprintf_filtered (stream
, _("a variable in $%s"),
3267 locexpr_regname (gdbarch
, data
[0] - DW_OP_reg0
));
3270 else if (data
[0] == DW_OP_regx
)
3274 data
= safe_read_uleb128 (data
+ 1, end
, ®
);
3275 fprintf_filtered (stream
, _("a variable in $%s"),
3276 locexpr_regname (gdbarch
, reg
));
3278 else if (data
[0] == DW_OP_fbreg
)
3280 const struct block
*b
;
3281 struct symbol
*framefunc
;
3283 int64_t frame_offset
;
3284 const gdb_byte
*base_data
, *new_data
, *save_data
= data
;
3286 int64_t base_offset
= 0;
3288 new_data
= safe_read_sleb128 (data
+ 1, end
, &frame_offset
);
3289 if (!piece_end_p (new_data
, end
))
3293 b
= block_for_pc (addr
);
3296 error (_("No block found for address for symbol \"%s\"."),
3297 symbol
->print_name ());
3299 framefunc
= block_linkage_function (b
);
3302 error (_("No function found for block for symbol \"%s\"."),
3303 symbol
->print_name ());
3305 func_get_frame_base_dwarf_block (framefunc
, addr
, &base_data
, &base_size
);
3307 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
3309 const gdb_byte
*buf_end
;
3311 frame_reg
= base_data
[0] - DW_OP_breg0
;
3312 buf_end
= safe_read_sleb128 (base_data
+ 1, base_data
+ base_size
,
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 ());
3319 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
3321 /* The frame base is just the register, with no offset. */
3322 frame_reg
= base_data
[0] - DW_OP_reg0
;
3327 /* We don't know what to do with the frame base expression,
3328 so we can't trace this variable; give up. */
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
));
3337 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
3338 && piece_end_p (data
, end
))
3342 data
= safe_read_sleb128 (data
+ 1, end
, &offset
);
3344 fprintf_filtered (stream
,
3345 _("a variable at offset %s from base reg $%s"),
3347 locexpr_regname (gdbarch
, data
[0] - DW_OP_breg0
));
3350 /* The location expression for a TLS variable looks like this (on a
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)
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. */
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
))
3373 offset
= extract_unsigned_integer (data
+ 1, addr_size
,
3374 gdbarch_byte_order (gdbarch
));
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
));
3381 data
+= 1 + addr_size
+ 1;
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
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
))
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
));
3407 else if (data
[0] >= DW_OP_lit0
3408 && data
[0] <= DW_OP_lit31
3410 && data
[1] == DW_OP_stack_value
)
3412 fprintf_filtered (stream
, _("the constant %d"), data
[0] - DW_OP_lit0
);
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. */
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
)
3437 || (data
[0] != DW_OP_piece
&& data
[0] != DW_OP_bit_piece
)))
3439 enum dwarf_location_atom op
= (enum dwarf_location_atom
) *data
++;
3444 name
= get_DW_OP_name (op
);
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
);
3455 ul
= extract_unsigned_integer (data
, addr_size
,
3456 gdbarch_byte_order (arch
));
3458 fprintf_filtered (stream
, " 0x%s", phex_nz (ul
, addr_size
));
3462 ul
= extract_unsigned_integer (data
, 1, gdbarch_byte_order (arch
));
3464 fprintf_filtered (stream
, " %s", pulongest (ul
));
3468 l
= extract_signed_integer (data
, 1, gdbarch_byte_order (arch
));
3470 fprintf_filtered (stream
, " %s", plongest (l
));
3474 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
3476 fprintf_filtered (stream
, " %s", pulongest (ul
));
3480 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3482 fprintf_filtered (stream
, " %s", plongest (l
));
3486 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
3488 fprintf_filtered (stream
, " %s", pulongest (ul
));
3492 l
= extract_signed_integer (data
, 4, gdbarch_byte_order (arch
));
3494 fprintf_filtered (stream
, " %s", plongest (l
));
3498 ul
= extract_unsigned_integer (data
, 8, gdbarch_byte_order (arch
));
3500 fprintf_filtered (stream
, " %s", pulongest (ul
));
3504 l
= extract_signed_integer (data
, 8, gdbarch_byte_order (arch
));
3506 fprintf_filtered (stream
, " %s", plongest (l
));
3510 data
= safe_read_uleb128 (data
, end
, &ul
);
3511 fprintf_filtered (stream
, " %s", pulongest (ul
));
3515 data
= safe_read_sleb128 (data
, end
, &l
);
3516 fprintf_filtered (stream
, " %s", plongest (l
));
3551 fprintf_filtered (stream
, " [$%s]",
3552 locexpr_regname (arch
, op
- DW_OP_reg0
));
3556 data
= safe_read_uleb128 (data
, end
, &ul
);
3557 fprintf_filtered (stream
, " %s [$%s]", pulongest (ul
),
3558 locexpr_regname (arch
, (int) ul
));
3561 case DW_OP_implicit_value
:
3562 data
= safe_read_uleb128 (data
, end
, &ul
);
3564 fprintf_filtered (stream
, " %s", pulongest (ul
));
3599 data
= safe_read_sleb128 (data
, end
, &l
);
3600 fprintf_filtered (stream
, " %s [$%s]", plongest (l
),
3601 locexpr_regname (arch
, op
- DW_OP_breg0
));
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",
3609 locexpr_regname (arch
, (int) ul
),
3614 data
= safe_read_sleb128 (data
, end
, &l
);
3615 fprintf_filtered (stream
, " %s", plongest (l
));
3618 case DW_OP_xderef_size
:
3619 case DW_OP_deref_size
:
3621 fprintf_filtered (stream
, " %d", *data
);
3625 case DW_OP_plus_uconst
:
3626 data
= safe_read_uleb128 (data
, end
, &ul
);
3627 fprintf_filtered (stream
, " %s", pulongest (ul
));
3631 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3633 fprintf_filtered (stream
, " to %ld",
3634 (long) (data
+ l
- start
));
3638 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3640 fprintf_filtered (stream
, " %ld",
3641 (long) (data
+ l
- start
));
3645 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
3647 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 2));
3651 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
3653 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 4));
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
));
3664 data
= safe_read_uleb128 (data
, end
, &ul
);
3665 fprintf_filtered (stream
, " %s (bytes)", pulongest (ul
));
3668 case DW_OP_bit_piece
:
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
));
3679 case DW_OP_implicit_pointer
:
3680 case DW_OP_GNU_implicit_pointer
:
3682 ul
= extract_unsigned_integer (data
, offset_size
,
3683 gdbarch_byte_order (arch
));
3684 data
+= offset_size
;
3686 data
= safe_read_sleb128 (data
, end
, &l
);
3688 fprintf_filtered (stream
, " DIE %s offset %s",
3689 phex_nz (ul
, offset_size
),
3694 case DW_OP_deref_type
:
3695 case DW_OP_GNU_deref_type
:
3697 int deref_addr_size
= *data
++;
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),
3711 case DW_OP_const_type
:
3712 case DW_OP_GNU_const_type
:
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));
3725 fprintf_filtered (stream
, " %d byte block:", n
);
3726 for (int i
= 0; i
< n
; ++i
)
3727 fprintf_filtered (stream
, " %02x", data
[i
]);
3732 case DW_OP_regval_type
:
3733 case DW_OP_GNU_regval_type
:
3738 data
= safe_read_uleb128 (data
, end
, ®
);
3739 data
= safe_read_uleb128 (data
, end
, &ul
);
3740 cu_offset type_die
= (cu_offset
) ul
;
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
));
3752 case DW_OP_GNU_convert
:
3753 case DW_OP_reinterpret
:
3754 case DW_OP_GNU_reinterpret
:
3756 data
= safe_read_uleb128 (data
, end
, &ul
);
3757 cu_offset type_die
= (cu_offset
) ul
;
3759 if (to_underlying (type_die
) == 0)
3760 fprintf_filtered (stream
, "<0>");
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));
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
);
3784 case DW_OP_GNU_parameter_ref
:
3785 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
3787 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 4));
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
));
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
));
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
));
3811 fprintf_filtered (stream
, "\n");
3817 static bool dwarf_always_disassemble
;
3820 show_dwarf_always_disassemble (struct ui_file
*file
, int from_tty
,
3821 struct cmd_list_element
*c
, const char *value
)
3823 fprintf_filtered (file
,
3824 _("Whether to always disassemble "
3825 "DWARF expressions is %s.\n"),
3829 /* Describe a single location, which may in turn consist of multiple
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
)
3840 const gdb_byte
*end
= data
+ size
;
3841 int first_piece
= 1, bad
= 0;
3842 objfile
*objfile
= per_objfile
->objfile
;
3846 const gdb_byte
*here
= data
;
3847 int disassemble
= 1;
3852 fprintf_filtered (stream
, _(", and "));
3854 if (!dwarf_always_disassemble
)
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. */
3862 || data
[0] == DW_OP_piece
3863 || data
[0] == DW_OP_bit_piece
)
3868 fprintf_filtered (stream
, _("a complex DWARF expression:\n"));
3869 data
= disassemble_dwarf_expression (stream
,
3871 addr_size
, offset_size
, data
,
3873 dwarf_always_disassemble
,
3874 per_cu
, per_objfile
);
3879 int empty
= data
== here
;
3882 fprintf_filtered (stream
, " ");
3883 if (data
[0] == DW_OP_piece
)
3887 data
= safe_read_uleb128 (data
+ 1, end
, &bytes
);
3890 fprintf_filtered (stream
, _("an empty %s-byte piece"),
3893 fprintf_filtered (stream
, _(" [%s-byte piece]"),
3896 else if (data
[0] == DW_OP_bit_piece
)
3898 uint64_t bits
, offset
;
3900 data
= safe_read_uleb128 (data
+ 1, end
, &bits
);
3901 data
= safe_read_uleb128 (data
, end
, &offset
);
3904 fprintf_filtered (stream
,
3905 _("an empty %s-bit piece"),
3908 fprintf_filtered (stream
,
3909 _(" [%s-bit piece, offset %s bits]"),
3910 pulongest (bits
), pulongest (offset
));
3920 if (bad
|| data
> end
)
3921 error (_("Corrupted DWARF2 expression for \"%s\"."),
3922 symbol
->print_name ());
3925 /* Print a natural-language description of SYMBOL to STREAM. This
3926 version is for a symbol with a single location. */
3929 locexpr_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
3930 struct ui_file
*stream
)
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 ();
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
);
3943 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3944 any necessary bytecode in AX. */
3947 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct agent_expr
*ax
,
3948 struct axs_value
*value
)
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 ();
3954 if (dlbaton
->size
== 0)
3955 value
->optimized_out
= 1;
3957 dwarf2_compile_expr_to_ax (ax
, value
, addr_size
, dlbaton
->data
,
3958 dlbaton
->data
+ dlbaton
->size
, dlbaton
->per_cu
,
3959 dlbaton
->per_objfile
);
3962 /* symbol_computed_ops 'generate_c_location' method. */
3965 locexpr_generate_c_location (struct symbol
*sym
, string_file
*stream
,
3966 struct gdbarch
*gdbarch
,
3967 std::vector
<bool> ®isters_used
,
3968 CORE_ADDR pc
, const char *result_name
)
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 ();
3974 if (dlbaton
->size
== 0)
3975 error (_("symbol \"%s\" is optimized out"), sym
->natural_name ());
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
);
3983 /* The set of location functions used with the DWARF-2 expression
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
3996 /* Wrapper functions for location lists. These generally find
3997 the appropriate location expression and call something above. */
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
)
4004 struct dwarf2_loclist_baton
*dlbaton
4005 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4007 const gdb_byte
*data
;
4009 CORE_ADDR pc
= frame
? get_frame_address_in_block (frame
) : 0;
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
);
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
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. */
4026 static struct value
*
4027 loclist_read_variable_at_entry (struct symbol
*symbol
, struct frame_info
*frame
)
4029 struct dwarf2_loclist_baton
*dlbaton
4030 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4031 const gdb_byte
*data
;
4035 if (frame
== NULL
|| !get_frame_func_if_available (frame
, &pc
))
4036 return allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
4038 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
4040 return allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
4042 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol
), frame
, data
, size
);
4045 /* Implementation of get_symbol_read_needs from
4046 symbol_computed_ops. */
4048 static enum symbol_needs_kind
4049 loclist_symbol_needs (struct symbol
*symbol
)
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
4057 return SYMBOL_NEEDS_FRAME
;
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. */
4065 loclist_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
4066 struct ui_file
*stream
)
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
;
4083 loc_ptr
= dlbaton
->data
;
4084 buf_end
= dlbaton
->data
+ dlbaton
->size
;
4086 fprintf_filtered (stream
, _("multi-location:\n"));
4088 /* Iterate through locations until we run out. */
4091 CORE_ADDR low
= 0, high
= 0; /* init for gcc -Wall */
4093 enum debug_loc_kind kind
;
4094 const gdb_byte
*new_ptr
= NULL
; /* init for gcc -Wall */
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
,
4104 byte_order
, addr_size
,
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
);
4115 case DEBUG_LOC_END_OF_LIST
:
4119 case DEBUG_LOC_BASE_ADDRESS
:
4120 base_address
= high
+ base_offset
;
4121 fprintf_filtered (stream
, _(" Base address %s"),
4122 paddress (gdbarch
, base_address
));
4125 case DEBUG_LOC_START_END
:
4126 case DEBUG_LOC_START_LENGTH
:
4127 case DEBUG_LOC_OFFSET_PAIR
:
4130 case DEBUG_LOC_BUFFER_OVERFLOW
:
4131 case DEBUG_LOC_INVALID_ENTRY
:
4132 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4133 symbol
->print_name ());
4136 gdb_assert_not_reached ("bad debug_loc_kind");
4139 /* Otherwise, a location expression entry. */
4140 low
+= base_address
;
4141 high
+= base_address
;
4143 low
= gdbarch_adjust_dwarf2_addr (gdbarch
, low
);
4144 high
= gdbarch_adjust_dwarf2_addr (gdbarch
, high
);
4146 if (dlbaton
->per_cu
->version () < 5)
4148 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
4153 unsigned int bytes_read
;
4154 length
= read_unsigned_leb128 (NULL
, loc_ptr
, &bytes_read
);
4155 loc_ptr
+= bytes_read
;
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
));
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
);
4168 fprintf_filtered (stream
, "\n");
4174 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4175 any necessary bytecode in AX. */
4177 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct agent_expr
*ax
,
4178 struct axs_value
*value
)
4180 struct dwarf2_loclist_baton
*dlbaton
4181 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4182 const gdb_byte
*data
;
4184 unsigned int addr_size
= dlbaton
->per_cu
->addr_size ();
4186 data
= dwarf2_find_location_expression (dlbaton
, &size
, ax
->scope
);
4188 value
->optimized_out
= 1;
4190 dwarf2_compile_expr_to_ax (ax
, value
, addr_size
, data
, data
+ size
,
4191 dlbaton
->per_cu
, dlbaton
->per_objfile
);
4194 /* symbol_computed_ops 'generate_c_location' method. */
4197 loclist_generate_c_location (struct symbol
*sym
, string_file
*stream
,
4198 struct gdbarch
*gdbarch
,
4199 std::vector
<bool> ®isters_used
,
4200 CORE_ADDR pc
, const char *result_name
)
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
;
4208 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
4210 error (_("symbol \"%s\" is optimized out"), sym
->natural_name ());
4212 compile_dwarf_expr_to_c (stream
, result_name
,
4213 sym
, pc
, gdbarch
, registers_used
, addr_size
,
4216 dlbaton
->per_objfile
);
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
4231 void _initialize_dwarf2loc ();
4233 _initialize_dwarf2loc ()
4235 add_setshow_zuinteger_cmd ("entry-values", class_maintenance
,
4236 &entry_values_debug
,
4237 _("Set entry values and tail call frames "
4239 _("Show entry values and tail call frames "
4241 _("When non-zero, the process of determining "
4242 "parameter values from function entry point "
4243 "and tail call frames will be printed."),
4245 show_entry_values_debug
,
4246 &setdebuglist
, &showdebuglist
);
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."),
4256 show_dwarf_always_disassemble
,
4258 &show_dwarf_cmdlist
);