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