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