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