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