Commit | Line | Data |
---|---|---|
4c2df51b | 1 | /* DWARF 2 location expression support for GDB. |
feb13ab0 | 2 | |
4c38e0a4 JB |
3 | Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010 |
4 | Free Software Foundation, Inc. | |
feb13ab0 | 5 | |
4c2df51b DJ |
6 | Contributed by Daniel Jacobowitz, MontaVista Software, Inc. |
7 | ||
8 | This file is part of GDB. | |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
a9762ec7 JB |
12 | the Free Software Foundation; either version 3 of the License, or |
13 | (at your option) any later version. | |
4c2df51b | 14 | |
a9762ec7 JB |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
4c2df51b DJ |
19 | |
20 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4c2df51b DJ |
22 | |
23 | #include "defs.h" | |
24 | #include "ui-out.h" | |
25 | #include "value.h" | |
26 | #include "frame.h" | |
27 | #include "gdbcore.h" | |
28 | #include "target.h" | |
29 | #include "inferior.h" | |
a55cc764 DJ |
30 | #include "ax.h" |
31 | #include "ax-gdb.h" | |
e4adbba9 | 32 | #include "regcache.h" |
c3228f12 | 33 | #include "objfiles.h" |
93ad78a7 | 34 | #include "exceptions.h" |
edb3359d | 35 | #include "block.h" |
4c2df51b | 36 | |
fa8f86ff | 37 | #include "dwarf2.h" |
4c2df51b DJ |
38 | #include "dwarf2expr.h" |
39 | #include "dwarf2loc.h" | |
e7802207 | 40 | #include "dwarf2-frame.h" |
4c2df51b DJ |
41 | |
42 | #include "gdb_string.h" | |
eff4f95e | 43 | #include "gdb_assert.h" |
4c2df51b | 44 | |
9eae7c52 TT |
45 | extern int dwarf2_always_disassemble; |
46 | ||
0936ad1d SS |
47 | static void |
48 | dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc, | |
0d45f56e | 49 | const gdb_byte **start, size_t *length); |
0936ad1d | 50 | |
0d53c4c4 DJ |
51 | /* A helper function for dealing with location lists. Given a |
52 | symbol baton (BATON) and a pc value (PC), find the appropriate | |
53 | location expression, set *LOCEXPR_LENGTH, and return a pointer | |
54 | to the beginning of the expression. Returns NULL on failure. | |
55 | ||
56 | For now, only return the first matching location expression; there | |
57 | can be more than one in the list. */ | |
58 | ||
947bb88f | 59 | static const gdb_byte * |
0d53c4c4 | 60 | find_location_expression (struct dwarf2_loclist_baton *baton, |
b6b08ebf | 61 | size_t *locexpr_length, CORE_ADDR pc) |
0d53c4c4 | 62 | { |
0d53c4c4 | 63 | CORE_ADDR low, high; |
947bb88f | 64 | const gdb_byte *loc_ptr, *buf_end; |
852483bc | 65 | int length; |
ae0d2f24 | 66 | struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu); |
f7fd4728 | 67 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
e17a4113 | 68 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
ae0d2f24 | 69 | unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu); |
d4a087c7 | 70 | int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd); |
0d53c4c4 | 71 | CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1)); |
8edfa926 | 72 | /* Adjust base_address for relocatable objects. */ |
ae0d2f24 UW |
73 | CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets, |
74 | SECT_OFF_TEXT (objfile)); | |
8edfa926 | 75 | CORE_ADDR base_address = baton->base_address + base_offset; |
0d53c4c4 DJ |
76 | |
77 | loc_ptr = baton->data; | |
78 | buf_end = baton->data + baton->size; | |
79 | ||
80 | while (1) | |
81 | { | |
b5758fe4 UW |
82 | if (buf_end - loc_ptr < 2 * addr_size) |
83 | error (_("find_location_expression: Corrupted DWARF expression.")); | |
0d53c4c4 | 84 | |
d4a087c7 UW |
85 | if (signed_addr_p) |
86 | low = extract_signed_integer (loc_ptr, addr_size, byte_order); | |
87 | else | |
88 | low = extract_unsigned_integer (loc_ptr, addr_size, byte_order); | |
89 | loc_ptr += addr_size; | |
90 | ||
91 | if (signed_addr_p) | |
92 | high = extract_signed_integer (loc_ptr, addr_size, byte_order); | |
93 | else | |
94 | high = extract_unsigned_integer (loc_ptr, addr_size, byte_order); | |
b5758fe4 | 95 | loc_ptr += addr_size; |
0d53c4c4 DJ |
96 | |
97 | /* A base-address-selection entry. */ | |
d4a087c7 | 98 | if ((low & base_mask) == base_mask) |
0d53c4c4 | 99 | { |
d4a087c7 | 100 | base_address = high + base_offset; |
0d53c4c4 DJ |
101 | continue; |
102 | } | |
103 | ||
b5758fe4 UW |
104 | /* An end-of-list entry. */ |
105 | if (low == 0 && high == 0) | |
106 | return NULL; | |
107 | ||
0d53c4c4 DJ |
108 | /* Otherwise, a location expression entry. */ |
109 | low += base_address; | |
110 | high += base_address; | |
111 | ||
e17a4113 | 112 | length = extract_unsigned_integer (loc_ptr, 2, byte_order); |
0d53c4c4 DJ |
113 | loc_ptr += 2; |
114 | ||
115 | if (pc >= low && pc < high) | |
116 | { | |
117 | *locexpr_length = length; | |
118 | return loc_ptr; | |
119 | } | |
120 | ||
121 | loc_ptr += length; | |
122 | } | |
123 | } | |
124 | ||
4c2df51b DJ |
125 | /* This is the baton used when performing dwarf2 expression |
126 | evaluation. */ | |
127 | struct dwarf_expr_baton | |
128 | { | |
129 | struct frame_info *frame; | |
17ea53c3 | 130 | struct dwarf2_per_cu_data *per_cu; |
4c2df51b DJ |
131 | }; |
132 | ||
133 | /* Helper functions for dwarf2_evaluate_loc_desc. */ | |
134 | ||
4bc9efe1 | 135 | /* Using the frame specified in BATON, return the value of register |
0b2b0195 | 136 | REGNUM, treated as a pointer. */ |
4c2df51b | 137 | static CORE_ADDR |
61fbb938 | 138 | dwarf_expr_read_reg (void *baton, int dwarf_regnum) |
4c2df51b | 139 | { |
4c2df51b | 140 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; |
5e2b427d | 141 | struct gdbarch *gdbarch = get_frame_arch (debaton->frame); |
e5192dd8 | 142 | CORE_ADDR result; |
0b2b0195 | 143 | int regnum; |
e4adbba9 | 144 | |
5e2b427d UW |
145 | regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum); |
146 | result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr, | |
0b2b0195 | 147 | regnum, debaton->frame); |
4c2df51b DJ |
148 | return result; |
149 | } | |
150 | ||
151 | /* Read memory at ADDR (length LEN) into BUF. */ | |
152 | ||
153 | static void | |
852483bc | 154 | dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len) |
4c2df51b DJ |
155 | { |
156 | read_memory (addr, buf, len); | |
157 | } | |
158 | ||
159 | /* Using the frame specified in BATON, find the location expression | |
160 | describing the frame base. Return a pointer to it in START and | |
161 | its length in LENGTH. */ | |
162 | static void | |
0d45f56e | 163 | dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length) |
4c2df51b | 164 | { |
da62e633 AC |
165 | /* FIXME: cagney/2003-03-26: This code should be using |
166 | get_frame_base_address(), and then implement a dwarf2 specific | |
167 | this_base method. */ | |
4c2df51b | 168 | struct symbol *framefunc; |
4c2df51b | 169 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; |
0d53c4c4 | 170 | |
edb3359d DJ |
171 | /* Use block_linkage_function, which returns a real (not inlined) |
172 | function, instead of get_frame_function, which may return an | |
173 | inlined function. */ | |
174 | framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL)); | |
0d53c4c4 | 175 | |
eff4f95e JG |
176 | /* If we found a frame-relative symbol then it was certainly within |
177 | some function associated with a frame. If we can't find the frame, | |
178 | something has gone wrong. */ | |
179 | gdb_assert (framefunc != NULL); | |
180 | ||
0936ad1d SS |
181 | dwarf_expr_frame_base_1 (framefunc, |
182 | get_frame_address_in_block (debaton->frame), | |
183 | start, length); | |
184 | } | |
185 | ||
186 | static void | |
187 | dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc, | |
0d45f56e | 188 | const gdb_byte **start, size_t *length) |
0936ad1d | 189 | { |
edb3359d DJ |
190 | if (SYMBOL_LOCATION_BATON (framefunc) == NULL) |
191 | *start = NULL; | |
192 | else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs) | |
0d53c4c4 DJ |
193 | { |
194 | struct dwarf2_loclist_baton *symbaton; | |
22c6caba | 195 | |
0d53c4c4 | 196 | symbaton = SYMBOL_LOCATION_BATON (framefunc); |
0936ad1d | 197 | *start = find_location_expression (symbaton, length, pc); |
0d53c4c4 DJ |
198 | } |
199 | else | |
200 | { | |
201 | struct dwarf2_locexpr_baton *symbaton; | |
9a619af0 | 202 | |
0d53c4c4 | 203 | symbaton = SYMBOL_LOCATION_BATON (framefunc); |
ebd3bcc1 JK |
204 | if (symbaton != NULL) |
205 | { | |
206 | *length = symbaton->size; | |
207 | *start = symbaton->data; | |
208 | } | |
209 | else | |
210 | *start = NULL; | |
0d53c4c4 DJ |
211 | } |
212 | ||
213 | if (*start == NULL) | |
8a3fe4f8 | 214 | error (_("Could not find the frame base for \"%s\"."), |
0d53c4c4 | 215 | SYMBOL_NATURAL_NAME (framefunc)); |
4c2df51b DJ |
216 | } |
217 | ||
e7802207 TT |
218 | /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for |
219 | the frame in BATON. */ | |
220 | ||
221 | static CORE_ADDR | |
222 | dwarf_expr_frame_cfa (void *baton) | |
223 | { | |
224 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; | |
9a619af0 | 225 | |
e7802207 TT |
226 | return dwarf2_frame_cfa (debaton->frame); |
227 | } | |
228 | ||
4c2df51b DJ |
229 | /* Using the objfile specified in BATON, find the address for the |
230 | current thread's thread-local storage with offset OFFSET. */ | |
231 | static CORE_ADDR | |
232 | dwarf_expr_tls_address (void *baton, CORE_ADDR offset) | |
233 | { | |
234 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; | |
17ea53c3 | 235 | struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu); |
4c2df51b | 236 | |
17ea53c3 | 237 | return target_translate_tls_address (objfile, offset); |
4c2df51b DJ |
238 | } |
239 | ||
5c631832 JK |
240 | /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in current CU |
241 | (as is PER_CU). State of the CTX is not affected by the call and return. */ | |
242 | ||
243 | static void | |
244 | per_cu_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset, | |
245 | struct dwarf2_per_cu_data *per_cu) | |
246 | { | |
247 | struct dwarf2_locexpr_baton block; | |
248 | ||
249 | block = dwarf2_fetch_die_location_block (die_offset, per_cu); | |
250 | ||
251 | /* DW_OP_call_ref is currently not supported. */ | |
252 | gdb_assert (block.per_cu == per_cu); | |
253 | ||
254 | dwarf_expr_eval (ctx, block.data, block.size); | |
255 | } | |
256 | ||
257 | /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */ | |
258 | ||
259 | static void | |
260 | dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset) | |
261 | { | |
262 | struct dwarf_expr_baton *debaton = ctx->baton; | |
263 | ||
264 | return per_cu_dwarf_call (ctx, die_offset, debaton->per_cu); | |
265 | } | |
266 | ||
052b9502 NF |
267 | struct piece_closure |
268 | { | |
88bfdde4 TT |
269 | /* Reference count. */ |
270 | int refc; | |
271 | ||
052b9502 NF |
272 | /* The number of pieces used to describe this variable. */ |
273 | int n_pieces; | |
274 | ||
6063c216 UW |
275 | /* The target address size, used only for DWARF_VALUE_STACK. */ |
276 | int addr_size; | |
cec03d70 | 277 | |
052b9502 NF |
278 | /* The pieces themselves. */ |
279 | struct dwarf_expr_piece *pieces; | |
280 | }; | |
281 | ||
282 | /* Allocate a closure for a value formed from separately-described | |
283 | PIECES. */ | |
284 | ||
285 | static struct piece_closure * | |
cec03d70 | 286 | allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces, |
6063c216 | 287 | int addr_size) |
052b9502 NF |
288 | { |
289 | struct piece_closure *c = XZALLOC (struct piece_closure); | |
290 | ||
88bfdde4 | 291 | c->refc = 1; |
052b9502 | 292 | c->n_pieces = n_pieces; |
6063c216 | 293 | c->addr_size = addr_size; |
052b9502 NF |
294 | c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece); |
295 | ||
296 | memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece)); | |
297 | ||
298 | return c; | |
299 | } | |
300 | ||
d3b1e874 TT |
301 | /* The lowest-level function to extract bits from a byte buffer. |
302 | SOURCE is the buffer. It is updated if we read to the end of a | |
303 | byte. | |
304 | SOURCE_OFFSET_BITS is the offset of the first bit to read. It is | |
305 | updated to reflect the number of bits actually read. | |
306 | NBITS is the number of bits we want to read. It is updated to | |
307 | reflect the number of bits actually read. This function may read | |
308 | fewer bits. | |
309 | BITS_BIG_ENDIAN is taken directly from gdbarch. | |
310 | This function returns the extracted bits. */ | |
311 | ||
312 | static unsigned int | |
313 | extract_bits_primitive (const gdb_byte **source, | |
314 | unsigned int *source_offset_bits, | |
315 | int *nbits, int bits_big_endian) | |
316 | { | |
317 | unsigned int avail, mask, datum; | |
318 | ||
319 | gdb_assert (*source_offset_bits < 8); | |
320 | ||
321 | avail = 8 - *source_offset_bits; | |
322 | if (avail > *nbits) | |
323 | avail = *nbits; | |
324 | ||
325 | mask = (1 << avail) - 1; | |
326 | datum = **source; | |
327 | if (bits_big_endian) | |
328 | datum >>= 8 - (*source_offset_bits + *nbits); | |
329 | else | |
330 | datum >>= *source_offset_bits; | |
331 | datum &= mask; | |
332 | ||
333 | *nbits -= avail; | |
334 | *source_offset_bits += avail; | |
335 | if (*source_offset_bits >= 8) | |
336 | { | |
337 | *source_offset_bits -= 8; | |
338 | ++*source; | |
339 | } | |
340 | ||
341 | return datum; | |
342 | } | |
343 | ||
344 | /* Extract some bits from a source buffer and move forward in the | |
345 | buffer. | |
346 | ||
347 | SOURCE is the source buffer. It is updated as bytes are read. | |
348 | SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as | |
349 | bits are read. | |
350 | NBITS is the number of bits to read. | |
351 | BITS_BIG_ENDIAN is taken directly from gdbarch. | |
352 | ||
353 | This function returns the bits that were read. */ | |
354 | ||
355 | static unsigned int | |
356 | extract_bits (const gdb_byte **source, unsigned int *source_offset_bits, | |
357 | int nbits, int bits_big_endian) | |
358 | { | |
359 | unsigned int datum; | |
360 | ||
361 | gdb_assert (nbits > 0 && nbits <= 8); | |
362 | ||
363 | datum = extract_bits_primitive (source, source_offset_bits, &nbits, | |
364 | bits_big_endian); | |
365 | if (nbits > 0) | |
366 | { | |
367 | unsigned int more; | |
368 | ||
369 | more = extract_bits_primitive (source, source_offset_bits, &nbits, | |
370 | bits_big_endian); | |
371 | if (bits_big_endian) | |
372 | datum <<= nbits; | |
373 | else | |
374 | more <<= nbits; | |
375 | datum |= more; | |
376 | } | |
377 | ||
378 | return datum; | |
379 | } | |
380 | ||
381 | /* Write some bits into a buffer and move forward in the buffer. | |
382 | ||
383 | DATUM is the bits to write. The low-order bits of DATUM are used. | |
384 | DEST is the destination buffer. It is updated as bytes are | |
385 | written. | |
386 | DEST_OFFSET_BITS is the bit offset in DEST at which writing is | |
387 | done. | |
388 | NBITS is the number of valid bits in DATUM. | |
389 | BITS_BIG_ENDIAN is taken directly from gdbarch. */ | |
390 | ||
391 | static void | |
392 | insert_bits (unsigned int datum, | |
393 | gdb_byte *dest, unsigned int dest_offset_bits, | |
394 | int nbits, int bits_big_endian) | |
395 | { | |
396 | unsigned int mask; | |
397 | ||
398 | gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8); | |
399 | ||
400 | mask = (1 << nbits) - 1; | |
401 | if (bits_big_endian) | |
402 | { | |
403 | datum <<= 8 - (dest_offset_bits + nbits); | |
404 | mask <<= 8 - (dest_offset_bits + nbits); | |
405 | } | |
406 | else | |
407 | { | |
408 | datum <<= dest_offset_bits; | |
409 | mask <<= dest_offset_bits; | |
410 | } | |
411 | ||
412 | gdb_assert ((datum & ~mask) == 0); | |
413 | ||
414 | *dest = (*dest & ~mask) | datum; | |
415 | } | |
416 | ||
417 | /* Copy bits from a source to a destination. | |
418 | ||
419 | DEST is where the bits should be written. | |
420 | DEST_OFFSET_BITS is the bit offset into DEST. | |
421 | SOURCE is the source of bits. | |
422 | SOURCE_OFFSET_BITS is the bit offset into SOURCE. | |
423 | BIT_COUNT is the number of bits to copy. | |
424 | BITS_BIG_ENDIAN is taken directly from gdbarch. */ | |
425 | ||
426 | static void | |
427 | copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits, | |
428 | const gdb_byte *source, unsigned int source_offset_bits, | |
429 | unsigned int bit_count, | |
430 | int bits_big_endian) | |
431 | { | |
432 | unsigned int dest_avail; | |
433 | int datum; | |
434 | ||
435 | /* Reduce everything to byte-size pieces. */ | |
436 | dest += dest_offset_bits / 8; | |
437 | dest_offset_bits %= 8; | |
438 | source += source_offset_bits / 8; | |
439 | source_offset_bits %= 8; | |
440 | ||
441 | dest_avail = 8 - dest_offset_bits % 8; | |
442 | ||
443 | /* See if we can fill the first destination byte. */ | |
444 | if (dest_avail < bit_count) | |
445 | { | |
446 | datum = extract_bits (&source, &source_offset_bits, dest_avail, | |
447 | bits_big_endian); | |
448 | insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian); | |
449 | ++dest; | |
450 | dest_offset_bits = 0; | |
451 | bit_count -= dest_avail; | |
452 | } | |
453 | ||
454 | /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer | |
455 | than 8 bits remaining. */ | |
456 | gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8); | |
457 | for (; bit_count >= 8; bit_count -= 8) | |
458 | { | |
459 | datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian); | |
460 | *dest++ = (gdb_byte) datum; | |
461 | } | |
462 | ||
463 | /* Finally, we may have a few leftover bits. */ | |
464 | gdb_assert (bit_count <= 8 - dest_offset_bits % 8); | |
465 | if (bit_count > 0) | |
466 | { | |
467 | datum = extract_bits (&source, &source_offset_bits, bit_count, | |
468 | bits_big_endian); | |
469 | insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian); | |
470 | } | |
471 | } | |
472 | ||
052b9502 NF |
473 | static void |
474 | read_pieced_value (struct value *v) | |
475 | { | |
476 | int i; | |
477 | long offset = 0; | |
d3b1e874 | 478 | ULONGEST bits_to_skip; |
052b9502 NF |
479 | gdb_byte *contents; |
480 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (v); | |
481 | struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v)); | |
afd74c5f | 482 | size_t type_len; |
d3b1e874 TT |
483 | size_t buffer_size = 0; |
484 | char *buffer = NULL; | |
485 | struct cleanup *cleanup; | |
486 | int bits_big_endian | |
487 | = gdbarch_bits_big_endian (get_type_arch (value_type (v))); | |
afd74c5f TT |
488 | |
489 | if (value_type (v) != value_enclosing_type (v)) | |
490 | internal_error (__FILE__, __LINE__, | |
491 | _("Should not be able to create a lazy value with " | |
492 | "an enclosing type")); | |
052b9502 | 493 | |
d3b1e874 TT |
494 | cleanup = make_cleanup (free_current_contents, &buffer); |
495 | ||
052b9502 | 496 | contents = value_contents_raw (v); |
d3b1e874 | 497 | bits_to_skip = 8 * value_offset (v); |
0e03807e TT |
498 | if (value_bitsize (v)) |
499 | { | |
500 | bits_to_skip += value_bitpos (v); | |
501 | type_len = value_bitsize (v); | |
502 | } | |
503 | else | |
504 | type_len = 8 * TYPE_LENGTH (value_type (v)); | |
d3b1e874 | 505 | |
afd74c5f | 506 | for (i = 0; i < c->n_pieces && offset < type_len; i++) |
052b9502 NF |
507 | { |
508 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
d3b1e874 TT |
509 | size_t this_size, this_size_bits; |
510 | long dest_offset_bits, source_offset_bits, source_offset; | |
0d45f56e | 511 | const gdb_byte *intermediate_buffer; |
d3b1e874 TT |
512 | |
513 | /* Compute size, source, and destination offsets for copying, in | |
514 | bits. */ | |
515 | this_size_bits = p->size; | |
516 | if (bits_to_skip > 0 && bits_to_skip >= this_size_bits) | |
afd74c5f | 517 | { |
d3b1e874 | 518 | bits_to_skip -= this_size_bits; |
afd74c5f TT |
519 | continue; |
520 | } | |
d3b1e874 TT |
521 | if (this_size_bits > type_len - offset) |
522 | this_size_bits = type_len - offset; | |
523 | if (bits_to_skip > 0) | |
afd74c5f | 524 | { |
d3b1e874 TT |
525 | dest_offset_bits = 0; |
526 | source_offset_bits = bits_to_skip; | |
527 | this_size_bits -= bits_to_skip; | |
528 | bits_to_skip = 0; | |
afd74c5f TT |
529 | } |
530 | else | |
531 | { | |
d3b1e874 TT |
532 | dest_offset_bits = offset; |
533 | source_offset_bits = 0; | |
afd74c5f | 534 | } |
9a619af0 | 535 | |
d3b1e874 TT |
536 | this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8; |
537 | source_offset = source_offset_bits / 8; | |
538 | if (buffer_size < this_size) | |
539 | { | |
540 | buffer_size = this_size; | |
541 | buffer = xrealloc (buffer, buffer_size); | |
542 | } | |
543 | intermediate_buffer = buffer; | |
544 | ||
545 | /* Copy from the source to DEST_BUFFER. */ | |
cec03d70 | 546 | switch (p->location) |
052b9502 | 547 | { |
cec03d70 TT |
548 | case DWARF_VALUE_REGISTER: |
549 | { | |
550 | struct gdbarch *arch = get_frame_arch (frame); | |
f2c7657e | 551 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.value); |
afd74c5f | 552 | int reg_offset = source_offset; |
dcbf108f UW |
553 | |
554 | if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG | |
afd74c5f | 555 | && this_size < register_size (arch, gdb_regnum)) |
d3b1e874 TT |
556 | { |
557 | /* Big-endian, and we want less than full size. */ | |
558 | reg_offset = register_size (arch, gdb_regnum) - this_size; | |
559 | /* We want the lower-order THIS_SIZE_BITS of the bytes | |
560 | we extract from the register. */ | |
561 | source_offset_bits += 8 * this_size - this_size_bits; | |
562 | } | |
dcbf108f | 563 | |
63b4f126 MGD |
564 | if (gdb_regnum != -1) |
565 | { | |
566 | get_frame_register_bytes (frame, gdb_regnum, reg_offset, | |
d3b1e874 | 567 | this_size, buffer); |
63b4f126 MGD |
568 | } |
569 | else | |
570 | { | |
571 | error (_("Unable to access DWARF register number %s"), | |
f2c7657e | 572 | paddress (arch, p->v.value)); |
63b4f126 | 573 | } |
cec03d70 TT |
574 | } |
575 | break; | |
576 | ||
577 | case DWARF_VALUE_MEMORY: | |
f2c7657e UW |
578 | if (p->v.mem.in_stack_memory) |
579 | read_stack (p->v.mem.addr + source_offset, buffer, this_size); | |
44353522 | 580 | else |
f2c7657e | 581 | read_memory (p->v.mem.addr + source_offset, buffer, this_size); |
cec03d70 TT |
582 | break; |
583 | ||
584 | case DWARF_VALUE_STACK: | |
585 | { | |
6063c216 | 586 | struct gdbarch *gdbarch = get_type_arch (value_type (v)); |
afd74c5f | 587 | size_t n = this_size; |
9a619af0 | 588 | |
afd74c5f TT |
589 | if (n > c->addr_size - source_offset) |
590 | n = (c->addr_size >= source_offset | |
591 | ? c->addr_size - source_offset | |
592 | : 0); | |
593 | if (n == 0) | |
594 | { | |
595 | /* Nothing. */ | |
596 | } | |
597 | else if (source_offset == 0) | |
d3b1e874 | 598 | store_unsigned_integer (buffer, n, |
afd74c5f | 599 | gdbarch_byte_order (gdbarch), |
f2c7657e | 600 | p->v.value); |
afd74c5f TT |
601 | else |
602 | { | |
603 | gdb_byte bytes[sizeof (ULONGEST)]; | |
604 | ||
605 | store_unsigned_integer (bytes, n + source_offset, | |
606 | gdbarch_byte_order (gdbarch), | |
f2c7657e | 607 | p->v.value); |
d3b1e874 | 608 | memcpy (buffer, bytes + source_offset, n); |
afd74c5f | 609 | } |
cec03d70 TT |
610 | } |
611 | break; | |
612 | ||
613 | case DWARF_VALUE_LITERAL: | |
614 | { | |
afd74c5f TT |
615 | size_t n = this_size; |
616 | ||
617 | if (n > p->v.literal.length - source_offset) | |
618 | n = (p->v.literal.length >= source_offset | |
619 | ? p->v.literal.length - source_offset | |
620 | : 0); | |
621 | if (n != 0) | |
d3b1e874 | 622 | intermediate_buffer = p->v.literal.data + source_offset; |
cec03d70 TT |
623 | } |
624 | break; | |
625 | ||
cb826367 | 626 | case DWARF_VALUE_OPTIMIZED_OUT: |
0e03807e | 627 | set_value_optimized_out (v, 1); |
cb826367 TT |
628 | break; |
629 | ||
cec03d70 TT |
630 | default: |
631 | internal_error (__FILE__, __LINE__, _("invalid location type")); | |
052b9502 | 632 | } |
d3b1e874 TT |
633 | |
634 | if (p->location != DWARF_VALUE_OPTIMIZED_OUT) | |
635 | copy_bitwise (contents, dest_offset_bits, | |
636 | intermediate_buffer, source_offset_bits % 8, | |
637 | this_size_bits, bits_big_endian); | |
638 | ||
639 | offset += this_size_bits; | |
052b9502 | 640 | } |
d3b1e874 TT |
641 | |
642 | do_cleanups (cleanup); | |
052b9502 NF |
643 | } |
644 | ||
645 | static void | |
646 | write_pieced_value (struct value *to, struct value *from) | |
647 | { | |
648 | int i; | |
649 | long offset = 0; | |
d3b1e874 | 650 | ULONGEST bits_to_skip; |
afd74c5f | 651 | const gdb_byte *contents; |
052b9502 NF |
652 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (to); |
653 | struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to)); | |
afd74c5f | 654 | size_t type_len; |
d3b1e874 TT |
655 | size_t buffer_size = 0; |
656 | char *buffer = NULL; | |
657 | struct cleanup *cleanup; | |
658 | int bits_big_endian | |
659 | = gdbarch_bits_big_endian (get_type_arch (value_type (to))); | |
052b9502 NF |
660 | |
661 | if (frame == NULL) | |
662 | { | |
663 | set_value_optimized_out (to, 1); | |
664 | return; | |
665 | } | |
666 | ||
d3b1e874 TT |
667 | cleanup = make_cleanup (free_current_contents, &buffer); |
668 | ||
afd74c5f | 669 | contents = value_contents (from); |
d3b1e874 | 670 | bits_to_skip = 8 * value_offset (to); |
0e03807e TT |
671 | if (value_bitsize (to)) |
672 | { | |
673 | bits_to_skip += value_bitpos (to); | |
674 | type_len = value_bitsize (to); | |
675 | } | |
676 | else | |
677 | type_len = 8 * TYPE_LENGTH (value_type (to)); | |
678 | ||
afd74c5f | 679 | for (i = 0; i < c->n_pieces && offset < type_len; i++) |
052b9502 NF |
680 | { |
681 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
d3b1e874 TT |
682 | size_t this_size_bits, this_size; |
683 | long dest_offset_bits, source_offset_bits, dest_offset, source_offset; | |
684 | int need_bitwise; | |
685 | const gdb_byte *source_buffer; | |
afd74c5f | 686 | |
d3b1e874 TT |
687 | this_size_bits = p->size; |
688 | if (bits_to_skip > 0 && bits_to_skip >= this_size_bits) | |
afd74c5f | 689 | { |
d3b1e874 | 690 | bits_to_skip -= this_size_bits; |
afd74c5f TT |
691 | continue; |
692 | } | |
d3b1e874 TT |
693 | if (this_size_bits > type_len - offset) |
694 | this_size_bits = type_len - offset; | |
695 | if (bits_to_skip > 0) | |
afd74c5f | 696 | { |
d3b1e874 TT |
697 | dest_offset_bits = bits_to_skip; |
698 | source_offset_bits = 0; | |
699 | this_size_bits -= bits_to_skip; | |
700 | bits_to_skip = 0; | |
afd74c5f TT |
701 | } |
702 | else | |
703 | { | |
d3b1e874 TT |
704 | dest_offset_bits = 0; |
705 | source_offset_bits = offset; | |
706 | } | |
707 | ||
708 | this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8; | |
709 | source_offset = source_offset_bits / 8; | |
710 | dest_offset = dest_offset_bits / 8; | |
711 | if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0) | |
712 | { | |
713 | source_buffer = contents + source_offset; | |
714 | need_bitwise = 0; | |
715 | } | |
716 | else | |
717 | { | |
718 | if (buffer_size < this_size) | |
719 | { | |
720 | buffer_size = this_size; | |
721 | buffer = xrealloc (buffer, buffer_size); | |
722 | } | |
723 | source_buffer = buffer; | |
724 | need_bitwise = 1; | |
afd74c5f | 725 | } |
9a619af0 | 726 | |
cec03d70 | 727 | switch (p->location) |
052b9502 | 728 | { |
cec03d70 TT |
729 | case DWARF_VALUE_REGISTER: |
730 | { | |
731 | struct gdbarch *arch = get_frame_arch (frame); | |
f2c7657e | 732 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.value); |
afd74c5f | 733 | int reg_offset = dest_offset; |
dcbf108f UW |
734 | |
735 | if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG | |
afd74c5f | 736 | && this_size <= register_size (arch, gdb_regnum)) |
dcbf108f | 737 | /* Big-endian, and we want less than full size. */ |
afd74c5f | 738 | reg_offset = register_size (arch, gdb_regnum) - this_size; |
dcbf108f | 739 | |
63b4f126 MGD |
740 | if (gdb_regnum != -1) |
741 | { | |
d3b1e874 TT |
742 | if (need_bitwise) |
743 | { | |
744 | get_frame_register_bytes (frame, gdb_regnum, reg_offset, | |
745 | this_size, buffer); | |
746 | copy_bitwise (buffer, dest_offset_bits, | |
747 | contents, source_offset_bits, | |
748 | this_size_bits, | |
749 | bits_big_endian); | |
750 | } | |
751 | ||
63b4f126 | 752 | put_frame_register_bytes (frame, gdb_regnum, reg_offset, |
d3b1e874 | 753 | this_size, source_buffer); |
63b4f126 MGD |
754 | } |
755 | else | |
756 | { | |
757 | error (_("Unable to write to DWARF register number %s"), | |
f2c7657e | 758 | paddress (arch, p->v.value)); |
63b4f126 | 759 | } |
cec03d70 TT |
760 | } |
761 | break; | |
762 | case DWARF_VALUE_MEMORY: | |
d3b1e874 TT |
763 | if (need_bitwise) |
764 | { | |
765 | /* Only the first and last bytes can possibly have any | |
766 | bits reused. */ | |
f2c7657e UW |
767 | read_memory (p->v.mem.addr + dest_offset, buffer, 1); |
768 | read_memory (p->v.mem.addr + dest_offset + this_size - 1, | |
d3b1e874 TT |
769 | buffer + this_size - 1, 1); |
770 | copy_bitwise (buffer, dest_offset_bits, | |
771 | contents, source_offset_bits, | |
772 | this_size_bits, | |
773 | bits_big_endian); | |
774 | } | |
775 | ||
f2c7657e | 776 | write_memory (p->v.mem.addr + dest_offset, |
d3b1e874 | 777 | source_buffer, this_size); |
cec03d70 TT |
778 | break; |
779 | default: | |
780 | set_value_optimized_out (to, 1); | |
0e03807e | 781 | break; |
052b9502 | 782 | } |
d3b1e874 | 783 | offset += this_size_bits; |
052b9502 | 784 | } |
d3b1e874 | 785 | |
d3b1e874 | 786 | do_cleanups (cleanup); |
052b9502 NF |
787 | } |
788 | ||
0e03807e TT |
789 | static int |
790 | check_pieced_value_bits (const struct value *value, int bit_offset, | |
791 | int bit_length, int validity) | |
792 | { | |
793 | struct piece_closure *c | |
794 | = (struct piece_closure *) value_computed_closure (value); | |
795 | int i; | |
796 | ||
797 | bit_offset += 8 * value_offset (value); | |
798 | if (value_bitsize (value)) | |
799 | bit_offset += value_bitpos (value); | |
800 | ||
801 | for (i = 0; i < c->n_pieces && bit_length > 0; i++) | |
802 | { | |
803 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
804 | size_t this_size_bits = p->size; | |
805 | ||
806 | if (bit_offset > 0) | |
807 | { | |
808 | if (bit_offset >= this_size_bits) | |
809 | { | |
810 | bit_offset -= this_size_bits; | |
811 | continue; | |
812 | } | |
813 | ||
814 | bit_length -= this_size_bits - bit_offset; | |
815 | bit_offset = 0; | |
816 | } | |
817 | else | |
818 | bit_length -= this_size_bits; | |
819 | ||
820 | if (p->location == DWARF_VALUE_OPTIMIZED_OUT) | |
821 | { | |
822 | if (validity) | |
823 | return 0; | |
824 | } | |
825 | else | |
826 | { | |
827 | if (!validity) | |
828 | return 1; | |
829 | } | |
830 | } | |
831 | ||
832 | return validity; | |
833 | } | |
834 | ||
835 | static int | |
836 | check_pieced_value_validity (const struct value *value, int bit_offset, | |
837 | int bit_length) | |
838 | { | |
839 | return check_pieced_value_bits (value, bit_offset, bit_length, 1); | |
840 | } | |
841 | ||
842 | static int | |
843 | check_pieced_value_invalid (const struct value *value) | |
844 | { | |
845 | return check_pieced_value_bits (value, 0, | |
846 | 8 * TYPE_LENGTH (value_type (value)), 0); | |
847 | } | |
848 | ||
052b9502 | 849 | static void * |
0e03807e | 850 | copy_pieced_value_closure (const struct value *v) |
052b9502 NF |
851 | { |
852 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (v); | |
853 | ||
88bfdde4 TT |
854 | ++c->refc; |
855 | return c; | |
052b9502 NF |
856 | } |
857 | ||
858 | static void | |
859 | free_pieced_value_closure (struct value *v) | |
860 | { | |
861 | struct piece_closure *c = (struct piece_closure *) value_computed_closure (v); | |
862 | ||
88bfdde4 TT |
863 | --c->refc; |
864 | if (c->refc == 0) | |
865 | { | |
866 | xfree (c->pieces); | |
867 | xfree (c); | |
868 | } | |
052b9502 NF |
869 | } |
870 | ||
871 | /* Functions for accessing a variable described by DW_OP_piece. */ | |
872 | static struct lval_funcs pieced_value_funcs = { | |
873 | read_pieced_value, | |
874 | write_pieced_value, | |
0e03807e TT |
875 | check_pieced_value_validity, |
876 | check_pieced_value_invalid, | |
052b9502 NF |
877 | copy_pieced_value_closure, |
878 | free_pieced_value_closure | |
879 | }; | |
880 | ||
4c2df51b | 881 | /* Evaluate a location description, starting at DATA and with length |
a2d33775 | 882 | SIZE, to find the current location of variable of TYPE in the context |
4c2df51b | 883 | of FRAME. */ |
a2d33775 | 884 | |
4c2df51b | 885 | static struct value * |
a2d33775 | 886 | dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame, |
947bb88f | 887 | const gdb_byte *data, unsigned short size, |
ae0d2f24 | 888 | struct dwarf2_per_cu_data *per_cu) |
4c2df51b | 889 | { |
4c2df51b DJ |
890 | struct value *retval; |
891 | struct dwarf_expr_baton baton; | |
892 | struct dwarf_expr_context *ctx; | |
4a227398 | 893 | struct cleanup *old_chain; |
4c2df51b | 894 | |
0d53c4c4 DJ |
895 | if (size == 0) |
896 | { | |
a2d33775 | 897 | retval = allocate_value (type); |
0d53c4c4 | 898 | VALUE_LVAL (retval) = not_lval; |
feb13ab0 | 899 | set_value_optimized_out (retval, 1); |
10fb19b6 | 900 | return retval; |
0d53c4c4 DJ |
901 | } |
902 | ||
4c2df51b | 903 | baton.frame = frame; |
17ea53c3 | 904 | baton.per_cu = per_cu; |
4c2df51b DJ |
905 | |
906 | ctx = new_dwarf_expr_context (); | |
4a227398 TT |
907 | old_chain = make_cleanup_free_dwarf_expr_context (ctx); |
908 | ||
17ea53c3 | 909 | ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu)); |
ae0d2f24 | 910 | ctx->addr_size = dwarf2_per_cu_addr_size (per_cu); |
4c2df51b DJ |
911 | ctx->baton = &baton; |
912 | ctx->read_reg = dwarf_expr_read_reg; | |
913 | ctx->read_mem = dwarf_expr_read_mem; | |
914 | ctx->get_frame_base = dwarf_expr_frame_base; | |
e7802207 | 915 | ctx->get_frame_cfa = dwarf_expr_frame_cfa; |
4c2df51b | 916 | ctx->get_tls_address = dwarf_expr_tls_address; |
5c631832 | 917 | ctx->dwarf_call = dwarf_expr_dwarf_call; |
4c2df51b DJ |
918 | |
919 | dwarf_expr_eval (ctx, data, size); | |
87808bd6 JB |
920 | if (ctx->num_pieces > 0) |
921 | { | |
052b9502 NF |
922 | struct piece_closure *c; |
923 | struct frame_id frame_id = get_frame_id (frame); | |
924 | ||
6063c216 UW |
925 | c = allocate_piece_closure (ctx->num_pieces, ctx->pieces, |
926 | ctx->addr_size); | |
a2d33775 | 927 | retval = allocate_computed_value (type, &pieced_value_funcs, c); |
052b9502 | 928 | VALUE_FRAME_ID (retval) = frame_id; |
87808bd6 | 929 | } |
4c2df51b DJ |
930 | else |
931 | { | |
cec03d70 TT |
932 | switch (ctx->location) |
933 | { | |
934 | case DWARF_VALUE_REGISTER: | |
935 | { | |
936 | struct gdbarch *arch = get_frame_arch (frame); | |
f2c7657e | 937 | ULONGEST dwarf_regnum = dwarf_expr_fetch (ctx, 0); |
cec03d70 | 938 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum); |
9a619af0 | 939 | |
63b4f126 | 940 | if (gdb_regnum != -1) |
a2d33775 | 941 | retval = value_from_register (type, gdb_regnum, frame); |
63b4f126 | 942 | else |
a2d33775 JK |
943 | error (_("Unable to access DWARF register number %s"), |
944 | paddress (arch, dwarf_regnum)); | |
cec03d70 TT |
945 | } |
946 | break; | |
947 | ||
948 | case DWARF_VALUE_MEMORY: | |
949 | { | |
f2c7657e | 950 | CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0); |
44353522 | 951 | int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0); |
cec03d70 | 952 | |
a2d33775 | 953 | retval = allocate_value (type); |
cec03d70 TT |
954 | VALUE_LVAL (retval) = lval_memory; |
955 | set_value_lazy (retval, 1); | |
44353522 DE |
956 | if (in_stack_memory) |
957 | set_value_stack (retval, 1); | |
cec03d70 TT |
958 | set_value_address (retval, address); |
959 | } | |
960 | break; | |
961 | ||
962 | case DWARF_VALUE_STACK: | |
963 | { | |
f2c7657e | 964 | ULONGEST value = dwarf_expr_fetch (ctx, 0); |
cec03d70 TT |
965 | bfd_byte *contents; |
966 | size_t n = ctx->addr_size; | |
967 | ||
a2d33775 | 968 | retval = allocate_value (type); |
cec03d70 | 969 | contents = value_contents_raw (retval); |
a2d33775 JK |
970 | if (n > TYPE_LENGTH (type)) |
971 | n = TYPE_LENGTH (type); | |
05566b3b TT |
972 | store_unsigned_integer (contents, n, |
973 | gdbarch_byte_order (ctx->gdbarch), | |
974 | value); | |
cec03d70 TT |
975 | } |
976 | break; | |
977 | ||
978 | case DWARF_VALUE_LITERAL: | |
979 | { | |
980 | bfd_byte *contents; | |
981 | size_t n = ctx->len; | |
982 | ||
a2d33775 | 983 | retval = allocate_value (type); |
cec03d70 | 984 | contents = value_contents_raw (retval); |
a2d33775 JK |
985 | if (n > TYPE_LENGTH (type)) |
986 | n = TYPE_LENGTH (type); | |
cec03d70 TT |
987 | memcpy (contents, ctx->data, n); |
988 | } | |
989 | break; | |
990 | ||
cb826367 TT |
991 | /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context -- |
992 | it can only be encountered when making a piece. */ | |
993 | case DWARF_VALUE_OPTIMIZED_OUT: | |
cec03d70 TT |
994 | default: |
995 | internal_error (__FILE__, __LINE__, _("invalid location type")); | |
996 | } | |
4c2df51b DJ |
997 | } |
998 | ||
42be36b3 CT |
999 | set_value_initialized (retval, ctx->initialized); |
1000 | ||
4a227398 | 1001 | do_cleanups (old_chain); |
4c2df51b DJ |
1002 | |
1003 | return retval; | |
1004 | } | |
4c2df51b DJ |
1005 | \f |
1006 | /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */ | |
1007 | ||
1008 | struct needs_frame_baton | |
1009 | { | |
1010 | int needs_frame; | |
17ea53c3 | 1011 | struct dwarf2_per_cu_data *per_cu; |
4c2df51b DJ |
1012 | }; |
1013 | ||
1014 | /* Reads from registers do require a frame. */ | |
1015 | static CORE_ADDR | |
61fbb938 | 1016 | needs_frame_read_reg (void *baton, int regnum) |
4c2df51b DJ |
1017 | { |
1018 | struct needs_frame_baton *nf_baton = baton; | |
9a619af0 | 1019 | |
4c2df51b DJ |
1020 | nf_baton->needs_frame = 1; |
1021 | return 1; | |
1022 | } | |
1023 | ||
1024 | /* Reads from memory do not require a frame. */ | |
1025 | static void | |
852483bc | 1026 | needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len) |
4c2df51b DJ |
1027 | { |
1028 | memset (buf, 0, len); | |
1029 | } | |
1030 | ||
1031 | /* Frame-relative accesses do require a frame. */ | |
1032 | static void | |
0d45f56e | 1033 | needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length) |
4c2df51b | 1034 | { |
852483bc | 1035 | static gdb_byte lit0 = DW_OP_lit0; |
4c2df51b DJ |
1036 | struct needs_frame_baton *nf_baton = baton; |
1037 | ||
1038 | *start = &lit0; | |
1039 | *length = 1; | |
1040 | ||
1041 | nf_baton->needs_frame = 1; | |
1042 | } | |
1043 | ||
e7802207 TT |
1044 | /* CFA accesses require a frame. */ |
1045 | ||
1046 | static CORE_ADDR | |
1047 | needs_frame_frame_cfa (void *baton) | |
1048 | { | |
1049 | struct needs_frame_baton *nf_baton = baton; | |
9a619af0 | 1050 | |
e7802207 TT |
1051 | nf_baton->needs_frame = 1; |
1052 | return 1; | |
1053 | } | |
1054 | ||
4c2df51b DJ |
1055 | /* Thread-local accesses do require a frame. */ |
1056 | static CORE_ADDR | |
1057 | needs_frame_tls_address (void *baton, CORE_ADDR offset) | |
1058 | { | |
1059 | struct needs_frame_baton *nf_baton = baton; | |
9a619af0 | 1060 | |
4c2df51b DJ |
1061 | nf_baton->needs_frame = 1; |
1062 | return 1; | |
1063 | } | |
1064 | ||
5c631832 JK |
1065 | /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */ |
1066 | ||
1067 | static void | |
1068 | needs_frame_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset) | |
1069 | { | |
1070 | struct needs_frame_baton *nf_baton = ctx->baton; | |
1071 | ||
1072 | return per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu); | |
1073 | } | |
1074 | ||
4c2df51b DJ |
1075 | /* Return non-zero iff the location expression at DATA (length SIZE) |
1076 | requires a frame to evaluate. */ | |
1077 | ||
1078 | static int | |
947bb88f | 1079 | dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size, |
ae0d2f24 | 1080 | struct dwarf2_per_cu_data *per_cu) |
4c2df51b DJ |
1081 | { |
1082 | struct needs_frame_baton baton; | |
1083 | struct dwarf_expr_context *ctx; | |
f630a401 | 1084 | int in_reg; |
4a227398 | 1085 | struct cleanup *old_chain; |
4c2df51b DJ |
1086 | |
1087 | baton.needs_frame = 0; | |
17ea53c3 | 1088 | baton.per_cu = per_cu; |
4c2df51b DJ |
1089 | |
1090 | ctx = new_dwarf_expr_context (); | |
4a227398 TT |
1091 | old_chain = make_cleanup_free_dwarf_expr_context (ctx); |
1092 | ||
f7fd4728 | 1093 | ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu)); |
ae0d2f24 | 1094 | ctx->addr_size = dwarf2_per_cu_addr_size (per_cu); |
4c2df51b DJ |
1095 | ctx->baton = &baton; |
1096 | ctx->read_reg = needs_frame_read_reg; | |
1097 | ctx->read_mem = needs_frame_read_mem; | |
1098 | ctx->get_frame_base = needs_frame_frame_base; | |
e7802207 | 1099 | ctx->get_frame_cfa = needs_frame_frame_cfa; |
4c2df51b | 1100 | ctx->get_tls_address = needs_frame_tls_address; |
5c631832 | 1101 | ctx->dwarf_call = needs_frame_dwarf_call; |
4c2df51b DJ |
1102 | |
1103 | dwarf_expr_eval (ctx, data, size); | |
1104 | ||
cec03d70 | 1105 | in_reg = ctx->location == DWARF_VALUE_REGISTER; |
f630a401 | 1106 | |
87808bd6 JB |
1107 | if (ctx->num_pieces > 0) |
1108 | { | |
1109 | int i; | |
1110 | ||
1111 | /* If the location has several pieces, and any of them are in | |
1112 | registers, then we will need a frame to fetch them from. */ | |
1113 | for (i = 0; i < ctx->num_pieces; i++) | |
cec03d70 | 1114 | if (ctx->pieces[i].location == DWARF_VALUE_REGISTER) |
87808bd6 JB |
1115 | in_reg = 1; |
1116 | } | |
1117 | ||
4a227398 | 1118 | do_cleanups (old_chain); |
4c2df51b | 1119 | |
f630a401 | 1120 | return baton.needs_frame || in_reg; |
4c2df51b DJ |
1121 | } |
1122 | ||
3cf03773 TT |
1123 | /* A helper function that throws an unimplemented error mentioning a |
1124 | given DWARF operator. */ | |
1125 | ||
1126 | static void | |
1127 | unimplemented (unsigned int op) | |
0d53c4c4 | 1128 | { |
3cf03773 TT |
1129 | error (_("DWARF operator %s cannot be translated to an agent expression"), |
1130 | dwarf_stack_op_name (op, 1)); | |
1131 | } | |
08922a10 | 1132 | |
3cf03773 TT |
1133 | /* A helper function to convert a DWARF register to an arch register. |
1134 | ARCH is the architecture. | |
1135 | DWARF_REG is the register. | |
1136 | This will throw an exception if the DWARF register cannot be | |
1137 | translated to an architecture register. */ | |
08922a10 | 1138 | |
3cf03773 TT |
1139 | static int |
1140 | translate_register (struct gdbarch *arch, int dwarf_reg) | |
1141 | { | |
1142 | int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg); | |
1143 | if (reg == -1) | |
1144 | error (_("Unable to access DWARF register number %d"), dwarf_reg); | |
1145 | return reg; | |
1146 | } | |
08922a10 | 1147 | |
3cf03773 TT |
1148 | /* A helper function that emits an access to memory. ARCH is the |
1149 | target architecture. EXPR is the expression which we are building. | |
1150 | NBITS is the number of bits we want to read. This emits the | |
1151 | opcodes needed to read the memory and then extract the desired | |
1152 | bits. */ | |
08922a10 | 1153 | |
3cf03773 TT |
1154 | static void |
1155 | access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits) | |
08922a10 | 1156 | { |
3cf03773 TT |
1157 | ULONGEST nbytes = (nbits + 7) / 8; |
1158 | ||
1159 | gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST)); | |
1160 | ||
1161 | if (trace_kludge) | |
1162 | ax_trace_quick (expr, nbytes); | |
1163 | ||
1164 | if (nbits <= 8) | |
1165 | ax_simple (expr, aop_ref8); | |
1166 | else if (nbits <= 16) | |
1167 | ax_simple (expr, aop_ref16); | |
1168 | else if (nbits <= 32) | |
1169 | ax_simple (expr, aop_ref32); | |
1170 | else | |
1171 | ax_simple (expr, aop_ref64); | |
1172 | ||
1173 | /* If we read exactly the number of bytes we wanted, we're done. */ | |
1174 | if (8 * nbytes == nbits) | |
1175 | return; | |
1176 | ||
1177 | if (gdbarch_bits_big_endian (arch)) | |
0d53c4c4 | 1178 | { |
3cf03773 TT |
1179 | /* On a bits-big-endian machine, we want the high-order |
1180 | NBITS. */ | |
1181 | ax_const_l (expr, 8 * nbytes - nbits); | |
1182 | ax_simple (expr, aop_rsh_unsigned); | |
0d53c4c4 | 1183 | } |
3cf03773 | 1184 | else |
0d53c4c4 | 1185 | { |
3cf03773 TT |
1186 | /* On a bits-little-endian box, we want the low-order NBITS. */ |
1187 | ax_zero_ext (expr, nbits); | |
0d53c4c4 | 1188 | } |
3cf03773 | 1189 | } |
0936ad1d | 1190 | |
3cf03773 TT |
1191 | /* Compile a DWARF location expression to an agent expression. |
1192 | ||
1193 | EXPR is the agent expression we are building. | |
1194 | LOC is the agent value we modify. | |
1195 | ARCH is the architecture. | |
1196 | ADDR_SIZE is the size of addresses, in bytes. | |
1197 | OP_PTR is the start of the location expression. | |
1198 | OP_END is one past the last byte of the location expression. | |
1199 | ||
1200 | This will throw an exception for various kinds of errors -- for | |
1201 | example, if the expression cannot be compiled, or if the expression | |
1202 | is invalid. */ | |
0936ad1d | 1203 | |
3cf03773 TT |
1204 | static void |
1205 | compile_dwarf_to_ax (struct agent_expr *expr, struct axs_value *loc, | |
1206 | struct gdbarch *arch, unsigned int addr_size, | |
1207 | const gdb_byte *op_ptr, const gdb_byte *op_end, | |
1208 | struct dwarf2_per_cu_data *per_cu) | |
1209 | { | |
1210 | struct cleanup *cleanups; | |
1211 | int i, *offsets; | |
1212 | VEC(int) *dw_labels = NULL, *patches = NULL; | |
1213 | const gdb_byte * const base = op_ptr; | |
1214 | const gdb_byte *previous_piece = op_ptr; | |
1215 | enum bfd_endian byte_order = gdbarch_byte_order (arch); | |
1216 | ULONGEST bits_collected = 0; | |
1217 | unsigned int addr_size_bits = 8 * addr_size; | |
1218 | int bits_big_endian = gdbarch_bits_big_endian (arch); | |
0936ad1d | 1219 | |
3cf03773 TT |
1220 | offsets = xmalloc ((op_end - op_ptr) * sizeof (int)); |
1221 | cleanups = make_cleanup (xfree, offsets); | |
0936ad1d | 1222 | |
3cf03773 TT |
1223 | for (i = 0; i < op_end - op_ptr; ++i) |
1224 | offsets[i] = -1; | |
0936ad1d | 1225 | |
3cf03773 TT |
1226 | make_cleanup (VEC_cleanup (int), &dw_labels); |
1227 | make_cleanup (VEC_cleanup (int), &patches); | |
0936ad1d | 1228 | |
3cf03773 TT |
1229 | /* By default we are making an address. */ |
1230 | loc->kind = axs_lvalue_memory; | |
0d45f56e | 1231 | |
3cf03773 TT |
1232 | while (op_ptr < op_end) |
1233 | { | |
1234 | enum dwarf_location_atom op = *op_ptr; | |
3cf03773 TT |
1235 | ULONGEST uoffset, reg; |
1236 | LONGEST offset; | |
1237 | int i; | |
1238 | ||
1239 | offsets[op_ptr - base] = expr->len; | |
1240 | ++op_ptr; | |
1241 | ||
1242 | /* Our basic approach to code generation is to map DWARF | |
1243 | operations directly to AX operations. However, there are | |
1244 | some differences. | |
1245 | ||
1246 | First, DWARF works on address-sized units, but AX always uses | |
1247 | LONGEST. For most operations we simply ignore this | |
1248 | difference; instead we generate sign extensions as needed | |
1249 | before division and comparison operations. It would be nice | |
1250 | to omit the sign extensions, but there is no way to determine | |
1251 | the size of the target's LONGEST. (This code uses the size | |
1252 | of the host LONGEST in some cases -- that is a bug but it is | |
1253 | difficult to fix.) | |
1254 | ||
1255 | Second, some DWARF operations cannot be translated to AX. | |
1256 | For these we simply fail. See | |
1257 | http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */ | |
1258 | switch (op) | |
0936ad1d | 1259 | { |
3cf03773 TT |
1260 | case DW_OP_lit0: |
1261 | case DW_OP_lit1: | |
1262 | case DW_OP_lit2: | |
1263 | case DW_OP_lit3: | |
1264 | case DW_OP_lit4: | |
1265 | case DW_OP_lit5: | |
1266 | case DW_OP_lit6: | |
1267 | case DW_OP_lit7: | |
1268 | case DW_OP_lit8: | |
1269 | case DW_OP_lit9: | |
1270 | case DW_OP_lit10: | |
1271 | case DW_OP_lit11: | |
1272 | case DW_OP_lit12: | |
1273 | case DW_OP_lit13: | |
1274 | case DW_OP_lit14: | |
1275 | case DW_OP_lit15: | |
1276 | case DW_OP_lit16: | |
1277 | case DW_OP_lit17: | |
1278 | case DW_OP_lit18: | |
1279 | case DW_OP_lit19: | |
1280 | case DW_OP_lit20: | |
1281 | case DW_OP_lit21: | |
1282 | case DW_OP_lit22: | |
1283 | case DW_OP_lit23: | |
1284 | case DW_OP_lit24: | |
1285 | case DW_OP_lit25: | |
1286 | case DW_OP_lit26: | |
1287 | case DW_OP_lit27: | |
1288 | case DW_OP_lit28: | |
1289 | case DW_OP_lit29: | |
1290 | case DW_OP_lit30: | |
1291 | case DW_OP_lit31: | |
1292 | ax_const_l (expr, op - DW_OP_lit0); | |
1293 | break; | |
0d53c4c4 | 1294 | |
3cf03773 | 1295 | case DW_OP_addr: |
f2c7657e UW |
1296 | ax_const_l (expr, extract_unsigned_integer (op_ptr, |
1297 | addr_size, byte_order)); | |
3cf03773 TT |
1298 | op_ptr += addr_size; |
1299 | break; | |
4c2df51b | 1300 | |
3cf03773 TT |
1301 | case DW_OP_const1u: |
1302 | ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order)); | |
1303 | op_ptr += 1; | |
1304 | break; | |
1305 | case DW_OP_const1s: | |
1306 | ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order)); | |
1307 | op_ptr += 1; | |
1308 | break; | |
1309 | case DW_OP_const2u: | |
1310 | ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order)); | |
1311 | op_ptr += 2; | |
1312 | break; | |
1313 | case DW_OP_const2s: | |
1314 | ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order)); | |
1315 | op_ptr += 2; | |
1316 | break; | |
1317 | case DW_OP_const4u: | |
1318 | ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order)); | |
1319 | op_ptr += 4; | |
1320 | break; | |
1321 | case DW_OP_const4s: | |
1322 | ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order)); | |
1323 | op_ptr += 4; | |
1324 | break; | |
1325 | case DW_OP_const8u: | |
1326 | ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order)); | |
1327 | op_ptr += 8; | |
1328 | break; | |
1329 | case DW_OP_const8s: | |
1330 | ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order)); | |
1331 | op_ptr += 8; | |
1332 | break; | |
1333 | case DW_OP_constu: | |
1334 | op_ptr = read_uleb128 (op_ptr, op_end, &uoffset); | |
1335 | ax_const_l (expr, uoffset); | |
1336 | break; | |
1337 | case DW_OP_consts: | |
1338 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
1339 | ax_const_l (expr, offset); | |
1340 | break; | |
9c238357 | 1341 | |
3cf03773 TT |
1342 | case DW_OP_reg0: |
1343 | case DW_OP_reg1: | |
1344 | case DW_OP_reg2: | |
1345 | case DW_OP_reg3: | |
1346 | case DW_OP_reg4: | |
1347 | case DW_OP_reg5: | |
1348 | case DW_OP_reg6: | |
1349 | case DW_OP_reg7: | |
1350 | case DW_OP_reg8: | |
1351 | case DW_OP_reg9: | |
1352 | case DW_OP_reg10: | |
1353 | case DW_OP_reg11: | |
1354 | case DW_OP_reg12: | |
1355 | case DW_OP_reg13: | |
1356 | case DW_OP_reg14: | |
1357 | case DW_OP_reg15: | |
1358 | case DW_OP_reg16: | |
1359 | case DW_OP_reg17: | |
1360 | case DW_OP_reg18: | |
1361 | case DW_OP_reg19: | |
1362 | case DW_OP_reg20: | |
1363 | case DW_OP_reg21: | |
1364 | case DW_OP_reg22: | |
1365 | case DW_OP_reg23: | |
1366 | case DW_OP_reg24: | |
1367 | case DW_OP_reg25: | |
1368 | case DW_OP_reg26: | |
1369 | case DW_OP_reg27: | |
1370 | case DW_OP_reg28: | |
1371 | case DW_OP_reg29: | |
1372 | case DW_OP_reg30: | |
1373 | case DW_OP_reg31: | |
1374 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx"); | |
1375 | loc->u.reg = translate_register (arch, op - DW_OP_reg0); | |
1376 | loc->kind = axs_lvalue_register; | |
1377 | break; | |
9c238357 | 1378 | |
3cf03773 TT |
1379 | case DW_OP_regx: |
1380 | op_ptr = read_uleb128 (op_ptr, op_end, ®); | |
1381 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx"); | |
1382 | loc->u.reg = translate_register (arch, reg); | |
1383 | loc->kind = axs_lvalue_register; | |
1384 | break; | |
08922a10 | 1385 | |
3cf03773 TT |
1386 | case DW_OP_implicit_value: |
1387 | { | |
1388 | ULONGEST len; | |
1389 | ||
1390 | op_ptr = read_uleb128 (op_ptr, op_end, &len); | |
1391 | if (op_ptr + len > op_end) | |
1392 | error (_("DW_OP_implicit_value: too few bytes available.")); | |
1393 | if (len > sizeof (ULONGEST)) | |
1394 | error (_("Cannot translate DW_OP_implicit_value of %d bytes"), | |
1395 | (int) len); | |
1396 | ||
1397 | ax_const_l (expr, extract_unsigned_integer (op_ptr, len, | |
1398 | byte_order)); | |
1399 | op_ptr += len; | |
1400 | dwarf_expr_require_composition (op_ptr, op_end, | |
1401 | "DW_OP_implicit_value"); | |
1402 | ||
1403 | loc->kind = axs_rvalue; | |
1404 | } | |
1405 | break; | |
08922a10 | 1406 | |
3cf03773 TT |
1407 | case DW_OP_stack_value: |
1408 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value"); | |
1409 | loc->kind = axs_rvalue; | |
1410 | break; | |
08922a10 | 1411 | |
3cf03773 TT |
1412 | case DW_OP_breg0: |
1413 | case DW_OP_breg1: | |
1414 | case DW_OP_breg2: | |
1415 | case DW_OP_breg3: | |
1416 | case DW_OP_breg4: | |
1417 | case DW_OP_breg5: | |
1418 | case DW_OP_breg6: | |
1419 | case DW_OP_breg7: | |
1420 | case DW_OP_breg8: | |
1421 | case DW_OP_breg9: | |
1422 | case DW_OP_breg10: | |
1423 | case DW_OP_breg11: | |
1424 | case DW_OP_breg12: | |
1425 | case DW_OP_breg13: | |
1426 | case DW_OP_breg14: | |
1427 | case DW_OP_breg15: | |
1428 | case DW_OP_breg16: | |
1429 | case DW_OP_breg17: | |
1430 | case DW_OP_breg18: | |
1431 | case DW_OP_breg19: | |
1432 | case DW_OP_breg20: | |
1433 | case DW_OP_breg21: | |
1434 | case DW_OP_breg22: | |
1435 | case DW_OP_breg23: | |
1436 | case DW_OP_breg24: | |
1437 | case DW_OP_breg25: | |
1438 | case DW_OP_breg26: | |
1439 | case DW_OP_breg27: | |
1440 | case DW_OP_breg28: | |
1441 | case DW_OP_breg29: | |
1442 | case DW_OP_breg30: | |
1443 | case DW_OP_breg31: | |
1444 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
1445 | i = translate_register (arch, op - DW_OP_breg0); | |
1446 | ax_reg (expr, i); | |
1447 | if (offset != 0) | |
1448 | { | |
1449 | ax_const_l (expr, offset); | |
1450 | ax_simple (expr, aop_add); | |
1451 | } | |
1452 | break; | |
1453 | case DW_OP_bregx: | |
1454 | { | |
1455 | op_ptr = read_uleb128 (op_ptr, op_end, ®); | |
1456 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
1457 | i = translate_register (arch, reg); | |
1458 | ax_reg (expr, i); | |
1459 | if (offset != 0) | |
1460 | { | |
1461 | ax_const_l (expr, offset); | |
1462 | ax_simple (expr, aop_add); | |
1463 | } | |
1464 | } | |
1465 | break; | |
1466 | case DW_OP_fbreg: | |
1467 | { | |
1468 | const gdb_byte *datastart; | |
1469 | size_t datalen; | |
1470 | unsigned int before_stack_len; | |
1471 | struct block *b; | |
1472 | struct symbol *framefunc; | |
1473 | LONGEST base_offset = 0; | |
08922a10 | 1474 | |
3cf03773 TT |
1475 | b = block_for_pc (expr->scope); |
1476 | ||
1477 | if (!b) | |
1478 | error (_("No block found for address")); | |
1479 | ||
1480 | framefunc = block_linkage_function (b); | |
1481 | ||
1482 | if (!framefunc) | |
1483 | error (_("No function found for block")); | |
1484 | ||
1485 | dwarf_expr_frame_base_1 (framefunc, expr->scope, | |
1486 | &datastart, &datalen); | |
1487 | ||
1488 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
1489 | compile_dwarf_to_ax (expr, loc, arch, addr_size, datastart, | |
1490 | datastart + datalen, per_cu); | |
1491 | ||
1492 | if (offset != 0) | |
1493 | { | |
1494 | ax_const_l (expr, offset); | |
1495 | ax_simple (expr, aop_add); | |
1496 | } | |
1497 | ||
1498 | loc->kind = axs_lvalue_memory; | |
1499 | } | |
08922a10 | 1500 | break; |
08922a10 | 1501 | |
3cf03773 TT |
1502 | case DW_OP_dup: |
1503 | ax_simple (expr, aop_dup); | |
1504 | break; | |
08922a10 | 1505 | |
3cf03773 TT |
1506 | case DW_OP_drop: |
1507 | ax_simple (expr, aop_pop); | |
1508 | break; | |
08922a10 | 1509 | |
3cf03773 TT |
1510 | case DW_OP_pick: |
1511 | offset = *op_ptr++; | |
1512 | unimplemented (op); | |
1513 | break; | |
1514 | ||
1515 | case DW_OP_swap: | |
1516 | ax_simple (expr, aop_swap); | |
1517 | break; | |
08922a10 | 1518 | |
3cf03773 TT |
1519 | case DW_OP_over: |
1520 | /* We can't directly support DW_OP_over, but GCC emits it as | |
1521 | part of a sequence to implement signed modulus. As a | |
1522 | hack, we recognize this sequence. Note that if GCC ever | |
1523 | generates a branch to the middle of this sequence, then | |
1524 | we will die somehow. */ | |
1525 | if (op_end - op_ptr >= 4 | |
1526 | && op_ptr[0] == DW_OP_over | |
1527 | && op_ptr[1] == DW_OP_div | |
1528 | && op_ptr[2] == DW_OP_mul | |
1529 | && op_ptr[3] == DW_OP_minus) | |
1530 | { | |
1531 | /* Sign extend the operands. */ | |
1532 | ax_ext (expr, addr_size_bits); | |
1533 | ax_simple (expr, aop_swap); | |
1534 | ax_ext (expr, addr_size_bits); | |
1535 | ax_simple (expr, aop_swap); | |
1536 | ax_simple (expr, aop_rem_signed); | |
1537 | op_ptr += 4; | |
1538 | } | |
1539 | else | |
1540 | unimplemented (op); | |
1541 | break; | |
08922a10 | 1542 | |
3cf03773 TT |
1543 | case DW_OP_rot: |
1544 | unimplemented (op); | |
1545 | break; | |
08922a10 | 1546 | |
3cf03773 TT |
1547 | case DW_OP_deref: |
1548 | case DW_OP_deref_size: | |
1549 | { | |
1550 | int size; | |
08922a10 | 1551 | |
3cf03773 TT |
1552 | if (op == DW_OP_deref_size) |
1553 | size = *op_ptr++; | |
1554 | else | |
1555 | size = addr_size; | |
1556 | ||
1557 | switch (size) | |
1558 | { | |
1559 | case 8: | |
1560 | ax_simple (expr, aop_ref8); | |
1561 | break; | |
1562 | case 16: | |
1563 | ax_simple (expr, aop_ref16); | |
1564 | break; | |
1565 | case 32: | |
1566 | ax_simple (expr, aop_ref32); | |
1567 | break; | |
1568 | case 64: | |
1569 | ax_simple (expr, aop_ref64); | |
1570 | break; | |
1571 | default: | |
1572 | error (_("Unsupported size %d in %s"), | |
1573 | size, dwarf_stack_op_name (op, 1)); | |
1574 | } | |
1575 | } | |
1576 | break; | |
1577 | ||
1578 | case DW_OP_abs: | |
1579 | /* Sign extend the operand. */ | |
1580 | ax_ext (expr, addr_size_bits); | |
1581 | ax_simple (expr, aop_dup); | |
1582 | ax_const_l (expr, 0); | |
1583 | ax_simple (expr, aop_less_signed); | |
1584 | ax_simple (expr, aop_log_not); | |
1585 | i = ax_goto (expr, aop_if_goto); | |
1586 | /* We have to emit 0 - X. */ | |
1587 | ax_const_l (expr, 0); | |
1588 | ax_simple (expr, aop_swap); | |
1589 | ax_simple (expr, aop_sub); | |
1590 | ax_label (expr, i, expr->len); | |
1591 | break; | |
1592 | ||
1593 | case DW_OP_neg: | |
1594 | /* No need to sign extend here. */ | |
1595 | ax_const_l (expr, 0); | |
1596 | ax_simple (expr, aop_swap); | |
1597 | ax_simple (expr, aop_sub); | |
1598 | break; | |
1599 | ||
1600 | case DW_OP_not: | |
1601 | /* Sign extend the operand. */ | |
1602 | ax_ext (expr, addr_size_bits); | |
1603 | ax_simple (expr, aop_bit_not); | |
1604 | break; | |
1605 | ||
1606 | case DW_OP_plus_uconst: | |
1607 | op_ptr = read_uleb128 (op_ptr, op_end, ®); | |
1608 | /* It would be really weird to emit `DW_OP_plus_uconst 0', | |
1609 | but we micro-optimize anyhow. */ | |
1610 | if (reg != 0) | |
1611 | { | |
1612 | ax_const_l (expr, reg); | |
1613 | ax_simple (expr, aop_add); | |
1614 | } | |
1615 | break; | |
1616 | ||
1617 | case DW_OP_and: | |
1618 | ax_simple (expr, aop_bit_and); | |
1619 | break; | |
1620 | ||
1621 | case DW_OP_div: | |
1622 | /* Sign extend the operands. */ | |
1623 | ax_ext (expr, addr_size_bits); | |
1624 | ax_simple (expr, aop_swap); | |
1625 | ax_ext (expr, addr_size_bits); | |
1626 | ax_simple (expr, aop_swap); | |
1627 | ax_simple (expr, aop_div_signed); | |
08922a10 SS |
1628 | break; |
1629 | ||
3cf03773 TT |
1630 | case DW_OP_minus: |
1631 | ax_simple (expr, aop_sub); | |
1632 | break; | |
1633 | ||
1634 | case DW_OP_mod: | |
1635 | ax_simple (expr, aop_rem_unsigned); | |
1636 | break; | |
1637 | ||
1638 | case DW_OP_mul: | |
1639 | ax_simple (expr, aop_mul); | |
1640 | break; | |
1641 | ||
1642 | case DW_OP_or: | |
1643 | ax_simple (expr, aop_bit_or); | |
1644 | break; | |
1645 | ||
1646 | case DW_OP_plus: | |
1647 | ax_simple (expr, aop_add); | |
1648 | break; | |
1649 | ||
1650 | case DW_OP_shl: | |
1651 | ax_simple (expr, aop_lsh); | |
1652 | break; | |
1653 | ||
1654 | case DW_OP_shr: | |
1655 | ax_simple (expr, aop_rsh_unsigned); | |
1656 | break; | |
1657 | ||
1658 | case DW_OP_shra: | |
1659 | ax_simple (expr, aop_rsh_signed); | |
1660 | break; | |
1661 | ||
1662 | case DW_OP_xor: | |
1663 | ax_simple (expr, aop_bit_xor); | |
1664 | break; | |
1665 | ||
1666 | case DW_OP_le: | |
1667 | /* Sign extend the operands. */ | |
1668 | ax_ext (expr, addr_size_bits); | |
1669 | ax_simple (expr, aop_swap); | |
1670 | ax_ext (expr, addr_size_bits); | |
1671 | /* Note no swap here: A <= B is !(B < A). */ | |
1672 | ax_simple (expr, aop_less_signed); | |
1673 | ax_simple (expr, aop_log_not); | |
1674 | break; | |
1675 | ||
1676 | case DW_OP_ge: | |
1677 | /* Sign extend the operands. */ | |
1678 | ax_ext (expr, addr_size_bits); | |
1679 | ax_simple (expr, aop_swap); | |
1680 | ax_ext (expr, addr_size_bits); | |
1681 | ax_simple (expr, aop_swap); | |
1682 | /* A >= B is !(A < B). */ | |
1683 | ax_simple (expr, aop_less_signed); | |
1684 | ax_simple (expr, aop_log_not); | |
1685 | break; | |
1686 | ||
1687 | case DW_OP_eq: | |
1688 | /* Sign extend the operands. */ | |
1689 | ax_ext (expr, addr_size_bits); | |
1690 | ax_simple (expr, aop_swap); | |
1691 | ax_ext (expr, addr_size_bits); | |
1692 | /* No need for a second swap here. */ | |
1693 | ax_simple (expr, aop_equal); | |
1694 | break; | |
1695 | ||
1696 | case DW_OP_lt: | |
1697 | /* Sign extend the operands. */ | |
1698 | ax_ext (expr, addr_size_bits); | |
1699 | ax_simple (expr, aop_swap); | |
1700 | ax_ext (expr, addr_size_bits); | |
1701 | ax_simple (expr, aop_swap); | |
1702 | ax_simple (expr, aop_less_signed); | |
1703 | break; | |
1704 | ||
1705 | case DW_OP_gt: | |
1706 | /* Sign extend the operands. */ | |
1707 | ax_ext (expr, addr_size_bits); | |
1708 | ax_simple (expr, aop_swap); | |
1709 | ax_ext (expr, addr_size_bits); | |
1710 | /* Note no swap here: A > B is B < A. */ | |
1711 | ax_simple (expr, aop_less_signed); | |
1712 | break; | |
1713 | ||
1714 | case DW_OP_ne: | |
1715 | /* Sign extend the operands. */ | |
1716 | ax_ext (expr, addr_size_bits); | |
1717 | ax_simple (expr, aop_swap); | |
1718 | ax_ext (expr, addr_size_bits); | |
1719 | /* No need for a swap here. */ | |
1720 | ax_simple (expr, aop_equal); | |
1721 | ax_simple (expr, aop_log_not); | |
1722 | break; | |
1723 | ||
1724 | case DW_OP_call_frame_cfa: | |
1725 | unimplemented (op); | |
1726 | break; | |
1727 | ||
1728 | case DW_OP_GNU_push_tls_address: | |
1729 | unimplemented (op); | |
1730 | break; | |
1731 | ||
1732 | case DW_OP_skip: | |
1733 | offset = extract_signed_integer (op_ptr, 2, byte_order); | |
1734 | op_ptr += 2; | |
1735 | i = ax_goto (expr, aop_goto); | |
1736 | VEC_safe_push (int, dw_labels, op_ptr + offset - base); | |
1737 | VEC_safe_push (int, patches, i); | |
1738 | break; | |
1739 | ||
1740 | case DW_OP_bra: | |
1741 | offset = extract_signed_integer (op_ptr, 2, byte_order); | |
1742 | op_ptr += 2; | |
1743 | /* Zero extend the operand. */ | |
1744 | ax_zero_ext (expr, addr_size_bits); | |
1745 | i = ax_goto (expr, aop_if_goto); | |
1746 | VEC_safe_push (int, dw_labels, op_ptr + offset - base); | |
1747 | VEC_safe_push (int, patches, i); | |
1748 | break; | |
1749 | ||
1750 | case DW_OP_nop: | |
1751 | break; | |
1752 | ||
1753 | case DW_OP_piece: | |
1754 | case DW_OP_bit_piece: | |
08922a10 | 1755 | { |
3cf03773 TT |
1756 | ULONGEST size, offset; |
1757 | ||
1758 | if (op_ptr - 1 == previous_piece) | |
1759 | error (_("Cannot translate empty pieces to agent expressions")); | |
1760 | previous_piece = op_ptr - 1; | |
1761 | ||
1762 | op_ptr = read_uleb128 (op_ptr, op_end, &size); | |
1763 | if (op == DW_OP_piece) | |
1764 | { | |
1765 | size *= 8; | |
1766 | offset = 0; | |
1767 | } | |
1768 | else | |
1769 | op_ptr = read_uleb128 (op_ptr, op_end, &offset); | |
08922a10 | 1770 | |
3cf03773 TT |
1771 | if (bits_collected + size > 8 * sizeof (LONGEST)) |
1772 | error (_("Expression pieces exceed word size")); | |
1773 | ||
1774 | /* Access the bits. */ | |
1775 | switch (loc->kind) | |
1776 | { | |
1777 | case axs_lvalue_register: | |
1778 | ax_reg (expr, loc->u.reg); | |
1779 | break; | |
1780 | ||
1781 | case axs_lvalue_memory: | |
1782 | /* Offset the pointer, if needed. */ | |
1783 | if (offset > 8) | |
1784 | { | |
1785 | ax_const_l (expr, offset / 8); | |
1786 | ax_simple (expr, aop_add); | |
1787 | offset %= 8; | |
1788 | } | |
1789 | access_memory (arch, expr, size); | |
1790 | break; | |
1791 | } | |
1792 | ||
1793 | /* For a bits-big-endian target, shift up what we already | |
1794 | have. For a bits-little-endian target, shift up the | |
1795 | new data. Note that there is a potential bug here if | |
1796 | the DWARF expression leaves multiple values on the | |
1797 | stack. */ | |
1798 | if (bits_collected > 0) | |
1799 | { | |
1800 | if (bits_big_endian) | |
1801 | { | |
1802 | ax_simple (expr, aop_swap); | |
1803 | ax_const_l (expr, size); | |
1804 | ax_simple (expr, aop_lsh); | |
1805 | /* We don't need a second swap here, because | |
1806 | aop_bit_or is symmetric. */ | |
1807 | } | |
1808 | else | |
1809 | { | |
1810 | ax_const_l (expr, size); | |
1811 | ax_simple (expr, aop_lsh); | |
1812 | } | |
1813 | ax_simple (expr, aop_bit_or); | |
1814 | } | |
1815 | ||
1816 | bits_collected += size; | |
1817 | loc->kind = axs_rvalue; | |
08922a10 SS |
1818 | } |
1819 | break; | |
08922a10 | 1820 | |
3cf03773 TT |
1821 | case DW_OP_GNU_uninit: |
1822 | unimplemented (op); | |
1823 | ||
1824 | case DW_OP_call2: | |
1825 | case DW_OP_call4: | |
1826 | { | |
1827 | struct dwarf2_locexpr_baton block; | |
1828 | int size = (op == DW_OP_call2 ? 2 : 4); | |
1829 | ||
1830 | uoffset = extract_unsigned_integer (op_ptr, size, byte_order); | |
1831 | op_ptr += size; | |
1832 | ||
1833 | block = dwarf2_fetch_die_location_block (uoffset, per_cu); | |
1834 | ||
1835 | /* DW_OP_call_ref is currently not supported. */ | |
1836 | gdb_assert (block.per_cu == per_cu); | |
1837 | ||
1838 | compile_dwarf_to_ax (expr, loc, arch, addr_size, | |
1839 | block.data, block.data + block.size, | |
1840 | per_cu); | |
1841 | } | |
1842 | break; | |
1843 | ||
1844 | case DW_OP_call_ref: | |
1845 | unimplemented (op); | |
1846 | ||
1847 | default: | |
1848 | error (_("Unhandled dwarf expression opcode 0x%x"), op); | |
08922a10 | 1849 | } |
08922a10 | 1850 | } |
3cf03773 TT |
1851 | |
1852 | /* Patch all the branches we emitted. */ | |
1853 | for (i = 0; i < VEC_length (int, patches); ++i) | |
1854 | { | |
1855 | int targ = offsets[VEC_index (int, dw_labels, i)]; | |
1856 | if (targ == -1) | |
1857 | internal_error (__FILE__, __LINE__, _("invalid label")); | |
1858 | ax_label (expr, VEC_index (int, patches, i), targ); | |
1859 | } | |
1860 | ||
1861 | do_cleanups (cleanups); | |
08922a10 SS |
1862 | } |
1863 | ||
4c2df51b DJ |
1864 | \f |
1865 | /* Return the value of SYMBOL in FRAME using the DWARF-2 expression | |
1866 | evaluator to calculate the location. */ | |
1867 | static struct value * | |
1868 | locexpr_read_variable (struct symbol *symbol, struct frame_info *frame) | |
1869 | { | |
1870 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
1871 | struct value *val; | |
9a619af0 | 1872 | |
a2d33775 JK |
1873 | val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data, |
1874 | dlbaton->size, dlbaton->per_cu); | |
4c2df51b DJ |
1875 | |
1876 | return val; | |
1877 | } | |
1878 | ||
1879 | /* Return non-zero iff we need a frame to evaluate SYMBOL. */ | |
1880 | static int | |
1881 | locexpr_read_needs_frame (struct symbol *symbol) | |
1882 | { | |
1883 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
9a619af0 | 1884 | |
ae0d2f24 UW |
1885 | return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size, |
1886 | dlbaton->per_cu); | |
4c2df51b DJ |
1887 | } |
1888 | ||
9eae7c52 TT |
1889 | /* Return true if DATA points to the end of a piece. END is one past |
1890 | the last byte in the expression. */ | |
1891 | ||
1892 | static int | |
1893 | piece_end_p (const gdb_byte *data, const gdb_byte *end) | |
1894 | { | |
1895 | return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece; | |
1896 | } | |
1897 | ||
1898 | /* Nicely describe a single piece of a location, returning an updated | |
1899 | position in the bytecode sequence. This function cannot recognize | |
1900 | all locations; if a location is not recognized, it simply returns | |
1901 | DATA. */ | |
08922a10 | 1902 | |
0d45f56e | 1903 | static const gdb_byte * |
08922a10 SS |
1904 | locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream, |
1905 | CORE_ADDR addr, struct objfile *objfile, | |
9eae7c52 | 1906 | const gdb_byte *data, const gdb_byte *end, |
0d45f56e | 1907 | unsigned int addr_size) |
4c2df51b | 1908 | { |
08922a10 SS |
1909 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
1910 | int regno; | |
1911 | ||
1912 | if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31) | |
1913 | { | |
1914 | regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0); | |
1915 | fprintf_filtered (stream, _("a variable in $%s"), | |
1916 | gdbarch_register_name (gdbarch, regno)); | |
1917 | data += 1; | |
1918 | } | |
1919 | else if (data[0] == DW_OP_regx) | |
1920 | { | |
1921 | ULONGEST reg; | |
4c2df51b | 1922 | |
9eae7c52 | 1923 | data = read_uleb128 (data + 1, end, ®); |
08922a10 SS |
1924 | regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg); |
1925 | fprintf_filtered (stream, _("a variable in $%s"), | |
1926 | gdbarch_register_name (gdbarch, regno)); | |
1927 | } | |
1928 | else if (data[0] == DW_OP_fbreg) | |
4c2df51b | 1929 | { |
08922a10 SS |
1930 | struct block *b; |
1931 | struct symbol *framefunc; | |
1932 | int frame_reg = 0; | |
1933 | LONGEST frame_offset; | |
9eae7c52 | 1934 | const gdb_byte *base_data, *new_data; |
08922a10 SS |
1935 | size_t base_size; |
1936 | LONGEST base_offset = 0; | |
1937 | ||
9eae7c52 TT |
1938 | new_data = read_sleb128 (data + 1, end, &frame_offset); |
1939 | if (!piece_end_p (new_data, end)) | |
1940 | return data; | |
1941 | data = new_data; | |
1942 | ||
08922a10 SS |
1943 | b = block_for_pc (addr); |
1944 | ||
1945 | if (!b) | |
1946 | error (_("No block found for address for symbol \"%s\"."), | |
1947 | SYMBOL_PRINT_NAME (symbol)); | |
1948 | ||
1949 | framefunc = block_linkage_function (b); | |
1950 | ||
1951 | if (!framefunc) | |
1952 | error (_("No function found for block for symbol \"%s\"."), | |
1953 | SYMBOL_PRINT_NAME (symbol)); | |
1954 | ||
1955 | dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size); | |
1956 | ||
1957 | if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31) | |
1958 | { | |
0d45f56e | 1959 | const gdb_byte *buf_end; |
08922a10 SS |
1960 | |
1961 | frame_reg = base_data[0] - DW_OP_breg0; | |
1962 | buf_end = read_sleb128 (base_data + 1, | |
1963 | base_data + base_size, &base_offset); | |
1964 | if (buf_end != base_data + base_size) | |
1965 | error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."), | |
1966 | frame_reg, SYMBOL_PRINT_NAME (symbol)); | |
1967 | } | |
1968 | else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31) | |
1969 | { | |
1970 | /* The frame base is just the register, with no offset. */ | |
1971 | frame_reg = base_data[0] - DW_OP_reg0; | |
1972 | base_offset = 0; | |
1973 | } | |
1974 | else | |
1975 | { | |
1976 | /* We don't know what to do with the frame base expression, | |
1977 | so we can't trace this variable; give up. */ | |
1978 | error (_("Cannot describe location of symbol \"%s\"; " | |
1979 | "DWARF 2 encoding not handled, " | |
1980 | "first opcode in base data is 0x%x."), | |
1981 | SYMBOL_PRINT_NAME (symbol), base_data[0]); | |
1982 | } | |
1983 | ||
1984 | regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg); | |
1985 | ||
08922a10 SS |
1986 | fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"), |
1987 | gdbarch_register_name (gdbarch, regno), | |
1988 | plongest (base_offset), plongest (frame_offset)); | |
1989 | } | |
9eae7c52 TT |
1990 | else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31 |
1991 | && piece_end_p (data, end)) | |
08922a10 SS |
1992 | { |
1993 | LONGEST offset; | |
1994 | ||
1995 | regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0); | |
1996 | ||
9eae7c52 | 1997 | data = read_sleb128 (data + 1, end, &offset); |
08922a10 | 1998 | |
4c2df51b | 1999 | fprintf_filtered (stream, |
08922a10 SS |
2000 | _("a variable at offset %s from base reg $%s"), |
2001 | plongest (offset), | |
5e2b427d | 2002 | gdbarch_register_name (gdbarch, regno)); |
4c2df51b DJ |
2003 | } |
2004 | ||
c3228f12 EZ |
2005 | /* The location expression for a TLS variable looks like this (on a |
2006 | 64-bit LE machine): | |
2007 | ||
2008 | DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0 | |
2009 | (DW_OP_addr: 4; DW_OP_GNU_push_tls_address) | |
2010 | ||
2011 | 0x3 is the encoding for DW_OP_addr, which has an operand as long | |
2012 | as the size of an address on the target machine (here is 8 | |
2013 | bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address. | |
2014 | The operand represents the offset at which the variable is within | |
2015 | the thread local storage. */ | |
2016 | ||
9eae7c52 TT |
2017 | else if (data + 1 + addr_size < end |
2018 | && data[0] == DW_OP_addr | |
2019 | && data[1 + addr_size] == DW_OP_GNU_push_tls_address | |
2020 | && piece_end_p (data + 2 + addr_size, end)) | |
08922a10 | 2021 | { |
d4a087c7 UW |
2022 | ULONGEST offset; |
2023 | offset = extract_unsigned_integer (data + 1, addr_size, | |
2024 | gdbarch_byte_order (gdbarch)); | |
9a619af0 | 2025 | |
08922a10 | 2026 | fprintf_filtered (stream, |
d4a087c7 | 2027 | _("a thread-local variable at offset 0x%s " |
08922a10 | 2028 | "in the thread-local storage for `%s'"), |
d4a087c7 | 2029 | phex_nz (offset, addr_size), objfile->name); |
08922a10 SS |
2030 | |
2031 | data += 1 + addr_size + 1; | |
2032 | } | |
9eae7c52 TT |
2033 | else if (data[0] >= DW_OP_lit0 |
2034 | && data[0] <= DW_OP_lit31 | |
2035 | && data + 1 < end | |
2036 | && data[1] == DW_OP_stack_value) | |
2037 | { | |
2038 | fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0); | |
2039 | data += 2; | |
2040 | } | |
2041 | ||
2042 | return data; | |
2043 | } | |
2044 | ||
2045 | /* Disassemble an expression, stopping at the end of a piece or at the | |
2046 | end of the expression. Returns a pointer to the next unread byte | |
2047 | in the input expression. If ALL is nonzero, then this function | |
2048 | will keep going until it reaches the end of the expression. */ | |
2049 | ||
2050 | static const gdb_byte * | |
2051 | disassemble_dwarf_expression (struct ui_file *stream, | |
2052 | struct gdbarch *arch, unsigned int addr_size, | |
2053 | int offset_size, | |
2054 | const gdb_byte *data, const gdb_byte *end, | |
2055 | int all) | |
2056 | { | |
2057 | const gdb_byte *start = data; | |
2058 | ||
2059 | fprintf_filtered (stream, _("a complex DWARF expression:\n")); | |
2060 | ||
2061 | while (data < end | |
2062 | && (all | |
2063 | || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece))) | |
2064 | { | |
2065 | enum dwarf_location_atom op = *data++; | |
9eae7c52 TT |
2066 | ULONGEST ul; |
2067 | LONGEST l; | |
2068 | const char *name; | |
2069 | ||
2070 | name = dwarf_stack_op_name (op, 0); | |
2071 | ||
2072 | if (!name) | |
2073 | error (_("Unrecognized DWARF opcode 0x%02x at %ld"), | |
2074 | op, (long) (data - start)); | |
2075 | fprintf_filtered (stream, " % 4ld: %s", (long) (data - start), name); | |
2076 | ||
2077 | switch (op) | |
2078 | { | |
2079 | case DW_OP_addr: | |
d4a087c7 UW |
2080 | ul = extract_unsigned_integer (data, addr_size, |
2081 | gdbarch_byte_order (arch)); | |
9eae7c52 | 2082 | data += addr_size; |
d4a087c7 | 2083 | fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size)); |
9eae7c52 TT |
2084 | break; |
2085 | ||
2086 | case DW_OP_const1u: | |
2087 | ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch)); | |
2088 | data += 1; | |
2089 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
2090 | break; | |
2091 | case DW_OP_const1s: | |
2092 | l = extract_signed_integer (data, 1, gdbarch_byte_order (arch)); | |
2093 | data += 1; | |
2094 | fprintf_filtered (stream, " %s", plongest (l)); | |
2095 | break; | |
2096 | case DW_OP_const2u: | |
2097 | ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch)); | |
2098 | data += 2; | |
2099 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
2100 | break; | |
2101 | case DW_OP_const2s: | |
2102 | l = extract_signed_integer (data, 2, gdbarch_byte_order (arch)); | |
2103 | data += 2; | |
2104 | fprintf_filtered (stream, " %s", plongest (l)); | |
2105 | break; | |
2106 | case DW_OP_const4u: | |
2107 | ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch)); | |
2108 | data += 4; | |
2109 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
2110 | break; | |
2111 | case DW_OP_const4s: | |
2112 | l = extract_signed_integer (data, 4, gdbarch_byte_order (arch)); | |
2113 | data += 4; | |
2114 | fprintf_filtered (stream, " %s", plongest (l)); | |
2115 | break; | |
2116 | case DW_OP_const8u: | |
2117 | ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch)); | |
2118 | data += 8; | |
2119 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
2120 | break; | |
2121 | case DW_OP_const8s: | |
2122 | l = extract_signed_integer (data, 8, gdbarch_byte_order (arch)); | |
2123 | data += 8; | |
2124 | fprintf_filtered (stream, " %s", plongest (l)); | |
2125 | break; | |
2126 | case DW_OP_constu: | |
2127 | data = read_uleb128 (data, end, &ul); | |
2128 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
2129 | break; | |
2130 | case DW_OP_consts: | |
44b5680a | 2131 | data = read_sleb128 (data, end, &l); |
9eae7c52 TT |
2132 | fprintf_filtered (stream, " %s", plongest (l)); |
2133 | break; | |
2134 | ||
2135 | case DW_OP_reg0: | |
2136 | case DW_OP_reg1: | |
2137 | case DW_OP_reg2: | |
2138 | case DW_OP_reg3: | |
2139 | case DW_OP_reg4: | |
2140 | case DW_OP_reg5: | |
2141 | case DW_OP_reg6: | |
2142 | case DW_OP_reg7: | |
2143 | case DW_OP_reg8: | |
2144 | case DW_OP_reg9: | |
2145 | case DW_OP_reg10: | |
2146 | case DW_OP_reg11: | |
2147 | case DW_OP_reg12: | |
2148 | case DW_OP_reg13: | |
2149 | case DW_OP_reg14: | |
2150 | case DW_OP_reg15: | |
2151 | case DW_OP_reg16: | |
2152 | case DW_OP_reg17: | |
2153 | case DW_OP_reg18: | |
2154 | case DW_OP_reg19: | |
2155 | case DW_OP_reg20: | |
2156 | case DW_OP_reg21: | |
2157 | case DW_OP_reg22: | |
2158 | case DW_OP_reg23: | |
2159 | case DW_OP_reg24: | |
2160 | case DW_OP_reg25: | |
2161 | case DW_OP_reg26: | |
2162 | case DW_OP_reg27: | |
2163 | case DW_OP_reg28: | |
2164 | case DW_OP_reg29: | |
2165 | case DW_OP_reg30: | |
2166 | case DW_OP_reg31: | |
2167 | fprintf_filtered (stream, " [$%s]", | |
2168 | gdbarch_register_name (arch, op - DW_OP_reg0)); | |
2169 | break; | |
2170 | ||
2171 | case DW_OP_regx: | |
2172 | data = read_uleb128 (data, end, &ul); | |
2173 | fprintf_filtered (stream, " %s [$%s]", pulongest (ul), | |
2174 | gdbarch_register_name (arch, (int) ul)); | |
2175 | break; | |
2176 | ||
2177 | case DW_OP_implicit_value: | |
2178 | data = read_uleb128 (data, end, &ul); | |
2179 | data += ul; | |
2180 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
2181 | break; | |
2182 | ||
2183 | case DW_OP_breg0: | |
2184 | case DW_OP_breg1: | |
2185 | case DW_OP_breg2: | |
2186 | case DW_OP_breg3: | |
2187 | case DW_OP_breg4: | |
2188 | case DW_OP_breg5: | |
2189 | case DW_OP_breg6: | |
2190 | case DW_OP_breg7: | |
2191 | case DW_OP_breg8: | |
2192 | case DW_OP_breg9: | |
2193 | case DW_OP_breg10: | |
2194 | case DW_OP_breg11: | |
2195 | case DW_OP_breg12: | |
2196 | case DW_OP_breg13: | |
2197 | case DW_OP_breg14: | |
2198 | case DW_OP_breg15: | |
2199 | case DW_OP_breg16: | |
2200 | case DW_OP_breg17: | |
2201 | case DW_OP_breg18: | |
2202 | case DW_OP_breg19: | |
2203 | case DW_OP_breg20: | |
2204 | case DW_OP_breg21: | |
2205 | case DW_OP_breg22: | |
2206 | case DW_OP_breg23: | |
2207 | case DW_OP_breg24: | |
2208 | case DW_OP_breg25: | |
2209 | case DW_OP_breg26: | |
2210 | case DW_OP_breg27: | |
2211 | case DW_OP_breg28: | |
2212 | case DW_OP_breg29: | |
2213 | case DW_OP_breg30: | |
2214 | case DW_OP_breg31: | |
2215 | data = read_sleb128 (data, end, &ul); | |
2216 | fprintf_filtered (stream, " %s [$%s]", pulongest (ul), | |
2217 | gdbarch_register_name (arch, op - DW_OP_breg0)); | |
2218 | break; | |
2219 | ||
2220 | case DW_OP_bregx: | |
2221 | { | |
2222 | ULONGEST offset; | |
2223 | ||
2224 | data = read_uleb128 (data, end, &ul); | |
2225 | data = read_sleb128 (data, end, &offset); | |
2226 | fprintf_filtered (stream, " register %s [$%s] offset %s", | |
2227 | pulongest (ul), | |
2228 | gdbarch_register_name (arch, (int) ul), | |
2229 | pulongest (offset)); | |
2230 | } | |
2231 | break; | |
2232 | ||
2233 | case DW_OP_fbreg: | |
2234 | data = read_sleb128 (data, end, &ul); | |
2235 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
2236 | break; | |
2237 | ||
2238 | case DW_OP_xderef_size: | |
2239 | case DW_OP_deref_size: | |
2240 | case DW_OP_pick: | |
2241 | fprintf_filtered (stream, " %d", *data); | |
2242 | ++data; | |
2243 | break; | |
2244 | ||
2245 | case DW_OP_plus_uconst: | |
2246 | data = read_uleb128 (data, end, &ul); | |
2247 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
2248 | break; | |
2249 | ||
2250 | case DW_OP_skip: | |
2251 | l = extract_signed_integer (data, 2, gdbarch_byte_order (arch)); | |
2252 | data += 2; | |
2253 | fprintf_filtered (stream, " to %ld", | |
2254 | (long) (data + l - start)); | |
2255 | break; | |
2256 | ||
2257 | case DW_OP_bra: | |
2258 | l = extract_signed_integer (data, 2, gdbarch_byte_order (arch)); | |
2259 | data += 2; | |
2260 | fprintf_filtered (stream, " %ld", | |
2261 | (long) (data + l - start)); | |
2262 | break; | |
2263 | ||
2264 | case DW_OP_call2: | |
2265 | ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch)); | |
2266 | data += 2; | |
2267 | fprintf_filtered (stream, " offset %s", phex_nz (ul, 2)); | |
2268 | break; | |
2269 | ||
2270 | case DW_OP_call4: | |
2271 | ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch)); | |
2272 | data += 4; | |
2273 | fprintf_filtered (stream, " offset %s", phex_nz (ul, 4)); | |
2274 | break; | |
2275 | ||
2276 | case DW_OP_call_ref: | |
2277 | ul = extract_unsigned_integer (data, offset_size, | |
2278 | gdbarch_byte_order (arch)); | |
2279 | data += offset_size; | |
2280 | fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size)); | |
2281 | break; | |
2282 | ||
2283 | case DW_OP_piece: | |
2284 | data = read_uleb128 (data, end, &ul); | |
2285 | fprintf_filtered (stream, " %s (bytes)", pulongest (ul)); | |
2286 | break; | |
2287 | ||
2288 | case DW_OP_bit_piece: | |
2289 | { | |
2290 | ULONGEST offset; | |
2291 | ||
2292 | data = read_uleb128 (data, end, &ul); | |
2293 | data = read_uleb128 (data, end, &offset); | |
2294 | fprintf_filtered (stream, " size %s offset %s (bits)", | |
2295 | pulongest (ul), pulongest (offset)); | |
2296 | } | |
2297 | break; | |
2298 | } | |
2299 | ||
2300 | fprintf_filtered (stream, "\n"); | |
2301 | } | |
c3228f12 | 2302 | |
08922a10 | 2303 | return data; |
4c2df51b DJ |
2304 | } |
2305 | ||
08922a10 SS |
2306 | /* Describe a single location, which may in turn consist of multiple |
2307 | pieces. */ | |
a55cc764 | 2308 | |
08922a10 SS |
2309 | static void |
2310 | locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr, | |
0d45f56e TT |
2311 | struct ui_file *stream, |
2312 | const gdb_byte *data, int size, | |
9eae7c52 TT |
2313 | struct objfile *objfile, unsigned int addr_size, |
2314 | int offset_size) | |
08922a10 | 2315 | { |
0d45f56e | 2316 | const gdb_byte *end = data + size; |
9eae7c52 | 2317 | int first_piece = 1, bad = 0; |
08922a10 | 2318 | |
08922a10 SS |
2319 | while (data < end) |
2320 | { | |
9eae7c52 TT |
2321 | const gdb_byte *here = data; |
2322 | int disassemble = 1; | |
2323 | ||
2324 | if (first_piece) | |
2325 | first_piece = 0; | |
2326 | else | |
2327 | fprintf_filtered (stream, _(", and ")); | |
08922a10 | 2328 | |
9eae7c52 TT |
2329 | if (!dwarf2_always_disassemble) |
2330 | { | |
08922a10 | 2331 | data = locexpr_describe_location_piece (symbol, stream, addr, objfile, |
9eae7c52 TT |
2332 | data, end, addr_size); |
2333 | /* If we printed anything, or if we have an empty piece, | |
2334 | then don't disassemble. */ | |
2335 | if (data != here | |
2336 | || data[0] == DW_OP_piece | |
2337 | || data[0] == DW_OP_bit_piece) | |
2338 | disassemble = 0; | |
08922a10 | 2339 | } |
9eae7c52 TT |
2340 | if (disassemble) |
2341 | data = disassemble_dwarf_expression (stream, get_objfile_arch (objfile), | |
2342 | addr_size, offset_size, data, end, | |
2343 | dwarf2_always_disassemble); | |
2344 | ||
2345 | if (data < end) | |
08922a10 | 2346 | { |
9eae7c52 | 2347 | int empty = data == here; |
08922a10 | 2348 | |
9eae7c52 TT |
2349 | if (disassemble) |
2350 | fprintf_filtered (stream, " "); | |
2351 | if (data[0] == DW_OP_piece) | |
2352 | { | |
2353 | ULONGEST bytes; | |
08922a10 | 2354 | |
9eae7c52 | 2355 | data = read_uleb128 (data + 1, end, &bytes); |
08922a10 | 2356 | |
9eae7c52 TT |
2357 | if (empty) |
2358 | fprintf_filtered (stream, _("an empty %s-byte piece"), | |
2359 | pulongest (bytes)); | |
2360 | else | |
2361 | fprintf_filtered (stream, _(" [%s-byte piece]"), | |
2362 | pulongest (bytes)); | |
2363 | } | |
2364 | else if (data[0] == DW_OP_bit_piece) | |
2365 | { | |
2366 | ULONGEST bits, offset; | |
2367 | ||
2368 | data = read_uleb128 (data + 1, end, &bits); | |
2369 | data = read_uleb128 (data, end, &offset); | |
2370 | ||
2371 | if (empty) | |
2372 | fprintf_filtered (stream, | |
2373 | _("an empty %s-bit piece"), | |
2374 | pulongest (bits)); | |
2375 | else | |
2376 | fprintf_filtered (stream, | |
2377 | _(" [%s-bit piece, offset %s bits]"), | |
2378 | pulongest (bits), pulongest (offset)); | |
2379 | } | |
2380 | else | |
2381 | { | |
2382 | bad = 1; | |
2383 | break; | |
2384 | } | |
08922a10 SS |
2385 | } |
2386 | } | |
2387 | ||
2388 | if (bad || data > end) | |
2389 | error (_("Corrupted DWARF2 expression for \"%s\"."), | |
2390 | SYMBOL_PRINT_NAME (symbol)); | |
2391 | } | |
2392 | ||
2393 | /* Print a natural-language description of SYMBOL to STREAM. This | |
2394 | version is for a symbol with a single location. */ | |
a55cc764 | 2395 | |
08922a10 SS |
2396 | static void |
2397 | locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr, | |
2398 | struct ui_file *stream) | |
2399 | { | |
2400 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
2401 | struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu); | |
2402 | unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu); | |
9eae7c52 | 2403 | int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu); |
08922a10 SS |
2404 | |
2405 | locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size, | |
9eae7c52 | 2406 | objfile, addr_size, offset_size); |
08922a10 SS |
2407 | } |
2408 | ||
2409 | /* Describe the location of SYMBOL as an agent value in VALUE, generating | |
2410 | any necessary bytecode in AX. */ | |
a55cc764 | 2411 | |
0d53c4c4 | 2412 | static void |
505e835d UW |
2413 | locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch, |
2414 | struct agent_expr *ax, struct axs_value *value) | |
a55cc764 DJ |
2415 | { |
2416 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
3cf03773 | 2417 | unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu); |
a55cc764 | 2418 | |
3cf03773 TT |
2419 | compile_dwarf_to_ax (ax, value, gdbarch, addr_size, |
2420 | dlbaton->data, dlbaton->data + dlbaton->size, | |
2421 | dlbaton->per_cu); | |
a55cc764 DJ |
2422 | } |
2423 | ||
4c2df51b DJ |
2424 | /* The set of location functions used with the DWARF-2 expression |
2425 | evaluator. */ | |
768a979c | 2426 | const struct symbol_computed_ops dwarf2_locexpr_funcs = { |
4c2df51b DJ |
2427 | locexpr_read_variable, |
2428 | locexpr_read_needs_frame, | |
2429 | locexpr_describe_location, | |
a55cc764 | 2430 | locexpr_tracepoint_var_ref |
4c2df51b | 2431 | }; |
0d53c4c4 DJ |
2432 | |
2433 | ||
2434 | /* Wrapper functions for location lists. These generally find | |
2435 | the appropriate location expression and call something above. */ | |
2436 | ||
2437 | /* Return the value of SYMBOL in FRAME using the DWARF-2 expression | |
2438 | evaluator to calculate the location. */ | |
2439 | static struct value * | |
2440 | loclist_read_variable (struct symbol *symbol, struct frame_info *frame) | |
2441 | { | |
2442 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
2443 | struct value *val; | |
947bb88f | 2444 | const gdb_byte *data; |
b6b08ebf | 2445 | size_t size; |
0d53c4c4 DJ |
2446 | |
2447 | data = find_location_expression (dlbaton, &size, | |
22c6caba JW |
2448 | frame ? get_frame_address_in_block (frame) |
2449 | : 0); | |
0d53c4c4 | 2450 | if (data == NULL) |
806048c6 DJ |
2451 | { |
2452 | val = allocate_value (SYMBOL_TYPE (symbol)); | |
2453 | VALUE_LVAL (val) = not_lval; | |
2454 | set_value_optimized_out (val, 1); | |
2455 | } | |
2456 | else | |
a2d33775 | 2457 | val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size, |
ae0d2f24 | 2458 | dlbaton->per_cu); |
0d53c4c4 DJ |
2459 | |
2460 | return val; | |
2461 | } | |
2462 | ||
2463 | /* Return non-zero iff we need a frame to evaluate SYMBOL. */ | |
2464 | static int | |
2465 | loclist_read_needs_frame (struct symbol *symbol) | |
2466 | { | |
2467 | /* If there's a location list, then assume we need to have a frame | |
2468 | to choose the appropriate location expression. With tracking of | |
2469 | global variables this is not necessarily true, but such tracking | |
2470 | is disabled in GCC at the moment until we figure out how to | |
2471 | represent it. */ | |
2472 | ||
2473 | return 1; | |
2474 | } | |
2475 | ||
08922a10 SS |
2476 | /* Print a natural-language description of SYMBOL to STREAM. This |
2477 | version applies when there is a list of different locations, each | |
2478 | with a specified address range. */ | |
2479 | ||
2480 | static void | |
2481 | loclist_describe_location (struct symbol *symbol, CORE_ADDR addr, | |
2482 | struct ui_file *stream) | |
0d53c4c4 | 2483 | { |
08922a10 SS |
2484 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); |
2485 | CORE_ADDR low, high; | |
947bb88f | 2486 | const gdb_byte *loc_ptr, *buf_end; |
08922a10 SS |
2487 | int length, first = 1; |
2488 | struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu); | |
2489 | struct gdbarch *gdbarch = get_objfile_arch (objfile); | |
2490 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
2491 | unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu); | |
9eae7c52 | 2492 | int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu); |
d4a087c7 | 2493 | int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd); |
08922a10 SS |
2494 | CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1)); |
2495 | /* Adjust base_address for relocatable objects. */ | |
2496 | CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets, | |
2497 | SECT_OFF_TEXT (objfile)); | |
2498 | CORE_ADDR base_address = dlbaton->base_address + base_offset; | |
2499 | ||
2500 | loc_ptr = dlbaton->data; | |
2501 | buf_end = dlbaton->data + dlbaton->size; | |
2502 | ||
9eae7c52 | 2503 | fprintf_filtered (stream, _("multi-location:\n")); |
08922a10 SS |
2504 | |
2505 | /* Iterate through locations until we run out. */ | |
2506 | while (1) | |
2507 | { | |
2508 | if (buf_end - loc_ptr < 2 * addr_size) | |
2509 | error (_("Corrupted DWARF expression for symbol \"%s\"."), | |
2510 | SYMBOL_PRINT_NAME (symbol)); | |
2511 | ||
d4a087c7 UW |
2512 | if (signed_addr_p) |
2513 | low = extract_signed_integer (loc_ptr, addr_size, byte_order); | |
2514 | else | |
2515 | low = extract_unsigned_integer (loc_ptr, addr_size, byte_order); | |
2516 | loc_ptr += addr_size; | |
2517 | ||
2518 | if (signed_addr_p) | |
2519 | high = extract_signed_integer (loc_ptr, addr_size, byte_order); | |
2520 | else | |
2521 | high = extract_unsigned_integer (loc_ptr, addr_size, byte_order); | |
08922a10 SS |
2522 | loc_ptr += addr_size; |
2523 | ||
2524 | /* A base-address-selection entry. */ | |
d4a087c7 | 2525 | if ((low & base_mask) == base_mask) |
08922a10 | 2526 | { |
d4a087c7 | 2527 | base_address = high + base_offset; |
9eae7c52 | 2528 | fprintf_filtered (stream, _(" Base address %s"), |
08922a10 | 2529 | paddress (gdbarch, base_address)); |
08922a10 SS |
2530 | continue; |
2531 | } | |
2532 | ||
08922a10 SS |
2533 | /* An end-of-list entry. */ |
2534 | if (low == 0 && high == 0) | |
9eae7c52 | 2535 | break; |
08922a10 SS |
2536 | |
2537 | /* Otherwise, a location expression entry. */ | |
2538 | low += base_address; | |
2539 | high += base_address; | |
2540 | ||
2541 | length = extract_unsigned_integer (loc_ptr, 2, byte_order); | |
2542 | loc_ptr += 2; | |
2543 | ||
08922a10 SS |
2544 | /* (It would improve readability to print only the minimum |
2545 | necessary digits of the second number of the range.) */ | |
9eae7c52 | 2546 | fprintf_filtered (stream, _(" Range %s-%s: "), |
08922a10 SS |
2547 | paddress (gdbarch, low), paddress (gdbarch, high)); |
2548 | ||
2549 | /* Now describe this particular location. */ | |
2550 | locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length, | |
9eae7c52 TT |
2551 | objfile, addr_size, offset_size); |
2552 | ||
2553 | fprintf_filtered (stream, "\n"); | |
08922a10 SS |
2554 | |
2555 | loc_ptr += length; | |
2556 | } | |
0d53c4c4 DJ |
2557 | } |
2558 | ||
2559 | /* Describe the location of SYMBOL as an agent value in VALUE, generating | |
2560 | any necessary bytecode in AX. */ | |
2561 | static void | |
505e835d UW |
2562 | loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch, |
2563 | struct agent_expr *ax, struct axs_value *value) | |
0d53c4c4 DJ |
2564 | { |
2565 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
947bb88f | 2566 | const gdb_byte *data; |
b6b08ebf | 2567 | size_t size; |
3cf03773 | 2568 | unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu); |
0d53c4c4 DJ |
2569 | |
2570 | data = find_location_expression (dlbaton, &size, ax->scope); | |
0d53c4c4 | 2571 | |
3cf03773 TT |
2572 | compile_dwarf_to_ax (ax, value, gdbarch, addr_size, data, data + size, |
2573 | dlbaton->per_cu); | |
0d53c4c4 DJ |
2574 | } |
2575 | ||
2576 | /* The set of location functions used with the DWARF-2 expression | |
2577 | evaluator and location lists. */ | |
768a979c | 2578 | const struct symbol_computed_ops dwarf2_loclist_funcs = { |
0d53c4c4 DJ |
2579 | loclist_read_variable, |
2580 | loclist_read_needs_frame, | |
2581 | loclist_describe_location, | |
2582 | loclist_tracepoint_var_ref | |
2583 | }; |