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