Commit | Line | Data |
---|---|---|
4c2df51b | 1 | /* DWARF 2 location expression support for GDB. |
feb13ab0 | 2 | |
0b302171 | 3 | Copyright (C) 2003, 2005, 2007-2012 Free Software Foundation, Inc. |
feb13ab0 | 4 | |
4c2df51b DJ |
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 | |
a9762ec7 JB |
11 | the Free Software Foundation; either version 3 of the License, or |
12 | (at your option) any later version. | |
4c2df51b | 13 | |
a9762ec7 JB |
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. | |
4c2df51b DJ |
18 | |
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4c2df51b DJ |
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" | |
a55cc764 DJ |
29 | #include "ax.h" |
30 | #include "ax-gdb.h" | |
e4adbba9 | 31 | #include "regcache.h" |
c3228f12 | 32 | #include "objfiles.h" |
93ad78a7 | 33 | #include "exceptions.h" |
edb3359d | 34 | #include "block.h" |
8e3b41a9 | 35 | #include "gdbcmd.h" |
4c2df51b | 36 | |
fa8f86ff | 37 | #include "dwarf2.h" |
4c2df51b DJ |
38 | #include "dwarf2expr.h" |
39 | #include "dwarf2loc.h" | |
e7802207 | 40 | #include "dwarf2-frame.h" |
4c2df51b DJ |
41 | |
42 | #include "gdb_string.h" | |
eff4f95e | 43 | #include "gdb_assert.h" |
4c2df51b | 44 | |
fa3064dd YQ |
45 | DEF_VEC_I(int); |
46 | ||
9eae7c52 TT |
47 | extern int dwarf2_always_disassemble; |
48 | ||
1632a688 JK |
49 | static void dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc, |
50 | const gdb_byte **start, size_t *length); | |
0936ad1d | 51 | |
8e3b41a9 JK |
52 | static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs; |
53 | ||
1632a688 JK |
54 | static struct value *dwarf2_evaluate_loc_desc_full (struct type *type, |
55 | struct frame_info *frame, | |
56 | const gdb_byte *data, | |
57 | unsigned short size, | |
58 | struct dwarf2_per_cu_data *per_cu, | |
59 | LONGEST byte_offset); | |
8cf6f0b1 | 60 | |
f664829e DE |
61 | /* Until these have formal names, we define these here. |
62 | ref: http://gcc.gnu.org/wiki/DebugFission | |
63 | Each entry in .debug_loc.dwo begins with a byte that describes the entry, | |
64 | and is then followed by data specific to that entry. */ | |
65 | ||
66 | enum debug_loc_kind | |
67 | { | |
68 | /* Indicates the end of the list of entries. */ | |
69 | DEBUG_LOC_END_OF_LIST = 0, | |
70 | ||
71 | /* This is followed by an unsigned LEB128 number that is an index into | |
72 | .debug_addr and specifies the base address for all following entries. */ | |
73 | DEBUG_LOC_BASE_ADDRESS = 1, | |
74 | ||
75 | /* This is followed by two unsigned LEB128 numbers that are indices into | |
76 | .debug_addr and specify the beginning and ending addresses, and then | |
77 | a normal location expression as in .debug_loc. */ | |
3771a44c DE |
78 | DEBUG_LOC_START_END = 2, |
79 | ||
80 | /* This is followed by an unsigned LEB128 number that is an index into | |
81 | .debug_addr and specifies the beginning address, and a 4 byte unsigned | |
82 | number that specifies the length, and then a normal location expression | |
83 | as in .debug_loc. */ | |
84 | DEBUG_LOC_START_LENGTH = 3, | |
f664829e DE |
85 | |
86 | /* An internal value indicating there is insufficient data. */ | |
87 | DEBUG_LOC_BUFFER_OVERFLOW = -1, | |
88 | ||
89 | /* An internal value indicating an invalid kind of entry was found. */ | |
90 | DEBUG_LOC_INVALID_ENTRY = -2 | |
91 | }; | |
92 | ||
93 | /* Decode the addresses in a non-dwo .debug_loc entry. | |
94 | A pointer to the next byte to examine is returned in *NEW_PTR. | |
95 | The encoded low,high addresses are return in *LOW,*HIGH. | |
96 | The result indicates the kind of entry found. */ | |
97 | ||
98 | static enum debug_loc_kind | |
99 | decode_debug_loc_addresses (const gdb_byte *loc_ptr, const gdb_byte *buf_end, | |
100 | const gdb_byte **new_ptr, | |
101 | CORE_ADDR *low, CORE_ADDR *high, | |
102 | enum bfd_endian byte_order, | |
103 | unsigned int addr_size, | |
104 | int signed_addr_p) | |
105 | { | |
106 | CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1)); | |
107 | ||
108 | if (buf_end - loc_ptr < 2 * addr_size) | |
109 | return DEBUG_LOC_BUFFER_OVERFLOW; | |
110 | ||
111 | if (signed_addr_p) | |
112 | *low = extract_signed_integer (loc_ptr, addr_size, byte_order); | |
113 | else | |
114 | *low = extract_unsigned_integer (loc_ptr, addr_size, byte_order); | |
115 | loc_ptr += addr_size; | |
116 | ||
117 | if (signed_addr_p) | |
118 | *high = extract_signed_integer (loc_ptr, addr_size, byte_order); | |
119 | else | |
120 | *high = extract_unsigned_integer (loc_ptr, addr_size, byte_order); | |
121 | loc_ptr += addr_size; | |
122 | ||
123 | *new_ptr = loc_ptr; | |
124 | ||
125 | /* A base-address-selection entry. */ | |
126 | if ((*low & base_mask) == base_mask) | |
127 | return DEBUG_LOC_BASE_ADDRESS; | |
128 | ||
129 | /* An end-of-list entry. */ | |
130 | if (*low == 0 && *high == 0) | |
131 | return DEBUG_LOC_END_OF_LIST; | |
132 | ||
3771a44c | 133 | return DEBUG_LOC_START_END; |
f664829e DE |
134 | } |
135 | ||
136 | /* Decode the addresses in .debug_loc.dwo entry. | |
137 | A pointer to the next byte to examine is returned in *NEW_PTR. | |
138 | The encoded low,high addresses are return in *LOW,*HIGH. | |
139 | The result indicates the kind of entry found. */ | |
140 | ||
141 | static enum debug_loc_kind | |
142 | decode_debug_loc_dwo_addresses (struct dwarf2_per_cu_data *per_cu, | |
143 | const gdb_byte *loc_ptr, | |
144 | const gdb_byte *buf_end, | |
145 | const gdb_byte **new_ptr, | |
3771a44c DE |
146 | CORE_ADDR *low, CORE_ADDR *high, |
147 | enum bfd_endian byte_order) | |
f664829e | 148 | { |
9fccedf7 | 149 | uint64_t low_index, high_index; |
f664829e DE |
150 | |
151 | if (loc_ptr == buf_end) | |
152 | return DEBUG_LOC_BUFFER_OVERFLOW; | |
153 | ||
154 | switch (*loc_ptr++) | |
155 | { | |
156 | case DEBUG_LOC_END_OF_LIST: | |
157 | *new_ptr = loc_ptr; | |
158 | return DEBUG_LOC_END_OF_LIST; | |
159 | case DEBUG_LOC_BASE_ADDRESS: | |
160 | *low = 0; | |
161 | loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index); | |
162 | if (loc_ptr == NULL) | |
163 | return DEBUG_LOC_BUFFER_OVERFLOW; | |
164 | *high = dwarf2_read_addr_index (per_cu, high_index); | |
165 | *new_ptr = loc_ptr; | |
166 | return DEBUG_LOC_BASE_ADDRESS; | |
3771a44c | 167 | case DEBUG_LOC_START_END: |
f664829e DE |
168 | loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index); |
169 | if (loc_ptr == NULL) | |
170 | return DEBUG_LOC_BUFFER_OVERFLOW; | |
171 | *low = dwarf2_read_addr_index (per_cu, low_index); | |
172 | loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &high_index); | |
173 | if (loc_ptr == NULL) | |
174 | return DEBUG_LOC_BUFFER_OVERFLOW; | |
175 | *high = dwarf2_read_addr_index (per_cu, high_index); | |
176 | *new_ptr = loc_ptr; | |
3771a44c DE |
177 | return DEBUG_LOC_START_END; |
178 | case DEBUG_LOC_START_LENGTH: | |
179 | loc_ptr = gdb_read_uleb128 (loc_ptr, buf_end, &low_index); | |
180 | if (loc_ptr == NULL) | |
181 | return DEBUG_LOC_BUFFER_OVERFLOW; | |
182 | *low = dwarf2_read_addr_index (per_cu, low_index); | |
183 | if (loc_ptr + 4 > buf_end) | |
184 | return DEBUG_LOC_BUFFER_OVERFLOW; | |
185 | *high = *low; | |
186 | *high += extract_unsigned_integer (loc_ptr, 4, byte_order); | |
187 | *new_ptr = loc_ptr + 4; | |
188 | return DEBUG_LOC_START_LENGTH; | |
f664829e DE |
189 | default: |
190 | return DEBUG_LOC_INVALID_ENTRY; | |
191 | } | |
192 | } | |
193 | ||
8cf6f0b1 | 194 | /* A function for dealing with location lists. Given a |
0d53c4c4 DJ |
195 | symbol baton (BATON) and a pc value (PC), find the appropriate |
196 | location expression, set *LOCEXPR_LENGTH, and return a pointer | |
197 | to the beginning of the expression. Returns NULL on failure. | |
198 | ||
199 | For now, only return the first matching location expression; there | |
200 | can be more than one in the list. */ | |
201 | ||
8cf6f0b1 TT |
202 | const gdb_byte * |
203 | dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton, | |
204 | size_t *locexpr_length, CORE_ADDR pc) | |
0d53c4c4 | 205 | { |
ae0d2f24 | 206 | struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu); |
f7fd4728 | 207 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
e17a4113 | 208 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
ae0d2f24 | 209 | unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu); |
d4a087c7 | 210 | int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd); |
8edfa926 | 211 | /* Adjust base_address for relocatable objects. */ |
9aa1f1e3 | 212 | CORE_ADDR base_offset = dwarf2_per_cu_text_offset (baton->per_cu); |
8edfa926 | 213 | CORE_ADDR base_address = baton->base_address + base_offset; |
f664829e | 214 | const gdb_byte *loc_ptr, *buf_end; |
0d53c4c4 DJ |
215 | |
216 | loc_ptr = baton->data; | |
217 | buf_end = baton->data + baton->size; | |
218 | ||
219 | while (1) | |
220 | { | |
f664829e DE |
221 | CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */ |
222 | int length; | |
223 | enum debug_loc_kind kind; | |
224 | const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */ | |
225 | ||
226 | if (baton->from_dwo) | |
227 | kind = decode_debug_loc_dwo_addresses (baton->per_cu, | |
228 | loc_ptr, buf_end, &new_ptr, | |
3771a44c | 229 | &low, &high, byte_order); |
d4a087c7 | 230 | else |
f664829e DE |
231 | kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr, |
232 | &low, &high, | |
233 | byte_order, addr_size, | |
234 | signed_addr_p); | |
235 | loc_ptr = new_ptr; | |
236 | switch (kind) | |
1d6edc3c | 237 | { |
f664829e | 238 | case DEBUG_LOC_END_OF_LIST: |
1d6edc3c JK |
239 | *locexpr_length = 0; |
240 | return NULL; | |
f664829e DE |
241 | case DEBUG_LOC_BASE_ADDRESS: |
242 | base_address = high + base_offset; | |
243 | continue; | |
3771a44c DE |
244 | case DEBUG_LOC_START_END: |
245 | case DEBUG_LOC_START_LENGTH: | |
f664829e DE |
246 | break; |
247 | case DEBUG_LOC_BUFFER_OVERFLOW: | |
248 | case DEBUG_LOC_INVALID_ENTRY: | |
249 | error (_("dwarf2_find_location_expression: " | |
250 | "Corrupted DWARF expression.")); | |
251 | default: | |
252 | gdb_assert_not_reached ("bad debug_loc_kind"); | |
1d6edc3c | 253 | } |
b5758fe4 | 254 | |
0d53c4c4 DJ |
255 | /* Otherwise, a location expression entry. */ |
256 | low += base_address; | |
257 | high += base_address; | |
258 | ||
e17a4113 | 259 | length = extract_unsigned_integer (loc_ptr, 2, byte_order); |
0d53c4c4 DJ |
260 | loc_ptr += 2; |
261 | ||
e18b2753 JK |
262 | if (low == high && pc == low) |
263 | { | |
264 | /* This is entry PC record present only at entry point | |
265 | of a function. Verify it is really the function entry point. */ | |
266 | ||
267 | struct block *pc_block = block_for_pc (pc); | |
268 | struct symbol *pc_func = NULL; | |
269 | ||
270 | if (pc_block) | |
271 | pc_func = block_linkage_function (pc_block); | |
272 | ||
273 | if (pc_func && pc == BLOCK_START (SYMBOL_BLOCK_VALUE (pc_func))) | |
274 | { | |
275 | *locexpr_length = length; | |
276 | return loc_ptr; | |
277 | } | |
278 | } | |
279 | ||
0d53c4c4 DJ |
280 | if (pc >= low && pc < high) |
281 | { | |
282 | *locexpr_length = length; | |
283 | return loc_ptr; | |
284 | } | |
285 | ||
286 | loc_ptr += length; | |
287 | } | |
288 | } | |
289 | ||
4c2df51b DJ |
290 | /* This is the baton used when performing dwarf2 expression |
291 | evaluation. */ | |
292 | struct dwarf_expr_baton | |
293 | { | |
294 | struct frame_info *frame; | |
17ea53c3 | 295 | struct dwarf2_per_cu_data *per_cu; |
4c2df51b DJ |
296 | }; |
297 | ||
298 | /* Helper functions for dwarf2_evaluate_loc_desc. */ | |
299 | ||
4bc9efe1 | 300 | /* Using the frame specified in BATON, return the value of register |
0b2b0195 | 301 | REGNUM, treated as a pointer. */ |
4c2df51b | 302 | static CORE_ADDR |
61fbb938 | 303 | dwarf_expr_read_reg (void *baton, int dwarf_regnum) |
4c2df51b | 304 | { |
4c2df51b | 305 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; |
5e2b427d | 306 | struct gdbarch *gdbarch = get_frame_arch (debaton->frame); |
e5192dd8 | 307 | CORE_ADDR result; |
0b2b0195 | 308 | int regnum; |
e4adbba9 | 309 | |
5e2b427d UW |
310 | regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum); |
311 | result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr, | |
0b2b0195 | 312 | regnum, debaton->frame); |
4c2df51b DJ |
313 | return result; |
314 | } | |
315 | ||
316 | /* Read memory at ADDR (length LEN) into BUF. */ | |
317 | ||
318 | static void | |
852483bc | 319 | dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len) |
4c2df51b DJ |
320 | { |
321 | read_memory (addr, buf, len); | |
322 | } | |
323 | ||
324 | /* Using the frame specified in BATON, find the location expression | |
325 | describing the frame base. Return a pointer to it in START and | |
326 | its length in LENGTH. */ | |
327 | static void | |
0d45f56e | 328 | dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length) |
4c2df51b | 329 | { |
da62e633 AC |
330 | /* FIXME: cagney/2003-03-26: This code should be using |
331 | get_frame_base_address(), and then implement a dwarf2 specific | |
332 | this_base method. */ | |
4c2df51b | 333 | struct symbol *framefunc; |
4c2df51b | 334 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; |
0d53c4c4 | 335 | |
edb3359d DJ |
336 | /* Use block_linkage_function, which returns a real (not inlined) |
337 | function, instead of get_frame_function, which may return an | |
338 | inlined function. */ | |
339 | framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL)); | |
0d53c4c4 | 340 | |
eff4f95e JG |
341 | /* If we found a frame-relative symbol then it was certainly within |
342 | some function associated with a frame. If we can't find the frame, | |
343 | something has gone wrong. */ | |
344 | gdb_assert (framefunc != NULL); | |
345 | ||
0936ad1d SS |
346 | dwarf_expr_frame_base_1 (framefunc, |
347 | get_frame_address_in_block (debaton->frame), | |
348 | start, length); | |
349 | } | |
350 | ||
351 | static void | |
352 | dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc, | |
0d45f56e | 353 | const gdb_byte **start, size_t *length) |
0936ad1d | 354 | { |
edb3359d | 355 | if (SYMBOL_LOCATION_BATON (framefunc) == NULL) |
1d6edc3c | 356 | *length = 0; |
edb3359d | 357 | else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs) |
0d53c4c4 DJ |
358 | { |
359 | struct dwarf2_loclist_baton *symbaton; | |
22c6caba | 360 | |
0d53c4c4 | 361 | symbaton = SYMBOL_LOCATION_BATON (framefunc); |
8cf6f0b1 | 362 | *start = dwarf2_find_location_expression (symbaton, length, pc); |
0d53c4c4 DJ |
363 | } |
364 | else | |
365 | { | |
366 | struct dwarf2_locexpr_baton *symbaton; | |
9a619af0 | 367 | |
0d53c4c4 | 368 | symbaton = SYMBOL_LOCATION_BATON (framefunc); |
ebd3bcc1 JK |
369 | if (symbaton != NULL) |
370 | { | |
371 | *length = symbaton->size; | |
372 | *start = symbaton->data; | |
373 | } | |
374 | else | |
1d6edc3c | 375 | *length = 0; |
0d53c4c4 DJ |
376 | } |
377 | ||
1d6edc3c | 378 | if (*length == 0) |
8a3fe4f8 | 379 | error (_("Could not find the frame base for \"%s\"."), |
0d53c4c4 | 380 | SYMBOL_NATURAL_NAME (framefunc)); |
4c2df51b DJ |
381 | } |
382 | ||
e7802207 TT |
383 | /* Helper function for dwarf2_evaluate_loc_desc. Computes the CFA for |
384 | the frame in BATON. */ | |
385 | ||
386 | static CORE_ADDR | |
387 | dwarf_expr_frame_cfa (void *baton) | |
388 | { | |
389 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; | |
9a619af0 | 390 | |
e7802207 TT |
391 | return dwarf2_frame_cfa (debaton->frame); |
392 | } | |
393 | ||
8cf6f0b1 TT |
394 | /* Helper function for dwarf2_evaluate_loc_desc. Computes the PC for |
395 | the frame in BATON. */ | |
396 | ||
397 | static CORE_ADDR | |
398 | dwarf_expr_frame_pc (void *baton) | |
399 | { | |
400 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; | |
401 | ||
402 | return get_frame_address_in_block (debaton->frame); | |
403 | } | |
404 | ||
4c2df51b DJ |
405 | /* Using the objfile specified in BATON, find the address for the |
406 | current thread's thread-local storage with offset OFFSET. */ | |
407 | static CORE_ADDR | |
408 | dwarf_expr_tls_address (void *baton, CORE_ADDR offset) | |
409 | { | |
410 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; | |
17ea53c3 | 411 | struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu); |
4c2df51b | 412 | |
17ea53c3 | 413 | return target_translate_tls_address (objfile, offset); |
4c2df51b DJ |
414 | } |
415 | ||
3e43a32a MS |
416 | /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in |
417 | current CU (as is PER_CU). State of the CTX is not affected by the | |
418 | call and return. */ | |
5c631832 JK |
419 | |
420 | static void | |
b64f50a1 | 421 | per_cu_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset, |
8cf6f0b1 TT |
422 | struct dwarf2_per_cu_data *per_cu, |
423 | CORE_ADDR (*get_frame_pc) (void *baton), | |
424 | void *baton) | |
5c631832 JK |
425 | { |
426 | struct dwarf2_locexpr_baton block; | |
427 | ||
8cf6f0b1 TT |
428 | block = dwarf2_fetch_die_location_block (die_offset, per_cu, |
429 | get_frame_pc, baton); | |
5c631832 JK |
430 | |
431 | /* DW_OP_call_ref is currently not supported. */ | |
432 | gdb_assert (block.per_cu == per_cu); | |
433 | ||
434 | dwarf_expr_eval (ctx, block.data, block.size); | |
435 | } | |
436 | ||
437 | /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc. */ | |
438 | ||
439 | static void | |
b64f50a1 | 440 | dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset) |
5c631832 JK |
441 | { |
442 | struct dwarf_expr_baton *debaton = ctx->baton; | |
443 | ||
37b50a69 | 444 | per_cu_dwarf_call (ctx, die_offset, debaton->per_cu, |
9e8b7a03 | 445 | ctx->funcs->get_frame_pc, ctx->baton); |
5c631832 JK |
446 | } |
447 | ||
8a9b8146 TT |
448 | /* Callback function for dwarf2_evaluate_loc_desc. */ |
449 | ||
450 | static struct type * | |
b64f50a1 JK |
451 | dwarf_expr_get_base_type (struct dwarf_expr_context *ctx, |
452 | cu_offset die_offset) | |
8a9b8146 TT |
453 | { |
454 | struct dwarf_expr_baton *debaton = ctx->baton; | |
455 | ||
456 | return dwarf2_get_die_type (die_offset, debaton->per_cu); | |
457 | } | |
458 | ||
8e3b41a9 JK |
459 | /* See dwarf2loc.h. */ |
460 | ||
461 | int entry_values_debug = 0; | |
462 | ||
463 | /* Helper to set entry_values_debug. */ | |
464 | ||
465 | static void | |
466 | show_entry_values_debug (struct ui_file *file, int from_tty, | |
467 | struct cmd_list_element *c, const char *value) | |
468 | { | |
469 | fprintf_filtered (file, | |
470 | _("Entry values and tail call frames debugging is %s.\n"), | |
471 | value); | |
472 | } | |
473 | ||
474 | /* Find DW_TAG_GNU_call_site's DW_AT_GNU_call_site_target address. | |
475 | CALLER_FRAME (for registers) can be NULL if it is not known. This function | |
476 | always returns valid address or it throws NO_ENTRY_VALUE_ERROR. */ | |
477 | ||
478 | static CORE_ADDR | |
479 | call_site_to_target_addr (struct gdbarch *call_site_gdbarch, | |
480 | struct call_site *call_site, | |
481 | struct frame_info *caller_frame) | |
482 | { | |
483 | switch (FIELD_LOC_KIND (call_site->target)) | |
484 | { | |
485 | case FIELD_LOC_KIND_DWARF_BLOCK: | |
486 | { | |
487 | struct dwarf2_locexpr_baton *dwarf_block; | |
488 | struct value *val; | |
489 | struct type *caller_core_addr_type; | |
490 | struct gdbarch *caller_arch; | |
491 | ||
492 | dwarf_block = FIELD_DWARF_BLOCK (call_site->target); | |
493 | if (dwarf_block == NULL) | |
494 | { | |
495 | struct minimal_symbol *msym; | |
496 | ||
497 | msym = lookup_minimal_symbol_by_pc (call_site->pc - 1); | |
498 | throw_error (NO_ENTRY_VALUE_ERROR, | |
499 | _("DW_AT_GNU_call_site_target is not specified " | |
500 | "at %s in %s"), | |
501 | paddress (call_site_gdbarch, call_site->pc), | |
502 | msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym)); | |
503 | ||
504 | } | |
505 | if (caller_frame == NULL) | |
506 | { | |
507 | struct minimal_symbol *msym; | |
508 | ||
509 | msym = lookup_minimal_symbol_by_pc (call_site->pc - 1); | |
510 | throw_error (NO_ENTRY_VALUE_ERROR, | |
511 | _("DW_AT_GNU_call_site_target DWARF block resolving " | |
512 | "requires known frame which is currently not " | |
513 | "available at %s in %s"), | |
514 | paddress (call_site_gdbarch, call_site->pc), | |
515 | msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym)); | |
516 | ||
517 | } | |
518 | caller_arch = get_frame_arch (caller_frame); | |
519 | caller_core_addr_type = builtin_type (caller_arch)->builtin_func_ptr; | |
520 | val = dwarf2_evaluate_loc_desc (caller_core_addr_type, caller_frame, | |
521 | dwarf_block->data, dwarf_block->size, | |
522 | dwarf_block->per_cu); | |
523 | /* DW_AT_GNU_call_site_target is a DWARF expression, not a DWARF | |
524 | location. */ | |
525 | if (VALUE_LVAL (val) == lval_memory) | |
526 | return value_address (val); | |
527 | else | |
528 | return value_as_address (val); | |
529 | } | |
530 | ||
531 | case FIELD_LOC_KIND_PHYSNAME: | |
532 | { | |
533 | const char *physname; | |
534 | struct minimal_symbol *msym; | |
535 | ||
536 | physname = FIELD_STATIC_PHYSNAME (call_site->target); | |
537 | msym = lookup_minimal_symbol_text (physname, NULL); | |
538 | if (msym == NULL) | |
539 | { | |
540 | msym = lookup_minimal_symbol_by_pc (call_site->pc - 1); | |
541 | throw_error (NO_ENTRY_VALUE_ERROR, | |
542 | _("Cannot find function \"%s\" for a call site target " | |
543 | "at %s in %s"), | |
544 | physname, paddress (call_site_gdbarch, call_site->pc), | |
545 | msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym)); | |
546 | ||
547 | } | |
548 | return SYMBOL_VALUE_ADDRESS (msym); | |
549 | } | |
550 | ||
551 | case FIELD_LOC_KIND_PHYSADDR: | |
552 | return FIELD_STATIC_PHYSADDR (call_site->target); | |
553 | ||
554 | default: | |
555 | internal_error (__FILE__, __LINE__, _("invalid call site target kind")); | |
556 | } | |
557 | } | |
558 | ||
111c6489 JK |
559 | /* Convert function entry point exact address ADDR to the function which is |
560 | compliant with TAIL_CALL_LIST_COMPLETE condition. Throw | |
561 | NO_ENTRY_VALUE_ERROR otherwise. */ | |
562 | ||
563 | static struct symbol * | |
564 | func_addr_to_tail_call_list (struct gdbarch *gdbarch, CORE_ADDR addr) | |
565 | { | |
566 | struct symbol *sym = find_pc_function (addr); | |
567 | struct type *type; | |
568 | ||
569 | if (sym == NULL || BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) != addr) | |
570 | throw_error (NO_ENTRY_VALUE_ERROR, | |
571 | _("DW_TAG_GNU_call_site resolving failed to find function " | |
572 | "name for address %s"), | |
573 | paddress (gdbarch, addr)); | |
574 | ||
575 | type = SYMBOL_TYPE (sym); | |
576 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_FUNC); | |
577 | gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC); | |
578 | ||
579 | return sym; | |
580 | } | |
581 | ||
2d6c5dc2 JK |
582 | /* Verify function with entry point exact address ADDR can never call itself |
583 | via its tail calls (incl. transitively). Throw NO_ENTRY_VALUE_ERROR if it | |
584 | can call itself via tail calls. | |
585 | ||
586 | If a funtion can tail call itself its entry value based parameters are | |
587 | unreliable. There is no verification whether the value of some/all | |
588 | parameters is unchanged through the self tail call, we expect if there is | |
589 | a self tail call all the parameters can be modified. */ | |
590 | ||
591 | static void | |
592 | func_verify_no_selftailcall (struct gdbarch *gdbarch, CORE_ADDR verify_addr) | |
593 | { | |
594 | struct obstack addr_obstack; | |
595 | struct cleanup *old_chain; | |
596 | CORE_ADDR addr; | |
597 | ||
598 | /* Track here CORE_ADDRs which were already visited. */ | |
599 | htab_t addr_hash; | |
600 | ||
601 | /* The verification is completely unordered. Track here function addresses | |
602 | which still need to be iterated. */ | |
603 | VEC (CORE_ADDR) *todo = NULL; | |
604 | ||
605 | obstack_init (&addr_obstack); | |
606 | old_chain = make_cleanup_obstack_free (&addr_obstack); | |
607 | addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL, | |
608 | &addr_obstack, hashtab_obstack_allocate, | |
609 | NULL); | |
610 | make_cleanup_htab_delete (addr_hash); | |
611 | ||
612 | make_cleanup (VEC_cleanup (CORE_ADDR), &todo); | |
613 | ||
614 | VEC_safe_push (CORE_ADDR, todo, verify_addr); | |
615 | while (!VEC_empty (CORE_ADDR, todo)) | |
616 | { | |
617 | struct symbol *func_sym; | |
618 | struct call_site *call_site; | |
619 | ||
620 | addr = VEC_pop (CORE_ADDR, todo); | |
621 | ||
622 | func_sym = func_addr_to_tail_call_list (gdbarch, addr); | |
623 | ||
624 | for (call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (func_sym)); | |
625 | call_site; call_site = call_site->tail_call_next) | |
626 | { | |
627 | CORE_ADDR target_addr; | |
628 | void **slot; | |
629 | ||
630 | /* CALLER_FRAME with registers is not available for tail-call jumped | |
631 | frames. */ | |
632 | target_addr = call_site_to_target_addr (gdbarch, call_site, NULL); | |
633 | ||
634 | if (target_addr == verify_addr) | |
635 | { | |
636 | struct minimal_symbol *msym; | |
637 | ||
638 | msym = lookup_minimal_symbol_by_pc (verify_addr); | |
639 | throw_error (NO_ENTRY_VALUE_ERROR, | |
640 | _("DW_OP_GNU_entry_value resolving has found " | |
641 | "function \"%s\" at %s can call itself via tail " | |
642 | "calls"), | |
643 | msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym), | |
644 | paddress (gdbarch, verify_addr)); | |
645 | } | |
646 | ||
647 | slot = htab_find_slot (addr_hash, &target_addr, INSERT); | |
648 | if (*slot == NULL) | |
649 | { | |
650 | *slot = obstack_copy (&addr_obstack, &target_addr, | |
651 | sizeof (target_addr)); | |
652 | VEC_safe_push (CORE_ADDR, todo, target_addr); | |
653 | } | |
654 | } | |
655 | } | |
656 | ||
657 | do_cleanups (old_chain); | |
658 | } | |
659 | ||
111c6489 JK |
660 | /* Print user readable form of CALL_SITE->PC to gdb_stdlog. Used only for |
661 | ENTRY_VALUES_DEBUG. */ | |
662 | ||
663 | static void | |
664 | tailcall_dump (struct gdbarch *gdbarch, const struct call_site *call_site) | |
665 | { | |
666 | CORE_ADDR addr = call_site->pc; | |
667 | struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (addr - 1); | |
668 | ||
669 | fprintf_unfiltered (gdb_stdlog, " %s(%s)", paddress (gdbarch, addr), | |
670 | msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym)); | |
671 | ||
672 | } | |
673 | ||
674 | /* vec.h needs single word type name, typedef it. */ | |
675 | typedef struct call_site *call_sitep; | |
676 | ||
677 | /* Define VEC (call_sitep) functions. */ | |
678 | DEF_VEC_P (call_sitep); | |
679 | ||
680 | /* Intersect RESULTP with CHAIN to keep RESULTP unambiguous, keep in RESULTP | |
681 | only top callers and bottom callees which are present in both. GDBARCH is | |
682 | used only for ENTRY_VALUES_DEBUG. RESULTP is NULL after return if there are | |
683 | no remaining possibilities to provide unambiguous non-trivial result. | |
684 | RESULTP should point to NULL on the first (initialization) call. Caller is | |
685 | responsible for xfree of any RESULTP data. */ | |
686 | ||
687 | static void | |
688 | chain_candidate (struct gdbarch *gdbarch, struct call_site_chain **resultp, | |
689 | VEC (call_sitep) *chain) | |
690 | { | |
691 | struct call_site_chain *result = *resultp; | |
692 | long length = VEC_length (call_sitep, chain); | |
693 | int callers, callees, idx; | |
694 | ||
695 | if (result == NULL) | |
696 | { | |
697 | /* Create the initial chain containing all the passed PCs. */ | |
698 | ||
699 | result = xmalloc (sizeof (*result) + sizeof (*result->call_site) | |
700 | * (length - 1)); | |
701 | result->length = length; | |
702 | result->callers = result->callees = length; | |
703 | memcpy (result->call_site, VEC_address (call_sitep, chain), | |
704 | sizeof (*result->call_site) * length); | |
705 | *resultp = result; | |
706 | ||
707 | if (entry_values_debug) | |
708 | { | |
709 | fprintf_unfiltered (gdb_stdlog, "tailcall: initial:"); | |
710 | for (idx = 0; idx < length; idx++) | |
711 | tailcall_dump (gdbarch, result->call_site[idx]); | |
712 | fputc_unfiltered ('\n', gdb_stdlog); | |
713 | } | |
714 | ||
715 | return; | |
716 | } | |
717 | ||
718 | if (entry_values_debug) | |
719 | { | |
720 | fprintf_unfiltered (gdb_stdlog, "tailcall: compare:"); | |
721 | for (idx = 0; idx < length; idx++) | |
722 | tailcall_dump (gdbarch, VEC_index (call_sitep, chain, idx)); | |
723 | fputc_unfiltered ('\n', gdb_stdlog); | |
724 | } | |
725 | ||
726 | /* Intersect callers. */ | |
727 | ||
728 | callers = min (result->callers, length); | |
729 | for (idx = 0; idx < callers; idx++) | |
730 | if (result->call_site[idx] != VEC_index (call_sitep, chain, idx)) | |
731 | { | |
732 | result->callers = idx; | |
733 | break; | |
734 | } | |
735 | ||
736 | /* Intersect callees. */ | |
737 | ||
738 | callees = min (result->callees, length); | |
739 | for (idx = 0; idx < callees; idx++) | |
740 | if (result->call_site[result->length - 1 - idx] | |
741 | != VEC_index (call_sitep, chain, length - 1 - idx)) | |
742 | { | |
743 | result->callees = idx; | |
744 | break; | |
745 | } | |
746 | ||
747 | if (entry_values_debug) | |
748 | { | |
749 | fprintf_unfiltered (gdb_stdlog, "tailcall: reduced:"); | |
750 | for (idx = 0; idx < result->callers; idx++) | |
751 | tailcall_dump (gdbarch, result->call_site[idx]); | |
752 | fputs_unfiltered (" |", gdb_stdlog); | |
753 | for (idx = 0; idx < result->callees; idx++) | |
754 | tailcall_dump (gdbarch, result->call_site[result->length | |
755 | - result->callees + idx]); | |
756 | fputc_unfiltered ('\n', gdb_stdlog); | |
757 | } | |
758 | ||
759 | if (result->callers == 0 && result->callees == 0) | |
760 | { | |
761 | /* There are no common callers or callees. It could be also a direct | |
762 | call (which has length 0) with ambiguous possibility of an indirect | |
763 | call - CALLERS == CALLEES == 0 is valid during the first allocation | |
764 | but any subsequence processing of such entry means ambiguity. */ | |
765 | xfree (result); | |
766 | *resultp = NULL; | |
767 | return; | |
768 | } | |
769 | ||
770 | /* See call_site_find_chain_1 why there is no way to reach the bottom callee | |
771 | PC again. In such case there must be two different code paths to reach | |
772 | it, therefore some of the former determined intermediate PCs must differ | |
773 | and the unambiguous chain gets shortened. */ | |
774 | gdb_assert (result->callers + result->callees < result->length); | |
775 | } | |
776 | ||
777 | /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the | |
778 | assumed frames between them use GDBARCH. Use depth first search so we can | |
779 | keep single CHAIN of call_site's back to CALLER_PC. Function recursion | |
780 | would have needless GDB stack overhead. Caller is responsible for xfree of | |
781 | the returned result. Any unreliability results in thrown | |
782 | NO_ENTRY_VALUE_ERROR. */ | |
783 | ||
784 | static struct call_site_chain * | |
785 | call_site_find_chain_1 (struct gdbarch *gdbarch, CORE_ADDR caller_pc, | |
786 | CORE_ADDR callee_pc) | |
787 | { | |
111c6489 JK |
788 | struct obstack addr_obstack; |
789 | struct cleanup *back_to_retval, *back_to_workdata; | |
790 | struct call_site_chain *retval = NULL; | |
791 | struct call_site *call_site; | |
792 | ||
793 | /* Mark CALL_SITEs so we do not visit the same ones twice. */ | |
794 | htab_t addr_hash; | |
795 | ||
796 | /* CHAIN contains only the intermediate CALL_SITEs. Neither CALLER_PC's | |
797 | call_site nor any possible call_site at CALLEE_PC's function is there. | |
798 | Any CALL_SITE in CHAIN will be iterated to its siblings - via | |
799 | TAIL_CALL_NEXT. This is inappropriate for CALLER_PC's call_site. */ | |
800 | VEC (call_sitep) *chain = NULL; | |
801 | ||
802 | /* We are not interested in the specific PC inside the callee function. */ | |
803 | callee_pc = get_pc_function_start (callee_pc); | |
804 | if (callee_pc == 0) | |
805 | throw_error (NO_ENTRY_VALUE_ERROR, _("Unable to find function for PC %s"), | |
806 | paddress (gdbarch, callee_pc)); | |
807 | ||
808 | back_to_retval = make_cleanup (free_current_contents, &retval); | |
809 | ||
810 | obstack_init (&addr_obstack); | |
811 | back_to_workdata = make_cleanup_obstack_free (&addr_obstack); | |
812 | addr_hash = htab_create_alloc_ex (64, core_addr_hash, core_addr_eq, NULL, | |
813 | &addr_obstack, hashtab_obstack_allocate, | |
814 | NULL); | |
815 | make_cleanup_htab_delete (addr_hash); | |
816 | ||
817 | make_cleanup (VEC_cleanup (call_sitep), &chain); | |
818 | ||
819 | /* Do not push CALL_SITE to CHAIN. Push there only the first tail call site | |
820 | at the target's function. All the possible tail call sites in the | |
821 | target's function will get iterated as already pushed into CHAIN via their | |
822 | TAIL_CALL_NEXT. */ | |
823 | call_site = call_site_for_pc (gdbarch, caller_pc); | |
824 | ||
825 | while (call_site) | |
826 | { | |
827 | CORE_ADDR target_func_addr; | |
828 | struct call_site *target_call_site; | |
829 | ||
830 | /* CALLER_FRAME with registers is not available for tail-call jumped | |
831 | frames. */ | |
832 | target_func_addr = call_site_to_target_addr (gdbarch, call_site, NULL); | |
833 | ||
834 | if (target_func_addr == callee_pc) | |
835 | { | |
836 | chain_candidate (gdbarch, &retval, chain); | |
837 | if (retval == NULL) | |
838 | break; | |
839 | ||
840 | /* There is no way to reach CALLEE_PC again as we would prevent | |
841 | entering it twice as being already marked in ADDR_HASH. */ | |
842 | target_call_site = NULL; | |
843 | } | |
844 | else | |
845 | { | |
846 | struct symbol *target_func; | |
847 | ||
848 | target_func = func_addr_to_tail_call_list (gdbarch, target_func_addr); | |
849 | target_call_site = TYPE_TAIL_CALL_LIST (SYMBOL_TYPE (target_func)); | |
850 | } | |
851 | ||
852 | do | |
853 | { | |
854 | /* Attempt to visit TARGET_CALL_SITE. */ | |
855 | ||
856 | if (target_call_site) | |
857 | { | |
858 | void **slot; | |
859 | ||
860 | slot = htab_find_slot (addr_hash, &target_call_site->pc, INSERT); | |
861 | if (*slot == NULL) | |
862 | { | |
863 | /* Successfully entered TARGET_CALL_SITE. */ | |
864 | ||
865 | *slot = &target_call_site->pc; | |
866 | VEC_safe_push (call_sitep, chain, target_call_site); | |
867 | break; | |
868 | } | |
869 | } | |
870 | ||
871 | /* Backtrack (without revisiting the originating call_site). Try the | |
872 | callers's sibling; if there isn't any try the callers's callers's | |
873 | sibling etc. */ | |
874 | ||
875 | target_call_site = NULL; | |
876 | while (!VEC_empty (call_sitep, chain)) | |
877 | { | |
878 | call_site = VEC_pop (call_sitep, chain); | |
879 | ||
880 | gdb_assert (htab_find_slot (addr_hash, &call_site->pc, | |
881 | NO_INSERT) != NULL); | |
882 | htab_remove_elt (addr_hash, &call_site->pc); | |
883 | ||
884 | target_call_site = call_site->tail_call_next; | |
885 | if (target_call_site) | |
886 | break; | |
887 | } | |
888 | } | |
889 | while (target_call_site); | |
890 | ||
891 | if (VEC_empty (call_sitep, chain)) | |
892 | call_site = NULL; | |
893 | else | |
894 | call_site = VEC_last (call_sitep, chain); | |
895 | } | |
896 | ||
897 | if (retval == NULL) | |
898 | { | |
899 | struct minimal_symbol *msym_caller, *msym_callee; | |
900 | ||
901 | msym_caller = lookup_minimal_symbol_by_pc (caller_pc); | |
902 | msym_callee = lookup_minimal_symbol_by_pc (callee_pc); | |
903 | throw_error (NO_ENTRY_VALUE_ERROR, | |
904 | _("There are no unambiguously determinable intermediate " | |
905 | "callers or callees between caller function \"%s\" at %s " | |
906 | "and callee function \"%s\" at %s"), | |
907 | (msym_caller == NULL | |
908 | ? "???" : SYMBOL_PRINT_NAME (msym_caller)), | |
909 | paddress (gdbarch, caller_pc), | |
910 | (msym_callee == NULL | |
911 | ? "???" : SYMBOL_PRINT_NAME (msym_callee)), | |
912 | paddress (gdbarch, callee_pc)); | |
913 | } | |
914 | ||
915 | do_cleanups (back_to_workdata); | |
916 | discard_cleanups (back_to_retval); | |
917 | return retval; | |
918 | } | |
919 | ||
920 | /* Create and return call_site_chain for CALLER_PC and CALLEE_PC. All the | |
921 | assumed frames between them use GDBARCH. If valid call_site_chain cannot be | |
922 | constructed return NULL. Caller is responsible for xfree of the returned | |
923 | result. */ | |
924 | ||
925 | struct call_site_chain * | |
926 | call_site_find_chain (struct gdbarch *gdbarch, CORE_ADDR caller_pc, | |
927 | CORE_ADDR callee_pc) | |
928 | { | |
929 | volatile struct gdb_exception e; | |
930 | struct call_site_chain *retval = NULL; | |
931 | ||
932 | TRY_CATCH (e, RETURN_MASK_ERROR) | |
933 | { | |
934 | retval = call_site_find_chain_1 (gdbarch, caller_pc, callee_pc); | |
935 | } | |
936 | if (e.reason < 0) | |
937 | { | |
938 | if (e.error == NO_ENTRY_VALUE_ERROR) | |
939 | { | |
940 | if (entry_values_debug) | |
941 | exception_print (gdb_stdout, e); | |
942 | ||
943 | return NULL; | |
944 | } | |
945 | else | |
946 | throw_exception (e); | |
947 | } | |
948 | return retval; | |
949 | } | |
950 | ||
24c5c679 JK |
951 | /* Return 1 if KIND and KIND_U match PARAMETER. Return 0 otherwise. */ |
952 | ||
953 | static int | |
954 | call_site_parameter_matches (struct call_site_parameter *parameter, | |
955 | enum call_site_parameter_kind kind, | |
956 | union call_site_parameter_u kind_u) | |
957 | { | |
958 | if (kind == parameter->kind) | |
959 | switch (kind) | |
960 | { | |
961 | case CALL_SITE_PARAMETER_DWARF_REG: | |
962 | return kind_u.dwarf_reg == parameter->u.dwarf_reg; | |
963 | case CALL_SITE_PARAMETER_FB_OFFSET: | |
964 | return kind_u.fb_offset == parameter->u.fb_offset; | |
1788b2d3 JK |
965 | case CALL_SITE_PARAMETER_PARAM_OFFSET: |
966 | return kind_u.param_offset.cu_off == parameter->u.param_offset.cu_off; | |
24c5c679 JK |
967 | } |
968 | return 0; | |
969 | } | |
970 | ||
971 | /* Fetch call_site_parameter from caller matching KIND and KIND_U. | |
972 | FRAME is for callee. | |
8e3b41a9 JK |
973 | |
974 | Function always returns non-NULL, it throws NO_ENTRY_VALUE_ERROR | |
975 | otherwise. */ | |
976 | ||
977 | static struct call_site_parameter * | |
24c5c679 JK |
978 | dwarf_expr_reg_to_entry_parameter (struct frame_info *frame, |
979 | enum call_site_parameter_kind kind, | |
980 | union call_site_parameter_u kind_u, | |
8e3b41a9 JK |
981 | struct dwarf2_per_cu_data **per_cu_return) |
982 | { | |
983 | CORE_ADDR func_addr = get_frame_func (frame); | |
984 | CORE_ADDR caller_pc; | |
985 | struct gdbarch *gdbarch = get_frame_arch (frame); | |
986 | struct frame_info *caller_frame = get_prev_frame (frame); | |
987 | struct call_site *call_site; | |
988 | int iparams; | |
509f0fd9 JK |
989 | /* Initialize it just to avoid a GCC false warning. */ |
990 | struct call_site_parameter *parameter = NULL; | |
8e3b41a9 JK |
991 | CORE_ADDR target_addr; |
992 | ||
993 | if (gdbarch != frame_unwind_arch (frame)) | |
994 | { | |
995 | struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr); | |
996 | struct gdbarch *caller_gdbarch = frame_unwind_arch (frame); | |
997 | ||
998 | throw_error (NO_ENTRY_VALUE_ERROR, | |
999 | _("DW_OP_GNU_entry_value resolving callee gdbarch %s " | |
1000 | "(of %s (%s)) does not match caller gdbarch %s"), | |
1001 | gdbarch_bfd_arch_info (gdbarch)->printable_name, | |
1002 | paddress (gdbarch, func_addr), | |
1003 | msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym), | |
1004 | gdbarch_bfd_arch_info (caller_gdbarch)->printable_name); | |
1005 | } | |
1006 | ||
1007 | if (caller_frame == NULL) | |
1008 | { | |
1009 | struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (func_addr); | |
1010 | ||
1011 | throw_error (NO_ENTRY_VALUE_ERROR, _("DW_OP_GNU_entry_value resolving " | |
1012 | "requires caller of %s (%s)"), | |
1013 | paddress (gdbarch, func_addr), | |
1014 | msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym)); | |
1015 | } | |
1016 | caller_pc = get_frame_pc (caller_frame); | |
1017 | call_site = call_site_for_pc (gdbarch, caller_pc); | |
1018 | ||
1019 | target_addr = call_site_to_target_addr (gdbarch, call_site, caller_frame); | |
1020 | if (target_addr != func_addr) | |
1021 | { | |
1022 | struct minimal_symbol *target_msym, *func_msym; | |
1023 | ||
1024 | target_msym = lookup_minimal_symbol_by_pc (target_addr); | |
1025 | func_msym = lookup_minimal_symbol_by_pc (func_addr); | |
1026 | throw_error (NO_ENTRY_VALUE_ERROR, | |
1027 | _("DW_OP_GNU_entry_value resolving expects callee %s at %s " | |
1028 | "but the called frame is for %s at %s"), | |
1029 | (target_msym == NULL ? "???" | |
1030 | : SYMBOL_PRINT_NAME (target_msym)), | |
1031 | paddress (gdbarch, target_addr), | |
1032 | func_msym == NULL ? "???" : SYMBOL_PRINT_NAME (func_msym), | |
1033 | paddress (gdbarch, func_addr)); | |
1034 | } | |
1035 | ||
2d6c5dc2 JK |
1036 | /* No entry value based parameters would be reliable if this function can |
1037 | call itself via tail calls. */ | |
1038 | func_verify_no_selftailcall (gdbarch, func_addr); | |
1039 | ||
8e3b41a9 JK |
1040 | for (iparams = 0; iparams < call_site->parameter_count; iparams++) |
1041 | { | |
1042 | parameter = &call_site->parameter[iparams]; | |
24c5c679 | 1043 | if (call_site_parameter_matches (parameter, kind, kind_u)) |
8e3b41a9 JK |
1044 | break; |
1045 | } | |
1046 | if (iparams == call_site->parameter_count) | |
1047 | { | |
1048 | struct minimal_symbol *msym = lookup_minimal_symbol_by_pc (caller_pc); | |
1049 | ||
1050 | /* DW_TAG_GNU_call_site_parameter will be missing just if GCC could not | |
1051 | determine its value. */ | |
1052 | throw_error (NO_ENTRY_VALUE_ERROR, _("Cannot find matching parameter " | |
1053 | "at DW_TAG_GNU_call_site %s at %s"), | |
1054 | paddress (gdbarch, caller_pc), | |
1055 | msym == NULL ? "???" : SYMBOL_PRINT_NAME (msym)); | |
1056 | } | |
1057 | ||
1058 | *per_cu_return = call_site->per_cu; | |
1059 | return parameter; | |
1060 | } | |
1061 | ||
a471c594 JK |
1062 | /* Return value for PARAMETER matching DEREF_SIZE. If DEREF_SIZE is -1, return |
1063 | the normal DW_AT_GNU_call_site_value block. Otherwise return the | |
1064 | DW_AT_GNU_call_site_data_value (dereferenced) block. | |
e18b2753 JK |
1065 | |
1066 | TYPE and CALLER_FRAME specify how to evaluate the DWARF block into returned | |
1067 | struct value. | |
1068 | ||
1069 | Function always returns non-NULL, non-optimized out value. It throws | |
1070 | NO_ENTRY_VALUE_ERROR if it cannot resolve the value for any reason. */ | |
1071 | ||
1072 | static struct value * | |
1073 | dwarf_entry_parameter_to_value (struct call_site_parameter *parameter, | |
a471c594 | 1074 | CORE_ADDR deref_size, struct type *type, |
e18b2753 JK |
1075 | struct frame_info *caller_frame, |
1076 | struct dwarf2_per_cu_data *per_cu) | |
1077 | { | |
a471c594 | 1078 | const gdb_byte *data_src; |
e18b2753 | 1079 | gdb_byte *data; |
a471c594 JK |
1080 | size_t size; |
1081 | ||
1082 | data_src = deref_size == -1 ? parameter->value : parameter->data_value; | |
1083 | size = deref_size == -1 ? parameter->value_size : parameter->data_value_size; | |
1084 | ||
1085 | /* DEREF_SIZE size is not verified here. */ | |
1086 | if (data_src == NULL) | |
1087 | throw_error (NO_ENTRY_VALUE_ERROR, | |
1088 | _("Cannot resolve DW_AT_GNU_call_site_data_value")); | |
e18b2753 JK |
1089 | |
1090 | /* DW_AT_GNU_call_site_value is a DWARF expression, not a DWARF | |
1091 | location. Postprocessing of DWARF_VALUE_MEMORY would lose the type from | |
1092 | DWARF block. */ | |
a471c594 JK |
1093 | data = alloca (size + 1); |
1094 | memcpy (data, data_src, size); | |
1095 | data[size] = DW_OP_stack_value; | |
e18b2753 | 1096 | |
a471c594 | 1097 | return dwarf2_evaluate_loc_desc (type, caller_frame, data, size + 1, per_cu); |
e18b2753 JK |
1098 | } |
1099 | ||
24c5c679 JK |
1100 | /* Execute DWARF block of call_site_parameter which matches KIND and KIND_U. |
1101 | Choose DEREF_SIZE value of that parameter. Search caller of the CTX's | |
1102 | frame. CTX must be of dwarf_expr_ctx_funcs kind. | |
8e3b41a9 JK |
1103 | |
1104 | The CTX caller can be from a different CU - per_cu_dwarf_call implementation | |
1105 | can be more simple as it does not support cross-CU DWARF executions. */ | |
1106 | ||
1107 | static void | |
1108 | dwarf_expr_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx, | |
24c5c679 JK |
1109 | enum call_site_parameter_kind kind, |
1110 | union call_site_parameter_u kind_u, | |
a471c594 | 1111 | int deref_size) |
8e3b41a9 JK |
1112 | { |
1113 | struct dwarf_expr_baton *debaton; | |
1114 | struct frame_info *frame, *caller_frame; | |
1115 | struct dwarf2_per_cu_data *caller_per_cu; | |
1116 | struct dwarf_expr_baton baton_local; | |
1117 | struct dwarf_expr_context saved_ctx; | |
1118 | struct call_site_parameter *parameter; | |
1119 | const gdb_byte *data_src; | |
1120 | size_t size; | |
1121 | ||
1122 | gdb_assert (ctx->funcs == &dwarf_expr_ctx_funcs); | |
1123 | debaton = ctx->baton; | |
1124 | frame = debaton->frame; | |
1125 | caller_frame = get_prev_frame (frame); | |
1126 | ||
24c5c679 | 1127 | parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u, |
8e3b41a9 | 1128 | &caller_per_cu); |
a471c594 JK |
1129 | data_src = deref_size == -1 ? parameter->value : parameter->data_value; |
1130 | size = deref_size == -1 ? parameter->value_size : parameter->data_value_size; | |
1131 | ||
1132 | /* DEREF_SIZE size is not verified here. */ | |
1133 | if (data_src == NULL) | |
1134 | throw_error (NO_ENTRY_VALUE_ERROR, | |
1135 | _("Cannot resolve DW_AT_GNU_call_site_data_value")); | |
8e3b41a9 JK |
1136 | |
1137 | baton_local.frame = caller_frame; | |
1138 | baton_local.per_cu = caller_per_cu; | |
1139 | ||
1140 | saved_ctx.gdbarch = ctx->gdbarch; | |
1141 | saved_ctx.addr_size = ctx->addr_size; | |
1142 | saved_ctx.offset = ctx->offset; | |
1143 | saved_ctx.baton = ctx->baton; | |
1144 | ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (baton_local.per_cu)); | |
1145 | ctx->addr_size = dwarf2_per_cu_addr_size (baton_local.per_cu); | |
1146 | ctx->offset = dwarf2_per_cu_text_offset (baton_local.per_cu); | |
1147 | ctx->baton = &baton_local; | |
1148 | ||
1149 | dwarf_expr_eval (ctx, data_src, size); | |
1150 | ||
1151 | ctx->gdbarch = saved_ctx.gdbarch; | |
1152 | ctx->addr_size = saved_ctx.addr_size; | |
1153 | ctx->offset = saved_ctx.offset; | |
1154 | ctx->baton = saved_ctx.baton; | |
1155 | } | |
1156 | ||
3019eac3 DE |
1157 | /* Callback function for dwarf2_evaluate_loc_desc. |
1158 | Fetch the address indexed by DW_OP_GNU_addr_index. */ | |
1159 | ||
1160 | static CORE_ADDR | |
1161 | dwarf_expr_get_addr_index (void *baton, unsigned int index) | |
1162 | { | |
1163 | struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton; | |
1164 | ||
1165 | return dwarf2_read_addr_index (debaton->per_cu, index); | |
1166 | } | |
1167 | ||
a471c594 JK |
1168 | /* VALUE must be of type lval_computed with entry_data_value_funcs. Perform |
1169 | the indirect method on it, that is use its stored target value, the sole | |
1170 | purpose of entry_data_value_funcs.. */ | |
1171 | ||
1172 | static struct value * | |
1173 | entry_data_value_coerce_ref (const struct value *value) | |
1174 | { | |
1175 | struct type *checked_type = check_typedef (value_type (value)); | |
1176 | struct value *target_val; | |
1177 | ||
1178 | if (TYPE_CODE (checked_type) != TYPE_CODE_REF) | |
1179 | return NULL; | |
1180 | ||
1181 | target_val = value_computed_closure (value); | |
1182 | value_incref (target_val); | |
1183 | return target_val; | |
1184 | } | |
1185 | ||
1186 | /* Implement copy_closure. */ | |
1187 | ||
1188 | static void * | |
1189 | entry_data_value_copy_closure (const struct value *v) | |
1190 | { | |
1191 | struct value *target_val = value_computed_closure (v); | |
1192 | ||
1193 | value_incref (target_val); | |
1194 | return target_val; | |
1195 | } | |
1196 | ||
1197 | /* Implement free_closure. */ | |
1198 | ||
1199 | static void | |
1200 | entry_data_value_free_closure (struct value *v) | |
1201 | { | |
1202 | struct value *target_val = value_computed_closure (v); | |
1203 | ||
1204 | value_free (target_val); | |
1205 | } | |
1206 | ||
1207 | /* Vector for methods for an entry value reference where the referenced value | |
1208 | is stored in the caller. On the first dereference use | |
1209 | DW_AT_GNU_call_site_data_value in the caller. */ | |
1210 | ||
1211 | static const struct lval_funcs entry_data_value_funcs = | |
1212 | { | |
1213 | NULL, /* read */ | |
1214 | NULL, /* write */ | |
1215 | NULL, /* check_validity */ | |
1216 | NULL, /* check_any_valid */ | |
1217 | NULL, /* indirect */ | |
1218 | entry_data_value_coerce_ref, | |
1219 | NULL, /* check_synthetic_pointer */ | |
1220 | entry_data_value_copy_closure, | |
1221 | entry_data_value_free_closure | |
1222 | }; | |
1223 | ||
24c5c679 JK |
1224 | /* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KIND_U |
1225 | are used to match DW_AT_location at the caller's | |
1226 | DW_TAG_GNU_call_site_parameter. | |
e18b2753 JK |
1227 | |
1228 | Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it | |
1229 | cannot resolve the parameter for any reason. */ | |
1230 | ||
1231 | static struct value * | |
1232 | value_of_dwarf_reg_entry (struct type *type, struct frame_info *frame, | |
24c5c679 JK |
1233 | enum call_site_parameter_kind kind, |
1234 | union call_site_parameter_u kind_u) | |
e18b2753 | 1235 | { |
a471c594 JK |
1236 | struct type *checked_type = check_typedef (type); |
1237 | struct type *target_type = TYPE_TARGET_TYPE (checked_type); | |
e18b2753 | 1238 | struct frame_info *caller_frame = get_prev_frame (frame); |
a471c594 | 1239 | struct value *outer_val, *target_val, *val; |
e18b2753 JK |
1240 | struct call_site_parameter *parameter; |
1241 | struct dwarf2_per_cu_data *caller_per_cu; | |
a471c594 | 1242 | CORE_ADDR addr; |
e18b2753 | 1243 | |
24c5c679 | 1244 | parameter = dwarf_expr_reg_to_entry_parameter (frame, kind, kind_u, |
e18b2753 JK |
1245 | &caller_per_cu); |
1246 | ||
a471c594 JK |
1247 | outer_val = dwarf_entry_parameter_to_value (parameter, -1 /* deref_size */, |
1248 | type, caller_frame, | |
1249 | caller_per_cu); | |
1250 | ||
1251 | /* Check if DW_AT_GNU_call_site_data_value cannot be used. If it should be | |
1252 | used and it is not available do not fall back to OUTER_VAL - dereferencing | |
1253 | TYPE_CODE_REF with non-entry data value would give current value - not the | |
1254 | entry value. */ | |
1255 | ||
1256 | if (TYPE_CODE (checked_type) != TYPE_CODE_REF | |
1257 | || TYPE_TARGET_TYPE (checked_type) == NULL) | |
1258 | return outer_val; | |
1259 | ||
1260 | target_val = dwarf_entry_parameter_to_value (parameter, | |
1261 | TYPE_LENGTH (target_type), | |
1262 | target_type, caller_frame, | |
1263 | caller_per_cu); | |
1264 | ||
1265 | /* value_as_address dereferences TYPE_CODE_REF. */ | |
1266 | addr = extract_typed_address (value_contents (outer_val), checked_type); | |
1267 | ||
1268 | /* The target entry value has artificial address of the entry value | |
1269 | reference. */ | |
1270 | VALUE_LVAL (target_val) = lval_memory; | |
1271 | set_value_address (target_val, addr); | |
1272 | ||
1273 | release_value (target_val); | |
1274 | val = allocate_computed_value (type, &entry_data_value_funcs, | |
1275 | target_val /* closure */); | |
1276 | ||
1277 | /* Copy the referencing pointer to the new computed value. */ | |
1278 | memcpy (value_contents_raw (val), value_contents_raw (outer_val), | |
1279 | TYPE_LENGTH (checked_type)); | |
1280 | set_value_lazy (val, 0); | |
1281 | ||
1282 | return val; | |
e18b2753 JK |
1283 | } |
1284 | ||
1285 | /* Read parameter of TYPE at (callee) FRAME's function entry. DATA and | |
1286 | SIZE are DWARF block used to match DW_AT_location at the caller's | |
1287 | DW_TAG_GNU_call_site_parameter. | |
1288 | ||
1289 | Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR if it | |
1290 | cannot resolve the parameter for any reason. */ | |
1291 | ||
1292 | static struct value * | |
1293 | value_of_dwarf_block_entry (struct type *type, struct frame_info *frame, | |
1294 | const gdb_byte *block, size_t block_len) | |
1295 | { | |
24c5c679 | 1296 | union call_site_parameter_u kind_u; |
e18b2753 | 1297 | |
24c5c679 JK |
1298 | kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (block, block + block_len); |
1299 | if (kind_u.dwarf_reg != -1) | |
1300 | return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_DWARF_REG, | |
1301 | kind_u); | |
e18b2753 | 1302 | |
24c5c679 JK |
1303 | if (dwarf_block_to_fb_offset (block, block + block_len, &kind_u.fb_offset)) |
1304 | return value_of_dwarf_reg_entry (type, frame, CALL_SITE_PARAMETER_FB_OFFSET, | |
1305 | kind_u); | |
e18b2753 JK |
1306 | |
1307 | /* This can normally happen - throw NO_ENTRY_VALUE_ERROR to get the message | |
1308 | suppressed during normal operation. The expression can be arbitrary if | |
1309 | there is no caller-callee entry value binding expected. */ | |
1310 | throw_error (NO_ENTRY_VALUE_ERROR, | |
1311 | _("DWARF-2 expression error: DW_OP_GNU_entry_value is supported " | |
1312 | "only for single DW_OP_reg* or for DW_OP_fbreg(*)")); | |
1313 | } | |
1314 | ||
052b9502 NF |
1315 | struct piece_closure |
1316 | { | |
88bfdde4 TT |
1317 | /* Reference count. */ |
1318 | int refc; | |
1319 | ||
8cf6f0b1 TT |
1320 | /* The CU from which this closure's expression came. */ |
1321 | struct dwarf2_per_cu_data *per_cu; | |
1322 | ||
052b9502 NF |
1323 | /* The number of pieces used to describe this variable. */ |
1324 | int n_pieces; | |
1325 | ||
6063c216 UW |
1326 | /* The target address size, used only for DWARF_VALUE_STACK. */ |
1327 | int addr_size; | |
cec03d70 | 1328 | |
052b9502 NF |
1329 | /* The pieces themselves. */ |
1330 | struct dwarf_expr_piece *pieces; | |
1331 | }; | |
1332 | ||
1333 | /* Allocate a closure for a value formed from separately-described | |
1334 | PIECES. */ | |
1335 | ||
1336 | static struct piece_closure * | |
8cf6f0b1 TT |
1337 | allocate_piece_closure (struct dwarf2_per_cu_data *per_cu, |
1338 | int n_pieces, struct dwarf_expr_piece *pieces, | |
6063c216 | 1339 | int addr_size) |
052b9502 NF |
1340 | { |
1341 | struct piece_closure *c = XZALLOC (struct piece_closure); | |
8a9b8146 | 1342 | int i; |
052b9502 | 1343 | |
88bfdde4 | 1344 | c->refc = 1; |
8cf6f0b1 | 1345 | c->per_cu = per_cu; |
052b9502 | 1346 | c->n_pieces = n_pieces; |
6063c216 | 1347 | c->addr_size = addr_size; |
052b9502 NF |
1348 | c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece); |
1349 | ||
1350 | memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece)); | |
8a9b8146 TT |
1351 | for (i = 0; i < n_pieces; ++i) |
1352 | if (c->pieces[i].location == DWARF_VALUE_STACK) | |
1353 | value_incref (c->pieces[i].v.value); | |
052b9502 NF |
1354 | |
1355 | return c; | |
1356 | } | |
1357 | ||
d3b1e874 TT |
1358 | /* The lowest-level function to extract bits from a byte buffer. |
1359 | SOURCE is the buffer. It is updated if we read to the end of a | |
1360 | byte. | |
1361 | SOURCE_OFFSET_BITS is the offset of the first bit to read. It is | |
1362 | updated to reflect the number of bits actually read. | |
1363 | NBITS is the number of bits we want to read. It is updated to | |
1364 | reflect the number of bits actually read. This function may read | |
1365 | fewer bits. | |
1366 | BITS_BIG_ENDIAN is taken directly from gdbarch. | |
1367 | This function returns the extracted bits. */ | |
1368 | ||
1369 | static unsigned int | |
1370 | extract_bits_primitive (const gdb_byte **source, | |
1371 | unsigned int *source_offset_bits, | |
1372 | int *nbits, int bits_big_endian) | |
1373 | { | |
1374 | unsigned int avail, mask, datum; | |
1375 | ||
1376 | gdb_assert (*source_offset_bits < 8); | |
1377 | ||
1378 | avail = 8 - *source_offset_bits; | |
1379 | if (avail > *nbits) | |
1380 | avail = *nbits; | |
1381 | ||
1382 | mask = (1 << avail) - 1; | |
1383 | datum = **source; | |
1384 | if (bits_big_endian) | |
1385 | datum >>= 8 - (*source_offset_bits + *nbits); | |
1386 | else | |
1387 | datum >>= *source_offset_bits; | |
1388 | datum &= mask; | |
1389 | ||
1390 | *nbits -= avail; | |
1391 | *source_offset_bits += avail; | |
1392 | if (*source_offset_bits >= 8) | |
1393 | { | |
1394 | *source_offset_bits -= 8; | |
1395 | ++*source; | |
1396 | } | |
1397 | ||
1398 | return datum; | |
1399 | } | |
1400 | ||
1401 | /* Extract some bits from a source buffer and move forward in the | |
1402 | buffer. | |
1403 | ||
1404 | SOURCE is the source buffer. It is updated as bytes are read. | |
1405 | SOURCE_OFFSET_BITS is the offset into SOURCE. It is updated as | |
1406 | bits are read. | |
1407 | NBITS is the number of bits to read. | |
1408 | BITS_BIG_ENDIAN is taken directly from gdbarch. | |
1409 | ||
1410 | This function returns the bits that were read. */ | |
1411 | ||
1412 | static unsigned int | |
1413 | extract_bits (const gdb_byte **source, unsigned int *source_offset_bits, | |
1414 | int nbits, int bits_big_endian) | |
1415 | { | |
1416 | unsigned int datum; | |
1417 | ||
1418 | gdb_assert (nbits > 0 && nbits <= 8); | |
1419 | ||
1420 | datum = extract_bits_primitive (source, source_offset_bits, &nbits, | |
1421 | bits_big_endian); | |
1422 | if (nbits > 0) | |
1423 | { | |
1424 | unsigned int more; | |
1425 | ||
1426 | more = extract_bits_primitive (source, source_offset_bits, &nbits, | |
1427 | bits_big_endian); | |
1428 | if (bits_big_endian) | |
1429 | datum <<= nbits; | |
1430 | else | |
1431 | more <<= nbits; | |
1432 | datum |= more; | |
1433 | } | |
1434 | ||
1435 | return datum; | |
1436 | } | |
1437 | ||
1438 | /* Write some bits into a buffer and move forward in the buffer. | |
1439 | ||
1440 | DATUM is the bits to write. The low-order bits of DATUM are used. | |
1441 | DEST is the destination buffer. It is updated as bytes are | |
1442 | written. | |
1443 | DEST_OFFSET_BITS is the bit offset in DEST at which writing is | |
1444 | done. | |
1445 | NBITS is the number of valid bits in DATUM. | |
1446 | BITS_BIG_ENDIAN is taken directly from gdbarch. */ | |
1447 | ||
1448 | static void | |
1449 | insert_bits (unsigned int datum, | |
1450 | gdb_byte *dest, unsigned int dest_offset_bits, | |
1451 | int nbits, int bits_big_endian) | |
1452 | { | |
1453 | unsigned int mask; | |
1454 | ||
8c814cdd | 1455 | gdb_assert (dest_offset_bits + nbits <= 8); |
d3b1e874 TT |
1456 | |
1457 | mask = (1 << nbits) - 1; | |
1458 | if (bits_big_endian) | |
1459 | { | |
1460 | datum <<= 8 - (dest_offset_bits + nbits); | |
1461 | mask <<= 8 - (dest_offset_bits + nbits); | |
1462 | } | |
1463 | else | |
1464 | { | |
1465 | datum <<= dest_offset_bits; | |
1466 | mask <<= dest_offset_bits; | |
1467 | } | |
1468 | ||
1469 | gdb_assert ((datum & ~mask) == 0); | |
1470 | ||
1471 | *dest = (*dest & ~mask) | datum; | |
1472 | } | |
1473 | ||
1474 | /* Copy bits from a source to a destination. | |
1475 | ||
1476 | DEST is where the bits should be written. | |
1477 | DEST_OFFSET_BITS is the bit offset into DEST. | |
1478 | SOURCE is the source of bits. | |
1479 | SOURCE_OFFSET_BITS is the bit offset into SOURCE. | |
1480 | BIT_COUNT is the number of bits to copy. | |
1481 | BITS_BIG_ENDIAN is taken directly from gdbarch. */ | |
1482 | ||
1483 | static void | |
1484 | copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits, | |
1485 | const gdb_byte *source, unsigned int source_offset_bits, | |
1486 | unsigned int bit_count, | |
1487 | int bits_big_endian) | |
1488 | { | |
1489 | unsigned int dest_avail; | |
1490 | int datum; | |
1491 | ||
1492 | /* Reduce everything to byte-size pieces. */ | |
1493 | dest += dest_offset_bits / 8; | |
1494 | dest_offset_bits %= 8; | |
1495 | source += source_offset_bits / 8; | |
1496 | source_offset_bits %= 8; | |
1497 | ||
1498 | dest_avail = 8 - dest_offset_bits % 8; | |
1499 | ||
1500 | /* See if we can fill the first destination byte. */ | |
1501 | if (dest_avail < bit_count) | |
1502 | { | |
1503 | datum = extract_bits (&source, &source_offset_bits, dest_avail, | |
1504 | bits_big_endian); | |
1505 | insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian); | |
1506 | ++dest; | |
1507 | dest_offset_bits = 0; | |
1508 | bit_count -= dest_avail; | |
1509 | } | |
1510 | ||
1511 | /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer | |
1512 | than 8 bits remaining. */ | |
1513 | gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8); | |
1514 | for (; bit_count >= 8; bit_count -= 8) | |
1515 | { | |
1516 | datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian); | |
1517 | *dest++ = (gdb_byte) datum; | |
1518 | } | |
1519 | ||
1520 | /* Finally, we may have a few leftover bits. */ | |
1521 | gdb_assert (bit_count <= 8 - dest_offset_bits % 8); | |
1522 | if (bit_count > 0) | |
1523 | { | |
1524 | datum = extract_bits (&source, &source_offset_bits, bit_count, | |
1525 | bits_big_endian); | |
1526 | insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian); | |
1527 | } | |
1528 | } | |
1529 | ||
052b9502 NF |
1530 | static void |
1531 | read_pieced_value (struct value *v) | |
1532 | { | |
1533 | int i; | |
1534 | long offset = 0; | |
d3b1e874 | 1535 | ULONGEST bits_to_skip; |
052b9502 | 1536 | gdb_byte *contents; |
3e43a32a MS |
1537 | struct piece_closure *c |
1538 | = (struct piece_closure *) value_computed_closure (v); | |
052b9502 | 1539 | struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v)); |
afd74c5f | 1540 | size_t type_len; |
d3b1e874 TT |
1541 | size_t buffer_size = 0; |
1542 | char *buffer = NULL; | |
1543 | struct cleanup *cleanup; | |
1544 | int bits_big_endian | |
1545 | = gdbarch_bits_big_endian (get_type_arch (value_type (v))); | |
afd74c5f TT |
1546 | |
1547 | if (value_type (v) != value_enclosing_type (v)) | |
1548 | internal_error (__FILE__, __LINE__, | |
1549 | _("Should not be able to create a lazy value with " | |
1550 | "an enclosing type")); | |
052b9502 | 1551 | |
d3b1e874 TT |
1552 | cleanup = make_cleanup (free_current_contents, &buffer); |
1553 | ||
052b9502 | 1554 | contents = value_contents_raw (v); |
d3b1e874 | 1555 | bits_to_skip = 8 * value_offset (v); |
0e03807e TT |
1556 | if (value_bitsize (v)) |
1557 | { | |
1558 | bits_to_skip += value_bitpos (v); | |
1559 | type_len = value_bitsize (v); | |
1560 | } | |
1561 | else | |
1562 | type_len = 8 * TYPE_LENGTH (value_type (v)); | |
d3b1e874 | 1563 | |
afd74c5f | 1564 | for (i = 0; i < c->n_pieces && offset < type_len; i++) |
052b9502 NF |
1565 | { |
1566 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
d3b1e874 TT |
1567 | size_t this_size, this_size_bits; |
1568 | long dest_offset_bits, source_offset_bits, source_offset; | |
0d45f56e | 1569 | const gdb_byte *intermediate_buffer; |
d3b1e874 TT |
1570 | |
1571 | /* Compute size, source, and destination offsets for copying, in | |
1572 | bits. */ | |
1573 | this_size_bits = p->size; | |
1574 | if (bits_to_skip > 0 && bits_to_skip >= this_size_bits) | |
afd74c5f | 1575 | { |
d3b1e874 | 1576 | bits_to_skip -= this_size_bits; |
afd74c5f TT |
1577 | continue; |
1578 | } | |
d3b1e874 TT |
1579 | if (this_size_bits > type_len - offset) |
1580 | this_size_bits = type_len - offset; | |
1581 | if (bits_to_skip > 0) | |
afd74c5f | 1582 | { |
d3b1e874 TT |
1583 | dest_offset_bits = 0; |
1584 | source_offset_bits = bits_to_skip; | |
1585 | this_size_bits -= bits_to_skip; | |
1586 | bits_to_skip = 0; | |
afd74c5f TT |
1587 | } |
1588 | else | |
1589 | { | |
d3b1e874 TT |
1590 | dest_offset_bits = offset; |
1591 | source_offset_bits = 0; | |
afd74c5f | 1592 | } |
9a619af0 | 1593 | |
d3b1e874 TT |
1594 | this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8; |
1595 | source_offset = source_offset_bits / 8; | |
1596 | if (buffer_size < this_size) | |
1597 | { | |
1598 | buffer_size = this_size; | |
1599 | buffer = xrealloc (buffer, buffer_size); | |
1600 | } | |
1601 | intermediate_buffer = buffer; | |
1602 | ||
1603 | /* Copy from the source to DEST_BUFFER. */ | |
cec03d70 | 1604 | switch (p->location) |
052b9502 | 1605 | { |
cec03d70 TT |
1606 | case DWARF_VALUE_REGISTER: |
1607 | { | |
1608 | struct gdbarch *arch = get_frame_arch (frame); | |
8a9b8146 | 1609 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno); |
afd74c5f | 1610 | int reg_offset = source_offset; |
dcbf108f UW |
1611 | |
1612 | if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG | |
afd74c5f | 1613 | && this_size < register_size (arch, gdb_regnum)) |
d3b1e874 TT |
1614 | { |
1615 | /* Big-endian, and we want less than full size. */ | |
1616 | reg_offset = register_size (arch, gdb_regnum) - this_size; | |
1617 | /* We want the lower-order THIS_SIZE_BITS of the bytes | |
1618 | we extract from the register. */ | |
1619 | source_offset_bits += 8 * this_size - this_size_bits; | |
1620 | } | |
dcbf108f | 1621 | |
63b4f126 MGD |
1622 | if (gdb_regnum != -1) |
1623 | { | |
8dccd430 PA |
1624 | int optim, unavail; |
1625 | ||
1626 | if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset, | |
1627 | this_size, buffer, | |
1628 | &optim, &unavail)) | |
1629 | { | |
1630 | /* Just so garbage doesn't ever shine through. */ | |
1631 | memset (buffer, 0, this_size); | |
1632 | ||
1633 | if (optim) | |
1634 | set_value_optimized_out (v, 1); | |
1635 | if (unavail) | |
1636 | mark_value_bytes_unavailable (v, offset, this_size); | |
1637 | } | |
63b4f126 MGD |
1638 | } |
1639 | else | |
1640 | { | |
1641 | error (_("Unable to access DWARF register number %s"), | |
8a9b8146 | 1642 | paddress (arch, p->v.regno)); |
63b4f126 | 1643 | } |
cec03d70 TT |
1644 | } |
1645 | break; | |
1646 | ||
1647 | case DWARF_VALUE_MEMORY: | |
e6ca34fc PA |
1648 | read_value_memory (v, offset, |
1649 | p->v.mem.in_stack_memory, | |
1650 | p->v.mem.addr + source_offset, | |
1651 | buffer, this_size); | |
cec03d70 TT |
1652 | break; |
1653 | ||
1654 | case DWARF_VALUE_STACK: | |
1655 | { | |
afd74c5f | 1656 | size_t n = this_size; |
9a619af0 | 1657 | |
afd74c5f TT |
1658 | if (n > c->addr_size - source_offset) |
1659 | n = (c->addr_size >= source_offset | |
1660 | ? c->addr_size - source_offset | |
1661 | : 0); | |
1662 | if (n == 0) | |
1663 | { | |
1664 | /* Nothing. */ | |
1665 | } | |
afd74c5f TT |
1666 | else |
1667 | { | |
8a9b8146 | 1668 | const gdb_byte *val_bytes = value_contents_all (p->v.value); |
afd74c5f | 1669 | |
8a9b8146 | 1670 | intermediate_buffer = val_bytes + source_offset; |
afd74c5f | 1671 | } |
cec03d70 TT |
1672 | } |
1673 | break; | |
1674 | ||
1675 | case DWARF_VALUE_LITERAL: | |
1676 | { | |
afd74c5f TT |
1677 | size_t n = this_size; |
1678 | ||
1679 | if (n > p->v.literal.length - source_offset) | |
1680 | n = (p->v.literal.length >= source_offset | |
1681 | ? p->v.literal.length - source_offset | |
1682 | : 0); | |
1683 | if (n != 0) | |
d3b1e874 | 1684 | intermediate_buffer = p->v.literal.data + source_offset; |
cec03d70 TT |
1685 | } |
1686 | break; | |
1687 | ||
8cf6f0b1 TT |
1688 | /* These bits show up as zeros -- but do not cause the value |
1689 | to be considered optimized-out. */ | |
1690 | case DWARF_VALUE_IMPLICIT_POINTER: | |
1691 | break; | |
1692 | ||
cb826367 | 1693 | case DWARF_VALUE_OPTIMIZED_OUT: |
0e03807e | 1694 | set_value_optimized_out (v, 1); |
cb826367 TT |
1695 | break; |
1696 | ||
cec03d70 TT |
1697 | default: |
1698 | internal_error (__FILE__, __LINE__, _("invalid location type")); | |
052b9502 | 1699 | } |
d3b1e874 | 1700 | |
8cf6f0b1 TT |
1701 | if (p->location != DWARF_VALUE_OPTIMIZED_OUT |
1702 | && p->location != DWARF_VALUE_IMPLICIT_POINTER) | |
d3b1e874 TT |
1703 | copy_bitwise (contents, dest_offset_bits, |
1704 | intermediate_buffer, source_offset_bits % 8, | |
1705 | this_size_bits, bits_big_endian); | |
1706 | ||
1707 | offset += this_size_bits; | |
052b9502 | 1708 | } |
d3b1e874 TT |
1709 | |
1710 | do_cleanups (cleanup); | |
052b9502 NF |
1711 | } |
1712 | ||
1713 | static void | |
1714 | write_pieced_value (struct value *to, struct value *from) | |
1715 | { | |
1716 | int i; | |
1717 | long offset = 0; | |
d3b1e874 | 1718 | ULONGEST bits_to_skip; |
afd74c5f | 1719 | const gdb_byte *contents; |
3e43a32a MS |
1720 | struct piece_closure *c |
1721 | = (struct piece_closure *) value_computed_closure (to); | |
052b9502 | 1722 | struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to)); |
afd74c5f | 1723 | size_t type_len; |
d3b1e874 TT |
1724 | size_t buffer_size = 0; |
1725 | char *buffer = NULL; | |
1726 | struct cleanup *cleanup; | |
1727 | int bits_big_endian | |
1728 | = gdbarch_bits_big_endian (get_type_arch (value_type (to))); | |
052b9502 NF |
1729 | |
1730 | if (frame == NULL) | |
1731 | { | |
1732 | set_value_optimized_out (to, 1); | |
1733 | return; | |
1734 | } | |
1735 | ||
d3b1e874 TT |
1736 | cleanup = make_cleanup (free_current_contents, &buffer); |
1737 | ||
afd74c5f | 1738 | contents = value_contents (from); |
d3b1e874 | 1739 | bits_to_skip = 8 * value_offset (to); |
0e03807e TT |
1740 | if (value_bitsize (to)) |
1741 | { | |
1742 | bits_to_skip += value_bitpos (to); | |
1743 | type_len = value_bitsize (to); | |
1744 | } | |
1745 | else | |
1746 | type_len = 8 * TYPE_LENGTH (value_type (to)); | |
1747 | ||
afd74c5f | 1748 | for (i = 0; i < c->n_pieces && offset < type_len; i++) |
052b9502 NF |
1749 | { |
1750 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
d3b1e874 TT |
1751 | size_t this_size_bits, this_size; |
1752 | long dest_offset_bits, source_offset_bits, dest_offset, source_offset; | |
1753 | int need_bitwise; | |
1754 | const gdb_byte *source_buffer; | |
afd74c5f | 1755 | |
d3b1e874 TT |
1756 | this_size_bits = p->size; |
1757 | if (bits_to_skip > 0 && bits_to_skip >= this_size_bits) | |
afd74c5f | 1758 | { |
d3b1e874 | 1759 | bits_to_skip -= this_size_bits; |
afd74c5f TT |
1760 | continue; |
1761 | } | |
d3b1e874 TT |
1762 | if (this_size_bits > type_len - offset) |
1763 | this_size_bits = type_len - offset; | |
1764 | if (bits_to_skip > 0) | |
afd74c5f | 1765 | { |
d3b1e874 TT |
1766 | dest_offset_bits = bits_to_skip; |
1767 | source_offset_bits = 0; | |
1768 | this_size_bits -= bits_to_skip; | |
1769 | bits_to_skip = 0; | |
afd74c5f TT |
1770 | } |
1771 | else | |
1772 | { | |
d3b1e874 TT |
1773 | dest_offset_bits = 0; |
1774 | source_offset_bits = offset; | |
1775 | } | |
1776 | ||
1777 | this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8; | |
1778 | source_offset = source_offset_bits / 8; | |
1779 | dest_offset = dest_offset_bits / 8; | |
1780 | if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0) | |
1781 | { | |
1782 | source_buffer = contents + source_offset; | |
1783 | need_bitwise = 0; | |
1784 | } | |
1785 | else | |
1786 | { | |
1787 | if (buffer_size < this_size) | |
1788 | { | |
1789 | buffer_size = this_size; | |
1790 | buffer = xrealloc (buffer, buffer_size); | |
1791 | } | |
1792 | source_buffer = buffer; | |
1793 | need_bitwise = 1; | |
afd74c5f | 1794 | } |
9a619af0 | 1795 | |
cec03d70 | 1796 | switch (p->location) |
052b9502 | 1797 | { |
cec03d70 TT |
1798 | case DWARF_VALUE_REGISTER: |
1799 | { | |
1800 | struct gdbarch *arch = get_frame_arch (frame); | |
8a9b8146 | 1801 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.regno); |
afd74c5f | 1802 | int reg_offset = dest_offset; |
dcbf108f UW |
1803 | |
1804 | if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG | |
afd74c5f | 1805 | && this_size <= register_size (arch, gdb_regnum)) |
dcbf108f | 1806 | /* Big-endian, and we want less than full size. */ |
afd74c5f | 1807 | reg_offset = register_size (arch, gdb_regnum) - this_size; |
dcbf108f | 1808 | |
63b4f126 MGD |
1809 | if (gdb_regnum != -1) |
1810 | { | |
d3b1e874 TT |
1811 | if (need_bitwise) |
1812 | { | |
8dccd430 PA |
1813 | int optim, unavail; |
1814 | ||
1815 | if (!get_frame_register_bytes (frame, gdb_regnum, reg_offset, | |
1816 | this_size, buffer, | |
1817 | &optim, &unavail)) | |
1818 | { | |
1819 | if (optim) | |
1820 | error (_("Can't do read-modify-write to " | |
1821 | "update bitfield; containing word has been " | |
1822 | "optimized out")); | |
1823 | if (unavail) | |
1824 | throw_error (NOT_AVAILABLE_ERROR, | |
1825 | _("Can't do read-modify-write to update " | |
1826 | "bitfield; containing word " | |
1827 | "is unavailable")); | |
1828 | } | |
d3b1e874 TT |
1829 | copy_bitwise (buffer, dest_offset_bits, |
1830 | contents, source_offset_bits, | |
1831 | this_size_bits, | |
1832 | bits_big_endian); | |
1833 | } | |
1834 | ||
63b4f126 | 1835 | put_frame_register_bytes (frame, gdb_regnum, reg_offset, |
d3b1e874 | 1836 | this_size, source_buffer); |
63b4f126 MGD |
1837 | } |
1838 | else | |
1839 | { | |
1840 | error (_("Unable to write to DWARF register number %s"), | |
8a9b8146 | 1841 | paddress (arch, p->v.regno)); |
63b4f126 | 1842 | } |
cec03d70 TT |
1843 | } |
1844 | break; | |
1845 | case DWARF_VALUE_MEMORY: | |
d3b1e874 TT |
1846 | if (need_bitwise) |
1847 | { | |
1848 | /* Only the first and last bytes can possibly have any | |
1849 | bits reused. */ | |
f2c7657e UW |
1850 | read_memory (p->v.mem.addr + dest_offset, buffer, 1); |
1851 | read_memory (p->v.mem.addr + dest_offset + this_size - 1, | |
d3b1e874 TT |
1852 | buffer + this_size - 1, 1); |
1853 | copy_bitwise (buffer, dest_offset_bits, | |
1854 | contents, source_offset_bits, | |
1855 | this_size_bits, | |
1856 | bits_big_endian); | |
1857 | } | |
1858 | ||
f2c7657e | 1859 | write_memory (p->v.mem.addr + dest_offset, |
d3b1e874 | 1860 | source_buffer, this_size); |
cec03d70 TT |
1861 | break; |
1862 | default: | |
1863 | set_value_optimized_out (to, 1); | |
0e03807e | 1864 | break; |
052b9502 | 1865 | } |
d3b1e874 | 1866 | offset += this_size_bits; |
052b9502 | 1867 | } |
d3b1e874 | 1868 | |
d3b1e874 | 1869 | do_cleanups (cleanup); |
052b9502 NF |
1870 | } |
1871 | ||
8cf6f0b1 TT |
1872 | /* A helper function that checks bit validity in a pieced value. |
1873 | CHECK_FOR indicates the kind of validity checking. | |
1874 | DWARF_VALUE_MEMORY means to check whether any bit is valid. | |
1875 | DWARF_VALUE_OPTIMIZED_OUT means to check whether any bit is | |
1876 | optimized out. | |
1877 | DWARF_VALUE_IMPLICIT_POINTER means to check whether the bits are an | |
1878 | implicit pointer. */ | |
1879 | ||
0e03807e TT |
1880 | static int |
1881 | check_pieced_value_bits (const struct value *value, int bit_offset, | |
8cf6f0b1 TT |
1882 | int bit_length, |
1883 | enum dwarf_value_location check_for) | |
0e03807e TT |
1884 | { |
1885 | struct piece_closure *c | |
1886 | = (struct piece_closure *) value_computed_closure (value); | |
1887 | int i; | |
8cf6f0b1 TT |
1888 | int validity = (check_for == DWARF_VALUE_MEMORY |
1889 | || check_for == DWARF_VALUE_IMPLICIT_POINTER); | |
0e03807e TT |
1890 | |
1891 | bit_offset += 8 * value_offset (value); | |
1892 | if (value_bitsize (value)) | |
1893 | bit_offset += value_bitpos (value); | |
1894 | ||
1895 | for (i = 0; i < c->n_pieces && bit_length > 0; i++) | |
1896 | { | |
1897 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
1898 | size_t this_size_bits = p->size; | |
1899 | ||
1900 | if (bit_offset > 0) | |
1901 | { | |
1902 | if (bit_offset >= this_size_bits) | |
1903 | { | |
1904 | bit_offset -= this_size_bits; | |
1905 | continue; | |
1906 | } | |
1907 | ||
1908 | bit_length -= this_size_bits - bit_offset; | |
1909 | bit_offset = 0; | |
1910 | } | |
1911 | else | |
1912 | bit_length -= this_size_bits; | |
1913 | ||
8cf6f0b1 TT |
1914 | if (check_for == DWARF_VALUE_IMPLICIT_POINTER) |
1915 | { | |
1916 | if (p->location != DWARF_VALUE_IMPLICIT_POINTER) | |
1917 | return 0; | |
1918 | } | |
1919 | else if (p->location == DWARF_VALUE_OPTIMIZED_OUT | |
1920 | || p->location == DWARF_VALUE_IMPLICIT_POINTER) | |
0e03807e TT |
1921 | { |
1922 | if (validity) | |
1923 | return 0; | |
1924 | } | |
1925 | else | |
1926 | { | |
1927 | if (!validity) | |
1928 | return 1; | |
1929 | } | |
1930 | } | |
1931 | ||
1932 | return validity; | |
1933 | } | |
1934 | ||
1935 | static int | |
1936 | check_pieced_value_validity (const struct value *value, int bit_offset, | |
1937 | int bit_length) | |
1938 | { | |
8cf6f0b1 TT |
1939 | return check_pieced_value_bits (value, bit_offset, bit_length, |
1940 | DWARF_VALUE_MEMORY); | |
0e03807e TT |
1941 | } |
1942 | ||
1943 | static int | |
1944 | check_pieced_value_invalid (const struct value *value) | |
1945 | { | |
1946 | return check_pieced_value_bits (value, 0, | |
8cf6f0b1 TT |
1947 | 8 * TYPE_LENGTH (value_type (value)), |
1948 | DWARF_VALUE_OPTIMIZED_OUT); | |
1949 | } | |
1950 | ||
1951 | /* An implementation of an lval_funcs method to see whether a value is | |
1952 | a synthetic pointer. */ | |
1953 | ||
1954 | static int | |
1955 | check_pieced_synthetic_pointer (const struct value *value, int bit_offset, | |
1956 | int bit_length) | |
1957 | { | |
1958 | return check_pieced_value_bits (value, bit_offset, bit_length, | |
1959 | DWARF_VALUE_IMPLICIT_POINTER); | |
1960 | } | |
1961 | ||
1962 | /* A wrapper function for get_frame_address_in_block. */ | |
1963 | ||
1964 | static CORE_ADDR | |
1965 | get_frame_address_in_block_wrapper (void *baton) | |
1966 | { | |
1967 | return get_frame_address_in_block (baton); | |
1968 | } | |
1969 | ||
1970 | /* An implementation of an lval_funcs method to indirect through a | |
1971 | pointer. This handles the synthetic pointer case when needed. */ | |
1972 | ||
1973 | static struct value * | |
1974 | indirect_pieced_value (struct value *value) | |
1975 | { | |
1976 | struct piece_closure *c | |
1977 | = (struct piece_closure *) value_computed_closure (value); | |
1978 | struct type *type; | |
1979 | struct frame_info *frame; | |
1980 | struct dwarf2_locexpr_baton baton; | |
1981 | int i, bit_offset, bit_length; | |
1982 | struct dwarf_expr_piece *piece = NULL; | |
8cf6f0b1 TT |
1983 | LONGEST byte_offset; |
1984 | ||
0e37a63c | 1985 | type = check_typedef (value_type (value)); |
8cf6f0b1 TT |
1986 | if (TYPE_CODE (type) != TYPE_CODE_PTR) |
1987 | return NULL; | |
1988 | ||
1989 | bit_length = 8 * TYPE_LENGTH (type); | |
1990 | bit_offset = 8 * value_offset (value); | |
1991 | if (value_bitsize (value)) | |
1992 | bit_offset += value_bitpos (value); | |
1993 | ||
1994 | for (i = 0; i < c->n_pieces && bit_length > 0; i++) | |
1995 | { | |
1996 | struct dwarf_expr_piece *p = &c->pieces[i]; | |
1997 | size_t this_size_bits = p->size; | |
1998 | ||
1999 | if (bit_offset > 0) | |
2000 | { | |
2001 | if (bit_offset >= this_size_bits) | |
2002 | { | |
2003 | bit_offset -= this_size_bits; | |
2004 | continue; | |
2005 | } | |
2006 | ||
2007 | bit_length -= this_size_bits - bit_offset; | |
2008 | bit_offset = 0; | |
2009 | } | |
2010 | else | |
2011 | bit_length -= this_size_bits; | |
2012 | ||
2013 | if (p->location != DWARF_VALUE_IMPLICIT_POINTER) | |
2014 | return NULL; | |
2015 | ||
2016 | if (bit_length != 0) | |
2017 | error (_("Invalid use of DW_OP_GNU_implicit_pointer")); | |
2018 | ||
2019 | piece = p; | |
2020 | break; | |
2021 | } | |
2022 | ||
2023 | frame = get_selected_frame (_("No frame selected.")); | |
543305c9 JK |
2024 | |
2025 | /* This is an offset requested by GDB, such as value subcripts. */ | |
8cf6f0b1 TT |
2026 | byte_offset = value_as_address (value); |
2027 | ||
e0e40094 | 2028 | gdb_assert (piece); |
8cf6f0b1 TT |
2029 | baton = dwarf2_fetch_die_location_block (piece->v.ptr.die, c->per_cu, |
2030 | get_frame_address_in_block_wrapper, | |
2031 | frame); | |
2032 | ||
d83e736b JK |
2033 | return dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame, |
2034 | baton.data, baton.size, baton.per_cu, | |
2035 | piece->v.ptr.offset + byte_offset); | |
0e03807e TT |
2036 | } |
2037 | ||
052b9502 | 2038 | static void * |
0e03807e | 2039 | copy_pieced_value_closure (const struct value *v) |
052b9502 | 2040 | { |
3e43a32a MS |
2041 | struct piece_closure *c |
2042 | = (struct piece_closure *) value_computed_closure (v); | |
052b9502 | 2043 | |
88bfdde4 TT |
2044 | ++c->refc; |
2045 | return c; | |
052b9502 NF |
2046 | } |
2047 | ||
2048 | static void | |
2049 | free_pieced_value_closure (struct value *v) | |
2050 | { | |
3e43a32a MS |
2051 | struct piece_closure *c |
2052 | = (struct piece_closure *) value_computed_closure (v); | |
052b9502 | 2053 | |
88bfdde4 TT |
2054 | --c->refc; |
2055 | if (c->refc == 0) | |
2056 | { | |
8a9b8146 TT |
2057 | int i; |
2058 | ||
2059 | for (i = 0; i < c->n_pieces; ++i) | |
2060 | if (c->pieces[i].location == DWARF_VALUE_STACK) | |
2061 | value_free (c->pieces[i].v.value); | |
2062 | ||
88bfdde4 TT |
2063 | xfree (c->pieces); |
2064 | xfree (c); | |
2065 | } | |
052b9502 NF |
2066 | } |
2067 | ||
2068 | /* Functions for accessing a variable described by DW_OP_piece. */ | |
c8f2448a | 2069 | static const struct lval_funcs pieced_value_funcs = { |
052b9502 NF |
2070 | read_pieced_value, |
2071 | write_pieced_value, | |
0e03807e TT |
2072 | check_pieced_value_validity, |
2073 | check_pieced_value_invalid, | |
8cf6f0b1 | 2074 | indirect_pieced_value, |
a471c594 | 2075 | NULL, /* coerce_ref */ |
8cf6f0b1 | 2076 | check_pieced_synthetic_pointer, |
052b9502 NF |
2077 | copy_pieced_value_closure, |
2078 | free_pieced_value_closure | |
2079 | }; | |
2080 | ||
8cf6f0b1 TT |
2081 | /* Helper function which throws an error if a synthetic pointer is |
2082 | invalid. */ | |
2083 | ||
2084 | static void | |
2085 | invalid_synthetic_pointer (void) | |
2086 | { | |
3e43a32a MS |
2087 | error (_("access outside bounds of object " |
2088 | "referenced via synthetic pointer")); | |
8cf6f0b1 TT |
2089 | } |
2090 | ||
9e8b7a03 JK |
2091 | /* Virtual method table for dwarf2_evaluate_loc_desc_full below. */ |
2092 | ||
2093 | static const struct dwarf_expr_context_funcs dwarf_expr_ctx_funcs = | |
2094 | { | |
2095 | dwarf_expr_read_reg, | |
2096 | dwarf_expr_read_mem, | |
2097 | dwarf_expr_frame_base, | |
2098 | dwarf_expr_frame_cfa, | |
2099 | dwarf_expr_frame_pc, | |
2100 | dwarf_expr_tls_address, | |
2101 | dwarf_expr_dwarf_call, | |
8e3b41a9 | 2102 | dwarf_expr_get_base_type, |
3019eac3 DE |
2103 | dwarf_expr_push_dwarf_reg_entry_value, |
2104 | dwarf_expr_get_addr_index | |
9e8b7a03 JK |
2105 | }; |
2106 | ||
4c2df51b | 2107 | /* Evaluate a location description, starting at DATA and with length |
8cf6f0b1 TT |
2108 | SIZE, to find the current location of variable of TYPE in the |
2109 | context of FRAME. BYTE_OFFSET is applied after the contents are | |
2110 | computed. */ | |
a2d33775 | 2111 | |
8cf6f0b1 TT |
2112 | static struct value * |
2113 | dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame, | |
2114 | const gdb_byte *data, unsigned short size, | |
2115 | struct dwarf2_per_cu_data *per_cu, | |
2116 | LONGEST byte_offset) | |
4c2df51b | 2117 | { |
4c2df51b DJ |
2118 | struct value *retval; |
2119 | struct dwarf_expr_baton baton; | |
2120 | struct dwarf_expr_context *ctx; | |
72fc29ff | 2121 | struct cleanup *old_chain, *value_chain; |
ac56253d | 2122 | struct objfile *objfile = dwarf2_per_cu_objfile (per_cu); |
79e1a869 | 2123 | volatile struct gdb_exception ex; |
4c2df51b | 2124 | |
8cf6f0b1 TT |
2125 | if (byte_offset < 0) |
2126 | invalid_synthetic_pointer (); | |
2127 | ||
0d53c4c4 | 2128 | if (size == 0) |
a7035dbb | 2129 | return allocate_optimized_out_value (type); |
0d53c4c4 | 2130 | |
4c2df51b | 2131 | baton.frame = frame; |
17ea53c3 | 2132 | baton.per_cu = per_cu; |
4c2df51b DJ |
2133 | |
2134 | ctx = new_dwarf_expr_context (); | |
4a227398 | 2135 | old_chain = make_cleanup_free_dwarf_expr_context (ctx); |
72fc29ff | 2136 | value_chain = make_cleanup_value_free_to_mark (value_mark ()); |
4a227398 | 2137 | |
ac56253d | 2138 | ctx->gdbarch = get_objfile_arch (objfile); |
ae0d2f24 | 2139 | ctx->addr_size = dwarf2_per_cu_addr_size (per_cu); |
181cebd4 | 2140 | ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu); |
9aa1f1e3 | 2141 | ctx->offset = dwarf2_per_cu_text_offset (per_cu); |
4c2df51b | 2142 | ctx->baton = &baton; |
9e8b7a03 | 2143 | ctx->funcs = &dwarf_expr_ctx_funcs; |
4c2df51b | 2144 | |
79e1a869 PA |
2145 | TRY_CATCH (ex, RETURN_MASK_ERROR) |
2146 | { | |
2147 | dwarf_expr_eval (ctx, data, size); | |
2148 | } | |
2149 | if (ex.reason < 0) | |
2150 | { | |
2151 | if (ex.error == NOT_AVAILABLE_ERROR) | |
2152 | { | |
72fc29ff | 2153 | do_cleanups (old_chain); |
79e1a869 PA |
2154 | retval = allocate_value (type); |
2155 | mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type)); | |
2156 | return retval; | |
2157 | } | |
8e3b41a9 JK |
2158 | else if (ex.error == NO_ENTRY_VALUE_ERROR) |
2159 | { | |
2160 | if (entry_values_debug) | |
2161 | exception_print (gdb_stdout, ex); | |
2162 | do_cleanups (old_chain); | |
2163 | return allocate_optimized_out_value (type); | |
2164 | } | |
79e1a869 PA |
2165 | else |
2166 | throw_exception (ex); | |
2167 | } | |
2168 | ||
87808bd6 JB |
2169 | if (ctx->num_pieces > 0) |
2170 | { | |
052b9502 NF |
2171 | struct piece_closure *c; |
2172 | struct frame_id frame_id = get_frame_id (frame); | |
8cf6f0b1 TT |
2173 | ULONGEST bit_size = 0; |
2174 | int i; | |
052b9502 | 2175 | |
8cf6f0b1 TT |
2176 | for (i = 0; i < ctx->num_pieces; ++i) |
2177 | bit_size += ctx->pieces[i].size; | |
2178 | if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size) | |
2179 | invalid_synthetic_pointer (); | |
2180 | ||
2181 | c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces, | |
6063c216 | 2182 | ctx->addr_size); |
72fc29ff TT |
2183 | /* We must clean up the value chain after creating the piece |
2184 | closure but before allocating the result. */ | |
2185 | do_cleanups (value_chain); | |
a2d33775 | 2186 | retval = allocate_computed_value (type, &pieced_value_funcs, c); |
052b9502 | 2187 | VALUE_FRAME_ID (retval) = frame_id; |
8cf6f0b1 | 2188 | set_value_offset (retval, byte_offset); |
87808bd6 | 2189 | } |
4c2df51b DJ |
2190 | else |
2191 | { | |
cec03d70 TT |
2192 | switch (ctx->location) |
2193 | { | |
2194 | case DWARF_VALUE_REGISTER: | |
2195 | { | |
2196 | struct gdbarch *arch = get_frame_arch (frame); | |
8a9b8146 | 2197 | ULONGEST dwarf_regnum = value_as_long (dwarf_expr_fetch (ctx, 0)); |
cec03d70 | 2198 | int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum); |
9a619af0 | 2199 | |
8cf6f0b1 TT |
2200 | if (byte_offset != 0) |
2201 | error (_("cannot use offset on synthetic pointer to register")); | |
72fc29ff | 2202 | do_cleanups (value_chain); |
63b4f126 | 2203 | if (gdb_regnum != -1) |
a2d33775 | 2204 | retval = value_from_register (type, gdb_regnum, frame); |
63b4f126 | 2205 | else |
a2d33775 JK |
2206 | error (_("Unable to access DWARF register number %s"), |
2207 | paddress (arch, dwarf_regnum)); | |
cec03d70 TT |
2208 | } |
2209 | break; | |
2210 | ||
2211 | case DWARF_VALUE_MEMORY: | |
2212 | { | |
f2c7657e | 2213 | CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0); |
44353522 | 2214 | int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0); |
cec03d70 | 2215 | |
72fc29ff | 2216 | do_cleanups (value_chain); |
41e8491f | 2217 | retval = allocate_value_lazy (type); |
cec03d70 | 2218 | VALUE_LVAL (retval) = lval_memory; |
44353522 DE |
2219 | if (in_stack_memory) |
2220 | set_value_stack (retval, 1); | |
8cf6f0b1 | 2221 | set_value_address (retval, address + byte_offset); |
cec03d70 TT |
2222 | } |
2223 | break; | |
2224 | ||
2225 | case DWARF_VALUE_STACK: | |
2226 | { | |
8a9b8146 TT |
2227 | struct value *value = dwarf_expr_fetch (ctx, 0); |
2228 | gdb_byte *contents; | |
2229 | const gdb_byte *val_bytes; | |
2230 | size_t n = TYPE_LENGTH (value_type (value)); | |
cec03d70 | 2231 | |
8cf6f0b1 TT |
2232 | if (byte_offset + TYPE_LENGTH (type) > n) |
2233 | invalid_synthetic_pointer (); | |
2234 | ||
8a9b8146 TT |
2235 | val_bytes = value_contents_all (value); |
2236 | val_bytes += byte_offset; | |
8cf6f0b1 TT |
2237 | n -= byte_offset; |
2238 | ||
72fc29ff TT |
2239 | /* Preserve VALUE because we are going to free values back |
2240 | to the mark, but we still need the value contents | |
2241 | below. */ | |
2242 | value_incref (value); | |
2243 | do_cleanups (value_chain); | |
2244 | make_cleanup_value_free (value); | |
2245 | ||
a2d33775 | 2246 | retval = allocate_value (type); |
cec03d70 | 2247 | contents = value_contents_raw (retval); |
a2d33775 | 2248 | if (n > TYPE_LENGTH (type)) |
b6cede78 JK |
2249 | { |
2250 | struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile); | |
2251 | ||
2252 | if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG) | |
2253 | val_bytes += n - TYPE_LENGTH (type); | |
2254 | n = TYPE_LENGTH (type); | |
2255 | } | |
8a9b8146 | 2256 | memcpy (contents, val_bytes, n); |
cec03d70 TT |
2257 | } |
2258 | break; | |
2259 | ||
2260 | case DWARF_VALUE_LITERAL: | |
2261 | { | |
2262 | bfd_byte *contents; | |
8c814cdd | 2263 | const bfd_byte *ldata; |
cec03d70 TT |
2264 | size_t n = ctx->len; |
2265 | ||
8cf6f0b1 TT |
2266 | if (byte_offset + TYPE_LENGTH (type) > n) |
2267 | invalid_synthetic_pointer (); | |
2268 | ||
72fc29ff | 2269 | do_cleanups (value_chain); |
a2d33775 | 2270 | retval = allocate_value (type); |
cec03d70 | 2271 | contents = value_contents_raw (retval); |
8cf6f0b1 | 2272 | |
8c814cdd | 2273 | ldata = ctx->data + byte_offset; |
8cf6f0b1 TT |
2274 | n -= byte_offset; |
2275 | ||
a2d33775 | 2276 | if (n > TYPE_LENGTH (type)) |
b6cede78 JK |
2277 | { |
2278 | struct gdbarch *objfile_gdbarch = get_objfile_arch (objfile); | |
2279 | ||
2280 | if (gdbarch_byte_order (objfile_gdbarch) == BFD_ENDIAN_BIG) | |
2281 | ldata += n - TYPE_LENGTH (type); | |
2282 | n = TYPE_LENGTH (type); | |
2283 | } | |
8c814cdd | 2284 | memcpy (contents, ldata, n); |
cec03d70 TT |
2285 | } |
2286 | break; | |
2287 | ||
dd90784c | 2288 | case DWARF_VALUE_OPTIMIZED_OUT: |
72fc29ff | 2289 | do_cleanups (value_chain); |
a7035dbb | 2290 | retval = allocate_optimized_out_value (type); |
dd90784c JK |
2291 | break; |
2292 | ||
8cf6f0b1 TT |
2293 | /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced |
2294 | operation by execute_stack_op. */ | |
2295 | case DWARF_VALUE_IMPLICIT_POINTER: | |
cb826367 TT |
2296 | /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context -- |
2297 | it can only be encountered when making a piece. */ | |
cec03d70 TT |
2298 | default: |
2299 | internal_error (__FILE__, __LINE__, _("invalid location type")); | |
2300 | } | |
4c2df51b DJ |
2301 | } |
2302 | ||
42be36b3 CT |
2303 | set_value_initialized (retval, ctx->initialized); |
2304 | ||
4a227398 | 2305 | do_cleanups (old_chain); |
4c2df51b DJ |
2306 | |
2307 | return retval; | |
2308 | } | |
8cf6f0b1 TT |
2309 | |
2310 | /* The exported interface to dwarf2_evaluate_loc_desc_full; it always | |
2311 | passes 0 as the byte_offset. */ | |
2312 | ||
2313 | struct value * | |
2314 | dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame, | |
2315 | const gdb_byte *data, unsigned short size, | |
2316 | struct dwarf2_per_cu_data *per_cu) | |
2317 | { | |
2318 | return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0); | |
2319 | } | |
2320 | ||
4c2df51b DJ |
2321 | \f |
2322 | /* Helper functions and baton for dwarf2_loc_desc_needs_frame. */ | |
2323 | ||
2324 | struct needs_frame_baton | |
2325 | { | |
2326 | int needs_frame; | |
17ea53c3 | 2327 | struct dwarf2_per_cu_data *per_cu; |
4c2df51b DJ |
2328 | }; |
2329 | ||
2330 | /* Reads from registers do require a frame. */ | |
2331 | static CORE_ADDR | |
61fbb938 | 2332 | needs_frame_read_reg (void *baton, int regnum) |
4c2df51b DJ |
2333 | { |
2334 | struct needs_frame_baton *nf_baton = baton; | |
9a619af0 | 2335 | |
4c2df51b DJ |
2336 | nf_baton->needs_frame = 1; |
2337 | return 1; | |
2338 | } | |
2339 | ||
2340 | /* Reads from memory do not require a frame. */ | |
2341 | static void | |
852483bc | 2342 | needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len) |
4c2df51b DJ |
2343 | { |
2344 | memset (buf, 0, len); | |
2345 | } | |
2346 | ||
2347 | /* Frame-relative accesses do require a frame. */ | |
2348 | static void | |
0d45f56e | 2349 | needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length) |
4c2df51b | 2350 | { |
852483bc | 2351 | static gdb_byte lit0 = DW_OP_lit0; |
4c2df51b DJ |
2352 | struct needs_frame_baton *nf_baton = baton; |
2353 | ||
2354 | *start = &lit0; | |
2355 | *length = 1; | |
2356 | ||
2357 | nf_baton->needs_frame = 1; | |
2358 | } | |
2359 | ||
e7802207 TT |
2360 | /* CFA accesses require a frame. */ |
2361 | ||
2362 | static CORE_ADDR | |
2363 | needs_frame_frame_cfa (void *baton) | |
2364 | { | |
2365 | struct needs_frame_baton *nf_baton = baton; | |
9a619af0 | 2366 | |
e7802207 TT |
2367 | nf_baton->needs_frame = 1; |
2368 | return 1; | |
2369 | } | |
2370 | ||
4c2df51b DJ |
2371 | /* Thread-local accesses do require a frame. */ |
2372 | static CORE_ADDR | |
2373 | needs_frame_tls_address (void *baton, CORE_ADDR offset) | |
2374 | { | |
2375 | struct needs_frame_baton *nf_baton = baton; | |
9a619af0 | 2376 | |
4c2df51b DJ |
2377 | nf_baton->needs_frame = 1; |
2378 | return 1; | |
2379 | } | |
2380 | ||
5c631832 JK |
2381 | /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame. */ |
2382 | ||
2383 | static void | |
b64f50a1 | 2384 | needs_frame_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset) |
5c631832 JK |
2385 | { |
2386 | struct needs_frame_baton *nf_baton = ctx->baton; | |
2387 | ||
37b50a69 | 2388 | per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu, |
9e8b7a03 | 2389 | ctx->funcs->get_frame_pc, ctx->baton); |
5c631832 JK |
2390 | } |
2391 | ||
8e3b41a9 JK |
2392 | /* DW_OP_GNU_entry_value accesses require a caller, therefore a frame. */ |
2393 | ||
2394 | static void | |
2395 | needs_dwarf_reg_entry_value (struct dwarf_expr_context *ctx, | |
24c5c679 JK |
2396 | enum call_site_parameter_kind kind, |
2397 | union call_site_parameter_u kind_u, int deref_size) | |
8e3b41a9 JK |
2398 | { |
2399 | struct needs_frame_baton *nf_baton = ctx->baton; | |
2400 | ||
2401 | nf_baton->needs_frame = 1; | |
1788b2d3 JK |
2402 | |
2403 | /* The expression may require some stub values on DWARF stack. */ | |
2404 | dwarf_expr_push_address (ctx, 0, 0); | |
8e3b41a9 JK |
2405 | } |
2406 | ||
3019eac3 DE |
2407 | /* DW_OP_GNU_addr_index doesn't require a frame. */ |
2408 | ||
2409 | static CORE_ADDR | |
2410 | needs_get_addr_index (void *baton, unsigned int index) | |
2411 | { | |
2412 | /* Nothing to do. */ | |
2413 | return 1; | |
2414 | } | |
2415 | ||
9e8b7a03 JK |
2416 | /* Virtual method table for dwarf2_loc_desc_needs_frame below. */ |
2417 | ||
2418 | static const struct dwarf_expr_context_funcs needs_frame_ctx_funcs = | |
2419 | { | |
2420 | needs_frame_read_reg, | |
2421 | needs_frame_read_mem, | |
2422 | needs_frame_frame_base, | |
2423 | needs_frame_frame_cfa, | |
2424 | needs_frame_frame_cfa, /* get_frame_pc */ | |
2425 | needs_frame_tls_address, | |
2426 | needs_frame_dwarf_call, | |
8e3b41a9 | 2427 | NULL, /* get_base_type */ |
3019eac3 DE |
2428 | needs_dwarf_reg_entry_value, |
2429 | needs_get_addr_index | |
9e8b7a03 JK |
2430 | }; |
2431 | ||
4c2df51b DJ |
2432 | /* Return non-zero iff the location expression at DATA (length SIZE) |
2433 | requires a frame to evaluate. */ | |
2434 | ||
2435 | static int | |
947bb88f | 2436 | dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size, |
ae0d2f24 | 2437 | struct dwarf2_per_cu_data *per_cu) |
4c2df51b DJ |
2438 | { |
2439 | struct needs_frame_baton baton; | |
2440 | struct dwarf_expr_context *ctx; | |
f630a401 | 2441 | int in_reg; |
4a227398 | 2442 | struct cleanup *old_chain; |
ac56253d | 2443 | struct objfile *objfile = dwarf2_per_cu_objfile (per_cu); |
4c2df51b DJ |
2444 | |
2445 | baton.needs_frame = 0; | |
17ea53c3 | 2446 | baton.per_cu = per_cu; |
4c2df51b DJ |
2447 | |
2448 | ctx = new_dwarf_expr_context (); | |
4a227398 | 2449 | old_chain = make_cleanup_free_dwarf_expr_context (ctx); |
72fc29ff | 2450 | make_cleanup_value_free_to_mark (value_mark ()); |
4a227398 | 2451 | |
ac56253d | 2452 | ctx->gdbarch = get_objfile_arch (objfile); |
ae0d2f24 | 2453 | ctx->addr_size = dwarf2_per_cu_addr_size (per_cu); |
181cebd4 | 2454 | ctx->ref_addr_size = dwarf2_per_cu_ref_addr_size (per_cu); |
9aa1f1e3 | 2455 | ctx->offset = dwarf2_per_cu_text_offset (per_cu); |
4c2df51b | 2456 | ctx->baton = &baton; |
9e8b7a03 | 2457 | ctx->funcs = &needs_frame_ctx_funcs; |
4c2df51b DJ |
2458 | |
2459 | dwarf_expr_eval (ctx, data, size); | |
2460 | ||
cec03d70 | 2461 | in_reg = ctx->location == DWARF_VALUE_REGISTER; |
f630a401 | 2462 | |
87808bd6 JB |
2463 | if (ctx->num_pieces > 0) |
2464 | { | |
2465 | int i; | |
2466 | ||
2467 | /* If the location has several pieces, and any of them are in | |
2468 | registers, then we will need a frame to fetch them from. */ | |
2469 | for (i = 0; i < ctx->num_pieces; i++) | |
cec03d70 | 2470 | if (ctx->pieces[i].location == DWARF_VALUE_REGISTER) |
87808bd6 JB |
2471 | in_reg = 1; |
2472 | } | |
2473 | ||
4a227398 | 2474 | do_cleanups (old_chain); |
4c2df51b | 2475 | |
f630a401 | 2476 | return baton.needs_frame || in_reg; |
4c2df51b DJ |
2477 | } |
2478 | ||
3cf03773 TT |
2479 | /* A helper function that throws an unimplemented error mentioning a |
2480 | given DWARF operator. */ | |
2481 | ||
2482 | static void | |
2483 | unimplemented (unsigned int op) | |
0d53c4c4 | 2484 | { |
f39c6ffd | 2485 | const char *name = get_DW_OP_name (op); |
b1bfef65 TT |
2486 | |
2487 | if (name) | |
2488 | error (_("DWARF operator %s cannot be translated to an agent expression"), | |
2489 | name); | |
2490 | else | |
1ba1b353 TT |
2491 | error (_("Unknown DWARF operator 0x%02x cannot be translated " |
2492 | "to an agent expression"), | |
b1bfef65 | 2493 | op); |
3cf03773 | 2494 | } |
08922a10 | 2495 | |
3cf03773 TT |
2496 | /* A helper function to convert a DWARF register to an arch register. |
2497 | ARCH is the architecture. | |
2498 | DWARF_REG is the register. | |
2499 | This will throw an exception if the DWARF register cannot be | |
2500 | translated to an architecture register. */ | |
08922a10 | 2501 | |
3cf03773 TT |
2502 | static int |
2503 | translate_register (struct gdbarch *arch, int dwarf_reg) | |
2504 | { | |
2505 | int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg); | |
2506 | if (reg == -1) | |
2507 | error (_("Unable to access DWARF register number %d"), dwarf_reg); | |
2508 | return reg; | |
2509 | } | |
08922a10 | 2510 | |
3cf03773 TT |
2511 | /* A helper function that emits an access to memory. ARCH is the |
2512 | target architecture. EXPR is the expression which we are building. | |
2513 | NBITS is the number of bits we want to read. This emits the | |
2514 | opcodes needed to read the memory and then extract the desired | |
2515 | bits. */ | |
08922a10 | 2516 | |
3cf03773 TT |
2517 | static void |
2518 | access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits) | |
08922a10 | 2519 | { |
3cf03773 TT |
2520 | ULONGEST nbytes = (nbits + 7) / 8; |
2521 | ||
2522 | gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST)); | |
2523 | ||
2524 | if (trace_kludge) | |
2525 | ax_trace_quick (expr, nbytes); | |
2526 | ||
2527 | if (nbits <= 8) | |
2528 | ax_simple (expr, aop_ref8); | |
2529 | else if (nbits <= 16) | |
2530 | ax_simple (expr, aop_ref16); | |
2531 | else if (nbits <= 32) | |
2532 | ax_simple (expr, aop_ref32); | |
2533 | else | |
2534 | ax_simple (expr, aop_ref64); | |
2535 | ||
2536 | /* If we read exactly the number of bytes we wanted, we're done. */ | |
2537 | if (8 * nbytes == nbits) | |
2538 | return; | |
2539 | ||
2540 | if (gdbarch_bits_big_endian (arch)) | |
0d53c4c4 | 2541 | { |
3cf03773 TT |
2542 | /* On a bits-big-endian machine, we want the high-order |
2543 | NBITS. */ | |
2544 | ax_const_l (expr, 8 * nbytes - nbits); | |
2545 | ax_simple (expr, aop_rsh_unsigned); | |
0d53c4c4 | 2546 | } |
3cf03773 | 2547 | else |
0d53c4c4 | 2548 | { |
3cf03773 TT |
2549 | /* On a bits-little-endian box, we want the low-order NBITS. */ |
2550 | ax_zero_ext (expr, nbits); | |
0d53c4c4 | 2551 | } |
3cf03773 | 2552 | } |
0936ad1d | 2553 | |
8cf6f0b1 TT |
2554 | /* A helper function to return the frame's PC. */ |
2555 | ||
2556 | static CORE_ADDR | |
2557 | get_ax_pc (void *baton) | |
2558 | { | |
2559 | struct agent_expr *expr = baton; | |
2560 | ||
2561 | return expr->scope; | |
2562 | } | |
2563 | ||
3cf03773 TT |
2564 | /* Compile a DWARF location expression to an agent expression. |
2565 | ||
2566 | EXPR is the agent expression we are building. | |
2567 | LOC is the agent value we modify. | |
2568 | ARCH is the architecture. | |
2569 | ADDR_SIZE is the size of addresses, in bytes. | |
2570 | OP_PTR is the start of the location expression. | |
2571 | OP_END is one past the last byte of the location expression. | |
2572 | ||
2573 | This will throw an exception for various kinds of errors -- for | |
2574 | example, if the expression cannot be compiled, or if the expression | |
2575 | is invalid. */ | |
0936ad1d | 2576 | |
9f6f94ff TT |
2577 | void |
2578 | dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc, | |
2579 | struct gdbarch *arch, unsigned int addr_size, | |
2580 | const gdb_byte *op_ptr, const gdb_byte *op_end, | |
2581 | struct dwarf2_per_cu_data *per_cu) | |
3cf03773 TT |
2582 | { |
2583 | struct cleanup *cleanups; | |
2584 | int i, *offsets; | |
2585 | VEC(int) *dw_labels = NULL, *patches = NULL; | |
2586 | const gdb_byte * const base = op_ptr; | |
2587 | const gdb_byte *previous_piece = op_ptr; | |
2588 | enum bfd_endian byte_order = gdbarch_byte_order (arch); | |
2589 | ULONGEST bits_collected = 0; | |
2590 | unsigned int addr_size_bits = 8 * addr_size; | |
2591 | int bits_big_endian = gdbarch_bits_big_endian (arch); | |
0936ad1d | 2592 | |
3cf03773 TT |
2593 | offsets = xmalloc ((op_end - op_ptr) * sizeof (int)); |
2594 | cleanups = make_cleanup (xfree, offsets); | |
0936ad1d | 2595 | |
3cf03773 TT |
2596 | for (i = 0; i < op_end - op_ptr; ++i) |
2597 | offsets[i] = -1; | |
0936ad1d | 2598 | |
3cf03773 TT |
2599 | make_cleanup (VEC_cleanup (int), &dw_labels); |
2600 | make_cleanup (VEC_cleanup (int), &patches); | |
0936ad1d | 2601 | |
3cf03773 TT |
2602 | /* By default we are making an address. */ |
2603 | loc->kind = axs_lvalue_memory; | |
0d45f56e | 2604 | |
3cf03773 TT |
2605 | while (op_ptr < op_end) |
2606 | { | |
2607 | enum dwarf_location_atom op = *op_ptr; | |
9fccedf7 DE |
2608 | uint64_t uoffset, reg; |
2609 | int64_t offset; | |
3cf03773 TT |
2610 | int i; |
2611 | ||
2612 | offsets[op_ptr - base] = expr->len; | |
2613 | ++op_ptr; | |
2614 | ||
2615 | /* Our basic approach to code generation is to map DWARF | |
2616 | operations directly to AX operations. However, there are | |
2617 | some differences. | |
2618 | ||
2619 | First, DWARF works on address-sized units, but AX always uses | |
2620 | LONGEST. For most operations we simply ignore this | |
2621 | difference; instead we generate sign extensions as needed | |
2622 | before division and comparison operations. It would be nice | |
2623 | to omit the sign extensions, but there is no way to determine | |
2624 | the size of the target's LONGEST. (This code uses the size | |
2625 | of the host LONGEST in some cases -- that is a bug but it is | |
2626 | difficult to fix.) | |
2627 | ||
2628 | Second, some DWARF operations cannot be translated to AX. | |
2629 | For these we simply fail. See | |
2630 | http://sourceware.org/bugzilla/show_bug.cgi?id=11662. */ | |
2631 | switch (op) | |
0936ad1d | 2632 | { |
3cf03773 TT |
2633 | case DW_OP_lit0: |
2634 | case DW_OP_lit1: | |
2635 | case DW_OP_lit2: | |
2636 | case DW_OP_lit3: | |
2637 | case DW_OP_lit4: | |
2638 | case DW_OP_lit5: | |
2639 | case DW_OP_lit6: | |
2640 | case DW_OP_lit7: | |
2641 | case DW_OP_lit8: | |
2642 | case DW_OP_lit9: | |
2643 | case DW_OP_lit10: | |
2644 | case DW_OP_lit11: | |
2645 | case DW_OP_lit12: | |
2646 | case DW_OP_lit13: | |
2647 | case DW_OP_lit14: | |
2648 | case DW_OP_lit15: | |
2649 | case DW_OP_lit16: | |
2650 | case DW_OP_lit17: | |
2651 | case DW_OP_lit18: | |
2652 | case DW_OP_lit19: | |
2653 | case DW_OP_lit20: | |
2654 | case DW_OP_lit21: | |
2655 | case DW_OP_lit22: | |
2656 | case DW_OP_lit23: | |
2657 | case DW_OP_lit24: | |
2658 | case DW_OP_lit25: | |
2659 | case DW_OP_lit26: | |
2660 | case DW_OP_lit27: | |
2661 | case DW_OP_lit28: | |
2662 | case DW_OP_lit29: | |
2663 | case DW_OP_lit30: | |
2664 | case DW_OP_lit31: | |
2665 | ax_const_l (expr, op - DW_OP_lit0); | |
2666 | break; | |
0d53c4c4 | 2667 | |
3cf03773 | 2668 | case DW_OP_addr: |
ac56253d | 2669 | uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order); |
3cf03773 | 2670 | op_ptr += addr_size; |
ac56253d TT |
2671 | /* Some versions of GCC emit DW_OP_addr before |
2672 | DW_OP_GNU_push_tls_address. In this case the value is an | |
2673 | index, not an address. We don't support things like | |
2674 | branching between the address and the TLS op. */ | |
2675 | if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address) | |
9aa1f1e3 | 2676 | uoffset += dwarf2_per_cu_text_offset (per_cu); |
ac56253d | 2677 | ax_const_l (expr, uoffset); |
3cf03773 | 2678 | break; |
4c2df51b | 2679 | |
3cf03773 TT |
2680 | case DW_OP_const1u: |
2681 | ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order)); | |
2682 | op_ptr += 1; | |
2683 | break; | |
2684 | case DW_OP_const1s: | |
2685 | ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order)); | |
2686 | op_ptr += 1; | |
2687 | break; | |
2688 | case DW_OP_const2u: | |
2689 | ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order)); | |
2690 | op_ptr += 2; | |
2691 | break; | |
2692 | case DW_OP_const2s: | |
2693 | ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order)); | |
2694 | op_ptr += 2; | |
2695 | break; | |
2696 | case DW_OP_const4u: | |
2697 | ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order)); | |
2698 | op_ptr += 4; | |
2699 | break; | |
2700 | case DW_OP_const4s: | |
2701 | ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order)); | |
2702 | op_ptr += 4; | |
2703 | break; | |
2704 | case DW_OP_const8u: | |
2705 | ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order)); | |
2706 | op_ptr += 8; | |
2707 | break; | |
2708 | case DW_OP_const8s: | |
2709 | ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order)); | |
2710 | op_ptr += 8; | |
2711 | break; | |
2712 | case DW_OP_constu: | |
f664829e | 2713 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset); |
3cf03773 TT |
2714 | ax_const_l (expr, uoffset); |
2715 | break; | |
2716 | case DW_OP_consts: | |
f664829e | 2717 | op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); |
3cf03773 TT |
2718 | ax_const_l (expr, offset); |
2719 | break; | |
9c238357 | 2720 | |
3cf03773 TT |
2721 | case DW_OP_reg0: |
2722 | case DW_OP_reg1: | |
2723 | case DW_OP_reg2: | |
2724 | case DW_OP_reg3: | |
2725 | case DW_OP_reg4: | |
2726 | case DW_OP_reg5: | |
2727 | case DW_OP_reg6: | |
2728 | case DW_OP_reg7: | |
2729 | case DW_OP_reg8: | |
2730 | case DW_OP_reg9: | |
2731 | case DW_OP_reg10: | |
2732 | case DW_OP_reg11: | |
2733 | case DW_OP_reg12: | |
2734 | case DW_OP_reg13: | |
2735 | case DW_OP_reg14: | |
2736 | case DW_OP_reg15: | |
2737 | case DW_OP_reg16: | |
2738 | case DW_OP_reg17: | |
2739 | case DW_OP_reg18: | |
2740 | case DW_OP_reg19: | |
2741 | case DW_OP_reg20: | |
2742 | case DW_OP_reg21: | |
2743 | case DW_OP_reg22: | |
2744 | case DW_OP_reg23: | |
2745 | case DW_OP_reg24: | |
2746 | case DW_OP_reg25: | |
2747 | case DW_OP_reg26: | |
2748 | case DW_OP_reg27: | |
2749 | case DW_OP_reg28: | |
2750 | case DW_OP_reg29: | |
2751 | case DW_OP_reg30: | |
2752 | case DW_OP_reg31: | |
2753 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx"); | |
2754 | loc->u.reg = translate_register (arch, op - DW_OP_reg0); | |
2755 | loc->kind = axs_lvalue_register; | |
2756 | break; | |
9c238357 | 2757 | |
3cf03773 | 2758 | case DW_OP_regx: |
f664829e | 2759 | op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); |
3cf03773 TT |
2760 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx"); |
2761 | loc->u.reg = translate_register (arch, reg); | |
2762 | loc->kind = axs_lvalue_register; | |
2763 | break; | |
08922a10 | 2764 | |
3cf03773 TT |
2765 | case DW_OP_implicit_value: |
2766 | { | |
9fccedf7 | 2767 | uint64_t len; |
3cf03773 | 2768 | |
f664829e | 2769 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &len); |
3cf03773 TT |
2770 | if (op_ptr + len > op_end) |
2771 | error (_("DW_OP_implicit_value: too few bytes available.")); | |
2772 | if (len > sizeof (ULONGEST)) | |
2773 | error (_("Cannot translate DW_OP_implicit_value of %d bytes"), | |
2774 | (int) len); | |
2775 | ||
2776 | ax_const_l (expr, extract_unsigned_integer (op_ptr, len, | |
2777 | byte_order)); | |
2778 | op_ptr += len; | |
2779 | dwarf_expr_require_composition (op_ptr, op_end, | |
2780 | "DW_OP_implicit_value"); | |
2781 | ||
2782 | loc->kind = axs_rvalue; | |
2783 | } | |
2784 | break; | |
08922a10 | 2785 | |
3cf03773 TT |
2786 | case DW_OP_stack_value: |
2787 | dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value"); | |
2788 | loc->kind = axs_rvalue; | |
2789 | break; | |
08922a10 | 2790 | |
3cf03773 TT |
2791 | case DW_OP_breg0: |
2792 | case DW_OP_breg1: | |
2793 | case DW_OP_breg2: | |
2794 | case DW_OP_breg3: | |
2795 | case DW_OP_breg4: | |
2796 | case DW_OP_breg5: | |
2797 | case DW_OP_breg6: | |
2798 | case DW_OP_breg7: | |
2799 | case DW_OP_breg8: | |
2800 | case DW_OP_breg9: | |
2801 | case DW_OP_breg10: | |
2802 | case DW_OP_breg11: | |
2803 | case DW_OP_breg12: | |
2804 | case DW_OP_breg13: | |
2805 | case DW_OP_breg14: | |
2806 | case DW_OP_breg15: | |
2807 | case DW_OP_breg16: | |
2808 | case DW_OP_breg17: | |
2809 | case DW_OP_breg18: | |
2810 | case DW_OP_breg19: | |
2811 | case DW_OP_breg20: | |
2812 | case DW_OP_breg21: | |
2813 | case DW_OP_breg22: | |
2814 | case DW_OP_breg23: | |
2815 | case DW_OP_breg24: | |
2816 | case DW_OP_breg25: | |
2817 | case DW_OP_breg26: | |
2818 | case DW_OP_breg27: | |
2819 | case DW_OP_breg28: | |
2820 | case DW_OP_breg29: | |
2821 | case DW_OP_breg30: | |
2822 | case DW_OP_breg31: | |
f664829e | 2823 | op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); |
3cf03773 TT |
2824 | i = translate_register (arch, op - DW_OP_breg0); |
2825 | ax_reg (expr, i); | |
2826 | if (offset != 0) | |
2827 | { | |
2828 | ax_const_l (expr, offset); | |
2829 | ax_simple (expr, aop_add); | |
2830 | } | |
2831 | break; | |
2832 | case DW_OP_bregx: | |
2833 | { | |
f664829e DE |
2834 | op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); |
2835 | op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); | |
3cf03773 TT |
2836 | i = translate_register (arch, reg); |
2837 | ax_reg (expr, i); | |
2838 | if (offset != 0) | |
2839 | { | |
2840 | ax_const_l (expr, offset); | |
2841 | ax_simple (expr, aop_add); | |
2842 | } | |
2843 | } | |
2844 | break; | |
2845 | case DW_OP_fbreg: | |
2846 | { | |
2847 | const gdb_byte *datastart; | |
2848 | size_t datalen; | |
3cf03773 TT |
2849 | struct block *b; |
2850 | struct symbol *framefunc; | |
2851 | LONGEST base_offset = 0; | |
08922a10 | 2852 | |
3cf03773 TT |
2853 | b = block_for_pc (expr->scope); |
2854 | ||
2855 | if (!b) | |
2856 | error (_("No block found for address")); | |
2857 | ||
2858 | framefunc = block_linkage_function (b); | |
2859 | ||
2860 | if (!framefunc) | |
2861 | error (_("No function found for block")); | |
2862 | ||
2863 | dwarf_expr_frame_base_1 (framefunc, expr->scope, | |
2864 | &datastart, &datalen); | |
2865 | ||
f664829e | 2866 | op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset); |
9f6f94ff TT |
2867 | dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart, |
2868 | datastart + datalen, per_cu); | |
3cf03773 TT |
2869 | |
2870 | if (offset != 0) | |
2871 | { | |
2872 | ax_const_l (expr, offset); | |
2873 | ax_simple (expr, aop_add); | |
2874 | } | |
2875 | ||
2876 | loc->kind = axs_lvalue_memory; | |
2877 | } | |
08922a10 | 2878 | break; |
08922a10 | 2879 | |
3cf03773 TT |
2880 | case DW_OP_dup: |
2881 | ax_simple (expr, aop_dup); | |
2882 | break; | |
08922a10 | 2883 | |
3cf03773 TT |
2884 | case DW_OP_drop: |
2885 | ax_simple (expr, aop_pop); | |
2886 | break; | |
08922a10 | 2887 | |
3cf03773 TT |
2888 | case DW_OP_pick: |
2889 | offset = *op_ptr++; | |
c7f96d2b | 2890 | ax_pick (expr, offset); |
3cf03773 TT |
2891 | break; |
2892 | ||
2893 | case DW_OP_swap: | |
2894 | ax_simple (expr, aop_swap); | |
2895 | break; | |
08922a10 | 2896 | |
3cf03773 | 2897 | case DW_OP_over: |
c7f96d2b | 2898 | ax_pick (expr, 1); |
3cf03773 | 2899 | break; |
08922a10 | 2900 | |
3cf03773 | 2901 | case DW_OP_rot: |
c7f96d2b | 2902 | ax_simple (expr, aop_rot); |
3cf03773 | 2903 | break; |
08922a10 | 2904 | |
3cf03773 TT |
2905 | case DW_OP_deref: |
2906 | case DW_OP_deref_size: | |
2907 | { | |
2908 | int size; | |
08922a10 | 2909 | |
3cf03773 TT |
2910 | if (op == DW_OP_deref_size) |
2911 | size = *op_ptr++; | |
2912 | else | |
2913 | size = addr_size; | |
2914 | ||
2915 | switch (size) | |
2916 | { | |
2917 | case 8: | |
2918 | ax_simple (expr, aop_ref8); | |
2919 | break; | |
2920 | case 16: | |
2921 | ax_simple (expr, aop_ref16); | |
2922 | break; | |
2923 | case 32: | |
2924 | ax_simple (expr, aop_ref32); | |
2925 | break; | |
2926 | case 64: | |
2927 | ax_simple (expr, aop_ref64); | |
2928 | break; | |
2929 | default: | |
f39c6ffd | 2930 | /* Note that get_DW_OP_name will never return |
b1bfef65 | 2931 | NULL here. */ |
3cf03773 | 2932 | error (_("Unsupported size %d in %s"), |
f39c6ffd | 2933 | size, get_DW_OP_name (op)); |
3cf03773 TT |
2934 | } |
2935 | } | |
2936 | break; | |
2937 | ||
2938 | case DW_OP_abs: | |
2939 | /* Sign extend the operand. */ | |
2940 | ax_ext (expr, addr_size_bits); | |
2941 | ax_simple (expr, aop_dup); | |
2942 | ax_const_l (expr, 0); | |
2943 | ax_simple (expr, aop_less_signed); | |
2944 | ax_simple (expr, aop_log_not); | |
2945 | i = ax_goto (expr, aop_if_goto); | |
2946 | /* We have to emit 0 - X. */ | |
2947 | ax_const_l (expr, 0); | |
2948 | ax_simple (expr, aop_swap); | |
2949 | ax_simple (expr, aop_sub); | |
2950 | ax_label (expr, i, expr->len); | |
2951 | break; | |
2952 | ||
2953 | case DW_OP_neg: | |
2954 | /* No need to sign extend here. */ | |
2955 | ax_const_l (expr, 0); | |
2956 | ax_simple (expr, aop_swap); | |
2957 | ax_simple (expr, aop_sub); | |
2958 | break; | |
2959 | ||
2960 | case DW_OP_not: | |
2961 | /* Sign extend the operand. */ | |
2962 | ax_ext (expr, addr_size_bits); | |
2963 | ax_simple (expr, aop_bit_not); | |
2964 | break; | |
2965 | ||
2966 | case DW_OP_plus_uconst: | |
f664829e | 2967 | op_ptr = safe_read_uleb128 (op_ptr, op_end, ®); |
3cf03773 TT |
2968 | /* It would be really weird to emit `DW_OP_plus_uconst 0', |
2969 | but we micro-optimize anyhow. */ | |
2970 | if (reg != 0) | |
2971 | { | |
2972 | ax_const_l (expr, reg); | |
2973 | ax_simple (expr, aop_add); | |
2974 | } | |
2975 | break; | |
2976 | ||
2977 | case DW_OP_and: | |
2978 | ax_simple (expr, aop_bit_and); | |
2979 | break; | |
2980 | ||
2981 | case DW_OP_div: | |
2982 | /* Sign extend the operands. */ | |
2983 | ax_ext (expr, addr_size_bits); | |
2984 | ax_simple (expr, aop_swap); | |
2985 | ax_ext (expr, addr_size_bits); | |
2986 | ax_simple (expr, aop_swap); | |
2987 | ax_simple (expr, aop_div_signed); | |
08922a10 SS |
2988 | break; |
2989 | ||
3cf03773 TT |
2990 | case DW_OP_minus: |
2991 | ax_simple (expr, aop_sub); | |
2992 | break; | |
2993 | ||
2994 | case DW_OP_mod: | |
2995 | ax_simple (expr, aop_rem_unsigned); | |
2996 | break; | |
2997 | ||
2998 | case DW_OP_mul: | |
2999 | ax_simple (expr, aop_mul); | |
3000 | break; | |
3001 | ||
3002 | case DW_OP_or: | |
3003 | ax_simple (expr, aop_bit_or); | |
3004 | break; | |
3005 | ||
3006 | case DW_OP_plus: | |
3007 | ax_simple (expr, aop_add); | |
3008 | break; | |
3009 | ||
3010 | case DW_OP_shl: | |
3011 | ax_simple (expr, aop_lsh); | |
3012 | break; | |
3013 | ||
3014 | case DW_OP_shr: | |
3015 | ax_simple (expr, aop_rsh_unsigned); | |
3016 | break; | |
3017 | ||
3018 | case DW_OP_shra: | |
3019 | ax_simple (expr, aop_rsh_signed); | |
3020 | break; | |
3021 | ||
3022 | case DW_OP_xor: | |
3023 | ax_simple (expr, aop_bit_xor); | |
3024 | break; | |
3025 | ||
3026 | case DW_OP_le: | |
3027 | /* Sign extend the operands. */ | |
3028 | ax_ext (expr, addr_size_bits); | |
3029 | ax_simple (expr, aop_swap); | |
3030 | ax_ext (expr, addr_size_bits); | |
3031 | /* Note no swap here: A <= B is !(B < A). */ | |
3032 | ax_simple (expr, aop_less_signed); | |
3033 | ax_simple (expr, aop_log_not); | |
3034 | break; | |
3035 | ||
3036 | case DW_OP_ge: | |
3037 | /* Sign extend the operands. */ | |
3038 | ax_ext (expr, addr_size_bits); | |
3039 | ax_simple (expr, aop_swap); | |
3040 | ax_ext (expr, addr_size_bits); | |
3041 | ax_simple (expr, aop_swap); | |
3042 | /* A >= B is !(A < B). */ | |
3043 | ax_simple (expr, aop_less_signed); | |
3044 | ax_simple (expr, aop_log_not); | |
3045 | break; | |
3046 | ||
3047 | case DW_OP_eq: | |
3048 | /* Sign extend the operands. */ | |
3049 | ax_ext (expr, addr_size_bits); | |
3050 | ax_simple (expr, aop_swap); | |
3051 | ax_ext (expr, addr_size_bits); | |
3052 | /* No need for a second swap here. */ | |
3053 | ax_simple (expr, aop_equal); | |
3054 | break; | |
3055 | ||
3056 | case DW_OP_lt: | |
3057 | /* Sign extend the operands. */ | |
3058 | ax_ext (expr, addr_size_bits); | |
3059 | ax_simple (expr, aop_swap); | |
3060 | ax_ext (expr, addr_size_bits); | |
3061 | ax_simple (expr, aop_swap); | |
3062 | ax_simple (expr, aop_less_signed); | |
3063 | break; | |
3064 | ||
3065 | case DW_OP_gt: | |
3066 | /* Sign extend the operands. */ | |
3067 | ax_ext (expr, addr_size_bits); | |
3068 | ax_simple (expr, aop_swap); | |
3069 | ax_ext (expr, addr_size_bits); | |
3070 | /* Note no swap here: A > B is B < A. */ | |
3071 | ax_simple (expr, aop_less_signed); | |
3072 | break; | |
3073 | ||
3074 | case DW_OP_ne: | |
3075 | /* Sign extend the operands. */ | |
3076 | ax_ext (expr, addr_size_bits); | |
3077 | ax_simple (expr, aop_swap); | |
3078 | ax_ext (expr, addr_size_bits); | |
3079 | /* No need for a swap here. */ | |
3080 | ax_simple (expr, aop_equal); | |
3081 | ax_simple (expr, aop_log_not); | |
3082 | break; | |
3083 | ||
3084 | case DW_OP_call_frame_cfa: | |
9f6f94ff TT |
3085 | dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu); |
3086 | loc->kind = axs_lvalue_memory; | |
3cf03773 TT |
3087 | break; |
3088 | ||
3089 | case DW_OP_GNU_push_tls_address: | |
3090 | unimplemented (op); | |
3091 | break; | |
3092 | ||
3093 | case DW_OP_skip: | |
3094 | offset = extract_signed_integer (op_ptr, 2, byte_order); | |
3095 | op_ptr += 2; | |
3096 | i = ax_goto (expr, aop_goto); | |
3097 | VEC_safe_push (int, dw_labels, op_ptr + offset - base); | |
3098 | VEC_safe_push (int, patches, i); | |
3099 | break; | |
3100 | ||
3101 | case DW_OP_bra: | |
3102 | offset = extract_signed_integer (op_ptr, 2, byte_order); | |
3103 | op_ptr += 2; | |
3104 | /* Zero extend the operand. */ | |
3105 | ax_zero_ext (expr, addr_size_bits); | |
3106 | i = ax_goto (expr, aop_if_goto); | |
3107 | VEC_safe_push (int, dw_labels, op_ptr + offset - base); | |
3108 | VEC_safe_push (int, patches, i); | |
3109 | break; | |
3110 | ||
3111 | case DW_OP_nop: | |
3112 | break; | |
3113 | ||
3114 | case DW_OP_piece: | |
3115 | case DW_OP_bit_piece: | |
08922a10 | 3116 | { |
9fccedf7 | 3117 | uint64_t size, offset; |
3cf03773 TT |
3118 | |
3119 | if (op_ptr - 1 == previous_piece) | |
3120 | error (_("Cannot translate empty pieces to agent expressions")); | |
3121 | previous_piece = op_ptr - 1; | |
3122 | ||
f664829e | 3123 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &size); |
3cf03773 TT |
3124 | if (op == DW_OP_piece) |
3125 | { | |
3126 | size *= 8; | |
3127 | offset = 0; | |
3128 | } | |
3129 | else | |
f664829e | 3130 | op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset); |
08922a10 | 3131 | |
3cf03773 TT |
3132 | if (bits_collected + size > 8 * sizeof (LONGEST)) |
3133 | error (_("Expression pieces exceed word size")); | |
3134 | ||
3135 | /* Access the bits. */ | |
3136 | switch (loc->kind) | |
3137 | { | |
3138 | case axs_lvalue_register: | |
3139 | ax_reg (expr, loc->u.reg); | |
3140 | break; | |
3141 | ||
3142 | case axs_lvalue_memory: | |
3143 | /* Offset the pointer, if needed. */ | |
3144 | if (offset > 8) | |
3145 | { | |
3146 | ax_const_l (expr, offset / 8); | |
3147 | ax_simple (expr, aop_add); | |
3148 | offset %= 8; | |
3149 | } | |
3150 | access_memory (arch, expr, size); | |
3151 | break; | |
3152 | } | |
3153 | ||
3154 | /* For a bits-big-endian target, shift up what we already | |
3155 | have. For a bits-little-endian target, shift up the | |
3156 | new data. Note that there is a potential bug here if | |
3157 | the DWARF expression leaves multiple values on the | |
3158 | stack. */ | |
3159 | if (bits_collected > 0) | |
3160 | { | |
3161 | if (bits_big_endian) | |
3162 | { | |
3163 | ax_simple (expr, aop_swap); | |
3164 | ax_const_l (expr, size); | |
3165 | ax_simple (expr, aop_lsh); | |
3166 | /* We don't need a second swap here, because | |
3167 | aop_bit_or is symmetric. */ | |
3168 | } | |
3169 | else | |
3170 | { | |
3171 | ax_const_l (expr, size); | |
3172 | ax_simple (expr, aop_lsh); | |
3173 | } | |
3174 | ax_simple (expr, aop_bit_or); | |
3175 | } | |
3176 | ||
3177 | bits_collected += size; | |
3178 | loc->kind = axs_rvalue; | |
08922a10 SS |
3179 | } |
3180 | break; | |
08922a10 | 3181 | |
3cf03773 TT |
3182 | case DW_OP_GNU_uninit: |
3183 | unimplemented (op); | |
3184 | ||
3185 | case DW_OP_call2: | |
3186 | case DW_OP_call4: | |
3187 | { | |
3188 | struct dwarf2_locexpr_baton block; | |
3189 | int size = (op == DW_OP_call2 ? 2 : 4); | |
b64f50a1 | 3190 | cu_offset offset; |
3cf03773 TT |
3191 | |
3192 | uoffset = extract_unsigned_integer (op_ptr, size, byte_order); | |
3193 | op_ptr += size; | |
3194 | ||
b64f50a1 JK |
3195 | offset.cu_off = uoffset; |
3196 | block = dwarf2_fetch_die_location_block (offset, per_cu, | |
8cf6f0b1 | 3197 | get_ax_pc, expr); |
3cf03773 TT |
3198 | |
3199 | /* DW_OP_call_ref is currently not supported. */ | |
3200 | gdb_assert (block.per_cu == per_cu); | |
3201 | ||
9f6f94ff TT |
3202 | dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, |
3203 | block.data, block.data + block.size, | |
3204 | per_cu); | |
3cf03773 TT |
3205 | } |
3206 | break; | |
3207 | ||
3208 | case DW_OP_call_ref: | |
3209 | unimplemented (op); | |
3210 | ||
3211 | default: | |
b1bfef65 | 3212 | unimplemented (op); |
08922a10 | 3213 | } |
08922a10 | 3214 | } |
3cf03773 TT |
3215 | |
3216 | /* Patch all the branches we emitted. */ | |
3217 | for (i = 0; i < VEC_length (int, patches); ++i) | |
3218 | { | |
3219 | int targ = offsets[VEC_index (int, dw_labels, i)]; | |
3220 | if (targ == -1) | |
3221 | internal_error (__FILE__, __LINE__, _("invalid label")); | |
3222 | ax_label (expr, VEC_index (int, patches, i), targ); | |
3223 | } | |
3224 | ||
3225 | do_cleanups (cleanups); | |
08922a10 SS |
3226 | } |
3227 | ||
4c2df51b DJ |
3228 | \f |
3229 | /* Return the value of SYMBOL in FRAME using the DWARF-2 expression | |
3230 | evaluator to calculate the location. */ | |
3231 | static struct value * | |
3232 | locexpr_read_variable (struct symbol *symbol, struct frame_info *frame) | |
3233 | { | |
3234 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
3235 | struct value *val; | |
9a619af0 | 3236 | |
a2d33775 JK |
3237 | val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data, |
3238 | dlbaton->size, dlbaton->per_cu); | |
4c2df51b DJ |
3239 | |
3240 | return val; | |
3241 | } | |
3242 | ||
e18b2753 JK |
3243 | /* Return the value of SYMBOL in FRAME at (callee) FRAME's function |
3244 | entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR | |
3245 | will be thrown. */ | |
3246 | ||
3247 | static struct value * | |
3248 | locexpr_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame) | |
3249 | { | |
3250 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
3251 | ||
3252 | return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, dlbaton->data, | |
3253 | dlbaton->size); | |
3254 | } | |
3255 | ||
4c2df51b DJ |
3256 | /* Return non-zero iff we need a frame to evaluate SYMBOL. */ |
3257 | static int | |
3258 | locexpr_read_needs_frame (struct symbol *symbol) | |
3259 | { | |
3260 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
9a619af0 | 3261 | |
ae0d2f24 UW |
3262 | return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size, |
3263 | dlbaton->per_cu); | |
4c2df51b DJ |
3264 | } |
3265 | ||
9eae7c52 TT |
3266 | /* Return true if DATA points to the end of a piece. END is one past |
3267 | the last byte in the expression. */ | |
3268 | ||
3269 | static int | |
3270 | piece_end_p (const gdb_byte *data, const gdb_byte *end) | |
3271 | { | |
3272 | return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece; | |
3273 | } | |
3274 | ||
5e44ecb3 TT |
3275 | /* Helper for locexpr_describe_location_piece that finds the name of a |
3276 | DWARF register. */ | |
3277 | ||
3278 | static const char * | |
3279 | locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum) | |
3280 | { | |
3281 | int regnum; | |
3282 | ||
3283 | regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum); | |
3284 | return gdbarch_register_name (gdbarch, regnum); | |
3285 | } | |
3286 | ||
9eae7c52 TT |
3287 | /* Nicely describe a single piece of a location, returning an updated |
3288 | position in the bytecode sequence. This function cannot recognize | |
3289 | all locations; if a location is not recognized, it simply returns | |
f664829e DE |
3290 | DATA. If there is an error during reading, e.g. we run off the end |
3291 | of the buffer, an error is thrown. */ | |
08922a10 | 3292 | |
0d45f56e | 3293 | static const gdb_byte * |
08922a10 SS |
3294 | locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream, |
3295 | CORE_ADDR addr, struct objfile *objfile, | |
49f6c839 | 3296 | struct dwarf2_per_cu_data *per_cu, |
9eae7c52 | 3297 | const gdb_byte *data, const gdb_byte *end, |
0d45f56e | 3298 | unsigned int addr_size) |
4c2df51b | 3299 | { |
08922a10 | 3300 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
49f6c839 | 3301 | size_t leb128_size; |
08922a10 SS |
3302 | |
3303 | if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31) | |
3304 | { | |
08922a10 | 3305 | fprintf_filtered (stream, _("a variable in $%s"), |
5e44ecb3 | 3306 | locexpr_regname (gdbarch, data[0] - DW_OP_reg0)); |
08922a10 SS |
3307 | data += 1; |
3308 | } | |
3309 | else if (data[0] == DW_OP_regx) | |
3310 | { | |
9fccedf7 | 3311 | uint64_t reg; |
4c2df51b | 3312 | |
f664829e | 3313 | data = safe_read_uleb128 (data + 1, end, ®); |
08922a10 | 3314 | fprintf_filtered (stream, _("a variable in $%s"), |
5e44ecb3 | 3315 | locexpr_regname (gdbarch, reg)); |
08922a10 SS |
3316 | } |
3317 | else if (data[0] == DW_OP_fbreg) | |
4c2df51b | 3318 | { |
08922a10 SS |
3319 | struct block *b; |
3320 | struct symbol *framefunc; | |
3321 | int frame_reg = 0; | |
9fccedf7 | 3322 | int64_t frame_offset; |
7155d578 | 3323 | const gdb_byte *base_data, *new_data, *save_data = data; |
08922a10 | 3324 | size_t base_size; |
9fccedf7 | 3325 | int64_t base_offset = 0; |
08922a10 | 3326 | |
f664829e | 3327 | new_data = safe_read_sleb128 (data + 1, end, &frame_offset); |
9eae7c52 TT |
3328 | if (!piece_end_p (new_data, end)) |
3329 | return data; | |
3330 | data = new_data; | |
3331 | ||
08922a10 SS |
3332 | b = block_for_pc (addr); |
3333 | ||
3334 | if (!b) | |
3335 | error (_("No block found for address for symbol \"%s\"."), | |
3336 | SYMBOL_PRINT_NAME (symbol)); | |
3337 | ||
3338 | framefunc = block_linkage_function (b); | |
3339 | ||
3340 | if (!framefunc) | |
3341 | error (_("No function found for block for symbol \"%s\"."), | |
3342 | SYMBOL_PRINT_NAME (symbol)); | |
3343 | ||
3344 | dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size); | |
3345 | ||
3346 | if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31) | |
3347 | { | |
0d45f56e | 3348 | const gdb_byte *buf_end; |
08922a10 SS |
3349 | |
3350 | frame_reg = base_data[0] - DW_OP_breg0; | |
f664829e DE |
3351 | buf_end = safe_read_sleb128 (base_data + 1, base_data + base_size, |
3352 | &base_offset); | |
08922a10 | 3353 | if (buf_end != base_data + base_size) |
3e43a32a MS |
3354 | error (_("Unexpected opcode after " |
3355 | "DW_OP_breg%u for symbol \"%s\"."), | |
08922a10 SS |
3356 | frame_reg, SYMBOL_PRINT_NAME (symbol)); |
3357 | } | |
3358 | else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31) | |
3359 | { | |
3360 | /* The frame base is just the register, with no offset. */ | |
3361 | frame_reg = base_data[0] - DW_OP_reg0; | |
3362 | base_offset = 0; | |
3363 | } | |
3364 | else | |
3365 | { | |
3366 | /* We don't know what to do with the frame base expression, | |
3367 | so we can't trace this variable; give up. */ | |
7155d578 | 3368 | return save_data; |
08922a10 SS |
3369 | } |
3370 | ||
3e43a32a MS |
3371 | fprintf_filtered (stream, |
3372 | _("a variable at frame base reg $%s offset %s+%s"), | |
5e44ecb3 | 3373 | locexpr_regname (gdbarch, frame_reg), |
08922a10 SS |
3374 | plongest (base_offset), plongest (frame_offset)); |
3375 | } | |
9eae7c52 TT |
3376 | else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31 |
3377 | && piece_end_p (data, end)) | |
08922a10 | 3378 | { |
9fccedf7 | 3379 | int64_t offset; |
08922a10 | 3380 | |
f664829e | 3381 | data = safe_read_sleb128 (data + 1, end, &offset); |
08922a10 | 3382 | |
4c2df51b | 3383 | fprintf_filtered (stream, |
08922a10 SS |
3384 | _("a variable at offset %s from base reg $%s"), |
3385 | plongest (offset), | |
5e44ecb3 | 3386 | locexpr_regname (gdbarch, data[0] - DW_OP_breg0)); |
4c2df51b DJ |
3387 | } |
3388 | ||
c3228f12 EZ |
3389 | /* The location expression for a TLS variable looks like this (on a |
3390 | 64-bit LE machine): | |
3391 | ||
3392 | DW_AT_location : 10 byte block: 3 4 0 0 0 0 0 0 0 e0 | |
3393 | (DW_OP_addr: 4; DW_OP_GNU_push_tls_address) | |
09d8bd00 | 3394 | |
c3228f12 EZ |
3395 | 0x3 is the encoding for DW_OP_addr, which has an operand as long |
3396 | as the size of an address on the target machine (here is 8 | |
09d8bd00 TT |
3397 | bytes). Note that more recent version of GCC emit DW_OP_const4u |
3398 | or DW_OP_const8u, depending on address size, rather than | |
0963b4bd MS |
3399 | DW_OP_addr. 0xe0 is the encoding for DW_OP_GNU_push_tls_address. |
3400 | The operand represents the offset at which the variable is within | |
3401 | the thread local storage. */ | |
c3228f12 | 3402 | |
9eae7c52 | 3403 | else if (data + 1 + addr_size < end |
09d8bd00 TT |
3404 | && (data[0] == DW_OP_addr |
3405 | || (addr_size == 4 && data[0] == DW_OP_const4u) | |
3406 | || (addr_size == 8 && data[0] == DW_OP_const8u)) | |
9eae7c52 TT |
3407 | && data[1 + addr_size] == DW_OP_GNU_push_tls_address |
3408 | && piece_end_p (data + 2 + addr_size, end)) | |
08922a10 | 3409 | { |
d4a087c7 UW |
3410 | ULONGEST offset; |
3411 | offset = extract_unsigned_integer (data + 1, addr_size, | |
3412 | gdbarch_byte_order (gdbarch)); | |
9a619af0 | 3413 | |
08922a10 | 3414 | fprintf_filtered (stream, |
d4a087c7 | 3415 | _("a thread-local variable at offset 0x%s " |
08922a10 | 3416 | "in the thread-local storage for `%s'"), |
d4a087c7 | 3417 | phex_nz (offset, addr_size), objfile->name); |
08922a10 SS |
3418 | |
3419 | data += 1 + addr_size + 1; | |
3420 | } | |
49f6c839 DE |
3421 | |
3422 | /* With -gsplit-dwarf a TLS variable can also look like this: | |
3423 | DW_AT_location : 3 byte block: fc 4 e0 | |
3424 | (DW_OP_GNU_const_index: 4; | |
3425 | DW_OP_GNU_push_tls_address) */ | |
3426 | else if (data + 3 <= end | |
3427 | && data + 1 + (leb128_size = skip_leb128 (data + 1, end)) < end | |
3428 | && data[0] == DW_OP_GNU_const_index | |
3429 | && leb128_size > 0 | |
3430 | && data[1 + leb128_size] == DW_OP_GNU_push_tls_address | |
3431 | && piece_end_p (data + 2 + leb128_size, end)) | |
3432 | { | |
3433 | ULONGEST offset; | |
3434 | ||
3435 | data = safe_read_uleb128 (data + 1, end, &offset); | |
3436 | offset = dwarf2_read_addr_index (per_cu, offset); | |
3437 | fprintf_filtered (stream, | |
3438 | _("a thread-local variable at offset 0x%s " | |
3439 | "in the thread-local storage for `%s'"), | |
3440 | phex_nz (offset, addr_size), objfile->name); | |
3441 | ++data; | |
3442 | } | |
3443 | ||
9eae7c52 TT |
3444 | else if (data[0] >= DW_OP_lit0 |
3445 | && data[0] <= DW_OP_lit31 | |
3446 | && data + 1 < end | |
3447 | && data[1] == DW_OP_stack_value) | |
3448 | { | |
3449 | fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0); | |
3450 | data += 2; | |
3451 | } | |
3452 | ||
3453 | return data; | |
3454 | } | |
3455 | ||
3456 | /* Disassemble an expression, stopping at the end of a piece or at the | |
3457 | end of the expression. Returns a pointer to the next unread byte | |
3458 | in the input expression. If ALL is nonzero, then this function | |
f664829e DE |
3459 | will keep going until it reaches the end of the expression. |
3460 | If there is an error during reading, e.g. we run off the end | |
3461 | of the buffer, an error is thrown. */ | |
9eae7c52 TT |
3462 | |
3463 | static const gdb_byte * | |
3464 | disassemble_dwarf_expression (struct ui_file *stream, | |
3465 | struct gdbarch *arch, unsigned int addr_size, | |
2bda9cc5 | 3466 | int offset_size, const gdb_byte *start, |
9eae7c52 | 3467 | const gdb_byte *data, const gdb_byte *end, |
2bda9cc5 | 3468 | int indent, int all, |
5e44ecb3 | 3469 | struct dwarf2_per_cu_data *per_cu) |
9eae7c52 | 3470 | { |
9eae7c52 TT |
3471 | while (data < end |
3472 | && (all | |
3473 | || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece))) | |
3474 | { | |
3475 | enum dwarf_location_atom op = *data++; | |
9fccedf7 DE |
3476 | uint64_t ul; |
3477 | int64_t l; | |
9eae7c52 TT |
3478 | const char *name; |
3479 | ||
f39c6ffd | 3480 | name = get_DW_OP_name (op); |
9eae7c52 TT |
3481 | |
3482 | if (!name) | |
3483 | error (_("Unrecognized DWARF opcode 0x%02x at %ld"), | |
06826322 | 3484 | op, (long) (data - 1 - start)); |
2bda9cc5 JK |
3485 | fprintf_filtered (stream, " %*ld: %s", indent + 4, |
3486 | (long) (data - 1 - start), name); | |
9eae7c52 TT |
3487 | |
3488 | switch (op) | |
3489 | { | |
3490 | case DW_OP_addr: | |
d4a087c7 UW |
3491 | ul = extract_unsigned_integer (data, addr_size, |
3492 | gdbarch_byte_order (arch)); | |
9eae7c52 | 3493 | data += addr_size; |
d4a087c7 | 3494 | fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size)); |
9eae7c52 TT |
3495 | break; |
3496 | ||
3497 | case DW_OP_const1u: | |
3498 | ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch)); | |
3499 | data += 1; | |
3500 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
3501 | break; | |
3502 | case DW_OP_const1s: | |
3503 | l = extract_signed_integer (data, 1, gdbarch_byte_order (arch)); | |
3504 | data += 1; | |
3505 | fprintf_filtered (stream, " %s", plongest (l)); | |
3506 | break; | |
3507 | case DW_OP_const2u: | |
3508 | ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch)); | |
3509 | data += 2; | |
3510 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
3511 | break; | |
3512 | case DW_OP_const2s: | |
3513 | l = extract_signed_integer (data, 2, gdbarch_byte_order (arch)); | |
3514 | data += 2; | |
3515 | fprintf_filtered (stream, " %s", plongest (l)); | |
3516 | break; | |
3517 | case DW_OP_const4u: | |
3518 | ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch)); | |
3519 | data += 4; | |
3520 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
3521 | break; | |
3522 | case DW_OP_const4s: | |
3523 | l = extract_signed_integer (data, 4, gdbarch_byte_order (arch)); | |
3524 | data += 4; | |
3525 | fprintf_filtered (stream, " %s", plongest (l)); | |
3526 | break; | |
3527 | case DW_OP_const8u: | |
3528 | ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch)); | |
3529 | data += 8; | |
3530 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
3531 | break; | |
3532 | case DW_OP_const8s: | |
3533 | l = extract_signed_integer (data, 8, gdbarch_byte_order (arch)); | |
3534 | data += 8; | |
3535 | fprintf_filtered (stream, " %s", plongest (l)); | |
3536 | break; | |
3537 | case DW_OP_constu: | |
f664829e | 3538 | data = safe_read_uleb128 (data, end, &ul); |
9eae7c52 TT |
3539 | fprintf_filtered (stream, " %s", pulongest (ul)); |
3540 | break; | |
3541 | case DW_OP_consts: | |
f664829e | 3542 | data = safe_read_sleb128 (data, end, &l); |
9eae7c52 TT |
3543 | fprintf_filtered (stream, " %s", plongest (l)); |
3544 | break; | |
3545 | ||
3546 | case DW_OP_reg0: | |
3547 | case DW_OP_reg1: | |
3548 | case DW_OP_reg2: | |
3549 | case DW_OP_reg3: | |
3550 | case DW_OP_reg4: | |
3551 | case DW_OP_reg5: | |
3552 | case DW_OP_reg6: | |
3553 | case DW_OP_reg7: | |
3554 | case DW_OP_reg8: | |
3555 | case DW_OP_reg9: | |
3556 | case DW_OP_reg10: | |
3557 | case DW_OP_reg11: | |
3558 | case DW_OP_reg12: | |
3559 | case DW_OP_reg13: | |
3560 | case DW_OP_reg14: | |
3561 | case DW_OP_reg15: | |
3562 | case DW_OP_reg16: | |
3563 | case DW_OP_reg17: | |
3564 | case DW_OP_reg18: | |
3565 | case DW_OP_reg19: | |
3566 | case DW_OP_reg20: | |
3567 | case DW_OP_reg21: | |
3568 | case DW_OP_reg22: | |
3569 | case DW_OP_reg23: | |
3570 | case DW_OP_reg24: | |
3571 | case DW_OP_reg25: | |
3572 | case DW_OP_reg26: | |
3573 | case DW_OP_reg27: | |
3574 | case DW_OP_reg28: | |
3575 | case DW_OP_reg29: | |
3576 | case DW_OP_reg30: | |
3577 | case DW_OP_reg31: | |
3578 | fprintf_filtered (stream, " [$%s]", | |
5e44ecb3 | 3579 | locexpr_regname (arch, op - DW_OP_reg0)); |
9eae7c52 TT |
3580 | break; |
3581 | ||
3582 | case DW_OP_regx: | |
f664829e | 3583 | data = safe_read_uleb128 (data, end, &ul); |
9eae7c52 | 3584 | fprintf_filtered (stream, " %s [$%s]", pulongest (ul), |
5e44ecb3 | 3585 | locexpr_regname (arch, (int) ul)); |
9eae7c52 TT |
3586 | break; |
3587 | ||
3588 | case DW_OP_implicit_value: | |
f664829e | 3589 | data = safe_read_uleb128 (data, end, &ul); |
9eae7c52 TT |
3590 | data += ul; |
3591 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
3592 | break; | |
3593 | ||
3594 | case DW_OP_breg0: | |
3595 | case DW_OP_breg1: | |
3596 | case DW_OP_breg2: | |
3597 | case DW_OP_breg3: | |
3598 | case DW_OP_breg4: | |
3599 | case DW_OP_breg5: | |
3600 | case DW_OP_breg6: | |
3601 | case DW_OP_breg7: | |
3602 | case DW_OP_breg8: | |
3603 | case DW_OP_breg9: | |
3604 | case DW_OP_breg10: | |
3605 | case DW_OP_breg11: | |
3606 | case DW_OP_breg12: | |
3607 | case DW_OP_breg13: | |
3608 | case DW_OP_breg14: | |
3609 | case DW_OP_breg15: | |
3610 | case DW_OP_breg16: | |
3611 | case DW_OP_breg17: | |
3612 | case DW_OP_breg18: | |
3613 | case DW_OP_breg19: | |
3614 | case DW_OP_breg20: | |
3615 | case DW_OP_breg21: | |
3616 | case DW_OP_breg22: | |
3617 | case DW_OP_breg23: | |
3618 | case DW_OP_breg24: | |
3619 | case DW_OP_breg25: | |
3620 | case DW_OP_breg26: | |
3621 | case DW_OP_breg27: | |
3622 | case DW_OP_breg28: | |
3623 | case DW_OP_breg29: | |
3624 | case DW_OP_breg30: | |
3625 | case DW_OP_breg31: | |
f664829e | 3626 | data = safe_read_sleb128 (data, end, &l); |
0502ed8c | 3627 | fprintf_filtered (stream, " %s [$%s]", plongest (l), |
5e44ecb3 | 3628 | locexpr_regname (arch, op - DW_OP_breg0)); |
9eae7c52 TT |
3629 | break; |
3630 | ||
3631 | case DW_OP_bregx: | |
f664829e DE |
3632 | data = safe_read_uleb128 (data, end, &ul); |
3633 | data = safe_read_sleb128 (data, end, &l); | |
0502ed8c JK |
3634 | fprintf_filtered (stream, " register %s [$%s] offset %s", |
3635 | pulongest (ul), | |
5e44ecb3 | 3636 | locexpr_regname (arch, (int) ul), |
0502ed8c | 3637 | plongest (l)); |
9eae7c52 TT |
3638 | break; |
3639 | ||
3640 | case DW_OP_fbreg: | |
f664829e | 3641 | data = safe_read_sleb128 (data, end, &l); |
0502ed8c | 3642 | fprintf_filtered (stream, " %s", plongest (l)); |
9eae7c52 TT |
3643 | break; |
3644 | ||
3645 | case DW_OP_xderef_size: | |
3646 | case DW_OP_deref_size: | |
3647 | case DW_OP_pick: | |
3648 | fprintf_filtered (stream, " %d", *data); | |
3649 | ++data; | |
3650 | break; | |
3651 | ||
3652 | case DW_OP_plus_uconst: | |
f664829e | 3653 | data = safe_read_uleb128 (data, end, &ul); |
9eae7c52 TT |
3654 | fprintf_filtered (stream, " %s", pulongest (ul)); |
3655 | break; | |
3656 | ||
3657 | case DW_OP_skip: | |
3658 | l = extract_signed_integer (data, 2, gdbarch_byte_order (arch)); | |
3659 | data += 2; | |
3660 | fprintf_filtered (stream, " to %ld", | |
3661 | (long) (data + l - start)); | |
3662 | break; | |
3663 | ||
3664 | case DW_OP_bra: | |
3665 | l = extract_signed_integer (data, 2, gdbarch_byte_order (arch)); | |
3666 | data += 2; | |
3667 | fprintf_filtered (stream, " %ld", | |
3668 | (long) (data + l - start)); | |
3669 | break; | |
3670 | ||
3671 | case DW_OP_call2: | |
3672 | ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch)); | |
3673 | data += 2; | |
3674 | fprintf_filtered (stream, " offset %s", phex_nz (ul, 2)); | |
3675 | break; | |
3676 | ||
3677 | case DW_OP_call4: | |
3678 | ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch)); | |
3679 | data += 4; | |
3680 | fprintf_filtered (stream, " offset %s", phex_nz (ul, 4)); | |
3681 | break; | |
3682 | ||
3683 | case DW_OP_call_ref: | |
3684 | ul = extract_unsigned_integer (data, offset_size, | |
3685 | gdbarch_byte_order (arch)); | |
3686 | data += offset_size; | |
3687 | fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size)); | |
3688 | break; | |
3689 | ||
3690 | case DW_OP_piece: | |
f664829e | 3691 | data = safe_read_uleb128 (data, end, &ul); |
9eae7c52 TT |
3692 | fprintf_filtered (stream, " %s (bytes)", pulongest (ul)); |
3693 | break; | |
3694 | ||
3695 | case DW_OP_bit_piece: | |
3696 | { | |
9fccedf7 | 3697 | uint64_t offset; |
9eae7c52 | 3698 | |
f664829e DE |
3699 | data = safe_read_uleb128 (data, end, &ul); |
3700 | data = safe_read_uleb128 (data, end, &offset); | |
9eae7c52 TT |
3701 | fprintf_filtered (stream, " size %s offset %s (bits)", |
3702 | pulongest (ul), pulongest (offset)); | |
3703 | } | |
3704 | break; | |
8cf6f0b1 TT |
3705 | |
3706 | case DW_OP_GNU_implicit_pointer: | |
3707 | { | |
3708 | ul = extract_unsigned_integer (data, offset_size, | |
3709 | gdbarch_byte_order (arch)); | |
3710 | data += offset_size; | |
3711 | ||
f664829e | 3712 | data = safe_read_sleb128 (data, end, &l); |
8cf6f0b1 TT |
3713 | |
3714 | fprintf_filtered (stream, " DIE %s offset %s", | |
3715 | phex_nz (ul, offset_size), | |
3716 | plongest (l)); | |
3717 | } | |
3718 | break; | |
5e44ecb3 TT |
3719 | |
3720 | case DW_OP_GNU_deref_type: | |
3721 | { | |
3722 | int addr_size = *data++; | |
b64f50a1 | 3723 | cu_offset offset; |
5e44ecb3 TT |
3724 | struct type *type; |
3725 | ||
f664829e | 3726 | data = safe_read_uleb128 (data, end, &ul); |
b64f50a1 | 3727 | offset.cu_off = ul; |
5e44ecb3 TT |
3728 | type = dwarf2_get_die_type (offset, per_cu); |
3729 | fprintf_filtered (stream, "<"); | |
3730 | type_print (type, "", stream, -1); | |
b64f50a1 | 3731 | fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset.cu_off, 0), |
5e44ecb3 TT |
3732 | addr_size); |
3733 | } | |
3734 | break; | |
3735 | ||
3736 | case DW_OP_GNU_const_type: | |
3737 | { | |
b64f50a1 | 3738 | cu_offset type_die; |
5e44ecb3 TT |
3739 | struct type *type; |
3740 | ||
f664829e | 3741 | data = safe_read_uleb128 (data, end, &ul); |
b64f50a1 | 3742 | type_die.cu_off = ul; |
5e44ecb3 TT |
3743 | type = dwarf2_get_die_type (type_die, per_cu); |
3744 | fprintf_filtered (stream, "<"); | |
3745 | type_print (type, "", stream, -1); | |
b64f50a1 | 3746 | fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0)); |
5e44ecb3 TT |
3747 | } |
3748 | break; | |
3749 | ||
3750 | case DW_OP_GNU_regval_type: | |
3751 | { | |
9fccedf7 | 3752 | uint64_t reg; |
b64f50a1 | 3753 | cu_offset type_die; |
5e44ecb3 TT |
3754 | struct type *type; |
3755 | ||
f664829e DE |
3756 | data = safe_read_uleb128 (data, end, ®); |
3757 | data = safe_read_uleb128 (data, end, &ul); | |
b64f50a1 | 3758 | type_die.cu_off = ul; |
5e44ecb3 TT |
3759 | |
3760 | type = dwarf2_get_die_type (type_die, per_cu); | |
3761 | fprintf_filtered (stream, "<"); | |
3762 | type_print (type, "", stream, -1); | |
b64f50a1 JK |
3763 | fprintf_filtered (stream, " [0x%s]> [$%s]", |
3764 | phex_nz (type_die.cu_off, 0), | |
5e44ecb3 TT |
3765 | locexpr_regname (arch, reg)); |
3766 | } | |
3767 | break; | |
3768 | ||
3769 | case DW_OP_GNU_convert: | |
3770 | case DW_OP_GNU_reinterpret: | |
3771 | { | |
b64f50a1 | 3772 | cu_offset type_die; |
5e44ecb3 | 3773 | |
f664829e | 3774 | data = safe_read_uleb128 (data, end, &ul); |
b64f50a1 | 3775 | type_die.cu_off = ul; |
5e44ecb3 | 3776 | |
b64f50a1 | 3777 | if (type_die.cu_off == 0) |
5e44ecb3 TT |
3778 | fprintf_filtered (stream, "<0>"); |
3779 | else | |
3780 | { | |
3781 | struct type *type; | |
3782 | ||
3783 | type = dwarf2_get_die_type (type_die, per_cu); | |
3784 | fprintf_filtered (stream, "<"); | |
3785 | type_print (type, "", stream, -1); | |
b64f50a1 | 3786 | fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die.cu_off, 0)); |
5e44ecb3 TT |
3787 | } |
3788 | } | |
3789 | break; | |
2bda9cc5 JK |
3790 | |
3791 | case DW_OP_GNU_entry_value: | |
f664829e | 3792 | data = safe_read_uleb128 (data, end, &ul); |
2bda9cc5 JK |
3793 | fputc_filtered ('\n', stream); |
3794 | disassemble_dwarf_expression (stream, arch, addr_size, offset_size, | |
3795 | start, data, data + ul, indent + 2, | |
3796 | all, per_cu); | |
3797 | data += ul; | |
3798 | continue; | |
49f6c839 DE |
3799 | |
3800 | case DW_OP_GNU_addr_index: | |
3801 | data = safe_read_uleb128 (data, end, &ul); | |
3802 | ul = dwarf2_read_addr_index (per_cu, ul); | |
3803 | fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size)); | |
3804 | break; | |
3805 | case DW_OP_GNU_const_index: | |
3806 | data = safe_read_uleb128 (data, end, &ul); | |
3807 | ul = dwarf2_read_addr_index (per_cu, ul); | |
3808 | fprintf_filtered (stream, " %s", pulongest (ul)); | |
3809 | break; | |
9eae7c52 TT |
3810 | } |
3811 | ||
3812 | fprintf_filtered (stream, "\n"); | |
3813 | } | |
c3228f12 | 3814 | |
08922a10 | 3815 | return data; |
4c2df51b DJ |
3816 | } |
3817 | ||
08922a10 SS |
3818 | /* Describe a single location, which may in turn consist of multiple |
3819 | pieces. */ | |
a55cc764 | 3820 | |
08922a10 SS |
3821 | static void |
3822 | locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr, | |
0d45f56e TT |
3823 | struct ui_file *stream, |
3824 | const gdb_byte *data, int size, | |
9eae7c52 | 3825 | struct objfile *objfile, unsigned int addr_size, |
5e44ecb3 | 3826 | int offset_size, struct dwarf2_per_cu_data *per_cu) |
08922a10 | 3827 | { |
0d45f56e | 3828 | const gdb_byte *end = data + size; |
9eae7c52 | 3829 | int first_piece = 1, bad = 0; |
08922a10 | 3830 | |
08922a10 SS |
3831 | while (data < end) |
3832 | { | |
9eae7c52 TT |
3833 | const gdb_byte *here = data; |
3834 | int disassemble = 1; | |
3835 | ||
3836 | if (first_piece) | |
3837 | first_piece = 0; | |
3838 | else | |
3839 | fprintf_filtered (stream, _(", and ")); | |
08922a10 | 3840 | |
9eae7c52 TT |
3841 | if (!dwarf2_always_disassemble) |
3842 | { | |
3e43a32a | 3843 | data = locexpr_describe_location_piece (symbol, stream, |
49f6c839 | 3844 | addr, objfile, per_cu, |
9eae7c52 TT |
3845 | data, end, addr_size); |
3846 | /* If we printed anything, or if we have an empty piece, | |
3847 | then don't disassemble. */ | |
3848 | if (data != here | |
3849 | || data[0] == DW_OP_piece | |
3850 | || data[0] == DW_OP_bit_piece) | |
3851 | disassemble = 0; | |
08922a10 | 3852 | } |
9eae7c52 | 3853 | if (disassemble) |
2bda9cc5 JK |
3854 | { |
3855 | fprintf_filtered (stream, _("a complex DWARF expression:\n")); | |
3856 | data = disassemble_dwarf_expression (stream, | |
3857 | get_objfile_arch (objfile), | |
3858 | addr_size, offset_size, data, | |
3859 | data, end, 0, | |
3860 | dwarf2_always_disassemble, | |
3861 | per_cu); | |
3862 | } | |
9eae7c52 TT |
3863 | |
3864 | if (data < end) | |
08922a10 | 3865 | { |
9eae7c52 | 3866 | int empty = data == here; |
08922a10 | 3867 | |
9eae7c52 TT |
3868 | if (disassemble) |
3869 | fprintf_filtered (stream, " "); | |
3870 | if (data[0] == DW_OP_piece) | |
3871 | { | |
9fccedf7 | 3872 | uint64_t bytes; |
08922a10 | 3873 | |
f664829e | 3874 | data = safe_read_uleb128 (data + 1, end, &bytes); |
08922a10 | 3875 | |
9eae7c52 TT |
3876 | if (empty) |
3877 | fprintf_filtered (stream, _("an empty %s-byte piece"), | |
3878 | pulongest (bytes)); | |
3879 | else | |
3880 | fprintf_filtered (stream, _(" [%s-byte piece]"), | |
3881 | pulongest (bytes)); | |
3882 | } | |
3883 | else if (data[0] == DW_OP_bit_piece) | |
3884 | { | |
9fccedf7 | 3885 | uint64_t bits, offset; |
9eae7c52 | 3886 | |
f664829e DE |
3887 | data = safe_read_uleb128 (data + 1, end, &bits); |
3888 | data = safe_read_uleb128 (data, end, &offset); | |
9eae7c52 TT |
3889 | |
3890 | if (empty) | |
3891 | fprintf_filtered (stream, | |
3892 | _("an empty %s-bit piece"), | |
3893 | pulongest (bits)); | |
3894 | else | |
3895 | fprintf_filtered (stream, | |
3896 | _(" [%s-bit piece, offset %s bits]"), | |
3897 | pulongest (bits), pulongest (offset)); | |
3898 | } | |
3899 | else | |
3900 | { | |
3901 | bad = 1; | |
3902 | break; | |
3903 | } | |
08922a10 SS |
3904 | } |
3905 | } | |
3906 | ||
3907 | if (bad || data > end) | |
3908 | error (_("Corrupted DWARF2 expression for \"%s\"."), | |
3909 | SYMBOL_PRINT_NAME (symbol)); | |
3910 | } | |
3911 | ||
3912 | /* Print a natural-language description of SYMBOL to STREAM. This | |
3913 | version is for a symbol with a single location. */ | |
a55cc764 | 3914 | |
08922a10 SS |
3915 | static void |
3916 | locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr, | |
3917 | struct ui_file *stream) | |
3918 | { | |
3919 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
3920 | struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu); | |
3921 | unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu); | |
9eae7c52 | 3922 | int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu); |
08922a10 | 3923 | |
3e43a32a MS |
3924 | locexpr_describe_location_1 (symbol, addr, stream, |
3925 | dlbaton->data, dlbaton->size, | |
5e44ecb3 TT |
3926 | objfile, addr_size, offset_size, |
3927 | dlbaton->per_cu); | |
08922a10 SS |
3928 | } |
3929 | ||
3930 | /* Describe the location of SYMBOL as an agent value in VALUE, generating | |
3931 | any necessary bytecode in AX. */ | |
a55cc764 | 3932 | |
0d53c4c4 | 3933 | static void |
505e835d UW |
3934 | locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch, |
3935 | struct agent_expr *ax, struct axs_value *value) | |
a55cc764 DJ |
3936 | { |
3937 | struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
3cf03773 | 3938 | unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu); |
a55cc764 | 3939 | |
1d6edc3c | 3940 | if (dlbaton->size == 0) |
cabe9ab6 PA |
3941 | value->optimized_out = 1; |
3942 | else | |
9f6f94ff TT |
3943 | dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, |
3944 | dlbaton->data, dlbaton->data + dlbaton->size, | |
3945 | dlbaton->per_cu); | |
a55cc764 DJ |
3946 | } |
3947 | ||
4c2df51b DJ |
3948 | /* The set of location functions used with the DWARF-2 expression |
3949 | evaluator. */ | |
768a979c | 3950 | const struct symbol_computed_ops dwarf2_locexpr_funcs = { |
4c2df51b | 3951 | locexpr_read_variable, |
e18b2753 | 3952 | locexpr_read_variable_at_entry, |
4c2df51b DJ |
3953 | locexpr_read_needs_frame, |
3954 | locexpr_describe_location, | |
a55cc764 | 3955 | locexpr_tracepoint_var_ref |
4c2df51b | 3956 | }; |
0d53c4c4 DJ |
3957 | |
3958 | ||
3959 | /* Wrapper functions for location lists. These generally find | |
3960 | the appropriate location expression and call something above. */ | |
3961 | ||
3962 | /* Return the value of SYMBOL in FRAME using the DWARF-2 expression | |
3963 | evaluator to calculate the location. */ | |
3964 | static struct value * | |
3965 | loclist_read_variable (struct symbol *symbol, struct frame_info *frame) | |
3966 | { | |
3967 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
3968 | struct value *val; | |
947bb88f | 3969 | const gdb_byte *data; |
b6b08ebf | 3970 | size_t size; |
8cf6f0b1 | 3971 | CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0; |
0d53c4c4 | 3972 | |
8cf6f0b1 | 3973 | data = dwarf2_find_location_expression (dlbaton, &size, pc); |
1d6edc3c JK |
3974 | val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size, |
3975 | dlbaton->per_cu); | |
0d53c4c4 DJ |
3976 | |
3977 | return val; | |
3978 | } | |
3979 | ||
e18b2753 JK |
3980 | /* Read variable SYMBOL like loclist_read_variable at (callee) FRAME's function |
3981 | entry. SYMBOL should be a function parameter, otherwise NO_ENTRY_VALUE_ERROR | |
3982 | will be thrown. | |
3983 | ||
3984 | Function always returns non-NULL value, it may be marked optimized out if | |
3985 | inferior frame information is not available. It throws NO_ENTRY_VALUE_ERROR | |
3986 | if it cannot resolve the parameter for any reason. */ | |
3987 | ||
3988 | static struct value * | |
3989 | loclist_read_variable_at_entry (struct symbol *symbol, struct frame_info *frame) | |
3990 | { | |
3991 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
3992 | const gdb_byte *data; | |
3993 | size_t size; | |
3994 | CORE_ADDR pc; | |
3995 | ||
3996 | if (frame == NULL || !get_frame_func_if_available (frame, &pc)) | |
3997 | return allocate_optimized_out_value (SYMBOL_TYPE (symbol)); | |
3998 | ||
3999 | data = dwarf2_find_location_expression (dlbaton, &size, pc); | |
4000 | if (data == NULL) | |
4001 | return allocate_optimized_out_value (SYMBOL_TYPE (symbol)); | |
4002 | ||
4003 | return value_of_dwarf_block_entry (SYMBOL_TYPE (symbol), frame, data, size); | |
4004 | } | |
4005 | ||
0d53c4c4 DJ |
4006 | /* Return non-zero iff we need a frame to evaluate SYMBOL. */ |
4007 | static int | |
4008 | loclist_read_needs_frame (struct symbol *symbol) | |
4009 | { | |
4010 | /* If there's a location list, then assume we need to have a frame | |
4011 | to choose the appropriate location expression. With tracking of | |
4012 | global variables this is not necessarily true, but such tracking | |
4013 | is disabled in GCC at the moment until we figure out how to | |
4014 | represent it. */ | |
4015 | ||
4016 | return 1; | |
4017 | } | |
4018 | ||
08922a10 SS |
4019 | /* Print a natural-language description of SYMBOL to STREAM. This |
4020 | version applies when there is a list of different locations, each | |
4021 | with a specified address range. */ | |
4022 | ||
4023 | static void | |
4024 | loclist_describe_location (struct symbol *symbol, CORE_ADDR addr, | |
4025 | struct ui_file *stream) | |
0d53c4c4 | 4026 | { |
08922a10 | 4027 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); |
947bb88f | 4028 | const gdb_byte *loc_ptr, *buf_end; |
f664829e | 4029 | int first = 1; |
08922a10 SS |
4030 | struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu); |
4031 | struct gdbarch *gdbarch = get_objfile_arch (objfile); | |
4032 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
4033 | unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu); | |
9eae7c52 | 4034 | int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu); |
d4a087c7 | 4035 | int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd); |
08922a10 | 4036 | /* Adjust base_address for relocatable objects. */ |
9aa1f1e3 | 4037 | CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu); |
08922a10 | 4038 | CORE_ADDR base_address = dlbaton->base_address + base_offset; |
f664829e | 4039 | int done = 0; |
08922a10 SS |
4040 | |
4041 | loc_ptr = dlbaton->data; | |
4042 | buf_end = dlbaton->data + dlbaton->size; | |
4043 | ||
9eae7c52 | 4044 | fprintf_filtered (stream, _("multi-location:\n")); |
08922a10 SS |
4045 | |
4046 | /* Iterate through locations until we run out. */ | |
f664829e | 4047 | while (!done) |
08922a10 | 4048 | { |
f664829e DE |
4049 | CORE_ADDR low = 0, high = 0; /* init for gcc -Wall */ |
4050 | int length; | |
4051 | enum debug_loc_kind kind; | |
4052 | const gdb_byte *new_ptr = NULL; /* init for gcc -Wall */ | |
4053 | ||
4054 | if (dlbaton->from_dwo) | |
4055 | kind = decode_debug_loc_dwo_addresses (dlbaton->per_cu, | |
4056 | loc_ptr, buf_end, &new_ptr, | |
3771a44c | 4057 | &low, &high, byte_order); |
d4a087c7 | 4058 | else |
f664829e DE |
4059 | kind = decode_debug_loc_addresses (loc_ptr, buf_end, &new_ptr, |
4060 | &low, &high, | |
4061 | byte_order, addr_size, | |
4062 | signed_addr_p); | |
4063 | loc_ptr = new_ptr; | |
4064 | switch (kind) | |
08922a10 | 4065 | { |
f664829e DE |
4066 | case DEBUG_LOC_END_OF_LIST: |
4067 | done = 1; | |
4068 | continue; | |
4069 | case DEBUG_LOC_BASE_ADDRESS: | |
d4a087c7 | 4070 | base_address = high + base_offset; |
9eae7c52 | 4071 | fprintf_filtered (stream, _(" Base address %s"), |
08922a10 | 4072 | paddress (gdbarch, base_address)); |
08922a10 | 4073 | continue; |
3771a44c DE |
4074 | case DEBUG_LOC_START_END: |
4075 | case DEBUG_LOC_START_LENGTH: | |
f664829e DE |
4076 | break; |
4077 | case DEBUG_LOC_BUFFER_OVERFLOW: | |
4078 | case DEBUG_LOC_INVALID_ENTRY: | |
4079 | error (_("Corrupted DWARF expression for symbol \"%s\"."), | |
4080 | SYMBOL_PRINT_NAME (symbol)); | |
4081 | default: | |
4082 | gdb_assert_not_reached ("bad debug_loc_kind"); | |
08922a10 SS |
4083 | } |
4084 | ||
08922a10 SS |
4085 | /* Otherwise, a location expression entry. */ |
4086 | low += base_address; | |
4087 | high += base_address; | |
4088 | ||
4089 | length = extract_unsigned_integer (loc_ptr, 2, byte_order); | |
4090 | loc_ptr += 2; | |
4091 | ||
08922a10 SS |
4092 | /* (It would improve readability to print only the minimum |
4093 | necessary digits of the second number of the range.) */ | |
9eae7c52 | 4094 | fprintf_filtered (stream, _(" Range %s-%s: "), |
08922a10 SS |
4095 | paddress (gdbarch, low), paddress (gdbarch, high)); |
4096 | ||
4097 | /* Now describe this particular location. */ | |
4098 | locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length, | |
5e44ecb3 TT |
4099 | objfile, addr_size, offset_size, |
4100 | dlbaton->per_cu); | |
9eae7c52 TT |
4101 | |
4102 | fprintf_filtered (stream, "\n"); | |
08922a10 SS |
4103 | |
4104 | loc_ptr += length; | |
4105 | } | |
0d53c4c4 DJ |
4106 | } |
4107 | ||
4108 | /* Describe the location of SYMBOL as an agent value in VALUE, generating | |
4109 | any necessary bytecode in AX. */ | |
4110 | static void | |
505e835d UW |
4111 | loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch, |
4112 | struct agent_expr *ax, struct axs_value *value) | |
0d53c4c4 DJ |
4113 | { |
4114 | struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol); | |
947bb88f | 4115 | const gdb_byte *data; |
b6b08ebf | 4116 | size_t size; |
3cf03773 | 4117 | unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu); |
0d53c4c4 | 4118 | |
8cf6f0b1 | 4119 | data = dwarf2_find_location_expression (dlbaton, &size, ax->scope); |
1d6edc3c | 4120 | if (size == 0) |
cabe9ab6 PA |
4121 | value->optimized_out = 1; |
4122 | else | |
9f6f94ff TT |
4123 | dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size, |
4124 | dlbaton->per_cu); | |
0d53c4c4 DJ |
4125 | } |
4126 | ||
4127 | /* The set of location functions used with the DWARF-2 expression | |
4128 | evaluator and location lists. */ | |
768a979c | 4129 | const struct symbol_computed_ops dwarf2_loclist_funcs = { |
0d53c4c4 | 4130 | loclist_read_variable, |
e18b2753 | 4131 | loclist_read_variable_at_entry, |
0d53c4c4 DJ |
4132 | loclist_read_needs_frame, |
4133 | loclist_describe_location, | |
4134 | loclist_tracepoint_var_ref | |
4135 | }; | |
8e3b41a9 | 4136 | |
70221824 PA |
4137 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
4138 | extern initialize_file_ftype _initialize_dwarf2loc; | |
4139 | ||
8e3b41a9 JK |
4140 | void |
4141 | _initialize_dwarf2loc (void) | |
4142 | { | |
4143 | add_setshow_zinteger_cmd ("entry-values", class_maintenance, | |
4144 | &entry_values_debug, | |
4145 | _("Set entry values and tail call frames " | |
4146 | "debugging."), | |
4147 | _("Show entry values and tail call frames " | |
4148 | "debugging."), | |
4149 | _("When non-zero, the process of determining " | |
4150 | "parameter values from function entry point " | |
4151 | "and tail call frames will be printed."), | |
4152 | NULL, | |
4153 | show_entry_values_debug, | |
4154 | &setdebuglist, &showdebuglist); | |
4155 | } |