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