1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003-2016 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"
45 extern int dwarf_always_disassemble;
47 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
48 struct frame_info *frame,
51 struct dwarf2_per_cu_data *per_cu,
54 static struct call_site_parameter *dwarf_expr_reg_to_entry_parameter
55 (struct frame_info *frame,
56 enum call_site_parameter_kind kind,
57 union call_site_parameter_u kind_u,
58 struct dwarf2_per_cu_data **per_cu_return);
60 /* Until these have formal names, we define these here.
61 ref: http://gcc.gnu.org/wiki/DebugFission
62 Each entry in .debug_loc.dwo begins with a byte that describes the entry,
63 and is then followed by data specific to that entry. */
67 /* Indicates the end of the list of entries. */
68 DEBUG_LOC_END_OF_LIST = 0,
70 /* This is followed by an unsigned LEB128 number that is an index into
71 .debug_addr and specifies the base address for all following entries. */
72 DEBUG_LOC_BASE_ADDRESS = 1,
74 /* This is followed by two unsigned LEB128 numbers that are indices into
75 .debug_addr and specify the beginning and ending addresses, and then
76 a normal location expression as in .debug_loc. */
77 DEBUG_LOC_START_END = 2,
79 /* This is followed by an unsigned LEB128 number that is an index into
80 .debug_addr and specifies the beginning address, and a 4 byte unsigned
81 number that specifies the length, and then a normal location expression
83 DEBUG_LOC_START_LENGTH = 3,
85 /* An internal value indicating there is insufficient data. */
86 DEBUG_LOC_BUFFER_OVERFLOW = -1,
88 /* An internal value indicating an invalid kind of entry was found. */
89 DEBUG_LOC_INVALID_ENTRY = -2
92 /* Helper function which throws an error if a synthetic pointer is
96 invalid_synthetic_pointer (void)
98 error (_("access outside bounds of object "
99 "referenced via synthetic pointer"));
102 /* Decode the addresses in a non-dwo .debug_loc entry.
103 A pointer to the next byte to examine is returned in *NEW_PTR.
104 The encoded low,high addresses are return in *LOW,*HIGH.
105 The result indicates the kind of entry found. */
107 static enum debug_loc_kind
108 decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
109 const gdb_byte **new_ptr,
110 CORE_ADDR *low, CORE_ADDR *high,
111 enum bfd_endian byte_order,
112 unsigned int addr_size,
115 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
117 if (buf_end - loc_ptr < 2 * addr_size)
118 return DEBUG_LOC_BUFFER_OVERFLOW;
121 *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
123 *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
124 loc_ptr += addr_size;
127 *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
129 *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
130 loc_ptr += addr_size;
134 /* A base-address-selection entry. */
135 if ((*low & base_mask) == base_mask)
136 return DEBUG_LOC_BASE_ADDRESS;
138 /* An end-of-list entry. */
139 if (*low == 0 && *high == 0)
140 return DEBUG_LOC_END_OF_LIST;
142 return DEBUG_LOC_START_END;
145 /* Decode the addresses in .debug_loc.dwo entry.
146 A pointer to the next byte to examine is returned in *NEW_PTR.
147 The encoded low,high addresses are return in *LOW,*HIGH.
148 The result indicates the kind of entry found. */
150 static enum debug_loc_kind
151 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
152 const gdb_byte *loc_ptr,
153 const gdb_byte *buf_end,
154 const gdb_byte **new_ptr,
155 CORE_ADDR *low, CORE_ADDR *high,
156 enum bfd_endian byte_order)
158 uint64_t low_index, high_index;
160 if (loc_ptr == buf_end)
161 return DEBUG_LOC_BUFFER_OVERFLOW;
165 case DEBUG_LOC_END_OF_LIST:
167 return DEBUG_LOC_END_OF_LIST;
168 case DEBUG_LOC_BASE_ADDRESS:
170 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
172 return DEBUG_LOC_BUFFER_OVERFLOW;
173 *high = dwarf2_read_addr_index (per_cu, high_index);
175 return DEBUG_LOC_BASE_ADDRESS;
176 case DEBUG_LOC_START_END:
177 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
179 return DEBUG_LOC_BUFFER_OVERFLOW;
180 *low = dwarf2_read_addr_index (per_cu, low_index);
181 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
183 return DEBUG_LOC_BUFFER_OVERFLOW;
184 *high = dwarf2_read_addr_index (per_cu, high_index);
186 return DEBUG_LOC_START_END;
187 case DEBUG_LOC_START_LENGTH:
188 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
190 return DEBUG_LOC_BUFFER_OVERFLOW;
191 *low = dwarf2_read_addr_index (per_cu, low_index);
192 if (loc_ptr + 4 > buf_end)
193 return DEBUG_LOC_BUFFER_OVERFLOW;
195 *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
196 *new_ptr = loc_ptr + 4;
197 return DEBUG_LOC_START_LENGTH;
199 return DEBUG_LOC_INVALID_ENTRY;
203 /* A function for dealing with location lists. Given a
204 symbol baton (BATON) and a pc value (PC), find the appropriate
205 location expression, set *LOCEXPR_LENGTH, and return a pointer
206 to the beginning of the expression. Returns NULL on failure.
208 For now, only return the first matching location expression; there
209 can be more than one in the list. */
212 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
213 size_t *locexpr_length, CORE_ADDR pc)
215 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
216 struct gdbarch *gdbarch = get_objfile_arch (objfile);
217 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
218 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
219 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
220 /* Adjust base_address for relocatable objects. */
221 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
222 CORE_ADDR base_address = baton->base_address + base_offset;
223 const gdb_byte *loc_ptr, *buf_end;
225 loc_ptr = baton->data;
226 buf_end = baton->data + baton->size;
230 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
232 enum debug_loc_kind kind;
233 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
236 kind = decode_debug_loc_dwo_addresses (baton->per_cu,
237 loc_ptr, buf_end, &new_ptr,
238 &low, &high, byte_order);
240 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
242 byte_order, addr_size,
247 case DEBUG_LOC_END_OF_LIST:
250 case DEBUG_LOC_BASE_ADDRESS:
251 base_address = high + base_offset;
253 case DEBUG_LOC_START_END:
254 case DEBUG_LOC_START_LENGTH:
256 case DEBUG_LOC_BUFFER_OVERFLOW:
257 case DEBUG_LOC_INVALID_ENTRY:
258 error (_("dwarf2_find_location_expression: "
259 "Corrupted DWARF expression."));
261 gdb_assert_not_reached ("bad debug_loc_kind");
264 /* Otherwise, a location expression entry.
265 If the entry is from a DWO, don't add base address: the entry is from
266 .debug_addr which already has the DWARF "base address". We still add
267 base_offset in case we're debugging a PIE executable. */
276 high += base_address;
279 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
282 if (low == high && pc == low)
284 /* This is entry PC record present only at entry point
285 of a function. Verify it is really the function entry point. */
287 const struct block *pc_block = block_for_pc (pc);
288 struct symbol *pc_func = NULL;
291 pc_func = block_linkage_function (pc_block);
293 if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
295 *locexpr_length = length;
300 if (pc >= low && pc < high)
302 *locexpr_length = length;
310 /* This is the baton used when performing dwarf2 expression
312 struct dwarf_expr_baton
314 struct frame_info *frame;
315 struct dwarf2_per_cu_data *per_cu;
316 CORE_ADDR obj_address;
319 /* Implement find_frame_base_location method for LOC_BLOCK functions using
320 DWARF expression for its DW_AT_frame_base. */
323 locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
324 const gdb_byte **start, size_t *length)
326 struct dwarf2_locexpr_baton *symbaton
327 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
329 *length = symbaton->size;
330 *start = symbaton->data;
333 /* Implement the struct symbol_block_ops::get_frame_base method for
334 LOC_BLOCK functions using a DWARF expression as its DW_AT_frame_base. */
337 locexpr_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
339 struct gdbarch *gdbarch;
341 struct dwarf2_locexpr_baton *dlbaton;
342 const gdb_byte *start;
344 struct value *result;
346 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
347 Thus, it's supposed to provide the find_frame_base_location method as
349 gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
351 gdbarch = get_frame_arch (frame);
352 type = builtin_type (gdbarch)->builtin_data_ptr;
353 dlbaton = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
355 SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
356 (framefunc, get_frame_pc (frame), &start, &length);
357 result = dwarf2_evaluate_loc_desc (type, frame, start, length,
360 /* The DW_AT_frame_base attribute contains a location description which
361 computes the base address itself. However, the call to
362 dwarf2_evaluate_loc_desc returns a value representing a variable at
363 that address. The frame base address is thus this variable's
365 return value_address (result);
368 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
369 function uses DWARF expression for its DW_AT_frame_base. */
371 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
373 locexpr_find_frame_base_location,
374 locexpr_get_frame_base
377 /* Implement find_frame_base_location method for LOC_BLOCK functions using
378 DWARF location list for its DW_AT_frame_base. */
381 loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
382 const gdb_byte **start, size_t *length)
384 struct dwarf2_loclist_baton *symbaton
385 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
387 *start = dwarf2_find_location_expression (symbaton, length, pc);
390 /* Implement the struct symbol_block_ops::get_frame_base method for
391 LOC_BLOCK functions using a DWARF location list as its DW_AT_frame_base. */
394 loclist_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
396 struct gdbarch *gdbarch;
398 struct dwarf2_loclist_baton *dlbaton;
399 const gdb_byte *start;
401 struct value *result;
403 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
404 Thus, it's supposed to provide the find_frame_base_location method as
406 gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
408 gdbarch = get_frame_arch (frame);
409 type = builtin_type (gdbarch)->builtin_data_ptr;
410 dlbaton = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
412 SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
413 (framefunc, get_frame_pc (frame), &start, &length);
414 result = dwarf2_evaluate_loc_desc (type, frame, start, length,
417 /* The DW_AT_frame_base attribute contains a location description which
418 computes the base address itself. However, the call to
419 dwarf2_evaluate_loc_desc returns a value representing a variable at
420 that address. The frame base address is thus this variable's
422 return value_address (result);
425 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
426 function uses DWARF location list for its DW_AT_frame_base. */
428 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
430 loclist_find_frame_base_location,
431 loclist_get_frame_base
434 /* See dwarf2loc.h. */
437 func_get_frame_base_dwarf_block (struct symbol *framefunc, CORE_ADDR pc,
438 const gdb_byte **start, size_t *length)
440 if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
442 const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
444 ops_block->find_frame_base_location (framefunc, pc, start, length);
450 error (_("Could not find the frame base for \"%s\"."),
451 SYMBOL_NATURAL_NAME (framefunc));
455 get_frame_pc_for_per_cu_dwarf_call (void *baton)
457 dwarf_expr_context *ctx = (dwarf_expr_context *) baton;
459 return ctx->get_frame_pc ();
463 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
464 struct dwarf2_per_cu_data *per_cu)
466 struct dwarf2_locexpr_baton block;
468 block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu,
469 get_frame_pc_for_per_cu_dwarf_call,
472 /* DW_OP_call_ref is currently not supported. */
473 gdb_assert (block.per_cu == per_cu);
475 ctx->eval (block.data, block.size);
478 class dwarf_evaluate_loc_desc : public dwarf_expr_context
482 struct frame_info *frame;
483 struct dwarf2_per_cu_data *per_cu;
484 CORE_ADDR obj_address;
486 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
487 the frame in BATON. */
489 CORE_ADDR get_frame_cfa () OVERRIDE
491 return dwarf2_frame_cfa (frame);
494 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
495 the frame in BATON. */
497 CORE_ADDR get_frame_pc () OVERRIDE
499 return get_frame_address_in_block (frame);
502 /* Using the objfile specified in BATON, find the address for the
503 current thread's thread-local storage with offset OFFSET. */
504 CORE_ADDR get_tls_address (CORE_ADDR offset) OVERRIDE
506 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
508 return target_translate_tls_address (objfile, offset);
511 /* Helper interface of per_cu_dwarf_call for
512 dwarf2_evaluate_loc_desc. */
514 void dwarf_call (cu_offset die_offset) OVERRIDE
516 per_cu_dwarf_call (this, die_offset, per_cu);
519 struct type *get_base_type (cu_offset die_offset, int size) OVERRIDE
521 struct type *result = dwarf2_get_die_type (die_offset, per_cu);
523 error (_("Could not find type for DW_OP_GNU_const_type"));
524 if (size != 0 && TYPE_LENGTH (result) != size)
525 error (_("DW_OP_GNU_const_type has different sizes for type and data"));
529 /* Callback function for dwarf2_evaluate_loc_desc.
530 Fetch the address indexed by DW_OP_GNU_addr_index. */
532 CORE_ADDR get_addr_index (unsigned int index) OVERRIDE
534 return dwarf2_read_addr_index (per_cu, index);
537 /* Callback function for get_object_address. Return the address of the VLA
540 CORE_ADDR get_object_address () OVERRIDE
542 if (obj_address == 0)
543 error (_("Location address is not set."));
547 /* Execute DWARF block of call_site_parameter which matches KIND and
548 KIND_U. Choose DEREF_SIZE value of that parameter. Search
549 caller of this objects's frame.
551 The caller can be from a different CU - per_cu_dwarf_call
552 implementation can be more simple as it does not support cross-CU
555 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind,
556 union call_site_parameter_u kind_u,
557 int deref_size) OVERRIDE
559 struct frame_info *caller_frame;
560 struct dwarf2_per_cu_data *caller_per_cu;
561 struct call_site_parameter *parameter;
562 const gdb_byte *data_src;
565 caller_frame = get_prev_frame (frame);
567 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
569 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
570 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
572 /* DEREF_SIZE size is not verified here. */
573 if (data_src == NULL)
574 throw_error (NO_ENTRY_VALUE_ERROR,
575 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
577 scoped_restore save_frame = make_scoped_restore (&this->frame,
579 scoped_restore save_per_cu = make_scoped_restore (&this->per_cu,
581 scoped_restore save_obj_addr = make_scoped_restore (&this->obj_address,
584 scoped_restore save_arch = make_scoped_restore (&this->gdbarch);
586 = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
587 scoped_restore save_addr_size = make_scoped_restore (&this->addr_size);
588 this->addr_size = dwarf2_per_cu_addr_size (per_cu);
589 scoped_restore save_offset = make_scoped_restore (&this->offset);
590 this->offset = dwarf2_per_cu_text_offset (per_cu);
592 this->eval (data_src, size);
595 /* Using the frame specified in BATON, find the location expression
596 describing the frame base. Return a pointer to it in START and
597 its length in LENGTH. */
598 void get_frame_base (const gdb_byte **start, size_t * length) OVERRIDE
600 /* FIXME: cagney/2003-03-26: This code should be using
601 get_frame_base_address(), and then implement a dwarf2 specific
603 struct symbol *framefunc;
604 const struct block *bl = get_frame_block (frame, NULL);
607 error (_("frame address is not available."));
609 /* Use block_linkage_function, which returns a real (not inlined)
610 function, instead of get_frame_function, which may return an
612 framefunc = block_linkage_function (bl);
614 /* If we found a frame-relative symbol then it was certainly within
615 some function associated with a frame. If we can't find the frame,
616 something has gone wrong. */
617 gdb_assert (framefunc != NULL);
619 func_get_frame_base_dwarf_block (framefunc,
620 get_frame_address_in_block (frame),
624 /* Read memory at ADDR (length LEN) into BUF. */
626 void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t len) OVERRIDE
628 read_memory (addr, buf, len);
631 /* Using the frame specified in BATON, return the value of register
632 REGNUM, treated as a pointer. */
633 CORE_ADDR read_addr_from_reg (int dwarf_regnum) OVERRIDE
635 struct gdbarch *gdbarch = get_frame_arch (frame);
636 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
638 return address_from_register (regnum, frame);
641 /* Implement "get_reg_value" callback. */
643 struct value *get_reg_value (struct type *type, int dwarf_regnum) OVERRIDE
645 struct gdbarch *gdbarch = get_frame_arch (frame);
646 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
648 return value_from_register (type, regnum, frame);
652 /* See dwarf2loc.h. */
654 unsigned int entry_values_debug = 0;
656 /* Helper to set entry_values_debug. */
659 show_entry_values_debug (struct ui_file *file, int from_tty,
660 struct cmd_list_element *c, const char *value)
662 fprintf_filtered (file,
663 _("Entry values and tail call frames debugging is %s.\n"),
667 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
668 CALLER_FRAME (for registers) can be NULL if it is not known. This function
669 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
672 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
673 struct call_site *call_site,
674 struct frame_info *caller_frame)
676 switch (FIELD_LOC_KIND (call_site->target))
678 case FIELD_LOC_KIND_DWARF_BLOCK:
680 struct dwarf2_locexpr_baton *dwarf_block;
682 struct type *caller_core_addr_type;
683 struct gdbarch *caller_arch;
685 dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
686 if (dwarf_block == NULL)
688 struct bound_minimal_symbol msym;
690 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
691 throw_error (NO_ENTRY_VALUE_ERROR,
692 _("DW_AT_GNU_call_site_target is not specified "
694 paddress (call_site_gdbarch, call_site->pc),
695 (msym.minsym == NULL ? "???"
696 : MSYMBOL_PRINT_NAME (msym.minsym)));
699 if (caller_frame == NULL)
701 struct bound_minimal_symbol msym;
703 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
704 throw_error (NO_ENTRY_VALUE_ERROR,
705 _("DW_AT_GNU_call_site_target DWARF block resolving "
706 "requires known frame which is currently not "
707 "available at %s in %s"),
708 paddress (call_site_gdbarch, call_site->pc),
709 (msym.minsym == NULL ? "???"
710 : MSYMBOL_PRINT_NAME (msym.minsym)));
713 caller_arch = get_frame_arch (caller_frame);
714 caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
715 val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
716 dwarf_block->data, dwarf_block->size,
717 dwarf_block->per_cu);
718 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
720 if (VALUE_LVAL (val) == lval_memory)
721 return value_address (val);
723 return value_as_address (val);
726 case FIELD_LOC_KIND_PHYSNAME:
728 const char *physname;
729 struct bound_minimal_symbol msym;
731 physname = FIELD_STATIC_PHYSNAME (call_site->target);
733 /* Handle both the mangled and demangled PHYSNAME. */
734 msym = lookup_minimal_symbol (physname, NULL, NULL);
735 if (msym.minsym == NULL)
737 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
738 throw_error (NO_ENTRY_VALUE_ERROR,
739 _("Cannot find function \"%s\" for a call site target "
741 physname, paddress (call_site_gdbarch, call_site->pc),
742 (msym.minsym == NULL ? "???"
743 : MSYMBOL_PRINT_NAME (msym.minsym)));
746 return BMSYMBOL_VALUE_ADDRESS (msym);
749 case FIELD_LOC_KIND_PHYSADDR:
750 return FIELD_STATIC_PHYSADDR (call_site->target);
753 internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
757 /* Convert function entry point exact address ADDR to the function which is
758 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
759 NO_ENTRY_VALUE_ERROR otherwise. */
761 static struct symbol *
762 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
764 struct symbol *sym = find_pc_function (addr);
767 if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
768 throw_error (NO_ENTRY_VALUE_ERROR,
769 _("DW_TAG_GNU_call_site resolving failed to find function "
770 "name for address %s"),
771 paddress (gdbarch, addr));
773 type = SYMBOL_TYPE (sym);
774 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
775 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
780 /* Verify function with entry point exact address ADDR can never call itself
781 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
782 can call itself via tail calls.
784 If a funtion can tail call itself its entry value based parameters are
785 unreliable. There is no verification whether the value of some/all
786 parameters is unchanged through the self tail call, we expect if there is
787 a self tail call all the parameters can be modified. */
790 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
792 struct obstack addr_obstack;
793 struct cleanup *old_chain;
796 /* Track here CORE_ADDRs which were already visited. */
799 /* The verification is completely unordered. Track here function addresses
800 which still need to be iterated. */
801 VEC (CORE_ADDR) *todo = NULL;
803 obstack_init (&addr_obstack);
804 old_chain = make_cleanup_obstack_free (&addr_obstack);
805 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
806 &addr_obstack, hashtab_obstack_allocate,
808 make_cleanup_htab_delete (addr_hash);
810 make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
812 VEC_safe_push (CORE_ADDR, todo, verify_addr);
813 while (!VEC_empty (CORE_ADDR, todo))
815 struct symbol *func_sym;
816 struct call_site *call_site;
818 addr = VEC_pop (CORE_ADDR, todo);
820 func_sym = func_addr_to_tail_call_list (gdbarch, addr);
822 for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
823 call_site; call_site = call_site->tail_call_next)
825 CORE_ADDR target_addr;
828 /* CALLER_FRAME with registers is not available for tail-call jumped
830 target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
832 if (target_addr == verify_addr)
834 struct bound_minimal_symbol msym;
836 msym = lookup_minimal_symbol_by_pc (verify_addr);
837 throw_error (NO_ENTRY_VALUE_ERROR,
838 _("DW_OP_GNU_entry_value resolving has found "
839 "function \"%s\" at %s can call itself via tail "
841 (msym.minsym == NULL ? "???"
842 : MSYMBOL_PRINT_NAME (msym.minsym)),
843 paddress (gdbarch, verify_addr));
846 slot = htab_find_slot (addr_hash, &target_addr, INSERT);
849 *slot = obstack_copy (&addr_obstack, &target_addr,
850 sizeof (target_addr));
851 VEC_safe_push (CORE_ADDR, todo, target_addr);
856 do_cleanups (old_chain);
859 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
860 ENTRY_VALUES_DEBUG. */
863 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
865 CORE_ADDR addr = call_site->pc;
866 struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
868 fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
869 (msym.minsym == NULL ? "???"
870 : MSYMBOL_PRINT_NAME (msym.minsym)));
874 /* vec.h needs single word type name, typedef it. */
875 typedef struct call_site *call_sitep;
877 /* Define VEC (call_sitep) functions. */
878 DEF_VEC_P (call_sitep);
880 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
881 only top callers and bottom callees which are present in both. GDBARCH is
882 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
883 no remaining possibilities to provide unambiguous non-trivial result.
884 RESULTP should point to NULL on the first (initialization) call. Caller is
885 responsible for xfree of any RESULTP data. */
888 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
889 VEC (call_sitep) *chain)
891 struct call_site_chain *result = *resultp;
892 long length = VEC_length (call_sitep, chain);
893 int callers, callees, idx;
897 /* Create the initial chain containing all the passed PCs. */
899 result = ((struct call_site_chain *)
900 xmalloc (sizeof (*result)
901 + sizeof (*result->call_site) * (length - 1)));
902 result->length = length;
903 result->callers = result->callees = length;
904 if (!VEC_empty (call_sitep, chain))
905 memcpy (result->call_site, VEC_address (call_sitep, chain),
906 sizeof (*result->call_site) * length);
909 if (entry_values_debug)
911 fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
912 for (idx = 0; idx < length; idx++)
913 tailcall_dump (gdbarch, result->call_site[idx]);
914 fputc_unfiltered ('\n', gdb_stdlog);
920 if (entry_values_debug)
922 fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
923 for (idx = 0; idx < length; idx++)
924 tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
925 fputc_unfiltered ('\n', gdb_stdlog);
928 /* Intersect callers. */
930 callers = std::min ((long) result->callers, length);
931 for (idx = 0; idx < callers; idx++)
932 if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
934 result->callers = idx;
938 /* Intersect callees. */
940 callees = std::min ((long) result->callees, length);
941 for (idx = 0; idx < callees; idx++)
942 if (result->call_site[result->length - 1 - idx]
943 != VEC_index (call_sitep, chain, length - 1 - idx))
945 result->callees = idx;
949 if (entry_values_debug)
951 fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
952 for (idx = 0; idx < result->callers; idx++)
953 tailcall_dump (gdbarch, result->call_site[idx]);
954 fputs_unfiltered (" |", gdb_stdlog);
955 for (idx = 0; idx < result->callees; idx++)
956 tailcall_dump (gdbarch, result->call_site[result->length
957 - result->callees + idx]);
958 fputc_unfiltered ('\n', gdb_stdlog);
961 if (result->callers == 0 && result->callees == 0)
963 /* There are no common callers or callees. It could be also a direct
964 call (which has length 0) with ambiguous possibility of an indirect
965 call - CALLERS == CALLEES == 0 is valid during the first allocation
966 but any subsequence processing of such entry means ambiguity. */
972 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
973 PC again. In such case there must be two different code paths to reach
974 it. CALLERS + CALLEES equal to LENGTH in the case of self tail-call. */
975 gdb_assert (result->callers + result->callees <= result->length);
978 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
979 assumed frames between them use GDBARCH. Use depth first search so we can
980 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
981 would have needless GDB stack overhead. Caller is responsible for xfree of
982 the returned result. Any unreliability results in thrown
983 NO_ENTRY_VALUE_ERROR. */
985 static struct call_site_chain *
986 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
989 CORE_ADDR save_callee_pc = callee_pc;
990 struct obstack addr_obstack;
991 struct cleanup *back_to_retval, *back_to_workdata;
992 struct call_site_chain *retval = NULL;
993 struct call_site *call_site;
995 /* Mark CALL_SITEs so we do not visit the same ones twice. */
998 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
999 call_site nor any possible call_site at CALLEE_PC's function is there.
1000 Any CALL_SITE in CHAIN will be iterated to its siblings - via
1001 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
1002 VEC (call_sitep) *chain = NULL;
1004 /* We are not interested in the specific PC inside the callee function. */
1005 callee_pc = get_pc_function_start (callee_pc);
1007 throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
1008 paddress (gdbarch, save_callee_pc));
1010 back_to_retval = make_cleanup (free_current_contents, &retval);
1012 obstack_init (&addr_obstack);
1013 back_to_workdata = make_cleanup_obstack_free (&addr_obstack);
1014 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
1015 &addr_obstack, hashtab_obstack_allocate,
1017 make_cleanup_htab_delete (addr_hash);
1019 make_cleanup (VEC_cleanup (call_sitep), &chain);
1021 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
1022 at the target's function. All the possible tail call sites in the
1023 target's function will get iterated as already pushed into CHAIN via their
1025 call_site = call_site_for_pc (gdbarch, caller_pc);
1029 CORE_ADDR target_func_addr;
1030 struct call_site *target_call_site;
1032 /* CALLER_FRAME with registers is not available for tail-call jumped
1034 target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
1036 if (target_func_addr == callee_pc)
1038 chain_candidate (gdbarch, &retval, chain);
1042 /* There is no way to reach CALLEE_PC again as we would prevent
1043 entering it twice as being already marked in ADDR_HASH. */
1044 target_call_site = NULL;
1048 struct symbol *target_func;
1050 target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
1051 target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
1056 /* Attempt to visit TARGET_CALL_SITE. */
1058 if (target_call_site)
1062 slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
1065 /* Successfully entered TARGET_CALL_SITE. */
1067 *slot = &target_call_site->pc;
1068 VEC_safe_push (call_sitep, chain, target_call_site);
1073 /* Backtrack (without revisiting the originating call_site). Try the
1074 callers's sibling; if there isn't any try the callers's callers's
1077 target_call_site = NULL;
1078 while (!VEC_empty (call_sitep, chain))
1080 call_site = VEC_pop (call_sitep, chain);
1082 gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
1083 NO_INSERT) != NULL);
1084 htab_remove_elt (addr_hash, &call_site->pc);
1086 target_call_site = call_site->tail_call_next;
1087 if (target_call_site)
1091 while (target_call_site);
1093 if (VEC_empty (call_sitep, chain))
1096 call_site = VEC_last (call_sitep, chain);
1101 struct bound_minimal_symbol msym_caller, msym_callee;
1103 msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
1104 msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
1105 throw_error (NO_ENTRY_VALUE_ERROR,
1106 _("There are no unambiguously determinable intermediate "
1107 "callers or callees between caller function \"%s\" at %s "
1108 "and callee function \"%s\" at %s"),
1109 (msym_caller.minsym == NULL
1110 ? "???" : MSYMBOL_PRINT_NAME (msym_caller.minsym)),
1111 paddress (gdbarch, caller_pc),
1112 (msym_callee.minsym == NULL
1113 ? "???" : MSYMBOL_PRINT_NAME (msym_callee.minsym)),
1114 paddress (gdbarch, callee_pc));
1117 do_cleanups (back_to_workdata);
1118 discard_cleanups (back_to_retval);
1122 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
1123 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
1124 constructed return NULL. Caller is responsible for xfree of the returned
1127 struct call_site_chain *
1128 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
1129 CORE_ADDR callee_pc)
1131 struct call_site_chain *retval = NULL;
1135 retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
1137 CATCH (e, RETURN_MASK_ERROR)
1139 if (e.error == NO_ENTRY_VALUE_ERROR)
1141 if (entry_values_debug)
1142 exception_print (gdb_stdout, e);
1147 throw_exception (e);
1154 /* Return 1 if KIND and KIND_U match PARAMETER. Return 0 otherwise. */
1157 call_site_parameter_matches (struct call_site_parameter *parameter,
1158 enum call_site_parameter_kind kind,
1159 union call_site_parameter_u kind_u)
1161 if (kind == parameter->kind)
1164 case CALL_SITE_PARAMETER_DWARF_REG:
1165 return kind_u.dwarf_reg == parameter->u.dwarf_reg;
1166 case CALL_SITE_PARAMETER_FB_OFFSET:
1167 return kind_u.fb_offset == parameter->u.fb_offset;
1168 case CALL_SITE_PARAMETER_PARAM_OFFSET:
1169 return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off;
1174 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
1175 FRAME is for callee.
1177 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
1180 static struct call_site_parameter *
1181 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
1182 enum call_site_parameter_kind kind,
1183 union call_site_parameter_u kind_u,
1184 struct dwarf2_per_cu_data **per_cu_return)
1186 CORE_ADDR func_addr, caller_pc;
1187 struct gdbarch *gdbarch;
1188 struct frame_info *caller_frame;
1189 struct call_site *call_site;
1191 /* Initialize it just to avoid a GCC false warning. */
1192 struct call_site_parameter *parameter = NULL;
1193 CORE_ADDR target_addr;
1195 while (get_frame_type (frame) == INLINE_FRAME)
1197 frame = get_prev_frame (frame);
1198 gdb_assert (frame != NULL);
1201 func_addr = get_frame_func (frame);
1202 gdbarch = get_frame_arch (frame);
1203 caller_frame = get_prev_frame (frame);
1204 if (gdbarch != frame_unwind_arch (frame))
1206 struct bound_minimal_symbol msym
1207 = lookup_minimal_symbol_by_pc (func_addr);
1208 struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
1210 throw_error (NO_ENTRY_VALUE_ERROR,
1211 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
1212 "(of %s (%s)) does not match caller gdbarch %s"),
1213 gdbarch_bfd_arch_info (gdbarch)->printable_name,
1214 paddress (gdbarch, func_addr),
1215 (msym.minsym == NULL ? "???"
1216 : MSYMBOL_PRINT_NAME (msym.minsym)),
1217 gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
1220 if (caller_frame == NULL)
1222 struct bound_minimal_symbol msym
1223 = lookup_minimal_symbol_by_pc (func_addr);
1225 throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
1226 "requires caller of %s (%s)"),
1227 paddress (gdbarch, func_addr),
1228 (msym.minsym == NULL ? "???"
1229 : MSYMBOL_PRINT_NAME (msym.minsym)));
1231 caller_pc = get_frame_pc (caller_frame);
1232 call_site = call_site_for_pc (gdbarch, caller_pc);
1234 target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
1235 if (target_addr != func_addr)
1237 struct minimal_symbol *target_msym, *func_msym;
1239 target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
1240 func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
1241 throw_error (NO_ENTRY_VALUE_ERROR,
1242 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
1243 "but the called frame is for %s at %s"),
1244 (target_msym == NULL ? "???"
1245 : MSYMBOL_PRINT_NAME (target_msym)),
1246 paddress (gdbarch, target_addr),
1247 func_msym == NULL ? "???" : MSYMBOL_PRINT_NAME (func_msym),
1248 paddress (gdbarch, func_addr));
1251 /* No entry value based parameters would be reliable if this function can
1252 call itself via tail calls. */
1253 func_verify_no_selftailcall (gdbarch, func_addr);
1255 for (iparams = 0; iparams < call_site->parameter_count; iparams++)
1257 parameter = &call_site->parameter[iparams];
1258 if (call_site_parameter_matches (parameter, kind, kind_u))
1261 if (iparams == call_site->parameter_count)
1263 struct minimal_symbol *msym
1264 = lookup_minimal_symbol_by_pc (caller_pc).minsym;
1266 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
1267 determine its value. */
1268 throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
1269 "at DW_TAG_GNU_call_site %s at %s"),
1270 paddress (gdbarch, caller_pc),
1271 msym == NULL ? "???" : MSYMBOL_PRINT_NAME (msym));
1274 *per_cu_return = call_site->per_cu;
1278 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
1279 the normal DW_AT_GNU_call_site_value block. Otherwise return the
1280 DW_AT_GNU_call_site_data_value (dereferenced) block.
1282 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1285 Function always returns non-NULL, non-optimized out value. It throws
1286 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
1288 static struct value *
1289 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
1290 CORE_ADDR deref_size, struct type *type,
1291 struct frame_info *caller_frame,
1292 struct dwarf2_per_cu_data *per_cu)
1294 const gdb_byte *data_src;
1298 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1299 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1301 /* DEREF_SIZE size is not verified here. */
1302 if (data_src == NULL)
1303 throw_error (NO_ENTRY_VALUE_ERROR,
1304 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1306 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
1307 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1309 data = (gdb_byte *) alloca (size + 1);
1310 memcpy (data, data_src, size);
1311 data[size] = DW_OP_stack_value;
1313 return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
1316 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1317 the indirect method on it, that is use its stored target value, the sole
1318 purpose of entry_data_value_funcs.. */
1320 static struct value *
1321 entry_data_value_coerce_ref (const struct value *value)
1323 struct type *checked_type = check_typedef (value_type (value));
1324 struct value *target_val;
1326 if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
1329 target_val = (struct value *) value_computed_closure (value);
1330 value_incref (target_val);
1334 /* Implement copy_closure. */
1337 entry_data_value_copy_closure (const struct value *v)
1339 struct value *target_val = (struct value *) value_computed_closure (v);
1341 value_incref (target_val);
1345 /* Implement free_closure. */
1348 entry_data_value_free_closure (struct value *v)
1350 struct value *target_val = (struct value *) value_computed_closure (v);
1352 value_free (target_val);
1355 /* Vector for methods for an entry value reference where the referenced value
1356 is stored in the caller. On the first dereference use
1357 DW_AT_GNU_call_site_data_value in the caller. */
1359 static const struct lval_funcs entry_data_value_funcs =
1363 NULL, /* indirect */
1364 entry_data_value_coerce_ref,
1365 NULL, /* check_synthetic_pointer */
1366 entry_data_value_copy_closure,
1367 entry_data_value_free_closure
1370 /* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KIND_U
1371 are used to match DW_AT_location at the caller's
1372 DW_TAG_GNU_call_site_parameter.
1374 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1375 cannot resolve the parameter for any reason. */
1377 static struct value *
1378 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1379 enum call_site_parameter_kind kind,
1380 union call_site_parameter_u kind_u)
1382 struct type *checked_type = check_typedef (type);
1383 struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1384 struct frame_info *caller_frame = get_prev_frame (frame);
1385 struct value *outer_val, *target_val, *val;
1386 struct call_site_parameter *parameter;
1387 struct dwarf2_per_cu_data *caller_per_cu;
1389 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1392 outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1396 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1397 used and it is not available do not fall back to OUTER_VAL - dereferencing
1398 TYPE_CODE_REF with non-entry data value would give current value - not the
1401 if (TYPE_CODE (checked_type) != TYPE_CODE_REF
1402 || TYPE_TARGET_TYPE (checked_type) == NULL)
1405 target_val = dwarf_entry_parameter_to_value (parameter,
1406 TYPE_LENGTH (target_type),
1407 target_type, caller_frame,
1410 release_value (target_val);
1411 val = allocate_computed_value (type, &entry_data_value_funcs,
1412 target_val /* closure */);
1414 /* Copy the referencing pointer to the new computed value. */
1415 memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1416 TYPE_LENGTH (checked_type));
1417 set_value_lazy (val, 0);
1422 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1423 SIZE are DWARF block used to match DW_AT_location at the caller's
1424 DW_TAG_GNU_call_site_parameter.
1426 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1427 cannot resolve the parameter for any reason. */
1429 static struct value *
1430 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1431 const gdb_byte *block, size_t block_len)
1433 union call_site_parameter_u kind_u;
1435 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1436 if (kind_u.dwarf_reg != -1)
1437 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
1440 if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
1441 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
1444 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1445 suppressed during normal operation. The expression can be arbitrary if
1446 there is no caller-callee entry value binding expected. */
1447 throw_error (NO_ENTRY_VALUE_ERROR,
1448 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1449 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1452 struct piece_closure
1454 /* Reference count. */
1457 /* The CU from which this closure's expression came. */
1458 struct dwarf2_per_cu_data *per_cu;
1460 /* The number of pieces used to describe this variable. */
1463 /* The target address size, used only for DWARF_VALUE_STACK. */
1466 /* The pieces themselves. */
1467 struct dwarf_expr_piece *pieces;
1470 /* Allocate a closure for a value formed from separately-described
1473 static struct piece_closure *
1474 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1475 int n_pieces, struct dwarf_expr_piece *pieces,
1478 struct piece_closure *c = XCNEW (struct piece_closure);
1483 c->n_pieces = n_pieces;
1484 c->addr_size = addr_size;
1485 c->pieces = XCNEWVEC (struct dwarf_expr_piece, n_pieces);
1487 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
1488 for (i = 0; i < n_pieces; ++i)
1489 if (c->pieces[i].location == DWARF_VALUE_STACK)
1490 value_incref (c->pieces[i].v.value);
1495 /* Copy NBITS bits from SOURCE to DEST starting at the given bit
1496 offsets. Use the bit order as specified by BITS_BIG_ENDIAN.
1497 Source and destination buffers must not overlap. */
1500 copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
1501 const gdb_byte *source, ULONGEST source_offset,
1502 ULONGEST nbits, int bits_big_endian)
1504 unsigned int buf, avail;
1509 if (bits_big_endian)
1511 /* Start from the end, then work backwards. */
1512 dest_offset += nbits - 1;
1513 dest += dest_offset / 8;
1514 dest_offset = 7 - dest_offset % 8;
1515 source_offset += nbits - 1;
1516 source += source_offset / 8;
1517 source_offset = 7 - source_offset % 8;
1521 dest += dest_offset / 8;
1523 source += source_offset / 8;
1527 /* Fill BUF with DEST_OFFSET bits from the destination and 8 -
1528 SOURCE_OFFSET bits from the source. */
1529 buf = *(bits_big_endian ? source-- : source++) >> source_offset;
1530 buf <<= dest_offset;
1531 buf |= *dest & ((1 << dest_offset) - 1);
1533 /* NBITS: bits yet to be written; AVAIL: BUF's fill level. */
1534 nbits += dest_offset;
1535 avail = dest_offset + 8 - source_offset;
1537 /* Flush 8 bits from BUF, if appropriate. */
1538 if (nbits >= 8 && avail >= 8)
1540 *(bits_big_endian ? dest-- : dest++) = buf;
1546 /* Copy the middle part. */
1549 size_t len = nbits / 8;
1551 /* Use a faster method for byte-aligned copies. */
1554 if (bits_big_endian)
1558 memcpy (dest + 1, source + 1, len);
1562 memcpy (dest, source, len);
1571 buf |= *(bits_big_endian ? source-- : source++) << avail;
1572 *(bits_big_endian ? dest-- : dest++) = buf;
1579 /* Write the last byte. */
1583 buf |= *source << avail;
1585 buf &= (1 << nbits) - 1;
1586 *dest = (*dest & (~0 << nbits)) | buf;
1592 namespace selftests {
1594 /* Helper function for the unit test of copy_bitwise. Convert NBITS bits
1595 out of BITS, starting at OFFS, to the respective '0'/'1'-string. MSB0
1596 specifies whether to assume big endian bit numbering. Store the
1597 resulting (not null-terminated) string at STR. */
1600 bits_to_str (char *str, const gdb_byte *bits, ULONGEST offs,
1601 ULONGEST nbits, int msb0)
1606 for (i = offs / 8, j = offs % 8; nbits; i++, j = 0)
1608 unsigned int ch = bits[i];
1609 for (; j < 8 && nbits; j++, nbits--)
1610 *str++ = (ch & (msb0 ? (1 << (7 - j)) : (1 << j))) ? '1' : '0';
1614 /* Check one invocation of copy_bitwise with the given parameters. */
1617 check_copy_bitwise (const gdb_byte *dest, unsigned int dest_offset,
1618 const gdb_byte *source, unsigned int source_offset,
1619 unsigned int nbits, int msb0)
1621 size_t len = align_up (dest_offset + nbits, 8);
1622 char *expected = (char *) alloca (len + 1);
1623 char *actual = (char *) alloca (len + 1);
1624 gdb_byte *buf = (gdb_byte *) alloca (len / 8);
1626 /* Compose a '0'/'1'-string that represents the expected result of
1628 Bits from [0, DEST_OFFSET) are filled from DEST.
1629 Bits from [DEST_OFFSET, DEST_OFFSET + NBITS) are filled from SOURCE.
1630 Bits from [DEST_OFFSET + NBITS, LEN) are filled from DEST.
1639 We should end up with:
1641 DDDDSSDD (D=dest, S=source)
1643 bits_to_str (expected, dest, 0, len, msb0);
1644 bits_to_str (expected + dest_offset, source, source_offset, nbits, msb0);
1646 /* Fill BUF with data from DEST, apply copy_bitwise, and convert the
1647 result to a '0'/'1'-string. */
1648 memcpy (buf, dest, len / 8);
1649 copy_bitwise (buf, dest_offset, source, source_offset, nbits, msb0);
1650 bits_to_str (actual, buf, 0, len, msb0);
1652 /* Compare the resulting strings. */
1653 expected[len] = actual[len] = '\0';
1654 if (strcmp (expected, actual) != 0)
1655 error (_("copy_bitwise %s != %s (%u+%u -> %u)"),
1656 expected, actual, source_offset, nbits, dest_offset);
1659 /* Unit test for copy_bitwise. */
1662 copy_bitwise_tests (void)
1664 /* Data to be used as both source and destination buffers. The two
1665 arrays below represent the lsb0- and msb0- encoded versions of the
1666 following bit string, respectively:
1667 00000000 00011111 11111111 01001000 10100101 11110010
1668 This pattern is chosen such that it contains:
1669 - constant 0- and 1- chunks of more than a full byte;
1670 - 0/1- and 1/0 transitions on all bit positions within a byte;
1671 - several sufficiently asymmetric bytes.
1673 static const gdb_byte data_lsb0[] = {
1674 0x00, 0xf8, 0xff, 0x12, 0xa5, 0x4f
1676 static const gdb_byte data_msb0[] = {
1677 0x00, 0x1f, 0xff, 0x48, 0xa5, 0xf2
1680 constexpr size_t data_nbits = 8 * sizeof (data_lsb0);
1681 constexpr unsigned max_nbits = 24;
1683 /* Try all combinations of:
1684 lsb0/msb0 bit order (using the respective data array)
1685 X [0, MAX_NBITS] copy bit width
1686 X feasible source offsets for the given copy bit width
1687 X feasible destination offsets
1689 for (int msb0 = 0; msb0 < 2; msb0++)
1691 const gdb_byte *data = msb0 ? data_msb0 : data_lsb0;
1693 for (unsigned int nbits = 1; nbits <= max_nbits; nbits++)
1695 const unsigned int max_offset = data_nbits - nbits;
1697 for (unsigned source_offset = 0;
1698 source_offset <= max_offset;
1701 for (unsigned dest_offset = 0;
1702 dest_offset <= max_offset;
1705 check_copy_bitwise (data + dest_offset / 8,
1707 data + source_offset / 8,
1714 /* Special cases: copy all, copy nothing. */
1715 check_copy_bitwise (data_lsb0, 0, data_msb0, 0, data_nbits, msb0);
1716 check_copy_bitwise (data_msb0, 0, data_lsb0, 0, data_nbits, msb0);
1717 check_copy_bitwise (data, data_nbits - 7, data, 9, 0, msb0);
1721 } /* namespace selftests */
1723 #endif /* GDB_SELF_TEST */
1726 read_pieced_value (struct value *v)
1730 ULONGEST bits_to_skip;
1732 struct piece_closure *c
1733 = (struct piece_closure *) value_computed_closure (v);
1734 struct frame_info *frame;
1736 size_t buffer_size = 0;
1737 std::vector<gdb_byte> buffer;
1739 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1741 /* VALUE_FRAME_ID is used instead of VALUE_NEXT_FRAME_ID here
1742 because FRAME is passed to get_frame_register_bytes(), which
1743 does its own "->next" operation. */
1744 frame = frame_find_by_id (VALUE_FRAME_ID (v));
1746 if (value_type (v) != value_enclosing_type (v))
1747 internal_error (__FILE__, __LINE__,
1748 _("Should not be able to create a lazy value with "
1749 "an enclosing type"));
1751 contents = value_contents_raw (v);
1752 bits_to_skip = 8 * value_offset (v);
1753 if (value_bitsize (v))
1755 bits_to_skip += value_bitpos (v);
1756 type_len = value_bitsize (v);
1759 type_len = 8 * TYPE_LENGTH (value_type (v));
1761 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1763 struct dwarf_expr_piece *p = &c->pieces[i];
1764 size_t this_size, this_size_bits;
1765 long dest_offset_bits, source_offset_bits, source_offset;
1766 const gdb_byte *intermediate_buffer;
1768 /* Compute size, source, and destination offsets for copying, in
1770 this_size_bits = p->size;
1771 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1773 bits_to_skip -= this_size_bits;
1776 if (bits_to_skip > 0)
1778 dest_offset_bits = 0;
1779 source_offset_bits = bits_to_skip;
1780 this_size_bits -= bits_to_skip;
1785 dest_offset_bits = offset;
1786 source_offset_bits = 0;
1788 if (this_size_bits > type_len - offset)
1789 this_size_bits = type_len - offset;
1791 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1792 source_offset = source_offset_bits / 8;
1793 if (buffer_size < this_size)
1795 buffer_size = this_size;
1796 buffer.reserve (buffer_size);
1798 intermediate_buffer = buffer.data ();
1800 /* Copy from the source to DEST_BUFFER. */
1801 switch (p->location)
1803 case DWARF_VALUE_REGISTER:
1805 struct gdbarch *arch = get_frame_arch (frame);
1806 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1808 LONGEST reg_offset = source_offset;
1810 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1811 && this_size < register_size (arch, gdb_regnum))
1813 /* Big-endian, and we want less than full size. */
1814 reg_offset = register_size (arch, gdb_regnum) - this_size;
1815 /* We want the lower-order THIS_SIZE_BITS of the bytes
1816 we extract from the register. */
1817 source_offset_bits += 8 * this_size - this_size_bits;
1820 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1821 this_size, buffer.data (),
1824 /* Just so garbage doesn't ever shine through. */
1825 memset (buffer.data (), 0, this_size);
1828 mark_value_bits_optimized_out (v, offset, this_size_bits);
1830 mark_value_bits_unavailable (v, offset, this_size_bits);
1835 case DWARF_VALUE_MEMORY:
1836 read_value_memory (v, offset,
1837 p->v.mem.in_stack_memory,
1838 p->v.mem.addr + source_offset,
1839 buffer.data (), this_size);
1842 case DWARF_VALUE_STACK:
1844 size_t n = this_size;
1846 if (n > c->addr_size - source_offset)
1847 n = (c->addr_size >= source_offset
1848 ? c->addr_size - source_offset
1856 const gdb_byte *val_bytes = value_contents_all (p->v.value);
1858 intermediate_buffer = val_bytes + source_offset;
1863 case DWARF_VALUE_LITERAL:
1865 size_t n = this_size;
1867 if (n > p->v.literal.length - source_offset)
1868 n = (p->v.literal.length >= source_offset
1869 ? p->v.literal.length - source_offset
1872 intermediate_buffer = p->v.literal.data + source_offset;
1876 /* These bits show up as zeros -- but do not cause the value
1877 to be considered optimized-out. */
1878 case DWARF_VALUE_IMPLICIT_POINTER:
1881 case DWARF_VALUE_OPTIMIZED_OUT:
1882 mark_value_bits_optimized_out (v, offset, this_size_bits);
1886 internal_error (__FILE__, __LINE__, _("invalid location type"));
1889 if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1890 && p->location != DWARF_VALUE_IMPLICIT_POINTER)
1891 copy_bitwise (contents, dest_offset_bits,
1892 intermediate_buffer, source_offset_bits % 8,
1893 this_size_bits, bits_big_endian);
1895 offset += this_size_bits;
1900 write_pieced_value (struct value *to, struct value *from)
1904 ULONGEST bits_to_skip;
1905 const gdb_byte *contents;
1906 struct piece_closure *c
1907 = (struct piece_closure *) value_computed_closure (to);
1908 struct frame_info *frame;
1910 size_t buffer_size = 0;
1911 std::vector<gdb_byte> buffer;
1913 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
1915 /* VALUE_FRAME_ID is used instead of VALUE_NEXT_FRAME_ID here
1916 because FRAME is passed to get_frame_register_bytes() and
1917 put_frame_register_bytes(), both of which do their own "->next"
1919 frame = frame_find_by_id (VALUE_FRAME_ID (to));
1922 mark_value_bytes_optimized_out (to, 0, TYPE_LENGTH (value_type (to)));
1926 contents = value_contents (from);
1927 bits_to_skip = 8 * value_offset (to);
1928 if (value_bitsize (to))
1930 bits_to_skip += value_bitpos (to);
1931 type_len = value_bitsize (to);
1934 type_len = 8 * TYPE_LENGTH (value_type (to));
1936 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1938 struct dwarf_expr_piece *p = &c->pieces[i];
1939 size_t this_size_bits, this_size;
1940 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1942 const gdb_byte *source_buffer;
1944 this_size_bits = p->size;
1945 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1947 bits_to_skip -= this_size_bits;
1950 if (this_size_bits > type_len - offset)
1951 this_size_bits = type_len - offset;
1952 if (bits_to_skip > 0)
1954 dest_offset_bits = bits_to_skip;
1955 source_offset_bits = 0;
1956 this_size_bits -= bits_to_skip;
1961 dest_offset_bits = 0;
1962 source_offset_bits = offset;
1965 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1966 source_offset = source_offset_bits / 8;
1967 dest_offset = dest_offset_bits / 8;
1968 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1970 source_buffer = contents + source_offset;
1975 if (buffer_size < this_size)
1977 buffer_size = this_size;
1978 buffer.reserve (buffer_size);
1980 source_buffer = buffer.data ();
1984 switch (p->location)
1986 case DWARF_VALUE_REGISTER:
1988 struct gdbarch *arch = get_frame_arch (frame);
1989 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1990 int reg_offset = dest_offset;
1992 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1993 && this_size <= register_size (arch, gdb_regnum))
1995 /* Big-endian, and we want less than full size. */
1996 reg_offset = register_size (arch, gdb_regnum) - this_size;
2003 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
2004 this_size, buffer.data (),
2008 throw_error (OPTIMIZED_OUT_ERROR,
2009 _("Can't do read-modify-write to "
2010 "update bitfield; containing word "
2011 "has been optimized out"));
2013 throw_error (NOT_AVAILABLE_ERROR,
2014 _("Can't do read-modify-write to update "
2015 "bitfield; containing word "
2018 copy_bitwise (buffer.data (), dest_offset_bits,
2019 contents, source_offset_bits,
2024 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
2025 this_size, source_buffer);
2028 case DWARF_VALUE_MEMORY:
2031 /* Only the first and last bytes can possibly have any
2033 read_memory (p->v.mem.addr + dest_offset, buffer.data (), 1);
2034 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
2035 &buffer[this_size - 1], 1);
2036 copy_bitwise (buffer.data (), dest_offset_bits,
2037 contents, source_offset_bits,
2042 write_memory (p->v.mem.addr + dest_offset,
2043 source_buffer, this_size);
2046 mark_value_bytes_optimized_out (to, 0, TYPE_LENGTH (value_type (to)));
2049 offset += this_size_bits;
2053 /* An implementation of an lval_funcs method to see whether a value is
2054 a synthetic pointer. */
2057 check_pieced_synthetic_pointer (const struct value *value, LONGEST bit_offset,
2060 struct piece_closure *c
2061 = (struct piece_closure *) value_computed_closure (value);
2064 bit_offset += 8 * value_offset (value);
2065 if (value_bitsize (value))
2066 bit_offset += value_bitpos (value);
2068 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2070 struct dwarf_expr_piece *p = &c->pieces[i];
2071 size_t this_size_bits = p->size;
2075 if (bit_offset >= this_size_bits)
2077 bit_offset -= this_size_bits;
2081 bit_length -= this_size_bits - bit_offset;
2085 bit_length -= this_size_bits;
2087 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2094 /* A wrapper function for get_frame_address_in_block. */
2097 get_frame_address_in_block_wrapper (void *baton)
2099 return get_frame_address_in_block ((struct frame_info *) baton);
2102 /* Fetch a DW_AT_const_value through a synthetic pointer. */
2104 static struct value *
2105 fetch_const_value_from_synthetic_pointer (sect_offset die, LONGEST byte_offset,
2106 struct dwarf2_per_cu_data *per_cu,
2109 struct value *result = NULL;
2110 struct obstack temp_obstack;
2111 struct cleanup *cleanup;
2112 const gdb_byte *bytes;
2115 obstack_init (&temp_obstack);
2116 cleanup = make_cleanup_obstack_free (&temp_obstack);
2117 bytes = dwarf2_fetch_constant_bytes (die, per_cu, &temp_obstack, &len);
2121 if (byte_offset >= 0
2122 && byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) <= len)
2124 bytes += byte_offset;
2125 result = value_from_contents (TYPE_TARGET_TYPE (type), bytes);
2128 invalid_synthetic_pointer ();
2131 result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type));
2133 do_cleanups (cleanup);
2138 /* Fetch the value pointed to by a synthetic pointer. */
2140 static struct value *
2141 indirect_synthetic_pointer (sect_offset die, LONGEST byte_offset,
2142 struct dwarf2_per_cu_data *per_cu,
2143 struct frame_info *frame, struct type *type)
2145 /* Fetch the location expression of the DIE we're pointing to. */
2146 struct dwarf2_locexpr_baton baton
2147 = dwarf2_fetch_die_loc_sect_off (die, per_cu,
2148 get_frame_address_in_block_wrapper, frame);
2150 /* If pointed-to DIE has a DW_AT_location, evaluate it and return the
2151 resulting value. Otherwise, it may have a DW_AT_const_value instead,
2152 or it may've been optimized out. */
2153 if (baton.data != NULL)
2154 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
2155 baton.data, baton.size, baton.per_cu,
2158 return fetch_const_value_from_synthetic_pointer (die, byte_offset, per_cu,
2162 /* An implementation of an lval_funcs method to indirect through a
2163 pointer. This handles the synthetic pointer case when needed. */
2165 static struct value *
2166 indirect_pieced_value (struct value *value)
2168 struct piece_closure *c
2169 = (struct piece_closure *) value_computed_closure (value);
2171 struct frame_info *frame;
2172 struct dwarf2_locexpr_baton baton;
2175 struct dwarf_expr_piece *piece = NULL;
2176 LONGEST byte_offset;
2177 enum bfd_endian byte_order;
2179 type = check_typedef (value_type (value));
2180 if (TYPE_CODE (type) != TYPE_CODE_PTR)
2183 bit_length = 8 * TYPE_LENGTH (type);
2184 bit_offset = 8 * value_offset (value);
2185 if (value_bitsize (value))
2186 bit_offset += value_bitpos (value);
2188 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2190 struct dwarf_expr_piece *p = &c->pieces[i];
2191 size_t this_size_bits = p->size;
2195 if (bit_offset >= this_size_bits)
2197 bit_offset -= this_size_bits;
2201 bit_length -= this_size_bits - bit_offset;
2205 bit_length -= this_size_bits;
2207 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2210 if (bit_length != 0)
2211 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
2217 gdb_assert (piece != NULL);
2218 frame = get_selected_frame (_("No frame selected."));
2220 /* This is an offset requested by GDB, such as value subscripts.
2221 However, due to how synthetic pointers are implemented, this is
2222 always presented to us as a pointer type. This means we have to
2223 sign-extend it manually as appropriate. Use raw
2224 extract_signed_integer directly rather than value_as_address and
2225 sign extend afterwards on architectures that would need it
2226 (mostly everywhere except MIPS, which has signed addresses) as
2227 the later would go through gdbarch_pointer_to_address and thus
2228 return a CORE_ADDR with high bits set on architectures that
2229 encode address spaces and other things in CORE_ADDR. */
2230 byte_order = gdbarch_byte_order (get_frame_arch (frame));
2231 byte_offset = extract_signed_integer (value_contents (value),
2232 TYPE_LENGTH (type), byte_order);
2233 byte_offset += piece->v.ptr.offset;
2235 return indirect_synthetic_pointer (piece->v.ptr.die, byte_offset, c->per_cu,
2239 /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
2242 static struct value *
2243 coerce_pieced_ref (const struct value *value)
2245 struct type *type = check_typedef (value_type (value));
2247 if (value_bits_synthetic_pointer (value, value_embedded_offset (value),
2248 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
2250 const struct piece_closure *closure
2251 = (struct piece_closure *) value_computed_closure (value);
2252 struct frame_info *frame
2253 = get_selected_frame (_("No frame selected."));
2255 /* gdb represents synthetic pointers as pieced values with a single
2257 gdb_assert (closure != NULL);
2258 gdb_assert (closure->n_pieces == 1);
2260 return indirect_synthetic_pointer (closure->pieces->v.ptr.die,
2261 closure->pieces->v.ptr.offset,
2262 closure->per_cu, frame, type);
2266 /* Else: not a synthetic reference; do nothing. */
2272 copy_pieced_value_closure (const struct value *v)
2274 struct piece_closure *c
2275 = (struct piece_closure *) value_computed_closure (v);
2282 free_pieced_value_closure (struct value *v)
2284 struct piece_closure *c
2285 = (struct piece_closure *) value_computed_closure (v);
2292 for (i = 0; i < c->n_pieces; ++i)
2293 if (c->pieces[i].location == DWARF_VALUE_STACK)
2294 value_free (c->pieces[i].v.value);
2301 /* Functions for accessing a variable described by DW_OP_piece. */
2302 static const struct lval_funcs pieced_value_funcs = {
2305 indirect_pieced_value,
2307 check_pieced_synthetic_pointer,
2308 copy_pieced_value_closure,
2309 free_pieced_value_closure
2312 /* Evaluate a location description, starting at DATA and with length
2313 SIZE, to find the current location of variable of TYPE in the
2314 context of FRAME. BYTE_OFFSET is applied after the contents are
2317 static struct value *
2318 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2319 const gdb_byte *data, size_t size,
2320 struct dwarf2_per_cu_data *per_cu,
2321 LONGEST byte_offset)
2323 struct value *retval;
2324 struct cleanup *value_chain;
2325 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2327 if (byte_offset < 0)
2328 invalid_synthetic_pointer ();
2331 return allocate_optimized_out_value (type);
2333 dwarf_evaluate_loc_desc ctx;
2335 ctx.per_cu = per_cu;
2336 ctx.obj_address = 0;
2338 value_chain = make_cleanup_value_free_to_mark (value_mark ());
2340 ctx.gdbarch = get_objfile_arch (objfile);
2341 ctx.addr_size = dwarf2_per_cu_addr_size (per_cu);
2342 ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2343 ctx.offset = dwarf2_per_cu_text_offset (per_cu);
2347 ctx.eval (data, size);
2349 CATCH (ex, RETURN_MASK_ERROR)
2351 if (ex.error == NOT_AVAILABLE_ERROR)
2353 do_cleanups (value_chain);
2354 retval = allocate_value (type);
2355 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
2358 else if (ex.error == NO_ENTRY_VALUE_ERROR)
2360 if (entry_values_debug)
2361 exception_print (gdb_stdout, ex);
2362 do_cleanups (value_chain);
2363 return allocate_optimized_out_value (type);
2366 throw_exception (ex);
2370 if (ctx.num_pieces > 0)
2372 struct piece_closure *c;
2373 struct frame_id frame_id
2376 : get_frame_id (get_next_frame_sentinel_okay (frame));
2377 ULONGEST bit_size = 0;
2380 for (i = 0; i < ctx.num_pieces; ++i)
2381 bit_size += ctx.pieces[i].size;
2382 if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
2383 invalid_synthetic_pointer ();
2385 c = allocate_piece_closure (per_cu, ctx.num_pieces, ctx.pieces,
2387 /* We must clean up the value chain after creating the piece
2388 closure but before allocating the result. */
2389 do_cleanups (value_chain);
2390 retval = allocate_computed_value (type, &pieced_value_funcs, c);
2391 VALUE_NEXT_FRAME_ID (retval) = frame_id;
2392 set_value_offset (retval, byte_offset);
2396 switch (ctx.location)
2398 case DWARF_VALUE_REGISTER:
2400 struct gdbarch *arch = get_frame_arch (frame);
2402 = longest_to_int (value_as_long (ctx.fetch (0)));
2403 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, dwarf_regnum);
2405 if (byte_offset != 0)
2406 error (_("cannot use offset on synthetic pointer to register"));
2407 do_cleanups (value_chain);
2408 retval = value_from_register (type, gdb_regnum, frame);
2409 if (value_optimized_out (retval))
2413 /* This means the register has undefined value / was
2414 not saved. As we're computing the location of some
2415 variable etc. in the program, not a value for
2416 inspecting a register ($pc, $sp, etc.), return a
2417 generic optimized out value instead, so that we show
2418 <optimized out> instead of <not saved>. */
2419 do_cleanups (value_chain);
2420 tmp = allocate_value (type);
2421 value_contents_copy (tmp, 0, retval, 0, TYPE_LENGTH (type));
2427 case DWARF_VALUE_MEMORY:
2429 struct type *ptr_type;
2430 CORE_ADDR address = ctx.fetch_address (0);
2431 int in_stack_memory = ctx.fetch_in_stack_memory (0);
2433 /* DW_OP_deref_size (and possibly other operations too) may
2434 create a pointer instead of an address. Ideally, the
2435 pointer to address conversion would be performed as part
2436 of those operations, but the type of the object to
2437 which the address refers is not known at the time of
2438 the operation. Therefore, we do the conversion here
2439 since the type is readily available. */
2441 switch (TYPE_CODE (type))
2443 case TYPE_CODE_FUNC:
2444 case TYPE_CODE_METHOD:
2445 ptr_type = builtin_type (ctx.gdbarch)->builtin_func_ptr;
2448 ptr_type = builtin_type (ctx.gdbarch)->builtin_data_ptr;
2451 address = value_as_address (value_from_pointer (ptr_type, address));
2453 do_cleanups (value_chain);
2454 retval = value_at_lazy (type, address + byte_offset);
2455 if (in_stack_memory)
2456 set_value_stack (retval, 1);
2460 case DWARF_VALUE_STACK:
2462 struct value *value = ctx.fetch (0);
2464 const gdb_byte *val_bytes;
2465 size_t n = TYPE_LENGTH (value_type (value));
2467 if (byte_offset + TYPE_LENGTH (type) > n)
2468 invalid_synthetic_pointer ();
2470 val_bytes = value_contents_all (value);
2471 val_bytes += byte_offset;
2474 /* Preserve VALUE because we are going to free values back
2475 to the mark, but we still need the value contents
2477 value_incref (value);
2478 do_cleanups (value_chain);
2479 make_cleanup_value_free (value);
2481 retval = allocate_value (type);
2482 contents = value_contents_raw (retval);
2483 if (n > TYPE_LENGTH (type))
2485 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2487 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2488 val_bytes += n - TYPE_LENGTH (type);
2489 n = TYPE_LENGTH (type);
2491 memcpy (contents, val_bytes, n);
2495 case DWARF_VALUE_LITERAL:
2498 const bfd_byte *ldata;
2501 if (byte_offset + TYPE_LENGTH (type) > n)
2502 invalid_synthetic_pointer ();
2504 do_cleanups (value_chain);
2505 retval = allocate_value (type);
2506 contents = value_contents_raw (retval);
2508 ldata = ctx.data + byte_offset;
2511 if (n > TYPE_LENGTH (type))
2513 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2515 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2516 ldata += n - TYPE_LENGTH (type);
2517 n = TYPE_LENGTH (type);
2519 memcpy (contents, ldata, n);
2523 case DWARF_VALUE_OPTIMIZED_OUT:
2524 do_cleanups (value_chain);
2525 retval = allocate_optimized_out_value (type);
2528 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2529 operation by execute_stack_op. */
2530 case DWARF_VALUE_IMPLICIT_POINTER:
2531 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2532 it can only be encountered when making a piece. */
2534 internal_error (__FILE__, __LINE__, _("invalid location type"));
2538 set_value_initialized (retval, ctx.initialized);
2540 do_cleanups (value_chain);
2545 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2546 passes 0 as the byte_offset. */
2549 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2550 const gdb_byte *data, size_t size,
2551 struct dwarf2_per_cu_data *per_cu)
2553 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2556 /* Evaluates a dwarf expression and stores the result in VAL, expecting
2557 that the dwarf expression only produces a single CORE_ADDR. FRAME is the
2558 frame in which the expression is evaluated. ADDR is a context (location of
2559 a variable) and might be needed to evaluate the location expression.
2560 Returns 1 on success, 0 otherwise. */
2563 dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton *dlbaton,
2564 struct frame_info *frame,
2568 struct objfile *objfile;
2569 struct cleanup *cleanup;
2571 if (dlbaton == NULL || dlbaton->size == 0)
2574 dwarf_evaluate_loc_desc ctx;
2577 ctx.per_cu = dlbaton->per_cu;
2578 ctx.obj_address = addr;
2580 objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2582 ctx.gdbarch = get_objfile_arch (objfile);
2583 ctx.addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2584 ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (dlbaton->per_cu);
2585 ctx.offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
2587 ctx.eval (dlbaton->data, dlbaton->size);
2589 switch (ctx.location)
2591 case DWARF_VALUE_REGISTER:
2592 case DWARF_VALUE_MEMORY:
2593 case DWARF_VALUE_STACK:
2594 *valp = ctx.fetch_address (0);
2595 if (ctx.location == DWARF_VALUE_REGISTER)
2596 *valp = ctx.read_addr_from_reg (*valp);
2598 case DWARF_VALUE_LITERAL:
2599 *valp = extract_signed_integer (ctx.data, ctx.len,
2600 gdbarch_byte_order (ctx.gdbarch));
2602 /* Unsupported dwarf values. */
2603 case DWARF_VALUE_OPTIMIZED_OUT:
2604 case DWARF_VALUE_IMPLICIT_POINTER:
2611 /* See dwarf2loc.h. */
2614 dwarf2_evaluate_property (const struct dynamic_prop *prop,
2615 struct frame_info *frame,
2616 struct property_addr_info *addr_stack,
2622 if (frame == NULL && has_stack_frames ())
2623 frame = get_selected_frame (NULL);
2629 const struct dwarf2_property_baton *baton
2630 = (const struct dwarf2_property_baton *) prop->data.baton;
2632 if (dwarf2_locexpr_baton_eval (&baton->locexpr, frame,
2633 addr_stack ? addr_stack->addr : 0,
2636 if (baton->referenced_type)
2638 struct value *val = value_at (baton->referenced_type, *value);
2640 *value = value_as_address (val);
2649 struct dwarf2_property_baton *baton
2650 = (struct dwarf2_property_baton *) prop->data.baton;
2651 CORE_ADDR pc = get_frame_address_in_block (frame);
2652 const gdb_byte *data;
2656 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2659 val = dwarf2_evaluate_loc_desc (baton->referenced_type, frame, data,
2660 size, baton->loclist.per_cu);
2661 if (!value_optimized_out (val))
2663 *value = value_as_address (val);
2671 *value = prop->data.const_val;
2674 case PROP_ADDR_OFFSET:
2676 struct dwarf2_property_baton *baton
2677 = (struct dwarf2_property_baton *) prop->data.baton;
2678 struct property_addr_info *pinfo;
2681 for (pinfo = addr_stack; pinfo != NULL; pinfo = pinfo->next)
2682 if (pinfo->type == baton->referenced_type)
2685 error (_("cannot find reference address for offset property"));
2686 if (pinfo->valaddr != NULL)
2687 val = value_from_contents
2688 (baton->offset_info.type,
2689 pinfo->valaddr + baton->offset_info.offset);
2691 val = value_at (baton->offset_info.type,
2692 pinfo->addr + baton->offset_info.offset);
2693 *value = value_as_address (val);
2701 /* See dwarf2loc.h. */
2704 dwarf2_compile_property_to_c (struct ui_file *stream,
2705 const char *result_name,
2706 struct gdbarch *gdbarch,
2707 unsigned char *registers_used,
2708 const struct dynamic_prop *prop,
2712 struct dwarf2_property_baton *baton
2713 = (struct dwarf2_property_baton *) prop->data.baton;
2714 const gdb_byte *data;
2716 struct dwarf2_per_cu_data *per_cu;
2718 if (prop->kind == PROP_LOCEXPR)
2720 data = baton->locexpr.data;
2721 size = baton->locexpr.size;
2722 per_cu = baton->locexpr.per_cu;
2726 gdb_assert (prop->kind == PROP_LOCLIST);
2728 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2729 per_cu = baton->loclist.per_cu;
2732 compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
2733 gdbarch, registers_used,
2734 dwarf2_per_cu_addr_size (per_cu),
2735 data, data + size, per_cu);
2739 /* Helper functions and baton for dwarf2_loc_desc_get_symbol_read_needs. */
2741 class symbol_needs_eval_context : public dwarf_expr_context
2745 enum symbol_needs_kind needs;
2746 struct dwarf2_per_cu_data *per_cu;
2748 /* Reads from registers do require a frame. */
2749 CORE_ADDR read_addr_from_reg (int regnum) OVERRIDE
2751 needs = SYMBOL_NEEDS_FRAME;
2755 /* "get_reg_value" callback: Reads from registers do require a
2758 struct value *get_reg_value (struct type *type, int regnum) OVERRIDE
2760 needs = SYMBOL_NEEDS_FRAME;
2761 return value_zero (type, not_lval);
2764 /* Reads from memory do not require a frame. */
2765 void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t len) OVERRIDE
2767 memset (buf, 0, len);
2770 /* Frame-relative accesses do require a frame. */
2771 void get_frame_base (const gdb_byte **start, size_t *length) OVERRIDE
2773 static gdb_byte lit0 = DW_OP_lit0;
2778 needs = SYMBOL_NEEDS_FRAME;
2781 /* CFA accesses require a frame. */
2782 CORE_ADDR get_frame_cfa () OVERRIDE
2784 needs = SYMBOL_NEEDS_FRAME;
2788 CORE_ADDR get_frame_pc () OVERRIDE
2790 needs = SYMBOL_NEEDS_FRAME;
2794 /* Thread-local accesses require registers, but not a frame. */
2795 CORE_ADDR get_tls_address (CORE_ADDR offset) OVERRIDE
2797 if (needs <= SYMBOL_NEEDS_REGISTERS)
2798 needs = SYMBOL_NEEDS_REGISTERS;
2802 /* Helper interface of per_cu_dwarf_call for
2803 dwarf2_loc_desc_get_symbol_read_needs. */
2805 void dwarf_call (cu_offset die_offset) OVERRIDE
2807 per_cu_dwarf_call (this, die_offset, per_cu);
2810 /* DW_OP_GNU_entry_value accesses require a caller, therefore a
2813 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind,
2814 union call_site_parameter_u kind_u,
2815 int deref_size) OVERRIDE
2817 needs = SYMBOL_NEEDS_FRAME;
2819 /* The expression may require some stub values on DWARF stack. */
2820 push_address (0, 0);
2823 /* DW_OP_GNU_addr_index doesn't require a frame. */
2825 CORE_ADDR get_addr_index (unsigned int index) OVERRIDE
2827 /* Nothing to do. */
2831 /* DW_OP_push_object_address has a frame already passed through. */
2833 CORE_ADDR get_object_address () OVERRIDE
2835 /* Nothing to do. */
2840 /* Compute the correct symbol_needs_kind value for the location
2841 expression at DATA (length SIZE). */
2843 static enum symbol_needs_kind
2844 dwarf2_loc_desc_get_symbol_read_needs (const gdb_byte *data, size_t size,
2845 struct dwarf2_per_cu_data *per_cu)
2848 struct cleanup *old_chain;
2849 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2851 symbol_needs_eval_context ctx;
2853 ctx.needs = SYMBOL_NEEDS_NONE;
2854 ctx.per_cu = per_cu;
2856 old_chain = make_cleanup_value_free_to_mark (value_mark ());
2858 ctx.gdbarch = get_objfile_arch (objfile);
2859 ctx.addr_size = dwarf2_per_cu_addr_size (per_cu);
2860 ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2861 ctx.offset = dwarf2_per_cu_text_offset (per_cu);
2863 ctx.eval (data, size);
2865 in_reg = ctx.location == DWARF_VALUE_REGISTER;
2867 if (ctx.num_pieces > 0)
2871 /* If the location has several pieces, and any of them are in
2872 registers, then we will need a frame to fetch them from. */
2873 for (i = 0; i < ctx.num_pieces; i++)
2874 if (ctx.pieces[i].location == DWARF_VALUE_REGISTER)
2878 do_cleanups (old_chain);
2881 ctx.needs = SYMBOL_NEEDS_FRAME;
2885 /* A helper function that throws an unimplemented error mentioning a
2886 given DWARF operator. */
2889 unimplemented (unsigned int op)
2891 const char *name = get_DW_OP_name (op);
2894 error (_("DWARF operator %s cannot be translated to an agent expression"),
2897 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2898 "to an agent expression"),
2904 This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2905 can issue a complaint, which is better than having every target's
2906 implementation of dwarf2_reg_to_regnum do it. */
2909 dwarf_reg_to_regnum (struct gdbarch *arch, int dwarf_reg)
2911 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2915 complaint (&symfile_complaints,
2916 _("bad DWARF register number %d"), dwarf_reg);
2921 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2922 Throw an error because DWARF_REG is bad. */
2925 throw_bad_regnum_error (ULONGEST dwarf_reg)
2927 /* Still want to print -1 as "-1".
2928 We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2929 but that's overkill for now. */
2930 if ((int) dwarf_reg == dwarf_reg)
2931 error (_("Unable to access DWARF register number %d"), (int) dwarf_reg);
2932 error (_("Unable to access DWARF register number %s"),
2933 pulongest (dwarf_reg));
2936 /* See dwarf2loc.h. */
2939 dwarf_reg_to_regnum_or_error (struct gdbarch *arch, ULONGEST dwarf_reg)
2943 if (dwarf_reg > INT_MAX)
2944 throw_bad_regnum_error (dwarf_reg);
2945 /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2946 bad, but that's ok. */
2947 reg = dwarf_reg_to_regnum (arch, (int) dwarf_reg);
2949 throw_bad_regnum_error (dwarf_reg);
2953 /* A helper function that emits an access to memory. ARCH is the
2954 target architecture. EXPR is the expression which we are building.
2955 NBITS is the number of bits we want to read. This emits the
2956 opcodes needed to read the memory and then extract the desired
2960 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2962 ULONGEST nbytes = (nbits + 7) / 8;
2964 gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
2967 ax_trace_quick (expr, nbytes);
2970 ax_simple (expr, aop_ref8);
2971 else if (nbits <= 16)
2972 ax_simple (expr, aop_ref16);
2973 else if (nbits <= 32)
2974 ax_simple (expr, aop_ref32);
2976 ax_simple (expr, aop_ref64);
2978 /* If we read exactly the number of bytes we wanted, we're done. */
2979 if (8 * nbytes == nbits)
2982 if (gdbarch_bits_big_endian (arch))
2984 /* On a bits-big-endian machine, we want the high-order
2986 ax_const_l (expr, 8 * nbytes - nbits);
2987 ax_simple (expr, aop_rsh_unsigned);
2991 /* On a bits-little-endian box, we want the low-order NBITS. */
2992 ax_zero_ext (expr, nbits);
2996 /* A helper function to return the frame's PC. */
2999 get_ax_pc (void *baton)
3001 struct agent_expr *expr = (struct agent_expr *) baton;
3006 /* Compile a DWARF location expression to an agent expression.
3008 EXPR is the agent expression we are building.
3009 LOC is the agent value we modify.
3010 ARCH is the architecture.
3011 ADDR_SIZE is the size of addresses, in bytes.
3012 OP_PTR is the start of the location expression.
3013 OP_END is one past the last byte of the location expression.
3015 This will throw an exception for various kinds of errors -- for
3016 example, if the expression cannot be compiled, or if the expression
3020 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
3021 struct gdbarch *arch, unsigned int addr_size,
3022 const gdb_byte *op_ptr, const gdb_byte *op_end,
3023 struct dwarf2_per_cu_data *per_cu)
3026 std::vector<int> dw_labels, patches;
3027 const gdb_byte * const base = op_ptr;
3028 const gdb_byte *previous_piece = op_ptr;
3029 enum bfd_endian byte_order = gdbarch_byte_order (arch);
3030 ULONGEST bits_collected = 0;
3031 unsigned int addr_size_bits = 8 * addr_size;
3032 int bits_big_endian = gdbarch_bits_big_endian (arch);
3034 std::vector<int> offsets (op_end - op_ptr, -1);
3036 /* By default we are making an address. */
3037 loc->kind = axs_lvalue_memory;
3039 while (op_ptr < op_end)
3041 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
3042 uint64_t uoffset, reg;
3046 offsets[op_ptr - base] = expr->len;
3049 /* Our basic approach to code generation is to map DWARF
3050 operations directly to AX operations. However, there are
3053 First, DWARF works on address-sized units, but AX always uses
3054 LONGEST. For most operations we simply ignore this
3055 difference; instead we generate sign extensions as needed
3056 before division and comparison operations. It would be nice
3057 to omit the sign extensions, but there is no way to determine
3058 the size of the target's LONGEST. (This code uses the size
3059 of the host LONGEST in some cases -- that is a bug but it is
3062 Second, some DWARF operations cannot be translated to AX.
3063 For these we simply fail. See
3064 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
3099 ax_const_l (expr, op - DW_OP_lit0);
3103 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
3104 op_ptr += addr_size;
3105 /* Some versions of GCC emit DW_OP_addr before
3106 DW_OP_GNU_push_tls_address. In this case the value is an
3107 index, not an address. We don't support things like
3108 branching between the address and the TLS op. */
3109 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
3110 uoffset += dwarf2_per_cu_text_offset (per_cu);
3111 ax_const_l (expr, uoffset);
3115 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
3119 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
3123 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
3127 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
3131 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
3135 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
3139 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
3143 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
3147 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3148 ax_const_l (expr, uoffset);
3151 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3152 ax_const_l (expr, offset);
3187 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3188 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
3189 loc->kind = axs_lvalue_register;
3193 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3194 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3195 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, reg);
3196 loc->kind = axs_lvalue_register;
3199 case DW_OP_implicit_value:
3203 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
3204 if (op_ptr + len > op_end)
3205 error (_("DW_OP_implicit_value: too few bytes available."));
3206 if (len > sizeof (ULONGEST))
3207 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3210 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
3213 dwarf_expr_require_composition (op_ptr, op_end,
3214 "DW_OP_implicit_value");
3216 loc->kind = axs_rvalue;
3220 case DW_OP_stack_value:
3221 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
3222 loc->kind = axs_rvalue;
3257 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3258 i = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
3262 ax_const_l (expr, offset);
3263 ax_simple (expr, aop_add);
3268 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3269 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3270 i = dwarf_reg_to_regnum_or_error (arch, reg);
3274 ax_const_l (expr, offset);
3275 ax_simple (expr, aop_add);
3281 const gdb_byte *datastart;
3283 const struct block *b;
3284 struct symbol *framefunc;
3286 b = block_for_pc (expr->scope);
3289 error (_("No block found for address"));
3291 framefunc = block_linkage_function (b);
3294 error (_("No function found for block"));
3296 func_get_frame_base_dwarf_block (framefunc, expr->scope,
3297 &datastart, &datalen);
3299 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3300 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
3301 datastart + datalen, per_cu);
3302 if (loc->kind == axs_lvalue_register)
3303 require_rvalue (expr, loc);
3307 ax_const_l (expr, offset);
3308 ax_simple (expr, aop_add);
3311 loc->kind = axs_lvalue_memory;
3316 ax_simple (expr, aop_dup);
3320 ax_simple (expr, aop_pop);
3325 ax_pick (expr, offset);
3329 ax_simple (expr, aop_swap);
3337 ax_simple (expr, aop_rot);
3341 case DW_OP_deref_size:
3345 if (op == DW_OP_deref_size)
3350 if (size != 1 && size != 2 && size != 4 && size != 8)
3351 error (_("Unsupported size %d in %s"),
3352 size, get_DW_OP_name (op));
3353 access_memory (arch, expr, size * TARGET_CHAR_BIT);
3358 /* Sign extend the operand. */
3359 ax_ext (expr, addr_size_bits);
3360 ax_simple (expr, aop_dup);
3361 ax_const_l (expr, 0);
3362 ax_simple (expr, aop_less_signed);
3363 ax_simple (expr, aop_log_not);
3364 i = ax_goto (expr, aop_if_goto);
3365 /* We have to emit 0 - X. */
3366 ax_const_l (expr, 0);
3367 ax_simple (expr, aop_swap);
3368 ax_simple (expr, aop_sub);
3369 ax_label (expr, i, expr->len);
3373 /* No need to sign extend here. */
3374 ax_const_l (expr, 0);
3375 ax_simple (expr, aop_swap);
3376 ax_simple (expr, aop_sub);
3380 /* Sign extend the operand. */
3381 ax_ext (expr, addr_size_bits);
3382 ax_simple (expr, aop_bit_not);
3385 case DW_OP_plus_uconst:
3386 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3387 /* It would be really weird to emit `DW_OP_plus_uconst 0',
3388 but we micro-optimize anyhow. */
3391 ax_const_l (expr, reg);
3392 ax_simple (expr, aop_add);
3397 ax_simple (expr, aop_bit_and);
3401 /* Sign extend the operands. */
3402 ax_ext (expr, addr_size_bits);
3403 ax_simple (expr, aop_swap);
3404 ax_ext (expr, addr_size_bits);
3405 ax_simple (expr, aop_swap);
3406 ax_simple (expr, aop_div_signed);
3410 ax_simple (expr, aop_sub);
3414 ax_simple (expr, aop_rem_unsigned);
3418 ax_simple (expr, aop_mul);
3422 ax_simple (expr, aop_bit_or);
3426 ax_simple (expr, aop_add);
3430 ax_simple (expr, aop_lsh);
3434 ax_simple (expr, aop_rsh_unsigned);
3438 ax_simple (expr, aop_rsh_signed);
3442 ax_simple (expr, aop_bit_xor);
3446 /* Sign extend the operands. */
3447 ax_ext (expr, addr_size_bits);
3448 ax_simple (expr, aop_swap);
3449 ax_ext (expr, addr_size_bits);
3450 /* Note no swap here: A <= B is !(B < A). */
3451 ax_simple (expr, aop_less_signed);
3452 ax_simple (expr, aop_log_not);
3456 /* Sign extend the operands. */
3457 ax_ext (expr, addr_size_bits);
3458 ax_simple (expr, aop_swap);
3459 ax_ext (expr, addr_size_bits);
3460 ax_simple (expr, aop_swap);
3461 /* A >= B is !(A < B). */
3462 ax_simple (expr, aop_less_signed);
3463 ax_simple (expr, aop_log_not);
3467 /* Sign extend the operands. */
3468 ax_ext (expr, addr_size_bits);
3469 ax_simple (expr, aop_swap);
3470 ax_ext (expr, addr_size_bits);
3471 /* No need for a second swap here. */
3472 ax_simple (expr, aop_equal);
3476 /* Sign extend the operands. */
3477 ax_ext (expr, addr_size_bits);
3478 ax_simple (expr, aop_swap);
3479 ax_ext (expr, addr_size_bits);
3480 ax_simple (expr, aop_swap);
3481 ax_simple (expr, aop_less_signed);
3485 /* Sign extend the operands. */
3486 ax_ext (expr, addr_size_bits);
3487 ax_simple (expr, aop_swap);
3488 ax_ext (expr, addr_size_bits);
3489 /* Note no swap here: A > B is B < A. */
3490 ax_simple (expr, aop_less_signed);
3494 /* Sign extend the operands. */
3495 ax_ext (expr, addr_size_bits);
3496 ax_simple (expr, aop_swap);
3497 ax_ext (expr, addr_size_bits);
3498 /* No need for a swap here. */
3499 ax_simple (expr, aop_equal);
3500 ax_simple (expr, aop_log_not);
3503 case DW_OP_call_frame_cfa:
3506 CORE_ADDR text_offset;
3508 const gdb_byte *cfa_start, *cfa_end;
3510 if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
3512 &text_offset, &cfa_start, &cfa_end))
3515 ax_reg (expr, regnum);
3518 ax_const_l (expr, off);
3519 ax_simple (expr, aop_add);
3524 /* Another expression. */
3525 ax_const_l (expr, text_offset);
3526 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3527 cfa_start, cfa_end, per_cu);
3530 loc->kind = axs_lvalue_memory;
3534 case DW_OP_GNU_push_tls_address:
3535 case DW_OP_form_tls_address:
3539 case DW_OP_push_object_address:
3544 offset = extract_signed_integer (op_ptr, 2, byte_order);
3546 i = ax_goto (expr, aop_goto);
3547 dw_labels.push_back (op_ptr + offset - base);
3548 patches.push_back (i);
3552 offset = extract_signed_integer (op_ptr, 2, byte_order);
3554 /* Zero extend the operand. */
3555 ax_zero_ext (expr, addr_size_bits);
3556 i = ax_goto (expr, aop_if_goto);
3557 dw_labels.push_back (op_ptr + offset - base);
3558 patches.push_back (i);
3565 case DW_OP_bit_piece:
3567 uint64_t size, offset;
3569 if (op_ptr - 1 == previous_piece)
3570 error (_("Cannot translate empty pieces to agent expressions"));
3571 previous_piece = op_ptr - 1;
3573 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3574 if (op == DW_OP_piece)
3580 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
3582 if (bits_collected + size > 8 * sizeof (LONGEST))
3583 error (_("Expression pieces exceed word size"));
3585 /* Access the bits. */
3588 case axs_lvalue_register:
3589 ax_reg (expr, loc->u.reg);
3592 case axs_lvalue_memory:
3593 /* Offset the pointer, if needed. */
3596 ax_const_l (expr, offset / 8);
3597 ax_simple (expr, aop_add);
3600 access_memory (arch, expr, size);
3604 /* For a bits-big-endian target, shift up what we already
3605 have. For a bits-little-endian target, shift up the
3606 new data. Note that there is a potential bug here if
3607 the DWARF expression leaves multiple values on the
3609 if (bits_collected > 0)
3611 if (bits_big_endian)
3613 ax_simple (expr, aop_swap);
3614 ax_const_l (expr, size);
3615 ax_simple (expr, aop_lsh);
3616 /* We don't need a second swap here, because
3617 aop_bit_or is symmetric. */
3621 ax_const_l (expr, size);
3622 ax_simple (expr, aop_lsh);
3624 ax_simple (expr, aop_bit_or);
3627 bits_collected += size;
3628 loc->kind = axs_rvalue;
3632 case DW_OP_GNU_uninit:
3638 struct dwarf2_locexpr_baton block;
3639 int size = (op == DW_OP_call2 ? 2 : 4);
3642 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3645 offset.cu_off = uoffset;
3646 block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
3649 /* DW_OP_call_ref is currently not supported. */
3650 gdb_assert (block.per_cu == per_cu);
3652 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3653 block.data, block.data + block.size,
3658 case DW_OP_call_ref:
3666 /* Patch all the branches we emitted. */
3667 for (i = 0; i < patches.size (); ++i)
3669 int targ = offsets[dw_labels[i]];
3671 internal_error (__FILE__, __LINE__, _("invalid label"));
3672 ax_label (expr, patches[i], targ);
3677 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3678 evaluator to calculate the location. */
3679 static struct value *
3680 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3682 struct dwarf2_locexpr_baton *dlbaton
3683 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3686 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3687 dlbaton->size, dlbaton->per_cu);
3692 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3693 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3696 static struct value *
3697 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3699 struct dwarf2_locexpr_baton *dlbaton
3700 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3702 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3706 /* Implementation of get_symbol_read_needs from
3707 symbol_computed_ops. */
3709 static enum symbol_needs_kind
3710 locexpr_get_symbol_read_needs (struct symbol *symbol)
3712 struct dwarf2_locexpr_baton *dlbaton
3713 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3715 return dwarf2_loc_desc_get_symbol_read_needs (dlbaton->data, dlbaton->size,
3719 /* Return true if DATA points to the end of a piece. END is one past
3720 the last byte in the expression. */
3723 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3725 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3728 /* Helper for locexpr_describe_location_piece that finds the name of a
3732 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3736 /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3737 We'd rather print *something* here than throw an error. */
3738 regnum = dwarf_reg_to_regnum (gdbarch, dwarf_regnum);
3739 /* gdbarch_register_name may just return "", return something more
3740 descriptive for bad register numbers. */
3743 /* The text is output as "$bad_register_number".
3744 That is why we use the underscores. */
3745 return _("bad_register_number");
3747 return gdbarch_register_name (gdbarch, regnum);
3750 /* Nicely describe a single piece of a location, returning an updated
3751 position in the bytecode sequence. This function cannot recognize
3752 all locations; if a location is not recognized, it simply returns
3753 DATA. If there is an error during reading, e.g. we run off the end
3754 of the buffer, an error is thrown. */
3756 static const gdb_byte *
3757 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3758 CORE_ADDR addr, struct objfile *objfile,
3759 struct dwarf2_per_cu_data *per_cu,
3760 const gdb_byte *data, const gdb_byte *end,
3761 unsigned int addr_size)
3763 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3766 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3768 fprintf_filtered (stream, _("a variable in $%s"),
3769 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3772 else if (data[0] == DW_OP_regx)
3776 data = safe_read_uleb128 (data + 1, end, ®);
3777 fprintf_filtered (stream, _("a variable in $%s"),
3778 locexpr_regname (gdbarch, reg));
3780 else if (data[0] == DW_OP_fbreg)
3782 const struct block *b;
3783 struct symbol *framefunc;
3785 int64_t frame_offset;
3786 const gdb_byte *base_data, *new_data, *save_data = data;
3788 int64_t base_offset = 0;
3790 new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3791 if (!piece_end_p (new_data, end))
3795 b = block_for_pc (addr);
3798 error (_("No block found for address for symbol \"%s\"."),
3799 SYMBOL_PRINT_NAME (symbol));
3801 framefunc = block_linkage_function (b);
3804 error (_("No function found for block for symbol \"%s\"."),
3805 SYMBOL_PRINT_NAME (symbol));
3807 func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
3809 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3811 const gdb_byte *buf_end;
3813 frame_reg = base_data[0] - DW_OP_breg0;
3814 buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3816 if (buf_end != base_data + base_size)
3817 error (_("Unexpected opcode after "
3818 "DW_OP_breg%u for symbol \"%s\"."),
3819 frame_reg, SYMBOL_PRINT_NAME (symbol));
3821 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3823 /* The frame base is just the register, with no offset. */
3824 frame_reg = base_data[0] - DW_OP_reg0;
3829 /* We don't know what to do with the frame base expression,
3830 so we can't trace this variable; give up. */
3834 fprintf_filtered (stream,
3835 _("a variable at frame base reg $%s offset %s+%s"),
3836 locexpr_regname (gdbarch, frame_reg),
3837 plongest (base_offset), plongest (frame_offset));
3839 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3840 && piece_end_p (data, end))
3844 data = safe_read_sleb128 (data + 1, end, &offset);
3846 fprintf_filtered (stream,
3847 _("a variable at offset %s from base reg $%s"),
3849 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3852 /* The location expression for a TLS variable looks like this (on a
3855 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3856 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3858 0x3 is the encoding for DW_OP_addr, which has an operand as long
3859 as the size of an address on the target machine (here is 8
3860 bytes). Note that more recent version of GCC emit DW_OP_const4u
3861 or DW_OP_const8u, depending on address size, rather than
3862 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3863 The operand represents the offset at which the variable is within
3864 the thread local storage. */
3866 else if (data + 1 + addr_size < end
3867 && (data[0] == DW_OP_addr
3868 || (addr_size == 4 && data[0] == DW_OP_const4u)
3869 || (addr_size == 8 && data[0] == DW_OP_const8u))
3870 && (data[1 + addr_size] == DW_OP_GNU_push_tls_address
3871 || data[1 + addr_size] == DW_OP_form_tls_address)
3872 && piece_end_p (data + 2 + addr_size, end))
3875 offset = extract_unsigned_integer (data + 1, addr_size,
3876 gdbarch_byte_order (gdbarch));
3878 fprintf_filtered (stream,
3879 _("a thread-local variable at offset 0x%s "
3880 "in the thread-local storage for `%s'"),
3881 phex_nz (offset, addr_size), objfile_name (objfile));
3883 data += 1 + addr_size + 1;
3886 /* With -gsplit-dwarf a TLS variable can also look like this:
3887 DW_AT_location : 3 byte block: fc 4 e0
3888 (DW_OP_GNU_const_index: 4;
3889 DW_OP_GNU_push_tls_address) */
3890 else if (data + 3 <= end
3891 && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3892 && data[0] == DW_OP_GNU_const_index
3894 && (data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3895 || data[1 + leb128_size] == DW_OP_form_tls_address)
3896 && piece_end_p (data + 2 + leb128_size, end))
3900 data = safe_read_uleb128 (data + 1, end, &offset);
3901 offset = dwarf2_read_addr_index (per_cu, offset);
3902 fprintf_filtered (stream,
3903 _("a thread-local variable at offset 0x%s "
3904 "in the thread-local storage for `%s'"),
3905 phex_nz (offset, addr_size), objfile_name (objfile));
3909 else if (data[0] >= DW_OP_lit0
3910 && data[0] <= DW_OP_lit31
3912 && data[1] == DW_OP_stack_value)
3914 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3921 /* Disassemble an expression, stopping at the end of a piece or at the
3922 end of the expression. Returns a pointer to the next unread byte
3923 in the input expression. If ALL is nonzero, then this function
3924 will keep going until it reaches the end of the expression.
3925 If there is an error during reading, e.g. we run off the end
3926 of the buffer, an error is thrown. */
3928 static const gdb_byte *
3929 disassemble_dwarf_expression (struct ui_file *stream,
3930 struct gdbarch *arch, unsigned int addr_size,
3931 int offset_size, const gdb_byte *start,
3932 const gdb_byte *data, const gdb_byte *end,
3933 int indent, int all,
3934 struct dwarf2_per_cu_data *per_cu)
3938 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3940 enum dwarf_location_atom op = (enum dwarf_location_atom) *data++;
3945 name = get_DW_OP_name (op);
3948 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3949 op, (long) (data - 1 - start));
3950 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3951 (long) (data - 1 - start), name);
3956 ul = extract_unsigned_integer (data, addr_size,
3957 gdbarch_byte_order (arch));
3959 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3963 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3965 fprintf_filtered (stream, " %s", pulongest (ul));
3968 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3970 fprintf_filtered (stream, " %s", plongest (l));
3973 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3975 fprintf_filtered (stream, " %s", pulongest (ul));
3978 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3980 fprintf_filtered (stream, " %s", plongest (l));
3983 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3985 fprintf_filtered (stream, " %s", pulongest (ul));
3988 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3990 fprintf_filtered (stream, " %s", plongest (l));
3993 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3995 fprintf_filtered (stream, " %s", pulongest (ul));
3998 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
4000 fprintf_filtered (stream, " %s", plongest (l));
4003 data = safe_read_uleb128 (data, end, &ul);
4004 fprintf_filtered (stream, " %s", pulongest (ul));
4007 data = safe_read_sleb128 (data, end, &l);
4008 fprintf_filtered (stream, " %s", plongest (l));
4043 fprintf_filtered (stream, " [$%s]",
4044 locexpr_regname (arch, op - DW_OP_reg0));
4048 data = safe_read_uleb128 (data, end, &ul);
4049 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
4050 locexpr_regname (arch, (int) ul));
4053 case DW_OP_implicit_value:
4054 data = safe_read_uleb128 (data, end, &ul);
4056 fprintf_filtered (stream, " %s", pulongest (ul));
4091 data = safe_read_sleb128 (data, end, &l);
4092 fprintf_filtered (stream, " %s [$%s]", plongest (l),
4093 locexpr_regname (arch, op - DW_OP_breg0));
4097 data = safe_read_uleb128 (data, end, &ul);
4098 data = safe_read_sleb128 (data, end, &l);
4099 fprintf_filtered (stream, " register %s [$%s] offset %s",
4101 locexpr_regname (arch, (int) ul),
4106 data = safe_read_sleb128 (data, end, &l);
4107 fprintf_filtered (stream, " %s", plongest (l));
4110 case DW_OP_xderef_size:
4111 case DW_OP_deref_size:
4113 fprintf_filtered (stream, " %d", *data);
4117 case DW_OP_plus_uconst:
4118 data = safe_read_uleb128 (data, end, &ul);
4119 fprintf_filtered (stream, " %s", pulongest (ul));
4123 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4125 fprintf_filtered (stream, " to %ld",
4126 (long) (data + l - start));
4130 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4132 fprintf_filtered (stream, " %ld",
4133 (long) (data + l - start));
4137 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
4139 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
4143 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4145 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4148 case DW_OP_call_ref:
4149 ul = extract_unsigned_integer (data, offset_size,
4150 gdbarch_byte_order (arch));
4151 data += offset_size;
4152 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4156 data = safe_read_uleb128 (data, end, &ul);
4157 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
4160 case DW_OP_bit_piece:
4164 data = safe_read_uleb128 (data, end, &ul);
4165 data = safe_read_uleb128 (data, end, &offset);
4166 fprintf_filtered (stream, " size %s offset %s (bits)",
4167 pulongest (ul), pulongest (offset));
4171 case DW_OP_GNU_implicit_pointer:
4173 ul = extract_unsigned_integer (data, offset_size,
4174 gdbarch_byte_order (arch));
4175 data += offset_size;
4177 data = safe_read_sleb128 (data, end, &l);
4179 fprintf_filtered (stream, " DIE %s offset %s",
4180 phex_nz (ul, offset_size),
4185 case DW_OP_GNU_deref_type:
4187 int addr_size = *data++;
4191 data = safe_read_uleb128 (data, end, &ul);
4193 type = dwarf2_get_die_type (offset, per_cu);
4194 fprintf_filtered (stream, "<");
4195 type_print (type, "", stream, -1);
4196 fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
4201 case DW_OP_GNU_const_type:
4206 data = safe_read_uleb128 (data, end, &ul);
4207 type_die.cu_off = ul;
4208 type = dwarf2_get_die_type (type_die, per_cu);
4209 fprintf_filtered (stream, "<");
4210 type_print (type, "", stream, -1);
4211 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
4215 case DW_OP_GNU_regval_type:
4221 data = safe_read_uleb128 (data, end, ®);
4222 data = safe_read_uleb128 (data, end, &ul);
4223 type_die.cu_off = ul;
4225 type = dwarf2_get_die_type (type_die, per_cu);
4226 fprintf_filtered (stream, "<");
4227 type_print (type, "", stream, -1);
4228 fprintf_filtered (stream, " [0x%s]> [$%s]",
4229 phex_nz (type_die.cu_off, 0),
4230 locexpr_regname (arch, reg));
4234 case DW_OP_GNU_convert:
4235 case DW_OP_GNU_reinterpret:
4239 data = safe_read_uleb128 (data, end, &ul);
4240 type_die.cu_off = ul;
4242 if (type_die.cu_off == 0)
4243 fprintf_filtered (stream, "<0>");
4248 type = dwarf2_get_die_type (type_die, per_cu);
4249 fprintf_filtered (stream, "<");
4250 type_print (type, "", stream, -1);
4251 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
4256 case DW_OP_GNU_entry_value:
4257 data = safe_read_uleb128 (data, end, &ul);
4258 fputc_filtered ('\n', stream);
4259 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
4260 start, data, data + ul, indent + 2,
4265 case DW_OP_GNU_parameter_ref:
4266 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4268 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4271 case DW_OP_GNU_addr_index:
4272 data = safe_read_uleb128 (data, end, &ul);
4273 ul = dwarf2_read_addr_index (per_cu, ul);
4274 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
4276 case DW_OP_GNU_const_index:
4277 data = safe_read_uleb128 (data, end, &ul);
4278 ul = dwarf2_read_addr_index (per_cu, ul);
4279 fprintf_filtered (stream, " %s", pulongest (ul));
4283 fprintf_filtered (stream, "\n");
4289 /* Describe a single location, which may in turn consist of multiple
4293 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
4294 struct ui_file *stream,
4295 const gdb_byte *data, size_t size,
4296 struct objfile *objfile, unsigned int addr_size,
4297 int offset_size, struct dwarf2_per_cu_data *per_cu)
4299 const gdb_byte *end = data + size;
4300 int first_piece = 1, bad = 0;
4304 const gdb_byte *here = data;
4305 int disassemble = 1;
4310 fprintf_filtered (stream, _(", and "));
4312 if (!dwarf_always_disassemble)
4314 data = locexpr_describe_location_piece (symbol, stream,
4315 addr, objfile, per_cu,
4316 data, end, addr_size);
4317 /* If we printed anything, or if we have an empty piece,
4318 then don't disassemble. */
4320 || data[0] == DW_OP_piece
4321 || data[0] == DW_OP_bit_piece)
4326 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
4327 data = disassemble_dwarf_expression (stream,
4328 get_objfile_arch (objfile),
4329 addr_size, offset_size, data,
4331 dwarf_always_disassemble,
4337 int empty = data == here;
4340 fprintf_filtered (stream, " ");
4341 if (data[0] == DW_OP_piece)
4345 data = safe_read_uleb128 (data + 1, end, &bytes);
4348 fprintf_filtered (stream, _("an empty %s-byte piece"),
4351 fprintf_filtered (stream, _(" [%s-byte piece]"),
4354 else if (data[0] == DW_OP_bit_piece)
4356 uint64_t bits, offset;
4358 data = safe_read_uleb128 (data + 1, end, &bits);
4359 data = safe_read_uleb128 (data, end, &offset);
4362 fprintf_filtered (stream,
4363 _("an empty %s-bit piece"),
4366 fprintf_filtered (stream,
4367 _(" [%s-bit piece, offset %s bits]"),
4368 pulongest (bits), pulongest (offset));
4378 if (bad || data > end)
4379 error (_("Corrupted DWARF2 expression for \"%s\"."),
4380 SYMBOL_PRINT_NAME (symbol));
4383 /* Print a natural-language description of SYMBOL to STREAM. This
4384 version is for a symbol with a single location. */
4387 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
4388 struct ui_file *stream)
4390 struct dwarf2_locexpr_baton *dlbaton
4391 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4392 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4393 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4394 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4396 locexpr_describe_location_1 (symbol, addr, stream,
4397 dlbaton->data, dlbaton->size,
4398 objfile, addr_size, offset_size,
4402 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4403 any necessary bytecode in AX. */
4406 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4407 struct agent_expr *ax, struct axs_value *value)
4409 struct dwarf2_locexpr_baton *dlbaton
4410 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4411 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4413 if (dlbaton->size == 0)
4414 value->optimized_out = 1;
4416 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
4417 dlbaton->data, dlbaton->data + dlbaton->size,
4421 /* symbol_computed_ops 'generate_c_location' method. */
4424 locexpr_generate_c_location (struct symbol *sym, struct ui_file *stream,
4425 struct gdbarch *gdbarch,
4426 unsigned char *registers_used,
4427 CORE_ADDR pc, const char *result_name)
4429 struct dwarf2_locexpr_baton *dlbaton
4430 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
4431 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4433 if (dlbaton->size == 0)
4434 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4436 compile_dwarf_expr_to_c (stream, result_name,
4437 sym, pc, gdbarch, registers_used, addr_size,
4438 dlbaton->data, dlbaton->data + dlbaton->size,
4442 /* The set of location functions used with the DWARF-2 expression
4444 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4445 locexpr_read_variable,
4446 locexpr_read_variable_at_entry,
4447 locexpr_get_symbol_read_needs,
4448 locexpr_describe_location,
4449 0, /* location_has_loclist */
4450 locexpr_tracepoint_var_ref,
4451 locexpr_generate_c_location
4455 /* Wrapper functions for location lists. These generally find
4456 the appropriate location expression and call something above. */
4458 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4459 evaluator to calculate the location. */
4460 static struct value *
4461 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4463 struct dwarf2_loclist_baton *dlbaton
4464 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4466 const gdb_byte *data;
4468 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
4470 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4471 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4477 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4478 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4481 Function always returns non-NULL value, it may be marked optimized out if
4482 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
4483 if it cannot resolve the parameter for any reason. */
4485 static struct value *
4486 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4488 struct dwarf2_loclist_baton *dlbaton
4489 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4490 const gdb_byte *data;
4494 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4495 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4497 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4499 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4501 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4504 /* Implementation of get_symbol_read_needs from
4505 symbol_computed_ops. */
4507 static enum symbol_needs_kind
4508 loclist_symbol_needs (struct symbol *symbol)
4510 /* If there's a location list, then assume we need to have a frame
4511 to choose the appropriate location expression. With tracking of
4512 global variables this is not necessarily true, but such tracking
4513 is disabled in GCC at the moment until we figure out how to
4516 return SYMBOL_NEEDS_FRAME;
4519 /* Print a natural-language description of SYMBOL to STREAM. This
4520 version applies when there is a list of different locations, each
4521 with a specified address range. */
4524 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4525 struct ui_file *stream)
4527 struct dwarf2_loclist_baton *dlbaton
4528 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4529 const gdb_byte *loc_ptr, *buf_end;
4530 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4531 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4532 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4533 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4534 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4535 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4536 /* Adjust base_address for relocatable objects. */
4537 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
4538 CORE_ADDR base_address = dlbaton->base_address + base_offset;
4541 loc_ptr = dlbaton->data;
4542 buf_end = dlbaton->data + dlbaton->size;
4544 fprintf_filtered (stream, _("multi-location:\n"));
4546 /* Iterate through locations until we run out. */
4549 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4551 enum debug_loc_kind kind;
4552 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4554 if (dlbaton->from_dwo)
4555 kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4556 loc_ptr, buf_end, &new_ptr,
4557 &low, &high, byte_order);
4559 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4561 byte_order, addr_size,
4566 case DEBUG_LOC_END_OF_LIST:
4569 case DEBUG_LOC_BASE_ADDRESS:
4570 base_address = high + base_offset;
4571 fprintf_filtered (stream, _(" Base address %s"),
4572 paddress (gdbarch, base_address));
4574 case DEBUG_LOC_START_END:
4575 case DEBUG_LOC_START_LENGTH:
4577 case DEBUG_LOC_BUFFER_OVERFLOW:
4578 case DEBUG_LOC_INVALID_ENTRY:
4579 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4580 SYMBOL_PRINT_NAME (symbol));
4582 gdb_assert_not_reached ("bad debug_loc_kind");
4585 /* Otherwise, a location expression entry. */
4586 low += base_address;
4587 high += base_address;
4589 low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
4590 high = gdbarch_adjust_dwarf2_addr (gdbarch, high);
4592 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4595 /* (It would improve readability to print only the minimum
4596 necessary digits of the second number of the range.) */
4597 fprintf_filtered (stream, _(" Range %s-%s: "),
4598 paddress (gdbarch, low), paddress (gdbarch, high));
4600 /* Now describe this particular location. */
4601 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4602 objfile, addr_size, offset_size,
4605 fprintf_filtered (stream, "\n");
4611 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4612 any necessary bytecode in AX. */
4614 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4615 struct agent_expr *ax, struct axs_value *value)
4617 struct dwarf2_loclist_baton *dlbaton
4618 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4619 const gdb_byte *data;
4621 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4623 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4625 value->optimized_out = 1;
4627 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
4631 /* symbol_computed_ops 'generate_c_location' method. */
4634 loclist_generate_c_location (struct symbol *sym, struct ui_file *stream,
4635 struct gdbarch *gdbarch,
4636 unsigned char *registers_used,
4637 CORE_ADDR pc, const char *result_name)
4639 struct dwarf2_loclist_baton *dlbaton
4640 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
4641 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4642 const gdb_byte *data;
4645 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4647 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4649 compile_dwarf_expr_to_c (stream, result_name,
4650 sym, pc, gdbarch, registers_used, addr_size,
4655 /* The set of location functions used with the DWARF-2 expression
4656 evaluator and location lists. */
4657 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4658 loclist_read_variable,
4659 loclist_read_variable_at_entry,
4660 loclist_symbol_needs,
4661 loclist_describe_location,
4662 1, /* location_has_loclist */
4663 loclist_tracepoint_var_ref,
4664 loclist_generate_c_location
4667 /* Provide a prototype to silence -Wmissing-prototypes. */
4668 extern initialize_file_ftype _initialize_dwarf2loc;
4671 _initialize_dwarf2loc (void)
4673 add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4674 &entry_values_debug,
4675 _("Set entry values and tail call frames "
4677 _("Show entry values and tail call frames "
4679 _("When non-zero, the process of determining "
4680 "parameter values from function entry point "
4681 "and tail call frames will be printed."),
4683 show_entry_values_debug,
4684 &setdebuglist, &showdebuglist);
4687 register_self_test (selftests::copy_bitwise_tests);