gdb
[deliverable/binutils-gdb.git] / gdb / dwarf2loc.c
CommitLineData
4c2df51b 1/* DWARF 2 location expression support for GDB.
feb13ab0 2
4c38e0a4
JB
3 Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
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
0d53c4c4
DJ
51/* A helper function for dealing with location lists. Given a
52 symbol baton (BATON) and a pc value (PC), find the appropriate
53 location expression, set *LOCEXPR_LENGTH, and return a pointer
54 to the beginning of the expression. Returns NULL on failure.
55
56 For now, only return the first matching location expression; there
57 can be more than one in the list. */
58
947bb88f 59static const gdb_byte *
0d53c4c4 60find_location_expression (struct dwarf2_loclist_baton *baton,
b6b08ebf 61 size_t *locexpr_length, CORE_ADDR pc)
0d53c4c4 62{
0d53c4c4 63 CORE_ADDR low, high;
947bb88f 64 const gdb_byte *loc_ptr, *buf_end;
852483bc 65 int length;
ae0d2f24 66 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
f7fd4728 67 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e17a4113 68 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
ae0d2f24 69 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
0d53c4c4 70 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8edfa926 71 /* Adjust base_address for relocatable objects. */
ae0d2f24
UW
72 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
73 SECT_OFF_TEXT (objfile));
8edfa926 74 CORE_ADDR base_address = baton->base_address + base_offset;
0d53c4c4
DJ
75
76 loc_ptr = baton->data;
77 buf_end = baton->data + baton->size;
78
79 while (1)
80 {
b5758fe4
UW
81 if (buf_end - loc_ptr < 2 * addr_size)
82 error (_("find_location_expression: Corrupted DWARF expression."));
0d53c4c4 83
b5758fe4
UW
84 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
85 loc_ptr += addr_size;
0d53c4c4
DJ
86
87 /* A base-address-selection entry. */
b5758fe4 88 if (low == base_mask)
0d53c4c4 89 {
b5758fe4
UW
90 base_address = dwarf2_read_address (gdbarch,
91 loc_ptr, buf_end, addr_size);
92 loc_ptr += addr_size;
0d53c4c4
DJ
93 continue;
94 }
95
b5758fe4
UW
96 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
97 loc_ptr += addr_size;
98
99 /* An end-of-list entry. */
100 if (low == 0 && high == 0)
101 return NULL;
102
0d53c4c4
DJ
103 /* Otherwise, a location expression entry. */
104 low += base_address;
105 high += base_address;
106
e17a4113 107 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
0d53c4c4
DJ
108 loc_ptr += 2;
109
110 if (pc >= low && pc < high)
111 {
112 *locexpr_length = length;
113 return loc_ptr;
114 }
115
116 loc_ptr += length;
117 }
118}
119
4c2df51b
DJ
120/* This is the baton used when performing dwarf2 expression
121 evaluation. */
122struct dwarf_expr_baton
123{
124 struct frame_info *frame;
125 struct objfile *objfile;
126};
127
128/* Helper functions for dwarf2_evaluate_loc_desc. */
129
4bc9efe1 130/* Using the frame specified in BATON, return the value of register
0b2b0195 131 REGNUM, treated as a pointer. */
4c2df51b 132static CORE_ADDR
61fbb938 133dwarf_expr_read_reg (void *baton, int dwarf_regnum)
4c2df51b 134{
4c2df51b 135 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
5e2b427d 136 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
e5192dd8 137 CORE_ADDR result;
0b2b0195 138 int regnum;
e4adbba9 139
5e2b427d
UW
140 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
141 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
0b2b0195 142 regnum, debaton->frame);
4c2df51b
DJ
143 return result;
144}
145
146/* Read memory at ADDR (length LEN) into BUF. */
147
148static void
852483bc 149dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
4c2df51b
DJ
150{
151 read_memory (addr, buf, len);
152}
153
154/* Using the frame specified in BATON, find the location expression
155 describing the frame base. Return a pointer to it in START and
156 its length in LENGTH. */
157static void
0d45f56e 158dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
4c2df51b 159{
da62e633
AC
160 /* FIXME: cagney/2003-03-26: This code should be using
161 get_frame_base_address(), and then implement a dwarf2 specific
162 this_base method. */
4c2df51b 163 struct symbol *framefunc;
4c2df51b 164 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
0d53c4c4 165
edb3359d
DJ
166 /* Use block_linkage_function, which returns a real (not inlined)
167 function, instead of get_frame_function, which may return an
168 inlined function. */
169 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
0d53c4c4 170
eff4f95e
JG
171 /* If we found a frame-relative symbol then it was certainly within
172 some function associated with a frame. If we can't find the frame,
173 something has gone wrong. */
174 gdb_assert (framefunc != NULL);
175
0936ad1d
SS
176 dwarf_expr_frame_base_1 (framefunc,
177 get_frame_address_in_block (debaton->frame),
178 start, length);
179}
180
181static void
182dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
0d45f56e 183 const gdb_byte **start, size_t *length)
0936ad1d 184{
edb3359d
DJ
185 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
186 *start = NULL;
187 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
0d53c4c4
DJ
188 {
189 struct dwarf2_loclist_baton *symbaton;
22c6caba 190
0d53c4c4 191 symbaton = SYMBOL_LOCATION_BATON (framefunc);
0936ad1d 192 *start = find_location_expression (symbaton, length, pc);
0d53c4c4
DJ
193 }
194 else
195 {
196 struct dwarf2_locexpr_baton *symbaton;
9a619af0 197
0d53c4c4 198 symbaton = SYMBOL_LOCATION_BATON (framefunc);
ebd3bcc1
JK
199 if (symbaton != NULL)
200 {
201 *length = symbaton->size;
202 *start = symbaton->data;
203 }
204 else
205 *start = NULL;
0d53c4c4
DJ
206 }
207
208 if (*start == NULL)
8a3fe4f8 209 error (_("Could not find the frame base for \"%s\"."),
0d53c4c4 210 SYMBOL_NATURAL_NAME (framefunc));
4c2df51b
DJ
211}
212
e7802207
TT
213/* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
214 the frame in BATON. */
215
216static CORE_ADDR
217dwarf_expr_frame_cfa (void *baton)
218{
219 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
9a619af0 220
e7802207
TT
221 return dwarf2_frame_cfa (debaton->frame);
222}
223
4c2df51b
DJ
224/* Using the objfile specified in BATON, find the address for the
225 current thread's thread-local storage with offset OFFSET. */
226static CORE_ADDR
227dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
228{
229 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
4c2df51b 230
9e35dae4 231 return target_translate_tls_address (debaton->objfile, offset);
4c2df51b
DJ
232}
233
052b9502
NF
234struct piece_closure
235{
88bfdde4
TT
236 /* Reference count. */
237 int refc;
238
052b9502
NF
239 /* The number of pieces used to describe this variable. */
240 int n_pieces;
241
6063c216
UW
242 /* The target address size, used only for DWARF_VALUE_STACK. */
243 int addr_size;
cec03d70 244
052b9502
NF
245 /* The pieces themselves. */
246 struct dwarf_expr_piece *pieces;
247};
248
249/* Allocate a closure for a value formed from separately-described
250 PIECES. */
251
252static struct piece_closure *
cec03d70 253allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
6063c216 254 int addr_size)
052b9502
NF
255{
256 struct piece_closure *c = XZALLOC (struct piece_closure);
257
88bfdde4 258 c->refc = 1;
052b9502 259 c->n_pieces = n_pieces;
6063c216 260 c->addr_size = addr_size;
052b9502
NF
261 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
262
263 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
264
265 return c;
266}
267
d3b1e874
TT
268/* The lowest-level function to extract bits from a byte buffer.
269 SOURCE is the buffer. It is updated if we read to the end of a
270 byte.
271 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
272 updated to reflect the number of bits actually read.
273 NBITS is the number of bits we want to read. It is updated to
274 reflect the number of bits actually read. This function may read
275 fewer bits.
276 BITS_BIG_ENDIAN is taken directly from gdbarch.
277 This function returns the extracted bits. */
278
279static unsigned int
280extract_bits_primitive (const gdb_byte **source,
281 unsigned int *source_offset_bits,
282 int *nbits, int bits_big_endian)
283{
284 unsigned int avail, mask, datum;
285
286 gdb_assert (*source_offset_bits < 8);
287
288 avail = 8 - *source_offset_bits;
289 if (avail > *nbits)
290 avail = *nbits;
291
292 mask = (1 << avail) - 1;
293 datum = **source;
294 if (bits_big_endian)
295 datum >>= 8 - (*source_offset_bits + *nbits);
296 else
297 datum >>= *source_offset_bits;
298 datum &= mask;
299
300 *nbits -= avail;
301 *source_offset_bits += avail;
302 if (*source_offset_bits >= 8)
303 {
304 *source_offset_bits -= 8;
305 ++*source;
306 }
307
308 return datum;
309}
310
311/* Extract some bits from a source buffer and move forward in the
312 buffer.
313
314 SOURCE is the source buffer. It is updated as bytes are read.
315 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
316 bits are read.
317 NBITS is the number of bits to read.
318 BITS_BIG_ENDIAN is taken directly from gdbarch.
319
320 This function returns the bits that were read. */
321
322static unsigned int
323extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
324 int nbits, int bits_big_endian)
325{
326 unsigned int datum;
327
328 gdb_assert (nbits > 0 && nbits <= 8);
329
330 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
331 bits_big_endian);
332 if (nbits > 0)
333 {
334 unsigned int more;
335
336 more = extract_bits_primitive (source, source_offset_bits, &nbits,
337 bits_big_endian);
338 if (bits_big_endian)
339 datum <<= nbits;
340 else
341 more <<= nbits;
342 datum |= more;
343 }
344
345 return datum;
346}
347
348/* Write some bits into a buffer and move forward in the buffer.
349
350 DATUM is the bits to write. The low-order bits of DATUM are used.
351 DEST is the destination buffer. It is updated as bytes are
352 written.
353 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
354 done.
355 NBITS is the number of valid bits in DATUM.
356 BITS_BIG_ENDIAN is taken directly from gdbarch. */
357
358static void
359insert_bits (unsigned int datum,
360 gdb_byte *dest, unsigned int dest_offset_bits,
361 int nbits, int bits_big_endian)
362{
363 unsigned int mask;
364
365 gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8);
366
367 mask = (1 << nbits) - 1;
368 if (bits_big_endian)
369 {
370 datum <<= 8 - (dest_offset_bits + nbits);
371 mask <<= 8 - (dest_offset_bits + nbits);
372 }
373 else
374 {
375 datum <<= dest_offset_bits;
376 mask <<= dest_offset_bits;
377 }
378
379 gdb_assert ((datum & ~mask) == 0);
380
381 *dest = (*dest & ~mask) | datum;
382}
383
384/* Copy bits from a source to a destination.
385
386 DEST is where the bits should be written.
387 DEST_OFFSET_BITS is the bit offset into DEST.
388 SOURCE is the source of bits.
389 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
390 BIT_COUNT is the number of bits to copy.
391 BITS_BIG_ENDIAN is taken directly from gdbarch. */
392
393static void
394copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
395 const gdb_byte *source, unsigned int source_offset_bits,
396 unsigned int bit_count,
397 int bits_big_endian)
398{
399 unsigned int dest_avail;
400 int datum;
401
402 /* Reduce everything to byte-size pieces. */
403 dest += dest_offset_bits / 8;
404 dest_offset_bits %= 8;
405 source += source_offset_bits / 8;
406 source_offset_bits %= 8;
407
408 dest_avail = 8 - dest_offset_bits % 8;
409
410 /* See if we can fill the first destination byte. */
411 if (dest_avail < bit_count)
412 {
413 datum = extract_bits (&source, &source_offset_bits, dest_avail,
414 bits_big_endian);
415 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
416 ++dest;
417 dest_offset_bits = 0;
418 bit_count -= dest_avail;
419 }
420
421 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
422 than 8 bits remaining. */
423 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
424 for (; bit_count >= 8; bit_count -= 8)
425 {
426 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
427 *dest++ = (gdb_byte) datum;
428 }
429
430 /* Finally, we may have a few leftover bits. */
431 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
432 if (bit_count > 0)
433 {
434 datum = extract_bits (&source, &source_offset_bits, bit_count,
435 bits_big_endian);
436 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
437 }
438}
439
052b9502
NF
440static void
441read_pieced_value (struct value *v)
442{
443 int i;
444 long offset = 0;
d3b1e874 445 ULONGEST bits_to_skip;
052b9502
NF
446 gdb_byte *contents;
447 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
448 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
afd74c5f 449 size_t type_len;
d3b1e874
TT
450 size_t buffer_size = 0;
451 char *buffer = NULL;
452 struct cleanup *cleanup;
453 int bits_big_endian
454 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
afd74c5f
TT
455
456 if (value_type (v) != value_enclosing_type (v))
457 internal_error (__FILE__, __LINE__,
458 _("Should not be able to create a lazy value with "
459 "an enclosing type"));
052b9502 460
d3b1e874
TT
461 cleanup = make_cleanup (free_current_contents, &buffer);
462
052b9502 463 contents = value_contents_raw (v);
d3b1e874
TT
464 bits_to_skip = 8 * value_offset (v);
465 type_len = 8 * TYPE_LENGTH (value_type (v));
466
afd74c5f 467 for (i = 0; i < c->n_pieces && offset < type_len; i++)
052b9502
NF
468 {
469 struct dwarf_expr_piece *p = &c->pieces[i];
d3b1e874
TT
470 size_t this_size, this_size_bits;
471 long dest_offset_bits, source_offset_bits, source_offset;
0d45f56e 472 const gdb_byte *intermediate_buffer;
d3b1e874
TT
473
474 /* Compute size, source, and destination offsets for copying, in
475 bits. */
476 this_size_bits = p->size;
477 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
afd74c5f 478 {
d3b1e874 479 bits_to_skip -= this_size_bits;
afd74c5f
TT
480 continue;
481 }
d3b1e874
TT
482 if (this_size_bits > type_len - offset)
483 this_size_bits = type_len - offset;
484 if (bits_to_skip > 0)
afd74c5f 485 {
d3b1e874
TT
486 dest_offset_bits = 0;
487 source_offset_bits = bits_to_skip;
488 this_size_bits -= bits_to_skip;
489 bits_to_skip = 0;
afd74c5f
TT
490 }
491 else
492 {
d3b1e874
TT
493 dest_offset_bits = offset;
494 source_offset_bits = 0;
afd74c5f 495 }
9a619af0 496
d3b1e874
TT
497 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
498 source_offset = source_offset_bits / 8;
499 if (buffer_size < this_size)
500 {
501 buffer_size = this_size;
502 buffer = xrealloc (buffer, buffer_size);
503 }
504 intermediate_buffer = buffer;
505
506 /* Copy from the source to DEST_BUFFER. */
cec03d70 507 switch (p->location)
052b9502 508 {
cec03d70
TT
509 case DWARF_VALUE_REGISTER:
510 {
511 struct gdbarch *arch = get_frame_arch (frame);
cec03d70 512 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
44353522 513 p->v.expr.value);
afd74c5f 514 int reg_offset = source_offset;
dcbf108f
UW
515
516 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
afd74c5f 517 && this_size < register_size (arch, gdb_regnum))
d3b1e874
TT
518 {
519 /* Big-endian, and we want less than full size. */
520 reg_offset = register_size (arch, gdb_regnum) - this_size;
521 /* We want the lower-order THIS_SIZE_BITS of the bytes
522 we extract from the register. */
523 source_offset_bits += 8 * this_size - this_size_bits;
524 }
dcbf108f 525
63b4f126
MGD
526 if (gdb_regnum != -1)
527 {
528 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
d3b1e874 529 this_size, buffer);
63b4f126
MGD
530 }
531 else
532 {
533 error (_("Unable to access DWARF register number %s"),
534 paddress (arch, p->v.expr.value));
535 }
cec03d70
TT
536 }
537 break;
538
539 case DWARF_VALUE_MEMORY:
44353522 540 if (p->v.expr.in_stack_memory)
d3b1e874 541 read_stack (p->v.expr.value + source_offset, buffer, this_size);
44353522 542 else
d3b1e874 543 read_memory (p->v.expr.value + source_offset, buffer, this_size);
cec03d70
TT
544 break;
545
546 case DWARF_VALUE_STACK:
547 {
6063c216 548 struct gdbarch *gdbarch = get_type_arch (value_type (v));
afd74c5f 549 size_t n = this_size;
9a619af0 550
afd74c5f
TT
551 if (n > c->addr_size - source_offset)
552 n = (c->addr_size >= source_offset
553 ? c->addr_size - source_offset
554 : 0);
555 if (n == 0)
556 {
557 /* Nothing. */
558 }
559 else if (source_offset == 0)
d3b1e874 560 store_unsigned_integer (buffer, n,
afd74c5f
TT
561 gdbarch_byte_order (gdbarch),
562 p->v.expr.value);
563 else
564 {
565 gdb_byte bytes[sizeof (ULONGEST)];
566
567 store_unsigned_integer (bytes, n + source_offset,
568 gdbarch_byte_order (gdbarch),
569 p->v.expr.value);
d3b1e874 570 memcpy (buffer, bytes + source_offset, n);
afd74c5f 571 }
cec03d70
TT
572 }
573 break;
574
575 case DWARF_VALUE_LITERAL:
576 {
afd74c5f
TT
577 size_t n = this_size;
578
579 if (n > p->v.literal.length - source_offset)
580 n = (p->v.literal.length >= source_offset
581 ? p->v.literal.length - source_offset
582 : 0);
583 if (n != 0)
d3b1e874 584 intermediate_buffer = p->v.literal.data + source_offset;
cec03d70
TT
585 }
586 break;
587
cb826367
TT
588 case DWARF_VALUE_OPTIMIZED_OUT:
589 /* We just leave the bits empty for now. This is not ideal
590 but gdb currently does not have a nice way to represent
591 optimized-out pieces. */
d3b1e874 592 warning (_("bits %ld-%ld in computed object were optimized out; "
cb826367
TT
593 "replacing with zeroes"),
594 offset,
d3b1e874 595 offset + (long) this_size_bits);
cb826367
TT
596 break;
597
cec03d70
TT
598 default:
599 internal_error (__FILE__, __LINE__, _("invalid location type"));
052b9502 600 }
d3b1e874
TT
601
602 if (p->location != DWARF_VALUE_OPTIMIZED_OUT)
603 copy_bitwise (contents, dest_offset_bits,
604 intermediate_buffer, source_offset_bits % 8,
605 this_size_bits, bits_big_endian);
606
607 offset += this_size_bits;
052b9502 608 }
d3b1e874
TT
609
610 do_cleanups (cleanup);
052b9502
NF
611}
612
613static void
614write_pieced_value (struct value *to, struct value *from)
615{
616 int i;
617 long offset = 0;
d3b1e874 618 ULONGEST bits_to_skip;
afd74c5f 619 const gdb_byte *contents;
052b9502
NF
620 struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
621 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
afd74c5f 622 size_t type_len;
d3b1e874
TT
623 size_t buffer_size = 0;
624 char *buffer = NULL;
625 struct cleanup *cleanup;
626 int bits_big_endian
627 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
052b9502
NF
628
629 if (frame == NULL)
630 {
631 set_value_optimized_out (to, 1);
632 return;
633 }
634
d3b1e874
TT
635 cleanup = make_cleanup (free_current_contents, &buffer);
636
afd74c5f 637 contents = value_contents (from);
d3b1e874
TT
638 bits_to_skip = 8 * value_offset (to);
639 type_len = 8 * TYPE_LENGTH (value_type (to));
afd74c5f 640 for (i = 0; i < c->n_pieces && offset < type_len; i++)
052b9502
NF
641 {
642 struct dwarf_expr_piece *p = &c->pieces[i];
d3b1e874
TT
643 size_t this_size_bits, this_size;
644 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
645 int need_bitwise;
646 const gdb_byte *source_buffer;
afd74c5f 647
d3b1e874
TT
648 this_size_bits = p->size;
649 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
afd74c5f 650 {
d3b1e874 651 bits_to_skip -= this_size_bits;
afd74c5f
TT
652 continue;
653 }
d3b1e874
TT
654 if (this_size_bits > type_len - offset)
655 this_size_bits = type_len - offset;
656 if (bits_to_skip > 0)
afd74c5f 657 {
d3b1e874
TT
658 dest_offset_bits = bits_to_skip;
659 source_offset_bits = 0;
660 this_size_bits -= bits_to_skip;
661 bits_to_skip = 0;
afd74c5f
TT
662 }
663 else
664 {
d3b1e874
TT
665 dest_offset_bits = 0;
666 source_offset_bits = offset;
667 }
668
669 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
670 source_offset = source_offset_bits / 8;
671 dest_offset = dest_offset_bits / 8;
672 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
673 {
674 source_buffer = contents + source_offset;
675 need_bitwise = 0;
676 }
677 else
678 {
679 if (buffer_size < this_size)
680 {
681 buffer_size = this_size;
682 buffer = xrealloc (buffer, buffer_size);
683 }
684 source_buffer = buffer;
685 need_bitwise = 1;
afd74c5f 686 }
9a619af0 687
cec03d70 688 switch (p->location)
052b9502 689 {
cec03d70
TT
690 case DWARF_VALUE_REGISTER:
691 {
692 struct gdbarch *arch = get_frame_arch (frame);
44353522 693 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
afd74c5f 694 int reg_offset = dest_offset;
dcbf108f
UW
695
696 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
afd74c5f 697 && this_size <= register_size (arch, gdb_regnum))
dcbf108f 698 /* Big-endian, and we want less than full size. */
afd74c5f 699 reg_offset = register_size (arch, gdb_regnum) - this_size;
dcbf108f 700
63b4f126
MGD
701 if (gdb_regnum != -1)
702 {
d3b1e874
TT
703 if (need_bitwise)
704 {
705 get_frame_register_bytes (frame, gdb_regnum, reg_offset,
706 this_size, buffer);
707 copy_bitwise (buffer, dest_offset_bits,
708 contents, source_offset_bits,
709 this_size_bits,
710 bits_big_endian);
711 }
712
63b4f126 713 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
d3b1e874 714 this_size, source_buffer);
63b4f126
MGD
715 }
716 else
717 {
718 error (_("Unable to write to DWARF register number %s"),
719 paddress (arch, p->v.expr.value));
720 }
cec03d70
TT
721 }
722 break;
723 case DWARF_VALUE_MEMORY:
d3b1e874
TT
724 if (need_bitwise)
725 {
726 /* Only the first and last bytes can possibly have any
727 bits reused. */
728 read_memory (p->v.expr.value + dest_offset, buffer, 1);
729 read_memory (p->v.expr.value + dest_offset + this_size - 1,
730 buffer + this_size - 1, 1);
731 copy_bitwise (buffer, dest_offset_bits,
732 contents, source_offset_bits,
733 this_size_bits,
734 bits_big_endian);
735 }
736
afd74c5f 737 write_memory (p->v.expr.value + dest_offset,
d3b1e874 738 source_buffer, this_size);
cec03d70
TT
739 break;
740 default:
741 set_value_optimized_out (to, 1);
d3b1e874 742 goto done;
052b9502 743 }
d3b1e874 744 offset += this_size_bits;
052b9502 745 }
d3b1e874
TT
746
747 done:
748 do_cleanups (cleanup);
052b9502
NF
749}
750
751static void *
752copy_pieced_value_closure (struct value *v)
753{
754 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
755
88bfdde4
TT
756 ++c->refc;
757 return c;
052b9502
NF
758}
759
760static void
761free_pieced_value_closure (struct value *v)
762{
763 struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
764
88bfdde4
TT
765 --c->refc;
766 if (c->refc == 0)
767 {
768 xfree (c->pieces);
769 xfree (c);
770 }
052b9502
NF
771}
772
773/* Functions for accessing a variable described by DW_OP_piece. */
774static struct lval_funcs pieced_value_funcs = {
775 read_pieced_value,
776 write_pieced_value,
777 copy_pieced_value_closure,
778 free_pieced_value_closure
779};
780
4c2df51b 781/* Evaluate a location description, starting at DATA and with length
a2d33775 782 SIZE, to find the current location of variable of TYPE in the context
4c2df51b 783 of FRAME. */
a2d33775 784
4c2df51b 785static struct value *
a2d33775 786dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
947bb88f 787 const gdb_byte *data, unsigned short size,
ae0d2f24 788 struct dwarf2_per_cu_data *per_cu)
4c2df51b 789{
4c2df51b
DJ
790 struct value *retval;
791 struct dwarf_expr_baton baton;
792 struct dwarf_expr_context *ctx;
4a227398 793 struct cleanup *old_chain;
4c2df51b 794
0d53c4c4
DJ
795 if (size == 0)
796 {
a2d33775 797 retval = allocate_value (type);
0d53c4c4 798 VALUE_LVAL (retval) = not_lval;
feb13ab0 799 set_value_optimized_out (retval, 1);
10fb19b6 800 return retval;
0d53c4c4
DJ
801 }
802
4c2df51b 803 baton.frame = frame;
ae0d2f24 804 baton.objfile = dwarf2_per_cu_objfile (per_cu);
4c2df51b
DJ
805
806 ctx = new_dwarf_expr_context ();
4a227398
TT
807 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
808
f7fd4728 809 ctx->gdbarch = get_objfile_arch (baton.objfile);
ae0d2f24 810 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
4c2df51b
DJ
811 ctx->baton = &baton;
812 ctx->read_reg = dwarf_expr_read_reg;
813 ctx->read_mem = dwarf_expr_read_mem;
814 ctx->get_frame_base = dwarf_expr_frame_base;
e7802207 815 ctx->get_frame_cfa = dwarf_expr_frame_cfa;
4c2df51b
DJ
816 ctx->get_tls_address = dwarf_expr_tls_address;
817
818 dwarf_expr_eval (ctx, data, size);
87808bd6
JB
819 if (ctx->num_pieces > 0)
820 {
052b9502
NF
821 struct piece_closure *c;
822 struct frame_id frame_id = get_frame_id (frame);
823
6063c216
UW
824 c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
825 ctx->addr_size);
a2d33775 826 retval = allocate_computed_value (type, &pieced_value_funcs, c);
052b9502 827 VALUE_FRAME_ID (retval) = frame_id;
87808bd6 828 }
4c2df51b
DJ
829 else
830 {
cec03d70
TT
831 switch (ctx->location)
832 {
833 case DWARF_VALUE_REGISTER:
834 {
835 struct gdbarch *arch = get_frame_arch (frame);
836 CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
837 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
9a619af0 838
63b4f126 839 if (gdb_regnum != -1)
a2d33775 840 retval = value_from_register (type, gdb_regnum, frame);
63b4f126 841 else
a2d33775
JK
842 error (_("Unable to access DWARF register number %s"),
843 paddress (arch, dwarf_regnum));
cec03d70
TT
844 }
845 break;
846
847 case DWARF_VALUE_MEMORY:
848 {
849 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
44353522 850 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
cec03d70 851
a2d33775 852 retval = allocate_value (type);
cec03d70
TT
853 VALUE_LVAL (retval) = lval_memory;
854 set_value_lazy (retval, 1);
44353522
DE
855 if (in_stack_memory)
856 set_value_stack (retval, 1);
cec03d70
TT
857 set_value_address (retval, address);
858 }
859 break;
860
861 case DWARF_VALUE_STACK:
862 {
cec03d70
TT
863 ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
864 bfd_byte *contents;
865 size_t n = ctx->addr_size;
866
a2d33775 867 retval = allocate_value (type);
cec03d70 868 contents = value_contents_raw (retval);
a2d33775
JK
869 if (n > TYPE_LENGTH (type))
870 n = TYPE_LENGTH (type);
05566b3b
TT
871 store_unsigned_integer (contents, n,
872 gdbarch_byte_order (ctx->gdbarch),
873 value);
cec03d70
TT
874 }
875 break;
876
877 case DWARF_VALUE_LITERAL:
878 {
879 bfd_byte *contents;
880 size_t n = ctx->len;
881
a2d33775 882 retval = allocate_value (type);
cec03d70 883 contents = value_contents_raw (retval);
a2d33775
JK
884 if (n > TYPE_LENGTH (type))
885 n = TYPE_LENGTH (type);
cec03d70
TT
886 memcpy (contents, ctx->data, n);
887 }
888 break;
889
cb826367
TT
890 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
891 it can only be encountered when making a piece. */
892 case DWARF_VALUE_OPTIMIZED_OUT:
cec03d70
TT
893 default:
894 internal_error (__FILE__, __LINE__, _("invalid location type"));
895 }
4c2df51b
DJ
896 }
897
42be36b3
CT
898 set_value_initialized (retval, ctx->initialized);
899
4a227398 900 do_cleanups (old_chain);
4c2df51b
DJ
901
902 return retval;
903}
4c2df51b
DJ
904\f
905/* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
906
907struct needs_frame_baton
908{
909 int needs_frame;
910};
911
912/* Reads from registers do require a frame. */
913static CORE_ADDR
61fbb938 914needs_frame_read_reg (void *baton, int regnum)
4c2df51b
DJ
915{
916 struct needs_frame_baton *nf_baton = baton;
9a619af0 917
4c2df51b
DJ
918 nf_baton->needs_frame = 1;
919 return 1;
920}
921
922/* Reads from memory do not require a frame. */
923static void
852483bc 924needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
4c2df51b
DJ
925{
926 memset (buf, 0, len);
927}
928
929/* Frame-relative accesses do require a frame. */
930static void
0d45f56e 931needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
4c2df51b 932{
852483bc 933 static gdb_byte lit0 = DW_OP_lit0;
4c2df51b
DJ
934 struct needs_frame_baton *nf_baton = baton;
935
936 *start = &lit0;
937 *length = 1;
938
939 nf_baton->needs_frame = 1;
940}
941
e7802207
TT
942/* CFA accesses require a frame. */
943
944static CORE_ADDR
945needs_frame_frame_cfa (void *baton)
946{
947 struct needs_frame_baton *nf_baton = baton;
9a619af0 948
e7802207
TT
949 nf_baton->needs_frame = 1;
950 return 1;
951}
952
4c2df51b
DJ
953/* Thread-local accesses do require a frame. */
954static CORE_ADDR
955needs_frame_tls_address (void *baton, CORE_ADDR offset)
956{
957 struct needs_frame_baton *nf_baton = baton;
9a619af0 958
4c2df51b
DJ
959 nf_baton->needs_frame = 1;
960 return 1;
961}
962
963/* Return non-zero iff the location expression at DATA (length SIZE)
964 requires a frame to evaluate. */
965
966static int
947bb88f 967dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
ae0d2f24 968 struct dwarf2_per_cu_data *per_cu)
4c2df51b
DJ
969{
970 struct needs_frame_baton baton;
971 struct dwarf_expr_context *ctx;
f630a401 972 int in_reg;
4a227398 973 struct cleanup *old_chain;
4c2df51b
DJ
974
975 baton.needs_frame = 0;
976
977 ctx = new_dwarf_expr_context ();
4a227398
TT
978 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
979
f7fd4728 980 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
ae0d2f24 981 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
4c2df51b
DJ
982 ctx->baton = &baton;
983 ctx->read_reg = needs_frame_read_reg;
984 ctx->read_mem = needs_frame_read_mem;
985 ctx->get_frame_base = needs_frame_frame_base;
e7802207 986 ctx->get_frame_cfa = needs_frame_frame_cfa;
4c2df51b
DJ
987 ctx->get_tls_address = needs_frame_tls_address;
988
989 dwarf_expr_eval (ctx, data, size);
990
cec03d70 991 in_reg = ctx->location == DWARF_VALUE_REGISTER;
f630a401 992
87808bd6
JB
993 if (ctx->num_pieces > 0)
994 {
995 int i;
996
997 /* If the location has several pieces, and any of them are in
998 registers, then we will need a frame to fetch them from. */
999 for (i = 0; i < ctx->num_pieces; i++)
cec03d70 1000 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
87808bd6
JB
1001 in_reg = 1;
1002 }
1003
4a227398 1004 do_cleanups (old_chain);
4c2df51b 1005
f630a401 1006 return baton.needs_frame || in_reg;
4c2df51b
DJ
1007}
1008
08922a10
SS
1009/* This struct keeps track of the pieces that make up a multi-location
1010 object, for use in agent expression generation. It is
1011 superficially similar to struct dwarf_expr_piece, but
1012 dwarf_expr_piece is designed for use in immediate evaluation, and
1013 does not, for example, have a way to record both base register and
1014 offset. */
1015
1016struct axs_var_loc
0d53c4c4 1017{
08922a10
SS
1018 /* Memory vs register, etc */
1019 enum axs_lvalue_kind kind;
1020
1021 /* If non-zero, number of bytes in this fragment */
1022 unsigned bytes;
1023
1024 /* (GDB-numbered) reg, or base reg if >= 0 */
1025 int reg;
1026
1027 /* offset from reg */
1028 LONGEST offset;
1029};
1030
0d45f56e 1031static const gdb_byte *
08922a10
SS
1032dwarf2_tracepoint_var_loc (struct symbol *symbol,
1033 struct agent_expr *ax,
1034 struct axs_var_loc *loc,
1035 struct gdbarch *gdbarch,
0d45f56e 1036 const gdb_byte *data, const gdb_byte *end)
08922a10
SS
1037{
1038 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
0d53c4c4 1039 {
08922a10
SS
1040 loc->kind = axs_lvalue_register;
1041 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1042 data += 1;
0d53c4c4
DJ
1043 }
1044 else if (data[0] == DW_OP_regx)
1045 {
1046 ULONGEST reg;
9a619af0 1047
08922a10
SS
1048 data = read_uleb128 (data + 1, end, &reg);
1049 loc->kind = axs_lvalue_register;
1050 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
0d53c4c4
DJ
1051 }
1052 else if (data[0] == DW_OP_fbreg)
1053 {
0936ad1d
SS
1054 struct block *b;
1055 struct symbol *framefunc;
1056 int frame_reg = 0;
0d53c4c4 1057 LONGEST frame_offset;
0d45f56e 1058 const gdb_byte *base_data;
0936ad1d
SS
1059 size_t base_size;
1060 LONGEST base_offset = 0;
1061
1062 b = block_for_pc (ax->scope);
1063
1064 if (!b)
1065 error (_("No block found for address"));
1066
1067 framefunc = block_linkage_function (b);
1068
1069 if (!framefunc)
1070 error (_("No function found for block"));
1071
1072 dwarf_expr_frame_base_1 (framefunc, ax->scope,
1073 &base_data, &base_size);
1074
08922a10 1075 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
0936ad1d 1076 {
0d45f56e
TT
1077 const gdb_byte *buf_end;
1078
0936ad1d 1079 frame_reg = base_data[0] - DW_OP_breg0;
08922a10
SS
1080 buf_end = read_sleb128 (base_data + 1,
1081 base_data + base_size, &base_offset);
0936ad1d
SS
1082 if (buf_end != base_data + base_size)
1083 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1084 frame_reg, SYMBOL_PRINT_NAME (symbol));
1085 }
08922a10
SS
1086 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1087 {
1088 /* The frame base is just the register, with no offset. */
1089 frame_reg = base_data[0] - DW_OP_reg0;
1090 base_offset = 0;
1091 }
0936ad1d
SS
1092 else
1093 {
1094 /* We don't know what to do with the frame base expression,
1095 so we can't trace this variable; give up. */
08922a10
SS
1096 error (_("Cannot generate expression to collect symbol \"%s\"; DWARF 2 encoding not handled, first opcode in base data is 0x%x."),
1097 SYMBOL_PRINT_NAME (symbol), base_data[0]);
0936ad1d 1098 }
0d53c4c4 1099
08922a10 1100 data = read_sleb128 (data + 1, end, &frame_offset);
4c2df51b 1101
08922a10
SS
1102 loc->kind = axs_lvalue_memory;
1103 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1104 loc->offset = base_offset + frame_offset;
9c238357 1105 }
08922a10 1106 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31)
9c238357
RC
1107 {
1108 unsigned int reg;
1109 LONGEST offset;
9c238357
RC
1110
1111 reg = data[0] - DW_OP_breg0;
08922a10 1112 data = read_sleb128 (data + 1, end, &offset);
9c238357 1113
08922a10
SS
1114 loc->kind = axs_lvalue_memory;
1115 loc->reg = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1116 loc->offset = offset;
0d53c4c4
DJ
1117 }
1118 else
9c238357
RC
1119 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
1120 data[0], SYMBOL_PRINT_NAME (symbol));
08922a10
SS
1121
1122 return data;
0d53c4c4 1123}
08922a10
SS
1124
1125/* Given the location of a piece, issue bytecodes that will access it. */
1126
1127static void
1128dwarf2_tracepoint_var_access (struct agent_expr *ax,
1129 struct axs_value *value,
1130 struct axs_var_loc *loc)
1131{
1132 value->kind = loc->kind;
1133
1134 switch (loc->kind)
1135 {
1136 case axs_lvalue_register:
1137 value->u.reg = loc->reg;
1138 break;
1139
1140 case axs_lvalue_memory:
1141 ax_reg (ax, loc->reg);
1142 if (loc->offset)
1143 {
1144 ax_const_l (ax, loc->offset);
1145 ax_simple (ax, aop_add);
1146 }
1147 break;
1148
1149 default:
1150 internal_error (__FILE__, __LINE__, _("Unhandled value kind in dwarf2_tracepoint_var_access"));
1151 }
1152}
1153
1154static void
1155dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1156 struct agent_expr *ax, struct axs_value *value,
0d45f56e 1157 const gdb_byte *data, int size)
08922a10 1158{
0d45f56e 1159 const gdb_byte *end = data + size;
08922a10
SS
1160 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1161 /* In practice, a variable is not going to be spread across
1162 dozens of registers or memory locations. If someone comes up
1163 with a real-world example, revisit this. */
1164#define MAX_FRAGS 16
1165 struct axs_var_loc fragments[MAX_FRAGS];
1166 int nfrags = 0, frag;
1167 int length = 0;
1168 int piece_ok = 0;
1169 int bad = 0;
1170 int first = 1;
1171
1172 if (!data || size == 0)
1173 {
1174 value->optimized_out = 1;
1175 return;
1176 }
1177
1178 while (data < end)
1179 {
1180 if (!piece_ok)
1181 {
1182 if (nfrags == MAX_FRAGS)
1183 error (_("Too many pieces in location for \"%s\"."),
1184 SYMBOL_PRINT_NAME (symbol));
1185
1186 fragments[nfrags].bytes = 0;
1187 data = dwarf2_tracepoint_var_loc (symbol, ax, &fragments[nfrags],
1188 gdbarch, data, end);
1189 nfrags++;
1190 piece_ok = 1;
1191 }
1192 else if (data[0] == DW_OP_piece)
1193 {
1194 ULONGEST bytes;
1195
1196 data = read_uleb128 (data + 1, end, &bytes);
1197 /* Only deal with 4 byte fragments for now. */
1198 if (bytes != 4)
1199 error (_("DW_OP_piece %s not supported in location for \"%s\"."),
1200 pulongest (bytes), SYMBOL_PRINT_NAME (symbol));
1201 fragments[nfrags - 1].bytes = bytes;
1202 length += bytes;
1203 piece_ok = 0;
1204 }
1205 else
1206 {
1207 bad = 1;
1208 break;
1209 }
1210 }
1211
1212 if (bad || data > end)
1213 error (_("Corrupted DWARF expression for \"%s\"."),
1214 SYMBOL_PRINT_NAME (symbol));
1215
1216 /* If single expression, no pieces, convert to external format. */
1217 if (length == 0)
1218 {
1219 dwarf2_tracepoint_var_access (ax, value, &fragments[0]);
1220 return;
1221 }
1222
1223 if (length != TYPE_LENGTH (value->type))
1224 error (_("Inconsistent piece information for \"%s\"."),
1225 SYMBOL_PRINT_NAME (symbol));
1226
1227 /* Emit bytecodes to assemble the pieces into a single stack entry. */
1228
1229 for ((frag = (byte_order == BFD_ENDIAN_BIG ? 0 : nfrags - 1));
1230 nfrags--;
1231 (frag += (byte_order == BFD_ENDIAN_BIG ? 1 : -1)))
1232 {
1233 if (!first)
1234 {
1235 /* shift the previous fragment up 32 bits */
1236 ax_const_l (ax, 32);
1237 ax_simple (ax, aop_lsh);
1238 }
1239
1240 dwarf2_tracepoint_var_access (ax, value, &fragments[frag]);
1241
1242 switch (value->kind)
1243 {
1244 case axs_lvalue_register:
1245 ax_reg (ax, value->u.reg);
1246 break;
1247
1248 case axs_lvalue_memory:
1249 {
1250 extern int trace_kludge; /* Ugh. */
1251
1252 gdb_assert (fragments[frag].bytes == 4);
1253 if (trace_kludge)
1254 ax_trace_quick (ax, 4);
1255 ax_simple (ax, aop_ref32);
1256 }
1257 break;
1258 }
1259
1260 if (!first)
1261 {
1262 /* or the new fragment into the previous */
1263 ax_zero_ext (ax, 32);
1264 ax_simple (ax, aop_bit_or);
1265 }
1266 first = 0;
1267 }
1268 value->kind = axs_rvalue;
1269}
1270
4c2df51b
DJ
1271\f
1272/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1273 evaluator to calculate the location. */
1274static struct value *
1275locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
1276{
1277 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1278 struct value *val;
9a619af0 1279
a2d33775
JK
1280 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
1281 dlbaton->size, dlbaton->per_cu);
4c2df51b
DJ
1282
1283 return val;
1284}
1285
1286/* Return non-zero iff we need a frame to evaluate SYMBOL. */
1287static int
1288locexpr_read_needs_frame (struct symbol *symbol)
1289{
1290 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
9a619af0 1291
ae0d2f24
UW
1292 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
1293 dlbaton->per_cu);
4c2df51b
DJ
1294}
1295
9eae7c52
TT
1296/* Return true if DATA points to the end of a piece. END is one past
1297 the last byte in the expression. */
1298
1299static int
1300piece_end_p (const gdb_byte *data, const gdb_byte *end)
1301{
1302 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
1303}
1304
1305/* Nicely describe a single piece of a location, returning an updated
1306 position in the bytecode sequence. This function cannot recognize
1307 all locations; if a location is not recognized, it simply returns
1308 DATA. */
08922a10 1309
0d45f56e 1310static const gdb_byte *
08922a10
SS
1311locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
1312 CORE_ADDR addr, struct objfile *objfile,
9eae7c52 1313 const gdb_byte *data, const gdb_byte *end,
0d45f56e 1314 unsigned int addr_size)
4c2df51b 1315{
08922a10
SS
1316 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1317 int regno;
1318
1319 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1320 {
1321 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1322 fprintf_filtered (stream, _("a variable in $%s"),
1323 gdbarch_register_name (gdbarch, regno));
1324 data += 1;
1325 }
1326 else if (data[0] == DW_OP_regx)
1327 {
1328 ULONGEST reg;
4c2df51b 1329
9eae7c52 1330 data = read_uleb128 (data + 1, end, &reg);
08922a10
SS
1331 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1332 fprintf_filtered (stream, _("a variable in $%s"),
1333 gdbarch_register_name (gdbarch, regno));
1334 }
1335 else if (data[0] == DW_OP_fbreg)
4c2df51b 1336 {
08922a10
SS
1337 struct block *b;
1338 struct symbol *framefunc;
1339 int frame_reg = 0;
1340 LONGEST frame_offset;
9eae7c52 1341 const gdb_byte *base_data, *new_data;
08922a10
SS
1342 size_t base_size;
1343 LONGEST base_offset = 0;
1344
9eae7c52
TT
1345 new_data = read_sleb128 (data + 1, end, &frame_offset);
1346 if (!piece_end_p (new_data, end))
1347 return data;
1348 data = new_data;
1349
08922a10
SS
1350 b = block_for_pc (addr);
1351
1352 if (!b)
1353 error (_("No block found for address for symbol \"%s\"."),
1354 SYMBOL_PRINT_NAME (symbol));
1355
1356 framefunc = block_linkage_function (b);
1357
1358 if (!framefunc)
1359 error (_("No function found for block for symbol \"%s\"."),
1360 SYMBOL_PRINT_NAME (symbol));
1361
1362 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
1363
1364 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1365 {
0d45f56e 1366 const gdb_byte *buf_end;
08922a10
SS
1367
1368 frame_reg = base_data[0] - DW_OP_breg0;
1369 buf_end = read_sleb128 (base_data + 1,
1370 base_data + base_size, &base_offset);
1371 if (buf_end != base_data + base_size)
1372 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1373 frame_reg, SYMBOL_PRINT_NAME (symbol));
1374 }
1375 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1376 {
1377 /* The frame base is just the register, with no offset. */
1378 frame_reg = base_data[0] - DW_OP_reg0;
1379 base_offset = 0;
1380 }
1381 else
1382 {
1383 /* We don't know what to do with the frame base expression,
1384 so we can't trace this variable; give up. */
1385 error (_("Cannot describe location of symbol \"%s\"; "
1386 "DWARF 2 encoding not handled, "
1387 "first opcode in base data is 0x%x."),
1388 SYMBOL_PRINT_NAME (symbol), base_data[0]);
1389 }
1390
1391 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1392
08922a10
SS
1393 fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1394 gdbarch_register_name (gdbarch, regno),
1395 plongest (base_offset), plongest (frame_offset));
1396 }
9eae7c52
TT
1397 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
1398 && piece_end_p (data, end))
08922a10
SS
1399 {
1400 LONGEST offset;
1401
1402 regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1403
9eae7c52 1404 data = read_sleb128 (data + 1, end, &offset);
08922a10 1405
4c2df51b 1406 fprintf_filtered (stream,
08922a10
SS
1407 _("a variable at offset %s from base reg $%s"),
1408 plongest (offset),
5e2b427d 1409 gdbarch_register_name (gdbarch, regno));
4c2df51b
DJ
1410 }
1411
c3228f12
EZ
1412 /* The location expression for a TLS variable looks like this (on a
1413 64-bit LE machine):
1414
1415 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1416 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1417
1418 0x3 is the encoding for DW_OP_addr, which has an operand as long
1419 as the size of an address on the target machine (here is 8
1420 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1421 The operand represents the offset at which the variable is within
1422 the thread local storage. */
1423
9eae7c52
TT
1424 else if (data + 1 + addr_size < end
1425 && data[0] == DW_OP_addr
1426 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
1427 && piece_end_p (data + 2 + addr_size, end))
08922a10
SS
1428 {
1429 CORE_ADDR offset = dwarf2_read_address (gdbarch,
1430 data + 1,
9eae7c52 1431 end,
08922a10 1432 addr_size);
9a619af0 1433
08922a10
SS
1434 fprintf_filtered (stream,
1435 _("a thread-local variable at offset %s "
1436 "in the thread-local storage for `%s'"),
1437 paddress (gdbarch, offset), objfile->name);
1438
1439 data += 1 + addr_size + 1;
1440 }
9eae7c52
TT
1441 else if (data[0] >= DW_OP_lit0
1442 && data[0] <= DW_OP_lit31
1443 && data + 1 < end
1444 && data[1] == DW_OP_stack_value)
1445 {
1446 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
1447 data += 2;
1448 }
1449
1450 return data;
1451}
1452
1453/* Disassemble an expression, stopping at the end of a piece or at the
1454 end of the expression. Returns a pointer to the next unread byte
1455 in the input expression. If ALL is nonzero, then this function
1456 will keep going until it reaches the end of the expression. */
1457
1458static const gdb_byte *
1459disassemble_dwarf_expression (struct ui_file *stream,
1460 struct gdbarch *arch, unsigned int addr_size,
1461 int offset_size,
1462 const gdb_byte *data, const gdb_byte *end,
1463 int all)
1464{
1465 const gdb_byte *start = data;
1466
1467 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
1468
1469 while (data < end
1470 && (all
1471 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
1472 {
1473 enum dwarf_location_atom op = *data++;
1474 CORE_ADDR addr;
1475 ULONGEST ul;
1476 LONGEST l;
1477 const char *name;
1478
1479 name = dwarf_stack_op_name (op, 0);
1480
1481 if (!name)
1482 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
1483 op, (long) (data - start));
1484 fprintf_filtered (stream, " % 4ld: %s", (long) (data - start), name);
1485
1486 switch (op)
1487 {
1488 case DW_OP_addr:
1489 addr = dwarf2_read_address (arch, data, end, addr_size);
1490 data += addr_size;
1491 fprintf_filtered (stream, " %s", paddress (arch, addr));
1492 break;
1493
1494 case DW_OP_const1u:
1495 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
1496 data += 1;
1497 fprintf_filtered (stream, " %s", pulongest (ul));
1498 break;
1499 case DW_OP_const1s:
1500 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
1501 data += 1;
1502 fprintf_filtered (stream, " %s", plongest (l));
1503 break;
1504 case DW_OP_const2u:
1505 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
1506 data += 2;
1507 fprintf_filtered (stream, " %s", pulongest (ul));
1508 break;
1509 case DW_OP_const2s:
1510 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1511 data += 2;
1512 fprintf_filtered (stream, " %s", plongest (l));
1513 break;
1514 case DW_OP_const4u:
1515 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
1516 data += 4;
1517 fprintf_filtered (stream, " %s", pulongest (ul));
1518 break;
1519 case DW_OP_const4s:
1520 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
1521 data += 4;
1522 fprintf_filtered (stream, " %s", plongest (l));
1523 break;
1524 case DW_OP_const8u:
1525 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
1526 data += 8;
1527 fprintf_filtered (stream, " %s", pulongest (ul));
1528 break;
1529 case DW_OP_const8s:
1530 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
1531 data += 8;
1532 fprintf_filtered (stream, " %s", plongest (l));
1533 break;
1534 case DW_OP_constu:
1535 data = read_uleb128 (data, end, &ul);
1536 fprintf_filtered (stream, " %s", pulongest (ul));
1537 break;
1538 case DW_OP_consts:
1539 data = read_sleb128 (data, end, &ul);
1540 fprintf_filtered (stream, " %s", plongest (l));
1541 break;
1542
1543 case DW_OP_reg0:
1544 case DW_OP_reg1:
1545 case DW_OP_reg2:
1546 case DW_OP_reg3:
1547 case DW_OP_reg4:
1548 case DW_OP_reg5:
1549 case DW_OP_reg6:
1550 case DW_OP_reg7:
1551 case DW_OP_reg8:
1552 case DW_OP_reg9:
1553 case DW_OP_reg10:
1554 case DW_OP_reg11:
1555 case DW_OP_reg12:
1556 case DW_OP_reg13:
1557 case DW_OP_reg14:
1558 case DW_OP_reg15:
1559 case DW_OP_reg16:
1560 case DW_OP_reg17:
1561 case DW_OP_reg18:
1562 case DW_OP_reg19:
1563 case DW_OP_reg20:
1564 case DW_OP_reg21:
1565 case DW_OP_reg22:
1566 case DW_OP_reg23:
1567 case DW_OP_reg24:
1568 case DW_OP_reg25:
1569 case DW_OP_reg26:
1570 case DW_OP_reg27:
1571 case DW_OP_reg28:
1572 case DW_OP_reg29:
1573 case DW_OP_reg30:
1574 case DW_OP_reg31:
1575 fprintf_filtered (stream, " [$%s]",
1576 gdbarch_register_name (arch, op - DW_OP_reg0));
1577 break;
1578
1579 case DW_OP_regx:
1580 data = read_uleb128 (data, end, &ul);
1581 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
1582 gdbarch_register_name (arch, (int) ul));
1583 break;
1584
1585 case DW_OP_implicit_value:
1586 data = read_uleb128 (data, end, &ul);
1587 data += ul;
1588 fprintf_filtered (stream, " %s", pulongest (ul));
1589 break;
1590
1591 case DW_OP_breg0:
1592 case DW_OP_breg1:
1593 case DW_OP_breg2:
1594 case DW_OP_breg3:
1595 case DW_OP_breg4:
1596 case DW_OP_breg5:
1597 case DW_OP_breg6:
1598 case DW_OP_breg7:
1599 case DW_OP_breg8:
1600 case DW_OP_breg9:
1601 case DW_OP_breg10:
1602 case DW_OP_breg11:
1603 case DW_OP_breg12:
1604 case DW_OP_breg13:
1605 case DW_OP_breg14:
1606 case DW_OP_breg15:
1607 case DW_OP_breg16:
1608 case DW_OP_breg17:
1609 case DW_OP_breg18:
1610 case DW_OP_breg19:
1611 case DW_OP_breg20:
1612 case DW_OP_breg21:
1613 case DW_OP_breg22:
1614 case DW_OP_breg23:
1615 case DW_OP_breg24:
1616 case DW_OP_breg25:
1617 case DW_OP_breg26:
1618 case DW_OP_breg27:
1619 case DW_OP_breg28:
1620 case DW_OP_breg29:
1621 case DW_OP_breg30:
1622 case DW_OP_breg31:
1623 data = read_sleb128 (data, end, &ul);
1624 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
1625 gdbarch_register_name (arch, op - DW_OP_breg0));
1626 break;
1627
1628 case DW_OP_bregx:
1629 {
1630 ULONGEST offset;
1631
1632 data = read_uleb128 (data, end, &ul);
1633 data = read_sleb128 (data, end, &offset);
1634 fprintf_filtered (stream, " register %s [$%s] offset %s",
1635 pulongest (ul),
1636 gdbarch_register_name (arch, (int) ul),
1637 pulongest (offset));
1638 }
1639 break;
1640
1641 case DW_OP_fbreg:
1642 data = read_sleb128 (data, end, &ul);
1643 fprintf_filtered (stream, " %s", pulongest (ul));
1644 break;
1645
1646 case DW_OP_xderef_size:
1647 case DW_OP_deref_size:
1648 case DW_OP_pick:
1649 fprintf_filtered (stream, " %d", *data);
1650 ++data;
1651 break;
1652
1653 case DW_OP_plus_uconst:
1654 data = read_uleb128 (data, end, &ul);
1655 fprintf_filtered (stream, " %s", pulongest (ul));
1656 break;
1657
1658 case DW_OP_skip:
1659 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1660 data += 2;
1661 fprintf_filtered (stream, " to %ld",
1662 (long) (data + l - start));
1663 break;
1664
1665 case DW_OP_bra:
1666 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
1667 data += 2;
1668 fprintf_filtered (stream, " %ld",
1669 (long) (data + l - start));
1670 break;
1671
1672 case DW_OP_call2:
1673 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
1674 data += 2;
1675 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
1676 break;
1677
1678 case DW_OP_call4:
1679 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
1680 data += 4;
1681 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
1682 break;
1683
1684 case DW_OP_call_ref:
1685 ul = extract_unsigned_integer (data, offset_size,
1686 gdbarch_byte_order (arch));
1687 data += offset_size;
1688 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
1689 break;
1690
1691 case DW_OP_piece:
1692 data = read_uleb128 (data, end, &ul);
1693 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
1694 break;
1695
1696 case DW_OP_bit_piece:
1697 {
1698 ULONGEST offset;
1699
1700 data = read_uleb128 (data, end, &ul);
1701 data = read_uleb128 (data, end, &offset);
1702 fprintf_filtered (stream, " size %s offset %s (bits)",
1703 pulongest (ul), pulongest (offset));
1704 }
1705 break;
1706 }
1707
1708 fprintf_filtered (stream, "\n");
1709 }
c3228f12 1710
08922a10 1711 return data;
4c2df51b
DJ
1712}
1713
08922a10
SS
1714/* Describe a single location, which may in turn consist of multiple
1715 pieces. */
a55cc764 1716
08922a10
SS
1717static void
1718locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
0d45f56e
TT
1719 struct ui_file *stream,
1720 const gdb_byte *data, int size,
9eae7c52
TT
1721 struct objfile *objfile, unsigned int addr_size,
1722 int offset_size)
08922a10 1723{
0d45f56e 1724 const gdb_byte *end = data + size;
9eae7c52 1725 int first_piece = 1, bad = 0;
08922a10 1726
08922a10
SS
1727 while (data < end)
1728 {
9eae7c52
TT
1729 const gdb_byte *here = data;
1730 int disassemble = 1;
1731
1732 if (first_piece)
1733 first_piece = 0;
1734 else
1735 fprintf_filtered (stream, _(", and "));
08922a10 1736
9eae7c52
TT
1737 if (!dwarf2_always_disassemble)
1738 {
08922a10 1739 data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
9eae7c52
TT
1740 data, end, addr_size);
1741 /* If we printed anything, or if we have an empty piece,
1742 then don't disassemble. */
1743 if (data != here
1744 || data[0] == DW_OP_piece
1745 || data[0] == DW_OP_bit_piece)
1746 disassemble = 0;
08922a10 1747 }
9eae7c52
TT
1748 if (disassemble)
1749 data = disassemble_dwarf_expression (stream, get_objfile_arch (objfile),
1750 addr_size, offset_size, data, end,
1751 dwarf2_always_disassemble);
1752
1753 if (data < end)
08922a10 1754 {
9eae7c52 1755 int empty = data == here;
08922a10 1756
9eae7c52
TT
1757 if (disassemble)
1758 fprintf_filtered (stream, " ");
1759 if (data[0] == DW_OP_piece)
1760 {
1761 ULONGEST bytes;
08922a10 1762
9eae7c52 1763 data = read_uleb128 (data + 1, end, &bytes);
08922a10 1764
9eae7c52
TT
1765 if (empty)
1766 fprintf_filtered (stream, _("an empty %s-byte piece"),
1767 pulongest (bytes));
1768 else
1769 fprintf_filtered (stream, _(" [%s-byte piece]"),
1770 pulongest (bytes));
1771 }
1772 else if (data[0] == DW_OP_bit_piece)
1773 {
1774 ULONGEST bits, offset;
1775
1776 data = read_uleb128 (data + 1, end, &bits);
1777 data = read_uleb128 (data, end, &offset);
1778
1779 if (empty)
1780 fprintf_filtered (stream,
1781 _("an empty %s-bit piece"),
1782 pulongest (bits));
1783 else
1784 fprintf_filtered (stream,
1785 _(" [%s-bit piece, offset %s bits]"),
1786 pulongest (bits), pulongest (offset));
1787 }
1788 else
1789 {
1790 bad = 1;
1791 break;
1792 }
08922a10
SS
1793 }
1794 }
1795
1796 if (bad || data > end)
1797 error (_("Corrupted DWARF2 expression for \"%s\"."),
1798 SYMBOL_PRINT_NAME (symbol));
1799}
1800
1801/* Print a natural-language description of SYMBOL to STREAM. This
1802 version is for a symbol with a single location. */
a55cc764 1803
08922a10
SS
1804static void
1805locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
1806 struct ui_file *stream)
1807{
1808 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1809 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1810 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
9eae7c52 1811 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
08922a10
SS
1812
1813 locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
9eae7c52 1814 objfile, addr_size, offset_size);
08922a10
SS
1815}
1816
1817/* Describe the location of SYMBOL as an agent value in VALUE, generating
1818 any necessary bytecode in AX. */
a55cc764 1819
0d53c4c4 1820static void
505e835d
UW
1821locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1822 struct agent_expr *ax, struct axs_value *value)
a55cc764
DJ
1823{
1824 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1825
505e835d
UW
1826 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
1827 dlbaton->data, dlbaton->size);
a55cc764
DJ
1828}
1829
4c2df51b
DJ
1830/* The set of location functions used with the DWARF-2 expression
1831 evaluator. */
768a979c 1832const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4c2df51b
DJ
1833 locexpr_read_variable,
1834 locexpr_read_needs_frame,
1835 locexpr_describe_location,
a55cc764 1836 locexpr_tracepoint_var_ref
4c2df51b 1837};
0d53c4c4
DJ
1838
1839
1840/* Wrapper functions for location lists. These generally find
1841 the appropriate location expression and call something above. */
1842
1843/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1844 evaluator to calculate the location. */
1845static struct value *
1846loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
1847{
1848 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1849 struct value *val;
947bb88f 1850 const gdb_byte *data;
b6b08ebf 1851 size_t size;
0d53c4c4
DJ
1852
1853 data = find_location_expression (dlbaton, &size,
22c6caba
JW
1854 frame ? get_frame_address_in_block (frame)
1855 : 0);
0d53c4c4 1856 if (data == NULL)
806048c6
DJ
1857 {
1858 val = allocate_value (SYMBOL_TYPE (symbol));
1859 VALUE_LVAL (val) = not_lval;
1860 set_value_optimized_out (val, 1);
1861 }
1862 else
a2d33775 1863 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
ae0d2f24 1864 dlbaton->per_cu);
0d53c4c4
DJ
1865
1866 return val;
1867}
1868
1869/* Return non-zero iff we need a frame to evaluate SYMBOL. */
1870static int
1871loclist_read_needs_frame (struct symbol *symbol)
1872{
1873 /* If there's a location list, then assume we need to have a frame
1874 to choose the appropriate location expression. With tracking of
1875 global variables this is not necessarily true, but such tracking
1876 is disabled in GCC at the moment until we figure out how to
1877 represent it. */
1878
1879 return 1;
1880}
1881
08922a10
SS
1882/* Print a natural-language description of SYMBOL to STREAM. This
1883 version applies when there is a list of different locations, each
1884 with a specified address range. */
1885
1886static void
1887loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
1888 struct ui_file *stream)
0d53c4c4 1889{
08922a10
SS
1890 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1891 CORE_ADDR low, high;
947bb88f 1892 const gdb_byte *loc_ptr, *buf_end;
08922a10
SS
1893 int length, first = 1;
1894 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
1895 struct gdbarch *gdbarch = get_objfile_arch (objfile);
1896 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1897 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
9eae7c52 1898 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
08922a10
SS
1899 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
1900 /* Adjust base_address for relocatable objects. */
1901 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
1902 SECT_OFF_TEXT (objfile));
1903 CORE_ADDR base_address = dlbaton->base_address + base_offset;
1904
1905 loc_ptr = dlbaton->data;
1906 buf_end = dlbaton->data + dlbaton->size;
1907
9eae7c52 1908 fprintf_filtered (stream, _("multi-location:\n"));
08922a10
SS
1909
1910 /* Iterate through locations until we run out. */
1911 while (1)
1912 {
1913 if (buf_end - loc_ptr < 2 * addr_size)
1914 error (_("Corrupted DWARF expression for symbol \"%s\"."),
1915 SYMBOL_PRINT_NAME (symbol));
1916
1917 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1918 loc_ptr += addr_size;
1919
1920 /* A base-address-selection entry. */
1921 if (low == base_mask)
1922 {
1923 base_address = dwarf2_read_address (gdbarch,
1924 loc_ptr, buf_end, addr_size);
9eae7c52 1925 fprintf_filtered (stream, _(" Base address %s"),
08922a10
SS
1926 paddress (gdbarch, base_address));
1927 loc_ptr += addr_size;
1928 continue;
1929 }
1930
1931 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
1932 loc_ptr += addr_size;
1933
1934 /* An end-of-list entry. */
1935 if (low == 0 && high == 0)
9eae7c52 1936 break;
08922a10
SS
1937
1938 /* Otherwise, a location expression entry. */
1939 low += base_address;
1940 high += base_address;
1941
1942 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
1943 loc_ptr += 2;
1944
08922a10
SS
1945 /* (It would improve readability to print only the minimum
1946 necessary digits of the second number of the range.) */
9eae7c52 1947 fprintf_filtered (stream, _(" Range %s-%s: "),
08922a10
SS
1948 paddress (gdbarch, low), paddress (gdbarch, high));
1949
1950 /* Now describe this particular location. */
1951 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
9eae7c52
TT
1952 objfile, addr_size, offset_size);
1953
1954 fprintf_filtered (stream, "\n");
08922a10
SS
1955
1956 loc_ptr += length;
1957 }
0d53c4c4
DJ
1958}
1959
1960/* Describe the location of SYMBOL as an agent value in VALUE, generating
1961 any necessary bytecode in AX. */
1962static void
505e835d
UW
1963loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
1964 struct agent_expr *ax, struct axs_value *value)
0d53c4c4
DJ
1965{
1966 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
947bb88f 1967 const gdb_byte *data;
b6b08ebf 1968 size_t size;
0d53c4c4
DJ
1969
1970 data = find_location_expression (dlbaton, &size, ax->scope);
0d53c4c4 1971
505e835d 1972 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
0d53c4c4
DJ
1973}
1974
1975/* The set of location functions used with the DWARF-2 expression
1976 evaluator and location lists. */
768a979c 1977const struct symbol_computed_ops dwarf2_loclist_funcs = {
0d53c4c4
DJ
1978 loclist_read_variable,
1979 loclist_read_needs_frame,
1980 loclist_describe_location,
1981 loclist_tracepoint_var_ref
1982};
This page took 0.614175 seconds and 4 git commands to generate.