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