1 /* DWARF 2 location expression support for GDB.
3 Copyright (C) 2003, 2005, 2007, 2008, 2009 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"
36 #include "elf/dwarf2.h"
37 #include "dwarf2expr.h"
38 #include "dwarf2loc.h"
40 #include "gdb_string.h"
41 #include "gdb_assert.h"
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.
48 For now, only return the first matching location expression; there
49 can be more than one in the list. */
52 find_location_expression (struct dwarf2_loclist_baton
*baton
,
53 size_t *locexpr_length
, CORE_ADDR pc
)
56 gdb_byte
*loc_ptr
, *buf_end
;
58 struct objfile
*objfile
= dwarf2_per_cu_objfile (baton
->per_cu
);
59 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
60 unsigned int addr_size
= dwarf2_per_cu_addr_size (baton
->per_cu
);
61 CORE_ADDR base_mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
62 /* Adjust base_address for relocatable objects. */
63 CORE_ADDR base_offset
= ANOFFSET (objfile
->section_offsets
,
64 SECT_OFF_TEXT (objfile
));
65 CORE_ADDR base_address
= baton
->base_address
+ base_offset
;
67 loc_ptr
= baton
->data
;
68 buf_end
= baton
->data
+ baton
->size
;
72 low
= dwarf2_read_address (gdbarch
, loc_ptr
, buf_end
, addr_size
);
74 high
= dwarf2_read_address (gdbarch
, loc_ptr
, buf_end
, addr_size
);
77 /* An end-of-list entry. */
78 if (low
== 0 && high
== 0)
81 /* A base-address-selection entry. */
82 if ((low
& base_mask
) == base_mask
)
88 /* Otherwise, a location expression entry. */
92 length
= extract_unsigned_integer (loc_ptr
, 2);
95 if (pc
>= low
&& pc
< high
)
97 *locexpr_length
= length
;
105 /* This is the baton used when performing dwarf2 expression
107 struct dwarf_expr_baton
109 struct frame_info
*frame
;
110 struct objfile
*objfile
;
113 /* Helper functions for dwarf2_evaluate_loc_desc. */
115 /* Using the frame specified in BATON, return the value of register
116 REGNUM, treated as a pointer. */
118 dwarf_expr_read_reg (void *baton
, int dwarf_regnum
)
120 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
121 struct gdbarch
*gdbarch
= get_frame_arch (debaton
->frame
);
125 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, dwarf_regnum
);
126 result
= address_from_register (builtin_type (gdbarch
)->builtin_data_ptr
,
127 regnum
, debaton
->frame
);
131 /* Read memory at ADDR (length LEN) into BUF. */
134 dwarf_expr_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
136 read_memory (addr
, buf
, len
);
139 /* Using the frame specified in BATON, find the location expression
140 describing the frame base. Return a pointer to it in START and
141 its length in LENGTH. */
143 dwarf_expr_frame_base (void *baton
, gdb_byte
**start
, size_t * length
)
145 /* FIXME: cagney/2003-03-26: This code should be using
146 get_frame_base_address(), and then implement a dwarf2 specific
148 struct symbol
*framefunc
;
149 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
151 /* Use block_linkage_function, which returns a real (not inlined)
152 function, instead of get_frame_function, which may return an
154 framefunc
= block_linkage_function (get_frame_block (debaton
->frame
, NULL
));
156 /* If we found a frame-relative symbol then it was certainly within
157 some function associated with a frame. If we can't find the frame,
158 something has gone wrong. */
159 gdb_assert (framefunc
!= NULL
);
161 if (SYMBOL_LOCATION_BATON (framefunc
) == NULL
)
163 else if (SYMBOL_COMPUTED_OPS (framefunc
) == &dwarf2_loclist_funcs
)
165 struct dwarf2_loclist_baton
*symbaton
;
166 struct frame_info
*frame
= debaton
->frame
;
168 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
169 *start
= find_location_expression (symbaton
, length
,
170 get_frame_address_in_block (frame
));
174 struct dwarf2_locexpr_baton
*symbaton
;
175 symbaton
= SYMBOL_LOCATION_BATON (framefunc
);
176 if (symbaton
!= NULL
)
178 *length
= symbaton
->size
;
179 *start
= symbaton
->data
;
186 error (_("Could not find the frame base for \"%s\"."),
187 SYMBOL_NATURAL_NAME (framefunc
));
190 /* Using the objfile specified in BATON, find the address for the
191 current thread's thread-local storage with offset OFFSET. */
193 dwarf_expr_tls_address (void *baton
, CORE_ADDR offset
)
195 struct dwarf_expr_baton
*debaton
= (struct dwarf_expr_baton
*) baton
;
197 return target_translate_tls_address (debaton
->objfile
, offset
);
200 /* Evaluate a location description, starting at DATA and with length
201 SIZE, to find the current location of variable VAR in the context
203 static struct value
*
204 dwarf2_evaluate_loc_desc (struct symbol
*var
, struct frame_info
*frame
,
205 gdb_byte
*data
, unsigned short size
,
206 struct dwarf2_per_cu_data
*per_cu
)
208 struct value
*retval
;
209 struct dwarf_expr_baton baton
;
210 struct dwarf_expr_context
*ctx
;
214 retval
= allocate_value (SYMBOL_TYPE (var
));
215 VALUE_LVAL (retval
) = not_lval
;
216 set_value_optimized_out (retval
, 1);
221 baton
.objfile
= dwarf2_per_cu_objfile (per_cu
);
223 ctx
= new_dwarf_expr_context ();
224 ctx
->gdbarch
= get_objfile_arch (baton
.objfile
);
225 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
227 ctx
->read_reg
= dwarf_expr_read_reg
;
228 ctx
->read_mem
= dwarf_expr_read_mem
;
229 ctx
->get_frame_base
= dwarf_expr_frame_base
;
230 ctx
->get_tls_address
= dwarf_expr_tls_address
;
232 dwarf_expr_eval (ctx
, data
, size
);
233 if (ctx
->num_pieces
> 0)
239 retval
= allocate_value (SYMBOL_TYPE (var
));
240 contents
= value_contents_raw (retval
);
241 for (i
= 0; i
< ctx
->num_pieces
; i
++)
243 struct dwarf_expr_piece
*p
= &ctx
->pieces
[i
];
246 struct gdbarch
*arch
= get_frame_arch (frame
);
247 bfd_byte regval
[MAX_REGISTER_SIZE
];
248 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, p
->value
);
249 get_frame_register (frame
, gdb_regnum
, regval
);
250 memcpy (contents
+ offset
, regval
, p
->size
);
252 else /* In memory? */
254 read_memory (p
->value
, contents
+ offset
, p
->size
);
259 else if (ctx
->in_reg
)
261 struct gdbarch
*arch
= get_frame_arch (frame
);
262 CORE_ADDR dwarf_regnum
= dwarf_expr_fetch (ctx
, 0);
263 int gdb_regnum
= gdbarch_dwarf2_reg_to_regnum (arch
, dwarf_regnum
);
264 retval
= value_from_register (SYMBOL_TYPE (var
), gdb_regnum
, frame
);
268 CORE_ADDR address
= dwarf_expr_fetch (ctx
, 0);
270 retval
= allocate_value (SYMBOL_TYPE (var
));
271 VALUE_LVAL (retval
) = lval_memory
;
272 set_value_lazy (retval
, 1);
273 set_value_address (retval
, address
);
276 set_value_initialized (retval
, ctx
->initialized
);
278 free_dwarf_expr_context (ctx
);
287 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
289 struct needs_frame_baton
294 /* Reads from registers do require a frame. */
296 needs_frame_read_reg (void *baton
, int regnum
)
298 struct needs_frame_baton
*nf_baton
= baton
;
299 nf_baton
->needs_frame
= 1;
303 /* Reads from memory do not require a frame. */
305 needs_frame_read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
307 memset (buf
, 0, len
);
310 /* Frame-relative accesses do require a frame. */
312 needs_frame_frame_base (void *baton
, gdb_byte
**start
, size_t * length
)
314 static gdb_byte lit0
= DW_OP_lit0
;
315 struct needs_frame_baton
*nf_baton
= baton
;
320 nf_baton
->needs_frame
= 1;
323 /* Thread-local accesses do require a frame. */
325 needs_frame_tls_address (void *baton
, CORE_ADDR offset
)
327 struct needs_frame_baton
*nf_baton
= baton
;
328 nf_baton
->needs_frame
= 1;
332 /* Return non-zero iff the location expression at DATA (length SIZE)
333 requires a frame to evaluate. */
336 dwarf2_loc_desc_needs_frame (gdb_byte
*data
, unsigned short size
,
337 struct dwarf2_per_cu_data
*per_cu
)
339 struct needs_frame_baton baton
;
340 struct dwarf_expr_context
*ctx
;
343 baton
.needs_frame
= 0;
345 ctx
= new_dwarf_expr_context ();
346 ctx
->gdbarch
= get_objfile_arch (dwarf2_per_cu_objfile (per_cu
));
347 ctx
->addr_size
= dwarf2_per_cu_addr_size (per_cu
);
349 ctx
->read_reg
= needs_frame_read_reg
;
350 ctx
->read_mem
= needs_frame_read_mem
;
351 ctx
->get_frame_base
= needs_frame_frame_base
;
352 ctx
->get_tls_address
= needs_frame_tls_address
;
354 dwarf_expr_eval (ctx
, data
, size
);
356 in_reg
= ctx
->in_reg
;
358 if (ctx
->num_pieces
> 0)
362 /* If the location has several pieces, and any of them are in
363 registers, then we will need a frame to fetch them from. */
364 for (i
= 0; i
< ctx
->num_pieces
; i
++)
365 if (ctx
->pieces
[i
].in_reg
)
369 free_dwarf_expr_context (ctx
);
371 return baton
.needs_frame
|| in_reg
;
375 dwarf2_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
376 struct agent_expr
*ax
, struct axs_value
*value
,
377 gdb_byte
*data
, int size
)
380 error (_("Symbol \"%s\" has been optimized out."),
381 SYMBOL_PRINT_NAME (symbol
));
384 && data
[0] >= DW_OP_reg0
385 && data
[0] <= DW_OP_reg31
)
387 value
->kind
= axs_lvalue_register
;
388 value
->u
.reg
= data
[0] - DW_OP_reg0
;
390 else if (data
[0] == DW_OP_regx
)
393 read_uleb128 (data
+ 1, data
+ size
, ®
);
394 value
->kind
= axs_lvalue_register
;
397 else if (data
[0] == DW_OP_fbreg
)
399 /* And this is worse than just minimal; we should honor the frame base
402 LONGEST frame_offset
;
405 buf_end
= read_sleb128 (data
+ 1, data
+ size
, &frame_offset
);
406 if (buf_end
!= data
+ size
)
407 error (_("Unexpected opcode after DW_OP_fbreg for symbol \"%s\"."),
408 SYMBOL_PRINT_NAME (symbol
));
410 gdbarch_virtual_frame_pointer (gdbarch
,
411 ax
->scope
, &frame_reg
, &frame_offset
);
412 ax_reg (ax
, frame_reg
);
413 ax_const_l (ax
, frame_offset
);
414 ax_simple (ax
, aop_add
);
416 value
->kind
= axs_lvalue_memory
;
418 else if (data
[0] >= DW_OP_breg0
419 && data
[0] <= DW_OP_breg31
)
425 reg
= data
[0] - DW_OP_breg0
;
426 buf_end
= read_sleb128 (data
+ 1, data
+ size
, &offset
);
427 if (buf_end
!= data
+ size
)
428 error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
429 reg
, SYMBOL_PRINT_NAME (symbol
));
432 ax_const_l (ax
, offset
);
433 ax_simple (ax
, aop_add
);
435 value
->kind
= axs_lvalue_memory
;
438 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
439 data
[0], SYMBOL_PRINT_NAME (symbol
));
442 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
443 evaluator to calculate the location. */
444 static struct value
*
445 locexpr_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
447 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
449 val
= dwarf2_evaluate_loc_desc (symbol
, frame
, dlbaton
->data
, dlbaton
->size
,
455 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
457 locexpr_read_needs_frame (struct symbol
*symbol
)
459 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
460 return dwarf2_loc_desc_needs_frame (dlbaton
->data
, dlbaton
->size
,
464 /* Print a natural-language description of SYMBOL to STREAM. */
466 locexpr_describe_location (struct symbol
*symbol
, struct ui_file
*stream
)
468 /* FIXME: be more extensive. */
469 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
470 int addr_size
= dwarf2_per_cu_addr_size (dlbaton
->per_cu
);
472 if (dlbaton
->size
== 1
473 && dlbaton
->data
[0] >= DW_OP_reg0
474 && dlbaton
->data
[0] <= DW_OP_reg31
)
476 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
477 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
478 int regno
= gdbarch_dwarf2_reg_to_regnum (gdbarch
,
479 dlbaton
->data
[0] - DW_OP_reg0
);
480 fprintf_filtered (stream
,
481 "a variable in register %s",
482 gdbarch_register_name (gdbarch
, regno
));
486 /* The location expression for a TLS variable looks like this (on a
489 DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
490 (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
492 0x3 is the encoding for DW_OP_addr, which has an operand as long
493 as the size of an address on the target machine (here is 8
494 bytes). 0xe0 is the encoding for DW_OP_GNU_push_tls_address.
495 The operand represents the offset at which the variable is within
496 the thread local storage. */
498 if (dlbaton
->size
> 1
499 && dlbaton
->data
[dlbaton
->size
- 1] == DW_OP_GNU_push_tls_address
)
500 if (dlbaton
->data
[0] == DW_OP_addr
)
502 struct objfile
*objfile
= dwarf2_per_cu_objfile (dlbaton
->per_cu
);
503 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
504 CORE_ADDR offset
= dwarf2_read_address (gdbarch
,
506 &dlbaton
->data
[dlbaton
->size
- 1],
508 fprintf_filtered (stream
,
509 "a thread-local variable at offset %s in the "
510 "thread-local storage for `%s'",
511 paddress (gdbarch
, offset
), objfile
->name
);
516 fprintf_filtered (stream
,
517 "a variable with complex or multiple locations (DWARF2)");
522 /* Describe the location of SYMBOL as an agent value in VALUE, generating
523 any necessary bytecode in AX.
525 NOTE drow/2003-02-26: This function is extremely minimal, because
526 doing it correctly is extremely complicated and there is no
527 publicly available stub with tracepoint support for me to test
528 against. When there is one this function should be revisited. */
531 locexpr_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
532 struct agent_expr
*ax
, struct axs_value
*value
)
534 struct dwarf2_locexpr_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
536 dwarf2_tracepoint_var_ref (symbol
, gdbarch
, ax
, value
,
537 dlbaton
->data
, dlbaton
->size
);
540 /* The set of location functions used with the DWARF-2 expression
542 const struct symbol_computed_ops dwarf2_locexpr_funcs
= {
543 locexpr_read_variable
,
544 locexpr_read_needs_frame
,
545 locexpr_describe_location
,
546 locexpr_tracepoint_var_ref
550 /* Wrapper functions for location lists. These generally find
551 the appropriate location expression and call something above. */
553 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
554 evaluator to calculate the location. */
555 static struct value
*
556 loclist_read_variable (struct symbol
*symbol
, struct frame_info
*frame
)
558 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
563 data
= find_location_expression (dlbaton
, &size
,
564 frame
? get_frame_address_in_block (frame
)
568 val
= allocate_value (SYMBOL_TYPE (symbol
));
569 VALUE_LVAL (val
) = not_lval
;
570 set_value_optimized_out (val
, 1);
573 val
= dwarf2_evaluate_loc_desc (symbol
, frame
, data
, size
,
579 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
581 loclist_read_needs_frame (struct symbol
*symbol
)
583 /* If there's a location list, then assume we need to have a frame
584 to choose the appropriate location expression. With tracking of
585 global variables this is not necessarily true, but such tracking
586 is disabled in GCC at the moment until we figure out how to
592 /* Print a natural-language description of SYMBOL to STREAM. */
594 loclist_describe_location (struct symbol
*symbol
, struct ui_file
*stream
)
596 /* FIXME: Could print the entire list of locations. */
597 fprintf_filtered (stream
, "a variable with multiple locations");
601 /* Describe the location of SYMBOL as an agent value in VALUE, generating
602 any necessary bytecode in AX. */
604 loclist_tracepoint_var_ref (struct symbol
*symbol
, struct gdbarch
*gdbarch
,
605 struct agent_expr
*ax
, struct axs_value
*value
)
607 struct dwarf2_loclist_baton
*dlbaton
= SYMBOL_LOCATION_BATON (symbol
);
611 data
= find_location_expression (dlbaton
, &size
, ax
->scope
);
613 error (_("Variable \"%s\" is not available."), SYMBOL_NATURAL_NAME (symbol
));
615 dwarf2_tracepoint_var_ref (symbol
, gdbarch
, ax
, value
, data
, size
);
618 /* The set of location functions used with the DWARF-2 expression
619 evaluator and location lists. */
620 const struct symbol_computed_ops dwarf2_loclist_funcs
= {
621 loclist_read_variable
,
622 loclist_read_needs_frame
,
623 loclist_describe_location
,
624 loclist_tracepoint_var_ref