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