1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003-2019 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 "dwarf2expr.h"
38 #include "dwarf2loc.h"
39 #include "dwarf2-frame.h"
40 #include "compile/compile.h"
41 #include "common/selftest.h"
44 #include <unordered_set>
45 #include "common/underlying.h"
46 #include "common/byte-vector.h"
48 extern int dwarf_always_disassemble;
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_end_of_list:
179 return DEBUG_LOC_END_OF_LIST;
180 case DW_LLE_base_address:
181 if (loc_ptr + addr_size > buf_end)
182 return DEBUG_LOC_BUFFER_OVERFLOW;
184 *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
186 *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
187 loc_ptr += addr_size;
189 return DEBUG_LOC_BASE_ADDRESS;
190 case DW_LLE_offset_pair:
191 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
193 return DEBUG_LOC_BUFFER_OVERFLOW;
195 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &u64);
197 return DEBUG_LOC_BUFFER_OVERFLOW;
200 return DEBUG_LOC_START_END;
202 return DEBUG_LOC_INVALID_ENTRY;
206 /* Decode the addresses in .debug_loc.dwo entry.
207 A pointer to the next byte to examine is returned in *NEW_PTR.
208 The encoded low,high addresses are return in *LOW,*HIGH.
209 The result indicates the kind of entry found. */
211 static enum debug_loc_kind
212 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
213 const gdb_byte *loc_ptr,
214 const gdb_byte *buf_end,
215 const gdb_byte **new_ptr,
216 CORE_ADDR *low, CORE_ADDR *high,
217 enum bfd_endian byte_order)
219 uint64_t low_index, high_index;
221 if (loc_ptr == buf_end)
222 return DEBUG_LOC_BUFFER_OVERFLOW;
226 case DW_LLE_GNU_end_of_list_entry:
228 return DEBUG_LOC_END_OF_LIST;
229 case DW_LLE_GNU_base_address_selection_entry:
231 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
233 return DEBUG_LOC_BUFFER_OVERFLOW;
234 *high = dwarf2_read_addr_index (per_cu, high_index);
236 return DEBUG_LOC_BASE_ADDRESS;
237 case DW_LLE_GNU_start_end_entry:
238 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
240 return DEBUG_LOC_BUFFER_OVERFLOW;
241 *low = dwarf2_read_addr_index (per_cu, low_index);
242 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
244 return DEBUG_LOC_BUFFER_OVERFLOW;
245 *high = dwarf2_read_addr_index (per_cu, high_index);
247 return DEBUG_LOC_START_END;
248 case DW_LLE_GNU_start_length_entry:
249 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
251 return DEBUG_LOC_BUFFER_OVERFLOW;
252 *low = dwarf2_read_addr_index (per_cu, low_index);
253 if (loc_ptr + 4 > buf_end)
254 return DEBUG_LOC_BUFFER_OVERFLOW;
256 *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
257 *new_ptr = loc_ptr + 4;
258 return DEBUG_LOC_START_LENGTH;
260 return DEBUG_LOC_INVALID_ENTRY;
264 /* A function for dealing with location lists. Given a
265 symbol baton (BATON) and a pc value (PC), find the appropriate
266 location expression, set *LOCEXPR_LENGTH, and return a pointer
267 to the beginning of the expression. Returns NULL on failure.
269 For now, only return the first matching location expression; there
270 can be more than one in the list. */
273 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
274 size_t *locexpr_length, CORE_ADDR pc)
276 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
277 struct gdbarch *gdbarch = get_objfile_arch (objfile);
278 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
279 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
280 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
281 /* Adjust base_address for relocatable objects. */
282 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
283 CORE_ADDR base_address = baton->base_address + base_offset;
284 const gdb_byte *loc_ptr, *buf_end;
286 loc_ptr = baton->data;
287 buf_end = baton->data + baton->size;
291 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
293 enum debug_loc_kind kind;
294 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
297 kind = decode_debug_loc_dwo_addresses (baton->per_cu,
298 loc_ptr, buf_end, &new_ptr,
299 &low, &high, byte_order);
300 else if (dwarf2_version (baton->per_cu) < 5)
301 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
303 byte_order, addr_size,
306 kind = decode_debug_loclists_addresses (baton->per_cu,
307 loc_ptr, buf_end, &new_ptr,
308 &low, &high, byte_order,
309 addr_size, signed_addr_p);
314 case DEBUG_LOC_END_OF_LIST:
317 case DEBUG_LOC_BASE_ADDRESS:
318 base_address = high + base_offset;
320 case DEBUG_LOC_START_END:
321 case DEBUG_LOC_START_LENGTH:
323 case DEBUG_LOC_BUFFER_OVERFLOW:
324 case DEBUG_LOC_INVALID_ENTRY:
325 error (_("dwarf2_find_location_expression: "
326 "Corrupted DWARF expression."));
328 gdb_assert_not_reached ("bad debug_loc_kind");
331 /* Otherwise, a location expression entry.
332 If the entry is from a DWO, don't add base address: the entry is from
333 .debug_addr which already has the DWARF "base address". We still add
334 base_offset in case we're debugging a PIE executable. */
343 high += base_address;
346 if (dwarf2_version (baton->per_cu) < 5)
348 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
353 unsigned int bytes_read;
355 length = read_unsigned_leb128 (NULL, loc_ptr, &bytes_read);
356 loc_ptr += bytes_read;
359 if (low == high && pc == low)
361 /* This is entry PC record present only at entry point
362 of a function. Verify it is really the function entry point. */
364 const struct block *pc_block = block_for_pc (pc);
365 struct symbol *pc_func = NULL;
368 pc_func = block_linkage_function (pc_block);
370 if (pc_func && pc == BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (pc_func)))
372 *locexpr_length = length;
377 if (pc >= low && pc < high)
379 *locexpr_length = length;
387 /* This is the baton used when performing dwarf2 expression
389 struct dwarf_expr_baton
391 struct frame_info *frame;
392 struct dwarf2_per_cu_data *per_cu;
393 CORE_ADDR obj_address;
396 /* Implement find_frame_base_location method for LOC_BLOCK functions using
397 DWARF expression for its DW_AT_frame_base. */
400 locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
401 const gdb_byte **start, size_t *length)
403 struct dwarf2_locexpr_baton *symbaton
404 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
406 *length = symbaton->size;
407 *start = symbaton->data;
410 /* Implement the struct symbol_block_ops::get_frame_base method for
411 LOC_BLOCK functions using a DWARF expression as its DW_AT_frame_base. */
414 locexpr_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
416 struct gdbarch *gdbarch;
418 struct dwarf2_locexpr_baton *dlbaton;
419 const gdb_byte *start;
421 struct value *result;
423 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
424 Thus, it's supposed to provide the find_frame_base_location method as
426 gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
428 gdbarch = get_frame_arch (frame);
429 type = builtin_type (gdbarch)->builtin_data_ptr;
430 dlbaton = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
432 SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
433 (framefunc, get_frame_pc (frame), &start, &length);
434 result = dwarf2_evaluate_loc_desc (type, frame, start, length,
437 /* The DW_AT_frame_base attribute contains a location description which
438 computes the base address itself. However, the call to
439 dwarf2_evaluate_loc_desc returns a value representing a variable at
440 that address. The frame base address is thus this variable's
442 return value_address (result);
445 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
446 function uses DWARF expression for its DW_AT_frame_base. */
448 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
450 locexpr_find_frame_base_location,
451 locexpr_get_frame_base
454 /* Implement find_frame_base_location method for LOC_BLOCK functions using
455 DWARF location list for its DW_AT_frame_base. */
458 loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
459 const gdb_byte **start, size_t *length)
461 struct dwarf2_loclist_baton *symbaton
462 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
464 *start = dwarf2_find_location_expression (symbaton, length, pc);
467 /* Implement the struct symbol_block_ops::get_frame_base method for
468 LOC_BLOCK functions using a DWARF location list as its DW_AT_frame_base. */
471 loclist_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
473 struct gdbarch *gdbarch;
475 struct dwarf2_loclist_baton *dlbaton;
476 const gdb_byte *start;
478 struct value *result;
480 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
481 Thus, it's supposed to provide the find_frame_base_location method as
483 gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
485 gdbarch = get_frame_arch (frame);
486 type = builtin_type (gdbarch)->builtin_data_ptr;
487 dlbaton = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
489 SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
490 (framefunc, get_frame_pc (frame), &start, &length);
491 result = dwarf2_evaluate_loc_desc (type, frame, start, length,
494 /* The DW_AT_frame_base attribute contains a location description which
495 computes the base address itself. However, the call to
496 dwarf2_evaluate_loc_desc returns a value representing a variable at
497 that address. The frame base address is thus this variable's
499 return value_address (result);
502 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
503 function uses DWARF location list for its DW_AT_frame_base. */
505 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
507 loclist_find_frame_base_location,
508 loclist_get_frame_base
511 /* See dwarf2loc.h. */
514 func_get_frame_base_dwarf_block (struct symbol *framefunc, CORE_ADDR pc,
515 const gdb_byte **start, size_t *length)
517 if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
519 const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
521 ops_block->find_frame_base_location (framefunc, pc, start, length);
527 error (_("Could not find the frame base for \"%s\"."),
528 SYMBOL_NATURAL_NAME (framefunc));
532 get_frame_pc_for_per_cu_dwarf_call (void *baton)
534 dwarf_expr_context *ctx = (dwarf_expr_context *) baton;
536 return ctx->get_frame_pc ();
540 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
541 struct dwarf2_per_cu_data *per_cu)
543 struct dwarf2_locexpr_baton block;
545 block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu,
546 get_frame_pc_for_per_cu_dwarf_call,
549 /* DW_OP_call_ref is currently not supported. */
550 gdb_assert (block.per_cu == per_cu);
552 ctx->eval (block.data, block.size);
555 /* Given context CTX, section offset SECT_OFF, and compilation unit
556 data PER_CU, execute the "variable value" operation on the DIE
557 found at SECT_OFF. */
559 static struct value *
560 sect_variable_value (struct dwarf_expr_context *ctx, sect_offset sect_off,
561 struct dwarf2_per_cu_data *per_cu)
563 struct type *die_type = dwarf2_fetch_die_type_sect_off (sect_off, per_cu);
565 if (die_type == NULL)
566 error (_("Bad DW_OP_GNU_variable_value DIE."));
568 /* Note: Things still work when the following test is removed. This
569 test and error is here to conform to the proposed specification. */
570 if (TYPE_CODE (die_type) != TYPE_CODE_INT
571 && TYPE_CODE (die_type) != TYPE_CODE_PTR)
572 error (_("Type of DW_OP_GNU_variable_value DIE must be an integer or pointer."));
574 struct type *type = lookup_pointer_type (die_type);
575 struct frame_info *frame = get_selected_frame (_("No frame selected."));
576 return indirect_synthetic_pointer (sect_off, 0, per_cu, frame, type, true);
579 class dwarf_evaluate_loc_desc : public dwarf_expr_context
583 struct frame_info *frame;
584 struct dwarf2_per_cu_data *per_cu;
585 CORE_ADDR obj_address;
587 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
588 the frame in BATON. */
590 CORE_ADDR get_frame_cfa () override
592 return dwarf2_frame_cfa (frame);
595 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
596 the frame in BATON. */
598 CORE_ADDR get_frame_pc () override
600 return get_frame_address_in_block (frame);
603 /* Using the objfile specified in BATON, find the address for the
604 current thread's thread-local storage with offset OFFSET. */
605 CORE_ADDR get_tls_address (CORE_ADDR offset) override
607 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
609 return target_translate_tls_address (objfile, offset);
612 /* Helper interface of per_cu_dwarf_call for
613 dwarf2_evaluate_loc_desc. */
615 void dwarf_call (cu_offset die_offset) override
617 per_cu_dwarf_call (this, die_offset, per_cu);
620 /* Helper interface of sect_variable_value for
621 dwarf2_evaluate_loc_desc. */
623 struct value *dwarf_variable_value (sect_offset sect_off) override
625 return sect_variable_value (this, sect_off, per_cu);
628 struct type *get_base_type (cu_offset die_offset, int size) override
630 struct type *result = dwarf2_get_die_type (die_offset, per_cu);
632 error (_("Could not find type for DW_OP_const_type"));
633 if (size != 0 && TYPE_LENGTH (result) != size)
634 error (_("DW_OP_const_type has different sizes for type and data"));
638 /* Callback function for dwarf2_evaluate_loc_desc.
639 Fetch the address indexed by DW_OP_GNU_addr_index. */
641 CORE_ADDR get_addr_index (unsigned int index) override
643 return dwarf2_read_addr_index (per_cu, index);
646 /* Callback function for get_object_address. Return the address of the VLA
649 CORE_ADDR get_object_address () override
651 if (obj_address == 0)
652 error (_("Location address is not set."));
656 /* Execute DWARF block of call_site_parameter which matches KIND and
657 KIND_U. Choose DEREF_SIZE value of that parameter. Search
658 caller of this objects's frame.
660 The caller can be from a different CU - per_cu_dwarf_call
661 implementation can be more simple as it does not support cross-CU
664 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind,
665 union call_site_parameter_u kind_u,
666 int deref_size) override
668 struct frame_info *caller_frame;
669 struct dwarf2_per_cu_data *caller_per_cu;
670 struct call_site_parameter *parameter;
671 const gdb_byte *data_src;
674 caller_frame = get_prev_frame (frame);
676 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
678 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
679 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
681 /* DEREF_SIZE size is not verified here. */
682 if (data_src == NULL)
683 throw_error (NO_ENTRY_VALUE_ERROR,
684 _("Cannot resolve DW_AT_call_data_value"));
686 scoped_restore save_frame = make_scoped_restore (&this->frame,
688 scoped_restore save_per_cu = make_scoped_restore (&this->per_cu,
690 scoped_restore save_obj_addr = make_scoped_restore (&this->obj_address,
693 scoped_restore save_arch = make_scoped_restore (&this->gdbarch);
695 = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
696 scoped_restore save_addr_size = make_scoped_restore (&this->addr_size);
697 this->addr_size = dwarf2_per_cu_addr_size (per_cu);
698 scoped_restore save_offset = make_scoped_restore (&this->offset);
699 this->offset = dwarf2_per_cu_text_offset (per_cu);
701 this->eval (data_src, size);
704 /* Using the frame specified in BATON, find the location expression
705 describing the frame base. Return a pointer to it in START and
706 its length in LENGTH. */
707 void get_frame_base (const gdb_byte **start, size_t * length) override
709 /* FIXME: cagney/2003-03-26: This code should be using
710 get_frame_base_address(), and then implement a dwarf2 specific
712 struct symbol *framefunc;
713 const struct block *bl = get_frame_block (frame, NULL);
716 error (_("frame address is not available."));
718 /* Use block_linkage_function, which returns a real (not inlined)
719 function, instead of get_frame_function, which may return an
721 framefunc = block_linkage_function (bl);
723 /* If we found a frame-relative symbol then it was certainly within
724 some function associated with a frame. If we can't find the frame,
725 something has gone wrong. */
726 gdb_assert (framefunc != NULL);
728 func_get_frame_base_dwarf_block (framefunc,
729 get_frame_address_in_block (frame),
733 /* Read memory at ADDR (length LEN) into BUF. */
735 void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t len) override
737 read_memory (addr, buf, len);
740 /* Using the frame specified in BATON, return the value of register
741 REGNUM, treated as a pointer. */
742 CORE_ADDR read_addr_from_reg (int dwarf_regnum) override
744 struct gdbarch *gdbarch = get_frame_arch (frame);
745 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
747 return address_from_register (regnum, frame);
750 /* Implement "get_reg_value" callback. */
752 struct value *get_reg_value (struct type *type, int dwarf_regnum) override
754 struct gdbarch *gdbarch = get_frame_arch (frame);
755 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
757 return value_from_register (type, regnum, frame);
761 /* See dwarf2loc.h. */
763 unsigned int entry_values_debug = 0;
765 /* Helper to set entry_values_debug. */
768 show_entry_values_debug (struct ui_file *file, int from_tty,
769 struct cmd_list_element *c, const char *value)
771 fprintf_filtered (file,
772 _("Entry values and tail call frames debugging is %s.\n"),
776 /* Find DW_TAG_call_site's DW_AT_call_target address.
777 CALLER_FRAME (for registers) can be NULL if it is not known. This function
778 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
781 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
782 struct call_site *call_site,
783 struct frame_info *caller_frame)
785 switch (FIELD_LOC_KIND (call_site->target))
787 case FIELD_LOC_KIND_DWARF_BLOCK:
789 struct dwarf2_locexpr_baton *dwarf_block;
791 struct type *caller_core_addr_type;
792 struct gdbarch *caller_arch;
794 dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
795 if (dwarf_block == NULL)
797 struct bound_minimal_symbol msym;
799 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
800 throw_error (NO_ENTRY_VALUE_ERROR,
801 _("DW_AT_call_target is not specified at %s in %s"),
802 paddress (call_site_gdbarch, call_site->pc),
803 (msym.minsym == NULL ? "???"
804 : MSYMBOL_PRINT_NAME (msym.minsym)));
807 if (caller_frame == NULL)
809 struct bound_minimal_symbol msym;
811 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
812 throw_error (NO_ENTRY_VALUE_ERROR,
813 _("DW_AT_call_target DWARF block resolving "
814 "requires known frame which is currently not "
815 "available at %s in %s"),
816 paddress (call_site_gdbarch, call_site->pc),
817 (msym.minsym == NULL ? "???"
818 : MSYMBOL_PRINT_NAME (msym.minsym)));
821 caller_arch = get_frame_arch (caller_frame);
822 caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
823 val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
824 dwarf_block->data, dwarf_block->size,
825 dwarf_block->per_cu);
826 /* DW_AT_call_target is a DWARF expression, not a DWARF location. */
827 if (VALUE_LVAL (val) == lval_memory)
828 return value_address (val);
830 return value_as_address (val);
833 case FIELD_LOC_KIND_PHYSNAME:
835 const char *physname;
836 struct bound_minimal_symbol msym;
838 physname = FIELD_STATIC_PHYSNAME (call_site->target);
840 /* Handle both the mangled and demangled PHYSNAME. */
841 msym = lookup_minimal_symbol (physname, NULL, NULL);
842 if (msym.minsym == NULL)
844 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
845 throw_error (NO_ENTRY_VALUE_ERROR,
846 _("Cannot find function \"%s\" for a call site target "
848 physname, paddress (call_site_gdbarch, call_site->pc),
849 (msym.minsym == NULL ? "???"
850 : MSYMBOL_PRINT_NAME (msym.minsym)));
853 return BMSYMBOL_VALUE_ADDRESS (msym);
856 case FIELD_LOC_KIND_PHYSADDR:
857 return FIELD_STATIC_PHYSADDR (call_site->target);
860 internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
864 /* Convert function entry point exact address ADDR to the function which is
865 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
866 NO_ENTRY_VALUE_ERROR otherwise. */
868 static struct symbol *
869 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
871 struct symbol *sym = find_pc_function (addr);
874 if (sym == NULL || BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)) != addr)
875 throw_error (NO_ENTRY_VALUE_ERROR,
876 _("DW_TAG_call_site resolving failed to find function "
877 "name for address %s"),
878 paddress (gdbarch, addr));
880 type = SYMBOL_TYPE (sym);
881 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
882 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
887 /* Verify function with entry point exact address ADDR can never call itself
888 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
889 can call itself via tail calls.
891 If a funtion can tail call itself its entry value based parameters are
892 unreliable. There is no verification whether the value of some/all
893 parameters is unchanged through the self tail call, we expect if there is
894 a self tail call all the parameters can be modified. */
897 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
901 /* The verification is completely unordered. Track here function addresses
902 which still need to be iterated. */
903 std::vector<CORE_ADDR> todo;
905 /* Track here CORE_ADDRs which were already visited. */
906 std::unordered_set<CORE_ADDR> addr_hash;
908 todo.push_back (verify_addr);
909 while (!todo.empty ())
911 struct symbol *func_sym;
912 struct call_site *call_site;
917 func_sym = func_addr_to_tail_call_list (gdbarch, addr);
919 for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
920 call_site; call_site = call_site->tail_call_next)
922 CORE_ADDR target_addr;
924 /* CALLER_FRAME with registers is not available for tail-call jumped
926 target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
928 if (target_addr == verify_addr)
930 struct bound_minimal_symbol msym;
932 msym = lookup_minimal_symbol_by_pc (verify_addr);
933 throw_error (NO_ENTRY_VALUE_ERROR,
934 _("DW_OP_entry_value resolving has found "
935 "function \"%s\" at %s can call itself via tail "
937 (msym.minsym == NULL ? "???"
938 : MSYMBOL_PRINT_NAME (msym.minsym)),
939 paddress (gdbarch, verify_addr));
942 if (addr_hash.insert (target_addr).second)
943 todo.push_back (target_addr);
948 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
949 ENTRY_VALUES_DEBUG. */
952 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
954 CORE_ADDR addr = call_site->pc;
955 struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
957 fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
958 (msym.minsym == NULL ? "???"
959 : MSYMBOL_PRINT_NAME (msym.minsym)));
963 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
964 only top callers and bottom callees which are present in both. GDBARCH is
965 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
966 no remaining possibilities to provide unambiguous non-trivial result.
967 RESULTP should point to NULL on the first (initialization) call. Caller is
968 responsible for xfree of any RESULTP data. */
971 chain_candidate (struct gdbarch *gdbarch,
972 gdb::unique_xmalloc_ptr<struct call_site_chain> *resultp,
973 std::vector<struct call_site *> *chain)
975 long length = chain->size ();
976 int callers, callees, idx;
978 if (*resultp == NULL)
980 /* Create the initial chain containing all the passed PCs. */
982 struct call_site_chain *result
983 = ((struct call_site_chain *)
984 xmalloc (sizeof (*result)
985 + sizeof (*result->call_site) * (length - 1)));
986 result->length = length;
987 result->callers = result->callees = length;
988 if (!chain->empty ())
989 memcpy (result->call_site, chain->data (),
990 sizeof (*result->call_site) * length);
991 resultp->reset (result);
993 if (entry_values_debug)
995 fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
996 for (idx = 0; idx < length; idx++)
997 tailcall_dump (gdbarch, result->call_site[idx]);
998 fputc_unfiltered ('\n', gdb_stdlog);
1004 if (entry_values_debug)
1006 fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
1007 for (idx = 0; idx < length; idx++)
1008 tailcall_dump (gdbarch, chain->at (idx));
1009 fputc_unfiltered ('\n', gdb_stdlog);
1012 /* Intersect callers. */
1014 callers = std::min ((long) (*resultp)->callers, length);
1015 for (idx = 0; idx < callers; idx++)
1016 if ((*resultp)->call_site[idx] != chain->at (idx))
1018 (*resultp)->callers = idx;
1022 /* Intersect callees. */
1024 callees = std::min ((long) (*resultp)->callees, length);
1025 for (idx = 0; idx < callees; idx++)
1026 if ((*resultp)->call_site[(*resultp)->length - 1 - idx]
1027 != chain->at (length - 1 - idx))
1029 (*resultp)->callees = idx;
1033 if (entry_values_debug)
1035 fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
1036 for (idx = 0; idx < (*resultp)->callers; idx++)
1037 tailcall_dump (gdbarch, (*resultp)->call_site[idx]);
1038 fputs_unfiltered (" |", gdb_stdlog);
1039 for (idx = 0; idx < (*resultp)->callees; idx++)
1040 tailcall_dump (gdbarch,
1041 (*resultp)->call_site[(*resultp)->length
1042 - (*resultp)->callees + idx]);
1043 fputc_unfiltered ('\n', gdb_stdlog);
1046 if ((*resultp)->callers == 0 && (*resultp)->callees == 0)
1048 /* There are no common callers or callees. It could be also a direct
1049 call (which has length 0) with ambiguous possibility of an indirect
1050 call - CALLERS == CALLEES == 0 is valid during the first allocation
1051 but any subsequence processing of such entry means ambiguity. */
1052 resultp->reset (NULL);
1056 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
1057 PC again. In such case there must be two different code paths to reach
1058 it. CALLERS + CALLEES equal to LENGTH in the case of self tail-call. */
1059 gdb_assert ((*resultp)->callers + (*resultp)->callees <= (*resultp)->length);
1062 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
1063 assumed frames between them use GDBARCH. Use depth first search so we can
1064 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
1065 would have needless GDB stack overhead. Caller is responsible for xfree of
1066 the returned result. Any unreliability results in thrown
1067 NO_ENTRY_VALUE_ERROR. */
1069 static struct call_site_chain *
1070 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
1071 CORE_ADDR callee_pc)
1073 CORE_ADDR save_callee_pc = callee_pc;
1074 gdb::unique_xmalloc_ptr<struct call_site_chain> retval;
1075 struct call_site *call_site;
1077 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
1078 call_site nor any possible call_site at CALLEE_PC's function is there.
1079 Any CALL_SITE in CHAIN will be iterated to its siblings - via
1080 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
1081 std::vector<struct call_site *> chain;
1083 /* We are not interested in the specific PC inside the callee function. */
1084 callee_pc = get_pc_function_start (callee_pc);
1086 throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
1087 paddress (gdbarch, save_callee_pc));
1089 /* Mark CALL_SITEs so we do not visit the same ones twice. */
1090 std::unordered_set<CORE_ADDR> addr_hash;
1092 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
1093 at the target's function. All the possible tail call sites in the
1094 target's function will get iterated as already pushed into CHAIN via their
1096 call_site = call_site_for_pc (gdbarch, caller_pc);
1100 CORE_ADDR target_func_addr;
1101 struct call_site *target_call_site;
1103 /* CALLER_FRAME with registers is not available for tail-call jumped
1105 target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
1107 if (target_func_addr == callee_pc)
1109 chain_candidate (gdbarch, &retval, &chain);
1113 /* There is no way to reach CALLEE_PC again as we would prevent
1114 entering it twice as being already marked in ADDR_HASH. */
1115 target_call_site = NULL;
1119 struct symbol *target_func;
1121 target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
1122 target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
1127 /* Attempt to visit TARGET_CALL_SITE. */
1129 if (target_call_site)
1131 if (addr_hash.insert (target_call_site->pc).second)
1133 /* Successfully entered TARGET_CALL_SITE. */
1135 chain.push_back (target_call_site);
1140 /* Backtrack (without revisiting the originating call_site). Try the
1141 callers's sibling; if there isn't any try the callers's callers's
1144 target_call_site = NULL;
1145 while (!chain.empty ())
1147 call_site = chain.back ();
1150 size_t removed = addr_hash.erase (call_site->pc);
1151 gdb_assert (removed == 1);
1153 target_call_site = call_site->tail_call_next;
1154 if (target_call_site)
1158 while (target_call_site);
1163 call_site = chain.back ();
1168 struct bound_minimal_symbol msym_caller, msym_callee;
1170 msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
1171 msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
1172 throw_error (NO_ENTRY_VALUE_ERROR,
1173 _("There are no unambiguously determinable intermediate "
1174 "callers or callees between caller function \"%s\" at %s "
1175 "and callee function \"%s\" at %s"),
1176 (msym_caller.minsym == NULL
1177 ? "???" : MSYMBOL_PRINT_NAME (msym_caller.minsym)),
1178 paddress (gdbarch, caller_pc),
1179 (msym_callee.minsym == NULL
1180 ? "???" : MSYMBOL_PRINT_NAME (msym_callee.minsym)),
1181 paddress (gdbarch, callee_pc));
1184 return retval.release ();
1187 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
1188 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
1189 constructed return NULL. Caller is responsible for xfree of the returned
1192 struct call_site_chain *
1193 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
1194 CORE_ADDR callee_pc)
1196 struct call_site_chain *retval = NULL;
1200 retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
1202 CATCH (e, RETURN_MASK_ERROR)
1204 if (e.error == NO_ENTRY_VALUE_ERROR)
1206 if (entry_values_debug)
1207 exception_print (gdb_stdout, e);
1212 throw_exception (e);
1219 /* Return 1 if KIND and KIND_U match PARAMETER. Return 0 otherwise. */
1222 call_site_parameter_matches (struct call_site_parameter *parameter,
1223 enum call_site_parameter_kind kind,
1224 union call_site_parameter_u kind_u)
1226 if (kind == parameter->kind)
1229 case CALL_SITE_PARAMETER_DWARF_REG:
1230 return kind_u.dwarf_reg == parameter->u.dwarf_reg;
1231 case CALL_SITE_PARAMETER_FB_OFFSET:
1232 return kind_u.fb_offset == parameter->u.fb_offset;
1233 case CALL_SITE_PARAMETER_PARAM_OFFSET:
1234 return kind_u.param_cu_off == parameter->u.param_cu_off;
1239 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
1240 FRAME is for callee.
1242 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
1245 static struct call_site_parameter *
1246 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
1247 enum call_site_parameter_kind kind,
1248 union call_site_parameter_u kind_u,
1249 struct dwarf2_per_cu_data **per_cu_return)
1251 CORE_ADDR func_addr, caller_pc;
1252 struct gdbarch *gdbarch;
1253 struct frame_info *caller_frame;
1254 struct call_site *call_site;
1256 /* Initialize it just to avoid a GCC false warning. */
1257 struct call_site_parameter *parameter = NULL;
1258 CORE_ADDR target_addr;
1260 while (get_frame_type (frame) == INLINE_FRAME)
1262 frame = get_prev_frame (frame);
1263 gdb_assert (frame != NULL);
1266 func_addr = get_frame_func (frame);
1267 gdbarch = get_frame_arch (frame);
1268 caller_frame = get_prev_frame (frame);
1269 if (gdbarch != frame_unwind_arch (frame))
1271 struct bound_minimal_symbol msym
1272 = lookup_minimal_symbol_by_pc (func_addr);
1273 struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
1275 throw_error (NO_ENTRY_VALUE_ERROR,
1276 _("DW_OP_entry_value resolving callee gdbarch %s "
1277 "(of %s (%s)) does not match caller gdbarch %s"),
1278 gdbarch_bfd_arch_info (gdbarch)->printable_name,
1279 paddress (gdbarch, func_addr),
1280 (msym.minsym == NULL ? "???"
1281 : MSYMBOL_PRINT_NAME (msym.minsym)),
1282 gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
1285 if (caller_frame == NULL)
1287 struct bound_minimal_symbol msym
1288 = lookup_minimal_symbol_by_pc (func_addr);
1290 throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_entry_value resolving "
1291 "requires caller of %s (%s)"),
1292 paddress (gdbarch, func_addr),
1293 (msym.minsym == NULL ? "???"
1294 : MSYMBOL_PRINT_NAME (msym.minsym)));
1296 caller_pc = get_frame_pc (caller_frame);
1297 call_site = call_site_for_pc (gdbarch, caller_pc);
1299 target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
1300 if (target_addr != func_addr)
1302 struct minimal_symbol *target_msym, *func_msym;
1304 target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
1305 func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
1306 throw_error (NO_ENTRY_VALUE_ERROR,
1307 _("DW_OP_entry_value resolving expects callee %s at %s "
1308 "but the called frame is for %s at %s"),
1309 (target_msym == NULL ? "???"
1310 : MSYMBOL_PRINT_NAME (target_msym)),
1311 paddress (gdbarch, target_addr),
1312 func_msym == NULL ? "???" : MSYMBOL_PRINT_NAME (func_msym),
1313 paddress (gdbarch, func_addr));
1316 /* No entry value based parameters would be reliable if this function can
1317 call itself via tail calls. */
1318 func_verify_no_selftailcall (gdbarch, func_addr);
1320 for (iparams = 0; iparams < call_site->parameter_count; iparams++)
1322 parameter = &call_site->parameter[iparams];
1323 if (call_site_parameter_matches (parameter, kind, kind_u))
1326 if (iparams == call_site->parameter_count)
1328 struct minimal_symbol *msym
1329 = lookup_minimal_symbol_by_pc (caller_pc).minsym;
1331 /* DW_TAG_call_site_parameter will be missing just if GCC could not
1332 determine its value. */
1333 throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
1334 "at DW_TAG_call_site %s at %s"),
1335 paddress (gdbarch, caller_pc),
1336 msym == NULL ? "???" : MSYMBOL_PRINT_NAME (msym));
1339 *per_cu_return = call_site->per_cu;
1343 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
1344 the normal DW_AT_call_value block. Otherwise return the
1345 DW_AT_call_data_value (dereferenced) block.
1347 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1350 Function always returns non-NULL, non-optimized out value. It throws
1351 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
1353 static struct value *
1354 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
1355 CORE_ADDR deref_size, struct type *type,
1356 struct frame_info *caller_frame,
1357 struct dwarf2_per_cu_data *per_cu)
1359 const gdb_byte *data_src;
1363 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1364 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1366 /* DEREF_SIZE size is not verified here. */
1367 if (data_src == NULL)
1368 throw_error (NO_ENTRY_VALUE_ERROR,
1369 _("Cannot resolve DW_AT_call_data_value"));
1371 /* DW_AT_call_value is a DWARF expression, not a DWARF
1372 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1374 data = (gdb_byte *) alloca (size + 1);
1375 memcpy (data, data_src, size);
1376 data[size] = DW_OP_stack_value;
1378 return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
1381 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1382 the indirect method on it, that is use its stored target value, the sole
1383 purpose of entry_data_value_funcs.. */
1385 static struct value *
1386 entry_data_value_coerce_ref (const struct value *value)
1388 struct type *checked_type = check_typedef (value_type (value));
1389 struct value *target_val;
1391 if (!TYPE_IS_REFERENCE (checked_type))
1394 target_val = (struct value *) value_computed_closure (value);
1395 value_incref (target_val);
1399 /* Implement copy_closure. */
1402 entry_data_value_copy_closure (const struct value *v)
1404 struct value *target_val = (struct value *) value_computed_closure (v);
1406 value_incref (target_val);
1410 /* Implement free_closure. */
1413 entry_data_value_free_closure (struct value *v)
1415 struct value *target_val = (struct value *) value_computed_closure (v);
1417 value_decref (target_val);
1420 /* Vector for methods for an entry value reference where the referenced value
1421 is stored in the caller. On the first dereference use
1422 DW_AT_call_data_value in the caller. */
1424 static const struct lval_funcs entry_data_value_funcs =
1428 NULL, /* indirect */
1429 entry_data_value_coerce_ref,
1430 NULL, /* check_synthetic_pointer */
1431 entry_data_value_copy_closure,
1432 entry_data_value_free_closure
1435 /* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KIND_U
1436 are used to match DW_AT_location at the caller's
1437 DW_TAG_call_site_parameter.
1439 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1440 cannot resolve the parameter for any reason. */
1442 static struct value *
1443 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1444 enum call_site_parameter_kind kind,
1445 union call_site_parameter_u kind_u)
1447 struct type *checked_type = check_typedef (type);
1448 struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1449 struct frame_info *caller_frame = get_prev_frame (frame);
1450 struct value *outer_val, *target_val, *val;
1451 struct call_site_parameter *parameter;
1452 struct dwarf2_per_cu_data *caller_per_cu;
1454 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1457 outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1461 /* Check if DW_AT_call_data_value cannot be used. If it should be
1462 used and it is not available do not fall back to OUTER_VAL - dereferencing
1463 TYPE_CODE_REF with non-entry data value would give current value - not the
1466 if (!TYPE_IS_REFERENCE (checked_type)
1467 || TYPE_TARGET_TYPE (checked_type) == NULL)
1470 target_val = dwarf_entry_parameter_to_value (parameter,
1471 TYPE_LENGTH (target_type),
1472 target_type, caller_frame,
1475 val = allocate_computed_value (type, &entry_data_value_funcs,
1476 release_value (target_val).release ());
1478 /* Copy the referencing pointer to the new computed value. */
1479 memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1480 TYPE_LENGTH (checked_type));
1481 set_value_lazy (val, 0);
1486 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1487 SIZE are DWARF block used to match DW_AT_location at the caller's
1488 DW_TAG_call_site_parameter.
1490 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1491 cannot resolve the parameter for any reason. */
1493 static struct value *
1494 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1495 const gdb_byte *block, size_t block_len)
1497 union call_site_parameter_u kind_u;
1499 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1500 if (kind_u.dwarf_reg != -1)
1501 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
1504 if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
1505 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
1508 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1509 suppressed during normal operation. The expression can be arbitrary if
1510 there is no caller-callee entry value binding expected. */
1511 throw_error (NO_ENTRY_VALUE_ERROR,
1512 _("DWARF-2 expression error: DW_OP_entry_value is supported "
1513 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1516 struct piece_closure
1518 /* Reference count. */
1521 /* The CU from which this closure's expression came. */
1522 struct dwarf2_per_cu_data *per_cu = NULL;
1524 /* The pieces describing this variable. */
1525 std::vector<dwarf_expr_piece> pieces;
1527 /* Frame ID of frame to which a register value is relative, used
1528 only by DWARF_VALUE_REGISTER. */
1529 struct frame_id frame_id;
1532 /* Allocate a closure for a value formed from separately-described
1535 static struct piece_closure *
1536 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1537 std::vector<dwarf_expr_piece> &&pieces,
1538 struct frame_info *frame)
1540 struct piece_closure *c = new piece_closure;
1544 c->pieces = std::move (pieces);
1546 c->frame_id = null_frame_id;
1548 c->frame_id = get_frame_id (frame);
1550 for (dwarf_expr_piece &piece : c->pieces)
1551 if (piece.location == DWARF_VALUE_STACK)
1552 value_incref (piece.v.value);
1557 /* Return the number of bytes overlapping a contiguous chunk of N_BITS
1558 bits whose first bit is located at bit offset START. */
1561 bits_to_bytes (ULONGEST start, ULONGEST n_bits)
1563 return (start % 8 + n_bits + 7) / 8;
1566 /* Read or write a pieced value V. If FROM != NULL, operate in "write
1567 mode": copy FROM into the pieces comprising V. If FROM == NULL,
1568 operate in "read mode": fetch the contents of the (lazy) value V by
1569 composing it from its pieces. */
1572 rw_pieced_value (struct value *v, struct value *from)
1575 LONGEST offset = 0, max_offset;
1576 ULONGEST bits_to_skip;
1577 gdb_byte *v_contents;
1578 const gdb_byte *from_contents;
1579 struct piece_closure *c
1580 = (struct piece_closure *) value_computed_closure (v);
1581 gdb::byte_vector buffer;
1583 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1587 from_contents = value_contents (from);
1592 if (value_type (v) != value_enclosing_type (v))
1593 internal_error (__FILE__, __LINE__,
1594 _("Should not be able to create a lazy value with "
1595 "an enclosing type"));
1596 v_contents = value_contents_raw (v);
1597 from_contents = NULL;
1600 bits_to_skip = 8 * value_offset (v);
1601 if (value_bitsize (v))
1603 bits_to_skip += (8 * value_offset (value_parent (v))
1604 + value_bitpos (v));
1606 && (gdbarch_byte_order (get_type_arch (value_type (from)))
1609 /* Use the least significant bits of FROM. */
1610 max_offset = 8 * TYPE_LENGTH (value_type (from));
1611 offset = max_offset - value_bitsize (v);
1614 max_offset = value_bitsize (v);
1617 max_offset = 8 * TYPE_LENGTH (value_type (v));
1619 /* Advance to the first non-skipped piece. */
1620 for (i = 0; i < c->pieces.size () && bits_to_skip >= c->pieces[i].size; i++)
1621 bits_to_skip -= c->pieces[i].size;
1623 for (; i < c->pieces.size () && offset < max_offset; i++)
1625 struct dwarf_expr_piece *p = &c->pieces[i];
1626 size_t this_size_bits, this_size;
1628 this_size_bits = p->size - bits_to_skip;
1629 if (this_size_bits > max_offset - offset)
1630 this_size_bits = max_offset - offset;
1632 switch (p->location)
1634 case DWARF_VALUE_REGISTER:
1636 struct frame_info *frame = frame_find_by_id (c->frame_id);
1637 struct gdbarch *arch = get_frame_arch (frame);
1638 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1639 ULONGEST reg_bits = 8 * register_size (arch, gdb_regnum);
1642 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1643 && p->offset + p->size < reg_bits)
1645 /* Big-endian, and we want less than full size. */
1646 bits_to_skip += reg_bits - (p->offset + p->size);
1649 bits_to_skip += p->offset;
1651 this_size = bits_to_bytes (bits_to_skip, this_size_bits);
1652 buffer.resize (this_size);
1657 if (!get_frame_register_bytes (frame, gdb_regnum,
1659 this_size, buffer.data (),
1663 mark_value_bits_optimized_out (v, offset,
1666 mark_value_bits_unavailable (v, offset,
1671 copy_bitwise (v_contents, offset,
1672 buffer.data (), bits_to_skip % 8,
1673 this_size_bits, bits_big_endian);
1678 if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
1680 /* Data is copied non-byte-aligned into the register.
1681 Need some bits from original register value. */
1682 get_frame_register_bytes (frame, gdb_regnum,
1684 this_size, buffer.data (),
1687 throw_error (OPTIMIZED_OUT_ERROR,
1688 _("Can't do read-modify-write to "
1689 "update bitfield; containing word "
1690 "has been optimized out"));
1692 throw_error (NOT_AVAILABLE_ERROR,
1693 _("Can't do read-modify-write to "
1694 "update bitfield; containing word "
1698 copy_bitwise (buffer.data (), bits_to_skip % 8,
1699 from_contents, offset,
1700 this_size_bits, bits_big_endian);
1701 put_frame_register_bytes (frame, gdb_regnum,
1703 this_size, buffer.data ());
1708 case DWARF_VALUE_MEMORY:
1710 bits_to_skip += p->offset;
1712 CORE_ADDR start_addr = p->v.mem.addr + bits_to_skip / 8;
1714 if (bits_to_skip % 8 == 0 && this_size_bits % 8 == 0
1717 /* Everything is byte-aligned; no buffer needed. */
1719 write_memory_with_notification (start_addr,
1722 this_size_bits / 8);
1724 read_value_memory (v, offset,
1725 p->v.mem.in_stack_memory,
1726 p->v.mem.addr + bits_to_skip / 8,
1727 v_contents + offset / 8,
1728 this_size_bits / 8);
1732 this_size = bits_to_bytes (bits_to_skip, this_size_bits);
1733 buffer.resize (this_size);
1738 read_value_memory (v, offset,
1739 p->v.mem.in_stack_memory,
1740 p->v.mem.addr + bits_to_skip / 8,
1741 buffer.data (), this_size);
1742 copy_bitwise (v_contents, offset,
1743 buffer.data (), bits_to_skip % 8,
1744 this_size_bits, bits_big_endian);
1749 if (bits_to_skip % 8 != 0 || this_size_bits % 8 != 0)
1753 /* Perform a single read for small sizes. */
1754 read_memory (start_addr, buffer.data (),
1759 /* Only the first and last bytes can possibly have
1761 read_memory (start_addr, buffer.data (), 1);
1762 read_memory (start_addr + this_size - 1,
1763 &buffer[this_size - 1], 1);
1767 copy_bitwise (buffer.data (), bits_to_skip % 8,
1768 from_contents, offset,
1769 this_size_bits, bits_big_endian);
1770 write_memory_with_notification (start_addr,
1777 case DWARF_VALUE_STACK:
1781 mark_value_bits_optimized_out (v, offset, this_size_bits);
1785 struct objfile *objfile = dwarf2_per_cu_objfile (c->per_cu);
1786 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
1787 ULONGEST stack_value_size_bits
1788 = 8 * TYPE_LENGTH (value_type (p->v.value));
1790 /* Use zeroes if piece reaches beyond stack value. */
1791 if (p->offset + p->size > stack_value_size_bits)
1794 /* Piece is anchored at least significant bit end. */
1795 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
1796 bits_to_skip += stack_value_size_bits - p->offset - p->size;
1798 bits_to_skip += p->offset;
1800 copy_bitwise (v_contents, offset,
1801 value_contents_all (p->v.value),
1803 this_size_bits, bits_big_endian);
1807 case DWARF_VALUE_LITERAL:
1811 mark_value_bits_optimized_out (v, offset, this_size_bits);
1815 ULONGEST literal_size_bits = 8 * p->v.literal.length;
1816 size_t n = this_size_bits;
1818 /* Cut off at the end of the implicit value. */
1819 bits_to_skip += p->offset;
1820 if (bits_to_skip >= literal_size_bits)
1822 if (n > literal_size_bits - bits_to_skip)
1823 n = literal_size_bits - bits_to_skip;
1825 copy_bitwise (v_contents, offset,
1826 p->v.literal.data, bits_to_skip,
1827 n, bits_big_endian);
1831 case DWARF_VALUE_IMPLICIT_POINTER:
1834 mark_value_bits_optimized_out (v, offset, this_size_bits);
1838 /* These bits show up as zeros -- but do not cause the value to
1839 be considered optimized-out. */
1842 case DWARF_VALUE_OPTIMIZED_OUT:
1843 mark_value_bits_optimized_out (v, offset, this_size_bits);
1847 internal_error (__FILE__, __LINE__, _("invalid location type"));
1850 offset += this_size_bits;
1857 read_pieced_value (struct value *v)
1859 rw_pieced_value (v, NULL);
1863 write_pieced_value (struct value *to, struct value *from)
1865 rw_pieced_value (to, from);
1868 /* An implementation of an lval_funcs method to see whether a value is
1869 a synthetic pointer. */
1872 check_pieced_synthetic_pointer (const struct value *value, LONGEST bit_offset,
1875 struct piece_closure *c
1876 = (struct piece_closure *) value_computed_closure (value);
1879 bit_offset += 8 * value_offset (value);
1880 if (value_bitsize (value))
1881 bit_offset += value_bitpos (value);
1883 for (i = 0; i < c->pieces.size () && bit_length > 0; i++)
1885 struct dwarf_expr_piece *p = &c->pieces[i];
1886 size_t this_size_bits = p->size;
1890 if (bit_offset >= this_size_bits)
1892 bit_offset -= this_size_bits;
1896 bit_length -= this_size_bits - bit_offset;
1900 bit_length -= this_size_bits;
1902 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1909 /* A wrapper function for get_frame_address_in_block. */
1912 get_frame_address_in_block_wrapper (void *baton)
1914 return get_frame_address_in_block ((struct frame_info *) baton);
1917 /* Fetch a DW_AT_const_value through a synthetic pointer. */
1919 static struct value *
1920 fetch_const_value_from_synthetic_pointer (sect_offset die, LONGEST byte_offset,
1921 struct dwarf2_per_cu_data *per_cu,
1924 struct value *result = NULL;
1925 const gdb_byte *bytes;
1928 auto_obstack temp_obstack;
1929 bytes = dwarf2_fetch_constant_bytes (die, per_cu, &temp_obstack, &len);
1933 if (byte_offset >= 0
1934 && byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) <= len)
1936 bytes += byte_offset;
1937 result = value_from_contents (TYPE_TARGET_TYPE (type), bytes);
1940 invalid_synthetic_pointer ();
1943 result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type));
1948 /* Fetch the value pointed to by a synthetic pointer. */
1950 static struct value *
1951 indirect_synthetic_pointer (sect_offset die, LONGEST byte_offset,
1952 struct dwarf2_per_cu_data *per_cu,
1953 struct frame_info *frame, struct type *type,
1954 bool resolve_abstract_p)
1956 /* Fetch the location expression of the DIE we're pointing to. */
1957 struct dwarf2_locexpr_baton baton
1958 = dwarf2_fetch_die_loc_sect_off (die, per_cu,
1959 get_frame_address_in_block_wrapper, frame,
1960 resolve_abstract_p);
1962 /* Get type of pointed-to DIE. */
1963 struct type *orig_type = dwarf2_fetch_die_type_sect_off (die, per_cu);
1964 if (orig_type == NULL)
1965 invalid_synthetic_pointer ();
1967 /* If pointed-to DIE has a DW_AT_location, evaluate it and return the
1968 resulting value. Otherwise, it may have a DW_AT_const_value instead,
1969 or it may've been optimized out. */
1970 if (baton.data != NULL)
1971 return dwarf2_evaluate_loc_desc_full (orig_type, frame, baton.data,
1972 baton.size, baton.per_cu,
1973 TYPE_TARGET_TYPE (type),
1976 return fetch_const_value_from_synthetic_pointer (die, byte_offset, per_cu,
1980 /* An implementation of an lval_funcs method to indirect through a
1981 pointer. This handles the synthetic pointer case when needed. */
1983 static struct value *
1984 indirect_pieced_value (struct value *value)
1986 struct piece_closure *c
1987 = (struct piece_closure *) value_computed_closure (value);
1989 struct frame_info *frame;
1992 struct dwarf_expr_piece *piece = NULL;
1993 LONGEST byte_offset;
1994 enum bfd_endian byte_order;
1996 type = check_typedef (value_type (value));
1997 if (TYPE_CODE (type) != TYPE_CODE_PTR)
2000 bit_length = 8 * TYPE_LENGTH (type);
2001 bit_offset = 8 * value_offset (value);
2002 if (value_bitsize (value))
2003 bit_offset += value_bitpos (value);
2005 for (i = 0; i < c->pieces.size () && bit_length > 0; i++)
2007 struct dwarf_expr_piece *p = &c->pieces[i];
2008 size_t this_size_bits = p->size;
2012 if (bit_offset >= this_size_bits)
2014 bit_offset -= this_size_bits;
2018 bit_length -= this_size_bits - bit_offset;
2022 bit_length -= this_size_bits;
2024 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2027 if (bit_length != 0)
2028 error (_("Invalid use of DW_OP_implicit_pointer"));
2034 gdb_assert (piece != NULL);
2035 frame = get_selected_frame (_("No frame selected."));
2037 /* This is an offset requested by GDB, such as value subscripts.
2038 However, due to how synthetic pointers are implemented, this is
2039 always presented to us as a pointer type. This means we have to
2040 sign-extend it manually as appropriate. Use raw
2041 extract_signed_integer directly rather than value_as_address and
2042 sign extend afterwards on architectures that would need it
2043 (mostly everywhere except MIPS, which has signed addresses) as
2044 the later would go through gdbarch_pointer_to_address and thus
2045 return a CORE_ADDR with high bits set on architectures that
2046 encode address spaces and other things in CORE_ADDR. */
2047 byte_order = gdbarch_byte_order (get_frame_arch (frame));
2048 byte_offset = extract_signed_integer (value_contents (value),
2049 TYPE_LENGTH (type), byte_order);
2050 byte_offset += piece->v.ptr.offset;
2052 return indirect_synthetic_pointer (piece->v.ptr.die_sect_off,
2053 byte_offset, c->per_cu,
2057 /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
2060 static struct value *
2061 coerce_pieced_ref (const struct value *value)
2063 struct type *type = check_typedef (value_type (value));
2065 if (value_bits_synthetic_pointer (value, value_embedded_offset (value),
2066 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
2068 const struct piece_closure *closure
2069 = (struct piece_closure *) value_computed_closure (value);
2070 struct frame_info *frame
2071 = get_selected_frame (_("No frame selected."));
2073 /* gdb represents synthetic pointers as pieced values with a single
2075 gdb_assert (closure != NULL);
2076 gdb_assert (closure->pieces.size () == 1);
2078 return indirect_synthetic_pointer
2079 (closure->pieces[0].v.ptr.die_sect_off,
2080 closure->pieces[0].v.ptr.offset,
2081 closure->per_cu, frame, type);
2085 /* Else: not a synthetic reference; do nothing. */
2091 copy_pieced_value_closure (const struct value *v)
2093 struct piece_closure *c
2094 = (struct piece_closure *) value_computed_closure (v);
2101 free_pieced_value_closure (struct value *v)
2103 struct piece_closure *c
2104 = (struct piece_closure *) value_computed_closure (v);
2109 for (dwarf_expr_piece &p : c->pieces)
2110 if (p.location == DWARF_VALUE_STACK)
2111 value_decref (p.v.value);
2117 /* Functions for accessing a variable described by DW_OP_piece. */
2118 static const struct lval_funcs pieced_value_funcs = {
2121 indirect_pieced_value,
2123 check_pieced_synthetic_pointer,
2124 copy_pieced_value_closure,
2125 free_pieced_value_closure
2128 /* Evaluate a location description, starting at DATA and with length
2129 SIZE, to find the current location of variable of TYPE in the
2130 context of FRAME. If SUBOBJ_TYPE is non-NULL, return instead the
2131 location of the subobject of type SUBOBJ_TYPE at byte offset
2132 SUBOBJ_BYTE_OFFSET within the variable of type TYPE. */
2134 static struct value *
2135 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2136 const gdb_byte *data, size_t size,
2137 struct dwarf2_per_cu_data *per_cu,
2138 struct type *subobj_type,
2139 LONGEST subobj_byte_offset)
2141 struct value *retval;
2142 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2144 if (subobj_type == NULL)
2147 subobj_byte_offset = 0;
2149 else if (subobj_byte_offset < 0)
2150 invalid_synthetic_pointer ();
2153 return allocate_optimized_out_value (subobj_type);
2155 dwarf_evaluate_loc_desc ctx;
2157 ctx.per_cu = per_cu;
2158 ctx.obj_address = 0;
2160 scoped_value_mark free_values;
2162 ctx.gdbarch = get_objfile_arch (objfile);
2163 ctx.addr_size = dwarf2_per_cu_addr_size (per_cu);
2164 ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2165 ctx.offset = dwarf2_per_cu_text_offset (per_cu);
2169 ctx.eval (data, size);
2171 CATCH (ex, RETURN_MASK_ERROR)
2173 if (ex.error == NOT_AVAILABLE_ERROR)
2175 free_values.free_to_mark ();
2176 retval = allocate_value (subobj_type);
2177 mark_value_bytes_unavailable (retval, 0,
2178 TYPE_LENGTH (subobj_type));
2181 else if (ex.error == NO_ENTRY_VALUE_ERROR)
2183 if (entry_values_debug)
2184 exception_print (gdb_stdout, ex);
2185 free_values.free_to_mark ();
2186 return allocate_optimized_out_value (subobj_type);
2189 throw_exception (ex);
2193 if (ctx.pieces.size () > 0)
2195 struct piece_closure *c;
2196 ULONGEST bit_size = 0;
2198 for (dwarf_expr_piece &piece : ctx.pieces)
2199 bit_size += piece.size;
2200 /* Complain if the expression is larger than the size of the
2202 if (bit_size > 8 * TYPE_LENGTH (type))
2203 invalid_synthetic_pointer ();
2205 c = allocate_piece_closure (per_cu, std::move (ctx.pieces), frame);
2206 /* We must clean up the value chain after creating the piece
2207 closure but before allocating the result. */
2208 free_values.free_to_mark ();
2209 retval = allocate_computed_value (subobj_type,
2210 &pieced_value_funcs, c);
2211 set_value_offset (retval, subobj_byte_offset);
2215 switch (ctx.location)
2217 case DWARF_VALUE_REGISTER:
2219 struct gdbarch *arch = get_frame_arch (frame);
2221 = longest_to_int (value_as_long (ctx.fetch (0)));
2222 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, dwarf_regnum);
2224 if (subobj_byte_offset != 0)
2225 error (_("cannot use offset on synthetic pointer to register"));
2226 free_values.free_to_mark ();
2227 retval = value_from_register (subobj_type, gdb_regnum, frame);
2228 if (value_optimized_out (retval))
2232 /* This means the register has undefined value / was
2233 not saved. As we're computing the location of some
2234 variable etc. in the program, not a value for
2235 inspecting a register ($pc, $sp, etc.), return a
2236 generic optimized out value instead, so that we show
2237 <optimized out> instead of <not saved>. */
2238 tmp = allocate_value (subobj_type);
2239 value_contents_copy (tmp, 0, retval, 0,
2240 TYPE_LENGTH (subobj_type));
2246 case DWARF_VALUE_MEMORY:
2248 struct type *ptr_type;
2249 CORE_ADDR address = ctx.fetch_address (0);
2250 bool in_stack_memory = ctx.fetch_in_stack_memory (0);
2252 /* DW_OP_deref_size (and possibly other operations too) may
2253 create a pointer instead of an address. Ideally, the
2254 pointer to address conversion would be performed as part
2255 of those operations, but the type of the object to
2256 which the address refers is not known at the time of
2257 the operation. Therefore, we do the conversion here
2258 since the type is readily available. */
2260 switch (TYPE_CODE (subobj_type))
2262 case TYPE_CODE_FUNC:
2263 case TYPE_CODE_METHOD:
2264 ptr_type = builtin_type (ctx.gdbarch)->builtin_func_ptr;
2267 ptr_type = builtin_type (ctx.gdbarch)->builtin_data_ptr;
2270 address = value_as_address (value_from_pointer (ptr_type, address));
2272 free_values.free_to_mark ();
2273 retval = value_at_lazy (subobj_type,
2274 address + subobj_byte_offset);
2275 if (in_stack_memory)
2276 set_value_stack (retval, 1);
2280 case DWARF_VALUE_STACK:
2282 struct value *value = ctx.fetch (0);
2283 size_t n = TYPE_LENGTH (value_type (value));
2284 size_t len = TYPE_LENGTH (subobj_type);
2285 size_t max = TYPE_LENGTH (type);
2286 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2288 if (subobj_byte_offset + len > max)
2289 invalid_synthetic_pointer ();
2291 /* Preserve VALUE because we are going to free values back
2292 to the mark, but we still need the value contents
2294 value_ref_ptr value_holder = value_ref_ptr::new_reference (value);
2295 free_values.free_to_mark ();
2297 retval = allocate_value (subobj_type);
2299 /* The given offset is relative to the actual object. */
2300 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2301 subobj_byte_offset += n - max;
2303 memcpy (value_contents_raw (retval),
2304 value_contents_all (value) + subobj_byte_offset, len);
2308 case DWARF_VALUE_LITERAL:
2311 size_t n = TYPE_LENGTH (subobj_type);
2313 if (subobj_byte_offset + n > ctx.len)
2314 invalid_synthetic_pointer ();
2316 free_values.free_to_mark ();
2317 retval = allocate_value (subobj_type);
2318 contents = value_contents_raw (retval);
2319 memcpy (contents, ctx.data + subobj_byte_offset, n);
2323 case DWARF_VALUE_OPTIMIZED_OUT:
2324 free_values.free_to_mark ();
2325 retval = allocate_optimized_out_value (subobj_type);
2328 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2329 operation by execute_stack_op. */
2330 case DWARF_VALUE_IMPLICIT_POINTER:
2331 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2332 it can only be encountered when making a piece. */
2334 internal_error (__FILE__, __LINE__, _("invalid location type"));
2338 set_value_initialized (retval, ctx.initialized);
2343 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2344 passes 0 as the byte_offset. */
2347 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2348 const gdb_byte *data, size_t size,
2349 struct dwarf2_per_cu_data *per_cu)
2351 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu,
2355 /* Evaluates a dwarf expression and stores the result in VAL, expecting
2356 that the dwarf expression only produces a single CORE_ADDR. FRAME is the
2357 frame in which the expression is evaluated. ADDR is a context (location of
2358 a variable) and might be needed to evaluate the location expression.
2359 Returns 1 on success, 0 otherwise. */
2362 dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton *dlbaton,
2363 struct frame_info *frame,
2367 struct objfile *objfile;
2369 if (dlbaton == NULL || dlbaton->size == 0)
2372 dwarf_evaluate_loc_desc ctx;
2375 ctx.per_cu = dlbaton->per_cu;
2376 ctx.obj_address = addr;
2378 objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2380 ctx.gdbarch = get_objfile_arch (objfile);
2381 ctx.addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2382 ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (dlbaton->per_cu);
2383 ctx.offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
2387 ctx.eval (dlbaton->data, dlbaton->size);
2389 CATCH (ex, RETURN_MASK_ERROR)
2391 if (ex.error == NOT_AVAILABLE_ERROR)
2395 else if (ex.error == NO_ENTRY_VALUE_ERROR)
2397 if (entry_values_debug)
2398 exception_print (gdb_stdout, ex);
2402 throw_exception (ex);
2406 switch (ctx.location)
2408 case DWARF_VALUE_REGISTER:
2409 case DWARF_VALUE_MEMORY:
2410 case DWARF_VALUE_STACK:
2411 *valp = ctx.fetch_address (0);
2412 if (ctx.location == DWARF_VALUE_REGISTER)
2413 *valp = ctx.read_addr_from_reg (*valp);
2415 case DWARF_VALUE_LITERAL:
2416 *valp = extract_signed_integer (ctx.data, ctx.len,
2417 gdbarch_byte_order (ctx.gdbarch));
2419 /* Unsupported dwarf values. */
2420 case DWARF_VALUE_OPTIMIZED_OUT:
2421 case DWARF_VALUE_IMPLICIT_POINTER:
2428 /* See dwarf2loc.h. */
2431 dwarf2_evaluate_property (const struct dynamic_prop *prop,
2432 struct frame_info *frame,
2433 struct property_addr_info *addr_stack,
2439 if (frame == NULL && has_stack_frames ())
2440 frame = get_selected_frame (NULL);
2446 const struct dwarf2_property_baton *baton
2447 = (const struct dwarf2_property_baton *) prop->data.baton;
2449 if (dwarf2_locexpr_baton_eval (&baton->locexpr, frame,
2450 addr_stack ? addr_stack->addr : 0,
2453 if (baton->referenced_type)
2455 struct value *val = value_at (baton->referenced_type, *value);
2457 *value = value_as_address (val);
2466 struct dwarf2_property_baton *baton
2467 = (struct dwarf2_property_baton *) prop->data.baton;
2468 CORE_ADDR pc = get_frame_address_in_block (frame);
2469 const gdb_byte *data;
2473 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2476 val = dwarf2_evaluate_loc_desc (baton->referenced_type, frame, data,
2477 size, baton->loclist.per_cu);
2478 if (!value_optimized_out (val))
2480 *value = value_as_address (val);
2488 *value = prop->data.const_val;
2491 case PROP_ADDR_OFFSET:
2493 struct dwarf2_property_baton *baton
2494 = (struct dwarf2_property_baton *) prop->data.baton;
2495 struct property_addr_info *pinfo;
2498 for (pinfo = addr_stack; pinfo != NULL; pinfo = pinfo->next)
2499 if (pinfo->type == baton->referenced_type)
2502 error (_("cannot find reference address for offset property"));
2503 if (pinfo->valaddr != NULL)
2504 val = value_from_contents
2505 (baton->offset_info.type,
2506 pinfo->valaddr + baton->offset_info.offset);
2508 val = value_at (baton->offset_info.type,
2509 pinfo->addr + baton->offset_info.offset);
2510 *value = value_as_address (val);
2518 /* See dwarf2loc.h. */
2521 dwarf2_compile_property_to_c (string_file *stream,
2522 const char *result_name,
2523 struct gdbarch *gdbarch,
2524 unsigned char *registers_used,
2525 const struct dynamic_prop *prop,
2529 struct dwarf2_property_baton *baton
2530 = (struct dwarf2_property_baton *) prop->data.baton;
2531 const gdb_byte *data;
2533 struct dwarf2_per_cu_data *per_cu;
2535 if (prop->kind == PROP_LOCEXPR)
2537 data = baton->locexpr.data;
2538 size = baton->locexpr.size;
2539 per_cu = baton->locexpr.per_cu;
2543 gdb_assert (prop->kind == PROP_LOCLIST);
2545 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2546 per_cu = baton->loclist.per_cu;
2549 compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
2550 gdbarch, registers_used,
2551 dwarf2_per_cu_addr_size (per_cu),
2552 data, data + size, per_cu);
2556 /* Helper functions and baton for dwarf2_loc_desc_get_symbol_read_needs. */
2558 class symbol_needs_eval_context : public dwarf_expr_context
2562 enum symbol_needs_kind needs;
2563 struct dwarf2_per_cu_data *per_cu;
2565 /* Reads from registers do require a frame. */
2566 CORE_ADDR read_addr_from_reg (int regnum) override
2568 needs = SYMBOL_NEEDS_FRAME;
2572 /* "get_reg_value" callback: Reads from registers do require a
2575 struct value *get_reg_value (struct type *type, int regnum) override
2577 needs = SYMBOL_NEEDS_FRAME;
2578 return value_zero (type, not_lval);
2581 /* Reads from memory do not require a frame. */
2582 void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t len) override
2584 memset (buf, 0, len);
2587 /* Frame-relative accesses do require a frame. */
2588 void get_frame_base (const gdb_byte **start, size_t *length) override
2590 static gdb_byte lit0 = DW_OP_lit0;
2595 needs = SYMBOL_NEEDS_FRAME;
2598 /* CFA accesses require a frame. */
2599 CORE_ADDR get_frame_cfa () override
2601 needs = SYMBOL_NEEDS_FRAME;
2605 CORE_ADDR get_frame_pc () override
2607 needs = SYMBOL_NEEDS_FRAME;
2611 /* Thread-local accesses require registers, but not a frame. */
2612 CORE_ADDR get_tls_address (CORE_ADDR offset) override
2614 if (needs <= SYMBOL_NEEDS_REGISTERS)
2615 needs = SYMBOL_NEEDS_REGISTERS;
2619 /* Helper interface of per_cu_dwarf_call for
2620 dwarf2_loc_desc_get_symbol_read_needs. */
2622 void dwarf_call (cu_offset die_offset) override
2624 per_cu_dwarf_call (this, die_offset, per_cu);
2627 /* Helper interface of sect_variable_value for
2628 dwarf2_loc_desc_get_symbol_read_needs. */
2630 struct value *dwarf_variable_value (sect_offset sect_off) override
2632 return sect_variable_value (this, sect_off, per_cu);
2635 /* DW_OP_entry_value accesses require a caller, therefore a
2638 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind,
2639 union call_site_parameter_u kind_u,
2640 int deref_size) override
2642 needs = SYMBOL_NEEDS_FRAME;
2644 /* The expression may require some stub values on DWARF stack. */
2645 push_address (0, 0);
2648 /* DW_OP_GNU_addr_index doesn't require a frame. */
2650 CORE_ADDR get_addr_index (unsigned int index) override
2652 /* Nothing to do. */
2656 /* DW_OP_push_object_address has a frame already passed through. */
2658 CORE_ADDR get_object_address () override
2660 /* Nothing to do. */
2665 /* Compute the correct symbol_needs_kind value for the location
2666 expression at DATA (length SIZE). */
2668 static enum symbol_needs_kind
2669 dwarf2_loc_desc_get_symbol_read_needs (const gdb_byte *data, size_t size,
2670 struct dwarf2_per_cu_data *per_cu)
2673 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2675 scoped_value_mark free_values;
2677 symbol_needs_eval_context ctx;
2679 ctx.needs = SYMBOL_NEEDS_NONE;
2680 ctx.per_cu = per_cu;
2681 ctx.gdbarch = get_objfile_arch (objfile);
2682 ctx.addr_size = dwarf2_per_cu_addr_size (per_cu);
2683 ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2684 ctx.offset = dwarf2_per_cu_text_offset (per_cu);
2686 ctx.eval (data, size);
2688 in_reg = ctx.location == DWARF_VALUE_REGISTER;
2690 /* If the location has several pieces, and any of them are in
2691 registers, then we will need a frame to fetch them from. */
2692 for (dwarf_expr_piece &p : ctx.pieces)
2693 if (p.location == DWARF_VALUE_REGISTER)
2697 ctx.needs = SYMBOL_NEEDS_FRAME;
2701 /* A helper function that throws an unimplemented error mentioning a
2702 given DWARF operator. */
2704 static void ATTRIBUTE_NORETURN
2705 unimplemented (unsigned int op)
2707 const char *name = get_DW_OP_name (op);
2710 error (_("DWARF operator %s cannot be translated to an agent expression"),
2713 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2714 "to an agent expression"),
2720 This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2721 can issue a complaint, which is better than having every target's
2722 implementation of dwarf2_reg_to_regnum do it. */
2725 dwarf_reg_to_regnum (struct gdbarch *arch, int dwarf_reg)
2727 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2731 complaint (_("bad DWARF register number %d"), dwarf_reg);
2736 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2737 Throw an error because DWARF_REG is bad. */
2740 throw_bad_regnum_error (ULONGEST dwarf_reg)
2742 /* Still want to print -1 as "-1".
2743 We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2744 but that's overkill for now. */
2745 if ((int) dwarf_reg == dwarf_reg)
2746 error (_("Unable to access DWARF register number %d"), (int) dwarf_reg);
2747 error (_("Unable to access DWARF register number %s"),
2748 pulongest (dwarf_reg));
2751 /* See dwarf2loc.h. */
2754 dwarf_reg_to_regnum_or_error (struct gdbarch *arch, ULONGEST dwarf_reg)
2758 if (dwarf_reg > INT_MAX)
2759 throw_bad_regnum_error (dwarf_reg);
2760 /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2761 bad, but that's ok. */
2762 reg = dwarf_reg_to_regnum (arch, (int) dwarf_reg);
2764 throw_bad_regnum_error (dwarf_reg);
2768 /* A helper function that emits an access to memory. ARCH is the
2769 target architecture. EXPR is the expression which we are building.
2770 NBITS is the number of bits we want to read. This emits the
2771 opcodes needed to read the memory and then extract the desired
2775 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2777 ULONGEST nbytes = (nbits + 7) / 8;
2779 gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
2782 ax_trace_quick (expr, nbytes);
2785 ax_simple (expr, aop_ref8);
2786 else if (nbits <= 16)
2787 ax_simple (expr, aop_ref16);
2788 else if (nbits <= 32)
2789 ax_simple (expr, aop_ref32);
2791 ax_simple (expr, aop_ref64);
2793 /* If we read exactly the number of bytes we wanted, we're done. */
2794 if (8 * nbytes == nbits)
2797 if (gdbarch_bits_big_endian (arch))
2799 /* On a bits-big-endian machine, we want the high-order
2801 ax_const_l (expr, 8 * nbytes - nbits);
2802 ax_simple (expr, aop_rsh_unsigned);
2806 /* On a bits-little-endian box, we want the low-order NBITS. */
2807 ax_zero_ext (expr, nbits);
2811 /* A helper function to return the frame's PC. */
2814 get_ax_pc (void *baton)
2816 struct agent_expr *expr = (struct agent_expr *) baton;
2821 /* Compile a DWARF location expression to an agent expression.
2823 EXPR is the agent expression we are building.
2824 LOC is the agent value we modify.
2825 ARCH is the architecture.
2826 ADDR_SIZE is the size of addresses, in bytes.
2827 OP_PTR is the start of the location expression.
2828 OP_END is one past the last byte of the location expression.
2830 This will throw an exception for various kinds of errors -- for
2831 example, if the expression cannot be compiled, or if the expression
2835 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
2836 unsigned int addr_size, const gdb_byte *op_ptr,
2837 const gdb_byte *op_end,
2838 struct dwarf2_per_cu_data *per_cu)
2840 gdbarch *arch = expr->gdbarch;
2841 std::vector<int> dw_labels, patches;
2842 const gdb_byte * const base = op_ptr;
2843 const gdb_byte *previous_piece = op_ptr;
2844 enum bfd_endian byte_order = gdbarch_byte_order (arch);
2845 ULONGEST bits_collected = 0;
2846 unsigned int addr_size_bits = 8 * addr_size;
2847 int bits_big_endian = gdbarch_bits_big_endian (arch);
2849 std::vector<int> offsets (op_end - op_ptr, -1);
2851 /* By default we are making an address. */
2852 loc->kind = axs_lvalue_memory;
2854 while (op_ptr < op_end)
2856 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
2857 uint64_t uoffset, reg;
2861 offsets[op_ptr - base] = expr->len;
2864 /* Our basic approach to code generation is to map DWARF
2865 operations directly to AX operations. However, there are
2868 First, DWARF works on address-sized units, but AX always uses
2869 LONGEST. For most operations we simply ignore this
2870 difference; instead we generate sign extensions as needed
2871 before division and comparison operations. It would be nice
2872 to omit the sign extensions, but there is no way to determine
2873 the size of the target's LONGEST. (This code uses the size
2874 of the host LONGEST in some cases -- that is a bug but it is
2877 Second, some DWARF operations cannot be translated to AX.
2878 For these we simply fail. See
2879 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
2914 ax_const_l (expr, op - DW_OP_lit0);
2918 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
2919 op_ptr += addr_size;
2920 /* Some versions of GCC emit DW_OP_addr before
2921 DW_OP_GNU_push_tls_address. In this case the value is an
2922 index, not an address. We don't support things like
2923 branching between the address and the TLS op. */
2924 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
2925 uoffset += dwarf2_per_cu_text_offset (per_cu);
2926 ax_const_l (expr, uoffset);
2930 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
2934 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
2938 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
2942 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
2946 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
2950 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
2954 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
2958 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
2962 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
2963 ax_const_l (expr, uoffset);
2966 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2967 ax_const_l (expr, offset);
3002 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3003 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
3004 loc->kind = axs_lvalue_register;
3008 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3009 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3010 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, reg);
3011 loc->kind = axs_lvalue_register;
3014 case DW_OP_implicit_value:
3018 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
3019 if (op_ptr + len > op_end)
3020 error (_("DW_OP_implicit_value: too few bytes available."));
3021 if (len > sizeof (ULONGEST))
3022 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3025 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
3028 dwarf_expr_require_composition (op_ptr, op_end,
3029 "DW_OP_implicit_value");
3031 loc->kind = axs_rvalue;
3035 case DW_OP_stack_value:
3036 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
3037 loc->kind = axs_rvalue;
3072 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3073 i = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
3077 ax_const_l (expr, offset);
3078 ax_simple (expr, aop_add);
3083 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3084 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3085 i = dwarf_reg_to_regnum_or_error (arch, reg);
3089 ax_const_l (expr, offset);
3090 ax_simple (expr, aop_add);
3096 const gdb_byte *datastart;
3098 const struct block *b;
3099 struct symbol *framefunc;
3101 b = block_for_pc (expr->scope);
3104 error (_("No block found for address"));
3106 framefunc = block_linkage_function (b);
3109 error (_("No function found for block"));
3111 func_get_frame_base_dwarf_block (framefunc, expr->scope,
3112 &datastart, &datalen);
3114 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3115 dwarf2_compile_expr_to_ax (expr, loc, addr_size, datastart,
3116 datastart + datalen, per_cu);
3117 if (loc->kind == axs_lvalue_register)
3118 require_rvalue (expr, loc);
3122 ax_const_l (expr, offset);
3123 ax_simple (expr, aop_add);
3126 loc->kind = axs_lvalue_memory;
3131 ax_simple (expr, aop_dup);
3135 ax_simple (expr, aop_pop);
3140 ax_pick (expr, offset);
3144 ax_simple (expr, aop_swap);
3152 ax_simple (expr, aop_rot);
3156 case DW_OP_deref_size:
3160 if (op == DW_OP_deref_size)
3165 if (size != 1 && size != 2 && size != 4 && size != 8)
3166 error (_("Unsupported size %d in %s"),
3167 size, get_DW_OP_name (op));
3168 access_memory (arch, expr, size * TARGET_CHAR_BIT);
3173 /* Sign extend the operand. */
3174 ax_ext (expr, addr_size_bits);
3175 ax_simple (expr, aop_dup);
3176 ax_const_l (expr, 0);
3177 ax_simple (expr, aop_less_signed);
3178 ax_simple (expr, aop_log_not);
3179 i = ax_goto (expr, aop_if_goto);
3180 /* We have to emit 0 - X. */
3181 ax_const_l (expr, 0);
3182 ax_simple (expr, aop_swap);
3183 ax_simple (expr, aop_sub);
3184 ax_label (expr, i, expr->len);
3188 /* No need to sign extend here. */
3189 ax_const_l (expr, 0);
3190 ax_simple (expr, aop_swap);
3191 ax_simple (expr, aop_sub);
3195 /* Sign extend the operand. */
3196 ax_ext (expr, addr_size_bits);
3197 ax_simple (expr, aop_bit_not);
3200 case DW_OP_plus_uconst:
3201 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3202 /* It would be really weird to emit `DW_OP_plus_uconst 0',
3203 but we micro-optimize anyhow. */
3206 ax_const_l (expr, reg);
3207 ax_simple (expr, aop_add);
3212 ax_simple (expr, aop_bit_and);
3216 /* Sign extend the operands. */
3217 ax_ext (expr, addr_size_bits);
3218 ax_simple (expr, aop_swap);
3219 ax_ext (expr, addr_size_bits);
3220 ax_simple (expr, aop_swap);
3221 ax_simple (expr, aop_div_signed);
3225 ax_simple (expr, aop_sub);
3229 ax_simple (expr, aop_rem_unsigned);
3233 ax_simple (expr, aop_mul);
3237 ax_simple (expr, aop_bit_or);
3241 ax_simple (expr, aop_add);
3245 ax_simple (expr, aop_lsh);
3249 ax_simple (expr, aop_rsh_unsigned);
3253 ax_simple (expr, aop_rsh_signed);
3257 ax_simple (expr, aop_bit_xor);
3261 /* Sign extend the operands. */
3262 ax_ext (expr, addr_size_bits);
3263 ax_simple (expr, aop_swap);
3264 ax_ext (expr, addr_size_bits);
3265 /* Note no swap here: A <= B is !(B < A). */
3266 ax_simple (expr, aop_less_signed);
3267 ax_simple (expr, aop_log_not);
3271 /* Sign extend the operands. */
3272 ax_ext (expr, addr_size_bits);
3273 ax_simple (expr, aop_swap);
3274 ax_ext (expr, addr_size_bits);
3275 ax_simple (expr, aop_swap);
3276 /* A >= B is !(A < B). */
3277 ax_simple (expr, aop_less_signed);
3278 ax_simple (expr, aop_log_not);
3282 /* Sign extend the operands. */
3283 ax_ext (expr, addr_size_bits);
3284 ax_simple (expr, aop_swap);
3285 ax_ext (expr, addr_size_bits);
3286 /* No need for a second swap here. */
3287 ax_simple (expr, aop_equal);
3291 /* Sign extend the operands. */
3292 ax_ext (expr, addr_size_bits);
3293 ax_simple (expr, aop_swap);
3294 ax_ext (expr, addr_size_bits);
3295 ax_simple (expr, aop_swap);
3296 ax_simple (expr, aop_less_signed);
3300 /* Sign extend the operands. */
3301 ax_ext (expr, addr_size_bits);
3302 ax_simple (expr, aop_swap);
3303 ax_ext (expr, addr_size_bits);
3304 /* Note no swap here: A > B is B < A. */
3305 ax_simple (expr, aop_less_signed);
3309 /* Sign extend the operands. */
3310 ax_ext (expr, addr_size_bits);
3311 ax_simple (expr, aop_swap);
3312 ax_ext (expr, addr_size_bits);
3313 /* No need for a swap here. */
3314 ax_simple (expr, aop_equal);
3315 ax_simple (expr, aop_log_not);
3318 case DW_OP_call_frame_cfa:
3321 CORE_ADDR text_offset;
3323 const gdb_byte *cfa_start, *cfa_end;
3325 if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
3327 &text_offset, &cfa_start, &cfa_end))
3330 ax_reg (expr, regnum);
3333 ax_const_l (expr, off);
3334 ax_simple (expr, aop_add);
3339 /* Another expression. */
3340 ax_const_l (expr, text_offset);
3341 dwarf2_compile_expr_to_ax (expr, loc, addr_size, cfa_start,
3345 loc->kind = axs_lvalue_memory;
3349 case DW_OP_GNU_push_tls_address:
3350 case DW_OP_form_tls_address:
3354 case DW_OP_push_object_address:
3359 offset = extract_signed_integer (op_ptr, 2, byte_order);
3361 i = ax_goto (expr, aop_goto);
3362 dw_labels.push_back (op_ptr + offset - base);
3363 patches.push_back (i);
3367 offset = extract_signed_integer (op_ptr, 2, byte_order);
3369 /* Zero extend the operand. */
3370 ax_zero_ext (expr, addr_size_bits);
3371 i = ax_goto (expr, aop_if_goto);
3372 dw_labels.push_back (op_ptr + offset - base);
3373 patches.push_back (i);
3380 case DW_OP_bit_piece:
3384 if (op_ptr - 1 == previous_piece)
3385 error (_("Cannot translate empty pieces to agent expressions"));
3386 previous_piece = op_ptr - 1;
3388 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3389 if (op == DW_OP_piece)
3395 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3397 if (bits_collected + size > 8 * sizeof (LONGEST))
3398 error (_("Expression pieces exceed word size"));
3400 /* Access the bits. */
3403 case axs_lvalue_register:
3404 ax_reg (expr, loc->u.reg);
3407 case axs_lvalue_memory:
3408 /* Offset the pointer, if needed. */
3411 ax_const_l (expr, uoffset / 8);
3412 ax_simple (expr, aop_add);
3415 access_memory (arch, expr, size);
3419 /* For a bits-big-endian target, shift up what we already
3420 have. For a bits-little-endian target, shift up the
3421 new data. Note that there is a potential bug here if
3422 the DWARF expression leaves multiple values on the
3424 if (bits_collected > 0)
3426 if (bits_big_endian)
3428 ax_simple (expr, aop_swap);
3429 ax_const_l (expr, size);
3430 ax_simple (expr, aop_lsh);
3431 /* We don't need a second swap here, because
3432 aop_bit_or is symmetric. */
3436 ax_const_l (expr, size);
3437 ax_simple (expr, aop_lsh);
3439 ax_simple (expr, aop_bit_or);
3442 bits_collected += size;
3443 loc->kind = axs_rvalue;
3447 case DW_OP_GNU_uninit:
3453 struct dwarf2_locexpr_baton block;
3454 int size = (op == DW_OP_call2 ? 2 : 4);
3456 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3459 cu_offset cuoffset = (cu_offset) uoffset;
3460 block = dwarf2_fetch_die_loc_cu_off (cuoffset, per_cu,
3463 /* DW_OP_call_ref is currently not supported. */
3464 gdb_assert (block.per_cu == per_cu);
3466 dwarf2_compile_expr_to_ax (expr, loc, addr_size, block.data,
3467 block.data + block.size, per_cu);
3471 case DW_OP_call_ref:
3474 case DW_OP_GNU_variable_value:
3482 /* Patch all the branches we emitted. */
3483 for (int i = 0; i < patches.size (); ++i)
3485 int targ = offsets[dw_labels[i]];
3487 internal_error (__FILE__, __LINE__, _("invalid label"));
3488 ax_label (expr, patches[i], targ);
3493 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3494 evaluator to calculate the location. */
3495 static struct value *
3496 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3498 struct dwarf2_locexpr_baton *dlbaton
3499 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3502 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3503 dlbaton->size, dlbaton->per_cu);
3508 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3509 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3512 static struct value *
3513 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3515 struct dwarf2_locexpr_baton *dlbaton
3516 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3518 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3522 /* Implementation of get_symbol_read_needs from
3523 symbol_computed_ops. */
3525 static enum symbol_needs_kind
3526 locexpr_get_symbol_read_needs (struct symbol *symbol)
3528 struct dwarf2_locexpr_baton *dlbaton
3529 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3531 return dwarf2_loc_desc_get_symbol_read_needs (dlbaton->data, dlbaton->size,
3535 /* Return true if DATA points to the end of a piece. END is one past
3536 the last byte in the expression. */
3539 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3541 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3544 /* Helper for locexpr_describe_location_piece that finds the name of a
3548 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3552 /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3553 We'd rather print *something* here than throw an error. */
3554 regnum = dwarf_reg_to_regnum (gdbarch, dwarf_regnum);
3555 /* gdbarch_register_name may just return "", return something more
3556 descriptive for bad register numbers. */
3559 /* The text is output as "$bad_register_number".
3560 That is why we use the underscores. */
3561 return _("bad_register_number");
3563 return gdbarch_register_name (gdbarch, regnum);
3566 /* Nicely describe a single piece of a location, returning an updated
3567 position in the bytecode sequence. This function cannot recognize
3568 all locations; if a location is not recognized, it simply returns
3569 DATA. If there is an error during reading, e.g. we run off the end
3570 of the buffer, an error is thrown. */
3572 static const gdb_byte *
3573 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3574 CORE_ADDR addr, struct objfile *objfile,
3575 struct dwarf2_per_cu_data *per_cu,
3576 const gdb_byte *data, const gdb_byte *end,
3577 unsigned int addr_size)
3579 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3582 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3584 fprintf_filtered (stream, _("a variable in $%s"),
3585 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3588 else if (data[0] == DW_OP_regx)
3592 data = safe_read_uleb128 (data + 1, end, ®);
3593 fprintf_filtered (stream, _("a variable in $%s"),
3594 locexpr_regname (gdbarch, reg));
3596 else if (data[0] == DW_OP_fbreg)
3598 const struct block *b;
3599 struct symbol *framefunc;
3601 int64_t frame_offset;
3602 const gdb_byte *base_data, *new_data, *save_data = data;
3604 int64_t base_offset = 0;
3606 new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3607 if (!piece_end_p (new_data, end))
3611 b = block_for_pc (addr);
3614 error (_("No block found for address for symbol \"%s\"."),
3615 SYMBOL_PRINT_NAME (symbol));
3617 framefunc = block_linkage_function (b);
3620 error (_("No function found for block for symbol \"%s\"."),
3621 SYMBOL_PRINT_NAME (symbol));
3623 func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
3625 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3627 const gdb_byte *buf_end;
3629 frame_reg = base_data[0] - DW_OP_breg0;
3630 buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3632 if (buf_end != base_data + base_size)
3633 error (_("Unexpected opcode after "
3634 "DW_OP_breg%u for symbol \"%s\"."),
3635 frame_reg, SYMBOL_PRINT_NAME (symbol));
3637 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3639 /* The frame base is just the register, with no offset. */
3640 frame_reg = base_data[0] - DW_OP_reg0;
3645 /* We don't know what to do with the frame base expression,
3646 so we can't trace this variable; give up. */
3650 fprintf_filtered (stream,
3651 _("a variable at frame base reg $%s offset %s+%s"),
3652 locexpr_regname (gdbarch, frame_reg),
3653 plongest (base_offset), plongest (frame_offset));
3655 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3656 && piece_end_p (data, end))
3660 data = safe_read_sleb128 (data + 1, end, &offset);
3662 fprintf_filtered (stream,
3663 _("a variable at offset %s from base reg $%s"),
3665 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3668 /* The location expression for a TLS variable looks like this (on a
3671 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3672 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3674 0x3 is the encoding for DW_OP_addr, which has an operand as long
3675 as the size of an address on the target machine (here is 8
3676 bytes). Note that more recent version of GCC emit DW_OP_const4u
3677 or DW_OP_const8u, depending on address size, rather than
3678 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3679 The operand represents the offset at which the variable is within
3680 the thread local storage. */
3682 else if (data + 1 + addr_size < end
3683 && (data[0] == DW_OP_addr
3684 || (addr_size == 4 && data[0] == DW_OP_const4u)
3685 || (addr_size == 8 && data[0] == DW_OP_const8u))
3686 && (data[1 + addr_size] == DW_OP_GNU_push_tls_address
3687 || data[1 + addr_size] == DW_OP_form_tls_address)
3688 && piece_end_p (data + 2 + addr_size, end))
3691 offset = extract_unsigned_integer (data + 1, addr_size,
3692 gdbarch_byte_order (gdbarch));
3694 fprintf_filtered (stream,
3695 _("a thread-local variable at offset 0x%s "
3696 "in the thread-local storage for `%s'"),
3697 phex_nz (offset, addr_size), objfile_name (objfile));
3699 data += 1 + addr_size + 1;
3702 /* With -gsplit-dwarf a TLS variable can also look like this:
3703 DW_AT_location : 3 byte block: fc 4 e0
3704 (DW_OP_GNU_const_index: 4;
3705 DW_OP_GNU_push_tls_address) */
3706 else if (data + 3 <= end
3707 && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3708 && data[0] == DW_OP_GNU_const_index
3710 && (data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3711 || data[1 + leb128_size] == DW_OP_form_tls_address)
3712 && piece_end_p (data + 2 + leb128_size, end))
3716 data = safe_read_uleb128 (data + 1, end, &offset);
3717 offset = dwarf2_read_addr_index (per_cu, offset);
3718 fprintf_filtered (stream,
3719 _("a thread-local variable at offset 0x%s "
3720 "in the thread-local storage for `%s'"),
3721 phex_nz (offset, addr_size), objfile_name (objfile));
3725 else if (data[0] >= DW_OP_lit0
3726 && data[0] <= DW_OP_lit31
3728 && data[1] == DW_OP_stack_value)
3730 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3737 /* Disassemble an expression, stopping at the end of a piece or at the
3738 end of the expression. Returns a pointer to the next unread byte
3739 in the input expression. If ALL is nonzero, then this function
3740 will keep going until it reaches the end of the expression.
3741 If there is an error during reading, e.g. we run off the end
3742 of the buffer, an error is thrown. */
3744 static const gdb_byte *
3745 disassemble_dwarf_expression (struct ui_file *stream,
3746 struct gdbarch *arch, unsigned int addr_size,
3747 int offset_size, const gdb_byte *start,
3748 const gdb_byte *data, const gdb_byte *end,
3749 int indent, int all,
3750 struct dwarf2_per_cu_data *per_cu)
3754 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3756 enum dwarf_location_atom op = (enum dwarf_location_atom) *data++;
3761 name = get_DW_OP_name (op);
3764 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3765 op, (long) (data - 1 - start));
3766 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3767 (long) (data - 1 - start), name);
3772 ul = extract_unsigned_integer (data, addr_size,
3773 gdbarch_byte_order (arch));
3775 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3779 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3781 fprintf_filtered (stream, " %s", pulongest (ul));
3784 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3786 fprintf_filtered (stream, " %s", plongest (l));
3789 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3791 fprintf_filtered (stream, " %s", pulongest (ul));
3794 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3796 fprintf_filtered (stream, " %s", plongest (l));
3799 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3801 fprintf_filtered (stream, " %s", pulongest (ul));
3804 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3806 fprintf_filtered (stream, " %s", plongest (l));
3809 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3811 fprintf_filtered (stream, " %s", pulongest (ul));
3814 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3816 fprintf_filtered (stream, " %s", plongest (l));
3819 data = safe_read_uleb128 (data, end, &ul);
3820 fprintf_filtered (stream, " %s", pulongest (ul));
3823 data = safe_read_sleb128 (data, end, &l);
3824 fprintf_filtered (stream, " %s", plongest (l));
3859 fprintf_filtered (stream, " [$%s]",
3860 locexpr_regname (arch, op - DW_OP_reg0));
3864 data = safe_read_uleb128 (data, end, &ul);
3865 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
3866 locexpr_regname (arch, (int) ul));
3869 case DW_OP_implicit_value:
3870 data = safe_read_uleb128 (data, end, &ul);
3872 fprintf_filtered (stream, " %s", pulongest (ul));
3907 data = safe_read_sleb128 (data, end, &l);
3908 fprintf_filtered (stream, " %s [$%s]", plongest (l),
3909 locexpr_regname (arch, op - DW_OP_breg0));
3913 data = safe_read_uleb128 (data, end, &ul);
3914 data = safe_read_sleb128 (data, end, &l);
3915 fprintf_filtered (stream, " register %s [$%s] offset %s",
3917 locexpr_regname (arch, (int) ul),
3922 data = safe_read_sleb128 (data, end, &l);
3923 fprintf_filtered (stream, " %s", plongest (l));
3926 case DW_OP_xderef_size:
3927 case DW_OP_deref_size:
3929 fprintf_filtered (stream, " %d", *data);
3933 case DW_OP_plus_uconst:
3934 data = safe_read_uleb128 (data, end, &ul);
3935 fprintf_filtered (stream, " %s", pulongest (ul));
3939 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3941 fprintf_filtered (stream, " to %ld",
3942 (long) (data + l - start));
3946 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3948 fprintf_filtered (stream, " %ld",
3949 (long) (data + l - start));
3953 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3955 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
3959 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3961 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3964 case DW_OP_call_ref:
3965 ul = extract_unsigned_integer (data, offset_size,
3966 gdbarch_byte_order (arch));
3967 data += offset_size;
3968 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
3972 data = safe_read_uleb128 (data, end, &ul);
3973 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
3976 case DW_OP_bit_piece:
3980 data = safe_read_uleb128 (data, end, &ul);
3981 data = safe_read_uleb128 (data, end, &offset);
3982 fprintf_filtered (stream, " size %s offset %s (bits)",
3983 pulongest (ul), pulongest (offset));
3987 case DW_OP_implicit_pointer:
3988 case DW_OP_GNU_implicit_pointer:
3990 ul = extract_unsigned_integer (data, offset_size,
3991 gdbarch_byte_order (arch));
3992 data += offset_size;
3994 data = safe_read_sleb128 (data, end, &l);
3996 fprintf_filtered (stream, " DIE %s offset %s",
3997 phex_nz (ul, offset_size),
4002 case DW_OP_deref_type:
4003 case DW_OP_GNU_deref_type:
4005 int deref_addr_size = *data++;
4008 data = safe_read_uleb128 (data, end, &ul);
4009 cu_offset offset = (cu_offset) ul;
4010 type = dwarf2_get_die_type (offset, per_cu);
4011 fprintf_filtered (stream, "<");
4012 type_print (type, "", stream, -1);
4013 fprintf_filtered (stream, " [0x%s]> %d",
4014 phex_nz (to_underlying (offset), 0),
4019 case DW_OP_const_type:
4020 case DW_OP_GNU_const_type:
4024 data = safe_read_uleb128 (data, end, &ul);
4025 cu_offset type_die = (cu_offset) ul;
4026 type = dwarf2_get_die_type (type_die, per_cu);
4027 fprintf_filtered (stream, "<");
4028 type_print (type, "", stream, -1);
4029 fprintf_filtered (stream, " [0x%s]>",
4030 phex_nz (to_underlying (type_die), 0));
4034 case DW_OP_regval_type:
4035 case DW_OP_GNU_regval_type:
4040 data = safe_read_uleb128 (data, end, ®);
4041 data = safe_read_uleb128 (data, end, &ul);
4042 cu_offset type_die = (cu_offset) ul;
4044 type = dwarf2_get_die_type (type_die, per_cu);
4045 fprintf_filtered (stream, "<");
4046 type_print (type, "", stream, -1);
4047 fprintf_filtered (stream, " [0x%s]> [$%s]",
4048 phex_nz (to_underlying (type_die), 0),
4049 locexpr_regname (arch, reg));
4054 case DW_OP_GNU_convert:
4055 case DW_OP_reinterpret:
4056 case DW_OP_GNU_reinterpret:
4058 data = safe_read_uleb128 (data, end, &ul);
4059 cu_offset type_die = (cu_offset) ul;
4061 if (to_underlying (type_die) == 0)
4062 fprintf_filtered (stream, "<0>");
4067 type = dwarf2_get_die_type (type_die, per_cu);
4068 fprintf_filtered (stream, "<");
4069 type_print (type, "", stream, -1);
4070 fprintf_filtered (stream, " [0x%s]>",
4071 phex_nz (to_underlying (type_die), 0));
4076 case DW_OP_entry_value:
4077 case DW_OP_GNU_entry_value:
4078 data = safe_read_uleb128 (data, end, &ul);
4079 fputc_filtered ('\n', stream);
4080 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
4081 start, data, data + ul, indent + 2,
4086 case DW_OP_GNU_parameter_ref:
4087 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4089 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4092 case DW_OP_GNU_addr_index:
4093 data = safe_read_uleb128 (data, end, &ul);
4094 ul = dwarf2_read_addr_index (per_cu, ul);
4095 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
4097 case DW_OP_GNU_const_index:
4098 data = safe_read_uleb128 (data, end, &ul);
4099 ul = dwarf2_read_addr_index (per_cu, ul);
4100 fprintf_filtered (stream, " %s", pulongest (ul));
4103 case DW_OP_GNU_variable_value:
4104 ul = extract_unsigned_integer (data, offset_size,
4105 gdbarch_byte_order (arch));
4106 data += offset_size;
4107 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4111 fprintf_filtered (stream, "\n");
4117 /* Describe a single location, which may in turn consist of multiple
4121 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
4122 struct ui_file *stream,
4123 const gdb_byte *data, size_t size,
4124 struct objfile *objfile, unsigned int addr_size,
4125 int offset_size, struct dwarf2_per_cu_data *per_cu)
4127 const gdb_byte *end = data + size;
4128 int first_piece = 1, bad = 0;
4132 const gdb_byte *here = data;
4133 int disassemble = 1;
4138 fprintf_filtered (stream, _(", and "));
4140 if (!dwarf_always_disassemble)
4142 data = locexpr_describe_location_piece (symbol, stream,
4143 addr, objfile, per_cu,
4144 data, end, addr_size);
4145 /* If we printed anything, or if we have an empty piece,
4146 then don't disassemble. */
4148 || data[0] == DW_OP_piece
4149 || data[0] == DW_OP_bit_piece)
4154 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
4155 data = disassemble_dwarf_expression (stream,
4156 get_objfile_arch (objfile),
4157 addr_size, offset_size, data,
4159 dwarf_always_disassemble,
4165 int empty = data == here;
4168 fprintf_filtered (stream, " ");
4169 if (data[0] == DW_OP_piece)
4173 data = safe_read_uleb128 (data + 1, end, &bytes);
4176 fprintf_filtered (stream, _("an empty %s-byte piece"),
4179 fprintf_filtered (stream, _(" [%s-byte piece]"),
4182 else if (data[0] == DW_OP_bit_piece)
4184 uint64_t bits, offset;
4186 data = safe_read_uleb128 (data + 1, end, &bits);
4187 data = safe_read_uleb128 (data, end, &offset);
4190 fprintf_filtered (stream,
4191 _("an empty %s-bit piece"),
4194 fprintf_filtered (stream,
4195 _(" [%s-bit piece, offset %s bits]"),
4196 pulongest (bits), pulongest (offset));
4206 if (bad || data > end)
4207 error (_("Corrupted DWARF2 expression for \"%s\"."),
4208 SYMBOL_PRINT_NAME (symbol));
4211 /* Print a natural-language description of SYMBOL to STREAM. This
4212 version is for a symbol with a single location. */
4215 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
4216 struct ui_file *stream)
4218 struct dwarf2_locexpr_baton *dlbaton
4219 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4220 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4221 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4222 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4224 locexpr_describe_location_1 (symbol, addr, stream,
4225 dlbaton->data, dlbaton->size,
4226 objfile, addr_size, offset_size,
4230 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4231 any necessary bytecode in AX. */
4234 locexpr_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
4235 struct axs_value *value)
4237 struct dwarf2_locexpr_baton *dlbaton
4238 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4239 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4241 if (dlbaton->size == 0)
4242 value->optimized_out = 1;
4244 dwarf2_compile_expr_to_ax (ax, value, addr_size, dlbaton->data,
4245 dlbaton->data + dlbaton->size, dlbaton->per_cu);
4248 /* symbol_computed_ops 'generate_c_location' method. */
4251 locexpr_generate_c_location (struct symbol *sym, string_file *stream,
4252 struct gdbarch *gdbarch,
4253 unsigned char *registers_used,
4254 CORE_ADDR pc, const char *result_name)
4256 struct dwarf2_locexpr_baton *dlbaton
4257 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
4258 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4260 if (dlbaton->size == 0)
4261 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4263 compile_dwarf_expr_to_c (stream, result_name,
4264 sym, pc, gdbarch, registers_used, addr_size,
4265 dlbaton->data, dlbaton->data + dlbaton->size,
4269 /* The set of location functions used with the DWARF-2 expression
4271 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4272 locexpr_read_variable,
4273 locexpr_read_variable_at_entry,
4274 locexpr_get_symbol_read_needs,
4275 locexpr_describe_location,
4276 0, /* location_has_loclist */
4277 locexpr_tracepoint_var_ref,
4278 locexpr_generate_c_location
4282 /* Wrapper functions for location lists. These generally find
4283 the appropriate location expression and call something above. */
4285 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4286 evaluator to calculate the location. */
4287 static struct value *
4288 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4290 struct dwarf2_loclist_baton *dlbaton
4291 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4293 const gdb_byte *data;
4295 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
4297 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4298 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4304 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4305 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4308 Function always returns non-NULL value, it may be marked optimized out if
4309 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
4310 if it cannot resolve the parameter for any reason. */
4312 static struct value *
4313 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4315 struct dwarf2_loclist_baton *dlbaton
4316 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4317 const gdb_byte *data;
4321 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4322 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4324 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4326 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4328 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4331 /* Implementation of get_symbol_read_needs from
4332 symbol_computed_ops. */
4334 static enum symbol_needs_kind
4335 loclist_symbol_needs (struct symbol *symbol)
4337 /* If there's a location list, then assume we need to have a frame
4338 to choose the appropriate location expression. With tracking of
4339 global variables this is not necessarily true, but such tracking
4340 is disabled in GCC at the moment until we figure out how to
4343 return SYMBOL_NEEDS_FRAME;
4346 /* Print a natural-language description of SYMBOL to STREAM. This
4347 version applies when there is a list of different locations, each
4348 with a specified address range. */
4351 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4352 struct ui_file *stream)
4354 struct dwarf2_loclist_baton *dlbaton
4355 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4356 const gdb_byte *loc_ptr, *buf_end;
4357 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4358 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4359 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4360 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4361 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4362 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4363 /* Adjust base_address for relocatable objects. */
4364 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
4365 CORE_ADDR base_address = dlbaton->base_address + base_offset;
4368 loc_ptr = dlbaton->data;
4369 buf_end = dlbaton->data + dlbaton->size;
4371 fprintf_filtered (stream, _("multi-location:\n"));
4373 /* Iterate through locations until we run out. */
4376 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4378 enum debug_loc_kind kind;
4379 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4381 if (dlbaton->from_dwo)
4382 kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4383 loc_ptr, buf_end, &new_ptr,
4384 &low, &high, byte_order);
4386 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4388 byte_order, addr_size,
4393 case DEBUG_LOC_END_OF_LIST:
4396 case DEBUG_LOC_BASE_ADDRESS:
4397 base_address = high + base_offset;
4398 fprintf_filtered (stream, _(" Base address %s"),
4399 paddress (gdbarch, base_address));
4401 case DEBUG_LOC_START_END:
4402 case DEBUG_LOC_START_LENGTH:
4404 case DEBUG_LOC_BUFFER_OVERFLOW:
4405 case DEBUG_LOC_INVALID_ENTRY:
4406 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4407 SYMBOL_PRINT_NAME (symbol));
4409 gdb_assert_not_reached ("bad debug_loc_kind");
4412 /* Otherwise, a location expression entry. */
4413 low += base_address;
4414 high += base_address;
4416 low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
4417 high = gdbarch_adjust_dwarf2_addr (gdbarch, high);
4419 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4422 /* (It would improve readability to print only the minimum
4423 necessary digits of the second number of the range.) */
4424 fprintf_filtered (stream, _(" Range %s-%s: "),
4425 paddress (gdbarch, low), paddress (gdbarch, high));
4427 /* Now describe this particular location. */
4428 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4429 objfile, addr_size, offset_size,
4432 fprintf_filtered (stream, "\n");
4438 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4439 any necessary bytecode in AX. */
4441 loclist_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
4442 struct axs_value *value)
4444 struct dwarf2_loclist_baton *dlbaton
4445 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4446 const gdb_byte *data;
4448 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4450 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4452 value->optimized_out = 1;
4454 dwarf2_compile_expr_to_ax (ax, value, addr_size, data, data + size,
4458 /* symbol_computed_ops 'generate_c_location' method. */
4461 loclist_generate_c_location (struct symbol *sym, string_file *stream,
4462 struct gdbarch *gdbarch,
4463 unsigned char *registers_used,
4464 CORE_ADDR pc, const char *result_name)
4466 struct dwarf2_loclist_baton *dlbaton
4467 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
4468 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4469 const gdb_byte *data;
4472 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4474 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4476 compile_dwarf_expr_to_c (stream, result_name,
4477 sym, pc, gdbarch, registers_used, addr_size,
4482 /* The set of location functions used with the DWARF-2 expression
4483 evaluator and location lists. */
4484 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4485 loclist_read_variable,
4486 loclist_read_variable_at_entry,
4487 loclist_symbol_needs,
4488 loclist_describe_location,
4489 1, /* location_has_loclist */
4490 loclist_tracepoint_var_ref,
4491 loclist_generate_c_location
4495 _initialize_dwarf2loc (void)
4497 add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4498 &entry_values_debug,
4499 _("Set entry values and tail call frames "
4501 _("Show entry values and tail call frames "
4503 _("When non-zero, the process of determining "
4504 "parameter values from function entry point "
4505 "and tail call frames will be printed."),
4507 show_entry_values_debug,
4508 &setdebuglist, &showdebuglist);