doc/ChangeLog:
[deliverable/binutils-gdb.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3 Copyright (C) 2003, 2005, 2007, 2008 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
35 #include "elf/dwarf2.h"
36 #include "dwarf2expr.h"
37 #include "dwarf2loc.h"
38
39 #include "gdb_string.h"
40 #include "gdb_assert.h"
41
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.
46
47 For now, only return the first matching location expression; there
48 can be more than one in the list. */
49
50 static gdb_byte *
51 find_location_expression (struct dwarf2_loclist_baton *baton,
52 size_t *locexpr_length, CORE_ADDR pc)
53 {
54 CORE_ADDR low, high;
55 gdb_byte *loc_ptr, *buf_end;
56 int length;
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;
64
65 loc_ptr = baton->data;
66 buf_end = baton->data + baton->size;
67
68 while (1)
69 {
70 low = dwarf2_read_address (loc_ptr, buf_end, addr_size);
71 loc_ptr += addr_size;
72 high = dwarf2_read_address (loc_ptr, buf_end, addr_size);
73 loc_ptr += addr_size;
74
75 /* An end-of-list entry. */
76 if (low == 0 && high == 0)
77 return NULL;
78
79 /* A base-address-selection entry. */
80 if ((low & base_mask) == base_mask)
81 {
82 base_address = high;
83 continue;
84 }
85
86 /* Otherwise, a location expression entry. */
87 low += base_address;
88 high += base_address;
89
90 length = extract_unsigned_integer (loc_ptr, 2);
91 loc_ptr += 2;
92
93 if (pc >= low && pc < high)
94 {
95 *locexpr_length = length;
96 return loc_ptr;
97 }
98
99 loc_ptr += length;
100 }
101 }
102
103 /* This is the baton used when performing dwarf2 expression
104 evaluation. */
105 struct dwarf_expr_baton
106 {
107 struct frame_info *frame;
108 struct objfile *objfile;
109 };
110
111 /* Helper functions for dwarf2_evaluate_loc_desc. */
112
113 /* Using the frame specified in BATON, return the value of register
114 REGNUM, treated as a pointer. */
115 static CORE_ADDR
116 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
117 {
118 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
119 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
120 CORE_ADDR result;
121 int regnum;
122
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);
126 return result;
127 }
128
129 /* Read memory at ADDR (length LEN) into BUF. */
130
131 static void
132 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
133 {
134 read_memory (addr, buf, len);
135 }
136
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. */
140 static void
141 dwarf_expr_frame_base (void *baton, gdb_byte **start, size_t * length)
142 {
143 /* FIXME: cagney/2003-03-26: This code should be using
144 get_frame_base_address(), and then implement a dwarf2 specific
145 this_base method. */
146 struct symbol *framefunc;
147 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
148
149 framefunc = get_frame_function (debaton->frame);
150
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);
155
156 if (SYMBOL_OPS (framefunc) == &dwarf2_loclist_funcs)
157 {
158 struct dwarf2_loclist_baton *symbaton;
159 struct frame_info *frame = debaton->frame;
160
161 symbaton = SYMBOL_LOCATION_BATON (framefunc);
162 *start = find_location_expression (symbaton, length,
163 get_frame_address_in_block (frame));
164 }
165 else
166 {
167 struct dwarf2_locexpr_baton *symbaton;
168 symbaton = SYMBOL_LOCATION_BATON (framefunc);
169 *length = symbaton->size;
170 *start = symbaton->data;
171 }
172
173 if (*start == NULL)
174 error (_("Could not find the frame base for \"%s\"."),
175 SYMBOL_NATURAL_NAME (framefunc));
176 }
177
178 /* Using the objfile specified in BATON, find the address for the
179 current thread's thread-local storage with offset OFFSET. */
180 static CORE_ADDR
181 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
182 {
183 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
184
185 return target_translate_tls_address (debaton->objfile, offset);
186 }
187
188 /* Evaluate a location description, starting at DATA and with length
189 SIZE, to find the current location of variable VAR in the context
190 of FRAME. */
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)
195 {
196 struct gdbarch *arch = get_frame_arch (frame);
197 struct value *retval;
198 struct dwarf_expr_baton baton;
199 struct dwarf_expr_context *ctx;
200
201 if (size == 0)
202 {
203 retval = allocate_value (SYMBOL_TYPE (var));
204 VALUE_LVAL (retval) = not_lval;
205 set_value_optimized_out (retval, 1);
206 return retval;
207 }
208
209 baton.frame = frame;
210 baton.objfile = dwarf2_per_cu_objfile (per_cu);
211
212 ctx = new_dwarf_expr_context ();
213 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
214 ctx->baton = &baton;
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;
219
220 dwarf_expr_eval (ctx, data, size);
221 if (ctx->num_pieces > 0)
222 {
223 int i;
224 long offset = 0;
225 bfd_byte *contents;
226
227 retval = allocate_value (SYMBOL_TYPE (var));
228 contents = value_contents_raw (retval);
229 for (i = 0; i < ctx->num_pieces; i++)
230 {
231 struct dwarf_expr_piece *p = &ctx->pieces[i];
232 if (p->in_reg)
233 {
234 bfd_byte regval[MAX_REGISTER_SIZE];
235 int gdb_regnum = gdbarch_dwarf2_reg_to_regnum
236 (arch, p->value);
237 get_frame_register (frame, gdb_regnum, regval);
238 memcpy (contents + offset, regval, p->size);
239 }
240 else /* In memory? */
241 {
242 read_memory (p->value, contents + offset, p->size);
243 }
244 offset += p->size;
245 }
246 }
247 else if (ctx->in_reg)
248 {
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);
253 }
254 else
255 {
256 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
257
258 retval = allocate_value (SYMBOL_TYPE (var));
259 VALUE_LVAL (retval) = lval_memory;
260 set_value_lazy (retval, 1);
261 VALUE_ADDRESS (retval) = address;
262 }
263
264 set_value_initialized (retval, ctx->initialized);
265
266 free_dwarf_expr_context (ctx);
267
268 return retval;
269 }
270
271
272
273
274 \f
275 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
276
277 struct needs_frame_baton
278 {
279 int needs_frame;
280 };
281
282 /* Reads from registers do require a frame. */
283 static CORE_ADDR
284 needs_frame_read_reg (void *baton, int regnum)
285 {
286 struct needs_frame_baton *nf_baton = baton;
287 nf_baton->needs_frame = 1;
288 return 1;
289 }
290
291 /* Reads from memory do not require a frame. */
292 static void
293 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
294 {
295 memset (buf, 0, len);
296 }
297
298 /* Frame-relative accesses do require a frame. */
299 static void
300 needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length)
301 {
302 static gdb_byte lit0 = DW_OP_lit0;
303 struct needs_frame_baton *nf_baton = baton;
304
305 *start = &lit0;
306 *length = 1;
307
308 nf_baton->needs_frame = 1;
309 }
310
311 /* Thread-local accesses do require a frame. */
312 static CORE_ADDR
313 needs_frame_tls_address (void *baton, CORE_ADDR offset)
314 {
315 struct needs_frame_baton *nf_baton = baton;
316 nf_baton->needs_frame = 1;
317 return 1;
318 }
319
320 /* Return non-zero iff the location expression at DATA (length SIZE)
321 requires a frame to evaluate. */
322
323 static int
324 dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size,
325 struct dwarf2_per_cu_data *per_cu)
326 {
327 struct needs_frame_baton baton;
328 struct dwarf_expr_context *ctx;
329 int in_reg;
330
331 baton.needs_frame = 0;
332
333 ctx = new_dwarf_expr_context ();
334 ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
335 ctx->baton = &baton;
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;
340
341 dwarf_expr_eval (ctx, data, size);
342
343 in_reg = ctx->in_reg;
344
345 if (ctx->num_pieces > 0)
346 {
347 int i;
348
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)
353 in_reg = 1;
354 }
355
356 free_dwarf_expr_context (ctx);
357
358 return baton.needs_frame || in_reg;
359 }
360
361 static void
362 dwarf2_tracepoint_var_ref (struct symbol *symbol, struct agent_expr *ax,
363 struct axs_value *value, gdb_byte *data,
364 int size)
365 {
366 if (size == 0)
367 error (_("Symbol \"%s\" has been optimized out."),
368 SYMBOL_PRINT_NAME (symbol));
369
370 if (size == 1
371 && data[0] >= DW_OP_reg0
372 && data[0] <= DW_OP_reg31)
373 {
374 value->kind = axs_lvalue_register;
375 value->u.reg = data[0] - DW_OP_reg0;
376 }
377 else if (data[0] == DW_OP_regx)
378 {
379 ULONGEST reg;
380 read_uleb128 (data + 1, data + size, &reg);
381 value->kind = axs_lvalue_register;
382 value->u.reg = reg;
383 }
384 else if (data[0] == DW_OP_fbreg)
385 {
386 /* And this is worse than just minimal; we should honor the frame base
387 as above. */
388 int frame_reg;
389 LONGEST frame_offset;
390 gdb_byte *buf_end;
391
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));
396
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);
402
403 value->kind = axs_lvalue_memory;
404 }
405 else if (data[0] >= DW_OP_breg0
406 && data[0] <= DW_OP_breg31)
407 {
408 unsigned int reg;
409 LONGEST offset;
410 gdb_byte *buf_end;
411
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));
417
418 ax_reg (ax, reg);
419 ax_const_l (ax, offset);
420 ax_simple (ax, aop_add);
421
422 value->kind = axs_lvalue_memory;
423 }
424 else
425 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
426 data[0], SYMBOL_PRINT_NAME (symbol));
427 }
428 \f
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)
433 {
434 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
435 struct value *val;
436 val = dwarf2_evaluate_loc_desc (symbol, frame, dlbaton->data, dlbaton->size,
437 dlbaton->per_cu);
438
439 return val;
440 }
441
442 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
443 static int
444 locexpr_read_needs_frame (struct symbol *symbol)
445 {
446 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
447 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
448 dlbaton->per_cu);
449 }
450
451 /* Print a natural-language description of SYMBOL to STREAM. */
452 static int
453 locexpr_describe_location (struct symbol *symbol, struct ui_file *stream)
454 {
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);
458
459 if (dlbaton->size == 1
460 && dlbaton->data[0] >= DW_OP_reg0
461 && dlbaton->data[0] <= DW_OP_reg31)
462 {
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));
470 return 1;
471 }
472
473 /* The location expression for a TLS variable looks like this (on a
474 64-bit LE machine):
475
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)
478
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. */
484
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)
488 {
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],
492 addr_size);
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);
497 return 1;
498 }
499
500
501 fprintf_filtered (stream,
502 "a variable with complex or multiple locations (DWARF2)");
503 return 1;
504 }
505
506
507 /* Describe the location of SYMBOL as an agent value in VALUE, generating
508 any necessary bytecode in AX.
509
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. */
514
515 static void
516 locexpr_tracepoint_var_ref (struct symbol * symbol, struct agent_expr * ax,
517 struct axs_value * value)
518 {
519 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
520
521 dwarf2_tracepoint_var_ref (symbol, ax, value, dlbaton->data, dlbaton->size);
522 }
523
524 /* The set of location functions used with the DWARF-2 expression
525 evaluator. */
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
531 };
532
533
534 /* Wrapper functions for location lists. These generally find
535 the appropriate location expression and call something above. */
536
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)
541 {
542 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
543 struct value *val;
544 gdb_byte *data;
545 size_t size;
546
547 data = find_location_expression (dlbaton, &size,
548 frame ? get_frame_address_in_block (frame)
549 : 0);
550 if (data == NULL)
551 {
552 val = allocate_value (SYMBOL_TYPE (symbol));
553 VALUE_LVAL (val) = not_lval;
554 set_value_optimized_out (val, 1);
555 }
556 else
557 val = dwarf2_evaluate_loc_desc (symbol, frame, data, size,
558 dlbaton->per_cu);
559
560 return val;
561 }
562
563 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
564 static int
565 loclist_read_needs_frame (struct symbol *symbol)
566 {
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
571 represent it. */
572
573 return 1;
574 }
575
576 /* Print a natural-language description of SYMBOL to STREAM. */
577 static int
578 loclist_describe_location (struct symbol *symbol, struct ui_file *stream)
579 {
580 /* FIXME: Could print the entire list of locations. */
581 fprintf_filtered (stream, "a variable with multiple locations");
582 return 1;
583 }
584
585 /* Describe the location of SYMBOL as an agent value in VALUE, generating
586 any necessary bytecode in AX. */
587 static void
588 loclist_tracepoint_var_ref (struct symbol * symbol, struct agent_expr * ax,
589 struct axs_value * value)
590 {
591 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
592 gdb_byte *data;
593 size_t size;
594
595 data = find_location_expression (dlbaton, &size, ax->scope);
596 if (data == NULL)
597 error (_("Variable \"%s\" is not available."), SYMBOL_NATURAL_NAME (symbol));
598
599 dwarf2_tracepoint_var_ref (symbol, ax, value, data, size);
600 }
601
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
609 };
This page took 0.043687 seconds and 4 git commands to generate.