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