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