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