1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003-2013 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. */
78 DEBUG_LOC_START_END = 2,
80 /* This is followed by an unsigned LEB128 number that is an index into
81 .debug_addr and specifies the beginning address, and a 4 byte unsigned
82 number that specifies the length, and then a normal location expression
84 DEBUG_LOC_START_LENGTH = 3,
86 /* An internal value indicating there is insufficient data. */
87 DEBUG_LOC_BUFFER_OVERFLOW = -1,
89 /* An internal value indicating an invalid kind of entry was found. */
90 DEBUG_LOC_INVALID_ENTRY = -2
93 /* Decode the addresses in a non-dwo .debug_loc entry.
94 A pointer to the next byte to examine is returned in *NEW_PTR.
95 The encoded low,high addresses are return in *LOW,*HIGH.
96 The result indicates the kind of entry found. */
98 static enum debug_loc_kind
99 decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
100 const gdb_byte **new_ptr,
101 CORE_ADDR *low, CORE_ADDR *high,
102 enum bfd_endian byte_order,
103 unsigned int addr_size,
106 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
108 if (buf_end - loc_ptr < 2 * addr_size)
109 return DEBUG_LOC_BUFFER_OVERFLOW;
112 *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
114 *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
115 loc_ptr += addr_size;
118 *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
120 *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
121 loc_ptr += addr_size;
125 /* A base-address-selection entry. */
126 if ((*low & base_mask) == base_mask)
127 return DEBUG_LOC_BASE_ADDRESS;
129 /* An end-of-list entry. */
130 if (*low == 0 && *high == 0)
131 return DEBUG_LOC_END_OF_LIST;
133 return DEBUG_LOC_START_END;
136 /* Decode the addresses in .debug_loc.dwo entry.
137 A pointer to the next byte to examine is returned in *NEW_PTR.
138 The encoded low,high addresses are return in *LOW,*HIGH.
139 The result indicates the kind of entry found. */
141 static enum debug_loc_kind
142 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
143 const gdb_byte *loc_ptr,
144 const gdb_byte *buf_end,
145 const gdb_byte **new_ptr,
146 CORE_ADDR *low, CORE_ADDR *high,
147 enum bfd_endian byte_order)
149 uint64_t low_index, high_index;
151 if (loc_ptr == buf_end)
152 return DEBUG_LOC_BUFFER_OVERFLOW;
156 case DEBUG_LOC_END_OF_LIST:
158 return DEBUG_LOC_END_OF_LIST;
159 case DEBUG_LOC_BASE_ADDRESS:
161 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
163 return DEBUG_LOC_BUFFER_OVERFLOW;
164 *high = dwarf2_read_addr_index (per_cu, high_index);
166 return DEBUG_LOC_BASE_ADDRESS;
167 case DEBUG_LOC_START_END:
168 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
170 return DEBUG_LOC_BUFFER_OVERFLOW;
171 *low = dwarf2_read_addr_index (per_cu, low_index);
172 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
174 return DEBUG_LOC_BUFFER_OVERFLOW;
175 *high = dwarf2_read_addr_index (per_cu, high_index);
177 return DEBUG_LOC_START_END;
178 case DEBUG_LOC_START_LENGTH:
179 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
181 return DEBUG_LOC_BUFFER_OVERFLOW;
182 *low = dwarf2_read_addr_index (per_cu, low_index);
183 if (loc_ptr + 4 > buf_end)
184 return DEBUG_LOC_BUFFER_OVERFLOW;
186 *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
187 *new_ptr = loc_ptr + 4;
188 return DEBUG_LOC_START_LENGTH;
190 return DEBUG_LOC_INVALID_ENTRY;
194 /* A function for dealing with location lists. Given a
195 symbol baton (BATON) and a pc value (PC), find the appropriate
196 location expression, set *LOCEXPR_LENGTH, and return a pointer
197 to the beginning of the expression. Returns NULL on failure.
199 For now, only return the first matching location expression; there
200 can be more than one in the list. */
203 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
204 size_t *locexpr_length, CORE_ADDR pc)
206 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
207 struct gdbarch *gdbarch = get_objfile_arch (objfile);
208 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
209 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
210 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
211 /* Adjust base_address for relocatable objects. */
212 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
213 CORE_ADDR base_address = baton->base_address + base_offset;
214 const gdb_byte *loc_ptr, *buf_end;
216 loc_ptr = baton->data;
217 buf_end = baton->data + baton->size;
221 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
223 enum debug_loc_kind kind;
224 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
227 kind = decode_debug_loc_dwo_addresses (baton->per_cu,
228 loc_ptr, buf_end, &new_ptr,
229 &low, &high, byte_order);
231 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
233 byte_order, addr_size,
238 case DEBUG_LOC_END_OF_LIST:
241 case DEBUG_LOC_BASE_ADDRESS:
242 base_address = high + base_offset;
244 case DEBUG_LOC_START_END:
245 case DEBUG_LOC_START_LENGTH:
247 case DEBUG_LOC_BUFFER_OVERFLOW:
248 case DEBUG_LOC_INVALID_ENTRY:
249 error (_("dwarf2_find_location_expression: "
250 "Corrupted DWARF expression."));
252 gdb_assert_not_reached ("bad debug_loc_kind");
255 /* Otherwise, a location expression entry.
256 If the entry is from a DWO, don't add base address: the entry is
257 from .debug_addr which has absolute addresses. */
258 if (! baton->from_dwo)
261 high += base_address;
264 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
267 if (low == high && pc == low)
269 /* This is entry PC record present only at entry point
270 of a function. Verify it is really the function entry point. */
272 struct block *pc_block = block_for_pc (pc);
273 struct symbol *pc_func = NULL;
276 pc_func = block_linkage_function (pc_block);
278 if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
280 *locexpr_length = length;
285 if (pc >= low && pc < high)
287 *locexpr_length = length;
295 /* This is the baton used when performing dwarf2 expression
297 struct dwarf_expr_baton
299 struct frame_info *frame;
300 struct dwarf2_per_cu_data *per_cu;
303 /* Helper functions for dwarf2_evaluate_loc_desc. */
305 /* Using the frame specified in BATON, return the value of register
306 REGNUM, treated as a pointer. */
308 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
310 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
311 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
315 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
316 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
317 regnum, debaton->frame);
321 /* Read memory at ADDR (length LEN) into BUF. */
324 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
326 read_memory (addr, buf, len);
329 /* Using the frame specified in BATON, find the location expression
330 describing the frame base. Return a pointer to it in START and
331 its length in LENGTH. */
333 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
335 /* FIXME: cagney/2003-03-26: This code should be using
336 get_frame_base_address(), and then implement a dwarf2 specific
338 struct symbol *framefunc;
339 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
340 struct block *bl = get_frame_block (debaton->frame, NULL);
343 error (_("frame address is not available."));
345 /* Use block_linkage_function, which returns a real (not inlined)
346 function, instead of get_frame_function, which may return an
348 framefunc = block_linkage_function (bl);
350 /* If we found a frame-relative symbol then it was certainly within
351 some function associated with a frame. If we can't find the frame,
352 something has gone wrong. */
353 gdb_assert (framefunc != NULL);
355 dwarf_expr_frame_base_1 (framefunc,
356 get_frame_address_in_block (debaton->frame),
361 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
362 const gdb_byte **start, size_t *length)
364 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
366 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
368 struct dwarf2_loclist_baton *symbaton;
370 symbaton = SYMBOL_LOCATION_BATON (framefunc);
371 *start = dwarf2_find_location_expression (symbaton, length, pc);
375 struct dwarf2_locexpr_baton *symbaton;
377 symbaton = SYMBOL_LOCATION_BATON (framefunc);
378 if (symbaton != NULL)
380 *length = symbaton->size;
381 *start = symbaton->data;
388 error (_("Could not find the frame base for \"%s\"."),
389 SYMBOL_NATURAL_NAME (framefunc));
392 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
393 the frame in BATON. */
396 dwarf_expr_frame_cfa (void *baton)
398 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
400 return dwarf2_frame_cfa (debaton->frame);
403 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
404 the frame in BATON. */
407 dwarf_expr_frame_pc (void *baton)
409 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
411 return get_frame_address_in_block (debaton->frame);
414 /* Using the objfile specified in BATON, find the address for the
415 current thread's thread-local storage with offset OFFSET. */
417 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
419 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
420 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
422 return target_translate_tls_address (objfile, offset);
425 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
426 current CU (as is PER_CU). State of the CTX is not affected by the
430 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
431 struct dwarf2_per_cu_data *per_cu,
432 CORE_ADDR (*get_frame_pc) (void *baton),
435 struct dwarf2_locexpr_baton block;
437 block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu, get_frame_pc, baton);
439 /* DW_OP_call_ref is currently not supported. */
440 gdb_assert (block.per_cu == per_cu);
442 dwarf_expr_eval (ctx, block.data, block.size);
445 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
448 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
450 struct dwarf_expr_baton *debaton = ctx->baton;
452 per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
453 ctx->funcs->get_frame_pc, ctx->baton);
456 /* Callback function for dwarf2_evaluate_loc_desc. */
459 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
460 cu_offset die_offset)
462 struct dwarf_expr_baton *debaton = ctx->baton;
464 return dwarf2_get_die_type (die_offset, debaton->per_cu);
467 /* See dwarf2loc.h. */
469 unsigned int entry_values_debug = 0;
471 /* Helper to set entry_values_debug. */
474 show_entry_values_debug (struct ui_file *file, int from_tty,
475 struct cmd_list_element *c, const char *value)
477 fprintf_filtered (file,
478 _("Entry values and tail call frames debugging is %s.\n"),
482 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
483 CALLER_FRAME (for registers) can be NULL if it is not known. This function
484 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
487 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
488 struct call_site *call_site,
489 struct frame_info *caller_frame)
491 switch (FIELD_LOC_KIND (call_site->target))
493 case FIELD_LOC_KIND_DWARF_BLOCK:
495 struct dwarf2_locexpr_baton *dwarf_block;
497 struct type *caller_core_addr_type;
498 struct gdbarch *caller_arch;
500 dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
501 if (dwarf_block == NULL)
503 struct minimal_symbol *msym;
505 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
506 throw_error (NO_ENTRY_VALUE_ERROR,
507 _("DW_AT_GNU_call_site_target is not specified "
509 paddress (call_site_gdbarch, call_site->pc),
510 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
513 if (caller_frame == NULL)
515 struct minimal_symbol *msym;
517 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
518 throw_error (NO_ENTRY_VALUE_ERROR,
519 _("DW_AT_GNU_call_site_target DWARF block resolving "
520 "requires known frame which is currently not "
521 "available at %s in %s"),
522 paddress (call_site_gdbarch, call_site->pc),
523 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
526 caller_arch = get_frame_arch (caller_frame);
527 caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
528 val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
529 dwarf_block->data, dwarf_block->size,
530 dwarf_block->per_cu);
531 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
533 if (VALUE_LVAL (val) == lval_memory)
534 return value_address (val);
536 return value_as_address (val);
539 case FIELD_LOC_KIND_PHYSNAME:
541 const char *physname;
542 struct minimal_symbol *msym;
544 physname = FIELD_STATIC_PHYSNAME (call_site->target);
545 msym = lookup_minimal_symbol_text (physname, NULL);
548 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
549 throw_error (NO_ENTRY_VALUE_ERROR,
550 _("Cannot find function \"%s\" for a call site target "
552 physname, paddress (call_site_gdbarch, call_site->pc),
553 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
556 return SYMBOL_VALUE_ADDRESS (msym);
559 case FIELD_LOC_KIND_PHYSADDR:
560 return FIELD_STATIC_PHYSADDR (call_site->target);
563 internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
567 /* Convert function entry point exact address ADDR to the function which is
568 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
569 NO_ENTRY_VALUE_ERROR otherwise. */
571 static struct symbol *
572 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
574 struct symbol *sym = find_pc_function (addr);
577 if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
578 throw_error (NO_ENTRY_VALUE_ERROR,
579 _("DW_TAG_GNU_call_site resolving failed to find function "
580 "name for address %s"),
581 paddress (gdbarch, addr));
583 type = SYMBOL_TYPE (sym);
584 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
585 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
590 /* Verify function with entry point exact address ADDR can never call itself
591 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
592 can call itself via tail calls.
594 If a funtion can tail call itself its entry value based parameters are
595 unreliable. There is no verification whether the value of some/all
596 parameters is unchanged through the self tail call, we expect if there is
597 a self tail call all the parameters can be modified. */
600 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
602 struct obstack addr_obstack;
603 struct cleanup *old_chain;
606 /* Track here CORE_ADDRs which were already visited. */
609 /* The verification is completely unordered. Track here function addresses
610 which still need to be iterated. */
611 VEC (CORE_ADDR) *todo = NULL;
613 obstack_init (&addr_obstack);
614 old_chain = make_cleanup_obstack_free (&addr_obstack);
615 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
616 &addr_obstack, hashtab_obstack_allocate,
618 make_cleanup_htab_delete (addr_hash);
620 make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
622 VEC_safe_push (CORE_ADDR, todo, verify_addr);
623 while (!VEC_empty (CORE_ADDR, todo))
625 struct symbol *func_sym;
626 struct call_site *call_site;
628 addr = VEC_pop (CORE_ADDR, todo);
630 func_sym = func_addr_to_tail_call_list (gdbarch, addr);
632 for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
633 call_site; call_site = call_site->tail_call_next)
635 CORE_ADDR target_addr;
638 /* CALLER_FRAME with registers is not available for tail-call jumped
640 target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
642 if (target_addr == verify_addr)
644 struct minimal_symbol *msym;
646 msym = lookup_minimal_symbol_by_pc (verify_addr);
647 throw_error (NO_ENTRY_VALUE_ERROR,
648 _("DW_OP_GNU_entry_value resolving has found "
649 "function \"%s\" at %s can call itself via tail "
651 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym),
652 paddress (gdbarch, verify_addr));
655 slot = htab_find_slot (addr_hash, &target_addr, INSERT);
658 *slot = obstack_copy (&addr_obstack, &target_addr,
659 sizeof (target_addr));
660 VEC_safe_push (CORE_ADDR, todo, target_addr);
665 do_cleanups (old_chain);
668 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
669 ENTRY_VALUES_DEBUG. */
672 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
674 CORE_ADDR addr = call_site->pc;
675 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (addr - 1);
677 fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
678 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
682 /* vec.h needs single word type name, typedef it. */
683 typedef struct call_site *call_sitep;
685 /* Define VEC (call_sitep) functions. */
686 DEF_VEC_P (call_sitep);
688 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
689 only top callers and bottom callees which are present in both. GDBARCH is
690 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
691 no remaining possibilities to provide unambiguous non-trivial result.
692 RESULTP should point to NULL on the first (initialization) call. Caller is
693 responsible for xfree of any RESULTP data. */
696 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
697 VEC (call_sitep) *chain)
699 struct call_site_chain *result = *resultp;
700 long length = VEC_length (call_sitep, chain);
701 int callers, callees, idx;
705 /* Create the initial chain containing all the passed PCs. */
707 result = xmalloc (sizeof (*result) + sizeof (*result->call_site)
709 result->length = length;
710 result->callers = result->callees = length;
711 memcpy (result->call_site, VEC_address (call_sitep, chain),
712 sizeof (*result->call_site) * length);
715 if (entry_values_debug)
717 fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
718 for (idx = 0; idx < length; idx++)
719 tailcall_dump (gdbarch, result->call_site[idx]);
720 fputc_unfiltered ('\n', gdb_stdlog);
726 if (entry_values_debug)
728 fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
729 for (idx = 0; idx < length; idx++)
730 tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
731 fputc_unfiltered ('\n', gdb_stdlog);
734 /* Intersect callers. */
736 callers = min (result->callers, length);
737 for (idx = 0; idx < callers; idx++)
738 if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
740 result->callers = idx;
744 /* Intersect callees. */
746 callees = min (result->callees, length);
747 for (idx = 0; idx < callees; idx++)
748 if (result->call_site[result->length - 1 - idx]
749 != VEC_index (call_sitep, chain, length - 1 - idx))
751 result->callees = idx;
755 if (entry_values_debug)
757 fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
758 for (idx = 0; idx < result->callers; idx++)
759 tailcall_dump (gdbarch, result->call_site[idx]);
760 fputs_unfiltered (" |", gdb_stdlog);
761 for (idx = 0; idx < result->callees; idx++)
762 tailcall_dump (gdbarch, result->call_site[result->length
763 - result->callees + idx]);
764 fputc_unfiltered ('\n', gdb_stdlog);
767 if (result->callers == 0 && result->callees == 0)
769 /* There are no common callers or callees. It could be also a direct
770 call (which has length 0) with ambiguous possibility of an indirect
771 call - CALLERS == CALLEES == 0 is valid during the first allocation
772 but any subsequence processing of such entry means ambiguity. */
778 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
779 PC again. In such case there must be two different code paths to reach
780 it, therefore some of the former determined intermediate PCs must differ
781 and the unambiguous chain gets shortened. */
782 gdb_assert (result->callers + result->callees < result->length);
785 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
786 assumed frames between them use GDBARCH. Use depth first search so we can
787 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
788 would have needless GDB stack overhead. Caller is responsible for xfree of
789 the returned result. Any unreliability results in thrown
790 NO_ENTRY_VALUE_ERROR. */
792 static struct call_site_chain *
793 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
796 CORE_ADDR save_callee_pc = callee_pc;
797 struct obstack addr_obstack;
798 struct cleanup *back_to_retval, *back_to_workdata;
799 struct call_site_chain *retval = NULL;
800 struct call_site *call_site;
802 /* Mark CALL_SITEs so we do not visit the same ones twice. */
805 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
806 call_site nor any possible call_site at CALLEE_PC's function is there.
807 Any CALL_SITE in CHAIN will be iterated to its siblings - via
808 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
809 VEC (call_sitep) *chain = NULL;
811 /* We are not interested in the specific PC inside the callee function. */
812 callee_pc = get_pc_function_start (callee_pc);
814 throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
815 paddress (gdbarch, save_callee_pc));
817 back_to_retval = make_cleanup (free_current_contents, &retval);
819 obstack_init (&addr_obstack);
820 back_to_workdata = make_cleanup_obstack_free (&addr_obstack);
821 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
822 &addr_obstack, hashtab_obstack_allocate,
824 make_cleanup_htab_delete (addr_hash);
826 make_cleanup (VEC_cleanup (call_sitep), &chain);
828 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
829 at the target's function. All the possible tail call sites in the
830 target's function will get iterated as already pushed into CHAIN via their
832 call_site = call_site_for_pc (gdbarch, caller_pc);
836 CORE_ADDR target_func_addr;
837 struct call_site *target_call_site;
839 /* CALLER_FRAME with registers is not available for tail-call jumped
841 target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
843 if (target_func_addr == callee_pc)
845 chain_candidate (gdbarch, &retval, chain);
849 /* There is no way to reach CALLEE_PC again as we would prevent
850 entering it twice as being already marked in ADDR_HASH. */
851 target_call_site = NULL;
855 struct symbol *target_func;
857 target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
858 target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
863 /* Attempt to visit TARGET_CALL_SITE. */
865 if (target_call_site)
869 slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
872 /* Successfully entered TARGET_CALL_SITE. */
874 *slot = &target_call_site->pc;
875 VEC_safe_push (call_sitep, chain, target_call_site);
880 /* Backtrack (without revisiting the originating call_site). Try the
881 callers's sibling; if there isn't any try the callers's callers's
884 target_call_site = NULL;
885 while (!VEC_empty (call_sitep, chain))
887 call_site = VEC_pop (call_sitep, chain);
889 gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
891 htab_remove_elt (addr_hash, &call_site->pc);
893 target_call_site = call_site->tail_call_next;
894 if (target_call_site)
898 while (target_call_site);
900 if (VEC_empty (call_sitep, chain))
903 call_site = VEC_last (call_sitep, chain);
908 struct minimal_symbol *msym_caller, *msym_callee;
910 msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
911 msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
912 throw_error (NO_ENTRY_VALUE_ERROR,
913 _("There are no unambiguously determinable intermediate "
914 "callers or callees between caller function \"%s\" at %s "
915 "and callee function \"%s\" at %s"),
917 ? "???" : SYMBOL_PRINT_NAME (msym_caller)),
918 paddress (gdbarch, caller_pc),
920 ? "???" : SYMBOL_PRINT_NAME (msym_callee)),
921 paddress (gdbarch, callee_pc));
924 do_cleanups (back_to_workdata);
925 discard_cleanups (back_to_retval);
929 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
930 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
931 constructed return NULL. Caller is responsible for xfree of the returned
934 struct call_site_chain *
935 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
938 volatile struct gdb_exception e;
939 struct call_site_chain *retval = NULL;
941 TRY_CATCH (e, RETURN_MASK_ERROR)
943 retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
947 if (e.error == NO_ENTRY_VALUE_ERROR)
949 if (entry_values_debug)
950 exception_print (gdb_stdout, e);
960 /* Return 1 if KIND and KIND_U match PARAMETER. Return 0 otherwise. */
963 call_site_parameter_matches (struct call_site_parameter *parameter,
964 enum call_site_parameter_kind kind,
965 union call_site_parameter_u kind_u)
967 if (kind == parameter->kind)
970 case CALL_SITE_PARAMETER_DWARF_REG:
971 return kind_u.dwarf_reg == parameter->u.dwarf_reg;
972 case CALL_SITE_PARAMETER_FB_OFFSET:
973 return kind_u.fb_offset == parameter->u.fb_offset;
974 case CALL_SITE_PARAMETER_PARAM_OFFSET:
975 return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off;
980 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
983 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
986 static struct call_site_parameter *
987 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
988 enum call_site_parameter_kind kind,
989 union call_site_parameter_u kind_u,
990 struct dwarf2_per_cu_data **per_cu_return)
992 CORE_ADDR func_addr, caller_pc;
993 struct gdbarch *gdbarch;
994 struct frame_info *caller_frame;
995 struct call_site *call_site;
997 /* Initialize it just to avoid a GCC false warning. */
998 struct call_site_parameter *parameter = NULL;
999 CORE_ADDR target_addr;
1001 while (get_frame_type (frame) == INLINE_FRAME)
1003 frame = get_prev_frame (frame);
1004 gdb_assert (frame != NULL);
1007 func_addr = get_frame_func (frame);
1008 gdbarch = get_frame_arch (frame);
1009 caller_frame = get_prev_frame (frame);
1010 if (gdbarch != frame_unwind_arch (frame))
1012 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr);
1013 struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
1015 throw_error (NO_ENTRY_VALUE_ERROR,
1016 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
1017 "(of %s (%s)) does not match caller gdbarch %s"),
1018 gdbarch_bfd_arch_info (gdbarch)->printable_name,
1019 paddress (gdbarch, func_addr),
1020 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym),
1021 gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
1024 if (caller_frame == NULL)
1026 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr);
1028 throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
1029 "requires caller of %s (%s)"),
1030 paddress (gdbarch, func_addr),
1031 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
1033 caller_pc = get_frame_pc (caller_frame);
1034 call_site = call_site_for_pc (gdbarch, caller_pc);
1036 target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
1037 if (target_addr != func_addr)
1039 struct minimal_symbol *target_msym, *func_msym;
1041 target_msym = lookup_minimal_symbol_by_pc (target_addr);
1042 func_msym = lookup_minimal_symbol_by_pc (func_addr);
1043 throw_error (NO_ENTRY_VALUE_ERROR,
1044 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
1045 "but the called frame is for %s at %s"),
1046 (target_msym == NULL ? "???"
1047 : SYMBOL_PRINT_NAME (target_msym)),
1048 paddress (gdbarch, target_addr),
1049 func_msym == NULL ? "???" : SYMBOL_PRINT_NAME (func_msym),
1050 paddress (gdbarch, func_addr));
1053 /* No entry value based parameters would be reliable if this function can
1054 call itself via tail calls. */
1055 func_verify_no_selftailcall (gdbarch, func_addr);
1057 for (iparams = 0; iparams < call_site->parameter_count; iparams++)
1059 parameter = &call_site->parameter[iparams];
1060 if (call_site_parameter_matches (parameter, kind, kind_u))
1063 if (iparams == call_site->parameter_count)
1065 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (caller_pc);
1067 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
1068 determine its value. */
1069 throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
1070 "at DW_TAG_GNU_call_site %s at %s"),
1071 paddress (gdbarch, caller_pc),
1072 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
1075 *per_cu_return = call_site->per_cu;
1079 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
1080 the normal DW_AT_GNU_call_site_value block. Otherwise return the
1081 DW_AT_GNU_call_site_data_value (dereferenced) block.
1083 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1086 Function always returns non-NULL, non-optimized out value. It throws
1087 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
1089 static struct value *
1090 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
1091 CORE_ADDR deref_size, struct type *type,
1092 struct frame_info *caller_frame,
1093 struct dwarf2_per_cu_data *per_cu)
1095 const gdb_byte *data_src;
1099 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1100 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1102 /* DEREF_SIZE size is not verified here. */
1103 if (data_src == NULL)
1104 throw_error (NO_ENTRY_VALUE_ERROR,
1105 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1107 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
1108 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1110 data = alloca (size + 1);
1111 memcpy (data, data_src, size);
1112 data[size] = DW_OP_stack_value;
1114 return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
1117 /* Execute DWARF block of call_site_parameter which matches KIND and KIND_U.
1118 Choose DEREF_SIZE value of that parameter. Search caller of the CTX's
1119 frame. CTX must be of dwarf_expr_ctx_funcs kind.
1121 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
1122 can be more simple as it does not support cross-CU DWARF executions. */
1125 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1126 enum call_site_parameter_kind kind,
1127 union call_site_parameter_u kind_u,
1130 struct dwarf_expr_baton *debaton;
1131 struct frame_info *frame, *caller_frame;
1132 struct dwarf2_per_cu_data *caller_per_cu;
1133 struct dwarf_expr_baton baton_local;
1134 struct dwarf_expr_context saved_ctx;
1135 struct call_site_parameter *parameter;
1136 const gdb_byte *data_src;
1139 gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
1140 debaton = ctx->baton;
1141 frame = debaton->frame;
1142 caller_frame = get_prev_frame (frame);
1144 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1146 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1147 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1149 /* DEREF_SIZE size is not verified here. */
1150 if (data_src == NULL)
1151 throw_error (NO_ENTRY_VALUE_ERROR,
1152 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1154 baton_local.frame = caller_frame;
1155 baton_local.per_cu = caller_per_cu;
1157 saved_ctx.gdbarch = ctx->gdbarch;
1158 saved_ctx.addr_size = ctx->addr_size;
1159 saved_ctx.offset = ctx->offset;
1160 saved_ctx.baton = ctx->baton;
1161 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
1162 ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
1163 ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
1164 ctx->baton = &baton_local;
1166 dwarf_expr_eval (ctx, data_src, size);
1168 ctx->gdbarch = saved_ctx.gdbarch;
1169 ctx->addr_size = saved_ctx.addr_size;
1170 ctx->offset = saved_ctx.offset;
1171 ctx->baton = saved_ctx.baton;
1174 /* Callback function for dwarf2_evaluate_loc_desc.
1175 Fetch the address indexed by DW_OP_GNU_addr_index. */
1178 dwarf_expr_get_addr_index (void *baton, unsigned int index)
1180 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
1182 return dwarf2_read_addr_index (debaton->per_cu, index);
1185 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1186 the indirect method on it, that is use its stored target value, the sole
1187 purpose of entry_data_value_funcs.. */
1189 static struct value *
1190 entry_data_value_coerce_ref (const struct value *value)
1192 struct type *checked_type = check_typedef (value_type (value));
1193 struct value *target_val;
1195 if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
1198 target_val = value_computed_closure (value);
1199 value_incref (target_val);
1203 /* Implement copy_closure. */
1206 entry_data_value_copy_closure (const struct value *v)
1208 struct value *target_val = value_computed_closure (v);
1210 value_incref (target_val);
1214 /* Implement free_closure. */
1217 entry_data_value_free_closure (struct value *v)
1219 struct value *target_val = value_computed_closure (v);
1221 value_free (target_val);
1224 /* Vector for methods for an entry value reference where the referenced value
1225 is stored in the caller. On the first dereference use
1226 DW_AT_GNU_call_site_data_value in the caller. */
1228 static const struct lval_funcs entry_data_value_funcs =
1232 NULL, /* check_validity */
1233 NULL, /* check_any_valid */
1234 NULL, /* indirect */
1235 entry_data_value_coerce_ref,
1236 NULL, /* check_synthetic_pointer */
1237 entry_data_value_copy_closure,
1238 entry_data_value_free_closure
1241 /* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KIND_U
1242 are used to match DW_AT_location at the caller's
1243 DW_TAG_GNU_call_site_parameter.
1245 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1246 cannot resolve the parameter for any reason. */
1248 static struct value *
1249 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1250 enum call_site_parameter_kind kind,
1251 union call_site_parameter_u kind_u)
1253 struct type *checked_type = check_typedef (type);
1254 struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1255 struct frame_info *caller_frame = get_prev_frame (frame);
1256 struct value *outer_val, *target_val, *val;
1257 struct call_site_parameter *parameter;
1258 struct dwarf2_per_cu_data *caller_per_cu;
1261 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1264 outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1268 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1269 used and it is not available do not fall back to OUTER_VAL - dereferencing
1270 TYPE_CODE_REF with non-entry data value would give current value - not the
1273 if (TYPE_CODE (checked_type) != TYPE_CODE_REF
1274 || TYPE_TARGET_TYPE (checked_type) == NULL)
1277 target_val = dwarf_entry_parameter_to_value (parameter,
1278 TYPE_LENGTH (target_type),
1279 target_type, caller_frame,
1282 /* value_as_address dereferences TYPE_CODE_REF. */
1283 addr = extract_typed_address (value_contents (outer_val), checked_type);
1285 /* The target entry value has artificial address of the entry value
1287 VALUE_LVAL (target_val) = lval_memory;
1288 set_value_address (target_val, addr);
1290 release_value (target_val);
1291 val = allocate_computed_value (type, &entry_data_value_funcs,
1292 target_val /* closure */);
1294 /* Copy the referencing pointer to the new computed value. */
1295 memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1296 TYPE_LENGTH (checked_type));
1297 set_value_lazy (val, 0);
1302 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1303 SIZE are DWARF block used to match DW_AT_location at the caller's
1304 DW_TAG_GNU_call_site_parameter.
1306 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1307 cannot resolve the parameter for any reason. */
1309 static struct value *
1310 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1311 const gdb_byte *block, size_t block_len)
1313 union call_site_parameter_u kind_u;
1315 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1316 if (kind_u.dwarf_reg != -1)
1317 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
1320 if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
1321 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
1324 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1325 suppressed during normal operation. The expression can be arbitrary if
1326 there is no caller-callee entry value binding expected. */
1327 throw_error (NO_ENTRY_VALUE_ERROR,
1328 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1329 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1332 struct piece_closure
1334 /* Reference count. */
1337 /* The CU from which this closure's expression came. */
1338 struct dwarf2_per_cu_data *per_cu;
1340 /* The number of pieces used to describe this variable. */
1343 /* The target address size, used only for DWARF_VALUE_STACK. */
1346 /* The pieces themselves. */
1347 struct dwarf_expr_piece *pieces;
1350 /* Allocate a closure for a value formed from separately-described
1353 static struct piece_closure *
1354 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1355 int n_pieces, struct dwarf_expr_piece *pieces,
1358 struct piece_closure *c = XZALLOC (struct piece_closure);
1363 c->n_pieces = n_pieces;
1364 c->addr_size = addr_size;
1365 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
1367 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
1368 for (i = 0; i < n_pieces; ++i)
1369 if (c->pieces[i].location == DWARF_VALUE_STACK)
1370 value_incref (c->pieces[i].v.value);
1375 /* The lowest-level function to extract bits from a byte buffer.
1376 SOURCE is the buffer. It is updated if we read to the end of a
1378 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1379 updated to reflect the number of bits actually read.
1380 NBITS is the number of bits we want to read. It is updated to
1381 reflect the number of bits actually read. This function may read
1383 BITS_BIG_ENDIAN is taken directly from gdbarch.
1384 This function returns the extracted bits. */
1387 extract_bits_primitive (const gdb_byte **source,
1388 unsigned int *source_offset_bits,
1389 int *nbits, int bits_big_endian)
1391 unsigned int avail, mask, datum;
1393 gdb_assert (*source_offset_bits < 8);
1395 avail = 8 - *source_offset_bits;
1399 mask = (1 << avail) - 1;
1401 if (bits_big_endian)
1402 datum >>= 8 - (*source_offset_bits + *nbits);
1404 datum >>= *source_offset_bits;
1408 *source_offset_bits += avail;
1409 if (*source_offset_bits >= 8)
1411 *source_offset_bits -= 8;
1418 /* Extract some bits from a source buffer and move forward in the
1421 SOURCE is the source buffer. It is updated as bytes are read.
1422 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1424 NBITS is the number of bits to read.
1425 BITS_BIG_ENDIAN is taken directly from gdbarch.
1427 This function returns the bits that were read. */
1430 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
1431 int nbits, int bits_big_endian)
1435 gdb_assert (nbits > 0 && nbits <= 8);
1437 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
1443 more = extract_bits_primitive (source, source_offset_bits, &nbits,
1445 if (bits_big_endian)
1455 /* Write some bits into a buffer and move forward in the buffer.
1457 DATUM is the bits to write. The low-order bits of DATUM are used.
1458 DEST is the destination buffer. It is updated as bytes are
1460 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1462 NBITS is the number of valid bits in DATUM.
1463 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1466 insert_bits (unsigned int datum,
1467 gdb_byte *dest, unsigned int dest_offset_bits,
1468 int nbits, int bits_big_endian)
1472 gdb_assert (dest_offset_bits + nbits <= 8);
1474 mask = (1 << nbits) - 1;
1475 if (bits_big_endian)
1477 datum <<= 8 - (dest_offset_bits + nbits);
1478 mask <<= 8 - (dest_offset_bits + nbits);
1482 datum <<= dest_offset_bits;
1483 mask <<= dest_offset_bits;
1486 gdb_assert ((datum & ~mask) == 0);
1488 *dest = (*dest & ~mask) | datum;
1491 /* Copy bits from a source to a destination.
1493 DEST is where the bits should be written.
1494 DEST_OFFSET_BITS is the bit offset into DEST.
1495 SOURCE is the source of bits.
1496 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1497 BIT_COUNT is the number of bits to copy.
1498 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1501 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
1502 const gdb_byte *source, unsigned int source_offset_bits,
1503 unsigned int bit_count,
1504 int bits_big_endian)
1506 unsigned int dest_avail;
1509 /* Reduce everything to byte-size pieces. */
1510 dest += dest_offset_bits / 8;
1511 dest_offset_bits %= 8;
1512 source += source_offset_bits / 8;
1513 source_offset_bits %= 8;
1515 dest_avail = 8 - dest_offset_bits % 8;
1517 /* See if we can fill the first destination byte. */
1518 if (dest_avail < bit_count)
1520 datum = extract_bits (&source, &source_offset_bits, dest_avail,
1522 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
1524 dest_offset_bits = 0;
1525 bit_count -= dest_avail;
1528 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1529 than 8 bits remaining. */
1530 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
1531 for (; bit_count >= 8; bit_count -= 8)
1533 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
1534 *dest++ = (gdb_byte) datum;
1537 /* Finally, we may have a few leftover bits. */
1538 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
1541 datum = extract_bits (&source, &source_offset_bits, bit_count,
1543 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
1548 read_pieced_value (struct value *v)
1552 ULONGEST bits_to_skip;
1554 struct piece_closure *c
1555 = (struct piece_closure *) value_computed_closure (v);
1556 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
1558 size_t buffer_size = 0;
1559 char *buffer = NULL;
1560 struct cleanup *cleanup;
1562 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1564 if (value_type (v) != value_enclosing_type (v))
1565 internal_error (__FILE__, __LINE__,
1566 _("Should not be able to create a lazy value with "
1567 "an enclosing type"));
1569 cleanup = make_cleanup (free_current_contents, &buffer);
1571 contents = value_contents_raw (v);
1572 bits_to_skip = 8 * value_offset (v);
1573 if (value_bitsize (v))
1575 bits_to_skip += value_bitpos (v);
1576 type_len = value_bitsize (v);
1579 type_len = 8 * TYPE_LENGTH (value_type (v));
1581 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1583 struct dwarf_expr_piece *p = &c->pieces[i];
1584 size_t this_size, this_size_bits;
1585 long dest_offset_bits, source_offset_bits, source_offset;
1586 const gdb_byte *intermediate_buffer;
1588 /* Compute size, source, and destination offsets for copying, in
1590 this_size_bits = p->size;
1591 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1593 bits_to_skip -= this_size_bits;
1596 if (this_size_bits > type_len - offset)
1597 this_size_bits = type_len - offset;
1598 if (bits_to_skip > 0)
1600 dest_offset_bits = 0;
1601 source_offset_bits = bits_to_skip;
1602 this_size_bits -= bits_to_skip;
1607 dest_offset_bits = offset;
1608 source_offset_bits = 0;
1611 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1612 source_offset = source_offset_bits / 8;
1613 if (buffer_size < this_size)
1615 buffer_size = this_size;
1616 buffer = xrealloc (buffer, buffer_size);
1618 intermediate_buffer = buffer;
1620 /* Copy from the source to DEST_BUFFER. */
1621 switch (p->location)
1623 case DWARF_VALUE_REGISTER:
1625 struct gdbarch *arch = get_frame_arch (frame);
1626 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1627 int reg_offset = source_offset;
1629 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1630 && this_size < register_size (arch, gdb_regnum))
1632 /* Big-endian, and we want less than full size. */
1633 reg_offset = register_size (arch, gdb_regnum) - this_size;
1634 /* We want the lower-order THIS_SIZE_BITS of the bytes
1635 we extract from the register. */
1636 source_offset_bits += 8 * this_size - this_size_bits;
1639 if (gdb_regnum != -1)
1643 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1647 /* Just so garbage doesn't ever shine through. */
1648 memset (buffer, 0, this_size);
1651 set_value_optimized_out (v, 1);
1653 mark_value_bytes_unavailable (v, offset, this_size);
1658 error (_("Unable to access DWARF register number %s"),
1659 paddress (arch, p->v.regno));
1664 case DWARF_VALUE_MEMORY:
1665 read_value_memory (v, offset,
1666 p->v.mem.in_stack_memory,
1667 p->v.mem.addr + source_offset,
1671 case DWARF_VALUE_STACK:
1673 size_t n = this_size;
1675 if (n > c->addr_size - source_offset)
1676 n = (c->addr_size >= source_offset
1677 ? c->addr_size - source_offset
1685 const gdb_byte *val_bytes = value_contents_all (p->v.value);
1687 intermediate_buffer = val_bytes + source_offset;
1692 case DWARF_VALUE_LITERAL:
1694 size_t n = this_size;
1696 if (n > p->v.literal.length - source_offset)
1697 n = (p->v.literal.length >= source_offset
1698 ? p->v.literal.length - source_offset
1701 intermediate_buffer = p->v.literal.data + source_offset;
1705 /* These bits show up as zeros -- but do not cause the value
1706 to be considered optimized-out. */
1707 case DWARF_VALUE_IMPLICIT_POINTER:
1710 case DWARF_VALUE_OPTIMIZED_OUT:
1711 set_value_optimized_out (v, 1);
1715 internal_error (__FILE__, __LINE__, _("invalid location type"));
1718 if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1719 && p->location != DWARF_VALUE_IMPLICIT_POINTER)
1720 copy_bitwise (contents, dest_offset_bits,
1721 intermediate_buffer, source_offset_bits % 8,
1722 this_size_bits, bits_big_endian);
1724 offset += this_size_bits;
1727 do_cleanups (cleanup);
1731 write_pieced_value (struct value *to, struct value *from)
1735 ULONGEST bits_to_skip;
1736 const gdb_byte *contents;
1737 struct piece_closure *c
1738 = (struct piece_closure *) value_computed_closure (to);
1739 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
1741 size_t buffer_size = 0;
1742 char *buffer = NULL;
1743 struct cleanup *cleanup;
1745 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
1749 set_value_optimized_out (to, 1);
1753 cleanup = make_cleanup (free_current_contents, &buffer);
1755 contents = value_contents (from);
1756 bits_to_skip = 8 * value_offset (to);
1757 if (value_bitsize (to))
1759 bits_to_skip += value_bitpos (to);
1760 type_len = value_bitsize (to);
1763 type_len = 8 * TYPE_LENGTH (value_type (to));
1765 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1767 struct dwarf_expr_piece *p = &c->pieces[i];
1768 size_t this_size_bits, this_size;
1769 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1771 const gdb_byte *source_buffer;
1773 this_size_bits = p->size;
1774 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1776 bits_to_skip -= this_size_bits;
1779 if (this_size_bits > type_len - offset)
1780 this_size_bits = type_len - offset;
1781 if (bits_to_skip > 0)
1783 dest_offset_bits = bits_to_skip;
1784 source_offset_bits = 0;
1785 this_size_bits -= bits_to_skip;
1790 dest_offset_bits = 0;
1791 source_offset_bits = offset;
1794 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1795 source_offset = source_offset_bits / 8;
1796 dest_offset = dest_offset_bits / 8;
1797 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1799 source_buffer = contents + source_offset;
1804 if (buffer_size < this_size)
1806 buffer_size = this_size;
1807 buffer = xrealloc (buffer, buffer_size);
1809 source_buffer = buffer;
1813 switch (p->location)
1815 case DWARF_VALUE_REGISTER:
1817 struct gdbarch *arch = get_frame_arch (frame);
1818 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
1819 int reg_offset = dest_offset;
1821 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1822 && this_size <= register_size (arch, gdb_regnum))
1823 /* Big-endian, and we want less than full size. */
1824 reg_offset = register_size (arch, gdb_regnum) - this_size;
1826 if (gdb_regnum != -1)
1832 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1837 error (_("Can't do read-modify-write to "
1838 "update bitfield; containing word has been "
1841 throw_error (NOT_AVAILABLE_ERROR,
1842 _("Can't do read-modify-write to update "
1843 "bitfield; containing word "
1846 copy_bitwise (buffer, dest_offset_bits,
1847 contents, source_offset_bits,
1852 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
1853 this_size, source_buffer);
1857 error (_("Unable to write to DWARF register number %s"),
1858 paddress (arch, p->v.regno));
1862 case DWARF_VALUE_MEMORY:
1865 /* Only the first and last bytes can possibly have any
1867 read_memory (p->v.mem.addr + dest_offset, buffer, 1);
1868 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
1869 buffer + this_size - 1, 1);
1870 copy_bitwise (buffer, dest_offset_bits,
1871 contents, source_offset_bits,
1876 write_memory (p->v.mem.addr + dest_offset,
1877 source_buffer, this_size);
1880 set_value_optimized_out (to, 1);
1883 offset += this_size_bits;
1886 do_cleanups (cleanup);
1889 /* A helper function that checks bit validity in a pieced value.
1890 CHECK_FOR indicates the kind of validity checking.
1891 DWARF_VALUE_MEMORY means to check whether any bit is valid.
1892 DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
1894 DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
1895 implicit pointer. */
1898 check_pieced_value_bits (const struct value *value, int bit_offset,
1900 enum dwarf_value_location check_for)
1902 struct piece_closure *c
1903 = (struct piece_closure *) value_computed_closure (value);
1905 int validity = (check_for == DWARF_VALUE_MEMORY
1906 || check_for == DWARF_VALUE_IMPLICIT_POINTER);
1908 bit_offset += 8 * value_offset (value);
1909 if (value_bitsize (value))
1910 bit_offset += value_bitpos (value);
1912 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1914 struct dwarf_expr_piece *p = &c->pieces[i];
1915 size_t this_size_bits = p->size;
1919 if (bit_offset >= this_size_bits)
1921 bit_offset -= this_size_bits;
1925 bit_length -= this_size_bits - bit_offset;
1929 bit_length -= this_size_bits;
1931 if (check_for == DWARF_VALUE_IMPLICIT_POINTER)
1933 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1936 else if (p->location == DWARF_VALUE_OPTIMIZED_OUT
1937 || p->location == DWARF_VALUE_IMPLICIT_POINTER)
1953 check_pieced_value_validity (const struct value *value, int bit_offset,
1956 return check_pieced_value_bits (value, bit_offset, bit_length,
1957 DWARF_VALUE_MEMORY);
1961 check_pieced_value_invalid (const struct value *value)
1963 return check_pieced_value_bits (value, 0,
1964 8 * TYPE_LENGTH (value_type (value)),
1965 DWARF_VALUE_OPTIMIZED_OUT);
1968 /* An implementation of an lval_funcs method to see whether a value is
1969 a synthetic pointer. */
1972 check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
1975 return check_pieced_value_bits (value, bit_offset, bit_length,
1976 DWARF_VALUE_IMPLICIT_POINTER);
1979 /* A wrapper function for get_frame_address_in_block. */
1982 get_frame_address_in_block_wrapper (void *baton)
1984 return get_frame_address_in_block (baton);
1987 /* An implementation of an lval_funcs method to indirect through a
1988 pointer. This handles the synthetic pointer case when needed. */
1990 static struct value *
1991 indirect_pieced_value (struct value *value)
1993 struct piece_closure *c
1994 = (struct piece_closure *) value_computed_closure (value);
1996 struct frame_info *frame;
1997 struct dwarf2_locexpr_baton baton;
1998 int i, bit_offset, bit_length;
1999 struct dwarf_expr_piece *piece = NULL;
2000 LONGEST byte_offset;
2002 type = check_typedef (value_type (value));
2003 if (TYPE_CODE (type) != TYPE_CODE_PTR)
2006 bit_length = 8 * TYPE_LENGTH (type);
2007 bit_offset = 8 * value_offset (value);
2008 if (value_bitsize (value))
2009 bit_offset += value_bitpos (value);
2011 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2013 struct dwarf_expr_piece *p = &c->pieces[i];
2014 size_t this_size_bits = p->size;
2018 if (bit_offset >= this_size_bits)
2020 bit_offset -= this_size_bits;
2024 bit_length -= this_size_bits - bit_offset;
2028 bit_length -= this_size_bits;
2030 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2033 if (bit_length != 0)
2034 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
2040 frame = get_selected_frame (_("No frame selected."));
2042 /* This is an offset requested by GDB, such as value subcripts. */
2043 byte_offset = value_as_address (value);
2047 = dwarf2_fetch_die_loc_sect_off (piece->v.ptr.die, c->per_cu,
2048 get_frame_address_in_block_wrapper,
2051 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
2052 baton.data, baton.size, baton.per_cu,
2053 piece->v.ptr.offset + byte_offset);
2057 copy_pieced_value_closure (const struct value *v)
2059 struct piece_closure *c
2060 = (struct piece_closure *) value_computed_closure (v);
2067 free_pieced_value_closure (struct value *v)
2069 struct piece_closure *c
2070 = (struct piece_closure *) value_computed_closure (v);
2077 for (i = 0; i < c->n_pieces; ++i)
2078 if (c->pieces[i].location == DWARF_VALUE_STACK)
2079 value_free (c->pieces[i].v.value);
2086 /* Functions for accessing a variable described by DW_OP_piece. */
2087 static const struct lval_funcs pieced_value_funcs = {
2090 check_pieced_value_validity,
2091 check_pieced_value_invalid,
2092 indirect_pieced_value,
2093 NULL, /* coerce_ref */
2094 check_pieced_synthetic_pointer,
2095 copy_pieced_value_closure,
2096 free_pieced_value_closure
2099 /* Helper function which throws an error if a synthetic pointer is
2103 invalid_synthetic_pointer (void)
2105 error (_("access outside bounds of object "
2106 "referenced via synthetic pointer"));
2109 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
2111 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
2113 dwarf_expr_read_reg,
2114 dwarf_expr_read_mem,
2115 dwarf_expr_frame_base,
2116 dwarf_expr_frame_cfa,
2117 dwarf_expr_frame_pc,
2118 dwarf_expr_tls_address,
2119 dwarf_expr_dwarf_call,
2120 dwarf_expr_get_base_type,
2121 dwarf_expr_push_dwarf_reg_entry_value,
2122 dwarf_expr_get_addr_index
2125 /* Evaluate a location description, starting at DATA and with length
2126 SIZE, to find the current location of variable of TYPE in the
2127 context of FRAME. BYTE_OFFSET is applied after the contents are
2130 static struct value *
2131 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2132 const gdb_byte *data, size_t size,
2133 struct dwarf2_per_cu_data *per_cu,
2134 LONGEST byte_offset)
2136 struct value *retval;
2137 struct dwarf_expr_baton baton;
2138 struct dwarf_expr_context *ctx;
2139 struct cleanup *old_chain, *value_chain;
2140 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2141 volatile struct gdb_exception ex;
2143 if (byte_offset < 0)
2144 invalid_synthetic_pointer ();
2147 return allocate_optimized_out_value (type);
2149 baton.frame = frame;
2150 baton.per_cu = per_cu;
2152 ctx = new_dwarf_expr_context ();
2153 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2154 value_chain = make_cleanup_value_free_to_mark (value_mark ());
2156 ctx->gdbarch = get_objfile_arch (objfile);
2157 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2158 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2159 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2160 ctx->baton = &baton;
2161 ctx->funcs = &dwarf_expr_ctx_funcs;
2163 TRY_CATCH (ex, RETURN_MASK_ERROR)
2165 dwarf_expr_eval (ctx, data, size);
2169 if (ex.error == NOT_AVAILABLE_ERROR)
2171 do_cleanups (old_chain);
2172 retval = allocate_value (type);
2173 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
2176 else if (ex.error == NO_ENTRY_VALUE_ERROR)
2178 if (entry_values_debug)
2179 exception_print (gdb_stdout, ex);
2180 do_cleanups (old_chain);
2181 return allocate_optimized_out_value (type);
2184 throw_exception (ex);
2187 if (ctx->num_pieces > 0)
2189 struct piece_closure *c;
2190 struct frame_id frame_id = get_frame_id (frame);
2191 ULONGEST bit_size = 0;
2194 for (i = 0; i < ctx->num_pieces; ++i)
2195 bit_size += ctx->pieces[i].size;
2196 if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
2197 invalid_synthetic_pointer ();
2199 c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
2201 /* We must clean up the value chain after creating the piece
2202 closure but before allocating the result. */
2203 do_cleanups (value_chain);
2204 retval = allocate_computed_value (type, &pieced_value_funcs, c);
2205 VALUE_FRAME_ID (retval) = frame_id;
2206 set_value_offset (retval, byte_offset);
2210 switch (ctx->location)
2212 case DWARF_VALUE_REGISTER:
2214 struct gdbarch *arch = get_frame_arch (frame);
2215 ULONGEST dwarf_regnum = value_as_long (dwarf_expr_fetch (ctx, 0));
2216 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
2218 if (byte_offset != 0)
2219 error (_("cannot use offset on synthetic pointer to register"));
2220 do_cleanups (value_chain);
2221 if (gdb_regnum != -1)
2222 retval = value_from_register (type, gdb_regnum, frame);
2224 error (_("Unable to access DWARF register number %s"),
2225 paddress (arch, dwarf_regnum));
2229 case DWARF_VALUE_MEMORY:
2231 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
2232 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
2234 do_cleanups (value_chain);
2235 retval = allocate_value_lazy (type);
2236 VALUE_LVAL (retval) = lval_memory;
2237 if (in_stack_memory)
2238 set_value_stack (retval, 1);
2239 set_value_address (retval, address + byte_offset);
2243 case DWARF_VALUE_STACK:
2245 struct value *value = dwarf_expr_fetch (ctx, 0);
2247 const gdb_byte *val_bytes;
2248 size_t n = TYPE_LENGTH (value_type (value));
2250 if (byte_offset + TYPE_LENGTH (type) > n)
2251 invalid_synthetic_pointer ();
2253 val_bytes = value_contents_all (value);
2254 val_bytes += byte_offset;
2257 /* Preserve VALUE because we are going to free values back
2258 to the mark, but we still need the value contents
2260 value_incref (value);
2261 do_cleanups (value_chain);
2262 make_cleanup_value_free (value);
2264 retval = allocate_value (type);
2265 contents = value_contents_raw (retval);
2266 if (n > TYPE_LENGTH (type))
2268 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2270 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2271 val_bytes += n - TYPE_LENGTH (type);
2272 n = TYPE_LENGTH (type);
2274 memcpy (contents, val_bytes, n);
2278 case DWARF_VALUE_LITERAL:
2281 const bfd_byte *ldata;
2282 size_t n = ctx->len;
2284 if (byte_offset + TYPE_LENGTH (type) > n)
2285 invalid_synthetic_pointer ();
2287 do_cleanups (value_chain);
2288 retval = allocate_value (type);
2289 contents = value_contents_raw (retval);
2291 ldata = ctx->data + byte_offset;
2294 if (n > TYPE_LENGTH (type))
2296 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2298 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2299 ldata += n - TYPE_LENGTH (type);
2300 n = TYPE_LENGTH (type);
2302 memcpy (contents, ldata, n);
2306 case DWARF_VALUE_OPTIMIZED_OUT:
2307 do_cleanups (value_chain);
2308 retval = allocate_optimized_out_value (type);
2311 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2312 operation by execute_stack_op. */
2313 case DWARF_VALUE_IMPLICIT_POINTER:
2314 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2315 it can only be encountered when making a piece. */
2317 internal_error (__FILE__, __LINE__, _("invalid location type"));
2321 set_value_initialized (retval, ctx->initialized);
2323 do_cleanups (old_chain);
2328 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2329 passes 0 as the byte_offset. */
2332 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2333 const gdb_byte *data, size_t size,
2334 struct dwarf2_per_cu_data *per_cu)
2336 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2340 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
2342 struct needs_frame_baton
2345 struct dwarf2_per_cu_data *per_cu;
2348 /* Reads from registers do require a frame. */
2350 needs_frame_read_reg (void *baton, int regnum)
2352 struct needs_frame_baton *nf_baton = baton;
2354 nf_baton->needs_frame = 1;
2358 /* Reads from memory do not require a frame. */
2360 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
2362 memset (buf, 0, len);
2365 /* Frame-relative accesses do require a frame. */
2367 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
2369 static gdb_byte lit0 = DW_OP_lit0;
2370 struct needs_frame_baton *nf_baton = baton;
2375 nf_baton->needs_frame = 1;
2378 /* CFA accesses require a frame. */
2381 needs_frame_frame_cfa (void *baton)
2383 struct needs_frame_baton *nf_baton = baton;
2385 nf_baton->needs_frame = 1;
2389 /* Thread-local accesses do require a frame. */
2391 needs_frame_tls_address (void *baton, CORE_ADDR offset)
2393 struct needs_frame_baton *nf_baton = baton;
2395 nf_baton->needs_frame = 1;
2399 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
2402 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
2404 struct needs_frame_baton *nf_baton = ctx->baton;
2406 per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
2407 ctx->funcs->get_frame_pc, ctx->baton);
2410 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
2413 needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
2414 enum call_site_parameter_kind kind,
2415 union call_site_parameter_u kind_u, int deref_size)
2417 struct needs_frame_baton *nf_baton = ctx->baton;
2419 nf_baton->needs_frame = 1;
2421 /* The expression may require some stub values on DWARF stack. */
2422 dwarf_expr_push_address (ctx, 0, 0);
2425 /* DW_OP_GNU_addr_index doesn't require a frame. */
2428 needs_get_addr_index (void *baton, unsigned int index)
2430 /* Nothing to do. */
2434 /* Virtual method table for dwarf2_loc_desc_needs_frame below. */
2436 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
2438 needs_frame_read_reg,
2439 needs_frame_read_mem,
2440 needs_frame_frame_base,
2441 needs_frame_frame_cfa,
2442 needs_frame_frame_cfa, /* get_frame_pc */
2443 needs_frame_tls_address,
2444 needs_frame_dwarf_call,
2445 NULL, /* get_base_type */
2446 needs_dwarf_reg_entry_value,
2447 needs_get_addr_index
2450 /* Return non-zero iff the location expression at DATA (length SIZE)
2451 requires a frame to evaluate. */
2454 dwarf2_loc_desc_needs_frame (const gdb_byte *data, size_t size,
2455 struct dwarf2_per_cu_data *per_cu)
2457 struct needs_frame_baton baton;
2458 struct dwarf_expr_context *ctx;
2460 struct cleanup *old_chain;
2461 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2463 baton.needs_frame = 0;
2464 baton.per_cu = per_cu;
2466 ctx = new_dwarf_expr_context ();
2467 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2468 make_cleanup_value_free_to_mark (value_mark ());
2470 ctx->gdbarch = get_objfile_arch (objfile);
2471 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2472 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2473 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2474 ctx->baton = &baton;
2475 ctx->funcs = &needs_frame_ctx_funcs;
2477 dwarf_expr_eval (ctx, data, size);
2479 in_reg = ctx->location == DWARF_VALUE_REGISTER;
2481 if (ctx->num_pieces > 0)
2485 /* If the location has several pieces, and any of them are in
2486 registers, then we will need a frame to fetch them from. */
2487 for (i = 0; i < ctx->num_pieces; i++)
2488 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
2492 do_cleanups (old_chain);
2494 return baton.needs_frame || in_reg;
2497 /* A helper function that throws an unimplemented error mentioning a
2498 given DWARF operator. */
2501 unimplemented (unsigned int op)
2503 const char *name = get_DW_OP_name (op);
2506 error (_("DWARF operator %s cannot be translated to an agent expression"),
2509 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2510 "to an agent expression"),
2514 /* A helper function to convert a DWARF register to an arch register.
2515 ARCH is the architecture.
2516 DWARF_REG is the register.
2517 This will throw an exception if the DWARF register cannot be
2518 translated to an architecture register. */
2521 translate_register (struct gdbarch *arch, int dwarf_reg)
2523 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2525 error (_("Unable to access DWARF register number %d"), dwarf_reg);
2529 /* A helper function that emits an access to memory. ARCH is the
2530 target architecture. EXPR is the expression which we are building.
2531 NBITS is the number of bits we want to read. This emits the
2532 opcodes needed to read the memory and then extract the desired
2536 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2538 ULONGEST nbytes = (nbits + 7) / 8;
2540 gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
2543 ax_trace_quick (expr, nbytes);
2546 ax_simple (expr, aop_ref8);
2547 else if (nbits <= 16)
2548 ax_simple (expr, aop_ref16);
2549 else if (nbits <= 32)
2550 ax_simple (expr, aop_ref32);
2552 ax_simple (expr, aop_ref64);
2554 /* If we read exactly the number of bytes we wanted, we're done. */
2555 if (8 * nbytes == nbits)
2558 if (gdbarch_bits_big_endian (arch))
2560 /* On a bits-big-endian machine, we want the high-order
2562 ax_const_l (expr, 8 * nbytes - nbits);
2563 ax_simple (expr, aop_rsh_unsigned);
2567 /* On a bits-little-endian box, we want the low-order NBITS. */
2568 ax_zero_ext (expr, nbits);
2572 /* A helper function to return the frame's PC. */
2575 get_ax_pc (void *baton)
2577 struct agent_expr *expr = baton;
2582 /* Compile a DWARF location expression to an agent expression.
2584 EXPR is the agent expression we are building.
2585 LOC is the agent value we modify.
2586 ARCH is the architecture.
2587 ADDR_SIZE is the size of addresses, in bytes.
2588 OP_PTR is the start of the location expression.
2589 OP_END is one past the last byte of the location expression.
2591 This will throw an exception for various kinds of errors -- for
2592 example, if the expression cannot be compiled, or if the expression
2596 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
2597 struct gdbarch *arch, unsigned int addr_size,
2598 const gdb_byte *op_ptr, const gdb_byte *op_end,
2599 struct dwarf2_per_cu_data *per_cu)
2601 struct cleanup *cleanups;
2603 VEC(int) *dw_labels = NULL, *patches = NULL;
2604 const gdb_byte * const base = op_ptr;
2605 const gdb_byte *previous_piece = op_ptr;
2606 enum bfd_endian byte_order = gdbarch_byte_order (arch);
2607 ULONGEST bits_collected = 0;
2608 unsigned int addr_size_bits = 8 * addr_size;
2609 int bits_big_endian = gdbarch_bits_big_endian (arch);
2611 offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
2612 cleanups = make_cleanup (xfree, offsets);
2614 for (i = 0; i < op_end - op_ptr; ++i)
2617 make_cleanup (VEC_cleanup (int), &dw_labels);
2618 make_cleanup (VEC_cleanup (int), &patches);
2620 /* By default we are making an address. */
2621 loc->kind = axs_lvalue_memory;
2623 while (op_ptr < op_end)
2625 enum dwarf_location_atom op = *op_ptr;
2626 uint64_t uoffset, reg;
2630 offsets[op_ptr - base] = expr->len;
2633 /* Our basic approach to code generation is to map DWARF
2634 operations directly to AX operations. However, there are
2637 First, DWARF works on address-sized units, but AX always uses
2638 LONGEST. For most operations we simply ignore this
2639 difference; instead we generate sign extensions as needed
2640 before division and comparison operations. It would be nice
2641 to omit the sign extensions, but there is no way to determine
2642 the size of the target's LONGEST. (This code uses the size
2643 of the host LONGEST in some cases -- that is a bug but it is
2646 Second, some DWARF operations cannot be translated to AX.
2647 For these we simply fail. See
2648 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
2683 ax_const_l (expr, op - DW_OP_lit0);
2687 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
2688 op_ptr += addr_size;
2689 /* Some versions of GCC emit DW_OP_addr before
2690 DW_OP_GNU_push_tls_address. In this case the value is an
2691 index, not an address. We don't support things like
2692 branching between the address and the TLS op. */
2693 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
2694 uoffset += dwarf2_per_cu_text_offset (per_cu);
2695 ax_const_l (expr, uoffset);
2699 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
2703 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
2707 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
2711 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
2715 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
2719 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
2723 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
2727 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
2731 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
2732 ax_const_l (expr, uoffset);
2735 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2736 ax_const_l (expr, offset);
2771 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2772 loc->u.reg = translate_register (arch, op - DW_OP_reg0);
2773 loc->kind = axs_lvalue_register;
2777 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
2778 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2779 loc->u.reg = translate_register (arch, reg);
2780 loc->kind = axs_lvalue_register;
2783 case DW_OP_implicit_value:
2787 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
2788 if (op_ptr + len > op_end)
2789 error (_("DW_OP_implicit_value: too few bytes available."));
2790 if (len > sizeof (ULONGEST))
2791 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
2794 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
2797 dwarf_expr_require_composition (op_ptr, op_end,
2798 "DW_OP_implicit_value");
2800 loc->kind = axs_rvalue;
2804 case DW_OP_stack_value:
2805 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
2806 loc->kind = axs_rvalue;
2841 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2842 i = translate_register (arch, op - DW_OP_breg0);
2846 ax_const_l (expr, offset);
2847 ax_simple (expr, aop_add);
2852 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
2853 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2854 i = translate_register (arch, reg);
2858 ax_const_l (expr, offset);
2859 ax_simple (expr, aop_add);
2865 const gdb_byte *datastart;
2868 struct symbol *framefunc;
2870 b = block_for_pc (expr->scope);
2873 error (_("No block found for address"));
2875 framefunc = block_linkage_function (b);
2878 error (_("No function found for block"));
2880 dwarf_expr_frame_base_1 (framefunc, expr->scope,
2881 &datastart, &datalen);
2883 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
2884 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
2885 datastart + datalen, per_cu);
2886 if (loc->kind == axs_lvalue_register)
2887 require_rvalue (expr, loc);
2891 ax_const_l (expr, offset);
2892 ax_simple (expr, aop_add);
2895 loc->kind = axs_lvalue_memory;
2900 ax_simple (expr, aop_dup);
2904 ax_simple (expr, aop_pop);
2909 ax_pick (expr, offset);
2913 ax_simple (expr, aop_swap);
2921 ax_simple (expr, aop_rot);
2925 case DW_OP_deref_size:
2929 if (op == DW_OP_deref_size)
2937 ax_simple (expr, aop_ref8);
2940 ax_simple (expr, aop_ref16);
2943 ax_simple (expr, aop_ref32);
2946 ax_simple (expr, aop_ref64);
2949 /* Note that get_DW_OP_name will never return
2951 error (_("Unsupported size %d in %s"),
2952 size, get_DW_OP_name (op));
2958 /* Sign extend the operand. */
2959 ax_ext (expr, addr_size_bits);
2960 ax_simple (expr, aop_dup);
2961 ax_const_l (expr, 0);
2962 ax_simple (expr, aop_less_signed);
2963 ax_simple (expr, aop_log_not);
2964 i = ax_goto (expr, aop_if_goto);
2965 /* We have to emit 0 - X. */
2966 ax_const_l (expr, 0);
2967 ax_simple (expr, aop_swap);
2968 ax_simple (expr, aop_sub);
2969 ax_label (expr, i, expr->len);
2973 /* No need to sign extend here. */
2974 ax_const_l (expr, 0);
2975 ax_simple (expr, aop_swap);
2976 ax_simple (expr, aop_sub);
2980 /* Sign extend the operand. */
2981 ax_ext (expr, addr_size_bits);
2982 ax_simple (expr, aop_bit_not);
2985 case DW_OP_plus_uconst:
2986 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
2987 /* It would be really weird to emit `DW_OP_plus_uconst 0',
2988 but we micro-optimize anyhow. */
2991 ax_const_l (expr, reg);
2992 ax_simple (expr, aop_add);
2997 ax_simple (expr, aop_bit_and);
3001 /* Sign extend the operands. */
3002 ax_ext (expr, addr_size_bits);
3003 ax_simple (expr, aop_swap);
3004 ax_ext (expr, addr_size_bits);
3005 ax_simple (expr, aop_swap);
3006 ax_simple (expr, aop_div_signed);
3010 ax_simple (expr, aop_sub);
3014 ax_simple (expr, aop_rem_unsigned);
3018 ax_simple (expr, aop_mul);
3022 ax_simple (expr, aop_bit_or);
3026 ax_simple (expr, aop_add);
3030 ax_simple (expr, aop_lsh);
3034 ax_simple (expr, aop_rsh_unsigned);
3038 ax_simple (expr, aop_rsh_signed);
3042 ax_simple (expr, aop_bit_xor);
3046 /* Sign extend the operands. */
3047 ax_ext (expr, addr_size_bits);
3048 ax_simple (expr, aop_swap);
3049 ax_ext (expr, addr_size_bits);
3050 /* Note no swap here: A <= B is !(B < A). */
3051 ax_simple (expr, aop_less_signed);
3052 ax_simple (expr, aop_log_not);
3056 /* Sign extend the operands. */
3057 ax_ext (expr, addr_size_bits);
3058 ax_simple (expr, aop_swap);
3059 ax_ext (expr, addr_size_bits);
3060 ax_simple (expr, aop_swap);
3061 /* A >= B is !(A < B). */
3062 ax_simple (expr, aop_less_signed);
3063 ax_simple (expr, aop_log_not);
3067 /* Sign extend the operands. */
3068 ax_ext (expr, addr_size_bits);
3069 ax_simple (expr, aop_swap);
3070 ax_ext (expr, addr_size_bits);
3071 /* No need for a second swap here. */
3072 ax_simple (expr, aop_equal);
3076 /* Sign extend the operands. */
3077 ax_ext (expr, addr_size_bits);
3078 ax_simple (expr, aop_swap);
3079 ax_ext (expr, addr_size_bits);
3080 ax_simple (expr, aop_swap);
3081 ax_simple (expr, aop_less_signed);
3085 /* Sign extend the operands. */
3086 ax_ext (expr, addr_size_bits);
3087 ax_simple (expr, aop_swap);
3088 ax_ext (expr, addr_size_bits);
3089 /* Note no swap here: A > B is B < A. */
3090 ax_simple (expr, aop_less_signed);
3094 /* Sign extend the operands. */
3095 ax_ext (expr, addr_size_bits);
3096 ax_simple (expr, aop_swap);
3097 ax_ext (expr, addr_size_bits);
3098 /* No need for a swap here. */
3099 ax_simple (expr, aop_equal);
3100 ax_simple (expr, aop_log_not);
3103 case DW_OP_call_frame_cfa:
3104 dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
3105 loc->kind = axs_lvalue_memory;
3108 case DW_OP_GNU_push_tls_address:
3113 offset = extract_signed_integer (op_ptr, 2, byte_order);
3115 i = ax_goto (expr, aop_goto);
3116 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3117 VEC_safe_push (int, patches, i);
3121 offset = extract_signed_integer (op_ptr, 2, byte_order);
3123 /* Zero extend the operand. */
3124 ax_zero_ext (expr, addr_size_bits);
3125 i = ax_goto (expr, aop_if_goto);
3126 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3127 VEC_safe_push (int, patches, i);
3134 case DW_OP_bit_piece:
3136 uint64_t size, offset;
3138 if (op_ptr - 1 == previous_piece)
3139 error (_("Cannot translate empty pieces to agent expressions"));
3140 previous_piece = op_ptr - 1;
3142 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3143 if (op == DW_OP_piece)
3149 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
3151 if (bits_collected + size > 8 * sizeof (LONGEST))
3152 error (_("Expression pieces exceed word size"));
3154 /* Access the bits. */
3157 case axs_lvalue_register:
3158 ax_reg (expr, loc->u.reg);
3161 case axs_lvalue_memory:
3162 /* Offset the pointer, if needed. */
3165 ax_const_l (expr, offset / 8);
3166 ax_simple (expr, aop_add);
3169 access_memory (arch, expr, size);
3173 /* For a bits-big-endian target, shift up what we already
3174 have. For a bits-little-endian target, shift up the
3175 new data. Note that there is a potential bug here if
3176 the DWARF expression leaves multiple values on the
3178 if (bits_collected > 0)
3180 if (bits_big_endian)
3182 ax_simple (expr, aop_swap);
3183 ax_const_l (expr, size);
3184 ax_simple (expr, aop_lsh);
3185 /* We don't need a second swap here, because
3186 aop_bit_or is symmetric. */
3190 ax_const_l (expr, size);
3191 ax_simple (expr, aop_lsh);
3193 ax_simple (expr, aop_bit_or);
3196 bits_collected += size;
3197 loc->kind = axs_rvalue;
3201 case DW_OP_GNU_uninit:
3207 struct dwarf2_locexpr_baton block;
3208 int size = (op == DW_OP_call2 ? 2 : 4);
3211 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3214 offset.cu_off = uoffset;
3215 block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
3218 /* DW_OP_call_ref is currently not supported. */
3219 gdb_assert (block.per_cu == per_cu);
3221 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3222 block.data, block.data + block.size,
3227 case DW_OP_call_ref:
3235 /* Patch all the branches we emitted. */
3236 for (i = 0; i < VEC_length (int, patches); ++i)
3238 int targ = offsets[VEC_index (int, dw_labels, i)];
3240 internal_error (__FILE__, __LINE__, _("invalid label"));
3241 ax_label (expr, VEC_index (int, patches, i), targ);
3244 do_cleanups (cleanups);
3248 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3249 evaluator to calculate the location. */
3250 static struct value *
3251 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3253 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3256 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3257 dlbaton->size, dlbaton->per_cu);
3262 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3263 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3266 static struct value *
3267 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3269 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3271 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3275 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3277 locexpr_read_needs_frame (struct symbol *symbol)
3279 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3281 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
3285 /* Return true if DATA points to the end of a piece. END is one past
3286 the last byte in the expression. */
3289 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3291 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3294 /* Helper for locexpr_describe_location_piece that finds the name of a
3298 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3302 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
3303 return gdbarch_register_name (gdbarch, regnum);
3306 /* Nicely describe a single piece of a location, returning an updated
3307 position in the bytecode sequence. This function cannot recognize
3308 all locations; if a location is not recognized, it simply returns
3309 DATA. If there is an error during reading, e.g. we run off the end
3310 of the buffer, an error is thrown. */
3312 static const gdb_byte *
3313 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3314 CORE_ADDR addr, struct objfile *objfile,
3315 struct dwarf2_per_cu_data *per_cu,
3316 const gdb_byte *data, const gdb_byte *end,
3317 unsigned int addr_size)
3319 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3322 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3324 fprintf_filtered (stream, _("a variable in $%s"),
3325 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3328 else if (data[0] == DW_OP_regx)
3332 data = safe_read_uleb128 (data + 1, end, ®);
3333 fprintf_filtered (stream, _("a variable in $%s"),
3334 locexpr_regname (gdbarch, reg));
3336 else if (data[0] == DW_OP_fbreg)
3339 struct symbol *framefunc;
3341 int64_t frame_offset;
3342 const gdb_byte *base_data, *new_data, *save_data = data;
3344 int64_t base_offset = 0;
3346 new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3347 if (!piece_end_p (new_data, end))
3351 b = block_for_pc (addr);
3354 error (_("No block found for address for symbol \"%s\"."),
3355 SYMBOL_PRINT_NAME (symbol));
3357 framefunc = block_linkage_function (b);
3360 error (_("No function found for block for symbol \"%s\"."),
3361 SYMBOL_PRINT_NAME (symbol));
3363 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
3365 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3367 const gdb_byte *buf_end;
3369 frame_reg = base_data[0] - DW_OP_breg0;
3370 buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3372 if (buf_end != base_data + base_size)
3373 error (_("Unexpected opcode after "
3374 "DW_OP_breg%u for symbol \"%s\"."),
3375 frame_reg, SYMBOL_PRINT_NAME (symbol));
3377 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3379 /* The frame base is just the register, with no offset. */
3380 frame_reg = base_data[0] - DW_OP_reg0;
3385 /* We don't know what to do with the frame base expression,
3386 so we can't trace this variable; give up. */
3390 fprintf_filtered (stream,
3391 _("a variable at frame base reg $%s offset %s+%s"),
3392 locexpr_regname (gdbarch, frame_reg),
3393 plongest (base_offset), plongest (frame_offset));
3395 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3396 && piece_end_p (data, end))
3400 data = safe_read_sleb128 (data + 1, end, &offset);
3402 fprintf_filtered (stream,
3403 _("a variable at offset %s from base reg $%s"),
3405 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3408 /* The location expression for a TLS variable looks like this (on a
3411 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3412 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3414 0x3 is the encoding for DW_OP_addr, which has an operand as long
3415 as the size of an address on the target machine (here is 8
3416 bytes). Note that more recent version of GCC emit DW_OP_const4u
3417 or DW_OP_const8u, depending on address size, rather than
3418 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3419 The operand represents the offset at which the variable is within
3420 the thread local storage. */
3422 else if (data + 1 + addr_size < end
3423 && (data[0] == DW_OP_addr
3424 || (addr_size == 4 && data[0] == DW_OP_const4u)
3425 || (addr_size == 8 && data[0] == DW_OP_const8u))
3426 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
3427 && piece_end_p (data + 2 + addr_size, end))
3430 offset = extract_unsigned_integer (data + 1, addr_size,
3431 gdbarch_byte_order (gdbarch));
3433 fprintf_filtered (stream,
3434 _("a thread-local variable at offset 0x%s "
3435 "in the thread-local storage for `%s'"),
3436 phex_nz (offset, addr_size), objfile->name);
3438 data += 1 + addr_size + 1;
3441 /* With -gsplit-dwarf a TLS variable can also look like this:
3442 DW_AT_location : 3 byte block: fc 4 e0
3443 (DW_OP_GNU_const_index: 4;
3444 DW_OP_GNU_push_tls_address) */
3445 else if (data + 3 <= end
3446 && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3447 && data[0] == DW_OP_GNU_const_index
3449 && data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3450 && piece_end_p (data + 2 + leb128_size, end))
3454 data = safe_read_uleb128 (data + 1, end, &offset);
3455 offset = dwarf2_read_addr_index (per_cu, offset);
3456 fprintf_filtered (stream,
3457 _("a thread-local variable at offset 0x%s "
3458 "in the thread-local storage for `%s'"),
3459 phex_nz (offset, addr_size), objfile->name);
3463 else if (data[0] >= DW_OP_lit0
3464 && data[0] <= DW_OP_lit31
3466 && data[1] == DW_OP_stack_value)
3468 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3475 /* Disassemble an expression, stopping at the end of a piece or at the
3476 end of the expression. Returns a pointer to the next unread byte
3477 in the input expression. If ALL is nonzero, then this function
3478 will keep going until it reaches the end of the expression.
3479 If there is an error during reading, e.g. we run off the end
3480 of the buffer, an error is thrown. */
3482 static const gdb_byte *
3483 disassemble_dwarf_expression (struct ui_file *stream,
3484 struct gdbarch *arch, unsigned int addr_size,
3485 int offset_size, const gdb_byte *start,
3486 const gdb_byte *data, const gdb_byte *end,
3487 int indent, int all,
3488 struct dwarf2_per_cu_data *per_cu)
3492 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3494 enum dwarf_location_atom op = *data++;
3499 name = get_DW_OP_name (op);
3502 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3503 op, (long) (data - 1 - start));
3504 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3505 (long) (data - 1 - start), name);
3510 ul = extract_unsigned_integer (data, addr_size,
3511 gdbarch_byte_order (arch));
3513 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3517 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3519 fprintf_filtered (stream, " %s", pulongest (ul));
3522 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3524 fprintf_filtered (stream, " %s", plongest (l));
3527 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3529 fprintf_filtered (stream, " %s", pulongest (ul));
3532 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3534 fprintf_filtered (stream, " %s", plongest (l));
3537 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3539 fprintf_filtered (stream, " %s", pulongest (ul));
3542 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3544 fprintf_filtered (stream, " %s", plongest (l));
3547 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3549 fprintf_filtered (stream, " %s", pulongest (ul));
3552 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3554 fprintf_filtered (stream, " %s", plongest (l));
3557 data = safe_read_uleb128 (data, end, &ul);
3558 fprintf_filtered (stream, " %s", pulongest (ul));
3561 data = safe_read_sleb128 (data, end, &l);
3562 fprintf_filtered (stream, " %s", plongest (l));
3597 fprintf_filtered (stream, " [$%s]",
3598 locexpr_regname (arch, op - DW_OP_reg0));
3602 data = safe_read_uleb128 (data, end, &ul);
3603 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
3604 locexpr_regname (arch, (int) ul));
3607 case DW_OP_implicit_value:
3608 data = safe_read_uleb128 (data, end, &ul);
3610 fprintf_filtered (stream, " %s", pulongest (ul));
3645 data = safe_read_sleb128 (data, end, &l);
3646 fprintf_filtered (stream, " %s [$%s]", plongest (l),
3647 locexpr_regname (arch, op - DW_OP_breg0));
3651 data = safe_read_uleb128 (data, end, &ul);
3652 data = safe_read_sleb128 (data, end, &l);
3653 fprintf_filtered (stream, " register %s [$%s] offset %s",
3655 locexpr_regname (arch, (int) ul),
3660 data = safe_read_sleb128 (data, end, &l);
3661 fprintf_filtered (stream, " %s", plongest (l));
3664 case DW_OP_xderef_size:
3665 case DW_OP_deref_size:
3667 fprintf_filtered (stream, " %d", *data);
3671 case DW_OP_plus_uconst:
3672 data = safe_read_uleb128 (data, end, &ul);
3673 fprintf_filtered (stream, " %s", pulongest (ul));
3677 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3679 fprintf_filtered (stream, " to %ld",
3680 (long) (data + l - start));
3684 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3686 fprintf_filtered (stream, " %ld",
3687 (long) (data + l - start));
3691 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3693 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
3697 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3699 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3702 case DW_OP_call_ref:
3703 ul = extract_unsigned_integer (data, offset_size,
3704 gdbarch_byte_order (arch));
3705 data += offset_size;
3706 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
3710 data = safe_read_uleb128 (data, end, &ul);
3711 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
3714 case DW_OP_bit_piece:
3718 data = safe_read_uleb128 (data, end, &ul);
3719 data = safe_read_uleb128 (data, end, &offset);
3720 fprintf_filtered (stream, " size %s offset %s (bits)",
3721 pulongest (ul), pulongest (offset));
3725 case DW_OP_GNU_implicit_pointer:
3727 ul = extract_unsigned_integer (data, offset_size,
3728 gdbarch_byte_order (arch));
3729 data += offset_size;
3731 data = safe_read_sleb128 (data, end, &l);
3733 fprintf_filtered (stream, " DIE %s offset %s",
3734 phex_nz (ul, offset_size),
3739 case DW_OP_GNU_deref_type:
3741 int addr_size = *data++;
3745 data = safe_read_uleb128 (data, end, &ul);
3747 type = dwarf2_get_die_type (offset, per_cu);
3748 fprintf_filtered (stream, "<");
3749 type_print (type, "", stream, -1);
3750 fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
3755 case DW_OP_GNU_const_type:
3760 data = safe_read_uleb128 (data, end, &ul);
3761 type_die.cu_off = ul;
3762 type = dwarf2_get_die_type (type_die, per_cu);
3763 fprintf_filtered (stream, "<");
3764 type_print (type, "", stream, -1);
3765 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3769 case DW_OP_GNU_regval_type:
3775 data = safe_read_uleb128 (data, end, ®);
3776 data = safe_read_uleb128 (data, end, &ul);
3777 type_die.cu_off = ul;
3779 type = dwarf2_get_die_type (type_die, per_cu);
3780 fprintf_filtered (stream, "<");
3781 type_print (type, "", stream, -1);
3782 fprintf_filtered (stream, " [0x%s]> [$%s]",
3783 phex_nz (type_die.cu_off, 0),
3784 locexpr_regname (arch, reg));
3788 case DW_OP_GNU_convert:
3789 case DW_OP_GNU_reinterpret:
3793 data = safe_read_uleb128 (data, end, &ul);
3794 type_die.cu_off = ul;
3796 if (type_die.cu_off == 0)
3797 fprintf_filtered (stream, "<0>");
3802 type = dwarf2_get_die_type (type_die, per_cu);
3803 fprintf_filtered (stream, "<");
3804 type_print (type, "", stream, -1);
3805 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
3810 case DW_OP_GNU_entry_value:
3811 data = safe_read_uleb128 (data, end, &ul);
3812 fputc_filtered ('\n', stream);
3813 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
3814 start, data, data + ul, indent + 2,
3819 case DW_OP_GNU_parameter_ref:
3820 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3822 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3825 case DW_OP_GNU_addr_index:
3826 data = safe_read_uleb128 (data, end, &ul);
3827 ul = dwarf2_read_addr_index (per_cu, ul);
3828 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3830 case DW_OP_GNU_const_index:
3831 data = safe_read_uleb128 (data, end, &ul);
3832 ul = dwarf2_read_addr_index (per_cu, ul);
3833 fprintf_filtered (stream, " %s", pulongest (ul));
3837 fprintf_filtered (stream, "\n");
3843 /* Describe a single location, which may in turn consist of multiple
3847 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
3848 struct ui_file *stream,
3849 const gdb_byte *data, size_t size,
3850 struct objfile *objfile, unsigned int addr_size,
3851 int offset_size, struct dwarf2_per_cu_data *per_cu)
3853 const gdb_byte *end = data + size;
3854 int first_piece = 1, bad = 0;
3858 const gdb_byte *here = data;
3859 int disassemble = 1;
3864 fprintf_filtered (stream, _(", and "));
3866 if (!dwarf2_always_disassemble)
3868 data = locexpr_describe_location_piece (symbol, stream,
3869 addr, objfile, per_cu,
3870 data, end, addr_size);
3871 /* If we printed anything, or if we have an empty piece,
3872 then don't disassemble. */
3874 || data[0] == DW_OP_piece
3875 || data[0] == DW_OP_bit_piece)
3880 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
3881 data = disassemble_dwarf_expression (stream,
3882 get_objfile_arch (objfile),
3883 addr_size, offset_size, data,
3885 dwarf2_always_disassemble,
3891 int empty = data == here;
3894 fprintf_filtered (stream, " ");
3895 if (data[0] == DW_OP_piece)
3899 data = safe_read_uleb128 (data + 1, end, &bytes);
3902 fprintf_filtered (stream, _("an empty %s-byte piece"),
3905 fprintf_filtered (stream, _(" [%s-byte piece]"),
3908 else if (data[0] == DW_OP_bit_piece)
3910 uint64_t bits, offset;
3912 data = safe_read_uleb128 (data + 1, end, &bits);
3913 data = safe_read_uleb128 (data, end, &offset);
3916 fprintf_filtered (stream,
3917 _("an empty %s-bit piece"),
3920 fprintf_filtered (stream,
3921 _(" [%s-bit piece, offset %s bits]"),
3922 pulongest (bits), pulongest (offset));
3932 if (bad || data > end)
3933 error (_("Corrupted DWARF2 expression for \"%s\"."),
3934 SYMBOL_PRINT_NAME (symbol));
3937 /* Print a natural-language description of SYMBOL to STREAM. This
3938 version is for a symbol with a single location. */
3941 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
3942 struct ui_file *stream)
3944 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3945 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3946 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3947 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
3949 locexpr_describe_location_1 (symbol, addr, stream,
3950 dlbaton->data, dlbaton->size,
3951 objfile, addr_size, offset_size,
3955 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3956 any necessary bytecode in AX. */
3959 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
3960 struct agent_expr *ax, struct axs_value *value)
3962 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3963 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
3965 if (dlbaton->size == 0)
3966 value->optimized_out = 1;
3968 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
3969 dlbaton->data, dlbaton->data + dlbaton->size,
3973 /* The set of location functions used with the DWARF-2 expression
3975 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
3976 locexpr_read_variable,
3977 locexpr_read_variable_at_entry,
3978 locexpr_read_needs_frame,
3979 locexpr_describe_location,
3980 locexpr_tracepoint_var_ref
3984 /* Wrapper functions for location lists. These generally find
3985 the appropriate location expression and call something above. */
3987 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3988 evaluator to calculate the location. */
3989 static struct value *
3990 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
3992 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3994 const gdb_byte *data;
3996 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
3998 data = dwarf2_find_location_expression (dlbaton, &size, pc);
3999 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4005 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4006 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4009 Function always returns non-NULL value, it may be marked optimized out if
4010 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
4011 if it cannot resolve the parameter for any reason. */
4013 static struct value *
4014 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4016 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4017 const gdb_byte *data;
4021 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4022 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4024 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4026 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4028 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4031 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
4033 loclist_read_needs_frame (struct symbol *symbol)
4035 /* If there's a location list, then assume we need to have a frame
4036 to choose the appropriate location expression. With tracking of
4037 global variables this is not necessarily true, but such tracking
4038 is disabled in GCC at the moment until we figure out how to
4044 /* Print a natural-language description of SYMBOL to STREAM. This
4045 version applies when there is a list of different locations, each
4046 with a specified address range. */
4049 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4050 struct ui_file *stream)
4052 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4053 const gdb_byte *loc_ptr, *buf_end;
4054 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4055 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4056 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4057 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4058 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4059 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4060 /* Adjust base_address for relocatable objects. */
4061 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
4062 CORE_ADDR base_address = dlbaton->base_address + base_offset;
4065 loc_ptr = dlbaton->data;
4066 buf_end = dlbaton->data + dlbaton->size;
4068 fprintf_filtered (stream, _("multi-location:\n"));
4070 /* Iterate through locations until we run out. */
4073 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4075 enum debug_loc_kind kind;
4076 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4078 if (dlbaton->from_dwo)
4079 kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4080 loc_ptr, buf_end, &new_ptr,
4081 &low, &high, byte_order);
4083 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4085 byte_order, addr_size,
4090 case DEBUG_LOC_END_OF_LIST:
4093 case DEBUG_LOC_BASE_ADDRESS:
4094 base_address = high + base_offset;
4095 fprintf_filtered (stream, _(" Base address %s"),
4096 paddress (gdbarch, base_address));
4098 case DEBUG_LOC_START_END:
4099 case DEBUG_LOC_START_LENGTH:
4101 case DEBUG_LOC_BUFFER_OVERFLOW:
4102 case DEBUG_LOC_INVALID_ENTRY:
4103 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4104 SYMBOL_PRINT_NAME (symbol));
4106 gdb_assert_not_reached ("bad debug_loc_kind");
4109 /* Otherwise, a location expression entry. */
4110 low += base_address;
4111 high += base_address;
4113 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4116 /* (It would improve readability to print only the minimum
4117 necessary digits of the second number of the range.) */
4118 fprintf_filtered (stream, _(" Range %s-%s: "),
4119 paddress (gdbarch, low), paddress (gdbarch, high));
4121 /* Now describe this particular location. */
4122 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4123 objfile, addr_size, offset_size,
4126 fprintf_filtered (stream, "\n");
4132 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4133 any necessary bytecode in AX. */
4135 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4136 struct agent_expr *ax, struct axs_value *value)
4138 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
4139 const gdb_byte *data;
4141 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4143 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4145 value->optimized_out = 1;
4147 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
4151 /* The set of location functions used with the DWARF-2 expression
4152 evaluator and location lists. */
4153 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4154 loclist_read_variable,
4155 loclist_read_variable_at_entry,
4156 loclist_read_needs_frame,
4157 loclist_describe_location,
4158 loclist_tracepoint_var_ref
4161 /* Provide a prototype to silence -Wmissing-prototypes. */
4162 extern initialize_file_ftype _initialize_dwarf2loc;
4165 _initialize_dwarf2loc (void)
4167 add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4168 &entry_values_debug,
4169 _("Set entry values and tail call frames "
4171 _("Show entry values and tail call frames "
4173 _("When non-zero, the process of determining "
4174 "parameter values from function entry point "
4175 "and tail call frames will be printed."),
4177 show_entry_values_debug,
4178 &setdebuglist, &showdebuglist);