15796c8dcSSimon Schubert /* DWARF 2 Expression Evaluator.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2001-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert Contributed by Daniel Berlin <dan@dberlin.org>.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This file is part of GDB.
85796c8dcSSimon Schubert
95796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert (at your option) any later version.
135796c8dcSSimon Schubert
145796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
175796c8dcSSimon Schubert GNU General Public License for more details.
185796c8dcSSimon Schubert
195796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
215796c8dcSSimon Schubert
225796c8dcSSimon Schubert #if !defined (DWARF2EXPR_H)
235796c8dcSSimon Schubert #define DWARF2EXPR_H
245796c8dcSSimon Schubert
25*ef5ccd6cSJohn Marino #include "leb128.h"
26*ef5ccd6cSJohn Marino #include "gdbtypes.h"
27*ef5ccd6cSJohn Marino
28a45ae5f8SJohn Marino struct dwarf_expr_context;
29a45ae5f8SJohn Marino
30a45ae5f8SJohn Marino /* Virtual method table for struct dwarf_expr_context below. */
31a45ae5f8SJohn Marino
32a45ae5f8SJohn Marino struct dwarf_expr_context_funcs
33a45ae5f8SJohn Marino {
34a45ae5f8SJohn Marino /* Return the value of register number REGNUM. */
35a45ae5f8SJohn Marino CORE_ADDR (*read_reg) (void *baton, int regnum);
36a45ae5f8SJohn Marino
37a45ae5f8SJohn Marino /* Read LENGTH bytes at ADDR into BUF. */
38a45ae5f8SJohn Marino void (*read_mem) (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t length);
39a45ae5f8SJohn Marino
40a45ae5f8SJohn Marino /* Return the location expression for the frame base attribute, in
41a45ae5f8SJohn Marino START and LENGTH. The result must be live until the current
42a45ae5f8SJohn Marino expression evaluation is complete. */
43a45ae5f8SJohn Marino void (*get_frame_base) (void *baton, const gdb_byte **start, size_t *length);
44a45ae5f8SJohn Marino
45a45ae5f8SJohn Marino /* Return the CFA for the frame. */
46a45ae5f8SJohn Marino CORE_ADDR (*get_frame_cfa) (void *baton);
47a45ae5f8SJohn Marino
48a45ae5f8SJohn Marino /* Return the PC for the frame. */
49a45ae5f8SJohn Marino CORE_ADDR (*get_frame_pc) (void *baton);
50a45ae5f8SJohn Marino
51a45ae5f8SJohn Marino /* Return the thread-local storage address for
52a45ae5f8SJohn Marino DW_OP_GNU_push_tls_address. */
53a45ae5f8SJohn Marino CORE_ADDR (*get_tls_address) (void *baton, CORE_ADDR offset);
54a45ae5f8SJohn Marino
55a45ae5f8SJohn Marino /* Execute DW_AT_location expression for the DWARF expression subroutine in
56a45ae5f8SJohn Marino the DIE at DIE_OFFSET in the CU from CTX. Do not touch STACK while it
57a45ae5f8SJohn Marino being passed to and returned from the called DWARF subroutine. */
58*ef5ccd6cSJohn Marino void (*dwarf_call) (struct dwarf_expr_context *ctx, cu_offset die_offset);
59a45ae5f8SJohn Marino
60a45ae5f8SJohn Marino /* Return the base type given by the indicated DIE. This can throw
61a45ae5f8SJohn Marino an exception if the DIE is invalid or does not represent a base
62a45ae5f8SJohn Marino type. If can also be NULL in the special case where the
63a45ae5f8SJohn Marino callbacks are not performing evaluation, and thus it is
64a45ae5f8SJohn Marino meaningful to substitute a stub type of the correct size. */
65*ef5ccd6cSJohn Marino struct type *(*get_base_type) (struct dwarf_expr_context *ctx, cu_offset die);
66a45ae5f8SJohn Marino
67a45ae5f8SJohn Marino /* Push on DWARF stack an entry evaluated for DW_TAG_GNU_call_site's
68*ef5ccd6cSJohn Marino parameter matching KIND and KIND_U at the caller of specified BATON.
69*ef5ccd6cSJohn Marino If DEREF_SIZE is not -1 then use DW_AT_GNU_call_site_data_value instead of
70*ef5ccd6cSJohn Marino DW_AT_GNU_call_site_value. */
71a45ae5f8SJohn Marino void (*push_dwarf_reg_entry_value) (struct dwarf_expr_context *ctx,
72*ef5ccd6cSJohn Marino enum call_site_parameter_kind kind,
73*ef5ccd6cSJohn Marino union call_site_parameter_u kind_u,
74a45ae5f8SJohn Marino int deref_size);
75a45ae5f8SJohn Marino
76*ef5ccd6cSJohn Marino /* Return the address indexed by DW_OP_GNU_addr_index.
77*ef5ccd6cSJohn Marino This can throw an exception if the index is out of range. */
78*ef5ccd6cSJohn Marino CORE_ADDR (*get_addr_index) (void *baton, unsigned int index);
79*ef5ccd6cSJohn Marino
80a45ae5f8SJohn Marino #if 0
81a45ae5f8SJohn Marino /* Not yet implemented. */
82a45ae5f8SJohn Marino
83a45ae5f8SJohn Marino /* Return the `object address' for DW_OP_push_object_address. */
84a45ae5f8SJohn Marino CORE_ADDR (*get_object_address) (void *baton);
85a45ae5f8SJohn Marino #endif
86a45ae5f8SJohn Marino };
87a45ae5f8SJohn Marino
885796c8dcSSimon Schubert /* The location of a value. */
895796c8dcSSimon Schubert enum dwarf_value_location
905796c8dcSSimon Schubert {
915796c8dcSSimon Schubert /* The piece is in memory.
925796c8dcSSimon Schubert The value on the dwarf stack is its address. */
935796c8dcSSimon Schubert DWARF_VALUE_MEMORY,
945796c8dcSSimon Schubert
955796c8dcSSimon Schubert /* The piece is in a register.
965796c8dcSSimon Schubert The value on the dwarf stack is the register number. */
975796c8dcSSimon Schubert DWARF_VALUE_REGISTER,
985796c8dcSSimon Schubert
995796c8dcSSimon Schubert /* The piece is on the dwarf stack. */
1005796c8dcSSimon Schubert DWARF_VALUE_STACK,
1015796c8dcSSimon Schubert
1025796c8dcSSimon Schubert /* The piece is a literal. */
103cf7f2e2dSJohn Marino DWARF_VALUE_LITERAL,
104cf7f2e2dSJohn Marino
105cf7f2e2dSJohn Marino /* The piece was optimized out. */
106c50c785cSJohn Marino DWARF_VALUE_OPTIMIZED_OUT,
107c50c785cSJohn Marino
108c50c785cSJohn Marino /* The piece is an implicit pointer. */
109c50c785cSJohn Marino DWARF_VALUE_IMPLICIT_POINTER
1105796c8dcSSimon Schubert };
1115796c8dcSSimon Schubert
1125796c8dcSSimon Schubert /* The dwarf expression stack. */
1135796c8dcSSimon Schubert
1145796c8dcSSimon Schubert struct dwarf_stack_value
1155796c8dcSSimon Schubert {
116a45ae5f8SJohn Marino struct value *value;
1175796c8dcSSimon Schubert
1185796c8dcSSimon Schubert /* Non-zero if the piece is in memory and is known to be
1195796c8dcSSimon Schubert on the program's stack. It is always ok to set this to zero.
1205796c8dcSSimon Schubert This is used, for example, to optimize memory access from the target.
1215796c8dcSSimon Schubert It can vastly speed up backtraces on long latency connections when
1225796c8dcSSimon Schubert "set stack-cache on". */
1235796c8dcSSimon Schubert int in_stack_memory;
1245796c8dcSSimon Schubert };
1255796c8dcSSimon Schubert
1265796c8dcSSimon Schubert /* The expression evaluator works with a dwarf_expr_context, describing
1275796c8dcSSimon Schubert its current state and its callbacks. */
1285796c8dcSSimon Schubert struct dwarf_expr_context
1295796c8dcSSimon Schubert {
1305796c8dcSSimon Schubert /* The stack of values, allocated with xmalloc. */
1315796c8dcSSimon Schubert struct dwarf_stack_value *stack;
1325796c8dcSSimon Schubert
1335796c8dcSSimon Schubert /* The number of values currently pushed on the stack, and the
1345796c8dcSSimon Schubert number of elements allocated to the stack. */
1355796c8dcSSimon Schubert int stack_len, stack_allocated;
1365796c8dcSSimon Schubert
1375796c8dcSSimon Schubert /* Target architecture to use for address operations. */
1385796c8dcSSimon Schubert struct gdbarch *gdbarch;
1395796c8dcSSimon Schubert
1405796c8dcSSimon Schubert /* Target address size in bytes. */
1415796c8dcSSimon Schubert int addr_size;
1425796c8dcSSimon Schubert
143a45ae5f8SJohn Marino /* DW_FORM_ref_addr size in bytes. If -1 DWARF is executed from a frame
144a45ae5f8SJohn Marino context and operations depending on DW_FORM_ref_addr are not allowed. */
145a45ae5f8SJohn Marino int ref_addr_size;
146a45ae5f8SJohn Marino
147*ef5ccd6cSJohn Marino /* Offset used to relocate DW_OP_addr and DW_OP_GNU_addr_index arguments. */
148cf7f2e2dSJohn Marino CORE_ADDR offset;
149cf7f2e2dSJohn Marino
1505796c8dcSSimon Schubert /* An opaque argument provided by the caller, which will be passed
1515796c8dcSSimon Schubert to all of the callback functions. */
1525796c8dcSSimon Schubert void *baton;
1535796c8dcSSimon Schubert
154a45ae5f8SJohn Marino /* Callback functions. */
155a45ae5f8SJohn Marino const struct dwarf_expr_context_funcs *funcs;
1565796c8dcSSimon Schubert
1575796c8dcSSimon Schubert /* The current depth of dwarf expression recursion, via DW_OP_call*,
1585796c8dcSSimon Schubert DW_OP_fbreg, DW_OP_push_object_address, etc., and the maximum
1595796c8dcSSimon Schubert depth we'll tolerate before raising an error. */
1605796c8dcSSimon Schubert int recursion_depth, max_recursion_depth;
1615796c8dcSSimon Schubert
1625796c8dcSSimon Schubert /* Location of the value. */
1635796c8dcSSimon Schubert enum dwarf_value_location location;
1645796c8dcSSimon Schubert
165c50c785cSJohn Marino /* For DWARF_VALUE_LITERAL, the current literal value's length and
166c50c785cSJohn Marino data. For DWARF_VALUE_IMPLICIT_POINTER, LEN is the offset of the
167*ef5ccd6cSJohn Marino target DIE of sect_offset kind. */
1685796c8dcSSimon Schubert ULONGEST len;
169cf7f2e2dSJohn Marino const gdb_byte *data;
1705796c8dcSSimon Schubert
1715796c8dcSSimon Schubert /* Initialization status of variable: Non-zero if variable has been
1725796c8dcSSimon Schubert initialized; zero otherwise. */
1735796c8dcSSimon Schubert int initialized;
1745796c8dcSSimon Schubert
1755796c8dcSSimon Schubert /* An array of pieces. PIECES points to its first element;
1765796c8dcSSimon Schubert NUM_PIECES is its length.
1775796c8dcSSimon Schubert
1785796c8dcSSimon Schubert Each time DW_OP_piece is executed, we add a new element to the
1795796c8dcSSimon Schubert end of this array, recording the current top of the stack, the
1805796c8dcSSimon Schubert current location, and the size given as the operand to
1815796c8dcSSimon Schubert DW_OP_piece. We then pop the top value from the stack, reset the
1825796c8dcSSimon Schubert location, and resume evaluation.
1835796c8dcSSimon Schubert
1845796c8dcSSimon Schubert The Dwarf spec doesn't say whether DW_OP_piece pops the top value
1855796c8dcSSimon Schubert from the stack. We do, ensuring that clients of this interface
1865796c8dcSSimon Schubert expecting to see a value left on the top of the stack (say, code
1875796c8dcSSimon Schubert evaluating frame base expressions or CFA's specified with
1885796c8dcSSimon Schubert DW_CFA_def_cfa_expression) will get an error if the expression
1895796c8dcSSimon Schubert actually marks all the values it computes as pieces.
1905796c8dcSSimon Schubert
1915796c8dcSSimon Schubert If an expression never uses DW_OP_piece, num_pieces will be zero.
1925796c8dcSSimon Schubert (It would be nice to present these cases as expressions yielding
1935796c8dcSSimon Schubert a single piece, so that callers need not distinguish between the
1945796c8dcSSimon Schubert no-DW_OP_piece and one-DW_OP_piece cases. But expressions with
1955796c8dcSSimon Schubert no DW_OP_piece operations have no value to place in a piece's
1965796c8dcSSimon Schubert 'size' field; the size comes from the surrounding data. So the
1975796c8dcSSimon Schubert two cases need to be handled separately.) */
1985796c8dcSSimon Schubert int num_pieces;
1995796c8dcSSimon Schubert struct dwarf_expr_piece *pieces;
2005796c8dcSSimon Schubert };
2015796c8dcSSimon Schubert
2025796c8dcSSimon Schubert
203cf7f2e2dSJohn Marino /* A piece of an object, as recorded by DW_OP_piece or DW_OP_bit_piece. */
2045796c8dcSSimon Schubert struct dwarf_expr_piece
2055796c8dcSSimon Schubert {
2065796c8dcSSimon Schubert enum dwarf_value_location location;
2075796c8dcSSimon Schubert
2085796c8dcSSimon Schubert union
2095796c8dcSSimon Schubert {
2105796c8dcSSimon Schubert struct
2115796c8dcSSimon Schubert {
212cf7f2e2dSJohn Marino /* This piece's address, for DWARF_VALUE_MEMORY pieces. */
213cf7f2e2dSJohn Marino CORE_ADDR addr;
2145796c8dcSSimon Schubert /* Non-zero if the piece is known to be in memory and on
2155796c8dcSSimon Schubert the program's stack. */
2165796c8dcSSimon Schubert int in_stack_memory;
217cf7f2e2dSJohn Marino } mem;
218cf7f2e2dSJohn Marino
219a45ae5f8SJohn Marino /* The piece's register number, for DWARF_VALUE_REGISTER pieces. */
220a45ae5f8SJohn Marino int regno;
221a45ae5f8SJohn Marino
222a45ae5f8SJohn Marino /* The piece's literal value, for DWARF_VALUE_STACK pieces. */
223a45ae5f8SJohn Marino struct value *value;
2245796c8dcSSimon Schubert
2255796c8dcSSimon Schubert struct
2265796c8dcSSimon Schubert {
227cf7f2e2dSJohn Marino /* A pointer to the data making up this piece,
228cf7f2e2dSJohn Marino for DWARF_VALUE_LITERAL pieces. */
229cf7f2e2dSJohn Marino const gdb_byte *data;
2305796c8dcSSimon Schubert /* The length of the available data. */
2315796c8dcSSimon Schubert ULONGEST length;
2325796c8dcSSimon Schubert } literal;
233c50c785cSJohn Marino
234c50c785cSJohn Marino /* Used for DWARF_VALUE_IMPLICIT_POINTER. */
235c50c785cSJohn Marino struct
236c50c785cSJohn Marino {
237c50c785cSJohn Marino /* The referent DIE from DW_OP_GNU_implicit_pointer. */
238*ef5ccd6cSJohn Marino sect_offset die;
239c50c785cSJohn Marino /* The byte offset into the resulting data. */
240c50c785cSJohn Marino LONGEST offset;
241c50c785cSJohn Marino } ptr;
2425796c8dcSSimon Schubert } v;
2435796c8dcSSimon Schubert
244cf7f2e2dSJohn Marino /* The length of the piece, in bits. */
2455796c8dcSSimon Schubert ULONGEST size;
246cf7f2e2dSJohn Marino /* The piece offset, in bits. */
247cf7f2e2dSJohn Marino ULONGEST offset;
2485796c8dcSSimon Schubert };
2495796c8dcSSimon Schubert
2505796c8dcSSimon Schubert struct dwarf_expr_context *new_dwarf_expr_context (void);
2515796c8dcSSimon Schubert void free_dwarf_expr_context (struct dwarf_expr_context *ctx);
2525796c8dcSSimon Schubert struct cleanup *
2535796c8dcSSimon Schubert make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx);
2545796c8dcSSimon Schubert
255a45ae5f8SJohn Marino void dwarf_expr_push_address (struct dwarf_expr_context *ctx,
256a45ae5f8SJohn Marino CORE_ADDR value,
2575796c8dcSSimon Schubert int in_stack_memory);
258cf7f2e2dSJohn Marino void dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
2595796c8dcSSimon Schubert size_t len);
260a45ae5f8SJohn Marino struct value *dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n);
261cf7f2e2dSJohn Marino CORE_ADDR dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n);
2625796c8dcSSimon Schubert int dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n);
2635796c8dcSSimon Schubert
264cf7f2e2dSJohn Marino void dwarf_expr_require_composition (const gdb_byte *, const gdb_byte *,
265cf7f2e2dSJohn Marino const char *);
2665796c8dcSSimon Schubert
267a45ae5f8SJohn Marino /* Stub dwarf_expr_context_funcs implementations. */
268a45ae5f8SJohn Marino
269a45ae5f8SJohn Marino void ctx_no_get_frame_base (void *baton, const gdb_byte **start,
270a45ae5f8SJohn Marino size_t *length);
271a45ae5f8SJohn Marino CORE_ADDR ctx_no_get_frame_cfa (void *baton);
272a45ae5f8SJohn Marino CORE_ADDR ctx_no_get_frame_pc (void *baton);
273a45ae5f8SJohn Marino CORE_ADDR ctx_no_get_tls_address (void *baton, CORE_ADDR offset);
274*ef5ccd6cSJohn Marino void ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset);
275*ef5ccd6cSJohn Marino struct type *ctx_no_get_base_type (struct dwarf_expr_context *ctx,
276*ef5ccd6cSJohn Marino cu_offset die);
277a45ae5f8SJohn Marino void ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
278*ef5ccd6cSJohn Marino enum call_site_parameter_kind kind,
279*ef5ccd6cSJohn Marino union call_site_parameter_u kind_u,
280a45ae5f8SJohn Marino int deref_size);
281*ef5ccd6cSJohn Marino CORE_ADDR ctx_no_get_addr_index (void *baton, unsigned int index);
282a45ae5f8SJohn Marino
283a45ae5f8SJohn Marino int dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end);
284a45ae5f8SJohn Marino
285a45ae5f8SJohn Marino int dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf,
286a45ae5f8SJohn Marino const gdb_byte *buf_end,
287a45ae5f8SJohn Marino CORE_ADDR *deref_size_return);
288a45ae5f8SJohn Marino
289a45ae5f8SJohn Marino int dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
290a45ae5f8SJohn Marino CORE_ADDR *fb_offset_return);
291a45ae5f8SJohn Marino
292a45ae5f8SJohn Marino int dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
293a45ae5f8SJohn Marino const gdb_byte *buf_end,
294a45ae5f8SJohn Marino CORE_ADDR *sp_offset_return);
295a45ae5f8SJohn Marino
296*ef5ccd6cSJohn Marino /* Wrappers around the leb128 reader routines to simplify them for our
297*ef5ccd6cSJohn Marino purposes. */
298*ef5ccd6cSJohn Marino
299*ef5ccd6cSJohn Marino static inline const gdb_byte *
gdb_read_uleb128(const gdb_byte * buf,const gdb_byte * buf_end,uint64_t * r)300*ef5ccd6cSJohn Marino gdb_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
301*ef5ccd6cSJohn Marino uint64_t *r)
302*ef5ccd6cSJohn Marino {
303*ef5ccd6cSJohn Marino size_t bytes_read = read_uleb128_to_uint64 (buf, buf_end, r);
304*ef5ccd6cSJohn Marino
305*ef5ccd6cSJohn Marino if (bytes_read == 0)
306*ef5ccd6cSJohn Marino return NULL;
307*ef5ccd6cSJohn Marino return buf + bytes_read;
308*ef5ccd6cSJohn Marino }
309*ef5ccd6cSJohn Marino
310*ef5ccd6cSJohn Marino static inline const gdb_byte *
gdb_read_sleb128(const gdb_byte * buf,const gdb_byte * buf_end,int64_t * r)311*ef5ccd6cSJohn Marino gdb_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
312*ef5ccd6cSJohn Marino int64_t *r)
313*ef5ccd6cSJohn Marino {
314*ef5ccd6cSJohn Marino size_t bytes_read = read_sleb128_to_int64 (buf, buf_end, r);
315*ef5ccd6cSJohn Marino
316*ef5ccd6cSJohn Marino if (bytes_read == 0)
317*ef5ccd6cSJohn Marino return NULL;
318*ef5ccd6cSJohn Marino return buf + bytes_read;
319*ef5ccd6cSJohn Marino }
320*ef5ccd6cSJohn Marino
321*ef5ccd6cSJohn Marino static inline const gdb_byte *
gdb_skip_leb128(const gdb_byte * buf,const gdb_byte * buf_end)322*ef5ccd6cSJohn Marino gdb_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
323*ef5ccd6cSJohn Marino {
324*ef5ccd6cSJohn Marino size_t bytes_read = skip_leb128 (buf, buf_end);
325*ef5ccd6cSJohn Marino
326*ef5ccd6cSJohn Marino if (bytes_read == 0)
327*ef5ccd6cSJohn Marino return NULL;
328*ef5ccd6cSJohn Marino return buf + bytes_read;
329*ef5ccd6cSJohn Marino }
330*ef5ccd6cSJohn Marino
331*ef5ccd6cSJohn Marino extern const gdb_byte *safe_read_uleb128 (const gdb_byte *buf,
332*ef5ccd6cSJohn Marino const gdb_byte *buf_end,
333*ef5ccd6cSJohn Marino uint64_t *r);
334*ef5ccd6cSJohn Marino
335*ef5ccd6cSJohn Marino extern const gdb_byte *safe_read_sleb128 (const gdb_byte *buf,
336*ef5ccd6cSJohn Marino const gdb_byte *buf_end,
337*ef5ccd6cSJohn Marino int64_t *r);
338*ef5ccd6cSJohn Marino
339*ef5ccd6cSJohn Marino extern const gdb_byte *safe_skip_leb128 (const gdb_byte *buf,
340*ef5ccd6cSJohn Marino const gdb_byte *buf_end);
341*ef5ccd6cSJohn Marino
3425796c8dcSSimon Schubert #endif /* dwarf2expr.h */
343