1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007-2012 Free Software Foundation, Inc.
5 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
33 #include "exceptions.h"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
47 extern int dwarf2_always_disassemble
;
49 static void dwarf_expr_frame_base_1 (struct symbol
*framefunc
, CORE_ADDR pc
,
50 const gdb_byte
**start
, size_t *length
);
52 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs
;
54 static struct value
*dwarf2_evaluate_loc_desc_full (struct type
*type
,
55 struct frame_info
*frame
,
58 struct dwarf2_per_cu_data
*per_cu
,
61 /* A function for dealing with location lists. Given a
62 symbol baton (BATON) and a pc value (PC), find the appropriate
63 location expression, set *LOCEXPR_LENGTH, and return a pointer
64 to the beginning of the expression. Returns NULL on failure.
66 For now, only return the first matching location expression; there
67 can be more than one in the list. */
70 dwarf2_find_location_expression (struct dwarf2_loclist_baton
*baton
,
71 size_t *locexpr_length
, CORE_ADDR pc
)
74 const gdb_byte
*loc_ptr
, *buf_end
;
76 struct objfile
*objfile
= dwarf2_per_cu_objfile (baton
->per_cu
);
77 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
78 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
79 unsigned int addr_size
= dwarf2_per_cu_addr_size (baton
->per_cu
);
80 int signed_addr_p
= bfd_get_sign_extend_vma (objfile
->obfd
);
81 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
82 /* Adjust base_address for relocatable objects. */
83 CORE_ADDR base_offset
= dwarf2_per_cu_text_offset (baton
->per_cu
);
84 CORE_ADDR base_address
= baton
->base_address
+ base_offset
;
86 loc_ptr
= baton
->data
;
87 buf_end
= baton
->data
+ baton
->size
;
91 if (buf_end
- loc_ptr
< 2 * addr_size
)
92 error (_("dwarf2_find_location_expression: "
93 "Corrupted DWARF expression."));
96 low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
98 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
102 high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
104 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
105 loc_ptr
+= addr_size
;
107 /* A base-address-selection entry. */
108 if ((low
& base_mask
) == base_mask
)
110 base_address
= high
+ base_offset
;
114 /* An end-of-list entry. */
115 if (low
== 0 && high
== 0)
121 /* Otherwise, a location expression entry. */
123 high
+= base_address
;
125 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
128 if (low
== high
&& pc
== low
)
130 /* This is entry PC record present only at entry point
131 of a function. Verify it is really the function entry point. */
133 struct block
*pc_block
= block_for_pc (pc
);
134 struct symbol
*pc_func
= NULL
;
137 pc_func
= block_linkage_function (pc_block
);
139 if (pc_func
&& pc
== BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func
)))
141 *locexpr_length
= length
;
146 if (pc
>= low
&& pc
< high
)
148 *locexpr_length
= length
;
156 /* This is the baton used when performing dwarf2 expression
158 struct dwarf_expr_baton
160 struct frame_info
*frame
;
161 struct dwarf2_per_cu_data
*per_cu
;
164 /* Helper functions for dwarf2_evaluate_loc_desc. */
166 /* Using the frame specified in BATON, return the value of register
167 REGNUM, treated as a pointer. */
169 dwarf_expr_read_reg (void *baton
, int dwarf_regnum
)
171 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
172 struct gdbarch
*gdbarch
= get_frame_arch (debaton
->frame
);
176 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, dwarf_regnum
);
177 result
= address_from_register (builtin_type (gdbarch
)->builtin_data_ptr
,
178 regnum
, debaton
->frame
);
182 /* Read memory at ADDR (length LEN) into BUF. */
185 dwarf_expr_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
187 read_memory (addr
, buf
, len
);
190 /* Using the frame specified in BATON, find the location expression
191 describing the frame base. Return a pointer to it in START and
192 its length in LENGTH. */
194 dwarf_expr_frame_base (void *baton
, const gdb_byte
**start
, size_t * length
)
196 /* FIXME: cagney/2003-03-26: This code should be using
197 get_frame_base_address(), and then implement a dwarf2 specific
199 struct symbol
*framefunc
;
200 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
202 /* Use block_linkage_function, which returns a real (not inlined)
203 function, instead of get_frame_function, which may return an
205 framefunc
= block_linkage_function (get_frame_block (debaton
->frame
, NULL
));
207 /* If we found a frame-relative symbol then it was certainly within
208 some function associated with a frame. If we can't find the frame,
209 something has gone wrong. */
210 gdb_assert (framefunc
!= NULL
);
212 dwarf_expr_frame_base_1 (framefunc
,
213 get_frame_address_in_block (debaton
->frame
),
218 dwarf_expr_frame_base_1 (struct symbol
*framefunc
, CORE_ADDR pc
,
219 const gdb_byte
**start
, size_t *length
)
221 if (SYMBOL_LOCATION_BATON (framefunc
) == NULL
)
223 else if (SYMBOL_COMPUTED_OPS (framefunc
) == &dwarf2_loclist_funcs
)
225 struct dwarf2_loclist_baton
*symbaton
;
227 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
228 *start
= dwarf2_find_location_expression (symbaton
, length
, pc
);
232 struct dwarf2_locexpr_baton
*symbaton
;
234 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
235 if (symbaton
!= NULL
)
237 *length
= symbaton
->size
;
238 *start
= symbaton
->data
;
245 error (_("Could not find the frame base for \"%s\"."),
246 SYMBOL_NATURAL_NAME (framefunc
));
249 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
250 the frame in BATON. */
253 dwarf_expr_frame_cfa (void *baton
)
255 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
257 return dwarf2_frame_cfa (debaton
->frame
);
260 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
261 the frame in BATON. */
264 dwarf_expr_frame_pc (void *baton
)
266 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
268 return get_frame_address_in_block (debaton
->frame
);
271 /* Using the objfile specified in BATON, find the address for the
272 current thread's thread-local storage with offset OFFSET. */
274 dwarf_expr_tls_address (void *baton
, CORE_ADDR offset
)
276 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
277 struct objfile
*objfile
= dwarf2_per_cu_objfile (debaton
->per_cu
);
279 return target_translate_tls_address (objfile
, offset
);
282 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
283 current CU (as is PER_CU). State of the CTX is not affected by the
287 per_cu_dwarf_call (struct dwarf_expr_context
*ctx
, cu_offset die_offset
,
288 struct dwarf2_per_cu_data
*per_cu
,
289 CORE_ADDR (*get_frame_pc
) (void *baton
),
292 struct dwarf2_locexpr_baton block
;
294 block
= dwarf2_fetch_die_location_block (die_offset
, per_cu
,
295 get_frame_pc
, baton
);
297 /* DW_OP_call_ref is currently not supported. */
298 gdb_assert (block
.per_cu
== per_cu
);
300 dwarf_expr_eval (ctx
, block
.data
, block
.size
);
303 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
306 dwarf_expr_dwarf_call (struct dwarf_expr_context
*ctx
, cu_offset die_offset
)
308 struct dwarf_expr_baton
*debaton
= ctx
->baton
;
310 per_cu_dwarf_call (ctx
, die_offset
, debaton
->per_cu
,
311 ctx
->funcs
->get_frame_pc
, ctx
->baton
);
314 /* Callback function for dwarf2_evaluate_loc_desc. */
317 dwarf_expr_get_base_type (struct dwarf_expr_context
*ctx
,
318 cu_offset die_offset
)
320 struct dwarf_expr_baton
*debaton
= ctx
->baton
;
322 return dwarf2_get_die_type (die_offset
, debaton
->per_cu
);
325 /* See dwarf2loc.h. */
327 int entry_values_debug
= 0;
329 /* Helper to set entry_values_debug. */
332 show_entry_values_debug (struct ui_file
*file
, int from_tty
,
333 struct cmd_list_element
*c
, const char *value
)
335 fprintf_filtered (file
,
336 _("Entry values and tail call frames debugging is %s.\n"),
340 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
341 CALLER_FRAME (for registers) can be NULL if it is not known. This function
342 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
345 call_site_to_target_addr (struct gdbarch
*call_site_gdbarch
,
346 struct call_site
*call_site
,
347 struct frame_info
*caller_frame
)
349 switch (FIELD_LOC_KIND (call_site
->target
))
351 case FIELD_LOC_KIND_DWARF_BLOCK
:
353 struct dwarf2_locexpr_baton
*dwarf_block
;
355 struct type
*caller_core_addr_type
;
356 struct gdbarch
*caller_arch
;
358 dwarf_block
= FIELD_DWARF_BLOCK (call_site
->target
);
359 if (dwarf_block
== NULL
)
361 struct minimal_symbol
*msym
;
363 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
364 throw_error (NO_ENTRY_VALUE_ERROR
,
365 _("DW_AT_GNU_call_site_target is not specified "
367 paddress (call_site_gdbarch
, call_site
->pc
),
368 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
371 if (caller_frame
== NULL
)
373 struct minimal_symbol
*msym
;
375 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
376 throw_error (NO_ENTRY_VALUE_ERROR
,
377 _("DW_AT_GNU_call_site_target DWARF block resolving "
378 "requires known frame which is currently not "
379 "available at %s in %s"),
380 paddress (call_site_gdbarch
, call_site
->pc
),
381 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
384 caller_arch
= get_frame_arch (caller_frame
);
385 caller_core_addr_type
= builtin_type (caller_arch
)->builtin_func_ptr
;
386 val
= dwarf2_evaluate_loc_desc (caller_core_addr_type
, caller_frame
,
387 dwarf_block
->data
, dwarf_block
->size
,
388 dwarf_block
->per_cu
);
389 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
391 if (VALUE_LVAL (val
) == lval_memory
)
392 return value_address (val
);
394 return value_as_address (val
);
397 case FIELD_LOC_KIND_PHYSNAME
:
399 const char *physname
;
400 struct minimal_symbol
*msym
;
402 physname
= FIELD_STATIC_PHYSNAME (call_site
->target
);
403 msym
= lookup_minimal_symbol_text (physname
, NULL
);
406 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
407 throw_error (NO_ENTRY_VALUE_ERROR
,
408 _("Cannot find function \"%s\" for a call site target "
410 physname
, paddress (call_site_gdbarch
, call_site
->pc
),
411 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
414 return SYMBOL_VALUE_ADDRESS (msym
);
417 case FIELD_LOC_KIND_PHYSADDR
:
418 return FIELD_STATIC_PHYSADDR (call_site
->target
);
421 internal_error (__FILE__
, __LINE__
, _("invalid call site target kind"));
425 /* Convert function entry point exact address ADDR to the function which is
426 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
427 NO_ENTRY_VALUE_ERROR otherwise. */
429 static struct symbol
*
430 func_addr_to_tail_call_list (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
432 struct symbol
*sym
= find_pc_function (addr
);
435 if (sym
== NULL
|| BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)) != addr
)
436 throw_error (NO_ENTRY_VALUE_ERROR
,
437 _("DW_TAG_GNU_call_site resolving failed to find function "
438 "name for address %s"),
439 paddress (gdbarch
, addr
));
441 type
= SYMBOL_TYPE (sym
);
442 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FUNC
);
443 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FUNC
);
448 /* Verify function with entry point exact address ADDR can never call itself
449 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
450 can call itself via tail calls.
452 If a funtion can tail call itself its entry value based parameters are
453 unreliable. There is no verification whether the value of some/all
454 parameters is unchanged through the self tail call, we expect if there is
455 a self tail call all the parameters can be modified. */
458 func_verify_no_selftailcall (struct gdbarch
*gdbarch
, CORE_ADDR verify_addr
)
460 struct obstack addr_obstack
;
461 struct cleanup
*old_chain
;
464 /* Track here CORE_ADDRs which were already visited. */
467 /* The verification is completely unordered. Track here function addresses
468 which still need to be iterated. */
469 VEC (CORE_ADDR
) *todo
= NULL
;
471 obstack_init (&addr_obstack
);
472 old_chain
= make_cleanup_obstack_free (&addr_obstack
);
473 addr_hash
= htab_create_alloc_ex (64, core_addr_hash
, core_addr_eq
, NULL
,
474 &addr_obstack
, hashtab_obstack_allocate
,
476 make_cleanup_htab_delete (addr_hash
);
478 make_cleanup (VEC_cleanup (CORE_ADDR
), &todo
);
480 VEC_safe_push (CORE_ADDR
, todo
, verify_addr
);
481 while (!VEC_empty (CORE_ADDR
, todo
))
483 struct symbol
*func_sym
;
484 struct call_site
*call_site
;
486 addr
= VEC_pop (CORE_ADDR
, todo
);
488 func_sym
= func_addr_to_tail_call_list (gdbarch
, addr
);
490 for (call_site
= TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym
));
491 call_site
; call_site
= call_site
->tail_call_next
)
493 CORE_ADDR target_addr
;
496 /* CALLER_FRAME with registers is not available for tail-call jumped
498 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, NULL
);
500 if (target_addr
== verify_addr
)
502 struct minimal_symbol
*msym
;
504 msym
= lookup_minimal_symbol_by_pc (verify_addr
);
505 throw_error (NO_ENTRY_VALUE_ERROR
,
506 _("DW_OP_GNU_entry_value resolving has found "
507 "function \"%s\" at %s can call itself via tail "
509 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
),
510 paddress (gdbarch
, verify_addr
));
513 slot
= htab_find_slot (addr_hash
, &target_addr
, INSERT
);
516 *slot
= obstack_copy (&addr_obstack
, &target_addr
,
517 sizeof (target_addr
));
518 VEC_safe_push (CORE_ADDR
, todo
, target_addr
);
523 do_cleanups (old_chain
);
526 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
527 ENTRY_VALUES_DEBUG. */
530 tailcall_dump (struct gdbarch
*gdbarch
, const struct call_site
*call_site
)
532 CORE_ADDR addr
= call_site
->pc
;
533 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (addr
- 1);
535 fprintf_unfiltered (gdb_stdlog
, " %s(%s)", paddress (gdbarch
, addr
),
536 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
540 /* vec.h needs single word type name, typedef it. */
541 typedef struct call_site
*call_sitep
;
543 /* Define VEC (call_sitep) functions. */
544 DEF_VEC_P (call_sitep
);
546 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
547 only top callers and bottom callees which are present in both. GDBARCH is
548 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
549 no remaining possibilities to provide unambiguous non-trivial result.
550 RESULTP should point to NULL on the first (initialization) call. Caller is
551 responsible for xfree of any RESULTP data. */
554 chain_candidate (struct gdbarch
*gdbarch
, struct call_site_chain
**resultp
,
555 VEC (call_sitep
) *chain
)
557 struct call_site_chain
*result
= *resultp
;
558 long length
= VEC_length (call_sitep
, chain
);
559 int callers
, callees
, idx
;
563 /* Create the initial chain containing all the passed PCs. */
565 result
= xmalloc (sizeof (*result
) + sizeof (*result
->call_site
)
567 result
->length
= length
;
568 result
->callers
= result
->callees
= length
;
569 memcpy (result
->call_site
, VEC_address (call_sitep
, chain
),
570 sizeof (*result
->call_site
) * length
);
573 if (entry_values_debug
)
575 fprintf_unfiltered (gdb_stdlog
, "tailcall: initial:");
576 for (idx
= 0; idx
< length
; idx
++)
577 tailcall_dump (gdbarch
, result
->call_site
[idx
]);
578 fputc_unfiltered ('\n', gdb_stdlog
);
584 if (entry_values_debug
)
586 fprintf_unfiltered (gdb_stdlog
, "tailcall: compare:");
587 for (idx
= 0; idx
< length
; idx
++)
588 tailcall_dump (gdbarch
, VEC_index (call_sitep
, chain
, idx
));
589 fputc_unfiltered ('\n', gdb_stdlog
);
592 /* Intersect callers. */
594 callers
= min (result
->callers
, length
);
595 for (idx
= 0; idx
< callers
; idx
++)
596 if (result
->call_site
[idx
] != VEC_index (call_sitep
, chain
, idx
))
598 result
->callers
= idx
;
602 /* Intersect callees. */
604 callees
= min (result
->callees
, length
);
605 for (idx
= 0; idx
< callees
; idx
++)
606 if (result
->call_site
[result
->length
- 1 - idx
]
607 != VEC_index (call_sitep
, chain
, length
- 1 - idx
))
609 result
->callees
= idx
;
613 if (entry_values_debug
)
615 fprintf_unfiltered (gdb_stdlog
, "tailcall: reduced:");
616 for (idx
= 0; idx
< result
->callers
; idx
++)
617 tailcall_dump (gdbarch
, result
->call_site
[idx
]);
618 fputs_unfiltered (" |", gdb_stdlog
);
619 for (idx
= 0; idx
< result
->callees
; idx
++)
620 tailcall_dump (gdbarch
, result
->call_site
[result
->length
621 - result
->callees
+ idx
]);
622 fputc_unfiltered ('\n', gdb_stdlog
);
625 if (result
->callers
== 0 && result
->callees
== 0)
627 /* There are no common callers or callees. It could be also a direct
628 call (which has length 0) with ambiguous possibility of an indirect
629 call - CALLERS == CALLEES == 0 is valid during the first allocation
630 but any subsequence processing of such entry means ambiguity. */
636 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
637 PC again. In such case there must be two different code paths to reach
638 it, therefore some of the former determined intermediate PCs must differ
639 and the unambiguous chain gets shortened. */
640 gdb_assert (result
->callers
+ result
->callees
< result
->length
);
643 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
644 assumed frames between them use GDBARCH. Use depth first search so we can
645 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
646 would have needless GDB stack overhead. Caller is responsible for xfree of
647 the returned result. Any unreliability results in thrown
648 NO_ENTRY_VALUE_ERROR. */
650 static struct call_site_chain
*
651 call_site_find_chain_1 (struct gdbarch
*gdbarch
, CORE_ADDR caller_pc
,
654 struct obstack addr_obstack
;
655 struct cleanup
*back_to_retval
, *back_to_workdata
;
656 struct call_site_chain
*retval
= NULL
;
657 struct call_site
*call_site
;
659 /* Mark CALL_SITEs so we do not visit the same ones twice. */
662 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
663 call_site nor any possible call_site at CALLEE_PC's function is there.
664 Any CALL_SITE in CHAIN will be iterated to its siblings - via
665 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
666 VEC (call_sitep
) *chain
= NULL
;
668 /* We are not interested in the specific PC inside the callee function. */
669 callee_pc
= get_pc_function_start (callee_pc
);
671 throw_error (NO_ENTRY_VALUE_ERROR
, _("Unable to find function for PC %s"),
672 paddress (gdbarch
, callee_pc
));
674 back_to_retval
= make_cleanup (free_current_contents
, &retval
);
676 obstack_init (&addr_obstack
);
677 back_to_workdata
= make_cleanup_obstack_free (&addr_obstack
);
678 addr_hash
= htab_create_alloc_ex (64, core_addr_hash
, core_addr_eq
, NULL
,
679 &addr_obstack
, hashtab_obstack_allocate
,
681 make_cleanup_htab_delete (addr_hash
);
683 make_cleanup (VEC_cleanup (call_sitep
), &chain
);
685 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
686 at the target's function. All the possible tail call sites in the
687 target's function will get iterated as already pushed into CHAIN via their
689 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
693 CORE_ADDR target_func_addr
;
694 struct call_site
*target_call_site
;
696 /* CALLER_FRAME with registers is not available for tail-call jumped
698 target_func_addr
= call_site_to_target_addr (gdbarch
, call_site
, NULL
);
700 if (target_func_addr
== callee_pc
)
702 chain_candidate (gdbarch
, &retval
, chain
);
706 /* There is no way to reach CALLEE_PC again as we would prevent
707 entering it twice as being already marked in ADDR_HASH. */
708 target_call_site
= NULL
;
712 struct symbol
*target_func
;
714 target_func
= func_addr_to_tail_call_list (gdbarch
, target_func_addr
);
715 target_call_site
= TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func
));
720 /* Attempt to visit TARGET_CALL_SITE. */
722 if (target_call_site
)
726 slot
= htab_find_slot (addr_hash
, &target_call_site
->pc
, INSERT
);
729 /* Successfully entered TARGET_CALL_SITE. */
731 *slot
= &target_call_site
->pc
;
732 VEC_safe_push (call_sitep
, chain
, target_call_site
);
737 /* Backtrack (without revisiting the originating call_site). Try the
738 callers's sibling; if there isn't any try the callers's callers's
741 target_call_site
= NULL
;
742 while (!VEC_empty (call_sitep
, chain
))
744 call_site
= VEC_pop (call_sitep
, chain
);
746 gdb_assert (htab_find_slot (addr_hash
, &call_site
->pc
,
748 htab_remove_elt (addr_hash
, &call_site
->pc
);
750 target_call_site
= call_site
->tail_call_next
;
751 if (target_call_site
)
755 while (target_call_site
);
757 if (VEC_empty (call_sitep
, chain
))
760 call_site
= VEC_last (call_sitep
, chain
);
765 struct minimal_symbol
*msym_caller
, *msym_callee
;
767 msym_caller
= lookup_minimal_symbol_by_pc (caller_pc
);
768 msym_callee
= lookup_minimal_symbol_by_pc (callee_pc
);
769 throw_error (NO_ENTRY_VALUE_ERROR
,
770 _("There are no unambiguously determinable intermediate "
771 "callers or callees between caller function \"%s\" at %s "
772 "and callee function \"%s\" at %s"),
774 ? "???" : SYMBOL_PRINT_NAME (msym_caller
)),
775 paddress (gdbarch
, caller_pc
),
777 ? "???" : SYMBOL_PRINT_NAME (msym_callee
)),
778 paddress (gdbarch
, callee_pc
));
781 do_cleanups (back_to_workdata
);
782 discard_cleanups (back_to_retval
);
786 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
787 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
788 constructed return NULL. Caller is responsible for xfree of the returned
791 struct call_site_chain
*
792 call_site_find_chain (struct gdbarch
*gdbarch
, CORE_ADDR caller_pc
,
795 volatile struct gdb_exception e
;
796 struct call_site_chain
*retval
= NULL
;
798 TRY_CATCH (e
, RETURN_MASK_ERROR
)
800 retval
= call_site_find_chain_1 (gdbarch
, caller_pc
, callee_pc
);
804 if (e
.error
== NO_ENTRY_VALUE_ERROR
)
806 if (entry_values_debug
)
807 exception_print (gdb_stdout
, e
);
817 /* Fetch call_site_parameter from caller matching the parameters. FRAME is for
818 callee. See DWARF_REG and FB_OFFSET description at struct
819 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
821 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
824 static struct call_site_parameter
*
825 dwarf_expr_reg_to_entry_parameter (struct frame_info
*frame
, int dwarf_reg
,
827 struct dwarf2_per_cu_data
**per_cu_return
)
829 CORE_ADDR func_addr
= get_frame_func (frame
);
831 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
832 struct frame_info
*caller_frame
= get_prev_frame (frame
);
833 struct call_site
*call_site
;
835 /* Initialize it just to avoid a GCC false warning. */
836 struct call_site_parameter
*parameter
= NULL
;
837 CORE_ADDR target_addr
;
839 if (gdbarch
!= frame_unwind_arch (frame
))
841 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (func_addr
);
842 struct gdbarch
*caller_gdbarch
= frame_unwind_arch (frame
);
844 throw_error (NO_ENTRY_VALUE_ERROR
,
845 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
846 "(of %s (%s)) does not match caller gdbarch %s"),
847 gdbarch_bfd_arch_info (gdbarch
)->printable_name
,
848 paddress (gdbarch
, func_addr
),
849 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
),
850 gdbarch_bfd_arch_info (caller_gdbarch
)->printable_name
);
853 if (caller_frame
== NULL
)
855 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (func_addr
);
857 throw_error (NO_ENTRY_VALUE_ERROR
, _("DW_OP_GNU_entry_value resolving "
858 "requires caller of %s (%s)"),
859 paddress (gdbarch
, func_addr
),
860 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
862 caller_pc
= get_frame_pc (caller_frame
);
863 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
865 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, caller_frame
);
866 if (target_addr
!= func_addr
)
868 struct minimal_symbol
*target_msym
, *func_msym
;
870 target_msym
= lookup_minimal_symbol_by_pc (target_addr
);
871 func_msym
= lookup_minimal_symbol_by_pc (func_addr
);
872 throw_error (NO_ENTRY_VALUE_ERROR
,
873 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
874 "but the called frame is for %s at %s"),
875 (target_msym
== NULL
? "???"
876 : SYMBOL_PRINT_NAME (target_msym
)),
877 paddress (gdbarch
, target_addr
),
878 func_msym
== NULL
? "???" : SYMBOL_PRINT_NAME (func_msym
),
879 paddress (gdbarch
, func_addr
));
882 /* No entry value based parameters would be reliable if this function can
883 call itself via tail calls. */
884 func_verify_no_selftailcall (gdbarch
, func_addr
);
886 for (iparams
= 0; iparams
< call_site
->parameter_count
; iparams
++)
888 parameter
= &call_site
->parameter
[iparams
];
889 if (parameter
->dwarf_reg
== -1 && dwarf_reg
== -1)
891 if (parameter
->fb_offset
== fb_offset
)
894 else if (parameter
->dwarf_reg
== dwarf_reg
)
897 if (iparams
== call_site
->parameter_count
)
899 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (caller_pc
);
901 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
902 determine its value. */
903 throw_error (NO_ENTRY_VALUE_ERROR
, _("Cannot find matching parameter "
904 "at DW_TAG_GNU_call_site %s at %s"),
905 paddress (gdbarch
, caller_pc
),
906 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
909 *per_cu_return
= call_site
->per_cu
;
913 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
914 the normal DW_AT_GNU_call_site_value block. Otherwise return the
915 DW_AT_GNU_call_site_data_value (dereferenced) block.
917 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
920 Function always returns non-NULL, non-optimized out value. It throws
921 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
923 static struct value
*
924 dwarf_entry_parameter_to_value (struct call_site_parameter
*parameter
,
925 CORE_ADDR deref_size
, struct type
*type
,
926 struct frame_info
*caller_frame
,
927 struct dwarf2_per_cu_data
*per_cu
)
929 const gdb_byte
*data_src
;
933 data_src
= deref_size
== -1 ? parameter
->value
: parameter
->data_value
;
934 size
= deref_size
== -1 ? parameter
->value_size
: parameter
->data_value_size
;
936 /* DEREF_SIZE size is not verified here. */
937 if (data_src
== NULL
)
938 throw_error (NO_ENTRY_VALUE_ERROR
,
939 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
941 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
942 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
944 data
= alloca (size
+ 1);
945 memcpy (data
, data_src
, size
);
946 data
[size
] = DW_OP_stack_value
;
948 return dwarf2_evaluate_loc_desc (type
, caller_frame
, data
, size
+ 1, per_cu
);
951 /* Execute call_site_parameter's DWARF block matching DEREF_SIZE for caller of
952 the CTX's frame. CTX must be of dwarf_expr_ctx_funcs kind. See DWARF_REG
953 and FB_OFFSET description at struct
954 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
956 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
957 can be more simple as it does not support cross-CU DWARF executions. */
960 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context
*ctx
,
961 int dwarf_reg
, CORE_ADDR fb_offset
,
964 struct dwarf_expr_baton
*debaton
;
965 struct frame_info
*frame
, *caller_frame
;
966 struct dwarf2_per_cu_data
*caller_per_cu
;
967 struct dwarf_expr_baton baton_local
;
968 struct dwarf_expr_context saved_ctx
;
969 struct call_site_parameter
*parameter
;
970 const gdb_byte
*data_src
;
973 gdb_assert (ctx
->funcs
== &dwarf_expr_ctx_funcs
);
974 debaton
= ctx
->baton
;
975 frame
= debaton
->frame
;
976 caller_frame
= get_prev_frame (frame
);
978 parameter
= dwarf_expr_reg_to_entry_parameter (frame
, dwarf_reg
, fb_offset
,
980 data_src
= deref_size
== -1 ? parameter
->value
: parameter
->data_value
;
981 size
= deref_size
== -1 ? parameter
->value_size
: parameter
->data_value_size
;
983 /* DEREF_SIZE size is not verified here. */
984 if (data_src
== NULL
)
985 throw_error (NO_ENTRY_VALUE_ERROR
,
986 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
988 baton_local
.frame
= caller_frame
;
989 baton_local
.per_cu
= caller_per_cu
;
991 saved_ctx
.gdbarch
= ctx
->gdbarch
;
992 saved_ctx
.addr_size
= ctx
->addr_size
;
993 saved_ctx
.offset
= ctx
->offset
;
994 saved_ctx
.baton
= ctx
->baton
;
995 ctx
->gdbarch
= get_objfile_arch (dwarf2_per_cu_objfile (baton_local
.per_cu
));
996 ctx
->addr_size
= dwarf2_per_cu_addr_size (baton_local
.per_cu
);
997 ctx
->offset
= dwarf2_per_cu_text_offset (baton_local
.per_cu
);
998 ctx
->baton
= &baton_local
;
1000 dwarf_expr_eval (ctx
, data_src
, size
);
1002 ctx
->gdbarch
= saved_ctx
.gdbarch
;
1003 ctx
->addr_size
= saved_ctx
.addr_size
;
1004 ctx
->offset
= saved_ctx
.offset
;
1005 ctx
->baton
= saved_ctx
.baton
;
1008 /* Callback function for dwarf2_evaluate_loc_desc.
1009 Fetch the address indexed by DW_OP_GNU_addr_index. */
1012 dwarf_expr_get_addr_index (void *baton
, unsigned int index
)
1014 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
1016 return dwarf2_read_addr_index (debaton
->per_cu
, index
);
1019 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1020 the indirect method on it, that is use its stored target value, the sole
1021 purpose of entry_data_value_funcs.. */
1023 static struct value
*
1024 entry_data_value_coerce_ref (const struct value
*value
)
1026 struct type
*checked_type
= check_typedef (value_type (value
));
1027 struct value
*target_val
;
1029 if (TYPE_CODE (checked_type
) != TYPE_CODE_REF
)
1032 target_val
= value_computed_closure (value
);
1033 value_incref (target_val
);
1037 /* Implement copy_closure. */
1040 entry_data_value_copy_closure (const struct value
*v
)
1042 struct value
*target_val
= value_computed_closure (v
);
1044 value_incref (target_val
);
1048 /* Implement free_closure. */
1051 entry_data_value_free_closure (struct value
*v
)
1053 struct value
*target_val
= value_computed_closure (v
);
1055 value_free (target_val
);
1058 /* Vector for methods for an entry value reference where the referenced value
1059 is stored in the caller. On the first dereference use
1060 DW_AT_GNU_call_site_data_value in the caller. */
1062 static const struct lval_funcs entry_data_value_funcs
=
1066 NULL
, /* check_validity */
1067 NULL
, /* check_any_valid */
1068 NULL
, /* indirect */
1069 entry_data_value_coerce_ref
,
1070 NULL
, /* check_synthetic_pointer */
1071 entry_data_value_copy_closure
,
1072 entry_data_value_free_closure
1075 /* Read parameter of TYPE at (callee) FRAME's function entry. DWARF_REG and
1076 FB_OFFSET are used to match DW_AT_location at the caller's
1077 DW_TAG_GNU_call_site_parameter. See DWARF_REG and FB_OFFSET description at
1078 struct dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
1080 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1081 cannot resolve the parameter for any reason. */
1083 static struct value
*
1084 value_of_dwarf_reg_entry (struct type
*type
, struct frame_info
*frame
,
1085 int dwarf_reg
, CORE_ADDR fb_offset
)
1087 struct type
*checked_type
= check_typedef (type
);
1088 struct type
*target_type
= TYPE_TARGET_TYPE (checked_type
);
1089 struct frame_info
*caller_frame
= get_prev_frame (frame
);
1090 struct value
*outer_val
, *target_val
, *val
;
1091 struct call_site_parameter
*parameter
;
1092 struct dwarf2_per_cu_data
*caller_per_cu
;
1095 parameter
= dwarf_expr_reg_to_entry_parameter (frame
, dwarf_reg
, fb_offset
,
1098 outer_val
= dwarf_entry_parameter_to_value (parameter
, -1 /* deref_size */,
1102 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1103 used and it is not available do not fall back to OUTER_VAL - dereferencing
1104 TYPE_CODE_REF with non-entry data value would give current value - not the
1107 if (TYPE_CODE (checked_type
) != TYPE_CODE_REF
1108 || TYPE_TARGET_TYPE (checked_type
) == NULL
)
1111 target_val
= dwarf_entry_parameter_to_value (parameter
,
1112 TYPE_LENGTH (target_type
),
1113 target_type
, caller_frame
,
1116 /* value_as_address dereferences TYPE_CODE_REF. */
1117 addr
= extract_typed_address (value_contents (outer_val
), checked_type
);
1119 /* The target entry value has artificial address of the entry value
1121 VALUE_LVAL (target_val
) = lval_memory
;
1122 set_value_address (target_val
, addr
);
1124 release_value (target_val
);
1125 val
= allocate_computed_value (type
, &entry_data_value_funcs
,
1126 target_val
/* closure */);
1128 /* Copy the referencing pointer to the new computed value. */
1129 memcpy (value_contents_raw (val
), value_contents_raw (outer_val
),
1130 TYPE_LENGTH (checked_type
));
1131 set_value_lazy (val
, 0);
1136 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1137 SIZE are DWARF block used to match DW_AT_location at the caller's
1138 DW_TAG_GNU_call_site_parameter.
1140 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1141 cannot resolve the parameter for any reason. */
1143 static struct value
*
1144 value_of_dwarf_block_entry (struct type
*type
, struct frame_info
*frame
,
1145 const gdb_byte
*block
, size_t block_len
)
1148 CORE_ADDR fb_offset
;
1150 dwarf_reg
= dwarf_block_to_dwarf_reg (block
, block
+ block_len
);
1151 if (dwarf_reg
!= -1)
1152 return value_of_dwarf_reg_entry (type
, frame
, dwarf_reg
, 0 /* unused */);
1154 if (dwarf_block_to_fb_offset (block
, block
+ block_len
, &fb_offset
))
1155 return value_of_dwarf_reg_entry (type
, frame
, -1, fb_offset
);
1157 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1158 suppressed during normal operation. The expression can be arbitrary if
1159 there is no caller-callee entry value binding expected. */
1160 throw_error (NO_ENTRY_VALUE_ERROR
,
1161 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1162 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1165 struct piece_closure
1167 /* Reference count. */
1170 /* The CU from which this closure's expression came. */
1171 struct dwarf2_per_cu_data
*per_cu
;
1173 /* The number of pieces used to describe this variable. */
1176 /* The target address size, used only for DWARF_VALUE_STACK. */
1179 /* The pieces themselves. */
1180 struct dwarf_expr_piece
*pieces
;
1183 /* Allocate a closure for a value formed from separately-described
1186 static struct piece_closure
*
1187 allocate_piece_closure (struct dwarf2_per_cu_data
*per_cu
,
1188 int n_pieces
, struct dwarf_expr_piece
*pieces
,
1191 struct piece_closure
*c
= XZALLOC (struct piece_closure
);
1196 c
->n_pieces
= n_pieces
;
1197 c
->addr_size
= addr_size
;
1198 c
->pieces
= XCALLOC (n_pieces
, struct dwarf_expr_piece
);
1200 memcpy (c
->pieces
, pieces
, n_pieces
* sizeof (struct dwarf_expr_piece
));
1201 for (i
= 0; i
< n_pieces
; ++i
)
1202 if (c
->pieces
[i
].location
== DWARF_VALUE_STACK
)
1203 value_incref (c
->pieces
[i
].v
.value
);
1208 /* The lowest-level function to extract bits from a byte buffer.
1209 SOURCE is the buffer. It is updated if we read to the end of a
1211 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1212 updated to reflect the number of bits actually read.
1213 NBITS is the number of bits we want to read. It is updated to
1214 reflect the number of bits actually read. This function may read
1216 BITS_BIG_ENDIAN is taken directly from gdbarch.
1217 This function returns the extracted bits. */
1220 extract_bits_primitive (const gdb_byte
**source
,
1221 unsigned int *source_offset_bits
,
1222 int *nbits
, int bits_big_endian
)
1224 unsigned int avail
, mask
, datum
;
1226 gdb_assert (*source_offset_bits
< 8);
1228 avail
= 8 - *source_offset_bits
;
1232 mask
= (1 << avail
) - 1;
1234 if (bits_big_endian
)
1235 datum
>>= 8 - (*source_offset_bits
+ *nbits
);
1237 datum
>>= *source_offset_bits
;
1241 *source_offset_bits
+= avail
;
1242 if (*source_offset_bits
>= 8)
1244 *source_offset_bits
-= 8;
1251 /* Extract some bits from a source buffer and move forward in the
1254 SOURCE is the source buffer. It is updated as bytes are read.
1255 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1257 NBITS is the number of bits to read.
1258 BITS_BIG_ENDIAN is taken directly from gdbarch.
1260 This function returns the bits that were read. */
1263 extract_bits (const gdb_byte
**source
, unsigned int *source_offset_bits
,
1264 int nbits
, int bits_big_endian
)
1268 gdb_assert (nbits
> 0 && nbits
<= 8);
1270 datum
= extract_bits_primitive (source
, source_offset_bits
, &nbits
,
1276 more
= extract_bits_primitive (source
, source_offset_bits
, &nbits
,
1278 if (bits_big_endian
)
1288 /* Write some bits into a buffer and move forward in the buffer.
1290 DATUM is the bits to write. The low-order bits of DATUM are used.
1291 DEST is the destination buffer. It is updated as bytes are
1293 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1295 NBITS is the number of valid bits in DATUM.
1296 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1299 insert_bits (unsigned int datum
,
1300 gdb_byte
*dest
, unsigned int dest_offset_bits
,
1301 int nbits
, int bits_big_endian
)
1305 gdb_assert (dest_offset_bits
+ nbits
<= 8);
1307 mask
= (1 << nbits
) - 1;
1308 if (bits_big_endian
)
1310 datum
<<= 8 - (dest_offset_bits
+ nbits
);
1311 mask
<<= 8 - (dest_offset_bits
+ nbits
);
1315 datum
<<= dest_offset_bits
;
1316 mask
<<= dest_offset_bits
;
1319 gdb_assert ((datum
& ~mask
) == 0);
1321 *dest
= (*dest
& ~mask
) | datum
;
1324 /* Copy bits from a source to a destination.
1326 DEST is where the bits should be written.
1327 DEST_OFFSET_BITS is the bit offset into DEST.
1328 SOURCE is the source of bits.
1329 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1330 BIT_COUNT is the number of bits to copy.
1331 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1334 copy_bitwise (gdb_byte
*dest
, unsigned int dest_offset_bits
,
1335 const gdb_byte
*source
, unsigned int source_offset_bits
,
1336 unsigned int bit_count
,
1337 int bits_big_endian
)
1339 unsigned int dest_avail
;
1342 /* Reduce everything to byte-size pieces. */
1343 dest
+= dest_offset_bits
/ 8;
1344 dest_offset_bits
%= 8;
1345 source
+= source_offset_bits
/ 8;
1346 source_offset_bits
%= 8;
1348 dest_avail
= 8 - dest_offset_bits
% 8;
1350 /* See if we can fill the first destination byte. */
1351 if (dest_avail
< bit_count
)
1353 datum
= extract_bits (&source
, &source_offset_bits
, dest_avail
,
1355 insert_bits (datum
, dest
, dest_offset_bits
, dest_avail
, bits_big_endian
);
1357 dest_offset_bits
= 0;
1358 bit_count
-= dest_avail
;
1361 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1362 than 8 bits remaining. */
1363 gdb_assert (dest_offset_bits
% 8 == 0 || bit_count
< 8);
1364 for (; bit_count
>= 8; bit_count
-= 8)
1366 datum
= extract_bits (&source
, &source_offset_bits
, 8, bits_big_endian
);
1367 *dest
++ = (gdb_byte
) datum
;
1370 /* Finally, we may have a few leftover bits. */
1371 gdb_assert (bit_count
<= 8 - dest_offset_bits
% 8);
1374 datum
= extract_bits (&source
, &source_offset_bits
, bit_count
,
1376 insert_bits (datum
, dest
, dest_offset_bits
, bit_count
, bits_big_endian
);
1381 read_pieced_value (struct value
*v
)
1385 ULONGEST bits_to_skip
;
1387 struct piece_closure
*c
1388 = (struct piece_closure
*) value_computed_closure (v
);
1389 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (v
));
1391 size_t buffer_size
= 0;
1392 char *buffer
= NULL
;
1393 struct cleanup
*cleanup
;
1395 = gdbarch_bits_big_endian (get_type_arch (value_type (v
)));
1397 if (value_type (v
) != value_enclosing_type (v
))
1398 internal_error (__FILE__
, __LINE__
,
1399 _("Should not be able to create a lazy value with "
1400 "an enclosing type"));
1402 cleanup
= make_cleanup (free_current_contents
, &buffer
);
1404 contents
= value_contents_raw (v
);
1405 bits_to_skip
= 8 * value_offset (v
);
1406 if (value_bitsize (v
))
1408 bits_to_skip
+= value_bitpos (v
);
1409 type_len
= value_bitsize (v
);
1412 type_len
= 8 * TYPE_LENGTH (value_type (v
));
1414 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
1416 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1417 size_t this_size
, this_size_bits
;
1418 long dest_offset_bits
, source_offset_bits
, source_offset
;
1419 const gdb_byte
*intermediate_buffer
;
1421 /* Compute size, source, and destination offsets for copying, in
1423 this_size_bits
= p
->size
;
1424 if (bits_to_skip
> 0 && bits_to_skip
>= this_size_bits
)
1426 bits_to_skip
-= this_size_bits
;
1429 if (this_size_bits
> type_len
- offset
)
1430 this_size_bits
= type_len
- offset
;
1431 if (bits_to_skip
> 0)
1433 dest_offset_bits
= 0;
1434 source_offset_bits
= bits_to_skip
;
1435 this_size_bits
-= bits_to_skip
;
1440 dest_offset_bits
= offset
;
1441 source_offset_bits
= 0;
1444 this_size
= (this_size_bits
+ source_offset_bits
% 8 + 7) / 8;
1445 source_offset
= source_offset_bits
/ 8;
1446 if (buffer_size
< this_size
)
1448 buffer_size
= this_size
;
1449 buffer
= xrealloc (buffer
, buffer_size
);
1451 intermediate_buffer
= buffer
;
1453 /* Copy from the source to DEST_BUFFER. */
1454 switch (p
->location
)
1456 case DWARF_VALUE_REGISTER
:
1458 struct gdbarch
*arch
= get_frame_arch (frame
);
1459 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->v
.regno
);
1460 int reg_offset
= source_offset
;
1462 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
1463 && this_size
< register_size (arch
, gdb_regnum
))
1465 /* Big-endian, and we want less than full size. */
1466 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
1467 /* We want the lower-order THIS_SIZE_BITS of the bytes
1468 we extract from the register. */
1469 source_offset_bits
+= 8 * this_size
- this_size_bits
;
1472 if (gdb_regnum
!= -1)
1476 if (!get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1480 /* Just so garbage doesn't ever shine through. */
1481 memset (buffer
, 0, this_size
);
1484 set_value_optimized_out (v
, 1);
1486 mark_value_bytes_unavailable (v
, offset
, this_size
);
1491 error (_("Unable to access DWARF register number %s"),
1492 paddress (arch
, p
->v
.regno
));
1497 case DWARF_VALUE_MEMORY
:
1498 read_value_memory (v
, offset
,
1499 p
->v
.mem
.in_stack_memory
,
1500 p
->v
.mem
.addr
+ source_offset
,
1504 case DWARF_VALUE_STACK
:
1506 size_t n
= this_size
;
1508 if (n
> c
->addr_size
- source_offset
)
1509 n
= (c
->addr_size
>= source_offset
1510 ? c
->addr_size
- source_offset
1518 const gdb_byte
*val_bytes
= value_contents_all (p
->v
.value
);
1520 intermediate_buffer
= val_bytes
+ source_offset
;
1525 case DWARF_VALUE_LITERAL
:
1527 size_t n
= this_size
;
1529 if (n
> p
->v
.literal
.length
- source_offset
)
1530 n
= (p
->v
.literal
.length
>= source_offset
1531 ? p
->v
.literal
.length
- source_offset
1534 intermediate_buffer
= p
->v
.literal
.data
+ source_offset
;
1538 /* These bits show up as zeros -- but do not cause the value
1539 to be considered optimized-out. */
1540 case DWARF_VALUE_IMPLICIT_POINTER
:
1543 case DWARF_VALUE_OPTIMIZED_OUT
:
1544 set_value_optimized_out (v
, 1);
1548 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
1551 if (p
->location
!= DWARF_VALUE_OPTIMIZED_OUT
1552 && p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
1553 copy_bitwise (contents
, dest_offset_bits
,
1554 intermediate_buffer
, source_offset_bits
% 8,
1555 this_size_bits
, bits_big_endian
);
1557 offset
+= this_size_bits
;
1560 do_cleanups (cleanup
);
1564 write_pieced_value (struct value
*to
, struct value
*from
)
1568 ULONGEST bits_to_skip
;
1569 const gdb_byte
*contents
;
1570 struct piece_closure
*c
1571 = (struct piece_closure
*) value_computed_closure (to
);
1572 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (to
));
1574 size_t buffer_size
= 0;
1575 char *buffer
= NULL
;
1576 struct cleanup
*cleanup
;
1578 = gdbarch_bits_big_endian (get_type_arch (value_type (to
)));
1582 set_value_optimized_out (to
, 1);
1586 cleanup
= make_cleanup (free_current_contents
, &buffer
);
1588 contents
= value_contents (from
);
1589 bits_to_skip
= 8 * value_offset (to
);
1590 if (value_bitsize (to
))
1592 bits_to_skip
+= value_bitpos (to
);
1593 type_len
= value_bitsize (to
);
1596 type_len
= 8 * TYPE_LENGTH (value_type (to
));
1598 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
1600 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1601 size_t this_size_bits
, this_size
;
1602 long dest_offset_bits
, source_offset_bits
, dest_offset
, source_offset
;
1604 const gdb_byte
*source_buffer
;
1606 this_size_bits
= p
->size
;
1607 if (bits_to_skip
> 0 && bits_to_skip
>= this_size_bits
)
1609 bits_to_skip
-= this_size_bits
;
1612 if (this_size_bits
> type_len
- offset
)
1613 this_size_bits
= type_len
- offset
;
1614 if (bits_to_skip
> 0)
1616 dest_offset_bits
= bits_to_skip
;
1617 source_offset_bits
= 0;
1618 this_size_bits
-= bits_to_skip
;
1623 dest_offset_bits
= 0;
1624 source_offset_bits
= offset
;
1627 this_size
= (this_size_bits
+ source_offset_bits
% 8 + 7) / 8;
1628 source_offset
= source_offset_bits
/ 8;
1629 dest_offset
= dest_offset_bits
/ 8;
1630 if (dest_offset_bits
% 8 == 0 && source_offset_bits
% 8 == 0)
1632 source_buffer
= contents
+ source_offset
;
1637 if (buffer_size
< this_size
)
1639 buffer_size
= this_size
;
1640 buffer
= xrealloc (buffer
, buffer_size
);
1642 source_buffer
= buffer
;
1646 switch (p
->location
)
1648 case DWARF_VALUE_REGISTER
:
1650 struct gdbarch
*arch
= get_frame_arch (frame
);
1651 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->v
.regno
);
1652 int reg_offset
= dest_offset
;
1654 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
1655 && this_size
<= register_size (arch
, gdb_regnum
))
1656 /* Big-endian, and we want less than full size. */
1657 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
1659 if (gdb_regnum
!= -1)
1665 if (!get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1670 error (_("Can't do read-modify-write to "
1671 "update bitfield; containing word has been "
1674 throw_error (NOT_AVAILABLE_ERROR
,
1675 _("Can't do read-modify-write to update "
1676 "bitfield; containing word "
1679 copy_bitwise (buffer
, dest_offset_bits
,
1680 contents
, source_offset_bits
,
1685 put_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1686 this_size
, source_buffer
);
1690 error (_("Unable to write to DWARF register number %s"),
1691 paddress (arch
, p
->v
.regno
));
1695 case DWARF_VALUE_MEMORY
:
1698 /* Only the first and last bytes can possibly have any
1700 read_memory (p
->v
.mem
.addr
+ dest_offset
, buffer
, 1);
1701 read_memory (p
->v
.mem
.addr
+ dest_offset
+ this_size
- 1,
1702 buffer
+ this_size
- 1, 1);
1703 copy_bitwise (buffer
, dest_offset_bits
,
1704 contents
, source_offset_bits
,
1709 write_memory (p
->v
.mem
.addr
+ dest_offset
,
1710 source_buffer
, this_size
);
1713 set_value_optimized_out (to
, 1);
1716 offset
+= this_size_bits
;
1719 do_cleanups (cleanup
);
1722 /* A helper function that checks bit validity in a pieced value.
1723 CHECK_FOR indicates the kind of validity checking.
1724 DWARF_VALUE_MEMORY means to check whether any bit is valid.
1725 DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
1727 DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
1728 implicit pointer. */
1731 check_pieced_value_bits (const struct value
*value
, int bit_offset
,
1733 enum dwarf_value_location check_for
)
1735 struct piece_closure
*c
1736 = (struct piece_closure
*) value_computed_closure (value
);
1738 int validity
= (check_for
== DWARF_VALUE_MEMORY
1739 || check_for
== DWARF_VALUE_IMPLICIT_POINTER
);
1741 bit_offset
+= 8 * value_offset (value
);
1742 if (value_bitsize (value
))
1743 bit_offset
+= value_bitpos (value
);
1745 for (i
= 0; i
< c
->n_pieces
&& bit_length
> 0; i
++)
1747 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1748 size_t this_size_bits
= p
->size
;
1752 if (bit_offset
>= this_size_bits
)
1754 bit_offset
-= this_size_bits
;
1758 bit_length
-= this_size_bits
- bit_offset
;
1762 bit_length
-= this_size_bits
;
1764 if (check_for
== DWARF_VALUE_IMPLICIT_POINTER
)
1766 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
1769 else if (p
->location
== DWARF_VALUE_OPTIMIZED_OUT
1770 || p
->location
== DWARF_VALUE_IMPLICIT_POINTER
)
1786 check_pieced_value_validity (const struct value
*value
, int bit_offset
,
1789 return check_pieced_value_bits (value
, bit_offset
, bit_length
,
1790 DWARF_VALUE_MEMORY
);
1794 check_pieced_value_invalid (const struct value
*value
)
1796 return check_pieced_value_bits (value
, 0,
1797 8 * TYPE_LENGTH (value_type (value
)),
1798 DWARF_VALUE_OPTIMIZED_OUT
);
1801 /* An implementation of an lval_funcs method to see whether a value is
1802 a synthetic pointer. */
1805 check_pieced_synthetic_pointer (const struct value
*value
, int bit_offset
,
1808 return check_pieced_value_bits (value
, bit_offset
, bit_length
,
1809 DWARF_VALUE_IMPLICIT_POINTER
);
1812 /* A wrapper function for get_frame_address_in_block. */
1815 get_frame_address_in_block_wrapper (void *baton
)
1817 return get_frame_address_in_block (baton
);
1820 /* An implementation of an lval_funcs method to indirect through a
1821 pointer. This handles the synthetic pointer case when needed. */
1823 static struct value
*
1824 indirect_pieced_value (struct value
*value
)
1826 struct piece_closure
*c
1827 = (struct piece_closure
*) value_computed_closure (value
);
1829 struct frame_info
*frame
;
1830 struct dwarf2_locexpr_baton baton
;
1831 int i
, bit_offset
, bit_length
;
1832 struct dwarf_expr_piece
*piece
= NULL
;
1833 LONGEST byte_offset
;
1835 type
= check_typedef (value_type (value
));
1836 if (TYPE_CODE (type
) != TYPE_CODE_PTR
)
1839 bit_length
= 8 * TYPE_LENGTH (type
);
1840 bit_offset
= 8 * value_offset (value
);
1841 if (value_bitsize (value
))
1842 bit_offset
+= value_bitpos (value
);
1844 for (i
= 0; i
< c
->n_pieces
&& bit_length
> 0; i
++)
1846 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1847 size_t this_size_bits
= p
->size
;
1851 if (bit_offset
>= this_size_bits
)
1853 bit_offset
-= this_size_bits
;
1857 bit_length
-= this_size_bits
- bit_offset
;
1861 bit_length
-= this_size_bits
;
1863 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
1866 if (bit_length
!= 0)
1867 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
1873 frame
= get_selected_frame (_("No frame selected."));
1875 /* This is an offset requested by GDB, such as value subcripts. */
1876 byte_offset
= value_as_address (value
);
1879 baton
= dwarf2_fetch_die_location_block (piece
->v
.ptr
.die
, c
->per_cu
,
1880 get_frame_address_in_block_wrapper
,
1883 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type
), frame
,
1884 baton
.data
, baton
.size
, baton
.per_cu
,
1885 piece
->v
.ptr
.offset
+ byte_offset
);
1889 copy_pieced_value_closure (const struct value
*v
)
1891 struct piece_closure
*c
1892 = (struct piece_closure
*) value_computed_closure (v
);
1899 free_pieced_value_closure (struct value
*v
)
1901 struct piece_closure
*c
1902 = (struct piece_closure
*) value_computed_closure (v
);
1909 for (i
= 0; i
< c
->n_pieces
; ++i
)
1910 if (c
->pieces
[i
].location
== DWARF_VALUE_STACK
)
1911 value_free (c
->pieces
[i
].v
.value
);
1918 /* Functions for accessing a variable described by DW_OP_piece. */
1919 static const struct lval_funcs pieced_value_funcs
= {
1922 check_pieced_value_validity
,
1923 check_pieced_value_invalid
,
1924 indirect_pieced_value
,
1925 NULL
, /* coerce_ref */
1926 check_pieced_synthetic_pointer
,
1927 copy_pieced_value_closure
,
1928 free_pieced_value_closure
1931 /* Helper function which throws an error if a synthetic pointer is
1935 invalid_synthetic_pointer (void)
1937 error (_("access outside bounds of object "
1938 "referenced via synthetic pointer"));
1941 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
1943 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs
=
1945 dwarf_expr_read_reg
,
1946 dwarf_expr_read_mem
,
1947 dwarf_expr_frame_base
,
1948 dwarf_expr_frame_cfa
,
1949 dwarf_expr_frame_pc
,
1950 dwarf_expr_tls_address
,
1951 dwarf_expr_dwarf_call
,
1952 dwarf_expr_get_base_type
,
1953 dwarf_expr_push_dwarf_reg_entry_value
,
1954 dwarf_expr_get_addr_index
1957 /* Evaluate a location description, starting at DATA and with length
1958 SIZE, to find the current location of variable of TYPE in the
1959 context of FRAME. BYTE_OFFSET is applied after the contents are
1962 static struct value
*
1963 dwarf2_evaluate_loc_desc_full (struct type
*type
, struct frame_info
*frame
,
1964 const gdb_byte
*data
, unsigned short size
,
1965 struct dwarf2_per_cu_data
*per_cu
,
1966 LONGEST byte_offset
)
1968 struct value
*retval
;
1969 struct dwarf_expr_baton baton
;
1970 struct dwarf_expr_context
*ctx
;
1971 struct cleanup
*old_chain
, *value_chain
;
1972 struct objfile
*objfile
= dwarf2_per_cu_objfile (per_cu
);
1973 volatile struct gdb_exception ex
;
1975 if (byte_offset
< 0)
1976 invalid_synthetic_pointer ();
1979 return allocate_optimized_out_value (type
);
1981 baton
.frame
= frame
;
1982 baton
.per_cu
= per_cu
;
1984 ctx
= new_dwarf_expr_context ();
1985 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
1986 value_chain
= make_cleanup_value_free_to_mark (value_mark ());
1988 ctx
->gdbarch
= get_objfile_arch (objfile
);
1989 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
1990 ctx
->ref_addr_size
= dwarf2_per_cu_ref_addr_size (per_cu
);
1991 ctx
->offset
= dwarf2_per_cu_text_offset (per_cu
);
1992 ctx
->baton
= &baton
;
1993 ctx
->funcs
= &dwarf_expr_ctx_funcs
;
1995 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
1997 dwarf_expr_eval (ctx
, data
, size
);
2001 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2003 do_cleanups (old_chain
);
2004 retval
= allocate_value (type
);
2005 mark_value_bytes_unavailable (retval
, 0, TYPE_LENGTH (type
));
2008 else if (ex
.error
== NO_ENTRY_VALUE_ERROR
)
2010 if (entry_values_debug
)
2011 exception_print (gdb_stdout
, ex
);
2012 do_cleanups (old_chain
);
2013 return allocate_optimized_out_value (type
);
2016 throw_exception (ex
);
2019 if (ctx
->num_pieces
> 0)
2021 struct piece_closure
*c
;
2022 struct frame_id frame_id
= get_frame_id (frame
);
2023 ULONGEST bit_size
= 0;
2026 for (i
= 0; i
< ctx
->num_pieces
; ++i
)
2027 bit_size
+= ctx
->pieces
[i
].size
;
2028 if (8 * (byte_offset
+ TYPE_LENGTH (type
)) > bit_size
)
2029 invalid_synthetic_pointer ();
2031 c
= allocate_piece_closure (per_cu
, ctx
->num_pieces
, ctx
->pieces
,
2033 /* We must clean up the value chain after creating the piece
2034 closure but before allocating the result. */
2035 do_cleanups (value_chain
);
2036 retval
= allocate_computed_value (type
, &pieced_value_funcs
, c
);
2037 VALUE_FRAME_ID (retval
) = frame_id
;
2038 set_value_offset (retval
, byte_offset
);
2042 switch (ctx
->location
)
2044 case DWARF_VALUE_REGISTER
:
2046 struct gdbarch
*arch
= get_frame_arch (frame
);
2047 ULONGEST dwarf_regnum
= value_as_long (dwarf_expr_fetch (ctx
, 0));
2048 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_regnum
);
2050 if (byte_offset
!= 0)
2051 error (_("cannot use offset on synthetic pointer to register"));
2052 do_cleanups (value_chain
);
2053 if (gdb_regnum
!= -1)
2054 retval
= value_from_register (type
, gdb_regnum
, frame
);
2056 error (_("Unable to access DWARF register number %s"),
2057 paddress (arch
, dwarf_regnum
));
2061 case DWARF_VALUE_MEMORY
:
2063 CORE_ADDR address
= dwarf_expr_fetch_address (ctx
, 0);
2064 int in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
2066 do_cleanups (value_chain
);
2067 retval
= allocate_value_lazy (type
);
2068 VALUE_LVAL (retval
) = lval_memory
;
2069 if (in_stack_memory
)
2070 set_value_stack (retval
, 1);
2071 set_value_address (retval
, address
+ byte_offset
);
2075 case DWARF_VALUE_STACK
:
2077 struct value
*value
= dwarf_expr_fetch (ctx
, 0);
2079 const gdb_byte
*val_bytes
;
2080 size_t n
= TYPE_LENGTH (value_type (value
));
2082 if (byte_offset
+ TYPE_LENGTH (type
) > n
)
2083 invalid_synthetic_pointer ();
2085 val_bytes
= value_contents_all (value
);
2086 val_bytes
+= byte_offset
;
2089 /* Preserve VALUE because we are going to free values back
2090 to the mark, but we still need the value contents
2092 value_incref (value
);
2093 do_cleanups (value_chain
);
2094 make_cleanup_value_free (value
);
2096 retval
= allocate_value (type
);
2097 contents
= value_contents_raw (retval
);
2098 if (n
> TYPE_LENGTH (type
))
2100 struct gdbarch
*objfile_gdbarch
= get_objfile_arch (objfile
);
2102 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
2103 val_bytes
+= n
- TYPE_LENGTH (type
);
2104 n
= TYPE_LENGTH (type
);
2106 memcpy (contents
, val_bytes
, n
);
2110 case DWARF_VALUE_LITERAL
:
2113 const bfd_byte
*ldata
;
2114 size_t n
= ctx
->len
;
2116 if (byte_offset
+ TYPE_LENGTH (type
) > n
)
2117 invalid_synthetic_pointer ();
2119 do_cleanups (value_chain
);
2120 retval
= allocate_value (type
);
2121 contents
= value_contents_raw (retval
);
2123 ldata
= ctx
->data
+ byte_offset
;
2126 if (n
> TYPE_LENGTH (type
))
2128 struct gdbarch
*objfile_gdbarch
= get_objfile_arch (objfile
);
2130 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
2131 ldata
+= n
- TYPE_LENGTH (type
);
2132 n
= TYPE_LENGTH (type
);
2134 memcpy (contents
, ldata
, n
);
2138 case DWARF_VALUE_OPTIMIZED_OUT
:
2139 do_cleanups (value_chain
);
2140 retval
= allocate_optimized_out_value (type
);
2143 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2144 operation by execute_stack_op. */
2145 case DWARF_VALUE_IMPLICIT_POINTER
:
2146 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2147 it can only be encountered when making a piece. */
2149 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
2153 set_value_initialized (retval
, ctx
->initialized
);
2155 do_cleanups (old_chain
);
2160 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2161 passes 0 as the byte_offset. */
2164 dwarf2_evaluate_loc_desc (struct type
*type
, struct frame_info
*frame
,
2165 const gdb_byte
*data
, unsigned short size
,
2166 struct dwarf2_per_cu_data
*per_cu
)
2168 return dwarf2_evaluate_loc_desc_full (type
, frame
, data
, size
, per_cu
, 0);
2172 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
2174 struct needs_frame_baton
2177 struct dwarf2_per_cu_data
*per_cu
;
2180 /* Reads from registers do require a frame. */
2182 needs_frame_read_reg (void *baton
, int regnum
)
2184 struct needs_frame_baton
*nf_baton
= baton
;
2186 nf_baton
->needs_frame
= 1;
2190 /* Reads from memory do not require a frame. */
2192 needs_frame_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
2194 memset (buf
, 0, len
);
2197 /* Frame-relative accesses do require a frame. */
2199 needs_frame_frame_base (void *baton
, const gdb_byte
**start
, size_t * length
)
2201 static gdb_byte lit0
= DW_OP_lit0
;
2202 struct needs_frame_baton
*nf_baton
= baton
;
2207 nf_baton
->needs_frame
= 1;
2210 /* CFA accesses require a frame. */
2213 needs_frame_frame_cfa (void *baton
)
2215 struct needs_frame_baton
*nf_baton
= baton
;
2217 nf_baton
->needs_frame
= 1;
2221 /* Thread-local accesses do require a frame. */
2223 needs_frame_tls_address (void *baton
, CORE_ADDR offset
)
2225 struct needs_frame_baton
*nf_baton
= baton
;
2227 nf_baton
->needs_frame
= 1;
2231 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
2234 needs_frame_dwarf_call (struct dwarf_expr_context
*ctx
, cu_offset die_offset
)
2236 struct needs_frame_baton
*nf_baton
= ctx
->baton
;
2238 per_cu_dwarf_call (ctx
, die_offset
, nf_baton
->per_cu
,
2239 ctx
->funcs
->get_frame_pc
, ctx
->baton
);
2242 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
2245 needs_dwarf_reg_entry_value (struct dwarf_expr_context
*ctx
,
2246 int dwarf_reg
, CORE_ADDR fb_offset
, int deref_size
)
2248 struct needs_frame_baton
*nf_baton
= ctx
->baton
;
2250 nf_baton
->needs_frame
= 1;
2253 /* DW_OP_GNU_addr_index doesn't require a frame. */
2256 needs_get_addr_index (void *baton
, unsigned int index
)
2258 /* Nothing to do. */
2262 /* Virtual method table for dwarf2_loc_desc_needs_frame below. */
2264 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs
=
2266 needs_frame_read_reg
,
2267 needs_frame_read_mem
,
2268 needs_frame_frame_base
,
2269 needs_frame_frame_cfa
,
2270 needs_frame_frame_cfa
, /* get_frame_pc */
2271 needs_frame_tls_address
,
2272 needs_frame_dwarf_call
,
2273 NULL
, /* get_base_type */
2274 needs_dwarf_reg_entry_value
,
2275 needs_get_addr_index
2278 /* Return non-zero iff the location expression at DATA (length SIZE)
2279 requires a frame to evaluate. */
2282 dwarf2_loc_desc_needs_frame (const gdb_byte
*data
, unsigned short size
,
2283 struct dwarf2_per_cu_data
*per_cu
)
2285 struct needs_frame_baton baton
;
2286 struct dwarf_expr_context
*ctx
;
2288 struct cleanup
*old_chain
;
2289 struct objfile
*objfile
= dwarf2_per_cu_objfile (per_cu
);
2291 baton
.needs_frame
= 0;
2292 baton
.per_cu
= per_cu
;
2294 ctx
= new_dwarf_expr_context ();
2295 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
2296 make_cleanup_value_free_to_mark (value_mark ());
2298 ctx
->gdbarch
= get_objfile_arch (objfile
);
2299 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
2300 ctx
->ref_addr_size
= dwarf2_per_cu_ref_addr_size (per_cu
);
2301 ctx
->offset
= dwarf2_per_cu_text_offset (per_cu
);
2302 ctx
->baton
= &baton
;
2303 ctx
->funcs
= &needs_frame_ctx_funcs
;
2305 dwarf_expr_eval (ctx
, data
, size
);
2307 in_reg
= ctx
->location
== DWARF_VALUE_REGISTER
;
2309 if (ctx
->num_pieces
> 0)
2313 /* If the location has several pieces, and any of them are in
2314 registers, then we will need a frame to fetch them from. */
2315 for (i
= 0; i
< ctx
->num_pieces
; i
++)
2316 if (ctx
->pieces
[i
].location
== DWARF_VALUE_REGISTER
)
2320 do_cleanups (old_chain
);
2322 return baton
.needs_frame
|| in_reg
;
2325 /* A helper function that throws an unimplemented error mentioning a
2326 given DWARF operator. */
2329 unimplemented (unsigned int op
)
2331 const char *name
= get_DW_OP_name (op
);
2334 error (_("DWARF operator %s cannot be translated to an agent expression"),
2337 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2338 "to an agent expression"),
2342 /* A helper function to convert a DWARF register to an arch register.
2343 ARCH is the architecture.
2344 DWARF_REG is the register.
2345 This will throw an exception if the DWARF register cannot be
2346 translated to an architecture register. */
2349 translate_register (struct gdbarch
*arch
, int dwarf_reg
)
2351 int reg
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_reg
);
2353 error (_("Unable to access DWARF register number %d"), dwarf_reg
);
2357 /* A helper function that emits an access to memory. ARCH is the
2358 target architecture. EXPR is the expression which we are building.
2359 NBITS is the number of bits we want to read. This emits the
2360 opcodes needed to read the memory and then extract the desired
2364 access_memory (struct gdbarch
*arch
, struct agent_expr
*expr
, ULONGEST nbits
)
2366 ULONGEST nbytes
= (nbits
+ 7) / 8;
2368 gdb_assert (nbits
> 0 && nbits
<= sizeof (LONGEST
));
2371 ax_trace_quick (expr
, nbytes
);
2374 ax_simple (expr
, aop_ref8
);
2375 else if (nbits
<= 16)
2376 ax_simple (expr
, aop_ref16
);
2377 else if (nbits
<= 32)
2378 ax_simple (expr
, aop_ref32
);
2380 ax_simple (expr
, aop_ref64
);
2382 /* If we read exactly the number of bytes we wanted, we're done. */
2383 if (8 * nbytes
== nbits
)
2386 if (gdbarch_bits_big_endian (arch
))
2388 /* On a bits-big-endian machine, we want the high-order
2390 ax_const_l (expr
, 8 * nbytes
- nbits
);
2391 ax_simple (expr
, aop_rsh_unsigned
);
2395 /* On a bits-little-endian box, we want the low-order NBITS. */
2396 ax_zero_ext (expr
, nbits
);
2400 /* A helper function to return the frame's PC. */
2403 get_ax_pc (void *baton
)
2405 struct agent_expr
*expr
= baton
;
2410 /* Compile a DWARF location expression to an agent expression.
2412 EXPR is the agent expression we are building.
2413 LOC is the agent value we modify.
2414 ARCH is the architecture.
2415 ADDR_SIZE is the size of addresses, in bytes.
2416 OP_PTR is the start of the location expression.
2417 OP_END is one past the last byte of the location expression.
2419 This will throw an exception for various kinds of errors -- for
2420 example, if the expression cannot be compiled, or if the expression
2424 dwarf2_compile_expr_to_ax (struct agent_expr
*expr
, struct axs_value
*loc
,
2425 struct gdbarch
*arch
, unsigned int addr_size
,
2426 const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
2427 struct dwarf2_per_cu_data
*per_cu
)
2429 struct cleanup
*cleanups
;
2431 VEC(int) *dw_labels
= NULL
, *patches
= NULL
;
2432 const gdb_byte
* const base
= op_ptr
;
2433 const gdb_byte
*previous_piece
= op_ptr
;
2434 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
2435 ULONGEST bits_collected
= 0;
2436 unsigned int addr_size_bits
= 8 * addr_size
;
2437 int bits_big_endian
= gdbarch_bits_big_endian (arch
);
2439 offsets
= xmalloc ((op_end
- op_ptr
) * sizeof (int));
2440 cleanups
= make_cleanup (xfree
, offsets
);
2442 for (i
= 0; i
< op_end
- op_ptr
; ++i
)
2445 make_cleanup (VEC_cleanup (int), &dw_labels
);
2446 make_cleanup (VEC_cleanup (int), &patches
);
2448 /* By default we are making an address. */
2449 loc
->kind
= axs_lvalue_memory
;
2451 while (op_ptr
< op_end
)
2453 enum dwarf_location_atom op
= *op_ptr
;
2454 ULONGEST uoffset
, reg
;
2458 offsets
[op_ptr
- base
] = expr
->len
;
2461 /* Our basic approach to code generation is to map DWARF
2462 operations directly to AX operations. However, there are
2465 First, DWARF works on address-sized units, but AX always uses
2466 LONGEST. For most operations we simply ignore this
2467 difference; instead we generate sign extensions as needed
2468 before division and comparison operations. It would be nice
2469 to omit the sign extensions, but there is no way to determine
2470 the size of the target's LONGEST. (This code uses the size
2471 of the host LONGEST in some cases -- that is a bug but it is
2474 Second, some DWARF operations cannot be translated to AX.
2475 For these we simply fail. See
2476 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
2511 ax_const_l (expr
, op
- DW_OP_lit0
);
2515 uoffset
= extract_unsigned_integer (op_ptr
, addr_size
, byte_order
);
2516 op_ptr
+= addr_size
;
2517 /* Some versions of GCC emit DW_OP_addr before
2518 DW_OP_GNU_push_tls_address. In this case the value is an
2519 index, not an address. We don't support things like
2520 branching between the address and the TLS op. */
2521 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
2522 uoffset
+= dwarf2_per_cu_text_offset (per_cu
);
2523 ax_const_l (expr
, uoffset
);
2527 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 1, byte_order
));
2531 ax_const_l (expr
, extract_signed_integer (op_ptr
, 1, byte_order
));
2535 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 2, byte_order
));
2539 ax_const_l (expr
, extract_signed_integer (op_ptr
, 2, byte_order
));
2543 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 4, byte_order
));
2547 ax_const_l (expr
, extract_signed_integer (op_ptr
, 4, byte_order
));
2551 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 8, byte_order
));
2555 ax_const_l (expr
, extract_signed_integer (op_ptr
, 8, byte_order
));
2559 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
2560 ax_const_l (expr
, uoffset
);
2563 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2564 ax_const_l (expr
, offset
);
2599 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
2600 loc
->u
.reg
= translate_register (arch
, op
- DW_OP_reg0
);
2601 loc
->kind
= axs_lvalue_register
;
2605 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
2606 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
2607 loc
->u
.reg
= translate_register (arch
, reg
);
2608 loc
->kind
= axs_lvalue_register
;
2611 case DW_OP_implicit_value
:
2615 op_ptr
= read_uleb128 (op_ptr
, op_end
, &len
);
2616 if (op_ptr
+ len
> op_end
)
2617 error (_("DW_OP_implicit_value: too few bytes available."));
2618 if (len
> sizeof (ULONGEST
))
2619 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
2622 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, len
,
2625 dwarf_expr_require_composition (op_ptr
, op_end
,
2626 "DW_OP_implicit_value");
2628 loc
->kind
= axs_rvalue
;
2632 case DW_OP_stack_value
:
2633 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
2634 loc
->kind
= axs_rvalue
;
2669 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2670 i
= translate_register (arch
, op
- DW_OP_breg0
);
2674 ax_const_l (expr
, offset
);
2675 ax_simple (expr
, aop_add
);
2680 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
2681 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2682 i
= translate_register (arch
, reg
);
2686 ax_const_l (expr
, offset
);
2687 ax_simple (expr
, aop_add
);
2693 const gdb_byte
*datastart
;
2696 struct symbol
*framefunc
;
2697 LONGEST base_offset
= 0;
2699 b
= block_for_pc (expr
->scope
);
2702 error (_("No block found for address"));
2704 framefunc
= block_linkage_function (b
);
2707 error (_("No function found for block"));
2709 dwarf_expr_frame_base_1 (framefunc
, expr
->scope
,
2710 &datastart
, &datalen
);
2712 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2713 dwarf2_compile_expr_to_ax (expr
, loc
, arch
, addr_size
, datastart
,
2714 datastart
+ datalen
, per_cu
);
2718 ax_const_l (expr
, offset
);
2719 ax_simple (expr
, aop_add
);
2722 loc
->kind
= axs_lvalue_memory
;
2727 ax_simple (expr
, aop_dup
);
2731 ax_simple (expr
, aop_pop
);
2736 ax_pick (expr
, offset
);
2740 ax_simple (expr
, aop_swap
);
2748 ax_simple (expr
, aop_rot
);
2752 case DW_OP_deref_size
:
2756 if (op
== DW_OP_deref_size
)
2764 ax_simple (expr
, aop_ref8
);
2767 ax_simple (expr
, aop_ref16
);
2770 ax_simple (expr
, aop_ref32
);
2773 ax_simple (expr
, aop_ref64
);
2776 /* Note that get_DW_OP_name will never return
2778 error (_("Unsupported size %d in %s"),
2779 size
, get_DW_OP_name (op
));
2785 /* Sign extend the operand. */
2786 ax_ext (expr
, addr_size_bits
);
2787 ax_simple (expr
, aop_dup
);
2788 ax_const_l (expr
, 0);
2789 ax_simple (expr
, aop_less_signed
);
2790 ax_simple (expr
, aop_log_not
);
2791 i
= ax_goto (expr
, aop_if_goto
);
2792 /* We have to emit 0 - X. */
2793 ax_const_l (expr
, 0);
2794 ax_simple (expr
, aop_swap
);
2795 ax_simple (expr
, aop_sub
);
2796 ax_label (expr
, i
, expr
->len
);
2800 /* No need to sign extend here. */
2801 ax_const_l (expr
, 0);
2802 ax_simple (expr
, aop_swap
);
2803 ax_simple (expr
, aop_sub
);
2807 /* Sign extend the operand. */
2808 ax_ext (expr
, addr_size_bits
);
2809 ax_simple (expr
, aop_bit_not
);
2812 case DW_OP_plus_uconst
:
2813 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
2814 /* It would be really weird to emit `DW_OP_plus_uconst 0',
2815 but we micro-optimize anyhow. */
2818 ax_const_l (expr
, reg
);
2819 ax_simple (expr
, aop_add
);
2824 ax_simple (expr
, aop_bit_and
);
2828 /* Sign extend the operands. */
2829 ax_ext (expr
, addr_size_bits
);
2830 ax_simple (expr
, aop_swap
);
2831 ax_ext (expr
, addr_size_bits
);
2832 ax_simple (expr
, aop_swap
);
2833 ax_simple (expr
, aop_div_signed
);
2837 ax_simple (expr
, aop_sub
);
2841 ax_simple (expr
, aop_rem_unsigned
);
2845 ax_simple (expr
, aop_mul
);
2849 ax_simple (expr
, aop_bit_or
);
2853 ax_simple (expr
, aop_add
);
2857 ax_simple (expr
, aop_lsh
);
2861 ax_simple (expr
, aop_rsh_unsigned
);
2865 ax_simple (expr
, aop_rsh_signed
);
2869 ax_simple (expr
, aop_bit_xor
);
2873 /* Sign extend the operands. */
2874 ax_ext (expr
, addr_size_bits
);
2875 ax_simple (expr
, aop_swap
);
2876 ax_ext (expr
, addr_size_bits
);
2877 /* Note no swap here: A <= B is !(B < A). */
2878 ax_simple (expr
, aop_less_signed
);
2879 ax_simple (expr
, aop_log_not
);
2883 /* Sign extend the operands. */
2884 ax_ext (expr
, addr_size_bits
);
2885 ax_simple (expr
, aop_swap
);
2886 ax_ext (expr
, addr_size_bits
);
2887 ax_simple (expr
, aop_swap
);
2888 /* A >= B is !(A < B). */
2889 ax_simple (expr
, aop_less_signed
);
2890 ax_simple (expr
, aop_log_not
);
2894 /* Sign extend the operands. */
2895 ax_ext (expr
, addr_size_bits
);
2896 ax_simple (expr
, aop_swap
);
2897 ax_ext (expr
, addr_size_bits
);
2898 /* No need for a second swap here. */
2899 ax_simple (expr
, aop_equal
);
2903 /* Sign extend the operands. */
2904 ax_ext (expr
, addr_size_bits
);
2905 ax_simple (expr
, aop_swap
);
2906 ax_ext (expr
, addr_size_bits
);
2907 ax_simple (expr
, aop_swap
);
2908 ax_simple (expr
, aop_less_signed
);
2912 /* Sign extend the operands. */
2913 ax_ext (expr
, addr_size_bits
);
2914 ax_simple (expr
, aop_swap
);
2915 ax_ext (expr
, addr_size_bits
);
2916 /* Note no swap here: A > B is B < A. */
2917 ax_simple (expr
, aop_less_signed
);
2921 /* Sign extend the operands. */
2922 ax_ext (expr
, addr_size_bits
);
2923 ax_simple (expr
, aop_swap
);
2924 ax_ext (expr
, addr_size_bits
);
2925 /* No need for a swap here. */
2926 ax_simple (expr
, aop_equal
);
2927 ax_simple (expr
, aop_log_not
);
2930 case DW_OP_call_frame_cfa
:
2931 dwarf2_compile_cfa_to_ax (expr
, loc
, arch
, expr
->scope
, per_cu
);
2932 loc
->kind
= axs_lvalue_memory
;
2935 case DW_OP_GNU_push_tls_address
:
2940 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
2942 i
= ax_goto (expr
, aop_goto
);
2943 VEC_safe_push (int, dw_labels
, op_ptr
+ offset
- base
);
2944 VEC_safe_push (int, patches
, i
);
2948 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
2950 /* Zero extend the operand. */
2951 ax_zero_ext (expr
, addr_size_bits
);
2952 i
= ax_goto (expr
, aop_if_goto
);
2953 VEC_safe_push (int, dw_labels
, op_ptr
+ offset
- base
);
2954 VEC_safe_push (int, patches
, i
);
2961 case DW_OP_bit_piece
:
2963 ULONGEST size
, offset
;
2965 if (op_ptr
- 1 == previous_piece
)
2966 error (_("Cannot translate empty pieces to agent expressions"));
2967 previous_piece
= op_ptr
- 1;
2969 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
2970 if (op
== DW_OP_piece
)
2976 op_ptr
= read_uleb128 (op_ptr
, op_end
, &offset
);
2978 if (bits_collected
+ size
> 8 * sizeof (LONGEST
))
2979 error (_("Expression pieces exceed word size"));
2981 /* Access the bits. */
2984 case axs_lvalue_register
:
2985 ax_reg (expr
, loc
->u
.reg
);
2988 case axs_lvalue_memory
:
2989 /* Offset the pointer, if needed. */
2992 ax_const_l (expr
, offset
/ 8);
2993 ax_simple (expr
, aop_add
);
2996 access_memory (arch
, expr
, size
);
3000 /* For a bits-big-endian target, shift up what we already
3001 have. For a bits-little-endian target, shift up the
3002 new data. Note that there is a potential bug here if
3003 the DWARF expression leaves multiple values on the
3005 if (bits_collected
> 0)
3007 if (bits_big_endian
)
3009 ax_simple (expr
, aop_swap
);
3010 ax_const_l (expr
, size
);
3011 ax_simple (expr
, aop_lsh
);
3012 /* We don't need a second swap here, because
3013 aop_bit_or is symmetric. */
3017 ax_const_l (expr
, size
);
3018 ax_simple (expr
, aop_lsh
);
3020 ax_simple (expr
, aop_bit_or
);
3023 bits_collected
+= size
;
3024 loc
->kind
= axs_rvalue
;
3028 case DW_OP_GNU_uninit
:
3034 struct dwarf2_locexpr_baton block
;
3035 int size
= (op
== DW_OP_call2
? 2 : 4);
3038 uoffset
= extract_unsigned_integer (op_ptr
, size
, byte_order
);
3041 offset
.cu_off
= uoffset
;
3042 block
= dwarf2_fetch_die_location_block (offset
, per_cu
,
3045 /* DW_OP_call_ref is currently not supported. */
3046 gdb_assert (block
.per_cu
== per_cu
);
3048 dwarf2_compile_expr_to_ax (expr
, loc
, arch
, addr_size
,
3049 block
.data
, block
.data
+ block
.size
,
3054 case DW_OP_call_ref
:
3062 /* Patch all the branches we emitted. */
3063 for (i
= 0; i
< VEC_length (int, patches
); ++i
)
3065 int targ
= offsets
[VEC_index (int, dw_labels
, i
)];
3067 internal_error (__FILE__
, __LINE__
, _("invalid label"));
3068 ax_label (expr
, VEC_index (int, patches
, i
), targ
);
3071 do_cleanups (cleanups
);
3075 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3076 evaluator to calculate the location. */
3077 static struct value
*
3078 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
3080 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3083 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
3084 dlbaton
->size
, dlbaton
->per_cu
);
3089 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3090 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3093 static struct value
*
3094 locexpr_read_variable_at_entry (struct symbol
*symbol
, struct frame_info
*frame
)
3096 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3098 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
3102 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3104 locexpr_read_needs_frame (struct symbol
*symbol
)
3106 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3108 return dwarf2_loc_desc_needs_frame (dlbaton
->data
, dlbaton
->size
,
3112 /* Return true if DATA points to the end of a piece. END is one past
3113 the last byte in the expression. */
3116 piece_end_p (const gdb_byte
*data
, const gdb_byte
*end
)
3118 return data
== end
|| data
[0] == DW_OP_piece
|| data
[0] == DW_OP_bit_piece
;
3121 /* Helper for locexpr_describe_location_piece that finds the name of a
3125 locexpr_regname (struct gdbarch
*gdbarch
, int dwarf_regnum
)
3129 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, dwarf_regnum
);
3130 return gdbarch_register_name (gdbarch
, regnum
);
3133 /* Nicely describe a single piece of a location, returning an updated
3134 position in the bytecode sequence. This function cannot recognize
3135 all locations; if a location is not recognized, it simply returns
3138 static const gdb_byte
*
3139 locexpr_describe_location_piece (struct symbol
*symbol
, struct ui_file
*stream
,
3140 CORE_ADDR addr
, struct objfile
*objfile
,
3141 const gdb_byte
*data
, const gdb_byte
*end
,
3142 unsigned int addr_size
)
3144 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3146 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
3148 fprintf_filtered (stream
, _("a variable in $%s"),
3149 locexpr_regname (gdbarch
, data
[0] - DW_OP_reg0
));
3152 else if (data
[0] == DW_OP_regx
)
3156 data
= read_uleb128 (data
+ 1, end
, ®
);
3157 fprintf_filtered (stream
, _("a variable in $%s"),
3158 locexpr_regname (gdbarch
, reg
));
3160 else if (data
[0] == DW_OP_fbreg
)
3163 struct symbol
*framefunc
;
3165 LONGEST frame_offset
;
3166 const gdb_byte
*base_data
, *new_data
, *save_data
= data
;
3168 LONGEST base_offset
= 0;
3170 new_data
= read_sleb128 (data
+ 1, end
, &frame_offset
);
3171 if (!piece_end_p (new_data
, end
))
3175 b
= block_for_pc (addr
);
3178 error (_("No block found for address for symbol \"%s\"."),
3179 SYMBOL_PRINT_NAME (symbol
));
3181 framefunc
= block_linkage_function (b
);
3184 error (_("No function found for block for symbol \"%s\"."),
3185 SYMBOL_PRINT_NAME (symbol
));
3187 dwarf_expr_frame_base_1 (framefunc
, addr
, &base_data
, &base_size
);
3189 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
3191 const gdb_byte
*buf_end
;
3193 frame_reg
= base_data
[0] - DW_OP_breg0
;
3194 buf_end
= read_sleb128 (base_data
+ 1,
3195 base_data
+ base_size
, &base_offset
);
3196 if (buf_end
!= base_data
+ base_size
)
3197 error (_("Unexpected opcode after "
3198 "DW_OP_breg%u for symbol \"%s\"."),
3199 frame_reg
, SYMBOL_PRINT_NAME (symbol
));
3201 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
3203 /* The frame base is just the register, with no offset. */
3204 frame_reg
= base_data
[0] - DW_OP_reg0
;
3209 /* We don't know what to do with the frame base expression,
3210 so we can't trace this variable; give up. */
3214 fprintf_filtered (stream
,
3215 _("a variable at frame base reg $%s offset %s+%s"),
3216 locexpr_regname (gdbarch
, frame_reg
),
3217 plongest (base_offset
), plongest (frame_offset
));
3219 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
3220 && piece_end_p (data
, end
))
3224 data
= read_sleb128 (data
+ 1, end
, &offset
);
3226 fprintf_filtered (stream
,
3227 _("a variable at offset %s from base reg $%s"),
3229 locexpr_regname (gdbarch
, data
[0] - DW_OP_breg0
));
3232 /* The location expression for a TLS variable looks like this (on a
3235 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3236 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3238 0x3 is the encoding for DW_OP_addr, which has an operand as long
3239 as the size of an address on the target machine (here is 8
3240 bytes). Note that more recent version of GCC emit DW_OP_const4u
3241 or DW_OP_const8u, depending on address size, rather than
3242 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3243 The operand represents the offset at which the variable is within
3244 the thread local storage. */
3246 else if (data
+ 1 + addr_size
< end
3247 && (data
[0] == DW_OP_addr
3248 || (addr_size
== 4 && data
[0] == DW_OP_const4u
)
3249 || (addr_size
== 8 && data
[0] == DW_OP_const8u
))
3250 && data
[1 + addr_size
] == DW_OP_GNU_push_tls_address
3251 && piece_end_p (data
+ 2 + addr_size
, end
))
3254 offset
= extract_unsigned_integer (data
+ 1, addr_size
,
3255 gdbarch_byte_order (gdbarch
));
3257 fprintf_filtered (stream
,
3258 _("a thread-local variable at offset 0x%s "
3259 "in the thread-local storage for `%s'"),
3260 phex_nz (offset
, addr_size
), objfile
->name
);
3262 data
+= 1 + addr_size
+ 1;
3264 else if (data
[0] >= DW_OP_lit0
3265 && data
[0] <= DW_OP_lit31
3267 && data
[1] == DW_OP_stack_value
)
3269 fprintf_filtered (stream
, _("the constant %d"), data
[0] - DW_OP_lit0
);
3276 /* Disassemble an expression, stopping at the end of a piece or at the
3277 end of the expression. Returns a pointer to the next unread byte
3278 in the input expression. If ALL is nonzero, then this function
3279 will keep going until it reaches the end of the expression. */
3281 static const gdb_byte
*
3282 disassemble_dwarf_expression (struct ui_file
*stream
,
3283 struct gdbarch
*arch
, unsigned int addr_size
,
3284 int offset_size
, const gdb_byte
*start
,
3285 const gdb_byte
*data
, const gdb_byte
*end
,
3286 int indent
, int all
,
3287 struct dwarf2_per_cu_data
*per_cu
)
3291 || (data
[0] != DW_OP_piece
&& data
[0] != DW_OP_bit_piece
)))
3293 enum dwarf_location_atom op
= *data
++;
3298 name
= get_DW_OP_name (op
);
3301 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3302 op
, (long) (data
- 1 - start
));
3303 fprintf_filtered (stream
, " %*ld: %s", indent
+ 4,
3304 (long) (data
- 1 - start
), name
);
3309 ul
= extract_unsigned_integer (data
, addr_size
,
3310 gdbarch_byte_order (arch
));
3312 fprintf_filtered (stream
, " 0x%s", phex_nz (ul
, addr_size
));
3316 ul
= extract_unsigned_integer (data
, 1, gdbarch_byte_order (arch
));
3318 fprintf_filtered (stream
, " %s", pulongest (ul
));
3321 l
= extract_signed_integer (data
, 1, gdbarch_byte_order (arch
));
3323 fprintf_filtered (stream
, " %s", plongest (l
));
3326 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
3328 fprintf_filtered (stream
, " %s", pulongest (ul
));
3331 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3333 fprintf_filtered (stream
, " %s", plongest (l
));
3336 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
3338 fprintf_filtered (stream
, " %s", pulongest (ul
));
3341 l
= extract_signed_integer (data
, 4, gdbarch_byte_order (arch
));
3343 fprintf_filtered (stream
, " %s", plongest (l
));
3346 ul
= extract_unsigned_integer (data
, 8, gdbarch_byte_order (arch
));
3348 fprintf_filtered (stream
, " %s", pulongest (ul
));
3351 l
= extract_signed_integer (data
, 8, gdbarch_byte_order (arch
));
3353 fprintf_filtered (stream
, " %s", plongest (l
));
3356 data
= read_uleb128 (data
, end
, &ul
);
3357 fprintf_filtered (stream
, " %s", pulongest (ul
));
3360 data
= read_sleb128 (data
, end
, &l
);
3361 fprintf_filtered (stream
, " %s", plongest (l
));
3396 fprintf_filtered (stream
, " [$%s]",
3397 locexpr_regname (arch
, op
- DW_OP_reg0
));
3401 data
= read_uleb128 (data
, end
, &ul
);
3402 fprintf_filtered (stream
, " %s [$%s]", pulongest (ul
),
3403 locexpr_regname (arch
, (int) ul
));
3406 case DW_OP_implicit_value
:
3407 data
= read_uleb128 (data
, end
, &ul
);
3409 fprintf_filtered (stream
, " %s", pulongest (ul
));
3444 data
= read_sleb128 (data
, end
, &l
);
3445 fprintf_filtered (stream
, " %s [$%s]", plongest (l
),
3446 locexpr_regname (arch
, op
- DW_OP_breg0
));
3450 data
= read_uleb128 (data
, end
, &ul
);
3451 data
= read_sleb128 (data
, end
, &l
);
3452 fprintf_filtered (stream
, " register %s [$%s] offset %s",
3454 locexpr_regname (arch
, (int) ul
),
3459 data
= read_sleb128 (data
, end
, &l
);
3460 fprintf_filtered (stream
, " %s", plongest (l
));
3463 case DW_OP_xderef_size
:
3464 case DW_OP_deref_size
:
3466 fprintf_filtered (stream
, " %d", *data
);
3470 case DW_OP_plus_uconst
:
3471 data
= read_uleb128 (data
, end
, &ul
);
3472 fprintf_filtered (stream
, " %s", pulongest (ul
));
3476 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3478 fprintf_filtered (stream
, " to %ld",
3479 (long) (data
+ l
- start
));
3483 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3485 fprintf_filtered (stream
, " %ld",
3486 (long) (data
+ l
- start
));
3490 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
3492 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 2));
3496 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
3498 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 4));
3501 case DW_OP_call_ref
:
3502 ul
= extract_unsigned_integer (data
, offset_size
,
3503 gdbarch_byte_order (arch
));
3504 data
+= offset_size
;
3505 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, offset_size
));
3509 data
= read_uleb128 (data
, end
, &ul
);
3510 fprintf_filtered (stream
, " %s (bytes)", pulongest (ul
));
3513 case DW_OP_bit_piece
:
3517 data
= read_uleb128 (data
, end
, &ul
);
3518 data
= read_uleb128 (data
, end
, &offset
);
3519 fprintf_filtered (stream
, " size %s offset %s (bits)",
3520 pulongest (ul
), pulongest (offset
));
3524 case DW_OP_GNU_implicit_pointer
:
3526 ul
= extract_unsigned_integer (data
, offset_size
,
3527 gdbarch_byte_order (arch
));
3528 data
+= offset_size
;
3530 data
= read_sleb128 (data
, end
, &l
);
3532 fprintf_filtered (stream
, " DIE %s offset %s",
3533 phex_nz (ul
, offset_size
),
3538 case DW_OP_GNU_deref_type
:
3540 int addr_size
= *data
++;
3544 data
= read_uleb128 (data
, end
, &ul
);
3546 type
= dwarf2_get_die_type (offset
, per_cu
);
3547 fprintf_filtered (stream
, "<");
3548 type_print (type
, "", stream
, -1);
3549 fprintf_filtered (stream
, " [0x%s]> %d", phex_nz (offset
.cu_off
, 0),
3554 case DW_OP_GNU_const_type
:
3559 data
= read_uleb128 (data
, end
, &ul
);
3560 type_die
.cu_off
= ul
;
3561 type
= dwarf2_get_die_type (type_die
, per_cu
);
3562 fprintf_filtered (stream
, "<");
3563 type_print (type
, "", stream
, -1);
3564 fprintf_filtered (stream
, " [0x%s]>", phex_nz (type_die
.cu_off
, 0));
3568 case DW_OP_GNU_regval_type
:
3574 data
= read_uleb128 (data
, end
, ®
);
3575 data
= read_uleb128 (data
, end
, &ul
);
3576 type_die
.cu_off
= ul
;
3578 type
= dwarf2_get_die_type (type_die
, per_cu
);
3579 fprintf_filtered (stream
, "<");
3580 type_print (type
, "", stream
, -1);
3581 fprintf_filtered (stream
, " [0x%s]> [$%s]",
3582 phex_nz (type_die
.cu_off
, 0),
3583 locexpr_regname (arch
, reg
));
3587 case DW_OP_GNU_convert
:
3588 case DW_OP_GNU_reinterpret
:
3592 data
= read_uleb128 (data
, end
, &ul
);
3593 type_die
.cu_off
= ul
;
3595 if (type_die
.cu_off
== 0)
3596 fprintf_filtered (stream
, "<0>");
3601 type
= dwarf2_get_die_type (type_die
, per_cu
);
3602 fprintf_filtered (stream
, "<");
3603 type_print (type
, "", stream
, -1);
3604 fprintf_filtered (stream
, " [0x%s]>", phex_nz (type_die
.cu_off
, 0));
3609 case DW_OP_GNU_entry_value
:
3610 data
= read_uleb128 (data
, end
, &ul
);
3611 fputc_filtered ('\n', stream
);
3612 disassemble_dwarf_expression (stream
, arch
, addr_size
, offset_size
,
3613 start
, data
, data
+ ul
, indent
+ 2,
3619 fprintf_filtered (stream
, "\n");
3625 /* Describe a single location, which may in turn consist of multiple
3629 locexpr_describe_location_1 (struct symbol
*symbol
, CORE_ADDR addr
,
3630 struct ui_file
*stream
,
3631 const gdb_byte
*data
, int size
,
3632 struct objfile
*objfile
, unsigned int addr_size
,
3633 int offset_size
, struct dwarf2_per_cu_data
*per_cu
)
3635 const gdb_byte
*end
= data
+ size
;
3636 int first_piece
= 1, bad
= 0;
3640 const gdb_byte
*here
= data
;
3641 int disassemble
= 1;
3646 fprintf_filtered (stream
, _(", and "));
3648 if (!dwarf2_always_disassemble
)
3650 data
= locexpr_describe_location_piece (symbol
, stream
,
3652 data
, end
, addr_size
);
3653 /* If we printed anything, or if we have an empty piece,
3654 then don't disassemble. */
3656 || data
[0] == DW_OP_piece
3657 || data
[0] == DW_OP_bit_piece
)
3662 fprintf_filtered (stream
, _("a complex DWARF expression:\n"));
3663 data
= disassemble_dwarf_expression (stream
,
3664 get_objfile_arch (objfile
),
3665 addr_size
, offset_size
, data
,
3667 dwarf2_always_disassemble
,
3673 int empty
= data
== here
;
3676 fprintf_filtered (stream
, " ");
3677 if (data
[0] == DW_OP_piece
)
3681 data
= read_uleb128 (data
+ 1, end
, &bytes
);
3684 fprintf_filtered (stream
, _("an empty %s-byte piece"),
3687 fprintf_filtered (stream
, _(" [%s-byte piece]"),
3690 else if (data
[0] == DW_OP_bit_piece
)
3692 ULONGEST bits
, offset
;
3694 data
= read_uleb128 (data
+ 1, end
, &bits
);
3695 data
= read_uleb128 (data
, end
, &offset
);
3698 fprintf_filtered (stream
,
3699 _("an empty %s-bit piece"),
3702 fprintf_filtered (stream
,
3703 _(" [%s-bit piece, offset %s bits]"),
3704 pulongest (bits
), pulongest (offset
));
3714 if (bad
|| data
> end
)
3715 error (_("Corrupted DWARF2 expression for \"%s\"."),
3716 SYMBOL_PRINT_NAME (symbol
));
3719 /* Print a natural-language description of SYMBOL to STREAM. This
3720 version is for a symbol with a single location. */
3723 locexpr_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
3724 struct ui_file
*stream
)
3726 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3727 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
3728 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3729 int offset_size
= dwarf2_per_cu_offset_size (dlbaton
->per_cu
);
3731 locexpr_describe_location_1 (symbol
, addr
, stream
,
3732 dlbaton
->data
, dlbaton
->size
,
3733 objfile
, addr_size
, offset_size
,
3737 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3738 any necessary bytecode in AX. */
3741 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
3742 struct agent_expr
*ax
, struct axs_value
*value
)
3744 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3745 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3747 if (dlbaton
->size
== 0)
3748 value
->optimized_out
= 1;
3750 dwarf2_compile_expr_to_ax (ax
, value
, gdbarch
, addr_size
,
3751 dlbaton
->data
, dlbaton
->data
+ dlbaton
->size
,
3755 /* The set of location functions used with the DWARF-2 expression
3757 const struct symbol_computed_ops dwarf2_locexpr_funcs
= {
3758 locexpr_read_variable
,
3759 locexpr_read_variable_at_entry
,
3760 locexpr_read_needs_frame
,
3761 locexpr_describe_location
,
3762 locexpr_tracepoint_var_ref
3766 /* Wrapper functions for location lists. These generally find
3767 the appropriate location expression and call something above. */
3769 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3770 evaluator to calculate the location. */
3771 static struct value
*
3772 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
3774 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3776 const gdb_byte
*data
;
3778 CORE_ADDR pc
= frame
? get_frame_address_in_block (frame
) : 0;
3780 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
3781 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, data
, size
,
3787 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
3788 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3791 Function always returns non-NULL value, it may be marked optimized out if
3792 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
3793 if it cannot resolve the parameter for any reason. */
3795 static struct value
*
3796 loclist_read_variable_at_entry (struct symbol
*symbol
, struct frame_info
*frame
)
3798 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3799 const gdb_byte
*data
;
3803 if (frame
== NULL
|| !get_frame_func_if_available (frame
, &pc
))
3804 return allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
3806 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
3808 return allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
3810 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol
), frame
, data
, size
);
3813 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3815 loclist_read_needs_frame (struct symbol
*symbol
)
3817 /* If there's a location list, then assume we need to have a frame
3818 to choose the appropriate location expression. With tracking of
3819 global variables this is not necessarily true, but such tracking
3820 is disabled in GCC at the moment until we figure out how to
3826 /* Print a natural-language description of SYMBOL to STREAM. This
3827 version applies when there is a list of different locations, each
3828 with a specified address range. */
3831 loclist_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
3832 struct ui_file
*stream
)
3834 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3835 CORE_ADDR low
, high
;
3836 const gdb_byte
*loc_ptr
, *buf_end
;
3837 int length
, first
= 1;
3838 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
3839 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3840 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
3841 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3842 int offset_size
= dwarf2_per_cu_offset_size (dlbaton
->per_cu
);
3843 int signed_addr_p
= bfd_get_sign_extend_vma (objfile
->obfd
);
3844 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
3845 /* Adjust base_address for relocatable objects. */
3846 CORE_ADDR base_offset
= dwarf2_per_cu_text_offset (dlbaton
->per_cu
);
3847 CORE_ADDR base_address
= dlbaton
->base_address
+ base_offset
;
3849 loc_ptr
= dlbaton
->data
;
3850 buf_end
= dlbaton
->data
+ dlbaton
->size
;
3852 fprintf_filtered (stream
, _("multi-location:\n"));
3854 /* Iterate through locations until we run out. */
3857 if (buf_end
- loc_ptr
< 2 * addr_size
)
3858 error (_("Corrupted DWARF expression for symbol \"%s\"."),
3859 SYMBOL_PRINT_NAME (symbol
));
3862 low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
3864 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
3865 loc_ptr
+= addr_size
;
3868 high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
3870 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
3871 loc_ptr
+= addr_size
;
3873 /* A base-address-selection entry. */
3874 if ((low
& base_mask
) == base_mask
)
3876 base_address
= high
+ base_offset
;
3877 fprintf_filtered (stream
, _(" Base address %s"),
3878 paddress (gdbarch
, base_address
));
3882 /* An end-of-list entry. */
3883 if (low
== 0 && high
== 0)
3886 /* Otherwise, a location expression entry. */
3887 low
+= base_address
;
3888 high
+= base_address
;
3890 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
3893 /* (It would improve readability to print only the minimum
3894 necessary digits of the second number of the range.) */
3895 fprintf_filtered (stream
, _(" Range %s-%s: "),
3896 paddress (gdbarch
, low
), paddress (gdbarch
, high
));
3898 /* Now describe this particular location. */
3899 locexpr_describe_location_1 (symbol
, low
, stream
, loc_ptr
, length
,
3900 objfile
, addr_size
, offset_size
,
3903 fprintf_filtered (stream
, "\n");
3909 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3910 any necessary bytecode in AX. */
3912 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
3913 struct agent_expr
*ax
, struct axs_value
*value
)
3915 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3916 const gdb_byte
*data
;
3918 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3920 data
= dwarf2_find_location_expression (dlbaton
, &size
, ax
->scope
);
3922 value
->optimized_out
= 1;
3924 dwarf2_compile_expr_to_ax (ax
, value
, gdbarch
, addr_size
, data
, data
+ size
,
3928 /* The set of location functions used with the DWARF-2 expression
3929 evaluator and location lists. */
3930 const struct symbol_computed_ops dwarf2_loclist_funcs
= {
3931 loclist_read_variable
,
3932 loclist_read_variable_at_entry
,
3933 loclist_read_needs_frame
,
3934 loclist_describe_location
,
3935 loclist_tracepoint_var_ref
3938 /* Provide a prototype to silence -Wmissing-prototypes. */
3939 extern initialize_file_ftype _initialize_dwarf2loc
;
3942 _initialize_dwarf2loc (void)
3944 add_setshow_zinteger_cmd ("entry-values", class_maintenance
,
3945 &entry_values_debug
,
3946 _("Set entry values and tail call frames "
3948 _("Show entry values and tail call frames "
3950 _("When non-zero, the process of determining "
3951 "parameter values from function entry point "
3952 "and tail call frames will be printed."),
3954 show_entry_values_debug
,
3955 &setdebuglist
, &showdebuglist
);