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