Commit | Line | Data |
---|---|---|
852483bc MK |
1 | /* DWARF 2 Expression Evaluator. |
2 | ||
6aba47ca | 3 | Copyright (C) 2001, 2002, 2003, 2005, 2007 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" | |
27 | #include "elf/dwarf2.h" | |
28 | #include "dwarf2expr.h" | |
29 | ||
30 | /* Local prototypes. */ | |
31 | ||
32 | static void execute_stack_op (struct dwarf_expr_context *, | |
852483bc | 33 | gdb_byte *, gdb_byte *); |
ace186d4 | 34 | static struct type *unsigned_address_type (void); |
4c2df51b DJ |
35 | |
36 | /* Create a new context for the expression evaluator. */ | |
37 | ||
38 | struct dwarf_expr_context * | |
e4adbba9 | 39 | new_dwarf_expr_context (void) |
4c2df51b DJ |
40 | { |
41 | struct dwarf_expr_context *retval; | |
42 | retval = xcalloc (1, sizeof (struct dwarf_expr_context)); | |
18ec9831 KB |
43 | retval->stack_len = 0; |
44 | retval->stack_allocated = 10; | |
45 | retval->stack = xmalloc (retval->stack_allocated * sizeof (CORE_ADDR)); | |
87808bd6 JB |
46 | retval->num_pieces = 0; |
47 | retval->pieces = 0; | |
4c2df51b DJ |
48 | return retval; |
49 | } | |
50 | ||
51 | /* Release the memory allocated to CTX. */ | |
52 | ||
53 | void | |
54 | free_dwarf_expr_context (struct dwarf_expr_context *ctx) | |
55 | { | |
56 | xfree (ctx->stack); | |
87808bd6 | 57 | xfree (ctx->pieces); |
4c2df51b DJ |
58 | xfree (ctx); |
59 | } | |
60 | ||
61 | /* Expand the memory allocated to CTX's stack to contain at least | |
62 | NEED more elements than are currently used. */ | |
63 | ||
64 | static void | |
65 | dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need) | |
66 | { | |
67 | if (ctx->stack_len + need > ctx->stack_allocated) | |
68 | { | |
18ec9831 | 69 | size_t newlen = ctx->stack_len + need + 10; |
4c2df51b | 70 | ctx->stack = xrealloc (ctx->stack, |
18ec9831 KB |
71 | newlen * sizeof (CORE_ADDR)); |
72 | ctx->stack_allocated = newlen; | |
4c2df51b DJ |
73 | } |
74 | } | |
75 | ||
76 | /* Push VALUE onto CTX's stack. */ | |
77 | ||
78 | void | |
79 | dwarf_expr_push (struct dwarf_expr_context *ctx, CORE_ADDR value) | |
80 | { | |
81 | dwarf_expr_grow_stack (ctx, 1); | |
82 | ctx->stack[ctx->stack_len++] = value; | |
83 | } | |
84 | ||
85 | /* Pop the top item off of CTX's stack. */ | |
86 | ||
87 | void | |
88 | dwarf_expr_pop (struct dwarf_expr_context *ctx) | |
89 | { | |
90 | if (ctx->stack_len <= 0) | |
8a3fe4f8 | 91 | error (_("dwarf expression stack underflow")); |
4c2df51b DJ |
92 | ctx->stack_len--; |
93 | } | |
94 | ||
95 | /* Retrieve the N'th item on CTX's stack. */ | |
96 | ||
97 | CORE_ADDR | |
98 | dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n) | |
99 | { | |
ef0fdf07 | 100 | if (ctx->stack_len <= n) |
8a3fe4f8 | 101 | error (_("Asked for position %d of stack, stack only has %d elements on it."), |
4c2df51b DJ |
102 | n, ctx->stack_len); |
103 | return ctx->stack[ctx->stack_len - (1 + n)]; | |
104 | ||
105 | } | |
106 | ||
87808bd6 JB |
107 | /* Add a new piece to CTX's piece list. */ |
108 | static void | |
109 | add_piece (struct dwarf_expr_context *ctx, | |
110 | int in_reg, CORE_ADDR value, ULONGEST size) | |
111 | { | |
112 | struct dwarf_expr_piece *p; | |
113 | ||
114 | ctx->num_pieces++; | |
115 | ||
116 | if (ctx->pieces) | |
117 | ctx->pieces = xrealloc (ctx->pieces, | |
118 | (ctx->num_pieces | |
119 | * sizeof (struct dwarf_expr_piece))); | |
120 | else | |
121 | ctx->pieces = xmalloc (ctx->num_pieces | |
122 | * sizeof (struct dwarf_expr_piece)); | |
123 | ||
124 | p = &ctx->pieces[ctx->num_pieces - 1]; | |
125 | p->in_reg = in_reg; | |
126 | p->value = value; | |
127 | p->size = size; | |
128 | } | |
129 | ||
4c2df51b DJ |
130 | /* Evaluate the expression at ADDR (LEN bytes long) using the context |
131 | CTX. */ | |
132 | ||
133 | void | |
852483bc | 134 | dwarf_expr_eval (struct dwarf_expr_context *ctx, gdb_byte *addr, size_t len) |
4c2df51b DJ |
135 | { |
136 | execute_stack_op (ctx, addr, addr + len); | |
137 | } | |
138 | ||
139 | /* Decode the unsigned LEB128 constant at BUF into the variable pointed to | |
140 | by R, and return the new value of BUF. Verify that it doesn't extend | |
141 | past BUF_END. */ | |
142 | ||
852483bc MK |
143 | gdb_byte * |
144 | read_uleb128 (gdb_byte *buf, gdb_byte *buf_end, ULONGEST * r) | |
4c2df51b DJ |
145 | { |
146 | unsigned shift = 0; | |
147 | ULONGEST result = 0; | |
852483bc | 148 | gdb_byte byte; |
4c2df51b DJ |
149 | |
150 | while (1) | |
151 | { | |
152 | if (buf >= buf_end) | |
8a3fe4f8 | 153 | error (_("read_uleb128: Corrupted DWARF expression.")); |
4c2df51b DJ |
154 | |
155 | byte = *buf++; | |
156 | result |= (byte & 0x7f) << shift; | |
157 | if ((byte & 0x80) == 0) | |
158 | break; | |
159 | shift += 7; | |
160 | } | |
161 | *r = result; | |
162 | return buf; | |
163 | } | |
164 | ||
165 | /* Decode the signed LEB128 constant at BUF into the variable pointed to | |
166 | by R, and return the new value of BUF. Verify that it doesn't extend | |
167 | past BUF_END. */ | |
168 | ||
852483bc MK |
169 | gdb_byte * |
170 | read_sleb128 (gdb_byte *buf, gdb_byte *buf_end, LONGEST * r) | |
4c2df51b DJ |
171 | { |
172 | unsigned shift = 0; | |
173 | LONGEST result = 0; | |
852483bc | 174 | gdb_byte byte; |
4c2df51b DJ |
175 | |
176 | while (1) | |
177 | { | |
178 | if (buf >= buf_end) | |
8a3fe4f8 | 179 | error (_("read_sleb128: Corrupted DWARF expression.")); |
4c2df51b DJ |
180 | |
181 | byte = *buf++; | |
182 | result |= (byte & 0x7f) << shift; | |
183 | shift += 7; | |
184 | if ((byte & 0x80) == 0) | |
185 | break; | |
186 | } | |
187 | if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0) | |
188 | result |= -(1 << shift); | |
189 | ||
190 | *r = result; | |
191 | return buf; | |
192 | } | |
193 | ||
194 | /* Read an address from BUF, and verify that it doesn't extend past | |
195 | BUF_END. The address is returned, and *BYTES_READ is set to the | |
196 | number of bytes read from BUF. */ | |
197 | ||
0d53c4c4 | 198 | CORE_ADDR |
852483bc | 199 | dwarf2_read_address (gdb_byte *buf, gdb_byte *buf_end, int *bytes_read) |
4c2df51b DJ |
200 | { |
201 | CORE_ADDR result; | |
202 | ||
17a912b6 | 203 | if (buf_end - buf < gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT) |
8a3fe4f8 | 204 | error (_("dwarf2_read_address: Corrupted DWARF expression.")); |
4c2df51b | 205 | |
17a912b6 | 206 | *bytes_read = gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT; |
ace186d4 KB |
207 | |
208 | /* For most architectures, calling extract_unsigned_integer() alone | |
209 | is sufficient for extracting an address. However, some | |
210 | architectures (e.g. MIPS) use signed addresses and using | |
211 | extract_unsigned_integer() will not produce a correct | |
212 | result. Turning the unsigned integer into a value and then | |
213 | decomposing that value as an address will cause | |
214 | gdbarch_integer_to_address() to be invoked for those | |
215 | architectures which require it. Thus, using value_as_address() | |
216 | will produce the correct result for both types of architectures. | |
217 | ||
218 | One concern regarding the use of values for this purpose is | |
219 | efficiency. Obviously, these extra calls will take more time to | |
220 | execute and creating a value takes more space, space which will | |
221 | have to be garbage collected at a later time. If constructing | |
222 | and then decomposing a value for this purpose proves to be too | |
223 | inefficient, then gdbarch_integer_to_address() can be called | |
224 | directly. | |
225 | ||
226 | The use of `unsigned_address_type' in the code below refers to | |
227 | the type of buf and has no bearing on the signedness of the | |
228 | address being returned. */ | |
229 | ||
230 | result = value_as_address (value_from_longest | |
231 | (unsigned_address_type (), | |
232 | extract_unsigned_integer | |
233 | (buf, | |
17a912b6 UW |
234 | gdbarch_addr_bit (current_gdbarch) |
235 | / TARGET_CHAR_BIT))); | |
ace186d4 | 236 | |
4c2df51b DJ |
237 | return result; |
238 | } | |
239 | ||
240 | /* Return the type of an address, for unsigned arithmetic. */ | |
241 | ||
242 | static struct type * | |
243 | unsigned_address_type (void) | |
244 | { | |
17a912b6 | 245 | switch (gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT) |
4c2df51b DJ |
246 | { |
247 | case 2: | |
248 | return builtin_type_uint16; | |
249 | case 4: | |
250 | return builtin_type_uint32; | |
251 | case 8: | |
252 | return builtin_type_uint64; | |
253 | default: | |
254 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 255 | _("Unsupported address size.\n")); |
4c2df51b DJ |
256 | } |
257 | } | |
258 | ||
259 | /* Return the type of an address, for signed arithmetic. */ | |
260 | ||
261 | static struct type * | |
262 | signed_address_type (void) | |
263 | { | |
17a912b6 | 264 | switch (gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT) |
4c2df51b DJ |
265 | { |
266 | case 2: | |
267 | return builtin_type_int16; | |
268 | case 4: | |
269 | return builtin_type_int32; | |
270 | case 8: | |
271 | return builtin_type_int64; | |
272 | default: | |
273 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 274 | _("Unsupported address size.\n")); |
4c2df51b DJ |
275 | } |
276 | } | |
277 | \f | |
278 | /* The engine for the expression evaluator. Using the context in CTX, | |
279 | evaluate the expression between OP_PTR and OP_END. */ | |
280 | ||
281 | static void | |
852483bc MK |
282 | execute_stack_op (struct dwarf_expr_context *ctx, |
283 | gdb_byte *op_ptr, gdb_byte *op_end) | |
4c2df51b | 284 | { |
18ec9831 | 285 | ctx->in_reg = 0; |
42be36b3 | 286 | ctx->initialized = 1; /* Default is initialized. */ |
18ec9831 | 287 | |
4c2df51b DJ |
288 | while (op_ptr < op_end) |
289 | { | |
290 | enum dwarf_location_atom op = *op_ptr++; | |
61fbb938 | 291 | CORE_ADDR result; |
4c2df51b DJ |
292 | ULONGEST uoffset, reg; |
293 | LONGEST offset; | |
294 | int bytes_read; | |
4c2df51b | 295 | |
4c2df51b DJ |
296 | switch (op) |
297 | { | |
298 | case DW_OP_lit0: | |
299 | case DW_OP_lit1: | |
300 | case DW_OP_lit2: | |
301 | case DW_OP_lit3: | |
302 | case DW_OP_lit4: | |
303 | case DW_OP_lit5: | |
304 | case DW_OP_lit6: | |
305 | case DW_OP_lit7: | |
306 | case DW_OP_lit8: | |
307 | case DW_OP_lit9: | |
308 | case DW_OP_lit10: | |
309 | case DW_OP_lit11: | |
310 | case DW_OP_lit12: | |
311 | case DW_OP_lit13: | |
312 | case DW_OP_lit14: | |
313 | case DW_OP_lit15: | |
314 | case DW_OP_lit16: | |
315 | case DW_OP_lit17: | |
316 | case DW_OP_lit18: | |
317 | case DW_OP_lit19: | |
318 | case DW_OP_lit20: | |
319 | case DW_OP_lit21: | |
320 | case DW_OP_lit22: | |
321 | case DW_OP_lit23: | |
322 | case DW_OP_lit24: | |
323 | case DW_OP_lit25: | |
324 | case DW_OP_lit26: | |
325 | case DW_OP_lit27: | |
326 | case DW_OP_lit28: | |
327 | case DW_OP_lit29: | |
328 | case DW_OP_lit30: | |
329 | case DW_OP_lit31: | |
330 | result = op - DW_OP_lit0; | |
331 | break; | |
332 | ||
333 | case DW_OP_addr: | |
0d53c4c4 | 334 | result = dwarf2_read_address (op_ptr, op_end, &bytes_read); |
4c2df51b DJ |
335 | op_ptr += bytes_read; |
336 | break; | |
337 | ||
338 | case DW_OP_const1u: | |
339 | result = extract_unsigned_integer (op_ptr, 1); | |
340 | op_ptr += 1; | |
341 | break; | |
342 | case DW_OP_const1s: | |
343 | result = extract_signed_integer (op_ptr, 1); | |
344 | op_ptr += 1; | |
345 | break; | |
346 | case DW_OP_const2u: | |
347 | result = extract_unsigned_integer (op_ptr, 2); | |
348 | op_ptr += 2; | |
349 | break; | |
350 | case DW_OP_const2s: | |
351 | result = extract_signed_integer (op_ptr, 2); | |
352 | op_ptr += 2; | |
353 | break; | |
354 | case DW_OP_const4u: | |
355 | result = extract_unsigned_integer (op_ptr, 4); | |
356 | op_ptr += 4; | |
357 | break; | |
358 | case DW_OP_const4s: | |
359 | result = extract_signed_integer (op_ptr, 4); | |
360 | op_ptr += 4; | |
361 | break; | |
362 | case DW_OP_const8u: | |
363 | result = extract_unsigned_integer (op_ptr, 8); | |
364 | op_ptr += 8; | |
365 | break; | |
366 | case DW_OP_const8s: | |
367 | result = extract_signed_integer (op_ptr, 8); | |
368 | op_ptr += 8; | |
369 | break; | |
370 | case DW_OP_constu: | |
371 | op_ptr = read_uleb128 (op_ptr, op_end, &uoffset); | |
372 | result = uoffset; | |
373 | break; | |
374 | case DW_OP_consts: | |
375 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
376 | result = offset; | |
377 | break; | |
378 | ||
379 | /* The DW_OP_reg operations are required to occur alone in | |
380 | location expressions. */ | |
381 | case DW_OP_reg0: | |
382 | case DW_OP_reg1: | |
383 | case DW_OP_reg2: | |
384 | case DW_OP_reg3: | |
385 | case DW_OP_reg4: | |
386 | case DW_OP_reg5: | |
387 | case DW_OP_reg6: | |
388 | case DW_OP_reg7: | |
389 | case DW_OP_reg8: | |
390 | case DW_OP_reg9: | |
391 | case DW_OP_reg10: | |
392 | case DW_OP_reg11: | |
393 | case DW_OP_reg12: | |
394 | case DW_OP_reg13: | |
395 | case DW_OP_reg14: | |
396 | case DW_OP_reg15: | |
397 | case DW_OP_reg16: | |
398 | case DW_OP_reg17: | |
399 | case DW_OP_reg18: | |
400 | case DW_OP_reg19: | |
401 | case DW_OP_reg20: | |
402 | case DW_OP_reg21: | |
403 | case DW_OP_reg22: | |
404 | case DW_OP_reg23: | |
405 | case DW_OP_reg24: | |
406 | case DW_OP_reg25: | |
407 | case DW_OP_reg26: | |
408 | case DW_OP_reg27: | |
409 | case DW_OP_reg28: | |
410 | case DW_OP_reg29: | |
411 | case DW_OP_reg30: | |
412 | case DW_OP_reg31: | |
42be36b3 CT |
413 | if (op_ptr != op_end |
414 | && *op_ptr != DW_OP_piece | |
415 | && *op_ptr != DW_OP_GNU_uninit) | |
8a3fe4f8 AC |
416 | error (_("DWARF-2 expression error: DW_OP_reg operations must be " |
417 | "used either alone or in conjuction with DW_OP_piece.")); | |
4c2df51b | 418 | |
61fbb938 DJ |
419 | result = op - DW_OP_reg0; |
420 | ctx->in_reg = 1; | |
4c2df51b DJ |
421 | |
422 | break; | |
423 | ||
424 | case DW_OP_regx: | |
425 | op_ptr = read_uleb128 (op_ptr, op_end, ®); | |
18ec9831 | 426 | if (op_ptr != op_end && *op_ptr != DW_OP_piece) |
8a3fe4f8 AC |
427 | error (_("DWARF-2 expression error: DW_OP_reg operations must be " |
428 | "used either alone or in conjuction with DW_OP_piece.")); | |
4c2df51b | 429 | |
61fbb938 DJ |
430 | result = reg; |
431 | ctx->in_reg = 1; | |
4c2df51b DJ |
432 | break; |
433 | ||
434 | case DW_OP_breg0: | |
435 | case DW_OP_breg1: | |
436 | case DW_OP_breg2: | |
437 | case DW_OP_breg3: | |
438 | case DW_OP_breg4: | |
439 | case DW_OP_breg5: | |
440 | case DW_OP_breg6: | |
441 | case DW_OP_breg7: | |
442 | case DW_OP_breg8: | |
443 | case DW_OP_breg9: | |
444 | case DW_OP_breg10: | |
445 | case DW_OP_breg11: | |
446 | case DW_OP_breg12: | |
447 | case DW_OP_breg13: | |
448 | case DW_OP_breg14: | |
449 | case DW_OP_breg15: | |
450 | case DW_OP_breg16: | |
451 | case DW_OP_breg17: | |
452 | case DW_OP_breg18: | |
453 | case DW_OP_breg19: | |
454 | case DW_OP_breg20: | |
455 | case DW_OP_breg21: | |
456 | case DW_OP_breg22: | |
457 | case DW_OP_breg23: | |
458 | case DW_OP_breg24: | |
459 | case DW_OP_breg25: | |
460 | case DW_OP_breg26: | |
461 | case DW_OP_breg27: | |
462 | case DW_OP_breg28: | |
463 | case DW_OP_breg29: | |
464 | case DW_OP_breg30: | |
465 | case DW_OP_breg31: | |
466 | { | |
467 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
61fbb938 | 468 | result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0); |
4c2df51b DJ |
469 | result += offset; |
470 | } | |
471 | break; | |
472 | case DW_OP_bregx: | |
473 | { | |
474 | op_ptr = read_uleb128 (op_ptr, op_end, ®); | |
475 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
61fbb938 | 476 | result = (ctx->read_reg) (ctx->baton, reg); |
4c2df51b DJ |
477 | result += offset; |
478 | } | |
479 | break; | |
480 | case DW_OP_fbreg: | |
481 | { | |
852483bc | 482 | gdb_byte *datastart; |
4c2df51b DJ |
483 | size_t datalen; |
484 | unsigned int before_stack_len; | |
485 | ||
486 | op_ptr = read_sleb128 (op_ptr, op_end, &offset); | |
487 | /* Rather than create a whole new context, we simply | |
488 | record the stack length before execution, then reset it | |
489 | afterwards, effectively erasing whatever the recursive | |
490 | call put there. */ | |
491 | before_stack_len = ctx->stack_len; | |
da62e633 AC |
492 | /* FIXME: cagney/2003-03-26: This code should be using |
493 | get_frame_base_address(), and then implement a dwarf2 | |
494 | specific this_base method. */ | |
4c2df51b DJ |
495 | (ctx->get_frame_base) (ctx->baton, &datastart, &datalen); |
496 | dwarf_expr_eval (ctx, datastart, datalen); | |
497 | result = dwarf_expr_fetch (ctx, 0); | |
61fbb938 DJ |
498 | if (ctx->in_reg) |
499 | result = (ctx->read_reg) (ctx->baton, result); | |
4c2df51b DJ |
500 | result = result + offset; |
501 | ctx->stack_len = before_stack_len; | |
502 | ctx->in_reg = 0; | |
503 | } | |
504 | break; | |
505 | case DW_OP_dup: | |
506 | result = dwarf_expr_fetch (ctx, 0); | |
507 | break; | |
508 | ||
509 | case DW_OP_drop: | |
510 | dwarf_expr_pop (ctx); | |
511 | goto no_push; | |
512 | ||
513 | case DW_OP_pick: | |
514 | offset = *op_ptr++; | |
515 | result = dwarf_expr_fetch (ctx, offset); | |
516 | break; | |
517 | ||
518 | case DW_OP_over: | |
519 | result = dwarf_expr_fetch (ctx, 1); | |
520 | break; | |
521 | ||
522 | case DW_OP_rot: | |
523 | { | |
524 | CORE_ADDR t1, t2, t3; | |
525 | ||
526 | if (ctx->stack_len < 3) | |
8a3fe4f8 | 527 | error (_("Not enough elements for DW_OP_rot. Need 3, have %d."), |
4c2df51b DJ |
528 | ctx->stack_len); |
529 | t1 = ctx->stack[ctx->stack_len - 1]; | |
530 | t2 = ctx->stack[ctx->stack_len - 2]; | |
531 | t3 = ctx->stack[ctx->stack_len - 3]; | |
532 | ctx->stack[ctx->stack_len - 1] = t2; | |
533 | ctx->stack[ctx->stack_len - 2] = t3; | |
534 | ctx->stack[ctx->stack_len - 3] = t1; | |
535 | goto no_push; | |
536 | } | |
537 | ||
538 | case DW_OP_deref: | |
539 | case DW_OP_deref_size: | |
540 | case DW_OP_abs: | |
541 | case DW_OP_neg: | |
542 | case DW_OP_not: | |
543 | case DW_OP_plus_uconst: | |
544 | /* Unary operations. */ | |
545 | result = dwarf_expr_fetch (ctx, 0); | |
546 | dwarf_expr_pop (ctx); | |
547 | ||
548 | switch (op) | |
549 | { | |
550 | case DW_OP_deref: | |
551 | { | |
17a912b6 UW |
552 | gdb_byte *buf = alloca (gdbarch_addr_bit (current_gdbarch) |
553 | / TARGET_CHAR_BIT); | |
4c2df51b DJ |
554 | int bytes_read; |
555 | ||
556 | (ctx->read_mem) (ctx->baton, buf, result, | |
17a912b6 UW |
557 | gdbarch_addr_bit (current_gdbarch) |
558 | / TARGET_CHAR_BIT); | |
0d53c4c4 | 559 | result = dwarf2_read_address (buf, |
17a912b6 UW |
560 | buf + (gdbarch_addr_bit |
561 | (current_gdbarch) | |
0d53c4c4 DJ |
562 | / TARGET_CHAR_BIT), |
563 | &bytes_read); | |
4c2df51b DJ |
564 | } |
565 | break; | |
566 | ||
567 | case DW_OP_deref_size: | |
568 | { | |
17a912b6 UW |
569 | gdb_byte *buf |
570 | = alloca (gdbarch_addr_bit (current_gdbarch) | |
571 | / TARGET_CHAR_BIT); | |
4c2df51b DJ |
572 | int bytes_read; |
573 | ||
574 | (ctx->read_mem) (ctx->baton, buf, result, *op_ptr++); | |
0d53c4c4 | 575 | result = dwarf2_read_address (buf, |
17a912b6 UW |
576 | buf + (gdbarch_addr_bit |
577 | (current_gdbarch) | |
0d53c4c4 DJ |
578 | / TARGET_CHAR_BIT), |
579 | &bytes_read); | |
4c2df51b DJ |
580 | } |
581 | break; | |
582 | ||
583 | case DW_OP_abs: | |
584 | if ((signed int) result < 0) | |
585 | result = -result; | |
586 | break; | |
587 | case DW_OP_neg: | |
588 | result = -result; | |
589 | break; | |
590 | case DW_OP_not: | |
591 | result = ~result; | |
592 | break; | |
593 | case DW_OP_plus_uconst: | |
594 | op_ptr = read_uleb128 (op_ptr, op_end, ®); | |
595 | result += reg; | |
596 | break; | |
597 | } | |
598 | break; | |
599 | ||
600 | case DW_OP_and: | |
601 | case DW_OP_div: | |
602 | case DW_OP_minus: | |
603 | case DW_OP_mod: | |
604 | case DW_OP_mul: | |
605 | case DW_OP_or: | |
606 | case DW_OP_plus: | |
607 | case DW_OP_shl: | |
608 | case DW_OP_shr: | |
609 | case DW_OP_shra: | |
610 | case DW_OP_xor: | |
611 | case DW_OP_le: | |
612 | case DW_OP_ge: | |
613 | case DW_OP_eq: | |
614 | case DW_OP_lt: | |
615 | case DW_OP_gt: | |
616 | case DW_OP_ne: | |
617 | { | |
618 | /* Binary operations. Use the value engine to do computations in | |
619 | the right width. */ | |
620 | CORE_ADDR first, second; | |
621 | enum exp_opcode binop; | |
622 | struct value *val1, *val2; | |
623 | ||
624 | second = dwarf_expr_fetch (ctx, 0); | |
625 | dwarf_expr_pop (ctx); | |
626 | ||
b263358a | 627 | first = dwarf_expr_fetch (ctx, 0); |
4c2df51b DJ |
628 | dwarf_expr_pop (ctx); |
629 | ||
630 | val1 = value_from_longest (unsigned_address_type (), first); | |
631 | val2 = value_from_longest (unsigned_address_type (), second); | |
632 | ||
633 | switch (op) | |
634 | { | |
635 | case DW_OP_and: | |
636 | binop = BINOP_BITWISE_AND; | |
637 | break; | |
638 | case DW_OP_div: | |
639 | binop = BINOP_DIV; | |
99c87dab | 640 | break; |
4c2df51b DJ |
641 | case DW_OP_minus: |
642 | binop = BINOP_SUB; | |
643 | break; | |
644 | case DW_OP_mod: | |
645 | binop = BINOP_MOD; | |
646 | break; | |
647 | case DW_OP_mul: | |
648 | binop = BINOP_MUL; | |
649 | break; | |
650 | case DW_OP_or: | |
651 | binop = BINOP_BITWISE_IOR; | |
652 | break; | |
653 | case DW_OP_plus: | |
654 | binop = BINOP_ADD; | |
655 | break; | |
656 | case DW_OP_shl: | |
657 | binop = BINOP_LSH; | |
658 | break; | |
659 | case DW_OP_shr: | |
660 | binop = BINOP_RSH; | |
99c87dab | 661 | break; |
4c2df51b DJ |
662 | case DW_OP_shra: |
663 | binop = BINOP_RSH; | |
664 | val1 = value_from_longest (signed_address_type (), first); | |
665 | break; | |
666 | case DW_OP_xor: | |
667 | binop = BINOP_BITWISE_XOR; | |
668 | break; | |
669 | case DW_OP_le: | |
670 | binop = BINOP_LEQ; | |
671 | break; | |
672 | case DW_OP_ge: | |
673 | binop = BINOP_GEQ; | |
674 | break; | |
675 | case DW_OP_eq: | |
676 | binop = BINOP_EQUAL; | |
677 | break; | |
678 | case DW_OP_lt: | |
679 | binop = BINOP_LESS; | |
680 | break; | |
681 | case DW_OP_gt: | |
682 | binop = BINOP_GTR; | |
683 | break; | |
684 | case DW_OP_ne: | |
685 | binop = BINOP_NOTEQUAL; | |
686 | break; | |
687 | default: | |
688 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 689 | _("Can't be reached.")); |
4c2df51b DJ |
690 | } |
691 | result = value_as_long (value_binop (val1, val2, binop)); | |
692 | } | |
693 | break; | |
694 | ||
695 | case DW_OP_GNU_push_tls_address: | |
c3228f12 EZ |
696 | /* Variable is at a constant offset in the thread-local |
697 | storage block into the objfile for the current thread and | |
698 | the dynamic linker module containing this expression. Here | |
699 | we return returns the offset from that base. The top of the | |
700 | stack has the offset from the beginning of the thread | |
701 | control block at which the variable is located. Nothing | |
702 | should follow this operator, so the top of stack would be | |
703 | returned. */ | |
4c2df51b DJ |
704 | result = dwarf_expr_fetch (ctx, 0); |
705 | dwarf_expr_pop (ctx); | |
706 | result = (ctx->get_tls_address) (ctx->baton, result); | |
707 | break; | |
708 | ||
709 | case DW_OP_skip: | |
710 | offset = extract_signed_integer (op_ptr, 2); | |
711 | op_ptr += 2; | |
712 | op_ptr += offset; | |
713 | goto no_push; | |
714 | ||
715 | case DW_OP_bra: | |
716 | offset = extract_signed_integer (op_ptr, 2); | |
717 | op_ptr += 2; | |
718 | if (dwarf_expr_fetch (ctx, 0) != 0) | |
719 | op_ptr += offset; | |
720 | dwarf_expr_pop (ctx); | |
721 | goto no_push; | |
722 | ||
723 | case DW_OP_nop: | |
724 | goto no_push; | |
725 | ||
87808bd6 JB |
726 | case DW_OP_piece: |
727 | { | |
728 | ULONGEST size; | |
729 | CORE_ADDR addr_or_regnum; | |
730 | ||
731 | /* Record the piece. */ | |
732 | op_ptr = read_uleb128 (op_ptr, op_end, &size); | |
733 | addr_or_regnum = dwarf_expr_fetch (ctx, 0); | |
734 | add_piece (ctx, ctx->in_reg, addr_or_regnum, size); | |
735 | ||
736 | /* Pop off the address/regnum, and clear the in_reg flag. */ | |
737 | dwarf_expr_pop (ctx); | |
738 | ctx->in_reg = 0; | |
739 | } | |
740 | goto no_push; | |
741 | ||
42be36b3 CT |
742 | case DW_OP_GNU_uninit: |
743 | if (op_ptr != op_end) | |
744 | error (_("DWARF-2 expression error: DW_OP_GNU_unint must always " | |
745 | "be the very last op.")); | |
746 | ||
747 | ctx->initialized = 0; | |
748 | goto no_push; | |
749 | ||
4c2df51b | 750 | default: |
8a3fe4f8 | 751 | error (_("Unhandled dwarf expression opcode 0x%x"), op); |
4c2df51b DJ |
752 | } |
753 | ||
754 | /* Most things push a result value. */ | |
755 | dwarf_expr_push (ctx, result); | |
756 | no_push:; | |
757 | } | |
758 | } |