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