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