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