1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
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"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
45 extern int dwarf2_always_disassemble
;
48 dwarf_expr_frame_base_1 (struct symbol
*framefunc
, CORE_ADDR pc
,
49 const gdb_byte
**start
, size_t *length
);
51 /* A helper function for dealing with location lists. Given a
52 symbol baton (BATON) and a pc value (PC), find the appropriate
53 location expression, set *LOCEXPR_LENGTH, and return a pointer
54 to the beginning of the expression. Returns NULL on failure.
56 For now, only return the first matching location expression; there
57 can be more than one in the list. */
59 static const gdb_byte
*
60 find_location_expression (struct dwarf2_loclist_baton
*baton
,
61 size_t *locexpr_length
, CORE_ADDR pc
)
64 const gdb_byte
*loc_ptr
, *buf_end
;
66 struct objfile
*objfile
= dwarf2_per_cu_objfile (baton
->per_cu
);
67 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
68 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
69 unsigned int addr_size
= dwarf2_per_cu_addr_size (baton
->per_cu
);
70 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
71 /* Adjust base_address for relocatable objects. */
72 CORE_ADDR base_offset
= ANOFFSET (objfile
->section_offsets
,
73 SECT_OFF_TEXT (objfile
));
74 CORE_ADDR base_address
= baton
->base_address
+ base_offset
;
76 loc_ptr
= baton
->data
;
77 buf_end
= baton
->data
+ baton
->size
;
81 if (buf_end
- loc_ptr
< 2 * addr_size
)
82 error (_("find_location_expression: Corrupted DWARF expression."));
84 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
87 /* A base-address-selection entry. */
90 base_address
= dwarf2_read_address (gdbarch
,
91 loc_ptr
, buf_end
, addr_size
);
96 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
99 /* An end-of-list entry. */
100 if (low
== 0 && high
== 0)
103 /* Otherwise, a location expression entry. */
105 high
+= base_address
;
107 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
110 if (pc
>= low
&& pc
< high
)
112 *locexpr_length
= length
;
120 /* This is the baton used when performing dwarf2 expression
122 struct dwarf_expr_baton
124 struct frame_info
*frame
;
125 struct dwarf2_per_cu_data
*per_cu
;
128 /* Helper functions for dwarf2_evaluate_loc_desc. */
130 /* Using the frame specified in BATON, return the value of register
131 REGNUM, treated as a pointer. */
133 dwarf_expr_read_reg (void *baton
, int dwarf_regnum
)
135 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
136 struct gdbarch
*gdbarch
= get_frame_arch (debaton
->frame
);
140 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, dwarf_regnum
);
141 result
= address_from_register (builtin_type (gdbarch
)->builtin_data_ptr
,
142 regnum
, debaton
->frame
);
146 /* Read memory at ADDR (length LEN) into BUF. */
149 dwarf_expr_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
151 read_memory (addr
, buf
, len
);
154 /* Using the frame specified in BATON, find the location expression
155 describing the frame base. Return a pointer to it in START and
156 its length in LENGTH. */
158 dwarf_expr_frame_base (void *baton
, const gdb_byte
**start
, size_t * length
)
160 /* FIXME: cagney/2003-03-26: This code should be using
161 get_frame_base_address(), and then implement a dwarf2 specific
163 struct symbol
*framefunc
;
164 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
166 /* Use block_linkage_function, which returns a real (not inlined)
167 function, instead of get_frame_function, which may return an
169 framefunc
= block_linkage_function (get_frame_block (debaton
->frame
, NULL
));
171 /* If we found a frame-relative symbol then it was certainly within
172 some function associated with a frame. If we can't find the frame,
173 something has gone wrong. */
174 gdb_assert (framefunc
!= NULL
);
176 dwarf_expr_frame_base_1 (framefunc
,
177 get_frame_address_in_block (debaton
->frame
),
182 dwarf_expr_frame_base_1 (struct symbol
*framefunc
, CORE_ADDR pc
,
183 const gdb_byte
**start
, size_t *length
)
185 if (SYMBOL_LOCATION_BATON (framefunc
) == NULL
)
187 else if (SYMBOL_COMPUTED_OPS (framefunc
) == &dwarf2_loclist_funcs
)
189 struct dwarf2_loclist_baton
*symbaton
;
191 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
192 *start
= find_location_expression (symbaton
, length
, pc
);
196 struct dwarf2_locexpr_baton
*symbaton
;
198 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
199 if (symbaton
!= NULL
)
201 *length
= symbaton
->size
;
202 *start
= symbaton
->data
;
209 error (_("Could not find the frame base for \"%s\"."),
210 SYMBOL_NATURAL_NAME (framefunc
));
213 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
214 the frame in BATON. */
217 dwarf_expr_frame_cfa (void *baton
)
219 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
221 return dwarf2_frame_cfa (debaton
->frame
);
224 /* Using the objfile specified in BATON, find the address for the
225 current thread's thread-local storage with offset OFFSET. */
227 dwarf_expr_tls_address (void *baton
, CORE_ADDR offset
)
229 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
230 struct objfile
*objfile
= dwarf2_per_cu_objfile (debaton
->per_cu
);
232 return target_translate_tls_address (objfile
, offset
);
235 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in current CU
236 (as is PER_CU). State of the CTX is not affected by the call and return. */
239 per_cu_dwarf_call (struct dwarf_expr_context
*ctx
, size_t die_offset
,
240 struct dwarf2_per_cu_data
*per_cu
)
242 struct dwarf2_locexpr_baton block
;
244 block
= dwarf2_fetch_die_location_block (die_offset
, per_cu
);
246 /* DW_OP_call_ref is currently not supported. */
247 gdb_assert (block
.per_cu
== per_cu
);
249 dwarf_expr_eval (ctx
, block
.data
, block
.size
);
252 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
255 dwarf_expr_dwarf_call (struct dwarf_expr_context
*ctx
, size_t die_offset
)
257 struct dwarf_expr_baton
*debaton
= ctx
->baton
;
259 return per_cu_dwarf_call (ctx
, die_offset
, debaton
->per_cu
);
264 /* Reference count. */
267 /* The number of pieces used to describe this variable. */
270 /* The target address size, used only for DWARF_VALUE_STACK. */
273 /* The pieces themselves. */
274 struct dwarf_expr_piece
*pieces
;
277 /* Allocate a closure for a value formed from separately-described
280 static struct piece_closure
*
281 allocate_piece_closure (int n_pieces
, struct dwarf_expr_piece
*pieces
,
284 struct piece_closure
*c
= XZALLOC (struct piece_closure
);
287 c
->n_pieces
= n_pieces
;
288 c
->addr_size
= addr_size
;
289 c
->pieces
= XCALLOC (n_pieces
, struct dwarf_expr_piece
);
291 memcpy (c
->pieces
, pieces
, n_pieces
* sizeof (struct dwarf_expr_piece
));
296 /* The lowest-level function to extract bits from a byte buffer.
297 SOURCE is the buffer. It is updated if we read to the end of a
299 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
300 updated to reflect the number of bits actually read.
301 NBITS is the number of bits we want to read. It is updated to
302 reflect the number of bits actually read. This function may read
304 BITS_BIG_ENDIAN is taken directly from gdbarch.
305 This function returns the extracted bits. */
308 extract_bits_primitive (const gdb_byte
**source
,
309 unsigned int *source_offset_bits
,
310 int *nbits
, int bits_big_endian
)
312 unsigned int avail
, mask
, datum
;
314 gdb_assert (*source_offset_bits
< 8);
316 avail
= 8 - *source_offset_bits
;
320 mask
= (1 << avail
) - 1;
323 datum
>>= 8 - (*source_offset_bits
+ *nbits
);
325 datum
>>= *source_offset_bits
;
329 *source_offset_bits
+= avail
;
330 if (*source_offset_bits
>= 8)
332 *source_offset_bits
-= 8;
339 /* Extract some bits from a source buffer and move forward in the
342 SOURCE is the source buffer. It is updated as bytes are read.
343 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
345 NBITS is the number of bits to read.
346 BITS_BIG_ENDIAN is taken directly from gdbarch.
348 This function returns the bits that were read. */
351 extract_bits (const gdb_byte
**source
, unsigned int *source_offset_bits
,
352 int nbits
, int bits_big_endian
)
356 gdb_assert (nbits
> 0 && nbits
<= 8);
358 datum
= extract_bits_primitive (source
, source_offset_bits
, &nbits
,
364 more
= extract_bits_primitive (source
, source_offset_bits
, &nbits
,
376 /* Write some bits into a buffer and move forward in the buffer.
378 DATUM is the bits to write. The low-order bits of DATUM are used.
379 DEST is the destination buffer. It is updated as bytes are
381 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
383 NBITS is the number of valid bits in DATUM.
384 BITS_BIG_ENDIAN is taken directly from gdbarch. */
387 insert_bits (unsigned int datum
,
388 gdb_byte
*dest
, unsigned int dest_offset_bits
,
389 int nbits
, int bits_big_endian
)
393 gdb_assert (dest_offset_bits
>= 0 && dest_offset_bits
+ nbits
<= 8);
395 mask
= (1 << nbits
) - 1;
398 datum
<<= 8 - (dest_offset_bits
+ nbits
);
399 mask
<<= 8 - (dest_offset_bits
+ nbits
);
403 datum
<<= dest_offset_bits
;
404 mask
<<= dest_offset_bits
;
407 gdb_assert ((datum
& ~mask
) == 0);
409 *dest
= (*dest
& ~mask
) | datum
;
412 /* Copy bits from a source to a destination.
414 DEST is where the bits should be written.
415 DEST_OFFSET_BITS is the bit offset into DEST.
416 SOURCE is the source of bits.
417 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
418 BIT_COUNT is the number of bits to copy.
419 BITS_BIG_ENDIAN is taken directly from gdbarch. */
422 copy_bitwise (gdb_byte
*dest
, unsigned int dest_offset_bits
,
423 const gdb_byte
*source
, unsigned int source_offset_bits
,
424 unsigned int bit_count
,
427 unsigned int dest_avail
;
430 /* Reduce everything to byte-size pieces. */
431 dest
+= dest_offset_bits
/ 8;
432 dest_offset_bits
%= 8;
433 source
+= source_offset_bits
/ 8;
434 source_offset_bits
%= 8;
436 dest_avail
= 8 - dest_offset_bits
% 8;
438 /* See if we can fill the first destination byte. */
439 if (dest_avail
< bit_count
)
441 datum
= extract_bits (&source
, &source_offset_bits
, dest_avail
,
443 insert_bits (datum
, dest
, dest_offset_bits
, dest_avail
, bits_big_endian
);
445 dest_offset_bits
= 0;
446 bit_count
-= dest_avail
;
449 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
450 than 8 bits remaining. */
451 gdb_assert (dest_offset_bits
% 8 == 0 || bit_count
< 8);
452 for (; bit_count
>= 8; bit_count
-= 8)
454 datum
= extract_bits (&source
, &source_offset_bits
, 8, bits_big_endian
);
455 *dest
++ = (gdb_byte
) datum
;
458 /* Finally, we may have a few leftover bits. */
459 gdb_assert (bit_count
<= 8 - dest_offset_bits
% 8);
462 datum
= extract_bits (&source
, &source_offset_bits
, bit_count
,
464 insert_bits (datum
, dest
, dest_offset_bits
, bit_count
, bits_big_endian
);
469 read_pieced_value (struct value
*v
)
473 ULONGEST bits_to_skip
;
475 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
476 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (v
));
478 size_t buffer_size
= 0;
480 struct cleanup
*cleanup
;
482 = gdbarch_bits_big_endian (get_type_arch (value_type (v
)));
484 if (value_type (v
) != value_enclosing_type (v
))
485 internal_error (__FILE__
, __LINE__
,
486 _("Should not be able to create a lazy value with "
487 "an enclosing type"));
489 cleanup
= make_cleanup (free_current_contents
, &buffer
);
491 contents
= value_contents_raw (v
);
492 bits_to_skip
= 8 * value_offset (v
);
493 if (value_bitsize (v
))
495 bits_to_skip
+= value_bitpos (v
);
496 type_len
= value_bitsize (v
);
499 type_len
= 8 * TYPE_LENGTH (value_type (v
));
501 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
503 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
504 size_t this_size
, this_size_bits
;
505 long dest_offset_bits
, source_offset_bits
, source_offset
;
506 const gdb_byte
*intermediate_buffer
;
508 /* Compute size, source, and destination offsets for copying, in
510 this_size_bits
= p
->size
;
511 if (bits_to_skip
> 0 && bits_to_skip
>= this_size_bits
)
513 bits_to_skip
-= this_size_bits
;
516 if (this_size_bits
> type_len
- offset
)
517 this_size_bits
= type_len
- offset
;
518 if (bits_to_skip
> 0)
520 dest_offset_bits
= 0;
521 source_offset_bits
= bits_to_skip
;
522 this_size_bits
-= bits_to_skip
;
527 dest_offset_bits
= offset
;
528 source_offset_bits
= 0;
531 this_size
= (this_size_bits
+ source_offset_bits
% 8 + 7) / 8;
532 source_offset
= source_offset_bits
/ 8;
533 if (buffer_size
< this_size
)
535 buffer_size
= this_size
;
536 buffer
= xrealloc (buffer
, buffer_size
);
538 intermediate_buffer
= buffer
;
540 /* Copy from the source to DEST_BUFFER. */
543 case DWARF_VALUE_REGISTER
:
545 struct gdbarch
*arch
= get_frame_arch (frame
);
546 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
,
548 int reg_offset
= source_offset
;
550 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
551 && this_size
< register_size (arch
, gdb_regnum
))
553 /* Big-endian, and we want less than full size. */
554 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
555 /* We want the lower-order THIS_SIZE_BITS of the bytes
556 we extract from the register. */
557 source_offset_bits
+= 8 * this_size
- this_size_bits
;
560 if (gdb_regnum
!= -1)
562 get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
567 error (_("Unable to access DWARF register number %s"),
568 paddress (arch
, p
->v
.expr
.value
));
573 case DWARF_VALUE_MEMORY
:
574 if (p
->v
.expr
.in_stack_memory
)
575 read_stack (p
->v
.expr
.value
+ source_offset
, buffer
, this_size
);
577 read_memory (p
->v
.expr
.value
+ source_offset
, buffer
, this_size
);
580 case DWARF_VALUE_STACK
:
582 struct gdbarch
*gdbarch
= get_type_arch (value_type (v
));
583 size_t n
= this_size
;
585 if (n
> c
->addr_size
- source_offset
)
586 n
= (c
->addr_size
>= source_offset
587 ? c
->addr_size
- source_offset
593 else if (source_offset
== 0)
594 store_unsigned_integer (buffer
, n
,
595 gdbarch_byte_order (gdbarch
),
599 gdb_byte bytes
[sizeof (ULONGEST
)];
601 store_unsigned_integer (bytes
, n
+ source_offset
,
602 gdbarch_byte_order (gdbarch
),
604 memcpy (buffer
, bytes
+ source_offset
, n
);
609 case DWARF_VALUE_LITERAL
:
611 size_t n
= this_size
;
613 if (n
> p
->v
.literal
.length
- source_offset
)
614 n
= (p
->v
.literal
.length
>= source_offset
615 ? p
->v
.literal
.length
- source_offset
618 intermediate_buffer
= p
->v
.literal
.data
+ source_offset
;
622 case DWARF_VALUE_OPTIMIZED_OUT
:
623 set_value_optimized_out (v
, 1);
627 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
630 if (p
->location
!= DWARF_VALUE_OPTIMIZED_OUT
)
631 copy_bitwise (contents
, dest_offset_bits
,
632 intermediate_buffer
, source_offset_bits
% 8,
633 this_size_bits
, bits_big_endian
);
635 offset
+= this_size_bits
;
638 do_cleanups (cleanup
);
642 write_pieced_value (struct value
*to
, struct value
*from
)
646 ULONGEST bits_to_skip
;
647 const gdb_byte
*contents
;
648 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (to
);
649 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (to
));
651 size_t buffer_size
= 0;
653 struct cleanup
*cleanup
;
655 = gdbarch_bits_big_endian (get_type_arch (value_type (to
)));
659 set_value_optimized_out (to
, 1);
663 cleanup
= make_cleanup (free_current_contents
, &buffer
);
665 contents
= value_contents (from
);
666 bits_to_skip
= 8 * value_offset (to
);
667 if (value_bitsize (to
))
669 bits_to_skip
+= value_bitpos (to
);
670 type_len
= value_bitsize (to
);
673 type_len
= 8 * TYPE_LENGTH (value_type (to
));
675 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
677 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
678 size_t this_size_bits
, this_size
;
679 long dest_offset_bits
, source_offset_bits
, dest_offset
, source_offset
;
681 const gdb_byte
*source_buffer
;
683 this_size_bits
= p
->size
;
684 if (bits_to_skip
> 0 && bits_to_skip
>= this_size_bits
)
686 bits_to_skip
-= this_size_bits
;
689 if (this_size_bits
> type_len
- offset
)
690 this_size_bits
= type_len
- offset
;
691 if (bits_to_skip
> 0)
693 dest_offset_bits
= bits_to_skip
;
694 source_offset_bits
= 0;
695 this_size_bits
-= bits_to_skip
;
700 dest_offset_bits
= 0;
701 source_offset_bits
= offset
;
704 this_size
= (this_size_bits
+ source_offset_bits
% 8 + 7) / 8;
705 source_offset
= source_offset_bits
/ 8;
706 dest_offset
= dest_offset_bits
/ 8;
707 if (dest_offset_bits
% 8 == 0 && source_offset_bits
% 8 == 0)
709 source_buffer
= contents
+ source_offset
;
714 if (buffer_size
< this_size
)
716 buffer_size
= this_size
;
717 buffer
= xrealloc (buffer
, buffer_size
);
719 source_buffer
= buffer
;
725 case DWARF_VALUE_REGISTER
:
727 struct gdbarch
*arch
= get_frame_arch (frame
);
728 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->v
.expr
.value
);
729 int reg_offset
= dest_offset
;
731 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
732 && this_size
<= register_size (arch
, gdb_regnum
))
733 /* Big-endian, and we want less than full size. */
734 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
736 if (gdb_regnum
!= -1)
740 get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
742 copy_bitwise (buffer
, dest_offset_bits
,
743 contents
, source_offset_bits
,
748 put_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
749 this_size
, source_buffer
);
753 error (_("Unable to write to DWARF register number %s"),
754 paddress (arch
, p
->v
.expr
.value
));
758 case DWARF_VALUE_MEMORY
:
761 /* Only the first and last bytes can possibly have any
763 read_memory (p
->v
.expr
.value
+ dest_offset
, buffer
, 1);
764 read_memory (p
->v
.expr
.value
+ dest_offset
+ this_size
- 1,
765 buffer
+ this_size
- 1, 1);
766 copy_bitwise (buffer
, dest_offset_bits
,
767 contents
, source_offset_bits
,
772 write_memory (p
->v
.expr
.value
+ dest_offset
,
773 source_buffer
, this_size
);
776 set_value_optimized_out (to
, 1);
779 offset
+= this_size_bits
;
782 do_cleanups (cleanup
);
786 check_pieced_value_bits (const struct value
*value
, int bit_offset
,
787 int bit_length
, int validity
)
789 struct piece_closure
*c
790 = (struct piece_closure
*) value_computed_closure (value
);
793 bit_offset
+= 8 * value_offset (value
);
794 if (value_bitsize (value
))
795 bit_offset
+= value_bitpos (value
);
797 for (i
= 0; i
< c
->n_pieces
&& bit_length
> 0; i
++)
799 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
800 size_t this_size_bits
= p
->size
;
804 if (bit_offset
>= this_size_bits
)
806 bit_offset
-= this_size_bits
;
810 bit_length
-= this_size_bits
- bit_offset
;
814 bit_length
-= this_size_bits
;
816 if (p
->location
== DWARF_VALUE_OPTIMIZED_OUT
)
832 check_pieced_value_validity (const struct value
*value
, int bit_offset
,
835 return check_pieced_value_bits (value
, bit_offset
, bit_length
, 1);
839 check_pieced_value_invalid (const struct value
*value
)
841 return check_pieced_value_bits (value
, 0,
842 8 * TYPE_LENGTH (value_type (value
)), 0);
846 copy_pieced_value_closure (const struct value
*v
)
848 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
855 free_pieced_value_closure (struct value
*v
)
857 struct piece_closure
*c
= (struct piece_closure
*) value_computed_closure (v
);
867 /* Functions for accessing a variable described by DW_OP_piece. */
868 static struct lval_funcs pieced_value_funcs
= {
871 check_pieced_value_validity
,
872 check_pieced_value_invalid
,
873 copy_pieced_value_closure
,
874 free_pieced_value_closure
877 /* Evaluate a location description, starting at DATA and with length
878 SIZE, to find the current location of variable of TYPE in the context
881 static struct value
*
882 dwarf2_evaluate_loc_desc (struct type
*type
, struct frame_info
*frame
,
883 const gdb_byte
*data
, unsigned short size
,
884 struct dwarf2_per_cu_data
*per_cu
)
886 struct value
*retval
;
887 struct dwarf_expr_baton baton
;
888 struct dwarf_expr_context
*ctx
;
889 struct cleanup
*old_chain
;
893 retval
= allocate_value (type
);
894 VALUE_LVAL (retval
) = not_lval
;
895 set_value_optimized_out (retval
, 1);
900 baton
.per_cu
= per_cu
;
902 ctx
= new_dwarf_expr_context ();
903 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
905 ctx
->gdbarch
= get_objfile_arch (dwarf2_per_cu_objfile (per_cu
));
906 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
908 ctx
->read_reg
= dwarf_expr_read_reg
;
909 ctx
->read_mem
= dwarf_expr_read_mem
;
910 ctx
->get_frame_base
= dwarf_expr_frame_base
;
911 ctx
->get_frame_cfa
= dwarf_expr_frame_cfa
;
912 ctx
->get_tls_address
= dwarf_expr_tls_address
;
913 ctx
->dwarf_call
= dwarf_expr_dwarf_call
;
915 dwarf_expr_eval (ctx
, data
, size
);
916 if (ctx
->num_pieces
> 0)
918 struct piece_closure
*c
;
919 struct frame_id frame_id
= get_frame_id (frame
);
921 c
= allocate_piece_closure (ctx
->num_pieces
, ctx
->pieces
,
923 retval
= allocate_computed_value (type
, &pieced_value_funcs
, c
);
924 VALUE_FRAME_ID (retval
) = frame_id
;
928 switch (ctx
->location
)
930 case DWARF_VALUE_REGISTER
:
932 struct gdbarch
*arch
= get_frame_arch (frame
);
933 CORE_ADDR dwarf_regnum
= dwarf_expr_fetch (ctx
, 0);
934 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_regnum
);
936 if (gdb_regnum
!= -1)
937 retval
= value_from_register (type
, gdb_regnum
, frame
);
939 error (_("Unable to access DWARF register number %s"),
940 paddress (arch
, dwarf_regnum
));
944 case DWARF_VALUE_MEMORY
:
946 CORE_ADDR address
= dwarf_expr_fetch (ctx
, 0);
947 int in_stack_memory
= dwarf_expr_fetch_in_stack_memory (ctx
, 0);
949 retval
= allocate_value (type
);
950 VALUE_LVAL (retval
) = lval_memory
;
951 set_value_lazy (retval
, 1);
953 set_value_stack (retval
, 1);
954 set_value_address (retval
, address
);
958 case DWARF_VALUE_STACK
:
960 ULONGEST value
= (ULONGEST
) dwarf_expr_fetch (ctx
, 0);
962 size_t n
= ctx
->addr_size
;
964 retval
= allocate_value (type
);
965 contents
= value_contents_raw (retval
);
966 if (n
> TYPE_LENGTH (type
))
967 n
= TYPE_LENGTH (type
);
968 store_unsigned_integer (contents
, n
,
969 gdbarch_byte_order (ctx
->gdbarch
),
974 case DWARF_VALUE_LITERAL
:
979 retval
= allocate_value (type
);
980 contents
= value_contents_raw (retval
);
981 if (n
> TYPE_LENGTH (type
))
982 n
= TYPE_LENGTH (type
);
983 memcpy (contents
, ctx
->data
, n
);
987 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
988 it can only be encountered when making a piece. */
989 case DWARF_VALUE_OPTIMIZED_OUT
:
991 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
995 set_value_initialized (retval
, ctx
->initialized
);
997 do_cleanups (old_chain
);
1002 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
1004 struct needs_frame_baton
1007 struct dwarf2_per_cu_data
*per_cu
;
1010 /* Reads from registers do require a frame. */
1012 needs_frame_read_reg (void *baton
, int regnum
)
1014 struct needs_frame_baton
*nf_baton
= baton
;
1016 nf_baton
->needs_frame
= 1;
1020 /* Reads from memory do not require a frame. */
1022 needs_frame_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
1024 memset (buf
, 0, len
);
1027 /* Frame-relative accesses do require a frame. */
1029 needs_frame_frame_base (void *baton
, const gdb_byte
**start
, size_t * length
)
1031 static gdb_byte lit0
= DW_OP_lit0
;
1032 struct needs_frame_baton
*nf_baton
= baton
;
1037 nf_baton
->needs_frame
= 1;
1040 /* CFA accesses require a frame. */
1043 needs_frame_frame_cfa (void *baton
)
1045 struct needs_frame_baton
*nf_baton
= baton
;
1047 nf_baton
->needs_frame
= 1;
1051 /* Thread-local accesses do require a frame. */
1053 needs_frame_tls_address (void *baton
, CORE_ADDR offset
)
1055 struct needs_frame_baton
*nf_baton
= baton
;
1057 nf_baton
->needs_frame
= 1;
1061 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
1064 needs_frame_dwarf_call (struct dwarf_expr_context
*ctx
, size_t die_offset
)
1066 struct needs_frame_baton
*nf_baton
= ctx
->baton
;
1068 return per_cu_dwarf_call (ctx
, die_offset
, nf_baton
->per_cu
);
1071 /* Return non-zero iff the location expression at DATA (length SIZE)
1072 requires a frame to evaluate. */
1075 dwarf2_loc_desc_needs_frame (const gdb_byte
*data
, unsigned short size
,
1076 struct dwarf2_per_cu_data
*per_cu
)
1078 struct needs_frame_baton baton
;
1079 struct dwarf_expr_context
*ctx
;
1081 struct cleanup
*old_chain
;
1083 baton
.needs_frame
= 0;
1084 baton
.per_cu
= per_cu
;
1086 ctx
= new_dwarf_expr_context ();
1087 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
1089 ctx
->gdbarch
= get_objfile_arch (dwarf2_per_cu_objfile (per_cu
));
1090 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
1091 ctx
->baton
= &baton
;
1092 ctx
->read_reg
= needs_frame_read_reg
;
1093 ctx
->read_mem
= needs_frame_read_mem
;
1094 ctx
->get_frame_base
= needs_frame_frame_base
;
1095 ctx
->get_frame_cfa
= needs_frame_frame_cfa
;
1096 ctx
->get_tls_address
= needs_frame_tls_address
;
1097 ctx
->dwarf_call
= needs_frame_dwarf_call
;
1099 dwarf_expr_eval (ctx
, data
, size
);
1101 in_reg
= ctx
->location
== DWARF_VALUE_REGISTER
;
1103 if (ctx
->num_pieces
> 0)
1107 /* If the location has several pieces, and any of them are in
1108 registers, then we will need a frame to fetch them from. */
1109 for (i
= 0; i
< ctx
->num_pieces
; i
++)
1110 if (ctx
->pieces
[i
].location
== DWARF_VALUE_REGISTER
)
1114 do_cleanups (old_chain
);
1116 return baton
.needs_frame
|| in_reg
;
1119 /* A helper function that throws an unimplemented error mentioning a
1120 given DWARF operator. */
1123 unimplemented (unsigned int op
)
1125 error (_("DWARF operator %s cannot be translated to an agent expression"),
1126 dwarf_stack_op_name (op
, 1));
1129 /* A helper function to convert a DWARF register to an arch register.
1130 ARCH is the architecture.
1131 DWARF_REG is the register.
1132 This will throw an exception if the DWARF register cannot be
1133 translated to an architecture register. */
1136 translate_register (struct gdbarch
*arch
, int dwarf_reg
)
1138 int reg
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_reg
);
1140 error (_("Unable to access DWARF register number %d"), dwarf_reg
);
1144 /* A helper function that emits an access to memory. ARCH is the
1145 target architecture. EXPR is the expression which we are building.
1146 NBITS is the number of bits we want to read. This emits the
1147 opcodes needed to read the memory and then extract the desired
1151 access_memory (struct gdbarch
*arch
, struct agent_expr
*expr
, ULONGEST nbits
)
1153 ULONGEST nbytes
= (nbits
+ 7) / 8;
1155 gdb_assert (nbits
> 0 && nbits
<= sizeof (LONGEST
));
1158 ax_trace_quick (expr
, nbytes
);
1161 ax_simple (expr
, aop_ref8
);
1162 else if (nbits
<= 16)
1163 ax_simple (expr
, aop_ref16
);
1164 else if (nbits
<= 32)
1165 ax_simple (expr
, aop_ref32
);
1167 ax_simple (expr
, aop_ref64
);
1169 /* If we read exactly the number of bytes we wanted, we're done. */
1170 if (8 * nbytes
== nbits
)
1173 if (gdbarch_bits_big_endian (arch
))
1175 /* On a bits-big-endian machine, we want the high-order
1177 ax_const_l (expr
, 8 * nbytes
- nbits
);
1178 ax_simple (expr
, aop_rsh_unsigned
);
1182 /* On a bits-little-endian box, we want the low-order NBITS. */
1183 ax_zero_ext (expr
, nbits
);
1187 /* Compile a DWARF location expression to an agent expression.
1189 EXPR is the agent expression we are building.
1190 LOC is the agent value we modify.
1191 ARCH is the architecture.
1192 ADDR_SIZE is the size of addresses, in bytes.
1193 OP_PTR is the start of the location expression.
1194 OP_END is one past the last byte of the location expression.
1196 This will throw an exception for various kinds of errors -- for
1197 example, if the expression cannot be compiled, or if the expression
1201 compile_dwarf_to_ax (struct agent_expr
*expr
, struct axs_value
*loc
,
1202 struct gdbarch
*arch
, unsigned int addr_size
,
1203 const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
1204 struct dwarf2_per_cu_data
*per_cu
)
1206 struct cleanup
*cleanups
;
1208 VEC(int) *dw_labels
= NULL
, *patches
= NULL
;
1209 const gdb_byte
* const base
= op_ptr
;
1210 const gdb_byte
*previous_piece
= op_ptr
;
1211 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
1212 ULONGEST bits_collected
= 0;
1213 unsigned int addr_size_bits
= 8 * addr_size
;
1214 int bits_big_endian
= gdbarch_bits_big_endian (arch
);
1216 offsets
= xmalloc ((op_end
- op_ptr
) * sizeof (int));
1217 cleanups
= make_cleanup (xfree
, offsets
);
1219 for (i
= 0; i
< op_end
- op_ptr
; ++i
)
1222 make_cleanup (VEC_cleanup (int), &dw_labels
);
1223 make_cleanup (VEC_cleanup (int), &patches
);
1225 /* By default we are making an address. */
1226 loc
->kind
= axs_lvalue_memory
;
1228 while (op_ptr
< op_end
)
1230 enum dwarf_location_atom op
= *op_ptr
;
1232 ULONGEST uoffset
, reg
;
1236 offsets
[op_ptr
- base
] = expr
->len
;
1239 /* Our basic approach to code generation is to map DWARF
1240 operations directly to AX operations. However, there are
1243 First, DWARF works on address-sized units, but AX always uses
1244 LONGEST. For most operations we simply ignore this
1245 difference; instead we generate sign extensions as needed
1246 before division and comparison operations. It would be nice
1247 to omit the sign extensions, but there is no way to determine
1248 the size of the target's LONGEST. (This code uses the size
1249 of the host LONGEST in some cases -- that is a bug but it is
1252 Second, some DWARF operations cannot be translated to AX.
1253 For these we simply fail. See
1254 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
1289 ax_const_l (expr
, op
- DW_OP_lit0
);
1293 result
= dwarf2_read_address (arch
, op_ptr
, op_end
, addr_size
);
1294 ax_const_l (expr
, result
);
1295 op_ptr
+= addr_size
;
1299 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 1, byte_order
));
1303 ax_const_l (expr
, extract_signed_integer (op_ptr
, 1, byte_order
));
1307 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 2, byte_order
));
1311 ax_const_l (expr
, extract_signed_integer (op_ptr
, 2, byte_order
));
1315 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 4, byte_order
));
1319 ax_const_l (expr
, extract_signed_integer (op_ptr
, 4, byte_order
));
1323 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 8, byte_order
));
1327 ax_const_l (expr
, extract_signed_integer (op_ptr
, 8, byte_order
));
1331 op_ptr
= read_uleb128 (op_ptr
, op_end
, &uoffset
);
1332 ax_const_l (expr
, uoffset
);
1335 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
1336 ax_const_l (expr
, offset
);
1371 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
1372 loc
->u
.reg
= translate_register (arch
, op
- DW_OP_reg0
);
1373 loc
->kind
= axs_lvalue_register
;
1377 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
1378 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
1379 loc
->u
.reg
= translate_register (arch
, reg
);
1380 loc
->kind
= axs_lvalue_register
;
1383 case DW_OP_implicit_value
:
1387 op_ptr
= read_uleb128 (op_ptr
, op_end
, &len
);
1388 if (op_ptr
+ len
> op_end
)
1389 error (_("DW_OP_implicit_value: too few bytes available."));
1390 if (len
> sizeof (ULONGEST
))
1391 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
1394 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, len
,
1397 dwarf_expr_require_composition (op_ptr
, op_end
,
1398 "DW_OP_implicit_value");
1400 loc
->kind
= axs_rvalue
;
1404 case DW_OP_stack_value
:
1405 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
1406 loc
->kind
= axs_rvalue
;
1441 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
1442 i
= translate_register (arch
, op
- DW_OP_breg0
);
1446 ax_const_l (expr
, offset
);
1447 ax_simple (expr
, aop_add
);
1452 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
1453 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
1454 i
= translate_register (arch
, reg
);
1458 ax_const_l (expr
, offset
);
1459 ax_simple (expr
, aop_add
);
1465 const gdb_byte
*datastart
;
1467 unsigned int before_stack_len
;
1469 struct symbol
*framefunc
;
1470 LONGEST base_offset
= 0;
1472 b
= block_for_pc (expr
->scope
);
1475 error (_("No block found for address"));
1477 framefunc
= block_linkage_function (b
);
1480 error (_("No function found for block"));
1482 dwarf_expr_frame_base_1 (framefunc
, expr
->scope
,
1483 &datastart
, &datalen
);
1485 op_ptr
= read_sleb128 (op_ptr
, op_end
, &offset
);
1486 compile_dwarf_to_ax (expr
, loc
, arch
, addr_size
, datastart
,
1487 datastart
+ datalen
, per_cu
);
1491 ax_const_l (expr
, offset
);
1492 ax_simple (expr
, aop_add
);
1495 loc
->kind
= axs_lvalue_memory
;
1500 ax_simple (expr
, aop_dup
);
1504 ax_simple (expr
, aop_pop
);
1513 ax_simple (expr
, aop_swap
);
1517 /* We can't directly support DW_OP_over, but GCC emits it as
1518 part of a sequence to implement signed modulus. As a
1519 hack, we recognize this sequence. Note that if GCC ever
1520 generates a branch to the middle of this sequence, then
1521 we will die somehow. */
1522 if (op_end
- op_ptr
>= 4
1523 && op_ptr
[0] == DW_OP_over
1524 && op_ptr
[1] == DW_OP_div
1525 && op_ptr
[2] == DW_OP_mul
1526 && op_ptr
[3] == DW_OP_minus
)
1528 /* Sign extend the operands. */
1529 ax_ext (expr
, addr_size_bits
);
1530 ax_simple (expr
, aop_swap
);
1531 ax_ext (expr
, addr_size_bits
);
1532 ax_simple (expr
, aop_swap
);
1533 ax_simple (expr
, aop_rem_signed
);
1545 case DW_OP_deref_size
:
1549 if (op
== DW_OP_deref_size
)
1557 ax_simple (expr
, aop_ref8
);
1560 ax_simple (expr
, aop_ref16
);
1563 ax_simple (expr
, aop_ref32
);
1566 ax_simple (expr
, aop_ref64
);
1569 error (_("Unsupported size %d in %s"),
1570 size
, dwarf_stack_op_name (op
, 1));
1576 /* Sign extend the operand. */
1577 ax_ext (expr
, addr_size_bits
);
1578 ax_simple (expr
, aop_dup
);
1579 ax_const_l (expr
, 0);
1580 ax_simple (expr
, aop_less_signed
);
1581 ax_simple (expr
, aop_log_not
);
1582 i
= ax_goto (expr
, aop_if_goto
);
1583 /* We have to emit 0 - X. */
1584 ax_const_l (expr
, 0);
1585 ax_simple (expr
, aop_swap
);
1586 ax_simple (expr
, aop_sub
);
1587 ax_label (expr
, i
, expr
->len
);
1591 /* No need to sign extend here. */
1592 ax_const_l (expr
, 0);
1593 ax_simple (expr
, aop_swap
);
1594 ax_simple (expr
, aop_sub
);
1598 /* Sign extend the operand. */
1599 ax_ext (expr
, addr_size_bits
);
1600 ax_simple (expr
, aop_bit_not
);
1603 case DW_OP_plus_uconst
:
1604 op_ptr
= read_uleb128 (op_ptr
, op_end
, ®
);
1605 /* It would be really weird to emit `DW_OP_plus_uconst 0',
1606 but we micro-optimize anyhow. */
1609 ax_const_l (expr
, reg
);
1610 ax_simple (expr
, aop_add
);
1615 ax_simple (expr
, aop_bit_and
);
1619 /* Sign extend the operands. */
1620 ax_ext (expr
, addr_size_bits
);
1621 ax_simple (expr
, aop_swap
);
1622 ax_ext (expr
, addr_size_bits
);
1623 ax_simple (expr
, aop_swap
);
1624 ax_simple (expr
, aop_div_signed
);
1628 ax_simple (expr
, aop_sub
);
1632 ax_simple (expr
, aop_rem_unsigned
);
1636 ax_simple (expr
, aop_mul
);
1640 ax_simple (expr
, aop_bit_or
);
1644 ax_simple (expr
, aop_add
);
1648 ax_simple (expr
, aop_lsh
);
1652 ax_simple (expr
, aop_rsh_unsigned
);
1656 ax_simple (expr
, aop_rsh_signed
);
1660 ax_simple (expr
, aop_bit_xor
);
1664 /* Sign extend the operands. */
1665 ax_ext (expr
, addr_size_bits
);
1666 ax_simple (expr
, aop_swap
);
1667 ax_ext (expr
, addr_size_bits
);
1668 /* Note no swap here: A <= B is !(B < A). */
1669 ax_simple (expr
, aop_less_signed
);
1670 ax_simple (expr
, aop_log_not
);
1674 /* Sign extend the operands. */
1675 ax_ext (expr
, addr_size_bits
);
1676 ax_simple (expr
, aop_swap
);
1677 ax_ext (expr
, addr_size_bits
);
1678 ax_simple (expr
, aop_swap
);
1679 /* A >= B is !(A < B). */
1680 ax_simple (expr
, aop_less_signed
);
1681 ax_simple (expr
, aop_log_not
);
1685 /* Sign extend the operands. */
1686 ax_ext (expr
, addr_size_bits
);
1687 ax_simple (expr
, aop_swap
);
1688 ax_ext (expr
, addr_size_bits
);
1689 /* No need for a second swap here. */
1690 ax_simple (expr
, aop_equal
);
1694 /* Sign extend the operands. */
1695 ax_ext (expr
, addr_size_bits
);
1696 ax_simple (expr
, aop_swap
);
1697 ax_ext (expr
, addr_size_bits
);
1698 ax_simple (expr
, aop_swap
);
1699 ax_simple (expr
, aop_less_signed
);
1703 /* Sign extend the operands. */
1704 ax_ext (expr
, addr_size_bits
);
1705 ax_simple (expr
, aop_swap
);
1706 ax_ext (expr
, addr_size_bits
);
1707 /* Note no swap here: A > B is B < A. */
1708 ax_simple (expr
, aop_less_signed
);
1712 /* Sign extend the operands. */
1713 ax_ext (expr
, addr_size_bits
);
1714 ax_simple (expr
, aop_swap
);
1715 ax_ext (expr
, addr_size_bits
);
1716 /* No need for a swap here. */
1717 ax_simple (expr
, aop_equal
);
1718 ax_simple (expr
, aop_log_not
);
1721 case DW_OP_call_frame_cfa
:
1725 case DW_OP_GNU_push_tls_address
:
1730 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
1732 i
= ax_goto (expr
, aop_goto
);
1733 VEC_safe_push (int, dw_labels
, op_ptr
+ offset
- base
);
1734 VEC_safe_push (int, patches
, i
);
1738 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
1740 /* Zero extend the operand. */
1741 ax_zero_ext (expr
, addr_size_bits
);
1742 i
= ax_goto (expr
, aop_if_goto
);
1743 VEC_safe_push (int, dw_labels
, op_ptr
+ offset
- base
);
1744 VEC_safe_push (int, patches
, i
);
1751 case DW_OP_bit_piece
:
1753 ULONGEST size
, offset
;
1755 if (op_ptr
- 1 == previous_piece
)
1756 error (_("Cannot translate empty pieces to agent expressions"));
1757 previous_piece
= op_ptr
- 1;
1759 op_ptr
= read_uleb128 (op_ptr
, op_end
, &size
);
1760 if (op
== DW_OP_piece
)
1766 op_ptr
= read_uleb128 (op_ptr
, op_end
, &offset
);
1768 if (bits_collected
+ size
> 8 * sizeof (LONGEST
))
1769 error (_("Expression pieces exceed word size"));
1771 /* Access the bits. */
1774 case axs_lvalue_register
:
1775 ax_reg (expr
, loc
->u
.reg
);
1778 case axs_lvalue_memory
:
1779 /* Offset the pointer, if needed. */
1782 ax_const_l (expr
, offset
/ 8);
1783 ax_simple (expr
, aop_add
);
1786 access_memory (arch
, expr
, size
);
1790 /* For a bits-big-endian target, shift up what we already
1791 have. For a bits-little-endian target, shift up the
1792 new data. Note that there is a potential bug here if
1793 the DWARF expression leaves multiple values on the
1795 if (bits_collected
> 0)
1797 if (bits_big_endian
)
1799 ax_simple (expr
, aop_swap
);
1800 ax_const_l (expr
, size
);
1801 ax_simple (expr
, aop_lsh
);
1802 /* We don't need a second swap here, because
1803 aop_bit_or is symmetric. */
1807 ax_const_l (expr
, size
);
1808 ax_simple (expr
, aop_lsh
);
1810 ax_simple (expr
, aop_bit_or
);
1813 bits_collected
+= size
;
1814 loc
->kind
= axs_rvalue
;
1818 case DW_OP_GNU_uninit
:
1824 struct dwarf2_locexpr_baton block
;
1825 int size
= (op
== DW_OP_call2
? 2 : 4);
1827 uoffset
= extract_unsigned_integer (op_ptr
, size
, byte_order
);
1830 block
= dwarf2_fetch_die_location_block (uoffset
, per_cu
);
1832 /* DW_OP_call_ref is currently not supported. */
1833 gdb_assert (block
.per_cu
== per_cu
);
1835 compile_dwarf_to_ax (expr
, loc
, arch
, addr_size
,
1836 block
.data
, block
.data
+ block
.size
,
1841 case DW_OP_call_ref
:
1845 error (_("Unhandled dwarf expression opcode 0x%x"), op
);
1849 /* Patch all the branches we emitted. */
1850 for (i
= 0; i
< VEC_length (int, patches
); ++i
)
1852 int targ
= offsets
[VEC_index (int, dw_labels
, i
)];
1854 internal_error (__FILE__
, __LINE__
, _("invalid label"));
1855 ax_label (expr
, VEC_index (int, patches
, i
), targ
);
1858 do_cleanups (cleanups
);
1862 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1863 evaluator to calculate the location. */
1864 static struct value
*
1865 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
1867 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1870 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
1871 dlbaton
->size
, dlbaton
->per_cu
);
1876 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1878 locexpr_read_needs_frame (struct symbol
*symbol
)
1880 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
1882 return dwarf2_loc_desc_needs_frame (dlbaton
->data
, dlbaton
->size
,
1886 /* Return true if DATA points to the end of a piece. END is one past
1887 the last byte in the expression. */
1890 piece_end_p (const gdb_byte
*data
, const gdb_byte
*end
)
1892 return data
== end
|| data
[0] == DW_OP_piece
|| data
[0] == DW_OP_bit_piece
;
1895 /* Nicely describe a single piece of a location, returning an updated
1896 position in the bytecode sequence. This function cannot recognize
1897 all locations; if a location is not recognized, it simply returns
1900 static const gdb_byte
*
1901 locexpr_describe_location_piece (struct symbol
*symbol
, struct ui_file
*stream
,
1902 CORE_ADDR addr
, struct objfile
*objfile
,
1903 const gdb_byte
*data
, const gdb_byte
*end
,
1904 unsigned int addr_size
)
1906 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
1909 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
1911 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, data
[0] - DW_OP_reg0
);
1912 fprintf_filtered (stream
, _("a variable in $%s"),
1913 gdbarch_register_name (gdbarch
, regno
));
1916 else if (data
[0] == DW_OP_regx
)
1920 data
= read_uleb128 (data
+ 1, end
, ®
);
1921 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
);
1922 fprintf_filtered (stream
, _("a variable in $%s"),
1923 gdbarch_register_name (gdbarch
, regno
));
1925 else if (data
[0] == DW_OP_fbreg
)
1928 struct symbol
*framefunc
;
1930 LONGEST frame_offset
;
1931 const gdb_byte
*base_data
, *new_data
;
1933 LONGEST base_offset
= 0;
1935 new_data
= read_sleb128 (data
+ 1, end
, &frame_offset
);
1936 if (!piece_end_p (new_data
, end
))
1940 b
= block_for_pc (addr
);
1943 error (_("No block found for address for symbol \"%s\"."),
1944 SYMBOL_PRINT_NAME (symbol
));
1946 framefunc
= block_linkage_function (b
);
1949 error (_("No function found for block for symbol \"%s\"."),
1950 SYMBOL_PRINT_NAME (symbol
));
1952 dwarf_expr_frame_base_1 (framefunc
, addr
, &base_data
, &base_size
);
1954 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
1956 const gdb_byte
*buf_end
;
1958 frame_reg
= base_data
[0] - DW_OP_breg0
;
1959 buf_end
= read_sleb128 (base_data
+ 1,
1960 base_data
+ base_size
, &base_offset
);
1961 if (buf_end
!= base_data
+ base_size
)
1962 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1963 frame_reg
, SYMBOL_PRINT_NAME (symbol
));
1965 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
1967 /* The frame base is just the register, with no offset. */
1968 frame_reg
= base_data
[0] - DW_OP_reg0
;
1973 /* We don't know what to do with the frame base expression,
1974 so we can't trace this variable; give up. */
1975 error (_("Cannot describe location of symbol \"%s\"; "
1976 "DWARF 2 encoding not handled, "
1977 "first opcode in base data is 0x%x."),
1978 SYMBOL_PRINT_NAME (symbol
), base_data
[0]);
1981 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, frame_reg
);
1983 fprintf_filtered (stream
, _("a variable at frame base reg $%s offset %s+%s"),
1984 gdbarch_register_name (gdbarch
, regno
),
1985 plongest (base_offset
), plongest (frame_offset
));
1987 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
1988 && piece_end_p (data
, end
))
1992 regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, data
[0] - DW_OP_breg0
);
1994 data
= read_sleb128 (data
+ 1, end
, &offset
);
1996 fprintf_filtered (stream
,
1997 _("a variable at offset %s from base reg $%s"),
1999 gdbarch_register_name (gdbarch
, regno
));
2002 /* The location expression for a TLS variable looks like this (on a
2005 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
2006 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
2008 0x3 is the encoding for DW_OP_addr, which has an operand as long
2009 as the size of an address on the target machine (here is 8
2010 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
2011 The operand represents the offset at which the variable is within
2012 the thread local storage. */
2014 else if (data
+ 1 + addr_size
< end
2015 && data
[0] == DW_OP_addr
2016 && data
[1 + addr_size
] == DW_OP_GNU_push_tls_address
2017 && piece_end_p (data
+ 2 + addr_size
, end
))
2019 CORE_ADDR offset
= dwarf2_read_address (gdbarch
,
2024 fprintf_filtered (stream
,
2025 _("a thread-local variable at offset %s "
2026 "in the thread-local storage for `%s'"),
2027 paddress (gdbarch
, offset
), objfile
->name
);
2029 data
+= 1 + addr_size
+ 1;
2031 else if (data
[0] >= DW_OP_lit0
2032 && data
[0] <= DW_OP_lit31
2034 && data
[1] == DW_OP_stack_value
)
2036 fprintf_filtered (stream
, _("the constant %d"), data
[0] - DW_OP_lit0
);
2043 /* Disassemble an expression, stopping at the end of a piece or at the
2044 end of the expression. Returns a pointer to the next unread byte
2045 in the input expression. If ALL is nonzero, then this function
2046 will keep going until it reaches the end of the expression. */
2048 static const gdb_byte
*
2049 disassemble_dwarf_expression (struct ui_file
*stream
,
2050 struct gdbarch
*arch
, unsigned int addr_size
,
2052 const gdb_byte
*data
, const gdb_byte
*end
,
2055 const gdb_byte
*start
= data
;
2057 fprintf_filtered (stream
, _("a complex DWARF expression:\n"));
2061 || (data
[0] != DW_OP_piece
&& data
[0] != DW_OP_bit_piece
)))
2063 enum dwarf_location_atom op
= *data
++;
2069 name
= dwarf_stack_op_name (op
, 0);
2072 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
2073 op
, (long) (data
- start
));
2074 fprintf_filtered (stream
, " % 4ld: %s", (long) (data
- start
), name
);
2079 addr
= dwarf2_read_address (arch
, data
, end
, addr_size
);
2081 fprintf_filtered (stream
, " %s", paddress (arch
, addr
));
2085 ul
= extract_unsigned_integer (data
, 1, gdbarch_byte_order (arch
));
2087 fprintf_filtered (stream
, " %s", pulongest (ul
));
2090 l
= extract_signed_integer (data
, 1, gdbarch_byte_order (arch
));
2092 fprintf_filtered (stream
, " %s", plongest (l
));
2095 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
2097 fprintf_filtered (stream
, " %s", pulongest (ul
));
2100 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
2102 fprintf_filtered (stream
, " %s", plongest (l
));
2105 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
2107 fprintf_filtered (stream
, " %s", pulongest (ul
));
2110 l
= extract_signed_integer (data
, 4, gdbarch_byte_order (arch
));
2112 fprintf_filtered (stream
, " %s", plongest (l
));
2115 ul
= extract_unsigned_integer (data
, 8, gdbarch_byte_order (arch
));
2117 fprintf_filtered (stream
, " %s", pulongest (ul
));
2120 l
= extract_signed_integer (data
, 8, gdbarch_byte_order (arch
));
2122 fprintf_filtered (stream
, " %s", plongest (l
));
2125 data
= read_uleb128 (data
, end
, &ul
);
2126 fprintf_filtered (stream
, " %s", pulongest (ul
));
2129 data
= read_sleb128 (data
, end
, &l
);
2130 fprintf_filtered (stream
, " %s", plongest (l
));
2165 fprintf_filtered (stream
, " [$%s]",
2166 gdbarch_register_name (arch
, op
- DW_OP_reg0
));
2170 data
= read_uleb128 (data
, end
, &ul
);
2171 fprintf_filtered (stream
, " %s [$%s]", pulongest (ul
),
2172 gdbarch_register_name (arch
, (int) ul
));
2175 case DW_OP_implicit_value
:
2176 data
= read_uleb128 (data
, end
, &ul
);
2178 fprintf_filtered (stream
, " %s", pulongest (ul
));
2213 data
= read_sleb128 (data
, end
, &ul
);
2214 fprintf_filtered (stream
, " %s [$%s]", pulongest (ul
),
2215 gdbarch_register_name (arch
, op
- DW_OP_breg0
));
2222 data
= read_uleb128 (data
, end
, &ul
);
2223 data
= read_sleb128 (data
, end
, &offset
);
2224 fprintf_filtered (stream
, " register %s [$%s] offset %s",
2226 gdbarch_register_name (arch
, (int) ul
),
2227 pulongest (offset
));
2232 data
= read_sleb128 (data
, end
, &ul
);
2233 fprintf_filtered (stream
, " %s", pulongest (ul
));
2236 case DW_OP_xderef_size
:
2237 case DW_OP_deref_size
:
2239 fprintf_filtered (stream
, " %d", *data
);
2243 case DW_OP_plus_uconst
:
2244 data
= read_uleb128 (data
, end
, &ul
);
2245 fprintf_filtered (stream
, " %s", pulongest (ul
));
2249 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
2251 fprintf_filtered (stream
, " to %ld",
2252 (long) (data
+ l
- start
));
2256 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
2258 fprintf_filtered (stream
, " %ld",
2259 (long) (data
+ l
- start
));
2263 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
2265 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 2));
2269 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
2271 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 4));
2274 case DW_OP_call_ref
:
2275 ul
= extract_unsigned_integer (data
, offset_size
,
2276 gdbarch_byte_order (arch
));
2277 data
+= offset_size
;
2278 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, offset_size
));
2282 data
= read_uleb128 (data
, end
, &ul
);
2283 fprintf_filtered (stream
, " %s (bytes)", pulongest (ul
));
2286 case DW_OP_bit_piece
:
2290 data
= read_uleb128 (data
, end
, &ul
);
2291 data
= read_uleb128 (data
, end
, &offset
);
2292 fprintf_filtered (stream
, " size %s offset %s (bits)",
2293 pulongest (ul
), pulongest (offset
));
2298 fprintf_filtered (stream
, "\n");
2304 /* Describe a single location, which may in turn consist of multiple
2308 locexpr_describe_location_1 (struct symbol
*symbol
, CORE_ADDR addr
,
2309 struct ui_file
*stream
,
2310 const gdb_byte
*data
, int size
,
2311 struct objfile
*objfile
, unsigned int addr_size
,
2314 const gdb_byte
*end
= data
+ size
;
2315 int first_piece
= 1, bad
= 0;
2319 const gdb_byte
*here
= data
;
2320 int disassemble
= 1;
2325 fprintf_filtered (stream
, _(", and "));
2327 if (!dwarf2_always_disassemble
)
2329 data
= locexpr_describe_location_piece (symbol
, stream
, addr
, objfile
,
2330 data
, end
, addr_size
);
2331 /* If we printed anything, or if we have an empty piece,
2332 then don't disassemble. */
2334 || data
[0] == DW_OP_piece
2335 || data
[0] == DW_OP_bit_piece
)
2339 data
= disassemble_dwarf_expression (stream
, get_objfile_arch (objfile
),
2340 addr_size
, offset_size
, data
, end
,
2341 dwarf2_always_disassemble
);
2345 int empty
= data
== here
;
2348 fprintf_filtered (stream
, " ");
2349 if (data
[0] == DW_OP_piece
)
2353 data
= read_uleb128 (data
+ 1, end
, &bytes
);
2356 fprintf_filtered (stream
, _("an empty %s-byte piece"),
2359 fprintf_filtered (stream
, _(" [%s-byte piece]"),
2362 else if (data
[0] == DW_OP_bit_piece
)
2364 ULONGEST bits
, offset
;
2366 data
= read_uleb128 (data
+ 1, end
, &bits
);
2367 data
= read_uleb128 (data
, end
, &offset
);
2370 fprintf_filtered (stream
,
2371 _("an empty %s-bit piece"),
2374 fprintf_filtered (stream
,
2375 _(" [%s-bit piece, offset %s bits]"),
2376 pulongest (bits
), pulongest (offset
));
2386 if (bad
|| data
> end
)
2387 error (_("Corrupted DWARF2 expression for \"%s\"."),
2388 SYMBOL_PRINT_NAME (symbol
));
2391 /* Print a natural-language description of SYMBOL to STREAM. This
2392 version is for a symbol with a single location. */
2395 locexpr_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
2396 struct ui_file
*stream
)
2398 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
2399 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
2400 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
2401 int offset_size
= dwarf2_per_cu_offset_size (dlbaton
->per_cu
);
2403 locexpr_describe_location_1 (symbol
, addr
, stream
, dlbaton
->data
, dlbaton
->size
,
2404 objfile
, addr_size
, offset_size
);
2407 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2408 any necessary bytecode in AX. */
2411 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
2412 struct agent_expr
*ax
, struct axs_value
*value
)
2414 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
2415 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
2417 compile_dwarf_to_ax (ax
, value
, gdbarch
, addr_size
,
2418 dlbaton
->data
, dlbaton
->data
+ dlbaton
->size
,
2422 /* The set of location functions used with the DWARF-2 expression
2424 const struct symbol_computed_ops dwarf2_locexpr_funcs
= {
2425 locexpr_read_variable
,
2426 locexpr_read_needs_frame
,
2427 locexpr_describe_location
,
2428 locexpr_tracepoint_var_ref
2432 /* Wrapper functions for location lists. These generally find
2433 the appropriate location expression and call something above. */
2435 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2436 evaluator to calculate the location. */
2437 static struct value
*
2438 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
2440 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
2442 const gdb_byte
*data
;
2445 data
= find_location_expression (dlbaton
, &size
,
2446 frame
? get_frame_address_in_block (frame
)
2450 val
= allocate_value (SYMBOL_TYPE (symbol
));
2451 VALUE_LVAL (val
) = not_lval
;
2452 set_value_optimized_out (val
, 1);
2455 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, data
, size
,
2461 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
2463 loclist_read_needs_frame (struct symbol
*symbol
)
2465 /* If there's a location list, then assume we need to have a frame
2466 to choose the appropriate location expression. With tracking of
2467 global variables this is not necessarily true, but such tracking
2468 is disabled in GCC at the moment until we figure out how to
2474 /* Print a natural-language description of SYMBOL to STREAM. This
2475 version applies when there is a list of different locations, each
2476 with a specified address range. */
2479 loclist_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
2480 struct ui_file
*stream
)
2482 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
2483 CORE_ADDR low
, high
;
2484 const gdb_byte
*loc_ptr
, *buf_end
;
2485 int length
, first
= 1;
2486 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
2487 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
2488 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2489 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
2490 int offset_size
= dwarf2_per_cu_offset_size (dlbaton
->per_cu
);
2491 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
2492 /* Adjust base_address for relocatable objects. */
2493 CORE_ADDR base_offset
= ANOFFSET (objfile
->section_offsets
,
2494 SECT_OFF_TEXT (objfile
));
2495 CORE_ADDR base_address
= dlbaton
->base_address
+ base_offset
;
2497 loc_ptr
= dlbaton
->data
;
2498 buf_end
= dlbaton
->data
+ dlbaton
->size
;
2500 fprintf_filtered (stream
, _("multi-location:\n"));
2502 /* Iterate through locations until we run out. */
2505 if (buf_end
- loc_ptr
< 2 * addr_size
)
2506 error (_("Corrupted DWARF expression for symbol \"%s\"."),
2507 SYMBOL_PRINT_NAME (symbol
));
2509 low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
2510 loc_ptr
+= addr_size
;
2512 /* A base-address-selection entry. */
2513 if (low
== base_mask
)
2515 base_address
= dwarf2_read_address (gdbarch
,
2516 loc_ptr
, buf_end
, addr_size
);
2517 fprintf_filtered (stream
, _(" Base address %s"),
2518 paddress (gdbarch
, base_address
));
2519 loc_ptr
+= addr_size
;
2523 high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
2524 loc_ptr
+= addr_size
;
2526 /* An end-of-list entry. */
2527 if (low
== 0 && high
== 0)
2530 /* Otherwise, a location expression entry. */
2531 low
+= base_address
;
2532 high
+= base_address
;
2534 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
2537 /* (It would improve readability to print only the minimum
2538 necessary digits of the second number of the range.) */
2539 fprintf_filtered (stream
, _(" Range %s-%s: "),
2540 paddress (gdbarch
, low
), paddress (gdbarch
, high
));
2542 /* Now describe this particular location. */
2543 locexpr_describe_location_1 (symbol
, low
, stream
, loc_ptr
, length
,
2544 objfile
, addr_size
, offset_size
);
2546 fprintf_filtered (stream
, "\n");
2552 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2553 any necessary bytecode in AX. */
2555 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
2556 struct agent_expr
*ax
, struct axs_value
*value
)
2558 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
2559 const gdb_byte
*data
;
2561 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
2563 data
= find_location_expression (dlbaton
, &size
, ax
->scope
);
2565 compile_dwarf_to_ax (ax
, value
, gdbarch
, addr_size
, data
, data
+ size
,
2569 /* The set of location functions used with the DWARF-2 expression
2570 evaluator and location lists. */
2571 const struct symbol_computed_ops dwarf2_loclist_funcs
= {
2572 loclist_read_variable
,
2573 loclist_read_needs_frame
,
2574 loclist_describe_location
,
2575 loclist_tracepoint_var_ref