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