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