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