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