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