1/* DWARF 2 location expression support for GDB.
2   Copyright 2003 Free Software Foundation, Inc.
3   Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
4
5   This file is part of GDB.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or (at
10   your option) any later version.
11
12   This program is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place - Suite 330,
20   Boston, MA 02111-1307, USA.  */
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
34#include "elf/dwarf2.h"
35#include "dwarf2expr.h"
36#include "dwarf2loc.h"
37
38#include "gdb_string.h"
39
40#ifndef DWARF2_REG_TO_REGNUM
41#define DWARF2_REG_TO_REGNUM(REG) (REG)
42#endif
43
44/* A helper function for dealing with location lists.  Given a
45   symbol baton (BATON) and a pc value (PC), find the appropriate
46   location expression, set *LOCEXPR_LENGTH, and return a pointer
47   to the beginning of the expression.  Returns NULL on failure.
48
49   For now, only return the first matching location expression; there
50   can be more than one in the list.  */
51
52static char *
53find_location_expression (struct dwarf2_loclist_baton *baton,
54			  size_t *locexpr_length, CORE_ADDR pc)
55{
56  CORE_ADDR low, high;
57  char *loc_ptr, *buf_end;
58  unsigned int addr_size = TARGET_ADDR_BIT / TARGET_CHAR_BIT, length;
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 (baton->objfile->section_offsets,
62				    SECT_OFF_TEXT (baton->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, &length);
71      loc_ptr += length;
72      high = dwarf2_read_address (loc_ptr, buf_end, &length);
73      loc_ptr += length;
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.  */
105struct 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, read register REGNUM.  The lval
114   type will be returned in LVALP, and for lval_memory the register
115   save address will be returned in ADDRP.  */
116static CORE_ADDR
117dwarf_expr_read_reg (void *baton, int dwarf_regnum)
118{
119  struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
120  CORE_ADDR result, save_addr;
121  enum lval_type lval_type;
122  char *buf;
123  int optimized, regnum, realnum, regsize;
124
125  regnum = DWARF2_REG_TO_REGNUM (dwarf_regnum);
126  regsize = register_size (current_gdbarch, regnum);
127  buf = (char *) alloca (regsize);
128
129  frame_register (debaton->frame, regnum, &optimized, &lval_type, &save_addr,
130		  &realnum, buf);
131  /* NOTE: cagney/2003-05-22: This extract is assuming that a DWARF 2
132     address is always unsigned.  That may or may not be true.  */
133  result = extract_unsigned_integer (buf, regsize);
134
135  return result;
136}
137
138/* Read memory at ADDR (length LEN) into BUF.  */
139
140static void
141dwarf_expr_read_mem (void *baton, char *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.  */
149static void
150dwarf_expr_frame_base (void *baton, unsigned char **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  framefunc = get_frame_function (debaton->frame);
159
160  if (SYMBOL_OPS (framefunc) == &dwarf2_loclist_funcs)
161    {
162      struct dwarf2_loclist_baton *symbaton;
163      symbaton = SYMBOL_LOCATION_BATON (framefunc);
164      *start = find_location_expression (symbaton, length,
165					 get_frame_pc (debaton->frame));
166    }
167  else
168    {
169      struct dwarf2_locexpr_baton *symbaton;
170      symbaton = SYMBOL_LOCATION_BATON (framefunc);
171      *length = symbaton->size;
172      *start = symbaton->data;
173    }
174
175  if (*start == NULL)
176    error ("Could not find the frame base for \"%s\".",
177	   SYMBOL_NATURAL_NAME (framefunc));
178}
179
180/* Using the objfile specified in BATON, find the address for the
181   current thread's thread-local storage with offset OFFSET.  */
182static CORE_ADDR
183dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
184{
185  struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
186  CORE_ADDR addr;
187
188  if (target_get_thread_local_address_p ())
189    addr = target_get_thread_local_address (inferior_ptid,
190					    debaton->objfile,
191					    offset);
192  /* It wouldn't be wrong here to try a gdbarch method, too; finding
193     TLS is an ABI-specific thing.  But we don't do that yet.  */
194  else
195    error ("Cannot find thread-local variables on this target");
196
197  return addr;
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.  */
203static struct value *
204dwarf2_evaluate_loc_desc (struct symbol *var, struct frame_info *frame,
205			  unsigned char *data, unsigned short size,
206			  struct objfile *objfile)
207{
208  CORE_ADDR result;
209  struct gdbarch *arch = get_frame_arch (frame);
210  struct value *retval;
211  struct dwarf_expr_baton baton;
212  struct dwarf_expr_context *ctx;
213
214  if (size == 0)
215    {
216      retval = allocate_value (SYMBOL_TYPE (var));
217      VALUE_LVAL (retval) = not_lval;
218      VALUE_OPTIMIZED_OUT (retval) = 1;
219    }
220
221  baton.frame = frame;
222  baton.objfile = objfile;
223
224  ctx = new_dwarf_expr_context ();
225  ctx->baton = &baton;
226  ctx->read_reg = dwarf_expr_read_reg;
227  ctx->read_mem = dwarf_expr_read_mem;
228  ctx->get_frame_base = dwarf_expr_frame_base;
229  ctx->get_tls_address = dwarf_expr_tls_address;
230
231  dwarf_expr_eval (ctx, data, size);
232  result = dwarf_expr_fetch (ctx, 0);
233
234  if (ctx->num_pieces > 0)
235    {
236      int i;
237      long offset = 0;
238      bfd_byte *contents;
239
240      retval = allocate_value (SYMBOL_TYPE (var));
241      contents = VALUE_CONTENTS_RAW (retval);
242      for (i = 0; i < ctx->num_pieces; i++)
243       {
244         struct dwarf_expr_piece *p = &ctx->pieces[i];
245         if (p->in_reg)
246           {
247             bfd_byte regval[MAX_REGISTER_SIZE];
248             int gdb_regnum = DWARF2_REG_TO_REGNUM (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      int regnum = DWARF2_REG_TO_REGNUM (result);
262      retval = value_from_register (SYMBOL_TYPE (var), regnum, frame);
263    }
264  else
265    {
266      retval = allocate_value (SYMBOL_TYPE (var));
267      VALUE_BFD_SECTION (retval) = SYMBOL_BFD_SECTION (var);
268
269      VALUE_LVAL (retval) = lval_memory;
270      VALUE_LAZY (retval) = 1;
271      VALUE_ADDRESS (retval) = result;
272    }
273
274  set_value_initialized (retval, ctx->initialized);
275
276  free_dwarf_expr_context (ctx);
277
278  return retval;
279}
280
281
282
283
284
285/* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
286
287struct needs_frame_baton
288{
289  int needs_frame;
290};
291
292/* Reads from registers do require a frame.  */
293static CORE_ADDR
294needs_frame_read_reg (void *baton, int regnum)
295{
296  struct needs_frame_baton *nf_baton = baton;
297  nf_baton->needs_frame = 1;
298  return 1;
299}
300
301/* Reads from memory do not require a frame.  */
302static void
303needs_frame_read_mem (void *baton, char *buf, CORE_ADDR addr, size_t len)
304{
305  memset (buf, 0, len);
306}
307
308/* Frame-relative accesses do require a frame.  */
309static void
310needs_frame_frame_base (void *baton, unsigned char **start, size_t * length)
311{
312  static char lit0 = DW_OP_lit0;
313  struct needs_frame_baton *nf_baton = baton;
314
315  *start = &lit0;
316  *length = 1;
317
318  nf_baton->needs_frame = 1;
319}
320
321/* Thread-local accesses do require a frame.  */
322static CORE_ADDR
323needs_frame_tls_address (void *baton, CORE_ADDR offset)
324{
325  struct needs_frame_baton *nf_baton = baton;
326  nf_baton->needs_frame = 1;
327  return 1;
328}
329
330/* Return non-zero iff the location expression at DATA (length SIZE)
331   requires a frame to evaluate.  */
332
333static int
334dwarf2_loc_desc_needs_frame (unsigned char *data, unsigned short size)
335{
336  struct needs_frame_baton baton;
337  struct dwarf_expr_context *ctx;
338  int in_reg;
339
340  baton.needs_frame = 0;
341
342  ctx = new_dwarf_expr_context ();
343  ctx->baton = &baton;
344  ctx->read_reg = needs_frame_read_reg;
345  ctx->read_mem = needs_frame_read_mem;
346  ctx->get_frame_base = needs_frame_frame_base;
347  ctx->get_tls_address = needs_frame_tls_address;
348
349  dwarf_expr_eval (ctx, data, size);
350
351  in_reg = ctx->in_reg;
352
353  if (ctx->num_pieces > 0)
354    {
355      int i;
356
357      /* If the location has several pieces, and any of them are in
358         registers, then we will need a frame to fetch them from.  */
359      for (i = 0; i < ctx->num_pieces; i++)
360        if (ctx->pieces[i].in_reg)
361          in_reg = 1;
362    }
363
364  free_dwarf_expr_context (ctx);
365
366  return baton.needs_frame || in_reg;
367}
368
369static void
370dwarf2_tracepoint_var_ref (struct symbol * symbol, struct agent_expr * ax,
371			   struct axs_value * value, unsigned char *data,
372			   int size)
373{
374  if (size == 0)
375    error ("Symbol \"%s\" has been optimized out.",
376	   SYMBOL_PRINT_NAME (symbol));
377
378  if (size == 1
379      && data[0] >= DW_OP_reg0
380      && data[0] <= DW_OP_reg31)
381    {
382      value->kind = axs_lvalue_register;
383      value->u.reg = data[0] - DW_OP_reg0;
384    }
385  else if (data[0] == DW_OP_regx)
386    {
387      ULONGEST reg;
388      read_uleb128 (data + 1, data + size, &reg);
389      value->kind = axs_lvalue_register;
390      value->u.reg = reg;
391    }
392  else if (data[0] == DW_OP_fbreg)
393    {
394      /* And this is worse than just minimal; we should honor the frame base
395	 as above.  */
396      int frame_reg;
397      LONGEST frame_offset;
398      unsigned char *buf_end;
399
400      buf_end = read_sleb128 (data + 1, data + size, &frame_offset);
401      if (buf_end != data + size)
402	error ("Unexpected opcode after DW_OP_fbreg for symbol \"%s\".",
403	       SYMBOL_PRINT_NAME (symbol));
404
405      TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
406      ax_reg (ax, frame_reg);
407      ax_const_l (ax, frame_offset);
408      ax_simple (ax, aop_add);
409
410      ax_const_l (ax, frame_offset);
411      ax_simple (ax, aop_add);
412      value->kind = axs_lvalue_memory;
413    }
414  else
415    error ("Unsupported DWARF opcode in the location of \"%s\".",
416	   SYMBOL_PRINT_NAME (symbol));
417}
418
419/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
420   evaluator to calculate the location.  */
421static struct value *
422locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
423{
424  struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
425  struct value *val;
426  val = dwarf2_evaluate_loc_desc (symbol, frame, dlbaton->data, dlbaton->size,
427				  dlbaton->objfile);
428
429  return val;
430}
431
432/* Return non-zero iff we need a frame to evaluate SYMBOL.  */
433static int
434locexpr_read_needs_frame (struct symbol *symbol)
435{
436  struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
437  return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size);
438}
439
440/* Print a natural-language description of SYMBOL to STREAM.  */
441static int
442locexpr_describe_location (struct symbol *symbol, struct ui_file *stream)
443{
444  /* FIXME: be more extensive.  */
445  struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
446
447  if (dlbaton->size == 1
448      && dlbaton->data[0] >= DW_OP_reg0
449      && dlbaton->data[0] <= DW_OP_reg31)
450    {
451      int regno = DWARF2_REG_TO_REGNUM (dlbaton->data[0] - DW_OP_reg0);
452      fprintf_filtered (stream,
453			"a variable in register %s", REGISTER_NAME (regno));
454      return 1;
455    }
456
457  /* The location expression for a TLS variable looks like this (on a
458     64-bit LE machine):
459
460     DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
461                        (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
462
463     0x3 is the encoding for DW_OP_addr, which has an operand as long
464     as the size of an address on the target machine (here is 8
465     bytes).  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
466     The operand represents the offset at which the variable is within
467     the thread local storage.  */
468
469  if (dlbaton->size > 1
470      && dlbaton->data[dlbaton->size - 1] == DW_OP_GNU_push_tls_address)
471    if (dlbaton->data[0] == DW_OP_addr)
472      {
473	int bytes_read;
474	CORE_ADDR offset = dwarf2_read_address (&dlbaton->data[1],
475						&dlbaton->data[dlbaton->size - 1],
476						&bytes_read);
477	fprintf_filtered (stream,
478			  "a thread-local variable at offset %s in the "
479			  "thread-local storage for `%s'",
480			  paddr_nz (offset), dlbaton->objfile->name);
481	return 1;
482      }
483
484
485  fprintf_filtered (stream,
486		    "a variable with complex or multiple locations (DWARF2)");
487  return 1;
488}
489
490
491/* Describe the location of SYMBOL as an agent value in VALUE, generating
492   any necessary bytecode in AX.
493
494   NOTE drow/2003-02-26: This function is extremely minimal, because
495   doing it correctly is extremely complicated and there is no
496   publicly available stub with tracepoint support for me to test
497   against.  When there is one this function should be revisited.  */
498
499static void
500locexpr_tracepoint_var_ref (struct symbol * symbol, struct agent_expr * ax,
501			    struct axs_value * value)
502{
503  struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
504
505  dwarf2_tracepoint_var_ref (symbol, ax, value, dlbaton->data, dlbaton->size);
506}
507
508/* The set of location functions used with the DWARF-2 expression
509   evaluator.  */
510const struct symbol_ops dwarf2_locexpr_funcs = {
511  locexpr_read_variable,
512  locexpr_read_needs_frame,
513  locexpr_describe_location,
514  locexpr_tracepoint_var_ref
515};
516
517
518/* Wrapper functions for location lists.  These generally find
519   the appropriate location expression and call something above.  */
520
521/* Return the value of SYMBOL in FRAME using the DWARF-2 expression
522   evaluator to calculate the location.  */
523static struct value *
524loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
525{
526  struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
527  struct value *val;
528  unsigned char *data;
529  size_t size;
530
531  data = find_location_expression (dlbaton, &size,
532				   frame ? get_frame_pc (frame) : 0);
533  if (data == NULL)
534    {
535      val = allocate_value (SYMBOL_TYPE (symbol));
536      VALUE_LVAL (val) = not_lval;
537      VALUE_OPTIMIZED_OUT (val) = 1;
538    }
539  else
540    val = dwarf2_evaluate_loc_desc (symbol, frame, data, size,
541				    dlbaton->objfile);
542
543  return val;
544}
545
546/* Return non-zero iff we need a frame to evaluate SYMBOL.  */
547static int
548loclist_read_needs_frame (struct symbol *symbol)
549{
550  /* If there's a location list, then assume we need to have a frame
551     to choose the appropriate location expression.  With tracking of
552     global variables this is not necessarily true, but such tracking
553     is disabled in GCC at the moment until we figure out how to
554     represent it.  */
555
556  return 1;
557}
558
559/* Print a natural-language description of SYMBOL to STREAM.  */
560static int
561loclist_describe_location (struct symbol *symbol, struct ui_file *stream)
562{
563  /* FIXME: Could print the entire list of locations.  */
564  fprintf_filtered (stream, "a variable with multiple locations");
565  return 1;
566}
567
568/* Describe the location of SYMBOL as an agent value in VALUE, generating
569   any necessary bytecode in AX.  */
570static void
571loclist_tracepoint_var_ref (struct symbol * symbol, struct agent_expr * ax,
572			    struct axs_value * value)
573{
574  struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
575  unsigned char *data;
576  size_t size;
577
578  data = find_location_expression (dlbaton, &size, ax->scope);
579  if (data == NULL)
580    error ("Variable \"%s\" is not available.", SYMBOL_NATURAL_NAME (symbol));
581
582  dwarf2_tracepoint_var_ref (symbol, ax, value, data, size);
583}
584
585/* The set of location functions used with the DWARF-2 expression
586   evaluator and location lists.  */
587const struct symbol_ops dwarf2_loclist_funcs = {
588  loclist_read_variable,
589  loclist_read_needs_frame,
590  loclist_describe_location,
591  loclist_tracepoint_var_ref
592};
593