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