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