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