* disasm.h (gdb_disassembly): Add GDBARCH parameter.
[deliverable/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 "elf/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 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;
66
67 loc_ptr = baton->data;
68 buf_end = baton->data + baton->size;
69
70 while (1)
71 {
72 low = dwarf2_read_address (gdbarch, loc_ptr, buf_end, addr_size);
73 loc_ptr += addr_size;
74 high = dwarf2_read_address (gdbarch, loc_ptr, buf_end, addr_size);
75 loc_ptr += addr_size;
76
77 /* An end-of-list entry. */
78 if (low == 0 && high == 0)
79 return NULL;
80
81 /* A base-address-selection entry. */
82 if ((low & base_mask) == base_mask)
83 {
84 base_address = high;
85 continue;
86 }
87
88 /* Otherwise, a location expression entry. */
89 low += base_address;
90 high += base_address;
91
92 length = extract_unsigned_integer (loc_ptr, 2);
93 loc_ptr += 2;
94
95 if (pc >= low && pc < high)
96 {
97 *locexpr_length = length;
98 return loc_ptr;
99 }
100
101 loc_ptr += length;
102 }
103 }
104
105 /* This is the baton used when performing dwarf2 expression
106 evaluation. */
107 struct dwarf_expr_baton
108 {
109 struct frame_info *frame;
110 struct objfile *objfile;
111 };
112
113 /* Helper functions for dwarf2_evaluate_loc_desc. */
114
115 /* Using the frame specified in BATON, return the value of register
116 REGNUM, treated as a pointer. */
117 static CORE_ADDR
118 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
119 {
120 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
121 struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
122 CORE_ADDR result;
123 int regnum;
124
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);
128 return result;
129 }
130
131 /* Read memory at ADDR (length LEN) into BUF. */
132
133 static void
134 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
135 {
136 read_memory (addr, buf, len);
137 }
138
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. */
142 static void
143 dwarf_expr_frame_base (void *baton, gdb_byte **start, size_t * length)
144 {
145 /* FIXME: cagney/2003-03-26: This code should be using
146 get_frame_base_address(), and then implement a dwarf2 specific
147 this_base method. */
148 struct symbol *framefunc;
149 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
150
151 /* Use block_linkage_function, which returns a real (not inlined)
152 function, instead of get_frame_function, which may return an
153 inlined function. */
154 framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
155
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);
160
161 if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
162 *start = NULL;
163 else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
164 {
165 struct dwarf2_loclist_baton *symbaton;
166 struct frame_info *frame = debaton->frame;
167
168 symbaton = SYMBOL_LOCATION_BATON (framefunc);
169 *start = find_location_expression (symbaton, length,
170 get_frame_address_in_block (frame));
171 }
172 else
173 {
174 struct dwarf2_locexpr_baton *symbaton;
175 symbaton = SYMBOL_LOCATION_BATON (framefunc);
176 if (symbaton != NULL)
177 {
178 *length = symbaton->size;
179 *start = symbaton->data;
180 }
181 else
182 *start = NULL;
183 }
184
185 if (*start == NULL)
186 error (_("Could not find the frame base for \"%s\"."),
187 SYMBOL_NATURAL_NAME (framefunc));
188 }
189
190 /* Using the objfile specified in BATON, find the address for the
191 current thread's thread-local storage with offset OFFSET. */
192 static CORE_ADDR
193 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
194 {
195 struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
196
197 return target_translate_tls_address (debaton->objfile, offset);
198 }
199
200 /* Evaluate a location description, starting at DATA and with length
201 SIZE, to find the current location of variable VAR in the context
202 of FRAME. */
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)
207 {
208 struct value *retval;
209 struct dwarf_expr_baton baton;
210 struct dwarf_expr_context *ctx;
211
212 if (size == 0)
213 {
214 retval = allocate_value (SYMBOL_TYPE (var));
215 VALUE_LVAL (retval) = not_lval;
216 set_value_optimized_out (retval, 1);
217 return retval;
218 }
219
220 baton.frame = frame;
221 baton.objfile = dwarf2_per_cu_objfile (per_cu);
222
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);
226 ctx->baton = &baton;
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;
231
232 dwarf_expr_eval (ctx, data, size);
233 if (ctx->num_pieces > 0)
234 {
235 int i;
236 long offset = 0;
237 bfd_byte *contents;
238
239 retval = allocate_value (SYMBOL_TYPE (var));
240 contents = value_contents_raw (retval);
241 for (i = 0; i < ctx->num_pieces; i++)
242 {
243 struct dwarf_expr_piece *p = &ctx->pieces[i];
244 if (p->in_reg)
245 {
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);
251 }
252 else /* In memory? */
253 {
254 read_memory (p->value, contents + offset, p->size);
255 }
256 offset += p->size;
257 }
258 }
259 else if (ctx->in_reg)
260 {
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);
265 }
266 else
267 {
268 CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
269
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);
274 }
275
276 set_value_initialized (retval, ctx->initialized);
277
278 free_dwarf_expr_context (ctx);
279
280 return retval;
281 }
282
283
284
285
286 \f
287 /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */
288
289 struct needs_frame_baton
290 {
291 int needs_frame;
292 };
293
294 /* Reads from registers do require a frame. */
295 static CORE_ADDR
296 needs_frame_read_reg (void *baton, int regnum)
297 {
298 struct needs_frame_baton *nf_baton = baton;
299 nf_baton->needs_frame = 1;
300 return 1;
301 }
302
303 /* Reads from memory do not require a frame. */
304 static void
305 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
306 {
307 memset (buf, 0, len);
308 }
309
310 /* Frame-relative accesses do require a frame. */
311 static void
312 needs_frame_frame_base (void *baton, gdb_byte **start, size_t * length)
313 {
314 static gdb_byte lit0 = DW_OP_lit0;
315 struct needs_frame_baton *nf_baton = baton;
316
317 *start = &lit0;
318 *length = 1;
319
320 nf_baton->needs_frame = 1;
321 }
322
323 /* Thread-local accesses do require a frame. */
324 static CORE_ADDR
325 needs_frame_tls_address (void *baton, CORE_ADDR offset)
326 {
327 struct needs_frame_baton *nf_baton = baton;
328 nf_baton->needs_frame = 1;
329 return 1;
330 }
331
332 /* Return non-zero iff the location expression at DATA (length SIZE)
333 requires a frame to evaluate. */
334
335 static int
336 dwarf2_loc_desc_needs_frame (gdb_byte *data, unsigned short size,
337 struct dwarf2_per_cu_data *per_cu)
338 {
339 struct needs_frame_baton baton;
340 struct dwarf_expr_context *ctx;
341 int in_reg;
342
343 baton.needs_frame = 0;
344
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);
348 ctx->baton = &baton;
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;
353
354 dwarf_expr_eval (ctx, data, size);
355
356 in_reg = ctx->in_reg;
357
358 if (ctx->num_pieces > 0)
359 {
360 int i;
361
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)
366 in_reg = 1;
367 }
368
369 free_dwarf_expr_context (ctx);
370
371 return baton.needs_frame || in_reg;
372 }
373
374 static void
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)
378 {
379 if (size == 0)
380 error (_("Symbol \"%s\" has been optimized out."),
381 SYMBOL_PRINT_NAME (symbol));
382
383 if (size == 1
384 && data[0] >= DW_OP_reg0
385 && data[0] <= DW_OP_reg31)
386 {
387 value->kind = axs_lvalue_register;
388 value->u.reg = data[0] - DW_OP_reg0;
389 }
390 else if (data[0] == DW_OP_regx)
391 {
392 ULONGEST reg;
393 read_uleb128 (data + 1, data + size, &reg);
394 value->kind = axs_lvalue_register;
395 value->u.reg = reg;
396 }
397 else if (data[0] == DW_OP_fbreg)
398 {
399 /* And this is worse than just minimal; we should honor the frame base
400 as above. */
401 int frame_reg;
402 LONGEST frame_offset;
403 gdb_byte *buf_end;
404
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));
409
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);
415
416 value->kind = axs_lvalue_memory;
417 }
418 else if (data[0] >= DW_OP_breg0
419 && data[0] <= DW_OP_breg31)
420 {
421 unsigned int reg;
422 LONGEST offset;
423 gdb_byte *buf_end;
424
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));
430
431 ax_reg (ax, reg);
432 ax_const_l (ax, offset);
433 ax_simple (ax, aop_add);
434
435 value->kind = axs_lvalue_memory;
436 }
437 else
438 error (_("Unsupported DWARF opcode 0x%x in the location of \"%s\"."),
439 data[0], SYMBOL_PRINT_NAME (symbol));
440 }
441 \f
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)
446 {
447 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
448 struct value *val;
449 val = dwarf2_evaluate_loc_desc (symbol, frame, dlbaton->data, dlbaton->size,
450 dlbaton->per_cu);
451
452 return val;
453 }
454
455 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
456 static int
457 locexpr_read_needs_frame (struct symbol *symbol)
458 {
459 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
460 return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
461 dlbaton->per_cu);
462 }
463
464 /* Print a natural-language description of SYMBOL to STREAM. */
465 static int
466 locexpr_describe_location (struct symbol *symbol, struct ui_file *stream)
467 {
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);
471
472 if (dlbaton->size == 1
473 && dlbaton->data[0] >= DW_OP_reg0
474 && dlbaton->data[0] <= DW_OP_reg31)
475 {
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));
483 return 1;
484 }
485
486 /* The location expression for a TLS variable looks like this (on a
487 64-bit LE machine):
488
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)
491
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. */
497
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)
501 {
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,
505 &dlbaton->data[1],
506 &dlbaton->data[dlbaton->size - 1],
507 addr_size);
508 fprintf_filtered (stream,
509 "a thread-local variable at offset %s in the "
510 "thread-local storage for `%s'",
511 paddr_nz (offset), objfile->name);
512 return 1;
513 }
514
515
516 fprintf_filtered (stream,
517 "a variable with complex or multiple locations (DWARF2)");
518 return 1;
519 }
520
521
522 /* Describe the location of SYMBOL as an agent value in VALUE, generating
523 any necessary bytecode in AX.
524
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. */
529
530 static void
531 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
532 struct agent_expr *ax, struct axs_value *value)
533 {
534 struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
535
536 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value,
537 dlbaton->data, dlbaton->size);
538 }
539
540 /* The set of location functions used with the DWARF-2 expression
541 evaluator. */
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
547 };
548
549
550 /* Wrapper functions for location lists. These generally find
551 the appropriate location expression and call something above. */
552
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)
557 {
558 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
559 struct value *val;
560 gdb_byte *data;
561 size_t size;
562
563 data = find_location_expression (dlbaton, &size,
564 frame ? get_frame_address_in_block (frame)
565 : 0);
566 if (data == NULL)
567 {
568 val = allocate_value (SYMBOL_TYPE (symbol));
569 VALUE_LVAL (val) = not_lval;
570 set_value_optimized_out (val, 1);
571 }
572 else
573 val = dwarf2_evaluate_loc_desc (symbol, frame, data, size,
574 dlbaton->per_cu);
575
576 return val;
577 }
578
579 /* Return non-zero iff we need a frame to evaluate SYMBOL. */
580 static int
581 loclist_read_needs_frame (struct symbol *symbol)
582 {
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
587 represent it. */
588
589 return 1;
590 }
591
592 /* Print a natural-language description of SYMBOL to STREAM. */
593 static int
594 loclist_describe_location (struct symbol *symbol, struct ui_file *stream)
595 {
596 /* FIXME: Could print the entire list of locations. */
597 fprintf_filtered (stream, "a variable with multiple locations");
598 return 1;
599 }
600
601 /* Describe the location of SYMBOL as an agent value in VALUE, generating
602 any necessary bytecode in AX. */
603 static void
604 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
605 struct agent_expr *ax, struct axs_value *value)
606 {
607 struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
608 gdb_byte *data;
609 size_t size;
610
611 data = find_location_expression (dlbaton, &size, ax->scope);
612 if (data == NULL)
613 error (_("Variable \"%s\" is not available."), SYMBOL_NATURAL_NAME (symbol));
614
615 dwarf2_tracepoint_var_ref (symbol, gdbarch, ax, value, data, size);
616 }
617
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
625 };
This page took 0.062855 seconds and 4 git commands to generate.