S/390: Move start of 64 bit binaries from 2GB to 256MB.
[binutils-gdb.git] / gdb / dwarf2loc.c
blobf9f3216418f8eb4b51b40391d528dfc398398af0
1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003-2016 Free Software Foundation, Inc.
5 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7 This file is part of GDB.
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.
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.
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/>. */
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 "block.h"
34 #include "gdbcmd.h"
35 #include "complaints.h"
36 #include "dwarf2.h"
37 #include "dwarf2expr.h"
38 #include "dwarf2loc.h"
39 #include "dwarf2-frame.h"
40 #include "compile/compile.h"
41 #include <algorithm>
43 extern int dwarf_always_disassemble;
45 extern const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs;
47 static struct value *dwarf2_evaluate_loc_desc_full (struct type *type,
48 struct frame_info *frame,
49 const gdb_byte *data,
50 size_t size,
51 struct dwarf2_per_cu_data *per_cu,
52 LONGEST byte_offset);
54 /* Until these have formal names, we define these here.
55 ref: http://gcc.gnu.org/wiki/DebugFission
56 Each entry in .debug_loc.dwo begins with a byte that describes the entry,
57 and is then followed by data specific to that entry. */
59 enum debug_loc_kind
61 /* Indicates the end of the list of entries. */
62 DEBUG_LOC_END_OF_LIST = 0,
64 /* This is followed by an unsigned LEB128 number that is an index into
65 .debug_addr and specifies the base address for all following entries. */
66 DEBUG_LOC_BASE_ADDRESS = 1,
68 /* This is followed by two unsigned LEB128 numbers that are indices into
69 .debug_addr and specify the beginning and ending addresses, and then
70 a normal location expression as in .debug_loc. */
71 DEBUG_LOC_START_END = 2,
73 /* This is followed by an unsigned LEB128 number that is an index into
74 .debug_addr and specifies the beginning address, and a 4 byte unsigned
75 number that specifies the length, and then a normal location expression
76 as in .debug_loc. */
77 DEBUG_LOC_START_LENGTH = 3,
79 /* An internal value indicating there is insufficient data. */
80 DEBUG_LOC_BUFFER_OVERFLOW = -1,
82 /* An internal value indicating an invalid kind of entry was found. */
83 DEBUG_LOC_INVALID_ENTRY = -2
86 /* Helper function which throws an error if a synthetic pointer is
87 invalid. */
89 static void
90 invalid_synthetic_pointer (void)
92 error (_("access outside bounds of object "
93 "referenced via synthetic pointer"));
96 /* Decode the addresses in a non-dwo .debug_loc entry.
97 A pointer to the next byte to examine is returned in *NEW_PTR.
98 The encoded low,high addresses are return in *LOW,*HIGH.
99 The result indicates the kind of entry found. */
101 static enum debug_loc_kind
102 decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end,
103 const gdb_byte **new_ptr,
104 CORE_ADDR *low, CORE_ADDR *high,
105 enum bfd_endian byte_order,
106 unsigned int addr_size,
107 int signed_addr_p)
109 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
111 if (buf_end - loc_ptr < 2 * addr_size)
112 return DEBUG_LOC_BUFFER_OVERFLOW;
114 if (signed_addr_p)
115 *low = extract_signed_integer (loc_ptr, addr_size, byte_order);
116 else
117 *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
118 loc_ptr += addr_size;
120 if (signed_addr_p)
121 *high = extract_signed_integer (loc_ptr, addr_size, byte_order);
122 else
123 *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
124 loc_ptr += addr_size;
126 *new_ptr = loc_ptr;
128 /* A base-address-selection entry. */
129 if ((*low & base_mask) == base_mask)
130 return DEBUG_LOC_BASE_ADDRESS;
132 /* An end-of-list entry. */
133 if (*low == 0 && *high == 0)
134 return DEBUG_LOC_END_OF_LIST;
136 return DEBUG_LOC_START_END;
139 /* Decode the addresses in .debug_loc.dwo entry.
140 A pointer to the next byte to examine is returned in *NEW_PTR.
141 The encoded low,high addresses are return in *LOW,*HIGH.
142 The result indicates the kind of entry found. */
144 static enum debug_loc_kind
145 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu,
146 const gdb_byte *loc_ptr,
147 const gdb_byte *buf_end,
148 const gdb_byte **new_ptr,
149 CORE_ADDR *low, CORE_ADDR *high,
150 enum bfd_endian byte_order)
152 uint64_t low_index, high_index;
154 if (loc_ptr == buf_end)
155 return DEBUG_LOC_BUFFER_OVERFLOW;
157 switch (*loc_ptr++)
159 case DEBUG_LOC_END_OF_LIST:
160 *new_ptr = loc_ptr;
161 return DEBUG_LOC_END_OF_LIST;
162 case DEBUG_LOC_BASE_ADDRESS:
163 *low = 0;
164 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
165 if (loc_ptr == NULL)
166 return DEBUG_LOC_BUFFER_OVERFLOW;
167 *high = dwarf2_read_addr_index (per_cu, high_index);
168 *new_ptr = loc_ptr;
169 return DEBUG_LOC_BASE_ADDRESS;
170 case DEBUG_LOC_START_END:
171 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
172 if (loc_ptr == NULL)
173 return DEBUG_LOC_BUFFER_OVERFLOW;
174 *low = dwarf2_read_addr_index (per_cu, low_index);
175 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index);
176 if (loc_ptr == NULL)
177 return DEBUG_LOC_BUFFER_OVERFLOW;
178 *high = dwarf2_read_addr_index (per_cu, high_index);
179 *new_ptr = loc_ptr;
180 return DEBUG_LOC_START_END;
181 case DEBUG_LOC_START_LENGTH:
182 loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index);
183 if (loc_ptr == NULL)
184 return DEBUG_LOC_BUFFER_OVERFLOW;
185 *low = dwarf2_read_addr_index (per_cu, low_index);
186 if (loc_ptr + 4 > buf_end)
187 return DEBUG_LOC_BUFFER_OVERFLOW;
188 *high = *low;
189 *high += extract_unsigned_integer (loc_ptr, 4, byte_order);
190 *new_ptr = loc_ptr + 4;
191 return DEBUG_LOC_START_LENGTH;
192 default:
193 return DEBUG_LOC_INVALID_ENTRY;
197 /* A function for dealing with location lists. Given a
198 symbol baton (BATON) and a pc value (PC), find the appropriate
199 location expression, set *LOCEXPR_LENGTH, and return a pointer
200 to the beginning of the expression. Returns NULL on failure.
202 For now, only return the first matching location expression; there
203 can be more than one in the list. */
205 const gdb_byte *
206 dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
207 size_t *locexpr_length, CORE_ADDR pc)
209 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
210 struct gdbarch *gdbarch = get_objfile_arch (objfile);
211 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
212 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
213 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
214 /* Adjust base_address for relocatable objects. */
215 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu);
216 CORE_ADDR base_address = baton->base_address + base_offset;
217 const gdb_byte *loc_ptr, *buf_end;
219 loc_ptr = baton->data;
220 buf_end = baton->data + baton->size;
222 while (1)
224 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
225 int length;
226 enum debug_loc_kind kind;
227 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
229 if (baton->from_dwo)
230 kind = decode_debug_loc_dwo_addresses (baton->per_cu,
231 loc_ptr, buf_end, &new_ptr,
232 &low, &high, byte_order);
233 else
234 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
235 &low, &high,
236 byte_order, addr_size,
237 signed_addr_p);
238 loc_ptr = new_ptr;
239 switch (kind)
241 case DEBUG_LOC_END_OF_LIST:
242 *locexpr_length = 0;
243 return NULL;
244 case DEBUG_LOC_BASE_ADDRESS:
245 base_address = high + base_offset;
246 continue;
247 case DEBUG_LOC_START_END:
248 case DEBUG_LOC_START_LENGTH:
249 break;
250 case DEBUG_LOC_BUFFER_OVERFLOW:
251 case DEBUG_LOC_INVALID_ENTRY:
252 error (_("dwarf2_find_location_expression: "
253 "Corrupted DWARF expression."));
254 default:
255 gdb_assert_not_reached ("bad debug_loc_kind");
258 /* Otherwise, a location expression entry.
259 If the entry is from a DWO, don't add base address: the entry is from
260 .debug_addr which already has the DWARF "base address". We still add
261 base_offset in case we're debugging a PIE executable. */
262 if (baton->from_dwo)
264 low += base_offset;
265 high += base_offset;
267 else
269 low += base_address;
270 high += base_address;
273 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
274 loc_ptr += 2;
276 if (low == high && pc == low)
278 /* This is entry PC record present only at entry point
279 of a function. Verify it is really the function entry point. */
281 const struct block *pc_block = block_for_pc (pc);
282 struct symbol *pc_func = NULL;
284 if (pc_block)
285 pc_func = block_linkage_function (pc_block);
287 if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func)))
289 *locexpr_length = length;
290 return loc_ptr;
294 if (pc >= low && pc < high)
296 *locexpr_length = length;
297 return loc_ptr;
300 loc_ptr += length;
304 /* This is the baton used when performing dwarf2 expression
305 evaluation. */
306 struct dwarf_expr_baton
308 struct frame_info *frame;
309 struct dwarf2_per_cu_data *per_cu;
310 CORE_ADDR obj_address;
313 /* Helper functions for dwarf2_evaluate_loc_desc. */
315 /* Using the frame specified in BATON, return the value of register
316 REGNUM, treated as a pointer. */
317 static CORE_ADDR
318 dwarf_expr_read_addr_from_reg (void *baton, int dwarf_regnum)
320 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
321 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
322 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
324 return address_from_register (regnum, debaton->frame);
327 /* Implement struct dwarf_expr_context_funcs' "get_reg_value" callback. */
329 static struct value *
330 dwarf_expr_get_reg_value (void *baton, struct type *type, int dwarf_regnum)
332 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
333 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
334 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, dwarf_regnum);
336 return value_from_register (type, regnum, debaton->frame);
339 /* Read memory at ADDR (length LEN) into BUF. */
341 static void
342 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
344 read_memory (addr, buf, len);
347 /* Using the frame specified in BATON, find the location expression
348 describing the frame base. Return a pointer to it in START and
349 its length in LENGTH. */
350 static void
351 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
353 /* FIXME: cagney/2003-03-26: This code should be using
354 get_frame_base_address(), and then implement a dwarf2 specific
355 this_base method. */
356 struct symbol *framefunc;
357 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
358 const struct block *bl = get_frame_block (debaton->frame, NULL);
360 if (bl == NULL)
361 error (_("frame address is not available."));
363 /* Use block_linkage_function, which returns a real (not inlined)
364 function, instead of get_frame_function, which may return an
365 inlined function. */
366 framefunc = block_linkage_function (bl);
368 /* If we found a frame-relative symbol then it was certainly within
369 some function associated with a frame. If we can't find the frame,
370 something has gone wrong. */
371 gdb_assert (framefunc != NULL);
373 func_get_frame_base_dwarf_block (framefunc,
374 get_frame_address_in_block (debaton->frame),
375 start, length);
378 /* Implement find_frame_base_location method for LOC_BLOCK functions using
379 DWARF expression for its DW_AT_frame_base. */
381 static void
382 locexpr_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
383 const gdb_byte **start, size_t *length)
385 struct dwarf2_locexpr_baton *symbaton
386 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
388 *length = symbaton->size;
389 *start = symbaton->data;
392 /* Implement the struct symbol_block_ops::get_frame_base method for
393 LOC_BLOCK functions using a DWARF expression as its DW_AT_frame_base. */
395 static CORE_ADDR
396 locexpr_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
398 struct gdbarch *gdbarch;
399 struct type *type;
400 struct dwarf2_locexpr_baton *dlbaton;
401 const gdb_byte *start;
402 size_t length;
403 struct value *result;
405 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
406 Thus, it's supposed to provide the find_frame_base_location method as
407 well. */
408 gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
410 gdbarch = get_frame_arch (frame);
411 type = builtin_type (gdbarch)->builtin_data_ptr;
412 dlbaton = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (framefunc);
414 SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
415 (framefunc, get_frame_pc (frame), &start, &length);
416 result = dwarf2_evaluate_loc_desc (type, frame, start, length,
417 dlbaton->per_cu);
419 /* The DW_AT_frame_base attribute contains a location description which
420 computes the base address itself. However, the call to
421 dwarf2_evaluate_loc_desc returns a value representing a variable at
422 that address. The frame base address is thus this variable's
423 address. */
424 return value_address (result);
427 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
428 function uses DWARF expression for its DW_AT_frame_base. */
430 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs =
432 locexpr_find_frame_base_location,
433 locexpr_get_frame_base
436 /* Implement find_frame_base_location method for LOC_BLOCK functions using
437 DWARF location list for its DW_AT_frame_base. */
439 static void
440 loclist_find_frame_base_location (struct symbol *framefunc, CORE_ADDR pc,
441 const gdb_byte **start, size_t *length)
443 struct dwarf2_loclist_baton *symbaton
444 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
446 *start = dwarf2_find_location_expression (symbaton, length, pc);
449 /* Implement the struct symbol_block_ops::get_frame_base method for
450 LOC_BLOCK functions using a DWARF location list as its DW_AT_frame_base. */
452 static CORE_ADDR
453 loclist_get_frame_base (struct symbol *framefunc, struct frame_info *frame)
455 struct gdbarch *gdbarch;
456 struct type *type;
457 struct dwarf2_loclist_baton *dlbaton;
458 const gdb_byte *start;
459 size_t length;
460 struct value *result;
462 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
463 Thus, it's supposed to provide the find_frame_base_location method as
464 well. */
465 gdb_assert (SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location != NULL);
467 gdbarch = get_frame_arch (frame);
468 type = builtin_type (gdbarch)->builtin_data_ptr;
469 dlbaton = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (framefunc);
471 SYMBOL_BLOCK_OPS (framefunc)->find_frame_base_location
472 (framefunc, get_frame_pc (frame), &start, &length);
473 result = dwarf2_evaluate_loc_desc (type, frame, start, length,
474 dlbaton->per_cu);
476 /* The DW_AT_frame_base attribute contains a location description which
477 computes the base address itself. However, the call to
478 dwarf2_evaluate_loc_desc returns a value representing a variable at
479 that address. The frame base address is thus this variable's
480 address. */
481 return value_address (result);
484 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
485 function uses DWARF location list for its DW_AT_frame_base. */
487 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs =
489 loclist_find_frame_base_location,
490 loclist_get_frame_base
493 /* See dwarf2loc.h. */
495 void
496 func_get_frame_base_dwarf_block (struct symbol *framefunc, CORE_ADDR pc,
497 const gdb_byte **start, size_t *length)
499 if (SYMBOL_BLOCK_OPS (framefunc) != NULL)
501 const struct symbol_block_ops *ops_block = SYMBOL_BLOCK_OPS (framefunc);
503 ops_block->find_frame_base_location (framefunc, pc, start, length);
505 else
506 *length = 0;
508 if (*length == 0)
509 error (_("Could not find the frame base for \"%s\"."),
510 SYMBOL_NATURAL_NAME (framefunc));
513 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
514 the frame in BATON. */
516 static CORE_ADDR
517 dwarf_expr_frame_cfa (void *baton)
519 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
521 return dwarf2_frame_cfa (debaton->frame);
524 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
525 the frame in BATON. */
527 static CORE_ADDR
528 dwarf_expr_frame_pc (void *baton)
530 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
532 return get_frame_address_in_block (debaton->frame);
535 /* Using the objfile specified in BATON, find the address for the
536 current thread's thread-local storage with offset OFFSET. */
537 static CORE_ADDR
538 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
540 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
541 struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
543 return target_translate_tls_address (objfile, offset);
546 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in
547 current CU (as is PER_CU). State of the CTX is not affected by the
548 call and return. */
550 static void
551 per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset,
552 struct dwarf2_per_cu_data *per_cu,
553 CORE_ADDR (*get_frame_pc) (void *baton),
554 void *baton)
556 struct dwarf2_locexpr_baton block;
558 block = dwarf2_fetch_die_loc_cu_off (die_offset, per_cu, get_frame_pc, baton);
560 /* DW_OP_call_ref is currently not supported. */
561 gdb_assert (block.per_cu == per_cu);
563 dwarf_expr_eval (ctx, block.data, block.size);
566 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */
568 static void
569 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
571 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) ctx->baton;
573 per_cu_dwarf_call (ctx, die_offset, debaton->per_cu,
574 ctx->funcs->get_frame_pc, ctx->baton);
577 /* Callback function for dwarf2_evaluate_loc_desc. */
579 static struct type *
580 dwarf_expr_get_base_type (struct dwarf_expr_context *ctx,
581 cu_offset die_offset)
583 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) ctx->baton;
585 return dwarf2_get_die_type (die_offset, debaton->per_cu);
588 /* See dwarf2loc.h. */
590 unsigned int entry_values_debug = 0;
592 /* Helper to set entry_values_debug. */
594 static void
595 show_entry_values_debug (struct ui_file *file, int from_tty,
596 struct cmd_list_element *c, const char *value)
598 fprintf_filtered (file,
599 _("Entry values and tail call frames debugging is %s.\n"),
600 value);
603 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
604 CALLER_FRAME (for registers) can be NULL if it is not known. This function
605 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
607 static CORE_ADDR
608 call_site_to_target_addr (struct gdbarch *call_site_gdbarch,
609 struct call_site *call_site,
610 struct frame_info *caller_frame)
612 switch (FIELD_LOC_KIND (call_site->target))
614 case FIELD_LOC_KIND_DWARF_BLOCK:
616 struct dwarf2_locexpr_baton *dwarf_block;
617 struct value *val;
618 struct type *caller_core_addr_type;
619 struct gdbarch *caller_arch;
621 dwarf_block = FIELD_DWARF_BLOCK (call_site->target);
622 if (dwarf_block == NULL)
624 struct bound_minimal_symbol msym;
626 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
627 throw_error (NO_ENTRY_VALUE_ERROR,
628 _("DW_AT_GNU_call_site_target is not specified "
629 "at %s in %s"),
630 paddress (call_site_gdbarch, call_site->pc),
631 (msym.minsym == NULL ? "???"
632 : MSYMBOL_PRINT_NAME (msym.minsym)));
635 if (caller_frame == NULL)
637 struct bound_minimal_symbol msym;
639 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
640 throw_error (NO_ENTRY_VALUE_ERROR,
641 _("DW_AT_GNU_call_site_target DWARF block resolving "
642 "requires known frame which is currently not "
643 "available at %s in %s"),
644 paddress (call_site_gdbarch, call_site->pc),
645 (msym.minsym == NULL ? "???"
646 : MSYMBOL_PRINT_NAME (msym.minsym)));
649 caller_arch = get_frame_arch (caller_frame);
650 caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr;
651 val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame,
652 dwarf_block->data, dwarf_block->size,
653 dwarf_block->per_cu);
654 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
655 location. */
656 if (VALUE_LVAL (val) == lval_memory)
657 return value_address (val);
658 else
659 return value_as_address (val);
662 case FIELD_LOC_KIND_PHYSNAME:
664 const char *physname;
665 struct bound_minimal_symbol msym;
667 physname = FIELD_STATIC_PHYSNAME (call_site->target);
669 /* Handle both the mangled and demangled PHYSNAME. */
670 msym = lookup_minimal_symbol (physname, NULL, NULL);
671 if (msym.minsym == NULL)
673 msym = lookup_minimal_symbol_by_pc (call_site->pc - 1);
674 throw_error (NO_ENTRY_VALUE_ERROR,
675 _("Cannot find function \"%s\" for a call site target "
676 "at %s in %s"),
677 physname, paddress (call_site_gdbarch, call_site->pc),
678 (msym.minsym == NULL ? "???"
679 : MSYMBOL_PRINT_NAME (msym.minsym)));
682 return BMSYMBOL_VALUE_ADDRESS (msym);
685 case FIELD_LOC_KIND_PHYSADDR:
686 return FIELD_STATIC_PHYSADDR (call_site->target);
688 default:
689 internal_error (__FILE__, __LINE__, _("invalid call site target kind"));
693 /* Convert function entry point exact address ADDR to the function which is
694 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
695 NO_ENTRY_VALUE_ERROR otherwise. */
697 static struct symbol *
698 func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr)
700 struct symbol *sym = find_pc_function (addr);
701 struct type *type;
703 if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr)
704 throw_error (NO_ENTRY_VALUE_ERROR,
705 _("DW_TAG_GNU_call_site resolving failed to find function "
706 "name for address %s"),
707 paddress (gdbarch, addr));
709 type = SYMBOL_TYPE (sym);
710 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC);
711 gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC);
713 return sym;
716 /* Verify function with entry point exact address ADDR can never call itself
717 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
718 can call itself via tail calls.
720 If a funtion can tail call itself its entry value based parameters are
721 unreliable. There is no verification whether the value of some/all
722 parameters is unchanged through the self tail call, we expect if there is
723 a self tail call all the parameters can be modified. */
725 static void
726 func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr)
728 struct obstack addr_obstack;
729 struct cleanup *old_chain;
730 CORE_ADDR addr;
732 /* Track here CORE_ADDRs which were already visited. */
733 htab_t addr_hash;
735 /* The verification is completely unordered. Track here function addresses
736 which still need to be iterated. */
737 VEC (CORE_ADDR) *todo = NULL;
739 obstack_init (&addr_obstack);
740 old_chain = make_cleanup_obstack_free (&addr_obstack);
741 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
742 &addr_obstack, hashtab_obstack_allocate,
743 NULL);
744 make_cleanup_htab_delete (addr_hash);
746 make_cleanup (VEC_cleanup (CORE_ADDR), &todo);
748 VEC_safe_push (CORE_ADDR, todo, verify_addr);
749 while (!VEC_empty (CORE_ADDR, todo))
751 struct symbol *func_sym;
752 struct call_site *call_site;
754 addr = VEC_pop (CORE_ADDR, todo);
756 func_sym = func_addr_to_tail_call_list (gdbarch, addr);
758 for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym));
759 call_site; call_site = call_site->tail_call_next)
761 CORE_ADDR target_addr;
762 void **slot;
764 /* CALLER_FRAME with registers is not available for tail-call jumped
765 frames. */
766 target_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
768 if (target_addr == verify_addr)
770 struct bound_minimal_symbol msym;
772 msym = lookup_minimal_symbol_by_pc (verify_addr);
773 throw_error (NO_ENTRY_VALUE_ERROR,
774 _("DW_OP_GNU_entry_value resolving has found "
775 "function \"%s\" at %s can call itself via tail "
776 "calls"),
777 (msym.minsym == NULL ? "???"
778 : MSYMBOL_PRINT_NAME (msym.minsym)),
779 paddress (gdbarch, verify_addr));
782 slot = htab_find_slot (addr_hash, &target_addr, INSERT);
783 if (*slot == NULL)
785 *slot = obstack_copy (&addr_obstack, &target_addr,
786 sizeof (target_addr));
787 VEC_safe_push (CORE_ADDR, todo, target_addr);
792 do_cleanups (old_chain);
795 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
796 ENTRY_VALUES_DEBUG. */
798 static void
799 tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site)
801 CORE_ADDR addr = call_site->pc;
802 struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (addr - 1);
804 fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr),
805 (msym.minsym == NULL ? "???"
806 : MSYMBOL_PRINT_NAME (msym.minsym)));
810 /* vec.h needs single word type name, typedef it. */
811 typedef struct call_site *call_sitep;
813 /* Define VEC (call_sitep) functions. */
814 DEF_VEC_P (call_sitep);
816 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
817 only top callers and bottom callees which are present in both. GDBARCH is
818 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
819 no remaining possibilities to provide unambiguous non-trivial result.
820 RESULTP should point to NULL on the first (initialization) call. Caller is
821 responsible for xfree of any RESULTP data. */
823 static void
824 chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp,
825 VEC (call_sitep) *chain)
827 struct call_site_chain *result = *resultp;
828 long length = VEC_length (call_sitep, chain);
829 int callers, callees, idx;
831 if (result == NULL)
833 /* Create the initial chain containing all the passed PCs. */
835 result = ((struct call_site_chain *)
836 xmalloc (sizeof (*result)
837 + sizeof (*result->call_site) * (length - 1)));
838 result->length = length;
839 result->callers = result->callees = length;
840 if (!VEC_empty (call_sitep, chain))
841 memcpy (result->call_site, VEC_address (call_sitep, chain),
842 sizeof (*result->call_site) * length);
843 *resultp = result;
845 if (entry_values_debug)
847 fprintf_unfiltered (gdb_stdlog, "tailcall: initial:");
848 for (idx = 0; idx < length; idx++)
849 tailcall_dump (gdbarch, result->call_site[idx]);
850 fputc_unfiltered ('\n', gdb_stdlog);
853 return;
856 if (entry_values_debug)
858 fprintf_unfiltered (gdb_stdlog, "tailcall: compare:");
859 for (idx = 0; idx < length; idx++)
860 tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx));
861 fputc_unfiltered ('\n', gdb_stdlog);
864 /* Intersect callers. */
866 callers = std::min ((long) result->callers, length);
867 for (idx = 0; idx < callers; idx++)
868 if (result->call_site[idx] != VEC_index (call_sitep, chain, idx))
870 result->callers = idx;
871 break;
874 /* Intersect callees. */
876 callees = std::min ((long) result->callees, length);
877 for (idx = 0; idx < callees; idx++)
878 if (result->call_site[result->length - 1 - idx]
879 != VEC_index (call_sitep, chain, length - 1 - idx))
881 result->callees = idx;
882 break;
885 if (entry_values_debug)
887 fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:");
888 for (idx = 0; idx < result->callers; idx++)
889 tailcall_dump (gdbarch, result->call_site[idx]);
890 fputs_unfiltered (" |", gdb_stdlog);
891 for (idx = 0; idx < result->callees; idx++)
892 tailcall_dump (gdbarch, result->call_site[result->length
893 - result->callees + idx]);
894 fputc_unfiltered ('\n', gdb_stdlog);
897 if (result->callers == 0 && result->callees == 0)
899 /* There are no common callers or callees. It could be also a direct
900 call (which has length 0) with ambiguous possibility of an indirect
901 call - CALLERS == CALLEES == 0 is valid during the first allocation
902 but any subsequence processing of such entry means ambiguity. */
903 xfree (result);
904 *resultp = NULL;
905 return;
908 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
909 PC again. In such case there must be two different code paths to reach
910 it. CALLERS + CALLEES equal to LENGTH in the case of self tail-call. */
911 gdb_assert (result->callers + result->callees <= result->length);
914 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
915 assumed frames between them use GDBARCH. Use depth first search so we can
916 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
917 would have needless GDB stack overhead. Caller is responsible for xfree of
918 the returned result. Any unreliability results in thrown
919 NO_ENTRY_VALUE_ERROR. */
921 static struct call_site_chain *
922 call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
923 CORE_ADDR callee_pc)
925 CORE_ADDR save_callee_pc = callee_pc;
926 struct obstack addr_obstack;
927 struct cleanup *back_to_retval, *back_to_workdata;
928 struct call_site_chain *retval = NULL;
929 struct call_site *call_site;
931 /* Mark CALL_SITEs so we do not visit the same ones twice. */
932 htab_t addr_hash;
934 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
935 call_site nor any possible call_site at CALLEE_PC's function is there.
936 Any CALL_SITE in CHAIN will be iterated to its siblings - via
937 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
938 VEC (call_sitep) *chain = NULL;
940 /* We are not interested in the specific PC inside the callee function. */
941 callee_pc = get_pc_function_start (callee_pc);
942 if (callee_pc == 0)
943 throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"),
944 paddress (gdbarch, save_callee_pc));
946 back_to_retval = make_cleanup (free_current_contents, &retval);
948 obstack_init (&addr_obstack);
949 back_to_workdata = make_cleanup_obstack_free (&addr_obstack);
950 addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL,
951 &addr_obstack, hashtab_obstack_allocate,
952 NULL);
953 make_cleanup_htab_delete (addr_hash);
955 make_cleanup (VEC_cleanup (call_sitep), &chain);
957 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
958 at the target's function. All the possible tail call sites in the
959 target's function will get iterated as already pushed into CHAIN via their
960 TAIL_CALL_NEXT. */
961 call_site = call_site_for_pc (gdbarch, caller_pc);
963 while (call_site)
965 CORE_ADDR target_func_addr;
966 struct call_site *target_call_site;
968 /* CALLER_FRAME with registers is not available for tail-call jumped
969 frames. */
970 target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL);
972 if (target_func_addr == callee_pc)
974 chain_candidate (gdbarch, &retval, chain);
975 if (retval == NULL)
976 break;
978 /* There is no way to reach CALLEE_PC again as we would prevent
979 entering it twice as being already marked in ADDR_HASH. */
980 target_call_site = NULL;
982 else
984 struct symbol *target_func;
986 target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr);
987 target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func));
992 /* Attempt to visit TARGET_CALL_SITE. */
994 if (target_call_site)
996 void **slot;
998 slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT);
999 if (*slot == NULL)
1001 /* Successfully entered TARGET_CALL_SITE. */
1003 *slot = &target_call_site->pc;
1004 VEC_safe_push (call_sitep, chain, target_call_site);
1005 break;
1009 /* Backtrack (without revisiting the originating call_site). Try the
1010 callers's sibling; if there isn't any try the callers's callers's
1011 sibling etc. */
1013 target_call_site = NULL;
1014 while (!VEC_empty (call_sitep, chain))
1016 call_site = VEC_pop (call_sitep, chain);
1018 gdb_assert (htab_find_slot (addr_hash, &call_site->pc,
1019 NO_INSERT) != NULL);
1020 htab_remove_elt (addr_hash, &call_site->pc);
1022 target_call_site = call_site->tail_call_next;
1023 if (target_call_site)
1024 break;
1027 while (target_call_site);
1029 if (VEC_empty (call_sitep, chain))
1030 call_site = NULL;
1031 else
1032 call_site = VEC_last (call_sitep, chain);
1035 if (retval == NULL)
1037 struct bound_minimal_symbol msym_caller, msym_callee;
1039 msym_caller = lookup_minimal_symbol_by_pc (caller_pc);
1040 msym_callee = lookup_minimal_symbol_by_pc (callee_pc);
1041 throw_error (NO_ENTRY_VALUE_ERROR,
1042 _("There are no unambiguously determinable intermediate "
1043 "callers or callees between caller function \"%s\" at %s "
1044 "and callee function \"%s\" at %s"),
1045 (msym_caller.minsym == NULL
1046 ? "???" : MSYMBOL_PRINT_NAME (msym_caller.minsym)),
1047 paddress (gdbarch, caller_pc),
1048 (msym_callee.minsym == NULL
1049 ? "???" : MSYMBOL_PRINT_NAME (msym_callee.minsym)),
1050 paddress (gdbarch, callee_pc));
1053 do_cleanups (back_to_workdata);
1054 discard_cleanups (back_to_retval);
1055 return retval;
1058 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
1059 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
1060 constructed return NULL. Caller is responsible for xfree of the returned
1061 result. */
1063 struct call_site_chain *
1064 call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc,
1065 CORE_ADDR callee_pc)
1067 struct call_site_chain *retval = NULL;
1071 retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc);
1073 CATCH (e, RETURN_MASK_ERROR)
1075 if (e.error == NO_ENTRY_VALUE_ERROR)
1077 if (entry_values_debug)
1078 exception_print (gdb_stdout, e);
1080 return NULL;
1082 else
1083 throw_exception (e);
1085 END_CATCH
1087 return retval;
1090 /* Return 1 if KIND and KIND_U match PARAMETER. Return 0 otherwise. */
1092 static int
1093 call_site_parameter_matches (struct call_site_parameter *parameter,
1094 enum call_site_parameter_kind kind,
1095 union call_site_parameter_u kind_u)
1097 if (kind == parameter->kind)
1098 switch (kind)
1100 case CALL_SITE_PARAMETER_DWARF_REG:
1101 return kind_u.dwarf_reg == parameter->u.dwarf_reg;
1102 case CALL_SITE_PARAMETER_FB_OFFSET:
1103 return kind_u.fb_offset == parameter->u.fb_offset;
1104 case CALL_SITE_PARAMETER_PARAM_OFFSET:
1105 return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off;
1107 return 0;
1110 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
1111 FRAME is for callee.
1113 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
1114 otherwise. */
1116 static struct call_site_parameter *
1117 dwarf_expr_reg_to_entry_parameter (struct frame_info *frame,
1118 enum call_site_parameter_kind kind,
1119 union call_site_parameter_u kind_u,
1120 struct dwarf2_per_cu_data **per_cu_return)
1122 CORE_ADDR func_addr, caller_pc;
1123 struct gdbarch *gdbarch;
1124 struct frame_info *caller_frame;
1125 struct call_site *call_site;
1126 int iparams;
1127 /* Initialize it just to avoid a GCC false warning. */
1128 struct call_site_parameter *parameter = NULL;
1129 CORE_ADDR target_addr;
1131 while (get_frame_type (frame) == INLINE_FRAME)
1133 frame = get_prev_frame (frame);
1134 gdb_assert (frame != NULL);
1137 func_addr = get_frame_func (frame);
1138 gdbarch = get_frame_arch (frame);
1139 caller_frame = get_prev_frame (frame);
1140 if (gdbarch != frame_unwind_arch (frame))
1142 struct bound_minimal_symbol msym
1143 = lookup_minimal_symbol_by_pc (func_addr);
1144 struct gdbarch *caller_gdbarch = frame_unwind_arch (frame);
1146 throw_error (NO_ENTRY_VALUE_ERROR,
1147 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
1148 "(of %s (%s)) does not match caller gdbarch %s"),
1149 gdbarch_bfd_arch_info (gdbarch)->printable_name,
1150 paddress (gdbarch, func_addr),
1151 (msym.minsym == NULL ? "???"
1152 : MSYMBOL_PRINT_NAME (msym.minsym)),
1153 gdbarch_bfd_arch_info (caller_gdbarch)->printable_name);
1156 if (caller_frame == NULL)
1158 struct bound_minimal_symbol msym
1159 = lookup_minimal_symbol_by_pc (func_addr);
1161 throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving "
1162 "requires caller of %s (%s)"),
1163 paddress (gdbarch, func_addr),
1164 (msym.minsym == NULL ? "???"
1165 : MSYMBOL_PRINT_NAME (msym.minsym)));
1167 caller_pc = get_frame_pc (caller_frame);
1168 call_site = call_site_for_pc (gdbarch, caller_pc);
1170 target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame);
1171 if (target_addr != func_addr)
1173 struct minimal_symbol *target_msym, *func_msym;
1175 target_msym = lookup_minimal_symbol_by_pc (target_addr).minsym;
1176 func_msym = lookup_minimal_symbol_by_pc (func_addr).minsym;
1177 throw_error (NO_ENTRY_VALUE_ERROR,
1178 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
1179 "but the called frame is for %s at %s"),
1180 (target_msym == NULL ? "???"
1181 : MSYMBOL_PRINT_NAME (target_msym)),
1182 paddress (gdbarch, target_addr),
1183 func_msym == NULL ? "???" : MSYMBOL_PRINT_NAME (func_msym),
1184 paddress (gdbarch, func_addr));
1187 /* No entry value based parameters would be reliable if this function can
1188 call itself via tail calls. */
1189 func_verify_no_selftailcall (gdbarch, func_addr);
1191 for (iparams = 0; iparams < call_site->parameter_count; iparams++)
1193 parameter = &call_site->parameter[iparams];
1194 if (call_site_parameter_matches (parameter, kind, kind_u))
1195 break;
1197 if (iparams == call_site->parameter_count)
1199 struct minimal_symbol *msym
1200 = lookup_minimal_symbol_by_pc (caller_pc).minsym;
1202 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
1203 determine its value. */
1204 throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter "
1205 "at DW_TAG_GNU_call_site %s at %s"),
1206 paddress (gdbarch, caller_pc),
1207 msym == NULL ? "???" : MSYMBOL_PRINT_NAME (msym));
1210 *per_cu_return = call_site->per_cu;
1211 return parameter;
1214 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
1215 the normal DW_AT_GNU_call_site_value block. Otherwise return the
1216 DW_AT_GNU_call_site_data_value (dereferenced) block.
1218 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1219 struct value.
1221 Function always returns non-NULL, non-optimized out value. It throws
1222 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
1224 static struct value *
1225 dwarf_entry_parameter_to_value (struct call_site_parameter *parameter,
1226 CORE_ADDR deref_size, struct type *type,
1227 struct frame_info *caller_frame,
1228 struct dwarf2_per_cu_data *per_cu)
1230 const gdb_byte *data_src;
1231 gdb_byte *data;
1232 size_t size;
1234 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1235 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1237 /* DEREF_SIZE size is not verified here. */
1238 if (data_src == NULL)
1239 throw_error (NO_ENTRY_VALUE_ERROR,
1240 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1242 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
1243 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1244 DWARF block. */
1245 data = (gdb_byte *) alloca (size + 1);
1246 memcpy (data, data_src, size);
1247 data[size] = DW_OP_stack_value;
1249 return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu);
1252 /* Execute DWARF block of call_site_parameter which matches KIND and KIND_U.
1253 Choose DEREF_SIZE value of that parameter. Search caller of the CTX's
1254 frame. CTX must be of dwarf_expr_ctx_funcs kind.
1256 The CTX caller can be from a different CU - per_cu_dwarf_call implementation
1257 can be more simple as it does not support cross-CU DWARF executions. */
1259 static void
1260 dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1261 enum call_site_parameter_kind kind,
1262 union call_site_parameter_u kind_u,
1263 int deref_size)
1265 struct dwarf_expr_baton *debaton;
1266 struct frame_info *frame, *caller_frame;
1267 struct dwarf2_per_cu_data *caller_per_cu;
1268 struct dwarf_expr_baton baton_local;
1269 struct dwarf_expr_context saved_ctx;
1270 struct call_site_parameter *parameter;
1271 const gdb_byte *data_src;
1272 size_t size;
1274 gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs);
1275 debaton = (struct dwarf_expr_baton *) ctx->baton;
1276 frame = debaton->frame;
1277 caller_frame = get_prev_frame (frame);
1279 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1280 &caller_per_cu);
1281 data_src = deref_size == -1 ? parameter->value : parameter->data_value;
1282 size = deref_size == -1 ? parameter->value_size : parameter->data_value_size;
1284 /* DEREF_SIZE size is not verified here. */
1285 if (data_src == NULL)
1286 throw_error (NO_ENTRY_VALUE_ERROR,
1287 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1289 baton_local.frame = caller_frame;
1290 baton_local.per_cu = caller_per_cu;
1291 baton_local.obj_address = 0;
1293 saved_ctx.gdbarch = ctx->gdbarch;
1294 saved_ctx.addr_size = ctx->addr_size;
1295 saved_ctx.offset = ctx->offset;
1296 saved_ctx.baton = ctx->baton;
1297 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu));
1298 ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu);
1299 ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu);
1300 ctx->baton = &baton_local;
1302 dwarf_expr_eval (ctx, data_src, size);
1304 ctx->gdbarch = saved_ctx.gdbarch;
1305 ctx->addr_size = saved_ctx.addr_size;
1306 ctx->offset = saved_ctx.offset;
1307 ctx->baton = saved_ctx.baton;
1310 /* Callback function for dwarf2_evaluate_loc_desc.
1311 Fetch the address indexed by DW_OP_GNU_addr_index. */
1313 static CORE_ADDR
1314 dwarf_expr_get_addr_index (void *baton, unsigned int index)
1316 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
1318 return dwarf2_read_addr_index (debaton->per_cu, index);
1321 /* Callback function for get_object_address. Return the address of the VLA
1322 object. */
1324 static CORE_ADDR
1325 dwarf_expr_get_obj_addr (void *baton)
1327 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
1329 gdb_assert (debaton != NULL);
1331 if (debaton->obj_address == 0)
1332 error (_("Location address is not set."));
1334 return debaton->obj_address;
1337 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1338 the indirect method on it, that is use its stored target value, the sole
1339 purpose of entry_data_value_funcs.. */
1341 static struct value *
1342 entry_data_value_coerce_ref (const struct value *value)
1344 struct type *checked_type = check_typedef (value_type (value));
1345 struct value *target_val;
1347 if (TYPE_CODE (checked_type) != TYPE_CODE_REF)
1348 return NULL;
1350 target_val = (struct value *) value_computed_closure (value);
1351 value_incref (target_val);
1352 return target_val;
1355 /* Implement copy_closure. */
1357 static void *
1358 entry_data_value_copy_closure (const struct value *v)
1360 struct value *target_val = (struct value *) value_computed_closure (v);
1362 value_incref (target_val);
1363 return target_val;
1366 /* Implement free_closure. */
1368 static void
1369 entry_data_value_free_closure (struct value *v)
1371 struct value *target_val = (struct value *) value_computed_closure (v);
1373 value_free (target_val);
1376 /* Vector for methods for an entry value reference where the referenced value
1377 is stored in the caller. On the first dereference use
1378 DW_AT_GNU_call_site_data_value in the caller. */
1380 static const struct lval_funcs entry_data_value_funcs =
1382 NULL, /* read */
1383 NULL, /* write */
1384 NULL, /* indirect */
1385 entry_data_value_coerce_ref,
1386 NULL, /* check_synthetic_pointer */
1387 entry_data_value_copy_closure,
1388 entry_data_value_free_closure
1391 /* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KIND_U
1392 are used to match DW_AT_location at the caller's
1393 DW_TAG_GNU_call_site_parameter.
1395 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1396 cannot resolve the parameter for any reason. */
1398 static struct value *
1399 value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame,
1400 enum call_site_parameter_kind kind,
1401 union call_site_parameter_u kind_u)
1403 struct type *checked_type = check_typedef (type);
1404 struct type *target_type = TYPE_TARGET_TYPE (checked_type);
1405 struct frame_info *caller_frame = get_prev_frame (frame);
1406 struct value *outer_val, *target_val, *val;
1407 struct call_site_parameter *parameter;
1408 struct dwarf2_per_cu_data *caller_per_cu;
1410 parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u,
1411 &caller_per_cu);
1413 outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */,
1414 type, caller_frame,
1415 caller_per_cu);
1417 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1418 used and it is not available do not fall back to OUTER_VAL - dereferencing
1419 TYPE_CODE_REF with non-entry data value would give current value - not the
1420 entry value. */
1422 if (TYPE_CODE (checked_type) != TYPE_CODE_REF
1423 || TYPE_TARGET_TYPE (checked_type) == NULL)
1424 return outer_val;
1426 target_val = dwarf_entry_parameter_to_value (parameter,
1427 TYPE_LENGTH (target_type),
1428 target_type, caller_frame,
1429 caller_per_cu);
1431 release_value (target_val);
1432 val = allocate_computed_value (type, &entry_data_value_funcs,
1433 target_val /* closure */);
1435 /* Copy the referencing pointer to the new computed value. */
1436 memcpy (value_contents_raw (val), value_contents_raw (outer_val),
1437 TYPE_LENGTH (checked_type));
1438 set_value_lazy (val, 0);
1440 return val;
1443 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1444 SIZE are DWARF block used to match DW_AT_location at the caller's
1445 DW_TAG_GNU_call_site_parameter.
1447 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1448 cannot resolve the parameter for any reason. */
1450 static struct value *
1451 value_of_dwarf_block_entry (struct type *type, struct frame_info *frame,
1452 const gdb_byte *block, size_t block_len)
1454 union call_site_parameter_u kind_u;
1456 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len);
1457 if (kind_u.dwarf_reg != -1)
1458 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG,
1459 kind_u);
1461 if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset))
1462 return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET,
1463 kind_u);
1465 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1466 suppressed during normal operation. The expression can be arbitrary if
1467 there is no caller-callee entry value binding expected. */
1468 throw_error (NO_ENTRY_VALUE_ERROR,
1469 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1470 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1473 struct piece_closure
1475 /* Reference count. */
1476 int refc;
1478 /* The CU from which this closure's expression came. */
1479 struct dwarf2_per_cu_data *per_cu;
1481 /* The number of pieces used to describe this variable. */
1482 int n_pieces;
1484 /* The target address size, used only for DWARF_VALUE_STACK. */
1485 int addr_size;
1487 /* The pieces themselves. */
1488 struct dwarf_expr_piece *pieces;
1491 /* Allocate a closure for a value formed from separately-described
1492 PIECES. */
1494 static struct piece_closure *
1495 allocate_piece_closure (struct dwarf2_per_cu_data *per_cu,
1496 int n_pieces, struct dwarf_expr_piece *pieces,
1497 int addr_size)
1499 struct piece_closure *c = XCNEW (struct piece_closure);
1500 int i;
1502 c->refc = 1;
1503 c->per_cu = per_cu;
1504 c->n_pieces = n_pieces;
1505 c->addr_size = addr_size;
1506 c->pieces = XCNEWVEC (struct dwarf_expr_piece, n_pieces);
1508 memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
1509 for (i = 0; i < n_pieces; ++i)
1510 if (c->pieces[i].location == DWARF_VALUE_STACK)
1511 value_incref (c->pieces[i].v.value);
1513 return c;
1516 /* The lowest-level function to extract bits from a byte buffer.
1517 SOURCE is the buffer. It is updated if we read to the end of a
1518 byte.
1519 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1520 updated to reflect the number of bits actually read.
1521 NBITS is the number of bits we want to read. It is updated to
1522 reflect the number of bits actually read. This function may read
1523 fewer bits.
1524 BITS_BIG_ENDIAN is taken directly from gdbarch.
1525 This function returns the extracted bits. */
1527 static unsigned int
1528 extract_bits_primitive (const gdb_byte **source,
1529 unsigned int *source_offset_bits,
1530 int *nbits, int bits_big_endian)
1532 unsigned int avail, mask, datum;
1534 gdb_assert (*source_offset_bits < 8);
1536 avail = 8 - *source_offset_bits;
1537 if (avail > *nbits)
1538 avail = *nbits;
1540 mask = (1 << avail) - 1;
1541 datum = **source;
1542 if (bits_big_endian)
1543 datum >>= 8 - (*source_offset_bits + *nbits);
1544 else
1545 datum >>= *source_offset_bits;
1546 datum &= mask;
1548 *nbits -= avail;
1549 *source_offset_bits += avail;
1550 if (*source_offset_bits >= 8)
1552 *source_offset_bits -= 8;
1553 ++*source;
1556 return datum;
1559 /* Extract some bits from a source buffer and move forward in the
1560 buffer.
1562 SOURCE is the source buffer. It is updated as bytes are read.
1563 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1564 bits are read.
1565 NBITS is the number of bits to read.
1566 BITS_BIG_ENDIAN is taken directly from gdbarch.
1568 This function returns the bits that were read. */
1570 static unsigned int
1571 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
1572 int nbits, int bits_big_endian)
1574 unsigned int datum;
1576 gdb_assert (nbits > 0 && nbits <= 8);
1578 datum = extract_bits_primitive (source, source_offset_bits, &nbits,
1579 bits_big_endian);
1580 if (nbits > 0)
1582 unsigned int more;
1584 more = extract_bits_primitive (source, source_offset_bits, &nbits,
1585 bits_big_endian);
1586 if (bits_big_endian)
1587 datum <<= nbits;
1588 else
1589 more <<= nbits;
1590 datum |= more;
1593 return datum;
1596 /* Write some bits into a buffer and move forward in the buffer.
1598 DATUM is the bits to write. The low-order bits of DATUM are used.
1599 DEST is the destination buffer. It is updated as bytes are
1600 written.
1601 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1602 done.
1603 NBITS is the number of valid bits in DATUM.
1604 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1606 static void
1607 insert_bits (unsigned int datum,
1608 gdb_byte *dest, unsigned int dest_offset_bits,
1609 int nbits, int bits_big_endian)
1611 unsigned int mask;
1613 gdb_assert (dest_offset_bits + nbits <= 8);
1615 mask = (1 << nbits) - 1;
1616 if (bits_big_endian)
1618 datum <<= 8 - (dest_offset_bits + nbits);
1619 mask <<= 8 - (dest_offset_bits + nbits);
1621 else
1623 datum <<= dest_offset_bits;
1624 mask <<= dest_offset_bits;
1627 gdb_assert ((datum & ~mask) == 0);
1629 *dest = (*dest & ~mask) | datum;
1632 /* Copy bits from a source to a destination.
1634 DEST is where the bits should be written.
1635 DEST_OFFSET_BITS is the bit offset into DEST.
1636 SOURCE is the source of bits.
1637 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1638 BIT_COUNT is the number of bits to copy.
1639 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1641 static void
1642 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
1643 const gdb_byte *source, unsigned int source_offset_bits,
1644 unsigned int bit_count,
1645 int bits_big_endian)
1647 unsigned int dest_avail;
1648 int datum;
1650 /* Reduce everything to byte-size pieces. */
1651 dest += dest_offset_bits / 8;
1652 dest_offset_bits %= 8;
1653 source += source_offset_bits / 8;
1654 source_offset_bits %= 8;
1656 dest_avail = 8 - dest_offset_bits % 8;
1658 /* See if we can fill the first destination byte. */
1659 if (dest_avail < bit_count)
1661 datum = extract_bits (&source, &source_offset_bits, dest_avail,
1662 bits_big_endian);
1663 insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
1664 ++dest;
1665 dest_offset_bits = 0;
1666 bit_count -= dest_avail;
1669 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1670 than 8 bits remaining. */
1671 gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
1672 for (; bit_count >= 8; bit_count -= 8)
1674 datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
1675 *dest++ = (gdb_byte) datum;
1678 /* Finally, we may have a few leftover bits. */
1679 gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
1680 if (bit_count > 0)
1682 datum = extract_bits (&source, &source_offset_bits, bit_count,
1683 bits_big_endian);
1684 insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
1688 static void
1689 read_pieced_value (struct value *v)
1691 int i;
1692 long offset = 0;
1693 ULONGEST bits_to_skip;
1694 gdb_byte *contents;
1695 struct piece_closure *c
1696 = (struct piece_closure *) value_computed_closure (v);
1697 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
1698 size_t type_len;
1699 size_t buffer_size = 0;
1700 gdb_byte *buffer = NULL;
1701 struct cleanup *cleanup;
1702 int bits_big_endian
1703 = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
1705 if (value_type (v) != value_enclosing_type (v))
1706 internal_error (__FILE__, __LINE__,
1707 _("Should not be able to create a lazy value with "
1708 "an enclosing type"));
1710 cleanup = make_cleanup (free_current_contents, &buffer);
1712 contents = value_contents_raw (v);
1713 bits_to_skip = 8 * value_offset (v);
1714 if (value_bitsize (v))
1716 bits_to_skip += value_bitpos (v);
1717 type_len = value_bitsize (v);
1719 else
1720 type_len = 8 * TYPE_LENGTH (value_type (v));
1722 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1724 struct dwarf_expr_piece *p = &c->pieces[i];
1725 size_t this_size, this_size_bits;
1726 long dest_offset_bits, source_offset_bits, source_offset;
1727 const gdb_byte *intermediate_buffer;
1729 /* Compute size, source, and destination offsets for copying, in
1730 bits. */
1731 this_size_bits = p->size;
1732 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1734 bits_to_skip -= this_size_bits;
1735 continue;
1737 if (bits_to_skip > 0)
1739 dest_offset_bits = 0;
1740 source_offset_bits = bits_to_skip;
1741 this_size_bits -= bits_to_skip;
1742 bits_to_skip = 0;
1744 else
1746 dest_offset_bits = offset;
1747 source_offset_bits = 0;
1749 if (this_size_bits > type_len - offset)
1750 this_size_bits = type_len - offset;
1752 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1753 source_offset = source_offset_bits / 8;
1754 if (buffer_size < this_size)
1756 buffer_size = this_size;
1757 buffer = (gdb_byte *) xrealloc (buffer, buffer_size);
1759 intermediate_buffer = buffer;
1761 /* Copy from the source to DEST_BUFFER. */
1762 switch (p->location)
1764 case DWARF_VALUE_REGISTER:
1766 struct gdbarch *arch = get_frame_arch (frame);
1767 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1768 int optim, unavail;
1769 LONGEST reg_offset = source_offset;
1771 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1772 && this_size < register_size (arch, gdb_regnum))
1774 /* Big-endian, and we want less than full size. */
1775 reg_offset = register_size (arch, gdb_regnum) - this_size;
1776 /* We want the lower-order THIS_SIZE_BITS of the bytes
1777 we extract from the register. */
1778 source_offset_bits += 8 * this_size - this_size_bits;
1781 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1782 this_size, buffer,
1783 &optim, &unavail))
1785 /* Just so garbage doesn't ever shine through. */
1786 memset (buffer, 0, this_size);
1788 if (optim)
1789 mark_value_bits_optimized_out (v, offset, this_size_bits);
1790 if (unavail)
1791 mark_value_bits_unavailable (v, offset, this_size_bits);
1794 break;
1796 case DWARF_VALUE_MEMORY:
1797 read_value_memory (v, offset,
1798 p->v.mem.in_stack_memory,
1799 p->v.mem.addr + source_offset,
1800 buffer, this_size);
1801 break;
1803 case DWARF_VALUE_STACK:
1805 size_t n = this_size;
1807 if (n > c->addr_size - source_offset)
1808 n = (c->addr_size >= source_offset
1809 ? c->addr_size - source_offset
1810 : 0);
1811 if (n == 0)
1813 /* Nothing. */
1815 else
1817 const gdb_byte *val_bytes = value_contents_all (p->v.value);
1819 intermediate_buffer = val_bytes + source_offset;
1822 break;
1824 case DWARF_VALUE_LITERAL:
1826 size_t n = this_size;
1828 if (n > p->v.literal.length - source_offset)
1829 n = (p->v.literal.length >= source_offset
1830 ? p->v.literal.length - source_offset
1831 : 0);
1832 if (n != 0)
1833 intermediate_buffer = p->v.literal.data + source_offset;
1835 break;
1837 /* These bits show up as zeros -- but do not cause the value
1838 to be considered optimized-out. */
1839 case DWARF_VALUE_IMPLICIT_POINTER:
1840 break;
1842 case DWARF_VALUE_OPTIMIZED_OUT:
1843 mark_value_bits_optimized_out (v, offset, this_size_bits);
1844 break;
1846 default:
1847 internal_error (__FILE__, __LINE__, _("invalid location type"));
1850 if (p->location != DWARF_VALUE_OPTIMIZED_OUT
1851 && p->location != DWARF_VALUE_IMPLICIT_POINTER)
1852 copy_bitwise (contents, dest_offset_bits,
1853 intermediate_buffer, source_offset_bits % 8,
1854 this_size_bits, bits_big_endian);
1856 offset += this_size_bits;
1859 do_cleanups (cleanup);
1862 static void
1863 write_pieced_value (struct value *to, struct value *from)
1865 int i;
1866 long offset = 0;
1867 ULONGEST bits_to_skip;
1868 const gdb_byte *contents;
1869 struct piece_closure *c
1870 = (struct piece_closure *) value_computed_closure (to);
1871 struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
1872 size_t type_len;
1873 size_t buffer_size = 0;
1874 gdb_byte *buffer = NULL;
1875 struct cleanup *cleanup;
1876 int bits_big_endian
1877 = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
1879 if (frame == NULL)
1881 mark_value_bytes_optimized_out (to, 0, TYPE_LENGTH (value_type (to)));
1882 return;
1885 cleanup = make_cleanup (free_current_contents, &buffer);
1887 contents = value_contents (from);
1888 bits_to_skip = 8 * value_offset (to);
1889 if (value_bitsize (to))
1891 bits_to_skip += value_bitpos (to);
1892 type_len = value_bitsize (to);
1894 else
1895 type_len = 8 * TYPE_LENGTH (value_type (to));
1897 for (i = 0; i < c->n_pieces && offset < type_len; i++)
1899 struct dwarf_expr_piece *p = &c->pieces[i];
1900 size_t this_size_bits, this_size;
1901 long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
1902 int need_bitwise;
1903 const gdb_byte *source_buffer;
1905 this_size_bits = p->size;
1906 if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
1908 bits_to_skip -= this_size_bits;
1909 continue;
1911 if (this_size_bits > type_len - offset)
1912 this_size_bits = type_len - offset;
1913 if (bits_to_skip > 0)
1915 dest_offset_bits = bits_to_skip;
1916 source_offset_bits = 0;
1917 this_size_bits -= bits_to_skip;
1918 bits_to_skip = 0;
1920 else
1922 dest_offset_bits = 0;
1923 source_offset_bits = offset;
1926 this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
1927 source_offset = source_offset_bits / 8;
1928 dest_offset = dest_offset_bits / 8;
1929 if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
1931 source_buffer = contents + source_offset;
1932 need_bitwise = 0;
1934 else
1936 if (buffer_size < this_size)
1938 buffer_size = this_size;
1939 buffer = (gdb_byte *) xrealloc (buffer, buffer_size);
1941 source_buffer = buffer;
1942 need_bitwise = 1;
1945 switch (p->location)
1947 case DWARF_VALUE_REGISTER:
1949 struct gdbarch *arch = get_frame_arch (frame);
1950 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, p->v.regno);
1951 int reg_offset = dest_offset;
1953 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
1954 && this_size <= register_size (arch, gdb_regnum))
1956 /* Big-endian, and we want less than full size. */
1957 reg_offset = register_size (arch, gdb_regnum) - this_size;
1960 if (need_bitwise)
1962 int optim, unavail;
1964 if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset,
1965 this_size, buffer,
1966 &optim, &unavail))
1968 if (optim)
1969 throw_error (OPTIMIZED_OUT_ERROR,
1970 _("Can't do read-modify-write to "
1971 "update bitfield; containing word "
1972 "has been optimized out"));
1973 if (unavail)
1974 throw_error (NOT_AVAILABLE_ERROR,
1975 _("Can't do read-modify-write to update "
1976 "bitfield; containing word "
1977 "is unavailable"));
1979 copy_bitwise (buffer, dest_offset_bits,
1980 contents, source_offset_bits,
1981 this_size_bits,
1982 bits_big_endian);
1985 put_frame_register_bytes (frame, gdb_regnum, reg_offset,
1986 this_size, source_buffer);
1988 break;
1989 case DWARF_VALUE_MEMORY:
1990 if (need_bitwise)
1992 /* Only the first and last bytes can possibly have any
1993 bits reused. */
1994 read_memory (p->v.mem.addr + dest_offset, buffer, 1);
1995 read_memory (p->v.mem.addr + dest_offset + this_size - 1,
1996 buffer + this_size - 1, 1);
1997 copy_bitwise (buffer, dest_offset_bits,
1998 contents, source_offset_bits,
1999 this_size_bits,
2000 bits_big_endian);
2003 write_memory (p->v.mem.addr + dest_offset,
2004 source_buffer, this_size);
2005 break;
2006 default:
2007 mark_value_bytes_optimized_out (to, 0, TYPE_LENGTH (value_type (to)));
2008 break;
2010 offset += this_size_bits;
2013 do_cleanups (cleanup);
2016 /* An implementation of an lval_funcs method to see whether a value is
2017 a synthetic pointer. */
2019 static int
2020 check_pieced_synthetic_pointer (const struct value *value, LONGEST bit_offset,
2021 int bit_length)
2023 struct piece_closure *c
2024 = (struct piece_closure *) value_computed_closure (value);
2025 int i;
2027 bit_offset += 8 * value_offset (value);
2028 if (value_bitsize (value))
2029 bit_offset += value_bitpos (value);
2031 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2033 struct dwarf_expr_piece *p = &c->pieces[i];
2034 size_t this_size_bits = p->size;
2036 if (bit_offset > 0)
2038 if (bit_offset >= this_size_bits)
2040 bit_offset -= this_size_bits;
2041 continue;
2044 bit_length -= this_size_bits - bit_offset;
2045 bit_offset = 0;
2047 else
2048 bit_length -= this_size_bits;
2050 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2051 return 0;
2054 return 1;
2057 /* A wrapper function for get_frame_address_in_block. */
2059 static CORE_ADDR
2060 get_frame_address_in_block_wrapper (void *baton)
2062 return get_frame_address_in_block ((struct frame_info *) baton);
2065 /* Fetch a DW_AT_const_value through a synthetic pointer. */
2067 static struct value *
2068 fetch_const_value_from_synthetic_pointer (sect_offset die, LONGEST byte_offset,
2069 struct dwarf2_per_cu_data *per_cu,
2070 struct type *type)
2072 struct value *result = NULL;
2073 struct obstack temp_obstack;
2074 struct cleanup *cleanup;
2075 const gdb_byte *bytes;
2076 LONGEST len;
2078 obstack_init (&temp_obstack);
2079 cleanup = make_cleanup_obstack_free (&temp_obstack);
2080 bytes = dwarf2_fetch_constant_bytes (die, per_cu, &temp_obstack, &len);
2082 if (bytes != NULL)
2084 if (byte_offset >= 0
2085 && byte_offset + TYPE_LENGTH (TYPE_TARGET_TYPE (type)) <= len)
2087 bytes += byte_offset;
2088 result = value_from_contents (TYPE_TARGET_TYPE (type), bytes);
2090 else
2091 invalid_synthetic_pointer ();
2093 else
2094 result = allocate_optimized_out_value (TYPE_TARGET_TYPE (type));
2096 do_cleanups (cleanup);
2098 return result;
2101 /* Fetch the value pointed to by a synthetic pointer. */
2103 static struct value *
2104 indirect_synthetic_pointer (sect_offset die, LONGEST byte_offset,
2105 struct dwarf2_per_cu_data *per_cu,
2106 struct frame_info *frame, struct type *type)
2108 /* Fetch the location expression of the DIE we're pointing to. */
2109 struct dwarf2_locexpr_baton baton
2110 = dwarf2_fetch_die_loc_sect_off (die, per_cu,
2111 get_frame_address_in_block_wrapper, frame);
2113 /* If pointed-to DIE has a DW_AT_location, evaluate it and return the
2114 resulting value. Otherwise, it may have a DW_AT_const_value instead,
2115 or it may've been optimized out. */
2116 if (baton.data != NULL)
2117 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
2118 baton.data, baton.size, baton.per_cu,
2119 byte_offset);
2120 else
2121 return fetch_const_value_from_synthetic_pointer (die, byte_offset, per_cu,
2122 type);
2125 /* An implementation of an lval_funcs method to indirect through a
2126 pointer. This handles the synthetic pointer case when needed. */
2128 static struct value *
2129 indirect_pieced_value (struct value *value)
2131 struct piece_closure *c
2132 = (struct piece_closure *) value_computed_closure (value);
2133 struct type *type;
2134 struct frame_info *frame;
2135 struct dwarf2_locexpr_baton baton;
2136 int i, bit_length;
2137 LONGEST bit_offset;
2138 struct dwarf_expr_piece *piece = NULL;
2139 LONGEST byte_offset;
2140 enum bfd_endian byte_order;
2142 type = check_typedef (value_type (value));
2143 if (TYPE_CODE (type) != TYPE_CODE_PTR)
2144 return NULL;
2146 bit_length = 8 * TYPE_LENGTH (type);
2147 bit_offset = 8 * value_offset (value);
2148 if (value_bitsize (value))
2149 bit_offset += value_bitpos (value);
2151 for (i = 0; i < c->n_pieces && bit_length > 0; i++)
2153 struct dwarf_expr_piece *p = &c->pieces[i];
2154 size_t this_size_bits = p->size;
2156 if (bit_offset > 0)
2158 if (bit_offset >= this_size_bits)
2160 bit_offset -= this_size_bits;
2161 continue;
2164 bit_length -= this_size_bits - bit_offset;
2165 bit_offset = 0;
2167 else
2168 bit_length -= this_size_bits;
2170 if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
2171 return NULL;
2173 if (bit_length != 0)
2174 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
2176 piece = p;
2177 break;
2180 gdb_assert (piece != NULL);
2181 frame = get_selected_frame (_("No frame selected."));
2183 /* This is an offset requested by GDB, such as value subscripts.
2184 However, due to how synthetic pointers are implemented, this is
2185 always presented to us as a pointer type. This means we have to
2186 sign-extend it manually as appropriate. Use raw
2187 extract_signed_integer directly rather than value_as_address and
2188 sign extend afterwards on architectures that would need it
2189 (mostly everywhere except MIPS, which has signed addresses) as
2190 the later would go through gdbarch_pointer_to_address and thus
2191 return a CORE_ADDR with high bits set on architectures that
2192 encode address spaces and other things in CORE_ADDR. */
2193 byte_order = gdbarch_byte_order (get_frame_arch (frame));
2194 byte_offset = extract_signed_integer (value_contents (value),
2195 TYPE_LENGTH (type), byte_order);
2196 byte_offset += piece->v.ptr.offset;
2198 return indirect_synthetic_pointer (piece->v.ptr.die, byte_offset, c->per_cu,
2199 frame, type);
2202 /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
2203 references. */
2205 static struct value *
2206 coerce_pieced_ref (const struct value *value)
2208 struct type *type = check_typedef (value_type (value));
2210 if (value_bits_synthetic_pointer (value, value_embedded_offset (value),
2211 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
2213 const struct piece_closure *closure
2214 = (struct piece_closure *) value_computed_closure (value);
2215 struct frame_info *frame
2216 = get_selected_frame (_("No frame selected."));
2218 /* gdb represents synthetic pointers as pieced values with a single
2219 piece. */
2220 gdb_assert (closure != NULL);
2221 gdb_assert (closure->n_pieces == 1);
2223 return indirect_synthetic_pointer (closure->pieces->v.ptr.die,
2224 closure->pieces->v.ptr.offset,
2225 closure->per_cu, frame, type);
2227 else
2229 /* Else: not a synthetic reference; do nothing. */
2230 return NULL;
2234 static void *
2235 copy_pieced_value_closure (const struct value *v)
2237 struct piece_closure *c
2238 = (struct piece_closure *) value_computed_closure (v);
2240 ++c->refc;
2241 return c;
2244 static void
2245 free_pieced_value_closure (struct value *v)
2247 struct piece_closure *c
2248 = (struct piece_closure *) value_computed_closure (v);
2250 --c->refc;
2251 if (c->refc == 0)
2253 int i;
2255 for (i = 0; i < c->n_pieces; ++i)
2256 if (c->pieces[i].location == DWARF_VALUE_STACK)
2257 value_free (c->pieces[i].v.value);
2259 xfree (c->pieces);
2260 xfree (c);
2264 /* Functions for accessing a variable described by DW_OP_piece. */
2265 static const struct lval_funcs pieced_value_funcs = {
2266 read_pieced_value,
2267 write_pieced_value,
2268 indirect_pieced_value,
2269 coerce_pieced_ref,
2270 check_pieced_synthetic_pointer,
2271 copy_pieced_value_closure,
2272 free_pieced_value_closure
2275 /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */
2277 const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs =
2279 dwarf_expr_read_addr_from_reg,
2280 dwarf_expr_get_reg_value,
2281 dwarf_expr_read_mem,
2282 dwarf_expr_frame_base,
2283 dwarf_expr_frame_cfa,
2284 dwarf_expr_frame_pc,
2285 dwarf_expr_tls_address,
2286 dwarf_expr_dwarf_call,
2287 dwarf_expr_get_base_type,
2288 dwarf_expr_push_dwarf_reg_entry_value,
2289 dwarf_expr_get_addr_index,
2290 dwarf_expr_get_obj_addr
2293 /* Evaluate a location description, starting at DATA and with length
2294 SIZE, to find the current location of variable of TYPE in the
2295 context of FRAME. BYTE_OFFSET is applied after the contents are
2296 computed. */
2298 static struct value *
2299 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
2300 const gdb_byte *data, size_t size,
2301 struct dwarf2_per_cu_data *per_cu,
2302 LONGEST byte_offset)
2304 struct value *retval;
2305 struct dwarf_expr_baton baton;
2306 struct dwarf_expr_context *ctx;
2307 struct cleanup *old_chain, *value_chain;
2308 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2310 if (byte_offset < 0)
2311 invalid_synthetic_pointer ();
2313 if (size == 0)
2314 return allocate_optimized_out_value (type);
2316 baton.frame = frame;
2317 baton.per_cu = per_cu;
2318 baton.obj_address = 0;
2320 ctx = new_dwarf_expr_context ();
2321 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2322 value_chain = make_cleanup_value_free_to_mark (value_mark ());
2324 ctx->gdbarch = get_objfile_arch (objfile);
2325 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2326 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2327 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2328 ctx->baton = &baton;
2329 ctx->funcs = &dwarf_expr_ctx_funcs;
2333 dwarf_expr_eval (ctx, data, size);
2335 CATCH (ex, RETURN_MASK_ERROR)
2337 if (ex.error == NOT_AVAILABLE_ERROR)
2339 do_cleanups (old_chain);
2340 retval = allocate_value (type);
2341 mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
2342 return retval;
2344 else if (ex.error == NO_ENTRY_VALUE_ERROR)
2346 if (entry_values_debug)
2347 exception_print (gdb_stdout, ex);
2348 do_cleanups (old_chain);
2349 return allocate_optimized_out_value (type);
2351 else
2352 throw_exception (ex);
2354 END_CATCH
2356 if (ctx->num_pieces > 0)
2358 struct piece_closure *c;
2359 struct frame_id frame_id = get_frame_id (frame);
2360 ULONGEST bit_size = 0;
2361 int i;
2363 for (i = 0; i < ctx->num_pieces; ++i)
2364 bit_size += ctx->pieces[i].size;
2365 if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
2366 invalid_synthetic_pointer ();
2368 c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
2369 ctx->addr_size);
2370 /* We must clean up the value chain after creating the piece
2371 closure but before allocating the result. */
2372 do_cleanups (value_chain);
2373 retval = allocate_computed_value (type, &pieced_value_funcs, c);
2374 VALUE_FRAME_ID (retval) = frame_id;
2375 set_value_offset (retval, byte_offset);
2377 else
2379 switch (ctx->location)
2381 case DWARF_VALUE_REGISTER:
2383 struct gdbarch *arch = get_frame_arch (frame);
2384 int dwarf_regnum
2385 = longest_to_int (value_as_long (dwarf_expr_fetch (ctx, 0)));
2386 int gdb_regnum = dwarf_reg_to_regnum_or_error (arch, dwarf_regnum);
2388 if (byte_offset != 0)
2389 error (_("cannot use offset on synthetic pointer to register"));
2390 do_cleanups (value_chain);
2391 retval = value_from_register (type, gdb_regnum, frame);
2392 if (value_optimized_out (retval))
2394 struct value *tmp;
2396 /* This means the register has undefined value / was
2397 not saved. As we're computing the location of some
2398 variable etc. in the program, not a value for
2399 inspecting a register ($pc, $sp, etc.), return a
2400 generic optimized out value instead, so that we show
2401 <optimized out> instead of <not saved>. */
2402 do_cleanups (value_chain);
2403 tmp = allocate_value (type);
2404 value_contents_copy (tmp, 0, retval, 0, TYPE_LENGTH (type));
2405 retval = tmp;
2408 break;
2410 case DWARF_VALUE_MEMORY:
2412 struct type *ptr_type;
2413 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
2414 int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
2416 /* DW_OP_deref_size (and possibly other operations too) may
2417 create a pointer instead of an address. Ideally, the
2418 pointer to address conversion would be performed as part
2419 of those operations, but the type of the object to
2420 which the address refers is not known at the time of
2421 the operation. Therefore, we do the conversion here
2422 since the type is readily available. */
2424 switch (TYPE_CODE (type))
2426 case TYPE_CODE_FUNC:
2427 case TYPE_CODE_METHOD:
2428 ptr_type = builtin_type (ctx->gdbarch)->builtin_func_ptr;
2429 break;
2430 default:
2431 ptr_type = builtin_type (ctx->gdbarch)->builtin_data_ptr;
2432 break;
2434 address = value_as_address (value_from_pointer (ptr_type, address));
2436 do_cleanups (value_chain);
2437 retval = value_at_lazy (type, address + byte_offset);
2438 if (in_stack_memory)
2439 set_value_stack (retval, 1);
2441 break;
2443 case DWARF_VALUE_STACK:
2445 struct value *value = dwarf_expr_fetch (ctx, 0);
2446 gdb_byte *contents;
2447 const gdb_byte *val_bytes;
2448 size_t n = TYPE_LENGTH (value_type (value));
2450 if (byte_offset + TYPE_LENGTH (type) > n)
2451 invalid_synthetic_pointer ();
2453 val_bytes = value_contents_all (value);
2454 val_bytes += byte_offset;
2455 n -= byte_offset;
2457 /* Preserve VALUE because we are going to free values back
2458 to the mark, but we still need the value contents
2459 below. */
2460 value_incref (value);
2461 do_cleanups (value_chain);
2462 make_cleanup_value_free (value);
2464 retval = allocate_value (type);
2465 contents = value_contents_raw (retval);
2466 if (n > TYPE_LENGTH (type))
2468 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2470 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2471 val_bytes += n - TYPE_LENGTH (type);
2472 n = TYPE_LENGTH (type);
2474 memcpy (contents, val_bytes, n);
2476 break;
2478 case DWARF_VALUE_LITERAL:
2480 bfd_byte *contents;
2481 const bfd_byte *ldata;
2482 size_t n = ctx->len;
2484 if (byte_offset + TYPE_LENGTH (type) > n)
2485 invalid_synthetic_pointer ();
2487 do_cleanups (value_chain);
2488 retval = allocate_value (type);
2489 contents = value_contents_raw (retval);
2491 ldata = ctx->data + byte_offset;
2492 n -= byte_offset;
2494 if (n > TYPE_LENGTH (type))
2496 struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile);
2498 if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG)
2499 ldata += n - TYPE_LENGTH (type);
2500 n = TYPE_LENGTH (type);
2502 memcpy (contents, ldata, n);
2504 break;
2506 case DWARF_VALUE_OPTIMIZED_OUT:
2507 do_cleanups (value_chain);
2508 retval = allocate_optimized_out_value (type);
2509 break;
2511 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2512 operation by execute_stack_op. */
2513 case DWARF_VALUE_IMPLICIT_POINTER:
2514 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2515 it can only be encountered when making a piece. */
2516 default:
2517 internal_error (__FILE__, __LINE__, _("invalid location type"));
2521 set_value_initialized (retval, ctx->initialized);
2523 do_cleanups (old_chain);
2525 return retval;
2528 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2529 passes 0 as the byte_offset. */
2531 struct value *
2532 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
2533 const gdb_byte *data, size_t size,
2534 struct dwarf2_per_cu_data *per_cu)
2536 return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
2539 /* Evaluates a dwarf expression and stores the result in VAL, expecting
2540 that the dwarf expression only produces a single CORE_ADDR. FRAME is the
2541 frame in which the expression is evaluated. ADDR is a context (location of
2542 a variable) and might be needed to evaluate the location expression.
2543 Returns 1 on success, 0 otherwise. */
2545 static int
2546 dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton *dlbaton,
2547 struct frame_info *frame,
2548 CORE_ADDR addr,
2549 CORE_ADDR *valp)
2551 struct dwarf_expr_context *ctx;
2552 struct dwarf_expr_baton baton;
2553 struct objfile *objfile;
2554 struct cleanup *cleanup;
2556 if (dlbaton == NULL || dlbaton->size == 0)
2557 return 0;
2559 ctx = new_dwarf_expr_context ();
2560 cleanup = make_cleanup_free_dwarf_expr_context (ctx);
2562 baton.frame = frame;
2563 baton.per_cu = dlbaton->per_cu;
2564 baton.obj_address = addr;
2566 objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2568 ctx->gdbarch = get_objfile_arch (objfile);
2569 ctx->addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2570 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (dlbaton->per_cu);
2571 ctx->offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
2572 ctx->funcs = &dwarf_expr_ctx_funcs;
2573 ctx->baton = &baton;
2575 dwarf_expr_eval (ctx, dlbaton->data, dlbaton->size);
2577 switch (ctx->location)
2579 case DWARF_VALUE_REGISTER:
2580 case DWARF_VALUE_MEMORY:
2581 case DWARF_VALUE_STACK:
2582 *valp = dwarf_expr_fetch_address (ctx, 0);
2583 if (ctx->location == DWARF_VALUE_REGISTER)
2584 *valp = dwarf_expr_read_addr_from_reg (&baton, *valp);
2585 do_cleanups (cleanup);
2586 return 1;
2587 case DWARF_VALUE_LITERAL:
2588 *valp = extract_signed_integer (ctx->data, ctx->len,
2589 gdbarch_byte_order (ctx->gdbarch));
2590 do_cleanups (cleanup);
2591 return 1;
2592 /* Unsupported dwarf values. */
2593 case DWARF_VALUE_OPTIMIZED_OUT:
2594 case DWARF_VALUE_IMPLICIT_POINTER:
2595 break;
2598 do_cleanups (cleanup);
2599 return 0;
2602 /* See dwarf2loc.h. */
2605 dwarf2_evaluate_property (const struct dynamic_prop *prop,
2606 struct frame_info *frame,
2607 struct property_addr_info *addr_stack,
2608 CORE_ADDR *value)
2610 if (prop == NULL)
2611 return 0;
2613 if (frame == NULL && has_stack_frames ())
2614 frame = get_selected_frame (NULL);
2616 switch (prop->kind)
2618 case PROP_LOCEXPR:
2620 const struct dwarf2_property_baton *baton
2621 = (const struct dwarf2_property_baton *) prop->data.baton;
2623 if (dwarf2_locexpr_baton_eval (&baton->locexpr, frame,
2624 addr_stack ? addr_stack->addr : 0,
2625 value))
2627 if (baton->referenced_type)
2629 struct value *val = value_at (baton->referenced_type, *value);
2631 *value = value_as_address (val);
2633 return 1;
2636 break;
2638 case PROP_LOCLIST:
2640 struct dwarf2_property_baton *baton
2641 = (struct dwarf2_property_baton *) prop->data.baton;
2642 CORE_ADDR pc = get_frame_address_in_block (frame);
2643 const gdb_byte *data;
2644 struct value *val;
2645 size_t size;
2647 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2648 if (data != NULL)
2650 val = dwarf2_evaluate_loc_desc (baton->referenced_type, frame, data,
2651 size, baton->loclist.per_cu);
2652 if (!value_optimized_out (val))
2654 *value = value_as_address (val);
2655 return 1;
2659 break;
2661 case PROP_CONST:
2662 *value = prop->data.const_val;
2663 return 1;
2665 case PROP_ADDR_OFFSET:
2667 struct dwarf2_property_baton *baton
2668 = (struct dwarf2_property_baton *) prop->data.baton;
2669 struct property_addr_info *pinfo;
2670 struct value *val;
2672 for (pinfo = addr_stack; pinfo != NULL; pinfo = pinfo->next)
2673 if (pinfo->type == baton->referenced_type)
2674 break;
2675 if (pinfo == NULL)
2676 error (_("cannot find reference address for offset property"));
2677 if (pinfo->valaddr != NULL)
2678 val = value_from_contents
2679 (baton->offset_info.type,
2680 pinfo->valaddr + baton->offset_info.offset);
2681 else
2682 val = value_at (baton->offset_info.type,
2683 pinfo->addr + baton->offset_info.offset);
2684 *value = value_as_address (val);
2685 return 1;
2689 return 0;
2692 /* See dwarf2loc.h. */
2694 void
2695 dwarf2_compile_property_to_c (struct ui_file *stream,
2696 const char *result_name,
2697 struct gdbarch *gdbarch,
2698 unsigned char *registers_used,
2699 const struct dynamic_prop *prop,
2700 CORE_ADDR pc,
2701 struct symbol *sym)
2703 struct dwarf2_property_baton *baton
2704 = (struct dwarf2_property_baton *) prop->data.baton;
2705 const gdb_byte *data;
2706 size_t size;
2707 struct dwarf2_per_cu_data *per_cu;
2709 if (prop->kind == PROP_LOCEXPR)
2711 data = baton->locexpr.data;
2712 size = baton->locexpr.size;
2713 per_cu = baton->locexpr.per_cu;
2715 else
2717 gdb_assert (prop->kind == PROP_LOCLIST);
2719 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2720 per_cu = baton->loclist.per_cu;
2723 compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
2724 gdbarch, registers_used,
2725 dwarf2_per_cu_addr_size (per_cu),
2726 data, data + size, per_cu);
2730 /* Helper functions and baton for dwarf2_loc_desc_get_symbol_read_needs. */
2732 struct symbol_needs_baton
2734 enum symbol_needs_kind needs;
2735 struct dwarf2_per_cu_data *per_cu;
2738 /* Reads from registers do require a frame. */
2739 static CORE_ADDR
2740 symbol_needs_read_addr_from_reg (void *baton, int regnum)
2742 struct symbol_needs_baton *nf_baton = (struct symbol_needs_baton *) baton;
2744 nf_baton->needs = SYMBOL_NEEDS_FRAME;
2745 return 1;
2748 /* struct dwarf_expr_context_funcs' "get_reg_value" callback:
2749 Reads from registers do require a frame. */
2751 static struct value *
2752 symbol_needs_get_reg_value (void *baton, struct type *type, int regnum)
2754 struct symbol_needs_baton *nf_baton = (struct symbol_needs_baton *) baton;
2756 nf_baton->needs = SYMBOL_NEEDS_FRAME;
2757 return value_zero (type, not_lval);
2760 /* Reads from memory do not require a frame. */
2761 static void
2762 symbol_needs_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
2764 memset (buf, 0, len);
2767 /* Frame-relative accesses do require a frame. */
2768 static void
2769 symbol_needs_frame_base (void *baton, const gdb_byte **start, size_t * length)
2771 static gdb_byte lit0 = DW_OP_lit0;
2772 struct symbol_needs_baton *nf_baton = (struct symbol_needs_baton *) baton;
2774 *start = &lit0;
2775 *length = 1;
2777 nf_baton->needs = SYMBOL_NEEDS_FRAME;
2780 /* CFA accesses require a frame. */
2782 static CORE_ADDR
2783 symbol_needs_frame_cfa (void *baton)
2785 struct symbol_needs_baton *nf_baton = (struct symbol_needs_baton *) baton;
2787 nf_baton->needs = SYMBOL_NEEDS_FRAME;
2788 return 1;
2791 /* Thread-local accesses require registers, but not a frame. */
2792 static CORE_ADDR
2793 symbol_needs_tls_address (void *baton, CORE_ADDR offset)
2795 struct symbol_needs_baton *nf_baton = (struct symbol_needs_baton *) baton;
2797 if (nf_baton->needs <= SYMBOL_NEEDS_REGISTERS)
2798 nf_baton->needs = SYMBOL_NEEDS_REGISTERS;
2799 return 1;
2802 /* Helper interface of per_cu_dwarf_call for
2803 dwarf2_loc_desc_get_symbol_read_needs. */
2805 static void
2806 symbol_needs_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
2808 struct symbol_needs_baton *nf_baton =
2809 (struct symbol_needs_baton *) ctx->baton;
2811 per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
2812 ctx->funcs->get_frame_pc, ctx->baton);
2815 /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */
2817 static void
2818 needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
2819 enum call_site_parameter_kind kind,
2820 union call_site_parameter_u kind_u, int deref_size)
2822 struct symbol_needs_baton *nf_baton =
2823 (struct symbol_needs_baton *) ctx->baton;
2825 nf_baton->needs = SYMBOL_NEEDS_FRAME;
2827 /* The expression may require some stub values on DWARF stack. */
2828 dwarf_expr_push_address (ctx, 0, 0);
2831 /* DW_OP_GNU_addr_index doesn't require a frame. */
2833 static CORE_ADDR
2834 needs_get_addr_index (void *baton, unsigned int index)
2836 /* Nothing to do. */
2837 return 1;
2840 /* DW_OP_push_object_address has a frame already passed through. */
2842 static CORE_ADDR
2843 needs_get_obj_addr (void *baton)
2845 /* Nothing to do. */
2846 return 1;
2849 /* Virtual method table for dwarf2_loc_desc_get_symbol_read_needs
2850 below. */
2852 static const struct dwarf_expr_context_funcs symbol_needs_ctx_funcs =
2854 symbol_needs_read_addr_from_reg,
2855 symbol_needs_get_reg_value,
2856 symbol_needs_read_mem,
2857 symbol_needs_frame_base,
2858 symbol_needs_frame_cfa,
2859 symbol_needs_frame_cfa, /* get_frame_pc */
2860 symbol_needs_tls_address,
2861 symbol_needs_dwarf_call,
2862 NULL, /* get_base_type */
2863 needs_dwarf_reg_entry_value,
2864 needs_get_addr_index,
2865 needs_get_obj_addr
2868 /* Compute the correct symbol_needs_kind value for the location
2869 expression at DATA (length SIZE). */
2871 static enum symbol_needs_kind
2872 dwarf2_loc_desc_get_symbol_read_needs (const gdb_byte *data, size_t size,
2873 struct dwarf2_per_cu_data *per_cu)
2875 struct symbol_needs_baton baton;
2876 struct dwarf_expr_context *ctx;
2877 int in_reg;
2878 struct cleanup *old_chain;
2879 struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
2881 baton.needs = SYMBOL_NEEDS_NONE;
2882 baton.per_cu = per_cu;
2884 ctx = new_dwarf_expr_context ();
2885 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
2886 make_cleanup_value_free_to_mark (value_mark ());
2888 ctx->gdbarch = get_objfile_arch (objfile);
2889 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
2890 ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu);
2891 ctx->offset = dwarf2_per_cu_text_offset (per_cu);
2892 ctx->baton = &baton;
2893 ctx->funcs = &symbol_needs_ctx_funcs;
2895 dwarf_expr_eval (ctx, data, size);
2897 in_reg = ctx->location == DWARF_VALUE_REGISTER;
2899 if (ctx->num_pieces > 0)
2901 int i;
2903 /* If the location has several pieces, and any of them are in
2904 registers, then we will need a frame to fetch them from. */
2905 for (i = 0; i < ctx->num_pieces; i++)
2906 if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
2907 in_reg = 1;
2910 do_cleanups (old_chain);
2912 if (in_reg)
2913 baton.needs = SYMBOL_NEEDS_FRAME;
2914 return baton.needs;
2917 /* A helper function that throws an unimplemented error mentioning a
2918 given DWARF operator. */
2920 static void
2921 unimplemented (unsigned int op)
2923 const char *name = get_DW_OP_name (op);
2925 if (name)
2926 error (_("DWARF operator %s cannot be translated to an agent expression"),
2927 name);
2928 else
2929 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2930 "to an agent expression"),
2931 op);
2934 /* See dwarf2loc.h.
2936 This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2937 can issue a complaint, which is better than having every target's
2938 implementation of dwarf2_reg_to_regnum do it. */
2941 dwarf_reg_to_regnum (struct gdbarch *arch, int dwarf_reg)
2943 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2945 if (reg == -1)
2947 complaint (&symfile_complaints,
2948 _("bad DWARF register number %d"), dwarf_reg);
2950 return reg;
2953 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2954 Throw an error because DWARF_REG is bad. */
2956 static void
2957 throw_bad_regnum_error (ULONGEST dwarf_reg)
2959 /* Still want to print -1 as "-1".
2960 We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2961 but that's overkill for now. */
2962 if ((int) dwarf_reg == dwarf_reg)
2963 error (_("Unable to access DWARF register number %d"), (int) dwarf_reg);
2964 error (_("Unable to access DWARF register number %s"),
2965 pulongest (dwarf_reg));
2968 /* See dwarf2loc.h. */
2971 dwarf_reg_to_regnum_or_error (struct gdbarch *arch, ULONGEST dwarf_reg)
2973 int reg;
2975 if (dwarf_reg > INT_MAX)
2976 throw_bad_regnum_error (dwarf_reg);
2977 /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2978 bad, but that's ok. */
2979 reg = dwarf_reg_to_regnum (arch, (int) dwarf_reg);
2980 if (reg == -1)
2981 throw_bad_regnum_error (dwarf_reg);
2982 return reg;
2985 /* A helper function that emits an access to memory. ARCH is the
2986 target architecture. EXPR is the expression which we are building.
2987 NBITS is the number of bits we want to read. This emits the
2988 opcodes needed to read the memory and then extract the desired
2989 bits. */
2991 static void
2992 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2994 ULONGEST nbytes = (nbits + 7) / 8;
2996 gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
2998 if (expr->tracing)
2999 ax_trace_quick (expr, nbytes);
3001 if (nbits <= 8)
3002 ax_simple (expr, aop_ref8);
3003 else if (nbits <= 16)
3004 ax_simple (expr, aop_ref16);
3005 else if (nbits <= 32)
3006 ax_simple (expr, aop_ref32);
3007 else
3008 ax_simple (expr, aop_ref64);
3010 /* If we read exactly the number of bytes we wanted, we're done. */
3011 if (8 * nbytes == nbits)
3012 return;
3014 if (gdbarch_bits_big_endian (arch))
3016 /* On a bits-big-endian machine, we want the high-order
3017 NBITS. */
3018 ax_const_l (expr, 8 * nbytes - nbits);
3019 ax_simple (expr, aop_rsh_unsigned);
3021 else
3023 /* On a bits-little-endian box, we want the low-order NBITS. */
3024 ax_zero_ext (expr, nbits);
3028 /* A helper function to return the frame's PC. */
3030 static CORE_ADDR
3031 get_ax_pc (void *baton)
3033 struct agent_expr *expr = (struct agent_expr *) baton;
3035 return expr->scope;
3038 /* Compile a DWARF location expression to an agent expression.
3040 EXPR is the agent expression we are building.
3041 LOC is the agent value we modify.
3042 ARCH is the architecture.
3043 ADDR_SIZE is the size of addresses, in bytes.
3044 OP_PTR is the start of the location expression.
3045 OP_END is one past the last byte of the location expression.
3047 This will throw an exception for various kinds of errors -- for
3048 example, if the expression cannot be compiled, or if the expression
3049 is invalid. */
3051 void
3052 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
3053 struct gdbarch *arch, unsigned int addr_size,
3054 const gdb_byte *op_ptr, const gdb_byte *op_end,
3055 struct dwarf2_per_cu_data *per_cu)
3057 struct cleanup *cleanups;
3058 int i, *offsets;
3059 VEC(int) *dw_labels = NULL, *patches = NULL;
3060 const gdb_byte * const base = op_ptr;
3061 const gdb_byte *previous_piece = op_ptr;
3062 enum bfd_endian byte_order = gdbarch_byte_order (arch);
3063 ULONGEST bits_collected = 0;
3064 unsigned int addr_size_bits = 8 * addr_size;
3065 int bits_big_endian = gdbarch_bits_big_endian (arch);
3067 offsets = XNEWVEC (int, op_end - op_ptr);
3068 cleanups = make_cleanup (xfree, offsets);
3070 for (i = 0; i < op_end - op_ptr; ++i)
3071 offsets[i] = -1;
3073 make_cleanup (VEC_cleanup (int), &dw_labels);
3074 make_cleanup (VEC_cleanup (int), &patches);
3076 /* By default we are making an address. */
3077 loc->kind = axs_lvalue_memory;
3079 while (op_ptr < op_end)
3081 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
3082 uint64_t uoffset, reg;
3083 int64_t offset;
3084 int i;
3086 offsets[op_ptr - base] = expr->len;
3087 ++op_ptr;
3089 /* Our basic approach to code generation is to map DWARF
3090 operations directly to AX operations. However, there are
3091 some differences.
3093 First, DWARF works on address-sized units, but AX always uses
3094 LONGEST. For most operations we simply ignore this
3095 difference; instead we generate sign extensions as needed
3096 before division and comparison operations. It would be nice
3097 to omit the sign extensions, but there is no way to determine
3098 the size of the target's LONGEST. (This code uses the size
3099 of the host LONGEST in some cases -- that is a bug but it is
3100 difficult to fix.)
3102 Second, some DWARF operations cannot be translated to AX.
3103 For these we simply fail. See
3104 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
3105 switch (op)
3107 case DW_OP_lit0:
3108 case DW_OP_lit1:
3109 case DW_OP_lit2:
3110 case DW_OP_lit3:
3111 case DW_OP_lit4:
3112 case DW_OP_lit5:
3113 case DW_OP_lit6:
3114 case DW_OP_lit7:
3115 case DW_OP_lit8:
3116 case DW_OP_lit9:
3117 case DW_OP_lit10:
3118 case DW_OP_lit11:
3119 case DW_OP_lit12:
3120 case DW_OP_lit13:
3121 case DW_OP_lit14:
3122 case DW_OP_lit15:
3123 case DW_OP_lit16:
3124 case DW_OP_lit17:
3125 case DW_OP_lit18:
3126 case DW_OP_lit19:
3127 case DW_OP_lit20:
3128 case DW_OP_lit21:
3129 case DW_OP_lit22:
3130 case DW_OP_lit23:
3131 case DW_OP_lit24:
3132 case DW_OP_lit25:
3133 case DW_OP_lit26:
3134 case DW_OP_lit27:
3135 case DW_OP_lit28:
3136 case DW_OP_lit29:
3137 case DW_OP_lit30:
3138 case DW_OP_lit31:
3139 ax_const_l (expr, op - DW_OP_lit0);
3140 break;
3142 case DW_OP_addr:
3143 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
3144 op_ptr += addr_size;
3145 /* Some versions of GCC emit DW_OP_addr before
3146 DW_OP_GNU_push_tls_address. In this case the value is an
3147 index, not an address. We don't support things like
3148 branching between the address and the TLS op. */
3149 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
3150 uoffset += dwarf2_per_cu_text_offset (per_cu);
3151 ax_const_l (expr, uoffset);
3152 break;
3154 case DW_OP_const1u:
3155 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
3156 op_ptr += 1;
3157 break;
3158 case DW_OP_const1s:
3159 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
3160 op_ptr += 1;
3161 break;
3162 case DW_OP_const2u:
3163 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
3164 op_ptr += 2;
3165 break;
3166 case DW_OP_const2s:
3167 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
3168 op_ptr += 2;
3169 break;
3170 case DW_OP_const4u:
3171 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
3172 op_ptr += 4;
3173 break;
3174 case DW_OP_const4s:
3175 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
3176 op_ptr += 4;
3177 break;
3178 case DW_OP_const8u:
3179 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
3180 op_ptr += 8;
3181 break;
3182 case DW_OP_const8s:
3183 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
3184 op_ptr += 8;
3185 break;
3186 case DW_OP_constu:
3187 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3188 ax_const_l (expr, uoffset);
3189 break;
3190 case DW_OP_consts:
3191 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3192 ax_const_l (expr, offset);
3193 break;
3195 case DW_OP_reg0:
3196 case DW_OP_reg1:
3197 case DW_OP_reg2:
3198 case DW_OP_reg3:
3199 case DW_OP_reg4:
3200 case DW_OP_reg5:
3201 case DW_OP_reg6:
3202 case DW_OP_reg7:
3203 case DW_OP_reg8:
3204 case DW_OP_reg9:
3205 case DW_OP_reg10:
3206 case DW_OP_reg11:
3207 case DW_OP_reg12:
3208 case DW_OP_reg13:
3209 case DW_OP_reg14:
3210 case DW_OP_reg15:
3211 case DW_OP_reg16:
3212 case DW_OP_reg17:
3213 case DW_OP_reg18:
3214 case DW_OP_reg19:
3215 case DW_OP_reg20:
3216 case DW_OP_reg21:
3217 case DW_OP_reg22:
3218 case DW_OP_reg23:
3219 case DW_OP_reg24:
3220 case DW_OP_reg25:
3221 case DW_OP_reg26:
3222 case DW_OP_reg27:
3223 case DW_OP_reg28:
3224 case DW_OP_reg29:
3225 case DW_OP_reg30:
3226 case DW_OP_reg31:
3227 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3228 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
3229 loc->kind = axs_lvalue_register;
3230 break;
3232 case DW_OP_regx:
3233 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3234 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3235 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, reg);
3236 loc->kind = axs_lvalue_register;
3237 break;
3239 case DW_OP_implicit_value:
3241 uint64_t len;
3243 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
3244 if (op_ptr + len > op_end)
3245 error (_("DW_OP_implicit_value: too few bytes available."));
3246 if (len > sizeof (ULONGEST))
3247 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3248 (int) len);
3250 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
3251 byte_order));
3252 op_ptr += len;
3253 dwarf_expr_require_composition (op_ptr, op_end,
3254 "DW_OP_implicit_value");
3256 loc->kind = axs_rvalue;
3258 break;
3260 case DW_OP_stack_value:
3261 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
3262 loc->kind = axs_rvalue;
3263 break;
3265 case DW_OP_breg0:
3266 case DW_OP_breg1:
3267 case DW_OP_breg2:
3268 case DW_OP_breg3:
3269 case DW_OP_breg4:
3270 case DW_OP_breg5:
3271 case DW_OP_breg6:
3272 case DW_OP_breg7:
3273 case DW_OP_breg8:
3274 case DW_OP_breg9:
3275 case DW_OP_breg10:
3276 case DW_OP_breg11:
3277 case DW_OP_breg12:
3278 case DW_OP_breg13:
3279 case DW_OP_breg14:
3280 case DW_OP_breg15:
3281 case DW_OP_breg16:
3282 case DW_OP_breg17:
3283 case DW_OP_breg18:
3284 case DW_OP_breg19:
3285 case DW_OP_breg20:
3286 case DW_OP_breg21:
3287 case DW_OP_breg22:
3288 case DW_OP_breg23:
3289 case DW_OP_breg24:
3290 case DW_OP_breg25:
3291 case DW_OP_breg26:
3292 case DW_OP_breg27:
3293 case DW_OP_breg28:
3294 case DW_OP_breg29:
3295 case DW_OP_breg30:
3296 case DW_OP_breg31:
3297 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3298 i = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
3299 ax_reg (expr, i);
3300 if (offset != 0)
3302 ax_const_l (expr, offset);
3303 ax_simple (expr, aop_add);
3305 break;
3306 case DW_OP_bregx:
3308 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3309 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3310 i = dwarf_reg_to_regnum_or_error (arch, reg);
3311 ax_reg (expr, i);
3312 if (offset != 0)
3314 ax_const_l (expr, offset);
3315 ax_simple (expr, aop_add);
3318 break;
3319 case DW_OP_fbreg:
3321 const gdb_byte *datastart;
3322 size_t datalen;
3323 const struct block *b;
3324 struct symbol *framefunc;
3326 b = block_for_pc (expr->scope);
3328 if (!b)
3329 error (_("No block found for address"));
3331 framefunc = block_linkage_function (b);
3333 if (!framefunc)
3334 error (_("No function found for block"));
3336 func_get_frame_base_dwarf_block (framefunc, expr->scope,
3337 &datastart, &datalen);
3339 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3340 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
3341 datastart + datalen, per_cu);
3342 if (loc->kind == axs_lvalue_register)
3343 require_rvalue (expr, loc);
3345 if (offset != 0)
3347 ax_const_l (expr, offset);
3348 ax_simple (expr, aop_add);
3351 loc->kind = axs_lvalue_memory;
3353 break;
3355 case DW_OP_dup:
3356 ax_simple (expr, aop_dup);
3357 break;
3359 case DW_OP_drop:
3360 ax_simple (expr, aop_pop);
3361 break;
3363 case DW_OP_pick:
3364 offset = *op_ptr++;
3365 ax_pick (expr, offset);
3366 break;
3368 case DW_OP_swap:
3369 ax_simple (expr, aop_swap);
3370 break;
3372 case DW_OP_over:
3373 ax_pick (expr, 1);
3374 break;
3376 case DW_OP_rot:
3377 ax_simple (expr, aop_rot);
3378 break;
3380 case DW_OP_deref:
3381 case DW_OP_deref_size:
3383 int size;
3385 if (op == DW_OP_deref_size)
3386 size = *op_ptr++;
3387 else
3388 size = addr_size;
3390 if (size != 1 && size != 2 && size != 4 && size != 8)
3391 error (_("Unsupported size %d in %s"),
3392 size, get_DW_OP_name (op));
3393 access_memory (arch, expr, size * TARGET_CHAR_BIT);
3395 break;
3397 case DW_OP_abs:
3398 /* Sign extend the operand. */
3399 ax_ext (expr, addr_size_bits);
3400 ax_simple (expr, aop_dup);
3401 ax_const_l (expr, 0);
3402 ax_simple (expr, aop_less_signed);
3403 ax_simple (expr, aop_log_not);
3404 i = ax_goto (expr, aop_if_goto);
3405 /* We have to emit 0 - X. */
3406 ax_const_l (expr, 0);
3407 ax_simple (expr, aop_swap);
3408 ax_simple (expr, aop_sub);
3409 ax_label (expr, i, expr->len);
3410 break;
3412 case DW_OP_neg:
3413 /* No need to sign extend here. */
3414 ax_const_l (expr, 0);
3415 ax_simple (expr, aop_swap);
3416 ax_simple (expr, aop_sub);
3417 break;
3419 case DW_OP_not:
3420 /* Sign extend the operand. */
3421 ax_ext (expr, addr_size_bits);
3422 ax_simple (expr, aop_bit_not);
3423 break;
3425 case DW_OP_plus_uconst:
3426 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3427 /* It would be really weird to emit `DW_OP_plus_uconst 0',
3428 but we micro-optimize anyhow. */
3429 if (reg != 0)
3431 ax_const_l (expr, reg);
3432 ax_simple (expr, aop_add);
3434 break;
3436 case DW_OP_and:
3437 ax_simple (expr, aop_bit_and);
3438 break;
3440 case DW_OP_div:
3441 /* Sign extend the operands. */
3442 ax_ext (expr, addr_size_bits);
3443 ax_simple (expr, aop_swap);
3444 ax_ext (expr, addr_size_bits);
3445 ax_simple (expr, aop_swap);
3446 ax_simple (expr, aop_div_signed);
3447 break;
3449 case DW_OP_minus:
3450 ax_simple (expr, aop_sub);
3451 break;
3453 case DW_OP_mod:
3454 ax_simple (expr, aop_rem_unsigned);
3455 break;
3457 case DW_OP_mul:
3458 ax_simple (expr, aop_mul);
3459 break;
3461 case DW_OP_or:
3462 ax_simple (expr, aop_bit_or);
3463 break;
3465 case DW_OP_plus:
3466 ax_simple (expr, aop_add);
3467 break;
3469 case DW_OP_shl:
3470 ax_simple (expr, aop_lsh);
3471 break;
3473 case DW_OP_shr:
3474 ax_simple (expr, aop_rsh_unsigned);
3475 break;
3477 case DW_OP_shra:
3478 ax_simple (expr, aop_rsh_signed);
3479 break;
3481 case DW_OP_xor:
3482 ax_simple (expr, aop_bit_xor);
3483 break;
3485 case DW_OP_le:
3486 /* Sign extend the operands. */
3487 ax_ext (expr, addr_size_bits);
3488 ax_simple (expr, aop_swap);
3489 ax_ext (expr, addr_size_bits);
3490 /* Note no swap here: A <= B is !(B < A). */
3491 ax_simple (expr, aop_less_signed);
3492 ax_simple (expr, aop_log_not);
3493 break;
3495 case DW_OP_ge:
3496 /* Sign extend the operands. */
3497 ax_ext (expr, addr_size_bits);
3498 ax_simple (expr, aop_swap);
3499 ax_ext (expr, addr_size_bits);
3500 ax_simple (expr, aop_swap);
3501 /* A >= B is !(A < B). */
3502 ax_simple (expr, aop_less_signed);
3503 ax_simple (expr, aop_log_not);
3504 break;
3506 case DW_OP_eq:
3507 /* Sign extend the operands. */
3508 ax_ext (expr, addr_size_bits);
3509 ax_simple (expr, aop_swap);
3510 ax_ext (expr, addr_size_bits);
3511 /* No need for a second swap here. */
3512 ax_simple (expr, aop_equal);
3513 break;
3515 case DW_OP_lt:
3516 /* Sign extend the operands. */
3517 ax_ext (expr, addr_size_bits);
3518 ax_simple (expr, aop_swap);
3519 ax_ext (expr, addr_size_bits);
3520 ax_simple (expr, aop_swap);
3521 ax_simple (expr, aop_less_signed);
3522 break;
3524 case DW_OP_gt:
3525 /* Sign extend the operands. */
3526 ax_ext (expr, addr_size_bits);
3527 ax_simple (expr, aop_swap);
3528 ax_ext (expr, addr_size_bits);
3529 /* Note no swap here: A > B is B < A. */
3530 ax_simple (expr, aop_less_signed);
3531 break;
3533 case DW_OP_ne:
3534 /* Sign extend the operands. */
3535 ax_ext (expr, addr_size_bits);
3536 ax_simple (expr, aop_swap);
3537 ax_ext (expr, addr_size_bits);
3538 /* No need for a swap here. */
3539 ax_simple (expr, aop_equal);
3540 ax_simple (expr, aop_log_not);
3541 break;
3543 case DW_OP_call_frame_cfa:
3545 int regnum;
3546 CORE_ADDR text_offset;
3547 LONGEST off;
3548 const gdb_byte *cfa_start, *cfa_end;
3550 if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
3551 &regnum, &off,
3552 &text_offset, &cfa_start, &cfa_end))
3554 /* Register. */
3555 ax_reg (expr, regnum);
3556 if (off != 0)
3558 ax_const_l (expr, off);
3559 ax_simple (expr, aop_add);
3562 else
3564 /* Another expression. */
3565 ax_const_l (expr, text_offset);
3566 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3567 cfa_start, cfa_end, per_cu);
3570 loc->kind = axs_lvalue_memory;
3572 break;
3574 case DW_OP_GNU_push_tls_address:
3575 case DW_OP_form_tls_address:
3576 unimplemented (op);
3577 break;
3579 case DW_OP_push_object_address:
3580 unimplemented (op);
3581 break;
3583 case DW_OP_skip:
3584 offset = extract_signed_integer (op_ptr, 2, byte_order);
3585 op_ptr += 2;
3586 i = ax_goto (expr, aop_goto);
3587 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3588 VEC_safe_push (int, patches, i);
3589 break;
3591 case DW_OP_bra:
3592 offset = extract_signed_integer (op_ptr, 2, byte_order);
3593 op_ptr += 2;
3594 /* Zero extend the operand. */
3595 ax_zero_ext (expr, addr_size_bits);
3596 i = ax_goto (expr, aop_if_goto);
3597 VEC_safe_push (int, dw_labels, op_ptr + offset - base);
3598 VEC_safe_push (int, patches, i);
3599 break;
3601 case DW_OP_nop:
3602 break;
3604 case DW_OP_piece:
3605 case DW_OP_bit_piece:
3607 uint64_t size, offset;
3609 if (op_ptr - 1 == previous_piece)
3610 error (_("Cannot translate empty pieces to agent expressions"));
3611 previous_piece = op_ptr - 1;
3613 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3614 if (op == DW_OP_piece)
3616 size *= 8;
3617 offset = 0;
3619 else
3620 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
3622 if (bits_collected + size > 8 * sizeof (LONGEST))
3623 error (_("Expression pieces exceed word size"));
3625 /* Access the bits. */
3626 switch (loc->kind)
3628 case axs_lvalue_register:
3629 ax_reg (expr, loc->u.reg);
3630 break;
3632 case axs_lvalue_memory:
3633 /* Offset the pointer, if needed. */
3634 if (offset > 8)
3636 ax_const_l (expr, offset / 8);
3637 ax_simple (expr, aop_add);
3638 offset %= 8;
3640 access_memory (arch, expr, size);
3641 break;
3644 /* For a bits-big-endian target, shift up what we already
3645 have. For a bits-little-endian target, shift up the
3646 new data. Note that there is a potential bug here if
3647 the DWARF expression leaves multiple values on the
3648 stack. */
3649 if (bits_collected > 0)
3651 if (bits_big_endian)
3653 ax_simple (expr, aop_swap);
3654 ax_const_l (expr, size);
3655 ax_simple (expr, aop_lsh);
3656 /* We don't need a second swap here, because
3657 aop_bit_or is symmetric. */
3659 else
3661 ax_const_l (expr, size);
3662 ax_simple (expr, aop_lsh);
3664 ax_simple (expr, aop_bit_or);
3667 bits_collected += size;
3668 loc->kind = axs_rvalue;
3670 break;
3672 case DW_OP_GNU_uninit:
3673 unimplemented (op);
3675 case DW_OP_call2:
3676 case DW_OP_call4:
3678 struct dwarf2_locexpr_baton block;
3679 int size = (op == DW_OP_call2 ? 2 : 4);
3680 cu_offset offset;
3682 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3683 op_ptr += size;
3685 offset.cu_off = uoffset;
3686 block = dwarf2_fetch_die_loc_cu_off (offset, per_cu,
3687 get_ax_pc, expr);
3689 /* DW_OP_call_ref is currently not supported. */
3690 gdb_assert (block.per_cu == per_cu);
3692 dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
3693 block.data, block.data + block.size,
3694 per_cu);
3696 break;
3698 case DW_OP_call_ref:
3699 unimplemented (op);
3701 default:
3702 unimplemented (op);
3706 /* Patch all the branches we emitted. */
3707 for (i = 0; i < VEC_length (int, patches); ++i)
3709 int targ = offsets[VEC_index (int, dw_labels, i)];
3710 if (targ == -1)
3711 internal_error (__FILE__, __LINE__, _("invalid label"));
3712 ax_label (expr, VEC_index (int, patches, i), targ);
3715 do_cleanups (cleanups);
3719 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3720 evaluator to calculate the location. */
3721 static struct value *
3722 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3724 struct dwarf2_locexpr_baton *dlbaton
3725 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3726 struct value *val;
3728 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3729 dlbaton->size, dlbaton->per_cu);
3731 return val;
3734 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3735 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3736 will be thrown. */
3738 static struct value *
3739 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3741 struct dwarf2_locexpr_baton *dlbaton
3742 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3744 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3745 dlbaton->size);
3748 /* Implementation of get_symbol_read_needs from
3749 symbol_computed_ops. */
3751 static enum symbol_needs_kind
3752 locexpr_get_symbol_read_needs (struct symbol *symbol)
3754 struct dwarf2_locexpr_baton *dlbaton
3755 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3757 return dwarf2_loc_desc_get_symbol_read_needs (dlbaton->data, dlbaton->size,
3758 dlbaton->per_cu);
3761 /* Return true if DATA points to the end of a piece. END is one past
3762 the last byte in the expression. */
3764 static int
3765 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3767 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3770 /* Helper for locexpr_describe_location_piece that finds the name of a
3771 DWARF register. */
3773 static const char *
3774 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3776 int regnum;
3778 /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3779 We'd rather print *something* here than throw an error. */
3780 regnum = dwarf_reg_to_regnum (gdbarch, dwarf_regnum);
3781 /* gdbarch_register_name may just return "", return something more
3782 descriptive for bad register numbers. */
3783 if (regnum == -1)
3785 /* The text is output as "$bad_register_number".
3786 That is why we use the underscores. */
3787 return _("bad_register_number");
3789 return gdbarch_register_name (gdbarch, regnum);
3792 /* Nicely describe a single piece of a location, returning an updated
3793 position in the bytecode sequence. This function cannot recognize
3794 all locations; if a location is not recognized, it simply returns
3795 DATA. If there is an error during reading, e.g. we run off the end
3796 of the buffer, an error is thrown. */
3798 static const gdb_byte *
3799 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3800 CORE_ADDR addr, struct objfile *objfile,
3801 struct dwarf2_per_cu_data *per_cu,
3802 const gdb_byte *data, const gdb_byte *end,
3803 unsigned int addr_size)
3805 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3806 size_t leb128_size;
3808 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3810 fprintf_filtered (stream, _("a variable in $%s"),
3811 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3812 data += 1;
3814 else if (data[0] == DW_OP_regx)
3816 uint64_t reg;
3818 data = safe_read_uleb128 (data + 1, end, &reg);
3819 fprintf_filtered (stream, _("a variable in $%s"),
3820 locexpr_regname (gdbarch, reg));
3822 else if (data[0] == DW_OP_fbreg)
3824 const struct block *b;
3825 struct symbol *framefunc;
3826 int frame_reg = 0;
3827 int64_t frame_offset;
3828 const gdb_byte *base_data, *new_data, *save_data = data;
3829 size_t base_size;
3830 int64_t base_offset = 0;
3832 new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3833 if (!piece_end_p (new_data, end))
3834 return data;
3835 data = new_data;
3837 b = block_for_pc (addr);
3839 if (!b)
3840 error (_("No block found for address for symbol \"%s\"."),
3841 SYMBOL_PRINT_NAME (symbol));
3843 framefunc = block_linkage_function (b);
3845 if (!framefunc)
3846 error (_("No function found for block for symbol \"%s\"."),
3847 SYMBOL_PRINT_NAME (symbol));
3849 func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
3851 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3853 const gdb_byte *buf_end;
3855 frame_reg = base_data[0] - DW_OP_breg0;
3856 buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3857 &base_offset);
3858 if (buf_end != base_data + base_size)
3859 error (_("Unexpected opcode after "
3860 "DW_OP_breg%u for symbol \"%s\"."),
3861 frame_reg, SYMBOL_PRINT_NAME (symbol));
3863 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3865 /* The frame base is just the register, with no offset. */
3866 frame_reg = base_data[0] - DW_OP_reg0;
3867 base_offset = 0;
3869 else
3871 /* We don't know what to do with the frame base expression,
3872 so we can't trace this variable; give up. */
3873 return save_data;
3876 fprintf_filtered (stream,
3877 _("a variable at frame base reg $%s offset %s+%s"),
3878 locexpr_regname (gdbarch, frame_reg),
3879 plongest (base_offset), plongest (frame_offset));
3881 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3882 && piece_end_p (data, end))
3884 int64_t offset;
3886 data = safe_read_sleb128 (data + 1, end, &offset);
3888 fprintf_filtered (stream,
3889 _("a variable at offset %s from base reg $%s"),
3890 plongest (offset),
3891 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3894 /* The location expression for a TLS variable looks like this (on a
3895 64-bit LE machine):
3897 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3898 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3900 0x3 is the encoding for DW_OP_addr, which has an operand as long
3901 as the size of an address on the target machine (here is 8
3902 bytes). Note that more recent version of GCC emit DW_OP_const4u
3903 or DW_OP_const8u, depending on address size, rather than
3904 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3905 The operand represents the offset at which the variable is within
3906 the thread local storage. */
3908 else if (data + 1 + addr_size < end
3909 && (data[0] == DW_OP_addr
3910 || (addr_size == 4 && data[0] == DW_OP_const4u)
3911 || (addr_size == 8 && data[0] == DW_OP_const8u))
3912 && (data[1 + addr_size] == DW_OP_GNU_push_tls_address
3913 || data[1 + addr_size] == DW_OP_form_tls_address)
3914 && piece_end_p (data + 2 + addr_size, end))
3916 ULONGEST offset;
3917 offset = extract_unsigned_integer (data + 1, addr_size,
3918 gdbarch_byte_order (gdbarch));
3920 fprintf_filtered (stream,
3921 _("a thread-local variable at offset 0x%s "
3922 "in the thread-local storage for `%s'"),
3923 phex_nz (offset, addr_size), objfile_name (objfile));
3925 data += 1 + addr_size + 1;
3928 /* With -gsplit-dwarf a TLS variable can also look like this:
3929 DW_AT_location : 3 byte block: fc 4 e0
3930 (DW_OP_GNU_const_index: 4;
3931 DW_OP_GNU_push_tls_address) */
3932 else if (data + 3 <= end
3933 && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3934 && data[0] == DW_OP_GNU_const_index
3935 && leb128_size > 0
3936 && (data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3937 || data[1 + leb128_size] == DW_OP_form_tls_address)
3938 && piece_end_p (data + 2 + leb128_size, end))
3940 uint64_t offset;
3942 data = safe_read_uleb128 (data + 1, end, &offset);
3943 offset = dwarf2_read_addr_index (per_cu, offset);
3944 fprintf_filtered (stream,
3945 _("a thread-local variable at offset 0x%s "
3946 "in the thread-local storage for `%s'"),
3947 phex_nz (offset, addr_size), objfile_name (objfile));
3948 ++data;
3951 else if (data[0] >= DW_OP_lit0
3952 && data[0] <= DW_OP_lit31
3953 && data + 1 < end
3954 && data[1] == DW_OP_stack_value)
3956 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3957 data += 2;
3960 return data;
3963 /* Disassemble an expression, stopping at the end of a piece or at the
3964 end of the expression. Returns a pointer to the next unread byte
3965 in the input expression. If ALL is nonzero, then this function
3966 will keep going until it reaches the end of the expression.
3967 If there is an error during reading, e.g. we run off the end
3968 of the buffer, an error is thrown. */
3970 static const gdb_byte *
3971 disassemble_dwarf_expression (struct ui_file *stream,
3972 struct gdbarch *arch, unsigned int addr_size,
3973 int offset_size, const gdb_byte *start,
3974 const gdb_byte *data, const gdb_byte *end,
3975 int indent, int all,
3976 struct dwarf2_per_cu_data *per_cu)
3978 while (data < end
3979 && (all
3980 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3982 enum dwarf_location_atom op = (enum dwarf_location_atom) *data++;
3983 uint64_t ul;
3984 int64_t l;
3985 const char *name;
3987 name = get_DW_OP_name (op);
3989 if (!name)
3990 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3991 op, (long) (data - 1 - start));
3992 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3993 (long) (data - 1 - start), name);
3995 switch (op)
3997 case DW_OP_addr:
3998 ul = extract_unsigned_integer (data, addr_size,
3999 gdbarch_byte_order (arch));
4000 data += addr_size;
4001 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
4002 break;
4004 case DW_OP_const1u:
4005 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
4006 data += 1;
4007 fprintf_filtered (stream, " %s", pulongest (ul));
4008 break;
4009 case DW_OP_const1s:
4010 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
4011 data += 1;
4012 fprintf_filtered (stream, " %s", plongest (l));
4013 break;
4014 case DW_OP_const2u:
4015 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
4016 data += 2;
4017 fprintf_filtered (stream, " %s", pulongest (ul));
4018 break;
4019 case DW_OP_const2s:
4020 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4021 data += 2;
4022 fprintf_filtered (stream, " %s", plongest (l));
4023 break;
4024 case DW_OP_const4u:
4025 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4026 data += 4;
4027 fprintf_filtered (stream, " %s", pulongest (ul));
4028 break;
4029 case DW_OP_const4s:
4030 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
4031 data += 4;
4032 fprintf_filtered (stream, " %s", plongest (l));
4033 break;
4034 case DW_OP_const8u:
4035 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
4036 data += 8;
4037 fprintf_filtered (stream, " %s", pulongest (ul));
4038 break;
4039 case DW_OP_const8s:
4040 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
4041 data += 8;
4042 fprintf_filtered (stream, " %s", plongest (l));
4043 break;
4044 case DW_OP_constu:
4045 data = safe_read_uleb128 (data, end, &ul);
4046 fprintf_filtered (stream, " %s", pulongest (ul));
4047 break;
4048 case DW_OP_consts:
4049 data = safe_read_sleb128 (data, end, &l);
4050 fprintf_filtered (stream, " %s", plongest (l));
4051 break;
4053 case DW_OP_reg0:
4054 case DW_OP_reg1:
4055 case DW_OP_reg2:
4056 case DW_OP_reg3:
4057 case DW_OP_reg4:
4058 case DW_OP_reg5:
4059 case DW_OP_reg6:
4060 case DW_OP_reg7:
4061 case DW_OP_reg8:
4062 case DW_OP_reg9:
4063 case DW_OP_reg10:
4064 case DW_OP_reg11:
4065 case DW_OP_reg12:
4066 case DW_OP_reg13:
4067 case DW_OP_reg14:
4068 case DW_OP_reg15:
4069 case DW_OP_reg16:
4070 case DW_OP_reg17:
4071 case DW_OP_reg18:
4072 case DW_OP_reg19:
4073 case DW_OP_reg20:
4074 case DW_OP_reg21:
4075 case DW_OP_reg22:
4076 case DW_OP_reg23:
4077 case DW_OP_reg24:
4078 case DW_OP_reg25:
4079 case DW_OP_reg26:
4080 case DW_OP_reg27:
4081 case DW_OP_reg28:
4082 case DW_OP_reg29:
4083 case DW_OP_reg30:
4084 case DW_OP_reg31:
4085 fprintf_filtered (stream, " [$%s]",
4086 locexpr_regname (arch, op - DW_OP_reg0));
4087 break;
4089 case DW_OP_regx:
4090 data = safe_read_uleb128 (data, end, &ul);
4091 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
4092 locexpr_regname (arch, (int) ul));
4093 break;
4095 case DW_OP_implicit_value:
4096 data = safe_read_uleb128 (data, end, &ul);
4097 data += ul;
4098 fprintf_filtered (stream, " %s", pulongest (ul));
4099 break;
4101 case DW_OP_breg0:
4102 case DW_OP_breg1:
4103 case DW_OP_breg2:
4104 case DW_OP_breg3:
4105 case DW_OP_breg4:
4106 case DW_OP_breg5:
4107 case DW_OP_breg6:
4108 case DW_OP_breg7:
4109 case DW_OP_breg8:
4110 case DW_OP_breg9:
4111 case DW_OP_breg10:
4112 case DW_OP_breg11:
4113 case DW_OP_breg12:
4114 case DW_OP_breg13:
4115 case DW_OP_breg14:
4116 case DW_OP_breg15:
4117 case DW_OP_breg16:
4118 case DW_OP_breg17:
4119 case DW_OP_breg18:
4120 case DW_OP_breg19:
4121 case DW_OP_breg20:
4122 case DW_OP_breg21:
4123 case DW_OP_breg22:
4124 case DW_OP_breg23:
4125 case DW_OP_breg24:
4126 case DW_OP_breg25:
4127 case DW_OP_breg26:
4128 case DW_OP_breg27:
4129 case DW_OP_breg28:
4130 case DW_OP_breg29:
4131 case DW_OP_breg30:
4132 case DW_OP_breg31:
4133 data = safe_read_sleb128 (data, end, &l);
4134 fprintf_filtered (stream, " %s [$%s]", plongest (l),
4135 locexpr_regname (arch, op - DW_OP_breg0));
4136 break;
4138 case DW_OP_bregx:
4139 data = safe_read_uleb128 (data, end, &ul);
4140 data = safe_read_sleb128 (data, end, &l);
4141 fprintf_filtered (stream, " register %s [$%s] offset %s",
4142 pulongest (ul),
4143 locexpr_regname (arch, (int) ul),
4144 plongest (l));
4145 break;
4147 case DW_OP_fbreg:
4148 data = safe_read_sleb128 (data, end, &l);
4149 fprintf_filtered (stream, " %s", plongest (l));
4150 break;
4152 case DW_OP_xderef_size:
4153 case DW_OP_deref_size:
4154 case DW_OP_pick:
4155 fprintf_filtered (stream, " %d", *data);
4156 ++data;
4157 break;
4159 case DW_OP_plus_uconst:
4160 data = safe_read_uleb128 (data, end, &ul);
4161 fprintf_filtered (stream, " %s", pulongest (ul));
4162 break;
4164 case DW_OP_skip:
4165 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4166 data += 2;
4167 fprintf_filtered (stream, " to %ld",
4168 (long) (data + l - start));
4169 break;
4171 case DW_OP_bra:
4172 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4173 data += 2;
4174 fprintf_filtered (stream, " %ld",
4175 (long) (data + l - start));
4176 break;
4178 case DW_OP_call2:
4179 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
4180 data += 2;
4181 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
4182 break;
4184 case DW_OP_call4:
4185 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4186 data += 4;
4187 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4188 break;
4190 case DW_OP_call_ref:
4191 ul = extract_unsigned_integer (data, offset_size,
4192 gdbarch_byte_order (arch));
4193 data += offset_size;
4194 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4195 break;
4197 case DW_OP_piece:
4198 data = safe_read_uleb128 (data, end, &ul);
4199 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
4200 break;
4202 case DW_OP_bit_piece:
4204 uint64_t offset;
4206 data = safe_read_uleb128 (data, end, &ul);
4207 data = safe_read_uleb128 (data, end, &offset);
4208 fprintf_filtered (stream, " size %s offset %s (bits)",
4209 pulongest (ul), pulongest (offset));
4211 break;
4213 case DW_OP_GNU_implicit_pointer:
4215 ul = extract_unsigned_integer (data, offset_size,
4216 gdbarch_byte_order (arch));
4217 data += offset_size;
4219 data = safe_read_sleb128 (data, end, &l);
4221 fprintf_filtered (stream, " DIE %s offset %s",
4222 phex_nz (ul, offset_size),
4223 plongest (l));
4225 break;
4227 case DW_OP_GNU_deref_type:
4229 int addr_size = *data++;
4230 cu_offset offset;
4231 struct type *type;
4233 data = safe_read_uleb128 (data, end, &ul);
4234 offset.cu_off = ul;
4235 type = dwarf2_get_die_type (offset, per_cu);
4236 fprintf_filtered (stream, "<");
4237 type_print (type, "", stream, -1);
4238 fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0),
4239 addr_size);
4241 break;
4243 case DW_OP_GNU_const_type:
4245 cu_offset type_die;
4246 struct type *type;
4248 data = safe_read_uleb128 (data, end, &ul);
4249 type_die.cu_off = ul;
4250 type = dwarf2_get_die_type (type_die, per_cu);
4251 fprintf_filtered (stream, "<");
4252 type_print (type, "", stream, -1);
4253 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
4255 break;
4257 case DW_OP_GNU_regval_type:
4259 uint64_t reg;
4260 cu_offset type_die;
4261 struct type *type;
4263 data = safe_read_uleb128 (data, end, &reg);
4264 data = safe_read_uleb128 (data, end, &ul);
4265 type_die.cu_off = ul;
4267 type = dwarf2_get_die_type (type_die, per_cu);
4268 fprintf_filtered (stream, "<");
4269 type_print (type, "", stream, -1);
4270 fprintf_filtered (stream, " [0x%s]> [$%s]",
4271 phex_nz (type_die.cu_off, 0),
4272 locexpr_regname (arch, reg));
4274 break;
4276 case DW_OP_GNU_convert:
4277 case DW_OP_GNU_reinterpret:
4279 cu_offset type_die;
4281 data = safe_read_uleb128 (data, end, &ul);
4282 type_die.cu_off = ul;
4284 if (type_die.cu_off == 0)
4285 fprintf_filtered (stream, "<0>");
4286 else
4288 struct type *type;
4290 type = dwarf2_get_die_type (type_die, per_cu);
4291 fprintf_filtered (stream, "<");
4292 type_print (type, "", stream, -1);
4293 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0));
4296 break;
4298 case DW_OP_GNU_entry_value:
4299 data = safe_read_uleb128 (data, end, &ul);
4300 fputc_filtered ('\n', stream);
4301 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
4302 start, data, data + ul, indent + 2,
4303 all, per_cu);
4304 data += ul;
4305 continue;
4307 case DW_OP_GNU_parameter_ref:
4308 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4309 data += 4;
4310 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4311 break;
4313 case DW_OP_GNU_addr_index:
4314 data = safe_read_uleb128 (data, end, &ul);
4315 ul = dwarf2_read_addr_index (per_cu, ul);
4316 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
4317 break;
4318 case DW_OP_GNU_const_index:
4319 data = safe_read_uleb128 (data, end, &ul);
4320 ul = dwarf2_read_addr_index (per_cu, ul);
4321 fprintf_filtered (stream, " %s", pulongest (ul));
4322 break;
4325 fprintf_filtered (stream, "\n");
4328 return data;
4331 /* Describe a single location, which may in turn consist of multiple
4332 pieces. */
4334 static void
4335 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
4336 struct ui_file *stream,
4337 const gdb_byte *data, size_t size,
4338 struct objfile *objfile, unsigned int addr_size,
4339 int offset_size, struct dwarf2_per_cu_data *per_cu)
4341 const gdb_byte *end = data + size;
4342 int first_piece = 1, bad = 0;
4344 while (data < end)
4346 const gdb_byte *here = data;
4347 int disassemble = 1;
4349 if (first_piece)
4350 first_piece = 0;
4351 else
4352 fprintf_filtered (stream, _(", and "));
4354 if (!dwarf_always_disassemble)
4356 data = locexpr_describe_location_piece (symbol, stream,
4357 addr, objfile, per_cu,
4358 data, end, addr_size);
4359 /* If we printed anything, or if we have an empty piece,
4360 then don't disassemble. */
4361 if (data != here
4362 || data[0] == DW_OP_piece
4363 || data[0] == DW_OP_bit_piece)
4364 disassemble = 0;
4366 if (disassemble)
4368 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
4369 data = disassemble_dwarf_expression (stream,
4370 get_objfile_arch (objfile),
4371 addr_size, offset_size, data,
4372 data, end, 0,
4373 dwarf_always_disassemble,
4374 per_cu);
4377 if (data < end)
4379 int empty = data == here;
4381 if (disassemble)
4382 fprintf_filtered (stream, " ");
4383 if (data[0] == DW_OP_piece)
4385 uint64_t bytes;
4387 data = safe_read_uleb128 (data + 1, end, &bytes);
4389 if (empty)
4390 fprintf_filtered (stream, _("an empty %s-byte piece"),
4391 pulongest (bytes));
4392 else
4393 fprintf_filtered (stream, _(" [%s-byte piece]"),
4394 pulongest (bytes));
4396 else if (data[0] == DW_OP_bit_piece)
4398 uint64_t bits, offset;
4400 data = safe_read_uleb128 (data + 1, end, &bits);
4401 data = safe_read_uleb128 (data, end, &offset);
4403 if (empty)
4404 fprintf_filtered (stream,
4405 _("an empty %s-bit piece"),
4406 pulongest (bits));
4407 else
4408 fprintf_filtered (stream,
4409 _(" [%s-bit piece, offset %s bits]"),
4410 pulongest (bits), pulongest (offset));
4412 else
4414 bad = 1;
4415 break;
4420 if (bad || data > end)
4421 error (_("Corrupted DWARF2 expression for \"%s\"."),
4422 SYMBOL_PRINT_NAME (symbol));
4425 /* Print a natural-language description of SYMBOL to STREAM. This
4426 version is for a symbol with a single location. */
4428 static void
4429 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
4430 struct ui_file *stream)
4432 struct dwarf2_locexpr_baton *dlbaton
4433 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4434 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4435 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4436 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4438 locexpr_describe_location_1 (symbol, addr, stream,
4439 dlbaton->data, dlbaton->size,
4440 objfile, addr_size, offset_size,
4441 dlbaton->per_cu);
4444 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4445 any necessary bytecode in AX. */
4447 static void
4448 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4449 struct agent_expr *ax, struct axs_value *value)
4451 struct dwarf2_locexpr_baton *dlbaton
4452 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4453 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4455 if (dlbaton->size == 0)
4456 value->optimized_out = 1;
4457 else
4458 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
4459 dlbaton->data, dlbaton->data + dlbaton->size,
4460 dlbaton->per_cu);
4463 /* symbol_computed_ops 'generate_c_location' method. */
4465 static void
4466 locexpr_generate_c_location (struct symbol *sym, struct ui_file *stream,
4467 struct gdbarch *gdbarch,
4468 unsigned char *registers_used,
4469 CORE_ADDR pc, const char *result_name)
4471 struct dwarf2_locexpr_baton *dlbaton
4472 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
4473 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4475 if (dlbaton->size == 0)
4476 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4478 compile_dwarf_expr_to_c (stream, result_name,
4479 sym, pc, gdbarch, registers_used, addr_size,
4480 dlbaton->data, dlbaton->data + dlbaton->size,
4481 dlbaton->per_cu);
4484 /* The set of location functions used with the DWARF-2 expression
4485 evaluator. */
4486 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4487 locexpr_read_variable,
4488 locexpr_read_variable_at_entry,
4489 locexpr_get_symbol_read_needs,
4490 locexpr_describe_location,
4491 0, /* location_has_loclist */
4492 locexpr_tracepoint_var_ref,
4493 locexpr_generate_c_location
4497 /* Wrapper functions for location lists. These generally find
4498 the appropriate location expression and call something above. */
4500 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4501 evaluator to calculate the location. */
4502 static struct value *
4503 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4505 struct dwarf2_loclist_baton *dlbaton
4506 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4507 struct value *val;
4508 const gdb_byte *data;
4509 size_t size;
4510 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
4512 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4513 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4514 dlbaton->per_cu);
4516 return val;
4519 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4520 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4521 will be thrown.
4523 Function always returns non-NULL value, it may be marked optimized out if
4524 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
4525 if it cannot resolve the parameter for any reason. */
4527 static struct value *
4528 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4530 struct dwarf2_loclist_baton *dlbaton
4531 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4532 const gdb_byte *data;
4533 size_t size;
4534 CORE_ADDR pc;
4536 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4537 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4539 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4540 if (data == NULL)
4541 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4543 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4546 /* Implementation of get_symbol_read_needs from
4547 symbol_computed_ops. */
4549 static enum symbol_needs_kind
4550 loclist_symbol_needs (struct symbol *symbol)
4552 /* If there's a location list, then assume we need to have a frame
4553 to choose the appropriate location expression. With tracking of
4554 global variables this is not necessarily true, but such tracking
4555 is disabled in GCC at the moment until we figure out how to
4556 represent it. */
4558 return SYMBOL_NEEDS_FRAME;
4561 /* Print a natural-language description of SYMBOL to STREAM. This
4562 version applies when there is a list of different locations, each
4563 with a specified address range. */
4565 static void
4566 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4567 struct ui_file *stream)
4569 struct dwarf2_loclist_baton *dlbaton
4570 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4571 const gdb_byte *loc_ptr, *buf_end;
4572 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
4573 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4574 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4575 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4576 int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
4577 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4578 /* Adjust base_address for relocatable objects. */
4579 CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
4580 CORE_ADDR base_address = dlbaton->base_address + base_offset;
4581 int done = 0;
4583 loc_ptr = dlbaton->data;
4584 buf_end = dlbaton->data + dlbaton->size;
4586 fprintf_filtered (stream, _("multi-location:\n"));
4588 /* Iterate through locations until we run out. */
4589 while (!done)
4591 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4592 int length;
4593 enum debug_loc_kind kind;
4594 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4596 if (dlbaton->from_dwo)
4597 kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4598 loc_ptr, buf_end, &new_ptr,
4599 &low, &high, byte_order);
4600 else
4601 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4602 &low, &high,
4603 byte_order, addr_size,
4604 signed_addr_p);
4605 loc_ptr = new_ptr;
4606 switch (kind)
4608 case DEBUG_LOC_END_OF_LIST:
4609 done = 1;
4610 continue;
4611 case DEBUG_LOC_BASE_ADDRESS:
4612 base_address = high + base_offset;
4613 fprintf_filtered (stream, _(" Base address %s"),
4614 paddress (gdbarch, base_address));
4615 continue;
4616 case DEBUG_LOC_START_END:
4617 case DEBUG_LOC_START_LENGTH:
4618 break;
4619 case DEBUG_LOC_BUFFER_OVERFLOW:
4620 case DEBUG_LOC_INVALID_ENTRY:
4621 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4622 SYMBOL_PRINT_NAME (symbol));
4623 default:
4624 gdb_assert_not_reached ("bad debug_loc_kind");
4627 /* Otherwise, a location expression entry. */
4628 low += base_address;
4629 high += base_address;
4631 low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
4632 high = gdbarch_adjust_dwarf2_addr (gdbarch, high);
4634 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4635 loc_ptr += 2;
4637 /* (It would improve readability to print only the minimum
4638 necessary digits of the second number of the range.) */
4639 fprintf_filtered (stream, _(" Range %s-%s: "),
4640 paddress (gdbarch, low), paddress (gdbarch, high));
4642 /* Now describe this particular location. */
4643 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4644 objfile, addr_size, offset_size,
4645 dlbaton->per_cu);
4647 fprintf_filtered (stream, "\n");
4649 loc_ptr += length;
4653 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4654 any necessary bytecode in AX. */
4655 static void
4656 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
4657 struct agent_expr *ax, struct axs_value *value)
4659 struct dwarf2_loclist_baton *dlbaton
4660 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4661 const gdb_byte *data;
4662 size_t size;
4663 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4665 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4666 if (size == 0)
4667 value->optimized_out = 1;
4668 else
4669 dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
4670 dlbaton->per_cu);
4673 /* symbol_computed_ops 'generate_c_location' method. */
4675 static void
4676 loclist_generate_c_location (struct symbol *sym, struct ui_file *stream,
4677 struct gdbarch *gdbarch,
4678 unsigned char *registers_used,
4679 CORE_ADDR pc, const char *result_name)
4681 struct dwarf2_loclist_baton *dlbaton
4682 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
4683 unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
4684 const gdb_byte *data;
4685 size_t size;
4687 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4688 if (size == 0)
4689 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym));
4691 compile_dwarf_expr_to_c (stream, result_name,
4692 sym, pc, gdbarch, registers_used, addr_size,
4693 data, data + size,
4694 dlbaton->per_cu);
4697 /* The set of location functions used with the DWARF-2 expression
4698 evaluator and location lists. */
4699 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4700 loclist_read_variable,
4701 loclist_read_variable_at_entry,
4702 loclist_symbol_needs,
4703 loclist_describe_location,
4704 1, /* location_has_loclist */
4705 loclist_tracepoint_var_ref,
4706 loclist_generate_c_location
4709 /* Provide a prototype to silence -Wmissing-prototypes. */
4710 extern initialize_file_ftype _initialize_dwarf2loc;
4712 void
4713 _initialize_dwarf2loc (void)
4715 add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4716 &entry_values_debug,
4717 _("Set entry values and tail call frames "
4718 "debugging."),
4719 _("Show entry values and tail call frames "
4720 "debugging."),
4721 _("When non-zero, the process of determining "
4722 "parameter values from function entry point "
4723 "and tail call frames will be printed."),
4724 NULL,
4725 show_entry_values_debug,
4726 &setdebuglist, &showdebuglist);