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;
836 struct call_site_parameter *parameter;
837 CORE_ADDR target_addr;
838
839 if (gdbarch != frame_unwind_arch (frame))
840 {
841 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr);
842 struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
843
844 throw_error (NO_ENTRY_VALUE_ERROR,
845 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
846 "(of %s (%s)) does not match caller gdbarch %s"),
847 gdbarch_bfd_arch_info (gdbarch)->printable_name,
848 paddress (gdbarch, func_addr),
849 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym),
850 gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
851 }
852
853 if (caller_frame == NULL)
854 {
855 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr);
856
857 throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
858 "requires caller of %s (%s)"),
859 paddress (gdbarch, func_addr),
860 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
861 }
862 caller_pc = get_frame_pc (caller_frame);
863 call_site = call_site_for_pc (gdbarch, caller_pc);
864
865 target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
866 if (target_addr != func_addr)
867 {
868 struct minimal_symbol *target_msym, *func_msym;
869
870 target_msym = lookup_minimal_symbol_by_pc (target_addr);
871 func_msym = lookup_minimal_symbol_by_pc (func_addr);
872 throw_error (NO_ENTRY_VALUE_ERROR,
873 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
874 "but the called frame is for %s at %s"),
875 (target_msym == NULL ? "???"
876 : SYMBOL_PRINT_NAME (target_msym)),
877 paddress (gdbarch, target_addr),
878 func_msym == NULL ? "???" : SYMBOL_PRINT_NAME (func_msym),
879 paddress (gdbarch, func_addr));
880 }
881
2d6c5dc2
JK
882 /* No entry value based parameters would be reliable if this function can
883 call itself via tail calls. */
884 func_verify_no_selftailcall (gdbarch, func_addr);
885
8e3b41a9
JK
886 for (iparams = 0; iparams < call_site->parameter_count; iparams++)
887 {
888 parameter = &call_site->parameter[iparams];
889 if (parameter->dwarf_reg == -1 && dwarf_reg == -1)
890 {
891 if (parameter->fb_offset == fb_offset)
892 break;
893 }
894 else if (parameter->dwarf_reg == dwarf_reg)
895 break;
896 }
897 if (iparams == call_site->parameter_count)
898 {
899 struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (caller_pc);
900
901 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
902 determine its value. */
903 throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
904 "at DW_TAG_GNU_call_site %s at %s"),
905 paddress (gdbarch, caller_pc),
906 msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym));
907 }
908
909 *per_cu_return = call_site->per_cu;
910 return parameter;
911}
912
a471c594
JK
913/* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
914 the normal DW_AT_GNU_call_site_value block. Otherwise return the
915 DW_AT_GNU_call_site_data_value (dereferenced) block.
e18b2753
JK
916
917 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
918 struct value.
919
920 Function always returns non-NULL, non-optimized out value. It throws
921 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
922
923static struct value *
924dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
a471c594 925 CORE_ADDR deref_size, struct type *type,
e18b2753
JK
926 struct frame_info *caller_frame,
927 struct dwarf2_per_cu_data *per_cu)
928{
a471c594 929 const gdb_byte *data_src;
e18b2753 930 gdb_byte *data;
a471c594
JK
931 size_t size;
932
933 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
934 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
935
936 /* DEREF_SIZE size is not verified here. */
937 if (data_src == NULL)
938 throw_error (NO_ENTRY_VALUE_ERROR,
939 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
e18b2753
JK
940
941 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
942 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
943 DWARF block. */
a471c594
JK
944 data = alloca (size + 1);
945 memcpy (data, data_src, size);
946 data[size] = DW_OP_stack_value;
e18b2753 947
a471c594 948 return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
e18b2753
JK
949}
950
a471c594
JK
951/* Execute call_site_parameter's DWARF block matching DEREF_SIZE for caller of
952 the CTX's frame. CTX must be of dwarf_expr_ctx_funcs kind. See DWARF_REG
953 and FB_OFFSET description at struct
954 dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
8e3b41a9
JK
955
956 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
957 can be more simple as it does not support cross-CU DWARF executions. */
958
959static void
960dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
a471c594
JK
961 int dwarf_reg, CORE_ADDR fb_offset,
962 int deref_size)
8e3b41a9
JK
963{
964 struct dwarf_expr_baton *debaton;
965 struct frame_info *frame, *caller_frame;
966 struct dwarf2_per_cu_data *caller_per_cu;
967 struct dwarf_expr_baton baton_local;
968 struct dwarf_expr_context saved_ctx;
969 struct call_site_parameter *parameter;
970 const gdb_byte *data_src;
971 size_t size;
972
973 gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
974 debaton = ctx->baton;
975 frame = debaton->frame;
976 caller_frame = get_prev_frame (frame);
977
978 parameter = dwarf_expr_reg_to_entry_parameter (frame, dwarf_reg, fb_offset,
979 &caller_per_cu);
a471c594
JK
980 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
981 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
982
983 /* DEREF_SIZE size is not verified here. */
984 if (data_src == NULL)
985 throw_error (NO_ENTRY_VALUE_ERROR,
986 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
8e3b41a9
JK
987
988 baton_local.frame = caller_frame;
989 baton_local.per_cu = caller_per_cu;
990
991 saved_ctx.gdbarch = ctx->gdbarch;
992 saved_ctx.addr_size = ctx->addr_size;
993 saved_ctx.offset = ctx->offset;
994 saved_ctx.baton = ctx->baton;
995 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
996 ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
997 ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
998 ctx->baton = &baton_local;
999
1000 dwarf_expr_eval (ctx, data_src, size);
1001
1002 ctx->gdbarch = saved_ctx.gdbarch;
1003 ctx->addr_size = saved_ctx.addr_size;
1004 ctx->offset = saved_ctx.offset;
1005 ctx->baton = saved_ctx.baton;
1006}
1007
a471c594
JK
1008/* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1009 the indirect method on it, that is use its stored target value, the sole
1010 purpose of entry_data_value_funcs.. */
1011
1012static struct value *
1013entry_data_value_coerce_ref (const struct value *value)
1014{
1015 struct type *checked_type = check_typedef (value_type (value));
1016 struct value *target_val;
1017
1018 if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
1019 return NULL;
1020
1021 target_val = value_computed_closure (value);
1022 value_incref (target_val);
1023 return target_val;
1024}
1025
1026/* Implement copy_closure. */
1027
1028static void *
1029entry_data_value_copy_closure (const struct value *v)
1030{
1031 struct value *target_val = value_computed_closure (v);
1032
1033 value_incref (target_val);
1034 return target_val;
1035}
1036
1037/* Implement free_closure. */
1038
1039static void
1040entry_data_value_free_closure (struct value *v)
1041{
1042 struct value *target_val = value_computed_closure (v);
1043
1044 value_free (target_val);
1045}
1046
1047/* Vector for methods for an entry value reference where the referenced value
1048 is stored in the caller. On the first dereference use
1049 DW_AT_GNU_call_site_data_value in the caller. */
1050
1051static const struct lval_funcs entry_data_value_funcs =
1052{
1053 NULL, /* read */
1054 NULL, /* write */
1055 NULL, /* check_validity */
1056 NULL, /* check_any_valid */
1057 NULL, /* indirect */
1058 entry_data_value_coerce_ref,
1059 NULL, /* check_synthetic_pointer */
1060 entry_data_value_copy_closure,
1061 entry_data_value_free_closure
1062};
1063
e18b2753
JK
1064/* Read parameter of TYPE at (callee) FRAME's function entry. DWARF_REG and
1065 FB_OFFSET are used to match DW_AT_location at the caller's
1066 DW_TAG_GNU_call_site_parameter. See DWARF_REG and FB_OFFSET description at
1067 struct dwarf_expr_context_funcs->push_dwarf_reg_entry_value.
1068
1069 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1070 cannot resolve the parameter for any reason. */
1071
1072static struct value *
1073value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1074 int dwarf_reg, CORE_ADDR fb_offset)
1075{
a471c594
JK
1076 struct type *checked_type = check_typedef (type);
1077 struct type *target_type = TYPE_TARGET_TYPE (checked_type);
e18b2753 1078 struct frame_info *caller_frame = get_prev_frame (frame);
a471c594 1079 struct value *outer_val, *target_val, *val;
e18b2753
JK
1080 struct call_site_parameter *parameter;
1081 struct dwarf2_per_cu_data *caller_per_cu;
a471c594 1082 CORE_ADDR addr;
e18b2753
JK
1083
1084 parameter = dwarf_expr_reg_to_entry_parameter (frame, dwarf_reg, fb_offset,
1085 &caller_per_cu);
1086
a471c594
JK
1087 outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1088 type, caller_frame,
1089 caller_per_cu);
1090
1091 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1092 used and it is not available do not fall back to OUTER_VAL - dereferencing
1093 TYPE_CODE_REF with non-entry data value would give current value - not the
1094 entry value. */
1095
1096 if (TYPE_CODE (checked_type) != TYPE_CODE_REF
1097 || TYPE_TARGET_TYPE (checked_type) == NULL)
1098 return outer_val;
1099
1100 target_val = dwarf_entry_parameter_to_value (parameter,
1101 TYPE_LENGTH (target_type),
1102 target_type, caller_frame,
1103 caller_per_cu);
1104
1105 /* value_as_address dereferences TYPE_CODE_REF. */
1106 addr = extract_typed_address (value_contents (outer_val), checked_type);
1107
1108 /* The target entry value has artificial address of the entry value
1109 reference. */
1110 VALUE_LVAL (target_val) = lval_memory;
1111 set_value_address (target_val, addr);
1112
1113 release_value (target_val);
1114 val = allocate_computed_value (type, &entry_data_value_funcs,
1115 target_val /* closure */);
1116
1117 /* Copy the referencing pointer to the new computed value. */
1118 memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1119 TYPE_LENGTH (checked_type));
1120 set_value_lazy (val, 0);
1121
1122 return val;
e18b2753
JK
1123}
1124
1125/* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1126 SIZE are DWARF block used to match DW_AT_location at the caller's
1127 DW_TAG_GNU_call_site_parameter.
1128
1129 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1130 cannot resolve the parameter for any reason. */
1131
1132static struct value *
1133value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1134 const gdb_byte *block, size_t block_len)
1135{
1136 int dwarf_reg;
1137 CORE_ADDR fb_offset;
1138
1139 dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1140 if (dwarf_reg != -1)
1141 return value_of_dwarf_reg_entry (type, frame, dwarf_reg, 0 /* unused */);
1142
1143 if (dwarf_block_to_fb_offset (block, block + block_len, &fb_offset))
1144 return value_of_dwarf_reg_entry (type, frame, -1, fb_offset);
1145
1146 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1147 suppressed during normal operation. The expression can be arbitrary if
1148 there is no caller-callee entry value binding expected. */
1149 throw_error (NO_ENTRY_VALUE_ERROR,
1150 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1151 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1152}
1153
052b9502
NF
1154struct piece_closure
1155{
88bfdde4
TT
1156 /* Reference count. */
1157 int refc;
1158
8cf6f0b1
TT
1159 /* The CU from which this closure's expression came. */
1160 struct dwarf2_per_cu_data *per_cu;
1161
052b9502
NF
1162 /* The number of pieces used to describe this variable. */
1163 int n_pieces;
1164
6063c216
UW
1165 /* The target address size, used only for DWARF_VALUE_STACK. */
1166 int addr_size;
cec03d70 1167
052b9502
NF
1168 /* The pieces themselves. */
1169 struct dwarf_expr_piece *pieces;
1170};
1171
1172/* Allocate a closure for a value formed from separately-described
1173 PIECES. */
1174
1175static struct piece_closure *
8cf6f0b1
TT
1176allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1177 int n_pieces, struct dwarf_expr_piece *pieces,
6063c216 1178 int addr_size)
052b9502
NF
1179{
1180 struct piece_closure *c = XZALLOC (struct piece_closure);
8a9b8146 1181 int i;
052b9502 1182
88bfdde4 1183 c->refc = 1;
8cf6f0b1 1184 c->per_cu = per_cu;
052b9502 1185 c->n_pieces = n_pieces;
6063c216 1186 c->addr_size = addr_size;
052b9502
NF
1187 c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
1188
1189 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
8a9b8146
TT
1190 for (i = 0; i < n_pieces; ++i)
1191 if (c->pieces[i].location == DWARF_VALUE_STACK)
1192 value_incref (c->pieces[i].v.value);
052b9502
NF
1193
1194 return c;
1195}
1196
d3b1e874
TT
1197/* The lowest-level function to extract bits from a byte buffer.
1198 SOURCE is the buffer. It is updated if we read to the end of a
1199 byte.
1200 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1201 updated to reflect the number of bits actually read.
1202 NBITS is the number of bits we want to read. It is updated to
1203 reflect the number of bits actually read. This function may read
1204 fewer bits.
1205 BITS_BIG_ENDIAN is taken directly from gdbarch.
1206 This function returns the extracted bits. */
1207
1208static unsigned int
1209extract_bits_primitive (const gdb_byte **source,
1210 unsigned int *source_offset_bits,
1211 int *nbits, int bits_big_endian)
1212{
1213 unsigned int avail, mask, datum;
1214
1215 gdb_assert (*source_offset_bits < 8);
1216
1217 avail = 8 - *source_offset_bits;
1218 if (avail > *nbits)
1219 avail = *nbits;
1220
1221 mask = (1 << avail) - 1;
1222 datum = **source;
1223 if (bits_big_endian)
1224 datum >>= 8 - (*source_offset_bits + *nbits);
1225 else
1226 datum >>= *source_offset_bits;
1227 datum &= mask;
1228
1229 *nbits -= avail;
1230 *source_offset_bits += avail;
1231 if (*source_offset_bits >= 8)
1232 {
1233 *source_offset_bits -= 8;
1234 ++*source;
1235 }
1236
1237 return datum;
1238}
1239
1240/* Extract some bits from a source buffer and move forward in the
1241 buffer.
1242
1243 SOURCE is the source buffer. It is updated as bytes are read.
1244 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1245 bits are read.
1246 NBITS is the number of bits to read.
1247 BITS_BIG_ENDIAN is taken directly from gdbarch.
1248
1249 This function returns the bits that were read. */
1250
1251static unsigned int
1252extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
1253 int nbits, int bits_big_endian)
1254{
1255 unsigned int datum;
1256
1257 gdb_assert (nbits > 0 && nbits <= 8);
1258
1259 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
1260 bits_big_endian);
1261 if (nbits > 0)
1262 {
1263 unsigned int more;
1264
1265 more = extract_bits_primitive (source, source_offset_bits, &nbits,
1266 bits_big_endian);
1267 if (bits_big_endian)
1268 datum <<= nbits;
1269 else
1270 more <<= nbits;
1271 datum |= more;
1272 }
1273
1274 return datum;
1275}
1276
1277/* Write some bits into a buffer and move forward in the buffer.
1278
1279 DATUM is the bits to write. The low-order bits of DATUM are used.
1280 DEST is the destination buffer. It is updated as bytes are
1281 written.
1282 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1283 done.
1284 NBITS is the number of valid bits in DATUM.
1285 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1286
1287static void
1288insert_bits (unsigned int datum,
1289 gdb_byte *dest, unsigned int dest_offset_bits,
1290 int nbits, int bits_big_endian)
1291{
1292 unsigned int mask;
1293
8c814cdd 1294 gdb_assert (dest_offset_bits + nbits <= 8);
d3b1e874
TT
1295
1296 mask = (1 << nbits) - 1;
1297 if (bits_big_endian)
1298 {
1299 datum <<= 8 - (dest_offset_bits + nbits);
1300 mask <<= 8 - (dest_offset_bits + nbits);
1301 }
1302 else
1303 {
1304 datum <<= dest_offset_bits;
1305 mask <<= dest_offset_bits;
1306 }
1307
1308 gdb_assert ((datum & ~mask) == 0);
1309
1310 *dest = (*dest & ~mask) | datum;
1311}
1312
1313/* Copy bits from a source to a destination.
1314
1315 DEST is where the bits should be written.
1316 DEST_OFFSET_BITS is the bit offset into DEST.
1317 SOURCE is the source of bits.
1318 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1319 BIT_COUNT is the number of bits to copy.
1320 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1321
1322static void
1323copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
1324 const gdb_byte *source, unsigned int source_offset_bits,
1325 unsigned int bit_count,
1326 int bits_big_endian)
1327{
1328 unsigned int dest_avail;
1329 int datum;
1330
1331 /* Reduce everything to byte-size pieces. */
1332 dest += dest_offset_bits / 8;
1333 dest_offset_bits %= 8;
1334 source += source_offset_bits / 8;
1335 source_offset_bits %= 8;
1336
1337 dest_avail = 8 - dest_offset_bits % 8;
1338
1339 /* See if we can fill the first destination byte. */
1340 if (dest_avail < bit_count)
1341 {
1342 datum = extract_bits (&source, &source_offset_bits, dest_avail,
1343 bits_big_endian);
1344 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
1345 ++dest;
1346 dest_offset_bits = 0;
1347 bit_count -= dest_avail;
1348 }
1349
1350 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1351 than 8 bits remaining. */
1352 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
1353 for (; bit_count >= 8; bit_count -= 8)
1354 {
1355 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
1356 *dest++ = (gdb_byte) datum;
1357 }
1358
1359 /* Finally, we may have a few leftover bits. */
1360 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
1361 if (bit_count > 0)
1362 {
1363 datum = extract_bits (&source, &source_offset_bits, bit_count,
1364 bits_big_endian);
1365 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
1366 }
1367}
1368
052b9502
NF
1369static void
1370read_pieced_value (struct value *v)
1371{
1372 int i;
1373 long offset = 0;
d3b1e874 1374 ULONGEST bits_to_skip;
052b9502 1375 gdb_byte *contents;
3e43a32a
MS
1376 struct piece_closure *c
1377 = (struct piece_closure *) value_computed_closure (v);
052b9502 1378 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
afd74c5f 1379 size_t type_len;
d3b1e874
TT
1380 size_t buffer_size = 0;
1381 char *buffer = NULL;
1382 struct cleanup *cleanup;
1383 int bits_big_endian
1384 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
afd74c5f
TT
1385
1386 if (value_type (v) != value_enclosing_type (v))
1387 internal_error (__FILE__, __LINE__,
1388 _("Should not be able to create a lazy value with "
1389 "an enclosing type"));
052b9502 1390
d3b1e874
TT
1391 cleanup = make_cleanup (free_current_contents, &buffer);
1392
052b9502 1393 contents = value_contents_raw (v);
d3b1e874 1394 bits_to_skip = 8 * value_offset (v);
0e03807e
TT
1395 if (value_bitsize (v))
1396 {
1397 bits_to_skip += value_bitpos (v);
1398 type_len = value_bitsize (v);
1399 }
1400 else
1401 type_len = 8 * TYPE_LENGTH (value_type (v));
d3b1e874 1402
afd74c5f 1403 for (i = 0; i < c->n_pieces && offset < type_len; i++)
052b9502
NF
1404 {
1405 struct dwarf_expr_piece *p = &c->pieces[i];
d3b1e874
TT
1406 size_t this_size, this_size_bits;
1407 long dest_offset_bits, source_offset_bits, source_offset;
0d45f56e 1408 const gdb_byte *intermediate_buffer;
d3b1e874
TT
1409
1410 /* Compute size, source, and destination offsets for copying, in
1411 bits. */
1412 this_size_bits = p->size;
1413 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
afd74c5f 1414 {
d3b1e874 1415 bits_to_skip -= this_size_bits;
afd74c5f
TT
1416 continue;
1417 }
d3b1e874
TT
1418 if (this_size_bits > type_len - offset)
1419 this_size_bits = type_len - offset;
1420 if (bits_to_skip > 0)
afd74c5f 1421 {
d3b1e874
TT
1422 dest_offset_bits = 0;
1423 source_offset_bits = bits_to_skip;
1424 this_size_bits -= bits_to_skip;
1425 bits_to_skip = 0;
afd74c5f
TT
1426 }
1427 else
1428 {
d3b1e874
TT
1429 dest_offset_bits = offset;
1430 source_offset_bits = 0;
afd74c5f 1431 }
9a619af0 1432
d3b1e874
TT
1433 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1434 source_offset = source_offset_bits / 8;
1435 if (buffer_size < this_size)
1436 {
1437 buffer_size = this_size;
1438 buffer = xrealloc (buffer, buffer_size);
1439 }
1440 intermediate_buffer = buffer;
1441
1442 /* Copy from the source to DEST_BUFFER. */
cec03d70 1443 switch (p->location)
052b9502 1444 {
cec03d70
TT
1445 case DWARF_VALUE_REGISTER:
1446 {
1447 struct gdbarch *arch = get_frame_arch (frame);
8a9b8146 1448 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
afd74c5f 1449 int reg_offset = source_offset;
dcbf108f
UW
1450
1451 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
afd74c5f 1452 && this_size < register_size (arch, gdb_regnum))
d3b1e874
TT
1453 {
1454 /* Big-endian, and we want less than full size. */
1455 reg_offset = register_size (arch, gdb_regnum) - this_size;
1456 /* We want the lower-order THIS_SIZE_BITS of the bytes
1457 we extract from the register. */
1458 source_offset_bits += 8 * this_size - this_size_bits;
1459 }
dcbf108f 1460
63b4f126
MGD
1461 if (gdb_regnum != -1)
1462 {
8dccd430
PA
1463 int optim, unavail;
1464
1465 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1466 this_size, buffer,
1467 &optim, &unavail))
1468 {
1469 /* Just so garbage doesn't ever shine through. */
1470 memset (buffer, 0, this_size);
1471
1472 if (optim)
1473 set_value_optimized_out (v, 1);
1474 if (unavail)
1475 mark_value_bytes_unavailable (v, offset, this_size);
1476 }
63b4f126
MGD
1477 }
1478 else
1479 {
1480 error (_("Unable to access DWARF register number %s"),
8a9b8146 1481 paddress (arch, p->v.regno));
63b4f126 1482 }
cec03d70
TT
1483 }
1484 break;
1485
1486 case DWARF_VALUE_MEMORY:
e6ca34fc
PA
1487 read_value_memory (v, offset,
1488 p->v.mem.in_stack_memory,
1489 p->v.mem.addr + source_offset,
1490 buffer, this_size);
cec03d70
TT
1491 break;
1492
1493 case DWARF_VALUE_STACK:
1494 {
afd74c5f 1495 size_t n = this_size;
9a619af0 1496
afd74c5f
TT
1497 if (n > c->addr_size - source_offset)
1498 n = (c->addr_size >= source_offset
1499 ? c->addr_size - source_offset
1500 : 0);
1501 if (n == 0)
1502 {
1503 /* Nothing. */
1504 }
afd74c5f
TT
1505 else
1506 {
8a9b8146 1507 const gdb_byte *val_bytes = value_contents_all (p->v.value);
afd74c5f 1508
8a9b8146 1509 intermediate_buffer = val_bytes + source_offset;
afd74c5f 1510 }
cec03d70
TT
1511 }
1512 break;
1513
1514 case DWARF_VALUE_LITERAL:
1515 {
afd74c5f
TT
1516 size_t n = this_size;
1517
1518 if (n > p->v.literal.length - source_offset)
1519 n = (p->v.literal.length >= source_offset
1520 ? p->v.literal.length - source_offset
1521 : 0);
1522 if (n != 0)
d3b1e874 1523 intermediate_buffer = p->v.literal.data + source_offset;
cec03d70
TT
1524 }
1525 break;
1526
8cf6f0b1
TT
1527 /* These bits show up as zeros -- but do not cause the value
1528 to be considered optimized-out. */
1529 case DWARF_VALUE_IMPLICIT_POINTER:
1530 break;
1531
cb826367 1532 case DWARF_VALUE_OPTIMIZED_OUT:
0e03807e 1533 set_value_optimized_out (v, 1);
cb826367
TT
1534 break;
1535
cec03d70
TT
1536 default:
1537 internal_error (__FILE__, __LINE__, _("invalid location type"));
052b9502 1538 }
d3b1e874 1539
8cf6f0b1
TT
1540 if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1541 && p->location != DWARF_VALUE_IMPLICIT_POINTER)
d3b1e874
TT
1542 copy_bitwise (contents, dest_offset_bits,
1543 intermediate_buffer, source_offset_bits % 8,
1544 this_size_bits, bits_big_endian);
1545
1546 offset += this_size_bits;
052b9502 1547 }
d3b1e874
TT
1548
1549 do_cleanups (cleanup);
052b9502
NF
1550}
1551
1552static void
1553write_pieced_value (struct value *to, struct value *from)
1554{
1555 int i;
1556 long offset = 0;
d3b1e874 1557 ULONGEST bits_to_skip;
afd74c5f 1558 const gdb_byte *contents;
3e43a32a
MS
1559 struct piece_closure *c
1560 = (struct piece_closure *) value_computed_closure (to);
052b9502 1561 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
afd74c5f 1562 size_t type_len;
d3b1e874
TT
1563 size_t buffer_size = 0;
1564 char *buffer = NULL;
1565 struct cleanup *cleanup;
1566 int bits_big_endian
1567 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
052b9502
NF
1568
1569 if (frame == NULL)
1570 {
1571 set_value_optimized_out (to, 1);
1572 return;
1573 }
1574
d3b1e874
TT
1575 cleanup = make_cleanup (free_current_contents, &buffer);
1576
afd74c5f 1577 contents = value_contents (from);
d3b1e874 1578 bits_to_skip = 8 * value_offset (to);
0e03807e
TT
1579 if (value_bitsize (to))
1580 {
1581 bits_to_skip += value_bitpos (to);
1582 type_len = value_bitsize (to);
1583 }
1584 else
1585 type_len = 8 * TYPE_LENGTH (value_type (to));
1586
afd74c5f 1587 for (i = 0; i < c->n_pieces && offset < type_len; i++)
052b9502
NF
1588 {
1589 struct dwarf_expr_piece *p = &c->pieces[i];
d3b1e874
TT
1590 size_t this_size_bits, this_size;
1591 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1592 int need_bitwise;
1593 const gdb_byte *source_buffer;
afd74c5f 1594
d3b1e874
TT
1595 this_size_bits = p->size;
1596 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
afd74c5f 1597 {
d3b1e874 1598 bits_to_skip -= this_size_bits;
afd74c5f
TT
1599 continue;
1600 }
d3b1e874
TT
1601 if (this_size_bits > type_len - offset)
1602 this_size_bits = type_len - offset;
1603 if (bits_to_skip > 0)
afd74c5f 1604 {
d3b1e874
TT
1605 dest_offset_bits = bits_to_skip;
1606 source_offset_bits = 0;
1607 this_size_bits -= bits_to_skip;
1608 bits_to_skip = 0;
afd74c5f
TT
1609 }
1610 else
1611 {
d3b1e874
TT
1612 dest_offset_bits = 0;
1613 source_offset_bits = offset;
1614 }
1615
1616 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1617 source_offset = source_offset_bits / 8;
1618 dest_offset = dest_offset_bits / 8;
1619 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1620 {
1621 source_buffer = contents + source_offset;
1622 need_bitwise = 0;
1623 }
1624 else
1625 {
1626 if (buffer_size < this_size)
1627 {
1628 buffer_size = this_size;
1629 buffer = xrealloc (buffer, buffer_size);
1630 }
1631 source_buffer = buffer;
1632 need_bitwise = 1;
afd74c5f 1633 }
9a619af0 1634
cec03d70 1635 switch (p->location)
052b9502 1636 {
cec03d70
TT
1637 case DWARF_VALUE_REGISTER:
1638 {
1639 struct gdbarch *arch = get_frame_arch (frame);
8a9b8146 1640 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno);
afd74c5f 1641 int reg_offset = dest_offset;
dcbf108f
UW
1642
1643 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
afd74c5f 1644 && this_size <= register_size (arch, gdb_regnum))
dcbf108f 1645 /* Big-endian, and we want less than full size. */
afd74c5f 1646 reg_offset = register_size (arch, gdb_regnum) - this_size;
dcbf108f 1647
63b4f126
MGD
1648 if (gdb_regnum != -1)
1649 {
d3b1e874
TT
1650 if (need_bitwise)
1651 {
8dccd430
PA
1652 int optim, unavail;
1653
1654 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1655 this_size, buffer,
1656 &optim, &unavail))
1657 {
1658 if (optim)
1659 error (_("Can't do read-modify-write to "
1660 "update bitfield; containing word has been "
1661 "optimized out"));
1662 if (unavail)
1663 throw_error (NOT_AVAILABLE_ERROR,
1664 _("Can't do read-modify-write to update "
1665 "bitfield; containing word "
1666 "is unavailable"));
1667 }
d3b1e874
TT
1668 copy_bitwise (buffer, dest_offset_bits,
1669 contents, source_offset_bits,
1670 this_size_bits,
1671 bits_big_endian);
1672 }
1673
63b4f126 1674 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
d3b1e874 1675 this_size, source_buffer);
63b4f126
MGD
1676 }
1677 else
1678 {
1679 error (_("Unable to write to DWARF register number %s"),
8a9b8146 1680 paddress (arch, p->v.regno));
63b4f126 1681 }
cec03d70
TT
1682 }
1683 break;
1684 case DWARF_VALUE_MEMORY:
d3b1e874
TT
1685 if (need_bitwise)
1686 {
1687 /* Only the first and last bytes can possibly have any
1688 bits reused. */
f2c7657e
UW
1689 read_memory (p->v.mem.addr + dest_offset, buffer, 1);
1690 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
d3b1e874
TT
1691 buffer + this_size - 1, 1);
1692 copy_bitwise (buffer, dest_offset_bits,
1693 contents, source_offset_bits,
1694 this_size_bits,
1695 bits_big_endian);
1696 }
1697
f2c7657e 1698 write_memory (p->v.mem.addr + dest_offset,
d3b1e874 1699 source_buffer, this_size);
cec03d70
TT
1700 break;
1701 default:
1702 set_value_optimized_out (to, 1);
0e03807e 1703 break;
052b9502 1704 }
d3b1e874 1705 offset += this_size_bits;
052b9502 1706 }
d3b1e874 1707
d3b1e874 1708 do_cleanups (cleanup);
052b9502
NF
1709}
1710
8cf6f0b1
TT
1711/* A helper function that checks bit validity in a pieced value.
1712 CHECK_FOR indicates the kind of validity checking.
1713 DWARF_VALUE_MEMORY means to check whether any bit is valid.
1714 DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is
1715 optimized out.
1716 DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an
1717 implicit pointer. */
1718
0e03807e
TT
1719static int
1720check_pieced_value_bits (const struct value *value, int bit_offset,
8cf6f0b1
TT
1721 int bit_length,
1722 enum dwarf_value_location check_for)
0e03807e
TT
1723{
1724 struct piece_closure *c
1725 = (struct piece_closure *) value_computed_closure (value);
1726 int i;
8cf6f0b1
TT
1727 int validity = (check_for == DWARF_VALUE_MEMORY
1728 || check_for == DWARF_VALUE_IMPLICIT_POINTER);
0e03807e
TT
1729
1730 bit_offset += 8 * value_offset (value);
1731 if (value_bitsize (value))
1732 bit_offset += value_bitpos (value);
1733
1734 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1735 {
1736 struct dwarf_expr_piece *p = &c->pieces[i];
1737 size_t this_size_bits = p->size;
1738
1739 if (bit_offset > 0)
1740 {
1741 if (bit_offset >= this_size_bits)
1742 {
1743 bit_offset -= this_size_bits;
1744 continue;
1745 }
1746
1747 bit_length -= this_size_bits - bit_offset;
1748 bit_offset = 0;
1749 }
1750 else
1751 bit_length -= this_size_bits;
1752
8cf6f0b1
TT
1753 if (check_for == DWARF_VALUE_IMPLICIT_POINTER)
1754 {
1755 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1756 return 0;
1757 }
1758 else if (p->location == DWARF_VALUE_OPTIMIZED_OUT
1759 || p->location == DWARF_VALUE_IMPLICIT_POINTER)
0e03807e
TT
1760 {
1761 if (validity)
1762 return 0;
1763 }
1764 else
1765 {
1766 if (!validity)
1767 return 1;
1768 }
1769 }
1770
1771 return validity;
1772}
1773
1774static int
1775check_pieced_value_validity (const struct value *value, int bit_offset,
1776 int bit_length)
1777{
8cf6f0b1
TT
1778 return check_pieced_value_bits (value, bit_offset, bit_length,
1779 DWARF_VALUE_MEMORY);
0e03807e
TT
1780}
1781
1782static int
1783check_pieced_value_invalid (const struct value *value)
1784{
1785 return check_pieced_value_bits (value, 0,
8cf6f0b1
TT
1786 8 * TYPE_LENGTH (value_type (value)),
1787 DWARF_VALUE_OPTIMIZED_OUT);
1788}
1789
1790/* An implementation of an lval_funcs method to see whether a value is
1791 a synthetic pointer. */
1792
1793static int
1794check_pieced_synthetic_pointer (const struct value *value, int bit_offset,
1795 int bit_length)
1796{
1797 return check_pieced_value_bits (value, bit_offset, bit_length,
1798 DWARF_VALUE_IMPLICIT_POINTER);
1799}
1800
1801/* A wrapper function for get_frame_address_in_block. */
1802
1803static CORE_ADDR
1804get_frame_address_in_block_wrapper (void *baton)
1805{
1806 return get_frame_address_in_block (baton);
1807}
1808
1809/* An implementation of an lval_funcs method to indirect through a
1810 pointer. This handles the synthetic pointer case when needed. */
1811
1812static struct value *
1813indirect_pieced_value (struct value *value)
1814{
1815 struct piece_closure *c
1816 = (struct piece_closure *) value_computed_closure (value);
1817 struct type *type;
1818 struct frame_info *frame;
1819 struct dwarf2_locexpr_baton baton;
1820 int i, bit_offset, bit_length;
1821 struct dwarf_expr_piece *piece = NULL;
8cf6f0b1
TT
1822 LONGEST byte_offset;
1823
0e37a63c 1824 type = check_typedef (value_type (value));
8cf6f0b1
TT
1825 if (TYPE_CODE (type) != TYPE_CODE_PTR)
1826 return NULL;
1827
1828 bit_length = 8 * TYPE_LENGTH (type);
1829 bit_offset = 8 * value_offset (value);
1830 if (value_bitsize (value))
1831 bit_offset += value_bitpos (value);
1832
1833 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
1834 {
1835 struct dwarf_expr_piece *p = &c->pieces[i];
1836 size_t this_size_bits = p->size;
1837
1838 if (bit_offset > 0)
1839 {
1840 if (bit_offset >= this_size_bits)
1841 {
1842 bit_offset -= this_size_bits;
1843 continue;
1844 }
1845
1846 bit_length -= this_size_bits - bit_offset;
1847 bit_offset = 0;
1848 }
1849 else
1850 bit_length -= this_size_bits;
1851
1852 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
1853 return NULL;
1854
1855 if (bit_length != 0)
1856 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
1857
1858 piece = p;
1859 break;
1860 }
1861
1862 frame = get_selected_frame (_("No frame selected."));
543305c9
JK
1863
1864 /* This is an offset requested by GDB, such as value subcripts. */
8cf6f0b1
TT
1865 byte_offset = value_as_address (value);
1866
e0e40094 1867 gdb_assert (piece);
8cf6f0b1
TT
1868 baton = dwarf2_fetch_die_location_block (piece->v.ptr.die, c->per_cu,
1869 get_frame_address_in_block_wrapper,
1870 frame);
1871
d83e736b
JK
1872 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
1873 baton.data, baton.size, baton.per_cu,
1874 piece->v.ptr.offset + byte_offset);
0e03807e
TT
1875}
1876
052b9502 1877static void *
0e03807e 1878copy_pieced_value_closure (const struct value *v)
052b9502 1879{
3e43a32a
MS
1880 struct piece_closure *c
1881 = (struct piece_closure *) value_computed_closure (v);
052b9502 1882
88bfdde4
TT
1883 ++c->refc;
1884 return c;
052b9502
NF
1885}
1886
1887static void
1888free_pieced_value_closure (struct value *v)
1889{
3e43a32a
MS
1890 struct piece_closure *c
1891 = (struct piece_closure *) value_computed_closure (v);
052b9502 1892
88bfdde4
TT
1893 --c->refc;
1894 if (c->refc == 0)
1895 {
8a9b8146
TT
1896 int i;
1897
1898 for (i = 0; i < c->n_pieces; ++i)
1899 if (c->pieces[i].location == DWARF_VALUE_STACK)
1900 value_free (c->pieces[i].v.value);
1901
88bfdde4
TT
1902 xfree (c->pieces);
1903 xfree (c);
1904 }
052b9502
NF
1905}
1906
1907/* Functions for accessing a variable described by DW_OP_piece. */
c8f2448a 1908static const struct lval_funcs pieced_value_funcs = {
052b9502
NF
1909 read_pieced_value,
1910 write_pieced_value,
0e03807e
TT
1911 check_pieced_value_validity,
1912 check_pieced_value_invalid,
8cf6f0b1 1913 indirect_pieced_value,
a471c594 1914 NULL, /* coerce_ref */
8cf6f0b1 1915 check_pieced_synthetic_pointer,
052b9502
NF
1916 copy_pieced_value_closure,
1917 free_pieced_value_closure
1918};
1919
8cf6f0b1
TT
1920/* Helper function which throws an error if a synthetic pointer is
1921 invalid. */
1922
1923static void
1924invalid_synthetic_pointer (void)
1925{
3e43a32a
MS
1926 error (_("access outside bounds of object "
1927 "referenced via synthetic pointer"));
8cf6f0b1
TT
1928}
1929
9e8b7a03
JK
1930/* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
1931
1932static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
1933{
1934 dwarf_expr_read_reg,
1935 dwarf_expr_read_mem,
1936 dwarf_expr_frame_base,
1937 dwarf_expr_frame_cfa,
1938 dwarf_expr_frame_pc,
1939 dwarf_expr_tls_address,
1940 dwarf_expr_dwarf_call,
8e3b41a9
JK
1941 dwarf_expr_get_base_type,
1942 dwarf_expr_push_dwarf_reg_entry_value
9e8b7a03
JK
1943};
1944
4c2df51b 1945/* Evaluate a location description, starting at DATA and with length
8cf6f0b1
TT
1946 SIZE, to find the current location of variable of TYPE in the
1947 context of FRAME. BYTE_OFFSET is applied after the contents are
1948 computed. */
a2d33775 1949
8cf6f0b1
TT
1950static struct value *
1951dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
1952 const gdb_byte *data, unsigned short size,
1953 struct dwarf2_per_cu_data *per_cu,
1954 LONGEST byte_offset)
4c2df51b 1955{
4c2df51b
DJ
1956 struct value *retval;
1957 struct dwarf_expr_baton baton;
1958 struct dwarf_expr_context *ctx;
72fc29ff 1959 struct cleanup *old_chain, *value_chain;
ac56253d 1960 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
79e1a869 1961 volatile struct gdb_exception ex;
4c2df51b 1962
8cf6f0b1
TT
1963 if (byte_offset < 0)
1964 invalid_synthetic_pointer ();
1965
0d53c4c4 1966 if (size == 0)
a7035dbb 1967 return allocate_optimized_out_value (type);
0d53c4c4 1968
4c2df51b 1969 baton.frame = frame;
17ea53c3 1970 baton.per_cu = per_cu;
4c2df51b
DJ
1971
1972 ctx = new_dwarf_expr_context ();
4a227398 1973 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
72fc29ff 1974 value_chain = make_cleanup_value_free_to_mark (value_mark ());
4a227398 1975
ac56253d 1976 ctx->gdbarch = get_objfile_arch (objfile);
ae0d2f24 1977 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
181cebd4 1978 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
9aa1f1e3 1979 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
4c2df51b 1980 ctx->baton = &baton;
9e8b7a03 1981 ctx->funcs = &dwarf_expr_ctx_funcs;
4c2df51b 1982
79e1a869
PA
1983 TRY_CATCH (ex, RETURN_MASK_ERROR)
1984 {
1985 dwarf_expr_eval (ctx, data, size);
1986 }
1987 if (ex.reason < 0)
1988 {
1989 if (ex.error == NOT_AVAILABLE_ERROR)
1990 {
72fc29ff 1991 do_cleanups (old_chain);
79e1a869
PA
1992 retval = allocate_value (type);
1993 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
1994 return retval;
1995 }
8e3b41a9
JK
1996 else if (ex.error == NO_ENTRY_VALUE_ERROR)
1997 {
1998 if (entry_values_debug)
1999 exception_print (gdb_stdout, ex);
2000 do_cleanups (old_chain);
2001 return allocate_optimized_out_value (type);
2002 }
79e1a869
PA
2003 else
2004 throw_exception (ex);
2005 }
2006
87808bd6
JB
2007 if (ctx->num_pieces > 0)
2008 {
052b9502
NF
2009 struct piece_closure *c;
2010 struct frame_id frame_id = get_frame_id (frame);
8cf6f0b1
TT
2011 ULONGEST bit_size = 0;
2012 int i;
052b9502 2013
8cf6f0b1
TT
2014 for (i = 0; i < ctx->num_pieces; ++i)
2015 bit_size += ctx->pieces[i].size;
2016 if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
2017 invalid_synthetic_pointer ();
2018
2019 c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
6063c216 2020 ctx->addr_size);
72fc29ff
TT
2021 /* We must clean up the value chain after creating the piece
2022 closure but before allocating the result. */
2023 do_cleanups (value_chain);
a2d33775 2024 retval = allocate_computed_value (type, &pieced_value_funcs, c);
052b9502 2025 VALUE_FRAME_ID (retval) = frame_id;
8cf6f0b1 2026 set_value_offset (retval, byte_offset);
87808bd6 2027 }
4c2df51b
DJ
2028 else
2029 {
cec03d70
TT
2030 switch (ctx->location)
2031 {
2032 case DWARF_VALUE_REGISTER:
2033 {
2034 struct gdbarch *arch = get_frame_arch (frame);
8a9b8146 2035 ULONGEST dwarf_regnum = value_as_long (dwarf_expr_fetch (ctx, 0));
cec03d70 2036 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
9a619af0 2037
8cf6f0b1
TT
2038 if (byte_offset != 0)
2039 error (_("cannot use offset on synthetic pointer to register"));
72fc29ff 2040 do_cleanups (value_chain);
63b4f126 2041 if (gdb_regnum != -1)
a2d33775 2042 retval = value_from_register (type, gdb_regnum, frame);
63b4f126 2043 else
a2d33775
JK
2044 error (_("Unable to access DWARF register number %s"),
2045 paddress (arch, dwarf_regnum));
cec03d70
TT
2046 }
2047 break;
2048
2049 case DWARF_VALUE_MEMORY:
2050 {
f2c7657e 2051 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
44353522 2052 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
cec03d70 2053
72fc29ff 2054 do_cleanups (value_chain);
41e8491f 2055 retval = allocate_value_lazy (type);
cec03d70 2056 VALUE_LVAL (retval) = lval_memory;
44353522
DE
2057 if (in_stack_memory)
2058 set_value_stack (retval, 1);
8cf6f0b1 2059 set_value_address (retval, address + byte_offset);
cec03d70
TT
2060 }
2061 break;
2062
2063 case DWARF_VALUE_STACK:
2064 {
8a9b8146
TT
2065 struct value *value = dwarf_expr_fetch (ctx, 0);
2066 gdb_byte *contents;
2067 const gdb_byte *val_bytes;
2068 size_t n = TYPE_LENGTH (value_type (value));
cec03d70 2069
8cf6f0b1
TT
2070 if (byte_offset + TYPE_LENGTH (type) > n)
2071 invalid_synthetic_pointer ();
2072
8a9b8146
TT
2073 val_bytes = value_contents_all (value);
2074 val_bytes += byte_offset;
8cf6f0b1
TT
2075 n -= byte_offset;
2076
72fc29ff
TT
2077 /* Preserve VALUE because we are going to free values back
2078 to the mark, but we still need the value contents
2079 below. */
2080 value_incref (value);
2081 do_cleanups (value_chain);
2082 make_cleanup_value_free (value);
2083
a2d33775 2084 retval = allocate_value (type);
cec03d70 2085 contents = value_contents_raw (retval);
a2d33775 2086 if (n > TYPE_LENGTH (type))
b6cede78
JK
2087 {
2088 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2089
2090 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2091 val_bytes += n - TYPE_LENGTH (type);
2092 n = TYPE_LENGTH (type);
2093 }
8a9b8146 2094 memcpy (contents, val_bytes, n);
cec03d70
TT
2095 }
2096 break;
2097
2098 case DWARF_VALUE_LITERAL:
2099 {
2100 bfd_byte *contents;
8c814cdd 2101 const bfd_byte *ldata;
cec03d70
TT
2102 size_t n = ctx->len;
2103
8cf6f0b1
TT
2104 if (byte_offset + TYPE_LENGTH (type) > n)
2105 invalid_synthetic_pointer ();
2106
72fc29ff 2107 do_cleanups (value_chain);
a2d33775 2108 retval = allocate_value (type);
cec03d70 2109 contents = value_contents_raw (retval);
8cf6f0b1 2110
8c814cdd 2111 ldata = ctx->data + byte_offset;
8cf6f0b1
TT
2112 n -= byte_offset;
2113
a2d33775 2114 if (n > TYPE_LENGTH (type))
b6cede78
JK
2115 {
2116 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2117
2118 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2119 ldata += n - TYPE_LENGTH (type);
2120 n = TYPE_LENGTH (type);
2121 }
8c814cdd 2122 memcpy (contents, ldata, n);
cec03d70
TT
2123 }
2124 break;
2125
dd90784c 2126 case DWARF_VALUE_OPTIMIZED_OUT:
72fc29ff 2127 do_cleanups (value_chain);
a7035dbb 2128 retval = allocate_optimized_out_value (type);
dd90784c
JK
2129 break;
2130
8cf6f0b1
TT
2131 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2132 operation by execute_stack_op. */
2133 case DWARF_VALUE_IMPLICIT_POINTER:
cb826367
TT
2134 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2135 it can only be encountered when making a piece. */
cec03d70
TT
2136 default:
2137 internal_error (__FILE__, __LINE__, _("invalid location type"));
2138 }
4c2df51b
DJ
2139 }
2140
42be36b3
CT
2141 set_value_initialized (retval, ctx->initialized);
2142
4a227398 2143 do_cleanups (old_chain);
4c2df51b
DJ
2144
2145 return retval;
2146}
8cf6f0b1
TT
2147
2148/* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2149 passes 0 as the byte_offset. */
2150
2151struct value *
2152dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2153 const gdb_byte *data, unsigned short size,
2154 struct dwarf2_per_cu_data *per_cu)
2155{
2156 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2157}
2158
4c2df51b
DJ
2159\f
2160/* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
2161
2162struct needs_frame_baton
2163{
2164 int needs_frame;
17ea53c3 2165 struct dwarf2_per_cu_data *per_cu;
4c2df51b
DJ
2166};
2167
2168/* Reads from registers do require a frame. */
2169static CORE_ADDR
61fbb938 2170needs_frame_read_reg (void *baton, int regnum)
4c2df51b
DJ
2171{
2172 struct needs_frame_baton *nf_baton = baton;
9a619af0 2173
4c2df51b
DJ
2174 nf_baton->needs_frame = 1;
2175 return 1;
2176}
2177
2178/* Reads from memory do not require a frame. */
2179static void
852483bc 2180needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
4c2df51b
DJ
2181{
2182 memset (buf, 0, len);
2183}
2184
2185/* Frame-relative accesses do require a frame. */
2186static void
0d45f56e 2187needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
4c2df51b 2188{
852483bc 2189 static gdb_byte lit0 = DW_OP_lit0;
4c2df51b
DJ
2190 struct needs_frame_baton *nf_baton = baton;
2191
2192 *start = &lit0;
2193 *length = 1;
2194
2195 nf_baton->needs_frame = 1;
2196}
2197
e7802207
TT
2198/* CFA accesses require a frame. */
2199
2200static CORE_ADDR
2201needs_frame_frame_cfa (void *baton)
2202{
2203 struct needs_frame_baton *nf_baton = baton;
9a619af0 2204
e7802207
TT
2205 nf_baton->needs_frame = 1;
2206 return 1;
2207}
2208
4c2df51b
DJ
2209/* Thread-local accesses do require a frame. */
2210static CORE_ADDR
2211needs_frame_tls_address (void *baton, CORE_ADDR offset)
2212{
2213 struct needs_frame_baton *nf_baton = baton;
9a619af0 2214
4c2df51b
DJ
2215 nf_baton->needs_frame = 1;
2216 return 1;
2217}
2218
5c631832
JK
2219/* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */
2220
2221static void
2222needs_frame_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
2223{
2224 struct needs_frame_baton *nf_baton = ctx->baton;
2225
37b50a69 2226 per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
9e8b7a03 2227 ctx->funcs->get_frame_pc, ctx->baton);
5c631832
JK
2228}
2229
8e3b41a9
JK
2230/* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
2231
2232static void
2233needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
a471c594 2234 int dwarf_reg, CORE_ADDR fb_offset, int deref_size)
8e3b41a9
JK
2235{
2236 struct needs_frame_baton *nf_baton = ctx->baton;
2237
2238 nf_baton->needs_frame = 1;
2239}
2240
9e8b7a03
JK
2241/* Virtual method table for dwarf2_loc_desc_needs_frame below. */
2242
2243static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs =
2244{
2245 needs_frame_read_reg,
2246 needs_frame_read_mem,
2247 needs_frame_frame_base,
2248 needs_frame_frame_cfa,
2249 needs_frame_frame_cfa, /* get_frame_pc */
2250 needs_frame_tls_address,
2251 needs_frame_dwarf_call,
8e3b41a9
JK
2252 NULL, /* get_base_type */
2253 needs_dwarf_reg_entry_value
9e8b7a03
JK
2254};
2255
4c2df51b
DJ
2256/* Return non-zero iff the location expression at DATA (length SIZE)
2257 requires a frame to evaluate. */
2258
2259static int
947bb88f 2260dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
ae0d2f24 2261 struct dwarf2_per_cu_data *per_cu)
4c2df51b
DJ
2262{
2263 struct needs_frame_baton baton;
2264 struct dwarf_expr_context *ctx;
f630a401 2265 int in_reg;
4a227398 2266 struct cleanup *old_chain;
ac56253d 2267 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
4c2df51b
DJ
2268
2269 baton.needs_frame = 0;
17ea53c3 2270 baton.per_cu = per_cu;
4c2df51b
DJ
2271
2272 ctx = new_dwarf_expr_context ();
4a227398 2273 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
72fc29ff 2274 make_cleanup_value_free_to_mark (value_mark ());
4a227398 2275
ac56253d 2276 ctx->gdbarch = get_objfile_arch (objfile);
ae0d2f24 2277 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
181cebd4 2278 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
9aa1f1e3 2279 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
4c2df51b 2280 ctx->baton = &baton;
9e8b7a03 2281 ctx->funcs = &needs_frame_ctx_funcs;
4c2df51b
DJ
2282
2283 dwarf_expr_eval (ctx, data, size);
2284
cec03d70 2285 in_reg = ctx->location == DWARF_VALUE_REGISTER;
f630a401 2286
87808bd6
JB
2287 if (ctx->num_pieces > 0)
2288 {
2289 int i;
2290
2291 /* If the location has several pieces, and any of them are in
2292 registers, then we will need a frame to fetch them from. */
2293 for (i = 0; i < ctx->num_pieces; i++)
cec03d70 2294 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
87808bd6
JB
2295 in_reg = 1;
2296 }
2297
4a227398 2298 do_cleanups (old_chain);
4c2df51b 2299
f630a401 2300 return baton.needs_frame || in_reg;
4c2df51b
DJ
2301}
2302
3cf03773
TT
2303/* A helper function that throws an unimplemented error mentioning a
2304 given DWARF operator. */
2305
2306static void
2307unimplemented (unsigned int op)
0d53c4c4 2308{
b1bfef65
TT
2309 const char *name = dwarf_stack_op_name (op);
2310
2311 if (name)
2312 error (_("DWARF operator %s cannot be translated to an agent expression"),
2313 name);
2314 else
1ba1b353
TT
2315 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2316 "to an agent expression"),
b1bfef65 2317 op);
3cf03773 2318}
08922a10 2319
3cf03773
TT
2320/* A helper function to convert a DWARF register to an arch register.
2321 ARCH is the architecture.
2322 DWARF_REG is the register.
2323 This will throw an exception if the DWARF register cannot be
2324 translated to an architecture register. */
08922a10 2325
3cf03773
TT
2326static int
2327translate_register (struct gdbarch *arch, int dwarf_reg)
2328{
2329 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2330 if (reg == -1)
2331 error (_("Unable to access DWARF register number %d"), dwarf_reg);
2332 return reg;
2333}
08922a10 2334
3cf03773
TT
2335/* A helper function that emits an access to memory. ARCH is the
2336 target architecture. EXPR is the expression which we are building.
2337 NBITS is the number of bits we want to read. This emits the
2338 opcodes needed to read the memory and then extract the desired
2339 bits. */
08922a10 2340
3cf03773
TT
2341static void
2342access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
08922a10 2343{
3cf03773
TT
2344 ULONGEST nbytes = (nbits + 7) / 8;
2345
2346 gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
2347
2348 if (trace_kludge)
2349 ax_trace_quick (expr, nbytes);
2350
2351 if (nbits <= 8)
2352 ax_simple (expr, aop_ref8);
2353 else if (nbits <= 16)
2354 ax_simple (expr, aop_ref16);
2355 else if (nbits <= 32)
2356 ax_simple (expr, aop_ref32);
2357 else
2358 ax_simple (expr, aop_ref64);
2359
2360 /* If we read exactly the number of bytes we wanted, we're done. */
2361 if (8 * nbytes == nbits)
2362 return;
2363
2364 if (gdbarch_bits_big_endian (arch))
0d53c4c4 2365 {
3cf03773
TT
2366 /* On a bits-big-endian machine, we want the high-order
2367 NBITS. */
2368 ax_const_l (expr, 8 * nbytes - nbits);
2369 ax_simple (expr, aop_rsh_unsigned);
0d53c4c4 2370 }
3cf03773 2371 else
0d53c4c4 2372 {
3cf03773
TT
2373 /* On a bits-little-endian box, we want the low-order NBITS. */
2374 ax_zero_ext (expr, nbits);
0d53c4c4 2375 }
3cf03773 2376}
0936ad1d 2377
8cf6f0b1
TT
2378/* A helper function to return the frame's PC. */
2379
2380static CORE_ADDR
2381get_ax_pc (void *baton)
2382{
2383 struct agent_expr *expr = baton;
2384
2385 return expr->scope;
2386}
2387
3cf03773
TT
2388/* Compile a DWARF location expression to an agent expression.
2389
2390 EXPR is the agent expression we are building.
2391 LOC is the agent value we modify.
2392 ARCH is the architecture.
2393 ADDR_SIZE is the size of addresses, in bytes.
2394 OP_PTR is the start of the location expression.
2395 OP_END is one past the last byte of the location expression.
2396
2397 This will throw an exception for various kinds of errors -- for
2398 example, if the expression cannot be compiled, or if the expression
2399 is invalid. */
0936ad1d 2400
9f6f94ff
TT
2401void
2402dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
2403 struct gdbarch *arch, unsigned int addr_size,
2404 const gdb_byte *op_ptr, const gdb_byte *op_end,
2405 struct dwarf2_per_cu_data *per_cu)
3cf03773
TT
2406{
2407 struct cleanup *cleanups;
2408 int i, *offsets;
2409 VEC(int) *dw_labels = NULL, *patches = NULL;
2410 const gdb_byte * const base = op_ptr;
2411 const gdb_byte *previous_piece = op_ptr;
2412 enum bfd_endian byte_order = gdbarch_byte_order (arch);
2413 ULONGEST bits_collected = 0;
2414 unsigned int addr_size_bits = 8 * addr_size;
2415 int bits_big_endian = gdbarch_bits_big_endian (arch);
0936ad1d 2416
3cf03773
TT
2417 offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
2418 cleanups = make_cleanup (xfree, offsets);
0936ad1d 2419
3cf03773
TT
2420 for (i = 0; i < op_end - op_ptr; ++i)
2421 offsets[i] = -1;
0936ad1d 2422
3cf03773
TT
2423 make_cleanup (VEC_cleanup (int), &dw_labels);
2424 make_cleanup (VEC_cleanup (int), &patches);
0936ad1d 2425
3cf03773
TT
2426 /* By default we are making an address. */
2427 loc->kind = axs_lvalue_memory;
0d45f56e 2428
3cf03773
TT
2429 while (op_ptr < op_end)
2430 {
2431 enum dwarf_location_atom op = *op_ptr;
3cf03773
TT
2432 ULONGEST uoffset, reg;
2433 LONGEST offset;
2434 int i;
2435
2436 offsets[op_ptr - base] = expr->len;
2437 ++op_ptr;
2438
2439 /* Our basic approach to code generation is to map DWARF
2440 operations directly to AX operations. However, there are
2441 some differences.
2442
2443 First, DWARF works on address-sized units, but AX always uses
2444 LONGEST. For most operations we simply ignore this
2445 difference; instead we generate sign extensions as needed
2446 before division and comparison operations. It would be nice
2447 to omit the sign extensions, but there is no way to determine
2448 the size of the target's LONGEST. (This code uses the size
2449 of the host LONGEST in some cases -- that is a bug but it is
2450 difficult to fix.)
2451
2452 Second, some DWARF operations cannot be translated to AX.
2453 For these we simply fail. See
2454 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
2455 switch (op)
0936ad1d 2456 {
3cf03773
TT
2457 case DW_OP_lit0:
2458 case DW_OP_lit1:
2459 case DW_OP_lit2:
2460 case DW_OP_lit3:
2461 case DW_OP_lit4:
2462 case DW_OP_lit5:
2463 case DW_OP_lit6:
2464 case DW_OP_lit7:
2465 case DW_OP_lit8:
2466 case DW_OP_lit9:
2467 case DW_OP_lit10:
2468 case DW_OP_lit11:
2469 case DW_OP_lit12:
2470 case DW_OP_lit13:
2471 case DW_OP_lit14:
2472 case DW_OP_lit15:
2473 case DW_OP_lit16:
2474 case DW_OP_lit17:
2475 case DW_OP_lit18:
2476 case DW_OP_lit19:
2477 case DW_OP_lit20:
2478 case DW_OP_lit21:
2479 case DW_OP_lit22:
2480 case DW_OP_lit23:
2481 case DW_OP_lit24:
2482 case DW_OP_lit25:
2483 case DW_OP_lit26:
2484 case DW_OP_lit27:
2485 case DW_OP_lit28:
2486 case DW_OP_lit29:
2487 case DW_OP_lit30:
2488 case DW_OP_lit31:
2489 ax_const_l (expr, op - DW_OP_lit0);
2490 break;
0d53c4c4 2491
3cf03773 2492 case DW_OP_addr:
ac56253d 2493 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
3cf03773 2494 op_ptr += addr_size;
ac56253d
TT
2495 /* Some versions of GCC emit DW_OP_addr before
2496 DW_OP_GNU_push_tls_address. In this case the value is an
2497 index, not an address. We don't support things like
2498 branching between the address and the TLS op. */
2499 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
9aa1f1e3 2500 uoffset += dwarf2_per_cu_text_offset (per_cu);
ac56253d 2501 ax_const_l (expr, uoffset);
3cf03773 2502 break;
4c2df51b 2503
3cf03773
TT
2504 case DW_OP_const1u:
2505 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
2506 op_ptr += 1;
2507 break;
2508 case DW_OP_const1s:
2509 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
2510 op_ptr += 1;
2511 break;
2512 case DW_OP_const2u:
2513 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
2514 op_ptr += 2;
2515 break;
2516 case DW_OP_const2s:
2517 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
2518 op_ptr += 2;
2519 break;
2520 case DW_OP_const4u:
2521 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
2522 op_ptr += 4;
2523 break;
2524 case DW_OP_const4s:
2525 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
2526 op_ptr += 4;
2527 break;
2528 case DW_OP_const8u:
2529 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
2530 op_ptr += 8;
2531 break;
2532 case DW_OP_const8s:
2533 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
2534 op_ptr += 8;
2535 break;
2536 case DW_OP_constu:
2537 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
2538 ax_const_l (expr, uoffset);
2539 break;
2540 case DW_OP_consts:
2541 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2542 ax_const_l (expr, offset);
2543 break;
9c238357 2544
3cf03773
TT
2545 case DW_OP_reg0:
2546 case DW_OP_reg1:
2547 case DW_OP_reg2:
2548 case DW_OP_reg3:
2549 case DW_OP_reg4:
2550 case DW_OP_reg5:
2551 case DW_OP_reg6:
2552 case DW_OP_reg7:
2553 case DW_OP_reg8:
2554 case DW_OP_reg9:
2555 case DW_OP_reg10:
2556 case DW_OP_reg11:
2557 case DW_OP_reg12:
2558 case DW_OP_reg13:
2559 case DW_OP_reg14:
2560 case DW_OP_reg15:
2561 case DW_OP_reg16:
2562 case DW_OP_reg17:
2563 case DW_OP_reg18:
2564 case DW_OP_reg19:
2565 case DW_OP_reg20:
2566 case DW_OP_reg21:
2567 case DW_OP_reg22:
2568 case DW_OP_reg23:
2569 case DW_OP_reg24:
2570 case DW_OP_reg25:
2571 case DW_OP_reg26:
2572 case DW_OP_reg27:
2573 case DW_OP_reg28:
2574 case DW_OP_reg29:
2575 case DW_OP_reg30:
2576 case DW_OP_reg31:
2577 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2578 loc->u.reg = translate_register (arch, op - DW_OP_reg0);
2579 loc->kind = axs_lvalue_register;
2580 break;
9c238357 2581
3cf03773
TT
2582 case DW_OP_regx:
2583 op_ptr = read_uleb128 (op_ptr, op_end, &reg);
2584 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
2585 loc->u.reg = translate_register (arch, reg);
2586 loc->kind = axs_lvalue_register;
2587 break;
08922a10 2588
3cf03773
TT
2589 case DW_OP_implicit_value:
2590 {
2591 ULONGEST len;
2592
2593 op_ptr = read_uleb128 (op_ptr, op_end, &len);
2594 if (op_ptr + len > op_end)
2595 error (_("DW_OP_implicit_value: too few bytes available."));
2596 if (len > sizeof (ULONGEST))
2597 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
2598 (int) len);
2599
2600 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
2601 byte_order));
2602 op_ptr += len;
2603 dwarf_expr_require_composition (op_ptr, op_end,
2604 "DW_OP_implicit_value");
2605
2606 loc->kind = axs_rvalue;
2607 }
2608 break;
08922a10 2609
3cf03773
TT
2610 case DW_OP_stack_value:
2611 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
2612 loc->kind = axs_rvalue;
2613 break;
08922a10 2614
3cf03773
TT
2615 case DW_OP_breg0:
2616 case DW_OP_breg1:
2617 case DW_OP_breg2:
2618 case DW_OP_breg3:
2619 case DW_OP_breg4:
2620 case DW_OP_breg5:
2621 case DW_OP_breg6:
2622 case DW_OP_breg7:
2623 case DW_OP_breg8:
2624 case DW_OP_breg9:
2625 case DW_OP_breg10:
2626 case DW_OP_breg11:
2627 case DW_OP_breg12:
2628 case DW_OP_breg13:
2629 case DW_OP_breg14:
2630 case DW_OP_breg15:
2631 case DW_OP_breg16:
2632 case DW_OP_breg17:
2633 case DW_OP_breg18:
2634 case DW_OP_breg19:
2635 case DW_OP_breg20:
2636 case DW_OP_breg21:
2637 case DW_OP_breg22:
2638 case DW_OP_breg23:
2639 case DW_OP_breg24:
2640 case DW_OP_breg25:
2641 case DW_OP_breg26:
2642 case DW_OP_breg27:
2643 case DW_OP_breg28:
2644 case DW_OP_breg29:
2645 case DW_OP_breg30:
2646 case DW_OP_breg31:
2647 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2648 i = translate_register (arch, op - DW_OP_breg0);
2649 ax_reg (expr, i);
2650 if (offset != 0)
2651 {
2652 ax_const_l (expr, offset);
2653 ax_simple (expr, aop_add);
2654 }
2655 break;
2656 case DW_OP_bregx:
2657 {
2658 op_ptr = read_uleb128 (op_ptr, op_end, &reg);
2659 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
2660 i = translate_register (arch, reg);
2661 ax_reg (expr, i);
2662 if (offset != 0)
2663 {
2664 ax_const_l (expr, offset);
2665 ax_simple (expr, aop_add);
2666 }
2667 }
2668 break;
2669 case DW_OP_fbreg:
2670 {
2671 const gdb_byte *datastart;
2672 size_t datalen;
2673 unsigned int before_stack_len;
2674 struct block *b;
2675 struct symbol *framefunc;
2676 LONGEST base_offset = 0;
08922a10 2677
3cf03773
TT
2678 b = block_for_pc (expr->scope);
2679
2680 if (!b)
2681 error (_("No block found for address"));
2682
2683 framefunc = block_linkage_function (b);
2684
2685 if (!framefunc)
2686 error (_("No function found for block"));
2687
2688 dwarf_expr_frame_base_1 (framefunc, expr->scope,
2689 &datastart, &datalen);
2690
2691 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
9f6f94ff
TT
2692 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
2693 datastart + datalen, per_cu);
3cf03773
TT
2694
2695 if (offset != 0)
2696 {
2697 ax_const_l (expr, offset);
2698 ax_simple (expr, aop_add);
2699 }
2700
2701 loc->kind = axs_lvalue_memory;
2702 }
08922a10 2703 break;
08922a10 2704
3cf03773
TT
2705 case DW_OP_dup:
2706 ax_simple (expr, aop_dup);
2707 break;
08922a10 2708
3cf03773
TT
2709 case DW_OP_drop:
2710 ax_simple (expr, aop_pop);
2711 break;
08922a10 2712
3cf03773
TT
2713 case DW_OP_pick:
2714 offset = *op_ptr++;
c7f96d2b 2715 ax_pick (expr, offset);
3cf03773
TT
2716 break;
2717
2718 case DW_OP_swap:
2719 ax_simple (expr, aop_swap);
2720 break;
08922a10 2721
3cf03773 2722 case DW_OP_over:
c7f96d2b 2723 ax_pick (expr, 1);
3cf03773 2724 break;
08922a10 2725
3cf03773 2726 case DW_OP_rot:
c7f96d2b 2727 ax_simple (expr, aop_rot);
3cf03773 2728 break;
08922a10 2729
3cf03773
TT
2730 case DW_OP_deref:
2731 case DW_OP_deref_size:
2732 {
2733 int size;
08922a10 2734
3cf03773
TT
2735 if (op == DW_OP_deref_size)
2736 size = *op_ptr++;
2737 else
2738 size = addr_size;
2739
2740 switch (size)
2741 {
2742 case 8:
2743 ax_simple (expr, aop_ref8);
2744 break;
2745 case 16:
2746 ax_simple (expr, aop_ref16);
2747 break;
2748 case 32:
2749 ax_simple (expr, aop_ref32);
2750 break;
2751 case 64:
2752 ax_simple (expr, aop_ref64);
2753 break;
2754 default:
b1bfef65
TT
2755 /* Note that dwarf_stack_op_name will never return
2756 NULL here. */
3cf03773 2757 error (_("Unsupported size %d in %s"),
b1bfef65 2758 size, dwarf_stack_op_name (op));
3cf03773
TT
2759 }
2760 }
2761 break;
2762
2763 case DW_OP_abs:
2764 /* Sign extend the operand. */
2765 ax_ext (expr, addr_size_bits);
2766 ax_simple (expr, aop_dup);
2767 ax_const_l (expr, 0);
2768 ax_simple (expr, aop_less_signed);
2769 ax_simple (expr, aop_log_not);
2770 i = ax_goto (expr, aop_if_goto);
2771 /* We have to emit 0 - X. */
2772 ax_const_l (expr, 0);
2773 ax_simple (expr, aop_swap);
2774 ax_simple (expr, aop_sub);
2775 ax_label (expr, i, expr->len);
2776 break;
2777
2778 case DW_OP_neg:
2779 /* No need to sign extend here. */
2780 ax_const_l (expr, 0);
2781 ax_simple (expr, aop_swap);
2782 ax_simple (expr, aop_sub);
2783 break;
2784
2785 case DW_OP_not:
2786 /* Sign extend the operand. */
2787 ax_ext (expr, addr_size_bits);
2788 ax_simple (expr, aop_bit_not);
2789 break;
2790
2791 case DW_OP_plus_uconst:
2792 op_ptr = read_uleb128 (op_ptr, op_end, &reg);
2793 /* It would be really weird to emit `DW_OP_plus_uconst 0',
2794 but we micro-optimize anyhow. */
2795 if (reg != 0)
2796 {
2797 ax_const_l (expr, reg);
2798 ax_simple (expr, aop_add);
2799 }
2800 break;
2801
2802 case DW_OP_and:
2803 ax_simple (expr, aop_bit_and);
2804 break;
2805
2806 case DW_OP_div:
2807 /* Sign extend the operands. */
2808 ax_ext (expr, addr_size_bits);
2809 ax_simple (expr, aop_swap);
2810 ax_ext (expr, addr_size_bits);
2811 ax_simple (expr, aop_swap);
2812 ax_simple (expr, aop_div_signed);
08922a10
SS
2813 break;
2814
3cf03773
TT
2815 case DW_OP_minus:
2816 ax_simple (expr, aop_sub);
2817 break;
2818
2819 case DW_OP_mod:
2820 ax_simple (expr, aop_rem_unsigned);
2821 break;
2822
2823 case DW_OP_mul:
2824 ax_simple (expr, aop_mul);
2825 break;
2826
2827 case DW_OP_or:
2828 ax_simple (expr, aop_bit_or);
2829 break;
2830
2831 case DW_OP_plus:
2832 ax_simple (expr, aop_add);
2833 break;
2834
2835 case DW_OP_shl:
2836 ax_simple (expr, aop_lsh);
2837 break;
2838
2839 case DW_OP_shr:
2840 ax_simple (expr, aop_rsh_unsigned);
2841 break;
2842
2843 case DW_OP_shra:
2844 ax_simple (expr, aop_rsh_signed);
2845 break;
2846
2847 case DW_OP_xor:
2848 ax_simple (expr, aop_bit_xor);
2849 break;
2850
2851 case DW_OP_le:
2852 /* Sign extend the operands. */
2853 ax_ext (expr, addr_size_bits);
2854 ax_simple (expr, aop_swap);
2855 ax_ext (expr, addr_size_bits);
2856 /* Note no swap here: A <= B is !(B < A). */
2857 ax_simple (expr, aop_less_signed);
2858 ax_simple (expr, aop_log_not);
2859 break;
2860
2861 case DW_OP_ge:
2862 /* Sign extend the operands. */
2863 ax_ext (expr, addr_size_bits);
2864 ax_simple (expr, aop_swap);
2865 ax_ext (expr, addr_size_bits);
2866 ax_simple (expr, aop_swap);
2867 /* A >= B is !(A < B). */
2868 ax_simple (expr, aop_less_signed);
2869 ax_simple (expr, aop_log_not);
2870 break;
2871
2872 case DW_OP_eq:
2873 /* Sign extend the operands. */
2874 ax_ext (expr, addr_size_bits);
2875 ax_simple (expr, aop_swap);
2876 ax_ext (expr, addr_size_bits);
2877 /* No need for a second swap here. */
2878 ax_simple (expr, aop_equal);
2879 break;
2880
2881 case DW_OP_lt:
2882 /* Sign extend the operands. */
2883 ax_ext (expr, addr_size_bits);
2884 ax_simple (expr, aop_swap);
2885 ax_ext (expr, addr_size_bits);
2886 ax_simple (expr, aop_swap);
2887 ax_simple (expr, aop_less_signed);
2888 break;
2889
2890 case DW_OP_gt:
2891 /* Sign extend the operands. */
2892 ax_ext (expr, addr_size_bits);
2893 ax_simple (expr, aop_swap);
2894 ax_ext (expr, addr_size_bits);
2895 /* Note no swap here: A > B is B < A. */
2896 ax_simple (expr, aop_less_signed);
2897 break;
2898
2899 case DW_OP_ne:
2900 /* Sign extend the operands. */
2901 ax_ext (expr, addr_size_bits);
2902 ax_simple (expr, aop_swap);
2903 ax_ext (expr, addr_size_bits);
2904 /* No need for a swap here. */
2905 ax_simple (expr, aop_equal);
2906 ax_simple (expr, aop_log_not);
2907 break;
2908
2909 case DW_OP_call_frame_cfa:
9f6f94ff
TT
2910 dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
2911 loc->kind = axs_lvalue_memory;
3cf03773
TT
2912 break;
2913
2914 case DW_OP_GNU_push_tls_address:
2915 unimplemented (op);
2916 break;
2917
2918 case DW_OP_skip:
2919 offset = extract_signed_integer (op_ptr, 2, byte_order);
2920 op_ptr += 2;
2921 i = ax_goto (expr, aop_goto);
2922 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2923 VEC_safe_push (int, patches, i);
2924 break;
2925
2926 case DW_OP_bra:
2927 offset = extract_signed_integer (op_ptr, 2, byte_order);
2928 op_ptr += 2;
2929 /* Zero extend the operand. */
2930 ax_zero_ext (expr, addr_size_bits);
2931 i = ax_goto (expr, aop_if_goto);
2932 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2933 VEC_safe_push (int, patches, i);
2934 break;
2935
2936 case DW_OP_nop:
2937 break;
2938
2939 case DW_OP_piece:
2940 case DW_OP_bit_piece:
08922a10 2941 {
3cf03773
TT
2942 ULONGEST size, offset;
2943
2944 if (op_ptr - 1 == previous_piece)
2945 error (_("Cannot translate empty pieces to agent expressions"));
2946 previous_piece = op_ptr - 1;
2947
2948 op_ptr = read_uleb128 (op_ptr, op_end, &size);
2949 if (op == DW_OP_piece)
2950 {
2951 size *= 8;
2952 offset = 0;
2953 }
2954 else
2955 op_ptr = read_uleb128 (op_ptr, op_end, &offset);
08922a10 2956
3cf03773
TT
2957 if (bits_collected + size > 8 * sizeof (LONGEST))
2958 error (_("Expression pieces exceed word size"));
2959
2960 /* Access the bits. */
2961 switch (loc->kind)
2962 {
2963 case axs_lvalue_register:
2964 ax_reg (expr, loc->u.reg);
2965 break;
2966
2967 case axs_lvalue_memory:
2968 /* Offset the pointer, if needed. */
2969 if (offset > 8)
2970 {
2971 ax_const_l (expr, offset / 8);
2972 ax_simple (expr, aop_add);
2973 offset %= 8;
2974 }
2975 access_memory (arch, expr, size);
2976 break;
2977 }
2978
2979 /* For a bits-big-endian target, shift up what we already
2980 have. For a bits-little-endian target, shift up the
2981 new data. Note that there is a potential bug here if
2982 the DWARF expression leaves multiple values on the
2983 stack. */
2984 if (bits_collected > 0)
2985 {
2986 if (bits_big_endian)
2987 {
2988 ax_simple (expr, aop_swap);
2989 ax_const_l (expr, size);
2990 ax_simple (expr, aop_lsh);
2991 /* We don't need a second swap here, because
2992 aop_bit_or is symmetric. */
2993 }
2994 else
2995 {
2996 ax_const_l (expr, size);
2997 ax_simple (expr, aop_lsh);
2998 }
2999 ax_simple (expr, aop_bit_or);
3000 }
3001
3002 bits_collected += size;
3003 loc->kind = axs_rvalue;
08922a10
SS
3004 }
3005 break;
08922a10 3006
3cf03773
TT
3007 case DW_OP_GNU_uninit:
3008 unimplemented (op);
3009
3010 case DW_OP_call2:
3011 case DW_OP_call4:
3012 {
3013 struct dwarf2_locexpr_baton block;
3014 int size = (op == DW_OP_call2 ? 2 : 4);
3015
3016 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3017 op_ptr += size;
3018
8cf6f0b1
TT
3019 block = dwarf2_fetch_die_location_block (uoffset, per_cu,
3020 get_ax_pc, expr);
3cf03773
TT
3021
3022 /* DW_OP_call_ref is currently not supported. */
3023 gdb_assert (block.per_cu == per_cu);
3024
9f6f94ff
TT
3025 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3026 block.data, block.data + block.size,
3027 per_cu);
3cf03773
TT
3028 }
3029 break;
3030
3031 case DW_OP_call_ref:
3032 unimplemented (op);
3033
3034 default:
b1bfef65 3035 unimplemented (op);
08922a10 3036 }
08922a10 3037 }
3cf03773
TT
3038
3039 /* Patch all the branches we emitted. */
3040 for (i = 0; i < VEC_length (int, patches); ++i)
3041 {
3042 int targ = offsets[VEC_index (int, dw_labels, i)];
3043 if (targ == -1)
3044 internal_error (__FILE__, __LINE__, _("invalid label"));
3045 ax_label (expr, VEC_index (int, patches, i), targ);
3046 }
3047
3048 do_cleanups (cleanups);
08922a10
SS
3049}
3050
4c2df51b
DJ
3051\f
3052/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3053 evaluator to calculate the location. */
3054static struct value *
3055locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3056{
3057 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3058 struct value *val;
9a619af0 3059
a2d33775
JK
3060 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3061 dlbaton->size, dlbaton->per_cu);
4c2df51b
DJ
3062
3063 return val;
3064}
3065
e18b2753
JK
3066/* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3067 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3068 will be thrown. */
3069
3070static struct value *
3071locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3072{
3073 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3074
3075 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3076 dlbaton->size);
3077}
3078
4c2df51b
DJ
3079/* Return non-zero iff we need a frame to evaluate SYMBOL. */
3080static int
3081locexpr_read_needs_frame (struct symbol *symbol)
3082{
3083 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
9a619af0 3084
ae0d2f24
UW
3085 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
3086 dlbaton->per_cu);
4c2df51b
DJ
3087}
3088
9eae7c52
TT
3089/* Return true if DATA points to the end of a piece. END is one past
3090 the last byte in the expression. */
3091
3092static int
3093piece_end_p (const gdb_byte *data, const gdb_byte *end)
3094{
3095 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3096}
3097
5e44ecb3
TT
3098/* Helper for locexpr_describe_location_piece that finds the name of a
3099 DWARF register. */
3100
3101static const char *
3102locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3103{
3104 int regnum;
3105
3106 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
3107 return gdbarch_register_name (gdbarch, regnum);
3108}
3109
9eae7c52
TT
3110/* Nicely describe a single piece of a location, returning an updated
3111 position in the bytecode sequence. This function cannot recognize
3112 all locations; if a location is not recognized, it simply returns
3113 DATA. */
08922a10 3114
0d45f56e 3115static const gdb_byte *
08922a10
SS
3116locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3117 CORE_ADDR addr, struct objfile *objfile,
9eae7c52 3118 const gdb_byte *data, const gdb_byte *end,
0d45f56e 3119 unsigned int addr_size)
4c2df51b 3120{
08922a10 3121 struct gdbarch *gdbarch = get_objfile_arch (objfile);
08922a10
SS
3122
3123 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3124 {
08922a10 3125 fprintf_filtered (stream, _("a variable in $%s"),
5e44ecb3 3126 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
08922a10
SS
3127 data += 1;
3128 }
3129 else if (data[0] == DW_OP_regx)
3130 {
3131 ULONGEST reg;
4c2df51b 3132
9eae7c52 3133 data = read_uleb128 (data + 1, end, &reg);
08922a10 3134 fprintf_filtered (stream, _("a variable in $%s"),
5e44ecb3 3135 locexpr_regname (gdbarch, reg));
08922a10
SS
3136 }
3137 else if (data[0] == DW_OP_fbreg)
4c2df51b 3138 {
08922a10
SS
3139 struct block *b;
3140 struct symbol *framefunc;
3141 int frame_reg = 0;
3142 LONGEST frame_offset;
7155d578 3143 const gdb_byte *base_data, *new_data, *save_data = data;
08922a10
SS
3144 size_t base_size;
3145 LONGEST base_offset = 0;
3146
9eae7c52
TT
3147 new_data = read_sleb128 (data + 1, end, &frame_offset);
3148 if (!piece_end_p (new_data, end))
3149 return data;
3150 data = new_data;
3151
08922a10
SS
3152 b = block_for_pc (addr);
3153
3154 if (!b)
3155 error (_("No block found for address for symbol \"%s\"."),
3156 SYMBOL_PRINT_NAME (symbol));
3157
3158 framefunc = block_linkage_function (b);
3159
3160 if (!framefunc)
3161 error (_("No function found for block for symbol \"%s\"."),
3162 SYMBOL_PRINT_NAME (symbol));
3163
3164 dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
3165
3166 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3167 {
0d45f56e 3168 const gdb_byte *buf_end;
08922a10
SS
3169
3170 frame_reg = base_data[0] - DW_OP_breg0;
3171 buf_end = read_sleb128 (base_data + 1,
3172 base_data + base_size, &base_offset);
3173 if (buf_end != base_data + base_size)
3e43a32a
MS
3174 error (_("Unexpected opcode after "
3175 "DW_OP_breg%u for symbol \"%s\"."),
08922a10
SS
3176 frame_reg, SYMBOL_PRINT_NAME (symbol));
3177 }
3178 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3179 {
3180 /* The frame base is just the register, with no offset. */
3181 frame_reg = base_data[0] - DW_OP_reg0;
3182 base_offset = 0;
3183 }
3184 else
3185 {
3186 /* We don't know what to do with the frame base expression,
3187 so we can't trace this variable; give up. */
7155d578 3188 return save_data;
08922a10
SS
3189 }
3190
3e43a32a
MS
3191 fprintf_filtered (stream,
3192 _("a variable at frame base reg $%s offset %s+%s"),
5e44ecb3 3193 locexpr_regname (gdbarch, frame_reg),
08922a10
SS
3194 plongest (base_offset), plongest (frame_offset));
3195 }
9eae7c52
TT
3196 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3197 && piece_end_p (data, end))
08922a10
SS
3198 {
3199 LONGEST offset;
3200
9eae7c52 3201 data = read_sleb128 (data + 1, end, &offset);
08922a10 3202
4c2df51b 3203 fprintf_filtered (stream,
08922a10
SS
3204 _("a variable at offset %s from base reg $%s"),
3205 plongest (offset),
5e44ecb3 3206 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
4c2df51b
DJ
3207 }
3208
c3228f12
EZ
3209 /* The location expression for a TLS variable looks like this (on a
3210 64-bit LE machine):
3211
3212 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3213 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
09d8bd00 3214
c3228f12
EZ
3215 0x3 is the encoding for DW_OP_addr, which has an operand as long
3216 as the size of an address on the target machine (here is 8
09d8bd00
TT
3217 bytes). Note that more recent version of GCC emit DW_OP_const4u
3218 or DW_OP_const8u, depending on address size, rather than
0963b4bd
MS
3219 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3220 The operand represents the offset at which the variable is within
3221 the thread local storage. */
c3228f12 3222
9eae7c52 3223 else if (data + 1 + addr_size < end
09d8bd00
TT
3224 && (data[0] == DW_OP_addr
3225 || (addr_size == 4 && data[0] == DW_OP_const4u)
3226 || (addr_size == 8 && data[0] == DW_OP_const8u))
9eae7c52
TT
3227 && data[1 + addr_size] == DW_OP_GNU_push_tls_address
3228 && piece_end_p (data + 2 + addr_size, end))
08922a10 3229 {
d4a087c7
UW
3230 ULONGEST offset;
3231 offset = extract_unsigned_integer (data + 1, addr_size,
3232 gdbarch_byte_order (gdbarch));
9a619af0 3233
08922a10 3234 fprintf_filtered (stream,
d4a087c7 3235 _("a thread-local variable at offset 0x%s "
08922a10 3236 "in the thread-local storage for `%s'"),
d4a087c7 3237 phex_nz (offset, addr_size), objfile->name);
08922a10
SS
3238
3239 data += 1 + addr_size + 1;
3240 }
9eae7c52
TT
3241 else if (data[0] >= DW_OP_lit0
3242 && data[0] <= DW_OP_lit31
3243 && data + 1 < end
3244 && data[1] == DW_OP_stack_value)
3245 {
3246 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3247 data += 2;
3248 }
3249
3250 return data;
3251}
3252
3253/* Disassemble an expression, stopping at the end of a piece or at the
3254 end of the expression. Returns a pointer to the next unread byte
3255 in the input expression. If ALL is nonzero, then this function
3256 will keep going until it reaches the end of the expression. */
3257
3258static const gdb_byte *
3259disassemble_dwarf_expression (struct ui_file *stream,
3260 struct gdbarch *arch, unsigned int addr_size,
2bda9cc5 3261 int offset_size, const gdb_byte *start,
9eae7c52 3262 const gdb_byte *data, const gdb_byte *end,
2bda9cc5 3263 int indent, int all,
5e44ecb3 3264 struct dwarf2_per_cu_data *per_cu)
9eae7c52 3265{
9eae7c52
TT
3266 while (data < end
3267 && (all
3268 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3269 {
3270 enum dwarf_location_atom op = *data++;
9eae7c52
TT
3271 ULONGEST ul;
3272 LONGEST l;
3273 const char *name;
3274
b1bfef65 3275 name = dwarf_stack_op_name (op);
9eae7c52
TT
3276
3277 if (!name)
3278 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
06826322 3279 op, (long) (data - 1 - start));
2bda9cc5
JK
3280 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3281 (long) (data - 1 - start), name);
9eae7c52
TT
3282
3283 switch (op)
3284 {
3285 case DW_OP_addr:
d4a087c7
UW
3286 ul = extract_unsigned_integer (data, addr_size,
3287 gdbarch_byte_order (arch));
9eae7c52 3288 data += addr_size;
d4a087c7 3289 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
9eae7c52
TT
3290 break;
3291
3292 case DW_OP_const1u:
3293 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3294 data += 1;
3295 fprintf_filtered (stream, " %s", pulongest (ul));
3296 break;
3297 case DW_OP_const1s:
3298 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3299 data += 1;
3300 fprintf_filtered (stream, " %s", plongest (l));
3301 break;
3302 case DW_OP_const2u:
3303 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3304 data += 2;
3305 fprintf_filtered (stream, " %s", pulongest (ul));
3306 break;
3307 case DW_OP_const2s:
3308 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3309 data += 2;
3310 fprintf_filtered (stream, " %s", plongest (l));
3311 break;
3312 case DW_OP_const4u:
3313 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3314 data += 4;
3315 fprintf_filtered (stream, " %s", pulongest (ul));
3316 break;
3317 case DW_OP_const4s:
3318 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3319 data += 4;
3320 fprintf_filtered (stream, " %s", plongest (l));
3321 break;
3322 case DW_OP_const8u:
3323 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3324 data += 8;
3325 fprintf_filtered (stream, " %s", pulongest (ul));
3326 break;
3327 case DW_OP_const8s:
3328 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3329 data += 8;
3330 fprintf_filtered (stream, " %s", plongest (l));
3331 break;
3332 case DW_OP_constu:
3333 data = read_uleb128 (data, end, &ul);
3334 fprintf_filtered (stream, " %s", pulongest (ul));
3335 break;
3336 case DW_OP_consts:
44b5680a 3337 data = read_sleb128 (data, end, &l);
9eae7c52
TT
3338 fprintf_filtered (stream, " %s", plongest (l));
3339 break;
3340
3341 case DW_OP_reg0:
3342 case DW_OP_reg1:
3343 case DW_OP_reg2:
3344 case DW_OP_reg3:
3345 case DW_OP_reg4:
3346 case DW_OP_reg5:
3347 case DW_OP_reg6:
3348 case DW_OP_reg7:
3349 case DW_OP_reg8:
3350 case DW_OP_reg9:
3351 case DW_OP_reg10:
3352 case DW_OP_reg11:
3353 case DW_OP_reg12:
3354 case DW_OP_reg13:
3355 case DW_OP_reg14:
3356 case DW_OP_reg15:
3357 case DW_OP_reg16:
3358 case DW_OP_reg17:
3359 case DW_OP_reg18:
3360 case DW_OP_reg19:
3361 case DW_OP_reg20:
3362 case DW_OP_reg21:
3363 case DW_OP_reg22:
3364 case DW_OP_reg23:
3365 case DW_OP_reg24:
3366 case DW_OP_reg25:
3367 case DW_OP_reg26:
3368 case DW_OP_reg27:
3369 case DW_OP_reg28:
3370 case DW_OP_reg29:
3371 case DW_OP_reg30:
3372 case DW_OP_reg31:
3373 fprintf_filtered (stream, " [$%s]",
5e44ecb3 3374 locexpr_regname (arch, op - DW_OP_reg0));
9eae7c52
TT
3375 break;
3376
3377 case DW_OP_regx:
3378 data = read_uleb128 (data, end, &ul);
3379 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
5e44ecb3 3380 locexpr_regname (arch, (int) ul));
9eae7c52
TT
3381 break;
3382
3383 case DW_OP_implicit_value:
3384 data = read_uleb128 (data, end, &ul);
3385 data += ul;
3386 fprintf_filtered (stream, " %s", pulongest (ul));
3387 break;
3388
3389 case DW_OP_breg0:
3390 case DW_OP_breg1:
3391 case DW_OP_breg2:
3392 case DW_OP_breg3:
3393 case DW_OP_breg4:
3394 case DW_OP_breg5:
3395 case DW_OP_breg6:
3396 case DW_OP_breg7:
3397 case DW_OP_breg8:
3398 case DW_OP_breg9:
3399 case DW_OP_breg10:
3400 case DW_OP_breg11:
3401 case DW_OP_breg12:
3402 case DW_OP_breg13:
3403 case DW_OP_breg14:
3404 case DW_OP_breg15:
3405 case DW_OP_breg16:
3406 case DW_OP_breg17:
3407 case DW_OP_breg18:
3408 case DW_OP_breg19:
3409 case DW_OP_breg20:
3410 case DW_OP_breg21:
3411 case DW_OP_breg22:
3412 case DW_OP_breg23:
3413 case DW_OP_breg24:
3414 case DW_OP_breg25:
3415 case DW_OP_breg26:
3416 case DW_OP_breg27:
3417 case DW_OP_breg28:
3418 case DW_OP_breg29:
3419 case DW_OP_breg30:
3420 case DW_OP_breg31:
0502ed8c
JK
3421 data = read_sleb128 (data, end, &l);
3422 fprintf_filtered (stream, " %s [$%s]", plongest (l),
5e44ecb3 3423 locexpr_regname (arch, op - DW_OP_breg0));
9eae7c52
TT
3424 break;
3425
3426 case DW_OP_bregx:
0502ed8c
JK
3427 data = read_uleb128 (data, end, &ul);
3428 data = read_sleb128 (data, end, &l);
3429 fprintf_filtered (stream, " register %s [$%s] offset %s",
3430 pulongest (ul),
5e44ecb3 3431 locexpr_regname (arch, (int) ul),
0502ed8c 3432 plongest (l));
9eae7c52
TT
3433 break;
3434
3435 case DW_OP_fbreg:
0502ed8c
JK
3436 data = read_sleb128 (data, end, &l);
3437 fprintf_filtered (stream, " %s", plongest (l));
9eae7c52
TT
3438 break;
3439
3440 case DW_OP_xderef_size:
3441 case DW_OP_deref_size:
3442 case DW_OP_pick:
3443 fprintf_filtered (stream, " %d", *data);
3444 ++data;
3445 break;
3446
3447 case DW_OP_plus_uconst:
3448 data = read_uleb128 (data, end, &ul);
3449 fprintf_filtered (stream, " %s", pulongest (ul));
3450 break;
3451
3452 case DW_OP_skip:
3453 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3454 data += 2;
3455 fprintf_filtered (stream, " to %ld",
3456 (long) (data + l - start));
3457 break;
3458
3459 case DW_OP_bra:
3460 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3461 data += 2;
3462 fprintf_filtered (stream, " %ld",
3463 (long) (data + l - start));
3464 break;
3465
3466 case DW_OP_call2:
3467 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3468 data += 2;
3469 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
3470 break;
3471
3472 case DW_OP_call4:
3473 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3474 data += 4;
3475 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
3476 break;
3477
3478 case DW_OP_call_ref:
3479 ul = extract_unsigned_integer (data, offset_size,
3480 gdbarch_byte_order (arch));
3481 data += offset_size;
3482 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
3483 break;
3484
3485 case DW_OP_piece:
3486 data = read_uleb128 (data, end, &ul);
3487 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
3488 break;
3489
3490 case DW_OP_bit_piece:
3491 {
3492 ULONGEST offset;
3493
3494 data = read_uleb128 (data, end, &ul);
3495 data = read_uleb128 (data, end, &offset);
3496 fprintf_filtered (stream, " size %s offset %s (bits)",
3497 pulongest (ul), pulongest (offset));
3498 }
3499 break;
8cf6f0b1
TT
3500
3501 case DW_OP_GNU_implicit_pointer:
3502 {
3503 ul = extract_unsigned_integer (data, offset_size,
3504 gdbarch_byte_order (arch));
3505 data += offset_size;
3506
3507 data = read_sleb128 (data, end, &l);
3508
3509 fprintf_filtered (stream, " DIE %s offset %s",
3510 phex_nz (ul, offset_size),
3511 plongest (l));
3512 }
3513 break;
5e44ecb3
TT
3514
3515 case DW_OP_GNU_deref_type:
3516 {
3517 int addr_size = *data++;
3518 ULONGEST offset;
3519 struct type *type;
3520
3521 data = read_uleb128 (data, end, &offset);
3522 type = dwarf2_get_die_type (offset, per_cu);
3523 fprintf_filtered (stream, "<");
3524 type_print (type, "", stream, -1);
3525 fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset, 0),
3526 addr_size);
3527 }
3528 break;
3529
3530 case DW_OP_GNU_const_type:
3531 {
3532 ULONGEST type_die;
3533 struct type *type;
3534
3535 data = read_uleb128 (data, end, &type_die);
3536 type = dwarf2_get_die_type (type_die, per_cu);
3537 fprintf_filtered (stream, "<");
3538 type_print (type, "", stream, -1);
3539 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die, 0));
3540 }
3541 break;
3542
3543 case DW_OP_GNU_regval_type:
3544 {
3545 ULONGEST type_die, reg;
3546 struct type *type;
3547
3548 data = read_uleb128 (data, end, &reg);
3549 data = read_uleb128 (data, end, &type_die);
3550
3551 type = dwarf2_get_die_type (type_die, per_cu);
3552 fprintf_filtered (stream, "<");
3553 type_print (type, "", stream, -1);
3554 fprintf_filtered (stream, " [0x%s]> [$%s]", phex_nz (type_die, 0),
3555 locexpr_regname (arch, reg));
3556 }
3557 break;
3558
3559 case DW_OP_GNU_convert:
3560 case DW_OP_GNU_reinterpret:
3561 {
3562 ULONGEST type_die;
3563
3564 data = read_uleb128 (data, end, &type_die);
3565
3566 if (type_die == 0)
3567 fprintf_filtered (stream, "<0>");
3568 else
3569 {
3570 struct type *type;
3571
3572 type = dwarf2_get_die_type (type_die, per_cu);
3573 fprintf_filtered (stream, "<");
3574 type_print (type, "", stream, -1);
3575 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die, 0));
3576 }
3577 }
3578 break;
2bda9cc5
JK
3579
3580 case DW_OP_GNU_entry_value:
3581 data = read_uleb128 (data, end, &ul);
3582 fputc_filtered ('\n', stream);
3583 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
3584 start, data, data + ul, indent + 2,
3585 all, per_cu);
3586 data += ul;
3587 continue;
9eae7c52
TT
3588 }
3589
3590 fprintf_filtered (stream, "\n");
3591 }
c3228f12 3592
08922a10 3593 return data;
4c2df51b
DJ
3594}
3595
08922a10
SS
3596/* Describe a single location, which may in turn consist of multiple
3597 pieces. */
a55cc764 3598
08922a10
SS
3599static void
3600locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
0d45f56e
TT
3601 struct ui_file *stream,
3602 const gdb_byte *data, int size,
9eae7c52 3603 struct objfile *objfile, unsigned int addr_size,
5e44ecb3 3604 int offset_size, struct dwarf2_per_cu_data *per_cu)
08922a10 3605{
0d45f56e 3606 const gdb_byte *end = data + size;
9eae7c52 3607 int first_piece = 1, bad = 0;
08922a10 3608
08922a10
SS
3609 while (data < end)
3610 {
9eae7c52
TT
3611 const gdb_byte *here = data;
3612 int disassemble = 1;
3613
3614 if (first_piece)
3615 first_piece = 0;
3616 else
3617 fprintf_filtered (stream, _(", and "));
08922a10 3618
9eae7c52
TT
3619 if (!dwarf2_always_disassemble)
3620 {
3e43a32a
MS
3621 data = locexpr_describe_location_piece (symbol, stream,
3622 addr, objfile,
9eae7c52
TT
3623 data, end, addr_size);
3624 /* If we printed anything, or if we have an empty piece,
3625 then don't disassemble. */
3626 if (data != here
3627 || data[0] == DW_OP_piece
3628 || data[0] == DW_OP_bit_piece)
3629 disassemble = 0;
08922a10 3630 }
9eae7c52 3631 if (disassemble)
2bda9cc5
JK
3632 {
3633 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
3634 data = disassemble_dwarf_expression (stream,
3635 get_objfile_arch (objfile),
3636 addr_size, offset_size, data,
3637 data, end, 0,
3638 dwarf2_always_disassemble,
3639 per_cu);
3640 }
9eae7c52
TT
3641
3642 if (data < end)
08922a10 3643 {
9eae7c52 3644 int empty = data == here;
08922a10 3645
9eae7c52
TT
3646 if (disassemble)
3647 fprintf_filtered (stream, " ");
3648 if (data[0] == DW_OP_piece)
3649 {
3650 ULONGEST bytes;
08922a10 3651
9eae7c52 3652 data = read_uleb128 (data + 1, end, &bytes);
08922a10 3653
9eae7c52
TT
3654 if (empty)
3655 fprintf_filtered (stream, _("an empty %s-byte piece"),
3656 pulongest (bytes));
3657 else
3658 fprintf_filtered (stream, _(" [%s-byte piece]"),
3659 pulongest (bytes));
3660 }
3661 else if (data[0] == DW_OP_bit_piece)
3662 {
3663 ULONGEST bits, offset;
3664
3665 data = read_uleb128 (data + 1, end, &bits);
3666 data = read_uleb128 (data, end, &offset);
3667
3668 if (empty)
3669 fprintf_filtered (stream,
3670 _("an empty %s-bit piece"),
3671 pulongest (bits));
3672 else
3673 fprintf_filtered (stream,
3674 _(" [%s-bit piece, offset %s bits]"),
3675 pulongest (bits), pulongest (offset));
3676 }
3677 else
3678 {
3679 bad = 1;
3680 break;
3681 }
08922a10
SS
3682 }
3683 }
3684
3685 if (bad || data > end)
3686 error (_("Corrupted DWARF2 expression for \"%s\"."),
3687 SYMBOL_PRINT_NAME (symbol));
3688}
3689
3690/* Print a natural-language description of SYMBOL to STREAM. This
3691 version is for a symbol with a single location. */
a55cc764 3692
08922a10
SS
3693static void
3694locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
3695 struct ui_file *stream)
3696{
3697 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3698 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3699 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
9eae7c52 3700 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
08922a10 3701
3e43a32a
MS
3702 locexpr_describe_location_1 (symbol, addr, stream,
3703 dlbaton->data, dlbaton->size,
5e44ecb3
TT
3704 objfile, addr_size, offset_size,
3705 dlbaton->per_cu);
08922a10
SS
3706}
3707
3708/* Describe the location of SYMBOL as an agent value in VALUE, generating
3709 any necessary bytecode in AX. */
a55cc764 3710
0d53c4c4 3711static void
505e835d
UW
3712locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
3713 struct agent_expr *ax, struct axs_value *value)
a55cc764
DJ
3714{
3715 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3cf03773 3716 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
a55cc764 3717
cabe9ab6
PA
3718 if (dlbaton->data == NULL || dlbaton->size == 0)
3719 value->optimized_out = 1;
3720 else
9f6f94ff
TT
3721 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
3722 dlbaton->data, dlbaton->data + dlbaton->size,
3723 dlbaton->per_cu);
a55cc764
DJ
3724}
3725
4c2df51b
DJ
3726/* The set of location functions used with the DWARF-2 expression
3727 evaluator. */
768a979c 3728const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4c2df51b 3729 locexpr_read_variable,
e18b2753 3730 locexpr_read_variable_at_entry,
4c2df51b
DJ
3731 locexpr_read_needs_frame,
3732 locexpr_describe_location,
a55cc764 3733 locexpr_tracepoint_var_ref
4c2df51b 3734};
0d53c4c4
DJ
3735
3736
3737/* Wrapper functions for location lists. These generally find
3738 the appropriate location expression and call something above. */
3739
3740/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3741 evaluator to calculate the location. */
3742static struct value *
3743loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
3744{
3745 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3746 struct value *val;
947bb88f 3747 const gdb_byte *data;
b6b08ebf 3748 size_t size;
8cf6f0b1 3749 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
0d53c4c4 3750
8cf6f0b1 3751 data = dwarf2_find_location_expression (dlbaton, &size, pc);
0d53c4c4 3752 if (data == NULL)
a7035dbb 3753 val = allocate_optimized_out_value (SYMBOL_TYPE (symbol));
806048c6 3754 else
a2d33775 3755 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
ae0d2f24 3756 dlbaton->per_cu);
0d53c4c4
DJ
3757
3758 return val;
3759}
3760
e18b2753
JK
3761/* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
3762 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3763 will be thrown.
3764
3765 Function always returns non-NULL value, it may be marked optimized out if
3766 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
3767 if it cannot resolve the parameter for any reason. */
3768
3769static struct value *
3770loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3771{
3772 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3773 const gdb_byte *data;
3774 size_t size;
3775 CORE_ADDR pc;
3776
3777 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
3778 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
3779
3780 data = dwarf2_find_location_expression (dlbaton, &size, pc);
3781 if (data == NULL)
3782 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
3783
3784 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
3785}
3786
0d53c4c4
DJ
3787/* Return non-zero iff we need a frame to evaluate SYMBOL. */
3788static int
3789loclist_read_needs_frame (struct symbol *symbol)
3790{
3791 /* If there's a location list, then assume we need to have a frame
3792 to choose the appropriate location expression. With tracking of
3793 global variables this is not necessarily true, but such tracking
3794 is disabled in GCC at the moment until we figure out how to
3795 represent it. */
3796
3797 return 1;
3798}
3799
08922a10
SS
3800/* Print a natural-language description of SYMBOL to STREAM. This
3801 version applies when there is a list of different locations, each
3802 with a specified address range. */
3803
3804static void
3805loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
3806 struct ui_file *stream)
0d53c4c4 3807{
08922a10
SS
3808 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
3809 CORE_ADDR low, high;
947bb88f 3810 const gdb_byte *loc_ptr, *buf_end;
08922a10
SS
3811 int length, first = 1;
3812 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
3813 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3814 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3815 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
9eae7c52 3816 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
d4a087c7 3817 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
08922a10
SS
3818 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3819 /* Adjust base_address for relocatable objects. */
9aa1f1e3 3820 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
08922a10
SS
3821 CORE_ADDR base_address = dlbaton->base_address + base_offset;
3822
3823 loc_ptr = dlbaton->data;
3824 buf_end = dlbaton->data + dlbaton->size;
3825
9eae7c52 3826 fprintf_filtered (stream, _("multi-location:\n"));
08922a10
SS
3827
3828 /* Iterate through locations until we run out. */
3829 while (1)
3830 {
3831 if (buf_end - loc_ptr < 2 * addr_size)
3832 error (_("Corrupted DWARF expression for symbol \"%s\"."),
3833 SYMBOL_PRINT_NAME (symbol));
3834
d4a087c7
UW
3835 if (signed_addr_p)
3836 low = extract_signed_integer (loc_ptr, addr_size, byte_order);
3837 else
3838 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
3839 loc_ptr += addr_size;
3840
3841 if (signed_addr_p)
3842 high = extract_signed_integer (loc_ptr, addr_size, byte_order);
3843 else
3844 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
08922a10
SS
3845 loc_ptr += addr_size;
3846
3847 /* A base-address-selection entry. */
d4a087c7 3848 if ((low & base_mask) == base_mask)
08922a10 3849 {
d4a087c7 3850 base_address = high + base_offset;
9eae7c52 3851 fprintf_filtered (stream, _(" Base address %s"),
08922a10 3852 paddress (gdbarch, base_address));
08922a10
SS
3853 continue;
3854 }
3855
08922a10
SS
3856 /* An end-of-list entry. */
3857 if (low == 0 && high == 0)
9eae7c52 3858 break;
08922a10
SS
3859
3860 /* Otherwise, a location expression entry. */
3861 low += base_address;
3862 high += base_address;
3863
3864 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
3865 loc_ptr += 2;
3866
08922a10
SS
3867 /* (It would improve readability to print only the minimum
3868 necessary digits of the second number of the range.) */
9eae7c52 3869 fprintf_filtered (stream, _(" Range %s-%s: "),
08922a10
SS
3870 paddress (gdbarch, low), paddress (gdbarch, high));
3871
3872 /* Now describe this particular location. */
3873 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
5e44ecb3
TT
3874 objfile, addr_size, offset_size,
3875 dlbaton->per_cu);
9eae7c52
TT
3876
3877 fprintf_filtered (stream, "\n");
08922a10
SS
3878
3879 loc_ptr += length;
3880 }
0d53c4c4
DJ
3881}
3882
3883/* Describe the location of SYMBOL as an agent value in VALUE, generating
3884 any necessary bytecode in AX. */
3885static void
505e835d
UW
3886loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
3887 struct agent_expr *ax, struct axs_value *value)
0d53c4c4
DJ
3888{
3889 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
947bb88f 3890 const gdb_byte *data;
b6b08ebf 3891 size_t size;
3cf03773 3892 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
0d53c4c4 3893
8cf6f0b1 3894 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
cabe9ab6
PA
3895 if (data == NULL || size == 0)
3896 value->optimized_out = 1;
3897 else
9f6f94ff
TT
3898 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
3899 dlbaton->per_cu);
0d53c4c4
DJ
3900}
3901
3902/* The set of location functions used with the DWARF-2 expression
3903 evaluator and location lists. */
768a979c 3904const struct symbol_computed_ops dwarf2_loclist_funcs = {
0d53c4c4 3905 loclist_read_variable,
e18b2753 3906 loclist_read_variable_at_entry,
0d53c4c4
DJ
3907 loclist_read_needs_frame,
3908 loclist_describe_location,
3909 loclist_tracepoint_var_ref
3910};
8e3b41a9
JK
3911
3912void
3913_initialize_dwarf2loc (void)
3914{
3915 add_setshow_zinteger_cmd ("entry-values", class_maintenance,
3916 &entry_values_debug,
3917 _("Set entry values and tail call frames "
3918 "debugging."),
3919 _("Show entry values and tail call frames "
3920 "debugging."),
3921 _("When non-zero, the process of determining "
3922 "parameter values from function entry point "
3923 "and tail call frames will be printed."),
3924 NULL,
3925 show_entry_values_debug,
3926 &setdebuglist, &showdebuglist);
3927}
This page took 0.770809 seconds and 4 git commands to generate.