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