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