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