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