1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007, 2008 Free Software Foundation, Inc.
5 Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7 This file is part of GDB.
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.
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.
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/>. */
33 #include "exceptions.h"
35 #include "elf/dwarf2.h"
36 #include "dwarf2expr.h"
37 #include "dwarf2loc.h"
39 #include "gdb_string.h"
40 #include "gdb_assert.h"
42 /* A helper function for dealing with location lists. Given a
43 symbol baton (BATON) and a pc value (PC), find the appropriate
44 location expression, set *LOCEXPR_LENGTH, and return a pointer
45 to the beginning of the expression. Returns NULL on failure.
47 For now, only return the first matching location expression; there
48 can be more than one in the list. */
51 find_location_expression (struct dwarf2_loclist_baton
*baton
,
52 size_t *locexpr_length
, CORE_ADDR pc
)
55 gdb_byte
*loc_ptr
, *buf_end
;
57 struct objfile
*objfile
= dwarf2_per_cu_objfile (baton
->per_cu
);
58 unsigned int addr_size
= dwarf2_per_cu_addr_size (baton
->per_cu
);
59 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
60 /* Adjust base_address for relocatable objects. */
61 CORE_ADDR base_offset
= ANOFFSET (objfile
->section_offsets
,
62 SECT_OFF_TEXT (objfile
));
63 CORE_ADDR base_address
= baton
->base_address
+ base_offset
;
65 loc_ptr
= baton
->data
;
66 buf_end
= baton
->data
+ baton
->size
;
70 low
= dwarf2_read_address (loc_ptr
, buf_end
, addr_size
);
72 high
= dwarf2_read_address (loc_ptr
, buf_end
, addr_size
);
75 /* An end-of-list entry. */
76 if (low
== 0 && high
== 0)
79 /* A base-address-selection entry. */
80 if ((low
& base_mask
) == base_mask
)
86 /* Otherwise, a location expression entry. */
90 length
= extract_unsigned_integer (loc_ptr
, 2);
93 if (pc
>= low
&& pc
< high
)
95 *locexpr_length
= length
;
103 /* This is the baton used when performing dwarf2 expression
105 struct dwarf_expr_baton
107 struct frame_info
*frame
;
108 struct objfile
*objfile
;
111 /* Helper functions for dwarf2_evaluate_loc_desc. */
113 /* Using the frame specified in BATON, return the value of register
114 REGNUM, treated as a pointer. */
116 dwarf_expr_read_reg (void *baton
, int dwarf_regnum
)
118 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
119 struct gdbarch
*gdbarch
= get_frame_arch (debaton
->frame
);
123 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, dwarf_regnum
);
124 result
= address_from_register (builtin_type (gdbarch
)->builtin_data_ptr
,
125 regnum
, debaton
->frame
);
129 /* Read memory at ADDR (length LEN) into BUF. */
132 dwarf_expr_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
134 read_memory (addr
, buf
, len
);
137 /* Using the frame specified in BATON, find the location expression
138 describing the frame base. Return a pointer to it in START and
139 its length in LENGTH. */
141 dwarf_expr_frame_base (void *baton
, gdb_byte
**start
, size_t * length
)
143 /* FIXME: cagney/2003-03-26: This code should be using
144 get_frame_base_address(), and then implement a dwarf2 specific
146 struct symbol
*framefunc
;
147 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
149 framefunc
= get_frame_function (debaton
->frame
);
151 /* If we found a frame-relative symbol then it was certainly within
152 some function associated with a frame. If we can't find the frame,
153 something has gone wrong. */
154 gdb_assert (framefunc
!= NULL
);
156 if (SYMBOL_OPS (framefunc
) == &dwarf2_loclist_funcs
)
158 struct dwarf2_loclist_baton
*symbaton
;
159 struct frame_info
*frame
= debaton
->frame
;
161 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
162 *start
= find_location_expression (symbaton
, length
,
163 get_frame_address_in_block (frame
));
167 struct dwarf2_locexpr_baton
*symbaton
;
168 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
169 *length
= symbaton
->size
;
170 *start
= symbaton
->data
;
174 error (_("Could not find the frame base for \"%s\"."),
175 SYMBOL_NATURAL_NAME (framefunc
));
178 /* Using the objfile specified in BATON, find the address for the
179 current thread's thread-local storage with offset OFFSET. */
181 dwarf_expr_tls_address (void *baton
, CORE_ADDR offset
)
183 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
185 return target_translate_tls_address (debaton
->objfile
, offset
);
188 /* Evaluate a location description, starting at DATA and with length
189 SIZE, to find the current location of variable VAR in the context
191 static struct value
*
192 dwarf2_evaluate_loc_desc (struct symbol
*var
, struct frame_info
*frame
,
193 gdb_byte
*data
, unsigned short size
,
194 struct dwarf2_per_cu_data
*per_cu
)
196 struct gdbarch
*arch
= get_frame_arch (frame
);
197 struct value
*retval
;
198 struct dwarf_expr_baton baton
;
199 struct dwarf_expr_context
*ctx
;
203 retval
= allocate_value (SYMBOL_TYPE (var
));
204 VALUE_LVAL (retval
) = not_lval
;
205 set_value_optimized_out (retval
, 1);
210 baton
.objfile
= dwarf2_per_cu_objfile (per_cu
);
212 ctx
= new_dwarf_expr_context ();
213 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
215 ctx
->read_reg
= dwarf_expr_read_reg
;
216 ctx
->read_mem
= dwarf_expr_read_mem
;
217 ctx
->get_frame_base
= dwarf_expr_frame_base
;
218 ctx
->get_tls_address
= dwarf_expr_tls_address
;
220 dwarf_expr_eval (ctx
, data
, size
);
221 if (ctx
->num_pieces
> 0)
227 retval
= allocate_value (SYMBOL_TYPE (var
));
228 contents
= value_contents_raw (retval
);
229 for (i
= 0; i
< ctx
->num_pieces
; i
++)
231 struct dwarf_expr_piece
*p
= &ctx
->pieces
[i
];
234 bfd_byte regval
[MAX_REGISTER_SIZE
];
235 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum
237 get_frame_register (frame
, gdb_regnum
, regval
);
238 memcpy (contents
+ offset
, regval
, p
->size
);
240 else /* In memory? */
242 read_memory (p
->value
, contents
+ offset
, p
->size
);
247 else if (ctx
->in_reg
)
249 CORE_ADDR dwarf_regnum
= dwarf_expr_fetch (ctx
, 0);
250 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum
251 (arch
, dwarf_regnum
);
252 retval
= value_from_register (SYMBOL_TYPE (var
), gdb_regnum
, frame
);
256 CORE_ADDR address
= dwarf_expr_fetch (ctx
, 0);
258 retval
= allocate_value (SYMBOL_TYPE (var
));
259 VALUE_LVAL (retval
) = lval_memory
;
260 set_value_lazy (retval
, 1);
261 VALUE_ADDRESS (retval
) = address
;
264 set_value_initialized (retval
, ctx
->initialized
);
266 free_dwarf_expr_context (ctx
);
275 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
277 struct needs_frame_baton
282 /* Reads from registers do require a frame. */
284 needs_frame_read_reg (void *baton
, int regnum
)
286 struct needs_frame_baton
*nf_baton
= baton
;
287 nf_baton
->needs_frame
= 1;
291 /* Reads from memory do not require a frame. */
293 needs_frame_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
295 memset (buf
, 0, len
);
298 /* Frame-relative accesses do require a frame. */
300 needs_frame_frame_base (void *baton
, gdb_byte
**start
, size_t * length
)
302 static gdb_byte lit0
= DW_OP_lit0
;
303 struct needs_frame_baton
*nf_baton
= baton
;
308 nf_baton
->needs_frame
= 1;
311 /* Thread-local accesses do require a frame. */
313 needs_frame_tls_address (void *baton
, CORE_ADDR offset
)
315 struct needs_frame_baton
*nf_baton
= baton
;
316 nf_baton
->needs_frame
= 1;
320 /* Return non-zero iff the location expression at DATA (length SIZE)
321 requires a frame to evaluate. */
324 dwarf2_loc_desc_needs_frame (gdb_byte
*data
, unsigned short size
,
325 struct dwarf2_per_cu_data
*per_cu
)
327 struct needs_frame_baton baton
;
328 struct dwarf_expr_context
*ctx
;
331 baton
.needs_frame
= 0;
333 ctx
= new_dwarf_expr_context ();
334 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
336 ctx
->read_reg
= needs_frame_read_reg
;
337 ctx
->read_mem
= needs_frame_read_mem
;
338 ctx
->get_frame_base
= needs_frame_frame_base
;
339 ctx
->get_tls_address
= needs_frame_tls_address
;
341 dwarf_expr_eval (ctx
, data
, size
);
343 in_reg
= ctx
->in_reg
;
345 if (ctx
->num_pieces
> 0)
349 /* If the location has several pieces, and any of them are in
350 registers, then we will need a frame to fetch them from. */
351 for (i
= 0; i
< ctx
->num_pieces
; i
++)
352 if (ctx
->pieces
[i
].in_reg
)
356 free_dwarf_expr_context (ctx
);
358 return baton
.needs_frame
|| in_reg
;
362 dwarf2_tracepoint_var_ref (struct symbol
*symbol
, struct agent_expr
*ax
,
363 struct axs_value
*value
, gdb_byte
*data
,
367 error (_("Symbol \"%s\" has been optimized out."),
368 SYMBOL_PRINT_NAME (symbol
));
371 && data
[0] >= DW_OP_reg0
372 && data
[0] <= DW_OP_reg31
)
374 value
->kind
= axs_lvalue_register
;
375 value
->u
.reg
= data
[0] - DW_OP_reg0
;
377 else if (data
[0] == DW_OP_regx
)
380 read_uleb128 (data
+ 1, data
+ size
, ®
);
381 value
->kind
= axs_lvalue_register
;
384 else if (data
[0] == DW_OP_fbreg
)
386 /* And this is worse than just minimal; we should honor the frame base
389 LONGEST frame_offset
;
392 buf_end
= read_sleb128 (data
+ 1, data
+ size
, &frame_offset
);
393 if (buf_end
!= data
+ size
)
394 error (_("Unexpected opcode after DW_OP_fbreg for symbol \"%s\"."),
395 SYMBOL_PRINT_NAME (symbol
));
397 gdbarch_virtual_frame_pointer (current_gdbarch
,
398 ax
->scope
, &frame_reg
, &frame_offset
);
399 ax_reg (ax
, frame_reg
);
400 ax_const_l (ax
, frame_offset
);
401 ax_simple (ax
, aop_add
);
403 value
->kind
= axs_lvalue_memory
;
405 else if (data
[0] >= DW_OP_breg0
406 && data
[0] <= DW_OP_breg31
)
412 reg
= data
[0] - DW_OP_breg0
;
413 buf_end
= read_sleb128 (data
+ 1, data
+ size
, &offset
);
414 if (buf_end
!= data
+ size
)
415 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
416 reg
, SYMBOL_PRINT_NAME (symbol
));
419 ax_const_l (ax
, offset
);
420 ax_simple (ax
, aop_add
);
422 value
->kind
= axs_lvalue_memory
;
425 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
426 data
[0], SYMBOL_PRINT_NAME (symbol
));
429 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
430 evaluator to calculate the location. */
431 static struct value
*
432 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
434 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
436 val
= dwarf2_evaluate_loc_desc (symbol
, frame
, dlbaton
->data
, dlbaton
->size
,
442 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
444 locexpr_read_needs_frame (struct symbol
*symbol
)
446 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
447 return dwarf2_loc_desc_needs_frame (dlbaton
->data
, dlbaton
->size
,
451 /* Print a natural-language description of SYMBOL to STREAM. */
453 locexpr_describe_location (struct symbol
*symbol
, struct ui_file
*stream
)
455 /* FIXME: be more extensive. */
456 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
457 int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
459 if (dlbaton
->size
== 1
460 && dlbaton
->data
[0] >= DW_OP_reg0
461 && dlbaton
->data
[0] <= DW_OP_reg31
)
463 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
464 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
465 int regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
,
466 dlbaton
->data
[0] - DW_OP_reg0
);
467 fprintf_filtered (stream
,
468 "a variable in register %s",
469 gdbarch_register_name (gdbarch
, regno
));
473 /* The location expression for a TLS variable looks like this (on a
476 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
477 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
479 0x3 is the encoding for DW_OP_addr, which has an operand as long
480 as the size of an address on the target machine (here is 8
481 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
482 The operand represents the offset at which the variable is within
483 the thread local storage. */
485 if (dlbaton
->size
> 1
486 && dlbaton
->data
[dlbaton
->size
- 1] == DW_OP_GNU_push_tls_address
)
487 if (dlbaton
->data
[0] == DW_OP_addr
)
489 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
490 CORE_ADDR offset
= dwarf2_read_address (&dlbaton
->data
[1],
491 &dlbaton
->data
[dlbaton
->size
- 1],
493 fprintf_filtered (stream
,
494 "a thread-local variable at offset %s in the "
495 "thread-local storage for `%s'",
496 paddr_nz (offset
), objfile
->name
);
501 fprintf_filtered (stream
,
502 "a variable with complex or multiple locations (DWARF2)");
507 /* Describe the location of SYMBOL as an agent value in VALUE, generating
508 any necessary bytecode in AX.
510 NOTE drow/2003-02-26: This function is extremely minimal, because
511 doing it correctly is extremely complicated and there is no
512 publicly available stub with tracepoint support for me to test
513 against. When there is one this function should be revisited. */
516 locexpr_tracepoint_var_ref (struct symbol
* symbol
, struct agent_expr
* ax
,
517 struct axs_value
* value
)
519 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
521 dwarf2_tracepoint_var_ref (symbol
, ax
, value
, dlbaton
->data
, dlbaton
->size
);
524 /* The set of location functions used with the DWARF-2 expression
526 const struct symbol_ops dwarf2_locexpr_funcs
= {
527 locexpr_read_variable
,
528 locexpr_read_needs_frame
,
529 locexpr_describe_location
,
530 locexpr_tracepoint_var_ref
534 /* Wrapper functions for location lists. These generally find
535 the appropriate location expression and call something above. */
537 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
538 evaluator to calculate the location. */
539 static struct value
*
540 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
542 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
547 data
= find_location_expression (dlbaton
, &size
,
548 frame
? get_frame_address_in_block (frame
)
552 val
= allocate_value (SYMBOL_TYPE (symbol
));
553 VALUE_LVAL (val
) = not_lval
;
554 set_value_optimized_out (val
, 1);
557 val
= dwarf2_evaluate_loc_desc (symbol
, frame
, data
, size
,
563 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
565 loclist_read_needs_frame (struct symbol
*symbol
)
567 /* If there's a location list, then assume we need to have a frame
568 to choose the appropriate location expression. With tracking of
569 global variables this is not necessarily true, but such tracking
570 is disabled in GCC at the moment until we figure out how to
576 /* Print a natural-language description of SYMBOL to STREAM. */
578 loclist_describe_location (struct symbol
*symbol
, struct ui_file
*stream
)
580 /* FIXME: Could print the entire list of locations. */
581 fprintf_filtered (stream
, "a variable with multiple locations");
585 /* Describe the location of SYMBOL as an agent value in VALUE, generating
586 any necessary bytecode in AX. */
588 loclist_tracepoint_var_ref (struct symbol
* symbol
, struct agent_expr
* ax
,
589 struct axs_value
* value
)
591 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
595 data
= find_location_expression (dlbaton
, &size
, ax
->scope
);
597 error (_("Variable \"%s\" is not available."), SYMBOL_NATURAL_NAME (symbol
));
599 dwarf2_tracepoint_var_ref (symbol
, ax
, value
, data
, size
);
602 /* The set of location functions used with the DWARF-2 expression
603 evaluator and location lists. */
604 const struct symbol_ops dwarf2_loclist_funcs
= {
605 loclist_read_variable
,
606 loclist_read_needs_frame
,
607 loclist_describe_location
,
608 loclist_tracepoint_var_ref