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