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