xref: /openbsd/gnu/usr.bin/binutils/gdb/dwarf2expr.h (revision 11efff7f)
1b725ae77Skettenis /* Dwarf2 Expression Evaluator
2b725ae77Skettenis    Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
3b725ae77Skettenis    Contributed by Daniel Berlin (dan@dberlin.org)
4b725ae77Skettenis    This file is part of GDB.
5b725ae77Skettenis 
6b725ae77Skettenis    This program is free software; you can redistribute it and/or modify
7b725ae77Skettenis    it under the terms of the GNU General Public License as published by
8b725ae77Skettenis    the Free Software Foundation; either version 2 of the License, or
9b725ae77Skettenis    (at your option) any later version.
10b725ae77Skettenis 
11b725ae77Skettenis    This program is distributed in the hope that it will be useful,
12b725ae77Skettenis    but WITHOUT ANY WARRANTY; without even the implied warranty of
13b725ae77Skettenis    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14b725ae77Skettenis    GNU General Public License for more details.
15b725ae77Skettenis 
16b725ae77Skettenis    You should have received a copy of the GNU General Public License
17b725ae77Skettenis    along with this program; if not, write to the Free Software
18b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
19b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
20b725ae77Skettenis 
21b725ae77Skettenis #if !defined (DWARF2EXPR_H)
22b725ae77Skettenis #define DWARF2EXPR_H
23b725ae77Skettenis 
24b725ae77Skettenis /* The expression evaluator works with a dwarf_expr_context, describing
25b725ae77Skettenis    its current state and its callbacks.  */
26b725ae77Skettenis struct dwarf_expr_context
27b725ae77Skettenis {
28b725ae77Skettenis   /* The stack of values, allocated with xmalloc.  */
29b725ae77Skettenis   CORE_ADDR *stack;
30b725ae77Skettenis 
31b725ae77Skettenis   /* The number of values currently pushed on the stack, and the
32b725ae77Skettenis      number of elements allocated to the stack.  */
33b725ae77Skettenis   int stack_len, stack_allocated;
34b725ae77Skettenis 
35b725ae77Skettenis   /* An opaque argument provided by the caller, which will be passed
36b725ae77Skettenis      to all of the callback functions.  */
37b725ae77Skettenis   void *baton;
38b725ae77Skettenis 
39b725ae77Skettenis   /* Return the value of register number REGNUM.  */
40b725ae77Skettenis   CORE_ADDR (*read_reg) (void *baton, int regnum);
41b725ae77Skettenis 
42b725ae77Skettenis   /* Read LENGTH bytes at ADDR into BUF.  */
43b725ae77Skettenis   void (*read_mem) (void *baton, char *buf, CORE_ADDR addr,
44b725ae77Skettenis 		    size_t length);
45b725ae77Skettenis 
46b725ae77Skettenis   /* Return the location expression for the frame base attribute, in
47b725ae77Skettenis      START and LENGTH.  The result must be live until the current
48b725ae77Skettenis      expression evaluation is complete.  */
49b725ae77Skettenis   void (*get_frame_base) (void *baton, unsigned char **start,
50b725ae77Skettenis 			 size_t *length);
51b725ae77Skettenis 
52b725ae77Skettenis   /* Return the thread-local storage address for
53b725ae77Skettenis      DW_OP_GNU_push_tls_address.  */
54b725ae77Skettenis   CORE_ADDR (*get_tls_address) (void *baton, CORE_ADDR offset);
55b725ae77Skettenis 
56b725ae77Skettenis #if 0
57b725ae77Skettenis   /* Not yet implemented.  */
58b725ae77Skettenis 
59b725ae77Skettenis   /* Return the location expression for the dwarf expression
60b725ae77Skettenis      subroutine in the die at OFFSET in the current compilation unit.
61b725ae77Skettenis      The result must be live until the current expression evaluation
62b725ae77Skettenis      is complete.  */
63b725ae77Skettenis   unsigned char *(*get_subr) (void *baton, off_t offset, size_t *length);
64b725ae77Skettenis 
65b725ae77Skettenis   /* Return the `object address' for DW_OP_push_object_address.  */
66b725ae77Skettenis   CORE_ADDR (*get_object_address) (void *baton);
67b725ae77Skettenis #endif
68b725ae77Skettenis 
69b725ae77Skettenis   /* The current depth of dwarf expression recursion, via DW_OP_call*,
70b725ae77Skettenis      DW_OP_fbreg, DW_OP_push_object_address, etc., and the maximum
71b725ae77Skettenis      depth we'll tolerate before raising an error.  */
72b725ae77Skettenis   int recursion_depth, max_recursion_depth;
73b725ae77Skettenis 
74b725ae77Skettenis   /* Non-zero if the result is in a register.  The register number
75b725ae77Skettenis      will be on the expression stack.  */
76b725ae77Skettenis   int in_reg;
77*11efff7fSkettenis 
78*11efff7fSkettenis   /* An array of pieces.  PIECES points to its first element;
79*11efff7fSkettenis      NUM_PIECES is its length.
80*11efff7fSkettenis 
81*11efff7fSkettenis      Each time DW_OP_piece is executed, we add a new element to the
82*11efff7fSkettenis      end of this array, recording the current top of the stack, the
83*11efff7fSkettenis      current in_reg flag, and the size given as the operand to
84*11efff7fSkettenis      DW_OP_piece.  We then pop the top value from the stack, clear the
85*11efff7fSkettenis      in_reg flag, and resume evaluation.
86*11efff7fSkettenis 
87*11efff7fSkettenis      The Dwarf spec doesn't say whether DW_OP_piece pops the top value
88*11efff7fSkettenis      from the stack.  We do, ensuring that clients of this interface
89*11efff7fSkettenis      expecting to see a value left on the top of the stack (say, code
90*11efff7fSkettenis      evaluating frame base expressions or CFA's specified with
91*11efff7fSkettenis      DW_CFA_def_cfa_expression) will get an error if the expression
92*11efff7fSkettenis      actually marks all the values it computes as pieces.
93*11efff7fSkettenis 
94*11efff7fSkettenis      If an expression never uses DW_OP_piece, num_pieces will be zero.
95*11efff7fSkettenis      (It would be nice to present these cases as expressions yielding
96*11efff7fSkettenis      a single piece, with in_reg clear, so that callers need not
97*11efff7fSkettenis      distinguish between the no-DW_OP_piece and one-DW_OP_piece cases.
98*11efff7fSkettenis      But expressions with no DW_OP_piece operations have no value to
99*11efff7fSkettenis      place in a piece's 'size' field; the size comes from the
100*11efff7fSkettenis      surrounding data.  So the two cases need to be handled
101*11efff7fSkettenis      separately.)  */
102*11efff7fSkettenis   int num_pieces;
103*11efff7fSkettenis   struct dwarf_expr_piece *pieces;
104*11efff7fSkettenis };
105*11efff7fSkettenis 
106*11efff7fSkettenis 
107*11efff7fSkettenis /* A piece of an object, as recorded by DW_OP_piece.  */
108*11efff7fSkettenis struct dwarf_expr_piece
109*11efff7fSkettenis {
110*11efff7fSkettenis   /* If IN_REG is zero, then the piece is in memory, and VALUE is its address.
111*11efff7fSkettenis      If IN_REG is non-zero, then the piece is in a register, and VALUE
112*11efff7fSkettenis      is the register number.  */
113*11efff7fSkettenis   int in_reg;
114*11efff7fSkettenis 
115*11efff7fSkettenis   /* This piece's address or register number.  */
116*11efff7fSkettenis   CORE_ADDR value;
117*11efff7fSkettenis 
118*11efff7fSkettenis   /* The length of the piece, in bytes.  */
119*11efff7fSkettenis   ULONGEST size;
120b725ae77Skettenis };
121b725ae77Skettenis 
122b725ae77Skettenis struct dwarf_expr_context *new_dwarf_expr_context (void);
123b725ae77Skettenis void free_dwarf_expr_context (struct dwarf_expr_context *ctx);
124b725ae77Skettenis 
125b725ae77Skettenis void dwarf_expr_push (struct dwarf_expr_context *ctx, CORE_ADDR value);
126b725ae77Skettenis void dwarf_expr_pop (struct dwarf_expr_context *ctx);
127b725ae77Skettenis void dwarf_expr_eval (struct dwarf_expr_context *ctx, unsigned char *addr,
128b725ae77Skettenis 		      size_t len);
129b725ae77Skettenis CORE_ADDR dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n);
130b725ae77Skettenis 
131b725ae77Skettenis 
132b725ae77Skettenis unsigned char *read_uleb128 (unsigned char *buf, unsigned char *buf_end,
133b725ae77Skettenis 			     ULONGEST * r);
134b725ae77Skettenis unsigned char *read_sleb128 (unsigned char *buf, unsigned char *buf_end,
135b725ae77Skettenis 			     LONGEST * r);
136b725ae77Skettenis CORE_ADDR dwarf2_read_address (unsigned char *buf, unsigned char *buf_end,
137b725ae77Skettenis 			       int *bytes_read);
138b725ae77Skettenis 
139b725ae77Skettenis #endif
140