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