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