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