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 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
71 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
72 /* Adjust base_address for relocatable objects. */
73 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
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."));
85 low = extract_signed_integer (loc_ptr, addr_size, byte_order);
87 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
91 high = extract_signed_integer (loc_ptr, addr_size, byte_order);
93 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
96 /* A base-address-selection entry. */
97 if ((low & base_mask) == base_mask)
99 base_address = high + base_offset;
103 /* An end-of-list entry. */
104 if (low == 0 && high == 0)
107 /* Otherwise, a location expression entry. */
109 high += base_address;
111 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
114 if (pc >= low && pc < high)
116 *locexpr_length = length;
124 /* This is the baton used when performing dwarf2 expression
126 struct dwarf_expr_baton
128 struct frame_info *frame;
129 struct dwarf2_per_cu_data *per_cu;
132 /* Helper functions for dwarf2_evaluate_loc_desc. */
134 /* Using the frame specified in BATON, return the value of register
135 REGNUM, treated as a pointer. */
137 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
139 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
140 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
144 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
145 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
146 regnum, debaton->frame);
150 /* Read memory at ADDR (length LEN) into BUF. */
153 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
155 read_memory (addr, buf, len);
158 /* Using the frame specified in BATON, find the location expression
159 describing the frame base. Return a pointer to it in START and
160 its length in LENGTH. */
162 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
164 /* FIXME: cagney/2003-03-26: This code should be using
165 get_frame_base_address(), and then implement a dwarf2 specific
167 struct symbol *framefunc;
168 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
170 /* Use block_linkage_function, which returns a real (not inlined)
171 function, instead of get_frame_function, which may return an
173 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
175 /* If we found a frame-relative symbol then it was certainly within
176 some function associated with a frame. If we can't find the frame,
177 something has gone wrong. */
178 gdb_assert (framefunc != NULL);
180 dwarf_expr_frame_base_1 (framefunc,
181 get_frame_address_in_block (debaton->frame),
186 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
187 const gdb_byte **start, size_t *length)
189 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
191 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
193 struct dwarf2_loclist_baton *symbaton;
195 symbaton = SYMBOL_LOCATION_BATON (framefunc);
196 *start = find_location_expression (symbaton, length, pc);
200 struct dwarf2_locexpr_baton *symbaton;
202 symbaton = SYMBOL_LOCATION_BATON (framefunc);
203 if (symbaton != NULL)
205 *length = symbaton->size;
206 *start = symbaton->data;
213 error (_("Could not find the frame base for \"%s\"."),
214 SYMBOL_NATURAL_NAME (framefunc));
217 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
218 the frame in BATON. */
221 dwarf_expr_frame_cfa (void *baton)
223 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
225 return dwarf2_frame_cfa (debaton->frame);
228 /* Using the objfile specified in BATON, find the address for the
229 current thread's thread-local storage with offset OFFSET. */
231 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
233 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
234 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
236 return target_translate_tls_address (objfile, offset);
239 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in current CU
240 (as is PER_CU). State of the CTX is not affected by the call and return. */
243 per_cu_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset,
244 struct dwarf2_per_cu_data *per_cu)
246 struct dwarf2_locexpr_baton block;
248 block = dwarf2_fetch_die_location_block (die_offset, per_cu);
250 /* DW_OP_call_ref is currently not supported. */
251 gdb_assert (block.per_cu == per_cu);
253 dwarf_expr_eval (ctx, block.data, block.size);
256 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
259 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
261 struct dwarf_expr_baton *debaton = ctx->baton;
263 return per_cu_dwarf_call (ctx, die_offset, debaton->per_cu);
268 /* Reference count. */
271 /* The number of pieces used to describe this variable. */
274 /* The target address size, used only for DWARF_VALUE_STACK. */
277 /* The pieces themselves. */
278 struct dwarf_expr_piece *pieces;
281 /* Allocate a closure for a value formed from separately-described
284 static struct piece_closure *
285 allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
288 struct piece_closure *c = XZALLOC (struct piece_closure);
291 c->n_pieces = n_pieces;
292 c->addr_size = addr_size;
293 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
295 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
300 /* The lowest-level function to extract bits from a byte buffer.
301 SOURCE is the buffer. It is updated if we read to the end of a
303 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
304 updated to reflect the number of bits actually read.
305 NBITS is the number of bits we want to read. It is updated to
306 reflect the number of bits actually read. This function may read
308 BITS_BIG_ENDIAN is taken directly from gdbarch.
309 This function returns the extracted bits. */
312 extract_bits_primitive (const gdb_byte **source,
313 unsigned int *source_offset_bits,
314 int *nbits, int bits_big_endian)
316 unsigned int avail, mask, datum;
318 gdb_assert (*source_offset_bits < 8);
320 avail = 8 - *source_offset_bits;
324 mask = (1 << avail) - 1;
327 datum >>= 8 - (*source_offset_bits + *nbits);
329 datum >>= *source_offset_bits;
333 *source_offset_bits += avail;
334 if (*source_offset_bits >= 8)
336 *source_offset_bits -= 8;
343 /* Extract some bits from a source buffer and move forward in the
346 SOURCE is the source buffer. It is updated as bytes are read.
347 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
349 NBITS is the number of bits to read.
350 BITS_BIG_ENDIAN is taken directly from gdbarch.
352 This function returns the bits that were read. */
355 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
356 int nbits, int bits_big_endian)
360 gdb_assert (nbits > 0 && nbits <= 8);
362 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
368 more = extract_bits_primitive (source, source_offset_bits, &nbits,
380 /* Write some bits into a buffer and move forward in the buffer.
382 DATUM is the bits to write. The low-order bits of DATUM are used.
383 DEST is the destination buffer. It is updated as bytes are
385 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
387 NBITS is the number of valid bits in DATUM.
388 BITS_BIG_ENDIAN is taken directly from gdbarch. */
391 insert_bits (unsigned int datum,
392 gdb_byte *dest, unsigned int dest_offset_bits,
393 int nbits, int bits_big_endian)
397 gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8);
399 mask = (1 << nbits) - 1;
402 datum <<= 8 - (dest_offset_bits + nbits);
403 mask <<= 8 - (dest_offset_bits + nbits);
407 datum <<= dest_offset_bits;
408 mask <<= dest_offset_bits;
411 gdb_assert ((datum & ~mask) == 0);
413 *dest = (*dest & ~mask) | datum;
416 /* Copy bits from a source to a destination.
418 DEST is where the bits should be written.
419 DEST_OFFSET_BITS is the bit offset into DEST.
420 SOURCE is the source of bits.
421 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
422 BIT_COUNT is the number of bits to copy.
423 BITS_BIG_ENDIAN is taken directly from gdbarch. */
426 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
427 const gdb_byte *source, unsigned int source_offset_bits,
428 unsigned int bit_count,
431 unsigned int dest_avail;
434 /* Reduce everything to byte-size pieces. */
435 dest += dest_offset_bits / 8;
436 dest_offset_bits %= 8;
437 source += source_offset_bits / 8;
438 source_offset_bits %= 8;
440 dest_avail = 8 - dest_offset_bits % 8;
442 /* See if we can fill the first destination byte. */
443 if (dest_avail < bit_count)
445 datum = extract_bits (&source, &source_offset_bits, dest_avail,
447 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
449 dest_offset_bits = 0;
450 bit_count -= dest_avail;
453 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
454 than 8 bits remaining. */
455 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
456 for (; bit_count >= 8; bit_count -= 8)
458 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
459 *dest++ = (gdb_byte) datum;
462 /* Finally, we may have a few leftover bits. */
463 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
466 datum = extract_bits (&source, &source_offset_bits, bit_count,
468 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
473 read_pieced_value (struct value *v)
477 ULONGEST bits_to_skip;
479 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
480 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
482 size_t buffer_size = 0;
484 struct cleanup *cleanup;
486 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
488 if (value_type (v) != value_enclosing_type (v))
489 internal_error (__FILE__, __LINE__,
490 _("Should not be able to create a lazy value with "
491 "an enclosing type"));
493 cleanup = make_cleanup (free_current_contents, &buffer);
495 contents = value_contents_raw (v);
496 bits_to_skip = 8 * value_offset (v);
497 if (value_bitsize (v))
499 bits_to_skip += value_bitpos (v);
500 type_len = value_bitsize (v);
503 type_len = 8 * TYPE_LENGTH (value_type (v));
505 for (i = 0; i < c->n_pieces && offset < type_len; i++)
507 struct dwarf_expr_piece *p = &c->pieces[i];
508 size_t this_size, this_size_bits;
509 long dest_offset_bits, source_offset_bits, source_offset;
510 const gdb_byte *intermediate_buffer;
512 /* Compute size, source, and destination offsets for copying, in
514 this_size_bits = p->size;
515 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
517 bits_to_skip -= this_size_bits;
520 if (this_size_bits > type_len - offset)
521 this_size_bits = type_len - offset;
522 if (bits_to_skip > 0)
524 dest_offset_bits = 0;
525 source_offset_bits = bits_to_skip;
526 this_size_bits -= bits_to_skip;
531 dest_offset_bits = offset;
532 source_offset_bits = 0;
535 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
536 source_offset = source_offset_bits / 8;
537 if (buffer_size < this_size)
539 buffer_size = this_size;
540 buffer = xrealloc (buffer, buffer_size);
542 intermediate_buffer = buffer;
544 /* Copy from the source to DEST_BUFFER. */
547 case DWARF_VALUE_REGISTER:
549 struct gdbarch *arch = get_frame_arch (frame);
550 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.value);
551 int reg_offset = source_offset;
553 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
554 && this_size < register_size (arch, gdb_regnum))
556 /* Big-endian, and we want less than full size. */
557 reg_offset = register_size (arch, gdb_regnum) - this_size;
558 /* We want the lower-order THIS_SIZE_BITS of the bytes
559 we extract from the register. */
560 source_offset_bits += 8 * this_size - this_size_bits;
563 if (gdb_regnum != -1)
565 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
570 error (_("Unable to access DWARF register number %s"),
571 paddress (arch, p->v.value));
576 case DWARF_VALUE_MEMORY:
577 if (p->v.mem.in_stack_memory)
578 read_stack (p->v.mem.addr + source_offset, buffer, this_size);
580 read_memory (p->v.mem.addr + source_offset, buffer, this_size);
583 case DWARF_VALUE_STACK:
585 struct gdbarch *gdbarch = get_type_arch (value_type (v));
586 size_t n = this_size;
588 if (n > c->addr_size - source_offset)
589 n = (c->addr_size >= source_offset
590 ? c->addr_size - source_offset
596 else if (source_offset == 0)
597 store_unsigned_integer (buffer, n,
598 gdbarch_byte_order (gdbarch),
602 gdb_byte bytes[sizeof (ULONGEST)];
604 store_unsigned_integer (bytes, n + source_offset,
605 gdbarch_byte_order (gdbarch),
607 memcpy (buffer, bytes + source_offset, n);
612 case DWARF_VALUE_LITERAL:
614 size_t n = this_size;
616 if (n > p->v.literal.length - source_offset)
617 n = (p->v.literal.length >= source_offset
618 ? p->v.literal.length - source_offset
621 intermediate_buffer = p->v.literal.data + source_offset;
625 case DWARF_VALUE_OPTIMIZED_OUT:
626 set_value_optimized_out (v, 1);
630 internal_error (__FILE__, __LINE__, _("invalid location type"));
633 if (p->location != DWARF_VALUE_OPTIMIZED_OUT)
634 copy_bitwise (contents, dest_offset_bits,
635 intermediate_buffer, source_offset_bits % 8,
636 this_size_bits, bits_big_endian);
638 offset += this_size_bits;
641 do_cleanups (cleanup);
645 write_pieced_value (struct value *to, struct value *from)
649 ULONGEST bits_to_skip;
650 const gdb_byte *contents;
651 struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
652 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
654 size_t buffer_size = 0;
656 struct cleanup *cleanup;
658 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
662 set_value_optimized_out (to, 1);
666 cleanup = make_cleanup (free_current_contents, &buffer);
668 contents = value_contents (from);
669 bits_to_skip = 8 * value_offset (to);
670 if (value_bitsize (to))
672 bits_to_skip += value_bitpos (to);
673 type_len = value_bitsize (to);
676 type_len = 8 * TYPE_LENGTH (value_type (to));
678 for (i = 0; i < c->n_pieces && offset < type_len; i++)
680 struct dwarf_expr_piece *p = &c->pieces[i];
681 size_t this_size_bits, this_size;
682 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
684 const gdb_byte *source_buffer;
686 this_size_bits = p->size;
687 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
689 bits_to_skip -= this_size_bits;
692 if (this_size_bits > type_len - offset)
693 this_size_bits = type_len - offset;
694 if (bits_to_skip > 0)
696 dest_offset_bits = bits_to_skip;
697 source_offset_bits = 0;
698 this_size_bits -= bits_to_skip;
703 dest_offset_bits = 0;
704 source_offset_bits = offset;
707 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
708 source_offset = source_offset_bits / 8;
709 dest_offset = dest_offset_bits / 8;
710 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
712 source_buffer = contents + source_offset;
717 if (buffer_size < this_size)
719 buffer_size = this_size;
720 buffer = xrealloc (buffer, buffer_size);
722 source_buffer = buffer;
728 case DWARF_VALUE_REGISTER:
730 struct gdbarch *arch = get_frame_arch (frame);
731 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.value);
732 int reg_offset = dest_offset;
734 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
735 && this_size <= register_size (arch, gdb_regnum))
736 /* Big-endian, and we want less than full size. */
737 reg_offset = register_size (arch, gdb_regnum) - this_size;
739 if (gdb_regnum != -1)
743 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
745 copy_bitwise (buffer, dest_offset_bits,
746 contents, source_offset_bits,
751 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
752 this_size, source_buffer);
756 error (_("Unable to write to DWARF register number %s"),
757 paddress (arch, p->v.value));
761 case DWARF_VALUE_MEMORY:
764 /* Only the first and last bytes can possibly have any
766 read_memory (p->v.mem.addr + dest_offset, buffer, 1);
767 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
768 buffer + this_size - 1, 1);
769 copy_bitwise (buffer, dest_offset_bits,
770 contents, source_offset_bits,
775 write_memory (p->v.mem.addr + dest_offset,
776 source_buffer, this_size);
779 set_value_optimized_out (to, 1);
782 offset += this_size_bits;
785 do_cleanups (cleanup);
789 check_pieced_value_bits (const struct value *value, int bit_offset,
790 int bit_length, int validity)
792 struct piece_closure *c
793 = (struct piece_closure *) value_computed_closure (value);
796 bit_offset += 8 * value_offset (value);
797 if (value_bitsize (value))
798 bit_offset += value_bitpos (value);
800 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
802 struct dwarf_expr_piece *p = &c->pieces[i];
803 size_t this_size_bits = p->size;
807 if (bit_offset >= this_size_bits)
809 bit_offset -= this_size_bits;
813 bit_length -= this_size_bits - bit_offset;
817 bit_length -= this_size_bits;
819 if (p->location == DWARF_VALUE_OPTIMIZED_OUT)
835 check_pieced_value_validity (const struct value *value, int bit_offset,
838 return check_pieced_value_bits (value, bit_offset, bit_length, 1);
842 check_pieced_value_invalid (const struct value *value)
844 return check_pieced_value_bits (value, 0,
845 8 * TYPE_LENGTH (value_type (value)), 0);
849 copy_pieced_value_closure (const struct value *v)
851 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
858 free_pieced_value_closure (struct value *v)
860 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
870 /* Functions for accessing a variable described by DW_OP_piece. */
871 static struct lval_funcs pieced_value_funcs = {
874 check_pieced_value_validity,
875 check_pieced_value_invalid,
876 copy_pieced_value_closure,
877 free_pieced_value_closure
880 /* Evaluate a location description, starting at DATA and with length
881 SIZE, to find the current location of variable of TYPE in the context
885 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
886 const gdb_byte *data, unsigned short size,
887 struct dwarf2_per_cu_data *per_cu)
889 struct value *retval;
890 struct dwarf_expr_baton baton;
891 struct dwarf_expr_context *ctx;
892 struct cleanup *old_chain;
893 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
897 retval = allocate_value (type);
898 VALUE_LVAL (retval) = not_lval;
899 set_value_optimized_out (retval, 1);
904 baton.per_cu = per_cu;
906 ctx = new_dwarf_expr_context ();
907 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
909 ctx->gdbarch = get_objfile_arch (objfile);
910 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
911 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
913 ctx->read_reg = dwarf_expr_read_reg;
914 ctx->read_mem = dwarf_expr_read_mem;
915 ctx->get_frame_base = dwarf_expr_frame_base;
916 ctx->get_frame_cfa = dwarf_expr_frame_cfa;
917 ctx->get_tls_address = dwarf_expr_tls_address;
918 ctx->dwarf_call = dwarf_expr_dwarf_call;
920 dwarf_expr_eval (ctx, data, size);
921 if (ctx->num_pieces > 0)
923 struct piece_closure *c;
924 struct frame_id frame_id = get_frame_id (frame);
926 c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
928 retval = allocate_computed_value (type, &pieced_value_funcs, c);
929 VALUE_FRAME_ID (retval) = frame_id;
933 switch (ctx->location)
935 case DWARF_VALUE_REGISTER:
937 struct gdbarch *arch = get_frame_arch (frame);
938 ULONGEST dwarf_regnum = dwarf_expr_fetch (ctx, 0);
939 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
941 if (gdb_regnum != -1)
942 retval = value_from_register (type, gdb_regnum, frame);
944 error (_("Unable to access DWARF register number %s"),
945 paddress (arch, dwarf_regnum));
949 case DWARF_VALUE_MEMORY:
951 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
952 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
954 retval = allocate_value (type);
955 VALUE_LVAL (retval) = lval_memory;
956 set_value_lazy (retval, 1);
958 set_value_stack (retval, 1);
959 set_value_address (retval, address);
963 case DWARF_VALUE_STACK:
965 ULONGEST value = dwarf_expr_fetch (ctx, 0);
967 size_t n = ctx->addr_size;
969 retval = allocate_value (type);
970 contents = value_contents_raw (retval);
971 if (n > TYPE_LENGTH (type))
972 n = TYPE_LENGTH (type);
973 store_unsigned_integer (contents, n,
974 gdbarch_byte_order (ctx->gdbarch),
979 case DWARF_VALUE_LITERAL:
984 retval = allocate_value (type);
985 contents = value_contents_raw (retval);
986 if (n > TYPE_LENGTH (type))
987 n = TYPE_LENGTH (type);
988 memcpy (contents, ctx->data, n);
992 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
993 it can only be encountered when making a piece. */
994 case DWARF_VALUE_OPTIMIZED_OUT:
996 internal_error (__FILE__, __LINE__, _("invalid location type"));
1000 set_value_initialized (retval, ctx->initialized);
1002 do_cleanups (old_chain);
1007 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
1009 struct needs_frame_baton
1012 struct dwarf2_per_cu_data *per_cu;
1015 /* Reads from registers do require a frame. */
1017 needs_frame_read_reg (void *baton, int regnum)
1019 struct needs_frame_baton *nf_baton = baton;
1021 nf_baton->needs_frame = 1;
1025 /* Reads from memory do not require a frame. */
1027 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
1029 memset (buf, 0, len);
1032 /* Frame-relative accesses do require a frame. */
1034 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
1036 static gdb_byte lit0 = DW_OP_lit0;
1037 struct needs_frame_baton *nf_baton = baton;
1042 nf_baton->needs_frame = 1;
1045 /* CFA accesses require a frame. */
1048 needs_frame_frame_cfa (void *baton)
1050 struct needs_frame_baton *nf_baton = baton;
1052 nf_baton->needs_frame = 1;
1056 /* Thread-local accesses do require a frame. */
1058 needs_frame_tls_address (void *baton, CORE_ADDR offset)
1060 struct needs_frame_baton *nf_baton = baton;
1062 nf_baton->needs_frame = 1;
1066 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
1069 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
1071 struct needs_frame_baton *nf_baton = ctx->baton;
1073 return per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu);
1076 /* Return non-zero iff the location expression at DATA (length SIZE)
1077 requires a frame to evaluate. */
1080 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
1081 struct dwarf2_per_cu_data *per_cu)
1083 struct needs_frame_baton baton;
1084 struct dwarf_expr_context *ctx;
1086 struct cleanup *old_chain;
1087 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1089 baton.needs_frame = 0;
1090 baton.per_cu = per_cu;
1092 ctx = new_dwarf_expr_context ();
1093 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1095 ctx->gdbarch = get_objfile_arch (objfile);
1096 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1097 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1098 ctx->baton = &baton;
1099 ctx->read_reg = needs_frame_read_reg;
1100 ctx->read_mem = needs_frame_read_mem;
1101 ctx->get_frame_base = needs_frame_frame_base;
1102 ctx->get_frame_cfa = needs_frame_frame_cfa;
1103 ctx->get_tls_address = needs_frame_tls_address;
1104 ctx->dwarf_call = needs_frame_dwarf_call;
1106 dwarf_expr_eval (ctx, data, size);
1108 in_reg = ctx->location == DWARF_VALUE_REGISTER;
1110 if (ctx->num_pieces > 0)
1114 /* If the location has several pieces, and any of them are in
1115 registers, then we will need a frame to fetch them from. */
1116 for (i = 0; i < ctx->num_pieces; i++)
1117 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
1121 do_cleanups (old_chain);
1123 return baton.needs_frame || in_reg;
1126 /* A helper function that throws an unimplemented error mentioning a
1127 given DWARF operator. */
1130 unimplemented (unsigned int op)
1132 error (_("DWARF operator %s cannot be translated to an agent expression"),
1133 dwarf_stack_op_name (op, 1));
1136 /* A helper function to convert a DWARF register to an arch register.
1137 ARCH is the architecture.
1138 DWARF_REG is the register.
1139 This will throw an exception if the DWARF register cannot be
1140 translated to an architecture register. */
1143 translate_register (struct gdbarch *arch, int dwarf_reg)
1145 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
1147 error (_("Unable to access DWARF register number %d"), dwarf_reg);
1151 /* A helper function that emits an access to memory. ARCH is the
1152 target architecture. EXPR is the expression which we are building.
1153 NBITS is the number of bits we want to read. This emits the
1154 opcodes needed to read the memory and then extract the desired
1158 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
1160 ULONGEST nbytes = (nbits + 7) / 8;
1162 gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
1165 ax_trace_quick (expr, nbytes);
1168 ax_simple (expr, aop_ref8);
1169 else if (nbits <= 16)
1170 ax_simple (expr, aop_ref16);
1171 else if (nbits <= 32)
1172 ax_simple (expr, aop_ref32);
1174 ax_simple (expr, aop_ref64);
1176 /* If we read exactly the number of bytes we wanted, we're done. */
1177 if (8 * nbytes == nbits)
1180 if (gdbarch_bits_big_endian (arch))
1182 /* On a bits-big-endian machine, we want the high-order
1184 ax_const_l (expr, 8 * nbytes - nbits);
1185 ax_simple (expr, aop_rsh_unsigned);
1189 /* On a bits-little-endian box, we want the low-order NBITS. */
1190 ax_zero_ext (expr, nbits);
1194 /* Compile a DWARF location expression to an agent expression.
1196 EXPR is the agent expression we are building.
1197 LOC is the agent value we modify.
1198 ARCH is the architecture.
1199 ADDR_SIZE is the size of addresses, in bytes.
1200 OP_PTR is the start of the location expression.
1201 OP_END is one past the last byte of the location expression.
1203 This will throw an exception for various kinds of errors -- for
1204 example, if the expression cannot be compiled, or if the expression
1208 compile_dwarf_to_ax (struct agent_expr *expr, struct axs_value *loc,
1209 struct gdbarch *arch, unsigned int addr_size,
1210 const gdb_byte *op_ptr, const gdb_byte *op_end,
1211 struct dwarf2_per_cu_data *per_cu)
1213 struct cleanup *cleanups;
1215 VEC(int) *dw_labels = NULL, *patches = NULL;
1216 const gdb_byte * const base = op_ptr;
1217 const gdb_byte *previous_piece = op_ptr;
1218 enum bfd_endian byte_order = gdbarch_byte_order (arch);
1219 ULONGEST bits_collected = 0;
1220 unsigned int addr_size_bits = 8 * addr_size;
1221 int bits_big_endian = gdbarch_bits_big_endian (arch);
1223 offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
1224 cleanups = make_cleanup (xfree, offsets);
1226 for (i = 0; i < op_end - op_ptr; ++i)
1229 make_cleanup (VEC_cleanup (int), &dw_labels);
1230 make_cleanup (VEC_cleanup (int), &patches);
1232 /* By default we are making an address. */
1233 loc->kind = axs_lvalue_memory;
1235 while (op_ptr < op_end)
1237 enum dwarf_location_atom op = *op_ptr;
1238 ULONGEST uoffset, reg;
1242 offsets[op_ptr - base] = expr->len;
1245 /* Our basic approach to code generation is to map DWARF
1246 operations directly to AX operations. However, there are
1249 First, DWARF works on address-sized units, but AX always uses
1250 LONGEST. For most operations we simply ignore this
1251 difference; instead we generate sign extensions as needed
1252 before division and comparison operations. It would be nice
1253 to omit the sign extensions, but there is no way to determine
1254 the size of the target's LONGEST. (This code uses the size
1255 of the host LONGEST in some cases -- that is a bug but it is
1258 Second, some DWARF operations cannot be translated to AX.
1259 For these we simply fail. See
1260 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
1295 ax_const_l (expr, op - DW_OP_lit0);
1299 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
1300 op_ptr += addr_size;
1301 /* Some versions of GCC emit DW_OP_addr before
1302 DW_OP_GNU_push_tls_address. In this case the value is an
1303 index, not an address. We don't support things like
1304 branching between the address and the TLS op. */
1305 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
1306 uoffset += dwarf2_per_cu_text_offset (per_cu);
1307 ax_const_l (expr, uoffset);
1311 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
1315 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
1319 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
1323 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
1327 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
1331 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
1335 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
1339 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
1343 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
1344 ax_const_l (expr, uoffset);
1347 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1348 ax_const_l (expr, offset);
1383 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1384 loc->u.reg = translate_register (arch, op - DW_OP_reg0);
1385 loc->kind = axs_lvalue_register;
1389 op_ptr = read_uleb128 (op_ptr, op_end, ®);
1390 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1391 loc->u.reg = translate_register (arch, reg);
1392 loc->kind = axs_lvalue_register;
1395 case DW_OP_implicit_value:
1399 op_ptr = read_uleb128 (op_ptr, op_end, &len);
1400 if (op_ptr + len > op_end)
1401 error (_("DW_OP_implicit_value: too few bytes available."));
1402 if (len > sizeof (ULONGEST))
1403 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
1406 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
1409 dwarf_expr_require_composition (op_ptr, op_end,
1410 "DW_OP_implicit_value");
1412 loc->kind = axs_rvalue;
1416 case DW_OP_stack_value:
1417 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
1418 loc->kind = axs_rvalue;
1453 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1454 i = translate_register (arch, op - DW_OP_breg0);
1458 ax_const_l (expr, offset);
1459 ax_simple (expr, aop_add);
1464 op_ptr = read_uleb128 (op_ptr, op_end, ®);
1465 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1466 i = translate_register (arch, reg);
1470 ax_const_l (expr, offset);
1471 ax_simple (expr, aop_add);
1477 const gdb_byte *datastart;
1479 unsigned int before_stack_len;
1481 struct symbol *framefunc;
1482 LONGEST base_offset = 0;
1484 b = block_for_pc (expr->scope);
1487 error (_("No block found for address"));
1489 framefunc = block_linkage_function (b);
1492 error (_("No function found for block"));
1494 dwarf_expr_frame_base_1 (framefunc, expr->scope,
1495 &datastart, &datalen);
1497 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1498 compile_dwarf_to_ax (expr, loc, arch, addr_size, datastart,
1499 datastart + datalen, per_cu);
1503 ax_const_l (expr, offset);
1504 ax_simple (expr, aop_add);
1507 loc->kind = axs_lvalue_memory;
1512 ax_simple (expr, aop_dup);
1516 ax_simple (expr, aop_pop);
1525 ax_simple (expr, aop_swap);
1529 /* We can't directly support DW_OP_over, but GCC emits it as
1530 part of a sequence to implement signed modulus. As a
1531 hack, we recognize this sequence. Note that if GCC ever
1532 generates a branch to the middle of this sequence, then
1533 we will die somehow. */
1534 if (op_end - op_ptr >= 4
1535 && op_ptr[0] == DW_OP_over
1536 && op_ptr[1] == DW_OP_div
1537 && op_ptr[2] == DW_OP_mul
1538 && op_ptr[3] == DW_OP_minus)
1540 /* Sign extend the operands. */
1541 ax_ext (expr, addr_size_bits);
1542 ax_simple (expr, aop_swap);
1543 ax_ext (expr, addr_size_bits);
1544 ax_simple (expr, aop_swap);
1545 ax_simple (expr, aop_rem_signed);
1557 case DW_OP_deref_size:
1561 if (op == DW_OP_deref_size)
1569 ax_simple (expr, aop_ref8);
1572 ax_simple (expr, aop_ref16);
1575 ax_simple (expr, aop_ref32);
1578 ax_simple (expr, aop_ref64);
1581 error (_("Unsupported size %d in %s"),
1582 size, dwarf_stack_op_name (op, 1));
1588 /* Sign extend the operand. */
1589 ax_ext (expr, addr_size_bits);
1590 ax_simple (expr, aop_dup);
1591 ax_const_l (expr, 0);
1592 ax_simple (expr, aop_less_signed);
1593 ax_simple (expr, aop_log_not);
1594 i = ax_goto (expr, aop_if_goto);
1595 /* We have to emit 0 - X. */
1596 ax_const_l (expr, 0);
1597 ax_simple (expr, aop_swap);
1598 ax_simple (expr, aop_sub);
1599 ax_label (expr, i, expr->len);
1603 /* No need to sign extend here. */
1604 ax_const_l (expr, 0);
1605 ax_simple (expr, aop_swap);
1606 ax_simple (expr, aop_sub);
1610 /* Sign extend the operand. */
1611 ax_ext (expr, addr_size_bits);
1612 ax_simple (expr, aop_bit_not);
1615 case DW_OP_plus_uconst:
1616 op_ptr = read_uleb128 (op_ptr, op_end, ®);
1617 /* It would be really weird to emit `DW_OP_plus_uconst 0',
1618 but we micro-optimize anyhow. */
1621 ax_const_l (expr, reg);
1622 ax_simple (expr, aop_add);
1627 ax_simple (expr, aop_bit_and);
1631 /* Sign extend the operands. */
1632 ax_ext (expr, addr_size_bits);
1633 ax_simple (expr, aop_swap);
1634 ax_ext (expr, addr_size_bits);
1635 ax_simple (expr, aop_swap);
1636 ax_simple (expr, aop_div_signed);
1640 ax_simple (expr, aop_sub);
1644 ax_simple (expr, aop_rem_unsigned);
1648 ax_simple (expr, aop_mul);
1652 ax_simple (expr, aop_bit_or);
1656 ax_simple (expr, aop_add);
1660 ax_simple (expr, aop_lsh);
1664 ax_simple (expr, aop_rsh_unsigned);
1668 ax_simple (expr, aop_rsh_signed);
1672 ax_simple (expr, aop_bit_xor);
1676 /* Sign extend the operands. */
1677 ax_ext (expr, addr_size_bits);
1678 ax_simple (expr, aop_swap);
1679 ax_ext (expr, addr_size_bits);
1680 /* Note no swap here: A <= B is !(B < A). */
1681 ax_simple (expr, aop_less_signed);
1682 ax_simple (expr, aop_log_not);
1686 /* Sign extend the operands. */
1687 ax_ext (expr, addr_size_bits);
1688 ax_simple (expr, aop_swap);
1689 ax_ext (expr, addr_size_bits);
1690 ax_simple (expr, aop_swap);
1691 /* A >= B is !(A < B). */
1692 ax_simple (expr, aop_less_signed);
1693 ax_simple (expr, aop_log_not);
1697 /* Sign extend the operands. */
1698 ax_ext (expr, addr_size_bits);
1699 ax_simple (expr, aop_swap);
1700 ax_ext (expr, addr_size_bits);
1701 /* No need for a second swap here. */
1702 ax_simple (expr, aop_equal);
1706 /* Sign extend the operands. */
1707 ax_ext (expr, addr_size_bits);
1708 ax_simple (expr, aop_swap);
1709 ax_ext (expr, addr_size_bits);
1710 ax_simple (expr, aop_swap);
1711 ax_simple (expr, aop_less_signed);
1715 /* Sign extend the operands. */
1716 ax_ext (expr, addr_size_bits);
1717 ax_simple (expr, aop_swap);
1718 ax_ext (expr, addr_size_bits);
1719 /* Note no swap here: A > B is B < A. */
1720 ax_simple (expr, aop_less_signed);
1724 /* Sign extend the operands. */
1725 ax_ext (expr, addr_size_bits);
1726 ax_simple (expr, aop_swap);
1727 ax_ext (expr, addr_size_bits);
1728 /* No need for a swap here. */
1729 ax_simple (expr, aop_equal);
1730 ax_simple (expr, aop_log_not);
1733 case DW_OP_call_frame_cfa:
1737 case DW_OP_GNU_push_tls_address:
1742 offset = extract_signed_integer (op_ptr, 2, byte_order);
1744 i = ax_goto (expr, aop_goto);
1745 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
1746 VEC_safe_push (int, patches, i);
1750 offset = extract_signed_integer (op_ptr, 2, byte_order);
1752 /* Zero extend the operand. */
1753 ax_zero_ext (expr, addr_size_bits);
1754 i = ax_goto (expr, aop_if_goto);
1755 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
1756 VEC_safe_push (int, patches, i);
1763 case DW_OP_bit_piece:
1765 ULONGEST size, offset;
1767 if (op_ptr - 1 == previous_piece)
1768 error (_("Cannot translate empty pieces to agent expressions"));
1769 previous_piece = op_ptr - 1;
1771 op_ptr = read_uleb128 (op_ptr, op_end, &size);
1772 if (op == DW_OP_piece)
1778 op_ptr = read_uleb128 (op_ptr, op_end, &offset);
1780 if (bits_collected + size > 8 * sizeof (LONGEST))
1781 error (_("Expression pieces exceed word size"));
1783 /* Access the bits. */
1786 case axs_lvalue_register:
1787 ax_reg (expr, loc->u.reg);
1790 case axs_lvalue_memory:
1791 /* Offset the pointer, if needed. */
1794 ax_const_l (expr, offset / 8);
1795 ax_simple (expr, aop_add);
1798 access_memory (arch, expr, size);
1802 /* For a bits-big-endian target, shift up what we already
1803 have. For a bits-little-endian target, shift up the
1804 new data. Note that there is a potential bug here if
1805 the DWARF expression leaves multiple values on the
1807 if (bits_collected > 0)
1809 if (bits_big_endian)
1811 ax_simple (expr, aop_swap);
1812 ax_const_l (expr, size);
1813 ax_simple (expr, aop_lsh);
1814 /* We don't need a second swap here, because
1815 aop_bit_or is symmetric. */
1819 ax_const_l (expr, size);
1820 ax_simple (expr, aop_lsh);
1822 ax_simple (expr, aop_bit_or);
1825 bits_collected += size;
1826 loc->kind = axs_rvalue;
1830 case DW_OP_GNU_uninit:
1836 struct dwarf2_locexpr_baton block;
1837 int size = (op == DW_OP_call2 ? 2 : 4);
1839 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
1842 block = dwarf2_fetch_die_location_block (uoffset, per_cu);
1844 /* DW_OP_call_ref is currently not supported. */
1845 gdb_assert (block.per_cu == per_cu);
1847 compile_dwarf_to_ax (expr, loc, arch, addr_size,
1848 block.data, block.data + block.size,
1853 case DW_OP_call_ref:
1857 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1861 /* Patch all the branches we emitted. */
1862 for (i = 0; i < VEC_length (int, patches); ++i)
1864 int targ = offsets[VEC_index (int, dw_labels, i)];
1866 internal_error (__FILE__, __LINE__, _("invalid label"));
1867 ax_label (expr, VEC_index (int, patches, i), targ);
1870 do_cleanups (cleanups);
1874 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1875 evaluator to calculate the location. */
1876 static struct value *
1877 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
1879 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1882 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
1883 dlbaton->size, dlbaton->per_cu);
1888 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1890 locexpr_read_needs_frame (struct symbol *symbol)
1892 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1894 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
1898 /* Return true if DATA points to the end of a piece. END is one past
1899 the last byte in the expression. */
1902 piece_end_p (const gdb_byte *data, const gdb_byte *end)
1904 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
1907 /* Nicely describe a single piece of a location, returning an updated
1908 position in the bytecode sequence. This function cannot recognize
1909 all locations; if a location is not recognized, it simply returns
1912 static const gdb_byte *
1913 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
1914 CORE_ADDR addr, struct objfile *objfile,
1915 const gdb_byte *data, const gdb_byte *end,
1916 unsigned int addr_size)
1918 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1921 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1923 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1924 fprintf_filtered (stream, _("a variable in $%s"),
1925 gdbarch_register_name (gdbarch, regno));
1928 else if (data[0] == DW_OP_regx)
1932 data = read_uleb128 (data + 1, end, ®);
1933 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1934 fprintf_filtered (stream, _("a variable in $%s"),
1935 gdbarch_register_name (gdbarch, regno));
1937 else if (data[0] == DW_OP_fbreg)
1940 struct symbol *framefunc;
1942 LONGEST frame_offset;
1943 const gdb_byte *base_data, *new_data, *save_data = data;
1945 LONGEST base_offset = 0;
1947 new_data = read_sleb128 (data + 1, end, &frame_offset);
1948 if (!piece_end_p (new_data, end))
1952 b = block_for_pc (addr);
1955 error (_("No block found for address for symbol \"%s\"."),
1956 SYMBOL_PRINT_NAME (symbol));
1958 framefunc = block_linkage_function (b);
1961 error (_("No function found for block for symbol \"%s\"."),
1962 SYMBOL_PRINT_NAME (symbol));
1964 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
1966 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1968 const gdb_byte *buf_end;
1970 frame_reg = base_data[0] - DW_OP_breg0;
1971 buf_end = read_sleb128 (base_data + 1,
1972 base_data + base_size, &base_offset);
1973 if (buf_end != base_data + base_size)
1974 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1975 frame_reg, SYMBOL_PRINT_NAME (symbol));
1977 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1979 /* The frame base is just the register, with no offset. */
1980 frame_reg = base_data[0] - DW_OP_reg0;
1985 /* We don't know what to do with the frame base expression,
1986 so we can't trace this variable; give up. */
1990 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1992 fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1993 gdbarch_register_name (gdbarch, regno),
1994 plongest (base_offset), plongest (frame_offset));
1996 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
1997 && piece_end_p (data, end))
2001 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
2003 data = read_sleb128 (data + 1, end, &offset);
2005 fprintf_filtered (stream,
2006 _("a variable at offset %s from base reg $%s"),
2008 gdbarch_register_name (gdbarch, regno));
2011 /* The location expression for a TLS variable looks like this (on a
2014 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
2015 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
2017 0x3 is the encoding for DW_OP_addr, which has an operand as long
2018 as the size of an address on the target machine (here is 8
2019 bytes). Note that more recent version of GCC emit DW_OP_const4u
2020 or DW_OP_const8u, depending on address size, rather than
2021 DW_OP_addr. 0xe0 is the encoding for
2022 DW_OP_GNU_push_tls_address. The operand represents the offset at
2023 which the variable is within the thread local storage. */
2025 else if (data + 1 + addr_size < end
2026 && (data[0] == DW_OP_addr
2027 || (addr_size == 4 && data[0] == DW_OP_const4u)
2028 || (addr_size == 8 && data[0] == DW_OP_const8u))
2029 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
2030 && piece_end_p (data + 2 + addr_size, end))
2033 offset = extract_unsigned_integer (data + 1, addr_size,
2034 gdbarch_byte_order (gdbarch));
2036 fprintf_filtered (stream,
2037 _("a thread-local variable at offset 0x%s "
2038 "in the thread-local storage for `%s'"),
2039 phex_nz (offset, addr_size), objfile->name);
2041 data += 1 + addr_size + 1;
2043 else if (data[0] >= DW_OP_lit0
2044 && data[0] <= DW_OP_lit31
2046 && data[1] == DW_OP_stack_value)
2048 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
2055 /* Disassemble an expression, stopping at the end of a piece or at the
2056 end of the expression. Returns a pointer to the next unread byte
2057 in the input expression. If ALL is nonzero, then this function
2058 will keep going until it reaches the end of the expression. */
2060 static const gdb_byte *
2061 disassemble_dwarf_expression (struct ui_file *stream,
2062 struct gdbarch *arch, unsigned int addr_size,
2064 const gdb_byte *data, const gdb_byte *end,
2067 const gdb_byte *start = data;
2069 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
2073 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
2075 enum dwarf_location_atom op = *data++;
2080 name = dwarf_stack_op_name (op, 0);
2083 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
2084 op, (long) (data - start));
2085 fprintf_filtered (stream, " % 4ld: %s", (long) (data - start), name);
2090 ul = extract_unsigned_integer (data, addr_size,
2091 gdbarch_byte_order (arch));
2093 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
2097 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
2099 fprintf_filtered (stream, " %s", pulongest (ul));
2102 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
2104 fprintf_filtered (stream, " %s", plongest (l));
2107 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2109 fprintf_filtered (stream, " %s", pulongest (ul));
2112 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2114 fprintf_filtered (stream, " %s", plongest (l));
2117 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2119 fprintf_filtered (stream, " %s", pulongest (ul));
2122 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
2124 fprintf_filtered (stream, " %s", plongest (l));
2127 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
2129 fprintf_filtered (stream, " %s", pulongest (ul));
2132 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
2134 fprintf_filtered (stream, " %s", plongest (l));
2137 data = read_uleb128 (data, end, &ul);
2138 fprintf_filtered (stream, " %s", pulongest (ul));
2141 data = read_sleb128 (data, end, &l);
2142 fprintf_filtered (stream, " %s", plongest (l));
2177 fprintf_filtered (stream, " [$%s]",
2178 gdbarch_register_name (arch, op - DW_OP_reg0));
2182 data = read_uleb128 (data, end, &ul);
2183 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2184 gdbarch_register_name (arch, (int) ul));
2187 case DW_OP_implicit_value:
2188 data = read_uleb128 (data, end, &ul);
2190 fprintf_filtered (stream, " %s", pulongest (ul));
2225 data = read_sleb128 (data, end, &ul);
2226 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2227 gdbarch_register_name (arch, op - DW_OP_breg0));
2234 data = read_uleb128 (data, end, &ul);
2235 data = read_sleb128 (data, end, &offset);
2236 fprintf_filtered (stream, " register %s [$%s] offset %s",
2238 gdbarch_register_name (arch, (int) ul),
2239 pulongest (offset));
2244 data = read_sleb128 (data, end, &ul);
2245 fprintf_filtered (stream, " %s", pulongest (ul));
2248 case DW_OP_xderef_size:
2249 case DW_OP_deref_size:
2251 fprintf_filtered (stream, " %d", *data);
2255 case DW_OP_plus_uconst:
2256 data = read_uleb128 (data, end, &ul);
2257 fprintf_filtered (stream, " %s", pulongest (ul));
2261 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2263 fprintf_filtered (stream, " to %ld",
2264 (long) (data + l - start));
2268 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2270 fprintf_filtered (stream, " %ld",
2271 (long) (data + l - start));
2275 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2277 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
2281 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2283 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
2286 case DW_OP_call_ref:
2287 ul = extract_unsigned_integer (data, offset_size,
2288 gdbarch_byte_order (arch));
2289 data += offset_size;
2290 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
2294 data = read_uleb128 (data, end, &ul);
2295 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
2298 case DW_OP_bit_piece:
2302 data = read_uleb128 (data, end, &ul);
2303 data = read_uleb128 (data, end, &offset);
2304 fprintf_filtered (stream, " size %s offset %s (bits)",
2305 pulongest (ul), pulongest (offset));
2310 fprintf_filtered (stream, "\n");
2316 /* Describe a single location, which may in turn consist of multiple
2320 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
2321 struct ui_file *stream,
2322 const gdb_byte *data, int size,
2323 struct objfile *objfile, unsigned int addr_size,
2326 const gdb_byte *end = data + size;
2327 int first_piece = 1, bad = 0;
2331 const gdb_byte *here = data;
2332 int disassemble = 1;
2337 fprintf_filtered (stream, _(", and "));
2339 if (!dwarf2_always_disassemble)
2341 data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
2342 data, end, addr_size);
2343 /* If we printed anything, or if we have an empty piece,
2344 then don't disassemble. */
2346 || data[0] == DW_OP_piece
2347 || data[0] == DW_OP_bit_piece)
2351 data = disassemble_dwarf_expression (stream, get_objfile_arch (objfile),
2352 addr_size, offset_size, data, end,
2353 dwarf2_always_disassemble);
2357 int empty = data == here;
2360 fprintf_filtered (stream, " ");
2361 if (data[0] == DW_OP_piece)
2365 data = read_uleb128 (data + 1, end, &bytes);
2368 fprintf_filtered (stream, _("an empty %s-byte piece"),
2371 fprintf_filtered (stream, _(" [%s-byte piece]"),
2374 else if (data[0] == DW_OP_bit_piece)
2376 ULONGEST bits, offset;
2378 data = read_uleb128 (data + 1, end, &bits);
2379 data = read_uleb128 (data, end, &offset);
2382 fprintf_filtered (stream,
2383 _("an empty %s-bit piece"),
2386 fprintf_filtered (stream,
2387 _(" [%s-bit piece, offset %s bits]"),
2388 pulongest (bits), pulongest (offset));
2398 if (bad || data > end)
2399 error (_("Corrupted DWARF2 expression for \"%s\"."),
2400 SYMBOL_PRINT_NAME (symbol));
2403 /* Print a natural-language description of SYMBOL to STREAM. This
2404 version is for a symbol with a single location. */
2407 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
2408 struct ui_file *stream)
2410 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2411 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2412 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2413 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2415 locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
2416 objfile, addr_size, offset_size);
2419 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2420 any necessary bytecode in AX. */
2423 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2424 struct agent_expr *ax, struct axs_value *value)
2426 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2427 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2429 if (dlbaton->data == NULL || dlbaton->size == 0)
2430 value->optimized_out = 1;
2432 compile_dwarf_to_ax (ax, value, gdbarch, addr_size,
2433 dlbaton->data, dlbaton->data + dlbaton->size,
2437 /* The set of location functions used with the DWARF-2 expression
2439 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
2440 locexpr_read_variable,
2441 locexpr_read_needs_frame,
2442 locexpr_describe_location,
2443 locexpr_tracepoint_var_ref
2447 /* Wrapper functions for location lists. These generally find
2448 the appropriate location expression and call something above. */
2450 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2451 evaluator to calculate the location. */
2452 static struct value *
2453 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
2455 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2457 const gdb_byte *data;
2460 data = find_location_expression (dlbaton, &size,
2461 frame ? get_frame_address_in_block (frame)
2465 val = allocate_value (SYMBOL_TYPE (symbol));
2466 VALUE_LVAL (val) = not_lval;
2467 set_value_optimized_out (val, 1);
2470 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
2476 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
2478 loclist_read_needs_frame (struct symbol *symbol)
2480 /* If there's a location list, then assume we need to have a frame
2481 to choose the appropriate location expression. With tracking of
2482 global variables this is not necessarily true, but such tracking
2483 is disabled in GCC at the moment until we figure out how to
2489 /* Print a natural-language description of SYMBOL to STREAM. This
2490 version applies when there is a list of different locations, each
2491 with a specified address range. */
2494 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
2495 struct ui_file *stream)
2497 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2498 CORE_ADDR low, high;
2499 const gdb_byte *loc_ptr, *buf_end;
2500 int length, first = 1;
2501 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2502 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2503 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2504 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2505 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2506 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
2507 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2508 /* Adjust base_address for relocatable objects. */
2509 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
2510 CORE_ADDR base_address = dlbaton->base_address + base_offset;
2512 loc_ptr = dlbaton->data;
2513 buf_end = dlbaton->data + dlbaton->size;
2515 fprintf_filtered (stream, _("multi-location:\n"));
2517 /* Iterate through locations until we run out. */
2520 if (buf_end - loc_ptr < 2 * addr_size)
2521 error (_("Corrupted DWARF expression for symbol \"%s\"."),
2522 SYMBOL_PRINT_NAME (symbol));
2525 low = extract_signed_integer (loc_ptr, addr_size, byte_order);
2527 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2528 loc_ptr += addr_size;
2531 high = extract_signed_integer (loc_ptr, addr_size, byte_order);
2533 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2534 loc_ptr += addr_size;
2536 /* A base-address-selection entry. */
2537 if ((low & base_mask) == base_mask)
2539 base_address = high + base_offset;
2540 fprintf_filtered (stream, _(" Base address %s"),
2541 paddress (gdbarch, base_address));
2545 /* An end-of-list entry. */
2546 if (low == 0 && high == 0)
2549 /* Otherwise, a location expression entry. */
2550 low += base_address;
2551 high += base_address;
2553 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
2556 /* (It would improve readability to print only the minimum
2557 necessary digits of the second number of the range.) */
2558 fprintf_filtered (stream, _(" Range %s-%s: "),
2559 paddress (gdbarch, low), paddress (gdbarch, high));
2561 /* Now describe this particular location. */
2562 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
2563 objfile, addr_size, offset_size);
2565 fprintf_filtered (stream, "\n");
2571 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2572 any necessary bytecode in AX. */
2574 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2575 struct agent_expr *ax, struct axs_value *value)
2577 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2578 const gdb_byte *data;
2580 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2582 data = find_location_expression (dlbaton, &size, ax->scope);
2583 if (data == NULL || size == 0)
2584 value->optimized_out = 1;
2586 compile_dwarf_to_ax (ax, value, gdbarch, addr_size, data, data + size,
2590 /* The set of location functions used with the DWARF-2 expression
2591 evaluator and location lists. */
2592 const struct symbol_computed_ops dwarf2_loclist_funcs = {
2593 loclist_read_variable,
2594 loclist_read_needs_frame,
2595 loclist_describe_location,
2596 loclist_tracepoint_var_ref