1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
34 #include "exceptions.h"
39 #include "dwarf2expr.h"
40 #include "dwarf2loc.h"
41 #include "dwarf2-frame.h"
43 #include "gdb_string.h"
44 #include "gdb_assert.h"
46 extern int dwarf2_always_disassemble
;
48 static void dwarf_expr_frame_base_1 (struct symbol
*framefunc
, CORE_ADDR pc
,
49 const gdb_byte
**start
, size_t *length
);
51 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs
;
53 static struct value
*dwarf2_evaluate_loc_desc_full (struct type
*type
,
54 struct frame_info
*frame
,
57 struct dwarf2_per_cu_data
*per_cu
,
60 /* A function for dealing with location lists. Given a
61 symbol baton (BATON) and a pc value (PC), find the appropriate
62 location expression, set *LOCEXPR_LENGTH, and return a pointer
63 to the beginning of the expression. Returns NULL on failure.
65 For now, only return the first matching location expression; there
66 can be more than one in the list. */
69 dwarf2_find_location_expression (struct dwarf2_loclist_baton
*baton
,
70 size_t *locexpr_length
, CORE_ADDR pc
)
73 const gdb_byte
*loc_ptr
, *buf_end
;
75 struct objfile
*objfile
= dwarf2_per_cu_objfile (baton
->per_cu
);
76 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
77 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
78 unsigned int addr_size
= dwarf2_per_cu_addr_size (baton
->per_cu
);
79 int signed_addr_p
= bfd_get_sign_extend_vma (objfile
->obfd
);
80 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
81 /* Adjust base_address for relocatable objects. */
82 CORE_ADDR base_offset
= dwarf2_per_cu_text_offset (baton
->per_cu
);
83 CORE_ADDR base_address
= baton
->base_address
+ base_offset
;
85 loc_ptr
= baton
->data
;
86 buf_end
= baton
->data
+ baton
->size
;
90 if (buf_end
- loc_ptr
< 2 * addr_size
)
91 error (_("dwarf2_find_location_expression: "
92 "Corrupted DWARF expression."));
95 low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
97 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
101 high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
103 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
104 loc_ptr
+= addr_size
;
106 /* A base-address-selection entry. */
107 if ((low
& base_mask
) == base_mask
)
109 base_address
= high
+ base_offset
;
113 /* An end-of-list entry. */
114 if (low
== 0 && high
== 0)
117 /* Otherwise, a location expression entry. */
119 high
+= base_address
;
121 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
124 if (pc
>= low
&& pc
< high
)
126 *locexpr_length
= length
;
134 /* This is the baton used when performing dwarf2 expression
136 struct dwarf_expr_baton
138 struct frame_info
*frame
;
139 struct dwarf2_per_cu_data
*per_cu
;
142 /* Helper functions for dwarf2_evaluate_loc_desc. */
144 /* Using the frame specified in BATON, return the value of register
145 REGNUM, treated as a pointer. */
147 dwarf_expr_read_reg (void *baton
, int dwarf_regnum
)
149 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
150 struct gdbarch
*gdbarch
= get_frame_arch (debaton
->frame
);
154 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, dwarf_regnum
);
155 result
= address_from_register (builtin_type (gdbarch
)->builtin_data_ptr
,
156 regnum
, debaton
->frame
);
160 /* Read memory at ADDR (length LEN) into BUF. */
163 dwarf_expr_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
165 read_memory (addr
, buf
, len
);
168 /* Using the frame specified in BATON, find the location expression
169 describing the frame base. Return a pointer to it in START and
170 its length in LENGTH. */
172 dwarf_expr_frame_base (void *baton
, const gdb_byte
**start
, size_t * length
)
174 /* FIXME: cagney/2003-03-26: This code should be using
175 get_frame_base_address(), and then implement a dwarf2 specific
177 struct symbol
*framefunc
;
178 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
180 /* Use block_linkage_function, which returns a real (not inlined)
181 function, instead of get_frame_function, which may return an
183 framefunc
= block_linkage_function (get_frame_block (debaton
->frame
, NULL
));
185 /* If we found a frame-relative symbol then it was certainly within
186 some function associated with a frame. If we can't find the frame,
187 something has gone wrong. */
188 gdb_assert (framefunc
!= NULL
);
190 dwarf_expr_frame_base_1 (framefunc
,
191 get_frame_address_in_block (debaton
->frame
),
196 dwarf_expr_frame_base_1 (struct symbol
*framefunc
, CORE_ADDR pc
,
197 const gdb_byte
**start
, size_t *length
)
199 if (SYMBOL_LOCATION_BATON (framefunc
) == NULL
)
201 else if (SYMBOL_COMPUTED_OPS (framefunc
) == &dwarf2_loclist_funcs
)
203 struct dwarf2_loclist_baton
*symbaton
;
205 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
206 *start
= dwarf2_find_location_expression (symbaton
, length
, pc
);
210 struct dwarf2_locexpr_baton
*symbaton
;
212 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
213 if (symbaton
!= NULL
)
215 *length
= symbaton
->size
;
216 *start
= symbaton
->data
;
223 error (_("Could not find the frame base for \"%s\"."),
224 SYMBOL_NATURAL_NAME (framefunc
));
227 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
228 the frame in BATON. */
231 dwarf_expr_frame_cfa (void *baton
)
233 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
235 return dwarf2_frame_cfa (debaton
->frame
);
238 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
239 the frame in BATON. */
242 dwarf_expr_frame_pc (void *baton
)
244 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
246 return get_frame_address_in_block (debaton
->frame
);
249 /* Using the objfile specified in BATON, find the address for the
250 current thread's thread-local storage with offset OFFSET. */
252 dwarf_expr_tls_address (void *baton
, CORE_ADDR offset
)
254 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
255 struct objfile
*objfile
= dwarf2_per_cu_objfile (debaton
->per_cu
);
257 return target_translate_tls_address (objfile
, offset
);
260 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
261 current CU (as is PER_CU). State of the CTX is not affected by the
265 per_cu_dwarf_call (struct dwarf_expr_context
*ctx
, size_t die_offset
,
266 struct dwarf2_per_cu_data
*per_cu
,
267 CORE_ADDR (*get_frame_pc
) (void *baton
),
270 struct dwarf2_locexpr_baton block
;
272 block
= dwarf2_fetch_die_location_block (die_offset
, per_cu
,
273 get_frame_pc
, baton
);
275 /* DW_OP_call_ref is currently not supported. */
276 gdb_assert (block
.per_cu
== per_cu
);
278 dwarf_expr_eval (ctx
, block
.data
, block
.size
);
281 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
284 dwarf_expr_dwarf_call (struct dwarf_expr_context
*ctx
, size_t die_offset
)
286 struct dwarf_expr_baton
*debaton
= ctx
->baton
;
288 per_cu_dwarf_call (ctx
, die_offset
, debaton
->per_cu
,
289 ctx
->funcs
->get_frame_pc
, ctx
->baton
);
292 /* Callback function for dwarf2_evaluate_loc_desc. */
295 dwarf_expr_get_base_type (struct dwarf_expr_context
*ctx
, size_t die_offset
)
297 struct dwarf_expr_baton
*debaton
= ctx
->baton
;
299 return dwarf2_get_die_type (die_offset
, debaton
->per_cu
);
302 /* See dwarf2loc.h. */
304 int entry_values_debug
= 0;
306 /* Helper to set entry_values_debug. */
309 show_entry_values_debug (struct ui_file
*file
, int from_tty
,
310 struct cmd_list_element
*c
, const char *value
)
312 fprintf_filtered (file
,
313 _("Entry values and tail call frames debugging is %s.\n"),
317 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
318 CALLER_FRAME (for registers) can be NULL if it is not known. This function
319 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
322 call_site_to_target_addr (struct gdbarch
*call_site_gdbarch
,
323 struct call_site
*call_site
,
324 struct frame_info
*caller_frame
)
326 switch (FIELD_LOC_KIND (call_site
->target
))
328 case FIELD_LOC_KIND_DWARF_BLOCK
:
330 struct dwarf2_locexpr_baton
*dwarf_block
;
332 struct type
*caller_core_addr_type
;
333 struct gdbarch
*caller_arch
;
335 dwarf_block
= FIELD_DWARF_BLOCK (call_site
->target
);
336 if (dwarf_block
== NULL
)
338 struct minimal_symbol
*msym
;
340 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
341 throw_error (NO_ENTRY_VALUE_ERROR
,
342 _("DW_AT_GNU_call_site_target is not specified "
344 paddress (call_site_gdbarch
, call_site
->pc
),
345 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
348 if (caller_frame
== NULL
)
350 struct minimal_symbol
*msym
;
352 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
353 throw_error (NO_ENTRY_VALUE_ERROR
,
354 _("DW_AT_GNU_call_site_target DWARF block resolving "
355 "requires known frame which is currently not "
356 "available at %s in %s"),
357 paddress (call_site_gdbarch
, call_site
->pc
),
358 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
361 caller_arch
= get_frame_arch (caller_frame
);
362 caller_core_addr_type
= builtin_type (caller_arch
)->builtin_func_ptr
;
363 val
= dwarf2_evaluate_loc_desc (caller_core_addr_type
, caller_frame
,
364 dwarf_block
->data
, dwarf_block
->size
,
365 dwarf_block
->per_cu
);
366 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
368 if (VALUE_LVAL (val
) == lval_memory
)
369 return value_address (val
);
371 return value_as_address (val
);
374 case FIELD_LOC_KIND_PHYSNAME
:
376 const char *physname
;
377 struct minimal_symbol
*msym
;
379 physname
= FIELD_STATIC_PHYSNAME (call_site
->target
);
380 msym
= lookup_minimal_symbol_text (physname
, NULL
);
383 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
384 throw_error (NO_ENTRY_VALUE_ERROR
,
385 _("Cannot find function \"%s\" for a call site target "
387 physname
, paddress (call_site_gdbarch
, call_site
->pc
),
388 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
391 return SYMBOL_VALUE_ADDRESS (msym
);
394 case FIELD_LOC_KIND_PHYSADDR
:
395 return FIELD_STATIC_PHYSADDR (call_site
->target
);
398 internal_error (__FILE__
, __LINE__
, _("invalid call site target kind"));
402 /* Fetch call_site_parameter from caller matching the parameters. FRAME is for
403 callee. See DWARF_REG and FB_OFFSET description at struct
404 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
406 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
409 static struct call_site_parameter
*
410 dwarf_expr_reg_to_entry_parameter (struct frame_info
*frame
, int dwarf_reg
,
412 struct dwarf2_per_cu_data
**per_cu_return
)
414 CORE_ADDR func_addr
= get_frame_func (frame
);
416 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
417 struct frame_info
*caller_frame
= get_prev_frame (frame
);
418 struct call_site
*call_site
;
421 struct dwarf2_locexpr_baton
*dwarf_block
;
422 struct call_site_parameter
*parameter
;
423 CORE_ADDR target_addr
;
425 if (gdbarch
!= frame_unwind_arch (frame
))
427 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (func_addr
);
428 struct gdbarch
*caller_gdbarch
= frame_unwind_arch (frame
);
430 throw_error (NO_ENTRY_VALUE_ERROR
,
431 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
432 "(of %s (%s)) does not match caller gdbarch %s"),
433 gdbarch_bfd_arch_info (gdbarch
)->printable_name
,
434 paddress (gdbarch
, func_addr
),
435 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
),
436 gdbarch_bfd_arch_info (caller_gdbarch
)->printable_name
);
439 if (caller_frame
== NULL
)
441 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (func_addr
);
443 throw_error (NO_ENTRY_VALUE_ERROR
, _("DW_OP_GNU_entry_value resolving "
444 "requires caller of %s (%s)"),
445 paddress (gdbarch
, func_addr
),
446 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
448 caller_pc
= get_frame_pc (caller_frame
);
449 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
451 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, caller_frame
);
452 if (target_addr
!= func_addr
)
454 struct minimal_symbol
*target_msym
, *func_msym
;
456 target_msym
= lookup_minimal_symbol_by_pc (target_addr
);
457 func_msym
= lookup_minimal_symbol_by_pc (func_addr
);
458 throw_error (NO_ENTRY_VALUE_ERROR
,
459 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
460 "but the called frame is for %s at %s"),
461 (target_msym
== NULL
? "???"
462 : SYMBOL_PRINT_NAME (target_msym
)),
463 paddress (gdbarch
, target_addr
),
464 func_msym
== NULL
? "???" : SYMBOL_PRINT_NAME (func_msym
),
465 paddress (gdbarch
, func_addr
));
468 for (iparams
= 0; iparams
< call_site
->parameter_count
; iparams
++)
470 parameter
= &call_site
->parameter
[iparams
];
471 if (parameter
->dwarf_reg
== -1 && dwarf_reg
== -1)
473 if (parameter
->fb_offset
== fb_offset
)
476 else if (parameter
->dwarf_reg
== dwarf_reg
)
479 if (iparams
== call_site
->parameter_count
)
481 struct minimal_symbol
*msym
= lookup_minimal_symbol_by_pc (caller_pc
);
483 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
484 determine its value. */
485 throw_error (NO_ENTRY_VALUE_ERROR
, _("Cannot find matching parameter "
486 "at DW_TAG_GNU_call_site %s at %s"),
487 paddress (gdbarch
, caller_pc
),
488 msym
== NULL
? "???" : SYMBOL_PRINT_NAME (msym
));
491 *per_cu_return
= call_site
->per_cu
;
495 /* Execute call_site_parameter's DWARF block for caller of the CTX's frame.
496 CTX must be of dwarf_expr_ctx_funcs kind. See DWARF_REG and FB_OFFSET
497 description at struct dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
499 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
500 can be more simple as it does not support cross-CU DWARF executions. */
503 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context
*ctx
,
504 int dwarf_reg
, CORE_ADDR fb_offset
)
506 struct dwarf_expr_baton
*debaton
;
507 struct frame_info
*frame
, *caller_frame
;
508 struct dwarf2_per_cu_data
*caller_per_cu
;
509 struct dwarf_expr_baton baton_local
;
510 struct dwarf_expr_context saved_ctx
;
511 struct call_site_parameter
*parameter
;
512 const gdb_byte
*data_src
;
515 gdb_assert (ctx
->funcs
== &dwarf_expr_ctx_funcs
);
516 debaton
= ctx
->baton
;
517 frame
= debaton
->frame
;
518 caller_frame
= get_prev_frame (frame
);
520 parameter
= dwarf_expr_reg_to_entry_parameter (frame
, dwarf_reg
, fb_offset
,
522 data_src
= parameter
->value
;
523 size
= parameter
->value_size
;
525 baton_local
.frame
= caller_frame
;
526 baton_local
.per_cu
= caller_per_cu
;
528 saved_ctx
.gdbarch
= ctx
->gdbarch
;
529 saved_ctx
.addr_size
= ctx
->addr_size
;
530 saved_ctx
.offset
= ctx
->offset
;
531 saved_ctx
.baton
= ctx
->baton
;
532 ctx
->gdbarch
= get_objfile_arch (dwarf2_per_cu_objfile (baton_local
.per_cu
));
533 ctx
->addr_size
= dwarf2_per_cu_addr_size (baton_local
.per_cu
);
534 ctx
->offset
= dwarf2_per_cu_text_offset (baton_local
.per_cu
);
535 ctx
->baton
= &baton_local
;
537 dwarf_expr_eval (ctx
, data_src
, size
);
539 ctx
->gdbarch
= saved_ctx
.gdbarch
;
540 ctx
->addr_size
= saved_ctx
.addr_size
;
541 ctx
->offset
= saved_ctx
.offset
;
542 ctx
->baton
= saved_ctx
.baton
;
547 /* Reference count. */
550 /* The CU from which this closure's expression came. */
551 struct dwarf2_per_cu_data
*per_cu
;
553 /* The number of pieces used to describe this variable. */
556 /* The target address size, used only for DWARF_VALUE_STACK. */
559 /* The pieces themselves. */
560 struct dwarf_expr_piece
*pieces
;
563 /* Allocate a closure for a value formed from separately-described
566 static struct piece_closure
*
567 allocate_piece_closure (struct dwarf2_per_cu_data
*per_cu
,
568 int n_pieces
, struct dwarf_expr_piece
*pieces
,
571 struct piece_closure
*c
= XZALLOC (struct piece_closure
);
576 c
->n_pieces
= n_pieces
;
577 c
->addr_size
= addr_size
;
578 c
->pieces
= XCALLOC (n_pieces
, struct dwarf_expr_piece
);
580 memcpy (c
->pieces
, pieces
, n_pieces
* sizeof (struct dwarf_expr_piece
));
581 for (i
= 0; i
< n_pieces
; ++i
)
582 if (c
->pieces
[i
].location
== DWARF_VALUE_STACK
)
583 value_incref (c
->pieces
[i
].v
.value
);
588 /* The lowest-level function to extract bits from a byte buffer.
589 SOURCE is the buffer. It is updated if we read to the end of a
591 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
592 updated to reflect the number of bits actually read.
593 NBITS is the number of bits we want to read. It is updated to
594 reflect the number of bits actually read. This function may read
596 BITS_BIG_ENDIAN is taken directly from gdbarch.
597 This function returns the extracted bits. */
600 extract_bits_primitive (const gdb_byte
**source
,
601 unsigned int *source_offset_bits
,
602 int *nbits
, int bits_big_endian
)
604 unsigned int avail
, mask
, datum
;
606 gdb_assert (*source_offset_bits
< 8);
608 avail
= 8 - *source_offset_bits
;
612 mask
= (1 << avail
) - 1;
615 datum
>>= 8 - (*source_offset_bits
+ *nbits
);
617 datum
>>= *source_offset_bits
;
621 *source_offset_bits
+= avail
;
622 if (*source_offset_bits
>= 8)
624 *source_offset_bits
-= 8;
631 /* Extract some bits from a source buffer and move forward in the
634 SOURCE is the source buffer. It is updated as bytes are read.
635 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
637 NBITS is the number of bits to read.
638 BITS_BIG_ENDIAN is taken directly from gdbarch.
640 This function returns the bits that were read. */
643 extract_bits (const gdb_byte
**source
, unsigned int *source_offset_bits
,
644 int nbits
, int bits_big_endian
)
648 gdb_assert (nbits
> 0 && nbits
<= 8);
650 datum
= extract_bits_primitive (source
, source_offset_bits
, &nbits
,
656 more
= extract_bits_primitive (source
, source_offset_bits
, &nbits
,
668 /* Write some bits into a buffer and move forward in the buffer.
670 DATUM is the bits to write. The low-order bits of DATUM are used.
671 DEST is the destination buffer. It is updated as bytes are
673 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
675 NBITS is the number of valid bits in DATUM.
676 BITS_BIG_ENDIAN is taken directly from gdbarch. */
679 insert_bits (unsigned int datum
,
680 gdb_byte
*dest
, unsigned int dest_offset_bits
,
681 int nbits
, int bits_big_endian
)
685 gdb_assert (dest_offset_bits
+ nbits
<= 8);
687 mask
= (1 << nbits
) - 1;
690 datum
<<= 8 - (dest_offset_bits
+ nbits
);
691 mask
<<= 8 - (dest_offset_bits
+ nbits
);
695 datum
<<= dest_offset_bits
;
696 mask
<<= dest_offset_bits
;
699 gdb_assert ((datum
& ~mask
) == 0);
701 *dest
= (*dest
& ~mask
) | datum
;
704 /* Copy bits from a source to a destination.
706 DEST is where the bits should be written.
707 DEST_OFFSET_BITS is the bit offset into DEST.
708 SOURCE is the source of bits.
709 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
710 BIT_COUNT is the number of bits to copy.
711 BITS_BIG_ENDIAN is taken directly from gdbarch. */
714 copy_bitwise (gdb_byte
*dest
, unsigned int dest_offset_bits
,
715 const gdb_byte
*source
, unsigned int source_offset_bits
,
716 unsigned int bit_count
,
719 unsigned int dest_avail
;
722 /* Reduce everything to byte-size pieces. */
723 dest
+= dest_offset_bits
/ 8;
724 dest_offset_bits
%= 8;
725 source
+= source_offset_bits
/ 8;
726 source_offset_bits
%= 8;
728 dest_avail
= 8 - dest_offset_bits
% 8;
730 /* See if we can fill the first destination byte. */
731 if (dest_avail
< bit_count
)
733 datum
= extract_bits (&source
, &source_offset_bits
, dest_avail
,
735 insert_bits (datum
, dest
, dest_offset_bits
, dest_avail
, bits_big_endian
);
737 dest_offset_bits
= 0;
738 bit_count
-= dest_avail
;
741 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
742 than 8 bits remaining. */
743 gdb_assert (dest_offset_bits
% 8 == 0 || bit_count
< 8);
744 for (; bit_count
>= 8; bit_count
-= 8)
746 datum
= extract_bits (&source
, &source_offset_bits
, 8, bits_big_endian
);
747 *dest
++ = (gdb_byte
) datum
;
750 /* Finally, we may have a few leftover bits. */
751 gdb_assert (bit_count
<= 8 - dest_offset_bits
% 8);
754 datum
= extract_bits (&source
, &source_offset_bits
, bit_count
,
756 insert_bits (datum
, dest
, dest_offset_bits
, bit_count
, bits_big_endian
);
761 read_pieced_value (struct value
*v
)
765 ULONGEST bits_to_skip
;
767 struct piece_closure
*c
768 = (struct piece_closure
*) value_computed_closure (v
);
769 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (v
));
771 size_t buffer_size
= 0;
773 struct cleanup
*cleanup
;
775 = gdbarch_bits_big_endian (get_type_arch (value_type (v
)));
777 if (value_type (v
) != value_enclosing_type (v
))
778 internal_error (__FILE__
, __LINE__
,
779 _("Should not be able to create a lazy value with "
780 "an enclosing type"));
782 cleanup
= make_cleanup (free_current_contents
, &buffer
);
784 contents
= value_contents_raw (v
);
785 bits_to_skip
= 8 * value_offset (v
);
786 if (value_bitsize (v
))
788 bits_to_skip
+= value_bitpos (v
);
789 type_len
= value_bitsize (v
);
792 type_len
= 8 * TYPE_LENGTH (value_type (v
));
794 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
796 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
797 size_t this_size
, this_size_bits
;
798 long dest_offset_bits
, source_offset_bits
, source_offset
;
799 const gdb_byte
*intermediate_buffer
;
801 /* Compute size, source, and destination offsets for copying, in
803 this_size_bits
= p
->size
;
804 if (bits_to_skip
> 0 && bits_to_skip
>= this_size_bits
)
806 bits_to_skip
-= this_size_bits
;
809 if (this_size_bits
> type_len
- offset
)
810 this_size_bits
= type_len
- offset
;
811 if (bits_to_skip
> 0)
813 dest_offset_bits
= 0;
814 source_offset_bits
= bits_to_skip
;
815 this_size_bits
-= bits_to_skip
;
820 dest_offset_bits
= offset
;
821 source_offset_bits
= 0;
824 this_size
= (this_size_bits
+ source_offset_bits
% 8 + 7) / 8;
825 source_offset
= source_offset_bits
/ 8;
826 if (buffer_size
< this_size
)
828 buffer_size
= this_size
;
829 buffer
= xrealloc (buffer
, buffer_size
);
831 intermediate_buffer
= buffer
;
833 /* Copy from the source to DEST_BUFFER. */
836 case DWARF_VALUE_REGISTER
:
838 struct gdbarch
*arch
= get_frame_arch (frame
);
839 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->v
.regno
);
840 int reg_offset
= source_offset
;
842 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
843 && this_size
< register_size (arch
, gdb_regnum
))
845 /* Big-endian, and we want less than full size. */
846 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
847 /* We want the lower-order THIS_SIZE_BITS of the bytes
848 we extract from the register. */
849 source_offset_bits
+= 8 * this_size
- this_size_bits
;
852 if (gdb_regnum
!= -1)
856 if (!get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
860 /* Just so garbage doesn't ever shine through. */
861 memset (buffer
, 0, this_size
);
864 set_value_optimized_out (v
, 1);
866 mark_value_bytes_unavailable (v
, offset
, this_size
);
871 error (_("Unable to access DWARF register number %s"),
872 paddress (arch
, p
->v
.regno
));
877 case DWARF_VALUE_MEMORY
:
878 read_value_memory (v
, offset
,
879 p
->v
.mem
.in_stack_memory
,
880 p
->v
.mem
.addr
+ source_offset
,
884 case DWARF_VALUE_STACK
:
886 size_t n
= this_size
;
888 if (n
> c
->addr_size
- source_offset
)
889 n
= (c
->addr_size
>= source_offset
890 ? c
->addr_size
- source_offset
898 const gdb_byte
*val_bytes
= value_contents_all (p
->v
.value
);
900 intermediate_buffer
= val_bytes
+ source_offset
;
905 case DWARF_VALUE_LITERAL
:
907 size_t n
= this_size
;
909 if (n
> p
->v
.literal
.length
- source_offset
)
910 n
= (p
->v
.literal
.length
>= source_offset
911 ? p
->v
.literal
.length
- source_offset
914 intermediate_buffer
= p
->v
.literal
.data
+ source_offset
;
918 /* These bits show up as zeros -- but do not cause the value
919 to be considered optimized-out. */
920 case DWARF_VALUE_IMPLICIT_POINTER
:
923 case DWARF_VALUE_OPTIMIZED_OUT
:
924 set_value_optimized_out (v
, 1);
928 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
931 if (p
->location
!= DWARF_VALUE_OPTIMIZED_OUT
932 && p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
933 copy_bitwise (contents
, dest_offset_bits
,
934 intermediate_buffer
, source_offset_bits
% 8,
935 this_size_bits
, bits_big_endian
);
937 offset
+= this_size_bits
;
940 do_cleanups (cleanup
);
944 write_pieced_value (struct value
*to
, struct value
*from
)
948 ULONGEST bits_to_skip
;
949 const gdb_byte
*contents
;
950 struct piece_closure
*c
951 = (struct piece_closure
*) value_computed_closure (to
);
952 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (to
));
954 size_t buffer_size
= 0;
956 struct cleanup
*cleanup
;
958 = gdbarch_bits_big_endian (get_type_arch (value_type (to
)));
962 set_value_optimized_out (to
, 1);
966 cleanup
= make_cleanup (free_current_contents
, &buffer
);
968 contents
= value_contents (from
);
969 bits_to_skip
= 8 * value_offset (to
);
970 if (value_bitsize (to
))
972 bits_to_skip
+= value_bitpos (to
);
973 type_len
= value_bitsize (to
);
976 type_len
= 8 * TYPE_LENGTH (value_type (to
));
978 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
980 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
981 size_t this_size_bits
, this_size
;
982 long dest_offset_bits
, source_offset_bits
, dest_offset
, source_offset
;
984 const gdb_byte
*source_buffer
;
986 this_size_bits
= p
->size
;
987 if (bits_to_skip
> 0 && bits_to_skip
>= this_size_bits
)
989 bits_to_skip
-= this_size_bits
;
992 if (this_size_bits
> type_len
- offset
)
993 this_size_bits
= type_len
- offset
;
994 if (bits_to_skip
> 0)
996 dest_offset_bits
= bits_to_skip
;
997 source_offset_bits
= 0;
998 this_size_bits
-= bits_to_skip
;
1003 dest_offset_bits
= 0;
1004 source_offset_bits
= offset
;
1007 this_size
= (this_size_bits
+ source_offset_bits
% 8 + 7) / 8;
1008 source_offset
= source_offset_bits
/ 8;
1009 dest_offset
= dest_offset_bits
/ 8;
1010 if (dest_offset_bits
% 8 == 0 && source_offset_bits
% 8 == 0)
1012 source_buffer
= contents
+ source_offset
;
1017 if (buffer_size
< this_size
)
1019 buffer_size
= this_size
;
1020 buffer
= xrealloc (buffer
, buffer_size
);
1022 source_buffer
= buffer
;
1026 switch (p
->location
)
1028 case DWARF_VALUE_REGISTER
:
1030 struct gdbarch
*arch
= get_frame_arch (frame
);
1031 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->v
.regno
);
1032 int reg_offset
= dest_offset
;
1034 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
1035 && this_size
<= register_size (arch
, gdb_regnum
))
1036 /* Big-endian, and we want less than full size. */
1037 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
1039 if (gdb_regnum
!= -1)
1045 if (!get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1050 error (_("Can't do read-modify-write to "
1051 "update bitfield; containing word has been "
1054 throw_error (NOT_AVAILABLE_ERROR
,
1055 _("Can't do read-modify-write to update "
1056 "bitfield; containing word "
1059 copy_bitwise (buffer
, dest_offset_bits
,
1060 contents
, source_offset_bits
,
1065 put_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1066 this_size
, source_buffer
);
1070 error (_("Unable to write to DWARF register number %s"),
1071 paddress (arch
, p
->v
.regno
));
1075 case DWARF_VALUE_MEMORY
:
1078 /* Only the first and last bytes can possibly have any
1080 read_memory (p
->v
.mem
.addr
+ dest_offset
, buffer
, 1);
1081 read_memory (p
->v
.mem
.addr
+ dest_offset
+ this_size
- 1,
1082 buffer
+ this_size
- 1, 1);
1083 copy_bitwise (buffer
, dest_offset_bits
,
1084 contents
, source_offset_bits
,
1089 write_memory (p
->v
.mem
.addr
+ dest_offset
,
1090 source_buffer
, this_size
);
1093 set_value_optimized_out (to
, 1);
1096 offset
+= this_size_bits
;
1099 do_cleanups (cleanup
);
1102 /* A helper function that checks bit validity in a pieced value.
1103 CHECK_FOR indicates the kind of validity checking.
1104 DWARF_VALUE_MEMORY means to check whether any bit is valid.
1105 DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
1107 DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
1108 implicit pointer. */
1111 check_pieced_value_bits (const struct value
*value
, int bit_offset
,
1113 enum dwarf_value_location check_for
)
1115 struct piece_closure
*c
1116 = (struct piece_closure
*) value_computed_closure (value
);
1118 int validity
= (check_for
== DWARF_VALUE_MEMORY
1119 || check_for
== DWARF_VALUE_IMPLICIT_POINTER
);
1121 bit_offset
+= 8 * value_offset (value
);
1122 if (value_bitsize (value
))
1123 bit_offset
+= value_bitpos (value
);
1125 for (i
= 0; i
< c
->n_pieces
&& bit_length
> 0; i
++)
1127 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1128 size_t this_size_bits
= p
->size
;
1132 if (bit_offset
>= this_size_bits
)
1134 bit_offset
-= this_size_bits
;
1138 bit_length
-= this_size_bits
- bit_offset
;
1142 bit_length
-= this_size_bits
;
1144 if (check_for
== DWARF_VALUE_IMPLICIT_POINTER
)
1146 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
1149 else if (p
->location
== DWARF_VALUE_OPTIMIZED_OUT
1150 || p
->location
== DWARF_VALUE_IMPLICIT_POINTER
)
1166 check_pieced_value_validity (const struct value
*value
, int bit_offset
,
1169 return check_pieced_value_bits (value
, bit_offset
, bit_length
,
1170 DWARF_VALUE_MEMORY
);
1174 check_pieced_value_invalid (const struct value
*value
)
1176 return check_pieced_value_bits (value
, 0,
1177 8 * TYPE_LENGTH (value_type (value
)),
1178 DWARF_VALUE_OPTIMIZED_OUT
);
1181 /* An implementation of an lval_funcs method to see whether a value is
1182 a synthetic pointer. */
1185 check_pieced_synthetic_pointer (const struct value
*value
, int bit_offset
,
1188 return check_pieced_value_bits (value
, bit_offset
, bit_length
,
1189 DWARF_VALUE_IMPLICIT_POINTER
);
1192 /* A wrapper function for get_frame_address_in_block. */
1195 get_frame_address_in_block_wrapper (void *baton
)
1197 return get_frame_address_in_block (baton
);
1200 /* An implementation of an lval_funcs method to indirect through a
1201 pointer. This handles the synthetic pointer case when needed. */
1203 static struct value
*
1204 indirect_pieced_value (struct value
*value
)
1206 struct piece_closure
*c
1207 = (struct piece_closure
*) value_computed_closure (value
);
1209 struct frame_info
*frame
;
1210 struct dwarf2_locexpr_baton baton
;
1211 int i
, bit_offset
, bit_length
;
1212 struct dwarf_expr_piece
*piece
= NULL
;
1213 LONGEST byte_offset
;
1215 type
= check_typedef (value_type (value
));
1216 if (TYPE_CODE (type
) != TYPE_CODE_PTR
)
1219 bit_length
= 8 * TYPE_LENGTH (type
);
1220 bit_offset
= 8 * value_offset (value
);
1221 if (value_bitsize (value
))
1222 bit_offset
+= value_bitpos (value
);
1224 for (i
= 0; i
< c
->n_pieces
&& bit_length
> 0; i
++)
1226 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1227 size_t this_size_bits
= p
->size
;
1231 if (bit_offset
>= this_size_bits
)
1233 bit_offset
-= this_size_bits
;
1237 bit_length
-= this_size_bits
- bit_offset
;
1241 bit_length
-= this_size_bits
;
1243 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
1246 if (bit_length
!= 0)
1247 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
1253 frame
= get_selected_frame (_("No frame selected."));
1255 /* This is an offset requested by GDB, such as value subcripts. */
1256 byte_offset
= value_as_address (value
);
1259 baton
= dwarf2_fetch_die_location_block (piece
->v
.ptr
.die
, c
->per_cu
,
1260 get_frame_address_in_block_wrapper
,
1263 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type
), frame
,
1264 baton
.data
, baton
.size
, baton
.per_cu
,
1265 piece
->v
.ptr
.offset
+ byte_offset
);
1269 copy_pieced_value_closure (const struct value
*v
)
1271 struct piece_closure
*c
1272 = (struct piece_closure
*) value_computed_closure (v
);
1279 free_pieced_value_closure (struct value
*v
)
1281 struct piece_closure
*c
1282 = (struct piece_closure
*) value_computed_closure (v
);
1289 for (i
= 0; i
< c
->n_pieces
; ++i
)
1290 if (c
->pieces
[i
].location
== DWARF_VALUE_STACK
)
1291 value_free (c
->pieces
[i
].v
.value
);
1298 /* Functions for accessing a variable described by DW_OP_piece. */
1299 static const struct lval_funcs pieced_value_funcs
= {
1302 check_pieced_value_validity
,
1303 check_pieced_value_invalid
,
1304 indirect_pieced_value
,
1305 check_pieced_synthetic_pointer
,
1306 copy_pieced_value_closure
,
1307 free_pieced_value_closure
1310 /* Helper function which throws an error if a synthetic pointer is
1314 invalid_synthetic_pointer (void)
1316 error (_("access outside bounds of object "
1317 "referenced via synthetic pointer"));
1320 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
1322 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs
=
1324 dwarf_expr_read_reg
,
1325 dwarf_expr_read_mem
,
1326 dwarf_expr_frame_base
,
1327 dwarf_expr_frame_cfa
,
1328 dwarf_expr_frame_pc
,
1329 dwarf_expr_tls_address
,
1330 dwarf_expr_dwarf_call
,
1331 dwarf_expr_get_base_type
,
1332 dwarf_expr_push_dwarf_reg_entry_value
1335 /* Evaluate a location description, starting at DATA and with length
1336 SIZE, to find the current location of variable of TYPE in the
1337 context of FRAME. BYTE_OFFSET is applied after the contents are
1340 static struct value
*
1341 dwarf2_evaluate_loc_desc_full (struct type
*type
, struct frame_info
*frame
,
1342 const gdb_byte
*data
, unsigned short size
,
1343 struct dwarf2_per_cu_data
*per_cu
,
1344 LONGEST byte_offset
)
1346 struct value
*retval
;
1347 struct dwarf_expr_baton baton
;
1348 struct dwarf_expr_context
*ctx
;
1349 struct cleanup
*old_chain
, *value_chain
;
1350 struct objfile
*objfile
= dwarf2_per_cu_objfile (per_cu
);
1351 volatile struct gdb_exception ex
;
1353 if (byte_offset
< 0)
1354 invalid_synthetic_pointer ();
1357 return allocate_optimized_out_value (type
);
1359 baton
.frame
= frame
;
1360 baton
.per_cu
= per_cu
;
1362 ctx
= new_dwarf_expr_context ();
1363 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
1364 value_chain
= make_cleanup_value_free_to_mark (value_mark ());
1366 ctx
->gdbarch
= get_objfile_arch (objfile
);
1367 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
1368 ctx
->ref_addr_size
= dwarf2_per_cu_ref_addr_size (per_cu
);
1369 ctx
->offset
= dwarf2_per_cu_text_offset (per_cu
);
1370 ctx
->baton
= &baton
;
1371 ctx
->funcs
= &dwarf_expr_ctx_funcs
;
1373 TRY_CATCH (ex
, RETURN_MASK_ERROR
)
1375 dwarf_expr_eval (ctx
, data
, size
);
1379 if (ex
.error
== NOT_AVAILABLE_ERROR
)
1381 do_cleanups (old_chain
);
1382 retval
= allocate_value (type
);
1383 mark_value_bytes_unavailable (retval
, 0, TYPE_LENGTH (type
));
1386 else if (ex
.error
== NO_ENTRY_VALUE_ERROR
)
1388 if (entry_values_debug
)
1389 exception_print (gdb_stdout
, ex
);
1390 do_cleanups (old_chain
);
1391 return allocate_optimized_out_value (type
);
1394 throw_exception (ex
);
1397 if (ctx
->num_pieces
> 0)
1399 struct piece_closure
*c
;
1400 struct frame_id frame_id
= get_frame_id (frame
);
1401 ULONGEST bit_size
= 0;
1404 for (i
= 0; i
< ctx
->num_pieces
; ++i
)
1405 bit_size
+= ctx
->pieces
[i
].size
;
1406 if (8 * (byte_offset
+ TYPE_LENGTH (type
)) > bit_size
)
1407 invalid_synthetic_pointer ();
1409 c
= allocate_piece_closure (per_cu
, ctx
->num_pieces
, ctx
->pieces
,
1411 /* We must clean up the value chain after creating the piece
1412 closure but before allocating the result. */
1413 do_cleanups (value_chain
);
1414 retval
= allocate_computed_value (type
, &pieced_value_funcs
, c
);
1415 VALUE_FRAME_ID (retval
) = frame_id
;
1416 set_value_offset (retval
, byte_offset
);
1420 switch (ctx
->location
)
1422 case DWARF_VALUE_REGISTER
:
1424 struct gdbarch
*arch
= get_frame_arch (frame
);
1425 ULONGEST dwarf_regnum
= value_as_long (dwarf_expr_fetch (ctx
, 0));
1426 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_regnum
);
1428 if (byte_offset
!= 0)
1429 error (_("cannot use offset on synthetic pointer to register"));
1430 do_cleanups (value_chain
);
1431 if (gdb_regnum
!= -1)
1432 retval
= value_from_register (type
, gdb_regnum
, frame
);
1434 error (_("Unable to access DWARF register number %s"),
1435 paddress (arch
, dwarf_regnum
));
1439 case DWARF_VALUE_MEMORY
:
1441 CORE_ADDR address
= dwarf_expr_fetch_address (ctx
, 0);
1442 int in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
1444 do_cleanups (value_chain
);
1445 retval
= allocate_value_lazy (type
);
1446 VALUE_LVAL (retval
) = lval_memory
;
1447 if (in_stack_memory
)
1448 set_value_stack (retval
, 1);
1449 set_value_address (retval
, address
+ byte_offset
);
1453 case DWARF_VALUE_STACK
:
1455 struct value
*value
= dwarf_expr_fetch (ctx
, 0);
1457 const gdb_byte
*val_bytes
;
1458 size_t n
= TYPE_LENGTH (value_type (value
));
1460 if (byte_offset
+ TYPE_LENGTH (type
) > n
)
1461 invalid_synthetic_pointer ();
1463 val_bytes
= value_contents_all (value
);
1464 val_bytes
+= byte_offset
;
1467 /* Preserve VALUE because we are going to free values back
1468 to the mark, but we still need the value contents
1470 value_incref (value
);
1471 do_cleanups (value_chain
);
1472 make_cleanup_value_free (value
);
1474 retval
= allocate_value (type
);
1475 contents
= value_contents_raw (retval
);
1476 if (n
> TYPE_LENGTH (type
))
1478 struct gdbarch
*objfile_gdbarch
= get_objfile_arch (objfile
);
1480 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
1481 val_bytes
+= n
- TYPE_LENGTH (type
);
1482 n
= TYPE_LENGTH (type
);
1484 memcpy (contents
, val_bytes
, n
);
1488 case DWARF_VALUE_LITERAL
:
1491 const bfd_byte
*ldata
;
1492 size_t n
= ctx
->len
;
1494 if (byte_offset
+ TYPE_LENGTH (type
) > n
)
1495 invalid_synthetic_pointer ();
1497 do_cleanups (value_chain
);
1498 retval
= allocate_value (type
);
1499 contents
= value_contents_raw (retval
);
1501 ldata
= ctx
->data
+ byte_offset
;
1504 if (n
> TYPE_LENGTH (type
))
1506 struct gdbarch
*objfile_gdbarch
= get_objfile_arch (objfile
);
1508 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
1509 ldata
+= n
- TYPE_LENGTH (type
);
1510 n
= TYPE_LENGTH (type
);
1512 memcpy (contents
, ldata
, n
);
1516 case DWARF_VALUE_OPTIMIZED_OUT
:
1517 do_cleanups (value_chain
);
1518 retval
= allocate_optimized_out_value (type
);
1521 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
1522 operation by execute_stack_op. */
1523 case DWARF_VALUE_IMPLICIT_POINTER
:
1524 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
1525 it can only be encountered when making a piece. */
1527 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
1531 set_value_initialized (retval
, ctx
->initialized
);
1533 do_cleanups (old_chain
);
1538 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
1539 passes 0 as the byte_offset. */
1542 dwarf2_evaluate_loc_desc (struct type
*type
, struct frame_info
*frame
,
1543 const gdb_byte
*data
, unsigned short size
,
1544 struct dwarf2_per_cu_data
*per_cu
)
1546 return dwarf2_evaluate_loc_desc_full (type
, frame
, data
, size
, per_cu
, 0);
1550 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
1552 struct needs_frame_baton
1555 struct dwarf2_per_cu_data
*per_cu
;
1558 /* Reads from registers do require a frame. */
1560 needs_frame_read_reg (void *baton
, int regnum
)
1562 struct needs_frame_baton
*nf_baton
= baton
;
1564 nf_baton
->needs_frame
= 1;
1568 /* Reads from memory do not require a frame. */
1570 needs_frame_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
1572 memset (buf
, 0, len
);
1575 /* Frame-relative accesses do require a frame. */
1577 needs_frame_frame_base (void *baton
, const gdb_byte
**start
, size_t * length
)
1579 static gdb_byte lit0
= DW_OP_lit0
;
1580 struct needs_frame_baton
*nf_baton
= baton
;
1585 nf_baton
->needs_frame
= 1;
1588 /* CFA accesses require a frame. */
1591 needs_frame_frame_cfa (void *baton
)
1593 struct needs_frame_baton
*nf_baton
= baton
;
1595 nf_baton
->needs_frame
= 1;
1599 /* Thread-local accesses do require a frame. */
1601 needs_frame_tls_address (void *baton
, CORE_ADDR offset
)
1603 struct needs_frame_baton
*nf_baton
= baton
;
1605 nf_baton
->needs_frame
= 1;
1609 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
1612 needs_frame_dwarf_call (struct dwarf_expr_context
*ctx
, size_t die_offset
)
1614 struct needs_frame_baton
*nf_baton
= ctx
->baton
;
1616 per_cu_dwarf_call (ctx
, die_offset
, nf_baton
->per_cu
,
1617 ctx
->funcs
->get_frame_pc
, ctx
->baton
);
1620 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
1623 needs_dwarf_reg_entry_value (struct dwarf_expr_context
*ctx
,
1624 int dwarf_reg
, CORE_ADDR fb_offset
)
1626 struct needs_frame_baton
*nf_baton
= ctx
->baton
;
1628 nf_baton
->needs_frame
= 1;
1631 /* Virtual method table for dwarf2_loc_desc_needs_frame below. */
1633 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs
=
1635 needs_frame_read_reg
,
1636 needs_frame_read_mem
,
1637 needs_frame_frame_base
,
1638 needs_frame_frame_cfa
,
1639 needs_frame_frame_cfa
, /* get_frame_pc */
1640 needs_frame_tls_address
,
1641 needs_frame_dwarf_call
,
1642 NULL
, /* get_base_type */
1643 needs_dwarf_reg_entry_value
1646 /* Return non-zero iff the location expression at DATA (length SIZE)
1647 requires a frame to evaluate. */
1650 dwarf2_loc_desc_needs_frame (const gdb_byte
*data
, unsigned short size
,
1651 struct dwarf2_per_cu_data
*per_cu
)
1653 struct needs_frame_baton baton
;
1654 struct dwarf_expr_context
*ctx
;
1656 struct cleanup
*old_chain
;
1657 struct objfile
*objfile
= dwarf2_per_cu_objfile (per_cu
);
1659 baton
.needs_frame
= 0;
1660 baton
.per_cu
= per_cu
;
1662 ctx
= new_dwarf_expr_context ();
1663 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
1664 make_cleanup_value_free_to_mark (value_mark ());
1666 ctx
->gdbarch
= get_objfile_arch (objfile
);
1667 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
1668 ctx
->ref_addr_size
= dwarf2_per_cu_ref_addr_size (per_cu
);
1669 ctx
->offset
= dwarf2_per_cu_text_offset (per_cu
);
1670 ctx
->baton
= &baton
;
1671 ctx
->funcs
= &needs_frame_ctx_funcs
;
1673 dwarf_expr_eval (ctx
, data
, size
);
1675 in_reg
= ctx
->location
== DWARF_VALUE_REGISTER
;
1677 if (ctx
->num_pieces
> 0)
1681 /* If the location has several pieces, and any of them are in
1682 registers, then we will need a frame to fetch them from. */
1683 for (i
= 0; i
< ctx
->num_pieces
; i
++)
1684 if (ctx
->pieces
[i
].location
== DWARF_VALUE_REGISTER
)
1688 do_cleanups (old_chain
);
1690 return baton
.needs_frame
|| in_reg
;
1693 /* A helper function that throws an unimplemented error mentioning a
1694 given DWARF operator. */
1697 unimplemented (unsigned int op
)
1699 const char *name
= dwarf_stack_op_name (op
);
1702 error (_("DWARF operator %s cannot be translated to an agent expression"),
1705 error (_("Unknown DWARF operator 0x%02x cannot be translated "
1706 "to an agent expression"),
1710 /* A helper function to convert a DWARF register to an arch register.
1711 ARCH is the architecture.
1712 DWARF_REG is the register.
1713 This will throw an exception if the DWARF register cannot be
1714 translated to an architecture register. */
1717 translate_register (struct gdbarch
*arch
, int dwarf_reg
)
1719 int reg
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_reg
);
1721 error (_("Unable to access DWARF register number %d"), dwarf_reg
);
1725 /* A helper function that emits an access to memory. ARCH is the
1726 target architecture. EXPR is the expression which we are building.
1727 NBITS is the number of bits we want to read. This emits the
1728 opcodes needed to read the memory and then extract the desired
1732 access_memory (struct gdbarch
*arch
, struct agent_expr
*expr
, ULONGEST nbits
)
1734 ULONGEST nbytes
= (nbits
+ 7) / 8;
1736 gdb_assert (nbits
> 0 && nbits
<= sizeof (LONGEST
));
1739 ax_trace_quick (expr
, nbytes
);
1742 ax_simple (expr
, aop_ref8
);
1743 else if (nbits
<= 16)
1744 ax_simple (expr
, aop_ref16
);
1745 else if (nbits
<= 32)
1746 ax_simple (expr
, aop_ref32
);
1748 ax_simple (expr
, aop_ref64
);
1750 /* If we read exactly the number of bytes we wanted, we're done. */
1751 if (8 * nbytes
== nbits
)
1754 if (gdbarch_bits_big_endian (arch
))
1756 /* On a bits-big-endian machine, we want the high-order
1758 ax_const_l (expr
, 8 * nbytes
- nbits
);
1759 ax_simple (expr
, aop_rsh_unsigned
);
1763 /* On a bits-little-endian box, we want the low-order NBITS. */
1764 ax_zero_ext (expr
, nbits
);
1768 /* A helper function to return the frame's PC. */
1771 get_ax_pc (void *baton
)
1773 struct agent_expr
*expr
= baton
;
1778 /* Compile a DWARF location expression to an agent expression.
1780 EXPR is the agent expression we are building.
1781 LOC is the agent value we modify.
1782 ARCH is the architecture.
1783 ADDR_SIZE is the size of addresses, in bytes.
1784 OP_PTR is the start of the location expression.
1785 OP_END is one past the last byte of the location expression.
1787 This will throw an exception for various kinds of errors -- for
1788 example, if the expression cannot be compiled, or if the expression
1792 dwarf2_compile_expr_to_ax (struct agent_expr
*expr
, struct axs_value
*loc
,
1793 struct gdbarch
*arch
, unsigned int addr_size
,
1794 const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
1795 struct dwarf2_per_cu_data
*per_cu
)
1797 struct cleanup
*cleanups
;
1799 VEC(int) *dw_labels
= NULL
, *patches
= NULL
;
1800 const gdb_byte
* const base
= op_ptr
;
1801 const gdb_byte
*previous_piece
= op_ptr
;
1802 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
1803 ULONGEST bits_collected
= 0;
1804 unsigned int addr_size_bits
= 8 * addr_size
;
1805 int bits_big_endian
= gdbarch_bits_big_endian (arch
);
1807 offsets
= xmalloc ((op_end
- op_ptr
) * sizeof (int));
1808 cleanups
= make_cleanup (xfree
, offsets
);
1810 for (i
= 0; i
< op_end
- op_ptr
; ++i
)
1813 make_cleanup (VEC_cleanup (int), &dw_labels
);
1814 make_cleanup (VEC_cleanup (int), &patches
);
1816 /* By default we are making an address. */
1817 loc
->kind
= axs_lvalue_memory
;
1819 while (op_ptr
< op_end
)
1821 enum dwarf_location_atom op
= *op_ptr
;
1822 ULONGEST uoffset
, reg
;
1826 offsets
[op_ptr
- base
] = expr
->len
;
1829 /* Our basic approach to code generation is to map DWARF
1830 operations directly to AX operations. However, there are
1833 First, DWARF works on address-sized units, but AX always uses
1834 LONGEST. For most operations we simply ignore this
1835 difference; instead we generate sign extensions as needed
1836 before division and comparison operations. It would be nice
1837 to omit the sign extensions, but there is no way to determine
1838 the size of the target's LONGEST. (This code uses the size
1839 of the host LONGEST in some cases -- that is a bug but it is
1842 Second, some DWARF operations cannot be translated to AX.
1843 For these we simply fail. See
1844 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
1879 ax_const_l (expr
, op
- DW_OP_lit0
);
1883 uoffset
= extract_unsigned_integer (op_ptr
, addr_size
, byte_order
);
1884 op_ptr
+= addr_size
;
1885 /* Some versions of GCC emit DW_OP_addr before
1886 DW_OP_GNU_push_tls_address. In this case the value is an
1887 index, not an address. We don't support things like
1888 branching between the address and the TLS op. */
1889 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
1890 uoffset
+= dwarf2_per_cu_text_offset (per_cu
);
1891 ax_const_l (expr
, uoffset
);
1895 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 1, byte_order
));
1899 ax_const_l (expr
, extract_signed_integer (op_ptr
, 1, byte_order
));
1903 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 2, byte_order
));
1907 ax_const_l (expr
, extract_signed_integer (op_ptr
, 2, byte_order
));
1911 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 4, byte_order
));
1915 ax_const_l (expr
, extract_signed_integer (op_ptr
, 4, byte_order
));
1919 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 8, byte_order
));
1923 ax_const_l (expr
, extract_signed_integer (op_ptr
, 8, byte_order
));
1927 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
1928 ax_const_l (expr
, uoffset
);
1931 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
1932 ax_const_l (expr
, offset
);
1967 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
1968 loc
->u
.reg
= translate_register (arch
, op
- DW_OP_reg0
);
1969 loc
->kind
= axs_lvalue_register
;
1973 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
1974 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
1975 loc
->u
.reg
= translate_register (arch
, reg
);
1976 loc
->kind
= axs_lvalue_register
;
1979 case DW_OP_implicit_value
:
1983 op_ptr
= read_uleb128 (op_ptr
, op_end
, &len
);
1984 if (op_ptr
+ len
> op_end
)
1985 error (_("DW_OP_implicit_value: too few bytes available."));
1986 if (len
> sizeof (ULONGEST
))
1987 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
1990 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, len
,
1993 dwarf_expr_require_composition (op_ptr
, op_end
,
1994 "DW_OP_implicit_value");
1996 loc
->kind
= axs_rvalue
;
2000 case DW_OP_stack_value
:
2001 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
2002 loc
->kind
= axs_rvalue
;
2037 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2038 i
= translate_register (arch
, op
- DW_OP_breg0
);
2042 ax_const_l (expr
, offset
);
2043 ax_simple (expr
, aop_add
);
2048 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
2049 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2050 i
= translate_register (arch
, reg
);
2054 ax_const_l (expr
, offset
);
2055 ax_simple (expr
, aop_add
);
2061 const gdb_byte
*datastart
;
2063 unsigned int before_stack_len
;
2065 struct symbol
*framefunc
;
2066 LONGEST base_offset
= 0;
2068 b
= block_for_pc (expr
->scope
);
2071 error (_("No block found for address"));
2073 framefunc
= block_linkage_function (b
);
2076 error (_("No function found for block"));
2078 dwarf_expr_frame_base_1 (framefunc
, expr
->scope
,
2079 &datastart
, &datalen
);
2081 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
2082 dwarf2_compile_expr_to_ax (expr
, loc
, arch
, addr_size
, datastart
,
2083 datastart
+ datalen
, per_cu
);
2087 ax_const_l (expr
, offset
);
2088 ax_simple (expr
, aop_add
);
2091 loc
->kind
= axs_lvalue_memory
;
2096 ax_simple (expr
, aop_dup
);
2100 ax_simple (expr
, aop_pop
);
2105 ax_pick (expr
, offset
);
2109 ax_simple (expr
, aop_swap
);
2117 ax_simple (expr
, aop_rot
);
2121 case DW_OP_deref_size
:
2125 if (op
== DW_OP_deref_size
)
2133 ax_simple (expr
, aop_ref8
);
2136 ax_simple (expr
, aop_ref16
);
2139 ax_simple (expr
, aop_ref32
);
2142 ax_simple (expr
, aop_ref64
);
2145 /* Note that dwarf_stack_op_name will never return
2147 error (_("Unsupported size %d in %s"),
2148 size
, dwarf_stack_op_name (op
));
2154 /* Sign extend the operand. */
2155 ax_ext (expr
, addr_size_bits
);
2156 ax_simple (expr
, aop_dup
);
2157 ax_const_l (expr
, 0);
2158 ax_simple (expr
, aop_less_signed
);
2159 ax_simple (expr
, aop_log_not
);
2160 i
= ax_goto (expr
, aop_if_goto
);
2161 /* We have to emit 0 - X. */
2162 ax_const_l (expr
, 0);
2163 ax_simple (expr
, aop_swap
);
2164 ax_simple (expr
, aop_sub
);
2165 ax_label (expr
, i
, expr
->len
);
2169 /* No need to sign extend here. */
2170 ax_const_l (expr
, 0);
2171 ax_simple (expr
, aop_swap
);
2172 ax_simple (expr
, aop_sub
);
2176 /* Sign extend the operand. */
2177 ax_ext (expr
, addr_size_bits
);
2178 ax_simple (expr
, aop_bit_not
);
2181 case DW_OP_plus_uconst
:
2182 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
2183 /* It would be really weird to emit `DW_OP_plus_uconst 0',
2184 but we micro-optimize anyhow. */
2187 ax_const_l (expr
, reg
);
2188 ax_simple (expr
, aop_add
);
2193 ax_simple (expr
, aop_bit_and
);
2197 /* Sign extend the operands. */
2198 ax_ext (expr
, addr_size_bits
);
2199 ax_simple (expr
, aop_swap
);
2200 ax_ext (expr
, addr_size_bits
);
2201 ax_simple (expr
, aop_swap
);
2202 ax_simple (expr
, aop_div_signed
);
2206 ax_simple (expr
, aop_sub
);
2210 ax_simple (expr
, aop_rem_unsigned
);
2214 ax_simple (expr
, aop_mul
);
2218 ax_simple (expr
, aop_bit_or
);
2222 ax_simple (expr
, aop_add
);
2226 ax_simple (expr
, aop_lsh
);
2230 ax_simple (expr
, aop_rsh_unsigned
);
2234 ax_simple (expr
, aop_rsh_signed
);
2238 ax_simple (expr
, aop_bit_xor
);
2242 /* Sign extend the operands. */
2243 ax_ext (expr
, addr_size_bits
);
2244 ax_simple (expr
, aop_swap
);
2245 ax_ext (expr
, addr_size_bits
);
2246 /* Note no swap here: A <= B is !(B < A). */
2247 ax_simple (expr
, aop_less_signed
);
2248 ax_simple (expr
, aop_log_not
);
2252 /* Sign extend the operands. */
2253 ax_ext (expr
, addr_size_bits
);
2254 ax_simple (expr
, aop_swap
);
2255 ax_ext (expr
, addr_size_bits
);
2256 ax_simple (expr
, aop_swap
);
2257 /* A >= B is !(A < B). */
2258 ax_simple (expr
, aop_less_signed
);
2259 ax_simple (expr
, aop_log_not
);
2263 /* Sign extend the operands. */
2264 ax_ext (expr
, addr_size_bits
);
2265 ax_simple (expr
, aop_swap
);
2266 ax_ext (expr
, addr_size_bits
);
2267 /* No need for a second swap here. */
2268 ax_simple (expr
, aop_equal
);
2272 /* Sign extend the operands. */
2273 ax_ext (expr
, addr_size_bits
);
2274 ax_simple (expr
, aop_swap
);
2275 ax_ext (expr
, addr_size_bits
);
2276 ax_simple (expr
, aop_swap
);
2277 ax_simple (expr
, aop_less_signed
);
2281 /* Sign extend the operands. */
2282 ax_ext (expr
, addr_size_bits
);
2283 ax_simple (expr
, aop_swap
);
2284 ax_ext (expr
, addr_size_bits
);
2285 /* Note no swap here: A > B is B < A. */
2286 ax_simple (expr
, aop_less_signed
);
2290 /* Sign extend the operands. */
2291 ax_ext (expr
, addr_size_bits
);
2292 ax_simple (expr
, aop_swap
);
2293 ax_ext (expr
, addr_size_bits
);
2294 /* No need for a swap here. */
2295 ax_simple (expr
, aop_equal
);
2296 ax_simple (expr
, aop_log_not
);
2299 case DW_OP_call_frame_cfa
:
2300 dwarf2_compile_cfa_to_ax (expr
, loc
, arch
, expr
->scope
, per_cu
);
2301 loc
->kind
= axs_lvalue_memory
;
2304 case DW_OP_GNU_push_tls_address
:
2309 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
2311 i
= ax_goto (expr
, aop_goto
);
2312 VEC_safe_push (int, dw_labels
, op_ptr
+ offset
- base
);
2313 VEC_safe_push (int, patches
, i
);
2317 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
2319 /* Zero extend the operand. */
2320 ax_zero_ext (expr
, addr_size_bits
);
2321 i
= ax_goto (expr
, aop_if_goto
);
2322 VEC_safe_push (int, dw_labels
, op_ptr
+ offset
- base
);
2323 VEC_safe_push (int, patches
, i
);
2330 case DW_OP_bit_piece
:
2332 ULONGEST size
, offset
;
2334 if (op_ptr
- 1 == previous_piece
)
2335 error (_("Cannot translate empty pieces to agent expressions"));
2336 previous_piece
= op_ptr
- 1;
2338 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
2339 if (op
== DW_OP_piece
)
2345 op_ptr
= read_uleb128 (op_ptr
, op_end
, &offset
);
2347 if (bits_collected
+ size
> 8 * sizeof (LONGEST
))
2348 error (_("Expression pieces exceed word size"));
2350 /* Access the bits. */
2353 case axs_lvalue_register
:
2354 ax_reg (expr
, loc
->u
.reg
);
2357 case axs_lvalue_memory
:
2358 /* Offset the pointer, if needed. */
2361 ax_const_l (expr
, offset
/ 8);
2362 ax_simple (expr
, aop_add
);
2365 access_memory (arch
, expr
, size
);
2369 /* For a bits-big-endian target, shift up what we already
2370 have. For a bits-little-endian target, shift up the
2371 new data. Note that there is a potential bug here if
2372 the DWARF expression leaves multiple values on the
2374 if (bits_collected
> 0)
2376 if (bits_big_endian
)
2378 ax_simple (expr
, aop_swap
);
2379 ax_const_l (expr
, size
);
2380 ax_simple (expr
, aop_lsh
);
2381 /* We don't need a second swap here, because
2382 aop_bit_or is symmetric. */
2386 ax_const_l (expr
, size
);
2387 ax_simple (expr
, aop_lsh
);
2389 ax_simple (expr
, aop_bit_or
);
2392 bits_collected
+= size
;
2393 loc
->kind
= axs_rvalue
;
2397 case DW_OP_GNU_uninit
:
2403 struct dwarf2_locexpr_baton block
;
2404 int size
= (op
== DW_OP_call2
? 2 : 4);
2406 uoffset
= extract_unsigned_integer (op_ptr
, size
, byte_order
);
2409 block
= dwarf2_fetch_die_location_block (uoffset
, per_cu
,
2412 /* DW_OP_call_ref is currently not supported. */
2413 gdb_assert (block
.per_cu
== per_cu
);
2415 dwarf2_compile_expr_to_ax (expr
, loc
, arch
, addr_size
,
2416 block
.data
, block
.data
+ block
.size
,
2421 case DW_OP_call_ref
:
2429 /* Patch all the branches we emitted. */
2430 for (i
= 0; i
< VEC_length (int, patches
); ++i
)
2432 int targ
= offsets
[VEC_index (int, dw_labels
, i
)];
2434 internal_error (__FILE__
, __LINE__
, _("invalid label"));
2435 ax_label (expr
, VEC_index (int, patches
, i
), targ
);
2438 do_cleanups (cleanups
);
2442 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2443 evaluator to calculate the location. */
2444 static struct value
*
2445 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
2447 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
2450 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
2451 dlbaton
->size
, dlbaton
->per_cu
);
2456 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
2458 locexpr_read_needs_frame (struct symbol
*symbol
)
2460 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
2462 return dwarf2_loc_desc_needs_frame (dlbaton
->data
, dlbaton
->size
,
2466 /* Return true if DATA points to the end of a piece. END is one past
2467 the last byte in the expression. */
2470 piece_end_p (const gdb_byte
*data
, const gdb_byte
*end
)
2472 return data
== end
|| data
[0] == DW_OP_piece
|| data
[0] == DW_OP_bit_piece
;
2475 /* Helper for locexpr_describe_location_piece that finds the name of a
2479 locexpr_regname (struct gdbarch
*gdbarch
, int dwarf_regnum
)
2483 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, dwarf_regnum
);
2484 return gdbarch_register_name (gdbarch
, regnum
);
2487 /* Nicely describe a single piece of a location, returning an updated
2488 position in the bytecode sequence. This function cannot recognize
2489 all locations; if a location is not recognized, it simply returns
2492 static const gdb_byte
*
2493 locexpr_describe_location_piece (struct symbol
*symbol
, struct ui_file
*stream
,
2494 CORE_ADDR addr
, struct objfile
*objfile
,
2495 const gdb_byte
*data
, const gdb_byte
*end
,
2496 unsigned int addr_size
)
2498 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
2500 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
2502 fprintf_filtered (stream
, _("a variable in $%s"),
2503 locexpr_regname (gdbarch
, data
[0] - DW_OP_reg0
));
2506 else if (data
[0] == DW_OP_regx
)
2510 data
= read_uleb128 (data
+ 1, end
, ®
);
2511 fprintf_filtered (stream
, _("a variable in $%s"),
2512 locexpr_regname (gdbarch
, reg
));
2514 else if (data
[0] == DW_OP_fbreg
)
2517 struct symbol
*framefunc
;
2519 LONGEST frame_offset
;
2520 const gdb_byte
*base_data
, *new_data
, *save_data
= data
;
2522 LONGEST base_offset
= 0;
2524 new_data
= read_sleb128 (data
+ 1, end
, &frame_offset
);
2525 if (!piece_end_p (new_data
, end
))
2529 b
= block_for_pc (addr
);
2532 error (_("No block found for address for symbol \"%s\"."),
2533 SYMBOL_PRINT_NAME (symbol
));
2535 framefunc
= block_linkage_function (b
);
2538 error (_("No function found for block for symbol \"%s\"."),
2539 SYMBOL_PRINT_NAME (symbol
));
2541 dwarf_expr_frame_base_1 (framefunc
, addr
, &base_data
, &base_size
);
2543 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
2545 const gdb_byte
*buf_end
;
2547 frame_reg
= base_data
[0] - DW_OP_breg0
;
2548 buf_end
= read_sleb128 (base_data
+ 1,
2549 base_data
+ base_size
, &base_offset
);
2550 if (buf_end
!= base_data
+ base_size
)
2551 error (_("Unexpected opcode after "
2552 "DW_OP_breg%u for symbol \"%s\"."),
2553 frame_reg
, SYMBOL_PRINT_NAME (symbol
));
2555 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
2557 /* The frame base is just the register, with no offset. */
2558 frame_reg
= base_data
[0] - DW_OP_reg0
;
2563 /* We don't know what to do with the frame base expression,
2564 so we can't trace this variable; give up. */
2568 fprintf_filtered (stream
,
2569 _("a variable at frame base reg $%s offset %s+%s"),
2570 locexpr_regname (gdbarch
, frame_reg
),
2571 plongest (base_offset
), plongest (frame_offset
));
2573 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
2574 && piece_end_p (data
, end
))
2578 data
= read_sleb128 (data
+ 1, end
, &offset
);
2580 fprintf_filtered (stream
,
2581 _("a variable at offset %s from base reg $%s"),
2583 locexpr_regname (gdbarch
, data
[0] - DW_OP_breg0
));
2586 /* The location expression for a TLS variable looks like this (on a
2589 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
2590 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
2592 0x3 is the encoding for DW_OP_addr, which has an operand as long
2593 as the size of an address on the target machine (here is 8
2594 bytes). Note that more recent version of GCC emit DW_OP_const4u
2595 or DW_OP_const8u, depending on address size, rather than
2596 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
2597 The operand represents the offset at which the variable is within
2598 the thread local storage. */
2600 else if (data
+ 1 + addr_size
< end
2601 && (data
[0] == DW_OP_addr
2602 || (addr_size
== 4 && data
[0] == DW_OP_const4u
)
2603 || (addr_size
== 8 && data
[0] == DW_OP_const8u
))
2604 && data
[1 + addr_size
] == DW_OP_GNU_push_tls_address
2605 && piece_end_p (data
+ 2 + addr_size
, end
))
2608 offset
= extract_unsigned_integer (data
+ 1, addr_size
,
2609 gdbarch_byte_order (gdbarch
));
2611 fprintf_filtered (stream
,
2612 _("a thread-local variable at offset 0x%s "
2613 "in the thread-local storage for `%s'"),
2614 phex_nz (offset
, addr_size
), objfile
->name
);
2616 data
+= 1 + addr_size
+ 1;
2618 else if (data
[0] >= DW_OP_lit0
2619 && data
[0] <= DW_OP_lit31
2621 && data
[1] == DW_OP_stack_value
)
2623 fprintf_filtered (stream
, _("the constant %d"), data
[0] - DW_OP_lit0
);
2630 /* Disassemble an expression, stopping at the end of a piece or at the
2631 end of the expression. Returns a pointer to the next unread byte
2632 in the input expression. If ALL is nonzero, then this function
2633 will keep going until it reaches the end of the expression. */
2635 static const gdb_byte
*
2636 disassemble_dwarf_expression (struct ui_file
*stream
,
2637 struct gdbarch
*arch
, unsigned int addr_size
,
2639 const gdb_byte
*data
, const gdb_byte
*end
,
2641 struct dwarf2_per_cu_data
*per_cu
)
2643 const gdb_byte
*start
= data
;
2645 fprintf_filtered (stream
, _("a complex DWARF expression:\n"));
2649 || (data
[0] != DW_OP_piece
&& data
[0] != DW_OP_bit_piece
)))
2651 enum dwarf_location_atom op
= *data
++;
2656 name
= dwarf_stack_op_name (op
);
2659 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
2660 op
, (long) (data
- 1 - start
));
2661 fprintf_filtered (stream
, " % 4ld: %s", (long) (data
- 1 - start
), name
);
2666 ul
= extract_unsigned_integer (data
, addr_size
,
2667 gdbarch_byte_order (arch
));
2669 fprintf_filtered (stream
, " 0x%s", phex_nz (ul
, addr_size
));
2673 ul
= extract_unsigned_integer (data
, 1, gdbarch_byte_order (arch
));
2675 fprintf_filtered (stream
, " %s", pulongest (ul
));
2678 l
= extract_signed_integer (data
, 1, gdbarch_byte_order (arch
));
2680 fprintf_filtered (stream
, " %s", plongest (l
));
2683 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
2685 fprintf_filtered (stream
, " %s", pulongest (ul
));
2688 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
2690 fprintf_filtered (stream
, " %s", plongest (l
));
2693 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
2695 fprintf_filtered (stream
, " %s", pulongest (ul
));
2698 l
= extract_signed_integer (data
, 4, gdbarch_byte_order (arch
));
2700 fprintf_filtered (stream
, " %s", plongest (l
));
2703 ul
= extract_unsigned_integer (data
, 8, gdbarch_byte_order (arch
));
2705 fprintf_filtered (stream
, " %s", pulongest (ul
));
2708 l
= extract_signed_integer (data
, 8, gdbarch_byte_order (arch
));
2710 fprintf_filtered (stream
, " %s", plongest (l
));
2713 data
= read_uleb128 (data
, end
, &ul
);
2714 fprintf_filtered (stream
, " %s", pulongest (ul
));
2717 data
= read_sleb128 (data
, end
, &l
);
2718 fprintf_filtered (stream
, " %s", plongest (l
));
2753 fprintf_filtered (stream
, " [$%s]",
2754 locexpr_regname (arch
, op
- DW_OP_reg0
));
2758 data
= read_uleb128 (data
, end
, &ul
);
2759 fprintf_filtered (stream
, " %s [$%s]", pulongest (ul
),
2760 locexpr_regname (arch
, (int) ul
));
2763 case DW_OP_implicit_value
:
2764 data
= read_uleb128 (data
, end
, &ul
);
2766 fprintf_filtered (stream
, " %s", pulongest (ul
));
2801 data
= read_sleb128 (data
, end
, &l
);
2802 fprintf_filtered (stream
, " %s [$%s]", plongest (l
),
2803 locexpr_regname (arch
, op
- DW_OP_breg0
));
2807 data
= read_uleb128 (data
, end
, &ul
);
2808 data
= read_sleb128 (data
, end
, &l
);
2809 fprintf_filtered (stream
, " register %s [$%s] offset %s",
2811 locexpr_regname (arch
, (int) ul
),
2816 data
= read_sleb128 (data
, end
, &l
);
2817 fprintf_filtered (stream
, " %s", plongest (l
));
2820 case DW_OP_xderef_size
:
2821 case DW_OP_deref_size
:
2823 fprintf_filtered (stream
, " %d", *data
);
2827 case DW_OP_plus_uconst
:
2828 data
= read_uleb128 (data
, end
, &ul
);
2829 fprintf_filtered (stream
, " %s", pulongest (ul
));
2833 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
2835 fprintf_filtered (stream
, " to %ld",
2836 (long) (data
+ l
- start
));
2840 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
2842 fprintf_filtered (stream
, " %ld",
2843 (long) (data
+ l
- start
));
2847 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
2849 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 2));
2853 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
2855 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 4));
2858 case DW_OP_call_ref
:
2859 ul
= extract_unsigned_integer (data
, offset_size
,
2860 gdbarch_byte_order (arch
));
2861 data
+= offset_size
;
2862 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, offset_size
));
2866 data
= read_uleb128 (data
, end
, &ul
);
2867 fprintf_filtered (stream
, " %s (bytes)", pulongest (ul
));
2870 case DW_OP_bit_piece
:
2874 data
= read_uleb128 (data
, end
, &ul
);
2875 data
= read_uleb128 (data
, end
, &offset
);
2876 fprintf_filtered (stream
, " size %s offset %s (bits)",
2877 pulongest (ul
), pulongest (offset
));
2881 case DW_OP_GNU_implicit_pointer
:
2883 ul
= extract_unsigned_integer (data
, offset_size
,
2884 gdbarch_byte_order (arch
));
2885 data
+= offset_size
;
2887 data
= read_sleb128 (data
, end
, &l
);
2889 fprintf_filtered (stream
, " DIE %s offset %s",
2890 phex_nz (ul
, offset_size
),
2895 case DW_OP_GNU_deref_type
:
2897 int addr_size
= *data
++;
2901 data
= read_uleb128 (data
, end
, &offset
);
2902 type
= dwarf2_get_die_type (offset
, per_cu
);
2903 fprintf_filtered (stream
, "<");
2904 type_print (type
, "", stream
, -1);
2905 fprintf_filtered (stream
, " [0x%s]> %d", phex_nz (offset
, 0),
2910 case DW_OP_GNU_const_type
:
2915 data
= read_uleb128 (data
, end
, &type_die
);
2916 type
= dwarf2_get_die_type (type_die
, per_cu
);
2917 fprintf_filtered (stream
, "<");
2918 type_print (type
, "", stream
, -1);
2919 fprintf_filtered (stream
, " [0x%s]>", phex_nz (type_die
, 0));
2923 case DW_OP_GNU_regval_type
:
2925 ULONGEST type_die
, reg
;
2928 data
= read_uleb128 (data
, end
, ®
);
2929 data
= read_uleb128 (data
, end
, &type_die
);
2931 type
= dwarf2_get_die_type (type_die
, per_cu
);
2932 fprintf_filtered (stream
, "<");
2933 type_print (type
, "", stream
, -1);
2934 fprintf_filtered (stream
, " [0x%s]> [$%s]", phex_nz (type_die
, 0),
2935 locexpr_regname (arch
, reg
));
2939 case DW_OP_GNU_convert
:
2940 case DW_OP_GNU_reinterpret
:
2944 data
= read_uleb128 (data
, end
, &type_die
);
2947 fprintf_filtered (stream
, "<0>");
2952 type
= dwarf2_get_die_type (type_die
, per_cu
);
2953 fprintf_filtered (stream
, "<");
2954 type_print (type
, "", stream
, -1);
2955 fprintf_filtered (stream
, " [0x%s]>", phex_nz (type_die
, 0));
2961 fprintf_filtered (stream
, "\n");
2967 /* Describe a single location, which may in turn consist of multiple
2971 locexpr_describe_location_1 (struct symbol
*symbol
, CORE_ADDR addr
,
2972 struct ui_file
*stream
,
2973 const gdb_byte
*data
, int size
,
2974 struct objfile
*objfile
, unsigned int addr_size
,
2975 int offset_size
, struct dwarf2_per_cu_data
*per_cu
)
2977 const gdb_byte
*end
= data
+ size
;
2978 int first_piece
= 1, bad
= 0;
2982 const gdb_byte
*here
= data
;
2983 int disassemble
= 1;
2988 fprintf_filtered (stream
, _(", and "));
2990 if (!dwarf2_always_disassemble
)
2992 data
= locexpr_describe_location_piece (symbol
, stream
,
2994 data
, end
, addr_size
);
2995 /* If we printed anything, or if we have an empty piece,
2996 then don't disassemble. */
2998 || data
[0] == DW_OP_piece
2999 || data
[0] == DW_OP_bit_piece
)
3003 data
= disassemble_dwarf_expression (stream
,
3004 get_objfile_arch (objfile
),
3005 addr_size
, offset_size
, data
, end
,
3006 dwarf2_always_disassemble
,
3011 int empty
= data
== here
;
3014 fprintf_filtered (stream
, " ");
3015 if (data
[0] == DW_OP_piece
)
3019 data
= read_uleb128 (data
+ 1, end
, &bytes
);
3022 fprintf_filtered (stream
, _("an empty %s-byte piece"),
3025 fprintf_filtered (stream
, _(" [%s-byte piece]"),
3028 else if (data
[0] == DW_OP_bit_piece
)
3030 ULONGEST bits
, offset
;
3032 data
= read_uleb128 (data
+ 1, end
, &bits
);
3033 data
= read_uleb128 (data
, end
, &offset
);
3036 fprintf_filtered (stream
,
3037 _("an empty %s-bit piece"),
3040 fprintf_filtered (stream
,
3041 _(" [%s-bit piece, offset %s bits]"),
3042 pulongest (bits
), pulongest (offset
));
3052 if (bad
|| data
> end
)
3053 error (_("Corrupted DWARF2 expression for \"%s\"."),
3054 SYMBOL_PRINT_NAME (symbol
));
3057 /* Print a natural-language description of SYMBOL to STREAM. This
3058 version is for a symbol with a single location. */
3061 locexpr_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
3062 struct ui_file
*stream
)
3064 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3065 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
3066 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3067 int offset_size
= dwarf2_per_cu_offset_size (dlbaton
->per_cu
);
3069 locexpr_describe_location_1 (symbol
, addr
, stream
,
3070 dlbaton
->data
, dlbaton
->size
,
3071 objfile
, addr_size
, offset_size
,
3075 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3076 any necessary bytecode in AX. */
3079 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
3080 struct agent_expr
*ax
, struct axs_value
*value
)
3082 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3083 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3085 if (dlbaton
->data
== NULL
|| dlbaton
->size
== 0)
3086 value
->optimized_out
= 1;
3088 dwarf2_compile_expr_to_ax (ax
, value
, gdbarch
, addr_size
,
3089 dlbaton
->data
, dlbaton
->data
+ dlbaton
->size
,
3093 /* The set of location functions used with the DWARF-2 expression
3095 const struct symbol_computed_ops dwarf2_locexpr_funcs
= {
3096 locexpr_read_variable
,
3097 locexpr_read_needs_frame
,
3098 locexpr_describe_location
,
3099 locexpr_tracepoint_var_ref
3103 /* Wrapper functions for location lists. These generally find
3104 the appropriate location expression and call something above. */
3106 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3107 evaluator to calculate the location. */
3108 static struct value
*
3109 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
3111 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3113 const gdb_byte
*data
;
3115 CORE_ADDR pc
= frame
? get_frame_address_in_block (frame
) : 0;
3117 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
3119 val
= allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
3121 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, data
, size
,
3127 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
3129 loclist_read_needs_frame (struct symbol
*symbol
)
3131 /* If there's a location list, then assume we need to have a frame
3132 to choose the appropriate location expression. With tracking of
3133 global variables this is not necessarily true, but such tracking
3134 is disabled in GCC at the moment until we figure out how to
3140 /* Print a natural-language description of SYMBOL to STREAM. This
3141 version applies when there is a list of different locations, each
3142 with a specified address range. */
3145 loclist_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
3146 struct ui_file
*stream
)
3148 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3149 CORE_ADDR low
, high
;
3150 const gdb_byte
*loc_ptr
, *buf_end
;
3151 int length
, first
= 1;
3152 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
3153 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3154 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
3155 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3156 int offset_size
= dwarf2_per_cu_offset_size (dlbaton
->per_cu
);
3157 int signed_addr_p
= bfd_get_sign_extend_vma (objfile
->obfd
);
3158 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
3159 /* Adjust base_address for relocatable objects. */
3160 CORE_ADDR base_offset
= dwarf2_per_cu_text_offset (dlbaton
->per_cu
);
3161 CORE_ADDR base_address
= dlbaton
->base_address
+ base_offset
;
3163 loc_ptr
= dlbaton
->data
;
3164 buf_end
= dlbaton
->data
+ dlbaton
->size
;
3166 fprintf_filtered (stream
, _("multi-location:\n"));
3168 /* Iterate through locations until we run out. */
3171 if (buf_end
- loc_ptr
< 2 * addr_size
)
3172 error (_("Corrupted DWARF expression for symbol \"%s\"."),
3173 SYMBOL_PRINT_NAME (symbol
));
3176 low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
3178 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
3179 loc_ptr
+= addr_size
;
3182 high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
3184 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
3185 loc_ptr
+= addr_size
;
3187 /* A base-address-selection entry. */
3188 if ((low
& base_mask
) == base_mask
)
3190 base_address
= high
+ base_offset
;
3191 fprintf_filtered (stream
, _(" Base address %s"),
3192 paddress (gdbarch
, base_address
));
3196 /* An end-of-list entry. */
3197 if (low
== 0 && high
== 0)
3200 /* Otherwise, a location expression entry. */
3201 low
+= base_address
;
3202 high
+= base_address
;
3204 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
3207 /* (It would improve readability to print only the minimum
3208 necessary digits of the second number of the range.) */
3209 fprintf_filtered (stream
, _(" Range %s-%s: "),
3210 paddress (gdbarch
, low
), paddress (gdbarch
, high
));
3212 /* Now describe this particular location. */
3213 locexpr_describe_location_1 (symbol
, low
, stream
, loc_ptr
, length
,
3214 objfile
, addr_size
, offset_size
,
3217 fprintf_filtered (stream
, "\n");
3223 /* Describe the location of SYMBOL as an agent value in VALUE, generating
3224 any necessary bytecode in AX. */
3226 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
3227 struct agent_expr
*ax
, struct axs_value
*value
)
3229 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
3230 const gdb_byte
*data
;
3232 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
3234 data
= dwarf2_find_location_expression (dlbaton
, &size
, ax
->scope
);
3235 if (data
== NULL
|| size
== 0)
3236 value
->optimized_out
= 1;
3238 dwarf2_compile_expr_to_ax (ax
, value
, gdbarch
, addr_size
, data
, data
+ size
,
3242 /* The set of location functions used with the DWARF-2 expression
3243 evaluator and location lists. */
3244 const struct symbol_computed_ops dwarf2_loclist_funcs
= {
3245 loclist_read_variable
,
3246 loclist_read_needs_frame
,
3247 loclist_describe_location
,
3248 loclist_tracepoint_var_ref
3252 _initialize_dwarf2loc (void)
3254 add_setshow_zinteger_cmd ("entry-values", class_maintenance
,
3255 &entry_values_debug
,
3256 _("Set entry values and tail call frames "
3258 _("Show entry values and tail call frames "
3260 _("When non-zero, the process of determining "
3261 "parameter values from function entry point "
3262 "and tail call frames will be printed."),
3264 show_entry_values_debug
,
3265 &setdebuglist
, &showdebuglist
);