1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007-2012 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/>. */
33 #include "exceptions.h"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
47 extern int dwarf2_always_disassemble;
49 static void dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
50 const gdb_byte **start, size_t *length);
52 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;
54 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
55 struct frame_info *frame,
58 struct dwarf2_per_cu_data *per_cu,
61 /* A function for dealing with location lists. Given a
62 symbol baton (BATON) and a pc value (PC), find the appropriate
63 location expression, set *LOCEXPR_LENGTH, and return a pointer
64 to the beginning of the expression. Returns NULL on failure.
66 For now, only return the first matching location expression; there
67 can be more than one in the list. */
70 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
71 size_t *locexpr_length, CORE_ADDR pc)
74 const gdb_byte *loc_ptr, *buf_end;
76 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
77 struct gdbarch *gdbarch = get_objfile_arch (objfile);
78 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
79 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
80 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
81 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
82 /* Adjust base_address for relocatable objects. */
83 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
84 CORE_ADDR base_address = baton->base_address + base_offset;
86 loc_ptr = baton->data;
87 buf_end = baton->data + baton->size;
91 if (buf_end - loc_ptr < 2 * addr_size)
92 error (_("dwarf2_find_location_expression: "
93 "Corrupted DWARF expression."));
96 low = extract_signed_integer (loc_ptr, addr_size, byte_order);
98 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
102 high = extract_signed_integer (loc_ptr, addr_size, byte_order);
104 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
105 loc_ptr += addr_size;
107 /* A base-address-selection entry. */
108 if ((low & base_mask) == base_mask)
110 base_address = high + base_offset;
114 /* An end-of-list entry. */
115 if (low == 0 && high == 0)
121 /* Otherwise, a location expression entry. */
123 high += base_address;
125 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
128 if (low == high && pc == low)
130 /* This is entry PC record present only at entry point
131 of a function. Verify it is really the function entry point. */
133 struct block *pc_block = block_for_pc (pc);
134 struct symbol *pc_func = NULL;
137 pc_func = block_linkage_function (pc_block);
139 if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
141 *locexpr_length = length;
146 if (pc >= low && pc < high)
148 *locexpr_length = length;
156 /* This is the baton used when performing dwarf2 expression
158 struct dwarf_expr_baton
160 struct frame_info *frame;
161 struct dwarf2_per_cu_data *per_cu;
164 /* Helper functions for dwarf2_evaluate_loc_desc. */
166 /* Using the frame specified in BATON, return the value of register
167 REGNUM, treated as a pointer. */
169 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
171 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
172 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
176 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
177 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
178 regnum, debaton->frame);
182 /* Read memory at ADDR (length LEN) into BUF. */
185 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
187 read_memory (addr, buf, len);
190 /* Using the frame specified in BATON, find the location expression
191 describing the frame base. Return a pointer to it in START and
192 its length in LENGTH. */
194 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
196 /* FIXME: cagney/2003-03-26: This code should be using
197 get_frame_base_address(), and then implement a dwarf2 specific
199 struct symbol *framefunc;
200 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
202 /* Use block_linkage_function, which returns a real (not inlined)
203 function, instead of get_frame_function, which may return an
205 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
207 /* If we found a frame-relative symbol then it was certainly within
208 some function associated with a frame. If we can't find the frame,
209 something has gone wrong. */
210 gdb_assert (framefunc != NULL);
212 dwarf_expr_frame_base_1 (framefunc,
213 get_frame_address_in_block (debaton->frame),
218 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
219 const gdb_byte **start, size_t *length)
221 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
223 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
225 struct dwarf2_loclist_baton *symbaton;
227 symbaton = SYMBOL_LOCATION_BATON (framefunc);
228 *start = dwarf2_find_location_expression (symbaton, length, pc);
232 struct dwarf2_locexpr_baton *symbaton;
234 symbaton = SYMBOL_LOCATION_BATON (framefunc);
235 if (symbaton != NULL)
237 *length = symbaton->size;
238 *start = symbaton->data;
245 error (_("Could not find the frame base for \"%s\"."),
246 SYMBOL_NATURAL_NAME (framefunc));
249 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
250 the frame in BATON. */
253 dwarf_expr_frame_cfa (void *baton)
255 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
257 return dwarf2_frame_cfa (debaton->frame);
260 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
261 the frame in BATON. */
264 dwarf_expr_frame_pc (void *baton)
266 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
268 return get_frame_address_in_block (debaton->frame);
271 /* Using the objfile specified in BATON, find the address for the
272 current thread's thread-local storage with offset OFFSET. */
274 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
276 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
277 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
279 return target_translate_tls_address (objfile, offset);
282 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
283 current CU (as is PER_CU). State of the CTX is not affected by the
287 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
288 struct dwarf2_per_cu_data *per_cu,
289 CORE_ADDR (*get_frame_pc) (void *baton),
292 struct dwarf2_locexpr_baton block;
294 block = dwarf2_fetch_die_location_block (die_offset, per_cu,
295 get_frame_pc, baton);
297 /* DW_OP_call_ref is currently not supported. */
298 gdb_assert (block.per_cu == per_cu);
300 dwarf_expr_eval (ctx, block.data, block.size);
303 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
306 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
308 struct dwarf_expr_baton *debaton = ctx->baton;
310 per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
311 ctx->funcs->get_frame_pc, ctx->baton);
314 /* Callback function for dwarf2_evaluate_loc_desc. */
317 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
318 cu_offset die_offset)
320 struct dwarf_expr_baton *debaton = ctx->baton;
322 return dwarf2_get_die_type (die_offset, debaton->per_cu);
325 /* See dwarf2loc.h. */
327 int entry_values_debug = 0;
329 /* Helper to set entry_values_debug. */
332 show_entry_values_debug (struct ui_file *file, int from_tty,
333 struct cmd_list_element *c, const char *value)
335 fprintf_filtered (file,
336 _("Entry values and tail call frames debugging is %s.\n"),
340 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
341 CALLER_FRAME (for registers) can be NULL if it is not known. This function
342 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
345 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
346 struct call_site *call_site,
347 struct frame_info *caller_frame)
349 switch (FIELD_LOC_KIND (call_site->target))
351 case FIELD_LOC_KIND_DWARF_BLOCK:
353 struct dwarf2_locexpr_baton *dwarf_block;
355 struct type *caller_core_addr_type;
356 struct gdbarch *caller_arch;
358 dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
359 if (dwarf_block == NULL)
361 struct minimal_symbol *msym;
363 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
364 throw_error (NO_ENTRY_VALUE_ERROR,
365 _("DW_AT_GNU_call_site_target is not specified "
367 paddress (call_site_gdbarch, call_site->pc),
368 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
371 if (caller_frame == NULL)
373 struct minimal_symbol *msym;
375 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
376 throw_error (NO_ENTRY_VALUE_ERROR,
377 _("DW_AT_GNU_call_site_target DWARF block resolving "
378 "requires known frame which is currently not "
379 "available at %s in %s"),
380 paddress (call_site_gdbarch, call_site->pc),
381 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
384 caller_arch = get_frame_arch (caller_frame);
385 caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
386 val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
387 dwarf_block->data, dwarf_block->size,
388 dwarf_block->per_cu);
389 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
391 if (VALUE_LVAL (val) == lval_memory)
392 return value_address (val);
394 return value_as_address (val);
397 case FIELD_LOC_KIND_PHYSNAME:
399 const char *physname;
400 struct minimal_symbol *msym;
402 physname = FIELD_STATIC_PHYSNAME (call_site->target);
403 msym = lookup_minimal_symbol_text (physname, NULL);
406 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
407 throw_error (NO_ENTRY_VALUE_ERROR,
408 _("Cannot find function \"%s\" for a call site target "
410 physname, paddress (call_site_gdbarch, call_site->pc),
411 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
414 return SYMBOL_VALUE_ADDRESS (msym);
417 case FIELD_LOC_KIND_PHYSADDR:
418 return FIELD_STATIC_PHYSADDR (call_site->target);
421 internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
425 /* Convert function entry point exact address ADDR to the function which is
426 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
427 NO_ENTRY_VALUE_ERROR otherwise. */
429 static struct symbol *
430 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
432 struct symbol *sym = find_pc_function (addr);
435 if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
436 throw_error (NO_ENTRY_VALUE_ERROR,
437 _("DW_TAG_GNU_call_site resolving failed to find function "
438 "name for address %s"),
439 paddress (gdbarch, addr));
441 type = SYMBOL_TYPE (sym);
442 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
443 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
448 /* Verify function with entry point exact address ADDR can never call itself
449 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
450 can call itself via tail calls.
452 If a funtion can tail call itself its entry value based parameters are
453 unreliable. There is no verification whether the value of some/all
454 parameters is unchanged through the self tail call, we expect if there is
455 a self tail call all the parameters can be modified. */
458 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
460 struct obstack addr_obstack;
461 struct cleanup *old_chain;
464 /* Track here CORE_ADDRs which were already visited. */
467 /* The verification is completely unordered. Track here function addresses
468 which still need to be iterated. */
469 VEC (CORE_ADDR) *todo = NULL;
471 obstack_init (&addr_obstack);
472 old_chain = make_cleanup_obstack_free (&addr_obstack);
473 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
474 &addr_obstack, hashtab_obstack_allocate,
476 make_cleanup_htab_delete (addr_hash);
478 make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
480 VEC_safe_push (CORE_ADDR, todo, verify_addr);
481 while (!VEC_empty (CORE_ADDR, todo))
483 struct symbol *func_sym;
484 struct call_site *call_site;
486 addr = VEC_pop (CORE_ADDR, todo);
488 func_sym = func_addr_to_tail_call_list (gdbarch, addr);
490 for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
491 call_site; call_site = call_site->tail_call_next)
493 CORE_ADDR target_addr;
496 /* CALLER_FRAME with registers is not available for tail-call jumped
498 target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
500 if (target_addr == verify_addr)
502 struct minimal_symbol *msym;
504 msym = lookup_minimal_symbol_by_pc (verify_addr);
505 throw_error (NO_ENTRY_VALUE_ERROR,
506 _("DW_OP_GNU_entry_value resolving has found "
507 "function \"%s\" at %s can call itself via tail "
509 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym),
510 paddress (gdbarch, verify_addr));
513 slot = htab_find_slot (addr_hash, &target_addr, INSERT);
516 *slot = obstack_copy (&addr_obstack, &target_addr,
517 sizeof (target_addr));
518 VEC_safe_push (CORE_ADDR, todo, target_addr);
523 do_cleanups (old_chain);
526 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
527 ENTRY_VALUES_DEBUG. */
530 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
532 CORE_ADDR addr = call_site->pc;
533 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (addr - 1);
535 fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
536 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
540 /* vec.h needs single word type name, typedef it. */
541 typedef struct call_site *call_sitep;
543 /* Define VEC (call_sitep) functions. */
544 DEF_VEC_P (call_sitep);
546 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
547 only top callers and bottom callees which are present in both. GDBARCH is
548 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
549 no remaining possibilities to provide unambiguous non-trivial result.
550 RESULTP should point to NULL on the first (initialization) call. Caller is
551 responsible for xfree of any RESULTP data. */
554 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
555 VEC (call_sitep) *chain)
557 struct call_site_chain *result = *resultp;
558 long length = VEC_length (call_sitep, chain);
559 int callers, callees, idx;
563 /* Create the initial chain containing all the passed PCs. */
565 result = xmalloc (sizeof (*result) + sizeof (*result->call_site)
567 result->length = length;
568 result->callers = result->callees = length;
569 memcpy (result->call_site, VEC_address (call_sitep, chain),
570 sizeof (*result->call_site) * length);
573 if (entry_values_debug)
575 fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
576 for (idx = 0; idx < length; idx++)
577 tailcall_dump (gdbarch, result->call_site[idx]);
578 fputc_unfiltered ('\n', gdb_stdlog);
584 if (entry_values_debug)
586 fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
587 for (idx = 0; idx < length; idx++)
588 tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
589 fputc_unfiltered ('\n', gdb_stdlog);
592 /* Intersect callers. */
594 callers = min (result->callers, length);
595 for (idx = 0; idx < callers; idx++)
596 if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
598 result->callers = idx;
602 /* Intersect callees. */
604 callees = min (result->callees, length);
605 for (idx = 0; idx < callees; idx++)
606 if (result->call_site[result->length - 1 - idx]
607 != VEC_index (call_sitep, chain, length - 1 - idx))
609 result->callees = idx;
613 if (entry_values_debug)
615 fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
616 for (idx = 0; idx < result->callers; idx++)
617 tailcall_dump (gdbarch, result->call_site[idx]);
618 fputs_unfiltered (" |", gdb_stdlog);
619 for (idx = 0; idx < result->callees; idx++)
620 tailcall_dump (gdbarch, result->call_site[result->length
621 - result->callees + idx]);
622 fputc_unfiltered ('\n', gdb_stdlog);
625 if (result->callers == 0 && result->callees == 0)
627 /* There are no common callers or callees. It could be also a direct
628 call (which has length 0) with ambiguous possibility of an indirect
629 call - CALLERS == CALLEES == 0 is valid during the first allocation
630 but any subsequence processing of such entry means ambiguity. */
636 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
637 PC again. In such case there must be two different code paths to reach
638 it, therefore some of the former determined intermediate PCs must differ
639 and the unambiguous chain gets shortened. */
640 gdb_assert (result->callers + result->callees < result->length);
643 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
644 assumed frames between them use GDBARCH. Use depth first search so we can
645 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
646 would have needless GDB stack overhead. Caller is responsible for xfree of
647 the returned result. Any unreliability results in thrown
648 NO_ENTRY_VALUE_ERROR. */
650 static struct call_site_chain *
651 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
654 struct func_type *func_specific;
655 struct obstack addr_obstack;
656 struct cleanup *back_to_retval, *back_to_workdata;
657 struct call_site_chain *retval = NULL;
658 struct call_site *call_site;
660 /* Mark CALL_SITEs so we do not visit the same ones twice. */
663 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
664 call_site nor any possible call_site at CALLEE_PC's function is there.
665 Any CALL_SITE in CHAIN will be iterated to its siblings - via
666 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
667 VEC (call_sitep) *chain = NULL;
669 /* We are not interested in the specific PC inside the callee function. */
670 callee_pc = get_pc_function_start (callee_pc);
672 throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
673 paddress (gdbarch, callee_pc));
675 back_to_retval = make_cleanup (free_current_contents, &retval);
677 obstack_init (&addr_obstack);
678 back_to_workdata = make_cleanup_obstack_free (&addr_obstack);
679 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
680 &addr_obstack, hashtab_obstack_allocate,
682 make_cleanup_htab_delete (addr_hash);
684 make_cleanup (VEC_cleanup (call_sitep), &chain);
686 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
687 at the target's function. All the possible tail call sites in the
688 target's function will get iterated as already pushed into CHAIN via their
690 call_site = call_site_for_pc (gdbarch, caller_pc);
694 CORE_ADDR target_func_addr;
695 struct call_site *target_call_site;
697 /* CALLER_FRAME with registers is not available for tail-call jumped
699 target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
701 if (target_func_addr == callee_pc)
703 chain_candidate (gdbarch, &retval, chain);
707 /* There is no way to reach CALLEE_PC again as we would prevent
708 entering it twice as being already marked in ADDR_HASH. */
709 target_call_site = NULL;
713 struct symbol *target_func;
715 target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
716 target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
721 /* Attempt to visit TARGET_CALL_SITE. */
723 if (target_call_site)
727 slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
730 /* Successfully entered TARGET_CALL_SITE. */
732 *slot = &target_call_site->pc;
733 VEC_safe_push (call_sitep, chain, target_call_site);
738 /* Backtrack (without revisiting the originating call_site). Try the
739 callers's sibling; if there isn't any try the callers's callers's
742 target_call_site = NULL;
743 while (!VEC_empty (call_sitep, chain))
745 call_site = VEC_pop (call_sitep, chain);
747 gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
749 htab_remove_elt (addr_hash, &call_site->pc);
751 target_call_site = call_site->tail_call_next;
752 if (target_call_site)
756 while (target_call_site);
758 if (VEC_empty (call_sitep, chain))
761 call_site = VEC_last (call_sitep, chain);
766 struct minimal_symbol *msym_caller, *msym_callee;
768 msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
769 msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
770 throw_error (NO_ENTRY_VALUE_ERROR,
771 _("There are no unambiguously determinable intermediate "
772 "callers or callees between caller function \"%s\" at %s "
773 "and callee function \"%s\" at %s"),
775 ? "???" : SYMBOL_PRINT_NAME (msym_caller)),
776 paddress (gdbarch, caller_pc),
778 ? "???" : SYMBOL_PRINT_NAME (msym_callee)),
779 paddress (gdbarch, callee_pc));
782 do_cleanups (back_to_workdata);
783 discard_cleanups (back_to_retval);
787 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
788 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
789 constructed return NULL. Caller is responsible for xfree of the returned
792 struct call_site_chain *
793 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
796 volatile struct gdb_exception e;
797 struct call_site_chain *retval = NULL;
799 TRY_CATCH (e, RETURN_MASK_ERROR)
801 retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
805 if (e.error == NO_ENTRY_VALUE_ERROR)
807 if (entry_values_debug)
808 exception_print (gdb_stdout, e);
818 /* Fetch call_site_parameter from caller matching the parameters. FRAME is for
819 callee. See DWARF_REG and FB_OFFSET description at struct
820 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
822 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
825 static struct call_site_parameter *
826 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame, int dwarf_reg,
828 struct dwarf2_per_cu_data **per_cu_return)
830 CORE_ADDR func_addr = get_frame_func (frame);
832 struct gdbarch *gdbarch = get_frame_arch (frame);
833 struct frame_info *caller_frame = get_prev_frame (frame);
834 struct call_site *call_site;
837 struct dwarf2_locexpr_baton *dwarf_block;
838 /* Initialize it just to avoid a GCC false warning. */
839 struct call_site_parameter *parameter = NULL;
840 CORE_ADDR target_addr;
842 if (gdbarch != frame_unwind_arch (frame))
844 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr);
845 struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
847 throw_error (NO_ENTRY_VALUE_ERROR,
848 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
849 "(of %s (%s)) does not match caller gdbarch %s"),
850 gdbarch_bfd_arch_info (gdbarch)->printable_name,
851 paddress (gdbarch, func_addr),
852 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym),
853 gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
856 if (caller_frame == NULL)
858 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr);
860 throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
861 "requires caller of %s (%s)"),
862 paddress (gdbarch, func_addr),
863 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
865 caller_pc = get_frame_pc (caller_frame);
866 call_site = call_site_for_pc (gdbarch, caller_pc);
868 target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
869 if (target_addr != func_addr)
871 struct minimal_symbol *target_msym, *func_msym;
873 target_msym = lookup_minimal_symbol_by_pc (target_addr);
874 func_msym = lookup_minimal_symbol_by_pc (func_addr);
875 throw_error (NO_ENTRY_VALUE_ERROR,
876 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
877 "but the called frame is for %s at %s"),
878 (target_msym == NULL ? "???"
879 : SYMBOL_PRINT_NAME (target_msym)),
880 paddress (gdbarch, target_addr),
881 func_msym == NULL ? "???" : SYMBOL_PRINT_NAME (func_msym),
882 paddress (gdbarch, func_addr));
885 /* No entry value based parameters would be reliable if this function can
886 call itself via tail calls. */
887 func_verify_no_selftailcall (gdbarch, func_addr);
889 for (iparams = 0; iparams < call_site->parameter_count; iparams++)
891 parameter = &call_site->parameter[iparams];
892 if (parameter->dwarf_reg == -1 && dwarf_reg == -1)
894 if (parameter->fb_offset == fb_offset)
897 else if (parameter->dwarf_reg == dwarf_reg)
900 if (iparams == call_site->parameter_count)
902 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (caller_pc);
904 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
905 determine its value. */
906 throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
907 "at DW_TAG_GNU_call_site %s at %s"),
908 paddress (gdbarch, caller_pc),
909 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
912 *per_cu_return = call_site->per_cu;
916 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
917 the normal DW_AT_GNU_call_site_value block. Otherwise return the
918 DW_AT_GNU_call_site_data_value (dereferenced) block.
920 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
923 Function always returns non-NULL, non-optimized out value. It throws
924 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
926 static struct value *
927 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
928 CORE_ADDR deref_size, struct type *type,
929 struct frame_info *caller_frame,
930 struct dwarf2_per_cu_data *per_cu)
932 const gdb_byte *data_src;
936 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
937 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
939 /* DEREF_SIZE size is not verified here. */
940 if (data_src == NULL)
941 throw_error (NO_ENTRY_VALUE_ERROR,
942 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
944 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
945 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
947 data = alloca (size + 1);
948 memcpy (data, data_src, size);
949 data[size] = DW_OP_stack_value;
951 return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
954 /* Execute call_site_parameter's DWARF block matching DEREF_SIZE for caller of
955 the CTX's frame. CTX must be of dwarf_expr_ctx_funcs kind. See DWARF_REG
956 and FB_OFFSET description at struct
957 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
959 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
960 can be more simple as it does not support cross-CU DWARF executions. */
963 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
964 int dwarf_reg, CORE_ADDR fb_offset,
967 struct dwarf_expr_baton *debaton;
968 struct frame_info *frame, *caller_frame;
969 struct dwarf2_per_cu_data *caller_per_cu;
970 struct dwarf_expr_baton baton_local;
971 struct dwarf_expr_context saved_ctx;
972 struct call_site_parameter *parameter;
973 const gdb_byte *data_src;
976 gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
977 debaton = ctx->baton;
978 frame = debaton->frame;
979 caller_frame = get_prev_frame (frame);
981 parameter = dwarf_expr_reg_to_entry_parameter (frame, dwarf_reg, fb_offset,
983 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
984 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
986 /* DEREF_SIZE size is not verified here. */
987 if (data_src == NULL)
988 throw_error (NO_ENTRY_VALUE_ERROR,
989 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
991 baton_local.frame = caller_frame;
992 baton_local.per_cu = caller_per_cu;
994 saved_ctx.gdbarch = ctx->gdbarch;
995 saved_ctx.addr_size = ctx->addr_size;
996 saved_ctx.offset = ctx->offset;
997 saved_ctx.baton = ctx->baton;
998 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
999 ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
1000 ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
1001 ctx->baton = &baton_local;
1003 dwarf_expr_eval (ctx, data_src, size);
1005 ctx->gdbarch = saved_ctx.gdbarch;
1006 ctx->addr_size = saved_ctx.addr_size;
1007 ctx->offset = saved_ctx.offset;
1008 ctx->baton = saved_ctx.baton;
1011 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1012 the indirect method on it, that is use its stored target value, the sole
1013 purpose of entry_data_value_funcs.. */
1015 static struct value *
1016 entry_data_value_coerce_ref (const struct value *value)
1018 struct type *checked_type = check_typedef (value_type (value));
1019 struct value *target_val;
1021 if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
1024 target_val = value_computed_closure (value);
1025 value_incref (target_val);
1029 /* Implement copy_closure. */
1032 entry_data_value_copy_closure (const struct value *v)
1034 struct value *target_val = value_computed_closure (v);
1036 value_incref (target_val);
1040 /* Implement free_closure. */
1043 entry_data_value_free_closure (struct value *v)
1045 struct value *target_val = value_computed_closure (v);
1047 value_free (target_val);
1050 /* Vector for methods for an entry value reference where the referenced value
1051 is stored in the caller. On the first dereference use
1052 DW_AT_GNU_call_site_data_value in the caller. */
1054 static const struct lval_funcs entry_data_value_funcs =
1058 NULL, /* check_validity */
1059 NULL, /* check_any_valid */
1060 NULL, /* indirect */
1061 entry_data_value_coerce_ref,
1062 NULL, /* check_synthetic_pointer */
1063 entry_data_value_copy_closure,
1064 entry_data_value_free_closure
1067 /* Read parameter of TYPE at (callee) FRAME's function entry. DWARF_REG and
1068 FB_OFFSET are used to match DW_AT_location at the caller's
1069 DW_TAG_GNU_call_site_parameter. See DWARF_REG and FB_OFFSET description at
1070 struct dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
1072 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1073 cannot resolve the parameter for any reason. */
1075 static struct value *
1076 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1077 int dwarf_reg, CORE_ADDR fb_offset)
1079 struct type *checked_type = check_typedef (type);
1080 struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1081 struct frame_info *caller_frame = get_prev_frame (frame);
1082 struct value *outer_val, *target_val, *val;
1083 struct call_site_parameter *parameter;
1084 struct dwarf2_per_cu_data *caller_per_cu;
1087 parameter = dwarf_expr_reg_to_entry_parameter (frame, dwarf_reg, fb_offset,
1090 outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1094 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1095 used and it is not available do not fall back to OUTER_VAL - dereferencing
1096 TYPE_CODE_REF with non-entry data value would give current value - not the
1099 if (TYPE_CODE (checked_type) != TYPE_CODE_REF
1100 || TYPE_TARGET_TYPE (checked_type) == NULL)
1103 target_val = dwarf_entry_parameter_to_value (parameter,
1104 TYPE_LENGTH (target_type),
1105 target_type, caller_frame,
1108 /* value_as_address dereferences TYPE_CODE_REF. */
1109 addr = extract_typed_address (value_contents (outer_val), checked_type);
1111 /* The target entry value has artificial address of the entry value
1113 VALUE_LVAL (target_val) = lval_memory;
1114 set_value_address (target_val, addr);
1116 release_value (target_val);
1117 val = allocate_computed_value (type, &entry_data_value_funcs,
1118 target_val /* closure */);
1120 /* Copy the referencing pointer to the new computed value. */
1121 memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1122 TYPE_LENGTH (checked_type));
1123 set_value_lazy (val, 0);
1128 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1129 SIZE are DWARF block used to match DW_AT_location at the caller's
1130 DW_TAG_GNU_call_site_parameter.
1132 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1133 cannot resolve the parameter for any reason. */
1135 static struct value *
1136 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1137 const gdb_byte *block, size_t block_len)
1140 CORE_ADDR fb_offset;
1142 dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1143 if (dwarf_reg != -1)
1144 return value_of_dwarf_reg_entry (type, frame, dwarf_reg, 0 /* unused */);
1146 if (dwarf_block_to_fb_offset (block, block + block_len, &fb_offset))
1147 return value_of_dwarf_reg_entry (type, frame, -1, fb_offset);
1149 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1150 suppressed during normal operation. The expression can be arbitrary if
1151 there is no caller-callee entry value binding expected. */
1152 throw_error (NO_ENTRY_VALUE_ERROR,
1153 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1154 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1157 struct piece_closure
1159 /* Reference count. */
1162 /* The CU from which this closure's expression came. */
1163 struct dwarf2_per_cu_data *per_cu;
1165 /* The number of pieces used to describe this variable. */
1168 /* The target address size, used only for DWARF_VALUE_STACK. */
1171 /* The pieces themselves. */
1172 struct dwarf_expr_piece *pieces;
1175 /* Allocate a closure for a value formed from separately-described
1178 static struct piece_closure *
1179 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1180 int n_pieces, struct dwarf_expr_piece *pieces,
1183 struct piece_closure *c = XZALLOC (struct piece_closure);
1188 c->n_pieces = n_pieces;
1189 c->addr_size = addr_size;
1190 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
1192 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
1193 for (i = 0; i < n_pieces; ++i)
1194 if (c->pieces[i].location == DWARF_VALUE_STACK)
1195 value_incref (c->pieces[i].v.value);
1200 /* The lowest-level function to extract bits from a byte buffer.
1201 SOURCE is the buffer. It is updated if we read to the end of a
1203 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1204 updated to reflect the number of bits actually read.
1205 NBITS is the number of bits we want to read. It is updated to
1206 reflect the number of bits actually read. This function may read
1208 BITS_BIG_ENDIAN is taken directly from gdbarch.
1209 This function returns the extracted bits. */
1212 extract_bits_primitive (const gdb_byte **source,
1213 unsigned int *source_offset_bits,
1214 int *nbits, int bits_big_endian)
1216 unsigned int avail, mask, datum;
1218 gdb_assert (*source_offset_bits < 8);
1220 avail = 8 - *source_offset_bits;
1224 mask = (1 << avail) - 1;
1226 if (bits_big_endian)
1227 datum >>= 8 - (*source_offset_bits + *nbits);
1229 datum >>= *source_offset_bits;
1233 *source_offset_bits += avail;
1234 if (*source_offset_bits >= 8)
1236 *source_offset_bits -= 8;
1243 /* Extract some bits from a source buffer and move forward in the
1246 SOURCE is the source buffer. It is updated as bytes are read.
1247 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1249 NBITS is the number of bits to read.
1250 BITS_BIG_ENDIAN is taken directly from gdbarch.
1252 This function returns the bits that were read. */
1255 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
1256 int nbits, int bits_big_endian)
1260 gdb_assert (nbits > 0 && nbits <= 8);
1262 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
1268 more = extract_bits_primitive (source, source_offset_bits, &nbits,
1270 if (bits_big_endian)
1280 /* Write some bits into a buffer and move forward in the buffer.
1282 DATUM is the bits to write. The low-order bits of DATUM are used.
1283 DEST is the destination buffer. It is updated as bytes are
1285 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1287 NBITS is the number of valid bits in DATUM.
1288 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1291 insert_bits (unsigned int datum,
1292 gdb_byte *dest, unsigned int dest_offset_bits,
1293 int nbits, int bits_big_endian)
1297 gdb_assert (dest_offset_bits + nbits <= 8);
1299 mask = (1 << nbits) - 1;
1300 if (bits_big_endian)
1302 datum <<= 8 - (dest_offset_bits + nbits);
1303 mask <<= 8 - (dest_offset_bits + nbits);
1307 datum <<= dest_offset_bits;
1308 mask <<= dest_offset_bits;
1311 gdb_assert ((datum & ~mask) == 0);
1313 *dest = (*dest & ~mask) | datum;
1316 /* Copy bits from a source to a destination.
1318 DEST is where the bits should be written.
1319 DEST_OFFSET_BITS is the bit offset into DEST.
1320 SOURCE is the source of bits.
1321 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1322 BIT_COUNT is the number of bits to copy.
1323 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1326 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
1327 const gdb_byte *source, unsigned int source_offset_bits,
1328 unsigned int bit_count,
1329 int bits_big_endian)
1331 unsigned int dest_avail;
1334 /* Reduce everything to byte-size pieces. */
1335 dest += dest_offset_bits / 8;
1336 dest_offset_bits %= 8;
1337 source += source_offset_bits / 8;
1338 source_offset_bits %= 8;
1340 dest_avail = 8 - dest_offset_bits % 8;
1342 /* See if we can fill the first destination byte. */
1343 if (dest_avail < bit_count)
1345 datum = extract_bits (&source, &source_offset_bits, dest_avail,
1347 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
1349 dest_offset_bits = 0;
1350 bit_count -= dest_avail;
1353 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1354 than 8 bits remaining. */
1355 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
1356 for (; bit_count >= 8; bit_count -= 8)
1358 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
1359 *dest++ = (gdb_byte) datum;
1362 /* Finally, we may have a few leftover bits. */
1363 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
1366 datum = extract_bits (&source, &source_offset_bits, bit_count,
1368 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
1373 read_pieced_value (struct value *v)
1377 ULONGEST bits_to_skip;
1379 struct piece_closure *c
1380 = (struct piece_closure *) value_computed_closure (v);
1381 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
1383 size_t buffer_size = 0;
1384 char *buffer = NULL;
1385 struct cleanup *cleanup;
1387 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1389 if (value_type (v) != value_enclosing_type (v))
1390 internal_error (__FILE__, __LINE__,
1391 _("Should not be able to create a lazy value with "
1392 "an enclosing type"));
1394 cleanup = make_cleanup (free_current_contents, &buffer);
1396 contents = value_contents_raw (v);
1397 bits_to_skip = 8 * value_offset (v);
1398 if (value_bitsize (v))
1400 bits_to_skip += value_bitpos (v);
1401 type_len = value_bitsize (v);
1404 type_len = 8 * TYPE_LENGTH (value_type (v));
1406 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1408 struct dwarf_expr_piece *p = &c->pieces[i];
1409 size_t this_size, this_size_bits;
1410 long dest_offset_bits, source_offset_bits, source_offset;
1411 const gdb_byte *intermediate_buffer;
1413 /* Compute size, source, and destination offsets for copying, in
1415 this_size_bits = p->size;
1416 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1418 bits_to_skip -= this_size_bits;
1421 if (this_size_bits > type_len - offset)
1422 this_size_bits = type_len - offset;
1423 if (bits_to_skip > 0)
1425 dest_offset_bits = 0;
1426 source_offset_bits = bits_to_skip;
1427 this_size_bits -= bits_to_skip;
1432 dest_offset_bits = offset;
1433 source_offset_bits = 0;
1436 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1437 source_offset = source_offset_bits / 8;
1438 if (buffer_size < this_size)
1440 buffer_size = this_size;
1441 buffer = xrealloc (buffer, buffer_size);
1443 intermediate_buffer = buffer;
1445 /* Copy from the source to DEST_BUFFER. */
1446 switch (p->location)
1448 case DWARF_VALUE_REGISTER:
1450 struct gdbarch *arch = get_frame_arch (frame);
1451 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1452 int reg_offset = source_offset;
1454 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1455 && this_size < register_size (arch, gdb_regnum))
1457 /* Big-endian, and we want less than full size. */
1458 reg_offset = register_size (arch, gdb_regnum) - this_size;
1459 /* We want the lower-order THIS_SIZE_BITS of the bytes
1460 we extract from the register. */
1461 source_offset_bits += 8 * this_size - this_size_bits;
1464 if (gdb_regnum != -1)
1468 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1472 /* Just so garbage doesn't ever shine through. */
1473 memset (buffer, 0, this_size);
1476 set_value_optimized_out (v, 1);
1478 mark_value_bytes_unavailable (v, offset, this_size);
1483 error (_("Unable to access DWARF register number %s"),
1484 paddress (arch, p->v.regno));
1489 case DWARF_VALUE_MEMORY:
1490 read_value_memory (v, offset,
1491 p->v.mem.in_stack_memory,
1492 p->v.mem.addr + source_offset,
1496 case DWARF_VALUE_STACK:
1498 size_t n = this_size;
1500 if (n > c->addr_size - source_offset)
1501 n = (c->addr_size >= source_offset
1502 ? c->addr_size - source_offset
1510 const gdb_byte *val_bytes = value_contents_all (p->v.value);
1512 intermediate_buffer = val_bytes + source_offset;
1517 case DWARF_VALUE_LITERAL:
1519 size_t n = this_size;
1521 if (n > p->v.literal.length - source_offset)
1522 n = (p->v.literal.length >= source_offset
1523 ? p->v.literal.length - source_offset
1526 intermediate_buffer = p->v.literal.data + source_offset;
1530 /* These bits show up as zeros -- but do not cause the value
1531 to be considered optimized-out. */
1532 case DWARF_VALUE_IMPLICIT_POINTER:
1535 case DWARF_VALUE_OPTIMIZED_OUT:
1536 set_value_optimized_out (v, 1);
1540 internal_error (__FILE__, __LINE__, _("invalid location type"));
1543 if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1544 && p->location != DWARF_VALUE_IMPLICIT_POINTER)
1545 copy_bitwise (contents, dest_offset_bits,
1546 intermediate_buffer, source_offset_bits % 8,
1547 this_size_bits, bits_big_endian);
1549 offset += this_size_bits;
1552 do_cleanups (cleanup);
1556 write_pieced_value (struct value *to, struct value *from)
1560 ULONGEST bits_to_skip;
1561 const gdb_byte *contents;
1562 struct piece_closure *c
1563 = (struct piece_closure *) value_computed_closure (to);
1564 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
1566 size_t buffer_size = 0;
1567 char *buffer = NULL;
1568 struct cleanup *cleanup;
1570 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
1574 set_value_optimized_out (to, 1);
1578 cleanup = make_cleanup (free_current_contents, &buffer);
1580 contents = value_contents (from);
1581 bits_to_skip = 8 * value_offset (to);
1582 if (value_bitsize (to))
1584 bits_to_skip += value_bitpos (to);
1585 type_len = value_bitsize (to);
1588 type_len = 8 * TYPE_LENGTH (value_type (to));
1590 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1592 struct dwarf_expr_piece *p = &c->pieces[i];
1593 size_t this_size_bits, this_size;
1594 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1596 const gdb_byte *source_buffer;
1598 this_size_bits = p->size;
1599 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1601 bits_to_skip -= this_size_bits;
1604 if (this_size_bits > type_len - offset)
1605 this_size_bits = type_len - offset;
1606 if (bits_to_skip > 0)
1608 dest_offset_bits = bits_to_skip;
1609 source_offset_bits = 0;
1610 this_size_bits -= bits_to_skip;
1615 dest_offset_bits = 0;
1616 source_offset_bits = offset;
1619 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1620 source_offset = source_offset_bits / 8;
1621 dest_offset = dest_offset_bits / 8;
1622 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1624 source_buffer = contents + source_offset;
1629 if (buffer_size < this_size)
1631 buffer_size = this_size;
1632 buffer = xrealloc (buffer, buffer_size);
1634 source_buffer = buffer;
1638 switch (p->location)
1640 case DWARF_VALUE_REGISTER:
1642 struct gdbarch *arch = get_frame_arch (frame);
1643 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1644 int reg_offset = dest_offset;
1646 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1647 && this_size <= register_size (arch, gdb_regnum))
1648 /* Big-endian, and we want less than full size. */
1649 reg_offset = register_size (arch, gdb_regnum) - this_size;
1651 if (gdb_regnum != -1)
1657 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1662 error (_("Can't do read-modify-write to "
1663 "update bitfield; containing word has been "
1666 throw_error (NOT_AVAILABLE_ERROR,
1667 _("Can't do read-modify-write to update "
1668 "bitfield; containing word "
1671 copy_bitwise (buffer, dest_offset_bits,
1672 contents, source_offset_bits,
1677 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
1678 this_size, source_buffer);
1682 error (_("Unable to write to DWARF register number %s"),
1683 paddress (arch, p->v.regno));
1687 case DWARF_VALUE_MEMORY:
1690 /* Only the first and last bytes can possibly have any
1692 read_memory (p->v.mem.addr + dest_offset, buffer, 1);
1693 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
1694 buffer + this_size - 1, 1);
1695 copy_bitwise (buffer, dest_offset_bits,
1696 contents, source_offset_bits,
1701 write_memory (p->v.mem.addr + dest_offset,
1702 source_buffer, this_size);
1705 set_value_optimized_out (to, 1);
1708 offset += this_size_bits;
1711 do_cleanups (cleanup);
1714 /* A helper function that checks bit validity in a pieced value.
1715 CHECK_FOR indicates the kind of validity checking.
1716 DWARF_VALUE_MEMORY means to check whether any bit is valid.
1717 DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
1719 DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
1720 implicit pointer. */
1723 check_pieced_value_bits (const struct value *value, int bit_offset,
1725 enum dwarf_value_location check_for)
1727 struct piece_closure *c
1728 = (struct piece_closure *) value_computed_closure (value);
1730 int validity = (check_for == DWARF_VALUE_MEMORY
1731 || check_for == DWARF_VALUE_IMPLICIT_POINTER);
1733 bit_offset += 8 * value_offset (value);
1734 if (value_bitsize (value))
1735 bit_offset += value_bitpos (value);
1737 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1739 struct dwarf_expr_piece *p = &c->pieces[i];
1740 size_t this_size_bits = p->size;
1744 if (bit_offset >= this_size_bits)
1746 bit_offset -= this_size_bits;
1750 bit_length -= this_size_bits - bit_offset;
1754 bit_length -= this_size_bits;
1756 if (check_for == DWARF_VALUE_IMPLICIT_POINTER)
1758 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1761 else if (p->location == DWARF_VALUE_OPTIMIZED_OUT
1762 || p->location == DWARF_VALUE_IMPLICIT_POINTER)
1778 check_pieced_value_validity (const struct value *value, int bit_offset,
1781 return check_pieced_value_bits (value, bit_offset, bit_length,
1782 DWARF_VALUE_MEMORY);
1786 check_pieced_value_invalid (const struct value *value)
1788 return check_pieced_value_bits (value, 0,
1789 8 * TYPE_LENGTH (value_type (value)),
1790 DWARF_VALUE_OPTIMIZED_OUT);
1793 /* An implementation of an lval_funcs method to see whether a value is
1794 a synthetic pointer. */
1797 check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
1800 return check_pieced_value_bits (value, bit_offset, bit_length,
1801 DWARF_VALUE_IMPLICIT_POINTER);
1804 /* A wrapper function for get_frame_address_in_block. */
1807 get_frame_address_in_block_wrapper (void *baton)
1809 return get_frame_address_in_block (baton);
1812 /* An implementation of an lval_funcs method to indirect through a
1813 pointer. This handles the synthetic pointer case when needed. */
1815 static struct value *
1816 indirect_pieced_value (struct value *value)
1818 struct piece_closure *c
1819 = (struct piece_closure *) value_computed_closure (value);
1821 struct frame_info *frame;
1822 struct dwarf2_locexpr_baton baton;
1823 int i, bit_offset, bit_length;
1824 struct dwarf_expr_piece *piece = NULL;
1825 LONGEST byte_offset;
1827 type = check_typedef (value_type (value));
1828 if (TYPE_CODE (type) != TYPE_CODE_PTR)
1831 bit_length = 8 * TYPE_LENGTH (type);
1832 bit_offset = 8 * value_offset (value);
1833 if (value_bitsize (value))
1834 bit_offset += value_bitpos (value);
1836 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1838 struct dwarf_expr_piece *p = &c->pieces[i];
1839 size_t this_size_bits = p->size;
1843 if (bit_offset >= this_size_bits)
1845 bit_offset -= this_size_bits;
1849 bit_length -= this_size_bits - bit_offset;
1853 bit_length -= this_size_bits;
1855 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1858 if (bit_length != 0)
1859 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
1865 frame = get_selected_frame (_("No frame selected."));
1867 /* This is an offset requested by GDB, such as value subcripts. */
1868 byte_offset = value_as_address (value);
1871 baton = dwarf2_fetch_die_location_block (piece->v.ptr.die, c->per_cu,
1872 get_frame_address_in_block_wrapper,
1875 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
1876 baton.data, baton.size, baton.per_cu,
1877 piece->v.ptr.offset + byte_offset);
1881 copy_pieced_value_closure (const struct value *v)
1883 struct piece_closure *c
1884 = (struct piece_closure *) value_computed_closure (v);
1891 free_pieced_value_closure (struct value *v)
1893 struct piece_closure *c
1894 = (struct piece_closure *) value_computed_closure (v);
1901 for (i = 0; i < c->n_pieces; ++i)
1902 if (c->pieces[i].location == DWARF_VALUE_STACK)
1903 value_free (c->pieces[i].v.value);
1910 /* Functions for accessing a variable described by DW_OP_piece. */
1911 static const struct lval_funcs pieced_value_funcs = {
1914 check_pieced_value_validity,
1915 check_pieced_value_invalid,
1916 indirect_pieced_value,
1917 NULL, /* coerce_ref */
1918 check_pieced_synthetic_pointer,
1919 copy_pieced_value_closure,
1920 free_pieced_value_closure
1923 /* Helper function which throws an error if a synthetic pointer is
1927 invalid_synthetic_pointer (void)
1929 error (_("access outside bounds of object "
1930 "referenced via synthetic pointer"));
1933 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
1935 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
1937 dwarf_expr_read_reg,
1938 dwarf_expr_read_mem,
1939 dwarf_expr_frame_base,
1940 dwarf_expr_frame_cfa,
1941 dwarf_expr_frame_pc,
1942 dwarf_expr_tls_address,
1943 dwarf_expr_dwarf_call,
1944 dwarf_expr_get_base_type,
1945 dwarf_expr_push_dwarf_reg_entry_value
1948 /* Evaluate a location description, starting at DATA and with length
1949 SIZE, to find the current location of variable of TYPE in the
1950 context of FRAME. BYTE_OFFSET is applied after the contents are
1953 static struct value *
1954 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
1955 const gdb_byte *data, unsigned short size,
1956 struct dwarf2_per_cu_data *per_cu,
1957 LONGEST byte_offset)
1959 struct value *retval;
1960 struct dwarf_expr_baton baton;
1961 struct dwarf_expr_context *ctx;
1962 struct cleanup *old_chain, *value_chain;
1963 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1964 volatile struct gdb_exception ex;
1966 if (byte_offset < 0)
1967 invalid_synthetic_pointer ();
1970 return allocate_optimized_out_value (type);
1972 baton.frame = frame;
1973 baton.per_cu = per_cu;
1975 ctx = new_dwarf_expr_context ();
1976 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1977 value_chain = make_cleanup_value_free_to_mark (value_mark ());
1979 ctx->gdbarch = get_objfile_arch (objfile);
1980 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1981 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
1982 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1983 ctx->baton = &baton;
1984 ctx->funcs = &dwarf_expr_ctx_funcs;
1986 TRY_CATCH (ex, RETURN_MASK_ERROR)
1988 dwarf_expr_eval (ctx, data, size);
1992 if (ex.error == NOT_AVAILABLE_ERROR)
1994 do_cleanups (old_chain);
1995 retval = allocate_value (type);
1996 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
1999 else if (ex.error == NO_ENTRY_VALUE_ERROR)
2001 if (entry_values_debug)
2002 exception_print (gdb_stdout, ex);
2003 do_cleanups (old_chain);
2004 return allocate_optimized_out_value (type);
2007 throw_exception (ex);
2010 if (ctx->num_pieces > 0)
2012 struct piece_closure *c;
2013 struct frame_id frame_id = get_frame_id (frame);
2014 ULONGEST bit_size = 0;
2017 for (i = 0; i < ctx->num_pieces; ++i)
2018 bit_size += ctx->pieces[i].size;
2019 if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
2020 invalid_synthetic_pointer ();
2022 c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
2024 /* We must clean up the value chain after creating the piece
2025 closure but before allocating the result. */
2026 do_cleanups (value_chain);
2027 retval = allocate_computed_value (type, &pieced_value_funcs, c);
2028 VALUE_FRAME_ID (retval) = frame_id;
2029 set_value_offset (retval, byte_offset);
2033 switch (ctx->location)
2035 case DWARF_VALUE_REGISTER:
2037 struct gdbarch *arch = get_frame_arch (frame);
2038 ULONGEST dwarf_regnum = value_as_long (dwarf_expr_fetch (ctx, 0));
2039 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
2041 if (byte_offset != 0)
2042 error (_("cannot use offset on synthetic pointer to register"));
2043 do_cleanups (value_chain);
2044 if (gdb_regnum != -1)
2045 retval = value_from_register (type, gdb_regnum, frame);
2047 error (_("Unable to access DWARF register number %s"),
2048 paddress (arch, dwarf_regnum));
2052 case DWARF_VALUE_MEMORY:
2054 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
2055 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
2057 do_cleanups (value_chain);
2058 retval = allocate_value_lazy (type);
2059 VALUE_LVAL (retval) = lval_memory;
2060 if (in_stack_memory)
2061 set_value_stack (retval, 1);
2062 set_value_address (retval, address + byte_offset);
2066 case DWARF_VALUE_STACK:
2068 struct value *value = dwarf_expr_fetch (ctx, 0);
2070 const gdb_byte *val_bytes;
2071 size_t n = TYPE_LENGTH (value_type (value));
2073 if (byte_offset + TYPE_LENGTH (type) > n)
2074 invalid_synthetic_pointer ();
2076 val_bytes = value_contents_all (value);
2077 val_bytes += byte_offset;
2080 /* Preserve VALUE because we are going to free values back
2081 to the mark, but we still need the value contents
2083 value_incref (value);
2084 do_cleanups (value_chain);
2085 make_cleanup_value_free (value);
2087 retval = allocate_value (type);
2088 contents = value_contents_raw (retval);
2089 if (n > TYPE_LENGTH (type))
2091 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2093 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2094 val_bytes += n - TYPE_LENGTH (type);
2095 n = TYPE_LENGTH (type);
2097 memcpy (contents, val_bytes, n);
2101 case DWARF_VALUE_LITERAL:
2104 const bfd_byte *ldata;
2105 size_t n = ctx->len;
2107 if (byte_offset + TYPE_LENGTH (type) > n)
2108 invalid_synthetic_pointer ();
2110 do_cleanups (value_chain);
2111 retval = allocate_value (type);
2112 contents = value_contents_raw (retval);
2114 ldata = ctx->data + byte_offset;
2117 if (n > TYPE_LENGTH (type))
2119 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2121 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2122 ldata += n - TYPE_LENGTH (type);
2123 n = TYPE_LENGTH (type);
2125 memcpy (contents, ldata, n);
2129 case DWARF_VALUE_OPTIMIZED_OUT:
2130 do_cleanups (value_chain);
2131 retval = allocate_optimized_out_value (type);
2134 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2135 operation by execute_stack_op. */
2136 case DWARF_VALUE_IMPLICIT_POINTER:
2137 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2138 it can only be encountered when making a piece. */
2140 internal_error (__FILE__, __LINE__, _("invalid location type"));
2144 set_value_initialized (retval, ctx->initialized);
2146 do_cleanups (old_chain);
2151 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2152 passes 0 as the byte_offset. */
2155 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2156 const gdb_byte *data, unsigned short size,
2157 struct dwarf2_per_cu_data *per_cu)
2159 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2163 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
2165 struct needs_frame_baton
2168 struct dwarf2_per_cu_data *per_cu;
2171 /* Reads from registers do require a frame. */
2173 needs_frame_read_reg (void *baton, int regnum)
2175 struct needs_frame_baton *nf_baton = baton;
2177 nf_baton->needs_frame = 1;
2181 /* Reads from memory do not require a frame. */
2183 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
2185 memset (buf, 0, len);
2188 /* Frame-relative accesses do require a frame. */
2190 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
2192 static gdb_byte lit0 = DW_OP_lit0;
2193 struct needs_frame_baton *nf_baton = baton;
2198 nf_baton->needs_frame = 1;
2201 /* CFA accesses require a frame. */
2204 needs_frame_frame_cfa (void *baton)
2206 struct needs_frame_baton *nf_baton = baton;
2208 nf_baton->needs_frame = 1;
2212 /* Thread-local accesses do require a frame. */
2214 needs_frame_tls_address (void *baton, CORE_ADDR offset)
2216 struct needs_frame_baton *nf_baton = baton;
2218 nf_baton->needs_frame = 1;
2222 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
2225 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
2227 struct needs_frame_baton *nf_baton = ctx->baton;
2229 per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
2230 ctx->funcs->get_frame_pc, ctx->baton);
2233 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
2236 needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
2237 int dwarf_reg, CORE_ADDR fb_offset, int deref_size)
2239 struct needs_frame_baton *nf_baton = ctx->baton;
2241 nf_baton->needs_frame = 1;
2244 /* Virtual method table for dwarf2_loc_desc_needs_frame below. */
2246 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
2248 needs_frame_read_reg,
2249 needs_frame_read_mem,
2250 needs_frame_frame_base,
2251 needs_frame_frame_cfa,
2252 needs_frame_frame_cfa, /* get_frame_pc */
2253 needs_frame_tls_address,
2254 needs_frame_dwarf_call,
2255 NULL, /* get_base_type */
2256 needs_dwarf_reg_entry_value
2259 /* Return non-zero iff the location expression at DATA (length SIZE)
2260 requires a frame to evaluate. */
2263 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
2264 struct dwarf2_per_cu_data *per_cu)
2266 struct needs_frame_baton baton;
2267 struct dwarf_expr_context *ctx;
2269 struct cleanup *old_chain;
2270 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2272 baton.needs_frame = 0;
2273 baton.per_cu = per_cu;
2275 ctx = new_dwarf_expr_context ();
2276 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2277 make_cleanup_value_free_to_mark (value_mark ());
2279 ctx->gdbarch = get_objfile_arch (objfile);
2280 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2281 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2282 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2283 ctx->baton = &baton;
2284 ctx->funcs = &needs_frame_ctx_funcs;
2286 dwarf_expr_eval (ctx, data, size);
2288 in_reg = ctx->location == DWARF_VALUE_REGISTER;
2290 if (ctx->num_pieces > 0)
2294 /* If the location has several pieces, and any of them are in
2295 registers, then we will need a frame to fetch them from. */
2296 for (i = 0; i < ctx->num_pieces; i++)
2297 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
2301 do_cleanups (old_chain);
2303 return baton.needs_frame || in_reg;
2306 /* A helper function that throws an unimplemented error mentioning a
2307 given DWARF operator. */
2310 unimplemented (unsigned int op)
2312 const char *name = dwarf_stack_op_name (op);
2315 error (_("DWARF operator %s cannot be translated to an agent expression"),
2318 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2319 "to an agent expression"),
2323 /* A helper function to convert a DWARF register to an arch register.
2324 ARCH is the architecture.
2325 DWARF_REG is the register.
2326 This will throw an exception if the DWARF register cannot be
2327 translated to an architecture register. */
2330 translate_register (struct gdbarch *arch, int dwarf_reg)
2332 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2334 error (_("Unable to access DWARF register number %d"), dwarf_reg);
2338 /* A helper function that emits an access to memory. ARCH is the
2339 target architecture. EXPR is the expression which we are building.
2340 NBITS is the number of bits we want to read. This emits the
2341 opcodes needed to read the memory and then extract the desired
2345 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2347 ULONGEST nbytes = (nbits + 7) / 8;
2349 gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
2352 ax_trace_quick (expr, nbytes);
2355 ax_simple (expr, aop_ref8);
2356 else if (nbits <= 16)
2357 ax_simple (expr, aop_ref16);
2358 else if (nbits <= 32)
2359 ax_simple (expr, aop_ref32);
2361 ax_simple (expr, aop_ref64);
2363 /* If we read exactly the number of bytes we wanted, we're done. */
2364 if (8 * nbytes == nbits)
2367 if (gdbarch_bits_big_endian (arch))
2369 /* On a bits-big-endian machine, we want the high-order
2371 ax_const_l (expr, 8 * nbytes - nbits);
2372 ax_simple (expr, aop_rsh_unsigned);
2376 /* On a bits-little-endian box, we want the low-order NBITS. */
2377 ax_zero_ext (expr, nbits);
2381 /* A helper function to return the frame's PC. */
2384 get_ax_pc (void *baton)
2386 struct agent_expr *expr = baton;
2391 /* Compile a DWARF location expression to an agent expression.
2393 EXPR is the agent expression we are building.
2394 LOC is the agent value we modify.
2395 ARCH is the architecture.
2396 ADDR_SIZE is the size of addresses, in bytes.
2397 OP_PTR is the start of the location expression.
2398 OP_END is one past the last byte of the location expression.
2400 This will throw an exception for various kinds of errors -- for
2401 example, if the expression cannot be compiled, or if the expression
2405 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
2406 struct gdbarch *arch, unsigned int addr_size,
2407 const gdb_byte *op_ptr, const gdb_byte *op_end,
2408 struct dwarf2_per_cu_data *per_cu)
2410 struct cleanup *cleanups;
2412 VEC(int) *dw_labels = NULL, *patches = NULL;
2413 const gdb_byte * const base = op_ptr;
2414 const gdb_byte *previous_piece = op_ptr;
2415 enum bfd_endian byte_order = gdbarch_byte_order (arch);
2416 ULONGEST bits_collected = 0;
2417 unsigned int addr_size_bits = 8 * addr_size;
2418 int bits_big_endian = gdbarch_bits_big_endian (arch);
2420 offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
2421 cleanups = make_cleanup (xfree, offsets);
2423 for (i = 0; i < op_end - op_ptr; ++i)
2426 make_cleanup (VEC_cleanup (int), &dw_labels);
2427 make_cleanup (VEC_cleanup (int), &patches);
2429 /* By default we are making an address. */
2430 loc->kind = axs_lvalue_memory;
2432 while (op_ptr < op_end)
2434 enum dwarf_location_atom op = *op_ptr;
2435 ULONGEST uoffset, reg;
2439 offsets[op_ptr - base] = expr->len;
2442 /* Our basic approach to code generation is to map DWARF
2443 operations directly to AX operations. However, there are
2446 First, DWARF works on address-sized units, but AX always uses
2447 LONGEST. For most operations we simply ignore this
2448 difference; instead we generate sign extensions as needed
2449 before division and comparison operations. It would be nice
2450 to omit the sign extensions, but there is no way to determine
2451 the size of the target's LONGEST. (This code uses the size
2452 of the host LONGEST in some cases -- that is a bug but it is
2455 Second, some DWARF operations cannot be translated to AX.
2456 For these we simply fail. See
2457 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
2492 ax_const_l (expr, op - DW_OP_lit0);
2496 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
2497 op_ptr += addr_size;
2498 /* Some versions of GCC emit DW_OP_addr before
2499 DW_OP_GNU_push_tls_address. In this case the value is an
2500 index, not an address. We don't support things like
2501 branching between the address and the TLS op. */
2502 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
2503 uoffset += dwarf2_per_cu_text_offset (per_cu);
2504 ax_const_l (expr, uoffset);
2508 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
2512 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
2516 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
2520 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
2524 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
2528 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
2532 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
2536 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
2540 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
2541 ax_const_l (expr, uoffset);
2544 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2545 ax_const_l (expr, offset);
2580 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2581 loc->u.reg = translate_register (arch, op - DW_OP_reg0);
2582 loc->kind = axs_lvalue_register;
2586 op_ptr = read_uleb128 (op_ptr, op_end, ®);
2587 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2588 loc->u.reg = translate_register (arch, reg);
2589 loc->kind = axs_lvalue_register;
2592 case DW_OP_implicit_value:
2596 op_ptr = read_uleb128 (op_ptr, op_end, &len);
2597 if (op_ptr + len > op_end)
2598 error (_("DW_OP_implicit_value: too few bytes available."));
2599 if (len > sizeof (ULONGEST))
2600 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
2603 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
2606 dwarf_expr_require_composition (op_ptr, op_end,
2607 "DW_OP_implicit_value");
2609 loc->kind = axs_rvalue;
2613 case DW_OP_stack_value:
2614 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
2615 loc->kind = axs_rvalue;
2650 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2651 i = translate_register (arch, op - DW_OP_breg0);
2655 ax_const_l (expr, offset);
2656 ax_simple (expr, aop_add);
2661 op_ptr = read_uleb128 (op_ptr, op_end, ®);
2662 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2663 i = translate_register (arch, reg);
2667 ax_const_l (expr, offset);
2668 ax_simple (expr, aop_add);
2674 const gdb_byte *datastart;
2676 unsigned int before_stack_len;
2678 struct symbol *framefunc;
2679 LONGEST base_offset = 0;
2681 b = block_for_pc (expr->scope);
2684 error (_("No block found for address"));
2686 framefunc = block_linkage_function (b);
2689 error (_("No function found for block"));
2691 dwarf_expr_frame_base_1 (framefunc, expr->scope,
2692 &datastart, &datalen);
2694 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2695 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
2696 datastart + datalen, per_cu);
2700 ax_const_l (expr, offset);
2701 ax_simple (expr, aop_add);
2704 loc->kind = axs_lvalue_memory;
2709 ax_simple (expr, aop_dup);
2713 ax_simple (expr, aop_pop);
2718 ax_pick (expr, offset);
2722 ax_simple (expr, aop_swap);
2730 ax_simple (expr, aop_rot);
2734 case DW_OP_deref_size:
2738 if (op == DW_OP_deref_size)
2746 ax_simple (expr, aop_ref8);
2749 ax_simple (expr, aop_ref16);
2752 ax_simple (expr, aop_ref32);
2755 ax_simple (expr, aop_ref64);
2758 /* Note that dwarf_stack_op_name will never return
2760 error (_("Unsupported size %d in %s"),
2761 size, dwarf_stack_op_name (op));
2767 /* Sign extend the operand. */
2768 ax_ext (expr, addr_size_bits);
2769 ax_simple (expr, aop_dup);
2770 ax_const_l (expr, 0);
2771 ax_simple (expr, aop_less_signed);
2772 ax_simple (expr, aop_log_not);
2773 i = ax_goto (expr, aop_if_goto);
2774 /* We have to emit 0 - X. */
2775 ax_const_l (expr, 0);
2776 ax_simple (expr, aop_swap);
2777 ax_simple (expr, aop_sub);
2778 ax_label (expr, i, expr->len);
2782 /* No need to sign extend here. */
2783 ax_const_l (expr, 0);
2784 ax_simple (expr, aop_swap);
2785 ax_simple (expr, aop_sub);
2789 /* Sign extend the operand. */
2790 ax_ext (expr, addr_size_bits);
2791 ax_simple (expr, aop_bit_not);
2794 case DW_OP_plus_uconst:
2795 op_ptr = read_uleb128 (op_ptr, op_end, ®);
2796 /* It would be really weird to emit `DW_OP_plus_uconst 0',
2797 but we micro-optimize anyhow. */
2800 ax_const_l (expr, reg);
2801 ax_simple (expr, aop_add);
2806 ax_simple (expr, aop_bit_and);
2810 /* Sign extend the operands. */
2811 ax_ext (expr, addr_size_bits);
2812 ax_simple (expr, aop_swap);
2813 ax_ext (expr, addr_size_bits);
2814 ax_simple (expr, aop_swap);
2815 ax_simple (expr, aop_div_signed);
2819 ax_simple (expr, aop_sub);
2823 ax_simple (expr, aop_rem_unsigned);
2827 ax_simple (expr, aop_mul);
2831 ax_simple (expr, aop_bit_or);
2835 ax_simple (expr, aop_add);
2839 ax_simple (expr, aop_lsh);
2843 ax_simple (expr, aop_rsh_unsigned);
2847 ax_simple (expr, aop_rsh_signed);
2851 ax_simple (expr, aop_bit_xor);
2855 /* Sign extend the operands. */
2856 ax_ext (expr, addr_size_bits);
2857 ax_simple (expr, aop_swap);
2858 ax_ext (expr, addr_size_bits);
2859 /* Note no swap here: A <= B is !(B < A). */
2860 ax_simple (expr, aop_less_signed);
2861 ax_simple (expr, aop_log_not);
2865 /* Sign extend the operands. */
2866 ax_ext (expr, addr_size_bits);
2867 ax_simple (expr, aop_swap);
2868 ax_ext (expr, addr_size_bits);
2869 ax_simple (expr, aop_swap);
2870 /* A >= B is !(A < B). */
2871 ax_simple (expr, aop_less_signed);
2872 ax_simple (expr, aop_log_not);
2876 /* Sign extend the operands. */
2877 ax_ext (expr, addr_size_bits);
2878 ax_simple (expr, aop_swap);
2879 ax_ext (expr, addr_size_bits);
2880 /* No need for a second swap here. */
2881 ax_simple (expr, aop_equal);
2885 /* Sign extend the operands. */
2886 ax_ext (expr, addr_size_bits);
2887 ax_simple (expr, aop_swap);
2888 ax_ext (expr, addr_size_bits);
2889 ax_simple (expr, aop_swap);
2890 ax_simple (expr, aop_less_signed);
2894 /* Sign extend the operands. */
2895 ax_ext (expr, addr_size_bits);
2896 ax_simple (expr, aop_swap);
2897 ax_ext (expr, addr_size_bits);
2898 /* Note no swap here: A > B is B < A. */
2899 ax_simple (expr, aop_less_signed);
2903 /* Sign extend the operands. */
2904 ax_ext (expr, addr_size_bits);
2905 ax_simple (expr, aop_swap);
2906 ax_ext (expr, addr_size_bits);
2907 /* No need for a swap here. */
2908 ax_simple (expr, aop_equal);
2909 ax_simple (expr, aop_log_not);
2912 case DW_OP_call_frame_cfa:
2913 dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
2914 loc->kind = axs_lvalue_memory;
2917 case DW_OP_GNU_push_tls_address:
2922 offset = extract_signed_integer (op_ptr, 2, byte_order);
2924 i = ax_goto (expr, aop_goto);
2925 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2926 VEC_safe_push (int, patches, i);
2930 offset = extract_signed_integer (op_ptr, 2, byte_order);
2932 /* Zero extend the operand. */
2933 ax_zero_ext (expr, addr_size_bits);
2934 i = ax_goto (expr, aop_if_goto);
2935 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2936 VEC_safe_push (int, patches, i);
2943 case DW_OP_bit_piece:
2945 ULONGEST size, offset;
2947 if (op_ptr - 1 == previous_piece)
2948 error (_("Cannot translate empty pieces to agent expressions"));
2949 previous_piece = op_ptr - 1;
2951 op_ptr = read_uleb128 (op_ptr, op_end, &size);
2952 if (op == DW_OP_piece)
2958 op_ptr = read_uleb128 (op_ptr, op_end, &offset);
2960 if (bits_collected + size > 8 * sizeof (LONGEST))
2961 error (_("Expression pieces exceed word size"));
2963 /* Access the bits. */
2966 case axs_lvalue_register:
2967 ax_reg (expr, loc->u.reg);
2970 case axs_lvalue_memory:
2971 /* Offset the pointer, if needed. */
2974 ax_const_l (expr, offset / 8);
2975 ax_simple (expr, aop_add);
2978 access_memory (arch, expr, size);
2982 /* For a bits-big-endian target, shift up what we already
2983 have. For a bits-little-endian target, shift up the
2984 new data. Note that there is a potential bug here if
2985 the DWARF expression leaves multiple values on the
2987 if (bits_collected > 0)
2989 if (bits_big_endian)
2991 ax_simple (expr, aop_swap);
2992 ax_const_l (expr, size);
2993 ax_simple (expr, aop_lsh);
2994 /* We don't need a second swap here, because
2995 aop_bit_or is symmetric. */
2999 ax_const_l (expr, size);
3000 ax_simple (expr, aop_lsh);
3002 ax_simple (expr, aop_bit_or);
3005 bits_collected += size;
3006 loc->kind = axs_rvalue;
3010 case DW_OP_GNU_uninit:
3016 struct dwarf2_locexpr_baton block;
3017 int size = (op == DW_OP_call2 ? 2 : 4);
3020 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3023 offset.cu_off = uoffset;
3024 block = dwarf2_fetch_die_location_block (offset, per_cu,
3027 /* DW_OP_call_ref is currently not supported. */
3028 gdb_assert (block.per_cu == per_cu);
3030 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3031 block.data, block.data + block.size,
3036 case DW_OP_call_ref:
3044 /* Patch all the branches we emitted. */
3045 for (i = 0; i < VEC_length (int, patches); ++i)
3047 int targ = offsets[VEC_index (int, dw_labels, i)];
3049 internal_error (__FILE__, __LINE__, _("invalid label"));
3050 ax_label (expr, VEC_index (int, patches, i), targ);
3053 do_cleanups (cleanups);
3057 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3058 evaluator to calculate the location. */
3059 static struct value *
3060 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3062 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3065 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3066 dlbaton->size, dlbaton->per_cu);
3071 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3072 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3075 static struct value *
3076 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3078 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3080 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3084 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3086 locexpr_read_needs_frame (struct symbol *symbol)
3088 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3090 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
3094 /* Return true if DATA points to the end of a piece. END is one past
3095 the last byte in the expression. */
3098 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3100 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3103 /* Helper for locexpr_describe_location_piece that finds the name of a
3107 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3111 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
3112 return gdbarch_register_name (gdbarch, regnum);
3115 /* Nicely describe a single piece of a location, returning an updated
3116 position in the bytecode sequence. This function cannot recognize
3117 all locations; if a location is not recognized, it simply returns
3120 static const gdb_byte *
3121 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3122 CORE_ADDR addr, struct objfile *objfile,
3123 const gdb_byte *data, const gdb_byte *end,
3124 unsigned int addr_size)
3126 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3128 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3130 fprintf_filtered (stream, _("a variable in $%s"),
3131 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3134 else if (data[0] == DW_OP_regx)
3138 data = read_uleb128 (data + 1, end, ®);
3139 fprintf_filtered (stream, _("a variable in $%s"),
3140 locexpr_regname (gdbarch, reg));
3142 else if (data[0] == DW_OP_fbreg)
3145 struct symbol *framefunc;
3147 LONGEST frame_offset;
3148 const gdb_byte *base_data, *new_data, *save_data = data;
3150 LONGEST base_offset = 0;
3152 new_data = read_sleb128 (data + 1, end, &frame_offset);
3153 if (!piece_end_p (new_data, end))
3157 b = block_for_pc (addr);
3160 error (_("No block found for address for symbol \"%s\"."),
3161 SYMBOL_PRINT_NAME (symbol));
3163 framefunc = block_linkage_function (b);
3166 error (_("No function found for block for symbol \"%s\"."),
3167 SYMBOL_PRINT_NAME (symbol));
3169 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
3171 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3173 const gdb_byte *buf_end;
3175 frame_reg = base_data[0] - DW_OP_breg0;
3176 buf_end = read_sleb128 (base_data + 1,
3177 base_data + base_size, &base_offset);
3178 if (buf_end != base_data + base_size)
3179 error (_("Unexpected opcode after "
3180 "DW_OP_breg%u for symbol \"%s\"."),
3181 frame_reg, SYMBOL_PRINT_NAME (symbol));
3183 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3185 /* The frame base is just the register, with no offset. */
3186 frame_reg = base_data[0] - DW_OP_reg0;
3191 /* We don't know what to do with the frame base expression,
3192 so we can't trace this variable; give up. */
3196 fprintf_filtered (stream,
3197 _("a variable at frame base reg $%s offset %s+%s"),
3198 locexpr_regname (gdbarch, frame_reg),
3199 plongest (base_offset), plongest (frame_offset));
3201 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3202 && piece_end_p (data, end))
3206 data = read_sleb128 (data + 1, end, &offset);
3208 fprintf_filtered (stream,
3209 _("a variable at offset %s from base reg $%s"),
3211 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3214 /* The location expression for a TLS variable looks like this (on a
3217 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3218 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3220 0x3 is the encoding for DW_OP_addr, which has an operand as long
3221 as the size of an address on the target machine (here is 8
3222 bytes). Note that more recent version of GCC emit DW_OP_const4u
3223 or DW_OP_const8u, depending on address size, rather than
3224 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3225 The operand represents the offset at which the variable is within
3226 the thread local storage. */
3228 else if (data + 1 + addr_size < end
3229 && (data[0] == DW_OP_addr
3230 || (addr_size == 4 && data[0] == DW_OP_const4u)
3231 || (addr_size == 8 && data[0] == DW_OP_const8u))
3232 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
3233 && piece_end_p (data + 2 + addr_size, end))
3236 offset = extract_unsigned_integer (data + 1, addr_size,
3237 gdbarch_byte_order (gdbarch));
3239 fprintf_filtered (stream,
3240 _("a thread-local variable at offset 0x%s "
3241 "in the thread-local storage for `%s'"),
3242 phex_nz (offset, addr_size), objfile->name);
3244 data += 1 + addr_size + 1;
3246 else if (data[0] >= DW_OP_lit0
3247 && data[0] <= DW_OP_lit31
3249 && data[1] == DW_OP_stack_value)
3251 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3258 /* Disassemble an expression, stopping at the end of a piece or at the
3259 end of the expression. Returns a pointer to the next unread byte
3260 in the input expression. If ALL is nonzero, then this function
3261 will keep going until it reaches the end of the expression. */
3263 static const gdb_byte *
3264 disassemble_dwarf_expression (struct ui_file *stream,
3265 struct gdbarch *arch, unsigned int addr_size,
3266 int offset_size, const gdb_byte *start,
3267 const gdb_byte *data, const gdb_byte *end,
3268 int indent, int all,
3269 struct dwarf2_per_cu_data *per_cu)
3273 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3275 enum dwarf_location_atom op = *data++;
3280 name = dwarf_stack_op_name (op);
3283 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3284 op, (long) (data - 1 - start));
3285 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3286 (long) (data - 1 - start), name);
3291 ul = extract_unsigned_integer (data, addr_size,
3292 gdbarch_byte_order (arch));
3294 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3298 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3300 fprintf_filtered (stream, " %s", pulongest (ul));
3303 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3305 fprintf_filtered (stream, " %s", plongest (l));
3308 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3310 fprintf_filtered (stream, " %s", pulongest (ul));
3313 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3315 fprintf_filtered (stream, " %s", plongest (l));
3318 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3320 fprintf_filtered (stream, " %s", pulongest (ul));
3323 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3325 fprintf_filtered (stream, " %s", plongest (l));
3328 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3330 fprintf_filtered (stream, " %s", pulongest (ul));
3333 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3335 fprintf_filtered (stream, " %s", plongest (l));
3338 data = read_uleb128 (data, end, &ul);
3339 fprintf_filtered (stream, " %s", pulongest (ul));
3342 data = read_sleb128 (data, end, &l);
3343 fprintf_filtered (stream, " %s", plongest (l));
3378 fprintf_filtered (stream, " [$%s]",
3379 locexpr_regname (arch, op - DW_OP_reg0));
3383 data = read_uleb128 (data, end, &ul);
3384 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
3385 locexpr_regname (arch, (int) ul));
3388 case DW_OP_implicit_value:
3389 data = read_uleb128 (data, end, &ul);
3391 fprintf_filtered (stream, " %s", pulongest (ul));
3426 data = read_sleb128 (data, end, &l);
3427 fprintf_filtered (stream, " %s [$%s]", plongest (l),
3428 locexpr_regname (arch, op - DW_OP_breg0));
3432 data = read_uleb128 (data, end, &ul);
3433 data = read_sleb128 (data, end, &l);
3434 fprintf_filtered (stream, " register %s [$%s] offset %s",
3436 locexpr_regname (arch, (int) ul),
3441 data = read_sleb128 (data, end, &l);
3442 fprintf_filtered (stream, " %s", plongest (l));
3445 case DW_OP_xderef_size:
3446 case DW_OP_deref_size:
3448 fprintf_filtered (stream, " %d", *data);
3452 case DW_OP_plus_uconst:
3453 data = read_uleb128 (data, end, &ul);
3454 fprintf_filtered (stream, " %s", pulongest (ul));
3458 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3460 fprintf_filtered (stream, " to %ld",
3461 (long) (data + l - start));
3465 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3467 fprintf_filtered (stream, " %ld",
3468 (long) (data + l - start));
3472 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3474 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
3478 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3480 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3483 case DW_OP_call_ref:
3484 ul = extract_unsigned_integer (data, offset_size,
3485 gdbarch_byte_order (arch));
3486 data += offset_size;
3487 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
3491 data = read_uleb128 (data, end, &ul);
3492 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
3495 case DW_OP_bit_piece:
3499 data = read_uleb128 (data, end, &ul);
3500 data = read_uleb128 (data, end, &offset);
3501 fprintf_filtered (stream, " size %s offset %s (bits)",
3502 pulongest (ul), pulongest (offset));
3506 case DW_OP_GNU_implicit_pointer:
3508 ul = extract_unsigned_integer (data, offset_size,
3509 gdbarch_byte_order (arch));
3510 data += offset_size;
3512 data = read_sleb128 (data, end, &l);
3514 fprintf_filtered (stream, " DIE %s offset %s",
3515 phex_nz (ul, offset_size),
3520 case DW_OP_GNU_deref_type:
3522 int addr_size = *data++;
3526 data = read_uleb128 (data, end, &ul);
3528 type = dwarf2_get_die_type (offset, per_cu);
3529 fprintf_filtered (stream, "<");
3530 type_print (type, "", stream, -1);
3531 fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
3536 case DW_OP_GNU_const_type:
3541 data = read_uleb128 (data, end, &ul);
3542 type_die.cu_off = ul;
3543 type = dwarf2_get_die_type (type_die, per_cu);
3544 fprintf_filtered (stream, "<");
3545 type_print (type, "", stream, -1);
3546 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3550 case DW_OP_GNU_regval_type:
3556 data = read_uleb128 (data, end, ®);
3557 data = read_uleb128 (data, end, &ul);
3558 type_die.cu_off = ul;
3560 type = dwarf2_get_die_type (type_die, per_cu);
3561 fprintf_filtered (stream, "<");
3562 type_print (type, "", stream, -1);
3563 fprintf_filtered (stream, " [0x%s]> [$%s]",
3564 phex_nz (type_die.cu_off, 0),
3565 locexpr_regname (arch, reg));
3569 case DW_OP_GNU_convert:
3570 case DW_OP_GNU_reinterpret:
3574 data = read_uleb128 (data, end, &ul);
3575 type_die.cu_off = ul;
3577 if (type_die.cu_off == 0)
3578 fprintf_filtered (stream, "<0>");
3583 type = dwarf2_get_die_type (type_die, per_cu);
3584 fprintf_filtered (stream, "<");
3585 type_print (type, "", stream, -1);
3586 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3591 case DW_OP_GNU_entry_value:
3592 data = read_uleb128 (data, end, &ul);
3593 fputc_filtered ('\n', stream);
3594 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
3595 start, data, data + ul, indent + 2,
3601 fprintf_filtered (stream, "\n");
3607 /* Describe a single location, which may in turn consist of multiple
3611 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
3612 struct ui_file *stream,
3613 const gdb_byte *data, int size,
3614 struct objfile *objfile, unsigned int addr_size,
3615 int offset_size, struct dwarf2_per_cu_data *per_cu)
3617 const gdb_byte *end = data + size;
3618 int first_piece = 1, bad = 0;
3622 const gdb_byte *here = data;
3623 int disassemble = 1;
3628 fprintf_filtered (stream, _(", and "));
3630 if (!dwarf2_always_disassemble)
3632 data = locexpr_describe_location_piece (symbol, stream,
3634 data, end, addr_size);
3635 /* If we printed anything, or if we have an empty piece,
3636 then don't disassemble. */
3638 || data[0] == DW_OP_piece
3639 || data[0] == DW_OP_bit_piece)
3644 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
3645 data = disassemble_dwarf_expression (stream,
3646 get_objfile_arch (objfile),
3647 addr_size, offset_size, data,
3649 dwarf2_always_disassemble,
3655 int empty = data == here;
3658 fprintf_filtered (stream, " ");
3659 if (data[0] == DW_OP_piece)
3663 data = read_uleb128 (data + 1, end, &bytes);
3666 fprintf_filtered (stream, _("an empty %s-byte piece"),
3669 fprintf_filtered (stream, _(" [%s-byte piece]"),
3672 else if (data[0] == DW_OP_bit_piece)
3674 ULONGEST bits, offset;
3676 data = read_uleb128 (data + 1, end, &bits);
3677 data = read_uleb128 (data, end, &offset);
3680 fprintf_filtered (stream,
3681 _("an empty %s-bit piece"),
3684 fprintf_filtered (stream,
3685 _(" [%s-bit piece, offset %s bits]"),
3686 pulongest (bits), pulongest (offset));
3696 if (bad || data > end)
3697 error (_("Corrupted DWARF2 expression for \"%s\"."),
3698 SYMBOL_PRINT_NAME (symbol));
3701 /* Print a natural-language description of SYMBOL to STREAM. This
3702 version is for a symbol with a single location. */
3705 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
3706 struct ui_file *stream)
3708 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3709 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3710 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3711 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
3713 locexpr_describe_location_1 (symbol, addr, stream,
3714 dlbaton->data, dlbaton->size,
3715 objfile, addr_size, offset_size,
3719 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3720 any necessary bytecode in AX. */
3723 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
3724 struct agent_expr *ax, struct axs_value *value)
3726 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3727 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3729 if (dlbaton->size == 0)
3730 value->optimized_out = 1;
3732 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
3733 dlbaton->data, dlbaton->data + dlbaton->size,
3737 /* The set of location functions used with the DWARF-2 expression
3739 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
3740 locexpr_read_variable,
3741 locexpr_read_variable_at_entry,
3742 locexpr_read_needs_frame,
3743 locexpr_describe_location,
3744 locexpr_tracepoint_var_ref
3748 /* Wrapper functions for location lists. These generally find
3749 the appropriate location expression and call something above. */
3751 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3752 evaluator to calculate the location. */
3753 static struct value *
3754 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
3756 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3758 const gdb_byte *data;
3760 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
3762 data = dwarf2_find_location_expression (dlbaton, &size, pc);
3763 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
3769 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
3770 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3773 Function always returns non-NULL value, it may be marked optimized out if
3774 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
3775 if it cannot resolve the parameter for any reason. */
3777 static struct value *
3778 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3780 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3781 const gdb_byte *data;
3785 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
3786 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
3788 data = dwarf2_find_location_expression (dlbaton, &size, pc);
3790 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
3792 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
3795 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3797 loclist_read_needs_frame (struct symbol *symbol)
3799 /* If there's a location list, then assume we need to have a frame
3800 to choose the appropriate location expression. With tracking of
3801 global variables this is not necessarily true, but such tracking
3802 is disabled in GCC at the moment until we figure out how to
3808 /* Print a natural-language description of SYMBOL to STREAM. This
3809 version applies when there is a list of different locations, each
3810 with a specified address range. */
3813 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
3814 struct ui_file *stream)
3816 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3817 CORE_ADDR low, high;
3818 const gdb_byte *loc_ptr, *buf_end;
3819 int length, first = 1;
3820 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3821 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3822 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3823 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3824 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
3825 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
3826 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3827 /* Adjust base_address for relocatable objects. */
3828 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
3829 CORE_ADDR base_address = dlbaton->base_address + base_offset;
3831 loc_ptr = dlbaton->data;
3832 buf_end = dlbaton->data + dlbaton->size;
3834 fprintf_filtered (stream, _("multi-location:\n"));
3836 /* Iterate through locations until we run out. */
3839 if (buf_end - loc_ptr < 2 * addr_size)
3840 error (_("Corrupted DWARF expression for symbol \"%s\"."),
3841 SYMBOL_PRINT_NAME (symbol));
3844 low = extract_signed_integer (loc_ptr, addr_size, byte_order);
3846 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
3847 loc_ptr += addr_size;
3850 high = extract_signed_integer (loc_ptr, addr_size, byte_order);
3852 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
3853 loc_ptr += addr_size;
3855 /* A base-address-selection entry. */
3856 if ((low & base_mask) == base_mask)
3858 base_address = high + base_offset;
3859 fprintf_filtered (stream, _(" Base address %s"),
3860 paddress (gdbarch, base_address));
3864 /* An end-of-list entry. */
3865 if (low == 0 && high == 0)
3868 /* Otherwise, a location expression entry. */
3869 low += base_address;
3870 high += base_address;
3872 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
3875 /* (It would improve readability to print only the minimum
3876 necessary digits of the second number of the range.) */
3877 fprintf_filtered (stream, _(" Range %s-%s: "),
3878 paddress (gdbarch, low), paddress (gdbarch, high));
3880 /* Now describe this particular location. */
3881 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
3882 objfile, addr_size, offset_size,
3885 fprintf_filtered (stream, "\n");
3891 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3892 any necessary bytecode in AX. */
3894 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
3895 struct agent_expr *ax, struct axs_value *value)
3897 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3898 const gdb_byte *data;
3900 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3902 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
3904 value->optimized_out = 1;
3906 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
3910 /* The set of location functions used with the DWARF-2 expression
3911 evaluator and location lists. */
3912 const struct symbol_computed_ops dwarf2_loclist_funcs = {
3913 loclist_read_variable,
3914 loclist_read_variable_at_entry,
3915 loclist_read_needs_frame,
3916 loclist_describe_location,
3917 loclist_tracepoint_var_ref
3920 /* Provide a prototype to silence -Wmissing-prototypes. */
3921 extern initialize_file_ftype _initialize_dwarf2loc;
3924 _initialize_dwarf2loc (void)
3926 add_setshow_zinteger_cmd ("entry-values", class_maintenance,
3927 &entry_values_debug,
3928 _("Set entry values and tail call frames "
3930 _("Show entry values and tail call frames "
3932 _("When non-zero, the process of determining "
3933 "parameter values from function entry point "
3934 "and tail call frames will be printed."),
3936 show_entry_values_debug,
3937 &setdebuglist, &showdebuglist);