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 type_len = 8 * TYPE_LENGTH (value_type (v));
495 for (i = 0; i < c->n_pieces && offset < type_len; i++)
497 struct dwarf_expr_piece *p = &c->pieces[i];
498 size_t this_size, this_size_bits;
499 long dest_offset_bits, source_offset_bits, source_offset;
500 const gdb_byte *intermediate_buffer;
502 /* Compute size, source, and destination offsets for copying, in
504 this_size_bits = p->size;
505 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
507 bits_to_skip -= this_size_bits;
510 if (this_size_bits > type_len - offset)
511 this_size_bits = type_len - offset;
512 if (bits_to_skip > 0)
514 dest_offset_bits = 0;
515 source_offset_bits = bits_to_skip;
516 this_size_bits -= bits_to_skip;
521 dest_offset_bits = offset;
522 source_offset_bits = 0;
525 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
526 source_offset = source_offset_bits / 8;
527 if (buffer_size < this_size)
529 buffer_size = this_size;
530 buffer = xrealloc (buffer, buffer_size);
532 intermediate_buffer = buffer;
534 /* Copy from the source to DEST_BUFFER. */
537 case DWARF_VALUE_REGISTER:
539 struct gdbarch *arch = get_frame_arch (frame);
540 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
542 int reg_offset = source_offset;
544 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
545 && this_size < register_size (arch, gdb_regnum))
547 /* Big-endian, and we want less than full size. */
548 reg_offset = register_size (arch, gdb_regnum) - this_size;
549 /* We want the lower-order THIS_SIZE_BITS of the bytes
550 we extract from the register. */
551 source_offset_bits += 8 * this_size - this_size_bits;
554 if (gdb_regnum != -1)
556 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
561 error (_("Unable to access DWARF register number %s"),
562 paddress (arch, p->v.expr.value));
567 case DWARF_VALUE_MEMORY:
568 if (p->v.expr.in_stack_memory)
569 read_stack (p->v.expr.value + source_offset, buffer, this_size);
571 read_memory (p->v.expr.value + source_offset, buffer, this_size);
574 case DWARF_VALUE_STACK:
576 struct gdbarch *gdbarch = get_type_arch (value_type (v));
577 size_t n = this_size;
579 if (n > c->addr_size - source_offset)
580 n = (c->addr_size >= source_offset
581 ? c->addr_size - source_offset
587 else if (source_offset == 0)
588 store_unsigned_integer (buffer, n,
589 gdbarch_byte_order (gdbarch),
593 gdb_byte bytes[sizeof (ULONGEST)];
595 store_unsigned_integer (bytes, n + source_offset,
596 gdbarch_byte_order (gdbarch),
598 memcpy (buffer, bytes + source_offset, n);
603 case DWARF_VALUE_LITERAL:
605 size_t n = this_size;
607 if (n > p->v.literal.length - source_offset)
608 n = (p->v.literal.length >= source_offset
609 ? p->v.literal.length - source_offset
612 intermediate_buffer = p->v.literal.data + source_offset;
616 case DWARF_VALUE_OPTIMIZED_OUT:
617 /* We just leave the bits empty for now. This is not ideal
618 but gdb currently does not have a nice way to represent
619 optimized-out pieces. */
620 warning (_("bits %ld-%ld in computed object were optimized out; "
621 "replacing with zeroes"),
623 offset + (long) this_size_bits);
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 type_len = 8 * TYPE_LENGTH (value_type (to));
668 for (i = 0; i < c->n_pieces && offset < type_len; i++)
670 struct dwarf_expr_piece *p = &c->pieces[i];
671 size_t this_size_bits, this_size;
672 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
674 const gdb_byte *source_buffer;
676 this_size_bits = p->size;
677 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
679 bits_to_skip -= this_size_bits;
682 if (this_size_bits > type_len - offset)
683 this_size_bits = type_len - offset;
684 if (bits_to_skip > 0)
686 dest_offset_bits = bits_to_skip;
687 source_offset_bits = 0;
688 this_size_bits -= bits_to_skip;
693 dest_offset_bits = 0;
694 source_offset_bits = offset;
697 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
698 source_offset = source_offset_bits / 8;
699 dest_offset = dest_offset_bits / 8;
700 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
702 source_buffer = contents + source_offset;
707 if (buffer_size < this_size)
709 buffer_size = this_size;
710 buffer = xrealloc (buffer, buffer_size);
712 source_buffer = buffer;
718 case DWARF_VALUE_REGISTER:
720 struct gdbarch *arch = get_frame_arch (frame);
721 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
722 int reg_offset = dest_offset;
724 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
725 && this_size <= register_size (arch, gdb_regnum))
726 /* Big-endian, and we want less than full size. */
727 reg_offset = register_size (arch, gdb_regnum) - this_size;
729 if (gdb_regnum != -1)
733 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
735 copy_bitwise (buffer, dest_offset_bits,
736 contents, source_offset_bits,
741 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
742 this_size, source_buffer);
746 error (_("Unable to write to DWARF register number %s"),
747 paddress (arch, p->v.expr.value));
751 case DWARF_VALUE_MEMORY:
754 /* Only the first and last bytes can possibly have any
756 read_memory (p->v.expr.value + dest_offset, buffer, 1);
757 read_memory (p->v.expr.value + dest_offset + this_size - 1,
758 buffer + this_size - 1, 1);
759 copy_bitwise (buffer, dest_offset_bits,
760 contents, source_offset_bits,
765 write_memory (p->v.expr.value + dest_offset,
766 source_buffer, this_size);
769 set_value_optimized_out (to, 1);
772 offset += this_size_bits;
776 do_cleanups (cleanup);
780 copy_pieced_value_closure (struct value *v)
782 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
789 free_pieced_value_closure (struct value *v)
791 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
801 /* Functions for accessing a variable described by DW_OP_piece. */
802 static struct lval_funcs pieced_value_funcs = {
805 copy_pieced_value_closure,
806 free_pieced_value_closure
809 /* Evaluate a location description, starting at DATA and with length
810 SIZE, to find the current location of variable of TYPE in the context
813 static struct value *
814 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
815 const gdb_byte *data, unsigned short size,
816 struct dwarf2_per_cu_data *per_cu)
818 struct value *retval;
819 struct dwarf_expr_baton baton;
820 struct dwarf_expr_context *ctx;
821 struct cleanup *old_chain;
825 retval = allocate_value (type);
826 VALUE_LVAL (retval) = not_lval;
827 set_value_optimized_out (retval, 1);
832 baton.per_cu = per_cu;
834 ctx = new_dwarf_expr_context ();
835 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
837 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
838 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
840 ctx->read_reg = dwarf_expr_read_reg;
841 ctx->read_mem = dwarf_expr_read_mem;
842 ctx->get_frame_base = dwarf_expr_frame_base;
843 ctx->get_frame_cfa = dwarf_expr_frame_cfa;
844 ctx->get_tls_address = dwarf_expr_tls_address;
845 ctx->dwarf_call = dwarf_expr_dwarf_call;
847 dwarf_expr_eval (ctx, data, size);
848 if (ctx->num_pieces > 0)
850 struct piece_closure *c;
851 struct frame_id frame_id = get_frame_id (frame);
853 c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
855 retval = allocate_computed_value (type, &pieced_value_funcs, c);
856 VALUE_FRAME_ID (retval) = frame_id;
860 switch (ctx->location)
862 case DWARF_VALUE_REGISTER:
864 struct gdbarch *arch = get_frame_arch (frame);
865 CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
866 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
868 if (gdb_regnum != -1)
869 retval = value_from_register (type, gdb_regnum, frame);
871 error (_("Unable to access DWARF register number %s"),
872 paddress (arch, dwarf_regnum));
876 case DWARF_VALUE_MEMORY:
878 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
879 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
881 retval = allocate_value (type);
882 VALUE_LVAL (retval) = lval_memory;
883 set_value_lazy (retval, 1);
885 set_value_stack (retval, 1);
886 set_value_address (retval, address);
890 case DWARF_VALUE_STACK:
892 ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
894 size_t n = ctx->addr_size;
896 retval = allocate_value (type);
897 contents = value_contents_raw (retval);
898 if (n > TYPE_LENGTH (type))
899 n = TYPE_LENGTH (type);
900 store_unsigned_integer (contents, n,
901 gdbarch_byte_order (ctx->gdbarch),
906 case DWARF_VALUE_LITERAL:
911 retval = allocate_value (type);
912 contents = value_contents_raw (retval);
913 if (n > TYPE_LENGTH (type))
914 n = TYPE_LENGTH (type);
915 memcpy (contents, ctx->data, n);
919 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
920 it can only be encountered when making a piece. */
921 case DWARF_VALUE_OPTIMIZED_OUT:
923 internal_error (__FILE__, __LINE__, _("invalid location type"));
927 set_value_initialized (retval, ctx->initialized);
929 do_cleanups (old_chain);
934 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
936 struct needs_frame_baton
939 struct dwarf2_per_cu_data *per_cu;
942 /* Reads from registers do require a frame. */
944 needs_frame_read_reg (void *baton, int regnum)
946 struct needs_frame_baton *nf_baton = baton;
948 nf_baton->needs_frame = 1;
952 /* Reads from memory do not require a frame. */
954 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
956 memset (buf, 0, len);
959 /* Frame-relative accesses do require a frame. */
961 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
963 static gdb_byte lit0 = DW_OP_lit0;
964 struct needs_frame_baton *nf_baton = baton;
969 nf_baton->needs_frame = 1;
972 /* CFA accesses require a frame. */
975 needs_frame_frame_cfa (void *baton)
977 struct needs_frame_baton *nf_baton = baton;
979 nf_baton->needs_frame = 1;
983 /* Thread-local accesses do require a frame. */
985 needs_frame_tls_address (void *baton, CORE_ADDR offset)
987 struct needs_frame_baton *nf_baton = baton;
989 nf_baton->needs_frame = 1;
993 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
996 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
998 struct needs_frame_baton *nf_baton = ctx->baton;
1000 return per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu);
1003 /* Return non-zero iff the location expression at DATA (length SIZE)
1004 requires a frame to evaluate. */
1007 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
1008 struct dwarf2_per_cu_data *per_cu)
1010 struct needs_frame_baton baton;
1011 struct dwarf_expr_context *ctx;
1013 struct cleanup *old_chain;
1015 baton.needs_frame = 0;
1016 baton.per_cu = per_cu;
1018 ctx = new_dwarf_expr_context ();
1019 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1021 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
1022 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1023 ctx->baton = &baton;
1024 ctx->read_reg = needs_frame_read_reg;
1025 ctx->read_mem = needs_frame_read_mem;
1026 ctx->get_frame_base = needs_frame_frame_base;
1027 ctx->get_frame_cfa = needs_frame_frame_cfa;
1028 ctx->get_tls_address = needs_frame_tls_address;
1029 ctx->dwarf_call = needs_frame_dwarf_call;
1031 dwarf_expr_eval (ctx, data, size);
1033 in_reg = ctx->location == DWARF_VALUE_REGISTER;
1035 if (ctx->num_pieces > 0)
1039 /* If the location has several pieces, and any of them are in
1040 registers, then we will need a frame to fetch them from. */
1041 for (i = 0; i < ctx->num_pieces; i++)
1042 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
1046 do_cleanups (old_chain);
1048 return baton.needs_frame || in_reg;
1051 /* A helper function that throws an unimplemented error mentioning a
1052 given DWARF operator. */
1055 unimplemented (unsigned int op)
1057 error (_("DWARF operator %s cannot be translated to an agent expression"),
1058 dwarf_stack_op_name (op, 1));
1061 /* A helper function to convert a DWARF register to an arch register.
1062 ARCH is the architecture.
1063 DWARF_REG is the register.
1064 This will throw an exception if the DWARF register cannot be
1065 translated to an architecture register. */
1068 translate_register (struct gdbarch *arch, int dwarf_reg)
1070 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
1072 error (_("Unable to access DWARF register number %d"), dwarf_reg);
1076 /* A helper function that emits an access to memory. ARCH is the
1077 target architecture. EXPR is the expression which we are building.
1078 NBITS is the number of bits we want to read. This emits the
1079 opcodes needed to read the memory and then extract the desired
1083 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
1085 ULONGEST nbytes = (nbits + 7) / 8;
1087 gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
1090 ax_trace_quick (expr, nbytes);
1093 ax_simple (expr, aop_ref8);
1094 else if (nbits <= 16)
1095 ax_simple (expr, aop_ref16);
1096 else if (nbits <= 32)
1097 ax_simple (expr, aop_ref32);
1099 ax_simple (expr, aop_ref64);
1101 /* If we read exactly the number of bytes we wanted, we're done. */
1102 if (8 * nbytes == nbits)
1105 if (gdbarch_bits_big_endian (arch))
1107 /* On a bits-big-endian machine, we want the high-order
1109 ax_const_l (expr, 8 * nbytes - nbits);
1110 ax_simple (expr, aop_rsh_unsigned);
1114 /* On a bits-little-endian box, we want the low-order NBITS. */
1115 ax_zero_ext (expr, nbits);
1119 /* Compile a DWARF location expression to an agent expression.
1121 EXPR is the agent expression we are building.
1122 LOC is the agent value we modify.
1123 ARCH is the architecture.
1124 ADDR_SIZE is the size of addresses, in bytes.
1125 OP_PTR is the start of the location expression.
1126 OP_END is one past the last byte of the location expression.
1128 This will throw an exception for various kinds of errors -- for
1129 example, if the expression cannot be compiled, or if the expression
1133 compile_dwarf_to_ax (struct agent_expr *expr, struct axs_value *loc,
1134 struct gdbarch *arch, unsigned int addr_size,
1135 const gdb_byte *op_ptr, const gdb_byte *op_end,
1136 struct dwarf2_per_cu_data *per_cu)
1138 struct cleanup *cleanups;
1140 VEC(int) *dw_labels = NULL, *patches = NULL;
1141 const gdb_byte * const base = op_ptr;
1142 const gdb_byte *previous_piece = op_ptr;
1143 enum bfd_endian byte_order = gdbarch_byte_order (arch);
1144 ULONGEST bits_collected = 0;
1145 unsigned int addr_size_bits = 8 * addr_size;
1146 int bits_big_endian = gdbarch_bits_big_endian (arch);
1148 offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
1149 cleanups = make_cleanup (xfree, offsets);
1151 for (i = 0; i < op_end - op_ptr; ++i)
1154 make_cleanup (VEC_cleanup (int), &dw_labels);
1155 make_cleanup (VEC_cleanup (int), &patches);
1157 /* By default we are making an address. */
1158 loc->kind = axs_lvalue_memory;
1160 while (op_ptr < op_end)
1162 enum dwarf_location_atom op = *op_ptr;
1164 ULONGEST uoffset, reg;
1168 offsets[op_ptr - base] = expr->len;
1171 /* Our basic approach to code generation is to map DWARF
1172 operations directly to AX operations. However, there are
1175 First, DWARF works on address-sized units, but AX always uses
1176 LONGEST. For most operations we simply ignore this
1177 difference; instead we generate sign extensions as needed
1178 before division and comparison operations. It would be nice
1179 to omit the sign extensions, but there is no way to determine
1180 the size of the target's LONGEST. (This code uses the size
1181 of the host LONGEST in some cases -- that is a bug but it is
1184 Second, some DWARF operations cannot be translated to AX.
1185 For these we simply fail. See
1186 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
1221 ax_const_l (expr, op - DW_OP_lit0);
1225 result = dwarf2_read_address (arch, op_ptr, op_end, addr_size);
1226 ax_const_l (expr, result);
1227 op_ptr += addr_size;
1231 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
1235 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
1239 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
1243 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
1247 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
1251 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
1255 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
1259 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
1263 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
1264 ax_const_l (expr, uoffset);
1267 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1268 ax_const_l (expr, offset);
1303 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1304 loc->u.reg = translate_register (arch, op - DW_OP_reg0);
1305 loc->kind = axs_lvalue_register;
1309 op_ptr = read_uleb128 (op_ptr, op_end, ®);
1310 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1311 loc->u.reg = translate_register (arch, reg);
1312 loc->kind = axs_lvalue_register;
1315 case DW_OP_implicit_value:
1319 op_ptr = read_uleb128 (op_ptr, op_end, &len);
1320 if (op_ptr + len > op_end)
1321 error (_("DW_OP_implicit_value: too few bytes available."));
1322 if (len > sizeof (ULONGEST))
1323 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
1326 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
1329 dwarf_expr_require_composition (op_ptr, op_end,
1330 "DW_OP_implicit_value");
1332 loc->kind = axs_rvalue;
1336 case DW_OP_stack_value:
1337 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
1338 loc->kind = axs_rvalue;
1373 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1374 i = translate_register (arch, op - DW_OP_breg0);
1378 ax_const_l (expr, offset);
1379 ax_simple (expr, aop_add);
1384 op_ptr = read_uleb128 (op_ptr, op_end, ®);
1385 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1386 i = translate_register (arch, reg);
1390 ax_const_l (expr, offset);
1391 ax_simple (expr, aop_add);
1397 const gdb_byte *datastart;
1399 unsigned int before_stack_len;
1401 struct symbol *framefunc;
1402 LONGEST base_offset = 0;
1404 b = block_for_pc (expr->scope);
1407 error (_("No block found for address"));
1409 framefunc = block_linkage_function (b);
1412 error (_("No function found for block"));
1414 dwarf_expr_frame_base_1 (framefunc, expr->scope,
1415 &datastart, &datalen);
1417 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1418 compile_dwarf_to_ax (expr, loc, arch, addr_size, datastart,
1419 datastart + datalen, per_cu);
1423 ax_const_l (expr, offset);
1424 ax_simple (expr, aop_add);
1427 loc->kind = axs_lvalue_memory;
1432 ax_simple (expr, aop_dup);
1436 ax_simple (expr, aop_pop);
1445 ax_simple (expr, aop_swap);
1449 /* We can't directly support DW_OP_over, but GCC emits it as
1450 part of a sequence to implement signed modulus. As a
1451 hack, we recognize this sequence. Note that if GCC ever
1452 generates a branch to the middle of this sequence, then
1453 we will die somehow. */
1454 if (op_end - op_ptr >= 4
1455 && op_ptr[0] == DW_OP_over
1456 && op_ptr[1] == DW_OP_div
1457 && op_ptr[2] == DW_OP_mul
1458 && op_ptr[3] == DW_OP_minus)
1460 /* Sign extend the operands. */
1461 ax_ext (expr, addr_size_bits);
1462 ax_simple (expr, aop_swap);
1463 ax_ext (expr, addr_size_bits);
1464 ax_simple (expr, aop_swap);
1465 ax_simple (expr, aop_rem_signed);
1477 case DW_OP_deref_size:
1481 if (op == DW_OP_deref_size)
1489 ax_simple (expr, aop_ref8);
1492 ax_simple (expr, aop_ref16);
1495 ax_simple (expr, aop_ref32);
1498 ax_simple (expr, aop_ref64);
1501 error (_("Unsupported size %d in %s"),
1502 size, dwarf_stack_op_name (op, 1));
1508 /* Sign extend the operand. */
1509 ax_ext (expr, addr_size_bits);
1510 ax_simple (expr, aop_dup);
1511 ax_const_l (expr, 0);
1512 ax_simple (expr, aop_less_signed);
1513 ax_simple (expr, aop_log_not);
1514 i = ax_goto (expr, aop_if_goto);
1515 /* We have to emit 0 - X. */
1516 ax_const_l (expr, 0);
1517 ax_simple (expr, aop_swap);
1518 ax_simple (expr, aop_sub);
1519 ax_label (expr, i, expr->len);
1523 /* No need to sign extend here. */
1524 ax_const_l (expr, 0);
1525 ax_simple (expr, aop_swap);
1526 ax_simple (expr, aop_sub);
1530 /* Sign extend the operand. */
1531 ax_ext (expr, addr_size_bits);
1532 ax_simple (expr, aop_bit_not);
1535 case DW_OP_plus_uconst:
1536 op_ptr = read_uleb128 (op_ptr, op_end, ®);
1537 /* It would be really weird to emit `DW_OP_plus_uconst 0',
1538 but we micro-optimize anyhow. */
1541 ax_const_l (expr, reg);
1542 ax_simple (expr, aop_add);
1547 ax_simple (expr, aop_bit_and);
1551 /* Sign extend the operands. */
1552 ax_ext (expr, addr_size_bits);
1553 ax_simple (expr, aop_swap);
1554 ax_ext (expr, addr_size_bits);
1555 ax_simple (expr, aop_swap);
1556 ax_simple (expr, aop_div_signed);
1560 ax_simple (expr, aop_sub);
1564 ax_simple (expr, aop_rem_unsigned);
1568 ax_simple (expr, aop_mul);
1572 ax_simple (expr, aop_bit_or);
1576 ax_simple (expr, aop_add);
1580 ax_simple (expr, aop_lsh);
1584 ax_simple (expr, aop_rsh_unsigned);
1588 ax_simple (expr, aop_rsh_signed);
1592 ax_simple (expr, aop_bit_xor);
1596 /* Sign extend the operands. */
1597 ax_ext (expr, addr_size_bits);
1598 ax_simple (expr, aop_swap);
1599 ax_ext (expr, addr_size_bits);
1600 /* Note no swap here: A <= B is !(B < A). */
1601 ax_simple (expr, aop_less_signed);
1602 ax_simple (expr, aop_log_not);
1606 /* Sign extend the operands. */
1607 ax_ext (expr, addr_size_bits);
1608 ax_simple (expr, aop_swap);
1609 ax_ext (expr, addr_size_bits);
1610 ax_simple (expr, aop_swap);
1611 /* A >= B is !(A < B). */
1612 ax_simple (expr, aop_less_signed);
1613 ax_simple (expr, aop_log_not);
1617 /* Sign extend the operands. */
1618 ax_ext (expr, addr_size_bits);
1619 ax_simple (expr, aop_swap);
1620 ax_ext (expr, addr_size_bits);
1621 /* No need for a second swap here. */
1622 ax_simple (expr, aop_equal);
1626 /* Sign extend the operands. */
1627 ax_ext (expr, addr_size_bits);
1628 ax_simple (expr, aop_swap);
1629 ax_ext (expr, addr_size_bits);
1630 ax_simple (expr, aop_swap);
1631 ax_simple (expr, aop_less_signed);
1635 /* Sign extend the operands. */
1636 ax_ext (expr, addr_size_bits);
1637 ax_simple (expr, aop_swap);
1638 ax_ext (expr, addr_size_bits);
1639 /* Note no swap here: A > B is B < A. */
1640 ax_simple (expr, aop_less_signed);
1644 /* Sign extend the operands. */
1645 ax_ext (expr, addr_size_bits);
1646 ax_simple (expr, aop_swap);
1647 ax_ext (expr, addr_size_bits);
1648 /* No need for a swap here. */
1649 ax_simple (expr, aop_equal);
1650 ax_simple (expr, aop_log_not);
1653 case DW_OP_call_frame_cfa:
1657 case DW_OP_GNU_push_tls_address:
1662 offset = extract_signed_integer (op_ptr, 2, byte_order);
1664 i = ax_goto (expr, aop_goto);
1665 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
1666 VEC_safe_push (int, patches, i);
1670 offset = extract_signed_integer (op_ptr, 2, byte_order);
1672 /* Zero extend the operand. */
1673 ax_zero_ext (expr, addr_size_bits);
1674 i = ax_goto (expr, aop_if_goto);
1675 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
1676 VEC_safe_push (int, patches, i);
1683 case DW_OP_bit_piece:
1685 ULONGEST size, offset;
1687 if (op_ptr - 1 == previous_piece)
1688 error (_("Cannot translate empty pieces to agent expressions"));
1689 previous_piece = op_ptr - 1;
1691 op_ptr = read_uleb128 (op_ptr, op_end, &size);
1692 if (op == DW_OP_piece)
1698 op_ptr = read_uleb128 (op_ptr, op_end, &offset);
1700 if (bits_collected + size > 8 * sizeof (LONGEST))
1701 error (_("Expression pieces exceed word size"));
1703 /* Access the bits. */
1706 case axs_lvalue_register:
1707 ax_reg (expr, loc->u.reg);
1710 case axs_lvalue_memory:
1711 /* Offset the pointer, if needed. */
1714 ax_const_l (expr, offset / 8);
1715 ax_simple (expr, aop_add);
1718 access_memory (arch, expr, size);
1722 /* For a bits-big-endian target, shift up what we already
1723 have. For a bits-little-endian target, shift up the
1724 new data. Note that there is a potential bug here if
1725 the DWARF expression leaves multiple values on the
1727 if (bits_collected > 0)
1729 if (bits_big_endian)
1731 ax_simple (expr, aop_swap);
1732 ax_const_l (expr, size);
1733 ax_simple (expr, aop_lsh);
1734 /* We don't need a second swap here, because
1735 aop_bit_or is symmetric. */
1739 ax_const_l (expr, size);
1740 ax_simple (expr, aop_lsh);
1742 ax_simple (expr, aop_bit_or);
1745 bits_collected += size;
1746 loc->kind = axs_rvalue;
1750 case DW_OP_GNU_uninit:
1756 struct dwarf2_locexpr_baton block;
1757 int size = (op == DW_OP_call2 ? 2 : 4);
1759 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
1762 block = dwarf2_fetch_die_location_block (uoffset, per_cu);
1764 /* DW_OP_call_ref is currently not supported. */
1765 gdb_assert (block.per_cu == per_cu);
1767 compile_dwarf_to_ax (expr, loc, arch, addr_size,
1768 block.data, block.data + block.size,
1773 case DW_OP_call_ref:
1777 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1781 /* Patch all the branches we emitted. */
1782 for (i = 0; i < VEC_length (int, patches); ++i)
1784 int targ = offsets[VEC_index (int, dw_labels, i)];
1786 internal_error (__FILE__, __LINE__, _("invalid label"));
1787 ax_label (expr, VEC_index (int, patches, i), targ);
1790 do_cleanups (cleanups);
1794 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1795 evaluator to calculate the location. */
1796 static struct value *
1797 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
1799 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1802 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
1803 dlbaton->size, dlbaton->per_cu);
1808 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1810 locexpr_read_needs_frame (struct symbol *symbol)
1812 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1814 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
1818 /* Return true if DATA points to the end of a piece. END is one past
1819 the last byte in the expression. */
1822 piece_end_p (const gdb_byte *data, const gdb_byte *end)
1824 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
1827 /* Nicely describe a single piece of a location, returning an updated
1828 position in the bytecode sequence. This function cannot recognize
1829 all locations; if a location is not recognized, it simply returns
1832 static const gdb_byte *
1833 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
1834 CORE_ADDR addr, struct objfile *objfile,
1835 const gdb_byte *data, const gdb_byte *end,
1836 unsigned int addr_size)
1838 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1841 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1843 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1844 fprintf_filtered (stream, _("a variable in $%s"),
1845 gdbarch_register_name (gdbarch, regno));
1848 else if (data[0] == DW_OP_regx)
1852 data = read_uleb128 (data + 1, end, ®);
1853 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1854 fprintf_filtered (stream, _("a variable in $%s"),
1855 gdbarch_register_name (gdbarch, regno));
1857 else if (data[0] == DW_OP_fbreg)
1860 struct symbol *framefunc;
1862 LONGEST frame_offset;
1863 const gdb_byte *base_data, *new_data;
1865 LONGEST base_offset = 0;
1867 new_data = read_sleb128 (data + 1, end, &frame_offset);
1868 if (!piece_end_p (new_data, end))
1872 b = block_for_pc (addr);
1875 error (_("No block found for address for symbol \"%s\"."),
1876 SYMBOL_PRINT_NAME (symbol));
1878 framefunc = block_linkage_function (b);
1881 error (_("No function found for block for symbol \"%s\"."),
1882 SYMBOL_PRINT_NAME (symbol));
1884 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
1886 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1888 const gdb_byte *buf_end;
1890 frame_reg = base_data[0] - DW_OP_breg0;
1891 buf_end = read_sleb128 (base_data + 1,
1892 base_data + base_size, &base_offset);
1893 if (buf_end != base_data + base_size)
1894 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1895 frame_reg, SYMBOL_PRINT_NAME (symbol));
1897 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1899 /* The frame base is just the register, with no offset. */
1900 frame_reg = base_data[0] - DW_OP_reg0;
1905 /* We don't know what to do with the frame base expression,
1906 so we can't trace this variable; give up. */
1907 error (_("Cannot describe location of symbol \"%s\"; "
1908 "DWARF 2 encoding not handled, "
1909 "first opcode in base data is 0x%x."),
1910 SYMBOL_PRINT_NAME (symbol), base_data[0]);
1913 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1915 fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1916 gdbarch_register_name (gdbarch, regno),
1917 plongest (base_offset), plongest (frame_offset));
1919 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
1920 && piece_end_p (data, end))
1924 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1926 data = read_sleb128 (data + 1, end, &offset);
1928 fprintf_filtered (stream,
1929 _("a variable at offset %s from base reg $%s"),
1931 gdbarch_register_name (gdbarch, regno));
1934 /* The location expression for a TLS variable looks like this (on a
1937 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1938 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1940 0x3 is the encoding for DW_OP_addr, which has an operand as long
1941 as the size of an address on the target machine (here is 8
1942 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1943 The operand represents the offset at which the variable is within
1944 the thread local storage. */
1946 else if (data + 1 + addr_size < end
1947 && data[0] == DW_OP_addr
1948 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
1949 && piece_end_p (data + 2 + addr_size, end))
1951 CORE_ADDR offset = dwarf2_read_address (gdbarch,
1956 fprintf_filtered (stream,
1957 _("a thread-local variable at offset %s "
1958 "in the thread-local storage for `%s'"),
1959 paddress (gdbarch, offset), objfile->name);
1961 data += 1 + addr_size + 1;
1963 else if (data[0] >= DW_OP_lit0
1964 && data[0] <= DW_OP_lit31
1966 && data[1] == DW_OP_stack_value)
1968 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
1975 /* Disassemble an expression, stopping at the end of a piece or at the
1976 end of the expression. Returns a pointer to the next unread byte
1977 in the input expression. If ALL is nonzero, then this function
1978 will keep going until it reaches the end of the expression. */
1980 static const gdb_byte *
1981 disassemble_dwarf_expression (struct ui_file *stream,
1982 struct gdbarch *arch, unsigned int addr_size,
1984 const gdb_byte *data, const gdb_byte *end,
1987 const gdb_byte *start = data;
1989 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
1993 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
1995 enum dwarf_location_atom op = *data++;
2001 name = dwarf_stack_op_name (op, 0);
2004 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
2005 op, (long) (data - start));
2006 fprintf_filtered (stream, " % 4ld: %s", (long) (data - start), name);
2011 addr = dwarf2_read_address (arch, data, end, addr_size);
2013 fprintf_filtered (stream, " %s", paddress (arch, addr));
2017 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
2019 fprintf_filtered (stream, " %s", pulongest (ul));
2022 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
2024 fprintf_filtered (stream, " %s", plongest (l));
2027 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2029 fprintf_filtered (stream, " %s", pulongest (ul));
2032 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2034 fprintf_filtered (stream, " %s", plongest (l));
2037 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2039 fprintf_filtered (stream, " %s", pulongest (ul));
2042 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
2044 fprintf_filtered (stream, " %s", plongest (l));
2047 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
2049 fprintf_filtered (stream, " %s", pulongest (ul));
2052 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
2054 fprintf_filtered (stream, " %s", plongest (l));
2057 data = read_uleb128 (data, end, &ul);
2058 fprintf_filtered (stream, " %s", pulongest (ul));
2061 data = read_sleb128 (data, end, &l);
2062 fprintf_filtered (stream, " %s", plongest (l));
2097 fprintf_filtered (stream, " [$%s]",
2098 gdbarch_register_name (arch, op - DW_OP_reg0));
2102 data = read_uleb128 (data, end, &ul);
2103 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2104 gdbarch_register_name (arch, (int) ul));
2107 case DW_OP_implicit_value:
2108 data = read_uleb128 (data, end, &ul);
2110 fprintf_filtered (stream, " %s", pulongest (ul));
2145 data = read_sleb128 (data, end, &ul);
2146 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2147 gdbarch_register_name (arch, op - DW_OP_breg0));
2154 data = read_uleb128 (data, end, &ul);
2155 data = read_sleb128 (data, end, &offset);
2156 fprintf_filtered (stream, " register %s [$%s] offset %s",
2158 gdbarch_register_name (arch, (int) ul),
2159 pulongest (offset));
2164 data = read_sleb128 (data, end, &ul);
2165 fprintf_filtered (stream, " %s", pulongest (ul));
2168 case DW_OP_xderef_size:
2169 case DW_OP_deref_size:
2171 fprintf_filtered (stream, " %d", *data);
2175 case DW_OP_plus_uconst:
2176 data = read_uleb128 (data, end, &ul);
2177 fprintf_filtered (stream, " %s", pulongest (ul));
2181 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2183 fprintf_filtered (stream, " to %ld",
2184 (long) (data + l - start));
2188 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2190 fprintf_filtered (stream, " %ld",
2191 (long) (data + l - start));
2195 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2197 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
2201 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2203 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
2206 case DW_OP_call_ref:
2207 ul = extract_unsigned_integer (data, offset_size,
2208 gdbarch_byte_order (arch));
2209 data += offset_size;
2210 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
2214 data = read_uleb128 (data, end, &ul);
2215 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
2218 case DW_OP_bit_piece:
2222 data = read_uleb128 (data, end, &ul);
2223 data = read_uleb128 (data, end, &offset);
2224 fprintf_filtered (stream, " size %s offset %s (bits)",
2225 pulongest (ul), pulongest (offset));
2230 fprintf_filtered (stream, "\n");
2236 /* Describe a single location, which may in turn consist of multiple
2240 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
2241 struct ui_file *stream,
2242 const gdb_byte *data, int size,
2243 struct objfile *objfile, unsigned int addr_size,
2246 const gdb_byte *end = data + size;
2247 int first_piece = 1, bad = 0;
2251 const gdb_byte *here = data;
2252 int disassemble = 1;
2257 fprintf_filtered (stream, _(", and "));
2259 if (!dwarf2_always_disassemble)
2261 data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
2262 data, end, addr_size);
2263 /* If we printed anything, or if we have an empty piece,
2264 then don't disassemble. */
2266 || data[0] == DW_OP_piece
2267 || data[0] == DW_OP_bit_piece)
2271 data = disassemble_dwarf_expression (stream, get_objfile_arch (objfile),
2272 addr_size, offset_size, data, end,
2273 dwarf2_always_disassemble);
2277 int empty = data == here;
2280 fprintf_filtered (stream, " ");
2281 if (data[0] == DW_OP_piece)
2285 data = read_uleb128 (data + 1, end, &bytes);
2288 fprintf_filtered (stream, _("an empty %s-byte piece"),
2291 fprintf_filtered (stream, _(" [%s-byte piece]"),
2294 else if (data[0] == DW_OP_bit_piece)
2296 ULONGEST bits, offset;
2298 data = read_uleb128 (data + 1, end, &bits);
2299 data = read_uleb128 (data, end, &offset);
2302 fprintf_filtered (stream,
2303 _("an empty %s-bit piece"),
2306 fprintf_filtered (stream,
2307 _(" [%s-bit piece, offset %s bits]"),
2308 pulongest (bits), pulongest (offset));
2318 if (bad || data > end)
2319 error (_("Corrupted DWARF2 expression for \"%s\"."),
2320 SYMBOL_PRINT_NAME (symbol));
2323 /* Print a natural-language description of SYMBOL to STREAM. This
2324 version is for a symbol with a single location. */
2327 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
2328 struct ui_file *stream)
2330 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2331 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2332 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2333 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2335 locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
2336 objfile, addr_size, offset_size);
2339 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2340 any necessary bytecode in AX. */
2343 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2344 struct agent_expr *ax, struct axs_value *value)
2346 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2347 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2349 compile_dwarf_to_ax (ax, value, gdbarch, addr_size,
2350 dlbaton->data, dlbaton->data + dlbaton->size,
2354 /* The set of location functions used with the DWARF-2 expression
2356 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
2357 locexpr_read_variable,
2358 locexpr_read_needs_frame,
2359 locexpr_describe_location,
2360 locexpr_tracepoint_var_ref
2364 /* Wrapper functions for location lists. These generally find
2365 the appropriate location expression and call something above. */
2367 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2368 evaluator to calculate the location. */
2369 static struct value *
2370 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
2372 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2374 const gdb_byte *data;
2377 data = find_location_expression (dlbaton, &size,
2378 frame ? get_frame_address_in_block (frame)
2382 val = allocate_value (SYMBOL_TYPE (symbol));
2383 VALUE_LVAL (val) = not_lval;
2384 set_value_optimized_out (val, 1);
2387 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
2393 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
2395 loclist_read_needs_frame (struct symbol *symbol)
2397 /* If there's a location list, then assume we need to have a frame
2398 to choose the appropriate location expression. With tracking of
2399 global variables this is not necessarily true, but such tracking
2400 is disabled in GCC at the moment until we figure out how to
2406 /* Print a natural-language description of SYMBOL to STREAM. This
2407 version applies when there is a list of different locations, each
2408 with a specified address range. */
2411 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
2412 struct ui_file *stream)
2414 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2415 CORE_ADDR low, high;
2416 const gdb_byte *loc_ptr, *buf_end;
2417 int length, first = 1;
2418 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2419 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2420 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2421 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2422 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2423 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2424 /* Adjust base_address for relocatable objects. */
2425 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
2426 SECT_OFF_TEXT (objfile));
2427 CORE_ADDR base_address = dlbaton->base_address + base_offset;
2429 loc_ptr = dlbaton->data;
2430 buf_end = dlbaton->data + dlbaton->size;
2432 fprintf_filtered (stream, _("multi-location:\n"));
2434 /* Iterate through locations until we run out. */
2437 if (buf_end - loc_ptr < 2 * addr_size)
2438 error (_("Corrupted DWARF expression for symbol \"%s\"."),
2439 SYMBOL_PRINT_NAME (symbol));
2441 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2442 loc_ptr += addr_size;
2444 /* A base-address-selection entry. */
2445 if (low == base_mask)
2447 base_address = dwarf2_read_address (gdbarch,
2448 loc_ptr, buf_end, addr_size);
2449 fprintf_filtered (stream, _(" Base address %s"),
2450 paddress (gdbarch, base_address));
2451 loc_ptr += addr_size;
2455 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2456 loc_ptr += addr_size;
2458 /* An end-of-list entry. */
2459 if (low == 0 && high == 0)
2462 /* Otherwise, a location expression entry. */
2463 low += base_address;
2464 high += base_address;
2466 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
2469 /* (It would improve readability to print only the minimum
2470 necessary digits of the second number of the range.) */
2471 fprintf_filtered (stream, _(" Range %s-%s: "),
2472 paddress (gdbarch, low), paddress (gdbarch, high));
2474 /* Now describe this particular location. */
2475 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
2476 objfile, addr_size, offset_size);
2478 fprintf_filtered (stream, "\n");
2484 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2485 any necessary bytecode in AX. */
2487 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2488 struct agent_expr *ax, struct axs_value *value)
2490 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2491 const gdb_byte *data;
2493 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2495 data = find_location_expression (dlbaton, &size, ax->scope);
2497 compile_dwarf_to_ax (ax, value, gdbarch, addr_size, data, data + size,
2501 /* The set of location functions used with the DWARF-2 expression
2502 evaluator and location lists. */
2503 const struct symbol_computed_ops dwarf2_loclist_funcs = {
2504 loclist_read_variable,
2505 loclist_read_needs_frame,
2506 loclist_describe_location,
2507 loclist_tracepoint_var_ref