1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
34 #include "exceptions.h"
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;
47 static void dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
48 const gdb_byte **start, size_t *length);
50 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
51 struct frame_info *frame,
54 struct dwarf2_per_cu_data *per_cu,
57 /* A function for dealing with location lists. Given a
58 symbol baton (BATON) and a pc value (PC), find the appropriate
59 location expression, set *LOCEXPR_LENGTH, and return a pointer
60 to the beginning of the expression. Returns NULL on failure.
62 For now, only return the first matching location expression; there
63 can be more than one in the list. */
66 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
67 size_t *locexpr_length, CORE_ADDR pc)
70 const gdb_byte *loc_ptr, *buf_end;
72 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
73 struct gdbarch *gdbarch = get_objfile_arch (objfile);
74 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
75 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
76 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
77 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
78 /* Adjust base_address for relocatable objects. */
79 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
80 CORE_ADDR base_address = baton->base_address + base_offset;
82 loc_ptr = baton->data;
83 buf_end = baton->data + baton->size;
87 if (buf_end - loc_ptr < 2 * addr_size)
88 error (_("dwarf2_find_location_expression: "
89 "Corrupted DWARF expression."));
92 low = extract_signed_integer (loc_ptr, addr_size, byte_order);
94 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
98 high = extract_signed_integer (loc_ptr, addr_size, byte_order);
100 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
101 loc_ptr += addr_size;
103 /* A base-address-selection entry. */
104 if ((low & base_mask) == base_mask)
106 base_address = high + base_offset;
110 /* An end-of-list entry. */
111 if (low == 0 && high == 0)
114 /* Otherwise, a location expression entry. */
116 high += base_address;
118 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
121 if (pc >= low && pc < high)
123 *locexpr_length = length;
131 /* This is the baton used when performing dwarf2 expression
133 struct dwarf_expr_baton
135 struct frame_info *frame;
136 struct dwarf2_per_cu_data *per_cu;
139 /* Helper functions for dwarf2_evaluate_loc_desc. */
141 /* Using the frame specified in BATON, return the value of register
142 REGNUM, treated as a pointer. */
144 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
146 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
147 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
151 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
152 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
153 regnum, debaton->frame);
157 /* Read memory at ADDR (length LEN) into BUF. */
160 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
162 read_memory (addr, buf, len);
165 /* Using the frame specified in BATON, find the location expression
166 describing the frame base. Return a pointer to it in START and
167 its length in LENGTH. */
169 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
171 /* FIXME: cagney/2003-03-26: This code should be using
172 get_frame_base_address(), and then implement a dwarf2 specific
174 struct symbol *framefunc;
175 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
177 /* Use block_linkage_function, which returns a real (not inlined)
178 function, instead of get_frame_function, which may return an
180 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
182 /* If we found a frame-relative symbol then it was certainly within
183 some function associated with a frame. If we can't find the frame,
184 something has gone wrong. */
185 gdb_assert (framefunc != NULL);
187 dwarf_expr_frame_base_1 (framefunc,
188 get_frame_address_in_block (debaton->frame),
193 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
194 const gdb_byte **start, size_t *length)
196 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
198 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
200 struct dwarf2_loclist_baton *symbaton;
202 symbaton = SYMBOL_LOCATION_BATON (framefunc);
203 *start = dwarf2_find_location_expression (symbaton, length, pc);
207 struct dwarf2_locexpr_baton *symbaton;
209 symbaton = SYMBOL_LOCATION_BATON (framefunc);
210 if (symbaton != NULL)
212 *length = symbaton->size;
213 *start = symbaton->data;
220 error (_("Could not find the frame base for \"%s\"."),
221 SYMBOL_NATURAL_NAME (framefunc));
224 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
225 the frame in BATON. */
228 dwarf_expr_frame_cfa (void *baton)
230 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
232 return dwarf2_frame_cfa (debaton->frame);
235 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
236 the frame in BATON. */
239 dwarf_expr_frame_pc (void *baton)
241 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
243 return get_frame_address_in_block (debaton->frame);
246 /* Using the objfile specified in BATON, find the address for the
247 current thread's thread-local storage with offset OFFSET. */
249 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
251 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
252 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
254 return target_translate_tls_address (objfile, offset);
257 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
258 current CU (as is PER_CU). State of the CTX is not affected by the
262 per_cu_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset,
263 struct dwarf2_per_cu_data *per_cu,
264 CORE_ADDR (*get_frame_pc) (void *baton),
267 struct dwarf2_locexpr_baton block;
268 struct cleanup *back_to;
270 block = dwarf2_fetch_die_location_block (die_offset, per_cu,
271 get_frame_pc, baton);
273 back_to = make_cleanup (xfree, (void *) block.data);
275 /* DW_OP_call_ref is currently not supported. */
276 gdb_assert (block.per_cu == per_cu);
278 dwarf_expr_eval (ctx, block.data, block.size);
280 do_cleanups (back_to);
283 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
286 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
288 struct dwarf_expr_baton *debaton = ctx->baton;
290 per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
291 ctx->funcs->get_frame_pc, ctx->baton);
294 /* Callback function for dwarf2_evaluate_loc_desc. */
297 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx, size_t die_offset)
299 struct dwarf_expr_baton *debaton = ctx->baton;
301 return dwarf2_get_die_type (die_offset, debaton->per_cu);
306 /* Reference count. */
309 /* The CU from which this closure's expression came. */
310 struct dwarf2_per_cu_data *per_cu;
312 /* The number of pieces used to describe this variable. */
315 /* The target address size, used only for DWARF_VALUE_STACK. */
318 /* The pieces themselves. */
319 struct dwarf_expr_piece *pieces;
322 /* Allocate a closure for a value formed from separately-described
325 static struct piece_closure *
326 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
327 int n_pieces, struct dwarf_expr_piece *pieces,
330 struct piece_closure *c = XZALLOC (struct piece_closure);
335 c->n_pieces = n_pieces;
336 c->addr_size = addr_size;
337 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
339 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
340 for (i = 0; i < n_pieces; ++i)
341 if (c->pieces[i].location == DWARF_VALUE_STACK)
342 value_incref (c->pieces[i].v.value);
347 /* The lowest-level function to extract bits from a byte buffer.
348 SOURCE is the buffer. It is updated if we read to the end of a
350 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
351 updated to reflect the number of bits actually read.
352 NBITS is the number of bits we want to read. It is updated to
353 reflect the number of bits actually read. This function may read
355 BITS_BIG_ENDIAN is taken directly from gdbarch.
356 This function returns the extracted bits. */
359 extract_bits_primitive (const gdb_byte **source,
360 unsigned int *source_offset_bits,
361 int *nbits, int bits_big_endian)
363 unsigned int avail, mask, datum;
365 gdb_assert (*source_offset_bits < 8);
367 avail = 8 - *source_offset_bits;
371 mask = (1 << avail) - 1;
374 datum >>= 8 - (*source_offset_bits + *nbits);
376 datum >>= *source_offset_bits;
380 *source_offset_bits += avail;
381 if (*source_offset_bits >= 8)
383 *source_offset_bits -= 8;
390 /* Extract some bits from a source buffer and move forward in the
393 SOURCE is the source buffer. It is updated as bytes are read.
394 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
396 NBITS is the number of bits to read.
397 BITS_BIG_ENDIAN is taken directly from gdbarch.
399 This function returns the bits that were read. */
402 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
403 int nbits, int bits_big_endian)
407 gdb_assert (nbits > 0 && nbits <= 8);
409 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
415 more = extract_bits_primitive (source, source_offset_bits, &nbits,
427 /* Write some bits into a buffer and move forward in the buffer.
429 DATUM is the bits to write. The low-order bits of DATUM are used.
430 DEST is the destination buffer. It is updated as bytes are
432 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
434 NBITS is the number of valid bits in DATUM.
435 BITS_BIG_ENDIAN is taken directly from gdbarch. */
438 insert_bits (unsigned int datum,
439 gdb_byte *dest, unsigned int dest_offset_bits,
440 int nbits, int bits_big_endian)
444 gdb_assert (dest_offset_bits + nbits <= 8);
446 mask = (1 << nbits) - 1;
449 datum <<= 8 - (dest_offset_bits + nbits);
450 mask <<= 8 - (dest_offset_bits + nbits);
454 datum <<= dest_offset_bits;
455 mask <<= dest_offset_bits;
458 gdb_assert ((datum & ~mask) == 0);
460 *dest = (*dest & ~mask) | datum;
463 /* Copy bits from a source to a destination.
465 DEST is where the bits should be written.
466 DEST_OFFSET_BITS is the bit offset into DEST.
467 SOURCE is the source of bits.
468 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
469 BIT_COUNT is the number of bits to copy.
470 BITS_BIG_ENDIAN is taken directly from gdbarch. */
473 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
474 const gdb_byte *source, unsigned int source_offset_bits,
475 unsigned int bit_count,
478 unsigned int dest_avail;
481 /* Reduce everything to byte-size pieces. */
482 dest += dest_offset_bits / 8;
483 dest_offset_bits %= 8;
484 source += source_offset_bits / 8;
485 source_offset_bits %= 8;
487 dest_avail = 8 - dest_offset_bits % 8;
489 /* See if we can fill the first destination byte. */
490 if (dest_avail < bit_count)
492 datum = extract_bits (&source, &source_offset_bits, dest_avail,
494 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
496 dest_offset_bits = 0;
497 bit_count -= dest_avail;
500 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
501 than 8 bits remaining. */
502 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
503 for (; bit_count >= 8; bit_count -= 8)
505 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
506 *dest++ = (gdb_byte) datum;
509 /* Finally, we may have a few leftover bits. */
510 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
513 datum = extract_bits (&source, &source_offset_bits, bit_count,
515 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
520 read_pieced_value (struct value *v)
524 ULONGEST bits_to_skip;
526 struct piece_closure *c
527 = (struct piece_closure *) value_computed_closure (v);
528 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
530 size_t buffer_size = 0;
532 struct cleanup *cleanup;
534 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
536 if (value_type (v) != value_enclosing_type (v))
537 internal_error (__FILE__, __LINE__,
538 _("Should not be able to create a lazy value with "
539 "an enclosing type"));
541 cleanup = make_cleanup (free_current_contents, &buffer);
543 contents = value_contents_raw (v);
544 bits_to_skip = 8 * value_offset (v);
545 if (value_bitsize (v))
547 bits_to_skip += value_bitpos (v);
548 type_len = value_bitsize (v);
551 type_len = 8 * TYPE_LENGTH (value_type (v));
553 for (i = 0; i < c->n_pieces && offset < type_len; i++)
555 struct dwarf_expr_piece *p = &c->pieces[i];
556 size_t this_size, this_size_bits;
557 long dest_offset_bits, source_offset_bits, source_offset;
558 const gdb_byte *intermediate_buffer;
560 /* Compute size, source, and destination offsets for copying, in
562 this_size_bits = p->size;
563 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
565 bits_to_skip -= this_size_bits;
568 if (this_size_bits > type_len - offset)
569 this_size_bits = type_len - offset;
570 if (bits_to_skip > 0)
572 dest_offset_bits = 0;
573 source_offset_bits = bits_to_skip;
574 this_size_bits -= bits_to_skip;
579 dest_offset_bits = offset;
580 source_offset_bits = 0;
583 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
584 source_offset = source_offset_bits / 8;
585 if (buffer_size < this_size)
587 buffer_size = this_size;
588 buffer = xrealloc (buffer, buffer_size);
590 intermediate_buffer = buffer;
592 /* Copy from the source to DEST_BUFFER. */
595 case DWARF_VALUE_REGISTER:
597 struct gdbarch *arch = get_frame_arch (frame);
598 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
599 int reg_offset = source_offset;
601 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
602 && this_size < register_size (arch, gdb_regnum))
604 /* Big-endian, and we want less than full size. */
605 reg_offset = register_size (arch, gdb_regnum) - this_size;
606 /* We want the lower-order THIS_SIZE_BITS of the bytes
607 we extract from the register. */
608 source_offset_bits += 8 * this_size - this_size_bits;
611 if (gdb_regnum != -1)
615 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
619 /* Just so garbage doesn't ever shine through. */
620 memset (buffer, 0, this_size);
623 set_value_optimized_out (v, 1);
625 mark_value_bytes_unavailable (v, offset, this_size);
630 error (_("Unable to access DWARF register number %s"),
631 paddress (arch, p->v.regno));
636 case DWARF_VALUE_MEMORY:
637 read_value_memory (v, offset,
638 p->v.mem.in_stack_memory,
639 p->v.mem.addr + source_offset,
643 case DWARF_VALUE_STACK:
645 size_t n = this_size;
647 if (n > c->addr_size - source_offset)
648 n = (c->addr_size >= source_offset
649 ? c->addr_size - source_offset
657 const gdb_byte *val_bytes = value_contents_all (p->v.value);
659 intermediate_buffer = val_bytes + source_offset;
664 case DWARF_VALUE_LITERAL:
666 size_t n = this_size;
668 if (n > p->v.literal.length - source_offset)
669 n = (p->v.literal.length >= source_offset
670 ? p->v.literal.length - source_offset
673 intermediate_buffer = p->v.literal.data + source_offset;
677 /* These bits show up as zeros -- but do not cause the value
678 to be considered optimized-out. */
679 case DWARF_VALUE_IMPLICIT_POINTER:
682 case DWARF_VALUE_OPTIMIZED_OUT:
683 set_value_optimized_out (v, 1);
687 internal_error (__FILE__, __LINE__, _("invalid location type"));
690 if (p->location != DWARF_VALUE_OPTIMIZED_OUT
691 && p->location != DWARF_VALUE_IMPLICIT_POINTER)
692 copy_bitwise (contents, dest_offset_bits,
693 intermediate_buffer, source_offset_bits % 8,
694 this_size_bits, bits_big_endian);
696 offset += this_size_bits;
699 do_cleanups (cleanup);
703 write_pieced_value (struct value *to, struct value *from)
707 ULONGEST bits_to_skip;
708 const gdb_byte *contents;
709 struct piece_closure *c
710 = (struct piece_closure *) value_computed_closure (to);
711 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
713 size_t buffer_size = 0;
715 struct cleanup *cleanup;
717 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
721 set_value_optimized_out (to, 1);
725 cleanup = make_cleanup (free_current_contents, &buffer);
727 contents = value_contents (from);
728 bits_to_skip = 8 * value_offset (to);
729 if (value_bitsize (to))
731 bits_to_skip += value_bitpos (to);
732 type_len = value_bitsize (to);
735 type_len = 8 * TYPE_LENGTH (value_type (to));
737 for (i = 0; i < c->n_pieces && offset < type_len; i++)
739 struct dwarf_expr_piece *p = &c->pieces[i];
740 size_t this_size_bits, this_size;
741 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
743 const gdb_byte *source_buffer;
745 this_size_bits = p->size;
746 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
748 bits_to_skip -= this_size_bits;
751 if (this_size_bits > type_len - offset)
752 this_size_bits = type_len - offset;
753 if (bits_to_skip > 0)
755 dest_offset_bits = bits_to_skip;
756 source_offset_bits = 0;
757 this_size_bits -= bits_to_skip;
762 dest_offset_bits = 0;
763 source_offset_bits = offset;
766 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
767 source_offset = source_offset_bits / 8;
768 dest_offset = dest_offset_bits / 8;
769 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
771 source_buffer = contents + source_offset;
776 if (buffer_size < this_size)
778 buffer_size = this_size;
779 buffer = xrealloc (buffer, buffer_size);
781 source_buffer = buffer;
787 case DWARF_VALUE_REGISTER:
789 struct gdbarch *arch = get_frame_arch (frame);
790 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
791 int reg_offset = dest_offset;
793 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
794 && this_size <= register_size (arch, gdb_regnum))
795 /* Big-endian, and we want less than full size. */
796 reg_offset = register_size (arch, gdb_regnum) - this_size;
798 if (gdb_regnum != -1)
804 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
809 error (_("Can't do read-modify-write to "
810 "update bitfield; containing word has been "
813 throw_error (NOT_AVAILABLE_ERROR,
814 _("Can't do read-modify-write to update "
815 "bitfield; containing word "
818 copy_bitwise (buffer, dest_offset_bits,
819 contents, source_offset_bits,
824 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
825 this_size, source_buffer);
829 error (_("Unable to write to DWARF register number %s"),
830 paddress (arch, p->v.regno));
834 case DWARF_VALUE_MEMORY:
837 /* Only the first and last bytes can possibly have any
839 read_memory (p->v.mem.addr + dest_offset, buffer, 1);
840 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
841 buffer + this_size - 1, 1);
842 copy_bitwise (buffer, dest_offset_bits,
843 contents, source_offset_bits,
848 write_memory (p->v.mem.addr + dest_offset,
849 source_buffer, this_size);
852 set_value_optimized_out (to, 1);
855 offset += this_size_bits;
858 do_cleanups (cleanup);
861 /* A helper function that checks bit validity in a pieced value.
862 CHECK_FOR indicates the kind of validity checking.
863 DWARF_VALUE_MEMORY means to check whether any bit is valid.
864 DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
866 DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
870 check_pieced_value_bits (const struct value *value, int bit_offset,
872 enum dwarf_value_location check_for)
874 struct piece_closure *c
875 = (struct piece_closure *) value_computed_closure (value);
877 int validity = (check_for == DWARF_VALUE_MEMORY
878 || check_for == DWARF_VALUE_IMPLICIT_POINTER);
880 bit_offset += 8 * value_offset (value);
881 if (value_bitsize (value))
882 bit_offset += value_bitpos (value);
884 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
886 struct dwarf_expr_piece *p = &c->pieces[i];
887 size_t this_size_bits = p->size;
891 if (bit_offset >= this_size_bits)
893 bit_offset -= this_size_bits;
897 bit_length -= this_size_bits - bit_offset;
901 bit_length -= this_size_bits;
903 if (check_for == DWARF_VALUE_IMPLICIT_POINTER)
905 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
908 else if (p->location == DWARF_VALUE_OPTIMIZED_OUT
909 || p->location == DWARF_VALUE_IMPLICIT_POINTER)
925 check_pieced_value_validity (const struct value *value, int bit_offset,
928 return check_pieced_value_bits (value, bit_offset, bit_length,
933 check_pieced_value_invalid (const struct value *value)
935 return check_pieced_value_bits (value, 0,
936 8 * TYPE_LENGTH (value_type (value)),
937 DWARF_VALUE_OPTIMIZED_OUT);
940 /* An implementation of an lval_funcs method to see whether a value is
941 a synthetic pointer. */
944 check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
947 return check_pieced_value_bits (value, bit_offset, bit_length,
948 DWARF_VALUE_IMPLICIT_POINTER);
951 /* A wrapper function for get_frame_address_in_block. */
954 get_frame_address_in_block_wrapper (void *baton)
956 return get_frame_address_in_block (baton);
959 /* An implementation of an lval_funcs method to indirect through a
960 pointer. This handles the synthetic pointer case when needed. */
962 static struct value *
963 indirect_pieced_value (struct value *value)
965 struct piece_closure *c
966 = (struct piece_closure *) value_computed_closure (value);
968 struct frame_info *frame;
969 struct dwarf2_locexpr_baton baton;
970 int i, bit_offset, bit_length;
971 struct dwarf_expr_piece *piece = NULL;
972 struct value *result;
974 struct cleanup *back_to;
976 type = check_typedef (value_type (value));
977 if (TYPE_CODE (type) != TYPE_CODE_PTR)
980 bit_length = 8 * TYPE_LENGTH (type);
981 bit_offset = 8 * value_offset (value);
982 if (value_bitsize (value))
983 bit_offset += value_bitpos (value);
985 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
987 struct dwarf_expr_piece *p = &c->pieces[i];
988 size_t this_size_bits = p->size;
992 if (bit_offset >= this_size_bits)
994 bit_offset -= this_size_bits;
998 bit_length -= this_size_bits - bit_offset;
1002 bit_length -= this_size_bits;
1004 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1007 if (bit_length != 0)
1008 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
1014 frame = get_selected_frame (_("No frame selected."));
1015 byte_offset = value_as_address (value);
1018 baton = dwarf2_fetch_die_location_block (piece->v.ptr.die, c->per_cu,
1019 get_frame_address_in_block_wrapper,
1022 back_to = make_cleanup (xfree, (void *) baton.data);
1024 result = dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
1025 baton.data, baton.size, baton.per_cu,
1028 do_cleanups (back_to);
1034 copy_pieced_value_closure (const struct value *v)
1036 struct piece_closure *c
1037 = (struct piece_closure *) value_computed_closure (v);
1044 free_pieced_value_closure (struct value *v)
1046 struct piece_closure *c
1047 = (struct piece_closure *) value_computed_closure (v);
1054 for (i = 0; i < c->n_pieces; ++i)
1055 if (c->pieces[i].location == DWARF_VALUE_STACK)
1056 value_free (c->pieces[i].v.value);
1063 /* Functions for accessing a variable described by DW_OP_piece. */
1064 static const struct lval_funcs pieced_value_funcs = {
1067 check_pieced_value_validity,
1068 check_pieced_value_invalid,
1069 indirect_pieced_value,
1070 check_pieced_synthetic_pointer,
1071 copy_pieced_value_closure,
1072 free_pieced_value_closure
1075 /* Helper function which throws an error if a synthetic pointer is
1079 invalid_synthetic_pointer (void)
1081 error (_("access outside bounds of object "
1082 "referenced via synthetic pointer"));
1085 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
1087 static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
1089 dwarf_expr_read_reg,
1090 dwarf_expr_read_mem,
1091 dwarf_expr_frame_base,
1092 dwarf_expr_frame_cfa,
1093 dwarf_expr_frame_pc,
1094 dwarf_expr_tls_address,
1095 dwarf_expr_dwarf_call,
1096 dwarf_expr_get_base_type
1099 /* Evaluate a location description, starting at DATA and with length
1100 SIZE, to find the current location of variable of TYPE in the
1101 context of FRAME. BYTE_OFFSET is applied after the contents are
1104 static struct value *
1105 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
1106 const gdb_byte *data, unsigned short size,
1107 struct dwarf2_per_cu_data *per_cu,
1108 LONGEST byte_offset)
1110 struct value *retval;
1111 struct dwarf_expr_baton baton;
1112 struct dwarf_expr_context *ctx;
1113 struct cleanup *old_chain, *value_chain;
1114 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1115 volatile struct gdb_exception ex;
1117 if (byte_offset < 0)
1118 invalid_synthetic_pointer ();
1121 return allocate_optimized_out_value (type);
1123 baton.frame = frame;
1124 baton.per_cu = per_cu;
1126 ctx = new_dwarf_expr_context ();
1127 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1128 value_chain = make_cleanup_value_free_to_mark (value_mark ());
1130 ctx->gdbarch = get_objfile_arch (objfile);
1131 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1132 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1133 ctx->baton = &baton;
1134 ctx->funcs = &dwarf_expr_ctx_funcs;
1136 TRY_CATCH (ex, RETURN_MASK_ERROR)
1138 dwarf_expr_eval (ctx, data, size);
1142 if (ex.error == NOT_AVAILABLE_ERROR)
1144 do_cleanups (old_chain);
1145 retval = allocate_value (type);
1146 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
1150 throw_exception (ex);
1153 if (ctx->num_pieces > 0)
1155 struct piece_closure *c;
1156 struct frame_id frame_id = get_frame_id (frame);
1157 ULONGEST bit_size = 0;
1160 for (i = 0; i < ctx->num_pieces; ++i)
1161 bit_size += ctx->pieces[i].size;
1162 if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
1163 invalid_synthetic_pointer ();
1165 c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
1167 /* We must clean up the value chain after creating the piece
1168 closure but before allocating the result. */
1169 do_cleanups (value_chain);
1170 retval = allocate_computed_value (type, &pieced_value_funcs, c);
1171 VALUE_FRAME_ID (retval) = frame_id;
1172 set_value_offset (retval, byte_offset);
1176 switch (ctx->location)
1178 case DWARF_VALUE_REGISTER:
1180 struct gdbarch *arch = get_frame_arch (frame);
1181 ULONGEST dwarf_regnum = value_as_long (dwarf_expr_fetch (ctx, 0));
1182 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
1184 if (byte_offset != 0)
1185 error (_("cannot use offset on synthetic pointer to register"));
1186 do_cleanups (value_chain);
1187 if (gdb_regnum != -1)
1188 retval = value_from_register (type, gdb_regnum, frame);
1190 error (_("Unable to access DWARF register number %s"),
1191 paddress (arch, dwarf_regnum));
1195 case DWARF_VALUE_MEMORY:
1197 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
1198 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
1200 do_cleanups (value_chain);
1201 retval = allocate_value_lazy (type);
1202 VALUE_LVAL (retval) = lval_memory;
1203 if (in_stack_memory)
1204 set_value_stack (retval, 1);
1205 set_value_address (retval, address + byte_offset);
1209 case DWARF_VALUE_STACK:
1211 struct value *value = dwarf_expr_fetch (ctx, 0);
1213 const gdb_byte *val_bytes;
1214 size_t n = TYPE_LENGTH (value_type (value));
1216 if (byte_offset + TYPE_LENGTH (type) > n)
1217 invalid_synthetic_pointer ();
1219 val_bytes = value_contents_all (value);
1220 val_bytes += byte_offset;
1223 /* Preserve VALUE because we are going to free values back
1224 to the mark, but we still need the value contents
1226 value_incref (value);
1227 do_cleanups (value_chain);
1228 make_cleanup_value_free (value);
1230 retval = allocate_value (type);
1231 contents = value_contents_raw (retval);
1232 if (n > TYPE_LENGTH (type))
1233 n = TYPE_LENGTH (type);
1234 memcpy (contents, val_bytes, n);
1238 case DWARF_VALUE_LITERAL:
1241 const bfd_byte *ldata;
1242 size_t n = ctx->len;
1244 if (byte_offset + TYPE_LENGTH (type) > n)
1245 invalid_synthetic_pointer ();
1247 do_cleanups (value_chain);
1248 retval = allocate_value (type);
1249 contents = value_contents_raw (retval);
1251 ldata = ctx->data + byte_offset;
1254 if (n > TYPE_LENGTH (type))
1255 n = TYPE_LENGTH (type);
1256 memcpy (contents, ldata, n);
1260 case DWARF_VALUE_OPTIMIZED_OUT:
1261 do_cleanups (value_chain);
1262 retval = allocate_optimized_out_value (type);
1265 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
1266 operation by execute_stack_op. */
1267 case DWARF_VALUE_IMPLICIT_POINTER:
1268 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
1269 it can only be encountered when making a piece. */
1271 internal_error (__FILE__, __LINE__, _("invalid location type"));
1275 set_value_initialized (retval, ctx->initialized);
1277 do_cleanups (old_chain);
1282 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
1283 passes 0 as the byte_offset. */
1286 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
1287 const gdb_byte *data, unsigned short size,
1288 struct dwarf2_per_cu_data *per_cu)
1290 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
1294 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
1296 struct needs_frame_baton
1299 struct dwarf2_per_cu_data *per_cu;
1302 /* Reads from registers do require a frame. */
1304 needs_frame_read_reg (void *baton, int regnum)
1306 struct needs_frame_baton *nf_baton = baton;
1308 nf_baton->needs_frame = 1;
1312 /* Reads from memory do not require a frame. */
1314 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
1316 memset (buf, 0, len);
1319 /* Frame-relative accesses do require a frame. */
1321 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
1323 static gdb_byte lit0 = DW_OP_lit0;
1324 struct needs_frame_baton *nf_baton = baton;
1329 nf_baton->needs_frame = 1;
1332 /* CFA accesses require a frame. */
1335 needs_frame_frame_cfa (void *baton)
1337 struct needs_frame_baton *nf_baton = baton;
1339 nf_baton->needs_frame = 1;
1343 /* Thread-local accesses do require a frame. */
1345 needs_frame_tls_address (void *baton, CORE_ADDR offset)
1347 struct needs_frame_baton *nf_baton = baton;
1349 nf_baton->needs_frame = 1;
1353 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
1356 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
1358 struct needs_frame_baton *nf_baton = ctx->baton;
1360 per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
1361 ctx->funcs->get_frame_pc, ctx->baton);
1364 /* Virtual method table for dwarf2_loc_desc_needs_frame below. */
1366 static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
1368 needs_frame_read_reg,
1369 needs_frame_read_mem,
1370 needs_frame_frame_base,
1371 needs_frame_frame_cfa,
1372 needs_frame_frame_cfa, /* get_frame_pc */
1373 needs_frame_tls_address,
1374 needs_frame_dwarf_call,
1375 NULL /* get_base_type */
1378 /* Return non-zero iff the location expression at DATA (length SIZE)
1379 requires a frame to evaluate. */
1382 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
1383 struct dwarf2_per_cu_data *per_cu)
1385 struct needs_frame_baton baton;
1386 struct dwarf_expr_context *ctx;
1388 struct cleanup *old_chain;
1389 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1391 baton.needs_frame = 0;
1392 baton.per_cu = per_cu;
1394 ctx = new_dwarf_expr_context ();
1395 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1396 make_cleanup_value_free_to_mark (value_mark ());
1398 ctx->gdbarch = get_objfile_arch (objfile);
1399 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1400 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1401 ctx->baton = &baton;
1402 ctx->funcs = &needs_frame_ctx_funcs;
1404 dwarf_expr_eval (ctx, data, size);
1406 in_reg = ctx->location == DWARF_VALUE_REGISTER;
1408 if (ctx->num_pieces > 0)
1412 /* If the location has several pieces, and any of them are in
1413 registers, then we will need a frame to fetch them from. */
1414 for (i = 0; i < ctx->num_pieces; i++)
1415 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
1419 do_cleanups (old_chain);
1421 return baton.needs_frame || in_reg;
1424 /* A helper function that throws an unimplemented error mentioning a
1425 given DWARF operator. */
1428 unimplemented (unsigned int op)
1430 const char *name = dwarf_stack_op_name (op);
1433 error (_("DWARF operator %s cannot be translated to an agent expression"),
1436 error (_("Unknown DWARF operator 0x%02x cannot be translated "
1437 "to an agent expression"),
1441 /* A helper function to convert a DWARF register to an arch register.
1442 ARCH is the architecture.
1443 DWARF_REG is the register.
1444 This will throw an exception if the DWARF register cannot be
1445 translated to an architecture register. */
1448 translate_register (struct gdbarch *arch, int dwarf_reg)
1450 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
1452 error (_("Unable to access DWARF register number %d"), dwarf_reg);
1456 /* A helper function that emits an access to memory. ARCH is the
1457 target architecture. EXPR is the expression which we are building.
1458 NBITS is the number of bits we want to read. This emits the
1459 opcodes needed to read the memory and then extract the desired
1463 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
1465 ULONGEST nbytes = (nbits + 7) / 8;
1467 gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
1470 ax_trace_quick (expr, nbytes);
1473 ax_simple (expr, aop_ref8);
1474 else if (nbits <= 16)
1475 ax_simple (expr, aop_ref16);
1476 else if (nbits <= 32)
1477 ax_simple (expr, aop_ref32);
1479 ax_simple (expr, aop_ref64);
1481 /* If we read exactly the number of bytes we wanted, we're done. */
1482 if (8 * nbytes == nbits)
1485 if (gdbarch_bits_big_endian (arch))
1487 /* On a bits-big-endian machine, we want the high-order
1489 ax_const_l (expr, 8 * nbytes - nbits);
1490 ax_simple (expr, aop_rsh_unsigned);
1494 /* On a bits-little-endian box, we want the low-order NBITS. */
1495 ax_zero_ext (expr, nbits);
1499 /* A helper function to return the frame's PC. */
1502 get_ax_pc (void *baton)
1504 struct agent_expr *expr = baton;
1509 /* Compile a DWARF location expression to an agent expression.
1511 EXPR is the agent expression we are building.
1512 LOC is the agent value we modify.
1513 ARCH is the architecture.
1514 ADDR_SIZE is the size of addresses, in bytes.
1515 OP_PTR is the start of the location expression.
1516 OP_END is one past the last byte of the location expression.
1518 This will throw an exception for various kinds of errors -- for
1519 example, if the expression cannot be compiled, or if the expression
1523 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
1524 struct gdbarch *arch, unsigned int addr_size,
1525 const gdb_byte *op_ptr, const gdb_byte *op_end,
1526 struct dwarf2_per_cu_data *per_cu)
1528 struct cleanup *cleanups;
1530 VEC(int) *dw_labels = NULL, *patches = NULL;
1531 const gdb_byte * const base = op_ptr;
1532 const gdb_byte *previous_piece = op_ptr;
1533 enum bfd_endian byte_order = gdbarch_byte_order (arch);
1534 ULONGEST bits_collected = 0;
1535 unsigned int addr_size_bits = 8 * addr_size;
1536 int bits_big_endian = gdbarch_bits_big_endian (arch);
1538 offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
1539 cleanups = make_cleanup (xfree, offsets);
1541 for (i = 0; i < op_end - op_ptr; ++i)
1544 make_cleanup (VEC_cleanup (int), &dw_labels);
1545 make_cleanup (VEC_cleanup (int), &patches);
1547 /* By default we are making an address. */
1548 loc->kind = axs_lvalue_memory;
1550 while (op_ptr < op_end)
1552 enum dwarf_location_atom op = *op_ptr;
1553 ULONGEST uoffset, reg;
1557 offsets[op_ptr - base] = expr->len;
1560 /* Our basic approach to code generation is to map DWARF
1561 operations directly to AX operations. However, there are
1564 First, DWARF works on address-sized units, but AX always uses
1565 LONGEST. For most operations we simply ignore this
1566 difference; instead we generate sign extensions as needed
1567 before division and comparison operations. It would be nice
1568 to omit the sign extensions, but there is no way to determine
1569 the size of the target's LONGEST. (This code uses the size
1570 of the host LONGEST in some cases -- that is a bug but it is
1573 Second, some DWARF operations cannot be translated to AX.
1574 For these we simply fail. See
1575 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
1610 ax_const_l (expr, op - DW_OP_lit0);
1614 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
1615 op_ptr += addr_size;
1616 /* Some versions of GCC emit DW_OP_addr before
1617 DW_OP_GNU_push_tls_address. In this case the value is an
1618 index, not an address. We don't support things like
1619 branching between the address and the TLS op. */
1620 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
1621 uoffset += dwarf2_per_cu_text_offset (per_cu);
1622 ax_const_l (expr, uoffset);
1626 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
1630 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
1634 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
1638 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
1642 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
1646 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
1650 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
1654 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
1658 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
1659 ax_const_l (expr, uoffset);
1662 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1663 ax_const_l (expr, offset);
1698 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1699 loc->u.reg = translate_register (arch, op - DW_OP_reg0);
1700 loc->kind = axs_lvalue_register;
1704 op_ptr = read_uleb128 (op_ptr, op_end, ®);
1705 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1706 loc->u.reg = translate_register (arch, reg);
1707 loc->kind = axs_lvalue_register;
1710 case DW_OP_implicit_value:
1714 op_ptr = read_uleb128 (op_ptr, op_end, &len);
1715 if (op_ptr + len > op_end)
1716 error (_("DW_OP_implicit_value: too few bytes available."));
1717 if (len > sizeof (ULONGEST))
1718 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
1721 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
1724 dwarf_expr_require_composition (op_ptr, op_end,
1725 "DW_OP_implicit_value");
1727 loc->kind = axs_rvalue;
1731 case DW_OP_stack_value:
1732 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
1733 loc->kind = axs_rvalue;
1768 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1769 i = translate_register (arch, op - DW_OP_breg0);
1773 ax_const_l (expr, offset);
1774 ax_simple (expr, aop_add);
1779 op_ptr = read_uleb128 (op_ptr, op_end, ®);
1780 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1781 i = translate_register (arch, reg);
1785 ax_const_l (expr, offset);
1786 ax_simple (expr, aop_add);
1792 const gdb_byte *datastart;
1794 unsigned int before_stack_len;
1796 struct symbol *framefunc;
1797 LONGEST base_offset = 0;
1799 b = block_for_pc (expr->scope);
1802 error (_("No block found for address"));
1804 framefunc = block_linkage_function (b);
1807 error (_("No function found for block"));
1809 dwarf_expr_frame_base_1 (framefunc, expr->scope,
1810 &datastart, &datalen);
1812 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1813 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
1814 datastart + datalen, per_cu);
1818 ax_const_l (expr, offset);
1819 ax_simple (expr, aop_add);
1822 loc->kind = axs_lvalue_memory;
1827 ax_simple (expr, aop_dup);
1831 ax_simple (expr, aop_pop);
1836 ax_pick (expr, offset);
1840 ax_simple (expr, aop_swap);
1848 ax_simple (expr, aop_rot);
1852 case DW_OP_deref_size:
1856 if (op == DW_OP_deref_size)
1864 ax_simple (expr, aop_ref8);
1867 ax_simple (expr, aop_ref16);
1870 ax_simple (expr, aop_ref32);
1873 ax_simple (expr, aop_ref64);
1876 /* Note that dwarf_stack_op_name will never return
1878 error (_("Unsupported size %d in %s"),
1879 size, dwarf_stack_op_name (op));
1885 /* Sign extend the operand. */
1886 ax_ext (expr, addr_size_bits);
1887 ax_simple (expr, aop_dup);
1888 ax_const_l (expr, 0);
1889 ax_simple (expr, aop_less_signed);
1890 ax_simple (expr, aop_log_not);
1891 i = ax_goto (expr, aop_if_goto);
1892 /* We have to emit 0 - X. */
1893 ax_const_l (expr, 0);
1894 ax_simple (expr, aop_swap);
1895 ax_simple (expr, aop_sub);
1896 ax_label (expr, i, expr->len);
1900 /* No need to sign extend here. */
1901 ax_const_l (expr, 0);
1902 ax_simple (expr, aop_swap);
1903 ax_simple (expr, aop_sub);
1907 /* Sign extend the operand. */
1908 ax_ext (expr, addr_size_bits);
1909 ax_simple (expr, aop_bit_not);
1912 case DW_OP_plus_uconst:
1913 op_ptr = read_uleb128 (op_ptr, op_end, ®);
1914 /* It would be really weird to emit `DW_OP_plus_uconst 0',
1915 but we micro-optimize anyhow. */
1918 ax_const_l (expr, reg);
1919 ax_simple (expr, aop_add);
1924 ax_simple (expr, aop_bit_and);
1928 /* Sign extend the operands. */
1929 ax_ext (expr, addr_size_bits);
1930 ax_simple (expr, aop_swap);
1931 ax_ext (expr, addr_size_bits);
1932 ax_simple (expr, aop_swap);
1933 ax_simple (expr, aop_div_signed);
1937 ax_simple (expr, aop_sub);
1941 ax_simple (expr, aop_rem_unsigned);
1945 ax_simple (expr, aop_mul);
1949 ax_simple (expr, aop_bit_or);
1953 ax_simple (expr, aop_add);
1957 ax_simple (expr, aop_lsh);
1961 ax_simple (expr, aop_rsh_unsigned);
1965 ax_simple (expr, aop_rsh_signed);
1969 ax_simple (expr, aop_bit_xor);
1973 /* Sign extend the operands. */
1974 ax_ext (expr, addr_size_bits);
1975 ax_simple (expr, aop_swap);
1976 ax_ext (expr, addr_size_bits);
1977 /* Note no swap here: A <= B is !(B < A). */
1978 ax_simple (expr, aop_less_signed);
1979 ax_simple (expr, aop_log_not);
1983 /* Sign extend the operands. */
1984 ax_ext (expr, addr_size_bits);
1985 ax_simple (expr, aop_swap);
1986 ax_ext (expr, addr_size_bits);
1987 ax_simple (expr, aop_swap);
1988 /* A >= B is !(A < B). */
1989 ax_simple (expr, aop_less_signed);
1990 ax_simple (expr, aop_log_not);
1994 /* Sign extend the operands. */
1995 ax_ext (expr, addr_size_bits);
1996 ax_simple (expr, aop_swap);
1997 ax_ext (expr, addr_size_bits);
1998 /* No need for a second swap here. */
1999 ax_simple (expr, aop_equal);
2003 /* Sign extend the operands. */
2004 ax_ext (expr, addr_size_bits);
2005 ax_simple (expr, aop_swap);
2006 ax_ext (expr, addr_size_bits);
2007 ax_simple (expr, aop_swap);
2008 ax_simple (expr, aop_less_signed);
2012 /* Sign extend the operands. */
2013 ax_ext (expr, addr_size_bits);
2014 ax_simple (expr, aop_swap);
2015 ax_ext (expr, addr_size_bits);
2016 /* Note no swap here: A > B is B < A. */
2017 ax_simple (expr, aop_less_signed);
2021 /* Sign extend the operands. */
2022 ax_ext (expr, addr_size_bits);
2023 ax_simple (expr, aop_swap);
2024 ax_ext (expr, addr_size_bits);
2025 /* No need for a swap here. */
2026 ax_simple (expr, aop_equal);
2027 ax_simple (expr, aop_log_not);
2030 case DW_OP_call_frame_cfa:
2031 dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
2032 loc->kind = axs_lvalue_memory;
2035 case DW_OP_GNU_push_tls_address:
2040 offset = extract_signed_integer (op_ptr, 2, byte_order);
2042 i = ax_goto (expr, aop_goto);
2043 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2044 VEC_safe_push (int, patches, i);
2048 offset = extract_signed_integer (op_ptr, 2, byte_order);
2050 /* Zero extend the operand. */
2051 ax_zero_ext (expr, addr_size_bits);
2052 i = ax_goto (expr, aop_if_goto);
2053 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2054 VEC_safe_push (int, patches, i);
2061 case DW_OP_bit_piece:
2063 ULONGEST size, offset;
2065 if (op_ptr - 1 == previous_piece)
2066 error (_("Cannot translate empty pieces to agent expressions"));
2067 previous_piece = op_ptr - 1;
2069 op_ptr = read_uleb128 (op_ptr, op_end, &size);
2070 if (op == DW_OP_piece)
2076 op_ptr = read_uleb128 (op_ptr, op_end, &offset);
2078 if (bits_collected + size > 8 * sizeof (LONGEST))
2079 error (_("Expression pieces exceed word size"));
2081 /* Access the bits. */
2084 case axs_lvalue_register:
2085 ax_reg (expr, loc->u.reg);
2088 case axs_lvalue_memory:
2089 /* Offset the pointer, if needed. */
2092 ax_const_l (expr, offset / 8);
2093 ax_simple (expr, aop_add);
2096 access_memory (arch, expr, size);
2100 /* For a bits-big-endian target, shift up what we already
2101 have. For a bits-little-endian target, shift up the
2102 new data. Note that there is a potential bug here if
2103 the DWARF expression leaves multiple values on the
2105 if (bits_collected > 0)
2107 if (bits_big_endian)
2109 ax_simple (expr, aop_swap);
2110 ax_const_l (expr, size);
2111 ax_simple (expr, aop_lsh);
2112 /* We don't need a second swap here, because
2113 aop_bit_or is symmetric. */
2117 ax_const_l (expr, size);
2118 ax_simple (expr, aop_lsh);
2120 ax_simple (expr, aop_bit_or);
2123 bits_collected += size;
2124 loc->kind = axs_rvalue;
2128 case DW_OP_GNU_uninit:
2134 struct dwarf2_locexpr_baton block;
2135 int size = (op == DW_OP_call2 ? 2 : 4);
2136 struct cleanup *back_to;
2138 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
2141 block = dwarf2_fetch_die_location_block (uoffset, per_cu,
2143 back_to = make_cleanup (xfree, (void *) block.data);
2145 /* DW_OP_call_ref is currently not supported. */
2146 gdb_assert (block.per_cu == per_cu);
2148 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
2149 block.data, block.data + block.size,
2152 do_cleanups (back_to);
2156 case DW_OP_call_ref:
2164 /* Patch all the branches we emitted. */
2165 for (i = 0; i < VEC_length (int, patches); ++i)
2167 int targ = offsets[VEC_index (int, dw_labels, i)];
2169 internal_error (__FILE__, __LINE__, _("invalid label"));
2170 ax_label (expr, VEC_index (int, patches, i), targ);
2173 do_cleanups (cleanups);
2177 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2178 evaluator to calculate the location. */
2179 static struct value *
2180 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
2182 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2185 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
2186 dlbaton->size, dlbaton->per_cu);
2191 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
2193 locexpr_read_needs_frame (struct symbol *symbol)
2195 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2197 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
2201 /* Return true if DATA points to the end of a piece. END is one past
2202 the last byte in the expression. */
2205 piece_end_p (const gdb_byte *data, const gdb_byte *end)
2207 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
2210 /* Helper for locexpr_describe_location_piece that finds the name of a
2214 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
2218 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
2219 return gdbarch_register_name (gdbarch, regnum);
2222 /* Nicely describe a single piece of a location, returning an updated
2223 position in the bytecode sequence. This function cannot recognize
2224 all locations; if a location is not recognized, it simply returns
2227 static const gdb_byte *
2228 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
2229 CORE_ADDR addr, struct objfile *objfile,
2230 const gdb_byte *data, const gdb_byte *end,
2231 unsigned int addr_size)
2233 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2235 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
2237 fprintf_filtered (stream, _("a variable in $%s"),
2238 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
2241 else if (data[0] == DW_OP_regx)
2245 data = read_uleb128 (data + 1, end, ®);
2246 fprintf_filtered (stream, _("a variable in $%s"),
2247 locexpr_regname (gdbarch, reg));
2249 else if (data[0] == DW_OP_fbreg)
2252 struct symbol *framefunc;
2254 LONGEST frame_offset;
2255 const gdb_byte *base_data, *new_data, *save_data = data;
2257 LONGEST base_offset = 0;
2259 new_data = read_sleb128 (data + 1, end, &frame_offset);
2260 if (!piece_end_p (new_data, end))
2264 b = block_for_pc (addr);
2267 error (_("No block found for address for symbol \"%s\"."),
2268 SYMBOL_PRINT_NAME (symbol));
2270 framefunc = block_linkage_function (b);
2273 error (_("No function found for block for symbol \"%s\"."),
2274 SYMBOL_PRINT_NAME (symbol));
2276 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
2278 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
2280 const gdb_byte *buf_end;
2282 frame_reg = base_data[0] - DW_OP_breg0;
2283 buf_end = read_sleb128 (base_data + 1,
2284 base_data + base_size, &base_offset);
2285 if (buf_end != base_data + base_size)
2286 error (_("Unexpected opcode after "
2287 "DW_OP_breg%u for symbol \"%s\"."),
2288 frame_reg, SYMBOL_PRINT_NAME (symbol));
2290 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
2292 /* The frame base is just the register, with no offset. */
2293 frame_reg = base_data[0] - DW_OP_reg0;
2298 /* We don't know what to do with the frame base expression,
2299 so we can't trace this variable; give up. */
2303 fprintf_filtered (stream,
2304 _("a variable at frame base reg $%s offset %s+%s"),
2305 locexpr_regname (gdbarch, frame_reg),
2306 plongest (base_offset), plongest (frame_offset));
2308 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
2309 && piece_end_p (data, end))
2313 data = read_sleb128 (data + 1, end, &offset);
2315 fprintf_filtered (stream,
2316 _("a variable at offset %s from base reg $%s"),
2318 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
2321 /* The location expression for a TLS variable looks like this (on a
2324 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
2325 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
2327 0x3 is the encoding for DW_OP_addr, which has an operand as long
2328 as the size of an address on the target machine (here is 8
2329 bytes). Note that more recent version of GCC emit DW_OP_const4u
2330 or DW_OP_const8u, depending on address size, rather than
2331 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
2332 The operand represents the offset at which the variable is within
2333 the thread local storage. */
2335 else if (data + 1 + addr_size < end
2336 && (data[0] == DW_OP_addr
2337 || (addr_size == 4 && data[0] == DW_OP_const4u)
2338 || (addr_size == 8 && data[0] == DW_OP_const8u))
2339 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
2340 && piece_end_p (data + 2 + addr_size, end))
2343 offset = extract_unsigned_integer (data + 1, addr_size,
2344 gdbarch_byte_order (gdbarch));
2346 fprintf_filtered (stream,
2347 _("a thread-local variable at offset 0x%s "
2348 "in the thread-local storage for `%s'"),
2349 phex_nz (offset, addr_size), objfile->name);
2351 data += 1 + addr_size + 1;
2353 else if (data[0] >= DW_OP_lit0
2354 && data[0] <= DW_OP_lit31
2356 && data[1] == DW_OP_stack_value)
2358 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
2365 /* Disassemble an expression, stopping at the end of a piece or at the
2366 end of the expression. Returns a pointer to the next unread byte
2367 in the input expression. If ALL is nonzero, then this function
2368 will keep going until it reaches the end of the expression. */
2370 static const gdb_byte *
2371 disassemble_dwarf_expression (struct ui_file *stream,
2372 struct gdbarch *arch, unsigned int addr_size,
2374 const gdb_byte *data, const gdb_byte *end,
2376 struct dwarf2_per_cu_data *per_cu)
2378 const gdb_byte *start = data;
2380 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
2384 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
2386 enum dwarf_location_atom op = *data++;
2391 name = dwarf_stack_op_name (op);
2394 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
2395 op, (long) (data - 1 - start));
2396 fprintf_filtered (stream, " % 4ld: %s", (long) (data - 1 - start), name);
2401 ul = extract_unsigned_integer (data, addr_size,
2402 gdbarch_byte_order (arch));
2404 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
2408 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
2410 fprintf_filtered (stream, " %s", pulongest (ul));
2413 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
2415 fprintf_filtered (stream, " %s", plongest (l));
2418 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2420 fprintf_filtered (stream, " %s", pulongest (ul));
2423 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2425 fprintf_filtered (stream, " %s", plongest (l));
2428 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2430 fprintf_filtered (stream, " %s", pulongest (ul));
2433 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
2435 fprintf_filtered (stream, " %s", plongest (l));
2438 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
2440 fprintf_filtered (stream, " %s", pulongest (ul));
2443 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
2445 fprintf_filtered (stream, " %s", plongest (l));
2448 data = read_uleb128 (data, end, &ul);
2449 fprintf_filtered (stream, " %s", pulongest (ul));
2452 data = read_sleb128 (data, end, &l);
2453 fprintf_filtered (stream, " %s", plongest (l));
2488 fprintf_filtered (stream, " [$%s]",
2489 locexpr_regname (arch, op - DW_OP_reg0));
2493 data = read_uleb128 (data, end, &ul);
2494 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2495 locexpr_regname (arch, (int) ul));
2498 case DW_OP_implicit_value:
2499 data = read_uleb128 (data, end, &ul);
2501 fprintf_filtered (stream, " %s", pulongest (ul));
2536 data = read_sleb128 (data, end, &l);
2537 fprintf_filtered (stream, " %s [$%s]", plongest (l),
2538 locexpr_regname (arch, op - DW_OP_breg0));
2542 data = read_uleb128 (data, end, &ul);
2543 data = read_sleb128 (data, end, &l);
2544 fprintf_filtered (stream, " register %s [$%s] offset %s",
2546 locexpr_regname (arch, (int) ul),
2551 data = read_sleb128 (data, end, &l);
2552 fprintf_filtered (stream, " %s", plongest (l));
2555 case DW_OP_xderef_size:
2556 case DW_OP_deref_size:
2558 fprintf_filtered (stream, " %d", *data);
2562 case DW_OP_plus_uconst:
2563 data = read_uleb128 (data, end, &ul);
2564 fprintf_filtered (stream, " %s", pulongest (ul));
2568 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2570 fprintf_filtered (stream, " to %ld",
2571 (long) (data + l - start));
2575 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2577 fprintf_filtered (stream, " %ld",
2578 (long) (data + l - start));
2582 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2584 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
2588 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2590 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
2593 case DW_OP_call_ref:
2594 ul = extract_unsigned_integer (data, offset_size,
2595 gdbarch_byte_order (arch));
2596 data += offset_size;
2597 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
2601 data = read_uleb128 (data, end, &ul);
2602 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
2605 case DW_OP_bit_piece:
2609 data = read_uleb128 (data, end, &ul);
2610 data = read_uleb128 (data, end, &offset);
2611 fprintf_filtered (stream, " size %s offset %s (bits)",
2612 pulongest (ul), pulongest (offset));
2616 case DW_OP_GNU_implicit_pointer:
2618 ul = extract_unsigned_integer (data, offset_size,
2619 gdbarch_byte_order (arch));
2620 data += offset_size;
2622 data = read_sleb128 (data, end, &l);
2624 fprintf_filtered (stream, " DIE %s offset %s",
2625 phex_nz (ul, offset_size),
2630 case DW_OP_GNU_deref_type:
2632 int addr_size = *data++;
2636 data = read_uleb128 (data, end, &offset);
2637 type = dwarf2_get_die_type (offset, per_cu);
2638 fprintf_filtered (stream, "<");
2639 type_print (type, "", stream, -1);
2640 fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset, 0),
2645 case DW_OP_GNU_const_type:
2650 data = read_uleb128 (data, end, &type_die);
2651 type = dwarf2_get_die_type (type_die, per_cu);
2652 fprintf_filtered (stream, "<");
2653 type_print (type, "", stream, -1);
2654 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die, 0));
2658 case DW_OP_GNU_regval_type:
2660 ULONGEST type_die, reg;
2663 data = read_uleb128 (data, end, ®);
2664 data = read_uleb128 (data, end, &type_die);
2666 type = dwarf2_get_die_type (type_die, per_cu);
2667 fprintf_filtered (stream, "<");
2668 type_print (type, "", stream, -1);
2669 fprintf_filtered (stream, " [0x%s]> [$%s]", phex_nz (type_die, 0),
2670 locexpr_regname (arch, reg));
2674 case DW_OP_GNU_convert:
2675 case DW_OP_GNU_reinterpret:
2679 data = read_uleb128 (data, end, &type_die);
2682 fprintf_filtered (stream, "<0>");
2687 type = dwarf2_get_die_type (type_die, per_cu);
2688 fprintf_filtered (stream, "<");
2689 type_print (type, "", stream, -1);
2690 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die, 0));
2696 fprintf_filtered (stream, "\n");
2702 /* Describe a single location, which may in turn consist of multiple
2706 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
2707 struct ui_file *stream,
2708 const gdb_byte *data, int size,
2709 struct objfile *objfile, unsigned int addr_size,
2710 int offset_size, struct dwarf2_per_cu_data *per_cu)
2712 const gdb_byte *end = data + size;
2713 int first_piece = 1, bad = 0;
2717 const gdb_byte *here = data;
2718 int disassemble = 1;
2723 fprintf_filtered (stream, _(", and "));
2725 if (!dwarf2_always_disassemble)
2727 data = locexpr_describe_location_piece (symbol, stream,
2729 data, end, addr_size);
2730 /* If we printed anything, or if we have an empty piece,
2731 then don't disassemble. */
2733 || data[0] == DW_OP_piece
2734 || data[0] == DW_OP_bit_piece)
2738 data = disassemble_dwarf_expression (stream,
2739 get_objfile_arch (objfile),
2740 addr_size, offset_size, data, end,
2741 dwarf2_always_disassemble,
2746 int empty = data == here;
2749 fprintf_filtered (stream, " ");
2750 if (data[0] == DW_OP_piece)
2754 data = read_uleb128 (data + 1, end, &bytes);
2757 fprintf_filtered (stream, _("an empty %s-byte piece"),
2760 fprintf_filtered (stream, _(" [%s-byte piece]"),
2763 else if (data[0] == DW_OP_bit_piece)
2765 ULONGEST bits, offset;
2767 data = read_uleb128 (data + 1, end, &bits);
2768 data = read_uleb128 (data, end, &offset);
2771 fprintf_filtered (stream,
2772 _("an empty %s-bit piece"),
2775 fprintf_filtered (stream,
2776 _(" [%s-bit piece, offset %s bits]"),
2777 pulongest (bits), pulongest (offset));
2787 if (bad || data > end)
2788 error (_("Corrupted DWARF2 expression for \"%s\"."),
2789 SYMBOL_PRINT_NAME (symbol));
2792 /* Print a natural-language description of SYMBOL to STREAM. This
2793 version is for a symbol with a single location. */
2796 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
2797 struct ui_file *stream)
2799 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2800 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2801 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2802 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2804 locexpr_describe_location_1 (symbol, addr, stream,
2805 dlbaton->data, dlbaton->size,
2806 objfile, addr_size, offset_size,
2810 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2811 any necessary bytecode in AX. */
2814 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2815 struct agent_expr *ax, struct axs_value *value)
2817 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2818 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2820 if (dlbaton->data == NULL || dlbaton->size == 0)
2821 value->optimized_out = 1;
2823 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
2824 dlbaton->data, dlbaton->data + dlbaton->size,
2828 /* The set of location functions used with the DWARF-2 expression
2830 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
2831 locexpr_read_variable,
2832 locexpr_read_needs_frame,
2833 locexpr_describe_location,
2834 locexpr_tracepoint_var_ref
2838 /* Wrapper functions for location lists. These generally find
2839 the appropriate location expression and call something above. */
2841 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2842 evaluator to calculate the location. */
2843 static struct value *
2844 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
2846 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2848 const gdb_byte *data;
2850 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
2852 data = dwarf2_find_location_expression (dlbaton, &size, pc);
2854 val = allocate_optimized_out_value (SYMBOL_TYPE (symbol));
2856 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
2862 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
2864 loclist_read_needs_frame (struct symbol *symbol)
2866 /* If there's a location list, then assume we need to have a frame
2867 to choose the appropriate location expression. With tracking of
2868 global variables this is not necessarily true, but such tracking
2869 is disabled in GCC at the moment until we figure out how to
2875 /* Print a natural-language description of SYMBOL to STREAM. This
2876 version applies when there is a list of different locations, each
2877 with a specified address range. */
2880 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
2881 struct ui_file *stream)
2883 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2884 CORE_ADDR low, high;
2885 const gdb_byte *loc_ptr, *buf_end;
2886 int length, first = 1;
2887 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2888 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2889 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2890 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2891 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2892 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
2893 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2894 /* Adjust base_address for relocatable objects. */
2895 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
2896 CORE_ADDR base_address = dlbaton->base_address + base_offset;
2898 loc_ptr = dlbaton->data;
2899 buf_end = dlbaton->data + dlbaton->size;
2901 fprintf_filtered (stream, _("multi-location:\n"));
2903 /* Iterate through locations until we run out. */
2906 if (buf_end - loc_ptr < 2 * addr_size)
2907 error (_("Corrupted DWARF expression for symbol \"%s\"."),
2908 SYMBOL_PRINT_NAME (symbol));
2911 low = extract_signed_integer (loc_ptr, addr_size, byte_order);
2913 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2914 loc_ptr += addr_size;
2917 high = extract_signed_integer (loc_ptr, addr_size, byte_order);
2919 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2920 loc_ptr += addr_size;
2922 /* A base-address-selection entry. */
2923 if ((low & base_mask) == base_mask)
2925 base_address = high + base_offset;
2926 fprintf_filtered (stream, _(" Base address %s"),
2927 paddress (gdbarch, base_address));
2931 /* An end-of-list entry. */
2932 if (low == 0 && high == 0)
2935 /* Otherwise, a location expression entry. */
2936 low += base_address;
2937 high += base_address;
2939 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
2942 /* (It would improve readability to print only the minimum
2943 necessary digits of the second number of the range.) */
2944 fprintf_filtered (stream, _(" Range %s-%s: "),
2945 paddress (gdbarch, low), paddress (gdbarch, high));
2947 /* Now describe this particular location. */
2948 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
2949 objfile, addr_size, offset_size,
2952 fprintf_filtered (stream, "\n");
2958 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2959 any necessary bytecode in AX. */
2961 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2962 struct agent_expr *ax, struct axs_value *value)
2964 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2965 const gdb_byte *data;
2967 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2969 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
2970 if (data == NULL || size == 0)
2971 value->optimized_out = 1;
2973 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
2977 /* The set of location functions used with the DWARF-2 expression
2978 evaluator and location lists. */
2979 const struct symbol_computed_ops dwarf2_loclist_funcs = {
2980 loclist_read_variable,
2981 loclist_read_needs_frame,
2982 loclist_describe_location,
2983 loclist_tracepoint_var_ref