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