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