Commit | Line | Data |
---|---|---|
852483bc MK |
1 | /* DWARF 2 Expression Evaluator. |
2 | ||
4c38e0a4 | 3 | Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010 |
9b254dd1 | 4 | Free Software Foundation, Inc. |
852483bc | 5 | |
4c2df51b DJ |
6 | Contributed by Daniel Berlin (dan@dberlin.org) |
7 | ||
8 | This file is part of GDB. | |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
4c2df51b DJ |
13 | (at your option) any later version. |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4c2df51b DJ |
22 | |
23 | #include "defs.h" | |
24 | #include "symtab.h" | |
25 | #include "gdbtypes.h" | |
26 | #include "value.h" | |
27 | #include "gdbcore.h" | |
fa8f86ff | 28 | #include "dwarf2.h" |
4c2df51b | 29 | #include "dwarf2expr.h" |
1e3a102a | 30 | #include "gdb_assert.h" |
4c2df51b DJ |
31 | |
32 | /* Local prototypes. */ | |
33 | ||
34 | static void execute_stack_op (struct dwarf_expr_context *, | |
0d45f56e | 35 | const gdb_byte *, const gdb_byte *); |
4c2df51b DJ |
36 | |
37 | /* Create a new context for the expression evaluator. */ | |
38 | ||
39 | struct dwarf_expr_context * | |
e4adbba9 | 40 | new_dwarf_expr_context (void) |
4c2df51b DJ |
41 | { |
42 | struct dwarf_expr_context *retval; | |
9a619af0 | 43 | |
4c2df51b | 44 | retval = xcalloc (1, sizeof (struct dwarf_expr_context)); |
18ec9831 KB |
45 | retval->stack_len = 0; |
46 | retval->stack_allocated = 10; | |
b966cb8a TT |
47 | retval->stack = xmalloc (retval->stack_allocated |
48 | * sizeof (struct dwarf_stack_value)); | |
87808bd6 JB |
49 | retval->num_pieces = 0; |
50 | retval->pieces = 0; | |
1e3a102a | 51 | retval->max_recursion_depth = 0x100; |
4c2df51b DJ |
52 | return retval; |
53 | } | |
54 | ||
55 | /* Release the memory allocated to CTX. */ | |
56 | ||
57 | void | |
58 | free_dwarf_expr_context (struct dwarf_expr_context *ctx) | |
59 | { | |
60 | xfree (ctx->stack); | |
87808bd6 | 61 | xfree (ctx->pieces); |
4c2df51b DJ |
62 | xfree (ctx); |
63 | } | |
64 | ||
4a227398 TT |
65 | /* Helper for make_cleanup_free_dwarf_expr_context. */ |
66 | ||
67 | static void | |
68 | free_dwarf_expr_context_cleanup (void *arg) | |
69 | { | |
70 | free_dwarf_expr_context (arg); | |
71 | } | |
72 | ||
73 | /* Return a cleanup that calls free_dwarf_expr_context. */ | |
74 | ||
75 | struct cleanup * | |
76 | make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx) | |
77 | { | |
78 | return make_cleanup (free_dwarf_expr_context_cleanup, ctx); | |
79 | } | |
80 | ||
4c2df51b DJ |
81 | /* Expand the memory allocated to CTX's stack to contain at least |
82 | NEED more elements than are currently used. */ | |
83 | ||
84 | static void | |
85 | dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need) | |
86 | { | |
87 | if (ctx->stack_len + need > ctx->stack_allocated) | |
88 | { | |
18ec9831 | 89 | size_t newlen = ctx->stack_len + need + 10; |
9a619af0 | 90 | |
4c2df51b | 91 | ctx->stack = xrealloc (ctx->stack, |
44353522 | 92 | newlen * sizeof (struct dwarf_stack_value)); |
18ec9831 | 93 | ctx->stack_allocated = newlen; |
4c2df51b DJ |
94 | } |
95 | } | |
96 | ||
97 | /* Push VALUE onto CTX's stack. */ | |
98 | ||
99 | void | |
f2c7657e | 100 | dwarf_expr_push (struct dwarf_expr_context *ctx, ULONGEST value, |
44353522 | 101 | int in_stack_memory) |
4c2df51b | 102 | { |
44353522 DE |
103 | struct dwarf_stack_value *v; |
104 | ||
f2c7657e UW |
105 | /* We keep all stack elements within the range defined by the |
106 | DWARF address size. */ | |
107 | if (ctx->addr_size < sizeof (ULONGEST)) | |
108 | value &= ((ULONGEST) 1 << (ctx->addr_size * HOST_CHAR_BIT)) - 1; | |
109 | ||
4c2df51b | 110 | dwarf_expr_grow_stack (ctx, 1); |
44353522 DE |
111 | v = &ctx->stack[ctx->stack_len++]; |
112 | v->value = value; | |
113 | v->in_stack_memory = in_stack_memory; | |
4c2df51b DJ |
114 | } |
115 | ||
116 | /* Pop the top item off of CTX's stack. */ | |
117 | ||
118 | void | |
119 | dwarf_expr_pop (struct dwarf_expr_context *ctx) | |
120 | { | |
121 | if (ctx->stack_len <= 0) | |
8a3fe4f8 | 122 | error (_("dwarf expression stack underflow")); |
4c2df51b DJ |
123 | ctx->stack_len--; |
124 | } | |
125 | ||
126 | /* Retrieve the N'th item on CTX's stack. */ | |
127 | ||
f2c7657e | 128 | ULONGEST |
4c2df51b DJ |
129 | dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n) |
130 | { | |
ef0fdf07 | 131 | if (ctx->stack_len <= n) |
8a3fe4f8 | 132 | error (_("Asked for position %d of stack, stack only has %d elements on it."), |
4c2df51b | 133 | n, ctx->stack_len); |
44353522 DE |
134 | return ctx->stack[ctx->stack_len - (1 + n)].value; |
135 | ||
136 | } | |
137 | ||
f2c7657e UW |
138 | /* Retrieve the N'th item on CTX's stack, converted to an address. */ |
139 | ||
140 | CORE_ADDR | |
141 | dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n) | |
142 | { | |
143 | ULONGEST result = dwarf_expr_fetch (ctx, n); | |
144 | ||
145 | /* For most architectures, calling extract_unsigned_integer() alone | |
146 | is sufficient for extracting an address. However, some | |
147 | architectures (e.g. MIPS) use signed addresses and using | |
148 | extract_unsigned_integer() will not produce a correct | |
149 | result. Make sure we invoke gdbarch_integer_to_address() | |
150 | for those architectures which require it. */ | |
151 | if (gdbarch_integer_to_address_p (ctx->gdbarch)) | |
152 | { | |
153 | enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch); | |
154 | gdb_byte *buf = alloca (ctx->addr_size); | |
155 | struct type *int_type; | |
156 | ||
157 | switch (ctx->addr_size) | |
158 | { | |
159 | case 2: | |
160 | int_type = builtin_type (ctx->gdbarch)->builtin_uint16; | |
161 | break; | |
162 | case 4: | |
163 | int_type = builtin_type (ctx->gdbarch)->builtin_uint32; | |
164 | break; | |
165 | case 8: | |
166 | int_type = builtin_type (ctx->gdbarch)->builtin_uint64; | |
167 | break; | |
168 | default: | |
169 | internal_error (__FILE__, __LINE__, | |
170 | _("Unsupported address size.\n")); | |
171 | } | |
172 | ||
173 | store_unsigned_integer (buf, ctx->addr_size, byte_order, result); | |
174 | return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf); | |
175 | } | |
176 | ||
177 | return (CORE_ADDR) result; | |
178 | } | |
179 | ||
44353522 DE |
180 | /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */ |
181 | ||
182 | int | |
183 | dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n) | |
184 | { | |
185 | if (ctx->stack_len <= n) | |
186 | error (_("Asked for position %d of stack, stack only has %d elements on it."), | |
187 | n, ctx->stack_len); | |
188 | return ctx->stack[ctx->stack_len - (1 + n)].in_stack_memory; | |
4c2df51b DJ |
189 | |
190 | } | |
191 | ||
cb826367 TT |
192 | /* Return true if the expression stack is empty. */ |
193 | ||
194 | static int | |
195 | dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx) | |
196 | { | |
197 | return ctx->stack_len == 0; | |
198 | } | |
199 | ||
87808bd6 JB |
200 | /* Add a new piece to CTX's piece list. */ |
201 | static void | |
d3b1e874 | 202 | add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset) |
87808bd6 JB |
203 | { |
204 | struct dwarf_expr_piece *p; | |
205 | ||
206 | ctx->num_pieces++; | |
207 | ||
d3b1e874 TT |
208 | ctx->pieces = xrealloc (ctx->pieces, |
209 | (ctx->num_pieces | |
210 | * sizeof (struct dwarf_expr_piece))); | |
87808bd6 JB |
211 | |
212 | p = &ctx->pieces[ctx->num_pieces - 1]; | |
cec03d70 | 213 | p->location = ctx->location; |
87808bd6 | 214 | p->size = size; |
d3b1e874 TT |
215 | p->offset = offset; |
216 | ||
cec03d70 TT |
217 | if (p->location == DWARF_VALUE_LITERAL) |
218 | { | |
219 | p->v.literal.data = ctx->data; | |
220 | p->v.literal.length = ctx->len; | |
221 | } | |
cb826367 TT |
222 | else if (dwarf_expr_stack_empty_p (ctx)) |
223 | { | |
224 | p->location = DWARF_VALUE_OPTIMIZED_OUT; | |
225 | /* Also reset the context's location, for our callers. This is | |
226 | a somewhat strange approach, but this lets us avoid setting | |
227 | the location to DWARF_VALUE_MEMORY in all the individual | |
228 | cases in the evaluator. */ | |
229 | ctx->location = DWARF_VALUE_OPTIMIZED_OUT; | |
230 | } | |
f2c7657e UW |
231 | else if (p->location == DWARF_VALUE_MEMORY) |
232 | { | |
233 | p->v.mem.addr = dwarf_expr_fetch_address (ctx, 0); | |
234 | p->v.mem.in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0); | |
235 | } | |
cec03d70 | 236 | else |
44353522 | 237 | { |
f2c7657e | 238 | p->v.value = dwarf_expr_fetch (ctx, 0); |
44353522 | 239 | } |
87808bd6 JB |
240 | } |
241 | ||
4c2df51b DJ |
242 | /* Evaluate the expression at ADDR (LEN bytes long) using the context |
243 | CTX. */ | |
244 | ||
245 | void | |
0d45f56e TT |
246 | dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr, |
247 | size_t len) | |
4c2df51b | 248 | { |
1e3a102a JK |
249 | int old_recursion_depth = ctx->recursion_depth; |
250 | ||
4c2df51b | 251 | execute_stack_op (ctx, addr, addr + len); |
1e3a102a JK |
252 | |
253 | /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */ | |
254 | ||
255 | gdb_assert (ctx->recursion_depth == old_recursion_depth); | |
4c2df51b DJ |
256 | } |
257 | ||
258 | /* Decode the unsigned LEB128 constant at BUF into the variable pointed to | |
259 | by R, and return the new value of BUF. Verify that it doesn't extend | |
260 | past BUF_END. */ | |
261 | ||
0d45f56e TT |
262 | const gdb_byte * |
263 | read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, ULONGEST * r) | |
4c2df51b DJ |
264 | { |
265 | unsigned shift = 0; | |
266 | ULONGEST result = 0; | |
852483bc | 267 | gdb_byte byte; |
4c2df51b DJ |
268 | |
269 | while (1) | |
270 | { | |
271 | if (buf >= buf_end) | |
8a3fe4f8 | 272 | error (_("read_uleb128: Corrupted DWARF expression.")); |
4c2df51b DJ |
273 | |
274 | byte = *buf++; | |
275 | result |= (byte & 0x7f) << shift; | |
276 | if ((byte & 0x80) == 0) | |
277 | break; | |
278 | shift += 7; | |
279 | } | |
280 | *r = result; | |
281 | return buf; | |
282 | } | |
283 | ||
284 | /* Decode the signed LEB128 constant at BUF into the variable pointed to | |
285 | by R, and return the new value of BUF. Verify that it doesn't extend | |
286 | past BUF_END. */ | |
287 | ||
0d45f56e TT |
288 | const gdb_byte * |
289 | read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, LONGEST * r) | |
4c2df51b DJ |
290 | { |
291 | unsigned shift = 0; | |
292 | LONGEST result = 0; | |
852483bc | 293 | gdb_byte byte; |
4c2df51b DJ |
294 | |
295 | while (1) | |
296 | { | |
297 | if (buf >= buf_end) | |
8a3fe4f8 | 298 | error (_("read_sleb128: Corrupted DWARF expression.")); |
4c2df51b DJ |
299 | |
300 | byte = *buf++; | |
301 | result |= (byte & 0x7f) << shift; | |
302 | shift += 7; | |
303 | if ((byte & 0x80) == 0) | |
304 | break; | |
305 | } | |
306 | if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0) | |
307 | result |= -(1 << shift); | |
308 | ||
309 | *r = result; | |
310 | return buf; | |
311 | } | |
4c2df51b | 312 | \f |
cec03d70 TT |
313 | |
314 | /* Check that the current operator is either at the end of an | |
315 | expression, or that it is followed by a composition operator. */ | |
316 | ||
3cf03773 TT |
317 | void |
318 | dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end, | |
319 | const char *op_name) | |
cec03d70 TT |
320 | { |
321 | /* It seems like DW_OP_GNU_uninit should be handled here. However, | |
322 | it doesn't seem to make sense for DW_OP_*_value, and it was not | |
323 | checked at the other place that this function is called. */ | |
324 | if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece) | |
325 | error (_("DWARF-2 expression error: `%s' operations must be " | |
326 | "used either alone or in conjuction with DW_OP_piece " | |
327 | "or DW_OP_bit_piece."), | |
328 | op_name); | |
329 | } | |
330 | ||
4c2df51b DJ |
331 | /* The engine for the expression evaluator. Using the context in CTX, |
332 | evaluate the expression between OP_PTR and OP_END. */ | |
333 | ||
334 | static void | |
852483bc | 335 | execute_stack_op (struct dwarf_expr_context *ctx, |
0d45f56e | 336 | const gdb_byte *op_ptr, const gdb_byte *op_end) |
4c2df51b | 337 | { |
43dabe42 | 338 | #define sign_ext(x) ((LONGEST) (((x) ^ sign_bit) - sign_bit)) |
f2c7657e UW |
339 | ULONGEST sign_bit = (ctx->addr_size >= sizeof (ULONGEST) ? 0 |
340 | : ((ULONGEST) 1) << (ctx->addr_size * 8 - 1)); | |
e17a4113 | 341 | enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch); |
9a619af0 | 342 | |
cec03d70 | 343 | ctx->location = DWARF_VALUE_MEMORY; |
42be36b3 | 344 | ctx->initialized = 1; /* Default is initialized. */ |
18ec9831 | 345 | |
1e3a102a JK |
346 | if (ctx->recursion_depth > ctx->max_recursion_depth) |
347 | error (_("DWARF-2 expression error: Loop detected (%d)."), | |
348 | ctx->recursion_depth); | |
349 | ctx->recursion_depth++; | |
350 | ||
4c2df51b DJ |
351 | while (op_ptr < op_end) |
352 | { | |
353 | enum dwarf_location_atom op = *op_ptr++; | |
f2c7657e | 354 | ULONGEST result; |
44353522 DE |
355 | /* Assume the value is not in stack memory. |
356 | Code that knows otherwise sets this to 1. | |
357 | Some arithmetic on stack addresses can probably be assumed to still | |
358 | be a stack address, but we skip this complication for now. | |
359 | This is just an optimization, so it's always ok to punt | |
360 | and leave this as 0. */ | |
361 | int in_stack_memory = 0; | |
4c2df51b DJ |
362 | ULONGEST uoffset, reg; |
363 | LONGEST offset; | |
4c2df51b | 364 | |
4c2df51b DJ |
365 | switch (op) |
366 | { | |
367 | case DW_OP_lit0: | |
368 | case DW_OP_lit1: | |
369 | case DW_OP_lit2: | |
370 | case DW_OP_lit3: | |
371 | case DW_OP_lit4: | |
372 | case DW_OP_lit5: | |
373 | case DW_OP_lit6: | |
374 | case DW_OP_lit7: | |
375 | case DW_OP_lit8: | |
376 | case DW_OP_lit9: | |
377 | case DW_OP_lit10: | |
378 | case DW_OP_lit11: | |
379 | case DW_OP_lit12: | |
380 | case DW_OP_lit13: | |
381 | case DW_OP_lit14: | |
382 | case DW_OP_lit15: | |
383 | case DW_OP_lit16: | |
384 | case DW_OP_lit17: | |
385 | case DW_OP_lit18: | |
386 | case DW_OP_lit19: | |
387 | case DW_OP_lit20: | |
388 | case DW_OP_lit21: | |
389 | case DW_OP_lit22: | |
390 | case DW_OP_lit23: | |
391 | case DW_OP_lit24: | |
392 | case DW_OP_lit25: | |
393 | case DW_OP_lit26: | |
394 | case DW_OP_lit27: | |
395 | case DW_OP_lit28: | |
396 | case DW_OP_lit29: | |
397 | case DW_OP_lit30: | |
398 | case DW_OP_lit31: | |
399 | result = op - DW_OP_lit0; | |
400 | break; | |
401 | ||
402 | case DW_OP_addr: | |
f2c7657e UW |
403 | result = extract_unsigned_integer (op_ptr, |
404 | ctx->addr_size, byte_order); | |
ae0d2f24 | 405 | op_ptr += ctx->addr_size; |
ac56253d TT |
406 | /* Some versions of GCC emit DW_OP_addr before |
407 | DW_OP_GNU_push_tls_address. In this case the value is an | |
408 | index, not an address. We don't support things like | |
409 | branching between the address and the TLS op. */ | |
410 | if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address) | |
411 | result += ctx->offset; | |
4c2df51b DJ |
412 | break; |
413 | ||
414 | case DW_OP_const1u: | |
e17a4113 | 415 | result = extract_unsigned_integer (op_ptr, 1, byte_order); |
4c2df51b DJ |
416 | op_ptr += 1; |
417 | break; | |
418 | case DW_OP_const1s: | |
e17a4113 | 419 | result = extract_signed_integer (op_ptr, 1, byte_order); |
4c2df51b DJ |
420 | op_ptr += 1; |
421 | break; | |
422 | case DW_OP_const2u: | |
e17a4113 | 423 | result = extract_unsigned_integer (op_ptr, 2, byte_order); |
4c2df51b DJ |
424 | op_ptr += 2; |
425 | break; | |
426 | case DW_OP_const2s: | |
e17a4113 | 427 | result = extract_signed_integer (op_ptr, 2, byte_order); |
4c2df51b DJ |
428 | op_ptr += 2; |
429 | break; | |
430 | case DW_OP_const4u: | |
e17a4113 | 431 | result = extract_unsigned_integer (op_ptr, 4, byte_order); |
4c2df51b DJ |
432 | op_ptr += 4; |
433 | break; | |
434 | case DW_OP_const4s: | |
e17a4113 | 435 | result = extract_signed_integer (op_ptr, 4, byte_order); |
4c2df51b DJ |
436 | op_ptr += 4; |
437 | break; | |
438 | case DW_OP_const8u: | |
e17a4113 | 439 | result = extract_unsigned_integer (op_ptr, 8, byte_order); |
4c2df51b DJ |
440 | op_ptr += 8; |
441 | break; | |
442 | case DW_OP_const8s: | |
e17a4113 | 443 | result = extract_signed_integer (op_ptr, 8, byte_order); |
4c2df51b DJ |
444 | op_ptr += 8; |
445 | break; | |
446 | case DW_OP_constu: | |
447 | op_ptr = read_uleb128 (op_ptr, op_end, &uoffset); | |
448 | result = uoffset; | |
449 | break; | |
450 | case DW_OP_consts: | |
451 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
452 | result = offset; | |
453 | break; | |
454 | ||
455 | /* The DW_OP_reg operations are required to occur alone in | |
456 | location expressions. */ | |
457 | case DW_OP_reg0: | |
458 | case DW_OP_reg1: | |
459 | case DW_OP_reg2: | |
460 | case DW_OP_reg3: | |
461 | case DW_OP_reg4: | |
462 | case DW_OP_reg5: | |
463 | case DW_OP_reg6: | |
464 | case DW_OP_reg7: | |
465 | case DW_OP_reg8: | |
466 | case DW_OP_reg9: | |
467 | case DW_OP_reg10: | |
468 | case DW_OP_reg11: | |
469 | case DW_OP_reg12: | |
470 | case DW_OP_reg13: | |
471 | case DW_OP_reg14: | |
472 | case DW_OP_reg15: | |
473 | case DW_OP_reg16: | |
474 | case DW_OP_reg17: | |
475 | case DW_OP_reg18: | |
476 | case DW_OP_reg19: | |
477 | case DW_OP_reg20: | |
478 | case DW_OP_reg21: | |
479 | case DW_OP_reg22: | |
480 | case DW_OP_reg23: | |
481 | case DW_OP_reg24: | |
482 | case DW_OP_reg25: | |
483 | case DW_OP_reg26: | |
484 | case DW_OP_reg27: | |
485 | case DW_OP_reg28: | |
486 | case DW_OP_reg29: | |
487 | case DW_OP_reg30: | |
488 | case DW_OP_reg31: | |
42be36b3 CT |
489 | if (op_ptr != op_end |
490 | && *op_ptr != DW_OP_piece | |
d3b1e874 | 491 | && *op_ptr != DW_OP_bit_piece |
42be36b3 | 492 | && *op_ptr != DW_OP_GNU_uninit) |
8a3fe4f8 | 493 | error (_("DWARF-2 expression error: DW_OP_reg operations must be " |
d3b1e874 TT |
494 | "used either alone or in conjuction with DW_OP_piece " |
495 | "or DW_OP_bit_piece.")); | |
4c2df51b | 496 | |
61fbb938 | 497 | result = op - DW_OP_reg0; |
cec03d70 | 498 | ctx->location = DWARF_VALUE_REGISTER; |
4c2df51b DJ |
499 | break; |
500 | ||
501 | case DW_OP_regx: | |
502 | op_ptr = read_uleb128 (op_ptr, op_end, ®); | |
3cf03773 | 503 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx"); |
4c2df51b | 504 | |
61fbb938 | 505 | result = reg; |
cec03d70 | 506 | ctx->location = DWARF_VALUE_REGISTER; |
4c2df51b DJ |
507 | break; |
508 | ||
cec03d70 TT |
509 | case DW_OP_implicit_value: |
510 | { | |
511 | ULONGEST len; | |
9a619af0 | 512 | |
cec03d70 TT |
513 | op_ptr = read_uleb128 (op_ptr, op_end, &len); |
514 | if (op_ptr + len > op_end) | |
515 | error (_("DW_OP_implicit_value: too few bytes available.")); | |
516 | ctx->len = len; | |
517 | ctx->data = op_ptr; | |
518 | ctx->location = DWARF_VALUE_LITERAL; | |
519 | op_ptr += len; | |
3cf03773 TT |
520 | dwarf_expr_require_composition (op_ptr, op_end, |
521 | "DW_OP_implicit_value"); | |
cec03d70 TT |
522 | } |
523 | goto no_push; | |
524 | ||
525 | case DW_OP_stack_value: | |
526 | ctx->location = DWARF_VALUE_STACK; | |
3cf03773 | 527 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value"); |
cec03d70 TT |
528 | goto no_push; |
529 | ||
4c2df51b DJ |
530 | case DW_OP_breg0: |
531 | case DW_OP_breg1: | |
532 | case DW_OP_breg2: | |
533 | case DW_OP_breg3: | |
534 | case DW_OP_breg4: | |
535 | case DW_OP_breg5: | |
536 | case DW_OP_breg6: | |
537 | case DW_OP_breg7: | |
538 | case DW_OP_breg8: | |
539 | case DW_OP_breg9: | |
540 | case DW_OP_breg10: | |
541 | case DW_OP_breg11: | |
542 | case DW_OP_breg12: | |
543 | case DW_OP_breg13: | |
544 | case DW_OP_breg14: | |
545 | case DW_OP_breg15: | |
546 | case DW_OP_breg16: | |
547 | case DW_OP_breg17: | |
548 | case DW_OP_breg18: | |
549 | case DW_OP_breg19: | |
550 | case DW_OP_breg20: | |
551 | case DW_OP_breg21: | |
552 | case DW_OP_breg22: | |
553 | case DW_OP_breg23: | |
554 | case DW_OP_breg24: | |
555 | case DW_OP_breg25: | |
556 | case DW_OP_breg26: | |
557 | case DW_OP_breg27: | |
558 | case DW_OP_breg28: | |
559 | case DW_OP_breg29: | |
560 | case DW_OP_breg30: | |
561 | case DW_OP_breg31: | |
562 | { | |
563 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
61fbb938 | 564 | result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0); |
4c2df51b DJ |
565 | result += offset; |
566 | } | |
567 | break; | |
568 | case DW_OP_bregx: | |
569 | { | |
570 | op_ptr = read_uleb128 (op_ptr, op_end, ®); | |
571 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
61fbb938 | 572 | result = (ctx->read_reg) (ctx->baton, reg); |
4c2df51b DJ |
573 | result += offset; |
574 | } | |
575 | break; | |
576 | case DW_OP_fbreg: | |
577 | { | |
0d45f56e | 578 | const gdb_byte *datastart; |
4c2df51b DJ |
579 | size_t datalen; |
580 | unsigned int before_stack_len; | |
581 | ||
582 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
583 | /* Rather than create a whole new context, we simply | |
584 | record the stack length before execution, then reset it | |
585 | afterwards, effectively erasing whatever the recursive | |
586 | call put there. */ | |
587 | before_stack_len = ctx->stack_len; | |
da62e633 AC |
588 | /* FIXME: cagney/2003-03-26: This code should be using |
589 | get_frame_base_address(), and then implement a dwarf2 | |
590 | specific this_base method. */ | |
4c2df51b DJ |
591 | (ctx->get_frame_base) (ctx->baton, &datastart, &datalen); |
592 | dwarf_expr_eval (ctx, datastart, datalen); | |
f2c7657e UW |
593 | if (ctx->location == DWARF_VALUE_MEMORY) |
594 | result = dwarf_expr_fetch_address (ctx, 0); | |
595 | else if (ctx->location == DWARF_VALUE_REGISTER) | |
596 | result = (ctx->read_reg) (ctx->baton, dwarf_expr_fetch (ctx, 0)); | |
597 | else | |
cec03d70 | 598 | error (_("Not implemented: computing frame base using explicit value operator")); |
4c2df51b | 599 | result = result + offset; |
44353522 | 600 | in_stack_memory = 1; |
4c2df51b | 601 | ctx->stack_len = before_stack_len; |
cec03d70 | 602 | ctx->location = DWARF_VALUE_MEMORY; |
4c2df51b DJ |
603 | } |
604 | break; | |
44353522 | 605 | |
4c2df51b DJ |
606 | case DW_OP_dup: |
607 | result = dwarf_expr_fetch (ctx, 0); | |
44353522 | 608 | in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0); |
4c2df51b DJ |
609 | break; |
610 | ||
611 | case DW_OP_drop: | |
612 | dwarf_expr_pop (ctx); | |
613 | goto no_push; | |
614 | ||
615 | case DW_OP_pick: | |
616 | offset = *op_ptr++; | |
617 | result = dwarf_expr_fetch (ctx, offset); | |
44353522 | 618 | in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, offset); |
4c2df51b | 619 | break; |
9f3fe11c TG |
620 | |
621 | case DW_OP_swap: | |
622 | { | |
44353522 | 623 | struct dwarf_stack_value t1, t2; |
9f3fe11c TG |
624 | |
625 | if (ctx->stack_len < 2) | |
626 | error (_("Not enough elements for DW_OP_swap. Need 2, have %d."), | |
627 | ctx->stack_len); | |
628 | t1 = ctx->stack[ctx->stack_len - 1]; | |
629 | t2 = ctx->stack[ctx->stack_len - 2]; | |
630 | ctx->stack[ctx->stack_len - 1] = t2; | |
631 | ctx->stack[ctx->stack_len - 2] = t1; | |
632 | goto no_push; | |
633 | } | |
4c2df51b DJ |
634 | |
635 | case DW_OP_over: | |
636 | result = dwarf_expr_fetch (ctx, 1); | |
44353522 | 637 | in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 1); |
4c2df51b DJ |
638 | break; |
639 | ||
640 | case DW_OP_rot: | |
641 | { | |
44353522 | 642 | struct dwarf_stack_value t1, t2, t3; |
4c2df51b DJ |
643 | |
644 | if (ctx->stack_len < 3) | |
8a3fe4f8 | 645 | error (_("Not enough elements for DW_OP_rot. Need 3, have %d."), |
4c2df51b DJ |
646 | ctx->stack_len); |
647 | t1 = ctx->stack[ctx->stack_len - 1]; | |
648 | t2 = ctx->stack[ctx->stack_len - 2]; | |
649 | t3 = ctx->stack[ctx->stack_len - 3]; | |
650 | ctx->stack[ctx->stack_len - 1] = t2; | |
651 | ctx->stack[ctx->stack_len - 2] = t3; | |
652 | ctx->stack[ctx->stack_len - 3] = t1; | |
653 | goto no_push; | |
654 | } | |
655 | ||
656 | case DW_OP_deref: | |
657 | case DW_OP_deref_size: | |
f2c7657e UW |
658 | { |
659 | int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++); | |
660 | gdb_byte *buf = alloca (addr_size); | |
661 | CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0); | |
662 | dwarf_expr_pop (ctx); | |
663 | ||
664 | (ctx->read_mem) (ctx->baton, buf, addr, addr_size); | |
665 | result = extract_unsigned_integer (buf, addr_size, byte_order); | |
666 | break; | |
667 | } | |
668 | ||
4c2df51b DJ |
669 | case DW_OP_abs: |
670 | case DW_OP_neg: | |
671 | case DW_OP_not: | |
672 | case DW_OP_plus_uconst: | |
673 | /* Unary operations. */ | |
674 | result = dwarf_expr_fetch (ctx, 0); | |
675 | dwarf_expr_pop (ctx); | |
676 | ||
677 | switch (op) | |
678 | { | |
4c2df51b | 679 | case DW_OP_abs: |
f2c7657e | 680 | if (sign_ext (result) < 0) |
4c2df51b DJ |
681 | result = -result; |
682 | break; | |
683 | case DW_OP_neg: | |
684 | result = -result; | |
685 | break; | |
686 | case DW_OP_not: | |
687 | result = ~result; | |
688 | break; | |
689 | case DW_OP_plus_uconst: | |
690 | op_ptr = read_uleb128 (op_ptr, op_end, ®); | |
691 | result += reg; | |
692 | break; | |
693 | } | |
694 | break; | |
695 | ||
696 | case DW_OP_and: | |
697 | case DW_OP_div: | |
698 | case DW_OP_minus: | |
699 | case DW_OP_mod: | |
700 | case DW_OP_mul: | |
701 | case DW_OP_or: | |
702 | case DW_OP_plus: | |
703 | case DW_OP_shl: | |
704 | case DW_OP_shr: | |
705 | case DW_OP_shra: | |
706 | case DW_OP_xor: | |
707 | case DW_OP_le: | |
708 | case DW_OP_ge: | |
709 | case DW_OP_eq: | |
710 | case DW_OP_lt: | |
711 | case DW_OP_gt: | |
712 | case DW_OP_ne: | |
713 | { | |
f2c7657e UW |
714 | /* Binary operations. */ |
715 | ULONGEST first, second; | |
4c2df51b DJ |
716 | |
717 | second = dwarf_expr_fetch (ctx, 0); | |
718 | dwarf_expr_pop (ctx); | |
719 | ||
b263358a | 720 | first = dwarf_expr_fetch (ctx, 0); |
4c2df51b DJ |
721 | dwarf_expr_pop (ctx); |
722 | ||
4c2df51b DJ |
723 | switch (op) |
724 | { | |
725 | case DW_OP_and: | |
f2c7657e | 726 | result = first & second; |
4c2df51b DJ |
727 | break; |
728 | case DW_OP_div: | |
f2c7657e UW |
729 | if (!second) |
730 | error (_("Division by zero")); | |
731 | result = sign_ext (first) / sign_ext (second); | |
99c87dab | 732 | break; |
4c2df51b | 733 | case DW_OP_minus: |
f2c7657e | 734 | result = first - second; |
4c2df51b DJ |
735 | break; |
736 | case DW_OP_mod: | |
f2c7657e UW |
737 | if (!second) |
738 | error (_("Division by zero")); | |
739 | result = first % second; | |
4c2df51b DJ |
740 | break; |
741 | case DW_OP_mul: | |
f2c7657e | 742 | result = first * second; |
4c2df51b DJ |
743 | break; |
744 | case DW_OP_or: | |
f2c7657e | 745 | result = first | second; |
4c2df51b DJ |
746 | break; |
747 | case DW_OP_plus: | |
f2c7657e | 748 | result = first + second; |
4c2df51b DJ |
749 | break; |
750 | case DW_OP_shl: | |
f2c7657e | 751 | result = first << second; |
4c2df51b DJ |
752 | break; |
753 | case DW_OP_shr: | |
f2c7657e | 754 | result = first >> second; |
99c87dab | 755 | break; |
4c2df51b | 756 | case DW_OP_shra: |
f2c7657e | 757 | result = sign_ext (first) >> second; |
4c2df51b DJ |
758 | break; |
759 | case DW_OP_xor: | |
f2c7657e | 760 | result = first ^ second; |
4c2df51b DJ |
761 | break; |
762 | case DW_OP_le: | |
f2c7657e | 763 | result = sign_ext (first) <= sign_ext (second); |
4c2df51b DJ |
764 | break; |
765 | case DW_OP_ge: | |
f2c7657e | 766 | result = sign_ext (first) >= sign_ext (second); |
4c2df51b DJ |
767 | break; |
768 | case DW_OP_eq: | |
f2c7657e | 769 | result = sign_ext (first) == sign_ext (second); |
4c2df51b DJ |
770 | break; |
771 | case DW_OP_lt: | |
f2c7657e | 772 | result = sign_ext (first) < sign_ext (second); |
4c2df51b DJ |
773 | break; |
774 | case DW_OP_gt: | |
f2c7657e | 775 | result = sign_ext (first) > sign_ext (second); |
4c2df51b DJ |
776 | break; |
777 | case DW_OP_ne: | |
f2c7657e | 778 | result = sign_ext (first) != sign_ext (second); |
4c2df51b DJ |
779 | break; |
780 | default: | |
781 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 782 | _("Can't be reached.")); |
4c2df51b | 783 | } |
4c2df51b DJ |
784 | } |
785 | break; | |
786 | ||
e7802207 TT |
787 | case DW_OP_call_frame_cfa: |
788 | result = (ctx->get_frame_cfa) (ctx->baton); | |
44353522 | 789 | in_stack_memory = 1; |
e7802207 TT |
790 | break; |
791 | ||
4c2df51b | 792 | case DW_OP_GNU_push_tls_address: |
c3228f12 EZ |
793 | /* Variable is at a constant offset in the thread-local |
794 | storage block into the objfile for the current thread and | |
795 | the dynamic linker module containing this expression. Here | |
796 | we return returns the offset from that base. The top of the | |
797 | stack has the offset from the beginning of the thread | |
798 | control block at which the variable is located. Nothing | |
799 | should follow this operator, so the top of stack would be | |
800 | returned. */ | |
4c2df51b DJ |
801 | result = dwarf_expr_fetch (ctx, 0); |
802 | dwarf_expr_pop (ctx); | |
803 | result = (ctx->get_tls_address) (ctx->baton, result); | |
804 | break; | |
805 | ||
806 | case DW_OP_skip: | |
e17a4113 | 807 | offset = extract_signed_integer (op_ptr, 2, byte_order); |
4c2df51b DJ |
808 | op_ptr += 2; |
809 | op_ptr += offset; | |
810 | goto no_push; | |
811 | ||
812 | case DW_OP_bra: | |
e17a4113 | 813 | offset = extract_signed_integer (op_ptr, 2, byte_order); |
4c2df51b DJ |
814 | op_ptr += 2; |
815 | if (dwarf_expr_fetch (ctx, 0) != 0) | |
816 | op_ptr += offset; | |
817 | dwarf_expr_pop (ctx); | |
818 | goto no_push; | |
819 | ||
820 | case DW_OP_nop: | |
821 | goto no_push; | |
822 | ||
87808bd6 JB |
823 | case DW_OP_piece: |
824 | { | |
825 | ULONGEST size; | |
87808bd6 JB |
826 | |
827 | /* Record the piece. */ | |
828 | op_ptr = read_uleb128 (op_ptr, op_end, &size); | |
d3b1e874 | 829 | add_piece (ctx, 8 * size, 0); |
87808bd6 | 830 | |
cec03d70 TT |
831 | /* Pop off the address/regnum, and reset the location |
832 | type. */ | |
cb826367 TT |
833 | if (ctx->location != DWARF_VALUE_LITERAL |
834 | && ctx->location != DWARF_VALUE_OPTIMIZED_OUT) | |
cec03d70 TT |
835 | dwarf_expr_pop (ctx); |
836 | ctx->location = DWARF_VALUE_MEMORY; | |
87808bd6 JB |
837 | } |
838 | goto no_push; | |
839 | ||
d3b1e874 TT |
840 | case DW_OP_bit_piece: |
841 | { | |
842 | ULONGEST size, offset; | |
843 | ||
844 | /* Record the piece. */ | |
845 | op_ptr = read_uleb128 (op_ptr, op_end, &size); | |
846 | op_ptr = read_uleb128 (op_ptr, op_end, &offset); | |
847 | add_piece (ctx, size, offset); | |
848 | ||
849 | /* Pop off the address/regnum, and reset the location | |
850 | type. */ | |
851 | if (ctx->location != DWARF_VALUE_LITERAL | |
852 | && ctx->location != DWARF_VALUE_OPTIMIZED_OUT) | |
853 | dwarf_expr_pop (ctx); | |
854 | ctx->location = DWARF_VALUE_MEMORY; | |
855 | } | |
856 | goto no_push; | |
857 | ||
42be36b3 CT |
858 | case DW_OP_GNU_uninit: |
859 | if (op_ptr != op_end) | |
9c482037 | 860 | error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always " |
42be36b3 CT |
861 | "be the very last op.")); |
862 | ||
863 | ctx->initialized = 0; | |
864 | goto no_push; | |
865 | ||
5c631832 JK |
866 | case DW_OP_call2: |
867 | result = extract_unsigned_integer (op_ptr, 2, byte_order); | |
868 | op_ptr += 2; | |
869 | ctx->dwarf_call (ctx, result); | |
870 | goto no_push; | |
871 | ||
872 | case DW_OP_call4: | |
873 | result = extract_unsigned_integer (op_ptr, 4, byte_order); | |
874 | op_ptr += 4; | |
875 | ctx->dwarf_call (ctx, result); | |
876 | goto no_push; | |
877 | ||
4c2df51b | 878 | default: |
8a3fe4f8 | 879 | error (_("Unhandled dwarf expression opcode 0x%x"), op); |
4c2df51b DJ |
880 | } |
881 | ||
882 | /* Most things push a result value. */ | |
44353522 | 883 | dwarf_expr_push (ctx, result, in_stack_memory); |
4c2df51b DJ |
884 | no_push:; |
885 | } | |
1e3a102a JK |
886 | |
887 | ctx->recursion_depth--; | |
888 | gdb_assert (ctx->recursion_depth >= 0); | |
43dabe42 | 889 | #undef sign_ext |
4c2df51b | 890 | } |