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