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"
44 extern int dwarf_always_disassemble;
46 extern const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;
48 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
49 struct frame_info *frame,
52 struct dwarf2_per_cu_data *per_cu,
55 /* Until these have formal names, we define these here.
56 ref: http://gcc.gnu.org/wiki/DebugFission
57 Each entry in .debug_loc.dwo begins with a byte that describes the entry,
58 and is then followed by data specific to that entry. */
62 /* Indicates the end of the list of entries. */
63 DEBUG_LOC_END_OF_LIST = 0,
65 /* This is followed by an unsigned LEB128 number that is an index into
66 .debug_addr and specifies the base address for all following entries. */
67 DEBUG_LOC_BASE_ADDRESS = 1,
69 /* This is followed by two unsigned LEB128 numbers that are indices into
70 .debug_addr and specify the beginning and ending addresses, and then
71 a normal location expression as in .debug_loc. */
72 DEBUG_LOC_START_END = 2,
74 /* This is followed by an unsigned LEB128 number that is an index into
75 .debug_addr and specifies the beginning address, and a 4 byte unsigned
76 number that specifies the length, and then a normal location expression
78 DEBUG_LOC_START_LENGTH = 3,
80 /* An internal value indicating there is insufficient data. */
81 DEBUG_LOC_BUFFER_OVERFLOW = -1,
83 /* An internal value indicating an invalid kind of entry was found. */
84 DEBUG_LOC_INVALID_ENTRY = -2
87 /* Helper function which throws an error if a synthetic pointer is
91 invalid_synthetic_pointer (void)
93 error (_("access outside bounds of object "
94 "referenced via synthetic pointer"));
97 /* Decode the addresses in a non-dwo .debug_loc entry.
98 A pointer to the next byte to examine is returned in *NEW_PTR.
99 The encoded low,high addresses are return in *LOW,*HIGH.
100 The result indicates the kind of entry found. */
102 static enum debug_loc_kind
103 decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
104 const gdb_byte **new_ptr,
105 CORE_ADDR *low, CORE_ADDR *high,
106 enum bfd_endian byte_order,
107 unsigned int addr_size,
110 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
112 if (buf_end - loc_ptr < 2 * addr_size)
113 return DEBUG_LOC_BUFFER_OVERFLOW;
116 *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
118 *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
119 loc_ptr += addr_size;
122 *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
124 *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
125 loc_ptr += addr_size;
129 /* A base-address-selection entry. */
130 if ((*low & base_mask) == base_mask)
131 return DEBUG_LOC_BASE_ADDRESS;
133 /* An end-of-list entry. */
134 if (*low == 0 && *high == 0)
135 return DEBUG_LOC_END_OF_LIST;
137 return DEBUG_LOC_START_END;
140 /* Decode the addresses in .debug_loc.dwo entry.
141 A pointer to the next byte to examine is returned in *NEW_PTR.
142 The encoded low,high addresses are return in *LOW,*HIGH.
143 The result indicates the kind of entry found. */
145 static enum debug_loc_kind
146 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
147 const gdb_byte *loc_ptr,
148 const gdb_byte *buf_end,
149 const gdb_byte **new_ptr,
150 CORE_ADDR *low, CORE_ADDR *high,
151 enum bfd_endian byte_order)
153 uint64_t low_index, high_index;
155 if (loc_ptr == buf_end)
156 return DEBUG_LOC_BUFFER_OVERFLOW;
160 case DEBUG_LOC_END_OF_LIST:
162 return DEBUG_LOC_END_OF_LIST;
163 case DEBUG_LOC_BASE_ADDRESS:
165 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
167 return DEBUG_LOC_BUFFER_OVERFLOW;
168 *high = dwarf2_read_addr_index (per_cu, high_index);
170 return DEBUG_LOC_BASE_ADDRESS;
171 case DEBUG_LOC_START_END:
172 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
174 return DEBUG_LOC_BUFFER_OVERFLOW;
175 *low = dwarf2_read_addr_index (per_cu, low_index);
176 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
178 return DEBUG_LOC_BUFFER_OVERFLOW;
179 *high = dwarf2_read_addr_index (per_cu, high_index);
181 return DEBUG_LOC_START_END;
182 case DEBUG_LOC_START_LENGTH:
183 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
185 return DEBUG_LOC_BUFFER_OVERFLOW;
186 *low = dwarf2_read_addr_index (per_cu, low_index);
187 if (loc_ptr + 4 > buf_end)
188 return DEBUG_LOC_BUFFER_OVERFLOW;
190 *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
191 *new_ptr = loc_ptr + 4;
192 return DEBUG_LOC_START_LENGTH;
194 return DEBUG_LOC_INVALID_ENTRY;
198 /* A function for dealing with location lists. Given a
199 symbol baton (BATON) and a pc value (PC), find the appropriate
200 location expression, set *LOCEXPR_LENGTH, and return a pointer
201 to the beginning of the expression. Returns NULL on failure.
203 For now, only return the first matching location expression; there
204 can be more than one in the list. */
207 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
208 size_t *locexpr_length, CORE_ADDR pc)
210 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
211 struct gdbarch *gdbarch = get_objfile_arch (objfile);
212 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
213 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
214 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
215 /* Adjust base_address for relocatable objects. */
216 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
217 CORE_ADDR base_address = baton->base_address + base_offset;
218 const gdb_byte *loc_ptr, *buf_end;
220 loc_ptr = baton->data;
221 buf_end = baton->data + baton->size;
225 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
227 enum debug_loc_kind kind;
228 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
231 kind = decode_debug_loc_dwo_addresses (baton->per_cu,
232 loc_ptr, buf_end, &new_ptr,
233 &low, &high, byte_order);
235 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
237 byte_order, addr_size,
242 case DEBUG_LOC_END_OF_LIST:
245 case DEBUG_LOC_BASE_ADDRESS:
246 base_address = high + base_offset;
248 case DEBUG_LOC_START_END:
249 case DEBUG_LOC_START_LENGTH:
251 case DEBUG_LOC_BUFFER_OVERFLOW:
252 case DEBUG_LOC_INVALID_ENTRY:
253 error (_("dwarf2_find_location_expression: "
254 "Corrupted DWARF expression."));
256 gdb_assert_not_reached ("bad debug_loc_kind");
259 /* Otherwise, a location expression entry.
260 If the entry is from a DWO, don't add base address: the entry is from
261 .debug_addr which already has the DWARF "base address". We still add
262 base_offset in case we're debugging a PIE executable. */
271 high += base_address;
274 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
277 if (low == high && pc == low)
279 /* This is entry PC record present only at entry point
280 of a function. Verify it is really the function entry point. */
282 const struct block *pc_block = block_for_pc (pc);
283 struct symbol *pc_func = NULL;
286 pc_func = block_linkage_function (pc_block);
288 if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
290 *locexpr_length = length;
295 if (pc >= low && pc < high)
297 *locexpr_length = length;
305 /* This is the baton used when performing dwarf2 expression
307 struct dwarf_expr_baton
309 struct frame_info *frame;
310 struct dwarf2_per_cu_data *per_cu;
311 CORE_ADDR obj_address;
314 /* Helper functions for dwarf2_evaluate_loc_desc. */
316 /* Using the frame specified in BATON, return the value of register
317 REGNUM, treated as a pointer. */
319 dwarf_expr_read_addr_from_reg (void *baton, int dwarf_regnum)
321 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
322 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
323 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
325 return address_from_register (regnum, debaton->frame);
328 /* Implement struct dwarf_expr_context_funcs' "get_reg_value" callback. */
330 static struct value *
331 dwarf_expr_get_reg_value (void *baton, struct type *type, int dwarf_regnum)
333 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
334 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
335 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
337 return value_from_register (type, regnum, debaton->frame);
340 /* Read memory at ADDR (length LEN) into BUF. */
343 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
345 read_memory (addr, buf, len);
348 /* Using the frame specified in BATON, find the location expression
349 describing the frame base. Return a pointer to it in START and
350 its length in LENGTH. */
352 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
354 /* FIXME: cagney/2003-03-26: This code should be using
355 get_frame_base_address(), and then implement a dwarf2 specific
357 struct symbol *framefunc;
358 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
359 const struct block *bl = get_frame_block (debaton->frame, NULL);
362 error (_("frame address is not available."));
364 /* Use block_linkage_function, which returns a real (not inlined)
365 function, instead of get_frame_function, which may return an
367 framefunc = block_linkage_function (bl);
369 /* If we found a frame-relative symbol then it was certainly within
370 some function associated with a frame. If we can't find the frame,
371 something has gone wrong. */
372 gdb_assert (framefunc != NULL);
374 func_get_frame_base_dwarf_block (framefunc,
375 get_frame_address_in_block (debaton->frame),
379 /* Implement find_frame_base_location method for LOC_BLOCK functions using
380 DWARF expression for its DW_AT_frame_base. */
383 locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
384 const gdb_byte **start, size_t *length)
386 struct dwarf2_locexpr_baton *symbaton
387 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
389 *length = symbaton->size;
390 *start = symbaton->data;
393 /* Implement the struct symbol_block_ops::get_frame_base method for
394 LOC_BLOCK functions using a DWARF expression as its DW_AT_frame_base. */
397 locexpr_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
399 struct gdbarch *gdbarch;
401 struct dwarf2_locexpr_baton *dlbaton;
402 const gdb_byte *start;
404 struct value *result;
406 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
407 Thus, it's supposed to provide the find_frame_base_location method as
409 gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
411 gdbarch = get_frame_arch (frame);
412 type = builtin_type (gdbarch)->builtin_data_ptr;
413 dlbaton = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
415 SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
416 (framefunc, get_frame_pc (frame), &start, &length);
417 result = dwarf2_evaluate_loc_desc (type, frame, start, length,
420 /* The DW_AT_frame_base attribute contains a location description which
421 computes the base address itself. However, the call to
422 dwarf2_evaluate_loc_desc returns a value representing a variable at
423 that address. The frame base address is thus this variable's
425 return value_address (result);
428 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
429 function uses DWARF expression for its DW_AT_frame_base. */
431 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
433 locexpr_find_frame_base_location,
434 locexpr_get_frame_base
437 /* Implement find_frame_base_location method for LOC_BLOCK functions using
438 DWARF location list for its DW_AT_frame_base. */
441 loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
442 const gdb_byte **start, size_t *length)
444 struct dwarf2_loclist_baton *symbaton
445 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
447 *start = dwarf2_find_location_expression (symbaton, length, pc);
450 /* Implement the struct symbol_block_ops::get_frame_base method for
451 LOC_BLOCK functions using a DWARF location list as its DW_AT_frame_base. */
454 loclist_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
456 struct gdbarch *gdbarch;
458 struct dwarf2_loclist_baton *dlbaton;
459 const gdb_byte *start;
461 struct value *result;
463 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
464 Thus, it's supposed to provide the find_frame_base_location method as
466 gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
468 gdbarch = get_frame_arch (frame);
469 type = builtin_type (gdbarch)->builtin_data_ptr;
470 dlbaton = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
472 SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
473 (framefunc, get_frame_pc (frame), &start, &length);
474 result = dwarf2_evaluate_loc_desc (type, frame, start, length,
477 /* The DW_AT_frame_base attribute contains a location description which
478 computes the base address itself. However, the call to
479 dwarf2_evaluate_loc_desc returns a value representing a variable at
480 that address. The frame base address is thus this variable's
482 return value_address (result);
485 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
486 function uses DWARF location list for its DW_AT_frame_base. */
488 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
490 loclist_find_frame_base_location,
491 loclist_get_frame_base
494 /* See dwarf2loc.h. */
497 func_get_frame_base_dwarf_block (struct symbol *framefunc, CORE_ADDR pc,
498 const gdb_byte **start, size_t *length)
500 if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
502 const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
504 ops_block->find_frame_base_location (framefunc, pc, start, length);
510 error (_("Could not find the frame base for \"%s\"."),
511 SYMBOL_NATURAL_NAME (framefunc));
514 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
515 the frame in BATON. */
518 dwarf_expr_frame_cfa (void *baton)
520 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
522 return dwarf2_frame_cfa (debaton->frame);
525 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
526 the frame in BATON. */
529 dwarf_expr_frame_pc (void *baton)
531 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
533 return get_frame_address_in_block (debaton->frame);
536 /* Using the objfile specified in BATON, find the address for the
537 current thread's thread-local storage with offset OFFSET. */
539 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
541 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
542 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
544 return target_translate_tls_address (objfile, offset);
547 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
548 current CU (as is PER_CU). State of the CTX is not affected by the
552 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
553 struct dwarf2_per_cu_data *per_cu,
554 CORE_ADDR (*get_frame_pc) (void *baton),
557 struct dwarf2_locexpr_baton block;
559 block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu, get_frame_pc, baton);
561 /* DW_OP_call_ref is currently not supported. */
562 gdb_assert (block.per_cu == per_cu);
564 ctx->eval (block.data, block.size);
567 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
570 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
572 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) ctx->baton;
574 per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
575 ctx->funcs->get_frame_pc, ctx->baton);
578 /* Callback function for dwarf2_evaluate_loc_desc. */
581 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
582 cu_offset die_offset)
584 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) ctx->baton;
586 return dwarf2_get_die_type (die_offset, debaton->per_cu);
589 /* See dwarf2loc.h. */
591 unsigned int entry_values_debug = 0;
593 /* Helper to set entry_values_debug. */
596 show_entry_values_debug (struct ui_file *file, int from_tty,
597 struct cmd_list_element *c, const char *value)
599 fprintf_filtered (file,
600 _("Entry values and tail call frames debugging is %s.\n"),
604 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
605 CALLER_FRAME (for registers) can be NULL if it is not known. This function
606 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
609 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
610 struct call_site *call_site,
611 struct frame_info *caller_frame)
613 switch (FIELD_LOC_KIND (call_site->target))
615 case FIELD_LOC_KIND_DWARF_BLOCK:
617 struct dwarf2_locexpr_baton *dwarf_block;
619 struct type *caller_core_addr_type;
620 struct gdbarch *caller_arch;
622 dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
623 if (dwarf_block == NULL)
625 struct bound_minimal_symbol msym;
627 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
628 throw_error (NO_ENTRY_VALUE_ERROR,
629 _("DW_AT_GNU_call_site_target is not specified "
631 paddress (call_site_gdbarch, call_site->pc),
632 (msym.minsym == NULL ? "???"
633 : MSYMBOL_PRINT_NAME (msym.minsym)));
636 if (caller_frame == NULL)
638 struct bound_minimal_symbol msym;
640 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
641 throw_error (NO_ENTRY_VALUE_ERROR,
642 _("DW_AT_GNU_call_site_target DWARF block resolving "
643 "requires known frame which is currently not "
644 "available at %s in %s"),
645 paddress (call_site_gdbarch, call_site->pc),
646 (msym.minsym == NULL ? "???"
647 : MSYMBOL_PRINT_NAME (msym.minsym)));
650 caller_arch = get_frame_arch (caller_frame);
651 caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
652 val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
653 dwarf_block->data, dwarf_block->size,
654 dwarf_block->per_cu);
655 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
657 if (VALUE_LVAL (val) == lval_memory)
658 return value_address (val);
660 return value_as_address (val);
663 case FIELD_LOC_KIND_PHYSNAME:
665 const char *physname;
666 struct bound_minimal_symbol msym;
668 physname = FIELD_STATIC_PHYSNAME (call_site->target);
670 /* Handle both the mangled and demangled PHYSNAME. */
671 msym = lookup_minimal_symbol (physname, NULL, NULL);
672 if (msym.minsym == NULL)
674 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
675 throw_error (NO_ENTRY_VALUE_ERROR,
676 _("Cannot find function \"%s\" for a call site target "
678 physname, paddress (call_site_gdbarch, call_site->pc),
679 (msym.minsym == NULL ? "???"
680 : MSYMBOL_PRINT_NAME (msym.minsym)));
683 return BMSYMBOL_VALUE_ADDRESS (msym);
686 case FIELD_LOC_KIND_PHYSADDR:
687 return FIELD_STATIC_PHYSADDR (call_site->target);
690 internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
694 /* Convert function entry point exact address ADDR to the function which is
695 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
696 NO_ENTRY_VALUE_ERROR otherwise. */
698 static struct symbol *
699 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
701 struct symbol *sym = find_pc_function (addr);
704 if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
705 throw_error (NO_ENTRY_VALUE_ERROR,
706 _("DW_TAG_GNU_call_site resolving failed to find function "
707 "name for address %s"),
708 paddress (gdbarch, addr));
710 type = SYMBOL_TYPE (sym);
711 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
712 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
717 /* Verify function with entry point exact address ADDR can never call itself
718 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
719 can call itself via tail calls.
721 If a funtion can tail call itself its entry value based parameters are
722 unreliable. There is no verification whether the value of some/all
723 parameters is unchanged through the self tail call, we expect if there is
724 a self tail call all the parameters can be modified. */
727 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
729 struct obstack addr_obstack;
730 struct cleanup *old_chain;
733 /* Track here CORE_ADDRs which were already visited. */
736 /* The verification is completely unordered. Track here function addresses
737 which still need to be iterated. */
738 VEC (CORE_ADDR) *todo = NULL;
740 obstack_init (&addr_obstack);
741 old_chain = make_cleanup_obstack_free (&addr_obstack);
742 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
743 &addr_obstack, hashtab_obstack_allocate,
745 make_cleanup_htab_delete (addr_hash);
747 make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
749 VEC_safe_push (CORE_ADDR, todo, verify_addr);
750 while (!VEC_empty (CORE_ADDR, todo))
752 struct symbol *func_sym;
753 struct call_site *call_site;
755 addr = VEC_pop (CORE_ADDR, todo);
757 func_sym = func_addr_to_tail_call_list (gdbarch, addr);
759 for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
760 call_site; call_site = call_site->tail_call_next)
762 CORE_ADDR target_addr;
765 /* CALLER_FRAME with registers is not available for tail-call jumped
767 target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
769 if (target_addr == verify_addr)
771 struct bound_minimal_symbol msym;
773 msym = lookup_minimal_symbol_by_pc (verify_addr);
774 throw_error (NO_ENTRY_VALUE_ERROR,
775 _("DW_OP_GNU_entry_value resolving has found "
776 "function \"%s\" at %s can call itself via tail "
778 (msym.minsym == NULL ? "???"
779 : MSYMBOL_PRINT_NAME (msym.minsym)),
780 paddress (gdbarch, verify_addr));
783 slot = htab_find_slot (addr_hash, &target_addr, INSERT);
786 *slot = obstack_copy (&addr_obstack, &target_addr,
787 sizeof (target_addr));
788 VEC_safe_push (CORE_ADDR, todo, target_addr);
793 do_cleanups (old_chain);
796 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
797 ENTRY_VALUES_DEBUG. */
800 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
802 CORE_ADDR addr = call_site->pc;
803 struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
805 fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
806 (msym.minsym == NULL ? "???"
807 : MSYMBOL_PRINT_NAME (msym.minsym)));
811 /* vec.h needs single word type name, typedef it. */
812 typedef struct call_site *call_sitep;
814 /* Define VEC (call_sitep) functions. */
815 DEF_VEC_P (call_sitep);
817 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
818 only top callers and bottom callees which are present in both. GDBARCH is
819 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
820 no remaining possibilities to provide unambiguous non-trivial result.
821 RESULTP should point to NULL on the first (initialization) call. Caller is
822 responsible for xfree of any RESULTP data. */
825 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
826 VEC (call_sitep) *chain)
828 struct call_site_chain *result = *resultp;
829 long length = VEC_length (call_sitep, chain);
830 int callers, callees, idx;
834 /* Create the initial chain containing all the passed PCs. */
836 result = ((struct call_site_chain *)
837 xmalloc (sizeof (*result)
838 + sizeof (*result->call_site) * (length - 1)));
839 result->length = length;
840 result->callers = result->callees = length;
841 if (!VEC_empty (call_sitep, chain))
842 memcpy (result->call_site, VEC_address (call_sitep, chain),
843 sizeof (*result->call_site) * length);
846 if (entry_values_debug)
848 fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
849 for (idx = 0; idx < length; idx++)
850 tailcall_dump (gdbarch, result->call_site[idx]);
851 fputc_unfiltered ('\n', gdb_stdlog);
857 if (entry_values_debug)
859 fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
860 for (idx = 0; idx < length; idx++)
861 tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
862 fputc_unfiltered ('\n', gdb_stdlog);
865 /* Intersect callers. */
867 callers = std::min ((long) result->callers, length);
868 for (idx = 0; idx < callers; idx++)
869 if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
871 result->callers = idx;
875 /* Intersect callees. */
877 callees = std::min ((long) result->callees, length);
878 for (idx = 0; idx < callees; idx++)
879 if (result->call_site[result->length - 1 - idx]
880 != VEC_index (call_sitep, chain, length - 1 - idx))
882 result->callees = idx;
886 if (entry_values_debug)
888 fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
889 for (idx = 0; idx < result->callers; idx++)
890 tailcall_dump (gdbarch, result->call_site[idx]);
891 fputs_unfiltered (" |", gdb_stdlog);
892 for (idx = 0; idx < result->callees; idx++)
893 tailcall_dump (gdbarch, result->call_site[result->length
894 - result->callees + idx]);
895 fputc_unfiltered ('\n', gdb_stdlog);
898 if (result->callers == 0 && result->callees == 0)
900 /* There are no common callers or callees. It could be also a direct
901 call (which has length 0) with ambiguous possibility of an indirect
902 call - CALLERS == CALLEES == 0 is valid during the first allocation
903 but any subsequence processing of such entry means ambiguity. */
909 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
910 PC again. In such case there must be two different code paths to reach
911 it. CALLERS + CALLEES equal to LENGTH in the case of self tail-call. */
912 gdb_assert (result->callers + result->callees <= result->length);
915 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
916 assumed frames between them use GDBARCH. Use depth first search so we can
917 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
918 would have needless GDB stack overhead. Caller is responsible for xfree of
919 the returned result. Any unreliability results in thrown
920 NO_ENTRY_VALUE_ERROR. */
922 static struct call_site_chain *
923 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
926 CORE_ADDR save_callee_pc = callee_pc;
927 struct obstack addr_obstack;
928 struct cleanup *back_to_retval, *back_to_workdata;
929 struct call_site_chain *retval = NULL;
930 struct call_site *call_site;
932 /* Mark CALL_SITEs so we do not visit the same ones twice. */
935 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
936 call_site nor any possible call_site at CALLEE_PC's function is there.
937 Any CALL_SITE in CHAIN will be iterated to its siblings - via
938 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
939 VEC (call_sitep) *chain = NULL;
941 /* We are not interested in the specific PC inside the callee function. */
942 callee_pc = get_pc_function_start (callee_pc);
944 throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
945 paddress (gdbarch, save_callee_pc));
947 back_to_retval = make_cleanup (free_current_contents, &retval);
949 obstack_init (&addr_obstack);
950 back_to_workdata = make_cleanup_obstack_free (&addr_obstack);
951 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
952 &addr_obstack, hashtab_obstack_allocate,
954 make_cleanup_htab_delete (addr_hash);
956 make_cleanup (VEC_cleanup (call_sitep), &chain);
958 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
959 at the target's function. All the possible tail call sites in the
960 target's function will get iterated as already pushed into CHAIN via their
962 call_site = call_site_for_pc (gdbarch, caller_pc);
966 CORE_ADDR target_func_addr;
967 struct call_site *target_call_site;
969 /* CALLER_FRAME with registers is not available for tail-call jumped
971 target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
973 if (target_func_addr == callee_pc)
975 chain_candidate (gdbarch, &retval, chain);
979 /* There is no way to reach CALLEE_PC again as we would prevent
980 entering it twice as being already marked in ADDR_HASH. */
981 target_call_site = NULL;
985 struct symbol *target_func;
987 target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
988 target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
993 /* Attempt to visit TARGET_CALL_SITE. */
995 if (target_call_site)
999 slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
1002 /* Successfully entered TARGET_CALL_SITE. */
1004 *slot = &target_call_site->pc;
1005 VEC_safe_push (call_sitep, chain, target_call_site);
1010 /* Backtrack (without revisiting the originating call_site). Try the
1011 callers's sibling; if there isn't any try the callers's callers's
1014 target_call_site = NULL;
1015 while (!VEC_empty (call_sitep, chain))
1017 call_site = VEC_pop (call_sitep, chain);
1019 gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
1020 NO_INSERT) != NULL);
1021 htab_remove_elt (addr_hash, &call_site->pc);
1023 target_call_site = call_site->tail_call_next;
1024 if (target_call_site)
1028 while (target_call_site);
1030 if (VEC_empty (call_sitep, chain))
1033 call_site = VEC_last (call_sitep, chain);
1038 struct bound_minimal_symbol msym_caller, msym_callee;
1040 msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
1041 msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
1042 throw_error (NO_ENTRY_VALUE_ERROR,
1043 _("There are no unambiguously determinable intermediate "
1044 "callers or callees between caller function \"%s\" at %s "
1045 "and callee function \"%s\" at %s"),
1046 (msym_caller.minsym == NULL
1047 ? "???" : MSYMBOL_PRINT_NAME (msym_caller.minsym)),
1048 paddress (gdbarch, caller_pc),
1049 (msym_callee.minsym == NULL
1050 ? "???" : MSYMBOL_PRINT_NAME (msym_callee.minsym)),
1051 paddress (gdbarch, callee_pc));
1054 do_cleanups (back_to_workdata);
1055 discard_cleanups (back_to_retval);
1059 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
1060 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
1061 constructed return NULL. Caller is responsible for xfree of the returned
1064 struct call_site_chain *
1065 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
1066 CORE_ADDR callee_pc)
1068 struct call_site_chain *retval = NULL;
1072 retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
1074 CATCH (e, RETURN_MASK_ERROR)
1076 if (e.error == NO_ENTRY_VALUE_ERROR)
1078 if (entry_values_debug)
1079 exception_print (gdb_stdout, e);
1084 throw_exception (e);
1091 /* Return 1 if KIND and KIND_U match PARAMETER. Return 0 otherwise. */
1094 call_site_parameter_matches (struct call_site_parameter *parameter,
1095 enum call_site_parameter_kind kind,
1096 union call_site_parameter_u kind_u)
1098 if (kind == parameter->kind)
1101 case CALL_SITE_PARAMETER_DWARF_REG:
1102 return kind_u.dwarf_reg == parameter->u.dwarf_reg;
1103 case CALL_SITE_PARAMETER_FB_OFFSET:
1104 return kind_u.fb_offset == parameter->u.fb_offset;
1105 case CALL_SITE_PARAMETER_PARAM_OFFSET:
1106 return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off;
1111 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
1112 FRAME is for callee.
1114 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
1117 static struct call_site_parameter *
1118 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
1119 enum call_site_parameter_kind kind,
1120 union call_site_parameter_u kind_u,
1121 struct dwarf2_per_cu_data **per_cu_return)
1123 CORE_ADDR func_addr, caller_pc;
1124 struct gdbarch *gdbarch;
1125 struct frame_info *caller_frame;
1126 struct call_site *call_site;
1128 /* Initialize it just to avoid a GCC false warning. */
1129 struct call_site_parameter *parameter = NULL;
1130 CORE_ADDR target_addr;
1132 while (get_frame_type (frame) == INLINE_FRAME)
1134 frame = get_prev_frame (frame);
1135 gdb_assert (frame != NULL);
1138 func_addr = get_frame_func (frame);
1139 gdbarch = get_frame_arch (frame);
1140 caller_frame = get_prev_frame (frame);
1141 if (gdbarch != frame_unwind_arch (frame))
1143 struct bound_minimal_symbol msym
1144 = lookup_minimal_symbol_by_pc (func_addr);
1145 struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
1147 throw_error (NO_ENTRY_VALUE_ERROR,
1148 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
1149 "(of %s (%s)) does not match caller gdbarch %s"),
1150 gdbarch_bfd_arch_info (gdbarch)->printable_name,
1151 paddress (gdbarch, func_addr),
1152 (msym.minsym == NULL ? "???"
1153 : MSYMBOL_PRINT_NAME (msym.minsym)),
1154 gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
1157 if (caller_frame == NULL)
1159 struct bound_minimal_symbol msym
1160 = lookup_minimal_symbol_by_pc (func_addr);
1162 throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
1163 "requires caller of %s (%s)"),
1164 paddress (gdbarch, func_addr),
1165 (msym.minsym == NULL ? "???"
1166 : MSYMBOL_PRINT_NAME (msym.minsym)));
1168 caller_pc = get_frame_pc (caller_frame);
1169 call_site = call_site_for_pc (gdbarch, caller_pc);
1171 target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
1172 if (target_addr != func_addr)
1174 struct minimal_symbol *target_msym, *func_msym;
1176 target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
1177 func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
1178 throw_error (NO_ENTRY_VALUE_ERROR,
1179 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
1180 "but the called frame is for %s at %s"),
1181 (target_msym == NULL ? "???"
1182 : MSYMBOL_PRINT_NAME (target_msym)),
1183 paddress (gdbarch, target_addr),
1184 func_msym == NULL ? "???" : MSYMBOL_PRINT_NAME (func_msym),
1185 paddress (gdbarch, func_addr));
1188 /* No entry value based parameters would be reliable if this function can
1189 call itself via tail calls. */
1190 func_verify_no_selftailcall (gdbarch, func_addr);
1192 for (iparams = 0; iparams < call_site->parameter_count; iparams++)
1194 parameter = &call_site->parameter[iparams];
1195 if (call_site_parameter_matches (parameter, kind, kind_u))
1198 if (iparams == call_site->parameter_count)
1200 struct minimal_symbol *msym
1201 = lookup_minimal_symbol_by_pc (caller_pc).minsym;
1203 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
1204 determine its value. */
1205 throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
1206 "at DW_TAG_GNU_call_site %s at %s"),
1207 paddress (gdbarch, caller_pc),
1208 msym == NULL ? "???" : MSYMBOL_PRINT_NAME (msym));
1211 *per_cu_return = call_site->per_cu;
1215 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
1216 the normal DW_AT_GNU_call_site_value block. Otherwise return the
1217 DW_AT_GNU_call_site_data_value (dereferenced) block.
1219 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1222 Function always returns non-NULL, non-optimized out value. It throws
1223 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
1225 static struct value *
1226 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
1227 CORE_ADDR deref_size, struct type *type,
1228 struct frame_info *caller_frame,
1229 struct dwarf2_per_cu_data *per_cu)
1231 const gdb_byte *data_src;
1235 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1236 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1238 /* DEREF_SIZE size is not verified here. */
1239 if (data_src == NULL)
1240 throw_error (NO_ENTRY_VALUE_ERROR,
1241 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1243 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
1244 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1246 data = (gdb_byte *) alloca (size + 1);
1247 memcpy (data, data_src, size);
1248 data[size] = DW_OP_stack_value;
1250 return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
1253 /* Execute DWARF block of call_site_parameter which matches KIND and KIND_U.
1254 Choose DEREF_SIZE value of that parameter. Search caller of the CTX's
1255 frame. CTX must be of dwarf_expr_ctx_funcs kind.
1257 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
1258 can be more simple as it does not support cross-CU DWARF executions. */
1261 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1262 enum call_site_parameter_kind kind,
1263 union call_site_parameter_u kind_u,
1266 struct dwarf_expr_baton *debaton;
1267 struct frame_info *frame, *caller_frame;
1268 struct dwarf2_per_cu_data *caller_per_cu;
1269 struct dwarf_expr_baton baton_local;
1270 struct dwarf_expr_context saved_ctx;
1271 struct call_site_parameter *parameter;
1272 const gdb_byte *data_src;
1275 gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
1276 debaton = (struct dwarf_expr_baton *) ctx->baton;
1277 frame = debaton->frame;
1278 caller_frame = get_prev_frame (frame);
1280 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1282 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1283 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1285 /* DEREF_SIZE size is not verified here. */
1286 if (data_src == NULL)
1287 throw_error (NO_ENTRY_VALUE_ERROR,
1288 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1290 baton_local.frame = caller_frame;
1291 baton_local.per_cu = caller_per_cu;
1292 baton_local.obj_address = 0;
1294 saved_ctx.gdbarch = ctx->gdbarch;
1295 saved_ctx.addr_size = ctx->addr_size;
1296 saved_ctx.offset = ctx->offset;
1297 saved_ctx.baton = ctx->baton;
1298 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
1299 ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
1300 ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
1301 ctx->baton = &baton_local;
1303 ctx->eval (data_src, size);
1305 ctx->gdbarch = saved_ctx.gdbarch;
1306 ctx->addr_size = saved_ctx.addr_size;
1307 ctx->offset = saved_ctx.offset;
1308 ctx->baton = saved_ctx.baton;
1311 /* Callback function for dwarf2_evaluate_loc_desc.
1312 Fetch the address indexed by DW_OP_GNU_addr_index. */
1315 dwarf_expr_get_addr_index (void *baton, unsigned int index)
1317 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
1319 return dwarf2_read_addr_index (debaton->per_cu, index);
1322 /* Callback function for get_object_address. Return the address of the VLA
1326 dwarf_expr_get_obj_addr (void *baton)
1328 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
1330 gdb_assert (debaton != NULL);
1332 if (debaton->obj_address == 0)
1333 error (_("Location address is not set."));
1335 return debaton->obj_address;
1338 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1339 the indirect method on it, that is use its stored target value, the sole
1340 purpose of entry_data_value_funcs.. */
1342 static struct value *
1343 entry_data_value_coerce_ref (const struct value *value)
1345 struct type *checked_type = check_typedef (value_type (value));
1346 struct value *target_val;
1348 if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
1351 target_val = (struct value *) value_computed_closure (value);
1352 value_incref (target_val);
1356 /* Implement copy_closure. */
1359 entry_data_value_copy_closure (const struct value *v)
1361 struct value *target_val = (struct value *) value_computed_closure (v);
1363 value_incref (target_val);
1367 /* Implement free_closure. */
1370 entry_data_value_free_closure (struct value *v)
1372 struct value *target_val = (struct value *) value_computed_closure (v);
1374 value_free (target_val);
1377 /* Vector for methods for an entry value reference where the referenced value
1378 is stored in the caller. On the first dereference use
1379 DW_AT_GNU_call_site_data_value in the caller. */
1381 static const struct lval_funcs entry_data_value_funcs =
1385 NULL, /* indirect */
1386 entry_data_value_coerce_ref,
1387 NULL, /* check_synthetic_pointer */
1388 entry_data_value_copy_closure,
1389 entry_data_value_free_closure
1392 /* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KIND_U
1393 are used to match DW_AT_location at the caller's
1394 DW_TAG_GNU_call_site_parameter.
1396 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1397 cannot resolve the parameter for any reason. */
1399 static struct value *
1400 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1401 enum call_site_parameter_kind kind,
1402 union call_site_parameter_u kind_u)
1404 struct type *checked_type = check_typedef (type);
1405 struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1406 struct frame_info *caller_frame = get_prev_frame (frame);
1407 struct value *outer_val, *target_val, *val;
1408 struct call_site_parameter *parameter;
1409 struct dwarf2_per_cu_data *caller_per_cu;
1411 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1414 outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1418 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1419 used and it is not available do not fall back to OUTER_VAL - dereferencing
1420 TYPE_CODE_REF with non-entry data value would give current value - not the
1423 if (TYPE_CODE (checked_type) != TYPE_CODE_REF
1424 || TYPE_TARGET_TYPE (checked_type) == NULL)
1427 target_val = dwarf_entry_parameter_to_value (parameter,
1428 TYPE_LENGTH (target_type),
1429 target_type, caller_frame,
1432 release_value (target_val);
1433 val = allocate_computed_value (type, &entry_data_value_funcs,
1434 target_val /* closure */);
1436 /* Copy the referencing pointer to the new computed value. */
1437 memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1438 TYPE_LENGTH (checked_type));
1439 set_value_lazy (val, 0);
1444 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1445 SIZE are DWARF block used to match DW_AT_location at the caller's
1446 DW_TAG_GNU_call_site_parameter.
1448 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1449 cannot resolve the parameter for any reason. */
1451 static struct value *
1452 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1453 const gdb_byte *block, size_t block_len)
1455 union call_site_parameter_u kind_u;
1457 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1458 if (kind_u.dwarf_reg != -1)
1459 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
1462 if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
1463 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
1466 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1467 suppressed during normal operation. The expression can be arbitrary if
1468 there is no caller-callee entry value binding expected. */
1469 throw_error (NO_ENTRY_VALUE_ERROR,
1470 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1471 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1474 struct piece_closure
1476 /* Reference count. */
1479 /* The CU from which this closure's expression came. */
1480 struct dwarf2_per_cu_data *per_cu;
1482 /* The number of pieces used to describe this variable. */
1485 /* The target address size, used only for DWARF_VALUE_STACK. */
1488 /* The pieces themselves. */
1489 struct dwarf_expr_piece *pieces;
1492 /* Allocate a closure for a value formed from separately-described
1495 static struct piece_closure *
1496 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1497 int n_pieces, struct dwarf_expr_piece *pieces,
1500 struct piece_closure *c = XCNEW (struct piece_closure);
1505 c->n_pieces = n_pieces;
1506 c->addr_size = addr_size;
1507 c->pieces = XCNEWVEC (struct dwarf_expr_piece, n_pieces);
1509 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
1510 for (i = 0; i < n_pieces; ++i)
1511 if (c->pieces[i].location == DWARF_VALUE_STACK)
1512 value_incref (c->pieces[i].v.value);
1517 /* The lowest-level function to extract bits from a byte buffer.
1518 SOURCE is the buffer. It is updated if we read to the end of a
1520 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1521 updated to reflect the number of bits actually read.
1522 NBITS is the number of bits we want to read. It is updated to
1523 reflect the number of bits actually read. This function may read
1525 BITS_BIG_ENDIAN is taken directly from gdbarch.
1526 This function returns the extracted bits. */
1529 extract_bits_primitive (const gdb_byte **source,
1530 unsigned int *source_offset_bits,
1531 int *nbits, int bits_big_endian)
1533 unsigned int avail, mask, datum;
1535 gdb_assert (*source_offset_bits < 8);
1537 avail = 8 - *source_offset_bits;
1541 mask = (1 << avail) - 1;
1543 if (bits_big_endian)
1544 datum >>= 8 - (*source_offset_bits + *nbits);
1546 datum >>= *source_offset_bits;
1550 *source_offset_bits += avail;
1551 if (*source_offset_bits >= 8)
1553 *source_offset_bits -= 8;
1560 /* Extract some bits from a source buffer and move forward in the
1563 SOURCE is the source buffer. It is updated as bytes are read.
1564 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1566 NBITS is the number of bits to read.
1567 BITS_BIG_ENDIAN is taken directly from gdbarch.
1569 This function returns the bits that were read. */
1572 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
1573 int nbits, int bits_big_endian)
1577 gdb_assert (nbits > 0 && nbits <= 8);
1579 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
1585 more = extract_bits_primitive (source, source_offset_bits, &nbits,
1587 if (bits_big_endian)
1597 /* Write some bits into a buffer and move forward in the buffer.
1599 DATUM is the bits to write. The low-order bits of DATUM are used.
1600 DEST is the destination buffer. It is updated as bytes are
1602 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1604 NBITS is the number of valid bits in DATUM.
1605 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1608 insert_bits (unsigned int datum,
1609 gdb_byte *dest, unsigned int dest_offset_bits,
1610 int nbits, int bits_big_endian)
1614 gdb_assert (dest_offset_bits + nbits <= 8);
1616 mask = (1 << nbits) - 1;
1617 if (bits_big_endian)
1619 datum <<= 8 - (dest_offset_bits + nbits);
1620 mask <<= 8 - (dest_offset_bits + nbits);
1624 datum <<= dest_offset_bits;
1625 mask <<= dest_offset_bits;
1628 gdb_assert ((datum & ~mask) == 0);
1630 *dest = (*dest & ~mask) | datum;
1633 /* Copy bits from a source to a destination.
1635 DEST is where the bits should be written.
1636 DEST_OFFSET_BITS is the bit offset into DEST.
1637 SOURCE is the source of bits.
1638 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1639 BIT_COUNT is the number of bits to copy.
1640 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1643 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
1644 const gdb_byte *source, unsigned int source_offset_bits,
1645 unsigned int bit_count,
1646 int bits_big_endian)
1648 unsigned int dest_avail;
1651 /* Reduce everything to byte-size pieces. */
1652 dest += dest_offset_bits / 8;
1653 dest_offset_bits %= 8;
1654 source += source_offset_bits / 8;
1655 source_offset_bits %= 8;
1657 dest_avail = 8 - dest_offset_bits % 8;
1659 /* See if we can fill the first destination byte. */
1660 if (dest_avail < bit_count)
1662 datum = extract_bits (&source, &source_offset_bits, dest_avail,
1664 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
1666 dest_offset_bits = 0;
1667 bit_count -= dest_avail;
1670 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1671 than 8 bits remaining. */
1672 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
1673 for (; bit_count >= 8; bit_count -= 8)
1675 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
1676 *dest++ = (gdb_byte) datum;
1679 /* Finally, we may have a few leftover bits. */
1680 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
1683 datum = extract_bits (&source, &source_offset_bits, bit_count,
1685 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
1690 read_pieced_value (struct value *v)
1694 ULONGEST bits_to_skip;
1696 struct piece_closure *c
1697 = (struct piece_closure *) value_computed_closure (v);
1698 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
1700 size_t buffer_size = 0;
1701 std::vector<gdb_byte> buffer;
1703 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1705 if (value_type (v) != value_enclosing_type (v))
1706 internal_error (__FILE__, __LINE__,
1707 _("Should not be able to create a lazy value with "
1708 "an enclosing type"));
1710 contents = value_contents_raw (v);
1711 bits_to_skip = 8 * value_offset (v);
1712 if (value_bitsize (v))
1714 bits_to_skip += value_bitpos (v);
1715 type_len = value_bitsize (v);
1718 type_len = 8 * TYPE_LENGTH (value_type (v));
1720 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1722 struct dwarf_expr_piece *p = &c->pieces[i];
1723 size_t this_size, this_size_bits;
1724 long dest_offset_bits, source_offset_bits, source_offset;
1725 const gdb_byte *intermediate_buffer;
1727 /* Compute size, source, and destination offsets for copying, in
1729 this_size_bits = p->size;
1730 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1732 bits_to_skip -= this_size_bits;
1735 if (bits_to_skip > 0)
1737 dest_offset_bits = 0;
1738 source_offset_bits = bits_to_skip;
1739 this_size_bits -= bits_to_skip;
1744 dest_offset_bits = offset;
1745 source_offset_bits = 0;
1747 if (this_size_bits > type_len - offset)
1748 this_size_bits = type_len - offset;
1750 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1751 source_offset = source_offset_bits / 8;
1752 if (buffer_size < this_size)
1754 buffer_size = this_size;
1755 buffer.reserve (buffer_size);
1757 intermediate_buffer = buffer.data ();
1759 /* Copy from the source to DEST_BUFFER. */
1760 switch (p->location)
1762 case DWARF_VALUE_REGISTER:
1764 struct gdbarch *arch = get_frame_arch (frame);
1765 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1767 LONGEST reg_offset = source_offset;
1769 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1770 && this_size < register_size (arch, gdb_regnum))
1772 /* Big-endian, and we want less than full size. */
1773 reg_offset = register_size (arch, gdb_regnum) - this_size;
1774 /* We want the lower-order THIS_SIZE_BITS of the bytes
1775 we extract from the register. */
1776 source_offset_bits += 8 * this_size - this_size_bits;
1779 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1780 this_size, buffer.data (),
1783 /* Just so garbage doesn't ever shine through. */
1784 memset (buffer.data (), 0, this_size);
1787 mark_value_bits_optimized_out (v, offset, this_size_bits);
1789 mark_value_bits_unavailable (v, offset, this_size_bits);
1794 case DWARF_VALUE_MEMORY:
1795 read_value_memory (v, offset,
1796 p->v.mem.in_stack_memory,
1797 p->v.mem.addr + source_offset,
1798 buffer.data (), this_size);
1801 case DWARF_VALUE_STACK:
1803 size_t n = this_size;
1805 if (n > c->addr_size - source_offset)
1806 n = (c->addr_size >= source_offset
1807 ? c->addr_size - source_offset
1815 const gdb_byte *val_bytes = value_contents_all (p->v.value);
1817 intermediate_buffer = val_bytes + source_offset;
1822 case DWARF_VALUE_LITERAL:
1824 size_t n = this_size;
1826 if (n > p->v.literal.length - source_offset)
1827 n = (p->v.literal.length >= source_offset
1828 ? p->v.literal.length - source_offset
1831 intermediate_buffer = p->v.literal.data + source_offset;
1835 /* These bits show up as zeros -- but do not cause the value
1836 to be considered optimized-out. */
1837 case DWARF_VALUE_IMPLICIT_POINTER:
1840 case DWARF_VALUE_OPTIMIZED_OUT:
1841 mark_value_bits_optimized_out (v, offset, this_size_bits);
1845 internal_error (__FILE__, __LINE__, _("invalid location type"));
1848 if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1849 && p->location != DWARF_VALUE_IMPLICIT_POINTER)
1850 copy_bitwise (contents, dest_offset_bits,
1851 intermediate_buffer, source_offset_bits % 8,
1852 this_size_bits, bits_big_endian);
1854 offset += this_size_bits;
1859 write_pieced_value (struct value *to, struct value *from)
1863 ULONGEST bits_to_skip;
1864 const gdb_byte *contents;
1865 struct piece_closure *c
1866 = (struct piece_closure *) value_computed_closure (to);
1867 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
1869 size_t buffer_size = 0;
1870 std::vector<gdb_byte> buffer;
1872 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
1876 mark_value_bytes_optimized_out (to, 0, TYPE_LENGTH (value_type (to)));
1880 contents = value_contents (from);
1881 bits_to_skip = 8 * value_offset (to);
1882 if (value_bitsize (to))
1884 bits_to_skip += value_bitpos (to);
1885 type_len = value_bitsize (to);
1888 type_len = 8 * TYPE_LENGTH (value_type (to));
1890 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1892 struct dwarf_expr_piece *p = &c->pieces[i];
1893 size_t this_size_bits, this_size;
1894 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1896 const gdb_byte *source_buffer;
1898 this_size_bits = p->size;
1899 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1901 bits_to_skip -= this_size_bits;
1904 if (this_size_bits > type_len - offset)
1905 this_size_bits = type_len - offset;
1906 if (bits_to_skip > 0)
1908 dest_offset_bits = bits_to_skip;
1909 source_offset_bits = 0;
1910 this_size_bits -= bits_to_skip;
1915 dest_offset_bits = 0;
1916 source_offset_bits = offset;
1919 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1920 source_offset = source_offset_bits / 8;
1921 dest_offset = dest_offset_bits / 8;
1922 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1924 source_buffer = contents + source_offset;
1929 if (buffer_size < this_size)
1931 buffer_size = this_size;
1932 buffer.reserve (buffer_size);
1934 source_buffer = buffer.data ();
1938 switch (p->location)
1940 case DWARF_VALUE_REGISTER:
1942 struct gdbarch *arch = get_frame_arch (frame);
1943 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1944 int reg_offset = dest_offset;
1946 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1947 && this_size <= register_size (arch, gdb_regnum))
1949 /* Big-endian, and we want less than full size. */
1950 reg_offset = register_size (arch, gdb_regnum) - this_size;
1957 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1958 this_size, buffer.data (),
1962 throw_error (OPTIMIZED_OUT_ERROR,
1963 _("Can't do read-modify-write to "
1964 "update bitfield; containing word "
1965 "has been optimized out"));
1967 throw_error (NOT_AVAILABLE_ERROR,
1968 _("Can't do read-modify-write to update "
1969 "bitfield; containing word "
1972 copy_bitwise (buffer.data (), dest_offset_bits,
1973 contents, source_offset_bits,
1978 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
1979 this_size, source_buffer);
1982 case DWARF_VALUE_MEMORY:
1985 /* Only the first and last bytes can possibly have any
1987 read_memory (p->v.mem.addr + dest_offset, buffer.data (), 1);
1988 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
1989 &buffer[this_size - 1], 1);
1990 copy_bitwise (buffer.data (), dest_offset_bits,
1991 contents, source_offset_bits,
1996 write_memory (p->v.mem.addr + dest_offset,
1997 source_buffer, this_size);
2000 mark_value_bytes_optimized_out (to, 0, TYPE_LENGTH (value_type (to)));
2003 offset += this_size_bits;
2007 /* An implementation of an lval_funcs method to see whether a value is
2008 a synthetic pointer. */
2011 check_pieced_synthetic_pointer (const struct value *value, LONGEST bit_offset,
2014 struct piece_closure *c
2015 = (struct piece_closure *) value_computed_closure (value);
2018 bit_offset += 8 * value_offset (value);
2019 if (value_bitsize (value))
2020 bit_offset += value_bitpos (value);
2022 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2024 struct dwarf_expr_piece *p = &c->pieces[i];
2025 size_t this_size_bits = p->size;
2029 if (bit_offset >= this_size_bits)
2031 bit_offset -= this_size_bits;
2035 bit_length -= this_size_bits - bit_offset;
2039 bit_length -= this_size_bits;
2041 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2048 /* A wrapper function for get_frame_address_in_block. */
2051 get_frame_address_in_block_wrapper (void *baton)
2053 return get_frame_address_in_block ((struct frame_info *) baton);
2056 /* Fetch a DW_AT_const_value through a synthetic pointer. */
2058 static struct value *
2059 fetch_const_value_from_synthetic_pointer (sect_offset die, LONGEST byte_offset,
2060 struct dwarf2_per_cu_data *per_cu,
2063 struct value *result = NULL;
2064 struct obstack temp_obstack;
2065 struct cleanup *cleanup;
2066 const gdb_byte *bytes;
2069 obstack_init (&temp_obstack);
2070 cleanup = make_cleanup_obstack_free (&temp_obstack);
2071 bytes = dwarf2_fetch_constant_bytes (die, per_cu, &temp_obstack, &len);
2075 if (byte_offset >= 0
2076 && byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) <= len)
2078 bytes += byte_offset;
2079 result = value_from_contents (TYPE_TARGET_TYPE (type), bytes);
2082 invalid_synthetic_pointer ();
2085 result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type));
2087 do_cleanups (cleanup);
2092 /* Fetch the value pointed to by a synthetic pointer. */
2094 static struct value *
2095 indirect_synthetic_pointer (sect_offset die, LONGEST byte_offset,
2096 struct dwarf2_per_cu_data *per_cu,
2097 struct frame_info *frame, struct type *type)
2099 /* Fetch the location expression of the DIE we're pointing to. */
2100 struct dwarf2_locexpr_baton baton
2101 = dwarf2_fetch_die_loc_sect_off (die, per_cu,
2102 get_frame_address_in_block_wrapper, frame);
2104 /* If pointed-to DIE has a DW_AT_location, evaluate it and return the
2105 resulting value. Otherwise, it may have a DW_AT_const_value instead,
2106 or it may've been optimized out. */
2107 if (baton.data != NULL)
2108 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
2109 baton.data, baton.size, baton.per_cu,
2112 return fetch_const_value_from_synthetic_pointer (die, byte_offset, per_cu,
2116 /* An implementation of an lval_funcs method to indirect through a
2117 pointer. This handles the synthetic pointer case when needed. */
2119 static struct value *
2120 indirect_pieced_value (struct value *value)
2122 struct piece_closure *c
2123 = (struct piece_closure *) value_computed_closure (value);
2125 struct frame_info *frame;
2126 struct dwarf2_locexpr_baton baton;
2129 struct dwarf_expr_piece *piece = NULL;
2130 LONGEST byte_offset;
2131 enum bfd_endian byte_order;
2133 type = check_typedef (value_type (value));
2134 if (TYPE_CODE (type) != TYPE_CODE_PTR)
2137 bit_length = 8 * TYPE_LENGTH (type);
2138 bit_offset = 8 * value_offset (value);
2139 if (value_bitsize (value))
2140 bit_offset += value_bitpos (value);
2142 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2144 struct dwarf_expr_piece *p = &c->pieces[i];
2145 size_t this_size_bits = p->size;
2149 if (bit_offset >= this_size_bits)
2151 bit_offset -= this_size_bits;
2155 bit_length -= this_size_bits - bit_offset;
2159 bit_length -= this_size_bits;
2161 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2164 if (bit_length != 0)
2165 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
2171 gdb_assert (piece != NULL);
2172 frame = get_selected_frame (_("No frame selected."));
2174 /* This is an offset requested by GDB, such as value subscripts.
2175 However, due to how synthetic pointers are implemented, this is
2176 always presented to us as a pointer type. This means we have to
2177 sign-extend it manually as appropriate. Use raw
2178 extract_signed_integer directly rather than value_as_address and
2179 sign extend afterwards on architectures that would need it
2180 (mostly everywhere except MIPS, which has signed addresses) as
2181 the later would go through gdbarch_pointer_to_address and thus
2182 return a CORE_ADDR with high bits set on architectures that
2183 encode address spaces and other things in CORE_ADDR. */
2184 byte_order = gdbarch_byte_order (get_frame_arch (frame));
2185 byte_offset = extract_signed_integer (value_contents (value),
2186 TYPE_LENGTH (type), byte_order);
2187 byte_offset += piece->v.ptr.offset;
2189 return indirect_synthetic_pointer (piece->v.ptr.die, byte_offset, c->per_cu,
2193 /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
2196 static struct value *
2197 coerce_pieced_ref (const struct value *value)
2199 struct type *type = check_typedef (value_type (value));
2201 if (value_bits_synthetic_pointer (value, value_embedded_offset (value),
2202 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
2204 const struct piece_closure *closure
2205 = (struct piece_closure *) value_computed_closure (value);
2206 struct frame_info *frame
2207 = get_selected_frame (_("No frame selected."));
2209 /* gdb represents synthetic pointers as pieced values with a single
2211 gdb_assert (closure != NULL);
2212 gdb_assert (closure->n_pieces == 1);
2214 return indirect_synthetic_pointer (closure->pieces->v.ptr.die,
2215 closure->pieces->v.ptr.offset,
2216 closure->per_cu, frame, type);
2220 /* Else: not a synthetic reference; do nothing. */
2226 copy_pieced_value_closure (const struct value *v)
2228 struct piece_closure *c
2229 = (struct piece_closure *) value_computed_closure (v);
2236 free_pieced_value_closure (struct value *v)
2238 struct piece_closure *c
2239 = (struct piece_closure *) value_computed_closure (v);
2246 for (i = 0; i < c->n_pieces; ++i)
2247 if (c->pieces[i].location == DWARF_VALUE_STACK)
2248 value_free (c->pieces[i].v.value);
2255 /* Functions for accessing a variable described by DW_OP_piece. */
2256 static const struct lval_funcs pieced_value_funcs = {
2259 indirect_pieced_value,
2261 check_pieced_synthetic_pointer,
2262 copy_pieced_value_closure,
2263 free_pieced_value_closure
2266 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
2268 const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
2270 dwarf_expr_read_addr_from_reg,
2271 dwarf_expr_get_reg_value,
2272 dwarf_expr_read_mem,
2273 dwarf_expr_frame_base,
2274 dwarf_expr_frame_cfa,
2275 dwarf_expr_frame_pc,
2276 dwarf_expr_tls_address,
2277 dwarf_expr_dwarf_call,
2278 dwarf_expr_get_base_type,
2279 dwarf_expr_push_dwarf_reg_entry_value,
2280 dwarf_expr_get_addr_index,
2281 dwarf_expr_get_obj_addr
2284 /* Evaluate a location description, starting at DATA and with length
2285 SIZE, to find the current location of variable of TYPE in the
2286 context of FRAME. BYTE_OFFSET is applied after the contents are
2289 static struct value *
2290 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2291 const gdb_byte *data, size_t size,
2292 struct dwarf2_per_cu_data *per_cu,
2293 LONGEST byte_offset)
2295 struct value *retval;
2296 struct dwarf_expr_baton baton;
2297 struct cleanup *value_chain;
2298 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2300 if (byte_offset < 0)
2301 invalid_synthetic_pointer ();
2304 return allocate_optimized_out_value (type);
2306 baton.frame = frame;
2307 baton.per_cu = per_cu;
2308 baton.obj_address = 0;
2310 dwarf_expr_context ctx;
2311 value_chain = make_cleanup_value_free_to_mark (value_mark ());
2313 ctx.gdbarch = get_objfile_arch (objfile);
2314 ctx.addr_size = dwarf2_per_cu_addr_size (per_cu);
2315 ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2316 ctx.offset = dwarf2_per_cu_text_offset (per_cu);
2318 ctx.funcs = &dwarf_expr_ctx_funcs;
2322 ctx.eval (data, size);
2324 CATCH (ex, RETURN_MASK_ERROR)
2326 if (ex.error == NOT_AVAILABLE_ERROR)
2328 do_cleanups (value_chain);
2329 retval = allocate_value (type);
2330 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
2333 else if (ex.error == NO_ENTRY_VALUE_ERROR)
2335 if (entry_values_debug)
2336 exception_print (gdb_stdout, ex);
2337 do_cleanups (value_chain);
2338 return allocate_optimized_out_value (type);
2341 throw_exception (ex);
2345 if (ctx.num_pieces > 0)
2347 struct piece_closure *c;
2348 struct frame_id frame_id = get_frame_id (frame);
2349 ULONGEST bit_size = 0;
2352 for (i = 0; i < ctx.num_pieces; ++i)
2353 bit_size += ctx.pieces[i].size;
2354 if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
2355 invalid_synthetic_pointer ();
2357 c = allocate_piece_closure (per_cu, ctx.num_pieces, ctx.pieces,
2359 /* We must clean up the value chain after creating the piece
2360 closure but before allocating the result. */
2361 do_cleanups (value_chain);
2362 retval = allocate_computed_value (type, &pieced_value_funcs, c);
2363 VALUE_FRAME_ID (retval) = frame_id;
2364 set_value_offset (retval, byte_offset);
2368 switch (ctx.location)
2370 case DWARF_VALUE_REGISTER:
2372 struct gdbarch *arch = get_frame_arch (frame);
2374 = longest_to_int (value_as_long (ctx.fetch (0)));
2375 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, dwarf_regnum);
2377 if (byte_offset != 0)
2378 error (_("cannot use offset on synthetic pointer to register"));
2379 do_cleanups (value_chain);
2380 retval = value_from_register (type, gdb_regnum, frame);
2381 if (value_optimized_out (retval))
2385 /* This means the register has undefined value / was
2386 not saved. As we're computing the location of some
2387 variable etc. in the program, not a value for
2388 inspecting a register ($pc, $sp, etc.), return a
2389 generic optimized out value instead, so that we show
2390 <optimized out> instead of <not saved>. */
2391 do_cleanups (value_chain);
2392 tmp = allocate_value (type);
2393 value_contents_copy (tmp, 0, retval, 0, TYPE_LENGTH (type));
2399 case DWARF_VALUE_MEMORY:
2401 struct type *ptr_type;
2402 CORE_ADDR address = ctx.fetch_address (0);
2403 int in_stack_memory = ctx.fetch_in_stack_memory (0);
2405 /* DW_OP_deref_size (and possibly other operations too) may
2406 create a pointer instead of an address. Ideally, the
2407 pointer to address conversion would be performed as part
2408 of those operations, but the type of the object to
2409 which the address refers is not known at the time of
2410 the operation. Therefore, we do the conversion here
2411 since the type is readily available. */
2413 switch (TYPE_CODE (type))
2415 case TYPE_CODE_FUNC:
2416 case TYPE_CODE_METHOD:
2417 ptr_type = builtin_type (ctx.gdbarch)->builtin_func_ptr;
2420 ptr_type = builtin_type (ctx.gdbarch)->builtin_data_ptr;
2423 address = value_as_address (value_from_pointer (ptr_type, address));
2425 do_cleanups (value_chain);
2426 retval = value_at_lazy (type, address + byte_offset);
2427 if (in_stack_memory)
2428 set_value_stack (retval, 1);
2432 case DWARF_VALUE_STACK:
2434 struct value *value = ctx.fetch (0);
2436 const gdb_byte *val_bytes;
2437 size_t n = TYPE_LENGTH (value_type (value));
2439 if (byte_offset + TYPE_LENGTH (type) > n)
2440 invalid_synthetic_pointer ();
2442 val_bytes = value_contents_all (value);
2443 val_bytes += byte_offset;
2446 /* Preserve VALUE because we are going to free values back
2447 to the mark, but we still need the value contents
2449 value_incref (value);
2450 do_cleanups (value_chain);
2451 make_cleanup_value_free (value);
2453 retval = allocate_value (type);
2454 contents = value_contents_raw (retval);
2455 if (n > TYPE_LENGTH (type))
2457 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2459 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2460 val_bytes += n - TYPE_LENGTH (type);
2461 n = TYPE_LENGTH (type);
2463 memcpy (contents, val_bytes, n);
2467 case DWARF_VALUE_LITERAL:
2470 const bfd_byte *ldata;
2473 if (byte_offset + TYPE_LENGTH (type) > n)
2474 invalid_synthetic_pointer ();
2476 do_cleanups (value_chain);
2477 retval = allocate_value (type);
2478 contents = value_contents_raw (retval);
2480 ldata = ctx.data + byte_offset;
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 ldata += n - TYPE_LENGTH (type);
2489 n = TYPE_LENGTH (type);
2491 memcpy (contents, ldata, n);
2495 case DWARF_VALUE_OPTIMIZED_OUT:
2496 do_cleanups (value_chain);
2497 retval = allocate_optimized_out_value (type);
2500 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2501 operation by execute_stack_op. */
2502 case DWARF_VALUE_IMPLICIT_POINTER:
2503 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2504 it can only be encountered when making a piece. */
2506 internal_error (__FILE__, __LINE__, _("invalid location type"));
2510 set_value_initialized (retval, ctx.initialized);
2512 do_cleanups (value_chain);
2517 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2518 passes 0 as the byte_offset. */
2521 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2522 const gdb_byte *data, size_t size,
2523 struct dwarf2_per_cu_data *per_cu)
2525 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2528 /* Evaluates a dwarf expression and stores the result in VAL, expecting
2529 that the dwarf expression only produces a single CORE_ADDR. FRAME is the
2530 frame in which the expression is evaluated. ADDR is a context (location of
2531 a variable) and might be needed to evaluate the location expression.
2532 Returns 1 on success, 0 otherwise. */
2535 dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton *dlbaton,
2536 struct frame_info *frame,
2540 struct dwarf_expr_baton baton;
2541 struct objfile *objfile;
2542 struct cleanup *cleanup;
2544 if (dlbaton == NULL || dlbaton->size == 0)
2547 dwarf_expr_context ctx;
2549 baton.frame = frame;
2550 baton.per_cu = dlbaton->per_cu;
2551 baton.obj_address = addr;
2553 objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2555 ctx.gdbarch = get_objfile_arch (objfile);
2556 ctx.addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2557 ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (dlbaton->per_cu);
2558 ctx.offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
2559 ctx.funcs = &dwarf_expr_ctx_funcs;
2562 ctx.eval (dlbaton->data, dlbaton->size);
2564 switch (ctx.location)
2566 case DWARF_VALUE_REGISTER:
2567 case DWARF_VALUE_MEMORY:
2568 case DWARF_VALUE_STACK:
2569 *valp = ctx.fetch_address (0);
2570 if (ctx.location == DWARF_VALUE_REGISTER)
2571 *valp = dwarf_expr_read_addr_from_reg (&baton, *valp);
2573 case DWARF_VALUE_LITERAL:
2574 *valp = extract_signed_integer (ctx.data, ctx.len,
2575 gdbarch_byte_order (ctx.gdbarch));
2577 /* Unsupported dwarf values. */
2578 case DWARF_VALUE_OPTIMIZED_OUT:
2579 case DWARF_VALUE_IMPLICIT_POINTER:
2586 /* See dwarf2loc.h. */
2589 dwarf2_evaluate_property (const struct dynamic_prop *prop,
2590 struct frame_info *frame,
2591 struct property_addr_info *addr_stack,
2597 if (frame == NULL && has_stack_frames ())
2598 frame = get_selected_frame (NULL);
2604 const struct dwarf2_property_baton *baton
2605 = (const struct dwarf2_property_baton *) prop->data.baton;
2607 if (dwarf2_locexpr_baton_eval (&baton->locexpr, frame,
2608 addr_stack ? addr_stack->addr : 0,
2611 if (baton->referenced_type)
2613 struct value *val = value_at (baton->referenced_type, *value);
2615 *value = value_as_address (val);
2624 struct dwarf2_property_baton *baton
2625 = (struct dwarf2_property_baton *) prop->data.baton;
2626 CORE_ADDR pc = get_frame_address_in_block (frame);
2627 const gdb_byte *data;
2631 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2634 val = dwarf2_evaluate_loc_desc (baton->referenced_type, frame, data,
2635 size, baton->loclist.per_cu);
2636 if (!value_optimized_out (val))
2638 *value = value_as_address (val);
2646 *value = prop->data.const_val;
2649 case PROP_ADDR_OFFSET:
2651 struct dwarf2_property_baton *baton
2652 = (struct dwarf2_property_baton *) prop->data.baton;
2653 struct property_addr_info *pinfo;
2656 for (pinfo = addr_stack; pinfo != NULL; pinfo = pinfo->next)
2657 if (pinfo->type == baton->referenced_type)
2660 error (_("cannot find reference address for offset property"));
2661 if (pinfo->valaddr != NULL)
2662 val = value_from_contents
2663 (baton->offset_info.type,
2664 pinfo->valaddr + baton->offset_info.offset);
2666 val = value_at (baton->offset_info.type,
2667 pinfo->addr + baton->offset_info.offset);
2668 *value = value_as_address (val);
2676 /* See dwarf2loc.h. */
2679 dwarf2_compile_property_to_c (struct ui_file *stream,
2680 const char *result_name,
2681 struct gdbarch *gdbarch,
2682 unsigned char *registers_used,
2683 const struct dynamic_prop *prop,
2687 struct dwarf2_property_baton *baton
2688 = (struct dwarf2_property_baton *) prop->data.baton;
2689 const gdb_byte *data;
2691 struct dwarf2_per_cu_data *per_cu;
2693 if (prop->kind == PROP_LOCEXPR)
2695 data = baton->locexpr.data;
2696 size = baton->locexpr.size;
2697 per_cu = baton->locexpr.per_cu;
2701 gdb_assert (prop->kind == PROP_LOCLIST);
2703 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2704 per_cu = baton->loclist.per_cu;
2707 compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
2708 gdbarch, registers_used,
2709 dwarf2_per_cu_addr_size (per_cu),
2710 data, data + size, per_cu);
2714 /* Helper functions and baton for dwarf2_loc_desc_get_symbol_read_needs. */
2716 struct symbol_needs_baton
2718 enum symbol_needs_kind needs;
2719 struct dwarf2_per_cu_data *per_cu;
2722 /* Reads from registers do require a frame. */
2724 symbol_needs_read_addr_from_reg (void *baton, int regnum)
2726 struct symbol_needs_baton *nf_baton = (struct symbol_needs_baton *) baton;
2728 nf_baton->needs = SYMBOL_NEEDS_FRAME;
2732 /* struct dwarf_expr_context_funcs' "get_reg_value" callback:
2733 Reads from registers do require a frame. */
2735 static struct value *
2736 symbol_needs_get_reg_value (void *baton, struct type *type, int regnum)
2738 struct symbol_needs_baton *nf_baton = (struct symbol_needs_baton *) baton;
2740 nf_baton->needs = SYMBOL_NEEDS_FRAME;
2741 return value_zero (type, not_lval);
2744 /* Reads from memory do not require a frame. */
2746 symbol_needs_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
2748 memset (buf, 0, len);
2751 /* Frame-relative accesses do require a frame. */
2753 symbol_needs_frame_base (void *baton, const gdb_byte **start, size_t * length)
2755 static gdb_byte lit0 = DW_OP_lit0;
2756 struct symbol_needs_baton *nf_baton = (struct symbol_needs_baton *) baton;
2761 nf_baton->needs = SYMBOL_NEEDS_FRAME;
2764 /* CFA accesses require a frame. */
2767 symbol_needs_frame_cfa (void *baton)
2769 struct symbol_needs_baton *nf_baton = (struct symbol_needs_baton *) baton;
2771 nf_baton->needs = SYMBOL_NEEDS_FRAME;
2775 /* Thread-local accesses require registers, but not a frame. */
2777 symbol_needs_tls_address (void *baton, CORE_ADDR offset)
2779 struct symbol_needs_baton *nf_baton = (struct symbol_needs_baton *) baton;
2781 if (nf_baton->needs <= SYMBOL_NEEDS_REGISTERS)
2782 nf_baton->needs = SYMBOL_NEEDS_REGISTERS;
2786 /* Helper interface of per_cu_dwarf_call for
2787 dwarf2_loc_desc_get_symbol_read_needs. */
2790 symbol_needs_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
2792 struct symbol_needs_baton *nf_baton =
2793 (struct symbol_needs_baton *) ctx->baton;
2795 per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
2796 ctx->funcs->get_frame_pc, ctx->baton);
2799 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
2802 needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
2803 enum call_site_parameter_kind kind,
2804 union call_site_parameter_u kind_u, int deref_size)
2806 struct symbol_needs_baton *nf_baton =
2807 (struct symbol_needs_baton *) ctx->baton;
2809 nf_baton->needs = SYMBOL_NEEDS_FRAME;
2811 /* The expression may require some stub values on DWARF stack. */
2812 ctx->push_address (0, 0);
2815 /* DW_OP_GNU_addr_index doesn't require a frame. */
2818 needs_get_addr_index (void *baton, unsigned int index)
2820 /* Nothing to do. */
2824 /* DW_OP_push_object_address has a frame already passed through. */
2827 needs_get_obj_addr (void *baton)
2829 /* Nothing to do. */
2833 /* Virtual method table for dwarf2_loc_desc_get_symbol_read_needs
2836 static const struct dwarf_expr_context_funcs symbol_needs_ctx_funcs =
2838 symbol_needs_read_addr_from_reg,
2839 symbol_needs_get_reg_value,
2840 symbol_needs_read_mem,
2841 symbol_needs_frame_base,
2842 symbol_needs_frame_cfa,
2843 symbol_needs_frame_cfa, /* get_frame_pc */
2844 symbol_needs_tls_address,
2845 symbol_needs_dwarf_call,
2846 NULL, /* get_base_type */
2847 needs_dwarf_reg_entry_value,
2848 needs_get_addr_index,
2852 /* Compute the correct symbol_needs_kind value for the location
2853 expression at DATA (length SIZE). */
2855 static enum symbol_needs_kind
2856 dwarf2_loc_desc_get_symbol_read_needs (const gdb_byte *data, size_t size,
2857 struct dwarf2_per_cu_data *per_cu)
2859 struct symbol_needs_baton baton;
2861 struct cleanup *old_chain;
2862 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2864 baton.needs = SYMBOL_NEEDS_NONE;
2865 baton.per_cu = per_cu;
2867 dwarf_expr_context ctx;
2868 old_chain = make_cleanup_value_free_to_mark (value_mark ());
2870 ctx.gdbarch = get_objfile_arch (objfile);
2871 ctx.addr_size = dwarf2_per_cu_addr_size (per_cu);
2872 ctx.ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2873 ctx.offset = dwarf2_per_cu_text_offset (per_cu);
2875 ctx.funcs = &symbol_needs_ctx_funcs;
2877 ctx.eval (data, size);
2879 in_reg = ctx.location == DWARF_VALUE_REGISTER;
2881 if (ctx.num_pieces > 0)
2885 /* If the location has several pieces, and any of them are in
2886 registers, then we will need a frame to fetch them from. */
2887 for (i = 0; i < ctx.num_pieces; i++)
2888 if (ctx.pieces[i].location == DWARF_VALUE_REGISTER)
2892 do_cleanups (old_chain);
2895 baton.needs = SYMBOL_NEEDS_FRAME;
2899 /* A helper function that throws an unimplemented error mentioning a
2900 given DWARF operator. */
2903 unimplemented (unsigned int op)
2905 const char *name = get_DW_OP_name (op);
2908 error (_("DWARF operator %s cannot be translated to an agent expression"),
2911 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2912 "to an agent expression"),
2918 This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2919 can issue a complaint, which is better than having every target's
2920 implementation of dwarf2_reg_to_regnum do it. */
2923 dwarf_reg_to_regnum (struct gdbarch *arch, int dwarf_reg)
2925 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2929 complaint (&symfile_complaints,
2930 _("bad DWARF register number %d"), dwarf_reg);
2935 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2936 Throw an error because DWARF_REG is bad. */
2939 throw_bad_regnum_error (ULONGEST dwarf_reg)
2941 /* Still want to print -1 as "-1".
2942 We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2943 but that's overkill for now. */
2944 if ((int) dwarf_reg == dwarf_reg)
2945 error (_("Unable to access DWARF register number %d"), (int) dwarf_reg);
2946 error (_("Unable to access DWARF register number %s"),
2947 pulongest (dwarf_reg));
2950 /* See dwarf2loc.h. */
2953 dwarf_reg_to_regnum_or_error (struct gdbarch *arch, ULONGEST dwarf_reg)
2957 if (dwarf_reg > INT_MAX)
2958 throw_bad_regnum_error (dwarf_reg);
2959 /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2960 bad, but that's ok. */
2961 reg = dwarf_reg_to_regnum (arch, (int) dwarf_reg);
2963 throw_bad_regnum_error (dwarf_reg);
2967 /* A helper function that emits an access to memory. ARCH is the
2968 target architecture. EXPR is the expression which we are building.
2969 NBITS is the number of bits we want to read. This emits the
2970 opcodes needed to read the memory and then extract the desired
2974 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2976 ULONGEST nbytes = (nbits + 7) / 8;
2978 gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
2981 ax_trace_quick (expr, nbytes);
2984 ax_simple (expr, aop_ref8);
2985 else if (nbits <= 16)
2986 ax_simple (expr, aop_ref16);
2987 else if (nbits <= 32)
2988 ax_simple (expr, aop_ref32);
2990 ax_simple (expr, aop_ref64);
2992 /* If we read exactly the number of bytes we wanted, we're done. */
2993 if (8 * nbytes == nbits)
2996 if (gdbarch_bits_big_endian (arch))
2998 /* On a bits-big-endian machine, we want the high-order
3000 ax_const_l (expr, 8 * nbytes - nbits);
3001 ax_simple (expr, aop_rsh_unsigned);
3005 /* On a bits-little-endian box, we want the low-order NBITS. */
3006 ax_zero_ext (expr, nbits);
3010 /* A helper function to return the frame's PC. */
3013 get_ax_pc (void *baton)
3015 struct agent_expr *expr = (struct agent_expr *) baton;
3020 /* Compile a DWARF location expression to an agent expression.
3022 EXPR is the agent expression we are building.
3023 LOC is the agent value we modify.
3024 ARCH is the architecture.
3025 ADDR_SIZE is the size of addresses, in bytes.
3026 OP_PTR is the start of the location expression.
3027 OP_END is one past the last byte of the location expression.
3029 This will throw an exception for various kinds of errors -- for
3030 example, if the expression cannot be compiled, or if the expression
3034 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
3035 struct gdbarch *arch, unsigned int addr_size,
3036 const gdb_byte *op_ptr, const gdb_byte *op_end,
3037 struct dwarf2_per_cu_data *per_cu)
3040 std::vector<int> dw_labels, patches;
3041 const gdb_byte * const base = op_ptr;
3042 const gdb_byte *previous_piece = op_ptr;
3043 enum bfd_endian byte_order = gdbarch_byte_order (arch);
3044 ULONGEST bits_collected = 0;
3045 unsigned int addr_size_bits = 8 * addr_size;
3046 int bits_big_endian = gdbarch_bits_big_endian (arch);
3048 std::vector<int> offsets (op_end - op_ptr, -1);
3050 /* By default we are making an address. */
3051 loc->kind = axs_lvalue_memory;
3053 while (op_ptr < op_end)
3055 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
3056 uint64_t uoffset, reg;
3060 offsets[op_ptr - base] = expr->len;
3063 /* Our basic approach to code generation is to map DWARF
3064 operations directly to AX operations. However, there are
3067 First, DWARF works on address-sized units, but AX always uses
3068 LONGEST. For most operations we simply ignore this
3069 difference; instead we generate sign extensions as needed
3070 before division and comparison operations. It would be nice
3071 to omit the sign extensions, but there is no way to determine
3072 the size of the target's LONGEST. (This code uses the size
3073 of the host LONGEST in some cases -- that is a bug but it is
3076 Second, some DWARF operations cannot be translated to AX.
3077 For these we simply fail. See
3078 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
3113 ax_const_l (expr, op - DW_OP_lit0);
3117 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
3118 op_ptr += addr_size;
3119 /* Some versions of GCC emit DW_OP_addr before
3120 DW_OP_GNU_push_tls_address. In this case the value is an
3121 index, not an address. We don't support things like
3122 branching between the address and the TLS op. */
3123 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
3124 uoffset += dwarf2_per_cu_text_offset (per_cu);
3125 ax_const_l (expr, uoffset);
3129 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
3133 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
3137 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
3141 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
3145 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
3149 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
3153 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
3157 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
3161 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3162 ax_const_l (expr, uoffset);
3165 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3166 ax_const_l (expr, offset);
3201 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3202 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
3203 loc->kind = axs_lvalue_register;
3207 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3208 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3209 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, reg);
3210 loc->kind = axs_lvalue_register;
3213 case DW_OP_implicit_value:
3217 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
3218 if (op_ptr + len > op_end)
3219 error (_("DW_OP_implicit_value: too few bytes available."));
3220 if (len > sizeof (ULONGEST))
3221 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3224 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
3227 dwarf_expr_require_composition (op_ptr, op_end,
3228 "DW_OP_implicit_value");
3230 loc->kind = axs_rvalue;
3234 case DW_OP_stack_value:
3235 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
3236 loc->kind = axs_rvalue;
3271 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3272 i = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
3276 ax_const_l (expr, offset);
3277 ax_simple (expr, aop_add);
3282 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3283 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3284 i = dwarf_reg_to_regnum_or_error (arch, reg);
3288 ax_const_l (expr, offset);
3289 ax_simple (expr, aop_add);
3295 const gdb_byte *datastart;
3297 const struct block *b;
3298 struct symbol *framefunc;
3300 b = block_for_pc (expr->scope);
3303 error (_("No block found for address"));
3305 framefunc = block_linkage_function (b);
3308 error (_("No function found for block"));
3310 func_get_frame_base_dwarf_block (framefunc, expr->scope,
3311 &datastart, &datalen);
3313 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3314 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
3315 datastart + datalen, per_cu);
3316 if (loc->kind == axs_lvalue_register)
3317 require_rvalue (expr, loc);
3321 ax_const_l (expr, offset);
3322 ax_simple (expr, aop_add);
3325 loc->kind = axs_lvalue_memory;
3330 ax_simple (expr, aop_dup);
3334 ax_simple (expr, aop_pop);
3339 ax_pick (expr, offset);
3343 ax_simple (expr, aop_swap);
3351 ax_simple (expr, aop_rot);
3355 case DW_OP_deref_size:
3359 if (op == DW_OP_deref_size)
3364 if (size != 1 && size != 2 && size != 4 && size != 8)
3365 error (_("Unsupported size %d in %s"),
3366 size, get_DW_OP_name (op));
3367 access_memory (arch, expr, size * TARGET_CHAR_BIT);
3372 /* Sign extend the operand. */
3373 ax_ext (expr, addr_size_bits);
3374 ax_simple (expr, aop_dup);
3375 ax_const_l (expr, 0);
3376 ax_simple (expr, aop_less_signed);
3377 ax_simple (expr, aop_log_not);
3378 i = ax_goto (expr, aop_if_goto);
3379 /* We have to emit 0 - X. */
3380 ax_const_l (expr, 0);
3381 ax_simple (expr, aop_swap);
3382 ax_simple (expr, aop_sub);
3383 ax_label (expr, i, expr->len);
3387 /* No need to sign extend here. */
3388 ax_const_l (expr, 0);
3389 ax_simple (expr, aop_swap);
3390 ax_simple (expr, aop_sub);
3394 /* Sign extend the operand. */
3395 ax_ext (expr, addr_size_bits);
3396 ax_simple (expr, aop_bit_not);
3399 case DW_OP_plus_uconst:
3400 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
3401 /* It would be really weird to emit `DW_OP_plus_uconst 0',
3402 but we micro-optimize anyhow. */
3405 ax_const_l (expr, reg);
3406 ax_simple (expr, aop_add);
3411 ax_simple (expr, aop_bit_and);
3415 /* Sign extend the operands. */
3416 ax_ext (expr, addr_size_bits);
3417 ax_simple (expr, aop_swap);
3418 ax_ext (expr, addr_size_bits);
3419 ax_simple (expr, aop_swap);
3420 ax_simple (expr, aop_div_signed);
3424 ax_simple (expr, aop_sub);
3428 ax_simple (expr, aop_rem_unsigned);
3432 ax_simple (expr, aop_mul);
3436 ax_simple (expr, aop_bit_or);
3440 ax_simple (expr, aop_add);
3444 ax_simple (expr, aop_lsh);
3448 ax_simple (expr, aop_rsh_unsigned);
3452 ax_simple (expr, aop_rsh_signed);
3456 ax_simple (expr, aop_bit_xor);
3460 /* Sign extend the operands. */
3461 ax_ext (expr, addr_size_bits);
3462 ax_simple (expr, aop_swap);
3463 ax_ext (expr, addr_size_bits);
3464 /* Note no swap here: A <= B is !(B < A). */
3465 ax_simple (expr, aop_less_signed);
3466 ax_simple (expr, aop_log_not);
3470 /* Sign extend the operands. */
3471 ax_ext (expr, addr_size_bits);
3472 ax_simple (expr, aop_swap);
3473 ax_ext (expr, addr_size_bits);
3474 ax_simple (expr, aop_swap);
3475 /* A >= B is !(A < B). */
3476 ax_simple (expr, aop_less_signed);
3477 ax_simple (expr, aop_log_not);
3481 /* Sign extend the operands. */
3482 ax_ext (expr, addr_size_bits);
3483 ax_simple (expr, aop_swap);
3484 ax_ext (expr, addr_size_bits);
3485 /* No need for a second swap here. */
3486 ax_simple (expr, aop_equal);
3490 /* Sign extend the operands. */
3491 ax_ext (expr, addr_size_bits);
3492 ax_simple (expr, aop_swap);
3493 ax_ext (expr, addr_size_bits);
3494 ax_simple (expr, aop_swap);
3495 ax_simple (expr, aop_less_signed);
3499 /* Sign extend the operands. */
3500 ax_ext (expr, addr_size_bits);
3501 ax_simple (expr, aop_swap);
3502 ax_ext (expr, addr_size_bits);
3503 /* Note no swap here: A > B is B < A. */
3504 ax_simple (expr, aop_less_signed);
3508 /* Sign extend the operands. */
3509 ax_ext (expr, addr_size_bits);
3510 ax_simple (expr, aop_swap);
3511 ax_ext (expr, addr_size_bits);
3512 /* No need for a swap here. */
3513 ax_simple (expr, aop_equal);
3514 ax_simple (expr, aop_log_not);
3517 case DW_OP_call_frame_cfa:
3520 CORE_ADDR text_offset;
3522 const gdb_byte *cfa_start, *cfa_end;
3524 if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
3526 &text_offset, &cfa_start, &cfa_end))
3529 ax_reg (expr, regnum);
3532 ax_const_l (expr, off);
3533 ax_simple (expr, aop_add);
3538 /* Another expression. */
3539 ax_const_l (expr, text_offset);
3540 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3541 cfa_start, cfa_end, per_cu);
3544 loc->kind = axs_lvalue_memory;
3548 case DW_OP_GNU_push_tls_address:
3549 case DW_OP_form_tls_address:
3553 case DW_OP_push_object_address:
3558 offset = extract_signed_integer (op_ptr, 2, byte_order);
3560 i = ax_goto (expr, aop_goto);
3561 dw_labels.push_back (op_ptr + offset - base);
3562 patches.push_back (i);
3566 offset = extract_signed_integer (op_ptr, 2, byte_order);
3568 /* Zero extend the operand. */
3569 ax_zero_ext (expr, addr_size_bits);
3570 i = ax_goto (expr, aop_if_goto);
3571 dw_labels.push_back (op_ptr + offset - base);
3572 patches.push_back (i);
3579 case DW_OP_bit_piece:
3581 uint64_t size, offset;
3583 if (op_ptr - 1 == previous_piece)
3584 error (_("Cannot translate empty pieces to agent expressions"));
3585 previous_piece = op_ptr - 1;
3587 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3588 if (op == DW_OP_piece)
3594 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
3596 if (bits_collected + size > 8 * sizeof (LONGEST))
3597 error (_("Expression pieces exceed word size"));
3599 /* Access the bits. */
3602 case axs_lvalue_register:
3603 ax_reg (expr, loc->u.reg);
3606 case axs_lvalue_memory:
3607 /* Offset the pointer, if needed. */
3610 ax_const_l (expr, offset / 8);
3611 ax_simple (expr, aop_add);
3614 access_memory (arch, expr, size);
3618 /* For a bits-big-endian target, shift up what we already
3619 have. For a bits-little-endian target, shift up the
3620 new data. Note that there is a potential bug here if
3621 the DWARF expression leaves multiple values on the
3623 if (bits_collected > 0)
3625 if (bits_big_endian)
3627 ax_simple (expr, aop_swap);
3628 ax_const_l (expr, size);
3629 ax_simple (expr, aop_lsh);
3630 /* We don't need a second swap here, because
3631 aop_bit_or is symmetric. */
3635 ax_const_l (expr, size);
3636 ax_simple (expr, aop_lsh);
3638 ax_simple (expr, aop_bit_or);
3641 bits_collected += size;
3642 loc->kind = axs_rvalue;
3646 case DW_OP_GNU_uninit:
3652 struct dwarf2_locexpr_baton block;
3653 int size = (op == DW_OP_call2 ? 2 : 4);
3656 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3659 offset.cu_off = uoffset;
3660 block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
3663 /* DW_OP_call_ref is currently not supported. */
3664 gdb_assert (block.per_cu == per_cu);
3666 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3667 block.data, block.data + block.size,
3672 case DW_OP_call_ref:
3680 /* Patch all the branches we emitted. */
3681 for (i = 0; i < patches.size (); ++i)
3683 int targ = offsets[dw_labels[i]];
3685 internal_error (__FILE__, __LINE__, _("invalid label"));
3686 ax_label (expr, patches[i], targ);
3691 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3692 evaluator to calculate the location. */
3693 static struct value *
3694 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3696 struct dwarf2_locexpr_baton *dlbaton
3697 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3700 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3701 dlbaton->size, dlbaton->per_cu);
3706 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3707 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3710 static struct value *
3711 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3713 struct dwarf2_locexpr_baton *dlbaton
3714 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3716 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3720 /* Implementation of get_symbol_read_needs from
3721 symbol_computed_ops. */
3723 static enum symbol_needs_kind
3724 locexpr_get_symbol_read_needs (struct symbol *symbol)
3726 struct dwarf2_locexpr_baton *dlbaton
3727 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3729 return dwarf2_loc_desc_get_symbol_read_needs (dlbaton->data, dlbaton->size,
3733 /* Return true if DATA points to the end of a piece. END is one past
3734 the last byte in the expression. */
3737 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3739 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3742 /* Helper for locexpr_describe_location_piece that finds the name of a
3746 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3750 /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3751 We'd rather print *something* here than throw an error. */
3752 regnum = dwarf_reg_to_regnum (gdbarch, dwarf_regnum);
3753 /* gdbarch_register_name may just return "", return something more
3754 descriptive for bad register numbers. */
3757 /* The text is output as "$bad_register_number".
3758 That is why we use the underscores. */
3759 return _("bad_register_number");
3761 return gdbarch_register_name (gdbarch, regnum);
3764 /* Nicely describe a single piece of a location, returning an updated
3765 position in the bytecode sequence. This function cannot recognize
3766 all locations; if a location is not recognized, it simply returns
3767 DATA. If there is an error during reading, e.g. we run off the end
3768 of the buffer, an error is thrown. */
3770 static const gdb_byte *
3771 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3772 CORE_ADDR addr, struct objfile *objfile,
3773 struct dwarf2_per_cu_data *per_cu,
3774 const gdb_byte *data, const gdb_byte *end,
3775 unsigned int addr_size)
3777 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3780 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3782 fprintf_filtered (stream, _("a variable in $%s"),
3783 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3786 else if (data[0] == DW_OP_regx)
3790 data = safe_read_uleb128 (data + 1, end, ®);
3791 fprintf_filtered (stream, _("a variable in $%s"),
3792 locexpr_regname (gdbarch, reg));
3794 else if (data[0] == DW_OP_fbreg)
3796 const struct block *b;
3797 struct symbol *framefunc;
3799 int64_t frame_offset;
3800 const gdb_byte *base_data, *new_data, *save_data = data;
3802 int64_t base_offset = 0;
3804 new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3805 if (!piece_end_p (new_data, end))
3809 b = block_for_pc (addr);
3812 error (_("No block found for address for symbol \"%s\"."),
3813 SYMBOL_PRINT_NAME (symbol));
3815 framefunc = block_linkage_function (b);
3818 error (_("No function found for block for symbol \"%s\"."),
3819 SYMBOL_PRINT_NAME (symbol));
3821 func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
3823 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3825 const gdb_byte *buf_end;
3827 frame_reg = base_data[0] - DW_OP_breg0;
3828 buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3830 if (buf_end != base_data + base_size)
3831 error (_("Unexpected opcode after "
3832 "DW_OP_breg%u for symbol \"%s\"."),
3833 frame_reg, SYMBOL_PRINT_NAME (symbol));
3835 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3837 /* The frame base is just the register, with no offset. */
3838 frame_reg = base_data[0] - DW_OP_reg0;
3843 /* We don't know what to do with the frame base expression,
3844 so we can't trace this variable; give up. */
3848 fprintf_filtered (stream,
3849 _("a variable at frame base reg $%s offset %s+%s"),
3850 locexpr_regname (gdbarch, frame_reg),
3851 plongest (base_offset), plongest (frame_offset));
3853 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3854 && piece_end_p (data, end))
3858 data = safe_read_sleb128 (data + 1, end, &offset);
3860 fprintf_filtered (stream,
3861 _("a variable at offset %s from base reg $%s"),
3863 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3866 /* The location expression for a TLS variable looks like this (on a
3869 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3870 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3872 0x3 is the encoding for DW_OP_addr, which has an operand as long
3873 as the size of an address on the target machine (here is 8
3874 bytes). Note that more recent version of GCC emit DW_OP_const4u
3875 or DW_OP_const8u, depending on address size, rather than
3876 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3877 The operand represents the offset at which the variable is within
3878 the thread local storage. */
3880 else if (data + 1 + addr_size < end
3881 && (data[0] == DW_OP_addr
3882 || (addr_size == 4 && data[0] == DW_OP_const4u)
3883 || (addr_size == 8 && data[0] == DW_OP_const8u))
3884 && (data[1 + addr_size] == DW_OP_GNU_push_tls_address
3885 || data[1 + addr_size] == DW_OP_form_tls_address)
3886 && piece_end_p (data + 2 + addr_size, end))
3889 offset = extract_unsigned_integer (data + 1, addr_size,
3890 gdbarch_byte_order (gdbarch));
3892 fprintf_filtered (stream,
3893 _("a thread-local variable at offset 0x%s "
3894 "in the thread-local storage for `%s'"),
3895 phex_nz (offset, addr_size), objfile_name (objfile));
3897 data += 1 + addr_size + 1;
3900 /* With -gsplit-dwarf a TLS variable can also look like this:
3901 DW_AT_location : 3 byte block: fc 4 e0
3902 (DW_OP_GNU_const_index: 4;
3903 DW_OP_GNU_push_tls_address) */
3904 else if (data + 3 <= end
3905 && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3906 && data[0] == DW_OP_GNU_const_index
3908 && (data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3909 || data[1 + leb128_size] == DW_OP_form_tls_address)
3910 && piece_end_p (data + 2 + leb128_size, end))
3914 data = safe_read_uleb128 (data + 1, end, &offset);
3915 offset = dwarf2_read_addr_index (per_cu, offset);
3916 fprintf_filtered (stream,
3917 _("a thread-local variable at offset 0x%s "
3918 "in the thread-local storage for `%s'"),
3919 phex_nz (offset, addr_size), objfile_name (objfile));
3923 else if (data[0] >= DW_OP_lit0
3924 && data[0] <= DW_OP_lit31
3926 && data[1] == DW_OP_stack_value)
3928 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3935 /* Disassemble an expression, stopping at the end of a piece or at the
3936 end of the expression. Returns a pointer to the next unread byte
3937 in the input expression. If ALL is nonzero, then this function
3938 will keep going until it reaches the end of the expression.
3939 If there is an error during reading, e.g. we run off the end
3940 of the buffer, an error is thrown. */
3942 static const gdb_byte *
3943 disassemble_dwarf_expression (struct ui_file *stream,
3944 struct gdbarch *arch, unsigned int addr_size,
3945 int offset_size, const gdb_byte *start,
3946 const gdb_byte *data, const gdb_byte *end,
3947 int indent, int all,
3948 struct dwarf2_per_cu_data *per_cu)
3952 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3954 enum dwarf_location_atom op = (enum dwarf_location_atom) *data++;
3959 name = get_DW_OP_name (op);
3962 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3963 op, (long) (data - 1 - start));
3964 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3965 (long) (data - 1 - start), name);
3970 ul = extract_unsigned_integer (data, addr_size,
3971 gdbarch_byte_order (arch));
3973 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3977 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3979 fprintf_filtered (stream, " %s", pulongest (ul));
3982 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3984 fprintf_filtered (stream, " %s", plongest (l));
3987 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3989 fprintf_filtered (stream, " %s", pulongest (ul));
3992 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3994 fprintf_filtered (stream, " %s", plongest (l));
3997 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3999 fprintf_filtered (stream, " %s", pulongest (ul));
4002 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
4004 fprintf_filtered (stream, " %s", plongest (l));
4007 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
4009 fprintf_filtered (stream, " %s", pulongest (ul));
4012 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
4014 fprintf_filtered (stream, " %s", plongest (l));
4017 data = safe_read_uleb128 (data, end, &ul);
4018 fprintf_filtered (stream, " %s", pulongest (ul));
4021 data = safe_read_sleb128 (data, end, &l);
4022 fprintf_filtered (stream, " %s", plongest (l));
4057 fprintf_filtered (stream, " [$%s]",
4058 locexpr_regname (arch, op - DW_OP_reg0));
4062 data = safe_read_uleb128 (data, end, &ul);
4063 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
4064 locexpr_regname (arch, (int) ul));
4067 case DW_OP_implicit_value:
4068 data = safe_read_uleb128 (data, end, &ul);
4070 fprintf_filtered (stream, " %s", pulongest (ul));
4105 data = safe_read_sleb128 (data, end, &l);
4106 fprintf_filtered (stream, " %s [$%s]", plongest (l),
4107 locexpr_regname (arch, op - DW_OP_breg0));
4111 data = safe_read_uleb128 (data, end, &ul);
4112 data = safe_read_sleb128 (data, end, &l);
4113 fprintf_filtered (stream, " register %s [$%s] offset %s",
4115 locexpr_regname (arch, (int) ul),
4120 data = safe_read_sleb128 (data, end, &l);
4121 fprintf_filtered (stream, " %s", plongest (l));
4124 case DW_OP_xderef_size:
4125 case DW_OP_deref_size:
4127 fprintf_filtered (stream, " %d", *data);
4131 case DW_OP_plus_uconst:
4132 data = safe_read_uleb128 (data, end, &ul);
4133 fprintf_filtered (stream, " %s", pulongest (ul));
4137 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4139 fprintf_filtered (stream, " to %ld",
4140 (long) (data + l - start));
4144 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4146 fprintf_filtered (stream, " %ld",
4147 (long) (data + l - start));
4151 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
4153 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
4157 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4159 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4162 case DW_OP_call_ref:
4163 ul = extract_unsigned_integer (data, offset_size,
4164 gdbarch_byte_order (arch));
4165 data += offset_size;
4166 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4170 data = safe_read_uleb128 (data, end, &ul);
4171 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
4174 case DW_OP_bit_piece:
4178 data = safe_read_uleb128 (data, end, &ul);
4179 data = safe_read_uleb128 (data, end, &offset);
4180 fprintf_filtered (stream, " size %s offset %s (bits)",
4181 pulongest (ul), pulongest (offset));
4185 case DW_OP_GNU_implicit_pointer:
4187 ul = extract_unsigned_integer (data, offset_size,
4188 gdbarch_byte_order (arch));
4189 data += offset_size;
4191 data = safe_read_sleb128 (data, end, &l);
4193 fprintf_filtered (stream, " DIE %s offset %s",
4194 phex_nz (ul, offset_size),
4199 case DW_OP_GNU_deref_type:
4201 int addr_size = *data++;
4205 data = safe_read_uleb128 (data, end, &ul);
4207 type = dwarf2_get_die_type (offset, per_cu);
4208 fprintf_filtered (stream, "<");
4209 type_print (type, "", stream, -1);
4210 fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
4215 case DW_OP_GNU_const_type:
4220 data = safe_read_uleb128 (data, end, &ul);
4221 type_die.cu_off = ul;
4222 type = dwarf2_get_die_type (type_die, per_cu);
4223 fprintf_filtered (stream, "<");
4224 type_print (type, "", stream, -1);
4225 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
4229 case DW_OP_GNU_regval_type:
4235 data = safe_read_uleb128 (data, end, ®);
4236 data = safe_read_uleb128 (data, end, &ul);
4237 type_die.cu_off = ul;
4239 type = dwarf2_get_die_type (type_die, per_cu);
4240 fprintf_filtered (stream, "<");
4241 type_print (type, "", stream, -1);
4242 fprintf_filtered (stream, " [0x%s]> [$%s]",
4243 phex_nz (type_die.cu_off, 0),
4244 locexpr_regname (arch, reg));
4248 case DW_OP_GNU_convert:
4249 case DW_OP_GNU_reinterpret:
4253 data = safe_read_uleb128 (data, end, &ul);
4254 type_die.cu_off = ul;
4256 if (type_die.cu_off == 0)
4257 fprintf_filtered (stream, "<0>");
4262 type = dwarf2_get_die_type (type_die, per_cu);
4263 fprintf_filtered (stream, "<");
4264 type_print (type, "", stream, -1);
4265 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
4270 case DW_OP_GNU_entry_value:
4271 data = safe_read_uleb128 (data, end, &ul);
4272 fputc_filtered ('\n', stream);
4273 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
4274 start, data, data + ul, indent + 2,
4279 case DW_OP_GNU_parameter_ref:
4280 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4282 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4285 case DW_OP_GNU_addr_index:
4286 data = safe_read_uleb128 (data, end, &ul);
4287 ul = dwarf2_read_addr_index (per_cu, ul);
4288 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
4290 case DW_OP_GNU_const_index:
4291 data = safe_read_uleb128 (data, end, &ul);
4292 ul = dwarf2_read_addr_index (per_cu, ul);
4293 fprintf_filtered (stream, " %s", pulongest (ul));
4297 fprintf_filtered (stream, "\n");
4303 /* Describe a single location, which may in turn consist of multiple
4307 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
4308 struct ui_file *stream,
4309 const gdb_byte *data, size_t size,
4310 struct objfile *objfile, unsigned int addr_size,
4311 int offset_size, struct dwarf2_per_cu_data *per_cu)
4313 const gdb_byte *end = data + size;
4314 int first_piece = 1, bad = 0;
4318 const gdb_byte *here = data;
4319 int disassemble = 1;
4324 fprintf_filtered (stream, _(", and "));
4326 if (!dwarf_always_disassemble)
4328 data = locexpr_describe_location_piece (symbol, stream,
4329 addr, objfile, per_cu,
4330 data, end, addr_size);
4331 /* If we printed anything, or if we have an empty piece,
4332 then don't disassemble. */
4334 || data[0] == DW_OP_piece
4335 || data[0] == DW_OP_bit_piece)
4340 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
4341 data = disassemble_dwarf_expression (stream,
4342 get_objfile_arch (objfile),
4343 addr_size, offset_size, data,
4345 dwarf_always_disassemble,
4351 int empty = data == here;
4354 fprintf_filtered (stream, " ");
4355 if (data[0] == DW_OP_piece)
4359 data = safe_read_uleb128 (data + 1, end, &bytes);
4362 fprintf_filtered (stream, _("an empty %s-byte piece"),
4365 fprintf_filtered (stream, _(" [%s-byte piece]"),
4368 else if (data[0] == DW_OP_bit_piece)
4370 uint64_t bits, offset;
4372 data = safe_read_uleb128 (data + 1, end, &bits);
4373 data = safe_read_uleb128 (data, end, &offset);
4376 fprintf_filtered (stream,
4377 _("an empty %s-bit piece"),
4380 fprintf_filtered (stream,
4381 _(" [%s-bit piece, offset %s bits]"),
4382 pulongest (bits), pulongest (offset));
4392 if (bad || data > end)
4393 error (_("Corrupted DWARF2 expression for \"%s\"."),
4394 SYMBOL_PRINT_NAME (symbol));
4397 /* Print a natural-language description of SYMBOL to STREAM. This
4398 version is for a symbol with a single location. */
4401 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
4402 struct ui_file *stream)
4404 struct dwarf2_locexpr_baton *dlbaton
4405 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4406 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4407 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4408 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4410 locexpr_describe_location_1 (symbol, addr, stream,
4411 dlbaton->data, dlbaton->size,
4412 objfile, addr_size, offset_size,
4416 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4417 any necessary bytecode in AX. */
4420 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4421 struct agent_expr *ax, struct axs_value *value)
4423 struct dwarf2_locexpr_baton *dlbaton
4424 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4425 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4427 if (dlbaton->size == 0)
4428 value->optimized_out = 1;
4430 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
4431 dlbaton->data, dlbaton->data + dlbaton->size,
4435 /* symbol_computed_ops 'generate_c_location' method. */
4438 locexpr_generate_c_location (struct symbol *sym, struct ui_file *stream,
4439 struct gdbarch *gdbarch,
4440 unsigned char *registers_used,
4441 CORE_ADDR pc, const char *result_name)
4443 struct dwarf2_locexpr_baton *dlbaton
4444 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
4445 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4447 if (dlbaton->size == 0)
4448 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4450 compile_dwarf_expr_to_c (stream, result_name,
4451 sym, pc, gdbarch, registers_used, addr_size,
4452 dlbaton->data, dlbaton->data + dlbaton->size,
4456 /* The set of location functions used with the DWARF-2 expression
4458 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4459 locexpr_read_variable,
4460 locexpr_read_variable_at_entry,
4461 locexpr_get_symbol_read_needs,
4462 locexpr_describe_location,
4463 0, /* location_has_loclist */
4464 locexpr_tracepoint_var_ref,
4465 locexpr_generate_c_location
4469 /* Wrapper functions for location lists. These generally find
4470 the appropriate location expression and call something above. */
4472 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4473 evaluator to calculate the location. */
4474 static struct value *
4475 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4477 struct dwarf2_loclist_baton *dlbaton
4478 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4480 const gdb_byte *data;
4482 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
4484 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4485 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4491 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4492 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4495 Function always returns non-NULL value, it may be marked optimized out if
4496 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
4497 if it cannot resolve the parameter for any reason. */
4499 static struct value *
4500 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4502 struct dwarf2_loclist_baton *dlbaton
4503 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4504 const gdb_byte *data;
4508 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4509 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4511 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4513 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4515 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4518 /* Implementation of get_symbol_read_needs from
4519 symbol_computed_ops. */
4521 static enum symbol_needs_kind
4522 loclist_symbol_needs (struct symbol *symbol)
4524 /* If there's a location list, then assume we need to have a frame
4525 to choose the appropriate location expression. With tracking of
4526 global variables this is not necessarily true, but such tracking
4527 is disabled in GCC at the moment until we figure out how to
4530 return SYMBOL_NEEDS_FRAME;
4533 /* Print a natural-language description of SYMBOL to STREAM. This
4534 version applies when there is a list of different locations, each
4535 with a specified address range. */
4538 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4539 struct ui_file *stream)
4541 struct dwarf2_loclist_baton *dlbaton
4542 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4543 const gdb_byte *loc_ptr, *buf_end;
4544 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4545 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4546 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4547 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4548 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4549 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4550 /* Adjust base_address for relocatable objects. */
4551 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
4552 CORE_ADDR base_address = dlbaton->base_address + base_offset;
4555 loc_ptr = dlbaton->data;
4556 buf_end = dlbaton->data + dlbaton->size;
4558 fprintf_filtered (stream, _("multi-location:\n"));
4560 /* Iterate through locations until we run out. */
4563 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4565 enum debug_loc_kind kind;
4566 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4568 if (dlbaton->from_dwo)
4569 kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4570 loc_ptr, buf_end, &new_ptr,
4571 &low, &high, byte_order);
4573 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4575 byte_order, addr_size,
4580 case DEBUG_LOC_END_OF_LIST:
4583 case DEBUG_LOC_BASE_ADDRESS:
4584 base_address = high + base_offset;
4585 fprintf_filtered (stream, _(" Base address %s"),
4586 paddress (gdbarch, base_address));
4588 case DEBUG_LOC_START_END:
4589 case DEBUG_LOC_START_LENGTH:
4591 case DEBUG_LOC_BUFFER_OVERFLOW:
4592 case DEBUG_LOC_INVALID_ENTRY:
4593 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4594 SYMBOL_PRINT_NAME (symbol));
4596 gdb_assert_not_reached ("bad debug_loc_kind");
4599 /* Otherwise, a location expression entry. */
4600 low += base_address;
4601 high += base_address;
4603 low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
4604 high = gdbarch_adjust_dwarf2_addr (gdbarch, high);
4606 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4609 /* (It would improve readability to print only the minimum
4610 necessary digits of the second number of the range.) */
4611 fprintf_filtered (stream, _(" Range %s-%s: "),
4612 paddress (gdbarch, low), paddress (gdbarch, high));
4614 /* Now describe this particular location. */
4615 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4616 objfile, addr_size, offset_size,
4619 fprintf_filtered (stream, "\n");
4625 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4626 any necessary bytecode in AX. */
4628 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4629 struct agent_expr *ax, struct axs_value *value)
4631 struct dwarf2_loclist_baton *dlbaton
4632 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4633 const gdb_byte *data;
4635 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4637 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4639 value->optimized_out = 1;
4641 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
4645 /* symbol_computed_ops 'generate_c_location' method. */
4648 loclist_generate_c_location (struct symbol *sym, struct ui_file *stream,
4649 struct gdbarch *gdbarch,
4650 unsigned char *registers_used,
4651 CORE_ADDR pc, const char *result_name)
4653 struct dwarf2_loclist_baton *dlbaton
4654 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
4655 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4656 const gdb_byte *data;
4659 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4661 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4663 compile_dwarf_expr_to_c (stream, result_name,
4664 sym, pc, gdbarch, registers_used, addr_size,
4669 /* The set of location functions used with the DWARF-2 expression
4670 evaluator and location lists. */
4671 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4672 loclist_read_variable,
4673 loclist_read_variable_at_entry,
4674 loclist_symbol_needs,
4675 loclist_describe_location,
4676 1, /* location_has_loclist */
4677 loclist_tracepoint_var_ref,
4678 loclist_generate_c_location
4681 /* Provide a prototype to silence -Wmissing-prototypes. */
4682 extern initialize_file_ftype _initialize_dwarf2loc;
4685 _initialize_dwarf2loc (void)
4687 add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4688 &entry_values_debug,
4689 _("Set entry values and tail call frames "
4691 _("Show entry values and tail call frames "
4693 _("When non-zero, the process of determining "
4694 "parameter values from function entry point "
4695 "and tail call frames will be printed."),
4697 show_entry_values_debug,
4698 &setdebuglist, &showdebuglist);