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