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