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