gdb: use bool in dwarf2_loc_desc_get_symbol_read_needs
[deliverable/binutils-gdb.git] / gdb / dwarf2 / loc.c
CommitLineData
4c2df51b 1/* DWARF 2 location expression support for GDB.
feb13ab0 2
b811d2c2 3 Copyright (C) 2003-2020 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,
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,
1751 this_size, buffer.data (),
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,
1776 this_size, buffer.data (),
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,
1795 this_size, buffer.data ());
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
TT
2501 CORE_ADDR *valp,
2502 bool push_initial_value)
80180f79 2503{
80180f79
SA
2504 if (dlbaton == NULL || dlbaton->size == 0)
2505 return 0;
2506
89b07335
SM
2507 dwarf2_per_objfile *per_objfile = dlbaton->per_objfile;
2508 evaluate_for_locexpr_baton ctx (per_objfile);
80180f79 2509
192ca6d8
TT
2510 ctx.frame = frame;
2511 ctx.per_cu = dlbaton->per_cu;
b249d2c2
TT
2512 if (addr_stack == nullptr)
2513 ctx.obj_address = 0;
2514 else
2515 {
2516 ctx.obj_address = addr_stack->addr;
2517 ctx.data_view = addr_stack->valaddr;
2518 }
80180f79 2519
89b07335 2520 ctx.gdbarch = per_objfile->objfile->arch ();
09ba997f
TT
2521 ctx.addr_size = dlbaton->per_cu->addr_size ();
2522 ctx.ref_addr_size = dlbaton->per_cu->ref_addr_size ();
80180f79 2523
61122aa9 2524 if (push_initial_value)
b249d2c2 2525 ctx.push_address (ctx.obj_address, false);
61122aa9 2526
a70b8144 2527 try
16f808ec
TV
2528 {
2529 ctx.eval (dlbaton->data, dlbaton->size);
2530 }
230d2906 2531 catch (const gdb_exception_error &ex)
16f808ec
TV
2532 {
2533 if (ex.error == NOT_AVAILABLE_ERROR)
2534 {
2535 return 0;
2536 }
2537 else if (ex.error == NO_ENTRY_VALUE_ERROR)
2538 {
2539 if (entry_values_debug)
2540 exception_print (gdb_stdout, ex);
2541 return 0;
2542 }
2543 else
eedc3f4f 2544 throw;
16f808ec 2545 }
80180f79 2546
718b9626 2547 switch (ctx.location)
80180f79
SA
2548 {
2549 case DWARF_VALUE_REGISTER:
2550 case DWARF_VALUE_MEMORY:
2551 case DWARF_VALUE_STACK:
595d2e30 2552 *valp = ctx.fetch_address (0);
718b9626 2553 if (ctx.location == DWARF_VALUE_REGISTER)
192ca6d8 2554 *valp = ctx.read_addr_from_reg (*valp);
80180f79
SA
2555 return 1;
2556 case DWARF_VALUE_LITERAL:
718b9626
TT
2557 *valp = extract_signed_integer (ctx.data, ctx.len,
2558 gdbarch_byte_order (ctx.gdbarch));
80180f79
SA
2559 return 1;
2560 /* Unsupported dwarf values. */
2561 case DWARF_VALUE_OPTIMIZED_OUT:
2562 case DWARF_VALUE_IMPLICIT_POINTER:
2563 break;
2564 }
2565
80180f79
SA
2566 return 0;
2567}
2568
2569/* See dwarf2loc.h. */
2570
603490bf 2571bool
08412b07 2572dwarf2_evaluate_property (const struct dynamic_prop *prop,
63e43d3a 2573 struct frame_info *frame,
fe26d3a3 2574 const struct property_addr_info *addr_stack,
61122aa9
TT
2575 CORE_ADDR *value,
2576 bool push_initial_value)
80180f79
SA
2577{
2578 if (prop == NULL)
603490bf 2579 return false;
80180f79 2580
63e43d3a
PMR
2581 if (frame == NULL && has_stack_frames ())
2582 frame = get_selected_frame (NULL);
2583
8c2e4e06 2584 switch (prop->kind ())
80180f79
SA
2585 {
2586 case PROP_LOCEXPR:
2587 {
9a3c8263 2588 const struct dwarf2_property_baton *baton
8c2e4e06 2589 = (const struct dwarf2_property_baton *) prop->baton ();
9a49df9d 2590 gdb_assert (baton->property_type != NULL);
80180f79 2591
b249d2c2 2592 if (dwarf2_locexpr_baton_eval (&baton->locexpr, frame, addr_stack,
61122aa9 2593 value, push_initial_value))
80180f79 2594 {
9a49df9d 2595 if (baton->locexpr.is_reference)
80180f79 2596 {
9a49df9d 2597 struct value *val = value_at (baton->property_type, *value);
80180f79
SA
2598 *value = value_as_address (val);
2599 }
0d4e84ed
AB
2600 else
2601 {
2602 gdb_assert (baton->property_type != NULL);
2603
2604 struct type *type = check_typedef (baton->property_type);
2605 if (TYPE_LENGTH (type) < sizeof (CORE_ADDR)
2606 && !TYPE_UNSIGNED (type))
2607 {
2608 /* If we have a valid return candidate and it's value
2609 is signed, we have to sign-extend the value because
2610 CORE_ADDR on 64bit machine has 8 bytes but address
2611 size of an 32bit application is bytes. */
2612 const int addr_size
09ba997f 2613 = (baton->locexpr.per_cu->addr_size ()
0d4e84ed
AB
2614 * TARGET_CHAR_BIT);
2615 const CORE_ADDR neg_mask
2616 = (~((CORE_ADDR) 0) << (addr_size - 1));
2617
2618 /* Check if signed bit is set and sign-extend values. */
2619 if (*value & neg_mask)
2620 *value |= neg_mask;
2621 }
2622 }
603490bf 2623 return true;
80180f79
SA
2624 }
2625 }
2626 break;
2627
2628 case PROP_LOCLIST:
2629 {
9a3c8263 2630 struct dwarf2_property_baton *baton
8c2e4e06 2631 = (struct dwarf2_property_baton *) prop->baton ();
1c33af77 2632 CORE_ADDR pc;
80180f79
SA
2633 const gdb_byte *data;
2634 struct value *val;
2635 size_t size;
2636
1c33af77
TV
2637 if (frame == NULL
2638 || !get_frame_address_in_block_if_available (frame, &pc))
2639 return false;
2640
80180f79
SA
2641 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2642 if (data != NULL)
2643 {
9a49df9d 2644 val = dwarf2_evaluate_loc_desc (baton->property_type, frame, data,
9f47c707
SM
2645 size, baton->loclist.per_cu,
2646 baton->loclist.per_objfile);
80180f79
SA
2647 if (!value_optimized_out (val))
2648 {
2649 *value = value_as_address (val);
603490bf 2650 return true;
80180f79
SA
2651 }
2652 }
2653 }
2654 break;
2655
2656 case PROP_CONST:
8c2e4e06 2657 *value = prop->const_val ();
603490bf 2658 return true;
df25ebbd
JB
2659
2660 case PROP_ADDR_OFFSET:
2661 {
9a3c8263 2662 struct dwarf2_property_baton *baton
8c2e4e06 2663 = (struct dwarf2_property_baton *) prop->baton ();
fe26d3a3 2664 const struct property_addr_info *pinfo;
df25ebbd
JB
2665 struct value *val;
2666
2667 for (pinfo = addr_stack; pinfo != NULL; pinfo = pinfo->next)
988915ee
TT
2668 {
2669 /* This approach lets us avoid checking the qualifiers. */
2670 if (TYPE_MAIN_TYPE (pinfo->type)
9a49df9d 2671 == TYPE_MAIN_TYPE (baton->property_type))
988915ee
TT
2672 break;
2673 }
df25ebbd 2674 if (pinfo == NULL)
2c811c0f 2675 error (_("cannot find reference address for offset property"));
b249d2c2 2676 if (pinfo->valaddr.data () != NULL)
c3345124
JB
2677 val = value_from_contents
2678 (baton->offset_info.type,
b249d2c2 2679 pinfo->valaddr.data () + baton->offset_info.offset);
c3345124
JB
2680 else
2681 val = value_at (baton->offset_info.type,
2682 pinfo->addr + baton->offset_info.offset);
df25ebbd 2683 *value = value_as_address (val);
603490bf 2684 return true;
df25ebbd 2685 }
80180f79
SA
2686 }
2687
603490bf 2688 return false;
80180f79
SA
2689}
2690
bb2ec1b3
TT
2691/* See dwarf2loc.h. */
2692
2693void
d82b3862 2694dwarf2_compile_property_to_c (string_file *stream,
bb2ec1b3
TT
2695 const char *result_name,
2696 struct gdbarch *gdbarch,
2697 unsigned char *registers_used,
2698 const struct dynamic_prop *prop,
2699 CORE_ADDR pc,
2700 struct symbol *sym)
2701{
9a3c8263 2702 struct dwarf2_property_baton *baton
8c2e4e06 2703 = (struct dwarf2_property_baton *) prop->baton ();
bb2ec1b3
TT
2704 const gdb_byte *data;
2705 size_t size;
4b167ea1
SM
2706 dwarf2_per_cu_data *per_cu;
2707 dwarf2_per_objfile *per_objfile;
bb2ec1b3 2708
8c2e4e06 2709 if (prop->kind () == PROP_LOCEXPR)
bb2ec1b3
TT
2710 {
2711 data = baton->locexpr.data;
2712 size = baton->locexpr.size;
2713 per_cu = baton->locexpr.per_cu;
4b167ea1 2714 per_objfile = baton->locexpr.per_objfile;
bb2ec1b3
TT
2715 }
2716 else
2717 {
8c2e4e06 2718 gdb_assert (prop->kind () == PROP_LOCLIST);
bb2ec1b3
TT
2719
2720 data = dwarf2_find_location_expression (&baton->loclist, &size, pc);
2721 per_cu = baton->loclist.per_cu;
4b167ea1 2722 per_objfile = baton->loclist.per_objfile;
bb2ec1b3
TT
2723 }
2724
2725 compile_dwarf_bounds_to_c (stream, result_name, prop, sym, pc,
2726 gdbarch, registers_used,
09ba997f 2727 per_cu->addr_size (),
4b167ea1 2728 data, data + size, per_cu, per_objfile);
bb2ec1b3
TT
2729}
2730
4c2df51b 2731\f
0b31a4bc 2732/* Helper functions and baton for dwarf2_loc_desc_get_symbol_read_needs. */
4c2df51b 2733
192ca6d8 2734class symbol_needs_eval_context : public dwarf_expr_context
4c2df51b 2735{
89b07335
SM
2736public:
2737 symbol_needs_eval_context (dwarf2_per_objfile *per_objfile)
2738 : dwarf_expr_context (per_objfile)
2739 {}
192ca6d8 2740
0b31a4bc 2741 enum symbol_needs_kind needs;
17ea53c3 2742 struct dwarf2_per_cu_data *per_cu;
4c2df51b 2743
192ca6d8 2744 /* Reads from registers do require a frame. */
632e107b 2745 CORE_ADDR read_addr_from_reg (int regnum) override
192ca6d8
TT
2746 {
2747 needs = SYMBOL_NEEDS_FRAME;
2748 return 1;
2749 }
2750
2751 /* "get_reg_value" callback: Reads from registers do require a
2752 frame. */
2753
632e107b 2754 struct value *get_reg_value (struct type *type, int regnum) override
192ca6d8
TT
2755 {
2756 needs = SYMBOL_NEEDS_FRAME;
2757 return value_zero (type, not_lval);
2758 }
2759
2760 /* Reads from memory do not require a frame. */
632e107b 2761 void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t len) override
192ca6d8
TT
2762 {
2763 memset (buf, 0, len);
2764 }
2765
2766 /* Frame-relative accesses do require a frame. */
632e107b 2767 void get_frame_base (const gdb_byte **start, size_t *length) override
192ca6d8
TT
2768 {
2769 static gdb_byte lit0 = DW_OP_lit0;
2770
2771 *start = &lit0;
2772 *length = 1;
2773
2774 needs = SYMBOL_NEEDS_FRAME;
2775 }
2776
2777 /* CFA accesses require a frame. */
632e107b 2778 CORE_ADDR get_frame_cfa () override
192ca6d8
TT
2779 {
2780 needs = SYMBOL_NEEDS_FRAME;
2781 return 1;
2782 }
2783
632e107b 2784 CORE_ADDR get_frame_pc () override
7d5697f9
TT
2785 {
2786 needs = SYMBOL_NEEDS_FRAME;
2787 return 1;
2788 }
2789
192ca6d8 2790 /* Thread-local accesses require registers, but not a frame. */
632e107b 2791 CORE_ADDR get_tls_address (CORE_ADDR offset) override
192ca6d8
TT
2792 {
2793 if (needs <= SYMBOL_NEEDS_REGISTERS)
2794 needs = SYMBOL_NEEDS_REGISTERS;
2795 return 1;
2796 }
2797
2798 /* Helper interface of per_cu_dwarf_call for
2799 dwarf2_loc_desc_get_symbol_read_needs. */
2800
632e107b 2801 void dwarf_call (cu_offset die_offset) override
192ca6d8 2802 {
14095eb3 2803 per_cu_dwarf_call (this, die_offset, per_cu, per_objfile);
192ca6d8
TT
2804 }
2805
a6b786da
KB
2806 /* Helper interface of sect_variable_value for
2807 dwarf2_loc_desc_get_symbol_read_needs. */
2808
2809 struct value *dwarf_variable_value (sect_offset sect_off) override
2810 {
14095eb3 2811 return sect_variable_value (this, sect_off, per_cu, per_objfile);
a6b786da
KB
2812 }
2813
216f72a1 2814 /* DW_OP_entry_value accesses require a caller, therefore a
192ca6d8
TT
2815 frame. */
2816
2817 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind,
2818 union call_site_parameter_u kind_u,
632e107b 2819 int deref_size) override
192ca6d8
TT
2820 {
2821 needs = SYMBOL_NEEDS_FRAME;
3019eac3 2822
192ca6d8
TT
2823 /* The expression may require some stub values on DWARF stack. */
2824 push_address (0, 0);
2825 }
3019eac3 2826
336d760d 2827 /* DW_OP_addrx and DW_OP_GNU_addr_index doesn't require a frame. */
08412b07 2828
632e107b 2829 CORE_ADDR get_addr_index (unsigned int index) override
192ca6d8
TT
2830 {
2831 /* Nothing to do. */
2832 return 1;
2833 }
08412b07 2834
192ca6d8 2835 /* DW_OP_push_object_address has a frame already passed through. */
9e8b7a03 2836
632e107b 2837 CORE_ADDR get_object_address () override
192ca6d8
TT
2838 {
2839 /* Nothing to do. */
2840 return 1;
2841 }
9e8b7a03
JK
2842};
2843
0b31a4bc
TT
2844/* Compute the correct symbol_needs_kind value for the location
2845 expression at DATA (length SIZE). */
4c2df51b 2846
0b31a4bc
TT
2847static enum symbol_needs_kind
2848dwarf2_loc_desc_get_symbol_read_needs (const gdb_byte *data, size_t size,
9f47c707
SM
2849 dwarf2_per_cu_data *per_cu,
2850 dwarf2_per_objfile *per_objfile)
4c2df51b 2851{
eb115069
TT
2852 scoped_value_mark free_values;
2853
9f47c707 2854 symbol_needs_eval_context ctx (per_objfile);
192ca6d8
TT
2855
2856 ctx.needs = SYMBOL_NEEDS_NONE;
2857 ctx.per_cu = per_cu;
9f47c707 2858 ctx.gdbarch = per_objfile->objfile->arch ();
09ba997f
TT
2859 ctx.addr_size = per_cu->addr_size ();
2860 ctx.ref_addr_size = per_cu->ref_addr_size ();
4c2df51b 2861
595d2e30 2862 ctx.eval (data, size);
4c2df51b 2863
f54be24b 2864 bool in_reg = ctx.location == DWARF_VALUE_REGISTER;
f630a401 2865
1e467161
SM
2866 /* If the location has several pieces, and any of them are in
2867 registers, then we will need a frame to fetch them from. */
2868 for (dwarf_expr_piece &p : ctx.pieces)
2869 if (p.location == DWARF_VALUE_REGISTER)
f54be24b 2870 in_reg = true;
87808bd6 2871
0b31a4bc 2872 if (in_reg)
192ca6d8 2873 ctx.needs = SYMBOL_NEEDS_FRAME;
f54be24b 2874
192ca6d8 2875 return ctx.needs;
4c2df51b
DJ
2876}
2877
3cf03773
TT
2878/* A helper function that throws an unimplemented error mentioning a
2879 given DWARF operator. */
2880
621846f4 2881static void ATTRIBUTE_NORETURN
3cf03773 2882unimplemented (unsigned int op)
0d53c4c4 2883{
f39c6ffd 2884 const char *name = get_DW_OP_name (op);
b1bfef65
TT
2885
2886 if (name)
2887 error (_("DWARF operator %s cannot be translated to an agent expression"),
2888 name);
2889 else
1ba1b353
TT
2890 error (_("Unknown DWARF operator 0x%02x cannot be translated "
2891 "to an agent expression"),
b1bfef65 2892 op);
3cf03773 2893}
08922a10 2894
0fde2c53
DE
2895/* See dwarf2loc.h.
2896
2897 This is basically a wrapper on gdbarch_dwarf2_reg_to_regnum so that we
2898 can issue a complaint, which is better than having every target's
2899 implementation of dwarf2_reg_to_regnum do it. */
08922a10 2900
d064d1be 2901int
0fde2c53 2902dwarf_reg_to_regnum (struct gdbarch *arch, int dwarf_reg)
3cf03773
TT
2903{
2904 int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
0fde2c53 2905
3cf03773 2906 if (reg == -1)
0fde2c53 2907 {
b98664d3 2908 complaint (_("bad DWARF register number %d"), dwarf_reg);
0fde2c53
DE
2909 }
2910 return reg;
2911}
2912
2913/* Subroutine of dwarf_reg_to_regnum_or_error to simplify it.
2914 Throw an error because DWARF_REG is bad. */
2915
2916static void
2917throw_bad_regnum_error (ULONGEST dwarf_reg)
2918{
2919 /* Still want to print -1 as "-1".
2920 We *could* have int and ULONGEST versions of dwarf2_reg_to_regnum_or_error
2921 but that's overkill for now. */
2922 if ((int) dwarf_reg == dwarf_reg)
2923 error (_("Unable to access DWARF register number %d"), (int) dwarf_reg);
2924 error (_("Unable to access DWARF register number %s"),
2925 pulongest (dwarf_reg));
2926}
2927
2928/* See dwarf2loc.h. */
2929
2930int
2931dwarf_reg_to_regnum_or_error (struct gdbarch *arch, ULONGEST dwarf_reg)
2932{
2933 int reg;
2934
2935 if (dwarf_reg > INT_MAX)
2936 throw_bad_regnum_error (dwarf_reg);
2937 /* Yes, we will end up issuing a complaint and an error if DWARF_REG is
2938 bad, but that's ok. */
2939 reg = dwarf_reg_to_regnum (arch, (int) dwarf_reg);
2940 if (reg == -1)
2941 throw_bad_regnum_error (dwarf_reg);
3cf03773
TT
2942 return reg;
2943}
08922a10 2944
3cf03773
TT
2945/* A helper function that emits an access to memory. ARCH is the
2946 target architecture. EXPR is the expression which we are building.
2947 NBITS is the number of bits we want to read. This emits the
2948 opcodes needed to read the memory and then extract the desired
2949 bits. */
08922a10 2950
3cf03773
TT
2951static void
2952access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
08922a10 2953{
3cf03773
TT
2954 ULONGEST nbytes = (nbits + 7) / 8;
2955
9df7235c 2956 gdb_assert (nbytes > 0 && nbytes <= sizeof (LONGEST));
3cf03773 2957
92bc6a20 2958 if (expr->tracing)
3cf03773
TT
2959 ax_trace_quick (expr, nbytes);
2960
2961 if (nbits <= 8)
2962 ax_simple (expr, aop_ref8);
2963 else if (nbits <= 16)
2964 ax_simple (expr, aop_ref16);
2965 else if (nbits <= 32)
2966 ax_simple (expr, aop_ref32);
2967 else
2968 ax_simple (expr, aop_ref64);
2969
2970 /* If we read exactly the number of bytes we wanted, we're done. */
2971 if (8 * nbytes == nbits)
2972 return;
2973
d5a22e77 2974 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
0d53c4c4 2975 {
3cf03773
TT
2976 /* On a bits-big-endian machine, we want the high-order
2977 NBITS. */
2978 ax_const_l (expr, 8 * nbytes - nbits);
2979 ax_simple (expr, aop_rsh_unsigned);
0d53c4c4 2980 }
3cf03773 2981 else
0d53c4c4 2982 {
3cf03773
TT
2983 /* On a bits-little-endian box, we want the low-order NBITS. */
2984 ax_zero_ext (expr, nbits);
0d53c4c4 2985 }
3cf03773 2986}
0936ad1d 2987
3cf03773
TT
2988/* Compile a DWARF location expression to an agent expression.
2989
2990 EXPR is the agent expression we are building.
2991 LOC is the agent value we modify.
2992 ARCH is the architecture.
2993 ADDR_SIZE is the size of addresses, in bytes.
2994 OP_PTR is the start of the location expression.
2995 OP_END is one past the last byte of the location expression.
2996
2997 This will throw an exception for various kinds of errors -- for
2998 example, if the expression cannot be compiled, or if the expression
2999 is invalid. */
0936ad1d 3000
5707a07a 3001static void
9f6f94ff 3002dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
40f4af28
SM
3003 unsigned int addr_size, const gdb_byte *op_ptr,
3004 const gdb_byte *op_end,
4b167ea1
SM
3005 dwarf2_per_cu_data *per_cu,
3006 dwarf2_per_objfile *per_objfile)
3cf03773 3007{
40f4af28 3008 gdbarch *arch = expr->gdbarch;
58414334 3009 std::vector<int> dw_labels, patches;
3cf03773
TT
3010 const gdb_byte * const base = op_ptr;
3011 const gdb_byte *previous_piece = op_ptr;
3012 enum bfd_endian byte_order = gdbarch_byte_order (arch);
3013 ULONGEST bits_collected = 0;
3014 unsigned int addr_size_bits = 8 * addr_size;
d5a22e77 3015 bool bits_big_endian = byte_order == BFD_ENDIAN_BIG;
0936ad1d 3016
58414334 3017 std::vector<int> offsets (op_end - op_ptr, -1);
0936ad1d 3018
3cf03773
TT
3019 /* By default we are making an address. */
3020 loc->kind = axs_lvalue_memory;
0d45f56e 3021
3cf03773
TT
3022 while (op_ptr < op_end)
3023 {
aead7601 3024 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
9fccedf7
DE
3025 uint64_t uoffset, reg;
3026 int64_t offset;
3cf03773
TT
3027 int i;
3028
3029 offsets[op_ptr - base] = expr->len;
3030 ++op_ptr;
3031
3032 /* Our basic approach to code generation is to map DWARF
3033 operations directly to AX operations. However, there are
3034 some differences.
3035
3036 First, DWARF works on address-sized units, but AX always uses
3037 LONGEST. For most operations we simply ignore this
3038 difference; instead we generate sign extensions as needed
3039 before division and comparison operations. It would be nice
3040 to omit the sign extensions, but there is no way to determine
3041 the size of the target's LONGEST. (This code uses the size
3042 of the host LONGEST in some cases -- that is a bug but it is
3043 difficult to fix.)
3044
3045 Second, some DWARF operations cannot be translated to AX.
3046 For these we simply fail. See
3047 http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */
3048 switch (op)
0936ad1d 3049 {
3cf03773
TT
3050 case DW_OP_lit0:
3051 case DW_OP_lit1:
3052 case DW_OP_lit2:
3053 case DW_OP_lit3:
3054 case DW_OP_lit4:
3055 case DW_OP_lit5:
3056 case DW_OP_lit6:
3057 case DW_OP_lit7:
3058 case DW_OP_lit8:
3059 case DW_OP_lit9:
3060 case DW_OP_lit10:
3061 case DW_OP_lit11:
3062 case DW_OP_lit12:
3063 case DW_OP_lit13:
3064 case DW_OP_lit14:
3065 case DW_OP_lit15:
3066 case DW_OP_lit16:
3067 case DW_OP_lit17:
3068 case DW_OP_lit18:
3069 case DW_OP_lit19:
3070 case DW_OP_lit20:
3071 case DW_OP_lit21:
3072 case DW_OP_lit22:
3073 case DW_OP_lit23:
3074 case DW_OP_lit24:
3075 case DW_OP_lit25:
3076 case DW_OP_lit26:
3077 case DW_OP_lit27:
3078 case DW_OP_lit28:
3079 case DW_OP_lit29:
3080 case DW_OP_lit30:
3081 case DW_OP_lit31:
3082 ax_const_l (expr, op - DW_OP_lit0);
3083 break;
0d53c4c4 3084
3cf03773 3085 case DW_OP_addr:
ac56253d 3086 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
3cf03773 3087 op_ptr += addr_size;
ac56253d
TT
3088 /* Some versions of GCC emit DW_OP_addr before
3089 DW_OP_GNU_push_tls_address. In this case the value is an
3090 index, not an address. We don't support things like
3091 branching between the address and the TLS op. */
3092 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
4b167ea1 3093 uoffset += per_objfile->objfile->text_section_offset ();
ac56253d 3094 ax_const_l (expr, uoffset);
3cf03773 3095 break;
4c2df51b 3096
3cf03773
TT
3097 case DW_OP_const1u:
3098 ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
3099 op_ptr += 1;
3100 break;
fc3ecb3e 3101
3cf03773
TT
3102 case DW_OP_const1s:
3103 ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
3104 op_ptr += 1;
3105 break;
fc3ecb3e 3106
3cf03773
TT
3107 case DW_OP_const2u:
3108 ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
3109 op_ptr += 2;
3110 break;
fc3ecb3e 3111
3cf03773
TT
3112 case DW_OP_const2s:
3113 ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
3114 op_ptr += 2;
3115 break;
fc3ecb3e 3116
3cf03773
TT
3117 case DW_OP_const4u:
3118 ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
3119 op_ptr += 4;
3120 break;
fc3ecb3e 3121
3cf03773
TT
3122 case DW_OP_const4s:
3123 ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
3124 op_ptr += 4;
3125 break;
fc3ecb3e 3126
3cf03773
TT
3127 case DW_OP_const8u:
3128 ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
3129 op_ptr += 8;
3130 break;
fc3ecb3e 3131
3cf03773
TT
3132 case DW_OP_const8s:
3133 ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
3134 op_ptr += 8;
3135 break;
fc3ecb3e 3136
3cf03773 3137 case DW_OP_constu:
f664829e 3138 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
3cf03773
TT
3139 ax_const_l (expr, uoffset);
3140 break;
fc3ecb3e 3141
3cf03773 3142 case DW_OP_consts:
f664829e 3143 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
3cf03773
TT
3144 ax_const_l (expr, offset);
3145 break;
9c238357 3146
3cf03773
TT
3147 case DW_OP_reg0:
3148 case DW_OP_reg1:
3149 case DW_OP_reg2:
3150 case DW_OP_reg3:
3151 case DW_OP_reg4:
3152 case DW_OP_reg5:
3153 case DW_OP_reg6:
3154 case DW_OP_reg7:
3155 case DW_OP_reg8:
3156 case DW_OP_reg9:
3157 case DW_OP_reg10:
3158 case DW_OP_reg11:
3159 case DW_OP_reg12:
3160 case DW_OP_reg13:
3161 case DW_OP_reg14:
3162 case DW_OP_reg15:
3163 case DW_OP_reg16:
3164 case DW_OP_reg17:
3165 case DW_OP_reg18:
3166 case DW_OP_reg19:
3167 case DW_OP_reg20:
3168 case DW_OP_reg21:
3169 case DW_OP_reg22:
3170 case DW_OP_reg23:
3171 case DW_OP_reg24:
3172 case DW_OP_reg25:
3173 case DW_OP_reg26:
3174 case DW_OP_reg27:
3175 case DW_OP_reg28:
3176 case DW_OP_reg29:
3177 case DW_OP_reg30:
3178 case DW_OP_reg31:
3179 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
0fde2c53 3180 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_reg0);
3cf03773
TT
3181 loc->kind = axs_lvalue_register;
3182 break;
9c238357 3183
3cf03773 3184 case DW_OP_regx:
f664829e 3185 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3cf03773 3186 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
0fde2c53 3187 loc->u.reg = dwarf_reg_to_regnum_or_error (arch, reg);
3cf03773
TT
3188 loc->kind = axs_lvalue_register;
3189 break;
08922a10 3190
3cf03773
TT
3191 case DW_OP_implicit_value:
3192 {
9fccedf7 3193 uint64_t len;
3cf03773 3194
f664829e 3195 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
3cf03773
TT
3196 if (op_ptr + len > op_end)
3197 error (_("DW_OP_implicit_value: too few bytes available."));
3198 if (len > sizeof (ULONGEST))
3199 error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
3200 (int) len);
3201
3202 ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
3203 byte_order));
3204 op_ptr += len;
3205 dwarf_expr_require_composition (op_ptr, op_end,
3206 "DW_OP_implicit_value");
3207
3208 loc->kind = axs_rvalue;
3209 }
3210 break;
08922a10 3211
3cf03773
TT
3212 case DW_OP_stack_value:
3213 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
3214 loc->kind = axs_rvalue;
3215 break;
08922a10 3216
3cf03773
TT
3217 case DW_OP_breg0:
3218 case DW_OP_breg1:
3219 case DW_OP_breg2:
3220 case DW_OP_breg3:
3221 case DW_OP_breg4:
3222 case DW_OP_breg5:
3223 case DW_OP_breg6:
3224 case DW_OP_breg7:
3225 case DW_OP_breg8:
3226 case DW_OP_breg9:
3227 case DW_OP_breg10:
3228 case DW_OP_breg11:
3229 case DW_OP_breg12:
3230 case DW_OP_breg13:
3231 case DW_OP_breg14:
3232 case DW_OP_breg15:
3233 case DW_OP_breg16:
3234 case DW_OP_breg17:
3235 case DW_OP_breg18:
3236 case DW_OP_breg19:
3237 case DW_OP_breg20:
3238 case DW_OP_breg21:
3239 case DW_OP_breg22:
3240 case DW_OP_breg23:
3241 case DW_OP_breg24:
3242 case DW_OP_breg25:
3243 case DW_OP_breg26:
3244 case DW_OP_breg27:
3245 case DW_OP_breg28:
3246 case DW_OP_breg29:
3247 case DW_OP_breg30:
3248 case DW_OP_breg31:
f664829e 3249 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
0fde2c53 3250 i = dwarf_reg_to_regnum_or_error (arch, op - DW_OP_breg0);
3cf03773
TT
3251 ax_reg (expr, i);
3252 if (offset != 0)
3253 {
3254 ax_const_l (expr, offset);
3255 ax_simple (expr, aop_add);
3256 }
3257 break;
fc3ecb3e 3258
3cf03773
TT
3259 case DW_OP_bregx:
3260 {
f664829e
DE
3261 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3262 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
0fde2c53 3263 i = dwarf_reg_to_regnum_or_error (arch, reg);
3cf03773
TT
3264 ax_reg (expr, i);
3265 if (offset != 0)
3266 {
3267 ax_const_l (expr, offset);
3268 ax_simple (expr, aop_add);
3269 }
3270 }
3271 break;
fc3ecb3e 3272
3cf03773
TT
3273 case DW_OP_fbreg:
3274 {
3275 const gdb_byte *datastart;
3276 size_t datalen;
3977b71f 3277 const struct block *b;
3cf03773 3278 struct symbol *framefunc;
08922a10 3279
3cf03773
TT
3280 b = block_for_pc (expr->scope);
3281
3282 if (!b)
3283 error (_("No block found for address"));
3284
3285 framefunc = block_linkage_function (b);
3286
3287 if (!framefunc)
3288 error (_("No function found for block"));
3289
af945b75
TT
3290 func_get_frame_base_dwarf_block (framefunc, expr->scope,
3291 &datastart, &datalen);
3cf03773 3292
f664829e 3293 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
40f4af28 3294 dwarf2_compile_expr_to_ax (expr, loc, addr_size, datastart,
4b167ea1
SM
3295 datastart + datalen, per_cu,
3296 per_objfile);
d84cf7eb
TT
3297 if (loc->kind == axs_lvalue_register)
3298 require_rvalue (expr, loc);
3cf03773
TT
3299
3300 if (offset != 0)
3301 {
3302 ax_const_l (expr, offset);
3303 ax_simple (expr, aop_add);
3304 }
3305
3306 loc->kind = axs_lvalue_memory;
3307 }
08922a10 3308 break;
08922a10 3309
3cf03773
TT
3310 case DW_OP_dup:
3311 ax_simple (expr, aop_dup);
3312 break;
08922a10 3313
3cf03773
TT
3314 case DW_OP_drop:
3315 ax_simple (expr, aop_pop);
3316 break;
08922a10 3317
3cf03773
TT
3318 case DW_OP_pick:
3319 offset = *op_ptr++;
c7f96d2b 3320 ax_pick (expr, offset);
3cf03773 3321 break;
fc3ecb3e 3322
3cf03773
TT
3323 case DW_OP_swap:
3324 ax_simple (expr, aop_swap);
3325 break;
08922a10 3326
3cf03773 3327 case DW_OP_over:
c7f96d2b 3328 ax_pick (expr, 1);
3cf03773 3329 break;
08922a10 3330
3cf03773 3331 case DW_OP_rot:
c7f96d2b 3332 ax_simple (expr, aop_rot);
3cf03773 3333 break;
08922a10 3334
3cf03773
TT
3335 case DW_OP_deref:
3336 case DW_OP_deref_size:
3337 {
3338 int size;
08922a10 3339
3cf03773
TT
3340 if (op == DW_OP_deref_size)
3341 size = *op_ptr++;
3342 else
3343 size = addr_size;
3344
9df7235c 3345 if (size != 1 && size != 2 && size != 4 && size != 8)
f3cec7e6
HZ
3346 error (_("Unsupported size %d in %s"),
3347 size, get_DW_OP_name (op));
9df7235c 3348 access_memory (arch, expr, size * TARGET_CHAR_BIT);
3cf03773
TT
3349 }
3350 break;
3351
3352 case DW_OP_abs:
3353 /* Sign extend the operand. */
3354 ax_ext (expr, addr_size_bits);
3355 ax_simple (expr, aop_dup);
3356 ax_const_l (expr, 0);
3357 ax_simple (expr, aop_less_signed);
3358 ax_simple (expr, aop_log_not);
3359 i = ax_goto (expr, aop_if_goto);
3360 /* We have to emit 0 - X. */
3361 ax_const_l (expr, 0);
3362 ax_simple (expr, aop_swap);
3363 ax_simple (expr, aop_sub);
3364 ax_label (expr, i, expr->len);
3365 break;
3366
3367 case DW_OP_neg:
3368 /* No need to sign extend here. */
3369 ax_const_l (expr, 0);
3370 ax_simple (expr, aop_swap);
3371 ax_simple (expr, aop_sub);
3372 break;
3373
3374 case DW_OP_not:
3375 /* Sign extend the operand. */
3376 ax_ext (expr, addr_size_bits);
3377 ax_simple (expr, aop_bit_not);
3378 break;
3379
3380 case DW_OP_plus_uconst:
f664829e 3381 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3cf03773
TT
3382 /* It would be really weird to emit `DW_OP_plus_uconst 0',
3383 but we micro-optimize anyhow. */
3384 if (reg != 0)
3385 {
3386 ax_const_l (expr, reg);
3387 ax_simple (expr, aop_add);
3388 }
3389 break;
3390
3391 case DW_OP_and:
3392 ax_simple (expr, aop_bit_and);
3393 break;
3394
3395 case DW_OP_div:
3396 /* Sign extend the operands. */
3397 ax_ext (expr, addr_size_bits);
3398 ax_simple (expr, aop_swap);
3399 ax_ext (expr, addr_size_bits);
3400 ax_simple (expr, aop_swap);
3401 ax_simple (expr, aop_div_signed);
08922a10
SS
3402 break;
3403
3cf03773
TT
3404 case DW_OP_minus:
3405 ax_simple (expr, aop_sub);
3406 break;
3407
3408 case DW_OP_mod:
3409 ax_simple (expr, aop_rem_unsigned);
3410 break;
3411
3412 case DW_OP_mul:
3413 ax_simple (expr, aop_mul);
3414 break;
3415
3416 case DW_OP_or:
3417 ax_simple (expr, aop_bit_or);
3418 break;
3419
3420 case DW_OP_plus:
3421 ax_simple (expr, aop_add);
3422 break;
3423
3424 case DW_OP_shl:
3425 ax_simple (expr, aop_lsh);
3426 break;
3427
3428 case DW_OP_shr:
3429 ax_simple (expr, aop_rsh_unsigned);
3430 break;
3431
3432 case DW_OP_shra:
3433 ax_simple (expr, aop_rsh_signed);
3434 break;
3435
3436 case DW_OP_xor:
3437 ax_simple (expr, aop_bit_xor);
3438 break;
3439
3440 case DW_OP_le:
3441 /* Sign extend the operands. */
3442 ax_ext (expr, addr_size_bits);
3443 ax_simple (expr, aop_swap);
3444 ax_ext (expr, addr_size_bits);
3445 /* Note no swap here: A <= B is !(B < A). */
3446 ax_simple (expr, aop_less_signed);
3447 ax_simple (expr, aop_log_not);
3448 break;
3449
3450 case DW_OP_ge:
3451 /* Sign extend the operands. */
3452 ax_ext (expr, addr_size_bits);
3453 ax_simple (expr, aop_swap);
3454 ax_ext (expr, addr_size_bits);
3455 ax_simple (expr, aop_swap);
3456 /* A >= B is !(A < B). */
3457 ax_simple (expr, aop_less_signed);
3458 ax_simple (expr, aop_log_not);
3459 break;
3460
3461 case DW_OP_eq:
3462 /* Sign extend the operands. */
3463 ax_ext (expr, addr_size_bits);
3464 ax_simple (expr, aop_swap);
3465 ax_ext (expr, addr_size_bits);
3466 /* No need for a second swap here. */
3467 ax_simple (expr, aop_equal);
3468 break;
3469
3470 case DW_OP_lt:
3471 /* Sign extend the operands. */
3472 ax_ext (expr, addr_size_bits);
3473 ax_simple (expr, aop_swap);
3474 ax_ext (expr, addr_size_bits);
3475 ax_simple (expr, aop_swap);
3476 ax_simple (expr, aop_less_signed);
3477 break;
3478
3479 case DW_OP_gt:
3480 /* Sign extend the operands. */
3481 ax_ext (expr, addr_size_bits);
3482 ax_simple (expr, aop_swap);
3483 ax_ext (expr, addr_size_bits);
3484 /* Note no swap here: A > B is B < A. */
3485 ax_simple (expr, aop_less_signed);
3486 break;
3487
3488 case DW_OP_ne:
3489 /* Sign extend the operands. */
3490 ax_ext (expr, addr_size_bits);
3491 ax_simple (expr, aop_swap);
3492 ax_ext (expr, addr_size_bits);
3493 /* No need for a swap here. */
3494 ax_simple (expr, aop_equal);
3495 ax_simple (expr, aop_log_not);
3496 break;
3497
3498 case DW_OP_call_frame_cfa:
a8fd5589
TT
3499 {
3500 int regnum;
3501 CORE_ADDR text_offset;
3502 LONGEST off;
3503 const gdb_byte *cfa_start, *cfa_end;
3504
3505 if (dwarf2_fetch_cfa_info (arch, expr->scope, per_cu,
3506 &regnum, &off,
3507 &text_offset, &cfa_start, &cfa_end))
3508 {
3509 /* Register. */
3510 ax_reg (expr, regnum);
3511 if (off != 0)
3512 {
3513 ax_const_l (expr, off);
3514 ax_simple (expr, aop_add);
3515 }
3516 }
3517 else
3518 {
3519 /* Another expression. */
3520 ax_const_l (expr, text_offset);
40f4af28 3521 dwarf2_compile_expr_to_ax (expr, loc, addr_size, cfa_start,
4b167ea1 3522 cfa_end, per_cu, per_objfile);
a8fd5589
TT
3523 }
3524
3525 loc->kind = axs_lvalue_memory;
3526 }
3cf03773
TT
3527 break;
3528
3529 case DW_OP_GNU_push_tls_address:
4aa4e28b 3530 case DW_OP_form_tls_address:
3cf03773
TT
3531 unimplemented (op);
3532 break;
3533
08412b07
JB
3534 case DW_OP_push_object_address:
3535 unimplemented (op);
3536 break;
3537
3cf03773
TT
3538 case DW_OP_skip:
3539 offset = extract_signed_integer (op_ptr, 2, byte_order);
3540 op_ptr += 2;
3541 i = ax_goto (expr, aop_goto);
58414334
TT
3542 dw_labels.push_back (op_ptr + offset - base);
3543 patches.push_back (i);
3cf03773
TT
3544 break;
3545
3546 case DW_OP_bra:
3547 offset = extract_signed_integer (op_ptr, 2, byte_order);
3548 op_ptr += 2;
3549 /* Zero extend the operand. */
3550 ax_zero_ext (expr, addr_size_bits);
3551 i = ax_goto (expr, aop_if_goto);
58414334
TT
3552 dw_labels.push_back (op_ptr + offset - base);
3553 patches.push_back (i);
3cf03773
TT
3554 break;
3555
3556 case DW_OP_nop:
3557 break;
3558
3559 case DW_OP_piece:
3560 case DW_OP_bit_piece:
08922a10 3561 {
b926417a 3562 uint64_t size;
3cf03773
TT
3563
3564 if (op_ptr - 1 == previous_piece)
3565 error (_("Cannot translate empty pieces to agent expressions"));
3566 previous_piece = op_ptr - 1;
3567
f664829e 3568 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
3cf03773
TT
3569 if (op == DW_OP_piece)
3570 {
3571 size *= 8;
b926417a 3572 uoffset = 0;
3cf03773
TT
3573 }
3574 else
b926417a 3575 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
08922a10 3576
3cf03773
TT
3577 if (bits_collected + size > 8 * sizeof (LONGEST))
3578 error (_("Expression pieces exceed word size"));
3579
3580 /* Access the bits. */
3581 switch (loc->kind)
3582 {
3583 case axs_lvalue_register:
3584 ax_reg (expr, loc->u.reg);
3585 break;
3586
3587 case axs_lvalue_memory:
3588 /* Offset the pointer, if needed. */
b926417a 3589 if (uoffset > 8)
3cf03773 3590 {
b926417a 3591 ax_const_l (expr, uoffset / 8);
3cf03773 3592 ax_simple (expr, aop_add);
b926417a 3593 uoffset %= 8;
3cf03773
TT
3594 }
3595 access_memory (arch, expr, size);
3596 break;
3597 }
3598
3599 /* For a bits-big-endian target, shift up what we already
3600 have. For a bits-little-endian target, shift up the
3601 new data. Note that there is a potential bug here if
3602 the DWARF expression leaves multiple values on the
3603 stack. */
3604 if (bits_collected > 0)
3605 {
3606 if (bits_big_endian)
3607 {
3608 ax_simple (expr, aop_swap);
3609 ax_const_l (expr, size);
3610 ax_simple (expr, aop_lsh);
3611 /* We don't need a second swap here, because
3612 aop_bit_or is symmetric. */
3613 }
3614 else
3615 {
3616 ax_const_l (expr, size);
3617 ax_simple (expr, aop_lsh);
3618 }
3619 ax_simple (expr, aop_bit_or);
3620 }
3621
3622 bits_collected += size;
3623 loc->kind = axs_rvalue;
08922a10
SS
3624 }
3625 break;
08922a10 3626
3cf03773
TT
3627 case DW_OP_GNU_uninit:
3628 unimplemented (op);
3629
3630 case DW_OP_call2:
3631 case DW_OP_call4:
3632 {
3633 struct dwarf2_locexpr_baton block;
3634 int size = (op == DW_OP_call2 ? 2 : 4);
3635
3636 uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
3637 op_ptr += size;
3638
041d9819
SM
3639 auto get_frame_pc_from_expr = [expr] ()
3640 {
3641 return expr->scope;
3642 };
b926417a 3643 cu_offset cuoffset = (cu_offset) uoffset;
14095eb3 3644 block = dwarf2_fetch_die_loc_cu_off (cuoffset, per_cu, per_objfile,
041d9819 3645 get_frame_pc_from_expr);
3cf03773
TT
3646
3647 /* DW_OP_call_ref is currently not supported. */
3648 gdb_assert (block.per_cu == per_cu);
3649
40f4af28 3650 dwarf2_compile_expr_to_ax (expr, loc, addr_size, block.data,
4b167ea1
SM
3651 block.data + block.size, per_cu,
3652 per_objfile);
3cf03773
TT
3653 }
3654 break;
3655
3656 case DW_OP_call_ref:
3657 unimplemented (op);
3658
a6b786da
KB
3659 case DW_OP_GNU_variable_value:
3660 unimplemented (op);
3661
3cf03773 3662 default:
b1bfef65 3663 unimplemented (op);
08922a10 3664 }
08922a10 3665 }
3cf03773
TT
3666
3667 /* Patch all the branches we emitted. */
b926417a 3668 for (int i = 0; i < patches.size (); ++i)
3cf03773 3669 {
58414334 3670 int targ = offsets[dw_labels[i]];
3cf03773
TT
3671 if (targ == -1)
3672 internal_error (__FILE__, __LINE__, _("invalid label"));
58414334 3673 ax_label (expr, patches[i], targ);
3cf03773 3674 }
08922a10
SS
3675}
3676
4c2df51b
DJ
3677\f
3678/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
3679 evaluator to calculate the location. */
3680static struct value *
3681locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
3682{
9a3c8263
SM
3683 struct dwarf2_locexpr_baton *dlbaton
3684 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
4c2df51b 3685 struct value *val;
9a619af0 3686
a2d33775 3687 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
9f47c707
SM
3688 dlbaton->size, dlbaton->per_cu,
3689 dlbaton->per_objfile);
4c2df51b
DJ
3690
3691 return val;
3692}
3693
e18b2753
JK
3694/* Return the value of SYMBOL in FRAME at (callee) FRAME's function
3695 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
3696 will be thrown. */
3697
3698static struct value *
3699locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
3700{
9a3c8263
SM
3701 struct dwarf2_locexpr_baton *dlbaton
3702 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
e18b2753
JK
3703
3704 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data,
3705 dlbaton->size);
3706}
3707
0b31a4bc
TT
3708/* Implementation of get_symbol_read_needs from
3709 symbol_computed_ops. */
3710
3711static enum symbol_needs_kind
3712locexpr_get_symbol_read_needs (struct symbol *symbol)
4c2df51b 3713{
9a3c8263
SM
3714 struct dwarf2_locexpr_baton *dlbaton
3715 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
9a619af0 3716
0b31a4bc 3717 return dwarf2_loc_desc_get_symbol_read_needs (dlbaton->data, dlbaton->size,
9f47c707
SM
3718 dlbaton->per_cu,
3719 dlbaton->per_objfile);
4c2df51b
DJ
3720}
3721
9eae7c52
TT
3722/* Return true if DATA points to the end of a piece. END is one past
3723 the last byte in the expression. */
3724
3725static int
3726piece_end_p (const gdb_byte *data, const gdb_byte *end)
3727{
3728 return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
3729}
3730
5e44ecb3
TT
3731/* Helper for locexpr_describe_location_piece that finds the name of a
3732 DWARF register. */
3733
3734static const char *
3735locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
3736{
3737 int regnum;
3738
0fde2c53
DE
3739 /* This doesn't use dwarf_reg_to_regnum_or_error on purpose.
3740 We'd rather print *something* here than throw an error. */
3741 regnum = dwarf_reg_to_regnum (gdbarch, dwarf_regnum);
3742 /* gdbarch_register_name may just return "", return something more
3743 descriptive for bad register numbers. */
3744 if (regnum == -1)
3745 {
3746 /* The text is output as "$bad_register_number".
3747 That is why we use the underscores. */
3748 return _("bad_register_number");
3749 }
5e44ecb3
TT
3750 return gdbarch_register_name (gdbarch, regnum);
3751}
3752
9eae7c52
TT
3753/* Nicely describe a single piece of a location, returning an updated
3754 position in the bytecode sequence. This function cannot recognize
3755 all locations; if a location is not recognized, it simply returns
f664829e
DE
3756 DATA. If there is an error during reading, e.g. we run off the end
3757 of the buffer, an error is thrown. */
08922a10 3758
0d45f56e 3759static const gdb_byte *
08922a10 3760locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
82ca3f51
SM
3761 CORE_ADDR addr, dwarf2_per_cu_data *per_cu,
3762 dwarf2_per_objfile *per_objfile,
9eae7c52 3763 const gdb_byte *data, const gdb_byte *end,
0d45f56e 3764 unsigned int addr_size)
4c2df51b 3765{
82ca3f51 3766 objfile *objfile = per_objfile->objfile;
08feed99 3767 struct gdbarch *gdbarch = objfile->arch ();
49f6c839 3768 size_t leb128_size;
08922a10
SS
3769
3770 if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
3771 {
08922a10 3772 fprintf_filtered (stream, _("a variable in $%s"),
5e44ecb3 3773 locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
08922a10
SS
3774 data += 1;
3775 }
3776 else if (data[0] == DW_OP_regx)
3777 {
9fccedf7 3778 uint64_t reg;
4c2df51b 3779
f664829e 3780 data = safe_read_uleb128 (data + 1, end, &reg);
08922a10 3781 fprintf_filtered (stream, _("a variable in $%s"),
5e44ecb3 3782 locexpr_regname (gdbarch, reg));
08922a10
SS
3783 }
3784 else if (data[0] == DW_OP_fbreg)
4c2df51b 3785 {
3977b71f 3786 const struct block *b;
08922a10
SS
3787 struct symbol *framefunc;
3788 int frame_reg = 0;
9fccedf7 3789 int64_t frame_offset;
7155d578 3790 const gdb_byte *base_data, *new_data, *save_data = data;
08922a10 3791 size_t base_size;
9fccedf7 3792 int64_t base_offset = 0;
08922a10 3793
f664829e 3794 new_data = safe_read_sleb128 (data + 1, end, &frame_offset);
9eae7c52
TT
3795 if (!piece_end_p (new_data, end))
3796 return data;
3797 data = new_data;
3798
08922a10
SS
3799 b = block_for_pc (addr);
3800
3801 if (!b)
3802 error (_("No block found for address for symbol \"%s\"."),
987012b8 3803 symbol->print_name ());
08922a10
SS
3804
3805 framefunc = block_linkage_function (b);
3806
3807 if (!framefunc)
3808 error (_("No function found for block for symbol \"%s\"."),
987012b8 3809 symbol->print_name ());
08922a10 3810
af945b75 3811 func_get_frame_base_dwarf_block (framefunc, addr, &base_data, &base_size);
08922a10
SS
3812
3813 if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
3814 {
0d45f56e 3815 const gdb_byte *buf_end;
08922a10
SS
3816
3817 frame_reg = base_data[0] - DW_OP_breg0;
f664829e
DE
3818 buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size,
3819 &base_offset);
08922a10 3820 if (buf_end != base_data + base_size)
3e43a32a
MS
3821 error (_("Unexpected opcode after "
3822 "DW_OP_breg%u for symbol \"%s\"."),
987012b8 3823 frame_reg, symbol->print_name ());
08922a10
SS
3824 }
3825 else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
3826 {
3827 /* The frame base is just the register, with no offset. */
3828 frame_reg = base_data[0] - DW_OP_reg0;
3829 base_offset = 0;
3830 }
3831 else
3832 {
3833 /* We don't know what to do with the frame base expression,
3834 so we can't trace this variable; give up. */
7155d578 3835 return save_data;
08922a10
SS
3836 }
3837
3e43a32a
MS
3838 fprintf_filtered (stream,
3839 _("a variable at frame base reg $%s offset %s+%s"),
5e44ecb3 3840 locexpr_regname (gdbarch, frame_reg),
08922a10
SS
3841 plongest (base_offset), plongest (frame_offset));
3842 }
9eae7c52
TT
3843 else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
3844 && piece_end_p (data, end))
08922a10 3845 {
9fccedf7 3846 int64_t offset;
08922a10 3847
f664829e 3848 data = safe_read_sleb128 (data + 1, end, &offset);
08922a10 3849
4c2df51b 3850 fprintf_filtered (stream,
08922a10
SS
3851 _("a variable at offset %s from base reg $%s"),
3852 plongest (offset),
5e44ecb3 3853 locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
4c2df51b
DJ
3854 }
3855
c3228f12
EZ
3856 /* The location expression for a TLS variable looks like this (on a
3857 64-bit LE machine):
3858
3859 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
3860 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
09d8bd00 3861
c3228f12
EZ
3862 0x3 is the encoding for DW_OP_addr, which has an operand as long
3863 as the size of an address on the target machine (here is 8
09d8bd00
TT
3864 bytes). Note that more recent version of GCC emit DW_OP_const4u
3865 or DW_OP_const8u, depending on address size, rather than
0963b4bd
MS
3866 DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
3867 The operand represents the offset at which the variable is within
3868 the thread local storage. */
c3228f12 3869
9eae7c52 3870 else if (data + 1 + addr_size < end
09d8bd00
TT
3871 && (data[0] == DW_OP_addr
3872 || (addr_size == 4 && data[0] == DW_OP_const4u)
3873 || (addr_size == 8 && data[0] == DW_OP_const8u))
4aa4e28b
TT
3874 && (data[1 + addr_size] == DW_OP_GNU_push_tls_address
3875 || data[1 + addr_size] == DW_OP_form_tls_address)
9eae7c52 3876 && piece_end_p (data + 2 + addr_size, end))
08922a10 3877 {
d4a087c7
UW
3878 ULONGEST offset;
3879 offset = extract_unsigned_integer (data + 1, addr_size,
3880 gdbarch_byte_order (gdbarch));
9a619af0 3881
08922a10 3882 fprintf_filtered (stream,
d4a087c7 3883 _("a thread-local variable at offset 0x%s "
08922a10 3884 "in the thread-local storage for `%s'"),
4262abfb 3885 phex_nz (offset, addr_size), objfile_name (objfile));
08922a10
SS
3886
3887 data += 1 + addr_size + 1;
3888 }
49f6c839
DE
3889
3890 /* With -gsplit-dwarf a TLS variable can also look like this:
3891 DW_AT_location : 3 byte block: fc 4 e0
3892 (DW_OP_GNU_const_index: 4;
3893 DW_OP_GNU_push_tls_address) */
3894 else if (data + 3 <= end
3895 && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end
3896 && data[0] == DW_OP_GNU_const_index
3897 && leb128_size > 0
4aa4e28b
TT
3898 && (data[1 + leb128_size] == DW_OP_GNU_push_tls_address
3899 || data[1 + leb128_size] == DW_OP_form_tls_address)
49f6c839
DE
3900 && piece_end_p (data + 2 + leb128_size, end))
3901 {
a55c1f32 3902 uint64_t offset;
49f6c839
DE
3903
3904 data = safe_read_uleb128 (data + 1, end, &offset);
82ca3f51 3905 offset = dwarf2_read_addr_index (per_cu, per_objfile, offset);
49f6c839
DE
3906 fprintf_filtered (stream,
3907 _("a thread-local variable at offset 0x%s "
3908 "in the thread-local storage for `%s'"),
4262abfb 3909 phex_nz (offset, addr_size), objfile_name (objfile));
49f6c839
DE
3910 ++data;
3911 }
3912
9eae7c52
TT
3913 else if (data[0] >= DW_OP_lit0
3914 && data[0] <= DW_OP_lit31
3915 && data + 1 < end
3916 && data[1] == DW_OP_stack_value)
3917 {
3918 fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
3919 data += 2;
3920 }
3921
3922 return data;
3923}
3924
3925/* Disassemble an expression, stopping at the end of a piece or at the
3926 end of the expression. Returns a pointer to the next unread byte
3927 in the input expression. If ALL is nonzero, then this function
f664829e
DE
3928 will keep going until it reaches the end of the expression.
3929 If there is an error during reading, e.g. we run off the end
3930 of the buffer, an error is thrown. */
9eae7c52
TT
3931
3932static const gdb_byte *
3933disassemble_dwarf_expression (struct ui_file *stream,
3934 struct gdbarch *arch, unsigned int addr_size,
2bda9cc5 3935 int offset_size, const gdb_byte *start,
9eae7c52 3936 const gdb_byte *data, const gdb_byte *end,
2bda9cc5 3937 int indent, int all,
82ca3f51
SM
3938 dwarf2_per_cu_data *per_cu,
3939 dwarf2_per_objfile *per_objfile)
9eae7c52 3940{
9eae7c52
TT
3941 while (data < end
3942 && (all
3943 || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
3944 {
aead7601 3945 enum dwarf_location_atom op = (enum dwarf_location_atom) *data++;
9fccedf7
DE
3946 uint64_t ul;
3947 int64_t l;
9eae7c52
TT
3948 const char *name;
3949
f39c6ffd 3950 name = get_DW_OP_name (op);
9eae7c52
TT
3951
3952 if (!name)
3953 error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
06826322 3954 op, (long) (data - 1 - start));
2bda9cc5
JK
3955 fprintf_filtered (stream, " %*ld: %s", indent + 4,
3956 (long) (data - 1 - start), name);
9eae7c52
TT
3957
3958 switch (op)
3959 {
3960 case DW_OP_addr:
d4a087c7
UW
3961 ul = extract_unsigned_integer (data, addr_size,
3962 gdbarch_byte_order (arch));
9eae7c52 3963 data += addr_size;
d4a087c7 3964 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
9eae7c52
TT
3965 break;
3966
3967 case DW_OP_const1u:
3968 ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
3969 data += 1;
3970 fprintf_filtered (stream, " %s", pulongest (ul));
3971 break;
fc3ecb3e 3972
9eae7c52
TT
3973 case DW_OP_const1s:
3974 l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
3975 data += 1;
3976 fprintf_filtered (stream, " %s", plongest (l));
3977 break;
fc3ecb3e 3978
9eae7c52
TT
3979 case DW_OP_const2u:
3980 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
3981 data += 2;
3982 fprintf_filtered (stream, " %s", pulongest (ul));
3983 break;
fc3ecb3e 3984
9eae7c52
TT
3985 case DW_OP_const2s:
3986 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
3987 data += 2;
3988 fprintf_filtered (stream, " %s", plongest (l));
3989 break;
fc3ecb3e 3990
9eae7c52
TT
3991 case DW_OP_const4u:
3992 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
3993 data += 4;
3994 fprintf_filtered (stream, " %s", pulongest (ul));
3995 break;
fc3ecb3e 3996
9eae7c52
TT
3997 case DW_OP_const4s:
3998 l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
3999 data += 4;
4000 fprintf_filtered (stream, " %s", plongest (l));
4001 break;
fc3ecb3e 4002
9eae7c52
TT
4003 case DW_OP_const8u:
4004 ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
4005 data += 8;
4006 fprintf_filtered (stream, " %s", pulongest (ul));
4007 break;
fc3ecb3e 4008
9eae7c52
TT
4009 case DW_OP_const8s:
4010 l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
4011 data += 8;
4012 fprintf_filtered (stream, " %s", plongest (l));
4013 break;
fc3ecb3e 4014
9eae7c52 4015 case DW_OP_constu:
f664829e 4016 data = safe_read_uleb128 (data, end, &ul);
9eae7c52
TT
4017 fprintf_filtered (stream, " %s", pulongest (ul));
4018 break;
fc3ecb3e 4019
9eae7c52 4020 case DW_OP_consts:
f664829e 4021 data = safe_read_sleb128 (data, end, &l);
9eae7c52
TT
4022 fprintf_filtered (stream, " %s", plongest (l));
4023 break;
4024
4025 case DW_OP_reg0:
4026 case DW_OP_reg1:
4027 case DW_OP_reg2:
4028 case DW_OP_reg3:
4029 case DW_OP_reg4:
4030 case DW_OP_reg5:
4031 case DW_OP_reg6:
4032 case DW_OP_reg7:
4033 case DW_OP_reg8:
4034 case DW_OP_reg9:
4035 case DW_OP_reg10:
4036 case DW_OP_reg11:
4037 case DW_OP_reg12:
4038 case DW_OP_reg13:
4039 case DW_OP_reg14:
4040 case DW_OP_reg15:
4041 case DW_OP_reg16:
4042 case DW_OP_reg17:
4043 case DW_OP_reg18:
4044 case DW_OP_reg19:
4045 case DW_OP_reg20:
4046 case DW_OP_reg21:
4047 case DW_OP_reg22:
4048 case DW_OP_reg23:
4049 case DW_OP_reg24:
4050 case DW_OP_reg25:
4051 case DW_OP_reg26:
4052 case DW_OP_reg27:
4053 case DW_OP_reg28:
4054 case DW_OP_reg29:
4055 case DW_OP_reg30:
4056 case DW_OP_reg31:
4057 fprintf_filtered (stream, " [$%s]",
5e44ecb3 4058 locexpr_regname (arch, op - DW_OP_reg0));
9eae7c52
TT
4059 break;
4060
4061 case DW_OP_regx:
f664829e 4062 data = safe_read_uleb128 (data, end, &ul);
9eae7c52 4063 fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
5e44ecb3 4064 locexpr_regname (arch, (int) ul));
9eae7c52
TT
4065 break;
4066
4067 case DW_OP_implicit_value:
f664829e 4068 data = safe_read_uleb128 (data, end, &ul);
9eae7c52
TT
4069 data += ul;
4070 fprintf_filtered (stream, " %s", pulongest (ul));
4071 break;
4072
4073 case DW_OP_breg0:
4074 case DW_OP_breg1:
4075 case DW_OP_breg2:
4076 case DW_OP_breg3:
4077 case DW_OP_breg4:
4078 case DW_OP_breg5:
4079 case DW_OP_breg6:
4080 case DW_OP_breg7:
4081 case DW_OP_breg8:
4082 case DW_OP_breg9:
4083 case DW_OP_breg10:
4084 case DW_OP_breg11:
4085 case DW_OP_breg12:
4086 case DW_OP_breg13:
4087 case DW_OP_breg14:
4088 case DW_OP_breg15:
4089 case DW_OP_breg16:
4090 case DW_OP_breg17:
4091 case DW_OP_breg18:
4092 case DW_OP_breg19:
4093 case DW_OP_breg20:
4094 case DW_OP_breg21:
4095 case DW_OP_breg22:
4096 case DW_OP_breg23:
4097 case DW_OP_breg24:
4098 case DW_OP_breg25:
4099 case DW_OP_breg26:
4100 case DW_OP_breg27:
4101 case DW_OP_breg28:
4102 case DW_OP_breg29:
4103 case DW_OP_breg30:
4104 case DW_OP_breg31:
f664829e 4105 data = safe_read_sleb128 (data, end, &l);
0502ed8c 4106 fprintf_filtered (stream, " %s [$%s]", plongest (l),
5e44ecb3 4107 locexpr_regname (arch, op - DW_OP_breg0));
9eae7c52
TT
4108 break;
4109
4110 case DW_OP_bregx:
f664829e
DE
4111 data = safe_read_uleb128 (data, end, &ul);
4112 data = safe_read_sleb128 (data, end, &l);
0502ed8c
JK
4113 fprintf_filtered (stream, " register %s [$%s] offset %s",
4114 pulongest (ul),
5e44ecb3 4115 locexpr_regname (arch, (int) ul),
0502ed8c 4116 plongest (l));
9eae7c52
TT
4117 break;
4118
4119 case DW_OP_fbreg:
f664829e 4120 data = safe_read_sleb128 (data, end, &l);
0502ed8c 4121 fprintf_filtered (stream, " %s", plongest (l));
9eae7c52
TT
4122 break;
4123
4124 case DW_OP_xderef_size:
4125 case DW_OP_deref_size:
4126 case DW_OP_pick:
4127 fprintf_filtered (stream, " %d", *data);
4128 ++data;
4129 break;
4130
4131 case DW_OP_plus_uconst:
f664829e 4132 data = safe_read_uleb128 (data, end, &ul);
9eae7c52
TT
4133 fprintf_filtered (stream, " %s", pulongest (ul));
4134 break;
4135
4136 case DW_OP_skip:
4137 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4138 data += 2;
4139 fprintf_filtered (stream, " to %ld",
4140 (long) (data + l - start));
4141 break;
4142
4143 case DW_OP_bra:
4144 l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
4145 data += 2;
4146 fprintf_filtered (stream, " %ld",
4147 (long) (data + l - start));
4148 break;
4149
4150 case DW_OP_call2:
4151 ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
4152 data += 2;
4153 fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
4154 break;
4155
4156 case DW_OP_call4:
4157 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4158 data += 4;
4159 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4160 break;
4161
4162 case DW_OP_call_ref:
4163 ul = extract_unsigned_integer (data, offset_size,
4164 gdbarch_byte_order (arch));
4165 data += offset_size;
4166 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4167 break;
4168
4169 case DW_OP_piece:
f664829e 4170 data = safe_read_uleb128 (data, end, &ul);
9eae7c52
TT
4171 fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
4172 break;
4173
4174 case DW_OP_bit_piece:
4175 {
9fccedf7 4176 uint64_t offset;
9eae7c52 4177
f664829e
DE
4178 data = safe_read_uleb128 (data, end, &ul);
4179 data = safe_read_uleb128 (data, end, &offset);
9eae7c52
TT
4180 fprintf_filtered (stream, " size %s offset %s (bits)",
4181 pulongest (ul), pulongest (offset));
4182 }
4183 break;
8cf6f0b1 4184
216f72a1 4185 case DW_OP_implicit_pointer:
8cf6f0b1
TT
4186 case DW_OP_GNU_implicit_pointer:
4187 {
4188 ul = extract_unsigned_integer (data, offset_size,
4189 gdbarch_byte_order (arch));
4190 data += offset_size;
4191
f664829e 4192 data = safe_read_sleb128 (data, end, &l);
8cf6f0b1
TT
4193
4194 fprintf_filtered (stream, " DIE %s offset %s",
4195 phex_nz (ul, offset_size),
4196 plongest (l));
4197 }
4198 break;
5e44ecb3 4199
216f72a1 4200 case DW_OP_deref_type:
5e44ecb3
TT
4201 case DW_OP_GNU_deref_type:
4202 {
b926417a 4203 int deref_addr_size = *data++;
5e44ecb3
TT
4204 struct type *type;
4205
f664829e 4206 data = safe_read_uleb128 (data, end, &ul);
9c541725 4207 cu_offset offset = (cu_offset) ul;
aa66c379 4208 type = dwarf2_get_die_type (offset, per_cu, per_objfile);
5e44ecb3
TT
4209 fprintf_filtered (stream, "<");
4210 type_print (type, "", stream, -1);
9c541725
PA
4211 fprintf_filtered (stream, " [0x%s]> %d",
4212 phex_nz (to_underlying (offset), 0),
b926417a 4213 deref_addr_size);
5e44ecb3
TT
4214 }
4215 break;
4216
216f72a1 4217 case DW_OP_const_type:
5e44ecb3
TT
4218 case DW_OP_GNU_const_type:
4219 {
5e44ecb3
TT
4220 struct type *type;
4221
f664829e 4222 data = safe_read_uleb128 (data, end, &ul);
9c541725 4223 cu_offset type_die = (cu_offset) ul;
aa66c379 4224 type = dwarf2_get_die_type (type_die, per_cu, per_objfile);
5e44ecb3
TT
4225 fprintf_filtered (stream, "<");
4226 type_print (type, "", stream, -1);
9c541725
PA
4227 fprintf_filtered (stream, " [0x%s]>",
4228 phex_nz (to_underlying (type_die), 0));
d9e49b61
TT
4229
4230 int n = *data++;
4231 fprintf_filtered (stream, " %d byte block:", n);
4232 for (int i = 0; i < n; ++i)
4233 fprintf_filtered (stream, " %02x", data[i]);
4234 data += n;
5e44ecb3
TT
4235 }
4236 break;
4237
216f72a1 4238 case DW_OP_regval_type:
5e44ecb3
TT
4239 case DW_OP_GNU_regval_type:
4240 {
9fccedf7 4241 uint64_t reg;
5e44ecb3
TT
4242 struct type *type;
4243
f664829e
DE
4244 data = safe_read_uleb128 (data, end, &reg);
4245 data = safe_read_uleb128 (data, end, &ul);
9c541725 4246 cu_offset type_die = (cu_offset) ul;
5e44ecb3 4247
aa66c379 4248 type = dwarf2_get_die_type (type_die, per_cu, per_objfile);
5e44ecb3
TT
4249 fprintf_filtered (stream, "<");
4250 type_print (type, "", stream, -1);
b64f50a1 4251 fprintf_filtered (stream, " [0x%s]> [$%s]",
9c541725 4252 phex_nz (to_underlying (type_die), 0),
5e44ecb3
TT
4253 locexpr_regname (arch, reg));
4254 }
4255 break;
4256
216f72a1 4257 case DW_OP_convert:
5e44ecb3 4258 case DW_OP_GNU_convert:
216f72a1 4259 case DW_OP_reinterpret:
5e44ecb3
TT
4260 case DW_OP_GNU_reinterpret:
4261 {
f664829e 4262 data = safe_read_uleb128 (data, end, &ul);
9c541725 4263 cu_offset type_die = (cu_offset) ul;
5e44ecb3 4264
9c541725 4265 if (to_underlying (type_die) == 0)
5e44ecb3
TT
4266 fprintf_filtered (stream, "<0>");
4267 else
4268 {
4269 struct type *type;
4270
aa66c379 4271 type = dwarf2_get_die_type (type_die, per_cu, per_objfile);
5e44ecb3
TT
4272 fprintf_filtered (stream, "<");
4273 type_print (type, "", stream, -1);
9c541725
PA
4274 fprintf_filtered (stream, " [0x%s]>",
4275 phex_nz (to_underlying (type_die), 0));
5e44ecb3
TT
4276 }
4277 }
4278 break;
2bda9cc5 4279
216f72a1 4280 case DW_OP_entry_value:
2bda9cc5 4281 case DW_OP_GNU_entry_value:
f664829e 4282 data = safe_read_uleb128 (data, end, &ul);
2bda9cc5
JK
4283 fputc_filtered ('\n', stream);
4284 disassemble_dwarf_expression (stream, arch, addr_size, offset_size,
4285 start, data, data + ul, indent + 2,
82ca3f51 4286 all, per_cu, per_objfile);
2bda9cc5
JK
4287 data += ul;
4288 continue;
49f6c839 4289
a24f71ab
JK
4290 case DW_OP_GNU_parameter_ref:
4291 ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
4292 data += 4;
4293 fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
4294 break;
4295
336d760d 4296 case DW_OP_addrx:
49f6c839
DE
4297 case DW_OP_GNU_addr_index:
4298 data = safe_read_uleb128 (data, end, &ul);
82ca3f51 4299 ul = dwarf2_read_addr_index (per_cu, per_objfile, ul);
49f6c839
DE
4300 fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
4301 break;
fc3ecb3e 4302
49f6c839
DE
4303 case DW_OP_GNU_const_index:
4304 data = safe_read_uleb128 (data, end, &ul);
82ca3f51 4305 ul = dwarf2_read_addr_index (per_cu, per_objfile, ul);
49f6c839
DE
4306 fprintf_filtered (stream, " %s", pulongest (ul));
4307 break;
a6b786da
KB
4308
4309 case DW_OP_GNU_variable_value:
4310 ul = extract_unsigned_integer (data, offset_size,
4311 gdbarch_byte_order (arch));
4312 data += offset_size;
4313 fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
4314 break;
9eae7c52
TT
4315 }
4316
4317 fprintf_filtered (stream, "\n");
4318 }
c3228f12 4319
08922a10 4320 return data;
4c2df51b
DJ
4321}
4322
009b64fc
TT
4323static bool dwarf_always_disassemble;
4324
4325static void
4326show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
4327 struct cmd_list_element *c, const char *value)
4328{
4329 fprintf_filtered (file,
4330 _("Whether to always disassemble "
4331 "DWARF expressions is %s.\n"),
4332 value);
4333}
4334
08922a10
SS
4335/* Describe a single location, which may in turn consist of multiple
4336 pieces. */
a55cc764 4337
08922a10
SS
4338static void
4339locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
0d45f56e 4340 struct ui_file *stream,
56eb65bd 4341 const gdb_byte *data, size_t size,
82ca3f51
SM
4342 unsigned int addr_size,
4343 int offset_size, dwarf2_per_cu_data *per_cu,
4344 dwarf2_per_objfile *per_objfile)
08922a10 4345{
0d45f56e 4346 const gdb_byte *end = data + size;
9eae7c52 4347 int first_piece = 1, bad = 0;
82ca3f51 4348 objfile *objfile = per_objfile->objfile;
08922a10 4349
08922a10
SS
4350 while (data < end)
4351 {
9eae7c52
TT
4352 const gdb_byte *here = data;
4353 int disassemble = 1;
4354
4355 if (first_piece)
4356 first_piece = 0;
4357 else
4358 fprintf_filtered (stream, _(", and "));
08922a10 4359
b4f54984 4360 if (!dwarf_always_disassemble)
9eae7c52 4361 {
3e43a32a 4362 data = locexpr_describe_location_piece (symbol, stream,
82ca3f51 4363 addr, per_cu, per_objfile,
9eae7c52
TT
4364 data, end, addr_size);
4365 /* If we printed anything, or if we have an empty piece,
4366 then don't disassemble. */
4367 if (data != here
4368 || data[0] == DW_OP_piece
4369 || data[0] == DW_OP_bit_piece)
4370 disassemble = 0;
08922a10 4371 }
9eae7c52 4372 if (disassemble)
2bda9cc5
JK
4373 {
4374 fprintf_filtered (stream, _("a complex DWARF expression:\n"));
4375 data = disassemble_dwarf_expression (stream,
08feed99 4376 objfile->arch (),
2bda9cc5
JK
4377 addr_size, offset_size, data,
4378 data, end, 0,
b4f54984 4379 dwarf_always_disassemble,
82ca3f51 4380 per_cu, per_objfile);
2bda9cc5 4381 }
9eae7c52
TT
4382
4383 if (data < end)
08922a10 4384 {
9eae7c52 4385 int empty = data == here;
08922a10 4386
9eae7c52
TT
4387 if (disassemble)
4388 fprintf_filtered (stream, " ");
4389 if (data[0] == DW_OP_piece)
4390 {
9fccedf7 4391 uint64_t bytes;
08922a10 4392
f664829e 4393 data = safe_read_uleb128 (data + 1, end, &bytes);
08922a10 4394
9eae7c52
TT
4395 if (empty)
4396 fprintf_filtered (stream, _("an empty %s-byte piece"),
4397 pulongest (bytes));
4398 else
4399 fprintf_filtered (stream, _(" [%s-byte piece]"),
4400 pulongest (bytes));
4401 }
4402 else if (data[0] == DW_OP_bit_piece)
4403 {
9fccedf7 4404 uint64_t bits, offset;
9eae7c52 4405
f664829e
DE
4406 data = safe_read_uleb128 (data + 1, end, &bits);
4407 data = safe_read_uleb128 (data, end, &offset);
9eae7c52
TT
4408
4409 if (empty)
4410 fprintf_filtered (stream,
4411 _("an empty %s-bit piece"),
4412 pulongest (bits));
4413 else
4414 fprintf_filtered (stream,
4415 _(" [%s-bit piece, offset %s bits]"),
4416 pulongest (bits), pulongest (offset));
4417 }
4418 else
4419 {
4420 bad = 1;
4421 break;
4422 }
08922a10
SS
4423 }
4424 }
4425
4426 if (bad || data > end)
4427 error (_("Corrupted DWARF2 expression for \"%s\"."),
987012b8 4428 symbol->print_name ());
08922a10
SS
4429}
4430
4431/* Print a natural-language description of SYMBOL to STREAM. This
4432 version is for a symbol with a single location. */
a55cc764 4433
08922a10
SS
4434static void
4435locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
4436 struct ui_file *stream)
4437{
9a3c8263
SM
4438 struct dwarf2_locexpr_baton *dlbaton
4439 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
09ba997f
TT
4440 unsigned int addr_size = dlbaton->per_cu->addr_size ();
4441 int offset_size = dlbaton->per_cu->offset_size ();
08922a10 4442
3e43a32a
MS
4443 locexpr_describe_location_1 (symbol, addr, stream,
4444 dlbaton->data, dlbaton->size,
82ca3f51
SM
4445 addr_size, offset_size,
4446 dlbaton->per_cu, dlbaton->per_objfile);
08922a10
SS
4447}
4448
4449/* Describe the location of SYMBOL as an agent value in VALUE, generating
4450 any necessary bytecode in AX. */
a55cc764 4451
0d53c4c4 4452static void
40f4af28
SM
4453locexpr_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
4454 struct axs_value *value)
a55cc764 4455{
9a3c8263
SM
4456 struct dwarf2_locexpr_baton *dlbaton
4457 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (symbol);
09ba997f 4458 unsigned int addr_size = dlbaton->per_cu->addr_size ();
a55cc764 4459
1d6edc3c 4460 if (dlbaton->size == 0)
cabe9ab6
PA
4461 value->optimized_out = 1;
4462 else
40f4af28 4463 dwarf2_compile_expr_to_ax (ax, value, addr_size, dlbaton->data,
4b167ea1
SM
4464 dlbaton->data + dlbaton->size, dlbaton->per_cu,
4465 dlbaton->per_objfile);
a55cc764
DJ
4466}
4467
bb2ec1b3
TT
4468/* symbol_computed_ops 'generate_c_location' method. */
4469
4470static void
d82b3862 4471locexpr_generate_c_location (struct symbol *sym, string_file *stream,
bb2ec1b3
TT
4472 struct gdbarch *gdbarch,
4473 unsigned char *registers_used,
4474 CORE_ADDR pc, const char *result_name)
4475{
9a3c8263
SM
4476 struct dwarf2_locexpr_baton *dlbaton
4477 = (struct dwarf2_locexpr_baton *) SYMBOL_LOCATION_BATON (sym);
09ba997f 4478 unsigned int addr_size = dlbaton->per_cu->addr_size ();
bb2ec1b3
TT
4479
4480 if (dlbaton->size == 0)
987012b8 4481 error (_("symbol \"%s\" is optimized out"), sym->natural_name ());
bb2ec1b3
TT
4482
4483 compile_dwarf_expr_to_c (stream, result_name,
4484 sym, pc, gdbarch, registers_used, addr_size,
4485 dlbaton->data, dlbaton->data + dlbaton->size,
4b167ea1 4486 dlbaton->per_cu, dlbaton->per_objfile);
bb2ec1b3
TT
4487}
4488
4c2df51b
DJ
4489/* The set of location functions used with the DWARF-2 expression
4490 evaluator. */
768a979c 4491const struct symbol_computed_ops dwarf2_locexpr_funcs = {
4c2df51b 4492 locexpr_read_variable,
e18b2753 4493 locexpr_read_variable_at_entry,
0b31a4bc 4494 locexpr_get_symbol_read_needs,
4c2df51b 4495 locexpr_describe_location,
f1e6e072 4496 0, /* location_has_loclist */
bb2ec1b3
TT
4497 locexpr_tracepoint_var_ref,
4498 locexpr_generate_c_location
4c2df51b 4499};
0d53c4c4
DJ
4500
4501
4502/* Wrapper functions for location lists. These generally find
4503 the appropriate location expression and call something above. */
4504
4505/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
4506 evaluator to calculate the location. */
4507static struct value *
4508loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
4509{
9a3c8263
SM
4510 struct dwarf2_loclist_baton *dlbaton
4511 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
0d53c4c4 4512 struct value *val;
947bb88f 4513 const gdb_byte *data;
b6b08ebf 4514 size_t size;
8cf6f0b1 4515 CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
0d53c4c4 4516
8cf6f0b1 4517 data = dwarf2_find_location_expression (dlbaton, &size, pc);
1d6edc3c 4518 val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
9f47c707 4519 dlbaton->per_cu, dlbaton->per_objfile);
0d53c4c4
DJ
4520
4521 return val;
4522}
4523
e18b2753
JK
4524/* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function
4525 entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR
4526 will be thrown.
4527
4528 Function always returns non-NULL value, it may be marked optimized out if
4529 inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR
4530 if it cannot resolve the parameter for any reason. */
4531
4532static struct value *
4533loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame)
4534{
9a3c8263
SM
4535 struct dwarf2_loclist_baton *dlbaton
4536 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
e18b2753
JK
4537 const gdb_byte *data;
4538 size_t size;
4539 CORE_ADDR pc;
4540
4541 if (frame == NULL || !get_frame_func_if_available (frame, &pc))
4542 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4543
4544 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4545 if (data == NULL)
4546 return allocate_optimized_out_value (SYMBOL_TYPE (symbol));
4547
4548 return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size);
4549}
4550
0b31a4bc
TT
4551/* Implementation of get_symbol_read_needs from
4552 symbol_computed_ops. */
4553
4554static enum symbol_needs_kind
4555loclist_symbol_needs (struct symbol *symbol)
0d53c4c4
DJ
4556{
4557 /* If there's a location list, then assume we need to have a frame
4558 to choose the appropriate location expression. With tracking of
4559 global variables this is not necessarily true, but such tracking
4560 is disabled in GCC at the moment until we figure out how to
4561 represent it. */
4562
0b31a4bc 4563 return SYMBOL_NEEDS_FRAME;
0d53c4c4
DJ
4564}
4565
08922a10
SS
4566/* Print a natural-language description of SYMBOL to STREAM. This
4567 version applies when there is a list of different locations, each
4568 with a specified address range. */
4569
4570static void
4571loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
4572 struct ui_file *stream)
0d53c4c4 4573{
9a3c8263
SM
4574 struct dwarf2_loclist_baton *dlbaton
4575 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
947bb88f 4576 const gdb_byte *loc_ptr, *buf_end;
a50264ba
TT
4577 dwarf2_per_objfile *per_objfile = dlbaton->per_objfile;
4578 struct objfile *objfile = per_objfile->objfile;
08feed99 4579 struct gdbarch *gdbarch = objfile->arch ();
08922a10 4580 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
09ba997f
TT
4581 unsigned int addr_size = dlbaton->per_cu->addr_size ();
4582 int offset_size = dlbaton->per_cu->offset_size ();
d4a087c7 4583 int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
08922a10 4584 /* Adjust base_address for relocatable objects. */
4b167ea1 4585 CORE_ADDR base_offset = objfile->text_section_offset ();
08922a10 4586 CORE_ADDR base_address = dlbaton->base_address + base_offset;
f664829e 4587 int done = 0;
08922a10
SS
4588
4589 loc_ptr = dlbaton->data;
4590 buf_end = dlbaton->data + dlbaton->size;
4591
9eae7c52 4592 fprintf_filtered (stream, _("multi-location:\n"));
08922a10
SS
4593
4594 /* Iterate through locations until we run out. */
f664829e 4595 while (!done)
08922a10 4596 {
f664829e
DE
4597 CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */
4598 int length;
4599 enum debug_loc_kind kind;
4600 const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */
4601
85a9510c 4602 if (dlbaton->per_cu->version () < 5 && dlbaton->from_dwo)
f664829e 4603 kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu,
82ca3f51 4604 dlbaton->per_objfile,
f664829e 4605 loc_ptr, buf_end, &new_ptr,
3771a44c 4606 &low, &high, byte_order);
85a9510c 4607 else if (dlbaton->per_cu->version () < 5)
f664829e
DE
4608 kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr,
4609 &low, &high,
4610 byte_order, addr_size,
4611 signed_addr_p);
85a9510c 4612 else
4613 kind = decode_debug_loclists_addresses (dlbaton->per_cu,
82ca3f51 4614 dlbaton->per_objfile,
85a9510c 4615 loc_ptr, buf_end, &new_ptr,
4616 &low, &high, byte_order,
4617 addr_size, signed_addr_p);
f664829e
DE
4618 loc_ptr = new_ptr;
4619 switch (kind)
08922a10 4620 {
f664829e
DE
4621 case DEBUG_LOC_END_OF_LIST:
4622 done = 1;
4623 continue;
fc3ecb3e 4624
f664829e 4625 case DEBUG_LOC_BASE_ADDRESS:
d4a087c7 4626 base_address = high + base_offset;
9eae7c52 4627 fprintf_filtered (stream, _(" Base address %s"),
08922a10 4628 paddress (gdbarch, base_address));
08922a10 4629 continue;
fc3ecb3e 4630
3771a44c
DE
4631 case DEBUG_LOC_START_END:
4632 case DEBUG_LOC_START_LENGTH:
85a9510c 4633 case DEBUG_LOC_OFFSET_PAIR:
f664829e 4634 break;
fc3ecb3e 4635
f664829e
DE
4636 case DEBUG_LOC_BUFFER_OVERFLOW:
4637 case DEBUG_LOC_INVALID_ENTRY:
4638 error (_("Corrupted DWARF expression for symbol \"%s\"."),
987012b8 4639 symbol->print_name ());
fc3ecb3e 4640
f664829e
DE
4641 default:
4642 gdb_assert_not_reached ("bad debug_loc_kind");
08922a10
SS
4643 }
4644
08922a10
SS
4645 /* Otherwise, a location expression entry. */
4646 low += base_address;
4647 high += base_address;
4648
3e29f34a
MR
4649 low = gdbarch_adjust_dwarf2_addr (gdbarch, low);
4650 high = gdbarch_adjust_dwarf2_addr (gdbarch, high);
4651
85a9510c 4652 if (dlbaton->per_cu->version () < 5)
4653 {
4654 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
4655 loc_ptr += 2;
4656 }
4657 else
4658 {
4659 unsigned int bytes_read;
4660 length = read_unsigned_leb128 (NULL, loc_ptr, &bytes_read);
4661 loc_ptr += bytes_read;
4662 }
08922a10 4663
08922a10
SS
4664 /* (It would improve readability to print only the minimum
4665 necessary digits of the second number of the range.) */
9eae7c52 4666 fprintf_filtered (stream, _(" Range %s-%s: "),
08922a10
SS
4667 paddress (gdbarch, low), paddress (gdbarch, high));
4668
4669 /* Now describe this particular location. */
4670 locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
82ca3f51
SM
4671 addr_size, offset_size,
4672 dlbaton->per_cu, dlbaton->per_objfile);
9eae7c52
TT
4673
4674 fprintf_filtered (stream, "\n");
08922a10
SS
4675
4676 loc_ptr += length;
4677 }
0d53c4c4
DJ
4678}
4679
4680/* Describe the location of SYMBOL as an agent value in VALUE, generating
4681 any necessary bytecode in AX. */
4682static void
40f4af28
SM
4683loclist_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
4684 struct axs_value *value)
0d53c4c4 4685{
9a3c8263
SM
4686 struct dwarf2_loclist_baton *dlbaton
4687 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (symbol);
947bb88f 4688 const gdb_byte *data;
b6b08ebf 4689 size_t size;
09ba997f 4690 unsigned int addr_size = dlbaton->per_cu->addr_size ();
0d53c4c4 4691
8cf6f0b1 4692 data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
1d6edc3c 4693 if (size == 0)
cabe9ab6
PA
4694 value->optimized_out = 1;
4695 else
40f4af28 4696 dwarf2_compile_expr_to_ax (ax, value, addr_size, data, data + size,
4b167ea1 4697 dlbaton->per_cu, dlbaton->per_objfile);
0d53c4c4
DJ
4698}
4699
bb2ec1b3
TT
4700/* symbol_computed_ops 'generate_c_location' method. */
4701
4702static void
d82b3862 4703loclist_generate_c_location (struct symbol *sym, string_file *stream,
bb2ec1b3
TT
4704 struct gdbarch *gdbarch,
4705 unsigned char *registers_used,
4706 CORE_ADDR pc, const char *result_name)
4707{
9a3c8263
SM
4708 struct dwarf2_loclist_baton *dlbaton
4709 = (struct dwarf2_loclist_baton *) SYMBOL_LOCATION_BATON (sym);
09ba997f 4710 unsigned int addr_size = dlbaton->per_cu->addr_size ();
bb2ec1b3
TT
4711 const gdb_byte *data;
4712 size_t size;
4713
4714 data = dwarf2_find_location_expression (dlbaton, &size, pc);
4715 if (size == 0)
987012b8 4716 error (_("symbol \"%s\" is optimized out"), sym->natural_name ());
bb2ec1b3
TT
4717
4718 compile_dwarf_expr_to_c (stream, result_name,
4719 sym, pc, gdbarch, registers_used, addr_size,
4720 data, data + size,
4b167ea1
SM
4721 dlbaton->per_cu,
4722 dlbaton->per_objfile);
bb2ec1b3
TT
4723}
4724
0d53c4c4
DJ
4725/* The set of location functions used with the DWARF-2 expression
4726 evaluator and location lists. */
768a979c 4727const struct symbol_computed_ops dwarf2_loclist_funcs = {
0d53c4c4 4728 loclist_read_variable,
e18b2753 4729 loclist_read_variable_at_entry,
0b31a4bc 4730 loclist_symbol_needs,
0d53c4c4 4731 loclist_describe_location,
f1e6e072 4732 1, /* location_has_loclist */
bb2ec1b3
TT
4733 loclist_tracepoint_var_ref,
4734 loclist_generate_c_location
0d53c4c4 4735};
8e3b41a9 4736
6c265988 4737void _initialize_dwarf2loc ();
8e3b41a9 4738void
6c265988 4739_initialize_dwarf2loc ()
8e3b41a9 4740{
ccce17b0
YQ
4741 add_setshow_zuinteger_cmd ("entry-values", class_maintenance,
4742 &entry_values_debug,
4743 _("Set entry values and tail call frames "
4744 "debugging."),
4745 _("Show entry values and tail call frames "
4746 "debugging."),
4747 _("When non-zero, the process of determining "
4748 "parameter values from function entry point "
4749 "and tail call frames will be printed."),
4750 NULL,
4751 show_entry_values_debug,
4752 &setdebuglist, &showdebuglist);
009b64fc
TT
4753
4754 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
4755 &dwarf_always_disassemble, _("\
4756Set whether `info address' always disassembles DWARF expressions."), _("\
4757Show whether `info address' always disassembles DWARF expressions."), _("\
4758When enabled, DWARF expressions are always printed in an assembly-like\n\
4759syntax. When disabled, expressions will be printed in a more\n\
4760conversational style, when possible."),
4761 NULL,
4762 show_dwarf_always_disassemble,
4763 &set_dwarf_cmdlist,
4764 &show_dwarf_cmdlist);
8e3b41a9 4765}
This page took 1.543739 seconds and 4 git commands to generate.