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