1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003-2020 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 (struct type
*type
,
51 struct frame_info
*frame
,
54 struct dwarf2_per_cu_data
*per_cu
,
55 struct type
*subobj_type
,
56 LONGEST subobj_byte_offset
);
58 static struct call_site_parameter
*dwarf_expr_reg_to_entry_parameter
59 (struct frame_info
*frame
,
60 enum call_site_parameter_kind kind
,
61 union call_site_parameter_u kind_u
,
62 struct dwarf2_per_cu_data
**per_cu_return
);
64 static struct value
*indirect_synthetic_pointer
65 (sect_offset die
, LONGEST byte_offset
,
66 struct dwarf2_per_cu_data
*per_cu
,
67 struct frame_info
*frame
,
68 struct type
*type
, bool resolve_abstract_p
= false);
70 /* Until these have formal names, we define these here.
71 ref: http://gcc.gnu.org/wiki/DebugFission
72 Each entry in .debug_loc.dwo begins with a byte that describes the entry,
73 and is then followed by data specific to that entry. */
77 /* Indicates the end of the list of entries. */
78 DEBUG_LOC_END_OF_LIST
= 0,
80 /* This is followed by an unsigned LEB128 number that is an index into
81 .debug_addr and specifies the base address for all following entries. */
82 DEBUG_LOC_BASE_ADDRESS
= 1,
84 /* This is followed by two unsigned LEB128 numbers that are indices into
85 .debug_addr and specify the beginning and ending addresses, and then
86 a normal location expression as in .debug_loc. */
87 DEBUG_LOC_START_END
= 2,
89 /* This is followed by an unsigned LEB128 number that is an index into
90 .debug_addr and specifies the beginning address, and a 4 byte unsigned
91 number that specifies the length, and then a normal location expression
93 DEBUG_LOC_START_LENGTH
= 3,
95 /* An internal value indicating there is insufficient data. */
96 DEBUG_LOC_BUFFER_OVERFLOW
= -1,
98 /* An internal value indicating an invalid kind of entry was found. */
99 DEBUG_LOC_INVALID_ENTRY
= -2
102 /* Helper function which throws an error if a synthetic pointer is
106 invalid_synthetic_pointer (void)
108 error (_("access outside bounds of object "
109 "referenced via synthetic pointer"));
112 /* Decode the addresses in a non-dwo .debug_loc entry.
113 A pointer to the next byte to examine is returned in *NEW_PTR.
114 The encoded low,high addresses are return in *LOW,*HIGH.
115 The result indicates the kind of entry found. */
117 static enum debug_loc_kind
118 decode_debug_loc_addresses (const gdb_byte
*loc_ptr
, const gdb_byte
*buf_end
,
119 const gdb_byte
**new_ptr
,
120 CORE_ADDR
*low
, CORE_ADDR
*high
,
121 enum bfd_endian byte_order
,
122 unsigned int addr_size
,
125 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
127 if (buf_end
- loc_ptr
< 2 * addr_size
)
128 return DEBUG_LOC_BUFFER_OVERFLOW
;
131 *low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
133 *low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
134 loc_ptr
+= addr_size
;
137 *high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
139 *high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
140 loc_ptr
+= addr_size
;
144 /* A base-address-selection entry. */
145 if ((*low
& base_mask
) == base_mask
)
146 return DEBUG_LOC_BASE_ADDRESS
;
148 /* An end-of-list entry. */
149 if (*low
== 0 && *high
== 0)
150 return DEBUG_LOC_END_OF_LIST
;
152 return DEBUG_LOC_START_END
;
155 /* Decode the addresses in .debug_loclists entry.
156 A pointer to the next byte to examine is returned in *NEW_PTR.
157 The encoded low,high addresses are return in *LOW,*HIGH.
158 The result indicates the kind of entry found. */
160 static enum debug_loc_kind
161 decode_debug_loclists_addresses (struct dwarf2_per_cu_data
*per_cu
,
162 const gdb_byte
*loc_ptr
,
163 const gdb_byte
*buf_end
,
164 const gdb_byte
**new_ptr
,
165 CORE_ADDR
*low
, CORE_ADDR
*high
,
166 enum bfd_endian byte_order
,
167 unsigned int addr_size
,
172 if (loc_ptr
== buf_end
)
173 return DEBUG_LOC_BUFFER_OVERFLOW
;
177 case DW_LLE_base_addressx
:
179 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
181 return DEBUG_LOC_BUFFER_OVERFLOW
;
182 *high
= dwarf2_read_addr_index (per_cu
, u64
);
184 return DEBUG_LOC_BASE_ADDRESS
;
185 case DW_LLE_startx_length
:
186 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
188 return DEBUG_LOC_BUFFER_OVERFLOW
;
189 *low
= dwarf2_read_addr_index (per_cu
, u64
);
191 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
193 return DEBUG_LOC_BUFFER_OVERFLOW
;
196 return DEBUG_LOC_START_LENGTH
;
197 case DW_LLE_start_length
:
198 if (buf_end
- loc_ptr
< addr_size
)
199 return DEBUG_LOC_BUFFER_OVERFLOW
;
201 *low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
203 *low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
204 loc_ptr
+= addr_size
;
206 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
208 return DEBUG_LOC_BUFFER_OVERFLOW
;
211 return DEBUG_LOC_START_LENGTH
;
212 case DW_LLE_end_of_list
:
214 return DEBUG_LOC_END_OF_LIST
;
215 case DW_LLE_base_address
:
216 if (loc_ptr
+ addr_size
> buf_end
)
217 return DEBUG_LOC_BUFFER_OVERFLOW
;
219 *high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
221 *high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
222 loc_ptr
+= addr_size
;
224 return DEBUG_LOC_BASE_ADDRESS
;
225 case DW_LLE_offset_pair
:
226 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
228 return DEBUG_LOC_BUFFER_OVERFLOW
;
230 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &u64
);
232 return DEBUG_LOC_BUFFER_OVERFLOW
;
235 return DEBUG_LOC_START_END
;
236 /* Following cases are not supported yet. */
237 case DW_LLE_startx_endx
:
238 case DW_LLE_start_end
:
239 case DW_LLE_default_location
:
241 return DEBUG_LOC_INVALID_ENTRY
;
245 /* Decode the addresses in .debug_loc.dwo entry.
246 A pointer to the next byte to examine is returned in *NEW_PTR.
247 The encoded low,high addresses are return in *LOW,*HIGH.
248 The result indicates the kind of entry found. */
250 static enum debug_loc_kind
251 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data
*per_cu
,
252 const gdb_byte
*loc_ptr
,
253 const gdb_byte
*buf_end
,
254 const gdb_byte
**new_ptr
,
255 CORE_ADDR
*low
, CORE_ADDR
*high
,
256 enum bfd_endian byte_order
)
258 uint64_t low_index
, high_index
;
260 if (loc_ptr
== buf_end
)
261 return DEBUG_LOC_BUFFER_OVERFLOW
;
265 case DW_LLE_GNU_end_of_list_entry
:
267 return DEBUG_LOC_END_OF_LIST
;
268 case DW_LLE_GNU_base_address_selection_entry
:
270 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &high_index
);
272 return DEBUG_LOC_BUFFER_OVERFLOW
;
273 *high
= dwarf2_read_addr_index (per_cu
, high_index
);
275 return DEBUG_LOC_BASE_ADDRESS
;
276 case DW_LLE_GNU_start_end_entry
:
277 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &low_index
);
279 return DEBUG_LOC_BUFFER_OVERFLOW
;
280 *low
= dwarf2_read_addr_index (per_cu
, low_index
);
281 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &high_index
);
283 return DEBUG_LOC_BUFFER_OVERFLOW
;
284 *high
= dwarf2_read_addr_index (per_cu
, high_index
);
286 return DEBUG_LOC_START_END
;
287 case DW_LLE_GNU_start_length_entry
:
288 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &low_index
);
290 return DEBUG_LOC_BUFFER_OVERFLOW
;
291 *low
= dwarf2_read_addr_index (per_cu
, low_index
);
292 if (loc_ptr
+ 4 > buf_end
)
293 return DEBUG_LOC_BUFFER_OVERFLOW
;
295 *high
+= extract_unsigned_integer (loc_ptr
, 4, byte_order
);
296 *new_ptr
= loc_ptr
+ 4;
297 return DEBUG_LOC_START_LENGTH
;
299 return DEBUG_LOC_INVALID_ENTRY
;
303 /* A function for dealing with location lists. Given a
304 symbol baton (BATON) and a pc value (PC), find the appropriate
305 location expression, set *LOCEXPR_LENGTH, and return a pointer
306 to the beginning of the expression. Returns NULL on failure.
308 For now, only return the first matching location expression; there
309 can be more than one in the list. */
312 dwarf2_find_location_expression (struct dwarf2_loclist_baton
*baton
,
313 size_t *locexpr_length
, CORE_ADDR pc
)
315 struct objfile
*objfile
= baton
->per_cu
->objfile ();
316 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
317 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
318 unsigned int addr_size
= baton
->per_cu
->addr_size ();
319 int signed_addr_p
= bfd_get_sign_extend_vma (objfile
->obfd
);
320 /* Adjust base_address for relocatable objects. */
321 CORE_ADDR base_offset
= baton
->per_cu
->text_offset ();
322 CORE_ADDR base_address
= baton
->base_address
+ base_offset
;
323 const gdb_byte
*loc_ptr
, *buf_end
;
325 loc_ptr
= baton
->data
;
326 buf_end
= baton
->data
+ baton
->size
;
330 CORE_ADDR low
= 0, high
= 0; /* init for gcc -Wall */
332 enum debug_loc_kind kind
;
333 const gdb_byte
*new_ptr
= NULL
; /* init for gcc -Wall */
336 kind
= decode_debug_loc_dwo_addresses (baton
->per_cu
,
337 loc_ptr
, buf_end
, &new_ptr
,
338 &low
, &high
, byte_order
);
339 else if (baton
->per_cu
->version () < 5)
340 kind
= decode_debug_loc_addresses (loc_ptr
, buf_end
, &new_ptr
,
342 byte_order
, addr_size
,
345 kind
= decode_debug_loclists_addresses (baton
->per_cu
,
346 loc_ptr
, buf_end
, &new_ptr
,
347 &low
, &high
, byte_order
,
348 addr_size
, signed_addr_p
);
353 case DEBUG_LOC_END_OF_LIST
:
356 case DEBUG_LOC_BASE_ADDRESS
:
357 base_address
= high
+ base_offset
;
359 case DEBUG_LOC_START_END
:
360 case DEBUG_LOC_START_LENGTH
:
362 case DEBUG_LOC_BUFFER_OVERFLOW
:
363 case DEBUG_LOC_INVALID_ENTRY
:
364 error (_("dwarf2_find_location_expression: "
365 "Corrupted DWARF expression."));
367 gdb_assert_not_reached ("bad debug_loc_kind");
370 /* Otherwise, a location expression entry.
371 If the entry is from a DWO, don't add base address: the entry is from
372 .debug_addr which already has the DWARF "base address". We still add
373 base_offset in case we're debugging a PIE executable. */
382 high
+= base_address
;
385 if (baton
->per_cu
->version () < 5)
387 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
392 unsigned int bytes_read
;
394 length
= read_unsigned_leb128 (NULL
, loc_ptr
, &bytes_read
);
395 loc_ptr
+= bytes_read
;
398 if (low
== high
&& pc
== low
)
400 /* This is entry PC record present only at entry point
401 of a function. Verify it is really the function entry point. */
403 const struct block
*pc_block
= block_for_pc (pc
);
404 struct symbol
*pc_func
= NULL
;
407 pc_func
= block_linkage_function (pc_block
);
409 if (pc_func
&& pc
== BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (pc_func
)))
411 *locexpr_length
= length
;
416 if (pc
>= low
&& pc
< high
)
418 *locexpr_length
= length
;
426 /* Implement find_frame_base_location method for LOC_BLOCK functions using
427 DWARF expression for its DW_AT_frame_base. */
430 locexpr_find_frame_base_location (struct symbol
*framefunc
, CORE_ADDR pc
,
431 const gdb_byte
**start
, size_t *length
)
433 struct dwarf2_locexpr_baton
*symbaton
434 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
436 *length
= symbaton
->size
;
437 *start
= symbaton
->data
;
440 /* Implement the struct symbol_block_ops::get_frame_base method for
441 LOC_BLOCK functions using a DWARF expression as its DW_AT_frame_base. */
444 locexpr_get_frame_base (struct symbol
*framefunc
, struct frame_info
*frame
)
446 struct gdbarch
*gdbarch
;
448 struct dwarf2_locexpr_baton
*dlbaton
;
449 const gdb_byte
*start
;
451 struct value
*result
;
453 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
454 Thus, it's supposed to provide the find_frame_base_location method as
456 gdb_assert (SYMBOL_BLOCK_OPS (framefunc
)->find_frame_base_location
!= NULL
);
458 gdbarch
= get_frame_arch (frame
);
459 type
= builtin_type (gdbarch
)->builtin_data_ptr
;
460 dlbaton
= (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
462 SYMBOL_BLOCK_OPS (framefunc
)->find_frame_base_location
463 (framefunc
, get_frame_pc (frame
), &start
, &length
);
464 result
= dwarf2_evaluate_loc_desc (type
, frame
, start
, length
,
467 /* The DW_AT_frame_base attribute contains a location description which
468 computes the base address itself. However, the call to
469 dwarf2_evaluate_loc_desc returns a value representing a variable at
470 that address. The frame base address is thus this variable's
472 return value_address (result
);
475 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
476 function uses DWARF expression for its DW_AT_frame_base. */
478 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs
=
480 locexpr_find_frame_base_location
,
481 locexpr_get_frame_base
484 /* Implement find_frame_base_location method for LOC_BLOCK functions using
485 DWARF location list for its DW_AT_frame_base. */
488 loclist_find_frame_base_location (struct symbol
*framefunc
, CORE_ADDR pc
,
489 const gdb_byte
**start
, size_t *length
)
491 struct dwarf2_loclist_baton
*symbaton
492 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
494 *start
= dwarf2_find_location_expression (symbaton
, length
, pc
);
497 /* Implement the struct symbol_block_ops::get_frame_base method for
498 LOC_BLOCK functions using a DWARF location list as its DW_AT_frame_base. */
501 loclist_get_frame_base (struct symbol
*framefunc
, struct frame_info
*frame
)
503 struct gdbarch
*gdbarch
;
505 struct dwarf2_loclist_baton
*dlbaton
;
506 const gdb_byte
*start
;
508 struct value
*result
;
510 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
511 Thus, it's supposed to provide the find_frame_base_location method as
513 gdb_assert (SYMBOL_BLOCK_OPS (framefunc
)->find_frame_base_location
!= NULL
);
515 gdbarch
= get_frame_arch (frame
);
516 type
= builtin_type (gdbarch
)->builtin_data_ptr
;
517 dlbaton
= (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
519 SYMBOL_BLOCK_OPS (framefunc
)->find_frame_base_location
520 (framefunc
, get_frame_pc (frame
), &start
, &length
);
521 result
= dwarf2_evaluate_loc_desc (type
, frame
, start
, length
,
524 /* The DW_AT_frame_base attribute contains a location description which
525 computes the base address itself. However, the call to
526 dwarf2_evaluate_loc_desc returns a value representing a variable at
527 that address. The frame base address is thus this variable's
529 return value_address (result
);
532 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
533 function uses DWARF location list for its DW_AT_frame_base. */
535 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs
=
537 loclist_find_frame_base_location
,
538 loclist_get_frame_base
541 /* See dwarf2loc.h. */
544 func_get_frame_base_dwarf_block (struct symbol
*framefunc
, CORE_ADDR pc
,
545 const gdb_byte
**start
, size_t *length
)
547 if (SYMBOL_BLOCK_OPS (framefunc
) != NULL
)
549 const struct symbol_block_ops
*ops_block
= SYMBOL_BLOCK_OPS (framefunc
);
551 ops_block
->find_frame_base_location (framefunc
, pc
, start
, length
);
557 error (_("Could not find the frame base for \"%s\"."),
558 framefunc
->natural_name ());
562 get_frame_pc_for_per_cu_dwarf_call (void *baton
)
564 dwarf_expr_context
*ctx
= (dwarf_expr_context
*) baton
;
566 return ctx
->get_frame_pc ();
570 per_cu_dwarf_call (struct dwarf_expr_context
*ctx
, cu_offset die_offset
,
571 struct dwarf2_per_cu_data
*per_cu
)
573 struct dwarf2_locexpr_baton block
;
575 block
= dwarf2_fetch_die_loc_cu_off (die_offset
, per_cu
,
576 get_frame_pc_for_per_cu_dwarf_call
,
579 /* DW_OP_call_ref is currently not supported. */
580 gdb_assert (block
.per_cu
== per_cu
);
582 ctx
->eval (block
.data
, block
.size
);
585 /* Given context CTX, section offset SECT_OFF, and compilation unit
586 data PER_CU, execute the "variable value" operation on the DIE
587 found at SECT_OFF. */
589 static struct value
*
590 sect_variable_value (struct dwarf_expr_context
*ctx
, sect_offset sect_off
,
591 struct dwarf2_per_cu_data
*per_cu
)
593 struct type
*die_type
= dwarf2_fetch_die_type_sect_off (sect_off
, per_cu
);
595 if (die_type
== NULL
)
596 error (_("Bad DW_OP_GNU_variable_value DIE."));
598 /* Note: Things still work when the following test is removed. This
599 test and error is here to conform to the proposed specification. */
600 if (TYPE_CODE (die_type
) != TYPE_CODE_INT
601 && TYPE_CODE (die_type
) != TYPE_CODE_PTR
)
602 error (_("Type of DW_OP_GNU_variable_value DIE must be an integer or pointer."));
604 struct type
*type
= lookup_pointer_type (die_type
);
605 struct frame_info
*frame
= get_selected_frame (_("No frame selected."));
606 return indirect_synthetic_pointer (sect_off
, 0, per_cu
, frame
, type
, true);
609 class dwarf_evaluate_loc_desc
: public dwarf_expr_context
613 struct frame_info
*frame
;
614 struct dwarf2_per_cu_data
*per_cu
;
615 CORE_ADDR obj_address
;
617 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
618 the frame in BATON. */
620 CORE_ADDR
get_frame_cfa () override
622 return dwarf2_frame_cfa (frame
);
625 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
626 the frame in BATON. */
628 CORE_ADDR
get_frame_pc () override
630 return get_frame_address_in_block (frame
);
633 /* Using the objfile specified in BATON, find the address for the
634 current thread's thread-local storage with offset OFFSET. */
635 CORE_ADDR
get_tls_address (CORE_ADDR offset
) override
637 struct objfile
*objfile
= per_cu
->objfile ();
639 return target_translate_tls_address (objfile
, offset
);
642 /* Helper interface of per_cu_dwarf_call for
643 dwarf2_evaluate_loc_desc. */
645 void dwarf_call (cu_offset die_offset
) override
647 per_cu_dwarf_call (this, die_offset
, per_cu
);
650 /* Helper interface of sect_variable_value for
651 dwarf2_evaluate_loc_desc. */
653 struct value
*dwarf_variable_value (sect_offset sect_off
) override
655 return sect_variable_value (this, sect_off
, per_cu
);
658 struct type
*get_base_type (cu_offset die_offset
, int size
) override
660 struct type
*result
= dwarf2_get_die_type (die_offset
, per_cu
);
662 error (_("Could not find type for DW_OP_const_type"));
663 if (size
!= 0 && TYPE_LENGTH (result
) != size
)
664 error (_("DW_OP_const_type has different sizes for type and data"));
668 /* Callback function for dwarf2_evaluate_loc_desc.
669 Fetch the address indexed by DW_OP_addrx or DW_OP_GNU_addr_index. */
671 CORE_ADDR
get_addr_index (unsigned int index
) override
673 return dwarf2_read_addr_index (per_cu
, index
);
676 /* Callback function for get_object_address. Return the address of the VLA
679 CORE_ADDR
get_object_address () override
681 if (obj_address
== 0)
682 error (_("Location address is not set."));
686 /* Execute DWARF block of call_site_parameter which matches KIND and
687 KIND_U. Choose DEREF_SIZE value of that parameter. Search
688 caller of this objects's frame.
690 The caller can be from a different CU - per_cu_dwarf_call
691 implementation can be more simple as it does not support cross-CU
694 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind
,
695 union call_site_parameter_u kind_u
,
696 int deref_size
) override
698 struct frame_info
*caller_frame
;
699 struct dwarf2_per_cu_data
*caller_per_cu
;
700 struct call_site_parameter
*parameter
;
701 const gdb_byte
*data_src
;
704 caller_frame
= get_prev_frame (frame
);
706 parameter
= dwarf_expr_reg_to_entry_parameter (frame
, kind
, kind_u
,
708 data_src
= deref_size
== -1 ? parameter
->value
: parameter
->data_value
;
709 size
= deref_size
== -1 ? parameter
->value_size
: parameter
->data_value_size
;
711 /* DEREF_SIZE size is not verified here. */
712 if (data_src
== NULL
)
713 throw_error (NO_ENTRY_VALUE_ERROR
,
714 _("Cannot resolve DW_AT_call_data_value"));
716 scoped_restore save_frame
= make_scoped_restore (&this->frame
,
718 scoped_restore save_per_cu
= make_scoped_restore (&this->per_cu
,
720 scoped_restore save_obj_addr
= make_scoped_restore (&this->obj_address
,
723 scoped_restore save_arch
= make_scoped_restore (&this->gdbarch
);
724 this->gdbarch
= get_objfile_arch (per_cu
->objfile ());
725 scoped_restore save_addr_size
= make_scoped_restore (&this->addr_size
);
726 this->addr_size
= per_cu
->addr_size ();
727 scoped_restore save_offset
= make_scoped_restore (&this->offset
);
728 this->offset
= per_cu
->text_offset ();
730 this->eval (data_src
, size
);
733 /* Using the frame specified in BATON, find the location expression
734 describing the frame base. Return a pointer to it in START and
735 its length in LENGTH. */
736 void get_frame_base (const gdb_byte
**start
, size_t * length
) override
738 /* FIXME: cagney/2003-03-26: This code should be using
739 get_frame_base_address(), and then implement a dwarf2 specific
741 struct symbol
*framefunc
;
742 const struct block
*bl
= get_frame_block (frame
, NULL
);
745 error (_("frame address is not available."));
747 /* Use block_linkage_function, which returns a real (not inlined)
748 function, instead of get_frame_function, which may return an
750 framefunc
= block_linkage_function (bl
);
752 /* If we found a frame-relative symbol then it was certainly within
753 some function associated with a frame. If we can't find the frame,
754 something has gone wrong. */
755 gdb_assert (framefunc
!= NULL
);
757 func_get_frame_base_dwarf_block (framefunc
,
758 get_frame_address_in_block (frame
),
762 /* Read memory at ADDR (length LEN) into BUF. */
764 void read_mem (gdb_byte
*buf
, CORE_ADDR addr
, size_t len
) override
766 read_memory (addr
, buf
, len
);
769 /* Using the frame specified in BATON, return the value of register
770 REGNUM, treated as a pointer. */
771 CORE_ADDR
read_addr_from_reg (int dwarf_regnum
) override
773 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
774 int regnum
= dwarf_reg_to_regnum_or_error (gdbarch
, dwarf_regnum
);
776 return address_from_register (regnum
, frame
);
779 /* Implement "get_reg_value" callback. */
781 struct value
*get_reg_value (struct type
*type
, int dwarf_regnum
) override
783 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
784 int regnum
= dwarf_reg_to_regnum_or_error (gdbarch
, dwarf_regnum
);
786 return value_from_register (type
, regnum
, frame
);
790 /* See dwarf2loc.h. */
792 unsigned int entry_values_debug
= 0;
794 /* Helper to set entry_values_debug. */
797 show_entry_values_debug (struct ui_file
*file
, int from_tty
,
798 struct cmd_list_element
*c
, const char *value
)
800 fprintf_filtered (file
,
801 _("Entry values and tail call frames debugging is %s.\n"),
805 /* Find DW_TAG_call_site's DW_AT_call_target address.
806 CALLER_FRAME (for registers) can be NULL if it is not known. This function
807 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
810 call_site_to_target_addr (struct gdbarch
*call_site_gdbarch
,
811 struct call_site
*call_site
,
812 struct frame_info
*caller_frame
)
814 switch (FIELD_LOC_KIND (call_site
->target
))
816 case FIELD_LOC_KIND_DWARF_BLOCK
:
818 struct dwarf2_locexpr_baton
*dwarf_block
;
820 struct type
*caller_core_addr_type
;
821 struct gdbarch
*caller_arch
;
823 dwarf_block
= FIELD_DWARF_BLOCK (call_site
->target
);
824 if (dwarf_block
== NULL
)
826 struct bound_minimal_symbol msym
;
828 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
829 throw_error (NO_ENTRY_VALUE_ERROR
,
830 _("DW_AT_call_target is not specified at %s in %s"),
831 paddress (call_site_gdbarch
, call_site
->pc
),
832 (msym
.minsym
== NULL
? "???"
833 : msym
.minsym
->print_name ()));
836 if (caller_frame
== NULL
)
838 struct bound_minimal_symbol msym
;
840 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
841 throw_error (NO_ENTRY_VALUE_ERROR
,
842 _("DW_AT_call_target DWARF block resolving "
843 "requires known frame which is currently not "
844 "available at %s in %s"),
845 paddress (call_site_gdbarch
, call_site
->pc
),
846 (msym
.minsym
== NULL
? "???"
847 : msym
.minsym
->print_name ()));
850 caller_arch
= get_frame_arch (caller_frame
);
851 caller_core_addr_type
= builtin_type (caller_arch
)->builtin_func_ptr
;
852 val
= dwarf2_evaluate_loc_desc (caller_core_addr_type
, caller_frame
,
853 dwarf_block
->data
, dwarf_block
->size
,
854 dwarf_block
->per_cu
);
855 /* DW_AT_call_target is a DWARF expression, not a DWARF location. */
856 if (VALUE_LVAL (val
) == lval_memory
)
857 return value_address (val
);
859 return value_as_address (val
);
862 case FIELD_LOC_KIND_PHYSNAME
:
864 const char *physname
;
865 struct bound_minimal_symbol msym
;
867 physname
= FIELD_STATIC_PHYSNAME (call_site
->target
);
869 /* Handle both the mangled and demangled PHYSNAME. */
870 msym
= lookup_minimal_symbol (physname
, NULL
, NULL
);
871 if (msym
.minsym
== NULL
)
873 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
874 throw_error (NO_ENTRY_VALUE_ERROR
,
875 _("Cannot find function \"%s\" for a call site target "
877 physname
, paddress (call_site_gdbarch
, call_site
->pc
),
878 (msym
.minsym
== NULL
? "???"
879 : msym
.minsym
->print_name ()));
882 return BMSYMBOL_VALUE_ADDRESS (msym
);
885 case FIELD_LOC_KIND_PHYSADDR
:
886 return FIELD_STATIC_PHYSADDR (call_site
->target
);
889 internal_error (__FILE__
, __LINE__
, _("invalid call site target kind"));
893 /* Convert function entry point exact address ADDR to the function which is
894 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
895 NO_ENTRY_VALUE_ERROR otherwise. */
897 static struct symbol
*
898 func_addr_to_tail_call_list (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
900 struct symbol
*sym
= find_pc_function (addr
);
903 if (sym
== NULL
|| BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
)) != addr
)
904 throw_error (NO_ENTRY_VALUE_ERROR
,
905 _("DW_TAG_call_site resolving failed to find function "
906 "name for address %s"),
907 paddress (gdbarch
, addr
));
909 type
= SYMBOL_TYPE (sym
);
910 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FUNC
);
911 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FUNC
);
916 /* Verify function with entry point exact address ADDR can never call itself
917 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
918 can call itself via tail calls.
920 If a funtion can tail call itself its entry value based parameters are
921 unreliable. There is no verification whether the value of some/all
922 parameters is unchanged through the self tail call, we expect if there is
923 a self tail call all the parameters can be modified. */
926 func_verify_no_selftailcall (struct gdbarch
*gdbarch
, CORE_ADDR verify_addr
)
930 /* The verification is completely unordered. Track here function addresses
931 which still need to be iterated. */
932 std::vector
<CORE_ADDR
> todo
;
934 /* Track here CORE_ADDRs which were already visited. */
935 std::unordered_set
<CORE_ADDR
> addr_hash
;
937 todo
.push_back (verify_addr
);
938 while (!todo
.empty ())
940 struct symbol
*func_sym
;
941 struct call_site
*call_site
;
946 func_sym
= func_addr_to_tail_call_list (gdbarch
, addr
);
948 for (call_site
= TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym
));
949 call_site
; call_site
= call_site
->tail_call_next
)
951 CORE_ADDR target_addr
;
953 /* CALLER_FRAME with registers is not available for tail-call jumped
955 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, NULL
);
957 if (target_addr
== verify_addr
)
959 struct bound_minimal_symbol msym
;
961 msym
= lookup_minimal_symbol_by_pc (verify_addr
);
962 throw_error (NO_ENTRY_VALUE_ERROR
,
963 _("DW_OP_entry_value resolving has found "
964 "function \"%s\" at %s can call itself via tail "
966 (msym
.minsym
== NULL
? "???"
967 : msym
.minsym
->print_name ()),
968 paddress (gdbarch
, verify_addr
));
971 if (addr_hash
.insert (target_addr
).second
)
972 todo
.push_back (target_addr
);
977 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
978 ENTRY_VALUES_DEBUG. */
981 tailcall_dump (struct gdbarch
*gdbarch
, const struct call_site
*call_site
)
983 CORE_ADDR addr
= call_site
->pc
;
984 struct bound_minimal_symbol msym
= lookup_minimal_symbol_by_pc (addr
- 1);
986 fprintf_unfiltered (gdb_stdlog
, " %s(%s)", paddress (gdbarch
, addr
),
987 (msym
.minsym
== NULL
? "???"
988 : msym
.minsym
->print_name ()));
992 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
993 only top callers and bottom callees which are present in both. GDBARCH is
994 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
995 no remaining possibilities to provide unambiguous non-trivial result.
996 RESULTP should point to NULL on the first (initialization) call. Caller is
997 responsible for xfree of any RESULTP data. */
1000 chain_candidate (struct gdbarch
*gdbarch
,
1001 gdb::unique_xmalloc_ptr
<struct call_site_chain
> *resultp
,
1002 std::vector
<struct call_site
*> *chain
)
1004 long length
= chain
->size ();
1005 int callers
, callees
, idx
;
1007 if (*resultp
== NULL
)
1009 /* Create the initial chain containing all the passed PCs. */
1011 struct call_site_chain
*result
1012 = ((struct call_site_chain
*)
1013 xmalloc (sizeof (*result
)
1014 + sizeof (*result
->call_site
) * (length
- 1)));
1015 result
->length
= length
;
1016 result
->callers
= result
->callees
= length
;
1017 if (!chain
->empty ())
1018 memcpy (result
->call_site
, chain
->data (),
1019 sizeof (*result
->call_site
) * length
);
1020 resultp
->reset (result
);
1022 if (entry_values_debug
)
1024 fprintf_unfiltered (gdb_stdlog
, "tailcall: initial:");
1025 for (idx
= 0; idx
< length
; idx
++)
1026 tailcall_dump (gdbarch
, result
->call_site
[idx
]);
1027 fputc_unfiltered ('\n', gdb_stdlog
);
1033 if (entry_values_debug
)
1035 fprintf_unfiltered (gdb_stdlog
, "tailcall: compare:");
1036 for (idx
= 0; idx
< length
; idx
++)
1037 tailcall_dump (gdbarch
, chain
->at (idx
));
1038 fputc_unfiltered ('\n', gdb_stdlog
);
1041 /* Intersect callers. */
1043 callers
= std::min ((long) (*resultp
)->callers
, length
);
1044 for (idx
= 0; idx
< callers
; idx
++)
1045 if ((*resultp
)->call_site
[idx
] != chain
->at (idx
))
1047 (*resultp
)->callers
= idx
;
1051 /* Intersect callees. */
1053 callees
= std::min ((long) (*resultp
)->callees
, length
);
1054 for (idx
= 0; idx
< callees
; idx
++)
1055 if ((*resultp
)->call_site
[(*resultp
)->length
- 1 - idx
]
1056 != chain
->at (length
- 1 - idx
))
1058 (*resultp
)->callees
= idx
;
1062 if (entry_values_debug
)
1064 fprintf_unfiltered (gdb_stdlog
, "tailcall: reduced:");
1065 for (idx
= 0; idx
< (*resultp
)->callers
; idx
++)
1066 tailcall_dump (gdbarch
, (*resultp
)->call_site
[idx
]);
1067 fputs_unfiltered (" |", gdb_stdlog
);
1068 for (idx
= 0; idx
< (*resultp
)->callees
; idx
++)
1069 tailcall_dump (gdbarch
,
1070 (*resultp
)->call_site
[(*resultp
)->length
1071 - (*resultp
)->callees
+ idx
]);
1072 fputc_unfiltered ('\n', gdb_stdlog
);
1075 if ((*resultp
)->callers
== 0 && (*resultp
)->callees
== 0)
1077 /* There are no common callers or callees. It could be also a direct
1078 call (which has length 0) with ambiguous possibility of an indirect
1079 call - CALLERS == CALLEES == 0 is valid during the first allocation
1080 but any subsequence processing of such entry means ambiguity. */
1081 resultp
->reset (NULL
);
1085 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
1086 PC again. In such case there must be two different code paths to reach
1087 it. CALLERS + CALLEES equal to LENGTH in the case of self tail-call. */
1088 gdb_assert ((*resultp
)->callers
+ (*resultp
)->callees
<= (*resultp
)->length
);
1091 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
1092 assumed frames between them use GDBARCH. Use depth first search so we can
1093 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
1094 would have needless GDB stack overhead. Any unreliability results
1095 in thrown NO_ENTRY_VALUE_ERROR. */
1097 static gdb::unique_xmalloc_ptr
<call_site_chain
>
1098 call_site_find_chain_1 (struct gdbarch
*gdbarch
, CORE_ADDR caller_pc
,
1099 CORE_ADDR callee_pc
)
1101 CORE_ADDR save_callee_pc
= callee_pc
;
1102 gdb::unique_xmalloc_ptr
<struct call_site_chain
> retval
;
1103 struct call_site
*call_site
;
1105 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
1106 call_site nor any possible call_site at CALLEE_PC's function is there.
1107 Any CALL_SITE in CHAIN will be iterated to its siblings - via
1108 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
1109 std::vector
<struct call_site
*> chain
;
1111 /* We are not interested in the specific PC inside the callee function. */
1112 callee_pc
= get_pc_function_start (callee_pc
);
1114 throw_error (NO_ENTRY_VALUE_ERROR
, _("Unable to find function for PC %s"),
1115 paddress (gdbarch
, save_callee_pc
));
1117 /* Mark CALL_SITEs so we do not visit the same ones twice. */
1118 std::unordered_set
<CORE_ADDR
> addr_hash
;
1120 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
1121 at the target's function. All the possible tail call sites in the
1122 target's function will get iterated as already pushed into CHAIN via their
1124 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
1128 CORE_ADDR target_func_addr
;
1129 struct call_site
*target_call_site
;
1131 /* CALLER_FRAME with registers is not available for tail-call jumped
1133 target_func_addr
= call_site_to_target_addr (gdbarch
, call_site
, NULL
);
1135 if (target_func_addr
== callee_pc
)
1137 chain_candidate (gdbarch
, &retval
, &chain
);
1141 /* There is no way to reach CALLEE_PC again as we would prevent
1142 entering it twice as being already marked in ADDR_HASH. */
1143 target_call_site
= NULL
;
1147 struct symbol
*target_func
;
1149 target_func
= func_addr_to_tail_call_list (gdbarch
, target_func_addr
);
1150 target_call_site
= TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func
));
1155 /* Attempt to visit TARGET_CALL_SITE. */
1157 if (target_call_site
)
1159 if (addr_hash
.insert (target_call_site
->pc
).second
)
1161 /* Successfully entered TARGET_CALL_SITE. */
1163 chain
.push_back (target_call_site
);
1168 /* Backtrack (without revisiting the originating call_site). Try the
1169 callers's sibling; if there isn't any try the callers's callers's
1172 target_call_site
= NULL
;
1173 while (!chain
.empty ())
1175 call_site
= chain
.back ();
1178 size_t removed
= addr_hash
.erase (call_site
->pc
);
1179 gdb_assert (removed
== 1);
1181 target_call_site
= call_site
->tail_call_next
;
1182 if (target_call_site
)
1186 while (target_call_site
);
1191 call_site
= chain
.back ();
1196 struct bound_minimal_symbol msym_caller
, msym_callee
;
1198 msym_caller
= lookup_minimal_symbol_by_pc (caller_pc
);
1199 msym_callee
= lookup_minimal_symbol_by_pc (callee_pc
);
1200 throw_error (NO_ENTRY_VALUE_ERROR
,
1201 _("There are no unambiguously determinable intermediate "
1202 "callers or callees between caller function \"%s\" at %s "
1203 "and callee function \"%s\" at %s"),
1204 (msym_caller
.minsym
== NULL
1205 ? "???" : msym_caller
.minsym
->print_name ()),
1206 paddress (gdbarch
, caller_pc
),
1207 (msym_callee
.minsym
== NULL
1208 ? "???" : msym_callee
.minsym
->print_name ()),
1209 paddress (gdbarch
, callee_pc
));
1215 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
1216 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
1217 constructed return NULL. */
1219 gdb::unique_xmalloc_ptr
<call_site_chain
>
1220 call_site_find_chain (struct gdbarch
*gdbarch
, CORE_ADDR caller_pc
,
1221 CORE_ADDR callee_pc
)
1223 gdb::unique_xmalloc_ptr
<call_site_chain
> retval
;
1227 retval
= call_site_find_chain_1 (gdbarch
, caller_pc
, callee_pc
);
1229 catch (const gdb_exception_error
&e
)
1231 if (e
.error
== NO_ENTRY_VALUE_ERROR
)
1233 if (entry_values_debug
)
1234 exception_print (gdb_stdout
, e
);
1245 /* Return 1 if KIND and KIND_U match PARAMETER. Return 0 otherwise. */
1248 call_site_parameter_matches (struct call_site_parameter
*parameter
,
1249 enum call_site_parameter_kind kind
,
1250 union call_site_parameter_u kind_u
)
1252 if (kind
== parameter
->kind
)
1255 case CALL_SITE_PARAMETER_DWARF_REG
:
1256 return kind_u
.dwarf_reg
== parameter
->u
.dwarf_reg
;
1257 case CALL_SITE_PARAMETER_FB_OFFSET
:
1258 return kind_u
.fb_offset
== parameter
->u
.fb_offset
;
1259 case CALL_SITE_PARAMETER_PARAM_OFFSET
:
1260 return kind_u
.param_cu_off
== parameter
->u
.param_cu_off
;
1265 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
1266 FRAME is for callee.
1268 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
1271 static struct call_site_parameter
*
1272 dwarf_expr_reg_to_entry_parameter (struct frame_info
*frame
,
1273 enum call_site_parameter_kind kind
,
1274 union call_site_parameter_u kind_u
,
1275 struct dwarf2_per_cu_data
**per_cu_return
)
1277 CORE_ADDR func_addr
, caller_pc
;
1278 struct gdbarch
*gdbarch
;
1279 struct frame_info
*caller_frame
;
1280 struct call_site
*call_site
;
1282 /* Initialize it just to avoid a GCC false warning. */
1283 struct call_site_parameter
*parameter
= NULL
;
1284 CORE_ADDR target_addr
;
1286 while (get_frame_type (frame
) == INLINE_FRAME
)
1288 frame
= get_prev_frame (frame
);
1289 gdb_assert (frame
!= NULL
);
1292 func_addr
= get_frame_func (frame
);
1293 gdbarch
= get_frame_arch (frame
);
1294 caller_frame
= get_prev_frame (frame
);
1295 if (gdbarch
!= frame_unwind_arch (frame
))
1297 struct bound_minimal_symbol msym
1298 = lookup_minimal_symbol_by_pc (func_addr
);
1299 struct gdbarch
*caller_gdbarch
= frame_unwind_arch (frame
);
1301 throw_error (NO_ENTRY_VALUE_ERROR
,
1302 _("DW_OP_entry_value resolving callee gdbarch %s "
1303 "(of %s (%s)) does not match caller gdbarch %s"),
1304 gdbarch_bfd_arch_info (gdbarch
)->printable_name
,
1305 paddress (gdbarch
, func_addr
),
1306 (msym
.minsym
== NULL
? "???"
1307 : msym
.minsym
->print_name ()),
1308 gdbarch_bfd_arch_info (caller_gdbarch
)->printable_name
);
1311 if (caller_frame
== NULL
)
1313 struct bound_minimal_symbol msym
1314 = lookup_minimal_symbol_by_pc (func_addr
);
1316 throw_error (NO_ENTRY_VALUE_ERROR
, _("DW_OP_entry_value resolving "
1317 "requires caller of %s (%s)"),
1318 paddress (gdbarch
, func_addr
),
1319 (msym
.minsym
== NULL
? "???"
1320 : msym
.minsym
->print_name ()));
1322 caller_pc
= get_frame_pc (caller_frame
);
1323 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
1325 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, caller_frame
);
1326 if (target_addr
!= func_addr
)
1328 struct minimal_symbol
*target_msym
, *func_msym
;
1330 target_msym
= lookup_minimal_symbol_by_pc (target_addr
).minsym
;
1331 func_msym
= lookup_minimal_symbol_by_pc (func_addr
).minsym
;
1332 throw_error (NO_ENTRY_VALUE_ERROR
,
1333 _("DW_OP_entry_value resolving expects callee %s at %s "
1334 "but the called frame is for %s at %s"),
1335 (target_msym
== NULL
? "???"
1336 : target_msym
->print_name ()),
1337 paddress (gdbarch
, target_addr
),
1338 func_msym
== NULL
? "???" : func_msym
->print_name (),
1339 paddress (gdbarch
, func_addr
));
1342 /* No entry value based parameters would be reliable if this function can
1343 call itself via tail calls. */
1344 func_verify_no_selftailcall (gdbarch
, func_addr
);
1346 for (iparams
= 0; iparams
< call_site
->parameter_count
; iparams
++)
1348 parameter
= &call_site
->parameter
[iparams
];
1349 if (call_site_parameter_matches (parameter
, kind
, kind_u
))
1352 if (iparams
== call_site
->parameter_count
)
1354 struct minimal_symbol
*msym
1355 = lookup_minimal_symbol_by_pc (caller_pc
).minsym
;
1357 /* DW_TAG_call_site_parameter will be missing just if GCC could not
1358 determine its value. */
1359 throw_error (NO_ENTRY_VALUE_ERROR
, _("Cannot find matching parameter "
1360 "at DW_TAG_call_site %s at %s"),
1361 paddress (gdbarch
, caller_pc
),
1362 msym
== NULL
? "???" : msym
->print_name ());
1365 *per_cu_return
= call_site
->per_cu
;
1369 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
1370 the normal DW_AT_call_value block. Otherwise return the
1371 DW_AT_call_data_value (dereferenced) block.
1373 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1376 Function always returns non-NULL, non-optimized out value. It throws
1377 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
1379 static struct value
*
1380 dwarf_entry_parameter_to_value (struct call_site_parameter
*parameter
,
1381 CORE_ADDR deref_size
, struct type
*type
,
1382 struct frame_info
*caller_frame
,
1383 struct dwarf2_per_cu_data
*per_cu
)
1385 const gdb_byte
*data_src
;
1389 data_src
= deref_size
== -1 ? parameter
->value
: parameter
->data_value
;
1390 size
= deref_size
== -1 ? parameter
->value_size
: parameter
->data_value_size
;
1392 /* DEREF_SIZE size is not verified here. */
1393 if (data_src
== NULL
)
1394 throw_error (NO_ENTRY_VALUE_ERROR
,
1395 _("Cannot resolve DW_AT_call_data_value"));
1397 /* DW_AT_call_value is a DWARF expression, not a DWARF
1398 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1400 data
= (gdb_byte
*) alloca (size
+ 1);
1401 memcpy (data
, data_src
, size
);
1402 data
[size
] = DW_OP_stack_value
;
1404 return dwarf2_evaluate_loc_desc (type
, caller_frame
, data
, size
+ 1, per_cu
);
1407 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1408 the indirect method on it, that is use its stored target value, the sole
1409 purpose of entry_data_value_funcs.. */
1411 static struct value
*
1412 entry_data_value_coerce_ref (const struct value
*value
)
1414 struct type
*checked_type
= check_typedef (value_type (value
));
1415 struct value
*target_val
;
1417 if (!TYPE_IS_REFERENCE (checked_type
))
1420 target_val
= (struct value
*) value_computed_closure (value
);
1421 value_incref (target_val
);
1425 /* Implement copy_closure. */
1428 entry_data_value_copy_closure (const struct value
*v
)
1430 struct value
*target_val
= (struct value
*) value_computed_closure (v
);
1432 value_incref (target_val
);
1436 /* Implement free_closure. */
1439 entry_data_value_free_closure (struct value
*v
)
1441 struct value
*target_val
= (struct value
*) value_computed_closure (v
);
1443 value_decref (target_val
);
1446 /* Vector for methods for an entry value reference where the referenced value
1447 is stored in the caller. On the first dereference use
1448 DW_AT_call_data_value in the caller. */
1450 static const struct lval_funcs entry_data_value_funcs
=
1454 NULL
, /* indirect */
1455 entry_data_value_coerce_ref
,
1456 NULL
, /* check_synthetic_pointer */
1457 entry_data_value_copy_closure
,
1458 entry_data_value_free_closure
1461 /* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KIND_U
1462 are used to match DW_AT_location at the caller's
1463 DW_TAG_call_site_parameter.
1465 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1466 cannot resolve the parameter for any reason. */
1468 static struct value
*
1469 value_of_dwarf_reg_entry (struct type
*type
, struct frame_info
*frame
,
1470 enum call_site_parameter_kind kind
,
1471 union call_site_parameter_u kind_u
)
1473 struct type
*checked_type
= check_typedef (type
);
1474 struct type
*target_type
= TYPE_TARGET_TYPE (checked_type
);
1475 struct frame_info
*caller_frame
= get_prev_frame (frame
);
1476 struct value
*outer_val
, *target_val
, *val
;
1477 struct call_site_parameter
*parameter
;
1478 struct dwarf2_per_cu_data
*caller_per_cu
;
1480 parameter
= dwarf_expr_reg_to_entry_parameter (frame
, kind
, kind_u
,
1483 outer_val
= dwarf_entry_parameter_to_value (parameter
, -1 /* deref_size */,
1487 /* Check if DW_AT_call_data_value cannot be used. If it should be
1488 used and it is not available do not fall back to OUTER_VAL - dereferencing
1489 TYPE_CODE_REF with non-entry data value would give current value - not the
1492 if (!TYPE_IS_REFERENCE (checked_type
)
1493 || TYPE_TARGET_TYPE (checked_type
) == NULL
)
1496 target_val
= dwarf_entry_parameter_to_value (parameter
,
1497 TYPE_LENGTH (target_type
),
1498 target_type
, caller_frame
,
1501 val
= allocate_computed_value (type
, &entry_data_value_funcs
,
1502 release_value (target_val
).release ());
1504 /* Copy the referencing pointer to the new computed value. */
1505 memcpy (value_contents_raw (val
), value_contents_raw (outer_val
),
1506 TYPE_LENGTH (checked_type
));
1507 set_value_lazy (val
, 0);
1512 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1513 SIZE are DWARF block used to match DW_AT_location at the caller's
1514 DW_TAG_call_site_parameter.
1516 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1517 cannot resolve the parameter for any reason. */
1519 static struct value
*
1520 value_of_dwarf_block_entry (struct type
*type
, struct frame_info
*frame
,
1521 const gdb_byte
*block
, size_t block_len
)
1523 union call_site_parameter_u kind_u
;
1525 kind_u
.dwarf_reg
= dwarf_block_to_dwarf_reg (block
, block
+ block_len
);
1526 if (kind_u
.dwarf_reg
!= -1)
1527 return value_of_dwarf_reg_entry (type
, frame
, CALL_SITE_PARAMETER_DWARF_REG
,
1530 if (dwarf_block_to_fb_offset (block
, block
+ block_len
, &kind_u
.fb_offset
))
1531 return value_of_dwarf_reg_entry (type
, frame
, CALL_SITE_PARAMETER_FB_OFFSET
,
1534 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1535 suppressed during normal operation. The expression can be arbitrary if
1536 there is no caller-callee entry value binding expected. */
1537 throw_error (NO_ENTRY_VALUE_ERROR
,
1538 _("DWARF-2 expression error: DW_OP_entry_value is supported "
1539 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1542 struct piece_closure
1544 /* Reference count. */
1547 /* The CU from which this closure's expression came. */
1548 struct dwarf2_per_cu_data
*per_cu
= NULL
;
1550 /* The pieces describing this variable. */
1551 std::vector
<dwarf_expr_piece
> pieces
;
1553 /* Frame ID of frame to which a register value is relative, used
1554 only by DWARF_VALUE_REGISTER. */
1555 struct frame_id frame_id
;
1558 /* Allocate a closure for a value formed from separately-described
1561 static struct piece_closure
*
1562 allocate_piece_closure (struct dwarf2_per_cu_data
*per_cu
,
1563 std::vector
<dwarf_expr_piece
> &&pieces
,
1564 struct frame_info
*frame
)
1566 struct piece_closure
*c
= new piece_closure
;
1570 c
->pieces
= std::move (pieces
);
1572 c
->frame_id
= null_frame_id
;
1574 c
->frame_id
= get_frame_id (frame
);
1576 for (dwarf_expr_piece
&piece
: c
->pieces
)
1577 if (piece
.location
== DWARF_VALUE_STACK
)
1578 value_incref (piece
.v
.value
);
1583 /* Return the number of bytes overlapping a contiguous chunk of N_BITS
1584 bits whose first bit is located at bit offset START. */
1587 bits_to_bytes (ULONGEST start
, ULONGEST n_bits
)
1589 return (start
% 8 + n_bits
+ 7) / 8;
1592 /* Read or write a pieced value V. If FROM != NULL, operate in "write
1593 mode": copy FROM into the pieces comprising V. If FROM == NULL,
1594 operate in "read mode": fetch the contents of the (lazy) value V by
1595 composing it from its pieces. */
1598 rw_pieced_value (struct value
*v
, struct value
*from
)
1601 LONGEST offset
= 0, max_offset
;
1602 ULONGEST bits_to_skip
;
1603 gdb_byte
*v_contents
;
1604 const gdb_byte
*from_contents
;
1605 struct piece_closure
*c
1606 = (struct piece_closure
*) value_computed_closure (v
);
1607 gdb::byte_vector buffer
;
1608 bool bits_big_endian
= type_byte_order (value_type (v
)) == BFD_ENDIAN_BIG
;
1612 from_contents
= value_contents (from
);
1617 if (value_type (v
) != value_enclosing_type (v
))
1618 internal_error (__FILE__
, __LINE__
,
1619 _("Should not be able to create a lazy value with "
1620 "an enclosing type"));
1621 v_contents
= value_contents_raw (v
);
1622 from_contents
= NULL
;
1625 bits_to_skip
= 8 * value_offset (v
);
1626 if (value_bitsize (v
))
1628 bits_to_skip
+= (8 * value_offset (value_parent (v
))
1629 + value_bitpos (v
));
1631 && (type_byte_order (value_type (from
))
1634 /* Use the least significant bits of FROM. */
1635 max_offset
= 8 * TYPE_LENGTH (value_type (from
));
1636 offset
= max_offset
- value_bitsize (v
);
1639 max_offset
= value_bitsize (v
);
1642 max_offset
= 8 * TYPE_LENGTH (value_type (v
));
1644 /* Advance to the first non-skipped piece. */
1645 for (i
= 0; i
< c
->pieces
.size () && bits_to_skip
>= c
->pieces
[i
].size
; i
++)
1646 bits_to_skip
-= c
->pieces
[i
].size
;
1648 for (; i
< c
->pieces
.size () && offset
< max_offset
; i
++)
1650 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1651 size_t this_size_bits
, this_size
;
1653 this_size_bits
= p
->size
- bits_to_skip
;
1654 if (this_size_bits
> max_offset
- offset
)
1655 this_size_bits
= max_offset
- offset
;
1657 switch (p
->location
)
1659 case DWARF_VALUE_REGISTER
:
1661 struct frame_info
*frame
= frame_find_by_id (c
->frame_id
);
1662 struct gdbarch
*arch
= get_frame_arch (frame
);
1663 int gdb_regnum
= dwarf_reg_to_regnum_or_error (arch
, p
->v
.regno
);
1664 ULONGEST reg_bits
= 8 * register_size (arch
, gdb_regnum
);
1667 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
1668 && p
->offset
+ p
->size
< reg_bits
)
1670 /* Big-endian, and we want less than full size. */
1671 bits_to_skip
+= reg_bits
- (p
->offset
+ p
->size
);
1674 bits_to_skip
+= p
->offset
;
1676 this_size
= bits_to_bytes (bits_to_skip
, this_size_bits
);
1677 buffer
.resize (this_size
);
1682 if (!get_frame_register_bytes (frame
, gdb_regnum
,
1684 this_size
, buffer
.data (),
1688 mark_value_bits_optimized_out (v
, offset
,
1691 mark_value_bits_unavailable (v
, offset
,
1696 copy_bitwise (v_contents
, offset
,
1697 buffer
.data (), bits_to_skip
% 8,
1698 this_size_bits
, bits_big_endian
);
1703 if (bits_to_skip
% 8 != 0 || this_size_bits
% 8 != 0)
1705 /* Data is copied non-byte-aligned into the register.
1706 Need some bits from original register value. */
1707 get_frame_register_bytes (frame
, gdb_regnum
,
1709 this_size
, buffer
.data (),
1712 throw_error (OPTIMIZED_OUT_ERROR
,
1713 _("Can't do read-modify-write to "
1714 "update bitfield; containing word "
1715 "has been optimized out"));
1717 throw_error (NOT_AVAILABLE_ERROR
,
1718 _("Can't do read-modify-write to "
1719 "update bitfield; containing word "
1723 copy_bitwise (buffer
.data (), bits_to_skip
% 8,
1724 from_contents
, offset
,
1725 this_size_bits
, bits_big_endian
);
1726 put_frame_register_bytes (frame
, gdb_regnum
,
1728 this_size
, buffer
.data ());
1733 case DWARF_VALUE_MEMORY
:
1735 bits_to_skip
+= p
->offset
;
1737 CORE_ADDR start_addr
= p
->v
.mem
.addr
+ bits_to_skip
/ 8;
1739 if (bits_to_skip
% 8 == 0 && this_size_bits
% 8 == 0
1742 /* Everything is byte-aligned; no buffer needed. */
1744 write_memory_with_notification (start_addr
,
1747 this_size_bits
/ 8);
1749 read_value_memory (v
, offset
,
1750 p
->v
.mem
.in_stack_memory
,
1751 p
->v
.mem
.addr
+ bits_to_skip
/ 8,
1752 v_contents
+ offset
/ 8,
1753 this_size_bits
/ 8);
1757 this_size
= bits_to_bytes (bits_to_skip
, this_size_bits
);
1758 buffer
.resize (this_size
);
1763 read_value_memory (v
, offset
,
1764 p
->v
.mem
.in_stack_memory
,
1765 p
->v
.mem
.addr
+ bits_to_skip
/ 8,
1766 buffer
.data (), this_size
);
1767 copy_bitwise (v_contents
, offset
,
1768 buffer
.data (), bits_to_skip
% 8,
1769 this_size_bits
, bits_big_endian
);
1774 if (bits_to_skip
% 8 != 0 || this_size_bits
% 8 != 0)
1778 /* Perform a single read for small sizes. */
1779 read_memory (start_addr
, buffer
.data (),
1784 /* Only the first and last bytes can possibly have
1786 read_memory (start_addr
, buffer
.data (), 1);
1787 read_memory (start_addr
+ this_size
- 1,
1788 &buffer
[this_size
- 1], 1);
1792 copy_bitwise (buffer
.data (), bits_to_skip
% 8,
1793 from_contents
, offset
,
1794 this_size_bits
, bits_big_endian
);
1795 write_memory_with_notification (start_addr
,
1802 case DWARF_VALUE_STACK
:
1806 mark_value_bits_optimized_out (v
, offset
, this_size_bits
);
1810 struct objfile
*objfile
= c
->per_cu
->objfile ();
1811 struct gdbarch
*objfile_gdbarch
= get_objfile_arch (objfile
);
1812 ULONGEST stack_value_size_bits
1813 = 8 * TYPE_LENGTH (value_type (p
->v
.value
));
1815 /* Use zeroes if piece reaches beyond stack value. */
1816 if (p
->offset
+ p
->size
> stack_value_size_bits
)
1819 /* Piece is anchored at least significant bit end. */
1820 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
1821 bits_to_skip
+= stack_value_size_bits
- p
->offset
- p
->size
;
1823 bits_to_skip
+= p
->offset
;
1825 copy_bitwise (v_contents
, offset
,
1826 value_contents_all (p
->v
.value
),
1828 this_size_bits
, bits_big_endian
);
1832 case DWARF_VALUE_LITERAL
:
1836 mark_value_bits_optimized_out (v
, offset
, this_size_bits
);
1840 ULONGEST literal_size_bits
= 8 * p
->v
.literal
.length
;
1841 size_t n
= this_size_bits
;
1843 /* Cut off at the end of the implicit value. */
1844 bits_to_skip
+= p
->offset
;
1845 if (bits_to_skip
>= literal_size_bits
)
1847 if (n
> literal_size_bits
- bits_to_skip
)
1848 n
= literal_size_bits
- bits_to_skip
;
1850 copy_bitwise (v_contents
, offset
,
1851 p
->v
.literal
.data
, bits_to_skip
,
1852 n
, bits_big_endian
);
1856 case DWARF_VALUE_IMPLICIT_POINTER
:
1859 mark_value_bits_optimized_out (v
, offset
, this_size_bits
);
1863 /* These bits show up as zeros -- but do not cause the value to
1864 be considered optimized-out. */
1867 case DWARF_VALUE_OPTIMIZED_OUT
:
1868 mark_value_bits_optimized_out (v
, offset
, this_size_bits
);
1872 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
1875 offset
+= this_size_bits
;
1882 read_pieced_value (struct value
*v
)
1884 rw_pieced_value (v
, NULL
);
1888 write_pieced_value (struct value
*to
, struct value
*from
)
1890 rw_pieced_value (to
, from
);
1893 /* An implementation of an lval_funcs method to see whether a value is
1894 a synthetic pointer. */
1897 check_pieced_synthetic_pointer (const struct value
*value
, LONGEST bit_offset
,
1900 struct piece_closure
*c
1901 = (struct piece_closure
*) value_computed_closure (value
);
1904 bit_offset
+= 8 * value_offset (value
);
1905 if (value_bitsize (value
))
1906 bit_offset
+= value_bitpos (value
);
1908 for (i
= 0; i
< c
->pieces
.size () && bit_length
> 0; i
++)
1910 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1911 size_t this_size_bits
= p
->size
;
1915 if (bit_offset
>= this_size_bits
)
1917 bit_offset
-= this_size_bits
;
1921 bit_length
-= this_size_bits
- bit_offset
;
1925 bit_length
-= this_size_bits
;
1927 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
1934 /* A wrapper function for get_frame_address_in_block. */
1937 get_frame_address_in_block_wrapper (void *baton
)
1939 return get_frame_address_in_block ((struct frame_info
*) baton
);
1942 /* Fetch a DW_AT_const_value through a synthetic pointer. */
1944 static struct value
*
1945 fetch_const_value_from_synthetic_pointer (sect_offset die
, LONGEST byte_offset
,
1946 struct dwarf2_per_cu_data
*per_cu
,
1949 struct value
*result
= NULL
;
1950 const gdb_byte
*bytes
;
1953 auto_obstack temp_obstack
;
1954 bytes
= dwarf2_fetch_constant_bytes (die
, per_cu
, &temp_obstack
, &len
);
1958 if (byte_offset
>= 0
1959 && byte_offset
+ TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) <= len
)
1961 bytes
+= byte_offset
;
1962 result
= value_from_contents (TYPE_TARGET_TYPE (type
), bytes
);
1965 invalid_synthetic_pointer ();
1968 result
= allocate_optimized_out_value (TYPE_TARGET_TYPE (type
));
1973 /* Fetch the value pointed to by a synthetic pointer. */
1975 static struct value
*
1976 indirect_synthetic_pointer (sect_offset die
, LONGEST byte_offset
,
1977 struct dwarf2_per_cu_data
*per_cu
,
1978 struct frame_info
*frame
, struct type
*type
,
1979 bool resolve_abstract_p
)
1981 /* Fetch the location expression of the DIE we're pointing to. */
1982 struct dwarf2_locexpr_baton baton
1983 = dwarf2_fetch_die_loc_sect_off (die
, per_cu
,
1984 get_frame_address_in_block_wrapper
, frame
,
1985 resolve_abstract_p
);
1987 /* Get type of pointed-to DIE. */
1988 struct type
*orig_type
= dwarf2_fetch_die_type_sect_off (die
, per_cu
);
1989 if (orig_type
== NULL
)
1990 invalid_synthetic_pointer ();
1992 /* If pointed-to DIE has a DW_AT_location, evaluate it and return the
1993 resulting value. Otherwise, it may have a DW_AT_const_value instead,
1994 or it may've been optimized out. */
1995 if (baton
.data
!= NULL
)
1996 return dwarf2_evaluate_loc_desc_full (orig_type
, frame
, baton
.data
,
1997 baton
.size
, baton
.per_cu
,
1998 TYPE_TARGET_TYPE (type
),
2001 return fetch_const_value_from_synthetic_pointer (die
, byte_offset
, per_cu
,
2005 /* An implementation of an lval_funcs method to indirect through a
2006 pointer. This handles the synthetic pointer case when needed. */
2008 static struct value
*
2009 indirect_pieced_value (struct value
*value
)
2011 struct piece_closure
*c
2012 = (struct piece_closure
*) value_computed_closure (value
);
2014 struct frame_info
*frame
;
2017 struct dwarf_expr_piece
*piece
= NULL
;
2018 LONGEST byte_offset
;
2019 enum bfd_endian byte_order
;
2021 type
= check_typedef (value_type (value
));
2022 if (TYPE_CODE (type
) != TYPE_CODE_PTR
)
2025 bit_length
= 8 * TYPE_LENGTH (type
);
2026 bit_offset
= 8 * value_offset (value
);
2027 if (value_bitsize (value
))
2028 bit_offset
+= value_bitpos (value
);
2030 for (i
= 0; i
< c
->pieces
.size () && bit_length
> 0; i
++)
2032 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
2033 size_t this_size_bits
= p
->size
;
2037 if (bit_offset
>= this_size_bits
)
2039 bit_offset
-= this_size_bits
;
2043 bit_length
-= this_size_bits
- bit_offset
;
2047 bit_length
-= this_size_bits
;
2049 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
2052 if (bit_length
!= 0)
2053 error (_("Invalid use of DW_OP_implicit_pointer"));
2059 gdb_assert (piece
!= NULL
);
2060 frame
= get_selected_frame (_("No frame selected."));
2062 /* This is an offset requested by GDB, such as value subscripts.
2063 However, due to how synthetic pointers are implemented, this is
2064 always presented to us as a pointer type. This means we have to
2065 sign-extend it manually as appropriate. Use raw
2066 extract_signed_integer directly rather than value_as_address and
2067 sign extend afterwards on architectures that would need it
2068 (mostly everywhere except MIPS, which has signed addresses) as
2069 the later would go through gdbarch_pointer_to_address and thus
2070 return a CORE_ADDR with high bits set on architectures that
2071 encode address spaces and other things in CORE_ADDR. */
2072 byte_order
= gdbarch_byte_order (get_frame_arch (frame
));
2073 byte_offset
= extract_signed_integer (value_contents (value
),
2074 TYPE_LENGTH (type
), byte_order
);
2075 byte_offset
+= piece
->v
.ptr
.offset
;
2077 return indirect_synthetic_pointer (piece
->v
.ptr
.die_sect_off
,
2078 byte_offset
, c
->per_cu
,
2082 /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
2085 static struct value
*
2086 coerce_pieced_ref (const struct value
*value
)
2088 struct type
*type
= check_typedef (value_type (value
));
2090 if (value_bits_synthetic_pointer (value
, value_embedded_offset (value
),
2091 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
2093 const struct piece_closure
*closure
2094 = (struct piece_closure
*) value_computed_closure (value
);
2095 struct frame_info
*frame
2096 = get_selected_frame (_("No frame selected."));
2098 /* gdb represents synthetic pointers as pieced values with a single
2100 gdb_assert (closure
!= NULL
);
2101 gdb_assert (closure
->pieces
.size () == 1);
2103 return indirect_synthetic_pointer
2104 (closure
->pieces
[0].v
.ptr
.die_sect_off
,
2105 closure
->pieces
[0].v
.ptr
.offset
,
2106 closure
->per_cu
, frame
, type
);
2110 /* Else: not a synthetic reference; do nothing. */
2116 copy_pieced_value_closure (const struct value
*v
)
2118 struct piece_closure
*c
2119 = (struct piece_closure
*) value_computed_closure (v
);
2126 free_pieced_value_closure (struct value
*v
)
2128 struct piece_closure
*c
2129 = (struct piece_closure
*) value_computed_closure (v
);
2134 for (dwarf_expr_piece
&p
: c
->pieces
)
2135 if (p
.location
== DWARF_VALUE_STACK
)
2136 value_decref (p
.v
.value
);
2142 /* Functions for accessing a variable described by DW_OP_piece. */
2143 static const struct lval_funcs pieced_value_funcs
= {
2146 indirect_pieced_value
,
2148 check_pieced_synthetic_pointer
,
2149 copy_pieced_value_closure
,
2150 free_pieced_value_closure
2153 /* Evaluate a location description, starting at DATA and with length
2154 SIZE, to find the current location of variable of TYPE in the
2155 context of FRAME. If SUBOBJ_TYPE is non-NULL, return instead the
2156 location of the subobject of type SUBOBJ_TYPE at byte offset
2157 SUBOBJ_BYTE_OFFSET within the variable of type TYPE. */
2159 static struct value
*
2160 dwarf2_evaluate_loc_desc_full (struct type
*type
, struct frame_info
*frame
,
2161 const gdb_byte
*data
, size_t size
,
2162 struct dwarf2_per_cu_data
*per_cu
,
2163 struct type
*subobj_type
,
2164 LONGEST subobj_byte_offset
)
2166 struct value
*retval
;
2167 struct objfile
*objfile
= per_cu
->objfile ();
2169 if (subobj_type
== NULL
)
2172 subobj_byte_offset
= 0;
2174 else if (subobj_byte_offset
< 0)
2175 invalid_synthetic_pointer ();
2178 return allocate_optimized_out_value (subobj_type
);
2180 dwarf_evaluate_loc_desc ctx
;
2182 ctx
.per_cu
= per_cu
;
2183 ctx
.obj_address
= 0;
2185 scoped_value_mark free_values
;
2187 ctx
.gdbarch
= get_objfile_arch (objfile
);
2188 ctx
.addr_size
= per_cu
->addr_size ();
2189 ctx
.ref_addr_size
= per_cu
->ref_addr_size ();
2190 ctx
.offset
= per_cu
->text_offset ();
2194 ctx
.eval (data
, size
);
2196 catch (const gdb_exception_error
&ex
)
2198 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2200 free_values
.free_to_mark ();
2201 retval
= allocate_value (subobj_type
);
2202 mark_value_bytes_unavailable (retval
, 0,
2203 TYPE_LENGTH (subobj_type
));
2206 else if (ex
.error
== NO_ENTRY_VALUE_ERROR
)
2208 if (entry_values_debug
)
2209 exception_print (gdb_stdout
, ex
);
2210 free_values
.free_to_mark ();
2211 return allocate_optimized_out_value (subobj_type
);
2217 if (ctx
.pieces
.size () > 0)
2219 struct piece_closure
*c
;
2220 ULONGEST bit_size
= 0;
2222 for (dwarf_expr_piece
&piece
: ctx
.pieces
)
2223 bit_size
+= piece
.size
;
2224 /* Complain if the expression is larger than the size of the
2226 if (bit_size
> 8 * TYPE_LENGTH (type
))
2227 invalid_synthetic_pointer ();
2229 c
= allocate_piece_closure (per_cu
, std::move (ctx
.pieces
), frame
);
2230 /* We must clean up the value chain after creating the piece
2231 closure but before allocating the result. */
2232 free_values
.free_to_mark ();
2233 retval
= allocate_computed_value (subobj_type
,
2234 &pieced_value_funcs
, c
);
2235 set_value_offset (retval
, subobj_byte_offset
);
2239 switch (ctx
.location
)
2241 case DWARF_VALUE_REGISTER
:
2243 struct gdbarch
*arch
= get_frame_arch (frame
);
2245 = longest_to_int (value_as_long (ctx
.fetch (0)));
2246 int gdb_regnum
= dwarf_reg_to_regnum_or_error (arch
, dwarf_regnum
);
2248 if (subobj_byte_offset
!= 0)
2249 error (_("cannot use offset on synthetic pointer to register"));
2250 free_values
.free_to_mark ();
2251 retval
= value_from_register (subobj_type
, gdb_regnum
, frame
);
2252 if (value_optimized_out (retval
))
2256 /* This means the register has undefined value / was
2257 not saved. As we're computing the location of some
2258 variable etc. in the program, not a value for
2259 inspecting a register ($pc, $sp, etc.), return a
2260 generic optimized out value instead, so that we show
2261 <optimized out> instead of <not saved>. */
2262 tmp
= allocate_value (subobj_type
);
2263 value_contents_copy (tmp
, 0, retval
, 0,
2264 TYPE_LENGTH (subobj_type
));
2270 case DWARF_VALUE_MEMORY
:
2272 struct type
*ptr_type
;
2273 CORE_ADDR address
= ctx
.fetch_address (0);
2274 bool in_stack_memory
= ctx
.fetch_in_stack_memory (0);
2276 /* DW_OP_deref_size (and possibly other operations too) may
2277 create a pointer instead of an address. Ideally, the
2278 pointer to address conversion would be performed as part
2279 of those operations, but the type of the object to
2280 which the address refers is not known at the time of
2281 the operation. Therefore, we do the conversion here
2282 since the type is readily available. */
2284 switch (TYPE_CODE (subobj_type
))
2286 case TYPE_CODE_FUNC
:
2287 case TYPE_CODE_METHOD
:
2288 ptr_type
= builtin_type (ctx
.gdbarch
)->builtin_func_ptr
;
2291 ptr_type
= builtin_type (ctx
.gdbarch
)->builtin_data_ptr
;
2294 address
= value_as_address (value_from_pointer (ptr_type
, address
));
2296 free_values
.free_to_mark ();
2297 retval
= value_at_lazy (subobj_type
,
2298 address
+ subobj_byte_offset
);
2299 if (in_stack_memory
)
2300 set_value_stack (retval
, 1);
2304 case DWARF_VALUE_STACK
:
2306 struct value
*value
= ctx
.fetch (0);
2307 size_t n
= TYPE_LENGTH (value_type (value
));
2308 size_t len
= TYPE_LENGTH (subobj_type
);
2309 size_t max
= TYPE_LENGTH (type
);
2310 struct gdbarch
*objfile_gdbarch
= get_objfile_arch (objfile
);
2312 if (subobj_byte_offset
+ len
> max
)
2313 invalid_synthetic_pointer ();
2315 /* Preserve VALUE because we are going to free values back
2316 to the mark, but we still need the value contents
2318 value_ref_ptr value_holder
= value_ref_ptr::new_reference (value
);
2319 free_values
.free_to_mark ();
2321 retval
= allocate_value (subobj_type
);
2323 /* The given offset is relative to the actual object. */
2324 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
2325 subobj_byte_offset
+= n
- max
;
2327 memcpy (value_contents_raw (retval
),
2328 value_contents_all (value
) + subobj_byte_offset
, len
);
2332 case DWARF_VALUE_LITERAL
:
2335 size_t n
= TYPE_LENGTH (subobj_type
);
2337 if (subobj_byte_offset
+ n
> ctx
.len
)
2338 invalid_synthetic_pointer ();
2340 free_values
.free_to_mark ();
2341 retval
= allocate_value (subobj_type
);
2342 contents
= value_contents_raw (retval
);
2343 memcpy (contents
, ctx
.data
+ subobj_byte_offset
, n
);
2347 case DWARF_VALUE_OPTIMIZED_OUT
:
2348 free_values
.free_to_mark ();
2349 retval
= allocate_optimized_out_value (subobj_type
);
2352 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2353 operation by execute_stack_op. */
2354 case DWARF_VALUE_IMPLICIT_POINTER
:
2355 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2356 it can only be encountered when making a piece. */
2358 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
2362 set_value_initialized (retval
, ctx
.initialized
);
2367 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2368 passes 0 as the byte_offset. */
2371 dwarf2_evaluate_loc_desc (struct type
*type
, struct frame_info
*frame
,
2372 const gdb_byte
*data
, size_t size
,
2373 struct dwarf2_per_cu_data
*per_cu
)
2375 return dwarf2_evaluate_loc_desc_full (type
, frame
, data
, size
, per_cu
,
2379 /* Evaluates a dwarf expression and stores the result in VAL, expecting
2380 that the dwarf expression only produces a single CORE_ADDR. FRAME is the
2381 frame in which the expression is evaluated. ADDR is a context (location of
2382 a variable) and might be needed to evaluate the location expression.
2383 Returns 1 on success, 0 otherwise. */
2386 dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton
*dlbaton
,
2387 struct frame_info
*frame
,
2391 struct objfile
*objfile
;
2393 if (dlbaton
== NULL
|| dlbaton
->size
== 0)
2396 dwarf_evaluate_loc_desc ctx
;
2399 ctx
.per_cu
= dlbaton
->per_cu
;
2400 ctx
.obj_address
= addr
;
2402 objfile
= dlbaton
->per_cu
->objfile ();
2404 ctx
.gdbarch
= get_objfile_arch (objfile
);
2405 ctx
.addr_size
= dlbaton
->per_cu
->addr_size ();
2406 ctx
.ref_addr_size
= dlbaton
->per_cu
->ref_addr_size ();
2407 ctx
.offset
= dlbaton
->per_cu
->text_offset ();
2411 ctx
.eval (dlbaton
->data
, dlbaton
->size
);
2413 catch (const gdb_exception_error
&ex
)
2415 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2419 else if (ex
.error
== NO_ENTRY_VALUE_ERROR
)
2421 if (entry_values_debug
)
2422 exception_print (gdb_stdout
, ex
);
2429 switch (ctx
.location
)
2431 case DWARF_VALUE_REGISTER
:
2432 case DWARF_VALUE_MEMORY
:
2433 case DWARF_VALUE_STACK
:
2434 *valp
= ctx
.fetch_address (0);
2435 if (ctx
.location
== DWARF_VALUE_REGISTER
)
2436 *valp
= ctx
.read_addr_from_reg (*valp
);
2438 case DWARF_VALUE_LITERAL
:
2439 *valp
= extract_signed_integer (ctx
.data
, ctx
.len
,
2440 gdbarch_byte_order (ctx
.gdbarch
));
2442 /* Unsupported dwarf values. */
2443 case DWARF_VALUE_OPTIMIZED_OUT
:
2444 case DWARF_VALUE_IMPLICIT_POINTER
:
2451 /* See dwarf2loc.h. */
2454 dwarf2_evaluate_property (const struct dynamic_prop
*prop
,
2455 struct frame_info
*frame
,
2456 struct property_addr_info
*addr_stack
,
2462 if (frame
== NULL
&& has_stack_frames ())
2463 frame
= get_selected_frame (NULL
);
2469 const struct dwarf2_property_baton
*baton
2470 = (const struct dwarf2_property_baton
*) prop
->data
.baton
;
2471 gdb_assert (baton
->property_type
!= NULL
);
2473 if (dwarf2_locexpr_baton_eval (&baton
->locexpr
, frame
,
2474 addr_stack
? addr_stack
->addr
: 0,
2477 if (baton
->locexpr
.is_reference
)
2479 struct value
*val
= value_at (baton
->property_type
, *value
);
2480 *value
= value_as_address (val
);
2484 gdb_assert (baton
->property_type
!= NULL
);
2486 struct type
*type
= check_typedef (baton
->property_type
);
2487 if (TYPE_LENGTH (type
) < sizeof (CORE_ADDR
)
2488 && !TYPE_UNSIGNED (type
))
2490 /* If we have a valid return candidate and it's value
2491 is signed, we have to sign-extend the value because
2492 CORE_ADDR on 64bit machine has 8 bytes but address
2493 size of an 32bit application is bytes. */
2495 = (baton
->locexpr
.per_cu
->addr_size ()
2497 const CORE_ADDR neg_mask
2498 = (~((CORE_ADDR
) 0) << (addr_size
- 1));
2500 /* Check if signed bit is set and sign-extend values. */
2501 if (*value
& neg_mask
)
2512 struct dwarf2_property_baton
*baton
2513 = (struct dwarf2_property_baton
*) prop
->data
.baton
;
2514 CORE_ADDR pc
= get_frame_address_in_block (frame
);
2515 const gdb_byte
*data
;
2519 data
= dwarf2_find_location_expression (&baton
->loclist
, &size
, pc
);
2522 val
= dwarf2_evaluate_loc_desc (baton
->property_type
, frame
, data
,
2523 size
, baton
->loclist
.per_cu
);
2524 if (!value_optimized_out (val
))
2526 *value
= value_as_address (val
);
2534 *value
= prop
->data
.const_val
;
2537 case PROP_ADDR_OFFSET
:
2539 struct dwarf2_property_baton
*baton
2540 = (struct dwarf2_property_baton
*) prop
->data
.baton
;
2541 struct property_addr_info
*pinfo
;
2544 for (pinfo
= addr_stack
; pinfo
!= NULL
; pinfo
= pinfo
->next
)
2546 /* This approach lets us avoid checking the qualifiers. */
2547 if (TYPE_MAIN_TYPE (pinfo
->type
)
2548 == TYPE_MAIN_TYPE (baton
->property_type
))
2552 error (_("cannot find reference address for offset property"));
2553 if (pinfo
->valaddr
!= NULL
)
2554 val
= value_from_contents
2555 (baton
->offset_info
.type
,
2556 pinfo
->valaddr
+ baton
->offset_info
.offset
);
2558 val
= value_at (baton
->offset_info
.type
,
2559 pinfo
->addr
+ baton
->offset_info
.offset
);
2560 *value
= value_as_address (val
);
2568 /* See dwarf2loc.h. */
2571 dwarf2_compile_property_to_c (string_file
*stream
,
2572 const char *result_name
,
2573 struct gdbarch
*gdbarch
,
2574 unsigned char *registers_used
,
2575 const struct dynamic_prop
*prop
,
2579 struct dwarf2_property_baton
*baton
2580 = (struct dwarf2_property_baton
*) prop
->data
.baton
;
2581 const gdb_byte
*data
;
2583 struct dwarf2_per_cu_data
*per_cu
;
2585 if (prop
->kind
== PROP_LOCEXPR
)
2587 data
= baton
->locexpr
.data
;
2588 size
= baton
->locexpr
.size
;
2589 per_cu
= baton
->locexpr
.per_cu
;
2593 gdb_assert (prop
->kind
== PROP_LOCLIST
);
2595 data
= dwarf2_find_location_expression (&baton
->loclist
, &size
, pc
);
2596 per_cu
= baton
->loclist
.per_cu
;
2599 compile_dwarf_bounds_to_c (stream
, result_name
, prop
, sym
, pc
,
2600 gdbarch
, registers_used
,
2601 per_cu
->addr_size (),
2602 data
, data
+ size
, per_cu
);
2606 /* Helper functions and baton for dwarf2_loc_desc_get_symbol_read_needs. */
2608 class symbol_needs_eval_context
: public dwarf_expr_context
2612 enum symbol_needs_kind needs
;
2613 struct dwarf2_per_cu_data
*per_cu
;
2615 /* Reads from registers do require a frame. */
2616 CORE_ADDR
read_addr_from_reg (int regnum
) override
2618 needs
= SYMBOL_NEEDS_FRAME
;
2622 /* "get_reg_value" callback: Reads from registers do require a
2625 struct value
*get_reg_value (struct type
*type
, int regnum
) override
2627 needs
= SYMBOL_NEEDS_FRAME
;
2628 return value_zero (type
, not_lval
);
2631 /* Reads from memory do not require a frame. */
2632 void read_mem (gdb_byte
*buf
, CORE_ADDR addr
, size_t len
) override
2634 memset (buf
, 0, len
);
2637 /* Frame-relative accesses do require a frame. */
2638 void get_frame_base (const gdb_byte
**start
, size_t *length
) override
2640 static gdb_byte lit0
= DW_OP_lit0
;
2645 needs
= SYMBOL_NEEDS_FRAME
;
2648 /* CFA accesses require a frame. */
2649 CORE_ADDR
get_frame_cfa () override
2651 needs
= SYMBOL_NEEDS_FRAME
;
2655 CORE_ADDR
get_frame_pc () override
2657 needs
= SYMBOL_NEEDS_FRAME
;
2661 /* Thread-local accesses require registers, but not a frame. */
2662 CORE_ADDR
get_tls_address (CORE_ADDR offset
) override
2664 if (needs
<= SYMBOL_NEEDS_REGISTERS
)
2665 needs
= SYMBOL_NEEDS_REGISTERS
;
2669 /* Helper interface of per_cu_dwarf_call for
2670 dwarf2_loc_desc_get_symbol_read_needs. */
2672 void dwarf_call (cu_offset die_offset
) override
2674 per_cu_dwarf_call (this, die_offset
, per_cu
);
2677 /* Helper interface of sect_variable_value for
2678 dwarf2_loc_desc_get_symbol_read_needs. */
2680 struct value
*dwarf_variable_value (sect_offset sect_off
) override
2682 return sect_variable_value (this, sect_off
, per_cu
);
2685 /* DW_OP_entry_value accesses require a caller, therefore a
2688 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind
,
2689 union call_site_parameter_u kind_u
,
2690 int deref_size
) override
2692 needs
= SYMBOL_NEEDS_FRAME
;
2694 /* The expression may require some stub values on DWARF stack. */
2695 push_address (0, 0);
2698 /* DW_OP_addrx and DW_OP_GNU_addr_index doesn't require a frame. */
2700 CORE_ADDR
get_addr_index (unsigned int index
) override
2702 /* Nothing to do. */
2706 /* DW_OP_push_object_address has a frame already passed through. */
2708 CORE_ADDR
get_object_address () override
2710 /* Nothing to do. */
2715 /* Compute the correct symbol_needs_kind value for the location
2716 expression at DATA (length SIZE). */
2718 static enum symbol_needs_kind
2719 dwarf2_loc_desc_get_symbol_read_needs (const gdb_byte
*data
, size_t size
,
2720 struct dwarf2_per_cu_data
*per_cu
)
2723 struct objfile
*objfile
= per_cu
->objfile ();
2725 scoped_value_mark free_values
;
2727 symbol_needs_eval_context ctx
;
2729 ctx
.needs
= SYMBOL_NEEDS_NONE
;
2730 ctx
.per_cu
= per_cu
;
2731 ctx
.gdbarch
= get_objfile_arch (objfile
);
2732 ctx
.addr_size
= per_cu
->addr_size ();
2733 ctx
.ref_addr_size
= per_cu
->ref_addr_size ();
2734 ctx
.offset
= per_cu
->text_offset ();
2736 ctx
.eval (data
, size
);
2738 in_reg
= ctx
.location
== DWARF_VALUE_REGISTER
;
2740 /* If the location has several pieces, and any of them are in
2741 registers, then we will need a frame to fetch them from. */
2742 for (dwarf_expr_piece
&p
: ctx
.pieces
)
2743 if (p
.location
== DWARF_VALUE_REGISTER
)
2747 ctx
.needs
= SYMBOL_NEEDS_FRAME
;
2751 /* A helper function that throws an unimplemented error mentioning a
2752 given DWARF operator. */
2754 static void ATTRIBUTE_NORETURN
2755 unimplemented (unsigned int op
)
2757 const char *name
= get_DW_OP_name (op
);
2760 error (_("DWARF operator %s cannot be translated to an agent expression"),
2763 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2764 "to an agent expression"),
2770 This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2771 can issue a complaint, which is better than having every target's
2772 implementation of dwarf2_reg_to_regnum do it. */
2775 dwarf_reg_to_regnum (struct gdbarch
*arch
, int dwarf_reg
)
2777 int reg
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_reg
);
2781 complaint (_("bad DWARF register number %d"), dwarf_reg
);
2786 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2787 Throw an error because DWARF_REG is bad. */
2790 throw_bad_regnum_error (ULONGEST dwarf_reg
)
2792 /* Still want to print -1 as "-1".
2793 We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2794 but that's overkill for now. */
2795 if ((int) dwarf_reg
== dwarf_reg
)
2796 error (_("Unable to access DWARF register number %d"), (int) dwarf_reg
);
2797 error (_("Unable to access DWARF register number %s"),
2798 pulongest (dwarf_reg
));
2801 /* See dwarf2loc.h. */
2804 dwarf_reg_to_regnum_or_error (struct gdbarch
*arch
, ULONGEST dwarf_reg
)
2808 if (dwarf_reg
> INT_MAX
)
2809 throw_bad_regnum_error (dwarf_reg
);
2810 /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2811 bad, but that's ok. */
2812 reg
= dwarf_reg_to_regnum (arch
, (int) dwarf_reg
);
2814 throw_bad_regnum_error (dwarf_reg
);
2818 /* A helper function that emits an access to memory. ARCH is the
2819 target architecture. EXPR is the expression which we are building.
2820 NBITS is the number of bits we want to read. This emits the
2821 opcodes needed to read the memory and then extract the desired
2825 access_memory (struct gdbarch
*arch
, struct agent_expr
*expr
, ULONGEST nbits
)
2827 ULONGEST nbytes
= (nbits
+ 7) / 8;
2829 gdb_assert (nbytes
> 0 && nbytes
<= sizeof (LONGEST
));
2832 ax_trace_quick (expr
, nbytes
);
2835 ax_simple (expr
, aop_ref8
);
2836 else if (nbits
<= 16)
2837 ax_simple (expr
, aop_ref16
);
2838 else if (nbits
<= 32)
2839 ax_simple (expr
, aop_ref32
);
2841 ax_simple (expr
, aop_ref64
);
2843 /* If we read exactly the number of bytes we wanted, we're done. */
2844 if (8 * nbytes
== nbits
)
2847 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
)
2849 /* On a bits-big-endian machine, we want the high-order
2851 ax_const_l (expr
, 8 * nbytes
- nbits
);
2852 ax_simple (expr
, aop_rsh_unsigned
);
2856 /* On a bits-little-endian box, we want the low-order NBITS. */
2857 ax_zero_ext (expr
, nbits
);
2861 /* A helper function to return the frame's PC. */
2864 get_ax_pc (void *baton
)
2866 struct agent_expr
*expr
= (struct agent_expr
*) baton
;
2871 /* Compile a DWARF location expression to an agent expression.
2873 EXPR is the agent expression we are building.
2874 LOC is the agent value we modify.
2875 ARCH is the architecture.
2876 ADDR_SIZE is the size of addresses, in bytes.
2877 OP_PTR is the start of the location expression.
2878 OP_END is one past the last byte of the location expression.
2880 This will throw an exception for various kinds of errors -- for
2881 example, if the expression cannot be compiled, or if the expression
2885 dwarf2_compile_expr_to_ax (struct agent_expr
*expr
, struct axs_value
*loc
,
2886 unsigned int addr_size
, const gdb_byte
*op_ptr
,
2887 const gdb_byte
*op_end
,
2888 struct dwarf2_per_cu_data
*per_cu
)
2890 gdbarch
*arch
= expr
->gdbarch
;
2891 std::vector
<int> dw_labels
, patches
;
2892 const gdb_byte
* const base
= op_ptr
;
2893 const gdb_byte
*previous_piece
= op_ptr
;
2894 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
2895 ULONGEST bits_collected
= 0;
2896 unsigned int addr_size_bits
= 8 * addr_size
;
2897 bool bits_big_endian
= byte_order
== BFD_ENDIAN_BIG
;
2899 std::vector
<int> offsets (op_end
- op_ptr
, -1);
2901 /* By default we are making an address. */
2902 loc
->kind
= axs_lvalue_memory
;
2904 while (op_ptr
< op_end
)
2906 enum dwarf_location_atom op
= (enum dwarf_location_atom
) *op_ptr
;
2907 uint64_t uoffset
, reg
;
2911 offsets
[op_ptr
- base
] = expr
->len
;
2914 /* Our basic approach to code generation is to map DWARF
2915 operations directly to AX operations. However, there are
2918 First, DWARF works on address-sized units, but AX always uses
2919 LONGEST. For most operations we simply ignore this
2920 difference; instead we generate sign extensions as needed
2921 before division and comparison operations. It would be nice
2922 to omit the sign extensions, but there is no way to determine
2923 the size of the target's LONGEST. (This code uses the size
2924 of the host LONGEST in some cases -- that is a bug but it is
2927 Second, some DWARF operations cannot be translated to AX.
2928 For these we simply fail. See
2929 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
2964 ax_const_l (expr
, op
- DW_OP_lit0
);
2968 uoffset
= extract_unsigned_integer (op_ptr
, addr_size
, byte_order
);
2969 op_ptr
+= addr_size
;
2970 /* Some versions of GCC emit DW_OP_addr before
2971 DW_OP_GNU_push_tls_address. In this case the value is an
2972 index, not an address. We don't support things like
2973 branching between the address and the TLS op. */
2974 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
2975 uoffset
+= per_cu
->text_offset ();
2976 ax_const_l (expr
, uoffset
);
2980 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 1, byte_order
));
2984 ax_const_l (expr
, extract_signed_integer (op_ptr
, 1, byte_order
));
2988 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 2, byte_order
));
2992 ax_const_l (expr
, extract_signed_integer (op_ptr
, 2, byte_order
));
2996 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 4, byte_order
));
3000 ax_const_l (expr
, extract_signed_integer (op_ptr
, 4, byte_order
));
3004 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 8, byte_order
));
3008 ax_const_l (expr
, extract_signed_integer (op_ptr
, 8, byte_order
));
3012 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
3013 ax_const_l (expr
, uoffset
);
3016 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
3017 ax_const_l (expr
, offset
);
3052 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
3053 loc
->u
.reg
= dwarf_reg_to_regnum_or_error (arch
, op
- DW_OP_reg0
);
3054 loc
->kind
= axs_lvalue_register
;
3058 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
3059 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
3060 loc
->u
.reg
= dwarf_reg_to_regnum_or_error (arch
, reg
);
3061 loc
->kind
= axs_lvalue_register
;
3064 case DW_OP_implicit_value
:
3068 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &len
);
3069 if (op_ptr
+ len
> op_end
)
3070 error (_("DW_OP_implicit_value: too few bytes available."));
3071 if (len
> sizeof (ULONGEST
))
3072 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3075 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, len
,
3078 dwarf_expr_require_composition (op_ptr
, op_end
,
3079 "DW_OP_implicit_value");
3081 loc
->kind
= axs_rvalue
;
3085 case DW_OP_stack_value
:
3086 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
3087 loc
->kind
= axs_rvalue
;
3122 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
3123 i
= dwarf_reg_to_regnum_or_error (arch
, op
- DW_OP_breg0
);
3127 ax_const_l (expr
, offset
);
3128 ax_simple (expr
, aop_add
);
3133 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
3134 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
3135 i
= dwarf_reg_to_regnum_or_error (arch
, reg
);
3139 ax_const_l (expr
, offset
);
3140 ax_simple (expr
, aop_add
);
3146 const gdb_byte
*datastart
;
3148 const struct block
*b
;
3149 struct symbol
*framefunc
;
3151 b
= block_for_pc (expr
->scope
);
3154 error (_("No block found for address"));
3156 framefunc
= block_linkage_function (b
);
3159 error (_("No function found for block"));
3161 func_get_frame_base_dwarf_block (framefunc
, expr
->scope
,
3162 &datastart
, &datalen
);
3164 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
3165 dwarf2_compile_expr_to_ax (expr
, loc
, addr_size
, datastart
,
3166 datastart
+ datalen
, per_cu
);
3167 if (loc
->kind
== axs_lvalue_register
)
3168 require_rvalue (expr
, loc
);
3172 ax_const_l (expr
, offset
);
3173 ax_simple (expr
, aop_add
);
3176 loc
->kind
= axs_lvalue_memory
;
3181 ax_simple (expr
, aop_dup
);
3185 ax_simple (expr
, aop_pop
);
3190 ax_pick (expr
, offset
);
3194 ax_simple (expr
, aop_swap
);
3202 ax_simple (expr
, aop_rot
);
3206 case DW_OP_deref_size
:
3210 if (op
== DW_OP_deref_size
)
3215 if (size
!= 1 && size
!= 2 && size
!= 4 && size
!= 8)
3216 error (_("Unsupported size %d in %s"),
3217 size
, get_DW_OP_name (op
));
3218 access_memory (arch
, expr
, size
* TARGET_CHAR_BIT
);
3223 /* Sign extend the operand. */
3224 ax_ext (expr
, addr_size_bits
);
3225 ax_simple (expr
, aop_dup
);
3226 ax_const_l (expr
, 0);
3227 ax_simple (expr
, aop_less_signed
);
3228 ax_simple (expr
, aop_log_not
);
3229 i
= ax_goto (expr
, aop_if_goto
);
3230 /* We have to emit 0 - X. */
3231 ax_const_l (expr
, 0);
3232 ax_simple (expr
, aop_swap
);
3233 ax_simple (expr
, aop_sub
);
3234 ax_label (expr
, i
, expr
->len
);
3238 /* No need to sign extend here. */
3239 ax_const_l (expr
, 0);
3240 ax_simple (expr
, aop_swap
);
3241 ax_simple (expr
, aop_sub
);
3245 /* Sign extend the operand. */
3246 ax_ext (expr
, addr_size_bits
);
3247 ax_simple (expr
, aop_bit_not
);
3250 case DW_OP_plus_uconst
:
3251 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
3252 /* It would be really weird to emit `DW_OP_plus_uconst 0',
3253 but we micro-optimize anyhow. */
3256 ax_const_l (expr
, reg
);
3257 ax_simple (expr
, aop_add
);
3262 ax_simple (expr
, aop_bit_and
);
3266 /* Sign extend the operands. */
3267 ax_ext (expr
, addr_size_bits
);
3268 ax_simple (expr
, aop_swap
);
3269 ax_ext (expr
, addr_size_bits
);
3270 ax_simple (expr
, aop_swap
);
3271 ax_simple (expr
, aop_div_signed
);
3275 ax_simple (expr
, aop_sub
);
3279 ax_simple (expr
, aop_rem_unsigned
);
3283 ax_simple (expr
, aop_mul
);
3287 ax_simple (expr
, aop_bit_or
);
3291 ax_simple (expr
, aop_add
);
3295 ax_simple (expr
, aop_lsh
);
3299 ax_simple (expr
, aop_rsh_unsigned
);
3303 ax_simple (expr
, aop_rsh_signed
);
3307 ax_simple (expr
, aop_bit_xor
);
3311 /* Sign extend the operands. */
3312 ax_ext (expr
, addr_size_bits
);
3313 ax_simple (expr
, aop_swap
);
3314 ax_ext (expr
, addr_size_bits
);
3315 /* Note no swap here: A <= B is !(B < A). */
3316 ax_simple (expr
, aop_less_signed
);
3317 ax_simple (expr
, aop_log_not
);
3321 /* Sign extend the operands. */
3322 ax_ext (expr
, addr_size_bits
);
3323 ax_simple (expr
, aop_swap
);
3324 ax_ext (expr
, addr_size_bits
);
3325 ax_simple (expr
, aop_swap
);
3326 /* A >= B is !(A < B). */
3327 ax_simple (expr
, aop_less_signed
);
3328 ax_simple (expr
, aop_log_not
);
3332 /* Sign extend the operands. */
3333 ax_ext (expr
, addr_size_bits
);
3334 ax_simple (expr
, aop_swap
);
3335 ax_ext (expr
, addr_size_bits
);
3336 /* No need for a second swap here. */
3337 ax_simple (expr
, aop_equal
);
3341 /* Sign extend the operands. */
3342 ax_ext (expr
, addr_size_bits
);
3343 ax_simple (expr
, aop_swap
);
3344 ax_ext (expr
, addr_size_bits
);
3345 ax_simple (expr
, aop_swap
);
3346 ax_simple (expr
, aop_less_signed
);
3350 /* Sign extend the operands. */
3351 ax_ext (expr
, addr_size_bits
);
3352 ax_simple (expr
, aop_swap
);
3353 ax_ext (expr
, addr_size_bits
);
3354 /* Note no swap here: A > B is B < A. */
3355 ax_simple (expr
, aop_less_signed
);
3359 /* Sign extend the operands. */
3360 ax_ext (expr
, addr_size_bits
);
3361 ax_simple (expr
, aop_swap
);
3362 ax_ext (expr
, addr_size_bits
);
3363 /* No need for a swap here. */
3364 ax_simple (expr
, aop_equal
);
3365 ax_simple (expr
, aop_log_not
);
3368 case DW_OP_call_frame_cfa
:
3371 CORE_ADDR text_offset
;
3373 const gdb_byte
*cfa_start
, *cfa_end
;
3375 if (dwarf2_fetch_cfa_info (arch
, expr
->scope
, per_cu
,
3377 &text_offset
, &cfa_start
, &cfa_end
))
3380 ax_reg (expr
, regnum
);
3383 ax_const_l (expr
, off
);
3384 ax_simple (expr
, aop_add
);
3389 /* Another expression. */
3390 ax_const_l (expr
, text_offset
);
3391 dwarf2_compile_expr_to_ax (expr
, loc
, addr_size
, cfa_start
,
3395 loc
->kind
= axs_lvalue_memory
;
3399 case DW_OP_GNU_push_tls_address
:
3400 case DW_OP_form_tls_address
:
3404 case DW_OP_push_object_address
:
3409 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
3411 i
= ax_goto (expr
, aop_goto
);
3412 dw_labels
.push_back (op_ptr
+ offset
- base
);
3413 patches
.push_back (i
);
3417 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
3419 /* Zero extend the operand. */
3420 ax_zero_ext (expr
, addr_size_bits
);
3421 i
= ax_goto (expr
, aop_if_goto
);
3422 dw_labels
.push_back (op_ptr
+ offset
- base
);
3423 patches
.push_back (i
);
3430 case DW_OP_bit_piece
:
3434 if (op_ptr
- 1 == previous_piece
)
3435 error (_("Cannot translate empty pieces to agent expressions"));
3436 previous_piece
= op_ptr
- 1;
3438 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
3439 if (op
== DW_OP_piece
)
3445 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
3447 if (bits_collected
+ size
> 8 * sizeof (LONGEST
))
3448 error (_("Expression pieces exceed word size"));
3450 /* Access the bits. */
3453 case axs_lvalue_register
:
3454 ax_reg (expr
, loc
->u
.reg
);
3457 case axs_lvalue_memory
:
3458 /* Offset the pointer, if needed. */
3461 ax_const_l (expr
, uoffset
/ 8);
3462 ax_simple (expr
, aop_add
);
3465 access_memory (arch
, expr
, size
);
3469 /* For a bits-big-endian target, shift up what we already
3470 have. For a bits-little-endian target, shift up the
3471 new data. Note that there is a potential bug here if
3472 the DWARF expression leaves multiple values on the
3474 if (bits_collected
> 0)
3476 if (bits_big_endian
)
3478 ax_simple (expr
, aop_swap
);
3479 ax_const_l (expr
, size
);
3480 ax_simple (expr
, aop_lsh
);
3481 /* We don't need a second swap here, because
3482 aop_bit_or is symmetric. */
3486 ax_const_l (expr
, size
);
3487 ax_simple (expr
, aop_lsh
);
3489 ax_simple (expr
, aop_bit_or
);
3492 bits_collected
+= size
;
3493 loc
->kind
= axs_rvalue
;
3497 case DW_OP_GNU_uninit
:
3503 struct dwarf2_locexpr_baton block
;
3504 int size
= (op
== DW_OP_call2
? 2 : 4);
3506 uoffset
= extract_unsigned_integer (op_ptr
, size
, byte_order
);
3509 cu_offset cuoffset
= (cu_offset
) uoffset
;
3510 block
= dwarf2_fetch_die_loc_cu_off (cuoffset
, per_cu
,
3513 /* DW_OP_call_ref is currently not supported. */
3514 gdb_assert (block
.per_cu
== per_cu
);
3516 dwarf2_compile_expr_to_ax (expr
, loc
, addr_size
, block
.data
,
3517 block
.data
+ block
.size
, per_cu
);
3521 case DW_OP_call_ref
:
3524 case DW_OP_GNU_variable_value
:
3532 /* Patch all the branches we emitted. */
3533 for (int i
= 0; i
< patches
.size (); ++i
)
3535 int targ
= offsets
[dw_labels
[i
]];
3537 internal_error (__FILE__
, __LINE__
, _("invalid label"));
3538 ax_label (expr
, patches
[i
], targ
);
3543 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3544 evaluator to calculate the location. */
3545 static struct value
*
3546 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
3548 struct dwarf2_locexpr_baton
*dlbaton
3549 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
3552 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
3553 dlbaton
->size
, dlbaton
->per_cu
);
3558 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3559 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3562 static struct value
*
3563 locexpr_read_variable_at_entry (struct symbol
*symbol
, struct frame_info
*frame
)
3565 struct dwarf2_locexpr_baton
*dlbaton
3566 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
3568 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
3572 /* Implementation of get_symbol_read_needs from
3573 symbol_computed_ops. */
3575 static enum symbol_needs_kind
3576 locexpr_get_symbol_read_needs (struct symbol
*symbol
)
3578 struct dwarf2_locexpr_baton
*dlbaton
3579 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
3581 return dwarf2_loc_desc_get_symbol_read_needs (dlbaton
->data
, dlbaton
->size
,
3585 /* Return true if DATA points to the end of a piece. END is one past
3586 the last byte in the expression. */
3589 piece_end_p (const gdb_byte
*data
, const gdb_byte
*end
)
3591 return data
== end
|| data
[0] == DW_OP_piece
|| data
[0] == DW_OP_bit_piece
;
3594 /* Helper for locexpr_describe_location_piece that finds the name of a
3598 locexpr_regname (struct gdbarch
*gdbarch
, int dwarf_regnum
)
3602 /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3603 We'd rather print *something* here than throw an error. */
3604 regnum
= dwarf_reg_to_regnum (gdbarch
, dwarf_regnum
);
3605 /* gdbarch_register_name may just return "", return something more
3606 descriptive for bad register numbers. */
3609 /* The text is output as "$bad_register_number".
3610 That is why we use the underscores. */
3611 return _("bad_register_number");
3613 return gdbarch_register_name (gdbarch
, regnum
);
3616 /* Nicely describe a single piece of a location, returning an updated
3617 position in the bytecode sequence. This function cannot recognize
3618 all locations; if a location is not recognized, it simply returns
3619 DATA. If there is an error during reading, e.g. we run off the end
3620 of the buffer, an error is thrown. */
3622 static const gdb_byte
*
3623 locexpr_describe_location_piece (struct symbol
*symbol
, struct ui_file
*stream
,
3624 CORE_ADDR addr
, struct objfile
*objfile
,
3625 struct dwarf2_per_cu_data
*per_cu
,
3626 const gdb_byte
*data
, const gdb_byte
*end
,
3627 unsigned int addr_size
)
3629 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3632 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
3634 fprintf_filtered (stream
, _("a variable in $%s"),
3635 locexpr_regname (gdbarch
, data
[0] - DW_OP_reg0
));
3638 else if (data
[0] == DW_OP_regx
)
3642 data
= safe_read_uleb128 (data
+ 1, end
, ®
);
3643 fprintf_filtered (stream
, _("a variable in $%s"),
3644 locexpr_regname (gdbarch
, reg
));
3646 else if (data
[0] == DW_OP_fbreg
)
3648 const struct block
*b
;
3649 struct symbol
*framefunc
;
3651 int64_t frame_offset
;
3652 const gdb_byte
*base_data
, *new_data
, *save_data
= data
;
3654 int64_t base_offset
= 0;
3656 new_data
= safe_read_sleb128 (data
+ 1, end
, &frame_offset
);
3657 if (!piece_end_p (new_data
, end
))
3661 b
= block_for_pc (addr
);
3664 error (_("No block found for address for symbol \"%s\"."),
3665 symbol
->print_name ());
3667 framefunc
= block_linkage_function (b
);
3670 error (_("No function found for block for symbol \"%s\"."),
3671 symbol
->print_name ());
3673 func_get_frame_base_dwarf_block (framefunc
, addr
, &base_data
, &base_size
);
3675 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
3677 const gdb_byte
*buf_end
;
3679 frame_reg
= base_data
[0] - DW_OP_breg0
;
3680 buf_end
= safe_read_sleb128 (base_data
+ 1, base_data
+ base_size
,
3682 if (buf_end
!= base_data
+ base_size
)
3683 error (_("Unexpected opcode after "
3684 "DW_OP_breg%u for symbol \"%s\"."),
3685 frame_reg
, symbol
->print_name ());
3687 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
3689 /* The frame base is just the register, with no offset. */
3690 frame_reg
= base_data
[0] - DW_OP_reg0
;
3695 /* We don't know what to do with the frame base expression,
3696 so we can't trace this variable; give up. */
3700 fprintf_filtered (stream
,
3701 _("a variable at frame base reg $%s offset %s+%s"),
3702 locexpr_regname (gdbarch
, frame_reg
),
3703 plongest (base_offset
), plongest (frame_offset
));
3705 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
3706 && piece_end_p (data
, end
))
3710 data
= safe_read_sleb128 (data
+ 1, end
, &offset
);
3712 fprintf_filtered (stream
,
3713 _("a variable at offset %s from base reg $%s"),
3715 locexpr_regname (gdbarch
, data
[0] - DW_OP_breg0
));
3718 /* The location expression for a TLS variable looks like this (on a
3721 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3722 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3724 0x3 is the encoding for DW_OP_addr, which has an operand as long
3725 as the size of an address on the target machine (here is 8
3726 bytes). Note that more recent version of GCC emit DW_OP_const4u
3727 or DW_OP_const8u, depending on address size, rather than
3728 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3729 The operand represents the offset at which the variable is within
3730 the thread local storage. */
3732 else if (data
+ 1 + addr_size
< end
3733 && (data
[0] == DW_OP_addr
3734 || (addr_size
== 4 && data
[0] == DW_OP_const4u
)
3735 || (addr_size
== 8 && data
[0] == DW_OP_const8u
))
3736 && (data
[1 + addr_size
] == DW_OP_GNU_push_tls_address
3737 || data
[1 + addr_size
] == DW_OP_form_tls_address
)
3738 && piece_end_p (data
+ 2 + addr_size
, end
))
3741 offset
= extract_unsigned_integer (data
+ 1, addr_size
,
3742 gdbarch_byte_order (gdbarch
));
3744 fprintf_filtered (stream
,
3745 _("a thread-local variable at offset 0x%s "
3746 "in the thread-local storage for `%s'"),
3747 phex_nz (offset
, addr_size
), objfile_name (objfile
));
3749 data
+= 1 + addr_size
+ 1;
3752 /* With -gsplit-dwarf a TLS variable can also look like this:
3753 DW_AT_location : 3 byte block: fc 4 e0
3754 (DW_OP_GNU_const_index: 4;
3755 DW_OP_GNU_push_tls_address) */
3756 else if (data
+ 3 <= end
3757 && data
+ 1 + (leb128_size
= skip_leb128 (data
+ 1, end
)) < end
3758 && data
[0] == DW_OP_GNU_const_index
3760 && (data
[1 + leb128_size
] == DW_OP_GNU_push_tls_address
3761 || data
[1 + leb128_size
] == DW_OP_form_tls_address
)
3762 && piece_end_p (data
+ 2 + leb128_size
, end
))
3766 data
= safe_read_uleb128 (data
+ 1, end
, &offset
);
3767 offset
= dwarf2_read_addr_index (per_cu
, offset
);
3768 fprintf_filtered (stream
,
3769 _("a thread-local variable at offset 0x%s "
3770 "in the thread-local storage for `%s'"),
3771 phex_nz (offset
, addr_size
), objfile_name (objfile
));
3775 else if (data
[0] >= DW_OP_lit0
3776 && data
[0] <= DW_OP_lit31
3778 && data
[1] == DW_OP_stack_value
)
3780 fprintf_filtered (stream
, _("the constant %d"), data
[0] - DW_OP_lit0
);
3787 /* Disassemble an expression, stopping at the end of a piece or at the
3788 end of the expression. Returns a pointer to the next unread byte
3789 in the input expression. If ALL is nonzero, then this function
3790 will keep going until it reaches the end of the expression.
3791 If there is an error during reading, e.g. we run off the end
3792 of the buffer, an error is thrown. */
3794 static const gdb_byte
*
3795 disassemble_dwarf_expression (struct ui_file
*stream
,
3796 struct gdbarch
*arch
, unsigned int addr_size
,
3797 int offset_size
, const gdb_byte
*start
,
3798 const gdb_byte
*data
, const gdb_byte
*end
,
3799 int indent
, int all
,
3800 struct dwarf2_per_cu_data
*per_cu
)
3804 || (data
[0] != DW_OP_piece
&& data
[0] != DW_OP_bit_piece
)))
3806 enum dwarf_location_atom op
= (enum dwarf_location_atom
) *data
++;
3811 name
= get_DW_OP_name (op
);
3814 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3815 op
, (long) (data
- 1 - start
));
3816 fprintf_filtered (stream
, " %*ld: %s", indent
+ 4,
3817 (long) (data
- 1 - start
), name
);
3822 ul
= extract_unsigned_integer (data
, addr_size
,
3823 gdbarch_byte_order (arch
));
3825 fprintf_filtered (stream
, " 0x%s", phex_nz (ul
, addr_size
));
3829 ul
= extract_unsigned_integer (data
, 1, gdbarch_byte_order (arch
));
3831 fprintf_filtered (stream
, " %s", pulongest (ul
));
3834 l
= extract_signed_integer (data
, 1, gdbarch_byte_order (arch
));
3836 fprintf_filtered (stream
, " %s", plongest (l
));
3839 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
3841 fprintf_filtered (stream
, " %s", pulongest (ul
));
3844 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3846 fprintf_filtered (stream
, " %s", plongest (l
));
3849 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
3851 fprintf_filtered (stream
, " %s", pulongest (ul
));
3854 l
= extract_signed_integer (data
, 4, gdbarch_byte_order (arch
));
3856 fprintf_filtered (stream
, " %s", plongest (l
));
3859 ul
= extract_unsigned_integer (data
, 8, gdbarch_byte_order (arch
));
3861 fprintf_filtered (stream
, " %s", pulongest (ul
));
3864 l
= extract_signed_integer (data
, 8, gdbarch_byte_order (arch
));
3866 fprintf_filtered (stream
, " %s", plongest (l
));
3869 data
= safe_read_uleb128 (data
, end
, &ul
);
3870 fprintf_filtered (stream
, " %s", pulongest (ul
));
3873 data
= safe_read_sleb128 (data
, end
, &l
);
3874 fprintf_filtered (stream
, " %s", plongest (l
));
3909 fprintf_filtered (stream
, " [$%s]",
3910 locexpr_regname (arch
, op
- DW_OP_reg0
));
3914 data
= safe_read_uleb128 (data
, end
, &ul
);
3915 fprintf_filtered (stream
, " %s [$%s]", pulongest (ul
),
3916 locexpr_regname (arch
, (int) ul
));
3919 case DW_OP_implicit_value
:
3920 data
= safe_read_uleb128 (data
, end
, &ul
);
3922 fprintf_filtered (stream
, " %s", pulongest (ul
));
3957 data
= safe_read_sleb128 (data
, end
, &l
);
3958 fprintf_filtered (stream
, " %s [$%s]", plongest (l
),
3959 locexpr_regname (arch
, op
- DW_OP_breg0
));
3963 data
= safe_read_uleb128 (data
, end
, &ul
);
3964 data
= safe_read_sleb128 (data
, end
, &l
);
3965 fprintf_filtered (stream
, " register %s [$%s] offset %s",
3967 locexpr_regname (arch
, (int) ul
),
3972 data
= safe_read_sleb128 (data
, end
, &l
);
3973 fprintf_filtered (stream
, " %s", plongest (l
));
3976 case DW_OP_xderef_size
:
3977 case DW_OP_deref_size
:
3979 fprintf_filtered (stream
, " %d", *data
);
3983 case DW_OP_plus_uconst
:
3984 data
= safe_read_uleb128 (data
, end
, &ul
);
3985 fprintf_filtered (stream
, " %s", pulongest (ul
));
3989 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3991 fprintf_filtered (stream
, " to %ld",
3992 (long) (data
+ l
- start
));
3996 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3998 fprintf_filtered (stream
, " %ld",
3999 (long) (data
+ l
- start
));
4003 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
4005 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 2));
4009 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
4011 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 4));
4014 case DW_OP_call_ref
:
4015 ul
= extract_unsigned_integer (data
, offset_size
,
4016 gdbarch_byte_order (arch
));
4017 data
+= offset_size
;
4018 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, offset_size
));
4022 data
= safe_read_uleb128 (data
, end
, &ul
);
4023 fprintf_filtered (stream
, " %s (bytes)", pulongest (ul
));
4026 case DW_OP_bit_piece
:
4030 data
= safe_read_uleb128 (data
, end
, &ul
);
4031 data
= safe_read_uleb128 (data
, end
, &offset
);
4032 fprintf_filtered (stream
, " size %s offset %s (bits)",
4033 pulongest (ul
), pulongest (offset
));
4037 case DW_OP_implicit_pointer
:
4038 case DW_OP_GNU_implicit_pointer
:
4040 ul
= extract_unsigned_integer (data
, offset_size
,
4041 gdbarch_byte_order (arch
));
4042 data
+= offset_size
;
4044 data
= safe_read_sleb128 (data
, end
, &l
);
4046 fprintf_filtered (stream
, " DIE %s offset %s",
4047 phex_nz (ul
, offset_size
),
4052 case DW_OP_deref_type
:
4053 case DW_OP_GNU_deref_type
:
4055 int deref_addr_size
= *data
++;
4058 data
= safe_read_uleb128 (data
, end
, &ul
);
4059 cu_offset offset
= (cu_offset
) ul
;
4060 type
= dwarf2_get_die_type (offset
, per_cu
);
4061 fprintf_filtered (stream
, "<");
4062 type_print (type
, "", stream
, -1);
4063 fprintf_filtered (stream
, " [0x%s]> %d",
4064 phex_nz (to_underlying (offset
), 0),
4069 case DW_OP_const_type
:
4070 case DW_OP_GNU_const_type
:
4074 data
= safe_read_uleb128 (data
, end
, &ul
);
4075 cu_offset type_die
= (cu_offset
) ul
;
4076 type
= dwarf2_get_die_type (type_die
, per_cu
);
4077 fprintf_filtered (stream
, "<");
4078 type_print (type
, "", stream
, -1);
4079 fprintf_filtered (stream
, " [0x%s]>",
4080 phex_nz (to_underlying (type_die
), 0));
4084 case DW_OP_regval_type
:
4085 case DW_OP_GNU_regval_type
:
4090 data
= safe_read_uleb128 (data
, end
, ®
);
4091 data
= safe_read_uleb128 (data
, end
, &ul
);
4092 cu_offset type_die
= (cu_offset
) ul
;
4094 type
= dwarf2_get_die_type (type_die
, per_cu
);
4095 fprintf_filtered (stream
, "<");
4096 type_print (type
, "", stream
, -1);
4097 fprintf_filtered (stream
, " [0x%s]> [$%s]",
4098 phex_nz (to_underlying (type_die
), 0),
4099 locexpr_regname (arch
, reg
));
4104 case DW_OP_GNU_convert
:
4105 case DW_OP_reinterpret
:
4106 case DW_OP_GNU_reinterpret
:
4108 data
= safe_read_uleb128 (data
, end
, &ul
);
4109 cu_offset type_die
= (cu_offset
) ul
;
4111 if (to_underlying (type_die
) == 0)
4112 fprintf_filtered (stream
, "<0>");
4117 type
= dwarf2_get_die_type (type_die
, per_cu
);
4118 fprintf_filtered (stream
, "<");
4119 type_print (type
, "", stream
, -1);
4120 fprintf_filtered (stream
, " [0x%s]>",
4121 phex_nz (to_underlying (type_die
), 0));
4126 case DW_OP_entry_value
:
4127 case DW_OP_GNU_entry_value
:
4128 data
= safe_read_uleb128 (data
, end
, &ul
);
4129 fputc_filtered ('\n', stream
);
4130 disassemble_dwarf_expression (stream
, arch
, addr_size
, offset_size
,
4131 start
, data
, data
+ ul
, indent
+ 2,
4136 case DW_OP_GNU_parameter_ref
:
4137 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
4139 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 4));
4143 case DW_OP_GNU_addr_index
:
4144 data
= safe_read_uleb128 (data
, end
, &ul
);
4145 ul
= dwarf2_read_addr_index (per_cu
, ul
);
4146 fprintf_filtered (stream
, " 0x%s", phex_nz (ul
, addr_size
));
4148 case DW_OP_GNU_const_index
:
4149 data
= safe_read_uleb128 (data
, end
, &ul
);
4150 ul
= dwarf2_read_addr_index (per_cu
, ul
);
4151 fprintf_filtered (stream
, " %s", pulongest (ul
));
4154 case DW_OP_GNU_variable_value
:
4155 ul
= extract_unsigned_integer (data
, offset_size
,
4156 gdbarch_byte_order (arch
));
4157 data
+= offset_size
;
4158 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, offset_size
));
4162 fprintf_filtered (stream
, "\n");
4168 static bool dwarf_always_disassemble
;
4171 show_dwarf_always_disassemble (struct ui_file
*file
, int from_tty
,
4172 struct cmd_list_element
*c
, const char *value
)
4174 fprintf_filtered (file
,
4175 _("Whether to always disassemble "
4176 "DWARF expressions is %s.\n"),
4180 /* Describe a single location, which may in turn consist of multiple
4184 locexpr_describe_location_1 (struct symbol
*symbol
, CORE_ADDR addr
,
4185 struct ui_file
*stream
,
4186 const gdb_byte
*data
, size_t size
,
4187 struct objfile
*objfile
, unsigned int addr_size
,
4188 int offset_size
, struct dwarf2_per_cu_data
*per_cu
)
4190 const gdb_byte
*end
= data
+ size
;
4191 int first_piece
= 1, bad
= 0;
4195 const gdb_byte
*here
= data
;
4196 int disassemble
= 1;
4201 fprintf_filtered (stream
, _(", and "));
4203 if (!dwarf_always_disassemble
)
4205 data
= locexpr_describe_location_piece (symbol
, stream
,
4206 addr
, objfile
, per_cu
,
4207 data
, end
, addr_size
);
4208 /* If we printed anything, or if we have an empty piece,
4209 then don't disassemble. */
4211 || data
[0] == DW_OP_piece
4212 || data
[0] == DW_OP_bit_piece
)
4217 fprintf_filtered (stream
, _("a complex DWARF expression:\n"));
4218 data
= disassemble_dwarf_expression (stream
,
4219 get_objfile_arch (objfile
),
4220 addr_size
, offset_size
, data
,
4222 dwarf_always_disassemble
,
4228 int empty
= data
== here
;
4231 fprintf_filtered (stream
, " ");
4232 if (data
[0] == DW_OP_piece
)
4236 data
= safe_read_uleb128 (data
+ 1, end
, &bytes
);
4239 fprintf_filtered (stream
, _("an empty %s-byte piece"),
4242 fprintf_filtered (stream
, _(" [%s-byte piece]"),
4245 else if (data
[0] == DW_OP_bit_piece
)
4247 uint64_t bits
, offset
;
4249 data
= safe_read_uleb128 (data
+ 1, end
, &bits
);
4250 data
= safe_read_uleb128 (data
, end
, &offset
);
4253 fprintf_filtered (stream
,
4254 _("an empty %s-bit piece"),
4257 fprintf_filtered (stream
,
4258 _(" [%s-bit piece, offset %s bits]"),
4259 pulongest (bits
), pulongest (offset
));
4269 if (bad
|| data
> end
)
4270 error (_("Corrupted DWARF2 expression for \"%s\"."),
4271 symbol
->print_name ());
4274 /* Print a natural-language description of SYMBOL to STREAM. This
4275 version is for a symbol with a single location. */
4278 locexpr_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
4279 struct ui_file
*stream
)
4281 struct dwarf2_locexpr_baton
*dlbaton
4282 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4283 struct objfile
*objfile
= dlbaton
->per_cu
->objfile ();
4284 unsigned int addr_size
= dlbaton
->per_cu
->addr_size ();
4285 int offset_size
= dlbaton
->per_cu
->offset_size ();
4287 locexpr_describe_location_1 (symbol
, addr
, stream
,
4288 dlbaton
->data
, dlbaton
->size
,
4289 objfile
, addr_size
, offset_size
,
4293 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4294 any necessary bytecode in AX. */
4297 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct agent_expr
*ax
,
4298 struct axs_value
*value
)
4300 struct dwarf2_locexpr_baton
*dlbaton
4301 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4302 unsigned int addr_size
= dlbaton
->per_cu
->addr_size ();
4304 if (dlbaton
->size
== 0)
4305 value
->optimized_out
= 1;
4307 dwarf2_compile_expr_to_ax (ax
, value
, addr_size
, dlbaton
->data
,
4308 dlbaton
->data
+ dlbaton
->size
, dlbaton
->per_cu
);
4311 /* symbol_computed_ops 'generate_c_location' method. */
4314 locexpr_generate_c_location (struct symbol
*sym
, string_file
*stream
,
4315 struct gdbarch
*gdbarch
,
4316 unsigned char *registers_used
,
4317 CORE_ADDR pc
, const char *result_name
)
4319 struct dwarf2_locexpr_baton
*dlbaton
4320 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (sym
);
4321 unsigned int addr_size
= dlbaton
->per_cu
->addr_size ();
4323 if (dlbaton
->size
== 0)
4324 error (_("symbol \"%s\" is optimized out"), sym
->natural_name ());
4326 compile_dwarf_expr_to_c (stream
, result_name
,
4327 sym
, pc
, gdbarch
, registers_used
, addr_size
,
4328 dlbaton
->data
, dlbaton
->data
+ dlbaton
->size
,
4332 /* The set of location functions used with the DWARF-2 expression
4334 const struct symbol_computed_ops dwarf2_locexpr_funcs
= {
4335 locexpr_read_variable
,
4336 locexpr_read_variable_at_entry
,
4337 locexpr_get_symbol_read_needs
,
4338 locexpr_describe_location
,
4339 0, /* location_has_loclist */
4340 locexpr_tracepoint_var_ref
,
4341 locexpr_generate_c_location
4345 /* Wrapper functions for location lists. These generally find
4346 the appropriate location expression and call something above. */
4348 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4349 evaluator to calculate the location. */
4350 static struct value
*
4351 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
4353 struct dwarf2_loclist_baton
*dlbaton
4354 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4356 const gdb_byte
*data
;
4358 CORE_ADDR pc
= frame
? get_frame_address_in_block (frame
) : 0;
4360 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
4361 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, data
, size
,
4367 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4368 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4371 Function always returns non-NULL value, it may be marked optimized out if
4372 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
4373 if it cannot resolve the parameter for any reason. */
4375 static struct value
*
4376 loclist_read_variable_at_entry (struct symbol
*symbol
, struct frame_info
*frame
)
4378 struct dwarf2_loclist_baton
*dlbaton
4379 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4380 const gdb_byte
*data
;
4384 if (frame
== NULL
|| !get_frame_func_if_available (frame
, &pc
))
4385 return allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
4387 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
4389 return allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
4391 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol
), frame
, data
, size
);
4394 /* Implementation of get_symbol_read_needs from
4395 symbol_computed_ops. */
4397 static enum symbol_needs_kind
4398 loclist_symbol_needs (struct symbol
*symbol
)
4400 /* If there's a location list, then assume we need to have a frame
4401 to choose the appropriate location expression. With tracking of
4402 global variables this is not necessarily true, but such tracking
4403 is disabled in GCC at the moment until we figure out how to
4406 return SYMBOL_NEEDS_FRAME
;
4409 /* Print a natural-language description of SYMBOL to STREAM. This
4410 version applies when there is a list of different locations, each
4411 with a specified address range. */
4414 loclist_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
4415 struct ui_file
*stream
)
4417 struct dwarf2_loclist_baton
*dlbaton
4418 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4419 const gdb_byte
*loc_ptr
, *buf_end
;
4420 struct objfile
*objfile
= dlbaton
->per_cu
->objfile ();
4421 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
4422 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
4423 unsigned int addr_size
= dlbaton
->per_cu
->addr_size ();
4424 int offset_size
= dlbaton
->per_cu
->offset_size ();
4425 int signed_addr_p
= bfd_get_sign_extend_vma (objfile
->obfd
);
4426 /* Adjust base_address for relocatable objects. */
4427 CORE_ADDR base_offset
= dlbaton
->per_cu
->text_offset ();
4428 CORE_ADDR base_address
= dlbaton
->base_address
+ base_offset
;
4431 loc_ptr
= dlbaton
->data
;
4432 buf_end
= dlbaton
->data
+ dlbaton
->size
;
4434 fprintf_filtered (stream
, _("multi-location:\n"));
4436 /* Iterate through locations until we run out. */
4439 CORE_ADDR low
= 0, high
= 0; /* init for gcc -Wall */
4441 enum debug_loc_kind kind
;
4442 const gdb_byte
*new_ptr
= NULL
; /* init for gcc -Wall */
4444 if (dlbaton
->from_dwo
)
4445 kind
= decode_debug_loc_dwo_addresses (dlbaton
->per_cu
,
4446 loc_ptr
, buf_end
, &new_ptr
,
4447 &low
, &high
, byte_order
);
4449 kind
= decode_debug_loc_addresses (loc_ptr
, buf_end
, &new_ptr
,
4451 byte_order
, addr_size
,
4456 case DEBUG_LOC_END_OF_LIST
:
4459 case DEBUG_LOC_BASE_ADDRESS
:
4460 base_address
= high
+ base_offset
;
4461 fprintf_filtered (stream
, _(" Base address %s"),
4462 paddress (gdbarch
, base_address
));
4464 case DEBUG_LOC_START_END
:
4465 case DEBUG_LOC_START_LENGTH
:
4467 case DEBUG_LOC_BUFFER_OVERFLOW
:
4468 case DEBUG_LOC_INVALID_ENTRY
:
4469 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4470 symbol
->print_name ());
4472 gdb_assert_not_reached ("bad debug_loc_kind");
4475 /* Otherwise, a location expression entry. */
4476 low
+= base_address
;
4477 high
+= base_address
;
4479 low
= gdbarch_adjust_dwarf2_addr (gdbarch
, low
);
4480 high
= gdbarch_adjust_dwarf2_addr (gdbarch
, high
);
4482 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
4485 /* (It would improve readability to print only the minimum
4486 necessary digits of the second number of the range.) */
4487 fprintf_filtered (stream
, _(" Range %s-%s: "),
4488 paddress (gdbarch
, low
), paddress (gdbarch
, high
));
4490 /* Now describe this particular location. */
4491 locexpr_describe_location_1 (symbol
, low
, stream
, loc_ptr
, length
,
4492 objfile
, addr_size
, offset_size
,
4495 fprintf_filtered (stream
, "\n");
4501 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4502 any necessary bytecode in AX. */
4504 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct agent_expr
*ax
,
4505 struct axs_value
*value
)
4507 struct dwarf2_loclist_baton
*dlbaton
4508 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4509 const gdb_byte
*data
;
4511 unsigned int addr_size
= dlbaton
->per_cu
->addr_size ();
4513 data
= dwarf2_find_location_expression (dlbaton
, &size
, ax
->scope
);
4515 value
->optimized_out
= 1;
4517 dwarf2_compile_expr_to_ax (ax
, value
, addr_size
, data
, data
+ size
,
4521 /* symbol_computed_ops 'generate_c_location' method. */
4524 loclist_generate_c_location (struct symbol
*sym
, string_file
*stream
,
4525 struct gdbarch
*gdbarch
,
4526 unsigned char *registers_used
,
4527 CORE_ADDR pc
, const char *result_name
)
4529 struct dwarf2_loclist_baton
*dlbaton
4530 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (sym
);
4531 unsigned int addr_size
= dlbaton
->per_cu
->addr_size ();
4532 const gdb_byte
*data
;
4535 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
4537 error (_("symbol \"%s\" is optimized out"), sym
->natural_name ());
4539 compile_dwarf_expr_to_c (stream
, result_name
,
4540 sym
, pc
, gdbarch
, registers_used
, addr_size
,
4545 /* The set of location functions used with the DWARF-2 expression
4546 evaluator and location lists. */
4547 const struct symbol_computed_ops dwarf2_loclist_funcs
= {
4548 loclist_read_variable
,
4549 loclist_read_variable_at_entry
,
4550 loclist_symbol_needs
,
4551 loclist_describe_location
,
4552 1, /* location_has_loclist */
4553 loclist_tracepoint_var_ref
,
4554 loclist_generate_c_location
4557 void _initialize_dwarf2loc ();
4559 _initialize_dwarf2loc ()
4561 add_setshow_zuinteger_cmd ("entry-values", class_maintenance
,
4562 &entry_values_debug
,
4563 _("Set entry values and tail call frames "
4565 _("Show entry values and tail call frames "
4567 _("When non-zero, the process of determining "
4568 "parameter values from function entry point "
4569 "and tail call frames will be printed."),
4571 show_entry_values_debug
,
4572 &setdebuglist
, &showdebuglist
);
4574 add_setshow_boolean_cmd ("always-disassemble", class_obscure
,
4575 &dwarf_always_disassemble
, _("\
4576 Set whether `info address' always disassembles DWARF expressions."), _("\
4577 Show whether `info address' always disassembles DWARF expressions."), _("\
4578 When enabled, DWARF expressions are always printed in an assembly-like\n\
4579 syntax. When disabled, expressions will be printed in a more\n\
4580 conversational style, when possible."),
4582 show_dwarf_always_disassemble
,
4584 &show_dwarf_cmdlist
);