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