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