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