Commit | Line | Data |
---|---|---|
852483bc MK |
1 | /* DWARF 2 Expression Evaluator. |
2 | ||
61baf725 | 3 | Copyright (C) 2001-2017 Free Software Foundation, Inc. |
852483bc | 4 | |
4c2df51b DJ |
5 | Contributed by Daniel Berlin (dan@dberlin.org) |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
4c2df51b DJ |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4c2df51b DJ |
21 | |
22 | #include "defs.h" | |
23 | #include "symtab.h" | |
24 | #include "gdbtypes.h" | |
25 | #include "value.h" | |
26 | #include "gdbcore.h" | |
fa8f86ff | 27 | #include "dwarf2.h" |
4c2df51b | 28 | #include "dwarf2expr.h" |
0fde2c53 | 29 | #include "dwarf2loc.h" |
9c541725 | 30 | #include "common/underlying.h" |
4c2df51b | 31 | |
8a9b8146 TT |
32 | /* Cookie for gdbarch data. */ |
33 | ||
34 | static struct gdbarch_data *dwarf_arch_cookie; | |
35 | ||
36 | /* This holds gdbarch-specific types used by the DWARF expression | |
37 | evaluator. See comments in execute_stack_op. */ | |
38 | ||
39 | struct dwarf_gdbarch_types | |
40 | { | |
41 | struct type *dw_types[3]; | |
42 | }; | |
43 | ||
44 | /* Allocate and fill in dwarf_gdbarch_types for an arch. */ | |
45 | ||
46 | static void * | |
47 | dwarf_gdbarch_types_init (struct gdbarch *gdbarch) | |
48 | { | |
49 | struct dwarf_gdbarch_types *types | |
50 | = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types); | |
51 | ||
52 | /* The types themselves are lazily initialized. */ | |
53 | ||
54 | return types; | |
55 | } | |
56 | ||
57 | /* Return the type used for DWARF operations where the type is | |
58 | unspecified in the DWARF spec. Only certain sizes are | |
59 | supported. */ | |
60 | ||
595d2e30 TT |
61 | struct type * |
62 | dwarf_expr_context::address_type () const | |
8a9b8146 | 63 | { |
9a3c8263 | 64 | struct dwarf_gdbarch_types *types |
595d2e30 | 65 | = (struct dwarf_gdbarch_types *) gdbarch_data (this->gdbarch, |
9a3c8263 | 66 | dwarf_arch_cookie); |
8a9b8146 TT |
67 | int ndx; |
68 | ||
595d2e30 | 69 | if (this->addr_size == 2) |
8a9b8146 | 70 | ndx = 0; |
595d2e30 | 71 | else if (this->addr_size == 4) |
8a9b8146 | 72 | ndx = 1; |
595d2e30 | 73 | else if (this->addr_size == 8) |
8a9b8146 TT |
74 | ndx = 2; |
75 | else | |
76 | error (_("Unsupported address size in DWARF expressions: %d bits"), | |
595d2e30 | 77 | 8 * this->addr_size); |
8a9b8146 TT |
78 | |
79 | if (types->dw_types[ndx] == NULL) | |
80 | types->dw_types[ndx] | |
595d2e30 TT |
81 | = arch_integer_type (this->gdbarch, |
82 | 8 * this->addr_size, | |
8a9b8146 TT |
83 | 0, "<signed DWARF address type>"); |
84 | ||
85 | return types->dw_types[ndx]; | |
86 | } | |
87 | ||
4c2df51b DJ |
88 | /* Create a new context for the expression evaluator. */ |
89 | ||
718b9626 TT |
90 | dwarf_expr_context::dwarf_expr_context () |
91 | : stack (NULL), | |
92 | stack_len (0), | |
93 | stack_allocated (10), | |
94 | gdbarch (NULL), | |
95 | addr_size (0), | |
96 | ref_addr_size (0), | |
97 | offset (0), | |
98 | recursion_depth (0), | |
99 | max_recursion_depth (0x100), | |
100 | location (DWARF_VALUE_MEMORY), | |
101 | len (0), | |
102 | data (NULL), | |
103 | initialized (0), | |
104 | num_pieces (0), | |
105 | pieces (NULL) | |
4c2df51b | 106 | { |
718b9626 | 107 | this->stack = XNEWVEC (struct dwarf_stack_value, this->stack_allocated); |
4c2df51b DJ |
108 | } |
109 | ||
718b9626 | 110 | /* Clean up a dwarf_expr_context. */ |
4c2df51b | 111 | |
718b9626 | 112 | dwarf_expr_context::~dwarf_expr_context () |
4c2df51b | 113 | { |
718b9626 TT |
114 | xfree (this->stack); |
115 | xfree (this->pieces); | |
4a227398 TT |
116 | } |
117 | ||
595d2e30 | 118 | /* Expand the memory allocated stack to contain at least |
4c2df51b DJ |
119 | NEED more elements than are currently used. */ |
120 | ||
595d2e30 TT |
121 | void |
122 | dwarf_expr_context::grow_stack (size_t need) | |
4c2df51b | 123 | { |
595d2e30 | 124 | if (this->stack_len + need > this->stack_allocated) |
4c2df51b | 125 | { |
595d2e30 | 126 | size_t newlen = this->stack_len + need + 10; |
9a619af0 | 127 | |
595d2e30 TT |
128 | this->stack = XRESIZEVEC (struct dwarf_stack_value, this->stack, newlen); |
129 | this->stack_allocated = newlen; | |
4c2df51b DJ |
130 | } |
131 | } | |
132 | ||
595d2e30 | 133 | /* Push VALUE onto the stack. */ |
4c2df51b | 134 | |
595d2e30 TT |
135 | void |
136 | dwarf_expr_context::push (struct value *value, int in_stack_memory) | |
4c2df51b | 137 | { |
44353522 DE |
138 | struct dwarf_stack_value *v; |
139 | ||
595d2e30 TT |
140 | grow_stack (1); |
141 | v = &this->stack[this->stack_len++]; | |
44353522 DE |
142 | v->value = value; |
143 | v->in_stack_memory = in_stack_memory; | |
4c2df51b DJ |
144 | } |
145 | ||
595d2e30 | 146 | /* Push VALUE onto the stack. */ |
4c2df51b DJ |
147 | |
148 | void | |
595d2e30 | 149 | dwarf_expr_context::push_address (CORE_ADDR value, int in_stack_memory) |
8a9b8146 | 150 | { |
595d2e30 | 151 | push (value_from_ulongest (address_type (), value), in_stack_memory); |
8a9b8146 TT |
152 | } |
153 | ||
595d2e30 | 154 | /* Pop the top item off of the stack. */ |
8a9b8146 | 155 | |
595d2e30 TT |
156 | void |
157 | dwarf_expr_context::pop () | |
4c2df51b | 158 | { |
595d2e30 | 159 | if (this->stack_len <= 0) |
8a3fe4f8 | 160 | error (_("dwarf expression stack underflow")); |
595d2e30 | 161 | this->stack_len--; |
4c2df51b DJ |
162 | } |
163 | ||
595d2e30 | 164 | /* Retrieve the N'th item on the stack. */ |
4c2df51b | 165 | |
8a9b8146 | 166 | struct value * |
595d2e30 | 167 | dwarf_expr_context::fetch (int n) |
4c2df51b | 168 | { |
595d2e30 | 169 | if (this->stack_len <= n) |
3e43a32a MS |
170 | error (_("Asked for position %d of stack, " |
171 | "stack only has %d elements on it."), | |
595d2e30 TT |
172 | n, this->stack_len); |
173 | return this->stack[this->stack_len - (1 + n)].value; | |
8a9b8146 TT |
174 | } |
175 | ||
176 | /* Require that TYPE be an integral type; throw an exception if not. */ | |
44353522 | 177 | |
8a9b8146 TT |
178 | static void |
179 | dwarf_require_integral (struct type *type) | |
180 | { | |
181 | if (TYPE_CODE (type) != TYPE_CODE_INT | |
182 | && TYPE_CODE (type) != TYPE_CODE_CHAR | |
183 | && TYPE_CODE (type) != TYPE_CODE_BOOL) | |
184 | error (_("integral type expected in DWARF expression")); | |
185 | } | |
186 | ||
187 | /* Return the unsigned form of TYPE. TYPE is necessarily an integral | |
188 | type. */ | |
189 | ||
190 | static struct type * | |
191 | get_unsigned_type (struct gdbarch *gdbarch, struct type *type) | |
192 | { | |
193 | switch (TYPE_LENGTH (type)) | |
194 | { | |
195 | case 1: | |
196 | return builtin_type (gdbarch)->builtin_uint8; | |
197 | case 2: | |
198 | return builtin_type (gdbarch)->builtin_uint16; | |
199 | case 4: | |
200 | return builtin_type (gdbarch)->builtin_uint32; | |
201 | case 8: | |
202 | return builtin_type (gdbarch)->builtin_uint64; | |
203 | default: | |
204 | error (_("no unsigned variant found for type, while evaluating " | |
205 | "DWARF expression")); | |
206 | } | |
44353522 DE |
207 | } |
208 | ||
8ddd9a20 TT |
209 | /* Return the signed form of TYPE. TYPE is necessarily an integral |
210 | type. */ | |
211 | ||
212 | static struct type * | |
213 | get_signed_type (struct gdbarch *gdbarch, struct type *type) | |
214 | { | |
215 | switch (TYPE_LENGTH (type)) | |
216 | { | |
217 | case 1: | |
218 | return builtin_type (gdbarch)->builtin_int8; | |
219 | case 2: | |
220 | return builtin_type (gdbarch)->builtin_int16; | |
221 | case 4: | |
222 | return builtin_type (gdbarch)->builtin_int32; | |
223 | case 8: | |
224 | return builtin_type (gdbarch)->builtin_int64; | |
225 | default: | |
226 | error (_("no signed variant found for type, while evaluating " | |
227 | "DWARF expression")); | |
228 | } | |
229 | } | |
230 | ||
595d2e30 | 231 | /* Retrieve the N'th item on the stack, converted to an address. */ |
f2c7657e UW |
232 | |
233 | CORE_ADDR | |
595d2e30 | 234 | dwarf_expr_context::fetch_address (int n) |
f2c7657e | 235 | { |
595d2e30 TT |
236 | struct value *result_val = fetch (n); |
237 | enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch); | |
8a9b8146 TT |
238 | ULONGEST result; |
239 | ||
240 | dwarf_require_integral (value_type (result_val)); | |
241 | result = extract_unsigned_integer (value_contents (result_val), | |
242 | TYPE_LENGTH (value_type (result_val)), | |
243 | byte_order); | |
f2c7657e UW |
244 | |
245 | /* For most architectures, calling extract_unsigned_integer() alone | |
246 | is sufficient for extracting an address. However, some | |
247 | architectures (e.g. MIPS) use signed addresses and using | |
248 | extract_unsigned_integer() will not produce a correct | |
249 | result. Make sure we invoke gdbarch_integer_to_address() | |
250 | for those architectures which require it. */ | |
595d2e30 | 251 | if (gdbarch_integer_to_address_p (this->gdbarch)) |
f2c7657e | 252 | { |
595d2e30 TT |
253 | gdb_byte *buf = (gdb_byte *) alloca (this->addr_size); |
254 | struct type *int_type = get_unsigned_type (this->gdbarch, | |
8a9b8146 | 255 | value_type (result_val)); |
f2c7657e | 256 | |
595d2e30 TT |
257 | store_unsigned_integer (buf, this->addr_size, byte_order, result); |
258 | return gdbarch_integer_to_address (this->gdbarch, int_type, buf); | |
f2c7657e UW |
259 | } |
260 | ||
261 | return (CORE_ADDR) result; | |
262 | } | |
263 | ||
595d2e30 | 264 | /* Retrieve the in_stack_memory flag of the N'th item on the stack. */ |
44353522 DE |
265 | |
266 | int | |
595d2e30 | 267 | dwarf_expr_context::fetch_in_stack_memory (int n) |
44353522 | 268 | { |
595d2e30 | 269 | if (this->stack_len <= n) |
3e43a32a MS |
270 | error (_("Asked for position %d of stack, " |
271 | "stack only has %d elements on it."), | |
595d2e30 TT |
272 | n, this->stack_len); |
273 | return this->stack[this->stack_len - (1 + n)].in_stack_memory; | |
4c2df51b DJ |
274 | } |
275 | ||
cb826367 TT |
276 | /* Return true if the expression stack is empty. */ |
277 | ||
595d2e30 TT |
278 | int |
279 | dwarf_expr_context::stack_empty_p () const | |
cb826367 | 280 | { |
595d2e30 | 281 | return this->stack_len == 0; |
cb826367 TT |
282 | } |
283 | ||
595d2e30 TT |
284 | /* Add a new piece to the dwarf_expr_context's piece list. */ |
285 | void | |
286 | dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset) | |
87808bd6 JB |
287 | { |
288 | struct dwarf_expr_piece *p; | |
289 | ||
595d2e30 | 290 | this->num_pieces++; |
87808bd6 | 291 | |
595d2e30 TT |
292 | this->pieces |
293 | = XRESIZEVEC (struct dwarf_expr_piece, this->pieces, this->num_pieces); | |
87808bd6 | 294 | |
595d2e30 TT |
295 | p = &this->pieces[this->num_pieces - 1]; |
296 | p->location = this->location; | |
87808bd6 | 297 | p->size = size; |
d3b1e874 TT |
298 | p->offset = offset; |
299 | ||
cec03d70 TT |
300 | if (p->location == DWARF_VALUE_LITERAL) |
301 | { | |
595d2e30 TT |
302 | p->v.literal.data = this->data; |
303 | p->v.literal.length = this->len; | |
cec03d70 | 304 | } |
595d2e30 | 305 | else if (stack_empty_p ()) |
cb826367 TT |
306 | { |
307 | p->location = DWARF_VALUE_OPTIMIZED_OUT; | |
308 | /* Also reset the context's location, for our callers. This is | |
309 | a somewhat strange approach, but this lets us avoid setting | |
310 | the location to DWARF_VALUE_MEMORY in all the individual | |
311 | cases in the evaluator. */ | |
595d2e30 | 312 | this->location = DWARF_VALUE_OPTIMIZED_OUT; |
cb826367 | 313 | } |
f2c7657e UW |
314 | else if (p->location == DWARF_VALUE_MEMORY) |
315 | { | |
595d2e30 TT |
316 | p->v.mem.addr = fetch_address (0); |
317 | p->v.mem.in_stack_memory = fetch_in_stack_memory (0); | |
f2c7657e | 318 | } |
8cf6f0b1 TT |
319 | else if (p->location == DWARF_VALUE_IMPLICIT_POINTER) |
320 | { | |
9c541725 | 321 | p->v.ptr.die_sect_off = (sect_offset) this->len; |
595d2e30 | 322 | p->v.ptr.offset = value_as_long (fetch (0)); |
8cf6f0b1 | 323 | } |
8a9b8146 | 324 | else if (p->location == DWARF_VALUE_REGISTER) |
595d2e30 | 325 | p->v.regno = value_as_long (fetch (0)); |
cec03d70 | 326 | else |
44353522 | 327 | { |
595d2e30 | 328 | p->v.value = fetch (0); |
44353522 | 329 | } |
87808bd6 JB |
330 | } |
331 | ||
595d2e30 | 332 | /* Evaluate the expression at ADDR (LEN bytes long). */ |
4c2df51b DJ |
333 | |
334 | void | |
595d2e30 | 335 | dwarf_expr_context::eval (const gdb_byte *addr, size_t len) |
4c2df51b | 336 | { |
595d2e30 | 337 | int old_recursion_depth = this->recursion_depth; |
1e3a102a | 338 | |
595d2e30 | 339 | execute_stack_op (addr, addr + len); |
1e3a102a | 340 | |
595d2e30 | 341 | /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */ |
1e3a102a | 342 | |
595d2e30 | 343 | gdb_assert (this->recursion_depth == old_recursion_depth); |
4c2df51b DJ |
344 | } |
345 | ||
f664829e | 346 | /* Helper to read a uleb128 value or throw an error. */ |
4c2df51b | 347 | |
0d45f56e | 348 | const gdb_byte * |
f664829e | 349 | safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, |
9fccedf7 | 350 | uint64_t *r) |
4c2df51b | 351 | { |
f664829e DE |
352 | buf = gdb_read_uleb128 (buf, buf_end, r); |
353 | if (buf == NULL) | |
354 | error (_("DWARF expression error: ran off end of buffer reading uleb128 value")); | |
4c2df51b DJ |
355 | return buf; |
356 | } | |
357 | ||
f664829e | 358 | /* Helper to read a sleb128 value or throw an error. */ |
4c2df51b | 359 | |
0d45f56e | 360 | const gdb_byte * |
f664829e | 361 | safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, |
9fccedf7 | 362 | int64_t *r) |
4c2df51b | 363 | { |
f664829e DE |
364 | buf = gdb_read_sleb128 (buf, buf_end, r); |
365 | if (buf == NULL) | |
366 | error (_("DWARF expression error: ran off end of buffer reading sleb128 value")); | |
367 | return buf; | |
368 | } | |
4c2df51b | 369 | |
f664829e DE |
370 | const gdb_byte * |
371 | safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end) | |
372 | { | |
373 | buf = gdb_skip_leb128 (buf, buf_end); | |
374 | if (buf == NULL) | |
375 | error (_("DWARF expression error: ran off end of buffer reading leb128 value")); | |
4c2df51b DJ |
376 | return buf; |
377 | } | |
4c2df51b | 378 | \f |
cec03d70 TT |
379 | |
380 | /* Check that the current operator is either at the end of an | |
f206f69c AA |
381 | expression, or that it is followed by a composition operator or by |
382 | DW_OP_GNU_uninit (which should terminate the expression). */ | |
cec03d70 | 383 | |
3cf03773 TT |
384 | void |
385 | dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end, | |
386 | const char *op_name) | |
cec03d70 | 387 | { |
f206f69c AA |
388 | if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece |
389 | && *op_ptr != DW_OP_GNU_uninit) | |
cec03d70 | 390 | error (_("DWARF-2 expression error: `%s' operations must be " |
64b9b334 | 391 | "used either alone or in conjunction with DW_OP_piece " |
cec03d70 TT |
392 | "or DW_OP_bit_piece."), |
393 | op_name); | |
394 | } | |
395 | ||
8a9b8146 TT |
396 | /* Return true iff the types T1 and T2 are "the same". This only does |
397 | checks that might reasonably be needed to compare DWARF base | |
398 | types. */ | |
399 | ||
400 | static int | |
401 | base_types_equal_p (struct type *t1, struct type *t2) | |
402 | { | |
403 | if (TYPE_CODE (t1) != TYPE_CODE (t2)) | |
404 | return 0; | |
405 | if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2)) | |
406 | return 0; | |
407 | return TYPE_LENGTH (t1) == TYPE_LENGTH (t2); | |
408 | } | |
409 | ||
8e3b41a9 JK |
410 | /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the |
411 | DWARF register number. Otherwise return -1. */ | |
412 | ||
413 | int | |
414 | dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end) | |
415 | { | |
9fccedf7 | 416 | uint64_t dwarf_reg; |
8e3b41a9 JK |
417 | |
418 | if (buf_end <= buf) | |
419 | return -1; | |
420 | if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31) | |
421 | { | |
422 | if (buf_end - buf != 1) | |
423 | return -1; | |
424 | return *buf - DW_OP_reg0; | |
425 | } | |
426 | ||
216f72a1 | 427 | if (*buf == DW_OP_regval_type || *buf == DW_OP_GNU_regval_type) |
8e3b41a9 JK |
428 | { |
429 | buf++; | |
f664829e DE |
430 | buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg); |
431 | if (buf == NULL) | |
432 | return -1; | |
433 | buf = gdb_skip_leb128 (buf, buf_end); | |
434 | if (buf == NULL) | |
435 | return -1; | |
8e3b41a9 JK |
436 | } |
437 | else if (*buf == DW_OP_regx) | |
438 | { | |
439 | buf++; | |
f664829e DE |
440 | buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg); |
441 | if (buf == NULL) | |
442 | return -1; | |
8e3b41a9 JK |
443 | } |
444 | else | |
445 | return -1; | |
446 | if (buf != buf_end || (int) dwarf_reg != dwarf_reg) | |
447 | return -1; | |
448 | return dwarf_reg; | |
449 | } | |
450 | ||
a471c594 JK |
451 | /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and |
452 | DW_OP_deref* return the DWARF register number. Otherwise return -1. | |
453 | DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the | |
454 | size from DW_OP_deref_size. */ | |
455 | ||
456 | int | |
457 | dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end, | |
458 | CORE_ADDR *deref_size_return) | |
459 | { | |
9fccedf7 DE |
460 | uint64_t dwarf_reg; |
461 | int64_t offset; | |
a471c594 JK |
462 | |
463 | if (buf_end <= buf) | |
464 | return -1; | |
f664829e | 465 | |
a471c594 JK |
466 | if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31) |
467 | { | |
468 | dwarf_reg = *buf - DW_OP_breg0; | |
469 | buf++; | |
f664829e DE |
470 | if (buf >= buf_end) |
471 | return -1; | |
a471c594 JK |
472 | } |
473 | else if (*buf == DW_OP_bregx) | |
474 | { | |
475 | buf++; | |
f664829e DE |
476 | buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg); |
477 | if (buf == NULL) | |
478 | return -1; | |
a471c594 JK |
479 | if ((int) dwarf_reg != dwarf_reg) |
480 | return -1; | |
481 | } | |
482 | else | |
483 | return -1; | |
484 | ||
f664829e DE |
485 | buf = gdb_read_sleb128 (buf, buf_end, &offset); |
486 | if (buf == NULL) | |
a471c594 | 487 | return -1; |
f664829e | 488 | if (offset != 0) |
a471c594 JK |
489 | return -1; |
490 | ||
491 | if (*buf == DW_OP_deref) | |
492 | { | |
493 | buf++; | |
494 | *deref_size_return = -1; | |
495 | } | |
496 | else if (*buf == DW_OP_deref_size) | |
497 | { | |
498 | buf++; | |
499 | if (buf >= buf_end) | |
500 | return -1; | |
501 | *deref_size_return = *buf++; | |
502 | } | |
503 | else | |
504 | return -1; | |
505 | ||
506 | if (buf != buf_end) | |
507 | return -1; | |
508 | ||
509 | return dwarf_reg; | |
510 | } | |
511 | ||
e18b2753 JK |
512 | /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill |
513 | in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */ | |
514 | ||
515 | int | |
516 | dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end, | |
517 | CORE_ADDR *fb_offset_return) | |
518 | { | |
9fccedf7 | 519 | int64_t fb_offset; |
e18b2753 JK |
520 | |
521 | if (buf_end <= buf) | |
522 | return 0; | |
523 | ||
524 | if (*buf != DW_OP_fbreg) | |
525 | return 0; | |
526 | buf++; | |
527 | ||
f664829e DE |
528 | buf = gdb_read_sleb128 (buf, buf_end, &fb_offset); |
529 | if (buf == NULL) | |
530 | return 0; | |
e18b2753 JK |
531 | *fb_offset_return = fb_offset; |
532 | if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return) | |
533 | return 0; | |
534 | ||
535 | return 1; | |
536 | } | |
537 | ||
538 | /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill | |
539 | in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. | |
540 | The matched SP register number depends on GDBARCH. */ | |
541 | ||
542 | int | |
543 | dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf, | |
544 | const gdb_byte *buf_end, CORE_ADDR *sp_offset_return) | |
545 | { | |
9fccedf7 DE |
546 | uint64_t dwarf_reg; |
547 | int64_t sp_offset; | |
e18b2753 JK |
548 | |
549 | if (buf_end <= buf) | |
550 | return 0; | |
551 | if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31) | |
552 | { | |
553 | dwarf_reg = *buf - DW_OP_breg0; | |
554 | buf++; | |
555 | } | |
556 | else | |
557 | { | |
558 | if (*buf != DW_OP_bregx) | |
559 | return 0; | |
560 | buf++; | |
f664829e DE |
561 | buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg); |
562 | if (buf == NULL) | |
563 | return 0; | |
e18b2753 JK |
564 | } |
565 | ||
0fde2c53 | 566 | if (dwarf_reg_to_regnum (gdbarch, dwarf_reg) |
e18b2753 JK |
567 | != gdbarch_sp_regnum (gdbarch)) |
568 | return 0; | |
569 | ||
f664829e DE |
570 | buf = gdb_read_sleb128 (buf, buf_end, &sp_offset); |
571 | if (buf == NULL) | |
572 | return 0; | |
e18b2753 JK |
573 | *sp_offset_return = sp_offset; |
574 | if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return) | |
575 | return 0; | |
576 | ||
577 | return 1; | |
578 | } | |
579 | ||
595d2e30 TT |
580 | /* The engine for the expression evaluator. Using the context in this |
581 | object, evaluate the expression between OP_PTR and OP_END. */ | |
4c2df51b | 582 | |
595d2e30 TT |
583 | void |
584 | dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr, | |
585 | const gdb_byte *op_end) | |
4c2df51b | 586 | { |
595d2e30 | 587 | enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch); |
8a9b8146 TT |
588 | /* Old-style "untyped" DWARF values need special treatment in a |
589 | couple of places, specifically DW_OP_mod and DW_OP_shr. We need | |
590 | a special type for these values so we can distinguish them from | |
591 | values that have an explicit type, because explicitly-typed | |
592 | values do not need special treatment. This special type must be | |
593 | different (in the `==' sense) from any base type coming from the | |
594 | CU. */ | |
595d2e30 | 595 | struct type *address_type = this->address_type (); |
9a619af0 | 596 | |
595d2e30 TT |
597 | this->location = DWARF_VALUE_MEMORY; |
598 | this->initialized = 1; /* Default is initialized. */ | |
18ec9831 | 599 | |
595d2e30 | 600 | if (this->recursion_depth > this->max_recursion_depth) |
1e3a102a | 601 | error (_("DWARF-2 expression error: Loop detected (%d)."), |
595d2e30 TT |
602 | this->recursion_depth); |
603 | this->recursion_depth++; | |
1e3a102a | 604 | |
4c2df51b DJ |
605 | while (op_ptr < op_end) |
606 | { | |
aead7601 | 607 | enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr++; |
f2c7657e | 608 | ULONGEST result; |
44353522 DE |
609 | /* Assume the value is not in stack memory. |
610 | Code that knows otherwise sets this to 1. | |
611 | Some arithmetic on stack addresses can probably be assumed to still | |
612 | be a stack address, but we skip this complication for now. | |
613 | This is just an optimization, so it's always ok to punt | |
614 | and leave this as 0. */ | |
615 | int in_stack_memory = 0; | |
9fccedf7 DE |
616 | uint64_t uoffset, reg; |
617 | int64_t offset; | |
8a9b8146 | 618 | struct value *result_val = NULL; |
4c2df51b | 619 | |
e0e9434c TT |
620 | /* The DWARF expression might have a bug causing an infinite |
621 | loop. In that case, quitting is the only way out. */ | |
622 | QUIT; | |
623 | ||
4c2df51b DJ |
624 | switch (op) |
625 | { | |
626 | case DW_OP_lit0: | |
627 | case DW_OP_lit1: | |
628 | case DW_OP_lit2: | |
629 | case DW_OP_lit3: | |
630 | case DW_OP_lit4: | |
631 | case DW_OP_lit5: | |
632 | case DW_OP_lit6: | |
633 | case DW_OP_lit7: | |
634 | case DW_OP_lit8: | |
635 | case DW_OP_lit9: | |
636 | case DW_OP_lit10: | |
637 | case DW_OP_lit11: | |
638 | case DW_OP_lit12: | |
639 | case DW_OP_lit13: | |
640 | case DW_OP_lit14: | |
641 | case DW_OP_lit15: | |
642 | case DW_OP_lit16: | |
643 | case DW_OP_lit17: | |
644 | case DW_OP_lit18: | |
645 | case DW_OP_lit19: | |
646 | case DW_OP_lit20: | |
647 | case DW_OP_lit21: | |
648 | case DW_OP_lit22: | |
649 | case DW_OP_lit23: | |
650 | case DW_OP_lit24: | |
651 | case DW_OP_lit25: | |
652 | case DW_OP_lit26: | |
653 | case DW_OP_lit27: | |
654 | case DW_OP_lit28: | |
655 | case DW_OP_lit29: | |
656 | case DW_OP_lit30: | |
657 | case DW_OP_lit31: | |
658 | result = op - DW_OP_lit0; | |
8a9b8146 | 659 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
660 | break; |
661 | ||
662 | case DW_OP_addr: | |
f2c7657e | 663 | result = extract_unsigned_integer (op_ptr, |
595d2e30 TT |
664 | this->addr_size, byte_order); |
665 | op_ptr += this->addr_size; | |
ac56253d TT |
666 | /* Some versions of GCC emit DW_OP_addr before |
667 | DW_OP_GNU_push_tls_address. In this case the value is an | |
668 | index, not an address. We don't support things like | |
669 | branching between the address and the TLS op. */ | |
670 | if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address) | |
595d2e30 | 671 | result += this->offset; |
8a9b8146 | 672 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
673 | break; |
674 | ||
3019eac3 | 675 | case DW_OP_GNU_addr_index: |
49f6c839 | 676 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); |
192ca6d8 | 677 | result = this->get_addr_index (uoffset); |
595d2e30 | 678 | result += this->offset; |
49f6c839 DE |
679 | result_val = value_from_ulongest (address_type, result); |
680 | break; | |
681 | case DW_OP_GNU_const_index: | |
f664829e | 682 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); |
192ca6d8 | 683 | result = this->get_addr_index (uoffset); |
3019eac3 DE |
684 | result_val = value_from_ulongest (address_type, result); |
685 | break; | |
686 | ||
4c2df51b | 687 | case DW_OP_const1u: |
e17a4113 | 688 | result = extract_unsigned_integer (op_ptr, 1, byte_order); |
8a9b8146 | 689 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
690 | op_ptr += 1; |
691 | break; | |
692 | case DW_OP_const1s: | |
e17a4113 | 693 | result = extract_signed_integer (op_ptr, 1, byte_order); |
8a9b8146 | 694 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
695 | op_ptr += 1; |
696 | break; | |
697 | case DW_OP_const2u: | |
e17a4113 | 698 | result = extract_unsigned_integer (op_ptr, 2, byte_order); |
8a9b8146 | 699 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
700 | op_ptr += 2; |
701 | break; | |
702 | case DW_OP_const2s: | |
e17a4113 | 703 | result = extract_signed_integer (op_ptr, 2, byte_order); |
8a9b8146 | 704 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
705 | op_ptr += 2; |
706 | break; | |
707 | case DW_OP_const4u: | |
e17a4113 | 708 | result = extract_unsigned_integer (op_ptr, 4, byte_order); |
8a9b8146 | 709 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
710 | op_ptr += 4; |
711 | break; | |
712 | case DW_OP_const4s: | |
e17a4113 | 713 | result = extract_signed_integer (op_ptr, 4, byte_order); |
8a9b8146 | 714 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
715 | op_ptr += 4; |
716 | break; | |
717 | case DW_OP_const8u: | |
e17a4113 | 718 | result = extract_unsigned_integer (op_ptr, 8, byte_order); |
8a9b8146 | 719 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
720 | op_ptr += 8; |
721 | break; | |
722 | case DW_OP_const8s: | |
e17a4113 | 723 | result = extract_signed_integer (op_ptr, 8, byte_order); |
8a9b8146 | 724 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
725 | op_ptr += 8; |
726 | break; | |
727 | case DW_OP_constu: | |
f664829e | 728 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); |
4c2df51b | 729 | result = uoffset; |
8a9b8146 | 730 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
731 | break; |
732 | case DW_OP_consts: | |
f664829e | 733 | op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); |
4c2df51b | 734 | result = offset; |
8a9b8146 | 735 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
736 | break; |
737 | ||
738 | /* The DW_OP_reg operations are required to occur alone in | |
739 | location expressions. */ | |
740 | case DW_OP_reg0: | |
741 | case DW_OP_reg1: | |
742 | case DW_OP_reg2: | |
743 | case DW_OP_reg3: | |
744 | case DW_OP_reg4: | |
745 | case DW_OP_reg5: | |
746 | case DW_OP_reg6: | |
747 | case DW_OP_reg7: | |
748 | case DW_OP_reg8: | |
749 | case DW_OP_reg9: | |
750 | case DW_OP_reg10: | |
751 | case DW_OP_reg11: | |
752 | case DW_OP_reg12: | |
753 | case DW_OP_reg13: | |
754 | case DW_OP_reg14: | |
755 | case DW_OP_reg15: | |
756 | case DW_OP_reg16: | |
757 | case DW_OP_reg17: | |
758 | case DW_OP_reg18: | |
759 | case DW_OP_reg19: | |
760 | case DW_OP_reg20: | |
761 | case DW_OP_reg21: | |
762 | case DW_OP_reg22: | |
763 | case DW_OP_reg23: | |
764 | case DW_OP_reg24: | |
765 | case DW_OP_reg25: | |
766 | case DW_OP_reg26: | |
767 | case DW_OP_reg27: | |
768 | case DW_OP_reg28: | |
769 | case DW_OP_reg29: | |
770 | case DW_OP_reg30: | |
771 | case DW_OP_reg31: | |
f206f69c | 772 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg"); |
4c2df51b | 773 | |
61fbb938 | 774 | result = op - DW_OP_reg0; |
8a9b8146 | 775 | result_val = value_from_ulongest (address_type, result); |
595d2e30 | 776 | this->location = DWARF_VALUE_REGISTER; |
4c2df51b DJ |
777 | break; |
778 | ||
779 | case DW_OP_regx: | |
f664829e | 780 | op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); |
3cf03773 | 781 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx"); |
4c2df51b | 782 | |
61fbb938 | 783 | result = reg; |
8a9b8146 | 784 | result_val = value_from_ulongest (address_type, result); |
595d2e30 | 785 | this->location = DWARF_VALUE_REGISTER; |
4c2df51b DJ |
786 | break; |
787 | ||
cec03d70 TT |
788 | case DW_OP_implicit_value: |
789 | { | |
9fccedf7 | 790 | uint64_t len; |
9a619af0 | 791 | |
f664829e | 792 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &len); |
cec03d70 TT |
793 | if (op_ptr + len > op_end) |
794 | error (_("DW_OP_implicit_value: too few bytes available.")); | |
595d2e30 TT |
795 | this->len = len; |
796 | this->data = op_ptr; | |
797 | this->location = DWARF_VALUE_LITERAL; | |
cec03d70 | 798 | op_ptr += len; |
3cf03773 TT |
799 | dwarf_expr_require_composition (op_ptr, op_end, |
800 | "DW_OP_implicit_value"); | |
cec03d70 TT |
801 | } |
802 | goto no_push; | |
803 | ||
804 | case DW_OP_stack_value: | |
595d2e30 | 805 | this->location = DWARF_VALUE_STACK; |
3cf03773 | 806 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value"); |
cec03d70 TT |
807 | goto no_push; |
808 | ||
216f72a1 | 809 | case DW_OP_implicit_pointer: |
8cf6f0b1 TT |
810 | case DW_OP_GNU_implicit_pointer: |
811 | { | |
9fccedf7 | 812 | int64_t len; |
8cf6f0b1 | 813 | |
595d2e30 | 814 | if (this->ref_addr_size == -1) |
216f72a1 | 815 | error (_("DWARF-2 expression error: DW_OP_implicit_pointer " |
181cebd4 JK |
816 | "is not allowed in frame context")); |
817 | ||
8b9737bf | 818 | /* The referred-to DIE of sect_offset kind. */ |
595d2e30 | 819 | this->len = extract_unsigned_integer (op_ptr, this->ref_addr_size, |
8cf6f0b1 | 820 | byte_order); |
595d2e30 | 821 | op_ptr += this->ref_addr_size; |
8cf6f0b1 TT |
822 | |
823 | /* The byte offset into the data. */ | |
f664829e | 824 | op_ptr = safe_read_sleb128 (op_ptr, op_end, &len); |
8cf6f0b1 | 825 | result = (ULONGEST) len; |
8a9b8146 | 826 | result_val = value_from_ulongest (address_type, result); |
8cf6f0b1 | 827 | |
595d2e30 | 828 | this->location = DWARF_VALUE_IMPLICIT_POINTER; |
8cf6f0b1 | 829 | dwarf_expr_require_composition (op_ptr, op_end, |
216f72a1 | 830 | "DW_OP_implicit_pointer"); |
8cf6f0b1 TT |
831 | } |
832 | break; | |
833 | ||
4c2df51b DJ |
834 | case DW_OP_breg0: |
835 | case DW_OP_breg1: | |
836 | case DW_OP_breg2: | |
837 | case DW_OP_breg3: | |
838 | case DW_OP_breg4: | |
839 | case DW_OP_breg5: | |
840 | case DW_OP_breg6: | |
841 | case DW_OP_breg7: | |
842 | case DW_OP_breg8: | |
843 | case DW_OP_breg9: | |
844 | case DW_OP_breg10: | |
845 | case DW_OP_breg11: | |
846 | case DW_OP_breg12: | |
847 | case DW_OP_breg13: | |
848 | case DW_OP_breg14: | |
849 | case DW_OP_breg15: | |
850 | case DW_OP_breg16: | |
851 | case DW_OP_breg17: | |
852 | case DW_OP_breg18: | |
853 | case DW_OP_breg19: | |
854 | case DW_OP_breg20: | |
855 | case DW_OP_breg21: | |
856 | case DW_OP_breg22: | |
857 | case DW_OP_breg23: | |
858 | case DW_OP_breg24: | |
859 | case DW_OP_breg25: | |
860 | case DW_OP_breg26: | |
861 | case DW_OP_breg27: | |
862 | case DW_OP_breg28: | |
863 | case DW_OP_breg29: | |
864 | case DW_OP_breg30: | |
865 | case DW_OP_breg31: | |
866 | { | |
f664829e | 867 | op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); |
192ca6d8 | 868 | result = this->read_addr_from_reg (op - DW_OP_breg0); |
4c2df51b | 869 | result += offset; |
8a9b8146 | 870 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
871 | } |
872 | break; | |
873 | case DW_OP_bregx: | |
874 | { | |
f664829e DE |
875 | op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); |
876 | op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); | |
192ca6d8 | 877 | result = this->read_addr_from_reg (reg); |
4c2df51b | 878 | result += offset; |
8a9b8146 | 879 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
880 | } |
881 | break; | |
882 | case DW_OP_fbreg: | |
883 | { | |
0d45f56e | 884 | const gdb_byte *datastart; |
4c2df51b DJ |
885 | size_t datalen; |
886 | unsigned int before_stack_len; | |
887 | ||
f664829e | 888 | op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); |
4c2df51b DJ |
889 | /* Rather than create a whole new context, we simply |
890 | record the stack length before execution, then reset it | |
891 | afterwards, effectively erasing whatever the recursive | |
892 | call put there. */ | |
595d2e30 | 893 | before_stack_len = this->stack_len; |
da62e633 AC |
894 | /* FIXME: cagney/2003-03-26: This code should be using |
895 | get_frame_base_address(), and then implement a dwarf2 | |
896 | specific this_base method. */ | |
192ca6d8 | 897 | this->get_frame_base (&datastart, &datalen); |
595d2e30 TT |
898 | eval (datastart, datalen); |
899 | if (this->location == DWARF_VALUE_MEMORY) | |
900 | result = fetch_address (0); | |
901 | else if (this->location == DWARF_VALUE_REGISTER) | |
192ca6d8 | 902 | result = this->read_addr_from_reg (value_as_long (fetch (0))); |
f2c7657e | 903 | else |
3e43a32a MS |
904 | error (_("Not implemented: computing frame " |
905 | "base using explicit value operator")); | |
4c2df51b | 906 | result = result + offset; |
8a9b8146 | 907 | result_val = value_from_ulongest (address_type, result); |
44353522 | 908 | in_stack_memory = 1; |
595d2e30 TT |
909 | this->stack_len = before_stack_len; |
910 | this->location = DWARF_VALUE_MEMORY; | |
4c2df51b DJ |
911 | } |
912 | break; | |
44353522 | 913 | |
4c2df51b | 914 | case DW_OP_dup: |
595d2e30 TT |
915 | result_val = fetch (0); |
916 | in_stack_memory = fetch_in_stack_memory (0); | |
4c2df51b DJ |
917 | break; |
918 | ||
919 | case DW_OP_drop: | |
595d2e30 | 920 | pop (); |
4c2df51b DJ |
921 | goto no_push; |
922 | ||
923 | case DW_OP_pick: | |
924 | offset = *op_ptr++; | |
595d2e30 TT |
925 | result_val = fetch (offset); |
926 | in_stack_memory = fetch_in_stack_memory (offset); | |
4c2df51b | 927 | break; |
9f3fe11c TG |
928 | |
929 | case DW_OP_swap: | |
930 | { | |
44353522 | 931 | struct dwarf_stack_value t1, t2; |
9f3fe11c | 932 | |
595d2e30 | 933 | if (this->stack_len < 2) |
3e43a32a | 934 | error (_("Not enough elements for " |
0963b4bd | 935 | "DW_OP_swap. Need 2, have %d."), |
595d2e30 TT |
936 | this->stack_len); |
937 | t1 = this->stack[this->stack_len - 1]; | |
938 | t2 = this->stack[this->stack_len - 2]; | |
939 | this->stack[this->stack_len - 1] = t2; | |
940 | this->stack[this->stack_len - 2] = t1; | |
9f3fe11c TG |
941 | goto no_push; |
942 | } | |
4c2df51b DJ |
943 | |
944 | case DW_OP_over: | |
595d2e30 TT |
945 | result_val = fetch (1); |
946 | in_stack_memory = fetch_in_stack_memory (1); | |
4c2df51b DJ |
947 | break; |
948 | ||
949 | case DW_OP_rot: | |
950 | { | |
44353522 | 951 | struct dwarf_stack_value t1, t2, t3; |
4c2df51b | 952 | |
595d2e30 | 953 | if (this->stack_len < 3) |
0963b4bd MS |
954 | error (_("Not enough elements for " |
955 | "DW_OP_rot. Need 3, have %d."), | |
595d2e30 TT |
956 | this->stack_len); |
957 | t1 = this->stack[this->stack_len - 1]; | |
958 | t2 = this->stack[this->stack_len - 2]; | |
959 | t3 = this->stack[this->stack_len - 3]; | |
960 | this->stack[this->stack_len - 1] = t2; | |
961 | this->stack[this->stack_len - 2] = t3; | |
962 | this->stack[this->stack_len - 3] = t1; | |
4c2df51b DJ |
963 | goto no_push; |
964 | } | |
965 | ||
966 | case DW_OP_deref: | |
967 | case DW_OP_deref_size: | |
216f72a1 | 968 | case DW_OP_deref_type: |
8a9b8146 | 969 | case DW_OP_GNU_deref_type: |
f2c7657e | 970 | { |
595d2e30 | 971 | int addr_size = (op == DW_OP_deref ? this->addr_size : *op_ptr++); |
224c3ddb | 972 | gdb_byte *buf = (gdb_byte *) alloca (addr_size); |
595d2e30 | 973 | CORE_ADDR addr = fetch_address (0); |
8a9b8146 TT |
974 | struct type *type; |
975 | ||
595d2e30 | 976 | pop (); |
f2c7657e | 977 | |
216f72a1 | 978 | if (op == DW_OP_deref_type || op == DW_OP_GNU_deref_type) |
8a9b8146 | 979 | { |
f664829e | 980 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); |
9c541725 PA |
981 | cu_offset type_die_cu_off = (cu_offset) uoffset; |
982 | type = get_base_type (type_die_cu_off, 0); | |
8a9b8146 TT |
983 | } |
984 | else | |
985 | type = address_type; | |
986 | ||
192ca6d8 | 987 | this->read_mem (buf, addr, addr_size); |
325663dc JB |
988 | |
989 | /* If the size of the object read from memory is different | |
990 | from the type length, we need to zero-extend it. */ | |
991 | if (TYPE_LENGTH (type) != addr_size) | |
992 | { | |
993 | ULONGEST result = | |
994 | extract_unsigned_integer (buf, addr_size, byte_order); | |
995 | ||
224c3ddb | 996 | buf = (gdb_byte *) alloca (TYPE_LENGTH (type)); |
325663dc JB |
997 | store_unsigned_integer (buf, TYPE_LENGTH (type), |
998 | byte_order, result); | |
999 | } | |
1000 | ||
8a9b8146 | 1001 | result_val = value_from_contents_and_address (type, buf, addr); |
f2c7657e UW |
1002 | break; |
1003 | } | |
1004 | ||
4c2df51b DJ |
1005 | case DW_OP_abs: |
1006 | case DW_OP_neg: | |
1007 | case DW_OP_not: | |
1008 | case DW_OP_plus_uconst: | |
8a9b8146 TT |
1009 | { |
1010 | /* Unary operations. */ | |
595d2e30 TT |
1011 | result_val = fetch (0); |
1012 | pop (); | |
4c2df51b | 1013 | |
8a9b8146 TT |
1014 | switch (op) |
1015 | { | |
1016 | case DW_OP_abs: | |
1017 | if (value_less (result_val, | |
1018 | value_zero (value_type (result_val), not_lval))) | |
1019 | result_val = value_neg (result_val); | |
1020 | break; | |
1021 | case DW_OP_neg: | |
1022 | result_val = value_neg (result_val); | |
1023 | break; | |
1024 | case DW_OP_not: | |
1025 | dwarf_require_integral (value_type (result_val)); | |
1026 | result_val = value_complement (result_val); | |
1027 | break; | |
1028 | case DW_OP_plus_uconst: | |
1029 | dwarf_require_integral (value_type (result_val)); | |
1030 | result = value_as_long (result_val); | |
f664829e | 1031 | op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); |
8a9b8146 TT |
1032 | result += reg; |
1033 | result_val = value_from_ulongest (address_type, result); | |
1034 | break; | |
1035 | } | |
1036 | } | |
4c2df51b DJ |
1037 | break; |
1038 | ||
1039 | case DW_OP_and: | |
1040 | case DW_OP_div: | |
1041 | case DW_OP_minus: | |
1042 | case DW_OP_mod: | |
1043 | case DW_OP_mul: | |
1044 | case DW_OP_or: | |
1045 | case DW_OP_plus: | |
1046 | case DW_OP_shl: | |
1047 | case DW_OP_shr: | |
1048 | case DW_OP_shra: | |
1049 | case DW_OP_xor: | |
1050 | case DW_OP_le: | |
1051 | case DW_OP_ge: | |
1052 | case DW_OP_eq: | |
1053 | case DW_OP_lt: | |
1054 | case DW_OP_gt: | |
1055 | case DW_OP_ne: | |
1056 | { | |
f2c7657e | 1057 | /* Binary operations. */ |
8a9b8146 | 1058 | struct value *first, *second; |
4c2df51b | 1059 | |
595d2e30 TT |
1060 | second = fetch (0); |
1061 | pop (); | |
4c2df51b | 1062 | |
595d2e30 TT |
1063 | first = fetch (0); |
1064 | pop (); | |
4c2df51b | 1065 | |
8a9b8146 TT |
1066 | if (! base_types_equal_p (value_type (first), value_type (second))) |
1067 | error (_("Incompatible types on DWARF stack")); | |
1068 | ||
4c2df51b DJ |
1069 | switch (op) |
1070 | { | |
1071 | case DW_OP_and: | |
8a9b8146 TT |
1072 | dwarf_require_integral (value_type (first)); |
1073 | dwarf_require_integral (value_type (second)); | |
1074 | result_val = value_binop (first, second, BINOP_BITWISE_AND); | |
4c2df51b DJ |
1075 | break; |
1076 | case DW_OP_div: | |
8a9b8146 | 1077 | result_val = value_binop (first, second, BINOP_DIV); |
99c87dab | 1078 | break; |
4c2df51b | 1079 | case DW_OP_minus: |
8a9b8146 | 1080 | result_val = value_binop (first, second, BINOP_SUB); |
4c2df51b DJ |
1081 | break; |
1082 | case DW_OP_mod: | |
8a9b8146 TT |
1083 | { |
1084 | int cast_back = 0; | |
1085 | struct type *orig_type = value_type (first); | |
1086 | ||
1087 | /* We have to special-case "old-style" untyped values | |
1088 | -- these must have mod computed using unsigned | |
1089 | math. */ | |
1090 | if (orig_type == address_type) | |
1091 | { | |
1092 | struct type *utype | |
595d2e30 | 1093 | = get_unsigned_type (this->gdbarch, orig_type); |
8a9b8146 TT |
1094 | |
1095 | cast_back = 1; | |
1096 | first = value_cast (utype, first); | |
1097 | second = value_cast (utype, second); | |
1098 | } | |
1099 | /* Note that value_binop doesn't handle float or | |
1100 | decimal float here. This seems unimportant. */ | |
1101 | result_val = value_binop (first, second, BINOP_MOD); | |
1102 | if (cast_back) | |
1103 | result_val = value_cast (orig_type, result_val); | |
1104 | } | |
4c2df51b DJ |
1105 | break; |
1106 | case DW_OP_mul: | |
8a9b8146 | 1107 | result_val = value_binop (first, second, BINOP_MUL); |
4c2df51b DJ |
1108 | break; |
1109 | case DW_OP_or: | |
8a9b8146 TT |
1110 | dwarf_require_integral (value_type (first)); |
1111 | dwarf_require_integral (value_type (second)); | |
1112 | result_val = value_binop (first, second, BINOP_BITWISE_IOR); | |
4c2df51b DJ |
1113 | break; |
1114 | case DW_OP_plus: | |
8a9b8146 | 1115 | result_val = value_binop (first, second, BINOP_ADD); |
4c2df51b DJ |
1116 | break; |
1117 | case DW_OP_shl: | |
8a9b8146 TT |
1118 | dwarf_require_integral (value_type (first)); |
1119 | dwarf_require_integral (value_type (second)); | |
1120 | result_val = value_binop (first, second, BINOP_LSH); | |
4c2df51b DJ |
1121 | break; |
1122 | case DW_OP_shr: | |
8a9b8146 TT |
1123 | dwarf_require_integral (value_type (first)); |
1124 | dwarf_require_integral (value_type (second)); | |
b087e0ed | 1125 | if (!TYPE_UNSIGNED (value_type (first))) |
8a9b8146 TT |
1126 | { |
1127 | struct type *utype | |
595d2e30 | 1128 | = get_unsigned_type (this->gdbarch, value_type (first)); |
8a9b8146 TT |
1129 | |
1130 | first = value_cast (utype, first); | |
1131 | } | |
1132 | ||
1133 | result_val = value_binop (first, second, BINOP_RSH); | |
1134 | /* Make sure we wind up with the same type we started | |
1135 | with. */ | |
1136 | if (value_type (result_val) != value_type (second)) | |
1137 | result_val = value_cast (value_type (second), result_val); | |
99c87dab | 1138 | break; |
4c2df51b | 1139 | case DW_OP_shra: |
8a9b8146 TT |
1140 | dwarf_require_integral (value_type (first)); |
1141 | dwarf_require_integral (value_type (second)); | |
8ddd9a20 TT |
1142 | if (TYPE_UNSIGNED (value_type (first))) |
1143 | { | |
1144 | struct type *stype | |
595d2e30 | 1145 | = get_signed_type (this->gdbarch, value_type (first)); |
8ddd9a20 TT |
1146 | |
1147 | first = value_cast (stype, first); | |
1148 | } | |
1149 | ||
8a9b8146 | 1150 | result_val = value_binop (first, second, BINOP_RSH); |
8ddd9a20 TT |
1151 | /* Make sure we wind up with the same type we started |
1152 | with. */ | |
1153 | if (value_type (result_val) != value_type (second)) | |
1154 | result_val = value_cast (value_type (second), result_val); | |
4c2df51b DJ |
1155 | break; |
1156 | case DW_OP_xor: | |
8a9b8146 TT |
1157 | dwarf_require_integral (value_type (first)); |
1158 | dwarf_require_integral (value_type (second)); | |
1159 | result_val = value_binop (first, second, BINOP_BITWISE_XOR); | |
4c2df51b DJ |
1160 | break; |
1161 | case DW_OP_le: | |
8a9b8146 TT |
1162 | /* A <= B is !(B < A). */ |
1163 | result = ! value_less (second, first); | |
1164 | result_val = value_from_ulongest (address_type, result); | |
4c2df51b DJ |
1165 | break; |
1166 | case DW_OP_ge: | |
8a9b8146 TT |
1167 | /* A >= B is !(A < B). */ |
1168 | result = ! value_less (first, second); | |
1169 | result_val = value_from_ulongest (address_type, result); | |
4c2df51b DJ |
1170 | break; |
1171 | case DW_OP_eq: | |
8a9b8146 TT |
1172 | result = value_equal (first, second); |
1173 | result_val = value_from_ulongest (address_type, result); | |
4c2df51b DJ |
1174 | break; |
1175 | case DW_OP_lt: | |
8a9b8146 TT |
1176 | result = value_less (first, second); |
1177 | result_val = value_from_ulongest (address_type, result); | |
4c2df51b DJ |
1178 | break; |
1179 | case DW_OP_gt: | |
8a9b8146 TT |
1180 | /* A > B is B < A. */ |
1181 | result = value_less (second, first); | |
1182 | result_val = value_from_ulongest (address_type, result); | |
4c2df51b DJ |
1183 | break; |
1184 | case DW_OP_ne: | |
8a9b8146 TT |
1185 | result = ! value_equal (first, second); |
1186 | result_val = value_from_ulongest (address_type, result); | |
4c2df51b DJ |
1187 | break; |
1188 | default: | |
1189 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 1190 | _("Can't be reached.")); |
4c2df51b | 1191 | } |
4c2df51b DJ |
1192 | } |
1193 | break; | |
1194 | ||
e7802207 | 1195 | case DW_OP_call_frame_cfa: |
192ca6d8 | 1196 | result = this->get_frame_cfa (); |
8a9b8146 | 1197 | result_val = value_from_ulongest (address_type, result); |
44353522 | 1198 | in_stack_memory = 1; |
e7802207 TT |
1199 | break; |
1200 | ||
4c2df51b | 1201 | case DW_OP_GNU_push_tls_address: |
4aa4e28b | 1202 | case DW_OP_form_tls_address: |
c3228f12 EZ |
1203 | /* Variable is at a constant offset in the thread-local |
1204 | storage block into the objfile for the current thread and | |
0963b4bd | 1205 | the dynamic linker module containing this expression. Here |
c3228f12 EZ |
1206 | we return returns the offset from that base. The top of the |
1207 | stack has the offset from the beginning of the thread | |
1208 | control block at which the variable is located. Nothing | |
1209 | should follow this operator, so the top of stack would be | |
1210 | returned. */ | |
595d2e30 TT |
1211 | result = value_as_long (fetch (0)); |
1212 | pop (); | |
192ca6d8 | 1213 | result = this->get_tls_address (result); |
8a9b8146 | 1214 | result_val = value_from_ulongest (address_type, result); |
4c2df51b DJ |
1215 | break; |
1216 | ||
1217 | case DW_OP_skip: | |
e17a4113 | 1218 | offset = extract_signed_integer (op_ptr, 2, byte_order); |
4c2df51b DJ |
1219 | op_ptr += 2; |
1220 | op_ptr += offset; | |
1221 | goto no_push; | |
1222 | ||
1223 | case DW_OP_bra: | |
8a9b8146 TT |
1224 | { |
1225 | struct value *val; | |
1226 | ||
1227 | offset = extract_signed_integer (op_ptr, 2, byte_order); | |
1228 | op_ptr += 2; | |
595d2e30 | 1229 | val = fetch (0); |
8a9b8146 TT |
1230 | dwarf_require_integral (value_type (val)); |
1231 | if (value_as_long (val) != 0) | |
1232 | op_ptr += offset; | |
595d2e30 | 1233 | pop (); |
8a9b8146 | 1234 | } |
4c2df51b DJ |
1235 | goto no_push; |
1236 | ||
1237 | case DW_OP_nop: | |
1238 | goto no_push; | |
1239 | ||
87808bd6 JB |
1240 | case DW_OP_piece: |
1241 | { | |
9fccedf7 | 1242 | uint64_t size; |
87808bd6 JB |
1243 | |
1244 | /* Record the piece. */ | |
f664829e | 1245 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &size); |
595d2e30 | 1246 | add_piece (8 * size, 0); |
87808bd6 | 1247 | |
cec03d70 TT |
1248 | /* Pop off the address/regnum, and reset the location |
1249 | type. */ | |
595d2e30 TT |
1250 | if (this->location != DWARF_VALUE_LITERAL |
1251 | && this->location != DWARF_VALUE_OPTIMIZED_OUT) | |
1252 | pop (); | |
1253 | this->location = DWARF_VALUE_MEMORY; | |
87808bd6 JB |
1254 | } |
1255 | goto no_push; | |
1256 | ||
d3b1e874 TT |
1257 | case DW_OP_bit_piece: |
1258 | { | |
9fccedf7 | 1259 | uint64_t size, offset; |
d3b1e874 TT |
1260 | |
1261 | /* Record the piece. */ | |
f664829e DE |
1262 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &size); |
1263 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset); | |
595d2e30 | 1264 | add_piece (size, offset); |
d3b1e874 TT |
1265 | |
1266 | /* Pop off the address/regnum, and reset the location | |
1267 | type. */ | |
595d2e30 TT |
1268 | if (this->location != DWARF_VALUE_LITERAL |
1269 | && this->location != DWARF_VALUE_OPTIMIZED_OUT) | |
1270 | pop (); | |
1271 | this->location = DWARF_VALUE_MEMORY; | |
d3b1e874 TT |
1272 | } |
1273 | goto no_push; | |
1274 | ||
42be36b3 CT |
1275 | case DW_OP_GNU_uninit: |
1276 | if (op_ptr != op_end) | |
9c482037 | 1277 | error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always " |
42be36b3 CT |
1278 | "be the very last op.")); |
1279 | ||
595d2e30 | 1280 | this->initialized = 0; |
42be36b3 CT |
1281 | goto no_push; |
1282 | ||
5c631832 | 1283 | case DW_OP_call2: |
b64f50a1 | 1284 | { |
9c541725 PA |
1285 | cu_offset cu_off |
1286 | = (cu_offset) extract_unsigned_integer (op_ptr, 2, byte_order); | |
b64f50a1 | 1287 | op_ptr += 2; |
9c541725 | 1288 | this->dwarf_call (cu_off); |
b64f50a1 | 1289 | } |
5c631832 JK |
1290 | goto no_push; |
1291 | ||
1292 | case DW_OP_call4: | |
b64f50a1 | 1293 | { |
9c541725 PA |
1294 | cu_offset cu_off |
1295 | = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order); | |
b64f50a1 | 1296 | op_ptr += 4; |
9c541725 | 1297 | this->dwarf_call (cu_off); |
b64f50a1 | 1298 | } |
5c631832 | 1299 | goto no_push; |
dd90784c | 1300 | |
216f72a1 | 1301 | case DW_OP_entry_value: |
dd90784c | 1302 | case DW_OP_GNU_entry_value: |
8e3b41a9 | 1303 | { |
9fccedf7 | 1304 | uint64_t len; |
8e3b41a9 | 1305 | CORE_ADDR deref_size; |
24c5c679 | 1306 | union call_site_parameter_u kind_u; |
8e3b41a9 | 1307 | |
f664829e | 1308 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &len); |
8e3b41a9 | 1309 | if (op_ptr + len > op_end) |
216f72a1 | 1310 | error (_("DW_OP_entry_value: too few bytes available.")); |
8e3b41a9 | 1311 | |
24c5c679 JK |
1312 | kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len); |
1313 | if (kind_u.dwarf_reg != -1) | |
8e3b41a9 JK |
1314 | { |
1315 | op_ptr += len; | |
192ca6d8 TT |
1316 | this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG, |
1317 | kind_u, | |
1318 | -1 /* deref_size */); | |
a471c594 JK |
1319 | goto no_push; |
1320 | } | |
1321 | ||
24c5c679 JK |
1322 | kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr, |
1323 | op_ptr + len, | |
1324 | &deref_size); | |
1325 | if (kind_u.dwarf_reg != -1) | |
a471c594 JK |
1326 | { |
1327 | if (deref_size == -1) | |
595d2e30 | 1328 | deref_size = this->addr_size; |
a471c594 | 1329 | op_ptr += len; |
192ca6d8 TT |
1330 | this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG, |
1331 | kind_u, deref_size); | |
8e3b41a9 JK |
1332 | goto no_push; |
1333 | } | |
1334 | ||
216f72a1 | 1335 | error (_("DWARF-2 expression error: DW_OP_entry_value is " |
a471c594 JK |
1336 | "supported only for single DW_OP_reg* " |
1337 | "or for DW_OP_breg*(0)+DW_OP_deref*")); | |
8e3b41a9 | 1338 | } |
5c631832 | 1339 | |
1788b2d3 JK |
1340 | case DW_OP_GNU_parameter_ref: |
1341 | { | |
1342 | union call_site_parameter_u kind_u; | |
1343 | ||
9c541725 PA |
1344 | kind_u.param_cu_off |
1345 | = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order); | |
1788b2d3 | 1346 | op_ptr += 4; |
192ca6d8 TT |
1347 | this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET, |
1348 | kind_u, | |
1349 | -1 /* deref_size */); | |
1788b2d3 JK |
1350 | } |
1351 | goto no_push; | |
1352 | ||
216f72a1 | 1353 | case DW_OP_const_type: |
8a9b8146 TT |
1354 | case DW_OP_GNU_const_type: |
1355 | { | |
8a9b8146 TT |
1356 | int n; |
1357 | const gdb_byte *data; | |
1358 | struct type *type; | |
1359 | ||
f664829e | 1360 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); |
9c541725 PA |
1361 | cu_offset type_die_cu_off = (cu_offset) uoffset; |
1362 | ||
8a9b8146 TT |
1363 | n = *op_ptr++; |
1364 | data = op_ptr; | |
1365 | op_ptr += n; | |
1366 | ||
9c541725 | 1367 | type = get_base_type (type_die_cu_off, n); |
8a9b8146 TT |
1368 | result_val = value_from_contents (type, data); |
1369 | } | |
1370 | break; | |
1371 | ||
216f72a1 | 1372 | case DW_OP_regval_type: |
8a9b8146 TT |
1373 | case DW_OP_GNU_regval_type: |
1374 | { | |
8a9b8146 TT |
1375 | struct type *type; |
1376 | ||
f664829e DE |
1377 | op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); |
1378 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); | |
9c541725 | 1379 | cu_offset type_die_cu_off = (cu_offset) uoffset; |
8a9b8146 | 1380 | |
9c541725 | 1381 | type = get_base_type (type_die_cu_off, 0); |
192ca6d8 | 1382 | result_val = this->get_reg_value (type, reg); |
8a9b8146 TT |
1383 | } |
1384 | break; | |
1385 | ||
216f72a1 | 1386 | case DW_OP_convert: |
8a9b8146 | 1387 | case DW_OP_GNU_convert: |
216f72a1 | 1388 | case DW_OP_reinterpret: |
8a9b8146 TT |
1389 | case DW_OP_GNU_reinterpret: |
1390 | { | |
8a9b8146 TT |
1391 | struct type *type; |
1392 | ||
f664829e | 1393 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); |
9c541725 | 1394 | cu_offset type_die_cu_off = (cu_offset) uoffset; |
8a9b8146 | 1395 | |
9c541725 | 1396 | if (to_underlying (type_die_cu_off) == 0) |
c38c4bc5 TT |
1397 | type = address_type; |
1398 | else | |
9c541725 | 1399 | type = get_base_type (type_die_cu_off, 0); |
8a9b8146 | 1400 | |
595d2e30 TT |
1401 | result_val = fetch (0); |
1402 | pop (); | |
8a9b8146 | 1403 | |
216f72a1 | 1404 | if (op == DW_OP_convert || op == DW_OP_GNU_convert) |
8a9b8146 TT |
1405 | result_val = value_cast (type, result_val); |
1406 | else if (type == value_type (result_val)) | |
1407 | { | |
1408 | /* Nothing. */ | |
1409 | } | |
1410 | else if (TYPE_LENGTH (type) | |
1411 | != TYPE_LENGTH (value_type (result_val))) | |
216f72a1 | 1412 | error (_("DW_OP_reinterpret has wrong size")); |
8a9b8146 TT |
1413 | else |
1414 | result_val | |
1415 | = value_from_contents (type, | |
1416 | value_contents_all (result_val)); | |
1417 | } | |
1418 | break; | |
1419 | ||
08412b07 JB |
1420 | case DW_OP_push_object_address: |
1421 | /* Return the address of the object we are currently observing. */ | |
192ca6d8 | 1422 | result = this->get_object_address (); |
08412b07 JB |
1423 | result_val = value_from_ulongest (address_type, result); |
1424 | break; | |
1425 | ||
4c2df51b | 1426 | default: |
8a3fe4f8 | 1427 | error (_("Unhandled dwarf expression opcode 0x%x"), op); |
4c2df51b DJ |
1428 | } |
1429 | ||
1430 | /* Most things push a result value. */ | |
8a9b8146 | 1431 | gdb_assert (result_val != NULL); |
595d2e30 | 1432 | push (result_val, in_stack_memory); |
82ae4854 | 1433 | no_push: |
b27cf2b3 | 1434 | ; |
4c2df51b | 1435 | } |
1e3a102a | 1436 | |
8cf6f0b1 TT |
1437 | /* To simplify our main caller, if the result is an implicit |
1438 | pointer, then make a pieced value. This is ok because we can't | |
1439 | have implicit pointers in contexts where pieces are invalid. */ | |
595d2e30 TT |
1440 | if (this->location == DWARF_VALUE_IMPLICIT_POINTER) |
1441 | add_piece (8 * this->addr_size, 0); | |
8cf6f0b1 | 1442 | |
dd90784c | 1443 | abort_expression: |
595d2e30 TT |
1444 | this->recursion_depth--; |
1445 | gdb_assert (this->recursion_depth >= 0); | |
8a9b8146 TT |
1446 | } |
1447 | ||
70221824 PA |
1448 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
1449 | extern initialize_file_ftype _initialize_dwarf2expr; | |
1450 | ||
8a9b8146 TT |
1451 | void |
1452 | _initialize_dwarf2expr (void) | |
1453 | { | |
1454 | dwarf_arch_cookie | |
1455 | = gdbarch_data_register_post_init (dwarf_gdbarch_types_init); | |
4c2df51b | 1456 | } |