1 /* DWARF 2 Expression Evaluator. 2 3 Copyright (C) 2001-2024 Free Software Foundation, Inc. 4 5 Contributed by Daniel Berlin <dan (at) dberlin.org>. 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 #if !defined (DWARF2EXPR_H) 23 #define DWARF2EXPR_H 24 25 #include "leb128.h" 26 #include "dwarf2/call-site.h" 27 28 struct dwarf2_per_objfile; 29 30 /* The location of a value. */ 31 enum dwarf_value_location 32 { 33 /* The piece is in memory. 34 The value on the dwarf stack is its address. */ 35 DWARF_VALUE_MEMORY, 36 37 /* The piece is in a register. 38 The value on the dwarf stack is the register number. */ 39 DWARF_VALUE_REGISTER, 40 41 /* The piece is on the dwarf stack. */ 42 DWARF_VALUE_STACK, 43 44 /* The piece is a literal. */ 45 DWARF_VALUE_LITERAL, 46 47 /* The piece was optimized out. */ 48 DWARF_VALUE_OPTIMIZED_OUT, 49 50 /* The piece is an implicit pointer. */ 51 DWARF_VALUE_IMPLICIT_POINTER 52 }; 53 54 /* A piece of an object, as recorded by DW_OP_piece or DW_OP_bit_piece. */ 55 struct dwarf_expr_piece 56 { 57 enum dwarf_value_location location; 58 59 union 60 { 61 struct 62 { 63 /* This piece's address, for DWARF_VALUE_MEMORY pieces. */ 64 CORE_ADDR addr; 65 /* Non-zero if the piece is known to be in memory and on 66 the program's stack. */ 67 bool in_stack_memory; 68 } mem; 69 70 /* The piece's register number, for DWARF_VALUE_REGISTER pieces. */ 71 int regno; 72 73 /* The piece's literal value, for DWARF_VALUE_STACK pieces. */ 74 struct value *value; 75 76 struct 77 { 78 /* A pointer to the data making up this piece, 79 for DWARF_VALUE_LITERAL pieces. */ 80 const gdb_byte *data; 81 /* The length of the available data. */ 82 ULONGEST length; 83 } literal; 84 85 /* Used for DWARF_VALUE_IMPLICIT_POINTER. */ 86 struct 87 { 88 /* The referent DIE from DW_OP_implicit_pointer. */ 89 sect_offset die_sect_off; 90 /* The byte offset into the resulting data. */ 91 LONGEST offset; 92 } ptr; 93 } v; 94 95 /* The length of the piece, in bits. */ 96 ULONGEST size; 97 /* The piece offset, in bits. */ 98 ULONGEST offset; 99 }; 100 101 /* The dwarf expression stack. */ 102 103 struct dwarf_stack_value 104 { 105 dwarf_stack_value (struct value *value_, int in_stack_memory_) 106 : value (value_), in_stack_memory (in_stack_memory_) 107 {} 108 109 struct value *value; 110 111 /* True if the piece is in memory and is known to be on the program's stack. 112 It is always ok to set this to zero. This is used, for example, to 113 optimize memory access from the target. It can vastly speed up backtraces 114 on long latency connections when "set stack-cache on". */ 115 bool in_stack_memory; 116 }; 117 118 /* The expression evaluator works with a dwarf_expr_context, describing 119 its current state and its callbacks. */ 120 struct dwarf_expr_context 121 { 122 dwarf_expr_context (dwarf2_per_objfile *per_objfile, 123 int addr_size); 124 virtual ~dwarf_expr_context () = default; 125 126 void push_address (CORE_ADDR value, bool in_stack_memory); 127 128 /* Evaluate the expression at ADDR (LEN bytes long) in a given PER_CU 129 and FRAME context. 130 131 AS_LVAL defines if the returned struct value is expected to be a 132 value (false) or a location description (true). 133 134 TYPE, SUBOBJ_TYPE and SUBOBJ_OFFSET describe the expected struct 135 value representation of the evaluation result. 136 137 The ADDR_INFO property can be specified to override the range of 138 memory addresses with the passed in buffer. */ 139 value *evaluate (const gdb_byte *addr, size_t len, bool as_lval, 140 dwarf2_per_cu_data *per_cu, const frame_info_ptr &frame, 141 const struct property_addr_info *addr_info = nullptr, 142 struct type *type = nullptr, 143 struct type *subobj_type = nullptr, 144 LONGEST subobj_offset = 0); 145 146 private: 147 /* The stack of values. */ 148 std::vector<dwarf_stack_value> m_stack; 149 150 /* Target address size in bytes. */ 151 int m_addr_size = 0; 152 153 /* The current depth of dwarf expression recursion, via DW_OP_call*, 154 DW_OP_fbreg, DW_OP_push_object_address, etc., and the maximum 155 depth we'll tolerate before raising an error. */ 156 int m_recursion_depth = 0, m_max_recursion_depth = 0x100; 157 158 /* Location of the value. */ 159 dwarf_value_location m_location = DWARF_VALUE_MEMORY; 160 161 /* For DWARF_VALUE_LITERAL, the current literal value's length and 162 data. For DWARF_VALUE_IMPLICIT_POINTER, LEN is the offset of the 163 target DIE of sect_offset kind. */ 164 ULONGEST m_len = 0; 165 const gdb_byte *m_data = nullptr; 166 167 /* Initialization status of variable: True if variable has been 168 initialized; false otherwise. */ 169 bool m_initialized = false; 170 171 /* A vector of pieces. 172 173 Each time DW_OP_piece is executed, we add a new element to the 174 end of this array, recording the current top of the stack, the 175 current location, and the size given as the operand to 176 DW_OP_piece. We then pop the top value from the stack, reset the 177 location, and resume evaluation. 178 179 The Dwarf spec doesn't say whether DW_OP_piece pops the top value 180 from the stack. We do, ensuring that clients of this interface 181 expecting to see a value left on the top of the stack (say, code 182 evaluating frame base expressions or CFA's specified with 183 DW_CFA_def_cfa_expression) will get an error if the expression 184 actually marks all the values it computes as pieces. 185 186 If an expression never uses DW_OP_piece, num_pieces will be zero. 187 (It would be nice to present these cases as expressions yielding 188 a single piece, so that callers need not distinguish between the 189 no-DW_OP_piece and one-DW_OP_piece cases. But expressions with 190 no DW_OP_piece operations have no value to place in a piece's 191 'size' field; the size comes from the surrounding data. So the 192 two cases need to be handled separately.) */ 193 std::vector<dwarf_expr_piece> m_pieces; 194 195 /* We evaluate the expression in the context of this objfile. */ 196 dwarf2_per_objfile *m_per_objfile; 197 198 /* Frame information used for the evaluation. */ 199 frame_info_ptr m_frame = nullptr; 200 201 /* Compilation unit used for the evaluation. */ 202 dwarf2_per_cu_data *m_per_cu = nullptr; 203 204 /* Property address info used for the evaluation. */ 205 const struct property_addr_info *m_addr_info = nullptr; 206 207 void eval (const gdb_byte *addr, size_t len); 208 struct type *address_type () const; 209 void push (struct value *value, bool in_stack_memory); 210 bool stack_empty_p () const; 211 void add_piece (ULONGEST size, ULONGEST offset); 212 void execute_stack_op (const gdb_byte *op_ptr, const gdb_byte *op_end); 213 void pop (); 214 struct value *fetch (int n); 215 CORE_ADDR fetch_address (int n); 216 bool fetch_in_stack_memory (int n); 217 218 /* Fetch the result of the expression evaluation in a form of 219 a struct value, where TYPE, SUBOBJ_TYPE and SUBOBJ_OFFSET 220 describe the source level representation of that result. 221 AS_LVAL defines if the fetched struct value is expected to 222 be a value or a location description. */ 223 value *fetch_result (struct type *type, struct type *subobj_type, 224 LONGEST subobj_offset, bool as_lval); 225 226 /* Return the location expression for the frame base attribute, in 227 START and LENGTH. The result must be live until the current 228 expression evaluation is complete. */ 229 void get_frame_base (const gdb_byte **start, size_t *length); 230 231 /* Return the base type given by the indicated DIE at DIE_CU_OFF. 232 This can throw an exception if the DIE is invalid or does not 233 represent a base type. */ 234 struct type *get_base_type (cu_offset die_cu_off); 235 236 /* Execute DW_AT_location expression for the DWARF expression 237 subroutine in the DIE at DIE_CU_OFF in the CU. Do not touch 238 STACK while it being passed to and returned from the called DWARF 239 subroutine. */ 240 void dwarf_call (cu_offset die_cu_off); 241 242 /* Push on DWARF stack an entry evaluated for DW_TAG_call_site's 243 parameter matching KIND and KIND_U at the caller of specified BATON. 244 If DEREF_SIZE is not -1 then use DW_AT_call_data_value instead of 245 DW_AT_call_value. */ 246 void push_dwarf_reg_entry_value (call_site_parameter_kind kind, 247 call_site_parameter_u kind_u, 248 int deref_size); 249 250 /* Read LENGTH bytes at ADDR into BUF. This method also handles the 251 case where a caller of the evaluator passes in some data, 252 but with the address being 0. In this situation, we arrange for 253 memory reads to come from the passed-in buffer. */ 254 void read_mem (gdb_byte *buf, CORE_ADDR addr, size_t length); 255 }; 256 257 /* Return the value of register number REG (a DWARF register number), 258 read as an address in a given FRAME. */ 259 CORE_ADDR read_addr_from_reg (const frame_info_ptr &frame, int reg); 260 261 void dwarf_expr_require_composition (const gdb_byte *, const gdb_byte *, 262 const char *); 263 264 int dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end); 265 266 int dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, 267 const gdb_byte *buf_end, 268 CORE_ADDR *deref_size_return); 269 270 int dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end, 271 CORE_ADDR *fb_offset_return); 272 273 int dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf, 274 const gdb_byte *buf_end, 275 CORE_ADDR *sp_offset_return); 276 277 /* Wrappers around the leb128 reader routines to simplify them for our 278 purposes. */ 279 280 static inline const gdb_byte * 281 gdb_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, 282 uint64_t *r) 283 { 284 size_t bytes_read = read_uleb128_to_uint64 (buf, buf_end, r); 285 286 if (bytes_read == 0) 287 return NULL; 288 return buf + bytes_read; 289 } 290 291 static inline const gdb_byte * 292 gdb_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, 293 int64_t *r) 294 { 295 size_t bytes_read = read_sleb128_to_int64 (buf, buf_end, r); 296 297 if (bytes_read == 0) 298 return NULL; 299 return buf + bytes_read; 300 } 301 302 static inline const gdb_byte * 303 gdb_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end) 304 { 305 size_t bytes_read = skip_leb128 (buf, buf_end); 306 307 if (bytes_read == 0) 308 return NULL; 309 return buf + bytes_read; 310 } 311 312 extern const gdb_byte *safe_read_uleb128 (const gdb_byte *buf, 313 const gdb_byte *buf_end, 314 uint64_t *r); 315 316 extern const gdb_byte *safe_read_sleb128 (const gdb_byte *buf, 317 const gdb_byte *buf_end, 318 int64_t *r); 319 320 extern const gdb_byte *safe_skip_leb128 (const gdb_byte *buf, 321 const gdb_byte *buf_end); 322 323 #endif /* DWARF2EXPR_H */ 324