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