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