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/>. */
35 #include "complaints.h"
37 #include "dwarf2expr.h"
38 #include "dwarf2loc.h"
39 #include "dwarf2-frame.h"
40 #include "compile/compile.h"
44 extern int dwarf_always_disassemble
;
46 static struct value
*dwarf2_evaluate_loc_desc_full (struct type
*type
,
47 struct frame_info
*frame
,
50 struct dwarf2_per_cu_data
*per_cu
,
53 static struct call_site_parameter
*dwarf_expr_reg_to_entry_parameter
54 (struct frame_info
*frame
,
55 enum call_site_parameter_kind kind
,
56 union call_site_parameter_u kind_u
,
57 struct dwarf2_per_cu_data
**per_cu_return
);
59 /* Until these have formal names, we define these here.
60 ref: http://gcc.gnu.org/wiki/DebugFission
61 Each entry in .debug_loc.dwo begins with a byte that describes the entry,
62 and is then followed by data specific to that entry. */
66 /* Indicates the end of the list of entries. */
67 DEBUG_LOC_END_OF_LIST
= 0,
69 /* This is followed by an unsigned LEB128 number that is an index into
70 .debug_addr and specifies the base address for all following entries. */
71 DEBUG_LOC_BASE_ADDRESS
= 1,
73 /* This is followed by two unsigned LEB128 numbers that are indices into
74 .debug_addr and specify the beginning and ending addresses, and then
75 a normal location expression as in .debug_loc. */
76 DEBUG_LOC_START_END
= 2,
78 /* This is followed by an unsigned LEB128 number that is an index into
79 .debug_addr and specifies the beginning address, and a 4 byte unsigned
80 number that specifies the length, and then a normal location expression
82 DEBUG_LOC_START_LENGTH
= 3,
84 /* An internal value indicating there is insufficient data. */
85 DEBUG_LOC_BUFFER_OVERFLOW
= -1,
87 /* An internal value indicating an invalid kind of entry was found. */
88 DEBUG_LOC_INVALID_ENTRY
= -2
91 /* Helper function which throws an error if a synthetic pointer is
95 invalid_synthetic_pointer (void)
97 error (_("access outside bounds of object "
98 "referenced via synthetic pointer"));
101 /* Decode the addresses in a non-dwo .debug_loc entry.
102 A pointer to the next byte to examine is returned in *NEW_PTR.
103 The encoded low,high addresses are return in *LOW,*HIGH.
104 The result indicates the kind of entry found. */
106 static enum debug_loc_kind
107 decode_debug_loc_addresses (const gdb_byte
*loc_ptr
, const gdb_byte
*buf_end
,
108 const gdb_byte
**new_ptr
,
109 CORE_ADDR
*low
, CORE_ADDR
*high
,
110 enum bfd_endian byte_order
,
111 unsigned int addr_size
,
114 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
116 if (buf_end
- loc_ptr
< 2 * addr_size
)
117 return DEBUG_LOC_BUFFER_OVERFLOW
;
120 *low
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
122 *low
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
123 loc_ptr
+= addr_size
;
126 *high
= extract_signed_integer (loc_ptr
, addr_size
, byte_order
);
128 *high
= extract_unsigned_integer (loc_ptr
, addr_size
, byte_order
);
129 loc_ptr
+= addr_size
;
133 /* A base-address-selection entry. */
134 if ((*low
& base_mask
) == base_mask
)
135 return DEBUG_LOC_BASE_ADDRESS
;
137 /* An end-of-list entry. */
138 if (*low
== 0 && *high
== 0)
139 return DEBUG_LOC_END_OF_LIST
;
141 return DEBUG_LOC_START_END
;
144 /* Decode the addresses in .debug_loc.dwo entry.
145 A pointer to the next byte to examine is returned in *NEW_PTR.
146 The encoded low,high addresses are return in *LOW,*HIGH.
147 The result indicates the kind of entry found. */
149 static enum debug_loc_kind
150 decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data
*per_cu
,
151 const gdb_byte
*loc_ptr
,
152 const gdb_byte
*buf_end
,
153 const gdb_byte
**new_ptr
,
154 CORE_ADDR
*low
, CORE_ADDR
*high
,
155 enum bfd_endian byte_order
)
157 uint64_t low_index
, high_index
;
159 if (loc_ptr
== buf_end
)
160 return DEBUG_LOC_BUFFER_OVERFLOW
;
164 case DEBUG_LOC_END_OF_LIST
:
166 return DEBUG_LOC_END_OF_LIST
;
167 case DEBUG_LOC_BASE_ADDRESS
:
169 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &high_index
);
171 return DEBUG_LOC_BUFFER_OVERFLOW
;
172 *high
= dwarf2_read_addr_index (per_cu
, high_index
);
174 return DEBUG_LOC_BASE_ADDRESS
;
175 case DEBUG_LOC_START_END
:
176 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &low_index
);
178 return DEBUG_LOC_BUFFER_OVERFLOW
;
179 *low
= dwarf2_read_addr_index (per_cu
, low_index
);
180 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &high_index
);
182 return DEBUG_LOC_BUFFER_OVERFLOW
;
183 *high
= dwarf2_read_addr_index (per_cu
, high_index
);
185 return DEBUG_LOC_START_END
;
186 case DEBUG_LOC_START_LENGTH
:
187 loc_ptr
= gdb_read_uleb128 (loc_ptr
, buf_end
, &low_index
);
189 return DEBUG_LOC_BUFFER_OVERFLOW
;
190 *low
= dwarf2_read_addr_index (per_cu
, low_index
);
191 if (loc_ptr
+ 4 > buf_end
)
192 return DEBUG_LOC_BUFFER_OVERFLOW
;
194 *high
+= extract_unsigned_integer (loc_ptr
, 4, byte_order
);
195 *new_ptr
= loc_ptr
+ 4;
196 return DEBUG_LOC_START_LENGTH
;
198 return DEBUG_LOC_INVALID_ENTRY
;
202 /* A function for dealing with location lists. Given a
203 symbol baton (BATON) and a pc value (PC), find the appropriate
204 location expression, set *LOCEXPR_LENGTH, and return a pointer
205 to the beginning of the expression. Returns NULL on failure.
207 For now, only return the first matching location expression; there
208 can be more than one in the list. */
211 dwarf2_find_location_expression (struct dwarf2_loclist_baton
*baton
,
212 size_t *locexpr_length
, CORE_ADDR pc
)
214 struct objfile
*objfile
= dwarf2_per_cu_objfile (baton
->per_cu
);
215 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
216 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
217 unsigned int addr_size
= dwarf2_per_cu_addr_size (baton
->per_cu
);
218 int signed_addr_p
= bfd_get_sign_extend_vma (objfile
->obfd
);
219 /* Adjust base_address for relocatable objects. */
220 CORE_ADDR base_offset
= dwarf2_per_cu_text_offset (baton
->per_cu
);
221 CORE_ADDR base_address
= baton
->base_address
+ base_offset
;
222 const gdb_byte
*loc_ptr
, *buf_end
;
224 loc_ptr
= baton
->data
;
225 buf_end
= baton
->data
+ baton
->size
;
229 CORE_ADDR low
= 0, high
= 0; /* init for gcc -Wall */
231 enum debug_loc_kind kind
;
232 const gdb_byte
*new_ptr
= NULL
; /* init for gcc -Wall */
235 kind
= decode_debug_loc_dwo_addresses (baton
->per_cu
,
236 loc_ptr
, buf_end
, &new_ptr
,
237 &low
, &high
, byte_order
);
239 kind
= decode_debug_loc_addresses (loc_ptr
, buf_end
, &new_ptr
,
241 byte_order
, addr_size
,
246 case DEBUG_LOC_END_OF_LIST
:
249 case DEBUG_LOC_BASE_ADDRESS
:
250 base_address
= high
+ base_offset
;
252 case DEBUG_LOC_START_END
:
253 case DEBUG_LOC_START_LENGTH
:
255 case DEBUG_LOC_BUFFER_OVERFLOW
:
256 case DEBUG_LOC_INVALID_ENTRY
:
257 error (_("dwarf2_find_location_expression: "
258 "Corrupted DWARF expression."));
260 gdb_assert_not_reached ("bad debug_loc_kind");
263 /* Otherwise, a location expression entry.
264 If the entry is from a DWO, don't add base address: the entry is from
265 .debug_addr which already has the DWARF "base address". We still add
266 base_offset in case we're debugging a PIE executable. */
275 high
+= base_address
;
278 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
281 if (low
== high
&& pc
== low
)
283 /* This is entry PC record present only at entry point
284 of a function. Verify it is really the function entry point. */
286 const struct block
*pc_block
= block_for_pc (pc
);
287 struct symbol
*pc_func
= NULL
;
290 pc_func
= block_linkage_function (pc_block
);
292 if (pc_func
&& pc
== BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func
)))
294 *locexpr_length
= length
;
299 if (pc
>= low
&& pc
< high
)
301 *locexpr_length
= length
;
309 /* This is the baton used when performing dwarf2 expression
311 struct dwarf_expr_baton
313 struct frame_info
*frame
;
314 struct dwarf2_per_cu_data
*per_cu
;
315 CORE_ADDR obj_address
;
318 /* Implement find_frame_base_location method for LOC_BLOCK functions using
319 DWARF expression for its DW_AT_frame_base. */
322 locexpr_find_frame_base_location (struct symbol
*framefunc
, CORE_ADDR pc
,
323 const gdb_byte
**start
, size_t *length
)
325 struct dwarf2_locexpr_baton
*symbaton
326 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
328 *length
= symbaton
->size
;
329 *start
= symbaton
->data
;
332 /* Implement the struct symbol_block_ops::get_frame_base method for
333 LOC_BLOCK functions using a DWARF expression as its DW_AT_frame_base. */
336 locexpr_get_frame_base (struct symbol
*framefunc
, struct frame_info
*frame
)
338 struct gdbarch
*gdbarch
;
340 struct dwarf2_locexpr_baton
*dlbaton
;
341 const gdb_byte
*start
;
343 struct value
*result
;
345 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
346 Thus, it's supposed to provide the find_frame_base_location method as
348 gdb_assert (SYMBOL_BLOCK_OPS (framefunc
)->find_frame_base_location
!= NULL
);
350 gdbarch
= get_frame_arch (frame
);
351 type
= builtin_type (gdbarch
)->builtin_data_ptr
;
352 dlbaton
= (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
354 SYMBOL_BLOCK_OPS (framefunc
)->find_frame_base_location
355 (framefunc
, get_frame_pc (frame
), &start
, &length
);
356 result
= dwarf2_evaluate_loc_desc (type
, frame
, start
, length
,
359 /* The DW_AT_frame_base attribute contains a location description which
360 computes the base address itself. However, the call to
361 dwarf2_evaluate_loc_desc returns a value representing a variable at
362 that address. The frame base address is thus this variable's
364 return value_address (result
);
367 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
368 function uses DWARF expression for its DW_AT_frame_base. */
370 const struct symbol_block_ops dwarf2_block_frame_base_locexpr_funcs
=
372 locexpr_find_frame_base_location
,
373 locexpr_get_frame_base
376 /* Implement find_frame_base_location method for LOC_BLOCK functions using
377 DWARF location list for its DW_AT_frame_base. */
380 loclist_find_frame_base_location (struct symbol
*framefunc
, CORE_ADDR pc
,
381 const gdb_byte
**start
, size_t *length
)
383 struct dwarf2_loclist_baton
*symbaton
384 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
386 *start
= dwarf2_find_location_expression (symbaton
, length
, pc
);
389 /* Implement the struct symbol_block_ops::get_frame_base method for
390 LOC_BLOCK functions using a DWARF location list as its DW_AT_frame_base. */
393 loclist_get_frame_base (struct symbol
*framefunc
, struct frame_info
*frame
)
395 struct gdbarch
*gdbarch
;
397 struct dwarf2_loclist_baton
*dlbaton
;
398 const gdb_byte
*start
;
400 struct value
*result
;
402 /* If this method is called, then FRAMEFUNC is supposed to be a DWARF block.
403 Thus, it's supposed to provide the find_frame_base_location method as
405 gdb_assert (SYMBOL_BLOCK_OPS (framefunc
)->find_frame_base_location
!= NULL
);
407 gdbarch
= get_frame_arch (frame
);
408 type
= builtin_type (gdbarch
)->builtin_data_ptr
;
409 dlbaton
= (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (framefunc
);
411 SYMBOL_BLOCK_OPS (framefunc
)->find_frame_base_location
412 (framefunc
, get_frame_pc (frame
), &start
, &length
);
413 result
= dwarf2_evaluate_loc_desc (type
, frame
, start
, length
,
416 /* The DW_AT_frame_base attribute contains a location description which
417 computes the base address itself. However, the call to
418 dwarf2_evaluate_loc_desc returns a value representing a variable at
419 that address. The frame base address is thus this variable's
421 return value_address (result
);
424 /* Vector for inferior functions as represented by LOC_BLOCK, if the inferior
425 function uses DWARF location list for its DW_AT_frame_base. */
427 const struct symbol_block_ops dwarf2_block_frame_base_loclist_funcs
=
429 loclist_find_frame_base_location
,
430 loclist_get_frame_base
433 /* See dwarf2loc.h. */
436 func_get_frame_base_dwarf_block (struct symbol
*framefunc
, CORE_ADDR pc
,
437 const gdb_byte
**start
, size_t *length
)
439 if (SYMBOL_BLOCK_OPS (framefunc
) != NULL
)
441 const struct symbol_block_ops
*ops_block
= SYMBOL_BLOCK_OPS (framefunc
);
443 ops_block
->find_frame_base_location (framefunc
, pc
, start
, length
);
449 error (_("Could not find the frame base for \"%s\"."),
450 SYMBOL_NATURAL_NAME (framefunc
));
454 get_frame_pc_for_per_cu_dwarf_call (void *baton
)
456 dwarf_expr_context
*ctx
= (dwarf_expr_context
*) baton
;
458 return ctx
->get_frame_pc ();
462 per_cu_dwarf_call (struct dwarf_expr_context
*ctx
, cu_offset die_offset
,
463 struct dwarf2_per_cu_data
*per_cu
)
465 struct dwarf2_locexpr_baton block
;
467 block
= dwarf2_fetch_die_loc_cu_off (die_offset
, per_cu
,
468 get_frame_pc_for_per_cu_dwarf_call
,
471 /* DW_OP_call_ref is currently not supported. */
472 gdb_assert (block
.per_cu
== per_cu
);
474 ctx
->eval (block
.data
, block
.size
);
477 class dwarf_evaluate_loc_desc
: public dwarf_expr_context
481 struct frame_info
*frame
;
482 struct dwarf2_per_cu_data
*per_cu
;
483 CORE_ADDR obj_address
;
485 /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for
486 the frame in BATON. */
488 CORE_ADDR
get_frame_cfa () OVERRIDE
490 return dwarf2_frame_cfa (frame
);
493 /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for
494 the frame in BATON. */
496 CORE_ADDR
get_frame_pc () OVERRIDE
498 return get_frame_address_in_block (frame
);
501 /* Using the objfile specified in BATON, find the address for the
502 current thread's thread-local storage with offset OFFSET. */
503 CORE_ADDR
get_tls_address (CORE_ADDR offset
) OVERRIDE
505 struct objfile
*objfile
= dwarf2_per_cu_objfile (per_cu
);
507 return target_translate_tls_address (objfile
, offset
);
510 /* Helper interface of per_cu_dwarf_call for
511 dwarf2_evaluate_loc_desc. */
513 void dwarf_call (cu_offset die_offset
) OVERRIDE
515 per_cu_dwarf_call (this, die_offset
, per_cu
);
518 /* Callback function for dwarf2_evaluate_loc_desc. */
519 struct type
*impl_get_base_type (cu_offset die_offset
) OVERRIDE
521 return dwarf2_get_die_type (die_offset
, per_cu
);
524 /* Callback function for dwarf2_evaluate_loc_desc.
525 Fetch the address indexed by DW_OP_GNU_addr_index. */
527 CORE_ADDR
get_addr_index (unsigned int index
) OVERRIDE
529 return dwarf2_read_addr_index (per_cu
, index
);
532 /* Callback function for get_object_address. Return the address of the VLA
535 CORE_ADDR
get_object_address () OVERRIDE
537 if (obj_address
== 0)
538 error (_("Location address is not set."));
542 /* Execute DWARF block of call_site_parameter which matches KIND and
543 KIND_U. Choose DEREF_SIZE value of that parameter. Search
544 caller of this objects's frame.
546 The caller can be from a different CU - per_cu_dwarf_call
547 implementation can be more simple as it does not support cross-CU
550 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind
,
551 union call_site_parameter_u kind_u
,
552 int deref_size
) OVERRIDE
554 struct frame_info
*caller_frame
;
555 struct dwarf2_per_cu_data
*caller_per_cu
;
556 struct dwarf_expr_baton baton_local
;
557 struct call_site_parameter
*parameter
;
558 const gdb_byte
*data_src
;
561 caller_frame
= get_prev_frame (frame
);
563 parameter
= dwarf_expr_reg_to_entry_parameter (frame
, kind
, kind_u
,
565 data_src
= deref_size
== -1 ? parameter
->value
: parameter
->data_value
;
566 size
= deref_size
== -1 ? parameter
->value_size
: parameter
->data_value_size
;
568 /* DEREF_SIZE size is not verified here. */
569 if (data_src
== NULL
)
570 throw_error (NO_ENTRY_VALUE_ERROR
,
571 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
573 baton_local
.frame
= caller_frame
;
574 baton_local
.per_cu
= caller_per_cu
;
575 baton_local
.obj_address
= 0;
577 scoped_restore save_arch
= make_scoped_restore (&this->gdbarch
);
579 = get_objfile_arch (dwarf2_per_cu_objfile (baton_local
.per_cu
));
580 scoped_restore save_addr_size
= make_scoped_restore (&this->addr_size
);
581 this->addr_size
= dwarf2_per_cu_addr_size (baton_local
.per_cu
);
582 scoped_restore save_offset
= make_scoped_restore (&this->offset
);
583 this->offset
= dwarf2_per_cu_text_offset (baton_local
.per_cu
);
585 this->eval (data_src
, size
);
588 /* Using the frame specified in BATON, find the location expression
589 describing the frame base. Return a pointer to it in START and
590 its length in LENGTH. */
591 void get_frame_base (const gdb_byte
**start
, size_t * length
) OVERRIDE
593 /* FIXME: cagney/2003-03-26: This code should be using
594 get_frame_base_address(), and then implement a dwarf2 specific
596 struct symbol
*framefunc
;
597 const struct block
*bl
= get_frame_block (frame
, NULL
);
600 error (_("frame address is not available."));
602 /* Use block_linkage_function, which returns a real (not inlined)
603 function, instead of get_frame_function, which may return an
605 framefunc
= block_linkage_function (bl
);
607 /* If we found a frame-relative symbol then it was certainly within
608 some function associated with a frame. If we can't find the frame,
609 something has gone wrong. */
610 gdb_assert (framefunc
!= NULL
);
612 func_get_frame_base_dwarf_block (framefunc
,
613 get_frame_address_in_block (frame
),
617 /* Read memory at ADDR (length LEN) into BUF. */
619 void read_mem (gdb_byte
*buf
, CORE_ADDR addr
, size_t len
) OVERRIDE
621 read_memory (addr
, buf
, len
);
624 /* Using the frame specified in BATON, return the value of register
625 REGNUM, treated as a pointer. */
626 CORE_ADDR
read_addr_from_reg (int dwarf_regnum
) OVERRIDE
628 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
629 int regnum
= dwarf_reg_to_regnum_or_error (gdbarch
, dwarf_regnum
);
631 return address_from_register (regnum
, frame
);
634 /* Implement "get_reg_value" callback. */
636 struct value
*get_reg_value (struct type
*type
, int dwarf_regnum
) OVERRIDE
638 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
639 int regnum
= dwarf_reg_to_regnum_or_error (gdbarch
, dwarf_regnum
);
641 return value_from_register (type
, regnum
, frame
);
645 /* See dwarf2loc.h. */
647 unsigned int entry_values_debug
= 0;
649 /* Helper to set entry_values_debug. */
652 show_entry_values_debug (struct ui_file
*file
, int from_tty
,
653 struct cmd_list_element
*c
, const char *value
)
655 fprintf_filtered (file
,
656 _("Entry values and tail call frames debugging is %s.\n"),
660 /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address.
661 CALLER_FRAME (for registers) can be NULL if it is not known. This function
662 always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */
665 call_site_to_target_addr (struct gdbarch
*call_site_gdbarch
,
666 struct call_site
*call_site
,
667 struct frame_info
*caller_frame
)
669 switch (FIELD_LOC_KIND (call_site
->target
))
671 case FIELD_LOC_KIND_DWARF_BLOCK
:
673 struct dwarf2_locexpr_baton
*dwarf_block
;
675 struct type
*caller_core_addr_type
;
676 struct gdbarch
*caller_arch
;
678 dwarf_block
= FIELD_DWARF_BLOCK (call_site
->target
);
679 if (dwarf_block
== NULL
)
681 struct bound_minimal_symbol msym
;
683 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
684 throw_error (NO_ENTRY_VALUE_ERROR
,
685 _("DW_AT_GNU_call_site_target is not specified "
687 paddress (call_site_gdbarch
, call_site
->pc
),
688 (msym
.minsym
== NULL
? "???"
689 : MSYMBOL_PRINT_NAME (msym
.minsym
)));
692 if (caller_frame
== NULL
)
694 struct bound_minimal_symbol msym
;
696 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
697 throw_error (NO_ENTRY_VALUE_ERROR
,
698 _("DW_AT_GNU_call_site_target DWARF block resolving "
699 "requires known frame which is currently not "
700 "available at %s in %s"),
701 paddress (call_site_gdbarch
, call_site
->pc
),
702 (msym
.minsym
== NULL
? "???"
703 : MSYMBOL_PRINT_NAME (msym
.minsym
)));
706 caller_arch
= get_frame_arch (caller_frame
);
707 caller_core_addr_type
= builtin_type (caller_arch
)->builtin_func_ptr
;
708 val
= dwarf2_evaluate_loc_desc (caller_core_addr_type
, caller_frame
,
709 dwarf_block
->data
, dwarf_block
->size
,
710 dwarf_block
->per_cu
);
711 /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF
713 if (VALUE_LVAL (val
) == lval_memory
)
714 return value_address (val
);
716 return value_as_address (val
);
719 case FIELD_LOC_KIND_PHYSNAME
:
721 const char *physname
;
722 struct bound_minimal_symbol msym
;
724 physname
= FIELD_STATIC_PHYSNAME (call_site
->target
);
726 /* Handle both the mangled and demangled PHYSNAME. */
727 msym
= lookup_minimal_symbol (physname
, NULL
, NULL
);
728 if (msym
.minsym
== NULL
)
730 msym
= lookup_minimal_symbol_by_pc (call_site
->pc
- 1);
731 throw_error (NO_ENTRY_VALUE_ERROR
,
732 _("Cannot find function \"%s\" for a call site target "
734 physname
, paddress (call_site_gdbarch
, call_site
->pc
),
735 (msym
.minsym
== NULL
? "???"
736 : MSYMBOL_PRINT_NAME (msym
.minsym
)));
739 return BMSYMBOL_VALUE_ADDRESS (msym
);
742 case FIELD_LOC_KIND_PHYSADDR
:
743 return FIELD_STATIC_PHYSADDR (call_site
->target
);
746 internal_error (__FILE__
, __LINE__
, _("invalid call site target kind"));
750 /* Convert function entry point exact address ADDR to the function which is
751 compliant with TAIL_CALL_LIST_COMPLETE condition. Throw
752 NO_ENTRY_VALUE_ERROR otherwise. */
754 static struct symbol
*
755 func_addr_to_tail_call_list (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
757 struct symbol
*sym
= find_pc_function (addr
);
760 if (sym
== NULL
|| BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)) != addr
)
761 throw_error (NO_ENTRY_VALUE_ERROR
,
762 _("DW_TAG_GNU_call_site resolving failed to find function "
763 "name for address %s"),
764 paddress (gdbarch
, addr
));
766 type
= SYMBOL_TYPE (sym
);
767 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FUNC
);
768 gdb_assert (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FUNC
);
773 /* Verify function with entry point exact address ADDR can never call itself
774 via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it
775 can call itself via tail calls.
777 If a funtion can tail call itself its entry value based parameters are
778 unreliable. There is no verification whether the value of some/all
779 parameters is unchanged through the self tail call, we expect if there is
780 a self tail call all the parameters can be modified. */
783 func_verify_no_selftailcall (struct gdbarch
*gdbarch
, CORE_ADDR verify_addr
)
785 struct obstack addr_obstack
;
786 struct cleanup
*old_chain
;
789 /* Track here CORE_ADDRs which were already visited. */
792 /* The verification is completely unordered. Track here function addresses
793 which still need to be iterated. */
794 VEC (CORE_ADDR
) *todo
= NULL
;
796 obstack_init (&addr_obstack
);
797 old_chain
= make_cleanup_obstack_free (&addr_obstack
);
798 addr_hash
= htab_create_alloc_ex (64, core_addr_hash
, core_addr_eq
, NULL
,
799 &addr_obstack
, hashtab_obstack_allocate
,
801 make_cleanup_htab_delete (addr_hash
);
803 make_cleanup (VEC_cleanup (CORE_ADDR
), &todo
);
805 VEC_safe_push (CORE_ADDR
, todo
, verify_addr
);
806 while (!VEC_empty (CORE_ADDR
, todo
))
808 struct symbol
*func_sym
;
809 struct call_site
*call_site
;
811 addr
= VEC_pop (CORE_ADDR
, todo
);
813 func_sym
= func_addr_to_tail_call_list (gdbarch
, addr
);
815 for (call_site
= TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym
));
816 call_site
; call_site
= call_site
->tail_call_next
)
818 CORE_ADDR target_addr
;
821 /* CALLER_FRAME with registers is not available for tail-call jumped
823 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, NULL
);
825 if (target_addr
== verify_addr
)
827 struct bound_minimal_symbol msym
;
829 msym
= lookup_minimal_symbol_by_pc (verify_addr
);
830 throw_error (NO_ENTRY_VALUE_ERROR
,
831 _("DW_OP_GNU_entry_value resolving has found "
832 "function \"%s\" at %s can call itself via tail "
834 (msym
.minsym
== NULL
? "???"
835 : MSYMBOL_PRINT_NAME (msym
.minsym
)),
836 paddress (gdbarch
, verify_addr
));
839 slot
= htab_find_slot (addr_hash
, &target_addr
, INSERT
);
842 *slot
= obstack_copy (&addr_obstack
, &target_addr
,
843 sizeof (target_addr
));
844 VEC_safe_push (CORE_ADDR
, todo
, target_addr
);
849 do_cleanups (old_chain
);
852 /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for
853 ENTRY_VALUES_DEBUG. */
856 tailcall_dump (struct gdbarch
*gdbarch
, const struct call_site
*call_site
)
858 CORE_ADDR addr
= call_site
->pc
;
859 struct bound_minimal_symbol msym
= lookup_minimal_symbol_by_pc (addr
- 1);
861 fprintf_unfiltered (gdb_stdlog
, " %s(%s)", paddress (gdbarch
, addr
),
862 (msym
.minsym
== NULL
? "???"
863 : MSYMBOL_PRINT_NAME (msym
.minsym
)));
867 /* vec.h needs single word type name, typedef it. */
868 typedef struct call_site
*call_sitep
;
870 /* Define VEC (call_sitep) functions. */
871 DEF_VEC_P (call_sitep
);
873 /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP
874 only top callers and bottom callees which are present in both. GDBARCH is
875 used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are
876 no remaining possibilities to provide unambiguous non-trivial result.
877 RESULTP should point to NULL on the first (initialization) call. Caller is
878 responsible for xfree of any RESULTP data. */
881 chain_candidate (struct gdbarch
*gdbarch
, struct call_site_chain
**resultp
,
882 VEC (call_sitep
) *chain
)
884 struct call_site_chain
*result
= *resultp
;
885 long length
= VEC_length (call_sitep
, chain
);
886 int callers
, callees
, idx
;
890 /* Create the initial chain containing all the passed PCs. */
892 result
= ((struct call_site_chain
*)
893 xmalloc (sizeof (*result
)
894 + sizeof (*result
->call_site
) * (length
- 1)));
895 result
->length
= length
;
896 result
->callers
= result
->callees
= length
;
897 if (!VEC_empty (call_sitep
, chain
))
898 memcpy (result
->call_site
, VEC_address (call_sitep
, chain
),
899 sizeof (*result
->call_site
) * length
);
902 if (entry_values_debug
)
904 fprintf_unfiltered (gdb_stdlog
, "tailcall: initial:");
905 for (idx
= 0; idx
< length
; idx
++)
906 tailcall_dump (gdbarch
, result
->call_site
[idx
]);
907 fputc_unfiltered ('\n', gdb_stdlog
);
913 if (entry_values_debug
)
915 fprintf_unfiltered (gdb_stdlog
, "tailcall: compare:");
916 for (idx
= 0; idx
< length
; idx
++)
917 tailcall_dump (gdbarch
, VEC_index (call_sitep
, chain
, idx
));
918 fputc_unfiltered ('\n', gdb_stdlog
);
921 /* Intersect callers. */
923 callers
= std::min ((long) result
->callers
, length
);
924 for (idx
= 0; idx
< callers
; idx
++)
925 if (result
->call_site
[idx
] != VEC_index (call_sitep
, chain
, idx
))
927 result
->callers
= idx
;
931 /* Intersect callees. */
933 callees
= std::min ((long) result
->callees
, length
);
934 for (idx
= 0; idx
< callees
; idx
++)
935 if (result
->call_site
[result
->length
- 1 - idx
]
936 != VEC_index (call_sitep
, chain
, length
- 1 - idx
))
938 result
->callees
= idx
;
942 if (entry_values_debug
)
944 fprintf_unfiltered (gdb_stdlog
, "tailcall: reduced:");
945 for (idx
= 0; idx
< result
->callers
; idx
++)
946 tailcall_dump (gdbarch
, result
->call_site
[idx
]);
947 fputs_unfiltered (" |", gdb_stdlog
);
948 for (idx
= 0; idx
< result
->callees
; idx
++)
949 tailcall_dump (gdbarch
, result
->call_site
[result
->length
950 - result
->callees
+ idx
]);
951 fputc_unfiltered ('\n', gdb_stdlog
);
954 if (result
->callers
== 0 && result
->callees
== 0)
956 /* There are no common callers or callees. It could be also a direct
957 call (which has length 0) with ambiguous possibility of an indirect
958 call - CALLERS == CALLEES == 0 is valid during the first allocation
959 but any subsequence processing of such entry means ambiguity. */
965 /* See call_site_find_chain_1 why there is no way to reach the bottom callee
966 PC again. In such case there must be two different code paths to reach
967 it. CALLERS + CALLEES equal to LENGTH in the case of self tail-call. */
968 gdb_assert (result
->callers
+ result
->callees
<= result
->length
);
971 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
972 assumed frames between them use GDBARCH. Use depth first search so we can
973 keep single CHAIN of call_site's back to CALLER_PC. Function recursion
974 would have needless GDB stack overhead. Caller is responsible for xfree of
975 the returned result. Any unreliability results in thrown
976 NO_ENTRY_VALUE_ERROR. */
978 static struct call_site_chain
*
979 call_site_find_chain_1 (struct gdbarch
*gdbarch
, CORE_ADDR caller_pc
,
982 CORE_ADDR save_callee_pc
= callee_pc
;
983 struct obstack addr_obstack
;
984 struct cleanup
*back_to_retval
, *back_to_workdata
;
985 struct call_site_chain
*retval
= NULL
;
986 struct call_site
*call_site
;
988 /* Mark CALL_SITEs so we do not visit the same ones twice. */
991 /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's
992 call_site nor any possible call_site at CALLEE_PC's function is there.
993 Any CALL_SITE in CHAIN will be iterated to its siblings - via
994 TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */
995 VEC (call_sitep
) *chain
= NULL
;
997 /* We are not interested in the specific PC inside the callee function. */
998 callee_pc
= get_pc_function_start (callee_pc
);
1000 throw_error (NO_ENTRY_VALUE_ERROR
, _("Unable to find function for PC %s"),
1001 paddress (gdbarch
, save_callee_pc
));
1003 back_to_retval
= make_cleanup (free_current_contents
, &retval
);
1005 obstack_init (&addr_obstack
);
1006 back_to_workdata
= make_cleanup_obstack_free (&addr_obstack
);
1007 addr_hash
= htab_create_alloc_ex (64, core_addr_hash
, core_addr_eq
, NULL
,
1008 &addr_obstack
, hashtab_obstack_allocate
,
1010 make_cleanup_htab_delete (addr_hash
);
1012 make_cleanup (VEC_cleanup (call_sitep
), &chain
);
1014 /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site
1015 at the target's function. All the possible tail call sites in the
1016 target's function will get iterated as already pushed into CHAIN via their
1018 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
1022 CORE_ADDR target_func_addr
;
1023 struct call_site
*target_call_site
;
1025 /* CALLER_FRAME with registers is not available for tail-call jumped
1027 target_func_addr
= call_site_to_target_addr (gdbarch
, call_site
, NULL
);
1029 if (target_func_addr
== callee_pc
)
1031 chain_candidate (gdbarch
, &retval
, chain
);
1035 /* There is no way to reach CALLEE_PC again as we would prevent
1036 entering it twice as being already marked in ADDR_HASH. */
1037 target_call_site
= NULL
;
1041 struct symbol
*target_func
;
1043 target_func
= func_addr_to_tail_call_list (gdbarch
, target_func_addr
);
1044 target_call_site
= TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func
));
1049 /* Attempt to visit TARGET_CALL_SITE. */
1051 if (target_call_site
)
1055 slot
= htab_find_slot (addr_hash
, &target_call_site
->pc
, INSERT
);
1058 /* Successfully entered TARGET_CALL_SITE. */
1060 *slot
= &target_call_site
->pc
;
1061 VEC_safe_push (call_sitep
, chain
, target_call_site
);
1066 /* Backtrack (without revisiting the originating call_site). Try the
1067 callers's sibling; if there isn't any try the callers's callers's
1070 target_call_site
= NULL
;
1071 while (!VEC_empty (call_sitep
, chain
))
1073 call_site
= VEC_pop (call_sitep
, chain
);
1075 gdb_assert (htab_find_slot (addr_hash
, &call_site
->pc
,
1076 NO_INSERT
) != NULL
);
1077 htab_remove_elt (addr_hash
, &call_site
->pc
);
1079 target_call_site
= call_site
->tail_call_next
;
1080 if (target_call_site
)
1084 while (target_call_site
);
1086 if (VEC_empty (call_sitep
, chain
))
1089 call_site
= VEC_last (call_sitep
, chain
);
1094 struct bound_minimal_symbol msym_caller
, msym_callee
;
1096 msym_caller
= lookup_minimal_symbol_by_pc (caller_pc
);
1097 msym_callee
= lookup_minimal_symbol_by_pc (callee_pc
);
1098 throw_error (NO_ENTRY_VALUE_ERROR
,
1099 _("There are no unambiguously determinable intermediate "
1100 "callers or callees between caller function \"%s\" at %s "
1101 "and callee function \"%s\" at %s"),
1102 (msym_caller
.minsym
== NULL
1103 ? "???" : MSYMBOL_PRINT_NAME (msym_caller
.minsym
)),
1104 paddress (gdbarch
, caller_pc
),
1105 (msym_callee
.minsym
== NULL
1106 ? "???" : MSYMBOL_PRINT_NAME (msym_callee
.minsym
)),
1107 paddress (gdbarch
, callee_pc
));
1110 do_cleanups (back_to_workdata
);
1111 discard_cleanups (back_to_retval
);
1115 /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the
1116 assumed frames between them use GDBARCH. If valid call_site_chain cannot be
1117 constructed return NULL. Caller is responsible for xfree of the returned
1120 struct call_site_chain
*
1121 call_site_find_chain (struct gdbarch
*gdbarch
, CORE_ADDR caller_pc
,
1122 CORE_ADDR callee_pc
)
1124 struct call_site_chain
*retval
= NULL
;
1128 retval
= call_site_find_chain_1 (gdbarch
, caller_pc
, callee_pc
);
1130 CATCH (e
, RETURN_MASK_ERROR
)
1132 if (e
.error
== NO_ENTRY_VALUE_ERROR
)
1134 if (entry_values_debug
)
1135 exception_print (gdb_stdout
, e
);
1140 throw_exception (e
);
1147 /* Return 1 if KIND and KIND_U match PARAMETER. Return 0 otherwise. */
1150 call_site_parameter_matches (struct call_site_parameter
*parameter
,
1151 enum call_site_parameter_kind kind
,
1152 union call_site_parameter_u kind_u
)
1154 if (kind
== parameter
->kind
)
1157 case CALL_SITE_PARAMETER_DWARF_REG
:
1158 return kind_u
.dwarf_reg
== parameter
->u
.dwarf_reg
;
1159 case CALL_SITE_PARAMETER_FB_OFFSET
:
1160 return kind_u
.fb_offset
== parameter
->u
.fb_offset
;
1161 case CALL_SITE_PARAMETER_PARAM_OFFSET
:
1162 return kind_u
.param_offset
.cu_off
== parameter
->u
.param_offset
.cu_off
;
1167 /* Fetch call_site_parameter from caller matching KIND and KIND_U.
1168 FRAME is for callee.
1170 Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR
1173 static struct call_site_parameter
*
1174 dwarf_expr_reg_to_entry_parameter (struct frame_info
*frame
,
1175 enum call_site_parameter_kind kind
,
1176 union call_site_parameter_u kind_u
,
1177 struct dwarf2_per_cu_data
**per_cu_return
)
1179 CORE_ADDR func_addr
, caller_pc
;
1180 struct gdbarch
*gdbarch
;
1181 struct frame_info
*caller_frame
;
1182 struct call_site
*call_site
;
1184 /* Initialize it just to avoid a GCC false warning. */
1185 struct call_site_parameter
*parameter
= NULL
;
1186 CORE_ADDR target_addr
;
1188 while (get_frame_type (frame
) == INLINE_FRAME
)
1190 frame
= get_prev_frame (frame
);
1191 gdb_assert (frame
!= NULL
);
1194 func_addr
= get_frame_func (frame
);
1195 gdbarch
= get_frame_arch (frame
);
1196 caller_frame
= get_prev_frame (frame
);
1197 if (gdbarch
!= frame_unwind_arch (frame
))
1199 struct bound_minimal_symbol msym
1200 = lookup_minimal_symbol_by_pc (func_addr
);
1201 struct gdbarch
*caller_gdbarch
= frame_unwind_arch (frame
);
1203 throw_error (NO_ENTRY_VALUE_ERROR
,
1204 _("DW_OP_GNU_entry_value resolving callee gdbarch %s "
1205 "(of %s (%s)) does not match caller gdbarch %s"),
1206 gdbarch_bfd_arch_info (gdbarch
)->printable_name
,
1207 paddress (gdbarch
, func_addr
),
1208 (msym
.minsym
== NULL
? "???"
1209 : MSYMBOL_PRINT_NAME (msym
.minsym
)),
1210 gdbarch_bfd_arch_info (caller_gdbarch
)->printable_name
);
1213 if (caller_frame
== NULL
)
1215 struct bound_minimal_symbol msym
1216 = lookup_minimal_symbol_by_pc (func_addr
);
1218 throw_error (NO_ENTRY_VALUE_ERROR
, _("DW_OP_GNU_entry_value resolving "
1219 "requires caller of %s (%s)"),
1220 paddress (gdbarch
, func_addr
),
1221 (msym
.minsym
== NULL
? "???"
1222 : MSYMBOL_PRINT_NAME (msym
.minsym
)));
1224 caller_pc
= get_frame_pc (caller_frame
);
1225 call_site
= call_site_for_pc (gdbarch
, caller_pc
);
1227 target_addr
= call_site_to_target_addr (gdbarch
, call_site
, caller_frame
);
1228 if (target_addr
!= func_addr
)
1230 struct minimal_symbol
*target_msym
, *func_msym
;
1232 target_msym
= lookup_minimal_symbol_by_pc (target_addr
).minsym
;
1233 func_msym
= lookup_minimal_symbol_by_pc (func_addr
).minsym
;
1234 throw_error (NO_ENTRY_VALUE_ERROR
,
1235 _("DW_OP_GNU_entry_value resolving expects callee %s at %s "
1236 "but the called frame is for %s at %s"),
1237 (target_msym
== NULL
? "???"
1238 : MSYMBOL_PRINT_NAME (target_msym
)),
1239 paddress (gdbarch
, target_addr
),
1240 func_msym
== NULL
? "???" : MSYMBOL_PRINT_NAME (func_msym
),
1241 paddress (gdbarch
, func_addr
));
1244 /* No entry value based parameters would be reliable if this function can
1245 call itself via tail calls. */
1246 func_verify_no_selftailcall (gdbarch
, func_addr
);
1248 for (iparams
= 0; iparams
< call_site
->parameter_count
; iparams
++)
1250 parameter
= &call_site
->parameter
[iparams
];
1251 if (call_site_parameter_matches (parameter
, kind
, kind_u
))
1254 if (iparams
== call_site
->parameter_count
)
1256 struct minimal_symbol
*msym
1257 = lookup_minimal_symbol_by_pc (caller_pc
).minsym
;
1259 /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not
1260 determine its value. */
1261 throw_error (NO_ENTRY_VALUE_ERROR
, _("Cannot find matching parameter "
1262 "at DW_TAG_GNU_call_site %s at %s"),
1263 paddress (gdbarch
, caller_pc
),
1264 msym
== NULL
? "???" : MSYMBOL_PRINT_NAME (msym
));
1267 *per_cu_return
= call_site
->per_cu
;
1271 /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return
1272 the normal DW_AT_GNU_call_site_value block. Otherwise return the
1273 DW_AT_GNU_call_site_data_value (dereferenced) block.
1275 TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned
1278 Function always returns non-NULL, non-optimized out value. It throws
1279 NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */
1281 static struct value
*
1282 dwarf_entry_parameter_to_value (struct call_site_parameter
*parameter
,
1283 CORE_ADDR deref_size
, struct type
*type
,
1284 struct frame_info
*caller_frame
,
1285 struct dwarf2_per_cu_data
*per_cu
)
1287 const gdb_byte
*data_src
;
1291 data_src
= deref_size
== -1 ? parameter
->value
: parameter
->data_value
;
1292 size
= deref_size
== -1 ? parameter
->value_size
: parameter
->data_value_size
;
1294 /* DEREF_SIZE size is not verified here. */
1295 if (data_src
== NULL
)
1296 throw_error (NO_ENTRY_VALUE_ERROR
,
1297 _("Cannot resolve DW_AT_GNU_call_site_data_value"));
1299 /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF
1300 location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from
1302 data
= (gdb_byte
*) alloca (size
+ 1);
1303 memcpy (data
, data_src
, size
);
1304 data
[size
] = DW_OP_stack_value
;
1306 return dwarf2_evaluate_loc_desc (type
, caller_frame
, data
, size
+ 1, per_cu
);
1309 /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform
1310 the indirect method on it, that is use its stored target value, the sole
1311 purpose of entry_data_value_funcs.. */
1313 static struct value
*
1314 entry_data_value_coerce_ref (const struct value
*value
)
1316 struct type
*checked_type
= check_typedef (value_type (value
));
1317 struct value
*target_val
;
1319 if (TYPE_CODE (checked_type
) != TYPE_CODE_REF
)
1322 target_val
= (struct value
*) value_computed_closure (value
);
1323 value_incref (target_val
);
1327 /* Implement copy_closure. */
1330 entry_data_value_copy_closure (const struct value
*v
)
1332 struct value
*target_val
= (struct value
*) value_computed_closure (v
);
1334 value_incref (target_val
);
1338 /* Implement free_closure. */
1341 entry_data_value_free_closure (struct value
*v
)
1343 struct value
*target_val
= (struct value
*) value_computed_closure (v
);
1345 value_free (target_val
);
1348 /* Vector for methods for an entry value reference where the referenced value
1349 is stored in the caller. On the first dereference use
1350 DW_AT_GNU_call_site_data_value in the caller. */
1352 static const struct lval_funcs entry_data_value_funcs
=
1356 NULL
, /* indirect */
1357 entry_data_value_coerce_ref
,
1358 NULL
, /* check_synthetic_pointer */
1359 entry_data_value_copy_closure
,
1360 entry_data_value_free_closure
1363 /* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KIND_U
1364 are used to match DW_AT_location at the caller's
1365 DW_TAG_GNU_call_site_parameter.
1367 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1368 cannot resolve the parameter for any reason. */
1370 static struct value
*
1371 value_of_dwarf_reg_entry (struct type
*type
, struct frame_info
*frame
,
1372 enum call_site_parameter_kind kind
,
1373 union call_site_parameter_u kind_u
)
1375 struct type
*checked_type
= check_typedef (type
);
1376 struct type
*target_type
= TYPE_TARGET_TYPE (checked_type
);
1377 struct frame_info
*caller_frame
= get_prev_frame (frame
);
1378 struct value
*outer_val
, *target_val
, *val
;
1379 struct call_site_parameter
*parameter
;
1380 struct dwarf2_per_cu_data
*caller_per_cu
;
1382 parameter
= dwarf_expr_reg_to_entry_parameter (frame
, kind
, kind_u
,
1385 outer_val
= dwarf_entry_parameter_to_value (parameter
, -1 /* deref_size */,
1389 /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be
1390 used and it is not available do not fall back to OUTER_VAL - dereferencing
1391 TYPE_CODE_REF with non-entry data value would give current value - not the
1394 if (TYPE_CODE (checked_type
) != TYPE_CODE_REF
1395 || TYPE_TARGET_TYPE (checked_type
) == NULL
)
1398 target_val
= dwarf_entry_parameter_to_value (parameter
,
1399 TYPE_LENGTH (target_type
),
1400 target_type
, caller_frame
,
1403 release_value (target_val
);
1404 val
= allocate_computed_value (type
, &entry_data_value_funcs
,
1405 target_val
/* closure */);
1407 /* Copy the referencing pointer to the new computed value. */
1408 memcpy (value_contents_raw (val
), value_contents_raw (outer_val
),
1409 TYPE_LENGTH (checked_type
));
1410 set_value_lazy (val
, 0);
1415 /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and
1416 SIZE are DWARF block used to match DW_AT_location at the caller's
1417 DW_TAG_GNU_call_site_parameter.
1419 Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it
1420 cannot resolve the parameter for any reason. */
1422 static struct value
*
1423 value_of_dwarf_block_entry (struct type
*type
, struct frame_info
*frame
,
1424 const gdb_byte
*block
, size_t block_len
)
1426 union call_site_parameter_u kind_u
;
1428 kind_u
.dwarf_reg
= dwarf_block_to_dwarf_reg (block
, block
+ block_len
);
1429 if (kind_u
.dwarf_reg
!= -1)
1430 return value_of_dwarf_reg_entry (type
, frame
, CALL_SITE_PARAMETER_DWARF_REG
,
1433 if (dwarf_block_to_fb_offset (block
, block
+ block_len
, &kind_u
.fb_offset
))
1434 return value_of_dwarf_reg_entry (type
, frame
, CALL_SITE_PARAMETER_FB_OFFSET
,
1437 /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message
1438 suppressed during normal operation. The expression can be arbitrary if
1439 there is no caller-callee entry value binding expected. */
1440 throw_error (NO_ENTRY_VALUE_ERROR
,
1441 _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported "
1442 "only for single DW_OP_reg* or for DW_OP_fbreg(*)"));
1445 struct piece_closure
1447 /* Reference count. */
1450 /* The CU from which this closure's expression came. */
1451 struct dwarf2_per_cu_data
*per_cu
;
1453 /* The number of pieces used to describe this variable. */
1456 /* The target address size, used only for DWARF_VALUE_STACK. */
1459 /* The pieces themselves. */
1460 struct dwarf_expr_piece
*pieces
;
1463 /* Allocate a closure for a value formed from separately-described
1466 static struct piece_closure
*
1467 allocate_piece_closure (struct dwarf2_per_cu_data
*per_cu
,
1468 int n_pieces
, struct dwarf_expr_piece
*pieces
,
1471 struct piece_closure
*c
= XCNEW (struct piece_closure
);
1476 c
->n_pieces
= n_pieces
;
1477 c
->addr_size
= addr_size
;
1478 c
->pieces
= XCNEWVEC (struct dwarf_expr_piece
, n_pieces
);
1480 memcpy (c
->pieces
, pieces
, n_pieces
* sizeof (struct dwarf_expr_piece
));
1481 for (i
= 0; i
< n_pieces
; ++i
)
1482 if (c
->pieces
[i
].location
== DWARF_VALUE_STACK
)
1483 value_incref (c
->pieces
[i
].v
.value
);
1488 /* The lowest-level function to extract bits from a byte buffer.
1489 SOURCE is the buffer. It is updated if we read to the end of a
1491 SOURCE_OFFSET_BITS is the offset of the first bit to read. It is
1492 updated to reflect the number of bits actually read.
1493 NBITS is the number of bits we want to read. It is updated to
1494 reflect the number of bits actually read. This function may read
1496 BITS_BIG_ENDIAN is taken directly from gdbarch.
1497 This function returns the extracted bits. */
1500 extract_bits_primitive (const gdb_byte
**source
,
1501 unsigned int *source_offset_bits
,
1502 int *nbits
, int bits_big_endian
)
1504 unsigned int avail
, mask
, datum
;
1506 gdb_assert (*source_offset_bits
< 8);
1508 avail
= 8 - *source_offset_bits
;
1512 mask
= (1 << avail
) - 1;
1514 if (bits_big_endian
)
1515 datum
>>= 8 - (*source_offset_bits
+ *nbits
);
1517 datum
>>= *source_offset_bits
;
1521 *source_offset_bits
+= avail
;
1522 if (*source_offset_bits
>= 8)
1524 *source_offset_bits
-= 8;
1531 /* Extract some bits from a source buffer and move forward in the
1534 SOURCE is the source buffer. It is updated as bytes are read.
1535 SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as
1537 NBITS is the number of bits to read.
1538 BITS_BIG_ENDIAN is taken directly from gdbarch.
1540 This function returns the bits that were read. */
1543 extract_bits (const gdb_byte
**source
, unsigned int *source_offset_bits
,
1544 int nbits
, int bits_big_endian
)
1548 gdb_assert (nbits
> 0 && nbits
<= 8);
1550 datum
= extract_bits_primitive (source
, source_offset_bits
, &nbits
,
1556 more
= extract_bits_primitive (source
, source_offset_bits
, &nbits
,
1558 if (bits_big_endian
)
1568 /* Write some bits into a buffer and move forward in the buffer.
1570 DATUM is the bits to write. The low-order bits of DATUM are used.
1571 DEST is the destination buffer. It is updated as bytes are
1573 DEST_OFFSET_BITS is the bit offset in DEST at which writing is
1575 NBITS is the number of valid bits in DATUM.
1576 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1579 insert_bits (unsigned int datum
,
1580 gdb_byte
*dest
, unsigned int dest_offset_bits
,
1581 int nbits
, int bits_big_endian
)
1585 gdb_assert (dest_offset_bits
+ nbits
<= 8);
1587 mask
= (1 << nbits
) - 1;
1588 if (bits_big_endian
)
1590 datum
<<= 8 - (dest_offset_bits
+ nbits
);
1591 mask
<<= 8 - (dest_offset_bits
+ nbits
);
1595 datum
<<= dest_offset_bits
;
1596 mask
<<= dest_offset_bits
;
1599 gdb_assert ((datum
& ~mask
) == 0);
1601 *dest
= (*dest
& ~mask
) | datum
;
1604 /* Copy bits from a source to a destination.
1606 DEST is where the bits should be written.
1607 DEST_OFFSET_BITS is the bit offset into DEST.
1608 SOURCE is the source of bits.
1609 SOURCE_OFFSET_BITS is the bit offset into SOURCE.
1610 BIT_COUNT is the number of bits to copy.
1611 BITS_BIG_ENDIAN is taken directly from gdbarch. */
1614 copy_bitwise (gdb_byte
*dest
, unsigned int dest_offset_bits
,
1615 const gdb_byte
*source
, unsigned int source_offset_bits
,
1616 unsigned int bit_count
,
1617 int bits_big_endian
)
1619 unsigned int dest_avail
;
1622 /* Reduce everything to byte-size pieces. */
1623 dest
+= dest_offset_bits
/ 8;
1624 dest_offset_bits
%= 8;
1625 source
+= source_offset_bits
/ 8;
1626 source_offset_bits
%= 8;
1628 dest_avail
= 8 - dest_offset_bits
% 8;
1630 /* See if we can fill the first destination byte. */
1631 if (dest_avail
< bit_count
)
1633 datum
= extract_bits (&source
, &source_offset_bits
, dest_avail
,
1635 insert_bits (datum
, dest
, dest_offset_bits
, dest_avail
, bits_big_endian
);
1637 dest_offset_bits
= 0;
1638 bit_count
-= dest_avail
;
1641 /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
1642 than 8 bits remaining. */
1643 gdb_assert (dest_offset_bits
% 8 == 0 || bit_count
< 8);
1644 for (; bit_count
>= 8; bit_count
-= 8)
1646 datum
= extract_bits (&source
, &source_offset_bits
, 8, bits_big_endian
);
1647 *dest
++ = (gdb_byte
) datum
;
1650 /* Finally, we may have a few leftover bits. */
1651 gdb_assert (bit_count
<= 8 - dest_offset_bits
% 8);
1654 datum
= extract_bits (&source
, &source_offset_bits
, bit_count
,
1656 insert_bits (datum
, dest
, dest_offset_bits
, bit_count
, bits_big_endian
);
1661 read_pieced_value (struct value
*v
)
1665 ULONGEST bits_to_skip
;
1667 struct piece_closure
*c
1668 = (struct piece_closure
*) value_computed_closure (v
);
1669 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (v
));
1671 size_t buffer_size
= 0;
1672 std::vector
<gdb_byte
> buffer
;
1674 = gdbarch_bits_big_endian (get_type_arch (value_type (v
)));
1676 if (value_type (v
) != value_enclosing_type (v
))
1677 internal_error (__FILE__
, __LINE__
,
1678 _("Should not be able to create a lazy value with "
1679 "an enclosing type"));
1681 contents
= value_contents_raw (v
);
1682 bits_to_skip
= 8 * value_offset (v
);
1683 if (value_bitsize (v
))
1685 bits_to_skip
+= value_bitpos (v
);
1686 type_len
= value_bitsize (v
);
1689 type_len
= 8 * TYPE_LENGTH (value_type (v
));
1691 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
1693 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1694 size_t this_size
, this_size_bits
;
1695 long dest_offset_bits
, source_offset_bits
, source_offset
;
1696 const gdb_byte
*intermediate_buffer
;
1698 /* Compute size, source, and destination offsets for copying, in
1700 this_size_bits
= p
->size
;
1701 if (bits_to_skip
> 0 && bits_to_skip
>= this_size_bits
)
1703 bits_to_skip
-= this_size_bits
;
1706 if (bits_to_skip
> 0)
1708 dest_offset_bits
= 0;
1709 source_offset_bits
= bits_to_skip
;
1710 this_size_bits
-= bits_to_skip
;
1715 dest_offset_bits
= offset
;
1716 source_offset_bits
= 0;
1718 if (this_size_bits
> type_len
- offset
)
1719 this_size_bits
= type_len
- offset
;
1721 this_size
= (this_size_bits
+ source_offset_bits
% 8 + 7) / 8;
1722 source_offset
= source_offset_bits
/ 8;
1723 if (buffer_size
< this_size
)
1725 buffer_size
= this_size
;
1726 buffer
.reserve (buffer_size
);
1728 intermediate_buffer
= buffer
.data ();
1730 /* Copy from the source to DEST_BUFFER. */
1731 switch (p
->location
)
1733 case DWARF_VALUE_REGISTER
:
1735 struct gdbarch
*arch
= get_frame_arch (frame
);
1736 int gdb_regnum
= dwarf_reg_to_regnum_or_error (arch
, p
->v
.regno
);
1738 LONGEST reg_offset
= source_offset
;
1740 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
1741 && this_size
< register_size (arch
, gdb_regnum
))
1743 /* Big-endian, and we want less than full size. */
1744 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
1745 /* We want the lower-order THIS_SIZE_BITS of the bytes
1746 we extract from the register. */
1747 source_offset_bits
+= 8 * this_size
- this_size_bits
;
1750 if (!get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1751 this_size
, buffer
.data (),
1754 /* Just so garbage doesn't ever shine through. */
1755 memset (buffer
.data (), 0, this_size
);
1758 mark_value_bits_optimized_out (v
, offset
, this_size_bits
);
1760 mark_value_bits_unavailable (v
, offset
, this_size_bits
);
1765 case DWARF_VALUE_MEMORY
:
1766 read_value_memory (v
, offset
,
1767 p
->v
.mem
.in_stack_memory
,
1768 p
->v
.mem
.addr
+ source_offset
,
1769 buffer
.data (), this_size
);
1772 case DWARF_VALUE_STACK
:
1774 size_t n
= this_size
;
1776 if (n
> c
->addr_size
- source_offset
)
1777 n
= (c
->addr_size
>= source_offset
1778 ? c
->addr_size
- source_offset
1786 const gdb_byte
*val_bytes
= value_contents_all (p
->v
.value
);
1788 intermediate_buffer
= val_bytes
+ source_offset
;
1793 case DWARF_VALUE_LITERAL
:
1795 size_t n
= this_size
;
1797 if (n
> p
->v
.literal
.length
- source_offset
)
1798 n
= (p
->v
.literal
.length
>= source_offset
1799 ? p
->v
.literal
.length
- source_offset
1802 intermediate_buffer
= p
->v
.literal
.data
+ source_offset
;
1806 /* These bits show up as zeros -- but do not cause the value
1807 to be considered optimized-out. */
1808 case DWARF_VALUE_IMPLICIT_POINTER
:
1811 case DWARF_VALUE_OPTIMIZED_OUT
:
1812 mark_value_bits_optimized_out (v
, offset
, this_size_bits
);
1816 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
1819 if (p
->location
!= DWARF_VALUE_OPTIMIZED_OUT
1820 && p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
1821 copy_bitwise (contents
, dest_offset_bits
,
1822 intermediate_buffer
, source_offset_bits
% 8,
1823 this_size_bits
, bits_big_endian
);
1825 offset
+= this_size_bits
;
1830 write_pieced_value (struct value
*to
, struct value
*from
)
1834 ULONGEST bits_to_skip
;
1835 const gdb_byte
*contents
;
1836 struct piece_closure
*c
1837 = (struct piece_closure
*) value_computed_closure (to
);
1838 struct frame_info
*frame
= frame_find_by_id (VALUE_FRAME_ID (to
));
1840 size_t buffer_size
= 0;
1841 std::vector
<gdb_byte
> buffer
;
1843 = gdbarch_bits_big_endian (get_type_arch (value_type (to
)));
1847 mark_value_bytes_optimized_out (to
, 0, TYPE_LENGTH (value_type (to
)));
1851 contents
= value_contents (from
);
1852 bits_to_skip
= 8 * value_offset (to
);
1853 if (value_bitsize (to
))
1855 bits_to_skip
+= value_bitpos (to
);
1856 type_len
= value_bitsize (to
);
1859 type_len
= 8 * TYPE_LENGTH (value_type (to
));
1861 for (i
= 0; i
< c
->n_pieces
&& offset
< type_len
; i
++)
1863 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1864 size_t this_size_bits
, this_size
;
1865 long dest_offset_bits
, source_offset_bits
, dest_offset
, source_offset
;
1867 const gdb_byte
*source_buffer
;
1869 this_size_bits
= p
->size
;
1870 if (bits_to_skip
> 0 && bits_to_skip
>= this_size_bits
)
1872 bits_to_skip
-= this_size_bits
;
1875 if (this_size_bits
> type_len
- offset
)
1876 this_size_bits
= type_len
- offset
;
1877 if (bits_to_skip
> 0)
1879 dest_offset_bits
= bits_to_skip
;
1880 source_offset_bits
= 0;
1881 this_size_bits
-= bits_to_skip
;
1886 dest_offset_bits
= 0;
1887 source_offset_bits
= offset
;
1890 this_size
= (this_size_bits
+ source_offset_bits
% 8 + 7) / 8;
1891 source_offset
= source_offset_bits
/ 8;
1892 dest_offset
= dest_offset_bits
/ 8;
1893 if (dest_offset_bits
% 8 == 0 && source_offset_bits
% 8 == 0)
1895 source_buffer
= contents
+ source_offset
;
1900 if (buffer_size
< this_size
)
1902 buffer_size
= this_size
;
1903 buffer
.reserve (buffer_size
);
1905 source_buffer
= buffer
.data ();
1909 switch (p
->location
)
1911 case DWARF_VALUE_REGISTER
:
1913 struct gdbarch
*arch
= get_frame_arch (frame
);
1914 int gdb_regnum
= dwarf_reg_to_regnum_or_error (arch
, p
->v
.regno
);
1915 int reg_offset
= dest_offset
;
1917 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_BIG
1918 && this_size
<= register_size (arch
, gdb_regnum
))
1920 /* Big-endian, and we want less than full size. */
1921 reg_offset
= register_size (arch
, gdb_regnum
) - this_size
;
1928 if (!get_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1929 this_size
, buffer
.data (),
1933 throw_error (OPTIMIZED_OUT_ERROR
,
1934 _("Can't do read-modify-write to "
1935 "update bitfield; containing word "
1936 "has been optimized out"));
1938 throw_error (NOT_AVAILABLE_ERROR
,
1939 _("Can't do read-modify-write to update "
1940 "bitfield; containing word "
1943 copy_bitwise (buffer
.data (), dest_offset_bits
,
1944 contents
, source_offset_bits
,
1949 put_frame_register_bytes (frame
, gdb_regnum
, reg_offset
,
1950 this_size
, source_buffer
);
1953 case DWARF_VALUE_MEMORY
:
1956 /* Only the first and last bytes can possibly have any
1958 read_memory (p
->v
.mem
.addr
+ dest_offset
, buffer
.data (), 1);
1959 read_memory (p
->v
.mem
.addr
+ dest_offset
+ this_size
- 1,
1960 &buffer
[this_size
- 1], 1);
1961 copy_bitwise (buffer
.data (), dest_offset_bits
,
1962 contents
, source_offset_bits
,
1967 write_memory (p
->v
.mem
.addr
+ dest_offset
,
1968 source_buffer
, this_size
);
1971 mark_value_bytes_optimized_out (to
, 0, TYPE_LENGTH (value_type (to
)));
1974 offset
+= this_size_bits
;
1978 /* An implementation of an lval_funcs method to see whether a value is
1979 a synthetic pointer. */
1982 check_pieced_synthetic_pointer (const struct value
*value
, LONGEST bit_offset
,
1985 struct piece_closure
*c
1986 = (struct piece_closure
*) value_computed_closure (value
);
1989 bit_offset
+= 8 * value_offset (value
);
1990 if (value_bitsize (value
))
1991 bit_offset
+= value_bitpos (value
);
1993 for (i
= 0; i
< c
->n_pieces
&& bit_length
> 0; i
++)
1995 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
1996 size_t this_size_bits
= p
->size
;
2000 if (bit_offset
>= this_size_bits
)
2002 bit_offset
-= this_size_bits
;
2006 bit_length
-= this_size_bits
- bit_offset
;
2010 bit_length
-= this_size_bits
;
2012 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
2019 /* A wrapper function for get_frame_address_in_block. */
2022 get_frame_address_in_block_wrapper (void *baton
)
2024 return get_frame_address_in_block ((struct frame_info
*) baton
);
2027 /* Fetch a DW_AT_const_value through a synthetic pointer. */
2029 static struct value
*
2030 fetch_const_value_from_synthetic_pointer (sect_offset die
, LONGEST byte_offset
,
2031 struct dwarf2_per_cu_data
*per_cu
,
2034 struct value
*result
= NULL
;
2035 struct obstack temp_obstack
;
2036 struct cleanup
*cleanup
;
2037 const gdb_byte
*bytes
;
2040 obstack_init (&temp_obstack
);
2041 cleanup
= make_cleanup_obstack_free (&temp_obstack
);
2042 bytes
= dwarf2_fetch_constant_bytes (die
, per_cu
, &temp_obstack
, &len
);
2046 if (byte_offset
>= 0
2047 && byte_offset
+ TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) <= len
)
2049 bytes
+= byte_offset
;
2050 result
= value_from_contents (TYPE_TARGET_TYPE (type
), bytes
);
2053 invalid_synthetic_pointer ();
2056 result
= allocate_optimized_out_value (TYPE_TARGET_TYPE (type
));
2058 do_cleanups (cleanup
);
2063 /* Fetch the value pointed to by a synthetic pointer. */
2065 static struct value
*
2066 indirect_synthetic_pointer (sect_offset die
, LONGEST byte_offset
,
2067 struct dwarf2_per_cu_data
*per_cu
,
2068 struct frame_info
*frame
, struct type
*type
)
2070 /* Fetch the location expression of the DIE we're pointing to. */
2071 struct dwarf2_locexpr_baton baton
2072 = dwarf2_fetch_die_loc_sect_off (die
, per_cu
,
2073 get_frame_address_in_block_wrapper
, frame
);
2075 /* If pointed-to DIE has a DW_AT_location, evaluate it and return the
2076 resulting value. Otherwise, it may have a DW_AT_const_value instead,
2077 or it may've been optimized out. */
2078 if (baton
.data
!= NULL
)
2079 return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type
), frame
,
2080 baton
.data
, baton
.size
, baton
.per_cu
,
2083 return fetch_const_value_from_synthetic_pointer (die
, byte_offset
, per_cu
,
2087 /* An implementation of an lval_funcs method to indirect through a
2088 pointer. This handles the synthetic pointer case when needed. */
2090 static struct value
*
2091 indirect_pieced_value (struct value
*value
)
2093 struct piece_closure
*c
2094 = (struct piece_closure
*) value_computed_closure (value
);
2096 struct frame_info
*frame
;
2097 struct dwarf2_locexpr_baton baton
;
2100 struct dwarf_expr_piece
*piece
= NULL
;
2101 LONGEST byte_offset
;
2102 enum bfd_endian byte_order
;
2104 type
= check_typedef (value_type (value
));
2105 if (TYPE_CODE (type
) != TYPE_CODE_PTR
)
2108 bit_length
= 8 * TYPE_LENGTH (type
);
2109 bit_offset
= 8 * value_offset (value
);
2110 if (value_bitsize (value
))
2111 bit_offset
+= value_bitpos (value
);
2113 for (i
= 0; i
< c
->n_pieces
&& bit_length
> 0; i
++)
2115 struct dwarf_expr_piece
*p
= &c
->pieces
[i
];
2116 size_t this_size_bits
= p
->size
;
2120 if (bit_offset
>= this_size_bits
)
2122 bit_offset
-= this_size_bits
;
2126 bit_length
-= this_size_bits
- bit_offset
;
2130 bit_length
-= this_size_bits
;
2132 if (p
->location
!= DWARF_VALUE_IMPLICIT_POINTER
)
2135 if (bit_length
!= 0)
2136 error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
2142 gdb_assert (piece
!= NULL
);
2143 frame
= get_selected_frame (_("No frame selected."));
2145 /* This is an offset requested by GDB, such as value subscripts.
2146 However, due to how synthetic pointers are implemented, this is
2147 always presented to us as a pointer type. This means we have to
2148 sign-extend it manually as appropriate. Use raw
2149 extract_signed_integer directly rather than value_as_address and
2150 sign extend afterwards on architectures that would need it
2151 (mostly everywhere except MIPS, which has signed addresses) as
2152 the later would go through gdbarch_pointer_to_address and thus
2153 return a CORE_ADDR with high bits set on architectures that
2154 encode address spaces and other things in CORE_ADDR. */
2155 byte_order
= gdbarch_byte_order (get_frame_arch (frame
));
2156 byte_offset
= extract_signed_integer (value_contents (value
),
2157 TYPE_LENGTH (type
), byte_order
);
2158 byte_offset
+= piece
->v
.ptr
.offset
;
2160 return indirect_synthetic_pointer (piece
->v
.ptr
.die
, byte_offset
, c
->per_cu
,
2164 /* Implementation of the coerce_ref method of lval_funcs for synthetic C++
2167 static struct value
*
2168 coerce_pieced_ref (const struct value
*value
)
2170 struct type
*type
= check_typedef (value_type (value
));
2172 if (value_bits_synthetic_pointer (value
, value_embedded_offset (value
),
2173 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
2175 const struct piece_closure
*closure
2176 = (struct piece_closure
*) value_computed_closure (value
);
2177 struct frame_info
*frame
2178 = get_selected_frame (_("No frame selected."));
2180 /* gdb represents synthetic pointers as pieced values with a single
2182 gdb_assert (closure
!= NULL
);
2183 gdb_assert (closure
->n_pieces
== 1);
2185 return indirect_synthetic_pointer (closure
->pieces
->v
.ptr
.die
,
2186 closure
->pieces
->v
.ptr
.offset
,
2187 closure
->per_cu
, frame
, type
);
2191 /* Else: not a synthetic reference; do nothing. */
2197 copy_pieced_value_closure (const struct value
*v
)
2199 struct piece_closure
*c
2200 = (struct piece_closure
*) value_computed_closure (v
);
2207 free_pieced_value_closure (struct value
*v
)
2209 struct piece_closure
*c
2210 = (struct piece_closure
*) value_computed_closure (v
);
2217 for (i
= 0; i
< c
->n_pieces
; ++i
)
2218 if (c
->pieces
[i
].location
== DWARF_VALUE_STACK
)
2219 value_free (c
->pieces
[i
].v
.value
);
2226 /* Functions for accessing a variable described by DW_OP_piece. */
2227 static const struct lval_funcs pieced_value_funcs
= {
2230 indirect_pieced_value
,
2232 check_pieced_synthetic_pointer
,
2233 copy_pieced_value_closure
,
2234 free_pieced_value_closure
2237 /* Evaluate a location description, starting at DATA and with length
2238 SIZE, to find the current location of variable of TYPE in the
2239 context of FRAME. BYTE_OFFSET is applied after the contents are
2242 static struct value
*
2243 dwarf2_evaluate_loc_desc_full (struct type
*type
, struct frame_info
*frame
,
2244 const gdb_byte
*data
, size_t size
,
2245 struct dwarf2_per_cu_data
*per_cu
,
2246 LONGEST byte_offset
)
2248 struct value
*retval
;
2249 struct cleanup
*value_chain
;
2250 struct objfile
*objfile
= dwarf2_per_cu_objfile (per_cu
);
2252 if (byte_offset
< 0)
2253 invalid_synthetic_pointer ();
2256 return allocate_optimized_out_value (type
);
2258 dwarf_evaluate_loc_desc ctx
;
2260 ctx
.per_cu
= per_cu
;
2261 ctx
.obj_address
= 0;
2263 value_chain
= make_cleanup_value_free_to_mark (value_mark ());
2265 ctx
.gdbarch
= get_objfile_arch (objfile
);
2266 ctx
.addr_size
= dwarf2_per_cu_addr_size (per_cu
);
2267 ctx
.ref_addr_size
= dwarf2_per_cu_ref_addr_size (per_cu
);
2268 ctx
.offset
= dwarf2_per_cu_text_offset (per_cu
);
2272 ctx
.eval (data
, size
);
2274 CATCH (ex
, RETURN_MASK_ERROR
)
2276 if (ex
.error
== NOT_AVAILABLE_ERROR
)
2278 do_cleanups (value_chain
);
2279 retval
= allocate_value (type
);
2280 mark_value_bytes_unavailable (retval
, 0, TYPE_LENGTH (type
));
2283 else if (ex
.error
== NO_ENTRY_VALUE_ERROR
)
2285 if (entry_values_debug
)
2286 exception_print (gdb_stdout
, ex
);
2287 do_cleanups (value_chain
);
2288 return allocate_optimized_out_value (type
);
2291 throw_exception (ex
);
2295 if (ctx
.num_pieces
> 0)
2297 struct piece_closure
*c
;
2298 struct frame_id frame_id
= get_frame_id (frame
);
2299 ULONGEST bit_size
= 0;
2302 for (i
= 0; i
< ctx
.num_pieces
; ++i
)
2303 bit_size
+= ctx
.pieces
[i
].size
;
2304 if (8 * (byte_offset
+ TYPE_LENGTH (type
)) > bit_size
)
2305 invalid_synthetic_pointer ();
2307 c
= allocate_piece_closure (per_cu
, ctx
.num_pieces
, ctx
.pieces
,
2309 /* We must clean up the value chain after creating the piece
2310 closure but before allocating the result. */
2311 do_cleanups (value_chain
);
2312 retval
= allocate_computed_value (type
, &pieced_value_funcs
, c
);
2313 VALUE_FRAME_ID (retval
) = frame_id
;
2314 set_value_offset (retval
, byte_offset
);
2318 switch (ctx
.location
)
2320 case DWARF_VALUE_REGISTER
:
2322 struct gdbarch
*arch
= get_frame_arch (frame
);
2324 = longest_to_int (value_as_long (ctx
.fetch (0)));
2325 int gdb_regnum
= dwarf_reg_to_regnum_or_error (arch
, dwarf_regnum
);
2327 if (byte_offset
!= 0)
2328 error (_("cannot use offset on synthetic pointer to register"));
2329 do_cleanups (value_chain
);
2330 retval
= value_from_register (type
, gdb_regnum
, frame
);
2331 if (value_optimized_out (retval
))
2335 /* This means the register has undefined value / was
2336 not saved. As we're computing the location of some
2337 variable etc. in the program, not a value for
2338 inspecting a register ($pc, $sp, etc.), return a
2339 generic optimized out value instead, so that we show
2340 <optimized out> instead of <not saved>. */
2341 do_cleanups (value_chain
);
2342 tmp
= allocate_value (type
);
2343 value_contents_copy (tmp
, 0, retval
, 0, TYPE_LENGTH (type
));
2349 case DWARF_VALUE_MEMORY
:
2351 struct type
*ptr_type
;
2352 CORE_ADDR address
= ctx
.fetch_address (0);
2353 int in_stack_memory
= ctx
.fetch_in_stack_memory (0);
2355 /* DW_OP_deref_size (and possibly other operations too) may
2356 create a pointer instead of an address. Ideally, the
2357 pointer to address conversion would be performed as part
2358 of those operations, but the type of the object to
2359 which the address refers is not known at the time of
2360 the operation. Therefore, we do the conversion here
2361 since the type is readily available. */
2363 switch (TYPE_CODE (type
))
2365 case TYPE_CODE_FUNC
:
2366 case TYPE_CODE_METHOD
:
2367 ptr_type
= builtin_type (ctx
.gdbarch
)->builtin_func_ptr
;
2370 ptr_type
= builtin_type (ctx
.gdbarch
)->builtin_data_ptr
;
2373 address
= value_as_address (value_from_pointer (ptr_type
, address
));
2375 do_cleanups (value_chain
);
2376 retval
= value_at_lazy (type
, address
+ byte_offset
);
2377 if (in_stack_memory
)
2378 set_value_stack (retval
, 1);
2382 case DWARF_VALUE_STACK
:
2384 struct value
*value
= ctx
.fetch (0);
2386 const gdb_byte
*val_bytes
;
2387 size_t n
= TYPE_LENGTH (value_type (value
));
2389 if (byte_offset
+ TYPE_LENGTH (type
) > n
)
2390 invalid_synthetic_pointer ();
2392 val_bytes
= value_contents_all (value
);
2393 val_bytes
+= byte_offset
;
2396 /* Preserve VALUE because we are going to free values back
2397 to the mark, but we still need the value contents
2399 value_incref (value
);
2400 do_cleanups (value_chain
);
2401 make_cleanup_value_free (value
);
2403 retval
= allocate_value (type
);
2404 contents
= value_contents_raw (retval
);
2405 if (n
> TYPE_LENGTH (type
))
2407 struct gdbarch
*objfile_gdbarch
= get_objfile_arch (objfile
);
2409 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
2410 val_bytes
+= n
- TYPE_LENGTH (type
);
2411 n
= TYPE_LENGTH (type
);
2413 memcpy (contents
, val_bytes
, n
);
2417 case DWARF_VALUE_LITERAL
:
2420 const bfd_byte
*ldata
;
2423 if (byte_offset
+ TYPE_LENGTH (type
) > n
)
2424 invalid_synthetic_pointer ();
2426 do_cleanups (value_chain
);
2427 retval
= allocate_value (type
);
2428 contents
= value_contents_raw (retval
);
2430 ldata
= ctx
.data
+ byte_offset
;
2433 if (n
> TYPE_LENGTH (type
))
2435 struct gdbarch
*objfile_gdbarch
= get_objfile_arch (objfile
);
2437 if (gdbarch_byte_order (objfile_gdbarch
) == BFD_ENDIAN_BIG
)
2438 ldata
+= n
- TYPE_LENGTH (type
);
2439 n
= TYPE_LENGTH (type
);
2441 memcpy (contents
, ldata
, n
);
2445 case DWARF_VALUE_OPTIMIZED_OUT
:
2446 do_cleanups (value_chain
);
2447 retval
= allocate_optimized_out_value (type
);
2450 /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
2451 operation by execute_stack_op. */
2452 case DWARF_VALUE_IMPLICIT_POINTER
:
2453 /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
2454 it can only be encountered when making a piece. */
2456 internal_error (__FILE__
, __LINE__
, _("invalid location type"));
2460 set_value_initialized (retval
, ctx
.initialized
);
2462 do_cleanups (value_chain
);
2467 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
2468 passes 0 as the byte_offset. */
2471 dwarf2_evaluate_loc_desc (struct type
*type
, struct frame_info
*frame
,
2472 const gdb_byte
*data
, size_t size
,
2473 struct dwarf2_per_cu_data
*per_cu
)
2475 return dwarf2_evaluate_loc_desc_full (type
, frame
, data
, size
, per_cu
, 0);
2478 /* Evaluates a dwarf expression and stores the result in VAL, expecting
2479 that the dwarf expression only produces a single CORE_ADDR. FRAME is the
2480 frame in which the expression is evaluated. ADDR is a context (location of
2481 a variable) and might be needed to evaluate the location expression.
2482 Returns 1 on success, 0 otherwise. */
2485 dwarf2_locexpr_baton_eval (const struct dwarf2_locexpr_baton
*dlbaton
,
2486 struct frame_info
*frame
,
2490 struct objfile
*objfile
;
2491 struct cleanup
*cleanup
;
2493 if (dlbaton
== NULL
|| dlbaton
->size
== 0)
2496 dwarf_evaluate_loc_desc ctx
;
2499 ctx
.per_cu
= dlbaton
->per_cu
;
2500 ctx
.obj_address
= addr
;
2502 objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
2504 ctx
.gdbarch
= get_objfile_arch (objfile
);
2505 ctx
.addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
2506 ctx
.ref_addr_size
= dwarf2_per_cu_ref_addr_size (dlbaton
->per_cu
);
2507 ctx
.offset
= dwarf2_per_cu_text_offset (dlbaton
->per_cu
);
2509 ctx
.eval (dlbaton
->data
, dlbaton
->size
);
2511 switch (ctx
.location
)
2513 case DWARF_VALUE_REGISTER
:
2514 case DWARF_VALUE_MEMORY
:
2515 case DWARF_VALUE_STACK
:
2516 *valp
= ctx
.fetch_address (0);
2517 if (ctx
.location
== DWARF_VALUE_REGISTER
)
2518 *valp
= ctx
.read_addr_from_reg (*valp
);
2520 case DWARF_VALUE_LITERAL
:
2521 *valp
= extract_signed_integer (ctx
.data
, ctx
.len
,
2522 gdbarch_byte_order (ctx
.gdbarch
));
2524 /* Unsupported dwarf values. */
2525 case DWARF_VALUE_OPTIMIZED_OUT
:
2526 case DWARF_VALUE_IMPLICIT_POINTER
:
2533 /* See dwarf2loc.h. */
2536 dwarf2_evaluate_property (const struct dynamic_prop
*prop
,
2537 struct frame_info
*frame
,
2538 struct property_addr_info
*addr_stack
,
2544 if (frame
== NULL
&& has_stack_frames ())
2545 frame
= get_selected_frame (NULL
);
2551 const struct dwarf2_property_baton
*baton
2552 = (const struct dwarf2_property_baton
*) prop
->data
.baton
;
2554 if (dwarf2_locexpr_baton_eval (&baton
->locexpr
, frame
,
2555 addr_stack
? addr_stack
->addr
: 0,
2558 if (baton
->referenced_type
)
2560 struct value
*val
= value_at (baton
->referenced_type
, *value
);
2562 *value
= value_as_address (val
);
2571 struct dwarf2_property_baton
*baton
2572 = (struct dwarf2_property_baton
*) prop
->data
.baton
;
2573 CORE_ADDR pc
= get_frame_address_in_block (frame
);
2574 const gdb_byte
*data
;
2578 data
= dwarf2_find_location_expression (&baton
->loclist
, &size
, pc
);
2581 val
= dwarf2_evaluate_loc_desc (baton
->referenced_type
, frame
, data
,
2582 size
, baton
->loclist
.per_cu
);
2583 if (!value_optimized_out (val
))
2585 *value
= value_as_address (val
);
2593 *value
= prop
->data
.const_val
;
2596 case PROP_ADDR_OFFSET
:
2598 struct dwarf2_property_baton
*baton
2599 = (struct dwarf2_property_baton
*) prop
->data
.baton
;
2600 struct property_addr_info
*pinfo
;
2603 for (pinfo
= addr_stack
; pinfo
!= NULL
; pinfo
= pinfo
->next
)
2604 if (pinfo
->type
== baton
->referenced_type
)
2607 error (_("cannot find reference address for offset property"));
2608 if (pinfo
->valaddr
!= NULL
)
2609 val
= value_from_contents
2610 (baton
->offset_info
.type
,
2611 pinfo
->valaddr
+ baton
->offset_info
.offset
);
2613 val
= value_at (baton
->offset_info
.type
,
2614 pinfo
->addr
+ baton
->offset_info
.offset
);
2615 *value
= value_as_address (val
);
2623 /* See dwarf2loc.h. */
2626 dwarf2_compile_property_to_c (struct ui_file
*stream
,
2627 const char *result_name
,
2628 struct gdbarch
*gdbarch
,
2629 unsigned char *registers_used
,
2630 const struct dynamic_prop
*prop
,
2634 struct dwarf2_property_baton
*baton
2635 = (struct dwarf2_property_baton
*) prop
->data
.baton
;
2636 const gdb_byte
*data
;
2638 struct dwarf2_per_cu_data
*per_cu
;
2640 if (prop
->kind
== PROP_LOCEXPR
)
2642 data
= baton
->locexpr
.data
;
2643 size
= baton
->locexpr
.size
;
2644 per_cu
= baton
->locexpr
.per_cu
;
2648 gdb_assert (prop
->kind
== PROP_LOCLIST
);
2650 data
= dwarf2_find_location_expression (&baton
->loclist
, &size
, pc
);
2651 per_cu
= baton
->loclist
.per_cu
;
2654 compile_dwarf_bounds_to_c (stream
, result_name
, prop
, sym
, pc
,
2655 gdbarch
, registers_used
,
2656 dwarf2_per_cu_addr_size (per_cu
),
2657 data
, data
+ size
, per_cu
);
2661 /* Helper functions and baton for dwarf2_loc_desc_get_symbol_read_needs. */
2663 class symbol_needs_eval_context
: public dwarf_expr_context
2667 enum symbol_needs_kind needs
;
2668 struct dwarf2_per_cu_data
*per_cu
;
2670 /* Reads from registers do require a frame. */
2671 CORE_ADDR
read_addr_from_reg (int regnum
) OVERRIDE
2673 needs
= SYMBOL_NEEDS_FRAME
;
2677 /* "get_reg_value" callback: Reads from registers do require a
2680 struct value
*get_reg_value (struct type
*type
, int regnum
) OVERRIDE
2682 needs
= SYMBOL_NEEDS_FRAME
;
2683 return value_zero (type
, not_lval
);
2686 /* Reads from memory do not require a frame. */
2687 void read_mem (gdb_byte
*buf
, CORE_ADDR addr
, size_t len
) OVERRIDE
2689 memset (buf
, 0, len
);
2692 /* Frame-relative accesses do require a frame. */
2693 void get_frame_base (const gdb_byte
**start
, size_t *length
) OVERRIDE
2695 static gdb_byte lit0
= DW_OP_lit0
;
2700 needs
= SYMBOL_NEEDS_FRAME
;
2703 /* CFA accesses require a frame. */
2704 CORE_ADDR
get_frame_cfa () OVERRIDE
2706 needs
= SYMBOL_NEEDS_FRAME
;
2710 /* Thread-local accesses require registers, but not a frame. */
2711 CORE_ADDR
get_tls_address (CORE_ADDR offset
) OVERRIDE
2713 if (needs
<= SYMBOL_NEEDS_REGISTERS
)
2714 needs
= SYMBOL_NEEDS_REGISTERS
;
2718 /* Helper interface of per_cu_dwarf_call for
2719 dwarf2_loc_desc_get_symbol_read_needs. */
2721 void dwarf_call (cu_offset die_offset
) OVERRIDE
2723 per_cu_dwarf_call (this, die_offset
, per_cu
);
2726 /* DW_OP_GNU_entry_value accesses require a caller, therefore a
2729 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind
,
2730 union call_site_parameter_u kind_u
,
2731 int deref_size
) OVERRIDE
2733 needs
= SYMBOL_NEEDS_FRAME
;
2735 /* The expression may require some stub values on DWARF stack. */
2736 push_address (0, 0);
2739 /* DW_OP_GNU_addr_index doesn't require a frame. */
2741 CORE_ADDR
get_addr_index (unsigned int index
) OVERRIDE
2743 /* Nothing to do. */
2747 /* DW_OP_push_object_address has a frame already passed through. */
2749 CORE_ADDR
get_object_address () OVERRIDE
2751 /* Nothing to do. */
2756 /* Compute the correct symbol_needs_kind value for the location
2757 expression at DATA (length SIZE). */
2759 static enum symbol_needs_kind
2760 dwarf2_loc_desc_get_symbol_read_needs (const gdb_byte
*data
, size_t size
,
2761 struct dwarf2_per_cu_data
*per_cu
)
2764 struct cleanup
*old_chain
;
2765 struct objfile
*objfile
= dwarf2_per_cu_objfile (per_cu
);
2767 symbol_needs_eval_context ctx
;
2769 ctx
.needs
= SYMBOL_NEEDS_NONE
;
2770 ctx
.per_cu
= per_cu
;
2772 old_chain
= make_cleanup_value_free_to_mark (value_mark ());
2774 ctx
.gdbarch
= get_objfile_arch (objfile
);
2775 ctx
.addr_size
= dwarf2_per_cu_addr_size (per_cu
);
2776 ctx
.ref_addr_size
= dwarf2_per_cu_ref_addr_size (per_cu
);
2777 ctx
.offset
= dwarf2_per_cu_text_offset (per_cu
);
2779 ctx
.eval (data
, size
);
2781 in_reg
= ctx
.location
== DWARF_VALUE_REGISTER
;
2783 if (ctx
.num_pieces
> 0)
2787 /* If the location has several pieces, and any of them are in
2788 registers, then we will need a frame to fetch them from. */
2789 for (i
= 0; i
< ctx
.num_pieces
; i
++)
2790 if (ctx
.pieces
[i
].location
== DWARF_VALUE_REGISTER
)
2794 do_cleanups (old_chain
);
2797 ctx
.needs
= SYMBOL_NEEDS_FRAME
;
2801 /* A helper function that throws an unimplemented error mentioning a
2802 given DWARF operator. */
2805 unimplemented (unsigned int op
)
2807 const char *name
= get_DW_OP_name (op
);
2810 error (_("DWARF operator %s cannot be translated to an agent expression"),
2813 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2814 "to an agent expression"),
2820 This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2821 can issue a complaint, which is better than having every target's
2822 implementation of dwarf2_reg_to_regnum do it. */
2825 dwarf_reg_to_regnum (struct gdbarch
*arch
, int dwarf_reg
)
2827 int reg
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_reg
);
2831 complaint (&symfile_complaints
,
2832 _("bad DWARF register number %d"), dwarf_reg
);
2837 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2838 Throw an error because DWARF_REG is bad. */
2841 throw_bad_regnum_error (ULONGEST dwarf_reg
)
2843 /* Still want to print -1 as "-1".
2844 We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2845 but that's overkill for now. */
2846 if ((int) dwarf_reg
== dwarf_reg
)
2847 error (_("Unable to access DWARF register number %d"), (int) dwarf_reg
);
2848 error (_("Unable to access DWARF register number %s"),
2849 pulongest (dwarf_reg
));
2852 /* See dwarf2loc.h. */
2855 dwarf_reg_to_regnum_or_error (struct gdbarch
*arch
, ULONGEST dwarf_reg
)
2859 if (dwarf_reg
> INT_MAX
)
2860 throw_bad_regnum_error (dwarf_reg
);
2861 /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2862 bad, but that's ok. */
2863 reg
= dwarf_reg_to_regnum (arch
, (int) dwarf_reg
);
2865 throw_bad_regnum_error (dwarf_reg
);
2869 /* A helper function that emits an access to memory. ARCH is the
2870 target architecture. EXPR is the expression which we are building.
2871 NBITS is the number of bits we want to read. This emits the
2872 opcodes needed to read the memory and then extract the desired
2876 access_memory (struct gdbarch
*arch
, struct agent_expr
*expr
, ULONGEST nbits
)
2878 ULONGEST nbytes
= (nbits
+ 7) / 8;
2880 gdb_assert (nbytes
> 0 && nbytes
<= sizeof (LONGEST
));
2883 ax_trace_quick (expr
, nbytes
);
2886 ax_simple (expr
, aop_ref8
);
2887 else if (nbits
<= 16)
2888 ax_simple (expr
, aop_ref16
);
2889 else if (nbits
<= 32)
2890 ax_simple (expr
, aop_ref32
);
2892 ax_simple (expr
, aop_ref64
);
2894 /* If we read exactly the number of bytes we wanted, we're done. */
2895 if (8 * nbytes
== nbits
)
2898 if (gdbarch_bits_big_endian (arch
))
2900 /* On a bits-big-endian machine, we want the high-order
2902 ax_const_l (expr
, 8 * nbytes
- nbits
);
2903 ax_simple (expr
, aop_rsh_unsigned
);
2907 /* On a bits-little-endian box, we want the low-order NBITS. */
2908 ax_zero_ext (expr
, nbits
);
2912 /* A helper function to return the frame's PC. */
2915 get_ax_pc (void *baton
)
2917 struct agent_expr
*expr
= (struct agent_expr
*) baton
;
2922 /* Compile a DWARF location expression to an agent expression.
2924 EXPR is the agent expression we are building.
2925 LOC is the agent value we modify.
2926 ARCH is the architecture.
2927 ADDR_SIZE is the size of addresses, in bytes.
2928 OP_PTR is the start of the location expression.
2929 OP_END is one past the last byte of the location expression.
2931 This will throw an exception for various kinds of errors -- for
2932 example, if the expression cannot be compiled, or if the expression
2936 dwarf2_compile_expr_to_ax (struct agent_expr
*expr
, struct axs_value
*loc
,
2937 struct gdbarch
*arch
, unsigned int addr_size
,
2938 const gdb_byte
*op_ptr
, const gdb_byte
*op_end
,
2939 struct dwarf2_per_cu_data
*per_cu
)
2942 std::vector
<int> dw_labels
, patches
;
2943 const gdb_byte
* const base
= op_ptr
;
2944 const gdb_byte
*previous_piece
= op_ptr
;
2945 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
2946 ULONGEST bits_collected
= 0;
2947 unsigned int addr_size_bits
= 8 * addr_size
;
2948 int bits_big_endian
= gdbarch_bits_big_endian (arch
);
2950 std::vector
<int> offsets (op_end
- op_ptr
, -1);
2952 /* By default we are making an address. */
2953 loc
->kind
= axs_lvalue_memory
;
2955 while (op_ptr
< op_end
)
2957 enum dwarf_location_atom op
= (enum dwarf_location_atom
) *op_ptr
;
2958 uint64_t uoffset
, reg
;
2962 offsets
[op_ptr
- base
] = expr
->len
;
2965 /* Our basic approach to code generation is to map DWARF
2966 operations directly to AX operations. However, there are
2969 First, DWARF works on address-sized units, but AX always uses
2970 LONGEST. For most operations we simply ignore this
2971 difference; instead we generate sign extensions as needed
2972 before division and comparison operations. It would be nice
2973 to omit the sign extensions, but there is no way to determine
2974 the size of the target's LONGEST. (This code uses the size
2975 of the host LONGEST in some cases -- that is a bug but it is
2978 Second, some DWARF operations cannot be translated to AX.
2979 For these we simply fail. See
2980 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
3015 ax_const_l (expr
, op
- DW_OP_lit0
);
3019 uoffset
= extract_unsigned_integer (op_ptr
, addr_size
, byte_order
);
3020 op_ptr
+= addr_size
;
3021 /* Some versions of GCC emit DW_OP_addr before
3022 DW_OP_GNU_push_tls_address. In this case the value is an
3023 index, not an address. We don't support things like
3024 branching between the address and the TLS op. */
3025 if (op_ptr
>= op_end
|| *op_ptr
!= DW_OP_GNU_push_tls_address
)
3026 uoffset
+= dwarf2_per_cu_text_offset (per_cu
);
3027 ax_const_l (expr
, uoffset
);
3031 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 1, byte_order
));
3035 ax_const_l (expr
, extract_signed_integer (op_ptr
, 1, byte_order
));
3039 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 2, byte_order
));
3043 ax_const_l (expr
, extract_signed_integer (op_ptr
, 2, byte_order
));
3047 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 4, byte_order
));
3051 ax_const_l (expr
, extract_signed_integer (op_ptr
, 4, byte_order
));
3055 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, 8, byte_order
));
3059 ax_const_l (expr
, extract_signed_integer (op_ptr
, 8, byte_order
));
3063 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &uoffset
);
3064 ax_const_l (expr
, uoffset
);
3067 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
3068 ax_const_l (expr
, offset
);
3103 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
3104 loc
->u
.reg
= dwarf_reg_to_regnum_or_error (arch
, op
- DW_OP_reg0
);
3105 loc
->kind
= axs_lvalue_register
;
3109 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
3110 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_regx");
3111 loc
->u
.reg
= dwarf_reg_to_regnum_or_error (arch
, reg
);
3112 loc
->kind
= axs_lvalue_register
;
3115 case DW_OP_implicit_value
:
3119 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &len
);
3120 if (op_ptr
+ len
> op_end
)
3121 error (_("DW_OP_implicit_value: too few bytes available."));
3122 if (len
> sizeof (ULONGEST
))
3123 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3126 ax_const_l (expr
, extract_unsigned_integer (op_ptr
, len
,
3129 dwarf_expr_require_composition (op_ptr
, op_end
,
3130 "DW_OP_implicit_value");
3132 loc
->kind
= axs_rvalue
;
3136 case DW_OP_stack_value
:
3137 dwarf_expr_require_composition (op_ptr
, op_end
, "DW_OP_stack_value");
3138 loc
->kind
= axs_rvalue
;
3173 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
3174 i
= dwarf_reg_to_regnum_or_error (arch
, op
- DW_OP_breg0
);
3178 ax_const_l (expr
, offset
);
3179 ax_simple (expr
, aop_add
);
3184 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
3185 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
3186 i
= dwarf_reg_to_regnum_or_error (arch
, reg
);
3190 ax_const_l (expr
, offset
);
3191 ax_simple (expr
, aop_add
);
3197 const gdb_byte
*datastart
;
3199 const struct block
*b
;
3200 struct symbol
*framefunc
;
3202 b
= block_for_pc (expr
->scope
);
3205 error (_("No block found for address"));
3207 framefunc
= block_linkage_function (b
);
3210 error (_("No function found for block"));
3212 func_get_frame_base_dwarf_block (framefunc
, expr
->scope
,
3213 &datastart
, &datalen
);
3215 op_ptr
= safe_read_sleb128 (op_ptr
, op_end
, &offset
);
3216 dwarf2_compile_expr_to_ax (expr
, loc
, arch
, addr_size
, datastart
,
3217 datastart
+ datalen
, per_cu
);
3218 if (loc
->kind
== axs_lvalue_register
)
3219 require_rvalue (expr
, loc
);
3223 ax_const_l (expr
, offset
);
3224 ax_simple (expr
, aop_add
);
3227 loc
->kind
= axs_lvalue_memory
;
3232 ax_simple (expr
, aop_dup
);
3236 ax_simple (expr
, aop_pop
);
3241 ax_pick (expr
, offset
);
3245 ax_simple (expr
, aop_swap
);
3253 ax_simple (expr
, aop_rot
);
3257 case DW_OP_deref_size
:
3261 if (op
== DW_OP_deref_size
)
3266 if (size
!= 1 && size
!= 2 && size
!= 4 && size
!= 8)
3267 error (_("Unsupported size %d in %s"),
3268 size
, get_DW_OP_name (op
));
3269 access_memory (arch
, expr
, size
* TARGET_CHAR_BIT
);
3274 /* Sign extend the operand. */
3275 ax_ext (expr
, addr_size_bits
);
3276 ax_simple (expr
, aop_dup
);
3277 ax_const_l (expr
, 0);
3278 ax_simple (expr
, aop_less_signed
);
3279 ax_simple (expr
, aop_log_not
);
3280 i
= ax_goto (expr
, aop_if_goto
);
3281 /* We have to emit 0 - X. */
3282 ax_const_l (expr
, 0);
3283 ax_simple (expr
, aop_swap
);
3284 ax_simple (expr
, aop_sub
);
3285 ax_label (expr
, i
, expr
->len
);
3289 /* No need to sign extend here. */
3290 ax_const_l (expr
, 0);
3291 ax_simple (expr
, aop_swap
);
3292 ax_simple (expr
, aop_sub
);
3296 /* Sign extend the operand. */
3297 ax_ext (expr
, addr_size_bits
);
3298 ax_simple (expr
, aop_bit_not
);
3301 case DW_OP_plus_uconst
:
3302 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, ®
);
3303 /* It would be really weird to emit `DW_OP_plus_uconst 0',
3304 but we micro-optimize anyhow. */
3307 ax_const_l (expr
, reg
);
3308 ax_simple (expr
, aop_add
);
3313 ax_simple (expr
, aop_bit_and
);
3317 /* Sign extend the operands. */
3318 ax_ext (expr
, addr_size_bits
);
3319 ax_simple (expr
, aop_swap
);
3320 ax_ext (expr
, addr_size_bits
);
3321 ax_simple (expr
, aop_swap
);
3322 ax_simple (expr
, aop_div_signed
);
3326 ax_simple (expr
, aop_sub
);
3330 ax_simple (expr
, aop_rem_unsigned
);
3334 ax_simple (expr
, aop_mul
);
3338 ax_simple (expr
, aop_bit_or
);
3342 ax_simple (expr
, aop_add
);
3346 ax_simple (expr
, aop_lsh
);
3350 ax_simple (expr
, aop_rsh_unsigned
);
3354 ax_simple (expr
, aop_rsh_signed
);
3358 ax_simple (expr
, aop_bit_xor
);
3362 /* Sign extend the operands. */
3363 ax_ext (expr
, addr_size_bits
);
3364 ax_simple (expr
, aop_swap
);
3365 ax_ext (expr
, addr_size_bits
);
3366 /* Note no swap here: A <= B is !(B < A). */
3367 ax_simple (expr
, aop_less_signed
);
3368 ax_simple (expr
, aop_log_not
);
3372 /* Sign extend the operands. */
3373 ax_ext (expr
, addr_size_bits
);
3374 ax_simple (expr
, aop_swap
);
3375 ax_ext (expr
, addr_size_bits
);
3376 ax_simple (expr
, aop_swap
);
3377 /* A >= B is !(A < B). */
3378 ax_simple (expr
, aop_less_signed
);
3379 ax_simple (expr
, aop_log_not
);
3383 /* Sign extend the operands. */
3384 ax_ext (expr
, addr_size_bits
);
3385 ax_simple (expr
, aop_swap
);
3386 ax_ext (expr
, addr_size_bits
);
3387 /* No need for a second swap here. */
3388 ax_simple (expr
, aop_equal
);
3392 /* Sign extend the operands. */
3393 ax_ext (expr
, addr_size_bits
);
3394 ax_simple (expr
, aop_swap
);
3395 ax_ext (expr
, addr_size_bits
);
3396 ax_simple (expr
, aop_swap
);
3397 ax_simple (expr
, aop_less_signed
);
3401 /* Sign extend the operands. */
3402 ax_ext (expr
, addr_size_bits
);
3403 ax_simple (expr
, aop_swap
);
3404 ax_ext (expr
, addr_size_bits
);
3405 /* Note no swap here: A > B is B < A. */
3406 ax_simple (expr
, aop_less_signed
);
3410 /* Sign extend the operands. */
3411 ax_ext (expr
, addr_size_bits
);
3412 ax_simple (expr
, aop_swap
);
3413 ax_ext (expr
, addr_size_bits
);
3414 /* No need for a swap here. */
3415 ax_simple (expr
, aop_equal
);
3416 ax_simple (expr
, aop_log_not
);
3419 case DW_OP_call_frame_cfa
:
3422 CORE_ADDR text_offset
;
3424 const gdb_byte
*cfa_start
, *cfa_end
;
3426 if (dwarf2_fetch_cfa_info (arch
, expr
->scope
, per_cu
,
3428 &text_offset
, &cfa_start
, &cfa_end
))
3431 ax_reg (expr
, regnum
);
3434 ax_const_l (expr
, off
);
3435 ax_simple (expr
, aop_add
);
3440 /* Another expression. */
3441 ax_const_l (expr
, text_offset
);
3442 dwarf2_compile_expr_to_ax (expr
, loc
, arch
, addr_size
,
3443 cfa_start
, cfa_end
, per_cu
);
3446 loc
->kind
= axs_lvalue_memory
;
3450 case DW_OP_GNU_push_tls_address
:
3451 case DW_OP_form_tls_address
:
3455 case DW_OP_push_object_address
:
3460 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
3462 i
= ax_goto (expr
, aop_goto
);
3463 dw_labels
.push_back (op_ptr
+ offset
- base
);
3464 patches
.push_back (i
);
3468 offset
= extract_signed_integer (op_ptr
, 2, byte_order
);
3470 /* Zero extend the operand. */
3471 ax_zero_ext (expr
, addr_size_bits
);
3472 i
= ax_goto (expr
, aop_if_goto
);
3473 dw_labels
.push_back (op_ptr
+ offset
- base
);
3474 patches
.push_back (i
);
3481 case DW_OP_bit_piece
:
3483 uint64_t size
, offset
;
3485 if (op_ptr
- 1 == previous_piece
)
3486 error (_("Cannot translate empty pieces to agent expressions"));
3487 previous_piece
= op_ptr
- 1;
3489 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &size
);
3490 if (op
== DW_OP_piece
)
3496 op_ptr
= safe_read_uleb128 (op_ptr
, op_end
, &offset
);
3498 if (bits_collected
+ size
> 8 * sizeof (LONGEST
))
3499 error (_("Expression pieces exceed word size"));
3501 /* Access the bits. */
3504 case axs_lvalue_register
:
3505 ax_reg (expr
, loc
->u
.reg
);
3508 case axs_lvalue_memory
:
3509 /* Offset the pointer, if needed. */
3512 ax_const_l (expr
, offset
/ 8);
3513 ax_simple (expr
, aop_add
);
3516 access_memory (arch
, expr
, size
);
3520 /* For a bits-big-endian target, shift up what we already
3521 have. For a bits-little-endian target, shift up the
3522 new data. Note that there is a potential bug here if
3523 the DWARF expression leaves multiple values on the
3525 if (bits_collected
> 0)
3527 if (bits_big_endian
)
3529 ax_simple (expr
, aop_swap
);
3530 ax_const_l (expr
, size
);
3531 ax_simple (expr
, aop_lsh
);
3532 /* We don't need a second swap here, because
3533 aop_bit_or is symmetric. */
3537 ax_const_l (expr
, size
);
3538 ax_simple (expr
, aop_lsh
);
3540 ax_simple (expr
, aop_bit_or
);
3543 bits_collected
+= size
;
3544 loc
->kind
= axs_rvalue
;
3548 case DW_OP_GNU_uninit
:
3554 struct dwarf2_locexpr_baton block
;
3555 int size
= (op
== DW_OP_call2
? 2 : 4);
3558 uoffset
= extract_unsigned_integer (op_ptr
, size
, byte_order
);
3561 offset
.cu_off
= uoffset
;
3562 block
= dwarf2_fetch_die_loc_cu_off (offset
, per_cu
,
3565 /* DW_OP_call_ref is currently not supported. */
3566 gdb_assert (block
.per_cu
== per_cu
);
3568 dwarf2_compile_expr_to_ax (expr
, loc
, arch
, addr_size
,
3569 block
.data
, block
.data
+ block
.size
,
3574 case DW_OP_call_ref
:
3582 /* Patch all the branches we emitted. */
3583 for (i
= 0; i
< patches
.size (); ++i
)
3585 int targ
= offsets
[dw_labels
[i
]];
3587 internal_error (__FILE__
, __LINE__
, _("invalid label"));
3588 ax_label (expr
, patches
[i
], targ
);
3593 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3594 evaluator to calculate the location. */
3595 static struct value
*
3596 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
3598 struct dwarf2_locexpr_baton
*dlbaton
3599 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
3602 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
3603 dlbaton
->size
, dlbaton
->per_cu
);
3608 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3609 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3612 static struct value
*
3613 locexpr_read_variable_at_entry (struct symbol
*symbol
, struct frame_info
*frame
)
3615 struct dwarf2_locexpr_baton
*dlbaton
3616 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
3618 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol
), frame
, dlbaton
->data
,
3622 /* Implementation of get_symbol_read_needs from
3623 symbol_computed_ops. */
3625 static enum symbol_needs_kind
3626 locexpr_get_symbol_read_needs (struct symbol
*symbol
)
3628 struct dwarf2_locexpr_baton
*dlbaton
3629 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
3631 return dwarf2_loc_desc_get_symbol_read_needs (dlbaton
->data
, dlbaton
->size
,
3635 /* Return true if DATA points to the end of a piece. END is one past
3636 the last byte in the expression. */
3639 piece_end_p (const gdb_byte
*data
, const gdb_byte
*end
)
3641 return data
== end
|| data
[0] == DW_OP_piece
|| data
[0] == DW_OP_bit_piece
;
3644 /* Helper for locexpr_describe_location_piece that finds the name of a
3648 locexpr_regname (struct gdbarch
*gdbarch
, int dwarf_regnum
)
3652 /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3653 We'd rather print *something* here than throw an error. */
3654 regnum
= dwarf_reg_to_regnum (gdbarch
, dwarf_regnum
);
3655 /* gdbarch_register_name may just return "", return something more
3656 descriptive for bad register numbers. */
3659 /* The text is output as "$bad_register_number".
3660 That is why we use the underscores. */
3661 return _("bad_register_number");
3663 return gdbarch_register_name (gdbarch
, regnum
);
3666 /* Nicely describe a single piece of a location, returning an updated
3667 position in the bytecode sequence. This function cannot recognize
3668 all locations; if a location is not recognized, it simply returns
3669 DATA. If there is an error during reading, e.g. we run off the end
3670 of the buffer, an error is thrown. */
3672 static const gdb_byte
*
3673 locexpr_describe_location_piece (struct symbol
*symbol
, struct ui_file
*stream
,
3674 CORE_ADDR addr
, struct objfile
*objfile
,
3675 struct dwarf2_per_cu_data
*per_cu
,
3676 const gdb_byte
*data
, const gdb_byte
*end
,
3677 unsigned int addr_size
)
3679 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
3682 if (data
[0] >= DW_OP_reg0
&& data
[0] <= DW_OP_reg31
)
3684 fprintf_filtered (stream
, _("a variable in $%s"),
3685 locexpr_regname (gdbarch
, data
[0] - DW_OP_reg0
));
3688 else if (data
[0] == DW_OP_regx
)
3692 data
= safe_read_uleb128 (data
+ 1, end
, ®
);
3693 fprintf_filtered (stream
, _("a variable in $%s"),
3694 locexpr_regname (gdbarch
, reg
));
3696 else if (data
[0] == DW_OP_fbreg
)
3698 const struct block
*b
;
3699 struct symbol
*framefunc
;
3701 int64_t frame_offset
;
3702 const gdb_byte
*base_data
, *new_data
, *save_data
= data
;
3704 int64_t base_offset
= 0;
3706 new_data
= safe_read_sleb128 (data
+ 1, end
, &frame_offset
);
3707 if (!piece_end_p (new_data
, end
))
3711 b
= block_for_pc (addr
);
3714 error (_("No block found for address for symbol \"%s\"."),
3715 SYMBOL_PRINT_NAME (symbol
));
3717 framefunc
= block_linkage_function (b
);
3720 error (_("No function found for block for symbol \"%s\"."),
3721 SYMBOL_PRINT_NAME (symbol
));
3723 func_get_frame_base_dwarf_block (framefunc
, addr
, &base_data
, &base_size
);
3725 if (base_data
[0] >= DW_OP_breg0
&& base_data
[0] <= DW_OP_breg31
)
3727 const gdb_byte
*buf_end
;
3729 frame_reg
= base_data
[0] - DW_OP_breg0
;
3730 buf_end
= safe_read_sleb128 (base_data
+ 1, base_data
+ base_size
,
3732 if (buf_end
!= base_data
+ base_size
)
3733 error (_("Unexpected opcode after "
3734 "DW_OP_breg%u for symbol \"%s\"."),
3735 frame_reg
, SYMBOL_PRINT_NAME (symbol
));
3737 else if (base_data
[0] >= DW_OP_reg0
&& base_data
[0] <= DW_OP_reg31
)
3739 /* The frame base is just the register, with no offset. */
3740 frame_reg
= base_data
[0] - DW_OP_reg0
;
3745 /* We don't know what to do with the frame base expression,
3746 so we can't trace this variable; give up. */
3750 fprintf_filtered (stream
,
3751 _("a variable at frame base reg $%s offset %s+%s"),
3752 locexpr_regname (gdbarch
, frame_reg
),
3753 plongest (base_offset
), plongest (frame_offset
));
3755 else if (data
[0] >= DW_OP_breg0
&& data
[0] <= DW_OP_breg31
3756 && piece_end_p (data
, end
))
3760 data
= safe_read_sleb128 (data
+ 1, end
, &offset
);
3762 fprintf_filtered (stream
,
3763 _("a variable at offset %s from base reg $%s"),
3765 locexpr_regname (gdbarch
, data
[0] - DW_OP_breg0
));
3768 /* The location expression for a TLS variable looks like this (on a
3771 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3772 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3774 0x3 is the encoding for DW_OP_addr, which has an operand as long
3775 as the size of an address on the target machine (here is 8
3776 bytes). Note that more recent version of GCC emit DW_OP_const4u
3777 or DW_OP_const8u, depending on address size, rather than
3778 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3779 The operand represents the offset at which the variable is within
3780 the thread local storage. */
3782 else if (data
+ 1 + addr_size
< end
3783 && (data
[0] == DW_OP_addr
3784 || (addr_size
== 4 && data
[0] == DW_OP_const4u
)
3785 || (addr_size
== 8 && data
[0] == DW_OP_const8u
))
3786 && (data
[1 + addr_size
] == DW_OP_GNU_push_tls_address
3787 || data
[1 + addr_size
] == DW_OP_form_tls_address
)
3788 && piece_end_p (data
+ 2 + addr_size
, end
))
3791 offset
= extract_unsigned_integer (data
+ 1, addr_size
,
3792 gdbarch_byte_order (gdbarch
));
3794 fprintf_filtered (stream
,
3795 _("a thread-local variable at offset 0x%s "
3796 "in the thread-local storage for `%s'"),
3797 phex_nz (offset
, addr_size
), objfile_name (objfile
));
3799 data
+= 1 + addr_size
+ 1;
3802 /* With -gsplit-dwarf a TLS variable can also look like this:
3803 DW_AT_location : 3 byte block: fc 4 e0
3804 (DW_OP_GNU_const_index: 4;
3805 DW_OP_GNU_push_tls_address) */
3806 else if (data
+ 3 <= end
3807 && data
+ 1 + (leb128_size
= skip_leb128 (data
+ 1, end
)) < end
3808 && data
[0] == DW_OP_GNU_const_index
3810 && (data
[1 + leb128_size
] == DW_OP_GNU_push_tls_address
3811 || data
[1 + leb128_size
] == DW_OP_form_tls_address
)
3812 && piece_end_p (data
+ 2 + leb128_size
, end
))
3816 data
= safe_read_uleb128 (data
+ 1, end
, &offset
);
3817 offset
= dwarf2_read_addr_index (per_cu
, offset
);
3818 fprintf_filtered (stream
,
3819 _("a thread-local variable at offset 0x%s "
3820 "in the thread-local storage for `%s'"),
3821 phex_nz (offset
, addr_size
), objfile_name (objfile
));
3825 else if (data
[0] >= DW_OP_lit0
3826 && data
[0] <= DW_OP_lit31
3828 && data
[1] == DW_OP_stack_value
)
3830 fprintf_filtered (stream
, _("the constant %d"), data
[0] - DW_OP_lit0
);
3837 /* Disassemble an expression, stopping at the end of a piece or at the
3838 end of the expression. Returns a pointer to the next unread byte
3839 in the input expression. If ALL is nonzero, then this function
3840 will keep going until it reaches the end of the expression.
3841 If there is an error during reading, e.g. we run off the end
3842 of the buffer, an error is thrown. */
3844 static const gdb_byte
*
3845 disassemble_dwarf_expression (struct ui_file
*stream
,
3846 struct gdbarch
*arch
, unsigned int addr_size
,
3847 int offset_size
, const gdb_byte
*start
,
3848 const gdb_byte
*data
, const gdb_byte
*end
,
3849 int indent
, int all
,
3850 struct dwarf2_per_cu_data
*per_cu
)
3854 || (data
[0] != DW_OP_piece
&& data
[0] != DW_OP_bit_piece
)))
3856 enum dwarf_location_atom op
= (enum dwarf_location_atom
) *data
++;
3861 name
= get_DW_OP_name (op
);
3864 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3865 op
, (long) (data
- 1 - start
));
3866 fprintf_filtered (stream
, " %*ld: %s", indent
+ 4,
3867 (long) (data
- 1 - start
), name
);
3872 ul
= extract_unsigned_integer (data
, addr_size
,
3873 gdbarch_byte_order (arch
));
3875 fprintf_filtered (stream
, " 0x%s", phex_nz (ul
, addr_size
));
3879 ul
= extract_unsigned_integer (data
, 1, gdbarch_byte_order (arch
));
3881 fprintf_filtered (stream
, " %s", pulongest (ul
));
3884 l
= extract_signed_integer (data
, 1, gdbarch_byte_order (arch
));
3886 fprintf_filtered (stream
, " %s", plongest (l
));
3889 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
3891 fprintf_filtered (stream
, " %s", pulongest (ul
));
3894 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
3896 fprintf_filtered (stream
, " %s", plongest (l
));
3899 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
3901 fprintf_filtered (stream
, " %s", pulongest (ul
));
3904 l
= extract_signed_integer (data
, 4, gdbarch_byte_order (arch
));
3906 fprintf_filtered (stream
, " %s", plongest (l
));
3909 ul
= extract_unsigned_integer (data
, 8, gdbarch_byte_order (arch
));
3911 fprintf_filtered (stream
, " %s", pulongest (ul
));
3914 l
= extract_signed_integer (data
, 8, gdbarch_byte_order (arch
));
3916 fprintf_filtered (stream
, " %s", plongest (l
));
3919 data
= safe_read_uleb128 (data
, end
, &ul
);
3920 fprintf_filtered (stream
, " %s", pulongest (ul
));
3923 data
= safe_read_sleb128 (data
, end
, &l
);
3924 fprintf_filtered (stream
, " %s", plongest (l
));
3959 fprintf_filtered (stream
, " [$%s]",
3960 locexpr_regname (arch
, op
- DW_OP_reg0
));
3964 data
= safe_read_uleb128 (data
, end
, &ul
);
3965 fprintf_filtered (stream
, " %s [$%s]", pulongest (ul
),
3966 locexpr_regname (arch
, (int) ul
));
3969 case DW_OP_implicit_value
:
3970 data
= safe_read_uleb128 (data
, end
, &ul
);
3972 fprintf_filtered (stream
, " %s", pulongest (ul
));
4007 data
= safe_read_sleb128 (data
, end
, &l
);
4008 fprintf_filtered (stream
, " %s [$%s]", plongest (l
),
4009 locexpr_regname (arch
, op
- DW_OP_breg0
));
4013 data
= safe_read_uleb128 (data
, end
, &ul
);
4014 data
= safe_read_sleb128 (data
, end
, &l
);
4015 fprintf_filtered (stream
, " register %s [$%s] offset %s",
4017 locexpr_regname (arch
, (int) ul
),
4022 data
= safe_read_sleb128 (data
, end
, &l
);
4023 fprintf_filtered (stream
, " %s", plongest (l
));
4026 case DW_OP_xderef_size
:
4027 case DW_OP_deref_size
:
4029 fprintf_filtered (stream
, " %d", *data
);
4033 case DW_OP_plus_uconst
:
4034 data
= safe_read_uleb128 (data
, end
, &ul
);
4035 fprintf_filtered (stream
, " %s", pulongest (ul
));
4039 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
4041 fprintf_filtered (stream
, " to %ld",
4042 (long) (data
+ l
- start
));
4046 l
= extract_signed_integer (data
, 2, gdbarch_byte_order (arch
));
4048 fprintf_filtered (stream
, " %ld",
4049 (long) (data
+ l
- start
));
4053 ul
= extract_unsigned_integer (data
, 2, gdbarch_byte_order (arch
));
4055 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 2));
4059 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
4061 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 4));
4064 case DW_OP_call_ref
:
4065 ul
= extract_unsigned_integer (data
, offset_size
,
4066 gdbarch_byte_order (arch
));
4067 data
+= offset_size
;
4068 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, offset_size
));
4072 data
= safe_read_uleb128 (data
, end
, &ul
);
4073 fprintf_filtered (stream
, " %s (bytes)", pulongest (ul
));
4076 case DW_OP_bit_piece
:
4080 data
= safe_read_uleb128 (data
, end
, &ul
);
4081 data
= safe_read_uleb128 (data
, end
, &offset
);
4082 fprintf_filtered (stream
, " size %s offset %s (bits)",
4083 pulongest (ul
), pulongest (offset
));
4087 case DW_OP_GNU_implicit_pointer
:
4089 ul
= extract_unsigned_integer (data
, offset_size
,
4090 gdbarch_byte_order (arch
));
4091 data
+= offset_size
;
4093 data
= safe_read_sleb128 (data
, end
, &l
);
4095 fprintf_filtered (stream
, " DIE %s offset %s",
4096 phex_nz (ul
, offset_size
),
4101 case DW_OP_GNU_deref_type
:
4103 int addr_size
= *data
++;
4107 data
= safe_read_uleb128 (data
, end
, &ul
);
4109 type
= dwarf2_get_die_type (offset
, per_cu
);
4110 fprintf_filtered (stream
, "<");
4111 type_print (type
, "", stream
, -1);
4112 fprintf_filtered (stream
, " [0x%s]> %d", phex_nz (offset
.cu_off
, 0),
4117 case DW_OP_GNU_const_type
:
4122 data
= safe_read_uleb128 (data
, end
, &ul
);
4123 type_die
.cu_off
= ul
;
4124 type
= dwarf2_get_die_type (type_die
, per_cu
);
4125 fprintf_filtered (stream
, "<");
4126 type_print (type
, "", stream
, -1);
4127 fprintf_filtered (stream
, " [0x%s]>", phex_nz (type_die
.cu_off
, 0));
4131 case DW_OP_GNU_regval_type
:
4137 data
= safe_read_uleb128 (data
, end
, ®
);
4138 data
= safe_read_uleb128 (data
, end
, &ul
);
4139 type_die
.cu_off
= ul
;
4141 type
= dwarf2_get_die_type (type_die
, per_cu
);
4142 fprintf_filtered (stream
, "<");
4143 type_print (type
, "", stream
, -1);
4144 fprintf_filtered (stream
, " [0x%s]> [$%s]",
4145 phex_nz (type_die
.cu_off
, 0),
4146 locexpr_regname (arch
, reg
));
4150 case DW_OP_GNU_convert
:
4151 case DW_OP_GNU_reinterpret
:
4155 data
= safe_read_uleb128 (data
, end
, &ul
);
4156 type_die
.cu_off
= ul
;
4158 if (type_die
.cu_off
== 0)
4159 fprintf_filtered (stream
, "<0>");
4164 type
= dwarf2_get_die_type (type_die
, per_cu
);
4165 fprintf_filtered (stream
, "<");
4166 type_print (type
, "", stream
, -1);
4167 fprintf_filtered (stream
, " [0x%s]>", phex_nz (type_die
.cu_off
, 0));
4172 case DW_OP_GNU_entry_value
:
4173 data
= safe_read_uleb128 (data
, end
, &ul
);
4174 fputc_filtered ('\n', stream
);
4175 disassemble_dwarf_expression (stream
, arch
, addr_size
, offset_size
,
4176 start
, data
, data
+ ul
, indent
+ 2,
4181 case DW_OP_GNU_parameter_ref
:
4182 ul
= extract_unsigned_integer (data
, 4, gdbarch_byte_order (arch
));
4184 fprintf_filtered (stream
, " offset %s", phex_nz (ul
, 4));
4187 case DW_OP_GNU_addr_index
:
4188 data
= safe_read_uleb128 (data
, end
, &ul
);
4189 ul
= dwarf2_read_addr_index (per_cu
, ul
);
4190 fprintf_filtered (stream
, " 0x%s", phex_nz (ul
, addr_size
));
4192 case DW_OP_GNU_const_index
:
4193 data
= safe_read_uleb128 (data
, end
, &ul
);
4194 ul
= dwarf2_read_addr_index (per_cu
, ul
);
4195 fprintf_filtered (stream
, " %s", pulongest (ul
));
4199 fprintf_filtered (stream
, "\n");
4205 /* Describe a single location, which may in turn consist of multiple
4209 locexpr_describe_location_1 (struct symbol
*symbol
, CORE_ADDR addr
,
4210 struct ui_file
*stream
,
4211 const gdb_byte
*data
, size_t size
,
4212 struct objfile
*objfile
, unsigned int addr_size
,
4213 int offset_size
, struct dwarf2_per_cu_data
*per_cu
)
4215 const gdb_byte
*end
= data
+ size
;
4216 int first_piece
= 1, bad
= 0;
4220 const gdb_byte
*here
= data
;
4221 int disassemble
= 1;
4226 fprintf_filtered (stream
, _(", and "));
4228 if (!dwarf_always_disassemble
)
4230 data
= locexpr_describe_location_piece (symbol
, stream
,
4231 addr
, objfile
, per_cu
,
4232 data
, end
, addr_size
);
4233 /* If we printed anything, or if we have an empty piece,
4234 then don't disassemble. */
4236 || data
[0] == DW_OP_piece
4237 || data
[0] == DW_OP_bit_piece
)
4242 fprintf_filtered (stream
, _("a complex DWARF expression:\n"));
4243 data
= disassemble_dwarf_expression (stream
,
4244 get_objfile_arch (objfile
),
4245 addr_size
, offset_size
, data
,
4247 dwarf_always_disassemble
,
4253 int empty
= data
== here
;
4256 fprintf_filtered (stream
, " ");
4257 if (data
[0] == DW_OP_piece
)
4261 data
= safe_read_uleb128 (data
+ 1, end
, &bytes
);
4264 fprintf_filtered (stream
, _("an empty %s-byte piece"),
4267 fprintf_filtered (stream
, _(" [%s-byte piece]"),
4270 else if (data
[0] == DW_OP_bit_piece
)
4272 uint64_t bits
, offset
;
4274 data
= safe_read_uleb128 (data
+ 1, end
, &bits
);
4275 data
= safe_read_uleb128 (data
, end
, &offset
);
4278 fprintf_filtered (stream
,
4279 _("an empty %s-bit piece"),
4282 fprintf_filtered (stream
,
4283 _(" [%s-bit piece, offset %s bits]"),
4284 pulongest (bits
), pulongest (offset
));
4294 if (bad
|| data
> end
)
4295 error (_("Corrupted DWARF2 expression for \"%s\"."),
4296 SYMBOL_PRINT_NAME (symbol
));
4299 /* Print a natural-language description of SYMBOL to STREAM. This
4300 version is for a symbol with a single location. */
4303 locexpr_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
4304 struct ui_file
*stream
)
4306 struct dwarf2_locexpr_baton
*dlbaton
4307 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4308 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
4309 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
4310 int offset_size
= dwarf2_per_cu_offset_size (dlbaton
->per_cu
);
4312 locexpr_describe_location_1 (symbol
, addr
, stream
,
4313 dlbaton
->data
, dlbaton
->size
,
4314 objfile
, addr_size
, offset_size
,
4318 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4319 any necessary bytecode in AX. */
4322 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
4323 struct agent_expr
*ax
, struct axs_value
*value
)
4325 struct dwarf2_locexpr_baton
*dlbaton
4326 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4327 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
4329 if (dlbaton
->size
== 0)
4330 value
->optimized_out
= 1;
4332 dwarf2_compile_expr_to_ax (ax
, value
, gdbarch
, addr_size
,
4333 dlbaton
->data
, dlbaton
->data
+ dlbaton
->size
,
4337 /* symbol_computed_ops 'generate_c_location' method. */
4340 locexpr_generate_c_location (struct symbol
*sym
, struct ui_file
*stream
,
4341 struct gdbarch
*gdbarch
,
4342 unsigned char *registers_used
,
4343 CORE_ADDR pc
, const char *result_name
)
4345 struct dwarf2_locexpr_baton
*dlbaton
4346 = (struct dwarf2_locexpr_baton
*) SYMBOL_LOCATION_BATON (sym
);
4347 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
4349 if (dlbaton
->size
== 0)
4350 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym
));
4352 compile_dwarf_expr_to_c (stream
, result_name
,
4353 sym
, pc
, gdbarch
, registers_used
, addr_size
,
4354 dlbaton
->data
, dlbaton
->data
+ dlbaton
->size
,
4358 /* The set of location functions used with the DWARF-2 expression
4360 const struct symbol_computed_ops dwarf2_locexpr_funcs
= {
4361 locexpr_read_variable
,
4362 locexpr_read_variable_at_entry
,
4363 locexpr_get_symbol_read_needs
,
4364 locexpr_describe_location
,
4365 0, /* location_has_loclist */
4366 locexpr_tracepoint_var_ref
,
4367 locexpr_generate_c_location
4371 /* Wrapper functions for location lists. These generally find
4372 the appropriate location expression and call something above. */
4374 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4375 evaluator to calculate the location. */
4376 static struct value
*
4377 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
4379 struct dwarf2_loclist_baton
*dlbaton
4380 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4382 const gdb_byte
*data
;
4384 CORE_ADDR pc
= frame
? get_frame_address_in_block (frame
) : 0;
4386 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
4387 val
= dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol
), frame
, data
, size
,
4393 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4394 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4397 Function always returns non-NULL value, it may be marked optimized out if
4398 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
4399 if it cannot resolve the parameter for any reason. */
4401 static struct value
*
4402 loclist_read_variable_at_entry (struct symbol
*symbol
, struct frame_info
*frame
)
4404 struct dwarf2_loclist_baton
*dlbaton
4405 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4406 const gdb_byte
*data
;
4410 if (frame
== NULL
|| !get_frame_func_if_available (frame
, &pc
))
4411 return allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
4413 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
4415 return allocate_optimized_out_value (SYMBOL_TYPE (symbol
));
4417 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol
), frame
, data
, size
);
4420 /* Implementation of get_symbol_read_needs from
4421 symbol_computed_ops. */
4423 static enum symbol_needs_kind
4424 loclist_symbol_needs (struct symbol
*symbol
)
4426 /* If there's a location list, then assume we need to have a frame
4427 to choose the appropriate location expression. With tracking of
4428 global variables this is not necessarily true, but such tracking
4429 is disabled in GCC at the moment until we figure out how to
4432 return SYMBOL_NEEDS_FRAME
;
4435 /* Print a natural-language description of SYMBOL to STREAM. This
4436 version applies when there is a list of different locations, each
4437 with a specified address range. */
4440 loclist_describe_location (struct symbol
*symbol
, CORE_ADDR addr
,
4441 struct ui_file
*stream
)
4443 struct dwarf2_loclist_baton
*dlbaton
4444 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4445 const gdb_byte
*loc_ptr
, *buf_end
;
4446 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
4447 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
4448 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
4449 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
4450 int offset_size
= dwarf2_per_cu_offset_size (dlbaton
->per_cu
);
4451 int signed_addr_p
= bfd_get_sign_extend_vma (objfile
->obfd
);
4452 /* Adjust base_address for relocatable objects. */
4453 CORE_ADDR base_offset
= dwarf2_per_cu_text_offset (dlbaton
->per_cu
);
4454 CORE_ADDR base_address
= dlbaton
->base_address
+ base_offset
;
4457 loc_ptr
= dlbaton
->data
;
4458 buf_end
= dlbaton
->data
+ dlbaton
->size
;
4460 fprintf_filtered (stream
, _("multi-location:\n"));
4462 /* Iterate through locations until we run out. */
4465 CORE_ADDR low
= 0, high
= 0; /* init for gcc -Wall */
4467 enum debug_loc_kind kind
;
4468 const gdb_byte
*new_ptr
= NULL
; /* init for gcc -Wall */
4470 if (dlbaton
->from_dwo
)
4471 kind
= decode_debug_loc_dwo_addresses (dlbaton
->per_cu
,
4472 loc_ptr
, buf_end
, &new_ptr
,
4473 &low
, &high
, byte_order
);
4475 kind
= decode_debug_loc_addresses (loc_ptr
, buf_end
, &new_ptr
,
4477 byte_order
, addr_size
,
4482 case DEBUG_LOC_END_OF_LIST
:
4485 case DEBUG_LOC_BASE_ADDRESS
:
4486 base_address
= high
+ base_offset
;
4487 fprintf_filtered (stream
, _(" Base address %s"),
4488 paddress (gdbarch
, base_address
));
4490 case DEBUG_LOC_START_END
:
4491 case DEBUG_LOC_START_LENGTH
:
4493 case DEBUG_LOC_BUFFER_OVERFLOW
:
4494 case DEBUG_LOC_INVALID_ENTRY
:
4495 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4496 SYMBOL_PRINT_NAME (symbol
));
4498 gdb_assert_not_reached ("bad debug_loc_kind");
4501 /* Otherwise, a location expression entry. */
4502 low
+= base_address
;
4503 high
+= base_address
;
4505 low
= gdbarch_adjust_dwarf2_addr (gdbarch
, low
);
4506 high
= gdbarch_adjust_dwarf2_addr (gdbarch
, high
);
4508 length
= extract_unsigned_integer (loc_ptr
, 2, byte_order
);
4511 /* (It would improve readability to print only the minimum
4512 necessary digits of the second number of the range.) */
4513 fprintf_filtered (stream
, _(" Range %s-%s: "),
4514 paddress (gdbarch
, low
), paddress (gdbarch
, high
));
4516 /* Now describe this particular location. */
4517 locexpr_describe_location_1 (symbol
, low
, stream
, loc_ptr
, length
,
4518 objfile
, addr_size
, offset_size
,
4521 fprintf_filtered (stream
, "\n");
4527 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4528 any necessary bytecode in AX. */
4530 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
4531 struct agent_expr
*ax
, struct axs_value
*value
)
4533 struct dwarf2_loclist_baton
*dlbaton
4534 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (symbol
);
4535 const gdb_byte
*data
;
4537 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
4539 data
= dwarf2_find_location_expression (dlbaton
, &size
, ax
->scope
);
4541 value
->optimized_out
= 1;
4543 dwarf2_compile_expr_to_ax (ax
, value
, gdbarch
, addr_size
, data
, data
+ size
,
4547 /* symbol_computed_ops 'generate_c_location' method. */
4550 loclist_generate_c_location (struct symbol
*sym
, struct ui_file
*stream
,
4551 struct gdbarch
*gdbarch
,
4552 unsigned char *registers_used
,
4553 CORE_ADDR pc
, const char *result_name
)
4555 struct dwarf2_loclist_baton
*dlbaton
4556 = (struct dwarf2_loclist_baton
*) SYMBOL_LOCATION_BATON (sym
);
4557 unsigned int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
4558 const gdb_byte
*data
;
4561 data
= dwarf2_find_location_expression (dlbaton
, &size
, pc
);
4563 error (_("symbol \"%s\" is optimized out"), SYMBOL_NATURAL_NAME (sym
));
4565 compile_dwarf_expr_to_c (stream
, result_name
,
4566 sym
, pc
, gdbarch
, registers_used
, addr_size
,
4571 /* The set of location functions used with the DWARF-2 expression
4572 evaluator and location lists. */
4573 const struct symbol_computed_ops dwarf2_loclist_funcs
= {
4574 loclist_read_variable
,
4575 loclist_read_variable_at_entry
,
4576 loclist_symbol_needs
,
4577 loclist_describe_location
,
4578 1, /* location_has_loclist */
4579 loclist_tracepoint_var_ref
,
4580 loclist_generate_c_location
4583 /* Provide a prototype to silence -Wmissing-prototypes. */
4584 extern initialize_file_ftype _initialize_dwarf2loc
;
4587 _initialize_dwarf2loc (void)
4589 add_setshow_zuinteger_cmd ("entry-values", class_maintenance
,
4590 &entry_values_debug
,
4591 _("Set entry values and tail call frames "
4593 _("Show entry values and tail call frames "
4595 _("When non-zero, the process of determining "
4596 "parameter values from function entry point "
4597 "and tail call frames will be printed."),
4599 show_entry_values_debug
,
4600 &setdebuglist
, &showdebuglist
);