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