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