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