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