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