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