*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5
6 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7
8 This file is part of GDB.
9
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.
14
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.
19
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/>. */
22
23 #include "defs.h"
24 #include "ui-out.h"
25 #include "value.h"
26 #include "frame.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "inferior.h"
30 #include "ax.h"
31 #include "ax-gdb.h"
32 #include "regcache.h"
33 #include "objfiles.h"
34 #include "exceptions.h"
35 #include "block.h"
36
37 #include "dwarf2.h"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
41
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
44
45 extern int dwarf2_always_disassemble;
46
47 static void
48 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
49 const gdb_byte **start, size_t *length);
50
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.
55
56 For now, only return the first matching location expression; there
57 can be more than one in the list. */
58
59 static const gdb_byte *
60 find_location_expression (struct dwarf2_loclist_baton *baton,
61 size_t *locexpr_length, CORE_ADDR pc)
62 {
63 CORE_ADDR low, high;
64 const gdb_byte *loc_ptr, *buf_end;
65 int length;
66 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
67 struct gdbarch *gdbarch = get_objfile_arch (objfile);
68 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
69 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
70 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
71 /* Adjust base_address for relocatable objects. */
72 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
73 SECT_OFF_TEXT (objfile));
74 CORE_ADDR base_address = baton->base_address + base_offset;
75
76 loc_ptr = baton->data;
77 buf_end = baton->data + baton->size;
78
79 while (1)
80 {
81 if (buf_end - loc_ptr < 2 * addr_size)
82 error (_("find_location_expression: Corrupted DWARF expression."));
83
84 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
85 loc_ptr += addr_size;
86
87 /* A base-address-selection entry. */
88 if (low == base_mask)
89 {
90 base_address = dwarf2_read_address (gdbarch,
91 loc_ptr, buf_end, addr_size);
92 loc_ptr += addr_size;
93 continue;
94 }
95
96 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
97 loc_ptr += addr_size;
98
99 /* An end-of-list entry. */
100 if (low == 0 && high == 0)
101 return NULL;
102
103 /* Otherwise, a location expression entry. */
104 low += base_address;
105 high += base_address;
106
107 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
108 loc_ptr += 2;
109
110 if (pc >= low && pc < high)
111 {
112 *locexpr_length = length;
113 return loc_ptr;
114 }
115
116 loc_ptr += length;
117 }
118 }
119
120 /* This is the baton used when performing dwarf2 expression
121 evaluation. */
122 struct dwarf_expr_baton
123 {
124 struct frame_info *frame;
125 struct dwarf2_per_cu_data *per_cu;
126 };
127
128 /* Helper functions for dwarf2_evaluate_loc_desc. */
129
130 /* Using the frame specified in BATON, return the value of register
131 REGNUM, treated as a pointer. */
132 static CORE_ADDR
133 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
134 {
135 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
136 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
137 CORE_ADDR result;
138 int regnum;
139
140 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
141 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
142 regnum, debaton->frame);
143 return result;
144 }
145
146 /* Read memory at ADDR (length LEN) into BUF. */
147
148 static void
149 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
150 {
151 read_memory (addr, buf, len);
152 }
153
154 /* Using the frame specified in BATON, find the location expression
155 describing the frame base. Return a pointer to it in START and
156 its length in LENGTH. */
157 static void
158 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
159 {
160 /* FIXME: cagney/2003-03-26: This code should be using
161 get_frame_base_address(), and then implement a dwarf2 specific
162 this_base method. */
163 struct symbol *framefunc;
164 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
165
166 /* Use block_linkage_function, which returns a real (not inlined)
167 function, instead of get_frame_function, which may return an
168 inlined function. */
169 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
170
171 /* If we found a frame-relative symbol then it was certainly within
172 some function associated with a frame. If we can't find the frame,
173 something has gone wrong. */
174 gdb_assert (framefunc != NULL);
175
176 dwarf_expr_frame_base_1 (framefunc,
177 get_frame_address_in_block (debaton->frame),
178 start, length);
179 }
180
181 static void
182 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
183 const gdb_byte **start, size_t *length)
184 {
185 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
186 *start = NULL;
187 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
188 {
189 struct dwarf2_loclist_baton *symbaton;
190
191 symbaton = SYMBOL_LOCATION_BATON (framefunc);
192 *start = find_location_expression (symbaton, length, pc);
193 }
194 else
195 {
196 struct dwarf2_locexpr_baton *symbaton;
197
198 symbaton = SYMBOL_LOCATION_BATON (framefunc);
199 if (symbaton != NULL)
200 {
201 *length = symbaton->size;
202 *start = symbaton->data;
203 }
204 else
205 *start = NULL;
206 }
207
208 if (*start == NULL)
209 error (_("Could not find the frame base for \"%s\"."),
210 SYMBOL_NATURAL_NAME (framefunc));
211 }
212
213 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
214 the frame in BATON. */
215
216 static CORE_ADDR
217 dwarf_expr_frame_cfa (void *baton)
218 {
219 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
220
221 return dwarf2_frame_cfa (debaton->frame);
222 }
223
224 /* Using the objfile specified in BATON, find the address for the
225 current thread's thread-local storage with offset OFFSET. */
226 static CORE_ADDR
227 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
228 {
229 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
230 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
231
232 return target_translate_tls_address (objfile, offset);
233 }
234
235 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in current CU
236 (as is PER_CU). State of the CTX is not affected by the call and return. */
237
238 static void
239 per_cu_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset,
240 struct dwarf2_per_cu_data *per_cu)
241 {
242 struct dwarf2_locexpr_baton block;
243
244 block = dwarf2_fetch_die_location_block (die_offset, per_cu);
245
246 /* DW_OP_call_ref is currently not supported. */
247 gdb_assert (block.per_cu == per_cu);
248
249 dwarf_expr_eval (ctx, block.data, block.size);
250 }
251
252 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
253
254 static void
255 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
256 {
257 struct dwarf_expr_baton *debaton = ctx->baton;
258
259 return per_cu_dwarf_call (ctx, die_offset, debaton->per_cu);
260 }
261
262 struct piece_closure
263 {
264 /* Reference count. */
265 int refc;
266
267 /* The number of pieces used to describe this variable. */
268 int n_pieces;
269
270 /* The target address size, used only for DWARF_VALUE_STACK. */
271 int addr_size;
272
273 /* The pieces themselves. */
274 struct dwarf_expr_piece *pieces;
275 };
276
277 /* Allocate a closure for a value formed from separately-described
278 PIECES. */
279
280 static struct piece_closure *
281 allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
282 int addr_size)
283 {
284 struct piece_closure *c = XZALLOC (struct piece_closure);
285
286 c->refc = 1;
287 c->n_pieces = n_pieces;
288 c->addr_size = addr_size;
289 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
290
291 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
292
293 return c;
294 }
295
296 /* The lowest-level function to extract bits from a byte buffer.
297 SOURCE is the buffer. It is updated if we read to the end of a
298 byte.
299 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
300 updated to reflect the number of bits actually read.
301 NBITS is the number of bits we want to read. It is updated to
302 reflect the number of bits actually read. This function may read
303 fewer bits.
304 BITS_BIG_ENDIAN is taken directly from gdbarch.
305 This function returns the extracted bits. */
306
307 static unsigned int
308 extract_bits_primitive (const gdb_byte **source,
309 unsigned int *source_offset_bits,
310 int *nbits, int bits_big_endian)
311 {
312 unsigned int avail, mask, datum;
313
314 gdb_assert (*source_offset_bits < 8);
315
316 avail = 8 - *source_offset_bits;
317 if (avail > *nbits)
318 avail = *nbits;
319
320 mask = (1 << avail) - 1;
321 datum = **source;
322 if (bits_big_endian)
323 datum >>= 8 - (*source_offset_bits + *nbits);
324 else
325 datum >>= *source_offset_bits;
326 datum &= mask;
327
328 *nbits -= avail;
329 *source_offset_bits += avail;
330 if (*source_offset_bits >= 8)
331 {
332 *source_offset_bits -= 8;
333 ++*source;
334 }
335
336 return datum;
337 }
338
339 /* Extract some bits from a source buffer and move forward in the
340 buffer.
341
342 SOURCE is the source buffer. It is updated as bytes are read.
343 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
344 bits are read.
345 NBITS is the number of bits to read.
346 BITS_BIG_ENDIAN is taken directly from gdbarch.
347
348 This function returns the bits that were read. */
349
350 static unsigned int
351 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
352 int nbits, int bits_big_endian)
353 {
354 unsigned int datum;
355
356 gdb_assert (nbits > 0 && nbits <= 8);
357
358 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
359 bits_big_endian);
360 if (nbits > 0)
361 {
362 unsigned int more;
363
364 more = extract_bits_primitive (source, source_offset_bits, &nbits,
365 bits_big_endian);
366 if (bits_big_endian)
367 datum <<= nbits;
368 else
369 more <<= nbits;
370 datum |= more;
371 }
372
373 return datum;
374 }
375
376 /* Write some bits into a buffer and move forward in the buffer.
377
378 DATUM is the bits to write. The low-order bits of DATUM are used.
379 DEST is the destination buffer. It is updated as bytes are
380 written.
381 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
382 done.
383 NBITS is the number of valid bits in DATUM.
384 BITS_BIG_ENDIAN is taken directly from gdbarch. */
385
386 static void
387 insert_bits (unsigned int datum,
388 gdb_byte *dest, unsigned int dest_offset_bits,
389 int nbits, int bits_big_endian)
390 {
391 unsigned int mask;
392
393 gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8);
394
395 mask = (1 << nbits) - 1;
396 if (bits_big_endian)
397 {
398 datum <<= 8 - (dest_offset_bits + nbits);
399 mask <<= 8 - (dest_offset_bits + nbits);
400 }
401 else
402 {
403 datum <<= dest_offset_bits;
404 mask <<= dest_offset_bits;
405 }
406
407 gdb_assert ((datum & ~mask) == 0);
408
409 *dest = (*dest & ~mask) | datum;
410 }
411
412 /* Copy bits from a source to a destination.
413
414 DEST is where the bits should be written.
415 DEST_OFFSET_BITS is the bit offset into DEST.
416 SOURCE is the source of bits.
417 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
418 BIT_COUNT is the number of bits to copy.
419 BITS_BIG_ENDIAN is taken directly from gdbarch. */
420
421 static void
422 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
423 const gdb_byte *source, unsigned int source_offset_bits,
424 unsigned int bit_count,
425 int bits_big_endian)
426 {
427 unsigned int dest_avail;
428 int datum;
429
430 /* Reduce everything to byte-size pieces. */
431 dest += dest_offset_bits / 8;
432 dest_offset_bits %= 8;
433 source += source_offset_bits / 8;
434 source_offset_bits %= 8;
435
436 dest_avail = 8 - dest_offset_bits % 8;
437
438 /* See if we can fill the first destination byte. */
439 if (dest_avail < bit_count)
440 {
441 datum = extract_bits (&source, &source_offset_bits, dest_avail,
442 bits_big_endian);
443 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
444 ++dest;
445 dest_offset_bits = 0;
446 bit_count -= dest_avail;
447 }
448
449 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
450 than 8 bits remaining. */
451 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
452 for (; bit_count >= 8; bit_count -= 8)
453 {
454 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
455 *dest++ = (gdb_byte) datum;
456 }
457
458 /* Finally, we may have a few leftover bits. */
459 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
460 if (bit_count > 0)
461 {
462 datum = extract_bits (&source, &source_offset_bits, bit_count,
463 bits_big_endian);
464 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
465 }
466 }
467
468 static void
469 read_pieced_value (struct value *v)
470 {
471 int i;
472 long offset = 0;
473 ULONGEST bits_to_skip;
474 gdb_byte *contents;
475 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
476 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
477 size_t type_len;
478 size_t buffer_size = 0;
479 char *buffer = NULL;
480 struct cleanup *cleanup;
481 int bits_big_endian
482 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
483
484 if (value_type (v) != value_enclosing_type (v))
485 internal_error (__FILE__, __LINE__,
486 _("Should not be able to create a lazy value with "
487 "an enclosing type"));
488
489 cleanup = make_cleanup (free_current_contents, &buffer);
490
491 contents = value_contents_raw (v);
492 bits_to_skip = 8 * value_offset (v);
493 if (value_bitsize (v))
494 {
495 bits_to_skip += value_bitpos (v);
496 type_len = value_bitsize (v);
497 }
498 else
499 type_len = 8 * TYPE_LENGTH (value_type (v));
500
501 for (i = 0; i < c->n_pieces && offset < type_len; i++)
502 {
503 struct dwarf_expr_piece *p = &c->pieces[i];
504 size_t this_size, this_size_bits;
505 long dest_offset_bits, source_offset_bits, source_offset;
506 const gdb_byte *intermediate_buffer;
507
508 /* Compute size, source, and destination offsets for copying, in
509 bits. */
510 this_size_bits = p->size;
511 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
512 {
513 bits_to_skip -= this_size_bits;
514 continue;
515 }
516 if (this_size_bits > type_len - offset)
517 this_size_bits = type_len - offset;
518 if (bits_to_skip > 0)
519 {
520 dest_offset_bits = 0;
521 source_offset_bits = bits_to_skip;
522 this_size_bits -= bits_to_skip;
523 bits_to_skip = 0;
524 }
525 else
526 {
527 dest_offset_bits = offset;
528 source_offset_bits = 0;
529 }
530
531 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
532 source_offset = source_offset_bits / 8;
533 if (buffer_size < this_size)
534 {
535 buffer_size = this_size;
536 buffer = xrealloc (buffer, buffer_size);
537 }
538 intermediate_buffer = buffer;
539
540 /* Copy from the source to DEST_BUFFER. */
541 switch (p->location)
542 {
543 case DWARF_VALUE_REGISTER:
544 {
545 struct gdbarch *arch = get_frame_arch (frame);
546 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
547 p->v.expr.value);
548 int reg_offset = source_offset;
549
550 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
551 && this_size < register_size (arch, gdb_regnum))
552 {
553 /* Big-endian, and we want less than full size. */
554 reg_offset = register_size (arch, gdb_regnum) - this_size;
555 /* We want the lower-order THIS_SIZE_BITS of the bytes
556 we extract from the register. */
557 source_offset_bits += 8 * this_size - this_size_bits;
558 }
559
560 if (gdb_regnum != -1)
561 {
562 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
563 this_size, buffer);
564 }
565 else
566 {
567 error (_("Unable to access DWARF register number %s"),
568 paddress (arch, p->v.expr.value));
569 }
570 }
571 break;
572
573 case DWARF_VALUE_MEMORY:
574 if (p->v.expr.in_stack_memory)
575 read_stack (p->v.expr.value + source_offset, buffer, this_size);
576 else
577 read_memory (p->v.expr.value + source_offset, buffer, this_size);
578 break;
579
580 case DWARF_VALUE_STACK:
581 {
582 struct gdbarch *gdbarch = get_type_arch (value_type (v));
583 size_t n = this_size;
584
585 if (n > c->addr_size - source_offset)
586 n = (c->addr_size >= source_offset
587 ? c->addr_size - source_offset
588 : 0);
589 if (n == 0)
590 {
591 /* Nothing. */
592 }
593 else if (source_offset == 0)
594 store_unsigned_integer (buffer, n,
595 gdbarch_byte_order (gdbarch),
596 p->v.expr.value);
597 else
598 {
599 gdb_byte bytes[sizeof (ULONGEST)];
600
601 store_unsigned_integer (bytes, n + source_offset,
602 gdbarch_byte_order (gdbarch),
603 p->v.expr.value);
604 memcpy (buffer, bytes + source_offset, n);
605 }
606 }
607 break;
608
609 case DWARF_VALUE_LITERAL:
610 {
611 size_t n = this_size;
612
613 if (n > p->v.literal.length - source_offset)
614 n = (p->v.literal.length >= source_offset
615 ? p->v.literal.length - source_offset
616 : 0);
617 if (n != 0)
618 intermediate_buffer = p->v.literal.data + source_offset;
619 }
620 break;
621
622 case DWARF_VALUE_OPTIMIZED_OUT:
623 set_value_optimized_out (v, 1);
624 break;
625
626 default:
627 internal_error (__FILE__, __LINE__, _("invalid location type"));
628 }
629
630 if (p->location != DWARF_VALUE_OPTIMIZED_OUT)
631 copy_bitwise (contents, dest_offset_bits,
632 intermediate_buffer, source_offset_bits % 8,
633 this_size_bits, bits_big_endian);
634
635 offset += this_size_bits;
636 }
637
638 do_cleanups (cleanup);
639 }
640
641 static void
642 write_pieced_value (struct value *to, struct value *from)
643 {
644 int i;
645 long offset = 0;
646 ULONGEST bits_to_skip;
647 const gdb_byte *contents;
648 struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
649 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
650 size_t type_len;
651 size_t buffer_size = 0;
652 char *buffer = NULL;
653 struct cleanup *cleanup;
654 int bits_big_endian
655 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
656
657 if (frame == NULL)
658 {
659 set_value_optimized_out (to, 1);
660 return;
661 }
662
663 cleanup = make_cleanup (free_current_contents, &buffer);
664
665 contents = value_contents (from);
666 bits_to_skip = 8 * value_offset (to);
667 if (value_bitsize (to))
668 {
669 bits_to_skip += value_bitpos (to);
670 type_len = value_bitsize (to);
671 }
672 else
673 type_len = 8 * TYPE_LENGTH (value_type (to));
674
675 for (i = 0; i < c->n_pieces && offset < type_len; i++)
676 {
677 struct dwarf_expr_piece *p = &c->pieces[i];
678 size_t this_size_bits, this_size;
679 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
680 int need_bitwise;
681 const gdb_byte *source_buffer;
682
683 this_size_bits = p->size;
684 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
685 {
686 bits_to_skip -= this_size_bits;
687 continue;
688 }
689 if (this_size_bits > type_len - offset)
690 this_size_bits = type_len - offset;
691 if (bits_to_skip > 0)
692 {
693 dest_offset_bits = bits_to_skip;
694 source_offset_bits = 0;
695 this_size_bits -= bits_to_skip;
696 bits_to_skip = 0;
697 }
698 else
699 {
700 dest_offset_bits = 0;
701 source_offset_bits = offset;
702 }
703
704 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
705 source_offset = source_offset_bits / 8;
706 dest_offset = dest_offset_bits / 8;
707 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
708 {
709 source_buffer = contents + source_offset;
710 need_bitwise = 0;
711 }
712 else
713 {
714 if (buffer_size < this_size)
715 {
716 buffer_size = this_size;
717 buffer = xrealloc (buffer, buffer_size);
718 }
719 source_buffer = buffer;
720 need_bitwise = 1;
721 }
722
723 switch (p->location)
724 {
725 case DWARF_VALUE_REGISTER:
726 {
727 struct gdbarch *arch = get_frame_arch (frame);
728 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
729 int reg_offset = dest_offset;
730
731 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
732 && this_size <= register_size (arch, gdb_regnum))
733 /* Big-endian, and we want less than full size. */
734 reg_offset = register_size (arch, gdb_regnum) - this_size;
735
736 if (gdb_regnum != -1)
737 {
738 if (need_bitwise)
739 {
740 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
741 this_size, buffer);
742 copy_bitwise (buffer, dest_offset_bits,
743 contents, source_offset_bits,
744 this_size_bits,
745 bits_big_endian);
746 }
747
748 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
749 this_size, source_buffer);
750 }
751 else
752 {
753 error (_("Unable to write to DWARF register number %s"),
754 paddress (arch, p->v.expr.value));
755 }
756 }
757 break;
758 case DWARF_VALUE_MEMORY:
759 if (need_bitwise)
760 {
761 /* Only the first and last bytes can possibly have any
762 bits reused. */
763 read_memory (p->v.expr.value + dest_offset, buffer, 1);
764 read_memory (p->v.expr.value + dest_offset + this_size - 1,
765 buffer + this_size - 1, 1);
766 copy_bitwise (buffer, dest_offset_bits,
767 contents, source_offset_bits,
768 this_size_bits,
769 bits_big_endian);
770 }
771
772 write_memory (p->v.expr.value + dest_offset,
773 source_buffer, this_size);
774 break;
775 default:
776 set_value_optimized_out (to, 1);
777 break;
778 }
779 offset += this_size_bits;
780 }
781
782 do_cleanups (cleanup);
783 }
784
785 static int
786 check_pieced_value_bits (const struct value *value, int bit_offset,
787 int bit_length, int validity)
788 {
789 struct piece_closure *c
790 = (struct piece_closure *) value_computed_closure (value);
791 int i;
792
793 bit_offset += 8 * value_offset (value);
794 if (value_bitsize (value))
795 bit_offset += value_bitpos (value);
796
797 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
798 {
799 struct dwarf_expr_piece *p = &c->pieces[i];
800 size_t this_size_bits = p->size;
801
802 if (bit_offset > 0)
803 {
804 if (bit_offset >= this_size_bits)
805 {
806 bit_offset -= this_size_bits;
807 continue;
808 }
809
810 bit_length -= this_size_bits - bit_offset;
811 bit_offset = 0;
812 }
813 else
814 bit_length -= this_size_bits;
815
816 if (p->location == DWARF_VALUE_OPTIMIZED_OUT)
817 {
818 if (validity)
819 return 0;
820 }
821 else
822 {
823 if (!validity)
824 return 1;
825 }
826 }
827
828 return validity;
829 }
830
831 static int
832 check_pieced_value_validity (const struct value *value, int bit_offset,
833 int bit_length)
834 {
835 return check_pieced_value_bits (value, bit_offset, bit_length, 1);
836 }
837
838 static int
839 check_pieced_value_invalid (const struct value *value)
840 {
841 return check_pieced_value_bits (value, 0,
842 8 * TYPE_LENGTH (value_type (value)), 0);
843 }
844
845 static void *
846 copy_pieced_value_closure (const struct value *v)
847 {
848 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
849
850 ++c->refc;
851 return c;
852 }
853
854 static void
855 free_pieced_value_closure (struct value *v)
856 {
857 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
858
859 --c->refc;
860 if (c->refc == 0)
861 {
862 xfree (c->pieces);
863 xfree (c);
864 }
865 }
866
867 /* Functions for accessing a variable described by DW_OP_piece. */
868 static struct lval_funcs pieced_value_funcs = {
869 read_pieced_value,
870 write_pieced_value,
871 check_pieced_value_validity,
872 check_pieced_value_invalid,
873 copy_pieced_value_closure,
874 free_pieced_value_closure
875 };
876
877 /* Evaluate a location description, starting at DATA and with length
878 SIZE, to find the current location of variable of TYPE in the context
879 of FRAME. */
880
881 static struct value *
882 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
883 const gdb_byte *data, unsigned short size,
884 struct dwarf2_per_cu_data *per_cu)
885 {
886 struct value *retval;
887 struct dwarf_expr_baton baton;
888 struct dwarf_expr_context *ctx;
889 struct cleanup *old_chain;
890
891 if (size == 0)
892 {
893 retval = allocate_value (type);
894 VALUE_LVAL (retval) = not_lval;
895 set_value_optimized_out (retval, 1);
896 return retval;
897 }
898
899 baton.frame = frame;
900 baton.per_cu = per_cu;
901
902 ctx = new_dwarf_expr_context ();
903 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
904
905 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
906 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
907 ctx->baton = &baton;
908 ctx->read_reg = dwarf_expr_read_reg;
909 ctx->read_mem = dwarf_expr_read_mem;
910 ctx->get_frame_base = dwarf_expr_frame_base;
911 ctx->get_frame_cfa = dwarf_expr_frame_cfa;
912 ctx->get_tls_address = dwarf_expr_tls_address;
913 ctx->dwarf_call = dwarf_expr_dwarf_call;
914
915 dwarf_expr_eval (ctx, data, size);
916 if (ctx->num_pieces > 0)
917 {
918 struct piece_closure *c;
919 struct frame_id frame_id = get_frame_id (frame);
920
921 c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
922 ctx->addr_size);
923 retval = allocate_computed_value (type, &pieced_value_funcs, c);
924 VALUE_FRAME_ID (retval) = frame_id;
925 }
926 else
927 {
928 switch (ctx->location)
929 {
930 case DWARF_VALUE_REGISTER:
931 {
932 struct gdbarch *arch = get_frame_arch (frame);
933 CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
934 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
935
936 if (gdb_regnum != -1)
937 retval = value_from_register (type, gdb_regnum, frame);
938 else
939 error (_("Unable to access DWARF register number %s"),
940 paddress (arch, dwarf_regnum));
941 }
942 break;
943
944 case DWARF_VALUE_MEMORY:
945 {
946 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
947 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
948
949 retval = allocate_value (type);
950 VALUE_LVAL (retval) = lval_memory;
951 set_value_lazy (retval, 1);
952 if (in_stack_memory)
953 set_value_stack (retval, 1);
954 set_value_address (retval, address);
955 }
956 break;
957
958 case DWARF_VALUE_STACK:
959 {
960 ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
961 bfd_byte *contents;
962 size_t n = ctx->addr_size;
963
964 retval = allocate_value (type);
965 contents = value_contents_raw (retval);
966 if (n > TYPE_LENGTH (type))
967 n = TYPE_LENGTH (type);
968 store_unsigned_integer (contents, n,
969 gdbarch_byte_order (ctx->gdbarch),
970 value);
971 }
972 break;
973
974 case DWARF_VALUE_LITERAL:
975 {
976 bfd_byte *contents;
977 size_t n = ctx->len;
978
979 retval = allocate_value (type);
980 contents = value_contents_raw (retval);
981 if (n > TYPE_LENGTH (type))
982 n = TYPE_LENGTH (type);
983 memcpy (contents, ctx->data, n);
984 }
985 break;
986
987 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
988 it can only be encountered when making a piece. */
989 case DWARF_VALUE_OPTIMIZED_OUT:
990 default:
991 internal_error (__FILE__, __LINE__, _("invalid location type"));
992 }
993 }
994
995 set_value_initialized (retval, ctx->initialized);
996
997 do_cleanups (old_chain);
998
999 return retval;
1000 }
1001 \f
1002 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
1003
1004 struct needs_frame_baton
1005 {
1006 int needs_frame;
1007 struct dwarf2_per_cu_data *per_cu;
1008 };
1009
1010 /* Reads from registers do require a frame. */
1011 static CORE_ADDR
1012 needs_frame_read_reg (void *baton, int regnum)
1013 {
1014 struct needs_frame_baton *nf_baton = baton;
1015
1016 nf_baton->needs_frame = 1;
1017 return 1;
1018 }
1019
1020 /* Reads from memory do not require a frame. */
1021 static void
1022 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
1023 {
1024 memset (buf, 0, len);
1025 }
1026
1027 /* Frame-relative accesses do require a frame. */
1028 static void
1029 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
1030 {
1031 static gdb_byte lit0 = DW_OP_lit0;
1032 struct needs_frame_baton *nf_baton = baton;
1033
1034 *start = &lit0;
1035 *length = 1;
1036
1037 nf_baton->needs_frame = 1;
1038 }
1039
1040 /* CFA accesses require a frame. */
1041
1042 static CORE_ADDR
1043 needs_frame_frame_cfa (void *baton)
1044 {
1045 struct needs_frame_baton *nf_baton = baton;
1046
1047 nf_baton->needs_frame = 1;
1048 return 1;
1049 }
1050
1051 /* Thread-local accesses do require a frame. */
1052 static CORE_ADDR
1053 needs_frame_tls_address (void *baton, CORE_ADDR offset)
1054 {
1055 struct needs_frame_baton *nf_baton = baton;
1056
1057 nf_baton->needs_frame = 1;
1058 return 1;
1059 }
1060
1061 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
1062
1063 static void
1064 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
1065 {
1066 struct needs_frame_baton *nf_baton = ctx->baton;
1067
1068 return per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu);
1069 }
1070
1071 /* Return non-zero iff the location expression at DATA (length SIZE)
1072 requires a frame to evaluate. */
1073
1074 static int
1075 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
1076 struct dwarf2_per_cu_data *per_cu)
1077 {
1078 struct needs_frame_baton baton;
1079 struct dwarf_expr_context *ctx;
1080 int in_reg;
1081 struct cleanup *old_chain;
1082
1083 baton.needs_frame = 0;
1084 baton.per_cu = per_cu;
1085
1086 ctx = new_dwarf_expr_context ();
1087 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1088
1089 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
1090 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1091 ctx->baton = &baton;
1092 ctx->read_reg = needs_frame_read_reg;
1093 ctx->read_mem = needs_frame_read_mem;
1094 ctx->get_frame_base = needs_frame_frame_base;
1095 ctx->get_frame_cfa = needs_frame_frame_cfa;
1096 ctx->get_tls_address = needs_frame_tls_address;
1097 ctx->dwarf_call = needs_frame_dwarf_call;
1098
1099 dwarf_expr_eval (ctx, data, size);
1100
1101 in_reg = ctx->location == DWARF_VALUE_REGISTER;
1102
1103 if (ctx->num_pieces > 0)
1104 {
1105 int i;
1106
1107 /* If the location has several pieces, and any of them are in
1108 registers, then we will need a frame to fetch them from. */
1109 for (i = 0; i < ctx->num_pieces; i++)
1110 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
1111 in_reg = 1;
1112 }
1113
1114 do_cleanups (old_chain);
1115
1116 return baton.needs_frame || in_reg;
1117 }
1118
1119 /* A helper function that throws an unimplemented error mentioning a
1120 given DWARF operator. */
1121
1122 static void
1123 unimplemented (unsigned int op)
1124 {
1125 error (_("DWARF operator %s cannot be translated to an agent expression"),
1126 dwarf_stack_op_name (op, 1));
1127 }
1128
1129 /* A helper function to convert a DWARF register to an arch register.
1130 ARCH is the architecture.
1131 DWARF_REG is the register.
1132 This will throw an exception if the DWARF register cannot be
1133 translated to an architecture register. */
1134
1135 static int
1136 translate_register (struct gdbarch *arch, int dwarf_reg)
1137 {
1138 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
1139 if (reg == -1)
1140 error (_("Unable to access DWARF register number %d"), dwarf_reg);
1141 return reg;
1142 }
1143
1144 /* A helper function that emits an access to memory. ARCH is the
1145 target architecture. EXPR is the expression which we are building.
1146 NBITS is the number of bits we want to read. This emits the
1147 opcodes needed to read the memory and then extract the desired
1148 bits. */
1149
1150 static void
1151 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
1152 {
1153 ULONGEST nbytes = (nbits + 7) / 8;
1154
1155 gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
1156
1157 if (trace_kludge)
1158 ax_trace_quick (expr, nbytes);
1159
1160 if (nbits <= 8)
1161 ax_simple (expr, aop_ref8);
1162 else if (nbits <= 16)
1163 ax_simple (expr, aop_ref16);
1164 else if (nbits <= 32)
1165 ax_simple (expr, aop_ref32);
1166 else
1167 ax_simple (expr, aop_ref64);
1168
1169 /* If we read exactly the number of bytes we wanted, we're done. */
1170 if (8 * nbytes == nbits)
1171 return;
1172
1173 if (gdbarch_bits_big_endian (arch))
1174 {
1175 /* On a bits-big-endian machine, we want the high-order
1176 NBITS. */
1177 ax_const_l (expr, 8 * nbytes - nbits);
1178 ax_simple (expr, aop_rsh_unsigned);
1179 }
1180 else
1181 {
1182 /* On a bits-little-endian box, we want the low-order NBITS. */
1183 ax_zero_ext (expr, nbits);
1184 }
1185 }
1186
1187 /* Compile a DWARF location expression to an agent expression.
1188
1189 EXPR is the agent expression we are building.
1190 LOC is the agent value we modify.
1191 ARCH is the architecture.
1192 ADDR_SIZE is the size of addresses, in bytes.
1193 OP_PTR is the start of the location expression.
1194 OP_END is one past the last byte of the location expression.
1195
1196 This will throw an exception for various kinds of errors -- for
1197 example, if the expression cannot be compiled, or if the expression
1198 is invalid. */
1199
1200 static void
1201 compile_dwarf_to_ax (struct agent_expr *expr, struct axs_value *loc,
1202 struct gdbarch *arch, unsigned int addr_size,
1203 const gdb_byte *op_ptr, const gdb_byte *op_end,
1204 struct dwarf2_per_cu_data *per_cu)
1205 {
1206 struct cleanup *cleanups;
1207 int i, *offsets;
1208 VEC(int) *dw_labels = NULL, *patches = NULL;
1209 const gdb_byte * const base = op_ptr;
1210 const gdb_byte *previous_piece = op_ptr;
1211 enum bfd_endian byte_order = gdbarch_byte_order (arch);
1212 ULONGEST bits_collected = 0;
1213 unsigned int addr_size_bits = 8 * addr_size;
1214 int bits_big_endian = gdbarch_bits_big_endian (arch);
1215
1216 offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
1217 cleanups = make_cleanup (xfree, offsets);
1218
1219 for (i = 0; i < op_end - op_ptr; ++i)
1220 offsets[i] = -1;
1221
1222 make_cleanup (VEC_cleanup (int), &dw_labels);
1223 make_cleanup (VEC_cleanup (int), &patches);
1224
1225 /* By default we are making an address. */
1226 loc->kind = axs_lvalue_memory;
1227
1228 while (op_ptr < op_end)
1229 {
1230 enum dwarf_location_atom op = *op_ptr;
1231 CORE_ADDR result;
1232 ULONGEST uoffset, reg;
1233 LONGEST offset;
1234 int i;
1235
1236 offsets[op_ptr - base] = expr->len;
1237 ++op_ptr;
1238
1239 /* Our basic approach to code generation is to map DWARF
1240 operations directly to AX operations. However, there are
1241 some differences.
1242
1243 First, DWARF works on address-sized units, but AX always uses
1244 LONGEST. For most operations we simply ignore this
1245 difference; instead we generate sign extensions as needed
1246 before division and comparison operations. It would be nice
1247 to omit the sign extensions, but there is no way to determine
1248 the size of the target's LONGEST. (This code uses the size
1249 of the host LONGEST in some cases -- that is a bug but it is
1250 difficult to fix.)
1251
1252 Second, some DWARF operations cannot be translated to AX.
1253 For these we simply fail. See
1254 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
1255 switch (op)
1256 {
1257 case DW_OP_lit0:
1258 case DW_OP_lit1:
1259 case DW_OP_lit2:
1260 case DW_OP_lit3:
1261 case DW_OP_lit4:
1262 case DW_OP_lit5:
1263 case DW_OP_lit6:
1264 case DW_OP_lit7:
1265 case DW_OP_lit8:
1266 case DW_OP_lit9:
1267 case DW_OP_lit10:
1268 case DW_OP_lit11:
1269 case DW_OP_lit12:
1270 case DW_OP_lit13:
1271 case DW_OP_lit14:
1272 case DW_OP_lit15:
1273 case DW_OP_lit16:
1274 case DW_OP_lit17:
1275 case DW_OP_lit18:
1276 case DW_OP_lit19:
1277 case DW_OP_lit20:
1278 case DW_OP_lit21:
1279 case DW_OP_lit22:
1280 case DW_OP_lit23:
1281 case DW_OP_lit24:
1282 case DW_OP_lit25:
1283 case DW_OP_lit26:
1284 case DW_OP_lit27:
1285 case DW_OP_lit28:
1286 case DW_OP_lit29:
1287 case DW_OP_lit30:
1288 case DW_OP_lit31:
1289 ax_const_l (expr, op - DW_OP_lit0);
1290 break;
1291
1292 case DW_OP_addr:
1293 result = dwarf2_read_address (arch, op_ptr, op_end, addr_size);
1294 ax_const_l (expr, result);
1295 op_ptr += addr_size;
1296 break;
1297
1298 case DW_OP_const1u:
1299 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
1300 op_ptr += 1;
1301 break;
1302 case DW_OP_const1s:
1303 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
1304 op_ptr += 1;
1305 break;
1306 case DW_OP_const2u:
1307 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
1308 op_ptr += 2;
1309 break;
1310 case DW_OP_const2s:
1311 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
1312 op_ptr += 2;
1313 break;
1314 case DW_OP_const4u:
1315 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
1316 op_ptr += 4;
1317 break;
1318 case DW_OP_const4s:
1319 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
1320 op_ptr += 4;
1321 break;
1322 case DW_OP_const8u:
1323 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
1324 op_ptr += 8;
1325 break;
1326 case DW_OP_const8s:
1327 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
1328 op_ptr += 8;
1329 break;
1330 case DW_OP_constu:
1331 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
1332 ax_const_l (expr, uoffset);
1333 break;
1334 case DW_OP_consts:
1335 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1336 ax_const_l (expr, offset);
1337 break;
1338
1339 case DW_OP_reg0:
1340 case DW_OP_reg1:
1341 case DW_OP_reg2:
1342 case DW_OP_reg3:
1343 case DW_OP_reg4:
1344 case DW_OP_reg5:
1345 case DW_OP_reg6:
1346 case DW_OP_reg7:
1347 case DW_OP_reg8:
1348 case DW_OP_reg9:
1349 case DW_OP_reg10:
1350 case DW_OP_reg11:
1351 case DW_OP_reg12:
1352 case DW_OP_reg13:
1353 case DW_OP_reg14:
1354 case DW_OP_reg15:
1355 case DW_OP_reg16:
1356 case DW_OP_reg17:
1357 case DW_OP_reg18:
1358 case DW_OP_reg19:
1359 case DW_OP_reg20:
1360 case DW_OP_reg21:
1361 case DW_OP_reg22:
1362 case DW_OP_reg23:
1363 case DW_OP_reg24:
1364 case DW_OP_reg25:
1365 case DW_OP_reg26:
1366 case DW_OP_reg27:
1367 case DW_OP_reg28:
1368 case DW_OP_reg29:
1369 case DW_OP_reg30:
1370 case DW_OP_reg31:
1371 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1372 loc->u.reg = translate_register (arch, op - DW_OP_reg0);
1373 loc->kind = axs_lvalue_register;
1374 break;
1375
1376 case DW_OP_regx:
1377 op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1378 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1379 loc->u.reg = translate_register (arch, reg);
1380 loc->kind = axs_lvalue_register;
1381 break;
1382
1383 case DW_OP_implicit_value:
1384 {
1385 ULONGEST len;
1386
1387 op_ptr = read_uleb128 (op_ptr, op_end, &len);
1388 if (op_ptr + len > op_end)
1389 error (_("DW_OP_implicit_value: too few bytes available."));
1390 if (len > sizeof (ULONGEST))
1391 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
1392 (int) len);
1393
1394 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
1395 byte_order));
1396 op_ptr += len;
1397 dwarf_expr_require_composition (op_ptr, op_end,
1398 "DW_OP_implicit_value");
1399
1400 loc->kind = axs_rvalue;
1401 }
1402 break;
1403
1404 case DW_OP_stack_value:
1405 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
1406 loc->kind = axs_rvalue;
1407 break;
1408
1409 case DW_OP_breg0:
1410 case DW_OP_breg1:
1411 case DW_OP_breg2:
1412 case DW_OP_breg3:
1413 case DW_OP_breg4:
1414 case DW_OP_breg5:
1415 case DW_OP_breg6:
1416 case DW_OP_breg7:
1417 case DW_OP_breg8:
1418 case DW_OP_breg9:
1419 case DW_OP_breg10:
1420 case DW_OP_breg11:
1421 case DW_OP_breg12:
1422 case DW_OP_breg13:
1423 case DW_OP_breg14:
1424 case DW_OP_breg15:
1425 case DW_OP_breg16:
1426 case DW_OP_breg17:
1427 case DW_OP_breg18:
1428 case DW_OP_breg19:
1429 case DW_OP_breg20:
1430 case DW_OP_breg21:
1431 case DW_OP_breg22:
1432 case DW_OP_breg23:
1433 case DW_OP_breg24:
1434 case DW_OP_breg25:
1435 case DW_OP_breg26:
1436 case DW_OP_breg27:
1437 case DW_OP_breg28:
1438 case DW_OP_breg29:
1439 case DW_OP_breg30:
1440 case DW_OP_breg31:
1441 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1442 i = translate_register (arch, op - DW_OP_breg0);
1443 ax_reg (expr, i);
1444 if (offset != 0)
1445 {
1446 ax_const_l (expr, offset);
1447 ax_simple (expr, aop_add);
1448 }
1449 break;
1450 case DW_OP_bregx:
1451 {
1452 op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1453 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1454 i = translate_register (arch, reg);
1455 ax_reg (expr, i);
1456 if (offset != 0)
1457 {
1458 ax_const_l (expr, offset);
1459 ax_simple (expr, aop_add);
1460 }
1461 }
1462 break;
1463 case DW_OP_fbreg:
1464 {
1465 const gdb_byte *datastart;
1466 size_t datalen;
1467 unsigned int before_stack_len;
1468 struct block *b;
1469 struct symbol *framefunc;
1470 LONGEST base_offset = 0;
1471
1472 b = block_for_pc (expr->scope);
1473
1474 if (!b)
1475 error (_("No block found for address"));
1476
1477 framefunc = block_linkage_function (b);
1478
1479 if (!framefunc)
1480 error (_("No function found for block"));
1481
1482 dwarf_expr_frame_base_1 (framefunc, expr->scope,
1483 &datastart, &datalen);
1484
1485 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1486 compile_dwarf_to_ax (expr, loc, arch, addr_size, datastart,
1487 datastart + datalen, per_cu);
1488
1489 if (offset != 0)
1490 {
1491 ax_const_l (expr, offset);
1492 ax_simple (expr, aop_add);
1493 }
1494
1495 loc->kind = axs_lvalue_memory;
1496 }
1497 break;
1498
1499 case DW_OP_dup:
1500 ax_simple (expr, aop_dup);
1501 break;
1502
1503 case DW_OP_drop:
1504 ax_simple (expr, aop_pop);
1505 break;
1506
1507 case DW_OP_pick:
1508 offset = *op_ptr++;
1509 unimplemented (op);
1510 break;
1511
1512 case DW_OP_swap:
1513 ax_simple (expr, aop_swap);
1514 break;
1515
1516 case DW_OP_over:
1517 /* We can't directly support DW_OP_over, but GCC emits it as
1518 part of a sequence to implement signed modulus. As a
1519 hack, we recognize this sequence. Note that if GCC ever
1520 generates a branch to the middle of this sequence, then
1521 we will die somehow. */
1522 if (op_end - op_ptr >= 4
1523 && op_ptr[0] == DW_OP_over
1524 && op_ptr[1] == DW_OP_div
1525 && op_ptr[2] == DW_OP_mul
1526 && op_ptr[3] == DW_OP_minus)
1527 {
1528 /* Sign extend the operands. */
1529 ax_ext (expr, addr_size_bits);
1530 ax_simple (expr, aop_swap);
1531 ax_ext (expr, addr_size_bits);
1532 ax_simple (expr, aop_swap);
1533 ax_simple (expr, aop_rem_signed);
1534 op_ptr += 4;
1535 }
1536 else
1537 unimplemented (op);
1538 break;
1539
1540 case DW_OP_rot:
1541 unimplemented (op);
1542 break;
1543
1544 case DW_OP_deref:
1545 case DW_OP_deref_size:
1546 {
1547 int size;
1548
1549 if (op == DW_OP_deref_size)
1550 size = *op_ptr++;
1551 else
1552 size = addr_size;
1553
1554 switch (size)
1555 {
1556 case 8:
1557 ax_simple (expr, aop_ref8);
1558 break;
1559 case 16:
1560 ax_simple (expr, aop_ref16);
1561 break;
1562 case 32:
1563 ax_simple (expr, aop_ref32);
1564 break;
1565 case 64:
1566 ax_simple (expr, aop_ref64);
1567 break;
1568 default:
1569 error (_("Unsupported size %d in %s"),
1570 size, dwarf_stack_op_name (op, 1));
1571 }
1572 }
1573 break;
1574
1575 case DW_OP_abs:
1576 /* Sign extend the operand. */
1577 ax_ext (expr, addr_size_bits);
1578 ax_simple (expr, aop_dup);
1579 ax_const_l (expr, 0);
1580 ax_simple (expr, aop_less_signed);
1581 ax_simple (expr, aop_log_not);
1582 i = ax_goto (expr, aop_if_goto);
1583 /* We have to emit 0 - X. */
1584 ax_const_l (expr, 0);
1585 ax_simple (expr, aop_swap);
1586 ax_simple (expr, aop_sub);
1587 ax_label (expr, i, expr->len);
1588 break;
1589
1590 case DW_OP_neg:
1591 /* No need to sign extend here. */
1592 ax_const_l (expr, 0);
1593 ax_simple (expr, aop_swap);
1594 ax_simple (expr, aop_sub);
1595 break;
1596
1597 case DW_OP_not:
1598 /* Sign extend the operand. */
1599 ax_ext (expr, addr_size_bits);
1600 ax_simple (expr, aop_bit_not);
1601 break;
1602
1603 case DW_OP_plus_uconst:
1604 op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1605 /* It would be really weird to emit `DW_OP_plus_uconst 0',
1606 but we micro-optimize anyhow. */
1607 if (reg != 0)
1608 {
1609 ax_const_l (expr, reg);
1610 ax_simple (expr, aop_add);
1611 }
1612 break;
1613
1614 case DW_OP_and:
1615 ax_simple (expr, aop_bit_and);
1616 break;
1617
1618 case DW_OP_div:
1619 /* Sign extend the operands. */
1620 ax_ext (expr, addr_size_bits);
1621 ax_simple (expr, aop_swap);
1622 ax_ext (expr, addr_size_bits);
1623 ax_simple (expr, aop_swap);
1624 ax_simple (expr, aop_div_signed);
1625 break;
1626
1627 case DW_OP_minus:
1628 ax_simple (expr, aop_sub);
1629 break;
1630
1631 case DW_OP_mod:
1632 ax_simple (expr, aop_rem_unsigned);
1633 break;
1634
1635 case DW_OP_mul:
1636 ax_simple (expr, aop_mul);
1637 break;
1638
1639 case DW_OP_or:
1640 ax_simple (expr, aop_bit_or);
1641 break;
1642
1643 case DW_OP_plus:
1644 ax_simple (expr, aop_add);
1645 break;
1646
1647 case DW_OP_shl:
1648 ax_simple (expr, aop_lsh);
1649 break;
1650
1651 case DW_OP_shr:
1652 ax_simple (expr, aop_rsh_unsigned);
1653 break;
1654
1655 case DW_OP_shra:
1656 ax_simple (expr, aop_rsh_signed);
1657 break;
1658
1659 case DW_OP_xor:
1660 ax_simple (expr, aop_bit_xor);
1661 break;
1662
1663 case DW_OP_le:
1664 /* Sign extend the operands. */
1665 ax_ext (expr, addr_size_bits);
1666 ax_simple (expr, aop_swap);
1667 ax_ext (expr, addr_size_bits);
1668 /* Note no swap here: A <= B is !(B < A). */
1669 ax_simple (expr, aop_less_signed);
1670 ax_simple (expr, aop_log_not);
1671 break;
1672
1673 case DW_OP_ge:
1674 /* Sign extend the operands. */
1675 ax_ext (expr, addr_size_bits);
1676 ax_simple (expr, aop_swap);
1677 ax_ext (expr, addr_size_bits);
1678 ax_simple (expr, aop_swap);
1679 /* A >= B is !(A < B). */
1680 ax_simple (expr, aop_less_signed);
1681 ax_simple (expr, aop_log_not);
1682 break;
1683
1684 case DW_OP_eq:
1685 /* Sign extend the operands. */
1686 ax_ext (expr, addr_size_bits);
1687 ax_simple (expr, aop_swap);
1688 ax_ext (expr, addr_size_bits);
1689 /* No need for a second swap here. */
1690 ax_simple (expr, aop_equal);
1691 break;
1692
1693 case DW_OP_lt:
1694 /* Sign extend the operands. */
1695 ax_ext (expr, addr_size_bits);
1696 ax_simple (expr, aop_swap);
1697 ax_ext (expr, addr_size_bits);
1698 ax_simple (expr, aop_swap);
1699 ax_simple (expr, aop_less_signed);
1700 break;
1701
1702 case DW_OP_gt:
1703 /* Sign extend the operands. */
1704 ax_ext (expr, addr_size_bits);
1705 ax_simple (expr, aop_swap);
1706 ax_ext (expr, addr_size_bits);
1707 /* Note no swap here: A > B is B < A. */
1708 ax_simple (expr, aop_less_signed);
1709 break;
1710
1711 case DW_OP_ne:
1712 /* Sign extend the operands. */
1713 ax_ext (expr, addr_size_bits);
1714 ax_simple (expr, aop_swap);
1715 ax_ext (expr, addr_size_bits);
1716 /* No need for a swap here. */
1717 ax_simple (expr, aop_equal);
1718 ax_simple (expr, aop_log_not);
1719 break;
1720
1721 case DW_OP_call_frame_cfa:
1722 unimplemented (op);
1723 break;
1724
1725 case DW_OP_GNU_push_tls_address:
1726 unimplemented (op);
1727 break;
1728
1729 case DW_OP_skip:
1730 offset = extract_signed_integer (op_ptr, 2, byte_order);
1731 op_ptr += 2;
1732 i = ax_goto (expr, aop_goto);
1733 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
1734 VEC_safe_push (int, patches, i);
1735 break;
1736
1737 case DW_OP_bra:
1738 offset = extract_signed_integer (op_ptr, 2, byte_order);
1739 op_ptr += 2;
1740 /* Zero extend the operand. */
1741 ax_zero_ext (expr, addr_size_bits);
1742 i = ax_goto (expr, aop_if_goto);
1743 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
1744 VEC_safe_push (int, patches, i);
1745 break;
1746
1747 case DW_OP_nop:
1748 break;
1749
1750 case DW_OP_piece:
1751 case DW_OP_bit_piece:
1752 {
1753 ULONGEST size, offset;
1754
1755 if (op_ptr - 1 == previous_piece)
1756 error (_("Cannot translate empty pieces to agent expressions"));
1757 previous_piece = op_ptr - 1;
1758
1759 op_ptr = read_uleb128 (op_ptr, op_end, &size);
1760 if (op == DW_OP_piece)
1761 {
1762 size *= 8;
1763 offset = 0;
1764 }
1765 else
1766 op_ptr = read_uleb128 (op_ptr, op_end, &offset);
1767
1768 if (bits_collected + size > 8 * sizeof (LONGEST))
1769 error (_("Expression pieces exceed word size"));
1770
1771 /* Access the bits. */
1772 switch (loc->kind)
1773 {
1774 case axs_lvalue_register:
1775 ax_reg (expr, loc->u.reg);
1776 break;
1777
1778 case axs_lvalue_memory:
1779 /* Offset the pointer, if needed. */
1780 if (offset > 8)
1781 {
1782 ax_const_l (expr, offset / 8);
1783 ax_simple (expr, aop_add);
1784 offset %= 8;
1785 }
1786 access_memory (arch, expr, size);
1787 break;
1788 }
1789
1790 /* For a bits-big-endian target, shift up what we already
1791 have. For a bits-little-endian target, shift up the
1792 new data. Note that there is a potential bug here if
1793 the DWARF expression leaves multiple values on the
1794 stack. */
1795 if (bits_collected > 0)
1796 {
1797 if (bits_big_endian)
1798 {
1799 ax_simple (expr, aop_swap);
1800 ax_const_l (expr, size);
1801 ax_simple (expr, aop_lsh);
1802 /* We don't need a second swap here, because
1803 aop_bit_or is symmetric. */
1804 }
1805 else
1806 {
1807 ax_const_l (expr, size);
1808 ax_simple (expr, aop_lsh);
1809 }
1810 ax_simple (expr, aop_bit_or);
1811 }
1812
1813 bits_collected += size;
1814 loc->kind = axs_rvalue;
1815 }
1816 break;
1817
1818 case DW_OP_GNU_uninit:
1819 unimplemented (op);
1820
1821 case DW_OP_call2:
1822 case DW_OP_call4:
1823 {
1824 struct dwarf2_locexpr_baton block;
1825 int size = (op == DW_OP_call2 ? 2 : 4);
1826
1827 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
1828 op_ptr += size;
1829
1830 block = dwarf2_fetch_die_location_block (uoffset, per_cu);
1831
1832 /* DW_OP_call_ref is currently not supported. */
1833 gdb_assert (block.per_cu == per_cu);
1834
1835 compile_dwarf_to_ax (expr, loc, arch, addr_size,
1836 block.data, block.data + block.size,
1837 per_cu);
1838 }
1839 break;
1840
1841 case DW_OP_call_ref:
1842 unimplemented (op);
1843
1844 default:
1845 error (_("Unhandled dwarf expression opcode 0x%x"), op);
1846 }
1847 }
1848
1849 /* Patch all the branches we emitted. */
1850 for (i = 0; i < VEC_length (int, patches); ++i)
1851 {
1852 int targ = offsets[VEC_index (int, dw_labels, i)];
1853 if (targ == -1)
1854 internal_error (__FILE__, __LINE__, _("invalid label"));
1855 ax_label (expr, VEC_index (int, patches, i), targ);
1856 }
1857
1858 do_cleanups (cleanups);
1859 }
1860
1861 \f
1862 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1863 evaluator to calculate the location. */
1864 static struct value *
1865 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
1866 {
1867 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1868 struct value *val;
1869
1870 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
1871 dlbaton->size, dlbaton->per_cu);
1872
1873 return val;
1874 }
1875
1876 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1877 static int
1878 locexpr_read_needs_frame (struct symbol *symbol)
1879 {
1880 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1881
1882 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
1883 dlbaton->per_cu);
1884 }
1885
1886 /* Return true if DATA points to the end of a piece. END is one past
1887 the last byte in the expression. */
1888
1889 static int
1890 piece_end_p (const gdb_byte *data, const gdb_byte *end)
1891 {
1892 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
1893 }
1894
1895 /* Nicely describe a single piece of a location, returning an updated
1896 position in the bytecode sequence. This function cannot recognize
1897 all locations; if a location is not recognized, it simply returns
1898 DATA. */
1899
1900 static const gdb_byte *
1901 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
1902 CORE_ADDR addr, struct objfile *objfile,
1903 const gdb_byte *data, const gdb_byte *end,
1904 unsigned int addr_size)
1905 {
1906 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1907 int regno;
1908
1909 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1910 {
1911 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1912 fprintf_filtered (stream, _("a variable in $%s"),
1913 gdbarch_register_name (gdbarch, regno));
1914 data += 1;
1915 }
1916 else if (data[0] == DW_OP_regx)
1917 {
1918 ULONGEST reg;
1919
1920 data = read_uleb128 (data + 1, end, &reg);
1921 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1922 fprintf_filtered (stream, _("a variable in $%s"),
1923 gdbarch_register_name (gdbarch, regno));
1924 }
1925 else if (data[0] == DW_OP_fbreg)
1926 {
1927 struct block *b;
1928 struct symbol *framefunc;
1929 int frame_reg = 0;
1930 LONGEST frame_offset;
1931 const gdb_byte *base_data, *new_data;
1932 size_t base_size;
1933 LONGEST base_offset = 0;
1934
1935 new_data = read_sleb128 (data + 1, end, &frame_offset);
1936 if (!piece_end_p (new_data, end))
1937 return data;
1938 data = new_data;
1939
1940 b = block_for_pc (addr);
1941
1942 if (!b)
1943 error (_("No block found for address for symbol \"%s\"."),
1944 SYMBOL_PRINT_NAME (symbol));
1945
1946 framefunc = block_linkage_function (b);
1947
1948 if (!framefunc)
1949 error (_("No function found for block for symbol \"%s\"."),
1950 SYMBOL_PRINT_NAME (symbol));
1951
1952 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
1953
1954 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1955 {
1956 const gdb_byte *buf_end;
1957
1958 frame_reg = base_data[0] - DW_OP_breg0;
1959 buf_end = read_sleb128 (base_data + 1,
1960 base_data + base_size, &base_offset);
1961 if (buf_end != base_data + base_size)
1962 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1963 frame_reg, SYMBOL_PRINT_NAME (symbol));
1964 }
1965 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1966 {
1967 /* The frame base is just the register, with no offset. */
1968 frame_reg = base_data[0] - DW_OP_reg0;
1969 base_offset = 0;
1970 }
1971 else
1972 {
1973 /* We don't know what to do with the frame base expression,
1974 so we can't trace this variable; give up. */
1975 error (_("Cannot describe location of symbol \"%s\"; "
1976 "DWARF 2 encoding not handled, "
1977 "first opcode in base data is 0x%x."),
1978 SYMBOL_PRINT_NAME (symbol), base_data[0]);
1979 }
1980
1981 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1982
1983 fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1984 gdbarch_register_name (gdbarch, regno),
1985 plongest (base_offset), plongest (frame_offset));
1986 }
1987 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
1988 && piece_end_p (data, end))
1989 {
1990 LONGEST offset;
1991
1992 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1993
1994 data = read_sleb128 (data + 1, end, &offset);
1995
1996 fprintf_filtered (stream,
1997 _("a variable at offset %s from base reg $%s"),
1998 plongest (offset),
1999 gdbarch_register_name (gdbarch, regno));
2000 }
2001
2002 /* The location expression for a TLS variable looks like this (on a
2003 64-bit LE machine):
2004
2005 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
2006 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
2007
2008 0x3 is the encoding for DW_OP_addr, which has an operand as long
2009 as the size of an address on the target machine (here is 8
2010 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
2011 The operand represents the offset at which the variable is within
2012 the thread local storage. */
2013
2014 else if (data + 1 + addr_size < end
2015 && data[0] == DW_OP_addr
2016 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
2017 && piece_end_p (data + 2 + addr_size, end))
2018 {
2019 CORE_ADDR offset = dwarf2_read_address (gdbarch,
2020 data + 1,
2021 end,
2022 addr_size);
2023
2024 fprintf_filtered (stream,
2025 _("a thread-local variable at offset %s "
2026 "in the thread-local storage for `%s'"),
2027 paddress (gdbarch, offset), objfile->name);
2028
2029 data += 1 + addr_size + 1;
2030 }
2031 else if (data[0] >= DW_OP_lit0
2032 && data[0] <= DW_OP_lit31
2033 && data + 1 < end
2034 && data[1] == DW_OP_stack_value)
2035 {
2036 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
2037 data += 2;
2038 }
2039
2040 return data;
2041 }
2042
2043 /* Disassemble an expression, stopping at the end of a piece or at the
2044 end of the expression. Returns a pointer to the next unread byte
2045 in the input expression. If ALL is nonzero, then this function
2046 will keep going until it reaches the end of the expression. */
2047
2048 static const gdb_byte *
2049 disassemble_dwarf_expression (struct ui_file *stream,
2050 struct gdbarch *arch, unsigned int addr_size,
2051 int offset_size,
2052 const gdb_byte *data, const gdb_byte *end,
2053 int all)
2054 {
2055 const gdb_byte *start = data;
2056
2057 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
2058
2059 while (data < end
2060 && (all
2061 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
2062 {
2063 enum dwarf_location_atom op = *data++;
2064 CORE_ADDR addr;
2065 ULONGEST ul;
2066 LONGEST l;
2067 const char *name;
2068
2069 name = dwarf_stack_op_name (op, 0);
2070
2071 if (!name)
2072 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
2073 op, (long) (data - start));
2074 fprintf_filtered (stream, " % 4ld: %s", (long) (data - start), name);
2075
2076 switch (op)
2077 {
2078 case DW_OP_addr:
2079 addr = dwarf2_read_address (arch, data, end, addr_size);
2080 data += addr_size;
2081 fprintf_filtered (stream, " %s", paddress (arch, addr));
2082 break;
2083
2084 case DW_OP_const1u:
2085 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
2086 data += 1;
2087 fprintf_filtered (stream, " %s", pulongest (ul));
2088 break;
2089 case DW_OP_const1s:
2090 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
2091 data += 1;
2092 fprintf_filtered (stream, " %s", plongest (l));
2093 break;
2094 case DW_OP_const2u:
2095 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2096 data += 2;
2097 fprintf_filtered (stream, " %s", pulongest (ul));
2098 break;
2099 case DW_OP_const2s:
2100 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2101 data += 2;
2102 fprintf_filtered (stream, " %s", plongest (l));
2103 break;
2104 case DW_OP_const4u:
2105 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2106 data += 4;
2107 fprintf_filtered (stream, " %s", pulongest (ul));
2108 break;
2109 case DW_OP_const4s:
2110 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
2111 data += 4;
2112 fprintf_filtered (stream, " %s", plongest (l));
2113 break;
2114 case DW_OP_const8u:
2115 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
2116 data += 8;
2117 fprintf_filtered (stream, " %s", pulongest (ul));
2118 break;
2119 case DW_OP_const8s:
2120 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
2121 data += 8;
2122 fprintf_filtered (stream, " %s", plongest (l));
2123 break;
2124 case DW_OP_constu:
2125 data = read_uleb128 (data, end, &ul);
2126 fprintf_filtered (stream, " %s", pulongest (ul));
2127 break;
2128 case DW_OP_consts:
2129 data = read_sleb128 (data, end, &l);
2130 fprintf_filtered (stream, " %s", plongest (l));
2131 break;
2132
2133 case DW_OP_reg0:
2134 case DW_OP_reg1:
2135 case DW_OP_reg2:
2136 case DW_OP_reg3:
2137 case DW_OP_reg4:
2138 case DW_OP_reg5:
2139 case DW_OP_reg6:
2140 case DW_OP_reg7:
2141 case DW_OP_reg8:
2142 case DW_OP_reg9:
2143 case DW_OP_reg10:
2144 case DW_OP_reg11:
2145 case DW_OP_reg12:
2146 case DW_OP_reg13:
2147 case DW_OP_reg14:
2148 case DW_OP_reg15:
2149 case DW_OP_reg16:
2150 case DW_OP_reg17:
2151 case DW_OP_reg18:
2152 case DW_OP_reg19:
2153 case DW_OP_reg20:
2154 case DW_OP_reg21:
2155 case DW_OP_reg22:
2156 case DW_OP_reg23:
2157 case DW_OP_reg24:
2158 case DW_OP_reg25:
2159 case DW_OP_reg26:
2160 case DW_OP_reg27:
2161 case DW_OP_reg28:
2162 case DW_OP_reg29:
2163 case DW_OP_reg30:
2164 case DW_OP_reg31:
2165 fprintf_filtered (stream, " [$%s]",
2166 gdbarch_register_name (arch, op - DW_OP_reg0));
2167 break;
2168
2169 case DW_OP_regx:
2170 data = read_uleb128 (data, end, &ul);
2171 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2172 gdbarch_register_name (arch, (int) ul));
2173 break;
2174
2175 case DW_OP_implicit_value:
2176 data = read_uleb128 (data, end, &ul);
2177 data += ul;
2178 fprintf_filtered (stream, " %s", pulongest (ul));
2179 break;
2180
2181 case DW_OP_breg0:
2182 case DW_OP_breg1:
2183 case DW_OP_breg2:
2184 case DW_OP_breg3:
2185 case DW_OP_breg4:
2186 case DW_OP_breg5:
2187 case DW_OP_breg6:
2188 case DW_OP_breg7:
2189 case DW_OP_breg8:
2190 case DW_OP_breg9:
2191 case DW_OP_breg10:
2192 case DW_OP_breg11:
2193 case DW_OP_breg12:
2194 case DW_OP_breg13:
2195 case DW_OP_breg14:
2196 case DW_OP_breg15:
2197 case DW_OP_breg16:
2198 case DW_OP_breg17:
2199 case DW_OP_breg18:
2200 case DW_OP_breg19:
2201 case DW_OP_breg20:
2202 case DW_OP_breg21:
2203 case DW_OP_breg22:
2204 case DW_OP_breg23:
2205 case DW_OP_breg24:
2206 case DW_OP_breg25:
2207 case DW_OP_breg26:
2208 case DW_OP_breg27:
2209 case DW_OP_breg28:
2210 case DW_OP_breg29:
2211 case DW_OP_breg30:
2212 case DW_OP_breg31:
2213 data = read_sleb128 (data, end, &ul);
2214 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2215 gdbarch_register_name (arch, op - DW_OP_breg0));
2216 break;
2217
2218 case DW_OP_bregx:
2219 {
2220 ULONGEST offset;
2221
2222 data = read_uleb128 (data, end, &ul);
2223 data = read_sleb128 (data, end, &offset);
2224 fprintf_filtered (stream, " register %s [$%s] offset %s",
2225 pulongest (ul),
2226 gdbarch_register_name (arch, (int) ul),
2227 pulongest (offset));
2228 }
2229 break;
2230
2231 case DW_OP_fbreg:
2232 data = read_sleb128 (data, end, &ul);
2233 fprintf_filtered (stream, " %s", pulongest (ul));
2234 break;
2235
2236 case DW_OP_xderef_size:
2237 case DW_OP_deref_size:
2238 case DW_OP_pick:
2239 fprintf_filtered (stream, " %d", *data);
2240 ++data;
2241 break;
2242
2243 case DW_OP_plus_uconst:
2244 data = read_uleb128 (data, end, &ul);
2245 fprintf_filtered (stream, " %s", pulongest (ul));
2246 break;
2247
2248 case DW_OP_skip:
2249 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2250 data += 2;
2251 fprintf_filtered (stream, " to %ld",
2252 (long) (data + l - start));
2253 break;
2254
2255 case DW_OP_bra:
2256 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2257 data += 2;
2258 fprintf_filtered (stream, " %ld",
2259 (long) (data + l - start));
2260 break;
2261
2262 case DW_OP_call2:
2263 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2264 data += 2;
2265 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
2266 break;
2267
2268 case DW_OP_call4:
2269 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2270 data += 4;
2271 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
2272 break;
2273
2274 case DW_OP_call_ref:
2275 ul = extract_unsigned_integer (data, offset_size,
2276 gdbarch_byte_order (arch));
2277 data += offset_size;
2278 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
2279 break;
2280
2281 case DW_OP_piece:
2282 data = read_uleb128 (data, end, &ul);
2283 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
2284 break;
2285
2286 case DW_OP_bit_piece:
2287 {
2288 ULONGEST offset;
2289
2290 data = read_uleb128 (data, end, &ul);
2291 data = read_uleb128 (data, end, &offset);
2292 fprintf_filtered (stream, " size %s offset %s (bits)",
2293 pulongest (ul), pulongest (offset));
2294 }
2295 break;
2296 }
2297
2298 fprintf_filtered (stream, "\n");
2299 }
2300
2301 return data;
2302 }
2303
2304 /* Describe a single location, which may in turn consist of multiple
2305 pieces. */
2306
2307 static void
2308 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
2309 struct ui_file *stream,
2310 const gdb_byte *data, int size,
2311 struct objfile *objfile, unsigned int addr_size,
2312 int offset_size)
2313 {
2314 const gdb_byte *end = data + size;
2315 int first_piece = 1, bad = 0;
2316
2317 while (data < end)
2318 {
2319 const gdb_byte *here = data;
2320 int disassemble = 1;
2321
2322 if (first_piece)
2323 first_piece = 0;
2324 else
2325 fprintf_filtered (stream, _(", and "));
2326
2327 if (!dwarf2_always_disassemble)
2328 {
2329 data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
2330 data, end, addr_size);
2331 /* If we printed anything, or if we have an empty piece,
2332 then don't disassemble. */
2333 if (data != here
2334 || data[0] == DW_OP_piece
2335 || data[0] == DW_OP_bit_piece)
2336 disassemble = 0;
2337 }
2338 if (disassemble)
2339 data = disassemble_dwarf_expression (stream, get_objfile_arch (objfile),
2340 addr_size, offset_size, data, end,
2341 dwarf2_always_disassemble);
2342
2343 if (data < end)
2344 {
2345 int empty = data == here;
2346
2347 if (disassemble)
2348 fprintf_filtered (stream, " ");
2349 if (data[0] == DW_OP_piece)
2350 {
2351 ULONGEST bytes;
2352
2353 data = read_uleb128 (data + 1, end, &bytes);
2354
2355 if (empty)
2356 fprintf_filtered (stream, _("an empty %s-byte piece"),
2357 pulongest (bytes));
2358 else
2359 fprintf_filtered (stream, _(" [%s-byte piece]"),
2360 pulongest (bytes));
2361 }
2362 else if (data[0] == DW_OP_bit_piece)
2363 {
2364 ULONGEST bits, offset;
2365
2366 data = read_uleb128 (data + 1, end, &bits);
2367 data = read_uleb128 (data, end, &offset);
2368
2369 if (empty)
2370 fprintf_filtered (stream,
2371 _("an empty %s-bit piece"),
2372 pulongest (bits));
2373 else
2374 fprintf_filtered (stream,
2375 _(" [%s-bit piece, offset %s bits]"),
2376 pulongest (bits), pulongest (offset));
2377 }
2378 else
2379 {
2380 bad = 1;
2381 break;
2382 }
2383 }
2384 }
2385
2386 if (bad || data > end)
2387 error (_("Corrupted DWARF2 expression for \"%s\"."),
2388 SYMBOL_PRINT_NAME (symbol));
2389 }
2390
2391 /* Print a natural-language description of SYMBOL to STREAM. This
2392 version is for a symbol with a single location. */
2393
2394 static void
2395 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
2396 struct ui_file *stream)
2397 {
2398 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2399 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2400 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2401 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2402
2403 locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
2404 objfile, addr_size, offset_size);
2405 }
2406
2407 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2408 any necessary bytecode in AX. */
2409
2410 static void
2411 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2412 struct agent_expr *ax, struct axs_value *value)
2413 {
2414 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2415 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2416
2417 compile_dwarf_to_ax (ax, value, gdbarch, addr_size,
2418 dlbaton->data, dlbaton->data + dlbaton->size,
2419 dlbaton->per_cu);
2420 }
2421
2422 /* The set of location functions used with the DWARF-2 expression
2423 evaluator. */
2424 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
2425 locexpr_read_variable,
2426 locexpr_read_needs_frame,
2427 locexpr_describe_location,
2428 locexpr_tracepoint_var_ref
2429 };
2430
2431
2432 /* Wrapper functions for location lists. These generally find
2433 the appropriate location expression and call something above. */
2434
2435 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2436 evaluator to calculate the location. */
2437 static struct value *
2438 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
2439 {
2440 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2441 struct value *val;
2442 const gdb_byte *data;
2443 size_t size;
2444
2445 data = find_location_expression (dlbaton, &size,
2446 frame ? get_frame_address_in_block (frame)
2447 : 0);
2448 if (data == NULL)
2449 {
2450 val = allocate_value (SYMBOL_TYPE (symbol));
2451 VALUE_LVAL (val) = not_lval;
2452 set_value_optimized_out (val, 1);
2453 }
2454 else
2455 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
2456 dlbaton->per_cu);
2457
2458 return val;
2459 }
2460
2461 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
2462 static int
2463 loclist_read_needs_frame (struct symbol *symbol)
2464 {
2465 /* If there's a location list, then assume we need to have a frame
2466 to choose the appropriate location expression. With tracking of
2467 global variables this is not necessarily true, but such tracking
2468 is disabled in GCC at the moment until we figure out how to
2469 represent it. */
2470
2471 return 1;
2472 }
2473
2474 /* Print a natural-language description of SYMBOL to STREAM. This
2475 version applies when there is a list of different locations, each
2476 with a specified address range. */
2477
2478 static void
2479 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
2480 struct ui_file *stream)
2481 {
2482 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2483 CORE_ADDR low, high;
2484 const gdb_byte *loc_ptr, *buf_end;
2485 int length, first = 1;
2486 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2487 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2488 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2489 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2490 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2491 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2492 /* Adjust base_address for relocatable objects. */
2493 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
2494 SECT_OFF_TEXT (objfile));
2495 CORE_ADDR base_address = dlbaton->base_address + base_offset;
2496
2497 loc_ptr = dlbaton->data;
2498 buf_end = dlbaton->data + dlbaton->size;
2499
2500 fprintf_filtered (stream, _("multi-location:\n"));
2501
2502 /* Iterate through locations until we run out. */
2503 while (1)
2504 {
2505 if (buf_end - loc_ptr < 2 * addr_size)
2506 error (_("Corrupted DWARF expression for symbol \"%s\"."),
2507 SYMBOL_PRINT_NAME (symbol));
2508
2509 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2510 loc_ptr += addr_size;
2511
2512 /* A base-address-selection entry. */
2513 if (low == base_mask)
2514 {
2515 base_address = dwarf2_read_address (gdbarch,
2516 loc_ptr, buf_end, addr_size);
2517 fprintf_filtered (stream, _(" Base address %s"),
2518 paddress (gdbarch, base_address));
2519 loc_ptr += addr_size;
2520 continue;
2521 }
2522
2523 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2524 loc_ptr += addr_size;
2525
2526 /* An end-of-list entry. */
2527 if (low == 0 && high == 0)
2528 break;
2529
2530 /* Otherwise, a location expression entry. */
2531 low += base_address;
2532 high += base_address;
2533
2534 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
2535 loc_ptr += 2;
2536
2537 /* (It would improve readability to print only the minimum
2538 necessary digits of the second number of the range.) */
2539 fprintf_filtered (stream, _(" Range %s-%s: "),
2540 paddress (gdbarch, low), paddress (gdbarch, high));
2541
2542 /* Now describe this particular location. */
2543 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
2544 objfile, addr_size, offset_size);
2545
2546 fprintf_filtered (stream, "\n");
2547
2548 loc_ptr += length;
2549 }
2550 }
2551
2552 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2553 any necessary bytecode in AX. */
2554 static void
2555 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2556 struct agent_expr *ax, struct axs_value *value)
2557 {
2558 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2559 const gdb_byte *data;
2560 size_t size;
2561 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2562
2563 data = find_location_expression (dlbaton, &size, ax->scope);
2564
2565 compile_dwarf_to_ax (ax, value, gdbarch, addr_size, data, data + size,
2566 dlbaton->per_cu);
2567 }
2568
2569 /* The set of location functions used with the DWARF-2 expression
2570 evaluator and location lists. */
2571 const struct symbol_computed_ops dwarf2_loclist_funcs = {
2572 loclist_read_variable,
2573 loclist_read_needs_frame,
2574 loclist_describe_location,
2575 loclist_tracepoint_var_ref
2576 };
This page took 0.089542 seconds and 4 git commands to generate.