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