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