Remove dwarf2_per_cu_data::text_offset
[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_objfile->objfile->text_section_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 dwarf2_per_cu_data *per_cu;
2653 dwarf2_per_objfile *per_objfile;
2654
2655 if (prop->kind == PROP_LOCEXPR)
2656 {
2657 data = baton->locexpr.data;
2658 size = baton->locexpr.size;
2659 per_cu = baton->locexpr.per_cu;
2660 per_objfile = baton->locexpr.per_objfile;
2661 }
2662 else
2663 {
2664 gdb_assert (prop->kind == PROP_LOCLIST);
2665
2666 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2667 per_cu = baton->loclist.per_cu;
2668 per_objfile = baton->loclist.per_objfile;
2669 }
2670
2671 compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
2672 gdbarch, registers_used,
2673 per_cu->addr_size (),
2674 data, data + size, per_cu, per_objfile);
2675 }
2676
2677 \f
2678 /* Helper functions and baton for dwarf2_loc_desc_get_symbol_read_needs. */
2679
2680 class symbol_needs_eval_context : public dwarf_expr_context
2681 {
2682 public:
2683 symbol_needs_eval_context (dwarf2_per_objfile *per_objfile)
2684 : dwarf_expr_context (per_objfile)
2685 {}
2686
2687 enum symbol_needs_kind needs;
2688 struct dwarf2_per_cu_data *per_cu;
2689
2690 /* Reads from registers do require a frame. */
2691 CORE_ADDR read_addr_from_reg (int regnum) override
2692 {
2693 needs = SYMBOL_NEEDS_FRAME;
2694 return 1;
2695 }
2696
2697 /* "get_reg_value" callback: Reads from registers do require a
2698 frame. */
2699
2700 struct value *get_reg_value (struct type *type, int regnum) override
2701 {
2702 needs = SYMBOL_NEEDS_FRAME;
2703 return value_zero (type, not_lval);
2704 }
2705
2706 /* Reads from memory do not require a frame. */
2707 void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t len) override
2708 {
2709 memset (buf, 0, len);
2710 }
2711
2712 /* Frame-relative accesses do require a frame. */
2713 void get_frame_base (const gdb_byte **start, size_t *length) override
2714 {
2715 static gdb_byte lit0 = DW_OP_lit0;
2716
2717 *start = &lit0;
2718 *length = 1;
2719
2720 needs = SYMBOL_NEEDS_FRAME;
2721 }
2722
2723 /* CFA accesses require a frame. */
2724 CORE_ADDR get_frame_cfa () override
2725 {
2726 needs = SYMBOL_NEEDS_FRAME;
2727 return 1;
2728 }
2729
2730 CORE_ADDR get_frame_pc () override
2731 {
2732 needs = SYMBOL_NEEDS_FRAME;
2733 return 1;
2734 }
2735
2736 /* Thread-local accesses require registers, but not a frame. */
2737 CORE_ADDR get_tls_address (CORE_ADDR offset) override
2738 {
2739 if (needs <= SYMBOL_NEEDS_REGISTERS)
2740 needs = SYMBOL_NEEDS_REGISTERS;
2741 return 1;
2742 }
2743
2744 /* Helper interface of per_cu_dwarf_call for
2745 dwarf2_loc_desc_get_symbol_read_needs. */
2746
2747 void dwarf_call (cu_offset die_offset) override
2748 {
2749 per_cu_dwarf_call (this, die_offset, per_cu);
2750 }
2751
2752 /* Helper interface of sect_variable_value for
2753 dwarf2_loc_desc_get_symbol_read_needs. */
2754
2755 struct value *dwarf_variable_value (sect_offset sect_off) override
2756 {
2757 return sect_variable_value (this, sect_off, per_cu);
2758 }
2759
2760 /* DW_OP_entry_value accesses require a caller, therefore a
2761 frame. */
2762
2763 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind,
2764 union call_site_parameter_u kind_u,
2765 int deref_size) override
2766 {
2767 needs = SYMBOL_NEEDS_FRAME;
2768
2769 /* The expression may require some stub values on DWARF stack. */
2770 push_address (0, 0);
2771 }
2772
2773 /* DW_OP_addrx and DW_OP_GNU_addr_index doesn't require a frame. */
2774
2775 CORE_ADDR get_addr_index (unsigned int index) override
2776 {
2777 /* Nothing to do. */
2778 return 1;
2779 }
2780
2781 /* DW_OP_push_object_address has a frame already passed through. */
2782
2783 CORE_ADDR get_object_address () override
2784 {
2785 /* Nothing to do. */
2786 return 1;
2787 }
2788 };
2789
2790 /* Compute the correct symbol_needs_kind value for the location
2791 expression at DATA (length SIZE). */
2792
2793 static enum symbol_needs_kind
2794 dwarf2_loc_desc_get_symbol_read_needs (const gdb_byte *data, size_t size,
2795 struct dwarf2_per_cu_data *per_cu)
2796 {
2797 int in_reg;
2798 struct objfile *objfile = per_cu->objfile ();
2799
2800 scoped_value_mark free_values;
2801
2802 symbol_needs_eval_context ctx (get_dwarf2_per_objfile (objfile));
2803
2804 ctx.needs = SYMBOL_NEEDS_NONE;
2805 ctx.per_cu = per_cu;
2806 ctx.gdbarch = objfile->arch ();
2807 ctx.addr_size = per_cu->addr_size ();
2808 ctx.ref_addr_size = per_cu->ref_addr_size ();
2809
2810 ctx.eval (data, size);
2811
2812 in_reg = ctx.location == DWARF_VALUE_REGISTER;
2813
2814 /* If the location has several pieces, and any of them are in
2815 registers, then we will need a frame to fetch them from. */
2816 for (dwarf_expr_piece &p : ctx.pieces)
2817 if (p.location == DWARF_VALUE_REGISTER)
2818 in_reg = 1;
2819
2820 if (in_reg)
2821 ctx.needs = SYMBOL_NEEDS_FRAME;
2822 return ctx.needs;
2823 }
2824
2825 /* A helper function that throws an unimplemented error mentioning a
2826 given DWARF operator. */
2827
2828 static void ATTRIBUTE_NORETURN
2829 unimplemented (unsigned int op)
2830 {
2831 const char *name = get_DW_OP_name (op);
2832
2833 if (name)
2834 error (_("DWARF operator %s cannot be translated to an agent expression"),
2835 name);
2836 else
2837 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2838 "to an agent expression"),
2839 op);
2840 }
2841
2842 /* See dwarf2loc.h.
2843
2844 This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2845 can issue a complaint, which is better than having every target's
2846 implementation of dwarf2_reg_to_regnum do it. */
2847
2848 int
2849 dwarf_reg_to_regnum (struct gdbarch *arch, int dwarf_reg)
2850 {
2851 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
2852
2853 if (reg == -1)
2854 {
2855 complaint (_("bad DWARF register number %d"), dwarf_reg);
2856 }
2857 return reg;
2858 }
2859
2860 /* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2861 Throw an error because DWARF_REG is bad. */
2862
2863 static void
2864 throw_bad_regnum_error (ULONGEST dwarf_reg)
2865 {
2866 /* Still want to print -1 as "-1".
2867 We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2868 but that's overkill for now. */
2869 if ((int) dwarf_reg == dwarf_reg)
2870 error (_("Unable to access DWARF register number %d"), (int) dwarf_reg);
2871 error (_("Unable to access DWARF register number %s"),
2872 pulongest (dwarf_reg));
2873 }
2874
2875 /* See dwarf2loc.h. */
2876
2877 int
2878 dwarf_reg_to_regnum_or_error (struct gdbarch *arch, ULONGEST dwarf_reg)
2879 {
2880 int reg;
2881
2882 if (dwarf_reg > INT_MAX)
2883 throw_bad_regnum_error (dwarf_reg);
2884 /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2885 bad, but that's ok. */
2886 reg = dwarf_reg_to_regnum (arch, (int) dwarf_reg);
2887 if (reg == -1)
2888 throw_bad_regnum_error (dwarf_reg);
2889 return reg;
2890 }
2891
2892 /* A helper function that emits an access to memory. ARCH is the
2893 target architecture. EXPR is the expression which we are building.
2894 NBITS is the number of bits we want to read. This emits the
2895 opcodes needed to read the memory and then extract the desired
2896 bits. */
2897
2898 static void
2899 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
2900 {
2901 ULONGEST nbytes = (nbits + 7) / 8;
2902
2903 gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
2904
2905 if (expr->tracing)
2906 ax_trace_quick (expr, nbytes);
2907
2908 if (nbits <= 8)
2909 ax_simple (expr, aop_ref8);
2910 else if (nbits <= 16)
2911 ax_simple (expr, aop_ref16);
2912 else if (nbits <= 32)
2913 ax_simple (expr, aop_ref32);
2914 else
2915 ax_simple (expr, aop_ref64);
2916
2917 /* If we read exactly the number of bytes we wanted, we're done. */
2918 if (8 * nbytes == nbits)
2919 return;
2920
2921 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
2922 {
2923 /* On a bits-big-endian machine, we want the high-order
2924 NBITS. */
2925 ax_const_l (expr, 8 * nbytes - nbits);
2926 ax_simple (expr, aop_rsh_unsigned);
2927 }
2928 else
2929 {
2930 /* On a bits-little-endian box, we want the low-order NBITS. */
2931 ax_zero_ext (expr, nbits);
2932 }
2933 }
2934
2935 /* A helper function to return the frame's PC. */
2936
2937 static CORE_ADDR
2938 get_ax_pc (void *baton)
2939 {
2940 struct agent_expr *expr = (struct agent_expr *) baton;
2941
2942 return expr->scope;
2943 }
2944
2945 /* Compile a DWARF location expression to an agent expression.
2946
2947 EXPR is the agent expression we are building.
2948 LOC is the agent value we modify.
2949 ARCH is the architecture.
2950 ADDR_SIZE is the size of addresses, in bytes.
2951 OP_PTR is the start of the location expression.
2952 OP_END is one past the last byte of the location expression.
2953
2954 This will throw an exception for various kinds of errors -- for
2955 example, if the expression cannot be compiled, or if the expression
2956 is invalid. */
2957
2958 static void
2959 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
2960 unsigned int addr_size, const gdb_byte *op_ptr,
2961 const gdb_byte *op_end,
2962 dwarf2_per_cu_data *per_cu,
2963 dwarf2_per_objfile *per_objfile)
2964 {
2965 gdbarch *arch = expr->gdbarch;
2966 std::vector<int> dw_labels, patches;
2967 const gdb_byte * const base = op_ptr;
2968 const gdb_byte *previous_piece = op_ptr;
2969 enum bfd_endian byte_order = gdbarch_byte_order (arch);
2970 ULONGEST bits_collected = 0;
2971 unsigned int addr_size_bits = 8 * addr_size;
2972 bool bits_big_endian = byte_order == BFD_ENDIAN_BIG;
2973
2974 std::vector<int> offsets (op_end - op_ptr, -1);
2975
2976 /* By default we are making an address. */
2977 loc->kind = axs_lvalue_memory;
2978
2979 while (op_ptr < op_end)
2980 {
2981 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
2982 uint64_t uoffset, reg;
2983 int64_t offset;
2984 int i;
2985
2986 offsets[op_ptr - base] = expr->len;
2987 ++op_ptr;
2988
2989 /* Our basic approach to code generation is to map DWARF
2990 operations directly to AX operations. However, there are
2991 some differences.
2992
2993 First, DWARF works on address-sized units, but AX always uses
2994 LONGEST. For most operations we simply ignore this
2995 difference; instead we generate sign extensions as needed
2996 before division and comparison operations. It would be nice
2997 to omit the sign extensions, but there is no way to determine
2998 the size of the target's LONGEST. (This code uses the size
2999 of the host LONGEST in some cases -- that is a bug but it is
3000 difficult to fix.)
3001
3002 Second, some DWARF operations cannot be translated to AX.
3003 For these we simply fail. See
3004 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
3005 switch (op)
3006 {
3007 case DW_OP_lit0:
3008 case DW_OP_lit1:
3009 case DW_OP_lit2:
3010 case DW_OP_lit3:
3011 case DW_OP_lit4:
3012 case DW_OP_lit5:
3013 case DW_OP_lit6:
3014 case DW_OP_lit7:
3015 case DW_OP_lit8:
3016 case DW_OP_lit9:
3017 case DW_OP_lit10:
3018 case DW_OP_lit11:
3019 case DW_OP_lit12:
3020 case DW_OP_lit13:
3021 case DW_OP_lit14:
3022 case DW_OP_lit15:
3023 case DW_OP_lit16:
3024 case DW_OP_lit17:
3025 case DW_OP_lit18:
3026 case DW_OP_lit19:
3027 case DW_OP_lit20:
3028 case DW_OP_lit21:
3029 case DW_OP_lit22:
3030 case DW_OP_lit23:
3031 case DW_OP_lit24:
3032 case DW_OP_lit25:
3033 case DW_OP_lit26:
3034 case DW_OP_lit27:
3035 case DW_OP_lit28:
3036 case DW_OP_lit29:
3037 case DW_OP_lit30:
3038 case DW_OP_lit31:
3039 ax_const_l (expr, op - DW_OP_lit0);
3040 break;
3041
3042 case DW_OP_addr:
3043 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
3044 op_ptr += addr_size;
3045 /* Some versions of GCC emit DW_OP_addr before
3046 DW_OP_GNU_push_tls_address. In this case the value is an
3047 index, not an address. We don't support things like
3048 branching between the address and the TLS op. */
3049 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
3050 uoffset += per_objfile->objfile->text_section_offset ();
3051 ax_const_l (expr, uoffset);
3052 break;
3053
3054 case DW_OP_const1u:
3055 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
3056 op_ptr += 1;
3057 break;
3058 case DW_OP_const1s:
3059 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
3060 op_ptr += 1;
3061 break;
3062 case DW_OP_const2u:
3063 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
3064 op_ptr += 2;
3065 break;
3066 case DW_OP_const2s:
3067 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
3068 op_ptr += 2;
3069 break;
3070 case DW_OP_const4u:
3071 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
3072 op_ptr += 4;
3073 break;
3074 case DW_OP_const4s:
3075 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
3076 op_ptr += 4;
3077 break;
3078 case DW_OP_const8u:
3079 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
3080 op_ptr += 8;
3081 break;
3082 case DW_OP_const8s:
3083 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
3084 op_ptr += 8;
3085 break;
3086 case DW_OP_constu:
3087 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3088 ax_const_l (expr, uoffset);
3089 break;
3090 case DW_OP_consts:
3091 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3092 ax_const_l (expr, offset);
3093 break;
3094
3095 case DW_OP_reg0:
3096 case DW_OP_reg1:
3097 case DW_OP_reg2:
3098 case DW_OP_reg3:
3099 case DW_OP_reg4:
3100 case DW_OP_reg5:
3101 case DW_OP_reg6:
3102 case DW_OP_reg7:
3103 case DW_OP_reg8:
3104 case DW_OP_reg9:
3105 case DW_OP_reg10:
3106 case DW_OP_reg11:
3107 case DW_OP_reg12:
3108 case DW_OP_reg13:
3109 case DW_OP_reg14:
3110 case DW_OP_reg15:
3111 case DW_OP_reg16:
3112 case DW_OP_reg17:
3113 case DW_OP_reg18:
3114 case DW_OP_reg19:
3115 case DW_OP_reg20:
3116 case DW_OP_reg21:
3117 case DW_OP_reg22:
3118 case DW_OP_reg23:
3119 case DW_OP_reg24:
3120 case DW_OP_reg25:
3121 case DW_OP_reg26:
3122 case DW_OP_reg27:
3123 case DW_OP_reg28:
3124 case DW_OP_reg29:
3125 case DW_OP_reg30:
3126 case DW_OP_reg31:
3127 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3128 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
3129 loc->kind = axs_lvalue_register;
3130 break;
3131
3132 case DW_OP_regx:
3133 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3134 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
3135 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, reg);
3136 loc->kind = axs_lvalue_register;
3137 break;
3138
3139 case DW_OP_implicit_value:
3140 {
3141 uint64_t len;
3142
3143 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
3144 if (op_ptr + len > op_end)
3145 error (_("DW_OP_implicit_value: too few bytes available."));
3146 if (len > sizeof (ULONGEST))
3147 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3148 (int) len);
3149
3150 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
3151 byte_order));
3152 op_ptr += len;
3153 dwarf_expr_require_composition (op_ptr, op_end,
3154 "DW_OP_implicit_value");
3155
3156 loc->kind = axs_rvalue;
3157 }
3158 break;
3159
3160 case DW_OP_stack_value:
3161 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
3162 loc->kind = axs_rvalue;
3163 break;
3164
3165 case DW_OP_breg0:
3166 case DW_OP_breg1:
3167 case DW_OP_breg2:
3168 case DW_OP_breg3:
3169 case DW_OP_breg4:
3170 case DW_OP_breg5:
3171 case DW_OP_breg6:
3172 case DW_OP_breg7:
3173 case DW_OP_breg8:
3174 case DW_OP_breg9:
3175 case DW_OP_breg10:
3176 case DW_OP_breg11:
3177 case DW_OP_breg12:
3178 case DW_OP_breg13:
3179 case DW_OP_breg14:
3180 case DW_OP_breg15:
3181 case DW_OP_breg16:
3182 case DW_OP_breg17:
3183 case DW_OP_breg18:
3184 case DW_OP_breg19:
3185 case DW_OP_breg20:
3186 case DW_OP_breg21:
3187 case DW_OP_breg22:
3188 case DW_OP_breg23:
3189 case DW_OP_breg24:
3190 case DW_OP_breg25:
3191 case DW_OP_breg26:
3192 case DW_OP_breg27:
3193 case DW_OP_breg28:
3194 case DW_OP_breg29:
3195 case DW_OP_breg30:
3196 case DW_OP_breg31:
3197 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3198 i = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
3199 ax_reg (expr, i);
3200 if (offset != 0)
3201 {
3202 ax_const_l (expr, offset);
3203 ax_simple (expr, aop_add);
3204 }
3205 break;
3206 case DW_OP_bregx:
3207 {
3208 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3209 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3210 i = dwarf_reg_to_regnum_or_error (arch, reg);
3211 ax_reg (expr, i);
3212 if (offset != 0)
3213 {
3214 ax_const_l (expr, offset);
3215 ax_simple (expr, aop_add);
3216 }
3217 }
3218 break;
3219 case DW_OP_fbreg:
3220 {
3221 const gdb_byte *datastart;
3222 size_t datalen;
3223 const struct block *b;
3224 struct symbol *framefunc;
3225
3226 b = block_for_pc (expr->scope);
3227
3228 if (!b)
3229 error (_("No block found for address"));
3230
3231 framefunc = block_linkage_function (b);
3232
3233 if (!framefunc)
3234 error (_("No function found for block"));
3235
3236 func_get_frame_base_dwarf_block (framefunc, expr->scope,
3237 &datastart, &datalen);
3238
3239 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3240 dwarf2_compile_expr_to_ax (expr, loc, addr_size, datastart,
3241 datastart + datalen, per_cu,
3242 per_objfile);
3243 if (loc->kind == axs_lvalue_register)
3244 require_rvalue (expr, loc);
3245
3246 if (offset != 0)
3247 {
3248 ax_const_l (expr, offset);
3249 ax_simple (expr, aop_add);
3250 }
3251
3252 loc->kind = axs_lvalue_memory;
3253 }
3254 break;
3255
3256 case DW_OP_dup:
3257 ax_simple (expr, aop_dup);
3258 break;
3259
3260 case DW_OP_drop:
3261 ax_simple (expr, aop_pop);
3262 break;
3263
3264 case DW_OP_pick:
3265 offset = *op_ptr++;
3266 ax_pick (expr, offset);
3267 break;
3268
3269 case DW_OP_swap:
3270 ax_simple (expr, aop_swap);
3271 break;
3272
3273 case DW_OP_over:
3274 ax_pick (expr, 1);
3275 break;
3276
3277 case DW_OP_rot:
3278 ax_simple (expr, aop_rot);
3279 break;
3280
3281 case DW_OP_deref:
3282 case DW_OP_deref_size:
3283 {
3284 int size;
3285
3286 if (op == DW_OP_deref_size)
3287 size = *op_ptr++;
3288 else
3289 size = addr_size;
3290
3291 if (size != 1 && size != 2 && size != 4 && size != 8)
3292 error (_("Unsupported size %d in %s"),
3293 size, get_DW_OP_name (op));
3294 access_memory (arch, expr, size * TARGET_CHAR_BIT);
3295 }
3296 break;
3297
3298 case DW_OP_abs:
3299 /* Sign extend the operand. */
3300 ax_ext (expr, addr_size_bits);
3301 ax_simple (expr, aop_dup);
3302 ax_const_l (expr, 0);
3303 ax_simple (expr, aop_less_signed);
3304 ax_simple (expr, aop_log_not);
3305 i = ax_goto (expr, aop_if_goto);
3306 /* We have to emit 0 - X. */
3307 ax_const_l (expr, 0);
3308 ax_simple (expr, aop_swap);
3309 ax_simple (expr, aop_sub);
3310 ax_label (expr, i, expr->len);
3311 break;
3312
3313 case DW_OP_neg:
3314 /* No need to sign extend here. */
3315 ax_const_l (expr, 0);
3316 ax_simple (expr, aop_swap);
3317 ax_simple (expr, aop_sub);
3318 break;
3319
3320 case DW_OP_not:
3321 /* Sign extend the operand. */
3322 ax_ext (expr, addr_size_bits);
3323 ax_simple (expr, aop_bit_not);
3324 break;
3325
3326 case DW_OP_plus_uconst:
3327 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3328 /* It would be really weird to emit `DW_OP_plus_uconst 0',
3329 but we micro-optimize anyhow. */
3330 if (reg != 0)
3331 {
3332 ax_const_l (expr, reg);
3333 ax_simple (expr, aop_add);
3334 }
3335 break;
3336
3337 case DW_OP_and:
3338 ax_simple (expr, aop_bit_and);
3339 break;
3340
3341 case DW_OP_div:
3342 /* Sign extend the operands. */
3343 ax_ext (expr, addr_size_bits);
3344 ax_simple (expr, aop_swap);
3345 ax_ext (expr, addr_size_bits);
3346 ax_simple (expr, aop_swap);
3347 ax_simple (expr, aop_div_signed);
3348 break;
3349
3350 case DW_OP_minus:
3351 ax_simple (expr, aop_sub);
3352 break;
3353
3354 case DW_OP_mod:
3355 ax_simple (expr, aop_rem_unsigned);
3356 break;
3357
3358 case DW_OP_mul:
3359 ax_simple (expr, aop_mul);
3360 break;
3361
3362 case DW_OP_or:
3363 ax_simple (expr, aop_bit_or);
3364 break;
3365
3366 case DW_OP_plus:
3367 ax_simple (expr, aop_add);
3368 break;
3369
3370 case DW_OP_shl:
3371 ax_simple (expr, aop_lsh);
3372 break;
3373
3374 case DW_OP_shr:
3375 ax_simple (expr, aop_rsh_unsigned);
3376 break;
3377
3378 case DW_OP_shra:
3379 ax_simple (expr, aop_rsh_signed);
3380 break;
3381
3382 case DW_OP_xor:
3383 ax_simple (expr, aop_bit_xor);
3384 break;
3385
3386 case DW_OP_le:
3387 /* Sign extend the operands. */
3388 ax_ext (expr, addr_size_bits);
3389 ax_simple (expr, aop_swap);
3390 ax_ext (expr, addr_size_bits);
3391 /* Note no swap here: A <= B is !(B < A). */
3392 ax_simple (expr, aop_less_signed);
3393 ax_simple (expr, aop_log_not);
3394 break;
3395
3396 case DW_OP_ge:
3397 /* Sign extend the operands. */
3398 ax_ext (expr, addr_size_bits);
3399 ax_simple (expr, aop_swap);
3400 ax_ext (expr, addr_size_bits);
3401 ax_simple (expr, aop_swap);
3402 /* A >= B is !(A < B). */
3403 ax_simple (expr, aop_less_signed);
3404 ax_simple (expr, aop_log_not);
3405 break;
3406
3407 case DW_OP_eq:
3408 /* Sign extend the operands. */
3409 ax_ext (expr, addr_size_bits);
3410 ax_simple (expr, aop_swap);
3411 ax_ext (expr, addr_size_bits);
3412 /* No need for a second swap here. */
3413 ax_simple (expr, aop_equal);
3414 break;
3415
3416 case DW_OP_lt:
3417 /* Sign extend the operands. */
3418 ax_ext (expr, addr_size_bits);
3419 ax_simple (expr, aop_swap);
3420 ax_ext (expr, addr_size_bits);
3421 ax_simple (expr, aop_swap);
3422 ax_simple (expr, aop_less_signed);
3423 break;
3424
3425 case DW_OP_gt:
3426 /* Sign extend the operands. */
3427 ax_ext (expr, addr_size_bits);
3428 ax_simple (expr, aop_swap);
3429 ax_ext (expr, addr_size_bits);
3430 /* Note no swap here: A > B is B < A. */
3431 ax_simple (expr, aop_less_signed);
3432 break;
3433
3434 case DW_OP_ne:
3435 /* Sign extend the operands. */
3436 ax_ext (expr, addr_size_bits);
3437 ax_simple (expr, aop_swap);
3438 ax_ext (expr, addr_size_bits);
3439 /* No need for a swap here. */
3440 ax_simple (expr, aop_equal);
3441 ax_simple (expr, aop_log_not);
3442 break;
3443
3444 case DW_OP_call_frame_cfa:
3445 {
3446 int regnum;
3447 CORE_ADDR text_offset;
3448 LONGEST off;
3449 const gdb_byte *cfa_start, *cfa_end;
3450
3451 if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
3452 &regnum, &off,
3453 &text_offset, &cfa_start, &cfa_end))
3454 {
3455 /* Register. */
3456 ax_reg (expr, regnum);
3457 if (off != 0)
3458 {
3459 ax_const_l (expr, off);
3460 ax_simple (expr, aop_add);
3461 }
3462 }
3463 else
3464 {
3465 /* Another expression. */
3466 ax_const_l (expr, text_offset);
3467 dwarf2_compile_expr_to_ax (expr, loc, addr_size, cfa_start,
3468 cfa_end, per_cu, per_objfile);
3469 }
3470
3471 loc->kind = axs_lvalue_memory;
3472 }
3473 break;
3474
3475 case DW_OP_GNU_push_tls_address:
3476 case DW_OP_form_tls_address:
3477 unimplemented (op);
3478 break;
3479
3480 case DW_OP_push_object_address:
3481 unimplemented (op);
3482 break;
3483
3484 case DW_OP_skip:
3485 offset = extract_signed_integer (op_ptr, 2, byte_order);
3486 op_ptr += 2;
3487 i = ax_goto (expr, aop_goto);
3488 dw_labels.push_back (op_ptr + offset - base);
3489 patches.push_back (i);
3490 break;
3491
3492 case DW_OP_bra:
3493 offset = extract_signed_integer (op_ptr, 2, byte_order);
3494 op_ptr += 2;
3495 /* Zero extend the operand. */
3496 ax_zero_ext (expr, addr_size_bits);
3497 i = ax_goto (expr, aop_if_goto);
3498 dw_labels.push_back (op_ptr + offset - base);
3499 patches.push_back (i);
3500 break;
3501
3502 case DW_OP_nop:
3503 break;
3504
3505 case DW_OP_piece:
3506 case DW_OP_bit_piece:
3507 {
3508 uint64_t size;
3509
3510 if (op_ptr - 1 == previous_piece)
3511 error (_("Cannot translate empty pieces to agent expressions"));
3512 previous_piece = op_ptr - 1;
3513
3514 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3515 if (op == DW_OP_piece)
3516 {
3517 size *= 8;
3518 uoffset = 0;
3519 }
3520 else
3521 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3522
3523 if (bits_collected + size > 8 * sizeof (LONGEST))
3524 error (_("Expression pieces exceed word size"));
3525
3526 /* Access the bits. */
3527 switch (loc->kind)
3528 {
3529 case axs_lvalue_register:
3530 ax_reg (expr, loc->u.reg);
3531 break;
3532
3533 case axs_lvalue_memory:
3534 /* Offset the pointer, if needed. */
3535 if (uoffset > 8)
3536 {
3537 ax_const_l (expr, uoffset / 8);
3538 ax_simple (expr, aop_add);
3539 uoffset %= 8;
3540 }
3541 access_memory (arch, expr, size);
3542 break;
3543 }
3544
3545 /* For a bits-big-endian target, shift up what we already
3546 have. For a bits-little-endian target, shift up the
3547 new data. Note that there is a potential bug here if
3548 the DWARF expression leaves multiple values on the
3549 stack. */
3550 if (bits_collected > 0)
3551 {
3552 if (bits_big_endian)
3553 {
3554 ax_simple (expr, aop_swap);
3555 ax_const_l (expr, size);
3556 ax_simple (expr, aop_lsh);
3557 /* We don't need a second swap here, because
3558 aop_bit_or is symmetric. */
3559 }
3560 else
3561 {
3562 ax_const_l (expr, size);
3563 ax_simple (expr, aop_lsh);
3564 }
3565 ax_simple (expr, aop_bit_or);
3566 }
3567
3568 bits_collected += size;
3569 loc->kind = axs_rvalue;
3570 }
3571 break;
3572
3573 case DW_OP_GNU_uninit:
3574 unimplemented (op);
3575
3576 case DW_OP_call2:
3577 case DW_OP_call4:
3578 {
3579 struct dwarf2_locexpr_baton block;
3580 int size = (op == DW_OP_call2 ? 2 : 4);
3581
3582 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3583 op_ptr += size;
3584
3585 cu_offset cuoffset = (cu_offset) uoffset;
3586 block = dwarf2_fetch_die_loc_cu_off (cuoffset, per_cu,
3587 get_ax_pc, expr);
3588
3589 /* DW_OP_call_ref is currently not supported. */
3590 gdb_assert (block.per_cu == per_cu);
3591
3592 dwarf2_compile_expr_to_ax (expr, loc, addr_size, block.data,
3593 block.data + block.size, per_cu,
3594 per_objfile);
3595 }
3596 break;
3597
3598 case DW_OP_call_ref:
3599 unimplemented (op);
3600
3601 case DW_OP_GNU_variable_value:
3602 unimplemented (op);
3603
3604 default:
3605 unimplemented (op);
3606 }
3607 }
3608
3609 /* Patch all the branches we emitted. */
3610 for (int i = 0; i < patches.size (); ++i)
3611 {
3612 int targ = offsets[dw_labels[i]];
3613 if (targ == -1)
3614 internal_error (__FILE__, __LINE__, _("invalid label"));
3615 ax_label (expr, patches[i], targ);
3616 }
3617 }
3618
3619 \f
3620 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3621 evaluator to calculate the location. */
3622 static struct value *
3623 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3624 {
3625 struct dwarf2_locexpr_baton *dlbaton
3626 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3627 struct value *val;
3628
3629 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3630 dlbaton->size, dlbaton->per_cu);
3631
3632 return val;
3633 }
3634
3635 /* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3636 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3637 will be thrown. */
3638
3639 static struct value *
3640 locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3641 {
3642 struct dwarf2_locexpr_baton *dlbaton
3643 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3644
3645 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3646 dlbaton->size);
3647 }
3648
3649 /* Implementation of get_symbol_read_needs from
3650 symbol_computed_ops. */
3651
3652 static enum symbol_needs_kind
3653 locexpr_get_symbol_read_needs (struct symbol *symbol)
3654 {
3655 struct dwarf2_locexpr_baton *dlbaton
3656 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
3657
3658 return dwarf2_loc_desc_get_symbol_read_needs (dlbaton->data, dlbaton->size,
3659 dlbaton->per_cu);
3660 }
3661
3662 /* Return true if DATA points to the end of a piece. END is one past
3663 the last byte in the expression. */
3664
3665 static int
3666 piece_end_p (const gdb_byte *data, const gdb_byte *end)
3667 {
3668 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3669 }
3670
3671 /* Helper for locexpr_describe_location_piece that finds the name of a
3672 DWARF register. */
3673
3674 static const char *
3675 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3676 {
3677 int regnum;
3678
3679 /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3680 We'd rather print *something* here than throw an error. */
3681 regnum = dwarf_reg_to_regnum (gdbarch, dwarf_regnum);
3682 /* gdbarch_register_name may just return "", return something more
3683 descriptive for bad register numbers. */
3684 if (regnum == -1)
3685 {
3686 /* The text is output as "$bad_register_number".
3687 That is why we use the underscores. */
3688 return _("bad_register_number");
3689 }
3690 return gdbarch_register_name (gdbarch, regnum);
3691 }
3692
3693 /* Nicely describe a single piece of a location, returning an updated
3694 position in the bytecode sequence. This function cannot recognize
3695 all locations; if a location is not recognized, it simply returns
3696 DATA. If there is an error during reading, e.g. we run off the end
3697 of the buffer, an error is thrown. */
3698
3699 static const gdb_byte *
3700 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
3701 CORE_ADDR addr, struct objfile *objfile,
3702 struct dwarf2_per_cu_data *per_cu,
3703 const gdb_byte *data, const gdb_byte *end,
3704 unsigned int addr_size)
3705 {
3706 struct gdbarch *gdbarch = objfile->arch ();
3707 size_t leb128_size;
3708
3709 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3710 {
3711 fprintf_filtered (stream, _("a variable in $%s"),
3712 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
3713 data += 1;
3714 }
3715 else if (data[0] == DW_OP_regx)
3716 {
3717 uint64_t reg;
3718
3719 data = safe_read_uleb128 (data + 1, end, &reg);
3720 fprintf_filtered (stream, _("a variable in $%s"),
3721 locexpr_regname (gdbarch, reg));
3722 }
3723 else if (data[0] == DW_OP_fbreg)
3724 {
3725 const struct block *b;
3726 struct symbol *framefunc;
3727 int frame_reg = 0;
3728 int64_t frame_offset;
3729 const gdb_byte *base_data, *new_data, *save_data = data;
3730 size_t base_size;
3731 int64_t base_offset = 0;
3732
3733 new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
3734 if (!piece_end_p (new_data, end))
3735 return data;
3736 data = new_data;
3737
3738 b = block_for_pc (addr);
3739
3740 if (!b)
3741 error (_("No block found for address for symbol \"%s\"."),
3742 symbol->print_name ());
3743
3744 framefunc = block_linkage_function (b);
3745
3746 if (!framefunc)
3747 error (_("No function found for block for symbol \"%s\"."),
3748 symbol->print_name ());
3749
3750 func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
3751
3752 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3753 {
3754 const gdb_byte *buf_end;
3755
3756 frame_reg = base_data[0] - DW_OP_breg0;
3757 buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3758 &base_offset);
3759 if (buf_end != base_data + base_size)
3760 error (_("Unexpected opcode after "
3761 "DW_OP_breg%u for symbol \"%s\"."),
3762 frame_reg, symbol->print_name ());
3763 }
3764 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3765 {
3766 /* The frame base is just the register, with no offset. */
3767 frame_reg = base_data[0] - DW_OP_reg0;
3768 base_offset = 0;
3769 }
3770 else
3771 {
3772 /* We don't know what to do with the frame base expression,
3773 so we can't trace this variable; give up. */
3774 return save_data;
3775 }
3776
3777 fprintf_filtered (stream,
3778 _("a variable at frame base reg $%s offset %s+%s"),
3779 locexpr_regname (gdbarch, frame_reg),
3780 plongest (base_offset), plongest (frame_offset));
3781 }
3782 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3783 && piece_end_p (data, end))
3784 {
3785 int64_t offset;
3786
3787 data = safe_read_sleb128 (data + 1, end, &offset);
3788
3789 fprintf_filtered (stream,
3790 _("a variable at offset %s from base reg $%s"),
3791 plongest (offset),
3792 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
3793 }
3794
3795 /* The location expression for a TLS variable looks like this (on a
3796 64-bit LE machine):
3797
3798 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3799 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
3800
3801 0x3 is the encoding for DW_OP_addr, which has an operand as long
3802 as the size of an address on the target machine (here is 8
3803 bytes). Note that more recent version of GCC emit DW_OP_const4u
3804 or DW_OP_const8u, depending on address size, rather than
3805 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3806 The operand represents the offset at which the variable is within
3807 the thread local storage. */
3808
3809 else if (data + 1 + addr_size < end
3810 && (data[0] == DW_OP_addr
3811 || (addr_size == 4 && data[0] == DW_OP_const4u)
3812 || (addr_size == 8 && data[0] == DW_OP_const8u))
3813 && (data[1 + addr_size] == DW_OP_GNU_push_tls_address
3814 || data[1 + addr_size] == DW_OP_form_tls_address)
3815 && piece_end_p (data + 2 + addr_size, end))
3816 {
3817 ULONGEST offset;
3818 offset = extract_unsigned_integer (data + 1, addr_size,
3819 gdbarch_byte_order (gdbarch));
3820
3821 fprintf_filtered (stream,
3822 _("a thread-local variable at offset 0x%s "
3823 "in the thread-local storage for `%s'"),
3824 phex_nz (offset, addr_size), objfile_name (objfile));
3825
3826 data += 1 + addr_size + 1;
3827 }
3828
3829 /* With -gsplit-dwarf a TLS variable can also look like this:
3830 DW_AT_location : 3 byte block: fc 4 e0
3831 (DW_OP_GNU_const_index: 4;
3832 DW_OP_GNU_push_tls_address) */
3833 else if (data + 3 <= end
3834 && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3835 && data[0] == DW_OP_GNU_const_index
3836 && leb128_size > 0
3837 && (data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3838 || data[1 + leb128_size] == DW_OP_form_tls_address)
3839 && piece_end_p (data + 2 + leb128_size, end))
3840 {
3841 uint64_t offset;
3842
3843 data = safe_read_uleb128 (data + 1, end, &offset);
3844 offset = dwarf2_read_addr_index (per_cu, offset);
3845 fprintf_filtered (stream,
3846 _("a thread-local variable at offset 0x%s "
3847 "in the thread-local storage for `%s'"),
3848 phex_nz (offset, addr_size), objfile_name (objfile));
3849 ++data;
3850 }
3851
3852 else if (data[0] >= DW_OP_lit0
3853 && data[0] <= DW_OP_lit31
3854 && data + 1 < end
3855 && data[1] == DW_OP_stack_value)
3856 {
3857 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3858 data += 2;
3859 }
3860
3861 return data;
3862 }
3863
3864 /* Disassemble an expression, stopping at the end of a piece or at the
3865 end of the expression. Returns a pointer to the next unread byte
3866 in the input expression. If ALL is nonzero, then this function
3867 will keep going until it reaches the end of the expression.
3868 If there is an error during reading, e.g. we run off the end
3869 of the buffer, an error is thrown. */
3870
3871 static const gdb_byte *
3872 disassemble_dwarf_expression (struct ui_file *stream,
3873 struct gdbarch *arch, unsigned int addr_size,
3874 int offset_size, const gdb_byte *start,
3875 const gdb_byte *data, const gdb_byte *end,
3876 int indent, int all,
3877 struct dwarf2_per_cu_data *per_cu)
3878 {
3879 while (data < end
3880 && (all
3881 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3882 {
3883 enum dwarf_location_atom op = (enum dwarf_location_atom) *data++;
3884 uint64_t ul;
3885 int64_t l;
3886 const char *name;
3887
3888 name = get_DW_OP_name (op);
3889
3890 if (!name)
3891 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
3892 op, (long) (data - 1 - start));
3893 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3894 (long) (data - 1 - start), name);
3895
3896 switch (op)
3897 {
3898 case DW_OP_addr:
3899 ul = extract_unsigned_integer (data, addr_size,
3900 gdbarch_byte_order (arch));
3901 data += addr_size;
3902 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
3903 break;
3904
3905 case DW_OP_const1u:
3906 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3907 data += 1;
3908 fprintf_filtered (stream, " %s", pulongest (ul));
3909 break;
3910 case DW_OP_const1s:
3911 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3912 data += 1;
3913 fprintf_filtered (stream, " %s", plongest (l));
3914 break;
3915 case DW_OP_const2u:
3916 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3917 data += 2;
3918 fprintf_filtered (stream, " %s", pulongest (ul));
3919 break;
3920 case DW_OP_const2s:
3921 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3922 data += 2;
3923 fprintf_filtered (stream, " %s", plongest (l));
3924 break;
3925 case DW_OP_const4u:
3926 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3927 data += 4;
3928 fprintf_filtered (stream, " %s", pulongest (ul));
3929 break;
3930 case DW_OP_const4s:
3931 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3932 data += 4;
3933 fprintf_filtered (stream, " %s", plongest (l));
3934 break;
3935 case DW_OP_const8u:
3936 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
3937 data += 8;
3938 fprintf_filtered (stream, " %s", pulongest (ul));
3939 break;
3940 case DW_OP_const8s:
3941 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
3942 data += 8;
3943 fprintf_filtered (stream, " %s", plongest (l));
3944 break;
3945 case DW_OP_constu:
3946 data = safe_read_uleb128 (data, end, &ul);
3947 fprintf_filtered (stream, " %s", pulongest (ul));
3948 break;
3949 case DW_OP_consts:
3950 data = safe_read_sleb128 (data, end, &l);
3951 fprintf_filtered (stream, " %s", plongest (l));
3952 break;
3953
3954 case DW_OP_reg0:
3955 case DW_OP_reg1:
3956 case DW_OP_reg2:
3957 case DW_OP_reg3:
3958 case DW_OP_reg4:
3959 case DW_OP_reg5:
3960 case DW_OP_reg6:
3961 case DW_OP_reg7:
3962 case DW_OP_reg8:
3963 case DW_OP_reg9:
3964 case DW_OP_reg10:
3965 case DW_OP_reg11:
3966 case DW_OP_reg12:
3967 case DW_OP_reg13:
3968 case DW_OP_reg14:
3969 case DW_OP_reg15:
3970 case DW_OP_reg16:
3971 case DW_OP_reg17:
3972 case DW_OP_reg18:
3973 case DW_OP_reg19:
3974 case DW_OP_reg20:
3975 case DW_OP_reg21:
3976 case DW_OP_reg22:
3977 case DW_OP_reg23:
3978 case DW_OP_reg24:
3979 case DW_OP_reg25:
3980 case DW_OP_reg26:
3981 case DW_OP_reg27:
3982 case DW_OP_reg28:
3983 case DW_OP_reg29:
3984 case DW_OP_reg30:
3985 case DW_OP_reg31:
3986 fprintf_filtered (stream, " [$%s]",
3987 locexpr_regname (arch, op - DW_OP_reg0));
3988 break;
3989
3990 case DW_OP_regx:
3991 data = safe_read_uleb128 (data, end, &ul);
3992 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
3993 locexpr_regname (arch, (int) ul));
3994 break;
3995
3996 case DW_OP_implicit_value:
3997 data = safe_read_uleb128 (data, end, &ul);
3998 data += ul;
3999 fprintf_filtered (stream, " %s", pulongest (ul));
4000 break;
4001
4002 case DW_OP_breg0:
4003 case DW_OP_breg1:
4004 case DW_OP_breg2:
4005 case DW_OP_breg3:
4006 case DW_OP_breg4:
4007 case DW_OP_breg5:
4008 case DW_OP_breg6:
4009 case DW_OP_breg7:
4010 case DW_OP_breg8:
4011 case DW_OP_breg9:
4012 case DW_OP_breg10:
4013 case DW_OP_breg11:
4014 case DW_OP_breg12:
4015 case DW_OP_breg13:
4016 case DW_OP_breg14:
4017 case DW_OP_breg15:
4018 case DW_OP_breg16:
4019 case DW_OP_breg17:
4020 case DW_OP_breg18:
4021 case DW_OP_breg19:
4022 case DW_OP_breg20:
4023 case DW_OP_breg21:
4024 case DW_OP_breg22:
4025 case DW_OP_breg23:
4026 case DW_OP_breg24:
4027 case DW_OP_breg25:
4028 case DW_OP_breg26:
4029 case DW_OP_breg27:
4030 case DW_OP_breg28:
4031 case DW_OP_breg29:
4032 case DW_OP_breg30:
4033 case DW_OP_breg31:
4034 data = safe_read_sleb128 (data, end, &l);
4035 fprintf_filtered (stream, " %s [$%s]", plongest (l),
4036 locexpr_regname (arch, op - DW_OP_breg0));
4037 break;
4038
4039 case DW_OP_bregx:
4040 data = safe_read_uleb128 (data, end, &ul);
4041 data = safe_read_sleb128 (data, end, &l);
4042 fprintf_filtered (stream, " register %s [$%s] offset %s",
4043 pulongest (ul),
4044 locexpr_regname (arch, (int) ul),
4045 plongest (l));
4046 break;
4047
4048 case DW_OP_fbreg:
4049 data = safe_read_sleb128 (data, end, &l);
4050 fprintf_filtered (stream, " %s", plongest (l));
4051 break;
4052
4053 case DW_OP_xderef_size:
4054 case DW_OP_deref_size:
4055 case DW_OP_pick:
4056 fprintf_filtered (stream, " %d", *data);
4057 ++data;
4058 break;
4059
4060 case DW_OP_plus_uconst:
4061 data = safe_read_uleb128 (data, end, &ul);
4062 fprintf_filtered (stream, " %s", pulongest (ul));
4063 break;
4064
4065 case DW_OP_skip:
4066 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4067 data += 2;
4068 fprintf_filtered (stream, " to %ld",
4069 (long) (data + l - start));
4070 break;
4071
4072 case DW_OP_bra:
4073 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4074 data += 2;
4075 fprintf_filtered (stream, " %ld",
4076 (long) (data + l - start));
4077 break;
4078
4079 case DW_OP_call2:
4080 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
4081 data += 2;
4082 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
4083 break;
4084
4085 case DW_OP_call4:
4086 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4087 data += 4;
4088 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4089 break;
4090
4091 case DW_OP_call_ref:
4092 ul = extract_unsigned_integer (data, offset_size,
4093 gdbarch_byte_order (arch));
4094 data += offset_size;
4095 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4096 break;
4097
4098 case DW_OP_piece:
4099 data = safe_read_uleb128 (data, end, &ul);
4100 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
4101 break;
4102
4103 case DW_OP_bit_piece:
4104 {
4105 uint64_t offset;
4106
4107 data = safe_read_uleb128 (data, end, &ul);
4108 data = safe_read_uleb128 (data, end, &offset);
4109 fprintf_filtered (stream, " size %s offset %s (bits)",
4110 pulongest (ul), pulongest (offset));
4111 }
4112 break;
4113
4114 case DW_OP_implicit_pointer:
4115 case DW_OP_GNU_implicit_pointer:
4116 {
4117 ul = extract_unsigned_integer (data, offset_size,
4118 gdbarch_byte_order (arch));
4119 data += offset_size;
4120
4121 data = safe_read_sleb128 (data, end, &l);
4122
4123 fprintf_filtered (stream, " DIE %s offset %s",
4124 phex_nz (ul, offset_size),
4125 plongest (l));
4126 }
4127 break;
4128
4129 case DW_OP_deref_type:
4130 case DW_OP_GNU_deref_type:
4131 {
4132 int deref_addr_size = *data++;
4133 struct type *type;
4134
4135 data = safe_read_uleb128 (data, end, &ul);
4136 cu_offset offset = (cu_offset) ul;
4137 type = dwarf2_get_die_type (offset, per_cu);
4138 fprintf_filtered (stream, "<");
4139 type_print (type, "", stream, -1);
4140 fprintf_filtered (stream, " [0x%s]> %d",
4141 phex_nz (to_underlying (offset), 0),
4142 deref_addr_size);
4143 }
4144 break;
4145
4146 case DW_OP_const_type:
4147 case DW_OP_GNU_const_type:
4148 {
4149 struct type *type;
4150
4151 data = safe_read_uleb128 (data, end, &ul);
4152 cu_offset type_die = (cu_offset) ul;
4153 type = dwarf2_get_die_type (type_die, per_cu);
4154 fprintf_filtered (stream, "<");
4155 type_print (type, "", stream, -1);
4156 fprintf_filtered (stream, " [0x%s]>",
4157 phex_nz (to_underlying (type_die), 0));
4158
4159 int n = *data++;
4160 fprintf_filtered (stream, " %d byte block:", n);
4161 for (int i = 0; i < n; ++i)
4162 fprintf_filtered (stream, " %02x", data[i]);
4163 data += n;
4164 }
4165 break;
4166
4167 case DW_OP_regval_type:
4168 case DW_OP_GNU_regval_type:
4169 {
4170 uint64_t reg;
4171 struct type *type;
4172
4173 data = safe_read_uleb128 (data, end, &reg);
4174 data = safe_read_uleb128 (data, end, &ul);
4175 cu_offset type_die = (cu_offset) ul;
4176
4177 type = dwarf2_get_die_type (type_die, per_cu);
4178 fprintf_filtered (stream, "<");
4179 type_print (type, "", stream, -1);
4180 fprintf_filtered (stream, " [0x%s]> [$%s]",
4181 phex_nz (to_underlying (type_die), 0),
4182 locexpr_regname (arch, reg));
4183 }
4184 break;
4185
4186 case DW_OP_convert:
4187 case DW_OP_GNU_convert:
4188 case DW_OP_reinterpret:
4189 case DW_OP_GNU_reinterpret:
4190 {
4191 data = safe_read_uleb128 (data, end, &ul);
4192 cu_offset type_die = (cu_offset) ul;
4193
4194 if (to_underlying (type_die) == 0)
4195 fprintf_filtered (stream, "<0>");
4196 else
4197 {
4198 struct type *type;
4199
4200 type = dwarf2_get_die_type (type_die, per_cu);
4201 fprintf_filtered (stream, "<");
4202 type_print (type, "", stream, -1);
4203 fprintf_filtered (stream, " [0x%s]>",
4204 phex_nz (to_underlying (type_die), 0));
4205 }
4206 }
4207 break;
4208
4209 case DW_OP_entry_value:
4210 case DW_OP_GNU_entry_value:
4211 data = safe_read_uleb128 (data, end, &ul);
4212 fputc_filtered ('\n', stream);
4213 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
4214 start, data, data + ul, indent + 2,
4215 all, per_cu);
4216 data += ul;
4217 continue;
4218
4219 case DW_OP_GNU_parameter_ref:
4220 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4221 data += 4;
4222 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4223 break;
4224
4225 case DW_OP_addrx:
4226 case DW_OP_GNU_addr_index:
4227 data = safe_read_uleb128 (data, end, &ul);
4228 ul = dwarf2_read_addr_index (per_cu, ul);
4229 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
4230 break;
4231 case DW_OP_GNU_const_index:
4232 data = safe_read_uleb128 (data, end, &ul);
4233 ul = dwarf2_read_addr_index (per_cu, ul);
4234 fprintf_filtered (stream, " %s", pulongest (ul));
4235 break;
4236
4237 case DW_OP_GNU_variable_value:
4238 ul = extract_unsigned_integer (data, offset_size,
4239 gdbarch_byte_order (arch));
4240 data += offset_size;
4241 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4242 break;
4243 }
4244
4245 fprintf_filtered (stream, "\n");
4246 }
4247
4248 return data;
4249 }
4250
4251 static bool dwarf_always_disassemble;
4252
4253 static void
4254 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
4255 struct cmd_list_element *c, const char *value)
4256 {
4257 fprintf_filtered (file,
4258 _("Whether to always disassemble "
4259 "DWARF expressions is %s.\n"),
4260 value);
4261 }
4262
4263 /* Describe a single location, which may in turn consist of multiple
4264 pieces. */
4265
4266 static void
4267 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
4268 struct ui_file *stream,
4269 const gdb_byte *data, size_t size,
4270 struct objfile *objfile, unsigned int addr_size,
4271 int offset_size, struct dwarf2_per_cu_data *per_cu)
4272 {
4273 const gdb_byte *end = data + size;
4274 int first_piece = 1, bad = 0;
4275
4276 while (data < end)
4277 {
4278 const gdb_byte *here = data;
4279 int disassemble = 1;
4280
4281 if (first_piece)
4282 first_piece = 0;
4283 else
4284 fprintf_filtered (stream, _(", and "));
4285
4286 if (!dwarf_always_disassemble)
4287 {
4288 data = locexpr_describe_location_piece (symbol, stream,
4289 addr, objfile, per_cu,
4290 data, end, addr_size);
4291 /* If we printed anything, or if we have an empty piece,
4292 then don't disassemble. */
4293 if (data != here
4294 || data[0] == DW_OP_piece
4295 || data[0] == DW_OP_bit_piece)
4296 disassemble = 0;
4297 }
4298 if (disassemble)
4299 {
4300 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
4301 data = disassemble_dwarf_expression (stream,
4302 objfile->arch (),
4303 addr_size, offset_size, data,
4304 data, end, 0,
4305 dwarf_always_disassemble,
4306 per_cu);
4307 }
4308
4309 if (data < end)
4310 {
4311 int empty = data == here;
4312
4313 if (disassemble)
4314 fprintf_filtered (stream, " ");
4315 if (data[0] == DW_OP_piece)
4316 {
4317 uint64_t bytes;
4318
4319 data = safe_read_uleb128 (data + 1, end, &bytes);
4320
4321 if (empty)
4322 fprintf_filtered (stream, _("an empty %s-byte piece"),
4323 pulongest (bytes));
4324 else
4325 fprintf_filtered (stream, _(" [%s-byte piece]"),
4326 pulongest (bytes));
4327 }
4328 else if (data[0] == DW_OP_bit_piece)
4329 {
4330 uint64_t bits, offset;
4331
4332 data = safe_read_uleb128 (data + 1, end, &bits);
4333 data = safe_read_uleb128 (data, end, &offset);
4334
4335 if (empty)
4336 fprintf_filtered (stream,
4337 _("an empty %s-bit piece"),
4338 pulongest (bits));
4339 else
4340 fprintf_filtered (stream,
4341 _(" [%s-bit piece, offset %s bits]"),
4342 pulongest (bits), pulongest (offset));
4343 }
4344 else
4345 {
4346 bad = 1;
4347 break;
4348 }
4349 }
4350 }
4351
4352 if (bad || data > end)
4353 error (_("Corrupted DWARF2 expression for \"%s\"."),
4354 symbol->print_name ());
4355 }
4356
4357 /* Print a natural-language description of SYMBOL to STREAM. This
4358 version is for a symbol with a single location. */
4359
4360 static void
4361 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
4362 struct ui_file *stream)
4363 {
4364 struct dwarf2_locexpr_baton *dlbaton
4365 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4366 dwarf2_per_objfile *per_objfile = dlbaton->per_objfile;
4367 struct objfile *objfile = per_objfile->objfile;
4368 unsigned int addr_size = dlbaton->per_cu->addr_size ();
4369 int offset_size = dlbaton->per_cu->offset_size ();
4370
4371 locexpr_describe_location_1 (symbol, addr, stream,
4372 dlbaton->data, dlbaton->size,
4373 objfile, addr_size, offset_size,
4374 dlbaton->per_cu);
4375 }
4376
4377 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4378 any necessary bytecode in AX. */
4379
4380 static void
4381 locexpr_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
4382 struct axs_value *value)
4383 {
4384 struct dwarf2_locexpr_baton *dlbaton
4385 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4386 unsigned int addr_size = dlbaton->per_cu->addr_size ();
4387
4388 if (dlbaton->size == 0)
4389 value->optimized_out = 1;
4390 else
4391 dwarf2_compile_expr_to_ax (ax, value, addr_size, dlbaton->data,
4392 dlbaton->data + dlbaton->size, dlbaton->per_cu,
4393 dlbaton->per_objfile);
4394 }
4395
4396 /* symbol_computed_ops 'generate_c_location' method. */
4397
4398 static void
4399 locexpr_generate_c_location (struct symbol *sym, string_file *stream,
4400 struct gdbarch *gdbarch,
4401 unsigned char *registers_used,
4402 CORE_ADDR pc, const char *result_name)
4403 {
4404 struct dwarf2_locexpr_baton *dlbaton
4405 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
4406 unsigned int addr_size = dlbaton->per_cu->addr_size ();
4407
4408 if (dlbaton->size == 0)
4409 error (_("symbol \"%s\" is optimized out"), sym->natural_name ());
4410
4411 compile_dwarf_expr_to_c (stream, result_name,
4412 sym, pc, gdbarch, registers_used, addr_size,
4413 dlbaton->data, dlbaton->data + dlbaton->size,
4414 dlbaton->per_cu, dlbaton->per_objfile);
4415 }
4416
4417 /* The set of location functions used with the DWARF-2 expression
4418 evaluator. */
4419 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4420 locexpr_read_variable,
4421 locexpr_read_variable_at_entry,
4422 locexpr_get_symbol_read_needs,
4423 locexpr_describe_location,
4424 0, /* location_has_loclist */
4425 locexpr_tracepoint_var_ref,
4426 locexpr_generate_c_location
4427 };
4428
4429
4430 /* Wrapper functions for location lists. These generally find
4431 the appropriate location expression and call something above. */
4432
4433 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4434 evaluator to calculate the location. */
4435 static struct value *
4436 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4437 {
4438 struct dwarf2_loclist_baton *dlbaton
4439 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4440 struct value *val;
4441 const gdb_byte *data;
4442 size_t size;
4443 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
4444
4445 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4446 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
4447 dlbaton->per_cu);
4448
4449 return val;
4450 }
4451
4452 /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4453 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4454 will be thrown.
4455
4456 Function always returns non-NULL value, it may be marked optimized out if
4457 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
4458 if it cannot resolve the parameter for any reason. */
4459
4460 static struct value *
4461 loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4462 {
4463 struct dwarf2_loclist_baton *dlbaton
4464 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4465 const gdb_byte *data;
4466 size_t size;
4467 CORE_ADDR pc;
4468
4469 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4470 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4471
4472 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4473 if (data == NULL)
4474 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4475
4476 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4477 }
4478
4479 /* Implementation of get_symbol_read_needs from
4480 symbol_computed_ops. */
4481
4482 static enum symbol_needs_kind
4483 loclist_symbol_needs (struct symbol *symbol)
4484 {
4485 /* If there's a location list, then assume we need to have a frame
4486 to choose the appropriate location expression. With tracking of
4487 global variables this is not necessarily true, but such tracking
4488 is disabled in GCC at the moment until we figure out how to
4489 represent it. */
4490
4491 return SYMBOL_NEEDS_FRAME;
4492 }
4493
4494 /* Print a natural-language description of SYMBOL to STREAM. This
4495 version applies when there is a list of different locations, each
4496 with a specified address range. */
4497
4498 static void
4499 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4500 struct ui_file *stream)
4501 {
4502 struct dwarf2_loclist_baton *dlbaton
4503 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4504 const gdb_byte *loc_ptr, *buf_end;
4505 dwarf2_per_objfile *per_objfile = dlbaton->per_objfile;
4506 struct objfile *objfile = per_objfile->objfile;
4507 struct gdbarch *gdbarch = objfile->arch ();
4508 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4509 unsigned int addr_size = dlbaton->per_cu->addr_size ();
4510 int offset_size = dlbaton->per_cu->offset_size ();
4511 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
4512 /* Adjust base_address for relocatable objects. */
4513 CORE_ADDR base_offset = objfile->text_section_offset ();
4514 CORE_ADDR base_address = dlbaton->base_address + base_offset;
4515 int done = 0;
4516
4517 loc_ptr = dlbaton->data;
4518 buf_end = dlbaton->data + dlbaton->size;
4519
4520 fprintf_filtered (stream, _("multi-location:\n"));
4521
4522 /* Iterate through locations until we run out. */
4523 while (!done)
4524 {
4525 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4526 int length;
4527 enum debug_loc_kind kind;
4528 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4529
4530 if (dlbaton->per_cu->version () < 5 && dlbaton->from_dwo)
4531 kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
4532 loc_ptr, buf_end, &new_ptr,
4533 &low, &high, byte_order);
4534 else if (dlbaton->per_cu->version () < 5)
4535 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4536 &low, &high,
4537 byte_order, addr_size,
4538 signed_addr_p);
4539 else
4540 kind = decode_debug_loclists_addresses (dlbaton->per_cu,
4541 loc_ptr, buf_end, &new_ptr,
4542 &low, &high, byte_order,
4543 addr_size, signed_addr_p);
4544 loc_ptr = new_ptr;
4545 switch (kind)
4546 {
4547 case DEBUG_LOC_END_OF_LIST:
4548 done = 1;
4549 continue;
4550 case DEBUG_LOC_BASE_ADDRESS:
4551 base_address = high + base_offset;
4552 fprintf_filtered (stream, _(" Base address %s"),
4553 paddress (gdbarch, base_address));
4554 continue;
4555 case DEBUG_LOC_START_END:
4556 case DEBUG_LOC_START_LENGTH:
4557 case DEBUG_LOC_OFFSET_PAIR:
4558 break;
4559 case DEBUG_LOC_BUFFER_OVERFLOW:
4560 case DEBUG_LOC_INVALID_ENTRY:
4561 error (_("Corrupted DWARF expression for symbol \"%s\"."),
4562 symbol->print_name ());
4563 default:
4564 gdb_assert_not_reached ("bad debug_loc_kind");
4565 }
4566
4567 /* Otherwise, a location expression entry. */
4568 low += base_address;
4569 high += base_address;
4570
4571 low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
4572 high = gdbarch_adjust_dwarf2_addr (gdbarch, high);
4573
4574 if (dlbaton->per_cu->version () < 5)
4575 {
4576 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4577 loc_ptr += 2;
4578 }
4579 else
4580 {
4581 unsigned int bytes_read;
4582 length = read_unsigned_leb128 (NULL, loc_ptr, &bytes_read);
4583 loc_ptr += bytes_read;
4584 }
4585
4586 /* (It would improve readability to print only the minimum
4587 necessary digits of the second number of the range.) */
4588 fprintf_filtered (stream, _(" Range %s-%s: "),
4589 paddress (gdbarch, low), paddress (gdbarch, high));
4590
4591 /* Now describe this particular location. */
4592 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
4593 objfile, addr_size, offset_size,
4594 dlbaton->per_cu);
4595
4596 fprintf_filtered (stream, "\n");
4597
4598 loc_ptr += length;
4599 }
4600 }
4601
4602 /* Describe the location of SYMBOL as an agent value in VALUE, generating
4603 any necessary bytecode in AX. */
4604 static void
4605 loclist_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
4606 struct axs_value *value)
4607 {
4608 struct dwarf2_loclist_baton *dlbaton
4609 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
4610 const gdb_byte *data;
4611 size_t size;
4612 unsigned int addr_size = dlbaton->per_cu->addr_size ();
4613
4614 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
4615 if (size == 0)
4616 value->optimized_out = 1;
4617 else
4618 dwarf2_compile_expr_to_ax (ax, value, addr_size, data, data + size,
4619 dlbaton->per_cu, dlbaton->per_objfile);
4620 }
4621
4622 /* symbol_computed_ops 'generate_c_location' method. */
4623
4624 static void
4625 loclist_generate_c_location (struct symbol *sym, string_file *stream,
4626 struct gdbarch *gdbarch,
4627 unsigned char *registers_used,
4628 CORE_ADDR pc, const char *result_name)
4629 {
4630 struct dwarf2_loclist_baton *dlbaton
4631 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
4632 unsigned int addr_size = dlbaton->per_cu->addr_size ();
4633 const gdb_byte *data;
4634 size_t size;
4635
4636 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4637 if (size == 0)
4638 error (_("symbol \"%s\" is optimized out"), sym->natural_name ());
4639
4640 compile_dwarf_expr_to_c (stream, result_name,
4641 sym, pc, gdbarch, registers_used, addr_size,
4642 data, data + size,
4643 dlbaton->per_cu,
4644 dlbaton->per_objfile);
4645 }
4646
4647 /* The set of location functions used with the DWARF-2 expression
4648 evaluator and location lists. */
4649 const struct symbol_computed_ops dwarf2_loclist_funcs = {
4650 loclist_read_variable,
4651 loclist_read_variable_at_entry,
4652 loclist_symbol_needs,
4653 loclist_describe_location,
4654 1, /* location_has_loclist */
4655 loclist_tracepoint_var_ref,
4656 loclist_generate_c_location
4657 };
4658
4659 void _initialize_dwarf2loc ();
4660 void
4661 _initialize_dwarf2loc ()
4662 {
4663 add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4664 &entry_values_debug,
4665 _("Set entry values and tail call frames "
4666 "debugging."),
4667 _("Show entry values and tail call frames "
4668 "debugging."),
4669 _("When non-zero, the process of determining "
4670 "parameter values from function entry point "
4671 "and tail call frames will be printed."),
4672 NULL,
4673 show_entry_values_debug,
4674 &setdebuglist, &showdebuglist);
4675
4676 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
4677 &dwarf_always_disassemble, _("\
4678 Set whether `info address' always disassembles DWARF expressions."), _("\
4679 Show whether `info address' always disassembles DWARF expressions."), _("\
4680 When enabled, DWARF expressions are always printed in an assembly-like\n\
4681 syntax. When disabled, expressions will be printed in a more\n\
4682 conversational style, when possible."),
4683 NULL,
4684 show_dwarf_always_disassemble,
4685 &set_dwarf_cmdlist,
4686 &show_dwarf_cmdlist);
4687 }
This page took 0.117886 seconds and 5 git commands to generate.