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