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