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