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