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