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