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 /* Until these have formal names, we define these here.
62 ref: http://gcc.gnu.org/wiki/DebugFission
63 Each entry in .debug_loc.dwo begins with a byte that describes the entry,
64 and is then followed by data specific to that entry. */
68 /* Indicates the end of the list of entries. */
69 DEBUG_LOC_END_OF_LIST = 0,
71 /* This is followed by an unsigned LEB128 number that is an index into
72 .debug_addr and specifies the base address for all following entries. */
73 DEBUG_LOC_BASE_ADDRESS = 1,
75 /* This is followed by two unsigned LEB128 numbers that are indices into
76 .debug_addr and specify the beginning and ending addresses, and then
77 a normal location expression as in .debug_loc. */
80 /* An internal value indicating there is insufficient data. */
81 DEBUG_LOC_BUFFER_OVERFLOW = -1,
83 /* An internal value indicating an invalid kind of entry was found. */
84 DEBUG_LOC_INVALID_ENTRY = -2
87 /* Decode the addresses in a non-dwo .debug_loc entry.
88 A pointer to the next byte to examine is returned in *NEW_PTR.
89 The encoded low,high addresses are return in *LOW,*HIGH.
90 The result indicates the kind of entry found. */
92 static enum debug_loc_kind
93 decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
94 const gdb_byte **new_ptr,
95 CORE_ADDR *low, CORE_ADDR *high,
96 enum bfd_endian byte_order,
97 unsigned int addr_size,
100 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
102 if (buf_end - loc_ptr < 2 * addr_size)
103 return DEBUG_LOC_BUFFER_OVERFLOW;
106 *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
108 *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
109 loc_ptr += addr_size;
112 *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
114 *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
115 loc_ptr += addr_size;
119 /* A base-address-selection entry. */
120 if ((*low & base_mask) == base_mask)
121 return DEBUG_LOC_BASE_ADDRESS;
123 /* An end-of-list entry. */
124 if (*low == 0 && *high == 0)
125 return DEBUG_LOC_END_OF_LIST;
127 return DEBUG_LOC_NORMAL;
130 /* Decode the addresses in .debug_loc.dwo entry.
131 A pointer to the next byte to examine is returned in *NEW_PTR.
132 The encoded low,high addresses are return in *LOW,*HIGH.
133 The result indicates the kind of entry found. */
135 static enum debug_loc_kind
136 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
137 const gdb_byte *loc_ptr,
138 const gdb_byte *buf_end,
139 const gdb_byte **new_ptr,
140 CORE_ADDR *low, CORE_ADDR *high)
142 unsigned long long low_index, high_index;
144 if (loc_ptr == buf_end)
145 return DEBUG_LOC_BUFFER_OVERFLOW;
149 case DEBUG_LOC_END_OF_LIST:
151 return DEBUG_LOC_END_OF_LIST;
152 case DEBUG_LOC_BASE_ADDRESS:
154 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
156 return DEBUG_LOC_BUFFER_OVERFLOW;
157 *high = dwarf2_read_addr_index (per_cu, high_index);
159 return DEBUG_LOC_BASE_ADDRESS;
160 case DEBUG_LOC_NORMAL:
161 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
163 return DEBUG_LOC_BUFFER_OVERFLOW;
164 *low = dwarf2_read_addr_index (per_cu, low_index);
165 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
167 return DEBUG_LOC_BUFFER_OVERFLOW;
168 *high = dwarf2_read_addr_index (per_cu, high_index);
170 return DEBUG_LOC_NORMAL;
172 return DEBUG_LOC_INVALID_ENTRY;
176 /* A function for dealing with location lists. Given a
177 symbol baton (BATON) and a pc value (PC), find the appropriate
178 location expression, set *LOCEXPR_LENGTH, and return a pointer
179 to the beginning of the expression. Returns NULL on failure.
181 For now, only return the first matching location expression; there
182 can be more than one in the list. */
185 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
186 size_t *locexpr_length, CORE_ADDR pc)
188 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
189 struct gdbarch *gdbarch = get_objfile_arch (objfile);
190 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
191 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
192 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
193 /* Adjust base_address for relocatable objects. */
194 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
195 CORE_ADDR base_address = baton->base_address + base_offset;
196 const gdb_byte *loc_ptr, *buf_end;
198 loc_ptr = baton->data;
199 buf_end = baton->data + baton->size;
203 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
205 enum debug_loc_kind kind;
206 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
209 kind = decode_debug_loc_dwo_addresses (baton->per_cu,
210 loc_ptr, buf_end, &new_ptr,
213 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
215 byte_order, addr_size,
220 case DEBUG_LOC_END_OF_LIST:
223 case DEBUG_LOC_BASE_ADDRESS:
224 base_address = high + base_offset;
226 case DEBUG_LOC_NORMAL:
228 case DEBUG_LOC_BUFFER_OVERFLOW:
229 case DEBUG_LOC_INVALID_ENTRY:
230 error (_("dwarf2_find_location_expression: "
231 "Corrupted DWARF expression."));
233 gdb_assert_not_reached ("bad debug_loc_kind");
236 /* Otherwise, a location expression entry. */
238 high += base_address;
240 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
243 if (low == high && pc == low)
245 /* This is entry PC record present only at entry point
246 of a function. Verify it is really the function entry point. */
248 struct block *pc_block = block_for_pc (pc);
249 struct symbol *pc_func = NULL;
252 pc_func = block_linkage_function (pc_block);
254 if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
256 *locexpr_length = length;
261 if (pc >= low && pc < high)
263 *locexpr_length = length;
271 /* This is the baton used when performing dwarf2 expression
273 struct dwarf_expr_baton
275 struct frame_info *frame;
276 struct dwarf2_per_cu_data *per_cu;
279 /* Helper functions for dwarf2_evaluate_loc_desc. */
281 /* Using the frame specified in BATON, return the value of register
282 REGNUM, treated as a pointer. */
284 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
286 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
287 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
291 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
292 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
293 regnum, debaton->frame);
297 /* Read memory at ADDR (length LEN) into BUF. */
300 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
302 read_memory (addr, buf, len);
305 /* Using the frame specified in BATON, find the location expression
306 describing the frame base. Return a pointer to it in START and
307 its length in LENGTH. */
309 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
311 /* FIXME: cagney/2003-03-26: This code should be using
312 get_frame_base_address(), and then implement a dwarf2 specific
314 struct symbol *framefunc;
315 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
317 /* Use block_linkage_function, which returns a real (not inlined)
318 function, instead of get_frame_function, which may return an
320 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
322 /* If we found a frame-relative symbol then it was certainly within
323 some function associated with a frame. If we can't find the frame,
324 something has gone wrong. */
325 gdb_assert (framefunc != NULL);
327 dwarf_expr_frame_base_1 (framefunc,
328 get_frame_address_in_block (debaton->frame),
333 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
334 const gdb_byte **start, size_t *length)
336 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
338 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
340 struct dwarf2_loclist_baton *symbaton;
342 symbaton = SYMBOL_LOCATION_BATON (framefunc);
343 *start = dwarf2_find_location_expression (symbaton, length, pc);
347 struct dwarf2_locexpr_baton *symbaton;
349 symbaton = SYMBOL_LOCATION_BATON (framefunc);
350 if (symbaton != NULL)
352 *length = symbaton->size;
353 *start = symbaton->data;
360 error (_("Could not find the frame base for \"%s\"."),
361 SYMBOL_NATURAL_NAME (framefunc));
364 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
365 the frame in BATON. */
368 dwarf_expr_frame_cfa (void *baton)
370 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
372 return dwarf2_frame_cfa (debaton->frame);
375 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
376 the frame in BATON. */
379 dwarf_expr_frame_pc (void *baton)
381 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
383 return get_frame_address_in_block (debaton->frame);
386 /* Using the objfile specified in BATON, find the address for the
387 current thread's thread-local storage with offset OFFSET. */
389 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
391 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
392 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
394 return target_translate_tls_address (objfile, offset);
397 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
398 current CU (as is PER_CU). State of the CTX is not affected by the
402 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
403 struct dwarf2_per_cu_data *per_cu,
404 CORE_ADDR (*get_frame_pc) (void *baton),
407 struct dwarf2_locexpr_baton block;
409 block = dwarf2_fetch_die_location_block (die_offset, per_cu,
410 get_frame_pc, baton);
412 /* DW_OP_call_ref is currently not supported. */
413 gdb_assert (block.per_cu == per_cu);
415 dwarf_expr_eval (ctx, block.data, block.size);
418 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
421 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
423 struct dwarf_expr_baton *debaton = ctx->baton;
425 per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
426 ctx->funcs->get_frame_pc, ctx->baton);
429 /* Callback function for dwarf2_evaluate_loc_desc. */
432 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
433 cu_offset die_offset)
435 struct dwarf_expr_baton *debaton = ctx->baton;
437 return dwarf2_get_die_type (die_offset, debaton->per_cu);
440 /* See dwarf2loc.h. */
442 int entry_values_debug = 0;
444 /* Helper to set entry_values_debug. */
447 show_entry_values_debug (struct ui_file *file, int from_tty,
448 struct cmd_list_element *c, const char *value)
450 fprintf_filtered (file,
451 _("Entry values and tail call frames debugging is %s.\n"),
455 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
456 CALLER_FRAME (for registers) can be NULL if it is not known. This function
457 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
460 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
461 struct call_site *call_site,
462 struct frame_info *caller_frame)
464 switch (FIELD_LOC_KIND (call_site->target))
466 case FIELD_LOC_KIND_DWARF_BLOCK:
468 struct dwarf2_locexpr_baton *dwarf_block;
470 struct type *caller_core_addr_type;
471 struct gdbarch *caller_arch;
473 dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
474 if (dwarf_block == NULL)
476 struct minimal_symbol *msym;
478 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
479 throw_error (NO_ENTRY_VALUE_ERROR,
480 _("DW_AT_GNU_call_site_target is not specified "
482 paddress (call_site_gdbarch, call_site->pc),
483 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
486 if (caller_frame == NULL)
488 struct minimal_symbol *msym;
490 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
491 throw_error (NO_ENTRY_VALUE_ERROR,
492 _("DW_AT_GNU_call_site_target DWARF block resolving "
493 "requires known frame which is currently not "
494 "available at %s in %s"),
495 paddress (call_site_gdbarch, call_site->pc),
496 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
499 caller_arch = get_frame_arch (caller_frame);
500 caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
501 val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
502 dwarf_block->data, dwarf_block->size,
503 dwarf_block->per_cu);
504 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
506 if (VALUE_LVAL (val) == lval_memory)
507 return value_address (val);
509 return value_as_address (val);
512 case FIELD_LOC_KIND_PHYSNAME:
514 const char *physname;
515 struct minimal_symbol *msym;
517 physname = FIELD_STATIC_PHYSNAME (call_site->target);
518 msym = lookup_minimal_symbol_text (physname, NULL);
521 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
522 throw_error (NO_ENTRY_VALUE_ERROR,
523 _("Cannot find function \"%s\" for a call site target "
525 physname, paddress (call_site_gdbarch, call_site->pc),
526 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
529 return SYMBOL_VALUE_ADDRESS (msym);
532 case FIELD_LOC_KIND_PHYSADDR:
533 return FIELD_STATIC_PHYSADDR (call_site->target);
536 internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
540 /* Convert function entry point exact address ADDR to the function which is
541 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
542 NO_ENTRY_VALUE_ERROR otherwise. */
544 static struct symbol *
545 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
547 struct symbol *sym = find_pc_function (addr);
550 if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
551 throw_error (NO_ENTRY_VALUE_ERROR,
552 _("DW_TAG_GNU_call_site resolving failed to find function "
553 "name for address %s"),
554 paddress (gdbarch, addr));
556 type = SYMBOL_TYPE (sym);
557 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
558 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
563 /* Verify function with entry point exact address ADDR can never call itself
564 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
565 can call itself via tail calls.
567 If a funtion can tail call itself its entry value based parameters are
568 unreliable. There is no verification whether the value of some/all
569 parameters is unchanged through the self tail call, we expect if there is
570 a self tail call all the parameters can be modified. */
573 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
575 struct obstack addr_obstack;
576 struct cleanup *old_chain;
579 /* Track here CORE_ADDRs which were already visited. */
582 /* The verification is completely unordered. Track here function addresses
583 which still need to be iterated. */
584 VEC (CORE_ADDR) *todo = NULL;
586 obstack_init (&addr_obstack);
587 old_chain = make_cleanup_obstack_free (&addr_obstack);
588 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
589 &addr_obstack, hashtab_obstack_allocate,
591 make_cleanup_htab_delete (addr_hash);
593 make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
595 VEC_safe_push (CORE_ADDR, todo, verify_addr);
596 while (!VEC_empty (CORE_ADDR, todo))
598 struct symbol *func_sym;
599 struct call_site *call_site;
601 addr = VEC_pop (CORE_ADDR, todo);
603 func_sym = func_addr_to_tail_call_list (gdbarch, addr);
605 for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
606 call_site; call_site = call_site->tail_call_next)
608 CORE_ADDR target_addr;
611 /* CALLER_FRAME with registers is not available for tail-call jumped
613 target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
615 if (target_addr == verify_addr)
617 struct minimal_symbol *msym;
619 msym = lookup_minimal_symbol_by_pc (verify_addr);
620 throw_error (NO_ENTRY_VALUE_ERROR,
621 _("DW_OP_GNU_entry_value resolving has found "
622 "function \"%s\" at %s can call itself via tail "
624 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym),
625 paddress (gdbarch, verify_addr));
628 slot = htab_find_slot (addr_hash, &target_addr, INSERT);
631 *slot = obstack_copy (&addr_obstack, &target_addr,
632 sizeof (target_addr));
633 VEC_safe_push (CORE_ADDR, todo, target_addr);
638 do_cleanups (old_chain);
641 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
642 ENTRY_VALUES_DEBUG. */
645 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
647 CORE_ADDR addr = call_site->pc;
648 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (addr - 1);
650 fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
651 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
655 /* vec.h needs single word type name, typedef it. */
656 typedef struct call_site *call_sitep;
658 /* Define VEC (call_sitep) functions. */
659 DEF_VEC_P (call_sitep);
661 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
662 only top callers and bottom callees which are present in both. GDBARCH is
663 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
664 no remaining possibilities to provide unambiguous non-trivial result.
665 RESULTP should point to NULL on the first (initialization) call. Caller is
666 responsible for xfree of any RESULTP data. */
669 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
670 VEC (call_sitep) *chain)
672 struct call_site_chain *result = *resultp;
673 long length = VEC_length (call_sitep, chain);
674 int callers, callees, idx;
678 /* Create the initial chain containing all the passed PCs. */
680 result = xmalloc (sizeof (*result) + sizeof (*result->call_site)
682 result->length = length;
683 result->callers = result->callees = length;
684 memcpy (result->call_site, VEC_address (call_sitep, chain),
685 sizeof (*result->call_site) * length);
688 if (entry_values_debug)
690 fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
691 for (idx = 0; idx < length; idx++)
692 tailcall_dump (gdbarch, result->call_site[idx]);
693 fputc_unfiltered ('\n', gdb_stdlog);
699 if (entry_values_debug)
701 fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
702 for (idx = 0; idx < length; idx++)
703 tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
704 fputc_unfiltered ('\n', gdb_stdlog);
707 /* Intersect callers. */
709 callers = min (result->callers, length);
710 for (idx = 0; idx < callers; idx++)
711 if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
713 result->callers = idx;
717 /* Intersect callees. */
719 callees = min (result->callees, length);
720 for (idx = 0; idx < callees; idx++)
721 if (result->call_site[result->length - 1 - idx]
722 != VEC_index (call_sitep, chain, length - 1 - idx))
724 result->callees = idx;
728 if (entry_values_debug)
730 fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
731 for (idx = 0; idx < result->callers; idx++)
732 tailcall_dump (gdbarch, result->call_site[idx]);
733 fputs_unfiltered (" |", gdb_stdlog);
734 for (idx = 0; idx < result->callees; idx++)
735 tailcall_dump (gdbarch, result->call_site[result->length
736 - result->callees + idx]);
737 fputc_unfiltered ('\n', gdb_stdlog);
740 if (result->callers == 0 && result->callees == 0)
742 /* There are no common callers or callees. It could be also a direct
743 call (which has length 0) with ambiguous possibility of an indirect
744 call - CALLERS == CALLEES == 0 is valid during the first allocation
745 but any subsequence processing of such entry means ambiguity. */
751 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
752 PC again. In such case there must be two different code paths to reach
753 it, therefore some of the former determined intermediate PCs must differ
754 and the unambiguous chain gets shortened. */
755 gdb_assert (result->callers + result->callees < result->length);
758 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
759 assumed frames between them use GDBARCH. Use depth first search so we can
760 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
761 would have needless GDB stack overhead. Caller is responsible for xfree of
762 the returned result. Any unreliability results in thrown
763 NO_ENTRY_VALUE_ERROR. */
765 static struct call_site_chain *
766 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
769 struct obstack addr_obstack;
770 struct cleanup *back_to_retval, *back_to_workdata;
771 struct call_site_chain *retval = NULL;
772 struct call_site *call_site;
774 /* Mark CALL_SITEs so we do not visit the same ones twice. */
777 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
778 call_site nor any possible call_site at CALLEE_PC's function is there.
779 Any CALL_SITE in CHAIN will be iterated to its siblings - via
780 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
781 VEC (call_sitep) *chain = NULL;
783 /* We are not interested in the specific PC inside the callee function. */
784 callee_pc = get_pc_function_start (callee_pc);
786 throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
787 paddress (gdbarch, callee_pc));
789 back_to_retval = make_cleanup (free_current_contents, &retval);
791 obstack_init (&addr_obstack);
792 back_to_workdata = make_cleanup_obstack_free (&addr_obstack);
793 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
794 &addr_obstack, hashtab_obstack_allocate,
796 make_cleanup_htab_delete (addr_hash);
798 make_cleanup (VEC_cleanup (call_sitep), &chain);
800 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
801 at the target's function. All the possible tail call sites in the
802 target's function will get iterated as already pushed into CHAIN via their
804 call_site = call_site_for_pc (gdbarch, caller_pc);
808 CORE_ADDR target_func_addr;
809 struct call_site *target_call_site;
811 /* CALLER_FRAME with registers is not available for tail-call jumped
813 target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
815 if (target_func_addr == callee_pc)
817 chain_candidate (gdbarch, &retval, chain);
821 /* There is no way to reach CALLEE_PC again as we would prevent
822 entering it twice as being already marked in ADDR_HASH. */
823 target_call_site = NULL;
827 struct symbol *target_func;
829 target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
830 target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
835 /* Attempt to visit TARGET_CALL_SITE. */
837 if (target_call_site)
841 slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
844 /* Successfully entered TARGET_CALL_SITE. */
846 *slot = &target_call_site->pc;
847 VEC_safe_push (call_sitep, chain, target_call_site);
852 /* Backtrack (without revisiting the originating call_site). Try the
853 callers's sibling; if there isn't any try the callers's callers's
856 target_call_site = NULL;
857 while (!VEC_empty (call_sitep, chain))
859 call_site = VEC_pop (call_sitep, chain);
861 gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
863 htab_remove_elt (addr_hash, &call_site->pc);
865 target_call_site = call_site->tail_call_next;
866 if (target_call_site)
870 while (target_call_site);
872 if (VEC_empty (call_sitep, chain))
875 call_site = VEC_last (call_sitep, chain);
880 struct minimal_symbol *msym_caller, *msym_callee;
882 msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
883 msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
884 throw_error (NO_ENTRY_VALUE_ERROR,
885 _("There are no unambiguously determinable intermediate "
886 "callers or callees between caller function \"%s\" at %s "
887 "and callee function \"%s\" at %s"),
889 ? "???" : SYMBOL_PRINT_NAME (msym_caller)),
890 paddress (gdbarch, caller_pc),
892 ? "???" : SYMBOL_PRINT_NAME (msym_callee)),
893 paddress (gdbarch, callee_pc));
896 do_cleanups (back_to_workdata);
897 discard_cleanups (back_to_retval);
901 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
902 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
903 constructed return NULL. Caller is responsible for xfree of the returned
906 struct call_site_chain *
907 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
910 volatile struct gdb_exception e;
911 struct call_site_chain *retval = NULL;
913 TRY_CATCH (e, RETURN_MASK_ERROR)
915 retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
919 if (e.error == NO_ENTRY_VALUE_ERROR)
921 if (entry_values_debug)
922 exception_print (gdb_stdout, e);
932 /* Fetch call_site_parameter from caller matching the parameters. FRAME is for
933 callee. See DWARF_REG and FB_OFFSET description at struct
934 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
936 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
939 static struct call_site_parameter *
940 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame, int dwarf_reg,
942 struct dwarf2_per_cu_data **per_cu_return)
944 CORE_ADDR func_addr = get_frame_func (frame);
946 struct gdbarch *gdbarch = get_frame_arch (frame);
947 struct frame_info *caller_frame = get_prev_frame (frame);
948 struct call_site *call_site;
950 /* Initialize it just to avoid a GCC false warning. */
951 struct call_site_parameter *parameter = NULL;
952 CORE_ADDR target_addr;
954 if (gdbarch != frame_unwind_arch (frame))
956 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr);
957 struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
959 throw_error (NO_ENTRY_VALUE_ERROR,
960 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
961 "(of %s (%s)) does not match caller gdbarch %s"),
962 gdbarch_bfd_arch_info (gdbarch)->printable_name,
963 paddress (gdbarch, func_addr),
964 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym),
965 gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
968 if (caller_frame == NULL)
970 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr);
972 throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
973 "requires caller of %s (%s)"),
974 paddress (gdbarch, func_addr),
975 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
977 caller_pc = get_frame_pc (caller_frame);
978 call_site = call_site_for_pc (gdbarch, caller_pc);
980 target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
981 if (target_addr != func_addr)
983 struct minimal_symbol *target_msym, *func_msym;
985 target_msym = lookup_minimal_symbol_by_pc (target_addr);
986 func_msym = lookup_minimal_symbol_by_pc (func_addr);
987 throw_error (NO_ENTRY_VALUE_ERROR,
988 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
989 "but the called frame is for %s at %s"),
990 (target_msym == NULL ? "???"
991 : SYMBOL_PRINT_NAME (target_msym)),
992 paddress (gdbarch, target_addr),
993 func_msym == NULL ? "???" : SYMBOL_PRINT_NAME (func_msym),
994 paddress (gdbarch, func_addr));
997 /* No entry value based parameters would be reliable if this function can
998 call itself via tail calls. */
999 func_verify_no_selftailcall (gdbarch, func_addr);
1001 for (iparams = 0; iparams < call_site->parameter_count; iparams++)
1003 parameter = &call_site->parameter[iparams];
1004 if (parameter->dwarf_reg == -1 && dwarf_reg == -1)
1006 if (parameter->fb_offset == fb_offset)
1009 else if (parameter->dwarf_reg == dwarf_reg)
1012 if (iparams == call_site->parameter_count)
1014 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (caller_pc);
1016 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
1017 determine its value. */
1018 throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
1019 "at DW_TAG_GNU_call_site %s at %s"),
1020 paddress (gdbarch, caller_pc),
1021 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
1024 *per_cu_return = call_site->per_cu;
1028 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
1029 the normal DW_AT_GNU_call_site_value block. Otherwise return the
1030 DW_AT_GNU_call_site_data_value (dereferenced) block.
1032 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1035 Function always returns non-NULL, non-optimized out value. It throws
1036 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
1038 static struct value *
1039 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
1040 CORE_ADDR deref_size, struct type *type,
1041 struct frame_info *caller_frame,
1042 struct dwarf2_per_cu_data *per_cu)
1044 const gdb_byte *data_src;
1048 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1049 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1051 /* DEREF_SIZE size is not verified here. */
1052 if (data_src == NULL)
1053 throw_error (NO_ENTRY_VALUE_ERROR,
1054 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1056 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
1057 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1059 data = alloca (size + 1);
1060 memcpy (data, data_src, size);
1061 data[size] = DW_OP_stack_value;
1063 return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
1066 /* Execute call_site_parameter's DWARF block matching DEREF_SIZE for caller of
1067 the CTX's frame. CTX must be of dwarf_expr_ctx_funcs kind. See DWARF_REG
1068 and FB_OFFSET description at struct
1069 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
1071 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
1072 can be more simple as it does not support cross-CU DWARF executions. */
1075 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1076 int dwarf_reg, CORE_ADDR fb_offset,
1079 struct dwarf_expr_baton *debaton;
1080 struct frame_info *frame, *caller_frame;
1081 struct dwarf2_per_cu_data *caller_per_cu;
1082 struct dwarf_expr_baton baton_local;
1083 struct dwarf_expr_context saved_ctx;
1084 struct call_site_parameter *parameter;
1085 const gdb_byte *data_src;
1088 gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
1089 debaton = ctx->baton;
1090 frame = debaton->frame;
1091 caller_frame = get_prev_frame (frame);
1093 parameter = dwarf_expr_reg_to_entry_parameter (frame, dwarf_reg, fb_offset,
1095 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1096 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1098 /* DEREF_SIZE size is not verified here. */
1099 if (data_src == NULL)
1100 throw_error (NO_ENTRY_VALUE_ERROR,
1101 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1103 baton_local.frame = caller_frame;
1104 baton_local.per_cu = caller_per_cu;
1106 saved_ctx.gdbarch = ctx->gdbarch;
1107 saved_ctx.addr_size = ctx->addr_size;
1108 saved_ctx.offset = ctx->offset;
1109 saved_ctx.baton = ctx->baton;
1110 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
1111 ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
1112 ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
1113 ctx->baton = &baton_local;
1115 dwarf_expr_eval (ctx, data_src, size);
1117 ctx->gdbarch = saved_ctx.gdbarch;
1118 ctx->addr_size = saved_ctx.addr_size;
1119 ctx->offset = saved_ctx.offset;
1120 ctx->baton = saved_ctx.baton;
1123 /* Callback function for dwarf2_evaluate_loc_desc.
1124 Fetch the address indexed by DW_OP_GNU_addr_index. */
1127 dwarf_expr_get_addr_index (void *baton, unsigned int index)
1129 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
1131 return dwarf2_read_addr_index (debaton->per_cu, index);
1134 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1135 the indirect method on it, that is use its stored target value, the sole
1136 purpose of entry_data_value_funcs.. */
1138 static struct value *
1139 entry_data_value_coerce_ref (const struct value *value)
1141 struct type *checked_type = check_typedef (value_type (value));
1142 struct value *target_val;
1144 if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
1147 target_val = value_computed_closure (value);
1148 value_incref (target_val);
1152 /* Implement copy_closure. */
1155 entry_data_value_copy_closure (const struct value *v)
1157 struct value *target_val = value_computed_closure (v);
1159 value_incref (target_val);
1163 /* Implement free_closure. */
1166 entry_data_value_free_closure (struct value *v)
1168 struct value *target_val = value_computed_closure (v);
1170 value_free (target_val);
1173 /* Vector for methods for an entry value reference where the referenced value
1174 is stored in the caller. On the first dereference use
1175 DW_AT_GNU_call_site_data_value in the caller. */
1177 static const struct lval_funcs entry_data_value_funcs =
1181 NULL, /* check_validity */
1182 NULL, /* check_any_valid */
1183 NULL, /* indirect */
1184 entry_data_value_coerce_ref,
1185 NULL, /* check_synthetic_pointer */
1186 entry_data_value_copy_closure,
1187 entry_data_value_free_closure
1190 /* Read parameter of TYPE at (callee) FRAME's function entry. DWARF_REG and
1191 FB_OFFSET are used to match DW_AT_location at the caller's
1192 DW_TAG_GNU_call_site_parameter. See DWARF_REG and FB_OFFSET description at
1193 struct dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
1195 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1196 cannot resolve the parameter for any reason. */
1198 static struct value *
1199 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1200 int dwarf_reg, CORE_ADDR fb_offset)
1202 struct type *checked_type = check_typedef (type);
1203 struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1204 struct frame_info *caller_frame = get_prev_frame (frame);
1205 struct value *outer_val, *target_val, *val;
1206 struct call_site_parameter *parameter;
1207 struct dwarf2_per_cu_data *caller_per_cu;
1210 parameter = dwarf_expr_reg_to_entry_parameter (frame, dwarf_reg, fb_offset,
1213 outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1217 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1218 used and it is not available do not fall back to OUTER_VAL - dereferencing
1219 TYPE_CODE_REF with non-entry data value would give current value - not the
1222 if (TYPE_CODE (checked_type) != TYPE_CODE_REF
1223 || TYPE_TARGET_TYPE (checked_type) == NULL)
1226 target_val = dwarf_entry_parameter_to_value (parameter,
1227 TYPE_LENGTH (target_type),
1228 target_type, caller_frame,
1231 /* value_as_address dereferences TYPE_CODE_REF. */
1232 addr = extract_typed_address (value_contents (outer_val), checked_type);
1234 /* The target entry value has artificial address of the entry value
1236 VALUE_LVAL (target_val) = lval_memory;
1237 set_value_address (target_val, addr);
1239 release_value (target_val);
1240 val = allocate_computed_value (type, &entry_data_value_funcs,
1241 target_val /* closure */);
1243 /* Copy the referencing pointer to the new computed value. */
1244 memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1245 TYPE_LENGTH (checked_type));
1246 set_value_lazy (val, 0);
1251 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1252 SIZE are DWARF block used to match DW_AT_location at the caller's
1253 DW_TAG_GNU_call_site_parameter.
1255 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1256 cannot resolve the parameter for any reason. */
1258 static struct value *
1259 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1260 const gdb_byte *block, size_t block_len)
1263 CORE_ADDR fb_offset;
1265 dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1266 if (dwarf_reg != -1)
1267 return value_of_dwarf_reg_entry (type, frame, dwarf_reg, 0 /* unused */);
1269 if (dwarf_block_to_fb_offset (block, block + block_len, &fb_offset))
1270 return value_of_dwarf_reg_entry (type, frame, -1, fb_offset);
1272 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1273 suppressed during normal operation. The expression can be arbitrary if
1274 there is no caller-callee entry value binding expected. */
1275 throw_error (NO_ENTRY_VALUE_ERROR,
1276 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1277 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1280 struct piece_closure
1282 /* Reference count. */
1285 /* The CU from which this closure's expression came. */
1286 struct dwarf2_per_cu_data *per_cu;
1288 /* The number of pieces used to describe this variable. */
1291 /* The target address size, used only for DWARF_VALUE_STACK. */
1294 /* The pieces themselves. */
1295 struct dwarf_expr_piece *pieces;
1298 /* Allocate a closure for a value formed from separately-described
1301 static struct piece_closure *
1302 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1303 int n_pieces, struct dwarf_expr_piece *pieces,
1306 struct piece_closure *c = XZALLOC (struct piece_closure);
1311 c->n_pieces = n_pieces;
1312 c->addr_size = addr_size;
1313 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
1315 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
1316 for (i = 0; i < n_pieces; ++i)
1317 if (c->pieces[i].location == DWARF_VALUE_STACK)
1318 value_incref (c->pieces[i].v.value);
1323 /* The lowest-level function to extract bits from a byte buffer.
1324 SOURCE is the buffer. It is updated if we read to the end of a
1326 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1327 updated to reflect the number of bits actually read.
1328 NBITS is the number of bits we want to read. It is updated to
1329 reflect the number of bits actually read. This function may read
1331 BITS_BIG_ENDIAN is taken directly from gdbarch.
1332 This function returns the extracted bits. */
1335 extract_bits_primitive (const gdb_byte **source,
1336 unsigned int *source_offset_bits,
1337 int *nbits, int bits_big_endian)
1339 unsigned int avail, mask, datum;
1341 gdb_assert (*source_offset_bits < 8);
1343 avail = 8 - *source_offset_bits;
1347 mask = (1 << avail) - 1;
1349 if (bits_big_endian)
1350 datum >>= 8 - (*source_offset_bits + *nbits);
1352 datum >>= *source_offset_bits;
1356 *source_offset_bits += avail;
1357 if (*source_offset_bits >= 8)
1359 *source_offset_bits -= 8;
1366 /* Extract some bits from a source buffer and move forward in the
1369 SOURCE is the source buffer. It is updated as bytes are read.
1370 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1372 NBITS is the number of bits to read.
1373 BITS_BIG_ENDIAN is taken directly from gdbarch.
1375 This function returns the bits that were read. */
1378 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
1379 int nbits, int bits_big_endian)
1383 gdb_assert (nbits > 0 && nbits <= 8);
1385 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
1391 more = extract_bits_primitive (source, source_offset_bits, &nbits,
1393 if (bits_big_endian)
1403 /* Write some bits into a buffer and move forward in the buffer.
1405 DATUM is the bits to write. The low-order bits of DATUM are used.
1406 DEST is the destination buffer. It is updated as bytes are
1408 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1410 NBITS is the number of valid bits in DATUM.
1411 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1414 insert_bits (unsigned int datum,
1415 gdb_byte *dest, unsigned int dest_offset_bits,
1416 int nbits, int bits_big_endian)
1420 gdb_assert (dest_offset_bits + nbits <= 8);
1422 mask = (1 << nbits) - 1;
1423 if (bits_big_endian)
1425 datum <<= 8 - (dest_offset_bits + nbits);
1426 mask <<= 8 - (dest_offset_bits + nbits);
1430 datum <<= dest_offset_bits;
1431 mask <<= dest_offset_bits;
1434 gdb_assert ((datum & ~mask) == 0);
1436 *dest = (*dest & ~mask) | datum;
1439 /* Copy bits from a source to a destination.
1441 DEST is where the bits should be written.
1442 DEST_OFFSET_BITS is the bit offset into DEST.
1443 SOURCE is the source of bits.
1444 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1445 BIT_COUNT is the number of bits to copy.
1446 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1449 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
1450 const gdb_byte *source, unsigned int source_offset_bits,
1451 unsigned int bit_count,
1452 int bits_big_endian)
1454 unsigned int dest_avail;
1457 /* Reduce everything to byte-size pieces. */
1458 dest += dest_offset_bits / 8;
1459 dest_offset_bits %= 8;
1460 source += source_offset_bits / 8;
1461 source_offset_bits %= 8;
1463 dest_avail = 8 - dest_offset_bits % 8;
1465 /* See if we can fill the first destination byte. */
1466 if (dest_avail < bit_count)
1468 datum = extract_bits (&source, &source_offset_bits, dest_avail,
1470 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
1472 dest_offset_bits = 0;
1473 bit_count -= dest_avail;
1476 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1477 than 8 bits remaining. */
1478 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
1479 for (; bit_count >= 8; bit_count -= 8)
1481 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
1482 *dest++ = (gdb_byte) datum;
1485 /* Finally, we may have a few leftover bits. */
1486 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
1489 datum = extract_bits (&source, &source_offset_bits, bit_count,
1491 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
1496 read_pieced_value (struct value *v)
1500 ULONGEST bits_to_skip;
1502 struct piece_closure *c
1503 = (struct piece_closure *) value_computed_closure (v);
1504 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
1506 size_t buffer_size = 0;
1507 char *buffer = NULL;
1508 struct cleanup *cleanup;
1510 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1512 if (value_type (v) != value_enclosing_type (v))
1513 internal_error (__FILE__, __LINE__,
1514 _("Should not be able to create a lazy value with "
1515 "an enclosing type"));
1517 cleanup = make_cleanup (free_current_contents, &buffer);
1519 contents = value_contents_raw (v);
1520 bits_to_skip = 8 * value_offset (v);
1521 if (value_bitsize (v))
1523 bits_to_skip += value_bitpos (v);
1524 type_len = value_bitsize (v);
1527 type_len = 8 * TYPE_LENGTH (value_type (v));
1529 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1531 struct dwarf_expr_piece *p = &c->pieces[i];
1532 size_t this_size, this_size_bits;
1533 long dest_offset_bits, source_offset_bits, source_offset;
1534 const gdb_byte *intermediate_buffer;
1536 /* Compute size, source, and destination offsets for copying, in
1538 this_size_bits = p->size;
1539 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1541 bits_to_skip -= this_size_bits;
1544 if (this_size_bits > type_len - offset)
1545 this_size_bits = type_len - offset;
1546 if (bits_to_skip > 0)
1548 dest_offset_bits = 0;
1549 source_offset_bits = bits_to_skip;
1550 this_size_bits -= bits_to_skip;
1555 dest_offset_bits = offset;
1556 source_offset_bits = 0;
1559 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1560 source_offset = source_offset_bits / 8;
1561 if (buffer_size < this_size)
1563 buffer_size = this_size;
1564 buffer = xrealloc (buffer, buffer_size);
1566 intermediate_buffer = buffer;
1568 /* Copy from the source to DEST_BUFFER. */
1569 switch (p->location)
1571 case DWARF_VALUE_REGISTER:
1573 struct gdbarch *arch = get_frame_arch (frame);
1574 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1575 int reg_offset = source_offset;
1577 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1578 && this_size < register_size (arch, gdb_regnum))
1580 /* Big-endian, and we want less than full size. */
1581 reg_offset = register_size (arch, gdb_regnum) - this_size;
1582 /* We want the lower-order THIS_SIZE_BITS of the bytes
1583 we extract from the register. */
1584 source_offset_bits += 8 * this_size - this_size_bits;
1587 if (gdb_regnum != -1)
1591 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1595 /* Just so garbage doesn't ever shine through. */
1596 memset (buffer, 0, this_size);
1599 set_value_optimized_out (v, 1);
1601 mark_value_bytes_unavailable (v, offset, this_size);
1606 error (_("Unable to access DWARF register number %s"),
1607 paddress (arch, p->v.regno));
1612 case DWARF_VALUE_MEMORY:
1613 read_value_memory (v, offset,
1614 p->v.mem.in_stack_memory,
1615 p->v.mem.addr + source_offset,
1619 case DWARF_VALUE_STACK:
1621 size_t n = this_size;
1623 if (n > c->addr_size - source_offset)
1624 n = (c->addr_size >= source_offset
1625 ? c->addr_size - source_offset
1633 const gdb_byte *val_bytes = value_contents_all (p->v.value);
1635 intermediate_buffer = val_bytes + source_offset;
1640 case DWARF_VALUE_LITERAL:
1642 size_t n = this_size;
1644 if (n > p->v.literal.length - source_offset)
1645 n = (p->v.literal.length >= source_offset
1646 ? p->v.literal.length - source_offset
1649 intermediate_buffer = p->v.literal.data + source_offset;
1653 /* These bits show up as zeros -- but do not cause the value
1654 to be considered optimized-out. */
1655 case DWARF_VALUE_IMPLICIT_POINTER:
1658 case DWARF_VALUE_OPTIMIZED_OUT:
1659 set_value_optimized_out (v, 1);
1663 internal_error (__FILE__, __LINE__, _("invalid location type"));
1666 if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1667 && p->location != DWARF_VALUE_IMPLICIT_POINTER)
1668 copy_bitwise (contents, dest_offset_bits,
1669 intermediate_buffer, source_offset_bits % 8,
1670 this_size_bits, bits_big_endian);
1672 offset += this_size_bits;
1675 do_cleanups (cleanup);
1679 write_pieced_value (struct value *to, struct value *from)
1683 ULONGEST bits_to_skip;
1684 const gdb_byte *contents;
1685 struct piece_closure *c
1686 = (struct piece_closure *) value_computed_closure (to);
1687 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
1689 size_t buffer_size = 0;
1690 char *buffer = NULL;
1691 struct cleanup *cleanup;
1693 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
1697 set_value_optimized_out (to, 1);
1701 cleanup = make_cleanup (free_current_contents, &buffer);
1703 contents = value_contents (from);
1704 bits_to_skip = 8 * value_offset (to);
1705 if (value_bitsize (to))
1707 bits_to_skip += value_bitpos (to);
1708 type_len = value_bitsize (to);
1711 type_len = 8 * TYPE_LENGTH (value_type (to));
1713 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1715 struct dwarf_expr_piece *p = &c->pieces[i];
1716 size_t this_size_bits, this_size;
1717 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1719 const gdb_byte *source_buffer;
1721 this_size_bits = p->size;
1722 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1724 bits_to_skip -= this_size_bits;
1727 if (this_size_bits > type_len - offset)
1728 this_size_bits = type_len - offset;
1729 if (bits_to_skip > 0)
1731 dest_offset_bits = bits_to_skip;
1732 source_offset_bits = 0;
1733 this_size_bits -= bits_to_skip;
1738 dest_offset_bits = 0;
1739 source_offset_bits = offset;
1742 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1743 source_offset = source_offset_bits / 8;
1744 dest_offset = dest_offset_bits / 8;
1745 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1747 source_buffer = contents + source_offset;
1752 if (buffer_size < this_size)
1754 buffer_size = this_size;
1755 buffer = xrealloc (buffer, buffer_size);
1757 source_buffer = buffer;
1761 switch (p->location)
1763 case DWARF_VALUE_REGISTER:
1765 struct gdbarch *arch = get_frame_arch (frame);
1766 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1767 int reg_offset = dest_offset;
1769 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1770 && this_size <= register_size (arch, gdb_regnum))
1771 /* Big-endian, and we want less than full size. */
1772 reg_offset = register_size (arch, gdb_regnum) - this_size;
1774 if (gdb_regnum != -1)
1780 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1785 error (_("Can't do read-modify-write to "
1786 "update bitfield; containing word has been "
1789 throw_error (NOT_AVAILABLE_ERROR,
1790 _("Can't do read-modify-write to update "
1791 "bitfield; containing word "
1794 copy_bitwise (buffer, dest_offset_bits,
1795 contents, source_offset_bits,
1800 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
1801 this_size, source_buffer);
1805 error (_("Unable to write to DWARF register number %s"),
1806 paddress (arch, p->v.regno));
1810 case DWARF_VALUE_MEMORY:
1813 /* Only the first and last bytes can possibly have any
1815 read_memory (p->v.mem.addr + dest_offset, buffer, 1);
1816 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
1817 buffer + this_size - 1, 1);
1818 copy_bitwise (buffer, dest_offset_bits,
1819 contents, source_offset_bits,
1824 write_memory (p->v.mem.addr + dest_offset,
1825 source_buffer, this_size);
1828 set_value_optimized_out (to, 1);
1831 offset += this_size_bits;
1834 do_cleanups (cleanup);
1837 /* A helper function that checks bit validity in a pieced value.
1838 CHECK_FOR indicates the kind of validity checking.
1839 DWARF_VALUE_MEMORY means to check whether any bit is valid.
1840 DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
1842 DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
1843 implicit pointer. */
1846 check_pieced_value_bits (const struct value *value, int bit_offset,
1848 enum dwarf_value_location check_for)
1850 struct piece_closure *c
1851 = (struct piece_closure *) value_computed_closure (value);
1853 int validity = (check_for == DWARF_VALUE_MEMORY
1854 || check_for == DWARF_VALUE_IMPLICIT_POINTER);
1856 bit_offset += 8 * value_offset (value);
1857 if (value_bitsize (value))
1858 bit_offset += value_bitpos (value);
1860 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1862 struct dwarf_expr_piece *p = &c->pieces[i];
1863 size_t this_size_bits = p->size;
1867 if (bit_offset >= this_size_bits)
1869 bit_offset -= this_size_bits;
1873 bit_length -= this_size_bits - bit_offset;
1877 bit_length -= this_size_bits;
1879 if (check_for == DWARF_VALUE_IMPLICIT_POINTER)
1881 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1884 else if (p->location == DWARF_VALUE_OPTIMIZED_OUT
1885 || p->location == DWARF_VALUE_IMPLICIT_POINTER)
1901 check_pieced_value_validity (const struct value *value, int bit_offset,
1904 return check_pieced_value_bits (value, bit_offset, bit_length,
1905 DWARF_VALUE_MEMORY);
1909 check_pieced_value_invalid (const struct value *value)
1911 return check_pieced_value_bits (value, 0,
1912 8 * TYPE_LENGTH (value_type (value)),
1913 DWARF_VALUE_OPTIMIZED_OUT);
1916 /* An implementation of an lval_funcs method to see whether a value is
1917 a synthetic pointer. */
1920 check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
1923 return check_pieced_value_bits (value, bit_offset, bit_length,
1924 DWARF_VALUE_IMPLICIT_POINTER);
1927 /* A wrapper function for get_frame_address_in_block. */
1930 get_frame_address_in_block_wrapper (void *baton)
1932 return get_frame_address_in_block (baton);
1935 /* An implementation of an lval_funcs method to indirect through a
1936 pointer. This handles the synthetic pointer case when needed. */
1938 static struct value *
1939 indirect_pieced_value (struct value *value)
1941 struct piece_closure *c
1942 = (struct piece_closure *) value_computed_closure (value);
1944 struct frame_info *frame;
1945 struct dwarf2_locexpr_baton baton;
1946 int i, bit_offset, bit_length;
1947 struct dwarf_expr_piece *piece = NULL;
1948 LONGEST byte_offset;
1950 type = check_typedef (value_type (value));
1951 if (TYPE_CODE (type) != TYPE_CODE_PTR)
1954 bit_length = 8 * TYPE_LENGTH (type);
1955 bit_offset = 8 * value_offset (value);
1956 if (value_bitsize (value))
1957 bit_offset += value_bitpos (value);
1959 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1961 struct dwarf_expr_piece *p = &c->pieces[i];
1962 size_t this_size_bits = p->size;
1966 if (bit_offset >= this_size_bits)
1968 bit_offset -= this_size_bits;
1972 bit_length -= this_size_bits - bit_offset;
1976 bit_length -= this_size_bits;
1978 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1981 if (bit_length != 0)
1982 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
1988 frame = get_selected_frame (_("No frame selected."));
1990 /* This is an offset requested by GDB, such as value subcripts. */
1991 byte_offset = value_as_address (value);
1994 baton = dwarf2_fetch_die_location_block (piece->v.ptr.die, c->per_cu,
1995 get_frame_address_in_block_wrapper,
1998 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
1999 baton.data, baton.size, baton.per_cu,
2000 piece->v.ptr.offset + byte_offset);
2004 copy_pieced_value_closure (const struct value *v)
2006 struct piece_closure *c
2007 = (struct piece_closure *) value_computed_closure (v);
2014 free_pieced_value_closure (struct value *v)
2016 struct piece_closure *c
2017 = (struct piece_closure *) value_computed_closure (v);
2024 for (i = 0; i < c->n_pieces; ++i)
2025 if (c->pieces[i].location == DWARF_VALUE_STACK)
2026 value_free (c->pieces[i].v.value);
2033 /* Functions for accessing a variable described by DW_OP_piece. */
2034 static const struct lval_funcs pieced_value_funcs = {
2037 check_pieced_value_validity,
2038 check_pieced_value_invalid,
2039 indirect_pieced_value,
2040 NULL, /* coerce_ref */
2041 check_pieced_synthetic_pointer,
2042 copy_pieced_value_closure,
2043 free_pieced_value_closure
2046 /* Helper function which throws an error if a synthetic pointer is
2050 invalid_synthetic_pointer (void)
2052 error (_("access outside bounds of object "
2053 "referenced via synthetic pointer"));
2056 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
2058 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
2060 dwarf_expr_read_reg,
2061 dwarf_expr_read_mem,
2062 dwarf_expr_frame_base,
2063 dwarf_expr_frame_cfa,
2064 dwarf_expr_frame_pc,
2065 dwarf_expr_tls_address,
2066 dwarf_expr_dwarf_call,
2067 dwarf_expr_get_base_type,
2068 dwarf_expr_push_dwarf_reg_entry_value,
2069 dwarf_expr_get_addr_index
2072 /* Evaluate a location description, starting at DATA and with length
2073 SIZE, to find the current location of variable of TYPE in the
2074 context of FRAME. BYTE_OFFSET is applied after the contents are
2077 static struct value *
2078 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2079 const gdb_byte *data, unsigned short size,
2080 struct dwarf2_per_cu_data *per_cu,
2081 LONGEST byte_offset)
2083 struct value *retval;
2084 struct dwarf_expr_baton baton;
2085 struct dwarf_expr_context *ctx;
2086 struct cleanup *old_chain, *value_chain;
2087 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2088 volatile struct gdb_exception ex;
2090 if (byte_offset < 0)
2091 invalid_synthetic_pointer ();
2094 return allocate_optimized_out_value (type);
2096 baton.frame = frame;
2097 baton.per_cu = per_cu;
2099 ctx = new_dwarf_expr_context ();
2100 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2101 value_chain = make_cleanup_value_free_to_mark (value_mark ());
2103 ctx->gdbarch = get_objfile_arch (objfile);
2104 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2105 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2106 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2107 ctx->baton = &baton;
2108 ctx->funcs = &dwarf_expr_ctx_funcs;
2110 TRY_CATCH (ex, RETURN_MASK_ERROR)
2112 dwarf_expr_eval (ctx, data, size);
2116 if (ex.error == NOT_AVAILABLE_ERROR)
2118 do_cleanups (old_chain);
2119 retval = allocate_value (type);
2120 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
2123 else if (ex.error == NO_ENTRY_VALUE_ERROR)
2125 if (entry_values_debug)
2126 exception_print (gdb_stdout, ex);
2127 do_cleanups (old_chain);
2128 return allocate_optimized_out_value (type);
2131 throw_exception (ex);
2134 if (ctx->num_pieces > 0)
2136 struct piece_closure *c;
2137 struct frame_id frame_id = get_frame_id (frame);
2138 ULONGEST bit_size = 0;
2141 for (i = 0; i < ctx->num_pieces; ++i)
2142 bit_size += ctx->pieces[i].size;
2143 if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
2144 invalid_synthetic_pointer ();
2146 c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
2148 /* We must clean up the value chain after creating the piece
2149 closure but before allocating the result. */
2150 do_cleanups (value_chain);
2151 retval = allocate_computed_value (type, &pieced_value_funcs, c);
2152 VALUE_FRAME_ID (retval) = frame_id;
2153 set_value_offset (retval, byte_offset);
2157 switch (ctx->location)
2159 case DWARF_VALUE_REGISTER:
2161 struct gdbarch *arch = get_frame_arch (frame);
2162 ULONGEST dwarf_regnum = value_as_long (dwarf_expr_fetch (ctx, 0));
2163 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
2165 if (byte_offset != 0)
2166 error (_("cannot use offset on synthetic pointer to register"));
2167 do_cleanups (value_chain);
2168 if (gdb_regnum != -1)
2169 retval = value_from_register (type, gdb_regnum, frame);
2171 error (_("Unable to access DWARF register number %s"),
2172 paddress (arch, dwarf_regnum));
2176 case DWARF_VALUE_MEMORY:
2178 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
2179 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
2181 do_cleanups (value_chain);
2182 retval = allocate_value_lazy (type);
2183 VALUE_LVAL (retval) = lval_memory;
2184 if (in_stack_memory)
2185 set_value_stack (retval, 1);
2186 set_value_address (retval, address + byte_offset);
2190 case DWARF_VALUE_STACK:
2192 struct value *value = dwarf_expr_fetch (ctx, 0);
2194 const gdb_byte *val_bytes;
2195 size_t n = TYPE_LENGTH (value_type (value));
2197 if (byte_offset + TYPE_LENGTH (type) > n)
2198 invalid_synthetic_pointer ();
2200 val_bytes = value_contents_all (value);
2201 val_bytes += byte_offset;
2204 /* Preserve VALUE because we are going to free values back
2205 to the mark, but we still need the value contents
2207 value_incref (value);
2208 do_cleanups (value_chain);
2209 make_cleanup_value_free (value);
2211 retval = allocate_value (type);
2212 contents = value_contents_raw (retval);
2213 if (n > TYPE_LENGTH (type))
2215 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2217 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2218 val_bytes += n - TYPE_LENGTH (type);
2219 n = TYPE_LENGTH (type);
2221 memcpy (contents, val_bytes, n);
2225 case DWARF_VALUE_LITERAL:
2228 const bfd_byte *ldata;
2229 size_t n = ctx->len;
2231 if (byte_offset + TYPE_LENGTH (type) > n)
2232 invalid_synthetic_pointer ();
2234 do_cleanups (value_chain);
2235 retval = allocate_value (type);
2236 contents = value_contents_raw (retval);
2238 ldata = ctx->data + byte_offset;
2241 if (n > TYPE_LENGTH (type))
2243 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2245 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2246 ldata += n - TYPE_LENGTH (type);
2247 n = TYPE_LENGTH (type);
2249 memcpy (contents, ldata, n);
2253 case DWARF_VALUE_OPTIMIZED_OUT:
2254 do_cleanups (value_chain);
2255 retval = allocate_optimized_out_value (type);
2258 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2259 operation by execute_stack_op. */
2260 case DWARF_VALUE_IMPLICIT_POINTER:
2261 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2262 it can only be encountered when making a piece. */
2264 internal_error (__FILE__, __LINE__, _("invalid location type"));
2268 set_value_initialized (retval, ctx->initialized);
2270 do_cleanups (old_chain);
2275 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2276 passes 0 as the byte_offset. */
2279 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2280 const gdb_byte *data, unsigned short size,
2281 struct dwarf2_per_cu_data *per_cu)
2283 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2287 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
2289 struct needs_frame_baton
2292 struct dwarf2_per_cu_data *per_cu;
2295 /* Reads from registers do require a frame. */
2297 needs_frame_read_reg (void *baton, int regnum)
2299 struct needs_frame_baton *nf_baton = baton;
2301 nf_baton->needs_frame = 1;
2305 /* Reads from memory do not require a frame. */
2307 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
2309 memset (buf, 0, len);
2312 /* Frame-relative accesses do require a frame. */
2314 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
2316 static gdb_byte lit0 = DW_OP_lit0;
2317 struct needs_frame_baton *nf_baton = baton;
2322 nf_baton->needs_frame = 1;
2325 /* CFA accesses require a frame. */
2328 needs_frame_frame_cfa (void *baton)
2330 struct needs_frame_baton *nf_baton = baton;
2332 nf_baton->needs_frame = 1;
2336 /* Thread-local accesses do require a frame. */
2338 needs_frame_tls_address (void *baton, CORE_ADDR offset)
2340 struct needs_frame_baton *nf_baton = baton;
2342 nf_baton->needs_frame = 1;
2346 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
2349 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
2351 struct needs_frame_baton *nf_baton = ctx->baton;
2353 per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
2354 ctx->funcs->get_frame_pc, ctx->baton);
2357 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
2360 needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
2361 int dwarf_reg, CORE_ADDR fb_offset, int deref_size)
2363 struct needs_frame_baton *nf_baton = ctx->baton;
2365 nf_baton->needs_frame = 1;
2368 /* DW_OP_GNU_addr_index doesn't require a frame. */
2371 needs_get_addr_index (void *baton, unsigned int index)
2373 /* Nothing to do. */
2377 /* Virtual method table for dwarf2_loc_desc_needs_frame below. */
2379 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
2381 needs_frame_read_reg,
2382 needs_frame_read_mem,
2383 needs_frame_frame_base,
2384 needs_frame_frame_cfa,
2385 needs_frame_frame_cfa, /* get_frame_pc */
2386 needs_frame_tls_address,
2387 needs_frame_dwarf_call,
2388 NULL, /* get_base_type */
2389 needs_dwarf_reg_entry_value,
2390 needs_get_addr_index
2393 /* Return non-zero iff the location expression at DATA (length SIZE)
2394 requires a frame to evaluate. */
2397 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
2398 struct dwarf2_per_cu_data *per_cu)
2400 struct needs_frame_baton baton;
2401 struct dwarf_expr_context *ctx;
2403 struct cleanup *old_chain;
2404 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2406 baton.needs_frame = 0;
2407 baton.per_cu = per_cu;
2409 ctx = new_dwarf_expr_context ();
2410 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2411 make_cleanup_value_free_to_mark (value_mark ());
2413 ctx->gdbarch = get_objfile_arch (objfile);
2414 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2415 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2416 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2417 ctx->baton = &baton;
2418 ctx->funcs = &needs_frame_ctx_funcs;
2420 dwarf_expr_eval (ctx, data, size);
2422 in_reg = ctx->location == DWARF_VALUE_REGISTER;
2424 if (ctx->num_pieces > 0)
2428 /* If the location has several pieces, and any of them are in
2429 registers, then we will need a frame to fetch them from. */
2430 for (i = 0; i < ctx->num_pieces; i++)
2431 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
2435 do_cleanups (old_chain);
2437 return baton.needs_frame || in_reg;
2440 /* A helper function that throws an unimplemented error mentioning a
2441 given DWARF operator. */
2444 unimplemented (unsigned int op)
2446 const char *name = get_DW_OP_name (op);
2449 error (_("DWARF operator %s cannot be translated to an agent expression"),
2452 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2453 "to an agent expression"),
2457 /* A helper function to convert a DWARF register to an arch register.
2458 ARCH is the architecture.
2459 DWARF_REG is the register.
2460 This will throw an exception if the DWARF register cannot be
2461 translated to an architecture register. */
2464 translate_register (struct gdbarch *arch, int dwarf_reg)
2466 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2468 error (_("Unable to access DWARF register number %d"), dwarf_reg);
2472 /* A helper function that emits an access to memory. ARCH is the
2473 target architecture. EXPR is the expression which we are building.
2474 NBITS is the number of bits we want to read. This emits the
2475 opcodes needed to read the memory and then extract the desired
2479 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2481 ULONGEST nbytes = (nbits + 7) / 8;
2483 gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
2486 ax_trace_quick (expr, nbytes);
2489 ax_simple (expr, aop_ref8);
2490 else if (nbits <= 16)
2491 ax_simple (expr, aop_ref16);
2492 else if (nbits <= 32)
2493 ax_simple (expr, aop_ref32);
2495 ax_simple (expr, aop_ref64);
2497 /* If we read exactly the number of bytes we wanted, we're done. */
2498 if (8 * nbytes == nbits)
2501 if (gdbarch_bits_big_endian (arch))
2503 /* On a bits-big-endian machine, we want the high-order
2505 ax_const_l (expr, 8 * nbytes - nbits);
2506 ax_simple (expr, aop_rsh_unsigned);
2510 /* On a bits-little-endian box, we want the low-order NBITS. */
2511 ax_zero_ext (expr, nbits);
2515 /* A helper function to return the frame's PC. */
2518 get_ax_pc (void *baton)
2520 struct agent_expr *expr = baton;
2525 /* Compile a DWARF location expression to an agent expression.
2527 EXPR is the agent expression we are building.
2528 LOC is the agent value we modify.
2529 ARCH is the architecture.
2530 ADDR_SIZE is the size of addresses, in bytes.
2531 OP_PTR is the start of the location expression.
2532 OP_END is one past the last byte of the location expression.
2534 This will throw an exception for various kinds of errors -- for
2535 example, if the expression cannot be compiled, or if the expression
2539 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
2540 struct gdbarch *arch, unsigned int addr_size,
2541 const gdb_byte *op_ptr, const gdb_byte *op_end,
2542 struct dwarf2_per_cu_data *per_cu)
2544 struct cleanup *cleanups;
2546 VEC(int) *dw_labels = NULL, *patches = NULL;
2547 const gdb_byte * const base = op_ptr;
2548 const gdb_byte *previous_piece = op_ptr;
2549 enum bfd_endian byte_order = gdbarch_byte_order (arch);
2550 ULONGEST bits_collected = 0;
2551 unsigned int addr_size_bits = 8 * addr_size;
2552 int bits_big_endian = gdbarch_bits_big_endian (arch);
2554 offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
2555 cleanups = make_cleanup (xfree, offsets);
2557 for (i = 0; i < op_end - op_ptr; ++i)
2560 make_cleanup (VEC_cleanup (int), &dw_labels);
2561 make_cleanup (VEC_cleanup (int), &patches);
2563 /* By default we are making an address. */
2564 loc->kind = axs_lvalue_memory;
2566 while (op_ptr < op_end)
2568 enum dwarf_location_atom op = *op_ptr;
2569 unsigned long long uoffset, reg;
2573 offsets[op_ptr - base] = expr->len;
2576 /* Our basic approach to code generation is to map DWARF
2577 operations directly to AX operations. However, there are
2580 First, DWARF works on address-sized units, but AX always uses
2581 LONGEST. For most operations we simply ignore this
2582 difference; instead we generate sign extensions as needed
2583 before division and comparison operations. It would be nice
2584 to omit the sign extensions, but there is no way to determine
2585 the size of the target's LONGEST. (This code uses the size
2586 of the host LONGEST in some cases -- that is a bug but it is
2589 Second, some DWARF operations cannot be translated to AX.
2590 For these we simply fail. See
2591 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
2626 ax_const_l (expr, op - DW_OP_lit0);
2630 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
2631 op_ptr += addr_size;
2632 /* Some versions of GCC emit DW_OP_addr before
2633 DW_OP_GNU_push_tls_address. In this case the value is an
2634 index, not an address. We don't support things like
2635 branching between the address and the TLS op. */
2636 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
2637 uoffset += dwarf2_per_cu_text_offset (per_cu);
2638 ax_const_l (expr, uoffset);
2642 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
2646 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
2650 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
2654 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
2658 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
2662 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
2666 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
2670 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
2674 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
2675 ax_const_l (expr, uoffset);
2678 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2679 ax_const_l (expr, offset);
2714 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2715 loc->u.reg = translate_register (arch, op - DW_OP_reg0);
2716 loc->kind = axs_lvalue_register;
2720 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
2721 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2722 loc->u.reg = translate_register (arch, reg);
2723 loc->kind = axs_lvalue_register;
2726 case DW_OP_implicit_value:
2728 unsigned long long len;
2730 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
2731 if (op_ptr + len > op_end)
2732 error (_("DW_OP_implicit_value: too few bytes available."));
2733 if (len > sizeof (ULONGEST))
2734 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
2737 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
2740 dwarf_expr_require_composition (op_ptr, op_end,
2741 "DW_OP_implicit_value");
2743 loc->kind = axs_rvalue;
2747 case DW_OP_stack_value:
2748 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
2749 loc->kind = axs_rvalue;
2784 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2785 i = translate_register (arch, op - DW_OP_breg0);
2789 ax_const_l (expr, offset);
2790 ax_simple (expr, aop_add);
2795 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
2796 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2797 i = translate_register (arch, reg);
2801 ax_const_l (expr, offset);
2802 ax_simple (expr, aop_add);
2808 const gdb_byte *datastart;
2811 struct symbol *framefunc;
2812 LONGEST base_offset = 0;
2814 b = block_for_pc (expr->scope);
2817 error (_("No block found for address"));
2819 framefunc = block_linkage_function (b);
2822 error (_("No function found for block"));
2824 dwarf_expr_frame_base_1 (framefunc, expr->scope,
2825 &datastart, &datalen);
2827 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2828 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
2829 datastart + datalen, per_cu);
2833 ax_const_l (expr, offset);
2834 ax_simple (expr, aop_add);
2837 loc->kind = axs_lvalue_memory;
2842 ax_simple (expr, aop_dup);
2846 ax_simple (expr, aop_pop);
2851 ax_pick (expr, offset);
2855 ax_simple (expr, aop_swap);
2863 ax_simple (expr, aop_rot);
2867 case DW_OP_deref_size:
2871 if (op == DW_OP_deref_size)
2879 ax_simple (expr, aop_ref8);
2882 ax_simple (expr, aop_ref16);
2885 ax_simple (expr, aop_ref32);
2888 ax_simple (expr, aop_ref64);
2891 /* Note that get_DW_OP_name will never return
2893 error (_("Unsupported size %d in %s"),
2894 size, get_DW_OP_name (op));
2900 /* Sign extend the operand. */
2901 ax_ext (expr, addr_size_bits);
2902 ax_simple (expr, aop_dup);
2903 ax_const_l (expr, 0);
2904 ax_simple (expr, aop_less_signed);
2905 ax_simple (expr, aop_log_not);
2906 i = ax_goto (expr, aop_if_goto);
2907 /* We have to emit 0 - X. */
2908 ax_const_l (expr, 0);
2909 ax_simple (expr, aop_swap);
2910 ax_simple (expr, aop_sub);
2911 ax_label (expr, i, expr->len);
2915 /* No need to sign extend here. */
2916 ax_const_l (expr, 0);
2917 ax_simple (expr, aop_swap);
2918 ax_simple (expr, aop_sub);
2922 /* Sign extend the operand. */
2923 ax_ext (expr, addr_size_bits);
2924 ax_simple (expr, aop_bit_not);
2927 case DW_OP_plus_uconst:
2928 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
2929 /* It would be really weird to emit `DW_OP_plus_uconst 0',
2930 but we micro-optimize anyhow. */
2933 ax_const_l (expr, reg);
2934 ax_simple (expr, aop_add);
2939 ax_simple (expr, aop_bit_and);
2943 /* Sign extend the operands. */
2944 ax_ext (expr, addr_size_bits);
2945 ax_simple (expr, aop_swap);
2946 ax_ext (expr, addr_size_bits);
2947 ax_simple (expr, aop_swap);
2948 ax_simple (expr, aop_div_signed);
2952 ax_simple (expr, aop_sub);
2956 ax_simple (expr, aop_rem_unsigned);
2960 ax_simple (expr, aop_mul);
2964 ax_simple (expr, aop_bit_or);
2968 ax_simple (expr, aop_add);
2972 ax_simple (expr, aop_lsh);
2976 ax_simple (expr, aop_rsh_unsigned);
2980 ax_simple (expr, aop_rsh_signed);
2984 ax_simple (expr, aop_bit_xor);
2988 /* Sign extend the operands. */
2989 ax_ext (expr, addr_size_bits);
2990 ax_simple (expr, aop_swap);
2991 ax_ext (expr, addr_size_bits);
2992 /* Note no swap here: A <= B is !(B < A). */
2993 ax_simple (expr, aop_less_signed);
2994 ax_simple (expr, aop_log_not);
2998 /* Sign extend the operands. */
2999 ax_ext (expr, addr_size_bits);
3000 ax_simple (expr, aop_swap);
3001 ax_ext (expr, addr_size_bits);
3002 ax_simple (expr, aop_swap);
3003 /* A >= B is !(A < B). */
3004 ax_simple (expr, aop_less_signed);
3005 ax_simple (expr, aop_log_not);
3009 /* Sign extend the operands. */
3010 ax_ext (expr, addr_size_bits);
3011 ax_simple (expr, aop_swap);
3012 ax_ext (expr, addr_size_bits);
3013 /* No need for a second swap here. */
3014 ax_simple (expr, aop_equal);
3018 /* Sign extend the operands. */
3019 ax_ext (expr, addr_size_bits);
3020 ax_simple (expr, aop_swap);
3021 ax_ext (expr, addr_size_bits);
3022 ax_simple (expr, aop_swap);
3023 ax_simple (expr, aop_less_signed);
3027 /* Sign extend the operands. */
3028 ax_ext (expr, addr_size_bits);
3029 ax_simple (expr, aop_swap);
3030 ax_ext (expr, addr_size_bits);
3031 /* Note no swap here: A > B is B < A. */
3032 ax_simple (expr, aop_less_signed);
3036 /* Sign extend the operands. */
3037 ax_ext (expr, addr_size_bits);
3038 ax_simple (expr, aop_swap);
3039 ax_ext (expr, addr_size_bits);
3040 /* No need for a swap here. */
3041 ax_simple (expr, aop_equal);
3042 ax_simple (expr, aop_log_not);
3045 case DW_OP_call_frame_cfa:
3046 dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
3047 loc->kind = axs_lvalue_memory;
3050 case DW_OP_GNU_push_tls_address:
3055 offset = extract_signed_integer (op_ptr, 2, byte_order);
3057 i = ax_goto (expr, aop_goto);
3058 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3059 VEC_safe_push (int, patches, i);
3063 offset = extract_signed_integer (op_ptr, 2, byte_order);
3065 /* Zero extend the operand. */
3066 ax_zero_ext (expr, addr_size_bits);
3067 i = ax_goto (expr, aop_if_goto);
3068 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3069 VEC_safe_push (int, patches, i);
3076 case DW_OP_bit_piece:
3078 unsigned long long size, offset;
3080 if (op_ptr - 1 == previous_piece)
3081 error (_("Cannot translate empty pieces to agent expressions"));
3082 previous_piece = op_ptr - 1;
3084 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3085 if (op == DW_OP_piece)
3091 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
3093 if (bits_collected + size > 8 * sizeof (LONGEST))
3094 error (_("Expression pieces exceed word size"));
3096 /* Access the bits. */
3099 case axs_lvalue_register:
3100 ax_reg (expr, loc->u.reg);
3103 case axs_lvalue_memory:
3104 /* Offset the pointer, if needed. */
3107 ax_const_l (expr, offset / 8);
3108 ax_simple (expr, aop_add);
3111 access_memory (arch, expr, size);
3115 /* For a bits-big-endian target, shift up what we already
3116 have. For a bits-little-endian target, shift up the
3117 new data. Note that there is a potential bug here if
3118 the DWARF expression leaves multiple values on the
3120 if (bits_collected > 0)
3122 if (bits_big_endian)
3124 ax_simple (expr, aop_swap);
3125 ax_const_l (expr, size);
3126 ax_simple (expr, aop_lsh);
3127 /* We don't need a second swap here, because
3128 aop_bit_or is symmetric. */
3132 ax_const_l (expr, size);
3133 ax_simple (expr, aop_lsh);
3135 ax_simple (expr, aop_bit_or);
3138 bits_collected += size;
3139 loc->kind = axs_rvalue;
3143 case DW_OP_GNU_uninit:
3149 struct dwarf2_locexpr_baton block;
3150 int size = (op == DW_OP_call2 ? 2 : 4);
3153 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3156 offset.cu_off = uoffset;
3157 block = dwarf2_fetch_die_location_block (offset, per_cu,
3160 /* DW_OP_call_ref is currently not supported. */
3161 gdb_assert (block.per_cu == per_cu);
3163 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3164 block.data, block.data + block.size,
3169 case DW_OP_call_ref:
3177 /* Patch all the branches we emitted. */
3178 for (i = 0; i < VEC_length (int, patches); ++i)
3180 int targ = offsets[VEC_index (int, dw_labels, i)];
3182 internal_error (__FILE__, __LINE__, _("invalid label"));
3183 ax_label (expr, VEC_index (int, patches, i), targ);
3186 do_cleanups (cleanups);
3190 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3191 evaluator to calculate the location. */
3192 static struct value *
3193 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3195 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3198 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3199 dlbaton->size, dlbaton->per_cu);
3204 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3205 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3208 static struct value *
3209 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3211 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3213 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3217 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3219 locexpr_read_needs_frame (struct symbol *symbol)
3221 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3223 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
3227 /* Return true if DATA points to the end of a piece. END is one past
3228 the last byte in the expression. */
3231 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3233 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3236 /* Helper for locexpr_describe_location_piece that finds the name of a
3240 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3244 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
3245 return gdbarch_register_name (gdbarch, regnum);
3248 /* Nicely describe a single piece of a location, returning an updated
3249 position in the bytecode sequence. This function cannot recognize
3250 all locations; if a location is not recognized, it simply returns
3251 DATA. If there is an error during reading, e.g. we run off the end
3252 of the buffer, an error is thrown. */
3254 static const gdb_byte *
3255 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3256 CORE_ADDR addr, struct objfile *objfile,
3257 const gdb_byte *data, const gdb_byte *end,
3258 unsigned int addr_size)
3260 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3262 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3264 fprintf_filtered (stream, _("a variable in $%s"),
3265 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3268 else if (data[0] == DW_OP_regx)
3270 unsigned long long reg;
3272 data = safe_read_uleb128 (data + 1, end, ®);
3273 fprintf_filtered (stream, _("a variable in $%s"),
3274 locexpr_regname (gdbarch, reg));
3276 else if (data[0] == DW_OP_fbreg)
3279 struct symbol *framefunc;
3281 long long frame_offset;
3282 const gdb_byte *base_data, *new_data, *save_data = data;
3284 long long base_offset = 0;
3286 new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3287 if (!piece_end_p (new_data, end))
3291 b = block_for_pc (addr);
3294 error (_("No block found for address for symbol \"%s\"."),
3295 SYMBOL_PRINT_NAME (symbol));
3297 framefunc = block_linkage_function (b);
3300 error (_("No function found for block for symbol \"%s\"."),
3301 SYMBOL_PRINT_NAME (symbol));
3303 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
3305 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3307 const gdb_byte *buf_end;
3309 frame_reg = base_data[0] - DW_OP_breg0;
3310 buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3312 if (buf_end != base_data + base_size)
3313 error (_("Unexpected opcode after "
3314 "DW_OP_breg%u for symbol \"%s\"."),
3315 frame_reg, SYMBOL_PRINT_NAME (symbol));
3317 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3319 /* The frame base is just the register, with no offset. */
3320 frame_reg = base_data[0] - DW_OP_reg0;
3325 /* We don't know what to do with the frame base expression,
3326 so we can't trace this variable; give up. */
3330 fprintf_filtered (stream,
3331 _("a variable at frame base reg $%s offset %s+%s"),
3332 locexpr_regname (gdbarch, frame_reg),
3333 plongest (base_offset), plongest (frame_offset));
3335 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3336 && piece_end_p (data, end))
3340 data = safe_read_sleb128 (data + 1, end, &offset);
3342 fprintf_filtered (stream,
3343 _("a variable at offset %s from base reg $%s"),
3345 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3348 /* The location expression for a TLS variable looks like this (on a
3351 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3352 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3354 0x3 is the encoding for DW_OP_addr, which has an operand as long
3355 as the size of an address on the target machine (here is 8
3356 bytes). Note that more recent version of GCC emit DW_OP_const4u
3357 or DW_OP_const8u, depending on address size, rather than
3358 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3359 The operand represents the offset at which the variable is within
3360 the thread local storage. */
3362 else if (data + 1 + addr_size < end
3363 && (data[0] == DW_OP_addr
3364 || (addr_size == 4 && data[0] == DW_OP_const4u)
3365 || (addr_size == 8 && data[0] == DW_OP_const8u))
3366 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
3367 && piece_end_p (data + 2 + addr_size, end))
3370 offset = extract_unsigned_integer (data + 1, addr_size,
3371 gdbarch_byte_order (gdbarch));
3373 fprintf_filtered (stream,
3374 _("a thread-local variable at offset 0x%s "
3375 "in the thread-local storage for `%s'"),
3376 phex_nz (offset, addr_size), objfile->name);
3378 data += 1 + addr_size + 1;
3380 else if (data[0] >= DW_OP_lit0
3381 && data[0] <= DW_OP_lit31
3383 && data[1] == DW_OP_stack_value)
3385 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3392 /* Disassemble an expression, stopping at the end of a piece or at the
3393 end of the expression. Returns a pointer to the next unread byte
3394 in the input expression. If ALL is nonzero, then this function
3395 will keep going until it reaches the end of the expression.
3396 If there is an error during reading, e.g. we run off the end
3397 of the buffer, an error is thrown. */
3399 static const gdb_byte *
3400 disassemble_dwarf_expression (struct ui_file *stream,
3401 struct gdbarch *arch, unsigned int addr_size,
3402 int offset_size, const gdb_byte *start,
3403 const gdb_byte *data, const gdb_byte *end,
3404 int indent, int all,
3405 struct dwarf2_per_cu_data *per_cu)
3409 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3411 enum dwarf_location_atom op = *data++;
3412 unsigned long long ul;
3416 name = get_DW_OP_name (op);
3419 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3420 op, (long) (data - 1 - start));
3421 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3422 (long) (data - 1 - start), name);
3427 ul = extract_unsigned_integer (data, addr_size,
3428 gdbarch_byte_order (arch));
3430 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3434 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3436 fprintf_filtered (stream, " %s", pulongest (ul));
3439 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3441 fprintf_filtered (stream, " %s", plongest (l));
3444 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3446 fprintf_filtered (stream, " %s", pulongest (ul));
3449 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3451 fprintf_filtered (stream, " %s", plongest (l));
3454 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3456 fprintf_filtered (stream, " %s", pulongest (ul));
3459 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3461 fprintf_filtered (stream, " %s", plongest (l));
3464 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3466 fprintf_filtered (stream, " %s", pulongest (ul));
3469 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3471 fprintf_filtered (stream, " %s", plongest (l));
3474 data = safe_read_uleb128 (data, end, &ul);
3475 fprintf_filtered (stream, " %s", pulongest (ul));
3478 data = safe_read_sleb128 (data, end, &l);
3479 fprintf_filtered (stream, " %s", plongest (l));
3514 fprintf_filtered (stream, " [$%s]",
3515 locexpr_regname (arch, op - DW_OP_reg0));
3519 data = safe_read_uleb128 (data, end, &ul);
3520 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
3521 locexpr_regname (arch, (int) ul));
3524 case DW_OP_implicit_value:
3525 data = safe_read_uleb128 (data, end, &ul);
3527 fprintf_filtered (stream, " %s", pulongest (ul));
3562 data = safe_read_sleb128 (data, end, &l);
3563 fprintf_filtered (stream, " %s [$%s]", plongest (l),
3564 locexpr_regname (arch, op - DW_OP_breg0));
3568 data = safe_read_uleb128 (data, end, &ul);
3569 data = safe_read_sleb128 (data, end, &l);
3570 fprintf_filtered (stream, " register %s [$%s] offset %s",
3572 locexpr_regname (arch, (int) ul),
3577 data = safe_read_sleb128 (data, end, &l);
3578 fprintf_filtered (stream, " %s", plongest (l));
3581 case DW_OP_xderef_size:
3582 case DW_OP_deref_size:
3584 fprintf_filtered (stream, " %d", *data);
3588 case DW_OP_plus_uconst:
3589 data = safe_read_uleb128 (data, end, &ul);
3590 fprintf_filtered (stream, " %s", pulongest (ul));
3594 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3596 fprintf_filtered (stream, " to %ld",
3597 (long) (data + l - start));
3601 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3603 fprintf_filtered (stream, " %ld",
3604 (long) (data + l - start));
3608 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3610 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
3614 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3616 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3619 case DW_OP_call_ref:
3620 ul = extract_unsigned_integer (data, offset_size,
3621 gdbarch_byte_order (arch));
3622 data += offset_size;
3623 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
3627 data = safe_read_uleb128 (data, end, &ul);
3628 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
3631 case DW_OP_bit_piece:
3633 unsigned long long offset;
3635 data = safe_read_uleb128 (data, end, &ul);
3636 data = safe_read_uleb128 (data, end, &offset);
3637 fprintf_filtered (stream, " size %s offset %s (bits)",
3638 pulongest (ul), pulongest (offset));
3642 case DW_OP_GNU_implicit_pointer:
3644 ul = extract_unsigned_integer (data, offset_size,
3645 gdbarch_byte_order (arch));
3646 data += offset_size;
3648 data = safe_read_sleb128 (data, end, &l);
3650 fprintf_filtered (stream, " DIE %s offset %s",
3651 phex_nz (ul, offset_size),
3656 case DW_OP_GNU_deref_type:
3658 int addr_size = *data++;
3662 data = safe_read_uleb128 (data, end, &ul);
3664 type = dwarf2_get_die_type (offset, per_cu);
3665 fprintf_filtered (stream, "<");
3666 type_print (type, "", stream, -1);
3667 fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
3672 case DW_OP_GNU_const_type:
3677 data = safe_read_uleb128 (data, end, &ul);
3678 type_die.cu_off = ul;
3679 type = dwarf2_get_die_type (type_die, per_cu);
3680 fprintf_filtered (stream, "<");
3681 type_print (type, "", stream, -1);
3682 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3686 case DW_OP_GNU_regval_type:
3688 unsigned long long reg;
3692 data = safe_read_uleb128 (data, end, ®);
3693 data = safe_read_uleb128 (data, end, &ul);
3694 type_die.cu_off = ul;
3696 type = dwarf2_get_die_type (type_die, per_cu);
3697 fprintf_filtered (stream, "<");
3698 type_print (type, "", stream, -1);
3699 fprintf_filtered (stream, " [0x%s]> [$%s]",
3700 phex_nz (type_die.cu_off, 0),
3701 locexpr_regname (arch, reg));
3705 case DW_OP_GNU_convert:
3706 case DW_OP_GNU_reinterpret:
3710 data = safe_read_uleb128 (data, end, &ul);
3711 type_die.cu_off = ul;
3713 if (type_die.cu_off == 0)
3714 fprintf_filtered (stream, "<0>");
3719 type = dwarf2_get_die_type (type_die, per_cu);
3720 fprintf_filtered (stream, "<");
3721 type_print (type, "", stream, -1);
3722 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3727 case DW_OP_GNU_entry_value:
3728 data = safe_read_uleb128 (data, end, &ul);
3729 fputc_filtered ('\n', stream);
3730 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
3731 start, data, data + ul, indent + 2,
3737 fprintf_filtered (stream, "\n");
3743 /* Describe a single location, which may in turn consist of multiple
3747 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
3748 struct ui_file *stream,
3749 const gdb_byte *data, int size,
3750 struct objfile *objfile, unsigned int addr_size,
3751 int offset_size, struct dwarf2_per_cu_data *per_cu)
3753 const gdb_byte *end = data + size;
3754 int first_piece = 1, bad = 0;
3758 const gdb_byte *here = data;
3759 int disassemble = 1;
3764 fprintf_filtered (stream, _(", and "));
3766 if (!dwarf2_always_disassemble)
3768 data = locexpr_describe_location_piece (symbol, stream,
3770 data, end, addr_size);
3771 /* If we printed anything, or if we have an empty piece,
3772 then don't disassemble. */
3774 || data[0] == DW_OP_piece
3775 || data[0] == DW_OP_bit_piece)
3780 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
3781 data = disassemble_dwarf_expression (stream,
3782 get_objfile_arch (objfile),
3783 addr_size, offset_size, data,
3785 dwarf2_always_disassemble,
3791 int empty = data == here;
3794 fprintf_filtered (stream, " ");
3795 if (data[0] == DW_OP_piece)
3797 unsigned long long bytes;
3799 data = safe_read_uleb128 (data + 1, end, &bytes);
3802 fprintf_filtered (stream, _("an empty %s-byte piece"),
3805 fprintf_filtered (stream, _(" [%s-byte piece]"),
3808 else if (data[0] == DW_OP_bit_piece)
3810 unsigned long long bits, offset;
3812 data = safe_read_uleb128 (data + 1, end, &bits);
3813 data = safe_read_uleb128 (data, end, &offset);
3816 fprintf_filtered (stream,
3817 _("an empty %s-bit piece"),
3820 fprintf_filtered (stream,
3821 _(" [%s-bit piece, offset %s bits]"),
3822 pulongest (bits), pulongest (offset));
3832 if (bad || data > end)
3833 error (_("Corrupted DWARF2 expression for \"%s\"."),
3834 SYMBOL_PRINT_NAME (symbol));
3837 /* Print a natural-language description of SYMBOL to STREAM. This
3838 version is for a symbol with a single location. */
3841 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
3842 struct ui_file *stream)
3844 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3845 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3846 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3847 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
3849 locexpr_describe_location_1 (symbol, addr, stream,
3850 dlbaton->data, dlbaton->size,
3851 objfile, addr_size, offset_size,
3855 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3856 any necessary bytecode in AX. */
3859 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
3860 struct agent_expr *ax, struct axs_value *value)
3862 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3863 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3865 if (dlbaton->size == 0)
3866 value->optimized_out = 1;
3868 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
3869 dlbaton->data, dlbaton->data + dlbaton->size,
3873 /* The set of location functions used with the DWARF-2 expression
3875 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
3876 locexpr_read_variable,
3877 locexpr_read_variable_at_entry,
3878 locexpr_read_needs_frame,
3879 locexpr_describe_location,
3880 locexpr_tracepoint_var_ref
3884 /* Wrapper functions for location lists. These generally find
3885 the appropriate location expression and call something above. */
3887 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3888 evaluator to calculate the location. */
3889 static struct value *
3890 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
3892 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3894 const gdb_byte *data;
3896 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
3898 data = dwarf2_find_location_expression (dlbaton, &size, pc);
3899 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
3905 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
3906 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3909 Function always returns non-NULL value, it may be marked optimized out if
3910 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
3911 if it cannot resolve the parameter for any reason. */
3913 static struct value *
3914 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3916 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3917 const gdb_byte *data;
3921 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
3922 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
3924 data = dwarf2_find_location_expression (dlbaton, &size, pc);
3926 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
3928 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
3931 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3933 loclist_read_needs_frame (struct symbol *symbol)
3935 /* If there's a location list, then assume we need to have a frame
3936 to choose the appropriate location expression. With tracking of
3937 global variables this is not necessarily true, but such tracking
3938 is disabled in GCC at the moment until we figure out how to
3944 /* Print a natural-language description of SYMBOL to STREAM. This
3945 version applies when there is a list of different locations, each
3946 with a specified address range. */
3949 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
3950 struct ui_file *stream)
3952 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3953 const gdb_byte *loc_ptr, *buf_end;
3955 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3956 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3957 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3958 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3959 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
3960 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
3961 /* Adjust base_address for relocatable objects. */
3962 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
3963 CORE_ADDR base_address = dlbaton->base_address + base_offset;
3966 loc_ptr = dlbaton->data;
3967 buf_end = dlbaton->data + dlbaton->size;
3969 fprintf_filtered (stream, _("multi-location:\n"));
3971 /* Iterate through locations until we run out. */
3974 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
3976 enum debug_loc_kind kind;
3977 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
3979 if (dlbaton->from_dwo)
3980 kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
3981 loc_ptr, buf_end, &new_ptr,
3984 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
3986 byte_order, addr_size,
3991 case DEBUG_LOC_END_OF_LIST:
3994 case DEBUG_LOC_BASE_ADDRESS:
3995 base_address = high + base_offset;
3996 fprintf_filtered (stream, _(" Base address %s"),
3997 paddress (gdbarch, base_address));
3999 case DEBUG_LOC_NORMAL:
4001 case DEBUG_LOC_BUFFER_OVERFLOW:
4002 case DEBUG_LOC_INVALID_ENTRY:
4003 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4004 SYMBOL_PRINT_NAME (symbol));
4006 gdb_assert_not_reached ("bad debug_loc_kind");
4009 /* Otherwise, a location expression entry. */
4010 low += base_address;
4011 high += base_address;
4013 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4016 /* (It would improve readability to print only the minimum
4017 necessary digits of the second number of the range.) */
4018 fprintf_filtered (stream, _(" Range %s-%s: "),
4019 paddress (gdbarch, low), paddress (gdbarch, high));
4021 /* Now describe this particular location. */
4022 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4023 objfile, addr_size, offset_size,
4026 fprintf_filtered (stream, "\n");
4032 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4033 any necessary bytecode in AX. */
4035 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4036 struct agent_expr *ax, struct axs_value *value)
4038 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4039 const gdb_byte *data;
4041 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4043 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4045 value->optimized_out = 1;
4047 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
4051 /* The set of location functions used with the DWARF-2 expression
4052 evaluator and location lists. */
4053 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4054 loclist_read_variable,
4055 loclist_read_variable_at_entry,
4056 loclist_read_needs_frame,
4057 loclist_describe_location,
4058 loclist_tracepoint_var_ref
4061 /* Provide a prototype to silence -Wmissing-prototypes. */
4062 extern initialize_file_ftype _initialize_dwarf2loc;
4065 _initialize_dwarf2loc (void)
4067 add_setshow_zinteger_cmd ("entry-values", class_maintenance,
4068 &entry_values_debug,
4069 _("Set entry values and tail call frames "
4071 _("Show entry values and tail call frames "
4073 _("When non-zero, the process of determining "
4074 "parameter values from function entry point "
4075 "and tail call frames will be printed."),
4077 show_entry_values_debug,
4078 &setdebuglist, &showdebuglist);