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