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