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