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