gdb/
[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 struct piece_closure
236 {
237 /* Reference count. */
238 int refc;
239
240 /* The number of pieces used to describe this variable. */
241 int n_pieces;
242
243 /* The target address size, used only for DWARF_VALUE_STACK. */
244 int addr_size;
245
246 /* The pieces themselves. */
247 struct dwarf_expr_piece *pieces;
248 };
249
250 /* Allocate a closure for a value formed from separately-described
251 PIECES. */
252
253 static struct piece_closure *
254 allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
255 int addr_size)
256 {
257 struct piece_closure *c = XZALLOC (struct piece_closure);
258
259 c->refc = 1;
260 c->n_pieces = n_pieces;
261 c->addr_size = addr_size;
262 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
263
264 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
265
266 return c;
267 }
268
269 /* The lowest-level function to extract bits from a byte buffer.
270 SOURCE is the buffer. It is updated if we read to the end of a
271 byte.
272 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
273 updated to reflect the number of bits actually read.
274 NBITS is the number of bits we want to read. It is updated to
275 reflect the number of bits actually read. This function may read
276 fewer bits.
277 BITS_BIG_ENDIAN is taken directly from gdbarch.
278 This function returns the extracted bits. */
279
280 static unsigned int
281 extract_bits_primitive (const gdb_byte **source,
282 unsigned int *source_offset_bits,
283 int *nbits, int bits_big_endian)
284 {
285 unsigned int avail, mask, datum;
286
287 gdb_assert (*source_offset_bits < 8);
288
289 avail = 8 - *source_offset_bits;
290 if (avail > *nbits)
291 avail = *nbits;
292
293 mask = (1 << avail) - 1;
294 datum = **source;
295 if (bits_big_endian)
296 datum >>= 8 - (*source_offset_bits + *nbits);
297 else
298 datum >>= *source_offset_bits;
299 datum &= mask;
300
301 *nbits -= avail;
302 *source_offset_bits += avail;
303 if (*source_offset_bits >= 8)
304 {
305 *source_offset_bits -= 8;
306 ++*source;
307 }
308
309 return datum;
310 }
311
312 /* Extract some bits from a source buffer and move forward in the
313 buffer.
314
315 SOURCE is the source buffer. It is updated as bytes are read.
316 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
317 bits are read.
318 NBITS is the number of bits to read.
319 BITS_BIG_ENDIAN is taken directly from gdbarch.
320
321 This function returns the bits that were read. */
322
323 static unsigned int
324 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
325 int nbits, int bits_big_endian)
326 {
327 unsigned int datum;
328
329 gdb_assert (nbits > 0 && nbits <= 8);
330
331 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
332 bits_big_endian);
333 if (nbits > 0)
334 {
335 unsigned int more;
336
337 more = extract_bits_primitive (source, source_offset_bits, &nbits,
338 bits_big_endian);
339 if (bits_big_endian)
340 datum <<= nbits;
341 else
342 more <<= nbits;
343 datum |= more;
344 }
345
346 return datum;
347 }
348
349 /* Write some bits into a buffer and move forward in the buffer.
350
351 DATUM is the bits to write. The low-order bits of DATUM are used.
352 DEST is the destination buffer. It is updated as bytes are
353 written.
354 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
355 done.
356 NBITS is the number of valid bits in DATUM.
357 BITS_BIG_ENDIAN is taken directly from gdbarch. */
358
359 static void
360 insert_bits (unsigned int datum,
361 gdb_byte *dest, unsigned int dest_offset_bits,
362 int nbits, int bits_big_endian)
363 {
364 unsigned int mask;
365
366 gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8);
367
368 mask = (1 << nbits) - 1;
369 if (bits_big_endian)
370 {
371 datum <<= 8 - (dest_offset_bits + nbits);
372 mask <<= 8 - (dest_offset_bits + nbits);
373 }
374 else
375 {
376 datum <<= dest_offset_bits;
377 mask <<= dest_offset_bits;
378 }
379
380 gdb_assert ((datum & ~mask) == 0);
381
382 *dest = (*dest & ~mask) | datum;
383 }
384
385 /* Copy bits from a source to a destination.
386
387 DEST is where the bits should be written.
388 DEST_OFFSET_BITS is the bit offset into DEST.
389 SOURCE is the source of bits.
390 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
391 BIT_COUNT is the number of bits to copy.
392 BITS_BIG_ENDIAN is taken directly from gdbarch. */
393
394 static void
395 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
396 const gdb_byte *source, unsigned int source_offset_bits,
397 unsigned int bit_count,
398 int bits_big_endian)
399 {
400 unsigned int dest_avail;
401 int datum;
402
403 /* Reduce everything to byte-size pieces. */
404 dest += dest_offset_bits / 8;
405 dest_offset_bits %= 8;
406 source += source_offset_bits / 8;
407 source_offset_bits %= 8;
408
409 dest_avail = 8 - dest_offset_bits % 8;
410
411 /* See if we can fill the first destination byte. */
412 if (dest_avail < bit_count)
413 {
414 datum = extract_bits (&source, &source_offset_bits, dest_avail,
415 bits_big_endian);
416 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
417 ++dest;
418 dest_offset_bits = 0;
419 bit_count -= dest_avail;
420 }
421
422 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
423 than 8 bits remaining. */
424 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
425 for (; bit_count >= 8; bit_count -= 8)
426 {
427 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
428 *dest++ = (gdb_byte) datum;
429 }
430
431 /* Finally, we may have a few leftover bits. */
432 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
433 if (bit_count > 0)
434 {
435 datum = extract_bits (&source, &source_offset_bits, bit_count,
436 bits_big_endian);
437 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
438 }
439 }
440
441 static void
442 read_pieced_value (struct value *v)
443 {
444 int i;
445 long offset = 0;
446 ULONGEST bits_to_skip;
447 gdb_byte *contents;
448 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
449 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
450 size_t type_len;
451 size_t buffer_size = 0;
452 char *buffer = NULL;
453 struct cleanup *cleanup;
454 int bits_big_endian
455 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
456
457 if (value_type (v) != value_enclosing_type (v))
458 internal_error (__FILE__, __LINE__,
459 _("Should not be able to create a lazy value with "
460 "an enclosing type"));
461
462 cleanup = make_cleanup (free_current_contents, &buffer);
463
464 contents = value_contents_raw (v);
465 bits_to_skip = 8 * value_offset (v);
466 type_len = 8 * TYPE_LENGTH (value_type (v));
467
468 for (i = 0; i < c->n_pieces && offset < type_len; i++)
469 {
470 struct dwarf_expr_piece *p = &c->pieces[i];
471 size_t this_size, this_size_bits;
472 long dest_offset_bits, source_offset_bits, source_offset;
473 const gdb_byte *intermediate_buffer;
474
475 /* Compute size, source, and destination offsets for copying, in
476 bits. */
477 this_size_bits = p->size;
478 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
479 {
480 bits_to_skip -= this_size_bits;
481 continue;
482 }
483 if (this_size_bits > type_len - offset)
484 this_size_bits = type_len - offset;
485 if (bits_to_skip > 0)
486 {
487 dest_offset_bits = 0;
488 source_offset_bits = bits_to_skip;
489 this_size_bits -= bits_to_skip;
490 bits_to_skip = 0;
491 }
492 else
493 {
494 dest_offset_bits = offset;
495 source_offset_bits = 0;
496 }
497
498 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
499 source_offset = source_offset_bits / 8;
500 if (buffer_size < this_size)
501 {
502 buffer_size = this_size;
503 buffer = xrealloc (buffer, buffer_size);
504 }
505 intermediate_buffer = buffer;
506
507 /* Copy from the source to DEST_BUFFER. */
508 switch (p->location)
509 {
510 case DWARF_VALUE_REGISTER:
511 {
512 struct gdbarch *arch = get_frame_arch (frame);
513 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
514 p->v.expr.value);
515 int reg_offset = source_offset;
516
517 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
518 && this_size < register_size (arch, gdb_regnum))
519 {
520 /* Big-endian, and we want less than full size. */
521 reg_offset = register_size (arch, gdb_regnum) - this_size;
522 /* We want the lower-order THIS_SIZE_BITS of the bytes
523 we extract from the register. */
524 source_offset_bits += 8 * this_size - this_size_bits;
525 }
526
527 if (gdb_regnum != -1)
528 {
529 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
530 this_size, buffer);
531 }
532 else
533 {
534 error (_("Unable to access DWARF register number %s"),
535 paddress (arch, p->v.expr.value));
536 }
537 }
538 break;
539
540 case DWARF_VALUE_MEMORY:
541 if (p->v.expr.in_stack_memory)
542 read_stack (p->v.expr.value + source_offset, buffer, this_size);
543 else
544 read_memory (p->v.expr.value + source_offset, buffer, this_size);
545 break;
546
547 case DWARF_VALUE_STACK:
548 {
549 struct gdbarch *gdbarch = get_type_arch (value_type (v));
550 size_t n = this_size;
551
552 if (n > c->addr_size - source_offset)
553 n = (c->addr_size >= source_offset
554 ? c->addr_size - source_offset
555 : 0);
556 if (n == 0)
557 {
558 /* Nothing. */
559 }
560 else if (source_offset == 0)
561 store_unsigned_integer (buffer, n,
562 gdbarch_byte_order (gdbarch),
563 p->v.expr.value);
564 else
565 {
566 gdb_byte bytes[sizeof (ULONGEST)];
567
568 store_unsigned_integer (bytes, n + source_offset,
569 gdbarch_byte_order (gdbarch),
570 p->v.expr.value);
571 memcpy (buffer, bytes + source_offset, n);
572 }
573 }
574 break;
575
576 case DWARF_VALUE_LITERAL:
577 {
578 size_t n = this_size;
579
580 if (n > p->v.literal.length - source_offset)
581 n = (p->v.literal.length >= source_offset
582 ? p->v.literal.length - source_offset
583 : 0);
584 if (n != 0)
585 intermediate_buffer = p->v.literal.data + source_offset;
586 }
587 break;
588
589 case DWARF_VALUE_OPTIMIZED_OUT:
590 /* We just leave the bits empty for now. This is not ideal
591 but gdb currently does not have a nice way to represent
592 optimized-out pieces. */
593 warning (_("bits %ld-%ld in computed object were optimized out; "
594 "replacing with zeroes"),
595 offset,
596 offset + (long) this_size_bits);
597 break;
598
599 default:
600 internal_error (__FILE__, __LINE__, _("invalid location type"));
601 }
602
603 if (p->location != DWARF_VALUE_OPTIMIZED_OUT)
604 copy_bitwise (contents, dest_offset_bits,
605 intermediate_buffer, source_offset_bits % 8,
606 this_size_bits, bits_big_endian);
607
608 offset += this_size_bits;
609 }
610
611 do_cleanups (cleanup);
612 }
613
614 static void
615 write_pieced_value (struct value *to, struct value *from)
616 {
617 int i;
618 long offset = 0;
619 ULONGEST bits_to_skip;
620 const gdb_byte *contents;
621 struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
622 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
623 size_t type_len;
624 size_t buffer_size = 0;
625 char *buffer = NULL;
626 struct cleanup *cleanup;
627 int bits_big_endian
628 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
629
630 if (frame == NULL)
631 {
632 set_value_optimized_out (to, 1);
633 return;
634 }
635
636 cleanup = make_cleanup (free_current_contents, &buffer);
637
638 contents = value_contents (from);
639 bits_to_skip = 8 * value_offset (to);
640 type_len = 8 * TYPE_LENGTH (value_type (to));
641 for (i = 0; i < c->n_pieces && offset < type_len; i++)
642 {
643 struct dwarf_expr_piece *p = &c->pieces[i];
644 size_t this_size_bits, this_size;
645 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
646 int need_bitwise;
647 const gdb_byte *source_buffer;
648
649 this_size_bits = p->size;
650 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
651 {
652 bits_to_skip -= this_size_bits;
653 continue;
654 }
655 if (this_size_bits > type_len - offset)
656 this_size_bits = type_len - offset;
657 if (bits_to_skip > 0)
658 {
659 dest_offset_bits = bits_to_skip;
660 source_offset_bits = 0;
661 this_size_bits -= bits_to_skip;
662 bits_to_skip = 0;
663 }
664 else
665 {
666 dest_offset_bits = 0;
667 source_offset_bits = offset;
668 }
669
670 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
671 source_offset = source_offset_bits / 8;
672 dest_offset = dest_offset_bits / 8;
673 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
674 {
675 source_buffer = contents + source_offset;
676 need_bitwise = 0;
677 }
678 else
679 {
680 if (buffer_size < this_size)
681 {
682 buffer_size = this_size;
683 buffer = xrealloc (buffer, buffer_size);
684 }
685 source_buffer = buffer;
686 need_bitwise = 1;
687 }
688
689 switch (p->location)
690 {
691 case DWARF_VALUE_REGISTER:
692 {
693 struct gdbarch *arch = get_frame_arch (frame);
694 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
695 int reg_offset = dest_offset;
696
697 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
698 && this_size <= register_size (arch, gdb_regnum))
699 /* Big-endian, and we want less than full size. */
700 reg_offset = register_size (arch, gdb_regnum) - this_size;
701
702 if (gdb_regnum != -1)
703 {
704 if (need_bitwise)
705 {
706 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
707 this_size, buffer);
708 copy_bitwise (buffer, dest_offset_bits,
709 contents, source_offset_bits,
710 this_size_bits,
711 bits_big_endian);
712 }
713
714 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
715 this_size, source_buffer);
716 }
717 else
718 {
719 error (_("Unable to write to DWARF register number %s"),
720 paddress (arch, p->v.expr.value));
721 }
722 }
723 break;
724 case DWARF_VALUE_MEMORY:
725 if (need_bitwise)
726 {
727 /* Only the first and last bytes can possibly have any
728 bits reused. */
729 read_memory (p->v.expr.value + dest_offset, buffer, 1);
730 read_memory (p->v.expr.value + dest_offset + this_size - 1,
731 buffer + this_size - 1, 1);
732 copy_bitwise (buffer, dest_offset_bits,
733 contents, source_offset_bits,
734 this_size_bits,
735 bits_big_endian);
736 }
737
738 write_memory (p->v.expr.value + dest_offset,
739 source_buffer, this_size);
740 break;
741 default:
742 set_value_optimized_out (to, 1);
743 goto done;
744 }
745 offset += this_size_bits;
746 }
747
748 done:
749 do_cleanups (cleanup);
750 }
751
752 static void *
753 copy_pieced_value_closure (struct value *v)
754 {
755 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
756
757 ++c->refc;
758 return c;
759 }
760
761 static void
762 free_pieced_value_closure (struct value *v)
763 {
764 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
765
766 --c->refc;
767 if (c->refc == 0)
768 {
769 xfree (c->pieces);
770 xfree (c);
771 }
772 }
773
774 /* Functions for accessing a variable described by DW_OP_piece. */
775 static struct lval_funcs pieced_value_funcs = {
776 read_pieced_value,
777 write_pieced_value,
778 copy_pieced_value_closure,
779 free_pieced_value_closure
780 };
781
782 /* Evaluate a location description, starting at DATA and with length
783 SIZE, to find the current location of variable of TYPE in the context
784 of FRAME. */
785
786 static struct value *
787 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
788 const gdb_byte *data, unsigned short size,
789 struct dwarf2_per_cu_data *per_cu)
790 {
791 struct value *retval;
792 struct dwarf_expr_baton baton;
793 struct dwarf_expr_context *ctx;
794 struct cleanup *old_chain;
795
796 if (size == 0)
797 {
798 retval = allocate_value (type);
799 VALUE_LVAL (retval) = not_lval;
800 set_value_optimized_out (retval, 1);
801 return retval;
802 }
803
804 baton.frame = frame;
805 baton.per_cu = per_cu;
806
807 ctx = new_dwarf_expr_context ();
808 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
809
810 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
811 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
812 ctx->baton = &baton;
813 ctx->read_reg = dwarf_expr_read_reg;
814 ctx->read_mem = dwarf_expr_read_mem;
815 ctx->get_frame_base = dwarf_expr_frame_base;
816 ctx->get_frame_cfa = dwarf_expr_frame_cfa;
817 ctx->get_tls_address = dwarf_expr_tls_address;
818
819 dwarf_expr_eval (ctx, data, size);
820 if (ctx->num_pieces > 0)
821 {
822 struct piece_closure *c;
823 struct frame_id frame_id = get_frame_id (frame);
824
825 c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
826 ctx->addr_size);
827 retval = allocate_computed_value (type, &pieced_value_funcs, c);
828 VALUE_FRAME_ID (retval) = frame_id;
829 }
830 else
831 {
832 switch (ctx->location)
833 {
834 case DWARF_VALUE_REGISTER:
835 {
836 struct gdbarch *arch = get_frame_arch (frame);
837 CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
838 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
839
840 if (gdb_regnum != -1)
841 retval = value_from_register (type, gdb_regnum, frame);
842 else
843 error (_("Unable to access DWARF register number %s"),
844 paddress (arch, dwarf_regnum));
845 }
846 break;
847
848 case DWARF_VALUE_MEMORY:
849 {
850 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
851 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
852
853 retval = allocate_value (type);
854 VALUE_LVAL (retval) = lval_memory;
855 set_value_lazy (retval, 1);
856 if (in_stack_memory)
857 set_value_stack (retval, 1);
858 set_value_address (retval, address);
859 }
860 break;
861
862 case DWARF_VALUE_STACK:
863 {
864 ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
865 bfd_byte *contents;
866 size_t n = ctx->addr_size;
867
868 retval = allocate_value (type);
869 contents = value_contents_raw (retval);
870 if (n > TYPE_LENGTH (type))
871 n = TYPE_LENGTH (type);
872 store_unsigned_integer (contents, n,
873 gdbarch_byte_order (ctx->gdbarch),
874 value);
875 }
876 break;
877
878 case DWARF_VALUE_LITERAL:
879 {
880 bfd_byte *contents;
881 size_t n = ctx->len;
882
883 retval = allocate_value (type);
884 contents = value_contents_raw (retval);
885 if (n > TYPE_LENGTH (type))
886 n = TYPE_LENGTH (type);
887 memcpy (contents, ctx->data, n);
888 }
889 break;
890
891 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
892 it can only be encountered when making a piece. */
893 case DWARF_VALUE_OPTIMIZED_OUT:
894 default:
895 internal_error (__FILE__, __LINE__, _("invalid location type"));
896 }
897 }
898
899 set_value_initialized (retval, ctx->initialized);
900
901 do_cleanups (old_chain);
902
903 return retval;
904 }
905 \f
906 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
907
908 struct needs_frame_baton
909 {
910 int needs_frame;
911 struct dwarf2_per_cu_data *per_cu;
912 };
913
914 /* Reads from registers do require a frame. */
915 static CORE_ADDR
916 needs_frame_read_reg (void *baton, int regnum)
917 {
918 struct needs_frame_baton *nf_baton = baton;
919
920 nf_baton->needs_frame = 1;
921 return 1;
922 }
923
924 /* Reads from memory do not require a frame. */
925 static void
926 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
927 {
928 memset (buf, 0, len);
929 }
930
931 /* Frame-relative accesses do require a frame. */
932 static void
933 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
934 {
935 static gdb_byte lit0 = DW_OP_lit0;
936 struct needs_frame_baton *nf_baton = baton;
937
938 *start = &lit0;
939 *length = 1;
940
941 nf_baton->needs_frame = 1;
942 }
943
944 /* CFA accesses require a frame. */
945
946 static CORE_ADDR
947 needs_frame_frame_cfa (void *baton)
948 {
949 struct needs_frame_baton *nf_baton = baton;
950
951 nf_baton->needs_frame = 1;
952 return 1;
953 }
954
955 /* Thread-local accesses do require a frame. */
956 static CORE_ADDR
957 needs_frame_tls_address (void *baton, CORE_ADDR offset)
958 {
959 struct needs_frame_baton *nf_baton = baton;
960
961 nf_baton->needs_frame = 1;
962 return 1;
963 }
964
965 /* Return non-zero iff the location expression at DATA (length SIZE)
966 requires a frame to evaluate. */
967
968 static int
969 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
970 struct dwarf2_per_cu_data *per_cu)
971 {
972 struct needs_frame_baton baton;
973 struct dwarf_expr_context *ctx;
974 int in_reg;
975 struct cleanup *old_chain;
976
977 baton.needs_frame = 0;
978 baton.per_cu = per_cu;
979
980 ctx = new_dwarf_expr_context ();
981 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
982
983 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
984 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
985 ctx->baton = &baton;
986 ctx->read_reg = needs_frame_read_reg;
987 ctx->read_mem = needs_frame_read_mem;
988 ctx->get_frame_base = needs_frame_frame_base;
989 ctx->get_frame_cfa = needs_frame_frame_cfa;
990 ctx->get_tls_address = needs_frame_tls_address;
991
992 dwarf_expr_eval (ctx, data, size);
993
994 in_reg = ctx->location == DWARF_VALUE_REGISTER;
995
996 if (ctx->num_pieces > 0)
997 {
998 int i;
999
1000 /* If the location has several pieces, and any of them are in
1001 registers, then we will need a frame to fetch them from. */
1002 for (i = 0; i < ctx->num_pieces; i++)
1003 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
1004 in_reg = 1;
1005 }
1006
1007 do_cleanups (old_chain);
1008
1009 return baton.needs_frame || in_reg;
1010 }
1011
1012 /* This struct keeps track of the pieces that make up a multi-location
1013 object, for use in agent expression generation. It is
1014 superficially similar to struct dwarf_expr_piece, but
1015 dwarf_expr_piece is designed for use in immediate evaluation, and
1016 does not, for example, have a way to record both base register and
1017 offset. */
1018
1019 struct axs_var_loc
1020 {
1021 /* Memory vs register, etc */
1022 enum axs_lvalue_kind kind;
1023
1024 /* If non-zero, number of bytes in this fragment */
1025 unsigned bytes;
1026
1027 /* (GDB-numbered) reg, or base reg if >= 0 */
1028 int reg;
1029
1030 /* offset from reg */
1031 LONGEST offset;
1032 };
1033
1034 static const gdb_byte *
1035 dwarf2_tracepoint_var_loc (struct symbol *symbol,
1036 struct agent_expr *ax,
1037 struct axs_var_loc *loc,
1038 struct gdbarch *gdbarch,
1039 const gdb_byte *data, const gdb_byte *end)
1040 {
1041 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1042 {
1043 loc->kind = axs_lvalue_register;
1044 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1045 data += 1;
1046 }
1047 else if (data[0] == DW_OP_regx)
1048 {
1049 ULONGEST reg;
1050
1051 data = read_uleb128 (data + 1, end, &reg);
1052 loc->kind = axs_lvalue_register;
1053 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1054 }
1055 else if (data[0] == DW_OP_fbreg)
1056 {
1057 struct block *b;
1058 struct symbol *framefunc;
1059 int frame_reg = 0;
1060 LONGEST frame_offset;
1061 const gdb_byte *base_data;
1062 size_t base_size;
1063 LONGEST base_offset = 0;
1064
1065 b = block_for_pc (ax->scope);
1066
1067 if (!b)
1068 error (_("No block found for address"));
1069
1070 framefunc = block_linkage_function (b);
1071
1072 if (!framefunc)
1073 error (_("No function found for block"));
1074
1075 dwarf_expr_frame_base_1 (framefunc, ax->scope,
1076 &base_data, &base_size);
1077
1078 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1079 {
1080 const gdb_byte *buf_end;
1081
1082 frame_reg = base_data[0] - DW_OP_breg0;
1083 buf_end = read_sleb128 (base_data + 1,
1084 base_data + base_size, &base_offset);
1085 if (buf_end != base_data + base_size)
1086 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1087 frame_reg, SYMBOL_PRINT_NAME (symbol));
1088 }
1089 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1090 {
1091 /* The frame base is just the register, with no offset. */
1092 frame_reg = base_data[0] - DW_OP_reg0;
1093 base_offset = 0;
1094 }
1095 else
1096 {
1097 /* We don't know what to do with the frame base expression,
1098 so we can't trace this variable; give up. */
1099 error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled, first opcode in base data is 0x%x."),
1100 SYMBOL_PRINT_NAME (symbol), base_data[0]);
1101 }
1102
1103 data = read_sleb128 (data + 1, end, &frame_offset);
1104
1105 loc->kind = axs_lvalue_memory;
1106 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1107 loc->offset = base_offset + frame_offset;
1108 }
1109 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
1110 {
1111 unsigned int reg;
1112 LONGEST offset;
1113
1114 reg = data[0] - DW_OP_breg0;
1115 data = read_sleb128 (data + 1, end, &offset);
1116
1117 loc->kind = axs_lvalue_memory;
1118 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1119 loc->offset = offset;
1120 }
1121 else
1122 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
1123 data[0], SYMBOL_PRINT_NAME (symbol));
1124
1125 return data;
1126 }
1127
1128 /* Given the location of a piece, issue bytecodes that will access it. */
1129
1130 static void
1131 dwarf2_tracepoint_var_access (struct agent_expr *ax,
1132 struct axs_value *value,
1133 struct axs_var_loc *loc)
1134 {
1135 value->kind = loc->kind;
1136
1137 switch (loc->kind)
1138 {
1139 case axs_lvalue_register:
1140 value->u.reg = loc->reg;
1141 break;
1142
1143 case axs_lvalue_memory:
1144 ax_reg (ax, loc->reg);
1145 if (loc->offset)
1146 {
1147 ax_const_l (ax, loc->offset);
1148 ax_simple (ax, aop_add);
1149 }
1150 break;
1151
1152 default:
1153 internal_error (__FILE__, __LINE__, _("Unhandled value kind in dwarf2_tracepoint_var_access"));
1154 }
1155 }
1156
1157 static void
1158 dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1159 struct agent_expr *ax, struct axs_value *value,
1160 const gdb_byte *data, int size)
1161 {
1162 const gdb_byte *end = data + size;
1163 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1164 /* In practice, a variable is not going to be spread across
1165 dozens of registers or memory locations. If someone comes up
1166 with a real-world example, revisit this. */
1167 #define MAX_FRAGS 16
1168 struct axs_var_loc fragments[MAX_FRAGS];
1169 int nfrags = 0, frag;
1170 int length = 0;
1171 int piece_ok = 0;
1172 int bad = 0;
1173 int first = 1;
1174
1175 if (!data || size == 0)
1176 {
1177 value->optimized_out = 1;
1178 return;
1179 }
1180
1181 while (data < end)
1182 {
1183 if (!piece_ok)
1184 {
1185 if (nfrags == MAX_FRAGS)
1186 error (_("Too many pieces in location for \"%s\"."),
1187 SYMBOL_PRINT_NAME (symbol));
1188
1189 fragments[nfrags].bytes = 0;
1190 data = dwarf2_tracepoint_var_loc (symbol, ax, &fragments[nfrags],
1191 gdbarch, data, end);
1192 nfrags++;
1193 piece_ok = 1;
1194 }
1195 else if (data[0] == DW_OP_piece)
1196 {
1197 ULONGEST bytes;
1198
1199 data = read_uleb128 (data + 1, end, &bytes);
1200 /* Only deal with 4 byte fragments for now. */
1201 if (bytes != 4)
1202 error (_("DW_OP_piece %s not supported in location for \"%s\"."),
1203 pulongest (bytes), SYMBOL_PRINT_NAME (symbol));
1204 fragments[nfrags - 1].bytes = bytes;
1205 length += bytes;
1206 piece_ok = 0;
1207 }
1208 else
1209 {
1210 bad = 1;
1211 break;
1212 }
1213 }
1214
1215 if (bad || data > end)
1216 error (_("Corrupted DWARF expression for \"%s\"."),
1217 SYMBOL_PRINT_NAME (symbol));
1218
1219 /* If single expression, no pieces, convert to external format. */
1220 if (length == 0)
1221 {
1222 dwarf2_tracepoint_var_access (ax, value, &fragments[0]);
1223 return;
1224 }
1225
1226 if (length != TYPE_LENGTH (value->type))
1227 error (_("Inconsistent piece information for \"%s\"."),
1228 SYMBOL_PRINT_NAME (symbol));
1229
1230 /* Emit bytecodes to assemble the pieces into a single stack entry. */
1231
1232 for ((frag = (byte_order == BFD_ENDIAN_BIG ? 0 : nfrags - 1));
1233 nfrags--;
1234 (frag += (byte_order == BFD_ENDIAN_BIG ? 1 : -1)))
1235 {
1236 if (!first)
1237 {
1238 /* shift the previous fragment up 32 bits */
1239 ax_const_l (ax, 32);
1240 ax_simple (ax, aop_lsh);
1241 }
1242
1243 dwarf2_tracepoint_var_access (ax, value, &fragments[frag]);
1244
1245 switch (value->kind)
1246 {
1247 case axs_lvalue_register:
1248 ax_reg (ax, value->u.reg);
1249 break;
1250
1251 case axs_lvalue_memory:
1252 {
1253 extern int trace_kludge; /* Ugh. */
1254
1255 gdb_assert (fragments[frag].bytes == 4);
1256 if (trace_kludge)
1257 ax_trace_quick (ax, 4);
1258 ax_simple (ax, aop_ref32);
1259 }
1260 break;
1261 }
1262
1263 if (!first)
1264 {
1265 /* or the new fragment into the previous */
1266 ax_zero_ext (ax, 32);
1267 ax_simple (ax, aop_bit_or);
1268 }
1269 first = 0;
1270 }
1271 value->kind = axs_rvalue;
1272 }
1273
1274 \f
1275 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1276 evaluator to calculate the location. */
1277 static struct value *
1278 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
1279 {
1280 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1281 struct value *val;
1282
1283 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
1284 dlbaton->size, dlbaton->per_cu);
1285
1286 return val;
1287 }
1288
1289 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1290 static int
1291 locexpr_read_needs_frame (struct symbol *symbol)
1292 {
1293 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1294
1295 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
1296 dlbaton->per_cu);
1297 }
1298
1299 /* Return true if DATA points to the end of a piece. END is one past
1300 the last byte in the expression. */
1301
1302 static int
1303 piece_end_p (const gdb_byte *data, const gdb_byte *end)
1304 {
1305 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
1306 }
1307
1308 /* Nicely describe a single piece of a location, returning an updated
1309 position in the bytecode sequence. This function cannot recognize
1310 all locations; if a location is not recognized, it simply returns
1311 DATA. */
1312
1313 static const gdb_byte *
1314 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
1315 CORE_ADDR addr, struct objfile *objfile,
1316 const gdb_byte *data, const gdb_byte *end,
1317 unsigned int addr_size)
1318 {
1319 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1320 int regno;
1321
1322 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1323 {
1324 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1325 fprintf_filtered (stream, _("a variable in $%s"),
1326 gdbarch_register_name (gdbarch, regno));
1327 data += 1;
1328 }
1329 else if (data[0] == DW_OP_regx)
1330 {
1331 ULONGEST reg;
1332
1333 data = read_uleb128 (data + 1, end, &reg);
1334 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1335 fprintf_filtered (stream, _("a variable in $%s"),
1336 gdbarch_register_name (gdbarch, regno));
1337 }
1338 else if (data[0] == DW_OP_fbreg)
1339 {
1340 struct block *b;
1341 struct symbol *framefunc;
1342 int frame_reg = 0;
1343 LONGEST frame_offset;
1344 const gdb_byte *base_data, *new_data;
1345 size_t base_size;
1346 LONGEST base_offset = 0;
1347
1348 new_data = read_sleb128 (data + 1, end, &frame_offset);
1349 if (!piece_end_p (new_data, end))
1350 return data;
1351 data = new_data;
1352
1353 b = block_for_pc (addr);
1354
1355 if (!b)
1356 error (_("No block found for address for symbol \"%s\"."),
1357 SYMBOL_PRINT_NAME (symbol));
1358
1359 framefunc = block_linkage_function (b);
1360
1361 if (!framefunc)
1362 error (_("No function found for block for symbol \"%s\"."),
1363 SYMBOL_PRINT_NAME (symbol));
1364
1365 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
1366
1367 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1368 {
1369 const gdb_byte *buf_end;
1370
1371 frame_reg = base_data[0] - DW_OP_breg0;
1372 buf_end = read_sleb128 (base_data + 1,
1373 base_data + base_size, &base_offset);
1374 if (buf_end != base_data + base_size)
1375 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1376 frame_reg, SYMBOL_PRINT_NAME (symbol));
1377 }
1378 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1379 {
1380 /* The frame base is just the register, with no offset. */
1381 frame_reg = base_data[0] - DW_OP_reg0;
1382 base_offset = 0;
1383 }
1384 else
1385 {
1386 /* We don't know what to do with the frame base expression,
1387 so we can't trace this variable; give up. */
1388 error (_("Cannot describe location of symbol \"%s\"; "
1389 "DWARF 2 encoding not handled, "
1390 "first opcode in base data is 0x%x."),
1391 SYMBOL_PRINT_NAME (symbol), base_data[0]);
1392 }
1393
1394 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1395
1396 fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1397 gdbarch_register_name (gdbarch, regno),
1398 plongest (base_offset), plongest (frame_offset));
1399 }
1400 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
1401 && piece_end_p (data, end))
1402 {
1403 LONGEST offset;
1404
1405 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1406
1407 data = read_sleb128 (data + 1, end, &offset);
1408
1409 fprintf_filtered (stream,
1410 _("a variable at offset %s from base reg $%s"),
1411 plongest (offset),
1412 gdbarch_register_name (gdbarch, regno));
1413 }
1414
1415 /* The location expression for a TLS variable looks like this (on a
1416 64-bit LE machine):
1417
1418 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1419 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1420
1421 0x3 is the encoding for DW_OP_addr, which has an operand as long
1422 as the size of an address on the target machine (here is 8
1423 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1424 The operand represents the offset at which the variable is within
1425 the thread local storage. */
1426
1427 else if (data + 1 + addr_size < end
1428 && data[0] == DW_OP_addr
1429 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
1430 && piece_end_p (data + 2 + addr_size, end))
1431 {
1432 CORE_ADDR offset = dwarf2_read_address (gdbarch,
1433 data + 1,
1434 end,
1435 addr_size);
1436
1437 fprintf_filtered (stream,
1438 _("a thread-local variable at offset %s "
1439 "in the thread-local storage for `%s'"),
1440 paddress (gdbarch, offset), objfile->name);
1441
1442 data += 1 + addr_size + 1;
1443 }
1444 else if (data[0] >= DW_OP_lit0
1445 && data[0] <= DW_OP_lit31
1446 && data + 1 < end
1447 && data[1] == DW_OP_stack_value)
1448 {
1449 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
1450 data += 2;
1451 }
1452
1453 return data;
1454 }
1455
1456 /* Disassemble an expression, stopping at the end of a piece or at the
1457 end of the expression. Returns a pointer to the next unread byte
1458 in the input expression. If ALL is nonzero, then this function
1459 will keep going until it reaches the end of the expression. */
1460
1461 static const gdb_byte *
1462 disassemble_dwarf_expression (struct ui_file *stream,
1463 struct gdbarch *arch, unsigned int addr_size,
1464 int offset_size,
1465 const gdb_byte *data, const gdb_byte *end,
1466 int all)
1467 {
1468 const gdb_byte *start = data;
1469
1470 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
1471
1472 while (data < end
1473 && (all
1474 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
1475 {
1476 enum dwarf_location_atom op = *data++;
1477 CORE_ADDR addr;
1478 ULONGEST ul;
1479 LONGEST l;
1480 const char *name;
1481
1482 name = dwarf_stack_op_name (op, 0);
1483
1484 if (!name)
1485 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
1486 op, (long) (data - start));
1487 fprintf_filtered (stream, " % 4ld: %s", (long) (data - start), name);
1488
1489 switch (op)
1490 {
1491 case DW_OP_addr:
1492 addr = dwarf2_read_address (arch, data, end, addr_size);
1493 data += addr_size;
1494 fprintf_filtered (stream, " %s", paddress (arch, addr));
1495 break;
1496
1497 case DW_OP_const1u:
1498 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
1499 data += 1;
1500 fprintf_filtered (stream, " %s", pulongest (ul));
1501 break;
1502 case DW_OP_const1s:
1503 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
1504 data += 1;
1505 fprintf_filtered (stream, " %s", plongest (l));
1506 break;
1507 case DW_OP_const2u:
1508 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
1509 data += 2;
1510 fprintf_filtered (stream, " %s", pulongest (ul));
1511 break;
1512 case DW_OP_const2s:
1513 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1514 data += 2;
1515 fprintf_filtered (stream, " %s", plongest (l));
1516 break;
1517 case DW_OP_const4u:
1518 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
1519 data += 4;
1520 fprintf_filtered (stream, " %s", pulongest (ul));
1521 break;
1522 case DW_OP_const4s:
1523 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
1524 data += 4;
1525 fprintf_filtered (stream, " %s", plongest (l));
1526 break;
1527 case DW_OP_const8u:
1528 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
1529 data += 8;
1530 fprintf_filtered (stream, " %s", pulongest (ul));
1531 break;
1532 case DW_OP_const8s:
1533 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
1534 data += 8;
1535 fprintf_filtered (stream, " %s", plongest (l));
1536 break;
1537 case DW_OP_constu:
1538 data = read_uleb128 (data, end, &ul);
1539 fprintf_filtered (stream, " %s", pulongest (ul));
1540 break;
1541 case DW_OP_consts:
1542 data = read_sleb128 (data, end, &l);
1543 fprintf_filtered (stream, " %s", plongest (l));
1544 break;
1545
1546 case DW_OP_reg0:
1547 case DW_OP_reg1:
1548 case DW_OP_reg2:
1549 case DW_OP_reg3:
1550 case DW_OP_reg4:
1551 case DW_OP_reg5:
1552 case DW_OP_reg6:
1553 case DW_OP_reg7:
1554 case DW_OP_reg8:
1555 case DW_OP_reg9:
1556 case DW_OP_reg10:
1557 case DW_OP_reg11:
1558 case DW_OP_reg12:
1559 case DW_OP_reg13:
1560 case DW_OP_reg14:
1561 case DW_OP_reg15:
1562 case DW_OP_reg16:
1563 case DW_OP_reg17:
1564 case DW_OP_reg18:
1565 case DW_OP_reg19:
1566 case DW_OP_reg20:
1567 case DW_OP_reg21:
1568 case DW_OP_reg22:
1569 case DW_OP_reg23:
1570 case DW_OP_reg24:
1571 case DW_OP_reg25:
1572 case DW_OP_reg26:
1573 case DW_OP_reg27:
1574 case DW_OP_reg28:
1575 case DW_OP_reg29:
1576 case DW_OP_reg30:
1577 case DW_OP_reg31:
1578 fprintf_filtered (stream, " [$%s]",
1579 gdbarch_register_name (arch, op - DW_OP_reg0));
1580 break;
1581
1582 case DW_OP_regx:
1583 data = read_uleb128 (data, end, &ul);
1584 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
1585 gdbarch_register_name (arch, (int) ul));
1586 break;
1587
1588 case DW_OP_implicit_value:
1589 data = read_uleb128 (data, end, &ul);
1590 data += ul;
1591 fprintf_filtered (stream, " %s", pulongest (ul));
1592 break;
1593
1594 case DW_OP_breg0:
1595 case DW_OP_breg1:
1596 case DW_OP_breg2:
1597 case DW_OP_breg3:
1598 case DW_OP_breg4:
1599 case DW_OP_breg5:
1600 case DW_OP_breg6:
1601 case DW_OP_breg7:
1602 case DW_OP_breg8:
1603 case DW_OP_breg9:
1604 case DW_OP_breg10:
1605 case DW_OP_breg11:
1606 case DW_OP_breg12:
1607 case DW_OP_breg13:
1608 case DW_OP_breg14:
1609 case DW_OP_breg15:
1610 case DW_OP_breg16:
1611 case DW_OP_breg17:
1612 case DW_OP_breg18:
1613 case DW_OP_breg19:
1614 case DW_OP_breg20:
1615 case DW_OP_breg21:
1616 case DW_OP_breg22:
1617 case DW_OP_breg23:
1618 case DW_OP_breg24:
1619 case DW_OP_breg25:
1620 case DW_OP_breg26:
1621 case DW_OP_breg27:
1622 case DW_OP_breg28:
1623 case DW_OP_breg29:
1624 case DW_OP_breg30:
1625 case DW_OP_breg31:
1626 data = read_sleb128 (data, end, &ul);
1627 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
1628 gdbarch_register_name (arch, op - DW_OP_breg0));
1629 break;
1630
1631 case DW_OP_bregx:
1632 {
1633 ULONGEST offset;
1634
1635 data = read_uleb128 (data, end, &ul);
1636 data = read_sleb128 (data, end, &offset);
1637 fprintf_filtered (stream, " register %s [$%s] offset %s",
1638 pulongest (ul),
1639 gdbarch_register_name (arch, (int) ul),
1640 pulongest (offset));
1641 }
1642 break;
1643
1644 case DW_OP_fbreg:
1645 data = read_sleb128 (data, end, &ul);
1646 fprintf_filtered (stream, " %s", pulongest (ul));
1647 break;
1648
1649 case DW_OP_xderef_size:
1650 case DW_OP_deref_size:
1651 case DW_OP_pick:
1652 fprintf_filtered (stream, " %d", *data);
1653 ++data;
1654 break;
1655
1656 case DW_OP_plus_uconst:
1657 data = read_uleb128 (data, end, &ul);
1658 fprintf_filtered (stream, " %s", pulongest (ul));
1659 break;
1660
1661 case DW_OP_skip:
1662 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1663 data += 2;
1664 fprintf_filtered (stream, " to %ld",
1665 (long) (data + l - start));
1666 break;
1667
1668 case DW_OP_bra:
1669 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1670 data += 2;
1671 fprintf_filtered (stream, " %ld",
1672 (long) (data + l - start));
1673 break;
1674
1675 case DW_OP_call2:
1676 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
1677 data += 2;
1678 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
1679 break;
1680
1681 case DW_OP_call4:
1682 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
1683 data += 4;
1684 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
1685 break;
1686
1687 case DW_OP_call_ref:
1688 ul = extract_unsigned_integer (data, offset_size,
1689 gdbarch_byte_order (arch));
1690 data += offset_size;
1691 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
1692 break;
1693
1694 case DW_OP_piece:
1695 data = read_uleb128 (data, end, &ul);
1696 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
1697 break;
1698
1699 case DW_OP_bit_piece:
1700 {
1701 ULONGEST offset;
1702
1703 data = read_uleb128 (data, end, &ul);
1704 data = read_uleb128 (data, end, &offset);
1705 fprintf_filtered (stream, " size %s offset %s (bits)",
1706 pulongest (ul), pulongest (offset));
1707 }
1708 break;
1709 }
1710
1711 fprintf_filtered (stream, "\n");
1712 }
1713
1714 return data;
1715 }
1716
1717 /* Describe a single location, which may in turn consist of multiple
1718 pieces. */
1719
1720 static void
1721 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
1722 struct ui_file *stream,
1723 const gdb_byte *data, int size,
1724 struct objfile *objfile, unsigned int addr_size,
1725 int offset_size)
1726 {
1727 const gdb_byte *end = data + size;
1728 int first_piece = 1, bad = 0;
1729
1730 while (data < end)
1731 {
1732 const gdb_byte *here = data;
1733 int disassemble = 1;
1734
1735 if (first_piece)
1736 first_piece = 0;
1737 else
1738 fprintf_filtered (stream, _(", and "));
1739
1740 if (!dwarf2_always_disassemble)
1741 {
1742 data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
1743 data, end, addr_size);
1744 /* If we printed anything, or if we have an empty piece,
1745 then don't disassemble. */
1746 if (data != here
1747 || data[0] == DW_OP_piece
1748 || data[0] == DW_OP_bit_piece)
1749 disassemble = 0;
1750 }
1751 if (disassemble)
1752 data = disassemble_dwarf_expression (stream, get_objfile_arch (objfile),
1753 addr_size, offset_size, data, end,
1754 dwarf2_always_disassemble);
1755
1756 if (data < end)
1757 {
1758 int empty = data == here;
1759
1760 if (disassemble)
1761 fprintf_filtered (stream, " ");
1762 if (data[0] == DW_OP_piece)
1763 {
1764 ULONGEST bytes;
1765
1766 data = read_uleb128 (data + 1, end, &bytes);
1767
1768 if (empty)
1769 fprintf_filtered (stream, _("an empty %s-byte piece"),
1770 pulongest (bytes));
1771 else
1772 fprintf_filtered (stream, _(" [%s-byte piece]"),
1773 pulongest (bytes));
1774 }
1775 else if (data[0] == DW_OP_bit_piece)
1776 {
1777 ULONGEST bits, offset;
1778
1779 data = read_uleb128 (data + 1, end, &bits);
1780 data = read_uleb128 (data, end, &offset);
1781
1782 if (empty)
1783 fprintf_filtered (stream,
1784 _("an empty %s-bit piece"),
1785 pulongest (bits));
1786 else
1787 fprintf_filtered (stream,
1788 _(" [%s-bit piece, offset %s bits]"),
1789 pulongest (bits), pulongest (offset));
1790 }
1791 else
1792 {
1793 bad = 1;
1794 break;
1795 }
1796 }
1797 }
1798
1799 if (bad || data > end)
1800 error (_("Corrupted DWARF2 expression for \"%s\"."),
1801 SYMBOL_PRINT_NAME (symbol));
1802 }
1803
1804 /* Print a natural-language description of SYMBOL to STREAM. This
1805 version is for a symbol with a single location. */
1806
1807 static void
1808 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
1809 struct ui_file *stream)
1810 {
1811 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1812 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1813 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1814 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
1815
1816 locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
1817 objfile, addr_size, offset_size);
1818 }
1819
1820 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1821 any necessary bytecode in AX. */
1822
1823 static void
1824 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1825 struct agent_expr *ax, struct axs_value *value)
1826 {
1827 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1828
1829 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
1830 dlbaton->data, dlbaton->size);
1831 }
1832
1833 /* The set of location functions used with the DWARF-2 expression
1834 evaluator. */
1835 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
1836 locexpr_read_variable,
1837 locexpr_read_needs_frame,
1838 locexpr_describe_location,
1839 locexpr_tracepoint_var_ref
1840 };
1841
1842
1843 /* Wrapper functions for location lists. These generally find
1844 the appropriate location expression and call something above. */
1845
1846 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1847 evaluator to calculate the location. */
1848 static struct value *
1849 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
1850 {
1851 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1852 struct value *val;
1853 const gdb_byte *data;
1854 size_t size;
1855
1856 data = find_location_expression (dlbaton, &size,
1857 frame ? get_frame_address_in_block (frame)
1858 : 0);
1859 if (data == NULL)
1860 {
1861 val = allocate_value (SYMBOL_TYPE (symbol));
1862 VALUE_LVAL (val) = not_lval;
1863 set_value_optimized_out (val, 1);
1864 }
1865 else
1866 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
1867 dlbaton->per_cu);
1868
1869 return val;
1870 }
1871
1872 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
1873 static int
1874 loclist_read_needs_frame (struct symbol *symbol)
1875 {
1876 /* If there's a location list, then assume we need to have a frame
1877 to choose the appropriate location expression. With tracking of
1878 global variables this is not necessarily true, but such tracking
1879 is disabled in GCC at the moment until we figure out how to
1880 represent it. */
1881
1882 return 1;
1883 }
1884
1885 /* Print a natural-language description of SYMBOL to STREAM. This
1886 version applies when there is a list of different locations, each
1887 with a specified address range. */
1888
1889 static void
1890 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
1891 struct ui_file *stream)
1892 {
1893 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1894 CORE_ADDR low, high;
1895 const gdb_byte *loc_ptr, *buf_end;
1896 int length, first = 1;
1897 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1898 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1899 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1900 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
1901 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
1902 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
1903 /* Adjust base_address for relocatable objects. */
1904 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
1905 SECT_OFF_TEXT (objfile));
1906 CORE_ADDR base_address = dlbaton->base_address + base_offset;
1907
1908 loc_ptr = dlbaton->data;
1909 buf_end = dlbaton->data + dlbaton->size;
1910
1911 fprintf_filtered (stream, _("multi-location:\n"));
1912
1913 /* Iterate through locations until we run out. */
1914 while (1)
1915 {
1916 if (buf_end - loc_ptr < 2 * addr_size)
1917 error (_("Corrupted DWARF expression for symbol \"%s\"."),
1918 SYMBOL_PRINT_NAME (symbol));
1919
1920 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1921 loc_ptr += addr_size;
1922
1923 /* A base-address-selection entry. */
1924 if (low == base_mask)
1925 {
1926 base_address = dwarf2_read_address (gdbarch,
1927 loc_ptr, buf_end, addr_size);
1928 fprintf_filtered (stream, _(" Base address %s"),
1929 paddress (gdbarch, base_address));
1930 loc_ptr += addr_size;
1931 continue;
1932 }
1933
1934 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1935 loc_ptr += addr_size;
1936
1937 /* An end-of-list entry. */
1938 if (low == 0 && high == 0)
1939 break;
1940
1941 /* Otherwise, a location expression entry. */
1942 low += base_address;
1943 high += base_address;
1944
1945 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
1946 loc_ptr += 2;
1947
1948 /* (It would improve readability to print only the minimum
1949 necessary digits of the second number of the range.) */
1950 fprintf_filtered (stream, _(" Range %s-%s: "),
1951 paddress (gdbarch, low), paddress (gdbarch, high));
1952
1953 /* Now describe this particular location. */
1954 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
1955 objfile, addr_size, offset_size);
1956
1957 fprintf_filtered (stream, "\n");
1958
1959 loc_ptr += length;
1960 }
1961 }
1962
1963 /* Describe the location of SYMBOL as an agent value in VALUE, generating
1964 any necessary bytecode in AX. */
1965 static void
1966 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1967 struct agent_expr *ax, struct axs_value *value)
1968 {
1969 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1970 const gdb_byte *data;
1971 size_t size;
1972
1973 data = find_location_expression (dlbaton, &size, ax->scope);
1974
1975 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
1976 }
1977
1978 /* The set of location functions used with the DWARF-2 expression
1979 evaluator and location lists. */
1980 const struct symbol_computed_ops dwarf2_loclist_funcs = {
1981 loclist_read_variable,
1982 loclist_read_needs_frame,
1983 loclist_describe_location,
1984 loclist_tracepoint_var_ref
1985 };