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