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