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