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