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