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