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