Implement TARGET_OBJECT_STACK_MEMORY.
[binutils-gdb.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3 Copyright (C) 2003, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
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
11 the Free Software Foundation; either version 3 of the License, or
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
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "ui-out.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "inferior.h"
29 #include "ax.h"
30 #include "ax-gdb.h"
31 #include "regcache.h"
32 #include "objfiles.h"
33 #include "exceptions.h"
34 #include "block.h"
35
36 #include "dwarf2.h"
37 #include "dwarf2expr.h"
38 #include "dwarf2loc.h"
39
40 #include "gdb_string.h"
41 #include "gdb_assert.h"
42
43 /* A helper function for dealing with location lists. Given a
44 symbol baton (BATON) and a pc value (PC), find the appropriate
45 location expression, set *LOCEXPR_LENGTH, and return a pointer
46 to the beginning of the expression. Returns NULL on failure.
47
48 For now, only return the first matching location expression; there
49 can be more than one in the list. */
50
51 static gdb_byte *
52 find_location_expression (struct dwarf2_loclist_baton *baton,
53 size_t *locexpr_length, CORE_ADDR pc)
54 {
55 CORE_ADDR low, high;
56 gdb_byte *loc_ptr, *buf_end;
57 int length;
58 struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
59 struct gdbarch *gdbarch = get_objfile_arch (objfile);
60 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
61 unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
62 CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
63 /* Adjust base_address for relocatable objects. */
64 CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
65 SECT_OFF_TEXT (objfile));
66 CORE_ADDR base_address = baton->base_address + base_offset;
67
68 loc_ptr = baton->data;
69 buf_end = baton->data + baton->size;
70
71 while (1)
72 {
73 if (buf_end - loc_ptr < 2 * addr_size)
74 error (_("find_location_expression: Corrupted DWARF expression."));
75
76 low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
77 loc_ptr += addr_size;
78
79 /* A base-address-selection entry. */
80 if (low == base_mask)
81 {
82 base_address = dwarf2_read_address (gdbarch,
83 loc_ptr, buf_end, addr_size);
84 loc_ptr += addr_size;
85 continue;
86 }
87
88 high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
89 loc_ptr += addr_size;
90
91 /* An end-of-list entry. */
92 if (low == 0 && high == 0)
93 return NULL;
94
95 /* Otherwise, a location expression entry. */
96 low += base_address;
97 high += base_address;
98
99 length = extract_unsigned_integer (loc_ptr, 2, byte_order);
100 loc_ptr += 2;
101
102 if (pc >= low && pc < high)
103 {
104 *locexpr_length = length;
105 return loc_ptr;
106 }
107
108 loc_ptr += length;
109 }
110 }
111
112 /* This is the baton used when performing dwarf2 expression
113 evaluation. */
114 struct dwarf_expr_baton
115 {
116 struct frame_info *frame;
117 struct objfile *objfile;
118 };
119
120 /* Helper functions for dwarf2_evaluate_loc_desc. */
121
122 /* Using the frame specified in BATON, return the value of register
123 REGNUM, treated as a pointer. */
124 static CORE_ADDR
125 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
126 {
127 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
128 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
129 CORE_ADDR result;
130 int regnum;
131
132 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
133 result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
134 regnum, debaton->frame);
135 return result;
136 }
137
138 /* Read memory at ADDR (length LEN) into BUF. */
139
140 static void
141 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
142 {
143 read_memory (addr, buf, len);
144 }
145
146 /* Using the frame specified in BATON, find the location expression
147 describing the frame base. Return a pointer to it in START and
148 its length in LENGTH. */
149 static void
150 dwarf_expr_frame_base (void *baton, gdb_byte **start, size_t * length)
151 {
152 /* FIXME: cagney/2003-03-26: This code should be using
153 get_frame_base_address(), and then implement a dwarf2 specific
154 this_base method. */
155 struct symbol *framefunc;
156 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
157
158 /* Use block_linkage_function, which returns a real (not inlined)
159 function, instead of get_frame_function, which may return an
160 inlined function. */
161 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
162
163 /* If we found a frame-relative symbol then it was certainly within
164 some function associated with a frame. If we can't find the frame,
165 something has gone wrong. */
166 gdb_assert (framefunc != NULL);
167
168 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
169 *start = NULL;
170 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
171 {
172 struct dwarf2_loclist_baton *symbaton;
173 struct frame_info *frame = debaton->frame;
174
175 symbaton = SYMBOL_LOCATION_BATON (framefunc);
176 *start = find_location_expression (symbaton, length,
177 get_frame_address_in_block (frame));
178 }
179 else
180 {
181 struct dwarf2_locexpr_baton *symbaton;
182 symbaton = SYMBOL_LOCATION_BATON (framefunc);
183 if (symbaton != NULL)
184 {
185 *length = symbaton->size;
186 *start = symbaton->data;
187 }
188 else
189 *start = NULL;
190 }
191
192 if (*start == NULL)
193 error (_("Could not find the frame base for \"%s\"."),
194 SYMBOL_NATURAL_NAME (framefunc));
195 }
196
197 /* Using the objfile specified in BATON, find the address for the
198 current thread's thread-local storage with offset OFFSET. */
199 static CORE_ADDR
200 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
201 {
202 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
203
204 return target_translate_tls_address (debaton->objfile, offset);
205 }
206
207 /* Evaluate a location description, starting at DATA and with length
208 SIZE, to find the current location of variable VAR in the context
209 of FRAME. */
210 static struct value *
211 dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
212 gdb_byte *data, unsigned short size,
213 struct dwarf2_per_cu_data *per_cu)
214 {
215 struct value *retval;
216 struct dwarf_expr_baton baton;
217 struct dwarf_expr_context *ctx;
218 struct cleanup *old_chain;
219
220 if (size == 0)
221 {
222 retval = allocate_value (SYMBOL_TYPE (var));
223 VALUE_LVAL (retval) = not_lval;
224 set_value_optimized_out (retval, 1);
225 return retval;
226 }
227
228 baton.frame = frame;
229 baton.objfile = dwarf2_per_cu_objfile (per_cu);
230
231 ctx = new_dwarf_expr_context ();
232 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
233
234 ctx->gdbarch = get_objfile_arch (baton.objfile);
235 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
236 ctx->baton = &baton;
237 ctx->read_reg = dwarf_expr_read_reg;
238 ctx->read_mem = dwarf_expr_read_mem;
239 ctx->get_frame_base = dwarf_expr_frame_base;
240 ctx->get_tls_address = dwarf_expr_tls_address;
241
242 dwarf_expr_eval (ctx, data, size);
243 if (ctx->num_pieces > 0)
244 {
245 int i;
246 long offset = 0;
247 bfd_byte *contents;
248
249 retval = allocate_value (SYMBOL_TYPE (var));
250 contents = value_contents_raw (retval);
251 for (i = 0; i < ctx->num_pieces; i++)
252 {
253 struct dwarf_expr_piece *p = &ctx->pieces[i];
254 if (p->in_reg)
255 {
256 struct gdbarch *arch = get_frame_arch (frame);
257 bfd_byte regval[MAX_REGISTER_SIZE];
258 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->value);
259 get_frame_register (frame, gdb_regnum, regval);
260 memcpy (contents + offset, regval, p->size);
261 }
262 else /* In memory? */
263 {
264 read_memory (p->value, contents + offset, p->size);
265 }
266 offset += p->size;
267 }
268 }
269 else if (ctx->in_reg)
270 {
271 struct gdbarch *arch = get_frame_arch (frame);
272 CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
273 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
274 retval = value_from_register (SYMBOL_TYPE (var), gdb_regnum, frame);
275 }
276 else
277 {
278 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
279
280 retval = allocate_value (SYMBOL_TYPE (var));
281 VALUE_LVAL (retval) = lval_memory;
282 set_value_lazy (retval, 1);
283 set_value_stack (retval, 1);
284 set_value_address (retval, address);
285 }
286
287 set_value_initialized (retval, ctx->initialized);
288
289 do_cleanups (old_chain);
290
291 return retval;
292 }
293
294
295
296
297 \f
298 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
299
300 struct needs_frame_baton
301 {
302 int needs_frame;
303 };
304
305 /* Reads from registers do require a frame. */
306 static CORE_ADDR
307 needs_frame_read_reg (void *baton, int regnum)
308 {
309 struct needs_frame_baton *nf_baton = baton;
310 nf_baton->needs_frame = 1;
311 return 1;
312 }
313
314 /* Reads from memory do not require a frame. */
315 static void
316 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
317 {
318 memset (buf, 0, len);
319 }
320
321 /* Frame-relative accesses do require a frame. */
322 static void
323 needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length)
324 {
325 static gdb_byte lit0 = DW_OP_lit0;
326 struct needs_frame_baton *nf_baton = baton;
327
328 *start = &lit0;
329 *length = 1;
330
331 nf_baton->needs_frame = 1;
332 }
333
334 /* Thread-local accesses do require a frame. */
335 static CORE_ADDR
336 needs_frame_tls_address (void *baton, CORE_ADDR offset)
337 {
338 struct needs_frame_baton *nf_baton = baton;
339 nf_baton->needs_frame = 1;
340 return 1;
341 }
342
343 /* Return non-zero iff the location expression at DATA (length SIZE)
344 requires a frame to evaluate. */
345
346 static int
347 dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size,
348 struct dwarf2_per_cu_data *per_cu)
349 {
350 struct needs_frame_baton baton;
351 struct dwarf_expr_context *ctx;
352 int in_reg;
353 struct cleanup *old_chain;
354
355 baton.needs_frame = 0;
356
357 ctx = new_dwarf_expr_context ();
358 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
359
360 ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
361 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
362 ctx->baton = &baton;
363 ctx->read_reg = needs_frame_read_reg;
364 ctx->read_mem = needs_frame_read_mem;
365 ctx->get_frame_base = needs_frame_frame_base;
366 ctx->get_tls_address = needs_frame_tls_address;
367
368 dwarf_expr_eval (ctx, data, size);
369
370 in_reg = ctx->in_reg;
371
372 if (ctx->num_pieces > 0)
373 {
374 int i;
375
376 /* If the location has several pieces, and any of them are in
377 registers, then we will need a frame to fetch them from. */
378 for (i = 0; i < ctx->num_pieces; i++)
379 if (ctx->pieces[i].in_reg)
380 in_reg = 1;
381 }
382
383 do_cleanups (old_chain);
384
385 return baton.needs_frame || in_reg;
386 }
387
388 static void
389 dwarf2_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
390 struct agent_expr *ax, struct axs_value *value,
391 gdb_byte *data, int size)
392 {
393 if (size == 0)
394 error (_("Symbol \"%s\" has been optimized out."),
395 SYMBOL_PRINT_NAME (symbol));
396
397 if (size == 1
398 && data[0] >= DW_OP_reg0
399 && data[0] <= DW_OP_reg31)
400 {
401 value->kind = axs_lvalue_register;
402 value->u.reg = data[0] - DW_OP_reg0;
403 }
404 else if (data[0] == DW_OP_regx)
405 {
406 ULONGEST reg;
407 read_uleb128 (data + 1, data + size, &reg);
408 value->kind = axs_lvalue_register;
409 value->u.reg = reg;
410 }
411 else if (data[0] == DW_OP_fbreg)
412 {
413 /* And this is worse than just minimal; we should honor the frame base
414 as above. */
415 int frame_reg;
416 LONGEST frame_offset;
417 gdb_byte *buf_end;
418
419 buf_end = read_sleb128 (data + 1, data + size, &frame_offset);
420 if (buf_end != data + size)
421 error (_("Unexpected opcode after DW_OP_fbreg for symbol \"%s\"."),
422 SYMBOL_PRINT_NAME (symbol));
423
424 gdbarch_virtual_frame_pointer (gdbarch,
425 ax->scope, &frame_reg, &frame_offset);
426 ax_reg (ax, frame_reg);
427 ax_const_l (ax, frame_offset);
428 ax_simple (ax, aop_add);
429
430 value->kind = axs_lvalue_memory;
431 }
432 else if (data[0] >= DW_OP_breg0
433 && data[0] <= DW_OP_breg31)
434 {
435 unsigned int reg;
436 LONGEST offset;
437 gdb_byte *buf_end;
438
439 reg = data[0] - DW_OP_breg0;
440 buf_end = read_sleb128 (data + 1, data + size, &offset);
441 if (buf_end != data + size)
442 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
443 reg, SYMBOL_PRINT_NAME (symbol));
444
445 ax_reg (ax, reg);
446 ax_const_l (ax, offset);
447 ax_simple (ax, aop_add);
448
449 value->kind = axs_lvalue_memory;
450 }
451 else
452 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
453 data[0], SYMBOL_PRINT_NAME (symbol));
454 }
455 \f
456 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
457 evaluator to calculate the location. */
458 static struct value *
459 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
460 {
461 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
462 struct value *val;
463 val = dwarf2_evaluate_loc_desc (symbol, frame, dlbaton->data, dlbaton->size,
464 dlbaton->per_cu);
465
466 return val;
467 }
468
469 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
470 static int
471 locexpr_read_needs_frame (struct symbol *symbol)
472 {
473 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
474 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
475 dlbaton->per_cu);
476 }
477
478 /* Print a natural-language description of SYMBOL to STREAM. */
479 static int
480 locexpr_describe_location (struct symbol *symbol, struct ui_file *stream)
481 {
482 /* FIXME: be more extensive. */
483 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
484 int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
485
486 if (dlbaton->size == 1
487 && dlbaton->data[0] >= DW_OP_reg0
488 && dlbaton->data[0] <= DW_OP_reg31)
489 {
490 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
491 struct gdbarch *gdbarch = get_objfile_arch (objfile);
492 int regno = gdbarch_dwarf2_reg_to_regnum (gdbarch,
493 dlbaton->data[0] - DW_OP_reg0);
494 fprintf_filtered (stream,
495 "a variable in register %s",
496 gdbarch_register_name (gdbarch, regno));
497 return 1;
498 }
499
500 /* The location expression for a TLS variable looks like this (on a
501 64-bit LE machine):
502
503 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
504 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
505
506 0x3 is the encoding for DW_OP_addr, which has an operand as long
507 as the size of an address on the target machine (here is 8
508 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
509 The operand represents the offset at which the variable is within
510 the thread local storage. */
511
512 if (dlbaton->size > 1
513 && dlbaton->data[dlbaton->size - 1] == DW_OP_GNU_push_tls_address)
514 if (dlbaton->data[0] == DW_OP_addr)
515 {
516 struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
517 struct gdbarch *gdbarch = get_objfile_arch (objfile);
518 CORE_ADDR offset = dwarf2_read_address (gdbarch,
519 &dlbaton->data[1],
520 &dlbaton->data[dlbaton->size - 1],
521 addr_size);
522 fprintf_filtered (stream,
523 "a thread-local variable at offset %s in the "
524 "thread-local storage for `%s'",
525 paddress (gdbarch, offset), objfile->name);
526 return 1;
527 }
528
529
530 fprintf_filtered (stream,
531 "a variable with complex or multiple locations (DWARF2)");
532 return 1;
533 }
534
535
536 /* Describe the location of SYMBOL as an agent value in VALUE, generating
537 any necessary bytecode in AX.
538
539 NOTE drow/2003-02-26: This function is extremely minimal, because
540 doing it correctly is extremely complicated and there is no
541 publicly available stub with tracepoint support for me to test
542 against. When there is one this function should be revisited. */
543
544 static void
545 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
546 struct agent_expr *ax, struct axs_value *value)
547 {
548 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
549
550 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
551 dlbaton->data, dlbaton->size);
552 }
553
554 /* The set of location functions used with the DWARF-2 expression
555 evaluator. */
556 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
557 locexpr_read_variable,
558 locexpr_read_needs_frame,
559 locexpr_describe_location,
560 locexpr_tracepoint_var_ref
561 };
562
563
564 /* Wrapper functions for location lists. These generally find
565 the appropriate location expression and call something above. */
566
567 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
568 evaluator to calculate the location. */
569 static struct value *
570 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
571 {
572 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
573 struct value *val;
574 gdb_byte *data;
575 size_t size;
576
577 data = find_location_expression (dlbaton, &size,
578 frame ? get_frame_address_in_block (frame)
579 : 0);
580 if (data == NULL)
581 {
582 val = allocate_value (SYMBOL_TYPE (symbol));
583 VALUE_LVAL (val) = not_lval;
584 set_value_optimized_out (val, 1);
585 }
586 else
587 val = dwarf2_evaluate_loc_desc (symbol, frame, data, size,
588 dlbaton->per_cu);
589
590 return val;
591 }
592
593 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
594 static int
595 loclist_read_needs_frame (struct symbol *symbol)
596 {
597 /* If there's a location list, then assume we need to have a frame
598 to choose the appropriate location expression. With tracking of
599 global variables this is not necessarily true, but such tracking
600 is disabled in GCC at the moment until we figure out how to
601 represent it. */
602
603 return 1;
604 }
605
606 /* Print a natural-language description of SYMBOL to STREAM. */
607 static int
608 loclist_describe_location (struct symbol *symbol, struct ui_file *stream)
609 {
610 /* FIXME: Could print the entire list of locations. */
611 fprintf_filtered (stream, "a variable with multiple locations");
612 return 1;
613 }
614
615 /* Describe the location of SYMBOL as an agent value in VALUE, generating
616 any necessary bytecode in AX. */
617 static void
618 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
619 struct agent_expr *ax, struct axs_value *value)
620 {
621 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
622 gdb_byte *data;
623 size_t size;
624
625 data = find_location_expression (dlbaton, &size, ax->scope);
626 if (data == NULL)
627 error (_("Variable \"%s\" is not available."), SYMBOL_NATURAL_NAME (symbol));
628
629 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
630 }
631
632 /* The set of location functions used with the DWARF-2 expression
633 evaluator and location lists. */
634 const struct symbol_computed_ops dwarf2_loclist_funcs = {
635 loclist_read_variable,
636 loclist_read_needs_frame,
637 loclist_describe_location,
638 loclist_tracepoint_var_ref
639 };