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