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