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