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