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